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