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/Serialization/ASTReader.h" 14 #include "ASTCommon.h" 15 #include "ASTReaderInternals.h" 16 #include "clang/AST/ASTConsumer.h" 17 #include "clang/AST/ASTContext.h" 18 #include "clang/AST/ASTMutationListener.h" 19 #include "clang/AST/ASTUnresolvedSet.h" 20 #include "clang/AST/Decl.h" 21 #include "clang/AST/DeclBase.h" 22 #include "clang/AST/DeclCXX.h" 23 #include "clang/AST/DeclFriend.h" 24 #include "clang/AST/DeclGroup.h" 25 #include "clang/AST/DeclObjC.h" 26 #include "clang/AST/DeclTemplate.h" 27 #include "clang/AST/DeclarationName.h" 28 #include "clang/AST/Expr.h" 29 #include "clang/AST/ExprCXX.h" 30 #include "clang/AST/ExternalASTSource.h" 31 #include "clang/AST/NestedNameSpecifier.h" 32 #include "clang/AST/ODRHash.h" 33 #include "clang/AST/RawCommentList.h" 34 #include "clang/AST/TemplateBase.h" 35 #include "clang/AST/TemplateName.h" 36 #include "clang/AST/Type.h" 37 #include "clang/AST/TypeLoc.h" 38 #include "clang/AST/TypeLocVisitor.h" 39 #include "clang/AST/UnresolvedSet.h" 40 #include "clang/Basic/CommentOptions.h" 41 #include "clang/Basic/Diagnostic.h" 42 #include "clang/Basic/DiagnosticOptions.h" 43 #include "clang/Basic/ExceptionSpecificationType.h" 44 #include "clang/Basic/FileManager.h" 45 #include "clang/Basic/FileSystemOptions.h" 46 #include "clang/Basic/IdentifierTable.h" 47 #include "clang/Basic/LLVM.h" 48 #include "clang/Basic/LangOptions.h" 49 #include "clang/Basic/Module.h" 50 #include "clang/Basic/ObjCRuntime.h" 51 #include "clang/Basic/OperatorKinds.h" 52 #include "clang/Basic/PragmaKinds.h" 53 #include "clang/Basic/Sanitizers.h" 54 #include "clang/Basic/SourceLocation.h" 55 #include "clang/Basic/SourceManager.h" 56 #include "clang/Basic/SourceManagerInternals.h" 57 #include "clang/Basic/Specifiers.h" 58 #include "clang/Basic/TargetInfo.h" 59 #include "clang/Basic/TargetOptions.h" 60 #include "clang/Basic/TokenKinds.h" 61 #include "clang/Basic/Version.h" 62 #include "clang/Lex/HeaderSearch.h" 63 #include "clang/Lex/HeaderSearchOptions.h" 64 #include "clang/Lex/MacroInfo.h" 65 #include "clang/Lex/ModuleMap.h" 66 #include "clang/Lex/PreprocessingRecord.h" 67 #include "clang/Lex/Preprocessor.h" 68 #include "clang/Lex/PreprocessorOptions.h" 69 #include "clang/Lex/Token.h" 70 #include "clang/Sema/ObjCMethodList.h" 71 #include "clang/Sema/Scope.h" 72 #include "clang/Sema/Sema.h" 73 #include "clang/Sema/Weak.h" 74 #include "clang/Serialization/ASTBitCodes.h" 75 #include "clang/Serialization/ASTDeserializationListener.h" 76 #include "clang/Serialization/ContinuousRangeMap.h" 77 #include "clang/Serialization/GlobalModuleIndex.h" 78 #include "clang/Serialization/InMemoryModuleCache.h" 79 #include "clang/Serialization/Module.h" 80 #include "clang/Serialization/ModuleFileExtension.h" 81 #include "clang/Serialization/ModuleManager.h" 82 #include "clang/Serialization/PCHContainerOperations.h" 83 #include "clang/Serialization/SerializationDiagnostic.h" 84 #include "llvm/ADT/APFloat.h" 85 #include "llvm/ADT/APInt.h" 86 #include "llvm/ADT/APSInt.h" 87 #include "llvm/ADT/ArrayRef.h" 88 #include "llvm/ADT/DenseMap.h" 89 #include "llvm/ADT/FoldingSet.h" 90 #include "llvm/ADT/Hashing.h" 91 #include "llvm/ADT/IntrusiveRefCntPtr.h" 92 #include "llvm/ADT/None.h" 93 #include "llvm/ADT/Optional.h" 94 #include "llvm/ADT/STLExtras.h" 95 #include "llvm/ADT/ScopeExit.h" 96 #include "llvm/ADT/SmallPtrSet.h" 97 #include "llvm/ADT/SmallString.h" 98 #include "llvm/ADT/SmallVector.h" 99 #include "llvm/ADT/StringExtras.h" 100 #include "llvm/ADT/StringMap.h" 101 #include "llvm/ADT/StringRef.h" 102 #include "llvm/ADT/Triple.h" 103 #include "llvm/ADT/iterator_range.h" 104 #include "llvm/Bitcode/BitstreamReader.h" 105 #include "llvm/Support/Casting.h" 106 #include "llvm/Support/Compiler.h" 107 #include "llvm/Support/Compression.h" 108 #include "llvm/Support/DJB.h" 109 #include "llvm/Support/Endian.h" 110 #include "llvm/Support/Error.h" 111 #include "llvm/Support/ErrorHandling.h" 112 #include "llvm/Support/FileSystem.h" 113 #include "llvm/Support/MemoryBuffer.h" 114 #include "llvm/Support/Path.h" 115 #include "llvm/Support/SaveAndRestore.h" 116 #include "llvm/Support/Timer.h" 117 #include "llvm/Support/VersionTuple.h" 118 #include "llvm/Support/raw_ostream.h" 119 #include <algorithm> 120 #include <cassert> 121 #include <cstddef> 122 #include <cstdint> 123 #include <cstdio> 124 #include <ctime> 125 #include <iterator> 126 #include <limits> 127 #include <map> 128 #include <memory> 129 #include <string> 130 #include <system_error> 131 #include <tuple> 132 #include <utility> 133 #include <vector> 134 135 using namespace clang; 136 using namespace clang::serialization; 137 using namespace clang::serialization::reader; 138 using llvm::BitstreamCursor; 139 140 //===----------------------------------------------------------------------===// 141 // ChainedASTReaderListener implementation 142 //===----------------------------------------------------------------------===// 143 144 bool 145 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 146 return First->ReadFullVersionInformation(FullVersion) || 147 Second->ReadFullVersionInformation(FullVersion); 148 } 149 150 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 151 First->ReadModuleName(ModuleName); 152 Second->ReadModuleName(ModuleName); 153 } 154 155 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 156 First->ReadModuleMapFile(ModuleMapPath); 157 Second->ReadModuleMapFile(ModuleMapPath); 158 } 159 160 bool 161 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 162 bool Complain, 163 bool AllowCompatibleDifferences) { 164 return First->ReadLanguageOptions(LangOpts, Complain, 165 AllowCompatibleDifferences) || 166 Second->ReadLanguageOptions(LangOpts, Complain, 167 AllowCompatibleDifferences); 168 } 169 170 bool ChainedASTReaderListener::ReadTargetOptions( 171 const TargetOptions &TargetOpts, bool Complain, 172 bool AllowCompatibleDifferences) { 173 return First->ReadTargetOptions(TargetOpts, Complain, 174 AllowCompatibleDifferences) || 175 Second->ReadTargetOptions(TargetOpts, Complain, 176 AllowCompatibleDifferences); 177 } 178 179 bool ChainedASTReaderListener::ReadDiagnosticOptions( 180 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 181 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 182 Second->ReadDiagnosticOptions(DiagOpts, Complain); 183 } 184 185 bool 186 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 187 bool Complain) { 188 return First->ReadFileSystemOptions(FSOpts, Complain) || 189 Second->ReadFileSystemOptions(FSOpts, Complain); 190 } 191 192 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 193 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 194 bool Complain) { 195 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 196 Complain) || 197 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 198 Complain); 199 } 200 201 bool ChainedASTReaderListener::ReadPreprocessorOptions( 202 const PreprocessorOptions &PPOpts, bool Complain, 203 std::string &SuggestedPredefines) { 204 return First->ReadPreprocessorOptions(PPOpts, Complain, 205 SuggestedPredefines) || 206 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 207 } 208 209 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 210 unsigned Value) { 211 First->ReadCounter(M, Value); 212 Second->ReadCounter(M, Value); 213 } 214 215 bool ChainedASTReaderListener::needsInputFileVisitation() { 216 return First->needsInputFileVisitation() || 217 Second->needsInputFileVisitation(); 218 } 219 220 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 221 return First->needsSystemInputFileVisitation() || 222 Second->needsSystemInputFileVisitation(); 223 } 224 225 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 226 ModuleKind Kind) { 227 First->visitModuleFile(Filename, Kind); 228 Second->visitModuleFile(Filename, Kind); 229 } 230 231 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 232 bool isSystem, 233 bool isOverridden, 234 bool isExplicitModule) { 235 bool Continue = false; 236 if (First->needsInputFileVisitation() && 237 (!isSystem || First->needsSystemInputFileVisitation())) 238 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 239 isExplicitModule); 240 if (Second->needsInputFileVisitation() && 241 (!isSystem || Second->needsSystemInputFileVisitation())) 242 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 243 isExplicitModule); 244 return Continue; 245 } 246 247 void ChainedASTReaderListener::readModuleFileExtension( 248 const ModuleFileExtensionMetadata &Metadata) { 249 First->readModuleFileExtension(Metadata); 250 Second->readModuleFileExtension(Metadata); 251 } 252 253 //===----------------------------------------------------------------------===// 254 // PCH validator implementation 255 //===----------------------------------------------------------------------===// 256 257 ASTReaderListener::~ASTReaderListener() = default; 258 259 /// Compare the given set of language options against an existing set of 260 /// language options. 261 /// 262 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 263 /// \param AllowCompatibleDifferences If true, differences between compatible 264 /// language options will be permitted. 265 /// 266 /// \returns true if the languagae options mis-match, false otherwise. 267 static bool checkLanguageOptions(const LangOptions &LangOpts, 268 const LangOptions &ExistingLangOpts, 269 DiagnosticsEngine *Diags, 270 bool AllowCompatibleDifferences = true) { 271 #define LANGOPT(Name, Bits, Default, Description) \ 272 if (ExistingLangOpts.Name != LangOpts.Name) { \ 273 if (Diags) \ 274 Diags->Report(diag::err_pch_langopt_mismatch) \ 275 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 276 return true; \ 277 } 278 279 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 280 if (ExistingLangOpts.Name != LangOpts.Name) { \ 281 if (Diags) \ 282 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 283 << Description; \ 284 return true; \ 285 } 286 287 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 288 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 289 if (Diags) \ 290 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 291 << Description; \ 292 return true; \ 293 } 294 295 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 296 if (!AllowCompatibleDifferences) \ 297 LANGOPT(Name, Bits, Default, Description) 298 299 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 300 if (!AllowCompatibleDifferences) \ 301 ENUM_LANGOPT(Name, Bits, Default, Description) 302 303 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 304 if (!AllowCompatibleDifferences) \ 305 VALUE_LANGOPT(Name, Bits, Default, Description) 306 307 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 308 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 309 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 310 #include "clang/Basic/LangOptions.def" 311 312 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 313 if (Diags) 314 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 315 return true; 316 } 317 318 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 319 if (Diags) 320 Diags->Report(diag::err_pch_langopt_value_mismatch) 321 << "target Objective-C runtime"; 322 return true; 323 } 324 325 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 326 LangOpts.CommentOpts.BlockCommandNames) { 327 if (Diags) 328 Diags->Report(diag::err_pch_langopt_value_mismatch) 329 << "block command names"; 330 return true; 331 } 332 333 // Sanitizer feature mismatches are treated as compatible differences. If 334 // compatible differences aren't allowed, we still only want to check for 335 // mismatches of non-modular sanitizers (the only ones which can affect AST 336 // generation). 337 if (!AllowCompatibleDifferences) { 338 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 339 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 340 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 341 ExistingSanitizers.clear(ModularSanitizers); 342 ImportedSanitizers.clear(ModularSanitizers); 343 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 344 const std::string Flag = "-fsanitize="; 345 if (Diags) { 346 #define SANITIZER(NAME, ID) \ 347 { \ 348 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 349 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 350 if (InExistingModule != InImportedModule) \ 351 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 352 << InExistingModule << (Flag + NAME); \ 353 } 354 #include "clang/Basic/Sanitizers.def" 355 } 356 return true; 357 } 358 } 359 360 return false; 361 } 362 363 /// Compare the given set of target options against an existing set of 364 /// target options. 365 /// 366 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 367 /// 368 /// \returns true if the target options mis-match, false otherwise. 369 static bool checkTargetOptions(const TargetOptions &TargetOpts, 370 const TargetOptions &ExistingTargetOpts, 371 DiagnosticsEngine *Diags, 372 bool AllowCompatibleDifferences = true) { 373 #define CHECK_TARGET_OPT(Field, Name) \ 374 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 375 if (Diags) \ 376 Diags->Report(diag::err_pch_targetopt_mismatch) \ 377 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 378 return true; \ 379 } 380 381 // The triple and ABI must match exactly. 382 CHECK_TARGET_OPT(Triple, "target"); 383 CHECK_TARGET_OPT(ABI, "target ABI"); 384 385 // We can tolerate different CPUs in many cases, notably when one CPU 386 // supports a strict superset of another. When allowing compatible 387 // differences skip this check. 388 if (!AllowCompatibleDifferences) 389 CHECK_TARGET_OPT(CPU, "target CPU"); 390 391 #undef CHECK_TARGET_OPT 392 393 // Compare feature sets. 394 SmallVector<StringRef, 4> ExistingFeatures( 395 ExistingTargetOpts.FeaturesAsWritten.begin(), 396 ExistingTargetOpts.FeaturesAsWritten.end()); 397 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 398 TargetOpts.FeaturesAsWritten.end()); 399 llvm::sort(ExistingFeatures); 400 llvm::sort(ReadFeatures); 401 402 // We compute the set difference in both directions explicitly so that we can 403 // diagnose the differences differently. 404 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 405 std::set_difference( 406 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 407 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 408 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 409 ExistingFeatures.begin(), ExistingFeatures.end(), 410 std::back_inserter(UnmatchedReadFeatures)); 411 412 // If we are allowing compatible differences and the read feature set is 413 // a strict subset of the existing feature set, there is nothing to diagnose. 414 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 415 return false; 416 417 if (Diags) { 418 for (StringRef Feature : UnmatchedReadFeatures) 419 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 420 << /* is-existing-feature */ false << Feature; 421 for (StringRef Feature : UnmatchedExistingFeatures) 422 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 423 << /* is-existing-feature */ true << Feature; 424 } 425 426 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 427 } 428 429 bool 430 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 431 bool Complain, 432 bool AllowCompatibleDifferences) { 433 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 434 return checkLanguageOptions(LangOpts, ExistingLangOpts, 435 Complain ? &Reader.Diags : nullptr, 436 AllowCompatibleDifferences); 437 } 438 439 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 440 bool Complain, 441 bool AllowCompatibleDifferences) { 442 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 443 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 444 Complain ? &Reader.Diags : nullptr, 445 AllowCompatibleDifferences); 446 } 447 448 namespace { 449 450 using MacroDefinitionsMap = 451 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 452 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 453 454 } // namespace 455 456 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 457 DiagnosticsEngine &Diags, 458 bool Complain) { 459 using Level = DiagnosticsEngine::Level; 460 461 // Check current mappings for new -Werror mappings, and the stored mappings 462 // for cases that were explicitly mapped to *not* be errors that are now 463 // errors because of options like -Werror. 464 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 465 466 for (DiagnosticsEngine *MappingSource : MappingSources) { 467 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 468 diag::kind DiagID = DiagIDMappingPair.first; 469 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 470 if (CurLevel < DiagnosticsEngine::Error) 471 continue; // not significant 472 Level StoredLevel = 473 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 474 if (StoredLevel < DiagnosticsEngine::Error) { 475 if (Complain) 476 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 477 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 478 return true; 479 } 480 } 481 } 482 483 return false; 484 } 485 486 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 487 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 488 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 489 return true; 490 return Ext >= diag::Severity::Error; 491 } 492 493 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 494 DiagnosticsEngine &Diags, 495 bool IsSystem, bool Complain) { 496 // Top-level options 497 if (IsSystem) { 498 if (Diags.getSuppressSystemWarnings()) 499 return false; 500 // If -Wsystem-headers was not enabled before, be conservative 501 if (StoredDiags.getSuppressSystemWarnings()) { 502 if (Complain) 503 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 504 return true; 505 } 506 } 507 508 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 509 if (Complain) 510 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 511 return true; 512 } 513 514 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 515 !StoredDiags.getEnableAllWarnings()) { 516 if (Complain) 517 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 518 return true; 519 } 520 521 if (isExtHandlingFromDiagsError(Diags) && 522 !isExtHandlingFromDiagsError(StoredDiags)) { 523 if (Complain) 524 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 525 return true; 526 } 527 528 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 529 } 530 531 /// Return the top import module if it is implicit, nullptr otherwise. 532 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 533 Preprocessor &PP) { 534 // If the original import came from a file explicitly generated by the user, 535 // don't check the diagnostic mappings. 536 // FIXME: currently this is approximated by checking whether this is not a 537 // module import of an implicitly-loaded module file. 538 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 539 // the transitive closure of its imports, since unrelated modules cannot be 540 // imported until after this module finishes validation. 541 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 542 while (!TopImport->ImportedBy.empty()) 543 TopImport = TopImport->ImportedBy[0]; 544 if (TopImport->Kind != MK_ImplicitModule) 545 return nullptr; 546 547 StringRef ModuleName = TopImport->ModuleName; 548 assert(!ModuleName.empty() && "diagnostic options read before module name"); 549 550 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 551 assert(M && "missing module"); 552 return M; 553 } 554 555 bool PCHValidator::ReadDiagnosticOptions( 556 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 557 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 558 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 559 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 560 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 561 // This should never fail, because we would have processed these options 562 // before writing them to an ASTFile. 563 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 564 565 ModuleManager &ModuleMgr = Reader.getModuleManager(); 566 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 567 568 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 569 if (!TopM) 570 return false; 571 572 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 573 // contains the union of their flags. 574 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 575 Complain); 576 } 577 578 /// Collect the macro definitions provided by the given preprocessor 579 /// options. 580 static void 581 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 582 MacroDefinitionsMap &Macros, 583 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 584 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 585 StringRef Macro = PPOpts.Macros[I].first; 586 bool IsUndef = PPOpts.Macros[I].second; 587 588 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 589 StringRef MacroName = MacroPair.first; 590 StringRef MacroBody = MacroPair.second; 591 592 // For an #undef'd macro, we only care about the name. 593 if (IsUndef) { 594 if (MacroNames && !Macros.count(MacroName)) 595 MacroNames->push_back(MacroName); 596 597 Macros[MacroName] = std::make_pair("", true); 598 continue; 599 } 600 601 // For a #define'd macro, figure out the actual definition. 602 if (MacroName.size() == Macro.size()) 603 MacroBody = "1"; 604 else { 605 // Note: GCC drops anything following an end-of-line character. 606 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 607 MacroBody = MacroBody.substr(0, End); 608 } 609 610 if (MacroNames && !Macros.count(MacroName)) 611 MacroNames->push_back(MacroName); 612 Macros[MacroName] = std::make_pair(MacroBody, false); 613 } 614 } 615 616 /// Check the preprocessor options deserialized from the control block 617 /// against the preprocessor options in an existing preprocessor. 618 /// 619 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 620 /// \param Validate If true, validate preprocessor options. If false, allow 621 /// macros defined by \p ExistingPPOpts to override those defined by 622 /// \p PPOpts in SuggestedPredefines. 623 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 624 const PreprocessorOptions &ExistingPPOpts, 625 DiagnosticsEngine *Diags, 626 FileManager &FileMgr, 627 std::string &SuggestedPredefines, 628 const LangOptions &LangOpts, 629 bool Validate = true) { 630 // Check macro definitions. 631 MacroDefinitionsMap ASTFileMacros; 632 collectMacroDefinitions(PPOpts, ASTFileMacros); 633 MacroDefinitionsMap ExistingMacros; 634 SmallVector<StringRef, 4> ExistingMacroNames; 635 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 636 637 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 638 // Dig out the macro definition in the existing preprocessor options. 639 StringRef MacroName = ExistingMacroNames[I]; 640 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 641 642 // Check whether we know anything about this macro name or not. 643 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 644 ASTFileMacros.find(MacroName); 645 if (!Validate || Known == ASTFileMacros.end()) { 646 // FIXME: Check whether this identifier was referenced anywhere in the 647 // AST file. If so, we should reject the AST file. Unfortunately, this 648 // information isn't in the control block. What shall we do about it? 649 650 if (Existing.second) { 651 SuggestedPredefines += "#undef "; 652 SuggestedPredefines += MacroName.str(); 653 SuggestedPredefines += '\n'; 654 } else { 655 SuggestedPredefines += "#define "; 656 SuggestedPredefines += MacroName.str(); 657 SuggestedPredefines += ' '; 658 SuggestedPredefines += Existing.first.str(); 659 SuggestedPredefines += '\n'; 660 } 661 continue; 662 } 663 664 // If the macro was defined in one but undef'd in the other, we have a 665 // conflict. 666 if (Existing.second != Known->second.second) { 667 if (Diags) { 668 Diags->Report(diag::err_pch_macro_def_undef) 669 << MacroName << Known->second.second; 670 } 671 return true; 672 } 673 674 // If the macro was #undef'd in both, or if the macro bodies are identical, 675 // it's fine. 676 if (Existing.second || Existing.first == Known->second.first) 677 continue; 678 679 // The macro bodies differ; complain. 680 if (Diags) { 681 Diags->Report(diag::err_pch_macro_def_conflict) 682 << MacroName << Known->second.first << Existing.first; 683 } 684 return true; 685 } 686 687 // Check whether we're using predefines. 688 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 689 if (Diags) { 690 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 691 } 692 return true; 693 } 694 695 // Detailed record is important since it is used for the module cache hash. 696 if (LangOpts.Modules && 697 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 698 if (Diags) { 699 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 700 } 701 return true; 702 } 703 704 // Compute the #include and #include_macros lines we need. 705 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 706 StringRef File = ExistingPPOpts.Includes[I]; 707 708 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 709 !ExistingPPOpts.PCHThroughHeader.empty()) { 710 // In case the through header is an include, we must add all the includes 711 // to the predefines so the start point can be determined. 712 SuggestedPredefines += "#include \""; 713 SuggestedPredefines += File; 714 SuggestedPredefines += "\"\n"; 715 continue; 716 } 717 718 if (File == ExistingPPOpts.ImplicitPCHInclude) 719 continue; 720 721 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 722 != PPOpts.Includes.end()) 723 continue; 724 725 SuggestedPredefines += "#include \""; 726 SuggestedPredefines += File; 727 SuggestedPredefines += "\"\n"; 728 } 729 730 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 731 StringRef File = ExistingPPOpts.MacroIncludes[I]; 732 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 733 File) 734 != PPOpts.MacroIncludes.end()) 735 continue; 736 737 SuggestedPredefines += "#__include_macros \""; 738 SuggestedPredefines += File; 739 SuggestedPredefines += "\"\n##\n"; 740 } 741 742 return false; 743 } 744 745 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 746 bool Complain, 747 std::string &SuggestedPredefines) { 748 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 749 750 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 751 Complain? &Reader.Diags : nullptr, 752 PP.getFileManager(), 753 SuggestedPredefines, 754 PP.getLangOpts()); 755 } 756 757 bool SimpleASTReaderListener::ReadPreprocessorOptions( 758 const PreprocessorOptions &PPOpts, 759 bool Complain, 760 std::string &SuggestedPredefines) { 761 return checkPreprocessorOptions(PPOpts, 762 PP.getPreprocessorOpts(), 763 nullptr, 764 PP.getFileManager(), 765 SuggestedPredefines, 766 PP.getLangOpts(), 767 false); 768 } 769 770 /// Check the header search options deserialized from the control block 771 /// against the header search options in an existing preprocessor. 772 /// 773 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 774 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 775 StringRef SpecificModuleCachePath, 776 StringRef ExistingModuleCachePath, 777 DiagnosticsEngine *Diags, 778 const LangOptions &LangOpts) { 779 if (LangOpts.Modules) { 780 if (SpecificModuleCachePath != ExistingModuleCachePath) { 781 if (Diags) 782 Diags->Report(diag::err_pch_modulecache_mismatch) 783 << SpecificModuleCachePath << ExistingModuleCachePath; 784 return true; 785 } 786 } 787 788 return false; 789 } 790 791 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 792 StringRef SpecificModuleCachePath, 793 bool Complain) { 794 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 795 PP.getHeaderSearchInfo().getModuleCachePath(), 796 Complain ? &Reader.Diags : nullptr, 797 PP.getLangOpts()); 798 } 799 800 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 801 PP.setCounterValue(Value); 802 } 803 804 //===----------------------------------------------------------------------===// 805 // AST reader implementation 806 //===----------------------------------------------------------------------===// 807 808 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 809 bool TakeOwnership) { 810 DeserializationListener = Listener; 811 OwnsDeserializationListener = TakeOwnership; 812 } 813 814 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 815 return serialization::ComputeHash(Sel); 816 } 817 818 std::pair<unsigned, unsigned> 819 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 820 using namespace llvm::support; 821 822 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 823 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 824 return std::make_pair(KeyLen, DataLen); 825 } 826 827 ASTSelectorLookupTrait::internal_key_type 828 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 829 using namespace llvm::support; 830 831 SelectorTable &SelTable = Reader.getContext().Selectors; 832 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 833 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 834 F, endian::readNext<uint32_t, little, unaligned>(d)); 835 if (N == 0) 836 return SelTable.getNullarySelector(FirstII); 837 else if (N == 1) 838 return SelTable.getUnarySelector(FirstII); 839 840 SmallVector<IdentifierInfo *, 16> Args; 841 Args.push_back(FirstII); 842 for (unsigned I = 1; I != N; ++I) 843 Args.push_back(Reader.getLocalIdentifier( 844 F, endian::readNext<uint32_t, little, unaligned>(d))); 845 846 return SelTable.getSelector(N, Args.data()); 847 } 848 849 ASTSelectorLookupTrait::data_type 850 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 851 unsigned DataLen) { 852 using namespace llvm::support; 853 854 data_type Result; 855 856 Result.ID = Reader.getGlobalSelectorID( 857 F, endian::readNext<uint32_t, little, unaligned>(d)); 858 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 859 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 860 Result.InstanceBits = FullInstanceBits & 0x3; 861 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 862 Result.FactoryBits = FullFactoryBits & 0x3; 863 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 864 unsigned NumInstanceMethods = FullInstanceBits >> 3; 865 unsigned NumFactoryMethods = FullFactoryBits >> 3; 866 867 // Load instance methods 868 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 869 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 870 F, endian::readNext<uint32_t, little, unaligned>(d))) 871 Result.Instance.push_back(Method); 872 } 873 874 // Load factory methods 875 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 876 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 877 F, endian::readNext<uint32_t, little, unaligned>(d))) 878 Result.Factory.push_back(Method); 879 } 880 881 return Result; 882 } 883 884 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 885 return llvm::djbHash(a); 886 } 887 888 std::pair<unsigned, unsigned> 889 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 890 using namespace llvm::support; 891 892 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 893 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 894 return std::make_pair(KeyLen, DataLen); 895 } 896 897 ASTIdentifierLookupTraitBase::internal_key_type 898 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 899 assert(n >= 2 && d[n-1] == '\0'); 900 return StringRef((const char*) d, n-1); 901 } 902 903 /// Whether the given identifier is "interesting". 904 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 905 bool IsModule) { 906 return II.hadMacroDefinition() || 907 II.isPoisoned() || 908 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) || 909 II.hasRevertedTokenIDToIdentifier() || 910 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 911 II.getFETokenInfo()); 912 } 913 914 static bool readBit(unsigned &Bits) { 915 bool Value = Bits & 0x1; 916 Bits >>= 1; 917 return Value; 918 } 919 920 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 921 using namespace llvm::support; 922 923 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 924 return Reader.getGlobalIdentifierID(F, RawID >> 1); 925 } 926 927 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 928 if (!II.isFromAST()) { 929 II.setIsFromAST(); 930 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 931 if (isInterestingIdentifier(Reader, II, IsModule)) 932 II.setChangedSinceDeserialization(); 933 } 934 } 935 936 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 937 const unsigned char* d, 938 unsigned DataLen) { 939 using namespace llvm::support; 940 941 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 942 bool IsInteresting = RawID & 0x01; 943 944 // Wipe out the "is interesting" bit. 945 RawID = RawID >> 1; 946 947 // Build the IdentifierInfo and link the identifier ID with it. 948 IdentifierInfo *II = KnownII; 949 if (!II) { 950 II = &Reader.getIdentifierTable().getOwn(k); 951 KnownII = II; 952 } 953 markIdentifierFromAST(Reader, *II); 954 Reader.markIdentifierUpToDate(II); 955 956 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 957 if (!IsInteresting) { 958 // For uninteresting identifiers, there's nothing else to do. Just notify 959 // the reader that we've finished loading this identifier. 960 Reader.SetIdentifierInfo(ID, II); 961 return II; 962 } 963 964 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 965 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 966 bool CPlusPlusOperatorKeyword = readBit(Bits); 967 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 968 bool HasRevertedBuiltin = readBit(Bits); 969 bool Poisoned = readBit(Bits); 970 bool ExtensionToken = readBit(Bits); 971 bool HadMacroDefinition = readBit(Bits); 972 973 assert(Bits == 0 && "Extra bits in the identifier?"); 974 DataLen -= 8; 975 976 // Set or check the various bits in the IdentifierInfo structure. 977 // Token IDs are read-only. 978 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 979 II->revertTokenIDToIdentifier(); 980 if (!F.isModule()) 981 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 982 else if (HasRevertedBuiltin && II->getBuiltinID()) { 983 II->revertBuiltin(); 984 assert((II->hasRevertedBuiltin() || 985 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) && 986 "Incorrect ObjC keyword or builtin ID"); 987 } 988 assert(II->isExtensionToken() == ExtensionToken && 989 "Incorrect extension token flag"); 990 (void)ExtensionToken; 991 if (Poisoned) 992 II->setIsPoisoned(true); 993 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 994 "Incorrect C++ operator keyword flag"); 995 (void)CPlusPlusOperatorKeyword; 996 997 // If this identifier is a macro, deserialize the macro 998 // definition. 999 if (HadMacroDefinition) { 1000 uint32_t MacroDirectivesOffset = 1001 endian::readNext<uint32_t, little, unaligned>(d); 1002 DataLen -= 4; 1003 1004 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1005 } 1006 1007 Reader.SetIdentifierInfo(ID, II); 1008 1009 // Read all of the declarations visible at global scope with this 1010 // name. 1011 if (DataLen > 0) { 1012 SmallVector<uint32_t, 4> DeclIDs; 1013 for (; DataLen > 0; DataLen -= 4) 1014 DeclIDs.push_back(Reader.getGlobalDeclID( 1015 F, endian::readNext<uint32_t, little, unaligned>(d))); 1016 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1017 } 1018 1019 return II; 1020 } 1021 1022 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1023 : Kind(Name.getNameKind()) { 1024 switch (Kind) { 1025 case DeclarationName::Identifier: 1026 Data = (uint64_t)Name.getAsIdentifierInfo(); 1027 break; 1028 case DeclarationName::ObjCZeroArgSelector: 1029 case DeclarationName::ObjCOneArgSelector: 1030 case DeclarationName::ObjCMultiArgSelector: 1031 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1032 break; 1033 case DeclarationName::CXXOperatorName: 1034 Data = Name.getCXXOverloadedOperator(); 1035 break; 1036 case DeclarationName::CXXLiteralOperatorName: 1037 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1038 break; 1039 case DeclarationName::CXXDeductionGuideName: 1040 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1041 ->getDeclName().getAsIdentifierInfo(); 1042 break; 1043 case DeclarationName::CXXConstructorName: 1044 case DeclarationName::CXXDestructorName: 1045 case DeclarationName::CXXConversionFunctionName: 1046 case DeclarationName::CXXUsingDirective: 1047 Data = 0; 1048 break; 1049 } 1050 } 1051 1052 unsigned DeclarationNameKey::getHash() const { 1053 llvm::FoldingSetNodeID ID; 1054 ID.AddInteger(Kind); 1055 1056 switch (Kind) { 1057 case DeclarationName::Identifier: 1058 case DeclarationName::CXXLiteralOperatorName: 1059 case DeclarationName::CXXDeductionGuideName: 1060 ID.AddString(((IdentifierInfo*)Data)->getName()); 1061 break; 1062 case DeclarationName::ObjCZeroArgSelector: 1063 case DeclarationName::ObjCOneArgSelector: 1064 case DeclarationName::ObjCMultiArgSelector: 1065 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1066 break; 1067 case DeclarationName::CXXOperatorName: 1068 ID.AddInteger((OverloadedOperatorKind)Data); 1069 break; 1070 case DeclarationName::CXXConstructorName: 1071 case DeclarationName::CXXDestructorName: 1072 case DeclarationName::CXXConversionFunctionName: 1073 case DeclarationName::CXXUsingDirective: 1074 break; 1075 } 1076 1077 return ID.ComputeHash(); 1078 } 1079 1080 ModuleFile * 1081 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1082 using namespace llvm::support; 1083 1084 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1085 return Reader.getLocalModuleFile(F, ModuleFileID); 1086 } 1087 1088 std::pair<unsigned, unsigned> 1089 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1090 using namespace llvm::support; 1091 1092 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 1093 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 1094 return std::make_pair(KeyLen, DataLen); 1095 } 1096 1097 ASTDeclContextNameLookupTrait::internal_key_type 1098 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1099 using namespace llvm::support; 1100 1101 auto Kind = (DeclarationName::NameKind)*d++; 1102 uint64_t Data; 1103 switch (Kind) { 1104 case DeclarationName::Identifier: 1105 case DeclarationName::CXXLiteralOperatorName: 1106 case DeclarationName::CXXDeductionGuideName: 1107 Data = (uint64_t)Reader.getLocalIdentifier( 1108 F, endian::readNext<uint32_t, little, unaligned>(d)); 1109 break; 1110 case DeclarationName::ObjCZeroArgSelector: 1111 case DeclarationName::ObjCOneArgSelector: 1112 case DeclarationName::ObjCMultiArgSelector: 1113 Data = 1114 (uint64_t)Reader.getLocalSelector( 1115 F, endian::readNext<uint32_t, little, unaligned>( 1116 d)).getAsOpaquePtr(); 1117 break; 1118 case DeclarationName::CXXOperatorName: 1119 Data = *d++; // OverloadedOperatorKind 1120 break; 1121 case DeclarationName::CXXConstructorName: 1122 case DeclarationName::CXXDestructorName: 1123 case DeclarationName::CXXConversionFunctionName: 1124 case DeclarationName::CXXUsingDirective: 1125 Data = 0; 1126 break; 1127 } 1128 1129 return DeclarationNameKey(Kind, Data); 1130 } 1131 1132 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1133 const unsigned char *d, 1134 unsigned DataLen, 1135 data_type_builder &Val) { 1136 using namespace llvm::support; 1137 1138 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1139 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1140 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1141 } 1142 } 1143 1144 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1145 BitstreamCursor &Cursor, 1146 uint64_t Offset, 1147 DeclContext *DC) { 1148 assert(Offset != 0); 1149 1150 SavedStreamPosition SavedPosition(Cursor); 1151 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1152 Error(std::move(Err)); 1153 return true; 1154 } 1155 1156 RecordData Record; 1157 StringRef Blob; 1158 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1159 if (!MaybeCode) { 1160 Error(MaybeCode.takeError()); 1161 return true; 1162 } 1163 unsigned Code = MaybeCode.get(); 1164 1165 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1166 if (!MaybeRecCode) { 1167 Error(MaybeRecCode.takeError()); 1168 return true; 1169 } 1170 unsigned RecCode = MaybeRecCode.get(); 1171 if (RecCode != DECL_CONTEXT_LEXICAL) { 1172 Error("Expected lexical block"); 1173 return true; 1174 } 1175 1176 assert(!isa<TranslationUnitDecl>(DC) && 1177 "expected a TU_UPDATE_LEXICAL record for TU"); 1178 // If we are handling a C++ class template instantiation, we can see multiple 1179 // lexical updates for the same record. It's important that we select only one 1180 // of them, so that field numbering works properly. Just pick the first one we 1181 // see. 1182 auto &Lex = LexicalDecls[DC]; 1183 if (!Lex.first) { 1184 Lex = std::make_pair( 1185 &M, llvm::makeArrayRef( 1186 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1187 Blob.data()), 1188 Blob.size() / 4)); 1189 } 1190 DC->setHasExternalLexicalStorage(true); 1191 return false; 1192 } 1193 1194 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1195 BitstreamCursor &Cursor, 1196 uint64_t Offset, 1197 DeclID ID) { 1198 assert(Offset != 0); 1199 1200 SavedStreamPosition SavedPosition(Cursor); 1201 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1202 Error(std::move(Err)); 1203 return true; 1204 } 1205 1206 RecordData Record; 1207 StringRef Blob; 1208 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1209 if (!MaybeCode) { 1210 Error(MaybeCode.takeError()); 1211 return true; 1212 } 1213 unsigned Code = MaybeCode.get(); 1214 1215 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1216 if (!MaybeRecCode) { 1217 Error(MaybeRecCode.takeError()); 1218 return true; 1219 } 1220 unsigned RecCode = MaybeRecCode.get(); 1221 if (RecCode != DECL_CONTEXT_VISIBLE) { 1222 Error("Expected visible lookup table block"); 1223 return true; 1224 } 1225 1226 // We can't safely determine the primary context yet, so delay attaching the 1227 // lookup table until we're done with recursive deserialization. 1228 auto *Data = (const unsigned char*)Blob.data(); 1229 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1230 return false; 1231 } 1232 1233 void ASTReader::Error(StringRef Msg) const { 1234 Error(diag::err_fe_pch_malformed, Msg); 1235 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1236 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1237 Diag(diag::note_module_cache_path) 1238 << PP.getHeaderSearchInfo().getModuleCachePath(); 1239 } 1240 } 1241 1242 void ASTReader::Error(unsigned DiagID, 1243 StringRef Arg1, StringRef Arg2) const { 1244 if (Diags.isDiagnosticInFlight()) 1245 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2); 1246 else 1247 Diag(DiagID) << Arg1 << Arg2; 1248 } 1249 1250 void ASTReader::Error(llvm::Error &&Err) const { 1251 Error(toString(std::move(Err))); 1252 } 1253 1254 //===----------------------------------------------------------------------===// 1255 // Source Manager Deserialization 1256 //===----------------------------------------------------------------------===// 1257 1258 /// Read the line table in the source manager block. 1259 /// \returns true if there was an error. 1260 bool ASTReader::ParseLineTable(ModuleFile &F, 1261 const RecordData &Record) { 1262 unsigned Idx = 0; 1263 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1264 1265 // Parse the file names 1266 std::map<int, int> FileIDs; 1267 FileIDs[-1] = -1; // For unspecified filenames. 1268 for (unsigned I = 0; Record[Idx]; ++I) { 1269 // Extract the file name 1270 auto Filename = ReadPath(F, Record, Idx); 1271 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1272 } 1273 ++Idx; 1274 1275 // Parse the line entries 1276 std::vector<LineEntry> Entries; 1277 while (Idx < Record.size()) { 1278 int FID = Record[Idx++]; 1279 assert(FID >= 0 && "Serialized line entries for non-local file."); 1280 // Remap FileID from 1-based old view. 1281 FID += F.SLocEntryBaseID - 1; 1282 1283 // Extract the line entries 1284 unsigned NumEntries = Record[Idx++]; 1285 assert(NumEntries && "no line entries for file ID"); 1286 Entries.clear(); 1287 Entries.reserve(NumEntries); 1288 for (unsigned I = 0; I != NumEntries; ++I) { 1289 unsigned FileOffset = Record[Idx++]; 1290 unsigned LineNo = Record[Idx++]; 1291 int FilenameID = FileIDs[Record[Idx++]]; 1292 SrcMgr::CharacteristicKind FileKind 1293 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1294 unsigned IncludeOffset = Record[Idx++]; 1295 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1296 FileKind, IncludeOffset)); 1297 } 1298 LineTable.AddEntry(FileID::get(FID), Entries); 1299 } 1300 1301 return false; 1302 } 1303 1304 /// Read a source manager block 1305 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1306 using namespace SrcMgr; 1307 1308 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1309 1310 // Set the source-location entry cursor to the current position in 1311 // the stream. This cursor will be used to read the contents of the 1312 // source manager block initially, and then lazily read 1313 // source-location entries as needed. 1314 SLocEntryCursor = F.Stream; 1315 1316 // The stream itself is going to skip over the source manager block. 1317 if (llvm::Error Err = F.Stream.SkipBlock()) { 1318 Error(std::move(Err)); 1319 return true; 1320 } 1321 1322 // Enter the source manager block. 1323 if (llvm::Error Err = 1324 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1325 Error(std::move(Err)); 1326 return true; 1327 } 1328 1329 RecordData Record; 1330 while (true) { 1331 Expected<llvm::BitstreamEntry> MaybeE = 1332 SLocEntryCursor.advanceSkippingSubblocks(); 1333 if (!MaybeE) { 1334 Error(MaybeE.takeError()); 1335 return true; 1336 } 1337 llvm::BitstreamEntry E = MaybeE.get(); 1338 1339 switch (E.Kind) { 1340 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1341 case llvm::BitstreamEntry::Error: 1342 Error("malformed block record in AST file"); 1343 return true; 1344 case llvm::BitstreamEntry::EndBlock: 1345 return false; 1346 case llvm::BitstreamEntry::Record: 1347 // The interesting case. 1348 break; 1349 } 1350 1351 // Read a record. 1352 Record.clear(); 1353 StringRef Blob; 1354 Expected<unsigned> MaybeRecord = 1355 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1356 if (!MaybeRecord) { 1357 Error(MaybeRecord.takeError()); 1358 return true; 1359 } 1360 switch (MaybeRecord.get()) { 1361 default: // Default behavior: ignore. 1362 break; 1363 1364 case SM_SLOC_FILE_ENTRY: 1365 case SM_SLOC_BUFFER_ENTRY: 1366 case SM_SLOC_EXPANSION_ENTRY: 1367 // Once we hit one of the source location entries, we're done. 1368 return false; 1369 } 1370 } 1371 } 1372 1373 /// If a header file is not found at the path that we expect it to be 1374 /// and the PCH file was moved from its original location, try to resolve the 1375 /// file by assuming that header+PCH were moved together and the header is in 1376 /// the same place relative to the PCH. 1377 static std::string 1378 resolveFileRelativeToOriginalDir(const std::string &Filename, 1379 const std::string &OriginalDir, 1380 const std::string &CurrDir) { 1381 assert(OriginalDir != CurrDir && 1382 "No point trying to resolve the file if the PCH dir didn't change"); 1383 1384 using namespace llvm::sys; 1385 1386 SmallString<128> filePath(Filename); 1387 fs::make_absolute(filePath); 1388 assert(path::is_absolute(OriginalDir)); 1389 SmallString<128> currPCHPath(CurrDir); 1390 1391 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1392 fileDirE = path::end(path::parent_path(filePath)); 1393 path::const_iterator origDirI = path::begin(OriginalDir), 1394 origDirE = path::end(OriginalDir); 1395 // Skip the common path components from filePath and OriginalDir. 1396 while (fileDirI != fileDirE && origDirI != origDirE && 1397 *fileDirI == *origDirI) { 1398 ++fileDirI; 1399 ++origDirI; 1400 } 1401 for (; origDirI != origDirE; ++origDirI) 1402 path::append(currPCHPath, ".."); 1403 path::append(currPCHPath, fileDirI, fileDirE); 1404 path::append(currPCHPath, path::filename(Filename)); 1405 return currPCHPath.str(); 1406 } 1407 1408 bool ASTReader::ReadSLocEntry(int ID) { 1409 if (ID == 0) 1410 return false; 1411 1412 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1413 Error("source location entry ID out-of-range for AST file"); 1414 return true; 1415 } 1416 1417 // Local helper to read the (possibly-compressed) buffer data following the 1418 // entry record. 1419 auto ReadBuffer = [this]( 1420 BitstreamCursor &SLocEntryCursor, 1421 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1422 RecordData Record; 1423 StringRef Blob; 1424 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1425 if (!MaybeCode) { 1426 Error(MaybeCode.takeError()); 1427 return nullptr; 1428 } 1429 unsigned Code = MaybeCode.get(); 1430 1431 Expected<unsigned> MaybeRecCode = 1432 SLocEntryCursor.readRecord(Code, Record, &Blob); 1433 if (!MaybeRecCode) { 1434 Error(MaybeRecCode.takeError()); 1435 return nullptr; 1436 } 1437 unsigned RecCode = MaybeRecCode.get(); 1438 1439 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1440 if (!llvm::zlib::isAvailable()) { 1441 Error("zlib is not available"); 1442 return nullptr; 1443 } 1444 SmallString<0> Uncompressed; 1445 if (llvm::Error E = 1446 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1447 Error("could not decompress embedded file contents: " + 1448 llvm::toString(std::move(E))); 1449 return nullptr; 1450 } 1451 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1452 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1453 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1454 } else { 1455 Error("AST record has invalid code"); 1456 return nullptr; 1457 } 1458 }; 1459 1460 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1461 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1462 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1463 Error(std::move(Err)); 1464 return true; 1465 } 1466 1467 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1468 unsigned BaseOffset = F->SLocEntryBaseOffset; 1469 1470 ++NumSLocEntriesRead; 1471 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1472 if (!MaybeEntry) { 1473 Error(MaybeEntry.takeError()); 1474 return true; 1475 } 1476 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1477 1478 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1479 Error("incorrectly-formatted source location entry in AST file"); 1480 return true; 1481 } 1482 1483 RecordData Record; 1484 StringRef Blob; 1485 Expected<unsigned> MaybeSLOC = 1486 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1487 if (!MaybeSLOC) { 1488 Error(MaybeSLOC.takeError()); 1489 return true; 1490 } 1491 switch (MaybeSLOC.get()) { 1492 default: 1493 Error("incorrectly-formatted source location entry in AST file"); 1494 return true; 1495 1496 case SM_SLOC_FILE_ENTRY: { 1497 // We will detect whether a file changed and return 'Failure' for it, but 1498 // we will also try to fail gracefully by setting up the SLocEntry. 1499 unsigned InputID = Record[4]; 1500 InputFile IF = getInputFile(*F, InputID); 1501 const FileEntry *File = IF.getFile(); 1502 bool OverriddenBuffer = IF.isOverridden(); 1503 1504 // Note that we only check if a File was returned. If it was out-of-date 1505 // we have complained but we will continue creating a FileID to recover 1506 // gracefully. 1507 if (!File) 1508 return true; 1509 1510 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1511 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1512 // This is the module's main file. 1513 IncludeLoc = getImportLocation(F); 1514 } 1515 SrcMgr::CharacteristicKind 1516 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1517 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1518 ID, BaseOffset + Record[0]); 1519 SrcMgr::FileInfo &FileInfo = 1520 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1521 FileInfo.NumCreatedFIDs = Record[5]; 1522 if (Record[3]) 1523 FileInfo.setHasLineDirectives(); 1524 1525 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1526 unsigned NumFileDecls = Record[7]; 1527 if (NumFileDecls && ContextObj) { 1528 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1529 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1530 NumFileDecls)); 1531 } 1532 1533 const SrcMgr::ContentCache *ContentCache 1534 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter)); 1535 if (OverriddenBuffer && !ContentCache->BufferOverridden && 1536 ContentCache->ContentsEntry == ContentCache->OrigEntry && 1537 !ContentCache->getRawBuffer()) { 1538 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1539 if (!Buffer) 1540 return true; 1541 SourceMgr.overrideFileContents(File, std::move(Buffer)); 1542 } 1543 1544 break; 1545 } 1546 1547 case SM_SLOC_BUFFER_ENTRY: { 1548 const char *Name = Blob.data(); 1549 unsigned Offset = Record[0]; 1550 SrcMgr::CharacteristicKind 1551 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1552 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1553 if (IncludeLoc.isInvalid() && F->isModule()) { 1554 IncludeLoc = getImportLocation(F); 1555 } 1556 1557 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1558 if (!Buffer) 1559 return true; 1560 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1561 BaseOffset + Offset, IncludeLoc); 1562 break; 1563 } 1564 1565 case SM_SLOC_EXPANSION_ENTRY: { 1566 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1567 SourceMgr.createExpansionLoc(SpellingLoc, 1568 ReadSourceLocation(*F, Record[2]), 1569 ReadSourceLocation(*F, Record[3]), 1570 Record[5], 1571 Record[4], 1572 ID, 1573 BaseOffset + Record[0]); 1574 break; 1575 } 1576 } 1577 1578 return false; 1579 } 1580 1581 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1582 if (ID == 0) 1583 return std::make_pair(SourceLocation(), ""); 1584 1585 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1586 Error("source location entry ID out-of-range for AST file"); 1587 return std::make_pair(SourceLocation(), ""); 1588 } 1589 1590 // Find which module file this entry lands in. 1591 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1592 if (!M->isModule()) 1593 return std::make_pair(SourceLocation(), ""); 1594 1595 // FIXME: Can we map this down to a particular submodule? That would be 1596 // ideal. 1597 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1598 } 1599 1600 /// Find the location where the module F is imported. 1601 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1602 if (F->ImportLoc.isValid()) 1603 return F->ImportLoc; 1604 1605 // Otherwise we have a PCH. It's considered to be "imported" at the first 1606 // location of its includer. 1607 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1608 // Main file is the importer. 1609 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1610 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1611 } 1612 return F->ImportedBy[0]->FirstLoc; 1613 } 1614 1615 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1616 /// the abbreviations that are at the top of the block and then leave the cursor 1617 /// pointing into the block. 1618 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) { 1619 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 1620 // FIXME this drops errors on the floor. 1621 consumeError(std::move(Err)); 1622 return true; 1623 } 1624 1625 while (true) { 1626 uint64_t Offset = Cursor.GetCurrentBitNo(); 1627 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1628 if (!MaybeCode) { 1629 // FIXME this drops errors on the floor. 1630 consumeError(MaybeCode.takeError()); 1631 return true; 1632 } 1633 unsigned Code = MaybeCode.get(); 1634 1635 // We expect all abbrevs to be at the start of the block. 1636 if (Code != llvm::bitc::DEFINE_ABBREV) { 1637 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1638 // FIXME this drops errors on the floor. 1639 consumeError(std::move(Err)); 1640 return true; 1641 } 1642 return false; 1643 } 1644 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) { 1645 // FIXME this drops errors on the floor. 1646 consumeError(std::move(Err)); 1647 return true; 1648 } 1649 } 1650 } 1651 1652 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1653 unsigned &Idx) { 1654 Token Tok; 1655 Tok.startToken(); 1656 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1657 Tok.setLength(Record[Idx++]); 1658 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1659 Tok.setIdentifierInfo(II); 1660 Tok.setKind((tok::TokenKind)Record[Idx++]); 1661 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1662 return Tok; 1663 } 1664 1665 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1666 BitstreamCursor &Stream = F.MacroCursor; 1667 1668 // Keep track of where we are in the stream, then jump back there 1669 // after reading this macro. 1670 SavedStreamPosition SavedPosition(Stream); 1671 1672 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1673 // FIXME this drops errors on the floor. 1674 consumeError(std::move(Err)); 1675 return nullptr; 1676 } 1677 RecordData Record; 1678 SmallVector<IdentifierInfo*, 16> MacroParams; 1679 MacroInfo *Macro = nullptr; 1680 1681 while (true) { 1682 // Advance to the next record, but if we get to the end of the block, don't 1683 // pop it (removing all the abbreviations from the cursor) since we want to 1684 // be able to reseek within the block and read entries. 1685 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1686 Expected<llvm::BitstreamEntry> MaybeEntry = 1687 Stream.advanceSkippingSubblocks(Flags); 1688 if (!MaybeEntry) { 1689 Error(MaybeEntry.takeError()); 1690 return Macro; 1691 } 1692 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1693 1694 switch (Entry.Kind) { 1695 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1696 case llvm::BitstreamEntry::Error: 1697 Error("malformed block record in AST file"); 1698 return Macro; 1699 case llvm::BitstreamEntry::EndBlock: 1700 return Macro; 1701 case llvm::BitstreamEntry::Record: 1702 // The interesting case. 1703 break; 1704 } 1705 1706 // Read a record. 1707 Record.clear(); 1708 PreprocessorRecordTypes RecType; 1709 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1710 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1711 else { 1712 Error(MaybeRecType.takeError()); 1713 return Macro; 1714 } 1715 switch (RecType) { 1716 case PP_MODULE_MACRO: 1717 case PP_MACRO_DIRECTIVE_HISTORY: 1718 return Macro; 1719 1720 case PP_MACRO_OBJECT_LIKE: 1721 case PP_MACRO_FUNCTION_LIKE: { 1722 // If we already have a macro, that means that we've hit the end 1723 // of the definition of the macro we were looking for. We're 1724 // done. 1725 if (Macro) 1726 return Macro; 1727 1728 unsigned NextIndex = 1; // Skip identifier ID. 1729 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1730 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1731 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1732 MI->setIsUsed(Record[NextIndex++]); 1733 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1734 1735 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1736 // Decode function-like macro info. 1737 bool isC99VarArgs = Record[NextIndex++]; 1738 bool isGNUVarArgs = Record[NextIndex++]; 1739 bool hasCommaPasting = Record[NextIndex++]; 1740 MacroParams.clear(); 1741 unsigned NumArgs = Record[NextIndex++]; 1742 for (unsigned i = 0; i != NumArgs; ++i) 1743 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1744 1745 // Install function-like macro info. 1746 MI->setIsFunctionLike(); 1747 if (isC99VarArgs) MI->setIsC99Varargs(); 1748 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1749 if (hasCommaPasting) MI->setHasCommaPasting(); 1750 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1751 } 1752 1753 // Remember that we saw this macro last so that we add the tokens that 1754 // form its body to it. 1755 Macro = MI; 1756 1757 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1758 Record[NextIndex]) { 1759 // We have a macro definition. Register the association 1760 PreprocessedEntityID 1761 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1762 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1763 PreprocessingRecord::PPEntityID PPID = 1764 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1765 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1766 PPRec.getPreprocessedEntity(PPID)); 1767 if (PPDef) 1768 PPRec.RegisterMacroDefinition(Macro, PPDef); 1769 } 1770 1771 ++NumMacrosRead; 1772 break; 1773 } 1774 1775 case PP_TOKEN: { 1776 // If we see a TOKEN before a PP_MACRO_*, then the file is 1777 // erroneous, just pretend we didn't see this. 1778 if (!Macro) break; 1779 1780 unsigned Idx = 0; 1781 Token Tok = ReadToken(F, Record, Idx); 1782 Macro->AddTokenToBody(Tok); 1783 break; 1784 } 1785 } 1786 } 1787 } 1788 1789 PreprocessedEntityID 1790 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1791 unsigned LocalID) const { 1792 if (!M.ModuleOffsetMap.empty()) 1793 ReadModuleOffsetMap(M); 1794 1795 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1796 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1797 assert(I != M.PreprocessedEntityRemap.end() 1798 && "Invalid index into preprocessed entity index remap"); 1799 1800 return LocalID + I->second; 1801 } 1802 1803 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1804 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1805 } 1806 1807 HeaderFileInfoTrait::internal_key_type 1808 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1809 internal_key_type ikey = {FE->getSize(), 1810 M.HasTimestamps ? FE->getModificationTime() : 0, 1811 FE->getName(), /*Imported*/ false}; 1812 return ikey; 1813 } 1814 1815 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1816 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1817 return false; 1818 1819 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1820 return true; 1821 1822 // Determine whether the actual files are equivalent. 1823 FileManager &FileMgr = Reader.getFileManager(); 1824 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1825 if (!Key.Imported) 1826 return FileMgr.getFile(Key.Filename); 1827 1828 std::string Resolved = Key.Filename; 1829 Reader.ResolveImportedPath(M, Resolved); 1830 return FileMgr.getFile(Resolved); 1831 }; 1832 1833 const FileEntry *FEA = GetFile(a); 1834 const FileEntry *FEB = GetFile(b); 1835 return FEA && FEA == FEB; 1836 } 1837 1838 std::pair<unsigned, unsigned> 1839 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1840 using namespace llvm::support; 1841 1842 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1843 unsigned DataLen = (unsigned) *d++; 1844 return std::make_pair(KeyLen, DataLen); 1845 } 1846 1847 HeaderFileInfoTrait::internal_key_type 1848 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1849 using namespace llvm::support; 1850 1851 internal_key_type ikey; 1852 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1853 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1854 ikey.Filename = (const char *)d; 1855 ikey.Imported = true; 1856 return ikey; 1857 } 1858 1859 HeaderFileInfoTrait::data_type 1860 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1861 unsigned DataLen) { 1862 using namespace llvm::support; 1863 1864 const unsigned char *End = d + DataLen; 1865 HeaderFileInfo HFI; 1866 unsigned Flags = *d++; 1867 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1868 HFI.isImport |= (Flags >> 5) & 0x01; 1869 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1870 HFI.DirInfo = (Flags >> 1) & 0x07; 1871 HFI.IndexHeaderMapHeader = Flags & 0x01; 1872 // FIXME: Find a better way to handle this. Maybe just store a 1873 // "has been included" flag? 1874 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1875 HFI.NumIncludes); 1876 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1877 M, endian::readNext<uint32_t, little, unaligned>(d)); 1878 if (unsigned FrameworkOffset = 1879 endian::readNext<uint32_t, little, unaligned>(d)) { 1880 // The framework offset is 1 greater than the actual offset, 1881 // since 0 is used as an indicator for "no framework name". 1882 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1883 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1884 } 1885 1886 assert((End - d) % 4 == 0 && 1887 "Wrong data length in HeaderFileInfo deserialization"); 1888 while (d != End) { 1889 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1890 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1891 LocalSMID >>= 2; 1892 1893 // This header is part of a module. Associate it with the module to enable 1894 // implicit module import. 1895 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1896 Module *Mod = Reader.getSubmodule(GlobalSMID); 1897 FileManager &FileMgr = Reader.getFileManager(); 1898 ModuleMap &ModMap = 1899 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1900 1901 std::string Filename = key.Filename; 1902 if (key.Imported) 1903 Reader.ResolveImportedPath(M, Filename); 1904 // FIXME: This is not always the right filename-as-written, but we're not 1905 // going to use this information to rebuild the module, so it doesn't make 1906 // a lot of difference. 1907 Module::Header H = { key.Filename, FileMgr.getFile(Filename) }; 1908 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1909 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1910 } 1911 1912 // This HeaderFileInfo was externally loaded. 1913 HFI.External = true; 1914 HFI.IsValid = true; 1915 return HFI; 1916 } 1917 1918 void ASTReader::addPendingMacro(IdentifierInfo *II, 1919 ModuleFile *M, 1920 uint64_t MacroDirectivesOffset) { 1921 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1922 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1923 } 1924 1925 void ASTReader::ReadDefinedMacros() { 1926 // Note that we are loading defined macros. 1927 Deserializing Macros(this); 1928 1929 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1930 BitstreamCursor &MacroCursor = I.MacroCursor; 1931 1932 // If there was no preprocessor block, skip this file. 1933 if (MacroCursor.getBitcodeBytes().empty()) 1934 continue; 1935 1936 BitstreamCursor Cursor = MacroCursor; 1937 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1938 Error(std::move(Err)); 1939 return; 1940 } 1941 1942 RecordData Record; 1943 while (true) { 1944 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1945 if (!MaybeE) { 1946 Error(MaybeE.takeError()); 1947 return; 1948 } 1949 llvm::BitstreamEntry E = MaybeE.get(); 1950 1951 switch (E.Kind) { 1952 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1953 case llvm::BitstreamEntry::Error: 1954 Error("malformed block record in AST file"); 1955 return; 1956 case llvm::BitstreamEntry::EndBlock: 1957 goto NextCursor; 1958 1959 case llvm::BitstreamEntry::Record: { 1960 Record.clear(); 1961 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1962 if (!MaybeRecord) { 1963 Error(MaybeRecord.takeError()); 1964 return; 1965 } 1966 switch (MaybeRecord.get()) { 1967 default: // Default behavior: ignore. 1968 break; 1969 1970 case PP_MACRO_OBJECT_LIKE: 1971 case PP_MACRO_FUNCTION_LIKE: { 1972 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1973 if (II->isOutOfDate()) 1974 updateOutOfDateIdentifier(*II); 1975 break; 1976 } 1977 1978 case PP_TOKEN: 1979 // Ignore tokens. 1980 break; 1981 } 1982 break; 1983 } 1984 } 1985 } 1986 NextCursor: ; 1987 } 1988 } 1989 1990 namespace { 1991 1992 /// Visitor class used to look up identifirs in an AST file. 1993 class IdentifierLookupVisitor { 1994 StringRef Name; 1995 unsigned NameHash; 1996 unsigned PriorGeneration; 1997 unsigned &NumIdentifierLookups; 1998 unsigned &NumIdentifierLookupHits; 1999 IdentifierInfo *Found = nullptr; 2000 2001 public: 2002 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2003 unsigned &NumIdentifierLookups, 2004 unsigned &NumIdentifierLookupHits) 2005 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2006 PriorGeneration(PriorGeneration), 2007 NumIdentifierLookups(NumIdentifierLookups), 2008 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2009 2010 bool operator()(ModuleFile &M) { 2011 // If we've already searched this module file, skip it now. 2012 if (M.Generation <= PriorGeneration) 2013 return true; 2014 2015 ASTIdentifierLookupTable *IdTable 2016 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2017 if (!IdTable) 2018 return false; 2019 2020 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2021 Found); 2022 ++NumIdentifierLookups; 2023 ASTIdentifierLookupTable::iterator Pos = 2024 IdTable->find_hashed(Name, NameHash, &Trait); 2025 if (Pos == IdTable->end()) 2026 return false; 2027 2028 // Dereferencing the iterator has the effect of building the 2029 // IdentifierInfo node and populating it with the various 2030 // declarations it needs. 2031 ++NumIdentifierLookupHits; 2032 Found = *Pos; 2033 return true; 2034 } 2035 2036 // Retrieve the identifier info found within the module 2037 // files. 2038 IdentifierInfo *getIdentifierInfo() const { return Found; } 2039 }; 2040 2041 } // namespace 2042 2043 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2044 // Note that we are loading an identifier. 2045 Deserializing AnIdentifier(this); 2046 2047 unsigned PriorGeneration = 0; 2048 if (getContext().getLangOpts().Modules) 2049 PriorGeneration = IdentifierGeneration[&II]; 2050 2051 // If there is a global index, look there first to determine which modules 2052 // provably do not have any results for this identifier. 2053 GlobalModuleIndex::HitSet Hits; 2054 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2055 if (!loadGlobalIndex()) { 2056 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2057 HitsPtr = &Hits; 2058 } 2059 } 2060 2061 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2062 NumIdentifierLookups, 2063 NumIdentifierLookupHits); 2064 ModuleMgr.visit(Visitor, HitsPtr); 2065 markIdentifierUpToDate(&II); 2066 } 2067 2068 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2069 if (!II) 2070 return; 2071 2072 II->setOutOfDate(false); 2073 2074 // Update the generation for this identifier. 2075 if (getContext().getLangOpts().Modules) 2076 IdentifierGeneration[II] = getGeneration(); 2077 } 2078 2079 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2080 const PendingMacroInfo &PMInfo) { 2081 ModuleFile &M = *PMInfo.M; 2082 2083 BitstreamCursor &Cursor = M.MacroCursor; 2084 SavedStreamPosition SavedPosition(Cursor); 2085 if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) { 2086 Error(std::move(Err)); 2087 return; 2088 } 2089 2090 struct ModuleMacroRecord { 2091 SubmoduleID SubModID; 2092 MacroInfo *MI; 2093 SmallVector<SubmoduleID, 8> Overrides; 2094 }; 2095 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2096 2097 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2098 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2099 // macro histroy. 2100 RecordData Record; 2101 while (true) { 2102 Expected<llvm::BitstreamEntry> MaybeEntry = 2103 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2104 if (!MaybeEntry) { 2105 Error(MaybeEntry.takeError()); 2106 return; 2107 } 2108 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2109 2110 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2111 Error("malformed block record in AST file"); 2112 return; 2113 } 2114 2115 Record.clear(); 2116 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2117 if (!MaybePP) { 2118 Error(MaybePP.takeError()); 2119 return; 2120 } 2121 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2122 case PP_MACRO_DIRECTIVE_HISTORY: 2123 break; 2124 2125 case PP_MODULE_MACRO: { 2126 ModuleMacros.push_back(ModuleMacroRecord()); 2127 auto &Info = ModuleMacros.back(); 2128 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2129 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2130 for (int I = 2, N = Record.size(); I != N; ++I) 2131 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2132 continue; 2133 } 2134 2135 default: 2136 Error("malformed block record in AST file"); 2137 return; 2138 } 2139 2140 // We found the macro directive history; that's the last record 2141 // for this macro. 2142 break; 2143 } 2144 2145 // Module macros are listed in reverse dependency order. 2146 { 2147 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2148 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2149 for (auto &MMR : ModuleMacros) { 2150 Overrides.clear(); 2151 for (unsigned ModID : MMR.Overrides) { 2152 Module *Mod = getSubmodule(ModID); 2153 auto *Macro = PP.getModuleMacro(Mod, II); 2154 assert(Macro && "missing definition for overridden macro"); 2155 Overrides.push_back(Macro); 2156 } 2157 2158 bool Inserted = false; 2159 Module *Owner = getSubmodule(MMR.SubModID); 2160 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2161 } 2162 } 2163 2164 // Don't read the directive history for a module; we don't have anywhere 2165 // to put it. 2166 if (M.isModule()) 2167 return; 2168 2169 // Deserialize the macro directives history in reverse source-order. 2170 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2171 unsigned Idx = 0, N = Record.size(); 2172 while (Idx < N) { 2173 MacroDirective *MD = nullptr; 2174 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2175 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2176 switch (K) { 2177 case MacroDirective::MD_Define: { 2178 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2179 MD = PP.AllocateDefMacroDirective(MI, Loc); 2180 break; 2181 } 2182 case MacroDirective::MD_Undefine: 2183 MD = PP.AllocateUndefMacroDirective(Loc); 2184 break; 2185 case MacroDirective::MD_Visibility: 2186 bool isPublic = Record[Idx++]; 2187 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2188 break; 2189 } 2190 2191 if (!Latest) 2192 Latest = MD; 2193 if (Earliest) 2194 Earliest->setPrevious(MD); 2195 Earliest = MD; 2196 } 2197 2198 if (Latest) 2199 PP.setLoadedMacroDirective(II, Earliest, Latest); 2200 } 2201 2202 ASTReader::InputFileInfo 2203 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2204 // Go find this input file. 2205 BitstreamCursor &Cursor = F.InputFilesCursor; 2206 SavedStreamPosition SavedPosition(Cursor); 2207 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2208 // FIXME this drops errors on the floor. 2209 consumeError(std::move(Err)); 2210 } 2211 2212 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2213 if (!MaybeCode) { 2214 // FIXME this drops errors on the floor. 2215 consumeError(MaybeCode.takeError()); 2216 } 2217 unsigned Code = MaybeCode.get(); 2218 RecordData Record; 2219 StringRef Blob; 2220 2221 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2222 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2223 "invalid record type for input file"); 2224 else { 2225 // FIXME this drops errors on the floor. 2226 consumeError(Maybe.takeError()); 2227 } 2228 2229 assert(Record[0] == ID && "Bogus stored ID or offset"); 2230 InputFileInfo R; 2231 R.StoredSize = static_cast<off_t>(Record[1]); 2232 R.StoredTime = static_cast<time_t>(Record[2]); 2233 R.Overridden = static_cast<bool>(Record[3]); 2234 R.Transient = static_cast<bool>(Record[4]); 2235 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2236 R.Filename = Blob; 2237 ResolveImportedPath(F, R.Filename); 2238 return R; 2239 } 2240 2241 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2242 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2243 // If this ID is bogus, just return an empty input file. 2244 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2245 return InputFile(); 2246 2247 // If we've already loaded this input file, return it. 2248 if (F.InputFilesLoaded[ID-1].getFile()) 2249 return F.InputFilesLoaded[ID-1]; 2250 2251 if (F.InputFilesLoaded[ID-1].isNotFound()) 2252 return InputFile(); 2253 2254 // Go find this input file. 2255 BitstreamCursor &Cursor = F.InputFilesCursor; 2256 SavedStreamPosition SavedPosition(Cursor); 2257 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2258 // FIXME this drops errors on the floor. 2259 consumeError(std::move(Err)); 2260 } 2261 2262 InputFileInfo FI = readInputFileInfo(F, ID); 2263 off_t StoredSize = FI.StoredSize; 2264 time_t StoredTime = FI.StoredTime; 2265 bool Overridden = FI.Overridden; 2266 bool Transient = FI.Transient; 2267 StringRef Filename = FI.Filename; 2268 2269 const FileEntry *File = FileMgr.getFile(Filename, /*OpenFile=*/false); 2270 // If we didn't find the file, resolve it relative to the 2271 // original directory from which this AST file was created. 2272 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2273 F.OriginalDir != F.BaseDirectory) { 2274 std::string Resolved = resolveFileRelativeToOriginalDir( 2275 Filename, F.OriginalDir, F.BaseDirectory); 2276 if (!Resolved.empty()) 2277 File = FileMgr.getFile(Resolved); 2278 } 2279 2280 // For an overridden file, create a virtual file with the stored 2281 // size/timestamp. 2282 if ((Overridden || Transient) && File == nullptr) 2283 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 2284 2285 if (File == nullptr) { 2286 if (Complain) { 2287 std::string ErrorStr = "could not find file '"; 2288 ErrorStr += Filename; 2289 ErrorStr += "' referenced by AST file '"; 2290 ErrorStr += F.FileName; 2291 ErrorStr += "'"; 2292 Error(ErrorStr); 2293 } 2294 // Record that we didn't find the file. 2295 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2296 return InputFile(); 2297 } 2298 2299 // Check if there was a request to override the contents of the file 2300 // that was part of the precompiled header. Overriding such a file 2301 // can lead to problems when lexing using the source locations from the 2302 // PCH. 2303 SourceManager &SM = getSourceManager(); 2304 // FIXME: Reject if the overrides are different. 2305 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2306 if (Complain) 2307 Error(diag::err_fe_pch_file_overridden, Filename); 2308 // After emitting the diagnostic, recover by disabling the override so 2309 // that the original file will be used. 2310 // 2311 // FIXME: This recovery is just as broken as the original state; there may 2312 // be another precompiled module that's using the overridden contents, or 2313 // we might be half way through parsing it. Instead, we should treat the 2314 // overridden contents as belonging to a separate FileEntry. 2315 SM.disableFileContentsOverride(File); 2316 // The FileEntry is a virtual file entry with the size of the contents 2317 // that would override the original contents. Set it to the original's 2318 // size/time. 2319 FileMgr.modifyFileEntry(const_cast<FileEntry*>(File), 2320 StoredSize, StoredTime); 2321 } 2322 2323 bool IsOutOfDate = false; 2324 2325 // For an overridden file, there is nothing to validate. 2326 if (!Overridden && // 2327 (StoredSize != File->getSize() || 2328 (StoredTime && StoredTime != File->getModificationTime() && 2329 !DisableValidation) 2330 )) { 2331 if (Complain) { 2332 // Build a list of the PCH imports that got us here (in reverse). 2333 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2334 while (!ImportStack.back()->ImportedBy.empty()) 2335 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2336 2337 // The top-level PCH is stale. 2338 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2339 unsigned DiagnosticKind = moduleKindForDiagnostic(ImportStack.back()->Kind); 2340 if (DiagnosticKind == 0) 2341 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName); 2342 else if (DiagnosticKind == 1) 2343 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName); 2344 else 2345 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName); 2346 2347 // Print the import stack. 2348 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) { 2349 Diag(diag::note_pch_required_by) 2350 << Filename << ImportStack[0]->FileName; 2351 for (unsigned I = 1; I < ImportStack.size(); ++I) 2352 Diag(diag::note_pch_required_by) 2353 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2354 } 2355 2356 if (!Diags.isDiagnosticInFlight()) 2357 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2358 } 2359 2360 IsOutOfDate = true; 2361 } 2362 // FIXME: If the file is overridden and we've already opened it, 2363 // issue an error (or split it into a separate FileEntry). 2364 2365 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate); 2366 2367 // Note that we've loaded this input file. 2368 F.InputFilesLoaded[ID-1] = IF; 2369 return IF; 2370 } 2371 2372 /// If we are loading a relocatable PCH or module file, and the filename 2373 /// is not an absolute path, add the system or module root to the beginning of 2374 /// the file name. 2375 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2376 // Resolve relative to the base directory, if we have one. 2377 if (!M.BaseDirectory.empty()) 2378 return ResolveImportedPath(Filename, M.BaseDirectory); 2379 } 2380 2381 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2382 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2383 return; 2384 2385 SmallString<128> Buffer; 2386 llvm::sys::path::append(Buffer, Prefix, Filename); 2387 Filename.assign(Buffer.begin(), Buffer.end()); 2388 } 2389 2390 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2391 switch (ARR) { 2392 case ASTReader::Failure: return true; 2393 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2394 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2395 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2396 case ASTReader::ConfigurationMismatch: 2397 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2398 case ASTReader::HadErrors: return true; 2399 case ASTReader::Success: return false; 2400 } 2401 2402 llvm_unreachable("unknown ASTReadResult"); 2403 } 2404 2405 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2406 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2407 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2408 std::string &SuggestedPredefines) { 2409 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2410 // FIXME this drops errors on the floor. 2411 consumeError(std::move(Err)); 2412 return Failure; 2413 } 2414 2415 // Read all of the records in the options block. 2416 RecordData Record; 2417 ASTReadResult Result = Success; 2418 while (true) { 2419 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2420 if (!MaybeEntry) { 2421 // FIXME this drops errors on the floor. 2422 consumeError(MaybeEntry.takeError()); 2423 return Failure; 2424 } 2425 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2426 2427 switch (Entry.Kind) { 2428 case llvm::BitstreamEntry::Error: 2429 case llvm::BitstreamEntry::SubBlock: 2430 return Failure; 2431 2432 case llvm::BitstreamEntry::EndBlock: 2433 return Result; 2434 2435 case llvm::BitstreamEntry::Record: 2436 // The interesting case. 2437 break; 2438 } 2439 2440 // Read and process a record. 2441 Record.clear(); 2442 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2443 if (!MaybeRecordType) { 2444 // FIXME this drops errors on the floor. 2445 consumeError(MaybeRecordType.takeError()); 2446 return Failure; 2447 } 2448 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2449 case LANGUAGE_OPTIONS: { 2450 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2451 if (ParseLanguageOptions(Record, Complain, Listener, 2452 AllowCompatibleConfigurationMismatch)) 2453 Result = ConfigurationMismatch; 2454 break; 2455 } 2456 2457 case TARGET_OPTIONS: { 2458 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2459 if (ParseTargetOptions(Record, Complain, Listener, 2460 AllowCompatibleConfigurationMismatch)) 2461 Result = ConfigurationMismatch; 2462 break; 2463 } 2464 2465 case FILE_SYSTEM_OPTIONS: { 2466 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2467 if (!AllowCompatibleConfigurationMismatch && 2468 ParseFileSystemOptions(Record, Complain, Listener)) 2469 Result = ConfigurationMismatch; 2470 break; 2471 } 2472 2473 case HEADER_SEARCH_OPTIONS: { 2474 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2475 if (!AllowCompatibleConfigurationMismatch && 2476 ParseHeaderSearchOptions(Record, Complain, Listener)) 2477 Result = ConfigurationMismatch; 2478 break; 2479 } 2480 2481 case PREPROCESSOR_OPTIONS: 2482 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2483 if (!AllowCompatibleConfigurationMismatch && 2484 ParsePreprocessorOptions(Record, Complain, Listener, 2485 SuggestedPredefines)) 2486 Result = ConfigurationMismatch; 2487 break; 2488 } 2489 } 2490 } 2491 2492 ASTReader::ASTReadResult 2493 ASTReader::ReadControlBlock(ModuleFile &F, 2494 SmallVectorImpl<ImportedModule> &Loaded, 2495 const ModuleFile *ImportedBy, 2496 unsigned ClientLoadCapabilities) { 2497 BitstreamCursor &Stream = F.Stream; 2498 ASTReadResult Result = Success; 2499 2500 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2501 Error(std::move(Err)); 2502 return Failure; 2503 } 2504 2505 // Lambda to read the unhashed control block the first time it's called. 2506 // 2507 // For PCM files, the unhashed control block cannot be read until after the 2508 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2509 // need to look ahead before reading the IMPORTS record. For consistency, 2510 // this block is always read somehow (see BitstreamEntry::EndBlock). 2511 bool HasReadUnhashedControlBlock = false; 2512 auto readUnhashedControlBlockOnce = [&]() { 2513 if (!HasReadUnhashedControlBlock) { 2514 HasReadUnhashedControlBlock = true; 2515 if (ASTReadResult Result = 2516 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2517 return Result; 2518 } 2519 return Success; 2520 }; 2521 2522 // Read all of the records and blocks in the control block. 2523 RecordData Record; 2524 unsigned NumInputs = 0; 2525 unsigned NumUserInputs = 0; 2526 StringRef BaseDirectoryAsWritten; 2527 while (true) { 2528 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2529 if (!MaybeEntry) { 2530 Error(MaybeEntry.takeError()); 2531 return Failure; 2532 } 2533 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2534 2535 switch (Entry.Kind) { 2536 case llvm::BitstreamEntry::Error: 2537 Error("malformed block record in AST file"); 2538 return Failure; 2539 case llvm::BitstreamEntry::EndBlock: { 2540 // Validate the module before returning. This call catches an AST with 2541 // no module name and no imports. 2542 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2543 return Result; 2544 2545 // Validate input files. 2546 const HeaderSearchOptions &HSOpts = 2547 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2548 2549 // All user input files reside at the index range [0, NumUserInputs), and 2550 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2551 // loaded module files, ignore missing inputs. 2552 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2553 F.Kind != MK_PrebuiltModule) { 2554 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2555 2556 // If we are reading a module, we will create a verification timestamp, 2557 // so we verify all input files. Otherwise, verify only user input 2558 // files. 2559 2560 unsigned N = NumUserInputs; 2561 if (ValidateSystemInputs || 2562 (HSOpts.ModulesValidateOncePerBuildSession && 2563 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2564 F.Kind == MK_ImplicitModule)) 2565 N = NumInputs; 2566 2567 for (unsigned I = 0; I < N; ++I) { 2568 InputFile IF = getInputFile(F, I+1, Complain); 2569 if (!IF.getFile() || IF.isOutOfDate()) 2570 return OutOfDate; 2571 } 2572 } 2573 2574 if (Listener) 2575 Listener->visitModuleFile(F.FileName, F.Kind); 2576 2577 if (Listener && Listener->needsInputFileVisitation()) { 2578 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2579 : NumUserInputs; 2580 for (unsigned I = 0; I < N; ++I) { 2581 bool IsSystem = I >= NumUserInputs; 2582 InputFileInfo FI = readInputFileInfo(F, I+1); 2583 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2584 F.Kind == MK_ExplicitModule || 2585 F.Kind == MK_PrebuiltModule); 2586 } 2587 } 2588 2589 return Result; 2590 } 2591 2592 case llvm::BitstreamEntry::SubBlock: 2593 switch (Entry.ID) { 2594 case INPUT_FILES_BLOCK_ID: 2595 F.InputFilesCursor = Stream; 2596 if (llvm::Error Err = Stream.SkipBlock()) { 2597 Error(std::move(Err)); 2598 return Failure; 2599 } 2600 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2601 Error("malformed block record in AST file"); 2602 return Failure; 2603 } 2604 continue; 2605 2606 case OPTIONS_BLOCK_ID: 2607 // If we're reading the first module for this group, check its options 2608 // are compatible with ours. For modules it imports, no further checking 2609 // is required, because we checked them when we built it. 2610 if (Listener && !ImportedBy) { 2611 // Should we allow the configuration of the module file to differ from 2612 // the configuration of the current translation unit in a compatible 2613 // way? 2614 // 2615 // FIXME: Allow this for files explicitly specified with -include-pch. 2616 bool AllowCompatibleConfigurationMismatch = 2617 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2618 2619 Result = ReadOptionsBlock(Stream, ClientLoadCapabilities, 2620 AllowCompatibleConfigurationMismatch, 2621 *Listener, SuggestedPredefines); 2622 if (Result == Failure) { 2623 Error("malformed block record in AST file"); 2624 return Result; 2625 } 2626 2627 if (DisableValidation || 2628 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2629 Result = Success; 2630 2631 // If we can't load the module, exit early since we likely 2632 // will rebuild the module anyway. The stream may be in the 2633 // middle of a block. 2634 if (Result != Success) 2635 return Result; 2636 } else if (llvm::Error Err = Stream.SkipBlock()) { 2637 Error(std::move(Err)); 2638 return Failure; 2639 } 2640 continue; 2641 2642 default: 2643 if (llvm::Error Err = Stream.SkipBlock()) { 2644 Error(std::move(Err)); 2645 return Failure; 2646 } 2647 continue; 2648 } 2649 2650 case llvm::BitstreamEntry::Record: 2651 // The interesting case. 2652 break; 2653 } 2654 2655 // Read and process a record. 2656 Record.clear(); 2657 StringRef Blob; 2658 Expected<unsigned> MaybeRecordType = 2659 Stream.readRecord(Entry.ID, Record, &Blob); 2660 if (!MaybeRecordType) { 2661 Error(MaybeRecordType.takeError()); 2662 return Failure; 2663 } 2664 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2665 case METADATA: { 2666 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2667 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2668 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2669 : diag::err_pch_version_too_new); 2670 return VersionMismatch; 2671 } 2672 2673 bool hasErrors = Record[7]; 2674 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2675 Diag(diag::err_pch_with_compiler_errors); 2676 return HadErrors; 2677 } 2678 if (hasErrors) { 2679 Diags.ErrorOccurred = true; 2680 Diags.UncompilableErrorOccurred = true; 2681 Diags.UnrecoverableErrorOccurred = true; 2682 } 2683 2684 F.RelocatablePCH = Record[4]; 2685 // Relative paths in a relocatable PCH are relative to our sysroot. 2686 if (F.RelocatablePCH) 2687 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2688 2689 F.HasTimestamps = Record[5]; 2690 2691 F.PCHHasObjectFile = Record[6]; 2692 2693 const std::string &CurBranch = getClangFullRepositoryVersion(); 2694 StringRef ASTBranch = Blob; 2695 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2696 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2697 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2698 return VersionMismatch; 2699 } 2700 break; 2701 } 2702 2703 case IMPORTS: { 2704 // Validate the AST before processing any imports (otherwise, untangling 2705 // them can be error-prone and expensive). A module will have a name and 2706 // will already have been validated, but this catches the PCH case. 2707 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2708 return Result; 2709 2710 // Load each of the imported PCH files. 2711 unsigned Idx = 0, N = Record.size(); 2712 while (Idx < N) { 2713 // Read information about the AST file. 2714 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2715 // The import location will be the local one for now; we will adjust 2716 // all import locations of module imports after the global source 2717 // location info are setup, in ReadAST. 2718 SourceLocation ImportLoc = 2719 ReadUntranslatedSourceLocation(Record[Idx++]); 2720 off_t StoredSize = (off_t)Record[Idx++]; 2721 time_t StoredModTime = (time_t)Record[Idx++]; 2722 ASTFileSignature StoredSignature = { 2723 {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++], 2724 (uint32_t)Record[Idx++], (uint32_t)Record[Idx++], 2725 (uint32_t)Record[Idx++]}}}; 2726 2727 std::string ImportedName = ReadString(Record, Idx); 2728 std::string ImportedFile; 2729 2730 // For prebuilt and explicit modules first consult the file map for 2731 // an override. Note that here we don't search prebuilt module 2732 // directories, only the explicit name to file mappings. Also, we will 2733 // still verify the size/signature making sure it is essentially the 2734 // same file but perhaps in a different location. 2735 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2736 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2737 ImportedName, /*FileMapOnly*/ true); 2738 2739 if (ImportedFile.empty()) 2740 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2741 // ModuleCache as when writing. 2742 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2743 else 2744 SkipPath(Record, Idx); 2745 2746 // If our client can't cope with us being out of date, we can't cope with 2747 // our dependency being missing. 2748 unsigned Capabilities = ClientLoadCapabilities; 2749 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2750 Capabilities &= ~ARR_Missing; 2751 2752 // Load the AST file. 2753 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2754 Loaded, StoredSize, StoredModTime, 2755 StoredSignature, Capabilities); 2756 2757 // If we diagnosed a problem, produce a backtrace. 2758 if (isDiagnosedResult(Result, Capabilities)) 2759 Diag(diag::note_module_file_imported_by) 2760 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2761 2762 switch (Result) { 2763 case Failure: return Failure; 2764 // If we have to ignore the dependency, we'll have to ignore this too. 2765 case Missing: 2766 case OutOfDate: return OutOfDate; 2767 case VersionMismatch: return VersionMismatch; 2768 case ConfigurationMismatch: return ConfigurationMismatch; 2769 case HadErrors: return HadErrors; 2770 case Success: break; 2771 } 2772 } 2773 break; 2774 } 2775 2776 case ORIGINAL_FILE: 2777 F.OriginalSourceFileID = FileID::get(Record[0]); 2778 F.ActualOriginalSourceFileName = Blob; 2779 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2780 ResolveImportedPath(F, F.OriginalSourceFileName); 2781 break; 2782 2783 case ORIGINAL_FILE_ID: 2784 F.OriginalSourceFileID = FileID::get(Record[0]); 2785 break; 2786 2787 case ORIGINAL_PCH_DIR: 2788 F.OriginalDir = Blob; 2789 break; 2790 2791 case MODULE_NAME: 2792 F.ModuleName = Blob; 2793 Diag(diag::remark_module_import) 2794 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2795 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2796 if (Listener) 2797 Listener->ReadModuleName(F.ModuleName); 2798 2799 // Validate the AST as soon as we have a name so we can exit early on 2800 // failure. 2801 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2802 return Result; 2803 2804 break; 2805 2806 case MODULE_DIRECTORY: { 2807 // Save the BaseDirectory as written in the PCM for computing the module 2808 // filename for the ModuleCache. 2809 BaseDirectoryAsWritten = Blob; 2810 assert(!F.ModuleName.empty() && 2811 "MODULE_DIRECTORY found before MODULE_NAME"); 2812 // If we've already loaded a module map file covering this module, we may 2813 // have a better path for it (relative to the current build). 2814 Module *M = PP.getHeaderSearchInfo().lookupModule( 2815 F.ModuleName, /*AllowSearch*/ true, 2816 /*AllowExtraModuleMapSearch*/ true); 2817 if (M && M->Directory) { 2818 // If we're implicitly loading a module, the base directory can't 2819 // change between the build and use. 2820 // Don't emit module relocation error if we have -fno-validate-pch 2821 if (!PP.getPreprocessorOpts().DisablePCHValidation && 2822 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2823 const DirectoryEntry *BuildDir = 2824 PP.getFileManager().getDirectory(Blob); 2825 if (!BuildDir || BuildDir != M->Directory) { 2826 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2827 Diag(diag::err_imported_module_relocated) 2828 << F.ModuleName << Blob << M->Directory->getName(); 2829 return OutOfDate; 2830 } 2831 } 2832 F.BaseDirectory = M->Directory->getName(); 2833 } else { 2834 F.BaseDirectory = Blob; 2835 } 2836 break; 2837 } 2838 2839 case MODULE_MAP_FILE: 2840 if (ASTReadResult Result = 2841 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2842 return Result; 2843 break; 2844 2845 case INPUT_FILE_OFFSETS: 2846 NumInputs = Record[0]; 2847 NumUserInputs = Record[1]; 2848 F.InputFileOffsets = 2849 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2850 F.InputFilesLoaded.resize(NumInputs); 2851 F.NumUserInputFiles = NumUserInputs; 2852 break; 2853 } 2854 } 2855 } 2856 2857 ASTReader::ASTReadResult 2858 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2859 BitstreamCursor &Stream = F.Stream; 2860 2861 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) { 2862 Error(std::move(Err)); 2863 return Failure; 2864 } 2865 2866 // Read all of the records and blocks for the AST file. 2867 RecordData Record; 2868 while (true) { 2869 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2870 if (!MaybeEntry) { 2871 Error(MaybeEntry.takeError()); 2872 return Failure; 2873 } 2874 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2875 2876 switch (Entry.Kind) { 2877 case llvm::BitstreamEntry::Error: 2878 Error("error at end of module block in AST file"); 2879 return Failure; 2880 case llvm::BitstreamEntry::EndBlock: 2881 // Outside of C++, we do not store a lookup map for the translation unit. 2882 // Instead, mark it as needing a lookup map to be built if this module 2883 // contains any declarations lexically within it (which it always does!). 2884 // This usually has no cost, since we very rarely need the lookup map for 2885 // the translation unit outside C++. 2886 if (ASTContext *Ctx = ContextObj) { 2887 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2888 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2889 DC->setMustBuildLookupTable(); 2890 } 2891 2892 return Success; 2893 case llvm::BitstreamEntry::SubBlock: 2894 switch (Entry.ID) { 2895 case DECLTYPES_BLOCK_ID: 2896 // We lazily load the decls block, but we want to set up the 2897 // DeclsCursor cursor to point into it. Clone our current bitcode 2898 // cursor to it, enter the block and read the abbrevs in that block. 2899 // With the main cursor, we just skip over it. 2900 F.DeclsCursor = Stream; 2901 if (llvm::Error Err = Stream.SkipBlock()) { 2902 Error(std::move(Err)); 2903 return Failure; 2904 } 2905 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 2906 Error("malformed block record in AST file"); 2907 return Failure; 2908 } 2909 break; 2910 2911 case PREPROCESSOR_BLOCK_ID: 2912 F.MacroCursor = Stream; 2913 if (!PP.getExternalSource()) 2914 PP.setExternalSource(this); 2915 2916 if (llvm::Error Err = Stream.SkipBlock()) { 2917 Error(std::move(Err)); 2918 return Failure; 2919 } 2920 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2921 Error("malformed block record in AST file"); 2922 return Failure; 2923 } 2924 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 2925 break; 2926 2927 case PREPROCESSOR_DETAIL_BLOCK_ID: 2928 F.PreprocessorDetailCursor = Stream; 2929 2930 if (llvm::Error Err = Stream.SkipBlock()) { 2931 Error(std::move(Err)); 2932 return Failure; 2933 } 2934 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor, 2935 PREPROCESSOR_DETAIL_BLOCK_ID)) { 2936 Error("malformed preprocessor detail record in AST file"); 2937 return Failure; 2938 } 2939 F.PreprocessorDetailStartOffset 2940 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 2941 2942 if (!PP.getPreprocessingRecord()) 2943 PP.createPreprocessingRecord(); 2944 if (!PP.getPreprocessingRecord()->getExternalSource()) 2945 PP.getPreprocessingRecord()->SetExternalSource(*this); 2946 break; 2947 2948 case SOURCE_MANAGER_BLOCK_ID: 2949 if (ReadSourceManagerBlock(F)) 2950 return Failure; 2951 break; 2952 2953 case SUBMODULE_BLOCK_ID: 2954 if (ASTReadResult Result = 2955 ReadSubmoduleBlock(F, ClientLoadCapabilities)) 2956 return Result; 2957 break; 2958 2959 case COMMENTS_BLOCK_ID: { 2960 BitstreamCursor C = Stream; 2961 2962 if (llvm::Error Err = Stream.SkipBlock()) { 2963 Error(std::move(Err)); 2964 return Failure; 2965 } 2966 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 2967 Error("malformed comments block in AST file"); 2968 return Failure; 2969 } 2970 CommentsCursors.push_back(std::make_pair(C, &F)); 2971 break; 2972 } 2973 2974 default: 2975 if (llvm::Error Err = Stream.SkipBlock()) { 2976 Error(std::move(Err)); 2977 return Failure; 2978 } 2979 break; 2980 } 2981 continue; 2982 2983 case llvm::BitstreamEntry::Record: 2984 // The interesting case. 2985 break; 2986 } 2987 2988 // Read and process a record. 2989 Record.clear(); 2990 StringRef Blob; 2991 Expected<unsigned> MaybeRecordType = 2992 Stream.readRecord(Entry.ID, Record, &Blob); 2993 if (!MaybeRecordType) { 2994 Error(MaybeRecordType.takeError()); 2995 return Failure; 2996 } 2997 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 2998 2999 // If we're not loading an AST context, we don't care about most records. 3000 if (!ContextObj) { 3001 switch (RecordType) { 3002 case IDENTIFIER_TABLE: 3003 case IDENTIFIER_OFFSET: 3004 case INTERESTING_IDENTIFIERS: 3005 case STATISTICS: 3006 case PP_CONDITIONAL_STACK: 3007 case PP_COUNTER_VALUE: 3008 case SOURCE_LOCATION_OFFSETS: 3009 case MODULE_OFFSET_MAP: 3010 case SOURCE_MANAGER_LINE_TABLE: 3011 case SOURCE_LOCATION_PRELOADS: 3012 case PPD_ENTITIES_OFFSETS: 3013 case HEADER_SEARCH_TABLE: 3014 case IMPORTED_MODULES: 3015 case MACRO_OFFSET: 3016 break; 3017 default: 3018 continue; 3019 } 3020 } 3021 3022 switch (RecordType) { 3023 default: // Default behavior: ignore. 3024 break; 3025 3026 case TYPE_OFFSET: { 3027 if (F.LocalNumTypes != 0) { 3028 Error("duplicate TYPE_OFFSET record in AST file"); 3029 return Failure; 3030 } 3031 F.TypeOffsets = (const uint32_t *)Blob.data(); 3032 F.LocalNumTypes = Record[0]; 3033 unsigned LocalBaseTypeIndex = Record[1]; 3034 F.BaseTypeIndex = getTotalNumTypes(); 3035 3036 if (F.LocalNumTypes > 0) { 3037 // Introduce the global -> local mapping for types within this module. 3038 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3039 3040 // Introduce the local -> global mapping for types within this module. 3041 F.TypeRemap.insertOrReplace( 3042 std::make_pair(LocalBaseTypeIndex, 3043 F.BaseTypeIndex - LocalBaseTypeIndex)); 3044 3045 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3046 } 3047 break; 3048 } 3049 3050 case DECL_OFFSET: { 3051 if (F.LocalNumDecls != 0) { 3052 Error("duplicate DECL_OFFSET record in AST file"); 3053 return Failure; 3054 } 3055 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3056 F.LocalNumDecls = Record[0]; 3057 unsigned LocalBaseDeclID = Record[1]; 3058 F.BaseDeclID = getTotalNumDecls(); 3059 3060 if (F.LocalNumDecls > 0) { 3061 // Introduce the global -> local mapping for declarations within this 3062 // module. 3063 GlobalDeclMap.insert( 3064 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3065 3066 // Introduce the local -> global mapping for declarations within this 3067 // module. 3068 F.DeclRemap.insertOrReplace( 3069 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3070 3071 // Introduce the global -> local mapping for declarations within this 3072 // module. 3073 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3074 3075 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3076 } 3077 break; 3078 } 3079 3080 case TU_UPDATE_LEXICAL: { 3081 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3082 LexicalContents Contents( 3083 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3084 Blob.data()), 3085 static_cast<unsigned int>(Blob.size() / 4)); 3086 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3087 TU->setHasExternalLexicalStorage(true); 3088 break; 3089 } 3090 3091 case UPDATE_VISIBLE: { 3092 unsigned Idx = 0; 3093 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3094 auto *Data = (const unsigned char*)Blob.data(); 3095 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3096 // If we've already loaded the decl, perform the updates when we finish 3097 // loading this block. 3098 if (Decl *D = GetExistingDecl(ID)) 3099 PendingUpdateRecords.push_back( 3100 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3101 break; 3102 } 3103 3104 case IDENTIFIER_TABLE: 3105 F.IdentifierTableData = Blob.data(); 3106 if (Record[0]) { 3107 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3108 (const unsigned char *)F.IdentifierTableData + Record[0], 3109 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 3110 (const unsigned char *)F.IdentifierTableData, 3111 ASTIdentifierLookupTrait(*this, F)); 3112 3113 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3114 } 3115 break; 3116 3117 case IDENTIFIER_OFFSET: { 3118 if (F.LocalNumIdentifiers != 0) { 3119 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 3120 return Failure; 3121 } 3122 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3123 F.LocalNumIdentifiers = Record[0]; 3124 unsigned LocalBaseIdentifierID = Record[1]; 3125 F.BaseIdentifierID = getTotalNumIdentifiers(); 3126 3127 if (F.LocalNumIdentifiers > 0) { 3128 // Introduce the global -> local mapping for identifiers within this 3129 // module. 3130 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3131 &F)); 3132 3133 // Introduce the local -> global mapping for identifiers within this 3134 // module. 3135 F.IdentifierRemap.insertOrReplace( 3136 std::make_pair(LocalBaseIdentifierID, 3137 F.BaseIdentifierID - LocalBaseIdentifierID)); 3138 3139 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3140 + F.LocalNumIdentifiers); 3141 } 3142 break; 3143 } 3144 3145 case INTERESTING_IDENTIFIERS: 3146 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3147 break; 3148 3149 case EAGERLY_DESERIALIZED_DECLS: 3150 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3151 // about "interesting" decls (for instance, if we're building a module). 3152 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3153 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3154 break; 3155 3156 case MODULAR_CODEGEN_DECLS: 3157 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3158 // them (ie: if we're not codegenerating this module). 3159 if (F.Kind == MK_MainFile) 3160 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3161 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3162 break; 3163 3164 case SPECIAL_TYPES: 3165 if (SpecialTypes.empty()) { 3166 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3167 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3168 break; 3169 } 3170 3171 if (SpecialTypes.size() != Record.size()) { 3172 Error("invalid special-types record"); 3173 return Failure; 3174 } 3175 3176 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3177 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3178 if (!SpecialTypes[I]) 3179 SpecialTypes[I] = ID; 3180 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3181 // merge step? 3182 } 3183 break; 3184 3185 case STATISTICS: 3186 TotalNumStatements += Record[0]; 3187 TotalNumMacros += Record[1]; 3188 TotalLexicalDeclContexts += Record[2]; 3189 TotalVisibleDeclContexts += Record[3]; 3190 break; 3191 3192 case UNUSED_FILESCOPED_DECLS: 3193 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3194 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3195 break; 3196 3197 case DELEGATING_CTORS: 3198 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3199 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3200 break; 3201 3202 case WEAK_UNDECLARED_IDENTIFIERS: 3203 if (Record.size() % 4 != 0) { 3204 Error("invalid weak identifiers record"); 3205 return Failure; 3206 } 3207 3208 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3209 // files. This isn't the way to do it :) 3210 WeakUndeclaredIdentifiers.clear(); 3211 3212 // Translate the weak, undeclared identifiers into global IDs. 3213 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3214 WeakUndeclaredIdentifiers.push_back( 3215 getGlobalIdentifierID(F, Record[I++])); 3216 WeakUndeclaredIdentifiers.push_back( 3217 getGlobalIdentifierID(F, Record[I++])); 3218 WeakUndeclaredIdentifiers.push_back( 3219 ReadSourceLocation(F, Record, I).getRawEncoding()); 3220 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3221 } 3222 break; 3223 3224 case SELECTOR_OFFSETS: { 3225 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3226 F.LocalNumSelectors = Record[0]; 3227 unsigned LocalBaseSelectorID = Record[1]; 3228 F.BaseSelectorID = getTotalNumSelectors(); 3229 3230 if (F.LocalNumSelectors > 0) { 3231 // Introduce the global -> local mapping for selectors within this 3232 // module. 3233 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3234 3235 // Introduce the local -> global mapping for selectors within this 3236 // module. 3237 F.SelectorRemap.insertOrReplace( 3238 std::make_pair(LocalBaseSelectorID, 3239 F.BaseSelectorID - LocalBaseSelectorID)); 3240 3241 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3242 } 3243 break; 3244 } 3245 3246 case METHOD_POOL: 3247 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3248 if (Record[0]) 3249 F.SelectorLookupTable 3250 = ASTSelectorLookupTable::Create( 3251 F.SelectorLookupTableData + Record[0], 3252 F.SelectorLookupTableData, 3253 ASTSelectorLookupTrait(*this, F)); 3254 TotalNumMethodPoolEntries += Record[1]; 3255 break; 3256 3257 case REFERENCED_SELECTOR_POOL: 3258 if (!Record.empty()) { 3259 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3260 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3261 Record[Idx++])); 3262 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3263 getRawEncoding()); 3264 } 3265 } 3266 break; 3267 3268 case PP_CONDITIONAL_STACK: 3269 if (!Record.empty()) { 3270 unsigned Idx = 0, End = Record.size() - 1; 3271 bool ReachedEOFWhileSkipping = Record[Idx++]; 3272 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3273 if (ReachedEOFWhileSkipping) { 3274 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3275 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3276 bool FoundNonSkipPortion = Record[Idx++]; 3277 bool FoundElse = Record[Idx++]; 3278 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3279 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3280 FoundElse, ElseLoc); 3281 } 3282 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3283 while (Idx < End) { 3284 auto Loc = ReadSourceLocation(F, Record, Idx); 3285 bool WasSkipping = Record[Idx++]; 3286 bool FoundNonSkip = Record[Idx++]; 3287 bool FoundElse = Record[Idx++]; 3288 ConditionalStack.push_back( 3289 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3290 } 3291 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3292 } 3293 break; 3294 3295 case PP_COUNTER_VALUE: 3296 if (!Record.empty() && Listener) 3297 Listener->ReadCounter(F, Record[0]); 3298 break; 3299 3300 case FILE_SORTED_DECLS: 3301 F.FileSortedDecls = (const DeclID *)Blob.data(); 3302 F.NumFileSortedDecls = Record[0]; 3303 break; 3304 3305 case SOURCE_LOCATION_OFFSETS: { 3306 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3307 F.LocalNumSLocEntries = Record[0]; 3308 unsigned SLocSpaceSize = Record[1]; 3309 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3310 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3311 SLocSpaceSize); 3312 if (!F.SLocEntryBaseID) { 3313 Error("ran out of source locations"); 3314 break; 3315 } 3316 // Make our entry in the range map. BaseID is negative and growing, so 3317 // we invert it. Because we invert it, though, we need the other end of 3318 // the range. 3319 unsigned RangeStart = 3320 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3321 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3322 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3323 3324 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3325 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3326 GlobalSLocOffsetMap.insert( 3327 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3328 - SLocSpaceSize,&F)); 3329 3330 // Initialize the remapping table. 3331 // Invalid stays invalid. 3332 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3333 // This module. Base was 2 when being compiled. 3334 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3335 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3336 3337 TotalNumSLocEntries += F.LocalNumSLocEntries; 3338 break; 3339 } 3340 3341 case MODULE_OFFSET_MAP: 3342 F.ModuleOffsetMap = Blob; 3343 break; 3344 3345 case SOURCE_MANAGER_LINE_TABLE: 3346 if (ParseLineTable(F, Record)) 3347 return Failure; 3348 break; 3349 3350 case SOURCE_LOCATION_PRELOADS: { 3351 // Need to transform from the local view (1-based IDs) to the global view, 3352 // which is based off F.SLocEntryBaseID. 3353 if (!F.PreloadSLocEntries.empty()) { 3354 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3355 return Failure; 3356 } 3357 3358 F.PreloadSLocEntries.swap(Record); 3359 break; 3360 } 3361 3362 case EXT_VECTOR_DECLS: 3363 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3364 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3365 break; 3366 3367 case VTABLE_USES: 3368 if (Record.size() % 3 != 0) { 3369 Error("Invalid VTABLE_USES record"); 3370 return Failure; 3371 } 3372 3373 // Later tables overwrite earlier ones. 3374 // FIXME: Modules will have some trouble with this. This is clearly not 3375 // the right way to do this. 3376 VTableUses.clear(); 3377 3378 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3379 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3380 VTableUses.push_back( 3381 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3382 VTableUses.push_back(Record[Idx++]); 3383 } 3384 break; 3385 3386 case PENDING_IMPLICIT_INSTANTIATIONS: 3387 if (PendingInstantiations.size() % 2 != 0) { 3388 Error("Invalid existing PendingInstantiations"); 3389 return Failure; 3390 } 3391 3392 if (Record.size() % 2 != 0) { 3393 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3394 return Failure; 3395 } 3396 3397 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3398 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3399 PendingInstantiations.push_back( 3400 ReadSourceLocation(F, Record, I).getRawEncoding()); 3401 } 3402 break; 3403 3404 case SEMA_DECL_REFS: 3405 if (Record.size() != 3) { 3406 Error("Invalid SEMA_DECL_REFS block"); 3407 return Failure; 3408 } 3409 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3410 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3411 break; 3412 3413 case PPD_ENTITIES_OFFSETS: { 3414 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3415 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3416 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3417 3418 unsigned LocalBasePreprocessedEntityID = Record[0]; 3419 3420 unsigned StartingID; 3421 if (!PP.getPreprocessingRecord()) 3422 PP.createPreprocessingRecord(); 3423 if (!PP.getPreprocessingRecord()->getExternalSource()) 3424 PP.getPreprocessingRecord()->SetExternalSource(*this); 3425 StartingID 3426 = PP.getPreprocessingRecord() 3427 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3428 F.BasePreprocessedEntityID = StartingID; 3429 3430 if (F.NumPreprocessedEntities > 0) { 3431 // Introduce the global -> local mapping for preprocessed entities in 3432 // this module. 3433 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3434 3435 // Introduce the local -> global mapping for preprocessed entities in 3436 // this module. 3437 F.PreprocessedEntityRemap.insertOrReplace( 3438 std::make_pair(LocalBasePreprocessedEntityID, 3439 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3440 } 3441 3442 break; 3443 } 3444 3445 case PPD_SKIPPED_RANGES: { 3446 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3447 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3448 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3449 3450 if (!PP.getPreprocessingRecord()) 3451 PP.createPreprocessingRecord(); 3452 if (!PP.getPreprocessingRecord()->getExternalSource()) 3453 PP.getPreprocessingRecord()->SetExternalSource(*this); 3454 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3455 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3456 3457 if (F.NumPreprocessedSkippedRanges > 0) 3458 GlobalSkippedRangeMap.insert( 3459 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3460 break; 3461 } 3462 3463 case DECL_UPDATE_OFFSETS: 3464 if (Record.size() % 2 != 0) { 3465 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3466 return Failure; 3467 } 3468 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3469 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3470 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3471 3472 // If we've already loaded the decl, perform the updates when we finish 3473 // loading this block. 3474 if (Decl *D = GetExistingDecl(ID)) 3475 PendingUpdateRecords.push_back( 3476 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3477 } 3478 break; 3479 3480 case OBJC_CATEGORIES_MAP: 3481 if (F.LocalNumObjCCategoriesInMap != 0) { 3482 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3483 return Failure; 3484 } 3485 3486 F.LocalNumObjCCategoriesInMap = Record[0]; 3487 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3488 break; 3489 3490 case OBJC_CATEGORIES: 3491 F.ObjCCategories.swap(Record); 3492 break; 3493 3494 case CUDA_SPECIAL_DECL_REFS: 3495 // Later tables overwrite earlier ones. 3496 // FIXME: Modules will have trouble with this. 3497 CUDASpecialDeclRefs.clear(); 3498 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3499 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3500 break; 3501 3502 case HEADER_SEARCH_TABLE: 3503 F.HeaderFileInfoTableData = Blob.data(); 3504 F.LocalNumHeaderFileInfos = Record[1]; 3505 if (Record[0]) { 3506 F.HeaderFileInfoTable 3507 = HeaderFileInfoLookupTable::Create( 3508 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3509 (const unsigned char *)F.HeaderFileInfoTableData, 3510 HeaderFileInfoTrait(*this, F, 3511 &PP.getHeaderSearchInfo(), 3512 Blob.data() + Record[2])); 3513 3514 PP.getHeaderSearchInfo().SetExternalSource(this); 3515 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3516 PP.getHeaderSearchInfo().SetExternalLookup(this); 3517 } 3518 break; 3519 3520 case FP_PRAGMA_OPTIONS: 3521 // Later tables overwrite earlier ones. 3522 FPPragmaOptions.swap(Record); 3523 break; 3524 3525 case OPENCL_EXTENSIONS: 3526 for (unsigned I = 0, E = Record.size(); I != E; ) { 3527 auto Name = ReadString(Record, I); 3528 auto &Opt = OpenCLExtensions.OptMap[Name]; 3529 Opt.Supported = Record[I++] != 0; 3530 Opt.Enabled = Record[I++] != 0; 3531 Opt.Avail = Record[I++]; 3532 Opt.Core = Record[I++]; 3533 } 3534 break; 3535 3536 case OPENCL_EXTENSION_TYPES: 3537 for (unsigned I = 0, E = Record.size(); I != E;) { 3538 auto TypeID = static_cast<::TypeID>(Record[I++]); 3539 auto *Type = GetType(TypeID).getTypePtr(); 3540 auto NumExt = static_cast<unsigned>(Record[I++]); 3541 for (unsigned II = 0; II != NumExt; ++II) { 3542 auto Ext = ReadString(Record, I); 3543 OpenCLTypeExtMap[Type].insert(Ext); 3544 } 3545 } 3546 break; 3547 3548 case OPENCL_EXTENSION_DECLS: 3549 for (unsigned I = 0, E = Record.size(); I != E;) { 3550 auto DeclID = static_cast<::DeclID>(Record[I++]); 3551 auto *Decl = GetDecl(DeclID); 3552 auto NumExt = static_cast<unsigned>(Record[I++]); 3553 for (unsigned II = 0; II != NumExt; ++II) { 3554 auto Ext = ReadString(Record, I); 3555 OpenCLDeclExtMap[Decl].insert(Ext); 3556 } 3557 } 3558 break; 3559 3560 case TENTATIVE_DEFINITIONS: 3561 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3562 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3563 break; 3564 3565 case KNOWN_NAMESPACES: 3566 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3567 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3568 break; 3569 3570 case UNDEFINED_BUT_USED: 3571 if (UndefinedButUsed.size() % 2 != 0) { 3572 Error("Invalid existing UndefinedButUsed"); 3573 return Failure; 3574 } 3575 3576 if (Record.size() % 2 != 0) { 3577 Error("invalid undefined-but-used record"); 3578 return Failure; 3579 } 3580 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3581 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3582 UndefinedButUsed.push_back( 3583 ReadSourceLocation(F, Record, I).getRawEncoding()); 3584 } 3585 break; 3586 3587 case DELETE_EXPRS_TO_ANALYZE: 3588 for (unsigned I = 0, N = Record.size(); I != N;) { 3589 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3590 const uint64_t Count = Record[I++]; 3591 DelayedDeleteExprs.push_back(Count); 3592 for (uint64_t C = 0; C < Count; ++C) { 3593 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3594 bool IsArrayForm = Record[I++] == 1; 3595 DelayedDeleteExprs.push_back(IsArrayForm); 3596 } 3597 } 3598 break; 3599 3600 case IMPORTED_MODULES: 3601 if (!F.isModule()) { 3602 // If we aren't loading a module (which has its own exports), make 3603 // all of the imported modules visible. 3604 // FIXME: Deal with macros-only imports. 3605 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3606 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3607 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3608 if (GlobalID) { 3609 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3610 if (DeserializationListener) 3611 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3612 } 3613 } 3614 } 3615 break; 3616 3617 case MACRO_OFFSET: { 3618 if (F.LocalNumMacros != 0) { 3619 Error("duplicate MACRO_OFFSET record in AST file"); 3620 return Failure; 3621 } 3622 F.MacroOffsets = (const uint32_t *)Blob.data(); 3623 F.LocalNumMacros = Record[0]; 3624 unsigned LocalBaseMacroID = Record[1]; 3625 F.BaseMacroID = getTotalNumMacros(); 3626 3627 if (F.LocalNumMacros > 0) { 3628 // Introduce the global -> local mapping for macros within this module. 3629 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3630 3631 // Introduce the local -> global mapping for macros within this module. 3632 F.MacroRemap.insertOrReplace( 3633 std::make_pair(LocalBaseMacroID, 3634 F.BaseMacroID - LocalBaseMacroID)); 3635 3636 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3637 } 3638 break; 3639 } 3640 3641 case LATE_PARSED_TEMPLATE: 3642 LateParsedTemplates.append(Record.begin(), Record.end()); 3643 break; 3644 3645 case OPTIMIZE_PRAGMA_OPTIONS: 3646 if (Record.size() != 1) { 3647 Error("invalid pragma optimize record"); 3648 return Failure; 3649 } 3650 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3651 break; 3652 3653 case MSSTRUCT_PRAGMA_OPTIONS: 3654 if (Record.size() != 1) { 3655 Error("invalid pragma ms_struct record"); 3656 return Failure; 3657 } 3658 PragmaMSStructState = Record[0]; 3659 break; 3660 3661 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3662 if (Record.size() != 2) { 3663 Error("invalid pragma ms_struct record"); 3664 return Failure; 3665 } 3666 PragmaMSPointersToMembersState = Record[0]; 3667 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3668 break; 3669 3670 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3671 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3672 UnusedLocalTypedefNameCandidates.push_back( 3673 getGlobalDeclID(F, Record[I])); 3674 break; 3675 3676 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3677 if (Record.size() != 1) { 3678 Error("invalid cuda pragma options record"); 3679 return Failure; 3680 } 3681 ForceCUDAHostDeviceDepth = Record[0]; 3682 break; 3683 3684 case PACK_PRAGMA_OPTIONS: { 3685 if (Record.size() < 3) { 3686 Error("invalid pragma pack record"); 3687 return Failure; 3688 } 3689 PragmaPackCurrentValue = Record[0]; 3690 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3691 unsigned NumStackEntries = Record[2]; 3692 unsigned Idx = 3; 3693 // Reset the stack when importing a new module. 3694 PragmaPackStack.clear(); 3695 for (unsigned I = 0; I < NumStackEntries; ++I) { 3696 PragmaPackStackEntry Entry; 3697 Entry.Value = Record[Idx++]; 3698 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3699 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3700 PragmaPackStrings.push_back(ReadString(Record, Idx)); 3701 Entry.SlotLabel = PragmaPackStrings.back(); 3702 PragmaPackStack.push_back(Entry); 3703 } 3704 break; 3705 } 3706 } 3707 } 3708 } 3709 3710 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3711 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3712 3713 // Additional remapping information. 3714 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3715 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3716 F.ModuleOffsetMap = StringRef(); 3717 3718 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3719 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3720 F.SLocRemap.insert(std::make_pair(0U, 0)); 3721 F.SLocRemap.insert(std::make_pair(2U, 1)); 3722 } 3723 3724 // Continuous range maps we may be updating in our module. 3725 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3726 RemapBuilder SLocRemap(F.SLocRemap); 3727 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3728 RemapBuilder MacroRemap(F.MacroRemap); 3729 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3730 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3731 RemapBuilder SelectorRemap(F.SelectorRemap); 3732 RemapBuilder DeclRemap(F.DeclRemap); 3733 RemapBuilder TypeRemap(F.TypeRemap); 3734 3735 while (Data < DataEnd) { 3736 // FIXME: Looking up dependency modules by filename is horrible. Let's 3737 // start fixing this with prebuilt and explicit modules and see how it 3738 // goes... 3739 using namespace llvm::support; 3740 ModuleKind Kind = static_cast<ModuleKind>( 3741 endian::readNext<uint8_t, little, unaligned>(Data)); 3742 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3743 StringRef Name = StringRef((const char*)Data, Len); 3744 Data += Len; 3745 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule 3746 ? ModuleMgr.lookupByModuleName(Name) 3747 : ModuleMgr.lookupByFileName(Name)); 3748 if (!OM) { 3749 std::string Msg = 3750 "SourceLocation remap refers to unknown module, cannot find "; 3751 Msg.append(Name); 3752 Error(Msg); 3753 return; 3754 } 3755 3756 uint32_t SLocOffset = 3757 endian::readNext<uint32_t, little, unaligned>(Data); 3758 uint32_t IdentifierIDOffset = 3759 endian::readNext<uint32_t, little, unaligned>(Data); 3760 uint32_t MacroIDOffset = 3761 endian::readNext<uint32_t, little, unaligned>(Data); 3762 uint32_t PreprocessedEntityIDOffset = 3763 endian::readNext<uint32_t, little, unaligned>(Data); 3764 uint32_t SubmoduleIDOffset = 3765 endian::readNext<uint32_t, little, unaligned>(Data); 3766 uint32_t SelectorIDOffset = 3767 endian::readNext<uint32_t, little, unaligned>(Data); 3768 uint32_t DeclIDOffset = 3769 endian::readNext<uint32_t, little, unaligned>(Data); 3770 uint32_t TypeIndexOffset = 3771 endian::readNext<uint32_t, little, unaligned>(Data); 3772 3773 uint32_t None = std::numeric_limits<uint32_t>::max(); 3774 3775 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3776 RemapBuilder &Remap) { 3777 if (Offset != None) 3778 Remap.insert(std::make_pair(Offset, 3779 static_cast<int>(BaseOffset - Offset))); 3780 }; 3781 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3782 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3783 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3784 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3785 PreprocessedEntityRemap); 3786 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3787 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3788 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3789 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3790 3791 // Global -> local mappings. 3792 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3793 } 3794 } 3795 3796 ASTReader::ASTReadResult 3797 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3798 const ModuleFile *ImportedBy, 3799 unsigned ClientLoadCapabilities) { 3800 unsigned Idx = 0; 3801 F.ModuleMapPath = ReadPath(F, Record, Idx); 3802 3803 // Try to resolve ModuleName in the current header search context and 3804 // verify that it is found in the same module map file as we saved. If the 3805 // top-level AST file is a main file, skip this check because there is no 3806 // usable header search context. 3807 assert(!F.ModuleName.empty() && 3808 "MODULE_NAME should come before MODULE_MAP_FILE"); 3809 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3810 // An implicitly-loaded module file should have its module listed in some 3811 // module map file that we've already loaded. 3812 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3813 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3814 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3815 // Don't emit module relocation error if we have -fno-validate-pch 3816 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) { 3817 assert(ImportedBy && "top-level import should be verified"); 3818 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3819 if (auto *ASTFE = M ? M->getASTFile() : nullptr) { 3820 // This module was defined by an imported (explicit) module. 3821 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3822 << ASTFE->getName(); 3823 } else { 3824 // This module was built with a different module map. 3825 Diag(diag::err_imported_module_not_found) 3826 << F.ModuleName << F.FileName << ImportedBy->FileName 3827 << F.ModuleMapPath; 3828 // In case it was imported by a PCH, there's a chance the user is 3829 // just missing to include the search path to the directory containing 3830 // the modulemap. 3831 if (ImportedBy->Kind == MK_PCH) 3832 Diag(diag::note_imported_by_pch_module_not_found) 3833 << llvm::sys::path::parent_path(F.ModuleMapPath); 3834 } 3835 } 3836 return OutOfDate; 3837 } 3838 3839 assert(M->Name == F.ModuleName && "found module with different name"); 3840 3841 // Check the primary module map file. 3842 const FileEntry *StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3843 if (StoredModMap == nullptr || StoredModMap != ModMap) { 3844 assert(ModMap && "found module is missing module map file"); 3845 assert(ImportedBy && "top-level import should be verified"); 3846 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3847 Diag(diag::err_imported_module_modmap_changed) 3848 << F.ModuleName << ImportedBy->FileName 3849 << ModMap->getName() << F.ModuleMapPath; 3850 return OutOfDate; 3851 } 3852 3853 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3854 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3855 // FIXME: we should use input files rather than storing names. 3856 std::string Filename = ReadPath(F, Record, Idx); 3857 const FileEntry *F = 3858 FileMgr.getFile(Filename, false, false); 3859 if (F == nullptr) { 3860 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3861 Error("could not find file '" + Filename +"' referenced by AST file"); 3862 return OutOfDate; 3863 } 3864 AdditionalStoredMaps.insert(F); 3865 } 3866 3867 // Check any additional module map files (e.g. module.private.modulemap) 3868 // that are not in the pcm. 3869 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3870 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3871 // Remove files that match 3872 // Note: SmallPtrSet::erase is really remove 3873 if (!AdditionalStoredMaps.erase(ModMap)) { 3874 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3875 Diag(diag::err_module_different_modmap) 3876 << F.ModuleName << /*new*/0 << ModMap->getName(); 3877 return OutOfDate; 3878 } 3879 } 3880 } 3881 3882 // Check any additional module map files that are in the pcm, but not 3883 // found in header search. Cases that match are already removed. 3884 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3885 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3886 Diag(diag::err_module_different_modmap) 3887 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3888 return OutOfDate; 3889 } 3890 } 3891 3892 if (Listener) 3893 Listener->ReadModuleMapFile(F.ModuleMapPath); 3894 return Success; 3895 } 3896 3897 /// Move the given method to the back of the global list of methods. 3898 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 3899 // Find the entry for this selector in the method pool. 3900 Sema::GlobalMethodPool::iterator Known 3901 = S.MethodPool.find(Method->getSelector()); 3902 if (Known == S.MethodPool.end()) 3903 return; 3904 3905 // Retrieve the appropriate method list. 3906 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 3907 : Known->second.second; 3908 bool Found = false; 3909 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 3910 if (!Found) { 3911 if (List->getMethod() == Method) { 3912 Found = true; 3913 } else { 3914 // Keep searching. 3915 continue; 3916 } 3917 } 3918 3919 if (List->getNext()) 3920 List->setMethod(List->getNext()->getMethod()); 3921 else 3922 List->setMethod(Method); 3923 } 3924 } 3925 3926 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 3927 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 3928 for (Decl *D : Names) { 3929 bool wasHidden = D->isHidden(); 3930 D->setVisibleDespiteOwningModule(); 3931 3932 if (wasHidden && SemaObj) { 3933 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 3934 moveMethodToBackOfGlobalList(*SemaObj, Method); 3935 } 3936 } 3937 } 3938 } 3939 3940 void ASTReader::makeModuleVisible(Module *Mod, 3941 Module::NameVisibilityKind NameVisibility, 3942 SourceLocation ImportLoc) { 3943 llvm::SmallPtrSet<Module *, 4> Visited; 3944 SmallVector<Module *, 4> Stack; 3945 Stack.push_back(Mod); 3946 while (!Stack.empty()) { 3947 Mod = Stack.pop_back_val(); 3948 3949 if (NameVisibility <= Mod->NameVisibility) { 3950 // This module already has this level of visibility (or greater), so 3951 // there is nothing more to do. 3952 continue; 3953 } 3954 3955 if (!Mod->isAvailable()) { 3956 // Modules that aren't available cannot be made visible. 3957 continue; 3958 } 3959 3960 // Update the module's name visibility. 3961 Mod->NameVisibility = NameVisibility; 3962 3963 // If we've already deserialized any names from this module, 3964 // mark them as visible. 3965 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 3966 if (Hidden != HiddenNamesMap.end()) { 3967 auto HiddenNames = std::move(*Hidden); 3968 HiddenNamesMap.erase(Hidden); 3969 makeNamesVisible(HiddenNames.second, HiddenNames.first); 3970 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 3971 "making names visible added hidden names"); 3972 } 3973 3974 // Push any exported modules onto the stack to be marked as visible. 3975 SmallVector<Module *, 16> Exports; 3976 Mod->getExportedModules(Exports); 3977 for (SmallVectorImpl<Module *>::iterator 3978 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 3979 Module *Exported = *I; 3980 if (Visited.insert(Exported).second) 3981 Stack.push_back(Exported); 3982 } 3983 } 3984 } 3985 3986 /// We've merged the definition \p MergedDef into the existing definition 3987 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 3988 /// visible. 3989 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 3990 NamedDecl *MergedDef) { 3991 if (Def->isHidden()) { 3992 // If MergedDef is visible or becomes visible, make the definition visible. 3993 if (!MergedDef->isHidden()) 3994 Def->setVisibleDespiteOwningModule(); 3995 else { 3996 getContext().mergeDefinitionIntoModule( 3997 Def, MergedDef->getImportedOwningModule(), 3998 /*NotifyListeners*/ false); 3999 PendingMergedDefinitionsToDeduplicate.insert(Def); 4000 } 4001 } 4002 } 4003 4004 bool ASTReader::loadGlobalIndex() { 4005 if (GlobalIndex) 4006 return false; 4007 4008 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4009 !PP.getLangOpts().Modules) 4010 return true; 4011 4012 // Try to load the global index. 4013 TriedLoadingGlobalIndex = true; 4014 StringRef ModuleCachePath 4015 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4016 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4017 GlobalModuleIndex::readIndex(ModuleCachePath); 4018 if (llvm::Error Err = std::move(Result.second)) { 4019 assert(!Result.first); 4020 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4021 return true; 4022 } 4023 4024 GlobalIndex.reset(Result.first); 4025 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4026 return false; 4027 } 4028 4029 bool ASTReader::isGlobalIndexUnavailable() const { 4030 return PP.getLangOpts().Modules && UseGlobalIndex && 4031 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4032 } 4033 4034 static void updateModuleTimestamp(ModuleFile &MF) { 4035 // Overwrite the timestamp file contents so that file's mtime changes. 4036 std::string TimestampFilename = MF.getTimestampFilename(); 4037 std::error_code EC; 4038 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::F_Text); 4039 if (EC) 4040 return; 4041 OS << "Timestamp file\n"; 4042 OS.close(); 4043 OS.clear_error(); // Avoid triggering a fatal error. 4044 } 4045 4046 /// Given a cursor at the start of an AST file, scan ahead and drop the 4047 /// cursor into the start of the given block ID, returning false on success and 4048 /// true on failure. 4049 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4050 while (true) { 4051 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4052 if (!MaybeEntry) { 4053 // FIXME this drops errors on the floor. 4054 consumeError(MaybeEntry.takeError()); 4055 return true; 4056 } 4057 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4058 4059 switch (Entry.Kind) { 4060 case llvm::BitstreamEntry::Error: 4061 case llvm::BitstreamEntry::EndBlock: 4062 return true; 4063 4064 case llvm::BitstreamEntry::Record: 4065 // Ignore top-level records. 4066 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4067 break; 4068 else { 4069 // FIXME this drops errors on the floor. 4070 consumeError(Skipped.takeError()); 4071 return true; 4072 } 4073 4074 case llvm::BitstreamEntry::SubBlock: 4075 if (Entry.ID == BlockID) { 4076 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4077 // FIXME this drops the error on the floor. 4078 consumeError(std::move(Err)); 4079 return true; 4080 } 4081 // Found it! 4082 return false; 4083 } 4084 4085 if (llvm::Error Err = Cursor.SkipBlock()) { 4086 // FIXME this drops the error on the floor. 4087 consumeError(std::move(Err)); 4088 return true; 4089 } 4090 } 4091 } 4092 } 4093 4094 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4095 ModuleKind Type, 4096 SourceLocation ImportLoc, 4097 unsigned ClientLoadCapabilities, 4098 SmallVectorImpl<ImportedSubmodule> *Imported) { 4099 llvm::SaveAndRestore<SourceLocation> 4100 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4101 4102 // Defer any pending actions until we get to the end of reading the AST file. 4103 Deserializing AnASTFile(this); 4104 4105 // Bump the generation number. 4106 unsigned PreviousGeneration = 0; 4107 if (ContextObj) 4108 PreviousGeneration = incrementGeneration(*ContextObj); 4109 4110 unsigned NumModules = ModuleMgr.size(); 4111 SmallVector<ImportedModule, 4> Loaded; 4112 switch (ASTReadResult ReadResult = 4113 ReadASTCore(FileName, Type, ImportLoc, 4114 /*ImportedBy=*/nullptr, Loaded, 0, 0, 4115 ASTFileSignature(), ClientLoadCapabilities)) { 4116 case Failure: 4117 case Missing: 4118 case OutOfDate: 4119 case VersionMismatch: 4120 case ConfigurationMismatch: 4121 case HadErrors: { 4122 llvm::SmallPtrSet<ModuleFile *, 4> LoadedSet; 4123 for (const ImportedModule &IM : Loaded) 4124 LoadedSet.insert(IM.Mod); 4125 4126 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, LoadedSet, 4127 PP.getLangOpts().Modules 4128 ? &PP.getHeaderSearchInfo().getModuleMap() 4129 : nullptr); 4130 4131 // If we find that any modules are unusable, the global index is going 4132 // to be out-of-date. Just remove it. 4133 GlobalIndex.reset(); 4134 ModuleMgr.setGlobalIndex(nullptr); 4135 return ReadResult; 4136 } 4137 case Success: 4138 break; 4139 } 4140 4141 // Here comes stuff that we only do once the entire chain is loaded. 4142 4143 // Load the AST blocks of all of the modules that we loaded. 4144 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 4145 MEnd = Loaded.end(); 4146 M != MEnd; ++M) { 4147 ModuleFile &F = *M->Mod; 4148 4149 // Read the AST block. 4150 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 4151 return Result; 4152 4153 // Read the extension blocks. 4154 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4155 if (ASTReadResult Result = ReadExtensionBlock(F)) 4156 return Result; 4157 } 4158 4159 // Once read, set the ModuleFile bit base offset and update the size in 4160 // bits of all files we've seen. 4161 F.GlobalBitOffset = TotalModulesSizeInBits; 4162 TotalModulesSizeInBits += F.SizeInBits; 4163 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4164 4165 // Preload SLocEntries. 4166 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4167 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4168 // Load it through the SourceManager and don't call ReadSLocEntry() 4169 // directly because the entry may have already been loaded in which case 4170 // calling ReadSLocEntry() directly would trigger an assertion in 4171 // SourceManager. 4172 SourceMgr.getLoadedSLocEntryByID(Index); 4173 } 4174 4175 // Map the original source file ID into the ID space of the current 4176 // compilation. 4177 if (F.OriginalSourceFileID.isValid()) { 4178 F.OriginalSourceFileID = FileID::get( 4179 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4180 } 4181 4182 // Preload all the pending interesting identifiers by marking them out of 4183 // date. 4184 for (auto Offset : F.PreloadIdentifierOffsets) { 4185 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 4186 F.IdentifierTableData + Offset); 4187 4188 ASTIdentifierLookupTrait Trait(*this, F); 4189 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4190 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4191 auto &II = PP.getIdentifierTable().getOwn(Key); 4192 II.setOutOfDate(true); 4193 4194 // Mark this identifier as being from an AST file so that we can track 4195 // whether we need to serialize it. 4196 markIdentifierFromAST(*this, II); 4197 4198 // Associate the ID with the identifier so that the writer can reuse it. 4199 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4200 SetIdentifierInfo(ID, &II); 4201 } 4202 } 4203 4204 // Setup the import locations and notify the module manager that we've 4205 // committed to these module files. 4206 for (SmallVectorImpl<ImportedModule>::iterator M = Loaded.begin(), 4207 MEnd = Loaded.end(); 4208 M != MEnd; ++M) { 4209 ModuleFile &F = *M->Mod; 4210 4211 ModuleMgr.moduleFileAccepted(&F); 4212 4213 // Set the import location. 4214 F.DirectImportLoc = ImportLoc; 4215 // FIXME: We assume that locations from PCH / preamble do not need 4216 // any translation. 4217 if (!M->ImportedBy) 4218 F.ImportLoc = M->ImportLoc; 4219 else 4220 F.ImportLoc = TranslateSourceLocation(*M->ImportedBy, M->ImportLoc); 4221 } 4222 4223 if (!PP.getLangOpts().CPlusPlus || 4224 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4225 Type != MK_PrebuiltModule)) { 4226 // Mark all of the identifiers in the identifier table as being out of date, 4227 // so that various accessors know to check the loaded modules when the 4228 // identifier is used. 4229 // 4230 // For C++ modules, we don't need information on many identifiers (just 4231 // those that provide macros or are poisoned), so we mark all of 4232 // the interesting ones via PreloadIdentifierOffsets. 4233 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4234 IdEnd = PP.getIdentifierTable().end(); 4235 Id != IdEnd; ++Id) 4236 Id->second->setOutOfDate(true); 4237 } 4238 // Mark selectors as out of date. 4239 for (auto Sel : SelectorGeneration) 4240 SelectorOutOfDate[Sel.first] = true; 4241 4242 // Resolve any unresolved module exports. 4243 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4244 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4245 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4246 Module *ResolvedMod = getSubmodule(GlobalID); 4247 4248 switch (Unresolved.Kind) { 4249 case UnresolvedModuleRef::Conflict: 4250 if (ResolvedMod) { 4251 Module::Conflict Conflict; 4252 Conflict.Other = ResolvedMod; 4253 Conflict.Message = Unresolved.String.str(); 4254 Unresolved.Mod->Conflicts.push_back(Conflict); 4255 } 4256 continue; 4257 4258 case UnresolvedModuleRef::Import: 4259 if (ResolvedMod) 4260 Unresolved.Mod->Imports.insert(ResolvedMod); 4261 continue; 4262 4263 case UnresolvedModuleRef::Export: 4264 if (ResolvedMod || Unresolved.IsWildcard) 4265 Unresolved.Mod->Exports.push_back( 4266 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4267 continue; 4268 } 4269 } 4270 UnresolvedModuleRefs.clear(); 4271 4272 if (Imported) 4273 Imported->append(ImportedModules.begin(), 4274 ImportedModules.end()); 4275 4276 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4277 // Might be unnecessary as use declarations are only used to build the 4278 // module itself. 4279 4280 if (ContextObj) 4281 InitializeContext(); 4282 4283 if (SemaObj) 4284 UpdateSema(); 4285 4286 if (DeserializationListener) 4287 DeserializationListener->ReaderInitialized(this); 4288 4289 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4290 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4291 // If this AST file is a precompiled preamble, then set the 4292 // preamble file ID of the source manager to the file source file 4293 // from which the preamble was built. 4294 if (Type == MK_Preamble) { 4295 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4296 } else if (Type == MK_MainFile) { 4297 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4298 } 4299 } 4300 4301 // For any Objective-C class definitions we have already loaded, make sure 4302 // that we load any additional categories. 4303 if (ContextObj) { 4304 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4305 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4306 ObjCClassesLoaded[I], 4307 PreviousGeneration); 4308 } 4309 } 4310 4311 if (PP.getHeaderSearchInfo() 4312 .getHeaderSearchOpts() 4313 .ModulesValidateOncePerBuildSession) { 4314 // Now we are certain that the module and all modules it depends on are 4315 // up to date. Create or update timestamp files for modules that are 4316 // located in the module cache (not for PCH files that could be anywhere 4317 // in the filesystem). 4318 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4319 ImportedModule &M = Loaded[I]; 4320 if (M.Mod->Kind == MK_ImplicitModule) { 4321 updateModuleTimestamp(*M.Mod); 4322 } 4323 } 4324 } 4325 4326 return Success; 4327 } 4328 4329 static ASTFileSignature readASTFileSignature(StringRef PCH); 4330 4331 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4332 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4333 // FIXME checking magic headers is done in other places such as 4334 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4335 // always done the same. Unify it all with a helper. 4336 if (!Stream.canSkipToPos(4)) 4337 return llvm::createStringError(std::errc::illegal_byte_sequence, 4338 "file too small to contain AST file magic"); 4339 for (unsigned C : {'C', 'P', 'C', 'H'}) 4340 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4341 if (Res.get() != C) 4342 return llvm::createStringError( 4343 std::errc::illegal_byte_sequence, 4344 "file doesn't start with AST file magic"); 4345 } else 4346 return Res.takeError(); 4347 return llvm::Error::success(); 4348 } 4349 4350 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4351 switch (Kind) { 4352 case MK_PCH: 4353 return 0; // PCH 4354 case MK_ImplicitModule: 4355 case MK_ExplicitModule: 4356 case MK_PrebuiltModule: 4357 return 1; // module 4358 case MK_MainFile: 4359 case MK_Preamble: 4360 return 2; // main source file 4361 } 4362 llvm_unreachable("unknown module kind"); 4363 } 4364 4365 ASTReader::ASTReadResult 4366 ASTReader::ReadASTCore(StringRef FileName, 4367 ModuleKind Type, 4368 SourceLocation ImportLoc, 4369 ModuleFile *ImportedBy, 4370 SmallVectorImpl<ImportedModule> &Loaded, 4371 off_t ExpectedSize, time_t ExpectedModTime, 4372 ASTFileSignature ExpectedSignature, 4373 unsigned ClientLoadCapabilities) { 4374 ModuleFile *M; 4375 std::string ErrorStr; 4376 ModuleManager::AddModuleResult AddResult 4377 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4378 getGeneration(), ExpectedSize, ExpectedModTime, 4379 ExpectedSignature, readASTFileSignature, 4380 M, ErrorStr); 4381 4382 switch (AddResult) { 4383 case ModuleManager::AlreadyLoaded: 4384 Diag(diag::remark_module_import) 4385 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4386 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4387 return Success; 4388 4389 case ModuleManager::NewlyLoaded: 4390 // Load module file below. 4391 break; 4392 4393 case ModuleManager::Missing: 4394 // The module file was missing; if the client can handle that, return 4395 // it. 4396 if (ClientLoadCapabilities & ARR_Missing) 4397 return Missing; 4398 4399 // Otherwise, return an error. 4400 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type) 4401 << FileName << !ErrorStr.empty() 4402 << ErrorStr; 4403 return Failure; 4404 4405 case ModuleManager::OutOfDate: 4406 // We couldn't load the module file because it is out-of-date. If the 4407 // client can handle out-of-date, return it. 4408 if (ClientLoadCapabilities & ARR_OutOfDate) 4409 return OutOfDate; 4410 4411 // Otherwise, return an error. 4412 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type) 4413 << FileName << !ErrorStr.empty() 4414 << ErrorStr; 4415 return Failure; 4416 } 4417 4418 assert(M && "Missing module file"); 4419 4420 bool ShouldFinalizePCM = false; 4421 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4422 auto &MC = getModuleManager().getModuleCache(); 4423 if (ShouldFinalizePCM) 4424 MC.finalizePCM(FileName); 4425 else 4426 MC.tryToDropPCM(FileName); 4427 }); 4428 ModuleFile &F = *M; 4429 BitstreamCursor &Stream = F.Stream; 4430 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4431 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4432 4433 // Sniff for the signature. 4434 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4435 Diag(diag::err_module_file_invalid) 4436 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4437 return Failure; 4438 } 4439 4440 // This is used for compatibility with older PCH formats. 4441 bool HaveReadControlBlock = false; 4442 while (true) { 4443 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4444 if (!MaybeEntry) { 4445 Error(MaybeEntry.takeError()); 4446 return Failure; 4447 } 4448 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4449 4450 switch (Entry.Kind) { 4451 case llvm::BitstreamEntry::Error: 4452 case llvm::BitstreamEntry::Record: 4453 case llvm::BitstreamEntry::EndBlock: 4454 Error("invalid record at top-level of AST file"); 4455 return Failure; 4456 4457 case llvm::BitstreamEntry::SubBlock: 4458 break; 4459 } 4460 4461 switch (Entry.ID) { 4462 case CONTROL_BLOCK_ID: 4463 HaveReadControlBlock = true; 4464 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4465 case Success: 4466 // Check that we didn't try to load a non-module AST file as a module. 4467 // 4468 // FIXME: Should we also perform the converse check? Loading a module as 4469 // a PCH file sort of works, but it's a bit wonky. 4470 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4471 Type == MK_PrebuiltModule) && 4472 F.ModuleName.empty()) { 4473 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4474 if (Result != OutOfDate || 4475 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4476 Diag(diag::err_module_file_not_module) << FileName; 4477 return Result; 4478 } 4479 break; 4480 4481 case Failure: return Failure; 4482 case Missing: return Missing; 4483 case OutOfDate: return OutOfDate; 4484 case VersionMismatch: return VersionMismatch; 4485 case ConfigurationMismatch: return ConfigurationMismatch; 4486 case HadErrors: return HadErrors; 4487 } 4488 break; 4489 4490 case AST_BLOCK_ID: 4491 if (!HaveReadControlBlock) { 4492 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4493 Diag(diag::err_pch_version_too_old); 4494 return VersionMismatch; 4495 } 4496 4497 // Record that we've loaded this module. 4498 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4499 ShouldFinalizePCM = true; 4500 return Success; 4501 4502 case UNHASHED_CONTROL_BLOCK_ID: 4503 // This block is handled using look-ahead during ReadControlBlock. We 4504 // shouldn't get here! 4505 Error("malformed block record in AST file"); 4506 return Failure; 4507 4508 default: 4509 if (llvm::Error Err = Stream.SkipBlock()) { 4510 Error(std::move(Err)); 4511 return Failure; 4512 } 4513 break; 4514 } 4515 } 4516 4517 llvm_unreachable("unexpected break; expected return"); 4518 } 4519 4520 ASTReader::ASTReadResult 4521 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4522 unsigned ClientLoadCapabilities) { 4523 const HeaderSearchOptions &HSOpts = 4524 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4525 bool AllowCompatibleConfigurationMismatch = 4526 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4527 4528 ASTReadResult Result = readUnhashedControlBlockImpl( 4529 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4530 Listener.get(), 4531 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4532 4533 // If F was directly imported by another module, it's implicitly validated by 4534 // the importing module. 4535 if (DisableValidation || WasImportedBy || 4536 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4537 return Success; 4538 4539 if (Result == Failure) { 4540 Error("malformed block record in AST file"); 4541 return Failure; 4542 } 4543 4544 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4545 // If this module has already been finalized in the ModuleCache, we're stuck 4546 // with it; we can only load a single version of each module. 4547 // 4548 // This can happen when a module is imported in two contexts: in one, as a 4549 // user module; in another, as a system module (due to an import from 4550 // another module marked with the [system] flag). It usually indicates a 4551 // bug in the module map: this module should also be marked with [system]. 4552 // 4553 // If -Wno-system-headers (the default), and the first import is as a 4554 // system module, then validation will fail during the as-user import, 4555 // since -Werror flags won't have been validated. However, it's reasonable 4556 // to treat this consistently as a system module. 4557 // 4558 // If -Wsystem-headers, the PCM on disk was built with 4559 // -Wno-system-headers, and the first import is as a user module, then 4560 // validation will fail during the as-system import since the PCM on disk 4561 // doesn't guarantee that -Werror was respected. However, the -Werror 4562 // flags were checked during the initial as-user import. 4563 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4564 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4565 return Success; 4566 } 4567 } 4568 4569 return Result; 4570 } 4571 4572 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4573 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4574 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4575 bool ValidateDiagnosticOptions) { 4576 // Initialize a stream. 4577 BitstreamCursor Stream(StreamData); 4578 4579 // Sniff for the signature. 4580 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4581 // FIXME this drops the error on the floor. 4582 consumeError(std::move(Err)); 4583 return Failure; 4584 } 4585 4586 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4587 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4588 return Failure; 4589 4590 // Read all of the records in the options block. 4591 RecordData Record; 4592 ASTReadResult Result = Success; 4593 while (true) { 4594 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4595 if (!MaybeEntry) { 4596 // FIXME this drops the error on the floor. 4597 consumeError(MaybeEntry.takeError()); 4598 return Failure; 4599 } 4600 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4601 4602 switch (Entry.Kind) { 4603 case llvm::BitstreamEntry::Error: 4604 case llvm::BitstreamEntry::SubBlock: 4605 return Failure; 4606 4607 case llvm::BitstreamEntry::EndBlock: 4608 return Result; 4609 4610 case llvm::BitstreamEntry::Record: 4611 // The interesting case. 4612 break; 4613 } 4614 4615 // Read and process a record. 4616 Record.clear(); 4617 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4618 if (!MaybeRecordType) { 4619 // FIXME this drops the error. 4620 return Failure; 4621 } 4622 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4623 case SIGNATURE: 4624 if (F) 4625 std::copy(Record.begin(), Record.end(), F->Signature.data()); 4626 break; 4627 case DIAGNOSTIC_OPTIONS: { 4628 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4629 if (Listener && ValidateDiagnosticOptions && 4630 !AllowCompatibleConfigurationMismatch && 4631 ParseDiagnosticOptions(Record, Complain, *Listener)) 4632 Result = OutOfDate; // Don't return early. Read the signature. 4633 break; 4634 } 4635 case DIAG_PRAGMA_MAPPINGS: 4636 if (!F) 4637 break; 4638 if (F->PragmaDiagMappings.empty()) 4639 F->PragmaDiagMappings.swap(Record); 4640 else 4641 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4642 Record.begin(), Record.end()); 4643 break; 4644 } 4645 } 4646 } 4647 4648 /// Parse a record and blob containing module file extension metadata. 4649 static bool parseModuleFileExtensionMetadata( 4650 const SmallVectorImpl<uint64_t> &Record, 4651 StringRef Blob, 4652 ModuleFileExtensionMetadata &Metadata) { 4653 if (Record.size() < 4) return true; 4654 4655 Metadata.MajorVersion = Record[0]; 4656 Metadata.MinorVersion = Record[1]; 4657 4658 unsigned BlockNameLen = Record[2]; 4659 unsigned UserInfoLen = Record[3]; 4660 4661 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4662 4663 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4664 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4665 Blob.data() + BlockNameLen + UserInfoLen); 4666 return false; 4667 } 4668 4669 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4670 BitstreamCursor &Stream = F.Stream; 4671 4672 RecordData Record; 4673 while (true) { 4674 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4675 if (!MaybeEntry) { 4676 Error(MaybeEntry.takeError()); 4677 return Failure; 4678 } 4679 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4680 4681 switch (Entry.Kind) { 4682 case llvm::BitstreamEntry::SubBlock: 4683 if (llvm::Error Err = Stream.SkipBlock()) { 4684 Error(std::move(Err)); 4685 return Failure; 4686 } 4687 continue; 4688 4689 case llvm::BitstreamEntry::EndBlock: 4690 return Success; 4691 4692 case llvm::BitstreamEntry::Error: 4693 return HadErrors; 4694 4695 case llvm::BitstreamEntry::Record: 4696 break; 4697 } 4698 4699 Record.clear(); 4700 StringRef Blob; 4701 Expected<unsigned> MaybeRecCode = 4702 Stream.readRecord(Entry.ID, Record, &Blob); 4703 if (!MaybeRecCode) { 4704 Error(MaybeRecCode.takeError()); 4705 return Failure; 4706 } 4707 switch (MaybeRecCode.get()) { 4708 case EXTENSION_METADATA: { 4709 ModuleFileExtensionMetadata Metadata; 4710 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4711 return Failure; 4712 4713 // Find a module file extension with this block name. 4714 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4715 if (Known == ModuleFileExtensions.end()) break; 4716 4717 // Form a reader. 4718 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4719 F, Stream)) { 4720 F.ExtensionReaders.push_back(std::move(Reader)); 4721 } 4722 4723 break; 4724 } 4725 } 4726 } 4727 4728 return Success; 4729 } 4730 4731 void ASTReader::InitializeContext() { 4732 assert(ContextObj && "no context to initialize"); 4733 ASTContext &Context = *ContextObj; 4734 4735 // If there's a listener, notify them that we "read" the translation unit. 4736 if (DeserializationListener) 4737 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4738 Context.getTranslationUnitDecl()); 4739 4740 // FIXME: Find a better way to deal with collisions between these 4741 // built-in types. Right now, we just ignore the problem. 4742 4743 // Load the special types. 4744 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4745 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4746 if (!Context.CFConstantStringTypeDecl) 4747 Context.setCFConstantStringType(GetType(String)); 4748 } 4749 4750 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4751 QualType FileType = GetType(File); 4752 if (FileType.isNull()) { 4753 Error("FILE type is NULL"); 4754 return; 4755 } 4756 4757 if (!Context.FILEDecl) { 4758 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4759 Context.setFILEDecl(Typedef->getDecl()); 4760 else { 4761 const TagType *Tag = FileType->getAs<TagType>(); 4762 if (!Tag) { 4763 Error("Invalid FILE type in AST file"); 4764 return; 4765 } 4766 Context.setFILEDecl(Tag->getDecl()); 4767 } 4768 } 4769 } 4770 4771 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4772 QualType Jmp_bufType = GetType(Jmp_buf); 4773 if (Jmp_bufType.isNull()) { 4774 Error("jmp_buf type is NULL"); 4775 return; 4776 } 4777 4778 if (!Context.jmp_bufDecl) { 4779 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4780 Context.setjmp_bufDecl(Typedef->getDecl()); 4781 else { 4782 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4783 if (!Tag) { 4784 Error("Invalid jmp_buf type in AST file"); 4785 return; 4786 } 4787 Context.setjmp_bufDecl(Tag->getDecl()); 4788 } 4789 } 4790 } 4791 4792 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4793 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4794 if (Sigjmp_bufType.isNull()) { 4795 Error("sigjmp_buf type is NULL"); 4796 return; 4797 } 4798 4799 if (!Context.sigjmp_bufDecl) { 4800 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4801 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4802 else { 4803 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4804 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4805 Context.setsigjmp_bufDecl(Tag->getDecl()); 4806 } 4807 } 4808 } 4809 4810 if (unsigned ObjCIdRedef 4811 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4812 if (Context.ObjCIdRedefinitionType.isNull()) 4813 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4814 } 4815 4816 if (unsigned ObjCClassRedef 4817 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4818 if (Context.ObjCClassRedefinitionType.isNull()) 4819 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4820 } 4821 4822 if (unsigned ObjCSelRedef 4823 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4824 if (Context.ObjCSelRedefinitionType.isNull()) 4825 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4826 } 4827 4828 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4829 QualType Ucontext_tType = GetType(Ucontext_t); 4830 if (Ucontext_tType.isNull()) { 4831 Error("ucontext_t type is NULL"); 4832 return; 4833 } 4834 4835 if (!Context.ucontext_tDecl) { 4836 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4837 Context.setucontext_tDecl(Typedef->getDecl()); 4838 else { 4839 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4840 assert(Tag && "Invalid ucontext_t type in AST file"); 4841 Context.setucontext_tDecl(Tag->getDecl()); 4842 } 4843 } 4844 } 4845 } 4846 4847 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4848 4849 // If there were any CUDA special declarations, deserialize them. 4850 if (!CUDASpecialDeclRefs.empty()) { 4851 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4852 Context.setcudaConfigureCallDecl( 4853 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4854 } 4855 4856 // Re-export any modules that were imported by a non-module AST file. 4857 // FIXME: This does not make macro-only imports visible again. 4858 for (auto &Import : ImportedModules) { 4859 if (Module *Imported = getSubmodule(Import.ID)) { 4860 makeModuleVisible(Imported, Module::AllVisible, 4861 /*ImportLoc=*/Import.ImportLoc); 4862 if (Import.ImportLoc.isValid()) 4863 PP.makeModuleVisible(Imported, Import.ImportLoc); 4864 // FIXME: should we tell Sema to make the module visible too? 4865 } 4866 } 4867 ImportedModules.clear(); 4868 } 4869 4870 void ASTReader::finalizeForWriting() { 4871 // Nothing to do for now. 4872 } 4873 4874 /// Reads and return the signature record from \p PCH's control block, or 4875 /// else returns 0. 4876 static ASTFileSignature readASTFileSignature(StringRef PCH) { 4877 BitstreamCursor Stream(PCH); 4878 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4879 // FIXME this drops the error on the floor. 4880 consumeError(std::move(Err)); 4881 return ASTFileSignature(); 4882 } 4883 4884 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4885 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4886 return ASTFileSignature(); 4887 4888 // Scan for SIGNATURE inside the diagnostic options block. 4889 ASTReader::RecordData Record; 4890 while (true) { 4891 Expected<llvm::BitstreamEntry> MaybeEntry = 4892 Stream.advanceSkippingSubblocks(); 4893 if (!MaybeEntry) { 4894 // FIXME this drops the error on the floor. 4895 consumeError(MaybeEntry.takeError()); 4896 return ASTFileSignature(); 4897 } 4898 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4899 4900 if (Entry.Kind != llvm::BitstreamEntry::Record) 4901 return ASTFileSignature(); 4902 4903 Record.clear(); 4904 StringRef Blob; 4905 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 4906 if (!MaybeRecord) { 4907 // FIXME this drops the error on the floor. 4908 consumeError(MaybeRecord.takeError()); 4909 return ASTFileSignature(); 4910 } 4911 if (SIGNATURE == MaybeRecord.get()) 4912 return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2], 4913 (uint32_t)Record[3], (uint32_t)Record[4]}}}; 4914 } 4915 } 4916 4917 /// Retrieve the name of the original source file name 4918 /// directly from the AST file, without actually loading the AST 4919 /// file. 4920 std::string ASTReader::getOriginalSourceFile( 4921 const std::string &ASTFileName, FileManager &FileMgr, 4922 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 4923 // Open the AST file. 4924 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 4925 if (!Buffer) { 4926 Diags.Report(diag::err_fe_unable_to_read_pch_file) 4927 << ASTFileName << Buffer.getError().message(); 4928 return std::string(); 4929 } 4930 4931 // Initialize the stream 4932 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 4933 4934 // Sniff for the signature. 4935 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4936 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 4937 return std::string(); 4938 } 4939 4940 // Scan for the CONTROL_BLOCK_ID block. 4941 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 4942 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 4943 return std::string(); 4944 } 4945 4946 // Scan for ORIGINAL_FILE inside the control block. 4947 RecordData Record; 4948 while (true) { 4949 Expected<llvm::BitstreamEntry> MaybeEntry = 4950 Stream.advanceSkippingSubblocks(); 4951 if (!MaybeEntry) { 4952 // FIXME this drops errors on the floor. 4953 consumeError(MaybeEntry.takeError()); 4954 return std::string(); 4955 } 4956 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4957 4958 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 4959 return std::string(); 4960 4961 if (Entry.Kind != llvm::BitstreamEntry::Record) { 4962 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 4963 return std::string(); 4964 } 4965 4966 Record.clear(); 4967 StringRef Blob; 4968 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 4969 if (!MaybeRecord) { 4970 // FIXME this drops the errors on the floor. 4971 consumeError(MaybeRecord.takeError()); 4972 return std::string(); 4973 } 4974 if (ORIGINAL_FILE == MaybeRecord.get()) 4975 return Blob.str(); 4976 } 4977 } 4978 4979 namespace { 4980 4981 class SimplePCHValidator : public ASTReaderListener { 4982 const LangOptions &ExistingLangOpts; 4983 const TargetOptions &ExistingTargetOpts; 4984 const PreprocessorOptions &ExistingPPOpts; 4985 std::string ExistingModuleCachePath; 4986 FileManager &FileMgr; 4987 4988 public: 4989 SimplePCHValidator(const LangOptions &ExistingLangOpts, 4990 const TargetOptions &ExistingTargetOpts, 4991 const PreprocessorOptions &ExistingPPOpts, 4992 StringRef ExistingModuleCachePath, 4993 FileManager &FileMgr) 4994 : ExistingLangOpts(ExistingLangOpts), 4995 ExistingTargetOpts(ExistingTargetOpts), 4996 ExistingPPOpts(ExistingPPOpts), 4997 ExistingModuleCachePath(ExistingModuleCachePath), 4998 FileMgr(FileMgr) {} 4999 5000 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5001 bool AllowCompatibleDifferences) override { 5002 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5003 AllowCompatibleDifferences); 5004 } 5005 5006 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5007 bool AllowCompatibleDifferences) override { 5008 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5009 AllowCompatibleDifferences); 5010 } 5011 5012 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5013 StringRef SpecificModuleCachePath, 5014 bool Complain) override { 5015 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5016 ExistingModuleCachePath, 5017 nullptr, ExistingLangOpts); 5018 } 5019 5020 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5021 bool Complain, 5022 std::string &SuggestedPredefines) override { 5023 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5024 SuggestedPredefines, ExistingLangOpts); 5025 } 5026 }; 5027 5028 } // namespace 5029 5030 bool ASTReader::readASTFileControlBlock( 5031 StringRef Filename, FileManager &FileMgr, 5032 const PCHContainerReader &PCHContainerRdr, 5033 bool FindModuleFileExtensions, 5034 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5035 // Open the AST file. 5036 // FIXME: This allows use of the VFS; we do not allow use of the 5037 // VFS when actually loading a module. 5038 auto Buffer = FileMgr.getBufferForFile(Filename); 5039 if (!Buffer) { 5040 return true; 5041 } 5042 5043 // Initialize the stream 5044 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5045 BitstreamCursor Stream(Bytes); 5046 5047 // Sniff for the signature. 5048 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5049 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5050 return true; 5051 } 5052 5053 // Scan for the CONTROL_BLOCK_ID block. 5054 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5055 return true; 5056 5057 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5058 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5059 bool NeedsImports = Listener.needsImportVisitation(); 5060 BitstreamCursor InputFilesCursor; 5061 5062 RecordData Record; 5063 std::string ModuleDir; 5064 bool DoneWithControlBlock = false; 5065 while (!DoneWithControlBlock) { 5066 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5067 if (!MaybeEntry) { 5068 // FIXME this drops the error on the floor. 5069 consumeError(MaybeEntry.takeError()); 5070 return true; 5071 } 5072 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5073 5074 switch (Entry.Kind) { 5075 case llvm::BitstreamEntry::SubBlock: { 5076 switch (Entry.ID) { 5077 case OPTIONS_BLOCK_ID: { 5078 std::string IgnoredSuggestedPredefines; 5079 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5080 /*AllowCompatibleConfigurationMismatch*/ false, 5081 Listener, IgnoredSuggestedPredefines) != Success) 5082 return true; 5083 break; 5084 } 5085 5086 case INPUT_FILES_BLOCK_ID: 5087 InputFilesCursor = Stream; 5088 if (llvm::Error Err = Stream.SkipBlock()) { 5089 // FIXME this drops the error on the floor. 5090 consumeError(std::move(Err)); 5091 return true; 5092 } 5093 if (NeedsInputFiles && 5094 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5095 return true; 5096 break; 5097 5098 default: 5099 if (llvm::Error Err = Stream.SkipBlock()) { 5100 // FIXME this drops the error on the floor. 5101 consumeError(std::move(Err)); 5102 return true; 5103 } 5104 break; 5105 } 5106 5107 continue; 5108 } 5109 5110 case llvm::BitstreamEntry::EndBlock: 5111 DoneWithControlBlock = true; 5112 break; 5113 5114 case llvm::BitstreamEntry::Error: 5115 return true; 5116 5117 case llvm::BitstreamEntry::Record: 5118 break; 5119 } 5120 5121 if (DoneWithControlBlock) break; 5122 5123 Record.clear(); 5124 StringRef Blob; 5125 Expected<unsigned> MaybeRecCode = 5126 Stream.readRecord(Entry.ID, Record, &Blob); 5127 if (!MaybeRecCode) { 5128 // FIXME this drops the error. 5129 return Failure; 5130 } 5131 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5132 case METADATA: 5133 if (Record[0] != VERSION_MAJOR) 5134 return true; 5135 if (Listener.ReadFullVersionInformation(Blob)) 5136 return true; 5137 break; 5138 case MODULE_NAME: 5139 Listener.ReadModuleName(Blob); 5140 break; 5141 case MODULE_DIRECTORY: 5142 ModuleDir = Blob; 5143 break; 5144 case MODULE_MAP_FILE: { 5145 unsigned Idx = 0; 5146 auto Path = ReadString(Record, Idx); 5147 ResolveImportedPath(Path, ModuleDir); 5148 Listener.ReadModuleMapFile(Path); 5149 break; 5150 } 5151 case INPUT_FILE_OFFSETS: { 5152 if (!NeedsInputFiles) 5153 break; 5154 5155 unsigned NumInputFiles = Record[0]; 5156 unsigned NumUserFiles = Record[1]; 5157 const llvm::support::unaligned_uint64_t *InputFileOffs = 5158 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5159 for (unsigned I = 0; I != NumInputFiles; ++I) { 5160 // Go find this input file. 5161 bool isSystemFile = I >= NumUserFiles; 5162 5163 if (isSystemFile && !NeedsSystemInputFiles) 5164 break; // the rest are system input files 5165 5166 BitstreamCursor &Cursor = InputFilesCursor; 5167 SavedStreamPosition SavedPosition(Cursor); 5168 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5169 // FIXME this drops errors on the floor. 5170 consumeError(std::move(Err)); 5171 } 5172 5173 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5174 if (!MaybeCode) { 5175 // FIXME this drops errors on the floor. 5176 consumeError(MaybeCode.takeError()); 5177 } 5178 unsigned Code = MaybeCode.get(); 5179 5180 RecordData Record; 5181 StringRef Blob; 5182 bool shouldContinue = false; 5183 Expected<unsigned> MaybeRecordType = 5184 Cursor.readRecord(Code, Record, &Blob); 5185 if (!MaybeRecordType) { 5186 // FIXME this drops errors on the floor. 5187 consumeError(MaybeRecordType.takeError()); 5188 } 5189 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5190 case INPUT_FILE: 5191 bool Overridden = static_cast<bool>(Record[3]); 5192 std::string Filename = Blob; 5193 ResolveImportedPath(Filename, ModuleDir); 5194 shouldContinue = Listener.visitInputFile( 5195 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5196 break; 5197 } 5198 if (!shouldContinue) 5199 break; 5200 } 5201 break; 5202 } 5203 5204 case IMPORTS: { 5205 if (!NeedsImports) 5206 break; 5207 5208 unsigned Idx = 0, N = Record.size(); 5209 while (Idx < N) { 5210 // Read information about the AST file. 5211 Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature 5212 std::string ModuleName = ReadString(Record, Idx); 5213 std::string Filename = ReadString(Record, Idx); 5214 ResolveImportedPath(Filename, ModuleDir); 5215 Listener.visitImport(ModuleName, Filename); 5216 } 5217 break; 5218 } 5219 5220 default: 5221 // No other validation to perform. 5222 break; 5223 } 5224 } 5225 5226 // Look for module file extension blocks, if requested. 5227 if (FindModuleFileExtensions) { 5228 BitstreamCursor SavedStream = Stream; 5229 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5230 bool DoneWithExtensionBlock = false; 5231 while (!DoneWithExtensionBlock) { 5232 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5233 if (!MaybeEntry) { 5234 // FIXME this drops the error. 5235 return true; 5236 } 5237 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5238 5239 switch (Entry.Kind) { 5240 case llvm::BitstreamEntry::SubBlock: 5241 if (llvm::Error Err = Stream.SkipBlock()) { 5242 // FIXME this drops the error on the floor. 5243 consumeError(std::move(Err)); 5244 return true; 5245 } 5246 continue; 5247 5248 case llvm::BitstreamEntry::EndBlock: 5249 DoneWithExtensionBlock = true; 5250 continue; 5251 5252 case llvm::BitstreamEntry::Error: 5253 return true; 5254 5255 case llvm::BitstreamEntry::Record: 5256 break; 5257 } 5258 5259 Record.clear(); 5260 StringRef Blob; 5261 Expected<unsigned> MaybeRecCode = 5262 Stream.readRecord(Entry.ID, Record, &Blob); 5263 if (!MaybeRecCode) { 5264 // FIXME this drops the error. 5265 return true; 5266 } 5267 switch (MaybeRecCode.get()) { 5268 case EXTENSION_METADATA: { 5269 ModuleFileExtensionMetadata Metadata; 5270 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5271 return true; 5272 5273 Listener.readModuleFileExtension(Metadata); 5274 break; 5275 } 5276 } 5277 } 5278 } 5279 Stream = SavedStream; 5280 } 5281 5282 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5283 if (readUnhashedControlBlockImpl( 5284 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5285 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5286 ValidateDiagnosticOptions) != Success) 5287 return true; 5288 5289 return false; 5290 } 5291 5292 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5293 const PCHContainerReader &PCHContainerRdr, 5294 const LangOptions &LangOpts, 5295 const TargetOptions &TargetOpts, 5296 const PreprocessorOptions &PPOpts, 5297 StringRef ExistingModuleCachePath) { 5298 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5299 ExistingModuleCachePath, FileMgr); 5300 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5301 /*FindModuleFileExtensions=*/false, 5302 validator, 5303 /*ValidateDiagnosticOptions=*/true); 5304 } 5305 5306 ASTReader::ASTReadResult 5307 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 5308 // Enter the submodule block. 5309 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 5310 Error(std::move(Err)); 5311 return Failure; 5312 } 5313 5314 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5315 bool First = true; 5316 Module *CurrentModule = nullptr; 5317 RecordData Record; 5318 while (true) { 5319 Expected<llvm::BitstreamEntry> MaybeEntry = 5320 F.Stream.advanceSkippingSubblocks(); 5321 if (!MaybeEntry) { 5322 Error(MaybeEntry.takeError()); 5323 return Failure; 5324 } 5325 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5326 5327 switch (Entry.Kind) { 5328 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5329 case llvm::BitstreamEntry::Error: 5330 Error("malformed block record in AST file"); 5331 return Failure; 5332 case llvm::BitstreamEntry::EndBlock: 5333 return Success; 5334 case llvm::BitstreamEntry::Record: 5335 // The interesting case. 5336 break; 5337 } 5338 5339 // Read a record. 5340 StringRef Blob; 5341 Record.clear(); 5342 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5343 if (!MaybeKind) { 5344 Error(MaybeKind.takeError()); 5345 return Failure; 5346 } 5347 unsigned Kind = MaybeKind.get(); 5348 5349 if ((Kind == SUBMODULE_METADATA) != First) { 5350 Error("submodule metadata record should be at beginning of block"); 5351 return Failure; 5352 } 5353 First = false; 5354 5355 // Submodule information is only valid if we have a current module. 5356 // FIXME: Should we error on these cases? 5357 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5358 Kind != SUBMODULE_DEFINITION) 5359 continue; 5360 5361 switch (Kind) { 5362 default: // Default behavior: ignore. 5363 break; 5364 5365 case SUBMODULE_DEFINITION: { 5366 if (Record.size() < 12) { 5367 Error("malformed module definition"); 5368 return Failure; 5369 } 5370 5371 StringRef Name = Blob; 5372 unsigned Idx = 0; 5373 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5374 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5375 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5376 bool IsFramework = Record[Idx++]; 5377 bool IsExplicit = Record[Idx++]; 5378 bool IsSystem = Record[Idx++]; 5379 bool IsExternC = Record[Idx++]; 5380 bool InferSubmodules = Record[Idx++]; 5381 bool InferExplicitSubmodules = Record[Idx++]; 5382 bool InferExportWildcard = Record[Idx++]; 5383 bool ConfigMacrosExhaustive = Record[Idx++]; 5384 bool ModuleMapIsPrivate = Record[Idx++]; 5385 5386 Module *ParentModule = nullptr; 5387 if (Parent) 5388 ParentModule = getSubmodule(Parent); 5389 5390 // Retrieve this (sub)module from the module map, creating it if 5391 // necessary. 5392 CurrentModule = 5393 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5394 .first; 5395 5396 // FIXME: set the definition loc for CurrentModule, or call 5397 // ModMap.setInferredModuleAllowedBy() 5398 5399 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5400 if (GlobalIndex >= SubmodulesLoaded.size() || 5401 SubmodulesLoaded[GlobalIndex]) { 5402 Error("too many submodules"); 5403 return Failure; 5404 } 5405 5406 if (!ParentModule) { 5407 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5408 // Don't emit module relocation error if we have -fno-validate-pch 5409 if (!PP.getPreprocessorOpts().DisablePCHValidation && 5410 CurFile != F.File) { 5411 if (!Diags.isDiagnosticInFlight()) { 5412 Diag(diag::err_module_file_conflict) 5413 << CurrentModule->getTopLevelModuleName() 5414 << CurFile->getName() 5415 << F.File->getName(); 5416 } 5417 return Failure; 5418 } 5419 } 5420 5421 CurrentModule->setASTFile(F.File); 5422 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5423 } 5424 5425 CurrentModule->Kind = Kind; 5426 CurrentModule->Signature = F.Signature; 5427 CurrentModule->IsFromModuleFile = true; 5428 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5429 CurrentModule->IsExternC = IsExternC; 5430 CurrentModule->InferSubmodules = InferSubmodules; 5431 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5432 CurrentModule->InferExportWildcard = InferExportWildcard; 5433 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5434 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5435 if (DeserializationListener) 5436 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5437 5438 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5439 5440 // Clear out data that will be replaced by what is in the module file. 5441 CurrentModule->LinkLibraries.clear(); 5442 CurrentModule->ConfigMacros.clear(); 5443 CurrentModule->UnresolvedConflicts.clear(); 5444 CurrentModule->Conflicts.clear(); 5445 5446 // The module is available unless it's missing a requirement; relevant 5447 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5448 // Missing headers that were present when the module was built do not 5449 // make it unavailable -- if we got this far, this must be an explicitly 5450 // imported module file. 5451 CurrentModule->Requirements.clear(); 5452 CurrentModule->MissingHeaders.clear(); 5453 CurrentModule->IsMissingRequirement = 5454 ParentModule && ParentModule->IsMissingRequirement; 5455 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement; 5456 break; 5457 } 5458 5459 case SUBMODULE_UMBRELLA_HEADER: { 5460 std::string Filename = Blob; 5461 ResolveImportedPath(F, Filename); 5462 if (auto *Umbrella = PP.getFileManager().getFile(Filename)) { 5463 if (!CurrentModule->getUmbrellaHeader()) 5464 ModMap.setUmbrellaHeader(CurrentModule, Umbrella, Blob); 5465 else if (CurrentModule->getUmbrellaHeader().Entry != Umbrella) { 5466 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5467 Error("mismatched umbrella headers in submodule"); 5468 return OutOfDate; 5469 } 5470 } 5471 break; 5472 } 5473 5474 case SUBMODULE_HEADER: 5475 case SUBMODULE_EXCLUDED_HEADER: 5476 case SUBMODULE_PRIVATE_HEADER: 5477 // We lazily associate headers with their modules via the HeaderInfo table. 5478 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5479 // of complete filenames or remove it entirely. 5480 break; 5481 5482 case SUBMODULE_TEXTUAL_HEADER: 5483 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5484 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5485 // them here. 5486 break; 5487 5488 case SUBMODULE_TOPHEADER: 5489 CurrentModule->addTopHeaderFilename(Blob); 5490 break; 5491 5492 case SUBMODULE_UMBRELLA_DIR: { 5493 std::string Dirname = Blob; 5494 ResolveImportedPath(F, Dirname); 5495 if (auto *Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5496 if (!CurrentModule->getUmbrellaDir()) 5497 ModMap.setUmbrellaDir(CurrentModule, Umbrella, Blob); 5498 else if (CurrentModule->getUmbrellaDir().Entry != Umbrella) { 5499 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5500 Error("mismatched umbrella directories in submodule"); 5501 return OutOfDate; 5502 } 5503 } 5504 break; 5505 } 5506 5507 case SUBMODULE_METADATA: { 5508 F.BaseSubmoduleID = getTotalNumSubmodules(); 5509 F.LocalNumSubmodules = Record[0]; 5510 unsigned LocalBaseSubmoduleID = Record[1]; 5511 if (F.LocalNumSubmodules > 0) { 5512 // Introduce the global -> local mapping for submodules within this 5513 // module. 5514 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5515 5516 // Introduce the local -> global mapping for submodules within this 5517 // module. 5518 F.SubmoduleRemap.insertOrReplace( 5519 std::make_pair(LocalBaseSubmoduleID, 5520 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5521 5522 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5523 } 5524 break; 5525 } 5526 5527 case SUBMODULE_IMPORTS: 5528 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5529 UnresolvedModuleRef Unresolved; 5530 Unresolved.File = &F; 5531 Unresolved.Mod = CurrentModule; 5532 Unresolved.ID = Record[Idx]; 5533 Unresolved.Kind = UnresolvedModuleRef::Import; 5534 Unresolved.IsWildcard = false; 5535 UnresolvedModuleRefs.push_back(Unresolved); 5536 } 5537 break; 5538 5539 case SUBMODULE_EXPORTS: 5540 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5541 UnresolvedModuleRef Unresolved; 5542 Unresolved.File = &F; 5543 Unresolved.Mod = CurrentModule; 5544 Unresolved.ID = Record[Idx]; 5545 Unresolved.Kind = UnresolvedModuleRef::Export; 5546 Unresolved.IsWildcard = Record[Idx + 1]; 5547 UnresolvedModuleRefs.push_back(Unresolved); 5548 } 5549 5550 // Once we've loaded the set of exports, there's no reason to keep 5551 // the parsed, unresolved exports around. 5552 CurrentModule->UnresolvedExports.clear(); 5553 break; 5554 5555 case SUBMODULE_REQUIRES: 5556 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5557 PP.getTargetInfo()); 5558 break; 5559 5560 case SUBMODULE_LINK_LIBRARY: 5561 ModMap.resolveLinkAsDependencies(CurrentModule); 5562 CurrentModule->LinkLibraries.push_back( 5563 Module::LinkLibrary(Blob, Record[0])); 5564 break; 5565 5566 case SUBMODULE_CONFIG_MACRO: 5567 CurrentModule->ConfigMacros.push_back(Blob.str()); 5568 break; 5569 5570 case SUBMODULE_CONFLICT: { 5571 UnresolvedModuleRef Unresolved; 5572 Unresolved.File = &F; 5573 Unresolved.Mod = CurrentModule; 5574 Unresolved.ID = Record[0]; 5575 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5576 Unresolved.IsWildcard = false; 5577 Unresolved.String = Blob; 5578 UnresolvedModuleRefs.push_back(Unresolved); 5579 break; 5580 } 5581 5582 case SUBMODULE_INITIALIZERS: { 5583 if (!ContextObj) 5584 break; 5585 SmallVector<uint32_t, 16> Inits; 5586 for (auto &ID : Record) 5587 Inits.push_back(getGlobalDeclID(F, ID)); 5588 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5589 break; 5590 } 5591 5592 case SUBMODULE_EXPORT_AS: 5593 CurrentModule->ExportAsModule = Blob.str(); 5594 ModMap.addLinkAsDependency(CurrentModule); 5595 break; 5596 } 5597 } 5598 } 5599 5600 /// Parse the record that corresponds to a LangOptions data 5601 /// structure. 5602 /// 5603 /// This routine parses the language options from the AST file and then gives 5604 /// them to the AST listener if one is set. 5605 /// 5606 /// \returns true if the listener deems the file unacceptable, false otherwise. 5607 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5608 bool Complain, 5609 ASTReaderListener &Listener, 5610 bool AllowCompatibleDifferences) { 5611 LangOptions LangOpts; 5612 unsigned Idx = 0; 5613 #define LANGOPT(Name, Bits, Default, Description) \ 5614 LangOpts.Name = Record[Idx++]; 5615 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5616 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5617 #include "clang/Basic/LangOptions.def" 5618 #define SANITIZER(NAME, ID) \ 5619 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5620 #include "clang/Basic/Sanitizers.def" 5621 5622 for (unsigned N = Record[Idx++]; N; --N) 5623 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5624 5625 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5626 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5627 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5628 5629 LangOpts.CurrentModule = ReadString(Record, Idx); 5630 5631 // Comment options. 5632 for (unsigned N = Record[Idx++]; N; --N) { 5633 LangOpts.CommentOpts.BlockCommandNames.push_back( 5634 ReadString(Record, Idx)); 5635 } 5636 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5637 5638 // OpenMP offloading options. 5639 for (unsigned N = Record[Idx++]; N; --N) { 5640 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5641 } 5642 5643 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5644 5645 return Listener.ReadLanguageOptions(LangOpts, Complain, 5646 AllowCompatibleDifferences); 5647 } 5648 5649 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5650 ASTReaderListener &Listener, 5651 bool AllowCompatibleDifferences) { 5652 unsigned Idx = 0; 5653 TargetOptions TargetOpts; 5654 TargetOpts.Triple = ReadString(Record, Idx); 5655 TargetOpts.CPU = ReadString(Record, Idx); 5656 TargetOpts.ABI = ReadString(Record, Idx); 5657 for (unsigned N = Record[Idx++]; N; --N) { 5658 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5659 } 5660 for (unsigned N = Record[Idx++]; N; --N) { 5661 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5662 } 5663 5664 return Listener.ReadTargetOptions(TargetOpts, Complain, 5665 AllowCompatibleDifferences); 5666 } 5667 5668 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5669 ASTReaderListener &Listener) { 5670 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5671 unsigned Idx = 0; 5672 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5673 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5674 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5675 #include "clang/Basic/DiagnosticOptions.def" 5676 5677 for (unsigned N = Record[Idx++]; N; --N) 5678 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5679 for (unsigned N = Record[Idx++]; N; --N) 5680 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5681 5682 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5683 } 5684 5685 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5686 ASTReaderListener &Listener) { 5687 FileSystemOptions FSOpts; 5688 unsigned Idx = 0; 5689 FSOpts.WorkingDir = ReadString(Record, Idx); 5690 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5691 } 5692 5693 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5694 bool Complain, 5695 ASTReaderListener &Listener) { 5696 HeaderSearchOptions HSOpts; 5697 unsigned Idx = 0; 5698 HSOpts.Sysroot = ReadString(Record, Idx); 5699 5700 // Include entries. 5701 for (unsigned N = Record[Idx++]; N; --N) { 5702 std::string Path = ReadString(Record, Idx); 5703 frontend::IncludeDirGroup Group 5704 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5705 bool IsFramework = Record[Idx++]; 5706 bool IgnoreSysRoot = Record[Idx++]; 5707 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5708 IgnoreSysRoot); 5709 } 5710 5711 // System header prefixes. 5712 for (unsigned N = Record[Idx++]; N; --N) { 5713 std::string Prefix = ReadString(Record, Idx); 5714 bool IsSystemHeader = Record[Idx++]; 5715 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5716 } 5717 5718 HSOpts.ResourceDir = ReadString(Record, Idx); 5719 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5720 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5721 HSOpts.DisableModuleHash = Record[Idx++]; 5722 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5723 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5724 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5725 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5726 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5727 HSOpts.UseLibcxx = Record[Idx++]; 5728 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5729 5730 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5731 Complain); 5732 } 5733 5734 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5735 bool Complain, 5736 ASTReaderListener &Listener, 5737 std::string &SuggestedPredefines) { 5738 PreprocessorOptions PPOpts; 5739 unsigned Idx = 0; 5740 5741 // Macro definitions/undefs 5742 for (unsigned N = Record[Idx++]; N; --N) { 5743 std::string Macro = ReadString(Record, Idx); 5744 bool IsUndef = Record[Idx++]; 5745 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5746 } 5747 5748 // Includes 5749 for (unsigned N = Record[Idx++]; N; --N) { 5750 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5751 } 5752 5753 // Macro Includes 5754 for (unsigned N = Record[Idx++]; N; --N) { 5755 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5756 } 5757 5758 PPOpts.UsePredefines = Record[Idx++]; 5759 PPOpts.DetailedRecord = Record[Idx++]; 5760 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5761 PPOpts.ObjCXXARCStandardLibrary = 5762 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5763 SuggestedPredefines.clear(); 5764 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5765 SuggestedPredefines); 5766 } 5767 5768 std::pair<ModuleFile *, unsigned> 5769 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5770 GlobalPreprocessedEntityMapType::iterator 5771 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5772 assert(I != GlobalPreprocessedEntityMap.end() && 5773 "Corrupted global preprocessed entity map"); 5774 ModuleFile *M = I->second; 5775 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5776 return std::make_pair(M, LocalIndex); 5777 } 5778 5779 llvm::iterator_range<PreprocessingRecord::iterator> 5780 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5781 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5782 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5783 Mod.NumPreprocessedEntities); 5784 5785 return llvm::make_range(PreprocessingRecord::iterator(), 5786 PreprocessingRecord::iterator()); 5787 } 5788 5789 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5790 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5791 return llvm::make_range( 5792 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5793 ModuleDeclIterator(this, &Mod, 5794 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5795 } 5796 5797 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5798 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5799 assert(I != GlobalSkippedRangeMap.end() && 5800 "Corrupted global skipped range map"); 5801 ModuleFile *M = I->second; 5802 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5803 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5804 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5805 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5806 TranslateSourceLocation(*M, RawRange.getEnd())); 5807 assert(Range.isValid()); 5808 return Range; 5809 } 5810 5811 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5812 PreprocessedEntityID PPID = Index+1; 5813 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5814 ModuleFile &M = *PPInfo.first; 5815 unsigned LocalIndex = PPInfo.second; 5816 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5817 5818 if (!PP.getPreprocessingRecord()) { 5819 Error("no preprocessing record"); 5820 return nullptr; 5821 } 5822 5823 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5824 if (llvm::Error Err = 5825 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) { 5826 Error(std::move(Err)); 5827 return nullptr; 5828 } 5829 5830 Expected<llvm::BitstreamEntry> MaybeEntry = 5831 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5832 if (!MaybeEntry) { 5833 Error(MaybeEntry.takeError()); 5834 return nullptr; 5835 } 5836 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5837 5838 if (Entry.Kind != llvm::BitstreamEntry::Record) 5839 return nullptr; 5840 5841 // Read the record. 5842 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5843 TranslateSourceLocation(M, PPOffs.getEnd())); 5844 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5845 StringRef Blob; 5846 RecordData Record; 5847 Expected<unsigned> MaybeRecType = 5848 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5849 if (!MaybeRecType) { 5850 Error(MaybeRecType.takeError()); 5851 return nullptr; 5852 } 5853 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 5854 case PPD_MACRO_EXPANSION: { 5855 bool isBuiltin = Record[0]; 5856 IdentifierInfo *Name = nullptr; 5857 MacroDefinitionRecord *Def = nullptr; 5858 if (isBuiltin) 5859 Name = getLocalIdentifier(M, Record[1]); 5860 else { 5861 PreprocessedEntityID GlobalID = 5862 getGlobalPreprocessedEntityID(M, Record[1]); 5863 Def = cast<MacroDefinitionRecord>( 5864 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5865 } 5866 5867 MacroExpansion *ME; 5868 if (isBuiltin) 5869 ME = new (PPRec) MacroExpansion(Name, Range); 5870 else 5871 ME = new (PPRec) MacroExpansion(Def, Range); 5872 5873 return ME; 5874 } 5875 5876 case PPD_MACRO_DEFINITION: { 5877 // Decode the identifier info and then check again; if the macro is 5878 // still defined and associated with the identifier, 5879 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 5880 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 5881 5882 if (DeserializationListener) 5883 DeserializationListener->MacroDefinitionRead(PPID, MD); 5884 5885 return MD; 5886 } 5887 5888 case PPD_INCLUSION_DIRECTIVE: { 5889 const char *FullFileNameStart = Blob.data() + Record[0]; 5890 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 5891 const FileEntry *File = nullptr; 5892 if (!FullFileName.empty()) 5893 File = PP.getFileManager().getFile(FullFileName); 5894 5895 // FIXME: Stable encoding 5896 InclusionDirective::InclusionKind Kind 5897 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 5898 InclusionDirective *ID 5899 = new (PPRec) InclusionDirective(PPRec, Kind, 5900 StringRef(Blob.data(), Record[0]), 5901 Record[1], Record[3], 5902 File, 5903 Range); 5904 return ID; 5905 } 5906 } 5907 5908 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 5909 } 5910 5911 /// Find the next module that contains entities and return the ID 5912 /// of the first entry. 5913 /// 5914 /// \param SLocMapI points at a chunk of a module that contains no 5915 /// preprocessed entities or the entities it contains are not the ones we are 5916 /// looking for. 5917 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 5918 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 5919 ++SLocMapI; 5920 for (GlobalSLocOffsetMapType::const_iterator 5921 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 5922 ModuleFile &M = *SLocMapI->second; 5923 if (M.NumPreprocessedEntities) 5924 return M.BasePreprocessedEntityID; 5925 } 5926 5927 return getTotalNumPreprocessedEntities(); 5928 } 5929 5930 namespace { 5931 5932 struct PPEntityComp { 5933 const ASTReader &Reader; 5934 ModuleFile &M; 5935 5936 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 5937 5938 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 5939 SourceLocation LHS = getLoc(L); 5940 SourceLocation RHS = getLoc(R); 5941 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5942 } 5943 5944 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 5945 SourceLocation LHS = getLoc(L); 5946 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5947 } 5948 5949 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 5950 SourceLocation RHS = getLoc(R); 5951 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 5952 } 5953 5954 SourceLocation getLoc(const PPEntityOffset &PPE) const { 5955 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 5956 } 5957 }; 5958 5959 } // namespace 5960 5961 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 5962 bool EndsAfter) const { 5963 if (SourceMgr.isLocalSourceLocation(Loc)) 5964 return getTotalNumPreprocessedEntities(); 5965 5966 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 5967 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 5968 assert(SLocMapI != GlobalSLocOffsetMap.end() && 5969 "Corrupted global sloc offset map"); 5970 5971 if (SLocMapI->second->NumPreprocessedEntities == 0) 5972 return findNextPreprocessedEntity(SLocMapI); 5973 5974 ModuleFile &M = *SLocMapI->second; 5975 5976 using pp_iterator = const PPEntityOffset *; 5977 5978 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 5979 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 5980 5981 size_t Count = M.NumPreprocessedEntities; 5982 size_t Half; 5983 pp_iterator First = pp_begin; 5984 pp_iterator PPI; 5985 5986 if (EndsAfter) { 5987 PPI = std::upper_bound(pp_begin, pp_end, Loc, 5988 PPEntityComp(*this, M)); 5989 } else { 5990 // Do a binary search manually instead of using std::lower_bound because 5991 // The end locations of entities may be unordered (when a macro expansion 5992 // is inside another macro argument), but for this case it is not important 5993 // whether we get the first macro expansion or its containing macro. 5994 while (Count > 0) { 5995 Half = Count / 2; 5996 PPI = First; 5997 std::advance(PPI, Half); 5998 if (SourceMgr.isBeforeInTranslationUnit( 5999 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6000 First = PPI; 6001 ++First; 6002 Count = Count - Half - 1; 6003 } else 6004 Count = Half; 6005 } 6006 } 6007 6008 if (PPI == pp_end) 6009 return findNextPreprocessedEntity(SLocMapI); 6010 6011 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6012 } 6013 6014 /// Returns a pair of [Begin, End) indices of preallocated 6015 /// preprocessed entities that \arg Range encompasses. 6016 std::pair<unsigned, unsigned> 6017 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6018 if (Range.isInvalid()) 6019 return std::make_pair(0,0); 6020 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6021 6022 PreprocessedEntityID BeginID = 6023 findPreprocessedEntity(Range.getBegin(), false); 6024 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6025 return std::make_pair(BeginID, EndID); 6026 } 6027 6028 /// Optionally returns true or false if the preallocated preprocessed 6029 /// entity with index \arg Index came from file \arg FID. 6030 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6031 FileID FID) { 6032 if (FID.isInvalid()) 6033 return false; 6034 6035 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6036 ModuleFile &M = *PPInfo.first; 6037 unsigned LocalIndex = PPInfo.second; 6038 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6039 6040 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6041 if (Loc.isInvalid()) 6042 return false; 6043 6044 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6045 return true; 6046 else 6047 return false; 6048 } 6049 6050 namespace { 6051 6052 /// Visitor used to search for information about a header file. 6053 class HeaderFileInfoVisitor { 6054 const FileEntry *FE; 6055 Optional<HeaderFileInfo> HFI; 6056 6057 public: 6058 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6059 6060 bool operator()(ModuleFile &M) { 6061 HeaderFileInfoLookupTable *Table 6062 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6063 if (!Table) 6064 return false; 6065 6066 // Look in the on-disk hash table for an entry for this file name. 6067 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6068 if (Pos == Table->end()) 6069 return false; 6070 6071 HFI = *Pos; 6072 return true; 6073 } 6074 6075 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6076 }; 6077 6078 } // namespace 6079 6080 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6081 HeaderFileInfoVisitor Visitor(FE); 6082 ModuleMgr.visit(Visitor); 6083 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6084 return *HFI; 6085 6086 return HeaderFileInfo(); 6087 } 6088 6089 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6090 using DiagState = DiagnosticsEngine::DiagState; 6091 SmallVector<DiagState *, 32> DiagStates; 6092 6093 for (ModuleFile &F : ModuleMgr) { 6094 unsigned Idx = 0; 6095 auto &Record = F.PragmaDiagMappings; 6096 if (Record.empty()) 6097 continue; 6098 6099 DiagStates.clear(); 6100 6101 auto ReadDiagState = 6102 [&](const DiagState &BasedOn, SourceLocation Loc, 6103 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6104 unsigned BackrefID = Record[Idx++]; 6105 if (BackrefID != 0) 6106 return DiagStates[BackrefID - 1]; 6107 6108 // A new DiagState was created here. 6109 Diag.DiagStates.push_back(BasedOn); 6110 DiagState *NewState = &Diag.DiagStates.back(); 6111 DiagStates.push_back(NewState); 6112 unsigned Size = Record[Idx++]; 6113 assert(Idx + Size * 2 <= Record.size() && 6114 "Invalid data, not enough diag/map pairs"); 6115 while (Size--) { 6116 unsigned DiagID = Record[Idx++]; 6117 DiagnosticMapping NewMapping = 6118 DiagnosticMapping::deserialize(Record[Idx++]); 6119 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6120 continue; 6121 6122 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6123 6124 // If this mapping was specified as a warning but the severity was 6125 // upgraded due to diagnostic settings, simulate the current diagnostic 6126 // settings (and use a warning). 6127 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6128 NewMapping.setSeverity(diag::Severity::Warning); 6129 NewMapping.setUpgradedFromWarning(false); 6130 } 6131 6132 Mapping = NewMapping; 6133 } 6134 return NewState; 6135 }; 6136 6137 // Read the first state. 6138 DiagState *FirstState; 6139 if (F.Kind == MK_ImplicitModule) { 6140 // Implicitly-built modules are reused with different diagnostic 6141 // settings. Use the initial diagnostic state from Diag to simulate this 6142 // compilation's diagnostic settings. 6143 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6144 DiagStates.push_back(FirstState); 6145 6146 // Skip the initial diagnostic state from the serialized module. 6147 assert(Record[1] == 0 && 6148 "Invalid data, unexpected backref in initial state"); 6149 Idx = 3 + Record[2] * 2; 6150 assert(Idx < Record.size() && 6151 "Invalid data, not enough state change pairs in initial state"); 6152 } else if (F.isModule()) { 6153 // For an explicit module, preserve the flags from the module build 6154 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6155 // -Wblah flags. 6156 unsigned Flags = Record[Idx++]; 6157 DiagState Initial; 6158 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6159 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6160 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6161 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6162 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6163 Initial.ExtBehavior = (diag::Severity)Flags; 6164 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6165 6166 assert(F.OriginalSourceFileID.isValid()); 6167 6168 // Set up the root buffer of the module to start with the initial 6169 // diagnostic state of the module itself, to cover files that contain no 6170 // explicit transitions (for which we did not serialize anything). 6171 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6172 .StateTransitions.push_back({FirstState, 0}); 6173 } else { 6174 // For prefix ASTs, start with whatever the user configured on the 6175 // command line. 6176 Idx++; // Skip flags. 6177 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6178 SourceLocation(), false); 6179 } 6180 6181 // Read the state transitions. 6182 unsigned NumLocations = Record[Idx++]; 6183 while (NumLocations--) { 6184 assert(Idx < Record.size() && 6185 "Invalid data, missing pragma diagnostic states"); 6186 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6187 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6188 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6189 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6190 unsigned Transitions = Record[Idx++]; 6191 6192 // Note that we don't need to set up Parent/ParentOffset here, because 6193 // we won't be changing the diagnostic state within imported FileIDs 6194 // (other than perhaps appending to the main source file, which has no 6195 // parent). 6196 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6197 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6198 for (unsigned I = 0; I != Transitions; ++I) { 6199 unsigned Offset = Record[Idx++]; 6200 auto *State = 6201 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6202 F.StateTransitions.push_back({State, Offset}); 6203 } 6204 } 6205 6206 // Read the final state. 6207 assert(Idx < Record.size() && 6208 "Invalid data, missing final pragma diagnostic state"); 6209 SourceLocation CurStateLoc = 6210 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6211 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6212 6213 if (!F.isModule()) { 6214 Diag.DiagStatesByLoc.CurDiagState = CurState; 6215 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6216 6217 // Preserve the property that the imaginary root file describes the 6218 // current state. 6219 FileID NullFile; 6220 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6221 if (T.empty()) 6222 T.push_back({CurState, 0}); 6223 else 6224 T[0].State = CurState; 6225 } 6226 6227 // Don't try to read these mappings again. 6228 Record.clear(); 6229 } 6230 } 6231 6232 /// Get the correct cursor and offset for loading a type. 6233 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6234 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6235 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6236 ModuleFile *M = I->second; 6237 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 6238 } 6239 6240 /// Read and return the type with the given index.. 6241 /// 6242 /// The index is the type ID, shifted and minus the number of predefs. This 6243 /// routine actually reads the record corresponding to the type at the given 6244 /// location. It is a helper routine for GetType, which deals with reading type 6245 /// IDs. 6246 QualType ASTReader::readTypeRecord(unsigned Index) { 6247 assert(ContextObj && "reading type with no AST context"); 6248 ASTContext &Context = *ContextObj; 6249 RecordLocation Loc = TypeCursorForIndex(Index); 6250 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6251 6252 // Keep track of where we are in the stream, then jump back there 6253 // after reading this type. 6254 SavedStreamPosition SavedPosition(DeclsCursor); 6255 6256 ReadingKindTracker ReadingKind(Read_Type, *this); 6257 6258 // Note that we are loading a type record. 6259 Deserializing AType(this); 6260 6261 unsigned Idx = 0; 6262 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6263 Error(std::move(Err)); 6264 return QualType(); 6265 } 6266 RecordData Record; 6267 Expected<unsigned> MaybeCode = DeclsCursor.ReadCode(); 6268 if (!MaybeCode) { 6269 Error(MaybeCode.takeError()); 6270 return QualType(); 6271 } 6272 unsigned Code = MaybeCode.get(); 6273 6274 Expected<unsigned> MaybeTypeCode = DeclsCursor.readRecord(Code, Record); 6275 if (!MaybeTypeCode) { 6276 Error(MaybeTypeCode.takeError()); 6277 return QualType(); 6278 } 6279 switch ((TypeCode)MaybeTypeCode.get()) { 6280 case TYPE_EXT_QUAL: { 6281 if (Record.size() != 2) { 6282 Error("Incorrect encoding of extended qualifier type"); 6283 return QualType(); 6284 } 6285 QualType Base = readType(*Loc.F, Record, Idx); 6286 Qualifiers Quals = Qualifiers::fromOpaqueValue(Record[Idx++]); 6287 return Context.getQualifiedType(Base, Quals); 6288 } 6289 6290 case TYPE_COMPLEX: { 6291 if (Record.size() != 1) { 6292 Error("Incorrect encoding of complex type"); 6293 return QualType(); 6294 } 6295 QualType ElemType = readType(*Loc.F, Record, Idx); 6296 return Context.getComplexType(ElemType); 6297 } 6298 6299 case TYPE_POINTER: { 6300 if (Record.size() != 1) { 6301 Error("Incorrect encoding of pointer type"); 6302 return QualType(); 6303 } 6304 QualType PointeeType = readType(*Loc.F, Record, Idx); 6305 return Context.getPointerType(PointeeType); 6306 } 6307 6308 case TYPE_DECAYED: { 6309 if (Record.size() != 1) { 6310 Error("Incorrect encoding of decayed type"); 6311 return QualType(); 6312 } 6313 QualType OriginalType = readType(*Loc.F, Record, Idx); 6314 QualType DT = Context.getAdjustedParameterType(OriginalType); 6315 if (!isa<DecayedType>(DT)) 6316 Error("Decayed type does not decay"); 6317 return DT; 6318 } 6319 6320 case TYPE_ADJUSTED: { 6321 if (Record.size() != 2) { 6322 Error("Incorrect encoding of adjusted type"); 6323 return QualType(); 6324 } 6325 QualType OriginalTy = readType(*Loc.F, Record, Idx); 6326 QualType AdjustedTy = readType(*Loc.F, Record, Idx); 6327 return Context.getAdjustedType(OriginalTy, AdjustedTy); 6328 } 6329 6330 case TYPE_BLOCK_POINTER: { 6331 if (Record.size() != 1) { 6332 Error("Incorrect encoding of block pointer type"); 6333 return QualType(); 6334 } 6335 QualType PointeeType = readType(*Loc.F, Record, Idx); 6336 return Context.getBlockPointerType(PointeeType); 6337 } 6338 6339 case TYPE_LVALUE_REFERENCE: { 6340 if (Record.size() != 2) { 6341 Error("Incorrect encoding of lvalue reference type"); 6342 return QualType(); 6343 } 6344 QualType PointeeType = readType(*Loc.F, Record, Idx); 6345 return Context.getLValueReferenceType(PointeeType, Record[1]); 6346 } 6347 6348 case TYPE_RVALUE_REFERENCE: { 6349 if (Record.size() != 1) { 6350 Error("Incorrect encoding of rvalue reference type"); 6351 return QualType(); 6352 } 6353 QualType PointeeType = readType(*Loc.F, Record, Idx); 6354 return Context.getRValueReferenceType(PointeeType); 6355 } 6356 6357 case TYPE_MEMBER_POINTER: { 6358 if (Record.size() != 2) { 6359 Error("Incorrect encoding of member pointer type"); 6360 return QualType(); 6361 } 6362 QualType PointeeType = readType(*Loc.F, Record, Idx); 6363 QualType ClassType = readType(*Loc.F, Record, Idx); 6364 if (PointeeType.isNull() || ClassType.isNull()) 6365 return QualType(); 6366 6367 return Context.getMemberPointerType(PointeeType, ClassType.getTypePtr()); 6368 } 6369 6370 case TYPE_CONSTANT_ARRAY: { 6371 QualType ElementType = readType(*Loc.F, Record, Idx); 6372 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 6373 unsigned IndexTypeQuals = Record[2]; 6374 unsigned Idx = 3; 6375 llvm::APInt Size = ReadAPInt(Record, Idx); 6376 return Context.getConstantArrayType(ElementType, Size, 6377 ASM, IndexTypeQuals); 6378 } 6379 6380 case TYPE_INCOMPLETE_ARRAY: { 6381 QualType ElementType = readType(*Loc.F, Record, Idx); 6382 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 6383 unsigned IndexTypeQuals = Record[2]; 6384 return Context.getIncompleteArrayType(ElementType, ASM, IndexTypeQuals); 6385 } 6386 6387 case TYPE_VARIABLE_ARRAY: { 6388 QualType ElementType = readType(*Loc.F, Record, Idx); 6389 ArrayType::ArraySizeModifier ASM = (ArrayType::ArraySizeModifier)Record[1]; 6390 unsigned IndexTypeQuals = Record[2]; 6391 SourceLocation LBLoc = ReadSourceLocation(*Loc.F, Record[3]); 6392 SourceLocation RBLoc = ReadSourceLocation(*Loc.F, Record[4]); 6393 return Context.getVariableArrayType(ElementType, ReadExpr(*Loc.F), 6394 ASM, IndexTypeQuals, 6395 SourceRange(LBLoc, RBLoc)); 6396 } 6397 6398 case TYPE_VECTOR: { 6399 if (Record.size() != 3) { 6400 Error("incorrect encoding of vector type in AST file"); 6401 return QualType(); 6402 } 6403 6404 QualType ElementType = readType(*Loc.F, Record, Idx); 6405 unsigned NumElements = Record[1]; 6406 unsigned VecKind = Record[2]; 6407 return Context.getVectorType(ElementType, NumElements, 6408 (VectorType::VectorKind)VecKind); 6409 } 6410 6411 case TYPE_EXT_VECTOR: { 6412 if (Record.size() != 3) { 6413 Error("incorrect encoding of extended vector type in AST file"); 6414 return QualType(); 6415 } 6416 6417 QualType ElementType = readType(*Loc.F, Record, Idx); 6418 unsigned NumElements = Record[1]; 6419 return Context.getExtVectorType(ElementType, NumElements); 6420 } 6421 6422 case TYPE_FUNCTION_NO_PROTO: { 6423 if (Record.size() != 8) { 6424 Error("incorrect encoding of no-proto function type"); 6425 return QualType(); 6426 } 6427 QualType ResultType = readType(*Loc.F, Record, Idx); 6428 FunctionType::ExtInfo Info(Record[1], Record[2], Record[3], 6429 (CallingConv)Record[4], Record[5], Record[6], 6430 Record[7]); 6431 return Context.getFunctionNoProtoType(ResultType, Info); 6432 } 6433 6434 case TYPE_FUNCTION_PROTO: { 6435 QualType ResultType = readType(*Loc.F, Record, Idx); 6436 6437 FunctionProtoType::ExtProtoInfo EPI; 6438 EPI.ExtInfo = FunctionType::ExtInfo(/*noreturn*/ Record[1], 6439 /*hasregparm*/ Record[2], 6440 /*regparm*/ Record[3], 6441 static_cast<CallingConv>(Record[4]), 6442 /*produces*/ Record[5], 6443 /*nocallersavedregs*/ Record[6], 6444 /*nocfcheck*/ Record[7]); 6445 6446 unsigned Idx = 8; 6447 6448 EPI.Variadic = Record[Idx++]; 6449 EPI.HasTrailingReturn = Record[Idx++]; 6450 EPI.TypeQuals = Qualifiers::fromOpaqueValue(Record[Idx++]); 6451 EPI.RefQualifier = static_cast<RefQualifierKind>(Record[Idx++]); 6452 SmallVector<QualType, 8> ExceptionStorage; 6453 readExceptionSpec(*Loc.F, ExceptionStorage, EPI.ExceptionSpec, Record, Idx); 6454 6455 unsigned NumParams = Record[Idx++]; 6456 SmallVector<QualType, 16> ParamTypes; 6457 for (unsigned I = 0; I != NumParams; ++I) 6458 ParamTypes.push_back(readType(*Loc.F, Record, Idx)); 6459 6460 SmallVector<FunctionProtoType::ExtParameterInfo, 4> ExtParameterInfos; 6461 if (Idx != Record.size()) { 6462 for (unsigned I = 0; I != NumParams; ++I) 6463 ExtParameterInfos.push_back( 6464 FunctionProtoType::ExtParameterInfo 6465 ::getFromOpaqueValue(Record[Idx++])); 6466 EPI.ExtParameterInfos = ExtParameterInfos.data(); 6467 } 6468 6469 assert(Idx == Record.size()); 6470 6471 return Context.getFunctionType(ResultType, ParamTypes, EPI); 6472 } 6473 6474 case TYPE_UNRESOLVED_USING: { 6475 unsigned Idx = 0; 6476 return Context.getTypeDeclType( 6477 ReadDeclAs<UnresolvedUsingTypenameDecl>(*Loc.F, Record, Idx)); 6478 } 6479 6480 case TYPE_TYPEDEF: { 6481 if (Record.size() != 2) { 6482 Error("incorrect encoding of typedef type"); 6483 return QualType(); 6484 } 6485 unsigned Idx = 0; 6486 TypedefNameDecl *Decl = ReadDeclAs<TypedefNameDecl>(*Loc.F, Record, Idx); 6487 QualType Canonical = readType(*Loc.F, Record, Idx); 6488 if (!Canonical.isNull()) 6489 Canonical = Context.getCanonicalType(Canonical); 6490 return Context.getTypedefType(Decl, Canonical); 6491 } 6492 6493 case TYPE_TYPEOF_EXPR: 6494 return Context.getTypeOfExprType(ReadExpr(*Loc.F)); 6495 6496 case TYPE_TYPEOF: { 6497 if (Record.size() != 1) { 6498 Error("incorrect encoding of typeof(type) in AST file"); 6499 return QualType(); 6500 } 6501 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 6502 return Context.getTypeOfType(UnderlyingType); 6503 } 6504 6505 case TYPE_DECLTYPE: { 6506 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 6507 return Context.getDecltypeType(ReadExpr(*Loc.F), UnderlyingType); 6508 } 6509 6510 case TYPE_UNARY_TRANSFORM: { 6511 QualType BaseType = readType(*Loc.F, Record, Idx); 6512 QualType UnderlyingType = readType(*Loc.F, Record, Idx); 6513 UnaryTransformType::UTTKind UKind = (UnaryTransformType::UTTKind)Record[2]; 6514 return Context.getUnaryTransformType(BaseType, UnderlyingType, UKind); 6515 } 6516 6517 case TYPE_AUTO: { 6518 QualType Deduced = readType(*Loc.F, Record, Idx); 6519 AutoTypeKeyword Keyword = (AutoTypeKeyword)Record[Idx++]; 6520 bool IsDependent = false, IsPack = false; 6521 if (Deduced.isNull()) { 6522 IsDependent = Record[Idx] > 0; 6523 IsPack = Record[Idx] > 1; 6524 ++Idx; 6525 } 6526 return Context.getAutoType(Deduced, Keyword, IsDependent, IsPack); 6527 } 6528 6529 case TYPE_DEDUCED_TEMPLATE_SPECIALIZATION: { 6530 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 6531 QualType Deduced = readType(*Loc.F, Record, Idx); 6532 bool IsDependent = Deduced.isNull() ? Record[Idx++] : false; 6533 return Context.getDeducedTemplateSpecializationType(Name, Deduced, 6534 IsDependent); 6535 } 6536 6537 case TYPE_RECORD: { 6538 if (Record.size() != 2) { 6539 Error("incorrect encoding of record type"); 6540 return QualType(); 6541 } 6542 unsigned Idx = 0; 6543 bool IsDependent = Record[Idx++]; 6544 RecordDecl *RD = ReadDeclAs<RecordDecl>(*Loc.F, Record, Idx); 6545 RD = cast_or_null<RecordDecl>(RD->getCanonicalDecl()); 6546 QualType T = Context.getRecordType(RD); 6547 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 6548 return T; 6549 } 6550 6551 case TYPE_ENUM: { 6552 if (Record.size() != 2) { 6553 Error("incorrect encoding of enum type"); 6554 return QualType(); 6555 } 6556 unsigned Idx = 0; 6557 bool IsDependent = Record[Idx++]; 6558 QualType T 6559 = Context.getEnumType(ReadDeclAs<EnumDecl>(*Loc.F, Record, Idx)); 6560 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 6561 return T; 6562 } 6563 6564 case TYPE_ATTRIBUTED: { 6565 if (Record.size() != 3) { 6566 Error("incorrect encoding of attributed type"); 6567 return QualType(); 6568 } 6569 QualType modifiedType = readType(*Loc.F, Record, Idx); 6570 QualType equivalentType = readType(*Loc.F, Record, Idx); 6571 AttributedType::Kind kind = static_cast<AttributedType::Kind>(Record[2]); 6572 return Context.getAttributedType(kind, modifiedType, equivalentType); 6573 } 6574 6575 case TYPE_PAREN: { 6576 if (Record.size() != 1) { 6577 Error("incorrect encoding of paren type"); 6578 return QualType(); 6579 } 6580 QualType InnerType = readType(*Loc.F, Record, Idx); 6581 return Context.getParenType(InnerType); 6582 } 6583 6584 case TYPE_MACRO_QUALIFIED: { 6585 if (Record.size() != 2) { 6586 Error("incorrect encoding of macro defined type"); 6587 return QualType(); 6588 } 6589 QualType UnderlyingTy = readType(*Loc.F, Record, Idx); 6590 IdentifierInfo *MacroII = GetIdentifierInfo(*Loc.F, Record, Idx); 6591 return Context.getMacroQualifiedType(UnderlyingTy, MacroII); 6592 } 6593 6594 case TYPE_PACK_EXPANSION: { 6595 if (Record.size() != 2) { 6596 Error("incorrect encoding of pack expansion type"); 6597 return QualType(); 6598 } 6599 QualType Pattern = readType(*Loc.F, Record, Idx); 6600 if (Pattern.isNull()) 6601 return QualType(); 6602 Optional<unsigned> NumExpansions; 6603 if (Record[1]) 6604 NumExpansions = Record[1] - 1; 6605 return Context.getPackExpansionType(Pattern, NumExpansions); 6606 } 6607 6608 case TYPE_ELABORATED: { 6609 unsigned Idx = 0; 6610 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 6611 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 6612 QualType NamedType = readType(*Loc.F, Record, Idx); 6613 TagDecl *OwnedTagDecl = ReadDeclAs<TagDecl>(*Loc.F, Record, Idx); 6614 return Context.getElaboratedType(Keyword, NNS, NamedType, OwnedTagDecl); 6615 } 6616 6617 case TYPE_OBJC_INTERFACE: { 6618 unsigned Idx = 0; 6619 ObjCInterfaceDecl *ItfD 6620 = ReadDeclAs<ObjCInterfaceDecl>(*Loc.F, Record, Idx); 6621 return Context.getObjCInterfaceType(ItfD->getCanonicalDecl()); 6622 } 6623 6624 case TYPE_OBJC_TYPE_PARAM: { 6625 unsigned Idx = 0; 6626 ObjCTypeParamDecl *Decl 6627 = ReadDeclAs<ObjCTypeParamDecl>(*Loc.F, Record, Idx); 6628 unsigned NumProtos = Record[Idx++]; 6629 SmallVector<ObjCProtocolDecl*, 4> Protos; 6630 for (unsigned I = 0; I != NumProtos; ++I) 6631 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 6632 return Context.getObjCTypeParamType(Decl, Protos); 6633 } 6634 6635 case TYPE_OBJC_OBJECT: { 6636 unsigned Idx = 0; 6637 QualType Base = readType(*Loc.F, Record, Idx); 6638 unsigned NumTypeArgs = Record[Idx++]; 6639 SmallVector<QualType, 4> TypeArgs; 6640 for (unsigned I = 0; I != NumTypeArgs; ++I) 6641 TypeArgs.push_back(readType(*Loc.F, Record, Idx)); 6642 unsigned NumProtos = Record[Idx++]; 6643 SmallVector<ObjCProtocolDecl*, 4> Protos; 6644 for (unsigned I = 0; I != NumProtos; ++I) 6645 Protos.push_back(ReadDeclAs<ObjCProtocolDecl>(*Loc.F, Record, Idx)); 6646 bool IsKindOf = Record[Idx++]; 6647 return Context.getObjCObjectType(Base, TypeArgs, Protos, IsKindOf); 6648 } 6649 6650 case TYPE_OBJC_OBJECT_POINTER: { 6651 unsigned Idx = 0; 6652 QualType Pointee = readType(*Loc.F, Record, Idx); 6653 return Context.getObjCObjectPointerType(Pointee); 6654 } 6655 6656 case TYPE_SUBST_TEMPLATE_TYPE_PARM: { 6657 unsigned Idx = 0; 6658 QualType Parm = readType(*Loc.F, Record, Idx); 6659 QualType Replacement = readType(*Loc.F, Record, Idx); 6660 return Context.getSubstTemplateTypeParmType( 6661 cast<TemplateTypeParmType>(Parm), 6662 Context.getCanonicalType(Replacement)); 6663 } 6664 6665 case TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK: { 6666 unsigned Idx = 0; 6667 QualType Parm = readType(*Loc.F, Record, Idx); 6668 TemplateArgument ArgPack = ReadTemplateArgument(*Loc.F, Record, Idx); 6669 return Context.getSubstTemplateTypeParmPackType( 6670 cast<TemplateTypeParmType>(Parm), 6671 ArgPack); 6672 } 6673 6674 case TYPE_INJECTED_CLASS_NAME: { 6675 CXXRecordDecl *D = ReadDeclAs<CXXRecordDecl>(*Loc.F, Record, Idx); 6676 QualType TST = readType(*Loc.F, Record, Idx); // probably derivable 6677 // FIXME: ASTContext::getInjectedClassNameType is not currently suitable 6678 // for AST reading, too much interdependencies. 6679 const Type *T = nullptr; 6680 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) { 6681 if (const Type *Existing = DI->getTypeForDecl()) { 6682 T = Existing; 6683 break; 6684 } 6685 } 6686 if (!T) { 6687 T = new (Context, TypeAlignment) InjectedClassNameType(D, TST); 6688 for (auto *DI = D; DI; DI = DI->getPreviousDecl()) 6689 DI->setTypeForDecl(T); 6690 } 6691 return QualType(T, 0); 6692 } 6693 6694 case TYPE_TEMPLATE_TYPE_PARM: { 6695 unsigned Idx = 0; 6696 unsigned Depth = Record[Idx++]; 6697 unsigned Index = Record[Idx++]; 6698 bool Pack = Record[Idx++]; 6699 TemplateTypeParmDecl *D 6700 = ReadDeclAs<TemplateTypeParmDecl>(*Loc.F, Record, Idx); 6701 return Context.getTemplateTypeParmType(Depth, Index, Pack, D); 6702 } 6703 6704 case TYPE_DEPENDENT_NAME: { 6705 unsigned Idx = 0; 6706 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 6707 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 6708 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx); 6709 QualType Canon = readType(*Loc.F, Record, Idx); 6710 if (!Canon.isNull()) 6711 Canon = Context.getCanonicalType(Canon); 6712 return Context.getDependentNameType(Keyword, NNS, Name, Canon); 6713 } 6714 6715 case TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { 6716 unsigned Idx = 0; 6717 ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; 6718 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); 6719 const IdentifierInfo *Name = GetIdentifierInfo(*Loc.F, Record, Idx); 6720 unsigned NumArgs = Record[Idx++]; 6721 SmallVector<TemplateArgument, 8> Args; 6722 Args.reserve(NumArgs); 6723 while (NumArgs--) 6724 Args.push_back(ReadTemplateArgument(*Loc.F, Record, Idx)); 6725 return Context.getDependentTemplateSpecializationType(Keyword, NNS, Name, 6726 Args); 6727 } 6728 6729 case TYPE_DEPENDENT_SIZED_ARRAY: { 6730 unsigned Idx = 0; 6731 6732 // ArrayType 6733 QualType ElementType = readType(*Loc.F, Record, Idx); 6734 ArrayType::ArraySizeModifier ASM 6735 = (ArrayType::ArraySizeModifier)Record[Idx++]; 6736 unsigned IndexTypeQuals = Record[Idx++]; 6737 6738 // DependentSizedArrayType 6739 Expr *NumElts = ReadExpr(*Loc.F); 6740 SourceRange Brackets = ReadSourceRange(*Loc.F, Record, Idx); 6741 6742 return Context.getDependentSizedArrayType(ElementType, NumElts, ASM, 6743 IndexTypeQuals, Brackets); 6744 } 6745 6746 case TYPE_TEMPLATE_SPECIALIZATION: { 6747 unsigned Idx = 0; 6748 bool IsDependent = Record[Idx++]; 6749 TemplateName Name = ReadTemplateName(*Loc.F, Record, Idx); 6750 SmallVector<TemplateArgument, 8> Args; 6751 ReadTemplateArgumentList(Args, *Loc.F, Record, Idx); 6752 QualType Underlying = readType(*Loc.F, Record, Idx); 6753 QualType T; 6754 if (Underlying.isNull()) 6755 T = Context.getCanonicalTemplateSpecializationType(Name, Args); 6756 else 6757 T = Context.getTemplateSpecializationType(Name, Args, Underlying); 6758 const_cast<Type*>(T.getTypePtr())->setDependent(IsDependent); 6759 return T; 6760 } 6761 6762 case TYPE_ATOMIC: { 6763 if (Record.size() != 1) { 6764 Error("Incorrect encoding of atomic type"); 6765 return QualType(); 6766 } 6767 QualType ValueType = readType(*Loc.F, Record, Idx); 6768 return Context.getAtomicType(ValueType); 6769 } 6770 6771 case TYPE_PIPE: { 6772 if (Record.size() != 2) { 6773 Error("Incorrect encoding of pipe type"); 6774 return QualType(); 6775 } 6776 6777 // Reading the pipe element type. 6778 QualType ElementType = readType(*Loc.F, Record, Idx); 6779 unsigned ReadOnly = Record[1]; 6780 return Context.getPipeType(ElementType, ReadOnly); 6781 } 6782 6783 case TYPE_DEPENDENT_SIZED_VECTOR: { 6784 unsigned Idx = 0; 6785 QualType ElementType = readType(*Loc.F, Record, Idx); 6786 Expr *SizeExpr = ReadExpr(*Loc.F); 6787 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx); 6788 unsigned VecKind = Record[Idx]; 6789 6790 return Context.getDependentVectorType(ElementType, SizeExpr, AttrLoc, 6791 (VectorType::VectorKind)VecKind); 6792 } 6793 6794 case TYPE_DEPENDENT_SIZED_EXT_VECTOR: { 6795 unsigned Idx = 0; 6796 6797 // DependentSizedExtVectorType 6798 QualType ElementType = readType(*Loc.F, Record, Idx); 6799 Expr *SizeExpr = ReadExpr(*Loc.F); 6800 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx); 6801 6802 return Context.getDependentSizedExtVectorType(ElementType, SizeExpr, 6803 AttrLoc); 6804 } 6805 6806 case TYPE_DEPENDENT_ADDRESS_SPACE: { 6807 unsigned Idx = 0; 6808 6809 // DependentAddressSpaceType 6810 QualType PointeeType = readType(*Loc.F, Record, Idx); 6811 Expr *AddrSpaceExpr = ReadExpr(*Loc.F); 6812 SourceLocation AttrLoc = ReadSourceLocation(*Loc.F, Record, Idx); 6813 6814 return Context.getDependentAddressSpaceType(PointeeType, AddrSpaceExpr, 6815 AttrLoc); 6816 } 6817 } 6818 llvm_unreachable("Invalid TypeCode!"); 6819 } 6820 6821 void ASTReader::readExceptionSpec(ModuleFile &ModuleFile, 6822 SmallVectorImpl<QualType> &Exceptions, 6823 FunctionProtoType::ExceptionSpecInfo &ESI, 6824 const RecordData &Record, unsigned &Idx) { 6825 ExceptionSpecificationType EST = 6826 static_cast<ExceptionSpecificationType>(Record[Idx++]); 6827 ESI.Type = EST; 6828 if (EST == EST_Dynamic) { 6829 for (unsigned I = 0, N = Record[Idx++]; I != N; ++I) 6830 Exceptions.push_back(readType(ModuleFile, Record, Idx)); 6831 ESI.Exceptions = Exceptions; 6832 } else if (isComputedNoexcept(EST)) { 6833 ESI.NoexceptExpr = ReadExpr(ModuleFile); 6834 } else if (EST == EST_Uninstantiated) { 6835 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 6836 ESI.SourceTemplate = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 6837 } else if (EST == EST_Unevaluated) { 6838 ESI.SourceDecl = ReadDeclAs<FunctionDecl>(ModuleFile, Record, Idx); 6839 } 6840 } 6841 6842 namespace clang { 6843 6844 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6845 ModuleFile *F; 6846 ASTReader *Reader; 6847 const ASTReader::RecordData &Record; 6848 unsigned &Idx; 6849 6850 SourceLocation ReadSourceLocation() { 6851 return Reader->ReadSourceLocation(*F, Record, Idx); 6852 } 6853 6854 TypeSourceInfo *GetTypeSourceInfo() { 6855 return Reader->GetTypeSourceInfo(*F, Record, Idx); 6856 } 6857 6858 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6859 return Reader->ReadNestedNameSpecifierLoc(*F, Record, Idx); 6860 } 6861 6862 Attr *ReadAttr() { 6863 return Reader->ReadAttr(*F, Record, Idx); 6864 } 6865 6866 public: 6867 TypeLocReader(ModuleFile &F, ASTReader &Reader, 6868 const ASTReader::RecordData &Record, unsigned &Idx) 6869 : F(&F), Reader(&Reader), Record(Record), Idx(Idx) {} 6870 6871 // We want compile-time assurance that we've enumerated all of 6872 // these, so unfortunately we have to declare them first, then 6873 // define them out-of-line. 6874 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6875 #define TYPELOC(CLASS, PARENT) \ 6876 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6877 #include "clang/AST/TypeLocNodes.def" 6878 6879 void VisitFunctionTypeLoc(FunctionTypeLoc); 6880 void VisitArrayTypeLoc(ArrayTypeLoc); 6881 }; 6882 6883 } // namespace clang 6884 6885 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6886 // nothing to do 6887 } 6888 6889 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6890 TL.setBuiltinLoc(ReadSourceLocation()); 6891 if (TL.needsExtraLocalData()) { 6892 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Record[Idx++])); 6893 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Record[Idx++])); 6894 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Record[Idx++])); 6895 TL.setModeAttr(Record[Idx++]); 6896 } 6897 } 6898 6899 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6900 TL.setNameLoc(ReadSourceLocation()); 6901 } 6902 6903 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6904 TL.setStarLoc(ReadSourceLocation()); 6905 } 6906 6907 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6908 // nothing to do 6909 } 6910 6911 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6912 // nothing to do 6913 } 6914 6915 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6916 TL.setExpansionLoc(ReadSourceLocation()); 6917 } 6918 6919 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6920 TL.setCaretLoc(ReadSourceLocation()); 6921 } 6922 6923 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6924 TL.setAmpLoc(ReadSourceLocation()); 6925 } 6926 6927 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6928 TL.setAmpAmpLoc(ReadSourceLocation()); 6929 } 6930 6931 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6932 TL.setStarLoc(ReadSourceLocation()); 6933 TL.setClassTInfo(GetTypeSourceInfo()); 6934 } 6935 6936 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6937 TL.setLBracketLoc(ReadSourceLocation()); 6938 TL.setRBracketLoc(ReadSourceLocation()); 6939 if (Record[Idx++]) 6940 TL.setSizeExpr(Reader->ReadExpr(*F)); 6941 else 6942 TL.setSizeExpr(nullptr); 6943 } 6944 6945 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6946 VisitArrayTypeLoc(TL); 6947 } 6948 6949 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6950 VisitArrayTypeLoc(TL); 6951 } 6952 6953 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6954 VisitArrayTypeLoc(TL); 6955 } 6956 6957 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6958 DependentSizedArrayTypeLoc TL) { 6959 VisitArrayTypeLoc(TL); 6960 } 6961 6962 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6963 DependentAddressSpaceTypeLoc TL) { 6964 6965 TL.setAttrNameLoc(ReadSourceLocation()); 6966 SourceRange range; 6967 range.setBegin(ReadSourceLocation()); 6968 range.setEnd(ReadSourceLocation()); 6969 TL.setAttrOperandParensRange(range); 6970 TL.setAttrExprOperand(Reader->ReadExpr(*F)); 6971 } 6972 6973 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6974 DependentSizedExtVectorTypeLoc TL) { 6975 TL.setNameLoc(ReadSourceLocation()); 6976 } 6977 6978 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6979 TL.setNameLoc(ReadSourceLocation()); 6980 } 6981 6982 void TypeLocReader::VisitDependentVectorTypeLoc( 6983 DependentVectorTypeLoc TL) { 6984 TL.setNameLoc(ReadSourceLocation()); 6985 } 6986 6987 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6988 TL.setNameLoc(ReadSourceLocation()); 6989 } 6990 6991 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6992 TL.setLocalRangeBegin(ReadSourceLocation()); 6993 TL.setLParenLoc(ReadSourceLocation()); 6994 TL.setRParenLoc(ReadSourceLocation()); 6995 TL.setExceptionSpecRange(SourceRange(Reader->ReadSourceLocation(*F, Record, Idx), 6996 Reader->ReadSourceLocation(*F, Record, Idx))); 6997 TL.setLocalRangeEnd(ReadSourceLocation()); 6998 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6999 TL.setParam(i, Reader->ReadDeclAs<ParmVarDecl>(*F, Record, Idx)); 7000 } 7001 } 7002 7003 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 7004 VisitFunctionTypeLoc(TL); 7005 } 7006 7007 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 7008 VisitFunctionTypeLoc(TL); 7009 } 7010 7011 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 7012 TL.setNameLoc(ReadSourceLocation()); 7013 } 7014 7015 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 7016 TL.setNameLoc(ReadSourceLocation()); 7017 } 7018 7019 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 7020 TL.setTypeofLoc(ReadSourceLocation()); 7021 TL.setLParenLoc(ReadSourceLocation()); 7022 TL.setRParenLoc(ReadSourceLocation()); 7023 } 7024 7025 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 7026 TL.setTypeofLoc(ReadSourceLocation()); 7027 TL.setLParenLoc(ReadSourceLocation()); 7028 TL.setRParenLoc(ReadSourceLocation()); 7029 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 7030 } 7031 7032 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 7033 TL.setNameLoc(ReadSourceLocation()); 7034 } 7035 7036 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 7037 TL.setKWLoc(ReadSourceLocation()); 7038 TL.setLParenLoc(ReadSourceLocation()); 7039 TL.setRParenLoc(ReadSourceLocation()); 7040 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 7041 } 7042 7043 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 7044 TL.setNameLoc(ReadSourceLocation()); 7045 } 7046 7047 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 7048 DeducedTemplateSpecializationTypeLoc TL) { 7049 TL.setTemplateNameLoc(ReadSourceLocation()); 7050 } 7051 7052 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 7053 TL.setNameLoc(ReadSourceLocation()); 7054 } 7055 7056 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 7057 TL.setNameLoc(ReadSourceLocation()); 7058 } 7059 7060 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 7061 TL.setAttr(ReadAttr()); 7062 } 7063 7064 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 7065 TL.setNameLoc(ReadSourceLocation()); 7066 } 7067 7068 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 7069 SubstTemplateTypeParmTypeLoc TL) { 7070 TL.setNameLoc(ReadSourceLocation()); 7071 } 7072 7073 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 7074 SubstTemplateTypeParmPackTypeLoc TL) { 7075 TL.setNameLoc(ReadSourceLocation()); 7076 } 7077 7078 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 7079 TemplateSpecializationTypeLoc TL) { 7080 TL.setTemplateKeywordLoc(ReadSourceLocation()); 7081 TL.setTemplateNameLoc(ReadSourceLocation()); 7082 TL.setLAngleLoc(ReadSourceLocation()); 7083 TL.setRAngleLoc(ReadSourceLocation()); 7084 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 7085 TL.setArgLocInfo( 7086 i, 7087 Reader->GetTemplateArgumentLocInfo( 7088 *F, TL.getTypePtr()->getArg(i).getKind(), Record, Idx)); 7089 } 7090 7091 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 7092 TL.setLParenLoc(ReadSourceLocation()); 7093 TL.setRParenLoc(ReadSourceLocation()); 7094 } 7095 7096 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 7097 TL.setElaboratedKeywordLoc(ReadSourceLocation()); 7098 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 7099 } 7100 7101 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 7102 TL.setNameLoc(ReadSourceLocation()); 7103 } 7104 7105 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 7106 TL.setElaboratedKeywordLoc(ReadSourceLocation()); 7107 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 7108 TL.setNameLoc(ReadSourceLocation()); 7109 } 7110 7111 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 7112 DependentTemplateSpecializationTypeLoc TL) { 7113 TL.setElaboratedKeywordLoc(ReadSourceLocation()); 7114 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 7115 TL.setTemplateKeywordLoc(ReadSourceLocation()); 7116 TL.setTemplateNameLoc(ReadSourceLocation()); 7117 TL.setLAngleLoc(ReadSourceLocation()); 7118 TL.setRAngleLoc(ReadSourceLocation()); 7119 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 7120 TL.setArgLocInfo( 7121 I, 7122 Reader->GetTemplateArgumentLocInfo( 7123 *F, TL.getTypePtr()->getArg(I).getKind(), Record, Idx)); 7124 } 7125 7126 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 7127 TL.setEllipsisLoc(ReadSourceLocation()); 7128 } 7129 7130 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 7131 TL.setNameLoc(ReadSourceLocation()); 7132 } 7133 7134 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 7135 if (TL.getNumProtocols()) { 7136 TL.setProtocolLAngleLoc(ReadSourceLocation()); 7137 TL.setProtocolRAngleLoc(ReadSourceLocation()); 7138 } 7139 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 7140 TL.setProtocolLoc(i, ReadSourceLocation()); 7141 } 7142 7143 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 7144 TL.setHasBaseTypeAsWritten(Record[Idx++]); 7145 TL.setTypeArgsLAngleLoc(ReadSourceLocation()); 7146 TL.setTypeArgsRAngleLoc(ReadSourceLocation()); 7147 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 7148 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 7149 TL.setProtocolLAngleLoc(ReadSourceLocation()); 7150 TL.setProtocolRAngleLoc(ReadSourceLocation()); 7151 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 7152 TL.setProtocolLoc(i, ReadSourceLocation()); 7153 } 7154 7155 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 7156 TL.setStarLoc(ReadSourceLocation()); 7157 } 7158 7159 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 7160 TL.setKWLoc(ReadSourceLocation()); 7161 TL.setLParenLoc(ReadSourceLocation()); 7162 TL.setRParenLoc(ReadSourceLocation()); 7163 } 7164 7165 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 7166 TL.setKWLoc(ReadSourceLocation()); 7167 } 7168 7169 void ASTReader::ReadTypeLoc(ModuleFile &F, const ASTReader::RecordData &Record, 7170 unsigned &Idx, TypeLoc TL) { 7171 TypeLocReader TLR(F, *this, Record, Idx); 7172 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 7173 TLR.Visit(TL); 7174 } 7175 7176 TypeSourceInfo * 7177 ASTReader::GetTypeSourceInfo(ModuleFile &F, const ASTReader::RecordData &Record, 7178 unsigned &Idx) { 7179 QualType InfoTy = readType(F, Record, Idx); 7180 if (InfoTy.isNull()) 7181 return nullptr; 7182 7183 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 7184 ReadTypeLoc(F, Record, Idx, TInfo->getTypeLoc()); 7185 return TInfo; 7186 } 7187 7188 QualType ASTReader::GetType(TypeID ID) { 7189 assert(ContextObj && "reading type with no AST context"); 7190 ASTContext &Context = *ContextObj; 7191 7192 unsigned FastQuals = ID & Qualifiers::FastMask; 7193 unsigned Index = ID >> Qualifiers::FastWidth; 7194 7195 if (Index < NUM_PREDEF_TYPE_IDS) { 7196 QualType T; 7197 switch ((PredefinedTypeIDs)Index) { 7198 case PREDEF_TYPE_NULL_ID: 7199 return QualType(); 7200 case PREDEF_TYPE_VOID_ID: 7201 T = Context.VoidTy; 7202 break; 7203 case PREDEF_TYPE_BOOL_ID: 7204 T = Context.BoolTy; 7205 break; 7206 case PREDEF_TYPE_CHAR_U_ID: 7207 case PREDEF_TYPE_CHAR_S_ID: 7208 // FIXME: Check that the signedness of CharTy is correct! 7209 T = Context.CharTy; 7210 break; 7211 case PREDEF_TYPE_UCHAR_ID: 7212 T = Context.UnsignedCharTy; 7213 break; 7214 case PREDEF_TYPE_USHORT_ID: 7215 T = Context.UnsignedShortTy; 7216 break; 7217 case PREDEF_TYPE_UINT_ID: 7218 T = Context.UnsignedIntTy; 7219 break; 7220 case PREDEF_TYPE_ULONG_ID: 7221 T = Context.UnsignedLongTy; 7222 break; 7223 case PREDEF_TYPE_ULONGLONG_ID: 7224 T = Context.UnsignedLongLongTy; 7225 break; 7226 case PREDEF_TYPE_UINT128_ID: 7227 T = Context.UnsignedInt128Ty; 7228 break; 7229 case PREDEF_TYPE_SCHAR_ID: 7230 T = Context.SignedCharTy; 7231 break; 7232 case PREDEF_TYPE_WCHAR_ID: 7233 T = Context.WCharTy; 7234 break; 7235 case PREDEF_TYPE_SHORT_ID: 7236 T = Context.ShortTy; 7237 break; 7238 case PREDEF_TYPE_INT_ID: 7239 T = Context.IntTy; 7240 break; 7241 case PREDEF_TYPE_LONG_ID: 7242 T = Context.LongTy; 7243 break; 7244 case PREDEF_TYPE_LONGLONG_ID: 7245 T = Context.LongLongTy; 7246 break; 7247 case PREDEF_TYPE_INT128_ID: 7248 T = Context.Int128Ty; 7249 break; 7250 case PREDEF_TYPE_HALF_ID: 7251 T = Context.HalfTy; 7252 break; 7253 case PREDEF_TYPE_FLOAT_ID: 7254 T = Context.FloatTy; 7255 break; 7256 case PREDEF_TYPE_DOUBLE_ID: 7257 T = Context.DoubleTy; 7258 break; 7259 case PREDEF_TYPE_LONGDOUBLE_ID: 7260 T = Context.LongDoubleTy; 7261 break; 7262 case PREDEF_TYPE_SHORT_ACCUM_ID: 7263 T = Context.ShortAccumTy; 7264 break; 7265 case PREDEF_TYPE_ACCUM_ID: 7266 T = Context.AccumTy; 7267 break; 7268 case PREDEF_TYPE_LONG_ACCUM_ID: 7269 T = Context.LongAccumTy; 7270 break; 7271 case PREDEF_TYPE_USHORT_ACCUM_ID: 7272 T = Context.UnsignedShortAccumTy; 7273 break; 7274 case PREDEF_TYPE_UACCUM_ID: 7275 T = Context.UnsignedAccumTy; 7276 break; 7277 case PREDEF_TYPE_ULONG_ACCUM_ID: 7278 T = Context.UnsignedLongAccumTy; 7279 break; 7280 case PREDEF_TYPE_SHORT_FRACT_ID: 7281 T = Context.ShortFractTy; 7282 break; 7283 case PREDEF_TYPE_FRACT_ID: 7284 T = Context.FractTy; 7285 break; 7286 case PREDEF_TYPE_LONG_FRACT_ID: 7287 T = Context.LongFractTy; 7288 break; 7289 case PREDEF_TYPE_USHORT_FRACT_ID: 7290 T = Context.UnsignedShortFractTy; 7291 break; 7292 case PREDEF_TYPE_UFRACT_ID: 7293 T = Context.UnsignedFractTy; 7294 break; 7295 case PREDEF_TYPE_ULONG_FRACT_ID: 7296 T = Context.UnsignedLongFractTy; 7297 break; 7298 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 7299 T = Context.SatShortAccumTy; 7300 break; 7301 case PREDEF_TYPE_SAT_ACCUM_ID: 7302 T = Context.SatAccumTy; 7303 break; 7304 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 7305 T = Context.SatLongAccumTy; 7306 break; 7307 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 7308 T = Context.SatUnsignedShortAccumTy; 7309 break; 7310 case PREDEF_TYPE_SAT_UACCUM_ID: 7311 T = Context.SatUnsignedAccumTy; 7312 break; 7313 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 7314 T = Context.SatUnsignedLongAccumTy; 7315 break; 7316 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 7317 T = Context.SatShortFractTy; 7318 break; 7319 case PREDEF_TYPE_SAT_FRACT_ID: 7320 T = Context.SatFractTy; 7321 break; 7322 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 7323 T = Context.SatLongFractTy; 7324 break; 7325 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 7326 T = Context.SatUnsignedShortFractTy; 7327 break; 7328 case PREDEF_TYPE_SAT_UFRACT_ID: 7329 T = Context.SatUnsignedFractTy; 7330 break; 7331 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 7332 T = Context.SatUnsignedLongFractTy; 7333 break; 7334 case PREDEF_TYPE_FLOAT16_ID: 7335 T = Context.Float16Ty; 7336 break; 7337 case PREDEF_TYPE_FLOAT128_ID: 7338 T = Context.Float128Ty; 7339 break; 7340 case PREDEF_TYPE_OVERLOAD_ID: 7341 T = Context.OverloadTy; 7342 break; 7343 case PREDEF_TYPE_BOUND_MEMBER: 7344 T = Context.BoundMemberTy; 7345 break; 7346 case PREDEF_TYPE_PSEUDO_OBJECT: 7347 T = Context.PseudoObjectTy; 7348 break; 7349 case PREDEF_TYPE_DEPENDENT_ID: 7350 T = Context.DependentTy; 7351 break; 7352 case PREDEF_TYPE_UNKNOWN_ANY: 7353 T = Context.UnknownAnyTy; 7354 break; 7355 case PREDEF_TYPE_NULLPTR_ID: 7356 T = Context.NullPtrTy; 7357 break; 7358 case PREDEF_TYPE_CHAR8_ID: 7359 T = Context.Char8Ty; 7360 break; 7361 case PREDEF_TYPE_CHAR16_ID: 7362 T = Context.Char16Ty; 7363 break; 7364 case PREDEF_TYPE_CHAR32_ID: 7365 T = Context.Char32Ty; 7366 break; 7367 case PREDEF_TYPE_OBJC_ID: 7368 T = Context.ObjCBuiltinIdTy; 7369 break; 7370 case PREDEF_TYPE_OBJC_CLASS: 7371 T = Context.ObjCBuiltinClassTy; 7372 break; 7373 case PREDEF_TYPE_OBJC_SEL: 7374 T = Context.ObjCBuiltinSelTy; 7375 break; 7376 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 7377 case PREDEF_TYPE_##Id##_ID: \ 7378 T = Context.SingletonId; \ 7379 break; 7380 #include "clang/Basic/OpenCLImageTypes.def" 7381 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 7382 case PREDEF_TYPE_##Id##_ID: \ 7383 T = Context.Id##Ty; \ 7384 break; 7385 #include "clang/Basic/OpenCLExtensionTypes.def" 7386 case PREDEF_TYPE_SAMPLER_ID: 7387 T = Context.OCLSamplerTy; 7388 break; 7389 case PREDEF_TYPE_EVENT_ID: 7390 T = Context.OCLEventTy; 7391 break; 7392 case PREDEF_TYPE_CLK_EVENT_ID: 7393 T = Context.OCLClkEventTy; 7394 break; 7395 case PREDEF_TYPE_QUEUE_ID: 7396 T = Context.OCLQueueTy; 7397 break; 7398 case PREDEF_TYPE_RESERVE_ID_ID: 7399 T = Context.OCLReserveIDTy; 7400 break; 7401 case PREDEF_TYPE_AUTO_DEDUCT: 7402 T = Context.getAutoDeductType(); 7403 break; 7404 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7405 T = Context.getAutoRRefDeductType(); 7406 break; 7407 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7408 T = Context.ARCUnbridgedCastTy; 7409 break; 7410 case PREDEF_TYPE_BUILTIN_FN: 7411 T = Context.BuiltinFnTy; 7412 break; 7413 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7414 T = Context.OMPArraySectionTy; 7415 break; 7416 } 7417 7418 assert(!T.isNull() && "Unknown predefined type"); 7419 return T.withFastQualifiers(FastQuals); 7420 } 7421 7422 Index -= NUM_PREDEF_TYPE_IDS; 7423 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7424 if (TypesLoaded[Index].isNull()) { 7425 TypesLoaded[Index] = readTypeRecord(Index); 7426 if (TypesLoaded[Index].isNull()) 7427 return QualType(); 7428 7429 TypesLoaded[Index]->setFromAST(); 7430 if (DeserializationListener) 7431 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7432 TypesLoaded[Index]); 7433 } 7434 7435 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7436 } 7437 7438 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7439 return GetType(getGlobalTypeID(F, LocalID)); 7440 } 7441 7442 serialization::TypeID 7443 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7444 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7445 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7446 7447 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7448 return LocalID; 7449 7450 if (!F.ModuleOffsetMap.empty()) 7451 ReadModuleOffsetMap(F); 7452 7453 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7454 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7455 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7456 7457 unsigned GlobalIndex = LocalIndex + I->second; 7458 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7459 } 7460 7461 TemplateArgumentLocInfo 7462 ASTReader::GetTemplateArgumentLocInfo(ModuleFile &F, 7463 TemplateArgument::ArgKind Kind, 7464 const RecordData &Record, 7465 unsigned &Index) { 7466 switch (Kind) { 7467 case TemplateArgument::Expression: 7468 return ReadExpr(F); 7469 case TemplateArgument::Type: 7470 return GetTypeSourceInfo(F, Record, Index); 7471 case TemplateArgument::Template: { 7472 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 7473 Index); 7474 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 7475 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7476 SourceLocation()); 7477 } 7478 case TemplateArgument::TemplateExpansion: { 7479 NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, 7480 Index); 7481 SourceLocation TemplateNameLoc = ReadSourceLocation(F, Record, Index); 7482 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Index); 7483 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7484 EllipsisLoc); 7485 } 7486 case TemplateArgument::Null: 7487 case TemplateArgument::Integral: 7488 case TemplateArgument::Declaration: 7489 case TemplateArgument::NullPtr: 7490 case TemplateArgument::Pack: 7491 // FIXME: Is this right? 7492 return TemplateArgumentLocInfo(); 7493 } 7494 llvm_unreachable("unexpected template argument loc"); 7495 } 7496 7497 TemplateArgumentLoc 7498 ASTReader::ReadTemplateArgumentLoc(ModuleFile &F, 7499 const RecordData &Record, unsigned &Index) { 7500 TemplateArgument Arg = ReadTemplateArgument(F, Record, Index); 7501 7502 if (Arg.getKind() == TemplateArgument::Expression) { 7503 if (Record[Index++]) // bool InfoHasSameExpr. 7504 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7505 } 7506 return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(F, Arg.getKind(), 7507 Record, Index)); 7508 } 7509 7510 const ASTTemplateArgumentListInfo* 7511 ASTReader::ReadASTTemplateArgumentListInfo(ModuleFile &F, 7512 const RecordData &Record, 7513 unsigned &Index) { 7514 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Index); 7515 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Index); 7516 unsigned NumArgsAsWritten = Record[Index++]; 7517 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7518 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7519 TemplArgsInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Index)); 7520 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7521 } 7522 7523 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7524 return GetDecl(ID); 7525 } 7526 7527 void ASTReader::CompleteRedeclChain(const Decl *D) { 7528 if (NumCurrentElementsDeserializing) { 7529 // We arrange to not care about the complete redeclaration chain while we're 7530 // deserializing. Just remember that the AST has marked this one as complete 7531 // but that it's not actually complete yet, so we know we still need to 7532 // complete it later. 7533 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7534 return; 7535 } 7536 7537 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7538 7539 // If this is a named declaration, complete it by looking it up 7540 // within its context. 7541 // 7542 // FIXME: Merging a function definition should merge 7543 // all mergeable entities within it. 7544 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7545 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7546 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7547 if (!getContext().getLangOpts().CPlusPlus && 7548 isa<TranslationUnitDecl>(DC)) { 7549 // Outside of C++, we don't have a lookup table for the TU, so update 7550 // the identifier instead. (For C++ modules, we don't store decls 7551 // in the serialized identifier table, so we do the lookup in the TU.) 7552 auto *II = Name.getAsIdentifierInfo(); 7553 assert(II && "non-identifier name in C?"); 7554 if (II->isOutOfDate()) 7555 updateOutOfDateIdentifier(*II); 7556 } else 7557 DC->lookup(Name); 7558 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7559 // Find all declarations of this kind from the relevant context. 7560 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7561 auto *DC = cast<DeclContext>(DCDecl); 7562 SmallVector<Decl*, 8> Decls; 7563 FindExternalLexicalDecls( 7564 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7565 } 7566 } 7567 } 7568 7569 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7570 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7571 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7572 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7573 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7574 if (auto *Template = FD->getPrimaryTemplate()) 7575 Template->LoadLazySpecializations(); 7576 } 7577 } 7578 7579 CXXCtorInitializer ** 7580 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7581 RecordLocation Loc = getLocalBitOffset(Offset); 7582 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7583 SavedStreamPosition SavedPosition(Cursor); 7584 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7585 Error(std::move(Err)); 7586 return nullptr; 7587 } 7588 ReadingKindTracker ReadingKind(Read_Decl, *this); 7589 7590 RecordData Record; 7591 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7592 if (!MaybeCode) { 7593 Error(MaybeCode.takeError()); 7594 return nullptr; 7595 } 7596 unsigned Code = MaybeCode.get(); 7597 7598 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record); 7599 if (!MaybeRecCode) { 7600 Error(MaybeRecCode.takeError()); 7601 return nullptr; 7602 } 7603 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7604 Error("malformed AST file: missing C++ ctor initializers"); 7605 return nullptr; 7606 } 7607 7608 unsigned Idx = 0; 7609 return ReadCXXCtorInitializers(*Loc.F, Record, Idx); 7610 } 7611 7612 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7613 assert(ContextObj && "reading base specifiers with no AST context"); 7614 ASTContext &Context = *ContextObj; 7615 7616 RecordLocation Loc = getLocalBitOffset(Offset); 7617 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7618 SavedStreamPosition SavedPosition(Cursor); 7619 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7620 Error(std::move(Err)); 7621 return nullptr; 7622 } 7623 ReadingKindTracker ReadingKind(Read_Decl, *this); 7624 RecordData Record; 7625 7626 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7627 if (!MaybeCode) { 7628 Error(MaybeCode.takeError()); 7629 return nullptr; 7630 } 7631 unsigned Code = MaybeCode.get(); 7632 7633 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record); 7634 if (!MaybeRecCode) { 7635 Error(MaybeCode.takeError()); 7636 return nullptr; 7637 } 7638 unsigned RecCode = MaybeRecCode.get(); 7639 7640 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7641 Error("malformed AST file: missing C++ base specifiers"); 7642 return nullptr; 7643 } 7644 7645 unsigned Idx = 0; 7646 unsigned NumBases = Record[Idx++]; 7647 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7648 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7649 for (unsigned I = 0; I != NumBases; ++I) 7650 Bases[I] = ReadCXXBaseSpecifier(*Loc.F, Record, Idx); 7651 return Bases; 7652 } 7653 7654 serialization::DeclID 7655 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7656 if (LocalID < NUM_PREDEF_DECL_IDS) 7657 return LocalID; 7658 7659 if (!F.ModuleOffsetMap.empty()) 7660 ReadModuleOffsetMap(F); 7661 7662 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7663 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7664 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7665 7666 return LocalID + I->second; 7667 } 7668 7669 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7670 ModuleFile &M) const { 7671 // Predefined decls aren't from any module. 7672 if (ID < NUM_PREDEF_DECL_IDS) 7673 return false; 7674 7675 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7676 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7677 } 7678 7679 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7680 if (!D->isFromASTFile()) 7681 return nullptr; 7682 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7683 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7684 return I->second; 7685 } 7686 7687 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7688 if (ID < NUM_PREDEF_DECL_IDS) 7689 return SourceLocation(); 7690 7691 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7692 7693 if (Index > DeclsLoaded.size()) { 7694 Error("declaration ID out-of-range for AST file"); 7695 return SourceLocation(); 7696 } 7697 7698 if (Decl *D = DeclsLoaded[Index]) 7699 return D->getLocation(); 7700 7701 SourceLocation Loc; 7702 DeclCursorForID(ID, Loc); 7703 return Loc; 7704 } 7705 7706 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7707 switch (ID) { 7708 case PREDEF_DECL_NULL_ID: 7709 return nullptr; 7710 7711 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7712 return Context.getTranslationUnitDecl(); 7713 7714 case PREDEF_DECL_OBJC_ID_ID: 7715 return Context.getObjCIdDecl(); 7716 7717 case PREDEF_DECL_OBJC_SEL_ID: 7718 return Context.getObjCSelDecl(); 7719 7720 case PREDEF_DECL_OBJC_CLASS_ID: 7721 return Context.getObjCClassDecl(); 7722 7723 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7724 return Context.getObjCProtocolDecl(); 7725 7726 case PREDEF_DECL_INT_128_ID: 7727 return Context.getInt128Decl(); 7728 7729 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7730 return Context.getUInt128Decl(); 7731 7732 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7733 return Context.getObjCInstanceTypeDecl(); 7734 7735 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7736 return Context.getBuiltinVaListDecl(); 7737 7738 case PREDEF_DECL_VA_LIST_TAG: 7739 return Context.getVaListTagDecl(); 7740 7741 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7742 return Context.getBuiltinMSVaListDecl(); 7743 7744 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7745 return Context.getExternCContextDecl(); 7746 7747 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7748 return Context.getMakeIntegerSeqDecl(); 7749 7750 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7751 return Context.getCFConstantStringDecl(); 7752 7753 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7754 return Context.getCFConstantStringTagDecl(); 7755 7756 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7757 return Context.getTypePackElementDecl(); 7758 } 7759 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7760 } 7761 7762 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7763 assert(ContextObj && "reading decl with no AST context"); 7764 if (ID < NUM_PREDEF_DECL_IDS) { 7765 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7766 if (D) { 7767 // Track that we have merged the declaration with ID \p ID into the 7768 // pre-existing predefined declaration \p D. 7769 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7770 if (Merged.empty()) 7771 Merged.push_back(ID); 7772 } 7773 return D; 7774 } 7775 7776 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7777 7778 if (Index >= DeclsLoaded.size()) { 7779 assert(0 && "declaration ID out-of-range for AST file"); 7780 Error("declaration ID out-of-range for AST file"); 7781 return nullptr; 7782 } 7783 7784 return DeclsLoaded[Index]; 7785 } 7786 7787 Decl *ASTReader::GetDecl(DeclID ID) { 7788 if (ID < NUM_PREDEF_DECL_IDS) 7789 return GetExistingDecl(ID); 7790 7791 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7792 7793 if (Index >= DeclsLoaded.size()) { 7794 assert(0 && "declaration ID out-of-range for AST file"); 7795 Error("declaration ID out-of-range for AST file"); 7796 return nullptr; 7797 } 7798 7799 if (!DeclsLoaded[Index]) { 7800 ReadDeclRecord(ID); 7801 if (DeserializationListener) 7802 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7803 } 7804 7805 return DeclsLoaded[Index]; 7806 } 7807 7808 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7809 DeclID GlobalID) { 7810 if (GlobalID < NUM_PREDEF_DECL_IDS) 7811 return GlobalID; 7812 7813 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7814 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7815 ModuleFile *Owner = I->second; 7816 7817 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7818 = M.GlobalToLocalDeclIDs.find(Owner); 7819 if (Pos == M.GlobalToLocalDeclIDs.end()) 7820 return 0; 7821 7822 return GlobalID - Owner->BaseDeclID + Pos->second; 7823 } 7824 7825 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7826 const RecordData &Record, 7827 unsigned &Idx) { 7828 if (Idx >= Record.size()) { 7829 Error("Corrupted AST file"); 7830 return 0; 7831 } 7832 7833 return getGlobalDeclID(F, Record[Idx++]); 7834 } 7835 7836 /// Resolve the offset of a statement into a statement. 7837 /// 7838 /// This operation will read a new statement from the external 7839 /// source each time it is called, and is meant to be used via a 7840 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7841 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7842 // Switch case IDs are per Decl. 7843 ClearSwitchCaseIDs(); 7844 7845 // Offset here is a global offset across the entire chain. 7846 RecordLocation Loc = getLocalBitOffset(Offset); 7847 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7848 Error(std::move(Err)); 7849 return nullptr; 7850 } 7851 assert(NumCurrentElementsDeserializing == 0 && 7852 "should not be called while already deserializing"); 7853 Deserializing D(this); 7854 return ReadStmtFromStream(*Loc.F); 7855 } 7856 7857 void ASTReader::FindExternalLexicalDecls( 7858 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7859 SmallVectorImpl<Decl *> &Decls) { 7860 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7861 7862 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7863 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7864 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7865 auto K = (Decl::Kind)+LexicalDecls[I]; 7866 if (!IsKindWeWant(K)) 7867 continue; 7868 7869 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7870 7871 // Don't add predefined declarations to the lexical context more 7872 // than once. 7873 if (ID < NUM_PREDEF_DECL_IDS) { 7874 if (PredefsVisited[ID]) 7875 continue; 7876 7877 PredefsVisited[ID] = true; 7878 } 7879 7880 if (Decl *D = GetLocalDecl(*M, ID)) { 7881 assert(D->getKind() == K && "wrong kind for lexical decl"); 7882 if (!DC->isDeclInLexicalTraversal(D)) 7883 Decls.push_back(D); 7884 } 7885 } 7886 }; 7887 7888 if (isa<TranslationUnitDecl>(DC)) { 7889 for (auto Lexical : TULexicalDecls) 7890 Visit(Lexical.first, Lexical.second); 7891 } else { 7892 auto I = LexicalDecls.find(DC); 7893 if (I != LexicalDecls.end()) 7894 Visit(I->second.first, I->second.second); 7895 } 7896 7897 ++NumLexicalDeclContextsRead; 7898 } 7899 7900 namespace { 7901 7902 class DeclIDComp { 7903 ASTReader &Reader; 7904 ModuleFile &Mod; 7905 7906 public: 7907 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7908 7909 bool operator()(LocalDeclID L, LocalDeclID R) const { 7910 SourceLocation LHS = getLocation(L); 7911 SourceLocation RHS = getLocation(R); 7912 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7913 } 7914 7915 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7916 SourceLocation RHS = getLocation(R); 7917 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7918 } 7919 7920 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7921 SourceLocation LHS = getLocation(L); 7922 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7923 } 7924 7925 SourceLocation getLocation(LocalDeclID ID) const { 7926 return Reader.getSourceManager().getFileLoc( 7927 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7928 } 7929 }; 7930 7931 } // namespace 7932 7933 void ASTReader::FindFileRegionDecls(FileID File, 7934 unsigned Offset, unsigned Length, 7935 SmallVectorImpl<Decl *> &Decls) { 7936 SourceManager &SM = getSourceManager(); 7937 7938 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7939 if (I == FileDeclIDs.end()) 7940 return; 7941 7942 FileDeclsInfo &DInfo = I->second; 7943 if (DInfo.Decls.empty()) 7944 return; 7945 7946 SourceLocation 7947 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7948 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7949 7950 DeclIDComp DIDComp(*this, *DInfo.Mod); 7951 ArrayRef<serialization::LocalDeclID>::iterator 7952 BeginIt = std::lower_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 7953 BeginLoc, DIDComp); 7954 if (BeginIt != DInfo.Decls.begin()) 7955 --BeginIt; 7956 7957 // If we are pointing at a top-level decl inside an objc container, we need 7958 // to backtrack until we find it otherwise we will fail to report that the 7959 // region overlaps with an objc container. 7960 while (BeginIt != DInfo.Decls.begin() && 7961 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7962 ->isTopLevelDeclInObjCContainer()) 7963 --BeginIt; 7964 7965 ArrayRef<serialization::LocalDeclID>::iterator 7966 EndIt = std::upper_bound(DInfo.Decls.begin(), DInfo.Decls.end(), 7967 EndLoc, DIDComp); 7968 if (EndIt != DInfo.Decls.end()) 7969 ++EndIt; 7970 7971 for (ArrayRef<serialization::LocalDeclID>::iterator 7972 DIt = BeginIt; DIt != EndIt; ++DIt) 7973 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7974 } 7975 7976 bool 7977 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7978 DeclarationName Name) { 7979 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7980 "DeclContext has no visible decls in storage"); 7981 if (!Name) 7982 return false; 7983 7984 auto It = Lookups.find(DC); 7985 if (It == Lookups.end()) 7986 return false; 7987 7988 Deserializing LookupResults(this); 7989 7990 // Load the list of declarations. 7991 SmallVector<NamedDecl *, 64> Decls; 7992 for (DeclID ID : It->second.Table.find(Name)) { 7993 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7994 if (ND->getDeclName() == Name) 7995 Decls.push_back(ND); 7996 } 7997 7998 ++NumVisibleDeclContextsRead; 7999 SetExternalVisibleDeclsForName(DC, Name, Decls); 8000 return !Decls.empty(); 8001 } 8002 8003 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 8004 if (!DC->hasExternalVisibleStorage()) 8005 return; 8006 8007 auto It = Lookups.find(DC); 8008 assert(It != Lookups.end() && 8009 "have external visible storage but no lookup tables"); 8010 8011 DeclsMap Decls; 8012 8013 for (DeclID ID : It->second.Table.findAll()) { 8014 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 8015 Decls[ND->getDeclName()].push_back(ND); 8016 } 8017 8018 ++NumVisibleDeclContextsRead; 8019 8020 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 8021 SetExternalVisibleDeclsForName(DC, I->first, I->second); 8022 } 8023 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 8024 } 8025 8026 const serialization::reader::DeclContextLookupTable * 8027 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 8028 auto I = Lookups.find(Primary); 8029 return I == Lookups.end() ? nullptr : &I->second; 8030 } 8031 8032 /// Under non-PCH compilation the consumer receives the objc methods 8033 /// before receiving the implementation, and codegen depends on this. 8034 /// We simulate this by deserializing and passing to consumer the methods of the 8035 /// implementation before passing the deserialized implementation decl. 8036 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 8037 ASTConsumer *Consumer) { 8038 assert(ImplD && Consumer); 8039 8040 for (auto *I : ImplD->methods()) 8041 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 8042 8043 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 8044 } 8045 8046 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 8047 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 8048 PassObjCImplDeclToConsumer(ImplD, Consumer); 8049 else 8050 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 8051 } 8052 8053 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 8054 this->Consumer = Consumer; 8055 8056 if (Consumer) 8057 PassInterestingDeclsToConsumer(); 8058 8059 if (DeserializationListener) 8060 DeserializationListener->ReaderInitialized(this); 8061 } 8062 8063 void ASTReader::PrintStats() { 8064 std::fprintf(stderr, "*** AST File Statistics:\n"); 8065 8066 unsigned NumTypesLoaded 8067 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 8068 QualType()); 8069 unsigned NumDeclsLoaded 8070 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 8071 (Decl *)nullptr); 8072 unsigned NumIdentifiersLoaded 8073 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 8074 IdentifiersLoaded.end(), 8075 (IdentifierInfo *)nullptr); 8076 unsigned NumMacrosLoaded 8077 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 8078 MacrosLoaded.end(), 8079 (MacroInfo *)nullptr); 8080 unsigned NumSelectorsLoaded 8081 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 8082 SelectorsLoaded.end(), 8083 Selector()); 8084 8085 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 8086 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 8087 NumSLocEntriesRead, TotalNumSLocEntries, 8088 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 8089 if (!TypesLoaded.empty()) 8090 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 8091 NumTypesLoaded, (unsigned)TypesLoaded.size(), 8092 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 8093 if (!DeclsLoaded.empty()) 8094 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 8095 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 8096 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 8097 if (!IdentifiersLoaded.empty()) 8098 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 8099 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 8100 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 8101 if (!MacrosLoaded.empty()) 8102 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 8103 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 8104 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 8105 if (!SelectorsLoaded.empty()) 8106 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 8107 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 8108 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 8109 if (TotalNumStatements) 8110 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 8111 NumStatementsRead, TotalNumStatements, 8112 ((float)NumStatementsRead/TotalNumStatements * 100)); 8113 if (TotalNumMacros) 8114 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 8115 NumMacrosRead, TotalNumMacros, 8116 ((float)NumMacrosRead/TotalNumMacros * 100)); 8117 if (TotalLexicalDeclContexts) 8118 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 8119 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 8120 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 8121 * 100)); 8122 if (TotalVisibleDeclContexts) 8123 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 8124 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 8125 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 8126 * 100)); 8127 if (TotalNumMethodPoolEntries) 8128 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 8129 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 8130 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 8131 * 100)); 8132 if (NumMethodPoolLookups) 8133 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 8134 NumMethodPoolHits, NumMethodPoolLookups, 8135 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 8136 if (NumMethodPoolTableLookups) 8137 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 8138 NumMethodPoolTableHits, NumMethodPoolTableLookups, 8139 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 8140 * 100.0)); 8141 if (NumIdentifierLookupHits) 8142 std::fprintf(stderr, 8143 " %u / %u identifier table lookups succeeded (%f%%)\n", 8144 NumIdentifierLookupHits, NumIdentifierLookups, 8145 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 8146 8147 if (GlobalIndex) { 8148 std::fprintf(stderr, "\n"); 8149 GlobalIndex->printStats(); 8150 } 8151 8152 std::fprintf(stderr, "\n"); 8153 dump(); 8154 std::fprintf(stderr, "\n"); 8155 } 8156 8157 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 8158 LLVM_DUMP_METHOD static void 8159 dumpModuleIDMap(StringRef Name, 8160 const ContinuousRangeMap<Key, ModuleFile *, 8161 InitialCapacity> &Map) { 8162 if (Map.begin() == Map.end()) 8163 return; 8164 8165 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 8166 8167 llvm::errs() << Name << ":\n"; 8168 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 8169 I != IEnd; ++I) { 8170 llvm::errs() << " " << I->first << " -> " << I->second->FileName 8171 << "\n"; 8172 } 8173 } 8174 8175 LLVM_DUMP_METHOD void ASTReader::dump() { 8176 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 8177 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 8178 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 8179 dumpModuleIDMap("Global type map", GlobalTypeMap); 8180 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 8181 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 8182 dumpModuleIDMap("Global macro map", GlobalMacroMap); 8183 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 8184 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 8185 dumpModuleIDMap("Global preprocessed entity map", 8186 GlobalPreprocessedEntityMap); 8187 8188 llvm::errs() << "\n*** PCH/Modules Loaded:"; 8189 for (ModuleFile &M : ModuleMgr) 8190 M.dump(); 8191 } 8192 8193 /// Return the amount of memory used by memory buffers, breaking down 8194 /// by heap-backed versus mmap'ed memory. 8195 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 8196 for (ModuleFile &I : ModuleMgr) { 8197 if (llvm::MemoryBuffer *buf = I.Buffer) { 8198 size_t bytes = buf->getBufferSize(); 8199 switch (buf->getBufferKind()) { 8200 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 8201 sizes.malloc_bytes += bytes; 8202 break; 8203 case llvm::MemoryBuffer::MemoryBuffer_MMap: 8204 sizes.mmap_bytes += bytes; 8205 break; 8206 } 8207 } 8208 } 8209 } 8210 8211 void ASTReader::InitializeSema(Sema &S) { 8212 SemaObj = &S; 8213 S.addExternalSource(this); 8214 8215 // Makes sure any declarations that were deserialized "too early" 8216 // still get added to the identifier's declaration chains. 8217 for (uint64_t ID : PreloadedDeclIDs) { 8218 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 8219 pushExternalDeclIntoScope(D, D->getDeclName()); 8220 } 8221 PreloadedDeclIDs.clear(); 8222 8223 // FIXME: What happens if these are changed by a module import? 8224 if (!FPPragmaOptions.empty()) { 8225 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 8226 SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]); 8227 } 8228 8229 SemaObj->OpenCLFeatures.copy(OpenCLExtensions); 8230 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 8231 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 8232 8233 UpdateSema(); 8234 } 8235 8236 void ASTReader::UpdateSema() { 8237 assert(SemaObj && "no Sema to update"); 8238 8239 // Load the offsets of the declarations that Sema references. 8240 // They will be lazily deserialized when needed. 8241 if (!SemaDeclRefs.empty()) { 8242 assert(SemaDeclRefs.size() % 3 == 0); 8243 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 8244 if (!SemaObj->StdNamespace) 8245 SemaObj->StdNamespace = SemaDeclRefs[I]; 8246 if (!SemaObj->StdBadAlloc) 8247 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 8248 if (!SemaObj->StdAlignValT) 8249 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 8250 } 8251 SemaDeclRefs.clear(); 8252 } 8253 8254 // Update the state of pragmas. Use the same API as if we had encountered the 8255 // pragma in the source. 8256 if(OptimizeOffPragmaLocation.isValid()) 8257 SemaObj->ActOnPragmaOptimize(/* IsOn = */ false, OptimizeOffPragmaLocation); 8258 if (PragmaMSStructState != -1) 8259 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 8260 if (PointersToMembersPragmaLocation.isValid()) { 8261 SemaObj->ActOnPragmaMSPointersToMembers( 8262 (LangOptions::PragmaMSPointersToMembersKind) 8263 PragmaMSPointersToMembersState, 8264 PointersToMembersPragmaLocation); 8265 } 8266 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 8267 8268 if (PragmaPackCurrentValue) { 8269 // The bottom of the stack might have a default value. It must be adjusted 8270 // to the current value to ensure that the packing state is preserved after 8271 // popping entries that were included/imported from a PCH/module. 8272 bool DropFirst = false; 8273 if (!PragmaPackStack.empty() && 8274 PragmaPackStack.front().Location.isInvalid()) { 8275 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue && 8276 "Expected a default alignment value"); 8277 SemaObj->PackStack.Stack.emplace_back( 8278 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue, 8279 SemaObj->PackStack.CurrentPragmaLocation, 8280 PragmaPackStack.front().PushLocation); 8281 DropFirst = true; 8282 } 8283 for (const auto &Entry : 8284 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0)) 8285 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value, 8286 Entry.Location, Entry.PushLocation); 8287 if (PragmaPackCurrentLocation.isInvalid()) { 8288 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue && 8289 "Expected a default alignment value"); 8290 // Keep the current values. 8291 } else { 8292 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue; 8293 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation; 8294 } 8295 } 8296 } 8297 8298 IdentifierInfo *ASTReader::get(StringRef Name) { 8299 // Note that we are loading an identifier. 8300 Deserializing AnIdentifier(this); 8301 8302 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 8303 NumIdentifierLookups, 8304 NumIdentifierLookupHits); 8305 8306 // We don't need to do identifier table lookups in C++ modules (we preload 8307 // all interesting declarations, and don't need to use the scope for name 8308 // lookups). Perform the lookup in PCH files, though, since we don't build 8309 // a complete initial identifier table if we're carrying on from a PCH. 8310 if (PP.getLangOpts().CPlusPlus) { 8311 for (auto F : ModuleMgr.pch_modules()) 8312 if (Visitor(*F)) 8313 break; 8314 } else { 8315 // If there is a global index, look there first to determine which modules 8316 // provably do not have any results for this identifier. 8317 GlobalModuleIndex::HitSet Hits; 8318 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 8319 if (!loadGlobalIndex()) { 8320 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 8321 HitsPtr = &Hits; 8322 } 8323 } 8324 8325 ModuleMgr.visit(Visitor, HitsPtr); 8326 } 8327 8328 IdentifierInfo *II = Visitor.getIdentifierInfo(); 8329 markIdentifierUpToDate(II); 8330 return II; 8331 } 8332 8333 namespace clang { 8334 8335 /// An identifier-lookup iterator that enumerates all of the 8336 /// identifiers stored within a set of AST files. 8337 class ASTIdentifierIterator : public IdentifierIterator { 8338 /// The AST reader whose identifiers are being enumerated. 8339 const ASTReader &Reader; 8340 8341 /// The current index into the chain of AST files stored in 8342 /// the AST reader. 8343 unsigned Index; 8344 8345 /// The current position within the identifier lookup table 8346 /// of the current AST file. 8347 ASTIdentifierLookupTable::key_iterator Current; 8348 8349 /// The end position within the identifier lookup table of 8350 /// the current AST file. 8351 ASTIdentifierLookupTable::key_iterator End; 8352 8353 /// Whether to skip any modules in the ASTReader. 8354 bool SkipModules; 8355 8356 public: 8357 explicit ASTIdentifierIterator(const ASTReader &Reader, 8358 bool SkipModules = false); 8359 8360 StringRef Next() override; 8361 }; 8362 8363 } // namespace clang 8364 8365 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8366 bool SkipModules) 8367 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8368 } 8369 8370 StringRef ASTIdentifierIterator::Next() { 8371 while (Current == End) { 8372 // If we have exhausted all of our AST files, we're done. 8373 if (Index == 0) 8374 return StringRef(); 8375 8376 --Index; 8377 ModuleFile &F = Reader.ModuleMgr[Index]; 8378 if (SkipModules && F.isModule()) 8379 continue; 8380 8381 ASTIdentifierLookupTable *IdTable = 8382 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8383 Current = IdTable->key_begin(); 8384 End = IdTable->key_end(); 8385 } 8386 8387 // We have any identifiers remaining in the current AST file; return 8388 // the next one. 8389 StringRef Result = *Current; 8390 ++Current; 8391 return Result; 8392 } 8393 8394 namespace { 8395 8396 /// A utility for appending two IdentifierIterators. 8397 class ChainedIdentifierIterator : public IdentifierIterator { 8398 std::unique_ptr<IdentifierIterator> Current; 8399 std::unique_ptr<IdentifierIterator> Queued; 8400 8401 public: 8402 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8403 std::unique_ptr<IdentifierIterator> Second) 8404 : Current(std::move(First)), Queued(std::move(Second)) {} 8405 8406 StringRef Next() override { 8407 if (!Current) 8408 return StringRef(); 8409 8410 StringRef result = Current->Next(); 8411 if (!result.empty()) 8412 return result; 8413 8414 // Try the queued iterator, which may itself be empty. 8415 Current.reset(); 8416 std::swap(Current, Queued); 8417 return Next(); 8418 } 8419 }; 8420 8421 } // namespace 8422 8423 IdentifierIterator *ASTReader::getIdentifiers() { 8424 if (!loadGlobalIndex()) { 8425 std::unique_ptr<IdentifierIterator> ReaderIter( 8426 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8427 std::unique_ptr<IdentifierIterator> ModulesIter( 8428 GlobalIndex->createIdentifierIterator()); 8429 return new ChainedIdentifierIterator(std::move(ReaderIter), 8430 std::move(ModulesIter)); 8431 } 8432 8433 return new ASTIdentifierIterator(*this); 8434 } 8435 8436 namespace clang { 8437 namespace serialization { 8438 8439 class ReadMethodPoolVisitor { 8440 ASTReader &Reader; 8441 Selector Sel; 8442 unsigned PriorGeneration; 8443 unsigned InstanceBits = 0; 8444 unsigned FactoryBits = 0; 8445 bool InstanceHasMoreThanOneDecl = false; 8446 bool FactoryHasMoreThanOneDecl = false; 8447 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8448 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8449 8450 public: 8451 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8452 unsigned PriorGeneration) 8453 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8454 8455 bool operator()(ModuleFile &M) { 8456 if (!M.SelectorLookupTable) 8457 return false; 8458 8459 // If we've already searched this module file, skip it now. 8460 if (M.Generation <= PriorGeneration) 8461 return true; 8462 8463 ++Reader.NumMethodPoolTableLookups; 8464 ASTSelectorLookupTable *PoolTable 8465 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8466 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8467 if (Pos == PoolTable->end()) 8468 return false; 8469 8470 ++Reader.NumMethodPoolTableHits; 8471 ++Reader.NumSelectorsRead; 8472 // FIXME: Not quite happy with the statistics here. We probably should 8473 // disable this tracking when called via LoadSelector. 8474 // Also, should entries without methods count as misses? 8475 ++Reader.NumMethodPoolEntriesRead; 8476 ASTSelectorLookupTrait::data_type Data = *Pos; 8477 if (Reader.DeserializationListener) 8478 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8479 8480 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8481 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8482 InstanceBits = Data.InstanceBits; 8483 FactoryBits = Data.FactoryBits; 8484 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8485 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8486 return true; 8487 } 8488 8489 /// Retrieve the instance methods found by this visitor. 8490 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8491 return InstanceMethods; 8492 } 8493 8494 /// Retrieve the instance methods found by this visitor. 8495 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8496 return FactoryMethods; 8497 } 8498 8499 unsigned getInstanceBits() const { return InstanceBits; } 8500 unsigned getFactoryBits() const { return FactoryBits; } 8501 8502 bool instanceHasMoreThanOneDecl() const { 8503 return InstanceHasMoreThanOneDecl; 8504 } 8505 8506 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8507 }; 8508 8509 } // namespace serialization 8510 } // namespace clang 8511 8512 /// Add the given set of methods to the method list. 8513 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8514 ObjCMethodList &List) { 8515 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8516 S.addMethodToGlobalList(&List, Methods[I]); 8517 } 8518 } 8519 8520 void ASTReader::ReadMethodPool(Selector Sel) { 8521 // Get the selector generation and update it to the current generation. 8522 unsigned &Generation = SelectorGeneration[Sel]; 8523 unsigned PriorGeneration = Generation; 8524 Generation = getGeneration(); 8525 SelectorOutOfDate[Sel] = false; 8526 8527 // Search for methods defined with this selector. 8528 ++NumMethodPoolLookups; 8529 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8530 ModuleMgr.visit(Visitor); 8531 8532 if (Visitor.getInstanceMethods().empty() && 8533 Visitor.getFactoryMethods().empty()) 8534 return; 8535 8536 ++NumMethodPoolHits; 8537 8538 if (!getSema()) 8539 return; 8540 8541 Sema &S = *getSema(); 8542 Sema::GlobalMethodPool::iterator Pos 8543 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8544 8545 Pos->second.first.setBits(Visitor.getInstanceBits()); 8546 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8547 Pos->second.second.setBits(Visitor.getFactoryBits()); 8548 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8549 8550 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8551 // when building a module we keep every method individually and may need to 8552 // update hasMoreThanOneDecl as we add the methods. 8553 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8554 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8555 } 8556 8557 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8558 if (SelectorOutOfDate[Sel]) 8559 ReadMethodPool(Sel); 8560 } 8561 8562 void ASTReader::ReadKnownNamespaces( 8563 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8564 Namespaces.clear(); 8565 8566 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8567 if (NamespaceDecl *Namespace 8568 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8569 Namespaces.push_back(Namespace); 8570 } 8571 } 8572 8573 void ASTReader::ReadUndefinedButUsed( 8574 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8575 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8576 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8577 SourceLocation Loc = 8578 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8579 Undefined.insert(std::make_pair(D, Loc)); 8580 } 8581 } 8582 8583 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8584 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8585 Exprs) { 8586 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8587 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8588 uint64_t Count = DelayedDeleteExprs[Idx++]; 8589 for (uint64_t C = 0; C < Count; ++C) { 8590 SourceLocation DeleteLoc = 8591 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8592 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8593 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8594 } 8595 } 8596 } 8597 8598 void ASTReader::ReadTentativeDefinitions( 8599 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8600 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8601 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8602 if (Var) 8603 TentativeDefs.push_back(Var); 8604 } 8605 TentativeDefinitions.clear(); 8606 } 8607 8608 void ASTReader::ReadUnusedFileScopedDecls( 8609 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8610 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8611 DeclaratorDecl *D 8612 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8613 if (D) 8614 Decls.push_back(D); 8615 } 8616 UnusedFileScopedDecls.clear(); 8617 } 8618 8619 void ASTReader::ReadDelegatingConstructors( 8620 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8621 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8622 CXXConstructorDecl *D 8623 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8624 if (D) 8625 Decls.push_back(D); 8626 } 8627 DelegatingCtorDecls.clear(); 8628 } 8629 8630 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8631 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8632 TypedefNameDecl *D 8633 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8634 if (D) 8635 Decls.push_back(D); 8636 } 8637 ExtVectorDecls.clear(); 8638 } 8639 8640 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8641 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8642 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8643 ++I) { 8644 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8645 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8646 if (D) 8647 Decls.insert(D); 8648 } 8649 UnusedLocalTypedefNameCandidates.clear(); 8650 } 8651 8652 void ASTReader::ReadReferencedSelectors( 8653 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8654 if (ReferencedSelectorsData.empty()) 8655 return; 8656 8657 // If there are @selector references added them to its pool. This is for 8658 // implementation of -Wselector. 8659 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8660 unsigned I = 0; 8661 while (I < DataSize) { 8662 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8663 SourceLocation SelLoc 8664 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8665 Sels.push_back(std::make_pair(Sel, SelLoc)); 8666 } 8667 ReferencedSelectorsData.clear(); 8668 } 8669 8670 void ASTReader::ReadWeakUndeclaredIdentifiers( 8671 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8672 if (WeakUndeclaredIdentifiers.empty()) 8673 return; 8674 8675 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8676 IdentifierInfo *WeakId 8677 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8678 IdentifierInfo *AliasId 8679 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8680 SourceLocation Loc 8681 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8682 bool Used = WeakUndeclaredIdentifiers[I++]; 8683 WeakInfo WI(AliasId, Loc); 8684 WI.setUsed(Used); 8685 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8686 } 8687 WeakUndeclaredIdentifiers.clear(); 8688 } 8689 8690 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8691 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8692 ExternalVTableUse VT; 8693 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8694 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8695 VT.DefinitionRequired = VTableUses[Idx++]; 8696 VTables.push_back(VT); 8697 } 8698 8699 VTableUses.clear(); 8700 } 8701 8702 void ASTReader::ReadPendingInstantiations( 8703 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8704 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8705 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8706 SourceLocation Loc 8707 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8708 8709 Pending.push_back(std::make_pair(D, Loc)); 8710 } 8711 PendingInstantiations.clear(); 8712 } 8713 8714 void ASTReader::ReadLateParsedTemplates( 8715 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8716 &LPTMap) { 8717 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N; 8718 /* In loop */) { 8719 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++])); 8720 8721 auto LT = llvm::make_unique<LateParsedTemplate>(); 8722 LT->D = GetDecl(LateParsedTemplates[Idx++]); 8723 8724 ModuleFile *F = getOwningModuleFile(LT->D); 8725 assert(F && "No module"); 8726 8727 unsigned TokN = LateParsedTemplates[Idx++]; 8728 LT->Toks.reserve(TokN); 8729 for (unsigned T = 0; T < TokN; ++T) 8730 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx)); 8731 8732 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8733 } 8734 8735 LateParsedTemplates.clear(); 8736 } 8737 8738 void ASTReader::LoadSelector(Selector Sel) { 8739 // It would be complicated to avoid reading the methods anyway. So don't. 8740 ReadMethodPool(Sel); 8741 } 8742 8743 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8744 assert(ID && "Non-zero identifier ID required"); 8745 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8746 IdentifiersLoaded[ID - 1] = II; 8747 if (DeserializationListener) 8748 DeserializationListener->IdentifierRead(ID, II); 8749 } 8750 8751 /// Set the globally-visible declarations associated with the given 8752 /// identifier. 8753 /// 8754 /// If the AST reader is currently in a state where the given declaration IDs 8755 /// cannot safely be resolved, they are queued until it is safe to resolve 8756 /// them. 8757 /// 8758 /// \param II an IdentifierInfo that refers to one or more globally-visible 8759 /// declarations. 8760 /// 8761 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8762 /// visible at global scope. 8763 /// 8764 /// \param Decls if non-null, this vector will be populated with the set of 8765 /// deserialized declarations. These declarations will not be pushed into 8766 /// scope. 8767 void 8768 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8769 const SmallVectorImpl<uint32_t> &DeclIDs, 8770 SmallVectorImpl<Decl *> *Decls) { 8771 if (NumCurrentElementsDeserializing && !Decls) { 8772 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8773 return; 8774 } 8775 8776 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8777 if (!SemaObj) { 8778 // Queue this declaration so that it will be added to the 8779 // translation unit scope and identifier's declaration chain 8780 // once a Sema object is known. 8781 PreloadedDeclIDs.push_back(DeclIDs[I]); 8782 continue; 8783 } 8784 8785 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8786 8787 // If we're simply supposed to record the declarations, do so now. 8788 if (Decls) { 8789 Decls->push_back(D); 8790 continue; 8791 } 8792 8793 // Introduce this declaration into the translation-unit scope 8794 // and add it to the declaration chain for this identifier, so 8795 // that (unqualified) name lookup will find it. 8796 pushExternalDeclIntoScope(D, II); 8797 } 8798 } 8799 8800 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8801 if (ID == 0) 8802 return nullptr; 8803 8804 if (IdentifiersLoaded.empty()) { 8805 Error("no identifier table in AST file"); 8806 return nullptr; 8807 } 8808 8809 ID -= 1; 8810 if (!IdentifiersLoaded[ID]) { 8811 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8812 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8813 ModuleFile *M = I->second; 8814 unsigned Index = ID - M->BaseIdentifierID; 8815 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8816 8817 // All of the strings in the AST file are preceded by a 16-bit length. 8818 // Extract that 16-bit length to avoid having to execute strlen(). 8819 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8820 // unsigned integers. This is important to avoid integer overflow when 8821 // we cast them to 'unsigned'. 8822 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8823 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8824 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8825 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8826 IdentifiersLoaded[ID] = &II; 8827 markIdentifierFromAST(*this, II); 8828 if (DeserializationListener) 8829 DeserializationListener->IdentifierRead(ID + 1, &II); 8830 } 8831 8832 return IdentifiersLoaded[ID]; 8833 } 8834 8835 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8836 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8837 } 8838 8839 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8840 if (LocalID < NUM_PREDEF_IDENT_IDS) 8841 return LocalID; 8842 8843 if (!M.ModuleOffsetMap.empty()) 8844 ReadModuleOffsetMap(M); 8845 8846 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8847 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8848 assert(I != M.IdentifierRemap.end() 8849 && "Invalid index into identifier index remap"); 8850 8851 return LocalID + I->second; 8852 } 8853 8854 MacroInfo *ASTReader::getMacro(MacroID ID) { 8855 if (ID == 0) 8856 return nullptr; 8857 8858 if (MacrosLoaded.empty()) { 8859 Error("no macro table in AST file"); 8860 return nullptr; 8861 } 8862 8863 ID -= NUM_PREDEF_MACRO_IDS; 8864 if (!MacrosLoaded[ID]) { 8865 GlobalMacroMapType::iterator I 8866 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8867 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8868 ModuleFile *M = I->second; 8869 unsigned Index = ID - M->BaseMacroID; 8870 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]); 8871 8872 if (DeserializationListener) 8873 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8874 MacrosLoaded[ID]); 8875 } 8876 8877 return MacrosLoaded[ID]; 8878 } 8879 8880 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8881 if (LocalID < NUM_PREDEF_MACRO_IDS) 8882 return LocalID; 8883 8884 if (!M.ModuleOffsetMap.empty()) 8885 ReadModuleOffsetMap(M); 8886 8887 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8888 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8889 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8890 8891 return LocalID + I->second; 8892 } 8893 8894 serialization::SubmoduleID 8895 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8896 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8897 return LocalID; 8898 8899 if (!M.ModuleOffsetMap.empty()) 8900 ReadModuleOffsetMap(M); 8901 8902 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8903 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8904 assert(I != M.SubmoduleRemap.end() 8905 && "Invalid index into submodule index remap"); 8906 8907 return LocalID + I->second; 8908 } 8909 8910 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8911 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8912 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8913 return nullptr; 8914 } 8915 8916 if (GlobalID > SubmodulesLoaded.size()) { 8917 Error("submodule ID out of range in AST file"); 8918 return nullptr; 8919 } 8920 8921 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8922 } 8923 8924 Module *ASTReader::getModule(unsigned ID) { 8925 return getSubmodule(ID); 8926 } 8927 8928 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) { 8929 ModuleFile *MF = getOwningModuleFile(D); 8930 return MF && MF->PCHHasObjectFile; 8931 } 8932 8933 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8934 if (ID & 1) { 8935 // It's a module, look it up by submodule ID. 8936 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8937 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8938 } else { 8939 // It's a prefix (preamble, PCH, ...). Look it up by index. 8940 unsigned IndexFromEnd = ID >> 1; 8941 assert(IndexFromEnd && "got reference to unknown module file"); 8942 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8943 } 8944 } 8945 8946 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8947 if (!F) 8948 return 1; 8949 8950 // For a file representing a module, use the submodule ID of the top-level 8951 // module as the file ID. For any other kind of file, the number of such 8952 // files loaded beforehand will be the same on reload. 8953 // FIXME: Is this true even if we have an explicit module file and a PCH? 8954 if (F->isModule()) 8955 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8956 8957 auto PCHModules = getModuleManager().pch_modules(); 8958 auto I = llvm::find(PCHModules, F); 8959 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8960 return (I - PCHModules.end()) << 1; 8961 } 8962 8963 llvm::Optional<ExternalASTSource::ASTSourceDescriptor> 8964 ASTReader::getSourceDescriptor(unsigned ID) { 8965 if (const Module *M = getSubmodule(ID)) 8966 return ExternalASTSource::ASTSourceDescriptor(*M); 8967 8968 // If there is only a single PCH, return it instead. 8969 // Chained PCH are not supported. 8970 const auto &PCHChain = ModuleMgr.pch_modules(); 8971 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8972 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8973 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8974 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8975 return ASTReader::ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8976 MF.Signature); 8977 } 8978 return None; 8979 } 8980 8981 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8982 auto I = DefinitionSource.find(FD); 8983 if (I == DefinitionSource.end()) 8984 return EK_ReplyHazy; 8985 return I->second ? EK_Never : EK_Always; 8986 } 8987 8988 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8989 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8990 } 8991 8992 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8993 if (ID == 0) 8994 return Selector(); 8995 8996 if (ID > SelectorsLoaded.size()) { 8997 Error("selector ID out of range in AST file"); 8998 return Selector(); 8999 } 9000 9001 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 9002 // Load this selector from the selector table. 9003 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 9004 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 9005 ModuleFile &M = *I->second; 9006 ASTSelectorLookupTrait Trait(*this, M); 9007 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 9008 SelectorsLoaded[ID - 1] = 9009 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 9010 if (DeserializationListener) 9011 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 9012 } 9013 9014 return SelectorsLoaded[ID - 1]; 9015 } 9016 9017 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 9018 return DecodeSelector(ID); 9019 } 9020 9021 uint32_t ASTReader::GetNumExternalSelectors() { 9022 // ID 0 (the null selector) is considered an external selector. 9023 return getTotalNumSelectors() + 1; 9024 } 9025 9026 serialization::SelectorID 9027 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 9028 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 9029 return LocalID; 9030 9031 if (!M.ModuleOffsetMap.empty()) 9032 ReadModuleOffsetMap(M); 9033 9034 ContinuousRangeMap<uint32_t, int, 2>::iterator I 9035 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 9036 assert(I != M.SelectorRemap.end() 9037 && "Invalid index into selector index remap"); 9038 9039 return LocalID + I->second; 9040 } 9041 9042 DeclarationName 9043 ASTReader::ReadDeclarationName(ModuleFile &F, 9044 const RecordData &Record, unsigned &Idx) { 9045 ASTContext &Context = getContext(); 9046 DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; 9047 switch (Kind) { 9048 case DeclarationName::Identifier: 9049 return DeclarationName(GetIdentifierInfo(F, Record, Idx)); 9050 9051 case DeclarationName::ObjCZeroArgSelector: 9052 case DeclarationName::ObjCOneArgSelector: 9053 case DeclarationName::ObjCMultiArgSelector: 9054 return DeclarationName(ReadSelector(F, Record, Idx)); 9055 9056 case DeclarationName::CXXConstructorName: 9057 return Context.DeclarationNames.getCXXConstructorName( 9058 Context.getCanonicalType(readType(F, Record, Idx))); 9059 9060 case DeclarationName::CXXDestructorName: 9061 return Context.DeclarationNames.getCXXDestructorName( 9062 Context.getCanonicalType(readType(F, Record, Idx))); 9063 9064 case DeclarationName::CXXDeductionGuideName: 9065 return Context.DeclarationNames.getCXXDeductionGuideName( 9066 ReadDeclAs<TemplateDecl>(F, Record, Idx)); 9067 9068 case DeclarationName::CXXConversionFunctionName: 9069 return Context.DeclarationNames.getCXXConversionFunctionName( 9070 Context.getCanonicalType(readType(F, Record, Idx))); 9071 9072 case DeclarationName::CXXOperatorName: 9073 return Context.DeclarationNames.getCXXOperatorName( 9074 (OverloadedOperatorKind)Record[Idx++]); 9075 9076 case DeclarationName::CXXLiteralOperatorName: 9077 return Context.DeclarationNames.getCXXLiteralOperatorName( 9078 GetIdentifierInfo(F, Record, Idx)); 9079 9080 case DeclarationName::CXXUsingDirective: 9081 return DeclarationName::getUsingDirectiveName(); 9082 } 9083 9084 llvm_unreachable("Invalid NameKind!"); 9085 } 9086 9087 void ASTReader::ReadDeclarationNameLoc(ModuleFile &F, 9088 DeclarationNameLoc &DNLoc, 9089 DeclarationName Name, 9090 const RecordData &Record, unsigned &Idx) { 9091 switch (Name.getNameKind()) { 9092 case DeclarationName::CXXConstructorName: 9093 case DeclarationName::CXXDestructorName: 9094 case DeclarationName::CXXConversionFunctionName: 9095 DNLoc.NamedType.TInfo = GetTypeSourceInfo(F, Record, Idx); 9096 break; 9097 9098 case DeclarationName::CXXOperatorName: 9099 DNLoc.CXXOperatorName.BeginOpNameLoc 9100 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 9101 DNLoc.CXXOperatorName.EndOpNameLoc 9102 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 9103 break; 9104 9105 case DeclarationName::CXXLiteralOperatorName: 9106 DNLoc.CXXLiteralOperatorName.OpNameLoc 9107 = ReadSourceLocation(F, Record, Idx).getRawEncoding(); 9108 break; 9109 9110 case DeclarationName::Identifier: 9111 case DeclarationName::ObjCZeroArgSelector: 9112 case DeclarationName::ObjCOneArgSelector: 9113 case DeclarationName::ObjCMultiArgSelector: 9114 case DeclarationName::CXXUsingDirective: 9115 case DeclarationName::CXXDeductionGuideName: 9116 break; 9117 } 9118 } 9119 9120 void ASTReader::ReadDeclarationNameInfo(ModuleFile &F, 9121 DeclarationNameInfo &NameInfo, 9122 const RecordData &Record, unsigned &Idx) { 9123 NameInfo.setName(ReadDeclarationName(F, Record, Idx)); 9124 NameInfo.setLoc(ReadSourceLocation(F, Record, Idx)); 9125 DeclarationNameLoc DNLoc; 9126 ReadDeclarationNameLoc(F, DNLoc, NameInfo.getName(), Record, Idx); 9127 NameInfo.setInfo(DNLoc); 9128 } 9129 9130 void ASTReader::ReadQualifierInfo(ModuleFile &F, QualifierInfo &Info, 9131 const RecordData &Record, unsigned &Idx) { 9132 Info.QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx); 9133 unsigned NumTPLists = Record[Idx++]; 9134 Info.NumTemplParamLists = NumTPLists; 9135 if (NumTPLists) { 9136 Info.TemplParamLists = 9137 new (getContext()) TemplateParameterList *[NumTPLists]; 9138 for (unsigned i = 0; i != NumTPLists; ++i) 9139 Info.TemplParamLists[i] = ReadTemplateParameterList(F, Record, Idx); 9140 } 9141 } 9142 9143 TemplateName 9144 ASTReader::ReadTemplateName(ModuleFile &F, const RecordData &Record, 9145 unsigned &Idx) { 9146 ASTContext &Context = getContext(); 9147 TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; 9148 switch (Kind) { 9149 case TemplateName::Template: 9150 return TemplateName(ReadDeclAs<TemplateDecl>(F, Record, Idx)); 9151 9152 case TemplateName::OverloadedTemplate: { 9153 unsigned size = Record[Idx++]; 9154 UnresolvedSet<8> Decls; 9155 while (size--) 9156 Decls.addDecl(ReadDeclAs<NamedDecl>(F, Record, Idx)); 9157 9158 return Context.getOverloadedTemplateName(Decls.begin(), Decls.end()); 9159 } 9160 9161 case TemplateName::AssumedTemplate: { 9162 DeclarationName Name = ReadDeclarationName(F, Record, Idx); 9163 return Context.getAssumedTemplateName(Name); 9164 } 9165 9166 case TemplateName::QualifiedTemplate: { 9167 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 9168 bool hasTemplKeyword = Record[Idx++]; 9169 TemplateDecl *Template = ReadDeclAs<TemplateDecl>(F, Record, Idx); 9170 return Context.getQualifiedTemplateName(NNS, hasTemplKeyword, Template); 9171 } 9172 9173 case TemplateName::DependentTemplate: { 9174 NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); 9175 if (Record[Idx++]) // isIdentifier 9176 return Context.getDependentTemplateName(NNS, 9177 GetIdentifierInfo(F, Record, 9178 Idx)); 9179 return Context.getDependentTemplateName(NNS, 9180 (OverloadedOperatorKind)Record[Idx++]); 9181 } 9182 9183 case TemplateName::SubstTemplateTemplateParm: { 9184 TemplateTemplateParmDecl *param 9185 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 9186 if (!param) return TemplateName(); 9187 TemplateName replacement = ReadTemplateName(F, Record, Idx); 9188 return Context.getSubstTemplateTemplateParm(param, replacement); 9189 } 9190 9191 case TemplateName::SubstTemplateTemplateParmPack: { 9192 TemplateTemplateParmDecl *Param 9193 = ReadDeclAs<TemplateTemplateParmDecl>(F, Record, Idx); 9194 if (!Param) 9195 return TemplateName(); 9196 9197 TemplateArgument ArgPack = ReadTemplateArgument(F, Record, Idx); 9198 if (ArgPack.getKind() != TemplateArgument::Pack) 9199 return TemplateName(); 9200 9201 return Context.getSubstTemplateTemplateParmPack(Param, ArgPack); 9202 } 9203 } 9204 9205 llvm_unreachable("Unhandled template name kind!"); 9206 } 9207 9208 TemplateArgument ASTReader::ReadTemplateArgument(ModuleFile &F, 9209 const RecordData &Record, 9210 unsigned &Idx, 9211 bool Canonicalize) { 9212 ASTContext &Context = getContext(); 9213 if (Canonicalize) { 9214 // The caller wants a canonical template argument. Sometimes the AST only 9215 // wants template arguments in canonical form (particularly as the template 9216 // argument lists of template specializations) so ensure we preserve that 9217 // canonical form across serialization. 9218 TemplateArgument Arg = ReadTemplateArgument(F, Record, Idx, false); 9219 return Context.getCanonicalTemplateArgument(Arg); 9220 } 9221 9222 TemplateArgument::ArgKind Kind = (TemplateArgument::ArgKind)Record[Idx++]; 9223 switch (Kind) { 9224 case TemplateArgument::Null: 9225 return TemplateArgument(); 9226 case TemplateArgument::Type: 9227 return TemplateArgument(readType(F, Record, Idx)); 9228 case TemplateArgument::Declaration: { 9229 ValueDecl *D = ReadDeclAs<ValueDecl>(F, Record, Idx); 9230 return TemplateArgument(D, readType(F, Record, Idx)); 9231 } 9232 case TemplateArgument::NullPtr: 9233 return TemplateArgument(readType(F, Record, Idx), /*isNullPtr*/true); 9234 case TemplateArgument::Integral: { 9235 llvm::APSInt Value = ReadAPSInt(Record, Idx); 9236 QualType T = readType(F, Record, Idx); 9237 return TemplateArgument(Context, Value, T); 9238 } 9239 case TemplateArgument::Template: 9240 return TemplateArgument(ReadTemplateName(F, Record, Idx)); 9241 case TemplateArgument::TemplateExpansion: { 9242 TemplateName Name = ReadTemplateName(F, Record, Idx); 9243 Optional<unsigned> NumTemplateExpansions; 9244 if (unsigned NumExpansions = Record[Idx++]) 9245 NumTemplateExpansions = NumExpansions - 1; 9246 return TemplateArgument(Name, NumTemplateExpansions); 9247 } 9248 case TemplateArgument::Expression: 9249 return TemplateArgument(ReadExpr(F)); 9250 case TemplateArgument::Pack: { 9251 unsigned NumArgs = Record[Idx++]; 9252 TemplateArgument *Args = new (Context) TemplateArgument[NumArgs]; 9253 for (unsigned I = 0; I != NumArgs; ++I) 9254 Args[I] = ReadTemplateArgument(F, Record, Idx); 9255 return TemplateArgument(llvm::makeArrayRef(Args, NumArgs)); 9256 } 9257 } 9258 9259 llvm_unreachable("Unhandled template argument kind!"); 9260 } 9261 9262 TemplateParameterList * 9263 ASTReader::ReadTemplateParameterList(ModuleFile &F, 9264 const RecordData &Record, unsigned &Idx) { 9265 SourceLocation TemplateLoc = ReadSourceLocation(F, Record, Idx); 9266 SourceLocation LAngleLoc = ReadSourceLocation(F, Record, Idx); 9267 SourceLocation RAngleLoc = ReadSourceLocation(F, Record, Idx); 9268 9269 unsigned NumParams = Record[Idx++]; 9270 SmallVector<NamedDecl *, 16> Params; 9271 Params.reserve(NumParams); 9272 while (NumParams--) 9273 Params.push_back(ReadDeclAs<NamedDecl>(F, Record, Idx)); 9274 9275 // TODO: Concepts 9276 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 9277 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, nullptr); 9278 return TemplateParams; 9279 } 9280 9281 void 9282 ASTReader:: 9283 ReadTemplateArgumentList(SmallVectorImpl<TemplateArgument> &TemplArgs, 9284 ModuleFile &F, const RecordData &Record, 9285 unsigned &Idx, bool Canonicalize) { 9286 unsigned NumTemplateArgs = Record[Idx++]; 9287 TemplArgs.reserve(NumTemplateArgs); 9288 while (NumTemplateArgs--) 9289 TemplArgs.push_back(ReadTemplateArgument(F, Record, Idx, Canonicalize)); 9290 } 9291 9292 /// Read a UnresolvedSet structure. 9293 void ASTReader::ReadUnresolvedSet(ModuleFile &F, LazyASTUnresolvedSet &Set, 9294 const RecordData &Record, unsigned &Idx) { 9295 unsigned NumDecls = Record[Idx++]; 9296 Set.reserve(getContext(), NumDecls); 9297 while (NumDecls--) { 9298 DeclID ID = ReadDeclID(F, Record, Idx); 9299 AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; 9300 Set.addLazyDecl(getContext(), ID, AS); 9301 } 9302 } 9303 9304 CXXBaseSpecifier 9305 ASTReader::ReadCXXBaseSpecifier(ModuleFile &F, 9306 const RecordData &Record, unsigned &Idx) { 9307 bool isVirtual = static_cast<bool>(Record[Idx++]); 9308 bool isBaseOfClass = static_cast<bool>(Record[Idx++]); 9309 AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); 9310 bool inheritConstructors = static_cast<bool>(Record[Idx++]); 9311 TypeSourceInfo *TInfo = GetTypeSourceInfo(F, Record, Idx); 9312 SourceRange Range = ReadSourceRange(F, Record, Idx); 9313 SourceLocation EllipsisLoc = ReadSourceLocation(F, Record, Idx); 9314 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 9315 EllipsisLoc); 9316 Result.setInheritConstructors(inheritConstructors); 9317 return Result; 9318 } 9319 9320 CXXCtorInitializer ** 9321 ASTReader::ReadCXXCtorInitializers(ModuleFile &F, const RecordData &Record, 9322 unsigned &Idx) { 9323 ASTContext &Context = getContext(); 9324 unsigned NumInitializers = Record[Idx++]; 9325 assert(NumInitializers && "wrote ctor initializers but have no inits"); 9326 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 9327 for (unsigned i = 0; i != NumInitializers; ++i) { 9328 TypeSourceInfo *TInfo = nullptr; 9329 bool IsBaseVirtual = false; 9330 FieldDecl *Member = nullptr; 9331 IndirectFieldDecl *IndirectMember = nullptr; 9332 9333 CtorInitializerType Type = (CtorInitializerType)Record[Idx++]; 9334 switch (Type) { 9335 case CTOR_INITIALIZER_BASE: 9336 TInfo = GetTypeSourceInfo(F, Record, Idx); 9337 IsBaseVirtual = Record[Idx++]; 9338 break; 9339 9340 case CTOR_INITIALIZER_DELEGATING: 9341 TInfo = GetTypeSourceInfo(F, Record, Idx); 9342 break; 9343 9344 case CTOR_INITIALIZER_MEMBER: 9345 Member = ReadDeclAs<FieldDecl>(F, Record, Idx); 9346 break; 9347 9348 case CTOR_INITIALIZER_INDIRECT_MEMBER: 9349 IndirectMember = ReadDeclAs<IndirectFieldDecl>(F, Record, Idx); 9350 break; 9351 } 9352 9353 SourceLocation MemberOrEllipsisLoc = ReadSourceLocation(F, Record, Idx); 9354 Expr *Init = ReadExpr(F); 9355 SourceLocation LParenLoc = ReadSourceLocation(F, Record, Idx); 9356 SourceLocation RParenLoc = ReadSourceLocation(F, Record, Idx); 9357 9358 CXXCtorInitializer *BOMInit; 9359 if (Type == CTOR_INITIALIZER_BASE) 9360 BOMInit = new (Context) 9361 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 9362 RParenLoc, MemberOrEllipsisLoc); 9363 else if (Type == CTOR_INITIALIZER_DELEGATING) 9364 BOMInit = new (Context) 9365 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 9366 else if (Member) 9367 BOMInit = new (Context) 9368 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 9369 Init, RParenLoc); 9370 else 9371 BOMInit = new (Context) 9372 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 9373 LParenLoc, Init, RParenLoc); 9374 9375 if (/*IsWritten*/Record[Idx++]) { 9376 unsigned SourceOrder = Record[Idx++]; 9377 BOMInit->setSourceOrder(SourceOrder); 9378 } 9379 9380 CtorInitializers[i] = BOMInit; 9381 } 9382 9383 return CtorInitializers; 9384 } 9385 9386 NestedNameSpecifier * 9387 ASTReader::ReadNestedNameSpecifier(ModuleFile &F, 9388 const RecordData &Record, unsigned &Idx) { 9389 ASTContext &Context = getContext(); 9390 unsigned N = Record[Idx++]; 9391 NestedNameSpecifier *NNS = nullptr, *Prev = nullptr; 9392 for (unsigned I = 0; I != N; ++I) { 9393 NestedNameSpecifier::SpecifierKind Kind 9394 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 9395 switch (Kind) { 9396 case NestedNameSpecifier::Identifier: { 9397 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 9398 NNS = NestedNameSpecifier::Create(Context, Prev, II); 9399 break; 9400 } 9401 9402 case NestedNameSpecifier::Namespace: { 9403 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 9404 NNS = NestedNameSpecifier::Create(Context, Prev, NS); 9405 break; 9406 } 9407 9408 case NestedNameSpecifier::NamespaceAlias: { 9409 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 9410 NNS = NestedNameSpecifier::Create(Context, Prev, Alias); 9411 break; 9412 } 9413 9414 case NestedNameSpecifier::TypeSpec: 9415 case NestedNameSpecifier::TypeSpecWithTemplate: { 9416 const Type *T = readType(F, Record, Idx).getTypePtrOrNull(); 9417 if (!T) 9418 return nullptr; 9419 9420 bool Template = Record[Idx++]; 9421 NNS = NestedNameSpecifier::Create(Context, Prev, Template, T); 9422 break; 9423 } 9424 9425 case NestedNameSpecifier::Global: 9426 NNS = NestedNameSpecifier::GlobalSpecifier(Context); 9427 // No associated value, and there can't be a prefix. 9428 break; 9429 9430 case NestedNameSpecifier::Super: { 9431 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx); 9432 NNS = NestedNameSpecifier::SuperSpecifier(Context, RD); 9433 break; 9434 } 9435 } 9436 Prev = NNS; 9437 } 9438 return NNS; 9439 } 9440 9441 NestedNameSpecifierLoc 9442 ASTReader::ReadNestedNameSpecifierLoc(ModuleFile &F, const RecordData &Record, 9443 unsigned &Idx) { 9444 ASTContext &Context = getContext(); 9445 unsigned N = Record[Idx++]; 9446 NestedNameSpecifierLocBuilder Builder; 9447 for (unsigned I = 0; I != N; ++I) { 9448 NestedNameSpecifier::SpecifierKind Kind 9449 = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; 9450 switch (Kind) { 9451 case NestedNameSpecifier::Identifier: { 9452 IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); 9453 SourceRange Range = ReadSourceRange(F, Record, Idx); 9454 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 9455 break; 9456 } 9457 9458 case NestedNameSpecifier::Namespace: { 9459 NamespaceDecl *NS = ReadDeclAs<NamespaceDecl>(F, Record, Idx); 9460 SourceRange Range = ReadSourceRange(F, Record, Idx); 9461 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 9462 break; 9463 } 9464 9465 case NestedNameSpecifier::NamespaceAlias: { 9466 NamespaceAliasDecl *Alias =ReadDeclAs<NamespaceAliasDecl>(F, Record, Idx); 9467 SourceRange Range = ReadSourceRange(F, Record, Idx); 9468 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 9469 break; 9470 } 9471 9472 case NestedNameSpecifier::TypeSpec: 9473 case NestedNameSpecifier::TypeSpecWithTemplate: { 9474 bool Template = Record[Idx++]; 9475 TypeSourceInfo *T = GetTypeSourceInfo(F, Record, Idx); 9476 if (!T) 9477 return NestedNameSpecifierLoc(); 9478 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 9479 9480 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 9481 Builder.Extend(Context, 9482 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 9483 T->getTypeLoc(), ColonColonLoc); 9484 break; 9485 } 9486 9487 case NestedNameSpecifier::Global: { 9488 SourceLocation ColonColonLoc = ReadSourceLocation(F, Record, Idx); 9489 Builder.MakeGlobal(Context, ColonColonLoc); 9490 break; 9491 } 9492 9493 case NestedNameSpecifier::Super: { 9494 CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(F, Record, Idx); 9495 SourceRange Range = ReadSourceRange(F, Record, Idx); 9496 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 9497 break; 9498 } 9499 } 9500 } 9501 9502 return Builder.getWithLocInContext(Context); 9503 } 9504 9505 SourceRange 9506 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 9507 unsigned &Idx) { 9508 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 9509 SourceLocation end = ReadSourceLocation(F, Record, Idx); 9510 return SourceRange(beg, end); 9511 } 9512 9513 static FixedPointSemantics 9514 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record, 9515 unsigned &Idx) { 9516 unsigned Width = Record[Idx++]; 9517 unsigned Scale = Record[Idx++]; 9518 uint64_t Tmp = Record[Idx++]; 9519 bool IsSigned = Tmp & 0x1; 9520 bool IsSaturated = Tmp & 0x2; 9521 bool HasUnsignedPadding = Tmp & 0x4; 9522 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated, 9523 HasUnsignedPadding); 9524 } 9525 9526 APValue ASTReader::ReadAPValue(const RecordData &Record, unsigned &Idx) { 9527 unsigned Kind = Record[Idx++]; 9528 switch (Kind) { 9529 case APValue::None: 9530 return APValue(); 9531 case APValue::Indeterminate: 9532 return APValue::IndeterminateValue(); 9533 case APValue::Int: 9534 return APValue(ReadAPSInt(Record, Idx)); 9535 case APValue::Float: { 9536 const llvm::fltSemantics &FloatSema = llvm::APFloatBase::EnumToSemantics( 9537 static_cast<llvm::APFloatBase::Semantics>(Record[Idx++])); 9538 return APValue(ReadAPFloat(Record, FloatSema, Idx)); 9539 } 9540 case APValue::FixedPoint: { 9541 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx); 9542 return APValue(APFixedPoint(ReadAPInt(Record, Idx), FPSema)); 9543 } 9544 case APValue::ComplexInt: { 9545 llvm::APSInt First = ReadAPSInt(Record, Idx); 9546 return APValue(std::move(First), ReadAPSInt(Record, Idx)); 9547 } 9548 case APValue::ComplexFloat: { 9549 const llvm::fltSemantics &FloatSema1 = llvm::APFloatBase::EnumToSemantics( 9550 static_cast<llvm::APFloatBase::Semantics>(Record[Idx++])); 9551 llvm::APFloat First = ReadAPFloat(Record, FloatSema1, Idx); 9552 const llvm::fltSemantics &FloatSema2 = llvm::APFloatBase::EnumToSemantics( 9553 static_cast<llvm::APFloatBase::Semantics>(Record[Idx++])); 9554 return APValue(std::move(First), ReadAPFloat(Record, FloatSema2, Idx)); 9555 } 9556 case APValue::LValue: 9557 case APValue::Vector: 9558 case APValue::Array: 9559 case APValue::Struct: 9560 case APValue::Union: 9561 case APValue::MemberPointer: 9562 case APValue::AddrLabelDiff: 9563 // TODO : Handle all these APValue::ValueKind. 9564 return APValue(); 9565 } 9566 llvm_unreachable("Invalid APValue::ValueKind"); 9567 } 9568 9569 /// Read an integral value 9570 llvm::APInt ASTReader::ReadAPInt(const RecordData &Record, unsigned &Idx) { 9571 unsigned BitWidth = Record[Idx++]; 9572 unsigned NumWords = llvm::APInt::getNumWords(BitWidth); 9573 llvm::APInt Result(BitWidth, NumWords, &Record[Idx]); 9574 Idx += NumWords; 9575 return Result; 9576 } 9577 9578 /// Read a signed integral value 9579 llvm::APSInt ASTReader::ReadAPSInt(const RecordData &Record, unsigned &Idx) { 9580 bool isUnsigned = Record[Idx++]; 9581 return llvm::APSInt(ReadAPInt(Record, Idx), isUnsigned); 9582 } 9583 9584 /// Read a floating-point value 9585 llvm::APFloat ASTReader::ReadAPFloat(const RecordData &Record, 9586 const llvm::fltSemantics &Sem, 9587 unsigned &Idx) { 9588 return llvm::APFloat(Sem, ReadAPInt(Record, Idx)); 9589 } 9590 9591 // Read a string 9592 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9593 unsigned Len = Record[Idx++]; 9594 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9595 Idx += Len; 9596 return Result; 9597 } 9598 9599 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9600 unsigned &Idx) { 9601 std::string Filename = ReadString(Record, Idx); 9602 ResolveImportedPath(F, Filename); 9603 return Filename; 9604 } 9605 9606 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9607 const RecordData &Record, unsigned &Idx) { 9608 std::string Filename = ReadString(Record, Idx); 9609 if (!BaseDirectory.empty()) 9610 ResolveImportedPath(Filename, BaseDirectory); 9611 return Filename; 9612 } 9613 9614 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9615 unsigned &Idx) { 9616 unsigned Major = Record[Idx++]; 9617 unsigned Minor = Record[Idx++]; 9618 unsigned Subminor = Record[Idx++]; 9619 if (Minor == 0) 9620 return VersionTuple(Major); 9621 if (Subminor == 0) 9622 return VersionTuple(Major, Minor - 1); 9623 return VersionTuple(Major, Minor - 1, Subminor - 1); 9624 } 9625 9626 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9627 const RecordData &Record, 9628 unsigned &Idx) { 9629 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9630 return CXXTemporary::Create(getContext(), Decl); 9631 } 9632 9633 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9634 return Diag(CurrentImportLoc, DiagID); 9635 } 9636 9637 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9638 return Diags.Report(Loc, DiagID); 9639 } 9640 9641 /// Retrieve the identifier table associated with the 9642 /// preprocessor. 9643 IdentifierTable &ASTReader::getIdentifierTable() { 9644 return PP.getIdentifierTable(); 9645 } 9646 9647 /// Record that the given ID maps to the given switch-case 9648 /// statement. 9649 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9650 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9651 "Already have a SwitchCase with this ID"); 9652 (*CurrSwitchCaseStmts)[ID] = SC; 9653 } 9654 9655 /// Retrieve the switch-case statement with the given ID. 9656 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9657 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9658 return (*CurrSwitchCaseStmts)[ID]; 9659 } 9660 9661 void ASTReader::ClearSwitchCaseIDs() { 9662 CurrSwitchCaseStmts->clear(); 9663 } 9664 9665 void ASTReader::ReadComments() { 9666 ASTContext &Context = getContext(); 9667 std::vector<RawComment *> Comments; 9668 for (SmallVectorImpl<std::pair<BitstreamCursor, 9669 serialization::ModuleFile *>>::iterator 9670 I = CommentsCursors.begin(), 9671 E = CommentsCursors.end(); 9672 I != E; ++I) { 9673 Comments.clear(); 9674 BitstreamCursor &Cursor = I->first; 9675 serialization::ModuleFile &F = *I->second; 9676 SavedStreamPosition SavedPosition(Cursor); 9677 9678 RecordData Record; 9679 while (true) { 9680 Expected<llvm::BitstreamEntry> MaybeEntry = 9681 Cursor.advanceSkippingSubblocks( 9682 BitstreamCursor::AF_DontPopBlockAtEnd); 9683 if (!MaybeEntry) { 9684 Error(MaybeEntry.takeError()); 9685 return; 9686 } 9687 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9688 9689 switch (Entry.Kind) { 9690 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9691 case llvm::BitstreamEntry::Error: 9692 Error("malformed block record in AST file"); 9693 return; 9694 case llvm::BitstreamEntry::EndBlock: 9695 goto NextCursor; 9696 case llvm::BitstreamEntry::Record: 9697 // The interesting case. 9698 break; 9699 } 9700 9701 // Read a record. 9702 Record.clear(); 9703 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9704 if (!MaybeComment) { 9705 Error(MaybeComment.takeError()); 9706 return; 9707 } 9708 switch ((CommentRecordTypes)MaybeComment.get()) { 9709 case COMMENTS_RAW_COMMENT: { 9710 unsigned Idx = 0; 9711 SourceRange SR = ReadSourceRange(F, Record, Idx); 9712 RawComment::CommentKind Kind = 9713 (RawComment::CommentKind) Record[Idx++]; 9714 bool IsTrailingComment = Record[Idx++]; 9715 bool IsAlmostTrailingComment = Record[Idx++]; 9716 Comments.push_back(new (Context) RawComment( 9717 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9718 break; 9719 } 9720 } 9721 } 9722 NextCursor: 9723 // De-serialized SourceLocations get negative FileIDs for other modules, 9724 // potentially invalidating the original order. Sort it again. 9725 llvm::sort(Comments, BeforeThanCompare<RawComment>(SourceMgr)); 9726 Context.Comments.addDeserializedComments(Comments); 9727 } 9728 } 9729 9730 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9731 bool IncludeSystem, bool Complain, 9732 llvm::function_ref<void(const serialization::InputFile &IF, 9733 bool isSystem)> Visitor) { 9734 unsigned NumUserInputs = MF.NumUserInputFiles; 9735 unsigned NumInputs = MF.InputFilesLoaded.size(); 9736 assert(NumUserInputs <= NumInputs); 9737 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9738 for (unsigned I = 0; I < N; ++I) { 9739 bool IsSystem = I >= NumUserInputs; 9740 InputFile IF = getInputFile(MF, I+1, Complain); 9741 Visitor(IF, IsSystem); 9742 } 9743 } 9744 9745 void ASTReader::visitTopLevelModuleMaps( 9746 serialization::ModuleFile &MF, 9747 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9748 unsigned NumInputs = MF.InputFilesLoaded.size(); 9749 for (unsigned I = 0; I < NumInputs; ++I) { 9750 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9751 if (IFI.TopLevelModuleMap) 9752 // FIXME: This unnecessarily re-reads the InputFileInfo. 9753 if (auto *FE = getInputFile(MF, I + 1).getFile()) 9754 Visitor(FE); 9755 } 9756 } 9757 9758 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9759 // If we know the owning module, use it. 9760 if (Module *M = D->getImportedOwningModule()) 9761 return M->getFullModuleName(); 9762 9763 // Otherwise, use the name of the top-level module the decl is within. 9764 if (ModuleFile *M = getOwningModuleFile(D)) 9765 return M->ModuleName; 9766 9767 // Not from a module. 9768 return {}; 9769 } 9770 9771 void ASTReader::finishPendingActions() { 9772 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9773 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9774 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9775 !PendingUpdateRecords.empty()) { 9776 // If any identifiers with corresponding top-level declarations have 9777 // been loaded, load those declarations now. 9778 using TopLevelDeclsMap = 9779 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9780 TopLevelDeclsMap TopLevelDecls; 9781 9782 while (!PendingIdentifierInfos.empty()) { 9783 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9784 SmallVector<uint32_t, 4> DeclIDs = 9785 std::move(PendingIdentifierInfos.back().second); 9786 PendingIdentifierInfos.pop_back(); 9787 9788 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9789 } 9790 9791 // Load each function type that we deferred loading because it was a 9792 // deduced type that might refer to a local type declared within itself. 9793 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9794 auto *FD = PendingFunctionTypes[I].first; 9795 FD->setType(GetType(PendingFunctionTypes[I].second)); 9796 9797 // If we gave a function a deduced return type, remember that we need to 9798 // propagate that along the redeclaration chain. 9799 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9800 if (DT && DT->isDeduced()) 9801 PendingDeducedTypeUpdates.insert( 9802 {FD->getCanonicalDecl(), FD->getReturnType()}); 9803 } 9804 PendingFunctionTypes.clear(); 9805 9806 // For each decl chain that we wanted to complete while deserializing, mark 9807 // it as "still needs to be completed". 9808 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9809 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9810 } 9811 PendingIncompleteDeclChains.clear(); 9812 9813 // Load pending declaration chains. 9814 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9815 loadPendingDeclChain(PendingDeclChains[I].first, 9816 PendingDeclChains[I].second); 9817 PendingDeclChains.clear(); 9818 9819 // Make the most recent of the top-level declarations visible. 9820 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9821 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9822 IdentifierInfo *II = TLD->first; 9823 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9824 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9825 } 9826 } 9827 9828 // Load any pending macro definitions. 9829 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9830 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9831 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9832 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9833 // Initialize the macro history from chained-PCHs ahead of module imports. 9834 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9835 ++IDIdx) { 9836 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9837 if (!Info.M->isModule()) 9838 resolvePendingMacro(II, Info); 9839 } 9840 // Handle module imports. 9841 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9842 ++IDIdx) { 9843 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9844 if (Info.M->isModule()) 9845 resolvePendingMacro(II, Info); 9846 } 9847 } 9848 PendingMacroIDs.clear(); 9849 9850 // Wire up the DeclContexts for Decls that we delayed setting until 9851 // recursive loading is completed. 9852 while (!PendingDeclContextInfos.empty()) { 9853 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9854 PendingDeclContextInfos.pop_front(); 9855 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9856 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9857 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9858 } 9859 9860 // Perform any pending declaration updates. 9861 while (!PendingUpdateRecords.empty()) { 9862 auto Update = PendingUpdateRecords.pop_back_val(); 9863 ReadingKindTracker ReadingKind(Read_Decl, *this); 9864 loadDeclUpdateRecords(Update); 9865 } 9866 } 9867 9868 // At this point, all update records for loaded decls are in place, so any 9869 // fake class definitions should have become real. 9870 assert(PendingFakeDefinitionData.empty() && 9871 "faked up a class definition but never saw the real one"); 9872 9873 // If we deserialized any C++ or Objective-C class definitions, any 9874 // Objective-C protocol definitions, or any redeclarable templates, make sure 9875 // that all redeclarations point to the definitions. Note that this can only 9876 // happen now, after the redeclaration chains have been fully wired. 9877 for (Decl *D : PendingDefinitions) { 9878 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9879 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9880 // Make sure that the TagType points at the definition. 9881 const_cast<TagType*>(TagT)->decl = TD; 9882 } 9883 9884 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9885 for (auto *R = getMostRecentExistingDecl(RD); R; 9886 R = R->getPreviousDecl()) { 9887 assert((R == D) == 9888 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9889 "declaration thinks it's the definition but it isn't"); 9890 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9891 } 9892 } 9893 9894 continue; 9895 } 9896 9897 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9898 // Make sure that the ObjCInterfaceType points at the definition. 9899 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9900 ->Decl = ID; 9901 9902 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9903 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9904 9905 continue; 9906 } 9907 9908 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9909 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9910 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9911 9912 continue; 9913 } 9914 9915 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9916 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9917 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9918 } 9919 PendingDefinitions.clear(); 9920 9921 // Load the bodies of any functions or methods we've encountered. We do 9922 // this now (delayed) so that we can be sure that the declaration chains 9923 // have been fully wired up (hasBody relies on this). 9924 // FIXME: We shouldn't require complete redeclaration chains here. 9925 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9926 PBEnd = PendingBodies.end(); 9927 PB != PBEnd; ++PB) { 9928 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9929 // For a function defined inline within a class template, force the 9930 // canonical definition to be the one inside the canonical definition of 9931 // the template. This ensures that we instantiate from a correct view 9932 // of the template. 9933 // 9934 // Sadly we can't do this more generally: we can't be sure that all 9935 // copies of an arbitrary class definition will have the same members 9936 // defined (eg, some member functions may not be instantiated, and some 9937 // special members may or may not have been implicitly defined). 9938 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9939 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9940 continue; 9941 9942 // FIXME: Check for =delete/=default? 9943 // FIXME: Complain about ODR violations here? 9944 const FunctionDecl *Defn = nullptr; 9945 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9946 FD->setLazyBody(PB->second); 9947 } else { 9948 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9949 mergeDefinitionVisibility(NonConstDefn, FD); 9950 9951 if (!FD->isLateTemplateParsed() && 9952 !NonConstDefn->isLateTemplateParsed() && 9953 FD->getODRHash() != NonConstDefn->getODRHash()) { 9954 if (!isa<CXXMethodDecl>(FD)) { 9955 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9956 } else if (FD->getLexicalParent()->isFileContext() && 9957 NonConstDefn->getLexicalParent()->isFileContext()) { 9958 // Only diagnose out-of-line method definitions. If they are 9959 // in class definitions, then an error will be generated when 9960 // processing the class bodies. 9961 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9962 } 9963 } 9964 } 9965 continue; 9966 } 9967 9968 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9969 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9970 MD->setLazyBody(PB->second); 9971 } 9972 PendingBodies.clear(); 9973 9974 // Do some cleanup. 9975 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9976 getContext().deduplicateMergedDefinitonsFor(ND); 9977 PendingMergedDefinitionsToDeduplicate.clear(); 9978 } 9979 9980 void ASTReader::diagnoseOdrViolations() { 9981 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9982 PendingFunctionOdrMergeFailures.empty() && 9983 PendingEnumOdrMergeFailures.empty()) 9984 return; 9985 9986 // Trigger the import of the full definition of each class that had any 9987 // odr-merging problems, so we can produce better diagnostics for them. 9988 // These updates may in turn find and diagnose some ODR failures, so take 9989 // ownership of the set first. 9990 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9991 PendingOdrMergeFailures.clear(); 9992 for (auto &Merge : OdrMergeFailures) { 9993 Merge.first->buildLookup(); 9994 Merge.first->decls_begin(); 9995 Merge.first->bases_begin(); 9996 Merge.first->vbases_begin(); 9997 for (auto &RecordPair : Merge.second) { 9998 auto *RD = RecordPair.first; 9999 RD->decls_begin(); 10000 RD->bases_begin(); 10001 RD->vbases_begin(); 10002 } 10003 } 10004 10005 // Trigger the import of functions. 10006 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 10007 PendingFunctionOdrMergeFailures.clear(); 10008 for (auto &Merge : FunctionOdrMergeFailures) { 10009 Merge.first->buildLookup(); 10010 Merge.first->decls_begin(); 10011 Merge.first->getBody(); 10012 for (auto &FD : Merge.second) { 10013 FD->buildLookup(); 10014 FD->decls_begin(); 10015 FD->getBody(); 10016 } 10017 } 10018 10019 // Trigger the import of enums. 10020 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 10021 PendingEnumOdrMergeFailures.clear(); 10022 for (auto &Merge : EnumOdrMergeFailures) { 10023 Merge.first->decls_begin(); 10024 for (auto &Enum : Merge.second) { 10025 Enum->decls_begin(); 10026 } 10027 } 10028 10029 // For each declaration from a merged context, check that the canonical 10030 // definition of that context also contains a declaration of the same 10031 // entity. 10032 // 10033 // Caution: this loop does things that might invalidate iterators into 10034 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 10035 while (!PendingOdrMergeChecks.empty()) { 10036 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 10037 10038 // FIXME: Skip over implicit declarations for now. This matters for things 10039 // like implicitly-declared special member functions. This isn't entirely 10040 // correct; we can end up with multiple unmerged declarations of the same 10041 // implicit entity. 10042 if (D->isImplicit()) 10043 continue; 10044 10045 DeclContext *CanonDef = D->getDeclContext(); 10046 10047 bool Found = false; 10048 const Decl *DCanon = D->getCanonicalDecl(); 10049 10050 for (auto RI : D->redecls()) { 10051 if (RI->getLexicalDeclContext() == CanonDef) { 10052 Found = true; 10053 break; 10054 } 10055 } 10056 if (Found) 10057 continue; 10058 10059 // Quick check failed, time to do the slow thing. Note, we can't just 10060 // look up the name of D in CanonDef here, because the member that is 10061 // in CanonDef might not be found by name lookup (it might have been 10062 // replaced by a more recent declaration in the lookup table), and we 10063 // can't necessarily find it in the redeclaration chain because it might 10064 // be merely mergeable, not redeclarable. 10065 llvm::SmallVector<const NamedDecl*, 4> Candidates; 10066 for (auto *CanonMember : CanonDef->decls()) { 10067 if (CanonMember->getCanonicalDecl() == DCanon) { 10068 // This can happen if the declaration is merely mergeable and not 10069 // actually redeclarable (we looked for redeclarations earlier). 10070 // 10071 // FIXME: We should be able to detect this more efficiently, without 10072 // pulling in all of the members of CanonDef. 10073 Found = true; 10074 break; 10075 } 10076 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 10077 if (ND->getDeclName() == D->getDeclName()) 10078 Candidates.push_back(ND); 10079 } 10080 10081 if (!Found) { 10082 // The AST doesn't like TagDecls becoming invalid after they've been 10083 // completed. We only really need to mark FieldDecls as invalid here. 10084 if (!isa<TagDecl>(D)) 10085 D->setInvalidDecl(); 10086 10087 // Ensure we don't accidentally recursively enter deserialization while 10088 // we're producing our diagnostic. 10089 Deserializing RecursionGuard(this); 10090 10091 std::string CanonDefModule = 10092 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 10093 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 10094 << D << getOwningModuleNameForDiagnostic(D) 10095 << CanonDef << CanonDefModule.empty() << CanonDefModule; 10096 10097 if (Candidates.empty()) 10098 Diag(cast<Decl>(CanonDef)->getLocation(), 10099 diag::note_module_odr_violation_no_possible_decls) << D; 10100 else { 10101 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 10102 Diag(Candidates[I]->getLocation(), 10103 diag::note_module_odr_violation_possible_decl) 10104 << Candidates[I]; 10105 } 10106 10107 DiagnosedOdrMergeFailures.insert(CanonDef); 10108 } 10109 } 10110 10111 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 10112 EnumOdrMergeFailures.empty()) 10113 return; 10114 10115 // Ensure we don't accidentally recursively enter deserialization while 10116 // we're producing our diagnostics. 10117 Deserializing RecursionGuard(this); 10118 10119 // Common code for hashing helpers. 10120 ODRHash Hash; 10121 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 10122 Hash.clear(); 10123 Hash.AddQualType(Ty); 10124 return Hash.CalculateHash(); 10125 }; 10126 10127 auto ComputeODRHash = [&Hash](const Stmt *S) { 10128 assert(S); 10129 Hash.clear(); 10130 Hash.AddStmt(S); 10131 return Hash.CalculateHash(); 10132 }; 10133 10134 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 10135 assert(D); 10136 Hash.clear(); 10137 Hash.AddSubDecl(D); 10138 return Hash.CalculateHash(); 10139 }; 10140 10141 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 10142 Hash.clear(); 10143 Hash.AddTemplateArgument(TA); 10144 return Hash.CalculateHash(); 10145 }; 10146 10147 auto ComputeTemplateParameterListODRHash = 10148 [&Hash](const TemplateParameterList *TPL) { 10149 assert(TPL); 10150 Hash.clear(); 10151 Hash.AddTemplateParameterList(TPL); 10152 return Hash.CalculateHash(); 10153 }; 10154 10155 // Issue any pending ODR-failure diagnostics. 10156 for (auto &Merge : OdrMergeFailures) { 10157 // If we've already pointed out a specific problem with this class, don't 10158 // bother issuing a general "something's different" diagnostic. 10159 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10160 continue; 10161 10162 bool Diagnosed = false; 10163 CXXRecordDecl *FirstRecord = Merge.first; 10164 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10165 for (auto &RecordPair : Merge.second) { 10166 CXXRecordDecl *SecondRecord = RecordPair.first; 10167 // Multiple different declarations got merged together; tell the user 10168 // where they came from. 10169 if (FirstRecord == SecondRecord) 10170 continue; 10171 10172 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10173 10174 auto *FirstDD = FirstRecord->DefinitionData; 10175 auto *SecondDD = RecordPair.second; 10176 10177 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10178 10179 // Diagnostics from DefinitionData are emitted here. 10180 if (FirstDD != SecondDD) { 10181 enum ODRDefinitionDataDifference { 10182 NumBases, 10183 NumVBases, 10184 BaseType, 10185 BaseVirtual, 10186 BaseAccess, 10187 }; 10188 auto ODRDiagError = [FirstRecord, &FirstModule, 10189 this](SourceLocation Loc, SourceRange Range, 10190 ODRDefinitionDataDifference DiffType) { 10191 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10192 << FirstRecord << FirstModule.empty() << FirstModule << Range 10193 << DiffType; 10194 }; 10195 auto ODRDiagNote = [&SecondModule, 10196 this](SourceLocation Loc, SourceRange Range, 10197 ODRDefinitionDataDifference DiffType) { 10198 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10199 << SecondModule << Range << DiffType; 10200 }; 10201 10202 unsigned FirstNumBases = FirstDD->NumBases; 10203 unsigned FirstNumVBases = FirstDD->NumVBases; 10204 unsigned SecondNumBases = SecondDD->NumBases; 10205 unsigned SecondNumVBases = SecondDD->NumVBases; 10206 10207 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10208 unsigned NumBases = DD->NumBases; 10209 if (NumBases == 0) return SourceRange(); 10210 auto bases = DD->bases(); 10211 return SourceRange(bases[0].getBeginLoc(), 10212 bases[NumBases - 1].getEndLoc()); 10213 }; 10214 10215 if (FirstNumBases != SecondNumBases) { 10216 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10217 NumBases) 10218 << FirstNumBases; 10219 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10220 NumBases) 10221 << SecondNumBases; 10222 Diagnosed = true; 10223 break; 10224 } 10225 10226 if (FirstNumVBases != SecondNumVBases) { 10227 ODRDiagError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10228 NumVBases) 10229 << FirstNumVBases; 10230 ODRDiagNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10231 NumVBases) 10232 << SecondNumVBases; 10233 Diagnosed = true; 10234 break; 10235 } 10236 10237 auto FirstBases = FirstDD->bases(); 10238 auto SecondBases = SecondDD->bases(); 10239 unsigned i = 0; 10240 for (i = 0; i < FirstNumBases; ++i) { 10241 auto FirstBase = FirstBases[i]; 10242 auto SecondBase = SecondBases[i]; 10243 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10244 ComputeQualTypeODRHash(SecondBase.getType())) { 10245 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(), 10246 BaseType) 10247 << (i + 1) << FirstBase.getType(); 10248 ODRDiagNote(SecondRecord->getLocation(), 10249 SecondBase.getSourceRange(), BaseType) 10250 << (i + 1) << SecondBase.getType(); 10251 break; 10252 } 10253 10254 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10255 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(), 10256 BaseVirtual) 10257 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10258 ODRDiagNote(SecondRecord->getLocation(), 10259 SecondBase.getSourceRange(), BaseVirtual) 10260 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10261 break; 10262 } 10263 10264 if (FirstBase.getAccessSpecifierAsWritten() != 10265 SecondBase.getAccessSpecifierAsWritten()) { 10266 ODRDiagError(FirstRecord->getLocation(), FirstBase.getSourceRange(), 10267 BaseAccess) 10268 << (i + 1) << FirstBase.getType() 10269 << (int)FirstBase.getAccessSpecifierAsWritten(); 10270 ODRDiagNote(SecondRecord->getLocation(), 10271 SecondBase.getSourceRange(), BaseAccess) 10272 << (i + 1) << SecondBase.getType() 10273 << (int)SecondBase.getAccessSpecifierAsWritten(); 10274 break; 10275 } 10276 } 10277 10278 if (i != FirstNumBases) { 10279 Diagnosed = true; 10280 break; 10281 } 10282 } 10283 10284 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 10285 10286 const ClassTemplateDecl *FirstTemplate = 10287 FirstRecord->getDescribedClassTemplate(); 10288 const ClassTemplateDecl *SecondTemplate = 10289 SecondRecord->getDescribedClassTemplate(); 10290 10291 assert(!FirstTemplate == !SecondTemplate && 10292 "Both pointers should be null or non-null"); 10293 10294 enum ODRTemplateDifference { 10295 ParamEmptyName, 10296 ParamName, 10297 ParamSingleDefaultArgument, 10298 ParamDifferentDefaultArgument, 10299 }; 10300 10301 if (FirstTemplate && SecondTemplate) { 10302 DeclHashes FirstTemplateHashes; 10303 DeclHashes SecondTemplateHashes; 10304 10305 auto PopulateTemplateParameterHashs = 10306 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10307 const ClassTemplateDecl *TD) { 10308 for (auto *D : TD->getTemplateParameters()->asArray()) { 10309 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10310 } 10311 }; 10312 10313 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10314 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10315 10316 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10317 "Number of template parameters should be equal."); 10318 10319 auto FirstIt = FirstTemplateHashes.begin(); 10320 auto FirstEnd = FirstTemplateHashes.end(); 10321 auto SecondIt = SecondTemplateHashes.begin(); 10322 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10323 if (FirstIt->second == SecondIt->second) 10324 continue; 10325 10326 auto ODRDiagError = [FirstRecord, &FirstModule, 10327 this](SourceLocation Loc, SourceRange Range, 10328 ODRTemplateDifference DiffType) { 10329 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10330 << FirstRecord << FirstModule.empty() << FirstModule << Range 10331 << DiffType; 10332 }; 10333 auto ODRDiagNote = [&SecondModule, 10334 this](SourceLocation Loc, SourceRange Range, 10335 ODRTemplateDifference DiffType) { 10336 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10337 << SecondModule << Range << DiffType; 10338 }; 10339 10340 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10341 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10342 10343 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10344 "Parameter Decl's should be the same kind."); 10345 10346 DeclarationName FirstName = FirstDecl->getDeclName(); 10347 DeclarationName SecondName = SecondDecl->getDeclName(); 10348 10349 if (FirstName != SecondName) { 10350 const bool FirstNameEmpty = 10351 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10352 const bool SecondNameEmpty = 10353 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10354 assert((!FirstNameEmpty || !SecondNameEmpty) && 10355 "Both template parameters cannot be unnamed."); 10356 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 10357 FirstNameEmpty ? ParamEmptyName : ParamName) 10358 << FirstName; 10359 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 10360 SecondNameEmpty ? ParamEmptyName : ParamName) 10361 << SecondName; 10362 break; 10363 } 10364 10365 switch (FirstDecl->getKind()) { 10366 default: 10367 llvm_unreachable("Invalid template parameter type."); 10368 case Decl::TemplateTypeParm: { 10369 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10370 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10371 const bool HasFirstDefaultArgument = 10372 FirstParam->hasDefaultArgument() && 10373 !FirstParam->defaultArgumentWasInherited(); 10374 const bool HasSecondDefaultArgument = 10375 SecondParam->hasDefaultArgument() && 10376 !SecondParam->defaultArgumentWasInherited(); 10377 10378 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10379 ODRDiagError(FirstDecl->getLocation(), 10380 FirstDecl->getSourceRange(), 10381 ParamSingleDefaultArgument) 10382 << HasFirstDefaultArgument; 10383 ODRDiagNote(SecondDecl->getLocation(), 10384 SecondDecl->getSourceRange(), 10385 ParamSingleDefaultArgument) 10386 << HasSecondDefaultArgument; 10387 break; 10388 } 10389 10390 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10391 "Expecting default arguments."); 10392 10393 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 10394 ParamDifferentDefaultArgument); 10395 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 10396 ParamDifferentDefaultArgument); 10397 10398 break; 10399 } 10400 case Decl::NonTypeTemplateParm: { 10401 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10402 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10403 const bool HasFirstDefaultArgument = 10404 FirstParam->hasDefaultArgument() && 10405 !FirstParam->defaultArgumentWasInherited(); 10406 const bool HasSecondDefaultArgument = 10407 SecondParam->hasDefaultArgument() && 10408 !SecondParam->defaultArgumentWasInherited(); 10409 10410 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10411 ODRDiagError(FirstDecl->getLocation(), 10412 FirstDecl->getSourceRange(), 10413 ParamSingleDefaultArgument) 10414 << HasFirstDefaultArgument; 10415 ODRDiagNote(SecondDecl->getLocation(), 10416 SecondDecl->getSourceRange(), 10417 ParamSingleDefaultArgument) 10418 << HasSecondDefaultArgument; 10419 break; 10420 } 10421 10422 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10423 "Expecting default arguments."); 10424 10425 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 10426 ParamDifferentDefaultArgument); 10427 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 10428 ParamDifferentDefaultArgument); 10429 10430 break; 10431 } 10432 case Decl::TemplateTemplateParm: { 10433 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10434 const auto *SecondParam = 10435 cast<TemplateTemplateParmDecl>(SecondDecl); 10436 const bool HasFirstDefaultArgument = 10437 FirstParam->hasDefaultArgument() && 10438 !FirstParam->defaultArgumentWasInherited(); 10439 const bool HasSecondDefaultArgument = 10440 SecondParam->hasDefaultArgument() && 10441 !SecondParam->defaultArgumentWasInherited(); 10442 10443 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10444 ODRDiagError(FirstDecl->getLocation(), 10445 FirstDecl->getSourceRange(), 10446 ParamSingleDefaultArgument) 10447 << HasFirstDefaultArgument; 10448 ODRDiagNote(SecondDecl->getLocation(), 10449 SecondDecl->getSourceRange(), 10450 ParamSingleDefaultArgument) 10451 << HasSecondDefaultArgument; 10452 break; 10453 } 10454 10455 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10456 "Expecting default arguments."); 10457 10458 ODRDiagError(FirstDecl->getLocation(), FirstDecl->getSourceRange(), 10459 ParamDifferentDefaultArgument); 10460 ODRDiagNote(SecondDecl->getLocation(), SecondDecl->getSourceRange(), 10461 ParamDifferentDefaultArgument); 10462 10463 break; 10464 } 10465 } 10466 10467 break; 10468 } 10469 10470 if (FirstIt != FirstEnd) { 10471 Diagnosed = true; 10472 break; 10473 } 10474 } 10475 10476 DeclHashes FirstHashes; 10477 DeclHashes SecondHashes; 10478 10479 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstRecord]( 10480 DeclHashes &Hashes, CXXRecordDecl *Record) { 10481 for (auto *D : Record->decls()) { 10482 // Due to decl merging, the first CXXRecordDecl is the parent of 10483 // Decls in both records. 10484 if (!ODRHash::isWhitelistedDecl(D, FirstRecord)) 10485 continue; 10486 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10487 } 10488 }; 10489 PopulateHashes(FirstHashes, FirstRecord); 10490 PopulateHashes(SecondHashes, SecondRecord); 10491 10492 // Used with err_module_odr_violation_mismatch_decl and 10493 // note_module_odr_violation_mismatch_decl 10494 // This list should be the same Decl's as in ODRHash::isWhiteListedDecl 10495 enum { 10496 EndOfClass, 10497 PublicSpecifer, 10498 PrivateSpecifer, 10499 ProtectedSpecifer, 10500 StaticAssert, 10501 Field, 10502 CXXMethod, 10503 TypeAlias, 10504 TypeDef, 10505 Var, 10506 Friend, 10507 FunctionTemplate, 10508 Other 10509 } FirstDiffType = Other, 10510 SecondDiffType = Other; 10511 10512 auto DifferenceSelector = [](Decl *D) { 10513 assert(D && "valid Decl required"); 10514 switch (D->getKind()) { 10515 default: 10516 return Other; 10517 case Decl::AccessSpec: 10518 switch (D->getAccess()) { 10519 case AS_public: 10520 return PublicSpecifer; 10521 case AS_private: 10522 return PrivateSpecifer; 10523 case AS_protected: 10524 return ProtectedSpecifer; 10525 case AS_none: 10526 break; 10527 } 10528 llvm_unreachable("Invalid access specifier"); 10529 case Decl::StaticAssert: 10530 return StaticAssert; 10531 case Decl::Field: 10532 return Field; 10533 case Decl::CXXMethod: 10534 case Decl::CXXConstructor: 10535 case Decl::CXXDestructor: 10536 return CXXMethod; 10537 case Decl::TypeAlias: 10538 return TypeAlias; 10539 case Decl::Typedef: 10540 return TypeDef; 10541 case Decl::Var: 10542 return Var; 10543 case Decl::Friend: 10544 return Friend; 10545 case Decl::FunctionTemplate: 10546 return FunctionTemplate; 10547 } 10548 }; 10549 10550 Decl *FirstDecl = nullptr; 10551 Decl *SecondDecl = nullptr; 10552 auto FirstIt = FirstHashes.begin(); 10553 auto SecondIt = SecondHashes.begin(); 10554 10555 // If there is a diagnoseable difference, FirstDiffType and 10556 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 10557 // filled in if not EndOfClass. 10558 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 10559 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 10560 FirstIt->second == SecondIt->second) { 10561 ++FirstIt; 10562 ++SecondIt; 10563 continue; 10564 } 10565 10566 FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 10567 SecondDecl = SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 10568 10569 FirstDiffType = FirstDecl ? DifferenceSelector(FirstDecl) : EndOfClass; 10570 SecondDiffType = 10571 SecondDecl ? DifferenceSelector(SecondDecl) : EndOfClass; 10572 10573 break; 10574 } 10575 10576 if (FirstDiffType == Other || SecondDiffType == Other) { 10577 // Reaching this point means an unexpected Decl was encountered 10578 // or no difference was detected. This causes a generic error 10579 // message to be emitted. 10580 Diag(FirstRecord->getLocation(), 10581 diag::err_module_odr_violation_different_definitions) 10582 << FirstRecord << FirstModule.empty() << FirstModule; 10583 10584 if (FirstDecl) { 10585 Diag(FirstDecl->getLocation(), diag::note_first_module_difference) 10586 << FirstRecord << FirstDecl->getSourceRange(); 10587 } 10588 10589 Diag(SecondRecord->getLocation(), 10590 diag::note_module_odr_violation_different_definitions) 10591 << SecondModule; 10592 10593 if (SecondDecl) { 10594 Diag(SecondDecl->getLocation(), diag::note_second_module_difference) 10595 << SecondDecl->getSourceRange(); 10596 } 10597 10598 Diagnosed = true; 10599 break; 10600 } 10601 10602 if (FirstDiffType != SecondDiffType) { 10603 SourceLocation FirstLoc; 10604 SourceRange FirstRange; 10605 if (FirstDiffType == EndOfClass) { 10606 FirstLoc = FirstRecord->getBraceRange().getEnd(); 10607 } else { 10608 FirstLoc = FirstIt->first->getLocation(); 10609 FirstRange = FirstIt->first->getSourceRange(); 10610 } 10611 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 10612 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 10613 << FirstDiffType; 10614 10615 SourceLocation SecondLoc; 10616 SourceRange SecondRange; 10617 if (SecondDiffType == EndOfClass) { 10618 SecondLoc = SecondRecord->getBraceRange().getEnd(); 10619 } else { 10620 SecondLoc = SecondDecl->getLocation(); 10621 SecondRange = SecondDecl->getSourceRange(); 10622 } 10623 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10624 << SecondModule << SecondRange << SecondDiffType; 10625 Diagnosed = true; 10626 break; 10627 } 10628 10629 assert(FirstDiffType == SecondDiffType); 10630 10631 // Used with err_module_odr_violation_mismatch_decl_diff and 10632 // note_module_odr_violation_mismatch_decl_diff 10633 enum ODRDeclDifference { 10634 StaticAssertCondition, 10635 StaticAssertMessage, 10636 StaticAssertOnlyMessage, 10637 FieldName, 10638 FieldTypeName, 10639 FieldSingleBitField, 10640 FieldDifferentWidthBitField, 10641 FieldSingleMutable, 10642 FieldSingleInitializer, 10643 FieldDifferentInitializers, 10644 MethodName, 10645 MethodDeleted, 10646 MethodDefaulted, 10647 MethodVirtual, 10648 MethodStatic, 10649 MethodVolatile, 10650 MethodConst, 10651 MethodInline, 10652 MethodNumberParameters, 10653 MethodParameterType, 10654 MethodParameterName, 10655 MethodParameterSingleDefaultArgument, 10656 MethodParameterDifferentDefaultArgument, 10657 MethodNoTemplateArguments, 10658 MethodDifferentNumberTemplateArguments, 10659 MethodDifferentTemplateArgument, 10660 MethodSingleBody, 10661 MethodDifferentBody, 10662 TypedefName, 10663 TypedefType, 10664 VarName, 10665 VarType, 10666 VarSingleInitializer, 10667 VarDifferentInitializer, 10668 VarConstexpr, 10669 FriendTypeFunction, 10670 FriendType, 10671 FriendFunction, 10672 FunctionTemplateDifferentNumberParameters, 10673 FunctionTemplateParameterDifferentKind, 10674 FunctionTemplateParameterName, 10675 FunctionTemplateParameterSingleDefaultArgument, 10676 FunctionTemplateParameterDifferentDefaultArgument, 10677 FunctionTemplateParameterDifferentType, 10678 FunctionTemplatePackParameter, 10679 }; 10680 10681 // These lambdas have the common portions of the ODR diagnostics. This 10682 // has the same return as Diag(), so addition parameters can be passed 10683 // in with operator<< 10684 auto ODRDiagError = [FirstRecord, &FirstModule, this]( 10685 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) { 10686 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 10687 << FirstRecord << FirstModule.empty() << FirstModule << Range 10688 << DiffType; 10689 }; 10690 auto ODRDiagNote = [&SecondModule, this]( 10691 SourceLocation Loc, SourceRange Range, ODRDeclDifference DiffType) { 10692 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 10693 << SecondModule << Range << DiffType; 10694 }; 10695 10696 switch (FirstDiffType) { 10697 case Other: 10698 case EndOfClass: 10699 case PublicSpecifer: 10700 case PrivateSpecifer: 10701 case ProtectedSpecifer: 10702 llvm_unreachable("Invalid diff type"); 10703 10704 case StaticAssert: { 10705 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10706 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10707 10708 Expr *FirstExpr = FirstSA->getAssertExpr(); 10709 Expr *SecondExpr = SecondSA->getAssertExpr(); 10710 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10711 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10712 if (FirstODRHash != SecondODRHash) { 10713 ODRDiagError(FirstExpr->getBeginLoc(), FirstExpr->getSourceRange(), 10714 StaticAssertCondition); 10715 ODRDiagNote(SecondExpr->getBeginLoc(), SecondExpr->getSourceRange(), 10716 StaticAssertCondition); 10717 Diagnosed = true; 10718 break; 10719 } 10720 10721 StringLiteral *FirstStr = FirstSA->getMessage(); 10722 StringLiteral *SecondStr = SecondSA->getMessage(); 10723 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10724 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10725 SourceLocation FirstLoc, SecondLoc; 10726 SourceRange FirstRange, SecondRange; 10727 if (FirstStr) { 10728 FirstLoc = FirstStr->getBeginLoc(); 10729 FirstRange = FirstStr->getSourceRange(); 10730 } else { 10731 FirstLoc = FirstSA->getBeginLoc(); 10732 FirstRange = FirstSA->getSourceRange(); 10733 } 10734 if (SecondStr) { 10735 SecondLoc = SecondStr->getBeginLoc(); 10736 SecondRange = SecondStr->getSourceRange(); 10737 } else { 10738 SecondLoc = SecondSA->getBeginLoc(); 10739 SecondRange = SecondSA->getSourceRange(); 10740 } 10741 ODRDiagError(FirstLoc, FirstRange, StaticAssertOnlyMessage) 10742 << (FirstStr == nullptr); 10743 ODRDiagNote(SecondLoc, SecondRange, StaticAssertOnlyMessage) 10744 << (SecondStr == nullptr); 10745 Diagnosed = true; 10746 break; 10747 } 10748 10749 if (FirstStr && SecondStr && 10750 FirstStr->getString() != SecondStr->getString()) { 10751 ODRDiagError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(), 10752 StaticAssertMessage); 10753 ODRDiagNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(), 10754 StaticAssertMessage); 10755 Diagnosed = true; 10756 break; 10757 } 10758 break; 10759 } 10760 case Field: { 10761 FieldDecl *FirstField = cast<FieldDecl>(FirstDecl); 10762 FieldDecl *SecondField = cast<FieldDecl>(SecondDecl); 10763 IdentifierInfo *FirstII = FirstField->getIdentifier(); 10764 IdentifierInfo *SecondII = SecondField->getIdentifier(); 10765 if (FirstII->getName() != SecondII->getName()) { 10766 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10767 FieldName) 10768 << FirstII; 10769 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10770 FieldName) 10771 << SecondII; 10772 10773 Diagnosed = true; 10774 break; 10775 } 10776 10777 assert(getContext().hasSameType(FirstField->getType(), 10778 SecondField->getType())); 10779 10780 QualType FirstType = FirstField->getType(); 10781 QualType SecondType = SecondField->getType(); 10782 if (ComputeQualTypeODRHash(FirstType) != 10783 ComputeQualTypeODRHash(SecondType)) { 10784 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10785 FieldTypeName) 10786 << FirstII << FirstType; 10787 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10788 FieldTypeName) 10789 << SecondII << SecondType; 10790 10791 Diagnosed = true; 10792 break; 10793 } 10794 10795 const bool IsFirstBitField = FirstField->isBitField(); 10796 const bool IsSecondBitField = SecondField->isBitField(); 10797 if (IsFirstBitField != IsSecondBitField) { 10798 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10799 FieldSingleBitField) 10800 << FirstII << IsFirstBitField; 10801 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10802 FieldSingleBitField) 10803 << SecondII << IsSecondBitField; 10804 Diagnosed = true; 10805 break; 10806 } 10807 10808 if (IsFirstBitField && IsSecondBitField) { 10809 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10810 FieldDifferentWidthBitField) 10811 << FirstII << FirstField->getBitWidth()->getSourceRange(); 10812 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10813 FieldDifferentWidthBitField) 10814 << SecondII << SecondField->getBitWidth()->getSourceRange(); 10815 Diagnosed = true; 10816 break; 10817 } 10818 10819 const bool IsFirstMutable = FirstField->isMutable(); 10820 const bool IsSecondMutable = SecondField->isMutable(); 10821 if (IsFirstMutable != IsSecondMutable) { 10822 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10823 FieldSingleMutable) 10824 << FirstII << IsFirstMutable; 10825 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10826 FieldSingleMutable) 10827 << SecondII << IsSecondMutable; 10828 Diagnosed = true; 10829 break; 10830 } 10831 10832 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 10833 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 10834 if ((!FirstInitializer && SecondInitializer) || 10835 (FirstInitializer && !SecondInitializer)) { 10836 ODRDiagError(FirstField->getLocation(), FirstField->getSourceRange(), 10837 FieldSingleInitializer) 10838 << FirstII << (FirstInitializer != nullptr); 10839 ODRDiagNote(SecondField->getLocation(), SecondField->getSourceRange(), 10840 FieldSingleInitializer) 10841 << SecondII << (SecondInitializer != nullptr); 10842 Diagnosed = true; 10843 break; 10844 } 10845 10846 if (FirstInitializer && SecondInitializer) { 10847 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 10848 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 10849 if (FirstInitHash != SecondInitHash) { 10850 ODRDiagError(FirstField->getLocation(), 10851 FirstField->getSourceRange(), 10852 FieldDifferentInitializers) 10853 << FirstII << FirstInitializer->getSourceRange(); 10854 ODRDiagNote(SecondField->getLocation(), 10855 SecondField->getSourceRange(), 10856 FieldDifferentInitializers) 10857 << SecondII << SecondInitializer->getSourceRange(); 10858 Diagnosed = true; 10859 break; 10860 } 10861 } 10862 10863 break; 10864 } 10865 case CXXMethod: { 10866 enum { 10867 DiagMethod, 10868 DiagConstructor, 10869 DiagDestructor, 10870 } FirstMethodType, 10871 SecondMethodType; 10872 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10873 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10874 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10875 return DiagMethod; 10876 }; 10877 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10878 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10879 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10880 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10881 auto FirstName = FirstMethod->getDeclName(); 10882 auto SecondName = SecondMethod->getDeclName(); 10883 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10884 ODRDiagError(FirstMethod->getLocation(), 10885 FirstMethod->getSourceRange(), MethodName) 10886 << FirstMethodType << FirstName; 10887 ODRDiagNote(SecondMethod->getLocation(), 10888 SecondMethod->getSourceRange(), MethodName) 10889 << SecondMethodType << SecondName; 10890 10891 Diagnosed = true; 10892 break; 10893 } 10894 10895 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10896 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10897 if (FirstDeleted != SecondDeleted) { 10898 ODRDiagError(FirstMethod->getLocation(), 10899 FirstMethod->getSourceRange(), MethodDeleted) 10900 << FirstMethodType << FirstName << FirstDeleted; 10901 10902 ODRDiagNote(SecondMethod->getLocation(), 10903 SecondMethod->getSourceRange(), MethodDeleted) 10904 << SecondMethodType << SecondName << SecondDeleted; 10905 Diagnosed = true; 10906 break; 10907 } 10908 10909 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10910 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10911 if (FirstDefaulted != SecondDefaulted) { 10912 ODRDiagError(FirstMethod->getLocation(), 10913 FirstMethod->getSourceRange(), MethodDefaulted) 10914 << FirstMethodType << FirstName << FirstDefaulted; 10915 10916 ODRDiagNote(SecondMethod->getLocation(), 10917 SecondMethod->getSourceRange(), MethodDefaulted) 10918 << SecondMethodType << SecondName << SecondDefaulted; 10919 Diagnosed = true; 10920 break; 10921 } 10922 10923 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10924 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10925 const bool FirstPure = FirstMethod->isPure(); 10926 const bool SecondPure = SecondMethod->isPure(); 10927 if ((FirstVirtual || SecondVirtual) && 10928 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10929 ODRDiagError(FirstMethod->getLocation(), 10930 FirstMethod->getSourceRange(), MethodVirtual) 10931 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10932 ODRDiagNote(SecondMethod->getLocation(), 10933 SecondMethod->getSourceRange(), MethodVirtual) 10934 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10935 Diagnosed = true; 10936 break; 10937 } 10938 10939 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10940 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10941 // class needs to be checked instead. 10942 const auto FirstStorage = FirstMethod->getStorageClass(); 10943 const auto SecondStorage = SecondMethod->getStorageClass(); 10944 const bool FirstStatic = FirstStorage == SC_Static; 10945 const bool SecondStatic = SecondStorage == SC_Static; 10946 if (FirstStatic != SecondStatic) { 10947 ODRDiagError(FirstMethod->getLocation(), 10948 FirstMethod->getSourceRange(), MethodStatic) 10949 << FirstMethodType << FirstName << FirstStatic; 10950 ODRDiagNote(SecondMethod->getLocation(), 10951 SecondMethod->getSourceRange(), MethodStatic) 10952 << SecondMethodType << SecondName << SecondStatic; 10953 Diagnosed = true; 10954 break; 10955 } 10956 10957 const bool FirstVolatile = FirstMethod->isVolatile(); 10958 const bool SecondVolatile = SecondMethod->isVolatile(); 10959 if (FirstVolatile != SecondVolatile) { 10960 ODRDiagError(FirstMethod->getLocation(), 10961 FirstMethod->getSourceRange(), MethodVolatile) 10962 << FirstMethodType << FirstName << FirstVolatile; 10963 ODRDiagNote(SecondMethod->getLocation(), 10964 SecondMethod->getSourceRange(), MethodVolatile) 10965 << SecondMethodType << SecondName << SecondVolatile; 10966 Diagnosed = true; 10967 break; 10968 } 10969 10970 const bool FirstConst = FirstMethod->isConst(); 10971 const bool SecondConst = SecondMethod->isConst(); 10972 if (FirstConst != SecondConst) { 10973 ODRDiagError(FirstMethod->getLocation(), 10974 FirstMethod->getSourceRange(), MethodConst) 10975 << FirstMethodType << FirstName << FirstConst; 10976 ODRDiagNote(SecondMethod->getLocation(), 10977 SecondMethod->getSourceRange(), MethodConst) 10978 << SecondMethodType << SecondName << SecondConst; 10979 Diagnosed = true; 10980 break; 10981 } 10982 10983 const bool FirstInline = FirstMethod->isInlineSpecified(); 10984 const bool SecondInline = SecondMethod->isInlineSpecified(); 10985 if (FirstInline != SecondInline) { 10986 ODRDiagError(FirstMethod->getLocation(), 10987 FirstMethod->getSourceRange(), MethodInline) 10988 << FirstMethodType << FirstName << FirstInline; 10989 ODRDiagNote(SecondMethod->getLocation(), 10990 SecondMethod->getSourceRange(), MethodInline) 10991 << SecondMethodType << SecondName << SecondInline; 10992 Diagnosed = true; 10993 break; 10994 } 10995 10996 const unsigned FirstNumParameters = FirstMethod->param_size(); 10997 const unsigned SecondNumParameters = SecondMethod->param_size(); 10998 if (FirstNumParameters != SecondNumParameters) { 10999 ODRDiagError(FirstMethod->getLocation(), 11000 FirstMethod->getSourceRange(), MethodNumberParameters) 11001 << FirstMethodType << FirstName << FirstNumParameters; 11002 ODRDiagNote(SecondMethod->getLocation(), 11003 SecondMethod->getSourceRange(), MethodNumberParameters) 11004 << SecondMethodType << SecondName << SecondNumParameters; 11005 Diagnosed = true; 11006 break; 11007 } 11008 11009 // Need this status boolean to know when break out of the switch. 11010 bool ParameterMismatch = false; 11011 for (unsigned I = 0; I < FirstNumParameters; ++I) { 11012 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 11013 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 11014 11015 QualType FirstParamType = FirstParam->getType(); 11016 QualType SecondParamType = SecondParam->getType(); 11017 if (FirstParamType != SecondParamType && 11018 ComputeQualTypeODRHash(FirstParamType) != 11019 ComputeQualTypeODRHash(SecondParamType)) { 11020 if (const DecayedType *ParamDecayedType = 11021 FirstParamType->getAs<DecayedType>()) { 11022 ODRDiagError(FirstMethod->getLocation(), 11023 FirstMethod->getSourceRange(), MethodParameterType) 11024 << FirstMethodType << FirstName << (I + 1) << FirstParamType 11025 << true << ParamDecayedType->getOriginalType(); 11026 } else { 11027 ODRDiagError(FirstMethod->getLocation(), 11028 FirstMethod->getSourceRange(), MethodParameterType) 11029 << FirstMethodType << FirstName << (I + 1) << FirstParamType 11030 << false; 11031 } 11032 11033 if (const DecayedType *ParamDecayedType = 11034 SecondParamType->getAs<DecayedType>()) { 11035 ODRDiagNote(SecondMethod->getLocation(), 11036 SecondMethod->getSourceRange(), MethodParameterType) 11037 << SecondMethodType << SecondName << (I + 1) 11038 << SecondParamType << true 11039 << ParamDecayedType->getOriginalType(); 11040 } else { 11041 ODRDiagNote(SecondMethod->getLocation(), 11042 SecondMethod->getSourceRange(), MethodParameterType) 11043 << SecondMethodType << SecondName << (I + 1) 11044 << SecondParamType << false; 11045 } 11046 ParameterMismatch = true; 11047 break; 11048 } 11049 11050 DeclarationName FirstParamName = FirstParam->getDeclName(); 11051 DeclarationName SecondParamName = SecondParam->getDeclName(); 11052 if (FirstParamName != SecondParamName) { 11053 ODRDiagError(FirstMethod->getLocation(), 11054 FirstMethod->getSourceRange(), MethodParameterName) 11055 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 11056 ODRDiagNote(SecondMethod->getLocation(), 11057 SecondMethod->getSourceRange(), MethodParameterName) 11058 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 11059 ParameterMismatch = true; 11060 break; 11061 } 11062 11063 const Expr *FirstInit = FirstParam->getInit(); 11064 const Expr *SecondInit = SecondParam->getInit(); 11065 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11066 ODRDiagError(FirstMethod->getLocation(), 11067 FirstMethod->getSourceRange(), 11068 MethodParameterSingleDefaultArgument) 11069 << FirstMethodType << FirstName << (I + 1) 11070 << (FirstInit == nullptr) 11071 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11072 ODRDiagNote(SecondMethod->getLocation(), 11073 SecondMethod->getSourceRange(), 11074 MethodParameterSingleDefaultArgument) 11075 << SecondMethodType << SecondName << (I + 1) 11076 << (SecondInit == nullptr) 11077 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11078 ParameterMismatch = true; 11079 break; 11080 } 11081 11082 if (FirstInit && SecondInit && 11083 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11084 ODRDiagError(FirstMethod->getLocation(), 11085 FirstMethod->getSourceRange(), 11086 MethodParameterDifferentDefaultArgument) 11087 << FirstMethodType << FirstName << (I + 1) 11088 << FirstInit->getSourceRange(); 11089 ODRDiagNote(SecondMethod->getLocation(), 11090 SecondMethod->getSourceRange(), 11091 MethodParameterDifferentDefaultArgument) 11092 << SecondMethodType << SecondName << (I + 1) 11093 << SecondInit->getSourceRange(); 11094 ParameterMismatch = true; 11095 break; 11096 11097 } 11098 } 11099 11100 if (ParameterMismatch) { 11101 Diagnosed = true; 11102 break; 11103 } 11104 11105 const auto *FirstTemplateArgs = 11106 FirstMethod->getTemplateSpecializationArgs(); 11107 const auto *SecondTemplateArgs = 11108 SecondMethod->getTemplateSpecializationArgs(); 11109 11110 if ((FirstTemplateArgs && !SecondTemplateArgs) || 11111 (!FirstTemplateArgs && SecondTemplateArgs)) { 11112 ODRDiagError(FirstMethod->getLocation(), 11113 FirstMethod->getSourceRange(), MethodNoTemplateArguments) 11114 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 11115 ODRDiagNote(SecondMethod->getLocation(), 11116 SecondMethod->getSourceRange(), MethodNoTemplateArguments) 11117 << SecondMethodType << SecondName 11118 << (SecondTemplateArgs != nullptr); 11119 11120 Diagnosed = true; 11121 break; 11122 } 11123 11124 if (FirstTemplateArgs && SecondTemplateArgs) { 11125 // Remove pack expansions from argument list. 11126 auto ExpandTemplateArgumentList = 11127 [](const TemplateArgumentList *TAL) { 11128 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 11129 for (const TemplateArgument &TA : TAL->asArray()) { 11130 if (TA.getKind() != TemplateArgument::Pack) { 11131 ExpandedList.push_back(&TA); 11132 continue; 11133 } 11134 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 11135 ExpandedList.push_back(&PackTA); 11136 } 11137 } 11138 return ExpandedList; 11139 }; 11140 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 11141 ExpandTemplateArgumentList(FirstTemplateArgs); 11142 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 11143 ExpandTemplateArgumentList(SecondTemplateArgs); 11144 11145 if (FirstExpandedList.size() != SecondExpandedList.size()) { 11146 ODRDiagError(FirstMethod->getLocation(), 11147 FirstMethod->getSourceRange(), 11148 MethodDifferentNumberTemplateArguments) 11149 << FirstMethodType << FirstName 11150 << (unsigned)FirstExpandedList.size(); 11151 ODRDiagNote(SecondMethod->getLocation(), 11152 SecondMethod->getSourceRange(), 11153 MethodDifferentNumberTemplateArguments) 11154 << SecondMethodType << SecondName 11155 << (unsigned)SecondExpandedList.size(); 11156 11157 Diagnosed = true; 11158 break; 11159 } 11160 11161 bool TemplateArgumentMismatch = false; 11162 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 11163 const TemplateArgument &FirstTA = *FirstExpandedList[i], 11164 &SecondTA = *SecondExpandedList[i]; 11165 if (ComputeTemplateArgumentODRHash(FirstTA) == 11166 ComputeTemplateArgumentODRHash(SecondTA)) { 11167 continue; 11168 } 11169 11170 ODRDiagError(FirstMethod->getLocation(), 11171 FirstMethod->getSourceRange(), 11172 MethodDifferentTemplateArgument) 11173 << FirstMethodType << FirstName << FirstTA << i + 1; 11174 ODRDiagNote(SecondMethod->getLocation(), 11175 SecondMethod->getSourceRange(), 11176 MethodDifferentTemplateArgument) 11177 << SecondMethodType << SecondName << SecondTA << i + 1; 11178 11179 TemplateArgumentMismatch = true; 11180 break; 11181 } 11182 11183 if (TemplateArgumentMismatch) { 11184 Diagnosed = true; 11185 break; 11186 } 11187 } 11188 11189 // Compute the hash of the method as if it has no body. 11190 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 11191 Hash.clear(); 11192 Hash.AddFunctionDecl(D, true /*SkipBody*/); 11193 return Hash.CalculateHash(); 11194 }; 11195 11196 // Compare the hash generated to the hash stored. A difference means 11197 // that a body was present in the original source. Due to merging, 11198 // the stardard way of detecting a body will not work. 11199 const bool HasFirstBody = 11200 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 11201 const bool HasSecondBody = 11202 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 11203 11204 if (HasFirstBody != HasSecondBody) { 11205 ODRDiagError(FirstMethod->getLocation(), 11206 FirstMethod->getSourceRange(), MethodSingleBody) 11207 << FirstMethodType << FirstName << HasFirstBody; 11208 ODRDiagNote(SecondMethod->getLocation(), 11209 SecondMethod->getSourceRange(), MethodSingleBody) 11210 << SecondMethodType << SecondName << HasSecondBody; 11211 Diagnosed = true; 11212 break; 11213 } 11214 11215 if (HasFirstBody && HasSecondBody) { 11216 ODRDiagError(FirstMethod->getLocation(), 11217 FirstMethod->getSourceRange(), MethodDifferentBody) 11218 << FirstMethodType << FirstName; 11219 ODRDiagNote(SecondMethod->getLocation(), 11220 SecondMethod->getSourceRange(), MethodDifferentBody) 11221 << SecondMethodType << SecondName; 11222 Diagnosed = true; 11223 break; 11224 } 11225 11226 break; 11227 } 11228 case TypeAlias: 11229 case TypeDef: { 11230 TypedefNameDecl *FirstTD = cast<TypedefNameDecl>(FirstDecl); 11231 TypedefNameDecl *SecondTD = cast<TypedefNameDecl>(SecondDecl); 11232 auto FirstName = FirstTD->getDeclName(); 11233 auto SecondName = SecondTD->getDeclName(); 11234 if (FirstName != SecondName) { 11235 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(), 11236 TypedefName) 11237 << (FirstDiffType == TypeAlias) << FirstName; 11238 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(), 11239 TypedefName) 11240 << (FirstDiffType == TypeAlias) << SecondName; 11241 Diagnosed = true; 11242 break; 11243 } 11244 11245 QualType FirstType = FirstTD->getUnderlyingType(); 11246 QualType SecondType = SecondTD->getUnderlyingType(); 11247 if (ComputeQualTypeODRHash(FirstType) != 11248 ComputeQualTypeODRHash(SecondType)) { 11249 ODRDiagError(FirstTD->getLocation(), FirstTD->getSourceRange(), 11250 TypedefType) 11251 << (FirstDiffType == TypeAlias) << FirstName << FirstType; 11252 ODRDiagNote(SecondTD->getLocation(), SecondTD->getSourceRange(), 11253 TypedefType) 11254 << (FirstDiffType == TypeAlias) << SecondName << SecondType; 11255 Diagnosed = true; 11256 break; 11257 } 11258 break; 11259 } 11260 case Var: { 11261 VarDecl *FirstVD = cast<VarDecl>(FirstDecl); 11262 VarDecl *SecondVD = cast<VarDecl>(SecondDecl); 11263 auto FirstName = FirstVD->getDeclName(); 11264 auto SecondName = SecondVD->getDeclName(); 11265 if (FirstName != SecondName) { 11266 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 11267 VarName) 11268 << FirstName; 11269 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 11270 VarName) 11271 << SecondName; 11272 Diagnosed = true; 11273 break; 11274 } 11275 11276 QualType FirstType = FirstVD->getType(); 11277 QualType SecondType = SecondVD->getType(); 11278 if (ComputeQualTypeODRHash(FirstType) != 11279 ComputeQualTypeODRHash(SecondType)) { 11280 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 11281 VarType) 11282 << FirstName << FirstType; 11283 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 11284 VarType) 11285 << SecondName << SecondType; 11286 Diagnosed = true; 11287 break; 11288 } 11289 11290 const Expr *FirstInit = FirstVD->getInit(); 11291 const Expr *SecondInit = SecondVD->getInit(); 11292 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11293 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 11294 VarSingleInitializer) 11295 << FirstName << (FirstInit == nullptr) 11296 << (FirstInit ? FirstInit->getSourceRange(): SourceRange()); 11297 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 11298 VarSingleInitializer) 11299 << SecondName << (SecondInit == nullptr) 11300 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11301 Diagnosed = true; 11302 break; 11303 } 11304 11305 if (FirstInit && SecondInit && 11306 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11307 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 11308 VarDifferentInitializer) 11309 << FirstName << FirstInit->getSourceRange(); 11310 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 11311 VarDifferentInitializer) 11312 << SecondName << SecondInit->getSourceRange(); 11313 Diagnosed = true; 11314 break; 11315 } 11316 11317 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 11318 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 11319 if (FirstIsConstexpr != SecondIsConstexpr) { 11320 ODRDiagError(FirstVD->getLocation(), FirstVD->getSourceRange(), 11321 VarConstexpr) 11322 << FirstName << FirstIsConstexpr; 11323 ODRDiagNote(SecondVD->getLocation(), SecondVD->getSourceRange(), 11324 VarConstexpr) 11325 << SecondName << SecondIsConstexpr; 11326 Diagnosed = true; 11327 break; 11328 } 11329 break; 11330 } 11331 case Friend: { 11332 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 11333 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 11334 11335 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 11336 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 11337 11338 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 11339 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 11340 11341 if (FirstND && SecondND) { 11342 ODRDiagError(FirstFriend->getFriendLoc(), 11343 FirstFriend->getSourceRange(), FriendFunction) 11344 << FirstND; 11345 ODRDiagNote(SecondFriend->getFriendLoc(), 11346 SecondFriend->getSourceRange(), FriendFunction) 11347 << SecondND; 11348 11349 Diagnosed = true; 11350 break; 11351 } 11352 11353 if (FirstTSI && SecondTSI) { 11354 QualType FirstFriendType = FirstTSI->getType(); 11355 QualType SecondFriendType = SecondTSI->getType(); 11356 assert(ComputeQualTypeODRHash(FirstFriendType) != 11357 ComputeQualTypeODRHash(SecondFriendType)); 11358 ODRDiagError(FirstFriend->getFriendLoc(), 11359 FirstFriend->getSourceRange(), FriendType) 11360 << FirstFriendType; 11361 ODRDiagNote(SecondFriend->getFriendLoc(), 11362 SecondFriend->getSourceRange(), FriendType) 11363 << SecondFriendType; 11364 Diagnosed = true; 11365 break; 11366 } 11367 11368 ODRDiagError(FirstFriend->getFriendLoc(), FirstFriend->getSourceRange(), 11369 FriendTypeFunction) 11370 << (FirstTSI == nullptr); 11371 ODRDiagNote(SecondFriend->getFriendLoc(), 11372 SecondFriend->getSourceRange(), FriendTypeFunction) 11373 << (SecondTSI == nullptr); 11374 11375 Diagnosed = true; 11376 break; 11377 } 11378 case FunctionTemplate: { 11379 FunctionTemplateDecl *FirstTemplate = 11380 cast<FunctionTemplateDecl>(FirstDecl); 11381 FunctionTemplateDecl *SecondTemplate = 11382 cast<FunctionTemplateDecl>(SecondDecl); 11383 11384 TemplateParameterList *FirstTPL = 11385 FirstTemplate->getTemplateParameters(); 11386 TemplateParameterList *SecondTPL = 11387 SecondTemplate->getTemplateParameters(); 11388 11389 if (FirstTPL->size() != SecondTPL->size()) { 11390 ODRDiagError(FirstTemplate->getLocation(), 11391 FirstTemplate->getSourceRange(), 11392 FunctionTemplateDifferentNumberParameters) 11393 << FirstTemplate << FirstTPL->size(); 11394 ODRDiagNote(SecondTemplate->getLocation(), 11395 SecondTemplate->getSourceRange(), 11396 FunctionTemplateDifferentNumberParameters) 11397 << SecondTemplate << SecondTPL->size(); 11398 11399 Diagnosed = true; 11400 break; 11401 } 11402 11403 bool ParameterMismatch = false; 11404 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 11405 NamedDecl *FirstParam = FirstTPL->getParam(i); 11406 NamedDecl *SecondParam = SecondTPL->getParam(i); 11407 11408 if (FirstParam->getKind() != SecondParam->getKind()) { 11409 enum { 11410 TemplateTypeParameter, 11411 NonTypeTemplateParameter, 11412 TemplateTemplateParameter, 11413 }; 11414 auto GetParamType = [](NamedDecl *D) { 11415 switch (D->getKind()) { 11416 default: 11417 llvm_unreachable("Unexpected template parameter type"); 11418 case Decl::TemplateTypeParm: 11419 return TemplateTypeParameter; 11420 case Decl::NonTypeTemplateParm: 11421 return NonTypeTemplateParameter; 11422 case Decl::TemplateTemplateParm: 11423 return TemplateTemplateParameter; 11424 } 11425 }; 11426 11427 ODRDiagError(FirstTemplate->getLocation(), 11428 FirstTemplate->getSourceRange(), 11429 FunctionTemplateParameterDifferentKind) 11430 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 11431 ODRDiagNote(SecondTemplate->getLocation(), 11432 SecondTemplate->getSourceRange(), 11433 FunctionTemplateParameterDifferentKind) 11434 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 11435 11436 ParameterMismatch = true; 11437 break; 11438 } 11439 11440 if (FirstParam->getName() != SecondParam->getName()) { 11441 ODRDiagError(FirstTemplate->getLocation(), 11442 FirstTemplate->getSourceRange(), 11443 FunctionTemplateParameterName) 11444 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 11445 << FirstParam; 11446 ODRDiagNote(SecondTemplate->getLocation(), 11447 SecondTemplate->getSourceRange(), 11448 FunctionTemplateParameterName) 11449 << SecondTemplate << (i + 1) 11450 << (bool)SecondParam->getIdentifier() << SecondParam; 11451 ParameterMismatch = true; 11452 break; 11453 } 11454 11455 if (isa<TemplateTypeParmDecl>(FirstParam) && 11456 isa<TemplateTypeParmDecl>(SecondParam)) { 11457 TemplateTypeParmDecl *FirstTTPD = 11458 cast<TemplateTypeParmDecl>(FirstParam); 11459 TemplateTypeParmDecl *SecondTTPD = 11460 cast<TemplateTypeParmDecl>(SecondParam); 11461 bool HasFirstDefaultArgument = 11462 FirstTTPD->hasDefaultArgument() && 11463 !FirstTTPD->defaultArgumentWasInherited(); 11464 bool HasSecondDefaultArgument = 11465 SecondTTPD->hasDefaultArgument() && 11466 !SecondTTPD->defaultArgumentWasInherited(); 11467 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11468 ODRDiagError(FirstTemplate->getLocation(), 11469 FirstTemplate->getSourceRange(), 11470 FunctionTemplateParameterSingleDefaultArgument) 11471 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11472 ODRDiagNote(SecondTemplate->getLocation(), 11473 SecondTemplate->getSourceRange(), 11474 FunctionTemplateParameterSingleDefaultArgument) 11475 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11476 ParameterMismatch = true; 11477 break; 11478 } 11479 11480 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11481 QualType FirstType = FirstTTPD->getDefaultArgument(); 11482 QualType SecondType = SecondTTPD->getDefaultArgument(); 11483 if (ComputeQualTypeODRHash(FirstType) != 11484 ComputeQualTypeODRHash(SecondType)) { 11485 ODRDiagError(FirstTemplate->getLocation(), 11486 FirstTemplate->getSourceRange(), 11487 FunctionTemplateParameterDifferentDefaultArgument) 11488 << FirstTemplate << (i + 1) << FirstType; 11489 ODRDiagNote(SecondTemplate->getLocation(), 11490 SecondTemplate->getSourceRange(), 11491 FunctionTemplateParameterDifferentDefaultArgument) 11492 << SecondTemplate << (i + 1) << SecondType; 11493 ParameterMismatch = true; 11494 break; 11495 } 11496 } 11497 11498 if (FirstTTPD->isParameterPack() != 11499 SecondTTPD->isParameterPack()) { 11500 ODRDiagError(FirstTemplate->getLocation(), 11501 FirstTemplate->getSourceRange(), 11502 FunctionTemplatePackParameter) 11503 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11504 ODRDiagNote(SecondTemplate->getLocation(), 11505 SecondTemplate->getSourceRange(), 11506 FunctionTemplatePackParameter) 11507 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11508 ParameterMismatch = true; 11509 break; 11510 } 11511 } 11512 11513 if (isa<TemplateTemplateParmDecl>(FirstParam) && 11514 isa<TemplateTemplateParmDecl>(SecondParam)) { 11515 TemplateTemplateParmDecl *FirstTTPD = 11516 cast<TemplateTemplateParmDecl>(FirstParam); 11517 TemplateTemplateParmDecl *SecondTTPD = 11518 cast<TemplateTemplateParmDecl>(SecondParam); 11519 11520 TemplateParameterList *FirstTPL = 11521 FirstTTPD->getTemplateParameters(); 11522 TemplateParameterList *SecondTPL = 11523 SecondTTPD->getTemplateParameters(); 11524 11525 if (ComputeTemplateParameterListODRHash(FirstTPL) != 11526 ComputeTemplateParameterListODRHash(SecondTPL)) { 11527 ODRDiagError(FirstTemplate->getLocation(), 11528 FirstTemplate->getSourceRange(), 11529 FunctionTemplateParameterDifferentType) 11530 << FirstTemplate << (i + 1); 11531 ODRDiagNote(SecondTemplate->getLocation(), 11532 SecondTemplate->getSourceRange(), 11533 FunctionTemplateParameterDifferentType) 11534 << SecondTemplate << (i + 1); 11535 ParameterMismatch = true; 11536 break; 11537 } 11538 11539 bool HasFirstDefaultArgument = 11540 FirstTTPD->hasDefaultArgument() && 11541 !FirstTTPD->defaultArgumentWasInherited(); 11542 bool HasSecondDefaultArgument = 11543 SecondTTPD->hasDefaultArgument() && 11544 !SecondTTPD->defaultArgumentWasInherited(); 11545 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11546 ODRDiagError(FirstTemplate->getLocation(), 11547 FirstTemplate->getSourceRange(), 11548 FunctionTemplateParameterSingleDefaultArgument) 11549 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11550 ODRDiagNote(SecondTemplate->getLocation(), 11551 SecondTemplate->getSourceRange(), 11552 FunctionTemplateParameterSingleDefaultArgument) 11553 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11554 ParameterMismatch = true; 11555 break; 11556 } 11557 11558 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11559 TemplateArgument FirstTA = 11560 FirstTTPD->getDefaultArgument().getArgument(); 11561 TemplateArgument SecondTA = 11562 SecondTTPD->getDefaultArgument().getArgument(); 11563 if (ComputeTemplateArgumentODRHash(FirstTA) != 11564 ComputeTemplateArgumentODRHash(SecondTA)) { 11565 ODRDiagError(FirstTemplate->getLocation(), 11566 FirstTemplate->getSourceRange(), 11567 FunctionTemplateParameterDifferentDefaultArgument) 11568 << FirstTemplate << (i + 1) << FirstTA; 11569 ODRDiagNote(SecondTemplate->getLocation(), 11570 SecondTemplate->getSourceRange(), 11571 FunctionTemplateParameterDifferentDefaultArgument) 11572 << SecondTemplate << (i + 1) << SecondTA; 11573 ParameterMismatch = true; 11574 break; 11575 } 11576 } 11577 11578 if (FirstTTPD->isParameterPack() != 11579 SecondTTPD->isParameterPack()) { 11580 ODRDiagError(FirstTemplate->getLocation(), 11581 FirstTemplate->getSourceRange(), 11582 FunctionTemplatePackParameter) 11583 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11584 ODRDiagNote(SecondTemplate->getLocation(), 11585 SecondTemplate->getSourceRange(), 11586 FunctionTemplatePackParameter) 11587 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11588 ParameterMismatch = true; 11589 break; 11590 } 11591 } 11592 11593 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11594 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11595 NonTypeTemplateParmDecl *FirstNTTPD = 11596 cast<NonTypeTemplateParmDecl>(FirstParam); 11597 NonTypeTemplateParmDecl *SecondNTTPD = 11598 cast<NonTypeTemplateParmDecl>(SecondParam); 11599 11600 QualType FirstType = FirstNTTPD->getType(); 11601 QualType SecondType = SecondNTTPD->getType(); 11602 if (ComputeQualTypeODRHash(FirstType) != 11603 ComputeQualTypeODRHash(SecondType)) { 11604 ODRDiagError(FirstTemplate->getLocation(), 11605 FirstTemplate->getSourceRange(), 11606 FunctionTemplateParameterDifferentType) 11607 << FirstTemplate << (i + 1); 11608 ODRDiagNote(SecondTemplate->getLocation(), 11609 SecondTemplate->getSourceRange(), 11610 FunctionTemplateParameterDifferentType) 11611 << SecondTemplate << (i + 1); 11612 ParameterMismatch = true; 11613 break; 11614 } 11615 11616 bool HasFirstDefaultArgument = 11617 FirstNTTPD->hasDefaultArgument() && 11618 !FirstNTTPD->defaultArgumentWasInherited(); 11619 bool HasSecondDefaultArgument = 11620 SecondNTTPD->hasDefaultArgument() && 11621 !SecondNTTPD->defaultArgumentWasInherited(); 11622 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11623 ODRDiagError(FirstTemplate->getLocation(), 11624 FirstTemplate->getSourceRange(), 11625 FunctionTemplateParameterSingleDefaultArgument) 11626 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11627 ODRDiagNote(SecondTemplate->getLocation(), 11628 SecondTemplate->getSourceRange(), 11629 FunctionTemplateParameterSingleDefaultArgument) 11630 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11631 ParameterMismatch = true; 11632 break; 11633 } 11634 11635 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11636 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11637 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11638 if (ComputeODRHash(FirstDefaultArgument) != 11639 ComputeODRHash(SecondDefaultArgument)) { 11640 ODRDiagError(FirstTemplate->getLocation(), 11641 FirstTemplate->getSourceRange(), 11642 FunctionTemplateParameterDifferentDefaultArgument) 11643 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11644 ODRDiagNote(SecondTemplate->getLocation(), 11645 SecondTemplate->getSourceRange(), 11646 FunctionTemplateParameterDifferentDefaultArgument) 11647 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11648 ParameterMismatch = true; 11649 break; 11650 } 11651 } 11652 11653 if (FirstNTTPD->isParameterPack() != 11654 SecondNTTPD->isParameterPack()) { 11655 ODRDiagError(FirstTemplate->getLocation(), 11656 FirstTemplate->getSourceRange(), 11657 FunctionTemplatePackParameter) 11658 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11659 ODRDiagNote(SecondTemplate->getLocation(), 11660 SecondTemplate->getSourceRange(), 11661 FunctionTemplatePackParameter) 11662 << SecondTemplate << (i + 1) 11663 << SecondNTTPD->isParameterPack(); 11664 ParameterMismatch = true; 11665 break; 11666 } 11667 } 11668 } 11669 11670 if (ParameterMismatch) { 11671 Diagnosed = true; 11672 break; 11673 } 11674 11675 break; 11676 } 11677 } 11678 11679 if (Diagnosed) 11680 continue; 11681 11682 Diag(FirstDecl->getLocation(), 11683 diag::err_module_odr_violation_mismatch_decl_unknown) 11684 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11685 << FirstDecl->getSourceRange(); 11686 Diag(SecondDecl->getLocation(), 11687 diag::note_module_odr_violation_mismatch_decl_unknown) 11688 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11689 Diagnosed = true; 11690 } 11691 11692 if (!Diagnosed) { 11693 // All definitions are updates to the same declaration. This happens if a 11694 // module instantiates the declaration of a class template specialization 11695 // and two or more other modules instantiate its definition. 11696 // 11697 // FIXME: Indicate which modules had instantiations of this definition. 11698 // FIXME: How can this even happen? 11699 Diag(Merge.first->getLocation(), 11700 diag::err_module_odr_violation_different_instantiations) 11701 << Merge.first; 11702 } 11703 } 11704 11705 // Issue ODR failures diagnostics for functions. 11706 for (auto &Merge : FunctionOdrMergeFailures) { 11707 enum ODRFunctionDifference { 11708 ReturnType, 11709 ParameterName, 11710 ParameterType, 11711 ParameterSingleDefaultArgument, 11712 ParameterDifferentDefaultArgument, 11713 FunctionBody, 11714 }; 11715 11716 FunctionDecl *FirstFunction = Merge.first; 11717 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11718 11719 bool Diagnosed = false; 11720 for (auto &SecondFunction : Merge.second) { 11721 11722 if (FirstFunction == SecondFunction) 11723 continue; 11724 11725 std::string SecondModule = 11726 getOwningModuleNameForDiagnostic(SecondFunction); 11727 11728 auto ODRDiagError = [FirstFunction, &FirstModule, 11729 this](SourceLocation Loc, SourceRange Range, 11730 ODRFunctionDifference DiffType) { 11731 return Diag(Loc, diag::err_module_odr_violation_function) 11732 << FirstFunction << FirstModule.empty() << FirstModule << Range 11733 << DiffType; 11734 }; 11735 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11736 SourceRange Range, 11737 ODRFunctionDifference DiffType) { 11738 return Diag(Loc, diag::note_module_odr_violation_function) 11739 << SecondModule << Range << DiffType; 11740 }; 11741 11742 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11743 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11744 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11745 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11746 << FirstFunction->getReturnType(); 11747 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11748 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11749 << SecondFunction->getReturnType(); 11750 Diagnosed = true; 11751 break; 11752 } 11753 11754 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11755 "Merged functions with different number of parameters"); 11756 11757 auto ParamSize = FirstFunction->param_size(); 11758 bool ParameterMismatch = false; 11759 for (unsigned I = 0; I < ParamSize; ++I) { 11760 auto *FirstParam = FirstFunction->getParamDecl(I); 11761 auto *SecondParam = SecondFunction->getParamDecl(I); 11762 11763 assert(getContext().hasSameType(FirstParam->getType(), 11764 SecondParam->getType()) && 11765 "Merged function has different parameter types."); 11766 11767 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11768 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11769 ParameterName) 11770 << I + 1 << FirstParam->getDeclName(); 11771 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11772 ParameterName) 11773 << I + 1 << SecondParam->getDeclName(); 11774 ParameterMismatch = true; 11775 break; 11776 }; 11777 11778 QualType FirstParamType = FirstParam->getType(); 11779 QualType SecondParamType = SecondParam->getType(); 11780 if (FirstParamType != SecondParamType && 11781 ComputeQualTypeODRHash(FirstParamType) != 11782 ComputeQualTypeODRHash(SecondParamType)) { 11783 if (const DecayedType *ParamDecayedType = 11784 FirstParamType->getAs<DecayedType>()) { 11785 ODRDiagError(FirstParam->getLocation(), 11786 FirstParam->getSourceRange(), ParameterType) 11787 << (I + 1) << FirstParamType << true 11788 << ParamDecayedType->getOriginalType(); 11789 } else { 11790 ODRDiagError(FirstParam->getLocation(), 11791 FirstParam->getSourceRange(), ParameterType) 11792 << (I + 1) << FirstParamType << false; 11793 } 11794 11795 if (const DecayedType *ParamDecayedType = 11796 SecondParamType->getAs<DecayedType>()) { 11797 ODRDiagNote(SecondParam->getLocation(), 11798 SecondParam->getSourceRange(), ParameterType) 11799 << (I + 1) << SecondParamType << true 11800 << ParamDecayedType->getOriginalType(); 11801 } else { 11802 ODRDiagNote(SecondParam->getLocation(), 11803 SecondParam->getSourceRange(), ParameterType) 11804 << (I + 1) << SecondParamType << false; 11805 } 11806 ParameterMismatch = true; 11807 break; 11808 } 11809 11810 const Expr *FirstInit = FirstParam->getInit(); 11811 const Expr *SecondInit = SecondParam->getInit(); 11812 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11813 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11814 ParameterSingleDefaultArgument) 11815 << (I + 1) << (FirstInit == nullptr) 11816 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11817 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11818 ParameterSingleDefaultArgument) 11819 << (I + 1) << (SecondInit == nullptr) 11820 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11821 ParameterMismatch = true; 11822 break; 11823 } 11824 11825 if (FirstInit && SecondInit && 11826 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11827 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11828 ParameterDifferentDefaultArgument) 11829 << (I + 1) << FirstInit->getSourceRange(); 11830 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11831 ParameterDifferentDefaultArgument) 11832 << (I + 1) << SecondInit->getSourceRange(); 11833 ParameterMismatch = true; 11834 break; 11835 } 11836 11837 assert(ComputeSubDeclODRHash(FirstParam) == 11838 ComputeSubDeclODRHash(SecondParam) && 11839 "Undiagnosed parameter difference."); 11840 } 11841 11842 if (ParameterMismatch) { 11843 Diagnosed = true; 11844 break; 11845 } 11846 11847 // If no error has been generated before now, assume the problem is in 11848 // the body and generate a message. 11849 ODRDiagError(FirstFunction->getLocation(), 11850 FirstFunction->getSourceRange(), FunctionBody); 11851 ODRDiagNote(SecondFunction->getLocation(), 11852 SecondFunction->getSourceRange(), FunctionBody); 11853 Diagnosed = true; 11854 break; 11855 } 11856 (void)Diagnosed; 11857 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11858 } 11859 11860 // Issue ODR failures diagnostics for enums. 11861 for (auto &Merge : EnumOdrMergeFailures) { 11862 enum ODREnumDifference { 11863 SingleScopedEnum, 11864 EnumTagKeywordMismatch, 11865 SingleSpecifiedType, 11866 DifferentSpecifiedTypes, 11867 DifferentNumberEnumConstants, 11868 EnumConstantName, 11869 EnumConstantSingleInitilizer, 11870 EnumConstantDifferentInitilizer, 11871 }; 11872 11873 // If we've already pointed out a specific problem with this enum, don't 11874 // bother issuing a general "something's different" diagnostic. 11875 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11876 continue; 11877 11878 EnumDecl *FirstEnum = Merge.first; 11879 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11880 11881 using DeclHashes = 11882 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11883 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11884 DeclHashes &Hashes, EnumDecl *Enum) { 11885 for (auto *D : Enum->decls()) { 11886 // Due to decl merging, the first EnumDecl is the parent of 11887 // Decls in both records. 11888 if (!ODRHash::isWhitelistedDecl(D, FirstEnum)) 11889 continue; 11890 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11891 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11892 ComputeSubDeclODRHash(D)); 11893 } 11894 }; 11895 DeclHashes FirstHashes; 11896 PopulateHashes(FirstHashes, FirstEnum); 11897 bool Diagnosed = false; 11898 for (auto &SecondEnum : Merge.second) { 11899 11900 if (FirstEnum == SecondEnum) 11901 continue; 11902 11903 std::string SecondModule = 11904 getOwningModuleNameForDiagnostic(SecondEnum); 11905 11906 auto ODRDiagError = [FirstEnum, &FirstModule, 11907 this](SourceLocation Loc, SourceRange Range, 11908 ODREnumDifference DiffType) { 11909 return Diag(Loc, diag::err_module_odr_violation_enum) 11910 << FirstEnum << FirstModule.empty() << FirstModule << Range 11911 << DiffType; 11912 }; 11913 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11914 SourceRange Range, 11915 ODREnumDifference DiffType) { 11916 return Diag(Loc, diag::note_module_odr_violation_enum) 11917 << SecondModule << Range << DiffType; 11918 }; 11919 11920 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11921 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11922 SingleScopedEnum) 11923 << FirstEnum->isScoped(); 11924 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11925 SingleScopedEnum) 11926 << SecondEnum->isScoped(); 11927 Diagnosed = true; 11928 continue; 11929 } 11930 11931 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11932 if (FirstEnum->isScopedUsingClassTag() != 11933 SecondEnum->isScopedUsingClassTag()) { 11934 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11935 EnumTagKeywordMismatch) 11936 << FirstEnum->isScopedUsingClassTag(); 11937 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11938 EnumTagKeywordMismatch) 11939 << SecondEnum->isScopedUsingClassTag(); 11940 Diagnosed = true; 11941 continue; 11942 } 11943 } 11944 11945 QualType FirstUnderlyingType = 11946 FirstEnum->getIntegerTypeSourceInfo() 11947 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11948 : QualType(); 11949 QualType SecondUnderlyingType = 11950 SecondEnum->getIntegerTypeSourceInfo() 11951 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11952 : QualType(); 11953 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11954 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11955 SingleSpecifiedType) 11956 << !FirstUnderlyingType.isNull(); 11957 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11958 SingleSpecifiedType) 11959 << !SecondUnderlyingType.isNull(); 11960 Diagnosed = true; 11961 continue; 11962 } 11963 11964 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11965 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11966 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11967 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11968 DifferentSpecifiedTypes) 11969 << FirstUnderlyingType; 11970 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11971 DifferentSpecifiedTypes) 11972 << SecondUnderlyingType; 11973 Diagnosed = true; 11974 continue; 11975 } 11976 } 11977 11978 DeclHashes SecondHashes; 11979 PopulateHashes(SecondHashes, SecondEnum); 11980 11981 if (FirstHashes.size() != SecondHashes.size()) { 11982 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11983 DifferentNumberEnumConstants) 11984 << (int)FirstHashes.size(); 11985 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11986 DifferentNumberEnumConstants) 11987 << (int)SecondHashes.size(); 11988 Diagnosed = true; 11989 continue; 11990 } 11991 11992 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11993 if (FirstHashes[I].second == SecondHashes[I].second) 11994 continue; 11995 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11996 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11997 11998 if (FirstEnumConstant->getDeclName() != 11999 SecondEnumConstant->getDeclName()) { 12000 12001 ODRDiagError(FirstEnumConstant->getLocation(), 12002 FirstEnumConstant->getSourceRange(), EnumConstantName) 12003 << I + 1 << FirstEnumConstant; 12004 ODRDiagNote(SecondEnumConstant->getLocation(), 12005 SecondEnumConstant->getSourceRange(), EnumConstantName) 12006 << I + 1 << SecondEnumConstant; 12007 Diagnosed = true; 12008 break; 12009 } 12010 12011 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 12012 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 12013 if (!FirstInit && !SecondInit) 12014 continue; 12015 12016 if (!FirstInit || !SecondInit) { 12017 ODRDiagError(FirstEnumConstant->getLocation(), 12018 FirstEnumConstant->getSourceRange(), 12019 EnumConstantSingleInitilizer) 12020 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 12021 ODRDiagNote(SecondEnumConstant->getLocation(), 12022 SecondEnumConstant->getSourceRange(), 12023 EnumConstantSingleInitilizer) 12024 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 12025 Diagnosed = true; 12026 break; 12027 } 12028 12029 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 12030 ODRDiagError(FirstEnumConstant->getLocation(), 12031 FirstEnumConstant->getSourceRange(), 12032 EnumConstantDifferentInitilizer) 12033 << I + 1 << FirstEnumConstant; 12034 ODRDiagNote(SecondEnumConstant->getLocation(), 12035 SecondEnumConstant->getSourceRange(), 12036 EnumConstantDifferentInitilizer) 12037 << I + 1 << SecondEnumConstant; 12038 Diagnosed = true; 12039 break; 12040 } 12041 } 12042 } 12043 12044 (void)Diagnosed; 12045 assert(Diagnosed && "Unable to emit ODR diagnostic."); 12046 } 12047 } 12048 12049 void ASTReader::StartedDeserializing() { 12050 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 12051 ReadTimer->startTimer(); 12052 } 12053 12054 void ASTReader::FinishedDeserializing() { 12055 assert(NumCurrentElementsDeserializing && 12056 "FinishedDeserializing not paired with StartedDeserializing"); 12057 if (NumCurrentElementsDeserializing == 1) { 12058 // We decrease NumCurrentElementsDeserializing only after pending actions 12059 // are finished, to avoid recursively re-calling finishPendingActions(). 12060 finishPendingActions(); 12061 } 12062 --NumCurrentElementsDeserializing; 12063 12064 if (NumCurrentElementsDeserializing == 0) { 12065 // Propagate exception specification and deduced type updates along 12066 // redeclaration chains. 12067 // 12068 // We do this now rather than in finishPendingActions because we want to 12069 // be able to walk the complete redeclaration chains of the updated decls. 12070 while (!PendingExceptionSpecUpdates.empty() || 12071 !PendingDeducedTypeUpdates.empty()) { 12072 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 12073 PendingExceptionSpecUpdates.clear(); 12074 for (auto Update : ESUpdates) { 12075 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 12076 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 12077 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 12078 if (auto *Listener = getContext().getASTMutationListener()) 12079 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 12080 for (auto *Redecl : Update.second->redecls()) 12081 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 12082 } 12083 12084 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 12085 PendingDeducedTypeUpdates.clear(); 12086 for (auto Update : DTUpdates) { 12087 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 12088 // FIXME: If the return type is already deduced, check that it matches. 12089 getContext().adjustDeducedFunctionResultType(Update.first, 12090 Update.second); 12091 } 12092 } 12093 12094 if (ReadTimer) 12095 ReadTimer->stopTimer(); 12096 12097 diagnoseOdrViolations(); 12098 12099 // We are not in recursive loading, so it's safe to pass the "interesting" 12100 // decls to the consumer. 12101 if (Consumer) 12102 PassInterestingDeclsToConsumer(); 12103 } 12104 } 12105 12106 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 12107 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 12108 // Remove any fake results before adding any real ones. 12109 auto It = PendingFakeLookupResults.find(II); 12110 if (It != PendingFakeLookupResults.end()) { 12111 for (auto *ND : It->second) 12112 SemaObj->IdResolver.RemoveDecl(ND); 12113 // FIXME: this works around module+PCH performance issue. 12114 // Rather than erase the result from the map, which is O(n), just clear 12115 // the vector of NamedDecls. 12116 It->second.clear(); 12117 } 12118 } 12119 12120 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 12121 SemaObj->TUScope->AddDecl(D); 12122 } else if (SemaObj->TUScope) { 12123 // Adding the decl to IdResolver may have failed because it was already in 12124 // (even though it was not added in scope). If it is already in, make sure 12125 // it gets in the scope as well. 12126 if (std::find(SemaObj->IdResolver.begin(Name), 12127 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 12128 SemaObj->TUScope->AddDecl(D); 12129 } 12130 } 12131 12132 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 12133 ASTContext *Context, 12134 const PCHContainerReader &PCHContainerRdr, 12135 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 12136 StringRef isysroot, bool DisableValidation, 12137 bool AllowASTWithCompilerErrors, 12138 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 12139 bool UseGlobalIndex, 12140 std::unique_ptr<llvm::Timer> ReadTimer) 12141 : Listener(DisableValidation 12142 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 12143 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 12144 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 12145 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 12146 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 12147 PCHContainerRdr, PP.getHeaderSearchInfo()), 12148 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 12149 DisableValidation(DisableValidation), 12150 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 12151 AllowConfigurationMismatch(AllowConfigurationMismatch), 12152 ValidateSystemInputs(ValidateSystemInputs), 12153 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 12154 SourceMgr.setExternalSLocEntrySource(this); 12155 12156 for (const auto &Ext : Extensions) { 12157 auto BlockName = Ext->getExtensionMetadata().BlockName; 12158 auto Known = ModuleFileExtensions.find(BlockName); 12159 if (Known != ModuleFileExtensions.end()) { 12160 Diags.Report(diag::warn_duplicate_module_file_extension) 12161 << BlockName; 12162 continue; 12163 } 12164 12165 ModuleFileExtensions.insert({BlockName, Ext}); 12166 } 12167 } 12168 12169 ASTReader::~ASTReader() { 12170 if (OwnsDeserializationListener) 12171 delete DeserializationListener; 12172 } 12173 12174 IdentifierResolver &ASTReader::getIdResolver() { 12175 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 12176 } 12177 12178 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 12179 unsigned AbbrevID) { 12180 Idx = 0; 12181 Record.clear(); 12182 return Cursor.readRecord(AbbrevID, Record); 12183 } 12184 //===----------------------------------------------------------------------===// 12185 //// OMPClauseReader implementation 12186 ////===----------------------------------------------------------------------===// 12187 12188 OMPClause *OMPClauseReader::readClause() { 12189 OMPClause *C; 12190 switch (Record.readInt()) { 12191 case OMPC_if: 12192 C = new (Context) OMPIfClause(); 12193 break; 12194 case OMPC_final: 12195 C = new (Context) OMPFinalClause(); 12196 break; 12197 case OMPC_num_threads: 12198 C = new (Context) OMPNumThreadsClause(); 12199 break; 12200 case OMPC_safelen: 12201 C = new (Context) OMPSafelenClause(); 12202 break; 12203 case OMPC_simdlen: 12204 C = new (Context) OMPSimdlenClause(); 12205 break; 12206 case OMPC_allocator: 12207 C = new (Context) OMPAllocatorClause(); 12208 break; 12209 case OMPC_collapse: 12210 C = new (Context) OMPCollapseClause(); 12211 break; 12212 case OMPC_default: 12213 C = new (Context) OMPDefaultClause(); 12214 break; 12215 case OMPC_proc_bind: 12216 C = new (Context) OMPProcBindClause(); 12217 break; 12218 case OMPC_schedule: 12219 C = new (Context) OMPScheduleClause(); 12220 break; 12221 case OMPC_ordered: 12222 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 12223 break; 12224 case OMPC_nowait: 12225 C = new (Context) OMPNowaitClause(); 12226 break; 12227 case OMPC_untied: 12228 C = new (Context) OMPUntiedClause(); 12229 break; 12230 case OMPC_mergeable: 12231 C = new (Context) OMPMergeableClause(); 12232 break; 12233 case OMPC_read: 12234 C = new (Context) OMPReadClause(); 12235 break; 12236 case OMPC_write: 12237 C = new (Context) OMPWriteClause(); 12238 break; 12239 case OMPC_update: 12240 C = new (Context) OMPUpdateClause(); 12241 break; 12242 case OMPC_capture: 12243 C = new (Context) OMPCaptureClause(); 12244 break; 12245 case OMPC_seq_cst: 12246 C = new (Context) OMPSeqCstClause(); 12247 break; 12248 case OMPC_threads: 12249 C = new (Context) OMPThreadsClause(); 12250 break; 12251 case OMPC_simd: 12252 C = new (Context) OMPSIMDClause(); 12253 break; 12254 case OMPC_nogroup: 12255 C = new (Context) OMPNogroupClause(); 12256 break; 12257 case OMPC_unified_address: 12258 C = new (Context) OMPUnifiedAddressClause(); 12259 break; 12260 case OMPC_unified_shared_memory: 12261 C = new (Context) OMPUnifiedSharedMemoryClause(); 12262 break; 12263 case OMPC_reverse_offload: 12264 C = new (Context) OMPReverseOffloadClause(); 12265 break; 12266 case OMPC_dynamic_allocators: 12267 C = new (Context) OMPDynamicAllocatorsClause(); 12268 break; 12269 case OMPC_atomic_default_mem_order: 12270 C = new (Context) OMPAtomicDefaultMemOrderClause(); 12271 break; 12272 case OMPC_private: 12273 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 12274 break; 12275 case OMPC_firstprivate: 12276 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 12277 break; 12278 case OMPC_lastprivate: 12279 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 12280 break; 12281 case OMPC_shared: 12282 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 12283 break; 12284 case OMPC_reduction: 12285 C = OMPReductionClause::CreateEmpty(Context, Record.readInt()); 12286 break; 12287 case OMPC_task_reduction: 12288 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 12289 break; 12290 case OMPC_in_reduction: 12291 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 12292 break; 12293 case OMPC_linear: 12294 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 12295 break; 12296 case OMPC_aligned: 12297 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 12298 break; 12299 case OMPC_copyin: 12300 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 12301 break; 12302 case OMPC_copyprivate: 12303 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 12304 break; 12305 case OMPC_flush: 12306 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 12307 break; 12308 case OMPC_depend: { 12309 unsigned NumVars = Record.readInt(); 12310 unsigned NumLoops = Record.readInt(); 12311 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 12312 break; 12313 } 12314 case OMPC_device: 12315 C = new (Context) OMPDeviceClause(); 12316 break; 12317 case OMPC_map: { 12318 OMPMappableExprListSizeTy Sizes; 12319 Sizes.NumVars = Record.readInt(); 12320 Sizes.NumUniqueDeclarations = Record.readInt(); 12321 Sizes.NumComponentLists = Record.readInt(); 12322 Sizes.NumComponents = Record.readInt(); 12323 C = OMPMapClause::CreateEmpty(Context, Sizes); 12324 break; 12325 } 12326 case OMPC_num_teams: 12327 C = new (Context) OMPNumTeamsClause(); 12328 break; 12329 case OMPC_thread_limit: 12330 C = new (Context) OMPThreadLimitClause(); 12331 break; 12332 case OMPC_priority: 12333 C = new (Context) OMPPriorityClause(); 12334 break; 12335 case OMPC_grainsize: 12336 C = new (Context) OMPGrainsizeClause(); 12337 break; 12338 case OMPC_num_tasks: 12339 C = new (Context) OMPNumTasksClause(); 12340 break; 12341 case OMPC_hint: 12342 C = new (Context) OMPHintClause(); 12343 break; 12344 case OMPC_dist_schedule: 12345 C = new (Context) OMPDistScheduleClause(); 12346 break; 12347 case OMPC_defaultmap: 12348 C = new (Context) OMPDefaultmapClause(); 12349 break; 12350 case OMPC_to: { 12351 OMPMappableExprListSizeTy Sizes; 12352 Sizes.NumVars = Record.readInt(); 12353 Sizes.NumUniqueDeclarations = Record.readInt(); 12354 Sizes.NumComponentLists = Record.readInt(); 12355 Sizes.NumComponents = Record.readInt(); 12356 C = OMPToClause::CreateEmpty(Context, Sizes); 12357 break; 12358 } 12359 case OMPC_from: { 12360 OMPMappableExprListSizeTy Sizes; 12361 Sizes.NumVars = Record.readInt(); 12362 Sizes.NumUniqueDeclarations = Record.readInt(); 12363 Sizes.NumComponentLists = Record.readInt(); 12364 Sizes.NumComponents = Record.readInt(); 12365 C = OMPFromClause::CreateEmpty(Context, Sizes); 12366 break; 12367 } 12368 case OMPC_use_device_ptr: { 12369 OMPMappableExprListSizeTy Sizes; 12370 Sizes.NumVars = Record.readInt(); 12371 Sizes.NumUniqueDeclarations = Record.readInt(); 12372 Sizes.NumComponentLists = Record.readInt(); 12373 Sizes.NumComponents = Record.readInt(); 12374 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 12375 break; 12376 } 12377 case OMPC_is_device_ptr: { 12378 OMPMappableExprListSizeTy Sizes; 12379 Sizes.NumVars = Record.readInt(); 12380 Sizes.NumUniqueDeclarations = Record.readInt(); 12381 Sizes.NumComponentLists = Record.readInt(); 12382 Sizes.NumComponents = Record.readInt(); 12383 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 12384 break; 12385 } 12386 case OMPC_allocate: 12387 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 12388 break; 12389 } 12390 Visit(C); 12391 C->setLocStart(Record.readSourceLocation()); 12392 C->setLocEnd(Record.readSourceLocation()); 12393 12394 return C; 12395 } 12396 12397 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 12398 C->setPreInitStmt(Record.readSubStmt(), 12399 static_cast<OpenMPDirectiveKind>(Record.readInt())); 12400 } 12401 12402 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 12403 VisitOMPClauseWithPreInit(C); 12404 C->setPostUpdateExpr(Record.readSubExpr()); 12405 } 12406 12407 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 12408 VisitOMPClauseWithPreInit(C); 12409 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 12410 C->setNameModifierLoc(Record.readSourceLocation()); 12411 C->setColonLoc(Record.readSourceLocation()); 12412 C->setCondition(Record.readSubExpr()); 12413 C->setLParenLoc(Record.readSourceLocation()); 12414 } 12415 12416 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12417 C->setCondition(Record.readSubExpr()); 12418 C->setLParenLoc(Record.readSourceLocation()); 12419 } 12420 12421 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12422 VisitOMPClauseWithPreInit(C); 12423 C->setNumThreads(Record.readSubExpr()); 12424 C->setLParenLoc(Record.readSourceLocation()); 12425 } 12426 12427 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12428 C->setSafelen(Record.readSubExpr()); 12429 C->setLParenLoc(Record.readSourceLocation()); 12430 } 12431 12432 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12433 C->setSimdlen(Record.readSubExpr()); 12434 C->setLParenLoc(Record.readSourceLocation()); 12435 } 12436 12437 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12438 C->setAllocator(Record.readExpr()); 12439 C->setLParenLoc(Record.readSourceLocation()); 12440 } 12441 12442 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12443 C->setNumForLoops(Record.readSubExpr()); 12444 C->setLParenLoc(Record.readSourceLocation()); 12445 } 12446 12447 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12448 C->setDefaultKind( 12449 static_cast<OpenMPDefaultClauseKind>(Record.readInt())); 12450 C->setLParenLoc(Record.readSourceLocation()); 12451 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12452 } 12453 12454 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12455 C->setProcBindKind( 12456 static_cast<OpenMPProcBindClauseKind>(Record.readInt())); 12457 C->setLParenLoc(Record.readSourceLocation()); 12458 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12459 } 12460 12461 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12462 VisitOMPClauseWithPreInit(C); 12463 C->setScheduleKind( 12464 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12465 C->setFirstScheduleModifier( 12466 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12467 C->setSecondScheduleModifier( 12468 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12469 C->setChunkSize(Record.readSubExpr()); 12470 C->setLParenLoc(Record.readSourceLocation()); 12471 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12472 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12473 C->setScheduleKindLoc(Record.readSourceLocation()); 12474 C->setCommaLoc(Record.readSourceLocation()); 12475 } 12476 12477 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12478 C->setNumForLoops(Record.readSubExpr()); 12479 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12480 C->setLoopNumIterations(I, Record.readSubExpr()); 12481 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12482 C->setLoopCounter(I, Record.readSubExpr()); 12483 C->setLParenLoc(Record.readSourceLocation()); 12484 } 12485 12486 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12487 12488 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12489 12490 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12491 12492 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12493 12494 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12495 12496 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *) {} 12497 12498 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12499 12500 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12501 12502 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12503 12504 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12505 12506 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12507 12508 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12509 12510 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12511 OMPUnifiedSharedMemoryClause *) {} 12512 12513 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12514 12515 void 12516 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12517 } 12518 12519 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12520 OMPAtomicDefaultMemOrderClause *C) { 12521 C->setAtomicDefaultMemOrderKind( 12522 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12523 C->setLParenLoc(Record.readSourceLocation()); 12524 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12525 } 12526 12527 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12528 C->setLParenLoc(Record.readSourceLocation()); 12529 unsigned NumVars = C->varlist_size(); 12530 SmallVector<Expr *, 16> Vars; 12531 Vars.reserve(NumVars); 12532 for (unsigned i = 0; i != NumVars; ++i) 12533 Vars.push_back(Record.readSubExpr()); 12534 C->setVarRefs(Vars); 12535 Vars.clear(); 12536 for (unsigned i = 0; i != NumVars; ++i) 12537 Vars.push_back(Record.readSubExpr()); 12538 C->setPrivateCopies(Vars); 12539 } 12540 12541 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12542 VisitOMPClauseWithPreInit(C); 12543 C->setLParenLoc(Record.readSourceLocation()); 12544 unsigned NumVars = C->varlist_size(); 12545 SmallVector<Expr *, 16> Vars; 12546 Vars.reserve(NumVars); 12547 for (unsigned i = 0; i != NumVars; ++i) 12548 Vars.push_back(Record.readSubExpr()); 12549 C->setVarRefs(Vars); 12550 Vars.clear(); 12551 for (unsigned i = 0; i != NumVars; ++i) 12552 Vars.push_back(Record.readSubExpr()); 12553 C->setPrivateCopies(Vars); 12554 Vars.clear(); 12555 for (unsigned i = 0; i != NumVars; ++i) 12556 Vars.push_back(Record.readSubExpr()); 12557 C->setInits(Vars); 12558 } 12559 12560 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12561 VisitOMPClauseWithPostUpdate(C); 12562 C->setLParenLoc(Record.readSourceLocation()); 12563 unsigned NumVars = C->varlist_size(); 12564 SmallVector<Expr *, 16> Vars; 12565 Vars.reserve(NumVars); 12566 for (unsigned i = 0; i != NumVars; ++i) 12567 Vars.push_back(Record.readSubExpr()); 12568 C->setVarRefs(Vars); 12569 Vars.clear(); 12570 for (unsigned i = 0; i != NumVars; ++i) 12571 Vars.push_back(Record.readSubExpr()); 12572 C->setPrivateCopies(Vars); 12573 Vars.clear(); 12574 for (unsigned i = 0; i != NumVars; ++i) 12575 Vars.push_back(Record.readSubExpr()); 12576 C->setSourceExprs(Vars); 12577 Vars.clear(); 12578 for (unsigned i = 0; i != NumVars; ++i) 12579 Vars.push_back(Record.readSubExpr()); 12580 C->setDestinationExprs(Vars); 12581 Vars.clear(); 12582 for (unsigned i = 0; i != NumVars; ++i) 12583 Vars.push_back(Record.readSubExpr()); 12584 C->setAssignmentOps(Vars); 12585 } 12586 12587 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12588 C->setLParenLoc(Record.readSourceLocation()); 12589 unsigned NumVars = C->varlist_size(); 12590 SmallVector<Expr *, 16> Vars; 12591 Vars.reserve(NumVars); 12592 for (unsigned i = 0; i != NumVars; ++i) 12593 Vars.push_back(Record.readSubExpr()); 12594 C->setVarRefs(Vars); 12595 } 12596 12597 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12598 VisitOMPClauseWithPostUpdate(C); 12599 C->setLParenLoc(Record.readSourceLocation()); 12600 C->setColonLoc(Record.readSourceLocation()); 12601 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12602 DeclarationNameInfo DNI; 12603 Record.readDeclarationNameInfo(DNI); 12604 C->setQualifierLoc(NNSL); 12605 C->setNameInfo(DNI); 12606 12607 unsigned NumVars = C->varlist_size(); 12608 SmallVector<Expr *, 16> Vars; 12609 Vars.reserve(NumVars); 12610 for (unsigned i = 0; i != NumVars; ++i) 12611 Vars.push_back(Record.readSubExpr()); 12612 C->setVarRefs(Vars); 12613 Vars.clear(); 12614 for (unsigned i = 0; i != NumVars; ++i) 12615 Vars.push_back(Record.readSubExpr()); 12616 C->setPrivates(Vars); 12617 Vars.clear(); 12618 for (unsigned i = 0; i != NumVars; ++i) 12619 Vars.push_back(Record.readSubExpr()); 12620 C->setLHSExprs(Vars); 12621 Vars.clear(); 12622 for (unsigned i = 0; i != NumVars; ++i) 12623 Vars.push_back(Record.readSubExpr()); 12624 C->setRHSExprs(Vars); 12625 Vars.clear(); 12626 for (unsigned i = 0; i != NumVars; ++i) 12627 Vars.push_back(Record.readSubExpr()); 12628 C->setReductionOps(Vars); 12629 } 12630 12631 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12632 VisitOMPClauseWithPostUpdate(C); 12633 C->setLParenLoc(Record.readSourceLocation()); 12634 C->setColonLoc(Record.readSourceLocation()); 12635 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12636 DeclarationNameInfo DNI; 12637 Record.readDeclarationNameInfo(DNI); 12638 C->setQualifierLoc(NNSL); 12639 C->setNameInfo(DNI); 12640 12641 unsigned NumVars = C->varlist_size(); 12642 SmallVector<Expr *, 16> Vars; 12643 Vars.reserve(NumVars); 12644 for (unsigned I = 0; I != NumVars; ++I) 12645 Vars.push_back(Record.readSubExpr()); 12646 C->setVarRefs(Vars); 12647 Vars.clear(); 12648 for (unsigned I = 0; I != NumVars; ++I) 12649 Vars.push_back(Record.readSubExpr()); 12650 C->setPrivates(Vars); 12651 Vars.clear(); 12652 for (unsigned I = 0; I != NumVars; ++I) 12653 Vars.push_back(Record.readSubExpr()); 12654 C->setLHSExprs(Vars); 12655 Vars.clear(); 12656 for (unsigned I = 0; I != NumVars; ++I) 12657 Vars.push_back(Record.readSubExpr()); 12658 C->setRHSExprs(Vars); 12659 Vars.clear(); 12660 for (unsigned I = 0; I != NumVars; ++I) 12661 Vars.push_back(Record.readSubExpr()); 12662 C->setReductionOps(Vars); 12663 } 12664 12665 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12666 VisitOMPClauseWithPostUpdate(C); 12667 C->setLParenLoc(Record.readSourceLocation()); 12668 C->setColonLoc(Record.readSourceLocation()); 12669 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12670 DeclarationNameInfo DNI; 12671 Record.readDeclarationNameInfo(DNI); 12672 C->setQualifierLoc(NNSL); 12673 C->setNameInfo(DNI); 12674 12675 unsigned NumVars = C->varlist_size(); 12676 SmallVector<Expr *, 16> Vars; 12677 Vars.reserve(NumVars); 12678 for (unsigned I = 0; I != NumVars; ++I) 12679 Vars.push_back(Record.readSubExpr()); 12680 C->setVarRefs(Vars); 12681 Vars.clear(); 12682 for (unsigned I = 0; I != NumVars; ++I) 12683 Vars.push_back(Record.readSubExpr()); 12684 C->setPrivates(Vars); 12685 Vars.clear(); 12686 for (unsigned I = 0; I != NumVars; ++I) 12687 Vars.push_back(Record.readSubExpr()); 12688 C->setLHSExprs(Vars); 12689 Vars.clear(); 12690 for (unsigned I = 0; I != NumVars; ++I) 12691 Vars.push_back(Record.readSubExpr()); 12692 C->setRHSExprs(Vars); 12693 Vars.clear(); 12694 for (unsigned I = 0; I != NumVars; ++I) 12695 Vars.push_back(Record.readSubExpr()); 12696 C->setReductionOps(Vars); 12697 Vars.clear(); 12698 for (unsigned I = 0; I != NumVars; ++I) 12699 Vars.push_back(Record.readSubExpr()); 12700 C->setTaskgroupDescriptors(Vars); 12701 } 12702 12703 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12704 VisitOMPClauseWithPostUpdate(C); 12705 C->setLParenLoc(Record.readSourceLocation()); 12706 C->setColonLoc(Record.readSourceLocation()); 12707 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12708 C->setModifierLoc(Record.readSourceLocation()); 12709 unsigned NumVars = C->varlist_size(); 12710 SmallVector<Expr *, 16> Vars; 12711 Vars.reserve(NumVars); 12712 for (unsigned i = 0; i != NumVars; ++i) 12713 Vars.push_back(Record.readSubExpr()); 12714 C->setVarRefs(Vars); 12715 Vars.clear(); 12716 for (unsigned i = 0; i != NumVars; ++i) 12717 Vars.push_back(Record.readSubExpr()); 12718 C->setPrivates(Vars); 12719 Vars.clear(); 12720 for (unsigned i = 0; i != NumVars; ++i) 12721 Vars.push_back(Record.readSubExpr()); 12722 C->setInits(Vars); 12723 Vars.clear(); 12724 for (unsigned i = 0; i != NumVars; ++i) 12725 Vars.push_back(Record.readSubExpr()); 12726 C->setUpdates(Vars); 12727 Vars.clear(); 12728 for (unsigned i = 0; i != NumVars; ++i) 12729 Vars.push_back(Record.readSubExpr()); 12730 C->setFinals(Vars); 12731 C->setStep(Record.readSubExpr()); 12732 C->setCalcStep(Record.readSubExpr()); 12733 } 12734 12735 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12736 C->setLParenLoc(Record.readSourceLocation()); 12737 C->setColonLoc(Record.readSourceLocation()); 12738 unsigned NumVars = C->varlist_size(); 12739 SmallVector<Expr *, 16> Vars; 12740 Vars.reserve(NumVars); 12741 for (unsigned i = 0; i != NumVars; ++i) 12742 Vars.push_back(Record.readSubExpr()); 12743 C->setVarRefs(Vars); 12744 C->setAlignment(Record.readSubExpr()); 12745 } 12746 12747 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12748 C->setLParenLoc(Record.readSourceLocation()); 12749 unsigned NumVars = C->varlist_size(); 12750 SmallVector<Expr *, 16> Exprs; 12751 Exprs.reserve(NumVars); 12752 for (unsigned i = 0; i != NumVars; ++i) 12753 Exprs.push_back(Record.readSubExpr()); 12754 C->setVarRefs(Exprs); 12755 Exprs.clear(); 12756 for (unsigned i = 0; i != NumVars; ++i) 12757 Exprs.push_back(Record.readSubExpr()); 12758 C->setSourceExprs(Exprs); 12759 Exprs.clear(); 12760 for (unsigned i = 0; i != NumVars; ++i) 12761 Exprs.push_back(Record.readSubExpr()); 12762 C->setDestinationExprs(Exprs); 12763 Exprs.clear(); 12764 for (unsigned i = 0; i != NumVars; ++i) 12765 Exprs.push_back(Record.readSubExpr()); 12766 C->setAssignmentOps(Exprs); 12767 } 12768 12769 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12770 C->setLParenLoc(Record.readSourceLocation()); 12771 unsigned NumVars = C->varlist_size(); 12772 SmallVector<Expr *, 16> Exprs; 12773 Exprs.reserve(NumVars); 12774 for (unsigned i = 0; i != NumVars; ++i) 12775 Exprs.push_back(Record.readSubExpr()); 12776 C->setVarRefs(Exprs); 12777 Exprs.clear(); 12778 for (unsigned i = 0; i != NumVars; ++i) 12779 Exprs.push_back(Record.readSubExpr()); 12780 C->setSourceExprs(Exprs); 12781 Exprs.clear(); 12782 for (unsigned i = 0; i != NumVars; ++i) 12783 Exprs.push_back(Record.readSubExpr()); 12784 C->setDestinationExprs(Exprs); 12785 Exprs.clear(); 12786 for (unsigned i = 0; i != NumVars; ++i) 12787 Exprs.push_back(Record.readSubExpr()); 12788 C->setAssignmentOps(Exprs); 12789 } 12790 12791 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12792 C->setLParenLoc(Record.readSourceLocation()); 12793 unsigned NumVars = C->varlist_size(); 12794 SmallVector<Expr *, 16> Vars; 12795 Vars.reserve(NumVars); 12796 for (unsigned i = 0; i != NumVars; ++i) 12797 Vars.push_back(Record.readSubExpr()); 12798 C->setVarRefs(Vars); 12799 } 12800 12801 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12802 C->setLParenLoc(Record.readSourceLocation()); 12803 C->setDependencyKind( 12804 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12805 C->setDependencyLoc(Record.readSourceLocation()); 12806 C->setColonLoc(Record.readSourceLocation()); 12807 unsigned NumVars = C->varlist_size(); 12808 SmallVector<Expr *, 16> Vars; 12809 Vars.reserve(NumVars); 12810 for (unsigned I = 0; I != NumVars; ++I) 12811 Vars.push_back(Record.readSubExpr()); 12812 C->setVarRefs(Vars); 12813 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12814 C->setLoopData(I, Record.readSubExpr()); 12815 } 12816 12817 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12818 VisitOMPClauseWithPreInit(C); 12819 C->setDevice(Record.readSubExpr()); 12820 C->setLParenLoc(Record.readSourceLocation()); 12821 } 12822 12823 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12824 C->setLParenLoc(Record.readSourceLocation()); 12825 for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) { 12826 C->setMapTypeModifier( 12827 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12828 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12829 } 12830 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12831 DeclarationNameInfo DNI; 12832 Record.readDeclarationNameInfo(DNI); 12833 C->setMapperIdInfo(DNI); 12834 C->setMapType( 12835 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12836 C->setMapLoc(Record.readSourceLocation()); 12837 C->setColonLoc(Record.readSourceLocation()); 12838 auto NumVars = C->varlist_size(); 12839 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12840 auto TotalLists = C->getTotalComponentListNum(); 12841 auto TotalComponents = C->getTotalComponentsNum(); 12842 12843 SmallVector<Expr *, 16> Vars; 12844 Vars.reserve(NumVars); 12845 for (unsigned i = 0; i != NumVars; ++i) 12846 Vars.push_back(Record.readExpr()); 12847 C->setVarRefs(Vars); 12848 12849 SmallVector<Expr *, 16> UDMappers; 12850 UDMappers.reserve(NumVars); 12851 for (unsigned I = 0; I < NumVars; ++I) 12852 UDMappers.push_back(Record.readExpr()); 12853 C->setUDMapperRefs(UDMappers); 12854 12855 SmallVector<ValueDecl *, 16> Decls; 12856 Decls.reserve(UniqueDecls); 12857 for (unsigned i = 0; i < UniqueDecls; ++i) 12858 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12859 C->setUniqueDecls(Decls); 12860 12861 SmallVector<unsigned, 16> ListsPerDecl; 12862 ListsPerDecl.reserve(UniqueDecls); 12863 for (unsigned i = 0; i < UniqueDecls; ++i) 12864 ListsPerDecl.push_back(Record.readInt()); 12865 C->setDeclNumLists(ListsPerDecl); 12866 12867 SmallVector<unsigned, 32> ListSizes; 12868 ListSizes.reserve(TotalLists); 12869 for (unsigned i = 0; i < TotalLists; ++i) 12870 ListSizes.push_back(Record.readInt()); 12871 C->setComponentListSizes(ListSizes); 12872 12873 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12874 Components.reserve(TotalComponents); 12875 for (unsigned i = 0; i < TotalComponents; ++i) { 12876 Expr *AssociatedExpr = Record.readExpr(); 12877 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12878 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12879 AssociatedExpr, AssociatedDecl)); 12880 } 12881 C->setComponents(Components, ListSizes); 12882 } 12883 12884 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12885 C->setLParenLoc(Record.readSourceLocation()); 12886 C->setColonLoc(Record.readSourceLocation()); 12887 C->setAllocator(Record.readSubExpr()); 12888 unsigned NumVars = C->varlist_size(); 12889 SmallVector<Expr *, 16> Vars; 12890 Vars.reserve(NumVars); 12891 for (unsigned i = 0; i != NumVars; ++i) 12892 Vars.push_back(Record.readSubExpr()); 12893 C->setVarRefs(Vars); 12894 } 12895 12896 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12897 VisitOMPClauseWithPreInit(C); 12898 C->setNumTeams(Record.readSubExpr()); 12899 C->setLParenLoc(Record.readSourceLocation()); 12900 } 12901 12902 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12903 VisitOMPClauseWithPreInit(C); 12904 C->setThreadLimit(Record.readSubExpr()); 12905 C->setLParenLoc(Record.readSourceLocation()); 12906 } 12907 12908 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12909 C->setPriority(Record.readSubExpr()); 12910 C->setLParenLoc(Record.readSourceLocation()); 12911 } 12912 12913 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12914 C->setGrainsize(Record.readSubExpr()); 12915 C->setLParenLoc(Record.readSourceLocation()); 12916 } 12917 12918 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12919 C->setNumTasks(Record.readSubExpr()); 12920 C->setLParenLoc(Record.readSourceLocation()); 12921 } 12922 12923 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12924 C->setHint(Record.readSubExpr()); 12925 C->setLParenLoc(Record.readSourceLocation()); 12926 } 12927 12928 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12929 VisitOMPClauseWithPreInit(C); 12930 C->setDistScheduleKind( 12931 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12932 C->setChunkSize(Record.readSubExpr()); 12933 C->setLParenLoc(Record.readSourceLocation()); 12934 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12935 C->setCommaLoc(Record.readSourceLocation()); 12936 } 12937 12938 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12939 C->setDefaultmapKind( 12940 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12941 C->setDefaultmapModifier( 12942 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12943 C->setLParenLoc(Record.readSourceLocation()); 12944 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12945 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12946 } 12947 12948 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12949 C->setLParenLoc(Record.readSourceLocation()); 12950 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12951 DeclarationNameInfo DNI; 12952 Record.readDeclarationNameInfo(DNI); 12953 C->setMapperIdInfo(DNI); 12954 auto NumVars = C->varlist_size(); 12955 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12956 auto TotalLists = C->getTotalComponentListNum(); 12957 auto TotalComponents = C->getTotalComponentsNum(); 12958 12959 SmallVector<Expr *, 16> Vars; 12960 Vars.reserve(NumVars); 12961 for (unsigned i = 0; i != NumVars; ++i) 12962 Vars.push_back(Record.readSubExpr()); 12963 C->setVarRefs(Vars); 12964 12965 SmallVector<Expr *, 16> UDMappers; 12966 UDMappers.reserve(NumVars); 12967 for (unsigned I = 0; I < NumVars; ++I) 12968 UDMappers.push_back(Record.readSubExpr()); 12969 C->setUDMapperRefs(UDMappers); 12970 12971 SmallVector<ValueDecl *, 16> Decls; 12972 Decls.reserve(UniqueDecls); 12973 for (unsigned i = 0; i < UniqueDecls; ++i) 12974 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12975 C->setUniqueDecls(Decls); 12976 12977 SmallVector<unsigned, 16> ListsPerDecl; 12978 ListsPerDecl.reserve(UniqueDecls); 12979 for (unsigned i = 0; i < UniqueDecls; ++i) 12980 ListsPerDecl.push_back(Record.readInt()); 12981 C->setDeclNumLists(ListsPerDecl); 12982 12983 SmallVector<unsigned, 32> ListSizes; 12984 ListSizes.reserve(TotalLists); 12985 for (unsigned i = 0; i < TotalLists; ++i) 12986 ListSizes.push_back(Record.readInt()); 12987 C->setComponentListSizes(ListSizes); 12988 12989 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12990 Components.reserve(TotalComponents); 12991 for (unsigned i = 0; i < TotalComponents; ++i) { 12992 Expr *AssociatedExpr = Record.readSubExpr(); 12993 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12994 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12995 AssociatedExpr, AssociatedDecl)); 12996 } 12997 C->setComponents(Components, ListSizes); 12998 } 12999 13000 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 13001 C->setLParenLoc(Record.readSourceLocation()); 13002 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 13003 DeclarationNameInfo DNI; 13004 Record.readDeclarationNameInfo(DNI); 13005 C->setMapperIdInfo(DNI); 13006 auto NumVars = C->varlist_size(); 13007 auto UniqueDecls = C->getUniqueDeclarationsNum(); 13008 auto TotalLists = C->getTotalComponentListNum(); 13009 auto TotalComponents = C->getTotalComponentsNum(); 13010 13011 SmallVector<Expr *, 16> Vars; 13012 Vars.reserve(NumVars); 13013 for (unsigned i = 0; i != NumVars; ++i) 13014 Vars.push_back(Record.readSubExpr()); 13015 C->setVarRefs(Vars); 13016 13017 SmallVector<Expr *, 16> UDMappers; 13018 UDMappers.reserve(NumVars); 13019 for (unsigned I = 0; I < NumVars; ++I) 13020 UDMappers.push_back(Record.readSubExpr()); 13021 C->setUDMapperRefs(UDMappers); 13022 13023 SmallVector<ValueDecl *, 16> Decls; 13024 Decls.reserve(UniqueDecls); 13025 for (unsigned i = 0; i < UniqueDecls; ++i) 13026 Decls.push_back(Record.readDeclAs<ValueDecl>()); 13027 C->setUniqueDecls(Decls); 13028 13029 SmallVector<unsigned, 16> ListsPerDecl; 13030 ListsPerDecl.reserve(UniqueDecls); 13031 for (unsigned i = 0; i < UniqueDecls; ++i) 13032 ListsPerDecl.push_back(Record.readInt()); 13033 C->setDeclNumLists(ListsPerDecl); 13034 13035 SmallVector<unsigned, 32> ListSizes; 13036 ListSizes.reserve(TotalLists); 13037 for (unsigned i = 0; i < TotalLists; ++i) 13038 ListSizes.push_back(Record.readInt()); 13039 C->setComponentListSizes(ListSizes); 13040 13041 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 13042 Components.reserve(TotalComponents); 13043 for (unsigned i = 0; i < TotalComponents; ++i) { 13044 Expr *AssociatedExpr = Record.readSubExpr(); 13045 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 13046 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 13047 AssociatedExpr, AssociatedDecl)); 13048 } 13049 C->setComponents(Components, ListSizes); 13050 } 13051 13052 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 13053 C->setLParenLoc(Record.readSourceLocation()); 13054 auto NumVars = C->varlist_size(); 13055 auto UniqueDecls = C->getUniqueDeclarationsNum(); 13056 auto TotalLists = C->getTotalComponentListNum(); 13057 auto TotalComponents = C->getTotalComponentsNum(); 13058 13059 SmallVector<Expr *, 16> Vars; 13060 Vars.reserve(NumVars); 13061 for (unsigned i = 0; i != NumVars; ++i) 13062 Vars.push_back(Record.readSubExpr()); 13063 C->setVarRefs(Vars); 13064 Vars.clear(); 13065 for (unsigned i = 0; i != NumVars; ++i) 13066 Vars.push_back(Record.readSubExpr()); 13067 C->setPrivateCopies(Vars); 13068 Vars.clear(); 13069 for (unsigned i = 0; i != NumVars; ++i) 13070 Vars.push_back(Record.readSubExpr()); 13071 C->setInits(Vars); 13072 13073 SmallVector<ValueDecl *, 16> Decls; 13074 Decls.reserve(UniqueDecls); 13075 for (unsigned i = 0; i < UniqueDecls; ++i) 13076 Decls.push_back(Record.readDeclAs<ValueDecl>()); 13077 C->setUniqueDecls(Decls); 13078 13079 SmallVector<unsigned, 16> ListsPerDecl; 13080 ListsPerDecl.reserve(UniqueDecls); 13081 for (unsigned i = 0; i < UniqueDecls; ++i) 13082 ListsPerDecl.push_back(Record.readInt()); 13083 C->setDeclNumLists(ListsPerDecl); 13084 13085 SmallVector<unsigned, 32> ListSizes; 13086 ListSizes.reserve(TotalLists); 13087 for (unsigned i = 0; i < TotalLists; ++i) 13088 ListSizes.push_back(Record.readInt()); 13089 C->setComponentListSizes(ListSizes); 13090 13091 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 13092 Components.reserve(TotalComponents); 13093 for (unsigned i = 0; i < TotalComponents; ++i) { 13094 Expr *AssociatedExpr = Record.readSubExpr(); 13095 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 13096 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 13097 AssociatedExpr, AssociatedDecl)); 13098 } 13099 C->setComponents(Components, ListSizes); 13100 } 13101 13102 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 13103 C->setLParenLoc(Record.readSourceLocation()); 13104 auto NumVars = C->varlist_size(); 13105 auto UniqueDecls = C->getUniqueDeclarationsNum(); 13106 auto TotalLists = C->getTotalComponentListNum(); 13107 auto TotalComponents = C->getTotalComponentsNum(); 13108 13109 SmallVector<Expr *, 16> Vars; 13110 Vars.reserve(NumVars); 13111 for (unsigned i = 0; i != NumVars; ++i) 13112 Vars.push_back(Record.readSubExpr()); 13113 C->setVarRefs(Vars); 13114 Vars.clear(); 13115 13116 SmallVector<ValueDecl *, 16> Decls; 13117 Decls.reserve(UniqueDecls); 13118 for (unsigned i = 0; i < UniqueDecls; ++i) 13119 Decls.push_back(Record.readDeclAs<ValueDecl>()); 13120 C->setUniqueDecls(Decls); 13121 13122 SmallVector<unsigned, 16> ListsPerDecl; 13123 ListsPerDecl.reserve(UniqueDecls); 13124 for (unsigned i = 0; i < UniqueDecls; ++i) 13125 ListsPerDecl.push_back(Record.readInt()); 13126 C->setDeclNumLists(ListsPerDecl); 13127 13128 SmallVector<unsigned, 32> ListSizes; 13129 ListSizes.reserve(TotalLists); 13130 for (unsigned i = 0; i < TotalLists; ++i) 13131 ListSizes.push_back(Record.readInt()); 13132 C->setComponentListSizes(ListSizes); 13133 13134 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 13135 Components.reserve(TotalComponents); 13136 for (unsigned i = 0; i < TotalComponents; ++i) { 13137 Expr *AssociatedExpr = Record.readSubExpr(); 13138 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 13139 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 13140 AssociatedExpr, AssociatedDecl)); 13141 } 13142 C->setComponents(Components, ListSizes); 13143 } 13144