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 "ASTCommon.h" 14 #include "ASTReaderInternals.h" 15 #include "clang/AST/ASTConsumer.h" 16 #include "clang/AST/ASTContext.h" 17 #include "clang/AST/ASTMutationListener.h" 18 #include "clang/AST/ASTStructuralEquivalence.h" 19 #include "clang/AST/ASTUnresolvedSet.h" 20 #include "clang/AST/AbstractTypeReader.h" 21 #include "clang/AST/Decl.h" 22 #include "clang/AST/DeclBase.h" 23 #include "clang/AST/DeclCXX.h" 24 #include "clang/AST/DeclFriend.h" 25 #include "clang/AST/DeclGroup.h" 26 #include "clang/AST/DeclObjC.h" 27 #include "clang/AST/DeclTemplate.h" 28 #include "clang/AST/DeclarationName.h" 29 #include "clang/AST/Expr.h" 30 #include "clang/AST/ExprCXX.h" 31 #include "clang/AST/ExternalASTSource.h" 32 #include "clang/AST/NestedNameSpecifier.h" 33 #include "clang/AST/ODRHash.h" 34 #include "clang/AST/OpenMPClause.h" 35 #include "clang/AST/RawCommentList.h" 36 #include "clang/AST/TemplateBase.h" 37 #include "clang/AST/TemplateName.h" 38 #include "clang/AST/Type.h" 39 #include "clang/AST/TypeLoc.h" 40 #include "clang/AST/TypeLocVisitor.h" 41 #include "clang/AST/UnresolvedSet.h" 42 #include "clang/Basic/CommentOptions.h" 43 #include "clang/Basic/Diagnostic.h" 44 #include "clang/Basic/DiagnosticError.h" 45 #include "clang/Basic/DiagnosticOptions.h" 46 #include "clang/Basic/DiagnosticSema.h" 47 #include "clang/Basic/ExceptionSpecificationType.h" 48 #include "clang/Basic/FileManager.h" 49 #include "clang/Basic/FileSystemOptions.h" 50 #include "clang/Basic/IdentifierTable.h" 51 #include "clang/Basic/LLVM.h" 52 #include "clang/Basic/LangOptions.h" 53 #include "clang/Basic/Module.h" 54 #include "clang/Basic/ObjCRuntime.h" 55 #include "clang/Basic/OpenMPKinds.h" 56 #include "clang/Basic/OperatorKinds.h" 57 #include "clang/Basic/PragmaKinds.h" 58 #include "clang/Basic/Sanitizers.h" 59 #include "clang/Basic/SourceLocation.h" 60 #include "clang/Basic/SourceManager.h" 61 #include "clang/Basic/SourceManagerInternals.h" 62 #include "clang/Basic/Specifiers.h" 63 #include "clang/Basic/TargetInfo.h" 64 #include "clang/Basic/TargetOptions.h" 65 #include "clang/Basic/TokenKinds.h" 66 #include "clang/Basic/Version.h" 67 #include "clang/Lex/HeaderSearch.h" 68 #include "clang/Lex/HeaderSearchOptions.h" 69 #include "clang/Lex/MacroInfo.h" 70 #include "clang/Lex/ModuleMap.h" 71 #include "clang/Lex/PreprocessingRecord.h" 72 #include "clang/Lex/Preprocessor.h" 73 #include "clang/Lex/PreprocessorOptions.h" 74 #include "clang/Lex/Token.h" 75 #include "clang/Sema/ObjCMethodList.h" 76 #include "clang/Sema/Scope.h" 77 #include "clang/Sema/Sema.h" 78 #include "clang/Sema/Weak.h" 79 #include "clang/Serialization/ASTBitCodes.h" 80 #include "clang/Serialization/ASTDeserializationListener.h" 81 #include "clang/Serialization/ASTRecordReader.h" 82 #include "clang/Serialization/ContinuousRangeMap.h" 83 #include "clang/Serialization/GlobalModuleIndex.h" 84 #include "clang/Serialization/InMemoryModuleCache.h" 85 #include "clang/Serialization/ModuleFile.h" 86 #include "clang/Serialization/ModuleFileExtension.h" 87 #include "clang/Serialization/ModuleManager.h" 88 #include "clang/Serialization/PCHContainerOperations.h" 89 #include "clang/Serialization/SerializationDiagnostic.h" 90 #include "llvm/ADT/APFloat.h" 91 #include "llvm/ADT/APInt.h" 92 #include "llvm/ADT/APSInt.h" 93 #include "llvm/ADT/ArrayRef.h" 94 #include "llvm/ADT/DenseMap.h" 95 #include "llvm/ADT/FloatingPointMode.h" 96 #include "llvm/ADT/FoldingSet.h" 97 #include "llvm/ADT/Hashing.h" 98 #include "llvm/ADT/IntrusiveRefCntPtr.h" 99 #include "llvm/ADT/None.h" 100 #include "llvm/ADT/Optional.h" 101 #include "llvm/ADT/STLExtras.h" 102 #include "llvm/ADT/ScopeExit.h" 103 #include "llvm/ADT/SmallPtrSet.h" 104 #include "llvm/ADT/SmallString.h" 105 #include "llvm/ADT/SmallVector.h" 106 #include "llvm/ADT/StringExtras.h" 107 #include "llvm/ADT/StringMap.h" 108 #include "llvm/ADT/StringRef.h" 109 #include "llvm/ADT/Triple.h" 110 #include "llvm/ADT/iterator_range.h" 111 #include "llvm/Bitstream/BitstreamReader.h" 112 #include "llvm/Support/Casting.h" 113 #include "llvm/Support/Compiler.h" 114 #include "llvm/Support/Compression.h" 115 #include "llvm/Support/DJB.h" 116 #include "llvm/Support/Endian.h" 117 #include "llvm/Support/Error.h" 118 #include "llvm/Support/ErrorHandling.h" 119 #include "llvm/Support/FileSystem.h" 120 #include "llvm/Support/LEB128.h" 121 #include "llvm/Support/MemoryBuffer.h" 122 #include "llvm/Support/Path.h" 123 #include "llvm/Support/SaveAndRestore.h" 124 #include "llvm/Support/Timer.h" 125 #include "llvm/Support/VersionTuple.h" 126 #include "llvm/Support/raw_ostream.h" 127 #include <algorithm> 128 #include <cassert> 129 #include <cstddef> 130 #include <cstdint> 131 #include <cstdio> 132 #include <ctime> 133 #include <iterator> 134 #include <limits> 135 #include <map> 136 #include <memory> 137 #include <string> 138 #include <system_error> 139 #include <tuple> 140 #include <utility> 141 #include <vector> 142 143 using namespace clang; 144 using namespace clang::serialization; 145 using namespace clang::serialization::reader; 146 using llvm::BitstreamCursor; 147 148 //===----------------------------------------------------------------------===// 149 // ChainedASTReaderListener implementation 150 //===----------------------------------------------------------------------===// 151 152 bool 153 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 154 return First->ReadFullVersionInformation(FullVersion) || 155 Second->ReadFullVersionInformation(FullVersion); 156 } 157 158 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 159 First->ReadModuleName(ModuleName); 160 Second->ReadModuleName(ModuleName); 161 } 162 163 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 164 First->ReadModuleMapFile(ModuleMapPath); 165 Second->ReadModuleMapFile(ModuleMapPath); 166 } 167 168 bool 169 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 170 bool Complain, 171 bool AllowCompatibleDifferences) { 172 return First->ReadLanguageOptions(LangOpts, Complain, 173 AllowCompatibleDifferences) || 174 Second->ReadLanguageOptions(LangOpts, Complain, 175 AllowCompatibleDifferences); 176 } 177 178 bool ChainedASTReaderListener::ReadTargetOptions( 179 const TargetOptions &TargetOpts, bool Complain, 180 bool AllowCompatibleDifferences) { 181 return First->ReadTargetOptions(TargetOpts, Complain, 182 AllowCompatibleDifferences) || 183 Second->ReadTargetOptions(TargetOpts, Complain, 184 AllowCompatibleDifferences); 185 } 186 187 bool ChainedASTReaderListener::ReadDiagnosticOptions( 188 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 189 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 190 Second->ReadDiagnosticOptions(DiagOpts, Complain); 191 } 192 193 bool 194 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 195 bool Complain) { 196 return First->ReadFileSystemOptions(FSOpts, Complain) || 197 Second->ReadFileSystemOptions(FSOpts, Complain); 198 } 199 200 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 201 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 202 bool Complain) { 203 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 204 Complain) || 205 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 206 Complain); 207 } 208 209 bool ChainedASTReaderListener::ReadPreprocessorOptions( 210 const PreprocessorOptions &PPOpts, bool Complain, 211 std::string &SuggestedPredefines) { 212 return First->ReadPreprocessorOptions(PPOpts, Complain, 213 SuggestedPredefines) || 214 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 215 } 216 217 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 218 unsigned Value) { 219 First->ReadCounter(M, Value); 220 Second->ReadCounter(M, Value); 221 } 222 223 bool ChainedASTReaderListener::needsInputFileVisitation() { 224 return First->needsInputFileVisitation() || 225 Second->needsInputFileVisitation(); 226 } 227 228 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 229 return First->needsSystemInputFileVisitation() || 230 Second->needsSystemInputFileVisitation(); 231 } 232 233 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 234 ModuleKind Kind) { 235 First->visitModuleFile(Filename, Kind); 236 Second->visitModuleFile(Filename, Kind); 237 } 238 239 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 240 bool isSystem, 241 bool isOverridden, 242 bool isExplicitModule) { 243 bool Continue = false; 244 if (First->needsInputFileVisitation() && 245 (!isSystem || First->needsSystemInputFileVisitation())) 246 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 247 isExplicitModule); 248 if (Second->needsInputFileVisitation() && 249 (!isSystem || Second->needsSystemInputFileVisitation())) 250 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 251 isExplicitModule); 252 return Continue; 253 } 254 255 void ChainedASTReaderListener::readModuleFileExtension( 256 const ModuleFileExtensionMetadata &Metadata) { 257 First->readModuleFileExtension(Metadata); 258 Second->readModuleFileExtension(Metadata); 259 } 260 261 //===----------------------------------------------------------------------===// 262 // PCH validator implementation 263 //===----------------------------------------------------------------------===// 264 265 ASTReaderListener::~ASTReaderListener() = default; 266 267 /// Compare the given set of language options against an existing set of 268 /// language options. 269 /// 270 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 271 /// \param AllowCompatibleDifferences If true, differences between compatible 272 /// language options will be permitted. 273 /// 274 /// \returns true if the languagae options mis-match, false otherwise. 275 static bool checkLanguageOptions(const LangOptions &LangOpts, 276 const LangOptions &ExistingLangOpts, 277 DiagnosticsEngine *Diags, 278 bool AllowCompatibleDifferences = true) { 279 #define LANGOPT(Name, Bits, Default, Description) \ 280 if (ExistingLangOpts.Name != LangOpts.Name) { \ 281 if (Diags) \ 282 Diags->Report(diag::err_pch_langopt_mismatch) \ 283 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 284 return true; \ 285 } 286 287 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 288 if (ExistingLangOpts.Name != LangOpts.Name) { \ 289 if (Diags) \ 290 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 291 << Description; \ 292 return true; \ 293 } 294 295 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 296 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 297 if (Diags) \ 298 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 299 << Description; \ 300 return true; \ 301 } 302 303 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 304 if (!AllowCompatibleDifferences) \ 305 LANGOPT(Name, Bits, Default, Description) 306 307 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 308 if (!AllowCompatibleDifferences) \ 309 ENUM_LANGOPT(Name, Bits, Default, Description) 310 311 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 312 if (!AllowCompatibleDifferences) \ 313 VALUE_LANGOPT(Name, Bits, Default, Description) 314 315 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 316 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 317 #define BENIGN_VALUE_LANGOPT(Name, Bits, Default, Description) 318 #include "clang/Basic/LangOptions.def" 319 320 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 321 if (Diags) 322 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 323 return true; 324 } 325 326 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 327 if (Diags) 328 Diags->Report(diag::err_pch_langopt_value_mismatch) 329 << "target Objective-C runtime"; 330 return true; 331 } 332 333 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 334 LangOpts.CommentOpts.BlockCommandNames) { 335 if (Diags) 336 Diags->Report(diag::err_pch_langopt_value_mismatch) 337 << "block command names"; 338 return true; 339 } 340 341 // Sanitizer feature mismatches are treated as compatible differences. If 342 // compatible differences aren't allowed, we still only want to check for 343 // mismatches of non-modular sanitizers (the only ones which can affect AST 344 // generation). 345 if (!AllowCompatibleDifferences) { 346 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 347 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 348 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 349 ExistingSanitizers.clear(ModularSanitizers); 350 ImportedSanitizers.clear(ModularSanitizers); 351 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 352 const std::string Flag = "-fsanitize="; 353 if (Diags) { 354 #define SANITIZER(NAME, ID) \ 355 { \ 356 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 357 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 358 if (InExistingModule != InImportedModule) \ 359 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 360 << InExistingModule << (Flag + NAME); \ 361 } 362 #include "clang/Basic/Sanitizers.def" 363 } 364 return true; 365 } 366 } 367 368 return false; 369 } 370 371 /// Compare the given set of target options against an existing set of 372 /// target options. 373 /// 374 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 375 /// 376 /// \returns true if the target options mis-match, false otherwise. 377 static bool checkTargetOptions(const TargetOptions &TargetOpts, 378 const TargetOptions &ExistingTargetOpts, 379 DiagnosticsEngine *Diags, 380 bool AllowCompatibleDifferences = true) { 381 #define CHECK_TARGET_OPT(Field, Name) \ 382 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 383 if (Diags) \ 384 Diags->Report(diag::err_pch_targetopt_mismatch) \ 385 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 386 return true; \ 387 } 388 389 // The triple and ABI must match exactly. 390 CHECK_TARGET_OPT(Triple, "target"); 391 CHECK_TARGET_OPT(ABI, "target ABI"); 392 393 // We can tolerate different CPUs in many cases, notably when one CPU 394 // supports a strict superset of another. When allowing compatible 395 // differences skip this check. 396 if (!AllowCompatibleDifferences) { 397 CHECK_TARGET_OPT(CPU, "target CPU"); 398 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 399 } 400 401 #undef CHECK_TARGET_OPT 402 403 // Compare feature sets. 404 SmallVector<StringRef, 4> ExistingFeatures( 405 ExistingTargetOpts.FeaturesAsWritten.begin(), 406 ExistingTargetOpts.FeaturesAsWritten.end()); 407 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 408 TargetOpts.FeaturesAsWritten.end()); 409 llvm::sort(ExistingFeatures); 410 llvm::sort(ReadFeatures); 411 412 // We compute the set difference in both directions explicitly so that we can 413 // diagnose the differences differently. 414 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 415 std::set_difference( 416 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 417 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 418 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 419 ExistingFeatures.begin(), ExistingFeatures.end(), 420 std::back_inserter(UnmatchedReadFeatures)); 421 422 // If we are allowing compatible differences and the read feature set is 423 // a strict subset of the existing feature set, there is nothing to diagnose. 424 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 425 return false; 426 427 if (Diags) { 428 for (StringRef Feature : UnmatchedReadFeatures) 429 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 430 << /* is-existing-feature */ false << Feature; 431 for (StringRef Feature : UnmatchedExistingFeatures) 432 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 433 << /* is-existing-feature */ true << Feature; 434 } 435 436 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 437 } 438 439 bool 440 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 441 bool Complain, 442 bool AllowCompatibleDifferences) { 443 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 444 return checkLanguageOptions(LangOpts, ExistingLangOpts, 445 Complain ? &Reader.Diags : nullptr, 446 AllowCompatibleDifferences); 447 } 448 449 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 450 bool Complain, 451 bool AllowCompatibleDifferences) { 452 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 453 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 454 Complain ? &Reader.Diags : nullptr, 455 AllowCompatibleDifferences); 456 } 457 458 namespace { 459 460 using MacroDefinitionsMap = 461 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 462 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 463 464 } // namespace 465 466 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 467 DiagnosticsEngine &Diags, 468 bool Complain) { 469 using Level = DiagnosticsEngine::Level; 470 471 // Check current mappings for new -Werror mappings, and the stored mappings 472 // for cases that were explicitly mapped to *not* be errors that are now 473 // errors because of options like -Werror. 474 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 475 476 for (DiagnosticsEngine *MappingSource : MappingSources) { 477 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 478 diag::kind DiagID = DiagIDMappingPair.first; 479 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 480 if (CurLevel < DiagnosticsEngine::Error) 481 continue; // not significant 482 Level StoredLevel = 483 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 484 if (StoredLevel < DiagnosticsEngine::Error) { 485 if (Complain) 486 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 487 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 488 return true; 489 } 490 } 491 } 492 493 return false; 494 } 495 496 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 497 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 498 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 499 return true; 500 return Ext >= diag::Severity::Error; 501 } 502 503 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 504 DiagnosticsEngine &Diags, 505 bool IsSystem, bool Complain) { 506 // Top-level options 507 if (IsSystem) { 508 if (Diags.getSuppressSystemWarnings()) 509 return false; 510 // If -Wsystem-headers was not enabled before, be conservative 511 if (StoredDiags.getSuppressSystemWarnings()) { 512 if (Complain) 513 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 514 return true; 515 } 516 } 517 518 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 519 if (Complain) 520 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 521 return true; 522 } 523 524 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 525 !StoredDiags.getEnableAllWarnings()) { 526 if (Complain) 527 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 528 return true; 529 } 530 531 if (isExtHandlingFromDiagsError(Diags) && 532 !isExtHandlingFromDiagsError(StoredDiags)) { 533 if (Complain) 534 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 535 return true; 536 } 537 538 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 539 } 540 541 /// Return the top import module if it is implicit, nullptr otherwise. 542 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 543 Preprocessor &PP) { 544 // If the original import came from a file explicitly generated by the user, 545 // don't check the diagnostic mappings. 546 // FIXME: currently this is approximated by checking whether this is not a 547 // module import of an implicitly-loaded module file. 548 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 549 // the transitive closure of its imports, since unrelated modules cannot be 550 // imported until after this module finishes validation. 551 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 552 while (!TopImport->ImportedBy.empty()) 553 TopImport = TopImport->ImportedBy[0]; 554 if (TopImport->Kind != MK_ImplicitModule) 555 return nullptr; 556 557 StringRef ModuleName = TopImport->ModuleName; 558 assert(!ModuleName.empty() && "diagnostic options read before module name"); 559 560 Module *M = 561 PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc); 562 assert(M && "missing module"); 563 return M; 564 } 565 566 bool PCHValidator::ReadDiagnosticOptions( 567 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 568 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 569 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 570 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 571 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 572 // This should never fail, because we would have processed these options 573 // before writing them to an ASTFile. 574 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 575 576 ModuleManager &ModuleMgr = Reader.getModuleManager(); 577 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 578 579 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 580 if (!TopM) 581 return false; 582 583 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 584 // contains the union of their flags. 585 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 586 Complain); 587 } 588 589 /// Collect the macro definitions provided by the given preprocessor 590 /// options. 591 static void 592 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 593 MacroDefinitionsMap &Macros, 594 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 595 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 596 StringRef Macro = PPOpts.Macros[I].first; 597 bool IsUndef = PPOpts.Macros[I].second; 598 599 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 600 StringRef MacroName = MacroPair.first; 601 StringRef MacroBody = MacroPair.second; 602 603 // For an #undef'd macro, we only care about the name. 604 if (IsUndef) { 605 if (MacroNames && !Macros.count(MacroName)) 606 MacroNames->push_back(MacroName); 607 608 Macros[MacroName] = std::make_pair("", true); 609 continue; 610 } 611 612 // For a #define'd macro, figure out the actual definition. 613 if (MacroName.size() == Macro.size()) 614 MacroBody = "1"; 615 else { 616 // Note: GCC drops anything following an end-of-line character. 617 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 618 MacroBody = MacroBody.substr(0, End); 619 } 620 621 if (MacroNames && !Macros.count(MacroName)) 622 MacroNames->push_back(MacroName); 623 Macros[MacroName] = std::make_pair(MacroBody, false); 624 } 625 } 626 627 enum OptionValidation { 628 OptionValidateNone, 629 OptionValidateContradictions, 630 OptionValidateStrictMatches, 631 }; 632 633 /// Check the preprocessor options deserialized from the control block 634 /// against the preprocessor options in an existing preprocessor. 635 /// 636 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 637 /// \param Validation If set to OptionValidateNone, ignore differences in 638 /// preprocessor options. If set to OptionValidateContradictions, 639 /// require that options passed both in the AST file and on the command 640 /// line (-D or -U) match, but tolerate options missing in one or the 641 /// other. If set to OptionValidateContradictions, require that there 642 /// are no differences in the options between the two. 643 static bool checkPreprocessorOptions( 644 const PreprocessorOptions &PPOpts, 645 const PreprocessorOptions &ExistingPPOpts, DiagnosticsEngine *Diags, 646 FileManager &FileMgr, std::string &SuggestedPredefines, 647 const LangOptions &LangOpts, 648 OptionValidation Validation = OptionValidateContradictions) { 649 // Check macro definitions. 650 MacroDefinitionsMap ASTFileMacros; 651 collectMacroDefinitions(PPOpts, ASTFileMacros); 652 MacroDefinitionsMap ExistingMacros; 653 SmallVector<StringRef, 4> ExistingMacroNames; 654 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 655 656 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 657 // Dig out the macro definition in the existing preprocessor options. 658 StringRef MacroName = ExistingMacroNames[I]; 659 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 660 661 // Check whether we know anything about this macro name or not. 662 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 663 ASTFileMacros.find(MacroName); 664 if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) { 665 if (Validation == OptionValidateStrictMatches) { 666 // If strict matches are requested, don't tolerate any extra defines on 667 // the command line that are missing in the AST file. 668 if (Diags) { 669 Diags->Report(diag::err_pch_macro_def_undef) << MacroName << true; 670 } 671 return true; 672 } 673 // FIXME: Check whether this identifier was referenced anywhere in the 674 // AST file. If so, we should reject the AST file. Unfortunately, this 675 // information isn't in the control block. What shall we do about it? 676 677 if (Existing.second) { 678 SuggestedPredefines += "#undef "; 679 SuggestedPredefines += MacroName.str(); 680 SuggestedPredefines += '\n'; 681 } else { 682 SuggestedPredefines += "#define "; 683 SuggestedPredefines += MacroName.str(); 684 SuggestedPredefines += ' '; 685 SuggestedPredefines += Existing.first.str(); 686 SuggestedPredefines += '\n'; 687 } 688 continue; 689 } 690 691 // If the macro was defined in one but undef'd in the other, we have a 692 // conflict. 693 if (Existing.second != Known->second.second) { 694 if (Diags) { 695 Diags->Report(diag::err_pch_macro_def_undef) 696 << MacroName << Known->second.second; 697 } 698 return true; 699 } 700 701 // If the macro was #undef'd in both, or if the macro bodies are identical, 702 // it's fine. 703 if (Existing.second || Existing.first == Known->second.first) { 704 ASTFileMacros.erase(Known); 705 continue; 706 } 707 708 // The macro bodies differ; complain. 709 if (Diags) { 710 Diags->Report(diag::err_pch_macro_def_conflict) 711 << MacroName << Known->second.first << Existing.first; 712 } 713 return true; 714 } 715 if (Validation == OptionValidateStrictMatches) { 716 // If strict matches are requested, don't tolerate any extra defines in 717 // the AST file that are missing on the command line. 718 for (const auto &MacroName : ASTFileMacros.keys()) { 719 if (Diags) { 720 Diags->Report(diag::err_pch_macro_def_undef) << MacroName << false; 721 } 722 return true; 723 } 724 } 725 726 // Check whether we're using predefines. 727 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && 728 Validation != OptionValidateNone) { 729 if (Diags) { 730 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 731 } 732 return true; 733 } 734 735 // Detailed record is important since it is used for the module cache hash. 736 if (LangOpts.Modules && 737 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && 738 Validation != OptionValidateNone) { 739 if (Diags) { 740 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 741 } 742 return true; 743 } 744 745 // Compute the #include and #include_macros lines we need. 746 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 747 StringRef File = ExistingPPOpts.Includes[I]; 748 749 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 750 !ExistingPPOpts.PCHThroughHeader.empty()) { 751 // In case the through header is an include, we must add all the includes 752 // to the predefines so the start point can be determined. 753 SuggestedPredefines += "#include \""; 754 SuggestedPredefines += File; 755 SuggestedPredefines += "\"\n"; 756 continue; 757 } 758 759 if (File == ExistingPPOpts.ImplicitPCHInclude) 760 continue; 761 762 if (llvm::is_contained(PPOpts.Includes, File)) 763 continue; 764 765 SuggestedPredefines += "#include \""; 766 SuggestedPredefines += File; 767 SuggestedPredefines += "\"\n"; 768 } 769 770 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 771 StringRef File = ExistingPPOpts.MacroIncludes[I]; 772 if (llvm::is_contained(PPOpts.MacroIncludes, File)) 773 continue; 774 775 SuggestedPredefines += "#__include_macros \""; 776 SuggestedPredefines += File; 777 SuggestedPredefines += "\"\n##\n"; 778 } 779 780 return false; 781 } 782 783 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 784 bool Complain, 785 std::string &SuggestedPredefines) { 786 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 787 788 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 789 Complain? &Reader.Diags : nullptr, 790 PP.getFileManager(), 791 SuggestedPredefines, 792 PP.getLangOpts()); 793 } 794 795 bool SimpleASTReaderListener::ReadPreprocessorOptions( 796 const PreprocessorOptions &PPOpts, 797 bool Complain, 798 std::string &SuggestedPredefines) { 799 return checkPreprocessorOptions(PPOpts, PP.getPreprocessorOpts(), nullptr, 800 PP.getFileManager(), SuggestedPredefines, 801 PP.getLangOpts(), OptionValidateNone); 802 } 803 804 /// Check the header search options deserialized from the control block 805 /// against the header search options in an existing preprocessor. 806 /// 807 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 808 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 809 StringRef SpecificModuleCachePath, 810 StringRef ExistingModuleCachePath, 811 DiagnosticsEngine *Diags, 812 const LangOptions &LangOpts, 813 const PreprocessorOptions &PPOpts) { 814 if (LangOpts.Modules) { 815 if (SpecificModuleCachePath != ExistingModuleCachePath && 816 !PPOpts.AllowPCHWithDifferentModulesCachePath) { 817 if (Diags) 818 Diags->Report(diag::err_pch_modulecache_mismatch) 819 << SpecificModuleCachePath << ExistingModuleCachePath; 820 return true; 821 } 822 } 823 824 return false; 825 } 826 827 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 828 StringRef SpecificModuleCachePath, 829 bool Complain) { 830 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 831 PP.getHeaderSearchInfo().getModuleCachePath(), 832 Complain ? &Reader.Diags : nullptr, 833 PP.getLangOpts(), PP.getPreprocessorOpts()); 834 } 835 836 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 837 PP.setCounterValue(Value); 838 } 839 840 //===----------------------------------------------------------------------===// 841 // AST reader implementation 842 //===----------------------------------------------------------------------===// 843 844 static uint64_t readULEB(const unsigned char *&P) { 845 unsigned Length = 0; 846 const char *Error = nullptr; 847 848 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error); 849 if (Error) 850 llvm::report_fatal_error(Error); 851 P += Length; 852 return Val; 853 } 854 855 /// Read ULEB-encoded key length and data length. 856 static std::pair<unsigned, unsigned> 857 readULEBKeyDataLength(const unsigned char *&P) { 858 unsigned KeyLen = readULEB(P); 859 if ((unsigned)KeyLen != KeyLen) 860 llvm::report_fatal_error("key too large"); 861 862 unsigned DataLen = readULEB(P); 863 if ((unsigned)DataLen != DataLen) 864 llvm::report_fatal_error("data too large"); 865 866 return std::make_pair(KeyLen, DataLen); 867 } 868 869 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 870 bool TakeOwnership) { 871 DeserializationListener = Listener; 872 OwnsDeserializationListener = TakeOwnership; 873 } 874 875 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 876 return serialization::ComputeHash(Sel); 877 } 878 879 std::pair<unsigned, unsigned> 880 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 881 return readULEBKeyDataLength(d); 882 } 883 884 ASTSelectorLookupTrait::internal_key_type 885 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 886 using namespace llvm::support; 887 888 SelectorTable &SelTable = Reader.getContext().Selectors; 889 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 890 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 891 F, endian::readNext<uint32_t, little, unaligned>(d)); 892 if (N == 0) 893 return SelTable.getNullarySelector(FirstII); 894 else if (N == 1) 895 return SelTable.getUnarySelector(FirstII); 896 897 SmallVector<IdentifierInfo *, 16> Args; 898 Args.push_back(FirstII); 899 for (unsigned I = 1; I != N; ++I) 900 Args.push_back(Reader.getLocalIdentifier( 901 F, endian::readNext<uint32_t, little, unaligned>(d))); 902 903 return SelTable.getSelector(N, Args.data()); 904 } 905 906 ASTSelectorLookupTrait::data_type 907 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 908 unsigned DataLen) { 909 using namespace llvm::support; 910 911 data_type Result; 912 913 Result.ID = Reader.getGlobalSelectorID( 914 F, endian::readNext<uint32_t, little, unaligned>(d)); 915 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 916 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 917 Result.InstanceBits = FullInstanceBits & 0x3; 918 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 919 Result.FactoryBits = FullFactoryBits & 0x3; 920 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 921 unsigned NumInstanceMethods = FullInstanceBits >> 3; 922 unsigned NumFactoryMethods = FullFactoryBits >> 3; 923 924 // Load instance methods 925 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 926 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 927 F, endian::readNext<uint32_t, little, unaligned>(d))) 928 Result.Instance.push_back(Method); 929 } 930 931 // Load factory methods 932 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 933 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 934 F, endian::readNext<uint32_t, little, unaligned>(d))) 935 Result.Factory.push_back(Method); 936 } 937 938 return Result; 939 } 940 941 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 942 return llvm::djbHash(a); 943 } 944 945 std::pair<unsigned, unsigned> 946 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 947 return readULEBKeyDataLength(d); 948 } 949 950 ASTIdentifierLookupTraitBase::internal_key_type 951 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 952 assert(n >= 2 && d[n-1] == '\0'); 953 return StringRef((const char*) d, n-1); 954 } 955 956 /// Whether the given identifier is "interesting". 957 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 958 bool IsModule) { 959 return II.hadMacroDefinition() || II.isPoisoned() || 960 (!IsModule && II.getObjCOrBuiltinID()) || 961 II.hasRevertedTokenIDToIdentifier() || 962 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 963 II.getFETokenInfo()); 964 } 965 966 static bool readBit(unsigned &Bits) { 967 bool Value = Bits & 0x1; 968 Bits >>= 1; 969 return Value; 970 } 971 972 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 973 using namespace llvm::support; 974 975 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 976 return Reader.getGlobalIdentifierID(F, RawID >> 1); 977 } 978 979 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 980 if (!II.isFromAST()) { 981 II.setIsFromAST(); 982 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 983 if (isInterestingIdentifier(Reader, II, IsModule)) 984 II.setChangedSinceDeserialization(); 985 } 986 } 987 988 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 989 const unsigned char* d, 990 unsigned DataLen) { 991 using namespace llvm::support; 992 993 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 994 bool IsInteresting = RawID & 0x01; 995 996 // Wipe out the "is interesting" bit. 997 RawID = RawID >> 1; 998 999 // Build the IdentifierInfo and link the identifier ID with it. 1000 IdentifierInfo *II = KnownII; 1001 if (!II) { 1002 II = &Reader.getIdentifierTable().getOwn(k); 1003 KnownII = II; 1004 } 1005 markIdentifierFromAST(Reader, *II); 1006 Reader.markIdentifierUpToDate(II); 1007 1008 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 1009 if (!IsInteresting) { 1010 // For uninteresting identifiers, there's nothing else to do. Just notify 1011 // the reader that we've finished loading this identifier. 1012 Reader.SetIdentifierInfo(ID, II); 1013 return II; 1014 } 1015 1016 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 1017 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 1018 bool CPlusPlusOperatorKeyword = readBit(Bits); 1019 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 1020 bool Poisoned = readBit(Bits); 1021 bool ExtensionToken = readBit(Bits); 1022 bool HadMacroDefinition = readBit(Bits); 1023 1024 assert(Bits == 0 && "Extra bits in the identifier?"); 1025 DataLen -= 8; 1026 1027 // Set or check the various bits in the IdentifierInfo structure. 1028 // Token IDs are read-only. 1029 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 1030 II->revertTokenIDToIdentifier(); 1031 if (!F.isModule()) 1032 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 1033 assert(II->isExtensionToken() == ExtensionToken && 1034 "Incorrect extension token flag"); 1035 (void)ExtensionToken; 1036 if (Poisoned) 1037 II->setIsPoisoned(true); 1038 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 1039 "Incorrect C++ operator keyword flag"); 1040 (void)CPlusPlusOperatorKeyword; 1041 1042 // If this identifier is a macro, deserialize the macro 1043 // definition. 1044 if (HadMacroDefinition) { 1045 uint32_t MacroDirectivesOffset = 1046 endian::readNext<uint32_t, little, unaligned>(d); 1047 DataLen -= 4; 1048 1049 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1050 } 1051 1052 Reader.SetIdentifierInfo(ID, II); 1053 1054 // Read all of the declarations visible at global scope with this 1055 // name. 1056 if (DataLen > 0) { 1057 SmallVector<uint32_t, 4> DeclIDs; 1058 for (; DataLen > 0; DataLen -= 4) 1059 DeclIDs.push_back(Reader.getGlobalDeclID( 1060 F, endian::readNext<uint32_t, little, unaligned>(d))); 1061 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1062 } 1063 1064 return II; 1065 } 1066 1067 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1068 : Kind(Name.getNameKind()) { 1069 switch (Kind) { 1070 case DeclarationName::Identifier: 1071 Data = (uint64_t)Name.getAsIdentifierInfo(); 1072 break; 1073 case DeclarationName::ObjCZeroArgSelector: 1074 case DeclarationName::ObjCOneArgSelector: 1075 case DeclarationName::ObjCMultiArgSelector: 1076 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1077 break; 1078 case DeclarationName::CXXOperatorName: 1079 Data = Name.getCXXOverloadedOperator(); 1080 break; 1081 case DeclarationName::CXXLiteralOperatorName: 1082 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1083 break; 1084 case DeclarationName::CXXDeductionGuideName: 1085 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1086 ->getDeclName().getAsIdentifierInfo(); 1087 break; 1088 case DeclarationName::CXXConstructorName: 1089 case DeclarationName::CXXDestructorName: 1090 case DeclarationName::CXXConversionFunctionName: 1091 case DeclarationName::CXXUsingDirective: 1092 Data = 0; 1093 break; 1094 } 1095 } 1096 1097 unsigned DeclarationNameKey::getHash() const { 1098 llvm::FoldingSetNodeID ID; 1099 ID.AddInteger(Kind); 1100 1101 switch (Kind) { 1102 case DeclarationName::Identifier: 1103 case DeclarationName::CXXLiteralOperatorName: 1104 case DeclarationName::CXXDeductionGuideName: 1105 ID.AddString(((IdentifierInfo*)Data)->getName()); 1106 break; 1107 case DeclarationName::ObjCZeroArgSelector: 1108 case DeclarationName::ObjCOneArgSelector: 1109 case DeclarationName::ObjCMultiArgSelector: 1110 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1111 break; 1112 case DeclarationName::CXXOperatorName: 1113 ID.AddInteger((OverloadedOperatorKind)Data); 1114 break; 1115 case DeclarationName::CXXConstructorName: 1116 case DeclarationName::CXXDestructorName: 1117 case DeclarationName::CXXConversionFunctionName: 1118 case DeclarationName::CXXUsingDirective: 1119 break; 1120 } 1121 1122 return ID.ComputeHash(); 1123 } 1124 1125 ModuleFile * 1126 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1127 using namespace llvm::support; 1128 1129 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1130 return Reader.getLocalModuleFile(F, ModuleFileID); 1131 } 1132 1133 std::pair<unsigned, unsigned> 1134 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1135 return readULEBKeyDataLength(d); 1136 } 1137 1138 ASTDeclContextNameLookupTrait::internal_key_type 1139 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1140 using namespace llvm::support; 1141 1142 auto Kind = (DeclarationName::NameKind)*d++; 1143 uint64_t Data; 1144 switch (Kind) { 1145 case DeclarationName::Identifier: 1146 case DeclarationName::CXXLiteralOperatorName: 1147 case DeclarationName::CXXDeductionGuideName: 1148 Data = (uint64_t)Reader.getLocalIdentifier( 1149 F, endian::readNext<uint32_t, little, unaligned>(d)); 1150 break; 1151 case DeclarationName::ObjCZeroArgSelector: 1152 case DeclarationName::ObjCOneArgSelector: 1153 case DeclarationName::ObjCMultiArgSelector: 1154 Data = 1155 (uint64_t)Reader.getLocalSelector( 1156 F, endian::readNext<uint32_t, little, unaligned>( 1157 d)).getAsOpaquePtr(); 1158 break; 1159 case DeclarationName::CXXOperatorName: 1160 Data = *d++; // OverloadedOperatorKind 1161 break; 1162 case DeclarationName::CXXConstructorName: 1163 case DeclarationName::CXXDestructorName: 1164 case DeclarationName::CXXConversionFunctionName: 1165 case DeclarationName::CXXUsingDirective: 1166 Data = 0; 1167 break; 1168 } 1169 1170 return DeclarationNameKey(Kind, Data); 1171 } 1172 1173 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1174 const unsigned char *d, 1175 unsigned DataLen, 1176 data_type_builder &Val) { 1177 using namespace llvm::support; 1178 1179 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1180 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1181 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1182 } 1183 } 1184 1185 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1186 BitstreamCursor &Cursor, 1187 uint64_t Offset, 1188 DeclContext *DC) { 1189 assert(Offset != 0); 1190 1191 SavedStreamPosition SavedPosition(Cursor); 1192 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1193 Error(std::move(Err)); 1194 return true; 1195 } 1196 1197 RecordData Record; 1198 StringRef Blob; 1199 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1200 if (!MaybeCode) { 1201 Error(MaybeCode.takeError()); 1202 return true; 1203 } 1204 unsigned Code = MaybeCode.get(); 1205 1206 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1207 if (!MaybeRecCode) { 1208 Error(MaybeRecCode.takeError()); 1209 return true; 1210 } 1211 unsigned RecCode = MaybeRecCode.get(); 1212 if (RecCode != DECL_CONTEXT_LEXICAL) { 1213 Error("Expected lexical block"); 1214 return true; 1215 } 1216 1217 assert(!isa<TranslationUnitDecl>(DC) && 1218 "expected a TU_UPDATE_LEXICAL record for TU"); 1219 // If we are handling a C++ class template instantiation, we can see multiple 1220 // lexical updates for the same record. It's important that we select only one 1221 // of them, so that field numbering works properly. Just pick the first one we 1222 // see. 1223 auto &Lex = LexicalDecls[DC]; 1224 if (!Lex.first) { 1225 Lex = std::make_pair( 1226 &M, llvm::makeArrayRef( 1227 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1228 Blob.data()), 1229 Blob.size() / 4)); 1230 } 1231 DC->setHasExternalLexicalStorage(true); 1232 return false; 1233 } 1234 1235 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1236 BitstreamCursor &Cursor, 1237 uint64_t Offset, 1238 DeclID ID) { 1239 assert(Offset != 0); 1240 1241 SavedStreamPosition SavedPosition(Cursor); 1242 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1243 Error(std::move(Err)); 1244 return true; 1245 } 1246 1247 RecordData Record; 1248 StringRef Blob; 1249 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1250 if (!MaybeCode) { 1251 Error(MaybeCode.takeError()); 1252 return true; 1253 } 1254 unsigned Code = MaybeCode.get(); 1255 1256 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1257 if (!MaybeRecCode) { 1258 Error(MaybeRecCode.takeError()); 1259 return true; 1260 } 1261 unsigned RecCode = MaybeRecCode.get(); 1262 if (RecCode != DECL_CONTEXT_VISIBLE) { 1263 Error("Expected visible lookup table block"); 1264 return true; 1265 } 1266 1267 // We can't safely determine the primary context yet, so delay attaching the 1268 // lookup table until we're done with recursive deserialization. 1269 auto *Data = (const unsigned char*)Blob.data(); 1270 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1271 return false; 1272 } 1273 1274 void ASTReader::Error(StringRef Msg) const { 1275 Error(diag::err_fe_pch_malformed, Msg); 1276 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1277 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1278 Diag(diag::note_module_cache_path) 1279 << PP.getHeaderSearchInfo().getModuleCachePath(); 1280 } 1281 } 1282 1283 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1284 StringRef Arg3) const { 1285 if (Diags.isDiagnosticInFlight()) 1286 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1287 else 1288 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1289 } 1290 1291 void ASTReader::Error(llvm::Error &&Err) const { 1292 llvm::Error RemainingErr = 1293 handleErrors(std::move(Err), [this](const DiagnosticError &E) { 1294 auto Diag = E.getDiagnostic().second; 1295 1296 // Ideally we'd just emit it, but have to handle a possible in-flight 1297 // diagnostic. Note that the location is currently ignored as well. 1298 auto NumArgs = Diag.getStorage()->NumDiagArgs; 1299 assert(NumArgs <= 3 && "Can only have up to 3 arguments"); 1300 StringRef Arg1, Arg2, Arg3; 1301 switch (NumArgs) { 1302 case 3: 1303 Arg3 = Diag.getStringArg(2); 1304 LLVM_FALLTHROUGH; 1305 case 2: 1306 Arg2 = Diag.getStringArg(1); 1307 LLVM_FALLTHROUGH; 1308 case 1: 1309 Arg1 = Diag.getStringArg(0); 1310 } 1311 Error(Diag.getDiagID(), Arg1, Arg2, Arg3); 1312 }); 1313 if (RemainingErr) 1314 Error(toString(std::move(RemainingErr))); 1315 } 1316 1317 //===----------------------------------------------------------------------===// 1318 // Source Manager Deserialization 1319 //===----------------------------------------------------------------------===// 1320 1321 /// Read the line table in the source manager block. 1322 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) { 1323 unsigned Idx = 0; 1324 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1325 1326 // Parse the file names 1327 std::map<int, int> FileIDs; 1328 FileIDs[-1] = -1; // For unspecified filenames. 1329 for (unsigned I = 0; Record[Idx]; ++I) { 1330 // Extract the file name 1331 auto Filename = ReadPath(F, Record, Idx); 1332 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1333 } 1334 ++Idx; 1335 1336 // Parse the line entries 1337 std::vector<LineEntry> Entries; 1338 while (Idx < Record.size()) { 1339 int FID = Record[Idx++]; 1340 assert(FID >= 0 && "Serialized line entries for non-local file."); 1341 // Remap FileID from 1-based old view. 1342 FID += F.SLocEntryBaseID - 1; 1343 1344 // Extract the line entries 1345 unsigned NumEntries = Record[Idx++]; 1346 assert(NumEntries && "no line entries for file ID"); 1347 Entries.clear(); 1348 Entries.reserve(NumEntries); 1349 for (unsigned I = 0; I != NumEntries; ++I) { 1350 unsigned FileOffset = Record[Idx++]; 1351 unsigned LineNo = Record[Idx++]; 1352 int FilenameID = FileIDs[Record[Idx++]]; 1353 SrcMgr::CharacteristicKind FileKind 1354 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1355 unsigned IncludeOffset = Record[Idx++]; 1356 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1357 FileKind, IncludeOffset)); 1358 } 1359 LineTable.AddEntry(FileID::get(FID), Entries); 1360 } 1361 } 1362 1363 /// Read a source manager block 1364 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1365 using namespace SrcMgr; 1366 1367 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1368 1369 // Set the source-location entry cursor to the current position in 1370 // the stream. This cursor will be used to read the contents of the 1371 // source manager block initially, and then lazily read 1372 // source-location entries as needed. 1373 SLocEntryCursor = F.Stream; 1374 1375 // The stream itself is going to skip over the source manager block. 1376 if (llvm::Error Err = F.Stream.SkipBlock()) 1377 return Err; 1378 1379 // Enter the source manager block. 1380 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) 1381 return Err; 1382 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1383 1384 RecordData Record; 1385 while (true) { 1386 Expected<llvm::BitstreamEntry> MaybeE = 1387 SLocEntryCursor.advanceSkippingSubblocks(); 1388 if (!MaybeE) 1389 return MaybeE.takeError(); 1390 llvm::BitstreamEntry E = MaybeE.get(); 1391 1392 switch (E.Kind) { 1393 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1394 case llvm::BitstreamEntry::Error: 1395 return llvm::createStringError(std::errc::illegal_byte_sequence, 1396 "malformed block record in AST file"); 1397 case llvm::BitstreamEntry::EndBlock: 1398 return llvm::Error::success(); 1399 case llvm::BitstreamEntry::Record: 1400 // The interesting case. 1401 break; 1402 } 1403 1404 // Read a record. 1405 Record.clear(); 1406 StringRef Blob; 1407 Expected<unsigned> MaybeRecord = 1408 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1409 if (!MaybeRecord) 1410 return MaybeRecord.takeError(); 1411 switch (MaybeRecord.get()) { 1412 default: // Default behavior: ignore. 1413 break; 1414 1415 case SM_SLOC_FILE_ENTRY: 1416 case SM_SLOC_BUFFER_ENTRY: 1417 case SM_SLOC_EXPANSION_ENTRY: 1418 // Once we hit one of the source location entries, we're done. 1419 return llvm::Error::success(); 1420 } 1421 } 1422 } 1423 1424 /// If a header file is not found at the path that we expect it to be 1425 /// and the PCH file was moved from its original location, try to resolve the 1426 /// file by assuming that header+PCH were moved together and the header is in 1427 /// the same place relative to the PCH. 1428 static std::string 1429 resolveFileRelativeToOriginalDir(const std::string &Filename, 1430 const std::string &OriginalDir, 1431 const std::string &CurrDir) { 1432 assert(OriginalDir != CurrDir && 1433 "No point trying to resolve the file if the PCH dir didn't change"); 1434 1435 using namespace llvm::sys; 1436 1437 SmallString<128> filePath(Filename); 1438 fs::make_absolute(filePath); 1439 assert(path::is_absolute(OriginalDir)); 1440 SmallString<128> currPCHPath(CurrDir); 1441 1442 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1443 fileDirE = path::end(path::parent_path(filePath)); 1444 path::const_iterator origDirI = path::begin(OriginalDir), 1445 origDirE = path::end(OriginalDir); 1446 // Skip the common path components from filePath and OriginalDir. 1447 while (fileDirI != fileDirE && origDirI != origDirE && 1448 *fileDirI == *origDirI) { 1449 ++fileDirI; 1450 ++origDirI; 1451 } 1452 for (; origDirI != origDirE; ++origDirI) 1453 path::append(currPCHPath, ".."); 1454 path::append(currPCHPath, fileDirI, fileDirE); 1455 path::append(currPCHPath, path::filename(Filename)); 1456 return std::string(currPCHPath.str()); 1457 } 1458 1459 bool ASTReader::ReadSLocEntry(int ID) { 1460 if (ID == 0) 1461 return false; 1462 1463 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1464 Error("source location entry ID out-of-range for AST file"); 1465 return true; 1466 } 1467 1468 // Local helper to read the (possibly-compressed) buffer data following the 1469 // entry record. 1470 auto ReadBuffer = [this]( 1471 BitstreamCursor &SLocEntryCursor, 1472 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1473 RecordData Record; 1474 StringRef Blob; 1475 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1476 if (!MaybeCode) { 1477 Error(MaybeCode.takeError()); 1478 return nullptr; 1479 } 1480 unsigned Code = MaybeCode.get(); 1481 1482 Expected<unsigned> MaybeRecCode = 1483 SLocEntryCursor.readRecord(Code, Record, &Blob); 1484 if (!MaybeRecCode) { 1485 Error(MaybeRecCode.takeError()); 1486 return nullptr; 1487 } 1488 unsigned RecCode = MaybeRecCode.get(); 1489 1490 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1491 if (!llvm::compression::zlib::isAvailable()) { 1492 Error("zlib is not available"); 1493 return nullptr; 1494 } 1495 SmallVector<uint8_t, 0> Uncompressed; 1496 if (llvm::Error E = llvm::compression::zlib::uncompress( 1497 llvm::arrayRefFromStringRef(Blob), Uncompressed, Record[0])) { 1498 Error("could not decompress embedded file contents: " + 1499 llvm::toString(std::move(E))); 1500 return nullptr; 1501 } 1502 return llvm::MemoryBuffer::getMemBufferCopy( 1503 llvm::toStringRef(Uncompressed), Name); 1504 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1505 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1506 } else { 1507 Error("AST record has invalid code"); 1508 return nullptr; 1509 } 1510 }; 1511 1512 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1513 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1514 F->SLocEntryOffsetsBase + 1515 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1516 Error(std::move(Err)); 1517 return true; 1518 } 1519 1520 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1521 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset; 1522 1523 ++NumSLocEntriesRead; 1524 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1525 if (!MaybeEntry) { 1526 Error(MaybeEntry.takeError()); 1527 return true; 1528 } 1529 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1530 1531 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1532 Error("incorrectly-formatted source location entry in AST file"); 1533 return true; 1534 } 1535 1536 RecordData Record; 1537 StringRef Blob; 1538 Expected<unsigned> MaybeSLOC = 1539 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1540 if (!MaybeSLOC) { 1541 Error(MaybeSLOC.takeError()); 1542 return true; 1543 } 1544 switch (MaybeSLOC.get()) { 1545 default: 1546 Error("incorrectly-formatted source location entry in AST file"); 1547 return true; 1548 1549 case SM_SLOC_FILE_ENTRY: { 1550 // We will detect whether a file changed and return 'Failure' for it, but 1551 // we will also try to fail gracefully by setting up the SLocEntry. 1552 unsigned InputID = Record[4]; 1553 InputFile IF = getInputFile(*F, InputID); 1554 Optional<FileEntryRef> File = IF.getFile(); 1555 bool OverriddenBuffer = IF.isOverridden(); 1556 1557 // Note that we only check if a File was returned. If it was out-of-date 1558 // we have complained but we will continue creating a FileID to recover 1559 // gracefully. 1560 if (!File) 1561 return true; 1562 1563 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1564 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1565 // This is the module's main file. 1566 IncludeLoc = getImportLocation(F); 1567 } 1568 SrcMgr::CharacteristicKind 1569 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1570 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID, 1571 BaseOffset + Record[0]); 1572 SrcMgr::FileInfo &FileInfo = 1573 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1574 FileInfo.NumCreatedFIDs = Record[5]; 1575 if (Record[3]) 1576 FileInfo.setHasLineDirectives(); 1577 1578 unsigned NumFileDecls = Record[7]; 1579 if (NumFileDecls && ContextObj) { 1580 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1581 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1582 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1583 NumFileDecls)); 1584 } 1585 1586 const SrcMgr::ContentCache &ContentCache = 1587 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter)); 1588 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1589 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1590 !ContentCache.getBufferIfLoaded()) { 1591 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1592 if (!Buffer) 1593 return true; 1594 SourceMgr.overrideFileContents(*File, std::move(Buffer)); 1595 } 1596 1597 break; 1598 } 1599 1600 case SM_SLOC_BUFFER_ENTRY: { 1601 const char *Name = Blob.data(); 1602 unsigned Offset = Record[0]; 1603 SrcMgr::CharacteristicKind 1604 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1605 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1606 if (IncludeLoc.isInvalid() && F->isModule()) { 1607 IncludeLoc = getImportLocation(F); 1608 } 1609 1610 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1611 if (!Buffer) 1612 return true; 1613 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1614 BaseOffset + Offset, IncludeLoc); 1615 break; 1616 } 1617 1618 case SM_SLOC_EXPANSION_ENTRY: { 1619 LocSeq::State Seq; 1620 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1], Seq); 1621 SourceLocation ExpansionBegin = ReadSourceLocation(*F, Record[2], Seq); 1622 SourceLocation ExpansionEnd = ReadSourceLocation(*F, Record[3], Seq); 1623 SourceMgr.createExpansionLoc(SpellingLoc, ExpansionBegin, ExpansionEnd, 1624 Record[5], Record[4], ID, 1625 BaseOffset + Record[0]); 1626 break; 1627 } 1628 } 1629 1630 return false; 1631 } 1632 1633 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1634 if (ID == 0) 1635 return std::make_pair(SourceLocation(), ""); 1636 1637 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1638 Error("source location entry ID out-of-range for AST file"); 1639 return std::make_pair(SourceLocation(), ""); 1640 } 1641 1642 // Find which module file this entry lands in. 1643 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1644 if (!M->isModule()) 1645 return std::make_pair(SourceLocation(), ""); 1646 1647 // FIXME: Can we map this down to a particular submodule? That would be 1648 // ideal. 1649 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1650 } 1651 1652 /// Find the location where the module F is imported. 1653 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1654 if (F->ImportLoc.isValid()) 1655 return F->ImportLoc; 1656 1657 // Otherwise we have a PCH. It's considered to be "imported" at the first 1658 // location of its includer. 1659 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1660 // Main file is the importer. 1661 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1662 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1663 } 1664 return F->ImportedBy[0]->FirstLoc; 1665 } 1666 1667 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1668 /// the abbreviations that are at the top of the block and then leave the cursor 1669 /// pointing into the block. 1670 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, 1671 unsigned BlockID, 1672 uint64_t *StartOfBlockOffset) { 1673 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) 1674 return Err; 1675 1676 if (StartOfBlockOffset) 1677 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1678 1679 while (true) { 1680 uint64_t Offset = Cursor.GetCurrentBitNo(); 1681 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1682 if (!MaybeCode) 1683 return MaybeCode.takeError(); 1684 unsigned Code = MaybeCode.get(); 1685 1686 // We expect all abbrevs to be at the start of the block. 1687 if (Code != llvm::bitc::DEFINE_ABBREV) { 1688 if (llvm::Error Err = Cursor.JumpToBit(Offset)) 1689 return Err; 1690 return llvm::Error::success(); 1691 } 1692 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) 1693 return Err; 1694 } 1695 } 1696 1697 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1698 unsigned &Idx) { 1699 Token Tok; 1700 Tok.startToken(); 1701 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1702 Tok.setLength(Record[Idx++]); 1703 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1704 Tok.setIdentifierInfo(II); 1705 Tok.setKind((tok::TokenKind)Record[Idx++]); 1706 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1707 return Tok; 1708 } 1709 1710 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1711 BitstreamCursor &Stream = F.MacroCursor; 1712 1713 // Keep track of where we are in the stream, then jump back there 1714 // after reading this macro. 1715 SavedStreamPosition SavedPosition(Stream); 1716 1717 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1718 // FIXME this drops errors on the floor. 1719 consumeError(std::move(Err)); 1720 return nullptr; 1721 } 1722 RecordData Record; 1723 SmallVector<IdentifierInfo*, 16> MacroParams; 1724 MacroInfo *Macro = nullptr; 1725 llvm::MutableArrayRef<Token> MacroTokens; 1726 1727 while (true) { 1728 // Advance to the next record, but if we get to the end of the block, don't 1729 // pop it (removing all the abbreviations from the cursor) since we want to 1730 // be able to reseek within the block and read entries. 1731 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1732 Expected<llvm::BitstreamEntry> MaybeEntry = 1733 Stream.advanceSkippingSubblocks(Flags); 1734 if (!MaybeEntry) { 1735 Error(MaybeEntry.takeError()); 1736 return Macro; 1737 } 1738 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1739 1740 switch (Entry.Kind) { 1741 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1742 case llvm::BitstreamEntry::Error: 1743 Error("malformed block record in AST file"); 1744 return Macro; 1745 case llvm::BitstreamEntry::EndBlock: 1746 return Macro; 1747 case llvm::BitstreamEntry::Record: 1748 // The interesting case. 1749 break; 1750 } 1751 1752 // Read a record. 1753 Record.clear(); 1754 PreprocessorRecordTypes RecType; 1755 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1756 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1757 else { 1758 Error(MaybeRecType.takeError()); 1759 return Macro; 1760 } 1761 switch (RecType) { 1762 case PP_MODULE_MACRO: 1763 case PP_MACRO_DIRECTIVE_HISTORY: 1764 return Macro; 1765 1766 case PP_MACRO_OBJECT_LIKE: 1767 case PP_MACRO_FUNCTION_LIKE: { 1768 // If we already have a macro, that means that we've hit the end 1769 // of the definition of the macro we were looking for. We're 1770 // done. 1771 if (Macro) 1772 return Macro; 1773 1774 unsigned NextIndex = 1; // Skip identifier ID. 1775 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1776 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1777 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1778 MI->setIsUsed(Record[NextIndex++]); 1779 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1780 MacroTokens = MI->allocateTokens(Record[NextIndex++], 1781 PP.getPreprocessorAllocator()); 1782 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1783 // Decode function-like macro info. 1784 bool isC99VarArgs = Record[NextIndex++]; 1785 bool isGNUVarArgs = Record[NextIndex++]; 1786 bool hasCommaPasting = Record[NextIndex++]; 1787 MacroParams.clear(); 1788 unsigned NumArgs = Record[NextIndex++]; 1789 for (unsigned i = 0; i != NumArgs; ++i) 1790 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1791 1792 // Install function-like macro info. 1793 MI->setIsFunctionLike(); 1794 if (isC99VarArgs) MI->setIsC99Varargs(); 1795 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1796 if (hasCommaPasting) MI->setHasCommaPasting(); 1797 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1798 } 1799 1800 // Remember that we saw this macro last so that we add the tokens that 1801 // form its body to it. 1802 Macro = MI; 1803 1804 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1805 Record[NextIndex]) { 1806 // We have a macro definition. Register the association 1807 PreprocessedEntityID 1808 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1809 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1810 PreprocessingRecord::PPEntityID PPID = 1811 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1812 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1813 PPRec.getPreprocessedEntity(PPID)); 1814 if (PPDef) 1815 PPRec.RegisterMacroDefinition(Macro, PPDef); 1816 } 1817 1818 ++NumMacrosRead; 1819 break; 1820 } 1821 1822 case PP_TOKEN: { 1823 // If we see a TOKEN before a PP_MACRO_*, then the file is 1824 // erroneous, just pretend we didn't see this. 1825 if (!Macro) break; 1826 if (MacroTokens.empty()) { 1827 Error("unexpected number of macro tokens for a macro in AST file"); 1828 return Macro; 1829 } 1830 1831 unsigned Idx = 0; 1832 MacroTokens[0] = ReadToken(F, Record, Idx); 1833 MacroTokens = MacroTokens.drop_front(); 1834 break; 1835 } 1836 } 1837 } 1838 } 1839 1840 PreprocessedEntityID 1841 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1842 unsigned LocalID) const { 1843 if (!M.ModuleOffsetMap.empty()) 1844 ReadModuleOffsetMap(M); 1845 1846 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1847 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1848 assert(I != M.PreprocessedEntityRemap.end() 1849 && "Invalid index into preprocessed entity index remap"); 1850 1851 return LocalID + I->second; 1852 } 1853 1854 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1855 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1856 } 1857 1858 HeaderFileInfoTrait::internal_key_type 1859 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1860 internal_key_type ikey = {FE->getSize(), 1861 M.HasTimestamps ? FE->getModificationTime() : 0, 1862 FE->getName(), /*Imported*/ false}; 1863 return ikey; 1864 } 1865 1866 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1867 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1868 return false; 1869 1870 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1871 return true; 1872 1873 // Determine whether the actual files are equivalent. 1874 FileManager &FileMgr = Reader.getFileManager(); 1875 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1876 if (!Key.Imported) { 1877 if (auto File = FileMgr.getFile(Key.Filename)) 1878 return *File; 1879 return nullptr; 1880 } 1881 1882 std::string Resolved = std::string(Key.Filename); 1883 Reader.ResolveImportedPath(M, Resolved); 1884 if (auto File = FileMgr.getFile(Resolved)) 1885 return *File; 1886 return nullptr; 1887 }; 1888 1889 const FileEntry *FEA = GetFile(a); 1890 const FileEntry *FEB = GetFile(b); 1891 return FEA && FEA == FEB; 1892 } 1893 1894 std::pair<unsigned, unsigned> 1895 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1896 return readULEBKeyDataLength(d); 1897 } 1898 1899 HeaderFileInfoTrait::internal_key_type 1900 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1901 using namespace llvm::support; 1902 1903 internal_key_type ikey; 1904 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1905 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1906 ikey.Filename = (const char *)d; 1907 ikey.Imported = true; 1908 return ikey; 1909 } 1910 1911 HeaderFileInfoTrait::data_type 1912 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1913 unsigned DataLen) { 1914 using namespace llvm::support; 1915 1916 const unsigned char *End = d + DataLen; 1917 HeaderFileInfo HFI; 1918 unsigned Flags = *d++; 1919 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1920 HFI.isImport |= (Flags >> 5) & 0x01; 1921 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1922 HFI.DirInfo = (Flags >> 1) & 0x07; 1923 HFI.IndexHeaderMapHeader = Flags & 0x01; 1924 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1925 M, endian::readNext<uint32_t, little, unaligned>(d)); 1926 if (unsigned FrameworkOffset = 1927 endian::readNext<uint32_t, little, unaligned>(d)) { 1928 // The framework offset is 1 greater than the actual offset, 1929 // since 0 is used as an indicator for "no framework name". 1930 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1931 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1932 } 1933 1934 assert((End - d) % 4 == 0 && 1935 "Wrong data length in HeaderFileInfo deserialization"); 1936 while (d != End) { 1937 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1938 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1939 LocalSMID >>= 2; 1940 1941 // This header is part of a module. Associate it with the module to enable 1942 // implicit module import. 1943 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1944 Module *Mod = Reader.getSubmodule(GlobalSMID); 1945 FileManager &FileMgr = Reader.getFileManager(); 1946 ModuleMap &ModMap = 1947 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1948 1949 std::string Filename = std::string(key.Filename); 1950 if (key.Imported) 1951 Reader.ResolveImportedPath(M, Filename); 1952 // FIXME: NameAsWritten 1953 Module::Header H = {std::string(key.Filename), "", 1954 *FileMgr.getFile(Filename)}; 1955 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1956 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1957 } 1958 1959 // This HeaderFileInfo was externally loaded. 1960 HFI.External = true; 1961 HFI.IsValid = true; 1962 return HFI; 1963 } 1964 1965 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1966 uint32_t MacroDirectivesOffset) { 1967 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1968 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1969 } 1970 1971 void ASTReader::ReadDefinedMacros() { 1972 // Note that we are loading defined macros. 1973 Deserializing Macros(this); 1974 1975 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1976 BitstreamCursor &MacroCursor = I.MacroCursor; 1977 1978 // If there was no preprocessor block, skip this file. 1979 if (MacroCursor.getBitcodeBytes().empty()) 1980 continue; 1981 1982 BitstreamCursor Cursor = MacroCursor; 1983 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1984 Error(std::move(Err)); 1985 return; 1986 } 1987 1988 RecordData Record; 1989 while (true) { 1990 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1991 if (!MaybeE) { 1992 Error(MaybeE.takeError()); 1993 return; 1994 } 1995 llvm::BitstreamEntry E = MaybeE.get(); 1996 1997 switch (E.Kind) { 1998 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1999 case llvm::BitstreamEntry::Error: 2000 Error("malformed block record in AST file"); 2001 return; 2002 case llvm::BitstreamEntry::EndBlock: 2003 goto NextCursor; 2004 2005 case llvm::BitstreamEntry::Record: { 2006 Record.clear(); 2007 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 2008 if (!MaybeRecord) { 2009 Error(MaybeRecord.takeError()); 2010 return; 2011 } 2012 switch (MaybeRecord.get()) { 2013 default: // Default behavior: ignore. 2014 break; 2015 2016 case PP_MACRO_OBJECT_LIKE: 2017 case PP_MACRO_FUNCTION_LIKE: { 2018 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 2019 if (II->isOutOfDate()) 2020 updateOutOfDateIdentifier(*II); 2021 break; 2022 } 2023 2024 case PP_TOKEN: 2025 // Ignore tokens. 2026 break; 2027 } 2028 break; 2029 } 2030 } 2031 } 2032 NextCursor: ; 2033 } 2034 } 2035 2036 namespace { 2037 2038 /// Visitor class used to look up identifirs in an AST file. 2039 class IdentifierLookupVisitor { 2040 StringRef Name; 2041 unsigned NameHash; 2042 unsigned PriorGeneration; 2043 unsigned &NumIdentifierLookups; 2044 unsigned &NumIdentifierLookupHits; 2045 IdentifierInfo *Found = nullptr; 2046 2047 public: 2048 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2049 unsigned &NumIdentifierLookups, 2050 unsigned &NumIdentifierLookupHits) 2051 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2052 PriorGeneration(PriorGeneration), 2053 NumIdentifierLookups(NumIdentifierLookups), 2054 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2055 2056 bool operator()(ModuleFile &M) { 2057 // If we've already searched this module file, skip it now. 2058 if (M.Generation <= PriorGeneration) 2059 return true; 2060 2061 ASTIdentifierLookupTable *IdTable 2062 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2063 if (!IdTable) 2064 return false; 2065 2066 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2067 Found); 2068 ++NumIdentifierLookups; 2069 ASTIdentifierLookupTable::iterator Pos = 2070 IdTable->find_hashed(Name, NameHash, &Trait); 2071 if (Pos == IdTable->end()) 2072 return false; 2073 2074 // Dereferencing the iterator has the effect of building the 2075 // IdentifierInfo node and populating it with the various 2076 // declarations it needs. 2077 ++NumIdentifierLookupHits; 2078 Found = *Pos; 2079 return true; 2080 } 2081 2082 // Retrieve the identifier info found within the module 2083 // files. 2084 IdentifierInfo *getIdentifierInfo() const { return Found; } 2085 }; 2086 2087 } // namespace 2088 2089 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2090 // Note that we are loading an identifier. 2091 Deserializing AnIdentifier(this); 2092 2093 unsigned PriorGeneration = 0; 2094 if (getContext().getLangOpts().Modules) 2095 PriorGeneration = IdentifierGeneration[&II]; 2096 2097 // If there is a global index, look there first to determine which modules 2098 // provably do not have any results for this identifier. 2099 GlobalModuleIndex::HitSet Hits; 2100 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2101 if (!loadGlobalIndex()) { 2102 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2103 HitsPtr = &Hits; 2104 } 2105 } 2106 2107 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2108 NumIdentifierLookups, 2109 NumIdentifierLookupHits); 2110 ModuleMgr.visit(Visitor, HitsPtr); 2111 markIdentifierUpToDate(&II); 2112 } 2113 2114 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2115 if (!II) 2116 return; 2117 2118 II->setOutOfDate(false); 2119 2120 // Update the generation for this identifier. 2121 if (getContext().getLangOpts().Modules) 2122 IdentifierGeneration[II] = getGeneration(); 2123 } 2124 2125 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2126 const PendingMacroInfo &PMInfo) { 2127 ModuleFile &M = *PMInfo.M; 2128 2129 BitstreamCursor &Cursor = M.MacroCursor; 2130 SavedStreamPosition SavedPosition(Cursor); 2131 if (llvm::Error Err = 2132 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2133 Error(std::move(Err)); 2134 return; 2135 } 2136 2137 struct ModuleMacroRecord { 2138 SubmoduleID SubModID; 2139 MacroInfo *MI; 2140 SmallVector<SubmoduleID, 8> Overrides; 2141 }; 2142 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2143 2144 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2145 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2146 // macro histroy. 2147 RecordData Record; 2148 while (true) { 2149 Expected<llvm::BitstreamEntry> MaybeEntry = 2150 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2151 if (!MaybeEntry) { 2152 Error(MaybeEntry.takeError()); 2153 return; 2154 } 2155 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2156 2157 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2158 Error("malformed block record in AST file"); 2159 return; 2160 } 2161 2162 Record.clear(); 2163 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2164 if (!MaybePP) { 2165 Error(MaybePP.takeError()); 2166 return; 2167 } 2168 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2169 case PP_MACRO_DIRECTIVE_HISTORY: 2170 break; 2171 2172 case PP_MODULE_MACRO: { 2173 ModuleMacros.push_back(ModuleMacroRecord()); 2174 auto &Info = ModuleMacros.back(); 2175 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2176 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2177 for (int I = 2, N = Record.size(); I != N; ++I) 2178 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2179 continue; 2180 } 2181 2182 default: 2183 Error("malformed block record in AST file"); 2184 return; 2185 } 2186 2187 // We found the macro directive history; that's the last record 2188 // for this macro. 2189 break; 2190 } 2191 2192 // Module macros are listed in reverse dependency order. 2193 { 2194 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2195 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2196 for (auto &MMR : ModuleMacros) { 2197 Overrides.clear(); 2198 for (unsigned ModID : MMR.Overrides) { 2199 Module *Mod = getSubmodule(ModID); 2200 auto *Macro = PP.getModuleMacro(Mod, II); 2201 assert(Macro && "missing definition for overridden macro"); 2202 Overrides.push_back(Macro); 2203 } 2204 2205 bool Inserted = false; 2206 Module *Owner = getSubmodule(MMR.SubModID); 2207 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2208 } 2209 } 2210 2211 // Don't read the directive history for a module; we don't have anywhere 2212 // to put it. 2213 if (M.isModule()) 2214 return; 2215 2216 // Deserialize the macro directives history in reverse source-order. 2217 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2218 unsigned Idx = 0, N = Record.size(); 2219 while (Idx < N) { 2220 MacroDirective *MD = nullptr; 2221 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2222 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2223 switch (K) { 2224 case MacroDirective::MD_Define: { 2225 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2226 MD = PP.AllocateDefMacroDirective(MI, Loc); 2227 break; 2228 } 2229 case MacroDirective::MD_Undefine: 2230 MD = PP.AllocateUndefMacroDirective(Loc); 2231 break; 2232 case MacroDirective::MD_Visibility: 2233 bool isPublic = Record[Idx++]; 2234 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2235 break; 2236 } 2237 2238 if (!Latest) 2239 Latest = MD; 2240 if (Earliest) 2241 Earliest->setPrevious(MD); 2242 Earliest = MD; 2243 } 2244 2245 if (Latest) 2246 PP.setLoadedMacroDirective(II, Earliest, Latest); 2247 } 2248 2249 bool ASTReader::shouldDisableValidationForFile( 2250 const serialization::ModuleFile &M) const { 2251 if (DisableValidationKind == DisableValidationForModuleKind::None) 2252 return false; 2253 2254 // If a PCH is loaded and validation is disabled for PCH then disable 2255 // validation for the PCH and the modules it loads. 2256 ModuleKind K = CurrentDeserializingModuleKind.value_or(M.Kind); 2257 2258 switch (K) { 2259 case MK_MainFile: 2260 case MK_Preamble: 2261 case MK_PCH: 2262 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH); 2263 case MK_ImplicitModule: 2264 case MK_ExplicitModule: 2265 case MK_PrebuiltModule: 2266 return bool(DisableValidationKind & DisableValidationForModuleKind::Module); 2267 } 2268 2269 return false; 2270 } 2271 2272 ASTReader::InputFileInfo 2273 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2274 // Go find this input file. 2275 BitstreamCursor &Cursor = F.InputFilesCursor; 2276 SavedStreamPosition SavedPosition(Cursor); 2277 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2278 // FIXME this drops errors on the floor. 2279 consumeError(std::move(Err)); 2280 } 2281 2282 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2283 if (!MaybeCode) { 2284 // FIXME this drops errors on the floor. 2285 consumeError(MaybeCode.takeError()); 2286 } 2287 unsigned Code = MaybeCode.get(); 2288 RecordData Record; 2289 StringRef Blob; 2290 2291 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2292 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2293 "invalid record type for input file"); 2294 else { 2295 // FIXME this drops errors on the floor. 2296 consumeError(Maybe.takeError()); 2297 } 2298 2299 assert(Record[0] == ID && "Bogus stored ID or offset"); 2300 InputFileInfo R; 2301 R.StoredSize = static_cast<off_t>(Record[1]); 2302 R.StoredTime = static_cast<time_t>(Record[2]); 2303 R.Overridden = static_cast<bool>(Record[3]); 2304 R.Transient = static_cast<bool>(Record[4]); 2305 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2306 R.Filename = std::string(Blob); 2307 ResolveImportedPath(F, R.Filename); 2308 2309 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2310 if (!MaybeEntry) // FIXME this drops errors on the floor. 2311 consumeError(MaybeEntry.takeError()); 2312 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2313 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2314 "expected record type for input file hash"); 2315 2316 Record.clear(); 2317 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2318 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2319 "invalid record type for input file hash"); 2320 else { 2321 // FIXME this drops errors on the floor. 2322 consumeError(Maybe.takeError()); 2323 } 2324 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2325 static_cast<uint64_t>(Record[0]); 2326 return R; 2327 } 2328 2329 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2330 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2331 // If this ID is bogus, just return an empty input file. 2332 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2333 return InputFile(); 2334 2335 // If we've already loaded this input file, return it. 2336 if (F.InputFilesLoaded[ID-1].getFile()) 2337 return F.InputFilesLoaded[ID-1]; 2338 2339 if (F.InputFilesLoaded[ID-1].isNotFound()) 2340 return InputFile(); 2341 2342 // Go find this input file. 2343 BitstreamCursor &Cursor = F.InputFilesCursor; 2344 SavedStreamPosition SavedPosition(Cursor); 2345 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2346 // FIXME this drops errors on the floor. 2347 consumeError(std::move(Err)); 2348 } 2349 2350 InputFileInfo FI = readInputFileInfo(F, ID); 2351 off_t StoredSize = FI.StoredSize; 2352 time_t StoredTime = FI.StoredTime; 2353 bool Overridden = FI.Overridden; 2354 bool Transient = FI.Transient; 2355 StringRef Filename = FI.Filename; 2356 uint64_t StoredContentHash = FI.ContentHash; 2357 2358 OptionalFileEntryRefDegradesToFileEntryPtr File = 2359 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2360 2361 // If we didn't find the file, resolve it relative to the 2362 // original directory from which this AST file was created. 2363 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2364 F.OriginalDir != F.BaseDirectory) { 2365 std::string Resolved = resolveFileRelativeToOriginalDir( 2366 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2367 if (!Resolved.empty()) 2368 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2369 } 2370 2371 // For an overridden file, create a virtual file with the stored 2372 // size/timestamp. 2373 if ((Overridden || Transient) && !File) 2374 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2375 2376 if (!File) { 2377 if (Complain) { 2378 std::string ErrorStr = "could not find file '"; 2379 ErrorStr += Filename; 2380 ErrorStr += "' referenced by AST file '"; 2381 ErrorStr += F.FileName; 2382 ErrorStr += "'"; 2383 Error(ErrorStr); 2384 } 2385 // Record that we didn't find the file. 2386 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2387 return InputFile(); 2388 } 2389 2390 // Check if there was a request to override the contents of the file 2391 // that was part of the precompiled header. Overriding such a file 2392 // can lead to problems when lexing using the source locations from the 2393 // PCH. 2394 SourceManager &SM = getSourceManager(); 2395 // FIXME: Reject if the overrides are different. 2396 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2397 if (Complain) 2398 Error(diag::err_fe_pch_file_overridden, Filename); 2399 2400 // After emitting the diagnostic, bypass the overriding file to recover 2401 // (this creates a separate FileEntry). 2402 File = SM.bypassFileContentsOverride(*File); 2403 if (!File) { 2404 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2405 return InputFile(); 2406 } 2407 } 2408 2409 struct Change { 2410 enum ModificationKind { 2411 Size, 2412 ModTime, 2413 Content, 2414 None, 2415 } Kind; 2416 llvm::Optional<int64_t> Old = llvm::None; 2417 llvm::Optional<int64_t> New = llvm::None; 2418 }; 2419 auto HasInputFileChanged = [&]() { 2420 if (StoredSize != File->getSize()) 2421 return Change{Change::Size, StoredSize, File->getSize()}; 2422 if (!shouldDisableValidationForFile(F) && StoredTime && 2423 StoredTime != File->getModificationTime()) { 2424 Change MTimeChange = {Change::ModTime, StoredTime, 2425 File->getModificationTime()}; 2426 2427 // In case the modification time changes but not the content, 2428 // accept the cached file as legit. 2429 if (ValidateASTInputFilesContent && 2430 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2431 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2432 if (!MemBuffOrError) { 2433 if (!Complain) 2434 return MTimeChange; 2435 std::string ErrorStr = "could not get buffer for file '"; 2436 ErrorStr += File->getName(); 2437 ErrorStr += "'"; 2438 Error(ErrorStr); 2439 return MTimeChange; 2440 } 2441 2442 // FIXME: hash_value is not guaranteed to be stable! 2443 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2444 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2445 return Change{Change::None}; 2446 2447 return Change{Change::Content}; 2448 } 2449 return MTimeChange; 2450 } 2451 return Change{Change::None}; 2452 }; 2453 2454 bool IsOutOfDate = false; 2455 auto FileChange = HasInputFileChanged(); 2456 // For an overridden file, there is nothing to validate. 2457 if (!Overridden && FileChange.Kind != Change::None) { 2458 if (Complain && !Diags.isDiagnosticInFlight()) { 2459 // Build a list of the PCH imports that got us here (in reverse). 2460 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2461 while (!ImportStack.back()->ImportedBy.empty()) 2462 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2463 2464 // The top-level PCH is stale. 2465 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2466 Diag(diag::err_fe_ast_file_modified) 2467 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2468 << TopLevelPCHName << FileChange.Kind 2469 << (FileChange.Old && FileChange.New) 2470 << llvm::itostr(FileChange.Old.value_or(0)) 2471 << llvm::itostr(FileChange.New.value_or(0)); 2472 2473 // Print the import stack. 2474 if (ImportStack.size() > 1) { 2475 Diag(diag::note_pch_required_by) 2476 << Filename << ImportStack[0]->FileName; 2477 for (unsigned I = 1; I < ImportStack.size(); ++I) 2478 Diag(diag::note_pch_required_by) 2479 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2480 } 2481 2482 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2483 } 2484 2485 IsOutOfDate = true; 2486 } 2487 // FIXME: If the file is overridden and we've already opened it, 2488 // issue an error (or split it into a separate FileEntry). 2489 2490 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2491 2492 // Note that we've loaded this input file. 2493 F.InputFilesLoaded[ID-1] = IF; 2494 return IF; 2495 } 2496 2497 /// If we are loading a relocatable PCH or module file, and the filename 2498 /// is not an absolute path, add the system or module root to the beginning of 2499 /// the file name. 2500 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2501 // Resolve relative to the base directory, if we have one. 2502 if (!M.BaseDirectory.empty()) 2503 return ResolveImportedPath(Filename, M.BaseDirectory); 2504 } 2505 2506 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2507 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2508 return; 2509 2510 SmallString<128> Buffer; 2511 llvm::sys::path::append(Buffer, Prefix, Filename); 2512 Filename.assign(Buffer.begin(), Buffer.end()); 2513 } 2514 2515 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2516 switch (ARR) { 2517 case ASTReader::Failure: return true; 2518 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2519 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2520 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2521 case ASTReader::ConfigurationMismatch: 2522 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2523 case ASTReader::HadErrors: return true; 2524 case ASTReader::Success: return false; 2525 } 2526 2527 llvm_unreachable("unknown ASTReadResult"); 2528 } 2529 2530 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2531 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2532 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2533 std::string &SuggestedPredefines) { 2534 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2535 // FIXME this drops errors on the floor. 2536 consumeError(std::move(Err)); 2537 return Failure; 2538 } 2539 2540 // Read all of the records in the options block. 2541 RecordData Record; 2542 ASTReadResult Result = Success; 2543 while (true) { 2544 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2545 if (!MaybeEntry) { 2546 // FIXME this drops errors on the floor. 2547 consumeError(MaybeEntry.takeError()); 2548 return Failure; 2549 } 2550 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2551 2552 switch (Entry.Kind) { 2553 case llvm::BitstreamEntry::Error: 2554 case llvm::BitstreamEntry::SubBlock: 2555 return Failure; 2556 2557 case llvm::BitstreamEntry::EndBlock: 2558 return Result; 2559 2560 case llvm::BitstreamEntry::Record: 2561 // The interesting case. 2562 break; 2563 } 2564 2565 // Read and process a record. 2566 Record.clear(); 2567 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2568 if (!MaybeRecordType) { 2569 // FIXME this drops errors on the floor. 2570 consumeError(MaybeRecordType.takeError()); 2571 return Failure; 2572 } 2573 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2574 case LANGUAGE_OPTIONS: { 2575 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2576 if (ParseLanguageOptions(Record, Complain, Listener, 2577 AllowCompatibleConfigurationMismatch)) 2578 Result = ConfigurationMismatch; 2579 break; 2580 } 2581 2582 case TARGET_OPTIONS: { 2583 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2584 if (ParseTargetOptions(Record, Complain, Listener, 2585 AllowCompatibleConfigurationMismatch)) 2586 Result = ConfigurationMismatch; 2587 break; 2588 } 2589 2590 case FILE_SYSTEM_OPTIONS: { 2591 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2592 if (!AllowCompatibleConfigurationMismatch && 2593 ParseFileSystemOptions(Record, Complain, Listener)) 2594 Result = ConfigurationMismatch; 2595 break; 2596 } 2597 2598 case HEADER_SEARCH_OPTIONS: { 2599 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2600 if (!AllowCompatibleConfigurationMismatch && 2601 ParseHeaderSearchOptions(Record, Complain, Listener)) 2602 Result = ConfigurationMismatch; 2603 break; 2604 } 2605 2606 case PREPROCESSOR_OPTIONS: 2607 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2608 if (!AllowCompatibleConfigurationMismatch && 2609 ParsePreprocessorOptions(Record, Complain, Listener, 2610 SuggestedPredefines)) 2611 Result = ConfigurationMismatch; 2612 break; 2613 } 2614 } 2615 } 2616 2617 ASTReader::ASTReadResult 2618 ASTReader::ReadControlBlock(ModuleFile &F, 2619 SmallVectorImpl<ImportedModule> &Loaded, 2620 const ModuleFile *ImportedBy, 2621 unsigned ClientLoadCapabilities) { 2622 BitstreamCursor &Stream = F.Stream; 2623 2624 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2625 Error(std::move(Err)); 2626 return Failure; 2627 } 2628 2629 // Lambda to read the unhashed control block the first time it's called. 2630 // 2631 // For PCM files, the unhashed control block cannot be read until after the 2632 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2633 // need to look ahead before reading the IMPORTS record. For consistency, 2634 // this block is always read somehow (see BitstreamEntry::EndBlock). 2635 bool HasReadUnhashedControlBlock = false; 2636 auto readUnhashedControlBlockOnce = [&]() { 2637 if (!HasReadUnhashedControlBlock) { 2638 HasReadUnhashedControlBlock = true; 2639 if (ASTReadResult Result = 2640 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2641 return Result; 2642 } 2643 return Success; 2644 }; 2645 2646 bool DisableValidation = shouldDisableValidationForFile(F); 2647 2648 // Read all of the records and blocks in the control block. 2649 RecordData Record; 2650 unsigned NumInputs = 0; 2651 unsigned NumUserInputs = 0; 2652 StringRef BaseDirectoryAsWritten; 2653 while (true) { 2654 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2655 if (!MaybeEntry) { 2656 Error(MaybeEntry.takeError()); 2657 return Failure; 2658 } 2659 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2660 2661 switch (Entry.Kind) { 2662 case llvm::BitstreamEntry::Error: 2663 Error("malformed block record in AST file"); 2664 return Failure; 2665 case llvm::BitstreamEntry::EndBlock: { 2666 // Validate the module before returning. This call catches an AST with 2667 // no module name and no imports. 2668 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2669 return Result; 2670 2671 // Validate input files. 2672 const HeaderSearchOptions &HSOpts = 2673 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2674 2675 // All user input files reside at the index range [0, NumUserInputs), and 2676 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2677 // loaded module files, ignore missing inputs. 2678 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2679 F.Kind != MK_PrebuiltModule) { 2680 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2681 2682 // If we are reading a module, we will create a verification timestamp, 2683 // so we verify all input files. Otherwise, verify only user input 2684 // files. 2685 2686 unsigned N = NumUserInputs; 2687 if (ValidateSystemInputs || 2688 (HSOpts.ModulesValidateOncePerBuildSession && 2689 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2690 F.Kind == MK_ImplicitModule)) 2691 N = NumInputs; 2692 2693 for (unsigned I = 0; I < N; ++I) { 2694 InputFile IF = getInputFile(F, I+1, Complain); 2695 if (!IF.getFile() || IF.isOutOfDate()) 2696 return OutOfDate; 2697 } 2698 } 2699 2700 if (Listener) 2701 Listener->visitModuleFile(F.FileName, F.Kind); 2702 2703 if (Listener && Listener->needsInputFileVisitation()) { 2704 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2705 : NumUserInputs; 2706 for (unsigned I = 0; I < N; ++I) { 2707 bool IsSystem = I >= NumUserInputs; 2708 InputFileInfo FI = readInputFileInfo(F, I+1); 2709 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2710 F.Kind == MK_ExplicitModule || 2711 F.Kind == MK_PrebuiltModule); 2712 } 2713 } 2714 2715 return Success; 2716 } 2717 2718 case llvm::BitstreamEntry::SubBlock: 2719 switch (Entry.ID) { 2720 case INPUT_FILES_BLOCK_ID: 2721 F.InputFilesCursor = Stream; 2722 if (llvm::Error Err = Stream.SkipBlock()) { 2723 Error(std::move(Err)); 2724 return Failure; 2725 } 2726 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2727 Error("malformed block record in AST file"); 2728 return Failure; 2729 } 2730 continue; 2731 2732 case OPTIONS_BLOCK_ID: 2733 // If we're reading the first module for this group, check its options 2734 // are compatible with ours. For modules it imports, no further checking 2735 // is required, because we checked them when we built it. 2736 if (Listener && !ImportedBy) { 2737 // Should we allow the configuration of the module file to differ from 2738 // the configuration of the current translation unit in a compatible 2739 // way? 2740 // 2741 // FIXME: Allow this for files explicitly specified with -include-pch. 2742 bool AllowCompatibleConfigurationMismatch = 2743 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2744 2745 ASTReadResult Result = 2746 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2747 AllowCompatibleConfigurationMismatch, *Listener, 2748 SuggestedPredefines); 2749 if (Result == Failure) { 2750 Error("malformed block record in AST file"); 2751 return Result; 2752 } 2753 2754 if (DisableValidation || 2755 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2756 Result = Success; 2757 2758 // If we can't load the module, exit early since we likely 2759 // will rebuild the module anyway. The stream may be in the 2760 // middle of a block. 2761 if (Result != Success) 2762 return Result; 2763 } else if (llvm::Error Err = Stream.SkipBlock()) { 2764 Error(std::move(Err)); 2765 return Failure; 2766 } 2767 continue; 2768 2769 default: 2770 if (llvm::Error Err = Stream.SkipBlock()) { 2771 Error(std::move(Err)); 2772 return Failure; 2773 } 2774 continue; 2775 } 2776 2777 case llvm::BitstreamEntry::Record: 2778 // The interesting case. 2779 break; 2780 } 2781 2782 // Read and process a record. 2783 Record.clear(); 2784 StringRef Blob; 2785 Expected<unsigned> MaybeRecordType = 2786 Stream.readRecord(Entry.ID, Record, &Blob); 2787 if (!MaybeRecordType) { 2788 Error(MaybeRecordType.takeError()); 2789 return Failure; 2790 } 2791 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2792 case METADATA: { 2793 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2794 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2795 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2796 : diag::err_pch_version_too_new); 2797 return VersionMismatch; 2798 } 2799 2800 bool hasErrors = Record[6]; 2801 if (hasErrors && !DisableValidation) { 2802 // If requested by the caller and the module hasn't already been read 2803 // or compiled, mark modules on error as out-of-date. 2804 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) && 2805 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2806 return OutOfDate; 2807 2808 if (!AllowASTWithCompilerErrors) { 2809 Diag(diag::err_pch_with_compiler_errors); 2810 return HadErrors; 2811 } 2812 } 2813 if (hasErrors) { 2814 Diags.ErrorOccurred = true; 2815 Diags.UncompilableErrorOccurred = true; 2816 Diags.UnrecoverableErrorOccurred = true; 2817 } 2818 2819 F.RelocatablePCH = Record[4]; 2820 // Relative paths in a relocatable PCH are relative to our sysroot. 2821 if (F.RelocatablePCH) 2822 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2823 2824 F.HasTimestamps = Record[5]; 2825 2826 const std::string &CurBranch = getClangFullRepositoryVersion(); 2827 StringRef ASTBranch = Blob; 2828 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2829 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2830 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2831 return VersionMismatch; 2832 } 2833 break; 2834 } 2835 2836 case IMPORTS: { 2837 // Validate the AST before processing any imports (otherwise, untangling 2838 // them can be error-prone and expensive). A module will have a name and 2839 // will already have been validated, but this catches the PCH case. 2840 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2841 return Result; 2842 2843 // Load each of the imported PCH files. 2844 unsigned Idx = 0, N = Record.size(); 2845 while (Idx < N) { 2846 // Read information about the AST file. 2847 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2848 // The import location will be the local one for now; we will adjust 2849 // all import locations of module imports after the global source 2850 // location info are setup, in ReadAST. 2851 SourceLocation ImportLoc = 2852 ReadUntranslatedSourceLocation(Record[Idx++]); 2853 off_t StoredSize = (off_t)Record[Idx++]; 2854 time_t StoredModTime = (time_t)Record[Idx++]; 2855 auto FirstSignatureByte = Record.begin() + Idx; 2856 ASTFileSignature StoredSignature = ASTFileSignature::create( 2857 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2858 Idx += ASTFileSignature::size; 2859 2860 std::string ImportedName = ReadString(Record, Idx); 2861 std::string ImportedFile; 2862 2863 // For prebuilt and explicit modules first consult the file map for 2864 // an override. Note that here we don't search prebuilt module 2865 // directories, only the explicit name to file mappings. Also, we will 2866 // still verify the size/signature making sure it is essentially the 2867 // same file but perhaps in a different location. 2868 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2869 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2870 ImportedName, /*FileMapOnly*/ true); 2871 2872 if (ImportedFile.empty()) 2873 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2874 // ModuleCache as when writing. 2875 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2876 else 2877 SkipPath(Record, Idx); 2878 2879 // If our client can't cope with us being out of date, we can't cope with 2880 // our dependency being missing. 2881 unsigned Capabilities = ClientLoadCapabilities; 2882 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2883 Capabilities &= ~ARR_Missing; 2884 2885 // Load the AST file. 2886 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2887 Loaded, StoredSize, StoredModTime, 2888 StoredSignature, Capabilities); 2889 2890 // If we diagnosed a problem, produce a backtrace. 2891 bool recompilingFinalized = 2892 Result == OutOfDate && (Capabilities & ARR_OutOfDate) && 2893 getModuleManager().getModuleCache().isPCMFinal(F.FileName); 2894 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized) 2895 Diag(diag::note_module_file_imported_by) 2896 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2897 if (recompilingFinalized) 2898 Diag(diag::note_module_file_conflict); 2899 2900 switch (Result) { 2901 case Failure: return Failure; 2902 // If we have to ignore the dependency, we'll have to ignore this too. 2903 case Missing: 2904 case OutOfDate: return OutOfDate; 2905 case VersionMismatch: return VersionMismatch; 2906 case ConfigurationMismatch: return ConfigurationMismatch; 2907 case HadErrors: return HadErrors; 2908 case Success: break; 2909 } 2910 } 2911 break; 2912 } 2913 2914 case ORIGINAL_FILE: 2915 F.OriginalSourceFileID = FileID::get(Record[0]); 2916 F.ActualOriginalSourceFileName = std::string(Blob); 2917 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2918 ResolveImportedPath(F, F.OriginalSourceFileName); 2919 break; 2920 2921 case ORIGINAL_FILE_ID: 2922 F.OriginalSourceFileID = FileID::get(Record[0]); 2923 break; 2924 2925 case ORIGINAL_PCH_DIR: 2926 F.OriginalDir = std::string(Blob); 2927 ResolveImportedPath(F, F.OriginalDir); 2928 break; 2929 2930 case MODULE_NAME: 2931 F.ModuleName = std::string(Blob); 2932 Diag(diag::remark_module_import) 2933 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2934 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2935 if (Listener) 2936 Listener->ReadModuleName(F.ModuleName); 2937 2938 // Validate the AST as soon as we have a name so we can exit early on 2939 // failure. 2940 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2941 return Result; 2942 2943 break; 2944 2945 case MODULE_DIRECTORY: { 2946 // Save the BaseDirectory as written in the PCM for computing the module 2947 // filename for the ModuleCache. 2948 BaseDirectoryAsWritten = Blob; 2949 assert(!F.ModuleName.empty() && 2950 "MODULE_DIRECTORY found before MODULE_NAME"); 2951 // If we've already loaded a module map file covering this module, we may 2952 // have a better path for it (relative to the current build). 2953 Module *M = PP.getHeaderSearchInfo().lookupModule( 2954 F.ModuleName, SourceLocation(), /*AllowSearch*/ true, 2955 /*AllowExtraModuleMapSearch*/ true); 2956 if (M && M->Directory) { 2957 // If we're implicitly loading a module, the base directory can't 2958 // change between the build and use. 2959 // Don't emit module relocation error if we have -fno-validate-pch 2960 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 2961 DisableValidationForModuleKind::Module) && 2962 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2963 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2964 if (!BuildDir || *BuildDir != M->Directory) { 2965 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2966 Diag(diag::err_imported_module_relocated) 2967 << F.ModuleName << Blob << M->Directory->getName(); 2968 return OutOfDate; 2969 } 2970 } 2971 F.BaseDirectory = std::string(M->Directory->getName()); 2972 } else { 2973 F.BaseDirectory = std::string(Blob); 2974 } 2975 break; 2976 } 2977 2978 case MODULE_MAP_FILE: 2979 if (ASTReadResult Result = 2980 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2981 return Result; 2982 break; 2983 2984 case INPUT_FILE_OFFSETS: 2985 NumInputs = Record[0]; 2986 NumUserInputs = Record[1]; 2987 F.InputFileOffsets = 2988 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2989 F.InputFilesLoaded.resize(NumInputs); 2990 F.NumUserInputFiles = NumUserInputs; 2991 break; 2992 } 2993 } 2994 } 2995 2996 void ASTReader::readIncludedFiles(ModuleFile &F, StringRef Blob, 2997 Preprocessor &PP) { 2998 using namespace llvm::support; 2999 3000 const unsigned char *D = (const unsigned char *)Blob.data(); 3001 unsigned FileCount = endian::readNext<uint32_t, little, unaligned>(D); 3002 3003 for (unsigned I = 0; I < FileCount; ++I) { 3004 size_t ID = endian::readNext<uint32_t, little, unaligned>(D); 3005 InputFileInfo IFI = readInputFileInfo(F, ID); 3006 if (llvm::ErrorOr<const FileEntry *> File = 3007 PP.getFileManager().getFile(IFI.Filename)) 3008 PP.getIncludedFiles().insert(*File); 3009 } 3010 } 3011 3012 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, 3013 unsigned ClientLoadCapabilities) { 3014 BitstreamCursor &Stream = F.Stream; 3015 3016 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) 3017 return Err; 3018 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 3019 3020 // Read all of the records and blocks for the AST file. 3021 RecordData Record; 3022 while (true) { 3023 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 3024 if (!MaybeEntry) 3025 return MaybeEntry.takeError(); 3026 llvm::BitstreamEntry Entry = MaybeEntry.get(); 3027 3028 switch (Entry.Kind) { 3029 case llvm::BitstreamEntry::Error: 3030 return llvm::createStringError( 3031 std::errc::illegal_byte_sequence, 3032 "error at end of module block in AST file"); 3033 case llvm::BitstreamEntry::EndBlock: 3034 // Outside of C++, we do not store a lookup map for the translation unit. 3035 // Instead, mark it as needing a lookup map to be built if this module 3036 // contains any declarations lexically within it (which it always does!). 3037 // This usually has no cost, since we very rarely need the lookup map for 3038 // the translation unit outside C++. 3039 if (ASTContext *Ctx = ContextObj) { 3040 DeclContext *DC = Ctx->getTranslationUnitDecl(); 3041 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 3042 DC->setMustBuildLookupTable(); 3043 } 3044 3045 return llvm::Error::success(); 3046 case llvm::BitstreamEntry::SubBlock: 3047 switch (Entry.ID) { 3048 case DECLTYPES_BLOCK_ID: 3049 // We lazily load the decls block, but we want to set up the 3050 // DeclsCursor cursor to point into it. Clone our current bitcode 3051 // cursor to it, enter the block and read the abbrevs in that block. 3052 // With the main cursor, we just skip over it. 3053 F.DeclsCursor = Stream; 3054 if (llvm::Error Err = Stream.SkipBlock()) 3055 return Err; 3056 if (llvm::Error Err = ReadBlockAbbrevs( 3057 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset)) 3058 return Err; 3059 break; 3060 3061 case PREPROCESSOR_BLOCK_ID: 3062 F.MacroCursor = Stream; 3063 if (!PP.getExternalSource()) 3064 PP.setExternalSource(this); 3065 3066 if (llvm::Error Err = Stream.SkipBlock()) 3067 return Err; 3068 if (llvm::Error Err = 3069 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) 3070 return Err; 3071 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3072 break; 3073 3074 case PREPROCESSOR_DETAIL_BLOCK_ID: 3075 F.PreprocessorDetailCursor = Stream; 3076 3077 if (llvm::Error Err = Stream.SkipBlock()) { 3078 return Err; 3079 } 3080 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3081 PREPROCESSOR_DETAIL_BLOCK_ID)) 3082 return Err; 3083 F.PreprocessorDetailStartOffset 3084 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3085 3086 if (!PP.getPreprocessingRecord()) 3087 PP.createPreprocessingRecord(); 3088 if (!PP.getPreprocessingRecord()->getExternalSource()) 3089 PP.getPreprocessingRecord()->SetExternalSource(*this); 3090 break; 3091 3092 case SOURCE_MANAGER_BLOCK_ID: 3093 if (llvm::Error Err = ReadSourceManagerBlock(F)) 3094 return Err; 3095 break; 3096 3097 case SUBMODULE_BLOCK_ID: 3098 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3099 return Err; 3100 break; 3101 3102 case COMMENTS_BLOCK_ID: { 3103 BitstreamCursor C = Stream; 3104 3105 if (llvm::Error Err = Stream.SkipBlock()) 3106 return Err; 3107 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) 3108 return Err; 3109 CommentsCursors.push_back(std::make_pair(C, &F)); 3110 break; 3111 } 3112 3113 default: 3114 if (llvm::Error Err = Stream.SkipBlock()) 3115 return Err; 3116 break; 3117 } 3118 continue; 3119 3120 case llvm::BitstreamEntry::Record: 3121 // The interesting case. 3122 break; 3123 } 3124 3125 // Read and process a record. 3126 Record.clear(); 3127 StringRef Blob; 3128 Expected<unsigned> MaybeRecordType = 3129 Stream.readRecord(Entry.ID, Record, &Blob); 3130 if (!MaybeRecordType) 3131 return MaybeRecordType.takeError(); 3132 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3133 3134 // If we're not loading an AST context, we don't care about most records. 3135 if (!ContextObj) { 3136 switch (RecordType) { 3137 case IDENTIFIER_TABLE: 3138 case IDENTIFIER_OFFSET: 3139 case INTERESTING_IDENTIFIERS: 3140 case STATISTICS: 3141 case PP_ASSUME_NONNULL_LOC: 3142 case PP_CONDITIONAL_STACK: 3143 case PP_COUNTER_VALUE: 3144 case SOURCE_LOCATION_OFFSETS: 3145 case MODULE_OFFSET_MAP: 3146 case SOURCE_MANAGER_LINE_TABLE: 3147 case SOURCE_LOCATION_PRELOADS: 3148 case PPD_ENTITIES_OFFSETS: 3149 case HEADER_SEARCH_TABLE: 3150 case IMPORTED_MODULES: 3151 case MACRO_OFFSET: 3152 break; 3153 default: 3154 continue; 3155 } 3156 } 3157 3158 switch (RecordType) { 3159 default: // Default behavior: ignore. 3160 break; 3161 3162 case TYPE_OFFSET: { 3163 if (F.LocalNumTypes != 0) 3164 return llvm::createStringError( 3165 std::errc::illegal_byte_sequence, 3166 "duplicate TYPE_OFFSET record in AST file"); 3167 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3168 F.LocalNumTypes = Record[0]; 3169 unsigned LocalBaseTypeIndex = Record[1]; 3170 F.BaseTypeIndex = getTotalNumTypes(); 3171 3172 if (F.LocalNumTypes > 0) { 3173 // Introduce the global -> local mapping for types within this module. 3174 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3175 3176 // Introduce the local -> global mapping for types within this module. 3177 F.TypeRemap.insertOrReplace( 3178 std::make_pair(LocalBaseTypeIndex, 3179 F.BaseTypeIndex - LocalBaseTypeIndex)); 3180 3181 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3182 } 3183 break; 3184 } 3185 3186 case DECL_OFFSET: { 3187 if (F.LocalNumDecls != 0) 3188 return llvm::createStringError( 3189 std::errc::illegal_byte_sequence, 3190 "duplicate DECL_OFFSET record in AST file"); 3191 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3192 F.LocalNumDecls = Record[0]; 3193 unsigned LocalBaseDeclID = Record[1]; 3194 F.BaseDeclID = getTotalNumDecls(); 3195 3196 if (F.LocalNumDecls > 0) { 3197 // Introduce the global -> local mapping for declarations within this 3198 // module. 3199 GlobalDeclMap.insert( 3200 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3201 3202 // Introduce the local -> global mapping for declarations within this 3203 // module. 3204 F.DeclRemap.insertOrReplace( 3205 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3206 3207 // Introduce the global -> local mapping for declarations within this 3208 // module. 3209 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3210 3211 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3212 } 3213 break; 3214 } 3215 3216 case TU_UPDATE_LEXICAL: { 3217 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3218 LexicalContents Contents( 3219 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3220 Blob.data()), 3221 static_cast<unsigned int>(Blob.size() / 4)); 3222 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3223 TU->setHasExternalLexicalStorage(true); 3224 break; 3225 } 3226 3227 case UPDATE_VISIBLE: { 3228 unsigned Idx = 0; 3229 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3230 auto *Data = (const unsigned char*)Blob.data(); 3231 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3232 // If we've already loaded the decl, perform the updates when we finish 3233 // loading this block. 3234 if (Decl *D = GetExistingDecl(ID)) 3235 PendingUpdateRecords.push_back( 3236 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3237 break; 3238 } 3239 3240 case IDENTIFIER_TABLE: 3241 F.IdentifierTableData = 3242 reinterpret_cast<const unsigned char *>(Blob.data()); 3243 if (Record[0]) { 3244 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3245 F.IdentifierTableData + Record[0], 3246 F.IdentifierTableData + sizeof(uint32_t), 3247 F.IdentifierTableData, 3248 ASTIdentifierLookupTrait(*this, F)); 3249 3250 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3251 } 3252 break; 3253 3254 case IDENTIFIER_OFFSET: { 3255 if (F.LocalNumIdentifiers != 0) 3256 return llvm::createStringError( 3257 std::errc::illegal_byte_sequence, 3258 "duplicate IDENTIFIER_OFFSET record in AST file"); 3259 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3260 F.LocalNumIdentifiers = Record[0]; 3261 unsigned LocalBaseIdentifierID = Record[1]; 3262 F.BaseIdentifierID = getTotalNumIdentifiers(); 3263 3264 if (F.LocalNumIdentifiers > 0) { 3265 // Introduce the global -> local mapping for identifiers within this 3266 // module. 3267 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3268 &F)); 3269 3270 // Introduce the local -> global mapping for identifiers within this 3271 // module. 3272 F.IdentifierRemap.insertOrReplace( 3273 std::make_pair(LocalBaseIdentifierID, 3274 F.BaseIdentifierID - LocalBaseIdentifierID)); 3275 3276 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3277 + F.LocalNumIdentifiers); 3278 } 3279 break; 3280 } 3281 3282 case INTERESTING_IDENTIFIERS: 3283 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3284 break; 3285 3286 case EAGERLY_DESERIALIZED_DECLS: 3287 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3288 // about "interesting" decls (for instance, if we're building a module). 3289 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3290 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3291 break; 3292 3293 case MODULAR_CODEGEN_DECLS: 3294 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3295 // them (ie: if we're not codegenerating this module). 3296 if (F.Kind == MK_MainFile || 3297 getContext().getLangOpts().BuildingPCHWithObjectFile) 3298 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3299 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3300 break; 3301 3302 case SPECIAL_TYPES: 3303 if (SpecialTypes.empty()) { 3304 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3305 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3306 break; 3307 } 3308 3309 if (SpecialTypes.size() != Record.size()) 3310 return llvm::createStringError(std::errc::illegal_byte_sequence, 3311 "invalid special-types record"); 3312 3313 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3314 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3315 if (!SpecialTypes[I]) 3316 SpecialTypes[I] = ID; 3317 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3318 // merge step? 3319 } 3320 break; 3321 3322 case STATISTICS: 3323 TotalNumStatements += Record[0]; 3324 TotalNumMacros += Record[1]; 3325 TotalLexicalDeclContexts += Record[2]; 3326 TotalVisibleDeclContexts += Record[3]; 3327 break; 3328 3329 case UNUSED_FILESCOPED_DECLS: 3330 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3331 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3332 break; 3333 3334 case DELEGATING_CTORS: 3335 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3336 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3337 break; 3338 3339 case WEAK_UNDECLARED_IDENTIFIERS: 3340 if (Record.size() % 3 != 0) 3341 return llvm::createStringError(std::errc::illegal_byte_sequence, 3342 "invalid weak identifiers record"); 3343 3344 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3345 // files. This isn't the way to do it :) 3346 WeakUndeclaredIdentifiers.clear(); 3347 3348 // Translate the weak, undeclared identifiers into global IDs. 3349 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3350 WeakUndeclaredIdentifiers.push_back( 3351 getGlobalIdentifierID(F, Record[I++])); 3352 WeakUndeclaredIdentifiers.push_back( 3353 getGlobalIdentifierID(F, Record[I++])); 3354 WeakUndeclaredIdentifiers.push_back( 3355 ReadSourceLocation(F, Record, I).getRawEncoding()); 3356 } 3357 break; 3358 3359 case SELECTOR_OFFSETS: { 3360 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3361 F.LocalNumSelectors = Record[0]; 3362 unsigned LocalBaseSelectorID = Record[1]; 3363 F.BaseSelectorID = getTotalNumSelectors(); 3364 3365 if (F.LocalNumSelectors > 0) { 3366 // Introduce the global -> local mapping for selectors within this 3367 // module. 3368 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3369 3370 // Introduce the local -> global mapping for selectors within this 3371 // module. 3372 F.SelectorRemap.insertOrReplace( 3373 std::make_pair(LocalBaseSelectorID, 3374 F.BaseSelectorID - LocalBaseSelectorID)); 3375 3376 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3377 } 3378 break; 3379 } 3380 3381 case METHOD_POOL: 3382 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3383 if (Record[0]) 3384 F.SelectorLookupTable 3385 = ASTSelectorLookupTable::Create( 3386 F.SelectorLookupTableData + Record[0], 3387 F.SelectorLookupTableData, 3388 ASTSelectorLookupTrait(*this, F)); 3389 TotalNumMethodPoolEntries += Record[1]; 3390 break; 3391 3392 case REFERENCED_SELECTOR_POOL: 3393 if (!Record.empty()) { 3394 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3395 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3396 Record[Idx++])); 3397 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3398 getRawEncoding()); 3399 } 3400 } 3401 break; 3402 3403 case PP_ASSUME_NONNULL_LOC: { 3404 unsigned Idx = 0; 3405 if (!Record.empty()) 3406 PP.setPreambleRecordedPragmaAssumeNonNullLoc( 3407 ReadSourceLocation(F, Record, Idx)); 3408 break; 3409 } 3410 3411 case PP_CONDITIONAL_STACK: 3412 if (!Record.empty()) { 3413 unsigned Idx = 0, End = Record.size() - 1; 3414 bool ReachedEOFWhileSkipping = Record[Idx++]; 3415 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3416 if (ReachedEOFWhileSkipping) { 3417 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3418 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3419 bool FoundNonSkipPortion = Record[Idx++]; 3420 bool FoundElse = Record[Idx++]; 3421 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3422 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3423 FoundElse, ElseLoc); 3424 } 3425 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3426 while (Idx < End) { 3427 auto Loc = ReadSourceLocation(F, Record, Idx); 3428 bool WasSkipping = Record[Idx++]; 3429 bool FoundNonSkip = Record[Idx++]; 3430 bool FoundElse = Record[Idx++]; 3431 ConditionalStack.push_back( 3432 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3433 } 3434 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3435 } 3436 break; 3437 3438 case PP_COUNTER_VALUE: 3439 if (!Record.empty() && Listener) 3440 Listener->ReadCounter(F, Record[0]); 3441 break; 3442 3443 case FILE_SORTED_DECLS: 3444 F.FileSortedDecls = (const DeclID *)Blob.data(); 3445 F.NumFileSortedDecls = Record[0]; 3446 break; 3447 3448 case SOURCE_LOCATION_OFFSETS: { 3449 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3450 F.LocalNumSLocEntries = Record[0]; 3451 SourceLocation::UIntTy SLocSpaceSize = Record[1]; 3452 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3453 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3454 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3455 SLocSpaceSize); 3456 if (!F.SLocEntryBaseID) 3457 return llvm::createStringError(std::errc::invalid_argument, 3458 "ran out of source locations"); 3459 // Make our entry in the range map. BaseID is negative and growing, so 3460 // we invert it. Because we invert it, though, we need the other end of 3461 // the range. 3462 unsigned RangeStart = 3463 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3464 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3465 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3466 3467 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3468 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0); 3469 GlobalSLocOffsetMap.insert( 3470 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3471 - SLocSpaceSize,&F)); 3472 3473 // Initialize the remapping table. 3474 // Invalid stays invalid. 3475 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3476 // This module. Base was 2 when being compiled. 3477 F.SLocRemap.insertOrReplace(std::make_pair( 3478 2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2))); 3479 3480 TotalNumSLocEntries += F.LocalNumSLocEntries; 3481 break; 3482 } 3483 3484 case MODULE_OFFSET_MAP: 3485 F.ModuleOffsetMap = Blob; 3486 break; 3487 3488 case SOURCE_MANAGER_LINE_TABLE: 3489 ParseLineTable(F, Record); 3490 break; 3491 3492 case SOURCE_LOCATION_PRELOADS: { 3493 // Need to transform from the local view (1-based IDs) to the global view, 3494 // which is based off F.SLocEntryBaseID. 3495 if (!F.PreloadSLocEntries.empty()) 3496 return llvm::createStringError( 3497 std::errc::illegal_byte_sequence, 3498 "Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3499 3500 F.PreloadSLocEntries.swap(Record); 3501 break; 3502 } 3503 3504 case EXT_VECTOR_DECLS: 3505 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3506 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3507 break; 3508 3509 case VTABLE_USES: 3510 if (Record.size() % 3 != 0) 3511 return llvm::createStringError(std::errc::illegal_byte_sequence, 3512 "Invalid VTABLE_USES record"); 3513 3514 // Later tables overwrite earlier ones. 3515 // FIXME: Modules will have some trouble with this. This is clearly not 3516 // the right way to do this. 3517 VTableUses.clear(); 3518 3519 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3520 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3521 VTableUses.push_back( 3522 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3523 VTableUses.push_back(Record[Idx++]); 3524 } 3525 break; 3526 3527 case PENDING_IMPLICIT_INSTANTIATIONS: 3528 if (PendingInstantiations.size() % 2 != 0) 3529 return llvm::createStringError( 3530 std::errc::illegal_byte_sequence, 3531 "Invalid existing PendingInstantiations"); 3532 3533 if (Record.size() % 2 != 0) 3534 return llvm::createStringError( 3535 std::errc::illegal_byte_sequence, 3536 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3537 3538 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3539 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3540 PendingInstantiations.push_back( 3541 ReadSourceLocation(F, Record, I).getRawEncoding()); 3542 } 3543 break; 3544 3545 case SEMA_DECL_REFS: 3546 if (Record.size() != 3) 3547 return llvm::createStringError(std::errc::illegal_byte_sequence, 3548 "Invalid SEMA_DECL_REFS block"); 3549 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3550 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3551 break; 3552 3553 case PPD_ENTITIES_OFFSETS: { 3554 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3555 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3556 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3557 3558 unsigned LocalBasePreprocessedEntityID = Record[0]; 3559 3560 unsigned StartingID; 3561 if (!PP.getPreprocessingRecord()) 3562 PP.createPreprocessingRecord(); 3563 if (!PP.getPreprocessingRecord()->getExternalSource()) 3564 PP.getPreprocessingRecord()->SetExternalSource(*this); 3565 StartingID 3566 = PP.getPreprocessingRecord() 3567 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3568 F.BasePreprocessedEntityID = StartingID; 3569 3570 if (F.NumPreprocessedEntities > 0) { 3571 // Introduce the global -> local mapping for preprocessed entities in 3572 // this module. 3573 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3574 3575 // Introduce the local -> global mapping for preprocessed entities in 3576 // this module. 3577 F.PreprocessedEntityRemap.insertOrReplace( 3578 std::make_pair(LocalBasePreprocessedEntityID, 3579 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3580 } 3581 3582 break; 3583 } 3584 3585 case PPD_SKIPPED_RANGES: { 3586 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3587 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3588 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3589 3590 if (!PP.getPreprocessingRecord()) 3591 PP.createPreprocessingRecord(); 3592 if (!PP.getPreprocessingRecord()->getExternalSource()) 3593 PP.getPreprocessingRecord()->SetExternalSource(*this); 3594 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3595 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3596 3597 if (F.NumPreprocessedSkippedRanges > 0) 3598 GlobalSkippedRangeMap.insert( 3599 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3600 break; 3601 } 3602 3603 case DECL_UPDATE_OFFSETS: 3604 if (Record.size() % 2 != 0) 3605 return llvm::createStringError( 3606 std::errc::illegal_byte_sequence, 3607 "invalid DECL_UPDATE_OFFSETS block in AST file"); 3608 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3609 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3610 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3611 3612 // If we've already loaded the decl, perform the updates when we finish 3613 // loading this block. 3614 if (Decl *D = GetExistingDecl(ID)) 3615 PendingUpdateRecords.push_back( 3616 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3617 } 3618 break; 3619 3620 case OBJC_CATEGORIES_MAP: 3621 if (F.LocalNumObjCCategoriesInMap != 0) 3622 return llvm::createStringError( 3623 std::errc::illegal_byte_sequence, 3624 "duplicate OBJC_CATEGORIES_MAP record in AST file"); 3625 3626 F.LocalNumObjCCategoriesInMap = Record[0]; 3627 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3628 break; 3629 3630 case OBJC_CATEGORIES: 3631 F.ObjCCategories.swap(Record); 3632 break; 3633 3634 case CUDA_SPECIAL_DECL_REFS: 3635 // Later tables overwrite earlier ones. 3636 // FIXME: Modules will have trouble with this. 3637 CUDASpecialDeclRefs.clear(); 3638 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3639 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3640 break; 3641 3642 case HEADER_SEARCH_TABLE: 3643 F.HeaderFileInfoTableData = Blob.data(); 3644 F.LocalNumHeaderFileInfos = Record[1]; 3645 if (Record[0]) { 3646 F.HeaderFileInfoTable 3647 = HeaderFileInfoLookupTable::Create( 3648 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3649 (const unsigned char *)F.HeaderFileInfoTableData, 3650 HeaderFileInfoTrait(*this, F, 3651 &PP.getHeaderSearchInfo(), 3652 Blob.data() + Record[2])); 3653 3654 PP.getHeaderSearchInfo().SetExternalSource(this); 3655 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3656 PP.getHeaderSearchInfo().SetExternalLookup(this); 3657 } 3658 break; 3659 3660 case FP_PRAGMA_OPTIONS: 3661 // Later tables overwrite earlier ones. 3662 FPPragmaOptions.swap(Record); 3663 break; 3664 3665 case OPENCL_EXTENSIONS: 3666 for (unsigned I = 0, E = Record.size(); I != E; ) { 3667 auto Name = ReadString(Record, I); 3668 auto &OptInfo = OpenCLExtensions.OptMap[Name]; 3669 OptInfo.Supported = Record[I++] != 0; 3670 OptInfo.Enabled = Record[I++] != 0; 3671 OptInfo.WithPragma = Record[I++] != 0; 3672 OptInfo.Avail = Record[I++]; 3673 OptInfo.Core = Record[I++]; 3674 OptInfo.Opt = Record[I++]; 3675 } 3676 break; 3677 3678 case TENTATIVE_DEFINITIONS: 3679 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3680 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3681 break; 3682 3683 case KNOWN_NAMESPACES: 3684 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3685 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3686 break; 3687 3688 case UNDEFINED_BUT_USED: 3689 if (UndefinedButUsed.size() % 2 != 0) 3690 return llvm::createStringError(std::errc::illegal_byte_sequence, 3691 "Invalid existing UndefinedButUsed"); 3692 3693 if (Record.size() % 2 != 0) 3694 return llvm::createStringError(std::errc::illegal_byte_sequence, 3695 "invalid undefined-but-used record"); 3696 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3697 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3698 UndefinedButUsed.push_back( 3699 ReadSourceLocation(F, Record, I).getRawEncoding()); 3700 } 3701 break; 3702 3703 case DELETE_EXPRS_TO_ANALYZE: 3704 for (unsigned I = 0, N = Record.size(); I != N;) { 3705 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3706 const uint64_t Count = Record[I++]; 3707 DelayedDeleteExprs.push_back(Count); 3708 for (uint64_t C = 0; C < Count; ++C) { 3709 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3710 bool IsArrayForm = Record[I++] == 1; 3711 DelayedDeleteExprs.push_back(IsArrayForm); 3712 } 3713 } 3714 break; 3715 3716 case IMPORTED_MODULES: 3717 if (!F.isModule()) { 3718 // If we aren't loading a module (which has its own exports), make 3719 // all of the imported modules visible. 3720 // FIXME: Deal with macros-only imports. 3721 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3722 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3723 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3724 if (GlobalID) { 3725 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3726 if (DeserializationListener) 3727 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3728 } 3729 } 3730 } 3731 break; 3732 3733 case MACRO_OFFSET: { 3734 if (F.LocalNumMacros != 0) 3735 return llvm::createStringError( 3736 std::errc::illegal_byte_sequence, 3737 "duplicate MACRO_OFFSET record in AST file"); 3738 F.MacroOffsets = (const uint32_t *)Blob.data(); 3739 F.LocalNumMacros = Record[0]; 3740 unsigned LocalBaseMacroID = Record[1]; 3741 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3742 F.BaseMacroID = getTotalNumMacros(); 3743 3744 if (F.LocalNumMacros > 0) { 3745 // Introduce the global -> local mapping for macros within this module. 3746 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3747 3748 // Introduce the local -> global mapping for macros within this module. 3749 F.MacroRemap.insertOrReplace( 3750 std::make_pair(LocalBaseMacroID, 3751 F.BaseMacroID - LocalBaseMacroID)); 3752 3753 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3754 } 3755 break; 3756 } 3757 3758 case PP_INCLUDED_FILES: 3759 readIncludedFiles(F, Blob, PP); 3760 break; 3761 3762 case LATE_PARSED_TEMPLATE: 3763 LateParsedTemplates.emplace_back( 3764 std::piecewise_construct, std::forward_as_tuple(&F), 3765 std::forward_as_tuple(Record.begin(), Record.end())); 3766 break; 3767 3768 case OPTIMIZE_PRAGMA_OPTIONS: 3769 if (Record.size() != 1) 3770 return llvm::createStringError(std::errc::illegal_byte_sequence, 3771 "invalid pragma optimize record"); 3772 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3773 break; 3774 3775 case MSSTRUCT_PRAGMA_OPTIONS: 3776 if (Record.size() != 1) 3777 return llvm::createStringError(std::errc::illegal_byte_sequence, 3778 "invalid pragma ms_struct record"); 3779 PragmaMSStructState = Record[0]; 3780 break; 3781 3782 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3783 if (Record.size() != 2) 3784 return llvm::createStringError( 3785 std::errc::illegal_byte_sequence, 3786 "invalid pragma pointers to members record"); 3787 PragmaMSPointersToMembersState = Record[0]; 3788 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3789 break; 3790 3791 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3792 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3793 UnusedLocalTypedefNameCandidates.push_back( 3794 getGlobalDeclID(F, Record[I])); 3795 break; 3796 3797 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3798 if (Record.size() != 1) 3799 return llvm::createStringError(std::errc::illegal_byte_sequence, 3800 "invalid cuda pragma options record"); 3801 ForceCUDAHostDeviceDepth = Record[0]; 3802 break; 3803 3804 case ALIGN_PACK_PRAGMA_OPTIONS: { 3805 if (Record.size() < 3) 3806 return llvm::createStringError(std::errc::illegal_byte_sequence, 3807 "invalid pragma pack record"); 3808 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]); 3809 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3810 unsigned NumStackEntries = Record[2]; 3811 unsigned Idx = 3; 3812 // Reset the stack when importing a new module. 3813 PragmaAlignPackStack.clear(); 3814 for (unsigned I = 0; I < NumStackEntries; ++I) { 3815 PragmaAlignPackStackEntry Entry; 3816 Entry.Value = ReadAlignPackInfo(Record[Idx++]); 3817 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3818 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3819 PragmaAlignPackStrings.push_back(ReadString(Record, Idx)); 3820 Entry.SlotLabel = PragmaAlignPackStrings.back(); 3821 PragmaAlignPackStack.push_back(Entry); 3822 } 3823 break; 3824 } 3825 3826 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3827 if (Record.size() < 3) 3828 return llvm::createStringError(std::errc::illegal_byte_sequence, 3829 "invalid pragma float control record"); 3830 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3831 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3832 unsigned NumStackEntries = Record[2]; 3833 unsigned Idx = 3; 3834 // Reset the stack when importing a new module. 3835 FpPragmaStack.clear(); 3836 for (unsigned I = 0; I < NumStackEntries; ++I) { 3837 FpPragmaStackEntry Entry; 3838 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3839 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3840 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3841 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3842 Entry.SlotLabel = FpPragmaStrings.back(); 3843 FpPragmaStack.push_back(Entry); 3844 } 3845 break; 3846 } 3847 3848 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3849 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3850 DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I])); 3851 break; 3852 } 3853 } 3854 } 3855 3856 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3857 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3858 3859 // Additional remapping information. 3860 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3861 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3862 F.ModuleOffsetMap = StringRef(); 3863 3864 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3865 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3866 F.SLocRemap.insert(std::make_pair(0U, 0)); 3867 F.SLocRemap.insert(std::make_pair(2U, 1)); 3868 } 3869 3870 // Continuous range maps we may be updating in our module. 3871 using SLocRemapBuilder = 3872 ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy, 3873 2>::Builder; 3874 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3875 SLocRemapBuilder SLocRemap(F.SLocRemap); 3876 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3877 RemapBuilder MacroRemap(F.MacroRemap); 3878 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3879 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3880 RemapBuilder SelectorRemap(F.SelectorRemap); 3881 RemapBuilder DeclRemap(F.DeclRemap); 3882 RemapBuilder TypeRemap(F.TypeRemap); 3883 3884 while (Data < DataEnd) { 3885 // FIXME: Looking up dependency modules by filename is horrible. Let's 3886 // start fixing this with prebuilt, explicit and implicit modules and see 3887 // how it goes... 3888 using namespace llvm::support; 3889 ModuleKind Kind = static_cast<ModuleKind>( 3890 endian::readNext<uint8_t, little, unaligned>(Data)); 3891 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3892 StringRef Name = StringRef((const char*)Data, Len); 3893 Data += Len; 3894 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3895 Kind == MK_ImplicitModule 3896 ? ModuleMgr.lookupByModuleName(Name) 3897 : ModuleMgr.lookupByFileName(Name)); 3898 if (!OM) { 3899 std::string Msg = 3900 "SourceLocation remap refers to unknown module, cannot find "; 3901 Msg.append(std::string(Name)); 3902 Error(Msg); 3903 return; 3904 } 3905 3906 SourceLocation::UIntTy SLocOffset = 3907 endian::readNext<uint32_t, little, unaligned>(Data); 3908 uint32_t IdentifierIDOffset = 3909 endian::readNext<uint32_t, little, unaligned>(Data); 3910 uint32_t MacroIDOffset = 3911 endian::readNext<uint32_t, little, unaligned>(Data); 3912 uint32_t PreprocessedEntityIDOffset = 3913 endian::readNext<uint32_t, little, unaligned>(Data); 3914 uint32_t SubmoduleIDOffset = 3915 endian::readNext<uint32_t, little, unaligned>(Data); 3916 uint32_t SelectorIDOffset = 3917 endian::readNext<uint32_t, little, unaligned>(Data); 3918 uint32_t DeclIDOffset = 3919 endian::readNext<uint32_t, little, unaligned>(Data); 3920 uint32_t TypeIndexOffset = 3921 endian::readNext<uint32_t, little, unaligned>(Data); 3922 3923 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3924 RemapBuilder &Remap) { 3925 constexpr uint32_t None = std::numeric_limits<uint32_t>::max(); 3926 if (Offset != None) 3927 Remap.insert(std::make_pair(Offset, 3928 static_cast<int>(BaseOffset - Offset))); 3929 }; 3930 3931 constexpr SourceLocation::UIntTy SLocNone = 3932 std::numeric_limits<SourceLocation::UIntTy>::max(); 3933 if (SLocOffset != SLocNone) 3934 SLocRemap.insert(std::make_pair( 3935 SLocOffset, static_cast<SourceLocation::IntTy>( 3936 OM->SLocEntryBaseOffset - SLocOffset))); 3937 3938 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3939 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3940 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3941 PreprocessedEntityRemap); 3942 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3943 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3944 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3945 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3946 3947 // Global -> local mappings. 3948 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3949 } 3950 } 3951 3952 ASTReader::ASTReadResult 3953 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3954 const ModuleFile *ImportedBy, 3955 unsigned ClientLoadCapabilities) { 3956 unsigned Idx = 0; 3957 F.ModuleMapPath = ReadPath(F, Record, Idx); 3958 3959 // Try to resolve ModuleName in the current header search context and 3960 // verify that it is found in the same module map file as we saved. If the 3961 // top-level AST file is a main file, skip this check because there is no 3962 // usable header search context. 3963 assert(!F.ModuleName.empty() && 3964 "MODULE_NAME should come before MODULE_MAP_FILE"); 3965 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3966 // An implicitly-loaded module file should have its module listed in some 3967 // module map file that we've already loaded. 3968 Module *M = 3969 PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc); 3970 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3971 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3972 // Don't emit module relocation error if we have -fno-validate-pch 3973 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 3974 DisableValidationForModuleKind::Module) && 3975 !ModMap) { 3976 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) { 3977 if (auto ASTFE = M ? M->getASTFile() : None) { 3978 // This module was defined by an imported (explicit) module. 3979 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3980 << ASTFE->getName(); 3981 } else { 3982 // This module was built with a different module map. 3983 Diag(diag::err_imported_module_not_found) 3984 << F.ModuleName << F.FileName 3985 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3986 << !ImportedBy; 3987 // In case it was imported by a PCH, there's a chance the user is 3988 // just missing to include the search path to the directory containing 3989 // the modulemap. 3990 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3991 Diag(diag::note_imported_by_pch_module_not_found) 3992 << llvm::sys::path::parent_path(F.ModuleMapPath); 3993 } 3994 } 3995 return OutOfDate; 3996 } 3997 3998 assert(M && M->Name == F.ModuleName && "found module with different name"); 3999 4000 // Check the primary module map file. 4001 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 4002 if (!StoredModMap || *StoredModMap != ModMap) { 4003 assert(ModMap && "found module is missing module map file"); 4004 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 4005 "top-level import should be verified"); 4006 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 4007 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 4008 Diag(diag::err_imported_module_modmap_changed) 4009 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 4010 << ModMap->getName() << F.ModuleMapPath << NotImported; 4011 return OutOfDate; 4012 } 4013 4014 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 4015 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 4016 // FIXME: we should use input files rather than storing names. 4017 std::string Filename = ReadPath(F, Record, Idx); 4018 auto SF = FileMgr.getFile(Filename, false, false); 4019 if (!SF) { 4020 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 4021 Error("could not find file '" + Filename +"' referenced by AST file"); 4022 return OutOfDate; 4023 } 4024 AdditionalStoredMaps.insert(*SF); 4025 } 4026 4027 // Check any additional module map files (e.g. module.private.modulemap) 4028 // that are not in the pcm. 4029 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 4030 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 4031 // Remove files that match 4032 // Note: SmallPtrSet::erase is really remove 4033 if (!AdditionalStoredMaps.erase(ModMap)) { 4034 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 4035 Diag(diag::err_module_different_modmap) 4036 << F.ModuleName << /*new*/0 << ModMap->getName(); 4037 return OutOfDate; 4038 } 4039 } 4040 } 4041 4042 // Check any additional module map files that are in the pcm, but not 4043 // found in header search. Cases that match are already removed. 4044 for (const FileEntry *ModMap : AdditionalStoredMaps) { 4045 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 4046 Diag(diag::err_module_different_modmap) 4047 << F.ModuleName << /*not new*/1 << ModMap->getName(); 4048 return OutOfDate; 4049 } 4050 } 4051 4052 if (Listener) 4053 Listener->ReadModuleMapFile(F.ModuleMapPath); 4054 return Success; 4055 } 4056 4057 /// Move the given method to the back of the global list of methods. 4058 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4059 // Find the entry for this selector in the method pool. 4060 Sema::GlobalMethodPool::iterator Known 4061 = S.MethodPool.find(Method->getSelector()); 4062 if (Known == S.MethodPool.end()) 4063 return; 4064 4065 // Retrieve the appropriate method list. 4066 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4067 : Known->second.second; 4068 bool Found = false; 4069 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4070 if (!Found) { 4071 if (List->getMethod() == Method) { 4072 Found = true; 4073 } else { 4074 // Keep searching. 4075 continue; 4076 } 4077 } 4078 4079 if (List->getNext()) 4080 List->setMethod(List->getNext()->getMethod()); 4081 else 4082 List->setMethod(Method); 4083 } 4084 } 4085 4086 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4087 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4088 for (Decl *D : Names) { 4089 bool wasHidden = !D->isUnconditionallyVisible(); 4090 D->setVisibleDespiteOwningModule(); 4091 4092 if (wasHidden && SemaObj) { 4093 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4094 moveMethodToBackOfGlobalList(*SemaObj, Method); 4095 } 4096 } 4097 } 4098 } 4099 4100 void ASTReader::makeModuleVisible(Module *Mod, 4101 Module::NameVisibilityKind NameVisibility, 4102 SourceLocation ImportLoc) { 4103 llvm::SmallPtrSet<Module *, 4> Visited; 4104 SmallVector<Module *, 4> Stack; 4105 Stack.push_back(Mod); 4106 while (!Stack.empty()) { 4107 Mod = Stack.pop_back_val(); 4108 4109 if (NameVisibility <= Mod->NameVisibility) { 4110 // This module already has this level of visibility (or greater), so 4111 // there is nothing more to do. 4112 continue; 4113 } 4114 4115 if (Mod->isUnimportable()) { 4116 // Modules that aren't importable cannot be made visible. 4117 continue; 4118 } 4119 4120 // Update the module's name visibility. 4121 Mod->NameVisibility = NameVisibility; 4122 4123 // If we've already deserialized any names from this module, 4124 // mark them as visible. 4125 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4126 if (Hidden != HiddenNamesMap.end()) { 4127 auto HiddenNames = std::move(*Hidden); 4128 HiddenNamesMap.erase(Hidden); 4129 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4130 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4131 "making names visible added hidden names"); 4132 } 4133 4134 // Push any exported modules onto the stack to be marked as visible. 4135 SmallVector<Module *, 16> Exports; 4136 Mod->getExportedModules(Exports); 4137 for (SmallVectorImpl<Module *>::iterator 4138 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4139 Module *Exported = *I; 4140 if (Visited.insert(Exported).second) 4141 Stack.push_back(Exported); 4142 } 4143 } 4144 } 4145 4146 /// We've merged the definition \p MergedDef into the existing definition 4147 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4148 /// visible. 4149 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4150 NamedDecl *MergedDef) { 4151 if (!Def->isUnconditionallyVisible()) { 4152 // If MergedDef is visible or becomes visible, make the definition visible. 4153 if (MergedDef->isUnconditionallyVisible()) 4154 Def->setVisibleDespiteOwningModule(); 4155 else { 4156 getContext().mergeDefinitionIntoModule( 4157 Def, MergedDef->getImportedOwningModule(), 4158 /*NotifyListeners*/ false); 4159 PendingMergedDefinitionsToDeduplicate.insert(Def); 4160 } 4161 } 4162 } 4163 4164 bool ASTReader::loadGlobalIndex() { 4165 if (GlobalIndex) 4166 return false; 4167 4168 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4169 !PP.getLangOpts().Modules) 4170 return true; 4171 4172 // Try to load the global index. 4173 TriedLoadingGlobalIndex = true; 4174 StringRef ModuleCachePath 4175 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4176 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4177 GlobalModuleIndex::readIndex(ModuleCachePath); 4178 if (llvm::Error Err = std::move(Result.second)) { 4179 assert(!Result.first); 4180 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4181 return true; 4182 } 4183 4184 GlobalIndex.reset(Result.first); 4185 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4186 return false; 4187 } 4188 4189 bool ASTReader::isGlobalIndexUnavailable() const { 4190 return PP.getLangOpts().Modules && UseGlobalIndex && 4191 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4192 } 4193 4194 static void updateModuleTimestamp(ModuleFile &MF) { 4195 // Overwrite the timestamp file contents so that file's mtime changes. 4196 std::string TimestampFilename = MF.getTimestampFilename(); 4197 std::error_code EC; 4198 llvm::raw_fd_ostream OS(TimestampFilename, EC, 4199 llvm::sys::fs::OF_TextWithCRLF); 4200 if (EC) 4201 return; 4202 OS << "Timestamp file\n"; 4203 OS.close(); 4204 OS.clear_error(); // Avoid triggering a fatal error. 4205 } 4206 4207 /// Given a cursor at the start of an AST file, scan ahead and drop the 4208 /// cursor into the start of the given block ID, returning false on success and 4209 /// true on failure. 4210 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4211 while (true) { 4212 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4213 if (!MaybeEntry) { 4214 // FIXME this drops errors on the floor. 4215 consumeError(MaybeEntry.takeError()); 4216 return true; 4217 } 4218 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4219 4220 switch (Entry.Kind) { 4221 case llvm::BitstreamEntry::Error: 4222 case llvm::BitstreamEntry::EndBlock: 4223 return true; 4224 4225 case llvm::BitstreamEntry::Record: 4226 // Ignore top-level records. 4227 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4228 break; 4229 else { 4230 // FIXME this drops errors on the floor. 4231 consumeError(Skipped.takeError()); 4232 return true; 4233 } 4234 4235 case llvm::BitstreamEntry::SubBlock: 4236 if (Entry.ID == BlockID) { 4237 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4238 // FIXME this drops the error on the floor. 4239 consumeError(std::move(Err)); 4240 return true; 4241 } 4242 // Found it! 4243 return false; 4244 } 4245 4246 if (llvm::Error Err = Cursor.SkipBlock()) { 4247 // FIXME this drops the error on the floor. 4248 consumeError(std::move(Err)); 4249 return true; 4250 } 4251 } 4252 } 4253 } 4254 4255 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4256 ModuleKind Type, 4257 SourceLocation ImportLoc, 4258 unsigned ClientLoadCapabilities, 4259 SmallVectorImpl<ImportedSubmodule> *Imported) { 4260 llvm::SaveAndRestore<SourceLocation> 4261 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4262 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII( 4263 CurrentDeserializingModuleKind, Type); 4264 4265 // Defer any pending actions until we get to the end of reading the AST file. 4266 Deserializing AnASTFile(this); 4267 4268 // Bump the generation number. 4269 unsigned PreviousGeneration = 0; 4270 if (ContextObj) 4271 PreviousGeneration = incrementGeneration(*ContextObj); 4272 4273 unsigned NumModules = ModuleMgr.size(); 4274 SmallVector<ImportedModule, 4> Loaded; 4275 if (ASTReadResult ReadResult = 4276 ReadASTCore(FileName, Type, ImportLoc, 4277 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(), 4278 ClientLoadCapabilities)) { 4279 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4280 PP.getLangOpts().Modules 4281 ? &PP.getHeaderSearchInfo().getModuleMap() 4282 : nullptr); 4283 4284 // If we find that any modules are unusable, the global index is going 4285 // to be out-of-date. Just remove it. 4286 GlobalIndex.reset(); 4287 ModuleMgr.setGlobalIndex(nullptr); 4288 return ReadResult; 4289 } 4290 4291 // Here comes stuff that we only do once the entire chain is loaded. Do *not* 4292 // remove modules from this point. Various fields are updated during reading 4293 // the AST block and removing the modules would result in dangling pointers. 4294 // They are generally only incidentally dereferenced, ie. a binary search 4295 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to 4296 // be dereferenced but it wouldn't actually be used. 4297 4298 // Load the AST blocks of all of the modules that we loaded. We can still 4299 // hit errors parsing the ASTs at this point. 4300 for (ImportedModule &M : Loaded) { 4301 ModuleFile &F = *M.Mod; 4302 4303 // Read the AST block. 4304 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) { 4305 Error(std::move(Err)); 4306 return Failure; 4307 } 4308 4309 // The AST block should always have a definition for the main module. 4310 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4311 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4312 return Failure; 4313 } 4314 4315 // Read the extension blocks. 4316 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4317 if (llvm::Error Err = ReadExtensionBlock(F)) { 4318 Error(std::move(Err)); 4319 return Failure; 4320 } 4321 } 4322 4323 // Once read, set the ModuleFile bit base offset and update the size in 4324 // bits of all files we've seen. 4325 F.GlobalBitOffset = TotalModulesSizeInBits; 4326 TotalModulesSizeInBits += F.SizeInBits; 4327 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4328 } 4329 4330 // Preload source locations and interesting indentifiers. 4331 for (ImportedModule &M : Loaded) { 4332 ModuleFile &F = *M.Mod; 4333 4334 // Preload SLocEntries. 4335 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4336 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4337 // Load it through the SourceManager and don't call ReadSLocEntry() 4338 // directly because the entry may have already been loaded in which case 4339 // calling ReadSLocEntry() directly would trigger an assertion in 4340 // SourceManager. 4341 SourceMgr.getLoadedSLocEntryByID(Index); 4342 } 4343 4344 // Map the original source file ID into the ID space of the current 4345 // compilation. 4346 if (F.OriginalSourceFileID.isValid()) { 4347 F.OriginalSourceFileID = FileID::get( 4348 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4349 } 4350 4351 // Preload all the pending interesting identifiers by marking them out of 4352 // date. 4353 for (auto Offset : F.PreloadIdentifierOffsets) { 4354 const unsigned char *Data = F.IdentifierTableData + Offset; 4355 4356 ASTIdentifierLookupTrait Trait(*this, F); 4357 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4358 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4359 auto &II = PP.getIdentifierTable().getOwn(Key); 4360 II.setOutOfDate(true); 4361 4362 // Mark this identifier as being from an AST file so that we can track 4363 // whether we need to serialize it. 4364 markIdentifierFromAST(*this, II); 4365 4366 // Associate the ID with the identifier so that the writer can reuse it. 4367 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4368 SetIdentifierInfo(ID, &II); 4369 } 4370 } 4371 4372 // Setup the import locations and notify the module manager that we've 4373 // committed to these module files. 4374 for (ImportedModule &M : Loaded) { 4375 ModuleFile &F = *M.Mod; 4376 4377 ModuleMgr.moduleFileAccepted(&F); 4378 4379 // Set the import location. 4380 F.DirectImportLoc = ImportLoc; 4381 // FIXME: We assume that locations from PCH / preamble do not need 4382 // any translation. 4383 if (!M.ImportedBy) 4384 F.ImportLoc = M.ImportLoc; 4385 else 4386 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4387 } 4388 4389 if (!PP.getLangOpts().CPlusPlus || 4390 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4391 Type != MK_PrebuiltModule)) { 4392 // Mark all of the identifiers in the identifier table as being out of date, 4393 // so that various accessors know to check the loaded modules when the 4394 // identifier is used. 4395 // 4396 // For C++ modules, we don't need information on many identifiers (just 4397 // those that provide macros or are poisoned), so we mark all of 4398 // the interesting ones via PreloadIdentifierOffsets. 4399 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4400 IdEnd = PP.getIdentifierTable().end(); 4401 Id != IdEnd; ++Id) 4402 Id->second->setOutOfDate(true); 4403 } 4404 // Mark selectors as out of date. 4405 for (auto Sel : SelectorGeneration) 4406 SelectorOutOfDate[Sel.first] = true; 4407 4408 // Resolve any unresolved module exports. 4409 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4410 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4411 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4412 Module *ResolvedMod = getSubmodule(GlobalID); 4413 4414 switch (Unresolved.Kind) { 4415 case UnresolvedModuleRef::Conflict: 4416 if (ResolvedMod) { 4417 Module::Conflict Conflict; 4418 Conflict.Other = ResolvedMod; 4419 Conflict.Message = Unresolved.String.str(); 4420 Unresolved.Mod->Conflicts.push_back(Conflict); 4421 } 4422 continue; 4423 4424 case UnresolvedModuleRef::Import: 4425 if (ResolvedMod) 4426 Unresolved.Mod->Imports.insert(ResolvedMod); 4427 continue; 4428 4429 case UnresolvedModuleRef::Export: 4430 if (ResolvedMod || Unresolved.IsWildcard) 4431 Unresolved.Mod->Exports.push_back( 4432 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4433 continue; 4434 } 4435 } 4436 UnresolvedModuleRefs.clear(); 4437 4438 if (Imported) 4439 Imported->append(ImportedModules.begin(), 4440 ImportedModules.end()); 4441 4442 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4443 // Might be unnecessary as use declarations are only used to build the 4444 // module itself. 4445 4446 if (ContextObj) 4447 InitializeContext(); 4448 4449 if (SemaObj) 4450 UpdateSema(); 4451 4452 if (DeserializationListener) 4453 DeserializationListener->ReaderInitialized(this); 4454 4455 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4456 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4457 // If this AST file is a precompiled preamble, then set the 4458 // preamble file ID of the source manager to the file source file 4459 // from which the preamble was built. 4460 if (Type == MK_Preamble) { 4461 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4462 } else if (Type == MK_MainFile) { 4463 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4464 } 4465 } 4466 4467 // For any Objective-C class definitions we have already loaded, make sure 4468 // that we load any additional categories. 4469 if (ContextObj) { 4470 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4471 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4472 ObjCClassesLoaded[I], 4473 PreviousGeneration); 4474 } 4475 } 4476 4477 if (PP.getHeaderSearchInfo() 4478 .getHeaderSearchOpts() 4479 .ModulesValidateOncePerBuildSession) { 4480 // Now we are certain that the module and all modules it depends on are 4481 // up to date. Create or update timestamp files for modules that are 4482 // located in the module cache (not for PCH files that could be anywhere 4483 // in the filesystem). 4484 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4485 ImportedModule &M = Loaded[I]; 4486 if (M.Mod->Kind == MK_ImplicitModule) { 4487 updateModuleTimestamp(*M.Mod); 4488 } 4489 } 4490 } 4491 4492 return Success; 4493 } 4494 4495 static ASTFileSignature readASTFileSignature(StringRef PCH); 4496 4497 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4498 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4499 // FIXME checking magic headers is done in other places such as 4500 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4501 // always done the same. Unify it all with a helper. 4502 if (!Stream.canSkipToPos(4)) 4503 return llvm::createStringError(std::errc::illegal_byte_sequence, 4504 "file too small to contain AST file magic"); 4505 for (unsigned C : {'C', 'P', 'C', 'H'}) 4506 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4507 if (Res.get() != C) 4508 return llvm::createStringError( 4509 std::errc::illegal_byte_sequence, 4510 "file doesn't start with AST file magic"); 4511 } else 4512 return Res.takeError(); 4513 return llvm::Error::success(); 4514 } 4515 4516 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4517 switch (Kind) { 4518 case MK_PCH: 4519 return 0; // PCH 4520 case MK_ImplicitModule: 4521 case MK_ExplicitModule: 4522 case MK_PrebuiltModule: 4523 return 1; // module 4524 case MK_MainFile: 4525 case MK_Preamble: 4526 return 2; // main source file 4527 } 4528 llvm_unreachable("unknown module kind"); 4529 } 4530 4531 ASTReader::ASTReadResult 4532 ASTReader::ReadASTCore(StringRef FileName, 4533 ModuleKind Type, 4534 SourceLocation ImportLoc, 4535 ModuleFile *ImportedBy, 4536 SmallVectorImpl<ImportedModule> &Loaded, 4537 off_t ExpectedSize, time_t ExpectedModTime, 4538 ASTFileSignature ExpectedSignature, 4539 unsigned ClientLoadCapabilities) { 4540 ModuleFile *M; 4541 std::string ErrorStr; 4542 ModuleManager::AddModuleResult AddResult 4543 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4544 getGeneration(), ExpectedSize, ExpectedModTime, 4545 ExpectedSignature, readASTFileSignature, 4546 M, ErrorStr); 4547 4548 switch (AddResult) { 4549 case ModuleManager::AlreadyLoaded: 4550 Diag(diag::remark_module_import) 4551 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4552 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4553 return Success; 4554 4555 case ModuleManager::NewlyLoaded: 4556 // Load module file below. 4557 break; 4558 4559 case ModuleManager::Missing: 4560 // The module file was missing; if the client can handle that, return 4561 // it. 4562 if (ClientLoadCapabilities & ARR_Missing) 4563 return Missing; 4564 4565 // Otherwise, return an error. 4566 Diag(diag::err_ast_file_not_found) 4567 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4568 << ErrorStr; 4569 return Failure; 4570 4571 case ModuleManager::OutOfDate: 4572 // We couldn't load the module file because it is out-of-date. If the 4573 // client can handle out-of-date, return it. 4574 if (ClientLoadCapabilities & ARR_OutOfDate) 4575 return OutOfDate; 4576 4577 // Otherwise, return an error. 4578 Diag(diag::err_ast_file_out_of_date) 4579 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4580 << ErrorStr; 4581 return Failure; 4582 } 4583 4584 assert(M && "Missing module file"); 4585 4586 bool ShouldFinalizePCM = false; 4587 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4588 auto &MC = getModuleManager().getModuleCache(); 4589 if (ShouldFinalizePCM) 4590 MC.finalizePCM(FileName); 4591 else 4592 MC.tryToDropPCM(FileName); 4593 }); 4594 ModuleFile &F = *M; 4595 BitstreamCursor &Stream = F.Stream; 4596 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4597 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4598 4599 // Sniff for the signature. 4600 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4601 Diag(diag::err_ast_file_invalid) 4602 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4603 return Failure; 4604 } 4605 4606 // This is used for compatibility with older PCH formats. 4607 bool HaveReadControlBlock = false; 4608 while (true) { 4609 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4610 if (!MaybeEntry) { 4611 Error(MaybeEntry.takeError()); 4612 return Failure; 4613 } 4614 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4615 4616 switch (Entry.Kind) { 4617 case llvm::BitstreamEntry::Error: 4618 case llvm::BitstreamEntry::Record: 4619 case llvm::BitstreamEntry::EndBlock: 4620 Error("invalid record at top-level of AST file"); 4621 return Failure; 4622 4623 case llvm::BitstreamEntry::SubBlock: 4624 break; 4625 } 4626 4627 switch (Entry.ID) { 4628 case CONTROL_BLOCK_ID: 4629 HaveReadControlBlock = true; 4630 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4631 case Success: 4632 // Check that we didn't try to load a non-module AST file as a module. 4633 // 4634 // FIXME: Should we also perform the converse check? Loading a module as 4635 // a PCH file sort of works, but it's a bit wonky. 4636 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4637 Type == MK_PrebuiltModule) && 4638 F.ModuleName.empty()) { 4639 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4640 if (Result != OutOfDate || 4641 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4642 Diag(diag::err_module_file_not_module) << FileName; 4643 return Result; 4644 } 4645 break; 4646 4647 case Failure: return Failure; 4648 case Missing: return Missing; 4649 case OutOfDate: return OutOfDate; 4650 case VersionMismatch: return VersionMismatch; 4651 case ConfigurationMismatch: return ConfigurationMismatch; 4652 case HadErrors: return HadErrors; 4653 } 4654 break; 4655 4656 case AST_BLOCK_ID: 4657 if (!HaveReadControlBlock) { 4658 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4659 Diag(diag::err_pch_version_too_old); 4660 return VersionMismatch; 4661 } 4662 4663 // Record that we've loaded this module. 4664 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4665 ShouldFinalizePCM = true; 4666 return Success; 4667 4668 case UNHASHED_CONTROL_BLOCK_ID: 4669 // This block is handled using look-ahead during ReadControlBlock. We 4670 // shouldn't get here! 4671 Error("malformed block record in AST file"); 4672 return Failure; 4673 4674 default: 4675 if (llvm::Error Err = Stream.SkipBlock()) { 4676 Error(std::move(Err)); 4677 return Failure; 4678 } 4679 break; 4680 } 4681 } 4682 4683 llvm_unreachable("unexpected break; expected return"); 4684 } 4685 4686 ASTReader::ASTReadResult 4687 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4688 unsigned ClientLoadCapabilities) { 4689 const HeaderSearchOptions &HSOpts = 4690 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4691 bool AllowCompatibleConfigurationMismatch = 4692 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4693 bool DisableValidation = shouldDisableValidationForFile(F); 4694 4695 ASTReadResult Result = readUnhashedControlBlockImpl( 4696 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4697 Listener.get(), 4698 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4699 4700 // If F was directly imported by another module, it's implicitly validated by 4701 // the importing module. 4702 if (DisableValidation || WasImportedBy || 4703 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4704 return Success; 4705 4706 if (Result == Failure) { 4707 Error("malformed block record in AST file"); 4708 return Failure; 4709 } 4710 4711 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4712 // If this module has already been finalized in the ModuleCache, we're stuck 4713 // with it; we can only load a single version of each module. 4714 // 4715 // This can happen when a module is imported in two contexts: in one, as a 4716 // user module; in another, as a system module (due to an import from 4717 // another module marked with the [system] flag). It usually indicates a 4718 // bug in the module map: this module should also be marked with [system]. 4719 // 4720 // If -Wno-system-headers (the default), and the first import is as a 4721 // system module, then validation will fail during the as-user import, 4722 // since -Werror flags won't have been validated. However, it's reasonable 4723 // to treat this consistently as a system module. 4724 // 4725 // If -Wsystem-headers, the PCM on disk was built with 4726 // -Wno-system-headers, and the first import is as a user module, then 4727 // validation will fail during the as-system import since the PCM on disk 4728 // doesn't guarantee that -Werror was respected. However, the -Werror 4729 // flags were checked during the initial as-user import. 4730 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4731 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4732 return Success; 4733 } 4734 } 4735 4736 return Result; 4737 } 4738 4739 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4740 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4741 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4742 bool ValidateDiagnosticOptions) { 4743 // Initialize a stream. 4744 BitstreamCursor Stream(StreamData); 4745 4746 // Sniff for the signature. 4747 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4748 // FIXME this drops the error on the floor. 4749 consumeError(std::move(Err)); 4750 return Failure; 4751 } 4752 4753 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4754 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4755 return Failure; 4756 4757 // Read all of the records in the options block. 4758 RecordData Record; 4759 ASTReadResult Result = Success; 4760 while (true) { 4761 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4762 if (!MaybeEntry) { 4763 // FIXME this drops the error on the floor. 4764 consumeError(MaybeEntry.takeError()); 4765 return Failure; 4766 } 4767 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4768 4769 switch (Entry.Kind) { 4770 case llvm::BitstreamEntry::Error: 4771 case llvm::BitstreamEntry::SubBlock: 4772 return Failure; 4773 4774 case llvm::BitstreamEntry::EndBlock: 4775 return Result; 4776 4777 case llvm::BitstreamEntry::Record: 4778 // The interesting case. 4779 break; 4780 } 4781 4782 // Read and process a record. 4783 Record.clear(); 4784 StringRef Blob; 4785 Expected<unsigned> MaybeRecordType = 4786 Stream.readRecord(Entry.ID, Record, &Blob); 4787 if (!MaybeRecordType) { 4788 // FIXME this drops the error. 4789 return Failure; 4790 } 4791 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4792 case SIGNATURE: 4793 if (F) 4794 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4795 break; 4796 case AST_BLOCK_HASH: 4797 if (F) 4798 F->ASTBlockHash = 4799 ASTFileSignature::create(Record.begin(), Record.end()); 4800 break; 4801 case DIAGNOSTIC_OPTIONS: { 4802 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4803 if (Listener && ValidateDiagnosticOptions && 4804 !AllowCompatibleConfigurationMismatch && 4805 ParseDiagnosticOptions(Record, Complain, *Listener)) 4806 Result = OutOfDate; // Don't return early. Read the signature. 4807 break; 4808 } 4809 case DIAG_PRAGMA_MAPPINGS: 4810 if (!F) 4811 break; 4812 if (F->PragmaDiagMappings.empty()) 4813 F->PragmaDiagMappings.swap(Record); 4814 else 4815 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4816 Record.begin(), Record.end()); 4817 break; 4818 case HEADER_SEARCH_ENTRY_USAGE: 4819 if (!F) 4820 break; 4821 unsigned Count = Record[0]; 4822 const char *Byte = Blob.data(); 4823 F->SearchPathUsage = llvm::BitVector(Count, false); 4824 for (unsigned I = 0; I < Count; ++Byte) 4825 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I) 4826 if (*Byte & (1 << Bit)) 4827 F->SearchPathUsage[I] = true; 4828 break; 4829 } 4830 } 4831 } 4832 4833 /// Parse a record and blob containing module file extension metadata. 4834 static bool parseModuleFileExtensionMetadata( 4835 const SmallVectorImpl<uint64_t> &Record, 4836 StringRef Blob, 4837 ModuleFileExtensionMetadata &Metadata) { 4838 if (Record.size() < 4) return true; 4839 4840 Metadata.MajorVersion = Record[0]; 4841 Metadata.MinorVersion = Record[1]; 4842 4843 unsigned BlockNameLen = Record[2]; 4844 unsigned UserInfoLen = Record[3]; 4845 4846 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4847 4848 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4849 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4850 Blob.data() + BlockNameLen + UserInfoLen); 4851 return false; 4852 } 4853 4854 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) { 4855 BitstreamCursor &Stream = F.Stream; 4856 4857 RecordData Record; 4858 while (true) { 4859 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4860 if (!MaybeEntry) 4861 return MaybeEntry.takeError(); 4862 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4863 4864 switch (Entry.Kind) { 4865 case llvm::BitstreamEntry::SubBlock: 4866 if (llvm::Error Err = Stream.SkipBlock()) 4867 return Err; 4868 continue; 4869 case llvm::BitstreamEntry::EndBlock: 4870 return llvm::Error::success(); 4871 case llvm::BitstreamEntry::Error: 4872 return llvm::createStringError(std::errc::illegal_byte_sequence, 4873 "malformed block record in AST file"); 4874 case llvm::BitstreamEntry::Record: 4875 break; 4876 } 4877 4878 Record.clear(); 4879 StringRef Blob; 4880 Expected<unsigned> MaybeRecCode = 4881 Stream.readRecord(Entry.ID, Record, &Blob); 4882 if (!MaybeRecCode) 4883 return MaybeRecCode.takeError(); 4884 switch (MaybeRecCode.get()) { 4885 case EXTENSION_METADATA: { 4886 ModuleFileExtensionMetadata Metadata; 4887 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4888 return llvm::createStringError( 4889 std::errc::illegal_byte_sequence, 4890 "malformed EXTENSION_METADATA in AST file"); 4891 4892 // Find a module file extension with this block name. 4893 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4894 if (Known == ModuleFileExtensions.end()) break; 4895 4896 // Form a reader. 4897 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4898 F, Stream)) { 4899 F.ExtensionReaders.push_back(std::move(Reader)); 4900 } 4901 4902 break; 4903 } 4904 } 4905 } 4906 4907 return llvm::Error::success(); 4908 } 4909 4910 void ASTReader::InitializeContext() { 4911 assert(ContextObj && "no context to initialize"); 4912 ASTContext &Context = *ContextObj; 4913 4914 // If there's a listener, notify them that we "read" the translation unit. 4915 if (DeserializationListener) 4916 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4917 Context.getTranslationUnitDecl()); 4918 4919 // FIXME: Find a better way to deal with collisions between these 4920 // built-in types. Right now, we just ignore the problem. 4921 4922 // Load the special types. 4923 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4924 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4925 if (!Context.CFConstantStringTypeDecl) 4926 Context.setCFConstantStringType(GetType(String)); 4927 } 4928 4929 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4930 QualType FileType = GetType(File); 4931 if (FileType.isNull()) { 4932 Error("FILE type is NULL"); 4933 return; 4934 } 4935 4936 if (!Context.FILEDecl) { 4937 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4938 Context.setFILEDecl(Typedef->getDecl()); 4939 else { 4940 const TagType *Tag = FileType->getAs<TagType>(); 4941 if (!Tag) { 4942 Error("Invalid FILE type in AST file"); 4943 return; 4944 } 4945 Context.setFILEDecl(Tag->getDecl()); 4946 } 4947 } 4948 } 4949 4950 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4951 QualType Jmp_bufType = GetType(Jmp_buf); 4952 if (Jmp_bufType.isNull()) { 4953 Error("jmp_buf type is NULL"); 4954 return; 4955 } 4956 4957 if (!Context.jmp_bufDecl) { 4958 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4959 Context.setjmp_bufDecl(Typedef->getDecl()); 4960 else { 4961 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4962 if (!Tag) { 4963 Error("Invalid jmp_buf type in AST file"); 4964 return; 4965 } 4966 Context.setjmp_bufDecl(Tag->getDecl()); 4967 } 4968 } 4969 } 4970 4971 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4972 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4973 if (Sigjmp_bufType.isNull()) { 4974 Error("sigjmp_buf type is NULL"); 4975 return; 4976 } 4977 4978 if (!Context.sigjmp_bufDecl) { 4979 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4980 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4981 else { 4982 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4983 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4984 Context.setsigjmp_bufDecl(Tag->getDecl()); 4985 } 4986 } 4987 } 4988 4989 if (unsigned ObjCIdRedef 4990 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4991 if (Context.ObjCIdRedefinitionType.isNull()) 4992 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4993 } 4994 4995 if (unsigned ObjCClassRedef 4996 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4997 if (Context.ObjCClassRedefinitionType.isNull()) 4998 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4999 } 5000 5001 if (unsigned ObjCSelRedef 5002 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 5003 if (Context.ObjCSelRedefinitionType.isNull()) 5004 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 5005 } 5006 5007 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 5008 QualType Ucontext_tType = GetType(Ucontext_t); 5009 if (Ucontext_tType.isNull()) { 5010 Error("ucontext_t type is NULL"); 5011 return; 5012 } 5013 5014 if (!Context.ucontext_tDecl) { 5015 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 5016 Context.setucontext_tDecl(Typedef->getDecl()); 5017 else { 5018 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 5019 assert(Tag && "Invalid ucontext_t type in AST file"); 5020 Context.setucontext_tDecl(Tag->getDecl()); 5021 } 5022 } 5023 } 5024 } 5025 5026 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 5027 5028 // If there were any CUDA special declarations, deserialize them. 5029 if (!CUDASpecialDeclRefs.empty()) { 5030 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 5031 Context.setcudaConfigureCallDecl( 5032 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 5033 } 5034 5035 // Re-export any modules that were imported by a non-module AST file. 5036 // FIXME: This does not make macro-only imports visible again. 5037 for (auto &Import : ImportedModules) { 5038 if (Module *Imported = getSubmodule(Import.ID)) { 5039 makeModuleVisible(Imported, Module::AllVisible, 5040 /*ImportLoc=*/Import.ImportLoc); 5041 if (Import.ImportLoc.isValid()) 5042 PP.makeModuleVisible(Imported, Import.ImportLoc); 5043 // This updates visibility for Preprocessor only. For Sema, which can be 5044 // nullptr here, we do the same later, in UpdateSema(). 5045 } 5046 } 5047 } 5048 5049 void ASTReader::finalizeForWriting() { 5050 // Nothing to do for now. 5051 } 5052 5053 /// Reads and return the signature record from \p PCH's control block, or 5054 /// else returns 0. 5055 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5056 BitstreamCursor Stream(PCH); 5057 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5058 // FIXME this drops the error on the floor. 5059 consumeError(std::move(Err)); 5060 return ASTFileSignature(); 5061 } 5062 5063 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5064 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5065 return ASTFileSignature(); 5066 5067 // Scan for SIGNATURE inside the diagnostic options block. 5068 ASTReader::RecordData Record; 5069 while (true) { 5070 Expected<llvm::BitstreamEntry> MaybeEntry = 5071 Stream.advanceSkippingSubblocks(); 5072 if (!MaybeEntry) { 5073 // FIXME this drops the error on the floor. 5074 consumeError(MaybeEntry.takeError()); 5075 return ASTFileSignature(); 5076 } 5077 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5078 5079 if (Entry.Kind != llvm::BitstreamEntry::Record) 5080 return ASTFileSignature(); 5081 5082 Record.clear(); 5083 StringRef Blob; 5084 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5085 if (!MaybeRecord) { 5086 // FIXME this drops the error on the floor. 5087 consumeError(MaybeRecord.takeError()); 5088 return ASTFileSignature(); 5089 } 5090 if (SIGNATURE == MaybeRecord.get()) 5091 return ASTFileSignature::create(Record.begin(), 5092 Record.begin() + ASTFileSignature::size); 5093 } 5094 } 5095 5096 /// Retrieve the name of the original source file name 5097 /// directly from the AST file, without actually loading the AST 5098 /// file. 5099 std::string ASTReader::getOriginalSourceFile( 5100 const std::string &ASTFileName, FileManager &FileMgr, 5101 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5102 // Open the AST file. 5103 auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false, 5104 /*RequiresNullTerminator=*/false); 5105 if (!Buffer) { 5106 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5107 << ASTFileName << Buffer.getError().message(); 5108 return std::string(); 5109 } 5110 5111 // Initialize the stream 5112 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5113 5114 // Sniff for the signature. 5115 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5116 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5117 return std::string(); 5118 } 5119 5120 // Scan for the CONTROL_BLOCK_ID block. 5121 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5122 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5123 return std::string(); 5124 } 5125 5126 // Scan for ORIGINAL_FILE inside the control block. 5127 RecordData Record; 5128 while (true) { 5129 Expected<llvm::BitstreamEntry> MaybeEntry = 5130 Stream.advanceSkippingSubblocks(); 5131 if (!MaybeEntry) { 5132 // FIXME this drops errors on the floor. 5133 consumeError(MaybeEntry.takeError()); 5134 return std::string(); 5135 } 5136 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5137 5138 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5139 return std::string(); 5140 5141 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5142 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5143 return std::string(); 5144 } 5145 5146 Record.clear(); 5147 StringRef Blob; 5148 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5149 if (!MaybeRecord) { 5150 // FIXME this drops the errors on the floor. 5151 consumeError(MaybeRecord.takeError()); 5152 return std::string(); 5153 } 5154 if (ORIGINAL_FILE == MaybeRecord.get()) 5155 return Blob.str(); 5156 } 5157 } 5158 5159 namespace { 5160 5161 class SimplePCHValidator : public ASTReaderListener { 5162 const LangOptions &ExistingLangOpts; 5163 const TargetOptions &ExistingTargetOpts; 5164 const PreprocessorOptions &ExistingPPOpts; 5165 std::string ExistingModuleCachePath; 5166 FileManager &FileMgr; 5167 bool StrictOptionMatches; 5168 5169 public: 5170 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5171 const TargetOptions &ExistingTargetOpts, 5172 const PreprocessorOptions &ExistingPPOpts, 5173 StringRef ExistingModuleCachePath, FileManager &FileMgr, 5174 bool StrictOptionMatches) 5175 : ExistingLangOpts(ExistingLangOpts), 5176 ExistingTargetOpts(ExistingTargetOpts), 5177 ExistingPPOpts(ExistingPPOpts), 5178 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr), 5179 StrictOptionMatches(StrictOptionMatches) {} 5180 5181 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5182 bool AllowCompatibleDifferences) override { 5183 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5184 AllowCompatibleDifferences); 5185 } 5186 5187 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5188 bool AllowCompatibleDifferences) override { 5189 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5190 AllowCompatibleDifferences); 5191 } 5192 5193 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5194 StringRef SpecificModuleCachePath, 5195 bool Complain) override { 5196 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5197 ExistingModuleCachePath, nullptr, 5198 ExistingLangOpts, ExistingPPOpts); 5199 } 5200 5201 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5202 bool Complain, 5203 std::string &SuggestedPredefines) override { 5204 return checkPreprocessorOptions( 5205 PPOpts, ExistingPPOpts, /*Diags=*/nullptr, FileMgr, 5206 SuggestedPredefines, ExistingLangOpts, 5207 StrictOptionMatches ? OptionValidateStrictMatches 5208 : OptionValidateContradictions); 5209 } 5210 }; 5211 5212 } // namespace 5213 5214 bool ASTReader::readASTFileControlBlock( 5215 StringRef Filename, FileManager &FileMgr, 5216 const PCHContainerReader &PCHContainerRdr, 5217 bool FindModuleFileExtensions, 5218 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5219 // Open the AST file. 5220 // FIXME: This allows use of the VFS; we do not allow use of the 5221 // VFS when actually loading a module. 5222 auto Buffer = FileMgr.getBufferForFile(Filename); 5223 if (!Buffer) { 5224 return true; 5225 } 5226 5227 // Initialize the stream 5228 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5229 BitstreamCursor Stream(Bytes); 5230 5231 // Sniff for the signature. 5232 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5233 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5234 return true; 5235 } 5236 5237 // Scan for the CONTROL_BLOCK_ID block. 5238 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5239 return true; 5240 5241 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5242 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5243 bool NeedsImports = Listener.needsImportVisitation(); 5244 BitstreamCursor InputFilesCursor; 5245 5246 RecordData Record; 5247 std::string ModuleDir; 5248 bool DoneWithControlBlock = false; 5249 while (!DoneWithControlBlock) { 5250 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5251 if (!MaybeEntry) { 5252 // FIXME this drops the error on the floor. 5253 consumeError(MaybeEntry.takeError()); 5254 return true; 5255 } 5256 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5257 5258 switch (Entry.Kind) { 5259 case llvm::BitstreamEntry::SubBlock: { 5260 switch (Entry.ID) { 5261 case OPTIONS_BLOCK_ID: { 5262 std::string IgnoredSuggestedPredefines; 5263 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5264 /*AllowCompatibleConfigurationMismatch*/ false, 5265 Listener, IgnoredSuggestedPredefines) != Success) 5266 return true; 5267 break; 5268 } 5269 5270 case INPUT_FILES_BLOCK_ID: 5271 InputFilesCursor = Stream; 5272 if (llvm::Error Err = Stream.SkipBlock()) { 5273 // FIXME this drops the error on the floor. 5274 consumeError(std::move(Err)); 5275 return true; 5276 } 5277 if (NeedsInputFiles && 5278 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5279 return true; 5280 break; 5281 5282 default: 5283 if (llvm::Error Err = Stream.SkipBlock()) { 5284 // FIXME this drops the error on the floor. 5285 consumeError(std::move(Err)); 5286 return true; 5287 } 5288 break; 5289 } 5290 5291 continue; 5292 } 5293 5294 case llvm::BitstreamEntry::EndBlock: 5295 DoneWithControlBlock = true; 5296 break; 5297 5298 case llvm::BitstreamEntry::Error: 5299 return true; 5300 5301 case llvm::BitstreamEntry::Record: 5302 break; 5303 } 5304 5305 if (DoneWithControlBlock) break; 5306 5307 Record.clear(); 5308 StringRef Blob; 5309 Expected<unsigned> MaybeRecCode = 5310 Stream.readRecord(Entry.ID, Record, &Blob); 5311 if (!MaybeRecCode) { 5312 // FIXME this drops the error. 5313 return Failure; 5314 } 5315 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5316 case METADATA: 5317 if (Record[0] != VERSION_MAJOR) 5318 return true; 5319 if (Listener.ReadFullVersionInformation(Blob)) 5320 return true; 5321 break; 5322 case MODULE_NAME: 5323 Listener.ReadModuleName(Blob); 5324 break; 5325 case MODULE_DIRECTORY: 5326 ModuleDir = std::string(Blob); 5327 break; 5328 case MODULE_MAP_FILE: { 5329 unsigned Idx = 0; 5330 auto Path = ReadString(Record, Idx); 5331 ResolveImportedPath(Path, ModuleDir); 5332 Listener.ReadModuleMapFile(Path); 5333 break; 5334 } 5335 case INPUT_FILE_OFFSETS: { 5336 if (!NeedsInputFiles) 5337 break; 5338 5339 unsigned NumInputFiles = Record[0]; 5340 unsigned NumUserFiles = Record[1]; 5341 const llvm::support::unaligned_uint64_t *InputFileOffs = 5342 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5343 for (unsigned I = 0; I != NumInputFiles; ++I) { 5344 // Go find this input file. 5345 bool isSystemFile = I >= NumUserFiles; 5346 5347 if (isSystemFile && !NeedsSystemInputFiles) 5348 break; // the rest are system input files 5349 5350 BitstreamCursor &Cursor = InputFilesCursor; 5351 SavedStreamPosition SavedPosition(Cursor); 5352 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5353 // FIXME this drops errors on the floor. 5354 consumeError(std::move(Err)); 5355 } 5356 5357 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5358 if (!MaybeCode) { 5359 // FIXME this drops errors on the floor. 5360 consumeError(MaybeCode.takeError()); 5361 } 5362 unsigned Code = MaybeCode.get(); 5363 5364 RecordData Record; 5365 StringRef Blob; 5366 bool shouldContinue = false; 5367 Expected<unsigned> MaybeRecordType = 5368 Cursor.readRecord(Code, Record, &Blob); 5369 if (!MaybeRecordType) { 5370 // FIXME this drops errors on the floor. 5371 consumeError(MaybeRecordType.takeError()); 5372 } 5373 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5374 case INPUT_FILE_HASH: 5375 break; 5376 case INPUT_FILE: 5377 bool Overridden = static_cast<bool>(Record[3]); 5378 std::string Filename = std::string(Blob); 5379 ResolveImportedPath(Filename, ModuleDir); 5380 shouldContinue = Listener.visitInputFile( 5381 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5382 break; 5383 } 5384 if (!shouldContinue) 5385 break; 5386 } 5387 break; 5388 } 5389 5390 case IMPORTS: { 5391 if (!NeedsImports) 5392 break; 5393 5394 unsigned Idx = 0, N = Record.size(); 5395 while (Idx < N) { 5396 // Read information about the AST file. 5397 Idx += 5398 1 + 1 + 1 + 1 + 5399 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5400 std::string ModuleName = ReadString(Record, Idx); 5401 std::string Filename = ReadString(Record, Idx); 5402 ResolveImportedPath(Filename, ModuleDir); 5403 Listener.visitImport(ModuleName, Filename); 5404 } 5405 break; 5406 } 5407 5408 default: 5409 // No other validation to perform. 5410 break; 5411 } 5412 } 5413 5414 // Look for module file extension blocks, if requested. 5415 if (FindModuleFileExtensions) { 5416 BitstreamCursor SavedStream = Stream; 5417 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5418 bool DoneWithExtensionBlock = false; 5419 while (!DoneWithExtensionBlock) { 5420 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5421 if (!MaybeEntry) { 5422 // FIXME this drops the error. 5423 return true; 5424 } 5425 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5426 5427 switch (Entry.Kind) { 5428 case llvm::BitstreamEntry::SubBlock: 5429 if (llvm::Error Err = Stream.SkipBlock()) { 5430 // FIXME this drops the error on the floor. 5431 consumeError(std::move(Err)); 5432 return true; 5433 } 5434 continue; 5435 5436 case llvm::BitstreamEntry::EndBlock: 5437 DoneWithExtensionBlock = true; 5438 continue; 5439 5440 case llvm::BitstreamEntry::Error: 5441 return true; 5442 5443 case llvm::BitstreamEntry::Record: 5444 break; 5445 } 5446 5447 Record.clear(); 5448 StringRef Blob; 5449 Expected<unsigned> MaybeRecCode = 5450 Stream.readRecord(Entry.ID, Record, &Blob); 5451 if (!MaybeRecCode) { 5452 // FIXME this drops the error. 5453 return true; 5454 } 5455 switch (MaybeRecCode.get()) { 5456 case EXTENSION_METADATA: { 5457 ModuleFileExtensionMetadata Metadata; 5458 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5459 return true; 5460 5461 Listener.readModuleFileExtension(Metadata); 5462 break; 5463 } 5464 } 5465 } 5466 } 5467 Stream = SavedStream; 5468 } 5469 5470 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5471 if (readUnhashedControlBlockImpl( 5472 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5473 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5474 ValidateDiagnosticOptions) != Success) 5475 return true; 5476 5477 return false; 5478 } 5479 5480 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5481 const PCHContainerReader &PCHContainerRdr, 5482 const LangOptions &LangOpts, 5483 const TargetOptions &TargetOpts, 5484 const PreprocessorOptions &PPOpts, 5485 StringRef ExistingModuleCachePath, 5486 bool RequireStrictOptionMatches) { 5487 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5488 ExistingModuleCachePath, FileMgr, 5489 RequireStrictOptionMatches); 5490 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5491 /*FindModuleFileExtensions=*/false, 5492 validator, 5493 /*ValidateDiagnosticOptions=*/true); 5494 } 5495 5496 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F, 5497 unsigned ClientLoadCapabilities) { 5498 // Enter the submodule block. 5499 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) 5500 return Err; 5501 5502 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5503 bool First = true; 5504 Module *CurrentModule = nullptr; 5505 RecordData Record; 5506 while (true) { 5507 Expected<llvm::BitstreamEntry> MaybeEntry = 5508 F.Stream.advanceSkippingSubblocks(); 5509 if (!MaybeEntry) 5510 return MaybeEntry.takeError(); 5511 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5512 5513 switch (Entry.Kind) { 5514 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5515 case llvm::BitstreamEntry::Error: 5516 return llvm::createStringError(std::errc::illegal_byte_sequence, 5517 "malformed block record in AST file"); 5518 case llvm::BitstreamEntry::EndBlock: 5519 return llvm::Error::success(); 5520 case llvm::BitstreamEntry::Record: 5521 // The interesting case. 5522 break; 5523 } 5524 5525 // Read a record. 5526 StringRef Blob; 5527 Record.clear(); 5528 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5529 if (!MaybeKind) 5530 return MaybeKind.takeError(); 5531 unsigned Kind = MaybeKind.get(); 5532 5533 if ((Kind == SUBMODULE_METADATA) != First) 5534 return llvm::createStringError( 5535 std::errc::illegal_byte_sequence, 5536 "submodule metadata record should be at beginning of block"); 5537 First = false; 5538 5539 // Submodule information is only valid if we have a current module. 5540 // FIXME: Should we error on these cases? 5541 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5542 Kind != SUBMODULE_DEFINITION) 5543 continue; 5544 5545 switch (Kind) { 5546 default: // Default behavior: ignore. 5547 break; 5548 5549 case SUBMODULE_DEFINITION: { 5550 if (Record.size() < 12) 5551 return llvm::createStringError(std::errc::illegal_byte_sequence, 5552 "malformed module definition"); 5553 5554 StringRef Name = Blob; 5555 unsigned Idx = 0; 5556 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5557 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5558 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5559 bool IsFramework = Record[Idx++]; 5560 bool IsExplicit = Record[Idx++]; 5561 bool IsSystem = Record[Idx++]; 5562 bool IsExternC = Record[Idx++]; 5563 bool InferSubmodules = Record[Idx++]; 5564 bool InferExplicitSubmodules = Record[Idx++]; 5565 bool InferExportWildcard = Record[Idx++]; 5566 bool ConfigMacrosExhaustive = Record[Idx++]; 5567 bool ModuleMapIsPrivate = Record[Idx++]; 5568 5569 Module *ParentModule = nullptr; 5570 if (Parent) 5571 ParentModule = getSubmodule(Parent); 5572 5573 // Retrieve this (sub)module from the module map, creating it if 5574 // necessary. 5575 CurrentModule = 5576 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5577 .first; 5578 5579 // FIXME: set the definition loc for CurrentModule, or call 5580 // ModMap.setInferredModuleAllowedBy() 5581 5582 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5583 if (GlobalIndex >= SubmodulesLoaded.size() || 5584 SubmodulesLoaded[GlobalIndex]) 5585 return llvm::createStringError(std::errc::invalid_argument, 5586 "too many submodules"); 5587 5588 if (!ParentModule) { 5589 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5590 // Don't emit module relocation error if we have -fno-validate-pch 5591 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 5592 DisableValidationForModuleKind::Module) && 5593 CurFile != F.File) { 5594 auto ConflictError = 5595 PartialDiagnostic(diag::err_module_file_conflict, 5596 ContextObj->DiagAllocator) 5597 << CurrentModule->getTopLevelModuleName() << CurFile->getName() 5598 << F.File->getName(); 5599 return DiagnosticError::create(CurrentImportLoc, ConflictError); 5600 } 5601 } 5602 5603 F.DidReadTopLevelSubmodule = true; 5604 CurrentModule->setASTFile(F.File); 5605 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5606 } 5607 5608 CurrentModule->Kind = Kind; 5609 CurrentModule->Signature = F.Signature; 5610 CurrentModule->IsFromModuleFile = true; 5611 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5612 CurrentModule->IsExternC = IsExternC; 5613 CurrentModule->InferSubmodules = InferSubmodules; 5614 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5615 CurrentModule->InferExportWildcard = InferExportWildcard; 5616 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5617 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5618 if (DeserializationListener) 5619 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5620 5621 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5622 5623 // Clear out data that will be replaced by what is in the module file. 5624 CurrentModule->LinkLibraries.clear(); 5625 CurrentModule->ConfigMacros.clear(); 5626 CurrentModule->UnresolvedConflicts.clear(); 5627 CurrentModule->Conflicts.clear(); 5628 5629 // The module is available unless it's missing a requirement; relevant 5630 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5631 // Missing headers that were present when the module was built do not 5632 // make it unavailable -- if we got this far, this must be an explicitly 5633 // imported module file. 5634 CurrentModule->Requirements.clear(); 5635 CurrentModule->MissingHeaders.clear(); 5636 CurrentModule->IsUnimportable = 5637 ParentModule && ParentModule->IsUnimportable; 5638 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5639 break; 5640 } 5641 5642 case SUBMODULE_UMBRELLA_HEADER: { 5643 // FIXME: This doesn't work for framework modules as `Filename` is the 5644 // name as written in the module file and does not include 5645 // `Headers/`, so this path will never exist. 5646 std::string Filename = std::string(Blob); 5647 ResolveImportedPath(F, Filename); 5648 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5649 if (!CurrentModule->getUmbrellaHeader()) { 5650 // FIXME: NameAsWritten 5651 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, ""); 5652 } 5653 // Note that it's too late at this point to return out of date if the 5654 // name from the PCM doesn't match up with the one in the module map, 5655 // but also quite unlikely since we will have already checked the 5656 // modification time and size of the module map file itself. 5657 } 5658 break; 5659 } 5660 5661 case SUBMODULE_HEADER: 5662 case SUBMODULE_EXCLUDED_HEADER: 5663 case SUBMODULE_PRIVATE_HEADER: 5664 // We lazily associate headers with their modules via the HeaderInfo table. 5665 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5666 // of complete filenames or remove it entirely. 5667 break; 5668 5669 case SUBMODULE_TEXTUAL_HEADER: 5670 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5671 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5672 // them here. 5673 break; 5674 5675 case SUBMODULE_TOPHEADER: { 5676 std::string HeaderName(Blob); 5677 ResolveImportedPath(F, HeaderName); 5678 CurrentModule->addTopHeaderFilename(HeaderName); 5679 break; 5680 } 5681 5682 case SUBMODULE_UMBRELLA_DIR: { 5683 // See comments in SUBMODULE_UMBRELLA_HEADER 5684 std::string Dirname = std::string(Blob); 5685 ResolveImportedPath(F, Dirname); 5686 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5687 if (!CurrentModule->getUmbrellaDir()) { 5688 // FIXME: NameAsWritten 5689 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, ""); 5690 } 5691 } 5692 break; 5693 } 5694 5695 case SUBMODULE_METADATA: { 5696 F.BaseSubmoduleID = getTotalNumSubmodules(); 5697 F.LocalNumSubmodules = Record[0]; 5698 unsigned LocalBaseSubmoduleID = Record[1]; 5699 if (F.LocalNumSubmodules > 0) { 5700 // Introduce the global -> local mapping for submodules within this 5701 // module. 5702 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5703 5704 // Introduce the local -> global mapping for submodules within this 5705 // module. 5706 F.SubmoduleRemap.insertOrReplace( 5707 std::make_pair(LocalBaseSubmoduleID, 5708 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5709 5710 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5711 } 5712 break; 5713 } 5714 5715 case SUBMODULE_IMPORTS: 5716 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5717 UnresolvedModuleRef Unresolved; 5718 Unresolved.File = &F; 5719 Unresolved.Mod = CurrentModule; 5720 Unresolved.ID = Record[Idx]; 5721 Unresolved.Kind = UnresolvedModuleRef::Import; 5722 Unresolved.IsWildcard = false; 5723 UnresolvedModuleRefs.push_back(Unresolved); 5724 } 5725 break; 5726 5727 case SUBMODULE_EXPORTS: 5728 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5729 UnresolvedModuleRef Unresolved; 5730 Unresolved.File = &F; 5731 Unresolved.Mod = CurrentModule; 5732 Unresolved.ID = Record[Idx]; 5733 Unresolved.Kind = UnresolvedModuleRef::Export; 5734 Unresolved.IsWildcard = Record[Idx + 1]; 5735 UnresolvedModuleRefs.push_back(Unresolved); 5736 } 5737 5738 // Once we've loaded the set of exports, there's no reason to keep 5739 // the parsed, unresolved exports around. 5740 CurrentModule->UnresolvedExports.clear(); 5741 break; 5742 5743 case SUBMODULE_REQUIRES: 5744 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5745 PP.getTargetInfo()); 5746 break; 5747 5748 case SUBMODULE_LINK_LIBRARY: 5749 ModMap.resolveLinkAsDependencies(CurrentModule); 5750 CurrentModule->LinkLibraries.push_back( 5751 Module::LinkLibrary(std::string(Blob), Record[0])); 5752 break; 5753 5754 case SUBMODULE_CONFIG_MACRO: 5755 CurrentModule->ConfigMacros.push_back(Blob.str()); 5756 break; 5757 5758 case SUBMODULE_CONFLICT: { 5759 UnresolvedModuleRef Unresolved; 5760 Unresolved.File = &F; 5761 Unresolved.Mod = CurrentModule; 5762 Unresolved.ID = Record[0]; 5763 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5764 Unresolved.IsWildcard = false; 5765 Unresolved.String = Blob; 5766 UnresolvedModuleRefs.push_back(Unresolved); 5767 break; 5768 } 5769 5770 case SUBMODULE_INITIALIZERS: { 5771 if (!ContextObj) 5772 break; 5773 SmallVector<uint32_t, 16> Inits; 5774 for (auto &ID : Record) 5775 Inits.push_back(getGlobalDeclID(F, ID)); 5776 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5777 break; 5778 } 5779 5780 case SUBMODULE_EXPORT_AS: 5781 CurrentModule->ExportAsModule = Blob.str(); 5782 ModMap.addLinkAsDependency(CurrentModule); 5783 break; 5784 } 5785 } 5786 } 5787 5788 /// Parse the record that corresponds to a LangOptions data 5789 /// structure. 5790 /// 5791 /// This routine parses the language options from the AST file and then gives 5792 /// them to the AST listener if one is set. 5793 /// 5794 /// \returns true if the listener deems the file unacceptable, false otherwise. 5795 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5796 bool Complain, 5797 ASTReaderListener &Listener, 5798 bool AllowCompatibleDifferences) { 5799 LangOptions LangOpts; 5800 unsigned Idx = 0; 5801 #define LANGOPT(Name, Bits, Default, Description) \ 5802 LangOpts.Name = Record[Idx++]; 5803 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5804 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5805 #include "clang/Basic/LangOptions.def" 5806 #define SANITIZER(NAME, ID) \ 5807 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5808 #include "clang/Basic/Sanitizers.def" 5809 5810 for (unsigned N = Record[Idx++]; N; --N) 5811 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5812 5813 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5814 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5815 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5816 5817 LangOpts.CurrentModule = ReadString(Record, Idx); 5818 5819 // Comment options. 5820 for (unsigned N = Record[Idx++]; N; --N) { 5821 LangOpts.CommentOpts.BlockCommandNames.push_back( 5822 ReadString(Record, Idx)); 5823 } 5824 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5825 5826 // OpenMP offloading options. 5827 for (unsigned N = Record[Idx++]; N; --N) { 5828 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5829 } 5830 5831 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5832 5833 return Listener.ReadLanguageOptions(LangOpts, Complain, 5834 AllowCompatibleDifferences); 5835 } 5836 5837 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5838 ASTReaderListener &Listener, 5839 bool AllowCompatibleDifferences) { 5840 unsigned Idx = 0; 5841 TargetOptions TargetOpts; 5842 TargetOpts.Triple = ReadString(Record, Idx); 5843 TargetOpts.CPU = ReadString(Record, Idx); 5844 TargetOpts.TuneCPU = ReadString(Record, Idx); 5845 TargetOpts.ABI = ReadString(Record, Idx); 5846 for (unsigned N = Record[Idx++]; N; --N) { 5847 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5848 } 5849 for (unsigned N = Record[Idx++]; N; --N) { 5850 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5851 } 5852 5853 return Listener.ReadTargetOptions(TargetOpts, Complain, 5854 AllowCompatibleDifferences); 5855 } 5856 5857 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5858 ASTReaderListener &Listener) { 5859 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5860 unsigned Idx = 0; 5861 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5862 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5863 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5864 #include "clang/Basic/DiagnosticOptions.def" 5865 5866 for (unsigned N = Record[Idx++]; N; --N) 5867 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5868 for (unsigned N = Record[Idx++]; N; --N) 5869 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5870 5871 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5872 } 5873 5874 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5875 ASTReaderListener &Listener) { 5876 FileSystemOptions FSOpts; 5877 unsigned Idx = 0; 5878 FSOpts.WorkingDir = ReadString(Record, Idx); 5879 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5880 } 5881 5882 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5883 bool Complain, 5884 ASTReaderListener &Listener) { 5885 HeaderSearchOptions HSOpts; 5886 unsigned Idx = 0; 5887 HSOpts.Sysroot = ReadString(Record, Idx); 5888 5889 // Include entries. 5890 for (unsigned N = Record[Idx++]; N; --N) { 5891 std::string Path = ReadString(Record, Idx); 5892 frontend::IncludeDirGroup Group 5893 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5894 bool IsFramework = Record[Idx++]; 5895 bool IgnoreSysRoot = Record[Idx++]; 5896 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5897 IgnoreSysRoot); 5898 } 5899 5900 // System header prefixes. 5901 for (unsigned N = Record[Idx++]; N; --N) { 5902 std::string Prefix = ReadString(Record, Idx); 5903 bool IsSystemHeader = Record[Idx++]; 5904 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5905 } 5906 5907 HSOpts.ResourceDir = ReadString(Record, Idx); 5908 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5909 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5910 HSOpts.DisableModuleHash = Record[Idx++]; 5911 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5912 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5913 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5914 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5915 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5916 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5917 HSOpts.UseLibcxx = Record[Idx++]; 5918 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5919 5920 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5921 Complain); 5922 } 5923 5924 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5925 bool Complain, 5926 ASTReaderListener &Listener, 5927 std::string &SuggestedPredefines) { 5928 PreprocessorOptions PPOpts; 5929 unsigned Idx = 0; 5930 5931 // Macro definitions/undefs 5932 for (unsigned N = Record[Idx++]; N; --N) { 5933 std::string Macro = ReadString(Record, Idx); 5934 bool IsUndef = Record[Idx++]; 5935 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5936 } 5937 5938 // Includes 5939 for (unsigned N = Record[Idx++]; N; --N) { 5940 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5941 } 5942 5943 // Macro Includes 5944 for (unsigned N = Record[Idx++]; N; --N) { 5945 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5946 } 5947 5948 PPOpts.UsePredefines = Record[Idx++]; 5949 PPOpts.DetailedRecord = Record[Idx++]; 5950 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5951 PPOpts.ObjCXXARCStandardLibrary = 5952 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5953 SuggestedPredefines.clear(); 5954 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5955 SuggestedPredefines); 5956 } 5957 5958 std::pair<ModuleFile *, unsigned> 5959 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5960 GlobalPreprocessedEntityMapType::iterator 5961 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5962 assert(I != GlobalPreprocessedEntityMap.end() && 5963 "Corrupted global preprocessed entity map"); 5964 ModuleFile *M = I->second; 5965 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5966 return std::make_pair(M, LocalIndex); 5967 } 5968 5969 llvm::iterator_range<PreprocessingRecord::iterator> 5970 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5971 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5972 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5973 Mod.NumPreprocessedEntities); 5974 5975 return llvm::make_range(PreprocessingRecord::iterator(), 5976 PreprocessingRecord::iterator()); 5977 } 5978 5979 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName, 5980 unsigned int ClientLoadCapabilities) { 5981 return ClientLoadCapabilities & ARR_OutOfDate && 5982 !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName); 5983 } 5984 5985 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5986 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5987 return llvm::make_range( 5988 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5989 ModuleDeclIterator(this, &Mod, 5990 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5991 } 5992 5993 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5994 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5995 assert(I != GlobalSkippedRangeMap.end() && 5996 "Corrupted global skipped range map"); 5997 ModuleFile *M = I->second; 5998 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5999 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 6000 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 6001 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 6002 TranslateSourceLocation(*M, RawRange.getEnd())); 6003 assert(Range.isValid()); 6004 return Range; 6005 } 6006 6007 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 6008 PreprocessedEntityID PPID = Index+1; 6009 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6010 ModuleFile &M = *PPInfo.first; 6011 unsigned LocalIndex = PPInfo.second; 6012 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6013 6014 if (!PP.getPreprocessingRecord()) { 6015 Error("no preprocessing record"); 6016 return nullptr; 6017 } 6018 6019 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 6020 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 6021 M.MacroOffsetsBase + PPOffs.BitOffset)) { 6022 Error(std::move(Err)); 6023 return nullptr; 6024 } 6025 6026 Expected<llvm::BitstreamEntry> MaybeEntry = 6027 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 6028 if (!MaybeEntry) { 6029 Error(MaybeEntry.takeError()); 6030 return nullptr; 6031 } 6032 llvm::BitstreamEntry Entry = MaybeEntry.get(); 6033 6034 if (Entry.Kind != llvm::BitstreamEntry::Record) 6035 return nullptr; 6036 6037 // Read the record. 6038 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 6039 TranslateSourceLocation(M, PPOffs.getEnd())); 6040 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 6041 StringRef Blob; 6042 RecordData Record; 6043 Expected<unsigned> MaybeRecType = 6044 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 6045 if (!MaybeRecType) { 6046 Error(MaybeRecType.takeError()); 6047 return nullptr; 6048 } 6049 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 6050 case PPD_MACRO_EXPANSION: { 6051 bool isBuiltin = Record[0]; 6052 IdentifierInfo *Name = nullptr; 6053 MacroDefinitionRecord *Def = nullptr; 6054 if (isBuiltin) 6055 Name = getLocalIdentifier(M, Record[1]); 6056 else { 6057 PreprocessedEntityID GlobalID = 6058 getGlobalPreprocessedEntityID(M, Record[1]); 6059 Def = cast<MacroDefinitionRecord>( 6060 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 6061 } 6062 6063 MacroExpansion *ME; 6064 if (isBuiltin) 6065 ME = new (PPRec) MacroExpansion(Name, Range); 6066 else 6067 ME = new (PPRec) MacroExpansion(Def, Range); 6068 6069 return ME; 6070 } 6071 6072 case PPD_MACRO_DEFINITION: { 6073 // Decode the identifier info and then check again; if the macro is 6074 // still defined and associated with the identifier, 6075 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6076 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6077 6078 if (DeserializationListener) 6079 DeserializationListener->MacroDefinitionRead(PPID, MD); 6080 6081 return MD; 6082 } 6083 6084 case PPD_INCLUSION_DIRECTIVE: { 6085 const char *FullFileNameStart = Blob.data() + Record[0]; 6086 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6087 Optional<FileEntryRef> File; 6088 if (!FullFileName.empty()) 6089 File = PP.getFileManager().getOptionalFileRef(FullFileName); 6090 6091 // FIXME: Stable encoding 6092 InclusionDirective::InclusionKind Kind 6093 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6094 InclusionDirective *ID 6095 = new (PPRec) InclusionDirective(PPRec, Kind, 6096 StringRef(Blob.data(), Record[0]), 6097 Record[1], Record[3], 6098 File, 6099 Range); 6100 return ID; 6101 } 6102 } 6103 6104 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6105 } 6106 6107 /// Find the next module that contains entities and return the ID 6108 /// of the first entry. 6109 /// 6110 /// \param SLocMapI points at a chunk of a module that contains no 6111 /// preprocessed entities or the entities it contains are not the ones we are 6112 /// looking for. 6113 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6114 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6115 ++SLocMapI; 6116 for (GlobalSLocOffsetMapType::const_iterator 6117 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6118 ModuleFile &M = *SLocMapI->second; 6119 if (M.NumPreprocessedEntities) 6120 return M.BasePreprocessedEntityID; 6121 } 6122 6123 return getTotalNumPreprocessedEntities(); 6124 } 6125 6126 namespace { 6127 6128 struct PPEntityComp { 6129 const ASTReader &Reader; 6130 ModuleFile &M; 6131 6132 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6133 6134 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6135 SourceLocation LHS = getLoc(L); 6136 SourceLocation RHS = getLoc(R); 6137 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6138 } 6139 6140 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6141 SourceLocation LHS = getLoc(L); 6142 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6143 } 6144 6145 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6146 SourceLocation RHS = getLoc(R); 6147 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6148 } 6149 6150 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6151 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6152 } 6153 }; 6154 6155 } // namespace 6156 6157 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6158 bool EndsAfter) const { 6159 if (SourceMgr.isLocalSourceLocation(Loc)) 6160 return getTotalNumPreprocessedEntities(); 6161 6162 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6163 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6164 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6165 "Corrupted global sloc offset map"); 6166 6167 if (SLocMapI->second->NumPreprocessedEntities == 0) 6168 return findNextPreprocessedEntity(SLocMapI); 6169 6170 ModuleFile &M = *SLocMapI->second; 6171 6172 using pp_iterator = const PPEntityOffset *; 6173 6174 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6175 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6176 6177 size_t Count = M.NumPreprocessedEntities; 6178 size_t Half; 6179 pp_iterator First = pp_begin; 6180 pp_iterator PPI; 6181 6182 if (EndsAfter) { 6183 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6184 PPEntityComp(*this, M)); 6185 } else { 6186 // Do a binary search manually instead of using std::lower_bound because 6187 // The end locations of entities may be unordered (when a macro expansion 6188 // is inside another macro argument), but for this case it is not important 6189 // whether we get the first macro expansion or its containing macro. 6190 while (Count > 0) { 6191 Half = Count / 2; 6192 PPI = First; 6193 std::advance(PPI, Half); 6194 if (SourceMgr.isBeforeInTranslationUnit( 6195 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6196 First = PPI; 6197 ++First; 6198 Count = Count - Half - 1; 6199 } else 6200 Count = Half; 6201 } 6202 } 6203 6204 if (PPI == pp_end) 6205 return findNextPreprocessedEntity(SLocMapI); 6206 6207 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6208 } 6209 6210 /// Returns a pair of [Begin, End) indices of preallocated 6211 /// preprocessed entities that \arg Range encompasses. 6212 std::pair<unsigned, unsigned> 6213 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6214 if (Range.isInvalid()) 6215 return std::make_pair(0,0); 6216 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6217 6218 PreprocessedEntityID BeginID = 6219 findPreprocessedEntity(Range.getBegin(), false); 6220 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6221 return std::make_pair(BeginID, EndID); 6222 } 6223 6224 /// Optionally returns true or false if the preallocated preprocessed 6225 /// entity with index \arg Index came from file \arg FID. 6226 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6227 FileID FID) { 6228 if (FID.isInvalid()) 6229 return false; 6230 6231 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6232 ModuleFile &M = *PPInfo.first; 6233 unsigned LocalIndex = PPInfo.second; 6234 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6235 6236 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6237 if (Loc.isInvalid()) 6238 return false; 6239 6240 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6241 return true; 6242 else 6243 return false; 6244 } 6245 6246 namespace { 6247 6248 /// Visitor used to search for information about a header file. 6249 class HeaderFileInfoVisitor { 6250 const FileEntry *FE; 6251 Optional<HeaderFileInfo> HFI; 6252 6253 public: 6254 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6255 6256 bool operator()(ModuleFile &M) { 6257 HeaderFileInfoLookupTable *Table 6258 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6259 if (!Table) 6260 return false; 6261 6262 // Look in the on-disk hash table for an entry for this file name. 6263 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6264 if (Pos == Table->end()) 6265 return false; 6266 6267 HFI = *Pos; 6268 return true; 6269 } 6270 6271 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6272 }; 6273 6274 } // namespace 6275 6276 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6277 HeaderFileInfoVisitor Visitor(FE); 6278 ModuleMgr.visit(Visitor); 6279 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6280 return *HFI; 6281 6282 return HeaderFileInfo(); 6283 } 6284 6285 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6286 using DiagState = DiagnosticsEngine::DiagState; 6287 SmallVector<DiagState *, 32> DiagStates; 6288 6289 for (ModuleFile &F : ModuleMgr) { 6290 unsigned Idx = 0; 6291 auto &Record = F.PragmaDiagMappings; 6292 if (Record.empty()) 6293 continue; 6294 6295 DiagStates.clear(); 6296 6297 auto ReadDiagState = 6298 [&](const DiagState &BasedOn, SourceLocation Loc, 6299 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6300 unsigned BackrefID = Record[Idx++]; 6301 if (BackrefID != 0) 6302 return DiagStates[BackrefID - 1]; 6303 6304 // A new DiagState was created here. 6305 Diag.DiagStates.push_back(BasedOn); 6306 DiagState *NewState = &Diag.DiagStates.back(); 6307 DiagStates.push_back(NewState); 6308 unsigned Size = Record[Idx++]; 6309 assert(Idx + Size * 2 <= Record.size() && 6310 "Invalid data, not enough diag/map pairs"); 6311 while (Size--) { 6312 unsigned DiagID = Record[Idx++]; 6313 DiagnosticMapping NewMapping = 6314 DiagnosticMapping::deserialize(Record[Idx++]); 6315 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6316 continue; 6317 6318 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6319 6320 // If this mapping was specified as a warning but the severity was 6321 // upgraded due to diagnostic settings, simulate the current diagnostic 6322 // settings (and use a warning). 6323 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6324 NewMapping.setSeverity(diag::Severity::Warning); 6325 NewMapping.setUpgradedFromWarning(false); 6326 } 6327 6328 Mapping = NewMapping; 6329 } 6330 return NewState; 6331 }; 6332 6333 // Read the first state. 6334 DiagState *FirstState; 6335 if (F.Kind == MK_ImplicitModule) { 6336 // Implicitly-built modules are reused with different diagnostic 6337 // settings. Use the initial diagnostic state from Diag to simulate this 6338 // compilation's diagnostic settings. 6339 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6340 DiagStates.push_back(FirstState); 6341 6342 // Skip the initial diagnostic state from the serialized module. 6343 assert(Record[1] == 0 && 6344 "Invalid data, unexpected backref in initial state"); 6345 Idx = 3 + Record[2] * 2; 6346 assert(Idx < Record.size() && 6347 "Invalid data, not enough state change pairs in initial state"); 6348 } else if (F.isModule()) { 6349 // For an explicit module, preserve the flags from the module build 6350 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6351 // -Wblah flags. 6352 unsigned Flags = Record[Idx++]; 6353 DiagState Initial; 6354 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6355 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6356 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6357 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6358 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6359 Initial.ExtBehavior = (diag::Severity)Flags; 6360 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6361 6362 assert(F.OriginalSourceFileID.isValid()); 6363 6364 // Set up the root buffer of the module to start with the initial 6365 // diagnostic state of the module itself, to cover files that contain no 6366 // explicit transitions (for which we did not serialize anything). 6367 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6368 .StateTransitions.push_back({FirstState, 0}); 6369 } else { 6370 // For prefix ASTs, start with whatever the user configured on the 6371 // command line. 6372 Idx++; // Skip flags. 6373 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6374 SourceLocation(), false); 6375 } 6376 6377 // Read the state transitions. 6378 unsigned NumLocations = Record[Idx++]; 6379 while (NumLocations--) { 6380 assert(Idx < Record.size() && 6381 "Invalid data, missing pragma diagnostic states"); 6382 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6383 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6384 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6385 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6386 unsigned Transitions = Record[Idx++]; 6387 6388 // Note that we don't need to set up Parent/ParentOffset here, because 6389 // we won't be changing the diagnostic state within imported FileIDs 6390 // (other than perhaps appending to the main source file, which has no 6391 // parent). 6392 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6393 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6394 for (unsigned I = 0; I != Transitions; ++I) { 6395 unsigned Offset = Record[Idx++]; 6396 auto *State = 6397 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6398 F.StateTransitions.push_back({State, Offset}); 6399 } 6400 } 6401 6402 // Read the final state. 6403 assert(Idx < Record.size() && 6404 "Invalid data, missing final pragma diagnostic state"); 6405 SourceLocation CurStateLoc = 6406 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6407 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6408 6409 if (!F.isModule()) { 6410 Diag.DiagStatesByLoc.CurDiagState = CurState; 6411 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6412 6413 // Preserve the property that the imaginary root file describes the 6414 // current state. 6415 FileID NullFile; 6416 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6417 if (T.empty()) 6418 T.push_back({CurState, 0}); 6419 else 6420 T[0].State = CurState; 6421 } 6422 6423 // Don't try to read these mappings again. 6424 Record.clear(); 6425 } 6426 } 6427 6428 /// Get the correct cursor and offset for loading a type. 6429 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6430 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6431 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6432 ModuleFile *M = I->second; 6433 return RecordLocation( 6434 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6435 M->DeclsBlockStartOffset); 6436 } 6437 6438 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6439 switch (code) { 6440 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6441 case TYPE_##CODE_ID: return Type::CLASS_ID; 6442 #include "clang/Serialization/TypeBitCodes.def" 6443 default: return llvm::None; 6444 } 6445 } 6446 6447 /// Read and return the type with the given index.. 6448 /// 6449 /// The index is the type ID, shifted and minus the number of predefs. This 6450 /// routine actually reads the record corresponding to the type at the given 6451 /// location. It is a helper routine for GetType, which deals with reading type 6452 /// IDs. 6453 QualType ASTReader::readTypeRecord(unsigned Index) { 6454 assert(ContextObj && "reading type with no AST context"); 6455 ASTContext &Context = *ContextObj; 6456 RecordLocation Loc = TypeCursorForIndex(Index); 6457 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6458 6459 // Keep track of where we are in the stream, then jump back there 6460 // after reading this type. 6461 SavedStreamPosition SavedPosition(DeclsCursor); 6462 6463 ReadingKindTracker ReadingKind(Read_Type, *this); 6464 6465 // Note that we are loading a type record. 6466 Deserializing AType(this); 6467 6468 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6469 Error(std::move(Err)); 6470 return QualType(); 6471 } 6472 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6473 if (!RawCode) { 6474 Error(RawCode.takeError()); 6475 return QualType(); 6476 } 6477 6478 ASTRecordReader Record(*this, *Loc.F); 6479 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6480 if (!Code) { 6481 Error(Code.takeError()); 6482 return QualType(); 6483 } 6484 if (Code.get() == TYPE_EXT_QUAL) { 6485 QualType baseType = Record.readQualType(); 6486 Qualifiers quals = Record.readQualifiers(); 6487 return Context.getQualifiedType(baseType, quals); 6488 } 6489 6490 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6491 if (!maybeClass) { 6492 Error("Unexpected code for type"); 6493 return QualType(); 6494 } 6495 6496 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6497 return TypeReader.read(*maybeClass); 6498 } 6499 6500 namespace clang { 6501 6502 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6503 using LocSeq = SourceLocationSequence; 6504 6505 ASTRecordReader &Reader; 6506 LocSeq *Seq; 6507 6508 SourceLocation readSourceLocation() { return Reader.readSourceLocation(Seq); } 6509 SourceRange readSourceRange() { return Reader.readSourceRange(Seq); } 6510 6511 TypeSourceInfo *GetTypeSourceInfo() { 6512 return Reader.readTypeSourceInfo(); 6513 } 6514 6515 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6516 return Reader.readNestedNameSpecifierLoc(); 6517 } 6518 6519 Attr *ReadAttr() { 6520 return Reader.readAttr(); 6521 } 6522 6523 public: 6524 TypeLocReader(ASTRecordReader &Reader, LocSeq *Seq) 6525 : Reader(Reader), Seq(Seq) {} 6526 6527 // We want compile-time assurance that we've enumerated all of 6528 // these, so unfortunately we have to declare them first, then 6529 // define them out-of-line. 6530 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6531 #define TYPELOC(CLASS, PARENT) \ 6532 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6533 #include "clang/AST/TypeLocNodes.def" 6534 6535 void VisitFunctionTypeLoc(FunctionTypeLoc); 6536 void VisitArrayTypeLoc(ArrayTypeLoc); 6537 }; 6538 6539 } // namespace clang 6540 6541 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6542 // nothing to do 6543 } 6544 6545 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6546 TL.setBuiltinLoc(readSourceLocation()); 6547 if (TL.needsExtraLocalData()) { 6548 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6549 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6550 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6551 TL.setModeAttr(Reader.readInt()); 6552 } 6553 } 6554 6555 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6556 TL.setNameLoc(readSourceLocation()); 6557 } 6558 6559 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6560 TL.setStarLoc(readSourceLocation()); 6561 } 6562 6563 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6564 // nothing to do 6565 } 6566 6567 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6568 // nothing to do 6569 } 6570 6571 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6572 TL.setExpansionLoc(readSourceLocation()); 6573 } 6574 6575 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6576 TL.setCaretLoc(readSourceLocation()); 6577 } 6578 6579 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6580 TL.setAmpLoc(readSourceLocation()); 6581 } 6582 6583 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6584 TL.setAmpAmpLoc(readSourceLocation()); 6585 } 6586 6587 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6588 TL.setStarLoc(readSourceLocation()); 6589 TL.setClassTInfo(GetTypeSourceInfo()); 6590 } 6591 6592 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6593 TL.setLBracketLoc(readSourceLocation()); 6594 TL.setRBracketLoc(readSourceLocation()); 6595 if (Reader.readBool()) 6596 TL.setSizeExpr(Reader.readExpr()); 6597 else 6598 TL.setSizeExpr(nullptr); 6599 } 6600 6601 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6602 VisitArrayTypeLoc(TL); 6603 } 6604 6605 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6606 VisitArrayTypeLoc(TL); 6607 } 6608 6609 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6610 VisitArrayTypeLoc(TL); 6611 } 6612 6613 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6614 DependentSizedArrayTypeLoc TL) { 6615 VisitArrayTypeLoc(TL); 6616 } 6617 6618 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6619 DependentAddressSpaceTypeLoc TL) { 6620 6621 TL.setAttrNameLoc(readSourceLocation()); 6622 TL.setAttrOperandParensRange(readSourceRange()); 6623 TL.setAttrExprOperand(Reader.readExpr()); 6624 } 6625 6626 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6627 DependentSizedExtVectorTypeLoc TL) { 6628 TL.setNameLoc(readSourceLocation()); 6629 } 6630 6631 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6632 TL.setNameLoc(readSourceLocation()); 6633 } 6634 6635 void TypeLocReader::VisitDependentVectorTypeLoc( 6636 DependentVectorTypeLoc TL) { 6637 TL.setNameLoc(readSourceLocation()); 6638 } 6639 6640 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6641 TL.setNameLoc(readSourceLocation()); 6642 } 6643 6644 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6645 TL.setAttrNameLoc(readSourceLocation()); 6646 TL.setAttrOperandParensRange(readSourceRange()); 6647 TL.setAttrRowOperand(Reader.readExpr()); 6648 TL.setAttrColumnOperand(Reader.readExpr()); 6649 } 6650 6651 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6652 DependentSizedMatrixTypeLoc TL) { 6653 TL.setAttrNameLoc(readSourceLocation()); 6654 TL.setAttrOperandParensRange(readSourceRange()); 6655 TL.setAttrRowOperand(Reader.readExpr()); 6656 TL.setAttrColumnOperand(Reader.readExpr()); 6657 } 6658 6659 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6660 TL.setLocalRangeBegin(readSourceLocation()); 6661 TL.setLParenLoc(readSourceLocation()); 6662 TL.setRParenLoc(readSourceLocation()); 6663 TL.setExceptionSpecRange(readSourceRange()); 6664 TL.setLocalRangeEnd(readSourceLocation()); 6665 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6666 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6667 } 6668 } 6669 6670 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6671 VisitFunctionTypeLoc(TL); 6672 } 6673 6674 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6675 VisitFunctionTypeLoc(TL); 6676 } 6677 6678 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6679 TL.setNameLoc(readSourceLocation()); 6680 } 6681 6682 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) { 6683 TL.setNameLoc(readSourceLocation()); 6684 } 6685 6686 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6687 TL.setNameLoc(readSourceLocation()); 6688 } 6689 6690 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6691 TL.setTypeofLoc(readSourceLocation()); 6692 TL.setLParenLoc(readSourceLocation()); 6693 TL.setRParenLoc(readSourceLocation()); 6694 } 6695 6696 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6697 TL.setTypeofLoc(readSourceLocation()); 6698 TL.setLParenLoc(readSourceLocation()); 6699 TL.setRParenLoc(readSourceLocation()); 6700 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6701 } 6702 6703 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6704 TL.setDecltypeLoc(readSourceLocation()); 6705 TL.setRParenLoc(readSourceLocation()); 6706 } 6707 6708 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6709 TL.setKWLoc(readSourceLocation()); 6710 TL.setLParenLoc(readSourceLocation()); 6711 TL.setRParenLoc(readSourceLocation()); 6712 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6713 } 6714 6715 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6716 TL.setNameLoc(readSourceLocation()); 6717 if (Reader.readBool()) { 6718 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6719 TL.setTemplateKWLoc(readSourceLocation()); 6720 TL.setConceptNameLoc(readSourceLocation()); 6721 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6722 TL.setLAngleLoc(readSourceLocation()); 6723 TL.setRAngleLoc(readSourceLocation()); 6724 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6725 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6726 TL.getTypePtr()->getArg(i).getKind())); 6727 } 6728 if (Reader.readBool()) 6729 TL.setRParenLoc(readSourceLocation()); 6730 } 6731 6732 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6733 DeducedTemplateSpecializationTypeLoc TL) { 6734 TL.setTemplateNameLoc(readSourceLocation()); 6735 } 6736 6737 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6738 TL.setNameLoc(readSourceLocation()); 6739 } 6740 6741 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6742 TL.setNameLoc(readSourceLocation()); 6743 } 6744 6745 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6746 TL.setAttr(ReadAttr()); 6747 } 6748 6749 void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) { 6750 // Nothing to do. 6751 } 6752 6753 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6754 TL.setNameLoc(readSourceLocation()); 6755 } 6756 6757 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6758 SubstTemplateTypeParmTypeLoc TL) { 6759 TL.setNameLoc(readSourceLocation()); 6760 } 6761 6762 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6763 SubstTemplateTypeParmPackTypeLoc TL) { 6764 TL.setNameLoc(readSourceLocation()); 6765 } 6766 6767 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6768 TemplateSpecializationTypeLoc TL) { 6769 TL.setTemplateKeywordLoc(readSourceLocation()); 6770 TL.setTemplateNameLoc(readSourceLocation()); 6771 TL.setLAngleLoc(readSourceLocation()); 6772 TL.setRAngleLoc(readSourceLocation()); 6773 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6774 TL.setArgLocInfo( 6775 i, 6776 Reader.readTemplateArgumentLocInfo( 6777 TL.getTypePtr()->getArg(i).getKind())); 6778 } 6779 6780 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6781 TL.setLParenLoc(readSourceLocation()); 6782 TL.setRParenLoc(readSourceLocation()); 6783 } 6784 6785 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6786 TL.setElaboratedKeywordLoc(readSourceLocation()); 6787 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6788 } 6789 6790 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6791 TL.setNameLoc(readSourceLocation()); 6792 } 6793 6794 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6795 TL.setElaboratedKeywordLoc(readSourceLocation()); 6796 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6797 TL.setNameLoc(readSourceLocation()); 6798 } 6799 6800 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6801 DependentTemplateSpecializationTypeLoc TL) { 6802 TL.setElaboratedKeywordLoc(readSourceLocation()); 6803 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6804 TL.setTemplateKeywordLoc(readSourceLocation()); 6805 TL.setTemplateNameLoc(readSourceLocation()); 6806 TL.setLAngleLoc(readSourceLocation()); 6807 TL.setRAngleLoc(readSourceLocation()); 6808 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6809 TL.setArgLocInfo( 6810 I, 6811 Reader.readTemplateArgumentLocInfo( 6812 TL.getTypePtr()->getArg(I).getKind())); 6813 } 6814 6815 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6816 TL.setEllipsisLoc(readSourceLocation()); 6817 } 6818 6819 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6820 TL.setNameLoc(readSourceLocation()); 6821 } 6822 6823 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6824 if (TL.getNumProtocols()) { 6825 TL.setProtocolLAngleLoc(readSourceLocation()); 6826 TL.setProtocolRAngleLoc(readSourceLocation()); 6827 } 6828 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6829 TL.setProtocolLoc(i, readSourceLocation()); 6830 } 6831 6832 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6833 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6834 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6835 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6836 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6837 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6838 TL.setProtocolLAngleLoc(readSourceLocation()); 6839 TL.setProtocolRAngleLoc(readSourceLocation()); 6840 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6841 TL.setProtocolLoc(i, readSourceLocation()); 6842 } 6843 6844 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6845 TL.setStarLoc(readSourceLocation()); 6846 } 6847 6848 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6849 TL.setKWLoc(readSourceLocation()); 6850 TL.setLParenLoc(readSourceLocation()); 6851 TL.setRParenLoc(readSourceLocation()); 6852 } 6853 6854 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6855 TL.setKWLoc(readSourceLocation()); 6856 } 6857 6858 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) { 6859 TL.setNameLoc(readSourceLocation()); 6860 } 6861 void TypeLocReader::VisitDependentBitIntTypeLoc( 6862 clang::DependentBitIntTypeLoc TL) { 6863 TL.setNameLoc(readSourceLocation()); 6864 } 6865 6866 void ASTRecordReader::readTypeLoc(TypeLoc TL, LocSeq *ParentSeq) { 6867 LocSeq::State Seq(ParentSeq); 6868 TypeLocReader TLR(*this, Seq); 6869 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6870 TLR.Visit(TL); 6871 } 6872 6873 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6874 QualType InfoTy = readType(); 6875 if (InfoTy.isNull()) 6876 return nullptr; 6877 6878 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6879 readTypeLoc(TInfo->getTypeLoc()); 6880 return TInfo; 6881 } 6882 6883 QualType ASTReader::GetType(TypeID ID) { 6884 assert(ContextObj && "reading type with no AST context"); 6885 ASTContext &Context = *ContextObj; 6886 6887 unsigned FastQuals = ID & Qualifiers::FastMask; 6888 unsigned Index = ID >> Qualifiers::FastWidth; 6889 6890 if (Index < NUM_PREDEF_TYPE_IDS) { 6891 QualType T; 6892 switch ((PredefinedTypeIDs)Index) { 6893 case PREDEF_TYPE_NULL_ID: 6894 return QualType(); 6895 case PREDEF_TYPE_VOID_ID: 6896 T = Context.VoidTy; 6897 break; 6898 case PREDEF_TYPE_BOOL_ID: 6899 T = Context.BoolTy; 6900 break; 6901 case PREDEF_TYPE_CHAR_U_ID: 6902 case PREDEF_TYPE_CHAR_S_ID: 6903 // FIXME: Check that the signedness of CharTy is correct! 6904 T = Context.CharTy; 6905 break; 6906 case PREDEF_TYPE_UCHAR_ID: 6907 T = Context.UnsignedCharTy; 6908 break; 6909 case PREDEF_TYPE_USHORT_ID: 6910 T = Context.UnsignedShortTy; 6911 break; 6912 case PREDEF_TYPE_UINT_ID: 6913 T = Context.UnsignedIntTy; 6914 break; 6915 case PREDEF_TYPE_ULONG_ID: 6916 T = Context.UnsignedLongTy; 6917 break; 6918 case PREDEF_TYPE_ULONGLONG_ID: 6919 T = Context.UnsignedLongLongTy; 6920 break; 6921 case PREDEF_TYPE_UINT128_ID: 6922 T = Context.UnsignedInt128Ty; 6923 break; 6924 case PREDEF_TYPE_SCHAR_ID: 6925 T = Context.SignedCharTy; 6926 break; 6927 case PREDEF_TYPE_WCHAR_ID: 6928 T = Context.WCharTy; 6929 break; 6930 case PREDEF_TYPE_SHORT_ID: 6931 T = Context.ShortTy; 6932 break; 6933 case PREDEF_TYPE_INT_ID: 6934 T = Context.IntTy; 6935 break; 6936 case PREDEF_TYPE_LONG_ID: 6937 T = Context.LongTy; 6938 break; 6939 case PREDEF_TYPE_LONGLONG_ID: 6940 T = Context.LongLongTy; 6941 break; 6942 case PREDEF_TYPE_INT128_ID: 6943 T = Context.Int128Ty; 6944 break; 6945 case PREDEF_TYPE_BFLOAT16_ID: 6946 T = Context.BFloat16Ty; 6947 break; 6948 case PREDEF_TYPE_HALF_ID: 6949 T = Context.HalfTy; 6950 break; 6951 case PREDEF_TYPE_FLOAT_ID: 6952 T = Context.FloatTy; 6953 break; 6954 case PREDEF_TYPE_DOUBLE_ID: 6955 T = Context.DoubleTy; 6956 break; 6957 case PREDEF_TYPE_LONGDOUBLE_ID: 6958 T = Context.LongDoubleTy; 6959 break; 6960 case PREDEF_TYPE_SHORT_ACCUM_ID: 6961 T = Context.ShortAccumTy; 6962 break; 6963 case PREDEF_TYPE_ACCUM_ID: 6964 T = Context.AccumTy; 6965 break; 6966 case PREDEF_TYPE_LONG_ACCUM_ID: 6967 T = Context.LongAccumTy; 6968 break; 6969 case PREDEF_TYPE_USHORT_ACCUM_ID: 6970 T = Context.UnsignedShortAccumTy; 6971 break; 6972 case PREDEF_TYPE_UACCUM_ID: 6973 T = Context.UnsignedAccumTy; 6974 break; 6975 case PREDEF_TYPE_ULONG_ACCUM_ID: 6976 T = Context.UnsignedLongAccumTy; 6977 break; 6978 case PREDEF_TYPE_SHORT_FRACT_ID: 6979 T = Context.ShortFractTy; 6980 break; 6981 case PREDEF_TYPE_FRACT_ID: 6982 T = Context.FractTy; 6983 break; 6984 case PREDEF_TYPE_LONG_FRACT_ID: 6985 T = Context.LongFractTy; 6986 break; 6987 case PREDEF_TYPE_USHORT_FRACT_ID: 6988 T = Context.UnsignedShortFractTy; 6989 break; 6990 case PREDEF_TYPE_UFRACT_ID: 6991 T = Context.UnsignedFractTy; 6992 break; 6993 case PREDEF_TYPE_ULONG_FRACT_ID: 6994 T = Context.UnsignedLongFractTy; 6995 break; 6996 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6997 T = Context.SatShortAccumTy; 6998 break; 6999 case PREDEF_TYPE_SAT_ACCUM_ID: 7000 T = Context.SatAccumTy; 7001 break; 7002 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 7003 T = Context.SatLongAccumTy; 7004 break; 7005 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 7006 T = Context.SatUnsignedShortAccumTy; 7007 break; 7008 case PREDEF_TYPE_SAT_UACCUM_ID: 7009 T = Context.SatUnsignedAccumTy; 7010 break; 7011 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 7012 T = Context.SatUnsignedLongAccumTy; 7013 break; 7014 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 7015 T = Context.SatShortFractTy; 7016 break; 7017 case PREDEF_TYPE_SAT_FRACT_ID: 7018 T = Context.SatFractTy; 7019 break; 7020 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 7021 T = Context.SatLongFractTy; 7022 break; 7023 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 7024 T = Context.SatUnsignedShortFractTy; 7025 break; 7026 case PREDEF_TYPE_SAT_UFRACT_ID: 7027 T = Context.SatUnsignedFractTy; 7028 break; 7029 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 7030 T = Context.SatUnsignedLongFractTy; 7031 break; 7032 case PREDEF_TYPE_FLOAT16_ID: 7033 T = Context.Float16Ty; 7034 break; 7035 case PREDEF_TYPE_FLOAT128_ID: 7036 T = Context.Float128Ty; 7037 break; 7038 case PREDEF_TYPE_IBM128_ID: 7039 T = Context.Ibm128Ty; 7040 break; 7041 case PREDEF_TYPE_OVERLOAD_ID: 7042 T = Context.OverloadTy; 7043 break; 7044 case PREDEF_TYPE_BOUND_MEMBER: 7045 T = Context.BoundMemberTy; 7046 break; 7047 case PREDEF_TYPE_PSEUDO_OBJECT: 7048 T = Context.PseudoObjectTy; 7049 break; 7050 case PREDEF_TYPE_DEPENDENT_ID: 7051 T = Context.DependentTy; 7052 break; 7053 case PREDEF_TYPE_UNKNOWN_ANY: 7054 T = Context.UnknownAnyTy; 7055 break; 7056 case PREDEF_TYPE_NULLPTR_ID: 7057 T = Context.NullPtrTy; 7058 break; 7059 case PREDEF_TYPE_CHAR8_ID: 7060 T = Context.Char8Ty; 7061 break; 7062 case PREDEF_TYPE_CHAR16_ID: 7063 T = Context.Char16Ty; 7064 break; 7065 case PREDEF_TYPE_CHAR32_ID: 7066 T = Context.Char32Ty; 7067 break; 7068 case PREDEF_TYPE_OBJC_ID: 7069 T = Context.ObjCBuiltinIdTy; 7070 break; 7071 case PREDEF_TYPE_OBJC_CLASS: 7072 T = Context.ObjCBuiltinClassTy; 7073 break; 7074 case PREDEF_TYPE_OBJC_SEL: 7075 T = Context.ObjCBuiltinSelTy; 7076 break; 7077 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 7078 case PREDEF_TYPE_##Id##_ID: \ 7079 T = Context.SingletonId; \ 7080 break; 7081 #include "clang/Basic/OpenCLImageTypes.def" 7082 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 7083 case PREDEF_TYPE_##Id##_ID: \ 7084 T = Context.Id##Ty; \ 7085 break; 7086 #include "clang/Basic/OpenCLExtensionTypes.def" 7087 case PREDEF_TYPE_SAMPLER_ID: 7088 T = Context.OCLSamplerTy; 7089 break; 7090 case PREDEF_TYPE_EVENT_ID: 7091 T = Context.OCLEventTy; 7092 break; 7093 case PREDEF_TYPE_CLK_EVENT_ID: 7094 T = Context.OCLClkEventTy; 7095 break; 7096 case PREDEF_TYPE_QUEUE_ID: 7097 T = Context.OCLQueueTy; 7098 break; 7099 case PREDEF_TYPE_RESERVE_ID_ID: 7100 T = Context.OCLReserveIDTy; 7101 break; 7102 case PREDEF_TYPE_AUTO_DEDUCT: 7103 T = Context.getAutoDeductType(); 7104 break; 7105 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7106 T = Context.getAutoRRefDeductType(); 7107 break; 7108 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7109 T = Context.ARCUnbridgedCastTy; 7110 break; 7111 case PREDEF_TYPE_BUILTIN_FN: 7112 T = Context.BuiltinFnTy; 7113 break; 7114 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7115 T = Context.IncompleteMatrixIdxTy; 7116 break; 7117 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7118 T = Context.OMPArraySectionTy; 7119 break; 7120 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7121 T = Context.OMPArraySectionTy; 7122 break; 7123 case PREDEF_TYPE_OMP_ITERATOR: 7124 T = Context.OMPIteratorTy; 7125 break; 7126 #define SVE_TYPE(Name, Id, SingletonId) \ 7127 case PREDEF_TYPE_##Id##_ID: \ 7128 T = Context.SingletonId; \ 7129 break; 7130 #include "clang/Basic/AArch64SVEACLETypes.def" 7131 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 7132 case PREDEF_TYPE_##Id##_ID: \ 7133 T = Context.Id##Ty; \ 7134 break; 7135 #include "clang/Basic/PPCTypes.def" 7136 #define RVV_TYPE(Name, Id, SingletonId) \ 7137 case PREDEF_TYPE_##Id##_ID: \ 7138 T = Context.SingletonId; \ 7139 break; 7140 #include "clang/Basic/RISCVVTypes.def" 7141 } 7142 7143 assert(!T.isNull() && "Unknown predefined type"); 7144 return T.withFastQualifiers(FastQuals); 7145 } 7146 7147 Index -= NUM_PREDEF_TYPE_IDS; 7148 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7149 if (TypesLoaded[Index].isNull()) { 7150 TypesLoaded[Index] = readTypeRecord(Index); 7151 if (TypesLoaded[Index].isNull()) 7152 return QualType(); 7153 7154 TypesLoaded[Index]->setFromAST(); 7155 if (DeserializationListener) 7156 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7157 TypesLoaded[Index]); 7158 } 7159 7160 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7161 } 7162 7163 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7164 return GetType(getGlobalTypeID(F, LocalID)); 7165 } 7166 7167 serialization::TypeID 7168 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7169 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7170 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7171 7172 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7173 return LocalID; 7174 7175 if (!F.ModuleOffsetMap.empty()) 7176 ReadModuleOffsetMap(F); 7177 7178 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7179 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7180 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7181 7182 unsigned GlobalIndex = LocalIndex + I->second; 7183 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7184 } 7185 7186 TemplateArgumentLocInfo 7187 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7188 switch (Kind) { 7189 case TemplateArgument::Expression: 7190 return readExpr(); 7191 case TemplateArgument::Type: 7192 return readTypeSourceInfo(); 7193 case TemplateArgument::Template: { 7194 NestedNameSpecifierLoc QualifierLoc = 7195 readNestedNameSpecifierLoc(); 7196 SourceLocation TemplateNameLoc = readSourceLocation(); 7197 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7198 TemplateNameLoc, SourceLocation()); 7199 } 7200 case TemplateArgument::TemplateExpansion: { 7201 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7202 SourceLocation TemplateNameLoc = readSourceLocation(); 7203 SourceLocation EllipsisLoc = readSourceLocation(); 7204 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7205 TemplateNameLoc, EllipsisLoc); 7206 } 7207 case TemplateArgument::Null: 7208 case TemplateArgument::Integral: 7209 case TemplateArgument::Declaration: 7210 case TemplateArgument::NullPtr: 7211 case TemplateArgument::Pack: 7212 // FIXME: Is this right? 7213 return TemplateArgumentLocInfo(); 7214 } 7215 llvm_unreachable("unexpected template argument loc"); 7216 } 7217 7218 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7219 TemplateArgument Arg = readTemplateArgument(); 7220 7221 if (Arg.getKind() == TemplateArgument::Expression) { 7222 if (readBool()) // bool InfoHasSameExpr. 7223 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7224 } 7225 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7226 } 7227 7228 const ASTTemplateArgumentListInfo * 7229 ASTRecordReader::readASTTemplateArgumentListInfo() { 7230 SourceLocation LAngleLoc = readSourceLocation(); 7231 SourceLocation RAngleLoc = readSourceLocation(); 7232 unsigned NumArgsAsWritten = readInt(); 7233 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7234 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7235 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7236 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7237 } 7238 7239 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7240 return GetDecl(ID); 7241 } 7242 7243 void ASTReader::CompleteRedeclChain(const Decl *D) { 7244 if (NumCurrentElementsDeserializing) { 7245 // We arrange to not care about the complete redeclaration chain while we're 7246 // deserializing. Just remember that the AST has marked this one as complete 7247 // but that it's not actually complete yet, so we know we still need to 7248 // complete it later. 7249 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7250 return; 7251 } 7252 7253 if (!D->getDeclContext()) { 7254 assert(isa<TranslationUnitDecl>(D) && "Not a TU?"); 7255 return; 7256 } 7257 7258 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7259 7260 // If this is a named declaration, complete it by looking it up 7261 // within its context. 7262 // 7263 // FIXME: Merging a function definition should merge 7264 // all mergeable entities within it. 7265 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7266 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7267 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7268 if (!getContext().getLangOpts().CPlusPlus && 7269 isa<TranslationUnitDecl>(DC)) { 7270 // Outside of C++, we don't have a lookup table for the TU, so update 7271 // the identifier instead. (For C++ modules, we don't store decls 7272 // in the serialized identifier table, so we do the lookup in the TU.) 7273 auto *II = Name.getAsIdentifierInfo(); 7274 assert(II && "non-identifier name in C?"); 7275 if (II->isOutOfDate()) 7276 updateOutOfDateIdentifier(*II); 7277 } else 7278 DC->lookup(Name); 7279 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7280 // Find all declarations of this kind from the relevant context. 7281 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7282 auto *DC = cast<DeclContext>(DCDecl); 7283 SmallVector<Decl*, 8> Decls; 7284 FindExternalLexicalDecls( 7285 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7286 } 7287 } 7288 } 7289 7290 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7291 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7292 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7293 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7294 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7295 if (auto *Template = FD->getPrimaryTemplate()) 7296 Template->LoadLazySpecializations(); 7297 } 7298 } 7299 7300 CXXCtorInitializer ** 7301 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7302 RecordLocation Loc = getLocalBitOffset(Offset); 7303 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7304 SavedStreamPosition SavedPosition(Cursor); 7305 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7306 Error(std::move(Err)); 7307 return nullptr; 7308 } 7309 ReadingKindTracker ReadingKind(Read_Decl, *this); 7310 7311 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7312 if (!MaybeCode) { 7313 Error(MaybeCode.takeError()); 7314 return nullptr; 7315 } 7316 unsigned Code = MaybeCode.get(); 7317 7318 ASTRecordReader Record(*this, *Loc.F); 7319 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7320 if (!MaybeRecCode) { 7321 Error(MaybeRecCode.takeError()); 7322 return nullptr; 7323 } 7324 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7325 Error("malformed AST file: missing C++ ctor initializers"); 7326 return nullptr; 7327 } 7328 7329 return Record.readCXXCtorInitializers(); 7330 } 7331 7332 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7333 assert(ContextObj && "reading base specifiers with no AST context"); 7334 ASTContext &Context = *ContextObj; 7335 7336 RecordLocation Loc = getLocalBitOffset(Offset); 7337 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7338 SavedStreamPosition SavedPosition(Cursor); 7339 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7340 Error(std::move(Err)); 7341 return nullptr; 7342 } 7343 ReadingKindTracker ReadingKind(Read_Decl, *this); 7344 7345 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7346 if (!MaybeCode) { 7347 Error(MaybeCode.takeError()); 7348 return nullptr; 7349 } 7350 unsigned Code = MaybeCode.get(); 7351 7352 ASTRecordReader Record(*this, *Loc.F); 7353 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7354 if (!MaybeRecCode) { 7355 Error(MaybeCode.takeError()); 7356 return nullptr; 7357 } 7358 unsigned RecCode = MaybeRecCode.get(); 7359 7360 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7361 Error("malformed AST file: missing C++ base specifiers"); 7362 return nullptr; 7363 } 7364 7365 unsigned NumBases = Record.readInt(); 7366 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7367 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7368 for (unsigned I = 0; I != NumBases; ++I) 7369 Bases[I] = Record.readCXXBaseSpecifier(); 7370 return Bases; 7371 } 7372 7373 serialization::DeclID 7374 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7375 if (LocalID < NUM_PREDEF_DECL_IDS) 7376 return LocalID; 7377 7378 if (!F.ModuleOffsetMap.empty()) 7379 ReadModuleOffsetMap(F); 7380 7381 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7382 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7383 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7384 7385 return LocalID + I->second; 7386 } 7387 7388 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7389 ModuleFile &M) const { 7390 // Predefined decls aren't from any module. 7391 if (ID < NUM_PREDEF_DECL_IDS) 7392 return false; 7393 7394 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7395 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7396 } 7397 7398 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7399 if (!D->isFromASTFile()) 7400 return nullptr; 7401 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7402 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7403 return I->second; 7404 } 7405 7406 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7407 if (ID < NUM_PREDEF_DECL_IDS) 7408 return SourceLocation(); 7409 7410 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7411 7412 if (Index > DeclsLoaded.size()) { 7413 Error("declaration ID out-of-range for AST file"); 7414 return SourceLocation(); 7415 } 7416 7417 if (Decl *D = DeclsLoaded[Index]) 7418 return D->getLocation(); 7419 7420 SourceLocation Loc; 7421 DeclCursorForID(ID, Loc); 7422 return Loc; 7423 } 7424 7425 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7426 switch (ID) { 7427 case PREDEF_DECL_NULL_ID: 7428 return nullptr; 7429 7430 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7431 return Context.getTranslationUnitDecl(); 7432 7433 case PREDEF_DECL_OBJC_ID_ID: 7434 return Context.getObjCIdDecl(); 7435 7436 case PREDEF_DECL_OBJC_SEL_ID: 7437 return Context.getObjCSelDecl(); 7438 7439 case PREDEF_DECL_OBJC_CLASS_ID: 7440 return Context.getObjCClassDecl(); 7441 7442 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7443 return Context.getObjCProtocolDecl(); 7444 7445 case PREDEF_DECL_INT_128_ID: 7446 return Context.getInt128Decl(); 7447 7448 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7449 return Context.getUInt128Decl(); 7450 7451 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7452 return Context.getObjCInstanceTypeDecl(); 7453 7454 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7455 return Context.getBuiltinVaListDecl(); 7456 7457 case PREDEF_DECL_VA_LIST_TAG: 7458 return Context.getVaListTagDecl(); 7459 7460 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7461 return Context.getBuiltinMSVaListDecl(); 7462 7463 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7464 return Context.getMSGuidTagDecl(); 7465 7466 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7467 return Context.getExternCContextDecl(); 7468 7469 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7470 return Context.getMakeIntegerSeqDecl(); 7471 7472 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7473 return Context.getCFConstantStringDecl(); 7474 7475 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7476 return Context.getCFConstantStringTagDecl(); 7477 7478 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7479 return Context.getTypePackElementDecl(); 7480 } 7481 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7482 } 7483 7484 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7485 assert(ContextObj && "reading decl with no AST context"); 7486 if (ID < NUM_PREDEF_DECL_IDS) { 7487 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7488 if (D) { 7489 // Track that we have merged the declaration with ID \p ID into the 7490 // pre-existing predefined declaration \p D. 7491 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7492 if (Merged.empty()) 7493 Merged.push_back(ID); 7494 } 7495 return D; 7496 } 7497 7498 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7499 7500 if (Index >= DeclsLoaded.size()) { 7501 assert(0 && "declaration ID out-of-range for AST file"); 7502 Error("declaration ID out-of-range for AST file"); 7503 return nullptr; 7504 } 7505 7506 return DeclsLoaded[Index]; 7507 } 7508 7509 Decl *ASTReader::GetDecl(DeclID ID) { 7510 if (ID < NUM_PREDEF_DECL_IDS) 7511 return GetExistingDecl(ID); 7512 7513 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7514 7515 if (Index >= DeclsLoaded.size()) { 7516 assert(0 && "declaration ID out-of-range for AST file"); 7517 Error("declaration ID out-of-range for AST file"); 7518 return nullptr; 7519 } 7520 7521 if (!DeclsLoaded[Index]) { 7522 ReadDeclRecord(ID); 7523 if (DeserializationListener) 7524 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7525 } 7526 7527 return DeclsLoaded[Index]; 7528 } 7529 7530 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7531 DeclID GlobalID) { 7532 if (GlobalID < NUM_PREDEF_DECL_IDS) 7533 return GlobalID; 7534 7535 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7536 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7537 ModuleFile *Owner = I->second; 7538 7539 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7540 = M.GlobalToLocalDeclIDs.find(Owner); 7541 if (Pos == M.GlobalToLocalDeclIDs.end()) 7542 return 0; 7543 7544 return GlobalID - Owner->BaseDeclID + Pos->second; 7545 } 7546 7547 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7548 const RecordData &Record, 7549 unsigned &Idx) { 7550 if (Idx >= Record.size()) { 7551 Error("Corrupted AST file"); 7552 return 0; 7553 } 7554 7555 return getGlobalDeclID(F, Record[Idx++]); 7556 } 7557 7558 /// Resolve the offset of a statement into a statement. 7559 /// 7560 /// This operation will read a new statement from the external 7561 /// source each time it is called, and is meant to be used via a 7562 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7563 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7564 // Switch case IDs are per Decl. 7565 ClearSwitchCaseIDs(); 7566 7567 // Offset here is a global offset across the entire chain. 7568 RecordLocation Loc = getLocalBitOffset(Offset); 7569 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7570 Error(std::move(Err)); 7571 return nullptr; 7572 } 7573 assert(NumCurrentElementsDeserializing == 0 && 7574 "should not be called while already deserializing"); 7575 Deserializing D(this); 7576 return ReadStmtFromStream(*Loc.F); 7577 } 7578 7579 void ASTReader::FindExternalLexicalDecls( 7580 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7581 SmallVectorImpl<Decl *> &Decls) { 7582 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7583 7584 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7585 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7586 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7587 auto K = (Decl::Kind)+LexicalDecls[I]; 7588 if (!IsKindWeWant(K)) 7589 continue; 7590 7591 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7592 7593 // Don't add predefined declarations to the lexical context more 7594 // than once. 7595 if (ID < NUM_PREDEF_DECL_IDS) { 7596 if (PredefsVisited[ID]) 7597 continue; 7598 7599 PredefsVisited[ID] = true; 7600 } 7601 7602 if (Decl *D = GetLocalDecl(*M, ID)) { 7603 assert(D->getKind() == K && "wrong kind for lexical decl"); 7604 if (!DC->isDeclInLexicalTraversal(D)) 7605 Decls.push_back(D); 7606 } 7607 } 7608 }; 7609 7610 if (isa<TranslationUnitDecl>(DC)) { 7611 for (auto Lexical : TULexicalDecls) 7612 Visit(Lexical.first, Lexical.second); 7613 } else { 7614 auto I = LexicalDecls.find(DC); 7615 if (I != LexicalDecls.end()) 7616 Visit(I->second.first, I->second.second); 7617 } 7618 7619 ++NumLexicalDeclContextsRead; 7620 } 7621 7622 namespace { 7623 7624 class DeclIDComp { 7625 ASTReader &Reader; 7626 ModuleFile &Mod; 7627 7628 public: 7629 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7630 7631 bool operator()(LocalDeclID L, LocalDeclID R) const { 7632 SourceLocation LHS = getLocation(L); 7633 SourceLocation RHS = getLocation(R); 7634 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7635 } 7636 7637 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7638 SourceLocation RHS = getLocation(R); 7639 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7640 } 7641 7642 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7643 SourceLocation LHS = getLocation(L); 7644 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7645 } 7646 7647 SourceLocation getLocation(LocalDeclID ID) const { 7648 return Reader.getSourceManager().getFileLoc( 7649 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7650 } 7651 }; 7652 7653 } // namespace 7654 7655 void ASTReader::FindFileRegionDecls(FileID File, 7656 unsigned Offset, unsigned Length, 7657 SmallVectorImpl<Decl *> &Decls) { 7658 SourceManager &SM = getSourceManager(); 7659 7660 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7661 if (I == FileDeclIDs.end()) 7662 return; 7663 7664 FileDeclsInfo &DInfo = I->second; 7665 if (DInfo.Decls.empty()) 7666 return; 7667 7668 SourceLocation 7669 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7670 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7671 7672 DeclIDComp DIDComp(*this, *DInfo.Mod); 7673 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7674 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7675 if (BeginIt != DInfo.Decls.begin()) 7676 --BeginIt; 7677 7678 // If we are pointing at a top-level decl inside an objc container, we need 7679 // to backtrack until we find it otherwise we will fail to report that the 7680 // region overlaps with an objc container. 7681 while (BeginIt != DInfo.Decls.begin() && 7682 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7683 ->isTopLevelDeclInObjCContainer()) 7684 --BeginIt; 7685 7686 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7687 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7688 if (EndIt != DInfo.Decls.end()) 7689 ++EndIt; 7690 7691 for (ArrayRef<serialization::LocalDeclID>::iterator 7692 DIt = BeginIt; DIt != EndIt; ++DIt) 7693 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7694 } 7695 7696 bool 7697 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7698 DeclarationName Name) { 7699 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7700 "DeclContext has no visible decls in storage"); 7701 if (!Name) 7702 return false; 7703 7704 auto It = Lookups.find(DC); 7705 if (It == Lookups.end()) 7706 return false; 7707 7708 Deserializing LookupResults(this); 7709 7710 // Load the list of declarations. 7711 SmallVector<NamedDecl *, 64> Decls; 7712 llvm::SmallPtrSet<NamedDecl *, 8> Found; 7713 for (DeclID ID : It->second.Table.find(Name)) { 7714 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7715 if (ND->getDeclName() == Name && Found.insert(ND).second) 7716 Decls.push_back(ND); 7717 } 7718 7719 ++NumVisibleDeclContextsRead; 7720 SetExternalVisibleDeclsForName(DC, Name, Decls); 7721 return !Decls.empty(); 7722 } 7723 7724 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7725 if (!DC->hasExternalVisibleStorage()) 7726 return; 7727 7728 auto It = Lookups.find(DC); 7729 assert(It != Lookups.end() && 7730 "have external visible storage but no lookup tables"); 7731 7732 DeclsMap Decls; 7733 7734 for (DeclID ID : It->second.Table.findAll()) { 7735 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7736 Decls[ND->getDeclName()].push_back(ND); 7737 } 7738 7739 ++NumVisibleDeclContextsRead; 7740 7741 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7742 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7743 } 7744 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7745 } 7746 7747 const serialization::reader::DeclContextLookupTable * 7748 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7749 auto I = Lookups.find(Primary); 7750 return I == Lookups.end() ? nullptr : &I->second; 7751 } 7752 7753 /// Under non-PCH compilation the consumer receives the objc methods 7754 /// before receiving the implementation, and codegen depends on this. 7755 /// We simulate this by deserializing and passing to consumer the methods of the 7756 /// implementation before passing the deserialized implementation decl. 7757 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7758 ASTConsumer *Consumer) { 7759 assert(ImplD && Consumer); 7760 7761 for (auto *I : ImplD->methods()) 7762 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7763 7764 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7765 } 7766 7767 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7768 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7769 PassObjCImplDeclToConsumer(ImplD, Consumer); 7770 else 7771 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7772 } 7773 7774 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7775 this->Consumer = Consumer; 7776 7777 if (Consumer) 7778 PassInterestingDeclsToConsumer(); 7779 7780 if (DeserializationListener) 7781 DeserializationListener->ReaderInitialized(this); 7782 } 7783 7784 void ASTReader::PrintStats() { 7785 std::fprintf(stderr, "*** AST File Statistics:\n"); 7786 7787 unsigned NumTypesLoaded = 7788 TypesLoaded.size() - llvm::count(TypesLoaded, QualType()); 7789 unsigned NumDeclsLoaded = 7790 DeclsLoaded.size() - llvm::count(DeclsLoaded, (Decl *)nullptr); 7791 unsigned NumIdentifiersLoaded = 7792 IdentifiersLoaded.size() - 7793 llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr); 7794 unsigned NumMacrosLoaded = 7795 MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr); 7796 unsigned NumSelectorsLoaded = 7797 SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector()); 7798 7799 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7800 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7801 NumSLocEntriesRead, TotalNumSLocEntries, 7802 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7803 if (!TypesLoaded.empty()) 7804 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7805 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7806 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7807 if (!DeclsLoaded.empty()) 7808 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7809 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7810 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7811 if (!IdentifiersLoaded.empty()) 7812 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7813 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7814 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7815 if (!MacrosLoaded.empty()) 7816 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7817 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7818 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7819 if (!SelectorsLoaded.empty()) 7820 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7821 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7822 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7823 if (TotalNumStatements) 7824 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7825 NumStatementsRead, TotalNumStatements, 7826 ((float)NumStatementsRead/TotalNumStatements * 100)); 7827 if (TotalNumMacros) 7828 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7829 NumMacrosRead, TotalNumMacros, 7830 ((float)NumMacrosRead/TotalNumMacros * 100)); 7831 if (TotalLexicalDeclContexts) 7832 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7833 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7834 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7835 * 100)); 7836 if (TotalVisibleDeclContexts) 7837 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7838 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7839 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7840 * 100)); 7841 if (TotalNumMethodPoolEntries) 7842 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7843 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7844 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7845 * 100)); 7846 if (NumMethodPoolLookups) 7847 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7848 NumMethodPoolHits, NumMethodPoolLookups, 7849 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7850 if (NumMethodPoolTableLookups) 7851 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7852 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7853 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7854 * 100.0)); 7855 if (NumIdentifierLookupHits) 7856 std::fprintf(stderr, 7857 " %u / %u identifier table lookups succeeded (%f%%)\n", 7858 NumIdentifierLookupHits, NumIdentifierLookups, 7859 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7860 7861 if (GlobalIndex) { 7862 std::fprintf(stderr, "\n"); 7863 GlobalIndex->printStats(); 7864 } 7865 7866 std::fprintf(stderr, "\n"); 7867 dump(); 7868 std::fprintf(stderr, "\n"); 7869 } 7870 7871 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7872 LLVM_DUMP_METHOD static void 7873 dumpModuleIDMap(StringRef Name, 7874 const ContinuousRangeMap<Key, ModuleFile *, 7875 InitialCapacity> &Map) { 7876 if (Map.begin() == Map.end()) 7877 return; 7878 7879 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7880 7881 llvm::errs() << Name << ":\n"; 7882 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7883 I != IEnd; ++I) { 7884 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7885 << "\n"; 7886 } 7887 } 7888 7889 LLVM_DUMP_METHOD void ASTReader::dump() { 7890 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7891 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7892 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7893 dumpModuleIDMap("Global type map", GlobalTypeMap); 7894 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7895 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7896 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7897 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7898 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7899 dumpModuleIDMap("Global preprocessed entity map", 7900 GlobalPreprocessedEntityMap); 7901 7902 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7903 for (ModuleFile &M : ModuleMgr) 7904 M.dump(); 7905 } 7906 7907 /// Return the amount of memory used by memory buffers, breaking down 7908 /// by heap-backed versus mmap'ed memory. 7909 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7910 for (ModuleFile &I : ModuleMgr) { 7911 if (llvm::MemoryBuffer *buf = I.Buffer) { 7912 size_t bytes = buf->getBufferSize(); 7913 switch (buf->getBufferKind()) { 7914 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7915 sizes.malloc_bytes += bytes; 7916 break; 7917 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7918 sizes.mmap_bytes += bytes; 7919 break; 7920 } 7921 } 7922 } 7923 } 7924 7925 void ASTReader::InitializeSema(Sema &S) { 7926 SemaObj = &S; 7927 S.addExternalSource(this); 7928 7929 // Makes sure any declarations that were deserialized "too early" 7930 // still get added to the identifier's declaration chains. 7931 for (uint64_t ID : PreloadedDeclIDs) { 7932 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7933 pushExternalDeclIntoScope(D, D->getDeclName()); 7934 } 7935 PreloadedDeclIDs.clear(); 7936 7937 // FIXME: What happens if these are changed by a module import? 7938 if (!FPPragmaOptions.empty()) { 7939 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7940 FPOptionsOverride NewOverrides = 7941 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7942 SemaObj->CurFPFeatures = 7943 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7944 } 7945 7946 SemaObj->OpenCLFeatures = OpenCLExtensions; 7947 7948 UpdateSema(); 7949 } 7950 7951 void ASTReader::UpdateSema() { 7952 assert(SemaObj && "no Sema to update"); 7953 7954 // Load the offsets of the declarations that Sema references. 7955 // They will be lazily deserialized when needed. 7956 if (!SemaDeclRefs.empty()) { 7957 assert(SemaDeclRefs.size() % 3 == 0); 7958 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7959 if (!SemaObj->StdNamespace) 7960 SemaObj->StdNamespace = SemaDeclRefs[I]; 7961 if (!SemaObj->StdBadAlloc) 7962 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7963 if (!SemaObj->StdAlignValT) 7964 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7965 } 7966 SemaDeclRefs.clear(); 7967 } 7968 7969 // Update the state of pragmas. Use the same API as if we had encountered the 7970 // pragma in the source. 7971 if(OptimizeOffPragmaLocation.isValid()) 7972 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7973 if (PragmaMSStructState != -1) 7974 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7975 if (PointersToMembersPragmaLocation.isValid()) { 7976 SemaObj->ActOnPragmaMSPointersToMembers( 7977 (LangOptions::PragmaMSPointersToMembersKind) 7978 PragmaMSPointersToMembersState, 7979 PointersToMembersPragmaLocation); 7980 } 7981 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7982 7983 if (PragmaAlignPackCurrentValue) { 7984 // The bottom of the stack might have a default value. It must be adjusted 7985 // to the current value to ensure that the packing state is preserved after 7986 // popping entries that were included/imported from a PCH/module. 7987 bool DropFirst = false; 7988 if (!PragmaAlignPackStack.empty() && 7989 PragmaAlignPackStack.front().Location.isInvalid()) { 7990 assert(PragmaAlignPackStack.front().Value == 7991 SemaObj->AlignPackStack.DefaultValue && 7992 "Expected a default alignment value"); 7993 SemaObj->AlignPackStack.Stack.emplace_back( 7994 PragmaAlignPackStack.front().SlotLabel, 7995 SemaObj->AlignPackStack.CurrentValue, 7996 SemaObj->AlignPackStack.CurrentPragmaLocation, 7997 PragmaAlignPackStack.front().PushLocation); 7998 DropFirst = true; 7999 } 8000 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack) 8001 .drop_front(DropFirst ? 1 : 0)) { 8002 SemaObj->AlignPackStack.Stack.emplace_back( 8003 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 8004 } 8005 if (PragmaAlignPackCurrentLocation.isInvalid()) { 8006 assert(*PragmaAlignPackCurrentValue == 8007 SemaObj->AlignPackStack.DefaultValue && 8008 "Expected a default align and pack value"); 8009 // Keep the current values. 8010 } else { 8011 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue; 8012 SemaObj->AlignPackStack.CurrentPragmaLocation = 8013 PragmaAlignPackCurrentLocation; 8014 } 8015 } 8016 if (FpPragmaCurrentValue) { 8017 // The bottom of the stack might have a default value. It must be adjusted 8018 // to the current value to ensure that fp-pragma state is preserved after 8019 // popping entries that were included/imported from a PCH/module. 8020 bool DropFirst = false; 8021 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 8022 assert(FpPragmaStack.front().Value == 8023 SemaObj->FpPragmaStack.DefaultValue && 8024 "Expected a default pragma float_control value"); 8025 SemaObj->FpPragmaStack.Stack.emplace_back( 8026 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 8027 SemaObj->FpPragmaStack.CurrentPragmaLocation, 8028 FpPragmaStack.front().PushLocation); 8029 DropFirst = true; 8030 } 8031 for (const auto &Entry : 8032 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 8033 SemaObj->FpPragmaStack.Stack.emplace_back( 8034 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 8035 if (FpPragmaCurrentLocation.isInvalid()) { 8036 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 8037 "Expected a default pragma float_control value"); 8038 // Keep the current values. 8039 } else { 8040 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 8041 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 8042 } 8043 } 8044 8045 // For non-modular AST files, restore visiblity of modules. 8046 for (auto &Import : ImportedModules) { 8047 if (Import.ImportLoc.isInvalid()) 8048 continue; 8049 if (Module *Imported = getSubmodule(Import.ID)) { 8050 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 8051 } 8052 } 8053 } 8054 8055 IdentifierInfo *ASTReader::get(StringRef Name) { 8056 // Note that we are loading an identifier. 8057 Deserializing AnIdentifier(this); 8058 8059 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 8060 NumIdentifierLookups, 8061 NumIdentifierLookupHits); 8062 8063 // We don't need to do identifier table lookups in C++ modules (we preload 8064 // all interesting declarations, and don't need to use the scope for name 8065 // lookups). Perform the lookup in PCH files, though, since we don't build 8066 // a complete initial identifier table if we're carrying on from a PCH. 8067 if (PP.getLangOpts().CPlusPlus) { 8068 for (auto F : ModuleMgr.pch_modules()) 8069 if (Visitor(*F)) 8070 break; 8071 } else { 8072 // If there is a global index, look there first to determine which modules 8073 // provably do not have any results for this identifier. 8074 GlobalModuleIndex::HitSet Hits; 8075 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 8076 if (!loadGlobalIndex()) { 8077 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 8078 HitsPtr = &Hits; 8079 } 8080 } 8081 8082 ModuleMgr.visit(Visitor, HitsPtr); 8083 } 8084 8085 IdentifierInfo *II = Visitor.getIdentifierInfo(); 8086 markIdentifierUpToDate(II); 8087 return II; 8088 } 8089 8090 namespace clang { 8091 8092 /// An identifier-lookup iterator that enumerates all of the 8093 /// identifiers stored within a set of AST files. 8094 class ASTIdentifierIterator : public IdentifierIterator { 8095 /// The AST reader whose identifiers are being enumerated. 8096 const ASTReader &Reader; 8097 8098 /// The current index into the chain of AST files stored in 8099 /// the AST reader. 8100 unsigned Index; 8101 8102 /// The current position within the identifier lookup table 8103 /// of the current AST file. 8104 ASTIdentifierLookupTable::key_iterator Current; 8105 8106 /// The end position within the identifier lookup table of 8107 /// the current AST file. 8108 ASTIdentifierLookupTable::key_iterator End; 8109 8110 /// Whether to skip any modules in the ASTReader. 8111 bool SkipModules; 8112 8113 public: 8114 explicit ASTIdentifierIterator(const ASTReader &Reader, 8115 bool SkipModules = false); 8116 8117 StringRef Next() override; 8118 }; 8119 8120 } // namespace clang 8121 8122 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8123 bool SkipModules) 8124 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8125 } 8126 8127 StringRef ASTIdentifierIterator::Next() { 8128 while (Current == End) { 8129 // If we have exhausted all of our AST files, we're done. 8130 if (Index == 0) 8131 return StringRef(); 8132 8133 --Index; 8134 ModuleFile &F = Reader.ModuleMgr[Index]; 8135 if (SkipModules && F.isModule()) 8136 continue; 8137 8138 ASTIdentifierLookupTable *IdTable = 8139 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8140 Current = IdTable->key_begin(); 8141 End = IdTable->key_end(); 8142 } 8143 8144 // We have any identifiers remaining in the current AST file; return 8145 // the next one. 8146 StringRef Result = *Current; 8147 ++Current; 8148 return Result; 8149 } 8150 8151 namespace { 8152 8153 /// A utility for appending two IdentifierIterators. 8154 class ChainedIdentifierIterator : public IdentifierIterator { 8155 std::unique_ptr<IdentifierIterator> Current; 8156 std::unique_ptr<IdentifierIterator> Queued; 8157 8158 public: 8159 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8160 std::unique_ptr<IdentifierIterator> Second) 8161 : Current(std::move(First)), Queued(std::move(Second)) {} 8162 8163 StringRef Next() override { 8164 if (!Current) 8165 return StringRef(); 8166 8167 StringRef result = Current->Next(); 8168 if (!result.empty()) 8169 return result; 8170 8171 // Try the queued iterator, which may itself be empty. 8172 Current.reset(); 8173 std::swap(Current, Queued); 8174 return Next(); 8175 } 8176 }; 8177 8178 } // namespace 8179 8180 IdentifierIterator *ASTReader::getIdentifiers() { 8181 if (!loadGlobalIndex()) { 8182 std::unique_ptr<IdentifierIterator> ReaderIter( 8183 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8184 std::unique_ptr<IdentifierIterator> ModulesIter( 8185 GlobalIndex->createIdentifierIterator()); 8186 return new ChainedIdentifierIterator(std::move(ReaderIter), 8187 std::move(ModulesIter)); 8188 } 8189 8190 return new ASTIdentifierIterator(*this); 8191 } 8192 8193 namespace clang { 8194 namespace serialization { 8195 8196 class ReadMethodPoolVisitor { 8197 ASTReader &Reader; 8198 Selector Sel; 8199 unsigned PriorGeneration; 8200 unsigned InstanceBits = 0; 8201 unsigned FactoryBits = 0; 8202 bool InstanceHasMoreThanOneDecl = false; 8203 bool FactoryHasMoreThanOneDecl = false; 8204 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8205 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8206 8207 public: 8208 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8209 unsigned PriorGeneration) 8210 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8211 8212 bool operator()(ModuleFile &M) { 8213 if (!M.SelectorLookupTable) 8214 return false; 8215 8216 // If we've already searched this module file, skip it now. 8217 if (M.Generation <= PriorGeneration) 8218 return true; 8219 8220 ++Reader.NumMethodPoolTableLookups; 8221 ASTSelectorLookupTable *PoolTable 8222 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8223 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8224 if (Pos == PoolTable->end()) 8225 return false; 8226 8227 ++Reader.NumMethodPoolTableHits; 8228 ++Reader.NumSelectorsRead; 8229 // FIXME: Not quite happy with the statistics here. We probably should 8230 // disable this tracking when called via LoadSelector. 8231 // Also, should entries without methods count as misses? 8232 ++Reader.NumMethodPoolEntriesRead; 8233 ASTSelectorLookupTrait::data_type Data = *Pos; 8234 if (Reader.DeserializationListener) 8235 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8236 8237 // Append methods in the reverse order, so that later we can process them 8238 // in the order they appear in the source code by iterating through 8239 // the vector in the reverse order. 8240 InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend()); 8241 FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend()); 8242 InstanceBits = Data.InstanceBits; 8243 FactoryBits = Data.FactoryBits; 8244 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8245 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8246 return false; 8247 } 8248 8249 /// Retrieve the instance methods found by this visitor. 8250 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8251 return InstanceMethods; 8252 } 8253 8254 /// Retrieve the instance methods found by this visitor. 8255 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8256 return FactoryMethods; 8257 } 8258 8259 unsigned getInstanceBits() const { return InstanceBits; } 8260 unsigned getFactoryBits() const { return FactoryBits; } 8261 8262 bool instanceHasMoreThanOneDecl() const { 8263 return InstanceHasMoreThanOneDecl; 8264 } 8265 8266 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8267 }; 8268 8269 } // namespace serialization 8270 } // namespace clang 8271 8272 /// Add the given set of methods to the method list. 8273 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8274 ObjCMethodList &List) { 8275 for (auto I = Methods.rbegin(), E = Methods.rend(); I != E; ++I) 8276 S.addMethodToGlobalList(&List, *I); 8277 } 8278 8279 void ASTReader::ReadMethodPool(Selector Sel) { 8280 // Get the selector generation and update it to the current generation. 8281 unsigned &Generation = SelectorGeneration[Sel]; 8282 unsigned PriorGeneration = Generation; 8283 Generation = getGeneration(); 8284 SelectorOutOfDate[Sel] = false; 8285 8286 // Search for methods defined with this selector. 8287 ++NumMethodPoolLookups; 8288 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8289 ModuleMgr.visit(Visitor); 8290 8291 if (Visitor.getInstanceMethods().empty() && 8292 Visitor.getFactoryMethods().empty()) 8293 return; 8294 8295 ++NumMethodPoolHits; 8296 8297 if (!getSema()) 8298 return; 8299 8300 Sema &S = *getSema(); 8301 Sema::GlobalMethodPool::iterator Pos = 8302 S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists())) 8303 .first; 8304 8305 Pos->second.first.setBits(Visitor.getInstanceBits()); 8306 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8307 Pos->second.second.setBits(Visitor.getFactoryBits()); 8308 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8309 8310 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8311 // when building a module we keep every method individually and may need to 8312 // update hasMoreThanOneDecl as we add the methods. 8313 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8314 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8315 } 8316 8317 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8318 if (SelectorOutOfDate[Sel]) 8319 ReadMethodPool(Sel); 8320 } 8321 8322 void ASTReader::ReadKnownNamespaces( 8323 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8324 Namespaces.clear(); 8325 8326 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8327 if (NamespaceDecl *Namespace 8328 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8329 Namespaces.push_back(Namespace); 8330 } 8331 } 8332 8333 void ASTReader::ReadUndefinedButUsed( 8334 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8335 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8336 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8337 SourceLocation Loc = 8338 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8339 Undefined.insert(std::make_pair(D, Loc)); 8340 } 8341 } 8342 8343 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8344 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8345 Exprs) { 8346 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8347 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8348 uint64_t Count = DelayedDeleteExprs[Idx++]; 8349 for (uint64_t C = 0; C < Count; ++C) { 8350 SourceLocation DeleteLoc = 8351 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8352 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8353 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8354 } 8355 } 8356 } 8357 8358 void ASTReader::ReadTentativeDefinitions( 8359 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8360 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8361 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8362 if (Var) 8363 TentativeDefs.push_back(Var); 8364 } 8365 TentativeDefinitions.clear(); 8366 } 8367 8368 void ASTReader::ReadUnusedFileScopedDecls( 8369 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8370 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8371 DeclaratorDecl *D 8372 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8373 if (D) 8374 Decls.push_back(D); 8375 } 8376 UnusedFileScopedDecls.clear(); 8377 } 8378 8379 void ASTReader::ReadDelegatingConstructors( 8380 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8381 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8382 CXXConstructorDecl *D 8383 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8384 if (D) 8385 Decls.push_back(D); 8386 } 8387 DelegatingCtorDecls.clear(); 8388 } 8389 8390 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8391 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8392 TypedefNameDecl *D 8393 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8394 if (D) 8395 Decls.push_back(D); 8396 } 8397 ExtVectorDecls.clear(); 8398 } 8399 8400 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8401 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8402 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8403 ++I) { 8404 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8405 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8406 if (D) 8407 Decls.insert(D); 8408 } 8409 UnusedLocalTypedefNameCandidates.clear(); 8410 } 8411 8412 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8413 llvm::SmallSetVector<Decl *, 4> &Decls) { 8414 for (auto I : DeclsToCheckForDeferredDiags) { 8415 auto *D = dyn_cast_or_null<Decl>(GetDecl(I)); 8416 if (D) 8417 Decls.insert(D); 8418 } 8419 DeclsToCheckForDeferredDiags.clear(); 8420 } 8421 8422 void ASTReader::ReadReferencedSelectors( 8423 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8424 if (ReferencedSelectorsData.empty()) 8425 return; 8426 8427 // If there are @selector references added them to its pool. This is for 8428 // implementation of -Wselector. 8429 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8430 unsigned I = 0; 8431 while (I < DataSize) { 8432 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8433 SourceLocation SelLoc 8434 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8435 Sels.push_back(std::make_pair(Sel, SelLoc)); 8436 } 8437 ReferencedSelectorsData.clear(); 8438 } 8439 8440 void ASTReader::ReadWeakUndeclaredIdentifiers( 8441 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8442 if (WeakUndeclaredIdentifiers.empty()) 8443 return; 8444 8445 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8446 IdentifierInfo *WeakId 8447 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8448 IdentifierInfo *AliasId 8449 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8450 SourceLocation Loc = 8451 SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8452 WeakInfo WI(AliasId, Loc); 8453 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8454 } 8455 WeakUndeclaredIdentifiers.clear(); 8456 } 8457 8458 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8459 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8460 ExternalVTableUse VT; 8461 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8462 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8463 VT.DefinitionRequired = VTableUses[Idx++]; 8464 VTables.push_back(VT); 8465 } 8466 8467 VTableUses.clear(); 8468 } 8469 8470 void ASTReader::ReadPendingInstantiations( 8471 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8472 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8473 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8474 SourceLocation Loc 8475 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8476 8477 Pending.push_back(std::make_pair(D, Loc)); 8478 } 8479 PendingInstantiations.clear(); 8480 } 8481 8482 void ASTReader::ReadLateParsedTemplates( 8483 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8484 &LPTMap) { 8485 for (auto &LPT : LateParsedTemplates) { 8486 ModuleFile *FMod = LPT.first; 8487 RecordDataImpl &LateParsed = LPT.second; 8488 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8489 /* In loop */) { 8490 FunctionDecl *FD = 8491 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8492 8493 auto LT = std::make_unique<LateParsedTemplate>(); 8494 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8495 8496 ModuleFile *F = getOwningModuleFile(LT->D); 8497 assert(F && "No module"); 8498 8499 unsigned TokN = LateParsed[Idx++]; 8500 LT->Toks.reserve(TokN); 8501 for (unsigned T = 0; T < TokN; ++T) 8502 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8503 8504 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8505 } 8506 } 8507 8508 LateParsedTemplates.clear(); 8509 } 8510 8511 void ASTReader::LoadSelector(Selector Sel) { 8512 // It would be complicated to avoid reading the methods anyway. So don't. 8513 ReadMethodPool(Sel); 8514 } 8515 8516 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8517 assert(ID && "Non-zero identifier ID required"); 8518 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8519 IdentifiersLoaded[ID - 1] = II; 8520 if (DeserializationListener) 8521 DeserializationListener->IdentifierRead(ID, II); 8522 } 8523 8524 /// Set the globally-visible declarations associated with the given 8525 /// identifier. 8526 /// 8527 /// If the AST reader is currently in a state where the given declaration IDs 8528 /// cannot safely be resolved, they are queued until it is safe to resolve 8529 /// them. 8530 /// 8531 /// \param II an IdentifierInfo that refers to one or more globally-visible 8532 /// declarations. 8533 /// 8534 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8535 /// visible at global scope. 8536 /// 8537 /// \param Decls if non-null, this vector will be populated with the set of 8538 /// deserialized declarations. These declarations will not be pushed into 8539 /// scope. 8540 void 8541 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8542 const SmallVectorImpl<uint32_t> &DeclIDs, 8543 SmallVectorImpl<Decl *> *Decls) { 8544 if (NumCurrentElementsDeserializing && !Decls) { 8545 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8546 return; 8547 } 8548 8549 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8550 if (!SemaObj) { 8551 // Queue this declaration so that it will be added to the 8552 // translation unit scope and identifier's declaration chain 8553 // once a Sema object is known. 8554 PreloadedDeclIDs.push_back(DeclIDs[I]); 8555 continue; 8556 } 8557 8558 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8559 8560 // If we're simply supposed to record the declarations, do so now. 8561 if (Decls) { 8562 Decls->push_back(D); 8563 continue; 8564 } 8565 8566 // Introduce this declaration into the translation-unit scope 8567 // and add it to the declaration chain for this identifier, so 8568 // that (unqualified) name lookup will find it. 8569 pushExternalDeclIntoScope(D, II); 8570 } 8571 } 8572 8573 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8574 if (ID == 0) 8575 return nullptr; 8576 8577 if (IdentifiersLoaded.empty()) { 8578 Error("no identifier table in AST file"); 8579 return nullptr; 8580 } 8581 8582 ID -= 1; 8583 if (!IdentifiersLoaded[ID]) { 8584 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8585 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8586 ModuleFile *M = I->second; 8587 unsigned Index = ID - M->BaseIdentifierID; 8588 const unsigned char *Data = 8589 M->IdentifierTableData + M->IdentifierOffsets[Index]; 8590 8591 ASTIdentifierLookupTrait Trait(*this, *M); 8592 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 8593 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 8594 auto &II = PP.getIdentifierTable().get(Key); 8595 IdentifiersLoaded[ID] = &II; 8596 markIdentifierFromAST(*this, II); 8597 if (DeserializationListener) 8598 DeserializationListener->IdentifierRead(ID + 1, &II); 8599 } 8600 8601 return IdentifiersLoaded[ID]; 8602 } 8603 8604 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8605 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8606 } 8607 8608 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8609 if (LocalID < NUM_PREDEF_IDENT_IDS) 8610 return LocalID; 8611 8612 if (!M.ModuleOffsetMap.empty()) 8613 ReadModuleOffsetMap(M); 8614 8615 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8616 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8617 assert(I != M.IdentifierRemap.end() 8618 && "Invalid index into identifier index remap"); 8619 8620 return LocalID + I->second; 8621 } 8622 8623 MacroInfo *ASTReader::getMacro(MacroID ID) { 8624 if (ID == 0) 8625 return nullptr; 8626 8627 if (MacrosLoaded.empty()) { 8628 Error("no macro table in AST file"); 8629 return nullptr; 8630 } 8631 8632 ID -= NUM_PREDEF_MACRO_IDS; 8633 if (!MacrosLoaded[ID]) { 8634 GlobalMacroMapType::iterator I 8635 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8636 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8637 ModuleFile *M = I->second; 8638 unsigned Index = ID - M->BaseMacroID; 8639 MacrosLoaded[ID] = 8640 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8641 8642 if (DeserializationListener) 8643 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8644 MacrosLoaded[ID]); 8645 } 8646 8647 return MacrosLoaded[ID]; 8648 } 8649 8650 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8651 if (LocalID < NUM_PREDEF_MACRO_IDS) 8652 return LocalID; 8653 8654 if (!M.ModuleOffsetMap.empty()) 8655 ReadModuleOffsetMap(M); 8656 8657 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8658 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8659 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8660 8661 return LocalID + I->second; 8662 } 8663 8664 serialization::SubmoduleID 8665 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8666 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8667 return LocalID; 8668 8669 if (!M.ModuleOffsetMap.empty()) 8670 ReadModuleOffsetMap(M); 8671 8672 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8673 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8674 assert(I != M.SubmoduleRemap.end() 8675 && "Invalid index into submodule index remap"); 8676 8677 return LocalID + I->second; 8678 } 8679 8680 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8681 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8682 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8683 return nullptr; 8684 } 8685 8686 if (GlobalID > SubmodulesLoaded.size()) { 8687 Error("submodule ID out of range in AST file"); 8688 return nullptr; 8689 } 8690 8691 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8692 } 8693 8694 Module *ASTReader::getModule(unsigned ID) { 8695 return getSubmodule(ID); 8696 } 8697 8698 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8699 if (ID & 1) { 8700 // It's a module, look it up by submodule ID. 8701 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8702 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8703 } else { 8704 // It's a prefix (preamble, PCH, ...). Look it up by index. 8705 unsigned IndexFromEnd = ID >> 1; 8706 assert(IndexFromEnd && "got reference to unknown module file"); 8707 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8708 } 8709 } 8710 8711 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8712 if (!F) 8713 return 1; 8714 8715 // For a file representing a module, use the submodule ID of the top-level 8716 // module as the file ID. For any other kind of file, the number of such 8717 // files loaded beforehand will be the same on reload. 8718 // FIXME: Is this true even if we have an explicit module file and a PCH? 8719 if (F->isModule()) 8720 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8721 8722 auto PCHModules = getModuleManager().pch_modules(); 8723 auto I = llvm::find(PCHModules, F); 8724 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8725 return (I - PCHModules.end()) << 1; 8726 } 8727 8728 llvm::Optional<ASTSourceDescriptor> 8729 ASTReader::getSourceDescriptor(unsigned ID) { 8730 if (Module *M = getSubmodule(ID)) 8731 return ASTSourceDescriptor(*M); 8732 8733 // If there is only a single PCH, return it instead. 8734 // Chained PCH are not supported. 8735 const auto &PCHChain = ModuleMgr.pch_modules(); 8736 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8737 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8738 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8739 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8740 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8741 MF.Signature); 8742 } 8743 return None; 8744 } 8745 8746 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8747 auto I = DefinitionSource.find(FD); 8748 if (I == DefinitionSource.end()) 8749 return EK_ReplyHazy; 8750 return I->second ? EK_Never : EK_Always; 8751 } 8752 8753 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8754 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8755 } 8756 8757 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8758 if (ID == 0) 8759 return Selector(); 8760 8761 if (ID > SelectorsLoaded.size()) { 8762 Error("selector ID out of range in AST file"); 8763 return Selector(); 8764 } 8765 8766 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8767 // Load this selector from the selector table. 8768 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8769 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8770 ModuleFile &M = *I->second; 8771 ASTSelectorLookupTrait Trait(*this, M); 8772 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8773 SelectorsLoaded[ID - 1] = 8774 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8775 if (DeserializationListener) 8776 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8777 } 8778 8779 return SelectorsLoaded[ID - 1]; 8780 } 8781 8782 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8783 return DecodeSelector(ID); 8784 } 8785 8786 uint32_t ASTReader::GetNumExternalSelectors() { 8787 // ID 0 (the null selector) is considered an external selector. 8788 return getTotalNumSelectors() + 1; 8789 } 8790 8791 serialization::SelectorID 8792 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8793 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8794 return LocalID; 8795 8796 if (!M.ModuleOffsetMap.empty()) 8797 ReadModuleOffsetMap(M); 8798 8799 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8800 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8801 assert(I != M.SelectorRemap.end() 8802 && "Invalid index into selector index remap"); 8803 8804 return LocalID + I->second; 8805 } 8806 8807 DeclarationNameLoc 8808 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8809 switch (Name.getNameKind()) { 8810 case DeclarationName::CXXConstructorName: 8811 case DeclarationName::CXXDestructorName: 8812 case DeclarationName::CXXConversionFunctionName: 8813 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); 8814 8815 case DeclarationName::CXXOperatorName: 8816 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); 8817 8818 case DeclarationName::CXXLiteralOperatorName: 8819 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( 8820 readSourceLocation()); 8821 8822 case DeclarationName::Identifier: 8823 case DeclarationName::ObjCZeroArgSelector: 8824 case DeclarationName::ObjCOneArgSelector: 8825 case DeclarationName::ObjCMultiArgSelector: 8826 case DeclarationName::CXXUsingDirective: 8827 case DeclarationName::CXXDeductionGuideName: 8828 break; 8829 } 8830 return DeclarationNameLoc(); 8831 } 8832 8833 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8834 DeclarationNameInfo NameInfo; 8835 NameInfo.setName(readDeclarationName()); 8836 NameInfo.setLoc(readSourceLocation()); 8837 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8838 return NameInfo; 8839 } 8840 8841 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8842 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8843 unsigned NumTPLists = readInt(); 8844 Info.NumTemplParamLists = NumTPLists; 8845 if (NumTPLists) { 8846 Info.TemplParamLists = 8847 new (getContext()) TemplateParameterList *[NumTPLists]; 8848 for (unsigned i = 0; i != NumTPLists; ++i) 8849 Info.TemplParamLists[i] = readTemplateParameterList(); 8850 } 8851 } 8852 8853 TemplateParameterList * 8854 ASTRecordReader::readTemplateParameterList() { 8855 SourceLocation TemplateLoc = readSourceLocation(); 8856 SourceLocation LAngleLoc = readSourceLocation(); 8857 SourceLocation RAngleLoc = readSourceLocation(); 8858 8859 unsigned NumParams = readInt(); 8860 SmallVector<NamedDecl *, 16> Params; 8861 Params.reserve(NumParams); 8862 while (NumParams--) 8863 Params.push_back(readDeclAs<NamedDecl>()); 8864 8865 bool HasRequiresClause = readBool(); 8866 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8867 8868 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8869 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8870 return TemplateParams; 8871 } 8872 8873 void ASTRecordReader::readTemplateArgumentList( 8874 SmallVectorImpl<TemplateArgument> &TemplArgs, 8875 bool Canonicalize) { 8876 unsigned NumTemplateArgs = readInt(); 8877 TemplArgs.reserve(NumTemplateArgs); 8878 while (NumTemplateArgs--) 8879 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8880 } 8881 8882 /// Read a UnresolvedSet structure. 8883 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8884 unsigned NumDecls = readInt(); 8885 Set.reserve(getContext(), NumDecls); 8886 while (NumDecls--) { 8887 DeclID ID = readDeclID(); 8888 AccessSpecifier AS = (AccessSpecifier) readInt(); 8889 Set.addLazyDecl(getContext(), ID, AS); 8890 } 8891 } 8892 8893 CXXBaseSpecifier 8894 ASTRecordReader::readCXXBaseSpecifier() { 8895 bool isVirtual = readBool(); 8896 bool isBaseOfClass = readBool(); 8897 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8898 bool inheritConstructors = readBool(); 8899 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8900 SourceRange Range = readSourceRange(); 8901 SourceLocation EllipsisLoc = readSourceLocation(); 8902 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8903 EllipsisLoc); 8904 Result.setInheritConstructors(inheritConstructors); 8905 return Result; 8906 } 8907 8908 CXXCtorInitializer ** 8909 ASTRecordReader::readCXXCtorInitializers() { 8910 ASTContext &Context = getContext(); 8911 unsigned NumInitializers = readInt(); 8912 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8913 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8914 for (unsigned i = 0; i != NumInitializers; ++i) { 8915 TypeSourceInfo *TInfo = nullptr; 8916 bool IsBaseVirtual = false; 8917 FieldDecl *Member = nullptr; 8918 IndirectFieldDecl *IndirectMember = nullptr; 8919 8920 CtorInitializerType Type = (CtorInitializerType) readInt(); 8921 switch (Type) { 8922 case CTOR_INITIALIZER_BASE: 8923 TInfo = readTypeSourceInfo(); 8924 IsBaseVirtual = readBool(); 8925 break; 8926 8927 case CTOR_INITIALIZER_DELEGATING: 8928 TInfo = readTypeSourceInfo(); 8929 break; 8930 8931 case CTOR_INITIALIZER_MEMBER: 8932 Member = readDeclAs<FieldDecl>(); 8933 break; 8934 8935 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8936 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8937 break; 8938 } 8939 8940 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8941 Expr *Init = readExpr(); 8942 SourceLocation LParenLoc = readSourceLocation(); 8943 SourceLocation RParenLoc = readSourceLocation(); 8944 8945 CXXCtorInitializer *BOMInit; 8946 if (Type == CTOR_INITIALIZER_BASE) 8947 BOMInit = new (Context) 8948 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8949 RParenLoc, MemberOrEllipsisLoc); 8950 else if (Type == CTOR_INITIALIZER_DELEGATING) 8951 BOMInit = new (Context) 8952 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8953 else if (Member) 8954 BOMInit = new (Context) 8955 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8956 Init, RParenLoc); 8957 else 8958 BOMInit = new (Context) 8959 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8960 LParenLoc, Init, RParenLoc); 8961 8962 if (/*IsWritten*/readBool()) { 8963 unsigned SourceOrder = readInt(); 8964 BOMInit->setSourceOrder(SourceOrder); 8965 } 8966 8967 CtorInitializers[i] = BOMInit; 8968 } 8969 8970 return CtorInitializers; 8971 } 8972 8973 NestedNameSpecifierLoc 8974 ASTRecordReader::readNestedNameSpecifierLoc() { 8975 ASTContext &Context = getContext(); 8976 unsigned N = readInt(); 8977 NestedNameSpecifierLocBuilder Builder; 8978 for (unsigned I = 0; I != N; ++I) { 8979 auto Kind = readNestedNameSpecifierKind(); 8980 switch (Kind) { 8981 case NestedNameSpecifier::Identifier: { 8982 IdentifierInfo *II = readIdentifier(); 8983 SourceRange Range = readSourceRange(); 8984 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8985 break; 8986 } 8987 8988 case NestedNameSpecifier::Namespace: { 8989 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8990 SourceRange Range = readSourceRange(); 8991 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8992 break; 8993 } 8994 8995 case NestedNameSpecifier::NamespaceAlias: { 8996 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8997 SourceRange Range = readSourceRange(); 8998 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8999 break; 9000 } 9001 9002 case NestedNameSpecifier::TypeSpec: 9003 case NestedNameSpecifier::TypeSpecWithTemplate: { 9004 bool Template = readBool(); 9005 TypeSourceInfo *T = readTypeSourceInfo(); 9006 if (!T) 9007 return NestedNameSpecifierLoc(); 9008 SourceLocation ColonColonLoc = readSourceLocation(); 9009 9010 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 9011 Builder.Extend(Context, 9012 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 9013 T->getTypeLoc(), ColonColonLoc); 9014 break; 9015 } 9016 9017 case NestedNameSpecifier::Global: { 9018 SourceLocation ColonColonLoc = readSourceLocation(); 9019 Builder.MakeGlobal(Context, ColonColonLoc); 9020 break; 9021 } 9022 9023 case NestedNameSpecifier::Super: { 9024 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 9025 SourceRange Range = readSourceRange(); 9026 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 9027 break; 9028 } 9029 } 9030 } 9031 9032 return Builder.getWithLocInContext(Context); 9033 } 9034 9035 SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 9036 unsigned &Idx, LocSeq *Seq) { 9037 SourceLocation beg = ReadSourceLocation(F, Record, Idx, Seq); 9038 SourceLocation end = ReadSourceLocation(F, Record, Idx, Seq); 9039 return SourceRange(beg, end); 9040 } 9041 9042 /// Read a floating-point value 9043 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 9044 return llvm::APFloat(Sem, readAPInt()); 9045 } 9046 9047 // Read a string 9048 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 9049 unsigned Len = Record[Idx++]; 9050 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9051 Idx += Len; 9052 return Result; 9053 } 9054 9055 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9056 unsigned &Idx) { 9057 std::string Filename = ReadString(Record, Idx); 9058 ResolveImportedPath(F, Filename); 9059 return Filename; 9060 } 9061 9062 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9063 const RecordData &Record, unsigned &Idx) { 9064 std::string Filename = ReadString(Record, Idx); 9065 if (!BaseDirectory.empty()) 9066 ResolveImportedPath(Filename, BaseDirectory); 9067 return Filename; 9068 } 9069 9070 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9071 unsigned &Idx) { 9072 unsigned Major = Record[Idx++]; 9073 unsigned Minor = Record[Idx++]; 9074 unsigned Subminor = Record[Idx++]; 9075 if (Minor == 0) 9076 return VersionTuple(Major); 9077 if (Subminor == 0) 9078 return VersionTuple(Major, Minor - 1); 9079 return VersionTuple(Major, Minor - 1, Subminor - 1); 9080 } 9081 9082 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9083 const RecordData &Record, 9084 unsigned &Idx) { 9085 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9086 return CXXTemporary::Create(getContext(), Decl); 9087 } 9088 9089 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9090 return Diag(CurrentImportLoc, DiagID); 9091 } 9092 9093 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9094 return Diags.Report(Loc, DiagID); 9095 } 9096 9097 /// Retrieve the identifier table associated with the 9098 /// preprocessor. 9099 IdentifierTable &ASTReader::getIdentifierTable() { 9100 return PP.getIdentifierTable(); 9101 } 9102 9103 /// Record that the given ID maps to the given switch-case 9104 /// statement. 9105 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9106 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9107 "Already have a SwitchCase with this ID"); 9108 (*CurrSwitchCaseStmts)[ID] = SC; 9109 } 9110 9111 /// Retrieve the switch-case statement with the given ID. 9112 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9113 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9114 return (*CurrSwitchCaseStmts)[ID]; 9115 } 9116 9117 void ASTReader::ClearSwitchCaseIDs() { 9118 CurrSwitchCaseStmts->clear(); 9119 } 9120 9121 void ASTReader::ReadComments() { 9122 ASTContext &Context = getContext(); 9123 std::vector<RawComment *> Comments; 9124 for (SmallVectorImpl<std::pair<BitstreamCursor, 9125 serialization::ModuleFile *>>::iterator 9126 I = CommentsCursors.begin(), 9127 E = CommentsCursors.end(); 9128 I != E; ++I) { 9129 Comments.clear(); 9130 BitstreamCursor &Cursor = I->first; 9131 serialization::ModuleFile &F = *I->second; 9132 SavedStreamPosition SavedPosition(Cursor); 9133 9134 RecordData Record; 9135 while (true) { 9136 Expected<llvm::BitstreamEntry> MaybeEntry = 9137 Cursor.advanceSkippingSubblocks( 9138 BitstreamCursor::AF_DontPopBlockAtEnd); 9139 if (!MaybeEntry) { 9140 Error(MaybeEntry.takeError()); 9141 return; 9142 } 9143 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9144 9145 switch (Entry.Kind) { 9146 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9147 case llvm::BitstreamEntry::Error: 9148 Error("malformed block record in AST file"); 9149 return; 9150 case llvm::BitstreamEntry::EndBlock: 9151 goto NextCursor; 9152 case llvm::BitstreamEntry::Record: 9153 // The interesting case. 9154 break; 9155 } 9156 9157 // Read a record. 9158 Record.clear(); 9159 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9160 if (!MaybeComment) { 9161 Error(MaybeComment.takeError()); 9162 return; 9163 } 9164 switch ((CommentRecordTypes)MaybeComment.get()) { 9165 case COMMENTS_RAW_COMMENT: { 9166 unsigned Idx = 0; 9167 SourceRange SR = ReadSourceRange(F, Record, Idx); 9168 RawComment::CommentKind Kind = 9169 (RawComment::CommentKind) Record[Idx++]; 9170 bool IsTrailingComment = Record[Idx++]; 9171 bool IsAlmostTrailingComment = Record[Idx++]; 9172 Comments.push_back(new (Context) RawComment( 9173 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9174 break; 9175 } 9176 } 9177 } 9178 NextCursor: 9179 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9180 FileToOffsetToComment; 9181 for (RawComment *C : Comments) { 9182 SourceLocation CommentLoc = C->getBeginLoc(); 9183 if (CommentLoc.isValid()) { 9184 std::pair<FileID, unsigned> Loc = 9185 SourceMgr.getDecomposedLoc(CommentLoc); 9186 if (Loc.first.isValid()) 9187 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9188 } 9189 } 9190 } 9191 } 9192 9193 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9194 bool IncludeSystem, bool Complain, 9195 llvm::function_ref<void(const serialization::InputFile &IF, 9196 bool isSystem)> Visitor) { 9197 unsigned NumUserInputs = MF.NumUserInputFiles; 9198 unsigned NumInputs = MF.InputFilesLoaded.size(); 9199 assert(NumUserInputs <= NumInputs); 9200 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9201 for (unsigned I = 0; I < N; ++I) { 9202 bool IsSystem = I >= NumUserInputs; 9203 InputFile IF = getInputFile(MF, I+1, Complain); 9204 Visitor(IF, IsSystem); 9205 } 9206 } 9207 9208 void ASTReader::visitTopLevelModuleMaps( 9209 serialization::ModuleFile &MF, 9210 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9211 unsigned NumInputs = MF.InputFilesLoaded.size(); 9212 for (unsigned I = 0; I < NumInputs; ++I) { 9213 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9214 if (IFI.TopLevelModuleMap) 9215 // FIXME: This unnecessarily re-reads the InputFileInfo. 9216 if (auto FE = getInputFile(MF, I + 1).getFile()) 9217 Visitor(FE); 9218 } 9219 } 9220 9221 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9222 // If we know the owning module, use it. 9223 if (Module *M = D->getImportedOwningModule()) 9224 return M->getFullModuleName(); 9225 9226 // Otherwise, use the name of the top-level module the decl is within. 9227 if (ModuleFile *M = getOwningModuleFile(D)) 9228 return M->ModuleName; 9229 9230 // Not from a module. 9231 return {}; 9232 } 9233 9234 void ASTReader::finishPendingActions() { 9235 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9236 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9237 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9238 !PendingUpdateRecords.empty() || 9239 !PendingObjCExtensionIvarRedeclarations.empty()) { 9240 // If any identifiers with corresponding top-level declarations have 9241 // been loaded, load those declarations now. 9242 using TopLevelDeclsMap = 9243 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9244 TopLevelDeclsMap TopLevelDecls; 9245 9246 while (!PendingIdentifierInfos.empty()) { 9247 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9248 SmallVector<uint32_t, 4> DeclIDs = 9249 std::move(PendingIdentifierInfos.back().second); 9250 PendingIdentifierInfos.pop_back(); 9251 9252 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9253 } 9254 9255 // Load each function type that we deferred loading because it was a 9256 // deduced type that might refer to a local type declared within itself. 9257 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9258 auto *FD = PendingFunctionTypes[I].first; 9259 FD->setType(GetType(PendingFunctionTypes[I].second)); 9260 9261 // If we gave a function a deduced return type, remember that we need to 9262 // propagate that along the redeclaration chain. 9263 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9264 if (DT && DT->isDeduced()) 9265 PendingDeducedTypeUpdates.insert( 9266 {FD->getCanonicalDecl(), FD->getReturnType()}); 9267 } 9268 PendingFunctionTypes.clear(); 9269 9270 // For each decl chain that we wanted to complete while deserializing, mark 9271 // it as "still needs to be completed". 9272 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9273 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9274 } 9275 PendingIncompleteDeclChains.clear(); 9276 9277 // Load pending declaration chains. 9278 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9279 loadPendingDeclChain(PendingDeclChains[I].first, 9280 PendingDeclChains[I].second); 9281 PendingDeclChains.clear(); 9282 9283 // Make the most recent of the top-level declarations visible. 9284 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9285 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9286 IdentifierInfo *II = TLD->first; 9287 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9288 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9289 } 9290 } 9291 9292 // Load any pending macro definitions. 9293 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9294 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9295 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9296 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9297 // Initialize the macro history from chained-PCHs ahead of module imports. 9298 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9299 ++IDIdx) { 9300 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9301 if (!Info.M->isModule()) 9302 resolvePendingMacro(II, Info); 9303 } 9304 // Handle module imports. 9305 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9306 ++IDIdx) { 9307 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9308 if (Info.M->isModule()) 9309 resolvePendingMacro(II, Info); 9310 } 9311 } 9312 PendingMacroIDs.clear(); 9313 9314 // Wire up the DeclContexts for Decls that we delayed setting until 9315 // recursive loading is completed. 9316 while (!PendingDeclContextInfos.empty()) { 9317 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9318 PendingDeclContextInfos.pop_front(); 9319 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9320 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9321 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9322 } 9323 9324 // Perform any pending declaration updates. 9325 while (!PendingUpdateRecords.empty()) { 9326 auto Update = PendingUpdateRecords.pop_back_val(); 9327 ReadingKindTracker ReadingKind(Read_Decl, *this); 9328 loadDeclUpdateRecords(Update); 9329 } 9330 9331 while (!PendingObjCExtensionIvarRedeclarations.empty()) { 9332 auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first; 9333 auto DuplicateIvars = 9334 PendingObjCExtensionIvarRedeclarations.back().second; 9335 llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls; 9336 StructuralEquivalenceContext Ctx( 9337 ExtensionsPair.first->getASTContext(), 9338 ExtensionsPair.second->getASTContext(), NonEquivalentDecls, 9339 StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false, 9340 /*Complain =*/false, 9341 /*ErrorOnTagTypeMismatch =*/true); 9342 if (Ctx.IsEquivalent(ExtensionsPair.first, ExtensionsPair.second)) { 9343 // Merge redeclared ivars with their predecessors. 9344 for (auto IvarPair : DuplicateIvars) { 9345 ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second; 9346 // Change semantic DeclContext but keep the lexical one. 9347 Ivar->setDeclContextsImpl(PrevIvar->getDeclContext(), 9348 Ivar->getLexicalDeclContext(), 9349 getContext()); 9350 getContext().setPrimaryMergedDecl(Ivar, PrevIvar->getCanonicalDecl()); 9351 } 9352 // Invalidate duplicate extension and the cached ivar list. 9353 ExtensionsPair.first->setInvalidDecl(); 9354 ExtensionsPair.second->getClassInterface() 9355 ->getDefinition() 9356 ->setIvarList(nullptr); 9357 } else { 9358 for (auto IvarPair : DuplicateIvars) { 9359 Diag(IvarPair.first->getLocation(), 9360 diag::err_duplicate_ivar_declaration) 9361 << IvarPair.first->getIdentifier(); 9362 Diag(IvarPair.second->getLocation(), diag::note_previous_definition); 9363 } 9364 } 9365 PendingObjCExtensionIvarRedeclarations.pop_back(); 9366 } 9367 } 9368 9369 // At this point, all update records for loaded decls are in place, so any 9370 // fake class definitions should have become real. 9371 assert(PendingFakeDefinitionData.empty() && 9372 "faked up a class definition but never saw the real one"); 9373 9374 // If we deserialized any C++ or Objective-C class definitions, any 9375 // Objective-C protocol definitions, or any redeclarable templates, make sure 9376 // that all redeclarations point to the definitions. Note that this can only 9377 // happen now, after the redeclaration chains have been fully wired. 9378 for (Decl *D : PendingDefinitions) { 9379 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9380 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9381 // Make sure that the TagType points at the definition. 9382 const_cast<TagType*>(TagT)->decl = TD; 9383 } 9384 9385 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9386 for (auto *R = getMostRecentExistingDecl(RD); R; 9387 R = R->getPreviousDecl()) { 9388 assert((R == D) == 9389 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9390 "declaration thinks it's the definition but it isn't"); 9391 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9392 } 9393 } 9394 9395 continue; 9396 } 9397 9398 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9399 // Make sure that the ObjCInterfaceType points at the definition. 9400 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9401 ->Decl = ID; 9402 9403 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9404 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9405 9406 continue; 9407 } 9408 9409 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9410 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9411 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9412 9413 continue; 9414 } 9415 9416 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9417 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9418 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9419 } 9420 PendingDefinitions.clear(); 9421 9422 // Load the bodies of any functions or methods we've encountered. We do 9423 // this now (delayed) so that we can be sure that the declaration chains 9424 // have been fully wired up (hasBody relies on this). 9425 // FIXME: We shouldn't require complete redeclaration chains here. 9426 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9427 PBEnd = PendingBodies.end(); 9428 PB != PBEnd; ++PB) { 9429 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9430 // For a function defined inline within a class template, force the 9431 // canonical definition to be the one inside the canonical definition of 9432 // the template. This ensures that we instantiate from a correct view 9433 // of the template. 9434 // 9435 // Sadly we can't do this more generally: we can't be sure that all 9436 // copies of an arbitrary class definition will have the same members 9437 // defined (eg, some member functions may not be instantiated, and some 9438 // special members may or may not have been implicitly defined). 9439 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9440 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9441 continue; 9442 9443 // FIXME: Check for =delete/=default? 9444 // FIXME: Complain about ODR violations here? 9445 const FunctionDecl *Defn = nullptr; 9446 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9447 FD->setLazyBody(PB->second); 9448 } else { 9449 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9450 mergeDefinitionVisibility(NonConstDefn, FD); 9451 9452 if (!FD->isLateTemplateParsed() && 9453 !NonConstDefn->isLateTemplateParsed() && 9454 FD->getODRHash() != NonConstDefn->getODRHash()) { 9455 if (!isa<CXXMethodDecl>(FD)) { 9456 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9457 } else if (FD->getLexicalParent()->isFileContext() && 9458 NonConstDefn->getLexicalParent()->isFileContext()) { 9459 // Only diagnose out-of-line method definitions. If they are 9460 // in class definitions, then an error will be generated when 9461 // processing the class bodies. 9462 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9463 } 9464 } 9465 } 9466 continue; 9467 } 9468 9469 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9470 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9471 MD->setLazyBody(PB->second); 9472 } 9473 PendingBodies.clear(); 9474 9475 // Do some cleanup. 9476 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9477 getContext().deduplicateMergedDefinitonsFor(ND); 9478 PendingMergedDefinitionsToDeduplicate.clear(); 9479 } 9480 9481 static unsigned computeODRHash(QualType Ty) { 9482 ODRHash Hasher; 9483 Hasher.AddQualType(Ty); 9484 return Hasher.CalculateHash(); 9485 } 9486 9487 static unsigned computeODRHash(const Stmt *S) { 9488 ODRHash Hasher; 9489 Hasher.AddStmt(S); 9490 return Hasher.CalculateHash(); 9491 } 9492 9493 static unsigned computeODRHash(const Decl *D) { 9494 assert(D); 9495 ODRHash Hasher; 9496 Hasher.AddSubDecl(D); 9497 return Hasher.CalculateHash(); 9498 } 9499 9500 static unsigned computeODRHash(const TemplateArgument &TA) { 9501 ODRHash Hasher; 9502 Hasher.AddTemplateArgument(TA); 9503 return Hasher.CalculateHash(); 9504 } 9505 9506 void ASTReader::diagnoseOdrViolations() { 9507 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9508 PendingFunctionOdrMergeFailures.empty() && 9509 PendingEnumOdrMergeFailures.empty()) 9510 return; 9511 9512 // Trigger the import of the full definition of each class that had any 9513 // odr-merging problems, so we can produce better diagnostics for them. 9514 // These updates may in turn find and diagnose some ODR failures, so take 9515 // ownership of the set first. 9516 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9517 PendingOdrMergeFailures.clear(); 9518 for (auto &Merge : OdrMergeFailures) { 9519 Merge.first->buildLookup(); 9520 Merge.first->decls_begin(); 9521 Merge.first->bases_begin(); 9522 Merge.first->vbases_begin(); 9523 for (auto &RecordPair : Merge.second) { 9524 auto *RD = RecordPair.first; 9525 RD->decls_begin(); 9526 RD->bases_begin(); 9527 RD->vbases_begin(); 9528 } 9529 } 9530 9531 // Trigger the import of functions. 9532 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9533 PendingFunctionOdrMergeFailures.clear(); 9534 for (auto &Merge : FunctionOdrMergeFailures) { 9535 Merge.first->buildLookup(); 9536 Merge.first->decls_begin(); 9537 Merge.first->getBody(); 9538 for (auto &FD : Merge.second) { 9539 FD->buildLookup(); 9540 FD->decls_begin(); 9541 FD->getBody(); 9542 } 9543 } 9544 9545 // Trigger the import of enums. 9546 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9547 PendingEnumOdrMergeFailures.clear(); 9548 for (auto &Merge : EnumOdrMergeFailures) { 9549 Merge.first->decls_begin(); 9550 for (auto &Enum : Merge.second) { 9551 Enum->decls_begin(); 9552 } 9553 } 9554 9555 // For each declaration from a merged context, check that the canonical 9556 // definition of that context also contains a declaration of the same 9557 // entity. 9558 // 9559 // Caution: this loop does things that might invalidate iterators into 9560 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9561 while (!PendingOdrMergeChecks.empty()) { 9562 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9563 9564 // FIXME: Skip over implicit declarations for now. This matters for things 9565 // like implicitly-declared special member functions. This isn't entirely 9566 // correct; we can end up with multiple unmerged declarations of the same 9567 // implicit entity. 9568 if (D->isImplicit()) 9569 continue; 9570 9571 DeclContext *CanonDef = D->getDeclContext(); 9572 9573 bool Found = false; 9574 const Decl *DCanon = D->getCanonicalDecl(); 9575 9576 for (auto RI : D->redecls()) { 9577 if (RI->getLexicalDeclContext() == CanonDef) { 9578 Found = true; 9579 break; 9580 } 9581 } 9582 if (Found) 9583 continue; 9584 9585 // Quick check failed, time to do the slow thing. Note, we can't just 9586 // look up the name of D in CanonDef here, because the member that is 9587 // in CanonDef might not be found by name lookup (it might have been 9588 // replaced by a more recent declaration in the lookup table), and we 9589 // can't necessarily find it in the redeclaration chain because it might 9590 // be merely mergeable, not redeclarable. 9591 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9592 for (auto *CanonMember : CanonDef->decls()) { 9593 if (CanonMember->getCanonicalDecl() == DCanon) { 9594 // This can happen if the declaration is merely mergeable and not 9595 // actually redeclarable (we looked for redeclarations earlier). 9596 // 9597 // FIXME: We should be able to detect this more efficiently, without 9598 // pulling in all of the members of CanonDef. 9599 Found = true; 9600 break; 9601 } 9602 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9603 if (ND->getDeclName() == D->getDeclName()) 9604 Candidates.push_back(ND); 9605 } 9606 9607 if (!Found) { 9608 // The AST doesn't like TagDecls becoming invalid after they've been 9609 // completed. We only really need to mark FieldDecls as invalid here. 9610 if (!isa<TagDecl>(D)) 9611 D->setInvalidDecl(); 9612 9613 // Ensure we don't accidentally recursively enter deserialization while 9614 // we're producing our diagnostic. 9615 Deserializing RecursionGuard(this); 9616 9617 std::string CanonDefModule = 9618 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9619 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9620 << D << getOwningModuleNameForDiagnostic(D) 9621 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9622 9623 if (Candidates.empty()) 9624 Diag(cast<Decl>(CanonDef)->getLocation(), 9625 diag::note_module_odr_violation_no_possible_decls) << D; 9626 else { 9627 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9628 Diag(Candidates[I]->getLocation(), 9629 diag::note_module_odr_violation_possible_decl) 9630 << Candidates[I]; 9631 } 9632 9633 DiagnosedOdrMergeFailures.insert(CanonDef); 9634 } 9635 } 9636 9637 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9638 EnumOdrMergeFailures.empty()) 9639 return; 9640 9641 // Ensure we don't accidentally recursively enter deserialization while 9642 // we're producing our diagnostics. 9643 Deserializing RecursionGuard(this); 9644 9645 // Used with err_module_odr_violation_mismatch_decl and 9646 // note_module_odr_violation_mismatch_decl 9647 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9648 enum ODRMismatchDecl { 9649 EndOfClass, 9650 PublicSpecifer, 9651 PrivateSpecifer, 9652 ProtectedSpecifer, 9653 StaticAssert, 9654 Field, 9655 CXXMethod, 9656 TypeAlias, 9657 TypeDef, 9658 Var, 9659 Friend, 9660 FunctionTemplate, 9661 Other 9662 }; 9663 9664 // These lambdas have the common portions of the ODR diagnostics. This 9665 // has the same return as Diag(), so addition parameters can be passed 9666 // in with operator<< 9667 auto ODRDiagField = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9668 StringRef SecondModule, 9669 const FieldDecl *FirstField, 9670 const FieldDecl *SecondField) { 9671 enum ODRFieldDifference { 9672 FieldName, 9673 FieldTypeName, 9674 FieldSingleBitField, 9675 FieldDifferentWidthBitField, 9676 FieldSingleMutable, 9677 FieldSingleInitializer, 9678 FieldDifferentInitializers, 9679 }; 9680 9681 auto DiagError = [FirstRecord, FirstField, FirstModule, 9682 this](ODRFieldDifference DiffType) { 9683 return Diag(FirstField->getLocation(), 9684 diag::err_module_odr_violation_field) 9685 << FirstRecord << FirstModule.empty() << FirstModule 9686 << FirstField->getSourceRange() << DiffType; 9687 }; 9688 auto DiagNote = [SecondField, SecondModule, 9689 this](ODRFieldDifference DiffType) { 9690 return Diag(SecondField->getLocation(), 9691 diag::note_module_odr_violation_field) 9692 << SecondModule << SecondField->getSourceRange() << DiffType; 9693 }; 9694 9695 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9696 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9697 if (FirstII->getName() != SecondII->getName()) { 9698 DiagError(FieldName) << FirstII; 9699 DiagNote(FieldName) << SecondII; 9700 return true; 9701 } 9702 9703 assert(getContext().hasSameType(FirstField->getType(), 9704 SecondField->getType())); 9705 9706 QualType FirstType = FirstField->getType(); 9707 QualType SecondType = SecondField->getType(); 9708 if (computeODRHash(FirstType) != computeODRHash(SecondType)) { 9709 DiagError(FieldTypeName) << FirstII << FirstType; 9710 DiagNote(FieldTypeName) << SecondII << SecondType; 9711 return true; 9712 } 9713 9714 const bool IsFirstBitField = FirstField->isBitField(); 9715 const bool IsSecondBitField = SecondField->isBitField(); 9716 if (IsFirstBitField != IsSecondBitField) { 9717 DiagError(FieldSingleBitField) << FirstII << IsFirstBitField; 9718 DiagNote(FieldSingleBitField) << SecondII << IsSecondBitField; 9719 return true; 9720 } 9721 9722 if (IsFirstBitField && IsSecondBitField) { 9723 unsigned FirstBitWidthHash = computeODRHash(FirstField->getBitWidth()); 9724 unsigned SecondBitWidthHash = computeODRHash(SecondField->getBitWidth()); 9725 if (FirstBitWidthHash != SecondBitWidthHash) { 9726 DiagError(FieldDifferentWidthBitField) 9727 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9728 DiagNote(FieldDifferentWidthBitField) 9729 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9730 return true; 9731 } 9732 } 9733 9734 if (!PP.getLangOpts().CPlusPlus) 9735 return false; 9736 9737 const bool IsFirstMutable = FirstField->isMutable(); 9738 const bool IsSecondMutable = SecondField->isMutable(); 9739 if (IsFirstMutable != IsSecondMutable) { 9740 DiagError(FieldSingleMutable) << FirstII << IsFirstMutable; 9741 DiagNote(FieldSingleMutable) << SecondII << IsSecondMutable; 9742 return true; 9743 } 9744 9745 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9746 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9747 if ((!FirstInitializer && SecondInitializer) || 9748 (FirstInitializer && !SecondInitializer)) { 9749 DiagError(FieldSingleInitializer) 9750 << FirstII << (FirstInitializer != nullptr); 9751 DiagNote(FieldSingleInitializer) 9752 << SecondII << (SecondInitializer != nullptr); 9753 return true; 9754 } 9755 9756 if (FirstInitializer && SecondInitializer) { 9757 unsigned FirstInitHash = computeODRHash(FirstInitializer); 9758 unsigned SecondInitHash = computeODRHash(SecondInitializer); 9759 if (FirstInitHash != SecondInitHash) { 9760 DiagError(FieldDifferentInitializers) 9761 << FirstII << FirstInitializer->getSourceRange(); 9762 DiagNote(FieldDifferentInitializers) 9763 << SecondII << SecondInitializer->getSourceRange(); 9764 return true; 9765 } 9766 } 9767 9768 return false; 9769 }; 9770 9771 auto ODRDiagTypeDefOrAlias = 9772 [this](NamedDecl *FirstRecord, StringRef FirstModule, 9773 StringRef SecondModule, const TypedefNameDecl *FirstTD, 9774 const TypedefNameDecl *SecondTD, bool IsTypeAlias) { 9775 enum ODRTypedefDifference { 9776 TypedefName, 9777 TypedefType, 9778 }; 9779 9780 auto DiagError = [FirstRecord, FirstTD, FirstModule, 9781 this](ODRTypedefDifference DiffType) { 9782 return Diag(FirstTD->getLocation(), 9783 diag::err_module_odr_violation_typedef) 9784 << FirstRecord << FirstModule.empty() << FirstModule 9785 << FirstTD->getSourceRange() << DiffType; 9786 }; 9787 auto DiagNote = [SecondTD, SecondModule, 9788 this](ODRTypedefDifference DiffType) { 9789 return Diag(SecondTD->getLocation(), 9790 diag::note_module_odr_violation_typedef) 9791 << SecondModule << SecondTD->getSourceRange() << DiffType; 9792 }; 9793 9794 DeclarationName FirstName = FirstTD->getDeclName(); 9795 DeclarationName SecondName = SecondTD->getDeclName(); 9796 if (FirstName != SecondName) { 9797 DiagError(TypedefName) << IsTypeAlias << FirstName; 9798 DiagNote(TypedefName) << IsTypeAlias << SecondName; 9799 return true; 9800 } 9801 9802 QualType FirstType = FirstTD->getUnderlyingType(); 9803 QualType SecondType = SecondTD->getUnderlyingType(); 9804 if (computeODRHash(FirstType) != computeODRHash(SecondType)) { 9805 DiagError(TypedefType) << IsTypeAlias << FirstName << FirstType; 9806 DiagNote(TypedefType) << IsTypeAlias << SecondName << SecondType; 9807 return true; 9808 } 9809 9810 return false; 9811 }; 9812 9813 auto ODRDiagVar = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9814 StringRef SecondModule, const VarDecl *FirstVD, 9815 const VarDecl *SecondVD) { 9816 enum ODRVarDifference { 9817 VarName, 9818 VarType, 9819 VarSingleInitializer, 9820 VarDifferentInitializer, 9821 VarConstexpr, 9822 }; 9823 9824 auto DiagError = [FirstRecord, FirstVD, FirstModule, 9825 this](ODRVarDifference DiffType) { 9826 return Diag(FirstVD->getLocation(), 9827 diag::err_module_odr_violation_variable) 9828 << FirstRecord << FirstModule.empty() << FirstModule 9829 << FirstVD->getSourceRange() << DiffType; 9830 }; 9831 auto DiagNote = [SecondVD, SecondModule, this](ODRVarDifference DiffType) { 9832 return Diag(SecondVD->getLocation(), 9833 diag::note_module_odr_violation_variable) 9834 << SecondModule << SecondVD->getSourceRange() << DiffType; 9835 }; 9836 9837 DeclarationName FirstName = FirstVD->getDeclName(); 9838 DeclarationName SecondName = SecondVD->getDeclName(); 9839 if (FirstName != SecondName) { 9840 DiagError(VarName) << FirstName; 9841 DiagNote(VarName) << SecondName; 9842 return true; 9843 } 9844 9845 QualType FirstType = FirstVD->getType(); 9846 QualType SecondType = SecondVD->getType(); 9847 if (computeODRHash(FirstType) != computeODRHash(SecondType)) { 9848 DiagError(VarType) << FirstName << FirstType; 9849 DiagNote(VarType) << SecondName << SecondType; 9850 return true; 9851 } 9852 9853 if (!PP.getLangOpts().CPlusPlus) 9854 return false; 9855 9856 const Expr *FirstInit = FirstVD->getInit(); 9857 const Expr *SecondInit = SecondVD->getInit(); 9858 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9859 DiagError(VarSingleInitializer) 9860 << FirstName << (FirstInit == nullptr) 9861 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9862 DiagNote(VarSingleInitializer) 9863 << SecondName << (SecondInit == nullptr) 9864 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9865 return true; 9866 } 9867 9868 if (FirstInit && SecondInit && 9869 computeODRHash(FirstInit) != computeODRHash(SecondInit)) { 9870 DiagError(VarDifferentInitializer) 9871 << FirstName << FirstInit->getSourceRange(); 9872 DiagNote(VarDifferentInitializer) 9873 << SecondName << SecondInit->getSourceRange(); 9874 return true; 9875 } 9876 9877 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9878 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9879 if (FirstIsConstexpr != SecondIsConstexpr) { 9880 DiagError(VarConstexpr) << FirstName << FirstIsConstexpr; 9881 DiagNote(VarConstexpr) << SecondName << SecondIsConstexpr; 9882 return true; 9883 } 9884 return false; 9885 }; 9886 9887 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9888 auto PopulateHashes = [](DeclHashes &Hashes, RecordDecl *Record, 9889 const DeclContext *DC) { 9890 for (auto *D : Record->decls()) { 9891 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9892 continue; 9893 Hashes.emplace_back(D, computeODRHash(D)); 9894 } 9895 }; 9896 9897 struct DiffResult { 9898 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9899 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9900 }; 9901 9902 // If there is a diagnoseable difference, FirstDiffType and 9903 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9904 // filled in if not EndOfClass. 9905 auto FindTypeDiffs = [](DeclHashes &FirstHashes, DeclHashes &SecondHashes) { 9906 auto DifferenceSelector = [](Decl *D) { 9907 assert(D && "valid Decl required"); 9908 switch (D->getKind()) { 9909 default: 9910 return Other; 9911 case Decl::AccessSpec: 9912 switch (D->getAccess()) { 9913 case AS_public: 9914 return PublicSpecifer; 9915 case AS_private: 9916 return PrivateSpecifer; 9917 case AS_protected: 9918 return ProtectedSpecifer; 9919 case AS_none: 9920 break; 9921 } 9922 llvm_unreachable("Invalid access specifier"); 9923 case Decl::StaticAssert: 9924 return StaticAssert; 9925 case Decl::Field: 9926 return Field; 9927 case Decl::CXXMethod: 9928 case Decl::CXXConstructor: 9929 case Decl::CXXDestructor: 9930 return CXXMethod; 9931 case Decl::TypeAlias: 9932 return TypeAlias; 9933 case Decl::Typedef: 9934 return TypeDef; 9935 case Decl::Var: 9936 return Var; 9937 case Decl::Friend: 9938 return Friend; 9939 case Decl::FunctionTemplate: 9940 return FunctionTemplate; 9941 } 9942 }; 9943 9944 DiffResult DR; 9945 auto FirstIt = FirstHashes.begin(); 9946 auto SecondIt = SecondHashes.begin(); 9947 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9948 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9949 FirstIt->second == SecondIt->second) { 9950 ++FirstIt; 9951 ++SecondIt; 9952 continue; 9953 } 9954 9955 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9956 DR.SecondDecl = 9957 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9958 9959 DR.FirstDiffType = 9960 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9961 DR.SecondDiffType = 9962 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9963 return DR; 9964 } 9965 return DR; 9966 }; 9967 9968 // Use this to diagnose that an unexpected Decl was encountered 9969 // or no difference was detected. This causes a generic error 9970 // message to be emitted. 9971 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9972 StringRef FirstModule, 9973 NamedDecl *SecondRecord, 9974 StringRef SecondModule) { 9975 Diag(FirstRecord->getLocation(), 9976 diag::err_module_odr_violation_different_definitions) 9977 << FirstRecord << FirstModule.empty() << FirstModule; 9978 9979 if (DR.FirstDecl) { 9980 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9981 << FirstRecord << DR.FirstDecl->getSourceRange(); 9982 } 9983 9984 Diag(SecondRecord->getLocation(), 9985 diag::note_module_odr_violation_different_definitions) 9986 << SecondModule; 9987 9988 if (DR.SecondDecl) { 9989 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9990 << DR.SecondDecl->getSourceRange(); 9991 } 9992 }; 9993 9994 auto DiagnoseODRMismatch = [this](DiffResult &DR, NamedDecl *FirstRecord, 9995 StringRef FirstModule, 9996 NamedDecl *SecondRecord, 9997 StringRef SecondModule) { 9998 auto GetMismatchedDeclLoc = [](const NamedDecl *Container, 9999 ODRMismatchDecl DiffType, const Decl *D) { 10000 SourceLocation Loc; 10001 SourceRange Range; 10002 auto *Tag = dyn_cast<TagDecl>(Container); 10003 if (DiffType == EndOfClass && Tag) { 10004 Loc = Tag->getBraceRange().getEnd(); 10005 } else { 10006 Loc = D->getLocation(); 10007 Range = D->getSourceRange(); 10008 } 10009 return std::make_pair(Loc, Range); 10010 }; 10011 10012 auto FirstDiagInfo = 10013 GetMismatchedDeclLoc(FirstRecord, DR.FirstDiffType, DR.FirstDecl); 10014 Diag(FirstDiagInfo.first, diag::err_module_odr_violation_mismatch_decl) 10015 << FirstRecord << FirstModule.empty() << FirstModule 10016 << FirstDiagInfo.second << DR.FirstDiffType; 10017 10018 auto SecondDiagInfo = 10019 GetMismatchedDeclLoc(SecondRecord, DR.SecondDiffType, DR.SecondDecl); 10020 Diag(SecondDiagInfo.first, diag::note_module_odr_violation_mismatch_decl) 10021 << SecondModule << SecondDiagInfo.second << DR.SecondDiffType; 10022 }; 10023 10024 // Issue any pending ODR-failure diagnostics. 10025 for (auto &Merge : OdrMergeFailures) { 10026 // If we've already pointed out a specific problem with this class, don't 10027 // bother issuing a general "something's different" diagnostic. 10028 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10029 continue; 10030 10031 bool Diagnosed = false; 10032 CXXRecordDecl *FirstRecord = Merge.first; 10033 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10034 for (auto &RecordPair : Merge.second) { 10035 CXXRecordDecl *SecondRecord = RecordPair.first; 10036 // Multiple different declarations got merged together; tell the user 10037 // where they came from. 10038 if (FirstRecord == SecondRecord) 10039 continue; 10040 10041 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10042 10043 auto *FirstDD = FirstRecord->DefinitionData; 10044 auto *SecondDD = RecordPair.second; 10045 10046 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10047 10048 // Diagnostics from DefinitionData are emitted here. 10049 if (FirstDD != SecondDD) { 10050 enum ODRDefinitionDataDifference { 10051 NumBases, 10052 NumVBases, 10053 BaseType, 10054 BaseVirtual, 10055 BaseAccess, 10056 }; 10057 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10058 this](SourceLocation Loc, SourceRange Range, 10059 ODRDefinitionDataDifference DiffType) { 10060 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10061 << FirstRecord << FirstModule.empty() << FirstModule << Range 10062 << DiffType; 10063 }; 10064 auto ODRDiagBaseNote = [&SecondModule, 10065 this](SourceLocation Loc, SourceRange Range, 10066 ODRDefinitionDataDifference DiffType) { 10067 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10068 << SecondModule << Range << DiffType; 10069 }; 10070 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10071 unsigned NumBases = DD->NumBases; 10072 if (NumBases == 0) return SourceRange(); 10073 ArrayRef<CXXBaseSpecifier> bases = DD->bases(); 10074 return SourceRange(bases[0].getBeginLoc(), 10075 bases[NumBases - 1].getEndLoc()); 10076 }; 10077 10078 unsigned FirstNumBases = FirstDD->NumBases; 10079 unsigned FirstNumVBases = FirstDD->NumVBases; 10080 unsigned SecondNumBases = SecondDD->NumBases; 10081 unsigned SecondNumVBases = SecondDD->NumVBases; 10082 if (FirstNumBases != SecondNumBases) { 10083 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10084 NumBases) 10085 << FirstNumBases; 10086 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10087 NumBases) 10088 << SecondNumBases; 10089 Diagnosed = true; 10090 break; 10091 } 10092 10093 if (FirstNumVBases != SecondNumVBases) { 10094 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10095 NumVBases) 10096 << FirstNumVBases; 10097 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10098 NumVBases) 10099 << SecondNumVBases; 10100 Diagnosed = true; 10101 break; 10102 } 10103 10104 ArrayRef<CXXBaseSpecifier> FirstBases = FirstDD->bases(); 10105 ArrayRef<CXXBaseSpecifier> SecondBases = SecondDD->bases(); 10106 unsigned I = 0; 10107 for (I = 0; I < FirstNumBases; ++I) { 10108 const CXXBaseSpecifier FirstBase = FirstBases[I]; 10109 const CXXBaseSpecifier SecondBase = SecondBases[I]; 10110 if (computeODRHash(FirstBase.getType()) != 10111 computeODRHash(SecondBase.getType())) { 10112 ODRDiagBaseError(FirstRecord->getLocation(), 10113 FirstBase.getSourceRange(), BaseType) 10114 << (I + 1) << FirstBase.getType(); 10115 ODRDiagBaseNote(SecondRecord->getLocation(), 10116 SecondBase.getSourceRange(), BaseType) 10117 << (I + 1) << SecondBase.getType(); 10118 break; 10119 } 10120 10121 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10122 ODRDiagBaseError(FirstRecord->getLocation(), 10123 FirstBase.getSourceRange(), BaseVirtual) 10124 << (I + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10125 ODRDiagBaseNote(SecondRecord->getLocation(), 10126 SecondBase.getSourceRange(), BaseVirtual) 10127 << (I + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10128 break; 10129 } 10130 10131 if (FirstBase.getAccessSpecifierAsWritten() != 10132 SecondBase.getAccessSpecifierAsWritten()) { 10133 ODRDiagBaseError(FirstRecord->getLocation(), 10134 FirstBase.getSourceRange(), BaseAccess) 10135 << (I + 1) << FirstBase.getType() 10136 << (int)FirstBase.getAccessSpecifierAsWritten(); 10137 ODRDiagBaseNote(SecondRecord->getLocation(), 10138 SecondBase.getSourceRange(), BaseAccess) 10139 << (I + 1) << SecondBase.getType() 10140 << (int)SecondBase.getAccessSpecifierAsWritten(); 10141 break; 10142 } 10143 } 10144 10145 if (I != FirstNumBases) { 10146 Diagnosed = true; 10147 break; 10148 } 10149 } 10150 10151 const ClassTemplateDecl *FirstTemplate = 10152 FirstRecord->getDescribedClassTemplate(); 10153 const ClassTemplateDecl *SecondTemplate = 10154 SecondRecord->getDescribedClassTemplate(); 10155 10156 assert(!FirstTemplate == !SecondTemplate && 10157 "Both pointers should be null or non-null"); 10158 10159 if (FirstTemplate && SecondTemplate) { 10160 DeclHashes FirstTemplateHashes; 10161 DeclHashes SecondTemplateHashes; 10162 10163 auto PopulateTemplateParameterHashs = [](DeclHashes &Hashes, 10164 const ClassTemplateDecl *TD) { 10165 for (auto *D : TD->getTemplateParameters()->asArray()) { 10166 Hashes.emplace_back(D, computeODRHash(D)); 10167 } 10168 }; 10169 10170 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10171 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10172 10173 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10174 "Number of template parameters should be equal."); 10175 10176 auto FirstIt = FirstTemplateHashes.begin(); 10177 auto FirstEnd = FirstTemplateHashes.end(); 10178 auto SecondIt = SecondTemplateHashes.begin(); 10179 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10180 if (FirstIt->second == SecondIt->second) 10181 continue; 10182 10183 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10184 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10185 10186 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10187 "Parameter Decl's should be the same kind."); 10188 10189 enum ODRTemplateDifference { 10190 ParamEmptyName, 10191 ParamName, 10192 ParamSingleDefaultArgument, 10193 ParamDifferentDefaultArgument, 10194 }; 10195 10196 auto hasDefaultArg = [](const NamedDecl *D) { 10197 if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 10198 return TTP->hasDefaultArgument() && 10199 !TTP->defaultArgumentWasInherited(); 10200 if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 10201 return NTTP->hasDefaultArgument() && 10202 !NTTP->defaultArgumentWasInherited(); 10203 auto *TTP = cast<TemplateTemplateParmDecl>(D); 10204 return TTP->hasDefaultArgument() && 10205 !TTP->defaultArgumentWasInherited(); 10206 }; 10207 bool hasFirstArg = hasDefaultArg(FirstDecl); 10208 bool hasSecondArg = hasDefaultArg(SecondDecl); 10209 10210 ODRTemplateDifference ErrDiffType; 10211 ODRTemplateDifference NoteDiffType; 10212 10213 DeclarationName FirstName = FirstDecl->getDeclName(); 10214 DeclarationName SecondName = SecondDecl->getDeclName(); 10215 10216 if (FirstName != SecondName) { 10217 bool FirstNameEmpty = 10218 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10219 bool SecondNameEmpty = SecondName.isIdentifier() && 10220 !SecondName.getAsIdentifierInfo(); 10221 ErrDiffType = FirstNameEmpty ? ParamEmptyName : ParamName; 10222 NoteDiffType = SecondNameEmpty ? ParamEmptyName : ParamName; 10223 } else if (hasFirstArg == hasSecondArg) 10224 ErrDiffType = NoteDiffType = ParamDifferentDefaultArgument; 10225 else 10226 ErrDiffType = NoteDiffType = ParamSingleDefaultArgument; 10227 10228 Diag(FirstDecl->getLocation(), 10229 diag::err_module_odr_violation_template_parameter) 10230 << FirstRecord << FirstModule.empty() << FirstModule 10231 << FirstDecl->getSourceRange() << ErrDiffType << hasFirstArg 10232 << FirstName; 10233 Diag(SecondDecl->getLocation(), 10234 diag::note_module_odr_violation_template_parameter) 10235 << SecondModule << SecondDecl->getSourceRange() << NoteDiffType 10236 << hasSecondArg << SecondName; 10237 break; 10238 } 10239 10240 if (FirstIt != FirstEnd) { 10241 Diagnosed = true; 10242 break; 10243 } 10244 } 10245 10246 DeclHashes FirstHashes; 10247 DeclHashes SecondHashes; 10248 const DeclContext *DC = FirstRecord; 10249 PopulateHashes(FirstHashes, FirstRecord, DC); 10250 PopulateHashes(SecondHashes, SecondRecord, DC); 10251 10252 DiffResult DR = FindTypeDiffs(FirstHashes, SecondHashes); 10253 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10254 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10255 const Decl *FirstDecl = DR.FirstDecl; 10256 const Decl *SecondDecl = DR.SecondDecl; 10257 10258 if (FirstDiffType == Other || SecondDiffType == Other) { 10259 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10260 SecondModule); 10261 Diagnosed = true; 10262 break; 10263 } 10264 10265 if (FirstDiffType != SecondDiffType) { 10266 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10267 SecondModule); 10268 Diagnosed = true; 10269 break; 10270 } 10271 10272 // Used with err_module_odr_violation_record and 10273 // note_module_odr_violation_record 10274 enum ODRCXXRecordDifference { 10275 StaticAssertCondition, 10276 StaticAssertMessage, 10277 StaticAssertOnlyMessage, 10278 MethodName, 10279 MethodDeleted, 10280 MethodDefaulted, 10281 MethodVirtual, 10282 MethodStatic, 10283 MethodVolatile, 10284 MethodConst, 10285 MethodInline, 10286 MethodNumberParameters, 10287 MethodParameterType, 10288 MethodParameterName, 10289 MethodParameterSingleDefaultArgument, 10290 MethodParameterDifferentDefaultArgument, 10291 MethodNoTemplateArguments, 10292 MethodDifferentNumberTemplateArguments, 10293 MethodDifferentTemplateArgument, 10294 MethodSingleBody, 10295 MethodDifferentBody, 10296 FriendTypeFunction, 10297 FriendType, 10298 FriendFunction, 10299 FunctionTemplateDifferentNumberParameters, 10300 FunctionTemplateParameterDifferentKind, 10301 FunctionTemplateParameterName, 10302 FunctionTemplateParameterSingleDefaultArgument, 10303 FunctionTemplateParameterDifferentDefaultArgument, 10304 FunctionTemplateParameterDifferentType, 10305 FunctionTemplatePackParameter, 10306 }; 10307 auto ODRDiagDeclError = [FirstRecord, &FirstModule, 10308 this](SourceLocation Loc, SourceRange Range, 10309 ODRCXXRecordDifference DiffType) { 10310 return Diag(Loc, diag::err_module_odr_violation_record) 10311 << FirstRecord << FirstModule.empty() << FirstModule << Range 10312 << DiffType; 10313 }; 10314 auto ODRDiagDeclNote = [&SecondModule, 10315 this](SourceLocation Loc, SourceRange Range, 10316 ODRCXXRecordDifference DiffType) { 10317 return Diag(Loc, diag::note_module_odr_violation_record) 10318 << SecondModule << Range << DiffType; 10319 }; 10320 10321 assert(FirstDiffType == SecondDiffType); 10322 switch (FirstDiffType) { 10323 case Other: 10324 case EndOfClass: 10325 case PublicSpecifer: 10326 case PrivateSpecifer: 10327 case ProtectedSpecifer: 10328 llvm_unreachable("Invalid diff type"); 10329 10330 case StaticAssert: { 10331 const StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10332 const StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10333 10334 const Expr *FirstExpr = FirstSA->getAssertExpr(); 10335 const Expr *SecondExpr = SecondSA->getAssertExpr(); 10336 unsigned FirstODRHash = computeODRHash(FirstExpr); 10337 unsigned SecondODRHash = computeODRHash(SecondExpr); 10338 if (FirstODRHash != SecondODRHash) { 10339 ODRDiagDeclError(FirstExpr->getBeginLoc(), 10340 FirstExpr->getSourceRange(), StaticAssertCondition); 10341 ODRDiagDeclNote(SecondExpr->getBeginLoc(), 10342 SecondExpr->getSourceRange(), StaticAssertCondition); 10343 Diagnosed = true; 10344 break; 10345 } 10346 10347 const StringLiteral *FirstStr = FirstSA->getMessage(); 10348 const StringLiteral *SecondStr = SecondSA->getMessage(); 10349 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10350 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10351 SourceLocation FirstLoc, SecondLoc; 10352 SourceRange FirstRange, SecondRange; 10353 if (FirstStr) { 10354 FirstLoc = FirstStr->getBeginLoc(); 10355 FirstRange = FirstStr->getSourceRange(); 10356 } else { 10357 FirstLoc = FirstSA->getBeginLoc(); 10358 FirstRange = FirstSA->getSourceRange(); 10359 } 10360 if (SecondStr) { 10361 SecondLoc = SecondStr->getBeginLoc(); 10362 SecondRange = SecondStr->getSourceRange(); 10363 } else { 10364 SecondLoc = SecondSA->getBeginLoc(); 10365 SecondRange = SecondSA->getSourceRange(); 10366 } 10367 ODRDiagDeclError(FirstLoc, FirstRange, StaticAssertOnlyMessage) 10368 << (FirstStr == nullptr); 10369 ODRDiagDeclNote(SecondLoc, SecondRange, StaticAssertOnlyMessage) 10370 << (SecondStr == nullptr); 10371 Diagnosed = true; 10372 break; 10373 } 10374 10375 if (FirstStr && SecondStr && 10376 FirstStr->getString() != SecondStr->getString()) { 10377 ODRDiagDeclError(FirstStr->getBeginLoc(), FirstStr->getSourceRange(), 10378 StaticAssertMessage); 10379 ODRDiagDeclNote(SecondStr->getBeginLoc(), SecondStr->getSourceRange(), 10380 StaticAssertMessage); 10381 Diagnosed = true; 10382 break; 10383 } 10384 break; 10385 } 10386 case Field: { 10387 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10388 cast<FieldDecl>(FirstDecl), 10389 cast<FieldDecl>(SecondDecl)); 10390 break; 10391 } 10392 case CXXMethod: { 10393 enum { 10394 DiagMethod, 10395 DiagConstructor, 10396 DiagDestructor, 10397 } FirstMethodType, 10398 SecondMethodType; 10399 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10400 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10401 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10402 return DiagMethod; 10403 }; 10404 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10405 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10406 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10407 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10408 DeclarationName FirstName = FirstMethod->getDeclName(); 10409 DeclarationName SecondName = SecondMethod->getDeclName(); 10410 auto DiagMethodError = [&ODRDiagDeclError, FirstMethod, FirstMethodType, 10411 FirstName](ODRCXXRecordDifference DiffType) { 10412 return ODRDiagDeclError(FirstMethod->getLocation(), 10413 FirstMethod->getSourceRange(), DiffType) 10414 << FirstMethodType << FirstName; 10415 }; 10416 auto DiagMethodNote = [&ODRDiagDeclNote, SecondMethod, SecondMethodType, 10417 SecondName](ODRCXXRecordDifference DiffType) { 10418 return ODRDiagDeclNote(SecondMethod->getLocation(), 10419 SecondMethod->getSourceRange(), DiffType) 10420 << SecondMethodType << SecondName; 10421 }; 10422 10423 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10424 DiagMethodError(MethodName); 10425 DiagMethodNote(MethodName); 10426 Diagnosed = true; 10427 break; 10428 } 10429 10430 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10431 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10432 if (FirstDeleted != SecondDeleted) { 10433 DiagMethodError(MethodDeleted) << FirstDeleted; 10434 DiagMethodNote(MethodDeleted) << SecondDeleted; 10435 Diagnosed = true; 10436 break; 10437 } 10438 10439 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10440 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10441 if (FirstDefaulted != SecondDefaulted) { 10442 DiagMethodError(MethodDefaulted) << FirstDefaulted; 10443 DiagMethodNote(MethodDefaulted) << SecondDefaulted; 10444 Diagnosed = true; 10445 break; 10446 } 10447 10448 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10449 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10450 const bool FirstPure = FirstMethod->isPure(); 10451 const bool SecondPure = SecondMethod->isPure(); 10452 if ((FirstVirtual || SecondVirtual) && 10453 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10454 DiagMethodError(MethodVirtual) << FirstPure << FirstVirtual; 10455 DiagMethodNote(MethodVirtual) << SecondPure << SecondVirtual; 10456 Diagnosed = true; 10457 break; 10458 } 10459 10460 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10461 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10462 // class needs to be checked instead. 10463 StorageClass FirstStorage = FirstMethod->getStorageClass(); 10464 StorageClass SecondStorage = SecondMethod->getStorageClass(); 10465 const bool FirstStatic = FirstStorage == SC_Static; 10466 const bool SecondStatic = SecondStorage == SC_Static; 10467 if (FirstStatic != SecondStatic) { 10468 DiagMethodError(MethodStatic) << FirstStatic; 10469 DiagMethodNote(MethodStatic) << SecondStatic; 10470 Diagnosed = true; 10471 break; 10472 } 10473 10474 const bool FirstVolatile = FirstMethod->isVolatile(); 10475 const bool SecondVolatile = SecondMethod->isVolatile(); 10476 if (FirstVolatile != SecondVolatile) { 10477 DiagMethodError(MethodVolatile) << FirstVolatile; 10478 DiagMethodNote(MethodVolatile) << SecondVolatile; 10479 Diagnosed = true; 10480 break; 10481 } 10482 10483 const bool FirstConst = FirstMethod->isConst(); 10484 const bool SecondConst = SecondMethod->isConst(); 10485 if (FirstConst != SecondConst) { 10486 DiagMethodError(MethodConst) << FirstConst; 10487 DiagMethodNote(MethodConst) << SecondConst; 10488 Diagnosed = true; 10489 break; 10490 } 10491 10492 const bool FirstInline = FirstMethod->isInlineSpecified(); 10493 const bool SecondInline = SecondMethod->isInlineSpecified(); 10494 if (FirstInline != SecondInline) { 10495 DiagMethodError(MethodInline) << FirstInline; 10496 DiagMethodNote(MethodInline) << SecondInline; 10497 Diagnosed = true; 10498 break; 10499 } 10500 10501 const unsigned FirstNumParameters = FirstMethod->param_size(); 10502 const unsigned SecondNumParameters = SecondMethod->param_size(); 10503 if (FirstNumParameters != SecondNumParameters) { 10504 DiagMethodError(MethodNumberParameters) << FirstNumParameters; 10505 DiagMethodNote(MethodNumberParameters) << SecondNumParameters; 10506 Diagnosed = true; 10507 break; 10508 } 10509 10510 // Need this status boolean to know when break out of the switch. 10511 bool ParameterMismatch = false; 10512 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10513 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10514 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10515 10516 QualType FirstParamType = FirstParam->getType(); 10517 QualType SecondParamType = SecondParam->getType(); 10518 if (FirstParamType != SecondParamType && 10519 computeODRHash(FirstParamType) != 10520 computeODRHash(SecondParamType)) { 10521 if (const DecayedType *ParamDecayedType = 10522 FirstParamType->getAs<DecayedType>()) { 10523 DiagMethodError(MethodParameterType) 10524 << (I + 1) << FirstParamType << true 10525 << ParamDecayedType->getOriginalType(); 10526 } else { 10527 DiagMethodError(MethodParameterType) 10528 << (I + 1) << FirstParamType << false; 10529 } 10530 10531 if (const DecayedType *ParamDecayedType = 10532 SecondParamType->getAs<DecayedType>()) { 10533 DiagMethodNote(MethodParameterType) 10534 << (I + 1) << SecondParamType << true 10535 << ParamDecayedType->getOriginalType(); 10536 } else { 10537 DiagMethodNote(MethodParameterType) 10538 << (I + 1) << SecondParamType << false; 10539 } 10540 ParameterMismatch = true; 10541 break; 10542 } 10543 10544 DeclarationName FirstParamName = FirstParam->getDeclName(); 10545 DeclarationName SecondParamName = SecondParam->getDeclName(); 10546 if (FirstParamName != SecondParamName) { 10547 DiagMethodError(MethodParameterName) << (I + 1) << FirstParamName; 10548 DiagMethodNote(MethodParameterName) << (I + 1) << SecondParamName; 10549 ParameterMismatch = true; 10550 break; 10551 } 10552 10553 const Expr *FirstInit = FirstParam->getInit(); 10554 const Expr *SecondInit = SecondParam->getInit(); 10555 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10556 DiagMethodError(MethodParameterSingleDefaultArgument) 10557 << (I + 1) << (FirstInit == nullptr) 10558 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10559 DiagMethodNote(MethodParameterSingleDefaultArgument) 10560 << (I + 1) << (SecondInit == nullptr) 10561 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10562 ParameterMismatch = true; 10563 break; 10564 } 10565 10566 if (FirstInit && SecondInit && 10567 computeODRHash(FirstInit) != computeODRHash(SecondInit)) { 10568 DiagMethodError(MethodParameterDifferentDefaultArgument) 10569 << (I + 1) << FirstInit->getSourceRange(); 10570 DiagMethodNote(MethodParameterDifferentDefaultArgument) 10571 << (I + 1) << SecondInit->getSourceRange(); 10572 ParameterMismatch = true; 10573 break; 10574 } 10575 } 10576 10577 if (ParameterMismatch) { 10578 Diagnosed = true; 10579 break; 10580 } 10581 10582 const TemplateArgumentList *FirstTemplateArgs = 10583 FirstMethod->getTemplateSpecializationArgs(); 10584 const TemplateArgumentList *SecondTemplateArgs = 10585 SecondMethod->getTemplateSpecializationArgs(); 10586 10587 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10588 (!FirstTemplateArgs && SecondTemplateArgs)) { 10589 DiagMethodError(MethodNoTemplateArguments) 10590 << (FirstTemplateArgs != nullptr); 10591 DiagMethodNote(MethodNoTemplateArguments) 10592 << (SecondTemplateArgs != nullptr); 10593 Diagnosed = true; 10594 break; 10595 } 10596 10597 if (FirstTemplateArgs && SecondTemplateArgs) { 10598 // Remove pack expansions from argument list. 10599 auto ExpandTemplateArgumentList = 10600 [](const TemplateArgumentList *TAL) { 10601 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10602 for (const TemplateArgument &TA : TAL->asArray()) { 10603 if (TA.getKind() != TemplateArgument::Pack) { 10604 ExpandedList.push_back(&TA); 10605 continue; 10606 } 10607 llvm::append_range(ExpandedList, llvm::make_pointer_range( 10608 TA.getPackAsArray())); 10609 } 10610 return ExpandedList; 10611 }; 10612 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10613 ExpandTemplateArgumentList(FirstTemplateArgs); 10614 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10615 ExpandTemplateArgumentList(SecondTemplateArgs); 10616 10617 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10618 DiagMethodError(MethodDifferentNumberTemplateArguments) 10619 << (unsigned)FirstExpandedList.size(); 10620 DiagMethodNote(MethodDifferentNumberTemplateArguments) 10621 << (unsigned)SecondExpandedList.size(); 10622 Diagnosed = true; 10623 break; 10624 } 10625 10626 bool TemplateArgumentMismatch = false; 10627 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10628 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10629 &SecondTA = *SecondExpandedList[i]; 10630 if (computeODRHash(FirstTA) == computeODRHash(SecondTA)) { 10631 continue; 10632 } 10633 10634 DiagMethodError(MethodDifferentTemplateArgument) 10635 << FirstTA << i + 1; 10636 DiagMethodNote(MethodDifferentTemplateArgument) 10637 << SecondTA << i + 1; 10638 TemplateArgumentMismatch = true; 10639 break; 10640 } 10641 10642 if (TemplateArgumentMismatch) { 10643 Diagnosed = true; 10644 break; 10645 } 10646 } 10647 10648 // Compute the hash of the method as if it has no body. 10649 auto ComputeCXXMethodODRHash = [](const CXXMethodDecl *D) { 10650 ODRHash Hasher; 10651 Hasher.AddFunctionDecl(D, true /*SkipBody*/); 10652 return Hasher.CalculateHash(); 10653 }; 10654 10655 // Compare the hash generated to the hash stored. A difference means 10656 // that a body was present in the original source. Due to merging, 10657 // the standard way of detecting a body will not work. 10658 const bool HasFirstBody = 10659 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10660 const bool HasSecondBody = 10661 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10662 10663 if (HasFirstBody != HasSecondBody) { 10664 DiagMethodError(MethodSingleBody) << HasFirstBody; 10665 DiagMethodNote(MethodSingleBody) << HasSecondBody; 10666 Diagnosed = true; 10667 break; 10668 } 10669 10670 if (HasFirstBody && HasSecondBody) { 10671 DiagMethodError(MethodDifferentBody); 10672 DiagMethodNote(MethodDifferentBody); 10673 Diagnosed = true; 10674 break; 10675 } 10676 10677 break; 10678 } 10679 case TypeAlias: 10680 case TypeDef: { 10681 Diagnosed = ODRDiagTypeDefOrAlias( 10682 FirstRecord, FirstModule, SecondModule, 10683 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10684 FirstDiffType == TypeAlias); 10685 break; 10686 } 10687 case Var: { 10688 Diagnosed = 10689 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10690 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10691 break; 10692 } 10693 case Friend: { 10694 const FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10695 const FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10696 10697 const NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10698 const NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10699 10700 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10701 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10702 10703 if (FirstND && SecondND) { 10704 ODRDiagDeclError(FirstFriend->getFriendLoc(), 10705 FirstFriend->getSourceRange(), FriendFunction) 10706 << FirstND; 10707 ODRDiagDeclNote(SecondFriend->getFriendLoc(), 10708 SecondFriend->getSourceRange(), FriendFunction) 10709 << SecondND; 10710 Diagnosed = true; 10711 break; 10712 } 10713 10714 if (FirstTSI && SecondTSI) { 10715 QualType FirstFriendType = FirstTSI->getType(); 10716 QualType SecondFriendType = SecondTSI->getType(); 10717 assert(computeODRHash(FirstFriendType) != 10718 computeODRHash(SecondFriendType)); 10719 ODRDiagDeclError(FirstFriend->getFriendLoc(), 10720 FirstFriend->getSourceRange(), FriendType) 10721 << FirstFriendType; 10722 ODRDiagDeclNote(SecondFriend->getFriendLoc(), 10723 SecondFriend->getSourceRange(), FriendType) 10724 << SecondFriendType; 10725 Diagnosed = true; 10726 break; 10727 } 10728 10729 ODRDiagDeclError(FirstFriend->getFriendLoc(), 10730 FirstFriend->getSourceRange(), FriendTypeFunction) 10731 << (FirstTSI == nullptr); 10732 ODRDiagDeclNote(SecondFriend->getFriendLoc(), 10733 SecondFriend->getSourceRange(), FriendTypeFunction) 10734 << (SecondTSI == nullptr); 10735 Diagnosed = true; 10736 break; 10737 } 10738 case FunctionTemplate: { 10739 const FunctionTemplateDecl *FirstTemplate = 10740 cast<FunctionTemplateDecl>(FirstDecl); 10741 const FunctionTemplateDecl *SecondTemplate = 10742 cast<FunctionTemplateDecl>(SecondDecl); 10743 10744 TemplateParameterList *FirstTPL = 10745 FirstTemplate->getTemplateParameters(); 10746 TemplateParameterList *SecondTPL = 10747 SecondTemplate->getTemplateParameters(); 10748 10749 auto DiagTemplateError = [&ODRDiagDeclError, FirstTemplate]( 10750 ODRCXXRecordDifference DiffType) { 10751 return ODRDiagDeclError(FirstTemplate->getLocation(), 10752 FirstTemplate->getSourceRange(), DiffType) 10753 << FirstTemplate; 10754 }; 10755 auto DiagTemplateNote = [&ODRDiagDeclNote, SecondTemplate]( 10756 ODRCXXRecordDifference DiffType) { 10757 return ODRDiagDeclNote(SecondTemplate->getLocation(), 10758 SecondTemplate->getSourceRange(), DiffType) 10759 << SecondTemplate; 10760 }; 10761 10762 if (FirstTPL->size() != SecondTPL->size()) { 10763 DiagTemplateError(FunctionTemplateDifferentNumberParameters) 10764 << FirstTPL->size(); 10765 DiagTemplateNote(FunctionTemplateDifferentNumberParameters) 10766 << SecondTPL->size(); 10767 Diagnosed = true; 10768 break; 10769 } 10770 10771 bool ParameterMismatch = false; 10772 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10773 NamedDecl *FirstParam = FirstTPL->getParam(i); 10774 NamedDecl *SecondParam = SecondTPL->getParam(i); 10775 10776 if (FirstParam->getKind() != SecondParam->getKind()) { 10777 enum { 10778 TemplateTypeParameter, 10779 NonTypeTemplateParameter, 10780 TemplateTemplateParameter, 10781 }; 10782 auto GetParamType = [](NamedDecl *D) { 10783 switch (D->getKind()) { 10784 default: 10785 llvm_unreachable("Unexpected template parameter type"); 10786 case Decl::TemplateTypeParm: 10787 return TemplateTypeParameter; 10788 case Decl::NonTypeTemplateParm: 10789 return NonTypeTemplateParameter; 10790 case Decl::TemplateTemplateParm: 10791 return TemplateTemplateParameter; 10792 } 10793 }; 10794 10795 DiagTemplateError(FunctionTemplateParameterDifferentKind) 10796 << (i + 1) << GetParamType(FirstParam); 10797 DiagTemplateNote(FunctionTemplateParameterDifferentKind) 10798 << (i + 1) << GetParamType(SecondParam); 10799 ParameterMismatch = true; 10800 break; 10801 } 10802 10803 if (FirstParam->getName() != SecondParam->getName()) { 10804 DiagTemplateError(FunctionTemplateParameterName) 10805 << (i + 1) << (bool)FirstParam->getIdentifier() << FirstParam; 10806 DiagTemplateNote(FunctionTemplateParameterName) 10807 << (i + 1) << (bool)SecondParam->getIdentifier() << SecondParam; 10808 ParameterMismatch = true; 10809 break; 10810 } 10811 10812 if (isa<TemplateTypeParmDecl>(FirstParam) && 10813 isa<TemplateTypeParmDecl>(SecondParam)) { 10814 TemplateTypeParmDecl *FirstTTPD = 10815 cast<TemplateTypeParmDecl>(FirstParam); 10816 TemplateTypeParmDecl *SecondTTPD = 10817 cast<TemplateTypeParmDecl>(SecondParam); 10818 bool HasFirstDefaultArgument = 10819 FirstTTPD->hasDefaultArgument() && 10820 !FirstTTPD->defaultArgumentWasInherited(); 10821 bool HasSecondDefaultArgument = 10822 SecondTTPD->hasDefaultArgument() && 10823 !SecondTTPD->defaultArgumentWasInherited(); 10824 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10825 DiagTemplateError(FunctionTemplateParameterSingleDefaultArgument) 10826 << (i + 1) << HasFirstDefaultArgument; 10827 DiagTemplateNote(FunctionTemplateParameterSingleDefaultArgument) 10828 << (i + 1) << HasSecondDefaultArgument; 10829 ParameterMismatch = true; 10830 break; 10831 } 10832 10833 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10834 QualType FirstType = FirstTTPD->getDefaultArgument(); 10835 QualType SecondType = SecondTTPD->getDefaultArgument(); 10836 if (computeODRHash(FirstType) != computeODRHash(SecondType)) { 10837 DiagTemplateError( 10838 FunctionTemplateParameterDifferentDefaultArgument) 10839 << (i + 1) << FirstType; 10840 DiagTemplateNote( 10841 FunctionTemplateParameterDifferentDefaultArgument) 10842 << (i + 1) << SecondType; 10843 ParameterMismatch = true; 10844 break; 10845 } 10846 } 10847 10848 if (FirstTTPD->isParameterPack() != 10849 SecondTTPD->isParameterPack()) { 10850 DiagTemplateError(FunctionTemplatePackParameter) 10851 << (i + 1) << FirstTTPD->isParameterPack(); 10852 DiagTemplateNote(FunctionTemplatePackParameter) 10853 << (i + 1) << SecondTTPD->isParameterPack(); 10854 ParameterMismatch = true; 10855 break; 10856 } 10857 } 10858 10859 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10860 isa<TemplateTemplateParmDecl>(SecondParam)) { 10861 TemplateTemplateParmDecl *FirstTTPD = 10862 cast<TemplateTemplateParmDecl>(FirstParam); 10863 TemplateTemplateParmDecl *SecondTTPD = 10864 cast<TemplateTemplateParmDecl>(SecondParam); 10865 10866 TemplateParameterList *FirstTPL = 10867 FirstTTPD->getTemplateParameters(); 10868 TemplateParameterList *SecondTPL = 10869 SecondTTPD->getTemplateParameters(); 10870 10871 auto ComputeTemplateParameterListODRHash = 10872 [](const TemplateParameterList *TPL) { 10873 assert(TPL); 10874 ODRHash Hasher; 10875 Hasher.AddTemplateParameterList(TPL); 10876 return Hasher.CalculateHash(); 10877 }; 10878 10879 if (ComputeTemplateParameterListODRHash(FirstTPL) != 10880 ComputeTemplateParameterListODRHash(SecondTPL)) { 10881 DiagTemplateError(FunctionTemplateParameterDifferentType) 10882 << (i + 1); 10883 DiagTemplateNote(FunctionTemplateParameterDifferentType) 10884 << (i + 1); 10885 ParameterMismatch = true; 10886 break; 10887 } 10888 10889 bool HasFirstDefaultArgument = 10890 FirstTTPD->hasDefaultArgument() && 10891 !FirstTTPD->defaultArgumentWasInherited(); 10892 bool HasSecondDefaultArgument = 10893 SecondTTPD->hasDefaultArgument() && 10894 !SecondTTPD->defaultArgumentWasInherited(); 10895 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10896 DiagTemplateError(FunctionTemplateParameterSingleDefaultArgument) 10897 << (i + 1) << HasFirstDefaultArgument; 10898 DiagTemplateNote(FunctionTemplateParameterSingleDefaultArgument) 10899 << (i + 1) << HasSecondDefaultArgument; 10900 ParameterMismatch = true; 10901 break; 10902 } 10903 10904 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10905 TemplateArgument FirstTA = 10906 FirstTTPD->getDefaultArgument().getArgument(); 10907 TemplateArgument SecondTA = 10908 SecondTTPD->getDefaultArgument().getArgument(); 10909 if (computeODRHash(FirstTA) != computeODRHash(SecondTA)) { 10910 DiagTemplateError( 10911 FunctionTemplateParameterDifferentDefaultArgument) 10912 << (i + 1) << FirstTA; 10913 DiagTemplateNote( 10914 FunctionTemplateParameterDifferentDefaultArgument) 10915 << (i + 1) << SecondTA; 10916 ParameterMismatch = true; 10917 break; 10918 } 10919 } 10920 10921 if (FirstTTPD->isParameterPack() != 10922 SecondTTPD->isParameterPack()) { 10923 DiagTemplateError(FunctionTemplatePackParameter) 10924 << (i + 1) << FirstTTPD->isParameterPack(); 10925 DiagTemplateNote(FunctionTemplatePackParameter) 10926 << (i + 1) << SecondTTPD->isParameterPack(); 10927 ParameterMismatch = true; 10928 break; 10929 } 10930 } 10931 10932 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 10933 isa<NonTypeTemplateParmDecl>(SecondParam)) { 10934 NonTypeTemplateParmDecl *FirstNTTPD = 10935 cast<NonTypeTemplateParmDecl>(FirstParam); 10936 NonTypeTemplateParmDecl *SecondNTTPD = 10937 cast<NonTypeTemplateParmDecl>(SecondParam); 10938 10939 QualType FirstType = FirstNTTPD->getType(); 10940 QualType SecondType = SecondNTTPD->getType(); 10941 if (computeODRHash(FirstType) != computeODRHash(SecondType)) { 10942 DiagTemplateError(FunctionTemplateParameterDifferentType) 10943 << (i + 1); 10944 DiagTemplateNote(FunctionTemplateParameterDifferentType) 10945 << (i + 1); 10946 ParameterMismatch = true; 10947 break; 10948 } 10949 10950 bool HasFirstDefaultArgument = 10951 FirstNTTPD->hasDefaultArgument() && 10952 !FirstNTTPD->defaultArgumentWasInherited(); 10953 bool HasSecondDefaultArgument = 10954 SecondNTTPD->hasDefaultArgument() && 10955 !SecondNTTPD->defaultArgumentWasInherited(); 10956 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10957 DiagTemplateError(FunctionTemplateParameterSingleDefaultArgument) 10958 << (i + 1) << HasFirstDefaultArgument; 10959 DiagTemplateNote(FunctionTemplateParameterSingleDefaultArgument) 10960 << (i + 1) << HasSecondDefaultArgument; 10961 ParameterMismatch = true; 10962 break; 10963 } 10964 10965 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10966 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 10967 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 10968 if (computeODRHash(FirstDefaultArgument) != 10969 computeODRHash(SecondDefaultArgument)) { 10970 DiagTemplateError( 10971 FunctionTemplateParameterDifferentDefaultArgument) 10972 << (i + 1) << FirstDefaultArgument; 10973 DiagTemplateNote( 10974 FunctionTemplateParameterDifferentDefaultArgument) 10975 << (i + 1) << SecondDefaultArgument; 10976 ParameterMismatch = true; 10977 break; 10978 } 10979 } 10980 10981 if (FirstNTTPD->isParameterPack() != 10982 SecondNTTPD->isParameterPack()) { 10983 DiagTemplateError(FunctionTemplatePackParameter) 10984 << (i + 1) << FirstNTTPD->isParameterPack(); 10985 DiagTemplateNote(FunctionTemplatePackParameter) 10986 << (i + 1) << SecondNTTPD->isParameterPack(); 10987 ParameterMismatch = true; 10988 break; 10989 } 10990 } 10991 } 10992 10993 if (ParameterMismatch) { 10994 Diagnosed = true; 10995 break; 10996 } 10997 10998 break; 10999 } 11000 } 11001 11002 if (Diagnosed) 11003 continue; 11004 11005 Diag(FirstDecl->getLocation(), 11006 diag::err_module_odr_violation_mismatch_decl_unknown) 11007 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11008 << FirstDecl->getSourceRange(); 11009 Diag(SecondDecl->getLocation(), 11010 diag::note_module_odr_violation_mismatch_decl_unknown) 11011 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11012 Diagnosed = true; 11013 } 11014 11015 if (!Diagnosed) { 11016 // All definitions are updates to the same declaration. This happens if a 11017 // module instantiates the declaration of a class template specialization 11018 // and two or more other modules instantiate its definition. 11019 // 11020 // FIXME: Indicate which modules had instantiations of this definition. 11021 // FIXME: How can this even happen? 11022 Diag(Merge.first->getLocation(), 11023 diag::err_module_odr_violation_different_instantiations) 11024 << Merge.first; 11025 } 11026 } 11027 11028 // Issue ODR failures diagnostics for functions. 11029 for (auto &Merge : FunctionOdrMergeFailures) { 11030 enum ODRFunctionDifference { 11031 ReturnType, 11032 ParameterName, 11033 ParameterType, 11034 ParameterSingleDefaultArgument, 11035 ParameterDifferentDefaultArgument, 11036 FunctionBody, 11037 }; 11038 11039 FunctionDecl *FirstFunction = Merge.first; 11040 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11041 11042 bool Diagnosed = false; 11043 for (auto &SecondFunction : Merge.second) { 11044 11045 if (FirstFunction == SecondFunction) 11046 continue; 11047 11048 std::string SecondModule = 11049 getOwningModuleNameForDiagnostic(SecondFunction); 11050 11051 auto ODRDiagError = [FirstFunction, &FirstModule, 11052 this](SourceLocation Loc, SourceRange Range, 11053 ODRFunctionDifference DiffType) { 11054 return Diag(Loc, diag::err_module_odr_violation_function) 11055 << FirstFunction << FirstModule.empty() << FirstModule << Range 11056 << DiffType; 11057 }; 11058 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11059 SourceRange Range, 11060 ODRFunctionDifference DiffType) { 11061 return Diag(Loc, diag::note_module_odr_violation_function) 11062 << SecondModule << Range << DiffType; 11063 }; 11064 11065 if (computeODRHash(FirstFunction->getReturnType()) != 11066 computeODRHash(SecondFunction->getReturnType())) { 11067 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11068 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11069 << FirstFunction->getReturnType(); 11070 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11071 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11072 << SecondFunction->getReturnType(); 11073 Diagnosed = true; 11074 break; 11075 } 11076 11077 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11078 "Merged functions with different number of parameters"); 11079 11080 size_t ParamSize = FirstFunction->param_size(); 11081 bool ParameterMismatch = false; 11082 for (unsigned I = 0; I < ParamSize; ++I) { 11083 const ParmVarDecl *FirstParam = FirstFunction->getParamDecl(I); 11084 const ParmVarDecl *SecondParam = SecondFunction->getParamDecl(I); 11085 11086 assert(getContext().hasSameType(FirstParam->getType(), 11087 SecondParam->getType()) && 11088 "Merged function has different parameter types."); 11089 11090 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11091 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11092 ParameterName) 11093 << I + 1 << FirstParam->getDeclName(); 11094 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11095 ParameterName) 11096 << I + 1 << SecondParam->getDeclName(); 11097 ParameterMismatch = true; 11098 break; 11099 }; 11100 11101 QualType FirstParamType = FirstParam->getType(); 11102 QualType SecondParamType = SecondParam->getType(); 11103 if (FirstParamType != SecondParamType && 11104 computeODRHash(FirstParamType) != computeODRHash(SecondParamType)) { 11105 if (const DecayedType *ParamDecayedType = 11106 FirstParamType->getAs<DecayedType>()) { 11107 ODRDiagError(FirstParam->getLocation(), 11108 FirstParam->getSourceRange(), ParameterType) 11109 << (I + 1) << FirstParamType << true 11110 << ParamDecayedType->getOriginalType(); 11111 } else { 11112 ODRDiagError(FirstParam->getLocation(), 11113 FirstParam->getSourceRange(), ParameterType) 11114 << (I + 1) << FirstParamType << false; 11115 } 11116 11117 if (const DecayedType *ParamDecayedType = 11118 SecondParamType->getAs<DecayedType>()) { 11119 ODRDiagNote(SecondParam->getLocation(), 11120 SecondParam->getSourceRange(), ParameterType) 11121 << (I + 1) << SecondParamType << true 11122 << ParamDecayedType->getOriginalType(); 11123 } else { 11124 ODRDiagNote(SecondParam->getLocation(), 11125 SecondParam->getSourceRange(), ParameterType) 11126 << (I + 1) << SecondParamType << false; 11127 } 11128 ParameterMismatch = true; 11129 break; 11130 } 11131 11132 const Expr *FirstInit = FirstParam->getInit(); 11133 const Expr *SecondInit = SecondParam->getInit(); 11134 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11135 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11136 ParameterSingleDefaultArgument) 11137 << (I + 1) << (FirstInit == nullptr) 11138 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11139 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11140 ParameterSingleDefaultArgument) 11141 << (I + 1) << (SecondInit == nullptr) 11142 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11143 ParameterMismatch = true; 11144 break; 11145 } 11146 11147 if (FirstInit && SecondInit && 11148 computeODRHash(FirstInit) != computeODRHash(SecondInit)) { 11149 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11150 ParameterDifferentDefaultArgument) 11151 << (I + 1) << FirstInit->getSourceRange(); 11152 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11153 ParameterDifferentDefaultArgument) 11154 << (I + 1) << SecondInit->getSourceRange(); 11155 ParameterMismatch = true; 11156 break; 11157 } 11158 11159 assert(computeODRHash(FirstParam) == computeODRHash(SecondParam) && 11160 "Undiagnosed parameter difference."); 11161 } 11162 11163 if (ParameterMismatch) { 11164 Diagnosed = true; 11165 break; 11166 } 11167 11168 // If no error has been generated before now, assume the problem is in 11169 // the body and generate a message. 11170 ODRDiagError(FirstFunction->getLocation(), 11171 FirstFunction->getSourceRange(), FunctionBody); 11172 ODRDiagNote(SecondFunction->getLocation(), 11173 SecondFunction->getSourceRange(), FunctionBody); 11174 Diagnosed = true; 11175 break; 11176 } 11177 (void)Diagnosed; 11178 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11179 } 11180 11181 // Issue ODR failures diagnostics for enums. 11182 for (auto &Merge : EnumOdrMergeFailures) { 11183 enum ODREnumDifference { 11184 SingleScopedEnum, 11185 EnumTagKeywordMismatch, 11186 SingleSpecifiedType, 11187 DifferentSpecifiedTypes, 11188 DifferentNumberEnumConstants, 11189 EnumConstantName, 11190 EnumConstantSingleInitializer, 11191 EnumConstantDifferentInitializer, 11192 }; 11193 11194 // If we've already pointed out a specific problem with this enum, don't 11195 // bother issuing a general "something's different" diagnostic. 11196 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11197 continue; 11198 11199 EnumDecl *FirstEnum = Merge.first; 11200 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11201 11202 using DeclHashes = 11203 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11204 auto PopulateHashes = [FirstEnum](DeclHashes &Hashes, EnumDecl *Enum) { 11205 for (auto *D : Enum->decls()) { 11206 // Due to decl merging, the first EnumDecl is the parent of 11207 // Decls in both records. 11208 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11209 continue; 11210 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11211 Hashes.emplace_back(cast<EnumConstantDecl>(D), computeODRHash(D)); 11212 } 11213 }; 11214 DeclHashes FirstHashes; 11215 PopulateHashes(FirstHashes, FirstEnum); 11216 bool Diagnosed = false; 11217 for (auto &SecondEnum : Merge.second) { 11218 11219 if (FirstEnum == SecondEnum) 11220 continue; 11221 11222 std::string SecondModule = 11223 getOwningModuleNameForDiagnostic(SecondEnum); 11224 11225 auto ODRDiagError = [FirstEnum, &FirstModule, 11226 this](const auto *DiagAnchor, 11227 ODREnumDifference DiffType) { 11228 return Diag(DiagAnchor->getLocation(), 11229 diag::err_module_odr_violation_enum) 11230 << FirstEnum << FirstModule.empty() << FirstModule 11231 << DiagAnchor->getSourceRange() << DiffType; 11232 }; 11233 auto ODRDiagNote = [&SecondModule, this](const auto *DiagAnchor, 11234 ODREnumDifference DiffType) { 11235 return Diag(DiagAnchor->getLocation(), 11236 diag::note_module_odr_violation_enum) 11237 << SecondModule << DiagAnchor->getSourceRange() << DiffType; 11238 }; 11239 11240 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11241 ODRDiagError(FirstEnum, SingleScopedEnum) << FirstEnum->isScoped(); 11242 ODRDiagNote(SecondEnum, SingleScopedEnum) << SecondEnum->isScoped(); 11243 Diagnosed = true; 11244 continue; 11245 } 11246 11247 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11248 if (FirstEnum->isScopedUsingClassTag() != 11249 SecondEnum->isScopedUsingClassTag()) { 11250 ODRDiagError(FirstEnum, EnumTagKeywordMismatch) 11251 << FirstEnum->isScopedUsingClassTag(); 11252 ODRDiagNote(SecondEnum, EnumTagKeywordMismatch) 11253 << SecondEnum->isScopedUsingClassTag(); 11254 Diagnosed = true; 11255 continue; 11256 } 11257 } 11258 11259 QualType FirstUnderlyingType = 11260 FirstEnum->getIntegerTypeSourceInfo() 11261 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11262 : QualType(); 11263 QualType SecondUnderlyingType = 11264 SecondEnum->getIntegerTypeSourceInfo() 11265 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11266 : QualType(); 11267 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11268 ODRDiagError(FirstEnum, SingleSpecifiedType) 11269 << !FirstUnderlyingType.isNull(); 11270 ODRDiagNote(SecondEnum, SingleSpecifiedType) 11271 << !SecondUnderlyingType.isNull(); 11272 Diagnosed = true; 11273 continue; 11274 } 11275 11276 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11277 if (computeODRHash(FirstUnderlyingType) != 11278 computeODRHash(SecondUnderlyingType)) { 11279 ODRDiagError(FirstEnum, DifferentSpecifiedTypes) 11280 << FirstUnderlyingType; 11281 ODRDiagNote(SecondEnum, DifferentSpecifiedTypes) 11282 << SecondUnderlyingType; 11283 Diagnosed = true; 11284 continue; 11285 } 11286 } 11287 11288 DeclHashes SecondHashes; 11289 PopulateHashes(SecondHashes, SecondEnum); 11290 11291 if (FirstHashes.size() != SecondHashes.size()) { 11292 ODRDiagError(FirstEnum, DifferentNumberEnumConstants) 11293 << (int)FirstHashes.size(); 11294 ODRDiagNote(SecondEnum, DifferentNumberEnumConstants) 11295 << (int)SecondHashes.size(); 11296 Diagnosed = true; 11297 continue; 11298 } 11299 11300 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11301 if (FirstHashes[I].second == SecondHashes[I].second) 11302 continue; 11303 const EnumConstantDecl *FirstConstant = FirstHashes[I].first; 11304 const EnumConstantDecl *SecondConstant = SecondHashes[I].first; 11305 11306 if (FirstConstant->getDeclName() != SecondConstant->getDeclName()) { 11307 11308 ODRDiagError(FirstConstant, EnumConstantName) 11309 << I + 1 << FirstConstant; 11310 ODRDiagNote(SecondConstant, EnumConstantName) 11311 << I + 1 << SecondConstant; 11312 Diagnosed = true; 11313 break; 11314 } 11315 11316 const Expr *FirstInit = FirstConstant->getInitExpr(); 11317 const Expr *SecondInit = SecondConstant->getInitExpr(); 11318 if (!FirstInit && !SecondInit) 11319 continue; 11320 11321 if (!FirstInit || !SecondInit) { 11322 ODRDiagError(FirstConstant, EnumConstantSingleInitializer) 11323 << I + 1 << FirstConstant << (FirstInit != nullptr); 11324 ODRDiagNote(SecondConstant, EnumConstantSingleInitializer) 11325 << I + 1 << SecondConstant << (SecondInit != nullptr); 11326 Diagnosed = true; 11327 break; 11328 } 11329 11330 if (computeODRHash(FirstInit) != computeODRHash(SecondInit)) { 11331 ODRDiagError(FirstConstant, EnumConstantDifferentInitializer) 11332 << I + 1 << FirstConstant; 11333 ODRDiagNote(SecondConstant, EnumConstantDifferentInitializer) 11334 << I + 1 << SecondConstant; 11335 Diagnosed = true; 11336 break; 11337 } 11338 } 11339 } 11340 11341 (void)Diagnosed; 11342 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11343 } 11344 } 11345 11346 void ASTReader::StartedDeserializing() { 11347 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11348 ReadTimer->startTimer(); 11349 } 11350 11351 void ASTReader::FinishedDeserializing() { 11352 assert(NumCurrentElementsDeserializing && 11353 "FinishedDeserializing not paired with StartedDeserializing"); 11354 if (NumCurrentElementsDeserializing == 1) { 11355 // We decrease NumCurrentElementsDeserializing only after pending actions 11356 // are finished, to avoid recursively re-calling finishPendingActions(). 11357 finishPendingActions(); 11358 } 11359 --NumCurrentElementsDeserializing; 11360 11361 if (NumCurrentElementsDeserializing == 0) { 11362 // Propagate exception specification and deduced type updates along 11363 // redeclaration chains. 11364 // 11365 // We do this now rather than in finishPendingActions because we want to 11366 // be able to walk the complete redeclaration chains of the updated decls. 11367 while (!PendingExceptionSpecUpdates.empty() || 11368 !PendingDeducedTypeUpdates.empty()) { 11369 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11370 PendingExceptionSpecUpdates.clear(); 11371 for (auto Update : ESUpdates) { 11372 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11373 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11374 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11375 if (auto *Listener = getContext().getASTMutationListener()) 11376 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11377 for (auto *Redecl : Update.second->redecls()) 11378 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11379 } 11380 11381 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11382 PendingDeducedTypeUpdates.clear(); 11383 for (auto Update : DTUpdates) { 11384 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11385 // FIXME: If the return type is already deduced, check that it matches. 11386 getContext().adjustDeducedFunctionResultType(Update.first, 11387 Update.second); 11388 } 11389 } 11390 11391 if (ReadTimer) 11392 ReadTimer->stopTimer(); 11393 11394 diagnoseOdrViolations(); 11395 11396 // We are not in recursive loading, so it's safe to pass the "interesting" 11397 // decls to the consumer. 11398 if (Consumer) 11399 PassInterestingDeclsToConsumer(); 11400 } 11401 } 11402 11403 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11404 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11405 // Remove any fake results before adding any real ones. 11406 auto It = PendingFakeLookupResults.find(II); 11407 if (It != PendingFakeLookupResults.end()) { 11408 for (auto *ND : It->second) 11409 SemaObj->IdResolver.RemoveDecl(ND); 11410 // FIXME: this works around module+PCH performance issue. 11411 // Rather than erase the result from the map, which is O(n), just clear 11412 // the vector of NamedDecls. 11413 It->second.clear(); 11414 } 11415 } 11416 11417 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11418 SemaObj->TUScope->AddDecl(D); 11419 } else if (SemaObj->TUScope) { 11420 // Adding the decl to IdResolver may have failed because it was already in 11421 // (even though it was not added in scope). If it is already in, make sure 11422 // it gets in the scope as well. 11423 if (std::find(SemaObj->IdResolver.begin(Name), 11424 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11425 SemaObj->TUScope->AddDecl(D); 11426 } 11427 } 11428 11429 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11430 ASTContext *Context, 11431 const PCHContainerReader &PCHContainerRdr, 11432 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11433 StringRef isysroot, 11434 DisableValidationForModuleKind DisableValidationKind, 11435 bool AllowASTWithCompilerErrors, 11436 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11437 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11438 std::unique_ptr<llvm::Timer> ReadTimer) 11439 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH) 11440 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11441 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11442 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11443 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11444 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11445 PCHContainerRdr, PP.getHeaderSearchInfo()), 11446 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11447 DisableValidationKind(DisableValidationKind), 11448 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11449 AllowConfigurationMismatch(AllowConfigurationMismatch), 11450 ValidateSystemInputs(ValidateSystemInputs), 11451 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11452 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11453 SourceMgr.setExternalSLocEntrySource(this); 11454 11455 for (const auto &Ext : Extensions) { 11456 auto BlockName = Ext->getExtensionMetadata().BlockName; 11457 auto Known = ModuleFileExtensions.find(BlockName); 11458 if (Known != ModuleFileExtensions.end()) { 11459 Diags.Report(diag::warn_duplicate_module_file_extension) 11460 << BlockName; 11461 continue; 11462 } 11463 11464 ModuleFileExtensions.insert({BlockName, Ext}); 11465 } 11466 } 11467 11468 ASTReader::~ASTReader() { 11469 if (OwnsDeserializationListener) 11470 delete DeserializationListener; 11471 } 11472 11473 IdentifierResolver &ASTReader::getIdResolver() { 11474 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11475 } 11476 11477 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11478 unsigned AbbrevID) { 11479 Idx = 0; 11480 Record.clear(); 11481 return Cursor.readRecord(AbbrevID, Record); 11482 } 11483 //===----------------------------------------------------------------------===// 11484 //// OMPClauseReader implementation 11485 ////===----------------------------------------------------------------------===// 11486 11487 // This has to be in namespace clang because it's friended by all 11488 // of the OMP clauses. 11489 namespace clang { 11490 11491 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11492 ASTRecordReader &Record; 11493 ASTContext &Context; 11494 11495 public: 11496 OMPClauseReader(ASTRecordReader &Record) 11497 : Record(Record), Context(Record.getContext()) {} 11498 #define GEN_CLANG_CLAUSE_CLASS 11499 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11500 #include "llvm/Frontend/OpenMP/OMP.inc" 11501 OMPClause *readClause(); 11502 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11503 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11504 }; 11505 11506 } // end namespace clang 11507 11508 OMPClause *ASTRecordReader::readOMPClause() { 11509 return OMPClauseReader(*this).readClause(); 11510 } 11511 11512 OMPClause *OMPClauseReader::readClause() { 11513 OMPClause *C = nullptr; 11514 switch (llvm::omp::Clause(Record.readInt())) { 11515 case llvm::omp::OMPC_if: 11516 C = new (Context) OMPIfClause(); 11517 break; 11518 case llvm::omp::OMPC_final: 11519 C = new (Context) OMPFinalClause(); 11520 break; 11521 case llvm::omp::OMPC_num_threads: 11522 C = new (Context) OMPNumThreadsClause(); 11523 break; 11524 case llvm::omp::OMPC_safelen: 11525 C = new (Context) OMPSafelenClause(); 11526 break; 11527 case llvm::omp::OMPC_simdlen: 11528 C = new (Context) OMPSimdlenClause(); 11529 break; 11530 case llvm::omp::OMPC_sizes: { 11531 unsigned NumSizes = Record.readInt(); 11532 C = OMPSizesClause::CreateEmpty(Context, NumSizes); 11533 break; 11534 } 11535 case llvm::omp::OMPC_full: 11536 C = OMPFullClause::CreateEmpty(Context); 11537 break; 11538 case llvm::omp::OMPC_partial: 11539 C = OMPPartialClause::CreateEmpty(Context); 11540 break; 11541 case llvm::omp::OMPC_allocator: 11542 C = new (Context) OMPAllocatorClause(); 11543 break; 11544 case llvm::omp::OMPC_collapse: 11545 C = new (Context) OMPCollapseClause(); 11546 break; 11547 case llvm::omp::OMPC_default: 11548 C = new (Context) OMPDefaultClause(); 11549 break; 11550 case llvm::omp::OMPC_proc_bind: 11551 C = new (Context) OMPProcBindClause(); 11552 break; 11553 case llvm::omp::OMPC_schedule: 11554 C = new (Context) OMPScheduleClause(); 11555 break; 11556 case llvm::omp::OMPC_ordered: 11557 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11558 break; 11559 case llvm::omp::OMPC_nowait: 11560 C = new (Context) OMPNowaitClause(); 11561 break; 11562 case llvm::omp::OMPC_untied: 11563 C = new (Context) OMPUntiedClause(); 11564 break; 11565 case llvm::omp::OMPC_mergeable: 11566 C = new (Context) OMPMergeableClause(); 11567 break; 11568 case llvm::omp::OMPC_read: 11569 C = new (Context) OMPReadClause(); 11570 break; 11571 case llvm::omp::OMPC_write: 11572 C = new (Context) OMPWriteClause(); 11573 break; 11574 case llvm::omp::OMPC_update: 11575 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11576 break; 11577 case llvm::omp::OMPC_capture: 11578 C = new (Context) OMPCaptureClause(); 11579 break; 11580 case llvm::omp::OMPC_compare: 11581 C = new (Context) OMPCompareClause(); 11582 break; 11583 case llvm::omp::OMPC_seq_cst: 11584 C = new (Context) OMPSeqCstClause(); 11585 break; 11586 case llvm::omp::OMPC_acq_rel: 11587 C = new (Context) OMPAcqRelClause(); 11588 break; 11589 case llvm::omp::OMPC_acquire: 11590 C = new (Context) OMPAcquireClause(); 11591 break; 11592 case llvm::omp::OMPC_release: 11593 C = new (Context) OMPReleaseClause(); 11594 break; 11595 case llvm::omp::OMPC_relaxed: 11596 C = new (Context) OMPRelaxedClause(); 11597 break; 11598 case llvm::omp::OMPC_threads: 11599 C = new (Context) OMPThreadsClause(); 11600 break; 11601 case llvm::omp::OMPC_simd: 11602 C = new (Context) OMPSIMDClause(); 11603 break; 11604 case llvm::omp::OMPC_nogroup: 11605 C = new (Context) OMPNogroupClause(); 11606 break; 11607 case llvm::omp::OMPC_unified_address: 11608 C = new (Context) OMPUnifiedAddressClause(); 11609 break; 11610 case llvm::omp::OMPC_unified_shared_memory: 11611 C = new (Context) OMPUnifiedSharedMemoryClause(); 11612 break; 11613 case llvm::omp::OMPC_reverse_offload: 11614 C = new (Context) OMPReverseOffloadClause(); 11615 break; 11616 case llvm::omp::OMPC_dynamic_allocators: 11617 C = new (Context) OMPDynamicAllocatorsClause(); 11618 break; 11619 case llvm::omp::OMPC_atomic_default_mem_order: 11620 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11621 break; 11622 case llvm::omp::OMPC_private: 11623 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11624 break; 11625 case llvm::omp::OMPC_firstprivate: 11626 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11627 break; 11628 case llvm::omp::OMPC_lastprivate: 11629 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11630 break; 11631 case llvm::omp::OMPC_shared: 11632 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11633 break; 11634 case llvm::omp::OMPC_reduction: { 11635 unsigned N = Record.readInt(); 11636 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11637 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11638 break; 11639 } 11640 case llvm::omp::OMPC_task_reduction: 11641 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11642 break; 11643 case llvm::omp::OMPC_in_reduction: 11644 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11645 break; 11646 case llvm::omp::OMPC_linear: 11647 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11648 break; 11649 case llvm::omp::OMPC_aligned: 11650 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11651 break; 11652 case llvm::omp::OMPC_copyin: 11653 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11654 break; 11655 case llvm::omp::OMPC_copyprivate: 11656 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11657 break; 11658 case llvm::omp::OMPC_flush: 11659 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11660 break; 11661 case llvm::omp::OMPC_depobj: 11662 C = OMPDepobjClause::CreateEmpty(Context); 11663 break; 11664 case llvm::omp::OMPC_depend: { 11665 unsigned NumVars = Record.readInt(); 11666 unsigned NumLoops = Record.readInt(); 11667 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11668 break; 11669 } 11670 case llvm::omp::OMPC_device: 11671 C = new (Context) OMPDeviceClause(); 11672 break; 11673 case llvm::omp::OMPC_map: { 11674 OMPMappableExprListSizeTy Sizes; 11675 Sizes.NumVars = Record.readInt(); 11676 Sizes.NumUniqueDeclarations = Record.readInt(); 11677 Sizes.NumComponentLists = Record.readInt(); 11678 Sizes.NumComponents = Record.readInt(); 11679 C = OMPMapClause::CreateEmpty(Context, Sizes); 11680 break; 11681 } 11682 case llvm::omp::OMPC_num_teams: 11683 C = new (Context) OMPNumTeamsClause(); 11684 break; 11685 case llvm::omp::OMPC_thread_limit: 11686 C = new (Context) OMPThreadLimitClause(); 11687 break; 11688 case llvm::omp::OMPC_priority: 11689 C = new (Context) OMPPriorityClause(); 11690 break; 11691 case llvm::omp::OMPC_grainsize: 11692 C = new (Context) OMPGrainsizeClause(); 11693 break; 11694 case llvm::omp::OMPC_num_tasks: 11695 C = new (Context) OMPNumTasksClause(); 11696 break; 11697 case llvm::omp::OMPC_hint: 11698 C = new (Context) OMPHintClause(); 11699 break; 11700 case llvm::omp::OMPC_dist_schedule: 11701 C = new (Context) OMPDistScheduleClause(); 11702 break; 11703 case llvm::omp::OMPC_defaultmap: 11704 C = new (Context) OMPDefaultmapClause(); 11705 break; 11706 case llvm::omp::OMPC_to: { 11707 OMPMappableExprListSizeTy Sizes; 11708 Sizes.NumVars = Record.readInt(); 11709 Sizes.NumUniqueDeclarations = Record.readInt(); 11710 Sizes.NumComponentLists = Record.readInt(); 11711 Sizes.NumComponents = Record.readInt(); 11712 C = OMPToClause::CreateEmpty(Context, Sizes); 11713 break; 11714 } 11715 case llvm::omp::OMPC_from: { 11716 OMPMappableExprListSizeTy Sizes; 11717 Sizes.NumVars = Record.readInt(); 11718 Sizes.NumUniqueDeclarations = Record.readInt(); 11719 Sizes.NumComponentLists = Record.readInt(); 11720 Sizes.NumComponents = Record.readInt(); 11721 C = OMPFromClause::CreateEmpty(Context, Sizes); 11722 break; 11723 } 11724 case llvm::omp::OMPC_use_device_ptr: { 11725 OMPMappableExprListSizeTy Sizes; 11726 Sizes.NumVars = Record.readInt(); 11727 Sizes.NumUniqueDeclarations = Record.readInt(); 11728 Sizes.NumComponentLists = Record.readInt(); 11729 Sizes.NumComponents = Record.readInt(); 11730 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11731 break; 11732 } 11733 case llvm::omp::OMPC_use_device_addr: { 11734 OMPMappableExprListSizeTy Sizes; 11735 Sizes.NumVars = Record.readInt(); 11736 Sizes.NumUniqueDeclarations = Record.readInt(); 11737 Sizes.NumComponentLists = Record.readInt(); 11738 Sizes.NumComponents = Record.readInt(); 11739 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11740 break; 11741 } 11742 case llvm::omp::OMPC_is_device_ptr: { 11743 OMPMappableExprListSizeTy Sizes; 11744 Sizes.NumVars = Record.readInt(); 11745 Sizes.NumUniqueDeclarations = Record.readInt(); 11746 Sizes.NumComponentLists = Record.readInt(); 11747 Sizes.NumComponents = Record.readInt(); 11748 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11749 break; 11750 } 11751 case llvm::omp::OMPC_has_device_addr: { 11752 OMPMappableExprListSizeTy Sizes; 11753 Sizes.NumVars = Record.readInt(); 11754 Sizes.NumUniqueDeclarations = Record.readInt(); 11755 Sizes.NumComponentLists = Record.readInt(); 11756 Sizes.NumComponents = Record.readInt(); 11757 C = OMPHasDeviceAddrClause::CreateEmpty(Context, Sizes); 11758 break; 11759 } 11760 case llvm::omp::OMPC_allocate: 11761 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11762 break; 11763 case llvm::omp::OMPC_nontemporal: 11764 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11765 break; 11766 case llvm::omp::OMPC_inclusive: 11767 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11768 break; 11769 case llvm::omp::OMPC_exclusive: 11770 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11771 break; 11772 case llvm::omp::OMPC_order: 11773 C = new (Context) OMPOrderClause(); 11774 break; 11775 case llvm::omp::OMPC_init: 11776 C = OMPInitClause::CreateEmpty(Context, Record.readInt()); 11777 break; 11778 case llvm::omp::OMPC_use: 11779 C = new (Context) OMPUseClause(); 11780 break; 11781 case llvm::omp::OMPC_destroy: 11782 C = new (Context) OMPDestroyClause(); 11783 break; 11784 case llvm::omp::OMPC_novariants: 11785 C = new (Context) OMPNovariantsClause(); 11786 break; 11787 case llvm::omp::OMPC_nocontext: 11788 C = new (Context) OMPNocontextClause(); 11789 break; 11790 case llvm::omp::OMPC_detach: 11791 C = new (Context) OMPDetachClause(); 11792 break; 11793 case llvm::omp::OMPC_uses_allocators: 11794 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11795 break; 11796 case llvm::omp::OMPC_affinity: 11797 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11798 break; 11799 case llvm::omp::OMPC_filter: 11800 C = new (Context) OMPFilterClause(); 11801 break; 11802 case llvm::omp::OMPC_bind: 11803 C = OMPBindClause::CreateEmpty(Context); 11804 break; 11805 case llvm::omp::OMPC_align: 11806 C = new (Context) OMPAlignClause(); 11807 break; 11808 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11809 case llvm::omp::Enum: \ 11810 break; 11811 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11812 default: 11813 break; 11814 } 11815 assert(C && "Unknown OMPClause type"); 11816 11817 Visit(C); 11818 C->setLocStart(Record.readSourceLocation()); 11819 C->setLocEnd(Record.readSourceLocation()); 11820 11821 return C; 11822 } 11823 11824 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11825 C->setPreInitStmt(Record.readSubStmt(), 11826 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11827 } 11828 11829 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11830 VisitOMPClauseWithPreInit(C); 11831 C->setPostUpdateExpr(Record.readSubExpr()); 11832 } 11833 11834 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 11835 VisitOMPClauseWithPreInit(C); 11836 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 11837 C->setNameModifierLoc(Record.readSourceLocation()); 11838 C->setColonLoc(Record.readSourceLocation()); 11839 C->setCondition(Record.readSubExpr()); 11840 C->setLParenLoc(Record.readSourceLocation()); 11841 } 11842 11843 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 11844 VisitOMPClauseWithPreInit(C); 11845 C->setCondition(Record.readSubExpr()); 11846 C->setLParenLoc(Record.readSourceLocation()); 11847 } 11848 11849 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 11850 VisitOMPClauseWithPreInit(C); 11851 C->setNumThreads(Record.readSubExpr()); 11852 C->setLParenLoc(Record.readSourceLocation()); 11853 } 11854 11855 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 11856 C->setSafelen(Record.readSubExpr()); 11857 C->setLParenLoc(Record.readSourceLocation()); 11858 } 11859 11860 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 11861 C->setSimdlen(Record.readSubExpr()); 11862 C->setLParenLoc(Record.readSourceLocation()); 11863 } 11864 11865 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) { 11866 for (Expr *&E : C->getSizesRefs()) 11867 E = Record.readSubExpr(); 11868 C->setLParenLoc(Record.readSourceLocation()); 11869 } 11870 11871 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {} 11872 11873 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) { 11874 C->setFactor(Record.readSubExpr()); 11875 C->setLParenLoc(Record.readSourceLocation()); 11876 } 11877 11878 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 11879 C->setAllocator(Record.readExpr()); 11880 C->setLParenLoc(Record.readSourceLocation()); 11881 } 11882 11883 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 11884 C->setNumForLoops(Record.readSubExpr()); 11885 C->setLParenLoc(Record.readSourceLocation()); 11886 } 11887 11888 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 11889 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 11890 C->setLParenLoc(Record.readSourceLocation()); 11891 C->setDefaultKindKwLoc(Record.readSourceLocation()); 11892 } 11893 11894 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 11895 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 11896 C->setLParenLoc(Record.readSourceLocation()); 11897 C->setProcBindKindKwLoc(Record.readSourceLocation()); 11898 } 11899 11900 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 11901 VisitOMPClauseWithPreInit(C); 11902 C->setScheduleKind( 11903 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 11904 C->setFirstScheduleModifier( 11905 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 11906 C->setSecondScheduleModifier( 11907 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 11908 C->setChunkSize(Record.readSubExpr()); 11909 C->setLParenLoc(Record.readSourceLocation()); 11910 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 11911 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 11912 C->setScheduleKindLoc(Record.readSourceLocation()); 11913 C->setCommaLoc(Record.readSourceLocation()); 11914 } 11915 11916 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 11917 C->setNumForLoops(Record.readSubExpr()); 11918 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 11919 C->setLoopNumIterations(I, Record.readSubExpr()); 11920 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 11921 C->setLoopCounter(I, Record.readSubExpr()); 11922 C->setLParenLoc(Record.readSourceLocation()); 11923 } 11924 11925 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 11926 C->setEventHandler(Record.readSubExpr()); 11927 C->setLParenLoc(Record.readSourceLocation()); 11928 } 11929 11930 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 11931 11932 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 11933 11934 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 11935 11936 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 11937 11938 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 11939 11940 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 11941 if (C->isExtended()) { 11942 C->setLParenLoc(Record.readSourceLocation()); 11943 C->setArgumentLoc(Record.readSourceLocation()); 11944 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 11945 } 11946 } 11947 11948 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 11949 11950 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {} 11951 11952 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 11953 11954 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 11955 11956 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 11957 11958 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 11959 11960 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 11961 11962 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 11963 11964 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 11965 11966 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 11967 11968 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) { 11969 unsigned NumVars = C->varlist_size(); 11970 SmallVector<Expr *, 16> Vars; 11971 Vars.reserve(NumVars); 11972 for (unsigned I = 0; I != NumVars; ++I) 11973 Vars.push_back(Record.readSubExpr()); 11974 C->setVarRefs(Vars); 11975 C->setIsTarget(Record.readBool()); 11976 C->setIsTargetSync(Record.readBool()); 11977 C->setLParenLoc(Record.readSourceLocation()); 11978 C->setVarLoc(Record.readSourceLocation()); 11979 } 11980 11981 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) { 11982 C->setInteropVar(Record.readSubExpr()); 11983 C->setLParenLoc(Record.readSourceLocation()); 11984 C->setVarLoc(Record.readSourceLocation()); 11985 } 11986 11987 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) { 11988 C->setInteropVar(Record.readSubExpr()); 11989 C->setLParenLoc(Record.readSourceLocation()); 11990 C->setVarLoc(Record.readSourceLocation()); 11991 } 11992 11993 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) { 11994 VisitOMPClauseWithPreInit(C); 11995 C->setCondition(Record.readSubExpr()); 11996 C->setLParenLoc(Record.readSourceLocation()); 11997 } 11998 11999 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) { 12000 VisitOMPClauseWithPreInit(C); 12001 C->setCondition(Record.readSubExpr()); 12002 C->setLParenLoc(Record.readSourceLocation()); 12003 } 12004 12005 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12006 12007 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12008 OMPUnifiedSharedMemoryClause *) {} 12009 12010 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12011 12012 void 12013 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12014 } 12015 12016 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12017 OMPAtomicDefaultMemOrderClause *C) { 12018 C->setAtomicDefaultMemOrderKind( 12019 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12020 C->setLParenLoc(Record.readSourceLocation()); 12021 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12022 } 12023 12024 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12025 C->setLParenLoc(Record.readSourceLocation()); 12026 unsigned NumVars = C->varlist_size(); 12027 SmallVector<Expr *, 16> Vars; 12028 Vars.reserve(NumVars); 12029 for (unsigned i = 0; i != NumVars; ++i) 12030 Vars.push_back(Record.readSubExpr()); 12031 C->setVarRefs(Vars); 12032 Vars.clear(); 12033 for (unsigned i = 0; i != NumVars; ++i) 12034 Vars.push_back(Record.readSubExpr()); 12035 C->setPrivateCopies(Vars); 12036 } 12037 12038 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12039 VisitOMPClauseWithPreInit(C); 12040 C->setLParenLoc(Record.readSourceLocation()); 12041 unsigned NumVars = C->varlist_size(); 12042 SmallVector<Expr *, 16> Vars; 12043 Vars.reserve(NumVars); 12044 for (unsigned i = 0; i != NumVars; ++i) 12045 Vars.push_back(Record.readSubExpr()); 12046 C->setVarRefs(Vars); 12047 Vars.clear(); 12048 for (unsigned i = 0; i != NumVars; ++i) 12049 Vars.push_back(Record.readSubExpr()); 12050 C->setPrivateCopies(Vars); 12051 Vars.clear(); 12052 for (unsigned i = 0; i != NumVars; ++i) 12053 Vars.push_back(Record.readSubExpr()); 12054 C->setInits(Vars); 12055 } 12056 12057 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12058 VisitOMPClauseWithPostUpdate(C); 12059 C->setLParenLoc(Record.readSourceLocation()); 12060 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12061 C->setKindLoc(Record.readSourceLocation()); 12062 C->setColonLoc(Record.readSourceLocation()); 12063 unsigned NumVars = C->varlist_size(); 12064 SmallVector<Expr *, 16> Vars; 12065 Vars.reserve(NumVars); 12066 for (unsigned i = 0; i != NumVars; ++i) 12067 Vars.push_back(Record.readSubExpr()); 12068 C->setVarRefs(Vars); 12069 Vars.clear(); 12070 for (unsigned i = 0; i != NumVars; ++i) 12071 Vars.push_back(Record.readSubExpr()); 12072 C->setPrivateCopies(Vars); 12073 Vars.clear(); 12074 for (unsigned i = 0; i != NumVars; ++i) 12075 Vars.push_back(Record.readSubExpr()); 12076 C->setSourceExprs(Vars); 12077 Vars.clear(); 12078 for (unsigned i = 0; i != NumVars; ++i) 12079 Vars.push_back(Record.readSubExpr()); 12080 C->setDestinationExprs(Vars); 12081 Vars.clear(); 12082 for (unsigned i = 0; i != NumVars; ++i) 12083 Vars.push_back(Record.readSubExpr()); 12084 C->setAssignmentOps(Vars); 12085 } 12086 12087 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12088 C->setLParenLoc(Record.readSourceLocation()); 12089 unsigned NumVars = C->varlist_size(); 12090 SmallVector<Expr *, 16> Vars; 12091 Vars.reserve(NumVars); 12092 for (unsigned i = 0; i != NumVars; ++i) 12093 Vars.push_back(Record.readSubExpr()); 12094 C->setVarRefs(Vars); 12095 } 12096 12097 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12098 VisitOMPClauseWithPostUpdate(C); 12099 C->setLParenLoc(Record.readSourceLocation()); 12100 C->setModifierLoc(Record.readSourceLocation()); 12101 C->setColonLoc(Record.readSourceLocation()); 12102 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12103 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12104 C->setQualifierLoc(NNSL); 12105 C->setNameInfo(DNI); 12106 12107 unsigned NumVars = C->varlist_size(); 12108 SmallVector<Expr *, 16> Vars; 12109 Vars.reserve(NumVars); 12110 for (unsigned i = 0; i != NumVars; ++i) 12111 Vars.push_back(Record.readSubExpr()); 12112 C->setVarRefs(Vars); 12113 Vars.clear(); 12114 for (unsigned i = 0; i != NumVars; ++i) 12115 Vars.push_back(Record.readSubExpr()); 12116 C->setPrivates(Vars); 12117 Vars.clear(); 12118 for (unsigned i = 0; i != NumVars; ++i) 12119 Vars.push_back(Record.readSubExpr()); 12120 C->setLHSExprs(Vars); 12121 Vars.clear(); 12122 for (unsigned i = 0; i != NumVars; ++i) 12123 Vars.push_back(Record.readSubExpr()); 12124 C->setRHSExprs(Vars); 12125 Vars.clear(); 12126 for (unsigned i = 0; i != NumVars; ++i) 12127 Vars.push_back(Record.readSubExpr()); 12128 C->setReductionOps(Vars); 12129 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12130 Vars.clear(); 12131 for (unsigned i = 0; i != NumVars; ++i) 12132 Vars.push_back(Record.readSubExpr()); 12133 C->setInscanCopyOps(Vars); 12134 Vars.clear(); 12135 for (unsigned i = 0; i != NumVars; ++i) 12136 Vars.push_back(Record.readSubExpr()); 12137 C->setInscanCopyArrayTemps(Vars); 12138 Vars.clear(); 12139 for (unsigned i = 0; i != NumVars; ++i) 12140 Vars.push_back(Record.readSubExpr()); 12141 C->setInscanCopyArrayElems(Vars); 12142 } 12143 } 12144 12145 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12146 VisitOMPClauseWithPostUpdate(C); 12147 C->setLParenLoc(Record.readSourceLocation()); 12148 C->setColonLoc(Record.readSourceLocation()); 12149 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12150 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12151 C->setQualifierLoc(NNSL); 12152 C->setNameInfo(DNI); 12153 12154 unsigned NumVars = C->varlist_size(); 12155 SmallVector<Expr *, 16> Vars; 12156 Vars.reserve(NumVars); 12157 for (unsigned I = 0; I != NumVars; ++I) 12158 Vars.push_back(Record.readSubExpr()); 12159 C->setVarRefs(Vars); 12160 Vars.clear(); 12161 for (unsigned I = 0; I != NumVars; ++I) 12162 Vars.push_back(Record.readSubExpr()); 12163 C->setPrivates(Vars); 12164 Vars.clear(); 12165 for (unsigned I = 0; I != NumVars; ++I) 12166 Vars.push_back(Record.readSubExpr()); 12167 C->setLHSExprs(Vars); 12168 Vars.clear(); 12169 for (unsigned I = 0; I != NumVars; ++I) 12170 Vars.push_back(Record.readSubExpr()); 12171 C->setRHSExprs(Vars); 12172 Vars.clear(); 12173 for (unsigned I = 0; I != NumVars; ++I) 12174 Vars.push_back(Record.readSubExpr()); 12175 C->setReductionOps(Vars); 12176 } 12177 12178 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12179 VisitOMPClauseWithPostUpdate(C); 12180 C->setLParenLoc(Record.readSourceLocation()); 12181 C->setColonLoc(Record.readSourceLocation()); 12182 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12183 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12184 C->setQualifierLoc(NNSL); 12185 C->setNameInfo(DNI); 12186 12187 unsigned NumVars = C->varlist_size(); 12188 SmallVector<Expr *, 16> Vars; 12189 Vars.reserve(NumVars); 12190 for (unsigned I = 0; I != NumVars; ++I) 12191 Vars.push_back(Record.readSubExpr()); 12192 C->setVarRefs(Vars); 12193 Vars.clear(); 12194 for (unsigned I = 0; I != NumVars; ++I) 12195 Vars.push_back(Record.readSubExpr()); 12196 C->setPrivates(Vars); 12197 Vars.clear(); 12198 for (unsigned I = 0; I != NumVars; ++I) 12199 Vars.push_back(Record.readSubExpr()); 12200 C->setLHSExprs(Vars); 12201 Vars.clear(); 12202 for (unsigned I = 0; I != NumVars; ++I) 12203 Vars.push_back(Record.readSubExpr()); 12204 C->setRHSExprs(Vars); 12205 Vars.clear(); 12206 for (unsigned I = 0; I != NumVars; ++I) 12207 Vars.push_back(Record.readSubExpr()); 12208 C->setReductionOps(Vars); 12209 Vars.clear(); 12210 for (unsigned I = 0; I != NumVars; ++I) 12211 Vars.push_back(Record.readSubExpr()); 12212 C->setTaskgroupDescriptors(Vars); 12213 } 12214 12215 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12216 VisitOMPClauseWithPostUpdate(C); 12217 C->setLParenLoc(Record.readSourceLocation()); 12218 C->setColonLoc(Record.readSourceLocation()); 12219 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12220 C->setModifierLoc(Record.readSourceLocation()); 12221 unsigned NumVars = C->varlist_size(); 12222 SmallVector<Expr *, 16> Vars; 12223 Vars.reserve(NumVars); 12224 for (unsigned i = 0; i != NumVars; ++i) 12225 Vars.push_back(Record.readSubExpr()); 12226 C->setVarRefs(Vars); 12227 Vars.clear(); 12228 for (unsigned i = 0; i != NumVars; ++i) 12229 Vars.push_back(Record.readSubExpr()); 12230 C->setPrivates(Vars); 12231 Vars.clear(); 12232 for (unsigned i = 0; i != NumVars; ++i) 12233 Vars.push_back(Record.readSubExpr()); 12234 C->setInits(Vars); 12235 Vars.clear(); 12236 for (unsigned i = 0; i != NumVars; ++i) 12237 Vars.push_back(Record.readSubExpr()); 12238 C->setUpdates(Vars); 12239 Vars.clear(); 12240 for (unsigned i = 0; i != NumVars; ++i) 12241 Vars.push_back(Record.readSubExpr()); 12242 C->setFinals(Vars); 12243 C->setStep(Record.readSubExpr()); 12244 C->setCalcStep(Record.readSubExpr()); 12245 Vars.clear(); 12246 for (unsigned I = 0; I != NumVars + 1; ++I) 12247 Vars.push_back(Record.readSubExpr()); 12248 C->setUsedExprs(Vars); 12249 } 12250 12251 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12252 C->setLParenLoc(Record.readSourceLocation()); 12253 C->setColonLoc(Record.readSourceLocation()); 12254 unsigned NumVars = C->varlist_size(); 12255 SmallVector<Expr *, 16> Vars; 12256 Vars.reserve(NumVars); 12257 for (unsigned i = 0; i != NumVars; ++i) 12258 Vars.push_back(Record.readSubExpr()); 12259 C->setVarRefs(Vars); 12260 C->setAlignment(Record.readSubExpr()); 12261 } 12262 12263 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12264 C->setLParenLoc(Record.readSourceLocation()); 12265 unsigned NumVars = C->varlist_size(); 12266 SmallVector<Expr *, 16> Exprs; 12267 Exprs.reserve(NumVars); 12268 for (unsigned i = 0; i != NumVars; ++i) 12269 Exprs.push_back(Record.readSubExpr()); 12270 C->setVarRefs(Exprs); 12271 Exprs.clear(); 12272 for (unsigned i = 0; i != NumVars; ++i) 12273 Exprs.push_back(Record.readSubExpr()); 12274 C->setSourceExprs(Exprs); 12275 Exprs.clear(); 12276 for (unsigned i = 0; i != NumVars; ++i) 12277 Exprs.push_back(Record.readSubExpr()); 12278 C->setDestinationExprs(Exprs); 12279 Exprs.clear(); 12280 for (unsigned i = 0; i != NumVars; ++i) 12281 Exprs.push_back(Record.readSubExpr()); 12282 C->setAssignmentOps(Exprs); 12283 } 12284 12285 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12286 C->setLParenLoc(Record.readSourceLocation()); 12287 unsigned NumVars = C->varlist_size(); 12288 SmallVector<Expr *, 16> Exprs; 12289 Exprs.reserve(NumVars); 12290 for (unsigned i = 0; i != NumVars; ++i) 12291 Exprs.push_back(Record.readSubExpr()); 12292 C->setVarRefs(Exprs); 12293 Exprs.clear(); 12294 for (unsigned i = 0; i != NumVars; ++i) 12295 Exprs.push_back(Record.readSubExpr()); 12296 C->setSourceExprs(Exprs); 12297 Exprs.clear(); 12298 for (unsigned i = 0; i != NumVars; ++i) 12299 Exprs.push_back(Record.readSubExpr()); 12300 C->setDestinationExprs(Exprs); 12301 Exprs.clear(); 12302 for (unsigned i = 0; i != NumVars; ++i) 12303 Exprs.push_back(Record.readSubExpr()); 12304 C->setAssignmentOps(Exprs); 12305 } 12306 12307 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12308 C->setLParenLoc(Record.readSourceLocation()); 12309 unsigned NumVars = C->varlist_size(); 12310 SmallVector<Expr *, 16> Vars; 12311 Vars.reserve(NumVars); 12312 for (unsigned i = 0; i != NumVars; ++i) 12313 Vars.push_back(Record.readSubExpr()); 12314 C->setVarRefs(Vars); 12315 } 12316 12317 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12318 C->setDepobj(Record.readSubExpr()); 12319 C->setLParenLoc(Record.readSourceLocation()); 12320 } 12321 12322 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12323 C->setLParenLoc(Record.readSourceLocation()); 12324 C->setModifier(Record.readSubExpr()); 12325 C->setDependencyKind( 12326 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12327 C->setDependencyLoc(Record.readSourceLocation()); 12328 C->setColonLoc(Record.readSourceLocation()); 12329 C->setOmpAllMemoryLoc(Record.readSourceLocation()); 12330 unsigned NumVars = C->varlist_size(); 12331 SmallVector<Expr *, 16> Vars; 12332 Vars.reserve(NumVars); 12333 for (unsigned I = 0; I != NumVars; ++I) 12334 Vars.push_back(Record.readSubExpr()); 12335 C->setVarRefs(Vars); 12336 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12337 C->setLoopData(I, Record.readSubExpr()); 12338 } 12339 12340 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12341 VisitOMPClauseWithPreInit(C); 12342 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12343 C->setDevice(Record.readSubExpr()); 12344 C->setModifierLoc(Record.readSourceLocation()); 12345 C->setLParenLoc(Record.readSourceLocation()); 12346 } 12347 12348 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12349 C->setLParenLoc(Record.readSourceLocation()); 12350 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12351 C->setMapTypeModifier( 12352 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12353 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12354 } 12355 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12356 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12357 C->setMapType( 12358 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12359 C->setMapLoc(Record.readSourceLocation()); 12360 C->setColonLoc(Record.readSourceLocation()); 12361 auto NumVars = C->varlist_size(); 12362 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12363 auto TotalLists = C->getTotalComponentListNum(); 12364 auto TotalComponents = C->getTotalComponentsNum(); 12365 12366 SmallVector<Expr *, 16> Vars; 12367 Vars.reserve(NumVars); 12368 for (unsigned i = 0; i != NumVars; ++i) 12369 Vars.push_back(Record.readExpr()); 12370 C->setVarRefs(Vars); 12371 12372 SmallVector<Expr *, 16> UDMappers; 12373 UDMappers.reserve(NumVars); 12374 for (unsigned I = 0; I < NumVars; ++I) 12375 UDMappers.push_back(Record.readExpr()); 12376 C->setUDMapperRefs(UDMappers); 12377 12378 SmallVector<ValueDecl *, 16> Decls; 12379 Decls.reserve(UniqueDecls); 12380 for (unsigned i = 0; i < UniqueDecls; ++i) 12381 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12382 C->setUniqueDecls(Decls); 12383 12384 SmallVector<unsigned, 16> ListsPerDecl; 12385 ListsPerDecl.reserve(UniqueDecls); 12386 for (unsigned i = 0; i < UniqueDecls; ++i) 12387 ListsPerDecl.push_back(Record.readInt()); 12388 C->setDeclNumLists(ListsPerDecl); 12389 12390 SmallVector<unsigned, 32> ListSizes; 12391 ListSizes.reserve(TotalLists); 12392 for (unsigned i = 0; i < TotalLists; ++i) 12393 ListSizes.push_back(Record.readInt()); 12394 C->setComponentListSizes(ListSizes); 12395 12396 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12397 Components.reserve(TotalComponents); 12398 for (unsigned i = 0; i < TotalComponents; ++i) { 12399 Expr *AssociatedExprPr = Record.readExpr(); 12400 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12401 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12402 /*IsNonContiguous=*/false); 12403 } 12404 C->setComponents(Components, ListSizes); 12405 } 12406 12407 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12408 C->setLParenLoc(Record.readSourceLocation()); 12409 C->setColonLoc(Record.readSourceLocation()); 12410 C->setAllocator(Record.readSubExpr()); 12411 unsigned NumVars = C->varlist_size(); 12412 SmallVector<Expr *, 16> Vars; 12413 Vars.reserve(NumVars); 12414 for (unsigned i = 0; i != NumVars; ++i) 12415 Vars.push_back(Record.readSubExpr()); 12416 C->setVarRefs(Vars); 12417 } 12418 12419 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12420 VisitOMPClauseWithPreInit(C); 12421 C->setNumTeams(Record.readSubExpr()); 12422 C->setLParenLoc(Record.readSourceLocation()); 12423 } 12424 12425 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12426 VisitOMPClauseWithPreInit(C); 12427 C->setThreadLimit(Record.readSubExpr()); 12428 C->setLParenLoc(Record.readSourceLocation()); 12429 } 12430 12431 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12432 VisitOMPClauseWithPreInit(C); 12433 C->setPriority(Record.readSubExpr()); 12434 C->setLParenLoc(Record.readSourceLocation()); 12435 } 12436 12437 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12438 VisitOMPClauseWithPreInit(C); 12439 C->setGrainsize(Record.readSubExpr()); 12440 C->setLParenLoc(Record.readSourceLocation()); 12441 } 12442 12443 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12444 VisitOMPClauseWithPreInit(C); 12445 C->setNumTasks(Record.readSubExpr()); 12446 C->setLParenLoc(Record.readSourceLocation()); 12447 } 12448 12449 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12450 C->setHint(Record.readSubExpr()); 12451 C->setLParenLoc(Record.readSourceLocation()); 12452 } 12453 12454 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12455 VisitOMPClauseWithPreInit(C); 12456 C->setDistScheduleKind( 12457 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12458 C->setChunkSize(Record.readSubExpr()); 12459 C->setLParenLoc(Record.readSourceLocation()); 12460 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12461 C->setCommaLoc(Record.readSourceLocation()); 12462 } 12463 12464 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12465 C->setDefaultmapKind( 12466 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12467 C->setDefaultmapModifier( 12468 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12469 C->setLParenLoc(Record.readSourceLocation()); 12470 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12471 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12472 } 12473 12474 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12475 C->setLParenLoc(Record.readSourceLocation()); 12476 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12477 C->setMotionModifier( 12478 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12479 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12480 } 12481 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12482 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12483 C->setColonLoc(Record.readSourceLocation()); 12484 auto NumVars = C->varlist_size(); 12485 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12486 auto TotalLists = C->getTotalComponentListNum(); 12487 auto TotalComponents = C->getTotalComponentsNum(); 12488 12489 SmallVector<Expr *, 16> Vars; 12490 Vars.reserve(NumVars); 12491 for (unsigned i = 0; i != NumVars; ++i) 12492 Vars.push_back(Record.readSubExpr()); 12493 C->setVarRefs(Vars); 12494 12495 SmallVector<Expr *, 16> UDMappers; 12496 UDMappers.reserve(NumVars); 12497 for (unsigned I = 0; I < NumVars; ++I) 12498 UDMappers.push_back(Record.readSubExpr()); 12499 C->setUDMapperRefs(UDMappers); 12500 12501 SmallVector<ValueDecl *, 16> Decls; 12502 Decls.reserve(UniqueDecls); 12503 for (unsigned i = 0; i < UniqueDecls; ++i) 12504 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12505 C->setUniqueDecls(Decls); 12506 12507 SmallVector<unsigned, 16> ListsPerDecl; 12508 ListsPerDecl.reserve(UniqueDecls); 12509 for (unsigned i = 0; i < UniqueDecls; ++i) 12510 ListsPerDecl.push_back(Record.readInt()); 12511 C->setDeclNumLists(ListsPerDecl); 12512 12513 SmallVector<unsigned, 32> ListSizes; 12514 ListSizes.reserve(TotalLists); 12515 for (unsigned i = 0; i < TotalLists; ++i) 12516 ListSizes.push_back(Record.readInt()); 12517 C->setComponentListSizes(ListSizes); 12518 12519 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12520 Components.reserve(TotalComponents); 12521 for (unsigned i = 0; i < TotalComponents; ++i) { 12522 Expr *AssociatedExprPr = Record.readSubExpr(); 12523 bool IsNonContiguous = Record.readBool(); 12524 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12525 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12526 } 12527 C->setComponents(Components, ListSizes); 12528 } 12529 12530 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12531 C->setLParenLoc(Record.readSourceLocation()); 12532 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12533 C->setMotionModifier( 12534 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12535 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12536 } 12537 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12538 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12539 C->setColonLoc(Record.readSourceLocation()); 12540 auto NumVars = C->varlist_size(); 12541 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12542 auto TotalLists = C->getTotalComponentListNum(); 12543 auto TotalComponents = C->getTotalComponentsNum(); 12544 12545 SmallVector<Expr *, 16> Vars; 12546 Vars.reserve(NumVars); 12547 for (unsigned i = 0; i != NumVars; ++i) 12548 Vars.push_back(Record.readSubExpr()); 12549 C->setVarRefs(Vars); 12550 12551 SmallVector<Expr *, 16> UDMappers; 12552 UDMappers.reserve(NumVars); 12553 for (unsigned I = 0; I < NumVars; ++I) 12554 UDMappers.push_back(Record.readSubExpr()); 12555 C->setUDMapperRefs(UDMappers); 12556 12557 SmallVector<ValueDecl *, 16> Decls; 12558 Decls.reserve(UniqueDecls); 12559 for (unsigned i = 0; i < UniqueDecls; ++i) 12560 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12561 C->setUniqueDecls(Decls); 12562 12563 SmallVector<unsigned, 16> ListsPerDecl; 12564 ListsPerDecl.reserve(UniqueDecls); 12565 for (unsigned i = 0; i < UniqueDecls; ++i) 12566 ListsPerDecl.push_back(Record.readInt()); 12567 C->setDeclNumLists(ListsPerDecl); 12568 12569 SmallVector<unsigned, 32> ListSizes; 12570 ListSizes.reserve(TotalLists); 12571 for (unsigned i = 0; i < TotalLists; ++i) 12572 ListSizes.push_back(Record.readInt()); 12573 C->setComponentListSizes(ListSizes); 12574 12575 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12576 Components.reserve(TotalComponents); 12577 for (unsigned i = 0; i < TotalComponents; ++i) { 12578 Expr *AssociatedExprPr = Record.readSubExpr(); 12579 bool IsNonContiguous = Record.readBool(); 12580 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12581 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12582 } 12583 C->setComponents(Components, ListSizes); 12584 } 12585 12586 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12587 C->setLParenLoc(Record.readSourceLocation()); 12588 auto NumVars = C->varlist_size(); 12589 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12590 auto TotalLists = C->getTotalComponentListNum(); 12591 auto TotalComponents = C->getTotalComponentsNum(); 12592 12593 SmallVector<Expr *, 16> Vars; 12594 Vars.reserve(NumVars); 12595 for (unsigned i = 0; i != NumVars; ++i) 12596 Vars.push_back(Record.readSubExpr()); 12597 C->setVarRefs(Vars); 12598 Vars.clear(); 12599 for (unsigned i = 0; i != NumVars; ++i) 12600 Vars.push_back(Record.readSubExpr()); 12601 C->setPrivateCopies(Vars); 12602 Vars.clear(); 12603 for (unsigned i = 0; i != NumVars; ++i) 12604 Vars.push_back(Record.readSubExpr()); 12605 C->setInits(Vars); 12606 12607 SmallVector<ValueDecl *, 16> Decls; 12608 Decls.reserve(UniqueDecls); 12609 for (unsigned i = 0; i < UniqueDecls; ++i) 12610 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12611 C->setUniqueDecls(Decls); 12612 12613 SmallVector<unsigned, 16> ListsPerDecl; 12614 ListsPerDecl.reserve(UniqueDecls); 12615 for (unsigned i = 0; i < UniqueDecls; ++i) 12616 ListsPerDecl.push_back(Record.readInt()); 12617 C->setDeclNumLists(ListsPerDecl); 12618 12619 SmallVector<unsigned, 32> ListSizes; 12620 ListSizes.reserve(TotalLists); 12621 for (unsigned i = 0; i < TotalLists; ++i) 12622 ListSizes.push_back(Record.readInt()); 12623 C->setComponentListSizes(ListSizes); 12624 12625 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12626 Components.reserve(TotalComponents); 12627 for (unsigned i = 0; i < TotalComponents; ++i) { 12628 auto *AssociatedExprPr = Record.readSubExpr(); 12629 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12630 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12631 /*IsNonContiguous=*/false); 12632 } 12633 C->setComponents(Components, ListSizes); 12634 } 12635 12636 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12637 C->setLParenLoc(Record.readSourceLocation()); 12638 auto NumVars = C->varlist_size(); 12639 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12640 auto TotalLists = C->getTotalComponentListNum(); 12641 auto TotalComponents = C->getTotalComponentsNum(); 12642 12643 SmallVector<Expr *, 16> Vars; 12644 Vars.reserve(NumVars); 12645 for (unsigned i = 0; i != NumVars; ++i) 12646 Vars.push_back(Record.readSubExpr()); 12647 C->setVarRefs(Vars); 12648 12649 SmallVector<ValueDecl *, 16> Decls; 12650 Decls.reserve(UniqueDecls); 12651 for (unsigned i = 0; i < UniqueDecls; ++i) 12652 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12653 C->setUniqueDecls(Decls); 12654 12655 SmallVector<unsigned, 16> ListsPerDecl; 12656 ListsPerDecl.reserve(UniqueDecls); 12657 for (unsigned i = 0; i < UniqueDecls; ++i) 12658 ListsPerDecl.push_back(Record.readInt()); 12659 C->setDeclNumLists(ListsPerDecl); 12660 12661 SmallVector<unsigned, 32> ListSizes; 12662 ListSizes.reserve(TotalLists); 12663 for (unsigned i = 0; i < TotalLists; ++i) 12664 ListSizes.push_back(Record.readInt()); 12665 C->setComponentListSizes(ListSizes); 12666 12667 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12668 Components.reserve(TotalComponents); 12669 for (unsigned i = 0; i < TotalComponents; ++i) { 12670 Expr *AssociatedExpr = Record.readSubExpr(); 12671 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12672 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12673 /*IsNonContiguous*/ false); 12674 } 12675 C->setComponents(Components, ListSizes); 12676 } 12677 12678 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12679 C->setLParenLoc(Record.readSourceLocation()); 12680 auto NumVars = C->varlist_size(); 12681 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12682 auto TotalLists = C->getTotalComponentListNum(); 12683 auto TotalComponents = C->getTotalComponentsNum(); 12684 12685 SmallVector<Expr *, 16> Vars; 12686 Vars.reserve(NumVars); 12687 for (unsigned i = 0; i != NumVars; ++i) 12688 Vars.push_back(Record.readSubExpr()); 12689 C->setVarRefs(Vars); 12690 Vars.clear(); 12691 12692 SmallVector<ValueDecl *, 16> Decls; 12693 Decls.reserve(UniqueDecls); 12694 for (unsigned i = 0; i < UniqueDecls; ++i) 12695 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12696 C->setUniqueDecls(Decls); 12697 12698 SmallVector<unsigned, 16> ListsPerDecl; 12699 ListsPerDecl.reserve(UniqueDecls); 12700 for (unsigned i = 0; i < UniqueDecls; ++i) 12701 ListsPerDecl.push_back(Record.readInt()); 12702 C->setDeclNumLists(ListsPerDecl); 12703 12704 SmallVector<unsigned, 32> ListSizes; 12705 ListSizes.reserve(TotalLists); 12706 for (unsigned i = 0; i < TotalLists; ++i) 12707 ListSizes.push_back(Record.readInt()); 12708 C->setComponentListSizes(ListSizes); 12709 12710 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12711 Components.reserve(TotalComponents); 12712 for (unsigned i = 0; i < TotalComponents; ++i) { 12713 Expr *AssociatedExpr = Record.readSubExpr(); 12714 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12715 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12716 /*IsNonContiguous=*/false); 12717 } 12718 C->setComponents(Components, ListSizes); 12719 } 12720 12721 void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) { 12722 C->setLParenLoc(Record.readSourceLocation()); 12723 auto NumVars = C->varlist_size(); 12724 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12725 auto TotalLists = C->getTotalComponentListNum(); 12726 auto TotalComponents = C->getTotalComponentsNum(); 12727 12728 SmallVector<Expr *, 16> Vars; 12729 Vars.reserve(NumVars); 12730 for (unsigned I = 0; I != NumVars; ++I) 12731 Vars.push_back(Record.readSubExpr()); 12732 C->setVarRefs(Vars); 12733 Vars.clear(); 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 Expr *AssociatedExpr = Record.readSubExpr(); 12757 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12758 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12759 /*IsNonContiguous=*/false); 12760 } 12761 C->setComponents(Components, ListSizes); 12762 } 12763 12764 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12765 C->setLParenLoc(Record.readSourceLocation()); 12766 unsigned NumVars = C->varlist_size(); 12767 SmallVector<Expr *, 16> Vars; 12768 Vars.reserve(NumVars); 12769 for (unsigned i = 0; i != NumVars; ++i) 12770 Vars.push_back(Record.readSubExpr()); 12771 C->setVarRefs(Vars); 12772 Vars.clear(); 12773 Vars.reserve(NumVars); 12774 for (unsigned i = 0; i != NumVars; ++i) 12775 Vars.push_back(Record.readSubExpr()); 12776 C->setPrivateRefs(Vars); 12777 } 12778 12779 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12780 C->setLParenLoc(Record.readSourceLocation()); 12781 unsigned NumVars = C->varlist_size(); 12782 SmallVector<Expr *, 16> Vars; 12783 Vars.reserve(NumVars); 12784 for (unsigned i = 0; i != NumVars; ++i) 12785 Vars.push_back(Record.readSubExpr()); 12786 C->setVarRefs(Vars); 12787 } 12788 12789 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12790 C->setLParenLoc(Record.readSourceLocation()); 12791 unsigned NumVars = C->varlist_size(); 12792 SmallVector<Expr *, 16> Vars; 12793 Vars.reserve(NumVars); 12794 for (unsigned i = 0; i != NumVars; ++i) 12795 Vars.push_back(Record.readSubExpr()); 12796 C->setVarRefs(Vars); 12797 } 12798 12799 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12800 C->setLParenLoc(Record.readSourceLocation()); 12801 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12802 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12803 Data.reserve(NumOfAllocators); 12804 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12805 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12806 D.Allocator = Record.readSubExpr(); 12807 D.AllocatorTraits = Record.readSubExpr(); 12808 D.LParenLoc = Record.readSourceLocation(); 12809 D.RParenLoc = Record.readSourceLocation(); 12810 } 12811 C->setAllocatorsData(Data); 12812 } 12813 12814 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12815 C->setLParenLoc(Record.readSourceLocation()); 12816 C->setModifier(Record.readSubExpr()); 12817 C->setColonLoc(Record.readSourceLocation()); 12818 unsigned NumOfLocators = C->varlist_size(); 12819 SmallVector<Expr *, 4> Locators; 12820 Locators.reserve(NumOfLocators); 12821 for (unsigned I = 0; I != NumOfLocators; ++I) 12822 Locators.push_back(Record.readSubExpr()); 12823 C->setVarRefs(Locators); 12824 } 12825 12826 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12827 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12828 C->setLParenLoc(Record.readSourceLocation()); 12829 C->setKindKwLoc(Record.readSourceLocation()); 12830 } 12831 12832 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) { 12833 VisitOMPClauseWithPreInit(C); 12834 C->setThreadID(Record.readSubExpr()); 12835 C->setLParenLoc(Record.readSourceLocation()); 12836 } 12837 12838 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) { 12839 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>()); 12840 C->setLParenLoc(Record.readSourceLocation()); 12841 C->setBindKindLoc(Record.readSourceLocation()); 12842 } 12843 12844 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) { 12845 C->setAlignment(Record.readExpr()); 12846 C->setLParenLoc(Record.readSourceLocation()); 12847 } 12848 12849 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12850 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12851 TI.Sets.resize(readUInt32()); 12852 for (auto &Set : TI.Sets) { 12853 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12854 Set.Selectors.resize(readUInt32()); 12855 for (auto &Selector : Set.Selectors) { 12856 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12857 Selector.ScoreOrCondition = nullptr; 12858 if (readBool()) 12859 Selector.ScoreOrCondition = readExprRef(); 12860 Selector.Properties.resize(readUInt32()); 12861 for (auto &Property : Selector.Properties) 12862 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12863 } 12864 } 12865 return &TI; 12866 } 12867 12868 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12869 if (!Data) 12870 return; 12871 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12872 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12873 skipInts(3); 12874 } 12875 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12876 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12877 Clauses[I] = readOMPClause(); 12878 Data->setClauses(Clauses); 12879 if (Data->hasAssociatedStmt()) 12880 Data->setAssociatedStmt(readStmt()); 12881 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12882 Data->getChildren()[I] = readStmt(); 12883 } 12884