1 //===- ASTReader.cpp - AST File Reader ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the ASTReader class, which reads AST files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/OpenMPKinds.h" 14 #include "clang/Serialization/ASTRecordReader.h" 15 #include "ASTCommon.h" 16 #include "ASTReaderInternals.h" 17 #include "clang/AST/AbstractTypeReader.h" 18 #include "clang/AST/ASTConsumer.h" 19 #include "clang/AST/ASTContext.h" 20 #include "clang/AST/ASTMutationListener.h" 21 #include "clang/AST/ASTUnresolvedSet.h" 22 #include "clang/AST/Decl.h" 23 #include "clang/AST/DeclBase.h" 24 #include "clang/AST/DeclCXX.h" 25 #include "clang/AST/DeclFriend.h" 26 #include "clang/AST/DeclGroup.h" 27 #include "clang/AST/DeclObjC.h" 28 #include "clang/AST/DeclTemplate.h" 29 #include "clang/AST/DeclarationName.h" 30 #include "clang/AST/Expr.h" 31 #include "clang/AST/ExprCXX.h" 32 #include "clang/AST/ExternalASTSource.h" 33 #include "clang/AST/NestedNameSpecifier.h" 34 #include "clang/AST/OpenMPClause.h" 35 #include "clang/AST/ODRHash.h" 36 #include "clang/AST/RawCommentList.h" 37 #include "clang/AST/TemplateBase.h" 38 #include "clang/AST/TemplateName.h" 39 #include "clang/AST/Type.h" 40 #include "clang/AST/TypeLoc.h" 41 #include "clang/AST/TypeLocVisitor.h" 42 #include "clang/AST/UnresolvedSet.h" 43 #include "clang/Basic/CommentOptions.h" 44 #include "clang/Basic/Diagnostic.h" 45 #include "clang/Basic/DiagnosticOptions.h" 46 #include "clang/Basic/ExceptionSpecificationType.h" 47 #include "clang/Basic/FileManager.h" 48 #include "clang/Basic/FileSystemOptions.h" 49 #include "clang/Basic/IdentifierTable.h" 50 #include "clang/Basic/LLVM.h" 51 #include "clang/Basic/LangOptions.h" 52 #include "clang/Basic/Module.h" 53 #include "clang/Basic/ObjCRuntime.h" 54 #include "clang/Basic/OperatorKinds.h" 55 #include "clang/Basic/PragmaKinds.h" 56 #include "clang/Basic/Sanitizers.h" 57 #include "clang/Basic/SourceLocation.h" 58 #include "clang/Basic/SourceManager.h" 59 #include "clang/Basic/SourceManagerInternals.h" 60 #include "clang/Basic/Specifiers.h" 61 #include "clang/Basic/TargetInfo.h" 62 #include "clang/Basic/TargetOptions.h" 63 #include "clang/Basic/TokenKinds.h" 64 #include "clang/Basic/Version.h" 65 #include "clang/Lex/HeaderSearch.h" 66 #include "clang/Lex/HeaderSearchOptions.h" 67 #include "clang/Lex/MacroInfo.h" 68 #include "clang/Lex/ModuleMap.h" 69 #include "clang/Lex/PreprocessingRecord.h" 70 #include "clang/Lex/Preprocessor.h" 71 #include "clang/Lex/PreprocessorOptions.h" 72 #include "clang/Lex/Token.h" 73 #include "clang/Sema/ObjCMethodList.h" 74 #include "clang/Sema/Scope.h" 75 #include "clang/Sema/Sema.h" 76 #include "clang/Sema/Weak.h" 77 #include "clang/Serialization/ASTBitCodes.h" 78 #include "clang/Serialization/ASTDeserializationListener.h" 79 #include "clang/Serialization/ContinuousRangeMap.h" 80 #include "clang/Serialization/GlobalModuleIndex.h" 81 #include "clang/Serialization/InMemoryModuleCache.h" 82 #include "clang/Serialization/ModuleFile.h" 83 #include "clang/Serialization/ModuleFileExtension.h" 84 #include "clang/Serialization/ModuleManager.h" 85 #include "clang/Serialization/PCHContainerOperations.h" 86 #include "clang/Serialization/SerializationDiagnostic.h" 87 #include "llvm/ADT/APFloat.h" 88 #include "llvm/ADT/APInt.h" 89 #include "llvm/ADT/APSInt.h" 90 #include "llvm/ADT/ArrayRef.h" 91 #include "llvm/ADT/DenseMap.h" 92 #include "llvm/ADT/FloatingPointMode.h" 93 #include "llvm/ADT/FoldingSet.h" 94 #include "llvm/ADT/Hashing.h" 95 #include "llvm/ADT/IntrusiveRefCntPtr.h" 96 #include "llvm/ADT/None.h" 97 #include "llvm/ADT/Optional.h" 98 #include "llvm/ADT/STLExtras.h" 99 #include "llvm/ADT/ScopeExit.h" 100 #include "llvm/ADT/SmallPtrSet.h" 101 #include "llvm/ADT/SmallString.h" 102 #include "llvm/ADT/SmallVector.h" 103 #include "llvm/ADT/StringExtras.h" 104 #include "llvm/ADT/StringMap.h" 105 #include "llvm/ADT/StringRef.h" 106 #include "llvm/ADT/Triple.h" 107 #include "llvm/ADT/iterator_range.h" 108 #include "llvm/Bitstream/BitstreamReader.h" 109 #include "llvm/Support/Casting.h" 110 #include "llvm/Support/Compiler.h" 111 #include "llvm/Support/Compression.h" 112 #include "llvm/Support/DJB.h" 113 #include "llvm/Support/Endian.h" 114 #include "llvm/Support/Error.h" 115 #include "llvm/Support/ErrorHandling.h" 116 #include "llvm/Support/FileSystem.h" 117 #include "llvm/Support/MemoryBuffer.h" 118 #include "llvm/Support/Path.h" 119 #include "llvm/Support/SaveAndRestore.h" 120 #include "llvm/Support/Timer.h" 121 #include "llvm/Support/VersionTuple.h" 122 #include "llvm/Support/raw_ostream.h" 123 #include <algorithm> 124 #include <cassert> 125 #include <cstddef> 126 #include <cstdint> 127 #include <cstdio> 128 #include <ctime> 129 #include <iterator> 130 #include <limits> 131 #include <map> 132 #include <memory> 133 #include <string> 134 #include <system_error> 135 #include <tuple> 136 #include <utility> 137 #include <vector> 138 139 using namespace clang; 140 using namespace clang::serialization; 141 using namespace clang::serialization::reader; 142 using llvm::BitstreamCursor; 143 using llvm::RoundingMode; 144 145 //===----------------------------------------------------------------------===// 146 // ChainedASTReaderListener implementation 147 //===----------------------------------------------------------------------===// 148 149 bool 150 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 151 return First->ReadFullVersionInformation(FullVersion) || 152 Second->ReadFullVersionInformation(FullVersion); 153 } 154 155 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 156 First->ReadModuleName(ModuleName); 157 Second->ReadModuleName(ModuleName); 158 } 159 160 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 161 First->ReadModuleMapFile(ModuleMapPath); 162 Second->ReadModuleMapFile(ModuleMapPath); 163 } 164 165 bool 166 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 167 bool Complain, 168 bool AllowCompatibleDifferences) { 169 return First->ReadLanguageOptions(LangOpts, Complain, 170 AllowCompatibleDifferences) || 171 Second->ReadLanguageOptions(LangOpts, Complain, 172 AllowCompatibleDifferences); 173 } 174 175 bool ChainedASTReaderListener::ReadTargetOptions( 176 const TargetOptions &TargetOpts, bool Complain, 177 bool AllowCompatibleDifferences) { 178 return First->ReadTargetOptions(TargetOpts, Complain, 179 AllowCompatibleDifferences) || 180 Second->ReadTargetOptions(TargetOpts, Complain, 181 AllowCompatibleDifferences); 182 } 183 184 bool ChainedASTReaderListener::ReadDiagnosticOptions( 185 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 186 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 187 Second->ReadDiagnosticOptions(DiagOpts, Complain); 188 } 189 190 bool 191 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 192 bool Complain) { 193 return First->ReadFileSystemOptions(FSOpts, Complain) || 194 Second->ReadFileSystemOptions(FSOpts, Complain); 195 } 196 197 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 198 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 199 bool Complain) { 200 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 201 Complain) || 202 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 203 Complain); 204 } 205 206 bool ChainedASTReaderListener::ReadPreprocessorOptions( 207 const PreprocessorOptions &PPOpts, bool Complain, 208 std::string &SuggestedPredefines) { 209 return First->ReadPreprocessorOptions(PPOpts, Complain, 210 SuggestedPredefines) || 211 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 212 } 213 214 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 215 unsigned Value) { 216 First->ReadCounter(M, Value); 217 Second->ReadCounter(M, Value); 218 } 219 220 bool ChainedASTReaderListener::needsInputFileVisitation() { 221 return First->needsInputFileVisitation() || 222 Second->needsInputFileVisitation(); 223 } 224 225 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 226 return First->needsSystemInputFileVisitation() || 227 Second->needsSystemInputFileVisitation(); 228 } 229 230 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 231 ModuleKind Kind) { 232 First->visitModuleFile(Filename, Kind); 233 Second->visitModuleFile(Filename, Kind); 234 } 235 236 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 237 bool isSystem, 238 bool isOverridden, 239 bool isExplicitModule) { 240 bool Continue = false; 241 if (First->needsInputFileVisitation() && 242 (!isSystem || First->needsSystemInputFileVisitation())) 243 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 244 isExplicitModule); 245 if (Second->needsInputFileVisitation() && 246 (!isSystem || Second->needsSystemInputFileVisitation())) 247 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 248 isExplicitModule); 249 return Continue; 250 } 251 252 void ChainedASTReaderListener::readModuleFileExtension( 253 const ModuleFileExtensionMetadata &Metadata) { 254 First->readModuleFileExtension(Metadata); 255 Second->readModuleFileExtension(Metadata); 256 } 257 258 //===----------------------------------------------------------------------===// 259 // PCH validator implementation 260 //===----------------------------------------------------------------------===// 261 262 ASTReaderListener::~ASTReaderListener() = default; 263 264 /// Compare the given set of language options against an existing set of 265 /// language options. 266 /// 267 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 268 /// \param AllowCompatibleDifferences If true, differences between compatible 269 /// language options will be permitted. 270 /// 271 /// \returns true if the languagae options mis-match, false otherwise. 272 static bool checkLanguageOptions(const LangOptions &LangOpts, 273 const LangOptions &ExistingLangOpts, 274 DiagnosticsEngine *Diags, 275 bool AllowCompatibleDifferences = true) { 276 #define LANGOPT(Name, Bits, Default, Description) \ 277 if (ExistingLangOpts.Name != LangOpts.Name) { \ 278 if (Diags) \ 279 Diags->Report(diag::err_pch_langopt_mismatch) \ 280 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 281 return true; \ 282 } 283 284 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 285 if (ExistingLangOpts.Name != LangOpts.Name) { \ 286 if (Diags) \ 287 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 288 << Description; \ 289 return true; \ 290 } 291 292 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 293 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 294 if (Diags) \ 295 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 296 << Description; \ 297 return true; \ 298 } 299 300 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 301 if (!AllowCompatibleDifferences) \ 302 LANGOPT(Name, Bits, Default, Description) 303 304 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 305 if (!AllowCompatibleDifferences) \ 306 ENUM_LANGOPT(Name, Bits, Default, Description) 307 308 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 309 if (!AllowCompatibleDifferences) \ 310 VALUE_LANGOPT(Name, Bits, Default, Description) 311 312 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 313 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 314 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 315 #include "clang/Basic/LangOptions.def" 316 317 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 318 if (Diags) 319 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 320 return true; 321 } 322 323 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 324 if (Diags) 325 Diags->Report(diag::err_pch_langopt_value_mismatch) 326 << "target Objective-C runtime"; 327 return true; 328 } 329 330 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 331 LangOpts.CommentOpts.BlockCommandNames) { 332 if (Diags) 333 Diags->Report(diag::err_pch_langopt_value_mismatch) 334 << "block command names"; 335 return true; 336 } 337 338 // Sanitizer feature mismatches are treated as compatible differences. If 339 // compatible differences aren't allowed, we still only want to check for 340 // mismatches of non-modular sanitizers (the only ones which can affect AST 341 // generation). 342 if (!AllowCompatibleDifferences) { 343 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 344 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 345 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 346 ExistingSanitizers.clear(ModularSanitizers); 347 ImportedSanitizers.clear(ModularSanitizers); 348 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 349 const std::string Flag = "-fsanitize="; 350 if (Diags) { 351 #define SANITIZER(NAME, ID) \ 352 { \ 353 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 354 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 355 if (InExistingModule != InImportedModule) \ 356 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 357 << InExistingModule << (Flag + NAME); \ 358 } 359 #include "clang/Basic/Sanitizers.def" 360 } 361 return true; 362 } 363 } 364 365 return false; 366 } 367 368 /// Compare the given set of target options against an existing set of 369 /// target options. 370 /// 371 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 372 /// 373 /// \returns true if the target options mis-match, false otherwise. 374 static bool checkTargetOptions(const TargetOptions &TargetOpts, 375 const TargetOptions &ExistingTargetOpts, 376 DiagnosticsEngine *Diags, 377 bool AllowCompatibleDifferences = true) { 378 #define CHECK_TARGET_OPT(Field, Name) \ 379 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 380 if (Diags) \ 381 Diags->Report(diag::err_pch_targetopt_mismatch) \ 382 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 383 return true; \ 384 } 385 386 // The triple and ABI must match exactly. 387 CHECK_TARGET_OPT(Triple, "target"); 388 CHECK_TARGET_OPT(ABI, "target ABI"); 389 390 // We can tolerate different CPUs in many cases, notably when one CPU 391 // supports a strict superset of another. When allowing compatible 392 // differences skip this check. 393 if (!AllowCompatibleDifferences) { 394 CHECK_TARGET_OPT(CPU, "target CPU"); 395 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 396 } 397 398 #undef CHECK_TARGET_OPT 399 400 // Compare feature sets. 401 SmallVector<StringRef, 4> ExistingFeatures( 402 ExistingTargetOpts.FeaturesAsWritten.begin(), 403 ExistingTargetOpts.FeaturesAsWritten.end()); 404 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 405 TargetOpts.FeaturesAsWritten.end()); 406 llvm::sort(ExistingFeatures); 407 llvm::sort(ReadFeatures); 408 409 // We compute the set difference in both directions explicitly so that we can 410 // diagnose the differences differently. 411 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 412 std::set_difference( 413 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 414 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 415 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 416 ExistingFeatures.begin(), ExistingFeatures.end(), 417 std::back_inserter(UnmatchedReadFeatures)); 418 419 // If we are allowing compatible differences and the read feature set is 420 // a strict subset of the existing feature set, there is nothing to diagnose. 421 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 422 return false; 423 424 if (Diags) { 425 for (StringRef Feature : UnmatchedReadFeatures) 426 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 427 << /* is-existing-feature */ false << Feature; 428 for (StringRef Feature : UnmatchedExistingFeatures) 429 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 430 << /* is-existing-feature */ true << Feature; 431 } 432 433 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 434 } 435 436 bool 437 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 438 bool Complain, 439 bool AllowCompatibleDifferences) { 440 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 441 return checkLanguageOptions(LangOpts, ExistingLangOpts, 442 Complain ? &Reader.Diags : nullptr, 443 AllowCompatibleDifferences); 444 } 445 446 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 447 bool Complain, 448 bool AllowCompatibleDifferences) { 449 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 450 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 451 Complain ? &Reader.Diags : nullptr, 452 AllowCompatibleDifferences); 453 } 454 455 namespace { 456 457 using MacroDefinitionsMap = 458 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 459 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 460 461 } // namespace 462 463 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 464 DiagnosticsEngine &Diags, 465 bool Complain) { 466 using Level = DiagnosticsEngine::Level; 467 468 // Check current mappings for new -Werror mappings, and the stored mappings 469 // for cases that were explicitly mapped to *not* be errors that are now 470 // errors because of options like -Werror. 471 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 472 473 for (DiagnosticsEngine *MappingSource : MappingSources) { 474 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 475 diag::kind DiagID = DiagIDMappingPair.first; 476 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 477 if (CurLevel < DiagnosticsEngine::Error) 478 continue; // not significant 479 Level StoredLevel = 480 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 481 if (StoredLevel < DiagnosticsEngine::Error) { 482 if (Complain) 483 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 484 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 485 return true; 486 } 487 } 488 } 489 490 return false; 491 } 492 493 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 494 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 495 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 496 return true; 497 return Ext >= diag::Severity::Error; 498 } 499 500 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 501 DiagnosticsEngine &Diags, 502 bool IsSystem, bool Complain) { 503 // Top-level options 504 if (IsSystem) { 505 if (Diags.getSuppressSystemWarnings()) 506 return false; 507 // If -Wsystem-headers was not enabled before, be conservative 508 if (StoredDiags.getSuppressSystemWarnings()) { 509 if (Complain) 510 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 511 return true; 512 } 513 } 514 515 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 516 if (Complain) 517 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 518 return true; 519 } 520 521 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 522 !StoredDiags.getEnableAllWarnings()) { 523 if (Complain) 524 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 525 return true; 526 } 527 528 if (isExtHandlingFromDiagsError(Diags) && 529 !isExtHandlingFromDiagsError(StoredDiags)) { 530 if (Complain) 531 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 532 return true; 533 } 534 535 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 536 } 537 538 /// Return the top import module if it is implicit, nullptr otherwise. 539 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 540 Preprocessor &PP) { 541 // If the original import came from a file explicitly generated by the user, 542 // don't check the diagnostic mappings. 543 // FIXME: currently this is approximated by checking whether this is not a 544 // module import of an implicitly-loaded module file. 545 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 546 // the transitive closure of its imports, since unrelated modules cannot be 547 // imported until after this module finishes validation. 548 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 549 while (!TopImport->ImportedBy.empty()) 550 TopImport = TopImport->ImportedBy[0]; 551 if (TopImport->Kind != MK_ImplicitModule) 552 return nullptr; 553 554 StringRef ModuleName = TopImport->ModuleName; 555 assert(!ModuleName.empty() && "diagnostic options read before module name"); 556 557 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 558 assert(M && "missing module"); 559 return M; 560 } 561 562 bool PCHValidator::ReadDiagnosticOptions( 563 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 564 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 565 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 566 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 567 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 568 // This should never fail, because we would have processed these options 569 // before writing them to an ASTFile. 570 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 571 572 ModuleManager &ModuleMgr = Reader.getModuleManager(); 573 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 574 575 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 576 if (!TopM) 577 return false; 578 579 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 580 // contains the union of their flags. 581 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 582 Complain); 583 } 584 585 /// Collect the macro definitions provided by the given preprocessor 586 /// options. 587 static void 588 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 589 MacroDefinitionsMap &Macros, 590 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 591 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 592 StringRef Macro = PPOpts.Macros[I].first; 593 bool IsUndef = PPOpts.Macros[I].second; 594 595 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 596 StringRef MacroName = MacroPair.first; 597 StringRef MacroBody = MacroPair.second; 598 599 // For an #undef'd macro, we only care about the name. 600 if (IsUndef) { 601 if (MacroNames && !Macros.count(MacroName)) 602 MacroNames->push_back(MacroName); 603 604 Macros[MacroName] = std::make_pair("", true); 605 continue; 606 } 607 608 // For a #define'd macro, figure out the actual definition. 609 if (MacroName.size() == Macro.size()) 610 MacroBody = "1"; 611 else { 612 // Note: GCC drops anything following an end-of-line character. 613 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 614 MacroBody = MacroBody.substr(0, End); 615 } 616 617 if (MacroNames && !Macros.count(MacroName)) 618 MacroNames->push_back(MacroName); 619 Macros[MacroName] = std::make_pair(MacroBody, false); 620 } 621 } 622 623 /// Check the preprocessor options deserialized from the control block 624 /// against the preprocessor options in an existing preprocessor. 625 /// 626 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 627 /// \param Validate If true, validate preprocessor options. If false, allow 628 /// macros defined by \p ExistingPPOpts to override those defined by 629 /// \p PPOpts in SuggestedPredefines. 630 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 631 const PreprocessorOptions &ExistingPPOpts, 632 DiagnosticsEngine *Diags, 633 FileManager &FileMgr, 634 std::string &SuggestedPredefines, 635 const LangOptions &LangOpts, 636 bool Validate = true) { 637 // Check macro definitions. 638 MacroDefinitionsMap ASTFileMacros; 639 collectMacroDefinitions(PPOpts, ASTFileMacros); 640 MacroDefinitionsMap ExistingMacros; 641 SmallVector<StringRef, 4> ExistingMacroNames; 642 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 643 644 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 645 // Dig out the macro definition in the existing preprocessor options. 646 StringRef MacroName = ExistingMacroNames[I]; 647 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 648 649 // Check whether we know anything about this macro name or not. 650 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 651 ASTFileMacros.find(MacroName); 652 if (!Validate || Known == ASTFileMacros.end()) { 653 // FIXME: Check whether this identifier was referenced anywhere in the 654 // AST file. If so, we should reject the AST file. Unfortunately, this 655 // information isn't in the control block. What shall we do about it? 656 657 if (Existing.second) { 658 SuggestedPredefines += "#undef "; 659 SuggestedPredefines += MacroName.str(); 660 SuggestedPredefines += '\n'; 661 } else { 662 SuggestedPredefines += "#define "; 663 SuggestedPredefines += MacroName.str(); 664 SuggestedPredefines += ' '; 665 SuggestedPredefines += Existing.first.str(); 666 SuggestedPredefines += '\n'; 667 } 668 continue; 669 } 670 671 // If the macro was defined in one but undef'd in the other, we have a 672 // conflict. 673 if (Existing.second != Known->second.second) { 674 if (Diags) { 675 Diags->Report(diag::err_pch_macro_def_undef) 676 << MacroName << Known->second.second; 677 } 678 return true; 679 } 680 681 // If the macro was #undef'd in both, or if the macro bodies are identical, 682 // it's fine. 683 if (Existing.second || Existing.first == Known->second.first) 684 continue; 685 686 // The macro bodies differ; complain. 687 if (Diags) { 688 Diags->Report(diag::err_pch_macro_def_conflict) 689 << MacroName << Known->second.first << Existing.first; 690 } 691 return true; 692 } 693 694 // Check whether we're using predefines. 695 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 696 if (Diags) { 697 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 698 } 699 return true; 700 } 701 702 // Detailed record is important since it is used for the module cache hash. 703 if (LangOpts.Modules && 704 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 705 if (Diags) { 706 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 707 } 708 return true; 709 } 710 711 // Compute the #include and #include_macros lines we need. 712 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 713 StringRef File = ExistingPPOpts.Includes[I]; 714 715 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 716 !ExistingPPOpts.PCHThroughHeader.empty()) { 717 // In case the through header is an include, we must add all the includes 718 // to the predefines so the start point can be determined. 719 SuggestedPredefines += "#include \""; 720 SuggestedPredefines += File; 721 SuggestedPredefines += "\"\n"; 722 continue; 723 } 724 725 if (File == ExistingPPOpts.ImplicitPCHInclude) 726 continue; 727 728 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 729 != PPOpts.Includes.end()) 730 continue; 731 732 SuggestedPredefines += "#include \""; 733 SuggestedPredefines += File; 734 SuggestedPredefines += "\"\n"; 735 } 736 737 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 738 StringRef File = ExistingPPOpts.MacroIncludes[I]; 739 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 740 File) 741 != PPOpts.MacroIncludes.end()) 742 continue; 743 744 SuggestedPredefines += "#__include_macros \""; 745 SuggestedPredefines += File; 746 SuggestedPredefines += "\"\n##\n"; 747 } 748 749 return false; 750 } 751 752 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 753 bool Complain, 754 std::string &SuggestedPredefines) { 755 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 756 757 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 758 Complain? &Reader.Diags : nullptr, 759 PP.getFileManager(), 760 SuggestedPredefines, 761 PP.getLangOpts()); 762 } 763 764 bool SimpleASTReaderListener::ReadPreprocessorOptions( 765 const PreprocessorOptions &PPOpts, 766 bool Complain, 767 std::string &SuggestedPredefines) { 768 return checkPreprocessorOptions(PPOpts, 769 PP.getPreprocessorOpts(), 770 nullptr, 771 PP.getFileManager(), 772 SuggestedPredefines, 773 PP.getLangOpts(), 774 false); 775 } 776 777 /// Check the header search options deserialized from the control block 778 /// against the header search options in an existing preprocessor. 779 /// 780 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 781 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 782 StringRef SpecificModuleCachePath, 783 StringRef ExistingModuleCachePath, 784 DiagnosticsEngine *Diags, 785 const LangOptions &LangOpts) { 786 if (LangOpts.Modules) { 787 if (SpecificModuleCachePath != ExistingModuleCachePath) { 788 if (Diags) 789 Diags->Report(diag::err_pch_modulecache_mismatch) 790 << SpecificModuleCachePath << ExistingModuleCachePath; 791 return true; 792 } 793 } 794 795 return false; 796 } 797 798 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 799 StringRef SpecificModuleCachePath, 800 bool Complain) { 801 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 802 PP.getHeaderSearchInfo().getModuleCachePath(), 803 Complain ? &Reader.Diags : nullptr, 804 PP.getLangOpts()); 805 } 806 807 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 808 PP.setCounterValue(Value); 809 } 810 811 //===----------------------------------------------------------------------===// 812 // AST reader implementation 813 //===----------------------------------------------------------------------===// 814 815 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 816 bool TakeOwnership) { 817 DeserializationListener = Listener; 818 OwnsDeserializationListener = TakeOwnership; 819 } 820 821 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 822 return serialization::ComputeHash(Sel); 823 } 824 825 std::pair<unsigned, unsigned> 826 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 827 using namespace llvm::support; 828 829 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 830 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 831 return std::make_pair(KeyLen, DataLen); 832 } 833 834 ASTSelectorLookupTrait::internal_key_type 835 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 836 using namespace llvm::support; 837 838 SelectorTable &SelTable = Reader.getContext().Selectors; 839 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 840 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 841 F, endian::readNext<uint32_t, little, unaligned>(d)); 842 if (N == 0) 843 return SelTable.getNullarySelector(FirstII); 844 else if (N == 1) 845 return SelTable.getUnarySelector(FirstII); 846 847 SmallVector<IdentifierInfo *, 16> Args; 848 Args.push_back(FirstII); 849 for (unsigned I = 1; I != N; ++I) 850 Args.push_back(Reader.getLocalIdentifier( 851 F, endian::readNext<uint32_t, little, unaligned>(d))); 852 853 return SelTable.getSelector(N, Args.data()); 854 } 855 856 ASTSelectorLookupTrait::data_type 857 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 858 unsigned DataLen) { 859 using namespace llvm::support; 860 861 data_type Result; 862 863 Result.ID = Reader.getGlobalSelectorID( 864 F, endian::readNext<uint32_t, little, unaligned>(d)); 865 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 866 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 867 Result.InstanceBits = FullInstanceBits & 0x3; 868 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 869 Result.FactoryBits = FullFactoryBits & 0x3; 870 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 871 unsigned NumInstanceMethods = FullInstanceBits >> 3; 872 unsigned NumFactoryMethods = FullFactoryBits >> 3; 873 874 // Load instance methods 875 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 876 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 877 F, endian::readNext<uint32_t, little, unaligned>(d))) 878 Result.Instance.push_back(Method); 879 } 880 881 // Load factory methods 882 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 883 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 884 F, endian::readNext<uint32_t, little, unaligned>(d))) 885 Result.Factory.push_back(Method); 886 } 887 888 return Result; 889 } 890 891 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 892 return llvm::djbHash(a); 893 } 894 895 std::pair<unsigned, unsigned> 896 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 897 using namespace llvm::support; 898 899 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 900 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 901 return std::make_pair(KeyLen, DataLen); 902 } 903 904 ASTIdentifierLookupTraitBase::internal_key_type 905 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 906 assert(n >= 2 && d[n-1] == '\0'); 907 return StringRef((const char*) d, n-1); 908 } 909 910 /// Whether the given identifier is "interesting". 911 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 912 bool IsModule) { 913 return II.hadMacroDefinition() || II.isPoisoned() || 914 (!IsModule && II.getObjCOrBuiltinID()) || 915 II.hasRevertedTokenIDToIdentifier() || 916 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 917 II.getFETokenInfo()); 918 } 919 920 static bool readBit(unsigned &Bits) { 921 bool Value = Bits & 0x1; 922 Bits >>= 1; 923 return Value; 924 } 925 926 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 927 using namespace llvm::support; 928 929 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 930 return Reader.getGlobalIdentifierID(F, RawID >> 1); 931 } 932 933 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 934 if (!II.isFromAST()) { 935 II.setIsFromAST(); 936 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 937 if (isInterestingIdentifier(Reader, II, IsModule)) 938 II.setChangedSinceDeserialization(); 939 } 940 } 941 942 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 943 const unsigned char* d, 944 unsigned DataLen) { 945 using namespace llvm::support; 946 947 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 948 bool IsInteresting = RawID & 0x01; 949 950 // Wipe out the "is interesting" bit. 951 RawID = RawID >> 1; 952 953 // Build the IdentifierInfo and link the identifier ID with it. 954 IdentifierInfo *II = KnownII; 955 if (!II) { 956 II = &Reader.getIdentifierTable().getOwn(k); 957 KnownII = II; 958 } 959 markIdentifierFromAST(Reader, *II); 960 Reader.markIdentifierUpToDate(II); 961 962 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 963 if (!IsInteresting) { 964 // For uninteresting identifiers, there's nothing else to do. Just notify 965 // the reader that we've finished loading this identifier. 966 Reader.SetIdentifierInfo(ID, II); 967 return II; 968 } 969 970 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 971 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 972 bool CPlusPlusOperatorKeyword = readBit(Bits); 973 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 974 bool Poisoned = readBit(Bits); 975 bool ExtensionToken = readBit(Bits); 976 bool HadMacroDefinition = readBit(Bits); 977 978 assert(Bits == 0 && "Extra bits in the identifier?"); 979 DataLen -= 8; 980 981 // Set or check the various bits in the IdentifierInfo structure. 982 // Token IDs are read-only. 983 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 984 II->revertTokenIDToIdentifier(); 985 if (!F.isModule()) 986 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 987 assert(II->isExtensionToken() == ExtensionToken && 988 "Incorrect extension token flag"); 989 (void)ExtensionToken; 990 if (Poisoned) 991 II->setIsPoisoned(true); 992 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 993 "Incorrect C++ operator keyword flag"); 994 (void)CPlusPlusOperatorKeyword; 995 996 // If this identifier is a macro, deserialize the macro 997 // definition. 998 if (HadMacroDefinition) { 999 uint32_t MacroDirectivesOffset = 1000 endian::readNext<uint32_t, little, unaligned>(d); 1001 DataLen -= 4; 1002 1003 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1004 } 1005 1006 Reader.SetIdentifierInfo(ID, II); 1007 1008 // Read all of the declarations visible at global scope with this 1009 // name. 1010 if (DataLen > 0) { 1011 SmallVector<uint32_t, 4> DeclIDs; 1012 for (; DataLen > 0; DataLen -= 4) 1013 DeclIDs.push_back(Reader.getGlobalDeclID( 1014 F, endian::readNext<uint32_t, little, unaligned>(d))); 1015 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1016 } 1017 1018 return II; 1019 } 1020 1021 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1022 : Kind(Name.getNameKind()) { 1023 switch (Kind) { 1024 case DeclarationName::Identifier: 1025 Data = (uint64_t)Name.getAsIdentifierInfo(); 1026 break; 1027 case DeclarationName::ObjCZeroArgSelector: 1028 case DeclarationName::ObjCOneArgSelector: 1029 case DeclarationName::ObjCMultiArgSelector: 1030 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1031 break; 1032 case DeclarationName::CXXOperatorName: 1033 Data = Name.getCXXOverloadedOperator(); 1034 break; 1035 case DeclarationName::CXXLiteralOperatorName: 1036 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1037 break; 1038 case DeclarationName::CXXDeductionGuideName: 1039 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1040 ->getDeclName().getAsIdentifierInfo(); 1041 break; 1042 case DeclarationName::CXXConstructorName: 1043 case DeclarationName::CXXDestructorName: 1044 case DeclarationName::CXXConversionFunctionName: 1045 case DeclarationName::CXXUsingDirective: 1046 Data = 0; 1047 break; 1048 } 1049 } 1050 1051 unsigned DeclarationNameKey::getHash() const { 1052 llvm::FoldingSetNodeID ID; 1053 ID.AddInteger(Kind); 1054 1055 switch (Kind) { 1056 case DeclarationName::Identifier: 1057 case DeclarationName::CXXLiteralOperatorName: 1058 case DeclarationName::CXXDeductionGuideName: 1059 ID.AddString(((IdentifierInfo*)Data)->getName()); 1060 break; 1061 case DeclarationName::ObjCZeroArgSelector: 1062 case DeclarationName::ObjCOneArgSelector: 1063 case DeclarationName::ObjCMultiArgSelector: 1064 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1065 break; 1066 case DeclarationName::CXXOperatorName: 1067 ID.AddInteger((OverloadedOperatorKind)Data); 1068 break; 1069 case DeclarationName::CXXConstructorName: 1070 case DeclarationName::CXXDestructorName: 1071 case DeclarationName::CXXConversionFunctionName: 1072 case DeclarationName::CXXUsingDirective: 1073 break; 1074 } 1075 1076 return ID.ComputeHash(); 1077 } 1078 1079 ModuleFile * 1080 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1081 using namespace llvm::support; 1082 1083 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1084 return Reader.getLocalModuleFile(F, ModuleFileID); 1085 } 1086 1087 std::pair<unsigned, unsigned> 1088 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1089 using namespace llvm::support; 1090 1091 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 1092 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 1093 return std::make_pair(KeyLen, DataLen); 1094 } 1095 1096 ASTDeclContextNameLookupTrait::internal_key_type 1097 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1098 using namespace llvm::support; 1099 1100 auto Kind = (DeclarationName::NameKind)*d++; 1101 uint64_t Data; 1102 switch (Kind) { 1103 case DeclarationName::Identifier: 1104 case DeclarationName::CXXLiteralOperatorName: 1105 case DeclarationName::CXXDeductionGuideName: 1106 Data = (uint64_t)Reader.getLocalIdentifier( 1107 F, endian::readNext<uint32_t, little, unaligned>(d)); 1108 break; 1109 case DeclarationName::ObjCZeroArgSelector: 1110 case DeclarationName::ObjCOneArgSelector: 1111 case DeclarationName::ObjCMultiArgSelector: 1112 Data = 1113 (uint64_t)Reader.getLocalSelector( 1114 F, endian::readNext<uint32_t, little, unaligned>( 1115 d)).getAsOpaquePtr(); 1116 break; 1117 case DeclarationName::CXXOperatorName: 1118 Data = *d++; // OverloadedOperatorKind 1119 break; 1120 case DeclarationName::CXXConstructorName: 1121 case DeclarationName::CXXDestructorName: 1122 case DeclarationName::CXXConversionFunctionName: 1123 case DeclarationName::CXXUsingDirective: 1124 Data = 0; 1125 break; 1126 } 1127 1128 return DeclarationNameKey(Kind, Data); 1129 } 1130 1131 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1132 const unsigned char *d, 1133 unsigned DataLen, 1134 data_type_builder &Val) { 1135 using namespace llvm::support; 1136 1137 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1138 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1139 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1140 } 1141 } 1142 1143 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1144 BitstreamCursor &Cursor, 1145 uint64_t Offset, 1146 DeclContext *DC) { 1147 assert(Offset != 0); 1148 1149 SavedStreamPosition SavedPosition(Cursor); 1150 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1151 Error(std::move(Err)); 1152 return true; 1153 } 1154 1155 RecordData Record; 1156 StringRef Blob; 1157 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1158 if (!MaybeCode) { 1159 Error(MaybeCode.takeError()); 1160 return true; 1161 } 1162 unsigned Code = MaybeCode.get(); 1163 1164 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1165 if (!MaybeRecCode) { 1166 Error(MaybeRecCode.takeError()); 1167 return true; 1168 } 1169 unsigned RecCode = MaybeRecCode.get(); 1170 if (RecCode != DECL_CONTEXT_LEXICAL) { 1171 Error("Expected lexical block"); 1172 return true; 1173 } 1174 1175 assert(!isa<TranslationUnitDecl>(DC) && 1176 "expected a TU_UPDATE_LEXICAL record for TU"); 1177 // If we are handling a C++ class template instantiation, we can see multiple 1178 // lexical updates for the same record. It's important that we select only one 1179 // of them, so that field numbering works properly. Just pick the first one we 1180 // see. 1181 auto &Lex = LexicalDecls[DC]; 1182 if (!Lex.first) { 1183 Lex = std::make_pair( 1184 &M, llvm::makeArrayRef( 1185 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1186 Blob.data()), 1187 Blob.size() / 4)); 1188 } 1189 DC->setHasExternalLexicalStorage(true); 1190 return false; 1191 } 1192 1193 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1194 BitstreamCursor &Cursor, 1195 uint64_t Offset, 1196 DeclID ID) { 1197 assert(Offset != 0); 1198 1199 SavedStreamPosition SavedPosition(Cursor); 1200 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1201 Error(std::move(Err)); 1202 return true; 1203 } 1204 1205 RecordData Record; 1206 StringRef Blob; 1207 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1208 if (!MaybeCode) { 1209 Error(MaybeCode.takeError()); 1210 return true; 1211 } 1212 unsigned Code = MaybeCode.get(); 1213 1214 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1215 if (!MaybeRecCode) { 1216 Error(MaybeRecCode.takeError()); 1217 return true; 1218 } 1219 unsigned RecCode = MaybeRecCode.get(); 1220 if (RecCode != DECL_CONTEXT_VISIBLE) { 1221 Error("Expected visible lookup table block"); 1222 return true; 1223 } 1224 1225 // We can't safely determine the primary context yet, so delay attaching the 1226 // lookup table until we're done with recursive deserialization. 1227 auto *Data = (const unsigned char*)Blob.data(); 1228 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1229 return false; 1230 } 1231 1232 void ASTReader::Error(StringRef Msg) const { 1233 Error(diag::err_fe_pch_malformed, Msg); 1234 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1235 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1236 Diag(diag::note_module_cache_path) 1237 << PP.getHeaderSearchInfo().getModuleCachePath(); 1238 } 1239 } 1240 1241 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1242 StringRef Arg3) const { 1243 if (Diags.isDiagnosticInFlight()) 1244 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1245 else 1246 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1247 } 1248 1249 void ASTReader::Error(llvm::Error &&Err) const { 1250 Error(toString(std::move(Err))); 1251 } 1252 1253 //===----------------------------------------------------------------------===// 1254 // Source Manager Deserialization 1255 //===----------------------------------------------------------------------===// 1256 1257 /// Read the line table in the source manager block. 1258 /// \returns true if there was an error. 1259 bool ASTReader::ParseLineTable(ModuleFile &F, 1260 const RecordData &Record) { 1261 unsigned Idx = 0; 1262 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1263 1264 // Parse the file names 1265 std::map<int, int> FileIDs; 1266 FileIDs[-1] = -1; // For unspecified filenames. 1267 for (unsigned I = 0; Record[Idx]; ++I) { 1268 // Extract the file name 1269 auto Filename = ReadPath(F, Record, Idx); 1270 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1271 } 1272 ++Idx; 1273 1274 // Parse the line entries 1275 std::vector<LineEntry> Entries; 1276 while (Idx < Record.size()) { 1277 int FID = Record[Idx++]; 1278 assert(FID >= 0 && "Serialized line entries for non-local file."); 1279 // Remap FileID from 1-based old view. 1280 FID += F.SLocEntryBaseID - 1; 1281 1282 // Extract the line entries 1283 unsigned NumEntries = Record[Idx++]; 1284 assert(NumEntries && "no line entries for file ID"); 1285 Entries.clear(); 1286 Entries.reserve(NumEntries); 1287 for (unsigned I = 0; I != NumEntries; ++I) { 1288 unsigned FileOffset = Record[Idx++]; 1289 unsigned LineNo = Record[Idx++]; 1290 int FilenameID = FileIDs[Record[Idx++]]; 1291 SrcMgr::CharacteristicKind FileKind 1292 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1293 unsigned IncludeOffset = Record[Idx++]; 1294 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1295 FileKind, IncludeOffset)); 1296 } 1297 LineTable.AddEntry(FileID::get(FID), Entries); 1298 } 1299 1300 return false; 1301 } 1302 1303 /// Read a source manager block 1304 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1305 using namespace SrcMgr; 1306 1307 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1308 1309 // Set the source-location entry cursor to the current position in 1310 // the stream. This cursor will be used to read the contents of the 1311 // source manager block initially, and then lazily read 1312 // source-location entries as needed. 1313 SLocEntryCursor = F.Stream; 1314 1315 // The stream itself is going to skip over the source manager block. 1316 if (llvm::Error Err = F.Stream.SkipBlock()) { 1317 Error(std::move(Err)); 1318 return true; 1319 } 1320 1321 // Enter the source manager block. 1322 if (llvm::Error Err = 1323 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1324 Error(std::move(Err)); 1325 return true; 1326 } 1327 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 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 std::string(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->SLocEntryOffsetsBase + 1463 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1464 Error(std::move(Err)); 1465 return true; 1466 } 1467 1468 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1469 unsigned BaseOffset = F->SLocEntryBaseOffset; 1470 1471 ++NumSLocEntriesRead; 1472 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1473 if (!MaybeEntry) { 1474 Error(MaybeEntry.takeError()); 1475 return true; 1476 } 1477 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1478 1479 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1480 Error("incorrectly-formatted source location entry in AST file"); 1481 return true; 1482 } 1483 1484 RecordData Record; 1485 StringRef Blob; 1486 Expected<unsigned> MaybeSLOC = 1487 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1488 if (!MaybeSLOC) { 1489 Error(MaybeSLOC.takeError()); 1490 return true; 1491 } 1492 switch (MaybeSLOC.get()) { 1493 default: 1494 Error("incorrectly-formatted source location entry in AST file"); 1495 return true; 1496 1497 case SM_SLOC_FILE_ENTRY: { 1498 // We will detect whether a file changed and return 'Failure' for it, but 1499 // we will also try to fail gracefully by setting up the SLocEntry. 1500 unsigned InputID = Record[4]; 1501 InputFile IF = getInputFile(*F, InputID); 1502 const FileEntry *File = IF.getFile(); 1503 bool OverriddenBuffer = IF.isOverridden(); 1504 1505 // Note that we only check if a File was returned. If it was out-of-date 1506 // we have complained but we will continue creating a FileID to recover 1507 // gracefully. 1508 if (!File) 1509 return true; 1510 1511 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1512 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1513 // This is the module's main file. 1514 IncludeLoc = getImportLocation(F); 1515 } 1516 SrcMgr::CharacteristicKind 1517 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1518 // FIXME: The FileID should be created from the FileEntryRef. 1519 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1520 ID, BaseOffset + Record[0]); 1521 SrcMgr::FileInfo &FileInfo = 1522 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1523 FileInfo.NumCreatedFIDs = Record[5]; 1524 if (Record[3]) 1525 FileInfo.setHasLineDirectives(); 1526 1527 unsigned NumFileDecls = Record[7]; 1528 if (NumFileDecls && ContextObj) { 1529 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1530 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1531 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1532 NumFileDecls)); 1533 } 1534 1535 const SrcMgr::ContentCache &ContentCache = 1536 SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter)); 1537 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1538 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1539 !ContentCache.getBufferIfLoaded()) { 1540 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1541 if (!Buffer) 1542 return true; 1543 SourceMgr.overrideFileContents(File, std::move(Buffer)); 1544 } 1545 1546 break; 1547 } 1548 1549 case SM_SLOC_BUFFER_ENTRY: { 1550 const char *Name = Blob.data(); 1551 unsigned Offset = Record[0]; 1552 SrcMgr::CharacteristicKind 1553 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1554 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1555 if (IncludeLoc.isInvalid() && F->isModule()) { 1556 IncludeLoc = getImportLocation(F); 1557 } 1558 1559 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1560 if (!Buffer) 1561 return true; 1562 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1563 BaseOffset + Offset, IncludeLoc); 1564 break; 1565 } 1566 1567 case SM_SLOC_EXPANSION_ENTRY: { 1568 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1569 SourceMgr.createExpansionLoc(SpellingLoc, 1570 ReadSourceLocation(*F, Record[2]), 1571 ReadSourceLocation(*F, Record[3]), 1572 Record[5], 1573 Record[4], 1574 ID, 1575 BaseOffset + Record[0]); 1576 break; 1577 } 1578 } 1579 1580 return false; 1581 } 1582 1583 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1584 if (ID == 0) 1585 return std::make_pair(SourceLocation(), ""); 1586 1587 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1588 Error("source location entry ID out-of-range for AST file"); 1589 return std::make_pair(SourceLocation(), ""); 1590 } 1591 1592 // Find which module file this entry lands in. 1593 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1594 if (!M->isModule()) 1595 return std::make_pair(SourceLocation(), ""); 1596 1597 // FIXME: Can we map this down to a particular submodule? That would be 1598 // ideal. 1599 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1600 } 1601 1602 /// Find the location where the module F is imported. 1603 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1604 if (F->ImportLoc.isValid()) 1605 return F->ImportLoc; 1606 1607 // Otherwise we have a PCH. It's considered to be "imported" at the first 1608 // location of its includer. 1609 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1610 // Main file is the importer. 1611 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1612 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1613 } 1614 return F->ImportedBy[0]->FirstLoc; 1615 } 1616 1617 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1618 /// the abbreviations that are at the top of the block and then leave the cursor 1619 /// pointing into the block. 1620 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID, 1621 uint64_t *StartOfBlockOffset) { 1622 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 1623 // FIXME this drops errors on the floor. 1624 consumeError(std::move(Err)); 1625 return true; 1626 } 1627 1628 if (StartOfBlockOffset) 1629 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1630 1631 while (true) { 1632 uint64_t Offset = Cursor.GetCurrentBitNo(); 1633 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1634 if (!MaybeCode) { 1635 // FIXME this drops errors on the floor. 1636 consumeError(MaybeCode.takeError()); 1637 return true; 1638 } 1639 unsigned Code = MaybeCode.get(); 1640 1641 // We expect all abbrevs to be at the start of the block. 1642 if (Code != llvm::bitc::DEFINE_ABBREV) { 1643 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1644 // FIXME this drops errors on the floor. 1645 consumeError(std::move(Err)); 1646 return true; 1647 } 1648 return false; 1649 } 1650 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) { 1651 // FIXME this drops errors on the floor. 1652 consumeError(std::move(Err)); 1653 return true; 1654 } 1655 } 1656 } 1657 1658 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1659 unsigned &Idx) { 1660 Token Tok; 1661 Tok.startToken(); 1662 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1663 Tok.setLength(Record[Idx++]); 1664 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1665 Tok.setIdentifierInfo(II); 1666 Tok.setKind((tok::TokenKind)Record[Idx++]); 1667 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1668 return Tok; 1669 } 1670 1671 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1672 BitstreamCursor &Stream = F.MacroCursor; 1673 1674 // Keep track of where we are in the stream, then jump back there 1675 // after reading this macro. 1676 SavedStreamPosition SavedPosition(Stream); 1677 1678 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1679 // FIXME this drops errors on the floor. 1680 consumeError(std::move(Err)); 1681 return nullptr; 1682 } 1683 RecordData Record; 1684 SmallVector<IdentifierInfo*, 16> MacroParams; 1685 MacroInfo *Macro = nullptr; 1686 1687 while (true) { 1688 // Advance to the next record, but if we get to the end of the block, don't 1689 // pop it (removing all the abbreviations from the cursor) since we want to 1690 // be able to reseek within the block and read entries. 1691 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1692 Expected<llvm::BitstreamEntry> MaybeEntry = 1693 Stream.advanceSkippingSubblocks(Flags); 1694 if (!MaybeEntry) { 1695 Error(MaybeEntry.takeError()); 1696 return Macro; 1697 } 1698 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1699 1700 switch (Entry.Kind) { 1701 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1702 case llvm::BitstreamEntry::Error: 1703 Error("malformed block record in AST file"); 1704 return Macro; 1705 case llvm::BitstreamEntry::EndBlock: 1706 return Macro; 1707 case llvm::BitstreamEntry::Record: 1708 // The interesting case. 1709 break; 1710 } 1711 1712 // Read a record. 1713 Record.clear(); 1714 PreprocessorRecordTypes RecType; 1715 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1716 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1717 else { 1718 Error(MaybeRecType.takeError()); 1719 return Macro; 1720 } 1721 switch (RecType) { 1722 case PP_MODULE_MACRO: 1723 case PP_MACRO_DIRECTIVE_HISTORY: 1724 return Macro; 1725 1726 case PP_MACRO_OBJECT_LIKE: 1727 case PP_MACRO_FUNCTION_LIKE: { 1728 // If we already have a macro, that means that we've hit the end 1729 // of the definition of the macro we were looking for. We're 1730 // done. 1731 if (Macro) 1732 return Macro; 1733 1734 unsigned NextIndex = 1; // Skip identifier ID. 1735 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1736 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1737 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1738 MI->setIsUsed(Record[NextIndex++]); 1739 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1740 1741 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1742 // Decode function-like macro info. 1743 bool isC99VarArgs = Record[NextIndex++]; 1744 bool isGNUVarArgs = Record[NextIndex++]; 1745 bool hasCommaPasting = Record[NextIndex++]; 1746 MacroParams.clear(); 1747 unsigned NumArgs = Record[NextIndex++]; 1748 for (unsigned i = 0; i != NumArgs; ++i) 1749 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1750 1751 // Install function-like macro info. 1752 MI->setIsFunctionLike(); 1753 if (isC99VarArgs) MI->setIsC99Varargs(); 1754 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1755 if (hasCommaPasting) MI->setHasCommaPasting(); 1756 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1757 } 1758 1759 // Remember that we saw this macro last so that we add the tokens that 1760 // form its body to it. 1761 Macro = MI; 1762 1763 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1764 Record[NextIndex]) { 1765 // We have a macro definition. Register the association 1766 PreprocessedEntityID 1767 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1768 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1769 PreprocessingRecord::PPEntityID PPID = 1770 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1771 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1772 PPRec.getPreprocessedEntity(PPID)); 1773 if (PPDef) 1774 PPRec.RegisterMacroDefinition(Macro, PPDef); 1775 } 1776 1777 ++NumMacrosRead; 1778 break; 1779 } 1780 1781 case PP_TOKEN: { 1782 // If we see a TOKEN before a PP_MACRO_*, then the file is 1783 // erroneous, just pretend we didn't see this. 1784 if (!Macro) break; 1785 1786 unsigned Idx = 0; 1787 Token Tok = ReadToken(F, Record, Idx); 1788 Macro->AddTokenToBody(Tok); 1789 break; 1790 } 1791 } 1792 } 1793 } 1794 1795 PreprocessedEntityID 1796 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1797 unsigned LocalID) const { 1798 if (!M.ModuleOffsetMap.empty()) 1799 ReadModuleOffsetMap(M); 1800 1801 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1802 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1803 assert(I != M.PreprocessedEntityRemap.end() 1804 && "Invalid index into preprocessed entity index remap"); 1805 1806 return LocalID + I->second; 1807 } 1808 1809 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1810 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1811 } 1812 1813 HeaderFileInfoTrait::internal_key_type 1814 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1815 internal_key_type ikey = {FE->getSize(), 1816 M.HasTimestamps ? FE->getModificationTime() : 0, 1817 FE->getName(), /*Imported*/ false}; 1818 return ikey; 1819 } 1820 1821 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1822 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1823 return false; 1824 1825 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1826 return true; 1827 1828 // Determine whether the actual files are equivalent. 1829 FileManager &FileMgr = Reader.getFileManager(); 1830 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1831 if (!Key.Imported) { 1832 if (auto File = FileMgr.getFile(Key.Filename)) 1833 return *File; 1834 return nullptr; 1835 } 1836 1837 std::string Resolved = std::string(Key.Filename); 1838 Reader.ResolveImportedPath(M, Resolved); 1839 if (auto File = FileMgr.getFile(Resolved)) 1840 return *File; 1841 return nullptr; 1842 }; 1843 1844 const FileEntry *FEA = GetFile(a); 1845 const FileEntry *FEB = GetFile(b); 1846 return FEA && FEA == FEB; 1847 } 1848 1849 std::pair<unsigned, unsigned> 1850 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1851 using namespace llvm::support; 1852 1853 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1854 unsigned DataLen = (unsigned) *d++; 1855 return std::make_pair(KeyLen, DataLen); 1856 } 1857 1858 HeaderFileInfoTrait::internal_key_type 1859 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1860 using namespace llvm::support; 1861 1862 internal_key_type ikey; 1863 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1864 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1865 ikey.Filename = (const char *)d; 1866 ikey.Imported = true; 1867 return ikey; 1868 } 1869 1870 HeaderFileInfoTrait::data_type 1871 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1872 unsigned DataLen) { 1873 using namespace llvm::support; 1874 1875 const unsigned char *End = d + DataLen; 1876 HeaderFileInfo HFI; 1877 unsigned Flags = *d++; 1878 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1879 HFI.isImport |= (Flags >> 5) & 0x01; 1880 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1881 HFI.DirInfo = (Flags >> 1) & 0x07; 1882 HFI.IndexHeaderMapHeader = Flags & 0x01; 1883 // FIXME: Find a better way to handle this. Maybe just store a 1884 // "has been included" flag? 1885 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1886 HFI.NumIncludes); 1887 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1888 M, endian::readNext<uint32_t, little, unaligned>(d)); 1889 if (unsigned FrameworkOffset = 1890 endian::readNext<uint32_t, little, unaligned>(d)) { 1891 // The framework offset is 1 greater than the actual offset, 1892 // since 0 is used as an indicator for "no framework name". 1893 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1894 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1895 } 1896 1897 assert((End - d) % 4 == 0 && 1898 "Wrong data length in HeaderFileInfo deserialization"); 1899 while (d != End) { 1900 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1901 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1902 LocalSMID >>= 2; 1903 1904 // This header is part of a module. Associate it with the module to enable 1905 // implicit module import. 1906 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1907 Module *Mod = Reader.getSubmodule(GlobalSMID); 1908 FileManager &FileMgr = Reader.getFileManager(); 1909 ModuleMap &ModMap = 1910 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1911 1912 std::string Filename = std::string(key.Filename); 1913 if (key.Imported) 1914 Reader.ResolveImportedPath(M, Filename); 1915 // FIXME: This is not always the right filename-as-written, but we're not 1916 // going to use this information to rebuild the module, so it doesn't make 1917 // a lot of difference. 1918 Module::Header H = {std::string(key.Filename), 1919 *FileMgr.getOptionalFileRef(Filename)}; 1920 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1921 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1922 } 1923 1924 // This HeaderFileInfo was externally loaded. 1925 HFI.External = true; 1926 HFI.IsValid = true; 1927 return HFI; 1928 } 1929 1930 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1931 uint32_t MacroDirectivesOffset) { 1932 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1933 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1934 } 1935 1936 void ASTReader::ReadDefinedMacros() { 1937 // Note that we are loading defined macros. 1938 Deserializing Macros(this); 1939 1940 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1941 BitstreamCursor &MacroCursor = I.MacroCursor; 1942 1943 // If there was no preprocessor block, skip this file. 1944 if (MacroCursor.getBitcodeBytes().empty()) 1945 continue; 1946 1947 BitstreamCursor Cursor = MacroCursor; 1948 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1949 Error(std::move(Err)); 1950 return; 1951 } 1952 1953 RecordData Record; 1954 while (true) { 1955 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1956 if (!MaybeE) { 1957 Error(MaybeE.takeError()); 1958 return; 1959 } 1960 llvm::BitstreamEntry E = MaybeE.get(); 1961 1962 switch (E.Kind) { 1963 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1964 case llvm::BitstreamEntry::Error: 1965 Error("malformed block record in AST file"); 1966 return; 1967 case llvm::BitstreamEntry::EndBlock: 1968 goto NextCursor; 1969 1970 case llvm::BitstreamEntry::Record: { 1971 Record.clear(); 1972 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1973 if (!MaybeRecord) { 1974 Error(MaybeRecord.takeError()); 1975 return; 1976 } 1977 switch (MaybeRecord.get()) { 1978 default: // Default behavior: ignore. 1979 break; 1980 1981 case PP_MACRO_OBJECT_LIKE: 1982 case PP_MACRO_FUNCTION_LIKE: { 1983 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1984 if (II->isOutOfDate()) 1985 updateOutOfDateIdentifier(*II); 1986 break; 1987 } 1988 1989 case PP_TOKEN: 1990 // Ignore tokens. 1991 break; 1992 } 1993 break; 1994 } 1995 } 1996 } 1997 NextCursor: ; 1998 } 1999 } 2000 2001 namespace { 2002 2003 /// Visitor class used to look up identifirs in an AST file. 2004 class IdentifierLookupVisitor { 2005 StringRef Name; 2006 unsigned NameHash; 2007 unsigned PriorGeneration; 2008 unsigned &NumIdentifierLookups; 2009 unsigned &NumIdentifierLookupHits; 2010 IdentifierInfo *Found = nullptr; 2011 2012 public: 2013 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2014 unsigned &NumIdentifierLookups, 2015 unsigned &NumIdentifierLookupHits) 2016 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2017 PriorGeneration(PriorGeneration), 2018 NumIdentifierLookups(NumIdentifierLookups), 2019 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2020 2021 bool operator()(ModuleFile &M) { 2022 // If we've already searched this module file, skip it now. 2023 if (M.Generation <= PriorGeneration) 2024 return true; 2025 2026 ASTIdentifierLookupTable *IdTable 2027 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2028 if (!IdTable) 2029 return false; 2030 2031 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2032 Found); 2033 ++NumIdentifierLookups; 2034 ASTIdentifierLookupTable::iterator Pos = 2035 IdTable->find_hashed(Name, NameHash, &Trait); 2036 if (Pos == IdTable->end()) 2037 return false; 2038 2039 // Dereferencing the iterator has the effect of building the 2040 // IdentifierInfo node and populating it with the various 2041 // declarations it needs. 2042 ++NumIdentifierLookupHits; 2043 Found = *Pos; 2044 return true; 2045 } 2046 2047 // Retrieve the identifier info found within the module 2048 // files. 2049 IdentifierInfo *getIdentifierInfo() const { return Found; } 2050 }; 2051 2052 } // namespace 2053 2054 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2055 // Note that we are loading an identifier. 2056 Deserializing AnIdentifier(this); 2057 2058 unsigned PriorGeneration = 0; 2059 if (getContext().getLangOpts().Modules) 2060 PriorGeneration = IdentifierGeneration[&II]; 2061 2062 // If there is a global index, look there first to determine which modules 2063 // provably do not have any results for this identifier. 2064 GlobalModuleIndex::HitSet Hits; 2065 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2066 if (!loadGlobalIndex()) { 2067 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2068 HitsPtr = &Hits; 2069 } 2070 } 2071 2072 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2073 NumIdentifierLookups, 2074 NumIdentifierLookupHits); 2075 ModuleMgr.visit(Visitor, HitsPtr); 2076 markIdentifierUpToDate(&II); 2077 } 2078 2079 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2080 if (!II) 2081 return; 2082 2083 II->setOutOfDate(false); 2084 2085 // Update the generation for this identifier. 2086 if (getContext().getLangOpts().Modules) 2087 IdentifierGeneration[II] = getGeneration(); 2088 } 2089 2090 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2091 const PendingMacroInfo &PMInfo) { 2092 ModuleFile &M = *PMInfo.M; 2093 2094 BitstreamCursor &Cursor = M.MacroCursor; 2095 SavedStreamPosition SavedPosition(Cursor); 2096 if (llvm::Error Err = 2097 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2098 Error(std::move(Err)); 2099 return; 2100 } 2101 2102 struct ModuleMacroRecord { 2103 SubmoduleID SubModID; 2104 MacroInfo *MI; 2105 SmallVector<SubmoduleID, 8> Overrides; 2106 }; 2107 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2108 2109 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2110 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2111 // macro histroy. 2112 RecordData Record; 2113 while (true) { 2114 Expected<llvm::BitstreamEntry> MaybeEntry = 2115 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2116 if (!MaybeEntry) { 2117 Error(MaybeEntry.takeError()); 2118 return; 2119 } 2120 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2121 2122 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2123 Error("malformed block record in AST file"); 2124 return; 2125 } 2126 2127 Record.clear(); 2128 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2129 if (!MaybePP) { 2130 Error(MaybePP.takeError()); 2131 return; 2132 } 2133 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2134 case PP_MACRO_DIRECTIVE_HISTORY: 2135 break; 2136 2137 case PP_MODULE_MACRO: { 2138 ModuleMacros.push_back(ModuleMacroRecord()); 2139 auto &Info = ModuleMacros.back(); 2140 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2141 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2142 for (int I = 2, N = Record.size(); I != N; ++I) 2143 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2144 continue; 2145 } 2146 2147 default: 2148 Error("malformed block record in AST file"); 2149 return; 2150 } 2151 2152 // We found the macro directive history; that's the last record 2153 // for this macro. 2154 break; 2155 } 2156 2157 // Module macros are listed in reverse dependency order. 2158 { 2159 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2160 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2161 for (auto &MMR : ModuleMacros) { 2162 Overrides.clear(); 2163 for (unsigned ModID : MMR.Overrides) { 2164 Module *Mod = getSubmodule(ModID); 2165 auto *Macro = PP.getModuleMacro(Mod, II); 2166 assert(Macro && "missing definition for overridden macro"); 2167 Overrides.push_back(Macro); 2168 } 2169 2170 bool Inserted = false; 2171 Module *Owner = getSubmodule(MMR.SubModID); 2172 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2173 } 2174 } 2175 2176 // Don't read the directive history for a module; we don't have anywhere 2177 // to put it. 2178 if (M.isModule()) 2179 return; 2180 2181 // Deserialize the macro directives history in reverse source-order. 2182 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2183 unsigned Idx = 0, N = Record.size(); 2184 while (Idx < N) { 2185 MacroDirective *MD = nullptr; 2186 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2187 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2188 switch (K) { 2189 case MacroDirective::MD_Define: { 2190 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2191 MD = PP.AllocateDefMacroDirective(MI, Loc); 2192 break; 2193 } 2194 case MacroDirective::MD_Undefine: 2195 MD = PP.AllocateUndefMacroDirective(Loc); 2196 break; 2197 case MacroDirective::MD_Visibility: 2198 bool isPublic = Record[Idx++]; 2199 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2200 break; 2201 } 2202 2203 if (!Latest) 2204 Latest = MD; 2205 if (Earliest) 2206 Earliest->setPrevious(MD); 2207 Earliest = MD; 2208 } 2209 2210 if (Latest) 2211 PP.setLoadedMacroDirective(II, Earliest, Latest); 2212 } 2213 2214 ASTReader::InputFileInfo 2215 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2216 // Go find this input file. 2217 BitstreamCursor &Cursor = F.InputFilesCursor; 2218 SavedStreamPosition SavedPosition(Cursor); 2219 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2220 // FIXME this drops errors on the floor. 2221 consumeError(std::move(Err)); 2222 } 2223 2224 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2225 if (!MaybeCode) { 2226 // FIXME this drops errors on the floor. 2227 consumeError(MaybeCode.takeError()); 2228 } 2229 unsigned Code = MaybeCode.get(); 2230 RecordData Record; 2231 StringRef Blob; 2232 2233 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2234 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2235 "invalid record type for input file"); 2236 else { 2237 // FIXME this drops errors on the floor. 2238 consumeError(Maybe.takeError()); 2239 } 2240 2241 assert(Record[0] == ID && "Bogus stored ID or offset"); 2242 InputFileInfo R; 2243 R.StoredSize = static_cast<off_t>(Record[1]); 2244 R.StoredTime = static_cast<time_t>(Record[2]); 2245 R.Overridden = static_cast<bool>(Record[3]); 2246 R.Transient = static_cast<bool>(Record[4]); 2247 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2248 R.Filename = std::string(Blob); 2249 ResolveImportedPath(F, R.Filename); 2250 2251 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2252 if (!MaybeEntry) // FIXME this drops errors on the floor. 2253 consumeError(MaybeEntry.takeError()); 2254 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2255 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2256 "expected record type for input file hash"); 2257 2258 Record.clear(); 2259 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2260 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2261 "invalid record type for input file hash"); 2262 else { 2263 // FIXME this drops errors on the floor. 2264 consumeError(Maybe.takeError()); 2265 } 2266 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2267 static_cast<uint64_t>(Record[0]); 2268 return R; 2269 } 2270 2271 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2272 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2273 // If this ID is bogus, just return an empty input file. 2274 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2275 return InputFile(); 2276 2277 // If we've already loaded this input file, return it. 2278 if (F.InputFilesLoaded[ID-1].getFile()) 2279 return F.InputFilesLoaded[ID-1]; 2280 2281 if (F.InputFilesLoaded[ID-1].isNotFound()) 2282 return InputFile(); 2283 2284 // Go find this input file. 2285 BitstreamCursor &Cursor = F.InputFilesCursor; 2286 SavedStreamPosition SavedPosition(Cursor); 2287 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2288 // FIXME this drops errors on the floor. 2289 consumeError(std::move(Err)); 2290 } 2291 2292 InputFileInfo FI = readInputFileInfo(F, ID); 2293 off_t StoredSize = FI.StoredSize; 2294 time_t StoredTime = FI.StoredTime; 2295 bool Overridden = FI.Overridden; 2296 bool Transient = FI.Transient; 2297 StringRef Filename = FI.Filename; 2298 uint64_t StoredContentHash = FI.ContentHash; 2299 2300 OptionalFileEntryRefDegradesToFileEntryPtr File = 2301 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2302 2303 // If we didn't find the file, resolve it relative to the 2304 // original directory from which this AST file was created. 2305 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2306 F.OriginalDir != F.BaseDirectory) { 2307 std::string Resolved = resolveFileRelativeToOriginalDir( 2308 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2309 if (!Resolved.empty()) 2310 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2311 } 2312 2313 // For an overridden file, create a virtual file with the stored 2314 // size/timestamp. 2315 if ((Overridden || Transient) && !File) 2316 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2317 2318 if (!File) { 2319 if (Complain) { 2320 std::string ErrorStr = "could not find file '"; 2321 ErrorStr += Filename; 2322 ErrorStr += "' referenced by AST file '"; 2323 ErrorStr += F.FileName; 2324 ErrorStr += "'"; 2325 Error(ErrorStr); 2326 } 2327 // Record that we didn't find the file. 2328 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2329 return InputFile(); 2330 } 2331 2332 // Check if there was a request to override the contents of the file 2333 // that was part of the precompiled header. Overriding such a file 2334 // can lead to problems when lexing using the source locations from the 2335 // PCH. 2336 SourceManager &SM = getSourceManager(); 2337 // FIXME: Reject if the overrides are different. 2338 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2339 if (Complain) 2340 Error(diag::err_fe_pch_file_overridden, Filename); 2341 2342 // After emitting the diagnostic, bypass the overriding file to recover 2343 // (this creates a separate FileEntry). 2344 File = SM.bypassFileContentsOverride(*File); 2345 if (!File) { 2346 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2347 return InputFile(); 2348 } 2349 } 2350 2351 enum ModificationType { 2352 Size, 2353 ModTime, 2354 Content, 2355 None, 2356 }; 2357 auto HasInputFileChanged = [&]() { 2358 if (StoredSize != File->getSize()) 2359 return ModificationType::Size; 2360 if (!DisableValidation && StoredTime && 2361 StoredTime != File->getModificationTime()) { 2362 // In case the modification time changes but not the content, 2363 // accept the cached file as legit. 2364 if (ValidateASTInputFilesContent && 2365 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2366 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2367 if (!MemBuffOrError) { 2368 if (!Complain) 2369 return ModificationType::ModTime; 2370 std::string ErrorStr = "could not get buffer for file '"; 2371 ErrorStr += File->getName(); 2372 ErrorStr += "'"; 2373 Error(ErrorStr); 2374 return ModificationType::ModTime; 2375 } 2376 2377 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2378 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2379 return ModificationType::None; 2380 return ModificationType::Content; 2381 } 2382 return ModificationType::ModTime; 2383 } 2384 return ModificationType::None; 2385 }; 2386 2387 bool IsOutOfDate = false; 2388 auto FileChange = HasInputFileChanged(); 2389 // For an overridden file, there is nothing to validate. 2390 if (!Overridden && FileChange != ModificationType::None) { 2391 if (Complain && !Diags.isDiagnosticInFlight()) { 2392 // Build a list of the PCH imports that got us here (in reverse). 2393 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2394 while (!ImportStack.back()->ImportedBy.empty()) 2395 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2396 2397 // The top-level PCH is stale. 2398 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2399 Diag(diag::err_fe_ast_file_modified) 2400 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2401 << TopLevelPCHName << FileChange; 2402 2403 // Print the import stack. 2404 if (ImportStack.size() > 1) { 2405 Diag(diag::note_pch_required_by) 2406 << Filename << ImportStack[0]->FileName; 2407 for (unsigned I = 1; I < ImportStack.size(); ++I) 2408 Diag(diag::note_pch_required_by) 2409 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2410 } 2411 2412 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2413 } 2414 2415 IsOutOfDate = true; 2416 } 2417 // FIXME: If the file is overridden and we've already opened it, 2418 // issue an error (or split it into a separate FileEntry). 2419 2420 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2421 2422 // Note that we've loaded this input file. 2423 F.InputFilesLoaded[ID-1] = IF; 2424 return IF; 2425 } 2426 2427 /// If we are loading a relocatable PCH or module file, and the filename 2428 /// is not an absolute path, add the system or module root to the beginning of 2429 /// the file name. 2430 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2431 // Resolve relative to the base directory, if we have one. 2432 if (!M.BaseDirectory.empty()) 2433 return ResolveImportedPath(Filename, M.BaseDirectory); 2434 } 2435 2436 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2437 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2438 return; 2439 2440 SmallString<128> Buffer; 2441 llvm::sys::path::append(Buffer, Prefix, Filename); 2442 Filename.assign(Buffer.begin(), Buffer.end()); 2443 } 2444 2445 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2446 switch (ARR) { 2447 case ASTReader::Failure: return true; 2448 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2449 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2450 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2451 case ASTReader::ConfigurationMismatch: 2452 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2453 case ASTReader::HadErrors: return true; 2454 case ASTReader::Success: return false; 2455 } 2456 2457 llvm_unreachable("unknown ASTReadResult"); 2458 } 2459 2460 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2461 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2462 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2463 std::string &SuggestedPredefines) { 2464 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2465 // FIXME this drops errors on the floor. 2466 consumeError(std::move(Err)); 2467 return Failure; 2468 } 2469 2470 // Read all of the records in the options block. 2471 RecordData Record; 2472 ASTReadResult Result = Success; 2473 while (true) { 2474 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2475 if (!MaybeEntry) { 2476 // FIXME this drops errors on the floor. 2477 consumeError(MaybeEntry.takeError()); 2478 return Failure; 2479 } 2480 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2481 2482 switch (Entry.Kind) { 2483 case llvm::BitstreamEntry::Error: 2484 case llvm::BitstreamEntry::SubBlock: 2485 return Failure; 2486 2487 case llvm::BitstreamEntry::EndBlock: 2488 return Result; 2489 2490 case llvm::BitstreamEntry::Record: 2491 // The interesting case. 2492 break; 2493 } 2494 2495 // Read and process a record. 2496 Record.clear(); 2497 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2498 if (!MaybeRecordType) { 2499 // FIXME this drops errors on the floor. 2500 consumeError(MaybeRecordType.takeError()); 2501 return Failure; 2502 } 2503 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2504 case LANGUAGE_OPTIONS: { 2505 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2506 if (ParseLanguageOptions(Record, Complain, Listener, 2507 AllowCompatibleConfigurationMismatch)) 2508 Result = ConfigurationMismatch; 2509 break; 2510 } 2511 2512 case TARGET_OPTIONS: { 2513 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2514 if (ParseTargetOptions(Record, Complain, Listener, 2515 AllowCompatibleConfigurationMismatch)) 2516 Result = ConfigurationMismatch; 2517 break; 2518 } 2519 2520 case FILE_SYSTEM_OPTIONS: { 2521 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2522 if (!AllowCompatibleConfigurationMismatch && 2523 ParseFileSystemOptions(Record, Complain, Listener)) 2524 Result = ConfigurationMismatch; 2525 break; 2526 } 2527 2528 case HEADER_SEARCH_OPTIONS: { 2529 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2530 if (!AllowCompatibleConfigurationMismatch && 2531 ParseHeaderSearchOptions(Record, Complain, Listener)) 2532 Result = ConfigurationMismatch; 2533 break; 2534 } 2535 2536 case PREPROCESSOR_OPTIONS: 2537 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2538 if (!AllowCompatibleConfigurationMismatch && 2539 ParsePreprocessorOptions(Record, Complain, Listener, 2540 SuggestedPredefines)) 2541 Result = ConfigurationMismatch; 2542 break; 2543 } 2544 } 2545 } 2546 2547 ASTReader::ASTReadResult 2548 ASTReader::ReadControlBlock(ModuleFile &F, 2549 SmallVectorImpl<ImportedModule> &Loaded, 2550 const ModuleFile *ImportedBy, 2551 unsigned ClientLoadCapabilities) { 2552 BitstreamCursor &Stream = F.Stream; 2553 2554 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2555 Error(std::move(Err)); 2556 return Failure; 2557 } 2558 2559 // Lambda to read the unhashed control block the first time it's called. 2560 // 2561 // For PCM files, the unhashed control block cannot be read until after the 2562 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2563 // need to look ahead before reading the IMPORTS record. For consistency, 2564 // this block is always read somehow (see BitstreamEntry::EndBlock). 2565 bool HasReadUnhashedControlBlock = false; 2566 auto readUnhashedControlBlockOnce = [&]() { 2567 if (!HasReadUnhashedControlBlock) { 2568 HasReadUnhashedControlBlock = true; 2569 if (ASTReadResult Result = 2570 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2571 return Result; 2572 } 2573 return Success; 2574 }; 2575 2576 // Read all of the records and blocks in the control block. 2577 RecordData Record; 2578 unsigned NumInputs = 0; 2579 unsigned NumUserInputs = 0; 2580 StringRef BaseDirectoryAsWritten; 2581 while (true) { 2582 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2583 if (!MaybeEntry) { 2584 Error(MaybeEntry.takeError()); 2585 return Failure; 2586 } 2587 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2588 2589 switch (Entry.Kind) { 2590 case llvm::BitstreamEntry::Error: 2591 Error("malformed block record in AST file"); 2592 return Failure; 2593 case llvm::BitstreamEntry::EndBlock: { 2594 // Validate the module before returning. This call catches an AST with 2595 // no module name and no imports. 2596 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2597 return Result; 2598 2599 // Validate input files. 2600 const HeaderSearchOptions &HSOpts = 2601 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2602 2603 // All user input files reside at the index range [0, NumUserInputs), and 2604 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2605 // loaded module files, ignore missing inputs. 2606 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2607 F.Kind != MK_PrebuiltModule) { 2608 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2609 2610 // If we are reading a module, we will create a verification timestamp, 2611 // so we verify all input files. Otherwise, verify only user input 2612 // files. 2613 2614 unsigned N = NumUserInputs; 2615 if (ValidateSystemInputs || 2616 (HSOpts.ModulesValidateOncePerBuildSession && 2617 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2618 F.Kind == MK_ImplicitModule)) 2619 N = NumInputs; 2620 2621 for (unsigned I = 0; I < N; ++I) { 2622 InputFile IF = getInputFile(F, I+1, Complain); 2623 if (!IF.getFile() || IF.isOutOfDate()) 2624 return OutOfDate; 2625 } 2626 } 2627 2628 if (Listener) 2629 Listener->visitModuleFile(F.FileName, F.Kind); 2630 2631 if (Listener && Listener->needsInputFileVisitation()) { 2632 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2633 : NumUserInputs; 2634 for (unsigned I = 0; I < N; ++I) { 2635 bool IsSystem = I >= NumUserInputs; 2636 InputFileInfo FI = readInputFileInfo(F, I+1); 2637 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2638 F.Kind == MK_ExplicitModule || 2639 F.Kind == MK_PrebuiltModule); 2640 } 2641 } 2642 2643 return Success; 2644 } 2645 2646 case llvm::BitstreamEntry::SubBlock: 2647 switch (Entry.ID) { 2648 case INPUT_FILES_BLOCK_ID: 2649 F.InputFilesCursor = Stream; 2650 if (llvm::Error Err = Stream.SkipBlock()) { 2651 Error(std::move(Err)); 2652 return Failure; 2653 } 2654 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2655 Error("malformed block record in AST file"); 2656 return Failure; 2657 } 2658 continue; 2659 2660 case OPTIONS_BLOCK_ID: 2661 // If we're reading the first module for this group, check its options 2662 // are compatible with ours. For modules it imports, no further checking 2663 // is required, because we checked them when we built it. 2664 if (Listener && !ImportedBy) { 2665 // Should we allow the configuration of the module file to differ from 2666 // the configuration of the current translation unit in a compatible 2667 // way? 2668 // 2669 // FIXME: Allow this for files explicitly specified with -include-pch. 2670 bool AllowCompatibleConfigurationMismatch = 2671 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2672 2673 ASTReadResult Result = 2674 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2675 AllowCompatibleConfigurationMismatch, *Listener, 2676 SuggestedPredefines); 2677 if (Result == Failure) { 2678 Error("malformed block record in AST file"); 2679 return Result; 2680 } 2681 2682 if (DisableValidation || 2683 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2684 Result = Success; 2685 2686 // If we can't load the module, exit early since we likely 2687 // will rebuild the module anyway. The stream may be in the 2688 // middle of a block. 2689 if (Result != Success) 2690 return Result; 2691 } else if (llvm::Error Err = Stream.SkipBlock()) { 2692 Error(std::move(Err)); 2693 return Failure; 2694 } 2695 continue; 2696 2697 default: 2698 if (llvm::Error Err = Stream.SkipBlock()) { 2699 Error(std::move(Err)); 2700 return Failure; 2701 } 2702 continue; 2703 } 2704 2705 case llvm::BitstreamEntry::Record: 2706 // The interesting case. 2707 break; 2708 } 2709 2710 // Read and process a record. 2711 Record.clear(); 2712 StringRef Blob; 2713 Expected<unsigned> MaybeRecordType = 2714 Stream.readRecord(Entry.ID, Record, &Blob); 2715 if (!MaybeRecordType) { 2716 Error(MaybeRecordType.takeError()); 2717 return Failure; 2718 } 2719 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2720 case METADATA: { 2721 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2722 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2723 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2724 : diag::err_pch_version_too_new); 2725 return VersionMismatch; 2726 } 2727 2728 bool hasErrors = Record[6]; 2729 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2730 Diag(diag::err_pch_with_compiler_errors); 2731 return HadErrors; 2732 } 2733 if (hasErrors) { 2734 Diags.ErrorOccurred = true; 2735 Diags.UncompilableErrorOccurred = true; 2736 Diags.UnrecoverableErrorOccurred = true; 2737 } 2738 2739 F.RelocatablePCH = Record[4]; 2740 // Relative paths in a relocatable PCH are relative to our sysroot. 2741 if (F.RelocatablePCH) 2742 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2743 2744 F.HasTimestamps = Record[5]; 2745 2746 const std::string &CurBranch = getClangFullRepositoryVersion(); 2747 StringRef ASTBranch = Blob; 2748 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2749 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2750 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2751 return VersionMismatch; 2752 } 2753 break; 2754 } 2755 2756 case IMPORTS: { 2757 // Validate the AST before processing any imports (otherwise, untangling 2758 // them can be error-prone and expensive). A module will have a name and 2759 // will already have been validated, but this catches the PCH case. 2760 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2761 return Result; 2762 2763 // Load each of the imported PCH files. 2764 unsigned Idx = 0, N = Record.size(); 2765 while (Idx < N) { 2766 // Read information about the AST file. 2767 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2768 // The import location will be the local one for now; we will adjust 2769 // all import locations of module imports after the global source 2770 // location info are setup, in ReadAST. 2771 SourceLocation ImportLoc = 2772 ReadUntranslatedSourceLocation(Record[Idx++]); 2773 off_t StoredSize = (off_t)Record[Idx++]; 2774 time_t StoredModTime = (time_t)Record[Idx++]; 2775 auto FirstSignatureByte = Record.begin() + Idx; 2776 ASTFileSignature StoredSignature = ASTFileSignature::create( 2777 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2778 Idx += ASTFileSignature::size; 2779 2780 std::string ImportedName = ReadString(Record, Idx); 2781 std::string ImportedFile; 2782 2783 // For prebuilt and explicit modules first consult the file map for 2784 // an override. Note that here we don't search prebuilt module 2785 // directories, only the explicit name to file mappings. Also, we will 2786 // still verify the size/signature making sure it is essentially the 2787 // same file but perhaps in a different location. 2788 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2789 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2790 ImportedName, /*FileMapOnly*/ true); 2791 2792 if (ImportedFile.empty()) 2793 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2794 // ModuleCache as when writing. 2795 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2796 else 2797 SkipPath(Record, Idx); 2798 2799 // If our client can't cope with us being out of date, we can't cope with 2800 // our dependency being missing. 2801 unsigned Capabilities = ClientLoadCapabilities; 2802 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2803 Capabilities &= ~ARR_Missing; 2804 2805 // Load the AST file. 2806 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2807 Loaded, StoredSize, StoredModTime, 2808 StoredSignature, Capabilities); 2809 2810 // If we diagnosed a problem, produce a backtrace. 2811 if (isDiagnosedResult(Result, Capabilities)) 2812 Diag(diag::note_module_file_imported_by) 2813 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2814 2815 switch (Result) { 2816 case Failure: return Failure; 2817 // If we have to ignore the dependency, we'll have to ignore this too. 2818 case Missing: 2819 case OutOfDate: return OutOfDate; 2820 case VersionMismatch: return VersionMismatch; 2821 case ConfigurationMismatch: return ConfigurationMismatch; 2822 case HadErrors: return HadErrors; 2823 case Success: break; 2824 } 2825 } 2826 break; 2827 } 2828 2829 case ORIGINAL_FILE: 2830 F.OriginalSourceFileID = FileID::get(Record[0]); 2831 F.ActualOriginalSourceFileName = std::string(Blob); 2832 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2833 ResolveImportedPath(F, F.OriginalSourceFileName); 2834 break; 2835 2836 case ORIGINAL_FILE_ID: 2837 F.OriginalSourceFileID = FileID::get(Record[0]); 2838 break; 2839 2840 case ORIGINAL_PCH_DIR: 2841 F.OriginalDir = std::string(Blob); 2842 break; 2843 2844 case MODULE_NAME: 2845 F.ModuleName = std::string(Blob); 2846 Diag(diag::remark_module_import) 2847 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2848 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2849 if (Listener) 2850 Listener->ReadModuleName(F.ModuleName); 2851 2852 // Validate the AST as soon as we have a name so we can exit early on 2853 // failure. 2854 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2855 return Result; 2856 2857 break; 2858 2859 case MODULE_DIRECTORY: { 2860 // Save the BaseDirectory as written in the PCM for computing the module 2861 // filename for the ModuleCache. 2862 BaseDirectoryAsWritten = Blob; 2863 assert(!F.ModuleName.empty() && 2864 "MODULE_DIRECTORY found before MODULE_NAME"); 2865 // If we've already loaded a module map file covering this module, we may 2866 // have a better path for it (relative to the current build). 2867 Module *M = PP.getHeaderSearchInfo().lookupModule( 2868 F.ModuleName, /*AllowSearch*/ true, 2869 /*AllowExtraModuleMapSearch*/ true); 2870 if (M && M->Directory) { 2871 // If we're implicitly loading a module, the base directory can't 2872 // change between the build and use. 2873 // Don't emit module relocation error if we have -fno-validate-pch 2874 if (!PP.getPreprocessorOpts().DisablePCHValidation && 2875 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2876 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2877 if (!BuildDir || *BuildDir != M->Directory) { 2878 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2879 Diag(diag::err_imported_module_relocated) 2880 << F.ModuleName << Blob << M->Directory->getName(); 2881 return OutOfDate; 2882 } 2883 } 2884 F.BaseDirectory = std::string(M->Directory->getName()); 2885 } else { 2886 F.BaseDirectory = std::string(Blob); 2887 } 2888 break; 2889 } 2890 2891 case MODULE_MAP_FILE: 2892 if (ASTReadResult Result = 2893 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2894 return Result; 2895 break; 2896 2897 case INPUT_FILE_OFFSETS: 2898 NumInputs = Record[0]; 2899 NumUserInputs = Record[1]; 2900 F.InputFileOffsets = 2901 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2902 F.InputFilesLoaded.resize(NumInputs); 2903 F.NumUserInputFiles = NumUserInputs; 2904 break; 2905 } 2906 } 2907 } 2908 2909 ASTReader::ASTReadResult 2910 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2911 BitstreamCursor &Stream = F.Stream; 2912 2913 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) { 2914 Error(std::move(Err)); 2915 return Failure; 2916 } 2917 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2918 2919 // Read all of the records and blocks for the AST file. 2920 RecordData Record; 2921 while (true) { 2922 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2923 if (!MaybeEntry) { 2924 Error(MaybeEntry.takeError()); 2925 return Failure; 2926 } 2927 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2928 2929 switch (Entry.Kind) { 2930 case llvm::BitstreamEntry::Error: 2931 Error("error at end of module block in AST file"); 2932 return Failure; 2933 case llvm::BitstreamEntry::EndBlock: 2934 // Outside of C++, we do not store a lookup map for the translation unit. 2935 // Instead, mark it as needing a lookup map to be built if this module 2936 // contains any declarations lexically within it (which it always does!). 2937 // This usually has no cost, since we very rarely need the lookup map for 2938 // the translation unit outside C++. 2939 if (ASTContext *Ctx = ContextObj) { 2940 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2941 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2942 DC->setMustBuildLookupTable(); 2943 } 2944 2945 return Success; 2946 case llvm::BitstreamEntry::SubBlock: 2947 switch (Entry.ID) { 2948 case DECLTYPES_BLOCK_ID: 2949 // We lazily load the decls block, but we want to set up the 2950 // DeclsCursor cursor to point into it. Clone our current bitcode 2951 // cursor to it, enter the block and read the abbrevs in that block. 2952 // With the main cursor, we just skip over it. 2953 F.DeclsCursor = Stream; 2954 if (llvm::Error Err = Stream.SkipBlock()) { 2955 Error(std::move(Err)); 2956 return Failure; 2957 } 2958 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID, 2959 &F.DeclsBlockStartOffset)) { 2960 Error("malformed block record in AST file"); 2961 return Failure; 2962 } 2963 break; 2964 2965 case PREPROCESSOR_BLOCK_ID: 2966 F.MacroCursor = Stream; 2967 if (!PP.getExternalSource()) 2968 PP.setExternalSource(this); 2969 2970 if (llvm::Error Err = Stream.SkipBlock()) { 2971 Error(std::move(Err)); 2972 return Failure; 2973 } 2974 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2975 Error("malformed block record in AST file"); 2976 return Failure; 2977 } 2978 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 2979 break; 2980 2981 case PREPROCESSOR_DETAIL_BLOCK_ID: 2982 F.PreprocessorDetailCursor = Stream; 2983 2984 if (llvm::Error Err = Stream.SkipBlock()) { 2985 Error(std::move(Err)); 2986 return Failure; 2987 } 2988 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor, 2989 PREPROCESSOR_DETAIL_BLOCK_ID)) { 2990 Error("malformed preprocessor detail record in AST file"); 2991 return Failure; 2992 } 2993 F.PreprocessorDetailStartOffset 2994 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 2995 2996 if (!PP.getPreprocessingRecord()) 2997 PP.createPreprocessingRecord(); 2998 if (!PP.getPreprocessingRecord()->getExternalSource()) 2999 PP.getPreprocessingRecord()->SetExternalSource(*this); 3000 break; 3001 3002 case SOURCE_MANAGER_BLOCK_ID: 3003 if (ReadSourceManagerBlock(F)) 3004 return Failure; 3005 break; 3006 3007 case SUBMODULE_BLOCK_ID: 3008 if (ASTReadResult Result = 3009 ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3010 return Result; 3011 break; 3012 3013 case COMMENTS_BLOCK_ID: { 3014 BitstreamCursor C = Stream; 3015 3016 if (llvm::Error Err = Stream.SkipBlock()) { 3017 Error(std::move(Err)); 3018 return Failure; 3019 } 3020 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 3021 Error("malformed comments block in AST file"); 3022 return Failure; 3023 } 3024 CommentsCursors.push_back(std::make_pair(C, &F)); 3025 break; 3026 } 3027 3028 default: 3029 if (llvm::Error Err = Stream.SkipBlock()) { 3030 Error(std::move(Err)); 3031 return Failure; 3032 } 3033 break; 3034 } 3035 continue; 3036 3037 case llvm::BitstreamEntry::Record: 3038 // The interesting case. 3039 break; 3040 } 3041 3042 // Read and process a record. 3043 Record.clear(); 3044 StringRef Blob; 3045 Expected<unsigned> MaybeRecordType = 3046 Stream.readRecord(Entry.ID, Record, &Blob); 3047 if (!MaybeRecordType) { 3048 Error(MaybeRecordType.takeError()); 3049 return Failure; 3050 } 3051 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3052 3053 // If we're not loading an AST context, we don't care about most records. 3054 if (!ContextObj) { 3055 switch (RecordType) { 3056 case IDENTIFIER_TABLE: 3057 case IDENTIFIER_OFFSET: 3058 case INTERESTING_IDENTIFIERS: 3059 case STATISTICS: 3060 case PP_CONDITIONAL_STACK: 3061 case PP_COUNTER_VALUE: 3062 case SOURCE_LOCATION_OFFSETS: 3063 case MODULE_OFFSET_MAP: 3064 case SOURCE_MANAGER_LINE_TABLE: 3065 case SOURCE_LOCATION_PRELOADS: 3066 case PPD_ENTITIES_OFFSETS: 3067 case HEADER_SEARCH_TABLE: 3068 case IMPORTED_MODULES: 3069 case MACRO_OFFSET: 3070 break; 3071 default: 3072 continue; 3073 } 3074 } 3075 3076 switch (RecordType) { 3077 default: // Default behavior: ignore. 3078 break; 3079 3080 case TYPE_OFFSET: { 3081 if (F.LocalNumTypes != 0) { 3082 Error("duplicate TYPE_OFFSET record in AST file"); 3083 return Failure; 3084 } 3085 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3086 F.LocalNumTypes = Record[0]; 3087 unsigned LocalBaseTypeIndex = Record[1]; 3088 F.BaseTypeIndex = getTotalNumTypes(); 3089 3090 if (F.LocalNumTypes > 0) { 3091 // Introduce the global -> local mapping for types within this module. 3092 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3093 3094 // Introduce the local -> global mapping for types within this module. 3095 F.TypeRemap.insertOrReplace( 3096 std::make_pair(LocalBaseTypeIndex, 3097 F.BaseTypeIndex - LocalBaseTypeIndex)); 3098 3099 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3100 } 3101 break; 3102 } 3103 3104 case DECL_OFFSET: { 3105 if (F.LocalNumDecls != 0) { 3106 Error("duplicate DECL_OFFSET record in AST file"); 3107 return Failure; 3108 } 3109 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3110 F.LocalNumDecls = Record[0]; 3111 unsigned LocalBaseDeclID = Record[1]; 3112 F.BaseDeclID = getTotalNumDecls(); 3113 3114 if (F.LocalNumDecls > 0) { 3115 // Introduce the global -> local mapping for declarations within this 3116 // module. 3117 GlobalDeclMap.insert( 3118 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3119 3120 // Introduce the local -> global mapping for declarations within this 3121 // module. 3122 F.DeclRemap.insertOrReplace( 3123 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3124 3125 // Introduce the global -> local mapping for declarations within this 3126 // module. 3127 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3128 3129 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3130 } 3131 break; 3132 } 3133 3134 case TU_UPDATE_LEXICAL: { 3135 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3136 LexicalContents Contents( 3137 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3138 Blob.data()), 3139 static_cast<unsigned int>(Blob.size() / 4)); 3140 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3141 TU->setHasExternalLexicalStorage(true); 3142 break; 3143 } 3144 3145 case UPDATE_VISIBLE: { 3146 unsigned Idx = 0; 3147 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3148 auto *Data = (const unsigned char*)Blob.data(); 3149 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3150 // If we've already loaded the decl, perform the updates when we finish 3151 // loading this block. 3152 if (Decl *D = GetExistingDecl(ID)) 3153 PendingUpdateRecords.push_back( 3154 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3155 break; 3156 } 3157 3158 case IDENTIFIER_TABLE: 3159 F.IdentifierTableData = Blob.data(); 3160 if (Record[0]) { 3161 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3162 (const unsigned char *)F.IdentifierTableData + Record[0], 3163 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 3164 (const unsigned char *)F.IdentifierTableData, 3165 ASTIdentifierLookupTrait(*this, F)); 3166 3167 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3168 } 3169 break; 3170 3171 case IDENTIFIER_OFFSET: { 3172 if (F.LocalNumIdentifiers != 0) { 3173 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 3174 return Failure; 3175 } 3176 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3177 F.LocalNumIdentifiers = Record[0]; 3178 unsigned LocalBaseIdentifierID = Record[1]; 3179 F.BaseIdentifierID = getTotalNumIdentifiers(); 3180 3181 if (F.LocalNumIdentifiers > 0) { 3182 // Introduce the global -> local mapping for identifiers within this 3183 // module. 3184 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3185 &F)); 3186 3187 // Introduce the local -> global mapping for identifiers within this 3188 // module. 3189 F.IdentifierRemap.insertOrReplace( 3190 std::make_pair(LocalBaseIdentifierID, 3191 F.BaseIdentifierID - LocalBaseIdentifierID)); 3192 3193 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3194 + F.LocalNumIdentifiers); 3195 } 3196 break; 3197 } 3198 3199 case INTERESTING_IDENTIFIERS: 3200 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3201 break; 3202 3203 case EAGERLY_DESERIALIZED_DECLS: 3204 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3205 // about "interesting" decls (for instance, if we're building a module). 3206 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3207 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3208 break; 3209 3210 case MODULAR_CODEGEN_DECLS: 3211 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3212 // them (ie: if we're not codegenerating this module). 3213 if (F.Kind == MK_MainFile || 3214 getContext().getLangOpts().BuildingPCHWithObjectFile) 3215 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3216 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3217 break; 3218 3219 case SPECIAL_TYPES: 3220 if (SpecialTypes.empty()) { 3221 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3222 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3223 break; 3224 } 3225 3226 if (SpecialTypes.size() != Record.size()) { 3227 Error("invalid special-types record"); 3228 return Failure; 3229 } 3230 3231 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3232 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3233 if (!SpecialTypes[I]) 3234 SpecialTypes[I] = ID; 3235 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3236 // merge step? 3237 } 3238 break; 3239 3240 case STATISTICS: 3241 TotalNumStatements += Record[0]; 3242 TotalNumMacros += Record[1]; 3243 TotalLexicalDeclContexts += Record[2]; 3244 TotalVisibleDeclContexts += Record[3]; 3245 break; 3246 3247 case UNUSED_FILESCOPED_DECLS: 3248 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3249 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3250 break; 3251 3252 case DELEGATING_CTORS: 3253 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3254 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3255 break; 3256 3257 case WEAK_UNDECLARED_IDENTIFIERS: 3258 if (Record.size() % 4 != 0) { 3259 Error("invalid weak identifiers record"); 3260 return Failure; 3261 } 3262 3263 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3264 // files. This isn't the way to do it :) 3265 WeakUndeclaredIdentifiers.clear(); 3266 3267 // Translate the weak, undeclared identifiers into global IDs. 3268 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3269 WeakUndeclaredIdentifiers.push_back( 3270 getGlobalIdentifierID(F, Record[I++])); 3271 WeakUndeclaredIdentifiers.push_back( 3272 getGlobalIdentifierID(F, Record[I++])); 3273 WeakUndeclaredIdentifiers.push_back( 3274 ReadSourceLocation(F, Record, I).getRawEncoding()); 3275 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3276 } 3277 break; 3278 3279 case SELECTOR_OFFSETS: { 3280 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3281 F.LocalNumSelectors = Record[0]; 3282 unsigned LocalBaseSelectorID = Record[1]; 3283 F.BaseSelectorID = getTotalNumSelectors(); 3284 3285 if (F.LocalNumSelectors > 0) { 3286 // Introduce the global -> local mapping for selectors within this 3287 // module. 3288 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3289 3290 // Introduce the local -> global mapping for selectors within this 3291 // module. 3292 F.SelectorRemap.insertOrReplace( 3293 std::make_pair(LocalBaseSelectorID, 3294 F.BaseSelectorID - LocalBaseSelectorID)); 3295 3296 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3297 } 3298 break; 3299 } 3300 3301 case METHOD_POOL: 3302 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3303 if (Record[0]) 3304 F.SelectorLookupTable 3305 = ASTSelectorLookupTable::Create( 3306 F.SelectorLookupTableData + Record[0], 3307 F.SelectorLookupTableData, 3308 ASTSelectorLookupTrait(*this, F)); 3309 TotalNumMethodPoolEntries += Record[1]; 3310 break; 3311 3312 case REFERENCED_SELECTOR_POOL: 3313 if (!Record.empty()) { 3314 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3315 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3316 Record[Idx++])); 3317 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3318 getRawEncoding()); 3319 } 3320 } 3321 break; 3322 3323 case PP_CONDITIONAL_STACK: 3324 if (!Record.empty()) { 3325 unsigned Idx = 0, End = Record.size() - 1; 3326 bool ReachedEOFWhileSkipping = Record[Idx++]; 3327 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3328 if (ReachedEOFWhileSkipping) { 3329 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3330 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3331 bool FoundNonSkipPortion = Record[Idx++]; 3332 bool FoundElse = Record[Idx++]; 3333 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3334 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3335 FoundElse, ElseLoc); 3336 } 3337 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3338 while (Idx < End) { 3339 auto Loc = ReadSourceLocation(F, Record, Idx); 3340 bool WasSkipping = Record[Idx++]; 3341 bool FoundNonSkip = Record[Idx++]; 3342 bool FoundElse = Record[Idx++]; 3343 ConditionalStack.push_back( 3344 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3345 } 3346 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3347 } 3348 break; 3349 3350 case PP_COUNTER_VALUE: 3351 if (!Record.empty() && Listener) 3352 Listener->ReadCounter(F, Record[0]); 3353 break; 3354 3355 case FILE_SORTED_DECLS: 3356 F.FileSortedDecls = (const DeclID *)Blob.data(); 3357 F.NumFileSortedDecls = Record[0]; 3358 break; 3359 3360 case SOURCE_LOCATION_OFFSETS: { 3361 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3362 F.LocalNumSLocEntries = Record[0]; 3363 unsigned SLocSpaceSize = Record[1]; 3364 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3365 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3366 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3367 SLocSpaceSize); 3368 if (!F.SLocEntryBaseID) { 3369 Error("ran out of source locations"); 3370 break; 3371 } 3372 // Make our entry in the range map. BaseID is negative and growing, so 3373 // we invert it. Because we invert it, though, we need the other end of 3374 // the range. 3375 unsigned RangeStart = 3376 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3377 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3378 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3379 3380 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3381 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3382 GlobalSLocOffsetMap.insert( 3383 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3384 - SLocSpaceSize,&F)); 3385 3386 // Initialize the remapping table. 3387 // Invalid stays invalid. 3388 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3389 // This module. Base was 2 when being compiled. 3390 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3391 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3392 3393 TotalNumSLocEntries += F.LocalNumSLocEntries; 3394 break; 3395 } 3396 3397 case MODULE_OFFSET_MAP: 3398 F.ModuleOffsetMap = Blob; 3399 break; 3400 3401 case SOURCE_MANAGER_LINE_TABLE: 3402 if (ParseLineTable(F, Record)) { 3403 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file"); 3404 return Failure; 3405 } 3406 break; 3407 3408 case SOURCE_LOCATION_PRELOADS: { 3409 // Need to transform from the local view (1-based IDs) to the global view, 3410 // which is based off F.SLocEntryBaseID. 3411 if (!F.PreloadSLocEntries.empty()) { 3412 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3413 return Failure; 3414 } 3415 3416 F.PreloadSLocEntries.swap(Record); 3417 break; 3418 } 3419 3420 case EXT_VECTOR_DECLS: 3421 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3422 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3423 break; 3424 3425 case VTABLE_USES: 3426 if (Record.size() % 3 != 0) { 3427 Error("Invalid VTABLE_USES record"); 3428 return Failure; 3429 } 3430 3431 // Later tables overwrite earlier ones. 3432 // FIXME: Modules will have some trouble with this. This is clearly not 3433 // the right way to do this. 3434 VTableUses.clear(); 3435 3436 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3437 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3438 VTableUses.push_back( 3439 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3440 VTableUses.push_back(Record[Idx++]); 3441 } 3442 break; 3443 3444 case PENDING_IMPLICIT_INSTANTIATIONS: 3445 if (PendingInstantiations.size() % 2 != 0) { 3446 Error("Invalid existing PendingInstantiations"); 3447 return Failure; 3448 } 3449 3450 if (Record.size() % 2 != 0) { 3451 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3452 return Failure; 3453 } 3454 3455 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3456 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3457 PendingInstantiations.push_back( 3458 ReadSourceLocation(F, Record, I).getRawEncoding()); 3459 } 3460 break; 3461 3462 case SEMA_DECL_REFS: 3463 if (Record.size() != 3) { 3464 Error("Invalid SEMA_DECL_REFS block"); 3465 return Failure; 3466 } 3467 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3468 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3469 break; 3470 3471 case PPD_ENTITIES_OFFSETS: { 3472 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3473 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3474 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3475 3476 unsigned LocalBasePreprocessedEntityID = Record[0]; 3477 3478 unsigned StartingID; 3479 if (!PP.getPreprocessingRecord()) 3480 PP.createPreprocessingRecord(); 3481 if (!PP.getPreprocessingRecord()->getExternalSource()) 3482 PP.getPreprocessingRecord()->SetExternalSource(*this); 3483 StartingID 3484 = PP.getPreprocessingRecord() 3485 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3486 F.BasePreprocessedEntityID = StartingID; 3487 3488 if (F.NumPreprocessedEntities > 0) { 3489 // Introduce the global -> local mapping for preprocessed entities in 3490 // this module. 3491 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3492 3493 // Introduce the local -> global mapping for preprocessed entities in 3494 // this module. 3495 F.PreprocessedEntityRemap.insertOrReplace( 3496 std::make_pair(LocalBasePreprocessedEntityID, 3497 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3498 } 3499 3500 break; 3501 } 3502 3503 case PPD_SKIPPED_RANGES: { 3504 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3505 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3506 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3507 3508 if (!PP.getPreprocessingRecord()) 3509 PP.createPreprocessingRecord(); 3510 if (!PP.getPreprocessingRecord()->getExternalSource()) 3511 PP.getPreprocessingRecord()->SetExternalSource(*this); 3512 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3513 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3514 3515 if (F.NumPreprocessedSkippedRanges > 0) 3516 GlobalSkippedRangeMap.insert( 3517 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3518 break; 3519 } 3520 3521 case DECL_UPDATE_OFFSETS: 3522 if (Record.size() % 2 != 0) { 3523 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3524 return Failure; 3525 } 3526 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3527 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3528 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3529 3530 // If we've already loaded the decl, perform the updates when we finish 3531 // loading this block. 3532 if (Decl *D = GetExistingDecl(ID)) 3533 PendingUpdateRecords.push_back( 3534 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3535 } 3536 break; 3537 3538 case OBJC_CATEGORIES_MAP: 3539 if (F.LocalNumObjCCategoriesInMap != 0) { 3540 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3541 return Failure; 3542 } 3543 3544 F.LocalNumObjCCategoriesInMap = Record[0]; 3545 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3546 break; 3547 3548 case OBJC_CATEGORIES: 3549 F.ObjCCategories.swap(Record); 3550 break; 3551 3552 case CUDA_SPECIAL_DECL_REFS: 3553 // Later tables overwrite earlier ones. 3554 // FIXME: Modules will have trouble with this. 3555 CUDASpecialDeclRefs.clear(); 3556 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3557 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3558 break; 3559 3560 case HEADER_SEARCH_TABLE: 3561 F.HeaderFileInfoTableData = Blob.data(); 3562 F.LocalNumHeaderFileInfos = Record[1]; 3563 if (Record[0]) { 3564 F.HeaderFileInfoTable 3565 = HeaderFileInfoLookupTable::Create( 3566 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3567 (const unsigned char *)F.HeaderFileInfoTableData, 3568 HeaderFileInfoTrait(*this, F, 3569 &PP.getHeaderSearchInfo(), 3570 Blob.data() + Record[2])); 3571 3572 PP.getHeaderSearchInfo().SetExternalSource(this); 3573 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3574 PP.getHeaderSearchInfo().SetExternalLookup(this); 3575 } 3576 break; 3577 3578 case FP_PRAGMA_OPTIONS: 3579 // Later tables overwrite earlier ones. 3580 FPPragmaOptions.swap(Record); 3581 break; 3582 3583 case OPENCL_EXTENSIONS: 3584 for (unsigned I = 0, E = Record.size(); I != E; ) { 3585 auto Name = ReadString(Record, I); 3586 auto &Opt = OpenCLExtensions.OptMap[Name]; 3587 Opt.Supported = Record[I++] != 0; 3588 Opt.Enabled = Record[I++] != 0; 3589 Opt.Avail = Record[I++]; 3590 Opt.Core = Record[I++]; 3591 } 3592 break; 3593 3594 case OPENCL_EXTENSION_TYPES: 3595 for (unsigned I = 0, E = Record.size(); I != E;) { 3596 auto TypeID = static_cast<::TypeID>(Record[I++]); 3597 auto *Type = GetType(TypeID).getTypePtr(); 3598 auto NumExt = static_cast<unsigned>(Record[I++]); 3599 for (unsigned II = 0; II != NumExt; ++II) { 3600 auto Ext = ReadString(Record, I); 3601 OpenCLTypeExtMap[Type].insert(Ext); 3602 } 3603 } 3604 break; 3605 3606 case OPENCL_EXTENSION_DECLS: 3607 for (unsigned I = 0, E = Record.size(); I != E;) { 3608 auto DeclID = static_cast<::DeclID>(Record[I++]); 3609 auto *Decl = GetDecl(DeclID); 3610 auto NumExt = static_cast<unsigned>(Record[I++]); 3611 for (unsigned II = 0; II != NumExt; ++II) { 3612 auto Ext = ReadString(Record, I); 3613 OpenCLDeclExtMap[Decl].insert(Ext); 3614 } 3615 } 3616 break; 3617 3618 case TENTATIVE_DEFINITIONS: 3619 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3620 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3621 break; 3622 3623 case KNOWN_NAMESPACES: 3624 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3625 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3626 break; 3627 3628 case UNDEFINED_BUT_USED: 3629 if (UndefinedButUsed.size() % 2 != 0) { 3630 Error("Invalid existing UndefinedButUsed"); 3631 return Failure; 3632 } 3633 3634 if (Record.size() % 2 != 0) { 3635 Error("invalid undefined-but-used record"); 3636 return Failure; 3637 } 3638 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3639 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3640 UndefinedButUsed.push_back( 3641 ReadSourceLocation(F, Record, I).getRawEncoding()); 3642 } 3643 break; 3644 3645 case DELETE_EXPRS_TO_ANALYZE: 3646 for (unsigned I = 0, N = Record.size(); I != N;) { 3647 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3648 const uint64_t Count = Record[I++]; 3649 DelayedDeleteExprs.push_back(Count); 3650 for (uint64_t C = 0; C < Count; ++C) { 3651 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3652 bool IsArrayForm = Record[I++] == 1; 3653 DelayedDeleteExprs.push_back(IsArrayForm); 3654 } 3655 } 3656 break; 3657 3658 case IMPORTED_MODULES: 3659 if (!F.isModule()) { 3660 // If we aren't loading a module (which has its own exports), make 3661 // all of the imported modules visible. 3662 // FIXME: Deal with macros-only imports. 3663 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3664 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3665 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3666 if (GlobalID) { 3667 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3668 if (DeserializationListener) 3669 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3670 } 3671 } 3672 } 3673 break; 3674 3675 case MACRO_OFFSET: { 3676 if (F.LocalNumMacros != 0) { 3677 Error("duplicate MACRO_OFFSET record in AST file"); 3678 return Failure; 3679 } 3680 F.MacroOffsets = (const uint32_t *)Blob.data(); 3681 F.LocalNumMacros = Record[0]; 3682 unsigned LocalBaseMacroID = Record[1]; 3683 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3684 F.BaseMacroID = getTotalNumMacros(); 3685 3686 if (F.LocalNumMacros > 0) { 3687 // Introduce the global -> local mapping for macros within this module. 3688 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3689 3690 // Introduce the local -> global mapping for macros within this module. 3691 F.MacroRemap.insertOrReplace( 3692 std::make_pair(LocalBaseMacroID, 3693 F.BaseMacroID - LocalBaseMacroID)); 3694 3695 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3696 } 3697 break; 3698 } 3699 3700 case LATE_PARSED_TEMPLATE: 3701 LateParsedTemplates.emplace_back( 3702 std::piecewise_construct, std::forward_as_tuple(&F), 3703 std::forward_as_tuple(Record.begin(), Record.end())); 3704 break; 3705 3706 case OPTIMIZE_PRAGMA_OPTIONS: 3707 if (Record.size() != 1) { 3708 Error("invalid pragma optimize record"); 3709 return Failure; 3710 } 3711 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3712 break; 3713 3714 case MSSTRUCT_PRAGMA_OPTIONS: 3715 if (Record.size() != 1) { 3716 Error("invalid pragma ms_struct record"); 3717 return Failure; 3718 } 3719 PragmaMSStructState = Record[0]; 3720 break; 3721 3722 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3723 if (Record.size() != 2) { 3724 Error("invalid pragma ms_struct record"); 3725 return Failure; 3726 } 3727 PragmaMSPointersToMembersState = Record[0]; 3728 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3729 break; 3730 3731 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3732 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3733 UnusedLocalTypedefNameCandidates.push_back( 3734 getGlobalDeclID(F, Record[I])); 3735 break; 3736 3737 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3738 if (Record.size() != 1) { 3739 Error("invalid cuda pragma options record"); 3740 return Failure; 3741 } 3742 ForceCUDAHostDeviceDepth = Record[0]; 3743 break; 3744 3745 case PACK_PRAGMA_OPTIONS: { 3746 if (Record.size() < 3) { 3747 Error("invalid pragma pack record"); 3748 return Failure; 3749 } 3750 PragmaPackCurrentValue = Record[0]; 3751 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3752 unsigned NumStackEntries = Record[2]; 3753 unsigned Idx = 3; 3754 // Reset the stack when importing a new module. 3755 PragmaPackStack.clear(); 3756 for (unsigned I = 0; I < NumStackEntries; ++I) { 3757 PragmaPackStackEntry Entry; 3758 Entry.Value = Record[Idx++]; 3759 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3760 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3761 PragmaPackStrings.push_back(ReadString(Record, Idx)); 3762 Entry.SlotLabel = PragmaPackStrings.back(); 3763 PragmaPackStack.push_back(Entry); 3764 } 3765 break; 3766 } 3767 3768 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3769 if (Record.size() < 3) { 3770 Error("invalid pragma pack record"); 3771 return Failure; 3772 } 3773 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3774 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3775 unsigned NumStackEntries = Record[2]; 3776 unsigned Idx = 3; 3777 // Reset the stack when importing a new module. 3778 FpPragmaStack.clear(); 3779 for (unsigned I = 0; I < NumStackEntries; ++I) { 3780 FpPragmaStackEntry Entry; 3781 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3782 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3783 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3784 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3785 Entry.SlotLabel = FpPragmaStrings.back(); 3786 FpPragmaStack.push_back(Entry); 3787 } 3788 break; 3789 } 3790 3791 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3792 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3793 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I])); 3794 break; 3795 } 3796 } 3797 } 3798 3799 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3800 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3801 3802 // Additional remapping information. 3803 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3804 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3805 F.ModuleOffsetMap = StringRef(); 3806 3807 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3808 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3809 F.SLocRemap.insert(std::make_pair(0U, 0)); 3810 F.SLocRemap.insert(std::make_pair(2U, 1)); 3811 } 3812 3813 // Continuous range maps we may be updating in our module. 3814 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3815 RemapBuilder SLocRemap(F.SLocRemap); 3816 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3817 RemapBuilder MacroRemap(F.MacroRemap); 3818 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3819 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3820 RemapBuilder SelectorRemap(F.SelectorRemap); 3821 RemapBuilder DeclRemap(F.DeclRemap); 3822 RemapBuilder TypeRemap(F.TypeRemap); 3823 3824 while (Data < DataEnd) { 3825 // FIXME: Looking up dependency modules by filename is horrible. Let's 3826 // start fixing this with prebuilt, explicit and implicit modules and see 3827 // how it goes... 3828 using namespace llvm::support; 3829 ModuleKind Kind = static_cast<ModuleKind>( 3830 endian::readNext<uint8_t, little, unaligned>(Data)); 3831 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3832 StringRef Name = StringRef((const char*)Data, Len); 3833 Data += Len; 3834 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3835 Kind == MK_ImplicitModule 3836 ? ModuleMgr.lookupByModuleName(Name) 3837 : ModuleMgr.lookupByFileName(Name)); 3838 if (!OM) { 3839 std::string Msg = 3840 "SourceLocation remap refers to unknown module, cannot find "; 3841 Msg.append(std::string(Name)); 3842 Error(Msg); 3843 return; 3844 } 3845 3846 uint32_t SLocOffset = 3847 endian::readNext<uint32_t, little, unaligned>(Data); 3848 uint32_t IdentifierIDOffset = 3849 endian::readNext<uint32_t, little, unaligned>(Data); 3850 uint32_t MacroIDOffset = 3851 endian::readNext<uint32_t, little, unaligned>(Data); 3852 uint32_t PreprocessedEntityIDOffset = 3853 endian::readNext<uint32_t, little, unaligned>(Data); 3854 uint32_t SubmoduleIDOffset = 3855 endian::readNext<uint32_t, little, unaligned>(Data); 3856 uint32_t SelectorIDOffset = 3857 endian::readNext<uint32_t, little, unaligned>(Data); 3858 uint32_t DeclIDOffset = 3859 endian::readNext<uint32_t, little, unaligned>(Data); 3860 uint32_t TypeIndexOffset = 3861 endian::readNext<uint32_t, little, unaligned>(Data); 3862 3863 uint32_t None = std::numeric_limits<uint32_t>::max(); 3864 3865 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3866 RemapBuilder &Remap) { 3867 if (Offset != None) 3868 Remap.insert(std::make_pair(Offset, 3869 static_cast<int>(BaseOffset - Offset))); 3870 }; 3871 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3872 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3873 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3874 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3875 PreprocessedEntityRemap); 3876 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3877 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3878 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3879 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3880 3881 // Global -> local mappings. 3882 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3883 } 3884 } 3885 3886 ASTReader::ASTReadResult 3887 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3888 const ModuleFile *ImportedBy, 3889 unsigned ClientLoadCapabilities) { 3890 unsigned Idx = 0; 3891 F.ModuleMapPath = ReadPath(F, Record, Idx); 3892 3893 // Try to resolve ModuleName in the current header search context and 3894 // verify that it is found in the same module map file as we saved. If the 3895 // top-level AST file is a main file, skip this check because there is no 3896 // usable header search context. 3897 assert(!F.ModuleName.empty() && 3898 "MODULE_NAME should come before MODULE_MAP_FILE"); 3899 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3900 // An implicitly-loaded module file should have its module listed in some 3901 // module map file that we've already loaded. 3902 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3903 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3904 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3905 // Don't emit module relocation error if we have -fno-validate-pch 3906 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) { 3907 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3908 if (auto ASTFE = M ? M->getASTFile() : None) { 3909 // This module was defined by an imported (explicit) module. 3910 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3911 << ASTFE->getName(); 3912 } else { 3913 // This module was built with a different module map. 3914 Diag(diag::err_imported_module_not_found) 3915 << F.ModuleName << F.FileName 3916 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3917 << !ImportedBy; 3918 // In case it was imported by a PCH, there's a chance the user is 3919 // just missing to include the search path to the directory containing 3920 // the modulemap. 3921 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3922 Diag(diag::note_imported_by_pch_module_not_found) 3923 << llvm::sys::path::parent_path(F.ModuleMapPath); 3924 } 3925 } 3926 return OutOfDate; 3927 } 3928 3929 assert(M && M->Name == F.ModuleName && "found module with different name"); 3930 3931 // Check the primary module map file. 3932 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3933 if (!StoredModMap || *StoredModMap != ModMap) { 3934 assert(ModMap && "found module is missing module map file"); 3935 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3936 "top-level import should be verified"); 3937 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3938 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3939 Diag(diag::err_imported_module_modmap_changed) 3940 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3941 << ModMap->getName() << F.ModuleMapPath << NotImported; 3942 return OutOfDate; 3943 } 3944 3945 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3946 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3947 // FIXME: we should use input files rather than storing names. 3948 std::string Filename = ReadPath(F, Record, Idx); 3949 auto F = FileMgr.getFile(Filename, false, false); 3950 if (!F) { 3951 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3952 Error("could not find file '" + Filename +"' referenced by AST file"); 3953 return OutOfDate; 3954 } 3955 AdditionalStoredMaps.insert(*F); 3956 } 3957 3958 // Check any additional module map files (e.g. module.private.modulemap) 3959 // that are not in the pcm. 3960 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3961 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3962 // Remove files that match 3963 // Note: SmallPtrSet::erase is really remove 3964 if (!AdditionalStoredMaps.erase(ModMap)) { 3965 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3966 Diag(diag::err_module_different_modmap) 3967 << F.ModuleName << /*new*/0 << ModMap->getName(); 3968 return OutOfDate; 3969 } 3970 } 3971 } 3972 3973 // Check any additional module map files that are in the pcm, but not 3974 // found in header search. Cases that match are already removed. 3975 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3976 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3977 Diag(diag::err_module_different_modmap) 3978 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3979 return OutOfDate; 3980 } 3981 } 3982 3983 if (Listener) 3984 Listener->ReadModuleMapFile(F.ModuleMapPath); 3985 return Success; 3986 } 3987 3988 /// Move the given method to the back of the global list of methods. 3989 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 3990 // Find the entry for this selector in the method pool. 3991 Sema::GlobalMethodPool::iterator Known 3992 = S.MethodPool.find(Method->getSelector()); 3993 if (Known == S.MethodPool.end()) 3994 return; 3995 3996 // Retrieve the appropriate method list. 3997 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 3998 : Known->second.second; 3999 bool Found = false; 4000 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4001 if (!Found) { 4002 if (List->getMethod() == Method) { 4003 Found = true; 4004 } else { 4005 // Keep searching. 4006 continue; 4007 } 4008 } 4009 4010 if (List->getNext()) 4011 List->setMethod(List->getNext()->getMethod()); 4012 else 4013 List->setMethod(Method); 4014 } 4015 } 4016 4017 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4018 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4019 for (Decl *D : Names) { 4020 bool wasHidden = !D->isUnconditionallyVisible(); 4021 D->setVisibleDespiteOwningModule(); 4022 4023 if (wasHidden && SemaObj) { 4024 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4025 moveMethodToBackOfGlobalList(*SemaObj, Method); 4026 } 4027 } 4028 } 4029 } 4030 4031 void ASTReader::makeModuleVisible(Module *Mod, 4032 Module::NameVisibilityKind NameVisibility, 4033 SourceLocation ImportLoc) { 4034 llvm::SmallPtrSet<Module *, 4> Visited; 4035 SmallVector<Module *, 4> Stack; 4036 Stack.push_back(Mod); 4037 while (!Stack.empty()) { 4038 Mod = Stack.pop_back_val(); 4039 4040 if (NameVisibility <= Mod->NameVisibility) { 4041 // This module already has this level of visibility (or greater), so 4042 // there is nothing more to do. 4043 continue; 4044 } 4045 4046 if (Mod->isUnimportable()) { 4047 // Modules that aren't importable cannot be made visible. 4048 continue; 4049 } 4050 4051 // Update the module's name visibility. 4052 Mod->NameVisibility = NameVisibility; 4053 4054 // If we've already deserialized any names from this module, 4055 // mark them as visible. 4056 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4057 if (Hidden != HiddenNamesMap.end()) { 4058 auto HiddenNames = std::move(*Hidden); 4059 HiddenNamesMap.erase(Hidden); 4060 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4061 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4062 "making names visible added hidden names"); 4063 } 4064 4065 // Push any exported modules onto the stack to be marked as visible. 4066 SmallVector<Module *, 16> Exports; 4067 Mod->getExportedModules(Exports); 4068 for (SmallVectorImpl<Module *>::iterator 4069 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4070 Module *Exported = *I; 4071 if (Visited.insert(Exported).second) 4072 Stack.push_back(Exported); 4073 } 4074 } 4075 } 4076 4077 /// We've merged the definition \p MergedDef into the existing definition 4078 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4079 /// visible. 4080 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4081 NamedDecl *MergedDef) { 4082 if (!Def->isUnconditionallyVisible()) { 4083 // If MergedDef is visible or becomes visible, make the definition visible. 4084 if (MergedDef->isUnconditionallyVisible()) 4085 Def->setVisibleDespiteOwningModule(); 4086 else { 4087 getContext().mergeDefinitionIntoModule( 4088 Def, MergedDef->getImportedOwningModule(), 4089 /*NotifyListeners*/ false); 4090 PendingMergedDefinitionsToDeduplicate.insert(Def); 4091 } 4092 } 4093 } 4094 4095 bool ASTReader::loadGlobalIndex() { 4096 if (GlobalIndex) 4097 return false; 4098 4099 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4100 !PP.getLangOpts().Modules) 4101 return true; 4102 4103 // Try to load the global index. 4104 TriedLoadingGlobalIndex = true; 4105 StringRef ModuleCachePath 4106 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4107 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4108 GlobalModuleIndex::readIndex(ModuleCachePath); 4109 if (llvm::Error Err = std::move(Result.second)) { 4110 assert(!Result.first); 4111 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4112 return true; 4113 } 4114 4115 GlobalIndex.reset(Result.first); 4116 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4117 return false; 4118 } 4119 4120 bool ASTReader::isGlobalIndexUnavailable() const { 4121 return PP.getLangOpts().Modules && UseGlobalIndex && 4122 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4123 } 4124 4125 static void updateModuleTimestamp(ModuleFile &MF) { 4126 // Overwrite the timestamp file contents so that file's mtime changes. 4127 std::string TimestampFilename = MF.getTimestampFilename(); 4128 std::error_code EC; 4129 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text); 4130 if (EC) 4131 return; 4132 OS << "Timestamp file\n"; 4133 OS.close(); 4134 OS.clear_error(); // Avoid triggering a fatal error. 4135 } 4136 4137 /// Given a cursor at the start of an AST file, scan ahead and drop the 4138 /// cursor into the start of the given block ID, returning false on success and 4139 /// true on failure. 4140 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4141 while (true) { 4142 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4143 if (!MaybeEntry) { 4144 // FIXME this drops errors on the floor. 4145 consumeError(MaybeEntry.takeError()); 4146 return true; 4147 } 4148 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4149 4150 switch (Entry.Kind) { 4151 case llvm::BitstreamEntry::Error: 4152 case llvm::BitstreamEntry::EndBlock: 4153 return true; 4154 4155 case llvm::BitstreamEntry::Record: 4156 // Ignore top-level records. 4157 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4158 break; 4159 else { 4160 // FIXME this drops errors on the floor. 4161 consumeError(Skipped.takeError()); 4162 return true; 4163 } 4164 4165 case llvm::BitstreamEntry::SubBlock: 4166 if (Entry.ID == BlockID) { 4167 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4168 // FIXME this drops the error on the floor. 4169 consumeError(std::move(Err)); 4170 return true; 4171 } 4172 // Found it! 4173 return false; 4174 } 4175 4176 if (llvm::Error Err = Cursor.SkipBlock()) { 4177 // FIXME this drops the error on the floor. 4178 consumeError(std::move(Err)); 4179 return true; 4180 } 4181 } 4182 } 4183 } 4184 4185 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4186 ModuleKind Type, 4187 SourceLocation ImportLoc, 4188 unsigned ClientLoadCapabilities, 4189 SmallVectorImpl<ImportedSubmodule> *Imported) { 4190 llvm::SaveAndRestore<SourceLocation> 4191 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4192 4193 // Defer any pending actions until we get to the end of reading the AST file. 4194 Deserializing AnASTFile(this); 4195 4196 // Bump the generation number. 4197 unsigned PreviousGeneration = 0; 4198 if (ContextObj) 4199 PreviousGeneration = incrementGeneration(*ContextObj); 4200 4201 unsigned NumModules = ModuleMgr.size(); 4202 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) { 4203 assert(ReadResult && "expected to return error"); 4204 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4205 PP.getLangOpts().Modules 4206 ? &PP.getHeaderSearchInfo().getModuleMap() 4207 : nullptr); 4208 4209 // If we find that any modules are unusable, the global index is going 4210 // to be out-of-date. Just remove it. 4211 GlobalIndex.reset(); 4212 ModuleMgr.setGlobalIndex(nullptr); 4213 return ReadResult; 4214 }; 4215 4216 SmallVector<ImportedModule, 4> Loaded; 4217 switch (ASTReadResult ReadResult = 4218 ReadASTCore(FileName, Type, ImportLoc, 4219 /*ImportedBy=*/nullptr, Loaded, 0, 0, 4220 ASTFileSignature(), ClientLoadCapabilities)) { 4221 case Failure: 4222 case Missing: 4223 case OutOfDate: 4224 case VersionMismatch: 4225 case ConfigurationMismatch: 4226 case HadErrors: 4227 return removeModulesAndReturn(ReadResult); 4228 case Success: 4229 break; 4230 } 4231 4232 // Here comes stuff that we only do once the entire chain is loaded. 4233 4234 // Load the AST blocks of all of the modules that we loaded. We can still 4235 // hit errors parsing the ASTs at this point. 4236 for (ImportedModule &M : Loaded) { 4237 ModuleFile &F = *M.Mod; 4238 4239 // Read the AST block. 4240 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 4241 return removeModulesAndReturn(Result); 4242 4243 // The AST block should always have a definition for the main module. 4244 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4245 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4246 return removeModulesAndReturn(Failure); 4247 } 4248 4249 // Read the extension blocks. 4250 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4251 if (ASTReadResult Result = ReadExtensionBlock(F)) 4252 return removeModulesAndReturn(Result); 4253 } 4254 4255 // Once read, set the ModuleFile bit base offset and update the size in 4256 // bits of all files we've seen. 4257 F.GlobalBitOffset = TotalModulesSizeInBits; 4258 TotalModulesSizeInBits += F.SizeInBits; 4259 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4260 } 4261 4262 // Preload source locations and interesting indentifiers. 4263 for (ImportedModule &M : Loaded) { 4264 ModuleFile &F = *M.Mod; 4265 4266 // Preload SLocEntries. 4267 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4268 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4269 // Load it through the SourceManager and don't call ReadSLocEntry() 4270 // directly because the entry may have already been loaded in which case 4271 // calling ReadSLocEntry() directly would trigger an assertion in 4272 // SourceManager. 4273 SourceMgr.getLoadedSLocEntryByID(Index); 4274 } 4275 4276 // Map the original source file ID into the ID space of the current 4277 // compilation. 4278 if (F.OriginalSourceFileID.isValid()) { 4279 F.OriginalSourceFileID = FileID::get( 4280 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4281 } 4282 4283 // Preload all the pending interesting identifiers by marking them out of 4284 // date. 4285 for (auto Offset : F.PreloadIdentifierOffsets) { 4286 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 4287 F.IdentifierTableData + Offset); 4288 4289 ASTIdentifierLookupTrait Trait(*this, F); 4290 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4291 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4292 auto &II = PP.getIdentifierTable().getOwn(Key); 4293 II.setOutOfDate(true); 4294 4295 // Mark this identifier as being from an AST file so that we can track 4296 // whether we need to serialize it. 4297 markIdentifierFromAST(*this, II); 4298 4299 // Associate the ID with the identifier so that the writer can reuse it. 4300 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4301 SetIdentifierInfo(ID, &II); 4302 } 4303 } 4304 4305 // Setup the import locations and notify the module manager that we've 4306 // committed to these module files. 4307 for (ImportedModule &M : Loaded) { 4308 ModuleFile &F = *M.Mod; 4309 4310 ModuleMgr.moduleFileAccepted(&F); 4311 4312 // Set the import location. 4313 F.DirectImportLoc = ImportLoc; 4314 // FIXME: We assume that locations from PCH / preamble do not need 4315 // any translation. 4316 if (!M.ImportedBy) 4317 F.ImportLoc = M.ImportLoc; 4318 else 4319 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4320 } 4321 4322 if (!PP.getLangOpts().CPlusPlus || 4323 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4324 Type != MK_PrebuiltModule)) { 4325 // Mark all of the identifiers in the identifier table as being out of date, 4326 // so that various accessors know to check the loaded modules when the 4327 // identifier is used. 4328 // 4329 // For C++ modules, we don't need information on many identifiers (just 4330 // those that provide macros or are poisoned), so we mark all of 4331 // the interesting ones via PreloadIdentifierOffsets. 4332 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4333 IdEnd = PP.getIdentifierTable().end(); 4334 Id != IdEnd; ++Id) 4335 Id->second->setOutOfDate(true); 4336 } 4337 // Mark selectors as out of date. 4338 for (auto Sel : SelectorGeneration) 4339 SelectorOutOfDate[Sel.first] = true; 4340 4341 // Resolve any unresolved module exports. 4342 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4343 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4344 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4345 Module *ResolvedMod = getSubmodule(GlobalID); 4346 4347 switch (Unresolved.Kind) { 4348 case UnresolvedModuleRef::Conflict: 4349 if (ResolvedMod) { 4350 Module::Conflict Conflict; 4351 Conflict.Other = ResolvedMod; 4352 Conflict.Message = Unresolved.String.str(); 4353 Unresolved.Mod->Conflicts.push_back(Conflict); 4354 } 4355 continue; 4356 4357 case UnresolvedModuleRef::Import: 4358 if (ResolvedMod) 4359 Unresolved.Mod->Imports.insert(ResolvedMod); 4360 continue; 4361 4362 case UnresolvedModuleRef::Export: 4363 if (ResolvedMod || Unresolved.IsWildcard) 4364 Unresolved.Mod->Exports.push_back( 4365 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4366 continue; 4367 } 4368 } 4369 UnresolvedModuleRefs.clear(); 4370 4371 if (Imported) 4372 Imported->append(ImportedModules.begin(), 4373 ImportedModules.end()); 4374 4375 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4376 // Might be unnecessary as use declarations are only used to build the 4377 // module itself. 4378 4379 if (ContextObj) 4380 InitializeContext(); 4381 4382 if (SemaObj) 4383 UpdateSema(); 4384 4385 if (DeserializationListener) 4386 DeserializationListener->ReaderInitialized(this); 4387 4388 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4389 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4390 // If this AST file is a precompiled preamble, then set the 4391 // preamble file ID of the source manager to the file source file 4392 // from which the preamble was built. 4393 if (Type == MK_Preamble) { 4394 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4395 } else if (Type == MK_MainFile) { 4396 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4397 } 4398 } 4399 4400 // For any Objective-C class definitions we have already loaded, make sure 4401 // that we load any additional categories. 4402 if (ContextObj) { 4403 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4404 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4405 ObjCClassesLoaded[I], 4406 PreviousGeneration); 4407 } 4408 } 4409 4410 if (PP.getHeaderSearchInfo() 4411 .getHeaderSearchOpts() 4412 .ModulesValidateOncePerBuildSession) { 4413 // Now we are certain that the module and all modules it depends on are 4414 // up to date. Create or update timestamp files for modules that are 4415 // located in the module cache (not for PCH files that could be anywhere 4416 // in the filesystem). 4417 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4418 ImportedModule &M = Loaded[I]; 4419 if (M.Mod->Kind == MK_ImplicitModule) { 4420 updateModuleTimestamp(*M.Mod); 4421 } 4422 } 4423 } 4424 4425 return Success; 4426 } 4427 4428 static ASTFileSignature readASTFileSignature(StringRef PCH); 4429 4430 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4431 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4432 // FIXME checking magic headers is done in other places such as 4433 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4434 // always done the same. Unify it all with a helper. 4435 if (!Stream.canSkipToPos(4)) 4436 return llvm::createStringError(std::errc::illegal_byte_sequence, 4437 "file too small to contain AST file magic"); 4438 for (unsigned C : {'C', 'P', 'C', 'H'}) 4439 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4440 if (Res.get() != C) 4441 return llvm::createStringError( 4442 std::errc::illegal_byte_sequence, 4443 "file doesn't start with AST file magic"); 4444 } else 4445 return Res.takeError(); 4446 return llvm::Error::success(); 4447 } 4448 4449 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4450 switch (Kind) { 4451 case MK_PCH: 4452 return 0; // PCH 4453 case MK_ImplicitModule: 4454 case MK_ExplicitModule: 4455 case MK_PrebuiltModule: 4456 return 1; // module 4457 case MK_MainFile: 4458 case MK_Preamble: 4459 return 2; // main source file 4460 } 4461 llvm_unreachable("unknown module kind"); 4462 } 4463 4464 ASTReader::ASTReadResult 4465 ASTReader::ReadASTCore(StringRef FileName, 4466 ModuleKind Type, 4467 SourceLocation ImportLoc, 4468 ModuleFile *ImportedBy, 4469 SmallVectorImpl<ImportedModule> &Loaded, 4470 off_t ExpectedSize, time_t ExpectedModTime, 4471 ASTFileSignature ExpectedSignature, 4472 unsigned ClientLoadCapabilities) { 4473 ModuleFile *M; 4474 std::string ErrorStr; 4475 ModuleManager::AddModuleResult AddResult 4476 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4477 getGeneration(), ExpectedSize, ExpectedModTime, 4478 ExpectedSignature, readASTFileSignature, 4479 M, ErrorStr); 4480 4481 switch (AddResult) { 4482 case ModuleManager::AlreadyLoaded: 4483 Diag(diag::remark_module_import) 4484 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4485 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4486 return Success; 4487 4488 case ModuleManager::NewlyLoaded: 4489 // Load module file below. 4490 break; 4491 4492 case ModuleManager::Missing: 4493 // The module file was missing; if the client can handle that, return 4494 // it. 4495 if (ClientLoadCapabilities & ARR_Missing) 4496 return Missing; 4497 4498 // Otherwise, return an error. 4499 Diag(diag::err_ast_file_not_found) 4500 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4501 << ErrorStr; 4502 return Failure; 4503 4504 case ModuleManager::OutOfDate: 4505 // We couldn't load the module file because it is out-of-date. If the 4506 // client can handle out-of-date, return it. 4507 if (ClientLoadCapabilities & ARR_OutOfDate) 4508 return OutOfDate; 4509 4510 // Otherwise, return an error. 4511 Diag(diag::err_ast_file_out_of_date) 4512 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4513 << ErrorStr; 4514 return Failure; 4515 } 4516 4517 assert(M && "Missing module file"); 4518 4519 bool ShouldFinalizePCM = false; 4520 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4521 auto &MC = getModuleManager().getModuleCache(); 4522 if (ShouldFinalizePCM) 4523 MC.finalizePCM(FileName); 4524 else 4525 MC.tryToDropPCM(FileName); 4526 }); 4527 ModuleFile &F = *M; 4528 BitstreamCursor &Stream = F.Stream; 4529 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4530 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4531 4532 // Sniff for the signature. 4533 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4534 Diag(diag::err_ast_file_invalid) 4535 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4536 return Failure; 4537 } 4538 4539 // This is used for compatibility with older PCH formats. 4540 bool HaveReadControlBlock = false; 4541 while (true) { 4542 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4543 if (!MaybeEntry) { 4544 Error(MaybeEntry.takeError()); 4545 return Failure; 4546 } 4547 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4548 4549 switch (Entry.Kind) { 4550 case llvm::BitstreamEntry::Error: 4551 case llvm::BitstreamEntry::Record: 4552 case llvm::BitstreamEntry::EndBlock: 4553 Error("invalid record at top-level of AST file"); 4554 return Failure; 4555 4556 case llvm::BitstreamEntry::SubBlock: 4557 break; 4558 } 4559 4560 switch (Entry.ID) { 4561 case CONTROL_BLOCK_ID: 4562 HaveReadControlBlock = true; 4563 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4564 case Success: 4565 // Check that we didn't try to load a non-module AST file as a module. 4566 // 4567 // FIXME: Should we also perform the converse check? Loading a module as 4568 // a PCH file sort of works, but it's a bit wonky. 4569 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4570 Type == MK_PrebuiltModule) && 4571 F.ModuleName.empty()) { 4572 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4573 if (Result != OutOfDate || 4574 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4575 Diag(diag::err_module_file_not_module) << FileName; 4576 return Result; 4577 } 4578 break; 4579 4580 case Failure: return Failure; 4581 case Missing: return Missing; 4582 case OutOfDate: return OutOfDate; 4583 case VersionMismatch: return VersionMismatch; 4584 case ConfigurationMismatch: return ConfigurationMismatch; 4585 case HadErrors: return HadErrors; 4586 } 4587 break; 4588 4589 case AST_BLOCK_ID: 4590 if (!HaveReadControlBlock) { 4591 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4592 Diag(diag::err_pch_version_too_old); 4593 return VersionMismatch; 4594 } 4595 4596 // Record that we've loaded this module. 4597 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4598 ShouldFinalizePCM = true; 4599 return Success; 4600 4601 case UNHASHED_CONTROL_BLOCK_ID: 4602 // This block is handled using look-ahead during ReadControlBlock. We 4603 // shouldn't get here! 4604 Error("malformed block record in AST file"); 4605 return Failure; 4606 4607 default: 4608 if (llvm::Error Err = Stream.SkipBlock()) { 4609 Error(std::move(Err)); 4610 return Failure; 4611 } 4612 break; 4613 } 4614 } 4615 4616 llvm_unreachable("unexpected break; expected return"); 4617 } 4618 4619 ASTReader::ASTReadResult 4620 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4621 unsigned ClientLoadCapabilities) { 4622 const HeaderSearchOptions &HSOpts = 4623 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4624 bool AllowCompatibleConfigurationMismatch = 4625 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4626 4627 ASTReadResult Result = readUnhashedControlBlockImpl( 4628 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4629 Listener.get(), 4630 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4631 4632 // If F was directly imported by another module, it's implicitly validated by 4633 // the importing module. 4634 if (DisableValidation || WasImportedBy || 4635 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4636 return Success; 4637 4638 if (Result == Failure) { 4639 Error("malformed block record in AST file"); 4640 return Failure; 4641 } 4642 4643 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4644 // If this module has already been finalized in the ModuleCache, we're stuck 4645 // with it; we can only load a single version of each module. 4646 // 4647 // This can happen when a module is imported in two contexts: in one, as a 4648 // user module; in another, as a system module (due to an import from 4649 // another module marked with the [system] flag). It usually indicates a 4650 // bug in the module map: this module should also be marked with [system]. 4651 // 4652 // If -Wno-system-headers (the default), and the first import is as a 4653 // system module, then validation will fail during the as-user import, 4654 // since -Werror flags won't have been validated. However, it's reasonable 4655 // to treat this consistently as a system module. 4656 // 4657 // If -Wsystem-headers, the PCM on disk was built with 4658 // -Wno-system-headers, and the first import is as a user module, then 4659 // validation will fail during the as-system import since the PCM on disk 4660 // doesn't guarantee that -Werror was respected. However, the -Werror 4661 // flags were checked during the initial as-user import. 4662 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4663 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4664 return Success; 4665 } 4666 } 4667 4668 return Result; 4669 } 4670 4671 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4672 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4673 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4674 bool ValidateDiagnosticOptions) { 4675 // Initialize a stream. 4676 BitstreamCursor Stream(StreamData); 4677 4678 // Sniff for the signature. 4679 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4680 // FIXME this drops the error on the floor. 4681 consumeError(std::move(Err)); 4682 return Failure; 4683 } 4684 4685 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4686 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4687 return Failure; 4688 4689 // Read all of the records in the options block. 4690 RecordData Record; 4691 ASTReadResult Result = Success; 4692 while (true) { 4693 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4694 if (!MaybeEntry) { 4695 // FIXME this drops the error on the floor. 4696 consumeError(MaybeEntry.takeError()); 4697 return Failure; 4698 } 4699 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4700 4701 switch (Entry.Kind) { 4702 case llvm::BitstreamEntry::Error: 4703 case llvm::BitstreamEntry::SubBlock: 4704 return Failure; 4705 4706 case llvm::BitstreamEntry::EndBlock: 4707 return Result; 4708 4709 case llvm::BitstreamEntry::Record: 4710 // The interesting case. 4711 break; 4712 } 4713 4714 // Read and process a record. 4715 Record.clear(); 4716 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4717 if (!MaybeRecordType) { 4718 // FIXME this drops the error. 4719 return Failure; 4720 } 4721 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4722 case SIGNATURE: 4723 if (F) 4724 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4725 break; 4726 case AST_BLOCK_HASH: 4727 if (F) 4728 F->ASTBlockHash = 4729 ASTFileSignature::create(Record.begin(), Record.end()); 4730 break; 4731 case DIAGNOSTIC_OPTIONS: { 4732 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4733 if (Listener && ValidateDiagnosticOptions && 4734 !AllowCompatibleConfigurationMismatch && 4735 ParseDiagnosticOptions(Record, Complain, *Listener)) 4736 Result = OutOfDate; // Don't return early. Read the signature. 4737 break; 4738 } 4739 case DIAG_PRAGMA_MAPPINGS: 4740 if (!F) 4741 break; 4742 if (F->PragmaDiagMappings.empty()) 4743 F->PragmaDiagMappings.swap(Record); 4744 else 4745 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4746 Record.begin(), Record.end()); 4747 break; 4748 } 4749 } 4750 } 4751 4752 /// Parse a record and blob containing module file extension metadata. 4753 static bool parseModuleFileExtensionMetadata( 4754 const SmallVectorImpl<uint64_t> &Record, 4755 StringRef Blob, 4756 ModuleFileExtensionMetadata &Metadata) { 4757 if (Record.size() < 4) return true; 4758 4759 Metadata.MajorVersion = Record[0]; 4760 Metadata.MinorVersion = Record[1]; 4761 4762 unsigned BlockNameLen = Record[2]; 4763 unsigned UserInfoLen = Record[3]; 4764 4765 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4766 4767 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4768 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4769 Blob.data() + BlockNameLen + UserInfoLen); 4770 return false; 4771 } 4772 4773 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4774 BitstreamCursor &Stream = F.Stream; 4775 4776 RecordData Record; 4777 while (true) { 4778 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4779 if (!MaybeEntry) { 4780 Error(MaybeEntry.takeError()); 4781 return Failure; 4782 } 4783 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4784 4785 switch (Entry.Kind) { 4786 case llvm::BitstreamEntry::SubBlock: 4787 if (llvm::Error Err = Stream.SkipBlock()) { 4788 Error(std::move(Err)); 4789 return Failure; 4790 } 4791 continue; 4792 4793 case llvm::BitstreamEntry::EndBlock: 4794 return Success; 4795 4796 case llvm::BitstreamEntry::Error: 4797 return HadErrors; 4798 4799 case llvm::BitstreamEntry::Record: 4800 break; 4801 } 4802 4803 Record.clear(); 4804 StringRef Blob; 4805 Expected<unsigned> MaybeRecCode = 4806 Stream.readRecord(Entry.ID, Record, &Blob); 4807 if (!MaybeRecCode) { 4808 Error(MaybeRecCode.takeError()); 4809 return Failure; 4810 } 4811 switch (MaybeRecCode.get()) { 4812 case EXTENSION_METADATA: { 4813 ModuleFileExtensionMetadata Metadata; 4814 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) { 4815 Error("malformed EXTENSION_METADATA in AST file"); 4816 return Failure; 4817 } 4818 4819 // Find a module file extension with this block name. 4820 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4821 if (Known == ModuleFileExtensions.end()) break; 4822 4823 // Form a reader. 4824 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4825 F, Stream)) { 4826 F.ExtensionReaders.push_back(std::move(Reader)); 4827 } 4828 4829 break; 4830 } 4831 } 4832 } 4833 4834 return Success; 4835 } 4836 4837 void ASTReader::InitializeContext() { 4838 assert(ContextObj && "no context to initialize"); 4839 ASTContext &Context = *ContextObj; 4840 4841 // If there's a listener, notify them that we "read" the translation unit. 4842 if (DeserializationListener) 4843 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4844 Context.getTranslationUnitDecl()); 4845 4846 // FIXME: Find a better way to deal with collisions between these 4847 // built-in types. Right now, we just ignore the problem. 4848 4849 // Load the special types. 4850 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4851 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4852 if (!Context.CFConstantStringTypeDecl) 4853 Context.setCFConstantStringType(GetType(String)); 4854 } 4855 4856 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4857 QualType FileType = GetType(File); 4858 if (FileType.isNull()) { 4859 Error("FILE type is NULL"); 4860 return; 4861 } 4862 4863 if (!Context.FILEDecl) { 4864 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4865 Context.setFILEDecl(Typedef->getDecl()); 4866 else { 4867 const TagType *Tag = FileType->getAs<TagType>(); 4868 if (!Tag) { 4869 Error("Invalid FILE type in AST file"); 4870 return; 4871 } 4872 Context.setFILEDecl(Tag->getDecl()); 4873 } 4874 } 4875 } 4876 4877 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4878 QualType Jmp_bufType = GetType(Jmp_buf); 4879 if (Jmp_bufType.isNull()) { 4880 Error("jmp_buf type is NULL"); 4881 return; 4882 } 4883 4884 if (!Context.jmp_bufDecl) { 4885 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4886 Context.setjmp_bufDecl(Typedef->getDecl()); 4887 else { 4888 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4889 if (!Tag) { 4890 Error("Invalid jmp_buf type in AST file"); 4891 return; 4892 } 4893 Context.setjmp_bufDecl(Tag->getDecl()); 4894 } 4895 } 4896 } 4897 4898 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4899 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4900 if (Sigjmp_bufType.isNull()) { 4901 Error("sigjmp_buf type is NULL"); 4902 return; 4903 } 4904 4905 if (!Context.sigjmp_bufDecl) { 4906 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4907 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4908 else { 4909 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4910 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4911 Context.setsigjmp_bufDecl(Tag->getDecl()); 4912 } 4913 } 4914 } 4915 4916 if (unsigned ObjCIdRedef 4917 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4918 if (Context.ObjCIdRedefinitionType.isNull()) 4919 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4920 } 4921 4922 if (unsigned ObjCClassRedef 4923 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4924 if (Context.ObjCClassRedefinitionType.isNull()) 4925 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4926 } 4927 4928 if (unsigned ObjCSelRedef 4929 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4930 if (Context.ObjCSelRedefinitionType.isNull()) 4931 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4932 } 4933 4934 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4935 QualType Ucontext_tType = GetType(Ucontext_t); 4936 if (Ucontext_tType.isNull()) { 4937 Error("ucontext_t type is NULL"); 4938 return; 4939 } 4940 4941 if (!Context.ucontext_tDecl) { 4942 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4943 Context.setucontext_tDecl(Typedef->getDecl()); 4944 else { 4945 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4946 assert(Tag && "Invalid ucontext_t type in AST file"); 4947 Context.setucontext_tDecl(Tag->getDecl()); 4948 } 4949 } 4950 } 4951 } 4952 4953 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4954 4955 // If there were any CUDA special declarations, deserialize them. 4956 if (!CUDASpecialDeclRefs.empty()) { 4957 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4958 Context.setcudaConfigureCallDecl( 4959 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4960 } 4961 4962 // Re-export any modules that were imported by a non-module AST file. 4963 // FIXME: This does not make macro-only imports visible again. 4964 for (auto &Import : ImportedModules) { 4965 if (Module *Imported = getSubmodule(Import.ID)) { 4966 makeModuleVisible(Imported, Module::AllVisible, 4967 /*ImportLoc=*/Import.ImportLoc); 4968 if (Import.ImportLoc.isValid()) 4969 PP.makeModuleVisible(Imported, Import.ImportLoc); 4970 // This updates visibility for Preprocessor only. For Sema, which can be 4971 // nullptr here, we do the same later, in UpdateSema(). 4972 } 4973 } 4974 } 4975 4976 void ASTReader::finalizeForWriting() { 4977 // Nothing to do for now. 4978 } 4979 4980 /// Reads and return the signature record from \p PCH's control block, or 4981 /// else returns 0. 4982 static ASTFileSignature readASTFileSignature(StringRef PCH) { 4983 BitstreamCursor Stream(PCH); 4984 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4985 // FIXME this drops the error on the floor. 4986 consumeError(std::move(Err)); 4987 return ASTFileSignature(); 4988 } 4989 4990 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4991 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4992 return ASTFileSignature(); 4993 4994 // Scan for SIGNATURE inside the diagnostic options block. 4995 ASTReader::RecordData Record; 4996 while (true) { 4997 Expected<llvm::BitstreamEntry> MaybeEntry = 4998 Stream.advanceSkippingSubblocks(); 4999 if (!MaybeEntry) { 5000 // FIXME this drops the error on the floor. 5001 consumeError(MaybeEntry.takeError()); 5002 return ASTFileSignature(); 5003 } 5004 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5005 5006 if (Entry.Kind != llvm::BitstreamEntry::Record) 5007 return ASTFileSignature(); 5008 5009 Record.clear(); 5010 StringRef Blob; 5011 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5012 if (!MaybeRecord) { 5013 // FIXME this drops the error on the floor. 5014 consumeError(MaybeRecord.takeError()); 5015 return ASTFileSignature(); 5016 } 5017 if (SIGNATURE == MaybeRecord.get()) 5018 return ASTFileSignature::create(Record.begin(), 5019 Record.begin() + ASTFileSignature::size); 5020 } 5021 } 5022 5023 /// Retrieve the name of the original source file name 5024 /// directly from the AST file, without actually loading the AST 5025 /// file. 5026 std::string ASTReader::getOriginalSourceFile( 5027 const std::string &ASTFileName, FileManager &FileMgr, 5028 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5029 // Open the AST file. 5030 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5031 if (!Buffer) { 5032 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5033 << ASTFileName << Buffer.getError().message(); 5034 return std::string(); 5035 } 5036 5037 // Initialize the stream 5038 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5039 5040 // Sniff for the signature. 5041 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5042 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5043 return std::string(); 5044 } 5045 5046 // Scan for the CONTROL_BLOCK_ID block. 5047 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5048 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5049 return std::string(); 5050 } 5051 5052 // Scan for ORIGINAL_FILE inside the control block. 5053 RecordData Record; 5054 while (true) { 5055 Expected<llvm::BitstreamEntry> MaybeEntry = 5056 Stream.advanceSkippingSubblocks(); 5057 if (!MaybeEntry) { 5058 // FIXME this drops errors on the floor. 5059 consumeError(MaybeEntry.takeError()); 5060 return std::string(); 5061 } 5062 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5063 5064 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5065 return std::string(); 5066 5067 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5068 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5069 return std::string(); 5070 } 5071 5072 Record.clear(); 5073 StringRef Blob; 5074 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5075 if (!MaybeRecord) { 5076 // FIXME this drops the errors on the floor. 5077 consumeError(MaybeRecord.takeError()); 5078 return std::string(); 5079 } 5080 if (ORIGINAL_FILE == MaybeRecord.get()) 5081 return Blob.str(); 5082 } 5083 } 5084 5085 namespace { 5086 5087 class SimplePCHValidator : public ASTReaderListener { 5088 const LangOptions &ExistingLangOpts; 5089 const TargetOptions &ExistingTargetOpts; 5090 const PreprocessorOptions &ExistingPPOpts; 5091 std::string ExistingModuleCachePath; 5092 FileManager &FileMgr; 5093 5094 public: 5095 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5096 const TargetOptions &ExistingTargetOpts, 5097 const PreprocessorOptions &ExistingPPOpts, 5098 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5099 : ExistingLangOpts(ExistingLangOpts), 5100 ExistingTargetOpts(ExistingTargetOpts), 5101 ExistingPPOpts(ExistingPPOpts), 5102 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5103 5104 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5105 bool AllowCompatibleDifferences) override { 5106 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5107 AllowCompatibleDifferences); 5108 } 5109 5110 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5111 bool AllowCompatibleDifferences) override { 5112 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5113 AllowCompatibleDifferences); 5114 } 5115 5116 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5117 StringRef SpecificModuleCachePath, 5118 bool Complain) override { 5119 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5120 ExistingModuleCachePath, 5121 nullptr, ExistingLangOpts); 5122 } 5123 5124 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5125 bool Complain, 5126 std::string &SuggestedPredefines) override { 5127 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5128 SuggestedPredefines, ExistingLangOpts); 5129 } 5130 }; 5131 5132 } // namespace 5133 5134 bool ASTReader::readASTFileControlBlock( 5135 StringRef Filename, FileManager &FileMgr, 5136 const PCHContainerReader &PCHContainerRdr, 5137 bool FindModuleFileExtensions, 5138 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5139 // Open the AST file. 5140 // FIXME: This allows use of the VFS; we do not allow use of the 5141 // VFS when actually loading a module. 5142 auto Buffer = FileMgr.getBufferForFile(Filename); 5143 if (!Buffer) { 5144 return true; 5145 } 5146 5147 // Initialize the stream 5148 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5149 BitstreamCursor Stream(Bytes); 5150 5151 // Sniff for the signature. 5152 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5153 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5154 return true; 5155 } 5156 5157 // Scan for the CONTROL_BLOCK_ID block. 5158 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5159 return true; 5160 5161 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5162 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5163 bool NeedsImports = Listener.needsImportVisitation(); 5164 BitstreamCursor InputFilesCursor; 5165 5166 RecordData Record; 5167 std::string ModuleDir; 5168 bool DoneWithControlBlock = false; 5169 while (!DoneWithControlBlock) { 5170 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5171 if (!MaybeEntry) { 5172 // FIXME this drops the error on the floor. 5173 consumeError(MaybeEntry.takeError()); 5174 return true; 5175 } 5176 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5177 5178 switch (Entry.Kind) { 5179 case llvm::BitstreamEntry::SubBlock: { 5180 switch (Entry.ID) { 5181 case OPTIONS_BLOCK_ID: { 5182 std::string IgnoredSuggestedPredefines; 5183 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5184 /*AllowCompatibleConfigurationMismatch*/ false, 5185 Listener, IgnoredSuggestedPredefines) != Success) 5186 return true; 5187 break; 5188 } 5189 5190 case INPUT_FILES_BLOCK_ID: 5191 InputFilesCursor = Stream; 5192 if (llvm::Error Err = Stream.SkipBlock()) { 5193 // FIXME this drops the error on the floor. 5194 consumeError(std::move(Err)); 5195 return true; 5196 } 5197 if (NeedsInputFiles && 5198 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5199 return true; 5200 break; 5201 5202 default: 5203 if (llvm::Error Err = Stream.SkipBlock()) { 5204 // FIXME this drops the error on the floor. 5205 consumeError(std::move(Err)); 5206 return true; 5207 } 5208 break; 5209 } 5210 5211 continue; 5212 } 5213 5214 case llvm::BitstreamEntry::EndBlock: 5215 DoneWithControlBlock = true; 5216 break; 5217 5218 case llvm::BitstreamEntry::Error: 5219 return true; 5220 5221 case llvm::BitstreamEntry::Record: 5222 break; 5223 } 5224 5225 if (DoneWithControlBlock) break; 5226 5227 Record.clear(); 5228 StringRef Blob; 5229 Expected<unsigned> MaybeRecCode = 5230 Stream.readRecord(Entry.ID, Record, &Blob); 5231 if (!MaybeRecCode) { 5232 // FIXME this drops the error. 5233 return Failure; 5234 } 5235 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5236 case METADATA: 5237 if (Record[0] != VERSION_MAJOR) 5238 return true; 5239 if (Listener.ReadFullVersionInformation(Blob)) 5240 return true; 5241 break; 5242 case MODULE_NAME: 5243 Listener.ReadModuleName(Blob); 5244 break; 5245 case MODULE_DIRECTORY: 5246 ModuleDir = std::string(Blob); 5247 break; 5248 case MODULE_MAP_FILE: { 5249 unsigned Idx = 0; 5250 auto Path = ReadString(Record, Idx); 5251 ResolveImportedPath(Path, ModuleDir); 5252 Listener.ReadModuleMapFile(Path); 5253 break; 5254 } 5255 case INPUT_FILE_OFFSETS: { 5256 if (!NeedsInputFiles) 5257 break; 5258 5259 unsigned NumInputFiles = Record[0]; 5260 unsigned NumUserFiles = Record[1]; 5261 const llvm::support::unaligned_uint64_t *InputFileOffs = 5262 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5263 for (unsigned I = 0; I != NumInputFiles; ++I) { 5264 // Go find this input file. 5265 bool isSystemFile = I >= NumUserFiles; 5266 5267 if (isSystemFile && !NeedsSystemInputFiles) 5268 break; // the rest are system input files 5269 5270 BitstreamCursor &Cursor = InputFilesCursor; 5271 SavedStreamPosition SavedPosition(Cursor); 5272 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5273 // FIXME this drops errors on the floor. 5274 consumeError(std::move(Err)); 5275 } 5276 5277 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5278 if (!MaybeCode) { 5279 // FIXME this drops errors on the floor. 5280 consumeError(MaybeCode.takeError()); 5281 } 5282 unsigned Code = MaybeCode.get(); 5283 5284 RecordData Record; 5285 StringRef Blob; 5286 bool shouldContinue = false; 5287 Expected<unsigned> MaybeRecordType = 5288 Cursor.readRecord(Code, Record, &Blob); 5289 if (!MaybeRecordType) { 5290 // FIXME this drops errors on the floor. 5291 consumeError(MaybeRecordType.takeError()); 5292 } 5293 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5294 case INPUT_FILE_HASH: 5295 break; 5296 case INPUT_FILE: 5297 bool Overridden = static_cast<bool>(Record[3]); 5298 std::string Filename = std::string(Blob); 5299 ResolveImportedPath(Filename, ModuleDir); 5300 shouldContinue = Listener.visitInputFile( 5301 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5302 break; 5303 } 5304 if (!shouldContinue) 5305 break; 5306 } 5307 break; 5308 } 5309 5310 case IMPORTS: { 5311 if (!NeedsImports) 5312 break; 5313 5314 unsigned Idx = 0, N = Record.size(); 5315 while (Idx < N) { 5316 // Read information about the AST file. 5317 Idx += 5318 1 + 1 + 1 + 1 + 5319 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5320 std::string ModuleName = ReadString(Record, Idx); 5321 std::string Filename = ReadString(Record, Idx); 5322 ResolveImportedPath(Filename, ModuleDir); 5323 Listener.visitImport(ModuleName, Filename); 5324 } 5325 break; 5326 } 5327 5328 default: 5329 // No other validation to perform. 5330 break; 5331 } 5332 } 5333 5334 // Look for module file extension blocks, if requested. 5335 if (FindModuleFileExtensions) { 5336 BitstreamCursor SavedStream = Stream; 5337 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5338 bool DoneWithExtensionBlock = false; 5339 while (!DoneWithExtensionBlock) { 5340 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5341 if (!MaybeEntry) { 5342 // FIXME this drops the error. 5343 return true; 5344 } 5345 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5346 5347 switch (Entry.Kind) { 5348 case llvm::BitstreamEntry::SubBlock: 5349 if (llvm::Error Err = Stream.SkipBlock()) { 5350 // FIXME this drops the error on the floor. 5351 consumeError(std::move(Err)); 5352 return true; 5353 } 5354 continue; 5355 5356 case llvm::BitstreamEntry::EndBlock: 5357 DoneWithExtensionBlock = true; 5358 continue; 5359 5360 case llvm::BitstreamEntry::Error: 5361 return true; 5362 5363 case llvm::BitstreamEntry::Record: 5364 break; 5365 } 5366 5367 Record.clear(); 5368 StringRef Blob; 5369 Expected<unsigned> MaybeRecCode = 5370 Stream.readRecord(Entry.ID, Record, &Blob); 5371 if (!MaybeRecCode) { 5372 // FIXME this drops the error. 5373 return true; 5374 } 5375 switch (MaybeRecCode.get()) { 5376 case EXTENSION_METADATA: { 5377 ModuleFileExtensionMetadata Metadata; 5378 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5379 return true; 5380 5381 Listener.readModuleFileExtension(Metadata); 5382 break; 5383 } 5384 } 5385 } 5386 } 5387 Stream = SavedStream; 5388 } 5389 5390 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5391 if (readUnhashedControlBlockImpl( 5392 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5393 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5394 ValidateDiagnosticOptions) != Success) 5395 return true; 5396 5397 return false; 5398 } 5399 5400 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5401 const PCHContainerReader &PCHContainerRdr, 5402 const LangOptions &LangOpts, 5403 const TargetOptions &TargetOpts, 5404 const PreprocessorOptions &PPOpts, 5405 StringRef ExistingModuleCachePath) { 5406 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5407 ExistingModuleCachePath, FileMgr); 5408 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5409 /*FindModuleFileExtensions=*/false, 5410 validator, 5411 /*ValidateDiagnosticOptions=*/true); 5412 } 5413 5414 ASTReader::ASTReadResult 5415 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 5416 // Enter the submodule block. 5417 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 5418 Error(std::move(Err)); 5419 return Failure; 5420 } 5421 5422 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5423 bool First = true; 5424 Module *CurrentModule = nullptr; 5425 RecordData Record; 5426 while (true) { 5427 Expected<llvm::BitstreamEntry> MaybeEntry = 5428 F.Stream.advanceSkippingSubblocks(); 5429 if (!MaybeEntry) { 5430 Error(MaybeEntry.takeError()); 5431 return Failure; 5432 } 5433 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5434 5435 switch (Entry.Kind) { 5436 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5437 case llvm::BitstreamEntry::Error: 5438 Error("malformed block record in AST file"); 5439 return Failure; 5440 case llvm::BitstreamEntry::EndBlock: 5441 return Success; 5442 case llvm::BitstreamEntry::Record: 5443 // The interesting case. 5444 break; 5445 } 5446 5447 // Read a record. 5448 StringRef Blob; 5449 Record.clear(); 5450 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5451 if (!MaybeKind) { 5452 Error(MaybeKind.takeError()); 5453 return Failure; 5454 } 5455 unsigned Kind = MaybeKind.get(); 5456 5457 if ((Kind == SUBMODULE_METADATA) != First) { 5458 Error("submodule metadata record should be at beginning of block"); 5459 return Failure; 5460 } 5461 First = false; 5462 5463 // Submodule information is only valid if we have a current module. 5464 // FIXME: Should we error on these cases? 5465 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5466 Kind != SUBMODULE_DEFINITION) 5467 continue; 5468 5469 switch (Kind) { 5470 default: // Default behavior: ignore. 5471 break; 5472 5473 case SUBMODULE_DEFINITION: { 5474 if (Record.size() < 12) { 5475 Error("malformed module definition"); 5476 return Failure; 5477 } 5478 5479 StringRef Name = Blob; 5480 unsigned Idx = 0; 5481 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5482 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5483 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5484 bool IsFramework = Record[Idx++]; 5485 bool IsExplicit = Record[Idx++]; 5486 bool IsSystem = Record[Idx++]; 5487 bool IsExternC = Record[Idx++]; 5488 bool InferSubmodules = Record[Idx++]; 5489 bool InferExplicitSubmodules = Record[Idx++]; 5490 bool InferExportWildcard = Record[Idx++]; 5491 bool ConfigMacrosExhaustive = Record[Idx++]; 5492 bool ModuleMapIsPrivate = Record[Idx++]; 5493 5494 Module *ParentModule = nullptr; 5495 if (Parent) 5496 ParentModule = getSubmodule(Parent); 5497 5498 // Retrieve this (sub)module from the module map, creating it if 5499 // necessary. 5500 CurrentModule = 5501 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5502 .first; 5503 5504 // FIXME: set the definition loc for CurrentModule, or call 5505 // ModMap.setInferredModuleAllowedBy() 5506 5507 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5508 if (GlobalIndex >= SubmodulesLoaded.size() || 5509 SubmodulesLoaded[GlobalIndex]) { 5510 Error("too many submodules"); 5511 return Failure; 5512 } 5513 5514 if (!ParentModule) { 5515 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5516 // Don't emit module relocation error if we have -fno-validate-pch 5517 if (!PP.getPreprocessorOpts().DisablePCHValidation && 5518 CurFile != F.File) { 5519 Error(diag::err_module_file_conflict, 5520 CurrentModule->getTopLevelModuleName(), CurFile->getName(), 5521 F.File->getName()); 5522 return Failure; 5523 } 5524 } 5525 5526 F.DidReadTopLevelSubmodule = true; 5527 CurrentModule->setASTFile(F.File); 5528 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5529 } 5530 5531 CurrentModule->Kind = Kind; 5532 CurrentModule->Signature = F.Signature; 5533 CurrentModule->IsFromModuleFile = true; 5534 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5535 CurrentModule->IsExternC = IsExternC; 5536 CurrentModule->InferSubmodules = InferSubmodules; 5537 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5538 CurrentModule->InferExportWildcard = InferExportWildcard; 5539 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5540 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5541 if (DeserializationListener) 5542 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5543 5544 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5545 5546 // Clear out data that will be replaced by what is in the module file. 5547 CurrentModule->LinkLibraries.clear(); 5548 CurrentModule->ConfigMacros.clear(); 5549 CurrentModule->UnresolvedConflicts.clear(); 5550 CurrentModule->Conflicts.clear(); 5551 5552 // The module is available unless it's missing a requirement; relevant 5553 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5554 // Missing headers that were present when the module was built do not 5555 // make it unavailable -- if we got this far, this must be an explicitly 5556 // imported module file. 5557 CurrentModule->Requirements.clear(); 5558 CurrentModule->MissingHeaders.clear(); 5559 CurrentModule->IsUnimportable = 5560 ParentModule && ParentModule->IsUnimportable; 5561 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5562 break; 5563 } 5564 5565 case SUBMODULE_UMBRELLA_HEADER: { 5566 std::string Filename = std::string(Blob); 5567 ResolveImportedPath(F, Filename); 5568 if (auto Umbrella = PP.getFileManager().getOptionalFileRef(Filename)) { 5569 if (!CurrentModule->getUmbrellaHeader()) 5570 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob); 5571 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) { 5572 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5573 Error("mismatched umbrella headers in submodule"); 5574 return OutOfDate; 5575 } 5576 } 5577 break; 5578 } 5579 5580 case SUBMODULE_HEADER: 5581 case SUBMODULE_EXCLUDED_HEADER: 5582 case SUBMODULE_PRIVATE_HEADER: 5583 // We lazily associate headers with their modules via the HeaderInfo table. 5584 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5585 // of complete filenames or remove it entirely. 5586 break; 5587 5588 case SUBMODULE_TEXTUAL_HEADER: 5589 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5590 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5591 // them here. 5592 break; 5593 5594 case SUBMODULE_TOPHEADER: 5595 CurrentModule->addTopHeaderFilename(Blob); 5596 break; 5597 5598 case SUBMODULE_UMBRELLA_DIR: { 5599 std::string Dirname = std::string(Blob); 5600 ResolveImportedPath(F, Dirname); 5601 if (auto Umbrella = 5602 PP.getFileManager().getOptionalDirectoryRef(Dirname)) { 5603 if (!CurrentModule->getUmbrellaDir()) 5604 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob); 5605 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) { 5606 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5607 Error("mismatched umbrella directories in submodule"); 5608 return OutOfDate; 5609 } 5610 } 5611 break; 5612 } 5613 5614 case SUBMODULE_METADATA: { 5615 F.BaseSubmoduleID = getTotalNumSubmodules(); 5616 F.LocalNumSubmodules = Record[0]; 5617 unsigned LocalBaseSubmoduleID = Record[1]; 5618 if (F.LocalNumSubmodules > 0) { 5619 // Introduce the global -> local mapping for submodules within this 5620 // module. 5621 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5622 5623 // Introduce the local -> global mapping for submodules within this 5624 // module. 5625 F.SubmoduleRemap.insertOrReplace( 5626 std::make_pair(LocalBaseSubmoduleID, 5627 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5628 5629 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5630 } 5631 break; 5632 } 5633 5634 case SUBMODULE_IMPORTS: 5635 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5636 UnresolvedModuleRef Unresolved; 5637 Unresolved.File = &F; 5638 Unresolved.Mod = CurrentModule; 5639 Unresolved.ID = Record[Idx]; 5640 Unresolved.Kind = UnresolvedModuleRef::Import; 5641 Unresolved.IsWildcard = false; 5642 UnresolvedModuleRefs.push_back(Unresolved); 5643 } 5644 break; 5645 5646 case SUBMODULE_EXPORTS: 5647 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5648 UnresolvedModuleRef Unresolved; 5649 Unresolved.File = &F; 5650 Unresolved.Mod = CurrentModule; 5651 Unresolved.ID = Record[Idx]; 5652 Unresolved.Kind = UnresolvedModuleRef::Export; 5653 Unresolved.IsWildcard = Record[Idx + 1]; 5654 UnresolvedModuleRefs.push_back(Unresolved); 5655 } 5656 5657 // Once we've loaded the set of exports, there's no reason to keep 5658 // the parsed, unresolved exports around. 5659 CurrentModule->UnresolvedExports.clear(); 5660 break; 5661 5662 case SUBMODULE_REQUIRES: 5663 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5664 PP.getTargetInfo()); 5665 break; 5666 5667 case SUBMODULE_LINK_LIBRARY: 5668 ModMap.resolveLinkAsDependencies(CurrentModule); 5669 CurrentModule->LinkLibraries.push_back( 5670 Module::LinkLibrary(std::string(Blob), Record[0])); 5671 break; 5672 5673 case SUBMODULE_CONFIG_MACRO: 5674 CurrentModule->ConfigMacros.push_back(Blob.str()); 5675 break; 5676 5677 case SUBMODULE_CONFLICT: { 5678 UnresolvedModuleRef Unresolved; 5679 Unresolved.File = &F; 5680 Unresolved.Mod = CurrentModule; 5681 Unresolved.ID = Record[0]; 5682 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5683 Unresolved.IsWildcard = false; 5684 Unresolved.String = Blob; 5685 UnresolvedModuleRefs.push_back(Unresolved); 5686 break; 5687 } 5688 5689 case SUBMODULE_INITIALIZERS: { 5690 if (!ContextObj) 5691 break; 5692 SmallVector<uint32_t, 16> Inits; 5693 for (auto &ID : Record) 5694 Inits.push_back(getGlobalDeclID(F, ID)); 5695 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5696 break; 5697 } 5698 5699 case SUBMODULE_EXPORT_AS: 5700 CurrentModule->ExportAsModule = Blob.str(); 5701 ModMap.addLinkAsDependency(CurrentModule); 5702 break; 5703 } 5704 } 5705 } 5706 5707 /// Parse the record that corresponds to a LangOptions data 5708 /// structure. 5709 /// 5710 /// This routine parses the language options from the AST file and then gives 5711 /// them to the AST listener if one is set. 5712 /// 5713 /// \returns true if the listener deems the file unacceptable, false otherwise. 5714 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5715 bool Complain, 5716 ASTReaderListener &Listener, 5717 bool AllowCompatibleDifferences) { 5718 LangOptions LangOpts; 5719 unsigned Idx = 0; 5720 #define LANGOPT(Name, Bits, Default, Description) \ 5721 LangOpts.Name = Record[Idx++]; 5722 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5723 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5724 #include "clang/Basic/LangOptions.def" 5725 #define SANITIZER(NAME, ID) \ 5726 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5727 #include "clang/Basic/Sanitizers.def" 5728 5729 for (unsigned N = Record[Idx++]; N; --N) 5730 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5731 5732 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5733 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5734 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5735 5736 LangOpts.CurrentModule = ReadString(Record, Idx); 5737 5738 // Comment options. 5739 for (unsigned N = Record[Idx++]; N; --N) { 5740 LangOpts.CommentOpts.BlockCommandNames.push_back( 5741 ReadString(Record, Idx)); 5742 } 5743 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5744 5745 // OpenMP offloading options. 5746 for (unsigned N = Record[Idx++]; N; --N) { 5747 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5748 } 5749 5750 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5751 5752 return Listener.ReadLanguageOptions(LangOpts, Complain, 5753 AllowCompatibleDifferences); 5754 } 5755 5756 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5757 ASTReaderListener &Listener, 5758 bool AllowCompatibleDifferences) { 5759 unsigned Idx = 0; 5760 TargetOptions TargetOpts; 5761 TargetOpts.Triple = ReadString(Record, Idx); 5762 TargetOpts.CPU = ReadString(Record, Idx); 5763 TargetOpts.TuneCPU = ReadString(Record, Idx); 5764 TargetOpts.ABI = ReadString(Record, Idx); 5765 for (unsigned N = Record[Idx++]; N; --N) { 5766 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5767 } 5768 for (unsigned N = Record[Idx++]; N; --N) { 5769 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5770 } 5771 5772 return Listener.ReadTargetOptions(TargetOpts, Complain, 5773 AllowCompatibleDifferences); 5774 } 5775 5776 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5777 ASTReaderListener &Listener) { 5778 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5779 unsigned Idx = 0; 5780 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5781 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5782 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5783 #include "clang/Basic/DiagnosticOptions.def" 5784 5785 for (unsigned N = Record[Idx++]; N; --N) 5786 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5787 for (unsigned N = Record[Idx++]; N; --N) 5788 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5789 5790 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5791 } 5792 5793 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5794 ASTReaderListener &Listener) { 5795 FileSystemOptions FSOpts; 5796 unsigned Idx = 0; 5797 FSOpts.WorkingDir = ReadString(Record, Idx); 5798 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5799 } 5800 5801 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5802 bool Complain, 5803 ASTReaderListener &Listener) { 5804 HeaderSearchOptions HSOpts; 5805 unsigned Idx = 0; 5806 HSOpts.Sysroot = ReadString(Record, Idx); 5807 5808 // Include entries. 5809 for (unsigned N = Record[Idx++]; N; --N) { 5810 std::string Path = ReadString(Record, Idx); 5811 frontend::IncludeDirGroup Group 5812 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5813 bool IsFramework = Record[Idx++]; 5814 bool IgnoreSysRoot = Record[Idx++]; 5815 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5816 IgnoreSysRoot); 5817 } 5818 5819 // System header prefixes. 5820 for (unsigned N = Record[Idx++]; N; --N) { 5821 std::string Prefix = ReadString(Record, Idx); 5822 bool IsSystemHeader = Record[Idx++]; 5823 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5824 } 5825 5826 HSOpts.ResourceDir = ReadString(Record, Idx); 5827 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5828 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5829 HSOpts.DisableModuleHash = Record[Idx++]; 5830 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5831 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5832 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5833 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5834 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5835 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5836 HSOpts.UseLibcxx = Record[Idx++]; 5837 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5838 5839 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5840 Complain); 5841 } 5842 5843 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5844 bool Complain, 5845 ASTReaderListener &Listener, 5846 std::string &SuggestedPredefines) { 5847 PreprocessorOptions PPOpts; 5848 unsigned Idx = 0; 5849 5850 // Macro definitions/undefs 5851 for (unsigned N = Record[Idx++]; N; --N) { 5852 std::string Macro = ReadString(Record, Idx); 5853 bool IsUndef = Record[Idx++]; 5854 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5855 } 5856 5857 // Includes 5858 for (unsigned N = Record[Idx++]; N; --N) { 5859 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5860 } 5861 5862 // Macro Includes 5863 for (unsigned N = Record[Idx++]; N; --N) { 5864 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5865 } 5866 5867 PPOpts.UsePredefines = Record[Idx++]; 5868 PPOpts.DetailedRecord = Record[Idx++]; 5869 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5870 PPOpts.ObjCXXARCStandardLibrary = 5871 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5872 SuggestedPredefines.clear(); 5873 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5874 SuggestedPredefines); 5875 } 5876 5877 std::pair<ModuleFile *, unsigned> 5878 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5879 GlobalPreprocessedEntityMapType::iterator 5880 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5881 assert(I != GlobalPreprocessedEntityMap.end() && 5882 "Corrupted global preprocessed entity map"); 5883 ModuleFile *M = I->second; 5884 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5885 return std::make_pair(M, LocalIndex); 5886 } 5887 5888 llvm::iterator_range<PreprocessingRecord::iterator> 5889 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5890 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5891 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5892 Mod.NumPreprocessedEntities); 5893 5894 return llvm::make_range(PreprocessingRecord::iterator(), 5895 PreprocessingRecord::iterator()); 5896 } 5897 5898 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5899 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5900 return llvm::make_range( 5901 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5902 ModuleDeclIterator(this, &Mod, 5903 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5904 } 5905 5906 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5907 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5908 assert(I != GlobalSkippedRangeMap.end() && 5909 "Corrupted global skipped range map"); 5910 ModuleFile *M = I->second; 5911 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5912 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5913 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5914 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5915 TranslateSourceLocation(*M, RawRange.getEnd())); 5916 assert(Range.isValid()); 5917 return Range; 5918 } 5919 5920 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5921 PreprocessedEntityID PPID = Index+1; 5922 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5923 ModuleFile &M = *PPInfo.first; 5924 unsigned LocalIndex = PPInfo.second; 5925 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5926 5927 if (!PP.getPreprocessingRecord()) { 5928 Error("no preprocessing record"); 5929 return nullptr; 5930 } 5931 5932 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5933 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5934 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5935 Error(std::move(Err)); 5936 return nullptr; 5937 } 5938 5939 Expected<llvm::BitstreamEntry> MaybeEntry = 5940 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5941 if (!MaybeEntry) { 5942 Error(MaybeEntry.takeError()); 5943 return nullptr; 5944 } 5945 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5946 5947 if (Entry.Kind != llvm::BitstreamEntry::Record) 5948 return nullptr; 5949 5950 // Read the record. 5951 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5952 TranslateSourceLocation(M, PPOffs.getEnd())); 5953 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5954 StringRef Blob; 5955 RecordData Record; 5956 Expected<unsigned> MaybeRecType = 5957 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5958 if (!MaybeRecType) { 5959 Error(MaybeRecType.takeError()); 5960 return nullptr; 5961 } 5962 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 5963 case PPD_MACRO_EXPANSION: { 5964 bool isBuiltin = Record[0]; 5965 IdentifierInfo *Name = nullptr; 5966 MacroDefinitionRecord *Def = nullptr; 5967 if (isBuiltin) 5968 Name = getLocalIdentifier(M, Record[1]); 5969 else { 5970 PreprocessedEntityID GlobalID = 5971 getGlobalPreprocessedEntityID(M, Record[1]); 5972 Def = cast<MacroDefinitionRecord>( 5973 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5974 } 5975 5976 MacroExpansion *ME; 5977 if (isBuiltin) 5978 ME = new (PPRec) MacroExpansion(Name, Range); 5979 else 5980 ME = new (PPRec) MacroExpansion(Def, Range); 5981 5982 return ME; 5983 } 5984 5985 case PPD_MACRO_DEFINITION: { 5986 // Decode the identifier info and then check again; if the macro is 5987 // still defined and associated with the identifier, 5988 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 5989 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 5990 5991 if (DeserializationListener) 5992 DeserializationListener->MacroDefinitionRead(PPID, MD); 5993 5994 return MD; 5995 } 5996 5997 case PPD_INCLUSION_DIRECTIVE: { 5998 const char *FullFileNameStart = Blob.data() + Record[0]; 5999 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6000 const FileEntry *File = nullptr; 6001 if (!FullFileName.empty()) 6002 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6003 File = *FE; 6004 6005 // FIXME: Stable encoding 6006 InclusionDirective::InclusionKind Kind 6007 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6008 InclusionDirective *ID 6009 = new (PPRec) InclusionDirective(PPRec, Kind, 6010 StringRef(Blob.data(), Record[0]), 6011 Record[1], Record[3], 6012 File, 6013 Range); 6014 return ID; 6015 } 6016 } 6017 6018 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6019 } 6020 6021 /// Find the next module that contains entities and return the ID 6022 /// of the first entry. 6023 /// 6024 /// \param SLocMapI points at a chunk of a module that contains no 6025 /// preprocessed entities or the entities it contains are not the ones we are 6026 /// looking for. 6027 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6028 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6029 ++SLocMapI; 6030 for (GlobalSLocOffsetMapType::const_iterator 6031 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6032 ModuleFile &M = *SLocMapI->second; 6033 if (M.NumPreprocessedEntities) 6034 return M.BasePreprocessedEntityID; 6035 } 6036 6037 return getTotalNumPreprocessedEntities(); 6038 } 6039 6040 namespace { 6041 6042 struct PPEntityComp { 6043 const ASTReader &Reader; 6044 ModuleFile &M; 6045 6046 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6047 6048 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6049 SourceLocation LHS = getLoc(L); 6050 SourceLocation RHS = getLoc(R); 6051 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6052 } 6053 6054 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6055 SourceLocation LHS = getLoc(L); 6056 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6057 } 6058 6059 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6060 SourceLocation RHS = getLoc(R); 6061 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6062 } 6063 6064 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6065 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6066 } 6067 }; 6068 6069 } // namespace 6070 6071 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6072 bool EndsAfter) const { 6073 if (SourceMgr.isLocalSourceLocation(Loc)) 6074 return getTotalNumPreprocessedEntities(); 6075 6076 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6077 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6078 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6079 "Corrupted global sloc offset map"); 6080 6081 if (SLocMapI->second->NumPreprocessedEntities == 0) 6082 return findNextPreprocessedEntity(SLocMapI); 6083 6084 ModuleFile &M = *SLocMapI->second; 6085 6086 using pp_iterator = const PPEntityOffset *; 6087 6088 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6089 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6090 6091 size_t Count = M.NumPreprocessedEntities; 6092 size_t Half; 6093 pp_iterator First = pp_begin; 6094 pp_iterator PPI; 6095 6096 if (EndsAfter) { 6097 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6098 PPEntityComp(*this, M)); 6099 } else { 6100 // Do a binary search manually instead of using std::lower_bound because 6101 // The end locations of entities may be unordered (when a macro expansion 6102 // is inside another macro argument), but for this case it is not important 6103 // whether we get the first macro expansion or its containing macro. 6104 while (Count > 0) { 6105 Half = Count / 2; 6106 PPI = First; 6107 std::advance(PPI, Half); 6108 if (SourceMgr.isBeforeInTranslationUnit( 6109 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6110 First = PPI; 6111 ++First; 6112 Count = Count - Half - 1; 6113 } else 6114 Count = Half; 6115 } 6116 } 6117 6118 if (PPI == pp_end) 6119 return findNextPreprocessedEntity(SLocMapI); 6120 6121 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6122 } 6123 6124 /// Returns a pair of [Begin, End) indices of preallocated 6125 /// preprocessed entities that \arg Range encompasses. 6126 std::pair<unsigned, unsigned> 6127 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6128 if (Range.isInvalid()) 6129 return std::make_pair(0,0); 6130 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6131 6132 PreprocessedEntityID BeginID = 6133 findPreprocessedEntity(Range.getBegin(), false); 6134 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6135 return std::make_pair(BeginID, EndID); 6136 } 6137 6138 /// Optionally returns true or false if the preallocated preprocessed 6139 /// entity with index \arg Index came from file \arg FID. 6140 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6141 FileID FID) { 6142 if (FID.isInvalid()) 6143 return false; 6144 6145 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6146 ModuleFile &M = *PPInfo.first; 6147 unsigned LocalIndex = PPInfo.second; 6148 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6149 6150 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6151 if (Loc.isInvalid()) 6152 return false; 6153 6154 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6155 return true; 6156 else 6157 return false; 6158 } 6159 6160 namespace { 6161 6162 /// Visitor used to search for information about a header file. 6163 class HeaderFileInfoVisitor { 6164 const FileEntry *FE; 6165 Optional<HeaderFileInfo> HFI; 6166 6167 public: 6168 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6169 6170 bool operator()(ModuleFile &M) { 6171 HeaderFileInfoLookupTable *Table 6172 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6173 if (!Table) 6174 return false; 6175 6176 // Look in the on-disk hash table for an entry for this file name. 6177 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6178 if (Pos == Table->end()) 6179 return false; 6180 6181 HFI = *Pos; 6182 return true; 6183 } 6184 6185 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6186 }; 6187 6188 } // namespace 6189 6190 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6191 HeaderFileInfoVisitor Visitor(FE); 6192 ModuleMgr.visit(Visitor); 6193 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6194 return *HFI; 6195 6196 return HeaderFileInfo(); 6197 } 6198 6199 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6200 using DiagState = DiagnosticsEngine::DiagState; 6201 SmallVector<DiagState *, 32> DiagStates; 6202 6203 for (ModuleFile &F : ModuleMgr) { 6204 unsigned Idx = 0; 6205 auto &Record = F.PragmaDiagMappings; 6206 if (Record.empty()) 6207 continue; 6208 6209 DiagStates.clear(); 6210 6211 auto ReadDiagState = 6212 [&](const DiagState &BasedOn, SourceLocation Loc, 6213 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6214 unsigned BackrefID = Record[Idx++]; 6215 if (BackrefID != 0) 6216 return DiagStates[BackrefID - 1]; 6217 6218 // A new DiagState was created here. 6219 Diag.DiagStates.push_back(BasedOn); 6220 DiagState *NewState = &Diag.DiagStates.back(); 6221 DiagStates.push_back(NewState); 6222 unsigned Size = Record[Idx++]; 6223 assert(Idx + Size * 2 <= Record.size() && 6224 "Invalid data, not enough diag/map pairs"); 6225 while (Size--) { 6226 unsigned DiagID = Record[Idx++]; 6227 DiagnosticMapping NewMapping = 6228 DiagnosticMapping::deserialize(Record[Idx++]); 6229 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6230 continue; 6231 6232 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6233 6234 // If this mapping was specified as a warning but the severity was 6235 // upgraded due to diagnostic settings, simulate the current diagnostic 6236 // settings (and use a warning). 6237 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6238 NewMapping.setSeverity(diag::Severity::Warning); 6239 NewMapping.setUpgradedFromWarning(false); 6240 } 6241 6242 Mapping = NewMapping; 6243 } 6244 return NewState; 6245 }; 6246 6247 // Read the first state. 6248 DiagState *FirstState; 6249 if (F.Kind == MK_ImplicitModule) { 6250 // Implicitly-built modules are reused with different diagnostic 6251 // settings. Use the initial diagnostic state from Diag to simulate this 6252 // compilation's diagnostic settings. 6253 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6254 DiagStates.push_back(FirstState); 6255 6256 // Skip the initial diagnostic state from the serialized module. 6257 assert(Record[1] == 0 && 6258 "Invalid data, unexpected backref in initial state"); 6259 Idx = 3 + Record[2] * 2; 6260 assert(Idx < Record.size() && 6261 "Invalid data, not enough state change pairs in initial state"); 6262 } else if (F.isModule()) { 6263 // For an explicit module, preserve the flags from the module build 6264 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6265 // -Wblah flags. 6266 unsigned Flags = Record[Idx++]; 6267 DiagState Initial; 6268 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6269 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6270 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6271 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6272 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6273 Initial.ExtBehavior = (diag::Severity)Flags; 6274 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6275 6276 assert(F.OriginalSourceFileID.isValid()); 6277 6278 // Set up the root buffer of the module to start with the initial 6279 // diagnostic state of the module itself, to cover files that contain no 6280 // explicit transitions (for which we did not serialize anything). 6281 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6282 .StateTransitions.push_back({FirstState, 0}); 6283 } else { 6284 // For prefix ASTs, start with whatever the user configured on the 6285 // command line. 6286 Idx++; // Skip flags. 6287 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6288 SourceLocation(), false); 6289 } 6290 6291 // Read the state transitions. 6292 unsigned NumLocations = Record[Idx++]; 6293 while (NumLocations--) { 6294 assert(Idx < Record.size() && 6295 "Invalid data, missing pragma diagnostic states"); 6296 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6297 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6298 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6299 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6300 unsigned Transitions = Record[Idx++]; 6301 6302 // Note that we don't need to set up Parent/ParentOffset here, because 6303 // we won't be changing the diagnostic state within imported FileIDs 6304 // (other than perhaps appending to the main source file, which has no 6305 // parent). 6306 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6307 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6308 for (unsigned I = 0; I != Transitions; ++I) { 6309 unsigned Offset = Record[Idx++]; 6310 auto *State = 6311 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6312 F.StateTransitions.push_back({State, Offset}); 6313 } 6314 } 6315 6316 // Read the final state. 6317 assert(Idx < Record.size() && 6318 "Invalid data, missing final pragma diagnostic state"); 6319 SourceLocation CurStateLoc = 6320 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6321 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6322 6323 if (!F.isModule()) { 6324 Diag.DiagStatesByLoc.CurDiagState = CurState; 6325 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6326 6327 // Preserve the property that the imaginary root file describes the 6328 // current state. 6329 FileID NullFile; 6330 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6331 if (T.empty()) 6332 T.push_back({CurState, 0}); 6333 else 6334 T[0].State = CurState; 6335 } 6336 6337 // Don't try to read these mappings again. 6338 Record.clear(); 6339 } 6340 } 6341 6342 /// Get the correct cursor and offset for loading a type. 6343 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6344 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6345 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6346 ModuleFile *M = I->second; 6347 return RecordLocation( 6348 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6349 M->DeclsBlockStartOffset); 6350 } 6351 6352 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6353 switch (code) { 6354 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6355 case TYPE_##CODE_ID: return Type::CLASS_ID; 6356 #include "clang/Serialization/TypeBitCodes.def" 6357 default: return llvm::None; 6358 } 6359 } 6360 6361 /// Read and return the type with the given index.. 6362 /// 6363 /// The index is the type ID, shifted and minus the number of predefs. This 6364 /// routine actually reads the record corresponding to the type at the given 6365 /// location. It is a helper routine for GetType, which deals with reading type 6366 /// IDs. 6367 QualType ASTReader::readTypeRecord(unsigned Index) { 6368 assert(ContextObj && "reading type with no AST context"); 6369 ASTContext &Context = *ContextObj; 6370 RecordLocation Loc = TypeCursorForIndex(Index); 6371 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6372 6373 // Keep track of where we are in the stream, then jump back there 6374 // after reading this type. 6375 SavedStreamPosition SavedPosition(DeclsCursor); 6376 6377 ReadingKindTracker ReadingKind(Read_Type, *this); 6378 6379 // Note that we are loading a type record. 6380 Deserializing AType(this); 6381 6382 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6383 Error(std::move(Err)); 6384 return QualType(); 6385 } 6386 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6387 if (!RawCode) { 6388 Error(RawCode.takeError()); 6389 return QualType(); 6390 } 6391 6392 ASTRecordReader Record(*this, *Loc.F); 6393 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6394 if (!Code) { 6395 Error(Code.takeError()); 6396 return QualType(); 6397 } 6398 if (Code.get() == TYPE_EXT_QUAL) { 6399 QualType baseType = Record.readQualType(); 6400 Qualifiers quals = Record.readQualifiers(); 6401 return Context.getQualifiedType(baseType, quals); 6402 } 6403 6404 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6405 if (!maybeClass) { 6406 Error("Unexpected code for type"); 6407 return QualType(); 6408 } 6409 6410 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6411 return TypeReader.read(*maybeClass); 6412 } 6413 6414 namespace clang { 6415 6416 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6417 ASTRecordReader &Reader; 6418 6419 SourceLocation readSourceLocation() { 6420 return Reader.readSourceLocation(); 6421 } 6422 6423 TypeSourceInfo *GetTypeSourceInfo() { 6424 return Reader.readTypeSourceInfo(); 6425 } 6426 6427 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6428 return Reader.readNestedNameSpecifierLoc(); 6429 } 6430 6431 Attr *ReadAttr() { 6432 return Reader.readAttr(); 6433 } 6434 6435 public: 6436 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6437 6438 // We want compile-time assurance that we've enumerated all of 6439 // these, so unfortunately we have to declare them first, then 6440 // define them out-of-line. 6441 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6442 #define TYPELOC(CLASS, PARENT) \ 6443 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6444 #include "clang/AST/TypeLocNodes.def" 6445 6446 void VisitFunctionTypeLoc(FunctionTypeLoc); 6447 void VisitArrayTypeLoc(ArrayTypeLoc); 6448 }; 6449 6450 } // namespace clang 6451 6452 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6453 // nothing to do 6454 } 6455 6456 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6457 TL.setBuiltinLoc(readSourceLocation()); 6458 if (TL.needsExtraLocalData()) { 6459 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6460 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6461 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6462 TL.setModeAttr(Reader.readInt()); 6463 } 6464 } 6465 6466 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6467 TL.setNameLoc(readSourceLocation()); 6468 } 6469 6470 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6471 TL.setStarLoc(readSourceLocation()); 6472 } 6473 6474 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6475 // nothing to do 6476 } 6477 6478 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6479 // nothing to do 6480 } 6481 6482 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6483 TL.setExpansionLoc(readSourceLocation()); 6484 } 6485 6486 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6487 TL.setCaretLoc(readSourceLocation()); 6488 } 6489 6490 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6491 TL.setAmpLoc(readSourceLocation()); 6492 } 6493 6494 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6495 TL.setAmpAmpLoc(readSourceLocation()); 6496 } 6497 6498 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6499 TL.setStarLoc(readSourceLocation()); 6500 TL.setClassTInfo(GetTypeSourceInfo()); 6501 } 6502 6503 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6504 TL.setLBracketLoc(readSourceLocation()); 6505 TL.setRBracketLoc(readSourceLocation()); 6506 if (Reader.readBool()) 6507 TL.setSizeExpr(Reader.readExpr()); 6508 else 6509 TL.setSizeExpr(nullptr); 6510 } 6511 6512 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6513 VisitArrayTypeLoc(TL); 6514 } 6515 6516 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6517 VisitArrayTypeLoc(TL); 6518 } 6519 6520 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6521 VisitArrayTypeLoc(TL); 6522 } 6523 6524 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6525 DependentSizedArrayTypeLoc TL) { 6526 VisitArrayTypeLoc(TL); 6527 } 6528 6529 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6530 DependentAddressSpaceTypeLoc TL) { 6531 6532 TL.setAttrNameLoc(readSourceLocation()); 6533 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6534 TL.setAttrExprOperand(Reader.readExpr()); 6535 } 6536 6537 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6538 DependentSizedExtVectorTypeLoc TL) { 6539 TL.setNameLoc(readSourceLocation()); 6540 } 6541 6542 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6543 TL.setNameLoc(readSourceLocation()); 6544 } 6545 6546 void TypeLocReader::VisitDependentVectorTypeLoc( 6547 DependentVectorTypeLoc TL) { 6548 TL.setNameLoc(readSourceLocation()); 6549 } 6550 6551 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6552 TL.setNameLoc(readSourceLocation()); 6553 } 6554 6555 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6556 TL.setAttrNameLoc(readSourceLocation()); 6557 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6558 TL.setAttrRowOperand(Reader.readExpr()); 6559 TL.setAttrColumnOperand(Reader.readExpr()); 6560 } 6561 6562 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6563 DependentSizedMatrixTypeLoc TL) { 6564 TL.setAttrNameLoc(readSourceLocation()); 6565 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6566 TL.setAttrRowOperand(Reader.readExpr()); 6567 TL.setAttrColumnOperand(Reader.readExpr()); 6568 } 6569 6570 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6571 TL.setLocalRangeBegin(readSourceLocation()); 6572 TL.setLParenLoc(readSourceLocation()); 6573 TL.setRParenLoc(readSourceLocation()); 6574 TL.setExceptionSpecRange(Reader.readSourceRange()); 6575 TL.setLocalRangeEnd(readSourceLocation()); 6576 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6577 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6578 } 6579 } 6580 6581 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6582 VisitFunctionTypeLoc(TL); 6583 } 6584 6585 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6586 VisitFunctionTypeLoc(TL); 6587 } 6588 6589 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6590 TL.setNameLoc(readSourceLocation()); 6591 } 6592 6593 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6594 TL.setNameLoc(readSourceLocation()); 6595 } 6596 6597 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6598 TL.setTypeofLoc(readSourceLocation()); 6599 TL.setLParenLoc(readSourceLocation()); 6600 TL.setRParenLoc(readSourceLocation()); 6601 } 6602 6603 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6604 TL.setTypeofLoc(readSourceLocation()); 6605 TL.setLParenLoc(readSourceLocation()); 6606 TL.setRParenLoc(readSourceLocation()); 6607 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6608 } 6609 6610 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6611 TL.setNameLoc(readSourceLocation()); 6612 } 6613 6614 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6615 TL.setKWLoc(readSourceLocation()); 6616 TL.setLParenLoc(readSourceLocation()); 6617 TL.setRParenLoc(readSourceLocation()); 6618 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6619 } 6620 6621 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6622 TL.setNameLoc(readSourceLocation()); 6623 if (Reader.readBool()) { 6624 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6625 TL.setTemplateKWLoc(readSourceLocation()); 6626 TL.setConceptNameLoc(readSourceLocation()); 6627 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6628 TL.setLAngleLoc(readSourceLocation()); 6629 TL.setRAngleLoc(readSourceLocation()); 6630 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6631 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6632 TL.getTypePtr()->getArg(i).getKind())); 6633 } 6634 } 6635 6636 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6637 DeducedTemplateSpecializationTypeLoc TL) { 6638 TL.setTemplateNameLoc(readSourceLocation()); 6639 } 6640 6641 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6642 TL.setNameLoc(readSourceLocation()); 6643 } 6644 6645 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6646 TL.setNameLoc(readSourceLocation()); 6647 } 6648 6649 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6650 TL.setAttr(ReadAttr()); 6651 } 6652 6653 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6654 TL.setNameLoc(readSourceLocation()); 6655 } 6656 6657 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6658 SubstTemplateTypeParmTypeLoc TL) { 6659 TL.setNameLoc(readSourceLocation()); 6660 } 6661 6662 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6663 SubstTemplateTypeParmPackTypeLoc TL) { 6664 TL.setNameLoc(readSourceLocation()); 6665 } 6666 6667 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6668 TemplateSpecializationTypeLoc TL) { 6669 TL.setTemplateKeywordLoc(readSourceLocation()); 6670 TL.setTemplateNameLoc(readSourceLocation()); 6671 TL.setLAngleLoc(readSourceLocation()); 6672 TL.setRAngleLoc(readSourceLocation()); 6673 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6674 TL.setArgLocInfo( 6675 i, 6676 Reader.readTemplateArgumentLocInfo( 6677 TL.getTypePtr()->getArg(i).getKind())); 6678 } 6679 6680 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6681 TL.setLParenLoc(readSourceLocation()); 6682 TL.setRParenLoc(readSourceLocation()); 6683 } 6684 6685 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6686 TL.setElaboratedKeywordLoc(readSourceLocation()); 6687 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6688 } 6689 6690 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6691 TL.setNameLoc(readSourceLocation()); 6692 } 6693 6694 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6695 TL.setElaboratedKeywordLoc(readSourceLocation()); 6696 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6697 TL.setNameLoc(readSourceLocation()); 6698 } 6699 6700 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6701 DependentTemplateSpecializationTypeLoc TL) { 6702 TL.setElaboratedKeywordLoc(readSourceLocation()); 6703 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6704 TL.setTemplateKeywordLoc(readSourceLocation()); 6705 TL.setTemplateNameLoc(readSourceLocation()); 6706 TL.setLAngleLoc(readSourceLocation()); 6707 TL.setRAngleLoc(readSourceLocation()); 6708 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6709 TL.setArgLocInfo( 6710 I, 6711 Reader.readTemplateArgumentLocInfo( 6712 TL.getTypePtr()->getArg(I).getKind())); 6713 } 6714 6715 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6716 TL.setEllipsisLoc(readSourceLocation()); 6717 } 6718 6719 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6720 TL.setNameLoc(readSourceLocation()); 6721 } 6722 6723 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6724 if (TL.getNumProtocols()) { 6725 TL.setProtocolLAngleLoc(readSourceLocation()); 6726 TL.setProtocolRAngleLoc(readSourceLocation()); 6727 } 6728 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6729 TL.setProtocolLoc(i, readSourceLocation()); 6730 } 6731 6732 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6733 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6734 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6735 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6736 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6737 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6738 TL.setProtocolLAngleLoc(readSourceLocation()); 6739 TL.setProtocolRAngleLoc(readSourceLocation()); 6740 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6741 TL.setProtocolLoc(i, readSourceLocation()); 6742 } 6743 6744 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6745 TL.setStarLoc(readSourceLocation()); 6746 } 6747 6748 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6749 TL.setKWLoc(readSourceLocation()); 6750 TL.setLParenLoc(readSourceLocation()); 6751 TL.setRParenLoc(readSourceLocation()); 6752 } 6753 6754 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6755 TL.setKWLoc(readSourceLocation()); 6756 } 6757 6758 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) { 6759 TL.setNameLoc(readSourceLocation()); 6760 } 6761 void TypeLocReader::VisitDependentExtIntTypeLoc( 6762 clang::DependentExtIntTypeLoc TL) { 6763 TL.setNameLoc(readSourceLocation()); 6764 } 6765 6766 6767 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6768 TypeLocReader TLR(*this); 6769 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6770 TLR.Visit(TL); 6771 } 6772 6773 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6774 QualType InfoTy = readType(); 6775 if (InfoTy.isNull()) 6776 return nullptr; 6777 6778 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6779 readTypeLoc(TInfo->getTypeLoc()); 6780 return TInfo; 6781 } 6782 6783 QualType ASTReader::GetType(TypeID ID) { 6784 assert(ContextObj && "reading type with no AST context"); 6785 ASTContext &Context = *ContextObj; 6786 6787 unsigned FastQuals = ID & Qualifiers::FastMask; 6788 unsigned Index = ID >> Qualifiers::FastWidth; 6789 6790 if (Index < NUM_PREDEF_TYPE_IDS) { 6791 QualType T; 6792 switch ((PredefinedTypeIDs)Index) { 6793 case PREDEF_TYPE_NULL_ID: 6794 return QualType(); 6795 case PREDEF_TYPE_VOID_ID: 6796 T = Context.VoidTy; 6797 break; 6798 case PREDEF_TYPE_BOOL_ID: 6799 T = Context.BoolTy; 6800 break; 6801 case PREDEF_TYPE_CHAR_U_ID: 6802 case PREDEF_TYPE_CHAR_S_ID: 6803 // FIXME: Check that the signedness of CharTy is correct! 6804 T = Context.CharTy; 6805 break; 6806 case PREDEF_TYPE_UCHAR_ID: 6807 T = Context.UnsignedCharTy; 6808 break; 6809 case PREDEF_TYPE_USHORT_ID: 6810 T = Context.UnsignedShortTy; 6811 break; 6812 case PREDEF_TYPE_UINT_ID: 6813 T = Context.UnsignedIntTy; 6814 break; 6815 case PREDEF_TYPE_ULONG_ID: 6816 T = Context.UnsignedLongTy; 6817 break; 6818 case PREDEF_TYPE_ULONGLONG_ID: 6819 T = Context.UnsignedLongLongTy; 6820 break; 6821 case PREDEF_TYPE_UINT128_ID: 6822 T = Context.UnsignedInt128Ty; 6823 break; 6824 case PREDEF_TYPE_SCHAR_ID: 6825 T = Context.SignedCharTy; 6826 break; 6827 case PREDEF_TYPE_WCHAR_ID: 6828 T = Context.WCharTy; 6829 break; 6830 case PREDEF_TYPE_SHORT_ID: 6831 T = Context.ShortTy; 6832 break; 6833 case PREDEF_TYPE_INT_ID: 6834 T = Context.IntTy; 6835 break; 6836 case PREDEF_TYPE_LONG_ID: 6837 T = Context.LongTy; 6838 break; 6839 case PREDEF_TYPE_LONGLONG_ID: 6840 T = Context.LongLongTy; 6841 break; 6842 case PREDEF_TYPE_INT128_ID: 6843 T = Context.Int128Ty; 6844 break; 6845 case PREDEF_TYPE_BFLOAT16_ID: 6846 T = Context.BFloat16Ty; 6847 break; 6848 case PREDEF_TYPE_HALF_ID: 6849 T = Context.HalfTy; 6850 break; 6851 case PREDEF_TYPE_FLOAT_ID: 6852 T = Context.FloatTy; 6853 break; 6854 case PREDEF_TYPE_DOUBLE_ID: 6855 T = Context.DoubleTy; 6856 break; 6857 case PREDEF_TYPE_LONGDOUBLE_ID: 6858 T = Context.LongDoubleTy; 6859 break; 6860 case PREDEF_TYPE_SHORT_ACCUM_ID: 6861 T = Context.ShortAccumTy; 6862 break; 6863 case PREDEF_TYPE_ACCUM_ID: 6864 T = Context.AccumTy; 6865 break; 6866 case PREDEF_TYPE_LONG_ACCUM_ID: 6867 T = Context.LongAccumTy; 6868 break; 6869 case PREDEF_TYPE_USHORT_ACCUM_ID: 6870 T = Context.UnsignedShortAccumTy; 6871 break; 6872 case PREDEF_TYPE_UACCUM_ID: 6873 T = Context.UnsignedAccumTy; 6874 break; 6875 case PREDEF_TYPE_ULONG_ACCUM_ID: 6876 T = Context.UnsignedLongAccumTy; 6877 break; 6878 case PREDEF_TYPE_SHORT_FRACT_ID: 6879 T = Context.ShortFractTy; 6880 break; 6881 case PREDEF_TYPE_FRACT_ID: 6882 T = Context.FractTy; 6883 break; 6884 case PREDEF_TYPE_LONG_FRACT_ID: 6885 T = Context.LongFractTy; 6886 break; 6887 case PREDEF_TYPE_USHORT_FRACT_ID: 6888 T = Context.UnsignedShortFractTy; 6889 break; 6890 case PREDEF_TYPE_UFRACT_ID: 6891 T = Context.UnsignedFractTy; 6892 break; 6893 case PREDEF_TYPE_ULONG_FRACT_ID: 6894 T = Context.UnsignedLongFractTy; 6895 break; 6896 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6897 T = Context.SatShortAccumTy; 6898 break; 6899 case PREDEF_TYPE_SAT_ACCUM_ID: 6900 T = Context.SatAccumTy; 6901 break; 6902 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6903 T = Context.SatLongAccumTy; 6904 break; 6905 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6906 T = Context.SatUnsignedShortAccumTy; 6907 break; 6908 case PREDEF_TYPE_SAT_UACCUM_ID: 6909 T = Context.SatUnsignedAccumTy; 6910 break; 6911 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6912 T = Context.SatUnsignedLongAccumTy; 6913 break; 6914 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6915 T = Context.SatShortFractTy; 6916 break; 6917 case PREDEF_TYPE_SAT_FRACT_ID: 6918 T = Context.SatFractTy; 6919 break; 6920 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6921 T = Context.SatLongFractTy; 6922 break; 6923 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6924 T = Context.SatUnsignedShortFractTy; 6925 break; 6926 case PREDEF_TYPE_SAT_UFRACT_ID: 6927 T = Context.SatUnsignedFractTy; 6928 break; 6929 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6930 T = Context.SatUnsignedLongFractTy; 6931 break; 6932 case PREDEF_TYPE_FLOAT16_ID: 6933 T = Context.Float16Ty; 6934 break; 6935 case PREDEF_TYPE_FLOAT128_ID: 6936 T = Context.Float128Ty; 6937 break; 6938 case PREDEF_TYPE_OVERLOAD_ID: 6939 T = Context.OverloadTy; 6940 break; 6941 case PREDEF_TYPE_BOUND_MEMBER: 6942 T = Context.BoundMemberTy; 6943 break; 6944 case PREDEF_TYPE_PSEUDO_OBJECT: 6945 T = Context.PseudoObjectTy; 6946 break; 6947 case PREDEF_TYPE_DEPENDENT_ID: 6948 T = Context.DependentTy; 6949 break; 6950 case PREDEF_TYPE_UNKNOWN_ANY: 6951 T = Context.UnknownAnyTy; 6952 break; 6953 case PREDEF_TYPE_NULLPTR_ID: 6954 T = Context.NullPtrTy; 6955 break; 6956 case PREDEF_TYPE_CHAR8_ID: 6957 T = Context.Char8Ty; 6958 break; 6959 case PREDEF_TYPE_CHAR16_ID: 6960 T = Context.Char16Ty; 6961 break; 6962 case PREDEF_TYPE_CHAR32_ID: 6963 T = Context.Char32Ty; 6964 break; 6965 case PREDEF_TYPE_OBJC_ID: 6966 T = Context.ObjCBuiltinIdTy; 6967 break; 6968 case PREDEF_TYPE_OBJC_CLASS: 6969 T = Context.ObjCBuiltinClassTy; 6970 break; 6971 case PREDEF_TYPE_OBJC_SEL: 6972 T = Context.ObjCBuiltinSelTy; 6973 break; 6974 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6975 case PREDEF_TYPE_##Id##_ID: \ 6976 T = Context.SingletonId; \ 6977 break; 6978 #include "clang/Basic/OpenCLImageTypes.def" 6979 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 6980 case PREDEF_TYPE_##Id##_ID: \ 6981 T = Context.Id##Ty; \ 6982 break; 6983 #include "clang/Basic/OpenCLExtensionTypes.def" 6984 case PREDEF_TYPE_SAMPLER_ID: 6985 T = Context.OCLSamplerTy; 6986 break; 6987 case PREDEF_TYPE_EVENT_ID: 6988 T = Context.OCLEventTy; 6989 break; 6990 case PREDEF_TYPE_CLK_EVENT_ID: 6991 T = Context.OCLClkEventTy; 6992 break; 6993 case PREDEF_TYPE_QUEUE_ID: 6994 T = Context.OCLQueueTy; 6995 break; 6996 case PREDEF_TYPE_RESERVE_ID_ID: 6997 T = Context.OCLReserveIDTy; 6998 break; 6999 case PREDEF_TYPE_AUTO_DEDUCT: 7000 T = Context.getAutoDeductType(); 7001 break; 7002 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7003 T = Context.getAutoRRefDeductType(); 7004 break; 7005 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7006 T = Context.ARCUnbridgedCastTy; 7007 break; 7008 case PREDEF_TYPE_BUILTIN_FN: 7009 T = Context.BuiltinFnTy; 7010 break; 7011 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7012 T = Context.IncompleteMatrixIdxTy; 7013 break; 7014 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7015 T = Context.OMPArraySectionTy; 7016 break; 7017 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7018 T = Context.OMPArraySectionTy; 7019 break; 7020 case PREDEF_TYPE_OMP_ITERATOR: 7021 T = Context.OMPIteratorTy; 7022 break; 7023 #define SVE_TYPE(Name, Id, SingletonId) \ 7024 case PREDEF_TYPE_##Id##_ID: \ 7025 T = Context.SingletonId; \ 7026 break; 7027 #include "clang/Basic/AArch64SVEACLETypes.def" 7028 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 7029 case PREDEF_TYPE_##Id##_ID: \ 7030 T = Context.Id##Ty; \ 7031 break; 7032 #include "clang/Basic/PPCTypes.def" 7033 } 7034 7035 assert(!T.isNull() && "Unknown predefined type"); 7036 return T.withFastQualifiers(FastQuals); 7037 } 7038 7039 Index -= NUM_PREDEF_TYPE_IDS; 7040 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7041 if (TypesLoaded[Index].isNull()) { 7042 TypesLoaded[Index] = readTypeRecord(Index); 7043 if (TypesLoaded[Index].isNull()) 7044 return QualType(); 7045 7046 TypesLoaded[Index]->setFromAST(); 7047 if (DeserializationListener) 7048 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7049 TypesLoaded[Index]); 7050 } 7051 7052 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7053 } 7054 7055 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7056 return GetType(getGlobalTypeID(F, LocalID)); 7057 } 7058 7059 serialization::TypeID 7060 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7061 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7062 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7063 7064 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7065 return LocalID; 7066 7067 if (!F.ModuleOffsetMap.empty()) 7068 ReadModuleOffsetMap(F); 7069 7070 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7071 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7072 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7073 7074 unsigned GlobalIndex = LocalIndex + I->second; 7075 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7076 } 7077 7078 TemplateArgumentLocInfo 7079 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7080 switch (Kind) { 7081 case TemplateArgument::Expression: 7082 return readExpr(); 7083 case TemplateArgument::Type: 7084 return readTypeSourceInfo(); 7085 case TemplateArgument::Template: { 7086 NestedNameSpecifierLoc QualifierLoc = 7087 readNestedNameSpecifierLoc(); 7088 SourceLocation TemplateNameLoc = readSourceLocation(); 7089 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7090 TemplateNameLoc, SourceLocation()); 7091 } 7092 case TemplateArgument::TemplateExpansion: { 7093 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7094 SourceLocation TemplateNameLoc = readSourceLocation(); 7095 SourceLocation EllipsisLoc = readSourceLocation(); 7096 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7097 TemplateNameLoc, EllipsisLoc); 7098 } 7099 case TemplateArgument::Null: 7100 case TemplateArgument::Integral: 7101 case TemplateArgument::Declaration: 7102 case TemplateArgument::NullPtr: 7103 case TemplateArgument::Pack: 7104 // FIXME: Is this right? 7105 return TemplateArgumentLocInfo(); 7106 } 7107 llvm_unreachable("unexpected template argument loc"); 7108 } 7109 7110 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7111 TemplateArgument Arg = readTemplateArgument(); 7112 7113 if (Arg.getKind() == TemplateArgument::Expression) { 7114 if (readBool()) // bool InfoHasSameExpr. 7115 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7116 } 7117 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7118 } 7119 7120 const ASTTemplateArgumentListInfo * 7121 ASTRecordReader::readASTTemplateArgumentListInfo() { 7122 SourceLocation LAngleLoc = readSourceLocation(); 7123 SourceLocation RAngleLoc = readSourceLocation(); 7124 unsigned NumArgsAsWritten = readInt(); 7125 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7126 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7127 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7128 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7129 } 7130 7131 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7132 return GetDecl(ID); 7133 } 7134 7135 void ASTReader::CompleteRedeclChain(const Decl *D) { 7136 if (NumCurrentElementsDeserializing) { 7137 // We arrange to not care about the complete redeclaration chain while we're 7138 // deserializing. Just remember that the AST has marked this one as complete 7139 // but that it's not actually complete yet, so we know we still need to 7140 // complete it later. 7141 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7142 return; 7143 } 7144 7145 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7146 7147 // If this is a named declaration, complete it by looking it up 7148 // within its context. 7149 // 7150 // FIXME: Merging a function definition should merge 7151 // all mergeable entities within it. 7152 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7153 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7154 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7155 if (!getContext().getLangOpts().CPlusPlus && 7156 isa<TranslationUnitDecl>(DC)) { 7157 // Outside of C++, we don't have a lookup table for the TU, so update 7158 // the identifier instead. (For C++ modules, we don't store decls 7159 // in the serialized identifier table, so we do the lookup in the TU.) 7160 auto *II = Name.getAsIdentifierInfo(); 7161 assert(II && "non-identifier name in C?"); 7162 if (II->isOutOfDate()) 7163 updateOutOfDateIdentifier(*II); 7164 } else 7165 DC->lookup(Name); 7166 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7167 // Find all declarations of this kind from the relevant context. 7168 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7169 auto *DC = cast<DeclContext>(DCDecl); 7170 SmallVector<Decl*, 8> Decls; 7171 FindExternalLexicalDecls( 7172 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7173 } 7174 } 7175 } 7176 7177 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7178 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7179 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7180 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7181 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7182 if (auto *Template = FD->getPrimaryTemplate()) 7183 Template->LoadLazySpecializations(); 7184 } 7185 } 7186 7187 CXXCtorInitializer ** 7188 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7189 RecordLocation Loc = getLocalBitOffset(Offset); 7190 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7191 SavedStreamPosition SavedPosition(Cursor); 7192 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7193 Error(std::move(Err)); 7194 return nullptr; 7195 } 7196 ReadingKindTracker ReadingKind(Read_Decl, *this); 7197 7198 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7199 if (!MaybeCode) { 7200 Error(MaybeCode.takeError()); 7201 return nullptr; 7202 } 7203 unsigned Code = MaybeCode.get(); 7204 7205 ASTRecordReader Record(*this, *Loc.F); 7206 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7207 if (!MaybeRecCode) { 7208 Error(MaybeRecCode.takeError()); 7209 return nullptr; 7210 } 7211 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7212 Error("malformed AST file: missing C++ ctor initializers"); 7213 return nullptr; 7214 } 7215 7216 return Record.readCXXCtorInitializers(); 7217 } 7218 7219 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7220 assert(ContextObj && "reading base specifiers with no AST context"); 7221 ASTContext &Context = *ContextObj; 7222 7223 RecordLocation Loc = getLocalBitOffset(Offset); 7224 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7225 SavedStreamPosition SavedPosition(Cursor); 7226 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7227 Error(std::move(Err)); 7228 return nullptr; 7229 } 7230 ReadingKindTracker ReadingKind(Read_Decl, *this); 7231 7232 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7233 if (!MaybeCode) { 7234 Error(MaybeCode.takeError()); 7235 return nullptr; 7236 } 7237 unsigned Code = MaybeCode.get(); 7238 7239 ASTRecordReader Record(*this, *Loc.F); 7240 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7241 if (!MaybeRecCode) { 7242 Error(MaybeCode.takeError()); 7243 return nullptr; 7244 } 7245 unsigned RecCode = MaybeRecCode.get(); 7246 7247 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7248 Error("malformed AST file: missing C++ base specifiers"); 7249 return nullptr; 7250 } 7251 7252 unsigned NumBases = Record.readInt(); 7253 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7254 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7255 for (unsigned I = 0; I != NumBases; ++I) 7256 Bases[I] = Record.readCXXBaseSpecifier(); 7257 return Bases; 7258 } 7259 7260 serialization::DeclID 7261 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7262 if (LocalID < NUM_PREDEF_DECL_IDS) 7263 return LocalID; 7264 7265 if (!F.ModuleOffsetMap.empty()) 7266 ReadModuleOffsetMap(F); 7267 7268 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7269 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7270 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7271 7272 return LocalID + I->second; 7273 } 7274 7275 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7276 ModuleFile &M) const { 7277 // Predefined decls aren't from any module. 7278 if (ID < NUM_PREDEF_DECL_IDS) 7279 return false; 7280 7281 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7282 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7283 } 7284 7285 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7286 if (!D->isFromASTFile()) 7287 return nullptr; 7288 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7289 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7290 return I->second; 7291 } 7292 7293 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7294 if (ID < NUM_PREDEF_DECL_IDS) 7295 return SourceLocation(); 7296 7297 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7298 7299 if (Index > DeclsLoaded.size()) { 7300 Error("declaration ID out-of-range for AST file"); 7301 return SourceLocation(); 7302 } 7303 7304 if (Decl *D = DeclsLoaded[Index]) 7305 return D->getLocation(); 7306 7307 SourceLocation Loc; 7308 DeclCursorForID(ID, Loc); 7309 return Loc; 7310 } 7311 7312 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7313 switch (ID) { 7314 case PREDEF_DECL_NULL_ID: 7315 return nullptr; 7316 7317 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7318 return Context.getTranslationUnitDecl(); 7319 7320 case PREDEF_DECL_OBJC_ID_ID: 7321 return Context.getObjCIdDecl(); 7322 7323 case PREDEF_DECL_OBJC_SEL_ID: 7324 return Context.getObjCSelDecl(); 7325 7326 case PREDEF_DECL_OBJC_CLASS_ID: 7327 return Context.getObjCClassDecl(); 7328 7329 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7330 return Context.getObjCProtocolDecl(); 7331 7332 case PREDEF_DECL_INT_128_ID: 7333 return Context.getInt128Decl(); 7334 7335 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7336 return Context.getUInt128Decl(); 7337 7338 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7339 return Context.getObjCInstanceTypeDecl(); 7340 7341 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7342 return Context.getBuiltinVaListDecl(); 7343 7344 case PREDEF_DECL_VA_LIST_TAG: 7345 return Context.getVaListTagDecl(); 7346 7347 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7348 return Context.getBuiltinMSVaListDecl(); 7349 7350 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7351 return Context.getMSGuidTagDecl(); 7352 7353 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7354 return Context.getExternCContextDecl(); 7355 7356 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7357 return Context.getMakeIntegerSeqDecl(); 7358 7359 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7360 return Context.getCFConstantStringDecl(); 7361 7362 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7363 return Context.getCFConstantStringTagDecl(); 7364 7365 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7366 return Context.getTypePackElementDecl(); 7367 } 7368 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7369 } 7370 7371 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7372 assert(ContextObj && "reading decl with no AST context"); 7373 if (ID < NUM_PREDEF_DECL_IDS) { 7374 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7375 if (D) { 7376 // Track that we have merged the declaration with ID \p ID into the 7377 // pre-existing predefined declaration \p D. 7378 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7379 if (Merged.empty()) 7380 Merged.push_back(ID); 7381 } 7382 return D; 7383 } 7384 7385 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7386 7387 if (Index >= DeclsLoaded.size()) { 7388 assert(0 && "declaration ID out-of-range for AST file"); 7389 Error("declaration ID out-of-range for AST file"); 7390 return nullptr; 7391 } 7392 7393 return DeclsLoaded[Index]; 7394 } 7395 7396 Decl *ASTReader::GetDecl(DeclID ID) { 7397 if (ID < NUM_PREDEF_DECL_IDS) 7398 return GetExistingDecl(ID); 7399 7400 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7401 7402 if (Index >= DeclsLoaded.size()) { 7403 assert(0 && "declaration ID out-of-range for AST file"); 7404 Error("declaration ID out-of-range for AST file"); 7405 return nullptr; 7406 } 7407 7408 if (!DeclsLoaded[Index]) { 7409 ReadDeclRecord(ID); 7410 if (DeserializationListener) 7411 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7412 } 7413 7414 return DeclsLoaded[Index]; 7415 } 7416 7417 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7418 DeclID GlobalID) { 7419 if (GlobalID < NUM_PREDEF_DECL_IDS) 7420 return GlobalID; 7421 7422 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7423 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7424 ModuleFile *Owner = I->second; 7425 7426 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7427 = M.GlobalToLocalDeclIDs.find(Owner); 7428 if (Pos == M.GlobalToLocalDeclIDs.end()) 7429 return 0; 7430 7431 return GlobalID - Owner->BaseDeclID + Pos->second; 7432 } 7433 7434 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7435 const RecordData &Record, 7436 unsigned &Idx) { 7437 if (Idx >= Record.size()) { 7438 Error("Corrupted AST file"); 7439 return 0; 7440 } 7441 7442 return getGlobalDeclID(F, Record[Idx++]); 7443 } 7444 7445 /// Resolve the offset of a statement into a statement. 7446 /// 7447 /// This operation will read a new statement from the external 7448 /// source each time it is called, and is meant to be used via a 7449 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7450 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7451 // Switch case IDs are per Decl. 7452 ClearSwitchCaseIDs(); 7453 7454 // Offset here is a global offset across the entire chain. 7455 RecordLocation Loc = getLocalBitOffset(Offset); 7456 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7457 Error(std::move(Err)); 7458 return nullptr; 7459 } 7460 assert(NumCurrentElementsDeserializing == 0 && 7461 "should not be called while already deserializing"); 7462 Deserializing D(this); 7463 return ReadStmtFromStream(*Loc.F); 7464 } 7465 7466 void ASTReader::FindExternalLexicalDecls( 7467 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7468 SmallVectorImpl<Decl *> &Decls) { 7469 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7470 7471 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7472 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7473 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7474 auto K = (Decl::Kind)+LexicalDecls[I]; 7475 if (!IsKindWeWant(K)) 7476 continue; 7477 7478 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7479 7480 // Don't add predefined declarations to the lexical context more 7481 // than once. 7482 if (ID < NUM_PREDEF_DECL_IDS) { 7483 if (PredefsVisited[ID]) 7484 continue; 7485 7486 PredefsVisited[ID] = true; 7487 } 7488 7489 if (Decl *D = GetLocalDecl(*M, ID)) { 7490 assert(D->getKind() == K && "wrong kind for lexical decl"); 7491 if (!DC->isDeclInLexicalTraversal(D)) 7492 Decls.push_back(D); 7493 } 7494 } 7495 }; 7496 7497 if (isa<TranslationUnitDecl>(DC)) { 7498 for (auto Lexical : TULexicalDecls) 7499 Visit(Lexical.first, Lexical.second); 7500 } else { 7501 auto I = LexicalDecls.find(DC); 7502 if (I != LexicalDecls.end()) 7503 Visit(I->second.first, I->second.second); 7504 } 7505 7506 ++NumLexicalDeclContextsRead; 7507 } 7508 7509 namespace { 7510 7511 class DeclIDComp { 7512 ASTReader &Reader; 7513 ModuleFile &Mod; 7514 7515 public: 7516 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7517 7518 bool operator()(LocalDeclID L, LocalDeclID R) const { 7519 SourceLocation LHS = getLocation(L); 7520 SourceLocation RHS = getLocation(R); 7521 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7522 } 7523 7524 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7525 SourceLocation RHS = getLocation(R); 7526 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7527 } 7528 7529 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7530 SourceLocation LHS = getLocation(L); 7531 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7532 } 7533 7534 SourceLocation getLocation(LocalDeclID ID) const { 7535 return Reader.getSourceManager().getFileLoc( 7536 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7537 } 7538 }; 7539 7540 } // namespace 7541 7542 void ASTReader::FindFileRegionDecls(FileID File, 7543 unsigned Offset, unsigned Length, 7544 SmallVectorImpl<Decl *> &Decls) { 7545 SourceManager &SM = getSourceManager(); 7546 7547 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7548 if (I == FileDeclIDs.end()) 7549 return; 7550 7551 FileDeclsInfo &DInfo = I->second; 7552 if (DInfo.Decls.empty()) 7553 return; 7554 7555 SourceLocation 7556 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7557 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7558 7559 DeclIDComp DIDComp(*this, *DInfo.Mod); 7560 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7561 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7562 if (BeginIt != DInfo.Decls.begin()) 7563 --BeginIt; 7564 7565 // If we are pointing at a top-level decl inside an objc container, we need 7566 // to backtrack until we find it otherwise we will fail to report that the 7567 // region overlaps with an objc container. 7568 while (BeginIt != DInfo.Decls.begin() && 7569 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7570 ->isTopLevelDeclInObjCContainer()) 7571 --BeginIt; 7572 7573 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7574 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7575 if (EndIt != DInfo.Decls.end()) 7576 ++EndIt; 7577 7578 for (ArrayRef<serialization::LocalDeclID>::iterator 7579 DIt = BeginIt; DIt != EndIt; ++DIt) 7580 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7581 } 7582 7583 bool 7584 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7585 DeclarationName Name) { 7586 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7587 "DeclContext has no visible decls in storage"); 7588 if (!Name) 7589 return false; 7590 7591 auto It = Lookups.find(DC); 7592 if (It == Lookups.end()) 7593 return false; 7594 7595 Deserializing LookupResults(this); 7596 7597 // Load the list of declarations. 7598 SmallVector<NamedDecl *, 64> Decls; 7599 for (DeclID ID : It->second.Table.find(Name)) { 7600 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7601 if (ND->getDeclName() == Name) 7602 Decls.push_back(ND); 7603 } 7604 7605 ++NumVisibleDeclContextsRead; 7606 SetExternalVisibleDeclsForName(DC, Name, Decls); 7607 return !Decls.empty(); 7608 } 7609 7610 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7611 if (!DC->hasExternalVisibleStorage()) 7612 return; 7613 7614 auto It = Lookups.find(DC); 7615 assert(It != Lookups.end() && 7616 "have external visible storage but no lookup tables"); 7617 7618 DeclsMap Decls; 7619 7620 for (DeclID ID : It->second.Table.findAll()) { 7621 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7622 Decls[ND->getDeclName()].push_back(ND); 7623 } 7624 7625 ++NumVisibleDeclContextsRead; 7626 7627 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7628 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7629 } 7630 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7631 } 7632 7633 const serialization::reader::DeclContextLookupTable * 7634 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7635 auto I = Lookups.find(Primary); 7636 return I == Lookups.end() ? nullptr : &I->second; 7637 } 7638 7639 /// Under non-PCH compilation the consumer receives the objc methods 7640 /// before receiving the implementation, and codegen depends on this. 7641 /// We simulate this by deserializing and passing to consumer the methods of the 7642 /// implementation before passing the deserialized implementation decl. 7643 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7644 ASTConsumer *Consumer) { 7645 assert(ImplD && Consumer); 7646 7647 for (auto *I : ImplD->methods()) 7648 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7649 7650 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7651 } 7652 7653 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7654 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7655 PassObjCImplDeclToConsumer(ImplD, Consumer); 7656 else 7657 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7658 } 7659 7660 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7661 this->Consumer = Consumer; 7662 7663 if (Consumer) 7664 PassInterestingDeclsToConsumer(); 7665 7666 if (DeserializationListener) 7667 DeserializationListener->ReaderInitialized(this); 7668 } 7669 7670 void ASTReader::PrintStats() { 7671 std::fprintf(stderr, "*** AST File Statistics:\n"); 7672 7673 unsigned NumTypesLoaded 7674 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7675 QualType()); 7676 unsigned NumDeclsLoaded 7677 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7678 (Decl *)nullptr); 7679 unsigned NumIdentifiersLoaded 7680 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7681 IdentifiersLoaded.end(), 7682 (IdentifierInfo *)nullptr); 7683 unsigned NumMacrosLoaded 7684 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7685 MacrosLoaded.end(), 7686 (MacroInfo *)nullptr); 7687 unsigned NumSelectorsLoaded 7688 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7689 SelectorsLoaded.end(), 7690 Selector()); 7691 7692 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7693 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7694 NumSLocEntriesRead, TotalNumSLocEntries, 7695 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7696 if (!TypesLoaded.empty()) 7697 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7698 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7699 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7700 if (!DeclsLoaded.empty()) 7701 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7702 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7703 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7704 if (!IdentifiersLoaded.empty()) 7705 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7706 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7707 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7708 if (!MacrosLoaded.empty()) 7709 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7710 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7711 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7712 if (!SelectorsLoaded.empty()) 7713 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7714 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7715 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7716 if (TotalNumStatements) 7717 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7718 NumStatementsRead, TotalNumStatements, 7719 ((float)NumStatementsRead/TotalNumStatements * 100)); 7720 if (TotalNumMacros) 7721 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7722 NumMacrosRead, TotalNumMacros, 7723 ((float)NumMacrosRead/TotalNumMacros * 100)); 7724 if (TotalLexicalDeclContexts) 7725 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7726 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7727 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7728 * 100)); 7729 if (TotalVisibleDeclContexts) 7730 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7731 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7732 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7733 * 100)); 7734 if (TotalNumMethodPoolEntries) 7735 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7736 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7737 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7738 * 100)); 7739 if (NumMethodPoolLookups) 7740 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7741 NumMethodPoolHits, NumMethodPoolLookups, 7742 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7743 if (NumMethodPoolTableLookups) 7744 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7745 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7746 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7747 * 100.0)); 7748 if (NumIdentifierLookupHits) 7749 std::fprintf(stderr, 7750 " %u / %u identifier table lookups succeeded (%f%%)\n", 7751 NumIdentifierLookupHits, NumIdentifierLookups, 7752 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7753 7754 if (GlobalIndex) { 7755 std::fprintf(stderr, "\n"); 7756 GlobalIndex->printStats(); 7757 } 7758 7759 std::fprintf(stderr, "\n"); 7760 dump(); 7761 std::fprintf(stderr, "\n"); 7762 } 7763 7764 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7765 LLVM_DUMP_METHOD static void 7766 dumpModuleIDMap(StringRef Name, 7767 const ContinuousRangeMap<Key, ModuleFile *, 7768 InitialCapacity> &Map) { 7769 if (Map.begin() == Map.end()) 7770 return; 7771 7772 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7773 7774 llvm::errs() << Name << ":\n"; 7775 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7776 I != IEnd; ++I) { 7777 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7778 << "\n"; 7779 } 7780 } 7781 7782 LLVM_DUMP_METHOD void ASTReader::dump() { 7783 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7784 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7785 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7786 dumpModuleIDMap("Global type map", GlobalTypeMap); 7787 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7788 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7789 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7790 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7791 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7792 dumpModuleIDMap("Global preprocessed entity map", 7793 GlobalPreprocessedEntityMap); 7794 7795 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7796 for (ModuleFile &M : ModuleMgr) 7797 M.dump(); 7798 } 7799 7800 /// Return the amount of memory used by memory buffers, breaking down 7801 /// by heap-backed versus mmap'ed memory. 7802 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7803 for (ModuleFile &I : ModuleMgr) { 7804 if (llvm::MemoryBuffer *buf = I.Buffer) { 7805 size_t bytes = buf->getBufferSize(); 7806 switch (buf->getBufferKind()) { 7807 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7808 sizes.malloc_bytes += bytes; 7809 break; 7810 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7811 sizes.mmap_bytes += bytes; 7812 break; 7813 } 7814 } 7815 } 7816 } 7817 7818 void ASTReader::InitializeSema(Sema &S) { 7819 SemaObj = &S; 7820 S.addExternalSource(this); 7821 7822 // Makes sure any declarations that were deserialized "too early" 7823 // still get added to the identifier's declaration chains. 7824 for (uint64_t ID : PreloadedDeclIDs) { 7825 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7826 pushExternalDeclIntoScope(D, D->getDeclName()); 7827 } 7828 PreloadedDeclIDs.clear(); 7829 7830 // FIXME: What happens if these are changed by a module import? 7831 if (!FPPragmaOptions.empty()) { 7832 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7833 FPOptionsOverride NewOverrides = 7834 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7835 SemaObj->CurFPFeatures = 7836 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7837 } 7838 7839 SemaObj->OpenCLFeatures.copy(OpenCLExtensions); 7840 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7841 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7842 7843 UpdateSema(); 7844 } 7845 7846 void ASTReader::UpdateSema() { 7847 assert(SemaObj && "no Sema to update"); 7848 7849 // Load the offsets of the declarations that Sema references. 7850 // They will be lazily deserialized when needed. 7851 if (!SemaDeclRefs.empty()) { 7852 assert(SemaDeclRefs.size() % 3 == 0); 7853 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7854 if (!SemaObj->StdNamespace) 7855 SemaObj->StdNamespace = SemaDeclRefs[I]; 7856 if (!SemaObj->StdBadAlloc) 7857 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7858 if (!SemaObj->StdAlignValT) 7859 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7860 } 7861 SemaDeclRefs.clear(); 7862 } 7863 7864 // Update the state of pragmas. Use the same API as if we had encountered the 7865 // pragma in the source. 7866 if(OptimizeOffPragmaLocation.isValid()) 7867 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7868 if (PragmaMSStructState != -1) 7869 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7870 if (PointersToMembersPragmaLocation.isValid()) { 7871 SemaObj->ActOnPragmaMSPointersToMembers( 7872 (LangOptions::PragmaMSPointersToMembersKind) 7873 PragmaMSPointersToMembersState, 7874 PointersToMembersPragmaLocation); 7875 } 7876 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7877 7878 if (PragmaPackCurrentValue) { 7879 // The bottom of the stack might have a default value. It must be adjusted 7880 // to the current value to ensure that the packing state is preserved after 7881 // popping entries that were included/imported from a PCH/module. 7882 bool DropFirst = false; 7883 if (!PragmaPackStack.empty() && 7884 PragmaPackStack.front().Location.isInvalid()) { 7885 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue && 7886 "Expected a default alignment value"); 7887 SemaObj->PackStack.Stack.emplace_back( 7888 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue, 7889 SemaObj->PackStack.CurrentPragmaLocation, 7890 PragmaPackStack.front().PushLocation); 7891 DropFirst = true; 7892 } 7893 for (const auto &Entry : 7894 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0)) 7895 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value, 7896 Entry.Location, Entry.PushLocation); 7897 if (PragmaPackCurrentLocation.isInvalid()) { 7898 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue && 7899 "Expected a default alignment value"); 7900 // Keep the current values. 7901 } else { 7902 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue; 7903 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation; 7904 } 7905 } 7906 if (FpPragmaCurrentValue) { 7907 // The bottom of the stack might have a default value. It must be adjusted 7908 // to the current value to ensure that fp-pragma state is preserved after 7909 // popping entries that were included/imported from a PCH/module. 7910 bool DropFirst = false; 7911 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7912 assert(FpPragmaStack.front().Value == 7913 SemaObj->FpPragmaStack.DefaultValue && 7914 "Expected a default pragma float_control value"); 7915 SemaObj->FpPragmaStack.Stack.emplace_back( 7916 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7917 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7918 FpPragmaStack.front().PushLocation); 7919 DropFirst = true; 7920 } 7921 for (const auto &Entry : 7922 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7923 SemaObj->FpPragmaStack.Stack.emplace_back( 7924 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7925 if (FpPragmaCurrentLocation.isInvalid()) { 7926 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7927 "Expected a default pragma float_control value"); 7928 // Keep the current values. 7929 } else { 7930 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7931 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7932 } 7933 } 7934 7935 // For non-modular AST files, restore visiblity of modules. 7936 for (auto &Import : ImportedModules) { 7937 if (Import.ImportLoc.isInvalid()) 7938 continue; 7939 if (Module *Imported = getSubmodule(Import.ID)) { 7940 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 7941 } 7942 } 7943 } 7944 7945 IdentifierInfo *ASTReader::get(StringRef Name) { 7946 // Note that we are loading an identifier. 7947 Deserializing AnIdentifier(this); 7948 7949 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7950 NumIdentifierLookups, 7951 NumIdentifierLookupHits); 7952 7953 // We don't need to do identifier table lookups in C++ modules (we preload 7954 // all interesting declarations, and don't need to use the scope for name 7955 // lookups). Perform the lookup in PCH files, though, since we don't build 7956 // a complete initial identifier table if we're carrying on from a PCH. 7957 if (PP.getLangOpts().CPlusPlus) { 7958 for (auto F : ModuleMgr.pch_modules()) 7959 if (Visitor(*F)) 7960 break; 7961 } else { 7962 // If there is a global index, look there first to determine which modules 7963 // provably do not have any results for this identifier. 7964 GlobalModuleIndex::HitSet Hits; 7965 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7966 if (!loadGlobalIndex()) { 7967 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7968 HitsPtr = &Hits; 7969 } 7970 } 7971 7972 ModuleMgr.visit(Visitor, HitsPtr); 7973 } 7974 7975 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7976 markIdentifierUpToDate(II); 7977 return II; 7978 } 7979 7980 namespace clang { 7981 7982 /// An identifier-lookup iterator that enumerates all of the 7983 /// identifiers stored within a set of AST files. 7984 class ASTIdentifierIterator : public IdentifierIterator { 7985 /// The AST reader whose identifiers are being enumerated. 7986 const ASTReader &Reader; 7987 7988 /// The current index into the chain of AST files stored in 7989 /// the AST reader. 7990 unsigned Index; 7991 7992 /// The current position within the identifier lookup table 7993 /// of the current AST file. 7994 ASTIdentifierLookupTable::key_iterator Current; 7995 7996 /// The end position within the identifier lookup table of 7997 /// the current AST file. 7998 ASTIdentifierLookupTable::key_iterator End; 7999 8000 /// Whether to skip any modules in the ASTReader. 8001 bool SkipModules; 8002 8003 public: 8004 explicit ASTIdentifierIterator(const ASTReader &Reader, 8005 bool SkipModules = false); 8006 8007 StringRef Next() override; 8008 }; 8009 8010 } // namespace clang 8011 8012 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8013 bool SkipModules) 8014 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8015 } 8016 8017 StringRef ASTIdentifierIterator::Next() { 8018 while (Current == End) { 8019 // If we have exhausted all of our AST files, we're done. 8020 if (Index == 0) 8021 return StringRef(); 8022 8023 --Index; 8024 ModuleFile &F = Reader.ModuleMgr[Index]; 8025 if (SkipModules && F.isModule()) 8026 continue; 8027 8028 ASTIdentifierLookupTable *IdTable = 8029 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8030 Current = IdTable->key_begin(); 8031 End = IdTable->key_end(); 8032 } 8033 8034 // We have any identifiers remaining in the current AST file; return 8035 // the next one. 8036 StringRef Result = *Current; 8037 ++Current; 8038 return Result; 8039 } 8040 8041 namespace { 8042 8043 /// A utility for appending two IdentifierIterators. 8044 class ChainedIdentifierIterator : public IdentifierIterator { 8045 std::unique_ptr<IdentifierIterator> Current; 8046 std::unique_ptr<IdentifierIterator> Queued; 8047 8048 public: 8049 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8050 std::unique_ptr<IdentifierIterator> Second) 8051 : Current(std::move(First)), Queued(std::move(Second)) {} 8052 8053 StringRef Next() override { 8054 if (!Current) 8055 return StringRef(); 8056 8057 StringRef result = Current->Next(); 8058 if (!result.empty()) 8059 return result; 8060 8061 // Try the queued iterator, which may itself be empty. 8062 Current.reset(); 8063 std::swap(Current, Queued); 8064 return Next(); 8065 } 8066 }; 8067 8068 } // namespace 8069 8070 IdentifierIterator *ASTReader::getIdentifiers() { 8071 if (!loadGlobalIndex()) { 8072 std::unique_ptr<IdentifierIterator> ReaderIter( 8073 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8074 std::unique_ptr<IdentifierIterator> ModulesIter( 8075 GlobalIndex->createIdentifierIterator()); 8076 return new ChainedIdentifierIterator(std::move(ReaderIter), 8077 std::move(ModulesIter)); 8078 } 8079 8080 return new ASTIdentifierIterator(*this); 8081 } 8082 8083 namespace clang { 8084 namespace serialization { 8085 8086 class ReadMethodPoolVisitor { 8087 ASTReader &Reader; 8088 Selector Sel; 8089 unsigned PriorGeneration; 8090 unsigned InstanceBits = 0; 8091 unsigned FactoryBits = 0; 8092 bool InstanceHasMoreThanOneDecl = false; 8093 bool FactoryHasMoreThanOneDecl = false; 8094 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8095 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8096 8097 public: 8098 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8099 unsigned PriorGeneration) 8100 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8101 8102 bool operator()(ModuleFile &M) { 8103 if (!M.SelectorLookupTable) 8104 return false; 8105 8106 // If we've already searched this module file, skip it now. 8107 if (M.Generation <= PriorGeneration) 8108 return true; 8109 8110 ++Reader.NumMethodPoolTableLookups; 8111 ASTSelectorLookupTable *PoolTable 8112 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8113 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8114 if (Pos == PoolTable->end()) 8115 return false; 8116 8117 ++Reader.NumMethodPoolTableHits; 8118 ++Reader.NumSelectorsRead; 8119 // FIXME: Not quite happy with the statistics here. We probably should 8120 // disable this tracking when called via LoadSelector. 8121 // Also, should entries without methods count as misses? 8122 ++Reader.NumMethodPoolEntriesRead; 8123 ASTSelectorLookupTrait::data_type Data = *Pos; 8124 if (Reader.DeserializationListener) 8125 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8126 8127 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8128 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8129 InstanceBits = Data.InstanceBits; 8130 FactoryBits = Data.FactoryBits; 8131 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8132 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8133 return true; 8134 } 8135 8136 /// Retrieve the instance methods found by this visitor. 8137 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8138 return InstanceMethods; 8139 } 8140 8141 /// Retrieve the instance methods found by this visitor. 8142 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8143 return FactoryMethods; 8144 } 8145 8146 unsigned getInstanceBits() const { return InstanceBits; } 8147 unsigned getFactoryBits() const { return FactoryBits; } 8148 8149 bool instanceHasMoreThanOneDecl() const { 8150 return InstanceHasMoreThanOneDecl; 8151 } 8152 8153 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8154 }; 8155 8156 } // namespace serialization 8157 } // namespace clang 8158 8159 /// Add the given set of methods to the method list. 8160 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8161 ObjCMethodList &List) { 8162 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8163 S.addMethodToGlobalList(&List, Methods[I]); 8164 } 8165 } 8166 8167 void ASTReader::ReadMethodPool(Selector Sel) { 8168 // Get the selector generation and update it to the current generation. 8169 unsigned &Generation = SelectorGeneration[Sel]; 8170 unsigned PriorGeneration = Generation; 8171 Generation = getGeneration(); 8172 SelectorOutOfDate[Sel] = false; 8173 8174 // Search for methods defined with this selector. 8175 ++NumMethodPoolLookups; 8176 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8177 ModuleMgr.visit(Visitor); 8178 8179 if (Visitor.getInstanceMethods().empty() && 8180 Visitor.getFactoryMethods().empty()) 8181 return; 8182 8183 ++NumMethodPoolHits; 8184 8185 if (!getSema()) 8186 return; 8187 8188 Sema &S = *getSema(); 8189 Sema::GlobalMethodPool::iterator Pos 8190 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8191 8192 Pos->second.first.setBits(Visitor.getInstanceBits()); 8193 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8194 Pos->second.second.setBits(Visitor.getFactoryBits()); 8195 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8196 8197 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8198 // when building a module we keep every method individually and may need to 8199 // update hasMoreThanOneDecl as we add the methods. 8200 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8201 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8202 } 8203 8204 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8205 if (SelectorOutOfDate[Sel]) 8206 ReadMethodPool(Sel); 8207 } 8208 8209 void ASTReader::ReadKnownNamespaces( 8210 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8211 Namespaces.clear(); 8212 8213 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8214 if (NamespaceDecl *Namespace 8215 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8216 Namespaces.push_back(Namespace); 8217 } 8218 } 8219 8220 void ASTReader::ReadUndefinedButUsed( 8221 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8222 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8223 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8224 SourceLocation Loc = 8225 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8226 Undefined.insert(std::make_pair(D, Loc)); 8227 } 8228 } 8229 8230 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8231 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8232 Exprs) { 8233 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8234 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8235 uint64_t Count = DelayedDeleteExprs[Idx++]; 8236 for (uint64_t C = 0; C < Count; ++C) { 8237 SourceLocation DeleteLoc = 8238 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8239 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8240 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8241 } 8242 } 8243 } 8244 8245 void ASTReader::ReadTentativeDefinitions( 8246 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8247 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8248 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8249 if (Var) 8250 TentativeDefs.push_back(Var); 8251 } 8252 TentativeDefinitions.clear(); 8253 } 8254 8255 void ASTReader::ReadUnusedFileScopedDecls( 8256 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8257 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8258 DeclaratorDecl *D 8259 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8260 if (D) 8261 Decls.push_back(D); 8262 } 8263 UnusedFileScopedDecls.clear(); 8264 } 8265 8266 void ASTReader::ReadDelegatingConstructors( 8267 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8268 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8269 CXXConstructorDecl *D 8270 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8271 if (D) 8272 Decls.push_back(D); 8273 } 8274 DelegatingCtorDecls.clear(); 8275 } 8276 8277 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8278 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8279 TypedefNameDecl *D 8280 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8281 if (D) 8282 Decls.push_back(D); 8283 } 8284 ExtVectorDecls.clear(); 8285 } 8286 8287 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8288 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8289 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8290 ++I) { 8291 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8292 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8293 if (D) 8294 Decls.insert(D); 8295 } 8296 UnusedLocalTypedefNameCandidates.clear(); 8297 } 8298 8299 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8300 llvm::SmallVector<Decl *, 4> &Decls) { 8301 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N; 8302 ++I) { 8303 auto *D = dyn_cast_or_null<Decl>( 8304 GetDecl(DeclsToCheckForDeferredDiags[I])); 8305 if (D) 8306 Decls.push_back(D); 8307 } 8308 DeclsToCheckForDeferredDiags.clear(); 8309 } 8310 8311 8312 void ASTReader::ReadReferencedSelectors( 8313 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8314 if (ReferencedSelectorsData.empty()) 8315 return; 8316 8317 // If there are @selector references added them to its pool. This is for 8318 // implementation of -Wselector. 8319 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8320 unsigned I = 0; 8321 while (I < DataSize) { 8322 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8323 SourceLocation SelLoc 8324 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8325 Sels.push_back(std::make_pair(Sel, SelLoc)); 8326 } 8327 ReferencedSelectorsData.clear(); 8328 } 8329 8330 void ASTReader::ReadWeakUndeclaredIdentifiers( 8331 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8332 if (WeakUndeclaredIdentifiers.empty()) 8333 return; 8334 8335 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8336 IdentifierInfo *WeakId 8337 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8338 IdentifierInfo *AliasId 8339 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8340 SourceLocation Loc 8341 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8342 bool Used = WeakUndeclaredIdentifiers[I++]; 8343 WeakInfo WI(AliasId, Loc); 8344 WI.setUsed(Used); 8345 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8346 } 8347 WeakUndeclaredIdentifiers.clear(); 8348 } 8349 8350 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8351 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8352 ExternalVTableUse VT; 8353 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8354 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8355 VT.DefinitionRequired = VTableUses[Idx++]; 8356 VTables.push_back(VT); 8357 } 8358 8359 VTableUses.clear(); 8360 } 8361 8362 void ASTReader::ReadPendingInstantiations( 8363 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8364 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8365 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8366 SourceLocation Loc 8367 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8368 8369 Pending.push_back(std::make_pair(D, Loc)); 8370 } 8371 PendingInstantiations.clear(); 8372 } 8373 8374 void ASTReader::ReadLateParsedTemplates( 8375 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8376 &LPTMap) { 8377 for (auto &LPT : LateParsedTemplates) { 8378 ModuleFile *FMod = LPT.first; 8379 RecordDataImpl &LateParsed = LPT.second; 8380 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8381 /* In loop */) { 8382 FunctionDecl *FD = 8383 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8384 8385 auto LT = std::make_unique<LateParsedTemplate>(); 8386 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8387 8388 ModuleFile *F = getOwningModuleFile(LT->D); 8389 assert(F && "No module"); 8390 8391 unsigned TokN = LateParsed[Idx++]; 8392 LT->Toks.reserve(TokN); 8393 for (unsigned T = 0; T < TokN; ++T) 8394 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8395 8396 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8397 } 8398 } 8399 } 8400 8401 void ASTReader::LoadSelector(Selector Sel) { 8402 // It would be complicated to avoid reading the methods anyway. So don't. 8403 ReadMethodPool(Sel); 8404 } 8405 8406 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8407 assert(ID && "Non-zero identifier ID required"); 8408 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8409 IdentifiersLoaded[ID - 1] = II; 8410 if (DeserializationListener) 8411 DeserializationListener->IdentifierRead(ID, II); 8412 } 8413 8414 /// Set the globally-visible declarations associated with the given 8415 /// identifier. 8416 /// 8417 /// If the AST reader is currently in a state where the given declaration IDs 8418 /// cannot safely be resolved, they are queued until it is safe to resolve 8419 /// them. 8420 /// 8421 /// \param II an IdentifierInfo that refers to one or more globally-visible 8422 /// declarations. 8423 /// 8424 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8425 /// visible at global scope. 8426 /// 8427 /// \param Decls if non-null, this vector will be populated with the set of 8428 /// deserialized declarations. These declarations will not be pushed into 8429 /// scope. 8430 void 8431 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8432 const SmallVectorImpl<uint32_t> &DeclIDs, 8433 SmallVectorImpl<Decl *> *Decls) { 8434 if (NumCurrentElementsDeserializing && !Decls) { 8435 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8436 return; 8437 } 8438 8439 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8440 if (!SemaObj) { 8441 // Queue this declaration so that it will be added to the 8442 // translation unit scope and identifier's declaration chain 8443 // once a Sema object is known. 8444 PreloadedDeclIDs.push_back(DeclIDs[I]); 8445 continue; 8446 } 8447 8448 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8449 8450 // If we're simply supposed to record the declarations, do so now. 8451 if (Decls) { 8452 Decls->push_back(D); 8453 continue; 8454 } 8455 8456 // Introduce this declaration into the translation-unit scope 8457 // and add it to the declaration chain for this identifier, so 8458 // that (unqualified) name lookup will find it. 8459 pushExternalDeclIntoScope(D, II); 8460 } 8461 } 8462 8463 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8464 if (ID == 0) 8465 return nullptr; 8466 8467 if (IdentifiersLoaded.empty()) { 8468 Error("no identifier table in AST file"); 8469 return nullptr; 8470 } 8471 8472 ID -= 1; 8473 if (!IdentifiersLoaded[ID]) { 8474 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8475 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8476 ModuleFile *M = I->second; 8477 unsigned Index = ID - M->BaseIdentifierID; 8478 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8479 8480 // All of the strings in the AST file are preceded by a 16-bit length. 8481 // Extract that 16-bit length to avoid having to execute strlen(). 8482 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8483 // unsigned integers. This is important to avoid integer overflow when 8484 // we cast them to 'unsigned'. 8485 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8486 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8487 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8488 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8489 IdentifiersLoaded[ID] = &II; 8490 markIdentifierFromAST(*this, II); 8491 if (DeserializationListener) 8492 DeserializationListener->IdentifierRead(ID + 1, &II); 8493 } 8494 8495 return IdentifiersLoaded[ID]; 8496 } 8497 8498 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8499 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8500 } 8501 8502 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8503 if (LocalID < NUM_PREDEF_IDENT_IDS) 8504 return LocalID; 8505 8506 if (!M.ModuleOffsetMap.empty()) 8507 ReadModuleOffsetMap(M); 8508 8509 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8510 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8511 assert(I != M.IdentifierRemap.end() 8512 && "Invalid index into identifier index remap"); 8513 8514 return LocalID + I->second; 8515 } 8516 8517 MacroInfo *ASTReader::getMacro(MacroID ID) { 8518 if (ID == 0) 8519 return nullptr; 8520 8521 if (MacrosLoaded.empty()) { 8522 Error("no macro table in AST file"); 8523 return nullptr; 8524 } 8525 8526 ID -= NUM_PREDEF_MACRO_IDS; 8527 if (!MacrosLoaded[ID]) { 8528 GlobalMacroMapType::iterator I 8529 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8530 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8531 ModuleFile *M = I->second; 8532 unsigned Index = ID - M->BaseMacroID; 8533 MacrosLoaded[ID] = 8534 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8535 8536 if (DeserializationListener) 8537 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8538 MacrosLoaded[ID]); 8539 } 8540 8541 return MacrosLoaded[ID]; 8542 } 8543 8544 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8545 if (LocalID < NUM_PREDEF_MACRO_IDS) 8546 return LocalID; 8547 8548 if (!M.ModuleOffsetMap.empty()) 8549 ReadModuleOffsetMap(M); 8550 8551 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8552 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8553 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8554 8555 return LocalID + I->second; 8556 } 8557 8558 serialization::SubmoduleID 8559 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8560 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8561 return LocalID; 8562 8563 if (!M.ModuleOffsetMap.empty()) 8564 ReadModuleOffsetMap(M); 8565 8566 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8567 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8568 assert(I != M.SubmoduleRemap.end() 8569 && "Invalid index into submodule index remap"); 8570 8571 return LocalID + I->second; 8572 } 8573 8574 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8575 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8576 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8577 return nullptr; 8578 } 8579 8580 if (GlobalID > SubmodulesLoaded.size()) { 8581 Error("submodule ID out of range in AST file"); 8582 return nullptr; 8583 } 8584 8585 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8586 } 8587 8588 Module *ASTReader::getModule(unsigned ID) { 8589 return getSubmodule(ID); 8590 } 8591 8592 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8593 if (ID & 1) { 8594 // It's a module, look it up by submodule ID. 8595 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8596 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8597 } else { 8598 // It's a prefix (preamble, PCH, ...). Look it up by index. 8599 unsigned IndexFromEnd = ID >> 1; 8600 assert(IndexFromEnd && "got reference to unknown module file"); 8601 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8602 } 8603 } 8604 8605 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8606 if (!F) 8607 return 1; 8608 8609 // For a file representing a module, use the submodule ID of the top-level 8610 // module as the file ID. For any other kind of file, the number of such 8611 // files loaded beforehand will be the same on reload. 8612 // FIXME: Is this true even if we have an explicit module file and a PCH? 8613 if (F->isModule()) 8614 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8615 8616 auto PCHModules = getModuleManager().pch_modules(); 8617 auto I = llvm::find(PCHModules, F); 8618 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8619 return (I - PCHModules.end()) << 1; 8620 } 8621 8622 llvm::Optional<ASTSourceDescriptor> 8623 ASTReader::getSourceDescriptor(unsigned ID) { 8624 if (Module *M = getSubmodule(ID)) 8625 return ASTSourceDescriptor(*M); 8626 8627 // If there is only a single PCH, return it instead. 8628 // Chained PCH are not supported. 8629 const auto &PCHChain = ModuleMgr.pch_modules(); 8630 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8631 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8632 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8633 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8634 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8635 MF.Signature); 8636 } 8637 return None; 8638 } 8639 8640 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8641 auto I = DefinitionSource.find(FD); 8642 if (I == DefinitionSource.end()) 8643 return EK_ReplyHazy; 8644 return I->second ? EK_Never : EK_Always; 8645 } 8646 8647 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8648 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8649 } 8650 8651 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8652 if (ID == 0) 8653 return Selector(); 8654 8655 if (ID > SelectorsLoaded.size()) { 8656 Error("selector ID out of range in AST file"); 8657 return Selector(); 8658 } 8659 8660 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8661 // Load this selector from the selector table. 8662 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8663 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8664 ModuleFile &M = *I->second; 8665 ASTSelectorLookupTrait Trait(*this, M); 8666 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8667 SelectorsLoaded[ID - 1] = 8668 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8669 if (DeserializationListener) 8670 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8671 } 8672 8673 return SelectorsLoaded[ID - 1]; 8674 } 8675 8676 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8677 return DecodeSelector(ID); 8678 } 8679 8680 uint32_t ASTReader::GetNumExternalSelectors() { 8681 // ID 0 (the null selector) is considered an external selector. 8682 return getTotalNumSelectors() + 1; 8683 } 8684 8685 serialization::SelectorID 8686 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8687 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8688 return LocalID; 8689 8690 if (!M.ModuleOffsetMap.empty()) 8691 ReadModuleOffsetMap(M); 8692 8693 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8694 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8695 assert(I != M.SelectorRemap.end() 8696 && "Invalid index into selector index remap"); 8697 8698 return LocalID + I->second; 8699 } 8700 8701 DeclarationNameLoc 8702 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8703 DeclarationNameLoc DNLoc; 8704 switch (Name.getNameKind()) { 8705 case DeclarationName::CXXConstructorName: 8706 case DeclarationName::CXXDestructorName: 8707 case DeclarationName::CXXConversionFunctionName: 8708 DNLoc.NamedType.TInfo = readTypeSourceInfo(); 8709 break; 8710 8711 case DeclarationName::CXXOperatorName: 8712 DNLoc.CXXOperatorName.BeginOpNameLoc 8713 = readSourceLocation().getRawEncoding(); 8714 DNLoc.CXXOperatorName.EndOpNameLoc 8715 = readSourceLocation().getRawEncoding(); 8716 break; 8717 8718 case DeclarationName::CXXLiteralOperatorName: 8719 DNLoc.CXXLiteralOperatorName.OpNameLoc 8720 = readSourceLocation().getRawEncoding(); 8721 break; 8722 8723 case DeclarationName::Identifier: 8724 case DeclarationName::ObjCZeroArgSelector: 8725 case DeclarationName::ObjCOneArgSelector: 8726 case DeclarationName::ObjCMultiArgSelector: 8727 case DeclarationName::CXXUsingDirective: 8728 case DeclarationName::CXXDeductionGuideName: 8729 break; 8730 } 8731 return DNLoc; 8732 } 8733 8734 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8735 DeclarationNameInfo NameInfo; 8736 NameInfo.setName(readDeclarationName()); 8737 NameInfo.setLoc(readSourceLocation()); 8738 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8739 return NameInfo; 8740 } 8741 8742 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8743 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8744 unsigned NumTPLists = readInt(); 8745 Info.NumTemplParamLists = NumTPLists; 8746 if (NumTPLists) { 8747 Info.TemplParamLists = 8748 new (getContext()) TemplateParameterList *[NumTPLists]; 8749 for (unsigned i = 0; i != NumTPLists; ++i) 8750 Info.TemplParamLists[i] = readTemplateParameterList(); 8751 } 8752 } 8753 8754 TemplateParameterList * 8755 ASTRecordReader::readTemplateParameterList() { 8756 SourceLocation TemplateLoc = readSourceLocation(); 8757 SourceLocation LAngleLoc = readSourceLocation(); 8758 SourceLocation RAngleLoc = readSourceLocation(); 8759 8760 unsigned NumParams = readInt(); 8761 SmallVector<NamedDecl *, 16> Params; 8762 Params.reserve(NumParams); 8763 while (NumParams--) 8764 Params.push_back(readDeclAs<NamedDecl>()); 8765 8766 bool HasRequiresClause = readBool(); 8767 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8768 8769 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8770 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8771 return TemplateParams; 8772 } 8773 8774 void ASTRecordReader::readTemplateArgumentList( 8775 SmallVectorImpl<TemplateArgument> &TemplArgs, 8776 bool Canonicalize) { 8777 unsigned NumTemplateArgs = readInt(); 8778 TemplArgs.reserve(NumTemplateArgs); 8779 while (NumTemplateArgs--) 8780 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8781 } 8782 8783 /// Read a UnresolvedSet structure. 8784 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8785 unsigned NumDecls = readInt(); 8786 Set.reserve(getContext(), NumDecls); 8787 while (NumDecls--) { 8788 DeclID ID = readDeclID(); 8789 AccessSpecifier AS = (AccessSpecifier) readInt(); 8790 Set.addLazyDecl(getContext(), ID, AS); 8791 } 8792 } 8793 8794 CXXBaseSpecifier 8795 ASTRecordReader::readCXXBaseSpecifier() { 8796 bool isVirtual = readBool(); 8797 bool isBaseOfClass = readBool(); 8798 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8799 bool inheritConstructors = readBool(); 8800 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8801 SourceRange Range = readSourceRange(); 8802 SourceLocation EllipsisLoc = readSourceLocation(); 8803 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8804 EllipsisLoc); 8805 Result.setInheritConstructors(inheritConstructors); 8806 return Result; 8807 } 8808 8809 CXXCtorInitializer ** 8810 ASTRecordReader::readCXXCtorInitializers() { 8811 ASTContext &Context = getContext(); 8812 unsigned NumInitializers = readInt(); 8813 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8814 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8815 for (unsigned i = 0; i != NumInitializers; ++i) { 8816 TypeSourceInfo *TInfo = nullptr; 8817 bool IsBaseVirtual = false; 8818 FieldDecl *Member = nullptr; 8819 IndirectFieldDecl *IndirectMember = nullptr; 8820 8821 CtorInitializerType Type = (CtorInitializerType) readInt(); 8822 switch (Type) { 8823 case CTOR_INITIALIZER_BASE: 8824 TInfo = readTypeSourceInfo(); 8825 IsBaseVirtual = readBool(); 8826 break; 8827 8828 case CTOR_INITIALIZER_DELEGATING: 8829 TInfo = readTypeSourceInfo(); 8830 break; 8831 8832 case CTOR_INITIALIZER_MEMBER: 8833 Member = readDeclAs<FieldDecl>(); 8834 break; 8835 8836 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8837 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8838 break; 8839 } 8840 8841 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8842 Expr *Init = readExpr(); 8843 SourceLocation LParenLoc = readSourceLocation(); 8844 SourceLocation RParenLoc = readSourceLocation(); 8845 8846 CXXCtorInitializer *BOMInit; 8847 if (Type == CTOR_INITIALIZER_BASE) 8848 BOMInit = new (Context) 8849 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8850 RParenLoc, MemberOrEllipsisLoc); 8851 else if (Type == CTOR_INITIALIZER_DELEGATING) 8852 BOMInit = new (Context) 8853 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8854 else if (Member) 8855 BOMInit = new (Context) 8856 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8857 Init, RParenLoc); 8858 else 8859 BOMInit = new (Context) 8860 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8861 LParenLoc, Init, RParenLoc); 8862 8863 if (/*IsWritten*/readBool()) { 8864 unsigned SourceOrder = readInt(); 8865 BOMInit->setSourceOrder(SourceOrder); 8866 } 8867 8868 CtorInitializers[i] = BOMInit; 8869 } 8870 8871 return CtorInitializers; 8872 } 8873 8874 NestedNameSpecifierLoc 8875 ASTRecordReader::readNestedNameSpecifierLoc() { 8876 ASTContext &Context = getContext(); 8877 unsigned N = readInt(); 8878 NestedNameSpecifierLocBuilder Builder; 8879 for (unsigned I = 0; I != N; ++I) { 8880 auto Kind = readNestedNameSpecifierKind(); 8881 switch (Kind) { 8882 case NestedNameSpecifier::Identifier: { 8883 IdentifierInfo *II = readIdentifier(); 8884 SourceRange Range = readSourceRange(); 8885 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8886 break; 8887 } 8888 8889 case NestedNameSpecifier::Namespace: { 8890 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8891 SourceRange Range = readSourceRange(); 8892 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8893 break; 8894 } 8895 8896 case NestedNameSpecifier::NamespaceAlias: { 8897 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8898 SourceRange Range = readSourceRange(); 8899 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8900 break; 8901 } 8902 8903 case NestedNameSpecifier::TypeSpec: 8904 case NestedNameSpecifier::TypeSpecWithTemplate: { 8905 bool Template = readBool(); 8906 TypeSourceInfo *T = readTypeSourceInfo(); 8907 if (!T) 8908 return NestedNameSpecifierLoc(); 8909 SourceLocation ColonColonLoc = readSourceLocation(); 8910 8911 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8912 Builder.Extend(Context, 8913 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8914 T->getTypeLoc(), ColonColonLoc); 8915 break; 8916 } 8917 8918 case NestedNameSpecifier::Global: { 8919 SourceLocation ColonColonLoc = readSourceLocation(); 8920 Builder.MakeGlobal(Context, ColonColonLoc); 8921 break; 8922 } 8923 8924 case NestedNameSpecifier::Super: { 8925 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8926 SourceRange Range = readSourceRange(); 8927 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8928 break; 8929 } 8930 } 8931 } 8932 8933 return Builder.getWithLocInContext(Context); 8934 } 8935 8936 SourceRange 8937 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8938 unsigned &Idx) { 8939 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8940 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8941 return SourceRange(beg, end); 8942 } 8943 8944 /// Read a floating-point value 8945 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 8946 return llvm::APFloat(Sem, readAPInt()); 8947 } 8948 8949 // Read a string 8950 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 8951 unsigned Len = Record[Idx++]; 8952 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 8953 Idx += Len; 8954 return Result; 8955 } 8956 8957 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 8958 unsigned &Idx) { 8959 std::string Filename = ReadString(Record, Idx); 8960 ResolveImportedPath(F, Filename); 8961 return Filename; 8962 } 8963 8964 std::string ASTReader::ReadPath(StringRef BaseDirectory, 8965 const RecordData &Record, unsigned &Idx) { 8966 std::string Filename = ReadString(Record, Idx); 8967 if (!BaseDirectory.empty()) 8968 ResolveImportedPath(Filename, BaseDirectory); 8969 return Filename; 8970 } 8971 8972 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 8973 unsigned &Idx) { 8974 unsigned Major = Record[Idx++]; 8975 unsigned Minor = Record[Idx++]; 8976 unsigned Subminor = Record[Idx++]; 8977 if (Minor == 0) 8978 return VersionTuple(Major); 8979 if (Subminor == 0) 8980 return VersionTuple(Major, Minor - 1); 8981 return VersionTuple(Major, Minor - 1, Subminor - 1); 8982 } 8983 8984 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 8985 const RecordData &Record, 8986 unsigned &Idx) { 8987 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 8988 return CXXTemporary::Create(getContext(), Decl); 8989 } 8990 8991 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 8992 return Diag(CurrentImportLoc, DiagID); 8993 } 8994 8995 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 8996 return Diags.Report(Loc, DiagID); 8997 } 8998 8999 /// Retrieve the identifier table associated with the 9000 /// preprocessor. 9001 IdentifierTable &ASTReader::getIdentifierTable() { 9002 return PP.getIdentifierTable(); 9003 } 9004 9005 /// Record that the given ID maps to the given switch-case 9006 /// statement. 9007 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9008 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9009 "Already have a SwitchCase with this ID"); 9010 (*CurrSwitchCaseStmts)[ID] = SC; 9011 } 9012 9013 /// Retrieve the switch-case statement with the given ID. 9014 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9015 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9016 return (*CurrSwitchCaseStmts)[ID]; 9017 } 9018 9019 void ASTReader::ClearSwitchCaseIDs() { 9020 CurrSwitchCaseStmts->clear(); 9021 } 9022 9023 void ASTReader::ReadComments() { 9024 ASTContext &Context = getContext(); 9025 std::vector<RawComment *> Comments; 9026 for (SmallVectorImpl<std::pair<BitstreamCursor, 9027 serialization::ModuleFile *>>::iterator 9028 I = CommentsCursors.begin(), 9029 E = CommentsCursors.end(); 9030 I != E; ++I) { 9031 Comments.clear(); 9032 BitstreamCursor &Cursor = I->first; 9033 serialization::ModuleFile &F = *I->second; 9034 SavedStreamPosition SavedPosition(Cursor); 9035 9036 RecordData Record; 9037 while (true) { 9038 Expected<llvm::BitstreamEntry> MaybeEntry = 9039 Cursor.advanceSkippingSubblocks( 9040 BitstreamCursor::AF_DontPopBlockAtEnd); 9041 if (!MaybeEntry) { 9042 Error(MaybeEntry.takeError()); 9043 return; 9044 } 9045 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9046 9047 switch (Entry.Kind) { 9048 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9049 case llvm::BitstreamEntry::Error: 9050 Error("malformed block record in AST file"); 9051 return; 9052 case llvm::BitstreamEntry::EndBlock: 9053 goto NextCursor; 9054 case llvm::BitstreamEntry::Record: 9055 // The interesting case. 9056 break; 9057 } 9058 9059 // Read a record. 9060 Record.clear(); 9061 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9062 if (!MaybeComment) { 9063 Error(MaybeComment.takeError()); 9064 return; 9065 } 9066 switch ((CommentRecordTypes)MaybeComment.get()) { 9067 case COMMENTS_RAW_COMMENT: { 9068 unsigned Idx = 0; 9069 SourceRange SR = ReadSourceRange(F, Record, Idx); 9070 RawComment::CommentKind Kind = 9071 (RawComment::CommentKind) Record[Idx++]; 9072 bool IsTrailingComment = Record[Idx++]; 9073 bool IsAlmostTrailingComment = Record[Idx++]; 9074 Comments.push_back(new (Context) RawComment( 9075 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9076 break; 9077 } 9078 } 9079 } 9080 NextCursor: 9081 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9082 FileToOffsetToComment; 9083 for (RawComment *C : Comments) { 9084 SourceLocation CommentLoc = C->getBeginLoc(); 9085 if (CommentLoc.isValid()) { 9086 std::pair<FileID, unsigned> Loc = 9087 SourceMgr.getDecomposedLoc(CommentLoc); 9088 if (Loc.first.isValid()) 9089 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9090 } 9091 } 9092 } 9093 } 9094 9095 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9096 bool IncludeSystem, bool Complain, 9097 llvm::function_ref<void(const serialization::InputFile &IF, 9098 bool isSystem)> Visitor) { 9099 unsigned NumUserInputs = MF.NumUserInputFiles; 9100 unsigned NumInputs = MF.InputFilesLoaded.size(); 9101 assert(NumUserInputs <= NumInputs); 9102 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9103 for (unsigned I = 0; I < N; ++I) { 9104 bool IsSystem = I >= NumUserInputs; 9105 InputFile IF = getInputFile(MF, I+1, Complain); 9106 Visitor(IF, IsSystem); 9107 } 9108 } 9109 9110 void ASTReader::visitTopLevelModuleMaps( 9111 serialization::ModuleFile &MF, 9112 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9113 unsigned NumInputs = MF.InputFilesLoaded.size(); 9114 for (unsigned I = 0; I < NumInputs; ++I) { 9115 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9116 if (IFI.TopLevelModuleMap) 9117 // FIXME: This unnecessarily re-reads the InputFileInfo. 9118 if (auto FE = getInputFile(MF, I + 1).getFile()) 9119 Visitor(FE); 9120 } 9121 } 9122 9123 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9124 // If we know the owning module, use it. 9125 if (Module *M = D->getImportedOwningModule()) 9126 return M->getFullModuleName(); 9127 9128 // Otherwise, use the name of the top-level module the decl is within. 9129 if (ModuleFile *M = getOwningModuleFile(D)) 9130 return M->ModuleName; 9131 9132 // Not from a module. 9133 return {}; 9134 } 9135 9136 void ASTReader::finishPendingActions() { 9137 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9138 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9139 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9140 !PendingUpdateRecords.empty()) { 9141 // If any identifiers with corresponding top-level declarations have 9142 // been loaded, load those declarations now. 9143 using TopLevelDeclsMap = 9144 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9145 TopLevelDeclsMap TopLevelDecls; 9146 9147 while (!PendingIdentifierInfos.empty()) { 9148 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9149 SmallVector<uint32_t, 4> DeclIDs = 9150 std::move(PendingIdentifierInfos.back().second); 9151 PendingIdentifierInfos.pop_back(); 9152 9153 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9154 } 9155 9156 // Load each function type that we deferred loading because it was a 9157 // deduced type that might refer to a local type declared within itself. 9158 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9159 auto *FD = PendingFunctionTypes[I].first; 9160 FD->setType(GetType(PendingFunctionTypes[I].second)); 9161 9162 // If we gave a function a deduced return type, remember that we need to 9163 // propagate that along the redeclaration chain. 9164 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9165 if (DT && DT->isDeduced()) 9166 PendingDeducedTypeUpdates.insert( 9167 {FD->getCanonicalDecl(), FD->getReturnType()}); 9168 } 9169 PendingFunctionTypes.clear(); 9170 9171 // For each decl chain that we wanted to complete while deserializing, mark 9172 // it as "still needs to be completed". 9173 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9174 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9175 } 9176 PendingIncompleteDeclChains.clear(); 9177 9178 // Load pending declaration chains. 9179 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9180 loadPendingDeclChain(PendingDeclChains[I].first, 9181 PendingDeclChains[I].second); 9182 PendingDeclChains.clear(); 9183 9184 // Make the most recent of the top-level declarations visible. 9185 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9186 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9187 IdentifierInfo *II = TLD->first; 9188 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9189 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9190 } 9191 } 9192 9193 // Load any pending macro definitions. 9194 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9195 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9196 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9197 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9198 // Initialize the macro history from chained-PCHs ahead of module imports. 9199 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9200 ++IDIdx) { 9201 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9202 if (!Info.M->isModule()) 9203 resolvePendingMacro(II, Info); 9204 } 9205 // Handle module imports. 9206 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9207 ++IDIdx) { 9208 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9209 if (Info.M->isModule()) 9210 resolvePendingMacro(II, Info); 9211 } 9212 } 9213 PendingMacroIDs.clear(); 9214 9215 // Wire up the DeclContexts for Decls that we delayed setting until 9216 // recursive loading is completed. 9217 while (!PendingDeclContextInfos.empty()) { 9218 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9219 PendingDeclContextInfos.pop_front(); 9220 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9221 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9222 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9223 } 9224 9225 // Perform any pending declaration updates. 9226 while (!PendingUpdateRecords.empty()) { 9227 auto Update = PendingUpdateRecords.pop_back_val(); 9228 ReadingKindTracker ReadingKind(Read_Decl, *this); 9229 loadDeclUpdateRecords(Update); 9230 } 9231 } 9232 9233 // At this point, all update records for loaded decls are in place, so any 9234 // fake class definitions should have become real. 9235 assert(PendingFakeDefinitionData.empty() && 9236 "faked up a class definition but never saw the real one"); 9237 9238 // If we deserialized any C++ or Objective-C class definitions, any 9239 // Objective-C protocol definitions, or any redeclarable templates, make sure 9240 // that all redeclarations point to the definitions. Note that this can only 9241 // happen now, after the redeclaration chains have been fully wired. 9242 for (Decl *D : PendingDefinitions) { 9243 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9244 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9245 // Make sure that the TagType points at the definition. 9246 const_cast<TagType*>(TagT)->decl = TD; 9247 } 9248 9249 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9250 for (auto *R = getMostRecentExistingDecl(RD); R; 9251 R = R->getPreviousDecl()) { 9252 assert((R == D) == 9253 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9254 "declaration thinks it's the definition but it isn't"); 9255 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9256 } 9257 } 9258 9259 continue; 9260 } 9261 9262 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9263 // Make sure that the ObjCInterfaceType points at the definition. 9264 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9265 ->Decl = ID; 9266 9267 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9268 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9269 9270 continue; 9271 } 9272 9273 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9274 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9275 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9276 9277 continue; 9278 } 9279 9280 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9281 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9282 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9283 } 9284 PendingDefinitions.clear(); 9285 9286 // Load the bodies of any functions or methods we've encountered. We do 9287 // this now (delayed) so that we can be sure that the declaration chains 9288 // have been fully wired up (hasBody relies on this). 9289 // FIXME: We shouldn't require complete redeclaration chains here. 9290 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9291 PBEnd = PendingBodies.end(); 9292 PB != PBEnd; ++PB) { 9293 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9294 // For a function defined inline within a class template, force the 9295 // canonical definition to be the one inside the canonical definition of 9296 // the template. This ensures that we instantiate from a correct view 9297 // of the template. 9298 // 9299 // Sadly we can't do this more generally: we can't be sure that all 9300 // copies of an arbitrary class definition will have the same members 9301 // defined (eg, some member functions may not be instantiated, and some 9302 // special members may or may not have been implicitly defined). 9303 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9304 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9305 continue; 9306 9307 // FIXME: Check for =delete/=default? 9308 // FIXME: Complain about ODR violations here? 9309 const FunctionDecl *Defn = nullptr; 9310 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9311 FD->setLazyBody(PB->second); 9312 } else { 9313 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9314 mergeDefinitionVisibility(NonConstDefn, FD); 9315 9316 if (!FD->isLateTemplateParsed() && 9317 !NonConstDefn->isLateTemplateParsed() && 9318 FD->getODRHash() != NonConstDefn->getODRHash()) { 9319 if (!isa<CXXMethodDecl>(FD)) { 9320 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9321 } else if (FD->getLexicalParent()->isFileContext() && 9322 NonConstDefn->getLexicalParent()->isFileContext()) { 9323 // Only diagnose out-of-line method definitions. If they are 9324 // in class definitions, then an error will be generated when 9325 // processing the class bodies. 9326 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9327 } 9328 } 9329 } 9330 continue; 9331 } 9332 9333 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9334 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9335 MD->setLazyBody(PB->second); 9336 } 9337 PendingBodies.clear(); 9338 9339 // Do some cleanup. 9340 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9341 getContext().deduplicateMergedDefinitonsFor(ND); 9342 PendingMergedDefinitionsToDeduplicate.clear(); 9343 } 9344 9345 void ASTReader::diagnoseOdrViolations() { 9346 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9347 PendingFunctionOdrMergeFailures.empty() && 9348 PendingEnumOdrMergeFailures.empty()) 9349 return; 9350 9351 // Trigger the import of the full definition of each class that had any 9352 // odr-merging problems, so we can produce better diagnostics for them. 9353 // These updates may in turn find and diagnose some ODR failures, so take 9354 // ownership of the set first. 9355 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9356 PendingOdrMergeFailures.clear(); 9357 for (auto &Merge : OdrMergeFailures) { 9358 Merge.first->buildLookup(); 9359 Merge.first->decls_begin(); 9360 Merge.first->bases_begin(); 9361 Merge.first->vbases_begin(); 9362 for (auto &RecordPair : Merge.second) { 9363 auto *RD = RecordPair.first; 9364 RD->decls_begin(); 9365 RD->bases_begin(); 9366 RD->vbases_begin(); 9367 } 9368 } 9369 9370 // Trigger the import of functions. 9371 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9372 PendingFunctionOdrMergeFailures.clear(); 9373 for (auto &Merge : FunctionOdrMergeFailures) { 9374 Merge.first->buildLookup(); 9375 Merge.first->decls_begin(); 9376 Merge.first->getBody(); 9377 for (auto &FD : Merge.second) { 9378 FD->buildLookup(); 9379 FD->decls_begin(); 9380 FD->getBody(); 9381 } 9382 } 9383 9384 // Trigger the import of enums. 9385 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9386 PendingEnumOdrMergeFailures.clear(); 9387 for (auto &Merge : EnumOdrMergeFailures) { 9388 Merge.first->decls_begin(); 9389 for (auto &Enum : Merge.second) { 9390 Enum->decls_begin(); 9391 } 9392 } 9393 9394 // For each declaration from a merged context, check that the canonical 9395 // definition of that context also contains a declaration of the same 9396 // entity. 9397 // 9398 // Caution: this loop does things that might invalidate iterators into 9399 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9400 while (!PendingOdrMergeChecks.empty()) { 9401 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9402 9403 // FIXME: Skip over implicit declarations for now. This matters for things 9404 // like implicitly-declared special member functions. This isn't entirely 9405 // correct; we can end up with multiple unmerged declarations of the same 9406 // implicit entity. 9407 if (D->isImplicit()) 9408 continue; 9409 9410 DeclContext *CanonDef = D->getDeclContext(); 9411 9412 bool Found = false; 9413 const Decl *DCanon = D->getCanonicalDecl(); 9414 9415 for (auto RI : D->redecls()) { 9416 if (RI->getLexicalDeclContext() == CanonDef) { 9417 Found = true; 9418 break; 9419 } 9420 } 9421 if (Found) 9422 continue; 9423 9424 // Quick check failed, time to do the slow thing. Note, we can't just 9425 // look up the name of D in CanonDef here, because the member that is 9426 // in CanonDef might not be found by name lookup (it might have been 9427 // replaced by a more recent declaration in the lookup table), and we 9428 // can't necessarily find it in the redeclaration chain because it might 9429 // be merely mergeable, not redeclarable. 9430 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9431 for (auto *CanonMember : CanonDef->decls()) { 9432 if (CanonMember->getCanonicalDecl() == DCanon) { 9433 // This can happen if the declaration is merely mergeable and not 9434 // actually redeclarable (we looked for redeclarations earlier). 9435 // 9436 // FIXME: We should be able to detect this more efficiently, without 9437 // pulling in all of the members of CanonDef. 9438 Found = true; 9439 break; 9440 } 9441 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9442 if (ND->getDeclName() == D->getDeclName()) 9443 Candidates.push_back(ND); 9444 } 9445 9446 if (!Found) { 9447 // The AST doesn't like TagDecls becoming invalid after they've been 9448 // completed. We only really need to mark FieldDecls as invalid here. 9449 if (!isa<TagDecl>(D)) 9450 D->setInvalidDecl(); 9451 9452 // Ensure we don't accidentally recursively enter deserialization while 9453 // we're producing our diagnostic. 9454 Deserializing RecursionGuard(this); 9455 9456 std::string CanonDefModule = 9457 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9458 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9459 << D << getOwningModuleNameForDiagnostic(D) 9460 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9461 9462 if (Candidates.empty()) 9463 Diag(cast<Decl>(CanonDef)->getLocation(), 9464 diag::note_module_odr_violation_no_possible_decls) << D; 9465 else { 9466 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9467 Diag(Candidates[I]->getLocation(), 9468 diag::note_module_odr_violation_possible_decl) 9469 << Candidates[I]; 9470 } 9471 9472 DiagnosedOdrMergeFailures.insert(CanonDef); 9473 } 9474 } 9475 9476 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9477 EnumOdrMergeFailures.empty()) 9478 return; 9479 9480 // Ensure we don't accidentally recursively enter deserialization while 9481 // we're producing our diagnostics. 9482 Deserializing RecursionGuard(this); 9483 9484 // Common code for hashing helpers. 9485 ODRHash Hash; 9486 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9487 Hash.clear(); 9488 Hash.AddQualType(Ty); 9489 return Hash.CalculateHash(); 9490 }; 9491 9492 auto ComputeODRHash = [&Hash](const Stmt *S) { 9493 assert(S); 9494 Hash.clear(); 9495 Hash.AddStmt(S); 9496 return Hash.CalculateHash(); 9497 }; 9498 9499 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9500 assert(D); 9501 Hash.clear(); 9502 Hash.AddSubDecl(D); 9503 return Hash.CalculateHash(); 9504 }; 9505 9506 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9507 Hash.clear(); 9508 Hash.AddTemplateArgument(TA); 9509 return Hash.CalculateHash(); 9510 }; 9511 9512 auto ComputeTemplateParameterListODRHash = 9513 [&Hash](const TemplateParameterList *TPL) { 9514 assert(TPL); 9515 Hash.clear(); 9516 Hash.AddTemplateParameterList(TPL); 9517 return Hash.CalculateHash(); 9518 }; 9519 9520 // Used with err_module_odr_violation_mismatch_decl and 9521 // note_module_odr_violation_mismatch_decl 9522 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9523 enum ODRMismatchDecl { 9524 EndOfClass, 9525 PublicSpecifer, 9526 PrivateSpecifer, 9527 ProtectedSpecifer, 9528 StaticAssert, 9529 Field, 9530 CXXMethod, 9531 TypeAlias, 9532 TypeDef, 9533 Var, 9534 Friend, 9535 FunctionTemplate, 9536 Other 9537 }; 9538 9539 // Used with err_module_odr_violation_mismatch_decl_diff and 9540 // note_module_odr_violation_mismatch_decl_diff 9541 enum ODRMismatchDeclDifference { 9542 StaticAssertCondition, 9543 StaticAssertMessage, 9544 StaticAssertOnlyMessage, 9545 FieldName, 9546 FieldTypeName, 9547 FieldSingleBitField, 9548 FieldDifferentWidthBitField, 9549 FieldSingleMutable, 9550 FieldSingleInitializer, 9551 FieldDifferentInitializers, 9552 MethodName, 9553 MethodDeleted, 9554 MethodDefaulted, 9555 MethodVirtual, 9556 MethodStatic, 9557 MethodVolatile, 9558 MethodConst, 9559 MethodInline, 9560 MethodNumberParameters, 9561 MethodParameterType, 9562 MethodParameterName, 9563 MethodParameterSingleDefaultArgument, 9564 MethodParameterDifferentDefaultArgument, 9565 MethodNoTemplateArguments, 9566 MethodDifferentNumberTemplateArguments, 9567 MethodDifferentTemplateArgument, 9568 MethodSingleBody, 9569 MethodDifferentBody, 9570 TypedefName, 9571 TypedefType, 9572 VarName, 9573 VarType, 9574 VarSingleInitializer, 9575 VarDifferentInitializer, 9576 VarConstexpr, 9577 FriendTypeFunction, 9578 FriendType, 9579 FriendFunction, 9580 FunctionTemplateDifferentNumberParameters, 9581 FunctionTemplateParameterDifferentKind, 9582 FunctionTemplateParameterName, 9583 FunctionTemplateParameterSingleDefaultArgument, 9584 FunctionTemplateParameterDifferentDefaultArgument, 9585 FunctionTemplateParameterDifferentType, 9586 FunctionTemplatePackParameter, 9587 }; 9588 9589 // These lambdas have the common portions of the ODR diagnostics. This 9590 // has the same return as Diag(), so addition parameters can be passed 9591 // in with operator<< 9592 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9593 SourceLocation Loc, SourceRange Range, 9594 ODRMismatchDeclDifference DiffType) { 9595 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9596 << FirstRecord << FirstModule.empty() << FirstModule << Range 9597 << DiffType; 9598 }; 9599 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9600 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9601 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9602 << SecondModule << Range << DiffType; 9603 }; 9604 9605 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9606 &ComputeQualTypeODRHash, &ComputeODRHash]( 9607 NamedDecl *FirstRecord, StringRef FirstModule, 9608 StringRef SecondModule, FieldDecl *FirstField, 9609 FieldDecl *SecondField) { 9610 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9611 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9612 if (FirstII->getName() != SecondII->getName()) { 9613 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9614 FirstField->getSourceRange(), FieldName) 9615 << FirstII; 9616 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9617 SecondField->getSourceRange(), FieldName) 9618 << SecondII; 9619 9620 return true; 9621 } 9622 9623 assert(getContext().hasSameType(FirstField->getType(), 9624 SecondField->getType())); 9625 9626 QualType FirstType = FirstField->getType(); 9627 QualType SecondType = SecondField->getType(); 9628 if (ComputeQualTypeODRHash(FirstType) != 9629 ComputeQualTypeODRHash(SecondType)) { 9630 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9631 FirstField->getSourceRange(), FieldTypeName) 9632 << FirstII << FirstType; 9633 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9634 SecondField->getSourceRange(), FieldTypeName) 9635 << SecondII << SecondType; 9636 9637 return true; 9638 } 9639 9640 const bool IsFirstBitField = FirstField->isBitField(); 9641 const bool IsSecondBitField = SecondField->isBitField(); 9642 if (IsFirstBitField != IsSecondBitField) { 9643 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9644 FirstField->getSourceRange(), FieldSingleBitField) 9645 << FirstII << IsFirstBitField; 9646 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9647 SecondField->getSourceRange(), FieldSingleBitField) 9648 << SecondII << IsSecondBitField; 9649 return true; 9650 } 9651 9652 if (IsFirstBitField && IsSecondBitField) { 9653 unsigned FirstBitWidthHash = 9654 ComputeODRHash(FirstField->getBitWidth()); 9655 unsigned SecondBitWidthHash = 9656 ComputeODRHash(SecondField->getBitWidth()); 9657 if (FirstBitWidthHash != SecondBitWidthHash) { 9658 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9659 FirstField->getSourceRange(), 9660 FieldDifferentWidthBitField) 9661 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9662 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9663 SecondField->getSourceRange(), 9664 FieldDifferentWidthBitField) 9665 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9666 return true; 9667 } 9668 } 9669 9670 if (!PP.getLangOpts().CPlusPlus) 9671 return false; 9672 9673 const bool IsFirstMutable = FirstField->isMutable(); 9674 const bool IsSecondMutable = SecondField->isMutable(); 9675 if (IsFirstMutable != IsSecondMutable) { 9676 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9677 FirstField->getSourceRange(), FieldSingleMutable) 9678 << FirstII << IsFirstMutable; 9679 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9680 SecondField->getSourceRange(), FieldSingleMutable) 9681 << SecondII << IsSecondMutable; 9682 return true; 9683 } 9684 9685 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9686 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9687 if ((!FirstInitializer && SecondInitializer) || 9688 (FirstInitializer && !SecondInitializer)) { 9689 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9690 FirstField->getSourceRange(), FieldSingleInitializer) 9691 << FirstII << (FirstInitializer != nullptr); 9692 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9693 SecondField->getSourceRange(), FieldSingleInitializer) 9694 << SecondII << (SecondInitializer != nullptr); 9695 return true; 9696 } 9697 9698 if (FirstInitializer && SecondInitializer) { 9699 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9700 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9701 if (FirstInitHash != SecondInitHash) { 9702 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9703 FirstField->getSourceRange(), 9704 FieldDifferentInitializers) 9705 << FirstII << FirstInitializer->getSourceRange(); 9706 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9707 SecondField->getSourceRange(), 9708 FieldDifferentInitializers) 9709 << SecondII << SecondInitializer->getSourceRange(); 9710 return true; 9711 } 9712 } 9713 9714 return false; 9715 }; 9716 9717 auto ODRDiagTypeDefOrAlias = 9718 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9719 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9720 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9721 bool IsTypeAlias) { 9722 auto FirstName = FirstTD->getDeclName(); 9723 auto SecondName = SecondTD->getDeclName(); 9724 if (FirstName != SecondName) { 9725 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9726 FirstTD->getSourceRange(), TypedefName) 9727 << IsTypeAlias << FirstName; 9728 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9729 SecondTD->getSourceRange(), TypedefName) 9730 << IsTypeAlias << SecondName; 9731 return true; 9732 } 9733 9734 QualType FirstType = FirstTD->getUnderlyingType(); 9735 QualType SecondType = SecondTD->getUnderlyingType(); 9736 if (ComputeQualTypeODRHash(FirstType) != 9737 ComputeQualTypeODRHash(SecondType)) { 9738 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9739 FirstTD->getSourceRange(), TypedefType) 9740 << IsTypeAlias << FirstName << FirstType; 9741 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9742 SecondTD->getSourceRange(), TypedefType) 9743 << IsTypeAlias << SecondName << SecondType; 9744 return true; 9745 } 9746 9747 return false; 9748 }; 9749 9750 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9751 &ComputeQualTypeODRHash, &ComputeODRHash, 9752 this](NamedDecl *FirstRecord, StringRef FirstModule, 9753 StringRef SecondModule, VarDecl *FirstVD, 9754 VarDecl *SecondVD) { 9755 auto FirstName = FirstVD->getDeclName(); 9756 auto SecondName = SecondVD->getDeclName(); 9757 if (FirstName != SecondName) { 9758 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9759 FirstVD->getSourceRange(), VarName) 9760 << FirstName; 9761 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9762 SecondVD->getSourceRange(), VarName) 9763 << SecondName; 9764 return true; 9765 } 9766 9767 QualType FirstType = FirstVD->getType(); 9768 QualType SecondType = SecondVD->getType(); 9769 if (ComputeQualTypeODRHash(FirstType) != 9770 ComputeQualTypeODRHash(SecondType)) { 9771 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9772 FirstVD->getSourceRange(), VarType) 9773 << FirstName << FirstType; 9774 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9775 SecondVD->getSourceRange(), VarType) 9776 << SecondName << SecondType; 9777 return true; 9778 } 9779 9780 if (!PP.getLangOpts().CPlusPlus) 9781 return false; 9782 9783 const Expr *FirstInit = FirstVD->getInit(); 9784 const Expr *SecondInit = SecondVD->getInit(); 9785 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9786 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9787 FirstVD->getSourceRange(), VarSingleInitializer) 9788 << FirstName << (FirstInit == nullptr) 9789 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9790 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9791 SecondVD->getSourceRange(), VarSingleInitializer) 9792 << SecondName << (SecondInit == nullptr) 9793 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9794 return true; 9795 } 9796 9797 if (FirstInit && SecondInit && 9798 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9799 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9800 FirstVD->getSourceRange(), VarDifferentInitializer) 9801 << FirstName << FirstInit->getSourceRange(); 9802 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9803 SecondVD->getSourceRange(), VarDifferentInitializer) 9804 << SecondName << SecondInit->getSourceRange(); 9805 return true; 9806 } 9807 9808 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9809 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9810 if (FirstIsConstexpr != SecondIsConstexpr) { 9811 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9812 FirstVD->getSourceRange(), VarConstexpr) 9813 << FirstName << FirstIsConstexpr; 9814 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9815 SecondVD->getSourceRange(), VarConstexpr) 9816 << SecondName << SecondIsConstexpr; 9817 return true; 9818 } 9819 return false; 9820 }; 9821 9822 auto DifferenceSelector = [](Decl *D) { 9823 assert(D && "valid Decl required"); 9824 switch (D->getKind()) { 9825 default: 9826 return Other; 9827 case Decl::AccessSpec: 9828 switch (D->getAccess()) { 9829 case AS_public: 9830 return PublicSpecifer; 9831 case AS_private: 9832 return PrivateSpecifer; 9833 case AS_protected: 9834 return ProtectedSpecifer; 9835 case AS_none: 9836 break; 9837 } 9838 llvm_unreachable("Invalid access specifier"); 9839 case Decl::StaticAssert: 9840 return StaticAssert; 9841 case Decl::Field: 9842 return Field; 9843 case Decl::CXXMethod: 9844 case Decl::CXXConstructor: 9845 case Decl::CXXDestructor: 9846 return CXXMethod; 9847 case Decl::TypeAlias: 9848 return TypeAlias; 9849 case Decl::Typedef: 9850 return TypeDef; 9851 case Decl::Var: 9852 return Var; 9853 case Decl::Friend: 9854 return Friend; 9855 case Decl::FunctionTemplate: 9856 return FunctionTemplate; 9857 } 9858 }; 9859 9860 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9861 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9862 RecordDecl *Record, 9863 const DeclContext *DC) { 9864 for (auto *D : Record->decls()) { 9865 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9866 continue; 9867 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9868 } 9869 }; 9870 9871 struct DiffResult { 9872 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9873 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9874 }; 9875 9876 // If there is a diagnoseable difference, FirstDiffType and 9877 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9878 // filled in if not EndOfClass. 9879 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9880 DeclHashes &SecondHashes) { 9881 DiffResult DR; 9882 auto FirstIt = FirstHashes.begin(); 9883 auto SecondIt = SecondHashes.begin(); 9884 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9885 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9886 FirstIt->second == SecondIt->second) { 9887 ++FirstIt; 9888 ++SecondIt; 9889 continue; 9890 } 9891 9892 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9893 DR.SecondDecl = 9894 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9895 9896 DR.FirstDiffType = 9897 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9898 DR.SecondDiffType = 9899 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9900 return DR; 9901 } 9902 return DR; 9903 }; 9904 9905 // Use this to diagnose that an unexpected Decl was encountered 9906 // or no difference was detected. This causes a generic error 9907 // message to be emitted. 9908 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9909 StringRef FirstModule, 9910 NamedDecl *SecondRecord, 9911 StringRef SecondModule) { 9912 Diag(FirstRecord->getLocation(), 9913 diag::err_module_odr_violation_different_definitions) 9914 << FirstRecord << FirstModule.empty() << FirstModule; 9915 9916 if (DR.FirstDecl) { 9917 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9918 << FirstRecord << DR.FirstDecl->getSourceRange(); 9919 } 9920 9921 Diag(SecondRecord->getLocation(), 9922 diag::note_module_odr_violation_different_definitions) 9923 << SecondModule; 9924 9925 if (DR.SecondDecl) { 9926 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9927 << DR.SecondDecl->getSourceRange(); 9928 } 9929 }; 9930 9931 auto DiagnoseODRMismatch = 9932 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9933 NamedDecl *SecondRecord, StringRef SecondModule) { 9934 SourceLocation FirstLoc; 9935 SourceRange FirstRange; 9936 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9937 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9938 FirstLoc = FirstTag->getBraceRange().getEnd(); 9939 } else { 9940 FirstLoc = DR.FirstDecl->getLocation(); 9941 FirstRange = DR.FirstDecl->getSourceRange(); 9942 } 9943 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 9944 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 9945 << DR.FirstDiffType; 9946 9947 SourceLocation SecondLoc; 9948 SourceRange SecondRange; 9949 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 9950 if (DR.SecondDiffType == EndOfClass && SecondTag) { 9951 SecondLoc = SecondTag->getBraceRange().getEnd(); 9952 } else { 9953 SecondLoc = DR.SecondDecl->getLocation(); 9954 SecondRange = DR.SecondDecl->getSourceRange(); 9955 } 9956 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 9957 << SecondModule << SecondRange << DR.SecondDiffType; 9958 }; 9959 9960 // Issue any pending ODR-failure diagnostics. 9961 for (auto &Merge : OdrMergeFailures) { 9962 // If we've already pointed out a specific problem with this class, don't 9963 // bother issuing a general "something's different" diagnostic. 9964 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 9965 continue; 9966 9967 bool Diagnosed = false; 9968 CXXRecordDecl *FirstRecord = Merge.first; 9969 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 9970 for (auto &RecordPair : Merge.second) { 9971 CXXRecordDecl *SecondRecord = RecordPair.first; 9972 // Multiple different declarations got merged together; tell the user 9973 // where they came from. 9974 if (FirstRecord == SecondRecord) 9975 continue; 9976 9977 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 9978 9979 auto *FirstDD = FirstRecord->DefinitionData; 9980 auto *SecondDD = RecordPair.second; 9981 9982 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 9983 9984 // Diagnostics from DefinitionData are emitted here. 9985 if (FirstDD != SecondDD) { 9986 enum ODRDefinitionDataDifference { 9987 NumBases, 9988 NumVBases, 9989 BaseType, 9990 BaseVirtual, 9991 BaseAccess, 9992 }; 9993 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 9994 this](SourceLocation Loc, SourceRange Range, 9995 ODRDefinitionDataDifference DiffType) { 9996 return Diag(Loc, diag::err_module_odr_violation_definition_data) 9997 << FirstRecord << FirstModule.empty() << FirstModule << Range 9998 << DiffType; 9999 }; 10000 auto ODRDiagBaseNote = [&SecondModule, 10001 this](SourceLocation Loc, SourceRange Range, 10002 ODRDefinitionDataDifference DiffType) { 10003 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10004 << SecondModule << Range << DiffType; 10005 }; 10006 10007 unsigned FirstNumBases = FirstDD->NumBases; 10008 unsigned FirstNumVBases = FirstDD->NumVBases; 10009 unsigned SecondNumBases = SecondDD->NumBases; 10010 unsigned SecondNumVBases = SecondDD->NumVBases; 10011 10012 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10013 unsigned NumBases = DD->NumBases; 10014 if (NumBases == 0) return SourceRange(); 10015 auto bases = DD->bases(); 10016 return SourceRange(bases[0].getBeginLoc(), 10017 bases[NumBases - 1].getEndLoc()); 10018 }; 10019 10020 if (FirstNumBases != SecondNumBases) { 10021 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10022 NumBases) 10023 << FirstNumBases; 10024 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10025 NumBases) 10026 << SecondNumBases; 10027 Diagnosed = true; 10028 break; 10029 } 10030 10031 if (FirstNumVBases != SecondNumVBases) { 10032 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10033 NumVBases) 10034 << FirstNumVBases; 10035 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10036 NumVBases) 10037 << SecondNumVBases; 10038 Diagnosed = true; 10039 break; 10040 } 10041 10042 auto FirstBases = FirstDD->bases(); 10043 auto SecondBases = SecondDD->bases(); 10044 unsigned i = 0; 10045 for (i = 0; i < FirstNumBases; ++i) { 10046 auto FirstBase = FirstBases[i]; 10047 auto SecondBase = SecondBases[i]; 10048 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10049 ComputeQualTypeODRHash(SecondBase.getType())) { 10050 ODRDiagBaseError(FirstRecord->getLocation(), 10051 FirstBase.getSourceRange(), BaseType) 10052 << (i + 1) << FirstBase.getType(); 10053 ODRDiagBaseNote(SecondRecord->getLocation(), 10054 SecondBase.getSourceRange(), BaseType) 10055 << (i + 1) << SecondBase.getType(); 10056 break; 10057 } 10058 10059 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10060 ODRDiagBaseError(FirstRecord->getLocation(), 10061 FirstBase.getSourceRange(), BaseVirtual) 10062 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10063 ODRDiagBaseNote(SecondRecord->getLocation(), 10064 SecondBase.getSourceRange(), BaseVirtual) 10065 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10066 break; 10067 } 10068 10069 if (FirstBase.getAccessSpecifierAsWritten() != 10070 SecondBase.getAccessSpecifierAsWritten()) { 10071 ODRDiagBaseError(FirstRecord->getLocation(), 10072 FirstBase.getSourceRange(), BaseAccess) 10073 << (i + 1) << FirstBase.getType() 10074 << (int)FirstBase.getAccessSpecifierAsWritten(); 10075 ODRDiagBaseNote(SecondRecord->getLocation(), 10076 SecondBase.getSourceRange(), BaseAccess) 10077 << (i + 1) << SecondBase.getType() 10078 << (int)SecondBase.getAccessSpecifierAsWritten(); 10079 break; 10080 } 10081 } 10082 10083 if (i != FirstNumBases) { 10084 Diagnosed = true; 10085 break; 10086 } 10087 } 10088 10089 const ClassTemplateDecl *FirstTemplate = 10090 FirstRecord->getDescribedClassTemplate(); 10091 const ClassTemplateDecl *SecondTemplate = 10092 SecondRecord->getDescribedClassTemplate(); 10093 10094 assert(!FirstTemplate == !SecondTemplate && 10095 "Both pointers should be null or non-null"); 10096 10097 enum ODRTemplateDifference { 10098 ParamEmptyName, 10099 ParamName, 10100 ParamSingleDefaultArgument, 10101 ParamDifferentDefaultArgument, 10102 }; 10103 10104 if (FirstTemplate && SecondTemplate) { 10105 DeclHashes FirstTemplateHashes; 10106 DeclHashes SecondTemplateHashes; 10107 10108 auto PopulateTemplateParameterHashs = 10109 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10110 const ClassTemplateDecl *TD) { 10111 for (auto *D : TD->getTemplateParameters()->asArray()) { 10112 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10113 } 10114 }; 10115 10116 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10117 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10118 10119 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10120 "Number of template parameters should be equal."); 10121 10122 auto FirstIt = FirstTemplateHashes.begin(); 10123 auto FirstEnd = FirstTemplateHashes.end(); 10124 auto SecondIt = SecondTemplateHashes.begin(); 10125 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10126 if (FirstIt->second == SecondIt->second) 10127 continue; 10128 10129 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10130 SourceLocation Loc, SourceRange Range, 10131 ODRTemplateDifference DiffType) { 10132 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10133 << FirstRecord << FirstModule.empty() << FirstModule << Range 10134 << DiffType; 10135 }; 10136 auto ODRDiagTemplateNote = [&SecondModule, this]( 10137 SourceLocation Loc, SourceRange Range, 10138 ODRTemplateDifference DiffType) { 10139 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10140 << SecondModule << Range << DiffType; 10141 }; 10142 10143 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10144 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10145 10146 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10147 "Parameter Decl's should be the same kind."); 10148 10149 DeclarationName FirstName = FirstDecl->getDeclName(); 10150 DeclarationName SecondName = SecondDecl->getDeclName(); 10151 10152 if (FirstName != SecondName) { 10153 const bool FirstNameEmpty = 10154 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10155 const bool SecondNameEmpty = 10156 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10157 assert((!FirstNameEmpty || !SecondNameEmpty) && 10158 "Both template parameters cannot be unnamed."); 10159 ODRDiagTemplateError(FirstDecl->getLocation(), 10160 FirstDecl->getSourceRange(), 10161 FirstNameEmpty ? ParamEmptyName : ParamName) 10162 << FirstName; 10163 ODRDiagTemplateNote(SecondDecl->getLocation(), 10164 SecondDecl->getSourceRange(), 10165 SecondNameEmpty ? ParamEmptyName : ParamName) 10166 << SecondName; 10167 break; 10168 } 10169 10170 switch (FirstDecl->getKind()) { 10171 default: 10172 llvm_unreachable("Invalid template parameter type."); 10173 case Decl::TemplateTypeParm: { 10174 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10175 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10176 const bool HasFirstDefaultArgument = 10177 FirstParam->hasDefaultArgument() && 10178 !FirstParam->defaultArgumentWasInherited(); 10179 const bool HasSecondDefaultArgument = 10180 SecondParam->hasDefaultArgument() && 10181 !SecondParam->defaultArgumentWasInherited(); 10182 10183 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10184 ODRDiagTemplateError(FirstDecl->getLocation(), 10185 FirstDecl->getSourceRange(), 10186 ParamSingleDefaultArgument) 10187 << HasFirstDefaultArgument; 10188 ODRDiagTemplateNote(SecondDecl->getLocation(), 10189 SecondDecl->getSourceRange(), 10190 ParamSingleDefaultArgument) 10191 << HasSecondDefaultArgument; 10192 break; 10193 } 10194 10195 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10196 "Expecting default arguments."); 10197 10198 ODRDiagTemplateError(FirstDecl->getLocation(), 10199 FirstDecl->getSourceRange(), 10200 ParamDifferentDefaultArgument); 10201 ODRDiagTemplateNote(SecondDecl->getLocation(), 10202 SecondDecl->getSourceRange(), 10203 ParamDifferentDefaultArgument); 10204 10205 break; 10206 } 10207 case Decl::NonTypeTemplateParm: { 10208 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10209 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10210 const bool HasFirstDefaultArgument = 10211 FirstParam->hasDefaultArgument() && 10212 !FirstParam->defaultArgumentWasInherited(); 10213 const bool HasSecondDefaultArgument = 10214 SecondParam->hasDefaultArgument() && 10215 !SecondParam->defaultArgumentWasInherited(); 10216 10217 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10218 ODRDiagTemplateError(FirstDecl->getLocation(), 10219 FirstDecl->getSourceRange(), 10220 ParamSingleDefaultArgument) 10221 << HasFirstDefaultArgument; 10222 ODRDiagTemplateNote(SecondDecl->getLocation(), 10223 SecondDecl->getSourceRange(), 10224 ParamSingleDefaultArgument) 10225 << HasSecondDefaultArgument; 10226 break; 10227 } 10228 10229 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10230 "Expecting default arguments."); 10231 10232 ODRDiagTemplateError(FirstDecl->getLocation(), 10233 FirstDecl->getSourceRange(), 10234 ParamDifferentDefaultArgument); 10235 ODRDiagTemplateNote(SecondDecl->getLocation(), 10236 SecondDecl->getSourceRange(), 10237 ParamDifferentDefaultArgument); 10238 10239 break; 10240 } 10241 case Decl::TemplateTemplateParm: { 10242 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10243 const auto *SecondParam = 10244 cast<TemplateTemplateParmDecl>(SecondDecl); 10245 const bool HasFirstDefaultArgument = 10246 FirstParam->hasDefaultArgument() && 10247 !FirstParam->defaultArgumentWasInherited(); 10248 const bool HasSecondDefaultArgument = 10249 SecondParam->hasDefaultArgument() && 10250 !SecondParam->defaultArgumentWasInherited(); 10251 10252 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10253 ODRDiagTemplateError(FirstDecl->getLocation(), 10254 FirstDecl->getSourceRange(), 10255 ParamSingleDefaultArgument) 10256 << HasFirstDefaultArgument; 10257 ODRDiagTemplateNote(SecondDecl->getLocation(), 10258 SecondDecl->getSourceRange(), 10259 ParamSingleDefaultArgument) 10260 << HasSecondDefaultArgument; 10261 break; 10262 } 10263 10264 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10265 "Expecting default arguments."); 10266 10267 ODRDiagTemplateError(FirstDecl->getLocation(), 10268 FirstDecl->getSourceRange(), 10269 ParamDifferentDefaultArgument); 10270 ODRDiagTemplateNote(SecondDecl->getLocation(), 10271 SecondDecl->getSourceRange(), 10272 ParamDifferentDefaultArgument); 10273 10274 break; 10275 } 10276 } 10277 10278 break; 10279 } 10280 10281 if (FirstIt != FirstEnd) { 10282 Diagnosed = true; 10283 break; 10284 } 10285 } 10286 10287 DeclHashes FirstHashes; 10288 DeclHashes SecondHashes; 10289 const DeclContext *DC = FirstRecord; 10290 PopulateHashes(FirstHashes, FirstRecord, DC); 10291 PopulateHashes(SecondHashes, SecondRecord, DC); 10292 10293 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10294 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10295 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10296 Decl *FirstDecl = DR.FirstDecl; 10297 Decl *SecondDecl = DR.SecondDecl; 10298 10299 if (FirstDiffType == Other || SecondDiffType == Other) { 10300 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10301 SecondModule); 10302 Diagnosed = true; 10303 break; 10304 } 10305 10306 if (FirstDiffType != SecondDiffType) { 10307 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10308 SecondModule); 10309 Diagnosed = true; 10310 break; 10311 } 10312 10313 assert(FirstDiffType == SecondDiffType); 10314 10315 switch (FirstDiffType) { 10316 case Other: 10317 case EndOfClass: 10318 case PublicSpecifer: 10319 case PrivateSpecifer: 10320 case ProtectedSpecifer: 10321 llvm_unreachable("Invalid diff type"); 10322 10323 case StaticAssert: { 10324 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10325 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10326 10327 Expr *FirstExpr = FirstSA->getAssertExpr(); 10328 Expr *SecondExpr = SecondSA->getAssertExpr(); 10329 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10330 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10331 if (FirstODRHash != SecondODRHash) { 10332 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10333 FirstExpr->getSourceRange(), StaticAssertCondition); 10334 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10335 SecondExpr->getSourceRange(), StaticAssertCondition); 10336 Diagnosed = true; 10337 break; 10338 } 10339 10340 StringLiteral *FirstStr = FirstSA->getMessage(); 10341 StringLiteral *SecondStr = SecondSA->getMessage(); 10342 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10343 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10344 SourceLocation FirstLoc, SecondLoc; 10345 SourceRange FirstRange, SecondRange; 10346 if (FirstStr) { 10347 FirstLoc = FirstStr->getBeginLoc(); 10348 FirstRange = FirstStr->getSourceRange(); 10349 } else { 10350 FirstLoc = FirstSA->getBeginLoc(); 10351 FirstRange = FirstSA->getSourceRange(); 10352 } 10353 if (SecondStr) { 10354 SecondLoc = SecondStr->getBeginLoc(); 10355 SecondRange = SecondStr->getSourceRange(); 10356 } else { 10357 SecondLoc = SecondSA->getBeginLoc(); 10358 SecondRange = SecondSA->getSourceRange(); 10359 } 10360 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10361 StaticAssertOnlyMessage) 10362 << (FirstStr == nullptr); 10363 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10364 StaticAssertOnlyMessage) 10365 << (SecondStr == nullptr); 10366 Diagnosed = true; 10367 break; 10368 } 10369 10370 if (FirstStr && SecondStr && 10371 FirstStr->getString() != SecondStr->getString()) { 10372 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10373 FirstStr->getSourceRange(), StaticAssertMessage); 10374 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10375 SecondStr->getSourceRange(), StaticAssertMessage); 10376 Diagnosed = true; 10377 break; 10378 } 10379 break; 10380 } 10381 case Field: { 10382 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10383 cast<FieldDecl>(FirstDecl), 10384 cast<FieldDecl>(SecondDecl)); 10385 break; 10386 } 10387 case CXXMethod: { 10388 enum { 10389 DiagMethod, 10390 DiagConstructor, 10391 DiagDestructor, 10392 } FirstMethodType, 10393 SecondMethodType; 10394 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10395 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10396 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10397 return DiagMethod; 10398 }; 10399 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10400 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10401 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10402 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10403 auto FirstName = FirstMethod->getDeclName(); 10404 auto SecondName = SecondMethod->getDeclName(); 10405 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10406 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10407 FirstMethod->getSourceRange(), MethodName) 10408 << FirstMethodType << FirstName; 10409 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10410 SecondMethod->getSourceRange(), MethodName) 10411 << SecondMethodType << SecondName; 10412 10413 Diagnosed = true; 10414 break; 10415 } 10416 10417 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10418 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10419 if (FirstDeleted != SecondDeleted) { 10420 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10421 FirstMethod->getSourceRange(), MethodDeleted) 10422 << FirstMethodType << FirstName << FirstDeleted; 10423 10424 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10425 SecondMethod->getSourceRange(), MethodDeleted) 10426 << SecondMethodType << SecondName << SecondDeleted; 10427 Diagnosed = true; 10428 break; 10429 } 10430 10431 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10432 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10433 if (FirstDefaulted != SecondDefaulted) { 10434 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10435 FirstMethod->getSourceRange(), MethodDefaulted) 10436 << FirstMethodType << FirstName << FirstDefaulted; 10437 10438 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10439 SecondMethod->getSourceRange(), MethodDefaulted) 10440 << SecondMethodType << SecondName << SecondDefaulted; 10441 Diagnosed = true; 10442 break; 10443 } 10444 10445 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10446 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10447 const bool FirstPure = FirstMethod->isPure(); 10448 const bool SecondPure = SecondMethod->isPure(); 10449 if ((FirstVirtual || SecondVirtual) && 10450 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10451 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10452 FirstMethod->getSourceRange(), MethodVirtual) 10453 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10454 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10455 SecondMethod->getSourceRange(), MethodVirtual) 10456 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10457 Diagnosed = true; 10458 break; 10459 } 10460 10461 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10462 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10463 // class needs to be checked instead. 10464 const auto FirstStorage = FirstMethod->getStorageClass(); 10465 const auto SecondStorage = SecondMethod->getStorageClass(); 10466 const bool FirstStatic = FirstStorage == SC_Static; 10467 const bool SecondStatic = SecondStorage == SC_Static; 10468 if (FirstStatic != SecondStatic) { 10469 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10470 FirstMethod->getSourceRange(), MethodStatic) 10471 << FirstMethodType << FirstName << FirstStatic; 10472 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10473 SecondMethod->getSourceRange(), MethodStatic) 10474 << SecondMethodType << SecondName << SecondStatic; 10475 Diagnosed = true; 10476 break; 10477 } 10478 10479 const bool FirstVolatile = FirstMethod->isVolatile(); 10480 const bool SecondVolatile = SecondMethod->isVolatile(); 10481 if (FirstVolatile != SecondVolatile) { 10482 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10483 FirstMethod->getSourceRange(), MethodVolatile) 10484 << FirstMethodType << FirstName << FirstVolatile; 10485 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10486 SecondMethod->getSourceRange(), MethodVolatile) 10487 << SecondMethodType << SecondName << SecondVolatile; 10488 Diagnosed = true; 10489 break; 10490 } 10491 10492 const bool FirstConst = FirstMethod->isConst(); 10493 const bool SecondConst = SecondMethod->isConst(); 10494 if (FirstConst != SecondConst) { 10495 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10496 FirstMethod->getSourceRange(), MethodConst) 10497 << FirstMethodType << FirstName << FirstConst; 10498 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10499 SecondMethod->getSourceRange(), MethodConst) 10500 << SecondMethodType << SecondName << SecondConst; 10501 Diagnosed = true; 10502 break; 10503 } 10504 10505 const bool FirstInline = FirstMethod->isInlineSpecified(); 10506 const bool SecondInline = SecondMethod->isInlineSpecified(); 10507 if (FirstInline != SecondInline) { 10508 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10509 FirstMethod->getSourceRange(), MethodInline) 10510 << FirstMethodType << FirstName << FirstInline; 10511 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10512 SecondMethod->getSourceRange(), MethodInline) 10513 << SecondMethodType << SecondName << SecondInline; 10514 Diagnosed = true; 10515 break; 10516 } 10517 10518 const unsigned FirstNumParameters = FirstMethod->param_size(); 10519 const unsigned SecondNumParameters = SecondMethod->param_size(); 10520 if (FirstNumParameters != SecondNumParameters) { 10521 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10522 FirstMethod->getSourceRange(), 10523 MethodNumberParameters) 10524 << FirstMethodType << FirstName << FirstNumParameters; 10525 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10526 SecondMethod->getSourceRange(), 10527 MethodNumberParameters) 10528 << SecondMethodType << SecondName << SecondNumParameters; 10529 Diagnosed = true; 10530 break; 10531 } 10532 10533 // Need this status boolean to know when break out of the switch. 10534 bool ParameterMismatch = false; 10535 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10536 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10537 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10538 10539 QualType FirstParamType = FirstParam->getType(); 10540 QualType SecondParamType = SecondParam->getType(); 10541 if (FirstParamType != SecondParamType && 10542 ComputeQualTypeODRHash(FirstParamType) != 10543 ComputeQualTypeODRHash(SecondParamType)) { 10544 if (const DecayedType *ParamDecayedType = 10545 FirstParamType->getAs<DecayedType>()) { 10546 ODRDiagDeclError( 10547 FirstRecord, FirstModule, FirstMethod->getLocation(), 10548 FirstMethod->getSourceRange(), MethodParameterType) 10549 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10550 << true << ParamDecayedType->getOriginalType(); 10551 } else { 10552 ODRDiagDeclError( 10553 FirstRecord, FirstModule, FirstMethod->getLocation(), 10554 FirstMethod->getSourceRange(), MethodParameterType) 10555 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10556 << false; 10557 } 10558 10559 if (const DecayedType *ParamDecayedType = 10560 SecondParamType->getAs<DecayedType>()) { 10561 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10562 SecondMethod->getSourceRange(), 10563 MethodParameterType) 10564 << SecondMethodType << SecondName << (I + 1) 10565 << SecondParamType << true 10566 << ParamDecayedType->getOriginalType(); 10567 } else { 10568 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10569 SecondMethod->getSourceRange(), 10570 MethodParameterType) 10571 << SecondMethodType << SecondName << (I + 1) 10572 << SecondParamType << false; 10573 } 10574 ParameterMismatch = true; 10575 break; 10576 } 10577 10578 DeclarationName FirstParamName = FirstParam->getDeclName(); 10579 DeclarationName SecondParamName = SecondParam->getDeclName(); 10580 if (FirstParamName != SecondParamName) { 10581 ODRDiagDeclError(FirstRecord, FirstModule, 10582 FirstMethod->getLocation(), 10583 FirstMethod->getSourceRange(), MethodParameterName) 10584 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10585 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10586 SecondMethod->getSourceRange(), MethodParameterName) 10587 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10588 ParameterMismatch = true; 10589 break; 10590 } 10591 10592 const Expr *FirstInit = FirstParam->getInit(); 10593 const Expr *SecondInit = SecondParam->getInit(); 10594 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10595 ODRDiagDeclError(FirstRecord, FirstModule, 10596 FirstMethod->getLocation(), 10597 FirstMethod->getSourceRange(), 10598 MethodParameterSingleDefaultArgument) 10599 << FirstMethodType << FirstName << (I + 1) 10600 << (FirstInit == nullptr) 10601 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10602 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10603 SecondMethod->getSourceRange(), 10604 MethodParameterSingleDefaultArgument) 10605 << SecondMethodType << SecondName << (I + 1) 10606 << (SecondInit == nullptr) 10607 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10608 ParameterMismatch = true; 10609 break; 10610 } 10611 10612 if (FirstInit && SecondInit && 10613 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10614 ODRDiagDeclError(FirstRecord, FirstModule, 10615 FirstMethod->getLocation(), 10616 FirstMethod->getSourceRange(), 10617 MethodParameterDifferentDefaultArgument) 10618 << FirstMethodType << FirstName << (I + 1) 10619 << FirstInit->getSourceRange(); 10620 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10621 SecondMethod->getSourceRange(), 10622 MethodParameterDifferentDefaultArgument) 10623 << SecondMethodType << SecondName << (I + 1) 10624 << SecondInit->getSourceRange(); 10625 ParameterMismatch = true; 10626 break; 10627 10628 } 10629 } 10630 10631 if (ParameterMismatch) { 10632 Diagnosed = true; 10633 break; 10634 } 10635 10636 const auto *FirstTemplateArgs = 10637 FirstMethod->getTemplateSpecializationArgs(); 10638 const auto *SecondTemplateArgs = 10639 SecondMethod->getTemplateSpecializationArgs(); 10640 10641 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10642 (!FirstTemplateArgs && SecondTemplateArgs)) { 10643 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10644 FirstMethod->getSourceRange(), 10645 MethodNoTemplateArguments) 10646 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10647 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10648 SecondMethod->getSourceRange(), 10649 MethodNoTemplateArguments) 10650 << SecondMethodType << SecondName 10651 << (SecondTemplateArgs != nullptr); 10652 10653 Diagnosed = true; 10654 break; 10655 } 10656 10657 if (FirstTemplateArgs && SecondTemplateArgs) { 10658 // Remove pack expansions from argument list. 10659 auto ExpandTemplateArgumentList = 10660 [](const TemplateArgumentList *TAL) { 10661 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10662 for (const TemplateArgument &TA : TAL->asArray()) { 10663 if (TA.getKind() != TemplateArgument::Pack) { 10664 ExpandedList.push_back(&TA); 10665 continue; 10666 } 10667 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10668 ExpandedList.push_back(&PackTA); 10669 } 10670 } 10671 return ExpandedList; 10672 }; 10673 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10674 ExpandTemplateArgumentList(FirstTemplateArgs); 10675 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10676 ExpandTemplateArgumentList(SecondTemplateArgs); 10677 10678 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10679 ODRDiagDeclError(FirstRecord, FirstModule, 10680 FirstMethod->getLocation(), 10681 FirstMethod->getSourceRange(), 10682 MethodDifferentNumberTemplateArguments) 10683 << FirstMethodType << FirstName 10684 << (unsigned)FirstExpandedList.size(); 10685 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10686 SecondMethod->getSourceRange(), 10687 MethodDifferentNumberTemplateArguments) 10688 << SecondMethodType << SecondName 10689 << (unsigned)SecondExpandedList.size(); 10690 10691 Diagnosed = true; 10692 break; 10693 } 10694 10695 bool TemplateArgumentMismatch = false; 10696 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10697 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10698 &SecondTA = *SecondExpandedList[i]; 10699 if (ComputeTemplateArgumentODRHash(FirstTA) == 10700 ComputeTemplateArgumentODRHash(SecondTA)) { 10701 continue; 10702 } 10703 10704 ODRDiagDeclError( 10705 FirstRecord, FirstModule, FirstMethod->getLocation(), 10706 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10707 << FirstMethodType << FirstName << FirstTA << i + 1; 10708 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10709 SecondMethod->getSourceRange(), 10710 MethodDifferentTemplateArgument) 10711 << SecondMethodType << SecondName << SecondTA << i + 1; 10712 10713 TemplateArgumentMismatch = true; 10714 break; 10715 } 10716 10717 if (TemplateArgumentMismatch) { 10718 Diagnosed = true; 10719 break; 10720 } 10721 } 10722 10723 // Compute the hash of the method as if it has no body. 10724 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10725 Hash.clear(); 10726 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10727 return Hash.CalculateHash(); 10728 }; 10729 10730 // Compare the hash generated to the hash stored. A difference means 10731 // that a body was present in the original source. Due to merging, 10732 // the stardard way of detecting a body will not work. 10733 const bool HasFirstBody = 10734 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10735 const bool HasSecondBody = 10736 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10737 10738 if (HasFirstBody != HasSecondBody) { 10739 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10740 FirstMethod->getSourceRange(), MethodSingleBody) 10741 << FirstMethodType << FirstName << HasFirstBody; 10742 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10743 SecondMethod->getSourceRange(), MethodSingleBody) 10744 << SecondMethodType << SecondName << HasSecondBody; 10745 Diagnosed = true; 10746 break; 10747 } 10748 10749 if (HasFirstBody && HasSecondBody) { 10750 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10751 FirstMethod->getSourceRange(), MethodDifferentBody) 10752 << FirstMethodType << FirstName; 10753 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10754 SecondMethod->getSourceRange(), MethodDifferentBody) 10755 << SecondMethodType << SecondName; 10756 Diagnosed = true; 10757 break; 10758 } 10759 10760 break; 10761 } 10762 case TypeAlias: 10763 case TypeDef: { 10764 Diagnosed = ODRDiagTypeDefOrAlias( 10765 FirstRecord, FirstModule, SecondModule, 10766 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10767 FirstDiffType == TypeAlias); 10768 break; 10769 } 10770 case Var: { 10771 Diagnosed = 10772 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10773 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10774 break; 10775 } 10776 case Friend: { 10777 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10778 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10779 10780 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10781 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10782 10783 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10784 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10785 10786 if (FirstND && SecondND) { 10787 ODRDiagDeclError(FirstRecord, FirstModule, 10788 FirstFriend->getFriendLoc(), 10789 FirstFriend->getSourceRange(), FriendFunction) 10790 << FirstND; 10791 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10792 SecondFriend->getSourceRange(), FriendFunction) 10793 << SecondND; 10794 10795 Diagnosed = true; 10796 break; 10797 } 10798 10799 if (FirstTSI && SecondTSI) { 10800 QualType FirstFriendType = FirstTSI->getType(); 10801 QualType SecondFriendType = SecondTSI->getType(); 10802 assert(ComputeQualTypeODRHash(FirstFriendType) != 10803 ComputeQualTypeODRHash(SecondFriendType)); 10804 ODRDiagDeclError(FirstRecord, FirstModule, 10805 FirstFriend->getFriendLoc(), 10806 FirstFriend->getSourceRange(), FriendType) 10807 << FirstFriendType; 10808 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10809 SecondFriend->getSourceRange(), FriendType) 10810 << SecondFriendType; 10811 Diagnosed = true; 10812 break; 10813 } 10814 10815 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10816 FirstFriend->getSourceRange(), FriendTypeFunction) 10817 << (FirstTSI == nullptr); 10818 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10819 SecondFriend->getSourceRange(), FriendTypeFunction) 10820 << (SecondTSI == nullptr); 10821 10822 Diagnosed = true; 10823 break; 10824 } 10825 case FunctionTemplate: { 10826 FunctionTemplateDecl *FirstTemplate = 10827 cast<FunctionTemplateDecl>(FirstDecl); 10828 FunctionTemplateDecl *SecondTemplate = 10829 cast<FunctionTemplateDecl>(SecondDecl); 10830 10831 TemplateParameterList *FirstTPL = 10832 FirstTemplate->getTemplateParameters(); 10833 TemplateParameterList *SecondTPL = 10834 SecondTemplate->getTemplateParameters(); 10835 10836 if (FirstTPL->size() != SecondTPL->size()) { 10837 ODRDiagDeclError(FirstRecord, FirstModule, 10838 FirstTemplate->getLocation(), 10839 FirstTemplate->getSourceRange(), 10840 FunctionTemplateDifferentNumberParameters) 10841 << FirstTemplate << FirstTPL->size(); 10842 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10843 SecondTemplate->getSourceRange(), 10844 FunctionTemplateDifferentNumberParameters) 10845 << SecondTemplate << SecondTPL->size(); 10846 10847 Diagnosed = true; 10848 break; 10849 } 10850 10851 bool ParameterMismatch = false; 10852 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10853 NamedDecl *FirstParam = FirstTPL->getParam(i); 10854 NamedDecl *SecondParam = SecondTPL->getParam(i); 10855 10856 if (FirstParam->getKind() != SecondParam->getKind()) { 10857 enum { 10858 TemplateTypeParameter, 10859 NonTypeTemplateParameter, 10860 TemplateTemplateParameter, 10861 }; 10862 auto GetParamType = [](NamedDecl *D) { 10863 switch (D->getKind()) { 10864 default: 10865 llvm_unreachable("Unexpected template parameter type"); 10866 case Decl::TemplateTypeParm: 10867 return TemplateTypeParameter; 10868 case Decl::NonTypeTemplateParm: 10869 return NonTypeTemplateParameter; 10870 case Decl::TemplateTemplateParm: 10871 return TemplateTemplateParameter; 10872 } 10873 }; 10874 10875 ODRDiagDeclError(FirstRecord, FirstModule, 10876 FirstTemplate->getLocation(), 10877 FirstTemplate->getSourceRange(), 10878 FunctionTemplateParameterDifferentKind) 10879 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10880 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10881 SecondTemplate->getSourceRange(), 10882 FunctionTemplateParameterDifferentKind) 10883 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10884 10885 ParameterMismatch = true; 10886 break; 10887 } 10888 10889 if (FirstParam->getName() != SecondParam->getName()) { 10890 ODRDiagDeclError( 10891 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10892 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10893 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10894 << FirstParam; 10895 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10896 SecondTemplate->getSourceRange(), 10897 FunctionTemplateParameterName) 10898 << SecondTemplate << (i + 1) 10899 << (bool)SecondParam->getIdentifier() << SecondParam; 10900 ParameterMismatch = true; 10901 break; 10902 } 10903 10904 if (isa<TemplateTypeParmDecl>(FirstParam) && 10905 isa<TemplateTypeParmDecl>(SecondParam)) { 10906 TemplateTypeParmDecl *FirstTTPD = 10907 cast<TemplateTypeParmDecl>(FirstParam); 10908 TemplateTypeParmDecl *SecondTTPD = 10909 cast<TemplateTypeParmDecl>(SecondParam); 10910 bool HasFirstDefaultArgument = 10911 FirstTTPD->hasDefaultArgument() && 10912 !FirstTTPD->defaultArgumentWasInherited(); 10913 bool HasSecondDefaultArgument = 10914 SecondTTPD->hasDefaultArgument() && 10915 !SecondTTPD->defaultArgumentWasInherited(); 10916 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10917 ODRDiagDeclError(FirstRecord, FirstModule, 10918 FirstTemplate->getLocation(), 10919 FirstTemplate->getSourceRange(), 10920 FunctionTemplateParameterSingleDefaultArgument) 10921 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10922 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10923 SecondTemplate->getSourceRange(), 10924 FunctionTemplateParameterSingleDefaultArgument) 10925 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10926 ParameterMismatch = true; 10927 break; 10928 } 10929 10930 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10931 QualType FirstType = FirstTTPD->getDefaultArgument(); 10932 QualType SecondType = SecondTTPD->getDefaultArgument(); 10933 if (ComputeQualTypeODRHash(FirstType) != 10934 ComputeQualTypeODRHash(SecondType)) { 10935 ODRDiagDeclError( 10936 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10937 FirstTemplate->getSourceRange(), 10938 FunctionTemplateParameterDifferentDefaultArgument) 10939 << FirstTemplate << (i + 1) << FirstType; 10940 ODRDiagDeclNote( 10941 SecondModule, SecondTemplate->getLocation(), 10942 SecondTemplate->getSourceRange(), 10943 FunctionTemplateParameterDifferentDefaultArgument) 10944 << SecondTemplate << (i + 1) << SecondType; 10945 ParameterMismatch = true; 10946 break; 10947 } 10948 } 10949 10950 if (FirstTTPD->isParameterPack() != 10951 SecondTTPD->isParameterPack()) { 10952 ODRDiagDeclError(FirstRecord, FirstModule, 10953 FirstTemplate->getLocation(), 10954 FirstTemplate->getSourceRange(), 10955 FunctionTemplatePackParameter) 10956 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10957 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10958 SecondTemplate->getSourceRange(), 10959 FunctionTemplatePackParameter) 10960 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10961 ParameterMismatch = true; 10962 break; 10963 } 10964 } 10965 10966 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10967 isa<TemplateTemplateParmDecl>(SecondParam)) { 10968 TemplateTemplateParmDecl *FirstTTPD = 10969 cast<TemplateTemplateParmDecl>(FirstParam); 10970 TemplateTemplateParmDecl *SecondTTPD = 10971 cast<TemplateTemplateParmDecl>(SecondParam); 10972 10973 TemplateParameterList *FirstTPL = 10974 FirstTTPD->getTemplateParameters(); 10975 TemplateParameterList *SecondTPL = 10976 SecondTTPD->getTemplateParameters(); 10977 10978 if (ComputeTemplateParameterListODRHash(FirstTPL) != 10979 ComputeTemplateParameterListODRHash(SecondTPL)) { 10980 ODRDiagDeclError(FirstRecord, FirstModule, 10981 FirstTemplate->getLocation(), 10982 FirstTemplate->getSourceRange(), 10983 FunctionTemplateParameterDifferentType) 10984 << FirstTemplate << (i + 1); 10985 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10986 SecondTemplate->getSourceRange(), 10987 FunctionTemplateParameterDifferentType) 10988 << SecondTemplate << (i + 1); 10989 ParameterMismatch = true; 10990 break; 10991 } 10992 10993 bool HasFirstDefaultArgument = 10994 FirstTTPD->hasDefaultArgument() && 10995 !FirstTTPD->defaultArgumentWasInherited(); 10996 bool HasSecondDefaultArgument = 10997 SecondTTPD->hasDefaultArgument() && 10998 !SecondTTPD->defaultArgumentWasInherited(); 10999 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11000 ODRDiagDeclError(FirstRecord, FirstModule, 11001 FirstTemplate->getLocation(), 11002 FirstTemplate->getSourceRange(), 11003 FunctionTemplateParameterSingleDefaultArgument) 11004 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11005 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11006 SecondTemplate->getSourceRange(), 11007 FunctionTemplateParameterSingleDefaultArgument) 11008 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11009 ParameterMismatch = true; 11010 break; 11011 } 11012 11013 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11014 TemplateArgument FirstTA = 11015 FirstTTPD->getDefaultArgument().getArgument(); 11016 TemplateArgument SecondTA = 11017 SecondTTPD->getDefaultArgument().getArgument(); 11018 if (ComputeTemplateArgumentODRHash(FirstTA) != 11019 ComputeTemplateArgumentODRHash(SecondTA)) { 11020 ODRDiagDeclError( 11021 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11022 FirstTemplate->getSourceRange(), 11023 FunctionTemplateParameterDifferentDefaultArgument) 11024 << FirstTemplate << (i + 1) << FirstTA; 11025 ODRDiagDeclNote( 11026 SecondModule, SecondTemplate->getLocation(), 11027 SecondTemplate->getSourceRange(), 11028 FunctionTemplateParameterDifferentDefaultArgument) 11029 << SecondTemplate << (i + 1) << SecondTA; 11030 ParameterMismatch = true; 11031 break; 11032 } 11033 } 11034 11035 if (FirstTTPD->isParameterPack() != 11036 SecondTTPD->isParameterPack()) { 11037 ODRDiagDeclError(FirstRecord, FirstModule, 11038 FirstTemplate->getLocation(), 11039 FirstTemplate->getSourceRange(), 11040 FunctionTemplatePackParameter) 11041 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11042 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11043 SecondTemplate->getSourceRange(), 11044 FunctionTemplatePackParameter) 11045 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11046 ParameterMismatch = true; 11047 break; 11048 } 11049 } 11050 11051 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11052 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11053 NonTypeTemplateParmDecl *FirstNTTPD = 11054 cast<NonTypeTemplateParmDecl>(FirstParam); 11055 NonTypeTemplateParmDecl *SecondNTTPD = 11056 cast<NonTypeTemplateParmDecl>(SecondParam); 11057 11058 QualType FirstType = FirstNTTPD->getType(); 11059 QualType SecondType = SecondNTTPD->getType(); 11060 if (ComputeQualTypeODRHash(FirstType) != 11061 ComputeQualTypeODRHash(SecondType)) { 11062 ODRDiagDeclError(FirstRecord, FirstModule, 11063 FirstTemplate->getLocation(), 11064 FirstTemplate->getSourceRange(), 11065 FunctionTemplateParameterDifferentType) 11066 << FirstTemplate << (i + 1); 11067 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11068 SecondTemplate->getSourceRange(), 11069 FunctionTemplateParameterDifferentType) 11070 << SecondTemplate << (i + 1); 11071 ParameterMismatch = true; 11072 break; 11073 } 11074 11075 bool HasFirstDefaultArgument = 11076 FirstNTTPD->hasDefaultArgument() && 11077 !FirstNTTPD->defaultArgumentWasInherited(); 11078 bool HasSecondDefaultArgument = 11079 SecondNTTPD->hasDefaultArgument() && 11080 !SecondNTTPD->defaultArgumentWasInherited(); 11081 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11082 ODRDiagDeclError(FirstRecord, FirstModule, 11083 FirstTemplate->getLocation(), 11084 FirstTemplate->getSourceRange(), 11085 FunctionTemplateParameterSingleDefaultArgument) 11086 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11087 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11088 SecondTemplate->getSourceRange(), 11089 FunctionTemplateParameterSingleDefaultArgument) 11090 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11091 ParameterMismatch = true; 11092 break; 11093 } 11094 11095 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11096 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11097 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11098 if (ComputeODRHash(FirstDefaultArgument) != 11099 ComputeODRHash(SecondDefaultArgument)) { 11100 ODRDiagDeclError( 11101 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11102 FirstTemplate->getSourceRange(), 11103 FunctionTemplateParameterDifferentDefaultArgument) 11104 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11105 ODRDiagDeclNote( 11106 SecondModule, SecondTemplate->getLocation(), 11107 SecondTemplate->getSourceRange(), 11108 FunctionTemplateParameterDifferentDefaultArgument) 11109 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11110 ParameterMismatch = true; 11111 break; 11112 } 11113 } 11114 11115 if (FirstNTTPD->isParameterPack() != 11116 SecondNTTPD->isParameterPack()) { 11117 ODRDiagDeclError(FirstRecord, FirstModule, 11118 FirstTemplate->getLocation(), 11119 FirstTemplate->getSourceRange(), 11120 FunctionTemplatePackParameter) 11121 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11122 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11123 SecondTemplate->getSourceRange(), 11124 FunctionTemplatePackParameter) 11125 << SecondTemplate << (i + 1) 11126 << SecondNTTPD->isParameterPack(); 11127 ParameterMismatch = true; 11128 break; 11129 } 11130 } 11131 } 11132 11133 if (ParameterMismatch) { 11134 Diagnosed = true; 11135 break; 11136 } 11137 11138 break; 11139 } 11140 } 11141 11142 if (Diagnosed) 11143 continue; 11144 11145 Diag(FirstDecl->getLocation(), 11146 diag::err_module_odr_violation_mismatch_decl_unknown) 11147 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11148 << FirstDecl->getSourceRange(); 11149 Diag(SecondDecl->getLocation(), 11150 diag::note_module_odr_violation_mismatch_decl_unknown) 11151 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11152 Diagnosed = true; 11153 } 11154 11155 if (!Diagnosed) { 11156 // All definitions are updates to the same declaration. This happens if a 11157 // module instantiates the declaration of a class template specialization 11158 // and two or more other modules instantiate its definition. 11159 // 11160 // FIXME: Indicate which modules had instantiations of this definition. 11161 // FIXME: How can this even happen? 11162 Diag(Merge.first->getLocation(), 11163 diag::err_module_odr_violation_different_instantiations) 11164 << Merge.first; 11165 } 11166 } 11167 11168 // Issue ODR failures diagnostics for functions. 11169 for (auto &Merge : FunctionOdrMergeFailures) { 11170 enum ODRFunctionDifference { 11171 ReturnType, 11172 ParameterName, 11173 ParameterType, 11174 ParameterSingleDefaultArgument, 11175 ParameterDifferentDefaultArgument, 11176 FunctionBody, 11177 }; 11178 11179 FunctionDecl *FirstFunction = Merge.first; 11180 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11181 11182 bool Diagnosed = false; 11183 for (auto &SecondFunction : Merge.second) { 11184 11185 if (FirstFunction == SecondFunction) 11186 continue; 11187 11188 std::string SecondModule = 11189 getOwningModuleNameForDiagnostic(SecondFunction); 11190 11191 auto ODRDiagError = [FirstFunction, &FirstModule, 11192 this](SourceLocation Loc, SourceRange Range, 11193 ODRFunctionDifference DiffType) { 11194 return Diag(Loc, diag::err_module_odr_violation_function) 11195 << FirstFunction << FirstModule.empty() << FirstModule << Range 11196 << DiffType; 11197 }; 11198 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11199 SourceRange Range, 11200 ODRFunctionDifference DiffType) { 11201 return Diag(Loc, diag::note_module_odr_violation_function) 11202 << SecondModule << Range << DiffType; 11203 }; 11204 11205 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11206 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11207 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11208 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11209 << FirstFunction->getReturnType(); 11210 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11211 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11212 << SecondFunction->getReturnType(); 11213 Diagnosed = true; 11214 break; 11215 } 11216 11217 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11218 "Merged functions with different number of parameters"); 11219 11220 auto ParamSize = FirstFunction->param_size(); 11221 bool ParameterMismatch = false; 11222 for (unsigned I = 0; I < ParamSize; ++I) { 11223 auto *FirstParam = FirstFunction->getParamDecl(I); 11224 auto *SecondParam = SecondFunction->getParamDecl(I); 11225 11226 assert(getContext().hasSameType(FirstParam->getType(), 11227 SecondParam->getType()) && 11228 "Merged function has different parameter types."); 11229 11230 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11231 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11232 ParameterName) 11233 << I + 1 << FirstParam->getDeclName(); 11234 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11235 ParameterName) 11236 << I + 1 << SecondParam->getDeclName(); 11237 ParameterMismatch = true; 11238 break; 11239 }; 11240 11241 QualType FirstParamType = FirstParam->getType(); 11242 QualType SecondParamType = SecondParam->getType(); 11243 if (FirstParamType != SecondParamType && 11244 ComputeQualTypeODRHash(FirstParamType) != 11245 ComputeQualTypeODRHash(SecondParamType)) { 11246 if (const DecayedType *ParamDecayedType = 11247 FirstParamType->getAs<DecayedType>()) { 11248 ODRDiagError(FirstParam->getLocation(), 11249 FirstParam->getSourceRange(), ParameterType) 11250 << (I + 1) << FirstParamType << true 11251 << ParamDecayedType->getOriginalType(); 11252 } else { 11253 ODRDiagError(FirstParam->getLocation(), 11254 FirstParam->getSourceRange(), ParameterType) 11255 << (I + 1) << FirstParamType << false; 11256 } 11257 11258 if (const DecayedType *ParamDecayedType = 11259 SecondParamType->getAs<DecayedType>()) { 11260 ODRDiagNote(SecondParam->getLocation(), 11261 SecondParam->getSourceRange(), ParameterType) 11262 << (I + 1) << SecondParamType << true 11263 << ParamDecayedType->getOriginalType(); 11264 } else { 11265 ODRDiagNote(SecondParam->getLocation(), 11266 SecondParam->getSourceRange(), ParameterType) 11267 << (I + 1) << SecondParamType << false; 11268 } 11269 ParameterMismatch = true; 11270 break; 11271 } 11272 11273 const Expr *FirstInit = FirstParam->getInit(); 11274 const Expr *SecondInit = SecondParam->getInit(); 11275 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11276 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11277 ParameterSingleDefaultArgument) 11278 << (I + 1) << (FirstInit == nullptr) 11279 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11280 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11281 ParameterSingleDefaultArgument) 11282 << (I + 1) << (SecondInit == nullptr) 11283 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11284 ParameterMismatch = true; 11285 break; 11286 } 11287 11288 if (FirstInit && SecondInit && 11289 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11290 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11291 ParameterDifferentDefaultArgument) 11292 << (I + 1) << FirstInit->getSourceRange(); 11293 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11294 ParameterDifferentDefaultArgument) 11295 << (I + 1) << SecondInit->getSourceRange(); 11296 ParameterMismatch = true; 11297 break; 11298 } 11299 11300 assert(ComputeSubDeclODRHash(FirstParam) == 11301 ComputeSubDeclODRHash(SecondParam) && 11302 "Undiagnosed parameter difference."); 11303 } 11304 11305 if (ParameterMismatch) { 11306 Diagnosed = true; 11307 break; 11308 } 11309 11310 // If no error has been generated before now, assume the problem is in 11311 // the body and generate a message. 11312 ODRDiagError(FirstFunction->getLocation(), 11313 FirstFunction->getSourceRange(), FunctionBody); 11314 ODRDiagNote(SecondFunction->getLocation(), 11315 SecondFunction->getSourceRange(), FunctionBody); 11316 Diagnosed = true; 11317 break; 11318 } 11319 (void)Diagnosed; 11320 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11321 } 11322 11323 // Issue ODR failures diagnostics for enums. 11324 for (auto &Merge : EnumOdrMergeFailures) { 11325 enum ODREnumDifference { 11326 SingleScopedEnum, 11327 EnumTagKeywordMismatch, 11328 SingleSpecifiedType, 11329 DifferentSpecifiedTypes, 11330 DifferentNumberEnumConstants, 11331 EnumConstantName, 11332 EnumConstantSingleInitilizer, 11333 EnumConstantDifferentInitilizer, 11334 }; 11335 11336 // If we've already pointed out a specific problem with this enum, don't 11337 // bother issuing a general "something's different" diagnostic. 11338 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11339 continue; 11340 11341 EnumDecl *FirstEnum = Merge.first; 11342 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11343 11344 using DeclHashes = 11345 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11346 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11347 DeclHashes &Hashes, EnumDecl *Enum) { 11348 for (auto *D : Enum->decls()) { 11349 // Due to decl merging, the first EnumDecl is the parent of 11350 // Decls in both records. 11351 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11352 continue; 11353 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11354 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11355 ComputeSubDeclODRHash(D)); 11356 } 11357 }; 11358 DeclHashes FirstHashes; 11359 PopulateHashes(FirstHashes, FirstEnum); 11360 bool Diagnosed = false; 11361 for (auto &SecondEnum : Merge.second) { 11362 11363 if (FirstEnum == SecondEnum) 11364 continue; 11365 11366 std::string SecondModule = 11367 getOwningModuleNameForDiagnostic(SecondEnum); 11368 11369 auto ODRDiagError = [FirstEnum, &FirstModule, 11370 this](SourceLocation Loc, SourceRange Range, 11371 ODREnumDifference DiffType) { 11372 return Diag(Loc, diag::err_module_odr_violation_enum) 11373 << FirstEnum << FirstModule.empty() << FirstModule << Range 11374 << DiffType; 11375 }; 11376 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11377 SourceRange Range, 11378 ODREnumDifference DiffType) { 11379 return Diag(Loc, diag::note_module_odr_violation_enum) 11380 << SecondModule << Range << DiffType; 11381 }; 11382 11383 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11384 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11385 SingleScopedEnum) 11386 << FirstEnum->isScoped(); 11387 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11388 SingleScopedEnum) 11389 << SecondEnum->isScoped(); 11390 Diagnosed = true; 11391 continue; 11392 } 11393 11394 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11395 if (FirstEnum->isScopedUsingClassTag() != 11396 SecondEnum->isScopedUsingClassTag()) { 11397 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11398 EnumTagKeywordMismatch) 11399 << FirstEnum->isScopedUsingClassTag(); 11400 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11401 EnumTagKeywordMismatch) 11402 << SecondEnum->isScopedUsingClassTag(); 11403 Diagnosed = true; 11404 continue; 11405 } 11406 } 11407 11408 QualType FirstUnderlyingType = 11409 FirstEnum->getIntegerTypeSourceInfo() 11410 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11411 : QualType(); 11412 QualType SecondUnderlyingType = 11413 SecondEnum->getIntegerTypeSourceInfo() 11414 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11415 : QualType(); 11416 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11417 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11418 SingleSpecifiedType) 11419 << !FirstUnderlyingType.isNull(); 11420 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11421 SingleSpecifiedType) 11422 << !SecondUnderlyingType.isNull(); 11423 Diagnosed = true; 11424 continue; 11425 } 11426 11427 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11428 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11429 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11430 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11431 DifferentSpecifiedTypes) 11432 << FirstUnderlyingType; 11433 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11434 DifferentSpecifiedTypes) 11435 << SecondUnderlyingType; 11436 Diagnosed = true; 11437 continue; 11438 } 11439 } 11440 11441 DeclHashes SecondHashes; 11442 PopulateHashes(SecondHashes, SecondEnum); 11443 11444 if (FirstHashes.size() != SecondHashes.size()) { 11445 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11446 DifferentNumberEnumConstants) 11447 << (int)FirstHashes.size(); 11448 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11449 DifferentNumberEnumConstants) 11450 << (int)SecondHashes.size(); 11451 Diagnosed = true; 11452 continue; 11453 } 11454 11455 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11456 if (FirstHashes[I].second == SecondHashes[I].second) 11457 continue; 11458 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11459 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11460 11461 if (FirstEnumConstant->getDeclName() != 11462 SecondEnumConstant->getDeclName()) { 11463 11464 ODRDiagError(FirstEnumConstant->getLocation(), 11465 FirstEnumConstant->getSourceRange(), EnumConstantName) 11466 << I + 1 << FirstEnumConstant; 11467 ODRDiagNote(SecondEnumConstant->getLocation(), 11468 SecondEnumConstant->getSourceRange(), EnumConstantName) 11469 << I + 1 << SecondEnumConstant; 11470 Diagnosed = true; 11471 break; 11472 } 11473 11474 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11475 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11476 if (!FirstInit && !SecondInit) 11477 continue; 11478 11479 if (!FirstInit || !SecondInit) { 11480 ODRDiagError(FirstEnumConstant->getLocation(), 11481 FirstEnumConstant->getSourceRange(), 11482 EnumConstantSingleInitilizer) 11483 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11484 ODRDiagNote(SecondEnumConstant->getLocation(), 11485 SecondEnumConstant->getSourceRange(), 11486 EnumConstantSingleInitilizer) 11487 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11488 Diagnosed = true; 11489 break; 11490 } 11491 11492 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11493 ODRDiagError(FirstEnumConstant->getLocation(), 11494 FirstEnumConstant->getSourceRange(), 11495 EnumConstantDifferentInitilizer) 11496 << I + 1 << FirstEnumConstant; 11497 ODRDiagNote(SecondEnumConstant->getLocation(), 11498 SecondEnumConstant->getSourceRange(), 11499 EnumConstantDifferentInitilizer) 11500 << I + 1 << SecondEnumConstant; 11501 Diagnosed = true; 11502 break; 11503 } 11504 } 11505 } 11506 11507 (void)Diagnosed; 11508 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11509 } 11510 } 11511 11512 void ASTReader::StartedDeserializing() { 11513 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11514 ReadTimer->startTimer(); 11515 } 11516 11517 void ASTReader::FinishedDeserializing() { 11518 assert(NumCurrentElementsDeserializing && 11519 "FinishedDeserializing not paired with StartedDeserializing"); 11520 if (NumCurrentElementsDeserializing == 1) { 11521 // We decrease NumCurrentElementsDeserializing only after pending actions 11522 // are finished, to avoid recursively re-calling finishPendingActions(). 11523 finishPendingActions(); 11524 } 11525 --NumCurrentElementsDeserializing; 11526 11527 if (NumCurrentElementsDeserializing == 0) { 11528 // Propagate exception specification and deduced type updates along 11529 // redeclaration chains. 11530 // 11531 // We do this now rather than in finishPendingActions because we want to 11532 // be able to walk the complete redeclaration chains of the updated decls. 11533 while (!PendingExceptionSpecUpdates.empty() || 11534 !PendingDeducedTypeUpdates.empty()) { 11535 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11536 PendingExceptionSpecUpdates.clear(); 11537 for (auto Update : ESUpdates) { 11538 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11539 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11540 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11541 if (auto *Listener = getContext().getASTMutationListener()) 11542 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11543 for (auto *Redecl : Update.second->redecls()) 11544 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11545 } 11546 11547 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11548 PendingDeducedTypeUpdates.clear(); 11549 for (auto Update : DTUpdates) { 11550 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11551 // FIXME: If the return type is already deduced, check that it matches. 11552 getContext().adjustDeducedFunctionResultType(Update.first, 11553 Update.second); 11554 } 11555 } 11556 11557 if (ReadTimer) 11558 ReadTimer->stopTimer(); 11559 11560 diagnoseOdrViolations(); 11561 11562 // We are not in recursive loading, so it's safe to pass the "interesting" 11563 // decls to the consumer. 11564 if (Consumer) 11565 PassInterestingDeclsToConsumer(); 11566 } 11567 } 11568 11569 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11570 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11571 // Remove any fake results before adding any real ones. 11572 auto It = PendingFakeLookupResults.find(II); 11573 if (It != PendingFakeLookupResults.end()) { 11574 for (auto *ND : It->second) 11575 SemaObj->IdResolver.RemoveDecl(ND); 11576 // FIXME: this works around module+PCH performance issue. 11577 // Rather than erase the result from the map, which is O(n), just clear 11578 // the vector of NamedDecls. 11579 It->second.clear(); 11580 } 11581 } 11582 11583 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11584 SemaObj->TUScope->AddDecl(D); 11585 } else if (SemaObj->TUScope) { 11586 // Adding the decl to IdResolver may have failed because it was already in 11587 // (even though it was not added in scope). If it is already in, make sure 11588 // it gets in the scope as well. 11589 if (std::find(SemaObj->IdResolver.begin(Name), 11590 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11591 SemaObj->TUScope->AddDecl(D); 11592 } 11593 } 11594 11595 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11596 ASTContext *Context, 11597 const PCHContainerReader &PCHContainerRdr, 11598 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11599 StringRef isysroot, bool DisableValidation, 11600 bool AllowASTWithCompilerErrors, 11601 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11602 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11603 std::unique_ptr<llvm::Timer> ReadTimer) 11604 : Listener(DisableValidation 11605 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11606 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11607 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11608 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11609 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11610 PCHContainerRdr, PP.getHeaderSearchInfo()), 11611 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11612 DisableValidation(DisableValidation), 11613 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11614 AllowConfigurationMismatch(AllowConfigurationMismatch), 11615 ValidateSystemInputs(ValidateSystemInputs), 11616 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11617 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11618 SourceMgr.setExternalSLocEntrySource(this); 11619 11620 for (const auto &Ext : Extensions) { 11621 auto BlockName = Ext->getExtensionMetadata().BlockName; 11622 auto Known = ModuleFileExtensions.find(BlockName); 11623 if (Known != ModuleFileExtensions.end()) { 11624 Diags.Report(diag::warn_duplicate_module_file_extension) 11625 << BlockName; 11626 continue; 11627 } 11628 11629 ModuleFileExtensions.insert({BlockName, Ext}); 11630 } 11631 } 11632 11633 ASTReader::~ASTReader() { 11634 if (OwnsDeserializationListener) 11635 delete DeserializationListener; 11636 } 11637 11638 IdentifierResolver &ASTReader::getIdResolver() { 11639 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11640 } 11641 11642 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11643 unsigned AbbrevID) { 11644 Idx = 0; 11645 Record.clear(); 11646 return Cursor.readRecord(AbbrevID, Record); 11647 } 11648 //===----------------------------------------------------------------------===// 11649 //// OMPClauseReader implementation 11650 ////===----------------------------------------------------------------------===// 11651 11652 // This has to be in namespace clang because it's friended by all 11653 // of the OMP clauses. 11654 namespace clang { 11655 11656 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11657 ASTRecordReader &Record; 11658 ASTContext &Context; 11659 11660 public: 11661 OMPClauseReader(ASTRecordReader &Record) 11662 : Record(Record), Context(Record.getContext()) {} 11663 #define GEN_CLANG_CLAUSE_CLASS 11664 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11665 #include "llvm/Frontend/OpenMP/OMP.inc" 11666 OMPClause *readClause(); 11667 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11668 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11669 }; 11670 11671 } // end namespace clang 11672 11673 OMPClause *ASTRecordReader::readOMPClause() { 11674 return OMPClauseReader(*this).readClause(); 11675 } 11676 11677 OMPClause *OMPClauseReader::readClause() { 11678 OMPClause *C = nullptr; 11679 switch (llvm::omp::Clause(Record.readInt())) { 11680 case llvm::omp::OMPC_if: 11681 C = new (Context) OMPIfClause(); 11682 break; 11683 case llvm::omp::OMPC_final: 11684 C = new (Context) OMPFinalClause(); 11685 break; 11686 case llvm::omp::OMPC_num_threads: 11687 C = new (Context) OMPNumThreadsClause(); 11688 break; 11689 case llvm::omp::OMPC_safelen: 11690 C = new (Context) OMPSafelenClause(); 11691 break; 11692 case llvm::omp::OMPC_simdlen: 11693 C = new (Context) OMPSimdlenClause(); 11694 break; 11695 case llvm::omp::OMPC_allocator: 11696 C = new (Context) OMPAllocatorClause(); 11697 break; 11698 case llvm::omp::OMPC_collapse: 11699 C = new (Context) OMPCollapseClause(); 11700 break; 11701 case llvm::omp::OMPC_default: 11702 C = new (Context) OMPDefaultClause(); 11703 break; 11704 case llvm::omp::OMPC_proc_bind: 11705 C = new (Context) OMPProcBindClause(); 11706 break; 11707 case llvm::omp::OMPC_schedule: 11708 C = new (Context) OMPScheduleClause(); 11709 break; 11710 case llvm::omp::OMPC_ordered: 11711 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11712 break; 11713 case llvm::omp::OMPC_nowait: 11714 C = new (Context) OMPNowaitClause(); 11715 break; 11716 case llvm::omp::OMPC_untied: 11717 C = new (Context) OMPUntiedClause(); 11718 break; 11719 case llvm::omp::OMPC_mergeable: 11720 C = new (Context) OMPMergeableClause(); 11721 break; 11722 case llvm::omp::OMPC_read: 11723 C = new (Context) OMPReadClause(); 11724 break; 11725 case llvm::omp::OMPC_write: 11726 C = new (Context) OMPWriteClause(); 11727 break; 11728 case llvm::omp::OMPC_update: 11729 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11730 break; 11731 case llvm::omp::OMPC_capture: 11732 C = new (Context) OMPCaptureClause(); 11733 break; 11734 case llvm::omp::OMPC_seq_cst: 11735 C = new (Context) OMPSeqCstClause(); 11736 break; 11737 case llvm::omp::OMPC_acq_rel: 11738 C = new (Context) OMPAcqRelClause(); 11739 break; 11740 case llvm::omp::OMPC_acquire: 11741 C = new (Context) OMPAcquireClause(); 11742 break; 11743 case llvm::omp::OMPC_release: 11744 C = new (Context) OMPReleaseClause(); 11745 break; 11746 case llvm::omp::OMPC_relaxed: 11747 C = new (Context) OMPRelaxedClause(); 11748 break; 11749 case llvm::omp::OMPC_threads: 11750 C = new (Context) OMPThreadsClause(); 11751 break; 11752 case llvm::omp::OMPC_simd: 11753 C = new (Context) OMPSIMDClause(); 11754 break; 11755 case llvm::omp::OMPC_nogroup: 11756 C = new (Context) OMPNogroupClause(); 11757 break; 11758 case llvm::omp::OMPC_unified_address: 11759 C = new (Context) OMPUnifiedAddressClause(); 11760 break; 11761 case llvm::omp::OMPC_unified_shared_memory: 11762 C = new (Context) OMPUnifiedSharedMemoryClause(); 11763 break; 11764 case llvm::omp::OMPC_reverse_offload: 11765 C = new (Context) OMPReverseOffloadClause(); 11766 break; 11767 case llvm::omp::OMPC_dynamic_allocators: 11768 C = new (Context) OMPDynamicAllocatorsClause(); 11769 break; 11770 case llvm::omp::OMPC_atomic_default_mem_order: 11771 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11772 break; 11773 case llvm::omp::OMPC_private: 11774 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11775 break; 11776 case llvm::omp::OMPC_firstprivate: 11777 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11778 break; 11779 case llvm::omp::OMPC_lastprivate: 11780 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11781 break; 11782 case llvm::omp::OMPC_shared: 11783 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11784 break; 11785 case llvm::omp::OMPC_reduction: { 11786 unsigned N = Record.readInt(); 11787 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11788 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11789 break; 11790 } 11791 case llvm::omp::OMPC_task_reduction: 11792 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11793 break; 11794 case llvm::omp::OMPC_in_reduction: 11795 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11796 break; 11797 case llvm::omp::OMPC_linear: 11798 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11799 break; 11800 case llvm::omp::OMPC_aligned: 11801 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11802 break; 11803 case llvm::omp::OMPC_copyin: 11804 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11805 break; 11806 case llvm::omp::OMPC_copyprivate: 11807 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11808 break; 11809 case llvm::omp::OMPC_flush: 11810 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11811 break; 11812 case llvm::omp::OMPC_depobj: 11813 C = OMPDepobjClause::CreateEmpty(Context); 11814 break; 11815 case llvm::omp::OMPC_depend: { 11816 unsigned NumVars = Record.readInt(); 11817 unsigned NumLoops = Record.readInt(); 11818 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11819 break; 11820 } 11821 case llvm::omp::OMPC_device: 11822 C = new (Context) OMPDeviceClause(); 11823 break; 11824 case llvm::omp::OMPC_map: { 11825 OMPMappableExprListSizeTy Sizes; 11826 Sizes.NumVars = Record.readInt(); 11827 Sizes.NumUniqueDeclarations = Record.readInt(); 11828 Sizes.NumComponentLists = Record.readInt(); 11829 Sizes.NumComponents = Record.readInt(); 11830 C = OMPMapClause::CreateEmpty(Context, Sizes); 11831 break; 11832 } 11833 case llvm::omp::OMPC_num_teams: 11834 C = new (Context) OMPNumTeamsClause(); 11835 break; 11836 case llvm::omp::OMPC_thread_limit: 11837 C = new (Context) OMPThreadLimitClause(); 11838 break; 11839 case llvm::omp::OMPC_priority: 11840 C = new (Context) OMPPriorityClause(); 11841 break; 11842 case llvm::omp::OMPC_grainsize: 11843 C = new (Context) OMPGrainsizeClause(); 11844 break; 11845 case llvm::omp::OMPC_num_tasks: 11846 C = new (Context) OMPNumTasksClause(); 11847 break; 11848 case llvm::omp::OMPC_hint: 11849 C = new (Context) OMPHintClause(); 11850 break; 11851 case llvm::omp::OMPC_dist_schedule: 11852 C = new (Context) OMPDistScheduleClause(); 11853 break; 11854 case llvm::omp::OMPC_defaultmap: 11855 C = new (Context) OMPDefaultmapClause(); 11856 break; 11857 case llvm::omp::OMPC_to: { 11858 OMPMappableExprListSizeTy Sizes; 11859 Sizes.NumVars = Record.readInt(); 11860 Sizes.NumUniqueDeclarations = Record.readInt(); 11861 Sizes.NumComponentLists = Record.readInt(); 11862 Sizes.NumComponents = Record.readInt(); 11863 C = OMPToClause::CreateEmpty(Context, Sizes); 11864 break; 11865 } 11866 case llvm::omp::OMPC_from: { 11867 OMPMappableExprListSizeTy Sizes; 11868 Sizes.NumVars = Record.readInt(); 11869 Sizes.NumUniqueDeclarations = Record.readInt(); 11870 Sizes.NumComponentLists = Record.readInt(); 11871 Sizes.NumComponents = Record.readInt(); 11872 C = OMPFromClause::CreateEmpty(Context, Sizes); 11873 break; 11874 } 11875 case llvm::omp::OMPC_use_device_ptr: { 11876 OMPMappableExprListSizeTy Sizes; 11877 Sizes.NumVars = Record.readInt(); 11878 Sizes.NumUniqueDeclarations = Record.readInt(); 11879 Sizes.NumComponentLists = Record.readInt(); 11880 Sizes.NumComponents = Record.readInt(); 11881 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11882 break; 11883 } 11884 case llvm::omp::OMPC_use_device_addr: { 11885 OMPMappableExprListSizeTy Sizes; 11886 Sizes.NumVars = Record.readInt(); 11887 Sizes.NumUniqueDeclarations = Record.readInt(); 11888 Sizes.NumComponentLists = Record.readInt(); 11889 Sizes.NumComponents = Record.readInt(); 11890 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11891 break; 11892 } 11893 case llvm::omp::OMPC_is_device_ptr: { 11894 OMPMappableExprListSizeTy Sizes; 11895 Sizes.NumVars = Record.readInt(); 11896 Sizes.NumUniqueDeclarations = Record.readInt(); 11897 Sizes.NumComponentLists = Record.readInt(); 11898 Sizes.NumComponents = Record.readInt(); 11899 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11900 break; 11901 } 11902 case llvm::omp::OMPC_allocate: 11903 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11904 break; 11905 case llvm::omp::OMPC_nontemporal: 11906 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11907 break; 11908 case llvm::omp::OMPC_inclusive: 11909 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11910 break; 11911 case llvm::omp::OMPC_exclusive: 11912 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11913 break; 11914 case llvm::omp::OMPC_order: 11915 C = new (Context) OMPOrderClause(); 11916 break; 11917 case llvm::omp::OMPC_destroy: 11918 C = new (Context) OMPDestroyClause(); 11919 break; 11920 case llvm::omp::OMPC_detach: 11921 C = new (Context) OMPDetachClause(); 11922 break; 11923 case llvm::omp::OMPC_uses_allocators: 11924 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11925 break; 11926 case llvm::omp::OMPC_affinity: 11927 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11928 break; 11929 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11930 case llvm::omp::Enum: \ 11931 break; 11932 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11933 default: 11934 break; 11935 } 11936 assert(C && "Unknown OMPClause type"); 11937 11938 Visit(C); 11939 C->setLocStart(Record.readSourceLocation()); 11940 C->setLocEnd(Record.readSourceLocation()); 11941 11942 return C; 11943 } 11944 11945 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11946 C->setPreInitStmt(Record.readSubStmt(), 11947 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11948 } 11949 11950 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11951 VisitOMPClauseWithPreInit(C); 11952 C->setPostUpdateExpr(Record.readSubExpr()); 11953 } 11954 11955 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 11956 VisitOMPClauseWithPreInit(C); 11957 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 11958 C->setNameModifierLoc(Record.readSourceLocation()); 11959 C->setColonLoc(Record.readSourceLocation()); 11960 C->setCondition(Record.readSubExpr()); 11961 C->setLParenLoc(Record.readSourceLocation()); 11962 } 11963 11964 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 11965 VisitOMPClauseWithPreInit(C); 11966 C->setCondition(Record.readSubExpr()); 11967 C->setLParenLoc(Record.readSourceLocation()); 11968 } 11969 11970 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 11971 VisitOMPClauseWithPreInit(C); 11972 C->setNumThreads(Record.readSubExpr()); 11973 C->setLParenLoc(Record.readSourceLocation()); 11974 } 11975 11976 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 11977 C->setSafelen(Record.readSubExpr()); 11978 C->setLParenLoc(Record.readSourceLocation()); 11979 } 11980 11981 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 11982 C->setSimdlen(Record.readSubExpr()); 11983 C->setLParenLoc(Record.readSourceLocation()); 11984 } 11985 11986 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 11987 C->setAllocator(Record.readExpr()); 11988 C->setLParenLoc(Record.readSourceLocation()); 11989 } 11990 11991 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 11992 C->setNumForLoops(Record.readSubExpr()); 11993 C->setLParenLoc(Record.readSourceLocation()); 11994 } 11995 11996 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 11997 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 11998 C->setLParenLoc(Record.readSourceLocation()); 11999 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12000 } 12001 12002 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12003 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12004 C->setLParenLoc(Record.readSourceLocation()); 12005 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12006 } 12007 12008 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12009 VisitOMPClauseWithPreInit(C); 12010 C->setScheduleKind( 12011 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12012 C->setFirstScheduleModifier( 12013 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12014 C->setSecondScheduleModifier( 12015 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12016 C->setChunkSize(Record.readSubExpr()); 12017 C->setLParenLoc(Record.readSourceLocation()); 12018 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12019 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12020 C->setScheduleKindLoc(Record.readSourceLocation()); 12021 C->setCommaLoc(Record.readSourceLocation()); 12022 } 12023 12024 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12025 C->setNumForLoops(Record.readSubExpr()); 12026 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12027 C->setLoopNumIterations(I, Record.readSubExpr()); 12028 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12029 C->setLoopCounter(I, Record.readSubExpr()); 12030 C->setLParenLoc(Record.readSourceLocation()); 12031 } 12032 12033 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12034 C->setEventHandler(Record.readSubExpr()); 12035 C->setLParenLoc(Record.readSourceLocation()); 12036 } 12037 12038 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12039 12040 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12041 12042 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12043 12044 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12045 12046 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12047 12048 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12049 if (C->isExtended()) { 12050 C->setLParenLoc(Record.readSourceLocation()); 12051 C->setArgumentLoc(Record.readSourceLocation()); 12052 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12053 } 12054 } 12055 12056 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12057 12058 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12059 12060 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12061 12062 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12063 12064 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12065 12066 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12067 12068 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12069 12070 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12071 12072 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12073 12074 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {} 12075 12076 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12077 12078 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12079 OMPUnifiedSharedMemoryClause *) {} 12080 12081 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12082 12083 void 12084 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12085 } 12086 12087 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12088 OMPAtomicDefaultMemOrderClause *C) { 12089 C->setAtomicDefaultMemOrderKind( 12090 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12091 C->setLParenLoc(Record.readSourceLocation()); 12092 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12093 } 12094 12095 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12096 C->setLParenLoc(Record.readSourceLocation()); 12097 unsigned NumVars = C->varlist_size(); 12098 SmallVector<Expr *, 16> Vars; 12099 Vars.reserve(NumVars); 12100 for (unsigned i = 0; i != NumVars; ++i) 12101 Vars.push_back(Record.readSubExpr()); 12102 C->setVarRefs(Vars); 12103 Vars.clear(); 12104 for (unsigned i = 0; i != NumVars; ++i) 12105 Vars.push_back(Record.readSubExpr()); 12106 C->setPrivateCopies(Vars); 12107 } 12108 12109 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12110 VisitOMPClauseWithPreInit(C); 12111 C->setLParenLoc(Record.readSourceLocation()); 12112 unsigned NumVars = C->varlist_size(); 12113 SmallVector<Expr *, 16> Vars; 12114 Vars.reserve(NumVars); 12115 for (unsigned i = 0; i != NumVars; ++i) 12116 Vars.push_back(Record.readSubExpr()); 12117 C->setVarRefs(Vars); 12118 Vars.clear(); 12119 for (unsigned i = 0; i != NumVars; ++i) 12120 Vars.push_back(Record.readSubExpr()); 12121 C->setPrivateCopies(Vars); 12122 Vars.clear(); 12123 for (unsigned i = 0; i != NumVars; ++i) 12124 Vars.push_back(Record.readSubExpr()); 12125 C->setInits(Vars); 12126 } 12127 12128 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12129 VisitOMPClauseWithPostUpdate(C); 12130 C->setLParenLoc(Record.readSourceLocation()); 12131 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12132 C->setKindLoc(Record.readSourceLocation()); 12133 C->setColonLoc(Record.readSourceLocation()); 12134 unsigned NumVars = C->varlist_size(); 12135 SmallVector<Expr *, 16> Vars; 12136 Vars.reserve(NumVars); 12137 for (unsigned i = 0; i != NumVars; ++i) 12138 Vars.push_back(Record.readSubExpr()); 12139 C->setVarRefs(Vars); 12140 Vars.clear(); 12141 for (unsigned i = 0; i != NumVars; ++i) 12142 Vars.push_back(Record.readSubExpr()); 12143 C->setPrivateCopies(Vars); 12144 Vars.clear(); 12145 for (unsigned i = 0; i != NumVars; ++i) 12146 Vars.push_back(Record.readSubExpr()); 12147 C->setSourceExprs(Vars); 12148 Vars.clear(); 12149 for (unsigned i = 0; i != NumVars; ++i) 12150 Vars.push_back(Record.readSubExpr()); 12151 C->setDestinationExprs(Vars); 12152 Vars.clear(); 12153 for (unsigned i = 0; i != NumVars; ++i) 12154 Vars.push_back(Record.readSubExpr()); 12155 C->setAssignmentOps(Vars); 12156 } 12157 12158 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12159 C->setLParenLoc(Record.readSourceLocation()); 12160 unsigned NumVars = C->varlist_size(); 12161 SmallVector<Expr *, 16> Vars; 12162 Vars.reserve(NumVars); 12163 for (unsigned i = 0; i != NumVars; ++i) 12164 Vars.push_back(Record.readSubExpr()); 12165 C->setVarRefs(Vars); 12166 } 12167 12168 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12169 VisitOMPClauseWithPostUpdate(C); 12170 C->setLParenLoc(Record.readSourceLocation()); 12171 C->setModifierLoc(Record.readSourceLocation()); 12172 C->setColonLoc(Record.readSourceLocation()); 12173 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12174 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12175 C->setQualifierLoc(NNSL); 12176 C->setNameInfo(DNI); 12177 12178 unsigned NumVars = C->varlist_size(); 12179 SmallVector<Expr *, 16> Vars; 12180 Vars.reserve(NumVars); 12181 for (unsigned i = 0; i != NumVars; ++i) 12182 Vars.push_back(Record.readSubExpr()); 12183 C->setVarRefs(Vars); 12184 Vars.clear(); 12185 for (unsigned i = 0; i != NumVars; ++i) 12186 Vars.push_back(Record.readSubExpr()); 12187 C->setPrivates(Vars); 12188 Vars.clear(); 12189 for (unsigned i = 0; i != NumVars; ++i) 12190 Vars.push_back(Record.readSubExpr()); 12191 C->setLHSExprs(Vars); 12192 Vars.clear(); 12193 for (unsigned i = 0; i != NumVars; ++i) 12194 Vars.push_back(Record.readSubExpr()); 12195 C->setRHSExprs(Vars); 12196 Vars.clear(); 12197 for (unsigned i = 0; i != NumVars; ++i) 12198 Vars.push_back(Record.readSubExpr()); 12199 C->setReductionOps(Vars); 12200 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12201 Vars.clear(); 12202 for (unsigned i = 0; i != NumVars; ++i) 12203 Vars.push_back(Record.readSubExpr()); 12204 C->setInscanCopyOps(Vars); 12205 Vars.clear(); 12206 for (unsigned i = 0; i != NumVars; ++i) 12207 Vars.push_back(Record.readSubExpr()); 12208 C->setInscanCopyArrayTemps(Vars); 12209 Vars.clear(); 12210 for (unsigned i = 0; i != NumVars; ++i) 12211 Vars.push_back(Record.readSubExpr()); 12212 C->setInscanCopyArrayElems(Vars); 12213 } 12214 } 12215 12216 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12217 VisitOMPClauseWithPostUpdate(C); 12218 C->setLParenLoc(Record.readSourceLocation()); 12219 C->setColonLoc(Record.readSourceLocation()); 12220 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12221 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12222 C->setQualifierLoc(NNSL); 12223 C->setNameInfo(DNI); 12224 12225 unsigned NumVars = C->varlist_size(); 12226 SmallVector<Expr *, 16> Vars; 12227 Vars.reserve(NumVars); 12228 for (unsigned I = 0; I != NumVars; ++I) 12229 Vars.push_back(Record.readSubExpr()); 12230 C->setVarRefs(Vars); 12231 Vars.clear(); 12232 for (unsigned I = 0; I != NumVars; ++I) 12233 Vars.push_back(Record.readSubExpr()); 12234 C->setPrivates(Vars); 12235 Vars.clear(); 12236 for (unsigned I = 0; I != NumVars; ++I) 12237 Vars.push_back(Record.readSubExpr()); 12238 C->setLHSExprs(Vars); 12239 Vars.clear(); 12240 for (unsigned I = 0; I != NumVars; ++I) 12241 Vars.push_back(Record.readSubExpr()); 12242 C->setRHSExprs(Vars); 12243 Vars.clear(); 12244 for (unsigned I = 0; I != NumVars; ++I) 12245 Vars.push_back(Record.readSubExpr()); 12246 C->setReductionOps(Vars); 12247 } 12248 12249 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12250 VisitOMPClauseWithPostUpdate(C); 12251 C->setLParenLoc(Record.readSourceLocation()); 12252 C->setColonLoc(Record.readSourceLocation()); 12253 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12254 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12255 C->setQualifierLoc(NNSL); 12256 C->setNameInfo(DNI); 12257 12258 unsigned NumVars = C->varlist_size(); 12259 SmallVector<Expr *, 16> Vars; 12260 Vars.reserve(NumVars); 12261 for (unsigned I = 0; I != NumVars; ++I) 12262 Vars.push_back(Record.readSubExpr()); 12263 C->setVarRefs(Vars); 12264 Vars.clear(); 12265 for (unsigned I = 0; I != NumVars; ++I) 12266 Vars.push_back(Record.readSubExpr()); 12267 C->setPrivates(Vars); 12268 Vars.clear(); 12269 for (unsigned I = 0; I != NumVars; ++I) 12270 Vars.push_back(Record.readSubExpr()); 12271 C->setLHSExprs(Vars); 12272 Vars.clear(); 12273 for (unsigned I = 0; I != NumVars; ++I) 12274 Vars.push_back(Record.readSubExpr()); 12275 C->setRHSExprs(Vars); 12276 Vars.clear(); 12277 for (unsigned I = 0; I != NumVars; ++I) 12278 Vars.push_back(Record.readSubExpr()); 12279 C->setReductionOps(Vars); 12280 Vars.clear(); 12281 for (unsigned I = 0; I != NumVars; ++I) 12282 Vars.push_back(Record.readSubExpr()); 12283 C->setTaskgroupDescriptors(Vars); 12284 } 12285 12286 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12287 VisitOMPClauseWithPostUpdate(C); 12288 C->setLParenLoc(Record.readSourceLocation()); 12289 C->setColonLoc(Record.readSourceLocation()); 12290 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12291 C->setModifierLoc(Record.readSourceLocation()); 12292 unsigned NumVars = C->varlist_size(); 12293 SmallVector<Expr *, 16> Vars; 12294 Vars.reserve(NumVars); 12295 for (unsigned i = 0; i != NumVars; ++i) 12296 Vars.push_back(Record.readSubExpr()); 12297 C->setVarRefs(Vars); 12298 Vars.clear(); 12299 for (unsigned i = 0; i != NumVars; ++i) 12300 Vars.push_back(Record.readSubExpr()); 12301 C->setPrivates(Vars); 12302 Vars.clear(); 12303 for (unsigned i = 0; i != NumVars; ++i) 12304 Vars.push_back(Record.readSubExpr()); 12305 C->setInits(Vars); 12306 Vars.clear(); 12307 for (unsigned i = 0; i != NumVars; ++i) 12308 Vars.push_back(Record.readSubExpr()); 12309 C->setUpdates(Vars); 12310 Vars.clear(); 12311 for (unsigned i = 0; i != NumVars; ++i) 12312 Vars.push_back(Record.readSubExpr()); 12313 C->setFinals(Vars); 12314 C->setStep(Record.readSubExpr()); 12315 C->setCalcStep(Record.readSubExpr()); 12316 Vars.clear(); 12317 for (unsigned I = 0; I != NumVars + 1; ++I) 12318 Vars.push_back(Record.readSubExpr()); 12319 C->setUsedExprs(Vars); 12320 } 12321 12322 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12323 C->setLParenLoc(Record.readSourceLocation()); 12324 C->setColonLoc(Record.readSourceLocation()); 12325 unsigned NumVars = C->varlist_size(); 12326 SmallVector<Expr *, 16> Vars; 12327 Vars.reserve(NumVars); 12328 for (unsigned i = 0; i != NumVars; ++i) 12329 Vars.push_back(Record.readSubExpr()); 12330 C->setVarRefs(Vars); 12331 C->setAlignment(Record.readSubExpr()); 12332 } 12333 12334 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12335 C->setLParenLoc(Record.readSourceLocation()); 12336 unsigned NumVars = C->varlist_size(); 12337 SmallVector<Expr *, 16> Exprs; 12338 Exprs.reserve(NumVars); 12339 for (unsigned i = 0; i != NumVars; ++i) 12340 Exprs.push_back(Record.readSubExpr()); 12341 C->setVarRefs(Exprs); 12342 Exprs.clear(); 12343 for (unsigned i = 0; i != NumVars; ++i) 12344 Exprs.push_back(Record.readSubExpr()); 12345 C->setSourceExprs(Exprs); 12346 Exprs.clear(); 12347 for (unsigned i = 0; i != NumVars; ++i) 12348 Exprs.push_back(Record.readSubExpr()); 12349 C->setDestinationExprs(Exprs); 12350 Exprs.clear(); 12351 for (unsigned i = 0; i != NumVars; ++i) 12352 Exprs.push_back(Record.readSubExpr()); 12353 C->setAssignmentOps(Exprs); 12354 } 12355 12356 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12357 C->setLParenLoc(Record.readSourceLocation()); 12358 unsigned NumVars = C->varlist_size(); 12359 SmallVector<Expr *, 16> Exprs; 12360 Exprs.reserve(NumVars); 12361 for (unsigned i = 0; i != NumVars; ++i) 12362 Exprs.push_back(Record.readSubExpr()); 12363 C->setVarRefs(Exprs); 12364 Exprs.clear(); 12365 for (unsigned i = 0; i != NumVars; ++i) 12366 Exprs.push_back(Record.readSubExpr()); 12367 C->setSourceExprs(Exprs); 12368 Exprs.clear(); 12369 for (unsigned i = 0; i != NumVars; ++i) 12370 Exprs.push_back(Record.readSubExpr()); 12371 C->setDestinationExprs(Exprs); 12372 Exprs.clear(); 12373 for (unsigned i = 0; i != NumVars; ++i) 12374 Exprs.push_back(Record.readSubExpr()); 12375 C->setAssignmentOps(Exprs); 12376 } 12377 12378 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12379 C->setLParenLoc(Record.readSourceLocation()); 12380 unsigned NumVars = C->varlist_size(); 12381 SmallVector<Expr *, 16> Vars; 12382 Vars.reserve(NumVars); 12383 for (unsigned i = 0; i != NumVars; ++i) 12384 Vars.push_back(Record.readSubExpr()); 12385 C->setVarRefs(Vars); 12386 } 12387 12388 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12389 C->setDepobj(Record.readSubExpr()); 12390 C->setLParenLoc(Record.readSourceLocation()); 12391 } 12392 12393 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12394 C->setLParenLoc(Record.readSourceLocation()); 12395 C->setModifier(Record.readSubExpr()); 12396 C->setDependencyKind( 12397 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12398 C->setDependencyLoc(Record.readSourceLocation()); 12399 C->setColonLoc(Record.readSourceLocation()); 12400 unsigned NumVars = C->varlist_size(); 12401 SmallVector<Expr *, 16> Vars; 12402 Vars.reserve(NumVars); 12403 for (unsigned I = 0; I != NumVars; ++I) 12404 Vars.push_back(Record.readSubExpr()); 12405 C->setVarRefs(Vars); 12406 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12407 C->setLoopData(I, Record.readSubExpr()); 12408 } 12409 12410 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12411 VisitOMPClauseWithPreInit(C); 12412 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12413 C->setDevice(Record.readSubExpr()); 12414 C->setModifierLoc(Record.readSourceLocation()); 12415 C->setLParenLoc(Record.readSourceLocation()); 12416 } 12417 12418 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12419 C->setLParenLoc(Record.readSourceLocation()); 12420 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12421 C->setMapTypeModifier( 12422 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12423 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12424 } 12425 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12426 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12427 C->setMapType( 12428 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12429 C->setMapLoc(Record.readSourceLocation()); 12430 C->setColonLoc(Record.readSourceLocation()); 12431 auto NumVars = C->varlist_size(); 12432 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12433 auto TotalLists = C->getTotalComponentListNum(); 12434 auto TotalComponents = C->getTotalComponentsNum(); 12435 12436 SmallVector<Expr *, 16> Vars; 12437 Vars.reserve(NumVars); 12438 for (unsigned i = 0; i != NumVars; ++i) 12439 Vars.push_back(Record.readExpr()); 12440 C->setVarRefs(Vars); 12441 12442 SmallVector<Expr *, 16> UDMappers; 12443 UDMappers.reserve(NumVars); 12444 for (unsigned I = 0; I < NumVars; ++I) 12445 UDMappers.push_back(Record.readExpr()); 12446 C->setUDMapperRefs(UDMappers); 12447 12448 SmallVector<ValueDecl *, 16> Decls; 12449 Decls.reserve(UniqueDecls); 12450 for (unsigned i = 0; i < UniqueDecls; ++i) 12451 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12452 C->setUniqueDecls(Decls); 12453 12454 SmallVector<unsigned, 16> ListsPerDecl; 12455 ListsPerDecl.reserve(UniqueDecls); 12456 for (unsigned i = 0; i < UniqueDecls; ++i) 12457 ListsPerDecl.push_back(Record.readInt()); 12458 C->setDeclNumLists(ListsPerDecl); 12459 12460 SmallVector<unsigned, 32> ListSizes; 12461 ListSizes.reserve(TotalLists); 12462 for (unsigned i = 0; i < TotalLists; ++i) 12463 ListSizes.push_back(Record.readInt()); 12464 C->setComponentListSizes(ListSizes); 12465 12466 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12467 Components.reserve(TotalComponents); 12468 for (unsigned i = 0; i < TotalComponents; ++i) { 12469 Expr *AssociatedExprPr = Record.readExpr(); 12470 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12471 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12472 /*IsNonContiguous=*/false); 12473 } 12474 C->setComponents(Components, ListSizes); 12475 } 12476 12477 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12478 C->setLParenLoc(Record.readSourceLocation()); 12479 C->setColonLoc(Record.readSourceLocation()); 12480 C->setAllocator(Record.readSubExpr()); 12481 unsigned NumVars = C->varlist_size(); 12482 SmallVector<Expr *, 16> Vars; 12483 Vars.reserve(NumVars); 12484 for (unsigned i = 0; i != NumVars; ++i) 12485 Vars.push_back(Record.readSubExpr()); 12486 C->setVarRefs(Vars); 12487 } 12488 12489 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12490 VisitOMPClauseWithPreInit(C); 12491 C->setNumTeams(Record.readSubExpr()); 12492 C->setLParenLoc(Record.readSourceLocation()); 12493 } 12494 12495 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12496 VisitOMPClauseWithPreInit(C); 12497 C->setThreadLimit(Record.readSubExpr()); 12498 C->setLParenLoc(Record.readSourceLocation()); 12499 } 12500 12501 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12502 VisitOMPClauseWithPreInit(C); 12503 C->setPriority(Record.readSubExpr()); 12504 C->setLParenLoc(Record.readSourceLocation()); 12505 } 12506 12507 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12508 VisitOMPClauseWithPreInit(C); 12509 C->setGrainsize(Record.readSubExpr()); 12510 C->setLParenLoc(Record.readSourceLocation()); 12511 } 12512 12513 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12514 VisitOMPClauseWithPreInit(C); 12515 C->setNumTasks(Record.readSubExpr()); 12516 C->setLParenLoc(Record.readSourceLocation()); 12517 } 12518 12519 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12520 C->setHint(Record.readSubExpr()); 12521 C->setLParenLoc(Record.readSourceLocation()); 12522 } 12523 12524 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12525 VisitOMPClauseWithPreInit(C); 12526 C->setDistScheduleKind( 12527 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12528 C->setChunkSize(Record.readSubExpr()); 12529 C->setLParenLoc(Record.readSourceLocation()); 12530 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12531 C->setCommaLoc(Record.readSourceLocation()); 12532 } 12533 12534 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12535 C->setDefaultmapKind( 12536 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12537 C->setDefaultmapModifier( 12538 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12539 C->setLParenLoc(Record.readSourceLocation()); 12540 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12541 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12542 } 12543 12544 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12545 C->setLParenLoc(Record.readSourceLocation()); 12546 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12547 C->setMotionModifier( 12548 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12549 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12550 } 12551 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12552 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12553 C->setColonLoc(Record.readSourceLocation()); 12554 auto NumVars = C->varlist_size(); 12555 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12556 auto TotalLists = C->getTotalComponentListNum(); 12557 auto TotalComponents = C->getTotalComponentsNum(); 12558 12559 SmallVector<Expr *, 16> Vars; 12560 Vars.reserve(NumVars); 12561 for (unsigned i = 0; i != NumVars; ++i) 12562 Vars.push_back(Record.readSubExpr()); 12563 C->setVarRefs(Vars); 12564 12565 SmallVector<Expr *, 16> UDMappers; 12566 UDMappers.reserve(NumVars); 12567 for (unsigned I = 0; I < NumVars; ++I) 12568 UDMappers.push_back(Record.readSubExpr()); 12569 C->setUDMapperRefs(UDMappers); 12570 12571 SmallVector<ValueDecl *, 16> Decls; 12572 Decls.reserve(UniqueDecls); 12573 for (unsigned i = 0; i < UniqueDecls; ++i) 12574 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12575 C->setUniqueDecls(Decls); 12576 12577 SmallVector<unsigned, 16> ListsPerDecl; 12578 ListsPerDecl.reserve(UniqueDecls); 12579 for (unsigned i = 0; i < UniqueDecls; ++i) 12580 ListsPerDecl.push_back(Record.readInt()); 12581 C->setDeclNumLists(ListsPerDecl); 12582 12583 SmallVector<unsigned, 32> ListSizes; 12584 ListSizes.reserve(TotalLists); 12585 for (unsigned i = 0; i < TotalLists; ++i) 12586 ListSizes.push_back(Record.readInt()); 12587 C->setComponentListSizes(ListSizes); 12588 12589 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12590 Components.reserve(TotalComponents); 12591 for (unsigned i = 0; i < TotalComponents; ++i) { 12592 Expr *AssociatedExprPr = Record.readSubExpr(); 12593 bool IsNonContiguous = Record.readBool(); 12594 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12595 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12596 } 12597 C->setComponents(Components, ListSizes); 12598 } 12599 12600 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12601 C->setLParenLoc(Record.readSourceLocation()); 12602 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12603 C->setMotionModifier( 12604 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12605 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12606 } 12607 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12608 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12609 C->setColonLoc(Record.readSourceLocation()); 12610 auto NumVars = C->varlist_size(); 12611 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12612 auto TotalLists = C->getTotalComponentListNum(); 12613 auto TotalComponents = C->getTotalComponentsNum(); 12614 12615 SmallVector<Expr *, 16> Vars; 12616 Vars.reserve(NumVars); 12617 for (unsigned i = 0; i != NumVars; ++i) 12618 Vars.push_back(Record.readSubExpr()); 12619 C->setVarRefs(Vars); 12620 12621 SmallVector<Expr *, 16> UDMappers; 12622 UDMappers.reserve(NumVars); 12623 for (unsigned I = 0; I < NumVars; ++I) 12624 UDMappers.push_back(Record.readSubExpr()); 12625 C->setUDMapperRefs(UDMappers); 12626 12627 SmallVector<ValueDecl *, 16> Decls; 12628 Decls.reserve(UniqueDecls); 12629 for (unsigned i = 0; i < UniqueDecls; ++i) 12630 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12631 C->setUniqueDecls(Decls); 12632 12633 SmallVector<unsigned, 16> ListsPerDecl; 12634 ListsPerDecl.reserve(UniqueDecls); 12635 for (unsigned i = 0; i < UniqueDecls; ++i) 12636 ListsPerDecl.push_back(Record.readInt()); 12637 C->setDeclNumLists(ListsPerDecl); 12638 12639 SmallVector<unsigned, 32> ListSizes; 12640 ListSizes.reserve(TotalLists); 12641 for (unsigned i = 0; i < TotalLists; ++i) 12642 ListSizes.push_back(Record.readInt()); 12643 C->setComponentListSizes(ListSizes); 12644 12645 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12646 Components.reserve(TotalComponents); 12647 for (unsigned i = 0; i < TotalComponents; ++i) { 12648 Expr *AssociatedExprPr = Record.readSubExpr(); 12649 bool IsNonContiguous = Record.readBool(); 12650 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12651 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12652 } 12653 C->setComponents(Components, ListSizes); 12654 } 12655 12656 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12657 C->setLParenLoc(Record.readSourceLocation()); 12658 auto NumVars = C->varlist_size(); 12659 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12660 auto TotalLists = C->getTotalComponentListNum(); 12661 auto TotalComponents = C->getTotalComponentsNum(); 12662 12663 SmallVector<Expr *, 16> Vars; 12664 Vars.reserve(NumVars); 12665 for (unsigned i = 0; i != NumVars; ++i) 12666 Vars.push_back(Record.readSubExpr()); 12667 C->setVarRefs(Vars); 12668 Vars.clear(); 12669 for (unsigned i = 0; i != NumVars; ++i) 12670 Vars.push_back(Record.readSubExpr()); 12671 C->setPrivateCopies(Vars); 12672 Vars.clear(); 12673 for (unsigned i = 0; i != NumVars; ++i) 12674 Vars.push_back(Record.readSubExpr()); 12675 C->setInits(Vars); 12676 12677 SmallVector<ValueDecl *, 16> Decls; 12678 Decls.reserve(UniqueDecls); 12679 for (unsigned i = 0; i < UniqueDecls; ++i) 12680 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12681 C->setUniqueDecls(Decls); 12682 12683 SmallVector<unsigned, 16> ListsPerDecl; 12684 ListsPerDecl.reserve(UniqueDecls); 12685 for (unsigned i = 0; i < UniqueDecls; ++i) 12686 ListsPerDecl.push_back(Record.readInt()); 12687 C->setDeclNumLists(ListsPerDecl); 12688 12689 SmallVector<unsigned, 32> ListSizes; 12690 ListSizes.reserve(TotalLists); 12691 for (unsigned i = 0; i < TotalLists; ++i) 12692 ListSizes.push_back(Record.readInt()); 12693 C->setComponentListSizes(ListSizes); 12694 12695 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12696 Components.reserve(TotalComponents); 12697 for (unsigned i = 0; i < TotalComponents; ++i) { 12698 auto *AssociatedExprPr = Record.readSubExpr(); 12699 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12700 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12701 /*IsNonContiguous=*/false); 12702 } 12703 C->setComponents(Components, ListSizes); 12704 } 12705 12706 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12707 C->setLParenLoc(Record.readSourceLocation()); 12708 auto NumVars = C->varlist_size(); 12709 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12710 auto TotalLists = C->getTotalComponentListNum(); 12711 auto TotalComponents = C->getTotalComponentsNum(); 12712 12713 SmallVector<Expr *, 16> Vars; 12714 Vars.reserve(NumVars); 12715 for (unsigned i = 0; i != NumVars; ++i) 12716 Vars.push_back(Record.readSubExpr()); 12717 C->setVarRefs(Vars); 12718 12719 SmallVector<ValueDecl *, 16> Decls; 12720 Decls.reserve(UniqueDecls); 12721 for (unsigned i = 0; i < UniqueDecls; ++i) 12722 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12723 C->setUniqueDecls(Decls); 12724 12725 SmallVector<unsigned, 16> ListsPerDecl; 12726 ListsPerDecl.reserve(UniqueDecls); 12727 for (unsigned i = 0; i < UniqueDecls; ++i) 12728 ListsPerDecl.push_back(Record.readInt()); 12729 C->setDeclNumLists(ListsPerDecl); 12730 12731 SmallVector<unsigned, 32> ListSizes; 12732 ListSizes.reserve(TotalLists); 12733 for (unsigned i = 0; i < TotalLists; ++i) 12734 ListSizes.push_back(Record.readInt()); 12735 C->setComponentListSizes(ListSizes); 12736 12737 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12738 Components.reserve(TotalComponents); 12739 for (unsigned i = 0; i < TotalComponents; ++i) { 12740 Expr *AssociatedExpr = Record.readSubExpr(); 12741 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12742 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12743 /*IsNonContiguous*/ false); 12744 } 12745 C->setComponents(Components, ListSizes); 12746 } 12747 12748 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12749 C->setLParenLoc(Record.readSourceLocation()); 12750 auto NumVars = C->varlist_size(); 12751 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12752 auto TotalLists = C->getTotalComponentListNum(); 12753 auto TotalComponents = C->getTotalComponentsNum(); 12754 12755 SmallVector<Expr *, 16> Vars; 12756 Vars.reserve(NumVars); 12757 for (unsigned i = 0; i != NumVars; ++i) 12758 Vars.push_back(Record.readSubExpr()); 12759 C->setVarRefs(Vars); 12760 Vars.clear(); 12761 12762 SmallVector<ValueDecl *, 16> Decls; 12763 Decls.reserve(UniqueDecls); 12764 for (unsigned i = 0; i < UniqueDecls; ++i) 12765 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12766 C->setUniqueDecls(Decls); 12767 12768 SmallVector<unsigned, 16> ListsPerDecl; 12769 ListsPerDecl.reserve(UniqueDecls); 12770 for (unsigned i = 0; i < UniqueDecls; ++i) 12771 ListsPerDecl.push_back(Record.readInt()); 12772 C->setDeclNumLists(ListsPerDecl); 12773 12774 SmallVector<unsigned, 32> ListSizes; 12775 ListSizes.reserve(TotalLists); 12776 for (unsigned i = 0; i < TotalLists; ++i) 12777 ListSizes.push_back(Record.readInt()); 12778 C->setComponentListSizes(ListSizes); 12779 12780 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12781 Components.reserve(TotalComponents); 12782 for (unsigned i = 0; i < TotalComponents; ++i) { 12783 Expr *AssociatedExpr = Record.readSubExpr(); 12784 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12785 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12786 /*IsNonContiguous=*/false); 12787 } 12788 C->setComponents(Components, ListSizes); 12789 } 12790 12791 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12792 C->setLParenLoc(Record.readSourceLocation()); 12793 unsigned NumVars = C->varlist_size(); 12794 SmallVector<Expr *, 16> Vars; 12795 Vars.reserve(NumVars); 12796 for (unsigned i = 0; i != NumVars; ++i) 12797 Vars.push_back(Record.readSubExpr()); 12798 C->setVarRefs(Vars); 12799 Vars.clear(); 12800 Vars.reserve(NumVars); 12801 for (unsigned i = 0; i != NumVars; ++i) 12802 Vars.push_back(Record.readSubExpr()); 12803 C->setPrivateRefs(Vars); 12804 } 12805 12806 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12807 C->setLParenLoc(Record.readSourceLocation()); 12808 unsigned NumVars = C->varlist_size(); 12809 SmallVector<Expr *, 16> Vars; 12810 Vars.reserve(NumVars); 12811 for (unsigned i = 0; i != NumVars; ++i) 12812 Vars.push_back(Record.readSubExpr()); 12813 C->setVarRefs(Vars); 12814 } 12815 12816 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12817 C->setLParenLoc(Record.readSourceLocation()); 12818 unsigned NumVars = C->varlist_size(); 12819 SmallVector<Expr *, 16> Vars; 12820 Vars.reserve(NumVars); 12821 for (unsigned i = 0; i != NumVars; ++i) 12822 Vars.push_back(Record.readSubExpr()); 12823 C->setVarRefs(Vars); 12824 } 12825 12826 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12827 C->setLParenLoc(Record.readSourceLocation()); 12828 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12829 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12830 Data.reserve(NumOfAllocators); 12831 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12832 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12833 D.Allocator = Record.readSubExpr(); 12834 D.AllocatorTraits = Record.readSubExpr(); 12835 D.LParenLoc = Record.readSourceLocation(); 12836 D.RParenLoc = Record.readSourceLocation(); 12837 } 12838 C->setAllocatorsData(Data); 12839 } 12840 12841 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12842 C->setLParenLoc(Record.readSourceLocation()); 12843 C->setModifier(Record.readSubExpr()); 12844 C->setColonLoc(Record.readSourceLocation()); 12845 unsigned NumOfLocators = C->varlist_size(); 12846 SmallVector<Expr *, 4> Locators; 12847 Locators.reserve(NumOfLocators); 12848 for (unsigned I = 0; I != NumOfLocators; ++I) 12849 Locators.push_back(Record.readSubExpr()); 12850 C->setVarRefs(Locators); 12851 } 12852 12853 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12854 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12855 C->setLParenLoc(Record.readSourceLocation()); 12856 C->setKindKwLoc(Record.readSourceLocation()); 12857 } 12858 12859 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12860 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12861 TI.Sets.resize(readUInt32()); 12862 for (auto &Set : TI.Sets) { 12863 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12864 Set.Selectors.resize(readUInt32()); 12865 for (auto &Selector : Set.Selectors) { 12866 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12867 Selector.ScoreOrCondition = nullptr; 12868 if (readBool()) 12869 Selector.ScoreOrCondition = readExprRef(); 12870 Selector.Properties.resize(readUInt32()); 12871 for (auto &Property : Selector.Properties) 12872 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12873 } 12874 } 12875 return &TI; 12876 } 12877 12878 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12879 if (!Data) 12880 return; 12881 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12882 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12883 skipInts(3); 12884 } 12885 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12886 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12887 Clauses[I] = readOMPClause(); 12888 Data->setClauses(Clauses); 12889 if (Data->hasAssociatedStmt()) 12890 Data->setAssociatedStmt(readStmt()); 12891 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12892 Data->getChildren()[I] = readStmt(); 12893 } 12894