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