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