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/LEB128.h" 118 #include "llvm/Support/MemoryBuffer.h" 119 #include "llvm/Support/Path.h" 120 #include "llvm/Support/SaveAndRestore.h" 121 #include "llvm/Support/Timer.h" 122 #include "llvm/Support/VersionTuple.h" 123 #include "llvm/Support/raw_ostream.h" 124 #include <algorithm> 125 #include <cassert> 126 #include <cstddef> 127 #include <cstdint> 128 #include <cstdio> 129 #include <ctime> 130 #include <iterator> 131 #include <limits> 132 #include <map> 133 #include <memory> 134 #include <string> 135 #include <system_error> 136 #include <tuple> 137 #include <utility> 138 #include <vector> 139 140 using namespace clang; 141 using namespace clang::serialization; 142 using namespace clang::serialization::reader; 143 using llvm::BitstreamCursor; 144 using llvm::RoundingMode; 145 146 //===----------------------------------------------------------------------===// 147 // ChainedASTReaderListener implementation 148 //===----------------------------------------------------------------------===// 149 150 bool 151 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 152 return First->ReadFullVersionInformation(FullVersion) || 153 Second->ReadFullVersionInformation(FullVersion); 154 } 155 156 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 157 First->ReadModuleName(ModuleName); 158 Second->ReadModuleName(ModuleName); 159 } 160 161 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 162 First->ReadModuleMapFile(ModuleMapPath); 163 Second->ReadModuleMapFile(ModuleMapPath); 164 } 165 166 bool 167 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 168 bool Complain, 169 bool AllowCompatibleDifferences) { 170 return First->ReadLanguageOptions(LangOpts, Complain, 171 AllowCompatibleDifferences) || 172 Second->ReadLanguageOptions(LangOpts, Complain, 173 AllowCompatibleDifferences); 174 } 175 176 bool ChainedASTReaderListener::ReadTargetOptions( 177 const TargetOptions &TargetOpts, bool Complain, 178 bool AllowCompatibleDifferences) { 179 return First->ReadTargetOptions(TargetOpts, Complain, 180 AllowCompatibleDifferences) || 181 Second->ReadTargetOptions(TargetOpts, Complain, 182 AllowCompatibleDifferences); 183 } 184 185 bool ChainedASTReaderListener::ReadDiagnosticOptions( 186 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 187 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 188 Second->ReadDiagnosticOptions(DiagOpts, Complain); 189 } 190 191 bool 192 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 193 bool Complain) { 194 return First->ReadFileSystemOptions(FSOpts, Complain) || 195 Second->ReadFileSystemOptions(FSOpts, Complain); 196 } 197 198 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 199 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 200 bool Complain) { 201 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 202 Complain) || 203 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 204 Complain); 205 } 206 207 bool ChainedASTReaderListener::ReadPreprocessorOptions( 208 const PreprocessorOptions &PPOpts, bool Complain, 209 std::string &SuggestedPredefines) { 210 return First->ReadPreprocessorOptions(PPOpts, Complain, 211 SuggestedPredefines) || 212 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 213 } 214 215 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 216 unsigned Value) { 217 First->ReadCounter(M, Value); 218 Second->ReadCounter(M, Value); 219 } 220 221 bool ChainedASTReaderListener::needsInputFileVisitation() { 222 return First->needsInputFileVisitation() || 223 Second->needsInputFileVisitation(); 224 } 225 226 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 227 return First->needsSystemInputFileVisitation() || 228 Second->needsSystemInputFileVisitation(); 229 } 230 231 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 232 ModuleKind Kind) { 233 First->visitModuleFile(Filename, Kind); 234 Second->visitModuleFile(Filename, Kind); 235 } 236 237 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 238 bool isSystem, 239 bool isOverridden, 240 bool isExplicitModule) { 241 bool Continue = false; 242 if (First->needsInputFileVisitation() && 243 (!isSystem || First->needsSystemInputFileVisitation())) 244 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 245 isExplicitModule); 246 if (Second->needsInputFileVisitation() && 247 (!isSystem || Second->needsSystemInputFileVisitation())) 248 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 249 isExplicitModule); 250 return Continue; 251 } 252 253 void ChainedASTReaderListener::readModuleFileExtension( 254 const ModuleFileExtensionMetadata &Metadata) { 255 First->readModuleFileExtension(Metadata); 256 Second->readModuleFileExtension(Metadata); 257 } 258 259 //===----------------------------------------------------------------------===// 260 // PCH validator implementation 261 //===----------------------------------------------------------------------===// 262 263 ASTReaderListener::~ASTReaderListener() = default; 264 265 /// Compare the given set of language options against an existing set of 266 /// language options. 267 /// 268 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 269 /// \param AllowCompatibleDifferences If true, differences between compatible 270 /// language options will be permitted. 271 /// 272 /// \returns true if the languagae options mis-match, false otherwise. 273 static bool checkLanguageOptions(const LangOptions &LangOpts, 274 const LangOptions &ExistingLangOpts, 275 DiagnosticsEngine *Diags, 276 bool AllowCompatibleDifferences = true) { 277 #define LANGOPT(Name, Bits, Default, Description) \ 278 if (ExistingLangOpts.Name != LangOpts.Name) { \ 279 if (Diags) \ 280 Diags->Report(diag::err_pch_langopt_mismatch) \ 281 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 282 return true; \ 283 } 284 285 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 286 if (ExistingLangOpts.Name != LangOpts.Name) { \ 287 if (Diags) \ 288 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 289 << Description; \ 290 return true; \ 291 } 292 293 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 294 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 295 if (Diags) \ 296 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 297 << Description; \ 298 return true; \ 299 } 300 301 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 302 if (!AllowCompatibleDifferences) \ 303 LANGOPT(Name, Bits, Default, Description) 304 305 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 306 if (!AllowCompatibleDifferences) \ 307 ENUM_LANGOPT(Name, Bits, Default, Description) 308 309 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 310 if (!AllowCompatibleDifferences) \ 311 VALUE_LANGOPT(Name, Bits, Default, Description) 312 313 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 314 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 315 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 316 #include "clang/Basic/LangOptions.def" 317 318 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 319 if (Diags) 320 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 321 return true; 322 } 323 324 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 325 if (Diags) 326 Diags->Report(diag::err_pch_langopt_value_mismatch) 327 << "target Objective-C runtime"; 328 return true; 329 } 330 331 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 332 LangOpts.CommentOpts.BlockCommandNames) { 333 if (Diags) 334 Diags->Report(diag::err_pch_langopt_value_mismatch) 335 << "block command names"; 336 return true; 337 } 338 339 // Sanitizer feature mismatches are treated as compatible differences. If 340 // compatible differences aren't allowed, we still only want to check for 341 // mismatches of non-modular sanitizers (the only ones which can affect AST 342 // generation). 343 if (!AllowCompatibleDifferences) { 344 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 345 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 346 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 347 ExistingSanitizers.clear(ModularSanitizers); 348 ImportedSanitizers.clear(ModularSanitizers); 349 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 350 const std::string Flag = "-fsanitize="; 351 if (Diags) { 352 #define SANITIZER(NAME, ID) \ 353 { \ 354 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 355 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 356 if (InExistingModule != InImportedModule) \ 357 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 358 << InExistingModule << (Flag + NAME); \ 359 } 360 #include "clang/Basic/Sanitizers.def" 361 } 362 return true; 363 } 364 } 365 366 return false; 367 } 368 369 /// Compare the given set of target options against an existing set of 370 /// target options. 371 /// 372 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 373 /// 374 /// \returns true if the target options mis-match, false otherwise. 375 static bool checkTargetOptions(const TargetOptions &TargetOpts, 376 const TargetOptions &ExistingTargetOpts, 377 DiagnosticsEngine *Diags, 378 bool AllowCompatibleDifferences = true) { 379 #define CHECK_TARGET_OPT(Field, Name) \ 380 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 381 if (Diags) \ 382 Diags->Report(diag::err_pch_targetopt_mismatch) \ 383 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 384 return true; \ 385 } 386 387 // The triple and ABI must match exactly. 388 CHECK_TARGET_OPT(Triple, "target"); 389 CHECK_TARGET_OPT(ABI, "target ABI"); 390 391 // We can tolerate different CPUs in many cases, notably when one CPU 392 // supports a strict superset of another. When allowing compatible 393 // differences skip this check. 394 if (!AllowCompatibleDifferences) { 395 CHECK_TARGET_OPT(CPU, "target CPU"); 396 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 397 } 398 399 #undef CHECK_TARGET_OPT 400 401 // Compare feature sets. 402 SmallVector<StringRef, 4> ExistingFeatures( 403 ExistingTargetOpts.FeaturesAsWritten.begin(), 404 ExistingTargetOpts.FeaturesAsWritten.end()); 405 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 406 TargetOpts.FeaturesAsWritten.end()); 407 llvm::sort(ExistingFeatures); 408 llvm::sort(ReadFeatures); 409 410 // We compute the set difference in both directions explicitly so that we can 411 // diagnose the differences differently. 412 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 413 std::set_difference( 414 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 415 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 416 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 417 ExistingFeatures.begin(), ExistingFeatures.end(), 418 std::back_inserter(UnmatchedReadFeatures)); 419 420 // If we are allowing compatible differences and the read feature set is 421 // a strict subset of the existing feature set, there is nothing to diagnose. 422 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 423 return false; 424 425 if (Diags) { 426 for (StringRef Feature : UnmatchedReadFeatures) 427 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 428 << /* is-existing-feature */ false << Feature; 429 for (StringRef Feature : UnmatchedExistingFeatures) 430 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 431 << /* is-existing-feature */ true << Feature; 432 } 433 434 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 435 } 436 437 bool 438 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 439 bool Complain, 440 bool AllowCompatibleDifferences) { 441 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 442 return checkLanguageOptions(LangOpts, ExistingLangOpts, 443 Complain ? &Reader.Diags : nullptr, 444 AllowCompatibleDifferences); 445 } 446 447 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 448 bool Complain, 449 bool AllowCompatibleDifferences) { 450 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 451 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 452 Complain ? &Reader.Diags : nullptr, 453 AllowCompatibleDifferences); 454 } 455 456 namespace { 457 458 using MacroDefinitionsMap = 459 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 460 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 461 462 } // namespace 463 464 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 465 DiagnosticsEngine &Diags, 466 bool Complain) { 467 using Level = DiagnosticsEngine::Level; 468 469 // Check current mappings for new -Werror mappings, and the stored mappings 470 // for cases that were explicitly mapped to *not* be errors that are now 471 // errors because of options like -Werror. 472 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 473 474 for (DiagnosticsEngine *MappingSource : MappingSources) { 475 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 476 diag::kind DiagID = DiagIDMappingPair.first; 477 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 478 if (CurLevel < DiagnosticsEngine::Error) 479 continue; // not significant 480 Level StoredLevel = 481 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 482 if (StoredLevel < DiagnosticsEngine::Error) { 483 if (Complain) 484 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 485 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 486 return true; 487 } 488 } 489 } 490 491 return false; 492 } 493 494 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 495 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 496 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 497 return true; 498 return Ext >= diag::Severity::Error; 499 } 500 501 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 502 DiagnosticsEngine &Diags, 503 bool IsSystem, bool Complain) { 504 // Top-level options 505 if (IsSystem) { 506 if (Diags.getSuppressSystemWarnings()) 507 return false; 508 // If -Wsystem-headers was not enabled before, be conservative 509 if (StoredDiags.getSuppressSystemWarnings()) { 510 if (Complain) 511 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 512 return true; 513 } 514 } 515 516 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 517 if (Complain) 518 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 519 return true; 520 } 521 522 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 523 !StoredDiags.getEnableAllWarnings()) { 524 if (Complain) 525 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 526 return true; 527 } 528 529 if (isExtHandlingFromDiagsError(Diags) && 530 !isExtHandlingFromDiagsError(StoredDiags)) { 531 if (Complain) 532 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 533 return true; 534 } 535 536 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 537 } 538 539 /// Return the top import module if it is implicit, nullptr otherwise. 540 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 541 Preprocessor &PP) { 542 // If the original import came from a file explicitly generated by the user, 543 // don't check the diagnostic mappings. 544 // FIXME: currently this is approximated by checking whether this is not a 545 // module import of an implicitly-loaded module file. 546 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 547 // the transitive closure of its imports, since unrelated modules cannot be 548 // imported until after this module finishes validation. 549 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 550 while (!TopImport->ImportedBy.empty()) 551 TopImport = TopImport->ImportedBy[0]; 552 if (TopImport->Kind != MK_ImplicitModule) 553 return nullptr; 554 555 StringRef ModuleName = TopImport->ModuleName; 556 assert(!ModuleName.empty() && "diagnostic options read before module name"); 557 558 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 559 assert(M && "missing module"); 560 return M; 561 } 562 563 bool PCHValidator::ReadDiagnosticOptions( 564 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 565 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 566 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 567 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 568 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 569 // This should never fail, because we would have processed these options 570 // before writing them to an ASTFile. 571 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 572 573 ModuleManager &ModuleMgr = Reader.getModuleManager(); 574 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 575 576 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 577 if (!TopM) 578 return false; 579 580 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 581 // contains the union of their flags. 582 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 583 Complain); 584 } 585 586 /// Collect the macro definitions provided by the given preprocessor 587 /// options. 588 static void 589 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 590 MacroDefinitionsMap &Macros, 591 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 592 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 593 StringRef Macro = PPOpts.Macros[I].first; 594 bool IsUndef = PPOpts.Macros[I].second; 595 596 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 597 StringRef MacroName = MacroPair.first; 598 StringRef MacroBody = MacroPair.second; 599 600 // For an #undef'd macro, we only care about the name. 601 if (IsUndef) { 602 if (MacroNames && !Macros.count(MacroName)) 603 MacroNames->push_back(MacroName); 604 605 Macros[MacroName] = std::make_pair("", true); 606 continue; 607 } 608 609 // For a #define'd macro, figure out the actual definition. 610 if (MacroName.size() == Macro.size()) 611 MacroBody = "1"; 612 else { 613 // Note: GCC drops anything following an end-of-line character. 614 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 615 MacroBody = MacroBody.substr(0, End); 616 } 617 618 if (MacroNames && !Macros.count(MacroName)) 619 MacroNames->push_back(MacroName); 620 Macros[MacroName] = std::make_pair(MacroBody, false); 621 } 622 } 623 624 /// Check the preprocessor options deserialized from the control block 625 /// against the preprocessor options in an existing preprocessor. 626 /// 627 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 628 /// \param Validate If true, validate preprocessor options. If false, allow 629 /// macros defined by \p ExistingPPOpts to override those defined by 630 /// \p PPOpts in SuggestedPredefines. 631 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 632 const PreprocessorOptions &ExistingPPOpts, 633 DiagnosticsEngine *Diags, 634 FileManager &FileMgr, 635 std::string &SuggestedPredefines, 636 const LangOptions &LangOpts, 637 bool Validate = true) { 638 // Check macro definitions. 639 MacroDefinitionsMap ASTFileMacros; 640 collectMacroDefinitions(PPOpts, ASTFileMacros); 641 MacroDefinitionsMap ExistingMacros; 642 SmallVector<StringRef, 4> ExistingMacroNames; 643 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 644 645 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 646 // Dig out the macro definition in the existing preprocessor options. 647 StringRef MacroName = ExistingMacroNames[I]; 648 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 649 650 // Check whether we know anything about this macro name or not. 651 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 652 ASTFileMacros.find(MacroName); 653 if (!Validate || Known == ASTFileMacros.end()) { 654 // FIXME: Check whether this identifier was referenced anywhere in the 655 // AST file. If so, we should reject the AST file. Unfortunately, this 656 // information isn't in the control block. What shall we do about it? 657 658 if (Existing.second) { 659 SuggestedPredefines += "#undef "; 660 SuggestedPredefines += MacroName.str(); 661 SuggestedPredefines += '\n'; 662 } else { 663 SuggestedPredefines += "#define "; 664 SuggestedPredefines += MacroName.str(); 665 SuggestedPredefines += ' '; 666 SuggestedPredefines += Existing.first.str(); 667 SuggestedPredefines += '\n'; 668 } 669 continue; 670 } 671 672 // If the macro was defined in one but undef'd in the other, we have a 673 // conflict. 674 if (Existing.second != Known->second.second) { 675 if (Diags) { 676 Diags->Report(diag::err_pch_macro_def_undef) 677 << MacroName << Known->second.second; 678 } 679 return true; 680 } 681 682 // If the macro was #undef'd in both, or if the macro bodies are identical, 683 // it's fine. 684 if (Existing.second || Existing.first == Known->second.first) 685 continue; 686 687 // The macro bodies differ; complain. 688 if (Diags) { 689 Diags->Report(diag::err_pch_macro_def_conflict) 690 << MacroName << Known->second.first << Existing.first; 691 } 692 return true; 693 } 694 695 // Check whether we're using predefines. 696 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 697 if (Diags) { 698 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 699 } 700 return true; 701 } 702 703 // Detailed record is important since it is used for the module cache hash. 704 if (LangOpts.Modules && 705 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 706 if (Diags) { 707 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 708 } 709 return true; 710 } 711 712 // Compute the #include and #include_macros lines we need. 713 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 714 StringRef File = ExistingPPOpts.Includes[I]; 715 716 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 717 !ExistingPPOpts.PCHThroughHeader.empty()) { 718 // In case the through header is an include, we must add all the includes 719 // to the predefines so the start point can be determined. 720 SuggestedPredefines += "#include \""; 721 SuggestedPredefines += File; 722 SuggestedPredefines += "\"\n"; 723 continue; 724 } 725 726 if (File == ExistingPPOpts.ImplicitPCHInclude) 727 continue; 728 729 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 730 != PPOpts.Includes.end()) 731 continue; 732 733 SuggestedPredefines += "#include \""; 734 SuggestedPredefines += File; 735 SuggestedPredefines += "\"\n"; 736 } 737 738 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 739 StringRef File = ExistingPPOpts.MacroIncludes[I]; 740 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 741 File) 742 != PPOpts.MacroIncludes.end()) 743 continue; 744 745 SuggestedPredefines += "#__include_macros \""; 746 SuggestedPredefines += File; 747 SuggestedPredefines += "\"\n##\n"; 748 } 749 750 return false; 751 } 752 753 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 754 bool Complain, 755 std::string &SuggestedPredefines) { 756 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 757 758 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 759 Complain? &Reader.Diags : nullptr, 760 PP.getFileManager(), 761 SuggestedPredefines, 762 PP.getLangOpts()); 763 } 764 765 bool SimpleASTReaderListener::ReadPreprocessorOptions( 766 const PreprocessorOptions &PPOpts, 767 bool Complain, 768 std::string &SuggestedPredefines) { 769 return checkPreprocessorOptions(PPOpts, 770 PP.getPreprocessorOpts(), 771 nullptr, 772 PP.getFileManager(), 773 SuggestedPredefines, 774 PP.getLangOpts(), 775 false); 776 } 777 778 /// Check the header search options deserialized from the control block 779 /// against the header search options in an existing preprocessor. 780 /// 781 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 782 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 783 StringRef SpecificModuleCachePath, 784 StringRef ExistingModuleCachePath, 785 DiagnosticsEngine *Diags, 786 const LangOptions &LangOpts) { 787 if (LangOpts.Modules) { 788 if (SpecificModuleCachePath != ExistingModuleCachePath) { 789 if (Diags) 790 Diags->Report(diag::err_pch_modulecache_mismatch) 791 << SpecificModuleCachePath << ExistingModuleCachePath; 792 return true; 793 } 794 } 795 796 return false; 797 } 798 799 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 800 StringRef SpecificModuleCachePath, 801 bool Complain) { 802 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 803 PP.getHeaderSearchInfo().getModuleCachePath(), 804 Complain ? &Reader.Diags : nullptr, 805 PP.getLangOpts()); 806 } 807 808 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 809 PP.setCounterValue(Value); 810 } 811 812 //===----------------------------------------------------------------------===// 813 // AST reader implementation 814 //===----------------------------------------------------------------------===// 815 816 static uint64_t readULEB(const unsigned char *&P) { 817 unsigned Length = 0; 818 const char *Error = nullptr; 819 820 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error); 821 if (Error) 822 llvm::report_fatal_error(Error); 823 P += Length; 824 return Val; 825 } 826 827 /// Read ULEB-encoded key length and data length. 828 static std::pair<unsigned, unsigned> 829 readULEBKeyDataLength(const unsigned char *&P) { 830 unsigned KeyLen = readULEB(P); 831 if ((unsigned)KeyLen != KeyLen) 832 llvm::report_fatal_error("key too large"); 833 834 unsigned DataLen = readULEB(P); 835 if ((unsigned)DataLen != DataLen) 836 llvm::report_fatal_error("data too large"); 837 838 return std::make_pair(KeyLen, DataLen); 839 } 840 841 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 842 bool TakeOwnership) { 843 DeserializationListener = Listener; 844 OwnsDeserializationListener = TakeOwnership; 845 } 846 847 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 848 return serialization::ComputeHash(Sel); 849 } 850 851 std::pair<unsigned, unsigned> 852 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 853 return readULEBKeyDataLength(d); 854 } 855 856 ASTSelectorLookupTrait::internal_key_type 857 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 858 using namespace llvm::support; 859 860 SelectorTable &SelTable = Reader.getContext().Selectors; 861 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 862 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 863 F, endian::readNext<uint32_t, little, unaligned>(d)); 864 if (N == 0) 865 return SelTable.getNullarySelector(FirstII); 866 else if (N == 1) 867 return SelTable.getUnarySelector(FirstII); 868 869 SmallVector<IdentifierInfo *, 16> Args; 870 Args.push_back(FirstII); 871 for (unsigned I = 1; I != N; ++I) 872 Args.push_back(Reader.getLocalIdentifier( 873 F, endian::readNext<uint32_t, little, unaligned>(d))); 874 875 return SelTable.getSelector(N, Args.data()); 876 } 877 878 ASTSelectorLookupTrait::data_type 879 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 880 unsigned DataLen) { 881 using namespace llvm::support; 882 883 data_type Result; 884 885 Result.ID = Reader.getGlobalSelectorID( 886 F, endian::readNext<uint32_t, little, unaligned>(d)); 887 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 888 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 889 Result.InstanceBits = FullInstanceBits & 0x3; 890 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 891 Result.FactoryBits = FullFactoryBits & 0x3; 892 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 893 unsigned NumInstanceMethods = FullInstanceBits >> 3; 894 unsigned NumFactoryMethods = FullFactoryBits >> 3; 895 896 // Load instance methods 897 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 898 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 899 F, endian::readNext<uint32_t, little, unaligned>(d))) 900 Result.Instance.push_back(Method); 901 } 902 903 // Load factory methods 904 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 905 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 906 F, endian::readNext<uint32_t, little, unaligned>(d))) 907 Result.Factory.push_back(Method); 908 } 909 910 return Result; 911 } 912 913 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 914 return llvm::djbHash(a); 915 } 916 917 std::pair<unsigned, unsigned> 918 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 919 return readULEBKeyDataLength(d); 920 } 921 922 ASTIdentifierLookupTraitBase::internal_key_type 923 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 924 assert(n >= 2 && d[n-1] == '\0'); 925 return StringRef((const char*) d, n-1); 926 } 927 928 /// Whether the given identifier is "interesting". 929 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 930 bool IsModule) { 931 return II.hadMacroDefinition() || II.isPoisoned() || 932 (!IsModule && II.getObjCOrBuiltinID()) || 933 II.hasRevertedTokenIDToIdentifier() || 934 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 935 II.getFETokenInfo()); 936 } 937 938 static bool readBit(unsigned &Bits) { 939 bool Value = Bits & 0x1; 940 Bits >>= 1; 941 return Value; 942 } 943 944 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 945 using namespace llvm::support; 946 947 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 948 return Reader.getGlobalIdentifierID(F, RawID >> 1); 949 } 950 951 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 952 if (!II.isFromAST()) { 953 II.setIsFromAST(); 954 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 955 if (isInterestingIdentifier(Reader, II, IsModule)) 956 II.setChangedSinceDeserialization(); 957 } 958 } 959 960 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 961 const unsigned char* d, 962 unsigned DataLen) { 963 using namespace llvm::support; 964 965 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 966 bool IsInteresting = RawID & 0x01; 967 968 // Wipe out the "is interesting" bit. 969 RawID = RawID >> 1; 970 971 // Build the IdentifierInfo and link the identifier ID with it. 972 IdentifierInfo *II = KnownII; 973 if (!II) { 974 II = &Reader.getIdentifierTable().getOwn(k); 975 KnownII = II; 976 } 977 markIdentifierFromAST(Reader, *II); 978 Reader.markIdentifierUpToDate(II); 979 980 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 981 if (!IsInteresting) { 982 // For uninteresting identifiers, there's nothing else to do. Just notify 983 // the reader that we've finished loading this identifier. 984 Reader.SetIdentifierInfo(ID, II); 985 return II; 986 } 987 988 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 989 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 990 bool CPlusPlusOperatorKeyword = readBit(Bits); 991 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 992 bool Poisoned = readBit(Bits); 993 bool ExtensionToken = readBit(Bits); 994 bool HadMacroDefinition = readBit(Bits); 995 996 assert(Bits == 0 && "Extra bits in the identifier?"); 997 DataLen -= 8; 998 999 // Set or check the various bits in the IdentifierInfo structure. 1000 // Token IDs are read-only. 1001 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 1002 II->revertTokenIDToIdentifier(); 1003 if (!F.isModule()) 1004 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 1005 assert(II->isExtensionToken() == ExtensionToken && 1006 "Incorrect extension token flag"); 1007 (void)ExtensionToken; 1008 if (Poisoned) 1009 II->setIsPoisoned(true); 1010 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 1011 "Incorrect C++ operator keyword flag"); 1012 (void)CPlusPlusOperatorKeyword; 1013 1014 // If this identifier is a macro, deserialize the macro 1015 // definition. 1016 if (HadMacroDefinition) { 1017 uint32_t MacroDirectivesOffset = 1018 endian::readNext<uint32_t, little, unaligned>(d); 1019 DataLen -= 4; 1020 1021 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1022 } 1023 1024 Reader.SetIdentifierInfo(ID, II); 1025 1026 // Read all of the declarations visible at global scope with this 1027 // name. 1028 if (DataLen > 0) { 1029 SmallVector<uint32_t, 4> DeclIDs; 1030 for (; DataLen > 0; DataLen -= 4) 1031 DeclIDs.push_back(Reader.getGlobalDeclID( 1032 F, endian::readNext<uint32_t, little, unaligned>(d))); 1033 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1034 } 1035 1036 return II; 1037 } 1038 1039 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1040 : Kind(Name.getNameKind()) { 1041 switch (Kind) { 1042 case DeclarationName::Identifier: 1043 Data = (uint64_t)Name.getAsIdentifierInfo(); 1044 break; 1045 case DeclarationName::ObjCZeroArgSelector: 1046 case DeclarationName::ObjCOneArgSelector: 1047 case DeclarationName::ObjCMultiArgSelector: 1048 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1049 break; 1050 case DeclarationName::CXXOperatorName: 1051 Data = Name.getCXXOverloadedOperator(); 1052 break; 1053 case DeclarationName::CXXLiteralOperatorName: 1054 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1055 break; 1056 case DeclarationName::CXXDeductionGuideName: 1057 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1058 ->getDeclName().getAsIdentifierInfo(); 1059 break; 1060 case DeclarationName::CXXConstructorName: 1061 case DeclarationName::CXXDestructorName: 1062 case DeclarationName::CXXConversionFunctionName: 1063 case DeclarationName::CXXUsingDirective: 1064 Data = 0; 1065 break; 1066 } 1067 } 1068 1069 unsigned DeclarationNameKey::getHash() const { 1070 llvm::FoldingSetNodeID ID; 1071 ID.AddInteger(Kind); 1072 1073 switch (Kind) { 1074 case DeclarationName::Identifier: 1075 case DeclarationName::CXXLiteralOperatorName: 1076 case DeclarationName::CXXDeductionGuideName: 1077 ID.AddString(((IdentifierInfo*)Data)->getName()); 1078 break; 1079 case DeclarationName::ObjCZeroArgSelector: 1080 case DeclarationName::ObjCOneArgSelector: 1081 case DeclarationName::ObjCMultiArgSelector: 1082 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1083 break; 1084 case DeclarationName::CXXOperatorName: 1085 ID.AddInteger((OverloadedOperatorKind)Data); 1086 break; 1087 case DeclarationName::CXXConstructorName: 1088 case DeclarationName::CXXDestructorName: 1089 case DeclarationName::CXXConversionFunctionName: 1090 case DeclarationName::CXXUsingDirective: 1091 break; 1092 } 1093 1094 return ID.ComputeHash(); 1095 } 1096 1097 ModuleFile * 1098 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1099 using namespace llvm::support; 1100 1101 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1102 return Reader.getLocalModuleFile(F, ModuleFileID); 1103 } 1104 1105 std::pair<unsigned, unsigned> 1106 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1107 return readULEBKeyDataLength(d); 1108 } 1109 1110 ASTDeclContextNameLookupTrait::internal_key_type 1111 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1112 using namespace llvm::support; 1113 1114 auto Kind = (DeclarationName::NameKind)*d++; 1115 uint64_t Data; 1116 switch (Kind) { 1117 case DeclarationName::Identifier: 1118 case DeclarationName::CXXLiteralOperatorName: 1119 case DeclarationName::CXXDeductionGuideName: 1120 Data = (uint64_t)Reader.getLocalIdentifier( 1121 F, endian::readNext<uint32_t, little, unaligned>(d)); 1122 break; 1123 case DeclarationName::ObjCZeroArgSelector: 1124 case DeclarationName::ObjCOneArgSelector: 1125 case DeclarationName::ObjCMultiArgSelector: 1126 Data = 1127 (uint64_t)Reader.getLocalSelector( 1128 F, endian::readNext<uint32_t, little, unaligned>( 1129 d)).getAsOpaquePtr(); 1130 break; 1131 case DeclarationName::CXXOperatorName: 1132 Data = *d++; // OverloadedOperatorKind 1133 break; 1134 case DeclarationName::CXXConstructorName: 1135 case DeclarationName::CXXDestructorName: 1136 case DeclarationName::CXXConversionFunctionName: 1137 case DeclarationName::CXXUsingDirective: 1138 Data = 0; 1139 break; 1140 } 1141 1142 return DeclarationNameKey(Kind, Data); 1143 } 1144 1145 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1146 const unsigned char *d, 1147 unsigned DataLen, 1148 data_type_builder &Val) { 1149 using namespace llvm::support; 1150 1151 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1152 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1153 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1154 } 1155 } 1156 1157 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1158 BitstreamCursor &Cursor, 1159 uint64_t Offset, 1160 DeclContext *DC) { 1161 assert(Offset != 0); 1162 1163 SavedStreamPosition SavedPosition(Cursor); 1164 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1165 Error(std::move(Err)); 1166 return true; 1167 } 1168 1169 RecordData Record; 1170 StringRef Blob; 1171 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1172 if (!MaybeCode) { 1173 Error(MaybeCode.takeError()); 1174 return true; 1175 } 1176 unsigned Code = MaybeCode.get(); 1177 1178 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1179 if (!MaybeRecCode) { 1180 Error(MaybeRecCode.takeError()); 1181 return true; 1182 } 1183 unsigned RecCode = MaybeRecCode.get(); 1184 if (RecCode != DECL_CONTEXT_LEXICAL) { 1185 Error("Expected lexical block"); 1186 return true; 1187 } 1188 1189 assert(!isa<TranslationUnitDecl>(DC) && 1190 "expected a TU_UPDATE_LEXICAL record for TU"); 1191 // If we are handling a C++ class template instantiation, we can see multiple 1192 // lexical updates for the same record. It's important that we select only one 1193 // of them, so that field numbering works properly. Just pick the first one we 1194 // see. 1195 auto &Lex = LexicalDecls[DC]; 1196 if (!Lex.first) { 1197 Lex = std::make_pair( 1198 &M, llvm::makeArrayRef( 1199 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1200 Blob.data()), 1201 Blob.size() / 4)); 1202 } 1203 DC->setHasExternalLexicalStorage(true); 1204 return false; 1205 } 1206 1207 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1208 BitstreamCursor &Cursor, 1209 uint64_t Offset, 1210 DeclID ID) { 1211 assert(Offset != 0); 1212 1213 SavedStreamPosition SavedPosition(Cursor); 1214 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1215 Error(std::move(Err)); 1216 return true; 1217 } 1218 1219 RecordData Record; 1220 StringRef Blob; 1221 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1222 if (!MaybeCode) { 1223 Error(MaybeCode.takeError()); 1224 return true; 1225 } 1226 unsigned Code = MaybeCode.get(); 1227 1228 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1229 if (!MaybeRecCode) { 1230 Error(MaybeRecCode.takeError()); 1231 return true; 1232 } 1233 unsigned RecCode = MaybeRecCode.get(); 1234 if (RecCode != DECL_CONTEXT_VISIBLE) { 1235 Error("Expected visible lookup table block"); 1236 return true; 1237 } 1238 1239 // We can't safely determine the primary context yet, so delay attaching the 1240 // lookup table until we're done with recursive deserialization. 1241 auto *Data = (const unsigned char*)Blob.data(); 1242 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1243 return false; 1244 } 1245 1246 void ASTReader::Error(StringRef Msg) const { 1247 Error(diag::err_fe_pch_malformed, Msg); 1248 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1249 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1250 Diag(diag::note_module_cache_path) 1251 << PP.getHeaderSearchInfo().getModuleCachePath(); 1252 } 1253 } 1254 1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1256 StringRef Arg3) const { 1257 if (Diags.isDiagnosticInFlight()) 1258 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1259 else 1260 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1261 } 1262 1263 void ASTReader::Error(llvm::Error &&Err) const { 1264 Error(toString(std::move(Err))); 1265 } 1266 1267 //===----------------------------------------------------------------------===// 1268 // Source Manager Deserialization 1269 //===----------------------------------------------------------------------===// 1270 1271 /// Read the line table in the source manager block. 1272 /// \returns true if there was an error. 1273 bool ASTReader::ParseLineTable(ModuleFile &F, 1274 const RecordData &Record) { 1275 unsigned Idx = 0; 1276 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1277 1278 // Parse the file names 1279 std::map<int, int> FileIDs; 1280 FileIDs[-1] = -1; // For unspecified filenames. 1281 for (unsigned I = 0; Record[Idx]; ++I) { 1282 // Extract the file name 1283 auto Filename = ReadPath(F, Record, Idx); 1284 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1285 } 1286 ++Idx; 1287 1288 // Parse the line entries 1289 std::vector<LineEntry> Entries; 1290 while (Idx < Record.size()) { 1291 int FID = Record[Idx++]; 1292 assert(FID >= 0 && "Serialized line entries for non-local file."); 1293 // Remap FileID from 1-based old view. 1294 FID += F.SLocEntryBaseID - 1; 1295 1296 // Extract the line entries 1297 unsigned NumEntries = Record[Idx++]; 1298 assert(NumEntries && "no line entries for file ID"); 1299 Entries.clear(); 1300 Entries.reserve(NumEntries); 1301 for (unsigned I = 0; I != NumEntries; ++I) { 1302 unsigned FileOffset = Record[Idx++]; 1303 unsigned LineNo = Record[Idx++]; 1304 int FilenameID = FileIDs[Record[Idx++]]; 1305 SrcMgr::CharacteristicKind FileKind 1306 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1307 unsigned IncludeOffset = Record[Idx++]; 1308 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1309 FileKind, IncludeOffset)); 1310 } 1311 LineTable.AddEntry(FileID::get(FID), Entries); 1312 } 1313 1314 return false; 1315 } 1316 1317 /// Read a source manager block 1318 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1319 using namespace SrcMgr; 1320 1321 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1322 1323 // Set the source-location entry cursor to the current position in 1324 // the stream. This cursor will be used to read the contents of the 1325 // source manager block initially, and then lazily read 1326 // source-location entries as needed. 1327 SLocEntryCursor = F.Stream; 1328 1329 // The stream itself is going to skip over the source manager block. 1330 if (llvm::Error Err = F.Stream.SkipBlock()) { 1331 Error(std::move(Err)); 1332 return true; 1333 } 1334 1335 // Enter the source manager block. 1336 if (llvm::Error Err = 1337 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1338 Error(std::move(Err)); 1339 return true; 1340 } 1341 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1342 1343 RecordData Record; 1344 while (true) { 1345 Expected<llvm::BitstreamEntry> MaybeE = 1346 SLocEntryCursor.advanceSkippingSubblocks(); 1347 if (!MaybeE) { 1348 Error(MaybeE.takeError()); 1349 return true; 1350 } 1351 llvm::BitstreamEntry E = MaybeE.get(); 1352 1353 switch (E.Kind) { 1354 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1355 case llvm::BitstreamEntry::Error: 1356 Error("malformed block record in AST file"); 1357 return true; 1358 case llvm::BitstreamEntry::EndBlock: 1359 return false; 1360 case llvm::BitstreamEntry::Record: 1361 // The interesting case. 1362 break; 1363 } 1364 1365 // Read a record. 1366 Record.clear(); 1367 StringRef Blob; 1368 Expected<unsigned> MaybeRecord = 1369 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1370 if (!MaybeRecord) { 1371 Error(MaybeRecord.takeError()); 1372 return true; 1373 } 1374 switch (MaybeRecord.get()) { 1375 default: // Default behavior: ignore. 1376 break; 1377 1378 case SM_SLOC_FILE_ENTRY: 1379 case SM_SLOC_BUFFER_ENTRY: 1380 case SM_SLOC_EXPANSION_ENTRY: 1381 // Once we hit one of the source location entries, we're done. 1382 return false; 1383 } 1384 } 1385 } 1386 1387 /// If a header file is not found at the path that we expect it to be 1388 /// and the PCH file was moved from its original location, try to resolve the 1389 /// file by assuming that header+PCH were moved together and the header is in 1390 /// the same place relative to the PCH. 1391 static std::string 1392 resolveFileRelativeToOriginalDir(const std::string &Filename, 1393 const std::string &OriginalDir, 1394 const std::string &CurrDir) { 1395 assert(OriginalDir != CurrDir && 1396 "No point trying to resolve the file if the PCH dir didn't change"); 1397 1398 using namespace llvm::sys; 1399 1400 SmallString<128> filePath(Filename); 1401 fs::make_absolute(filePath); 1402 assert(path::is_absolute(OriginalDir)); 1403 SmallString<128> currPCHPath(CurrDir); 1404 1405 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1406 fileDirE = path::end(path::parent_path(filePath)); 1407 path::const_iterator origDirI = path::begin(OriginalDir), 1408 origDirE = path::end(OriginalDir); 1409 // Skip the common path components from filePath and OriginalDir. 1410 while (fileDirI != fileDirE && origDirI != origDirE && 1411 *fileDirI == *origDirI) { 1412 ++fileDirI; 1413 ++origDirI; 1414 } 1415 for (; origDirI != origDirE; ++origDirI) 1416 path::append(currPCHPath, ".."); 1417 path::append(currPCHPath, fileDirI, fileDirE); 1418 path::append(currPCHPath, path::filename(Filename)); 1419 return std::string(currPCHPath.str()); 1420 } 1421 1422 bool ASTReader::ReadSLocEntry(int ID) { 1423 if (ID == 0) 1424 return false; 1425 1426 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1427 Error("source location entry ID out-of-range for AST file"); 1428 return true; 1429 } 1430 1431 // Local helper to read the (possibly-compressed) buffer data following the 1432 // entry record. 1433 auto ReadBuffer = [this]( 1434 BitstreamCursor &SLocEntryCursor, 1435 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1436 RecordData Record; 1437 StringRef Blob; 1438 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1439 if (!MaybeCode) { 1440 Error(MaybeCode.takeError()); 1441 return nullptr; 1442 } 1443 unsigned Code = MaybeCode.get(); 1444 1445 Expected<unsigned> MaybeRecCode = 1446 SLocEntryCursor.readRecord(Code, Record, &Blob); 1447 if (!MaybeRecCode) { 1448 Error(MaybeRecCode.takeError()); 1449 return nullptr; 1450 } 1451 unsigned RecCode = MaybeRecCode.get(); 1452 1453 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1454 if (!llvm::zlib::isAvailable()) { 1455 Error("zlib is not available"); 1456 return nullptr; 1457 } 1458 SmallString<0> Uncompressed; 1459 if (llvm::Error E = 1460 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1461 Error("could not decompress embedded file contents: " + 1462 llvm::toString(std::move(E))); 1463 return nullptr; 1464 } 1465 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1466 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1467 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1468 } else { 1469 Error("AST record has invalid code"); 1470 return nullptr; 1471 } 1472 }; 1473 1474 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1475 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1476 F->SLocEntryOffsetsBase + 1477 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1478 Error(std::move(Err)); 1479 return true; 1480 } 1481 1482 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1483 unsigned BaseOffset = F->SLocEntryBaseOffset; 1484 1485 ++NumSLocEntriesRead; 1486 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1487 if (!MaybeEntry) { 1488 Error(MaybeEntry.takeError()); 1489 return true; 1490 } 1491 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1492 1493 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1494 Error("incorrectly-formatted source location entry in AST file"); 1495 return true; 1496 } 1497 1498 RecordData Record; 1499 StringRef Blob; 1500 Expected<unsigned> MaybeSLOC = 1501 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1502 if (!MaybeSLOC) { 1503 Error(MaybeSLOC.takeError()); 1504 return true; 1505 } 1506 switch (MaybeSLOC.get()) { 1507 default: 1508 Error("incorrectly-formatted source location entry in AST file"); 1509 return true; 1510 1511 case SM_SLOC_FILE_ENTRY: { 1512 // We will detect whether a file changed and return 'Failure' for it, but 1513 // we will also try to fail gracefully by setting up the SLocEntry. 1514 unsigned InputID = Record[4]; 1515 InputFile IF = getInputFile(*F, InputID); 1516 Optional<FileEntryRef> File = IF.getFile(); 1517 bool OverriddenBuffer = IF.isOverridden(); 1518 1519 // Note that we only check if a File was returned. If it was out-of-date 1520 // we have complained but we will continue creating a FileID to recover 1521 // gracefully. 1522 if (!File) 1523 return true; 1524 1525 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1526 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1527 // This is the module's main file. 1528 IncludeLoc = getImportLocation(F); 1529 } 1530 SrcMgr::CharacteristicKind 1531 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1532 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID, 1533 BaseOffset + Record[0]); 1534 SrcMgr::FileInfo &FileInfo = 1535 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1536 FileInfo.NumCreatedFIDs = Record[5]; 1537 if (Record[3]) 1538 FileInfo.setHasLineDirectives(); 1539 1540 unsigned NumFileDecls = Record[7]; 1541 if (NumFileDecls && ContextObj) { 1542 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1543 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1544 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1545 NumFileDecls)); 1546 } 1547 1548 const SrcMgr::ContentCache &ContentCache = 1549 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter)); 1550 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1551 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1552 !ContentCache.getBufferIfLoaded()) { 1553 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1554 if (!Buffer) 1555 return true; 1556 SourceMgr.overrideFileContents(*File, std::move(Buffer)); 1557 } 1558 1559 break; 1560 } 1561 1562 case SM_SLOC_BUFFER_ENTRY: { 1563 const char *Name = Blob.data(); 1564 unsigned Offset = Record[0]; 1565 SrcMgr::CharacteristicKind 1566 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1567 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1568 if (IncludeLoc.isInvalid() && F->isModule()) { 1569 IncludeLoc = getImportLocation(F); 1570 } 1571 1572 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1573 if (!Buffer) 1574 return true; 1575 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1576 BaseOffset + Offset, IncludeLoc); 1577 break; 1578 } 1579 1580 case SM_SLOC_EXPANSION_ENTRY: { 1581 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1582 SourceMgr.createExpansionLoc(SpellingLoc, 1583 ReadSourceLocation(*F, Record[2]), 1584 ReadSourceLocation(*F, Record[3]), 1585 Record[5], 1586 Record[4], 1587 ID, 1588 BaseOffset + Record[0]); 1589 break; 1590 } 1591 } 1592 1593 return false; 1594 } 1595 1596 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1597 if (ID == 0) 1598 return std::make_pair(SourceLocation(), ""); 1599 1600 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1601 Error("source location entry ID out-of-range for AST file"); 1602 return std::make_pair(SourceLocation(), ""); 1603 } 1604 1605 // Find which module file this entry lands in. 1606 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1607 if (!M->isModule()) 1608 return std::make_pair(SourceLocation(), ""); 1609 1610 // FIXME: Can we map this down to a particular submodule? That would be 1611 // ideal. 1612 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1613 } 1614 1615 /// Find the location where the module F is imported. 1616 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1617 if (F->ImportLoc.isValid()) 1618 return F->ImportLoc; 1619 1620 // Otherwise we have a PCH. It's considered to be "imported" at the first 1621 // location of its includer. 1622 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1623 // Main file is the importer. 1624 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1625 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1626 } 1627 return F->ImportedBy[0]->FirstLoc; 1628 } 1629 1630 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1631 /// the abbreviations that are at the top of the block and then leave the cursor 1632 /// pointing into the block. 1633 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID, 1634 uint64_t *StartOfBlockOffset) { 1635 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 1636 // FIXME this drops errors on the floor. 1637 consumeError(std::move(Err)); 1638 return true; 1639 } 1640 1641 if (StartOfBlockOffset) 1642 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1643 1644 while (true) { 1645 uint64_t Offset = Cursor.GetCurrentBitNo(); 1646 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1647 if (!MaybeCode) { 1648 // FIXME this drops errors on the floor. 1649 consumeError(MaybeCode.takeError()); 1650 return true; 1651 } 1652 unsigned Code = MaybeCode.get(); 1653 1654 // We expect all abbrevs to be at the start of the block. 1655 if (Code != llvm::bitc::DEFINE_ABBREV) { 1656 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1657 // FIXME this drops errors on the floor. 1658 consumeError(std::move(Err)); 1659 return true; 1660 } 1661 return false; 1662 } 1663 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) { 1664 // FIXME this drops errors on the floor. 1665 consumeError(std::move(Err)); 1666 return true; 1667 } 1668 } 1669 } 1670 1671 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1672 unsigned &Idx) { 1673 Token Tok; 1674 Tok.startToken(); 1675 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1676 Tok.setLength(Record[Idx++]); 1677 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1678 Tok.setIdentifierInfo(II); 1679 Tok.setKind((tok::TokenKind)Record[Idx++]); 1680 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1681 return Tok; 1682 } 1683 1684 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1685 BitstreamCursor &Stream = F.MacroCursor; 1686 1687 // Keep track of where we are in the stream, then jump back there 1688 // after reading this macro. 1689 SavedStreamPosition SavedPosition(Stream); 1690 1691 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1692 // FIXME this drops errors on the floor. 1693 consumeError(std::move(Err)); 1694 return nullptr; 1695 } 1696 RecordData Record; 1697 SmallVector<IdentifierInfo*, 16> MacroParams; 1698 MacroInfo *Macro = nullptr; 1699 1700 while (true) { 1701 // Advance to the next record, but if we get to the end of the block, don't 1702 // pop it (removing all the abbreviations from the cursor) since we want to 1703 // be able to reseek within the block and read entries. 1704 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1705 Expected<llvm::BitstreamEntry> MaybeEntry = 1706 Stream.advanceSkippingSubblocks(Flags); 1707 if (!MaybeEntry) { 1708 Error(MaybeEntry.takeError()); 1709 return Macro; 1710 } 1711 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1712 1713 switch (Entry.Kind) { 1714 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1715 case llvm::BitstreamEntry::Error: 1716 Error("malformed block record in AST file"); 1717 return Macro; 1718 case llvm::BitstreamEntry::EndBlock: 1719 return Macro; 1720 case llvm::BitstreamEntry::Record: 1721 // The interesting case. 1722 break; 1723 } 1724 1725 // Read a record. 1726 Record.clear(); 1727 PreprocessorRecordTypes RecType; 1728 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1729 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1730 else { 1731 Error(MaybeRecType.takeError()); 1732 return Macro; 1733 } 1734 switch (RecType) { 1735 case PP_MODULE_MACRO: 1736 case PP_MACRO_DIRECTIVE_HISTORY: 1737 return Macro; 1738 1739 case PP_MACRO_OBJECT_LIKE: 1740 case PP_MACRO_FUNCTION_LIKE: { 1741 // If we already have a macro, that means that we've hit the end 1742 // of the definition of the macro we were looking for. We're 1743 // done. 1744 if (Macro) 1745 return Macro; 1746 1747 unsigned NextIndex = 1; // Skip identifier ID. 1748 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1749 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1750 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1751 MI->setIsUsed(Record[NextIndex++]); 1752 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1753 1754 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1755 // Decode function-like macro info. 1756 bool isC99VarArgs = Record[NextIndex++]; 1757 bool isGNUVarArgs = Record[NextIndex++]; 1758 bool hasCommaPasting = Record[NextIndex++]; 1759 MacroParams.clear(); 1760 unsigned NumArgs = Record[NextIndex++]; 1761 for (unsigned i = 0; i != NumArgs; ++i) 1762 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1763 1764 // Install function-like macro info. 1765 MI->setIsFunctionLike(); 1766 if (isC99VarArgs) MI->setIsC99Varargs(); 1767 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1768 if (hasCommaPasting) MI->setHasCommaPasting(); 1769 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1770 } 1771 1772 // Remember that we saw this macro last so that we add the tokens that 1773 // form its body to it. 1774 Macro = MI; 1775 1776 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1777 Record[NextIndex]) { 1778 // We have a macro definition. Register the association 1779 PreprocessedEntityID 1780 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1781 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1782 PreprocessingRecord::PPEntityID PPID = 1783 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1784 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1785 PPRec.getPreprocessedEntity(PPID)); 1786 if (PPDef) 1787 PPRec.RegisterMacroDefinition(Macro, PPDef); 1788 } 1789 1790 ++NumMacrosRead; 1791 break; 1792 } 1793 1794 case PP_TOKEN: { 1795 // If we see a TOKEN before a PP_MACRO_*, then the file is 1796 // erroneous, just pretend we didn't see this. 1797 if (!Macro) break; 1798 1799 unsigned Idx = 0; 1800 Token Tok = ReadToken(F, Record, Idx); 1801 Macro->AddTokenToBody(Tok); 1802 break; 1803 } 1804 } 1805 } 1806 } 1807 1808 PreprocessedEntityID 1809 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1810 unsigned LocalID) const { 1811 if (!M.ModuleOffsetMap.empty()) 1812 ReadModuleOffsetMap(M); 1813 1814 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1815 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1816 assert(I != M.PreprocessedEntityRemap.end() 1817 && "Invalid index into preprocessed entity index remap"); 1818 1819 return LocalID + I->second; 1820 } 1821 1822 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1823 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1824 } 1825 1826 HeaderFileInfoTrait::internal_key_type 1827 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1828 internal_key_type ikey = {FE->getSize(), 1829 M.HasTimestamps ? FE->getModificationTime() : 0, 1830 FE->getName(), /*Imported*/ false}; 1831 return ikey; 1832 } 1833 1834 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1835 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1836 return false; 1837 1838 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1839 return true; 1840 1841 // Determine whether the actual files are equivalent. 1842 FileManager &FileMgr = Reader.getFileManager(); 1843 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1844 if (!Key.Imported) { 1845 if (auto File = FileMgr.getFile(Key.Filename)) 1846 return *File; 1847 return nullptr; 1848 } 1849 1850 std::string Resolved = std::string(Key.Filename); 1851 Reader.ResolveImportedPath(M, Resolved); 1852 if (auto File = FileMgr.getFile(Resolved)) 1853 return *File; 1854 return nullptr; 1855 }; 1856 1857 const FileEntry *FEA = GetFile(a); 1858 const FileEntry *FEB = GetFile(b); 1859 return FEA && FEA == FEB; 1860 } 1861 1862 std::pair<unsigned, unsigned> 1863 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1864 return readULEBKeyDataLength(d); 1865 } 1866 1867 HeaderFileInfoTrait::internal_key_type 1868 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1869 using namespace llvm::support; 1870 1871 internal_key_type ikey; 1872 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1873 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1874 ikey.Filename = (const char *)d; 1875 ikey.Imported = true; 1876 return ikey; 1877 } 1878 1879 HeaderFileInfoTrait::data_type 1880 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1881 unsigned DataLen) { 1882 using namespace llvm::support; 1883 1884 const unsigned char *End = d + DataLen; 1885 HeaderFileInfo HFI; 1886 unsigned Flags = *d++; 1887 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1888 HFI.isImport |= (Flags >> 5) & 0x01; 1889 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1890 HFI.DirInfo = (Flags >> 1) & 0x07; 1891 HFI.IndexHeaderMapHeader = Flags & 0x01; 1892 // FIXME: Find a better way to handle this. Maybe just store a 1893 // "has been included" flag? 1894 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1895 HFI.NumIncludes); 1896 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1897 M, endian::readNext<uint32_t, little, unaligned>(d)); 1898 if (unsigned FrameworkOffset = 1899 endian::readNext<uint32_t, little, unaligned>(d)) { 1900 // The framework offset is 1 greater than the actual offset, 1901 // since 0 is used as an indicator for "no framework name". 1902 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1903 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1904 } 1905 1906 assert((End - d) % 4 == 0 && 1907 "Wrong data length in HeaderFileInfo deserialization"); 1908 while (d != End) { 1909 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1910 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1911 LocalSMID >>= 2; 1912 1913 // This header is part of a module. Associate it with the module to enable 1914 // implicit module import. 1915 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1916 Module *Mod = Reader.getSubmodule(GlobalSMID); 1917 FileManager &FileMgr = Reader.getFileManager(); 1918 ModuleMap &ModMap = 1919 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1920 1921 std::string Filename = std::string(key.Filename); 1922 if (key.Imported) 1923 Reader.ResolveImportedPath(M, Filename); 1924 // FIXME: This is not always the right filename-as-written, but we're not 1925 // going to use this information to rebuild the module, so it doesn't make 1926 // a lot of difference. 1927 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)}; 1928 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1929 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1930 } 1931 1932 // This HeaderFileInfo was externally loaded. 1933 HFI.External = true; 1934 HFI.IsValid = true; 1935 return HFI; 1936 } 1937 1938 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1939 uint32_t MacroDirectivesOffset) { 1940 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1941 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1942 } 1943 1944 void ASTReader::ReadDefinedMacros() { 1945 // Note that we are loading defined macros. 1946 Deserializing Macros(this); 1947 1948 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1949 BitstreamCursor &MacroCursor = I.MacroCursor; 1950 1951 // If there was no preprocessor block, skip this file. 1952 if (MacroCursor.getBitcodeBytes().empty()) 1953 continue; 1954 1955 BitstreamCursor Cursor = MacroCursor; 1956 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1957 Error(std::move(Err)); 1958 return; 1959 } 1960 1961 RecordData Record; 1962 while (true) { 1963 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1964 if (!MaybeE) { 1965 Error(MaybeE.takeError()); 1966 return; 1967 } 1968 llvm::BitstreamEntry E = MaybeE.get(); 1969 1970 switch (E.Kind) { 1971 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1972 case llvm::BitstreamEntry::Error: 1973 Error("malformed block record in AST file"); 1974 return; 1975 case llvm::BitstreamEntry::EndBlock: 1976 goto NextCursor; 1977 1978 case llvm::BitstreamEntry::Record: { 1979 Record.clear(); 1980 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1981 if (!MaybeRecord) { 1982 Error(MaybeRecord.takeError()); 1983 return; 1984 } 1985 switch (MaybeRecord.get()) { 1986 default: // Default behavior: ignore. 1987 break; 1988 1989 case PP_MACRO_OBJECT_LIKE: 1990 case PP_MACRO_FUNCTION_LIKE: { 1991 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1992 if (II->isOutOfDate()) 1993 updateOutOfDateIdentifier(*II); 1994 break; 1995 } 1996 1997 case PP_TOKEN: 1998 // Ignore tokens. 1999 break; 2000 } 2001 break; 2002 } 2003 } 2004 } 2005 NextCursor: ; 2006 } 2007 } 2008 2009 namespace { 2010 2011 /// Visitor class used to look up identifirs in an AST file. 2012 class IdentifierLookupVisitor { 2013 StringRef Name; 2014 unsigned NameHash; 2015 unsigned PriorGeneration; 2016 unsigned &NumIdentifierLookups; 2017 unsigned &NumIdentifierLookupHits; 2018 IdentifierInfo *Found = nullptr; 2019 2020 public: 2021 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2022 unsigned &NumIdentifierLookups, 2023 unsigned &NumIdentifierLookupHits) 2024 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2025 PriorGeneration(PriorGeneration), 2026 NumIdentifierLookups(NumIdentifierLookups), 2027 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2028 2029 bool operator()(ModuleFile &M) { 2030 // If we've already searched this module file, skip it now. 2031 if (M.Generation <= PriorGeneration) 2032 return true; 2033 2034 ASTIdentifierLookupTable *IdTable 2035 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2036 if (!IdTable) 2037 return false; 2038 2039 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2040 Found); 2041 ++NumIdentifierLookups; 2042 ASTIdentifierLookupTable::iterator Pos = 2043 IdTable->find_hashed(Name, NameHash, &Trait); 2044 if (Pos == IdTable->end()) 2045 return false; 2046 2047 // Dereferencing the iterator has the effect of building the 2048 // IdentifierInfo node and populating it with the various 2049 // declarations it needs. 2050 ++NumIdentifierLookupHits; 2051 Found = *Pos; 2052 return true; 2053 } 2054 2055 // Retrieve the identifier info found within the module 2056 // files. 2057 IdentifierInfo *getIdentifierInfo() const { return Found; } 2058 }; 2059 2060 } // namespace 2061 2062 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2063 // Note that we are loading an identifier. 2064 Deserializing AnIdentifier(this); 2065 2066 unsigned PriorGeneration = 0; 2067 if (getContext().getLangOpts().Modules) 2068 PriorGeneration = IdentifierGeneration[&II]; 2069 2070 // If there is a global index, look there first to determine which modules 2071 // provably do not have any results for this identifier. 2072 GlobalModuleIndex::HitSet Hits; 2073 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2074 if (!loadGlobalIndex()) { 2075 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2076 HitsPtr = &Hits; 2077 } 2078 } 2079 2080 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2081 NumIdentifierLookups, 2082 NumIdentifierLookupHits); 2083 ModuleMgr.visit(Visitor, HitsPtr); 2084 markIdentifierUpToDate(&II); 2085 } 2086 2087 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2088 if (!II) 2089 return; 2090 2091 II->setOutOfDate(false); 2092 2093 // Update the generation for this identifier. 2094 if (getContext().getLangOpts().Modules) 2095 IdentifierGeneration[II] = getGeneration(); 2096 } 2097 2098 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2099 const PendingMacroInfo &PMInfo) { 2100 ModuleFile &M = *PMInfo.M; 2101 2102 BitstreamCursor &Cursor = M.MacroCursor; 2103 SavedStreamPosition SavedPosition(Cursor); 2104 if (llvm::Error Err = 2105 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2106 Error(std::move(Err)); 2107 return; 2108 } 2109 2110 struct ModuleMacroRecord { 2111 SubmoduleID SubModID; 2112 MacroInfo *MI; 2113 SmallVector<SubmoduleID, 8> Overrides; 2114 }; 2115 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2116 2117 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2118 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2119 // macro histroy. 2120 RecordData Record; 2121 while (true) { 2122 Expected<llvm::BitstreamEntry> MaybeEntry = 2123 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2124 if (!MaybeEntry) { 2125 Error(MaybeEntry.takeError()); 2126 return; 2127 } 2128 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2129 2130 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2131 Error("malformed block record in AST file"); 2132 return; 2133 } 2134 2135 Record.clear(); 2136 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2137 if (!MaybePP) { 2138 Error(MaybePP.takeError()); 2139 return; 2140 } 2141 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2142 case PP_MACRO_DIRECTIVE_HISTORY: 2143 break; 2144 2145 case PP_MODULE_MACRO: { 2146 ModuleMacros.push_back(ModuleMacroRecord()); 2147 auto &Info = ModuleMacros.back(); 2148 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2149 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2150 for (int I = 2, N = Record.size(); I != N; ++I) 2151 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2152 continue; 2153 } 2154 2155 default: 2156 Error("malformed block record in AST file"); 2157 return; 2158 } 2159 2160 // We found the macro directive history; that's the last record 2161 // for this macro. 2162 break; 2163 } 2164 2165 // Module macros are listed in reverse dependency order. 2166 { 2167 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2168 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2169 for (auto &MMR : ModuleMacros) { 2170 Overrides.clear(); 2171 for (unsigned ModID : MMR.Overrides) { 2172 Module *Mod = getSubmodule(ModID); 2173 auto *Macro = PP.getModuleMacro(Mod, II); 2174 assert(Macro && "missing definition for overridden macro"); 2175 Overrides.push_back(Macro); 2176 } 2177 2178 bool Inserted = false; 2179 Module *Owner = getSubmodule(MMR.SubModID); 2180 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2181 } 2182 } 2183 2184 // Don't read the directive history for a module; we don't have anywhere 2185 // to put it. 2186 if (M.isModule()) 2187 return; 2188 2189 // Deserialize the macro directives history in reverse source-order. 2190 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2191 unsigned Idx = 0, N = Record.size(); 2192 while (Idx < N) { 2193 MacroDirective *MD = nullptr; 2194 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2195 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2196 switch (K) { 2197 case MacroDirective::MD_Define: { 2198 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2199 MD = PP.AllocateDefMacroDirective(MI, Loc); 2200 break; 2201 } 2202 case MacroDirective::MD_Undefine: 2203 MD = PP.AllocateUndefMacroDirective(Loc); 2204 break; 2205 case MacroDirective::MD_Visibility: 2206 bool isPublic = Record[Idx++]; 2207 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2208 break; 2209 } 2210 2211 if (!Latest) 2212 Latest = MD; 2213 if (Earliest) 2214 Earliest->setPrevious(MD); 2215 Earliest = MD; 2216 } 2217 2218 if (Latest) 2219 PP.setLoadedMacroDirective(II, Earliest, Latest); 2220 } 2221 2222 bool ASTReader::shouldDisableValidationForFile( 2223 const serialization::ModuleFile &M) const { 2224 if (DisableValidationKind == DisableValidationForModuleKind::None) 2225 return false; 2226 2227 // If a PCH is loaded and validation is disabled for PCH then disable 2228 // validation for the PCH and the modules it loads. 2229 ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind); 2230 2231 switch (K) { 2232 case MK_MainFile: 2233 case MK_Preamble: 2234 case MK_PCH: 2235 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH); 2236 case MK_ImplicitModule: 2237 case MK_ExplicitModule: 2238 case MK_PrebuiltModule: 2239 return bool(DisableValidationKind & DisableValidationForModuleKind::Module); 2240 } 2241 2242 return false; 2243 } 2244 2245 ASTReader::InputFileInfo 2246 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2247 // Go find this input file. 2248 BitstreamCursor &Cursor = F.InputFilesCursor; 2249 SavedStreamPosition SavedPosition(Cursor); 2250 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2251 // FIXME this drops errors on the floor. 2252 consumeError(std::move(Err)); 2253 } 2254 2255 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2256 if (!MaybeCode) { 2257 // FIXME this drops errors on the floor. 2258 consumeError(MaybeCode.takeError()); 2259 } 2260 unsigned Code = MaybeCode.get(); 2261 RecordData Record; 2262 StringRef Blob; 2263 2264 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2265 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2266 "invalid record type for input file"); 2267 else { 2268 // FIXME this drops errors on the floor. 2269 consumeError(Maybe.takeError()); 2270 } 2271 2272 assert(Record[0] == ID && "Bogus stored ID or offset"); 2273 InputFileInfo R; 2274 R.StoredSize = static_cast<off_t>(Record[1]); 2275 R.StoredTime = static_cast<time_t>(Record[2]); 2276 R.Overridden = static_cast<bool>(Record[3]); 2277 R.Transient = static_cast<bool>(Record[4]); 2278 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2279 R.Filename = std::string(Blob); 2280 ResolveImportedPath(F, R.Filename); 2281 2282 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2283 if (!MaybeEntry) // FIXME this drops errors on the floor. 2284 consumeError(MaybeEntry.takeError()); 2285 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2286 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2287 "expected record type for input file hash"); 2288 2289 Record.clear(); 2290 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2291 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2292 "invalid record type for input file hash"); 2293 else { 2294 // FIXME this drops errors on the floor. 2295 consumeError(Maybe.takeError()); 2296 } 2297 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2298 static_cast<uint64_t>(Record[0]); 2299 return R; 2300 } 2301 2302 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2303 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2304 // If this ID is bogus, just return an empty input file. 2305 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2306 return InputFile(); 2307 2308 // If we've already loaded this input file, return it. 2309 if (F.InputFilesLoaded[ID-1].getFile()) 2310 return F.InputFilesLoaded[ID-1]; 2311 2312 if (F.InputFilesLoaded[ID-1].isNotFound()) 2313 return InputFile(); 2314 2315 // Go find this input file. 2316 BitstreamCursor &Cursor = F.InputFilesCursor; 2317 SavedStreamPosition SavedPosition(Cursor); 2318 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2319 // FIXME this drops errors on the floor. 2320 consumeError(std::move(Err)); 2321 } 2322 2323 InputFileInfo FI = readInputFileInfo(F, ID); 2324 off_t StoredSize = FI.StoredSize; 2325 time_t StoredTime = FI.StoredTime; 2326 bool Overridden = FI.Overridden; 2327 bool Transient = FI.Transient; 2328 StringRef Filename = FI.Filename; 2329 uint64_t StoredContentHash = FI.ContentHash; 2330 2331 OptionalFileEntryRefDegradesToFileEntryPtr File = 2332 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2333 2334 // If we didn't find the file, resolve it relative to the 2335 // original directory from which this AST file was created. 2336 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2337 F.OriginalDir != F.BaseDirectory) { 2338 std::string Resolved = resolveFileRelativeToOriginalDir( 2339 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2340 if (!Resolved.empty()) 2341 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2342 } 2343 2344 // For an overridden file, create a virtual file with the stored 2345 // size/timestamp. 2346 if ((Overridden || Transient) && !File) 2347 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2348 2349 if (!File) { 2350 if (Complain) { 2351 std::string ErrorStr = "could not find file '"; 2352 ErrorStr += Filename; 2353 ErrorStr += "' referenced by AST file '"; 2354 ErrorStr += F.FileName; 2355 ErrorStr += "'"; 2356 Error(ErrorStr); 2357 } 2358 // Record that we didn't find the file. 2359 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2360 return InputFile(); 2361 } 2362 2363 // Check if there was a request to override the contents of the file 2364 // that was part of the precompiled header. Overriding such a file 2365 // can lead to problems when lexing using the source locations from the 2366 // PCH. 2367 SourceManager &SM = getSourceManager(); 2368 // FIXME: Reject if the overrides are different. 2369 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2370 if (Complain) 2371 Error(diag::err_fe_pch_file_overridden, Filename); 2372 2373 // After emitting the diagnostic, bypass the overriding file to recover 2374 // (this creates a separate FileEntry). 2375 File = SM.bypassFileContentsOverride(*File); 2376 if (!File) { 2377 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2378 return InputFile(); 2379 } 2380 } 2381 2382 enum ModificationType { 2383 Size, 2384 ModTime, 2385 Content, 2386 None, 2387 }; 2388 auto HasInputFileChanged = [&]() { 2389 if (StoredSize != File->getSize()) 2390 return ModificationType::Size; 2391 if (!shouldDisableValidationForFile(F) && StoredTime && 2392 StoredTime != File->getModificationTime()) { 2393 // In case the modification time changes but not the content, 2394 // accept the cached file as legit. 2395 if (ValidateASTInputFilesContent && 2396 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2397 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2398 if (!MemBuffOrError) { 2399 if (!Complain) 2400 return ModificationType::ModTime; 2401 std::string ErrorStr = "could not get buffer for file '"; 2402 ErrorStr += File->getName(); 2403 ErrorStr += "'"; 2404 Error(ErrorStr); 2405 return ModificationType::ModTime; 2406 } 2407 2408 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2409 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2410 return ModificationType::None; 2411 return ModificationType::Content; 2412 } 2413 return ModificationType::ModTime; 2414 } 2415 return ModificationType::None; 2416 }; 2417 2418 bool IsOutOfDate = false; 2419 auto FileChange = HasInputFileChanged(); 2420 // For an overridden file, there is nothing to validate. 2421 if (!Overridden && FileChange != ModificationType::None) { 2422 if (Complain && !Diags.isDiagnosticInFlight()) { 2423 // Build a list of the PCH imports that got us here (in reverse). 2424 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2425 while (!ImportStack.back()->ImportedBy.empty()) 2426 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2427 2428 // The top-level PCH is stale. 2429 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2430 Diag(diag::err_fe_ast_file_modified) 2431 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2432 << TopLevelPCHName << FileChange; 2433 2434 // Print the import stack. 2435 if (ImportStack.size() > 1) { 2436 Diag(diag::note_pch_required_by) 2437 << Filename << ImportStack[0]->FileName; 2438 for (unsigned I = 1; I < ImportStack.size(); ++I) 2439 Diag(diag::note_pch_required_by) 2440 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2441 } 2442 2443 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2444 } 2445 2446 IsOutOfDate = true; 2447 } 2448 // FIXME: If the file is overridden and we've already opened it, 2449 // issue an error (or split it into a separate FileEntry). 2450 2451 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2452 2453 // Note that we've loaded this input file. 2454 F.InputFilesLoaded[ID-1] = IF; 2455 return IF; 2456 } 2457 2458 /// If we are loading a relocatable PCH or module file, and the filename 2459 /// is not an absolute path, add the system or module root to the beginning of 2460 /// the file name. 2461 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2462 // Resolve relative to the base directory, if we have one. 2463 if (!M.BaseDirectory.empty()) 2464 return ResolveImportedPath(Filename, M.BaseDirectory); 2465 } 2466 2467 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2468 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2469 return; 2470 2471 SmallString<128> Buffer; 2472 llvm::sys::path::append(Buffer, Prefix, Filename); 2473 Filename.assign(Buffer.begin(), Buffer.end()); 2474 } 2475 2476 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2477 switch (ARR) { 2478 case ASTReader::Failure: return true; 2479 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2480 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2481 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2482 case ASTReader::ConfigurationMismatch: 2483 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2484 case ASTReader::HadErrors: return true; 2485 case ASTReader::Success: return false; 2486 } 2487 2488 llvm_unreachable("unknown ASTReadResult"); 2489 } 2490 2491 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2492 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2493 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2494 std::string &SuggestedPredefines) { 2495 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2496 // FIXME this drops errors on the floor. 2497 consumeError(std::move(Err)); 2498 return Failure; 2499 } 2500 2501 // Read all of the records in the options block. 2502 RecordData Record; 2503 ASTReadResult Result = Success; 2504 while (true) { 2505 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2506 if (!MaybeEntry) { 2507 // FIXME this drops errors on the floor. 2508 consumeError(MaybeEntry.takeError()); 2509 return Failure; 2510 } 2511 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2512 2513 switch (Entry.Kind) { 2514 case llvm::BitstreamEntry::Error: 2515 case llvm::BitstreamEntry::SubBlock: 2516 return Failure; 2517 2518 case llvm::BitstreamEntry::EndBlock: 2519 return Result; 2520 2521 case llvm::BitstreamEntry::Record: 2522 // The interesting case. 2523 break; 2524 } 2525 2526 // Read and process a record. 2527 Record.clear(); 2528 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2529 if (!MaybeRecordType) { 2530 // FIXME this drops errors on the floor. 2531 consumeError(MaybeRecordType.takeError()); 2532 return Failure; 2533 } 2534 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2535 case LANGUAGE_OPTIONS: { 2536 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2537 if (ParseLanguageOptions(Record, Complain, Listener, 2538 AllowCompatibleConfigurationMismatch)) 2539 Result = ConfigurationMismatch; 2540 break; 2541 } 2542 2543 case TARGET_OPTIONS: { 2544 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2545 if (ParseTargetOptions(Record, Complain, Listener, 2546 AllowCompatibleConfigurationMismatch)) 2547 Result = ConfigurationMismatch; 2548 break; 2549 } 2550 2551 case FILE_SYSTEM_OPTIONS: { 2552 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2553 if (!AllowCompatibleConfigurationMismatch && 2554 ParseFileSystemOptions(Record, Complain, Listener)) 2555 Result = ConfigurationMismatch; 2556 break; 2557 } 2558 2559 case HEADER_SEARCH_OPTIONS: { 2560 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2561 if (!AllowCompatibleConfigurationMismatch && 2562 ParseHeaderSearchOptions(Record, Complain, Listener)) 2563 Result = ConfigurationMismatch; 2564 break; 2565 } 2566 2567 case PREPROCESSOR_OPTIONS: 2568 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2569 if (!AllowCompatibleConfigurationMismatch && 2570 ParsePreprocessorOptions(Record, Complain, Listener, 2571 SuggestedPredefines)) 2572 Result = ConfigurationMismatch; 2573 break; 2574 } 2575 } 2576 } 2577 2578 ASTReader::ASTReadResult 2579 ASTReader::ReadControlBlock(ModuleFile &F, 2580 SmallVectorImpl<ImportedModule> &Loaded, 2581 const ModuleFile *ImportedBy, 2582 unsigned ClientLoadCapabilities) { 2583 BitstreamCursor &Stream = F.Stream; 2584 2585 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2586 Error(std::move(Err)); 2587 return Failure; 2588 } 2589 2590 // Lambda to read the unhashed control block the first time it's called. 2591 // 2592 // For PCM files, the unhashed control block cannot be read until after the 2593 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2594 // need to look ahead before reading the IMPORTS record. For consistency, 2595 // this block is always read somehow (see BitstreamEntry::EndBlock). 2596 bool HasReadUnhashedControlBlock = false; 2597 auto readUnhashedControlBlockOnce = [&]() { 2598 if (!HasReadUnhashedControlBlock) { 2599 HasReadUnhashedControlBlock = true; 2600 if (ASTReadResult Result = 2601 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2602 return Result; 2603 } 2604 return Success; 2605 }; 2606 2607 bool DisableValidation = shouldDisableValidationForFile(F); 2608 2609 // Read all of the records and blocks in the control block. 2610 RecordData Record; 2611 unsigned NumInputs = 0; 2612 unsigned NumUserInputs = 0; 2613 StringRef BaseDirectoryAsWritten; 2614 while (true) { 2615 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2616 if (!MaybeEntry) { 2617 Error(MaybeEntry.takeError()); 2618 return Failure; 2619 } 2620 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2621 2622 switch (Entry.Kind) { 2623 case llvm::BitstreamEntry::Error: 2624 Error("malformed block record in AST file"); 2625 return Failure; 2626 case llvm::BitstreamEntry::EndBlock: { 2627 // Validate the module before returning. This call catches an AST with 2628 // no module name and no imports. 2629 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2630 return Result; 2631 2632 // Validate input files. 2633 const HeaderSearchOptions &HSOpts = 2634 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2635 2636 // All user input files reside at the index range [0, NumUserInputs), and 2637 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2638 // loaded module files, ignore missing inputs. 2639 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2640 F.Kind != MK_PrebuiltModule) { 2641 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2642 2643 // If we are reading a module, we will create a verification timestamp, 2644 // so we verify all input files. Otherwise, verify only user input 2645 // files. 2646 2647 unsigned N = NumUserInputs; 2648 if (ValidateSystemInputs || 2649 (HSOpts.ModulesValidateOncePerBuildSession && 2650 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2651 F.Kind == MK_ImplicitModule)) 2652 N = NumInputs; 2653 2654 for (unsigned I = 0; I < N; ++I) { 2655 InputFile IF = getInputFile(F, I+1, Complain); 2656 if (!IF.getFile() || IF.isOutOfDate()) 2657 return OutOfDate; 2658 } 2659 } 2660 2661 if (Listener) 2662 Listener->visitModuleFile(F.FileName, F.Kind); 2663 2664 if (Listener && Listener->needsInputFileVisitation()) { 2665 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2666 : NumUserInputs; 2667 for (unsigned I = 0; I < N; ++I) { 2668 bool IsSystem = I >= NumUserInputs; 2669 InputFileInfo FI = readInputFileInfo(F, I+1); 2670 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2671 F.Kind == MK_ExplicitModule || 2672 F.Kind == MK_PrebuiltModule); 2673 } 2674 } 2675 2676 return Success; 2677 } 2678 2679 case llvm::BitstreamEntry::SubBlock: 2680 switch (Entry.ID) { 2681 case INPUT_FILES_BLOCK_ID: 2682 F.InputFilesCursor = Stream; 2683 if (llvm::Error Err = Stream.SkipBlock()) { 2684 Error(std::move(Err)); 2685 return Failure; 2686 } 2687 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2688 Error("malformed block record in AST file"); 2689 return Failure; 2690 } 2691 continue; 2692 2693 case OPTIONS_BLOCK_ID: 2694 // If we're reading the first module for this group, check its options 2695 // are compatible with ours. For modules it imports, no further checking 2696 // is required, because we checked them when we built it. 2697 if (Listener && !ImportedBy) { 2698 // Should we allow the configuration of the module file to differ from 2699 // the configuration of the current translation unit in a compatible 2700 // way? 2701 // 2702 // FIXME: Allow this for files explicitly specified with -include-pch. 2703 bool AllowCompatibleConfigurationMismatch = 2704 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2705 2706 ASTReadResult Result = 2707 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2708 AllowCompatibleConfigurationMismatch, *Listener, 2709 SuggestedPredefines); 2710 if (Result == Failure) { 2711 Error("malformed block record in AST file"); 2712 return Result; 2713 } 2714 2715 if (DisableValidation || 2716 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2717 Result = Success; 2718 2719 // If we can't load the module, exit early since we likely 2720 // will rebuild the module anyway. The stream may be in the 2721 // middle of a block. 2722 if (Result != Success) 2723 return Result; 2724 } else if (llvm::Error Err = Stream.SkipBlock()) { 2725 Error(std::move(Err)); 2726 return Failure; 2727 } 2728 continue; 2729 2730 default: 2731 if (llvm::Error Err = Stream.SkipBlock()) { 2732 Error(std::move(Err)); 2733 return Failure; 2734 } 2735 continue; 2736 } 2737 2738 case llvm::BitstreamEntry::Record: 2739 // The interesting case. 2740 break; 2741 } 2742 2743 // Read and process a record. 2744 Record.clear(); 2745 StringRef Blob; 2746 Expected<unsigned> MaybeRecordType = 2747 Stream.readRecord(Entry.ID, Record, &Blob); 2748 if (!MaybeRecordType) { 2749 Error(MaybeRecordType.takeError()); 2750 return Failure; 2751 } 2752 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2753 case METADATA: { 2754 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2755 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2756 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2757 : diag::err_pch_version_too_new); 2758 return VersionMismatch; 2759 } 2760 2761 bool hasErrors = Record[6]; 2762 if (hasErrors && !DisableValidation) { 2763 // If requested by the caller and the module hasn't already been read 2764 // or compiled, mark modules on error as out-of-date. 2765 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) && 2766 !ModuleMgr.getModuleCache().isPCMFinal(F.FileName)) 2767 return OutOfDate; 2768 2769 if (!AllowASTWithCompilerErrors) { 2770 Diag(diag::err_pch_with_compiler_errors); 2771 return HadErrors; 2772 } 2773 } 2774 if (hasErrors) { 2775 Diags.ErrorOccurred = true; 2776 Diags.UncompilableErrorOccurred = true; 2777 Diags.UnrecoverableErrorOccurred = true; 2778 } 2779 2780 F.RelocatablePCH = Record[4]; 2781 // Relative paths in a relocatable PCH are relative to our sysroot. 2782 if (F.RelocatablePCH) 2783 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2784 2785 F.HasTimestamps = Record[5]; 2786 2787 const std::string &CurBranch = getClangFullRepositoryVersion(); 2788 StringRef ASTBranch = Blob; 2789 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2790 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2791 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2792 return VersionMismatch; 2793 } 2794 break; 2795 } 2796 2797 case IMPORTS: { 2798 // Validate the AST before processing any imports (otherwise, untangling 2799 // them can be error-prone and expensive). A module will have a name and 2800 // will already have been validated, but this catches the PCH case. 2801 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2802 return Result; 2803 2804 // Load each of the imported PCH files. 2805 unsigned Idx = 0, N = Record.size(); 2806 while (Idx < N) { 2807 // Read information about the AST file. 2808 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2809 // The import location will be the local one for now; we will adjust 2810 // all import locations of module imports after the global source 2811 // location info are setup, in ReadAST. 2812 SourceLocation ImportLoc = 2813 ReadUntranslatedSourceLocation(Record[Idx++]); 2814 off_t StoredSize = (off_t)Record[Idx++]; 2815 time_t StoredModTime = (time_t)Record[Idx++]; 2816 auto FirstSignatureByte = Record.begin() + Idx; 2817 ASTFileSignature StoredSignature = ASTFileSignature::create( 2818 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2819 Idx += ASTFileSignature::size; 2820 2821 std::string ImportedName = ReadString(Record, Idx); 2822 std::string ImportedFile; 2823 2824 // For prebuilt and explicit modules first consult the file map for 2825 // an override. Note that here we don't search prebuilt module 2826 // directories, only the explicit name to file mappings. Also, we will 2827 // still verify the size/signature making sure it is essentially the 2828 // same file but perhaps in a different location. 2829 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2830 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2831 ImportedName, /*FileMapOnly*/ true); 2832 2833 if (ImportedFile.empty()) 2834 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2835 // ModuleCache as when writing. 2836 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2837 else 2838 SkipPath(Record, Idx); 2839 2840 // If our client can't cope with us being out of date, we can't cope with 2841 // our dependency being missing. 2842 unsigned Capabilities = ClientLoadCapabilities; 2843 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2844 Capabilities &= ~ARR_Missing; 2845 2846 // Load the AST file. 2847 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2848 Loaded, StoredSize, StoredModTime, 2849 StoredSignature, Capabilities); 2850 2851 // If we diagnosed a problem, produce a backtrace. 2852 if (isDiagnosedResult(Result, Capabilities)) 2853 Diag(diag::note_module_file_imported_by) 2854 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2855 2856 switch (Result) { 2857 case Failure: return Failure; 2858 // If we have to ignore the dependency, we'll have to ignore this too. 2859 case Missing: 2860 case OutOfDate: return OutOfDate; 2861 case VersionMismatch: return VersionMismatch; 2862 case ConfigurationMismatch: return ConfigurationMismatch; 2863 case HadErrors: return HadErrors; 2864 case Success: break; 2865 } 2866 } 2867 break; 2868 } 2869 2870 case ORIGINAL_FILE: 2871 F.OriginalSourceFileID = FileID::get(Record[0]); 2872 F.ActualOriginalSourceFileName = std::string(Blob); 2873 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2874 ResolveImportedPath(F, F.OriginalSourceFileName); 2875 break; 2876 2877 case ORIGINAL_FILE_ID: 2878 F.OriginalSourceFileID = FileID::get(Record[0]); 2879 break; 2880 2881 case ORIGINAL_PCH_DIR: 2882 F.OriginalDir = std::string(Blob); 2883 break; 2884 2885 case MODULE_NAME: 2886 F.ModuleName = std::string(Blob); 2887 Diag(diag::remark_module_import) 2888 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2889 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2890 if (Listener) 2891 Listener->ReadModuleName(F.ModuleName); 2892 2893 // Validate the AST as soon as we have a name so we can exit early on 2894 // failure. 2895 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2896 return Result; 2897 2898 break; 2899 2900 case MODULE_DIRECTORY: { 2901 // Save the BaseDirectory as written in the PCM for computing the module 2902 // filename for the ModuleCache. 2903 BaseDirectoryAsWritten = Blob; 2904 assert(!F.ModuleName.empty() && 2905 "MODULE_DIRECTORY found before MODULE_NAME"); 2906 // If we've already loaded a module map file covering this module, we may 2907 // have a better path for it (relative to the current build). 2908 Module *M = PP.getHeaderSearchInfo().lookupModule( 2909 F.ModuleName, /*AllowSearch*/ true, 2910 /*AllowExtraModuleMapSearch*/ true); 2911 if (M && M->Directory) { 2912 // If we're implicitly loading a module, the base directory can't 2913 // change between the build and use. 2914 // Don't emit module relocation error if we have -fno-validate-pch 2915 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 2916 DisableValidationForModuleKind::Module) && 2917 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2918 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2919 if (!BuildDir || *BuildDir != M->Directory) { 2920 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2921 Diag(diag::err_imported_module_relocated) 2922 << F.ModuleName << Blob << M->Directory->getName(); 2923 return OutOfDate; 2924 } 2925 } 2926 F.BaseDirectory = std::string(M->Directory->getName()); 2927 } else { 2928 F.BaseDirectory = std::string(Blob); 2929 } 2930 break; 2931 } 2932 2933 case MODULE_MAP_FILE: 2934 if (ASTReadResult Result = 2935 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2936 return Result; 2937 break; 2938 2939 case INPUT_FILE_OFFSETS: 2940 NumInputs = Record[0]; 2941 NumUserInputs = Record[1]; 2942 F.InputFileOffsets = 2943 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2944 F.InputFilesLoaded.resize(NumInputs); 2945 F.NumUserInputFiles = NumUserInputs; 2946 break; 2947 } 2948 } 2949 } 2950 2951 ASTReader::ASTReadResult 2952 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2953 BitstreamCursor &Stream = F.Stream; 2954 2955 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) { 2956 Error(std::move(Err)); 2957 return Failure; 2958 } 2959 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2960 2961 // Read all of the records and blocks for the AST file. 2962 RecordData Record; 2963 while (true) { 2964 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2965 if (!MaybeEntry) { 2966 Error(MaybeEntry.takeError()); 2967 return Failure; 2968 } 2969 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2970 2971 switch (Entry.Kind) { 2972 case llvm::BitstreamEntry::Error: 2973 Error("error at end of module block in AST file"); 2974 return Failure; 2975 case llvm::BitstreamEntry::EndBlock: 2976 // Outside of C++, we do not store a lookup map for the translation unit. 2977 // Instead, mark it as needing a lookup map to be built if this module 2978 // contains any declarations lexically within it (which it always does!). 2979 // This usually has no cost, since we very rarely need the lookup map for 2980 // the translation unit outside C++. 2981 if (ASTContext *Ctx = ContextObj) { 2982 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2983 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2984 DC->setMustBuildLookupTable(); 2985 } 2986 2987 return Success; 2988 case llvm::BitstreamEntry::SubBlock: 2989 switch (Entry.ID) { 2990 case DECLTYPES_BLOCK_ID: 2991 // We lazily load the decls block, but we want to set up the 2992 // DeclsCursor cursor to point into it. Clone our current bitcode 2993 // cursor to it, enter the block and read the abbrevs in that block. 2994 // With the main cursor, we just skip over it. 2995 F.DeclsCursor = Stream; 2996 if (llvm::Error Err = Stream.SkipBlock()) { 2997 Error(std::move(Err)); 2998 return Failure; 2999 } 3000 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID, 3001 &F.DeclsBlockStartOffset)) { 3002 Error("malformed block record in AST file"); 3003 return Failure; 3004 } 3005 break; 3006 3007 case PREPROCESSOR_BLOCK_ID: 3008 F.MacroCursor = Stream; 3009 if (!PP.getExternalSource()) 3010 PP.setExternalSource(this); 3011 3012 if (llvm::Error Err = Stream.SkipBlock()) { 3013 Error(std::move(Err)); 3014 return Failure; 3015 } 3016 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 3017 Error("malformed block record in AST file"); 3018 return Failure; 3019 } 3020 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3021 break; 3022 3023 case PREPROCESSOR_DETAIL_BLOCK_ID: 3024 F.PreprocessorDetailCursor = Stream; 3025 3026 if (llvm::Error Err = Stream.SkipBlock()) { 3027 Error(std::move(Err)); 3028 return Failure; 3029 } 3030 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3031 PREPROCESSOR_DETAIL_BLOCK_ID)) { 3032 Error("malformed preprocessor detail record in AST file"); 3033 return Failure; 3034 } 3035 F.PreprocessorDetailStartOffset 3036 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3037 3038 if (!PP.getPreprocessingRecord()) 3039 PP.createPreprocessingRecord(); 3040 if (!PP.getPreprocessingRecord()->getExternalSource()) 3041 PP.getPreprocessingRecord()->SetExternalSource(*this); 3042 break; 3043 3044 case SOURCE_MANAGER_BLOCK_ID: 3045 if (ReadSourceManagerBlock(F)) 3046 return Failure; 3047 break; 3048 3049 case SUBMODULE_BLOCK_ID: 3050 if (ASTReadResult Result = 3051 ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3052 return Result; 3053 break; 3054 3055 case COMMENTS_BLOCK_ID: { 3056 BitstreamCursor C = Stream; 3057 3058 if (llvm::Error Err = Stream.SkipBlock()) { 3059 Error(std::move(Err)); 3060 return Failure; 3061 } 3062 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 3063 Error("malformed comments block in AST file"); 3064 return Failure; 3065 } 3066 CommentsCursors.push_back(std::make_pair(C, &F)); 3067 break; 3068 } 3069 3070 default: 3071 if (llvm::Error Err = Stream.SkipBlock()) { 3072 Error(std::move(Err)); 3073 return Failure; 3074 } 3075 break; 3076 } 3077 continue; 3078 3079 case llvm::BitstreamEntry::Record: 3080 // The interesting case. 3081 break; 3082 } 3083 3084 // Read and process a record. 3085 Record.clear(); 3086 StringRef Blob; 3087 Expected<unsigned> MaybeRecordType = 3088 Stream.readRecord(Entry.ID, Record, &Blob); 3089 if (!MaybeRecordType) { 3090 Error(MaybeRecordType.takeError()); 3091 return Failure; 3092 } 3093 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3094 3095 // If we're not loading an AST context, we don't care about most records. 3096 if (!ContextObj) { 3097 switch (RecordType) { 3098 case IDENTIFIER_TABLE: 3099 case IDENTIFIER_OFFSET: 3100 case INTERESTING_IDENTIFIERS: 3101 case STATISTICS: 3102 case PP_CONDITIONAL_STACK: 3103 case PP_COUNTER_VALUE: 3104 case SOURCE_LOCATION_OFFSETS: 3105 case MODULE_OFFSET_MAP: 3106 case SOURCE_MANAGER_LINE_TABLE: 3107 case SOURCE_LOCATION_PRELOADS: 3108 case PPD_ENTITIES_OFFSETS: 3109 case HEADER_SEARCH_TABLE: 3110 case IMPORTED_MODULES: 3111 case MACRO_OFFSET: 3112 break; 3113 default: 3114 continue; 3115 } 3116 } 3117 3118 switch (RecordType) { 3119 default: // Default behavior: ignore. 3120 break; 3121 3122 case TYPE_OFFSET: { 3123 if (F.LocalNumTypes != 0) { 3124 Error("duplicate TYPE_OFFSET record in AST file"); 3125 return Failure; 3126 } 3127 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3128 F.LocalNumTypes = Record[0]; 3129 unsigned LocalBaseTypeIndex = Record[1]; 3130 F.BaseTypeIndex = getTotalNumTypes(); 3131 3132 if (F.LocalNumTypes > 0) { 3133 // Introduce the global -> local mapping for types within this module. 3134 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3135 3136 // Introduce the local -> global mapping for types within this module. 3137 F.TypeRemap.insertOrReplace( 3138 std::make_pair(LocalBaseTypeIndex, 3139 F.BaseTypeIndex - LocalBaseTypeIndex)); 3140 3141 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3142 } 3143 break; 3144 } 3145 3146 case DECL_OFFSET: { 3147 if (F.LocalNumDecls != 0) { 3148 Error("duplicate DECL_OFFSET record in AST file"); 3149 return Failure; 3150 } 3151 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3152 F.LocalNumDecls = Record[0]; 3153 unsigned LocalBaseDeclID = Record[1]; 3154 F.BaseDeclID = getTotalNumDecls(); 3155 3156 if (F.LocalNumDecls > 0) { 3157 // Introduce the global -> local mapping for declarations within this 3158 // module. 3159 GlobalDeclMap.insert( 3160 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3161 3162 // Introduce the local -> global mapping for declarations within this 3163 // module. 3164 F.DeclRemap.insertOrReplace( 3165 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3166 3167 // Introduce the global -> local mapping for declarations within this 3168 // module. 3169 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3170 3171 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3172 } 3173 break; 3174 } 3175 3176 case TU_UPDATE_LEXICAL: { 3177 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3178 LexicalContents Contents( 3179 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3180 Blob.data()), 3181 static_cast<unsigned int>(Blob.size() / 4)); 3182 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3183 TU->setHasExternalLexicalStorage(true); 3184 break; 3185 } 3186 3187 case UPDATE_VISIBLE: { 3188 unsigned Idx = 0; 3189 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3190 auto *Data = (const unsigned char*)Blob.data(); 3191 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3192 // If we've already loaded the decl, perform the updates when we finish 3193 // loading this block. 3194 if (Decl *D = GetExistingDecl(ID)) 3195 PendingUpdateRecords.push_back( 3196 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3197 break; 3198 } 3199 3200 case IDENTIFIER_TABLE: 3201 F.IdentifierTableData = 3202 reinterpret_cast<const unsigned char *>(Blob.data()); 3203 if (Record[0]) { 3204 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3205 F.IdentifierTableData + Record[0], 3206 F.IdentifierTableData + sizeof(uint32_t), 3207 F.IdentifierTableData, 3208 ASTIdentifierLookupTrait(*this, F)); 3209 3210 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3211 } 3212 break; 3213 3214 case IDENTIFIER_OFFSET: { 3215 if (F.LocalNumIdentifiers != 0) { 3216 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 3217 return Failure; 3218 } 3219 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3220 F.LocalNumIdentifiers = Record[0]; 3221 unsigned LocalBaseIdentifierID = Record[1]; 3222 F.BaseIdentifierID = getTotalNumIdentifiers(); 3223 3224 if (F.LocalNumIdentifiers > 0) { 3225 // Introduce the global -> local mapping for identifiers within this 3226 // module. 3227 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3228 &F)); 3229 3230 // Introduce the local -> global mapping for identifiers within this 3231 // module. 3232 F.IdentifierRemap.insertOrReplace( 3233 std::make_pair(LocalBaseIdentifierID, 3234 F.BaseIdentifierID - LocalBaseIdentifierID)); 3235 3236 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3237 + F.LocalNumIdentifiers); 3238 } 3239 break; 3240 } 3241 3242 case INTERESTING_IDENTIFIERS: 3243 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3244 break; 3245 3246 case EAGERLY_DESERIALIZED_DECLS: 3247 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3248 // about "interesting" decls (for instance, if we're building a module). 3249 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3250 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3251 break; 3252 3253 case MODULAR_CODEGEN_DECLS: 3254 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3255 // them (ie: if we're not codegenerating this module). 3256 if (F.Kind == MK_MainFile || 3257 getContext().getLangOpts().BuildingPCHWithObjectFile) 3258 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3259 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3260 break; 3261 3262 case SPECIAL_TYPES: 3263 if (SpecialTypes.empty()) { 3264 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3265 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3266 break; 3267 } 3268 3269 if (SpecialTypes.size() != Record.size()) { 3270 Error("invalid special-types record"); 3271 return Failure; 3272 } 3273 3274 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3275 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3276 if (!SpecialTypes[I]) 3277 SpecialTypes[I] = ID; 3278 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3279 // merge step? 3280 } 3281 break; 3282 3283 case STATISTICS: 3284 TotalNumStatements += Record[0]; 3285 TotalNumMacros += Record[1]; 3286 TotalLexicalDeclContexts += Record[2]; 3287 TotalVisibleDeclContexts += Record[3]; 3288 break; 3289 3290 case UNUSED_FILESCOPED_DECLS: 3291 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3292 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3293 break; 3294 3295 case DELEGATING_CTORS: 3296 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3297 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3298 break; 3299 3300 case WEAK_UNDECLARED_IDENTIFIERS: 3301 if (Record.size() % 4 != 0) { 3302 Error("invalid weak identifiers record"); 3303 return Failure; 3304 } 3305 3306 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3307 // files. This isn't the way to do it :) 3308 WeakUndeclaredIdentifiers.clear(); 3309 3310 // Translate the weak, undeclared identifiers into global IDs. 3311 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3312 WeakUndeclaredIdentifiers.push_back( 3313 getGlobalIdentifierID(F, Record[I++])); 3314 WeakUndeclaredIdentifiers.push_back( 3315 getGlobalIdentifierID(F, Record[I++])); 3316 WeakUndeclaredIdentifiers.push_back( 3317 ReadSourceLocation(F, Record, I).getRawEncoding()); 3318 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3319 } 3320 break; 3321 3322 case SELECTOR_OFFSETS: { 3323 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3324 F.LocalNumSelectors = Record[0]; 3325 unsigned LocalBaseSelectorID = Record[1]; 3326 F.BaseSelectorID = getTotalNumSelectors(); 3327 3328 if (F.LocalNumSelectors > 0) { 3329 // Introduce the global -> local mapping for selectors within this 3330 // module. 3331 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3332 3333 // Introduce the local -> global mapping for selectors within this 3334 // module. 3335 F.SelectorRemap.insertOrReplace( 3336 std::make_pair(LocalBaseSelectorID, 3337 F.BaseSelectorID - LocalBaseSelectorID)); 3338 3339 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3340 } 3341 break; 3342 } 3343 3344 case METHOD_POOL: 3345 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3346 if (Record[0]) 3347 F.SelectorLookupTable 3348 = ASTSelectorLookupTable::Create( 3349 F.SelectorLookupTableData + Record[0], 3350 F.SelectorLookupTableData, 3351 ASTSelectorLookupTrait(*this, F)); 3352 TotalNumMethodPoolEntries += Record[1]; 3353 break; 3354 3355 case REFERENCED_SELECTOR_POOL: 3356 if (!Record.empty()) { 3357 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3358 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3359 Record[Idx++])); 3360 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3361 getRawEncoding()); 3362 } 3363 } 3364 break; 3365 3366 case PP_CONDITIONAL_STACK: 3367 if (!Record.empty()) { 3368 unsigned Idx = 0, End = Record.size() - 1; 3369 bool ReachedEOFWhileSkipping = Record[Idx++]; 3370 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3371 if (ReachedEOFWhileSkipping) { 3372 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3373 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3374 bool FoundNonSkipPortion = Record[Idx++]; 3375 bool FoundElse = Record[Idx++]; 3376 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3377 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3378 FoundElse, ElseLoc); 3379 } 3380 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3381 while (Idx < End) { 3382 auto Loc = ReadSourceLocation(F, Record, Idx); 3383 bool WasSkipping = Record[Idx++]; 3384 bool FoundNonSkip = Record[Idx++]; 3385 bool FoundElse = Record[Idx++]; 3386 ConditionalStack.push_back( 3387 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3388 } 3389 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3390 } 3391 break; 3392 3393 case PP_COUNTER_VALUE: 3394 if (!Record.empty() && Listener) 3395 Listener->ReadCounter(F, Record[0]); 3396 break; 3397 3398 case FILE_SORTED_DECLS: 3399 F.FileSortedDecls = (const DeclID *)Blob.data(); 3400 F.NumFileSortedDecls = Record[0]; 3401 break; 3402 3403 case SOURCE_LOCATION_OFFSETS: { 3404 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3405 F.LocalNumSLocEntries = Record[0]; 3406 unsigned SLocSpaceSize = Record[1]; 3407 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3408 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3409 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3410 SLocSpaceSize); 3411 if (!F.SLocEntryBaseID) { 3412 Error("ran out of source locations"); 3413 break; 3414 } 3415 // Make our entry in the range map. BaseID is negative and growing, so 3416 // we invert it. Because we invert it, though, we need the other end of 3417 // the range. 3418 unsigned RangeStart = 3419 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3420 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3421 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3422 3423 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3424 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3425 GlobalSLocOffsetMap.insert( 3426 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3427 - SLocSpaceSize,&F)); 3428 3429 // Initialize the remapping table. 3430 // Invalid stays invalid. 3431 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3432 // This module. Base was 2 when being compiled. 3433 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3434 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3435 3436 TotalNumSLocEntries += F.LocalNumSLocEntries; 3437 break; 3438 } 3439 3440 case MODULE_OFFSET_MAP: 3441 F.ModuleOffsetMap = Blob; 3442 break; 3443 3444 case SOURCE_MANAGER_LINE_TABLE: 3445 if (ParseLineTable(F, Record)) { 3446 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file"); 3447 return Failure; 3448 } 3449 break; 3450 3451 case SOURCE_LOCATION_PRELOADS: { 3452 // Need to transform from the local view (1-based IDs) to the global view, 3453 // which is based off F.SLocEntryBaseID. 3454 if (!F.PreloadSLocEntries.empty()) { 3455 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3456 return Failure; 3457 } 3458 3459 F.PreloadSLocEntries.swap(Record); 3460 break; 3461 } 3462 3463 case EXT_VECTOR_DECLS: 3464 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3465 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3466 break; 3467 3468 case VTABLE_USES: 3469 if (Record.size() % 3 != 0) { 3470 Error("Invalid VTABLE_USES record"); 3471 return Failure; 3472 } 3473 3474 // Later tables overwrite earlier ones. 3475 // FIXME: Modules will have some trouble with this. This is clearly not 3476 // the right way to do this. 3477 VTableUses.clear(); 3478 3479 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3480 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3481 VTableUses.push_back( 3482 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3483 VTableUses.push_back(Record[Idx++]); 3484 } 3485 break; 3486 3487 case PENDING_IMPLICIT_INSTANTIATIONS: 3488 if (PendingInstantiations.size() % 2 != 0) { 3489 Error("Invalid existing PendingInstantiations"); 3490 return Failure; 3491 } 3492 3493 if (Record.size() % 2 != 0) { 3494 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3495 return Failure; 3496 } 3497 3498 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3499 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3500 PendingInstantiations.push_back( 3501 ReadSourceLocation(F, Record, I).getRawEncoding()); 3502 } 3503 break; 3504 3505 case SEMA_DECL_REFS: 3506 if (Record.size() != 3) { 3507 Error("Invalid SEMA_DECL_REFS block"); 3508 return Failure; 3509 } 3510 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3511 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3512 break; 3513 3514 case PPD_ENTITIES_OFFSETS: { 3515 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3516 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3517 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3518 3519 unsigned LocalBasePreprocessedEntityID = Record[0]; 3520 3521 unsigned StartingID; 3522 if (!PP.getPreprocessingRecord()) 3523 PP.createPreprocessingRecord(); 3524 if (!PP.getPreprocessingRecord()->getExternalSource()) 3525 PP.getPreprocessingRecord()->SetExternalSource(*this); 3526 StartingID 3527 = PP.getPreprocessingRecord() 3528 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3529 F.BasePreprocessedEntityID = StartingID; 3530 3531 if (F.NumPreprocessedEntities > 0) { 3532 // Introduce the global -> local mapping for preprocessed entities in 3533 // this module. 3534 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3535 3536 // Introduce the local -> global mapping for preprocessed entities in 3537 // this module. 3538 F.PreprocessedEntityRemap.insertOrReplace( 3539 std::make_pair(LocalBasePreprocessedEntityID, 3540 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3541 } 3542 3543 break; 3544 } 3545 3546 case PPD_SKIPPED_RANGES: { 3547 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3548 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3549 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3550 3551 if (!PP.getPreprocessingRecord()) 3552 PP.createPreprocessingRecord(); 3553 if (!PP.getPreprocessingRecord()->getExternalSource()) 3554 PP.getPreprocessingRecord()->SetExternalSource(*this); 3555 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3556 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3557 3558 if (F.NumPreprocessedSkippedRanges > 0) 3559 GlobalSkippedRangeMap.insert( 3560 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3561 break; 3562 } 3563 3564 case DECL_UPDATE_OFFSETS: 3565 if (Record.size() % 2 != 0) { 3566 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3567 return Failure; 3568 } 3569 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3570 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3571 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3572 3573 // If we've already loaded the decl, perform the updates when we finish 3574 // loading this block. 3575 if (Decl *D = GetExistingDecl(ID)) 3576 PendingUpdateRecords.push_back( 3577 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3578 } 3579 break; 3580 3581 case OBJC_CATEGORIES_MAP: 3582 if (F.LocalNumObjCCategoriesInMap != 0) { 3583 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3584 return Failure; 3585 } 3586 3587 F.LocalNumObjCCategoriesInMap = Record[0]; 3588 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3589 break; 3590 3591 case OBJC_CATEGORIES: 3592 F.ObjCCategories.swap(Record); 3593 break; 3594 3595 case CUDA_SPECIAL_DECL_REFS: 3596 // Later tables overwrite earlier ones. 3597 // FIXME: Modules will have trouble with this. 3598 CUDASpecialDeclRefs.clear(); 3599 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3600 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3601 break; 3602 3603 case HEADER_SEARCH_TABLE: 3604 F.HeaderFileInfoTableData = Blob.data(); 3605 F.LocalNumHeaderFileInfos = Record[1]; 3606 if (Record[0]) { 3607 F.HeaderFileInfoTable 3608 = HeaderFileInfoLookupTable::Create( 3609 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3610 (const unsigned char *)F.HeaderFileInfoTableData, 3611 HeaderFileInfoTrait(*this, F, 3612 &PP.getHeaderSearchInfo(), 3613 Blob.data() + Record[2])); 3614 3615 PP.getHeaderSearchInfo().SetExternalSource(this); 3616 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3617 PP.getHeaderSearchInfo().SetExternalLookup(this); 3618 } 3619 break; 3620 3621 case FP_PRAGMA_OPTIONS: 3622 // Later tables overwrite earlier ones. 3623 FPPragmaOptions.swap(Record); 3624 break; 3625 3626 case OPENCL_EXTENSIONS: 3627 for (unsigned I = 0, E = Record.size(); I != E; ) { 3628 auto Name = ReadString(Record, I); 3629 auto &OptInfo = OpenCLExtensions.OptMap[Name]; 3630 OptInfo.Supported = Record[I++] != 0; 3631 OptInfo.Enabled = Record[I++] != 0; 3632 OptInfo.WithPragma = Record[I++] != 0; 3633 OptInfo.Avail = Record[I++]; 3634 OptInfo.Core = Record[I++]; 3635 OptInfo.Opt = Record[I++]; 3636 } 3637 break; 3638 3639 case OPENCL_EXTENSION_TYPES: 3640 for (unsigned I = 0, E = Record.size(); I != E;) { 3641 auto TypeID = static_cast<::TypeID>(Record[I++]); 3642 auto *Type = GetType(TypeID).getTypePtr(); 3643 auto NumExt = static_cast<unsigned>(Record[I++]); 3644 for (unsigned II = 0; II != NumExt; ++II) { 3645 auto Ext = ReadString(Record, I); 3646 OpenCLTypeExtMap[Type].insert(Ext); 3647 } 3648 } 3649 break; 3650 3651 case OPENCL_EXTENSION_DECLS: 3652 for (unsigned I = 0, E = Record.size(); I != E;) { 3653 auto DeclID = static_cast<::DeclID>(Record[I++]); 3654 auto *Decl = GetDecl(DeclID); 3655 auto NumExt = static_cast<unsigned>(Record[I++]); 3656 for (unsigned II = 0; II != NumExt; ++II) { 3657 auto Ext = ReadString(Record, I); 3658 OpenCLDeclExtMap[Decl].insert(Ext); 3659 } 3660 } 3661 break; 3662 3663 case TENTATIVE_DEFINITIONS: 3664 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3665 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3666 break; 3667 3668 case KNOWN_NAMESPACES: 3669 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3670 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3671 break; 3672 3673 case UNDEFINED_BUT_USED: 3674 if (UndefinedButUsed.size() % 2 != 0) { 3675 Error("Invalid existing UndefinedButUsed"); 3676 return Failure; 3677 } 3678 3679 if (Record.size() % 2 != 0) { 3680 Error("invalid undefined-but-used record"); 3681 return Failure; 3682 } 3683 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3684 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3685 UndefinedButUsed.push_back( 3686 ReadSourceLocation(F, Record, I).getRawEncoding()); 3687 } 3688 break; 3689 3690 case DELETE_EXPRS_TO_ANALYZE: 3691 for (unsigned I = 0, N = Record.size(); I != N;) { 3692 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3693 const uint64_t Count = Record[I++]; 3694 DelayedDeleteExprs.push_back(Count); 3695 for (uint64_t C = 0; C < Count; ++C) { 3696 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3697 bool IsArrayForm = Record[I++] == 1; 3698 DelayedDeleteExprs.push_back(IsArrayForm); 3699 } 3700 } 3701 break; 3702 3703 case IMPORTED_MODULES: 3704 if (!F.isModule()) { 3705 // If we aren't loading a module (which has its own exports), make 3706 // all of the imported modules visible. 3707 // FIXME: Deal with macros-only imports. 3708 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3709 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3710 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3711 if (GlobalID) { 3712 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3713 if (DeserializationListener) 3714 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3715 } 3716 } 3717 } 3718 break; 3719 3720 case MACRO_OFFSET: { 3721 if (F.LocalNumMacros != 0) { 3722 Error("duplicate MACRO_OFFSET record in AST file"); 3723 return Failure; 3724 } 3725 F.MacroOffsets = (const uint32_t *)Blob.data(); 3726 F.LocalNumMacros = Record[0]; 3727 unsigned LocalBaseMacroID = Record[1]; 3728 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3729 F.BaseMacroID = getTotalNumMacros(); 3730 3731 if (F.LocalNumMacros > 0) { 3732 // Introduce the global -> local mapping for macros within this module. 3733 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3734 3735 // Introduce the local -> global mapping for macros within this module. 3736 F.MacroRemap.insertOrReplace( 3737 std::make_pair(LocalBaseMacroID, 3738 F.BaseMacroID - LocalBaseMacroID)); 3739 3740 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3741 } 3742 break; 3743 } 3744 3745 case LATE_PARSED_TEMPLATE: 3746 LateParsedTemplates.emplace_back( 3747 std::piecewise_construct, std::forward_as_tuple(&F), 3748 std::forward_as_tuple(Record.begin(), Record.end())); 3749 break; 3750 3751 case OPTIMIZE_PRAGMA_OPTIONS: 3752 if (Record.size() != 1) { 3753 Error("invalid pragma optimize record"); 3754 return Failure; 3755 } 3756 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3757 break; 3758 3759 case MSSTRUCT_PRAGMA_OPTIONS: 3760 if (Record.size() != 1) { 3761 Error("invalid pragma ms_struct record"); 3762 return Failure; 3763 } 3764 PragmaMSStructState = Record[0]; 3765 break; 3766 3767 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3768 if (Record.size() != 2) { 3769 Error("invalid pragma ms_struct record"); 3770 return Failure; 3771 } 3772 PragmaMSPointersToMembersState = Record[0]; 3773 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3774 break; 3775 3776 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3777 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3778 UnusedLocalTypedefNameCandidates.push_back( 3779 getGlobalDeclID(F, Record[I])); 3780 break; 3781 3782 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3783 if (Record.size() != 1) { 3784 Error("invalid cuda pragma options record"); 3785 return Failure; 3786 } 3787 ForceCUDAHostDeviceDepth = Record[0]; 3788 break; 3789 3790 case ALIGN_PACK_PRAGMA_OPTIONS: { 3791 if (Record.size() < 3) { 3792 Error("invalid pragma pack record"); 3793 return Failure; 3794 } 3795 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]); 3796 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3797 unsigned NumStackEntries = Record[2]; 3798 unsigned Idx = 3; 3799 // Reset the stack when importing a new module. 3800 PragmaAlignPackStack.clear(); 3801 for (unsigned I = 0; I < NumStackEntries; ++I) { 3802 PragmaAlignPackStackEntry Entry; 3803 Entry.Value = ReadAlignPackInfo(Record[Idx++]); 3804 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3805 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3806 PragmaAlignPackStrings.push_back(ReadString(Record, Idx)); 3807 Entry.SlotLabel = PragmaAlignPackStrings.back(); 3808 PragmaAlignPackStack.push_back(Entry); 3809 } 3810 break; 3811 } 3812 3813 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3814 if (Record.size() < 3) { 3815 Error("invalid pragma pack record"); 3816 return Failure; 3817 } 3818 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3819 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3820 unsigned NumStackEntries = Record[2]; 3821 unsigned Idx = 3; 3822 // Reset the stack when importing a new module. 3823 FpPragmaStack.clear(); 3824 for (unsigned I = 0; I < NumStackEntries; ++I) { 3825 FpPragmaStackEntry Entry; 3826 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3827 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3828 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3829 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3830 Entry.SlotLabel = FpPragmaStrings.back(); 3831 FpPragmaStack.push_back(Entry); 3832 } 3833 break; 3834 } 3835 3836 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3837 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3838 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I])); 3839 break; 3840 } 3841 } 3842 } 3843 3844 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3845 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3846 3847 // Additional remapping information. 3848 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3849 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3850 F.ModuleOffsetMap = StringRef(); 3851 3852 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3853 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3854 F.SLocRemap.insert(std::make_pair(0U, 0)); 3855 F.SLocRemap.insert(std::make_pair(2U, 1)); 3856 } 3857 3858 // Continuous range maps we may be updating in our module. 3859 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3860 RemapBuilder SLocRemap(F.SLocRemap); 3861 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3862 RemapBuilder MacroRemap(F.MacroRemap); 3863 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3864 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3865 RemapBuilder SelectorRemap(F.SelectorRemap); 3866 RemapBuilder DeclRemap(F.DeclRemap); 3867 RemapBuilder TypeRemap(F.TypeRemap); 3868 3869 while (Data < DataEnd) { 3870 // FIXME: Looking up dependency modules by filename is horrible. Let's 3871 // start fixing this with prebuilt, explicit and implicit modules and see 3872 // how it goes... 3873 using namespace llvm::support; 3874 ModuleKind Kind = static_cast<ModuleKind>( 3875 endian::readNext<uint8_t, little, unaligned>(Data)); 3876 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3877 StringRef Name = StringRef((const char*)Data, Len); 3878 Data += Len; 3879 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3880 Kind == MK_ImplicitModule 3881 ? ModuleMgr.lookupByModuleName(Name) 3882 : ModuleMgr.lookupByFileName(Name)); 3883 if (!OM) { 3884 std::string Msg = 3885 "SourceLocation remap refers to unknown module, cannot find "; 3886 Msg.append(std::string(Name)); 3887 Error(Msg); 3888 return; 3889 } 3890 3891 uint32_t SLocOffset = 3892 endian::readNext<uint32_t, little, unaligned>(Data); 3893 uint32_t IdentifierIDOffset = 3894 endian::readNext<uint32_t, little, unaligned>(Data); 3895 uint32_t MacroIDOffset = 3896 endian::readNext<uint32_t, little, unaligned>(Data); 3897 uint32_t PreprocessedEntityIDOffset = 3898 endian::readNext<uint32_t, little, unaligned>(Data); 3899 uint32_t SubmoduleIDOffset = 3900 endian::readNext<uint32_t, little, unaligned>(Data); 3901 uint32_t SelectorIDOffset = 3902 endian::readNext<uint32_t, little, unaligned>(Data); 3903 uint32_t DeclIDOffset = 3904 endian::readNext<uint32_t, little, unaligned>(Data); 3905 uint32_t TypeIndexOffset = 3906 endian::readNext<uint32_t, little, unaligned>(Data); 3907 3908 uint32_t None = std::numeric_limits<uint32_t>::max(); 3909 3910 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3911 RemapBuilder &Remap) { 3912 if (Offset != None) 3913 Remap.insert(std::make_pair(Offset, 3914 static_cast<int>(BaseOffset - Offset))); 3915 }; 3916 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3917 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3918 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3919 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3920 PreprocessedEntityRemap); 3921 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3922 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3923 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3924 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3925 3926 // Global -> local mappings. 3927 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3928 } 3929 } 3930 3931 ASTReader::ASTReadResult 3932 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3933 const ModuleFile *ImportedBy, 3934 unsigned ClientLoadCapabilities) { 3935 unsigned Idx = 0; 3936 F.ModuleMapPath = ReadPath(F, Record, Idx); 3937 3938 // Try to resolve ModuleName in the current header search context and 3939 // verify that it is found in the same module map file as we saved. If the 3940 // top-level AST file is a main file, skip this check because there is no 3941 // usable header search context. 3942 assert(!F.ModuleName.empty() && 3943 "MODULE_NAME should come before MODULE_MAP_FILE"); 3944 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3945 // An implicitly-loaded module file should have its module listed in some 3946 // module map file that we've already loaded. 3947 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3948 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3949 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3950 // Don't emit module relocation error if we have -fno-validate-pch 3951 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 3952 DisableValidationForModuleKind::Module) && 3953 !ModMap) { 3954 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3955 if (auto ASTFE = M ? M->getASTFile() : None) { 3956 // This module was defined by an imported (explicit) module. 3957 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3958 << ASTFE->getName(); 3959 } else { 3960 // This module was built with a different module map. 3961 Diag(diag::err_imported_module_not_found) 3962 << F.ModuleName << F.FileName 3963 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3964 << !ImportedBy; 3965 // In case it was imported by a PCH, there's a chance the user is 3966 // just missing to include the search path to the directory containing 3967 // the modulemap. 3968 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3969 Diag(diag::note_imported_by_pch_module_not_found) 3970 << llvm::sys::path::parent_path(F.ModuleMapPath); 3971 } 3972 } 3973 return OutOfDate; 3974 } 3975 3976 assert(M && M->Name == F.ModuleName && "found module with different name"); 3977 3978 // Check the primary module map file. 3979 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3980 if (!StoredModMap || *StoredModMap != ModMap) { 3981 assert(ModMap && "found module is missing module map file"); 3982 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3983 "top-level import should be verified"); 3984 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3985 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3986 Diag(diag::err_imported_module_modmap_changed) 3987 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3988 << ModMap->getName() << F.ModuleMapPath << NotImported; 3989 return OutOfDate; 3990 } 3991 3992 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3993 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3994 // FIXME: we should use input files rather than storing names. 3995 std::string Filename = ReadPath(F, Record, Idx); 3996 auto F = FileMgr.getFile(Filename, false, false); 3997 if (!F) { 3998 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3999 Error("could not find file '" + Filename +"' referenced by AST file"); 4000 return OutOfDate; 4001 } 4002 AdditionalStoredMaps.insert(*F); 4003 } 4004 4005 // Check any additional module map files (e.g. module.private.modulemap) 4006 // that are not in the pcm. 4007 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 4008 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 4009 // Remove files that match 4010 // Note: SmallPtrSet::erase is really remove 4011 if (!AdditionalStoredMaps.erase(ModMap)) { 4012 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 4013 Diag(diag::err_module_different_modmap) 4014 << F.ModuleName << /*new*/0 << ModMap->getName(); 4015 return OutOfDate; 4016 } 4017 } 4018 } 4019 4020 // Check any additional module map files that are in the pcm, but not 4021 // found in header search. Cases that match are already removed. 4022 for (const FileEntry *ModMap : AdditionalStoredMaps) { 4023 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 4024 Diag(diag::err_module_different_modmap) 4025 << F.ModuleName << /*not new*/1 << ModMap->getName(); 4026 return OutOfDate; 4027 } 4028 } 4029 4030 if (Listener) 4031 Listener->ReadModuleMapFile(F.ModuleMapPath); 4032 return Success; 4033 } 4034 4035 /// Move the given method to the back of the global list of methods. 4036 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4037 // Find the entry for this selector in the method pool. 4038 Sema::GlobalMethodPool::iterator Known 4039 = S.MethodPool.find(Method->getSelector()); 4040 if (Known == S.MethodPool.end()) 4041 return; 4042 4043 // Retrieve the appropriate method list. 4044 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4045 : Known->second.second; 4046 bool Found = false; 4047 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4048 if (!Found) { 4049 if (List->getMethod() == Method) { 4050 Found = true; 4051 } else { 4052 // Keep searching. 4053 continue; 4054 } 4055 } 4056 4057 if (List->getNext()) 4058 List->setMethod(List->getNext()->getMethod()); 4059 else 4060 List->setMethod(Method); 4061 } 4062 } 4063 4064 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4065 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4066 for (Decl *D : Names) { 4067 bool wasHidden = !D->isUnconditionallyVisible(); 4068 D->setVisibleDespiteOwningModule(); 4069 4070 if (wasHidden && SemaObj) { 4071 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4072 moveMethodToBackOfGlobalList(*SemaObj, Method); 4073 } 4074 } 4075 } 4076 } 4077 4078 void ASTReader::makeModuleVisible(Module *Mod, 4079 Module::NameVisibilityKind NameVisibility, 4080 SourceLocation ImportLoc) { 4081 llvm::SmallPtrSet<Module *, 4> Visited; 4082 SmallVector<Module *, 4> Stack; 4083 Stack.push_back(Mod); 4084 while (!Stack.empty()) { 4085 Mod = Stack.pop_back_val(); 4086 4087 if (NameVisibility <= Mod->NameVisibility) { 4088 // This module already has this level of visibility (or greater), so 4089 // there is nothing more to do. 4090 continue; 4091 } 4092 4093 if (Mod->isUnimportable()) { 4094 // Modules that aren't importable cannot be made visible. 4095 continue; 4096 } 4097 4098 // Update the module's name visibility. 4099 Mod->NameVisibility = NameVisibility; 4100 4101 // If we've already deserialized any names from this module, 4102 // mark them as visible. 4103 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4104 if (Hidden != HiddenNamesMap.end()) { 4105 auto HiddenNames = std::move(*Hidden); 4106 HiddenNamesMap.erase(Hidden); 4107 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4108 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4109 "making names visible added hidden names"); 4110 } 4111 4112 // Push any exported modules onto the stack to be marked as visible. 4113 SmallVector<Module *, 16> Exports; 4114 Mod->getExportedModules(Exports); 4115 for (SmallVectorImpl<Module *>::iterator 4116 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4117 Module *Exported = *I; 4118 if (Visited.insert(Exported).second) 4119 Stack.push_back(Exported); 4120 } 4121 } 4122 } 4123 4124 /// We've merged the definition \p MergedDef into the existing definition 4125 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4126 /// visible. 4127 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4128 NamedDecl *MergedDef) { 4129 if (!Def->isUnconditionallyVisible()) { 4130 // If MergedDef is visible or becomes visible, make the definition visible. 4131 if (MergedDef->isUnconditionallyVisible()) 4132 Def->setVisibleDespiteOwningModule(); 4133 else { 4134 getContext().mergeDefinitionIntoModule( 4135 Def, MergedDef->getImportedOwningModule(), 4136 /*NotifyListeners*/ false); 4137 PendingMergedDefinitionsToDeduplicate.insert(Def); 4138 } 4139 } 4140 } 4141 4142 bool ASTReader::loadGlobalIndex() { 4143 if (GlobalIndex) 4144 return false; 4145 4146 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4147 !PP.getLangOpts().Modules) 4148 return true; 4149 4150 // Try to load the global index. 4151 TriedLoadingGlobalIndex = true; 4152 StringRef ModuleCachePath 4153 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4154 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4155 GlobalModuleIndex::readIndex(ModuleCachePath); 4156 if (llvm::Error Err = std::move(Result.second)) { 4157 assert(!Result.first); 4158 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4159 return true; 4160 } 4161 4162 GlobalIndex.reset(Result.first); 4163 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4164 return false; 4165 } 4166 4167 bool ASTReader::isGlobalIndexUnavailable() const { 4168 return PP.getLangOpts().Modules && UseGlobalIndex && 4169 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4170 } 4171 4172 static void updateModuleTimestamp(ModuleFile &MF) { 4173 // Overwrite the timestamp file contents so that file's mtime changes. 4174 std::string TimestampFilename = MF.getTimestampFilename(); 4175 std::error_code EC; 4176 llvm::raw_fd_ostream OS(TimestampFilename, EC, 4177 llvm::sys::fs::OF_TextWithCRLF); 4178 if (EC) 4179 return; 4180 OS << "Timestamp file\n"; 4181 OS.close(); 4182 OS.clear_error(); // Avoid triggering a fatal error. 4183 } 4184 4185 /// Given a cursor at the start of an AST file, scan ahead and drop the 4186 /// cursor into the start of the given block ID, returning false on success and 4187 /// true on failure. 4188 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4189 while (true) { 4190 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4191 if (!MaybeEntry) { 4192 // FIXME this drops errors on the floor. 4193 consumeError(MaybeEntry.takeError()); 4194 return true; 4195 } 4196 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4197 4198 switch (Entry.Kind) { 4199 case llvm::BitstreamEntry::Error: 4200 case llvm::BitstreamEntry::EndBlock: 4201 return true; 4202 4203 case llvm::BitstreamEntry::Record: 4204 // Ignore top-level records. 4205 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4206 break; 4207 else { 4208 // FIXME this drops errors on the floor. 4209 consumeError(Skipped.takeError()); 4210 return true; 4211 } 4212 4213 case llvm::BitstreamEntry::SubBlock: 4214 if (Entry.ID == BlockID) { 4215 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4216 // FIXME this drops the error on the floor. 4217 consumeError(std::move(Err)); 4218 return true; 4219 } 4220 // Found it! 4221 return false; 4222 } 4223 4224 if (llvm::Error Err = Cursor.SkipBlock()) { 4225 // FIXME this drops the error on the floor. 4226 consumeError(std::move(Err)); 4227 return true; 4228 } 4229 } 4230 } 4231 } 4232 4233 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4234 ModuleKind Type, 4235 SourceLocation ImportLoc, 4236 unsigned ClientLoadCapabilities, 4237 SmallVectorImpl<ImportedSubmodule> *Imported) { 4238 llvm::SaveAndRestore<SourceLocation> 4239 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4240 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII( 4241 CurrentDeserializingModuleKind, Type); 4242 4243 // Defer any pending actions until we get to the end of reading the AST file. 4244 Deserializing AnASTFile(this); 4245 4246 // Bump the generation number. 4247 unsigned PreviousGeneration = 0; 4248 if (ContextObj) 4249 PreviousGeneration = incrementGeneration(*ContextObj); 4250 4251 unsigned NumModules = ModuleMgr.size(); 4252 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) { 4253 assert(ReadResult && "expected to return error"); 4254 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4255 PP.getLangOpts().Modules 4256 ? &PP.getHeaderSearchInfo().getModuleMap() 4257 : nullptr); 4258 4259 // If we find that any modules are unusable, the global index is going 4260 // to be out-of-date. Just remove it. 4261 GlobalIndex.reset(); 4262 ModuleMgr.setGlobalIndex(nullptr); 4263 return ReadResult; 4264 }; 4265 4266 SmallVector<ImportedModule, 4> Loaded; 4267 switch (ASTReadResult ReadResult = 4268 ReadASTCore(FileName, Type, ImportLoc, 4269 /*ImportedBy=*/nullptr, Loaded, 0, 0, 4270 ASTFileSignature(), ClientLoadCapabilities)) { 4271 case Failure: 4272 case Missing: 4273 case OutOfDate: 4274 case VersionMismatch: 4275 case ConfigurationMismatch: 4276 case HadErrors: 4277 return removeModulesAndReturn(ReadResult); 4278 case Success: 4279 break; 4280 } 4281 4282 // Here comes stuff that we only do once the entire chain is loaded. 4283 4284 // Load the AST blocks of all of the modules that we loaded. We can still 4285 // hit errors parsing the ASTs at this point. 4286 for (ImportedModule &M : Loaded) { 4287 ModuleFile &F = *M.Mod; 4288 4289 // Read the AST block. 4290 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 4291 return removeModulesAndReturn(Result); 4292 4293 // The AST block should always have a definition for the main module. 4294 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4295 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4296 return removeModulesAndReturn(Failure); 4297 } 4298 4299 // Read the extension blocks. 4300 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4301 if (ASTReadResult Result = ReadExtensionBlock(F)) 4302 return removeModulesAndReturn(Result); 4303 } 4304 4305 // Once read, set the ModuleFile bit base offset and update the size in 4306 // bits of all files we've seen. 4307 F.GlobalBitOffset = TotalModulesSizeInBits; 4308 TotalModulesSizeInBits += F.SizeInBits; 4309 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4310 } 4311 4312 // Preload source locations and interesting indentifiers. 4313 for (ImportedModule &M : Loaded) { 4314 ModuleFile &F = *M.Mod; 4315 4316 // Preload SLocEntries. 4317 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4318 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4319 // Load it through the SourceManager and don't call ReadSLocEntry() 4320 // directly because the entry may have already been loaded in which case 4321 // calling ReadSLocEntry() directly would trigger an assertion in 4322 // SourceManager. 4323 SourceMgr.getLoadedSLocEntryByID(Index); 4324 } 4325 4326 // Map the original source file ID into the ID space of the current 4327 // compilation. 4328 if (F.OriginalSourceFileID.isValid()) { 4329 F.OriginalSourceFileID = FileID::get( 4330 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4331 } 4332 4333 // Preload all the pending interesting identifiers by marking them out of 4334 // date. 4335 for (auto Offset : F.PreloadIdentifierOffsets) { 4336 const unsigned char *Data = F.IdentifierTableData + Offset; 4337 4338 ASTIdentifierLookupTrait Trait(*this, F); 4339 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4340 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4341 auto &II = PP.getIdentifierTable().getOwn(Key); 4342 II.setOutOfDate(true); 4343 4344 // Mark this identifier as being from an AST file so that we can track 4345 // whether we need to serialize it. 4346 markIdentifierFromAST(*this, II); 4347 4348 // Associate the ID with the identifier so that the writer can reuse it. 4349 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4350 SetIdentifierInfo(ID, &II); 4351 } 4352 } 4353 4354 // Setup the import locations and notify the module manager that we've 4355 // committed to these module files. 4356 for (ImportedModule &M : Loaded) { 4357 ModuleFile &F = *M.Mod; 4358 4359 ModuleMgr.moduleFileAccepted(&F); 4360 4361 // Set the import location. 4362 F.DirectImportLoc = ImportLoc; 4363 // FIXME: We assume that locations from PCH / preamble do not need 4364 // any translation. 4365 if (!M.ImportedBy) 4366 F.ImportLoc = M.ImportLoc; 4367 else 4368 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4369 } 4370 4371 if (!PP.getLangOpts().CPlusPlus || 4372 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4373 Type != MK_PrebuiltModule)) { 4374 // Mark all of the identifiers in the identifier table as being out of date, 4375 // so that various accessors know to check the loaded modules when the 4376 // identifier is used. 4377 // 4378 // For C++ modules, we don't need information on many identifiers (just 4379 // those that provide macros or are poisoned), so we mark all of 4380 // the interesting ones via PreloadIdentifierOffsets. 4381 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4382 IdEnd = PP.getIdentifierTable().end(); 4383 Id != IdEnd; ++Id) 4384 Id->second->setOutOfDate(true); 4385 } 4386 // Mark selectors as out of date. 4387 for (auto Sel : SelectorGeneration) 4388 SelectorOutOfDate[Sel.first] = true; 4389 4390 // Resolve any unresolved module exports. 4391 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4392 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4393 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4394 Module *ResolvedMod = getSubmodule(GlobalID); 4395 4396 switch (Unresolved.Kind) { 4397 case UnresolvedModuleRef::Conflict: 4398 if (ResolvedMod) { 4399 Module::Conflict Conflict; 4400 Conflict.Other = ResolvedMod; 4401 Conflict.Message = Unresolved.String.str(); 4402 Unresolved.Mod->Conflicts.push_back(Conflict); 4403 } 4404 continue; 4405 4406 case UnresolvedModuleRef::Import: 4407 if (ResolvedMod) 4408 Unresolved.Mod->Imports.insert(ResolvedMod); 4409 continue; 4410 4411 case UnresolvedModuleRef::Export: 4412 if (ResolvedMod || Unresolved.IsWildcard) 4413 Unresolved.Mod->Exports.push_back( 4414 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4415 continue; 4416 } 4417 } 4418 UnresolvedModuleRefs.clear(); 4419 4420 if (Imported) 4421 Imported->append(ImportedModules.begin(), 4422 ImportedModules.end()); 4423 4424 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4425 // Might be unnecessary as use declarations are only used to build the 4426 // module itself. 4427 4428 if (ContextObj) 4429 InitializeContext(); 4430 4431 if (SemaObj) 4432 UpdateSema(); 4433 4434 if (DeserializationListener) 4435 DeserializationListener->ReaderInitialized(this); 4436 4437 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4438 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4439 // If this AST file is a precompiled preamble, then set the 4440 // preamble file ID of the source manager to the file source file 4441 // from which the preamble was built. 4442 if (Type == MK_Preamble) { 4443 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4444 } else if (Type == MK_MainFile) { 4445 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4446 } 4447 } 4448 4449 // For any Objective-C class definitions we have already loaded, make sure 4450 // that we load any additional categories. 4451 if (ContextObj) { 4452 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4453 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4454 ObjCClassesLoaded[I], 4455 PreviousGeneration); 4456 } 4457 } 4458 4459 if (PP.getHeaderSearchInfo() 4460 .getHeaderSearchOpts() 4461 .ModulesValidateOncePerBuildSession) { 4462 // Now we are certain that the module and all modules it depends on are 4463 // up to date. Create or update timestamp files for modules that are 4464 // located in the module cache (not for PCH files that could be anywhere 4465 // in the filesystem). 4466 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4467 ImportedModule &M = Loaded[I]; 4468 if (M.Mod->Kind == MK_ImplicitModule) { 4469 updateModuleTimestamp(*M.Mod); 4470 } 4471 } 4472 } 4473 4474 return Success; 4475 } 4476 4477 static ASTFileSignature readASTFileSignature(StringRef PCH); 4478 4479 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4480 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4481 // FIXME checking magic headers is done in other places such as 4482 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4483 // always done the same. Unify it all with a helper. 4484 if (!Stream.canSkipToPos(4)) 4485 return llvm::createStringError(std::errc::illegal_byte_sequence, 4486 "file too small to contain AST file magic"); 4487 for (unsigned C : {'C', 'P', 'C', 'H'}) 4488 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4489 if (Res.get() != C) 4490 return llvm::createStringError( 4491 std::errc::illegal_byte_sequence, 4492 "file doesn't start with AST file magic"); 4493 } else 4494 return Res.takeError(); 4495 return llvm::Error::success(); 4496 } 4497 4498 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4499 switch (Kind) { 4500 case MK_PCH: 4501 return 0; // PCH 4502 case MK_ImplicitModule: 4503 case MK_ExplicitModule: 4504 case MK_PrebuiltModule: 4505 return 1; // module 4506 case MK_MainFile: 4507 case MK_Preamble: 4508 return 2; // main source file 4509 } 4510 llvm_unreachable("unknown module kind"); 4511 } 4512 4513 ASTReader::ASTReadResult 4514 ASTReader::ReadASTCore(StringRef FileName, 4515 ModuleKind Type, 4516 SourceLocation ImportLoc, 4517 ModuleFile *ImportedBy, 4518 SmallVectorImpl<ImportedModule> &Loaded, 4519 off_t ExpectedSize, time_t ExpectedModTime, 4520 ASTFileSignature ExpectedSignature, 4521 unsigned ClientLoadCapabilities) { 4522 ModuleFile *M; 4523 std::string ErrorStr; 4524 ModuleManager::AddModuleResult AddResult 4525 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4526 getGeneration(), ExpectedSize, ExpectedModTime, 4527 ExpectedSignature, readASTFileSignature, 4528 M, ErrorStr); 4529 4530 switch (AddResult) { 4531 case ModuleManager::AlreadyLoaded: 4532 Diag(diag::remark_module_import) 4533 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4534 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4535 return Success; 4536 4537 case ModuleManager::NewlyLoaded: 4538 // Load module file below. 4539 break; 4540 4541 case ModuleManager::Missing: 4542 // The module file was missing; if the client can handle that, return 4543 // it. 4544 if (ClientLoadCapabilities & ARR_Missing) 4545 return Missing; 4546 4547 // Otherwise, return an error. 4548 Diag(diag::err_ast_file_not_found) 4549 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4550 << ErrorStr; 4551 return Failure; 4552 4553 case ModuleManager::OutOfDate: 4554 // We couldn't load the module file because it is out-of-date. If the 4555 // client can handle out-of-date, return it. 4556 if (ClientLoadCapabilities & ARR_OutOfDate) 4557 return OutOfDate; 4558 4559 // Otherwise, return an error. 4560 Diag(diag::err_ast_file_out_of_date) 4561 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4562 << ErrorStr; 4563 return Failure; 4564 } 4565 4566 assert(M && "Missing module file"); 4567 4568 bool ShouldFinalizePCM = false; 4569 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4570 auto &MC = getModuleManager().getModuleCache(); 4571 if (ShouldFinalizePCM) 4572 MC.finalizePCM(FileName); 4573 else 4574 MC.tryToDropPCM(FileName); 4575 }); 4576 ModuleFile &F = *M; 4577 BitstreamCursor &Stream = F.Stream; 4578 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4579 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4580 4581 // Sniff for the signature. 4582 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4583 Diag(diag::err_ast_file_invalid) 4584 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4585 return Failure; 4586 } 4587 4588 // This is used for compatibility with older PCH formats. 4589 bool HaveReadControlBlock = false; 4590 while (true) { 4591 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4592 if (!MaybeEntry) { 4593 Error(MaybeEntry.takeError()); 4594 return Failure; 4595 } 4596 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4597 4598 switch (Entry.Kind) { 4599 case llvm::BitstreamEntry::Error: 4600 case llvm::BitstreamEntry::Record: 4601 case llvm::BitstreamEntry::EndBlock: 4602 Error("invalid record at top-level of AST file"); 4603 return Failure; 4604 4605 case llvm::BitstreamEntry::SubBlock: 4606 break; 4607 } 4608 4609 switch (Entry.ID) { 4610 case CONTROL_BLOCK_ID: 4611 HaveReadControlBlock = true; 4612 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4613 case Success: 4614 // Check that we didn't try to load a non-module AST file as a module. 4615 // 4616 // FIXME: Should we also perform the converse check? Loading a module as 4617 // a PCH file sort of works, but it's a bit wonky. 4618 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4619 Type == MK_PrebuiltModule) && 4620 F.ModuleName.empty()) { 4621 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4622 if (Result != OutOfDate || 4623 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4624 Diag(diag::err_module_file_not_module) << FileName; 4625 return Result; 4626 } 4627 break; 4628 4629 case Failure: return Failure; 4630 case Missing: return Missing; 4631 case OutOfDate: return OutOfDate; 4632 case VersionMismatch: return VersionMismatch; 4633 case ConfigurationMismatch: return ConfigurationMismatch; 4634 case HadErrors: return HadErrors; 4635 } 4636 break; 4637 4638 case AST_BLOCK_ID: 4639 if (!HaveReadControlBlock) { 4640 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4641 Diag(diag::err_pch_version_too_old); 4642 return VersionMismatch; 4643 } 4644 4645 // Record that we've loaded this module. 4646 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4647 ShouldFinalizePCM = true; 4648 return Success; 4649 4650 case UNHASHED_CONTROL_BLOCK_ID: 4651 // This block is handled using look-ahead during ReadControlBlock. We 4652 // shouldn't get here! 4653 Error("malformed block record in AST file"); 4654 return Failure; 4655 4656 default: 4657 if (llvm::Error Err = Stream.SkipBlock()) { 4658 Error(std::move(Err)); 4659 return Failure; 4660 } 4661 break; 4662 } 4663 } 4664 4665 llvm_unreachable("unexpected break; expected return"); 4666 } 4667 4668 ASTReader::ASTReadResult 4669 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4670 unsigned ClientLoadCapabilities) { 4671 const HeaderSearchOptions &HSOpts = 4672 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4673 bool AllowCompatibleConfigurationMismatch = 4674 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4675 bool DisableValidation = shouldDisableValidationForFile(F); 4676 4677 ASTReadResult Result = readUnhashedControlBlockImpl( 4678 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4679 Listener.get(), 4680 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4681 4682 // If F was directly imported by another module, it's implicitly validated by 4683 // the importing module. 4684 if (DisableValidation || WasImportedBy || 4685 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4686 return Success; 4687 4688 if (Result == Failure) { 4689 Error("malformed block record in AST file"); 4690 return Failure; 4691 } 4692 4693 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4694 // If this module has already been finalized in the ModuleCache, we're stuck 4695 // with it; we can only load a single version of each module. 4696 // 4697 // This can happen when a module is imported in two contexts: in one, as a 4698 // user module; in another, as a system module (due to an import from 4699 // another module marked with the [system] flag). It usually indicates a 4700 // bug in the module map: this module should also be marked with [system]. 4701 // 4702 // If -Wno-system-headers (the default), and the first import is as a 4703 // system module, then validation will fail during the as-user import, 4704 // since -Werror flags won't have been validated. However, it's reasonable 4705 // to treat this consistently as a system module. 4706 // 4707 // If -Wsystem-headers, the PCM on disk was built with 4708 // -Wno-system-headers, and the first import is as a user module, then 4709 // validation will fail during the as-system import since the PCM on disk 4710 // doesn't guarantee that -Werror was respected. However, the -Werror 4711 // flags were checked during the initial as-user import. 4712 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4713 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4714 return Success; 4715 } 4716 } 4717 4718 return Result; 4719 } 4720 4721 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4722 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4723 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4724 bool ValidateDiagnosticOptions) { 4725 // Initialize a stream. 4726 BitstreamCursor Stream(StreamData); 4727 4728 // Sniff for the signature. 4729 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4730 // FIXME this drops the error on the floor. 4731 consumeError(std::move(Err)); 4732 return Failure; 4733 } 4734 4735 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4736 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4737 return Failure; 4738 4739 // Read all of the records in the options block. 4740 RecordData Record; 4741 ASTReadResult Result = Success; 4742 while (true) { 4743 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4744 if (!MaybeEntry) { 4745 // FIXME this drops the error on the floor. 4746 consumeError(MaybeEntry.takeError()); 4747 return Failure; 4748 } 4749 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4750 4751 switch (Entry.Kind) { 4752 case llvm::BitstreamEntry::Error: 4753 case llvm::BitstreamEntry::SubBlock: 4754 return Failure; 4755 4756 case llvm::BitstreamEntry::EndBlock: 4757 return Result; 4758 4759 case llvm::BitstreamEntry::Record: 4760 // The interesting case. 4761 break; 4762 } 4763 4764 // Read and process a record. 4765 Record.clear(); 4766 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4767 if (!MaybeRecordType) { 4768 // FIXME this drops the error. 4769 return Failure; 4770 } 4771 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4772 case SIGNATURE: 4773 if (F) 4774 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4775 break; 4776 case AST_BLOCK_HASH: 4777 if (F) 4778 F->ASTBlockHash = 4779 ASTFileSignature::create(Record.begin(), Record.end()); 4780 break; 4781 case DIAGNOSTIC_OPTIONS: { 4782 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4783 if (Listener && ValidateDiagnosticOptions && 4784 !AllowCompatibleConfigurationMismatch && 4785 ParseDiagnosticOptions(Record, Complain, *Listener)) 4786 Result = OutOfDate; // Don't return early. Read the signature. 4787 break; 4788 } 4789 case DIAG_PRAGMA_MAPPINGS: 4790 if (!F) 4791 break; 4792 if (F->PragmaDiagMappings.empty()) 4793 F->PragmaDiagMappings.swap(Record); 4794 else 4795 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4796 Record.begin(), Record.end()); 4797 break; 4798 } 4799 } 4800 } 4801 4802 /// Parse a record and blob containing module file extension metadata. 4803 static bool parseModuleFileExtensionMetadata( 4804 const SmallVectorImpl<uint64_t> &Record, 4805 StringRef Blob, 4806 ModuleFileExtensionMetadata &Metadata) { 4807 if (Record.size() < 4) return true; 4808 4809 Metadata.MajorVersion = Record[0]; 4810 Metadata.MinorVersion = Record[1]; 4811 4812 unsigned BlockNameLen = Record[2]; 4813 unsigned UserInfoLen = Record[3]; 4814 4815 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4816 4817 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4818 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4819 Blob.data() + BlockNameLen + UserInfoLen); 4820 return false; 4821 } 4822 4823 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4824 BitstreamCursor &Stream = F.Stream; 4825 4826 RecordData Record; 4827 while (true) { 4828 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4829 if (!MaybeEntry) { 4830 Error(MaybeEntry.takeError()); 4831 return Failure; 4832 } 4833 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4834 4835 switch (Entry.Kind) { 4836 case llvm::BitstreamEntry::SubBlock: 4837 if (llvm::Error Err = Stream.SkipBlock()) { 4838 Error(std::move(Err)); 4839 return Failure; 4840 } 4841 continue; 4842 4843 case llvm::BitstreamEntry::EndBlock: 4844 return Success; 4845 4846 case llvm::BitstreamEntry::Error: 4847 return HadErrors; 4848 4849 case llvm::BitstreamEntry::Record: 4850 break; 4851 } 4852 4853 Record.clear(); 4854 StringRef Blob; 4855 Expected<unsigned> MaybeRecCode = 4856 Stream.readRecord(Entry.ID, Record, &Blob); 4857 if (!MaybeRecCode) { 4858 Error(MaybeRecCode.takeError()); 4859 return Failure; 4860 } 4861 switch (MaybeRecCode.get()) { 4862 case EXTENSION_METADATA: { 4863 ModuleFileExtensionMetadata Metadata; 4864 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) { 4865 Error("malformed EXTENSION_METADATA in AST file"); 4866 return Failure; 4867 } 4868 4869 // Find a module file extension with this block name. 4870 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4871 if (Known == ModuleFileExtensions.end()) break; 4872 4873 // Form a reader. 4874 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4875 F, Stream)) { 4876 F.ExtensionReaders.push_back(std::move(Reader)); 4877 } 4878 4879 break; 4880 } 4881 } 4882 } 4883 4884 return Success; 4885 } 4886 4887 void ASTReader::InitializeContext() { 4888 assert(ContextObj && "no context to initialize"); 4889 ASTContext &Context = *ContextObj; 4890 4891 // If there's a listener, notify them that we "read" the translation unit. 4892 if (DeserializationListener) 4893 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4894 Context.getTranslationUnitDecl()); 4895 4896 // FIXME: Find a better way to deal with collisions between these 4897 // built-in types. Right now, we just ignore the problem. 4898 4899 // Load the special types. 4900 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4901 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4902 if (!Context.CFConstantStringTypeDecl) 4903 Context.setCFConstantStringType(GetType(String)); 4904 } 4905 4906 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4907 QualType FileType = GetType(File); 4908 if (FileType.isNull()) { 4909 Error("FILE type is NULL"); 4910 return; 4911 } 4912 4913 if (!Context.FILEDecl) { 4914 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4915 Context.setFILEDecl(Typedef->getDecl()); 4916 else { 4917 const TagType *Tag = FileType->getAs<TagType>(); 4918 if (!Tag) { 4919 Error("Invalid FILE type in AST file"); 4920 return; 4921 } 4922 Context.setFILEDecl(Tag->getDecl()); 4923 } 4924 } 4925 } 4926 4927 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4928 QualType Jmp_bufType = GetType(Jmp_buf); 4929 if (Jmp_bufType.isNull()) { 4930 Error("jmp_buf type is NULL"); 4931 return; 4932 } 4933 4934 if (!Context.jmp_bufDecl) { 4935 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4936 Context.setjmp_bufDecl(Typedef->getDecl()); 4937 else { 4938 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4939 if (!Tag) { 4940 Error("Invalid jmp_buf type in AST file"); 4941 return; 4942 } 4943 Context.setjmp_bufDecl(Tag->getDecl()); 4944 } 4945 } 4946 } 4947 4948 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4949 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4950 if (Sigjmp_bufType.isNull()) { 4951 Error("sigjmp_buf type is NULL"); 4952 return; 4953 } 4954 4955 if (!Context.sigjmp_bufDecl) { 4956 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4957 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4958 else { 4959 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4960 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4961 Context.setsigjmp_bufDecl(Tag->getDecl()); 4962 } 4963 } 4964 } 4965 4966 if (unsigned ObjCIdRedef 4967 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4968 if (Context.ObjCIdRedefinitionType.isNull()) 4969 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4970 } 4971 4972 if (unsigned ObjCClassRedef 4973 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4974 if (Context.ObjCClassRedefinitionType.isNull()) 4975 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4976 } 4977 4978 if (unsigned ObjCSelRedef 4979 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4980 if (Context.ObjCSelRedefinitionType.isNull()) 4981 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4982 } 4983 4984 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4985 QualType Ucontext_tType = GetType(Ucontext_t); 4986 if (Ucontext_tType.isNull()) { 4987 Error("ucontext_t type is NULL"); 4988 return; 4989 } 4990 4991 if (!Context.ucontext_tDecl) { 4992 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4993 Context.setucontext_tDecl(Typedef->getDecl()); 4994 else { 4995 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4996 assert(Tag && "Invalid ucontext_t type in AST file"); 4997 Context.setucontext_tDecl(Tag->getDecl()); 4998 } 4999 } 5000 } 5001 } 5002 5003 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 5004 5005 // If there were any CUDA special declarations, deserialize them. 5006 if (!CUDASpecialDeclRefs.empty()) { 5007 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 5008 Context.setcudaConfigureCallDecl( 5009 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 5010 } 5011 5012 // Re-export any modules that were imported by a non-module AST file. 5013 // FIXME: This does not make macro-only imports visible again. 5014 for (auto &Import : ImportedModules) { 5015 if (Module *Imported = getSubmodule(Import.ID)) { 5016 makeModuleVisible(Imported, Module::AllVisible, 5017 /*ImportLoc=*/Import.ImportLoc); 5018 if (Import.ImportLoc.isValid()) 5019 PP.makeModuleVisible(Imported, Import.ImportLoc); 5020 // This updates visibility for Preprocessor only. For Sema, which can be 5021 // nullptr here, we do the same later, in UpdateSema(). 5022 } 5023 } 5024 } 5025 5026 void ASTReader::finalizeForWriting() { 5027 // Nothing to do for now. 5028 } 5029 5030 /// Reads and return the signature record from \p PCH's control block, or 5031 /// else returns 0. 5032 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5033 BitstreamCursor Stream(PCH); 5034 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5035 // FIXME this drops the error on the floor. 5036 consumeError(std::move(Err)); 5037 return ASTFileSignature(); 5038 } 5039 5040 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5041 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5042 return ASTFileSignature(); 5043 5044 // Scan for SIGNATURE inside the diagnostic options block. 5045 ASTReader::RecordData Record; 5046 while (true) { 5047 Expected<llvm::BitstreamEntry> MaybeEntry = 5048 Stream.advanceSkippingSubblocks(); 5049 if (!MaybeEntry) { 5050 // FIXME this drops the error on the floor. 5051 consumeError(MaybeEntry.takeError()); 5052 return ASTFileSignature(); 5053 } 5054 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5055 5056 if (Entry.Kind != llvm::BitstreamEntry::Record) 5057 return ASTFileSignature(); 5058 5059 Record.clear(); 5060 StringRef Blob; 5061 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5062 if (!MaybeRecord) { 5063 // FIXME this drops the error on the floor. 5064 consumeError(MaybeRecord.takeError()); 5065 return ASTFileSignature(); 5066 } 5067 if (SIGNATURE == MaybeRecord.get()) 5068 return ASTFileSignature::create(Record.begin(), 5069 Record.begin() + ASTFileSignature::size); 5070 } 5071 } 5072 5073 /// Retrieve the name of the original source file name 5074 /// directly from the AST file, without actually loading the AST 5075 /// file. 5076 std::string ASTReader::getOriginalSourceFile( 5077 const std::string &ASTFileName, FileManager &FileMgr, 5078 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5079 // Open the AST file. 5080 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5081 if (!Buffer) { 5082 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5083 << ASTFileName << Buffer.getError().message(); 5084 return std::string(); 5085 } 5086 5087 // Initialize the stream 5088 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5089 5090 // Sniff for the signature. 5091 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5092 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5093 return std::string(); 5094 } 5095 5096 // Scan for the CONTROL_BLOCK_ID block. 5097 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5098 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5099 return std::string(); 5100 } 5101 5102 // Scan for ORIGINAL_FILE inside the control block. 5103 RecordData Record; 5104 while (true) { 5105 Expected<llvm::BitstreamEntry> MaybeEntry = 5106 Stream.advanceSkippingSubblocks(); 5107 if (!MaybeEntry) { 5108 // FIXME this drops errors on the floor. 5109 consumeError(MaybeEntry.takeError()); 5110 return std::string(); 5111 } 5112 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5113 5114 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5115 return std::string(); 5116 5117 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5118 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5119 return std::string(); 5120 } 5121 5122 Record.clear(); 5123 StringRef Blob; 5124 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5125 if (!MaybeRecord) { 5126 // FIXME this drops the errors on the floor. 5127 consumeError(MaybeRecord.takeError()); 5128 return std::string(); 5129 } 5130 if (ORIGINAL_FILE == MaybeRecord.get()) 5131 return Blob.str(); 5132 } 5133 } 5134 5135 namespace { 5136 5137 class SimplePCHValidator : public ASTReaderListener { 5138 const LangOptions &ExistingLangOpts; 5139 const TargetOptions &ExistingTargetOpts; 5140 const PreprocessorOptions &ExistingPPOpts; 5141 std::string ExistingModuleCachePath; 5142 FileManager &FileMgr; 5143 5144 public: 5145 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5146 const TargetOptions &ExistingTargetOpts, 5147 const PreprocessorOptions &ExistingPPOpts, 5148 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5149 : ExistingLangOpts(ExistingLangOpts), 5150 ExistingTargetOpts(ExistingTargetOpts), 5151 ExistingPPOpts(ExistingPPOpts), 5152 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5153 5154 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5155 bool AllowCompatibleDifferences) override { 5156 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5157 AllowCompatibleDifferences); 5158 } 5159 5160 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5161 bool AllowCompatibleDifferences) override { 5162 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5163 AllowCompatibleDifferences); 5164 } 5165 5166 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5167 StringRef SpecificModuleCachePath, 5168 bool Complain) override { 5169 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5170 ExistingModuleCachePath, 5171 nullptr, ExistingLangOpts); 5172 } 5173 5174 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5175 bool Complain, 5176 std::string &SuggestedPredefines) override { 5177 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5178 SuggestedPredefines, ExistingLangOpts); 5179 } 5180 }; 5181 5182 } // namespace 5183 5184 bool ASTReader::readASTFileControlBlock( 5185 StringRef Filename, FileManager &FileMgr, 5186 const PCHContainerReader &PCHContainerRdr, 5187 bool FindModuleFileExtensions, 5188 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5189 // Open the AST file. 5190 // FIXME: This allows use of the VFS; we do not allow use of the 5191 // VFS when actually loading a module. 5192 auto Buffer = FileMgr.getBufferForFile(Filename); 5193 if (!Buffer) { 5194 return true; 5195 } 5196 5197 // Initialize the stream 5198 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5199 BitstreamCursor Stream(Bytes); 5200 5201 // Sniff for the signature. 5202 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5203 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5204 return true; 5205 } 5206 5207 // Scan for the CONTROL_BLOCK_ID block. 5208 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5209 return true; 5210 5211 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5212 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5213 bool NeedsImports = Listener.needsImportVisitation(); 5214 BitstreamCursor InputFilesCursor; 5215 5216 RecordData Record; 5217 std::string ModuleDir; 5218 bool DoneWithControlBlock = false; 5219 while (!DoneWithControlBlock) { 5220 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5221 if (!MaybeEntry) { 5222 // FIXME this drops the error on the floor. 5223 consumeError(MaybeEntry.takeError()); 5224 return true; 5225 } 5226 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5227 5228 switch (Entry.Kind) { 5229 case llvm::BitstreamEntry::SubBlock: { 5230 switch (Entry.ID) { 5231 case OPTIONS_BLOCK_ID: { 5232 std::string IgnoredSuggestedPredefines; 5233 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5234 /*AllowCompatibleConfigurationMismatch*/ false, 5235 Listener, IgnoredSuggestedPredefines) != Success) 5236 return true; 5237 break; 5238 } 5239 5240 case INPUT_FILES_BLOCK_ID: 5241 InputFilesCursor = Stream; 5242 if (llvm::Error Err = Stream.SkipBlock()) { 5243 // FIXME this drops the error on the floor. 5244 consumeError(std::move(Err)); 5245 return true; 5246 } 5247 if (NeedsInputFiles && 5248 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5249 return true; 5250 break; 5251 5252 default: 5253 if (llvm::Error Err = Stream.SkipBlock()) { 5254 // FIXME this drops the error on the floor. 5255 consumeError(std::move(Err)); 5256 return true; 5257 } 5258 break; 5259 } 5260 5261 continue; 5262 } 5263 5264 case llvm::BitstreamEntry::EndBlock: 5265 DoneWithControlBlock = true; 5266 break; 5267 5268 case llvm::BitstreamEntry::Error: 5269 return true; 5270 5271 case llvm::BitstreamEntry::Record: 5272 break; 5273 } 5274 5275 if (DoneWithControlBlock) break; 5276 5277 Record.clear(); 5278 StringRef Blob; 5279 Expected<unsigned> MaybeRecCode = 5280 Stream.readRecord(Entry.ID, Record, &Blob); 5281 if (!MaybeRecCode) { 5282 // FIXME this drops the error. 5283 return Failure; 5284 } 5285 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5286 case METADATA: 5287 if (Record[0] != VERSION_MAJOR) 5288 return true; 5289 if (Listener.ReadFullVersionInformation(Blob)) 5290 return true; 5291 break; 5292 case MODULE_NAME: 5293 Listener.ReadModuleName(Blob); 5294 break; 5295 case MODULE_DIRECTORY: 5296 ModuleDir = std::string(Blob); 5297 break; 5298 case MODULE_MAP_FILE: { 5299 unsigned Idx = 0; 5300 auto Path = ReadString(Record, Idx); 5301 ResolveImportedPath(Path, ModuleDir); 5302 Listener.ReadModuleMapFile(Path); 5303 break; 5304 } 5305 case INPUT_FILE_OFFSETS: { 5306 if (!NeedsInputFiles) 5307 break; 5308 5309 unsigned NumInputFiles = Record[0]; 5310 unsigned NumUserFiles = Record[1]; 5311 const llvm::support::unaligned_uint64_t *InputFileOffs = 5312 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5313 for (unsigned I = 0; I != NumInputFiles; ++I) { 5314 // Go find this input file. 5315 bool isSystemFile = I >= NumUserFiles; 5316 5317 if (isSystemFile && !NeedsSystemInputFiles) 5318 break; // the rest are system input files 5319 5320 BitstreamCursor &Cursor = InputFilesCursor; 5321 SavedStreamPosition SavedPosition(Cursor); 5322 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5323 // FIXME this drops errors on the floor. 5324 consumeError(std::move(Err)); 5325 } 5326 5327 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5328 if (!MaybeCode) { 5329 // FIXME this drops errors on the floor. 5330 consumeError(MaybeCode.takeError()); 5331 } 5332 unsigned Code = MaybeCode.get(); 5333 5334 RecordData Record; 5335 StringRef Blob; 5336 bool shouldContinue = false; 5337 Expected<unsigned> MaybeRecordType = 5338 Cursor.readRecord(Code, Record, &Blob); 5339 if (!MaybeRecordType) { 5340 // FIXME this drops errors on the floor. 5341 consumeError(MaybeRecordType.takeError()); 5342 } 5343 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5344 case INPUT_FILE_HASH: 5345 break; 5346 case INPUT_FILE: 5347 bool Overridden = static_cast<bool>(Record[3]); 5348 std::string Filename = std::string(Blob); 5349 ResolveImportedPath(Filename, ModuleDir); 5350 shouldContinue = Listener.visitInputFile( 5351 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5352 break; 5353 } 5354 if (!shouldContinue) 5355 break; 5356 } 5357 break; 5358 } 5359 5360 case IMPORTS: { 5361 if (!NeedsImports) 5362 break; 5363 5364 unsigned Idx = 0, N = Record.size(); 5365 while (Idx < N) { 5366 // Read information about the AST file. 5367 Idx += 5368 1 + 1 + 1 + 1 + 5369 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5370 std::string ModuleName = ReadString(Record, Idx); 5371 std::string Filename = ReadString(Record, Idx); 5372 ResolveImportedPath(Filename, ModuleDir); 5373 Listener.visitImport(ModuleName, Filename); 5374 } 5375 break; 5376 } 5377 5378 default: 5379 // No other validation to perform. 5380 break; 5381 } 5382 } 5383 5384 // Look for module file extension blocks, if requested. 5385 if (FindModuleFileExtensions) { 5386 BitstreamCursor SavedStream = Stream; 5387 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5388 bool DoneWithExtensionBlock = false; 5389 while (!DoneWithExtensionBlock) { 5390 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5391 if (!MaybeEntry) { 5392 // FIXME this drops the error. 5393 return true; 5394 } 5395 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5396 5397 switch (Entry.Kind) { 5398 case llvm::BitstreamEntry::SubBlock: 5399 if (llvm::Error Err = Stream.SkipBlock()) { 5400 // FIXME this drops the error on the floor. 5401 consumeError(std::move(Err)); 5402 return true; 5403 } 5404 continue; 5405 5406 case llvm::BitstreamEntry::EndBlock: 5407 DoneWithExtensionBlock = true; 5408 continue; 5409 5410 case llvm::BitstreamEntry::Error: 5411 return true; 5412 5413 case llvm::BitstreamEntry::Record: 5414 break; 5415 } 5416 5417 Record.clear(); 5418 StringRef Blob; 5419 Expected<unsigned> MaybeRecCode = 5420 Stream.readRecord(Entry.ID, Record, &Blob); 5421 if (!MaybeRecCode) { 5422 // FIXME this drops the error. 5423 return true; 5424 } 5425 switch (MaybeRecCode.get()) { 5426 case EXTENSION_METADATA: { 5427 ModuleFileExtensionMetadata Metadata; 5428 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5429 return true; 5430 5431 Listener.readModuleFileExtension(Metadata); 5432 break; 5433 } 5434 } 5435 } 5436 } 5437 Stream = SavedStream; 5438 } 5439 5440 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5441 if (readUnhashedControlBlockImpl( 5442 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5443 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5444 ValidateDiagnosticOptions) != Success) 5445 return true; 5446 5447 return false; 5448 } 5449 5450 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5451 const PCHContainerReader &PCHContainerRdr, 5452 const LangOptions &LangOpts, 5453 const TargetOptions &TargetOpts, 5454 const PreprocessorOptions &PPOpts, 5455 StringRef ExistingModuleCachePath) { 5456 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5457 ExistingModuleCachePath, FileMgr); 5458 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5459 /*FindModuleFileExtensions=*/false, 5460 validator, 5461 /*ValidateDiagnosticOptions=*/true); 5462 } 5463 5464 ASTReader::ASTReadResult 5465 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 5466 // Enter the submodule block. 5467 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 5468 Error(std::move(Err)); 5469 return Failure; 5470 } 5471 5472 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5473 bool First = true; 5474 Module *CurrentModule = nullptr; 5475 RecordData Record; 5476 while (true) { 5477 Expected<llvm::BitstreamEntry> MaybeEntry = 5478 F.Stream.advanceSkippingSubblocks(); 5479 if (!MaybeEntry) { 5480 Error(MaybeEntry.takeError()); 5481 return Failure; 5482 } 5483 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5484 5485 switch (Entry.Kind) { 5486 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5487 case llvm::BitstreamEntry::Error: 5488 Error("malformed block record in AST file"); 5489 return Failure; 5490 case llvm::BitstreamEntry::EndBlock: 5491 return Success; 5492 case llvm::BitstreamEntry::Record: 5493 // The interesting case. 5494 break; 5495 } 5496 5497 // Read a record. 5498 StringRef Blob; 5499 Record.clear(); 5500 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5501 if (!MaybeKind) { 5502 Error(MaybeKind.takeError()); 5503 return Failure; 5504 } 5505 unsigned Kind = MaybeKind.get(); 5506 5507 if ((Kind == SUBMODULE_METADATA) != First) { 5508 Error("submodule metadata record should be at beginning of block"); 5509 return Failure; 5510 } 5511 First = false; 5512 5513 // Submodule information is only valid if we have a current module. 5514 // FIXME: Should we error on these cases? 5515 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5516 Kind != SUBMODULE_DEFINITION) 5517 continue; 5518 5519 switch (Kind) { 5520 default: // Default behavior: ignore. 5521 break; 5522 5523 case SUBMODULE_DEFINITION: { 5524 if (Record.size() < 12) { 5525 Error("malformed module definition"); 5526 return Failure; 5527 } 5528 5529 StringRef Name = Blob; 5530 unsigned Idx = 0; 5531 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5532 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5533 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5534 bool IsFramework = Record[Idx++]; 5535 bool IsExplicit = Record[Idx++]; 5536 bool IsSystem = Record[Idx++]; 5537 bool IsExternC = Record[Idx++]; 5538 bool InferSubmodules = Record[Idx++]; 5539 bool InferExplicitSubmodules = Record[Idx++]; 5540 bool InferExportWildcard = Record[Idx++]; 5541 bool ConfigMacrosExhaustive = Record[Idx++]; 5542 bool ModuleMapIsPrivate = Record[Idx++]; 5543 5544 Module *ParentModule = nullptr; 5545 if (Parent) 5546 ParentModule = getSubmodule(Parent); 5547 5548 // Retrieve this (sub)module from the module map, creating it if 5549 // necessary. 5550 CurrentModule = 5551 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5552 .first; 5553 5554 // FIXME: set the definition loc for CurrentModule, or call 5555 // ModMap.setInferredModuleAllowedBy() 5556 5557 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5558 if (GlobalIndex >= SubmodulesLoaded.size() || 5559 SubmodulesLoaded[GlobalIndex]) { 5560 Error("too many submodules"); 5561 return Failure; 5562 } 5563 5564 if (!ParentModule) { 5565 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5566 // Don't emit module relocation error if we have -fno-validate-pch 5567 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 5568 DisableValidationForModuleKind::Module) && 5569 CurFile != F.File) { 5570 Error(diag::err_module_file_conflict, 5571 CurrentModule->getTopLevelModuleName(), CurFile->getName(), 5572 F.File->getName()); 5573 return Failure; 5574 } 5575 } 5576 5577 F.DidReadTopLevelSubmodule = true; 5578 CurrentModule->setASTFile(F.File); 5579 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5580 } 5581 5582 CurrentModule->Kind = Kind; 5583 CurrentModule->Signature = F.Signature; 5584 CurrentModule->IsFromModuleFile = true; 5585 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5586 CurrentModule->IsExternC = IsExternC; 5587 CurrentModule->InferSubmodules = InferSubmodules; 5588 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5589 CurrentModule->InferExportWildcard = InferExportWildcard; 5590 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5591 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5592 if (DeserializationListener) 5593 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5594 5595 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5596 5597 // Clear out data that will be replaced by what is in the module file. 5598 CurrentModule->LinkLibraries.clear(); 5599 CurrentModule->ConfigMacros.clear(); 5600 CurrentModule->UnresolvedConflicts.clear(); 5601 CurrentModule->Conflicts.clear(); 5602 5603 // The module is available unless it's missing a requirement; relevant 5604 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5605 // Missing headers that were present when the module was built do not 5606 // make it unavailable -- if we got this far, this must be an explicitly 5607 // imported module file. 5608 CurrentModule->Requirements.clear(); 5609 CurrentModule->MissingHeaders.clear(); 5610 CurrentModule->IsUnimportable = 5611 ParentModule && ParentModule->IsUnimportable; 5612 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5613 break; 5614 } 5615 5616 case SUBMODULE_UMBRELLA_HEADER: { 5617 std::string Filename = std::string(Blob); 5618 ResolveImportedPath(F, Filename); 5619 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5620 if (!CurrentModule->getUmbrellaHeader()) 5621 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob); 5622 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) { 5623 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5624 Error("mismatched umbrella headers in submodule"); 5625 return OutOfDate; 5626 } 5627 } 5628 break; 5629 } 5630 5631 case SUBMODULE_HEADER: 5632 case SUBMODULE_EXCLUDED_HEADER: 5633 case SUBMODULE_PRIVATE_HEADER: 5634 // We lazily associate headers with their modules via the HeaderInfo table. 5635 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5636 // of complete filenames or remove it entirely. 5637 break; 5638 5639 case SUBMODULE_TEXTUAL_HEADER: 5640 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5641 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5642 // them here. 5643 break; 5644 5645 case SUBMODULE_TOPHEADER: 5646 CurrentModule->addTopHeaderFilename(Blob); 5647 break; 5648 5649 case SUBMODULE_UMBRELLA_DIR: { 5650 std::string Dirname = std::string(Blob); 5651 ResolveImportedPath(F, Dirname); 5652 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5653 if (!CurrentModule->getUmbrellaDir()) 5654 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob); 5655 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) { 5656 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5657 Error("mismatched umbrella directories in submodule"); 5658 return OutOfDate; 5659 } 5660 } 5661 break; 5662 } 5663 5664 case SUBMODULE_METADATA: { 5665 F.BaseSubmoduleID = getTotalNumSubmodules(); 5666 F.LocalNumSubmodules = Record[0]; 5667 unsigned LocalBaseSubmoduleID = Record[1]; 5668 if (F.LocalNumSubmodules > 0) { 5669 // Introduce the global -> local mapping for submodules within this 5670 // module. 5671 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5672 5673 // Introduce the local -> global mapping for submodules within this 5674 // module. 5675 F.SubmoduleRemap.insertOrReplace( 5676 std::make_pair(LocalBaseSubmoduleID, 5677 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5678 5679 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5680 } 5681 break; 5682 } 5683 5684 case SUBMODULE_IMPORTS: 5685 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5686 UnresolvedModuleRef Unresolved; 5687 Unresolved.File = &F; 5688 Unresolved.Mod = CurrentModule; 5689 Unresolved.ID = Record[Idx]; 5690 Unresolved.Kind = UnresolvedModuleRef::Import; 5691 Unresolved.IsWildcard = false; 5692 UnresolvedModuleRefs.push_back(Unresolved); 5693 } 5694 break; 5695 5696 case SUBMODULE_EXPORTS: 5697 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5698 UnresolvedModuleRef Unresolved; 5699 Unresolved.File = &F; 5700 Unresolved.Mod = CurrentModule; 5701 Unresolved.ID = Record[Idx]; 5702 Unresolved.Kind = UnresolvedModuleRef::Export; 5703 Unresolved.IsWildcard = Record[Idx + 1]; 5704 UnresolvedModuleRefs.push_back(Unresolved); 5705 } 5706 5707 // Once we've loaded the set of exports, there's no reason to keep 5708 // the parsed, unresolved exports around. 5709 CurrentModule->UnresolvedExports.clear(); 5710 break; 5711 5712 case SUBMODULE_REQUIRES: 5713 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5714 PP.getTargetInfo()); 5715 break; 5716 5717 case SUBMODULE_LINK_LIBRARY: 5718 ModMap.resolveLinkAsDependencies(CurrentModule); 5719 CurrentModule->LinkLibraries.push_back( 5720 Module::LinkLibrary(std::string(Blob), Record[0])); 5721 break; 5722 5723 case SUBMODULE_CONFIG_MACRO: 5724 CurrentModule->ConfigMacros.push_back(Blob.str()); 5725 break; 5726 5727 case SUBMODULE_CONFLICT: { 5728 UnresolvedModuleRef Unresolved; 5729 Unresolved.File = &F; 5730 Unresolved.Mod = CurrentModule; 5731 Unresolved.ID = Record[0]; 5732 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5733 Unresolved.IsWildcard = false; 5734 Unresolved.String = Blob; 5735 UnresolvedModuleRefs.push_back(Unresolved); 5736 break; 5737 } 5738 5739 case SUBMODULE_INITIALIZERS: { 5740 if (!ContextObj) 5741 break; 5742 SmallVector<uint32_t, 16> Inits; 5743 for (auto &ID : Record) 5744 Inits.push_back(getGlobalDeclID(F, ID)); 5745 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5746 break; 5747 } 5748 5749 case SUBMODULE_EXPORT_AS: 5750 CurrentModule->ExportAsModule = Blob.str(); 5751 ModMap.addLinkAsDependency(CurrentModule); 5752 break; 5753 } 5754 } 5755 } 5756 5757 /// Parse the record that corresponds to a LangOptions data 5758 /// structure. 5759 /// 5760 /// This routine parses the language options from the AST file and then gives 5761 /// them to the AST listener if one is set. 5762 /// 5763 /// \returns true if the listener deems the file unacceptable, false otherwise. 5764 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5765 bool Complain, 5766 ASTReaderListener &Listener, 5767 bool AllowCompatibleDifferences) { 5768 LangOptions LangOpts; 5769 unsigned Idx = 0; 5770 #define LANGOPT(Name, Bits, Default, Description) \ 5771 LangOpts.Name = Record[Idx++]; 5772 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5773 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5774 #include "clang/Basic/LangOptions.def" 5775 #define SANITIZER(NAME, ID) \ 5776 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5777 #include "clang/Basic/Sanitizers.def" 5778 5779 for (unsigned N = Record[Idx++]; N; --N) 5780 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5781 5782 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5783 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5784 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5785 5786 LangOpts.CurrentModule = ReadString(Record, Idx); 5787 5788 // Comment options. 5789 for (unsigned N = Record[Idx++]; N; --N) { 5790 LangOpts.CommentOpts.BlockCommandNames.push_back( 5791 ReadString(Record, Idx)); 5792 } 5793 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5794 5795 // OpenMP offloading options. 5796 for (unsigned N = Record[Idx++]; N; --N) { 5797 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5798 } 5799 5800 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5801 5802 return Listener.ReadLanguageOptions(LangOpts, Complain, 5803 AllowCompatibleDifferences); 5804 } 5805 5806 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5807 ASTReaderListener &Listener, 5808 bool AllowCompatibleDifferences) { 5809 unsigned Idx = 0; 5810 TargetOptions TargetOpts; 5811 TargetOpts.Triple = ReadString(Record, Idx); 5812 TargetOpts.CPU = ReadString(Record, Idx); 5813 TargetOpts.TuneCPU = ReadString(Record, Idx); 5814 TargetOpts.ABI = ReadString(Record, Idx); 5815 for (unsigned N = Record[Idx++]; N; --N) { 5816 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5817 } 5818 for (unsigned N = Record[Idx++]; N; --N) { 5819 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5820 } 5821 5822 return Listener.ReadTargetOptions(TargetOpts, Complain, 5823 AllowCompatibleDifferences); 5824 } 5825 5826 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5827 ASTReaderListener &Listener) { 5828 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5829 unsigned Idx = 0; 5830 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5831 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5832 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5833 #include "clang/Basic/DiagnosticOptions.def" 5834 5835 for (unsigned N = Record[Idx++]; N; --N) 5836 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5837 for (unsigned N = Record[Idx++]; N; --N) 5838 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5839 5840 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5841 } 5842 5843 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5844 ASTReaderListener &Listener) { 5845 FileSystemOptions FSOpts; 5846 unsigned Idx = 0; 5847 FSOpts.WorkingDir = ReadString(Record, Idx); 5848 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5849 } 5850 5851 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5852 bool Complain, 5853 ASTReaderListener &Listener) { 5854 HeaderSearchOptions HSOpts; 5855 unsigned Idx = 0; 5856 HSOpts.Sysroot = ReadString(Record, Idx); 5857 5858 // Include entries. 5859 for (unsigned N = Record[Idx++]; N; --N) { 5860 std::string Path = ReadString(Record, Idx); 5861 frontend::IncludeDirGroup Group 5862 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5863 bool IsFramework = Record[Idx++]; 5864 bool IgnoreSysRoot = Record[Idx++]; 5865 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5866 IgnoreSysRoot); 5867 } 5868 5869 // System header prefixes. 5870 for (unsigned N = Record[Idx++]; N; --N) { 5871 std::string Prefix = ReadString(Record, Idx); 5872 bool IsSystemHeader = Record[Idx++]; 5873 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5874 } 5875 5876 HSOpts.ResourceDir = ReadString(Record, Idx); 5877 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5878 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5879 HSOpts.DisableModuleHash = Record[Idx++]; 5880 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5881 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5882 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5883 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5884 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5885 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5886 HSOpts.UseLibcxx = Record[Idx++]; 5887 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5888 5889 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5890 Complain); 5891 } 5892 5893 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5894 bool Complain, 5895 ASTReaderListener &Listener, 5896 std::string &SuggestedPredefines) { 5897 PreprocessorOptions PPOpts; 5898 unsigned Idx = 0; 5899 5900 // Macro definitions/undefs 5901 for (unsigned N = Record[Idx++]; N; --N) { 5902 std::string Macro = ReadString(Record, Idx); 5903 bool IsUndef = Record[Idx++]; 5904 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5905 } 5906 5907 // Includes 5908 for (unsigned N = Record[Idx++]; N; --N) { 5909 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5910 } 5911 5912 // Macro Includes 5913 for (unsigned N = Record[Idx++]; N; --N) { 5914 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5915 } 5916 5917 PPOpts.UsePredefines = Record[Idx++]; 5918 PPOpts.DetailedRecord = Record[Idx++]; 5919 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5920 PPOpts.ObjCXXARCStandardLibrary = 5921 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5922 SuggestedPredefines.clear(); 5923 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5924 SuggestedPredefines); 5925 } 5926 5927 std::pair<ModuleFile *, unsigned> 5928 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5929 GlobalPreprocessedEntityMapType::iterator 5930 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5931 assert(I != GlobalPreprocessedEntityMap.end() && 5932 "Corrupted global preprocessed entity map"); 5933 ModuleFile *M = I->second; 5934 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5935 return std::make_pair(M, LocalIndex); 5936 } 5937 5938 llvm::iterator_range<PreprocessingRecord::iterator> 5939 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5940 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5941 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5942 Mod.NumPreprocessedEntities); 5943 5944 return llvm::make_range(PreprocessingRecord::iterator(), 5945 PreprocessingRecord::iterator()); 5946 } 5947 5948 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5949 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5950 return llvm::make_range( 5951 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5952 ModuleDeclIterator(this, &Mod, 5953 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5954 } 5955 5956 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5957 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5958 assert(I != GlobalSkippedRangeMap.end() && 5959 "Corrupted global skipped range map"); 5960 ModuleFile *M = I->second; 5961 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5962 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5963 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5964 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5965 TranslateSourceLocation(*M, RawRange.getEnd())); 5966 assert(Range.isValid()); 5967 return Range; 5968 } 5969 5970 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5971 PreprocessedEntityID PPID = Index+1; 5972 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5973 ModuleFile &M = *PPInfo.first; 5974 unsigned LocalIndex = PPInfo.second; 5975 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5976 5977 if (!PP.getPreprocessingRecord()) { 5978 Error("no preprocessing record"); 5979 return nullptr; 5980 } 5981 5982 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5983 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5984 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5985 Error(std::move(Err)); 5986 return nullptr; 5987 } 5988 5989 Expected<llvm::BitstreamEntry> MaybeEntry = 5990 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5991 if (!MaybeEntry) { 5992 Error(MaybeEntry.takeError()); 5993 return nullptr; 5994 } 5995 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5996 5997 if (Entry.Kind != llvm::BitstreamEntry::Record) 5998 return nullptr; 5999 6000 // Read the record. 6001 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 6002 TranslateSourceLocation(M, PPOffs.getEnd())); 6003 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 6004 StringRef Blob; 6005 RecordData Record; 6006 Expected<unsigned> MaybeRecType = 6007 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 6008 if (!MaybeRecType) { 6009 Error(MaybeRecType.takeError()); 6010 return nullptr; 6011 } 6012 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 6013 case PPD_MACRO_EXPANSION: { 6014 bool isBuiltin = Record[0]; 6015 IdentifierInfo *Name = nullptr; 6016 MacroDefinitionRecord *Def = nullptr; 6017 if (isBuiltin) 6018 Name = getLocalIdentifier(M, Record[1]); 6019 else { 6020 PreprocessedEntityID GlobalID = 6021 getGlobalPreprocessedEntityID(M, Record[1]); 6022 Def = cast<MacroDefinitionRecord>( 6023 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 6024 } 6025 6026 MacroExpansion *ME; 6027 if (isBuiltin) 6028 ME = new (PPRec) MacroExpansion(Name, Range); 6029 else 6030 ME = new (PPRec) MacroExpansion(Def, Range); 6031 6032 return ME; 6033 } 6034 6035 case PPD_MACRO_DEFINITION: { 6036 // Decode the identifier info and then check again; if the macro is 6037 // still defined and associated with the identifier, 6038 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6039 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6040 6041 if (DeserializationListener) 6042 DeserializationListener->MacroDefinitionRead(PPID, MD); 6043 6044 return MD; 6045 } 6046 6047 case PPD_INCLUSION_DIRECTIVE: { 6048 const char *FullFileNameStart = Blob.data() + Record[0]; 6049 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6050 const FileEntry *File = nullptr; 6051 if (!FullFileName.empty()) 6052 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6053 File = *FE; 6054 6055 // FIXME: Stable encoding 6056 InclusionDirective::InclusionKind Kind 6057 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6058 InclusionDirective *ID 6059 = new (PPRec) InclusionDirective(PPRec, Kind, 6060 StringRef(Blob.data(), Record[0]), 6061 Record[1], Record[3], 6062 File, 6063 Range); 6064 return ID; 6065 } 6066 } 6067 6068 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6069 } 6070 6071 /// Find the next module that contains entities and return the ID 6072 /// of the first entry. 6073 /// 6074 /// \param SLocMapI points at a chunk of a module that contains no 6075 /// preprocessed entities or the entities it contains are not the ones we are 6076 /// looking for. 6077 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6078 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6079 ++SLocMapI; 6080 for (GlobalSLocOffsetMapType::const_iterator 6081 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6082 ModuleFile &M = *SLocMapI->second; 6083 if (M.NumPreprocessedEntities) 6084 return M.BasePreprocessedEntityID; 6085 } 6086 6087 return getTotalNumPreprocessedEntities(); 6088 } 6089 6090 namespace { 6091 6092 struct PPEntityComp { 6093 const ASTReader &Reader; 6094 ModuleFile &M; 6095 6096 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6097 6098 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6099 SourceLocation LHS = getLoc(L); 6100 SourceLocation RHS = getLoc(R); 6101 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6102 } 6103 6104 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6105 SourceLocation LHS = getLoc(L); 6106 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6107 } 6108 6109 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6110 SourceLocation RHS = getLoc(R); 6111 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6112 } 6113 6114 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6115 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6116 } 6117 }; 6118 6119 } // namespace 6120 6121 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6122 bool EndsAfter) const { 6123 if (SourceMgr.isLocalSourceLocation(Loc)) 6124 return getTotalNumPreprocessedEntities(); 6125 6126 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6127 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6128 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6129 "Corrupted global sloc offset map"); 6130 6131 if (SLocMapI->second->NumPreprocessedEntities == 0) 6132 return findNextPreprocessedEntity(SLocMapI); 6133 6134 ModuleFile &M = *SLocMapI->second; 6135 6136 using pp_iterator = const PPEntityOffset *; 6137 6138 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6139 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6140 6141 size_t Count = M.NumPreprocessedEntities; 6142 size_t Half; 6143 pp_iterator First = pp_begin; 6144 pp_iterator PPI; 6145 6146 if (EndsAfter) { 6147 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6148 PPEntityComp(*this, M)); 6149 } else { 6150 // Do a binary search manually instead of using std::lower_bound because 6151 // The end locations of entities may be unordered (when a macro expansion 6152 // is inside another macro argument), but for this case it is not important 6153 // whether we get the first macro expansion or its containing macro. 6154 while (Count > 0) { 6155 Half = Count / 2; 6156 PPI = First; 6157 std::advance(PPI, Half); 6158 if (SourceMgr.isBeforeInTranslationUnit( 6159 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6160 First = PPI; 6161 ++First; 6162 Count = Count - Half - 1; 6163 } else 6164 Count = Half; 6165 } 6166 } 6167 6168 if (PPI == pp_end) 6169 return findNextPreprocessedEntity(SLocMapI); 6170 6171 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6172 } 6173 6174 /// Returns a pair of [Begin, End) indices of preallocated 6175 /// preprocessed entities that \arg Range encompasses. 6176 std::pair<unsigned, unsigned> 6177 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6178 if (Range.isInvalid()) 6179 return std::make_pair(0,0); 6180 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6181 6182 PreprocessedEntityID BeginID = 6183 findPreprocessedEntity(Range.getBegin(), false); 6184 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6185 return std::make_pair(BeginID, EndID); 6186 } 6187 6188 /// Optionally returns true or false if the preallocated preprocessed 6189 /// entity with index \arg Index came from file \arg FID. 6190 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6191 FileID FID) { 6192 if (FID.isInvalid()) 6193 return false; 6194 6195 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6196 ModuleFile &M = *PPInfo.first; 6197 unsigned LocalIndex = PPInfo.second; 6198 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6199 6200 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6201 if (Loc.isInvalid()) 6202 return false; 6203 6204 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6205 return true; 6206 else 6207 return false; 6208 } 6209 6210 namespace { 6211 6212 /// Visitor used to search for information about a header file. 6213 class HeaderFileInfoVisitor { 6214 const FileEntry *FE; 6215 Optional<HeaderFileInfo> HFI; 6216 6217 public: 6218 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6219 6220 bool operator()(ModuleFile &M) { 6221 HeaderFileInfoLookupTable *Table 6222 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6223 if (!Table) 6224 return false; 6225 6226 // Look in the on-disk hash table for an entry for this file name. 6227 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6228 if (Pos == Table->end()) 6229 return false; 6230 6231 HFI = *Pos; 6232 return true; 6233 } 6234 6235 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6236 }; 6237 6238 } // namespace 6239 6240 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6241 HeaderFileInfoVisitor Visitor(FE); 6242 ModuleMgr.visit(Visitor); 6243 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6244 return *HFI; 6245 6246 return HeaderFileInfo(); 6247 } 6248 6249 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6250 using DiagState = DiagnosticsEngine::DiagState; 6251 SmallVector<DiagState *, 32> DiagStates; 6252 6253 for (ModuleFile &F : ModuleMgr) { 6254 unsigned Idx = 0; 6255 auto &Record = F.PragmaDiagMappings; 6256 if (Record.empty()) 6257 continue; 6258 6259 DiagStates.clear(); 6260 6261 auto ReadDiagState = 6262 [&](const DiagState &BasedOn, SourceLocation Loc, 6263 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6264 unsigned BackrefID = Record[Idx++]; 6265 if (BackrefID != 0) 6266 return DiagStates[BackrefID - 1]; 6267 6268 // A new DiagState was created here. 6269 Diag.DiagStates.push_back(BasedOn); 6270 DiagState *NewState = &Diag.DiagStates.back(); 6271 DiagStates.push_back(NewState); 6272 unsigned Size = Record[Idx++]; 6273 assert(Idx + Size * 2 <= Record.size() && 6274 "Invalid data, not enough diag/map pairs"); 6275 while (Size--) { 6276 unsigned DiagID = Record[Idx++]; 6277 DiagnosticMapping NewMapping = 6278 DiagnosticMapping::deserialize(Record[Idx++]); 6279 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6280 continue; 6281 6282 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6283 6284 // If this mapping was specified as a warning but the severity was 6285 // upgraded due to diagnostic settings, simulate the current diagnostic 6286 // settings (and use a warning). 6287 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6288 NewMapping.setSeverity(diag::Severity::Warning); 6289 NewMapping.setUpgradedFromWarning(false); 6290 } 6291 6292 Mapping = NewMapping; 6293 } 6294 return NewState; 6295 }; 6296 6297 // Read the first state. 6298 DiagState *FirstState; 6299 if (F.Kind == MK_ImplicitModule) { 6300 // Implicitly-built modules are reused with different diagnostic 6301 // settings. Use the initial diagnostic state from Diag to simulate this 6302 // compilation's diagnostic settings. 6303 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6304 DiagStates.push_back(FirstState); 6305 6306 // Skip the initial diagnostic state from the serialized module. 6307 assert(Record[1] == 0 && 6308 "Invalid data, unexpected backref in initial state"); 6309 Idx = 3 + Record[2] * 2; 6310 assert(Idx < Record.size() && 6311 "Invalid data, not enough state change pairs in initial state"); 6312 } else if (F.isModule()) { 6313 // For an explicit module, preserve the flags from the module build 6314 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6315 // -Wblah flags. 6316 unsigned Flags = Record[Idx++]; 6317 DiagState Initial; 6318 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6319 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6320 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6321 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6322 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6323 Initial.ExtBehavior = (diag::Severity)Flags; 6324 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6325 6326 assert(F.OriginalSourceFileID.isValid()); 6327 6328 // Set up the root buffer of the module to start with the initial 6329 // diagnostic state of the module itself, to cover files that contain no 6330 // explicit transitions (for which we did not serialize anything). 6331 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6332 .StateTransitions.push_back({FirstState, 0}); 6333 } else { 6334 // For prefix ASTs, start with whatever the user configured on the 6335 // command line. 6336 Idx++; // Skip flags. 6337 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6338 SourceLocation(), false); 6339 } 6340 6341 // Read the state transitions. 6342 unsigned NumLocations = Record[Idx++]; 6343 while (NumLocations--) { 6344 assert(Idx < Record.size() && 6345 "Invalid data, missing pragma diagnostic states"); 6346 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6347 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6348 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6349 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6350 unsigned Transitions = Record[Idx++]; 6351 6352 // Note that we don't need to set up Parent/ParentOffset here, because 6353 // we won't be changing the diagnostic state within imported FileIDs 6354 // (other than perhaps appending to the main source file, which has no 6355 // parent). 6356 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6357 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6358 for (unsigned I = 0; I != Transitions; ++I) { 6359 unsigned Offset = Record[Idx++]; 6360 auto *State = 6361 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6362 F.StateTransitions.push_back({State, Offset}); 6363 } 6364 } 6365 6366 // Read the final state. 6367 assert(Idx < Record.size() && 6368 "Invalid data, missing final pragma diagnostic state"); 6369 SourceLocation CurStateLoc = 6370 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6371 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6372 6373 if (!F.isModule()) { 6374 Diag.DiagStatesByLoc.CurDiagState = CurState; 6375 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6376 6377 // Preserve the property that the imaginary root file describes the 6378 // current state. 6379 FileID NullFile; 6380 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6381 if (T.empty()) 6382 T.push_back({CurState, 0}); 6383 else 6384 T[0].State = CurState; 6385 } 6386 6387 // Don't try to read these mappings again. 6388 Record.clear(); 6389 } 6390 } 6391 6392 /// Get the correct cursor and offset for loading a type. 6393 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6394 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6395 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6396 ModuleFile *M = I->second; 6397 return RecordLocation( 6398 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6399 M->DeclsBlockStartOffset); 6400 } 6401 6402 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6403 switch (code) { 6404 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6405 case TYPE_##CODE_ID: return Type::CLASS_ID; 6406 #include "clang/Serialization/TypeBitCodes.def" 6407 default: return llvm::None; 6408 } 6409 } 6410 6411 /// Read and return the type with the given index.. 6412 /// 6413 /// The index is the type ID, shifted and minus the number of predefs. This 6414 /// routine actually reads the record corresponding to the type at the given 6415 /// location. It is a helper routine for GetType, which deals with reading type 6416 /// IDs. 6417 QualType ASTReader::readTypeRecord(unsigned Index) { 6418 assert(ContextObj && "reading type with no AST context"); 6419 ASTContext &Context = *ContextObj; 6420 RecordLocation Loc = TypeCursorForIndex(Index); 6421 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6422 6423 // Keep track of where we are in the stream, then jump back there 6424 // after reading this type. 6425 SavedStreamPosition SavedPosition(DeclsCursor); 6426 6427 ReadingKindTracker ReadingKind(Read_Type, *this); 6428 6429 // Note that we are loading a type record. 6430 Deserializing AType(this); 6431 6432 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6433 Error(std::move(Err)); 6434 return QualType(); 6435 } 6436 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6437 if (!RawCode) { 6438 Error(RawCode.takeError()); 6439 return QualType(); 6440 } 6441 6442 ASTRecordReader Record(*this, *Loc.F); 6443 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6444 if (!Code) { 6445 Error(Code.takeError()); 6446 return QualType(); 6447 } 6448 if (Code.get() == TYPE_EXT_QUAL) { 6449 QualType baseType = Record.readQualType(); 6450 Qualifiers quals = Record.readQualifiers(); 6451 return Context.getQualifiedType(baseType, quals); 6452 } 6453 6454 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6455 if (!maybeClass) { 6456 Error("Unexpected code for type"); 6457 return QualType(); 6458 } 6459 6460 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6461 return TypeReader.read(*maybeClass); 6462 } 6463 6464 namespace clang { 6465 6466 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6467 ASTRecordReader &Reader; 6468 6469 SourceLocation readSourceLocation() { 6470 return Reader.readSourceLocation(); 6471 } 6472 6473 TypeSourceInfo *GetTypeSourceInfo() { 6474 return Reader.readTypeSourceInfo(); 6475 } 6476 6477 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6478 return Reader.readNestedNameSpecifierLoc(); 6479 } 6480 6481 Attr *ReadAttr() { 6482 return Reader.readAttr(); 6483 } 6484 6485 public: 6486 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6487 6488 // We want compile-time assurance that we've enumerated all of 6489 // these, so unfortunately we have to declare them first, then 6490 // define them out-of-line. 6491 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6492 #define TYPELOC(CLASS, PARENT) \ 6493 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6494 #include "clang/AST/TypeLocNodes.def" 6495 6496 void VisitFunctionTypeLoc(FunctionTypeLoc); 6497 void VisitArrayTypeLoc(ArrayTypeLoc); 6498 }; 6499 6500 } // namespace clang 6501 6502 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6503 // nothing to do 6504 } 6505 6506 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6507 TL.setBuiltinLoc(readSourceLocation()); 6508 if (TL.needsExtraLocalData()) { 6509 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6510 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6511 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6512 TL.setModeAttr(Reader.readInt()); 6513 } 6514 } 6515 6516 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6517 TL.setNameLoc(readSourceLocation()); 6518 } 6519 6520 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6521 TL.setStarLoc(readSourceLocation()); 6522 } 6523 6524 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6525 // nothing to do 6526 } 6527 6528 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6529 // nothing to do 6530 } 6531 6532 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6533 TL.setExpansionLoc(readSourceLocation()); 6534 } 6535 6536 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6537 TL.setCaretLoc(readSourceLocation()); 6538 } 6539 6540 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6541 TL.setAmpLoc(readSourceLocation()); 6542 } 6543 6544 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6545 TL.setAmpAmpLoc(readSourceLocation()); 6546 } 6547 6548 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6549 TL.setStarLoc(readSourceLocation()); 6550 TL.setClassTInfo(GetTypeSourceInfo()); 6551 } 6552 6553 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6554 TL.setLBracketLoc(readSourceLocation()); 6555 TL.setRBracketLoc(readSourceLocation()); 6556 if (Reader.readBool()) 6557 TL.setSizeExpr(Reader.readExpr()); 6558 else 6559 TL.setSizeExpr(nullptr); 6560 } 6561 6562 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6563 VisitArrayTypeLoc(TL); 6564 } 6565 6566 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6567 VisitArrayTypeLoc(TL); 6568 } 6569 6570 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6571 VisitArrayTypeLoc(TL); 6572 } 6573 6574 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6575 DependentSizedArrayTypeLoc TL) { 6576 VisitArrayTypeLoc(TL); 6577 } 6578 6579 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6580 DependentAddressSpaceTypeLoc TL) { 6581 6582 TL.setAttrNameLoc(readSourceLocation()); 6583 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6584 TL.setAttrExprOperand(Reader.readExpr()); 6585 } 6586 6587 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6588 DependentSizedExtVectorTypeLoc TL) { 6589 TL.setNameLoc(readSourceLocation()); 6590 } 6591 6592 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6593 TL.setNameLoc(readSourceLocation()); 6594 } 6595 6596 void TypeLocReader::VisitDependentVectorTypeLoc( 6597 DependentVectorTypeLoc TL) { 6598 TL.setNameLoc(readSourceLocation()); 6599 } 6600 6601 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6602 TL.setNameLoc(readSourceLocation()); 6603 } 6604 6605 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6606 TL.setAttrNameLoc(readSourceLocation()); 6607 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6608 TL.setAttrRowOperand(Reader.readExpr()); 6609 TL.setAttrColumnOperand(Reader.readExpr()); 6610 } 6611 6612 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6613 DependentSizedMatrixTypeLoc TL) { 6614 TL.setAttrNameLoc(readSourceLocation()); 6615 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6616 TL.setAttrRowOperand(Reader.readExpr()); 6617 TL.setAttrColumnOperand(Reader.readExpr()); 6618 } 6619 6620 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6621 TL.setLocalRangeBegin(readSourceLocation()); 6622 TL.setLParenLoc(readSourceLocation()); 6623 TL.setRParenLoc(readSourceLocation()); 6624 TL.setExceptionSpecRange(Reader.readSourceRange()); 6625 TL.setLocalRangeEnd(readSourceLocation()); 6626 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6627 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6628 } 6629 } 6630 6631 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6632 VisitFunctionTypeLoc(TL); 6633 } 6634 6635 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6636 VisitFunctionTypeLoc(TL); 6637 } 6638 6639 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6640 TL.setNameLoc(readSourceLocation()); 6641 } 6642 6643 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6644 TL.setNameLoc(readSourceLocation()); 6645 } 6646 6647 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6648 TL.setTypeofLoc(readSourceLocation()); 6649 TL.setLParenLoc(readSourceLocation()); 6650 TL.setRParenLoc(readSourceLocation()); 6651 } 6652 6653 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6654 TL.setTypeofLoc(readSourceLocation()); 6655 TL.setLParenLoc(readSourceLocation()); 6656 TL.setRParenLoc(readSourceLocation()); 6657 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6658 } 6659 6660 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6661 TL.setNameLoc(readSourceLocation()); 6662 } 6663 6664 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6665 TL.setKWLoc(readSourceLocation()); 6666 TL.setLParenLoc(readSourceLocation()); 6667 TL.setRParenLoc(readSourceLocation()); 6668 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6669 } 6670 6671 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6672 TL.setNameLoc(readSourceLocation()); 6673 if (Reader.readBool()) { 6674 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6675 TL.setTemplateKWLoc(readSourceLocation()); 6676 TL.setConceptNameLoc(readSourceLocation()); 6677 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6678 TL.setLAngleLoc(readSourceLocation()); 6679 TL.setRAngleLoc(readSourceLocation()); 6680 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6681 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6682 TL.getTypePtr()->getArg(i).getKind())); 6683 } 6684 } 6685 6686 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6687 DeducedTemplateSpecializationTypeLoc TL) { 6688 TL.setTemplateNameLoc(readSourceLocation()); 6689 } 6690 6691 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6692 TL.setNameLoc(readSourceLocation()); 6693 } 6694 6695 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6696 TL.setNameLoc(readSourceLocation()); 6697 } 6698 6699 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6700 TL.setAttr(ReadAttr()); 6701 } 6702 6703 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6704 TL.setNameLoc(readSourceLocation()); 6705 } 6706 6707 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6708 SubstTemplateTypeParmTypeLoc TL) { 6709 TL.setNameLoc(readSourceLocation()); 6710 } 6711 6712 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6713 SubstTemplateTypeParmPackTypeLoc TL) { 6714 TL.setNameLoc(readSourceLocation()); 6715 } 6716 6717 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6718 TemplateSpecializationTypeLoc TL) { 6719 TL.setTemplateKeywordLoc(readSourceLocation()); 6720 TL.setTemplateNameLoc(readSourceLocation()); 6721 TL.setLAngleLoc(readSourceLocation()); 6722 TL.setRAngleLoc(readSourceLocation()); 6723 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6724 TL.setArgLocInfo( 6725 i, 6726 Reader.readTemplateArgumentLocInfo( 6727 TL.getTypePtr()->getArg(i).getKind())); 6728 } 6729 6730 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6731 TL.setLParenLoc(readSourceLocation()); 6732 TL.setRParenLoc(readSourceLocation()); 6733 } 6734 6735 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6736 TL.setElaboratedKeywordLoc(readSourceLocation()); 6737 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6738 } 6739 6740 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6741 TL.setNameLoc(readSourceLocation()); 6742 } 6743 6744 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6745 TL.setElaboratedKeywordLoc(readSourceLocation()); 6746 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6747 TL.setNameLoc(readSourceLocation()); 6748 } 6749 6750 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6751 DependentTemplateSpecializationTypeLoc TL) { 6752 TL.setElaboratedKeywordLoc(readSourceLocation()); 6753 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6754 TL.setTemplateKeywordLoc(readSourceLocation()); 6755 TL.setTemplateNameLoc(readSourceLocation()); 6756 TL.setLAngleLoc(readSourceLocation()); 6757 TL.setRAngleLoc(readSourceLocation()); 6758 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6759 TL.setArgLocInfo( 6760 I, 6761 Reader.readTemplateArgumentLocInfo( 6762 TL.getTypePtr()->getArg(I).getKind())); 6763 } 6764 6765 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6766 TL.setEllipsisLoc(readSourceLocation()); 6767 } 6768 6769 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6770 TL.setNameLoc(readSourceLocation()); 6771 } 6772 6773 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6774 if (TL.getNumProtocols()) { 6775 TL.setProtocolLAngleLoc(readSourceLocation()); 6776 TL.setProtocolRAngleLoc(readSourceLocation()); 6777 } 6778 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6779 TL.setProtocolLoc(i, readSourceLocation()); 6780 } 6781 6782 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6783 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6784 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6785 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6786 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6787 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6788 TL.setProtocolLAngleLoc(readSourceLocation()); 6789 TL.setProtocolRAngleLoc(readSourceLocation()); 6790 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6791 TL.setProtocolLoc(i, readSourceLocation()); 6792 } 6793 6794 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6795 TL.setStarLoc(readSourceLocation()); 6796 } 6797 6798 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6799 TL.setKWLoc(readSourceLocation()); 6800 TL.setLParenLoc(readSourceLocation()); 6801 TL.setRParenLoc(readSourceLocation()); 6802 } 6803 6804 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6805 TL.setKWLoc(readSourceLocation()); 6806 } 6807 6808 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) { 6809 TL.setNameLoc(readSourceLocation()); 6810 } 6811 void TypeLocReader::VisitDependentExtIntTypeLoc( 6812 clang::DependentExtIntTypeLoc TL) { 6813 TL.setNameLoc(readSourceLocation()); 6814 } 6815 6816 6817 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6818 TypeLocReader TLR(*this); 6819 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6820 TLR.Visit(TL); 6821 } 6822 6823 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6824 QualType InfoTy = readType(); 6825 if (InfoTy.isNull()) 6826 return nullptr; 6827 6828 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6829 readTypeLoc(TInfo->getTypeLoc()); 6830 return TInfo; 6831 } 6832 6833 QualType ASTReader::GetType(TypeID ID) { 6834 assert(ContextObj && "reading type with no AST context"); 6835 ASTContext &Context = *ContextObj; 6836 6837 unsigned FastQuals = ID & Qualifiers::FastMask; 6838 unsigned Index = ID >> Qualifiers::FastWidth; 6839 6840 if (Index < NUM_PREDEF_TYPE_IDS) { 6841 QualType T; 6842 switch ((PredefinedTypeIDs)Index) { 6843 case PREDEF_TYPE_NULL_ID: 6844 return QualType(); 6845 case PREDEF_TYPE_VOID_ID: 6846 T = Context.VoidTy; 6847 break; 6848 case PREDEF_TYPE_BOOL_ID: 6849 T = Context.BoolTy; 6850 break; 6851 case PREDEF_TYPE_CHAR_U_ID: 6852 case PREDEF_TYPE_CHAR_S_ID: 6853 // FIXME: Check that the signedness of CharTy is correct! 6854 T = Context.CharTy; 6855 break; 6856 case PREDEF_TYPE_UCHAR_ID: 6857 T = Context.UnsignedCharTy; 6858 break; 6859 case PREDEF_TYPE_USHORT_ID: 6860 T = Context.UnsignedShortTy; 6861 break; 6862 case PREDEF_TYPE_UINT_ID: 6863 T = Context.UnsignedIntTy; 6864 break; 6865 case PREDEF_TYPE_ULONG_ID: 6866 T = Context.UnsignedLongTy; 6867 break; 6868 case PREDEF_TYPE_ULONGLONG_ID: 6869 T = Context.UnsignedLongLongTy; 6870 break; 6871 case PREDEF_TYPE_UINT128_ID: 6872 T = Context.UnsignedInt128Ty; 6873 break; 6874 case PREDEF_TYPE_SCHAR_ID: 6875 T = Context.SignedCharTy; 6876 break; 6877 case PREDEF_TYPE_WCHAR_ID: 6878 T = Context.WCharTy; 6879 break; 6880 case PREDEF_TYPE_SHORT_ID: 6881 T = Context.ShortTy; 6882 break; 6883 case PREDEF_TYPE_INT_ID: 6884 T = Context.IntTy; 6885 break; 6886 case PREDEF_TYPE_LONG_ID: 6887 T = Context.LongTy; 6888 break; 6889 case PREDEF_TYPE_LONGLONG_ID: 6890 T = Context.LongLongTy; 6891 break; 6892 case PREDEF_TYPE_INT128_ID: 6893 T = Context.Int128Ty; 6894 break; 6895 case PREDEF_TYPE_BFLOAT16_ID: 6896 T = Context.BFloat16Ty; 6897 break; 6898 case PREDEF_TYPE_HALF_ID: 6899 T = Context.HalfTy; 6900 break; 6901 case PREDEF_TYPE_FLOAT_ID: 6902 T = Context.FloatTy; 6903 break; 6904 case PREDEF_TYPE_DOUBLE_ID: 6905 T = Context.DoubleTy; 6906 break; 6907 case PREDEF_TYPE_LONGDOUBLE_ID: 6908 T = Context.LongDoubleTy; 6909 break; 6910 case PREDEF_TYPE_SHORT_ACCUM_ID: 6911 T = Context.ShortAccumTy; 6912 break; 6913 case PREDEF_TYPE_ACCUM_ID: 6914 T = Context.AccumTy; 6915 break; 6916 case PREDEF_TYPE_LONG_ACCUM_ID: 6917 T = Context.LongAccumTy; 6918 break; 6919 case PREDEF_TYPE_USHORT_ACCUM_ID: 6920 T = Context.UnsignedShortAccumTy; 6921 break; 6922 case PREDEF_TYPE_UACCUM_ID: 6923 T = Context.UnsignedAccumTy; 6924 break; 6925 case PREDEF_TYPE_ULONG_ACCUM_ID: 6926 T = Context.UnsignedLongAccumTy; 6927 break; 6928 case PREDEF_TYPE_SHORT_FRACT_ID: 6929 T = Context.ShortFractTy; 6930 break; 6931 case PREDEF_TYPE_FRACT_ID: 6932 T = Context.FractTy; 6933 break; 6934 case PREDEF_TYPE_LONG_FRACT_ID: 6935 T = Context.LongFractTy; 6936 break; 6937 case PREDEF_TYPE_USHORT_FRACT_ID: 6938 T = Context.UnsignedShortFractTy; 6939 break; 6940 case PREDEF_TYPE_UFRACT_ID: 6941 T = Context.UnsignedFractTy; 6942 break; 6943 case PREDEF_TYPE_ULONG_FRACT_ID: 6944 T = Context.UnsignedLongFractTy; 6945 break; 6946 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6947 T = Context.SatShortAccumTy; 6948 break; 6949 case PREDEF_TYPE_SAT_ACCUM_ID: 6950 T = Context.SatAccumTy; 6951 break; 6952 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6953 T = Context.SatLongAccumTy; 6954 break; 6955 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6956 T = Context.SatUnsignedShortAccumTy; 6957 break; 6958 case PREDEF_TYPE_SAT_UACCUM_ID: 6959 T = Context.SatUnsignedAccumTy; 6960 break; 6961 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6962 T = Context.SatUnsignedLongAccumTy; 6963 break; 6964 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6965 T = Context.SatShortFractTy; 6966 break; 6967 case PREDEF_TYPE_SAT_FRACT_ID: 6968 T = Context.SatFractTy; 6969 break; 6970 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6971 T = Context.SatLongFractTy; 6972 break; 6973 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6974 T = Context.SatUnsignedShortFractTy; 6975 break; 6976 case PREDEF_TYPE_SAT_UFRACT_ID: 6977 T = Context.SatUnsignedFractTy; 6978 break; 6979 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6980 T = Context.SatUnsignedLongFractTy; 6981 break; 6982 case PREDEF_TYPE_FLOAT16_ID: 6983 T = Context.Float16Ty; 6984 break; 6985 case PREDEF_TYPE_FLOAT128_ID: 6986 T = Context.Float128Ty; 6987 break; 6988 case PREDEF_TYPE_OVERLOAD_ID: 6989 T = Context.OverloadTy; 6990 break; 6991 case PREDEF_TYPE_BOUND_MEMBER: 6992 T = Context.BoundMemberTy; 6993 break; 6994 case PREDEF_TYPE_PSEUDO_OBJECT: 6995 T = Context.PseudoObjectTy; 6996 break; 6997 case PREDEF_TYPE_DEPENDENT_ID: 6998 T = Context.DependentTy; 6999 break; 7000 case PREDEF_TYPE_UNKNOWN_ANY: 7001 T = Context.UnknownAnyTy; 7002 break; 7003 case PREDEF_TYPE_NULLPTR_ID: 7004 T = Context.NullPtrTy; 7005 break; 7006 case PREDEF_TYPE_CHAR8_ID: 7007 T = Context.Char8Ty; 7008 break; 7009 case PREDEF_TYPE_CHAR16_ID: 7010 T = Context.Char16Ty; 7011 break; 7012 case PREDEF_TYPE_CHAR32_ID: 7013 T = Context.Char32Ty; 7014 break; 7015 case PREDEF_TYPE_OBJC_ID: 7016 T = Context.ObjCBuiltinIdTy; 7017 break; 7018 case PREDEF_TYPE_OBJC_CLASS: 7019 T = Context.ObjCBuiltinClassTy; 7020 break; 7021 case PREDEF_TYPE_OBJC_SEL: 7022 T = Context.ObjCBuiltinSelTy; 7023 break; 7024 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 7025 case PREDEF_TYPE_##Id##_ID: \ 7026 T = Context.SingletonId; \ 7027 break; 7028 #include "clang/Basic/OpenCLImageTypes.def" 7029 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 7030 case PREDEF_TYPE_##Id##_ID: \ 7031 T = Context.Id##Ty; \ 7032 break; 7033 #include "clang/Basic/OpenCLExtensionTypes.def" 7034 case PREDEF_TYPE_SAMPLER_ID: 7035 T = Context.OCLSamplerTy; 7036 break; 7037 case PREDEF_TYPE_EVENT_ID: 7038 T = Context.OCLEventTy; 7039 break; 7040 case PREDEF_TYPE_CLK_EVENT_ID: 7041 T = Context.OCLClkEventTy; 7042 break; 7043 case PREDEF_TYPE_QUEUE_ID: 7044 T = Context.OCLQueueTy; 7045 break; 7046 case PREDEF_TYPE_RESERVE_ID_ID: 7047 T = Context.OCLReserveIDTy; 7048 break; 7049 case PREDEF_TYPE_AUTO_DEDUCT: 7050 T = Context.getAutoDeductType(); 7051 break; 7052 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7053 T = Context.getAutoRRefDeductType(); 7054 break; 7055 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7056 T = Context.ARCUnbridgedCastTy; 7057 break; 7058 case PREDEF_TYPE_BUILTIN_FN: 7059 T = Context.BuiltinFnTy; 7060 break; 7061 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7062 T = Context.IncompleteMatrixIdxTy; 7063 break; 7064 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7065 T = Context.OMPArraySectionTy; 7066 break; 7067 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7068 T = Context.OMPArraySectionTy; 7069 break; 7070 case PREDEF_TYPE_OMP_ITERATOR: 7071 T = Context.OMPIteratorTy; 7072 break; 7073 #define SVE_TYPE(Name, Id, SingletonId) \ 7074 case PREDEF_TYPE_##Id##_ID: \ 7075 T = Context.SingletonId; \ 7076 break; 7077 #include "clang/Basic/AArch64SVEACLETypes.def" 7078 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 7079 case PREDEF_TYPE_##Id##_ID: \ 7080 T = Context.Id##Ty; \ 7081 break; 7082 #include "clang/Basic/PPCTypes.def" 7083 #define RVV_TYPE(Name, Id, SingletonId) \ 7084 case PREDEF_TYPE_##Id##_ID: \ 7085 T = Context.SingletonId; \ 7086 break; 7087 #include "clang/Basic/RISCVVTypes.def" 7088 } 7089 7090 assert(!T.isNull() && "Unknown predefined type"); 7091 return T.withFastQualifiers(FastQuals); 7092 } 7093 7094 Index -= NUM_PREDEF_TYPE_IDS; 7095 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7096 if (TypesLoaded[Index].isNull()) { 7097 TypesLoaded[Index] = readTypeRecord(Index); 7098 if (TypesLoaded[Index].isNull()) 7099 return QualType(); 7100 7101 TypesLoaded[Index]->setFromAST(); 7102 if (DeserializationListener) 7103 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7104 TypesLoaded[Index]); 7105 } 7106 7107 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7108 } 7109 7110 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7111 return GetType(getGlobalTypeID(F, LocalID)); 7112 } 7113 7114 serialization::TypeID 7115 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7116 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7117 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7118 7119 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7120 return LocalID; 7121 7122 if (!F.ModuleOffsetMap.empty()) 7123 ReadModuleOffsetMap(F); 7124 7125 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7126 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7127 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7128 7129 unsigned GlobalIndex = LocalIndex + I->second; 7130 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7131 } 7132 7133 TemplateArgumentLocInfo 7134 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7135 switch (Kind) { 7136 case TemplateArgument::Expression: 7137 return readExpr(); 7138 case TemplateArgument::Type: 7139 return readTypeSourceInfo(); 7140 case TemplateArgument::Template: { 7141 NestedNameSpecifierLoc QualifierLoc = 7142 readNestedNameSpecifierLoc(); 7143 SourceLocation TemplateNameLoc = readSourceLocation(); 7144 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7145 TemplateNameLoc, SourceLocation()); 7146 } 7147 case TemplateArgument::TemplateExpansion: { 7148 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7149 SourceLocation TemplateNameLoc = readSourceLocation(); 7150 SourceLocation EllipsisLoc = readSourceLocation(); 7151 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7152 TemplateNameLoc, EllipsisLoc); 7153 } 7154 case TemplateArgument::Null: 7155 case TemplateArgument::Integral: 7156 case TemplateArgument::Declaration: 7157 case TemplateArgument::NullPtr: 7158 case TemplateArgument::Pack: 7159 // FIXME: Is this right? 7160 return TemplateArgumentLocInfo(); 7161 } 7162 llvm_unreachable("unexpected template argument loc"); 7163 } 7164 7165 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7166 TemplateArgument Arg = readTemplateArgument(); 7167 7168 if (Arg.getKind() == TemplateArgument::Expression) { 7169 if (readBool()) // bool InfoHasSameExpr. 7170 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7171 } 7172 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7173 } 7174 7175 const ASTTemplateArgumentListInfo * 7176 ASTRecordReader::readASTTemplateArgumentListInfo() { 7177 SourceLocation LAngleLoc = readSourceLocation(); 7178 SourceLocation RAngleLoc = readSourceLocation(); 7179 unsigned NumArgsAsWritten = readInt(); 7180 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7181 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7182 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7183 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7184 } 7185 7186 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7187 return GetDecl(ID); 7188 } 7189 7190 void ASTReader::CompleteRedeclChain(const Decl *D) { 7191 if (NumCurrentElementsDeserializing) { 7192 // We arrange to not care about the complete redeclaration chain while we're 7193 // deserializing. Just remember that the AST has marked this one as complete 7194 // but that it's not actually complete yet, so we know we still need to 7195 // complete it later. 7196 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7197 return; 7198 } 7199 7200 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7201 7202 // If this is a named declaration, complete it by looking it up 7203 // within its context. 7204 // 7205 // FIXME: Merging a function definition should merge 7206 // all mergeable entities within it. 7207 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7208 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7209 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7210 if (!getContext().getLangOpts().CPlusPlus && 7211 isa<TranslationUnitDecl>(DC)) { 7212 // Outside of C++, we don't have a lookup table for the TU, so update 7213 // the identifier instead. (For C++ modules, we don't store decls 7214 // in the serialized identifier table, so we do the lookup in the TU.) 7215 auto *II = Name.getAsIdentifierInfo(); 7216 assert(II && "non-identifier name in C?"); 7217 if (II->isOutOfDate()) 7218 updateOutOfDateIdentifier(*II); 7219 } else 7220 DC->lookup(Name); 7221 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7222 // Find all declarations of this kind from the relevant context. 7223 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7224 auto *DC = cast<DeclContext>(DCDecl); 7225 SmallVector<Decl*, 8> Decls; 7226 FindExternalLexicalDecls( 7227 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7228 } 7229 } 7230 } 7231 7232 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7233 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7234 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7235 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7236 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7237 if (auto *Template = FD->getPrimaryTemplate()) 7238 Template->LoadLazySpecializations(); 7239 } 7240 } 7241 7242 CXXCtorInitializer ** 7243 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7244 RecordLocation Loc = getLocalBitOffset(Offset); 7245 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7246 SavedStreamPosition SavedPosition(Cursor); 7247 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7248 Error(std::move(Err)); 7249 return nullptr; 7250 } 7251 ReadingKindTracker ReadingKind(Read_Decl, *this); 7252 7253 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7254 if (!MaybeCode) { 7255 Error(MaybeCode.takeError()); 7256 return nullptr; 7257 } 7258 unsigned Code = MaybeCode.get(); 7259 7260 ASTRecordReader Record(*this, *Loc.F); 7261 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7262 if (!MaybeRecCode) { 7263 Error(MaybeRecCode.takeError()); 7264 return nullptr; 7265 } 7266 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7267 Error("malformed AST file: missing C++ ctor initializers"); 7268 return nullptr; 7269 } 7270 7271 return Record.readCXXCtorInitializers(); 7272 } 7273 7274 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7275 assert(ContextObj && "reading base specifiers with no AST context"); 7276 ASTContext &Context = *ContextObj; 7277 7278 RecordLocation Loc = getLocalBitOffset(Offset); 7279 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7280 SavedStreamPosition SavedPosition(Cursor); 7281 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7282 Error(std::move(Err)); 7283 return nullptr; 7284 } 7285 ReadingKindTracker ReadingKind(Read_Decl, *this); 7286 7287 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7288 if (!MaybeCode) { 7289 Error(MaybeCode.takeError()); 7290 return nullptr; 7291 } 7292 unsigned Code = MaybeCode.get(); 7293 7294 ASTRecordReader Record(*this, *Loc.F); 7295 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7296 if (!MaybeRecCode) { 7297 Error(MaybeCode.takeError()); 7298 return nullptr; 7299 } 7300 unsigned RecCode = MaybeRecCode.get(); 7301 7302 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7303 Error("malformed AST file: missing C++ base specifiers"); 7304 return nullptr; 7305 } 7306 7307 unsigned NumBases = Record.readInt(); 7308 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7309 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7310 for (unsigned I = 0; I != NumBases; ++I) 7311 Bases[I] = Record.readCXXBaseSpecifier(); 7312 return Bases; 7313 } 7314 7315 serialization::DeclID 7316 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7317 if (LocalID < NUM_PREDEF_DECL_IDS) 7318 return LocalID; 7319 7320 if (!F.ModuleOffsetMap.empty()) 7321 ReadModuleOffsetMap(F); 7322 7323 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7324 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7325 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7326 7327 return LocalID + I->second; 7328 } 7329 7330 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7331 ModuleFile &M) const { 7332 // Predefined decls aren't from any module. 7333 if (ID < NUM_PREDEF_DECL_IDS) 7334 return false; 7335 7336 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7337 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7338 } 7339 7340 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7341 if (!D->isFromASTFile()) 7342 return nullptr; 7343 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7344 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7345 return I->second; 7346 } 7347 7348 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7349 if (ID < NUM_PREDEF_DECL_IDS) 7350 return SourceLocation(); 7351 7352 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7353 7354 if (Index > DeclsLoaded.size()) { 7355 Error("declaration ID out-of-range for AST file"); 7356 return SourceLocation(); 7357 } 7358 7359 if (Decl *D = DeclsLoaded[Index]) 7360 return D->getLocation(); 7361 7362 SourceLocation Loc; 7363 DeclCursorForID(ID, Loc); 7364 return Loc; 7365 } 7366 7367 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7368 switch (ID) { 7369 case PREDEF_DECL_NULL_ID: 7370 return nullptr; 7371 7372 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7373 return Context.getTranslationUnitDecl(); 7374 7375 case PREDEF_DECL_OBJC_ID_ID: 7376 return Context.getObjCIdDecl(); 7377 7378 case PREDEF_DECL_OBJC_SEL_ID: 7379 return Context.getObjCSelDecl(); 7380 7381 case PREDEF_DECL_OBJC_CLASS_ID: 7382 return Context.getObjCClassDecl(); 7383 7384 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7385 return Context.getObjCProtocolDecl(); 7386 7387 case PREDEF_DECL_INT_128_ID: 7388 return Context.getInt128Decl(); 7389 7390 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7391 return Context.getUInt128Decl(); 7392 7393 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7394 return Context.getObjCInstanceTypeDecl(); 7395 7396 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7397 return Context.getBuiltinVaListDecl(); 7398 7399 case PREDEF_DECL_VA_LIST_TAG: 7400 return Context.getVaListTagDecl(); 7401 7402 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7403 return Context.getBuiltinMSVaListDecl(); 7404 7405 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7406 return Context.getMSGuidTagDecl(); 7407 7408 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7409 return Context.getExternCContextDecl(); 7410 7411 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7412 return Context.getMakeIntegerSeqDecl(); 7413 7414 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7415 return Context.getCFConstantStringDecl(); 7416 7417 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7418 return Context.getCFConstantStringTagDecl(); 7419 7420 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7421 return Context.getTypePackElementDecl(); 7422 } 7423 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7424 } 7425 7426 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7427 assert(ContextObj && "reading decl with no AST context"); 7428 if (ID < NUM_PREDEF_DECL_IDS) { 7429 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7430 if (D) { 7431 // Track that we have merged the declaration with ID \p ID into the 7432 // pre-existing predefined declaration \p D. 7433 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7434 if (Merged.empty()) 7435 Merged.push_back(ID); 7436 } 7437 return D; 7438 } 7439 7440 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7441 7442 if (Index >= DeclsLoaded.size()) { 7443 assert(0 && "declaration ID out-of-range for AST file"); 7444 Error("declaration ID out-of-range for AST file"); 7445 return nullptr; 7446 } 7447 7448 return DeclsLoaded[Index]; 7449 } 7450 7451 Decl *ASTReader::GetDecl(DeclID ID) { 7452 if (ID < NUM_PREDEF_DECL_IDS) 7453 return GetExistingDecl(ID); 7454 7455 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7456 7457 if (Index >= DeclsLoaded.size()) { 7458 assert(0 && "declaration ID out-of-range for AST file"); 7459 Error("declaration ID out-of-range for AST file"); 7460 return nullptr; 7461 } 7462 7463 if (!DeclsLoaded[Index]) { 7464 ReadDeclRecord(ID); 7465 if (DeserializationListener) 7466 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7467 } 7468 7469 return DeclsLoaded[Index]; 7470 } 7471 7472 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7473 DeclID GlobalID) { 7474 if (GlobalID < NUM_PREDEF_DECL_IDS) 7475 return GlobalID; 7476 7477 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7478 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7479 ModuleFile *Owner = I->second; 7480 7481 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7482 = M.GlobalToLocalDeclIDs.find(Owner); 7483 if (Pos == M.GlobalToLocalDeclIDs.end()) 7484 return 0; 7485 7486 return GlobalID - Owner->BaseDeclID + Pos->second; 7487 } 7488 7489 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7490 const RecordData &Record, 7491 unsigned &Idx) { 7492 if (Idx >= Record.size()) { 7493 Error("Corrupted AST file"); 7494 return 0; 7495 } 7496 7497 return getGlobalDeclID(F, Record[Idx++]); 7498 } 7499 7500 /// Resolve the offset of a statement into a statement. 7501 /// 7502 /// This operation will read a new statement from the external 7503 /// source each time it is called, and is meant to be used via a 7504 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7505 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7506 // Switch case IDs are per Decl. 7507 ClearSwitchCaseIDs(); 7508 7509 // Offset here is a global offset across the entire chain. 7510 RecordLocation Loc = getLocalBitOffset(Offset); 7511 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7512 Error(std::move(Err)); 7513 return nullptr; 7514 } 7515 assert(NumCurrentElementsDeserializing == 0 && 7516 "should not be called while already deserializing"); 7517 Deserializing D(this); 7518 return ReadStmtFromStream(*Loc.F); 7519 } 7520 7521 void ASTReader::FindExternalLexicalDecls( 7522 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7523 SmallVectorImpl<Decl *> &Decls) { 7524 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7525 7526 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7527 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7528 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7529 auto K = (Decl::Kind)+LexicalDecls[I]; 7530 if (!IsKindWeWant(K)) 7531 continue; 7532 7533 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7534 7535 // Don't add predefined declarations to the lexical context more 7536 // than once. 7537 if (ID < NUM_PREDEF_DECL_IDS) { 7538 if (PredefsVisited[ID]) 7539 continue; 7540 7541 PredefsVisited[ID] = true; 7542 } 7543 7544 if (Decl *D = GetLocalDecl(*M, ID)) { 7545 assert(D->getKind() == K && "wrong kind for lexical decl"); 7546 if (!DC->isDeclInLexicalTraversal(D)) 7547 Decls.push_back(D); 7548 } 7549 } 7550 }; 7551 7552 if (isa<TranslationUnitDecl>(DC)) { 7553 for (auto Lexical : TULexicalDecls) 7554 Visit(Lexical.first, Lexical.second); 7555 } else { 7556 auto I = LexicalDecls.find(DC); 7557 if (I != LexicalDecls.end()) 7558 Visit(I->second.first, I->second.second); 7559 } 7560 7561 ++NumLexicalDeclContextsRead; 7562 } 7563 7564 namespace { 7565 7566 class DeclIDComp { 7567 ASTReader &Reader; 7568 ModuleFile &Mod; 7569 7570 public: 7571 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7572 7573 bool operator()(LocalDeclID L, LocalDeclID R) const { 7574 SourceLocation LHS = getLocation(L); 7575 SourceLocation RHS = getLocation(R); 7576 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7577 } 7578 7579 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7580 SourceLocation RHS = getLocation(R); 7581 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7582 } 7583 7584 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7585 SourceLocation LHS = getLocation(L); 7586 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7587 } 7588 7589 SourceLocation getLocation(LocalDeclID ID) const { 7590 return Reader.getSourceManager().getFileLoc( 7591 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7592 } 7593 }; 7594 7595 } // namespace 7596 7597 void ASTReader::FindFileRegionDecls(FileID File, 7598 unsigned Offset, unsigned Length, 7599 SmallVectorImpl<Decl *> &Decls) { 7600 SourceManager &SM = getSourceManager(); 7601 7602 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7603 if (I == FileDeclIDs.end()) 7604 return; 7605 7606 FileDeclsInfo &DInfo = I->second; 7607 if (DInfo.Decls.empty()) 7608 return; 7609 7610 SourceLocation 7611 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7612 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7613 7614 DeclIDComp DIDComp(*this, *DInfo.Mod); 7615 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7616 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7617 if (BeginIt != DInfo.Decls.begin()) 7618 --BeginIt; 7619 7620 // If we are pointing at a top-level decl inside an objc container, we need 7621 // to backtrack until we find it otherwise we will fail to report that the 7622 // region overlaps with an objc container. 7623 while (BeginIt != DInfo.Decls.begin() && 7624 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7625 ->isTopLevelDeclInObjCContainer()) 7626 --BeginIt; 7627 7628 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7629 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7630 if (EndIt != DInfo.Decls.end()) 7631 ++EndIt; 7632 7633 for (ArrayRef<serialization::LocalDeclID>::iterator 7634 DIt = BeginIt; DIt != EndIt; ++DIt) 7635 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7636 } 7637 7638 bool 7639 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7640 DeclarationName Name) { 7641 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7642 "DeclContext has no visible decls in storage"); 7643 if (!Name) 7644 return false; 7645 7646 auto It = Lookups.find(DC); 7647 if (It == Lookups.end()) 7648 return false; 7649 7650 Deserializing LookupResults(this); 7651 7652 // Load the list of declarations. 7653 SmallVector<NamedDecl *, 64> Decls; 7654 llvm::SmallPtrSet<NamedDecl *, 8> Found; 7655 for (DeclID ID : It->second.Table.find(Name)) { 7656 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7657 if (ND->getDeclName() == Name && Found.insert(ND).second) 7658 Decls.push_back(ND); 7659 } 7660 7661 ++NumVisibleDeclContextsRead; 7662 SetExternalVisibleDeclsForName(DC, Name, Decls); 7663 return !Decls.empty(); 7664 } 7665 7666 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7667 if (!DC->hasExternalVisibleStorage()) 7668 return; 7669 7670 auto It = Lookups.find(DC); 7671 assert(It != Lookups.end() && 7672 "have external visible storage but no lookup tables"); 7673 7674 DeclsMap Decls; 7675 7676 for (DeclID ID : It->second.Table.findAll()) { 7677 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7678 Decls[ND->getDeclName()].push_back(ND); 7679 } 7680 7681 ++NumVisibleDeclContextsRead; 7682 7683 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7684 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7685 } 7686 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7687 } 7688 7689 const serialization::reader::DeclContextLookupTable * 7690 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7691 auto I = Lookups.find(Primary); 7692 return I == Lookups.end() ? nullptr : &I->second; 7693 } 7694 7695 /// Under non-PCH compilation the consumer receives the objc methods 7696 /// before receiving the implementation, and codegen depends on this. 7697 /// We simulate this by deserializing and passing to consumer the methods of the 7698 /// implementation before passing the deserialized implementation decl. 7699 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7700 ASTConsumer *Consumer) { 7701 assert(ImplD && Consumer); 7702 7703 for (auto *I : ImplD->methods()) 7704 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7705 7706 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7707 } 7708 7709 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7710 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7711 PassObjCImplDeclToConsumer(ImplD, Consumer); 7712 else 7713 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7714 } 7715 7716 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7717 this->Consumer = Consumer; 7718 7719 if (Consumer) 7720 PassInterestingDeclsToConsumer(); 7721 7722 if (DeserializationListener) 7723 DeserializationListener->ReaderInitialized(this); 7724 } 7725 7726 void ASTReader::PrintStats() { 7727 std::fprintf(stderr, "*** AST File Statistics:\n"); 7728 7729 unsigned NumTypesLoaded 7730 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7731 QualType()); 7732 unsigned NumDeclsLoaded 7733 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7734 (Decl *)nullptr); 7735 unsigned NumIdentifiersLoaded 7736 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7737 IdentifiersLoaded.end(), 7738 (IdentifierInfo *)nullptr); 7739 unsigned NumMacrosLoaded 7740 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7741 MacrosLoaded.end(), 7742 (MacroInfo *)nullptr); 7743 unsigned NumSelectorsLoaded 7744 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7745 SelectorsLoaded.end(), 7746 Selector()); 7747 7748 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7749 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7750 NumSLocEntriesRead, TotalNumSLocEntries, 7751 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7752 if (!TypesLoaded.empty()) 7753 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7754 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7755 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7756 if (!DeclsLoaded.empty()) 7757 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7758 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7759 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7760 if (!IdentifiersLoaded.empty()) 7761 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7762 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7763 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7764 if (!MacrosLoaded.empty()) 7765 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7766 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7767 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7768 if (!SelectorsLoaded.empty()) 7769 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7770 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7771 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7772 if (TotalNumStatements) 7773 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7774 NumStatementsRead, TotalNumStatements, 7775 ((float)NumStatementsRead/TotalNumStatements * 100)); 7776 if (TotalNumMacros) 7777 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7778 NumMacrosRead, TotalNumMacros, 7779 ((float)NumMacrosRead/TotalNumMacros * 100)); 7780 if (TotalLexicalDeclContexts) 7781 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7782 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7783 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7784 * 100)); 7785 if (TotalVisibleDeclContexts) 7786 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7787 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7788 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7789 * 100)); 7790 if (TotalNumMethodPoolEntries) 7791 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7792 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7793 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7794 * 100)); 7795 if (NumMethodPoolLookups) 7796 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7797 NumMethodPoolHits, NumMethodPoolLookups, 7798 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7799 if (NumMethodPoolTableLookups) 7800 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7801 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7802 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7803 * 100.0)); 7804 if (NumIdentifierLookupHits) 7805 std::fprintf(stderr, 7806 " %u / %u identifier table lookups succeeded (%f%%)\n", 7807 NumIdentifierLookupHits, NumIdentifierLookups, 7808 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7809 7810 if (GlobalIndex) { 7811 std::fprintf(stderr, "\n"); 7812 GlobalIndex->printStats(); 7813 } 7814 7815 std::fprintf(stderr, "\n"); 7816 dump(); 7817 std::fprintf(stderr, "\n"); 7818 } 7819 7820 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7821 LLVM_DUMP_METHOD static void 7822 dumpModuleIDMap(StringRef Name, 7823 const ContinuousRangeMap<Key, ModuleFile *, 7824 InitialCapacity> &Map) { 7825 if (Map.begin() == Map.end()) 7826 return; 7827 7828 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7829 7830 llvm::errs() << Name << ":\n"; 7831 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7832 I != IEnd; ++I) { 7833 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7834 << "\n"; 7835 } 7836 } 7837 7838 LLVM_DUMP_METHOD void ASTReader::dump() { 7839 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7840 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7841 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7842 dumpModuleIDMap("Global type map", GlobalTypeMap); 7843 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7844 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7845 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7846 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7847 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7848 dumpModuleIDMap("Global preprocessed entity map", 7849 GlobalPreprocessedEntityMap); 7850 7851 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7852 for (ModuleFile &M : ModuleMgr) 7853 M.dump(); 7854 } 7855 7856 /// Return the amount of memory used by memory buffers, breaking down 7857 /// by heap-backed versus mmap'ed memory. 7858 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7859 for (ModuleFile &I : ModuleMgr) { 7860 if (llvm::MemoryBuffer *buf = I.Buffer) { 7861 size_t bytes = buf->getBufferSize(); 7862 switch (buf->getBufferKind()) { 7863 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7864 sizes.malloc_bytes += bytes; 7865 break; 7866 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7867 sizes.mmap_bytes += bytes; 7868 break; 7869 } 7870 } 7871 } 7872 } 7873 7874 void ASTReader::InitializeSema(Sema &S) { 7875 SemaObj = &S; 7876 S.addExternalSource(this); 7877 7878 // Makes sure any declarations that were deserialized "too early" 7879 // still get added to the identifier's declaration chains. 7880 for (uint64_t ID : PreloadedDeclIDs) { 7881 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7882 pushExternalDeclIntoScope(D, D->getDeclName()); 7883 } 7884 PreloadedDeclIDs.clear(); 7885 7886 // FIXME: What happens if these are changed by a module import? 7887 if (!FPPragmaOptions.empty()) { 7888 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7889 FPOptionsOverride NewOverrides = 7890 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7891 SemaObj->CurFPFeatures = 7892 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7893 } 7894 7895 SemaObj->OpenCLFeatures = OpenCLExtensions; 7896 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7897 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7898 7899 UpdateSema(); 7900 } 7901 7902 void ASTReader::UpdateSema() { 7903 assert(SemaObj && "no Sema to update"); 7904 7905 // Load the offsets of the declarations that Sema references. 7906 // They will be lazily deserialized when needed. 7907 if (!SemaDeclRefs.empty()) { 7908 assert(SemaDeclRefs.size() % 3 == 0); 7909 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7910 if (!SemaObj->StdNamespace) 7911 SemaObj->StdNamespace = SemaDeclRefs[I]; 7912 if (!SemaObj->StdBadAlloc) 7913 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7914 if (!SemaObj->StdAlignValT) 7915 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7916 } 7917 SemaDeclRefs.clear(); 7918 } 7919 7920 // Update the state of pragmas. Use the same API as if we had encountered the 7921 // pragma in the source. 7922 if(OptimizeOffPragmaLocation.isValid()) 7923 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7924 if (PragmaMSStructState != -1) 7925 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7926 if (PointersToMembersPragmaLocation.isValid()) { 7927 SemaObj->ActOnPragmaMSPointersToMembers( 7928 (LangOptions::PragmaMSPointersToMembersKind) 7929 PragmaMSPointersToMembersState, 7930 PointersToMembersPragmaLocation); 7931 } 7932 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7933 7934 if (PragmaAlignPackCurrentValue) { 7935 // The bottom of the stack might have a default value. It must be adjusted 7936 // to the current value to ensure that the packing state is preserved after 7937 // popping entries that were included/imported from a PCH/module. 7938 bool DropFirst = false; 7939 if (!PragmaAlignPackStack.empty() && 7940 PragmaAlignPackStack.front().Location.isInvalid()) { 7941 assert(PragmaAlignPackStack.front().Value == 7942 SemaObj->AlignPackStack.DefaultValue && 7943 "Expected a default alignment value"); 7944 SemaObj->AlignPackStack.Stack.emplace_back( 7945 PragmaAlignPackStack.front().SlotLabel, 7946 SemaObj->AlignPackStack.CurrentValue, 7947 SemaObj->AlignPackStack.CurrentPragmaLocation, 7948 PragmaAlignPackStack.front().PushLocation); 7949 DropFirst = true; 7950 } 7951 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack) 7952 .drop_front(DropFirst ? 1 : 0)) { 7953 SemaObj->AlignPackStack.Stack.emplace_back( 7954 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7955 } 7956 if (PragmaAlignPackCurrentLocation.isInvalid()) { 7957 assert(*PragmaAlignPackCurrentValue == 7958 SemaObj->AlignPackStack.DefaultValue && 7959 "Expected a default align and pack value"); 7960 // Keep the current values. 7961 } else { 7962 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue; 7963 SemaObj->AlignPackStack.CurrentPragmaLocation = 7964 PragmaAlignPackCurrentLocation; 7965 } 7966 } 7967 if (FpPragmaCurrentValue) { 7968 // The bottom of the stack might have a default value. It must be adjusted 7969 // to the current value to ensure that fp-pragma state is preserved after 7970 // popping entries that were included/imported from a PCH/module. 7971 bool DropFirst = false; 7972 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7973 assert(FpPragmaStack.front().Value == 7974 SemaObj->FpPragmaStack.DefaultValue && 7975 "Expected a default pragma float_control value"); 7976 SemaObj->FpPragmaStack.Stack.emplace_back( 7977 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7978 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7979 FpPragmaStack.front().PushLocation); 7980 DropFirst = true; 7981 } 7982 for (const auto &Entry : 7983 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7984 SemaObj->FpPragmaStack.Stack.emplace_back( 7985 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7986 if (FpPragmaCurrentLocation.isInvalid()) { 7987 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7988 "Expected a default pragma float_control value"); 7989 // Keep the current values. 7990 } else { 7991 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7992 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7993 } 7994 } 7995 7996 // For non-modular AST files, restore visiblity of modules. 7997 for (auto &Import : ImportedModules) { 7998 if (Import.ImportLoc.isInvalid()) 7999 continue; 8000 if (Module *Imported = getSubmodule(Import.ID)) { 8001 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 8002 } 8003 } 8004 } 8005 8006 IdentifierInfo *ASTReader::get(StringRef Name) { 8007 // Note that we are loading an identifier. 8008 Deserializing AnIdentifier(this); 8009 8010 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 8011 NumIdentifierLookups, 8012 NumIdentifierLookupHits); 8013 8014 // We don't need to do identifier table lookups in C++ modules (we preload 8015 // all interesting declarations, and don't need to use the scope for name 8016 // lookups). Perform the lookup in PCH files, though, since we don't build 8017 // a complete initial identifier table if we're carrying on from a PCH. 8018 if (PP.getLangOpts().CPlusPlus) { 8019 for (auto F : ModuleMgr.pch_modules()) 8020 if (Visitor(*F)) 8021 break; 8022 } else { 8023 // If there is a global index, look there first to determine which modules 8024 // provably do not have any results for this identifier. 8025 GlobalModuleIndex::HitSet Hits; 8026 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 8027 if (!loadGlobalIndex()) { 8028 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 8029 HitsPtr = &Hits; 8030 } 8031 } 8032 8033 ModuleMgr.visit(Visitor, HitsPtr); 8034 } 8035 8036 IdentifierInfo *II = Visitor.getIdentifierInfo(); 8037 markIdentifierUpToDate(II); 8038 return II; 8039 } 8040 8041 namespace clang { 8042 8043 /// An identifier-lookup iterator that enumerates all of the 8044 /// identifiers stored within a set of AST files. 8045 class ASTIdentifierIterator : public IdentifierIterator { 8046 /// The AST reader whose identifiers are being enumerated. 8047 const ASTReader &Reader; 8048 8049 /// The current index into the chain of AST files stored in 8050 /// the AST reader. 8051 unsigned Index; 8052 8053 /// The current position within the identifier lookup table 8054 /// of the current AST file. 8055 ASTIdentifierLookupTable::key_iterator Current; 8056 8057 /// The end position within the identifier lookup table of 8058 /// the current AST file. 8059 ASTIdentifierLookupTable::key_iterator End; 8060 8061 /// Whether to skip any modules in the ASTReader. 8062 bool SkipModules; 8063 8064 public: 8065 explicit ASTIdentifierIterator(const ASTReader &Reader, 8066 bool SkipModules = false); 8067 8068 StringRef Next() override; 8069 }; 8070 8071 } // namespace clang 8072 8073 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8074 bool SkipModules) 8075 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8076 } 8077 8078 StringRef ASTIdentifierIterator::Next() { 8079 while (Current == End) { 8080 // If we have exhausted all of our AST files, we're done. 8081 if (Index == 0) 8082 return StringRef(); 8083 8084 --Index; 8085 ModuleFile &F = Reader.ModuleMgr[Index]; 8086 if (SkipModules && F.isModule()) 8087 continue; 8088 8089 ASTIdentifierLookupTable *IdTable = 8090 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8091 Current = IdTable->key_begin(); 8092 End = IdTable->key_end(); 8093 } 8094 8095 // We have any identifiers remaining in the current AST file; return 8096 // the next one. 8097 StringRef Result = *Current; 8098 ++Current; 8099 return Result; 8100 } 8101 8102 namespace { 8103 8104 /// A utility for appending two IdentifierIterators. 8105 class ChainedIdentifierIterator : public IdentifierIterator { 8106 std::unique_ptr<IdentifierIterator> Current; 8107 std::unique_ptr<IdentifierIterator> Queued; 8108 8109 public: 8110 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8111 std::unique_ptr<IdentifierIterator> Second) 8112 : Current(std::move(First)), Queued(std::move(Second)) {} 8113 8114 StringRef Next() override { 8115 if (!Current) 8116 return StringRef(); 8117 8118 StringRef result = Current->Next(); 8119 if (!result.empty()) 8120 return result; 8121 8122 // Try the queued iterator, which may itself be empty. 8123 Current.reset(); 8124 std::swap(Current, Queued); 8125 return Next(); 8126 } 8127 }; 8128 8129 } // namespace 8130 8131 IdentifierIterator *ASTReader::getIdentifiers() { 8132 if (!loadGlobalIndex()) { 8133 std::unique_ptr<IdentifierIterator> ReaderIter( 8134 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8135 std::unique_ptr<IdentifierIterator> ModulesIter( 8136 GlobalIndex->createIdentifierIterator()); 8137 return new ChainedIdentifierIterator(std::move(ReaderIter), 8138 std::move(ModulesIter)); 8139 } 8140 8141 return new ASTIdentifierIterator(*this); 8142 } 8143 8144 namespace clang { 8145 namespace serialization { 8146 8147 class ReadMethodPoolVisitor { 8148 ASTReader &Reader; 8149 Selector Sel; 8150 unsigned PriorGeneration; 8151 unsigned InstanceBits = 0; 8152 unsigned FactoryBits = 0; 8153 bool InstanceHasMoreThanOneDecl = false; 8154 bool FactoryHasMoreThanOneDecl = false; 8155 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8156 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8157 8158 public: 8159 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8160 unsigned PriorGeneration) 8161 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8162 8163 bool operator()(ModuleFile &M) { 8164 if (!M.SelectorLookupTable) 8165 return false; 8166 8167 // If we've already searched this module file, skip it now. 8168 if (M.Generation <= PriorGeneration) 8169 return true; 8170 8171 ++Reader.NumMethodPoolTableLookups; 8172 ASTSelectorLookupTable *PoolTable 8173 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8174 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8175 if (Pos == PoolTable->end()) 8176 return false; 8177 8178 ++Reader.NumMethodPoolTableHits; 8179 ++Reader.NumSelectorsRead; 8180 // FIXME: Not quite happy with the statistics here. We probably should 8181 // disable this tracking when called via LoadSelector. 8182 // Also, should entries without methods count as misses? 8183 ++Reader.NumMethodPoolEntriesRead; 8184 ASTSelectorLookupTrait::data_type Data = *Pos; 8185 if (Reader.DeserializationListener) 8186 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8187 8188 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8189 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8190 InstanceBits = Data.InstanceBits; 8191 FactoryBits = Data.FactoryBits; 8192 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8193 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8194 return true; 8195 } 8196 8197 /// Retrieve the instance methods found by this visitor. 8198 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8199 return InstanceMethods; 8200 } 8201 8202 /// Retrieve the instance methods found by this visitor. 8203 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8204 return FactoryMethods; 8205 } 8206 8207 unsigned getInstanceBits() const { return InstanceBits; } 8208 unsigned getFactoryBits() const { return FactoryBits; } 8209 8210 bool instanceHasMoreThanOneDecl() const { 8211 return InstanceHasMoreThanOneDecl; 8212 } 8213 8214 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8215 }; 8216 8217 } // namespace serialization 8218 } // namespace clang 8219 8220 /// Add the given set of methods to the method list. 8221 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8222 ObjCMethodList &List) { 8223 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8224 S.addMethodToGlobalList(&List, Methods[I]); 8225 } 8226 } 8227 8228 void ASTReader::ReadMethodPool(Selector Sel) { 8229 // Get the selector generation and update it to the current generation. 8230 unsigned &Generation = SelectorGeneration[Sel]; 8231 unsigned PriorGeneration = Generation; 8232 Generation = getGeneration(); 8233 SelectorOutOfDate[Sel] = false; 8234 8235 // Search for methods defined with this selector. 8236 ++NumMethodPoolLookups; 8237 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8238 ModuleMgr.visit(Visitor); 8239 8240 if (Visitor.getInstanceMethods().empty() && 8241 Visitor.getFactoryMethods().empty()) 8242 return; 8243 8244 ++NumMethodPoolHits; 8245 8246 if (!getSema()) 8247 return; 8248 8249 Sema &S = *getSema(); 8250 Sema::GlobalMethodPool::iterator Pos 8251 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8252 8253 Pos->second.first.setBits(Visitor.getInstanceBits()); 8254 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8255 Pos->second.second.setBits(Visitor.getFactoryBits()); 8256 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8257 8258 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8259 // when building a module we keep every method individually and may need to 8260 // update hasMoreThanOneDecl as we add the methods. 8261 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8262 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8263 } 8264 8265 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8266 if (SelectorOutOfDate[Sel]) 8267 ReadMethodPool(Sel); 8268 } 8269 8270 void ASTReader::ReadKnownNamespaces( 8271 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8272 Namespaces.clear(); 8273 8274 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8275 if (NamespaceDecl *Namespace 8276 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8277 Namespaces.push_back(Namespace); 8278 } 8279 } 8280 8281 void ASTReader::ReadUndefinedButUsed( 8282 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8283 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8284 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8285 SourceLocation Loc = 8286 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8287 Undefined.insert(std::make_pair(D, Loc)); 8288 } 8289 } 8290 8291 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8292 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8293 Exprs) { 8294 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8295 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8296 uint64_t Count = DelayedDeleteExprs[Idx++]; 8297 for (uint64_t C = 0; C < Count; ++C) { 8298 SourceLocation DeleteLoc = 8299 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8300 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8301 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8302 } 8303 } 8304 } 8305 8306 void ASTReader::ReadTentativeDefinitions( 8307 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8308 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8309 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8310 if (Var) 8311 TentativeDefs.push_back(Var); 8312 } 8313 TentativeDefinitions.clear(); 8314 } 8315 8316 void ASTReader::ReadUnusedFileScopedDecls( 8317 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8318 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8319 DeclaratorDecl *D 8320 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8321 if (D) 8322 Decls.push_back(D); 8323 } 8324 UnusedFileScopedDecls.clear(); 8325 } 8326 8327 void ASTReader::ReadDelegatingConstructors( 8328 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8329 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8330 CXXConstructorDecl *D 8331 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8332 if (D) 8333 Decls.push_back(D); 8334 } 8335 DelegatingCtorDecls.clear(); 8336 } 8337 8338 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8339 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8340 TypedefNameDecl *D 8341 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8342 if (D) 8343 Decls.push_back(D); 8344 } 8345 ExtVectorDecls.clear(); 8346 } 8347 8348 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8349 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8350 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8351 ++I) { 8352 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8353 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8354 if (D) 8355 Decls.insert(D); 8356 } 8357 UnusedLocalTypedefNameCandidates.clear(); 8358 } 8359 8360 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8361 llvm::SmallVector<Decl *, 4> &Decls) { 8362 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N; 8363 ++I) { 8364 auto *D = dyn_cast_or_null<Decl>( 8365 GetDecl(DeclsToCheckForDeferredDiags[I])); 8366 if (D) 8367 Decls.push_back(D); 8368 } 8369 DeclsToCheckForDeferredDiags.clear(); 8370 } 8371 8372 8373 void ASTReader::ReadReferencedSelectors( 8374 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8375 if (ReferencedSelectorsData.empty()) 8376 return; 8377 8378 // If there are @selector references added them to its pool. This is for 8379 // implementation of -Wselector. 8380 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8381 unsigned I = 0; 8382 while (I < DataSize) { 8383 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8384 SourceLocation SelLoc 8385 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8386 Sels.push_back(std::make_pair(Sel, SelLoc)); 8387 } 8388 ReferencedSelectorsData.clear(); 8389 } 8390 8391 void ASTReader::ReadWeakUndeclaredIdentifiers( 8392 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8393 if (WeakUndeclaredIdentifiers.empty()) 8394 return; 8395 8396 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8397 IdentifierInfo *WeakId 8398 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8399 IdentifierInfo *AliasId 8400 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8401 SourceLocation Loc 8402 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8403 bool Used = WeakUndeclaredIdentifiers[I++]; 8404 WeakInfo WI(AliasId, Loc); 8405 WI.setUsed(Used); 8406 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8407 } 8408 WeakUndeclaredIdentifiers.clear(); 8409 } 8410 8411 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8412 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8413 ExternalVTableUse VT; 8414 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8415 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8416 VT.DefinitionRequired = VTableUses[Idx++]; 8417 VTables.push_back(VT); 8418 } 8419 8420 VTableUses.clear(); 8421 } 8422 8423 void ASTReader::ReadPendingInstantiations( 8424 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8425 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8426 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8427 SourceLocation Loc 8428 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8429 8430 Pending.push_back(std::make_pair(D, Loc)); 8431 } 8432 PendingInstantiations.clear(); 8433 } 8434 8435 void ASTReader::ReadLateParsedTemplates( 8436 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8437 &LPTMap) { 8438 for (auto &LPT : LateParsedTemplates) { 8439 ModuleFile *FMod = LPT.first; 8440 RecordDataImpl &LateParsed = LPT.second; 8441 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8442 /* In loop */) { 8443 FunctionDecl *FD = 8444 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8445 8446 auto LT = std::make_unique<LateParsedTemplate>(); 8447 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8448 8449 ModuleFile *F = getOwningModuleFile(LT->D); 8450 assert(F && "No module"); 8451 8452 unsigned TokN = LateParsed[Idx++]; 8453 LT->Toks.reserve(TokN); 8454 for (unsigned T = 0; T < TokN; ++T) 8455 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8456 8457 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8458 } 8459 } 8460 } 8461 8462 void ASTReader::LoadSelector(Selector Sel) { 8463 // It would be complicated to avoid reading the methods anyway. So don't. 8464 ReadMethodPool(Sel); 8465 } 8466 8467 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8468 assert(ID && "Non-zero identifier ID required"); 8469 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8470 IdentifiersLoaded[ID - 1] = II; 8471 if (DeserializationListener) 8472 DeserializationListener->IdentifierRead(ID, II); 8473 } 8474 8475 /// Set the globally-visible declarations associated with the given 8476 /// identifier. 8477 /// 8478 /// If the AST reader is currently in a state where the given declaration IDs 8479 /// cannot safely be resolved, they are queued until it is safe to resolve 8480 /// them. 8481 /// 8482 /// \param II an IdentifierInfo that refers to one or more globally-visible 8483 /// declarations. 8484 /// 8485 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8486 /// visible at global scope. 8487 /// 8488 /// \param Decls if non-null, this vector will be populated with the set of 8489 /// deserialized declarations. These declarations will not be pushed into 8490 /// scope. 8491 void 8492 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8493 const SmallVectorImpl<uint32_t> &DeclIDs, 8494 SmallVectorImpl<Decl *> *Decls) { 8495 if (NumCurrentElementsDeserializing && !Decls) { 8496 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8497 return; 8498 } 8499 8500 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8501 if (!SemaObj) { 8502 // Queue this declaration so that it will be added to the 8503 // translation unit scope and identifier's declaration chain 8504 // once a Sema object is known. 8505 PreloadedDeclIDs.push_back(DeclIDs[I]); 8506 continue; 8507 } 8508 8509 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8510 8511 // If we're simply supposed to record the declarations, do so now. 8512 if (Decls) { 8513 Decls->push_back(D); 8514 continue; 8515 } 8516 8517 // Introduce this declaration into the translation-unit scope 8518 // and add it to the declaration chain for this identifier, so 8519 // that (unqualified) name lookup will find it. 8520 pushExternalDeclIntoScope(D, II); 8521 } 8522 } 8523 8524 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8525 if (ID == 0) 8526 return nullptr; 8527 8528 if (IdentifiersLoaded.empty()) { 8529 Error("no identifier table in AST file"); 8530 return nullptr; 8531 } 8532 8533 ID -= 1; 8534 if (!IdentifiersLoaded[ID]) { 8535 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8536 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8537 ModuleFile *M = I->second; 8538 unsigned Index = ID - M->BaseIdentifierID; 8539 const unsigned char *Data = 8540 M->IdentifierTableData + M->IdentifierOffsets[Index]; 8541 8542 ASTIdentifierLookupTrait Trait(*this, *M); 8543 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 8544 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 8545 auto &II = PP.getIdentifierTable().get(Key); 8546 IdentifiersLoaded[ID] = &II; 8547 markIdentifierFromAST(*this, II); 8548 if (DeserializationListener) 8549 DeserializationListener->IdentifierRead(ID + 1, &II); 8550 } 8551 8552 return IdentifiersLoaded[ID]; 8553 } 8554 8555 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8556 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8557 } 8558 8559 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8560 if (LocalID < NUM_PREDEF_IDENT_IDS) 8561 return LocalID; 8562 8563 if (!M.ModuleOffsetMap.empty()) 8564 ReadModuleOffsetMap(M); 8565 8566 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8567 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8568 assert(I != M.IdentifierRemap.end() 8569 && "Invalid index into identifier index remap"); 8570 8571 return LocalID + I->second; 8572 } 8573 8574 MacroInfo *ASTReader::getMacro(MacroID ID) { 8575 if (ID == 0) 8576 return nullptr; 8577 8578 if (MacrosLoaded.empty()) { 8579 Error("no macro table in AST file"); 8580 return nullptr; 8581 } 8582 8583 ID -= NUM_PREDEF_MACRO_IDS; 8584 if (!MacrosLoaded[ID]) { 8585 GlobalMacroMapType::iterator I 8586 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8587 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8588 ModuleFile *M = I->second; 8589 unsigned Index = ID - M->BaseMacroID; 8590 MacrosLoaded[ID] = 8591 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8592 8593 if (DeserializationListener) 8594 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8595 MacrosLoaded[ID]); 8596 } 8597 8598 return MacrosLoaded[ID]; 8599 } 8600 8601 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8602 if (LocalID < NUM_PREDEF_MACRO_IDS) 8603 return LocalID; 8604 8605 if (!M.ModuleOffsetMap.empty()) 8606 ReadModuleOffsetMap(M); 8607 8608 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8609 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8610 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8611 8612 return LocalID + I->second; 8613 } 8614 8615 serialization::SubmoduleID 8616 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8617 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8618 return LocalID; 8619 8620 if (!M.ModuleOffsetMap.empty()) 8621 ReadModuleOffsetMap(M); 8622 8623 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8624 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8625 assert(I != M.SubmoduleRemap.end() 8626 && "Invalid index into submodule index remap"); 8627 8628 return LocalID + I->second; 8629 } 8630 8631 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8632 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8633 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8634 return nullptr; 8635 } 8636 8637 if (GlobalID > SubmodulesLoaded.size()) { 8638 Error("submodule ID out of range in AST file"); 8639 return nullptr; 8640 } 8641 8642 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8643 } 8644 8645 Module *ASTReader::getModule(unsigned ID) { 8646 return getSubmodule(ID); 8647 } 8648 8649 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8650 if (ID & 1) { 8651 // It's a module, look it up by submodule ID. 8652 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8653 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8654 } else { 8655 // It's a prefix (preamble, PCH, ...). Look it up by index. 8656 unsigned IndexFromEnd = ID >> 1; 8657 assert(IndexFromEnd && "got reference to unknown module file"); 8658 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8659 } 8660 } 8661 8662 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8663 if (!F) 8664 return 1; 8665 8666 // For a file representing a module, use the submodule ID of the top-level 8667 // module as the file ID. For any other kind of file, the number of such 8668 // files loaded beforehand will be the same on reload. 8669 // FIXME: Is this true even if we have an explicit module file and a PCH? 8670 if (F->isModule()) 8671 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8672 8673 auto PCHModules = getModuleManager().pch_modules(); 8674 auto I = llvm::find(PCHModules, F); 8675 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8676 return (I - PCHModules.end()) << 1; 8677 } 8678 8679 llvm::Optional<ASTSourceDescriptor> 8680 ASTReader::getSourceDescriptor(unsigned ID) { 8681 if (Module *M = getSubmodule(ID)) 8682 return ASTSourceDescriptor(*M); 8683 8684 // If there is only a single PCH, return it instead. 8685 // Chained PCH are not supported. 8686 const auto &PCHChain = ModuleMgr.pch_modules(); 8687 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8688 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8689 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8690 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8691 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8692 MF.Signature); 8693 } 8694 return None; 8695 } 8696 8697 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8698 auto I = DefinitionSource.find(FD); 8699 if (I == DefinitionSource.end()) 8700 return EK_ReplyHazy; 8701 return I->second ? EK_Never : EK_Always; 8702 } 8703 8704 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8705 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8706 } 8707 8708 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8709 if (ID == 0) 8710 return Selector(); 8711 8712 if (ID > SelectorsLoaded.size()) { 8713 Error("selector ID out of range in AST file"); 8714 return Selector(); 8715 } 8716 8717 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8718 // Load this selector from the selector table. 8719 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8720 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8721 ModuleFile &M = *I->second; 8722 ASTSelectorLookupTrait Trait(*this, M); 8723 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8724 SelectorsLoaded[ID - 1] = 8725 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8726 if (DeserializationListener) 8727 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8728 } 8729 8730 return SelectorsLoaded[ID - 1]; 8731 } 8732 8733 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8734 return DecodeSelector(ID); 8735 } 8736 8737 uint32_t ASTReader::GetNumExternalSelectors() { 8738 // ID 0 (the null selector) is considered an external selector. 8739 return getTotalNumSelectors() + 1; 8740 } 8741 8742 serialization::SelectorID 8743 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8744 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8745 return LocalID; 8746 8747 if (!M.ModuleOffsetMap.empty()) 8748 ReadModuleOffsetMap(M); 8749 8750 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8751 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8752 assert(I != M.SelectorRemap.end() 8753 && "Invalid index into selector index remap"); 8754 8755 return LocalID + I->second; 8756 } 8757 8758 DeclarationNameLoc 8759 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8760 switch (Name.getNameKind()) { 8761 case DeclarationName::CXXConstructorName: 8762 case DeclarationName::CXXDestructorName: 8763 case DeclarationName::CXXConversionFunctionName: 8764 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); 8765 8766 case DeclarationName::CXXOperatorName: 8767 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); 8768 8769 case DeclarationName::CXXLiteralOperatorName: 8770 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( 8771 readSourceLocation()); 8772 8773 case DeclarationName::Identifier: 8774 case DeclarationName::ObjCZeroArgSelector: 8775 case DeclarationName::ObjCOneArgSelector: 8776 case DeclarationName::ObjCMultiArgSelector: 8777 case DeclarationName::CXXUsingDirective: 8778 case DeclarationName::CXXDeductionGuideName: 8779 break; 8780 } 8781 return DeclarationNameLoc(); 8782 } 8783 8784 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8785 DeclarationNameInfo NameInfo; 8786 NameInfo.setName(readDeclarationName()); 8787 NameInfo.setLoc(readSourceLocation()); 8788 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8789 return NameInfo; 8790 } 8791 8792 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8793 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8794 unsigned NumTPLists = readInt(); 8795 Info.NumTemplParamLists = NumTPLists; 8796 if (NumTPLists) { 8797 Info.TemplParamLists = 8798 new (getContext()) TemplateParameterList *[NumTPLists]; 8799 for (unsigned i = 0; i != NumTPLists; ++i) 8800 Info.TemplParamLists[i] = readTemplateParameterList(); 8801 } 8802 } 8803 8804 TemplateParameterList * 8805 ASTRecordReader::readTemplateParameterList() { 8806 SourceLocation TemplateLoc = readSourceLocation(); 8807 SourceLocation LAngleLoc = readSourceLocation(); 8808 SourceLocation RAngleLoc = readSourceLocation(); 8809 8810 unsigned NumParams = readInt(); 8811 SmallVector<NamedDecl *, 16> Params; 8812 Params.reserve(NumParams); 8813 while (NumParams--) 8814 Params.push_back(readDeclAs<NamedDecl>()); 8815 8816 bool HasRequiresClause = readBool(); 8817 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8818 8819 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8820 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8821 return TemplateParams; 8822 } 8823 8824 void ASTRecordReader::readTemplateArgumentList( 8825 SmallVectorImpl<TemplateArgument> &TemplArgs, 8826 bool Canonicalize) { 8827 unsigned NumTemplateArgs = readInt(); 8828 TemplArgs.reserve(NumTemplateArgs); 8829 while (NumTemplateArgs--) 8830 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8831 } 8832 8833 /// Read a UnresolvedSet structure. 8834 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8835 unsigned NumDecls = readInt(); 8836 Set.reserve(getContext(), NumDecls); 8837 while (NumDecls--) { 8838 DeclID ID = readDeclID(); 8839 AccessSpecifier AS = (AccessSpecifier) readInt(); 8840 Set.addLazyDecl(getContext(), ID, AS); 8841 } 8842 } 8843 8844 CXXBaseSpecifier 8845 ASTRecordReader::readCXXBaseSpecifier() { 8846 bool isVirtual = readBool(); 8847 bool isBaseOfClass = readBool(); 8848 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8849 bool inheritConstructors = readBool(); 8850 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8851 SourceRange Range = readSourceRange(); 8852 SourceLocation EllipsisLoc = readSourceLocation(); 8853 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8854 EllipsisLoc); 8855 Result.setInheritConstructors(inheritConstructors); 8856 return Result; 8857 } 8858 8859 CXXCtorInitializer ** 8860 ASTRecordReader::readCXXCtorInitializers() { 8861 ASTContext &Context = getContext(); 8862 unsigned NumInitializers = readInt(); 8863 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8864 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8865 for (unsigned i = 0; i != NumInitializers; ++i) { 8866 TypeSourceInfo *TInfo = nullptr; 8867 bool IsBaseVirtual = false; 8868 FieldDecl *Member = nullptr; 8869 IndirectFieldDecl *IndirectMember = nullptr; 8870 8871 CtorInitializerType Type = (CtorInitializerType) readInt(); 8872 switch (Type) { 8873 case CTOR_INITIALIZER_BASE: 8874 TInfo = readTypeSourceInfo(); 8875 IsBaseVirtual = readBool(); 8876 break; 8877 8878 case CTOR_INITIALIZER_DELEGATING: 8879 TInfo = readTypeSourceInfo(); 8880 break; 8881 8882 case CTOR_INITIALIZER_MEMBER: 8883 Member = readDeclAs<FieldDecl>(); 8884 break; 8885 8886 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8887 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8888 break; 8889 } 8890 8891 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8892 Expr *Init = readExpr(); 8893 SourceLocation LParenLoc = readSourceLocation(); 8894 SourceLocation RParenLoc = readSourceLocation(); 8895 8896 CXXCtorInitializer *BOMInit; 8897 if (Type == CTOR_INITIALIZER_BASE) 8898 BOMInit = new (Context) 8899 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8900 RParenLoc, MemberOrEllipsisLoc); 8901 else if (Type == CTOR_INITIALIZER_DELEGATING) 8902 BOMInit = new (Context) 8903 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8904 else if (Member) 8905 BOMInit = new (Context) 8906 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8907 Init, RParenLoc); 8908 else 8909 BOMInit = new (Context) 8910 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8911 LParenLoc, Init, RParenLoc); 8912 8913 if (/*IsWritten*/readBool()) { 8914 unsigned SourceOrder = readInt(); 8915 BOMInit->setSourceOrder(SourceOrder); 8916 } 8917 8918 CtorInitializers[i] = BOMInit; 8919 } 8920 8921 return CtorInitializers; 8922 } 8923 8924 NestedNameSpecifierLoc 8925 ASTRecordReader::readNestedNameSpecifierLoc() { 8926 ASTContext &Context = getContext(); 8927 unsigned N = readInt(); 8928 NestedNameSpecifierLocBuilder Builder; 8929 for (unsigned I = 0; I != N; ++I) { 8930 auto Kind = readNestedNameSpecifierKind(); 8931 switch (Kind) { 8932 case NestedNameSpecifier::Identifier: { 8933 IdentifierInfo *II = readIdentifier(); 8934 SourceRange Range = readSourceRange(); 8935 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8936 break; 8937 } 8938 8939 case NestedNameSpecifier::Namespace: { 8940 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8941 SourceRange Range = readSourceRange(); 8942 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8943 break; 8944 } 8945 8946 case NestedNameSpecifier::NamespaceAlias: { 8947 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8948 SourceRange Range = readSourceRange(); 8949 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8950 break; 8951 } 8952 8953 case NestedNameSpecifier::TypeSpec: 8954 case NestedNameSpecifier::TypeSpecWithTemplate: { 8955 bool Template = readBool(); 8956 TypeSourceInfo *T = readTypeSourceInfo(); 8957 if (!T) 8958 return NestedNameSpecifierLoc(); 8959 SourceLocation ColonColonLoc = readSourceLocation(); 8960 8961 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8962 Builder.Extend(Context, 8963 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8964 T->getTypeLoc(), ColonColonLoc); 8965 break; 8966 } 8967 8968 case NestedNameSpecifier::Global: { 8969 SourceLocation ColonColonLoc = readSourceLocation(); 8970 Builder.MakeGlobal(Context, ColonColonLoc); 8971 break; 8972 } 8973 8974 case NestedNameSpecifier::Super: { 8975 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8976 SourceRange Range = readSourceRange(); 8977 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8978 break; 8979 } 8980 } 8981 } 8982 8983 return Builder.getWithLocInContext(Context); 8984 } 8985 8986 SourceRange 8987 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8988 unsigned &Idx) { 8989 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8990 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8991 return SourceRange(beg, end); 8992 } 8993 8994 /// Read a floating-point value 8995 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 8996 return llvm::APFloat(Sem, readAPInt()); 8997 } 8998 8999 // Read a string 9000 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9001 unsigned Len = Record[Idx++]; 9002 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9003 Idx += Len; 9004 return Result; 9005 } 9006 9007 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9008 unsigned &Idx) { 9009 std::string Filename = ReadString(Record, Idx); 9010 ResolveImportedPath(F, Filename); 9011 return Filename; 9012 } 9013 9014 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9015 const RecordData &Record, unsigned &Idx) { 9016 std::string Filename = ReadString(Record, Idx); 9017 if (!BaseDirectory.empty()) 9018 ResolveImportedPath(Filename, BaseDirectory); 9019 return Filename; 9020 } 9021 9022 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9023 unsigned &Idx) { 9024 unsigned Major = Record[Idx++]; 9025 unsigned Minor = Record[Idx++]; 9026 unsigned Subminor = Record[Idx++]; 9027 if (Minor == 0) 9028 return VersionTuple(Major); 9029 if (Subminor == 0) 9030 return VersionTuple(Major, Minor - 1); 9031 return VersionTuple(Major, Minor - 1, Subminor - 1); 9032 } 9033 9034 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9035 const RecordData &Record, 9036 unsigned &Idx) { 9037 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9038 return CXXTemporary::Create(getContext(), Decl); 9039 } 9040 9041 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9042 return Diag(CurrentImportLoc, DiagID); 9043 } 9044 9045 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9046 return Diags.Report(Loc, DiagID); 9047 } 9048 9049 /// Retrieve the identifier table associated with the 9050 /// preprocessor. 9051 IdentifierTable &ASTReader::getIdentifierTable() { 9052 return PP.getIdentifierTable(); 9053 } 9054 9055 /// Record that the given ID maps to the given switch-case 9056 /// statement. 9057 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9058 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9059 "Already have a SwitchCase with this ID"); 9060 (*CurrSwitchCaseStmts)[ID] = SC; 9061 } 9062 9063 /// Retrieve the switch-case statement with the given ID. 9064 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9065 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9066 return (*CurrSwitchCaseStmts)[ID]; 9067 } 9068 9069 void ASTReader::ClearSwitchCaseIDs() { 9070 CurrSwitchCaseStmts->clear(); 9071 } 9072 9073 void ASTReader::ReadComments() { 9074 ASTContext &Context = getContext(); 9075 std::vector<RawComment *> Comments; 9076 for (SmallVectorImpl<std::pair<BitstreamCursor, 9077 serialization::ModuleFile *>>::iterator 9078 I = CommentsCursors.begin(), 9079 E = CommentsCursors.end(); 9080 I != E; ++I) { 9081 Comments.clear(); 9082 BitstreamCursor &Cursor = I->first; 9083 serialization::ModuleFile &F = *I->second; 9084 SavedStreamPosition SavedPosition(Cursor); 9085 9086 RecordData Record; 9087 while (true) { 9088 Expected<llvm::BitstreamEntry> MaybeEntry = 9089 Cursor.advanceSkippingSubblocks( 9090 BitstreamCursor::AF_DontPopBlockAtEnd); 9091 if (!MaybeEntry) { 9092 Error(MaybeEntry.takeError()); 9093 return; 9094 } 9095 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9096 9097 switch (Entry.Kind) { 9098 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9099 case llvm::BitstreamEntry::Error: 9100 Error("malformed block record in AST file"); 9101 return; 9102 case llvm::BitstreamEntry::EndBlock: 9103 goto NextCursor; 9104 case llvm::BitstreamEntry::Record: 9105 // The interesting case. 9106 break; 9107 } 9108 9109 // Read a record. 9110 Record.clear(); 9111 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9112 if (!MaybeComment) { 9113 Error(MaybeComment.takeError()); 9114 return; 9115 } 9116 switch ((CommentRecordTypes)MaybeComment.get()) { 9117 case COMMENTS_RAW_COMMENT: { 9118 unsigned Idx = 0; 9119 SourceRange SR = ReadSourceRange(F, Record, Idx); 9120 RawComment::CommentKind Kind = 9121 (RawComment::CommentKind) Record[Idx++]; 9122 bool IsTrailingComment = Record[Idx++]; 9123 bool IsAlmostTrailingComment = Record[Idx++]; 9124 Comments.push_back(new (Context) RawComment( 9125 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9126 break; 9127 } 9128 } 9129 } 9130 NextCursor: 9131 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9132 FileToOffsetToComment; 9133 for (RawComment *C : Comments) { 9134 SourceLocation CommentLoc = C->getBeginLoc(); 9135 if (CommentLoc.isValid()) { 9136 std::pair<FileID, unsigned> Loc = 9137 SourceMgr.getDecomposedLoc(CommentLoc); 9138 if (Loc.first.isValid()) 9139 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9140 } 9141 } 9142 } 9143 } 9144 9145 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9146 bool IncludeSystem, bool Complain, 9147 llvm::function_ref<void(const serialization::InputFile &IF, 9148 bool isSystem)> Visitor) { 9149 unsigned NumUserInputs = MF.NumUserInputFiles; 9150 unsigned NumInputs = MF.InputFilesLoaded.size(); 9151 assert(NumUserInputs <= NumInputs); 9152 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9153 for (unsigned I = 0; I < N; ++I) { 9154 bool IsSystem = I >= NumUserInputs; 9155 InputFile IF = getInputFile(MF, I+1, Complain); 9156 Visitor(IF, IsSystem); 9157 } 9158 } 9159 9160 void ASTReader::visitTopLevelModuleMaps( 9161 serialization::ModuleFile &MF, 9162 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9163 unsigned NumInputs = MF.InputFilesLoaded.size(); 9164 for (unsigned I = 0; I < NumInputs; ++I) { 9165 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9166 if (IFI.TopLevelModuleMap) 9167 // FIXME: This unnecessarily re-reads the InputFileInfo. 9168 if (auto FE = getInputFile(MF, I + 1).getFile()) 9169 Visitor(FE); 9170 } 9171 } 9172 9173 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9174 // If we know the owning module, use it. 9175 if (Module *M = D->getImportedOwningModule()) 9176 return M->getFullModuleName(); 9177 9178 // Otherwise, use the name of the top-level module the decl is within. 9179 if (ModuleFile *M = getOwningModuleFile(D)) 9180 return M->ModuleName; 9181 9182 // Not from a module. 9183 return {}; 9184 } 9185 9186 void ASTReader::finishPendingActions() { 9187 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9188 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9189 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9190 !PendingUpdateRecords.empty()) { 9191 // If any identifiers with corresponding top-level declarations have 9192 // been loaded, load those declarations now. 9193 using TopLevelDeclsMap = 9194 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9195 TopLevelDeclsMap TopLevelDecls; 9196 9197 while (!PendingIdentifierInfos.empty()) { 9198 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9199 SmallVector<uint32_t, 4> DeclIDs = 9200 std::move(PendingIdentifierInfos.back().second); 9201 PendingIdentifierInfos.pop_back(); 9202 9203 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9204 } 9205 9206 // Load each function type that we deferred loading because it was a 9207 // deduced type that might refer to a local type declared within itself. 9208 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9209 auto *FD = PendingFunctionTypes[I].first; 9210 FD->setType(GetType(PendingFunctionTypes[I].second)); 9211 9212 // If we gave a function a deduced return type, remember that we need to 9213 // propagate that along the redeclaration chain. 9214 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9215 if (DT && DT->isDeduced()) 9216 PendingDeducedTypeUpdates.insert( 9217 {FD->getCanonicalDecl(), FD->getReturnType()}); 9218 } 9219 PendingFunctionTypes.clear(); 9220 9221 // For each decl chain that we wanted to complete while deserializing, mark 9222 // it as "still needs to be completed". 9223 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9224 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9225 } 9226 PendingIncompleteDeclChains.clear(); 9227 9228 // Load pending declaration chains. 9229 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9230 loadPendingDeclChain(PendingDeclChains[I].first, 9231 PendingDeclChains[I].second); 9232 PendingDeclChains.clear(); 9233 9234 // Make the most recent of the top-level declarations visible. 9235 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9236 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9237 IdentifierInfo *II = TLD->first; 9238 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9239 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9240 } 9241 } 9242 9243 // Load any pending macro definitions. 9244 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9245 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9246 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9247 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9248 // Initialize the macro history from chained-PCHs ahead of module imports. 9249 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9250 ++IDIdx) { 9251 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9252 if (!Info.M->isModule()) 9253 resolvePendingMacro(II, Info); 9254 } 9255 // Handle module imports. 9256 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9257 ++IDIdx) { 9258 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9259 if (Info.M->isModule()) 9260 resolvePendingMacro(II, Info); 9261 } 9262 } 9263 PendingMacroIDs.clear(); 9264 9265 // Wire up the DeclContexts for Decls that we delayed setting until 9266 // recursive loading is completed. 9267 while (!PendingDeclContextInfos.empty()) { 9268 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9269 PendingDeclContextInfos.pop_front(); 9270 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9271 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9272 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9273 } 9274 9275 // Perform any pending declaration updates. 9276 while (!PendingUpdateRecords.empty()) { 9277 auto Update = PendingUpdateRecords.pop_back_val(); 9278 ReadingKindTracker ReadingKind(Read_Decl, *this); 9279 loadDeclUpdateRecords(Update); 9280 } 9281 } 9282 9283 // At this point, all update records for loaded decls are in place, so any 9284 // fake class definitions should have become real. 9285 assert(PendingFakeDefinitionData.empty() && 9286 "faked up a class definition but never saw the real one"); 9287 9288 // If we deserialized any C++ or Objective-C class definitions, any 9289 // Objective-C protocol definitions, or any redeclarable templates, make sure 9290 // that all redeclarations point to the definitions. Note that this can only 9291 // happen now, after the redeclaration chains have been fully wired. 9292 for (Decl *D : PendingDefinitions) { 9293 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9294 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9295 // Make sure that the TagType points at the definition. 9296 const_cast<TagType*>(TagT)->decl = TD; 9297 } 9298 9299 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9300 for (auto *R = getMostRecentExistingDecl(RD); R; 9301 R = R->getPreviousDecl()) { 9302 assert((R == D) == 9303 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9304 "declaration thinks it's the definition but it isn't"); 9305 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9306 } 9307 } 9308 9309 continue; 9310 } 9311 9312 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9313 // Make sure that the ObjCInterfaceType points at the definition. 9314 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9315 ->Decl = ID; 9316 9317 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9318 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9319 9320 continue; 9321 } 9322 9323 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9324 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9325 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9326 9327 continue; 9328 } 9329 9330 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9331 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9332 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9333 } 9334 PendingDefinitions.clear(); 9335 9336 // Load the bodies of any functions or methods we've encountered. We do 9337 // this now (delayed) so that we can be sure that the declaration chains 9338 // have been fully wired up (hasBody relies on this). 9339 // FIXME: We shouldn't require complete redeclaration chains here. 9340 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9341 PBEnd = PendingBodies.end(); 9342 PB != PBEnd; ++PB) { 9343 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9344 // For a function defined inline within a class template, force the 9345 // canonical definition to be the one inside the canonical definition of 9346 // the template. This ensures that we instantiate from a correct view 9347 // of the template. 9348 // 9349 // Sadly we can't do this more generally: we can't be sure that all 9350 // copies of an arbitrary class definition will have the same members 9351 // defined (eg, some member functions may not be instantiated, and some 9352 // special members may or may not have been implicitly defined). 9353 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9354 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9355 continue; 9356 9357 // FIXME: Check for =delete/=default? 9358 // FIXME: Complain about ODR violations here? 9359 const FunctionDecl *Defn = nullptr; 9360 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9361 FD->setLazyBody(PB->second); 9362 } else { 9363 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9364 mergeDefinitionVisibility(NonConstDefn, FD); 9365 9366 if (!FD->isLateTemplateParsed() && 9367 !NonConstDefn->isLateTemplateParsed() && 9368 FD->getODRHash() != NonConstDefn->getODRHash()) { 9369 if (!isa<CXXMethodDecl>(FD)) { 9370 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9371 } else if (FD->getLexicalParent()->isFileContext() && 9372 NonConstDefn->getLexicalParent()->isFileContext()) { 9373 // Only diagnose out-of-line method definitions. If they are 9374 // in class definitions, then an error will be generated when 9375 // processing the class bodies. 9376 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9377 } 9378 } 9379 } 9380 continue; 9381 } 9382 9383 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9384 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9385 MD->setLazyBody(PB->second); 9386 } 9387 PendingBodies.clear(); 9388 9389 // Do some cleanup. 9390 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9391 getContext().deduplicateMergedDefinitonsFor(ND); 9392 PendingMergedDefinitionsToDeduplicate.clear(); 9393 } 9394 9395 void ASTReader::diagnoseOdrViolations() { 9396 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9397 PendingFunctionOdrMergeFailures.empty() && 9398 PendingEnumOdrMergeFailures.empty()) 9399 return; 9400 9401 // Trigger the import of the full definition of each class that had any 9402 // odr-merging problems, so we can produce better diagnostics for them. 9403 // These updates may in turn find and diagnose some ODR failures, so take 9404 // ownership of the set first. 9405 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9406 PendingOdrMergeFailures.clear(); 9407 for (auto &Merge : OdrMergeFailures) { 9408 Merge.first->buildLookup(); 9409 Merge.first->decls_begin(); 9410 Merge.first->bases_begin(); 9411 Merge.first->vbases_begin(); 9412 for (auto &RecordPair : Merge.second) { 9413 auto *RD = RecordPair.first; 9414 RD->decls_begin(); 9415 RD->bases_begin(); 9416 RD->vbases_begin(); 9417 } 9418 } 9419 9420 // Trigger the import of functions. 9421 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9422 PendingFunctionOdrMergeFailures.clear(); 9423 for (auto &Merge : FunctionOdrMergeFailures) { 9424 Merge.first->buildLookup(); 9425 Merge.first->decls_begin(); 9426 Merge.first->getBody(); 9427 for (auto &FD : Merge.second) { 9428 FD->buildLookup(); 9429 FD->decls_begin(); 9430 FD->getBody(); 9431 } 9432 } 9433 9434 // Trigger the import of enums. 9435 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9436 PendingEnumOdrMergeFailures.clear(); 9437 for (auto &Merge : EnumOdrMergeFailures) { 9438 Merge.first->decls_begin(); 9439 for (auto &Enum : Merge.second) { 9440 Enum->decls_begin(); 9441 } 9442 } 9443 9444 // For each declaration from a merged context, check that the canonical 9445 // definition of that context also contains a declaration of the same 9446 // entity. 9447 // 9448 // Caution: this loop does things that might invalidate iterators into 9449 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9450 while (!PendingOdrMergeChecks.empty()) { 9451 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9452 9453 // FIXME: Skip over implicit declarations for now. This matters for things 9454 // like implicitly-declared special member functions. This isn't entirely 9455 // correct; we can end up with multiple unmerged declarations of the same 9456 // implicit entity. 9457 if (D->isImplicit()) 9458 continue; 9459 9460 DeclContext *CanonDef = D->getDeclContext(); 9461 9462 bool Found = false; 9463 const Decl *DCanon = D->getCanonicalDecl(); 9464 9465 for (auto RI : D->redecls()) { 9466 if (RI->getLexicalDeclContext() == CanonDef) { 9467 Found = true; 9468 break; 9469 } 9470 } 9471 if (Found) 9472 continue; 9473 9474 // Quick check failed, time to do the slow thing. Note, we can't just 9475 // look up the name of D in CanonDef here, because the member that is 9476 // in CanonDef might not be found by name lookup (it might have been 9477 // replaced by a more recent declaration in the lookup table), and we 9478 // can't necessarily find it in the redeclaration chain because it might 9479 // be merely mergeable, not redeclarable. 9480 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9481 for (auto *CanonMember : CanonDef->decls()) { 9482 if (CanonMember->getCanonicalDecl() == DCanon) { 9483 // This can happen if the declaration is merely mergeable and not 9484 // actually redeclarable (we looked for redeclarations earlier). 9485 // 9486 // FIXME: We should be able to detect this more efficiently, without 9487 // pulling in all of the members of CanonDef. 9488 Found = true; 9489 break; 9490 } 9491 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9492 if (ND->getDeclName() == D->getDeclName()) 9493 Candidates.push_back(ND); 9494 } 9495 9496 if (!Found) { 9497 // The AST doesn't like TagDecls becoming invalid after they've been 9498 // completed. We only really need to mark FieldDecls as invalid here. 9499 if (!isa<TagDecl>(D)) 9500 D->setInvalidDecl(); 9501 9502 // Ensure we don't accidentally recursively enter deserialization while 9503 // we're producing our diagnostic. 9504 Deserializing RecursionGuard(this); 9505 9506 std::string CanonDefModule = 9507 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9508 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9509 << D << getOwningModuleNameForDiagnostic(D) 9510 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9511 9512 if (Candidates.empty()) 9513 Diag(cast<Decl>(CanonDef)->getLocation(), 9514 diag::note_module_odr_violation_no_possible_decls) << D; 9515 else { 9516 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9517 Diag(Candidates[I]->getLocation(), 9518 diag::note_module_odr_violation_possible_decl) 9519 << Candidates[I]; 9520 } 9521 9522 DiagnosedOdrMergeFailures.insert(CanonDef); 9523 } 9524 } 9525 9526 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9527 EnumOdrMergeFailures.empty()) 9528 return; 9529 9530 // Ensure we don't accidentally recursively enter deserialization while 9531 // we're producing our diagnostics. 9532 Deserializing RecursionGuard(this); 9533 9534 // Common code for hashing helpers. 9535 ODRHash Hash; 9536 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9537 Hash.clear(); 9538 Hash.AddQualType(Ty); 9539 return Hash.CalculateHash(); 9540 }; 9541 9542 auto ComputeODRHash = [&Hash](const Stmt *S) { 9543 assert(S); 9544 Hash.clear(); 9545 Hash.AddStmt(S); 9546 return Hash.CalculateHash(); 9547 }; 9548 9549 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9550 assert(D); 9551 Hash.clear(); 9552 Hash.AddSubDecl(D); 9553 return Hash.CalculateHash(); 9554 }; 9555 9556 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9557 Hash.clear(); 9558 Hash.AddTemplateArgument(TA); 9559 return Hash.CalculateHash(); 9560 }; 9561 9562 auto ComputeTemplateParameterListODRHash = 9563 [&Hash](const TemplateParameterList *TPL) { 9564 assert(TPL); 9565 Hash.clear(); 9566 Hash.AddTemplateParameterList(TPL); 9567 return Hash.CalculateHash(); 9568 }; 9569 9570 // Used with err_module_odr_violation_mismatch_decl and 9571 // note_module_odr_violation_mismatch_decl 9572 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9573 enum ODRMismatchDecl { 9574 EndOfClass, 9575 PublicSpecifer, 9576 PrivateSpecifer, 9577 ProtectedSpecifer, 9578 StaticAssert, 9579 Field, 9580 CXXMethod, 9581 TypeAlias, 9582 TypeDef, 9583 Var, 9584 Friend, 9585 FunctionTemplate, 9586 Other 9587 }; 9588 9589 // Used with err_module_odr_violation_mismatch_decl_diff and 9590 // note_module_odr_violation_mismatch_decl_diff 9591 enum ODRMismatchDeclDifference { 9592 StaticAssertCondition, 9593 StaticAssertMessage, 9594 StaticAssertOnlyMessage, 9595 FieldName, 9596 FieldTypeName, 9597 FieldSingleBitField, 9598 FieldDifferentWidthBitField, 9599 FieldSingleMutable, 9600 FieldSingleInitializer, 9601 FieldDifferentInitializers, 9602 MethodName, 9603 MethodDeleted, 9604 MethodDefaulted, 9605 MethodVirtual, 9606 MethodStatic, 9607 MethodVolatile, 9608 MethodConst, 9609 MethodInline, 9610 MethodNumberParameters, 9611 MethodParameterType, 9612 MethodParameterName, 9613 MethodParameterSingleDefaultArgument, 9614 MethodParameterDifferentDefaultArgument, 9615 MethodNoTemplateArguments, 9616 MethodDifferentNumberTemplateArguments, 9617 MethodDifferentTemplateArgument, 9618 MethodSingleBody, 9619 MethodDifferentBody, 9620 TypedefName, 9621 TypedefType, 9622 VarName, 9623 VarType, 9624 VarSingleInitializer, 9625 VarDifferentInitializer, 9626 VarConstexpr, 9627 FriendTypeFunction, 9628 FriendType, 9629 FriendFunction, 9630 FunctionTemplateDifferentNumberParameters, 9631 FunctionTemplateParameterDifferentKind, 9632 FunctionTemplateParameterName, 9633 FunctionTemplateParameterSingleDefaultArgument, 9634 FunctionTemplateParameterDifferentDefaultArgument, 9635 FunctionTemplateParameterDifferentType, 9636 FunctionTemplatePackParameter, 9637 }; 9638 9639 // These lambdas have the common portions of the ODR diagnostics. This 9640 // has the same return as Diag(), so addition parameters can be passed 9641 // in with operator<< 9642 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9643 SourceLocation Loc, SourceRange Range, 9644 ODRMismatchDeclDifference DiffType) { 9645 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9646 << FirstRecord << FirstModule.empty() << FirstModule << Range 9647 << DiffType; 9648 }; 9649 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9650 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9651 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9652 << SecondModule << Range << DiffType; 9653 }; 9654 9655 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9656 &ComputeQualTypeODRHash, &ComputeODRHash]( 9657 NamedDecl *FirstRecord, StringRef FirstModule, 9658 StringRef SecondModule, FieldDecl *FirstField, 9659 FieldDecl *SecondField) { 9660 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9661 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9662 if (FirstII->getName() != SecondII->getName()) { 9663 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9664 FirstField->getSourceRange(), FieldName) 9665 << FirstII; 9666 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9667 SecondField->getSourceRange(), FieldName) 9668 << SecondII; 9669 9670 return true; 9671 } 9672 9673 assert(getContext().hasSameType(FirstField->getType(), 9674 SecondField->getType())); 9675 9676 QualType FirstType = FirstField->getType(); 9677 QualType SecondType = SecondField->getType(); 9678 if (ComputeQualTypeODRHash(FirstType) != 9679 ComputeQualTypeODRHash(SecondType)) { 9680 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9681 FirstField->getSourceRange(), FieldTypeName) 9682 << FirstII << FirstType; 9683 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9684 SecondField->getSourceRange(), FieldTypeName) 9685 << SecondII << SecondType; 9686 9687 return true; 9688 } 9689 9690 const bool IsFirstBitField = FirstField->isBitField(); 9691 const bool IsSecondBitField = SecondField->isBitField(); 9692 if (IsFirstBitField != IsSecondBitField) { 9693 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9694 FirstField->getSourceRange(), FieldSingleBitField) 9695 << FirstII << IsFirstBitField; 9696 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9697 SecondField->getSourceRange(), FieldSingleBitField) 9698 << SecondII << IsSecondBitField; 9699 return true; 9700 } 9701 9702 if (IsFirstBitField && IsSecondBitField) { 9703 unsigned FirstBitWidthHash = 9704 ComputeODRHash(FirstField->getBitWidth()); 9705 unsigned SecondBitWidthHash = 9706 ComputeODRHash(SecondField->getBitWidth()); 9707 if (FirstBitWidthHash != SecondBitWidthHash) { 9708 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9709 FirstField->getSourceRange(), 9710 FieldDifferentWidthBitField) 9711 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9712 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9713 SecondField->getSourceRange(), 9714 FieldDifferentWidthBitField) 9715 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9716 return true; 9717 } 9718 } 9719 9720 if (!PP.getLangOpts().CPlusPlus) 9721 return false; 9722 9723 const bool IsFirstMutable = FirstField->isMutable(); 9724 const bool IsSecondMutable = SecondField->isMutable(); 9725 if (IsFirstMutable != IsSecondMutable) { 9726 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9727 FirstField->getSourceRange(), FieldSingleMutable) 9728 << FirstII << IsFirstMutable; 9729 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9730 SecondField->getSourceRange(), FieldSingleMutable) 9731 << SecondII << IsSecondMutable; 9732 return true; 9733 } 9734 9735 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9736 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9737 if ((!FirstInitializer && SecondInitializer) || 9738 (FirstInitializer && !SecondInitializer)) { 9739 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9740 FirstField->getSourceRange(), FieldSingleInitializer) 9741 << FirstII << (FirstInitializer != nullptr); 9742 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9743 SecondField->getSourceRange(), FieldSingleInitializer) 9744 << SecondII << (SecondInitializer != nullptr); 9745 return true; 9746 } 9747 9748 if (FirstInitializer && SecondInitializer) { 9749 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9750 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9751 if (FirstInitHash != SecondInitHash) { 9752 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9753 FirstField->getSourceRange(), 9754 FieldDifferentInitializers) 9755 << FirstII << FirstInitializer->getSourceRange(); 9756 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9757 SecondField->getSourceRange(), 9758 FieldDifferentInitializers) 9759 << SecondII << SecondInitializer->getSourceRange(); 9760 return true; 9761 } 9762 } 9763 9764 return false; 9765 }; 9766 9767 auto ODRDiagTypeDefOrAlias = 9768 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9769 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9770 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9771 bool IsTypeAlias) { 9772 auto FirstName = FirstTD->getDeclName(); 9773 auto SecondName = SecondTD->getDeclName(); 9774 if (FirstName != SecondName) { 9775 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9776 FirstTD->getSourceRange(), TypedefName) 9777 << IsTypeAlias << FirstName; 9778 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9779 SecondTD->getSourceRange(), TypedefName) 9780 << IsTypeAlias << SecondName; 9781 return true; 9782 } 9783 9784 QualType FirstType = FirstTD->getUnderlyingType(); 9785 QualType SecondType = SecondTD->getUnderlyingType(); 9786 if (ComputeQualTypeODRHash(FirstType) != 9787 ComputeQualTypeODRHash(SecondType)) { 9788 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9789 FirstTD->getSourceRange(), TypedefType) 9790 << IsTypeAlias << FirstName << FirstType; 9791 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9792 SecondTD->getSourceRange(), TypedefType) 9793 << IsTypeAlias << SecondName << SecondType; 9794 return true; 9795 } 9796 9797 return false; 9798 }; 9799 9800 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9801 &ComputeQualTypeODRHash, &ComputeODRHash, 9802 this](NamedDecl *FirstRecord, StringRef FirstModule, 9803 StringRef SecondModule, VarDecl *FirstVD, 9804 VarDecl *SecondVD) { 9805 auto FirstName = FirstVD->getDeclName(); 9806 auto SecondName = SecondVD->getDeclName(); 9807 if (FirstName != SecondName) { 9808 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9809 FirstVD->getSourceRange(), VarName) 9810 << FirstName; 9811 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9812 SecondVD->getSourceRange(), VarName) 9813 << SecondName; 9814 return true; 9815 } 9816 9817 QualType FirstType = FirstVD->getType(); 9818 QualType SecondType = SecondVD->getType(); 9819 if (ComputeQualTypeODRHash(FirstType) != 9820 ComputeQualTypeODRHash(SecondType)) { 9821 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9822 FirstVD->getSourceRange(), VarType) 9823 << FirstName << FirstType; 9824 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9825 SecondVD->getSourceRange(), VarType) 9826 << SecondName << SecondType; 9827 return true; 9828 } 9829 9830 if (!PP.getLangOpts().CPlusPlus) 9831 return false; 9832 9833 const Expr *FirstInit = FirstVD->getInit(); 9834 const Expr *SecondInit = SecondVD->getInit(); 9835 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9836 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9837 FirstVD->getSourceRange(), VarSingleInitializer) 9838 << FirstName << (FirstInit == nullptr) 9839 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9840 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9841 SecondVD->getSourceRange(), VarSingleInitializer) 9842 << SecondName << (SecondInit == nullptr) 9843 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9844 return true; 9845 } 9846 9847 if (FirstInit && SecondInit && 9848 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9849 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9850 FirstVD->getSourceRange(), VarDifferentInitializer) 9851 << FirstName << FirstInit->getSourceRange(); 9852 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9853 SecondVD->getSourceRange(), VarDifferentInitializer) 9854 << SecondName << SecondInit->getSourceRange(); 9855 return true; 9856 } 9857 9858 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9859 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9860 if (FirstIsConstexpr != SecondIsConstexpr) { 9861 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9862 FirstVD->getSourceRange(), VarConstexpr) 9863 << FirstName << FirstIsConstexpr; 9864 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9865 SecondVD->getSourceRange(), VarConstexpr) 9866 << SecondName << SecondIsConstexpr; 9867 return true; 9868 } 9869 return false; 9870 }; 9871 9872 auto DifferenceSelector = [](Decl *D) { 9873 assert(D && "valid Decl required"); 9874 switch (D->getKind()) { 9875 default: 9876 return Other; 9877 case Decl::AccessSpec: 9878 switch (D->getAccess()) { 9879 case AS_public: 9880 return PublicSpecifer; 9881 case AS_private: 9882 return PrivateSpecifer; 9883 case AS_protected: 9884 return ProtectedSpecifer; 9885 case AS_none: 9886 break; 9887 } 9888 llvm_unreachable("Invalid access specifier"); 9889 case Decl::StaticAssert: 9890 return StaticAssert; 9891 case Decl::Field: 9892 return Field; 9893 case Decl::CXXMethod: 9894 case Decl::CXXConstructor: 9895 case Decl::CXXDestructor: 9896 return CXXMethod; 9897 case Decl::TypeAlias: 9898 return TypeAlias; 9899 case Decl::Typedef: 9900 return TypeDef; 9901 case Decl::Var: 9902 return Var; 9903 case Decl::Friend: 9904 return Friend; 9905 case Decl::FunctionTemplate: 9906 return FunctionTemplate; 9907 } 9908 }; 9909 9910 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9911 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9912 RecordDecl *Record, 9913 const DeclContext *DC) { 9914 for (auto *D : Record->decls()) { 9915 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9916 continue; 9917 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9918 } 9919 }; 9920 9921 struct DiffResult { 9922 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9923 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9924 }; 9925 9926 // If there is a diagnoseable difference, FirstDiffType and 9927 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9928 // filled in if not EndOfClass. 9929 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9930 DeclHashes &SecondHashes) { 9931 DiffResult DR; 9932 auto FirstIt = FirstHashes.begin(); 9933 auto SecondIt = SecondHashes.begin(); 9934 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9935 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9936 FirstIt->second == SecondIt->second) { 9937 ++FirstIt; 9938 ++SecondIt; 9939 continue; 9940 } 9941 9942 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9943 DR.SecondDecl = 9944 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9945 9946 DR.FirstDiffType = 9947 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9948 DR.SecondDiffType = 9949 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9950 return DR; 9951 } 9952 return DR; 9953 }; 9954 9955 // Use this to diagnose that an unexpected Decl was encountered 9956 // or no difference was detected. This causes a generic error 9957 // message to be emitted. 9958 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9959 StringRef FirstModule, 9960 NamedDecl *SecondRecord, 9961 StringRef SecondModule) { 9962 Diag(FirstRecord->getLocation(), 9963 diag::err_module_odr_violation_different_definitions) 9964 << FirstRecord << FirstModule.empty() << FirstModule; 9965 9966 if (DR.FirstDecl) { 9967 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9968 << FirstRecord << DR.FirstDecl->getSourceRange(); 9969 } 9970 9971 Diag(SecondRecord->getLocation(), 9972 diag::note_module_odr_violation_different_definitions) 9973 << SecondModule; 9974 9975 if (DR.SecondDecl) { 9976 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9977 << DR.SecondDecl->getSourceRange(); 9978 } 9979 }; 9980 9981 auto DiagnoseODRMismatch = 9982 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9983 NamedDecl *SecondRecord, StringRef SecondModule) { 9984 SourceLocation FirstLoc; 9985 SourceRange FirstRange; 9986 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9987 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9988 FirstLoc = FirstTag->getBraceRange().getEnd(); 9989 } else { 9990 FirstLoc = DR.FirstDecl->getLocation(); 9991 FirstRange = DR.FirstDecl->getSourceRange(); 9992 } 9993 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 9994 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 9995 << DR.FirstDiffType; 9996 9997 SourceLocation SecondLoc; 9998 SourceRange SecondRange; 9999 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 10000 if (DR.SecondDiffType == EndOfClass && SecondTag) { 10001 SecondLoc = SecondTag->getBraceRange().getEnd(); 10002 } else { 10003 SecondLoc = DR.SecondDecl->getLocation(); 10004 SecondRange = DR.SecondDecl->getSourceRange(); 10005 } 10006 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10007 << SecondModule << SecondRange << DR.SecondDiffType; 10008 }; 10009 10010 // Issue any pending ODR-failure diagnostics. 10011 for (auto &Merge : OdrMergeFailures) { 10012 // If we've already pointed out a specific problem with this class, don't 10013 // bother issuing a general "something's different" diagnostic. 10014 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10015 continue; 10016 10017 bool Diagnosed = false; 10018 CXXRecordDecl *FirstRecord = Merge.first; 10019 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10020 for (auto &RecordPair : Merge.second) { 10021 CXXRecordDecl *SecondRecord = RecordPair.first; 10022 // Multiple different declarations got merged together; tell the user 10023 // where they came from. 10024 if (FirstRecord == SecondRecord) 10025 continue; 10026 10027 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10028 10029 auto *FirstDD = FirstRecord->DefinitionData; 10030 auto *SecondDD = RecordPair.second; 10031 10032 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10033 10034 // Diagnostics from DefinitionData are emitted here. 10035 if (FirstDD != SecondDD) { 10036 enum ODRDefinitionDataDifference { 10037 NumBases, 10038 NumVBases, 10039 BaseType, 10040 BaseVirtual, 10041 BaseAccess, 10042 }; 10043 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10044 this](SourceLocation Loc, SourceRange Range, 10045 ODRDefinitionDataDifference DiffType) { 10046 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10047 << FirstRecord << FirstModule.empty() << FirstModule << Range 10048 << DiffType; 10049 }; 10050 auto ODRDiagBaseNote = [&SecondModule, 10051 this](SourceLocation Loc, SourceRange Range, 10052 ODRDefinitionDataDifference DiffType) { 10053 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10054 << SecondModule << Range << DiffType; 10055 }; 10056 10057 unsigned FirstNumBases = FirstDD->NumBases; 10058 unsigned FirstNumVBases = FirstDD->NumVBases; 10059 unsigned SecondNumBases = SecondDD->NumBases; 10060 unsigned SecondNumVBases = SecondDD->NumVBases; 10061 10062 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10063 unsigned NumBases = DD->NumBases; 10064 if (NumBases == 0) return SourceRange(); 10065 auto bases = DD->bases(); 10066 return SourceRange(bases[0].getBeginLoc(), 10067 bases[NumBases - 1].getEndLoc()); 10068 }; 10069 10070 if (FirstNumBases != SecondNumBases) { 10071 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10072 NumBases) 10073 << FirstNumBases; 10074 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10075 NumBases) 10076 << SecondNumBases; 10077 Diagnosed = true; 10078 break; 10079 } 10080 10081 if (FirstNumVBases != SecondNumVBases) { 10082 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10083 NumVBases) 10084 << FirstNumVBases; 10085 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10086 NumVBases) 10087 << SecondNumVBases; 10088 Diagnosed = true; 10089 break; 10090 } 10091 10092 auto FirstBases = FirstDD->bases(); 10093 auto SecondBases = SecondDD->bases(); 10094 unsigned i = 0; 10095 for (i = 0; i < FirstNumBases; ++i) { 10096 auto FirstBase = FirstBases[i]; 10097 auto SecondBase = SecondBases[i]; 10098 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10099 ComputeQualTypeODRHash(SecondBase.getType())) { 10100 ODRDiagBaseError(FirstRecord->getLocation(), 10101 FirstBase.getSourceRange(), BaseType) 10102 << (i + 1) << FirstBase.getType(); 10103 ODRDiagBaseNote(SecondRecord->getLocation(), 10104 SecondBase.getSourceRange(), BaseType) 10105 << (i + 1) << SecondBase.getType(); 10106 break; 10107 } 10108 10109 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10110 ODRDiagBaseError(FirstRecord->getLocation(), 10111 FirstBase.getSourceRange(), BaseVirtual) 10112 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10113 ODRDiagBaseNote(SecondRecord->getLocation(), 10114 SecondBase.getSourceRange(), BaseVirtual) 10115 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10116 break; 10117 } 10118 10119 if (FirstBase.getAccessSpecifierAsWritten() != 10120 SecondBase.getAccessSpecifierAsWritten()) { 10121 ODRDiagBaseError(FirstRecord->getLocation(), 10122 FirstBase.getSourceRange(), BaseAccess) 10123 << (i + 1) << FirstBase.getType() 10124 << (int)FirstBase.getAccessSpecifierAsWritten(); 10125 ODRDiagBaseNote(SecondRecord->getLocation(), 10126 SecondBase.getSourceRange(), BaseAccess) 10127 << (i + 1) << SecondBase.getType() 10128 << (int)SecondBase.getAccessSpecifierAsWritten(); 10129 break; 10130 } 10131 } 10132 10133 if (i != FirstNumBases) { 10134 Diagnosed = true; 10135 break; 10136 } 10137 } 10138 10139 const ClassTemplateDecl *FirstTemplate = 10140 FirstRecord->getDescribedClassTemplate(); 10141 const ClassTemplateDecl *SecondTemplate = 10142 SecondRecord->getDescribedClassTemplate(); 10143 10144 assert(!FirstTemplate == !SecondTemplate && 10145 "Both pointers should be null or non-null"); 10146 10147 enum ODRTemplateDifference { 10148 ParamEmptyName, 10149 ParamName, 10150 ParamSingleDefaultArgument, 10151 ParamDifferentDefaultArgument, 10152 }; 10153 10154 if (FirstTemplate && SecondTemplate) { 10155 DeclHashes FirstTemplateHashes; 10156 DeclHashes SecondTemplateHashes; 10157 10158 auto PopulateTemplateParameterHashs = 10159 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10160 const ClassTemplateDecl *TD) { 10161 for (auto *D : TD->getTemplateParameters()->asArray()) { 10162 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10163 } 10164 }; 10165 10166 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10167 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10168 10169 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10170 "Number of template parameters should be equal."); 10171 10172 auto FirstIt = FirstTemplateHashes.begin(); 10173 auto FirstEnd = FirstTemplateHashes.end(); 10174 auto SecondIt = SecondTemplateHashes.begin(); 10175 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10176 if (FirstIt->second == SecondIt->second) 10177 continue; 10178 10179 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10180 SourceLocation Loc, SourceRange Range, 10181 ODRTemplateDifference DiffType) { 10182 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10183 << FirstRecord << FirstModule.empty() << FirstModule << Range 10184 << DiffType; 10185 }; 10186 auto ODRDiagTemplateNote = [&SecondModule, this]( 10187 SourceLocation Loc, SourceRange Range, 10188 ODRTemplateDifference DiffType) { 10189 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10190 << SecondModule << Range << DiffType; 10191 }; 10192 10193 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10194 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10195 10196 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10197 "Parameter Decl's should be the same kind."); 10198 10199 DeclarationName FirstName = FirstDecl->getDeclName(); 10200 DeclarationName SecondName = SecondDecl->getDeclName(); 10201 10202 if (FirstName != SecondName) { 10203 const bool FirstNameEmpty = 10204 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10205 const bool SecondNameEmpty = 10206 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10207 assert((!FirstNameEmpty || !SecondNameEmpty) && 10208 "Both template parameters cannot be unnamed."); 10209 ODRDiagTemplateError(FirstDecl->getLocation(), 10210 FirstDecl->getSourceRange(), 10211 FirstNameEmpty ? ParamEmptyName : ParamName) 10212 << FirstName; 10213 ODRDiagTemplateNote(SecondDecl->getLocation(), 10214 SecondDecl->getSourceRange(), 10215 SecondNameEmpty ? ParamEmptyName : ParamName) 10216 << SecondName; 10217 break; 10218 } 10219 10220 switch (FirstDecl->getKind()) { 10221 default: 10222 llvm_unreachable("Invalid template parameter type."); 10223 case Decl::TemplateTypeParm: { 10224 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10225 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10226 const bool HasFirstDefaultArgument = 10227 FirstParam->hasDefaultArgument() && 10228 !FirstParam->defaultArgumentWasInherited(); 10229 const bool HasSecondDefaultArgument = 10230 SecondParam->hasDefaultArgument() && 10231 !SecondParam->defaultArgumentWasInherited(); 10232 10233 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10234 ODRDiagTemplateError(FirstDecl->getLocation(), 10235 FirstDecl->getSourceRange(), 10236 ParamSingleDefaultArgument) 10237 << HasFirstDefaultArgument; 10238 ODRDiagTemplateNote(SecondDecl->getLocation(), 10239 SecondDecl->getSourceRange(), 10240 ParamSingleDefaultArgument) 10241 << HasSecondDefaultArgument; 10242 break; 10243 } 10244 10245 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10246 "Expecting default arguments."); 10247 10248 ODRDiagTemplateError(FirstDecl->getLocation(), 10249 FirstDecl->getSourceRange(), 10250 ParamDifferentDefaultArgument); 10251 ODRDiagTemplateNote(SecondDecl->getLocation(), 10252 SecondDecl->getSourceRange(), 10253 ParamDifferentDefaultArgument); 10254 10255 break; 10256 } 10257 case Decl::NonTypeTemplateParm: { 10258 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10259 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10260 const bool HasFirstDefaultArgument = 10261 FirstParam->hasDefaultArgument() && 10262 !FirstParam->defaultArgumentWasInherited(); 10263 const bool HasSecondDefaultArgument = 10264 SecondParam->hasDefaultArgument() && 10265 !SecondParam->defaultArgumentWasInherited(); 10266 10267 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10268 ODRDiagTemplateError(FirstDecl->getLocation(), 10269 FirstDecl->getSourceRange(), 10270 ParamSingleDefaultArgument) 10271 << HasFirstDefaultArgument; 10272 ODRDiagTemplateNote(SecondDecl->getLocation(), 10273 SecondDecl->getSourceRange(), 10274 ParamSingleDefaultArgument) 10275 << HasSecondDefaultArgument; 10276 break; 10277 } 10278 10279 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10280 "Expecting default arguments."); 10281 10282 ODRDiagTemplateError(FirstDecl->getLocation(), 10283 FirstDecl->getSourceRange(), 10284 ParamDifferentDefaultArgument); 10285 ODRDiagTemplateNote(SecondDecl->getLocation(), 10286 SecondDecl->getSourceRange(), 10287 ParamDifferentDefaultArgument); 10288 10289 break; 10290 } 10291 case Decl::TemplateTemplateParm: { 10292 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10293 const auto *SecondParam = 10294 cast<TemplateTemplateParmDecl>(SecondDecl); 10295 const bool HasFirstDefaultArgument = 10296 FirstParam->hasDefaultArgument() && 10297 !FirstParam->defaultArgumentWasInherited(); 10298 const bool HasSecondDefaultArgument = 10299 SecondParam->hasDefaultArgument() && 10300 !SecondParam->defaultArgumentWasInherited(); 10301 10302 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10303 ODRDiagTemplateError(FirstDecl->getLocation(), 10304 FirstDecl->getSourceRange(), 10305 ParamSingleDefaultArgument) 10306 << HasFirstDefaultArgument; 10307 ODRDiagTemplateNote(SecondDecl->getLocation(), 10308 SecondDecl->getSourceRange(), 10309 ParamSingleDefaultArgument) 10310 << HasSecondDefaultArgument; 10311 break; 10312 } 10313 10314 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10315 "Expecting default arguments."); 10316 10317 ODRDiagTemplateError(FirstDecl->getLocation(), 10318 FirstDecl->getSourceRange(), 10319 ParamDifferentDefaultArgument); 10320 ODRDiagTemplateNote(SecondDecl->getLocation(), 10321 SecondDecl->getSourceRange(), 10322 ParamDifferentDefaultArgument); 10323 10324 break; 10325 } 10326 } 10327 10328 break; 10329 } 10330 10331 if (FirstIt != FirstEnd) { 10332 Diagnosed = true; 10333 break; 10334 } 10335 } 10336 10337 DeclHashes FirstHashes; 10338 DeclHashes SecondHashes; 10339 const DeclContext *DC = FirstRecord; 10340 PopulateHashes(FirstHashes, FirstRecord, DC); 10341 PopulateHashes(SecondHashes, SecondRecord, DC); 10342 10343 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10344 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10345 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10346 Decl *FirstDecl = DR.FirstDecl; 10347 Decl *SecondDecl = DR.SecondDecl; 10348 10349 if (FirstDiffType == Other || SecondDiffType == Other) { 10350 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10351 SecondModule); 10352 Diagnosed = true; 10353 break; 10354 } 10355 10356 if (FirstDiffType != SecondDiffType) { 10357 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10358 SecondModule); 10359 Diagnosed = true; 10360 break; 10361 } 10362 10363 assert(FirstDiffType == SecondDiffType); 10364 10365 switch (FirstDiffType) { 10366 case Other: 10367 case EndOfClass: 10368 case PublicSpecifer: 10369 case PrivateSpecifer: 10370 case ProtectedSpecifer: 10371 llvm_unreachable("Invalid diff type"); 10372 10373 case StaticAssert: { 10374 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10375 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10376 10377 Expr *FirstExpr = FirstSA->getAssertExpr(); 10378 Expr *SecondExpr = SecondSA->getAssertExpr(); 10379 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10380 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10381 if (FirstODRHash != SecondODRHash) { 10382 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10383 FirstExpr->getSourceRange(), StaticAssertCondition); 10384 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10385 SecondExpr->getSourceRange(), StaticAssertCondition); 10386 Diagnosed = true; 10387 break; 10388 } 10389 10390 StringLiteral *FirstStr = FirstSA->getMessage(); 10391 StringLiteral *SecondStr = SecondSA->getMessage(); 10392 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10393 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10394 SourceLocation FirstLoc, SecondLoc; 10395 SourceRange FirstRange, SecondRange; 10396 if (FirstStr) { 10397 FirstLoc = FirstStr->getBeginLoc(); 10398 FirstRange = FirstStr->getSourceRange(); 10399 } else { 10400 FirstLoc = FirstSA->getBeginLoc(); 10401 FirstRange = FirstSA->getSourceRange(); 10402 } 10403 if (SecondStr) { 10404 SecondLoc = SecondStr->getBeginLoc(); 10405 SecondRange = SecondStr->getSourceRange(); 10406 } else { 10407 SecondLoc = SecondSA->getBeginLoc(); 10408 SecondRange = SecondSA->getSourceRange(); 10409 } 10410 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10411 StaticAssertOnlyMessage) 10412 << (FirstStr == nullptr); 10413 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10414 StaticAssertOnlyMessage) 10415 << (SecondStr == nullptr); 10416 Diagnosed = true; 10417 break; 10418 } 10419 10420 if (FirstStr && SecondStr && 10421 FirstStr->getString() != SecondStr->getString()) { 10422 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10423 FirstStr->getSourceRange(), StaticAssertMessage); 10424 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10425 SecondStr->getSourceRange(), StaticAssertMessage); 10426 Diagnosed = true; 10427 break; 10428 } 10429 break; 10430 } 10431 case Field: { 10432 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10433 cast<FieldDecl>(FirstDecl), 10434 cast<FieldDecl>(SecondDecl)); 10435 break; 10436 } 10437 case CXXMethod: { 10438 enum { 10439 DiagMethod, 10440 DiagConstructor, 10441 DiagDestructor, 10442 } FirstMethodType, 10443 SecondMethodType; 10444 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10445 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10446 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10447 return DiagMethod; 10448 }; 10449 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10450 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10451 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10452 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10453 auto FirstName = FirstMethod->getDeclName(); 10454 auto SecondName = SecondMethod->getDeclName(); 10455 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10456 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10457 FirstMethod->getSourceRange(), MethodName) 10458 << FirstMethodType << FirstName; 10459 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10460 SecondMethod->getSourceRange(), MethodName) 10461 << SecondMethodType << SecondName; 10462 10463 Diagnosed = true; 10464 break; 10465 } 10466 10467 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10468 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10469 if (FirstDeleted != SecondDeleted) { 10470 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10471 FirstMethod->getSourceRange(), MethodDeleted) 10472 << FirstMethodType << FirstName << FirstDeleted; 10473 10474 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10475 SecondMethod->getSourceRange(), MethodDeleted) 10476 << SecondMethodType << SecondName << SecondDeleted; 10477 Diagnosed = true; 10478 break; 10479 } 10480 10481 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10482 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10483 if (FirstDefaulted != SecondDefaulted) { 10484 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10485 FirstMethod->getSourceRange(), MethodDefaulted) 10486 << FirstMethodType << FirstName << FirstDefaulted; 10487 10488 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10489 SecondMethod->getSourceRange(), MethodDefaulted) 10490 << SecondMethodType << SecondName << SecondDefaulted; 10491 Diagnosed = true; 10492 break; 10493 } 10494 10495 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10496 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10497 const bool FirstPure = FirstMethod->isPure(); 10498 const bool SecondPure = SecondMethod->isPure(); 10499 if ((FirstVirtual || SecondVirtual) && 10500 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10501 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10502 FirstMethod->getSourceRange(), MethodVirtual) 10503 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10504 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10505 SecondMethod->getSourceRange(), MethodVirtual) 10506 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10507 Diagnosed = true; 10508 break; 10509 } 10510 10511 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10512 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10513 // class needs to be checked instead. 10514 const auto FirstStorage = FirstMethod->getStorageClass(); 10515 const auto SecondStorage = SecondMethod->getStorageClass(); 10516 const bool FirstStatic = FirstStorage == SC_Static; 10517 const bool SecondStatic = SecondStorage == SC_Static; 10518 if (FirstStatic != SecondStatic) { 10519 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10520 FirstMethod->getSourceRange(), MethodStatic) 10521 << FirstMethodType << FirstName << FirstStatic; 10522 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10523 SecondMethod->getSourceRange(), MethodStatic) 10524 << SecondMethodType << SecondName << SecondStatic; 10525 Diagnosed = true; 10526 break; 10527 } 10528 10529 const bool FirstVolatile = FirstMethod->isVolatile(); 10530 const bool SecondVolatile = SecondMethod->isVolatile(); 10531 if (FirstVolatile != SecondVolatile) { 10532 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10533 FirstMethod->getSourceRange(), MethodVolatile) 10534 << FirstMethodType << FirstName << FirstVolatile; 10535 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10536 SecondMethod->getSourceRange(), MethodVolatile) 10537 << SecondMethodType << SecondName << SecondVolatile; 10538 Diagnosed = true; 10539 break; 10540 } 10541 10542 const bool FirstConst = FirstMethod->isConst(); 10543 const bool SecondConst = SecondMethod->isConst(); 10544 if (FirstConst != SecondConst) { 10545 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10546 FirstMethod->getSourceRange(), MethodConst) 10547 << FirstMethodType << FirstName << FirstConst; 10548 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10549 SecondMethod->getSourceRange(), MethodConst) 10550 << SecondMethodType << SecondName << SecondConst; 10551 Diagnosed = true; 10552 break; 10553 } 10554 10555 const bool FirstInline = FirstMethod->isInlineSpecified(); 10556 const bool SecondInline = SecondMethod->isInlineSpecified(); 10557 if (FirstInline != SecondInline) { 10558 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10559 FirstMethod->getSourceRange(), MethodInline) 10560 << FirstMethodType << FirstName << FirstInline; 10561 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10562 SecondMethod->getSourceRange(), MethodInline) 10563 << SecondMethodType << SecondName << SecondInline; 10564 Diagnosed = true; 10565 break; 10566 } 10567 10568 const unsigned FirstNumParameters = FirstMethod->param_size(); 10569 const unsigned SecondNumParameters = SecondMethod->param_size(); 10570 if (FirstNumParameters != SecondNumParameters) { 10571 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10572 FirstMethod->getSourceRange(), 10573 MethodNumberParameters) 10574 << FirstMethodType << FirstName << FirstNumParameters; 10575 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10576 SecondMethod->getSourceRange(), 10577 MethodNumberParameters) 10578 << SecondMethodType << SecondName << SecondNumParameters; 10579 Diagnosed = true; 10580 break; 10581 } 10582 10583 // Need this status boolean to know when break out of the switch. 10584 bool ParameterMismatch = false; 10585 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10586 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10587 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10588 10589 QualType FirstParamType = FirstParam->getType(); 10590 QualType SecondParamType = SecondParam->getType(); 10591 if (FirstParamType != SecondParamType && 10592 ComputeQualTypeODRHash(FirstParamType) != 10593 ComputeQualTypeODRHash(SecondParamType)) { 10594 if (const DecayedType *ParamDecayedType = 10595 FirstParamType->getAs<DecayedType>()) { 10596 ODRDiagDeclError( 10597 FirstRecord, FirstModule, FirstMethod->getLocation(), 10598 FirstMethod->getSourceRange(), MethodParameterType) 10599 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10600 << true << ParamDecayedType->getOriginalType(); 10601 } else { 10602 ODRDiagDeclError( 10603 FirstRecord, FirstModule, FirstMethod->getLocation(), 10604 FirstMethod->getSourceRange(), MethodParameterType) 10605 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10606 << false; 10607 } 10608 10609 if (const DecayedType *ParamDecayedType = 10610 SecondParamType->getAs<DecayedType>()) { 10611 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10612 SecondMethod->getSourceRange(), 10613 MethodParameterType) 10614 << SecondMethodType << SecondName << (I + 1) 10615 << SecondParamType << true 10616 << ParamDecayedType->getOriginalType(); 10617 } else { 10618 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10619 SecondMethod->getSourceRange(), 10620 MethodParameterType) 10621 << SecondMethodType << SecondName << (I + 1) 10622 << SecondParamType << false; 10623 } 10624 ParameterMismatch = true; 10625 break; 10626 } 10627 10628 DeclarationName FirstParamName = FirstParam->getDeclName(); 10629 DeclarationName SecondParamName = SecondParam->getDeclName(); 10630 if (FirstParamName != SecondParamName) { 10631 ODRDiagDeclError(FirstRecord, FirstModule, 10632 FirstMethod->getLocation(), 10633 FirstMethod->getSourceRange(), MethodParameterName) 10634 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10635 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10636 SecondMethod->getSourceRange(), MethodParameterName) 10637 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10638 ParameterMismatch = true; 10639 break; 10640 } 10641 10642 const Expr *FirstInit = FirstParam->getInit(); 10643 const Expr *SecondInit = SecondParam->getInit(); 10644 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10645 ODRDiagDeclError(FirstRecord, FirstModule, 10646 FirstMethod->getLocation(), 10647 FirstMethod->getSourceRange(), 10648 MethodParameterSingleDefaultArgument) 10649 << FirstMethodType << FirstName << (I + 1) 10650 << (FirstInit == nullptr) 10651 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10652 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10653 SecondMethod->getSourceRange(), 10654 MethodParameterSingleDefaultArgument) 10655 << SecondMethodType << SecondName << (I + 1) 10656 << (SecondInit == nullptr) 10657 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10658 ParameterMismatch = true; 10659 break; 10660 } 10661 10662 if (FirstInit && SecondInit && 10663 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10664 ODRDiagDeclError(FirstRecord, FirstModule, 10665 FirstMethod->getLocation(), 10666 FirstMethod->getSourceRange(), 10667 MethodParameterDifferentDefaultArgument) 10668 << FirstMethodType << FirstName << (I + 1) 10669 << FirstInit->getSourceRange(); 10670 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10671 SecondMethod->getSourceRange(), 10672 MethodParameterDifferentDefaultArgument) 10673 << SecondMethodType << SecondName << (I + 1) 10674 << SecondInit->getSourceRange(); 10675 ParameterMismatch = true; 10676 break; 10677 10678 } 10679 } 10680 10681 if (ParameterMismatch) { 10682 Diagnosed = true; 10683 break; 10684 } 10685 10686 const auto *FirstTemplateArgs = 10687 FirstMethod->getTemplateSpecializationArgs(); 10688 const auto *SecondTemplateArgs = 10689 SecondMethod->getTemplateSpecializationArgs(); 10690 10691 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10692 (!FirstTemplateArgs && SecondTemplateArgs)) { 10693 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10694 FirstMethod->getSourceRange(), 10695 MethodNoTemplateArguments) 10696 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10697 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10698 SecondMethod->getSourceRange(), 10699 MethodNoTemplateArguments) 10700 << SecondMethodType << SecondName 10701 << (SecondTemplateArgs != nullptr); 10702 10703 Diagnosed = true; 10704 break; 10705 } 10706 10707 if (FirstTemplateArgs && SecondTemplateArgs) { 10708 // Remove pack expansions from argument list. 10709 auto ExpandTemplateArgumentList = 10710 [](const TemplateArgumentList *TAL) { 10711 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10712 for (const TemplateArgument &TA : TAL->asArray()) { 10713 if (TA.getKind() != TemplateArgument::Pack) { 10714 ExpandedList.push_back(&TA); 10715 continue; 10716 } 10717 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10718 ExpandedList.push_back(&PackTA); 10719 } 10720 } 10721 return ExpandedList; 10722 }; 10723 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10724 ExpandTemplateArgumentList(FirstTemplateArgs); 10725 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10726 ExpandTemplateArgumentList(SecondTemplateArgs); 10727 10728 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10729 ODRDiagDeclError(FirstRecord, FirstModule, 10730 FirstMethod->getLocation(), 10731 FirstMethod->getSourceRange(), 10732 MethodDifferentNumberTemplateArguments) 10733 << FirstMethodType << FirstName 10734 << (unsigned)FirstExpandedList.size(); 10735 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10736 SecondMethod->getSourceRange(), 10737 MethodDifferentNumberTemplateArguments) 10738 << SecondMethodType << SecondName 10739 << (unsigned)SecondExpandedList.size(); 10740 10741 Diagnosed = true; 10742 break; 10743 } 10744 10745 bool TemplateArgumentMismatch = false; 10746 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10747 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10748 &SecondTA = *SecondExpandedList[i]; 10749 if (ComputeTemplateArgumentODRHash(FirstTA) == 10750 ComputeTemplateArgumentODRHash(SecondTA)) { 10751 continue; 10752 } 10753 10754 ODRDiagDeclError( 10755 FirstRecord, FirstModule, FirstMethod->getLocation(), 10756 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10757 << FirstMethodType << FirstName << FirstTA << i + 1; 10758 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10759 SecondMethod->getSourceRange(), 10760 MethodDifferentTemplateArgument) 10761 << SecondMethodType << SecondName << SecondTA << i + 1; 10762 10763 TemplateArgumentMismatch = true; 10764 break; 10765 } 10766 10767 if (TemplateArgumentMismatch) { 10768 Diagnosed = true; 10769 break; 10770 } 10771 } 10772 10773 // Compute the hash of the method as if it has no body. 10774 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10775 Hash.clear(); 10776 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10777 return Hash.CalculateHash(); 10778 }; 10779 10780 // Compare the hash generated to the hash stored. A difference means 10781 // that a body was present in the original source. Due to merging, 10782 // the stardard way of detecting a body will not work. 10783 const bool HasFirstBody = 10784 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10785 const bool HasSecondBody = 10786 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10787 10788 if (HasFirstBody != HasSecondBody) { 10789 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10790 FirstMethod->getSourceRange(), MethodSingleBody) 10791 << FirstMethodType << FirstName << HasFirstBody; 10792 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10793 SecondMethod->getSourceRange(), MethodSingleBody) 10794 << SecondMethodType << SecondName << HasSecondBody; 10795 Diagnosed = true; 10796 break; 10797 } 10798 10799 if (HasFirstBody && HasSecondBody) { 10800 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10801 FirstMethod->getSourceRange(), MethodDifferentBody) 10802 << FirstMethodType << FirstName; 10803 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10804 SecondMethod->getSourceRange(), MethodDifferentBody) 10805 << SecondMethodType << SecondName; 10806 Diagnosed = true; 10807 break; 10808 } 10809 10810 break; 10811 } 10812 case TypeAlias: 10813 case TypeDef: { 10814 Diagnosed = ODRDiagTypeDefOrAlias( 10815 FirstRecord, FirstModule, SecondModule, 10816 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10817 FirstDiffType == TypeAlias); 10818 break; 10819 } 10820 case Var: { 10821 Diagnosed = 10822 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10823 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10824 break; 10825 } 10826 case Friend: { 10827 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10828 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10829 10830 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10831 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10832 10833 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10834 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10835 10836 if (FirstND && SecondND) { 10837 ODRDiagDeclError(FirstRecord, FirstModule, 10838 FirstFriend->getFriendLoc(), 10839 FirstFriend->getSourceRange(), FriendFunction) 10840 << FirstND; 10841 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10842 SecondFriend->getSourceRange(), FriendFunction) 10843 << SecondND; 10844 10845 Diagnosed = true; 10846 break; 10847 } 10848 10849 if (FirstTSI && SecondTSI) { 10850 QualType FirstFriendType = FirstTSI->getType(); 10851 QualType SecondFriendType = SecondTSI->getType(); 10852 assert(ComputeQualTypeODRHash(FirstFriendType) != 10853 ComputeQualTypeODRHash(SecondFriendType)); 10854 ODRDiagDeclError(FirstRecord, FirstModule, 10855 FirstFriend->getFriendLoc(), 10856 FirstFriend->getSourceRange(), FriendType) 10857 << FirstFriendType; 10858 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10859 SecondFriend->getSourceRange(), FriendType) 10860 << SecondFriendType; 10861 Diagnosed = true; 10862 break; 10863 } 10864 10865 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10866 FirstFriend->getSourceRange(), FriendTypeFunction) 10867 << (FirstTSI == nullptr); 10868 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10869 SecondFriend->getSourceRange(), FriendTypeFunction) 10870 << (SecondTSI == nullptr); 10871 10872 Diagnosed = true; 10873 break; 10874 } 10875 case FunctionTemplate: { 10876 FunctionTemplateDecl *FirstTemplate = 10877 cast<FunctionTemplateDecl>(FirstDecl); 10878 FunctionTemplateDecl *SecondTemplate = 10879 cast<FunctionTemplateDecl>(SecondDecl); 10880 10881 TemplateParameterList *FirstTPL = 10882 FirstTemplate->getTemplateParameters(); 10883 TemplateParameterList *SecondTPL = 10884 SecondTemplate->getTemplateParameters(); 10885 10886 if (FirstTPL->size() != SecondTPL->size()) { 10887 ODRDiagDeclError(FirstRecord, FirstModule, 10888 FirstTemplate->getLocation(), 10889 FirstTemplate->getSourceRange(), 10890 FunctionTemplateDifferentNumberParameters) 10891 << FirstTemplate << FirstTPL->size(); 10892 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10893 SecondTemplate->getSourceRange(), 10894 FunctionTemplateDifferentNumberParameters) 10895 << SecondTemplate << SecondTPL->size(); 10896 10897 Diagnosed = true; 10898 break; 10899 } 10900 10901 bool ParameterMismatch = false; 10902 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10903 NamedDecl *FirstParam = FirstTPL->getParam(i); 10904 NamedDecl *SecondParam = SecondTPL->getParam(i); 10905 10906 if (FirstParam->getKind() != SecondParam->getKind()) { 10907 enum { 10908 TemplateTypeParameter, 10909 NonTypeTemplateParameter, 10910 TemplateTemplateParameter, 10911 }; 10912 auto GetParamType = [](NamedDecl *D) { 10913 switch (D->getKind()) { 10914 default: 10915 llvm_unreachable("Unexpected template parameter type"); 10916 case Decl::TemplateTypeParm: 10917 return TemplateTypeParameter; 10918 case Decl::NonTypeTemplateParm: 10919 return NonTypeTemplateParameter; 10920 case Decl::TemplateTemplateParm: 10921 return TemplateTemplateParameter; 10922 } 10923 }; 10924 10925 ODRDiagDeclError(FirstRecord, FirstModule, 10926 FirstTemplate->getLocation(), 10927 FirstTemplate->getSourceRange(), 10928 FunctionTemplateParameterDifferentKind) 10929 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10930 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10931 SecondTemplate->getSourceRange(), 10932 FunctionTemplateParameterDifferentKind) 10933 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10934 10935 ParameterMismatch = true; 10936 break; 10937 } 10938 10939 if (FirstParam->getName() != SecondParam->getName()) { 10940 ODRDiagDeclError( 10941 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10942 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10943 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10944 << FirstParam; 10945 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10946 SecondTemplate->getSourceRange(), 10947 FunctionTemplateParameterName) 10948 << SecondTemplate << (i + 1) 10949 << (bool)SecondParam->getIdentifier() << SecondParam; 10950 ParameterMismatch = true; 10951 break; 10952 } 10953 10954 if (isa<TemplateTypeParmDecl>(FirstParam) && 10955 isa<TemplateTypeParmDecl>(SecondParam)) { 10956 TemplateTypeParmDecl *FirstTTPD = 10957 cast<TemplateTypeParmDecl>(FirstParam); 10958 TemplateTypeParmDecl *SecondTTPD = 10959 cast<TemplateTypeParmDecl>(SecondParam); 10960 bool HasFirstDefaultArgument = 10961 FirstTTPD->hasDefaultArgument() && 10962 !FirstTTPD->defaultArgumentWasInherited(); 10963 bool HasSecondDefaultArgument = 10964 SecondTTPD->hasDefaultArgument() && 10965 !SecondTTPD->defaultArgumentWasInherited(); 10966 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10967 ODRDiagDeclError(FirstRecord, FirstModule, 10968 FirstTemplate->getLocation(), 10969 FirstTemplate->getSourceRange(), 10970 FunctionTemplateParameterSingleDefaultArgument) 10971 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10972 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10973 SecondTemplate->getSourceRange(), 10974 FunctionTemplateParameterSingleDefaultArgument) 10975 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10976 ParameterMismatch = true; 10977 break; 10978 } 10979 10980 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10981 QualType FirstType = FirstTTPD->getDefaultArgument(); 10982 QualType SecondType = SecondTTPD->getDefaultArgument(); 10983 if (ComputeQualTypeODRHash(FirstType) != 10984 ComputeQualTypeODRHash(SecondType)) { 10985 ODRDiagDeclError( 10986 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10987 FirstTemplate->getSourceRange(), 10988 FunctionTemplateParameterDifferentDefaultArgument) 10989 << FirstTemplate << (i + 1) << FirstType; 10990 ODRDiagDeclNote( 10991 SecondModule, SecondTemplate->getLocation(), 10992 SecondTemplate->getSourceRange(), 10993 FunctionTemplateParameterDifferentDefaultArgument) 10994 << SecondTemplate << (i + 1) << SecondType; 10995 ParameterMismatch = true; 10996 break; 10997 } 10998 } 10999 11000 if (FirstTTPD->isParameterPack() != 11001 SecondTTPD->isParameterPack()) { 11002 ODRDiagDeclError(FirstRecord, FirstModule, 11003 FirstTemplate->getLocation(), 11004 FirstTemplate->getSourceRange(), 11005 FunctionTemplatePackParameter) 11006 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11007 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11008 SecondTemplate->getSourceRange(), 11009 FunctionTemplatePackParameter) 11010 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11011 ParameterMismatch = true; 11012 break; 11013 } 11014 } 11015 11016 if (isa<TemplateTemplateParmDecl>(FirstParam) && 11017 isa<TemplateTemplateParmDecl>(SecondParam)) { 11018 TemplateTemplateParmDecl *FirstTTPD = 11019 cast<TemplateTemplateParmDecl>(FirstParam); 11020 TemplateTemplateParmDecl *SecondTTPD = 11021 cast<TemplateTemplateParmDecl>(SecondParam); 11022 11023 TemplateParameterList *FirstTPL = 11024 FirstTTPD->getTemplateParameters(); 11025 TemplateParameterList *SecondTPL = 11026 SecondTTPD->getTemplateParameters(); 11027 11028 if (ComputeTemplateParameterListODRHash(FirstTPL) != 11029 ComputeTemplateParameterListODRHash(SecondTPL)) { 11030 ODRDiagDeclError(FirstRecord, FirstModule, 11031 FirstTemplate->getLocation(), 11032 FirstTemplate->getSourceRange(), 11033 FunctionTemplateParameterDifferentType) 11034 << FirstTemplate << (i + 1); 11035 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11036 SecondTemplate->getSourceRange(), 11037 FunctionTemplateParameterDifferentType) 11038 << SecondTemplate << (i + 1); 11039 ParameterMismatch = true; 11040 break; 11041 } 11042 11043 bool HasFirstDefaultArgument = 11044 FirstTTPD->hasDefaultArgument() && 11045 !FirstTTPD->defaultArgumentWasInherited(); 11046 bool HasSecondDefaultArgument = 11047 SecondTTPD->hasDefaultArgument() && 11048 !SecondTTPD->defaultArgumentWasInherited(); 11049 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11050 ODRDiagDeclError(FirstRecord, FirstModule, 11051 FirstTemplate->getLocation(), 11052 FirstTemplate->getSourceRange(), 11053 FunctionTemplateParameterSingleDefaultArgument) 11054 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11055 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11056 SecondTemplate->getSourceRange(), 11057 FunctionTemplateParameterSingleDefaultArgument) 11058 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11059 ParameterMismatch = true; 11060 break; 11061 } 11062 11063 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11064 TemplateArgument FirstTA = 11065 FirstTTPD->getDefaultArgument().getArgument(); 11066 TemplateArgument SecondTA = 11067 SecondTTPD->getDefaultArgument().getArgument(); 11068 if (ComputeTemplateArgumentODRHash(FirstTA) != 11069 ComputeTemplateArgumentODRHash(SecondTA)) { 11070 ODRDiagDeclError( 11071 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11072 FirstTemplate->getSourceRange(), 11073 FunctionTemplateParameterDifferentDefaultArgument) 11074 << FirstTemplate << (i + 1) << FirstTA; 11075 ODRDiagDeclNote( 11076 SecondModule, SecondTemplate->getLocation(), 11077 SecondTemplate->getSourceRange(), 11078 FunctionTemplateParameterDifferentDefaultArgument) 11079 << SecondTemplate << (i + 1) << SecondTA; 11080 ParameterMismatch = true; 11081 break; 11082 } 11083 } 11084 11085 if (FirstTTPD->isParameterPack() != 11086 SecondTTPD->isParameterPack()) { 11087 ODRDiagDeclError(FirstRecord, FirstModule, 11088 FirstTemplate->getLocation(), 11089 FirstTemplate->getSourceRange(), 11090 FunctionTemplatePackParameter) 11091 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11092 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11093 SecondTemplate->getSourceRange(), 11094 FunctionTemplatePackParameter) 11095 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11096 ParameterMismatch = true; 11097 break; 11098 } 11099 } 11100 11101 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11102 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11103 NonTypeTemplateParmDecl *FirstNTTPD = 11104 cast<NonTypeTemplateParmDecl>(FirstParam); 11105 NonTypeTemplateParmDecl *SecondNTTPD = 11106 cast<NonTypeTemplateParmDecl>(SecondParam); 11107 11108 QualType FirstType = FirstNTTPD->getType(); 11109 QualType SecondType = SecondNTTPD->getType(); 11110 if (ComputeQualTypeODRHash(FirstType) != 11111 ComputeQualTypeODRHash(SecondType)) { 11112 ODRDiagDeclError(FirstRecord, FirstModule, 11113 FirstTemplate->getLocation(), 11114 FirstTemplate->getSourceRange(), 11115 FunctionTemplateParameterDifferentType) 11116 << FirstTemplate << (i + 1); 11117 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11118 SecondTemplate->getSourceRange(), 11119 FunctionTemplateParameterDifferentType) 11120 << SecondTemplate << (i + 1); 11121 ParameterMismatch = true; 11122 break; 11123 } 11124 11125 bool HasFirstDefaultArgument = 11126 FirstNTTPD->hasDefaultArgument() && 11127 !FirstNTTPD->defaultArgumentWasInherited(); 11128 bool HasSecondDefaultArgument = 11129 SecondNTTPD->hasDefaultArgument() && 11130 !SecondNTTPD->defaultArgumentWasInherited(); 11131 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11132 ODRDiagDeclError(FirstRecord, FirstModule, 11133 FirstTemplate->getLocation(), 11134 FirstTemplate->getSourceRange(), 11135 FunctionTemplateParameterSingleDefaultArgument) 11136 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11137 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11138 SecondTemplate->getSourceRange(), 11139 FunctionTemplateParameterSingleDefaultArgument) 11140 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11141 ParameterMismatch = true; 11142 break; 11143 } 11144 11145 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11146 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11147 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11148 if (ComputeODRHash(FirstDefaultArgument) != 11149 ComputeODRHash(SecondDefaultArgument)) { 11150 ODRDiagDeclError( 11151 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11152 FirstTemplate->getSourceRange(), 11153 FunctionTemplateParameterDifferentDefaultArgument) 11154 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11155 ODRDiagDeclNote( 11156 SecondModule, SecondTemplate->getLocation(), 11157 SecondTemplate->getSourceRange(), 11158 FunctionTemplateParameterDifferentDefaultArgument) 11159 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11160 ParameterMismatch = true; 11161 break; 11162 } 11163 } 11164 11165 if (FirstNTTPD->isParameterPack() != 11166 SecondNTTPD->isParameterPack()) { 11167 ODRDiagDeclError(FirstRecord, FirstModule, 11168 FirstTemplate->getLocation(), 11169 FirstTemplate->getSourceRange(), 11170 FunctionTemplatePackParameter) 11171 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11172 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11173 SecondTemplate->getSourceRange(), 11174 FunctionTemplatePackParameter) 11175 << SecondTemplate << (i + 1) 11176 << SecondNTTPD->isParameterPack(); 11177 ParameterMismatch = true; 11178 break; 11179 } 11180 } 11181 } 11182 11183 if (ParameterMismatch) { 11184 Diagnosed = true; 11185 break; 11186 } 11187 11188 break; 11189 } 11190 } 11191 11192 if (Diagnosed) 11193 continue; 11194 11195 Diag(FirstDecl->getLocation(), 11196 diag::err_module_odr_violation_mismatch_decl_unknown) 11197 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11198 << FirstDecl->getSourceRange(); 11199 Diag(SecondDecl->getLocation(), 11200 diag::note_module_odr_violation_mismatch_decl_unknown) 11201 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11202 Diagnosed = true; 11203 } 11204 11205 if (!Diagnosed) { 11206 // All definitions are updates to the same declaration. This happens if a 11207 // module instantiates the declaration of a class template specialization 11208 // and two or more other modules instantiate its definition. 11209 // 11210 // FIXME: Indicate which modules had instantiations of this definition. 11211 // FIXME: How can this even happen? 11212 Diag(Merge.first->getLocation(), 11213 diag::err_module_odr_violation_different_instantiations) 11214 << Merge.first; 11215 } 11216 } 11217 11218 // Issue ODR failures diagnostics for functions. 11219 for (auto &Merge : FunctionOdrMergeFailures) { 11220 enum ODRFunctionDifference { 11221 ReturnType, 11222 ParameterName, 11223 ParameterType, 11224 ParameterSingleDefaultArgument, 11225 ParameterDifferentDefaultArgument, 11226 FunctionBody, 11227 }; 11228 11229 FunctionDecl *FirstFunction = Merge.first; 11230 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11231 11232 bool Diagnosed = false; 11233 for (auto &SecondFunction : Merge.second) { 11234 11235 if (FirstFunction == SecondFunction) 11236 continue; 11237 11238 std::string SecondModule = 11239 getOwningModuleNameForDiagnostic(SecondFunction); 11240 11241 auto ODRDiagError = [FirstFunction, &FirstModule, 11242 this](SourceLocation Loc, SourceRange Range, 11243 ODRFunctionDifference DiffType) { 11244 return Diag(Loc, diag::err_module_odr_violation_function) 11245 << FirstFunction << FirstModule.empty() << FirstModule << Range 11246 << DiffType; 11247 }; 11248 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11249 SourceRange Range, 11250 ODRFunctionDifference DiffType) { 11251 return Diag(Loc, diag::note_module_odr_violation_function) 11252 << SecondModule << Range << DiffType; 11253 }; 11254 11255 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11256 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11257 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11258 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11259 << FirstFunction->getReturnType(); 11260 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11261 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11262 << SecondFunction->getReturnType(); 11263 Diagnosed = true; 11264 break; 11265 } 11266 11267 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11268 "Merged functions with different number of parameters"); 11269 11270 auto ParamSize = FirstFunction->param_size(); 11271 bool ParameterMismatch = false; 11272 for (unsigned I = 0; I < ParamSize; ++I) { 11273 auto *FirstParam = FirstFunction->getParamDecl(I); 11274 auto *SecondParam = SecondFunction->getParamDecl(I); 11275 11276 assert(getContext().hasSameType(FirstParam->getType(), 11277 SecondParam->getType()) && 11278 "Merged function has different parameter types."); 11279 11280 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11281 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11282 ParameterName) 11283 << I + 1 << FirstParam->getDeclName(); 11284 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11285 ParameterName) 11286 << I + 1 << SecondParam->getDeclName(); 11287 ParameterMismatch = true; 11288 break; 11289 }; 11290 11291 QualType FirstParamType = FirstParam->getType(); 11292 QualType SecondParamType = SecondParam->getType(); 11293 if (FirstParamType != SecondParamType && 11294 ComputeQualTypeODRHash(FirstParamType) != 11295 ComputeQualTypeODRHash(SecondParamType)) { 11296 if (const DecayedType *ParamDecayedType = 11297 FirstParamType->getAs<DecayedType>()) { 11298 ODRDiagError(FirstParam->getLocation(), 11299 FirstParam->getSourceRange(), ParameterType) 11300 << (I + 1) << FirstParamType << true 11301 << ParamDecayedType->getOriginalType(); 11302 } else { 11303 ODRDiagError(FirstParam->getLocation(), 11304 FirstParam->getSourceRange(), ParameterType) 11305 << (I + 1) << FirstParamType << false; 11306 } 11307 11308 if (const DecayedType *ParamDecayedType = 11309 SecondParamType->getAs<DecayedType>()) { 11310 ODRDiagNote(SecondParam->getLocation(), 11311 SecondParam->getSourceRange(), ParameterType) 11312 << (I + 1) << SecondParamType << true 11313 << ParamDecayedType->getOriginalType(); 11314 } else { 11315 ODRDiagNote(SecondParam->getLocation(), 11316 SecondParam->getSourceRange(), ParameterType) 11317 << (I + 1) << SecondParamType << false; 11318 } 11319 ParameterMismatch = true; 11320 break; 11321 } 11322 11323 const Expr *FirstInit = FirstParam->getInit(); 11324 const Expr *SecondInit = SecondParam->getInit(); 11325 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11326 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11327 ParameterSingleDefaultArgument) 11328 << (I + 1) << (FirstInit == nullptr) 11329 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11330 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11331 ParameterSingleDefaultArgument) 11332 << (I + 1) << (SecondInit == nullptr) 11333 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11334 ParameterMismatch = true; 11335 break; 11336 } 11337 11338 if (FirstInit && SecondInit && 11339 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11340 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11341 ParameterDifferentDefaultArgument) 11342 << (I + 1) << FirstInit->getSourceRange(); 11343 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11344 ParameterDifferentDefaultArgument) 11345 << (I + 1) << SecondInit->getSourceRange(); 11346 ParameterMismatch = true; 11347 break; 11348 } 11349 11350 assert(ComputeSubDeclODRHash(FirstParam) == 11351 ComputeSubDeclODRHash(SecondParam) && 11352 "Undiagnosed parameter difference."); 11353 } 11354 11355 if (ParameterMismatch) { 11356 Diagnosed = true; 11357 break; 11358 } 11359 11360 // If no error has been generated before now, assume the problem is in 11361 // the body and generate a message. 11362 ODRDiagError(FirstFunction->getLocation(), 11363 FirstFunction->getSourceRange(), FunctionBody); 11364 ODRDiagNote(SecondFunction->getLocation(), 11365 SecondFunction->getSourceRange(), FunctionBody); 11366 Diagnosed = true; 11367 break; 11368 } 11369 (void)Diagnosed; 11370 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11371 } 11372 11373 // Issue ODR failures diagnostics for enums. 11374 for (auto &Merge : EnumOdrMergeFailures) { 11375 enum ODREnumDifference { 11376 SingleScopedEnum, 11377 EnumTagKeywordMismatch, 11378 SingleSpecifiedType, 11379 DifferentSpecifiedTypes, 11380 DifferentNumberEnumConstants, 11381 EnumConstantName, 11382 EnumConstantSingleInitilizer, 11383 EnumConstantDifferentInitilizer, 11384 }; 11385 11386 // If we've already pointed out a specific problem with this enum, don't 11387 // bother issuing a general "something's different" diagnostic. 11388 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11389 continue; 11390 11391 EnumDecl *FirstEnum = Merge.first; 11392 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11393 11394 using DeclHashes = 11395 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11396 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11397 DeclHashes &Hashes, EnumDecl *Enum) { 11398 for (auto *D : Enum->decls()) { 11399 // Due to decl merging, the first EnumDecl is the parent of 11400 // Decls in both records. 11401 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11402 continue; 11403 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11404 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11405 ComputeSubDeclODRHash(D)); 11406 } 11407 }; 11408 DeclHashes FirstHashes; 11409 PopulateHashes(FirstHashes, FirstEnum); 11410 bool Diagnosed = false; 11411 for (auto &SecondEnum : Merge.second) { 11412 11413 if (FirstEnum == SecondEnum) 11414 continue; 11415 11416 std::string SecondModule = 11417 getOwningModuleNameForDiagnostic(SecondEnum); 11418 11419 auto ODRDiagError = [FirstEnum, &FirstModule, 11420 this](SourceLocation Loc, SourceRange Range, 11421 ODREnumDifference DiffType) { 11422 return Diag(Loc, diag::err_module_odr_violation_enum) 11423 << FirstEnum << FirstModule.empty() << FirstModule << Range 11424 << DiffType; 11425 }; 11426 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11427 SourceRange Range, 11428 ODREnumDifference DiffType) { 11429 return Diag(Loc, diag::note_module_odr_violation_enum) 11430 << SecondModule << Range << DiffType; 11431 }; 11432 11433 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11434 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11435 SingleScopedEnum) 11436 << FirstEnum->isScoped(); 11437 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11438 SingleScopedEnum) 11439 << SecondEnum->isScoped(); 11440 Diagnosed = true; 11441 continue; 11442 } 11443 11444 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11445 if (FirstEnum->isScopedUsingClassTag() != 11446 SecondEnum->isScopedUsingClassTag()) { 11447 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11448 EnumTagKeywordMismatch) 11449 << FirstEnum->isScopedUsingClassTag(); 11450 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11451 EnumTagKeywordMismatch) 11452 << SecondEnum->isScopedUsingClassTag(); 11453 Diagnosed = true; 11454 continue; 11455 } 11456 } 11457 11458 QualType FirstUnderlyingType = 11459 FirstEnum->getIntegerTypeSourceInfo() 11460 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11461 : QualType(); 11462 QualType SecondUnderlyingType = 11463 SecondEnum->getIntegerTypeSourceInfo() 11464 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11465 : QualType(); 11466 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11467 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11468 SingleSpecifiedType) 11469 << !FirstUnderlyingType.isNull(); 11470 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11471 SingleSpecifiedType) 11472 << !SecondUnderlyingType.isNull(); 11473 Diagnosed = true; 11474 continue; 11475 } 11476 11477 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11478 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11479 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11480 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11481 DifferentSpecifiedTypes) 11482 << FirstUnderlyingType; 11483 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11484 DifferentSpecifiedTypes) 11485 << SecondUnderlyingType; 11486 Diagnosed = true; 11487 continue; 11488 } 11489 } 11490 11491 DeclHashes SecondHashes; 11492 PopulateHashes(SecondHashes, SecondEnum); 11493 11494 if (FirstHashes.size() != SecondHashes.size()) { 11495 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11496 DifferentNumberEnumConstants) 11497 << (int)FirstHashes.size(); 11498 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11499 DifferentNumberEnumConstants) 11500 << (int)SecondHashes.size(); 11501 Diagnosed = true; 11502 continue; 11503 } 11504 11505 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11506 if (FirstHashes[I].second == SecondHashes[I].second) 11507 continue; 11508 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11509 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11510 11511 if (FirstEnumConstant->getDeclName() != 11512 SecondEnumConstant->getDeclName()) { 11513 11514 ODRDiagError(FirstEnumConstant->getLocation(), 11515 FirstEnumConstant->getSourceRange(), EnumConstantName) 11516 << I + 1 << FirstEnumConstant; 11517 ODRDiagNote(SecondEnumConstant->getLocation(), 11518 SecondEnumConstant->getSourceRange(), EnumConstantName) 11519 << I + 1 << SecondEnumConstant; 11520 Diagnosed = true; 11521 break; 11522 } 11523 11524 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11525 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11526 if (!FirstInit && !SecondInit) 11527 continue; 11528 11529 if (!FirstInit || !SecondInit) { 11530 ODRDiagError(FirstEnumConstant->getLocation(), 11531 FirstEnumConstant->getSourceRange(), 11532 EnumConstantSingleInitilizer) 11533 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11534 ODRDiagNote(SecondEnumConstant->getLocation(), 11535 SecondEnumConstant->getSourceRange(), 11536 EnumConstantSingleInitilizer) 11537 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11538 Diagnosed = true; 11539 break; 11540 } 11541 11542 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11543 ODRDiagError(FirstEnumConstant->getLocation(), 11544 FirstEnumConstant->getSourceRange(), 11545 EnumConstantDifferentInitilizer) 11546 << I + 1 << FirstEnumConstant; 11547 ODRDiagNote(SecondEnumConstant->getLocation(), 11548 SecondEnumConstant->getSourceRange(), 11549 EnumConstantDifferentInitilizer) 11550 << I + 1 << SecondEnumConstant; 11551 Diagnosed = true; 11552 break; 11553 } 11554 } 11555 } 11556 11557 (void)Diagnosed; 11558 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11559 } 11560 } 11561 11562 void ASTReader::StartedDeserializing() { 11563 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11564 ReadTimer->startTimer(); 11565 } 11566 11567 void ASTReader::FinishedDeserializing() { 11568 assert(NumCurrentElementsDeserializing && 11569 "FinishedDeserializing not paired with StartedDeserializing"); 11570 if (NumCurrentElementsDeserializing == 1) { 11571 // We decrease NumCurrentElementsDeserializing only after pending actions 11572 // are finished, to avoid recursively re-calling finishPendingActions(). 11573 finishPendingActions(); 11574 } 11575 --NumCurrentElementsDeserializing; 11576 11577 if (NumCurrentElementsDeserializing == 0) { 11578 // Propagate exception specification and deduced type updates along 11579 // redeclaration chains. 11580 // 11581 // We do this now rather than in finishPendingActions because we want to 11582 // be able to walk the complete redeclaration chains of the updated decls. 11583 while (!PendingExceptionSpecUpdates.empty() || 11584 !PendingDeducedTypeUpdates.empty()) { 11585 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11586 PendingExceptionSpecUpdates.clear(); 11587 for (auto Update : ESUpdates) { 11588 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11589 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11590 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11591 if (auto *Listener = getContext().getASTMutationListener()) 11592 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11593 for (auto *Redecl : Update.second->redecls()) 11594 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11595 } 11596 11597 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11598 PendingDeducedTypeUpdates.clear(); 11599 for (auto Update : DTUpdates) { 11600 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11601 // FIXME: If the return type is already deduced, check that it matches. 11602 getContext().adjustDeducedFunctionResultType(Update.first, 11603 Update.second); 11604 } 11605 } 11606 11607 if (ReadTimer) 11608 ReadTimer->stopTimer(); 11609 11610 diagnoseOdrViolations(); 11611 11612 // We are not in recursive loading, so it's safe to pass the "interesting" 11613 // decls to the consumer. 11614 if (Consumer) 11615 PassInterestingDeclsToConsumer(); 11616 } 11617 } 11618 11619 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11620 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11621 // Remove any fake results before adding any real ones. 11622 auto It = PendingFakeLookupResults.find(II); 11623 if (It != PendingFakeLookupResults.end()) { 11624 for (auto *ND : It->second) 11625 SemaObj->IdResolver.RemoveDecl(ND); 11626 // FIXME: this works around module+PCH performance issue. 11627 // Rather than erase the result from the map, which is O(n), just clear 11628 // the vector of NamedDecls. 11629 It->second.clear(); 11630 } 11631 } 11632 11633 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11634 SemaObj->TUScope->AddDecl(D); 11635 } else if (SemaObj->TUScope) { 11636 // Adding the decl to IdResolver may have failed because it was already in 11637 // (even though it was not added in scope). If it is already in, make sure 11638 // it gets in the scope as well. 11639 if (std::find(SemaObj->IdResolver.begin(Name), 11640 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11641 SemaObj->TUScope->AddDecl(D); 11642 } 11643 } 11644 11645 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11646 ASTContext *Context, 11647 const PCHContainerReader &PCHContainerRdr, 11648 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11649 StringRef isysroot, 11650 DisableValidationForModuleKind DisableValidationKind, 11651 bool AllowASTWithCompilerErrors, 11652 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11653 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11654 std::unique_ptr<llvm::Timer> ReadTimer) 11655 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH) 11656 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11657 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11658 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11659 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11660 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11661 PCHContainerRdr, PP.getHeaderSearchInfo()), 11662 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11663 DisableValidationKind(DisableValidationKind), 11664 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11665 AllowConfigurationMismatch(AllowConfigurationMismatch), 11666 ValidateSystemInputs(ValidateSystemInputs), 11667 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11668 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11669 SourceMgr.setExternalSLocEntrySource(this); 11670 11671 for (const auto &Ext : Extensions) { 11672 auto BlockName = Ext->getExtensionMetadata().BlockName; 11673 auto Known = ModuleFileExtensions.find(BlockName); 11674 if (Known != ModuleFileExtensions.end()) { 11675 Diags.Report(diag::warn_duplicate_module_file_extension) 11676 << BlockName; 11677 continue; 11678 } 11679 11680 ModuleFileExtensions.insert({BlockName, Ext}); 11681 } 11682 } 11683 11684 ASTReader::~ASTReader() { 11685 if (OwnsDeserializationListener) 11686 delete DeserializationListener; 11687 } 11688 11689 IdentifierResolver &ASTReader::getIdResolver() { 11690 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11691 } 11692 11693 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11694 unsigned AbbrevID) { 11695 Idx = 0; 11696 Record.clear(); 11697 return Cursor.readRecord(AbbrevID, Record); 11698 } 11699 //===----------------------------------------------------------------------===// 11700 //// OMPClauseReader implementation 11701 ////===----------------------------------------------------------------------===// 11702 11703 // This has to be in namespace clang because it's friended by all 11704 // of the OMP clauses. 11705 namespace clang { 11706 11707 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11708 ASTRecordReader &Record; 11709 ASTContext &Context; 11710 11711 public: 11712 OMPClauseReader(ASTRecordReader &Record) 11713 : Record(Record), Context(Record.getContext()) {} 11714 #define GEN_CLANG_CLAUSE_CLASS 11715 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11716 #include "llvm/Frontend/OpenMP/OMP.inc" 11717 OMPClause *readClause(); 11718 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11719 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11720 }; 11721 11722 } // end namespace clang 11723 11724 OMPClause *ASTRecordReader::readOMPClause() { 11725 return OMPClauseReader(*this).readClause(); 11726 } 11727 11728 OMPClause *OMPClauseReader::readClause() { 11729 OMPClause *C = nullptr; 11730 switch (llvm::omp::Clause(Record.readInt())) { 11731 case llvm::omp::OMPC_if: 11732 C = new (Context) OMPIfClause(); 11733 break; 11734 case llvm::omp::OMPC_final: 11735 C = new (Context) OMPFinalClause(); 11736 break; 11737 case llvm::omp::OMPC_num_threads: 11738 C = new (Context) OMPNumThreadsClause(); 11739 break; 11740 case llvm::omp::OMPC_safelen: 11741 C = new (Context) OMPSafelenClause(); 11742 break; 11743 case llvm::omp::OMPC_simdlen: 11744 C = new (Context) OMPSimdlenClause(); 11745 break; 11746 case llvm::omp::OMPC_sizes: { 11747 unsigned NumSizes = Record.readInt(); 11748 C = OMPSizesClause::CreateEmpty(Context, NumSizes); 11749 break; 11750 } 11751 case llvm::omp::OMPC_allocator: 11752 C = new (Context) OMPAllocatorClause(); 11753 break; 11754 case llvm::omp::OMPC_collapse: 11755 C = new (Context) OMPCollapseClause(); 11756 break; 11757 case llvm::omp::OMPC_default: 11758 C = new (Context) OMPDefaultClause(); 11759 break; 11760 case llvm::omp::OMPC_proc_bind: 11761 C = new (Context) OMPProcBindClause(); 11762 break; 11763 case llvm::omp::OMPC_schedule: 11764 C = new (Context) OMPScheduleClause(); 11765 break; 11766 case llvm::omp::OMPC_ordered: 11767 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11768 break; 11769 case llvm::omp::OMPC_nowait: 11770 C = new (Context) OMPNowaitClause(); 11771 break; 11772 case llvm::omp::OMPC_untied: 11773 C = new (Context) OMPUntiedClause(); 11774 break; 11775 case llvm::omp::OMPC_mergeable: 11776 C = new (Context) OMPMergeableClause(); 11777 break; 11778 case llvm::omp::OMPC_read: 11779 C = new (Context) OMPReadClause(); 11780 break; 11781 case llvm::omp::OMPC_write: 11782 C = new (Context) OMPWriteClause(); 11783 break; 11784 case llvm::omp::OMPC_update: 11785 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11786 break; 11787 case llvm::omp::OMPC_capture: 11788 C = new (Context) OMPCaptureClause(); 11789 break; 11790 case llvm::omp::OMPC_seq_cst: 11791 C = new (Context) OMPSeqCstClause(); 11792 break; 11793 case llvm::omp::OMPC_acq_rel: 11794 C = new (Context) OMPAcqRelClause(); 11795 break; 11796 case llvm::omp::OMPC_acquire: 11797 C = new (Context) OMPAcquireClause(); 11798 break; 11799 case llvm::omp::OMPC_release: 11800 C = new (Context) OMPReleaseClause(); 11801 break; 11802 case llvm::omp::OMPC_relaxed: 11803 C = new (Context) OMPRelaxedClause(); 11804 break; 11805 case llvm::omp::OMPC_threads: 11806 C = new (Context) OMPThreadsClause(); 11807 break; 11808 case llvm::omp::OMPC_simd: 11809 C = new (Context) OMPSIMDClause(); 11810 break; 11811 case llvm::omp::OMPC_nogroup: 11812 C = new (Context) OMPNogroupClause(); 11813 break; 11814 case llvm::omp::OMPC_unified_address: 11815 C = new (Context) OMPUnifiedAddressClause(); 11816 break; 11817 case llvm::omp::OMPC_unified_shared_memory: 11818 C = new (Context) OMPUnifiedSharedMemoryClause(); 11819 break; 11820 case llvm::omp::OMPC_reverse_offload: 11821 C = new (Context) OMPReverseOffloadClause(); 11822 break; 11823 case llvm::omp::OMPC_dynamic_allocators: 11824 C = new (Context) OMPDynamicAllocatorsClause(); 11825 break; 11826 case llvm::omp::OMPC_atomic_default_mem_order: 11827 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11828 break; 11829 case llvm::omp::OMPC_private: 11830 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11831 break; 11832 case llvm::omp::OMPC_firstprivate: 11833 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11834 break; 11835 case llvm::omp::OMPC_lastprivate: 11836 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11837 break; 11838 case llvm::omp::OMPC_shared: 11839 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11840 break; 11841 case llvm::omp::OMPC_reduction: { 11842 unsigned N = Record.readInt(); 11843 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11844 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11845 break; 11846 } 11847 case llvm::omp::OMPC_task_reduction: 11848 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11849 break; 11850 case llvm::omp::OMPC_in_reduction: 11851 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11852 break; 11853 case llvm::omp::OMPC_linear: 11854 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11855 break; 11856 case llvm::omp::OMPC_aligned: 11857 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11858 break; 11859 case llvm::omp::OMPC_copyin: 11860 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11861 break; 11862 case llvm::omp::OMPC_copyprivate: 11863 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11864 break; 11865 case llvm::omp::OMPC_flush: 11866 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11867 break; 11868 case llvm::omp::OMPC_depobj: 11869 C = OMPDepobjClause::CreateEmpty(Context); 11870 break; 11871 case llvm::omp::OMPC_depend: { 11872 unsigned NumVars = Record.readInt(); 11873 unsigned NumLoops = Record.readInt(); 11874 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11875 break; 11876 } 11877 case llvm::omp::OMPC_device: 11878 C = new (Context) OMPDeviceClause(); 11879 break; 11880 case llvm::omp::OMPC_map: { 11881 OMPMappableExprListSizeTy Sizes; 11882 Sizes.NumVars = Record.readInt(); 11883 Sizes.NumUniqueDeclarations = Record.readInt(); 11884 Sizes.NumComponentLists = Record.readInt(); 11885 Sizes.NumComponents = Record.readInt(); 11886 C = OMPMapClause::CreateEmpty(Context, Sizes); 11887 break; 11888 } 11889 case llvm::omp::OMPC_num_teams: 11890 C = new (Context) OMPNumTeamsClause(); 11891 break; 11892 case llvm::omp::OMPC_thread_limit: 11893 C = new (Context) OMPThreadLimitClause(); 11894 break; 11895 case llvm::omp::OMPC_priority: 11896 C = new (Context) OMPPriorityClause(); 11897 break; 11898 case llvm::omp::OMPC_grainsize: 11899 C = new (Context) OMPGrainsizeClause(); 11900 break; 11901 case llvm::omp::OMPC_num_tasks: 11902 C = new (Context) OMPNumTasksClause(); 11903 break; 11904 case llvm::omp::OMPC_hint: 11905 C = new (Context) OMPHintClause(); 11906 break; 11907 case llvm::omp::OMPC_dist_schedule: 11908 C = new (Context) OMPDistScheduleClause(); 11909 break; 11910 case llvm::omp::OMPC_defaultmap: 11911 C = new (Context) OMPDefaultmapClause(); 11912 break; 11913 case llvm::omp::OMPC_to: { 11914 OMPMappableExprListSizeTy Sizes; 11915 Sizes.NumVars = Record.readInt(); 11916 Sizes.NumUniqueDeclarations = Record.readInt(); 11917 Sizes.NumComponentLists = Record.readInt(); 11918 Sizes.NumComponents = Record.readInt(); 11919 C = OMPToClause::CreateEmpty(Context, Sizes); 11920 break; 11921 } 11922 case llvm::omp::OMPC_from: { 11923 OMPMappableExprListSizeTy Sizes; 11924 Sizes.NumVars = Record.readInt(); 11925 Sizes.NumUniqueDeclarations = Record.readInt(); 11926 Sizes.NumComponentLists = Record.readInt(); 11927 Sizes.NumComponents = Record.readInt(); 11928 C = OMPFromClause::CreateEmpty(Context, Sizes); 11929 break; 11930 } 11931 case llvm::omp::OMPC_use_device_ptr: { 11932 OMPMappableExprListSizeTy Sizes; 11933 Sizes.NumVars = Record.readInt(); 11934 Sizes.NumUniqueDeclarations = Record.readInt(); 11935 Sizes.NumComponentLists = Record.readInt(); 11936 Sizes.NumComponents = Record.readInt(); 11937 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11938 break; 11939 } 11940 case llvm::omp::OMPC_use_device_addr: { 11941 OMPMappableExprListSizeTy Sizes; 11942 Sizes.NumVars = Record.readInt(); 11943 Sizes.NumUniqueDeclarations = Record.readInt(); 11944 Sizes.NumComponentLists = Record.readInt(); 11945 Sizes.NumComponents = Record.readInt(); 11946 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11947 break; 11948 } 11949 case llvm::omp::OMPC_is_device_ptr: { 11950 OMPMappableExprListSizeTy Sizes; 11951 Sizes.NumVars = Record.readInt(); 11952 Sizes.NumUniqueDeclarations = Record.readInt(); 11953 Sizes.NumComponentLists = Record.readInt(); 11954 Sizes.NumComponents = Record.readInt(); 11955 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11956 break; 11957 } 11958 case llvm::omp::OMPC_allocate: 11959 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11960 break; 11961 case llvm::omp::OMPC_nontemporal: 11962 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11963 break; 11964 case llvm::omp::OMPC_inclusive: 11965 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11966 break; 11967 case llvm::omp::OMPC_exclusive: 11968 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11969 break; 11970 case llvm::omp::OMPC_order: 11971 C = new (Context) OMPOrderClause(); 11972 break; 11973 case llvm::omp::OMPC_init: 11974 C = OMPInitClause::CreateEmpty(Context, Record.readInt()); 11975 break; 11976 case llvm::omp::OMPC_use: 11977 C = new (Context) OMPUseClause(); 11978 break; 11979 case llvm::omp::OMPC_destroy: 11980 C = new (Context) OMPDestroyClause(); 11981 break; 11982 case llvm::omp::OMPC_novariants: 11983 C = new (Context) OMPNovariantsClause(); 11984 break; 11985 case llvm::omp::OMPC_nocontext: 11986 C = new (Context) OMPNocontextClause(); 11987 break; 11988 case llvm::omp::OMPC_detach: 11989 C = new (Context) OMPDetachClause(); 11990 break; 11991 case llvm::omp::OMPC_uses_allocators: 11992 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11993 break; 11994 case llvm::omp::OMPC_affinity: 11995 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11996 break; 11997 case llvm::omp::OMPC_filter: 11998 C = new (Context) OMPFilterClause(); 11999 break; 12000 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 12001 case llvm::omp::Enum: \ 12002 break; 12003 #include "llvm/Frontend/OpenMP/OMPKinds.def" 12004 default: 12005 break; 12006 } 12007 assert(C && "Unknown OMPClause type"); 12008 12009 Visit(C); 12010 C->setLocStart(Record.readSourceLocation()); 12011 C->setLocEnd(Record.readSourceLocation()); 12012 12013 return C; 12014 } 12015 12016 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 12017 C->setPreInitStmt(Record.readSubStmt(), 12018 static_cast<OpenMPDirectiveKind>(Record.readInt())); 12019 } 12020 12021 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 12022 VisitOMPClauseWithPreInit(C); 12023 C->setPostUpdateExpr(Record.readSubExpr()); 12024 } 12025 12026 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 12027 VisitOMPClauseWithPreInit(C); 12028 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 12029 C->setNameModifierLoc(Record.readSourceLocation()); 12030 C->setColonLoc(Record.readSourceLocation()); 12031 C->setCondition(Record.readSubExpr()); 12032 C->setLParenLoc(Record.readSourceLocation()); 12033 } 12034 12035 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12036 VisitOMPClauseWithPreInit(C); 12037 C->setCondition(Record.readSubExpr()); 12038 C->setLParenLoc(Record.readSourceLocation()); 12039 } 12040 12041 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12042 VisitOMPClauseWithPreInit(C); 12043 C->setNumThreads(Record.readSubExpr()); 12044 C->setLParenLoc(Record.readSourceLocation()); 12045 } 12046 12047 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12048 C->setSafelen(Record.readSubExpr()); 12049 C->setLParenLoc(Record.readSourceLocation()); 12050 } 12051 12052 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12053 C->setSimdlen(Record.readSubExpr()); 12054 C->setLParenLoc(Record.readSourceLocation()); 12055 } 12056 12057 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) { 12058 for (Expr *&E : C->getSizesRefs()) 12059 E = Record.readSubExpr(); 12060 C->setLParenLoc(Record.readSourceLocation()); 12061 } 12062 12063 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12064 C->setAllocator(Record.readExpr()); 12065 C->setLParenLoc(Record.readSourceLocation()); 12066 } 12067 12068 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12069 C->setNumForLoops(Record.readSubExpr()); 12070 C->setLParenLoc(Record.readSourceLocation()); 12071 } 12072 12073 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12074 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12075 C->setLParenLoc(Record.readSourceLocation()); 12076 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12077 } 12078 12079 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12080 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12081 C->setLParenLoc(Record.readSourceLocation()); 12082 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12083 } 12084 12085 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12086 VisitOMPClauseWithPreInit(C); 12087 C->setScheduleKind( 12088 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12089 C->setFirstScheduleModifier( 12090 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12091 C->setSecondScheduleModifier( 12092 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12093 C->setChunkSize(Record.readSubExpr()); 12094 C->setLParenLoc(Record.readSourceLocation()); 12095 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12096 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12097 C->setScheduleKindLoc(Record.readSourceLocation()); 12098 C->setCommaLoc(Record.readSourceLocation()); 12099 } 12100 12101 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12102 C->setNumForLoops(Record.readSubExpr()); 12103 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12104 C->setLoopNumIterations(I, Record.readSubExpr()); 12105 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12106 C->setLoopCounter(I, Record.readSubExpr()); 12107 C->setLParenLoc(Record.readSourceLocation()); 12108 } 12109 12110 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12111 C->setEventHandler(Record.readSubExpr()); 12112 C->setLParenLoc(Record.readSourceLocation()); 12113 } 12114 12115 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12116 12117 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12118 12119 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12120 12121 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12122 12123 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12124 12125 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12126 if (C->isExtended()) { 12127 C->setLParenLoc(Record.readSourceLocation()); 12128 C->setArgumentLoc(Record.readSourceLocation()); 12129 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12130 } 12131 } 12132 12133 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12134 12135 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12136 12137 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12138 12139 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12140 12141 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12142 12143 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12144 12145 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12146 12147 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12148 12149 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12150 12151 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) { 12152 unsigned NumVars = C->varlist_size(); 12153 SmallVector<Expr *, 16> Vars; 12154 Vars.reserve(NumVars); 12155 for (unsigned I = 0; I != NumVars; ++I) 12156 Vars.push_back(Record.readSubExpr()); 12157 C->setVarRefs(Vars); 12158 C->setIsTarget(Record.readBool()); 12159 C->setIsTargetSync(Record.readBool()); 12160 C->setLParenLoc(Record.readSourceLocation()); 12161 C->setVarLoc(Record.readSourceLocation()); 12162 } 12163 12164 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) { 12165 C->setInteropVar(Record.readSubExpr()); 12166 C->setLParenLoc(Record.readSourceLocation()); 12167 C->setVarLoc(Record.readSourceLocation()); 12168 } 12169 12170 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) { 12171 C->setInteropVar(Record.readSubExpr()); 12172 C->setLParenLoc(Record.readSourceLocation()); 12173 C->setVarLoc(Record.readSourceLocation()); 12174 } 12175 12176 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) { 12177 VisitOMPClauseWithPreInit(C); 12178 C->setCondition(Record.readSubExpr()); 12179 C->setLParenLoc(Record.readSourceLocation()); 12180 } 12181 12182 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) { 12183 VisitOMPClauseWithPreInit(C); 12184 C->setCondition(Record.readSubExpr()); 12185 C->setLParenLoc(Record.readSourceLocation()); 12186 } 12187 12188 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12189 12190 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12191 OMPUnifiedSharedMemoryClause *) {} 12192 12193 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12194 12195 void 12196 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12197 } 12198 12199 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12200 OMPAtomicDefaultMemOrderClause *C) { 12201 C->setAtomicDefaultMemOrderKind( 12202 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12203 C->setLParenLoc(Record.readSourceLocation()); 12204 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12205 } 12206 12207 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12208 C->setLParenLoc(Record.readSourceLocation()); 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->setPrivateCopies(Vars); 12219 } 12220 12221 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12222 VisitOMPClauseWithPreInit(C); 12223 C->setLParenLoc(Record.readSourceLocation()); 12224 unsigned NumVars = C->varlist_size(); 12225 SmallVector<Expr *, 16> Vars; 12226 Vars.reserve(NumVars); 12227 for (unsigned i = 0; i != NumVars; ++i) 12228 Vars.push_back(Record.readSubExpr()); 12229 C->setVarRefs(Vars); 12230 Vars.clear(); 12231 for (unsigned i = 0; i != NumVars; ++i) 12232 Vars.push_back(Record.readSubExpr()); 12233 C->setPrivateCopies(Vars); 12234 Vars.clear(); 12235 for (unsigned i = 0; i != NumVars; ++i) 12236 Vars.push_back(Record.readSubExpr()); 12237 C->setInits(Vars); 12238 } 12239 12240 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12241 VisitOMPClauseWithPostUpdate(C); 12242 C->setLParenLoc(Record.readSourceLocation()); 12243 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12244 C->setKindLoc(Record.readSourceLocation()); 12245 C->setColonLoc(Record.readSourceLocation()); 12246 unsigned NumVars = C->varlist_size(); 12247 SmallVector<Expr *, 16> Vars; 12248 Vars.reserve(NumVars); 12249 for (unsigned i = 0; i != NumVars; ++i) 12250 Vars.push_back(Record.readSubExpr()); 12251 C->setVarRefs(Vars); 12252 Vars.clear(); 12253 for (unsigned i = 0; i != NumVars; ++i) 12254 Vars.push_back(Record.readSubExpr()); 12255 C->setPrivateCopies(Vars); 12256 Vars.clear(); 12257 for (unsigned i = 0; i != NumVars; ++i) 12258 Vars.push_back(Record.readSubExpr()); 12259 C->setSourceExprs(Vars); 12260 Vars.clear(); 12261 for (unsigned i = 0; i != NumVars; ++i) 12262 Vars.push_back(Record.readSubExpr()); 12263 C->setDestinationExprs(Vars); 12264 Vars.clear(); 12265 for (unsigned i = 0; i != NumVars; ++i) 12266 Vars.push_back(Record.readSubExpr()); 12267 C->setAssignmentOps(Vars); 12268 } 12269 12270 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12271 C->setLParenLoc(Record.readSourceLocation()); 12272 unsigned NumVars = C->varlist_size(); 12273 SmallVector<Expr *, 16> Vars; 12274 Vars.reserve(NumVars); 12275 for (unsigned i = 0; i != NumVars; ++i) 12276 Vars.push_back(Record.readSubExpr()); 12277 C->setVarRefs(Vars); 12278 } 12279 12280 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12281 VisitOMPClauseWithPostUpdate(C); 12282 C->setLParenLoc(Record.readSourceLocation()); 12283 C->setModifierLoc(Record.readSourceLocation()); 12284 C->setColonLoc(Record.readSourceLocation()); 12285 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12286 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12287 C->setQualifierLoc(NNSL); 12288 C->setNameInfo(DNI); 12289 12290 unsigned NumVars = C->varlist_size(); 12291 SmallVector<Expr *, 16> Vars; 12292 Vars.reserve(NumVars); 12293 for (unsigned i = 0; i != NumVars; ++i) 12294 Vars.push_back(Record.readSubExpr()); 12295 C->setVarRefs(Vars); 12296 Vars.clear(); 12297 for (unsigned i = 0; i != NumVars; ++i) 12298 Vars.push_back(Record.readSubExpr()); 12299 C->setPrivates(Vars); 12300 Vars.clear(); 12301 for (unsigned i = 0; i != NumVars; ++i) 12302 Vars.push_back(Record.readSubExpr()); 12303 C->setLHSExprs(Vars); 12304 Vars.clear(); 12305 for (unsigned i = 0; i != NumVars; ++i) 12306 Vars.push_back(Record.readSubExpr()); 12307 C->setRHSExprs(Vars); 12308 Vars.clear(); 12309 for (unsigned i = 0; i != NumVars; ++i) 12310 Vars.push_back(Record.readSubExpr()); 12311 C->setReductionOps(Vars); 12312 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12313 Vars.clear(); 12314 for (unsigned i = 0; i != NumVars; ++i) 12315 Vars.push_back(Record.readSubExpr()); 12316 C->setInscanCopyOps(Vars); 12317 Vars.clear(); 12318 for (unsigned i = 0; i != NumVars; ++i) 12319 Vars.push_back(Record.readSubExpr()); 12320 C->setInscanCopyArrayTemps(Vars); 12321 Vars.clear(); 12322 for (unsigned i = 0; i != NumVars; ++i) 12323 Vars.push_back(Record.readSubExpr()); 12324 C->setInscanCopyArrayElems(Vars); 12325 } 12326 } 12327 12328 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12329 VisitOMPClauseWithPostUpdate(C); 12330 C->setLParenLoc(Record.readSourceLocation()); 12331 C->setColonLoc(Record.readSourceLocation()); 12332 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12333 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12334 C->setQualifierLoc(NNSL); 12335 C->setNameInfo(DNI); 12336 12337 unsigned NumVars = C->varlist_size(); 12338 SmallVector<Expr *, 16> Vars; 12339 Vars.reserve(NumVars); 12340 for (unsigned I = 0; I != NumVars; ++I) 12341 Vars.push_back(Record.readSubExpr()); 12342 C->setVarRefs(Vars); 12343 Vars.clear(); 12344 for (unsigned I = 0; I != NumVars; ++I) 12345 Vars.push_back(Record.readSubExpr()); 12346 C->setPrivates(Vars); 12347 Vars.clear(); 12348 for (unsigned I = 0; I != NumVars; ++I) 12349 Vars.push_back(Record.readSubExpr()); 12350 C->setLHSExprs(Vars); 12351 Vars.clear(); 12352 for (unsigned I = 0; I != NumVars; ++I) 12353 Vars.push_back(Record.readSubExpr()); 12354 C->setRHSExprs(Vars); 12355 Vars.clear(); 12356 for (unsigned I = 0; I != NumVars; ++I) 12357 Vars.push_back(Record.readSubExpr()); 12358 C->setReductionOps(Vars); 12359 } 12360 12361 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12362 VisitOMPClauseWithPostUpdate(C); 12363 C->setLParenLoc(Record.readSourceLocation()); 12364 C->setColonLoc(Record.readSourceLocation()); 12365 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12366 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12367 C->setQualifierLoc(NNSL); 12368 C->setNameInfo(DNI); 12369 12370 unsigned NumVars = C->varlist_size(); 12371 SmallVector<Expr *, 16> Vars; 12372 Vars.reserve(NumVars); 12373 for (unsigned I = 0; I != NumVars; ++I) 12374 Vars.push_back(Record.readSubExpr()); 12375 C->setVarRefs(Vars); 12376 Vars.clear(); 12377 for (unsigned I = 0; I != NumVars; ++I) 12378 Vars.push_back(Record.readSubExpr()); 12379 C->setPrivates(Vars); 12380 Vars.clear(); 12381 for (unsigned I = 0; I != NumVars; ++I) 12382 Vars.push_back(Record.readSubExpr()); 12383 C->setLHSExprs(Vars); 12384 Vars.clear(); 12385 for (unsigned I = 0; I != NumVars; ++I) 12386 Vars.push_back(Record.readSubExpr()); 12387 C->setRHSExprs(Vars); 12388 Vars.clear(); 12389 for (unsigned I = 0; I != NumVars; ++I) 12390 Vars.push_back(Record.readSubExpr()); 12391 C->setReductionOps(Vars); 12392 Vars.clear(); 12393 for (unsigned I = 0; I != NumVars; ++I) 12394 Vars.push_back(Record.readSubExpr()); 12395 C->setTaskgroupDescriptors(Vars); 12396 } 12397 12398 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12399 VisitOMPClauseWithPostUpdate(C); 12400 C->setLParenLoc(Record.readSourceLocation()); 12401 C->setColonLoc(Record.readSourceLocation()); 12402 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12403 C->setModifierLoc(Record.readSourceLocation()); 12404 unsigned NumVars = C->varlist_size(); 12405 SmallVector<Expr *, 16> Vars; 12406 Vars.reserve(NumVars); 12407 for (unsigned i = 0; i != NumVars; ++i) 12408 Vars.push_back(Record.readSubExpr()); 12409 C->setVarRefs(Vars); 12410 Vars.clear(); 12411 for (unsigned i = 0; i != NumVars; ++i) 12412 Vars.push_back(Record.readSubExpr()); 12413 C->setPrivates(Vars); 12414 Vars.clear(); 12415 for (unsigned i = 0; i != NumVars; ++i) 12416 Vars.push_back(Record.readSubExpr()); 12417 C->setInits(Vars); 12418 Vars.clear(); 12419 for (unsigned i = 0; i != NumVars; ++i) 12420 Vars.push_back(Record.readSubExpr()); 12421 C->setUpdates(Vars); 12422 Vars.clear(); 12423 for (unsigned i = 0; i != NumVars; ++i) 12424 Vars.push_back(Record.readSubExpr()); 12425 C->setFinals(Vars); 12426 C->setStep(Record.readSubExpr()); 12427 C->setCalcStep(Record.readSubExpr()); 12428 Vars.clear(); 12429 for (unsigned I = 0; I != NumVars + 1; ++I) 12430 Vars.push_back(Record.readSubExpr()); 12431 C->setUsedExprs(Vars); 12432 } 12433 12434 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12435 C->setLParenLoc(Record.readSourceLocation()); 12436 C->setColonLoc(Record.readSourceLocation()); 12437 unsigned NumVars = C->varlist_size(); 12438 SmallVector<Expr *, 16> Vars; 12439 Vars.reserve(NumVars); 12440 for (unsigned i = 0; i != NumVars; ++i) 12441 Vars.push_back(Record.readSubExpr()); 12442 C->setVarRefs(Vars); 12443 C->setAlignment(Record.readSubExpr()); 12444 } 12445 12446 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12447 C->setLParenLoc(Record.readSourceLocation()); 12448 unsigned NumVars = C->varlist_size(); 12449 SmallVector<Expr *, 16> Exprs; 12450 Exprs.reserve(NumVars); 12451 for (unsigned i = 0; i != NumVars; ++i) 12452 Exprs.push_back(Record.readSubExpr()); 12453 C->setVarRefs(Exprs); 12454 Exprs.clear(); 12455 for (unsigned i = 0; i != NumVars; ++i) 12456 Exprs.push_back(Record.readSubExpr()); 12457 C->setSourceExprs(Exprs); 12458 Exprs.clear(); 12459 for (unsigned i = 0; i != NumVars; ++i) 12460 Exprs.push_back(Record.readSubExpr()); 12461 C->setDestinationExprs(Exprs); 12462 Exprs.clear(); 12463 for (unsigned i = 0; i != NumVars; ++i) 12464 Exprs.push_back(Record.readSubExpr()); 12465 C->setAssignmentOps(Exprs); 12466 } 12467 12468 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12469 C->setLParenLoc(Record.readSourceLocation()); 12470 unsigned NumVars = C->varlist_size(); 12471 SmallVector<Expr *, 16> Exprs; 12472 Exprs.reserve(NumVars); 12473 for (unsigned i = 0; i != NumVars; ++i) 12474 Exprs.push_back(Record.readSubExpr()); 12475 C->setVarRefs(Exprs); 12476 Exprs.clear(); 12477 for (unsigned i = 0; i != NumVars; ++i) 12478 Exprs.push_back(Record.readSubExpr()); 12479 C->setSourceExprs(Exprs); 12480 Exprs.clear(); 12481 for (unsigned i = 0; i != NumVars; ++i) 12482 Exprs.push_back(Record.readSubExpr()); 12483 C->setDestinationExprs(Exprs); 12484 Exprs.clear(); 12485 for (unsigned i = 0; i != NumVars; ++i) 12486 Exprs.push_back(Record.readSubExpr()); 12487 C->setAssignmentOps(Exprs); 12488 } 12489 12490 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12491 C->setLParenLoc(Record.readSourceLocation()); 12492 unsigned NumVars = C->varlist_size(); 12493 SmallVector<Expr *, 16> Vars; 12494 Vars.reserve(NumVars); 12495 for (unsigned i = 0; i != NumVars; ++i) 12496 Vars.push_back(Record.readSubExpr()); 12497 C->setVarRefs(Vars); 12498 } 12499 12500 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12501 C->setDepobj(Record.readSubExpr()); 12502 C->setLParenLoc(Record.readSourceLocation()); 12503 } 12504 12505 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12506 C->setLParenLoc(Record.readSourceLocation()); 12507 C->setModifier(Record.readSubExpr()); 12508 C->setDependencyKind( 12509 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12510 C->setDependencyLoc(Record.readSourceLocation()); 12511 C->setColonLoc(Record.readSourceLocation()); 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 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12519 C->setLoopData(I, Record.readSubExpr()); 12520 } 12521 12522 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12523 VisitOMPClauseWithPreInit(C); 12524 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12525 C->setDevice(Record.readSubExpr()); 12526 C->setModifierLoc(Record.readSourceLocation()); 12527 C->setLParenLoc(Record.readSourceLocation()); 12528 } 12529 12530 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12531 C->setLParenLoc(Record.readSourceLocation()); 12532 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12533 C->setMapTypeModifier( 12534 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12535 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12536 } 12537 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12538 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12539 C->setMapType( 12540 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12541 C->setMapLoc(Record.readSourceLocation()); 12542 C->setColonLoc(Record.readSourceLocation()); 12543 auto NumVars = C->varlist_size(); 12544 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12545 auto TotalLists = C->getTotalComponentListNum(); 12546 auto TotalComponents = C->getTotalComponentsNum(); 12547 12548 SmallVector<Expr *, 16> Vars; 12549 Vars.reserve(NumVars); 12550 for (unsigned i = 0; i != NumVars; ++i) 12551 Vars.push_back(Record.readExpr()); 12552 C->setVarRefs(Vars); 12553 12554 SmallVector<Expr *, 16> UDMappers; 12555 UDMappers.reserve(NumVars); 12556 for (unsigned I = 0; I < NumVars; ++I) 12557 UDMappers.push_back(Record.readExpr()); 12558 C->setUDMapperRefs(UDMappers); 12559 12560 SmallVector<ValueDecl *, 16> Decls; 12561 Decls.reserve(UniqueDecls); 12562 for (unsigned i = 0; i < UniqueDecls; ++i) 12563 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12564 C->setUniqueDecls(Decls); 12565 12566 SmallVector<unsigned, 16> ListsPerDecl; 12567 ListsPerDecl.reserve(UniqueDecls); 12568 for (unsigned i = 0; i < UniqueDecls; ++i) 12569 ListsPerDecl.push_back(Record.readInt()); 12570 C->setDeclNumLists(ListsPerDecl); 12571 12572 SmallVector<unsigned, 32> ListSizes; 12573 ListSizes.reserve(TotalLists); 12574 for (unsigned i = 0; i < TotalLists; ++i) 12575 ListSizes.push_back(Record.readInt()); 12576 C->setComponentListSizes(ListSizes); 12577 12578 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12579 Components.reserve(TotalComponents); 12580 for (unsigned i = 0; i < TotalComponents; ++i) { 12581 Expr *AssociatedExprPr = Record.readExpr(); 12582 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12583 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12584 /*IsNonContiguous=*/false); 12585 } 12586 C->setComponents(Components, ListSizes); 12587 } 12588 12589 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12590 C->setLParenLoc(Record.readSourceLocation()); 12591 C->setColonLoc(Record.readSourceLocation()); 12592 C->setAllocator(Record.readSubExpr()); 12593 unsigned NumVars = C->varlist_size(); 12594 SmallVector<Expr *, 16> Vars; 12595 Vars.reserve(NumVars); 12596 for (unsigned i = 0; i != NumVars; ++i) 12597 Vars.push_back(Record.readSubExpr()); 12598 C->setVarRefs(Vars); 12599 } 12600 12601 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12602 VisitOMPClauseWithPreInit(C); 12603 C->setNumTeams(Record.readSubExpr()); 12604 C->setLParenLoc(Record.readSourceLocation()); 12605 } 12606 12607 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12608 VisitOMPClauseWithPreInit(C); 12609 C->setThreadLimit(Record.readSubExpr()); 12610 C->setLParenLoc(Record.readSourceLocation()); 12611 } 12612 12613 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12614 VisitOMPClauseWithPreInit(C); 12615 C->setPriority(Record.readSubExpr()); 12616 C->setLParenLoc(Record.readSourceLocation()); 12617 } 12618 12619 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12620 VisitOMPClauseWithPreInit(C); 12621 C->setGrainsize(Record.readSubExpr()); 12622 C->setLParenLoc(Record.readSourceLocation()); 12623 } 12624 12625 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12626 VisitOMPClauseWithPreInit(C); 12627 C->setNumTasks(Record.readSubExpr()); 12628 C->setLParenLoc(Record.readSourceLocation()); 12629 } 12630 12631 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12632 C->setHint(Record.readSubExpr()); 12633 C->setLParenLoc(Record.readSourceLocation()); 12634 } 12635 12636 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12637 VisitOMPClauseWithPreInit(C); 12638 C->setDistScheduleKind( 12639 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12640 C->setChunkSize(Record.readSubExpr()); 12641 C->setLParenLoc(Record.readSourceLocation()); 12642 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12643 C->setCommaLoc(Record.readSourceLocation()); 12644 } 12645 12646 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12647 C->setDefaultmapKind( 12648 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12649 C->setDefaultmapModifier( 12650 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12651 C->setLParenLoc(Record.readSourceLocation()); 12652 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12653 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12654 } 12655 12656 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12657 C->setLParenLoc(Record.readSourceLocation()); 12658 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12659 C->setMotionModifier( 12660 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12661 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12662 } 12663 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12664 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12665 C->setColonLoc(Record.readSourceLocation()); 12666 auto NumVars = C->varlist_size(); 12667 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12668 auto TotalLists = C->getTotalComponentListNum(); 12669 auto TotalComponents = C->getTotalComponentsNum(); 12670 12671 SmallVector<Expr *, 16> Vars; 12672 Vars.reserve(NumVars); 12673 for (unsigned i = 0; i != NumVars; ++i) 12674 Vars.push_back(Record.readSubExpr()); 12675 C->setVarRefs(Vars); 12676 12677 SmallVector<Expr *, 16> UDMappers; 12678 UDMappers.reserve(NumVars); 12679 for (unsigned I = 0; I < NumVars; ++I) 12680 UDMappers.push_back(Record.readSubExpr()); 12681 C->setUDMapperRefs(UDMappers); 12682 12683 SmallVector<ValueDecl *, 16> Decls; 12684 Decls.reserve(UniqueDecls); 12685 for (unsigned i = 0; i < UniqueDecls; ++i) 12686 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12687 C->setUniqueDecls(Decls); 12688 12689 SmallVector<unsigned, 16> ListsPerDecl; 12690 ListsPerDecl.reserve(UniqueDecls); 12691 for (unsigned i = 0; i < UniqueDecls; ++i) 12692 ListsPerDecl.push_back(Record.readInt()); 12693 C->setDeclNumLists(ListsPerDecl); 12694 12695 SmallVector<unsigned, 32> ListSizes; 12696 ListSizes.reserve(TotalLists); 12697 for (unsigned i = 0; i < TotalLists; ++i) 12698 ListSizes.push_back(Record.readInt()); 12699 C->setComponentListSizes(ListSizes); 12700 12701 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12702 Components.reserve(TotalComponents); 12703 for (unsigned i = 0; i < TotalComponents; ++i) { 12704 Expr *AssociatedExprPr = Record.readSubExpr(); 12705 bool IsNonContiguous = Record.readBool(); 12706 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12707 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12708 } 12709 C->setComponents(Components, ListSizes); 12710 } 12711 12712 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12713 C->setLParenLoc(Record.readSourceLocation()); 12714 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12715 C->setMotionModifier( 12716 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12717 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12718 } 12719 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12720 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12721 C->setColonLoc(Record.readSourceLocation()); 12722 auto NumVars = C->varlist_size(); 12723 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12724 auto TotalLists = C->getTotalComponentListNum(); 12725 auto TotalComponents = C->getTotalComponentsNum(); 12726 12727 SmallVector<Expr *, 16> Vars; 12728 Vars.reserve(NumVars); 12729 for (unsigned i = 0; i != NumVars; ++i) 12730 Vars.push_back(Record.readSubExpr()); 12731 C->setVarRefs(Vars); 12732 12733 SmallVector<Expr *, 16> UDMappers; 12734 UDMappers.reserve(NumVars); 12735 for (unsigned I = 0; I < NumVars; ++I) 12736 UDMappers.push_back(Record.readSubExpr()); 12737 C->setUDMapperRefs(UDMappers); 12738 12739 SmallVector<ValueDecl *, 16> Decls; 12740 Decls.reserve(UniqueDecls); 12741 for (unsigned i = 0; i < UniqueDecls; ++i) 12742 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12743 C->setUniqueDecls(Decls); 12744 12745 SmallVector<unsigned, 16> ListsPerDecl; 12746 ListsPerDecl.reserve(UniqueDecls); 12747 for (unsigned i = 0; i < UniqueDecls; ++i) 12748 ListsPerDecl.push_back(Record.readInt()); 12749 C->setDeclNumLists(ListsPerDecl); 12750 12751 SmallVector<unsigned, 32> ListSizes; 12752 ListSizes.reserve(TotalLists); 12753 for (unsigned i = 0; i < TotalLists; ++i) 12754 ListSizes.push_back(Record.readInt()); 12755 C->setComponentListSizes(ListSizes); 12756 12757 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12758 Components.reserve(TotalComponents); 12759 for (unsigned i = 0; i < TotalComponents; ++i) { 12760 Expr *AssociatedExprPr = Record.readSubExpr(); 12761 bool IsNonContiguous = Record.readBool(); 12762 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12763 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12764 } 12765 C->setComponents(Components, ListSizes); 12766 } 12767 12768 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12769 C->setLParenLoc(Record.readSourceLocation()); 12770 auto NumVars = C->varlist_size(); 12771 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12772 auto TotalLists = C->getTotalComponentListNum(); 12773 auto TotalComponents = C->getTotalComponentsNum(); 12774 12775 SmallVector<Expr *, 16> Vars; 12776 Vars.reserve(NumVars); 12777 for (unsigned i = 0; i != NumVars; ++i) 12778 Vars.push_back(Record.readSubExpr()); 12779 C->setVarRefs(Vars); 12780 Vars.clear(); 12781 for (unsigned i = 0; i != NumVars; ++i) 12782 Vars.push_back(Record.readSubExpr()); 12783 C->setPrivateCopies(Vars); 12784 Vars.clear(); 12785 for (unsigned i = 0; i != NumVars; ++i) 12786 Vars.push_back(Record.readSubExpr()); 12787 C->setInits(Vars); 12788 12789 SmallVector<ValueDecl *, 16> Decls; 12790 Decls.reserve(UniqueDecls); 12791 for (unsigned i = 0; i < UniqueDecls; ++i) 12792 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12793 C->setUniqueDecls(Decls); 12794 12795 SmallVector<unsigned, 16> ListsPerDecl; 12796 ListsPerDecl.reserve(UniqueDecls); 12797 for (unsigned i = 0; i < UniqueDecls; ++i) 12798 ListsPerDecl.push_back(Record.readInt()); 12799 C->setDeclNumLists(ListsPerDecl); 12800 12801 SmallVector<unsigned, 32> ListSizes; 12802 ListSizes.reserve(TotalLists); 12803 for (unsigned i = 0; i < TotalLists; ++i) 12804 ListSizes.push_back(Record.readInt()); 12805 C->setComponentListSizes(ListSizes); 12806 12807 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12808 Components.reserve(TotalComponents); 12809 for (unsigned i = 0; i < TotalComponents; ++i) { 12810 auto *AssociatedExprPr = Record.readSubExpr(); 12811 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12812 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12813 /*IsNonContiguous=*/false); 12814 } 12815 C->setComponents(Components, ListSizes); 12816 } 12817 12818 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12819 C->setLParenLoc(Record.readSourceLocation()); 12820 auto NumVars = C->varlist_size(); 12821 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12822 auto TotalLists = C->getTotalComponentListNum(); 12823 auto TotalComponents = C->getTotalComponentsNum(); 12824 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 12831 SmallVector<ValueDecl *, 16> Decls; 12832 Decls.reserve(UniqueDecls); 12833 for (unsigned i = 0; i < UniqueDecls; ++i) 12834 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12835 C->setUniqueDecls(Decls); 12836 12837 SmallVector<unsigned, 16> ListsPerDecl; 12838 ListsPerDecl.reserve(UniqueDecls); 12839 for (unsigned i = 0; i < UniqueDecls; ++i) 12840 ListsPerDecl.push_back(Record.readInt()); 12841 C->setDeclNumLists(ListsPerDecl); 12842 12843 SmallVector<unsigned, 32> ListSizes; 12844 ListSizes.reserve(TotalLists); 12845 for (unsigned i = 0; i < TotalLists; ++i) 12846 ListSizes.push_back(Record.readInt()); 12847 C->setComponentListSizes(ListSizes); 12848 12849 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12850 Components.reserve(TotalComponents); 12851 for (unsigned i = 0; i < TotalComponents; ++i) { 12852 Expr *AssociatedExpr = Record.readSubExpr(); 12853 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12854 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12855 /*IsNonContiguous*/ false); 12856 } 12857 C->setComponents(Components, ListSizes); 12858 } 12859 12860 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12861 C->setLParenLoc(Record.readSourceLocation()); 12862 auto NumVars = C->varlist_size(); 12863 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12864 auto TotalLists = C->getTotalComponentListNum(); 12865 auto TotalComponents = C->getTotalComponentsNum(); 12866 12867 SmallVector<Expr *, 16> Vars; 12868 Vars.reserve(NumVars); 12869 for (unsigned i = 0; i != NumVars; ++i) 12870 Vars.push_back(Record.readSubExpr()); 12871 C->setVarRefs(Vars); 12872 Vars.clear(); 12873 12874 SmallVector<ValueDecl *, 16> Decls; 12875 Decls.reserve(UniqueDecls); 12876 for (unsigned i = 0; i < UniqueDecls; ++i) 12877 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12878 C->setUniqueDecls(Decls); 12879 12880 SmallVector<unsigned, 16> ListsPerDecl; 12881 ListsPerDecl.reserve(UniqueDecls); 12882 for (unsigned i = 0; i < UniqueDecls; ++i) 12883 ListsPerDecl.push_back(Record.readInt()); 12884 C->setDeclNumLists(ListsPerDecl); 12885 12886 SmallVector<unsigned, 32> ListSizes; 12887 ListSizes.reserve(TotalLists); 12888 for (unsigned i = 0; i < TotalLists; ++i) 12889 ListSizes.push_back(Record.readInt()); 12890 C->setComponentListSizes(ListSizes); 12891 12892 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12893 Components.reserve(TotalComponents); 12894 for (unsigned i = 0; i < TotalComponents; ++i) { 12895 Expr *AssociatedExpr = Record.readSubExpr(); 12896 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12897 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12898 /*IsNonContiguous=*/false); 12899 } 12900 C->setComponents(Components, ListSizes); 12901 } 12902 12903 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12904 C->setLParenLoc(Record.readSourceLocation()); 12905 unsigned NumVars = C->varlist_size(); 12906 SmallVector<Expr *, 16> Vars; 12907 Vars.reserve(NumVars); 12908 for (unsigned i = 0; i != NumVars; ++i) 12909 Vars.push_back(Record.readSubExpr()); 12910 C->setVarRefs(Vars); 12911 Vars.clear(); 12912 Vars.reserve(NumVars); 12913 for (unsigned i = 0; i != NumVars; ++i) 12914 Vars.push_back(Record.readSubExpr()); 12915 C->setPrivateRefs(Vars); 12916 } 12917 12918 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12919 C->setLParenLoc(Record.readSourceLocation()); 12920 unsigned NumVars = C->varlist_size(); 12921 SmallVector<Expr *, 16> Vars; 12922 Vars.reserve(NumVars); 12923 for (unsigned i = 0; i != NumVars; ++i) 12924 Vars.push_back(Record.readSubExpr()); 12925 C->setVarRefs(Vars); 12926 } 12927 12928 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12929 C->setLParenLoc(Record.readSourceLocation()); 12930 unsigned NumVars = C->varlist_size(); 12931 SmallVector<Expr *, 16> Vars; 12932 Vars.reserve(NumVars); 12933 for (unsigned i = 0; i != NumVars; ++i) 12934 Vars.push_back(Record.readSubExpr()); 12935 C->setVarRefs(Vars); 12936 } 12937 12938 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12939 C->setLParenLoc(Record.readSourceLocation()); 12940 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12941 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12942 Data.reserve(NumOfAllocators); 12943 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12944 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12945 D.Allocator = Record.readSubExpr(); 12946 D.AllocatorTraits = Record.readSubExpr(); 12947 D.LParenLoc = Record.readSourceLocation(); 12948 D.RParenLoc = Record.readSourceLocation(); 12949 } 12950 C->setAllocatorsData(Data); 12951 } 12952 12953 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12954 C->setLParenLoc(Record.readSourceLocation()); 12955 C->setModifier(Record.readSubExpr()); 12956 C->setColonLoc(Record.readSourceLocation()); 12957 unsigned NumOfLocators = C->varlist_size(); 12958 SmallVector<Expr *, 4> Locators; 12959 Locators.reserve(NumOfLocators); 12960 for (unsigned I = 0; I != NumOfLocators; ++I) 12961 Locators.push_back(Record.readSubExpr()); 12962 C->setVarRefs(Locators); 12963 } 12964 12965 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12966 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12967 C->setLParenLoc(Record.readSourceLocation()); 12968 C->setKindKwLoc(Record.readSourceLocation()); 12969 } 12970 12971 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) { 12972 VisitOMPClauseWithPreInit(C); 12973 C->setThreadID(Record.readSubExpr()); 12974 C->setLParenLoc(Record.readSourceLocation()); 12975 } 12976 12977 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12978 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12979 TI.Sets.resize(readUInt32()); 12980 for (auto &Set : TI.Sets) { 12981 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12982 Set.Selectors.resize(readUInt32()); 12983 for (auto &Selector : Set.Selectors) { 12984 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12985 Selector.ScoreOrCondition = nullptr; 12986 if (readBool()) 12987 Selector.ScoreOrCondition = readExprRef(); 12988 Selector.Properties.resize(readUInt32()); 12989 for (auto &Property : Selector.Properties) 12990 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12991 } 12992 } 12993 return &TI; 12994 } 12995 12996 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12997 if (!Data) 12998 return; 12999 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 13000 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 13001 skipInts(3); 13002 } 13003 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 13004 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 13005 Clauses[I] = readOMPClause(); 13006 Data->setClauses(Clauses); 13007 if (Data->hasAssociatedStmt()) 13008 Data->setAssociatedStmt(readStmt()); 13009 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 13010 Data->getChildren()[I] = readStmt(); 13011 } 13012