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/ASTUnresolvedSet.h" 19 #include "clang/AST/AbstractTypeReader.h" 20 #include "clang/AST/Decl.h" 21 #include "clang/AST/DeclBase.h" 22 #include "clang/AST/DeclCXX.h" 23 #include "clang/AST/DeclFriend.h" 24 #include "clang/AST/DeclGroup.h" 25 #include "clang/AST/DeclObjC.h" 26 #include "clang/AST/DeclTemplate.h" 27 #include "clang/AST/DeclarationName.h" 28 #include "clang/AST/Expr.h" 29 #include "clang/AST/ExprCXX.h" 30 #include "clang/AST/ExternalASTSource.h" 31 #include "clang/AST/NestedNameSpecifier.h" 32 #include "clang/AST/ODRHash.h" 33 #include "clang/AST/OpenMPClause.h" 34 #include "clang/AST/RawCommentList.h" 35 #include "clang/AST/TemplateBase.h" 36 #include "clang/AST/TemplateName.h" 37 #include "clang/AST/Type.h" 38 #include "clang/AST/TypeLoc.h" 39 #include "clang/AST/TypeLocVisitor.h" 40 #include "clang/AST/UnresolvedSet.h" 41 #include "clang/Basic/CommentOptions.h" 42 #include "clang/Basic/Diagnostic.h" 43 #include "clang/Basic/DiagnosticError.h" 44 #include "clang/Basic/DiagnosticOptions.h" 45 #include "clang/Basic/ExceptionSpecificationType.h" 46 #include "clang/Basic/FileManager.h" 47 #include "clang/Basic/FileSystemOptions.h" 48 #include "clang/Basic/IdentifierTable.h" 49 #include "clang/Basic/LLVM.h" 50 #include "clang/Basic/LangOptions.h" 51 #include "clang/Basic/Module.h" 52 #include "clang/Basic/ObjCRuntime.h" 53 #include "clang/Basic/OpenMPKinds.h" 54 #include "clang/Basic/OperatorKinds.h" 55 #include "clang/Basic/PragmaKinds.h" 56 #include "clang/Basic/Sanitizers.h" 57 #include "clang/Basic/SourceLocation.h" 58 #include "clang/Basic/SourceManager.h" 59 #include "clang/Basic/SourceManagerInternals.h" 60 #include "clang/Basic/Specifiers.h" 61 #include "clang/Basic/TargetInfo.h" 62 #include "clang/Basic/TargetOptions.h" 63 #include "clang/Basic/TokenKinds.h" 64 #include "clang/Basic/Version.h" 65 #include "clang/Lex/HeaderSearch.h" 66 #include "clang/Lex/HeaderSearchOptions.h" 67 #include "clang/Lex/MacroInfo.h" 68 #include "clang/Lex/ModuleMap.h" 69 #include "clang/Lex/PreprocessingRecord.h" 70 #include "clang/Lex/Preprocessor.h" 71 #include "clang/Lex/PreprocessorOptions.h" 72 #include "clang/Lex/Token.h" 73 #include "clang/Sema/ObjCMethodList.h" 74 #include "clang/Sema/Scope.h" 75 #include "clang/Sema/Sema.h" 76 #include "clang/Sema/Weak.h" 77 #include "clang/Serialization/ASTBitCodes.h" 78 #include "clang/Serialization/ASTDeserializationListener.h" 79 #include "clang/Serialization/ASTRecordReader.h" 80 #include "clang/Serialization/ContinuousRangeMap.h" 81 #include "clang/Serialization/GlobalModuleIndex.h" 82 #include "clang/Serialization/InMemoryModuleCache.h" 83 #include "clang/Serialization/ModuleFile.h" 84 #include "clang/Serialization/ModuleFileExtension.h" 85 #include "clang/Serialization/ModuleManager.h" 86 #include "clang/Serialization/PCHContainerOperations.h" 87 #include "clang/Serialization/SerializationDiagnostic.h" 88 #include "llvm/ADT/APFloat.h" 89 #include "llvm/ADT/APInt.h" 90 #include "llvm/ADT/APSInt.h" 91 #include "llvm/ADT/ArrayRef.h" 92 #include "llvm/ADT/DenseMap.h" 93 #include "llvm/ADT/FloatingPointMode.h" 94 #include "llvm/ADT/FoldingSet.h" 95 #include "llvm/ADT/Hashing.h" 96 #include "llvm/ADT/IntrusiveRefCntPtr.h" 97 #include "llvm/ADT/None.h" 98 #include "llvm/ADT/Optional.h" 99 #include "llvm/ADT/STLExtras.h" 100 #include "llvm/ADT/ScopeExit.h" 101 #include "llvm/ADT/SmallPtrSet.h" 102 #include "llvm/ADT/SmallString.h" 103 #include "llvm/ADT/SmallVector.h" 104 #include "llvm/ADT/StringExtras.h" 105 #include "llvm/ADT/StringMap.h" 106 #include "llvm/ADT/StringRef.h" 107 #include "llvm/ADT/Triple.h" 108 #include "llvm/ADT/iterator_range.h" 109 #include "llvm/Bitstream/BitstreamReader.h" 110 #include "llvm/Support/Casting.h" 111 #include "llvm/Support/Compiler.h" 112 #include "llvm/Support/Compression.h" 113 #include "llvm/Support/DJB.h" 114 #include "llvm/Support/Endian.h" 115 #include "llvm/Support/Error.h" 116 #include "llvm/Support/ErrorHandling.h" 117 #include "llvm/Support/FileSystem.h" 118 #include "llvm/Support/LEB128.h" 119 #include "llvm/Support/MemoryBuffer.h" 120 #include "llvm/Support/Path.h" 121 #include "llvm/Support/SaveAndRestore.h" 122 #include "llvm/Support/Timer.h" 123 #include "llvm/Support/VersionTuple.h" 124 #include "llvm/Support/raw_ostream.h" 125 #include <algorithm> 126 #include <cassert> 127 #include <cstddef> 128 #include <cstdint> 129 #include <cstdio> 130 #include <ctime> 131 #include <iterator> 132 #include <limits> 133 #include <map> 134 #include <memory> 135 #include <string> 136 #include <system_error> 137 #include <tuple> 138 #include <utility> 139 #include <vector> 140 141 using namespace clang; 142 using namespace clang::serialization; 143 using namespace clang::serialization::reader; 144 using llvm::BitstreamCursor; 145 146 //===----------------------------------------------------------------------===// 147 // ChainedASTReaderListener implementation 148 //===----------------------------------------------------------------------===// 149 150 bool 151 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 152 return First->ReadFullVersionInformation(FullVersion) || 153 Second->ReadFullVersionInformation(FullVersion); 154 } 155 156 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 157 First->ReadModuleName(ModuleName); 158 Second->ReadModuleName(ModuleName); 159 } 160 161 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 162 First->ReadModuleMapFile(ModuleMapPath); 163 Second->ReadModuleMapFile(ModuleMapPath); 164 } 165 166 bool 167 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 168 bool Complain, 169 bool AllowCompatibleDifferences) { 170 return First->ReadLanguageOptions(LangOpts, Complain, 171 AllowCompatibleDifferences) || 172 Second->ReadLanguageOptions(LangOpts, Complain, 173 AllowCompatibleDifferences); 174 } 175 176 bool ChainedASTReaderListener::ReadTargetOptions( 177 const TargetOptions &TargetOpts, bool Complain, 178 bool AllowCompatibleDifferences) { 179 return First->ReadTargetOptions(TargetOpts, Complain, 180 AllowCompatibleDifferences) || 181 Second->ReadTargetOptions(TargetOpts, Complain, 182 AllowCompatibleDifferences); 183 } 184 185 bool ChainedASTReaderListener::ReadDiagnosticOptions( 186 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 187 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 188 Second->ReadDiagnosticOptions(DiagOpts, Complain); 189 } 190 191 bool 192 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 193 bool Complain) { 194 return First->ReadFileSystemOptions(FSOpts, Complain) || 195 Second->ReadFileSystemOptions(FSOpts, Complain); 196 } 197 198 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 199 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 200 bool Complain) { 201 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 202 Complain) || 203 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 204 Complain); 205 } 206 207 bool ChainedASTReaderListener::ReadPreprocessorOptions( 208 const PreprocessorOptions &PPOpts, bool Complain, 209 std::string &SuggestedPredefines) { 210 return First->ReadPreprocessorOptions(PPOpts, Complain, 211 SuggestedPredefines) || 212 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 213 } 214 215 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 216 unsigned Value) { 217 First->ReadCounter(M, Value); 218 Second->ReadCounter(M, Value); 219 } 220 221 bool ChainedASTReaderListener::needsInputFileVisitation() { 222 return First->needsInputFileVisitation() || 223 Second->needsInputFileVisitation(); 224 } 225 226 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 227 return First->needsSystemInputFileVisitation() || 228 Second->needsSystemInputFileVisitation(); 229 } 230 231 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 232 ModuleKind Kind) { 233 First->visitModuleFile(Filename, Kind); 234 Second->visitModuleFile(Filename, Kind); 235 } 236 237 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 238 bool isSystem, 239 bool isOverridden, 240 bool isExplicitModule) { 241 bool Continue = false; 242 if (First->needsInputFileVisitation() && 243 (!isSystem || First->needsSystemInputFileVisitation())) 244 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 245 isExplicitModule); 246 if (Second->needsInputFileVisitation() && 247 (!isSystem || Second->needsSystemInputFileVisitation())) 248 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 249 isExplicitModule); 250 return Continue; 251 } 252 253 void ChainedASTReaderListener::readModuleFileExtension( 254 const ModuleFileExtensionMetadata &Metadata) { 255 First->readModuleFileExtension(Metadata); 256 Second->readModuleFileExtension(Metadata); 257 } 258 259 //===----------------------------------------------------------------------===// 260 // PCH validator implementation 261 //===----------------------------------------------------------------------===// 262 263 ASTReaderListener::~ASTReaderListener() = default; 264 265 /// Compare the given set of language options against an existing set of 266 /// language options. 267 /// 268 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 269 /// \param AllowCompatibleDifferences If true, differences between compatible 270 /// language options will be permitted. 271 /// 272 /// \returns true if the languagae options mis-match, false otherwise. 273 static bool checkLanguageOptions(const LangOptions &LangOpts, 274 const LangOptions &ExistingLangOpts, 275 DiagnosticsEngine *Diags, 276 bool AllowCompatibleDifferences = true) { 277 #define LANGOPT(Name, Bits, Default, Description) \ 278 if (ExistingLangOpts.Name != LangOpts.Name) { \ 279 if (Diags) \ 280 Diags->Report(diag::err_pch_langopt_mismatch) \ 281 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 282 return true; \ 283 } 284 285 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 286 if (ExistingLangOpts.Name != LangOpts.Name) { \ 287 if (Diags) \ 288 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 289 << Description; \ 290 return true; \ 291 } 292 293 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 294 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 295 if (Diags) \ 296 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 297 << Description; \ 298 return true; \ 299 } 300 301 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 302 if (!AllowCompatibleDifferences) \ 303 LANGOPT(Name, Bits, Default, Description) 304 305 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 306 if (!AllowCompatibleDifferences) \ 307 ENUM_LANGOPT(Name, Bits, Default, Description) 308 309 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 310 if (!AllowCompatibleDifferences) \ 311 VALUE_LANGOPT(Name, Bits, Default, Description) 312 313 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 314 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 315 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 316 #include "clang/Basic/LangOptions.def" 317 318 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 319 if (Diags) 320 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 321 return true; 322 } 323 324 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 325 if (Diags) 326 Diags->Report(diag::err_pch_langopt_value_mismatch) 327 << "target Objective-C runtime"; 328 return true; 329 } 330 331 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 332 LangOpts.CommentOpts.BlockCommandNames) { 333 if (Diags) 334 Diags->Report(diag::err_pch_langopt_value_mismatch) 335 << "block command names"; 336 return true; 337 } 338 339 // Sanitizer feature mismatches are treated as compatible differences. If 340 // compatible differences aren't allowed, we still only want to check for 341 // mismatches of non-modular sanitizers (the only ones which can affect AST 342 // generation). 343 if (!AllowCompatibleDifferences) { 344 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 345 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 346 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 347 ExistingSanitizers.clear(ModularSanitizers); 348 ImportedSanitizers.clear(ModularSanitizers); 349 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 350 const std::string Flag = "-fsanitize="; 351 if (Diags) { 352 #define SANITIZER(NAME, ID) \ 353 { \ 354 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 355 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 356 if (InExistingModule != InImportedModule) \ 357 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 358 << InExistingModule << (Flag + NAME); \ 359 } 360 #include "clang/Basic/Sanitizers.def" 361 } 362 return true; 363 } 364 } 365 366 return false; 367 } 368 369 /// Compare the given set of target options against an existing set of 370 /// target options. 371 /// 372 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 373 /// 374 /// \returns true if the target options mis-match, false otherwise. 375 static bool checkTargetOptions(const TargetOptions &TargetOpts, 376 const TargetOptions &ExistingTargetOpts, 377 DiagnosticsEngine *Diags, 378 bool AllowCompatibleDifferences = true) { 379 #define CHECK_TARGET_OPT(Field, Name) \ 380 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 381 if (Diags) \ 382 Diags->Report(diag::err_pch_targetopt_mismatch) \ 383 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 384 return true; \ 385 } 386 387 // The triple and ABI must match exactly. 388 CHECK_TARGET_OPT(Triple, "target"); 389 CHECK_TARGET_OPT(ABI, "target ABI"); 390 391 // We can tolerate different CPUs in many cases, notably when one CPU 392 // supports a strict superset of another. When allowing compatible 393 // differences skip this check. 394 if (!AllowCompatibleDifferences) { 395 CHECK_TARGET_OPT(CPU, "target CPU"); 396 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 397 } 398 399 #undef CHECK_TARGET_OPT 400 401 // Compare feature sets. 402 SmallVector<StringRef, 4> ExistingFeatures( 403 ExistingTargetOpts.FeaturesAsWritten.begin(), 404 ExistingTargetOpts.FeaturesAsWritten.end()); 405 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 406 TargetOpts.FeaturesAsWritten.end()); 407 llvm::sort(ExistingFeatures); 408 llvm::sort(ReadFeatures); 409 410 // We compute the set difference in both directions explicitly so that we can 411 // diagnose the differences differently. 412 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 413 std::set_difference( 414 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 415 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 416 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 417 ExistingFeatures.begin(), ExistingFeatures.end(), 418 std::back_inserter(UnmatchedReadFeatures)); 419 420 // If we are allowing compatible differences and the read feature set is 421 // a strict subset of the existing feature set, there is nothing to diagnose. 422 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 423 return false; 424 425 if (Diags) { 426 for (StringRef Feature : UnmatchedReadFeatures) 427 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 428 << /* is-existing-feature */ false << Feature; 429 for (StringRef Feature : UnmatchedExistingFeatures) 430 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 431 << /* is-existing-feature */ true << Feature; 432 } 433 434 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 435 } 436 437 bool 438 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 439 bool Complain, 440 bool AllowCompatibleDifferences) { 441 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 442 return checkLanguageOptions(LangOpts, ExistingLangOpts, 443 Complain ? &Reader.Diags : nullptr, 444 AllowCompatibleDifferences); 445 } 446 447 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 448 bool Complain, 449 bool AllowCompatibleDifferences) { 450 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 451 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 452 Complain ? &Reader.Diags : nullptr, 453 AllowCompatibleDifferences); 454 } 455 456 namespace { 457 458 using MacroDefinitionsMap = 459 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 460 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 461 462 } // namespace 463 464 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 465 DiagnosticsEngine &Diags, 466 bool Complain) { 467 using Level = DiagnosticsEngine::Level; 468 469 // Check current mappings for new -Werror mappings, and the stored mappings 470 // for cases that were explicitly mapped to *not* be errors that are now 471 // errors because of options like -Werror. 472 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 473 474 for (DiagnosticsEngine *MappingSource : MappingSources) { 475 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 476 diag::kind DiagID = DiagIDMappingPair.first; 477 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 478 if (CurLevel < DiagnosticsEngine::Error) 479 continue; // not significant 480 Level StoredLevel = 481 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 482 if (StoredLevel < DiagnosticsEngine::Error) { 483 if (Complain) 484 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 485 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 486 return true; 487 } 488 } 489 } 490 491 return false; 492 } 493 494 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 495 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 496 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 497 return true; 498 return Ext >= diag::Severity::Error; 499 } 500 501 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 502 DiagnosticsEngine &Diags, 503 bool IsSystem, bool Complain) { 504 // Top-level options 505 if (IsSystem) { 506 if (Diags.getSuppressSystemWarnings()) 507 return false; 508 // If -Wsystem-headers was not enabled before, be conservative 509 if (StoredDiags.getSuppressSystemWarnings()) { 510 if (Complain) 511 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 512 return true; 513 } 514 } 515 516 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 517 if (Complain) 518 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 519 return true; 520 } 521 522 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 523 !StoredDiags.getEnableAllWarnings()) { 524 if (Complain) 525 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 526 return true; 527 } 528 529 if (isExtHandlingFromDiagsError(Diags) && 530 !isExtHandlingFromDiagsError(StoredDiags)) { 531 if (Complain) 532 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 533 return true; 534 } 535 536 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 537 } 538 539 /// Return the top import module if it is implicit, nullptr otherwise. 540 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 541 Preprocessor &PP) { 542 // If the original import came from a file explicitly generated by the user, 543 // don't check the diagnostic mappings. 544 // FIXME: currently this is approximated by checking whether this is not a 545 // module import of an implicitly-loaded module file. 546 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 547 // the transitive closure of its imports, since unrelated modules cannot be 548 // imported until after this module finishes validation. 549 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 550 while (!TopImport->ImportedBy.empty()) 551 TopImport = TopImport->ImportedBy[0]; 552 if (TopImport->Kind != MK_ImplicitModule) 553 return nullptr; 554 555 StringRef ModuleName = TopImport->ModuleName; 556 assert(!ModuleName.empty() && "diagnostic options read before module name"); 557 558 Module *M = 559 PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc); 560 assert(M && "missing module"); 561 return M; 562 } 563 564 bool PCHValidator::ReadDiagnosticOptions( 565 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 566 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 567 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 568 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 569 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 570 // This should never fail, because we would have processed these options 571 // before writing them to an ASTFile. 572 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 573 574 ModuleManager &ModuleMgr = Reader.getModuleManager(); 575 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 576 577 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 578 if (!TopM) 579 return false; 580 581 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 582 // contains the union of their flags. 583 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 584 Complain); 585 } 586 587 /// Collect the macro definitions provided by the given preprocessor 588 /// options. 589 static void 590 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 591 MacroDefinitionsMap &Macros, 592 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 593 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 594 StringRef Macro = PPOpts.Macros[I].first; 595 bool IsUndef = PPOpts.Macros[I].second; 596 597 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 598 StringRef MacroName = MacroPair.first; 599 StringRef MacroBody = MacroPair.second; 600 601 // For an #undef'd macro, we only care about the name. 602 if (IsUndef) { 603 if (MacroNames && !Macros.count(MacroName)) 604 MacroNames->push_back(MacroName); 605 606 Macros[MacroName] = std::make_pair("", true); 607 continue; 608 } 609 610 // For a #define'd macro, figure out the actual definition. 611 if (MacroName.size() == Macro.size()) 612 MacroBody = "1"; 613 else { 614 // Note: GCC drops anything following an end-of-line character. 615 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 616 MacroBody = MacroBody.substr(0, End); 617 } 618 619 if (MacroNames && !Macros.count(MacroName)) 620 MacroNames->push_back(MacroName); 621 Macros[MacroName] = std::make_pair(MacroBody, false); 622 } 623 } 624 625 /// Check the preprocessor options deserialized from the control block 626 /// against the preprocessor options in an existing preprocessor. 627 /// 628 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 629 /// \param Validate If true, validate preprocessor options. If false, allow 630 /// macros defined by \p ExistingPPOpts to override those defined by 631 /// \p PPOpts in SuggestedPredefines. 632 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 633 const PreprocessorOptions &ExistingPPOpts, 634 DiagnosticsEngine *Diags, 635 FileManager &FileMgr, 636 std::string &SuggestedPredefines, 637 const LangOptions &LangOpts, 638 bool Validate = true) { 639 // Check macro definitions. 640 MacroDefinitionsMap ASTFileMacros; 641 collectMacroDefinitions(PPOpts, ASTFileMacros); 642 MacroDefinitionsMap ExistingMacros; 643 SmallVector<StringRef, 4> ExistingMacroNames; 644 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 645 646 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 647 // Dig out the macro definition in the existing preprocessor options. 648 StringRef MacroName = ExistingMacroNames[I]; 649 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 650 651 // Check whether we know anything about this macro name or not. 652 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 653 ASTFileMacros.find(MacroName); 654 if (!Validate || Known == ASTFileMacros.end()) { 655 // FIXME: Check whether this identifier was referenced anywhere in the 656 // AST file. If so, we should reject the AST file. Unfortunately, this 657 // information isn't in the control block. What shall we do about it? 658 659 if (Existing.second) { 660 SuggestedPredefines += "#undef "; 661 SuggestedPredefines += MacroName.str(); 662 SuggestedPredefines += '\n'; 663 } else { 664 SuggestedPredefines += "#define "; 665 SuggestedPredefines += MacroName.str(); 666 SuggestedPredefines += ' '; 667 SuggestedPredefines += Existing.first.str(); 668 SuggestedPredefines += '\n'; 669 } 670 continue; 671 } 672 673 // If the macro was defined in one but undef'd in the other, we have a 674 // conflict. 675 if (Existing.second != Known->second.second) { 676 if (Diags) { 677 Diags->Report(diag::err_pch_macro_def_undef) 678 << MacroName << Known->second.second; 679 } 680 return true; 681 } 682 683 // If the macro was #undef'd in both, or if the macro bodies are identical, 684 // it's fine. 685 if (Existing.second || Existing.first == Known->second.first) 686 continue; 687 688 // The macro bodies differ; complain. 689 if (Diags) { 690 Diags->Report(diag::err_pch_macro_def_conflict) 691 << MacroName << Known->second.first << Existing.first; 692 } 693 return true; 694 } 695 696 // Check whether we're using predefines. 697 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 698 if (Diags) { 699 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 700 } 701 return true; 702 } 703 704 // Detailed record is important since it is used for the module cache hash. 705 if (LangOpts.Modules && 706 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 707 if (Diags) { 708 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 709 } 710 return true; 711 } 712 713 // Compute the #include and #include_macros lines we need. 714 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 715 StringRef File = ExistingPPOpts.Includes[I]; 716 717 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 718 !ExistingPPOpts.PCHThroughHeader.empty()) { 719 // In case the through header is an include, we must add all the includes 720 // to the predefines so the start point can be determined. 721 SuggestedPredefines += "#include \""; 722 SuggestedPredefines += File; 723 SuggestedPredefines += "\"\n"; 724 continue; 725 } 726 727 if (File == ExistingPPOpts.ImplicitPCHInclude) 728 continue; 729 730 if (llvm::is_contained(PPOpts.Includes, File)) 731 continue; 732 733 SuggestedPredefines += "#include \""; 734 SuggestedPredefines += File; 735 SuggestedPredefines += "\"\n"; 736 } 737 738 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 739 StringRef File = ExistingPPOpts.MacroIncludes[I]; 740 if (llvm::is_contained(PPOpts.MacroIncludes, File)) 741 continue; 742 743 SuggestedPredefines += "#__include_macros \""; 744 SuggestedPredefines += File; 745 SuggestedPredefines += "\"\n##\n"; 746 } 747 748 return false; 749 } 750 751 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 752 bool Complain, 753 std::string &SuggestedPredefines) { 754 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 755 756 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 757 Complain? &Reader.Diags : nullptr, 758 PP.getFileManager(), 759 SuggestedPredefines, 760 PP.getLangOpts()); 761 } 762 763 bool SimpleASTReaderListener::ReadPreprocessorOptions( 764 const PreprocessorOptions &PPOpts, 765 bool Complain, 766 std::string &SuggestedPredefines) { 767 return checkPreprocessorOptions(PPOpts, 768 PP.getPreprocessorOpts(), 769 nullptr, 770 PP.getFileManager(), 771 SuggestedPredefines, 772 PP.getLangOpts(), 773 false); 774 } 775 776 /// Check the header search options deserialized from the control block 777 /// against the header search options in an existing preprocessor. 778 /// 779 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 780 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 781 StringRef SpecificModuleCachePath, 782 StringRef ExistingModuleCachePath, 783 DiagnosticsEngine *Diags, 784 const LangOptions &LangOpts, 785 const PreprocessorOptions &PPOpts) { 786 if (LangOpts.Modules) { 787 if (SpecificModuleCachePath != ExistingModuleCachePath && 788 !PPOpts.AllowPCHWithDifferentModulesCachePath) { 789 if (Diags) 790 Diags->Report(diag::err_pch_modulecache_mismatch) 791 << SpecificModuleCachePath << ExistingModuleCachePath; 792 return true; 793 } 794 } 795 796 return false; 797 } 798 799 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 800 StringRef SpecificModuleCachePath, 801 bool Complain) { 802 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 803 PP.getHeaderSearchInfo().getModuleCachePath(), 804 Complain ? &Reader.Diags : nullptr, 805 PP.getLangOpts(), PP.getPreprocessorOpts()); 806 } 807 808 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 809 PP.setCounterValue(Value); 810 } 811 812 //===----------------------------------------------------------------------===// 813 // AST reader implementation 814 //===----------------------------------------------------------------------===// 815 816 static uint64_t readULEB(const unsigned char *&P) { 817 unsigned Length = 0; 818 const char *Error = nullptr; 819 820 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error); 821 if (Error) 822 llvm::report_fatal_error(Error); 823 P += Length; 824 return Val; 825 } 826 827 /// Read ULEB-encoded key length and data length. 828 static std::pair<unsigned, unsigned> 829 readULEBKeyDataLength(const unsigned char *&P) { 830 unsigned KeyLen = readULEB(P); 831 if ((unsigned)KeyLen != KeyLen) 832 llvm::report_fatal_error("key too large"); 833 834 unsigned DataLen = readULEB(P); 835 if ((unsigned)DataLen != DataLen) 836 llvm::report_fatal_error("data too large"); 837 838 return std::make_pair(KeyLen, DataLen); 839 } 840 841 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 842 bool TakeOwnership) { 843 DeserializationListener = Listener; 844 OwnsDeserializationListener = TakeOwnership; 845 } 846 847 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 848 return serialization::ComputeHash(Sel); 849 } 850 851 std::pair<unsigned, unsigned> 852 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 853 return readULEBKeyDataLength(d); 854 } 855 856 ASTSelectorLookupTrait::internal_key_type 857 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 858 using namespace llvm::support; 859 860 SelectorTable &SelTable = Reader.getContext().Selectors; 861 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 862 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 863 F, endian::readNext<uint32_t, little, unaligned>(d)); 864 if (N == 0) 865 return SelTable.getNullarySelector(FirstII); 866 else if (N == 1) 867 return SelTable.getUnarySelector(FirstII); 868 869 SmallVector<IdentifierInfo *, 16> Args; 870 Args.push_back(FirstII); 871 for (unsigned I = 1; I != N; ++I) 872 Args.push_back(Reader.getLocalIdentifier( 873 F, endian::readNext<uint32_t, little, unaligned>(d))); 874 875 return SelTable.getSelector(N, Args.data()); 876 } 877 878 ASTSelectorLookupTrait::data_type 879 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 880 unsigned DataLen) { 881 using namespace llvm::support; 882 883 data_type Result; 884 885 Result.ID = Reader.getGlobalSelectorID( 886 F, endian::readNext<uint32_t, little, unaligned>(d)); 887 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 888 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 889 Result.InstanceBits = FullInstanceBits & 0x3; 890 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 891 Result.FactoryBits = FullFactoryBits & 0x3; 892 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 893 unsigned NumInstanceMethods = FullInstanceBits >> 3; 894 unsigned NumFactoryMethods = FullFactoryBits >> 3; 895 896 // Load instance methods 897 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 898 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 899 F, endian::readNext<uint32_t, little, unaligned>(d))) 900 Result.Instance.push_back(Method); 901 } 902 903 // Load factory methods 904 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 905 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 906 F, endian::readNext<uint32_t, little, unaligned>(d))) 907 Result.Factory.push_back(Method); 908 } 909 910 return Result; 911 } 912 913 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 914 return llvm::djbHash(a); 915 } 916 917 std::pair<unsigned, unsigned> 918 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 919 return readULEBKeyDataLength(d); 920 } 921 922 ASTIdentifierLookupTraitBase::internal_key_type 923 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 924 assert(n >= 2 && d[n-1] == '\0'); 925 return StringRef((const char*) d, n-1); 926 } 927 928 /// Whether the given identifier is "interesting". 929 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 930 bool IsModule) { 931 return II.hadMacroDefinition() || II.isPoisoned() || 932 (!IsModule && II.getObjCOrBuiltinID()) || 933 II.hasRevertedTokenIDToIdentifier() || 934 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 935 II.getFETokenInfo()); 936 } 937 938 static bool readBit(unsigned &Bits) { 939 bool Value = Bits & 0x1; 940 Bits >>= 1; 941 return Value; 942 } 943 944 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 945 using namespace llvm::support; 946 947 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 948 return Reader.getGlobalIdentifierID(F, RawID >> 1); 949 } 950 951 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 952 if (!II.isFromAST()) { 953 II.setIsFromAST(); 954 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 955 if (isInterestingIdentifier(Reader, II, IsModule)) 956 II.setChangedSinceDeserialization(); 957 } 958 } 959 960 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 961 const unsigned char* d, 962 unsigned DataLen) { 963 using namespace llvm::support; 964 965 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 966 bool IsInteresting = RawID & 0x01; 967 968 // Wipe out the "is interesting" bit. 969 RawID = RawID >> 1; 970 971 // Build the IdentifierInfo and link the identifier ID with it. 972 IdentifierInfo *II = KnownII; 973 if (!II) { 974 II = &Reader.getIdentifierTable().getOwn(k); 975 KnownII = II; 976 } 977 markIdentifierFromAST(Reader, *II); 978 Reader.markIdentifierUpToDate(II); 979 980 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 981 if (!IsInteresting) { 982 // For uninteresting identifiers, there's nothing else to do. Just notify 983 // the reader that we've finished loading this identifier. 984 Reader.SetIdentifierInfo(ID, II); 985 return II; 986 } 987 988 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 989 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 990 bool CPlusPlusOperatorKeyword = readBit(Bits); 991 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 992 bool Poisoned = readBit(Bits); 993 bool ExtensionToken = readBit(Bits); 994 bool HadMacroDefinition = readBit(Bits); 995 996 assert(Bits == 0 && "Extra bits in the identifier?"); 997 DataLen -= 8; 998 999 // Set or check the various bits in the IdentifierInfo structure. 1000 // Token IDs are read-only. 1001 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 1002 II->revertTokenIDToIdentifier(); 1003 if (!F.isModule()) 1004 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 1005 assert(II->isExtensionToken() == ExtensionToken && 1006 "Incorrect extension token flag"); 1007 (void)ExtensionToken; 1008 if (Poisoned) 1009 II->setIsPoisoned(true); 1010 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 1011 "Incorrect C++ operator keyword flag"); 1012 (void)CPlusPlusOperatorKeyword; 1013 1014 // If this identifier is a macro, deserialize the macro 1015 // definition. 1016 if (HadMacroDefinition) { 1017 uint32_t MacroDirectivesOffset = 1018 endian::readNext<uint32_t, little, unaligned>(d); 1019 DataLen -= 4; 1020 1021 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1022 } 1023 1024 Reader.SetIdentifierInfo(ID, II); 1025 1026 // Read all of the declarations visible at global scope with this 1027 // name. 1028 if (DataLen > 0) { 1029 SmallVector<uint32_t, 4> DeclIDs; 1030 for (; DataLen > 0; DataLen -= 4) 1031 DeclIDs.push_back(Reader.getGlobalDeclID( 1032 F, endian::readNext<uint32_t, little, unaligned>(d))); 1033 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1034 } 1035 1036 return II; 1037 } 1038 1039 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1040 : Kind(Name.getNameKind()) { 1041 switch (Kind) { 1042 case DeclarationName::Identifier: 1043 Data = (uint64_t)Name.getAsIdentifierInfo(); 1044 break; 1045 case DeclarationName::ObjCZeroArgSelector: 1046 case DeclarationName::ObjCOneArgSelector: 1047 case DeclarationName::ObjCMultiArgSelector: 1048 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1049 break; 1050 case DeclarationName::CXXOperatorName: 1051 Data = Name.getCXXOverloadedOperator(); 1052 break; 1053 case DeclarationName::CXXLiteralOperatorName: 1054 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1055 break; 1056 case DeclarationName::CXXDeductionGuideName: 1057 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1058 ->getDeclName().getAsIdentifierInfo(); 1059 break; 1060 case DeclarationName::CXXConstructorName: 1061 case DeclarationName::CXXDestructorName: 1062 case DeclarationName::CXXConversionFunctionName: 1063 case DeclarationName::CXXUsingDirective: 1064 Data = 0; 1065 break; 1066 } 1067 } 1068 1069 unsigned DeclarationNameKey::getHash() const { 1070 llvm::FoldingSetNodeID ID; 1071 ID.AddInteger(Kind); 1072 1073 switch (Kind) { 1074 case DeclarationName::Identifier: 1075 case DeclarationName::CXXLiteralOperatorName: 1076 case DeclarationName::CXXDeductionGuideName: 1077 ID.AddString(((IdentifierInfo*)Data)->getName()); 1078 break; 1079 case DeclarationName::ObjCZeroArgSelector: 1080 case DeclarationName::ObjCOneArgSelector: 1081 case DeclarationName::ObjCMultiArgSelector: 1082 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1083 break; 1084 case DeclarationName::CXXOperatorName: 1085 ID.AddInteger((OverloadedOperatorKind)Data); 1086 break; 1087 case DeclarationName::CXXConstructorName: 1088 case DeclarationName::CXXDestructorName: 1089 case DeclarationName::CXXConversionFunctionName: 1090 case DeclarationName::CXXUsingDirective: 1091 break; 1092 } 1093 1094 return ID.ComputeHash(); 1095 } 1096 1097 ModuleFile * 1098 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1099 using namespace llvm::support; 1100 1101 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1102 return Reader.getLocalModuleFile(F, ModuleFileID); 1103 } 1104 1105 std::pair<unsigned, unsigned> 1106 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1107 return readULEBKeyDataLength(d); 1108 } 1109 1110 ASTDeclContextNameLookupTrait::internal_key_type 1111 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1112 using namespace llvm::support; 1113 1114 auto Kind = (DeclarationName::NameKind)*d++; 1115 uint64_t Data; 1116 switch (Kind) { 1117 case DeclarationName::Identifier: 1118 case DeclarationName::CXXLiteralOperatorName: 1119 case DeclarationName::CXXDeductionGuideName: 1120 Data = (uint64_t)Reader.getLocalIdentifier( 1121 F, endian::readNext<uint32_t, little, unaligned>(d)); 1122 break; 1123 case DeclarationName::ObjCZeroArgSelector: 1124 case DeclarationName::ObjCOneArgSelector: 1125 case DeclarationName::ObjCMultiArgSelector: 1126 Data = 1127 (uint64_t)Reader.getLocalSelector( 1128 F, endian::readNext<uint32_t, little, unaligned>( 1129 d)).getAsOpaquePtr(); 1130 break; 1131 case DeclarationName::CXXOperatorName: 1132 Data = *d++; // OverloadedOperatorKind 1133 break; 1134 case DeclarationName::CXXConstructorName: 1135 case DeclarationName::CXXDestructorName: 1136 case DeclarationName::CXXConversionFunctionName: 1137 case DeclarationName::CXXUsingDirective: 1138 Data = 0; 1139 break; 1140 } 1141 1142 return DeclarationNameKey(Kind, Data); 1143 } 1144 1145 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1146 const unsigned char *d, 1147 unsigned DataLen, 1148 data_type_builder &Val) { 1149 using namespace llvm::support; 1150 1151 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1152 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1153 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1154 } 1155 } 1156 1157 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1158 BitstreamCursor &Cursor, 1159 uint64_t Offset, 1160 DeclContext *DC) { 1161 assert(Offset != 0); 1162 1163 SavedStreamPosition SavedPosition(Cursor); 1164 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1165 Error(std::move(Err)); 1166 return true; 1167 } 1168 1169 RecordData Record; 1170 StringRef Blob; 1171 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1172 if (!MaybeCode) { 1173 Error(MaybeCode.takeError()); 1174 return true; 1175 } 1176 unsigned Code = MaybeCode.get(); 1177 1178 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1179 if (!MaybeRecCode) { 1180 Error(MaybeRecCode.takeError()); 1181 return true; 1182 } 1183 unsigned RecCode = MaybeRecCode.get(); 1184 if (RecCode != DECL_CONTEXT_LEXICAL) { 1185 Error("Expected lexical block"); 1186 return true; 1187 } 1188 1189 assert(!isa<TranslationUnitDecl>(DC) && 1190 "expected a TU_UPDATE_LEXICAL record for TU"); 1191 // If we are handling a C++ class template instantiation, we can see multiple 1192 // lexical updates for the same record. It's important that we select only one 1193 // of them, so that field numbering works properly. Just pick the first one we 1194 // see. 1195 auto &Lex = LexicalDecls[DC]; 1196 if (!Lex.first) { 1197 Lex = std::make_pair( 1198 &M, llvm::makeArrayRef( 1199 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1200 Blob.data()), 1201 Blob.size() / 4)); 1202 } 1203 DC->setHasExternalLexicalStorage(true); 1204 return false; 1205 } 1206 1207 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1208 BitstreamCursor &Cursor, 1209 uint64_t Offset, 1210 DeclID ID) { 1211 assert(Offset != 0); 1212 1213 SavedStreamPosition SavedPosition(Cursor); 1214 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1215 Error(std::move(Err)); 1216 return true; 1217 } 1218 1219 RecordData Record; 1220 StringRef Blob; 1221 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1222 if (!MaybeCode) { 1223 Error(MaybeCode.takeError()); 1224 return true; 1225 } 1226 unsigned Code = MaybeCode.get(); 1227 1228 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1229 if (!MaybeRecCode) { 1230 Error(MaybeRecCode.takeError()); 1231 return true; 1232 } 1233 unsigned RecCode = MaybeRecCode.get(); 1234 if (RecCode != DECL_CONTEXT_VISIBLE) { 1235 Error("Expected visible lookup table block"); 1236 return true; 1237 } 1238 1239 // We can't safely determine the primary context yet, so delay attaching the 1240 // lookup table until we're done with recursive deserialization. 1241 auto *Data = (const unsigned char*)Blob.data(); 1242 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1243 return false; 1244 } 1245 1246 void ASTReader::Error(StringRef Msg) const { 1247 Error(diag::err_fe_pch_malformed, Msg); 1248 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1249 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1250 Diag(diag::note_module_cache_path) 1251 << PP.getHeaderSearchInfo().getModuleCachePath(); 1252 } 1253 } 1254 1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1256 StringRef Arg3) const { 1257 if (Diags.isDiagnosticInFlight()) 1258 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1259 else 1260 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1261 } 1262 1263 void ASTReader::Error(llvm::Error &&Err) const { 1264 llvm::Error RemainingErr = 1265 handleErrors(std::move(Err), [this](const DiagnosticError &E) { 1266 auto Diag = E.getDiagnostic().second; 1267 1268 // Ideally we'd just emit it, but have to handle a possible in-flight 1269 // diagnostic. Note that the location is currently ignored as well. 1270 auto NumArgs = Diag.getStorage()->NumDiagArgs; 1271 assert(NumArgs <= 3 && "Can only have up to 3 arguments"); 1272 StringRef Arg1, Arg2, Arg3; 1273 switch (NumArgs) { 1274 case 3: 1275 Arg3 = Diag.getStringArg(2); 1276 LLVM_FALLTHROUGH; 1277 case 2: 1278 Arg2 = Diag.getStringArg(1); 1279 LLVM_FALLTHROUGH; 1280 case 1: 1281 Arg1 = Diag.getStringArg(0); 1282 } 1283 Error(Diag.getDiagID(), Arg1, Arg2, Arg3); 1284 }); 1285 if (RemainingErr) 1286 Error(toString(std::move(RemainingErr))); 1287 } 1288 1289 //===----------------------------------------------------------------------===// 1290 // Source Manager Deserialization 1291 //===----------------------------------------------------------------------===// 1292 1293 /// Read the line table in the source manager block. 1294 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) { 1295 unsigned Idx = 0; 1296 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1297 1298 // Parse the file names 1299 std::map<int, int> FileIDs; 1300 FileIDs[-1] = -1; // For unspecified filenames. 1301 for (unsigned I = 0; Record[Idx]; ++I) { 1302 // Extract the file name 1303 auto Filename = ReadPath(F, Record, Idx); 1304 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1305 } 1306 ++Idx; 1307 1308 // Parse the line entries 1309 std::vector<LineEntry> Entries; 1310 while (Idx < Record.size()) { 1311 int FID = Record[Idx++]; 1312 assert(FID >= 0 && "Serialized line entries for non-local file."); 1313 // Remap FileID from 1-based old view. 1314 FID += F.SLocEntryBaseID - 1; 1315 1316 // Extract the line entries 1317 unsigned NumEntries = Record[Idx++]; 1318 assert(NumEntries && "no line entries for file ID"); 1319 Entries.clear(); 1320 Entries.reserve(NumEntries); 1321 for (unsigned I = 0; I != NumEntries; ++I) { 1322 unsigned FileOffset = Record[Idx++]; 1323 unsigned LineNo = Record[Idx++]; 1324 int FilenameID = FileIDs[Record[Idx++]]; 1325 SrcMgr::CharacteristicKind FileKind 1326 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1327 unsigned IncludeOffset = Record[Idx++]; 1328 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1329 FileKind, IncludeOffset)); 1330 } 1331 LineTable.AddEntry(FileID::get(FID), Entries); 1332 } 1333 } 1334 1335 /// Read a source manager block 1336 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1337 using namespace SrcMgr; 1338 1339 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1340 1341 // Set the source-location entry cursor to the current position in 1342 // the stream. This cursor will be used to read the contents of the 1343 // source manager block initially, and then lazily read 1344 // source-location entries as needed. 1345 SLocEntryCursor = F.Stream; 1346 1347 // The stream itself is going to skip over the source manager block. 1348 if (llvm::Error Err = F.Stream.SkipBlock()) 1349 return Err; 1350 1351 // Enter the source manager block. 1352 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) 1353 return Err; 1354 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1355 1356 RecordData Record; 1357 while (true) { 1358 Expected<llvm::BitstreamEntry> MaybeE = 1359 SLocEntryCursor.advanceSkippingSubblocks(); 1360 if (!MaybeE) 1361 return MaybeE.takeError(); 1362 llvm::BitstreamEntry E = MaybeE.get(); 1363 1364 switch (E.Kind) { 1365 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1366 case llvm::BitstreamEntry::Error: 1367 return llvm::createStringError(std::errc::illegal_byte_sequence, 1368 "malformed block record in AST file"); 1369 case llvm::BitstreamEntry::EndBlock: 1370 return llvm::Error::success(); 1371 case llvm::BitstreamEntry::Record: 1372 // The interesting case. 1373 break; 1374 } 1375 1376 // Read a record. 1377 Record.clear(); 1378 StringRef Blob; 1379 Expected<unsigned> MaybeRecord = 1380 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1381 if (!MaybeRecord) 1382 return MaybeRecord.takeError(); 1383 switch (MaybeRecord.get()) { 1384 default: // Default behavior: ignore. 1385 break; 1386 1387 case SM_SLOC_FILE_ENTRY: 1388 case SM_SLOC_BUFFER_ENTRY: 1389 case SM_SLOC_EXPANSION_ENTRY: 1390 // Once we hit one of the source location entries, we're done. 1391 return llvm::Error::success(); 1392 } 1393 } 1394 } 1395 1396 /// If a header file is not found at the path that we expect it to be 1397 /// and the PCH file was moved from its original location, try to resolve the 1398 /// file by assuming that header+PCH were moved together and the header is in 1399 /// the same place relative to the PCH. 1400 static std::string 1401 resolveFileRelativeToOriginalDir(const std::string &Filename, 1402 const std::string &OriginalDir, 1403 const std::string &CurrDir) { 1404 assert(OriginalDir != CurrDir && 1405 "No point trying to resolve the file if the PCH dir didn't change"); 1406 1407 using namespace llvm::sys; 1408 1409 SmallString<128> filePath(Filename); 1410 fs::make_absolute(filePath); 1411 assert(path::is_absolute(OriginalDir)); 1412 SmallString<128> currPCHPath(CurrDir); 1413 1414 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1415 fileDirE = path::end(path::parent_path(filePath)); 1416 path::const_iterator origDirI = path::begin(OriginalDir), 1417 origDirE = path::end(OriginalDir); 1418 // Skip the common path components from filePath and OriginalDir. 1419 while (fileDirI != fileDirE && origDirI != origDirE && 1420 *fileDirI == *origDirI) { 1421 ++fileDirI; 1422 ++origDirI; 1423 } 1424 for (; origDirI != origDirE; ++origDirI) 1425 path::append(currPCHPath, ".."); 1426 path::append(currPCHPath, fileDirI, fileDirE); 1427 path::append(currPCHPath, path::filename(Filename)); 1428 return std::string(currPCHPath.str()); 1429 } 1430 1431 bool ASTReader::ReadSLocEntry(int ID) { 1432 if (ID == 0) 1433 return false; 1434 1435 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1436 Error("source location entry ID out-of-range for AST file"); 1437 return true; 1438 } 1439 1440 // Local helper to read the (possibly-compressed) buffer data following the 1441 // entry record. 1442 auto ReadBuffer = [this]( 1443 BitstreamCursor &SLocEntryCursor, 1444 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1445 RecordData Record; 1446 StringRef Blob; 1447 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1448 if (!MaybeCode) { 1449 Error(MaybeCode.takeError()); 1450 return nullptr; 1451 } 1452 unsigned Code = MaybeCode.get(); 1453 1454 Expected<unsigned> MaybeRecCode = 1455 SLocEntryCursor.readRecord(Code, Record, &Blob); 1456 if (!MaybeRecCode) { 1457 Error(MaybeRecCode.takeError()); 1458 return nullptr; 1459 } 1460 unsigned RecCode = MaybeRecCode.get(); 1461 1462 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1463 if (!llvm::zlib::isAvailable()) { 1464 Error("zlib is not available"); 1465 return nullptr; 1466 } 1467 SmallString<0> Uncompressed; 1468 if (llvm::Error E = 1469 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1470 Error("could not decompress embedded file contents: " + 1471 llvm::toString(std::move(E))); 1472 return nullptr; 1473 } 1474 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1475 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1476 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1477 } else { 1478 Error("AST record has invalid code"); 1479 return nullptr; 1480 } 1481 }; 1482 1483 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1484 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1485 F->SLocEntryOffsetsBase + 1486 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1487 Error(std::move(Err)); 1488 return true; 1489 } 1490 1491 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1492 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset; 1493 1494 ++NumSLocEntriesRead; 1495 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1496 if (!MaybeEntry) { 1497 Error(MaybeEntry.takeError()); 1498 return true; 1499 } 1500 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1501 1502 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1503 Error("incorrectly-formatted source location entry in AST file"); 1504 return true; 1505 } 1506 1507 RecordData Record; 1508 StringRef Blob; 1509 Expected<unsigned> MaybeSLOC = 1510 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1511 if (!MaybeSLOC) { 1512 Error(MaybeSLOC.takeError()); 1513 return true; 1514 } 1515 switch (MaybeSLOC.get()) { 1516 default: 1517 Error("incorrectly-formatted source location entry in AST file"); 1518 return true; 1519 1520 case SM_SLOC_FILE_ENTRY: { 1521 // We will detect whether a file changed and return 'Failure' for it, but 1522 // we will also try to fail gracefully by setting up the SLocEntry. 1523 unsigned InputID = Record[4]; 1524 InputFile IF = getInputFile(*F, InputID); 1525 Optional<FileEntryRef> File = IF.getFile(); 1526 bool OverriddenBuffer = IF.isOverridden(); 1527 1528 // Note that we only check if a File was returned. If it was out-of-date 1529 // we have complained but we will continue creating a FileID to recover 1530 // gracefully. 1531 if (!File) 1532 return true; 1533 1534 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1535 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1536 // This is the module's main file. 1537 IncludeLoc = getImportLocation(F); 1538 } 1539 SrcMgr::CharacteristicKind 1540 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1541 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID, 1542 BaseOffset + Record[0]); 1543 SrcMgr::FileInfo &FileInfo = 1544 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1545 FileInfo.NumCreatedFIDs = Record[5]; 1546 if (Record[3]) 1547 FileInfo.setHasLineDirectives(); 1548 1549 unsigned NumFileDecls = Record[7]; 1550 if (NumFileDecls && ContextObj) { 1551 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1552 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1553 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1554 NumFileDecls)); 1555 } 1556 1557 const SrcMgr::ContentCache &ContentCache = 1558 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter)); 1559 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1560 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1561 !ContentCache.getBufferIfLoaded()) { 1562 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1563 if (!Buffer) 1564 return true; 1565 SourceMgr.overrideFileContents(*File, std::move(Buffer)); 1566 } 1567 1568 break; 1569 } 1570 1571 case SM_SLOC_BUFFER_ENTRY: { 1572 const char *Name = Blob.data(); 1573 unsigned Offset = Record[0]; 1574 SrcMgr::CharacteristicKind 1575 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1576 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1577 if (IncludeLoc.isInvalid() && F->isModule()) { 1578 IncludeLoc = getImportLocation(F); 1579 } 1580 1581 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1582 if (!Buffer) 1583 return true; 1584 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1585 BaseOffset + Offset, IncludeLoc); 1586 break; 1587 } 1588 1589 case SM_SLOC_EXPANSION_ENTRY: { 1590 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1591 SourceMgr.createExpansionLoc(SpellingLoc, 1592 ReadSourceLocation(*F, Record[2]), 1593 ReadSourceLocation(*F, Record[3]), 1594 Record[5], 1595 Record[4], 1596 ID, 1597 BaseOffset + Record[0]); 1598 break; 1599 } 1600 } 1601 1602 return false; 1603 } 1604 1605 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1606 if (ID == 0) 1607 return std::make_pair(SourceLocation(), ""); 1608 1609 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1610 Error("source location entry ID out-of-range for AST file"); 1611 return std::make_pair(SourceLocation(), ""); 1612 } 1613 1614 // Find which module file this entry lands in. 1615 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1616 if (!M->isModule()) 1617 return std::make_pair(SourceLocation(), ""); 1618 1619 // FIXME: Can we map this down to a particular submodule? That would be 1620 // ideal. 1621 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1622 } 1623 1624 /// Find the location where the module F is imported. 1625 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1626 if (F->ImportLoc.isValid()) 1627 return F->ImportLoc; 1628 1629 // Otherwise we have a PCH. It's considered to be "imported" at the first 1630 // location of its includer. 1631 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1632 // Main file is the importer. 1633 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1634 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1635 } 1636 return F->ImportedBy[0]->FirstLoc; 1637 } 1638 1639 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1640 /// the abbreviations that are at the top of the block and then leave the cursor 1641 /// pointing into the block. 1642 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, 1643 unsigned BlockID, 1644 uint64_t *StartOfBlockOffset) { 1645 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) 1646 return Err; 1647 1648 if (StartOfBlockOffset) 1649 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1650 1651 while (true) { 1652 uint64_t Offset = Cursor.GetCurrentBitNo(); 1653 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1654 if (!MaybeCode) 1655 return MaybeCode.takeError(); 1656 unsigned Code = MaybeCode.get(); 1657 1658 // We expect all abbrevs to be at the start of the block. 1659 if (Code != llvm::bitc::DEFINE_ABBREV) { 1660 if (llvm::Error Err = Cursor.JumpToBit(Offset)) 1661 return Err; 1662 return llvm::Error::success(); 1663 } 1664 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) 1665 return Err; 1666 } 1667 } 1668 1669 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1670 unsigned &Idx) { 1671 Token Tok; 1672 Tok.startToken(); 1673 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1674 Tok.setLength(Record[Idx++]); 1675 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1676 Tok.setIdentifierInfo(II); 1677 Tok.setKind((tok::TokenKind)Record[Idx++]); 1678 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1679 return Tok; 1680 } 1681 1682 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1683 BitstreamCursor &Stream = F.MacroCursor; 1684 1685 // Keep track of where we are in the stream, then jump back there 1686 // after reading this macro. 1687 SavedStreamPosition SavedPosition(Stream); 1688 1689 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1690 // FIXME this drops errors on the floor. 1691 consumeError(std::move(Err)); 1692 return nullptr; 1693 } 1694 RecordData Record; 1695 SmallVector<IdentifierInfo*, 16> MacroParams; 1696 MacroInfo *Macro = nullptr; 1697 llvm::MutableArrayRef<Token> MacroTokens; 1698 1699 while (true) { 1700 // Advance to the next record, but if we get to the end of the block, don't 1701 // pop it (removing all the abbreviations from the cursor) since we want to 1702 // be able to reseek within the block and read entries. 1703 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1704 Expected<llvm::BitstreamEntry> MaybeEntry = 1705 Stream.advanceSkippingSubblocks(Flags); 1706 if (!MaybeEntry) { 1707 Error(MaybeEntry.takeError()); 1708 return Macro; 1709 } 1710 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1711 1712 switch (Entry.Kind) { 1713 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1714 case llvm::BitstreamEntry::Error: 1715 Error("malformed block record in AST file"); 1716 return Macro; 1717 case llvm::BitstreamEntry::EndBlock: 1718 return Macro; 1719 case llvm::BitstreamEntry::Record: 1720 // The interesting case. 1721 break; 1722 } 1723 1724 // Read a record. 1725 Record.clear(); 1726 PreprocessorRecordTypes RecType; 1727 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1728 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1729 else { 1730 Error(MaybeRecType.takeError()); 1731 return Macro; 1732 } 1733 switch (RecType) { 1734 case PP_MODULE_MACRO: 1735 case PP_MACRO_DIRECTIVE_HISTORY: 1736 return Macro; 1737 1738 case PP_MACRO_OBJECT_LIKE: 1739 case PP_MACRO_FUNCTION_LIKE: { 1740 // If we already have a macro, that means that we've hit the end 1741 // of the definition of the macro we were looking for. We're 1742 // done. 1743 if (Macro) 1744 return Macro; 1745 1746 unsigned NextIndex = 1; // Skip identifier ID. 1747 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1748 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1749 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1750 MI->setIsUsed(Record[NextIndex++]); 1751 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1752 MacroTokens = MI->allocateTokens(Record[NextIndex++], 1753 PP.getPreprocessorAllocator()); 1754 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1755 // Decode function-like macro info. 1756 bool isC99VarArgs = Record[NextIndex++]; 1757 bool isGNUVarArgs = Record[NextIndex++]; 1758 bool hasCommaPasting = Record[NextIndex++]; 1759 MacroParams.clear(); 1760 unsigned NumArgs = Record[NextIndex++]; 1761 for (unsigned i = 0; i != NumArgs; ++i) 1762 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1763 1764 // Install function-like macro info. 1765 MI->setIsFunctionLike(); 1766 if (isC99VarArgs) MI->setIsC99Varargs(); 1767 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1768 if (hasCommaPasting) MI->setHasCommaPasting(); 1769 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1770 } 1771 1772 // Remember that we saw this macro last so that we add the tokens that 1773 // form its body to it. 1774 Macro = MI; 1775 1776 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1777 Record[NextIndex]) { 1778 // We have a macro definition. Register the association 1779 PreprocessedEntityID 1780 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1781 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1782 PreprocessingRecord::PPEntityID PPID = 1783 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1784 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1785 PPRec.getPreprocessedEntity(PPID)); 1786 if (PPDef) 1787 PPRec.RegisterMacroDefinition(Macro, PPDef); 1788 } 1789 1790 ++NumMacrosRead; 1791 break; 1792 } 1793 1794 case PP_TOKEN: { 1795 // If we see a TOKEN before a PP_MACRO_*, then the file is 1796 // erroneous, just pretend we didn't see this. 1797 if (!Macro) break; 1798 if (MacroTokens.empty()) { 1799 Error("unexpected number of macro tokens for a macro in AST file"); 1800 return Macro; 1801 } 1802 1803 unsigned Idx = 0; 1804 MacroTokens[0] = ReadToken(F, Record, Idx); 1805 MacroTokens = MacroTokens.drop_front(); 1806 break; 1807 } 1808 } 1809 } 1810 } 1811 1812 PreprocessedEntityID 1813 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1814 unsigned LocalID) const { 1815 if (!M.ModuleOffsetMap.empty()) 1816 ReadModuleOffsetMap(M); 1817 1818 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1819 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1820 assert(I != M.PreprocessedEntityRemap.end() 1821 && "Invalid index into preprocessed entity index remap"); 1822 1823 return LocalID + I->second; 1824 } 1825 1826 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1827 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1828 } 1829 1830 HeaderFileInfoTrait::internal_key_type 1831 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1832 internal_key_type ikey = {FE->getSize(), 1833 M.HasTimestamps ? FE->getModificationTime() : 0, 1834 FE->getName(), /*Imported*/ false}; 1835 return ikey; 1836 } 1837 1838 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1839 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1840 return false; 1841 1842 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1843 return true; 1844 1845 // Determine whether the actual files are equivalent. 1846 FileManager &FileMgr = Reader.getFileManager(); 1847 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1848 if (!Key.Imported) { 1849 if (auto File = FileMgr.getFile(Key.Filename)) 1850 return *File; 1851 return nullptr; 1852 } 1853 1854 std::string Resolved = std::string(Key.Filename); 1855 Reader.ResolveImportedPath(M, Resolved); 1856 if (auto File = FileMgr.getFile(Resolved)) 1857 return *File; 1858 return nullptr; 1859 }; 1860 1861 const FileEntry *FEA = GetFile(a); 1862 const FileEntry *FEB = GetFile(b); 1863 return FEA && FEA == FEB; 1864 } 1865 1866 std::pair<unsigned, unsigned> 1867 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1868 return readULEBKeyDataLength(d); 1869 } 1870 1871 HeaderFileInfoTrait::internal_key_type 1872 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1873 using namespace llvm::support; 1874 1875 internal_key_type ikey; 1876 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1877 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1878 ikey.Filename = (const char *)d; 1879 ikey.Imported = true; 1880 return ikey; 1881 } 1882 1883 HeaderFileInfoTrait::data_type 1884 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1885 unsigned DataLen) { 1886 using namespace llvm::support; 1887 1888 const unsigned char *End = d + DataLen; 1889 HeaderFileInfo HFI; 1890 unsigned Flags = *d++; 1891 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1892 HFI.isImport |= (Flags >> 5) & 0x01; 1893 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1894 HFI.DirInfo = (Flags >> 1) & 0x07; 1895 HFI.IndexHeaderMapHeader = Flags & 0x01; 1896 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1897 M, endian::readNext<uint32_t, little, unaligned>(d)); 1898 if (unsigned FrameworkOffset = 1899 endian::readNext<uint32_t, little, unaligned>(d)) { 1900 // The framework offset is 1 greater than the actual offset, 1901 // since 0 is used as an indicator for "no framework name". 1902 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1903 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1904 } 1905 1906 assert((End - d) % 4 == 0 && 1907 "Wrong data length in HeaderFileInfo deserialization"); 1908 while (d != End) { 1909 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1910 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1911 LocalSMID >>= 2; 1912 1913 // This header is part of a module. Associate it with the module to enable 1914 // implicit module import. 1915 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1916 Module *Mod = Reader.getSubmodule(GlobalSMID); 1917 FileManager &FileMgr = Reader.getFileManager(); 1918 ModuleMap &ModMap = 1919 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1920 1921 std::string Filename = std::string(key.Filename); 1922 if (key.Imported) 1923 Reader.ResolveImportedPath(M, Filename); 1924 // FIXME: NameAsWritten 1925 Module::Header H = {std::string(key.Filename), "", 1926 *FileMgr.getFile(Filename)}; 1927 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1928 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1929 } 1930 1931 // This HeaderFileInfo was externally loaded. 1932 HFI.External = true; 1933 HFI.IsValid = true; 1934 return HFI; 1935 } 1936 1937 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1938 uint32_t MacroDirectivesOffset) { 1939 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1940 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1941 } 1942 1943 void ASTReader::ReadDefinedMacros() { 1944 // Note that we are loading defined macros. 1945 Deserializing Macros(this); 1946 1947 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1948 BitstreamCursor &MacroCursor = I.MacroCursor; 1949 1950 // If there was no preprocessor block, skip this file. 1951 if (MacroCursor.getBitcodeBytes().empty()) 1952 continue; 1953 1954 BitstreamCursor Cursor = MacroCursor; 1955 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1956 Error(std::move(Err)); 1957 return; 1958 } 1959 1960 RecordData Record; 1961 while (true) { 1962 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1963 if (!MaybeE) { 1964 Error(MaybeE.takeError()); 1965 return; 1966 } 1967 llvm::BitstreamEntry E = MaybeE.get(); 1968 1969 switch (E.Kind) { 1970 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1971 case llvm::BitstreamEntry::Error: 1972 Error("malformed block record in AST file"); 1973 return; 1974 case llvm::BitstreamEntry::EndBlock: 1975 goto NextCursor; 1976 1977 case llvm::BitstreamEntry::Record: { 1978 Record.clear(); 1979 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1980 if (!MaybeRecord) { 1981 Error(MaybeRecord.takeError()); 1982 return; 1983 } 1984 switch (MaybeRecord.get()) { 1985 default: // Default behavior: ignore. 1986 break; 1987 1988 case PP_MACRO_OBJECT_LIKE: 1989 case PP_MACRO_FUNCTION_LIKE: { 1990 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1991 if (II->isOutOfDate()) 1992 updateOutOfDateIdentifier(*II); 1993 break; 1994 } 1995 1996 case PP_TOKEN: 1997 // Ignore tokens. 1998 break; 1999 } 2000 break; 2001 } 2002 } 2003 } 2004 NextCursor: ; 2005 } 2006 } 2007 2008 namespace { 2009 2010 /// Visitor class used to look up identifirs in an AST file. 2011 class IdentifierLookupVisitor { 2012 StringRef Name; 2013 unsigned NameHash; 2014 unsigned PriorGeneration; 2015 unsigned &NumIdentifierLookups; 2016 unsigned &NumIdentifierLookupHits; 2017 IdentifierInfo *Found = nullptr; 2018 2019 public: 2020 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2021 unsigned &NumIdentifierLookups, 2022 unsigned &NumIdentifierLookupHits) 2023 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2024 PriorGeneration(PriorGeneration), 2025 NumIdentifierLookups(NumIdentifierLookups), 2026 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2027 2028 bool operator()(ModuleFile &M) { 2029 // If we've already searched this module file, skip it now. 2030 if (M.Generation <= PriorGeneration) 2031 return true; 2032 2033 ASTIdentifierLookupTable *IdTable 2034 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2035 if (!IdTable) 2036 return false; 2037 2038 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2039 Found); 2040 ++NumIdentifierLookups; 2041 ASTIdentifierLookupTable::iterator Pos = 2042 IdTable->find_hashed(Name, NameHash, &Trait); 2043 if (Pos == IdTable->end()) 2044 return false; 2045 2046 // Dereferencing the iterator has the effect of building the 2047 // IdentifierInfo node and populating it with the various 2048 // declarations it needs. 2049 ++NumIdentifierLookupHits; 2050 Found = *Pos; 2051 return true; 2052 } 2053 2054 // Retrieve the identifier info found within the module 2055 // files. 2056 IdentifierInfo *getIdentifierInfo() const { return Found; } 2057 }; 2058 2059 } // namespace 2060 2061 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2062 // Note that we are loading an identifier. 2063 Deserializing AnIdentifier(this); 2064 2065 unsigned PriorGeneration = 0; 2066 if (getContext().getLangOpts().Modules) 2067 PriorGeneration = IdentifierGeneration[&II]; 2068 2069 // If there is a global index, look there first to determine which modules 2070 // provably do not have any results for this identifier. 2071 GlobalModuleIndex::HitSet Hits; 2072 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2073 if (!loadGlobalIndex()) { 2074 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2075 HitsPtr = &Hits; 2076 } 2077 } 2078 2079 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2080 NumIdentifierLookups, 2081 NumIdentifierLookupHits); 2082 ModuleMgr.visit(Visitor, HitsPtr); 2083 markIdentifierUpToDate(&II); 2084 } 2085 2086 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2087 if (!II) 2088 return; 2089 2090 II->setOutOfDate(false); 2091 2092 // Update the generation for this identifier. 2093 if (getContext().getLangOpts().Modules) 2094 IdentifierGeneration[II] = getGeneration(); 2095 } 2096 2097 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2098 const PendingMacroInfo &PMInfo) { 2099 ModuleFile &M = *PMInfo.M; 2100 2101 BitstreamCursor &Cursor = M.MacroCursor; 2102 SavedStreamPosition SavedPosition(Cursor); 2103 if (llvm::Error Err = 2104 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2105 Error(std::move(Err)); 2106 return; 2107 } 2108 2109 struct ModuleMacroRecord { 2110 SubmoduleID SubModID; 2111 MacroInfo *MI; 2112 SmallVector<SubmoduleID, 8> Overrides; 2113 }; 2114 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2115 2116 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2117 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2118 // macro histroy. 2119 RecordData Record; 2120 while (true) { 2121 Expected<llvm::BitstreamEntry> MaybeEntry = 2122 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2123 if (!MaybeEntry) { 2124 Error(MaybeEntry.takeError()); 2125 return; 2126 } 2127 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2128 2129 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2130 Error("malformed block record in AST file"); 2131 return; 2132 } 2133 2134 Record.clear(); 2135 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2136 if (!MaybePP) { 2137 Error(MaybePP.takeError()); 2138 return; 2139 } 2140 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2141 case PP_MACRO_DIRECTIVE_HISTORY: 2142 break; 2143 2144 case PP_MODULE_MACRO: { 2145 ModuleMacros.push_back(ModuleMacroRecord()); 2146 auto &Info = ModuleMacros.back(); 2147 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2148 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2149 for (int I = 2, N = Record.size(); I != N; ++I) 2150 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2151 continue; 2152 } 2153 2154 default: 2155 Error("malformed block record in AST file"); 2156 return; 2157 } 2158 2159 // We found the macro directive history; that's the last record 2160 // for this macro. 2161 break; 2162 } 2163 2164 // Module macros are listed in reverse dependency order. 2165 { 2166 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2167 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2168 for (auto &MMR : ModuleMacros) { 2169 Overrides.clear(); 2170 for (unsigned ModID : MMR.Overrides) { 2171 Module *Mod = getSubmodule(ModID); 2172 auto *Macro = PP.getModuleMacro(Mod, II); 2173 assert(Macro && "missing definition for overridden macro"); 2174 Overrides.push_back(Macro); 2175 } 2176 2177 bool Inserted = false; 2178 Module *Owner = getSubmodule(MMR.SubModID); 2179 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2180 } 2181 } 2182 2183 // Don't read the directive history for a module; we don't have anywhere 2184 // to put it. 2185 if (M.isModule()) 2186 return; 2187 2188 // Deserialize the macro directives history in reverse source-order. 2189 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2190 unsigned Idx = 0, N = Record.size(); 2191 while (Idx < N) { 2192 MacroDirective *MD = nullptr; 2193 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2194 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2195 switch (K) { 2196 case MacroDirective::MD_Define: { 2197 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2198 MD = PP.AllocateDefMacroDirective(MI, Loc); 2199 break; 2200 } 2201 case MacroDirective::MD_Undefine: 2202 MD = PP.AllocateUndefMacroDirective(Loc); 2203 break; 2204 case MacroDirective::MD_Visibility: 2205 bool isPublic = Record[Idx++]; 2206 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2207 break; 2208 } 2209 2210 if (!Latest) 2211 Latest = MD; 2212 if (Earliest) 2213 Earliest->setPrevious(MD); 2214 Earliest = MD; 2215 } 2216 2217 if (Latest) 2218 PP.setLoadedMacroDirective(II, Earliest, Latest); 2219 } 2220 2221 bool ASTReader::shouldDisableValidationForFile( 2222 const serialization::ModuleFile &M) const { 2223 if (DisableValidationKind == DisableValidationForModuleKind::None) 2224 return false; 2225 2226 // If a PCH is loaded and validation is disabled for PCH then disable 2227 // validation for the PCH and the modules it loads. 2228 ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind); 2229 2230 switch (K) { 2231 case MK_MainFile: 2232 case MK_Preamble: 2233 case MK_PCH: 2234 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH); 2235 case MK_ImplicitModule: 2236 case MK_ExplicitModule: 2237 case MK_PrebuiltModule: 2238 return bool(DisableValidationKind & DisableValidationForModuleKind::Module); 2239 } 2240 2241 return false; 2242 } 2243 2244 ASTReader::InputFileInfo 2245 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2246 // Go find this input file. 2247 BitstreamCursor &Cursor = F.InputFilesCursor; 2248 SavedStreamPosition SavedPosition(Cursor); 2249 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2250 // FIXME this drops errors on the floor. 2251 consumeError(std::move(Err)); 2252 } 2253 2254 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2255 if (!MaybeCode) { 2256 // FIXME this drops errors on the floor. 2257 consumeError(MaybeCode.takeError()); 2258 } 2259 unsigned Code = MaybeCode.get(); 2260 RecordData Record; 2261 StringRef Blob; 2262 2263 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2264 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2265 "invalid record type for input file"); 2266 else { 2267 // FIXME this drops errors on the floor. 2268 consumeError(Maybe.takeError()); 2269 } 2270 2271 assert(Record[0] == ID && "Bogus stored ID or offset"); 2272 InputFileInfo R; 2273 R.StoredSize = static_cast<off_t>(Record[1]); 2274 R.StoredTime = static_cast<time_t>(Record[2]); 2275 R.Overridden = static_cast<bool>(Record[3]); 2276 R.Transient = static_cast<bool>(Record[4]); 2277 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2278 R.Filename = std::string(Blob); 2279 ResolveImportedPath(F, R.Filename); 2280 2281 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2282 if (!MaybeEntry) // FIXME this drops errors on the floor. 2283 consumeError(MaybeEntry.takeError()); 2284 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2285 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2286 "expected record type for input file hash"); 2287 2288 Record.clear(); 2289 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2290 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2291 "invalid record type for input file hash"); 2292 else { 2293 // FIXME this drops errors on the floor. 2294 consumeError(Maybe.takeError()); 2295 } 2296 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2297 static_cast<uint64_t>(Record[0]); 2298 return R; 2299 } 2300 2301 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2302 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2303 // If this ID is bogus, just return an empty input file. 2304 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2305 return InputFile(); 2306 2307 // If we've already loaded this input file, return it. 2308 if (F.InputFilesLoaded[ID-1].getFile()) 2309 return F.InputFilesLoaded[ID-1]; 2310 2311 if (F.InputFilesLoaded[ID-1].isNotFound()) 2312 return InputFile(); 2313 2314 // Go find this input file. 2315 BitstreamCursor &Cursor = F.InputFilesCursor; 2316 SavedStreamPosition SavedPosition(Cursor); 2317 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2318 // FIXME this drops errors on the floor. 2319 consumeError(std::move(Err)); 2320 } 2321 2322 InputFileInfo FI = readInputFileInfo(F, ID); 2323 off_t StoredSize = FI.StoredSize; 2324 time_t StoredTime = FI.StoredTime; 2325 bool Overridden = FI.Overridden; 2326 bool Transient = FI.Transient; 2327 StringRef Filename = FI.Filename; 2328 uint64_t StoredContentHash = FI.ContentHash; 2329 2330 OptionalFileEntryRefDegradesToFileEntryPtr File = 2331 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2332 2333 // If we didn't find the file, resolve it relative to the 2334 // original directory from which this AST file was created. 2335 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2336 F.OriginalDir != F.BaseDirectory) { 2337 std::string Resolved = resolveFileRelativeToOriginalDir( 2338 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2339 if (!Resolved.empty()) 2340 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2341 } 2342 2343 // For an overridden file, create a virtual file with the stored 2344 // size/timestamp. 2345 if ((Overridden || Transient) && !File) 2346 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2347 2348 if (!File) { 2349 if (Complain) { 2350 std::string ErrorStr = "could not find file '"; 2351 ErrorStr += Filename; 2352 ErrorStr += "' referenced by AST file '"; 2353 ErrorStr += F.FileName; 2354 ErrorStr += "'"; 2355 Error(ErrorStr); 2356 } 2357 // Record that we didn't find the file. 2358 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2359 return InputFile(); 2360 } 2361 2362 // Check if there was a request to override the contents of the file 2363 // that was part of the precompiled header. Overriding such a file 2364 // can lead to problems when lexing using the source locations from the 2365 // PCH. 2366 SourceManager &SM = getSourceManager(); 2367 // FIXME: Reject if the overrides are different. 2368 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2369 if (Complain) 2370 Error(diag::err_fe_pch_file_overridden, Filename); 2371 2372 // After emitting the diagnostic, bypass the overriding file to recover 2373 // (this creates a separate FileEntry). 2374 File = SM.bypassFileContentsOverride(*File); 2375 if (!File) { 2376 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2377 return InputFile(); 2378 } 2379 } 2380 2381 struct Change { 2382 enum ModificationKind { 2383 Size, 2384 ModTime, 2385 Content, 2386 None, 2387 } Kind; 2388 llvm::Optional<int64_t> Old = llvm::None; 2389 llvm::Optional<int64_t> New = llvm::None; 2390 }; 2391 auto HasInputFileChanged = [&]() { 2392 if (StoredSize != File->getSize()) 2393 return Change{Change::Size, StoredSize, File->getSize()}; 2394 if (!shouldDisableValidationForFile(F) && StoredTime && 2395 StoredTime != File->getModificationTime()) { 2396 Change MTimeChange = {Change::ModTime, StoredTime, 2397 File->getModificationTime()}; 2398 2399 // In case the modification time changes but not the content, 2400 // accept the cached file as legit. 2401 if (ValidateASTInputFilesContent && 2402 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2403 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2404 if (!MemBuffOrError) { 2405 if (!Complain) 2406 return MTimeChange; 2407 std::string ErrorStr = "could not get buffer for file '"; 2408 ErrorStr += File->getName(); 2409 ErrorStr += "'"; 2410 Error(ErrorStr); 2411 return MTimeChange; 2412 } 2413 2414 // FIXME: hash_value is not guaranteed to be stable! 2415 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2416 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2417 return Change{Change::None}; 2418 2419 return Change{Change::Content}; 2420 } 2421 return MTimeChange; 2422 } 2423 return Change{Change::None}; 2424 }; 2425 2426 bool IsOutOfDate = false; 2427 auto FileChange = HasInputFileChanged(); 2428 // For an overridden file, there is nothing to validate. 2429 if (!Overridden && FileChange.Kind != Change::None) { 2430 if (Complain && !Diags.isDiagnosticInFlight()) { 2431 // Build a list of the PCH imports that got us here (in reverse). 2432 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2433 while (!ImportStack.back()->ImportedBy.empty()) 2434 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2435 2436 // The top-level PCH is stale. 2437 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2438 Diag(diag::err_fe_ast_file_modified) 2439 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2440 << TopLevelPCHName << FileChange.Kind 2441 << (FileChange.Old && FileChange.New) 2442 << llvm::itostr(FileChange.Old.getValueOr(0)) 2443 << llvm::itostr(FileChange.New.getValueOr(0)); 2444 2445 // Print the import stack. 2446 if (ImportStack.size() > 1) { 2447 Diag(diag::note_pch_required_by) 2448 << Filename << ImportStack[0]->FileName; 2449 for (unsigned I = 1; I < ImportStack.size(); ++I) 2450 Diag(diag::note_pch_required_by) 2451 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2452 } 2453 2454 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2455 } 2456 2457 IsOutOfDate = true; 2458 } 2459 // FIXME: If the file is overridden and we've already opened it, 2460 // issue an error (or split it into a separate FileEntry). 2461 2462 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2463 2464 // Note that we've loaded this input file. 2465 F.InputFilesLoaded[ID-1] = IF; 2466 return IF; 2467 } 2468 2469 /// If we are loading a relocatable PCH or module file, and the filename 2470 /// is not an absolute path, add the system or module root to the beginning of 2471 /// the file name. 2472 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2473 // Resolve relative to the base directory, if we have one. 2474 if (!M.BaseDirectory.empty()) 2475 return ResolveImportedPath(Filename, M.BaseDirectory); 2476 } 2477 2478 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2479 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2480 return; 2481 2482 SmallString<128> Buffer; 2483 llvm::sys::path::append(Buffer, Prefix, Filename); 2484 Filename.assign(Buffer.begin(), Buffer.end()); 2485 } 2486 2487 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2488 switch (ARR) { 2489 case ASTReader::Failure: return true; 2490 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2491 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2492 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2493 case ASTReader::ConfigurationMismatch: 2494 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2495 case ASTReader::HadErrors: return true; 2496 case ASTReader::Success: return false; 2497 } 2498 2499 llvm_unreachable("unknown ASTReadResult"); 2500 } 2501 2502 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2503 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2504 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2505 std::string &SuggestedPredefines) { 2506 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2507 // FIXME this drops errors on the floor. 2508 consumeError(std::move(Err)); 2509 return Failure; 2510 } 2511 2512 // Read all of the records in the options block. 2513 RecordData Record; 2514 ASTReadResult Result = Success; 2515 while (true) { 2516 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2517 if (!MaybeEntry) { 2518 // FIXME this drops errors on the floor. 2519 consumeError(MaybeEntry.takeError()); 2520 return Failure; 2521 } 2522 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2523 2524 switch (Entry.Kind) { 2525 case llvm::BitstreamEntry::Error: 2526 case llvm::BitstreamEntry::SubBlock: 2527 return Failure; 2528 2529 case llvm::BitstreamEntry::EndBlock: 2530 return Result; 2531 2532 case llvm::BitstreamEntry::Record: 2533 // The interesting case. 2534 break; 2535 } 2536 2537 // Read and process a record. 2538 Record.clear(); 2539 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2540 if (!MaybeRecordType) { 2541 // FIXME this drops errors on the floor. 2542 consumeError(MaybeRecordType.takeError()); 2543 return Failure; 2544 } 2545 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2546 case LANGUAGE_OPTIONS: { 2547 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2548 if (ParseLanguageOptions(Record, Complain, Listener, 2549 AllowCompatibleConfigurationMismatch)) 2550 Result = ConfigurationMismatch; 2551 break; 2552 } 2553 2554 case TARGET_OPTIONS: { 2555 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2556 if (ParseTargetOptions(Record, Complain, Listener, 2557 AllowCompatibleConfigurationMismatch)) 2558 Result = ConfigurationMismatch; 2559 break; 2560 } 2561 2562 case FILE_SYSTEM_OPTIONS: { 2563 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2564 if (!AllowCompatibleConfigurationMismatch && 2565 ParseFileSystemOptions(Record, Complain, Listener)) 2566 Result = ConfigurationMismatch; 2567 break; 2568 } 2569 2570 case HEADER_SEARCH_OPTIONS: { 2571 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2572 if (!AllowCompatibleConfigurationMismatch && 2573 ParseHeaderSearchOptions(Record, Complain, Listener)) 2574 Result = ConfigurationMismatch; 2575 break; 2576 } 2577 2578 case PREPROCESSOR_OPTIONS: 2579 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2580 if (!AllowCompatibleConfigurationMismatch && 2581 ParsePreprocessorOptions(Record, Complain, Listener, 2582 SuggestedPredefines)) 2583 Result = ConfigurationMismatch; 2584 break; 2585 } 2586 } 2587 } 2588 2589 ASTReader::ASTReadResult 2590 ASTReader::ReadControlBlock(ModuleFile &F, 2591 SmallVectorImpl<ImportedModule> &Loaded, 2592 const ModuleFile *ImportedBy, 2593 unsigned ClientLoadCapabilities) { 2594 BitstreamCursor &Stream = F.Stream; 2595 2596 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2597 Error(std::move(Err)); 2598 return Failure; 2599 } 2600 2601 // Lambda to read the unhashed control block the first time it's called. 2602 // 2603 // For PCM files, the unhashed control block cannot be read until after the 2604 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2605 // need to look ahead before reading the IMPORTS record. For consistency, 2606 // this block is always read somehow (see BitstreamEntry::EndBlock). 2607 bool HasReadUnhashedControlBlock = false; 2608 auto readUnhashedControlBlockOnce = [&]() { 2609 if (!HasReadUnhashedControlBlock) { 2610 HasReadUnhashedControlBlock = true; 2611 if (ASTReadResult Result = 2612 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2613 return Result; 2614 } 2615 return Success; 2616 }; 2617 2618 bool DisableValidation = shouldDisableValidationForFile(F); 2619 2620 // Read all of the records and blocks in the control block. 2621 RecordData Record; 2622 unsigned NumInputs = 0; 2623 unsigned NumUserInputs = 0; 2624 StringRef BaseDirectoryAsWritten; 2625 while (true) { 2626 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2627 if (!MaybeEntry) { 2628 Error(MaybeEntry.takeError()); 2629 return Failure; 2630 } 2631 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2632 2633 switch (Entry.Kind) { 2634 case llvm::BitstreamEntry::Error: 2635 Error("malformed block record in AST file"); 2636 return Failure; 2637 case llvm::BitstreamEntry::EndBlock: { 2638 // Validate the module before returning. This call catches an AST with 2639 // no module name and no imports. 2640 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2641 return Result; 2642 2643 // Validate input files. 2644 const HeaderSearchOptions &HSOpts = 2645 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2646 2647 // All user input files reside at the index range [0, NumUserInputs), and 2648 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2649 // loaded module files, ignore missing inputs. 2650 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2651 F.Kind != MK_PrebuiltModule) { 2652 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2653 2654 // If we are reading a module, we will create a verification timestamp, 2655 // so we verify all input files. Otherwise, verify only user input 2656 // files. 2657 2658 unsigned N = NumUserInputs; 2659 if (ValidateSystemInputs || 2660 (HSOpts.ModulesValidateOncePerBuildSession && 2661 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2662 F.Kind == MK_ImplicitModule)) 2663 N = NumInputs; 2664 2665 for (unsigned I = 0; I < N; ++I) { 2666 InputFile IF = getInputFile(F, I+1, Complain); 2667 if (!IF.getFile() || IF.isOutOfDate()) 2668 return OutOfDate; 2669 } 2670 } 2671 2672 if (Listener) 2673 Listener->visitModuleFile(F.FileName, F.Kind); 2674 2675 if (Listener && Listener->needsInputFileVisitation()) { 2676 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2677 : NumUserInputs; 2678 for (unsigned I = 0; I < N; ++I) { 2679 bool IsSystem = I >= NumUserInputs; 2680 InputFileInfo FI = readInputFileInfo(F, I+1); 2681 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2682 F.Kind == MK_ExplicitModule || 2683 F.Kind == MK_PrebuiltModule); 2684 } 2685 } 2686 2687 return Success; 2688 } 2689 2690 case llvm::BitstreamEntry::SubBlock: 2691 switch (Entry.ID) { 2692 case INPUT_FILES_BLOCK_ID: 2693 F.InputFilesCursor = Stream; 2694 if (llvm::Error Err = Stream.SkipBlock()) { 2695 Error(std::move(Err)); 2696 return Failure; 2697 } 2698 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2699 Error("malformed block record in AST file"); 2700 return Failure; 2701 } 2702 continue; 2703 2704 case OPTIONS_BLOCK_ID: 2705 // If we're reading the first module for this group, check its options 2706 // are compatible with ours. For modules it imports, no further checking 2707 // is required, because we checked them when we built it. 2708 if (Listener && !ImportedBy) { 2709 // Should we allow the configuration of the module file to differ from 2710 // the configuration of the current translation unit in a compatible 2711 // way? 2712 // 2713 // FIXME: Allow this for files explicitly specified with -include-pch. 2714 bool AllowCompatibleConfigurationMismatch = 2715 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2716 2717 ASTReadResult Result = 2718 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2719 AllowCompatibleConfigurationMismatch, *Listener, 2720 SuggestedPredefines); 2721 if (Result == Failure) { 2722 Error("malformed block record in AST file"); 2723 return Result; 2724 } 2725 2726 if (DisableValidation || 2727 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2728 Result = Success; 2729 2730 // If we can't load the module, exit early since we likely 2731 // will rebuild the module anyway. The stream may be in the 2732 // middle of a block. 2733 if (Result != Success) 2734 return Result; 2735 } else if (llvm::Error Err = Stream.SkipBlock()) { 2736 Error(std::move(Err)); 2737 return Failure; 2738 } 2739 continue; 2740 2741 default: 2742 if (llvm::Error Err = Stream.SkipBlock()) { 2743 Error(std::move(Err)); 2744 return Failure; 2745 } 2746 continue; 2747 } 2748 2749 case llvm::BitstreamEntry::Record: 2750 // The interesting case. 2751 break; 2752 } 2753 2754 // Read and process a record. 2755 Record.clear(); 2756 StringRef Blob; 2757 Expected<unsigned> MaybeRecordType = 2758 Stream.readRecord(Entry.ID, Record, &Blob); 2759 if (!MaybeRecordType) { 2760 Error(MaybeRecordType.takeError()); 2761 return Failure; 2762 } 2763 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2764 case METADATA: { 2765 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2766 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2767 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2768 : diag::err_pch_version_too_new); 2769 return VersionMismatch; 2770 } 2771 2772 bool hasErrors = Record[6]; 2773 if (hasErrors && !DisableValidation) { 2774 // If requested by the caller and the module hasn't already been read 2775 // or compiled, mark modules on error as out-of-date. 2776 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) && 2777 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2778 return OutOfDate; 2779 2780 if (!AllowASTWithCompilerErrors) { 2781 Diag(diag::err_pch_with_compiler_errors); 2782 return HadErrors; 2783 } 2784 } 2785 if (hasErrors) { 2786 Diags.ErrorOccurred = true; 2787 Diags.UncompilableErrorOccurred = true; 2788 Diags.UnrecoverableErrorOccurred = true; 2789 } 2790 2791 F.RelocatablePCH = Record[4]; 2792 // Relative paths in a relocatable PCH are relative to our sysroot. 2793 if (F.RelocatablePCH) 2794 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2795 2796 F.HasTimestamps = Record[5]; 2797 2798 const std::string &CurBranch = getClangFullRepositoryVersion(); 2799 StringRef ASTBranch = Blob; 2800 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2801 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2802 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2803 return VersionMismatch; 2804 } 2805 break; 2806 } 2807 2808 case IMPORTS: { 2809 // Validate the AST before processing any imports (otherwise, untangling 2810 // them can be error-prone and expensive). A module will have a name and 2811 // will already have been validated, but this catches the PCH case. 2812 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2813 return Result; 2814 2815 // Load each of the imported PCH files. 2816 unsigned Idx = 0, N = Record.size(); 2817 while (Idx < N) { 2818 // Read information about the AST file. 2819 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2820 // The import location will be the local one for now; we will adjust 2821 // all import locations of module imports after the global source 2822 // location info are setup, in ReadAST. 2823 SourceLocation ImportLoc = 2824 ReadUntranslatedSourceLocation(Record[Idx++]); 2825 off_t StoredSize = (off_t)Record[Idx++]; 2826 time_t StoredModTime = (time_t)Record[Idx++]; 2827 auto FirstSignatureByte = Record.begin() + Idx; 2828 ASTFileSignature StoredSignature = ASTFileSignature::create( 2829 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2830 Idx += ASTFileSignature::size; 2831 2832 std::string ImportedName = ReadString(Record, Idx); 2833 std::string ImportedFile; 2834 2835 // For prebuilt and explicit modules first consult the file map for 2836 // an override. Note that here we don't search prebuilt module 2837 // directories, only the explicit name to file mappings. Also, we will 2838 // still verify the size/signature making sure it is essentially the 2839 // same file but perhaps in a different location. 2840 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2841 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2842 ImportedName, /*FileMapOnly*/ true); 2843 2844 if (ImportedFile.empty()) 2845 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2846 // ModuleCache as when writing. 2847 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2848 else 2849 SkipPath(Record, Idx); 2850 2851 // If our client can't cope with us being out of date, we can't cope with 2852 // our dependency being missing. 2853 unsigned Capabilities = ClientLoadCapabilities; 2854 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2855 Capabilities &= ~ARR_Missing; 2856 2857 // Load the AST file. 2858 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2859 Loaded, StoredSize, StoredModTime, 2860 StoredSignature, Capabilities); 2861 2862 // If we diagnosed a problem, produce a backtrace. 2863 bool recompilingFinalized = 2864 Result == OutOfDate && (Capabilities & ARR_OutOfDate) && 2865 getModuleManager().getModuleCache().isPCMFinal(F.FileName); 2866 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized) 2867 Diag(diag::note_module_file_imported_by) 2868 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2869 if (recompilingFinalized) 2870 Diag(diag::note_module_file_conflict); 2871 2872 switch (Result) { 2873 case Failure: return Failure; 2874 // If we have to ignore the dependency, we'll have to ignore this too. 2875 case Missing: 2876 case OutOfDate: return OutOfDate; 2877 case VersionMismatch: return VersionMismatch; 2878 case ConfigurationMismatch: return ConfigurationMismatch; 2879 case HadErrors: return HadErrors; 2880 case Success: break; 2881 } 2882 } 2883 break; 2884 } 2885 2886 case ORIGINAL_FILE: 2887 F.OriginalSourceFileID = FileID::get(Record[0]); 2888 F.ActualOriginalSourceFileName = std::string(Blob); 2889 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2890 ResolveImportedPath(F, F.OriginalSourceFileName); 2891 break; 2892 2893 case ORIGINAL_FILE_ID: 2894 F.OriginalSourceFileID = FileID::get(Record[0]); 2895 break; 2896 2897 case ORIGINAL_PCH_DIR: 2898 F.OriginalDir = std::string(Blob); 2899 break; 2900 2901 case MODULE_NAME: 2902 F.ModuleName = std::string(Blob); 2903 Diag(diag::remark_module_import) 2904 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2905 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2906 if (Listener) 2907 Listener->ReadModuleName(F.ModuleName); 2908 2909 // Validate the AST as soon as we have a name so we can exit early on 2910 // failure. 2911 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2912 return Result; 2913 2914 break; 2915 2916 case MODULE_DIRECTORY: { 2917 // Save the BaseDirectory as written in the PCM for computing the module 2918 // filename for the ModuleCache. 2919 BaseDirectoryAsWritten = Blob; 2920 assert(!F.ModuleName.empty() && 2921 "MODULE_DIRECTORY found before MODULE_NAME"); 2922 // If we've already loaded a module map file covering this module, we may 2923 // have a better path for it (relative to the current build). 2924 Module *M = PP.getHeaderSearchInfo().lookupModule( 2925 F.ModuleName, SourceLocation(), /*AllowSearch*/ true, 2926 /*AllowExtraModuleMapSearch*/ true); 2927 if (M && M->Directory) { 2928 // If we're implicitly loading a module, the base directory can't 2929 // change between the build and use. 2930 // Don't emit module relocation error if we have -fno-validate-pch 2931 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 2932 DisableValidationForModuleKind::Module) && 2933 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2934 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2935 if (!BuildDir || *BuildDir != M->Directory) { 2936 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2937 Diag(diag::err_imported_module_relocated) 2938 << F.ModuleName << Blob << M->Directory->getName(); 2939 return OutOfDate; 2940 } 2941 } 2942 F.BaseDirectory = std::string(M->Directory->getName()); 2943 } else { 2944 F.BaseDirectory = std::string(Blob); 2945 } 2946 break; 2947 } 2948 2949 case MODULE_MAP_FILE: 2950 if (ASTReadResult Result = 2951 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2952 return Result; 2953 break; 2954 2955 case INPUT_FILE_OFFSETS: 2956 NumInputs = Record[0]; 2957 NumUserInputs = Record[1]; 2958 F.InputFileOffsets = 2959 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2960 F.InputFilesLoaded.resize(NumInputs); 2961 F.NumUserInputFiles = NumUserInputs; 2962 break; 2963 } 2964 } 2965 } 2966 2967 void ASTReader::readIncludedFiles(ModuleFile &F, StringRef Blob, 2968 Preprocessor &PP) { 2969 using namespace llvm::support; 2970 2971 const unsigned char *D = (const unsigned char *)Blob.data(); 2972 unsigned FileCount = endian::readNext<uint32_t, little, unaligned>(D); 2973 2974 for (unsigned I = 0; I < FileCount; ++I) { 2975 size_t ID = endian::readNext<uint32_t, little, unaligned>(D); 2976 InputFileInfo IFI = readInputFileInfo(F, ID); 2977 if (llvm::ErrorOr<const FileEntry *> File = 2978 PP.getFileManager().getFile(IFI.Filename)) 2979 PP.getIncludedFiles().insert(*File); 2980 } 2981 } 2982 2983 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, 2984 unsigned ClientLoadCapabilities) { 2985 BitstreamCursor &Stream = F.Stream; 2986 2987 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) 2988 return Err; 2989 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2990 2991 // Read all of the records and blocks for the AST file. 2992 RecordData Record; 2993 while (true) { 2994 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2995 if (!MaybeEntry) 2996 return MaybeEntry.takeError(); 2997 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2998 2999 switch (Entry.Kind) { 3000 case llvm::BitstreamEntry::Error: 3001 return llvm::createStringError( 3002 std::errc::illegal_byte_sequence, 3003 "error at end of module block in AST file"); 3004 case llvm::BitstreamEntry::EndBlock: 3005 // Outside of C++, we do not store a lookup map for the translation unit. 3006 // Instead, mark it as needing a lookup map to be built if this module 3007 // contains any declarations lexically within it (which it always does!). 3008 // This usually has no cost, since we very rarely need the lookup map for 3009 // the translation unit outside C++. 3010 if (ASTContext *Ctx = ContextObj) { 3011 DeclContext *DC = Ctx->getTranslationUnitDecl(); 3012 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 3013 DC->setMustBuildLookupTable(); 3014 } 3015 3016 return llvm::Error::success(); 3017 case llvm::BitstreamEntry::SubBlock: 3018 switch (Entry.ID) { 3019 case DECLTYPES_BLOCK_ID: 3020 // We lazily load the decls block, but we want to set up the 3021 // DeclsCursor cursor to point into it. Clone our current bitcode 3022 // cursor to it, enter the block and read the abbrevs in that block. 3023 // With the main cursor, we just skip over it. 3024 F.DeclsCursor = Stream; 3025 if (llvm::Error Err = Stream.SkipBlock()) 3026 return Err; 3027 if (llvm::Error Err = ReadBlockAbbrevs( 3028 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset)) 3029 return Err; 3030 break; 3031 3032 case PREPROCESSOR_BLOCK_ID: 3033 F.MacroCursor = Stream; 3034 if (!PP.getExternalSource()) 3035 PP.setExternalSource(this); 3036 3037 if (llvm::Error Err = Stream.SkipBlock()) 3038 return Err; 3039 if (llvm::Error Err = 3040 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) 3041 return Err; 3042 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3043 break; 3044 3045 case PREPROCESSOR_DETAIL_BLOCK_ID: 3046 F.PreprocessorDetailCursor = Stream; 3047 3048 if (llvm::Error Err = Stream.SkipBlock()) { 3049 return Err; 3050 } 3051 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3052 PREPROCESSOR_DETAIL_BLOCK_ID)) 3053 return Err; 3054 F.PreprocessorDetailStartOffset 3055 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3056 3057 if (!PP.getPreprocessingRecord()) 3058 PP.createPreprocessingRecord(); 3059 if (!PP.getPreprocessingRecord()->getExternalSource()) 3060 PP.getPreprocessingRecord()->SetExternalSource(*this); 3061 break; 3062 3063 case SOURCE_MANAGER_BLOCK_ID: 3064 if (llvm::Error Err = ReadSourceManagerBlock(F)) 3065 return Err; 3066 break; 3067 3068 case SUBMODULE_BLOCK_ID: 3069 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3070 return Err; 3071 break; 3072 3073 case COMMENTS_BLOCK_ID: { 3074 BitstreamCursor C = Stream; 3075 3076 if (llvm::Error Err = Stream.SkipBlock()) 3077 return Err; 3078 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) 3079 return Err; 3080 CommentsCursors.push_back(std::make_pair(C, &F)); 3081 break; 3082 } 3083 3084 default: 3085 if (llvm::Error Err = Stream.SkipBlock()) 3086 return Err; 3087 break; 3088 } 3089 continue; 3090 3091 case llvm::BitstreamEntry::Record: 3092 // The interesting case. 3093 break; 3094 } 3095 3096 // Read and process a record. 3097 Record.clear(); 3098 StringRef Blob; 3099 Expected<unsigned> MaybeRecordType = 3100 Stream.readRecord(Entry.ID, Record, &Blob); 3101 if (!MaybeRecordType) 3102 return MaybeRecordType.takeError(); 3103 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3104 3105 // If we're not loading an AST context, we don't care about most records. 3106 if (!ContextObj) { 3107 switch (RecordType) { 3108 case IDENTIFIER_TABLE: 3109 case IDENTIFIER_OFFSET: 3110 case INTERESTING_IDENTIFIERS: 3111 case STATISTICS: 3112 case PP_CONDITIONAL_STACK: 3113 case PP_COUNTER_VALUE: 3114 case SOURCE_LOCATION_OFFSETS: 3115 case MODULE_OFFSET_MAP: 3116 case SOURCE_MANAGER_LINE_TABLE: 3117 case SOURCE_LOCATION_PRELOADS: 3118 case PPD_ENTITIES_OFFSETS: 3119 case HEADER_SEARCH_TABLE: 3120 case IMPORTED_MODULES: 3121 case MACRO_OFFSET: 3122 break; 3123 default: 3124 continue; 3125 } 3126 } 3127 3128 switch (RecordType) { 3129 default: // Default behavior: ignore. 3130 break; 3131 3132 case TYPE_OFFSET: { 3133 if (F.LocalNumTypes != 0) 3134 return llvm::createStringError( 3135 std::errc::illegal_byte_sequence, 3136 "duplicate TYPE_OFFSET record in AST file"); 3137 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3138 F.LocalNumTypes = Record[0]; 3139 unsigned LocalBaseTypeIndex = Record[1]; 3140 F.BaseTypeIndex = getTotalNumTypes(); 3141 3142 if (F.LocalNumTypes > 0) { 3143 // Introduce the global -> local mapping for types within this module. 3144 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3145 3146 // Introduce the local -> global mapping for types within this module. 3147 F.TypeRemap.insertOrReplace( 3148 std::make_pair(LocalBaseTypeIndex, 3149 F.BaseTypeIndex - LocalBaseTypeIndex)); 3150 3151 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3152 } 3153 break; 3154 } 3155 3156 case DECL_OFFSET: { 3157 if (F.LocalNumDecls != 0) 3158 return llvm::createStringError( 3159 std::errc::illegal_byte_sequence, 3160 "duplicate DECL_OFFSET record in AST file"); 3161 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3162 F.LocalNumDecls = Record[0]; 3163 unsigned LocalBaseDeclID = Record[1]; 3164 F.BaseDeclID = getTotalNumDecls(); 3165 3166 if (F.LocalNumDecls > 0) { 3167 // Introduce the global -> local mapping for declarations within this 3168 // module. 3169 GlobalDeclMap.insert( 3170 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3171 3172 // Introduce the local -> global mapping for declarations within this 3173 // module. 3174 F.DeclRemap.insertOrReplace( 3175 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3176 3177 // Introduce the global -> local mapping for declarations within this 3178 // module. 3179 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3180 3181 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3182 } 3183 break; 3184 } 3185 3186 case TU_UPDATE_LEXICAL: { 3187 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3188 LexicalContents Contents( 3189 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3190 Blob.data()), 3191 static_cast<unsigned int>(Blob.size() / 4)); 3192 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3193 TU->setHasExternalLexicalStorage(true); 3194 break; 3195 } 3196 3197 case UPDATE_VISIBLE: { 3198 unsigned Idx = 0; 3199 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3200 auto *Data = (const unsigned char*)Blob.data(); 3201 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3202 // If we've already loaded the decl, perform the updates when we finish 3203 // loading this block. 3204 if (Decl *D = GetExistingDecl(ID)) 3205 PendingUpdateRecords.push_back( 3206 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3207 break; 3208 } 3209 3210 case IDENTIFIER_TABLE: 3211 F.IdentifierTableData = 3212 reinterpret_cast<const unsigned char *>(Blob.data()); 3213 if (Record[0]) { 3214 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3215 F.IdentifierTableData + Record[0], 3216 F.IdentifierTableData + sizeof(uint32_t), 3217 F.IdentifierTableData, 3218 ASTIdentifierLookupTrait(*this, F)); 3219 3220 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3221 } 3222 break; 3223 3224 case IDENTIFIER_OFFSET: { 3225 if (F.LocalNumIdentifiers != 0) 3226 return llvm::createStringError( 3227 std::errc::illegal_byte_sequence, 3228 "duplicate IDENTIFIER_OFFSET record in AST file"); 3229 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3230 F.LocalNumIdentifiers = Record[0]; 3231 unsigned LocalBaseIdentifierID = Record[1]; 3232 F.BaseIdentifierID = getTotalNumIdentifiers(); 3233 3234 if (F.LocalNumIdentifiers > 0) { 3235 // Introduce the global -> local mapping for identifiers within this 3236 // module. 3237 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3238 &F)); 3239 3240 // Introduce the local -> global mapping for identifiers within this 3241 // module. 3242 F.IdentifierRemap.insertOrReplace( 3243 std::make_pair(LocalBaseIdentifierID, 3244 F.BaseIdentifierID - LocalBaseIdentifierID)); 3245 3246 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3247 + F.LocalNumIdentifiers); 3248 } 3249 break; 3250 } 3251 3252 case INTERESTING_IDENTIFIERS: 3253 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3254 break; 3255 3256 case EAGERLY_DESERIALIZED_DECLS: 3257 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3258 // about "interesting" decls (for instance, if we're building a module). 3259 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3260 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3261 break; 3262 3263 case MODULAR_CODEGEN_DECLS: 3264 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3265 // them (ie: if we're not codegenerating this module). 3266 if (F.Kind == MK_MainFile || 3267 getContext().getLangOpts().BuildingPCHWithObjectFile) 3268 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3269 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3270 break; 3271 3272 case SPECIAL_TYPES: 3273 if (SpecialTypes.empty()) { 3274 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3275 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3276 break; 3277 } 3278 3279 if (SpecialTypes.size() != Record.size()) 3280 return llvm::createStringError(std::errc::illegal_byte_sequence, 3281 "invalid special-types record"); 3282 3283 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3284 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3285 if (!SpecialTypes[I]) 3286 SpecialTypes[I] = ID; 3287 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3288 // merge step? 3289 } 3290 break; 3291 3292 case STATISTICS: 3293 TotalNumStatements += Record[0]; 3294 TotalNumMacros += Record[1]; 3295 TotalLexicalDeclContexts += Record[2]; 3296 TotalVisibleDeclContexts += Record[3]; 3297 break; 3298 3299 case UNUSED_FILESCOPED_DECLS: 3300 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3301 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3302 break; 3303 3304 case DELEGATING_CTORS: 3305 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3306 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3307 break; 3308 3309 case WEAK_UNDECLARED_IDENTIFIERS: 3310 if (Record.size() % 3 != 0) 3311 return llvm::createStringError(std::errc::illegal_byte_sequence, 3312 "invalid weak identifiers record"); 3313 3314 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3315 // files. This isn't the way to do it :) 3316 WeakUndeclaredIdentifiers.clear(); 3317 3318 // Translate the weak, undeclared identifiers into global IDs. 3319 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3320 WeakUndeclaredIdentifiers.push_back( 3321 getGlobalIdentifierID(F, Record[I++])); 3322 WeakUndeclaredIdentifiers.push_back( 3323 getGlobalIdentifierID(F, Record[I++])); 3324 WeakUndeclaredIdentifiers.push_back( 3325 ReadSourceLocation(F, Record, I).getRawEncoding()); 3326 } 3327 break; 3328 3329 case SELECTOR_OFFSETS: { 3330 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3331 F.LocalNumSelectors = Record[0]; 3332 unsigned LocalBaseSelectorID = Record[1]; 3333 F.BaseSelectorID = getTotalNumSelectors(); 3334 3335 if (F.LocalNumSelectors > 0) { 3336 // Introduce the global -> local mapping for selectors within this 3337 // module. 3338 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3339 3340 // Introduce the local -> global mapping for selectors within this 3341 // module. 3342 F.SelectorRemap.insertOrReplace( 3343 std::make_pair(LocalBaseSelectorID, 3344 F.BaseSelectorID - LocalBaseSelectorID)); 3345 3346 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3347 } 3348 break; 3349 } 3350 3351 case METHOD_POOL: 3352 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3353 if (Record[0]) 3354 F.SelectorLookupTable 3355 = ASTSelectorLookupTable::Create( 3356 F.SelectorLookupTableData + Record[0], 3357 F.SelectorLookupTableData, 3358 ASTSelectorLookupTrait(*this, F)); 3359 TotalNumMethodPoolEntries += Record[1]; 3360 break; 3361 3362 case REFERENCED_SELECTOR_POOL: 3363 if (!Record.empty()) { 3364 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3365 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3366 Record[Idx++])); 3367 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3368 getRawEncoding()); 3369 } 3370 } 3371 break; 3372 3373 case PP_CONDITIONAL_STACK: 3374 if (!Record.empty()) { 3375 unsigned Idx = 0, End = Record.size() - 1; 3376 bool ReachedEOFWhileSkipping = Record[Idx++]; 3377 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3378 if (ReachedEOFWhileSkipping) { 3379 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3380 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3381 bool FoundNonSkipPortion = Record[Idx++]; 3382 bool FoundElse = Record[Idx++]; 3383 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3384 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3385 FoundElse, ElseLoc); 3386 } 3387 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3388 while (Idx < End) { 3389 auto Loc = ReadSourceLocation(F, Record, Idx); 3390 bool WasSkipping = Record[Idx++]; 3391 bool FoundNonSkip = Record[Idx++]; 3392 bool FoundElse = Record[Idx++]; 3393 ConditionalStack.push_back( 3394 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3395 } 3396 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3397 } 3398 break; 3399 3400 case PP_COUNTER_VALUE: 3401 if (!Record.empty() && Listener) 3402 Listener->ReadCounter(F, Record[0]); 3403 break; 3404 3405 case FILE_SORTED_DECLS: 3406 F.FileSortedDecls = (const DeclID *)Blob.data(); 3407 F.NumFileSortedDecls = Record[0]; 3408 break; 3409 3410 case SOURCE_LOCATION_OFFSETS: { 3411 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3412 F.LocalNumSLocEntries = Record[0]; 3413 SourceLocation::UIntTy SLocSpaceSize = Record[1]; 3414 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3415 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3416 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3417 SLocSpaceSize); 3418 if (!F.SLocEntryBaseID) 3419 return llvm::createStringError(std::errc::invalid_argument, 3420 "ran out of source locations"); 3421 // Make our entry in the range map. BaseID is negative and growing, so 3422 // we invert it. Because we invert it, though, we need the other end of 3423 // the range. 3424 unsigned RangeStart = 3425 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3426 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3427 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3428 3429 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3430 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0); 3431 GlobalSLocOffsetMap.insert( 3432 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3433 - SLocSpaceSize,&F)); 3434 3435 // Initialize the remapping table. 3436 // Invalid stays invalid. 3437 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3438 // This module. Base was 2 when being compiled. 3439 F.SLocRemap.insertOrReplace(std::make_pair( 3440 2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2))); 3441 3442 TotalNumSLocEntries += F.LocalNumSLocEntries; 3443 break; 3444 } 3445 3446 case MODULE_OFFSET_MAP: 3447 F.ModuleOffsetMap = Blob; 3448 break; 3449 3450 case SOURCE_MANAGER_LINE_TABLE: 3451 ParseLineTable(F, Record); 3452 break; 3453 3454 case SOURCE_LOCATION_PRELOADS: { 3455 // Need to transform from the local view (1-based IDs) to the global view, 3456 // which is based off F.SLocEntryBaseID. 3457 if (!F.PreloadSLocEntries.empty()) 3458 return llvm::createStringError( 3459 std::errc::illegal_byte_sequence, 3460 "Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3461 3462 F.PreloadSLocEntries.swap(Record); 3463 break; 3464 } 3465 3466 case EXT_VECTOR_DECLS: 3467 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3468 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3469 break; 3470 3471 case VTABLE_USES: 3472 if (Record.size() % 3 != 0) 3473 return llvm::createStringError(std::errc::illegal_byte_sequence, 3474 "Invalid VTABLE_USES record"); 3475 3476 // Later tables overwrite earlier ones. 3477 // FIXME: Modules will have some trouble with this. This is clearly not 3478 // the right way to do this. 3479 VTableUses.clear(); 3480 3481 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3482 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3483 VTableUses.push_back( 3484 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3485 VTableUses.push_back(Record[Idx++]); 3486 } 3487 break; 3488 3489 case PENDING_IMPLICIT_INSTANTIATIONS: 3490 if (PendingInstantiations.size() % 2 != 0) 3491 return llvm::createStringError( 3492 std::errc::illegal_byte_sequence, 3493 "Invalid existing PendingInstantiations"); 3494 3495 if (Record.size() % 2 != 0) 3496 return llvm::createStringError( 3497 std::errc::illegal_byte_sequence, 3498 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3499 3500 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3501 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3502 PendingInstantiations.push_back( 3503 ReadSourceLocation(F, Record, I).getRawEncoding()); 3504 } 3505 break; 3506 3507 case SEMA_DECL_REFS: 3508 if (Record.size() != 3) 3509 return llvm::createStringError(std::errc::illegal_byte_sequence, 3510 "Invalid SEMA_DECL_REFS block"); 3511 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3512 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3513 break; 3514 3515 case PPD_ENTITIES_OFFSETS: { 3516 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3517 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3518 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3519 3520 unsigned LocalBasePreprocessedEntityID = Record[0]; 3521 3522 unsigned StartingID; 3523 if (!PP.getPreprocessingRecord()) 3524 PP.createPreprocessingRecord(); 3525 if (!PP.getPreprocessingRecord()->getExternalSource()) 3526 PP.getPreprocessingRecord()->SetExternalSource(*this); 3527 StartingID 3528 = PP.getPreprocessingRecord() 3529 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3530 F.BasePreprocessedEntityID = StartingID; 3531 3532 if (F.NumPreprocessedEntities > 0) { 3533 // Introduce the global -> local mapping for preprocessed entities in 3534 // this module. 3535 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3536 3537 // Introduce the local -> global mapping for preprocessed entities in 3538 // this module. 3539 F.PreprocessedEntityRemap.insertOrReplace( 3540 std::make_pair(LocalBasePreprocessedEntityID, 3541 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3542 } 3543 3544 break; 3545 } 3546 3547 case PPD_SKIPPED_RANGES: { 3548 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3549 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3550 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3551 3552 if (!PP.getPreprocessingRecord()) 3553 PP.createPreprocessingRecord(); 3554 if (!PP.getPreprocessingRecord()->getExternalSource()) 3555 PP.getPreprocessingRecord()->SetExternalSource(*this); 3556 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3557 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3558 3559 if (F.NumPreprocessedSkippedRanges > 0) 3560 GlobalSkippedRangeMap.insert( 3561 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3562 break; 3563 } 3564 3565 case DECL_UPDATE_OFFSETS: 3566 if (Record.size() % 2 != 0) 3567 return llvm::createStringError( 3568 std::errc::illegal_byte_sequence, 3569 "invalid DECL_UPDATE_OFFSETS block in AST file"); 3570 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3571 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3572 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3573 3574 // If we've already loaded the decl, perform the updates when we finish 3575 // loading this block. 3576 if (Decl *D = GetExistingDecl(ID)) 3577 PendingUpdateRecords.push_back( 3578 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3579 } 3580 break; 3581 3582 case OBJC_CATEGORIES_MAP: 3583 if (F.LocalNumObjCCategoriesInMap != 0) 3584 return llvm::createStringError( 3585 std::errc::illegal_byte_sequence, 3586 "duplicate OBJC_CATEGORIES_MAP record in AST file"); 3587 3588 F.LocalNumObjCCategoriesInMap = Record[0]; 3589 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3590 break; 3591 3592 case OBJC_CATEGORIES: 3593 F.ObjCCategories.swap(Record); 3594 break; 3595 3596 case CUDA_SPECIAL_DECL_REFS: 3597 // Later tables overwrite earlier ones. 3598 // FIXME: Modules will have trouble with this. 3599 CUDASpecialDeclRefs.clear(); 3600 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3601 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3602 break; 3603 3604 case HEADER_SEARCH_TABLE: 3605 F.HeaderFileInfoTableData = Blob.data(); 3606 F.LocalNumHeaderFileInfos = Record[1]; 3607 if (Record[0]) { 3608 F.HeaderFileInfoTable 3609 = HeaderFileInfoLookupTable::Create( 3610 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3611 (const unsigned char *)F.HeaderFileInfoTableData, 3612 HeaderFileInfoTrait(*this, F, 3613 &PP.getHeaderSearchInfo(), 3614 Blob.data() + Record[2])); 3615 3616 PP.getHeaderSearchInfo().SetExternalSource(this); 3617 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3618 PP.getHeaderSearchInfo().SetExternalLookup(this); 3619 } 3620 break; 3621 3622 case FP_PRAGMA_OPTIONS: 3623 // Later tables overwrite earlier ones. 3624 FPPragmaOptions.swap(Record); 3625 break; 3626 3627 case OPENCL_EXTENSIONS: 3628 for (unsigned I = 0, E = Record.size(); I != E; ) { 3629 auto Name = ReadString(Record, I); 3630 auto &OptInfo = OpenCLExtensions.OptMap[Name]; 3631 OptInfo.Supported = Record[I++] != 0; 3632 OptInfo.Enabled = Record[I++] != 0; 3633 OptInfo.WithPragma = Record[I++] != 0; 3634 OptInfo.Avail = Record[I++]; 3635 OptInfo.Core = Record[I++]; 3636 OptInfo.Opt = Record[I++]; 3637 } 3638 break; 3639 3640 case TENTATIVE_DEFINITIONS: 3641 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3642 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3643 break; 3644 3645 case KNOWN_NAMESPACES: 3646 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3647 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3648 break; 3649 3650 case UNDEFINED_BUT_USED: 3651 if (UndefinedButUsed.size() % 2 != 0) 3652 return llvm::createStringError(std::errc::illegal_byte_sequence, 3653 "Invalid existing UndefinedButUsed"); 3654 3655 if (Record.size() % 2 != 0) 3656 return llvm::createStringError(std::errc::illegal_byte_sequence, 3657 "invalid undefined-but-used record"); 3658 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3659 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3660 UndefinedButUsed.push_back( 3661 ReadSourceLocation(F, Record, I).getRawEncoding()); 3662 } 3663 break; 3664 3665 case DELETE_EXPRS_TO_ANALYZE: 3666 for (unsigned I = 0, N = Record.size(); I != N;) { 3667 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3668 const uint64_t Count = Record[I++]; 3669 DelayedDeleteExprs.push_back(Count); 3670 for (uint64_t C = 0; C < Count; ++C) { 3671 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3672 bool IsArrayForm = Record[I++] == 1; 3673 DelayedDeleteExprs.push_back(IsArrayForm); 3674 } 3675 } 3676 break; 3677 3678 case IMPORTED_MODULES: 3679 if (!F.isModule()) { 3680 // If we aren't loading a module (which has its own exports), make 3681 // all of the imported modules visible. 3682 // FIXME: Deal with macros-only imports. 3683 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3684 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3685 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3686 if (GlobalID) { 3687 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3688 if (DeserializationListener) 3689 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3690 } 3691 } 3692 } 3693 break; 3694 3695 case MACRO_OFFSET: { 3696 if (F.LocalNumMacros != 0) 3697 return llvm::createStringError( 3698 std::errc::illegal_byte_sequence, 3699 "duplicate MACRO_OFFSET record in AST file"); 3700 F.MacroOffsets = (const uint32_t *)Blob.data(); 3701 F.LocalNumMacros = Record[0]; 3702 unsigned LocalBaseMacroID = Record[1]; 3703 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3704 F.BaseMacroID = getTotalNumMacros(); 3705 3706 if (F.LocalNumMacros > 0) { 3707 // Introduce the global -> local mapping for macros within this module. 3708 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3709 3710 // Introduce the local -> global mapping for macros within this module. 3711 F.MacroRemap.insertOrReplace( 3712 std::make_pair(LocalBaseMacroID, 3713 F.BaseMacroID - LocalBaseMacroID)); 3714 3715 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3716 } 3717 break; 3718 } 3719 3720 case PP_INCLUDED_FILES: 3721 readIncludedFiles(F, Blob, PP); 3722 break; 3723 3724 case LATE_PARSED_TEMPLATE: 3725 LateParsedTemplates.emplace_back( 3726 std::piecewise_construct, std::forward_as_tuple(&F), 3727 std::forward_as_tuple(Record.begin(), Record.end())); 3728 break; 3729 3730 case OPTIMIZE_PRAGMA_OPTIONS: 3731 if (Record.size() != 1) 3732 return llvm::createStringError(std::errc::illegal_byte_sequence, 3733 "invalid pragma optimize record"); 3734 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3735 break; 3736 3737 case MSSTRUCT_PRAGMA_OPTIONS: 3738 if (Record.size() != 1) 3739 return llvm::createStringError(std::errc::illegal_byte_sequence, 3740 "invalid pragma ms_struct record"); 3741 PragmaMSStructState = Record[0]; 3742 break; 3743 3744 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3745 if (Record.size() != 2) 3746 return llvm::createStringError( 3747 std::errc::illegal_byte_sequence, 3748 "invalid pragma pointers to members record"); 3749 PragmaMSPointersToMembersState = Record[0]; 3750 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3751 break; 3752 3753 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3754 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3755 UnusedLocalTypedefNameCandidates.push_back( 3756 getGlobalDeclID(F, Record[I])); 3757 break; 3758 3759 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3760 if (Record.size() != 1) 3761 return llvm::createStringError(std::errc::illegal_byte_sequence, 3762 "invalid cuda pragma options record"); 3763 ForceCUDAHostDeviceDepth = Record[0]; 3764 break; 3765 3766 case ALIGN_PACK_PRAGMA_OPTIONS: { 3767 if (Record.size() < 3) 3768 return llvm::createStringError(std::errc::illegal_byte_sequence, 3769 "invalid pragma pack record"); 3770 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]); 3771 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3772 unsigned NumStackEntries = Record[2]; 3773 unsigned Idx = 3; 3774 // Reset the stack when importing a new module. 3775 PragmaAlignPackStack.clear(); 3776 for (unsigned I = 0; I < NumStackEntries; ++I) { 3777 PragmaAlignPackStackEntry Entry; 3778 Entry.Value = ReadAlignPackInfo(Record[Idx++]); 3779 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3780 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3781 PragmaAlignPackStrings.push_back(ReadString(Record, Idx)); 3782 Entry.SlotLabel = PragmaAlignPackStrings.back(); 3783 PragmaAlignPackStack.push_back(Entry); 3784 } 3785 break; 3786 } 3787 3788 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3789 if (Record.size() < 3) 3790 return llvm::createStringError(std::errc::illegal_byte_sequence, 3791 "invalid pragma float control record"); 3792 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3793 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3794 unsigned NumStackEntries = Record[2]; 3795 unsigned Idx = 3; 3796 // Reset the stack when importing a new module. 3797 FpPragmaStack.clear(); 3798 for (unsigned I = 0; I < NumStackEntries; ++I) { 3799 FpPragmaStackEntry Entry; 3800 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3801 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3802 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3803 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3804 Entry.SlotLabel = FpPragmaStrings.back(); 3805 FpPragmaStack.push_back(Entry); 3806 } 3807 break; 3808 } 3809 3810 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3811 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3812 DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I])); 3813 break; 3814 } 3815 } 3816 } 3817 3818 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3819 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3820 3821 // Additional remapping information. 3822 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3823 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3824 F.ModuleOffsetMap = StringRef(); 3825 3826 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3827 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3828 F.SLocRemap.insert(std::make_pair(0U, 0)); 3829 F.SLocRemap.insert(std::make_pair(2U, 1)); 3830 } 3831 3832 // Continuous range maps we may be updating in our module. 3833 using SLocRemapBuilder = 3834 ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy, 3835 2>::Builder; 3836 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3837 SLocRemapBuilder SLocRemap(F.SLocRemap); 3838 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3839 RemapBuilder MacroRemap(F.MacroRemap); 3840 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3841 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3842 RemapBuilder SelectorRemap(F.SelectorRemap); 3843 RemapBuilder DeclRemap(F.DeclRemap); 3844 RemapBuilder TypeRemap(F.TypeRemap); 3845 3846 while (Data < DataEnd) { 3847 // FIXME: Looking up dependency modules by filename is horrible. Let's 3848 // start fixing this with prebuilt, explicit and implicit modules and see 3849 // how it goes... 3850 using namespace llvm::support; 3851 ModuleKind Kind = static_cast<ModuleKind>( 3852 endian::readNext<uint8_t, little, unaligned>(Data)); 3853 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3854 StringRef Name = StringRef((const char*)Data, Len); 3855 Data += Len; 3856 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3857 Kind == MK_ImplicitModule 3858 ? ModuleMgr.lookupByModuleName(Name) 3859 : ModuleMgr.lookupByFileName(Name)); 3860 if (!OM) { 3861 std::string Msg = 3862 "SourceLocation remap refers to unknown module, cannot find "; 3863 Msg.append(std::string(Name)); 3864 Error(Msg); 3865 return; 3866 } 3867 3868 SourceLocation::UIntTy SLocOffset = 3869 endian::readNext<uint32_t, little, unaligned>(Data); 3870 uint32_t IdentifierIDOffset = 3871 endian::readNext<uint32_t, little, unaligned>(Data); 3872 uint32_t MacroIDOffset = 3873 endian::readNext<uint32_t, little, unaligned>(Data); 3874 uint32_t PreprocessedEntityIDOffset = 3875 endian::readNext<uint32_t, little, unaligned>(Data); 3876 uint32_t SubmoduleIDOffset = 3877 endian::readNext<uint32_t, little, unaligned>(Data); 3878 uint32_t SelectorIDOffset = 3879 endian::readNext<uint32_t, little, unaligned>(Data); 3880 uint32_t DeclIDOffset = 3881 endian::readNext<uint32_t, little, unaligned>(Data); 3882 uint32_t TypeIndexOffset = 3883 endian::readNext<uint32_t, little, unaligned>(Data); 3884 3885 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3886 RemapBuilder &Remap) { 3887 constexpr uint32_t None = std::numeric_limits<uint32_t>::max(); 3888 if (Offset != None) 3889 Remap.insert(std::make_pair(Offset, 3890 static_cast<int>(BaseOffset - Offset))); 3891 }; 3892 3893 constexpr SourceLocation::UIntTy SLocNone = 3894 std::numeric_limits<SourceLocation::UIntTy>::max(); 3895 if (SLocOffset != SLocNone) 3896 SLocRemap.insert(std::make_pair( 3897 SLocOffset, static_cast<SourceLocation::IntTy>( 3898 OM->SLocEntryBaseOffset - SLocOffset))); 3899 3900 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3901 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3902 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3903 PreprocessedEntityRemap); 3904 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3905 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3906 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3907 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3908 3909 // Global -> local mappings. 3910 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3911 } 3912 } 3913 3914 ASTReader::ASTReadResult 3915 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3916 const ModuleFile *ImportedBy, 3917 unsigned ClientLoadCapabilities) { 3918 unsigned Idx = 0; 3919 F.ModuleMapPath = ReadPath(F, Record, Idx); 3920 3921 // Try to resolve ModuleName in the current header search context and 3922 // verify that it is found in the same module map file as we saved. If the 3923 // top-level AST file is a main file, skip this check because there is no 3924 // usable header search context. 3925 assert(!F.ModuleName.empty() && 3926 "MODULE_NAME should come before MODULE_MAP_FILE"); 3927 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3928 // An implicitly-loaded module file should have its module listed in some 3929 // module map file that we've already loaded. 3930 Module *M = 3931 PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc); 3932 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3933 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3934 // Don't emit module relocation error if we have -fno-validate-pch 3935 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 3936 DisableValidationForModuleKind::Module) && 3937 !ModMap) { 3938 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) { 3939 if (auto ASTFE = M ? M->getASTFile() : None) { 3940 // This module was defined by an imported (explicit) module. 3941 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3942 << ASTFE->getName(); 3943 } else { 3944 // This module was built with a different module map. 3945 Diag(diag::err_imported_module_not_found) 3946 << F.ModuleName << F.FileName 3947 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3948 << !ImportedBy; 3949 // In case it was imported by a PCH, there's a chance the user is 3950 // just missing to include the search path to the directory containing 3951 // the modulemap. 3952 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3953 Diag(diag::note_imported_by_pch_module_not_found) 3954 << llvm::sys::path::parent_path(F.ModuleMapPath); 3955 } 3956 } 3957 return OutOfDate; 3958 } 3959 3960 assert(M && M->Name == F.ModuleName && "found module with different name"); 3961 3962 // Check the primary module map file. 3963 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3964 if (!StoredModMap || *StoredModMap != ModMap) { 3965 assert(ModMap && "found module is missing module map file"); 3966 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3967 "top-level import should be verified"); 3968 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3969 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3970 Diag(diag::err_imported_module_modmap_changed) 3971 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3972 << ModMap->getName() << F.ModuleMapPath << NotImported; 3973 return OutOfDate; 3974 } 3975 3976 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3977 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3978 // FIXME: we should use input files rather than storing names. 3979 std::string Filename = ReadPath(F, Record, Idx); 3980 auto SF = FileMgr.getFile(Filename, false, false); 3981 if (!SF) { 3982 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3983 Error("could not find file '" + Filename +"' referenced by AST file"); 3984 return OutOfDate; 3985 } 3986 AdditionalStoredMaps.insert(*SF); 3987 } 3988 3989 // Check any additional module map files (e.g. module.private.modulemap) 3990 // that are not in the pcm. 3991 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3992 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3993 // Remove files that match 3994 // Note: SmallPtrSet::erase is really remove 3995 if (!AdditionalStoredMaps.erase(ModMap)) { 3996 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3997 Diag(diag::err_module_different_modmap) 3998 << F.ModuleName << /*new*/0 << ModMap->getName(); 3999 return OutOfDate; 4000 } 4001 } 4002 } 4003 4004 // Check any additional module map files that are in the pcm, but not 4005 // found in header search. Cases that match are already removed. 4006 for (const FileEntry *ModMap : AdditionalStoredMaps) { 4007 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 4008 Diag(diag::err_module_different_modmap) 4009 << F.ModuleName << /*not new*/1 << ModMap->getName(); 4010 return OutOfDate; 4011 } 4012 } 4013 4014 if (Listener) 4015 Listener->ReadModuleMapFile(F.ModuleMapPath); 4016 return Success; 4017 } 4018 4019 /// Move the given method to the back of the global list of methods. 4020 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4021 // Find the entry for this selector in the method pool. 4022 Sema::GlobalMethodPool::iterator Known 4023 = S.MethodPool.find(Method->getSelector()); 4024 if (Known == S.MethodPool.end()) 4025 return; 4026 4027 // Retrieve the appropriate method list. 4028 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4029 : Known->second.second; 4030 bool Found = false; 4031 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4032 if (!Found) { 4033 if (List->getMethod() == Method) { 4034 Found = true; 4035 } else { 4036 // Keep searching. 4037 continue; 4038 } 4039 } 4040 4041 if (List->getNext()) 4042 List->setMethod(List->getNext()->getMethod()); 4043 else 4044 List->setMethod(Method); 4045 } 4046 } 4047 4048 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4049 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4050 for (Decl *D : Names) { 4051 bool wasHidden = !D->isUnconditionallyVisible(); 4052 D->setVisibleDespiteOwningModule(); 4053 4054 if (wasHidden && SemaObj) { 4055 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4056 moveMethodToBackOfGlobalList(*SemaObj, Method); 4057 } 4058 } 4059 } 4060 } 4061 4062 void ASTReader::makeModuleVisible(Module *Mod, 4063 Module::NameVisibilityKind NameVisibility, 4064 SourceLocation ImportLoc) { 4065 llvm::SmallPtrSet<Module *, 4> Visited; 4066 SmallVector<Module *, 4> Stack; 4067 Stack.push_back(Mod); 4068 while (!Stack.empty()) { 4069 Mod = Stack.pop_back_val(); 4070 4071 if (NameVisibility <= Mod->NameVisibility) { 4072 // This module already has this level of visibility (or greater), so 4073 // there is nothing more to do. 4074 continue; 4075 } 4076 4077 if (Mod->isUnimportable()) { 4078 // Modules that aren't importable cannot be made visible. 4079 continue; 4080 } 4081 4082 // Update the module's name visibility. 4083 Mod->NameVisibility = NameVisibility; 4084 4085 // If we've already deserialized any names from this module, 4086 // mark them as visible. 4087 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4088 if (Hidden != HiddenNamesMap.end()) { 4089 auto HiddenNames = std::move(*Hidden); 4090 HiddenNamesMap.erase(Hidden); 4091 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4092 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4093 "making names visible added hidden names"); 4094 } 4095 4096 // Push any exported modules onto the stack to be marked as visible. 4097 SmallVector<Module *, 16> Exports; 4098 Mod->getExportedModules(Exports); 4099 for (SmallVectorImpl<Module *>::iterator 4100 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4101 Module *Exported = *I; 4102 if (Visited.insert(Exported).second) 4103 Stack.push_back(Exported); 4104 } 4105 } 4106 } 4107 4108 /// We've merged the definition \p MergedDef into the existing definition 4109 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4110 /// visible. 4111 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4112 NamedDecl *MergedDef) { 4113 if (!Def->isUnconditionallyVisible()) { 4114 // If MergedDef is visible or becomes visible, make the definition visible. 4115 if (MergedDef->isUnconditionallyVisible()) 4116 Def->setVisibleDespiteOwningModule(); 4117 else { 4118 getContext().mergeDefinitionIntoModule( 4119 Def, MergedDef->getImportedOwningModule(), 4120 /*NotifyListeners*/ false); 4121 PendingMergedDefinitionsToDeduplicate.insert(Def); 4122 } 4123 } 4124 } 4125 4126 bool ASTReader::loadGlobalIndex() { 4127 if (GlobalIndex) 4128 return false; 4129 4130 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4131 !PP.getLangOpts().Modules) 4132 return true; 4133 4134 // Try to load the global index. 4135 TriedLoadingGlobalIndex = true; 4136 StringRef ModuleCachePath 4137 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4138 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4139 GlobalModuleIndex::readIndex(ModuleCachePath); 4140 if (llvm::Error Err = std::move(Result.second)) { 4141 assert(!Result.first); 4142 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4143 return true; 4144 } 4145 4146 GlobalIndex.reset(Result.first); 4147 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4148 return false; 4149 } 4150 4151 bool ASTReader::isGlobalIndexUnavailable() const { 4152 return PP.getLangOpts().Modules && UseGlobalIndex && 4153 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4154 } 4155 4156 static void updateModuleTimestamp(ModuleFile &MF) { 4157 // Overwrite the timestamp file contents so that file's mtime changes. 4158 std::string TimestampFilename = MF.getTimestampFilename(); 4159 std::error_code EC; 4160 llvm::raw_fd_ostream OS(TimestampFilename, EC, 4161 llvm::sys::fs::OF_TextWithCRLF); 4162 if (EC) 4163 return; 4164 OS << "Timestamp file\n"; 4165 OS.close(); 4166 OS.clear_error(); // Avoid triggering a fatal error. 4167 } 4168 4169 /// Given a cursor at the start of an AST file, scan ahead and drop the 4170 /// cursor into the start of the given block ID, returning false on success and 4171 /// true on failure. 4172 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4173 while (true) { 4174 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4175 if (!MaybeEntry) { 4176 // FIXME this drops errors on the floor. 4177 consumeError(MaybeEntry.takeError()); 4178 return true; 4179 } 4180 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4181 4182 switch (Entry.Kind) { 4183 case llvm::BitstreamEntry::Error: 4184 case llvm::BitstreamEntry::EndBlock: 4185 return true; 4186 4187 case llvm::BitstreamEntry::Record: 4188 // Ignore top-level records. 4189 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4190 break; 4191 else { 4192 // FIXME this drops errors on the floor. 4193 consumeError(Skipped.takeError()); 4194 return true; 4195 } 4196 4197 case llvm::BitstreamEntry::SubBlock: 4198 if (Entry.ID == BlockID) { 4199 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4200 // FIXME this drops the error on the floor. 4201 consumeError(std::move(Err)); 4202 return true; 4203 } 4204 // Found it! 4205 return false; 4206 } 4207 4208 if (llvm::Error Err = Cursor.SkipBlock()) { 4209 // FIXME this drops the error on the floor. 4210 consumeError(std::move(Err)); 4211 return true; 4212 } 4213 } 4214 } 4215 } 4216 4217 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4218 ModuleKind Type, 4219 SourceLocation ImportLoc, 4220 unsigned ClientLoadCapabilities, 4221 SmallVectorImpl<ImportedSubmodule> *Imported) { 4222 llvm::SaveAndRestore<SourceLocation> 4223 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4224 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII( 4225 CurrentDeserializingModuleKind, Type); 4226 4227 // Defer any pending actions until we get to the end of reading the AST file. 4228 Deserializing AnASTFile(this); 4229 4230 // Bump the generation number. 4231 unsigned PreviousGeneration = 0; 4232 if (ContextObj) 4233 PreviousGeneration = incrementGeneration(*ContextObj); 4234 4235 unsigned NumModules = ModuleMgr.size(); 4236 SmallVector<ImportedModule, 4> Loaded; 4237 if (ASTReadResult ReadResult = 4238 ReadASTCore(FileName, Type, ImportLoc, 4239 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(), 4240 ClientLoadCapabilities)) { 4241 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4242 PP.getLangOpts().Modules 4243 ? &PP.getHeaderSearchInfo().getModuleMap() 4244 : nullptr); 4245 4246 // If we find that any modules are unusable, the global index is going 4247 // to be out-of-date. Just remove it. 4248 GlobalIndex.reset(); 4249 ModuleMgr.setGlobalIndex(nullptr); 4250 return ReadResult; 4251 } 4252 4253 // Here comes stuff that we only do once the entire chain is loaded. Do *not* 4254 // remove modules from this point. Various fields are updated during reading 4255 // the AST block and removing the modules would result in dangling pointers. 4256 // They are generally only incidentally dereferenced, ie. a binary search 4257 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to 4258 // be dereferenced but it wouldn't actually be used. 4259 4260 // Load the AST blocks of all of the modules that we loaded. We can still 4261 // hit errors parsing the ASTs at this point. 4262 for (ImportedModule &M : Loaded) { 4263 ModuleFile &F = *M.Mod; 4264 4265 // Read the AST block. 4266 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) { 4267 Error(std::move(Err)); 4268 return Failure; 4269 } 4270 4271 // The AST block should always have a definition for the main module. 4272 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4273 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4274 return Failure; 4275 } 4276 4277 // Read the extension blocks. 4278 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4279 if (llvm::Error Err = ReadExtensionBlock(F)) { 4280 Error(std::move(Err)); 4281 return Failure; 4282 } 4283 } 4284 4285 // Once read, set the ModuleFile bit base offset and update the size in 4286 // bits of all files we've seen. 4287 F.GlobalBitOffset = TotalModulesSizeInBits; 4288 TotalModulesSizeInBits += F.SizeInBits; 4289 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4290 } 4291 4292 // Preload source locations and interesting indentifiers. 4293 for (ImportedModule &M : Loaded) { 4294 ModuleFile &F = *M.Mod; 4295 4296 // Preload SLocEntries. 4297 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4298 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4299 // Load it through the SourceManager and don't call ReadSLocEntry() 4300 // directly because the entry may have already been loaded in which case 4301 // calling ReadSLocEntry() directly would trigger an assertion in 4302 // SourceManager. 4303 SourceMgr.getLoadedSLocEntryByID(Index); 4304 } 4305 4306 // Map the original source file ID into the ID space of the current 4307 // compilation. 4308 if (F.OriginalSourceFileID.isValid()) { 4309 F.OriginalSourceFileID = FileID::get( 4310 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4311 } 4312 4313 // Preload all the pending interesting identifiers by marking them out of 4314 // date. 4315 for (auto Offset : F.PreloadIdentifierOffsets) { 4316 const unsigned char *Data = F.IdentifierTableData + Offset; 4317 4318 ASTIdentifierLookupTrait Trait(*this, F); 4319 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4320 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4321 auto &II = PP.getIdentifierTable().getOwn(Key); 4322 II.setOutOfDate(true); 4323 4324 // Mark this identifier as being from an AST file so that we can track 4325 // whether we need to serialize it. 4326 markIdentifierFromAST(*this, II); 4327 4328 // Associate the ID with the identifier so that the writer can reuse it. 4329 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4330 SetIdentifierInfo(ID, &II); 4331 } 4332 } 4333 4334 // Setup the import locations and notify the module manager that we've 4335 // committed to these module files. 4336 for (ImportedModule &M : Loaded) { 4337 ModuleFile &F = *M.Mod; 4338 4339 ModuleMgr.moduleFileAccepted(&F); 4340 4341 // Set the import location. 4342 F.DirectImportLoc = ImportLoc; 4343 // FIXME: We assume that locations from PCH / preamble do not need 4344 // any translation. 4345 if (!M.ImportedBy) 4346 F.ImportLoc = M.ImportLoc; 4347 else 4348 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4349 } 4350 4351 if (!PP.getLangOpts().CPlusPlus || 4352 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4353 Type != MK_PrebuiltModule)) { 4354 // Mark all of the identifiers in the identifier table as being out of date, 4355 // so that various accessors know to check the loaded modules when the 4356 // identifier is used. 4357 // 4358 // For C++ modules, we don't need information on many identifiers (just 4359 // those that provide macros or are poisoned), so we mark all of 4360 // the interesting ones via PreloadIdentifierOffsets. 4361 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4362 IdEnd = PP.getIdentifierTable().end(); 4363 Id != IdEnd; ++Id) 4364 Id->second->setOutOfDate(true); 4365 } 4366 // Mark selectors as out of date. 4367 for (auto Sel : SelectorGeneration) 4368 SelectorOutOfDate[Sel.first] = true; 4369 4370 // Resolve any unresolved module exports. 4371 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4372 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4373 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4374 Module *ResolvedMod = getSubmodule(GlobalID); 4375 4376 switch (Unresolved.Kind) { 4377 case UnresolvedModuleRef::Conflict: 4378 if (ResolvedMod) { 4379 Module::Conflict Conflict; 4380 Conflict.Other = ResolvedMod; 4381 Conflict.Message = Unresolved.String.str(); 4382 Unresolved.Mod->Conflicts.push_back(Conflict); 4383 } 4384 continue; 4385 4386 case UnresolvedModuleRef::Import: 4387 if (ResolvedMod) 4388 Unresolved.Mod->Imports.insert(ResolvedMod); 4389 continue; 4390 4391 case UnresolvedModuleRef::Export: 4392 if (ResolvedMod || Unresolved.IsWildcard) 4393 Unresolved.Mod->Exports.push_back( 4394 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4395 continue; 4396 } 4397 } 4398 UnresolvedModuleRefs.clear(); 4399 4400 if (Imported) 4401 Imported->append(ImportedModules.begin(), 4402 ImportedModules.end()); 4403 4404 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4405 // Might be unnecessary as use declarations are only used to build the 4406 // module itself. 4407 4408 if (ContextObj) 4409 InitializeContext(); 4410 4411 if (SemaObj) 4412 UpdateSema(); 4413 4414 if (DeserializationListener) 4415 DeserializationListener->ReaderInitialized(this); 4416 4417 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4418 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4419 // If this AST file is a precompiled preamble, then set the 4420 // preamble file ID of the source manager to the file source file 4421 // from which the preamble was built. 4422 if (Type == MK_Preamble) { 4423 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4424 } else if (Type == MK_MainFile) { 4425 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4426 } 4427 } 4428 4429 // For any Objective-C class definitions we have already loaded, make sure 4430 // that we load any additional categories. 4431 if (ContextObj) { 4432 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4433 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4434 ObjCClassesLoaded[I], 4435 PreviousGeneration); 4436 } 4437 } 4438 4439 if (PP.getHeaderSearchInfo() 4440 .getHeaderSearchOpts() 4441 .ModulesValidateOncePerBuildSession) { 4442 // Now we are certain that the module and all modules it depends on are 4443 // up to date. Create or update timestamp files for modules that are 4444 // located in the module cache (not for PCH files that could be anywhere 4445 // in the filesystem). 4446 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4447 ImportedModule &M = Loaded[I]; 4448 if (M.Mod->Kind == MK_ImplicitModule) { 4449 updateModuleTimestamp(*M.Mod); 4450 } 4451 } 4452 } 4453 4454 return Success; 4455 } 4456 4457 static ASTFileSignature readASTFileSignature(StringRef PCH); 4458 4459 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4460 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4461 // FIXME checking magic headers is done in other places such as 4462 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4463 // always done the same. Unify it all with a helper. 4464 if (!Stream.canSkipToPos(4)) 4465 return llvm::createStringError(std::errc::illegal_byte_sequence, 4466 "file too small to contain AST file magic"); 4467 for (unsigned C : {'C', 'P', 'C', 'H'}) 4468 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4469 if (Res.get() != C) 4470 return llvm::createStringError( 4471 std::errc::illegal_byte_sequence, 4472 "file doesn't start with AST file magic"); 4473 } else 4474 return Res.takeError(); 4475 return llvm::Error::success(); 4476 } 4477 4478 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4479 switch (Kind) { 4480 case MK_PCH: 4481 return 0; // PCH 4482 case MK_ImplicitModule: 4483 case MK_ExplicitModule: 4484 case MK_PrebuiltModule: 4485 return 1; // module 4486 case MK_MainFile: 4487 case MK_Preamble: 4488 return 2; // main source file 4489 } 4490 llvm_unreachable("unknown module kind"); 4491 } 4492 4493 ASTReader::ASTReadResult 4494 ASTReader::ReadASTCore(StringRef FileName, 4495 ModuleKind Type, 4496 SourceLocation ImportLoc, 4497 ModuleFile *ImportedBy, 4498 SmallVectorImpl<ImportedModule> &Loaded, 4499 off_t ExpectedSize, time_t ExpectedModTime, 4500 ASTFileSignature ExpectedSignature, 4501 unsigned ClientLoadCapabilities) { 4502 ModuleFile *M; 4503 std::string ErrorStr; 4504 ModuleManager::AddModuleResult AddResult 4505 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4506 getGeneration(), ExpectedSize, ExpectedModTime, 4507 ExpectedSignature, readASTFileSignature, 4508 M, ErrorStr); 4509 4510 switch (AddResult) { 4511 case ModuleManager::AlreadyLoaded: 4512 Diag(diag::remark_module_import) 4513 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4514 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4515 return Success; 4516 4517 case ModuleManager::NewlyLoaded: 4518 // Load module file below. 4519 break; 4520 4521 case ModuleManager::Missing: 4522 // The module file was missing; if the client can handle that, return 4523 // it. 4524 if (ClientLoadCapabilities & ARR_Missing) 4525 return Missing; 4526 4527 // Otherwise, return an error. 4528 Diag(diag::err_ast_file_not_found) 4529 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4530 << ErrorStr; 4531 return Failure; 4532 4533 case ModuleManager::OutOfDate: 4534 // We couldn't load the module file because it is out-of-date. If the 4535 // client can handle out-of-date, return it. 4536 if (ClientLoadCapabilities & ARR_OutOfDate) 4537 return OutOfDate; 4538 4539 // Otherwise, return an error. 4540 Diag(diag::err_ast_file_out_of_date) 4541 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4542 << ErrorStr; 4543 return Failure; 4544 } 4545 4546 assert(M && "Missing module file"); 4547 4548 bool ShouldFinalizePCM = false; 4549 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4550 auto &MC = getModuleManager().getModuleCache(); 4551 if (ShouldFinalizePCM) 4552 MC.finalizePCM(FileName); 4553 else 4554 MC.tryToDropPCM(FileName); 4555 }); 4556 ModuleFile &F = *M; 4557 BitstreamCursor &Stream = F.Stream; 4558 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4559 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4560 4561 // Sniff for the signature. 4562 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4563 Diag(diag::err_ast_file_invalid) 4564 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4565 return Failure; 4566 } 4567 4568 // This is used for compatibility with older PCH formats. 4569 bool HaveReadControlBlock = false; 4570 while (true) { 4571 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4572 if (!MaybeEntry) { 4573 Error(MaybeEntry.takeError()); 4574 return Failure; 4575 } 4576 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4577 4578 switch (Entry.Kind) { 4579 case llvm::BitstreamEntry::Error: 4580 case llvm::BitstreamEntry::Record: 4581 case llvm::BitstreamEntry::EndBlock: 4582 Error("invalid record at top-level of AST file"); 4583 return Failure; 4584 4585 case llvm::BitstreamEntry::SubBlock: 4586 break; 4587 } 4588 4589 switch (Entry.ID) { 4590 case CONTROL_BLOCK_ID: 4591 HaveReadControlBlock = true; 4592 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4593 case Success: 4594 // Check that we didn't try to load a non-module AST file as a module. 4595 // 4596 // FIXME: Should we also perform the converse check? Loading a module as 4597 // a PCH file sort of works, but it's a bit wonky. 4598 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4599 Type == MK_PrebuiltModule) && 4600 F.ModuleName.empty()) { 4601 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4602 if (Result != OutOfDate || 4603 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4604 Diag(diag::err_module_file_not_module) << FileName; 4605 return Result; 4606 } 4607 break; 4608 4609 case Failure: return Failure; 4610 case Missing: return Missing; 4611 case OutOfDate: return OutOfDate; 4612 case VersionMismatch: return VersionMismatch; 4613 case ConfigurationMismatch: return ConfigurationMismatch; 4614 case HadErrors: return HadErrors; 4615 } 4616 break; 4617 4618 case AST_BLOCK_ID: 4619 if (!HaveReadControlBlock) { 4620 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4621 Diag(diag::err_pch_version_too_old); 4622 return VersionMismatch; 4623 } 4624 4625 // Record that we've loaded this module. 4626 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4627 ShouldFinalizePCM = true; 4628 return Success; 4629 4630 case UNHASHED_CONTROL_BLOCK_ID: 4631 // This block is handled using look-ahead during ReadControlBlock. We 4632 // shouldn't get here! 4633 Error("malformed block record in AST file"); 4634 return Failure; 4635 4636 default: 4637 if (llvm::Error Err = Stream.SkipBlock()) { 4638 Error(std::move(Err)); 4639 return Failure; 4640 } 4641 break; 4642 } 4643 } 4644 4645 llvm_unreachable("unexpected break; expected return"); 4646 } 4647 4648 ASTReader::ASTReadResult 4649 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4650 unsigned ClientLoadCapabilities) { 4651 const HeaderSearchOptions &HSOpts = 4652 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4653 bool AllowCompatibleConfigurationMismatch = 4654 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4655 bool DisableValidation = shouldDisableValidationForFile(F); 4656 4657 ASTReadResult Result = readUnhashedControlBlockImpl( 4658 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4659 Listener.get(), 4660 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4661 4662 // If F was directly imported by another module, it's implicitly validated by 4663 // the importing module. 4664 if (DisableValidation || WasImportedBy || 4665 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4666 return Success; 4667 4668 if (Result == Failure) { 4669 Error("malformed block record in AST file"); 4670 return Failure; 4671 } 4672 4673 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4674 // If this module has already been finalized in the ModuleCache, we're stuck 4675 // with it; we can only load a single version of each module. 4676 // 4677 // This can happen when a module is imported in two contexts: in one, as a 4678 // user module; in another, as a system module (due to an import from 4679 // another module marked with the [system] flag). It usually indicates a 4680 // bug in the module map: this module should also be marked with [system]. 4681 // 4682 // If -Wno-system-headers (the default), and the first import is as a 4683 // system module, then validation will fail during the as-user import, 4684 // since -Werror flags won't have been validated. However, it's reasonable 4685 // to treat this consistently as a system module. 4686 // 4687 // If -Wsystem-headers, the PCM on disk was built with 4688 // -Wno-system-headers, and the first import is as a user module, then 4689 // validation will fail during the as-system import since the PCM on disk 4690 // doesn't guarantee that -Werror was respected. However, the -Werror 4691 // flags were checked during the initial as-user import. 4692 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4693 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4694 return Success; 4695 } 4696 } 4697 4698 return Result; 4699 } 4700 4701 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4702 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4703 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4704 bool ValidateDiagnosticOptions) { 4705 // Initialize a stream. 4706 BitstreamCursor Stream(StreamData); 4707 4708 // Sniff for the signature. 4709 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4710 // FIXME this drops the error on the floor. 4711 consumeError(std::move(Err)); 4712 return Failure; 4713 } 4714 4715 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4716 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4717 return Failure; 4718 4719 // Read all of the records in the options block. 4720 RecordData Record; 4721 ASTReadResult Result = Success; 4722 while (true) { 4723 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4724 if (!MaybeEntry) { 4725 // FIXME this drops the error on the floor. 4726 consumeError(MaybeEntry.takeError()); 4727 return Failure; 4728 } 4729 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4730 4731 switch (Entry.Kind) { 4732 case llvm::BitstreamEntry::Error: 4733 case llvm::BitstreamEntry::SubBlock: 4734 return Failure; 4735 4736 case llvm::BitstreamEntry::EndBlock: 4737 return Result; 4738 4739 case llvm::BitstreamEntry::Record: 4740 // The interesting case. 4741 break; 4742 } 4743 4744 // Read and process a record. 4745 Record.clear(); 4746 StringRef Blob; 4747 Expected<unsigned> MaybeRecordType = 4748 Stream.readRecord(Entry.ID, Record, &Blob); 4749 if (!MaybeRecordType) { 4750 // FIXME this drops the error. 4751 return Failure; 4752 } 4753 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4754 case SIGNATURE: 4755 if (F) 4756 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4757 break; 4758 case AST_BLOCK_HASH: 4759 if (F) 4760 F->ASTBlockHash = 4761 ASTFileSignature::create(Record.begin(), Record.end()); 4762 break; 4763 case DIAGNOSTIC_OPTIONS: { 4764 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4765 if (Listener && ValidateDiagnosticOptions && 4766 !AllowCompatibleConfigurationMismatch && 4767 ParseDiagnosticOptions(Record, Complain, *Listener)) 4768 Result = OutOfDate; // Don't return early. Read the signature. 4769 break; 4770 } 4771 case DIAG_PRAGMA_MAPPINGS: 4772 if (!F) 4773 break; 4774 if (F->PragmaDiagMappings.empty()) 4775 F->PragmaDiagMappings.swap(Record); 4776 else 4777 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4778 Record.begin(), Record.end()); 4779 break; 4780 case HEADER_SEARCH_ENTRY_USAGE: 4781 if (!F) 4782 break; 4783 unsigned Count = Record[0]; 4784 const char *Byte = Blob.data(); 4785 F->SearchPathUsage = llvm::BitVector(Count, false); 4786 for (unsigned I = 0; I < Count; ++Byte) 4787 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I) 4788 if (*Byte & (1 << Bit)) 4789 F->SearchPathUsage[I] = true; 4790 break; 4791 } 4792 } 4793 } 4794 4795 /// Parse a record and blob containing module file extension metadata. 4796 static bool parseModuleFileExtensionMetadata( 4797 const SmallVectorImpl<uint64_t> &Record, 4798 StringRef Blob, 4799 ModuleFileExtensionMetadata &Metadata) { 4800 if (Record.size() < 4) return true; 4801 4802 Metadata.MajorVersion = Record[0]; 4803 Metadata.MinorVersion = Record[1]; 4804 4805 unsigned BlockNameLen = Record[2]; 4806 unsigned UserInfoLen = Record[3]; 4807 4808 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4809 4810 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4811 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4812 Blob.data() + BlockNameLen + UserInfoLen); 4813 return false; 4814 } 4815 4816 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) { 4817 BitstreamCursor &Stream = F.Stream; 4818 4819 RecordData Record; 4820 while (true) { 4821 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4822 if (!MaybeEntry) 4823 return MaybeEntry.takeError(); 4824 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4825 4826 switch (Entry.Kind) { 4827 case llvm::BitstreamEntry::SubBlock: 4828 if (llvm::Error Err = Stream.SkipBlock()) 4829 return Err; 4830 continue; 4831 case llvm::BitstreamEntry::EndBlock: 4832 return llvm::Error::success(); 4833 case llvm::BitstreamEntry::Error: 4834 return llvm::createStringError(std::errc::illegal_byte_sequence, 4835 "malformed block record in AST file"); 4836 case llvm::BitstreamEntry::Record: 4837 break; 4838 } 4839 4840 Record.clear(); 4841 StringRef Blob; 4842 Expected<unsigned> MaybeRecCode = 4843 Stream.readRecord(Entry.ID, Record, &Blob); 4844 if (!MaybeRecCode) 4845 return MaybeRecCode.takeError(); 4846 switch (MaybeRecCode.get()) { 4847 case EXTENSION_METADATA: { 4848 ModuleFileExtensionMetadata Metadata; 4849 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4850 return llvm::createStringError( 4851 std::errc::illegal_byte_sequence, 4852 "malformed EXTENSION_METADATA in AST file"); 4853 4854 // Find a module file extension with this block name. 4855 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4856 if (Known == ModuleFileExtensions.end()) break; 4857 4858 // Form a reader. 4859 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4860 F, Stream)) { 4861 F.ExtensionReaders.push_back(std::move(Reader)); 4862 } 4863 4864 break; 4865 } 4866 } 4867 } 4868 4869 return llvm::Error::success(); 4870 } 4871 4872 void ASTReader::InitializeContext() { 4873 assert(ContextObj && "no context to initialize"); 4874 ASTContext &Context = *ContextObj; 4875 4876 // If there's a listener, notify them that we "read" the translation unit. 4877 if (DeserializationListener) 4878 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4879 Context.getTranslationUnitDecl()); 4880 4881 // FIXME: Find a better way to deal with collisions between these 4882 // built-in types. Right now, we just ignore the problem. 4883 4884 // Load the special types. 4885 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4886 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4887 if (!Context.CFConstantStringTypeDecl) 4888 Context.setCFConstantStringType(GetType(String)); 4889 } 4890 4891 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4892 QualType FileType = GetType(File); 4893 if (FileType.isNull()) { 4894 Error("FILE type is NULL"); 4895 return; 4896 } 4897 4898 if (!Context.FILEDecl) { 4899 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4900 Context.setFILEDecl(Typedef->getDecl()); 4901 else { 4902 const TagType *Tag = FileType->getAs<TagType>(); 4903 if (!Tag) { 4904 Error("Invalid FILE type in AST file"); 4905 return; 4906 } 4907 Context.setFILEDecl(Tag->getDecl()); 4908 } 4909 } 4910 } 4911 4912 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4913 QualType Jmp_bufType = GetType(Jmp_buf); 4914 if (Jmp_bufType.isNull()) { 4915 Error("jmp_buf type is NULL"); 4916 return; 4917 } 4918 4919 if (!Context.jmp_bufDecl) { 4920 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4921 Context.setjmp_bufDecl(Typedef->getDecl()); 4922 else { 4923 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4924 if (!Tag) { 4925 Error("Invalid jmp_buf type in AST file"); 4926 return; 4927 } 4928 Context.setjmp_bufDecl(Tag->getDecl()); 4929 } 4930 } 4931 } 4932 4933 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4934 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4935 if (Sigjmp_bufType.isNull()) { 4936 Error("sigjmp_buf type is NULL"); 4937 return; 4938 } 4939 4940 if (!Context.sigjmp_bufDecl) { 4941 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4942 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4943 else { 4944 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4945 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4946 Context.setsigjmp_bufDecl(Tag->getDecl()); 4947 } 4948 } 4949 } 4950 4951 if (unsigned ObjCIdRedef 4952 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4953 if (Context.ObjCIdRedefinitionType.isNull()) 4954 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4955 } 4956 4957 if (unsigned ObjCClassRedef 4958 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4959 if (Context.ObjCClassRedefinitionType.isNull()) 4960 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4961 } 4962 4963 if (unsigned ObjCSelRedef 4964 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4965 if (Context.ObjCSelRedefinitionType.isNull()) 4966 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4967 } 4968 4969 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4970 QualType Ucontext_tType = GetType(Ucontext_t); 4971 if (Ucontext_tType.isNull()) { 4972 Error("ucontext_t type is NULL"); 4973 return; 4974 } 4975 4976 if (!Context.ucontext_tDecl) { 4977 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4978 Context.setucontext_tDecl(Typedef->getDecl()); 4979 else { 4980 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4981 assert(Tag && "Invalid ucontext_t type in AST file"); 4982 Context.setucontext_tDecl(Tag->getDecl()); 4983 } 4984 } 4985 } 4986 } 4987 4988 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4989 4990 // If there were any CUDA special declarations, deserialize them. 4991 if (!CUDASpecialDeclRefs.empty()) { 4992 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4993 Context.setcudaConfigureCallDecl( 4994 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4995 } 4996 4997 // Re-export any modules that were imported by a non-module AST file. 4998 // FIXME: This does not make macro-only imports visible again. 4999 for (auto &Import : ImportedModules) { 5000 if (Module *Imported = getSubmodule(Import.ID)) { 5001 makeModuleVisible(Imported, Module::AllVisible, 5002 /*ImportLoc=*/Import.ImportLoc); 5003 if (Import.ImportLoc.isValid()) 5004 PP.makeModuleVisible(Imported, Import.ImportLoc); 5005 // This updates visibility for Preprocessor only. For Sema, which can be 5006 // nullptr here, we do the same later, in UpdateSema(). 5007 } 5008 } 5009 } 5010 5011 void ASTReader::finalizeForWriting() { 5012 // Nothing to do for now. 5013 } 5014 5015 /// Reads and return the signature record from \p PCH's control block, or 5016 /// else returns 0. 5017 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5018 BitstreamCursor Stream(PCH); 5019 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5020 // FIXME this drops the error on the floor. 5021 consumeError(std::move(Err)); 5022 return ASTFileSignature(); 5023 } 5024 5025 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5026 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5027 return ASTFileSignature(); 5028 5029 // Scan for SIGNATURE inside the diagnostic options block. 5030 ASTReader::RecordData Record; 5031 while (true) { 5032 Expected<llvm::BitstreamEntry> MaybeEntry = 5033 Stream.advanceSkippingSubblocks(); 5034 if (!MaybeEntry) { 5035 // FIXME this drops the error on the floor. 5036 consumeError(MaybeEntry.takeError()); 5037 return ASTFileSignature(); 5038 } 5039 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5040 5041 if (Entry.Kind != llvm::BitstreamEntry::Record) 5042 return ASTFileSignature(); 5043 5044 Record.clear(); 5045 StringRef Blob; 5046 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5047 if (!MaybeRecord) { 5048 // FIXME this drops the error on the floor. 5049 consumeError(MaybeRecord.takeError()); 5050 return ASTFileSignature(); 5051 } 5052 if (SIGNATURE == MaybeRecord.get()) 5053 return ASTFileSignature::create(Record.begin(), 5054 Record.begin() + ASTFileSignature::size); 5055 } 5056 } 5057 5058 /// Retrieve the name of the original source file name 5059 /// directly from the AST file, without actually loading the AST 5060 /// file. 5061 std::string ASTReader::getOriginalSourceFile( 5062 const std::string &ASTFileName, FileManager &FileMgr, 5063 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5064 // Open the AST file. 5065 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5066 if (!Buffer) { 5067 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5068 << ASTFileName << Buffer.getError().message(); 5069 return std::string(); 5070 } 5071 5072 // Initialize the stream 5073 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5074 5075 // Sniff for the signature. 5076 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5077 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5078 return std::string(); 5079 } 5080 5081 // Scan for the CONTROL_BLOCK_ID block. 5082 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5083 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5084 return std::string(); 5085 } 5086 5087 // Scan for ORIGINAL_FILE inside the control block. 5088 RecordData Record; 5089 while (true) { 5090 Expected<llvm::BitstreamEntry> MaybeEntry = 5091 Stream.advanceSkippingSubblocks(); 5092 if (!MaybeEntry) { 5093 // FIXME this drops errors on the floor. 5094 consumeError(MaybeEntry.takeError()); 5095 return std::string(); 5096 } 5097 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5098 5099 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5100 return std::string(); 5101 5102 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5103 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5104 return std::string(); 5105 } 5106 5107 Record.clear(); 5108 StringRef Blob; 5109 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5110 if (!MaybeRecord) { 5111 // FIXME this drops the errors on the floor. 5112 consumeError(MaybeRecord.takeError()); 5113 return std::string(); 5114 } 5115 if (ORIGINAL_FILE == MaybeRecord.get()) 5116 return Blob.str(); 5117 } 5118 } 5119 5120 namespace { 5121 5122 class SimplePCHValidator : public ASTReaderListener { 5123 const LangOptions &ExistingLangOpts; 5124 const TargetOptions &ExistingTargetOpts; 5125 const PreprocessorOptions &ExistingPPOpts; 5126 std::string ExistingModuleCachePath; 5127 FileManager &FileMgr; 5128 5129 public: 5130 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5131 const TargetOptions &ExistingTargetOpts, 5132 const PreprocessorOptions &ExistingPPOpts, 5133 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5134 : ExistingLangOpts(ExistingLangOpts), 5135 ExistingTargetOpts(ExistingTargetOpts), 5136 ExistingPPOpts(ExistingPPOpts), 5137 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5138 5139 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5140 bool AllowCompatibleDifferences) override { 5141 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5142 AllowCompatibleDifferences); 5143 } 5144 5145 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5146 bool AllowCompatibleDifferences) override { 5147 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5148 AllowCompatibleDifferences); 5149 } 5150 5151 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5152 StringRef SpecificModuleCachePath, 5153 bool Complain) override { 5154 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5155 ExistingModuleCachePath, nullptr, 5156 ExistingLangOpts, ExistingPPOpts); 5157 } 5158 5159 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5160 bool Complain, 5161 std::string &SuggestedPredefines) override { 5162 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5163 SuggestedPredefines, ExistingLangOpts); 5164 } 5165 }; 5166 5167 } // namespace 5168 5169 bool ASTReader::readASTFileControlBlock( 5170 StringRef Filename, FileManager &FileMgr, 5171 const PCHContainerReader &PCHContainerRdr, 5172 bool FindModuleFileExtensions, 5173 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5174 // Open the AST file. 5175 // FIXME: This allows use of the VFS; we do not allow use of the 5176 // VFS when actually loading a module. 5177 auto Buffer = FileMgr.getBufferForFile(Filename); 5178 if (!Buffer) { 5179 return true; 5180 } 5181 5182 // Initialize the stream 5183 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5184 BitstreamCursor Stream(Bytes); 5185 5186 // Sniff for the signature. 5187 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5188 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5189 return true; 5190 } 5191 5192 // Scan for the CONTROL_BLOCK_ID block. 5193 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5194 return true; 5195 5196 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5197 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5198 bool NeedsImports = Listener.needsImportVisitation(); 5199 BitstreamCursor InputFilesCursor; 5200 5201 RecordData Record; 5202 std::string ModuleDir; 5203 bool DoneWithControlBlock = false; 5204 while (!DoneWithControlBlock) { 5205 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5206 if (!MaybeEntry) { 5207 // FIXME this drops the error on the floor. 5208 consumeError(MaybeEntry.takeError()); 5209 return true; 5210 } 5211 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5212 5213 switch (Entry.Kind) { 5214 case llvm::BitstreamEntry::SubBlock: { 5215 switch (Entry.ID) { 5216 case OPTIONS_BLOCK_ID: { 5217 std::string IgnoredSuggestedPredefines; 5218 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5219 /*AllowCompatibleConfigurationMismatch*/ false, 5220 Listener, IgnoredSuggestedPredefines) != Success) 5221 return true; 5222 break; 5223 } 5224 5225 case INPUT_FILES_BLOCK_ID: 5226 InputFilesCursor = Stream; 5227 if (llvm::Error Err = Stream.SkipBlock()) { 5228 // FIXME this drops the error on the floor. 5229 consumeError(std::move(Err)); 5230 return true; 5231 } 5232 if (NeedsInputFiles && 5233 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5234 return true; 5235 break; 5236 5237 default: 5238 if (llvm::Error Err = Stream.SkipBlock()) { 5239 // FIXME this drops the error on the floor. 5240 consumeError(std::move(Err)); 5241 return true; 5242 } 5243 break; 5244 } 5245 5246 continue; 5247 } 5248 5249 case llvm::BitstreamEntry::EndBlock: 5250 DoneWithControlBlock = true; 5251 break; 5252 5253 case llvm::BitstreamEntry::Error: 5254 return true; 5255 5256 case llvm::BitstreamEntry::Record: 5257 break; 5258 } 5259 5260 if (DoneWithControlBlock) break; 5261 5262 Record.clear(); 5263 StringRef Blob; 5264 Expected<unsigned> MaybeRecCode = 5265 Stream.readRecord(Entry.ID, Record, &Blob); 5266 if (!MaybeRecCode) { 5267 // FIXME this drops the error. 5268 return Failure; 5269 } 5270 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5271 case METADATA: 5272 if (Record[0] != VERSION_MAJOR) 5273 return true; 5274 if (Listener.ReadFullVersionInformation(Blob)) 5275 return true; 5276 break; 5277 case MODULE_NAME: 5278 Listener.ReadModuleName(Blob); 5279 break; 5280 case MODULE_DIRECTORY: 5281 ModuleDir = std::string(Blob); 5282 break; 5283 case MODULE_MAP_FILE: { 5284 unsigned Idx = 0; 5285 auto Path = ReadString(Record, Idx); 5286 ResolveImportedPath(Path, ModuleDir); 5287 Listener.ReadModuleMapFile(Path); 5288 break; 5289 } 5290 case INPUT_FILE_OFFSETS: { 5291 if (!NeedsInputFiles) 5292 break; 5293 5294 unsigned NumInputFiles = Record[0]; 5295 unsigned NumUserFiles = Record[1]; 5296 const llvm::support::unaligned_uint64_t *InputFileOffs = 5297 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5298 for (unsigned I = 0; I != NumInputFiles; ++I) { 5299 // Go find this input file. 5300 bool isSystemFile = I >= NumUserFiles; 5301 5302 if (isSystemFile && !NeedsSystemInputFiles) 5303 break; // the rest are system input files 5304 5305 BitstreamCursor &Cursor = InputFilesCursor; 5306 SavedStreamPosition SavedPosition(Cursor); 5307 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5308 // FIXME this drops errors on the floor. 5309 consumeError(std::move(Err)); 5310 } 5311 5312 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5313 if (!MaybeCode) { 5314 // FIXME this drops errors on the floor. 5315 consumeError(MaybeCode.takeError()); 5316 } 5317 unsigned Code = MaybeCode.get(); 5318 5319 RecordData Record; 5320 StringRef Blob; 5321 bool shouldContinue = false; 5322 Expected<unsigned> MaybeRecordType = 5323 Cursor.readRecord(Code, Record, &Blob); 5324 if (!MaybeRecordType) { 5325 // FIXME this drops errors on the floor. 5326 consumeError(MaybeRecordType.takeError()); 5327 } 5328 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5329 case INPUT_FILE_HASH: 5330 break; 5331 case INPUT_FILE: 5332 bool Overridden = static_cast<bool>(Record[3]); 5333 std::string Filename = std::string(Blob); 5334 ResolveImportedPath(Filename, ModuleDir); 5335 shouldContinue = Listener.visitInputFile( 5336 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5337 break; 5338 } 5339 if (!shouldContinue) 5340 break; 5341 } 5342 break; 5343 } 5344 5345 case IMPORTS: { 5346 if (!NeedsImports) 5347 break; 5348 5349 unsigned Idx = 0, N = Record.size(); 5350 while (Idx < N) { 5351 // Read information about the AST file. 5352 Idx += 5353 1 + 1 + 1 + 1 + 5354 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5355 std::string ModuleName = ReadString(Record, Idx); 5356 std::string Filename = ReadString(Record, Idx); 5357 ResolveImportedPath(Filename, ModuleDir); 5358 Listener.visitImport(ModuleName, Filename); 5359 } 5360 break; 5361 } 5362 5363 default: 5364 // No other validation to perform. 5365 break; 5366 } 5367 } 5368 5369 // Look for module file extension blocks, if requested. 5370 if (FindModuleFileExtensions) { 5371 BitstreamCursor SavedStream = Stream; 5372 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5373 bool DoneWithExtensionBlock = false; 5374 while (!DoneWithExtensionBlock) { 5375 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5376 if (!MaybeEntry) { 5377 // FIXME this drops the error. 5378 return true; 5379 } 5380 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5381 5382 switch (Entry.Kind) { 5383 case llvm::BitstreamEntry::SubBlock: 5384 if (llvm::Error Err = Stream.SkipBlock()) { 5385 // FIXME this drops the error on the floor. 5386 consumeError(std::move(Err)); 5387 return true; 5388 } 5389 continue; 5390 5391 case llvm::BitstreamEntry::EndBlock: 5392 DoneWithExtensionBlock = true; 5393 continue; 5394 5395 case llvm::BitstreamEntry::Error: 5396 return true; 5397 5398 case llvm::BitstreamEntry::Record: 5399 break; 5400 } 5401 5402 Record.clear(); 5403 StringRef Blob; 5404 Expected<unsigned> MaybeRecCode = 5405 Stream.readRecord(Entry.ID, Record, &Blob); 5406 if (!MaybeRecCode) { 5407 // FIXME this drops the error. 5408 return true; 5409 } 5410 switch (MaybeRecCode.get()) { 5411 case EXTENSION_METADATA: { 5412 ModuleFileExtensionMetadata Metadata; 5413 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5414 return true; 5415 5416 Listener.readModuleFileExtension(Metadata); 5417 break; 5418 } 5419 } 5420 } 5421 } 5422 Stream = SavedStream; 5423 } 5424 5425 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5426 if (readUnhashedControlBlockImpl( 5427 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5428 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5429 ValidateDiagnosticOptions) != Success) 5430 return true; 5431 5432 return false; 5433 } 5434 5435 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5436 const PCHContainerReader &PCHContainerRdr, 5437 const LangOptions &LangOpts, 5438 const TargetOptions &TargetOpts, 5439 const PreprocessorOptions &PPOpts, 5440 StringRef ExistingModuleCachePath) { 5441 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5442 ExistingModuleCachePath, FileMgr); 5443 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5444 /*FindModuleFileExtensions=*/false, 5445 validator, 5446 /*ValidateDiagnosticOptions=*/true); 5447 } 5448 5449 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F, 5450 unsigned ClientLoadCapabilities) { 5451 // Enter the submodule block. 5452 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) 5453 return Err; 5454 5455 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5456 bool First = true; 5457 Module *CurrentModule = nullptr; 5458 RecordData Record; 5459 while (true) { 5460 Expected<llvm::BitstreamEntry> MaybeEntry = 5461 F.Stream.advanceSkippingSubblocks(); 5462 if (!MaybeEntry) 5463 return MaybeEntry.takeError(); 5464 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5465 5466 switch (Entry.Kind) { 5467 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5468 case llvm::BitstreamEntry::Error: 5469 return llvm::createStringError(std::errc::illegal_byte_sequence, 5470 "malformed block record in AST file"); 5471 case llvm::BitstreamEntry::EndBlock: 5472 return llvm::Error::success(); 5473 case llvm::BitstreamEntry::Record: 5474 // The interesting case. 5475 break; 5476 } 5477 5478 // Read a record. 5479 StringRef Blob; 5480 Record.clear(); 5481 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5482 if (!MaybeKind) 5483 return MaybeKind.takeError(); 5484 unsigned Kind = MaybeKind.get(); 5485 5486 if ((Kind == SUBMODULE_METADATA) != First) 5487 return llvm::createStringError( 5488 std::errc::illegal_byte_sequence, 5489 "submodule metadata record should be at beginning of block"); 5490 First = false; 5491 5492 // Submodule information is only valid if we have a current module. 5493 // FIXME: Should we error on these cases? 5494 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5495 Kind != SUBMODULE_DEFINITION) 5496 continue; 5497 5498 switch (Kind) { 5499 default: // Default behavior: ignore. 5500 break; 5501 5502 case SUBMODULE_DEFINITION: { 5503 if (Record.size() < 12) 5504 return llvm::createStringError(std::errc::illegal_byte_sequence, 5505 "malformed module definition"); 5506 5507 StringRef Name = Blob; 5508 unsigned Idx = 0; 5509 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5510 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5511 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5512 bool IsFramework = Record[Idx++]; 5513 bool IsExplicit = Record[Idx++]; 5514 bool IsSystem = Record[Idx++]; 5515 bool IsExternC = Record[Idx++]; 5516 bool InferSubmodules = Record[Idx++]; 5517 bool InferExplicitSubmodules = Record[Idx++]; 5518 bool InferExportWildcard = Record[Idx++]; 5519 bool ConfigMacrosExhaustive = Record[Idx++]; 5520 bool ModuleMapIsPrivate = Record[Idx++]; 5521 5522 Module *ParentModule = nullptr; 5523 if (Parent) 5524 ParentModule = getSubmodule(Parent); 5525 5526 // Retrieve this (sub)module from the module map, creating it if 5527 // necessary. 5528 CurrentModule = 5529 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5530 .first; 5531 5532 // FIXME: set the definition loc for CurrentModule, or call 5533 // ModMap.setInferredModuleAllowedBy() 5534 5535 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5536 if (GlobalIndex >= SubmodulesLoaded.size() || 5537 SubmodulesLoaded[GlobalIndex]) 5538 return llvm::createStringError(std::errc::invalid_argument, 5539 "too many submodules"); 5540 5541 if (!ParentModule) { 5542 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5543 // Don't emit module relocation error if we have -fno-validate-pch 5544 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 5545 DisableValidationForModuleKind::Module) && 5546 CurFile != F.File) { 5547 auto ConflictError = 5548 PartialDiagnostic(diag::err_module_file_conflict, 5549 ContextObj->DiagAllocator) 5550 << CurrentModule->getTopLevelModuleName() << CurFile->getName() 5551 << F.File->getName(); 5552 return DiagnosticError::create(CurrentImportLoc, ConflictError); 5553 } 5554 } 5555 5556 F.DidReadTopLevelSubmodule = true; 5557 CurrentModule->setASTFile(F.File); 5558 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5559 } 5560 5561 CurrentModule->Kind = Kind; 5562 CurrentModule->Signature = F.Signature; 5563 CurrentModule->IsFromModuleFile = true; 5564 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5565 CurrentModule->IsExternC = IsExternC; 5566 CurrentModule->InferSubmodules = InferSubmodules; 5567 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5568 CurrentModule->InferExportWildcard = InferExportWildcard; 5569 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5570 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5571 if (DeserializationListener) 5572 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5573 5574 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5575 5576 // Clear out data that will be replaced by what is in the module file. 5577 CurrentModule->LinkLibraries.clear(); 5578 CurrentModule->ConfigMacros.clear(); 5579 CurrentModule->UnresolvedConflicts.clear(); 5580 CurrentModule->Conflicts.clear(); 5581 5582 // The module is available unless it's missing a requirement; relevant 5583 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5584 // Missing headers that were present when the module was built do not 5585 // make it unavailable -- if we got this far, this must be an explicitly 5586 // imported module file. 5587 CurrentModule->Requirements.clear(); 5588 CurrentModule->MissingHeaders.clear(); 5589 CurrentModule->IsUnimportable = 5590 ParentModule && ParentModule->IsUnimportable; 5591 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5592 break; 5593 } 5594 5595 case SUBMODULE_UMBRELLA_HEADER: { 5596 // FIXME: This doesn't work for framework modules as `Filename` is the 5597 // name as written in the module file and does not include 5598 // `Headers/`, so this path will never exist. 5599 std::string Filename = std::string(Blob); 5600 ResolveImportedPath(F, Filename); 5601 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5602 if (!CurrentModule->getUmbrellaHeader()) { 5603 // FIXME: NameAsWritten 5604 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, ""); 5605 } 5606 // Note that it's too late at this point to return out of date if the 5607 // name from the PCM doesn't match up with the one in the module map, 5608 // but also quite unlikely since we will have already checked the 5609 // modification time and size of the module map file itself. 5610 } 5611 break; 5612 } 5613 5614 case SUBMODULE_HEADER: 5615 case SUBMODULE_EXCLUDED_HEADER: 5616 case SUBMODULE_PRIVATE_HEADER: 5617 // We lazily associate headers with their modules via the HeaderInfo table. 5618 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5619 // of complete filenames or remove it entirely. 5620 break; 5621 5622 case SUBMODULE_TEXTUAL_HEADER: 5623 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5624 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5625 // them here. 5626 break; 5627 5628 case SUBMODULE_TOPHEADER: 5629 CurrentModule->addTopHeaderFilename(Blob); 5630 break; 5631 5632 case SUBMODULE_UMBRELLA_DIR: { 5633 // See comments in SUBMODULE_UMBRELLA_HEADER 5634 std::string Dirname = std::string(Blob); 5635 ResolveImportedPath(F, Dirname); 5636 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5637 if (!CurrentModule->getUmbrellaDir()) { 5638 // FIXME: NameAsWritten 5639 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, ""); 5640 } 5641 } 5642 break; 5643 } 5644 5645 case SUBMODULE_METADATA: { 5646 F.BaseSubmoduleID = getTotalNumSubmodules(); 5647 F.LocalNumSubmodules = Record[0]; 5648 unsigned LocalBaseSubmoduleID = Record[1]; 5649 if (F.LocalNumSubmodules > 0) { 5650 // Introduce the global -> local mapping for submodules within this 5651 // module. 5652 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5653 5654 // Introduce the local -> global mapping for submodules within this 5655 // module. 5656 F.SubmoduleRemap.insertOrReplace( 5657 std::make_pair(LocalBaseSubmoduleID, 5658 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5659 5660 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5661 } 5662 break; 5663 } 5664 5665 case SUBMODULE_IMPORTS: 5666 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5667 UnresolvedModuleRef Unresolved; 5668 Unresolved.File = &F; 5669 Unresolved.Mod = CurrentModule; 5670 Unresolved.ID = Record[Idx]; 5671 Unresolved.Kind = UnresolvedModuleRef::Import; 5672 Unresolved.IsWildcard = false; 5673 UnresolvedModuleRefs.push_back(Unresolved); 5674 } 5675 break; 5676 5677 case SUBMODULE_EXPORTS: 5678 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5679 UnresolvedModuleRef Unresolved; 5680 Unresolved.File = &F; 5681 Unresolved.Mod = CurrentModule; 5682 Unresolved.ID = Record[Idx]; 5683 Unresolved.Kind = UnresolvedModuleRef::Export; 5684 Unresolved.IsWildcard = Record[Idx + 1]; 5685 UnresolvedModuleRefs.push_back(Unresolved); 5686 } 5687 5688 // Once we've loaded the set of exports, there's no reason to keep 5689 // the parsed, unresolved exports around. 5690 CurrentModule->UnresolvedExports.clear(); 5691 break; 5692 5693 case SUBMODULE_REQUIRES: 5694 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5695 PP.getTargetInfo()); 5696 break; 5697 5698 case SUBMODULE_LINK_LIBRARY: 5699 ModMap.resolveLinkAsDependencies(CurrentModule); 5700 CurrentModule->LinkLibraries.push_back( 5701 Module::LinkLibrary(std::string(Blob), Record[0])); 5702 break; 5703 5704 case SUBMODULE_CONFIG_MACRO: 5705 CurrentModule->ConfigMacros.push_back(Blob.str()); 5706 break; 5707 5708 case SUBMODULE_CONFLICT: { 5709 UnresolvedModuleRef Unresolved; 5710 Unresolved.File = &F; 5711 Unresolved.Mod = CurrentModule; 5712 Unresolved.ID = Record[0]; 5713 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5714 Unresolved.IsWildcard = false; 5715 Unresolved.String = Blob; 5716 UnresolvedModuleRefs.push_back(Unresolved); 5717 break; 5718 } 5719 5720 case SUBMODULE_INITIALIZERS: { 5721 if (!ContextObj) 5722 break; 5723 SmallVector<uint32_t, 16> Inits; 5724 for (auto &ID : Record) 5725 Inits.push_back(getGlobalDeclID(F, ID)); 5726 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5727 break; 5728 } 5729 5730 case SUBMODULE_EXPORT_AS: 5731 CurrentModule->ExportAsModule = Blob.str(); 5732 ModMap.addLinkAsDependency(CurrentModule); 5733 break; 5734 } 5735 } 5736 } 5737 5738 /// Parse the record that corresponds to a LangOptions data 5739 /// structure. 5740 /// 5741 /// This routine parses the language options from the AST file and then gives 5742 /// them to the AST listener if one is set. 5743 /// 5744 /// \returns true if the listener deems the file unacceptable, false otherwise. 5745 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5746 bool Complain, 5747 ASTReaderListener &Listener, 5748 bool AllowCompatibleDifferences) { 5749 LangOptions LangOpts; 5750 unsigned Idx = 0; 5751 #define LANGOPT(Name, Bits, Default, Description) \ 5752 LangOpts.Name = Record[Idx++]; 5753 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5754 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5755 #include "clang/Basic/LangOptions.def" 5756 #define SANITIZER(NAME, ID) \ 5757 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5758 #include "clang/Basic/Sanitizers.def" 5759 5760 for (unsigned N = Record[Idx++]; N; --N) 5761 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5762 5763 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5764 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5765 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5766 5767 LangOpts.CurrentModule = ReadString(Record, Idx); 5768 5769 // Comment options. 5770 for (unsigned N = Record[Idx++]; N; --N) { 5771 LangOpts.CommentOpts.BlockCommandNames.push_back( 5772 ReadString(Record, Idx)); 5773 } 5774 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5775 5776 // OpenMP offloading options. 5777 for (unsigned N = Record[Idx++]; N; --N) { 5778 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5779 } 5780 5781 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5782 5783 return Listener.ReadLanguageOptions(LangOpts, Complain, 5784 AllowCompatibleDifferences); 5785 } 5786 5787 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5788 ASTReaderListener &Listener, 5789 bool AllowCompatibleDifferences) { 5790 unsigned Idx = 0; 5791 TargetOptions TargetOpts; 5792 TargetOpts.Triple = ReadString(Record, Idx); 5793 TargetOpts.CPU = ReadString(Record, Idx); 5794 TargetOpts.TuneCPU = ReadString(Record, Idx); 5795 TargetOpts.ABI = ReadString(Record, Idx); 5796 for (unsigned N = Record[Idx++]; N; --N) { 5797 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5798 } 5799 for (unsigned N = Record[Idx++]; N; --N) { 5800 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5801 } 5802 5803 return Listener.ReadTargetOptions(TargetOpts, Complain, 5804 AllowCompatibleDifferences); 5805 } 5806 5807 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5808 ASTReaderListener &Listener) { 5809 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5810 unsigned Idx = 0; 5811 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5812 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5813 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5814 #include "clang/Basic/DiagnosticOptions.def" 5815 5816 for (unsigned N = Record[Idx++]; N; --N) 5817 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5818 for (unsigned N = Record[Idx++]; N; --N) 5819 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5820 5821 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5822 } 5823 5824 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5825 ASTReaderListener &Listener) { 5826 FileSystemOptions FSOpts; 5827 unsigned Idx = 0; 5828 FSOpts.WorkingDir = ReadString(Record, Idx); 5829 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5830 } 5831 5832 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5833 bool Complain, 5834 ASTReaderListener &Listener) { 5835 HeaderSearchOptions HSOpts; 5836 unsigned Idx = 0; 5837 HSOpts.Sysroot = ReadString(Record, Idx); 5838 5839 // Include entries. 5840 for (unsigned N = Record[Idx++]; N; --N) { 5841 std::string Path = ReadString(Record, Idx); 5842 frontend::IncludeDirGroup Group 5843 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5844 bool IsFramework = Record[Idx++]; 5845 bool IgnoreSysRoot = Record[Idx++]; 5846 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5847 IgnoreSysRoot); 5848 } 5849 5850 // System header prefixes. 5851 for (unsigned N = Record[Idx++]; N; --N) { 5852 std::string Prefix = ReadString(Record, Idx); 5853 bool IsSystemHeader = Record[Idx++]; 5854 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5855 } 5856 5857 HSOpts.ResourceDir = ReadString(Record, Idx); 5858 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5859 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5860 HSOpts.DisableModuleHash = Record[Idx++]; 5861 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5862 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5863 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5864 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5865 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5866 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5867 HSOpts.UseLibcxx = Record[Idx++]; 5868 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5869 5870 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5871 Complain); 5872 } 5873 5874 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5875 bool Complain, 5876 ASTReaderListener &Listener, 5877 std::string &SuggestedPredefines) { 5878 PreprocessorOptions PPOpts; 5879 unsigned Idx = 0; 5880 5881 // Macro definitions/undefs 5882 for (unsigned N = Record[Idx++]; N; --N) { 5883 std::string Macro = ReadString(Record, Idx); 5884 bool IsUndef = Record[Idx++]; 5885 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5886 } 5887 5888 // Includes 5889 for (unsigned N = Record[Idx++]; N; --N) { 5890 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5891 } 5892 5893 // Macro Includes 5894 for (unsigned N = Record[Idx++]; N; --N) { 5895 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5896 } 5897 5898 PPOpts.UsePredefines = Record[Idx++]; 5899 PPOpts.DetailedRecord = Record[Idx++]; 5900 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5901 PPOpts.ObjCXXARCStandardLibrary = 5902 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5903 SuggestedPredefines.clear(); 5904 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5905 SuggestedPredefines); 5906 } 5907 5908 std::pair<ModuleFile *, unsigned> 5909 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5910 GlobalPreprocessedEntityMapType::iterator 5911 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5912 assert(I != GlobalPreprocessedEntityMap.end() && 5913 "Corrupted global preprocessed entity map"); 5914 ModuleFile *M = I->second; 5915 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5916 return std::make_pair(M, LocalIndex); 5917 } 5918 5919 llvm::iterator_range<PreprocessingRecord::iterator> 5920 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5921 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5922 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5923 Mod.NumPreprocessedEntities); 5924 5925 return llvm::make_range(PreprocessingRecord::iterator(), 5926 PreprocessingRecord::iterator()); 5927 } 5928 5929 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName, 5930 unsigned int ClientLoadCapabilities) { 5931 return ClientLoadCapabilities & ARR_OutOfDate && 5932 !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName); 5933 } 5934 5935 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5936 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5937 return llvm::make_range( 5938 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5939 ModuleDeclIterator(this, &Mod, 5940 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5941 } 5942 5943 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5944 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5945 assert(I != GlobalSkippedRangeMap.end() && 5946 "Corrupted global skipped range map"); 5947 ModuleFile *M = I->second; 5948 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5949 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5950 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5951 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5952 TranslateSourceLocation(*M, RawRange.getEnd())); 5953 assert(Range.isValid()); 5954 return Range; 5955 } 5956 5957 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5958 PreprocessedEntityID PPID = Index+1; 5959 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5960 ModuleFile &M = *PPInfo.first; 5961 unsigned LocalIndex = PPInfo.second; 5962 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5963 5964 if (!PP.getPreprocessingRecord()) { 5965 Error("no preprocessing record"); 5966 return nullptr; 5967 } 5968 5969 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5970 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5971 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5972 Error(std::move(Err)); 5973 return nullptr; 5974 } 5975 5976 Expected<llvm::BitstreamEntry> MaybeEntry = 5977 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5978 if (!MaybeEntry) { 5979 Error(MaybeEntry.takeError()); 5980 return nullptr; 5981 } 5982 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5983 5984 if (Entry.Kind != llvm::BitstreamEntry::Record) 5985 return nullptr; 5986 5987 // Read the record. 5988 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5989 TranslateSourceLocation(M, PPOffs.getEnd())); 5990 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5991 StringRef Blob; 5992 RecordData Record; 5993 Expected<unsigned> MaybeRecType = 5994 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5995 if (!MaybeRecType) { 5996 Error(MaybeRecType.takeError()); 5997 return nullptr; 5998 } 5999 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 6000 case PPD_MACRO_EXPANSION: { 6001 bool isBuiltin = Record[0]; 6002 IdentifierInfo *Name = nullptr; 6003 MacroDefinitionRecord *Def = nullptr; 6004 if (isBuiltin) 6005 Name = getLocalIdentifier(M, Record[1]); 6006 else { 6007 PreprocessedEntityID GlobalID = 6008 getGlobalPreprocessedEntityID(M, Record[1]); 6009 Def = cast<MacroDefinitionRecord>( 6010 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 6011 } 6012 6013 MacroExpansion *ME; 6014 if (isBuiltin) 6015 ME = new (PPRec) MacroExpansion(Name, Range); 6016 else 6017 ME = new (PPRec) MacroExpansion(Def, Range); 6018 6019 return ME; 6020 } 6021 6022 case PPD_MACRO_DEFINITION: { 6023 // Decode the identifier info and then check again; if the macro is 6024 // still defined and associated with the identifier, 6025 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6026 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6027 6028 if (DeserializationListener) 6029 DeserializationListener->MacroDefinitionRead(PPID, MD); 6030 6031 return MD; 6032 } 6033 6034 case PPD_INCLUSION_DIRECTIVE: { 6035 const char *FullFileNameStart = Blob.data() + Record[0]; 6036 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6037 const FileEntry *File = nullptr; 6038 if (!FullFileName.empty()) 6039 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6040 File = *FE; 6041 6042 // FIXME: Stable encoding 6043 InclusionDirective::InclusionKind Kind 6044 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6045 InclusionDirective *ID 6046 = new (PPRec) InclusionDirective(PPRec, Kind, 6047 StringRef(Blob.data(), Record[0]), 6048 Record[1], Record[3], 6049 File, 6050 Range); 6051 return ID; 6052 } 6053 } 6054 6055 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6056 } 6057 6058 /// Find the next module that contains entities and return the ID 6059 /// of the first entry. 6060 /// 6061 /// \param SLocMapI points at a chunk of a module that contains no 6062 /// preprocessed entities or the entities it contains are not the ones we are 6063 /// looking for. 6064 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6065 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6066 ++SLocMapI; 6067 for (GlobalSLocOffsetMapType::const_iterator 6068 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6069 ModuleFile &M = *SLocMapI->second; 6070 if (M.NumPreprocessedEntities) 6071 return M.BasePreprocessedEntityID; 6072 } 6073 6074 return getTotalNumPreprocessedEntities(); 6075 } 6076 6077 namespace { 6078 6079 struct PPEntityComp { 6080 const ASTReader &Reader; 6081 ModuleFile &M; 6082 6083 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6084 6085 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6086 SourceLocation LHS = getLoc(L); 6087 SourceLocation RHS = getLoc(R); 6088 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6089 } 6090 6091 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6092 SourceLocation LHS = getLoc(L); 6093 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6094 } 6095 6096 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6097 SourceLocation RHS = getLoc(R); 6098 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6099 } 6100 6101 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6102 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6103 } 6104 }; 6105 6106 } // namespace 6107 6108 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6109 bool EndsAfter) const { 6110 if (SourceMgr.isLocalSourceLocation(Loc)) 6111 return getTotalNumPreprocessedEntities(); 6112 6113 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6114 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6115 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6116 "Corrupted global sloc offset map"); 6117 6118 if (SLocMapI->second->NumPreprocessedEntities == 0) 6119 return findNextPreprocessedEntity(SLocMapI); 6120 6121 ModuleFile &M = *SLocMapI->second; 6122 6123 using pp_iterator = const PPEntityOffset *; 6124 6125 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6126 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6127 6128 size_t Count = M.NumPreprocessedEntities; 6129 size_t Half; 6130 pp_iterator First = pp_begin; 6131 pp_iterator PPI; 6132 6133 if (EndsAfter) { 6134 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6135 PPEntityComp(*this, M)); 6136 } else { 6137 // Do a binary search manually instead of using std::lower_bound because 6138 // The end locations of entities may be unordered (when a macro expansion 6139 // is inside another macro argument), but for this case it is not important 6140 // whether we get the first macro expansion or its containing macro. 6141 while (Count > 0) { 6142 Half = Count / 2; 6143 PPI = First; 6144 std::advance(PPI, Half); 6145 if (SourceMgr.isBeforeInTranslationUnit( 6146 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6147 First = PPI; 6148 ++First; 6149 Count = Count - Half - 1; 6150 } else 6151 Count = Half; 6152 } 6153 } 6154 6155 if (PPI == pp_end) 6156 return findNextPreprocessedEntity(SLocMapI); 6157 6158 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6159 } 6160 6161 /// Returns a pair of [Begin, End) indices of preallocated 6162 /// preprocessed entities that \arg Range encompasses. 6163 std::pair<unsigned, unsigned> 6164 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6165 if (Range.isInvalid()) 6166 return std::make_pair(0,0); 6167 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6168 6169 PreprocessedEntityID BeginID = 6170 findPreprocessedEntity(Range.getBegin(), false); 6171 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6172 return std::make_pair(BeginID, EndID); 6173 } 6174 6175 /// Optionally returns true or false if the preallocated preprocessed 6176 /// entity with index \arg Index came from file \arg FID. 6177 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6178 FileID FID) { 6179 if (FID.isInvalid()) 6180 return false; 6181 6182 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6183 ModuleFile &M = *PPInfo.first; 6184 unsigned LocalIndex = PPInfo.second; 6185 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6186 6187 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6188 if (Loc.isInvalid()) 6189 return false; 6190 6191 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6192 return true; 6193 else 6194 return false; 6195 } 6196 6197 namespace { 6198 6199 /// Visitor used to search for information about a header file. 6200 class HeaderFileInfoVisitor { 6201 const FileEntry *FE; 6202 Optional<HeaderFileInfo> HFI; 6203 6204 public: 6205 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6206 6207 bool operator()(ModuleFile &M) { 6208 HeaderFileInfoLookupTable *Table 6209 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6210 if (!Table) 6211 return false; 6212 6213 // Look in the on-disk hash table for an entry for this file name. 6214 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6215 if (Pos == Table->end()) 6216 return false; 6217 6218 HFI = *Pos; 6219 return true; 6220 } 6221 6222 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6223 }; 6224 6225 } // namespace 6226 6227 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6228 HeaderFileInfoVisitor Visitor(FE); 6229 ModuleMgr.visit(Visitor); 6230 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6231 return *HFI; 6232 6233 return HeaderFileInfo(); 6234 } 6235 6236 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6237 using DiagState = DiagnosticsEngine::DiagState; 6238 SmallVector<DiagState *, 32> DiagStates; 6239 6240 for (ModuleFile &F : ModuleMgr) { 6241 unsigned Idx = 0; 6242 auto &Record = F.PragmaDiagMappings; 6243 if (Record.empty()) 6244 continue; 6245 6246 DiagStates.clear(); 6247 6248 auto ReadDiagState = 6249 [&](const DiagState &BasedOn, SourceLocation Loc, 6250 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6251 unsigned BackrefID = Record[Idx++]; 6252 if (BackrefID != 0) 6253 return DiagStates[BackrefID - 1]; 6254 6255 // A new DiagState was created here. 6256 Diag.DiagStates.push_back(BasedOn); 6257 DiagState *NewState = &Diag.DiagStates.back(); 6258 DiagStates.push_back(NewState); 6259 unsigned Size = Record[Idx++]; 6260 assert(Idx + Size * 2 <= Record.size() && 6261 "Invalid data, not enough diag/map pairs"); 6262 while (Size--) { 6263 unsigned DiagID = Record[Idx++]; 6264 DiagnosticMapping NewMapping = 6265 DiagnosticMapping::deserialize(Record[Idx++]); 6266 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6267 continue; 6268 6269 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6270 6271 // If this mapping was specified as a warning but the severity was 6272 // upgraded due to diagnostic settings, simulate the current diagnostic 6273 // settings (and use a warning). 6274 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6275 NewMapping.setSeverity(diag::Severity::Warning); 6276 NewMapping.setUpgradedFromWarning(false); 6277 } 6278 6279 Mapping = NewMapping; 6280 } 6281 return NewState; 6282 }; 6283 6284 // Read the first state. 6285 DiagState *FirstState; 6286 if (F.Kind == MK_ImplicitModule) { 6287 // Implicitly-built modules are reused with different diagnostic 6288 // settings. Use the initial diagnostic state from Diag to simulate this 6289 // compilation's diagnostic settings. 6290 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6291 DiagStates.push_back(FirstState); 6292 6293 // Skip the initial diagnostic state from the serialized module. 6294 assert(Record[1] == 0 && 6295 "Invalid data, unexpected backref in initial state"); 6296 Idx = 3 + Record[2] * 2; 6297 assert(Idx < Record.size() && 6298 "Invalid data, not enough state change pairs in initial state"); 6299 } else if (F.isModule()) { 6300 // For an explicit module, preserve the flags from the module build 6301 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6302 // -Wblah flags. 6303 unsigned Flags = Record[Idx++]; 6304 DiagState Initial; 6305 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6306 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6307 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6308 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6309 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6310 Initial.ExtBehavior = (diag::Severity)Flags; 6311 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6312 6313 assert(F.OriginalSourceFileID.isValid()); 6314 6315 // Set up the root buffer of the module to start with the initial 6316 // diagnostic state of the module itself, to cover files that contain no 6317 // explicit transitions (for which we did not serialize anything). 6318 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6319 .StateTransitions.push_back({FirstState, 0}); 6320 } else { 6321 // For prefix ASTs, start with whatever the user configured on the 6322 // command line. 6323 Idx++; // Skip flags. 6324 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6325 SourceLocation(), false); 6326 } 6327 6328 // Read the state transitions. 6329 unsigned NumLocations = Record[Idx++]; 6330 while (NumLocations--) { 6331 assert(Idx < Record.size() && 6332 "Invalid data, missing pragma diagnostic states"); 6333 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6334 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6335 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6336 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6337 unsigned Transitions = Record[Idx++]; 6338 6339 // Note that we don't need to set up Parent/ParentOffset here, because 6340 // we won't be changing the diagnostic state within imported FileIDs 6341 // (other than perhaps appending to the main source file, which has no 6342 // parent). 6343 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6344 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6345 for (unsigned I = 0; I != Transitions; ++I) { 6346 unsigned Offset = Record[Idx++]; 6347 auto *State = 6348 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6349 F.StateTransitions.push_back({State, Offset}); 6350 } 6351 } 6352 6353 // Read the final state. 6354 assert(Idx < Record.size() && 6355 "Invalid data, missing final pragma diagnostic state"); 6356 SourceLocation CurStateLoc = 6357 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6358 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6359 6360 if (!F.isModule()) { 6361 Diag.DiagStatesByLoc.CurDiagState = CurState; 6362 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6363 6364 // Preserve the property that the imaginary root file describes the 6365 // current state. 6366 FileID NullFile; 6367 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6368 if (T.empty()) 6369 T.push_back({CurState, 0}); 6370 else 6371 T[0].State = CurState; 6372 } 6373 6374 // Don't try to read these mappings again. 6375 Record.clear(); 6376 } 6377 } 6378 6379 /// Get the correct cursor and offset for loading a type. 6380 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6381 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6382 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6383 ModuleFile *M = I->second; 6384 return RecordLocation( 6385 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6386 M->DeclsBlockStartOffset); 6387 } 6388 6389 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6390 switch (code) { 6391 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6392 case TYPE_##CODE_ID: return Type::CLASS_ID; 6393 #include "clang/Serialization/TypeBitCodes.def" 6394 default: return llvm::None; 6395 } 6396 } 6397 6398 /// Read and return the type with the given index.. 6399 /// 6400 /// The index is the type ID, shifted and minus the number of predefs. This 6401 /// routine actually reads the record corresponding to the type at the given 6402 /// location. It is a helper routine for GetType, which deals with reading type 6403 /// IDs. 6404 QualType ASTReader::readTypeRecord(unsigned Index) { 6405 assert(ContextObj && "reading type with no AST context"); 6406 ASTContext &Context = *ContextObj; 6407 RecordLocation Loc = TypeCursorForIndex(Index); 6408 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6409 6410 // Keep track of where we are in the stream, then jump back there 6411 // after reading this type. 6412 SavedStreamPosition SavedPosition(DeclsCursor); 6413 6414 ReadingKindTracker ReadingKind(Read_Type, *this); 6415 6416 // Note that we are loading a type record. 6417 Deserializing AType(this); 6418 6419 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6420 Error(std::move(Err)); 6421 return QualType(); 6422 } 6423 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6424 if (!RawCode) { 6425 Error(RawCode.takeError()); 6426 return QualType(); 6427 } 6428 6429 ASTRecordReader Record(*this, *Loc.F); 6430 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6431 if (!Code) { 6432 Error(Code.takeError()); 6433 return QualType(); 6434 } 6435 if (Code.get() == TYPE_EXT_QUAL) { 6436 QualType baseType = Record.readQualType(); 6437 Qualifiers quals = Record.readQualifiers(); 6438 return Context.getQualifiedType(baseType, quals); 6439 } 6440 6441 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6442 if (!maybeClass) { 6443 Error("Unexpected code for type"); 6444 return QualType(); 6445 } 6446 6447 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6448 return TypeReader.read(*maybeClass); 6449 } 6450 6451 namespace clang { 6452 6453 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6454 ASTRecordReader &Reader; 6455 6456 SourceLocation readSourceLocation() { 6457 return Reader.readSourceLocation(); 6458 } 6459 6460 TypeSourceInfo *GetTypeSourceInfo() { 6461 return Reader.readTypeSourceInfo(); 6462 } 6463 6464 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6465 return Reader.readNestedNameSpecifierLoc(); 6466 } 6467 6468 Attr *ReadAttr() { 6469 return Reader.readAttr(); 6470 } 6471 6472 public: 6473 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6474 6475 // We want compile-time assurance that we've enumerated all of 6476 // these, so unfortunately we have to declare them first, then 6477 // define them out-of-line. 6478 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6479 #define TYPELOC(CLASS, PARENT) \ 6480 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6481 #include "clang/AST/TypeLocNodes.def" 6482 6483 void VisitFunctionTypeLoc(FunctionTypeLoc); 6484 void VisitArrayTypeLoc(ArrayTypeLoc); 6485 }; 6486 6487 } // namespace clang 6488 6489 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6490 // nothing to do 6491 } 6492 6493 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6494 TL.setBuiltinLoc(readSourceLocation()); 6495 if (TL.needsExtraLocalData()) { 6496 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6497 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6498 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6499 TL.setModeAttr(Reader.readInt()); 6500 } 6501 } 6502 6503 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6504 TL.setNameLoc(readSourceLocation()); 6505 } 6506 6507 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6508 TL.setStarLoc(readSourceLocation()); 6509 } 6510 6511 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6512 // nothing to do 6513 } 6514 6515 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6516 // nothing to do 6517 } 6518 6519 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6520 TL.setExpansionLoc(readSourceLocation()); 6521 } 6522 6523 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6524 TL.setCaretLoc(readSourceLocation()); 6525 } 6526 6527 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6528 TL.setAmpLoc(readSourceLocation()); 6529 } 6530 6531 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6532 TL.setAmpAmpLoc(readSourceLocation()); 6533 } 6534 6535 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6536 TL.setStarLoc(readSourceLocation()); 6537 TL.setClassTInfo(GetTypeSourceInfo()); 6538 } 6539 6540 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6541 TL.setLBracketLoc(readSourceLocation()); 6542 TL.setRBracketLoc(readSourceLocation()); 6543 if (Reader.readBool()) 6544 TL.setSizeExpr(Reader.readExpr()); 6545 else 6546 TL.setSizeExpr(nullptr); 6547 } 6548 6549 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6550 VisitArrayTypeLoc(TL); 6551 } 6552 6553 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6554 VisitArrayTypeLoc(TL); 6555 } 6556 6557 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6558 VisitArrayTypeLoc(TL); 6559 } 6560 6561 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6562 DependentSizedArrayTypeLoc TL) { 6563 VisitArrayTypeLoc(TL); 6564 } 6565 6566 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6567 DependentAddressSpaceTypeLoc TL) { 6568 6569 TL.setAttrNameLoc(readSourceLocation()); 6570 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6571 TL.setAttrExprOperand(Reader.readExpr()); 6572 } 6573 6574 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6575 DependentSizedExtVectorTypeLoc TL) { 6576 TL.setNameLoc(readSourceLocation()); 6577 } 6578 6579 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6580 TL.setNameLoc(readSourceLocation()); 6581 } 6582 6583 void TypeLocReader::VisitDependentVectorTypeLoc( 6584 DependentVectorTypeLoc TL) { 6585 TL.setNameLoc(readSourceLocation()); 6586 } 6587 6588 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6589 TL.setNameLoc(readSourceLocation()); 6590 } 6591 6592 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6593 TL.setAttrNameLoc(readSourceLocation()); 6594 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6595 TL.setAttrRowOperand(Reader.readExpr()); 6596 TL.setAttrColumnOperand(Reader.readExpr()); 6597 } 6598 6599 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6600 DependentSizedMatrixTypeLoc TL) { 6601 TL.setAttrNameLoc(readSourceLocation()); 6602 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6603 TL.setAttrRowOperand(Reader.readExpr()); 6604 TL.setAttrColumnOperand(Reader.readExpr()); 6605 } 6606 6607 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6608 TL.setLocalRangeBegin(readSourceLocation()); 6609 TL.setLParenLoc(readSourceLocation()); 6610 TL.setRParenLoc(readSourceLocation()); 6611 TL.setExceptionSpecRange(Reader.readSourceRange()); 6612 TL.setLocalRangeEnd(readSourceLocation()); 6613 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6614 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6615 } 6616 } 6617 6618 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6619 VisitFunctionTypeLoc(TL); 6620 } 6621 6622 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6623 VisitFunctionTypeLoc(TL); 6624 } 6625 6626 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6627 TL.setNameLoc(readSourceLocation()); 6628 } 6629 6630 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) { 6631 TL.setNameLoc(readSourceLocation()); 6632 } 6633 6634 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6635 TL.setNameLoc(readSourceLocation()); 6636 } 6637 6638 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6639 TL.setTypeofLoc(readSourceLocation()); 6640 TL.setLParenLoc(readSourceLocation()); 6641 TL.setRParenLoc(readSourceLocation()); 6642 } 6643 6644 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6645 TL.setTypeofLoc(readSourceLocation()); 6646 TL.setLParenLoc(readSourceLocation()); 6647 TL.setRParenLoc(readSourceLocation()); 6648 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6649 } 6650 6651 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6652 TL.setDecltypeLoc(readSourceLocation()); 6653 TL.setRParenLoc(readSourceLocation()); 6654 } 6655 6656 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6657 TL.setKWLoc(readSourceLocation()); 6658 TL.setLParenLoc(readSourceLocation()); 6659 TL.setRParenLoc(readSourceLocation()); 6660 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6661 } 6662 6663 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6664 TL.setNameLoc(readSourceLocation()); 6665 if (Reader.readBool()) { 6666 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6667 TL.setTemplateKWLoc(readSourceLocation()); 6668 TL.setConceptNameLoc(readSourceLocation()); 6669 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6670 TL.setLAngleLoc(readSourceLocation()); 6671 TL.setRAngleLoc(readSourceLocation()); 6672 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6673 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6674 TL.getTypePtr()->getArg(i).getKind())); 6675 } 6676 if (Reader.readBool()) 6677 TL.setRParenLoc(readSourceLocation()); 6678 } 6679 6680 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6681 DeducedTemplateSpecializationTypeLoc TL) { 6682 TL.setTemplateNameLoc(readSourceLocation()); 6683 } 6684 6685 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6686 TL.setNameLoc(readSourceLocation()); 6687 } 6688 6689 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6690 TL.setNameLoc(readSourceLocation()); 6691 } 6692 6693 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6694 TL.setAttr(ReadAttr()); 6695 } 6696 6697 void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) { 6698 // Nothing to do. 6699 } 6700 6701 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6702 TL.setNameLoc(readSourceLocation()); 6703 } 6704 6705 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6706 SubstTemplateTypeParmTypeLoc TL) { 6707 TL.setNameLoc(readSourceLocation()); 6708 } 6709 6710 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6711 SubstTemplateTypeParmPackTypeLoc TL) { 6712 TL.setNameLoc(readSourceLocation()); 6713 } 6714 6715 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6716 TemplateSpecializationTypeLoc TL) { 6717 TL.setTemplateKeywordLoc(readSourceLocation()); 6718 TL.setTemplateNameLoc(readSourceLocation()); 6719 TL.setLAngleLoc(readSourceLocation()); 6720 TL.setRAngleLoc(readSourceLocation()); 6721 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6722 TL.setArgLocInfo( 6723 i, 6724 Reader.readTemplateArgumentLocInfo( 6725 TL.getTypePtr()->getArg(i).getKind())); 6726 } 6727 6728 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6729 TL.setLParenLoc(readSourceLocation()); 6730 TL.setRParenLoc(readSourceLocation()); 6731 } 6732 6733 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6734 TL.setElaboratedKeywordLoc(readSourceLocation()); 6735 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6736 } 6737 6738 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6739 TL.setNameLoc(readSourceLocation()); 6740 } 6741 6742 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6743 TL.setElaboratedKeywordLoc(readSourceLocation()); 6744 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6745 TL.setNameLoc(readSourceLocation()); 6746 } 6747 6748 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6749 DependentTemplateSpecializationTypeLoc TL) { 6750 TL.setElaboratedKeywordLoc(readSourceLocation()); 6751 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6752 TL.setTemplateKeywordLoc(readSourceLocation()); 6753 TL.setTemplateNameLoc(readSourceLocation()); 6754 TL.setLAngleLoc(readSourceLocation()); 6755 TL.setRAngleLoc(readSourceLocation()); 6756 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6757 TL.setArgLocInfo( 6758 I, 6759 Reader.readTemplateArgumentLocInfo( 6760 TL.getTypePtr()->getArg(I).getKind())); 6761 } 6762 6763 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6764 TL.setEllipsisLoc(readSourceLocation()); 6765 } 6766 6767 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6768 TL.setNameLoc(readSourceLocation()); 6769 } 6770 6771 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6772 if (TL.getNumProtocols()) { 6773 TL.setProtocolLAngleLoc(readSourceLocation()); 6774 TL.setProtocolRAngleLoc(readSourceLocation()); 6775 } 6776 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6777 TL.setProtocolLoc(i, readSourceLocation()); 6778 } 6779 6780 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6781 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6782 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6783 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6784 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6785 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6786 TL.setProtocolLAngleLoc(readSourceLocation()); 6787 TL.setProtocolRAngleLoc(readSourceLocation()); 6788 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6789 TL.setProtocolLoc(i, readSourceLocation()); 6790 } 6791 6792 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6793 TL.setStarLoc(readSourceLocation()); 6794 } 6795 6796 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6797 TL.setKWLoc(readSourceLocation()); 6798 TL.setLParenLoc(readSourceLocation()); 6799 TL.setRParenLoc(readSourceLocation()); 6800 } 6801 6802 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6803 TL.setKWLoc(readSourceLocation()); 6804 } 6805 6806 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) { 6807 TL.setNameLoc(readSourceLocation()); 6808 } 6809 void TypeLocReader::VisitDependentBitIntTypeLoc( 6810 clang::DependentBitIntTypeLoc TL) { 6811 TL.setNameLoc(readSourceLocation()); 6812 } 6813 6814 6815 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6816 TypeLocReader TLR(*this); 6817 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6818 TLR.Visit(TL); 6819 } 6820 6821 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6822 QualType InfoTy = readType(); 6823 if (InfoTy.isNull()) 6824 return nullptr; 6825 6826 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6827 readTypeLoc(TInfo->getTypeLoc()); 6828 return TInfo; 6829 } 6830 6831 QualType ASTReader::GetType(TypeID ID) { 6832 assert(ContextObj && "reading type with no AST context"); 6833 ASTContext &Context = *ContextObj; 6834 6835 unsigned FastQuals = ID & Qualifiers::FastMask; 6836 unsigned Index = ID >> Qualifiers::FastWidth; 6837 6838 if (Index < NUM_PREDEF_TYPE_IDS) { 6839 QualType T; 6840 switch ((PredefinedTypeIDs)Index) { 6841 case PREDEF_TYPE_NULL_ID: 6842 return QualType(); 6843 case PREDEF_TYPE_VOID_ID: 6844 T = Context.VoidTy; 6845 break; 6846 case PREDEF_TYPE_BOOL_ID: 6847 T = Context.BoolTy; 6848 break; 6849 case PREDEF_TYPE_CHAR_U_ID: 6850 case PREDEF_TYPE_CHAR_S_ID: 6851 // FIXME: Check that the signedness of CharTy is correct! 6852 T = Context.CharTy; 6853 break; 6854 case PREDEF_TYPE_UCHAR_ID: 6855 T = Context.UnsignedCharTy; 6856 break; 6857 case PREDEF_TYPE_USHORT_ID: 6858 T = Context.UnsignedShortTy; 6859 break; 6860 case PREDEF_TYPE_UINT_ID: 6861 T = Context.UnsignedIntTy; 6862 break; 6863 case PREDEF_TYPE_ULONG_ID: 6864 T = Context.UnsignedLongTy; 6865 break; 6866 case PREDEF_TYPE_ULONGLONG_ID: 6867 T = Context.UnsignedLongLongTy; 6868 break; 6869 case PREDEF_TYPE_UINT128_ID: 6870 T = Context.UnsignedInt128Ty; 6871 break; 6872 case PREDEF_TYPE_SCHAR_ID: 6873 T = Context.SignedCharTy; 6874 break; 6875 case PREDEF_TYPE_WCHAR_ID: 6876 T = Context.WCharTy; 6877 break; 6878 case PREDEF_TYPE_SHORT_ID: 6879 T = Context.ShortTy; 6880 break; 6881 case PREDEF_TYPE_INT_ID: 6882 T = Context.IntTy; 6883 break; 6884 case PREDEF_TYPE_LONG_ID: 6885 T = Context.LongTy; 6886 break; 6887 case PREDEF_TYPE_LONGLONG_ID: 6888 T = Context.LongLongTy; 6889 break; 6890 case PREDEF_TYPE_INT128_ID: 6891 T = Context.Int128Ty; 6892 break; 6893 case PREDEF_TYPE_BFLOAT16_ID: 6894 T = Context.BFloat16Ty; 6895 break; 6896 case PREDEF_TYPE_HALF_ID: 6897 T = Context.HalfTy; 6898 break; 6899 case PREDEF_TYPE_FLOAT_ID: 6900 T = Context.FloatTy; 6901 break; 6902 case PREDEF_TYPE_DOUBLE_ID: 6903 T = Context.DoubleTy; 6904 break; 6905 case PREDEF_TYPE_LONGDOUBLE_ID: 6906 T = Context.LongDoubleTy; 6907 break; 6908 case PREDEF_TYPE_SHORT_ACCUM_ID: 6909 T = Context.ShortAccumTy; 6910 break; 6911 case PREDEF_TYPE_ACCUM_ID: 6912 T = Context.AccumTy; 6913 break; 6914 case PREDEF_TYPE_LONG_ACCUM_ID: 6915 T = Context.LongAccumTy; 6916 break; 6917 case PREDEF_TYPE_USHORT_ACCUM_ID: 6918 T = Context.UnsignedShortAccumTy; 6919 break; 6920 case PREDEF_TYPE_UACCUM_ID: 6921 T = Context.UnsignedAccumTy; 6922 break; 6923 case PREDEF_TYPE_ULONG_ACCUM_ID: 6924 T = Context.UnsignedLongAccumTy; 6925 break; 6926 case PREDEF_TYPE_SHORT_FRACT_ID: 6927 T = Context.ShortFractTy; 6928 break; 6929 case PREDEF_TYPE_FRACT_ID: 6930 T = Context.FractTy; 6931 break; 6932 case PREDEF_TYPE_LONG_FRACT_ID: 6933 T = Context.LongFractTy; 6934 break; 6935 case PREDEF_TYPE_USHORT_FRACT_ID: 6936 T = Context.UnsignedShortFractTy; 6937 break; 6938 case PREDEF_TYPE_UFRACT_ID: 6939 T = Context.UnsignedFractTy; 6940 break; 6941 case PREDEF_TYPE_ULONG_FRACT_ID: 6942 T = Context.UnsignedLongFractTy; 6943 break; 6944 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6945 T = Context.SatShortAccumTy; 6946 break; 6947 case PREDEF_TYPE_SAT_ACCUM_ID: 6948 T = Context.SatAccumTy; 6949 break; 6950 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6951 T = Context.SatLongAccumTy; 6952 break; 6953 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6954 T = Context.SatUnsignedShortAccumTy; 6955 break; 6956 case PREDEF_TYPE_SAT_UACCUM_ID: 6957 T = Context.SatUnsignedAccumTy; 6958 break; 6959 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6960 T = Context.SatUnsignedLongAccumTy; 6961 break; 6962 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6963 T = Context.SatShortFractTy; 6964 break; 6965 case PREDEF_TYPE_SAT_FRACT_ID: 6966 T = Context.SatFractTy; 6967 break; 6968 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6969 T = Context.SatLongFractTy; 6970 break; 6971 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6972 T = Context.SatUnsignedShortFractTy; 6973 break; 6974 case PREDEF_TYPE_SAT_UFRACT_ID: 6975 T = Context.SatUnsignedFractTy; 6976 break; 6977 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6978 T = Context.SatUnsignedLongFractTy; 6979 break; 6980 case PREDEF_TYPE_FLOAT16_ID: 6981 T = Context.Float16Ty; 6982 break; 6983 case PREDEF_TYPE_FLOAT128_ID: 6984 T = Context.Float128Ty; 6985 break; 6986 case PREDEF_TYPE_IBM128_ID: 6987 T = Context.Ibm128Ty; 6988 break; 6989 case PREDEF_TYPE_OVERLOAD_ID: 6990 T = Context.OverloadTy; 6991 break; 6992 case PREDEF_TYPE_BOUND_MEMBER: 6993 T = Context.BoundMemberTy; 6994 break; 6995 case PREDEF_TYPE_PSEUDO_OBJECT: 6996 T = Context.PseudoObjectTy; 6997 break; 6998 case PREDEF_TYPE_DEPENDENT_ID: 6999 T = Context.DependentTy; 7000 break; 7001 case PREDEF_TYPE_UNKNOWN_ANY: 7002 T = Context.UnknownAnyTy; 7003 break; 7004 case PREDEF_TYPE_NULLPTR_ID: 7005 T = Context.NullPtrTy; 7006 break; 7007 case PREDEF_TYPE_CHAR8_ID: 7008 T = Context.Char8Ty; 7009 break; 7010 case PREDEF_TYPE_CHAR16_ID: 7011 T = Context.Char16Ty; 7012 break; 7013 case PREDEF_TYPE_CHAR32_ID: 7014 T = Context.Char32Ty; 7015 break; 7016 case PREDEF_TYPE_OBJC_ID: 7017 T = Context.ObjCBuiltinIdTy; 7018 break; 7019 case PREDEF_TYPE_OBJC_CLASS: 7020 T = Context.ObjCBuiltinClassTy; 7021 break; 7022 case PREDEF_TYPE_OBJC_SEL: 7023 T = Context.ObjCBuiltinSelTy; 7024 break; 7025 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 7026 case PREDEF_TYPE_##Id##_ID: \ 7027 T = Context.SingletonId; \ 7028 break; 7029 #include "clang/Basic/OpenCLImageTypes.def" 7030 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 7031 case PREDEF_TYPE_##Id##_ID: \ 7032 T = Context.Id##Ty; \ 7033 break; 7034 #include "clang/Basic/OpenCLExtensionTypes.def" 7035 case PREDEF_TYPE_SAMPLER_ID: 7036 T = Context.OCLSamplerTy; 7037 break; 7038 case PREDEF_TYPE_EVENT_ID: 7039 T = Context.OCLEventTy; 7040 break; 7041 case PREDEF_TYPE_CLK_EVENT_ID: 7042 T = Context.OCLClkEventTy; 7043 break; 7044 case PREDEF_TYPE_QUEUE_ID: 7045 T = Context.OCLQueueTy; 7046 break; 7047 case PREDEF_TYPE_RESERVE_ID_ID: 7048 T = Context.OCLReserveIDTy; 7049 break; 7050 case PREDEF_TYPE_AUTO_DEDUCT: 7051 T = Context.getAutoDeductType(); 7052 break; 7053 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7054 T = Context.getAutoRRefDeductType(); 7055 break; 7056 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7057 T = Context.ARCUnbridgedCastTy; 7058 break; 7059 case PREDEF_TYPE_BUILTIN_FN: 7060 T = Context.BuiltinFnTy; 7061 break; 7062 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7063 T = Context.IncompleteMatrixIdxTy; 7064 break; 7065 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7066 T = Context.OMPArraySectionTy; 7067 break; 7068 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7069 T = Context.OMPArraySectionTy; 7070 break; 7071 case PREDEF_TYPE_OMP_ITERATOR: 7072 T = Context.OMPIteratorTy; 7073 break; 7074 #define SVE_TYPE(Name, Id, SingletonId) \ 7075 case PREDEF_TYPE_##Id##_ID: \ 7076 T = Context.SingletonId; \ 7077 break; 7078 #include "clang/Basic/AArch64SVEACLETypes.def" 7079 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 7080 case PREDEF_TYPE_##Id##_ID: \ 7081 T = Context.Id##Ty; \ 7082 break; 7083 #include "clang/Basic/PPCTypes.def" 7084 #define RVV_TYPE(Name, Id, SingletonId) \ 7085 case PREDEF_TYPE_##Id##_ID: \ 7086 T = Context.SingletonId; \ 7087 break; 7088 #include "clang/Basic/RISCVVTypes.def" 7089 } 7090 7091 assert(!T.isNull() && "Unknown predefined type"); 7092 return T.withFastQualifiers(FastQuals); 7093 } 7094 7095 Index -= NUM_PREDEF_TYPE_IDS; 7096 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7097 if (TypesLoaded[Index].isNull()) { 7098 TypesLoaded[Index] = readTypeRecord(Index); 7099 if (TypesLoaded[Index].isNull()) 7100 return QualType(); 7101 7102 TypesLoaded[Index]->setFromAST(); 7103 if (DeserializationListener) 7104 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7105 TypesLoaded[Index]); 7106 } 7107 7108 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7109 } 7110 7111 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7112 return GetType(getGlobalTypeID(F, LocalID)); 7113 } 7114 7115 serialization::TypeID 7116 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7117 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7118 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7119 7120 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7121 return LocalID; 7122 7123 if (!F.ModuleOffsetMap.empty()) 7124 ReadModuleOffsetMap(F); 7125 7126 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7127 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7128 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7129 7130 unsigned GlobalIndex = LocalIndex + I->second; 7131 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7132 } 7133 7134 TemplateArgumentLocInfo 7135 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7136 switch (Kind) { 7137 case TemplateArgument::Expression: 7138 return readExpr(); 7139 case TemplateArgument::Type: 7140 return readTypeSourceInfo(); 7141 case TemplateArgument::Template: { 7142 NestedNameSpecifierLoc QualifierLoc = 7143 readNestedNameSpecifierLoc(); 7144 SourceLocation TemplateNameLoc = readSourceLocation(); 7145 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7146 TemplateNameLoc, SourceLocation()); 7147 } 7148 case TemplateArgument::TemplateExpansion: { 7149 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7150 SourceLocation TemplateNameLoc = readSourceLocation(); 7151 SourceLocation EllipsisLoc = readSourceLocation(); 7152 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7153 TemplateNameLoc, EllipsisLoc); 7154 } 7155 case TemplateArgument::Null: 7156 case TemplateArgument::Integral: 7157 case TemplateArgument::Declaration: 7158 case TemplateArgument::NullPtr: 7159 case TemplateArgument::Pack: 7160 // FIXME: Is this right? 7161 return TemplateArgumentLocInfo(); 7162 } 7163 llvm_unreachable("unexpected template argument loc"); 7164 } 7165 7166 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7167 TemplateArgument Arg = readTemplateArgument(); 7168 7169 if (Arg.getKind() == TemplateArgument::Expression) { 7170 if (readBool()) // bool InfoHasSameExpr. 7171 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7172 } 7173 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7174 } 7175 7176 const ASTTemplateArgumentListInfo * 7177 ASTRecordReader::readASTTemplateArgumentListInfo() { 7178 SourceLocation LAngleLoc = readSourceLocation(); 7179 SourceLocation RAngleLoc = readSourceLocation(); 7180 unsigned NumArgsAsWritten = readInt(); 7181 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7182 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7183 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7184 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7185 } 7186 7187 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7188 return GetDecl(ID); 7189 } 7190 7191 void ASTReader::CompleteRedeclChain(const Decl *D) { 7192 if (NumCurrentElementsDeserializing) { 7193 // We arrange to not care about the complete redeclaration chain while we're 7194 // deserializing. Just remember that the AST has marked this one as complete 7195 // but that it's not actually complete yet, so we know we still need to 7196 // complete it later. 7197 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7198 return; 7199 } 7200 7201 if (!D->getDeclContext()) { 7202 assert(isa<TranslationUnitDecl>(D) && "Not a TU?"); 7203 return; 7204 } 7205 7206 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7207 7208 // If this is a named declaration, complete it by looking it up 7209 // within its context. 7210 // 7211 // FIXME: Merging a function definition should merge 7212 // all mergeable entities within it. 7213 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7214 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7215 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7216 if (!getContext().getLangOpts().CPlusPlus && 7217 isa<TranslationUnitDecl>(DC)) { 7218 // Outside of C++, we don't have a lookup table for the TU, so update 7219 // the identifier instead. (For C++ modules, we don't store decls 7220 // in the serialized identifier table, so we do the lookup in the TU.) 7221 auto *II = Name.getAsIdentifierInfo(); 7222 assert(II && "non-identifier name in C?"); 7223 if (II->isOutOfDate()) 7224 updateOutOfDateIdentifier(*II); 7225 } else 7226 DC->lookup(Name); 7227 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7228 // Find all declarations of this kind from the relevant context. 7229 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7230 auto *DC = cast<DeclContext>(DCDecl); 7231 SmallVector<Decl*, 8> Decls; 7232 FindExternalLexicalDecls( 7233 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7234 } 7235 } 7236 } 7237 7238 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7239 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7240 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7241 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7242 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7243 if (auto *Template = FD->getPrimaryTemplate()) 7244 Template->LoadLazySpecializations(); 7245 } 7246 } 7247 7248 CXXCtorInitializer ** 7249 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7250 RecordLocation Loc = getLocalBitOffset(Offset); 7251 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7252 SavedStreamPosition SavedPosition(Cursor); 7253 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7254 Error(std::move(Err)); 7255 return nullptr; 7256 } 7257 ReadingKindTracker ReadingKind(Read_Decl, *this); 7258 7259 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7260 if (!MaybeCode) { 7261 Error(MaybeCode.takeError()); 7262 return nullptr; 7263 } 7264 unsigned Code = MaybeCode.get(); 7265 7266 ASTRecordReader Record(*this, *Loc.F); 7267 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7268 if (!MaybeRecCode) { 7269 Error(MaybeRecCode.takeError()); 7270 return nullptr; 7271 } 7272 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7273 Error("malformed AST file: missing C++ ctor initializers"); 7274 return nullptr; 7275 } 7276 7277 return Record.readCXXCtorInitializers(); 7278 } 7279 7280 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7281 assert(ContextObj && "reading base specifiers with no AST context"); 7282 ASTContext &Context = *ContextObj; 7283 7284 RecordLocation Loc = getLocalBitOffset(Offset); 7285 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7286 SavedStreamPosition SavedPosition(Cursor); 7287 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7288 Error(std::move(Err)); 7289 return nullptr; 7290 } 7291 ReadingKindTracker ReadingKind(Read_Decl, *this); 7292 7293 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7294 if (!MaybeCode) { 7295 Error(MaybeCode.takeError()); 7296 return nullptr; 7297 } 7298 unsigned Code = MaybeCode.get(); 7299 7300 ASTRecordReader Record(*this, *Loc.F); 7301 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7302 if (!MaybeRecCode) { 7303 Error(MaybeCode.takeError()); 7304 return nullptr; 7305 } 7306 unsigned RecCode = MaybeRecCode.get(); 7307 7308 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7309 Error("malformed AST file: missing C++ base specifiers"); 7310 return nullptr; 7311 } 7312 7313 unsigned NumBases = Record.readInt(); 7314 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7315 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7316 for (unsigned I = 0; I != NumBases; ++I) 7317 Bases[I] = Record.readCXXBaseSpecifier(); 7318 return Bases; 7319 } 7320 7321 serialization::DeclID 7322 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7323 if (LocalID < NUM_PREDEF_DECL_IDS) 7324 return LocalID; 7325 7326 if (!F.ModuleOffsetMap.empty()) 7327 ReadModuleOffsetMap(F); 7328 7329 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7330 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7331 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7332 7333 return LocalID + I->second; 7334 } 7335 7336 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7337 ModuleFile &M) const { 7338 // Predefined decls aren't from any module. 7339 if (ID < NUM_PREDEF_DECL_IDS) 7340 return false; 7341 7342 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7343 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7344 } 7345 7346 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7347 if (!D->isFromASTFile()) 7348 return nullptr; 7349 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7350 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7351 return I->second; 7352 } 7353 7354 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7355 if (ID < NUM_PREDEF_DECL_IDS) 7356 return SourceLocation(); 7357 7358 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7359 7360 if (Index > DeclsLoaded.size()) { 7361 Error("declaration ID out-of-range for AST file"); 7362 return SourceLocation(); 7363 } 7364 7365 if (Decl *D = DeclsLoaded[Index]) 7366 return D->getLocation(); 7367 7368 SourceLocation Loc; 7369 DeclCursorForID(ID, Loc); 7370 return Loc; 7371 } 7372 7373 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7374 switch (ID) { 7375 case PREDEF_DECL_NULL_ID: 7376 return nullptr; 7377 7378 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7379 return Context.getTranslationUnitDecl(); 7380 7381 case PREDEF_DECL_OBJC_ID_ID: 7382 return Context.getObjCIdDecl(); 7383 7384 case PREDEF_DECL_OBJC_SEL_ID: 7385 return Context.getObjCSelDecl(); 7386 7387 case PREDEF_DECL_OBJC_CLASS_ID: 7388 return Context.getObjCClassDecl(); 7389 7390 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7391 return Context.getObjCProtocolDecl(); 7392 7393 case PREDEF_DECL_INT_128_ID: 7394 return Context.getInt128Decl(); 7395 7396 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7397 return Context.getUInt128Decl(); 7398 7399 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7400 return Context.getObjCInstanceTypeDecl(); 7401 7402 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7403 return Context.getBuiltinVaListDecl(); 7404 7405 case PREDEF_DECL_VA_LIST_TAG: 7406 return Context.getVaListTagDecl(); 7407 7408 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7409 return Context.getBuiltinMSVaListDecl(); 7410 7411 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7412 return Context.getMSGuidTagDecl(); 7413 7414 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7415 return Context.getExternCContextDecl(); 7416 7417 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7418 return Context.getMakeIntegerSeqDecl(); 7419 7420 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7421 return Context.getCFConstantStringDecl(); 7422 7423 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7424 return Context.getCFConstantStringTagDecl(); 7425 7426 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7427 return Context.getTypePackElementDecl(); 7428 } 7429 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7430 } 7431 7432 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7433 assert(ContextObj && "reading decl with no AST context"); 7434 if (ID < NUM_PREDEF_DECL_IDS) { 7435 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7436 if (D) { 7437 // Track that we have merged the declaration with ID \p ID into the 7438 // pre-existing predefined declaration \p D. 7439 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7440 if (Merged.empty()) 7441 Merged.push_back(ID); 7442 } 7443 return D; 7444 } 7445 7446 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7447 7448 if (Index >= DeclsLoaded.size()) { 7449 assert(0 && "declaration ID out-of-range for AST file"); 7450 Error("declaration ID out-of-range for AST file"); 7451 return nullptr; 7452 } 7453 7454 return DeclsLoaded[Index]; 7455 } 7456 7457 Decl *ASTReader::GetDecl(DeclID ID) { 7458 if (ID < NUM_PREDEF_DECL_IDS) 7459 return GetExistingDecl(ID); 7460 7461 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7462 7463 if (Index >= DeclsLoaded.size()) { 7464 assert(0 && "declaration ID out-of-range for AST file"); 7465 Error("declaration ID out-of-range for AST file"); 7466 return nullptr; 7467 } 7468 7469 if (!DeclsLoaded[Index]) { 7470 ReadDeclRecord(ID); 7471 if (DeserializationListener) 7472 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7473 } 7474 7475 return DeclsLoaded[Index]; 7476 } 7477 7478 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7479 DeclID GlobalID) { 7480 if (GlobalID < NUM_PREDEF_DECL_IDS) 7481 return GlobalID; 7482 7483 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7484 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7485 ModuleFile *Owner = I->second; 7486 7487 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7488 = M.GlobalToLocalDeclIDs.find(Owner); 7489 if (Pos == M.GlobalToLocalDeclIDs.end()) 7490 return 0; 7491 7492 return GlobalID - Owner->BaseDeclID + Pos->second; 7493 } 7494 7495 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7496 const RecordData &Record, 7497 unsigned &Idx) { 7498 if (Idx >= Record.size()) { 7499 Error("Corrupted AST file"); 7500 return 0; 7501 } 7502 7503 return getGlobalDeclID(F, Record[Idx++]); 7504 } 7505 7506 /// Resolve the offset of a statement into a statement. 7507 /// 7508 /// This operation will read a new statement from the external 7509 /// source each time it is called, and is meant to be used via a 7510 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7511 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7512 // Switch case IDs are per Decl. 7513 ClearSwitchCaseIDs(); 7514 7515 // Offset here is a global offset across the entire chain. 7516 RecordLocation Loc = getLocalBitOffset(Offset); 7517 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7518 Error(std::move(Err)); 7519 return nullptr; 7520 } 7521 assert(NumCurrentElementsDeserializing == 0 && 7522 "should not be called while already deserializing"); 7523 Deserializing D(this); 7524 return ReadStmtFromStream(*Loc.F); 7525 } 7526 7527 void ASTReader::FindExternalLexicalDecls( 7528 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7529 SmallVectorImpl<Decl *> &Decls) { 7530 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7531 7532 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7533 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7534 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7535 auto K = (Decl::Kind)+LexicalDecls[I]; 7536 if (!IsKindWeWant(K)) 7537 continue; 7538 7539 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7540 7541 // Don't add predefined declarations to the lexical context more 7542 // than once. 7543 if (ID < NUM_PREDEF_DECL_IDS) { 7544 if (PredefsVisited[ID]) 7545 continue; 7546 7547 PredefsVisited[ID] = true; 7548 } 7549 7550 if (Decl *D = GetLocalDecl(*M, ID)) { 7551 assert(D->getKind() == K && "wrong kind for lexical decl"); 7552 if (!DC->isDeclInLexicalTraversal(D)) 7553 Decls.push_back(D); 7554 } 7555 } 7556 }; 7557 7558 if (isa<TranslationUnitDecl>(DC)) { 7559 for (auto Lexical : TULexicalDecls) 7560 Visit(Lexical.first, Lexical.second); 7561 } else { 7562 auto I = LexicalDecls.find(DC); 7563 if (I != LexicalDecls.end()) 7564 Visit(I->second.first, I->second.second); 7565 } 7566 7567 ++NumLexicalDeclContextsRead; 7568 } 7569 7570 namespace { 7571 7572 class DeclIDComp { 7573 ASTReader &Reader; 7574 ModuleFile &Mod; 7575 7576 public: 7577 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7578 7579 bool operator()(LocalDeclID L, LocalDeclID R) const { 7580 SourceLocation LHS = getLocation(L); 7581 SourceLocation RHS = getLocation(R); 7582 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7583 } 7584 7585 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7586 SourceLocation RHS = getLocation(R); 7587 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7588 } 7589 7590 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7591 SourceLocation LHS = getLocation(L); 7592 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7593 } 7594 7595 SourceLocation getLocation(LocalDeclID ID) const { 7596 return Reader.getSourceManager().getFileLoc( 7597 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7598 } 7599 }; 7600 7601 } // namespace 7602 7603 void ASTReader::FindFileRegionDecls(FileID File, 7604 unsigned Offset, unsigned Length, 7605 SmallVectorImpl<Decl *> &Decls) { 7606 SourceManager &SM = getSourceManager(); 7607 7608 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7609 if (I == FileDeclIDs.end()) 7610 return; 7611 7612 FileDeclsInfo &DInfo = I->second; 7613 if (DInfo.Decls.empty()) 7614 return; 7615 7616 SourceLocation 7617 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7618 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7619 7620 DeclIDComp DIDComp(*this, *DInfo.Mod); 7621 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7622 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7623 if (BeginIt != DInfo.Decls.begin()) 7624 --BeginIt; 7625 7626 // If we are pointing at a top-level decl inside an objc container, we need 7627 // to backtrack until we find it otherwise we will fail to report that the 7628 // region overlaps with an objc container. 7629 while (BeginIt != DInfo.Decls.begin() && 7630 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7631 ->isTopLevelDeclInObjCContainer()) 7632 --BeginIt; 7633 7634 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7635 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7636 if (EndIt != DInfo.Decls.end()) 7637 ++EndIt; 7638 7639 for (ArrayRef<serialization::LocalDeclID>::iterator 7640 DIt = BeginIt; DIt != EndIt; ++DIt) 7641 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7642 } 7643 7644 bool 7645 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7646 DeclarationName Name) { 7647 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7648 "DeclContext has no visible decls in storage"); 7649 if (!Name) 7650 return false; 7651 7652 auto It = Lookups.find(DC); 7653 if (It == Lookups.end()) 7654 return false; 7655 7656 Deserializing LookupResults(this); 7657 7658 // Load the list of declarations. 7659 SmallVector<NamedDecl *, 64> Decls; 7660 llvm::SmallPtrSet<NamedDecl *, 8> Found; 7661 for (DeclID ID : It->second.Table.find(Name)) { 7662 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7663 if (ND->getDeclName() == Name && Found.insert(ND).second) 7664 Decls.push_back(ND); 7665 } 7666 7667 ++NumVisibleDeclContextsRead; 7668 SetExternalVisibleDeclsForName(DC, Name, Decls); 7669 return !Decls.empty(); 7670 } 7671 7672 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7673 if (!DC->hasExternalVisibleStorage()) 7674 return; 7675 7676 auto It = Lookups.find(DC); 7677 assert(It != Lookups.end() && 7678 "have external visible storage but no lookup tables"); 7679 7680 DeclsMap Decls; 7681 7682 for (DeclID ID : It->second.Table.findAll()) { 7683 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7684 Decls[ND->getDeclName()].push_back(ND); 7685 } 7686 7687 ++NumVisibleDeclContextsRead; 7688 7689 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7690 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7691 } 7692 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7693 } 7694 7695 const serialization::reader::DeclContextLookupTable * 7696 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7697 auto I = Lookups.find(Primary); 7698 return I == Lookups.end() ? nullptr : &I->second; 7699 } 7700 7701 /// Under non-PCH compilation the consumer receives the objc methods 7702 /// before receiving the implementation, and codegen depends on this. 7703 /// We simulate this by deserializing and passing to consumer the methods of the 7704 /// implementation before passing the deserialized implementation decl. 7705 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7706 ASTConsumer *Consumer) { 7707 assert(ImplD && Consumer); 7708 7709 for (auto *I : ImplD->methods()) 7710 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7711 7712 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7713 } 7714 7715 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7716 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7717 PassObjCImplDeclToConsumer(ImplD, Consumer); 7718 else 7719 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7720 } 7721 7722 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7723 this->Consumer = Consumer; 7724 7725 if (Consumer) 7726 PassInterestingDeclsToConsumer(); 7727 7728 if (DeserializationListener) 7729 DeserializationListener->ReaderInitialized(this); 7730 } 7731 7732 void ASTReader::PrintStats() { 7733 std::fprintf(stderr, "*** AST File Statistics:\n"); 7734 7735 unsigned NumTypesLoaded = 7736 TypesLoaded.size() - llvm::count(TypesLoaded, QualType()); 7737 unsigned NumDeclsLoaded = 7738 DeclsLoaded.size() - llvm::count(DeclsLoaded, (Decl *)nullptr); 7739 unsigned NumIdentifiersLoaded = 7740 IdentifiersLoaded.size() - 7741 llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr); 7742 unsigned NumMacrosLoaded = 7743 MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr); 7744 unsigned NumSelectorsLoaded = 7745 SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector()); 7746 7747 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7748 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7749 NumSLocEntriesRead, TotalNumSLocEntries, 7750 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7751 if (!TypesLoaded.empty()) 7752 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7753 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7754 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7755 if (!DeclsLoaded.empty()) 7756 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7757 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7758 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7759 if (!IdentifiersLoaded.empty()) 7760 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7761 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7762 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7763 if (!MacrosLoaded.empty()) 7764 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7765 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7766 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7767 if (!SelectorsLoaded.empty()) 7768 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7769 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7770 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7771 if (TotalNumStatements) 7772 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7773 NumStatementsRead, TotalNumStatements, 7774 ((float)NumStatementsRead/TotalNumStatements * 100)); 7775 if (TotalNumMacros) 7776 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7777 NumMacrosRead, TotalNumMacros, 7778 ((float)NumMacrosRead/TotalNumMacros * 100)); 7779 if (TotalLexicalDeclContexts) 7780 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7781 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7782 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7783 * 100)); 7784 if (TotalVisibleDeclContexts) 7785 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7786 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7787 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7788 * 100)); 7789 if (TotalNumMethodPoolEntries) 7790 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7791 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7792 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7793 * 100)); 7794 if (NumMethodPoolLookups) 7795 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7796 NumMethodPoolHits, NumMethodPoolLookups, 7797 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7798 if (NumMethodPoolTableLookups) 7799 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7800 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7801 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7802 * 100.0)); 7803 if (NumIdentifierLookupHits) 7804 std::fprintf(stderr, 7805 " %u / %u identifier table lookups succeeded (%f%%)\n", 7806 NumIdentifierLookupHits, NumIdentifierLookups, 7807 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7808 7809 if (GlobalIndex) { 7810 std::fprintf(stderr, "\n"); 7811 GlobalIndex->printStats(); 7812 } 7813 7814 std::fprintf(stderr, "\n"); 7815 dump(); 7816 std::fprintf(stderr, "\n"); 7817 } 7818 7819 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7820 LLVM_DUMP_METHOD static void 7821 dumpModuleIDMap(StringRef Name, 7822 const ContinuousRangeMap<Key, ModuleFile *, 7823 InitialCapacity> &Map) { 7824 if (Map.begin() == Map.end()) 7825 return; 7826 7827 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7828 7829 llvm::errs() << Name << ":\n"; 7830 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7831 I != IEnd; ++I) { 7832 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7833 << "\n"; 7834 } 7835 } 7836 7837 LLVM_DUMP_METHOD void ASTReader::dump() { 7838 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7839 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7840 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7841 dumpModuleIDMap("Global type map", GlobalTypeMap); 7842 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7843 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7844 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7845 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7846 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7847 dumpModuleIDMap("Global preprocessed entity map", 7848 GlobalPreprocessedEntityMap); 7849 7850 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7851 for (ModuleFile &M : ModuleMgr) 7852 M.dump(); 7853 } 7854 7855 /// Return the amount of memory used by memory buffers, breaking down 7856 /// by heap-backed versus mmap'ed memory. 7857 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7858 for (ModuleFile &I : ModuleMgr) { 7859 if (llvm::MemoryBuffer *buf = I.Buffer) { 7860 size_t bytes = buf->getBufferSize(); 7861 switch (buf->getBufferKind()) { 7862 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7863 sizes.malloc_bytes += bytes; 7864 break; 7865 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7866 sizes.mmap_bytes += bytes; 7867 break; 7868 } 7869 } 7870 } 7871 } 7872 7873 void ASTReader::InitializeSema(Sema &S) { 7874 SemaObj = &S; 7875 S.addExternalSource(this); 7876 7877 // Makes sure any declarations that were deserialized "too early" 7878 // still get added to the identifier's declaration chains. 7879 for (uint64_t ID : PreloadedDeclIDs) { 7880 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7881 pushExternalDeclIntoScope(D, D->getDeclName()); 7882 } 7883 PreloadedDeclIDs.clear(); 7884 7885 // FIXME: What happens if these are changed by a module import? 7886 if (!FPPragmaOptions.empty()) { 7887 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7888 FPOptionsOverride NewOverrides = 7889 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7890 SemaObj->CurFPFeatures = 7891 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7892 } 7893 7894 SemaObj->OpenCLFeatures = OpenCLExtensions; 7895 7896 UpdateSema(); 7897 } 7898 7899 void ASTReader::UpdateSema() { 7900 assert(SemaObj && "no Sema to update"); 7901 7902 // Load the offsets of the declarations that Sema references. 7903 // They will be lazily deserialized when needed. 7904 if (!SemaDeclRefs.empty()) { 7905 assert(SemaDeclRefs.size() % 3 == 0); 7906 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7907 if (!SemaObj->StdNamespace) 7908 SemaObj->StdNamespace = SemaDeclRefs[I]; 7909 if (!SemaObj->StdBadAlloc) 7910 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7911 if (!SemaObj->StdAlignValT) 7912 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7913 } 7914 SemaDeclRefs.clear(); 7915 } 7916 7917 // Update the state of pragmas. Use the same API as if we had encountered the 7918 // pragma in the source. 7919 if(OptimizeOffPragmaLocation.isValid()) 7920 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7921 if (PragmaMSStructState != -1) 7922 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7923 if (PointersToMembersPragmaLocation.isValid()) { 7924 SemaObj->ActOnPragmaMSPointersToMembers( 7925 (LangOptions::PragmaMSPointersToMembersKind) 7926 PragmaMSPointersToMembersState, 7927 PointersToMembersPragmaLocation); 7928 } 7929 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7930 7931 if (PragmaAlignPackCurrentValue) { 7932 // The bottom of the stack might have a default value. It must be adjusted 7933 // to the current value to ensure that the packing state is preserved after 7934 // popping entries that were included/imported from a PCH/module. 7935 bool DropFirst = false; 7936 if (!PragmaAlignPackStack.empty() && 7937 PragmaAlignPackStack.front().Location.isInvalid()) { 7938 assert(PragmaAlignPackStack.front().Value == 7939 SemaObj->AlignPackStack.DefaultValue && 7940 "Expected a default alignment value"); 7941 SemaObj->AlignPackStack.Stack.emplace_back( 7942 PragmaAlignPackStack.front().SlotLabel, 7943 SemaObj->AlignPackStack.CurrentValue, 7944 SemaObj->AlignPackStack.CurrentPragmaLocation, 7945 PragmaAlignPackStack.front().PushLocation); 7946 DropFirst = true; 7947 } 7948 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack) 7949 .drop_front(DropFirst ? 1 : 0)) { 7950 SemaObj->AlignPackStack.Stack.emplace_back( 7951 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7952 } 7953 if (PragmaAlignPackCurrentLocation.isInvalid()) { 7954 assert(*PragmaAlignPackCurrentValue == 7955 SemaObj->AlignPackStack.DefaultValue && 7956 "Expected a default align and pack value"); 7957 // Keep the current values. 7958 } else { 7959 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue; 7960 SemaObj->AlignPackStack.CurrentPragmaLocation = 7961 PragmaAlignPackCurrentLocation; 7962 } 7963 } 7964 if (FpPragmaCurrentValue) { 7965 // The bottom of the stack might have a default value. It must be adjusted 7966 // to the current value to ensure that fp-pragma state is preserved after 7967 // popping entries that were included/imported from a PCH/module. 7968 bool DropFirst = false; 7969 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7970 assert(FpPragmaStack.front().Value == 7971 SemaObj->FpPragmaStack.DefaultValue && 7972 "Expected a default pragma float_control value"); 7973 SemaObj->FpPragmaStack.Stack.emplace_back( 7974 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7975 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7976 FpPragmaStack.front().PushLocation); 7977 DropFirst = true; 7978 } 7979 for (const auto &Entry : 7980 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7981 SemaObj->FpPragmaStack.Stack.emplace_back( 7982 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7983 if (FpPragmaCurrentLocation.isInvalid()) { 7984 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7985 "Expected a default pragma float_control value"); 7986 // Keep the current values. 7987 } else { 7988 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7989 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7990 } 7991 } 7992 7993 // For non-modular AST files, restore visiblity of modules. 7994 for (auto &Import : ImportedModules) { 7995 if (Import.ImportLoc.isInvalid()) 7996 continue; 7997 if (Module *Imported = getSubmodule(Import.ID)) { 7998 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 7999 } 8000 } 8001 } 8002 8003 IdentifierInfo *ASTReader::get(StringRef Name) { 8004 // Note that we are loading an identifier. 8005 Deserializing AnIdentifier(this); 8006 8007 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 8008 NumIdentifierLookups, 8009 NumIdentifierLookupHits); 8010 8011 // We don't need to do identifier table lookups in C++ modules (we preload 8012 // all interesting declarations, and don't need to use the scope for name 8013 // lookups). Perform the lookup in PCH files, though, since we don't build 8014 // a complete initial identifier table if we're carrying on from a PCH. 8015 if (PP.getLangOpts().CPlusPlus) { 8016 for (auto F : ModuleMgr.pch_modules()) 8017 if (Visitor(*F)) 8018 break; 8019 } else { 8020 // If there is a global index, look there first to determine which modules 8021 // provably do not have any results for this identifier. 8022 GlobalModuleIndex::HitSet Hits; 8023 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 8024 if (!loadGlobalIndex()) { 8025 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 8026 HitsPtr = &Hits; 8027 } 8028 } 8029 8030 ModuleMgr.visit(Visitor, HitsPtr); 8031 } 8032 8033 IdentifierInfo *II = Visitor.getIdentifierInfo(); 8034 markIdentifierUpToDate(II); 8035 return II; 8036 } 8037 8038 namespace clang { 8039 8040 /// An identifier-lookup iterator that enumerates all of the 8041 /// identifiers stored within a set of AST files. 8042 class ASTIdentifierIterator : public IdentifierIterator { 8043 /// The AST reader whose identifiers are being enumerated. 8044 const ASTReader &Reader; 8045 8046 /// The current index into the chain of AST files stored in 8047 /// the AST reader. 8048 unsigned Index; 8049 8050 /// The current position within the identifier lookup table 8051 /// of the current AST file. 8052 ASTIdentifierLookupTable::key_iterator Current; 8053 8054 /// The end position within the identifier lookup table of 8055 /// the current AST file. 8056 ASTIdentifierLookupTable::key_iterator End; 8057 8058 /// Whether to skip any modules in the ASTReader. 8059 bool SkipModules; 8060 8061 public: 8062 explicit ASTIdentifierIterator(const ASTReader &Reader, 8063 bool SkipModules = false); 8064 8065 StringRef Next() override; 8066 }; 8067 8068 } // namespace clang 8069 8070 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8071 bool SkipModules) 8072 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8073 } 8074 8075 StringRef ASTIdentifierIterator::Next() { 8076 while (Current == End) { 8077 // If we have exhausted all of our AST files, we're done. 8078 if (Index == 0) 8079 return StringRef(); 8080 8081 --Index; 8082 ModuleFile &F = Reader.ModuleMgr[Index]; 8083 if (SkipModules && F.isModule()) 8084 continue; 8085 8086 ASTIdentifierLookupTable *IdTable = 8087 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8088 Current = IdTable->key_begin(); 8089 End = IdTable->key_end(); 8090 } 8091 8092 // We have any identifiers remaining in the current AST file; return 8093 // the next one. 8094 StringRef Result = *Current; 8095 ++Current; 8096 return Result; 8097 } 8098 8099 namespace { 8100 8101 /// A utility for appending two IdentifierIterators. 8102 class ChainedIdentifierIterator : public IdentifierIterator { 8103 std::unique_ptr<IdentifierIterator> Current; 8104 std::unique_ptr<IdentifierIterator> Queued; 8105 8106 public: 8107 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8108 std::unique_ptr<IdentifierIterator> Second) 8109 : Current(std::move(First)), Queued(std::move(Second)) {} 8110 8111 StringRef Next() override { 8112 if (!Current) 8113 return StringRef(); 8114 8115 StringRef result = Current->Next(); 8116 if (!result.empty()) 8117 return result; 8118 8119 // Try the queued iterator, which may itself be empty. 8120 Current.reset(); 8121 std::swap(Current, Queued); 8122 return Next(); 8123 } 8124 }; 8125 8126 } // namespace 8127 8128 IdentifierIterator *ASTReader::getIdentifiers() { 8129 if (!loadGlobalIndex()) { 8130 std::unique_ptr<IdentifierIterator> ReaderIter( 8131 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8132 std::unique_ptr<IdentifierIterator> ModulesIter( 8133 GlobalIndex->createIdentifierIterator()); 8134 return new ChainedIdentifierIterator(std::move(ReaderIter), 8135 std::move(ModulesIter)); 8136 } 8137 8138 return new ASTIdentifierIterator(*this); 8139 } 8140 8141 namespace clang { 8142 namespace serialization { 8143 8144 class ReadMethodPoolVisitor { 8145 ASTReader &Reader; 8146 Selector Sel; 8147 unsigned PriorGeneration; 8148 unsigned InstanceBits = 0; 8149 unsigned FactoryBits = 0; 8150 bool InstanceHasMoreThanOneDecl = false; 8151 bool FactoryHasMoreThanOneDecl = false; 8152 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8153 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8154 8155 public: 8156 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8157 unsigned PriorGeneration) 8158 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8159 8160 bool operator()(ModuleFile &M) { 8161 if (!M.SelectorLookupTable) 8162 return false; 8163 8164 // If we've already searched this module file, skip it now. 8165 if (M.Generation <= PriorGeneration) 8166 return true; 8167 8168 ++Reader.NumMethodPoolTableLookups; 8169 ASTSelectorLookupTable *PoolTable 8170 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8171 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8172 if (Pos == PoolTable->end()) 8173 return false; 8174 8175 ++Reader.NumMethodPoolTableHits; 8176 ++Reader.NumSelectorsRead; 8177 // FIXME: Not quite happy with the statistics here. We probably should 8178 // disable this tracking when called via LoadSelector. 8179 // Also, should entries without methods count as misses? 8180 ++Reader.NumMethodPoolEntriesRead; 8181 ASTSelectorLookupTrait::data_type Data = *Pos; 8182 if (Reader.DeserializationListener) 8183 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8184 8185 // Append methods in the reverse order, so that later we can process them 8186 // in the order they appear in the source code by iterating through 8187 // the vector in the reverse order. 8188 InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend()); 8189 FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend()); 8190 InstanceBits = Data.InstanceBits; 8191 FactoryBits = Data.FactoryBits; 8192 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8193 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8194 return false; 8195 } 8196 8197 /// Retrieve the instance methods found by this visitor. 8198 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8199 return InstanceMethods; 8200 } 8201 8202 /// Retrieve the instance methods found by this visitor. 8203 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8204 return FactoryMethods; 8205 } 8206 8207 unsigned getInstanceBits() const { return InstanceBits; } 8208 unsigned getFactoryBits() const { return FactoryBits; } 8209 8210 bool instanceHasMoreThanOneDecl() const { 8211 return InstanceHasMoreThanOneDecl; 8212 } 8213 8214 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8215 }; 8216 8217 } // namespace serialization 8218 } // namespace clang 8219 8220 /// Add the given set of methods to the method list. 8221 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8222 ObjCMethodList &List) { 8223 for (auto I = Methods.rbegin(), E = Methods.rend(); I != E; ++I) 8224 S.addMethodToGlobalList(&List, *I); 8225 } 8226 8227 void ASTReader::ReadMethodPool(Selector Sel) { 8228 // Get the selector generation and update it to the current generation. 8229 unsigned &Generation = SelectorGeneration[Sel]; 8230 unsigned PriorGeneration = Generation; 8231 Generation = getGeneration(); 8232 SelectorOutOfDate[Sel] = false; 8233 8234 // Search for methods defined with this selector. 8235 ++NumMethodPoolLookups; 8236 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8237 ModuleMgr.visit(Visitor); 8238 8239 if (Visitor.getInstanceMethods().empty() && 8240 Visitor.getFactoryMethods().empty()) 8241 return; 8242 8243 ++NumMethodPoolHits; 8244 8245 if (!getSema()) 8246 return; 8247 8248 Sema &S = *getSema(); 8249 Sema::GlobalMethodPool::iterator Pos = 8250 S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists())) 8251 .first; 8252 8253 Pos->second.first.setBits(Visitor.getInstanceBits()); 8254 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8255 Pos->second.second.setBits(Visitor.getFactoryBits()); 8256 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8257 8258 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8259 // when building a module we keep every method individually and may need to 8260 // update hasMoreThanOneDecl as we add the methods. 8261 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8262 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8263 } 8264 8265 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8266 if (SelectorOutOfDate[Sel]) 8267 ReadMethodPool(Sel); 8268 } 8269 8270 void ASTReader::ReadKnownNamespaces( 8271 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8272 Namespaces.clear(); 8273 8274 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8275 if (NamespaceDecl *Namespace 8276 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8277 Namespaces.push_back(Namespace); 8278 } 8279 } 8280 8281 void ASTReader::ReadUndefinedButUsed( 8282 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8283 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8284 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8285 SourceLocation Loc = 8286 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8287 Undefined.insert(std::make_pair(D, Loc)); 8288 } 8289 } 8290 8291 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8292 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8293 Exprs) { 8294 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8295 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8296 uint64_t Count = DelayedDeleteExprs[Idx++]; 8297 for (uint64_t C = 0; C < Count; ++C) { 8298 SourceLocation DeleteLoc = 8299 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8300 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8301 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8302 } 8303 } 8304 } 8305 8306 void ASTReader::ReadTentativeDefinitions( 8307 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8308 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8309 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8310 if (Var) 8311 TentativeDefs.push_back(Var); 8312 } 8313 TentativeDefinitions.clear(); 8314 } 8315 8316 void ASTReader::ReadUnusedFileScopedDecls( 8317 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8318 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8319 DeclaratorDecl *D 8320 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8321 if (D) 8322 Decls.push_back(D); 8323 } 8324 UnusedFileScopedDecls.clear(); 8325 } 8326 8327 void ASTReader::ReadDelegatingConstructors( 8328 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8329 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8330 CXXConstructorDecl *D 8331 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8332 if (D) 8333 Decls.push_back(D); 8334 } 8335 DelegatingCtorDecls.clear(); 8336 } 8337 8338 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8339 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8340 TypedefNameDecl *D 8341 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8342 if (D) 8343 Decls.push_back(D); 8344 } 8345 ExtVectorDecls.clear(); 8346 } 8347 8348 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8349 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8350 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8351 ++I) { 8352 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8353 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8354 if (D) 8355 Decls.insert(D); 8356 } 8357 UnusedLocalTypedefNameCandidates.clear(); 8358 } 8359 8360 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8361 llvm::SmallSetVector<Decl *, 4> &Decls) { 8362 for (auto I : DeclsToCheckForDeferredDiags) { 8363 auto *D = dyn_cast_or_null<Decl>(GetDecl(I)); 8364 if (D) 8365 Decls.insert(D); 8366 } 8367 DeclsToCheckForDeferredDiags.clear(); 8368 } 8369 8370 void ASTReader::ReadReferencedSelectors( 8371 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8372 if (ReferencedSelectorsData.empty()) 8373 return; 8374 8375 // If there are @selector references added them to its pool. This is for 8376 // implementation of -Wselector. 8377 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8378 unsigned I = 0; 8379 while (I < DataSize) { 8380 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8381 SourceLocation SelLoc 8382 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8383 Sels.push_back(std::make_pair(Sel, SelLoc)); 8384 } 8385 ReferencedSelectorsData.clear(); 8386 } 8387 8388 void ASTReader::ReadWeakUndeclaredIdentifiers( 8389 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8390 if (WeakUndeclaredIdentifiers.empty()) 8391 return; 8392 8393 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8394 IdentifierInfo *WeakId 8395 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8396 IdentifierInfo *AliasId 8397 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8398 SourceLocation Loc = 8399 SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8400 WeakInfo WI(AliasId, Loc); 8401 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8402 } 8403 WeakUndeclaredIdentifiers.clear(); 8404 } 8405 8406 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8407 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8408 ExternalVTableUse VT; 8409 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8410 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8411 VT.DefinitionRequired = VTableUses[Idx++]; 8412 VTables.push_back(VT); 8413 } 8414 8415 VTableUses.clear(); 8416 } 8417 8418 void ASTReader::ReadPendingInstantiations( 8419 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8420 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8421 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8422 SourceLocation Loc 8423 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8424 8425 Pending.push_back(std::make_pair(D, Loc)); 8426 } 8427 PendingInstantiations.clear(); 8428 } 8429 8430 void ASTReader::ReadLateParsedTemplates( 8431 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8432 &LPTMap) { 8433 for (auto &LPT : LateParsedTemplates) { 8434 ModuleFile *FMod = LPT.first; 8435 RecordDataImpl &LateParsed = LPT.second; 8436 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8437 /* In loop */) { 8438 FunctionDecl *FD = 8439 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8440 8441 auto LT = std::make_unique<LateParsedTemplate>(); 8442 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8443 8444 ModuleFile *F = getOwningModuleFile(LT->D); 8445 assert(F && "No module"); 8446 8447 unsigned TokN = LateParsed[Idx++]; 8448 LT->Toks.reserve(TokN); 8449 for (unsigned T = 0; T < TokN; ++T) 8450 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8451 8452 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8453 } 8454 } 8455 8456 LateParsedTemplates.clear(); 8457 } 8458 8459 void ASTReader::LoadSelector(Selector Sel) { 8460 // It would be complicated to avoid reading the methods anyway. So don't. 8461 ReadMethodPool(Sel); 8462 } 8463 8464 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8465 assert(ID && "Non-zero identifier ID required"); 8466 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8467 IdentifiersLoaded[ID - 1] = II; 8468 if (DeserializationListener) 8469 DeserializationListener->IdentifierRead(ID, II); 8470 } 8471 8472 /// Set the globally-visible declarations associated with the given 8473 /// identifier. 8474 /// 8475 /// If the AST reader is currently in a state where the given declaration IDs 8476 /// cannot safely be resolved, they are queued until it is safe to resolve 8477 /// them. 8478 /// 8479 /// \param II an IdentifierInfo that refers to one or more globally-visible 8480 /// declarations. 8481 /// 8482 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8483 /// visible at global scope. 8484 /// 8485 /// \param Decls if non-null, this vector will be populated with the set of 8486 /// deserialized declarations. These declarations will not be pushed into 8487 /// scope. 8488 void 8489 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8490 const SmallVectorImpl<uint32_t> &DeclIDs, 8491 SmallVectorImpl<Decl *> *Decls) { 8492 if (NumCurrentElementsDeserializing && !Decls) { 8493 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8494 return; 8495 } 8496 8497 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8498 if (!SemaObj) { 8499 // Queue this declaration so that it will be added to the 8500 // translation unit scope and identifier's declaration chain 8501 // once a Sema object is known. 8502 PreloadedDeclIDs.push_back(DeclIDs[I]); 8503 continue; 8504 } 8505 8506 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8507 8508 // If we're simply supposed to record the declarations, do so now. 8509 if (Decls) { 8510 Decls->push_back(D); 8511 continue; 8512 } 8513 8514 // Introduce this declaration into the translation-unit scope 8515 // and add it to the declaration chain for this identifier, so 8516 // that (unqualified) name lookup will find it. 8517 pushExternalDeclIntoScope(D, II); 8518 } 8519 } 8520 8521 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8522 if (ID == 0) 8523 return nullptr; 8524 8525 if (IdentifiersLoaded.empty()) { 8526 Error("no identifier table in AST file"); 8527 return nullptr; 8528 } 8529 8530 ID -= 1; 8531 if (!IdentifiersLoaded[ID]) { 8532 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8533 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8534 ModuleFile *M = I->second; 8535 unsigned Index = ID - M->BaseIdentifierID; 8536 const unsigned char *Data = 8537 M->IdentifierTableData + M->IdentifierOffsets[Index]; 8538 8539 ASTIdentifierLookupTrait Trait(*this, *M); 8540 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 8541 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 8542 auto &II = PP.getIdentifierTable().get(Key); 8543 IdentifiersLoaded[ID] = &II; 8544 markIdentifierFromAST(*this, II); 8545 if (DeserializationListener) 8546 DeserializationListener->IdentifierRead(ID + 1, &II); 8547 } 8548 8549 return IdentifiersLoaded[ID]; 8550 } 8551 8552 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8553 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8554 } 8555 8556 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8557 if (LocalID < NUM_PREDEF_IDENT_IDS) 8558 return LocalID; 8559 8560 if (!M.ModuleOffsetMap.empty()) 8561 ReadModuleOffsetMap(M); 8562 8563 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8564 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8565 assert(I != M.IdentifierRemap.end() 8566 && "Invalid index into identifier index remap"); 8567 8568 return LocalID + I->second; 8569 } 8570 8571 MacroInfo *ASTReader::getMacro(MacroID ID) { 8572 if (ID == 0) 8573 return nullptr; 8574 8575 if (MacrosLoaded.empty()) { 8576 Error("no macro table in AST file"); 8577 return nullptr; 8578 } 8579 8580 ID -= NUM_PREDEF_MACRO_IDS; 8581 if (!MacrosLoaded[ID]) { 8582 GlobalMacroMapType::iterator I 8583 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8584 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8585 ModuleFile *M = I->second; 8586 unsigned Index = ID - M->BaseMacroID; 8587 MacrosLoaded[ID] = 8588 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8589 8590 if (DeserializationListener) 8591 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8592 MacrosLoaded[ID]); 8593 } 8594 8595 return MacrosLoaded[ID]; 8596 } 8597 8598 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8599 if (LocalID < NUM_PREDEF_MACRO_IDS) 8600 return LocalID; 8601 8602 if (!M.ModuleOffsetMap.empty()) 8603 ReadModuleOffsetMap(M); 8604 8605 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8606 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8607 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8608 8609 return LocalID + I->second; 8610 } 8611 8612 serialization::SubmoduleID 8613 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8614 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8615 return LocalID; 8616 8617 if (!M.ModuleOffsetMap.empty()) 8618 ReadModuleOffsetMap(M); 8619 8620 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8621 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8622 assert(I != M.SubmoduleRemap.end() 8623 && "Invalid index into submodule index remap"); 8624 8625 return LocalID + I->second; 8626 } 8627 8628 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8629 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8630 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8631 return nullptr; 8632 } 8633 8634 if (GlobalID > SubmodulesLoaded.size()) { 8635 Error("submodule ID out of range in AST file"); 8636 return nullptr; 8637 } 8638 8639 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8640 } 8641 8642 Module *ASTReader::getModule(unsigned ID) { 8643 return getSubmodule(ID); 8644 } 8645 8646 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8647 if (ID & 1) { 8648 // It's a module, look it up by submodule ID. 8649 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8650 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8651 } else { 8652 // It's a prefix (preamble, PCH, ...). Look it up by index. 8653 unsigned IndexFromEnd = ID >> 1; 8654 assert(IndexFromEnd && "got reference to unknown module file"); 8655 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8656 } 8657 } 8658 8659 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8660 if (!F) 8661 return 1; 8662 8663 // For a file representing a module, use the submodule ID of the top-level 8664 // module as the file ID. For any other kind of file, the number of such 8665 // files loaded beforehand will be the same on reload. 8666 // FIXME: Is this true even if we have an explicit module file and a PCH? 8667 if (F->isModule()) 8668 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8669 8670 auto PCHModules = getModuleManager().pch_modules(); 8671 auto I = llvm::find(PCHModules, F); 8672 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8673 return (I - PCHModules.end()) << 1; 8674 } 8675 8676 llvm::Optional<ASTSourceDescriptor> 8677 ASTReader::getSourceDescriptor(unsigned ID) { 8678 if (Module *M = getSubmodule(ID)) 8679 return ASTSourceDescriptor(*M); 8680 8681 // If there is only a single PCH, return it instead. 8682 // Chained PCH are not supported. 8683 const auto &PCHChain = ModuleMgr.pch_modules(); 8684 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8685 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8686 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8687 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8688 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8689 MF.Signature); 8690 } 8691 return None; 8692 } 8693 8694 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8695 auto I = DefinitionSource.find(FD); 8696 if (I == DefinitionSource.end()) 8697 return EK_ReplyHazy; 8698 return I->second ? EK_Never : EK_Always; 8699 } 8700 8701 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8702 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8703 } 8704 8705 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8706 if (ID == 0) 8707 return Selector(); 8708 8709 if (ID > SelectorsLoaded.size()) { 8710 Error("selector ID out of range in AST file"); 8711 return Selector(); 8712 } 8713 8714 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8715 // Load this selector from the selector table. 8716 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8717 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8718 ModuleFile &M = *I->second; 8719 ASTSelectorLookupTrait Trait(*this, M); 8720 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8721 SelectorsLoaded[ID - 1] = 8722 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8723 if (DeserializationListener) 8724 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8725 } 8726 8727 return SelectorsLoaded[ID - 1]; 8728 } 8729 8730 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8731 return DecodeSelector(ID); 8732 } 8733 8734 uint32_t ASTReader::GetNumExternalSelectors() { 8735 // ID 0 (the null selector) is considered an external selector. 8736 return getTotalNumSelectors() + 1; 8737 } 8738 8739 serialization::SelectorID 8740 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8741 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8742 return LocalID; 8743 8744 if (!M.ModuleOffsetMap.empty()) 8745 ReadModuleOffsetMap(M); 8746 8747 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8748 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8749 assert(I != M.SelectorRemap.end() 8750 && "Invalid index into selector index remap"); 8751 8752 return LocalID + I->second; 8753 } 8754 8755 DeclarationNameLoc 8756 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8757 switch (Name.getNameKind()) { 8758 case DeclarationName::CXXConstructorName: 8759 case DeclarationName::CXXDestructorName: 8760 case DeclarationName::CXXConversionFunctionName: 8761 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); 8762 8763 case DeclarationName::CXXOperatorName: 8764 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); 8765 8766 case DeclarationName::CXXLiteralOperatorName: 8767 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( 8768 readSourceLocation()); 8769 8770 case DeclarationName::Identifier: 8771 case DeclarationName::ObjCZeroArgSelector: 8772 case DeclarationName::ObjCOneArgSelector: 8773 case DeclarationName::ObjCMultiArgSelector: 8774 case DeclarationName::CXXUsingDirective: 8775 case DeclarationName::CXXDeductionGuideName: 8776 break; 8777 } 8778 return DeclarationNameLoc(); 8779 } 8780 8781 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8782 DeclarationNameInfo NameInfo; 8783 NameInfo.setName(readDeclarationName()); 8784 NameInfo.setLoc(readSourceLocation()); 8785 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8786 return NameInfo; 8787 } 8788 8789 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8790 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8791 unsigned NumTPLists = readInt(); 8792 Info.NumTemplParamLists = NumTPLists; 8793 if (NumTPLists) { 8794 Info.TemplParamLists = 8795 new (getContext()) TemplateParameterList *[NumTPLists]; 8796 for (unsigned i = 0; i != NumTPLists; ++i) 8797 Info.TemplParamLists[i] = readTemplateParameterList(); 8798 } 8799 } 8800 8801 TemplateParameterList * 8802 ASTRecordReader::readTemplateParameterList() { 8803 SourceLocation TemplateLoc = readSourceLocation(); 8804 SourceLocation LAngleLoc = readSourceLocation(); 8805 SourceLocation RAngleLoc = readSourceLocation(); 8806 8807 unsigned NumParams = readInt(); 8808 SmallVector<NamedDecl *, 16> Params; 8809 Params.reserve(NumParams); 8810 while (NumParams--) 8811 Params.push_back(readDeclAs<NamedDecl>()); 8812 8813 bool HasRequiresClause = readBool(); 8814 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8815 8816 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8817 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8818 return TemplateParams; 8819 } 8820 8821 void ASTRecordReader::readTemplateArgumentList( 8822 SmallVectorImpl<TemplateArgument> &TemplArgs, 8823 bool Canonicalize) { 8824 unsigned NumTemplateArgs = readInt(); 8825 TemplArgs.reserve(NumTemplateArgs); 8826 while (NumTemplateArgs--) 8827 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8828 } 8829 8830 /// Read a UnresolvedSet structure. 8831 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8832 unsigned NumDecls = readInt(); 8833 Set.reserve(getContext(), NumDecls); 8834 while (NumDecls--) { 8835 DeclID ID = readDeclID(); 8836 AccessSpecifier AS = (AccessSpecifier) readInt(); 8837 Set.addLazyDecl(getContext(), ID, AS); 8838 } 8839 } 8840 8841 CXXBaseSpecifier 8842 ASTRecordReader::readCXXBaseSpecifier() { 8843 bool isVirtual = readBool(); 8844 bool isBaseOfClass = readBool(); 8845 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8846 bool inheritConstructors = readBool(); 8847 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8848 SourceRange Range = readSourceRange(); 8849 SourceLocation EllipsisLoc = readSourceLocation(); 8850 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8851 EllipsisLoc); 8852 Result.setInheritConstructors(inheritConstructors); 8853 return Result; 8854 } 8855 8856 CXXCtorInitializer ** 8857 ASTRecordReader::readCXXCtorInitializers() { 8858 ASTContext &Context = getContext(); 8859 unsigned NumInitializers = readInt(); 8860 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8861 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8862 for (unsigned i = 0; i != NumInitializers; ++i) { 8863 TypeSourceInfo *TInfo = nullptr; 8864 bool IsBaseVirtual = false; 8865 FieldDecl *Member = nullptr; 8866 IndirectFieldDecl *IndirectMember = nullptr; 8867 8868 CtorInitializerType Type = (CtorInitializerType) readInt(); 8869 switch (Type) { 8870 case CTOR_INITIALIZER_BASE: 8871 TInfo = readTypeSourceInfo(); 8872 IsBaseVirtual = readBool(); 8873 break; 8874 8875 case CTOR_INITIALIZER_DELEGATING: 8876 TInfo = readTypeSourceInfo(); 8877 break; 8878 8879 case CTOR_INITIALIZER_MEMBER: 8880 Member = readDeclAs<FieldDecl>(); 8881 break; 8882 8883 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8884 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8885 break; 8886 } 8887 8888 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8889 Expr *Init = readExpr(); 8890 SourceLocation LParenLoc = readSourceLocation(); 8891 SourceLocation RParenLoc = readSourceLocation(); 8892 8893 CXXCtorInitializer *BOMInit; 8894 if (Type == CTOR_INITIALIZER_BASE) 8895 BOMInit = new (Context) 8896 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8897 RParenLoc, MemberOrEllipsisLoc); 8898 else if (Type == CTOR_INITIALIZER_DELEGATING) 8899 BOMInit = new (Context) 8900 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8901 else if (Member) 8902 BOMInit = new (Context) 8903 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8904 Init, RParenLoc); 8905 else 8906 BOMInit = new (Context) 8907 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8908 LParenLoc, Init, RParenLoc); 8909 8910 if (/*IsWritten*/readBool()) { 8911 unsigned SourceOrder = readInt(); 8912 BOMInit->setSourceOrder(SourceOrder); 8913 } 8914 8915 CtorInitializers[i] = BOMInit; 8916 } 8917 8918 return CtorInitializers; 8919 } 8920 8921 NestedNameSpecifierLoc 8922 ASTRecordReader::readNestedNameSpecifierLoc() { 8923 ASTContext &Context = getContext(); 8924 unsigned N = readInt(); 8925 NestedNameSpecifierLocBuilder Builder; 8926 for (unsigned I = 0; I != N; ++I) { 8927 auto Kind = readNestedNameSpecifierKind(); 8928 switch (Kind) { 8929 case NestedNameSpecifier::Identifier: { 8930 IdentifierInfo *II = readIdentifier(); 8931 SourceRange Range = readSourceRange(); 8932 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8933 break; 8934 } 8935 8936 case NestedNameSpecifier::Namespace: { 8937 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8938 SourceRange Range = readSourceRange(); 8939 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8940 break; 8941 } 8942 8943 case NestedNameSpecifier::NamespaceAlias: { 8944 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8945 SourceRange Range = readSourceRange(); 8946 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8947 break; 8948 } 8949 8950 case NestedNameSpecifier::TypeSpec: 8951 case NestedNameSpecifier::TypeSpecWithTemplate: { 8952 bool Template = readBool(); 8953 TypeSourceInfo *T = readTypeSourceInfo(); 8954 if (!T) 8955 return NestedNameSpecifierLoc(); 8956 SourceLocation ColonColonLoc = readSourceLocation(); 8957 8958 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8959 Builder.Extend(Context, 8960 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8961 T->getTypeLoc(), ColonColonLoc); 8962 break; 8963 } 8964 8965 case NestedNameSpecifier::Global: { 8966 SourceLocation ColonColonLoc = readSourceLocation(); 8967 Builder.MakeGlobal(Context, ColonColonLoc); 8968 break; 8969 } 8970 8971 case NestedNameSpecifier::Super: { 8972 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8973 SourceRange Range = readSourceRange(); 8974 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8975 break; 8976 } 8977 } 8978 } 8979 8980 return Builder.getWithLocInContext(Context); 8981 } 8982 8983 SourceRange 8984 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8985 unsigned &Idx) { 8986 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8987 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8988 return SourceRange(beg, end); 8989 } 8990 8991 /// Read a floating-point value 8992 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 8993 return llvm::APFloat(Sem, readAPInt()); 8994 } 8995 8996 // Read a string 8997 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 8998 unsigned Len = Record[Idx++]; 8999 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 9000 Idx += Len; 9001 return Result; 9002 } 9003 9004 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 9005 unsigned &Idx) { 9006 std::string Filename = ReadString(Record, Idx); 9007 ResolveImportedPath(F, Filename); 9008 return Filename; 9009 } 9010 9011 std::string ASTReader::ReadPath(StringRef BaseDirectory, 9012 const RecordData &Record, unsigned &Idx) { 9013 std::string Filename = ReadString(Record, Idx); 9014 if (!BaseDirectory.empty()) 9015 ResolveImportedPath(Filename, BaseDirectory); 9016 return Filename; 9017 } 9018 9019 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 9020 unsigned &Idx) { 9021 unsigned Major = Record[Idx++]; 9022 unsigned Minor = Record[Idx++]; 9023 unsigned Subminor = Record[Idx++]; 9024 if (Minor == 0) 9025 return VersionTuple(Major); 9026 if (Subminor == 0) 9027 return VersionTuple(Major, Minor - 1); 9028 return VersionTuple(Major, Minor - 1, Subminor - 1); 9029 } 9030 9031 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9032 const RecordData &Record, 9033 unsigned &Idx) { 9034 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9035 return CXXTemporary::Create(getContext(), Decl); 9036 } 9037 9038 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9039 return Diag(CurrentImportLoc, DiagID); 9040 } 9041 9042 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9043 return Diags.Report(Loc, DiagID); 9044 } 9045 9046 /// Retrieve the identifier table associated with the 9047 /// preprocessor. 9048 IdentifierTable &ASTReader::getIdentifierTable() { 9049 return PP.getIdentifierTable(); 9050 } 9051 9052 /// Record that the given ID maps to the given switch-case 9053 /// statement. 9054 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9055 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9056 "Already have a SwitchCase with this ID"); 9057 (*CurrSwitchCaseStmts)[ID] = SC; 9058 } 9059 9060 /// Retrieve the switch-case statement with the given ID. 9061 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9062 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9063 return (*CurrSwitchCaseStmts)[ID]; 9064 } 9065 9066 void ASTReader::ClearSwitchCaseIDs() { 9067 CurrSwitchCaseStmts->clear(); 9068 } 9069 9070 void ASTReader::ReadComments() { 9071 ASTContext &Context = getContext(); 9072 std::vector<RawComment *> Comments; 9073 for (SmallVectorImpl<std::pair<BitstreamCursor, 9074 serialization::ModuleFile *>>::iterator 9075 I = CommentsCursors.begin(), 9076 E = CommentsCursors.end(); 9077 I != E; ++I) { 9078 Comments.clear(); 9079 BitstreamCursor &Cursor = I->first; 9080 serialization::ModuleFile &F = *I->second; 9081 SavedStreamPosition SavedPosition(Cursor); 9082 9083 RecordData Record; 9084 while (true) { 9085 Expected<llvm::BitstreamEntry> MaybeEntry = 9086 Cursor.advanceSkippingSubblocks( 9087 BitstreamCursor::AF_DontPopBlockAtEnd); 9088 if (!MaybeEntry) { 9089 Error(MaybeEntry.takeError()); 9090 return; 9091 } 9092 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9093 9094 switch (Entry.Kind) { 9095 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9096 case llvm::BitstreamEntry::Error: 9097 Error("malformed block record in AST file"); 9098 return; 9099 case llvm::BitstreamEntry::EndBlock: 9100 goto NextCursor; 9101 case llvm::BitstreamEntry::Record: 9102 // The interesting case. 9103 break; 9104 } 9105 9106 // Read a record. 9107 Record.clear(); 9108 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9109 if (!MaybeComment) { 9110 Error(MaybeComment.takeError()); 9111 return; 9112 } 9113 switch ((CommentRecordTypes)MaybeComment.get()) { 9114 case COMMENTS_RAW_COMMENT: { 9115 unsigned Idx = 0; 9116 SourceRange SR = ReadSourceRange(F, Record, Idx); 9117 RawComment::CommentKind Kind = 9118 (RawComment::CommentKind) Record[Idx++]; 9119 bool IsTrailingComment = Record[Idx++]; 9120 bool IsAlmostTrailingComment = Record[Idx++]; 9121 Comments.push_back(new (Context) RawComment( 9122 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9123 break; 9124 } 9125 } 9126 } 9127 NextCursor: 9128 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9129 FileToOffsetToComment; 9130 for (RawComment *C : Comments) { 9131 SourceLocation CommentLoc = C->getBeginLoc(); 9132 if (CommentLoc.isValid()) { 9133 std::pair<FileID, unsigned> Loc = 9134 SourceMgr.getDecomposedLoc(CommentLoc); 9135 if (Loc.first.isValid()) 9136 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9137 } 9138 } 9139 } 9140 } 9141 9142 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9143 bool IncludeSystem, bool Complain, 9144 llvm::function_ref<void(const serialization::InputFile &IF, 9145 bool isSystem)> Visitor) { 9146 unsigned NumUserInputs = MF.NumUserInputFiles; 9147 unsigned NumInputs = MF.InputFilesLoaded.size(); 9148 assert(NumUserInputs <= NumInputs); 9149 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9150 for (unsigned I = 0; I < N; ++I) { 9151 bool IsSystem = I >= NumUserInputs; 9152 InputFile IF = getInputFile(MF, I+1, Complain); 9153 Visitor(IF, IsSystem); 9154 } 9155 } 9156 9157 void ASTReader::visitTopLevelModuleMaps( 9158 serialization::ModuleFile &MF, 9159 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9160 unsigned NumInputs = MF.InputFilesLoaded.size(); 9161 for (unsigned I = 0; I < NumInputs; ++I) { 9162 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9163 if (IFI.TopLevelModuleMap) 9164 // FIXME: This unnecessarily re-reads the InputFileInfo. 9165 if (auto FE = getInputFile(MF, I + 1).getFile()) 9166 Visitor(FE); 9167 } 9168 } 9169 9170 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9171 // If we know the owning module, use it. 9172 if (Module *M = D->getImportedOwningModule()) 9173 return M->getFullModuleName(); 9174 9175 // Otherwise, use the name of the top-level module the decl is within. 9176 if (ModuleFile *M = getOwningModuleFile(D)) 9177 return M->ModuleName; 9178 9179 // Not from a module. 9180 return {}; 9181 } 9182 9183 void ASTReader::finishPendingActions() { 9184 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9185 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9186 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9187 !PendingUpdateRecords.empty()) { 9188 // If any identifiers with corresponding top-level declarations have 9189 // been loaded, load those declarations now. 9190 using TopLevelDeclsMap = 9191 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9192 TopLevelDeclsMap TopLevelDecls; 9193 9194 while (!PendingIdentifierInfos.empty()) { 9195 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9196 SmallVector<uint32_t, 4> DeclIDs = 9197 std::move(PendingIdentifierInfos.back().second); 9198 PendingIdentifierInfos.pop_back(); 9199 9200 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9201 } 9202 9203 // Load each function type that we deferred loading because it was a 9204 // deduced type that might refer to a local type declared within itself. 9205 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9206 auto *FD = PendingFunctionTypes[I].first; 9207 FD->setType(GetType(PendingFunctionTypes[I].second)); 9208 9209 // If we gave a function a deduced return type, remember that we need to 9210 // propagate that along the redeclaration chain. 9211 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9212 if (DT && DT->isDeduced()) 9213 PendingDeducedTypeUpdates.insert( 9214 {FD->getCanonicalDecl(), FD->getReturnType()}); 9215 } 9216 PendingFunctionTypes.clear(); 9217 9218 // For each decl chain that we wanted to complete while deserializing, mark 9219 // it as "still needs to be completed". 9220 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9221 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9222 } 9223 PendingIncompleteDeclChains.clear(); 9224 9225 // Load pending declaration chains. 9226 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9227 loadPendingDeclChain(PendingDeclChains[I].first, 9228 PendingDeclChains[I].second); 9229 PendingDeclChains.clear(); 9230 9231 // Make the most recent of the top-level declarations visible. 9232 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9233 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9234 IdentifierInfo *II = TLD->first; 9235 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9236 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9237 } 9238 } 9239 9240 // Load any pending macro definitions. 9241 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9242 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9243 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9244 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9245 // Initialize the macro history from chained-PCHs ahead of module imports. 9246 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9247 ++IDIdx) { 9248 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9249 if (!Info.M->isModule()) 9250 resolvePendingMacro(II, Info); 9251 } 9252 // Handle module imports. 9253 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9254 ++IDIdx) { 9255 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9256 if (Info.M->isModule()) 9257 resolvePendingMacro(II, Info); 9258 } 9259 } 9260 PendingMacroIDs.clear(); 9261 9262 // Wire up the DeclContexts for Decls that we delayed setting until 9263 // recursive loading is completed. 9264 while (!PendingDeclContextInfos.empty()) { 9265 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9266 PendingDeclContextInfos.pop_front(); 9267 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9268 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9269 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9270 } 9271 9272 // Perform any pending declaration updates. 9273 while (!PendingUpdateRecords.empty()) { 9274 auto Update = PendingUpdateRecords.pop_back_val(); 9275 ReadingKindTracker ReadingKind(Read_Decl, *this); 9276 loadDeclUpdateRecords(Update); 9277 } 9278 } 9279 9280 // At this point, all update records for loaded decls are in place, so any 9281 // fake class definitions should have become real. 9282 assert(PendingFakeDefinitionData.empty() && 9283 "faked up a class definition but never saw the real one"); 9284 9285 // If we deserialized any C++ or Objective-C class definitions, any 9286 // Objective-C protocol definitions, or any redeclarable templates, make sure 9287 // that all redeclarations point to the definitions. Note that this can only 9288 // happen now, after the redeclaration chains have been fully wired. 9289 for (Decl *D : PendingDefinitions) { 9290 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9291 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9292 // Make sure that the TagType points at the definition. 9293 const_cast<TagType*>(TagT)->decl = TD; 9294 } 9295 9296 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9297 for (auto *R = getMostRecentExistingDecl(RD); R; 9298 R = R->getPreviousDecl()) { 9299 assert((R == D) == 9300 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9301 "declaration thinks it's the definition but it isn't"); 9302 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9303 } 9304 } 9305 9306 continue; 9307 } 9308 9309 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9310 // Make sure that the ObjCInterfaceType points at the definition. 9311 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9312 ->Decl = ID; 9313 9314 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9315 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9316 9317 continue; 9318 } 9319 9320 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9321 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9322 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9323 9324 continue; 9325 } 9326 9327 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9328 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9329 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9330 } 9331 PendingDefinitions.clear(); 9332 9333 // Load the bodies of any functions or methods we've encountered. We do 9334 // this now (delayed) so that we can be sure that the declaration chains 9335 // have been fully wired up (hasBody relies on this). 9336 // FIXME: We shouldn't require complete redeclaration chains here. 9337 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9338 PBEnd = PendingBodies.end(); 9339 PB != PBEnd; ++PB) { 9340 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9341 // For a function defined inline within a class template, force the 9342 // canonical definition to be the one inside the canonical definition of 9343 // the template. This ensures that we instantiate from a correct view 9344 // of the template. 9345 // 9346 // Sadly we can't do this more generally: we can't be sure that all 9347 // copies of an arbitrary class definition will have the same members 9348 // defined (eg, some member functions may not be instantiated, and some 9349 // special members may or may not have been implicitly defined). 9350 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9351 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9352 continue; 9353 9354 // FIXME: Check for =delete/=default? 9355 // FIXME: Complain about ODR violations here? 9356 const FunctionDecl *Defn = nullptr; 9357 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9358 FD->setLazyBody(PB->second); 9359 } else { 9360 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9361 mergeDefinitionVisibility(NonConstDefn, FD); 9362 9363 if (!FD->isLateTemplateParsed() && 9364 !NonConstDefn->isLateTemplateParsed() && 9365 FD->getODRHash() != NonConstDefn->getODRHash()) { 9366 if (!isa<CXXMethodDecl>(FD)) { 9367 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9368 } else if (FD->getLexicalParent()->isFileContext() && 9369 NonConstDefn->getLexicalParent()->isFileContext()) { 9370 // Only diagnose out-of-line method definitions. If they are 9371 // in class definitions, then an error will be generated when 9372 // processing the class bodies. 9373 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9374 } 9375 } 9376 } 9377 continue; 9378 } 9379 9380 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9381 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9382 MD->setLazyBody(PB->second); 9383 } 9384 PendingBodies.clear(); 9385 9386 // Do some cleanup. 9387 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9388 getContext().deduplicateMergedDefinitonsFor(ND); 9389 PendingMergedDefinitionsToDeduplicate.clear(); 9390 } 9391 9392 void ASTReader::diagnoseOdrViolations() { 9393 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9394 PendingFunctionOdrMergeFailures.empty() && 9395 PendingEnumOdrMergeFailures.empty()) 9396 return; 9397 9398 // Trigger the import of the full definition of each class that had any 9399 // odr-merging problems, so we can produce better diagnostics for them. 9400 // These updates may in turn find and diagnose some ODR failures, so take 9401 // ownership of the set first. 9402 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9403 PendingOdrMergeFailures.clear(); 9404 for (auto &Merge : OdrMergeFailures) { 9405 Merge.first->buildLookup(); 9406 Merge.first->decls_begin(); 9407 Merge.first->bases_begin(); 9408 Merge.first->vbases_begin(); 9409 for (auto &RecordPair : Merge.second) { 9410 auto *RD = RecordPair.first; 9411 RD->decls_begin(); 9412 RD->bases_begin(); 9413 RD->vbases_begin(); 9414 } 9415 } 9416 9417 // Trigger the import of functions. 9418 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9419 PendingFunctionOdrMergeFailures.clear(); 9420 for (auto &Merge : FunctionOdrMergeFailures) { 9421 Merge.first->buildLookup(); 9422 Merge.first->decls_begin(); 9423 Merge.first->getBody(); 9424 for (auto &FD : Merge.second) { 9425 FD->buildLookup(); 9426 FD->decls_begin(); 9427 FD->getBody(); 9428 } 9429 } 9430 9431 // Trigger the import of enums. 9432 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9433 PendingEnumOdrMergeFailures.clear(); 9434 for (auto &Merge : EnumOdrMergeFailures) { 9435 Merge.first->decls_begin(); 9436 for (auto &Enum : Merge.second) { 9437 Enum->decls_begin(); 9438 } 9439 } 9440 9441 // For each declaration from a merged context, check that the canonical 9442 // definition of that context also contains a declaration of the same 9443 // entity. 9444 // 9445 // Caution: this loop does things that might invalidate iterators into 9446 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9447 while (!PendingOdrMergeChecks.empty()) { 9448 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9449 9450 // FIXME: Skip over implicit declarations for now. This matters for things 9451 // like implicitly-declared special member functions. This isn't entirely 9452 // correct; we can end up with multiple unmerged declarations of the same 9453 // implicit entity. 9454 if (D->isImplicit()) 9455 continue; 9456 9457 DeclContext *CanonDef = D->getDeclContext(); 9458 9459 bool Found = false; 9460 const Decl *DCanon = D->getCanonicalDecl(); 9461 9462 for (auto RI : D->redecls()) { 9463 if (RI->getLexicalDeclContext() == CanonDef) { 9464 Found = true; 9465 break; 9466 } 9467 } 9468 if (Found) 9469 continue; 9470 9471 // Quick check failed, time to do the slow thing. Note, we can't just 9472 // look up the name of D in CanonDef here, because the member that is 9473 // in CanonDef might not be found by name lookup (it might have been 9474 // replaced by a more recent declaration in the lookup table), and we 9475 // can't necessarily find it in the redeclaration chain because it might 9476 // be merely mergeable, not redeclarable. 9477 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9478 for (auto *CanonMember : CanonDef->decls()) { 9479 if (CanonMember->getCanonicalDecl() == DCanon) { 9480 // This can happen if the declaration is merely mergeable and not 9481 // actually redeclarable (we looked for redeclarations earlier). 9482 // 9483 // FIXME: We should be able to detect this more efficiently, without 9484 // pulling in all of the members of CanonDef. 9485 Found = true; 9486 break; 9487 } 9488 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9489 if (ND->getDeclName() == D->getDeclName()) 9490 Candidates.push_back(ND); 9491 } 9492 9493 if (!Found) { 9494 // The AST doesn't like TagDecls becoming invalid after they've been 9495 // completed. We only really need to mark FieldDecls as invalid here. 9496 if (!isa<TagDecl>(D)) 9497 D->setInvalidDecl(); 9498 9499 // Ensure we don't accidentally recursively enter deserialization while 9500 // we're producing our diagnostic. 9501 Deserializing RecursionGuard(this); 9502 9503 std::string CanonDefModule = 9504 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9505 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9506 << D << getOwningModuleNameForDiagnostic(D) 9507 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9508 9509 if (Candidates.empty()) 9510 Diag(cast<Decl>(CanonDef)->getLocation(), 9511 diag::note_module_odr_violation_no_possible_decls) << D; 9512 else { 9513 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9514 Diag(Candidates[I]->getLocation(), 9515 diag::note_module_odr_violation_possible_decl) 9516 << Candidates[I]; 9517 } 9518 9519 DiagnosedOdrMergeFailures.insert(CanonDef); 9520 } 9521 } 9522 9523 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9524 EnumOdrMergeFailures.empty()) 9525 return; 9526 9527 // Ensure we don't accidentally recursively enter deserialization while 9528 // we're producing our diagnostics. 9529 Deserializing RecursionGuard(this); 9530 9531 // Common code for hashing helpers. 9532 ODRHash Hash; 9533 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9534 Hash.clear(); 9535 Hash.AddQualType(Ty); 9536 return Hash.CalculateHash(); 9537 }; 9538 9539 auto ComputeODRHash = [&Hash](const Stmt *S) { 9540 assert(S); 9541 Hash.clear(); 9542 Hash.AddStmt(S); 9543 return Hash.CalculateHash(); 9544 }; 9545 9546 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9547 assert(D); 9548 Hash.clear(); 9549 Hash.AddSubDecl(D); 9550 return Hash.CalculateHash(); 9551 }; 9552 9553 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9554 Hash.clear(); 9555 Hash.AddTemplateArgument(TA); 9556 return Hash.CalculateHash(); 9557 }; 9558 9559 auto ComputeTemplateParameterListODRHash = 9560 [&Hash](const TemplateParameterList *TPL) { 9561 assert(TPL); 9562 Hash.clear(); 9563 Hash.AddTemplateParameterList(TPL); 9564 return Hash.CalculateHash(); 9565 }; 9566 9567 // Used with err_module_odr_violation_mismatch_decl and 9568 // note_module_odr_violation_mismatch_decl 9569 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9570 enum ODRMismatchDecl { 9571 EndOfClass, 9572 PublicSpecifer, 9573 PrivateSpecifer, 9574 ProtectedSpecifer, 9575 StaticAssert, 9576 Field, 9577 CXXMethod, 9578 TypeAlias, 9579 TypeDef, 9580 Var, 9581 Friend, 9582 FunctionTemplate, 9583 Other 9584 }; 9585 9586 // Used with err_module_odr_violation_mismatch_decl_diff and 9587 // note_module_odr_violation_mismatch_decl_diff 9588 enum ODRMismatchDeclDifference { 9589 StaticAssertCondition, 9590 StaticAssertMessage, 9591 StaticAssertOnlyMessage, 9592 FieldName, 9593 FieldTypeName, 9594 FieldSingleBitField, 9595 FieldDifferentWidthBitField, 9596 FieldSingleMutable, 9597 FieldSingleInitializer, 9598 FieldDifferentInitializers, 9599 MethodName, 9600 MethodDeleted, 9601 MethodDefaulted, 9602 MethodVirtual, 9603 MethodStatic, 9604 MethodVolatile, 9605 MethodConst, 9606 MethodInline, 9607 MethodNumberParameters, 9608 MethodParameterType, 9609 MethodParameterName, 9610 MethodParameterSingleDefaultArgument, 9611 MethodParameterDifferentDefaultArgument, 9612 MethodNoTemplateArguments, 9613 MethodDifferentNumberTemplateArguments, 9614 MethodDifferentTemplateArgument, 9615 MethodSingleBody, 9616 MethodDifferentBody, 9617 TypedefName, 9618 TypedefType, 9619 VarName, 9620 VarType, 9621 VarSingleInitializer, 9622 VarDifferentInitializer, 9623 VarConstexpr, 9624 FriendTypeFunction, 9625 FriendType, 9626 FriendFunction, 9627 FunctionTemplateDifferentNumberParameters, 9628 FunctionTemplateParameterDifferentKind, 9629 FunctionTemplateParameterName, 9630 FunctionTemplateParameterSingleDefaultArgument, 9631 FunctionTemplateParameterDifferentDefaultArgument, 9632 FunctionTemplateParameterDifferentType, 9633 FunctionTemplatePackParameter, 9634 }; 9635 9636 // These lambdas have the common portions of the ODR diagnostics. This 9637 // has the same return as Diag(), so addition parameters can be passed 9638 // in with operator<< 9639 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9640 SourceLocation Loc, SourceRange Range, 9641 ODRMismatchDeclDifference DiffType) { 9642 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9643 << FirstRecord << FirstModule.empty() << FirstModule << Range 9644 << DiffType; 9645 }; 9646 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9647 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9648 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9649 << SecondModule << Range << DiffType; 9650 }; 9651 9652 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9653 &ComputeQualTypeODRHash, &ComputeODRHash]( 9654 NamedDecl *FirstRecord, StringRef FirstModule, 9655 StringRef SecondModule, FieldDecl *FirstField, 9656 FieldDecl *SecondField) { 9657 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9658 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9659 if (FirstII->getName() != SecondII->getName()) { 9660 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9661 FirstField->getSourceRange(), FieldName) 9662 << FirstII; 9663 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9664 SecondField->getSourceRange(), FieldName) 9665 << SecondII; 9666 9667 return true; 9668 } 9669 9670 assert(getContext().hasSameType(FirstField->getType(), 9671 SecondField->getType())); 9672 9673 QualType FirstType = FirstField->getType(); 9674 QualType SecondType = SecondField->getType(); 9675 if (ComputeQualTypeODRHash(FirstType) != 9676 ComputeQualTypeODRHash(SecondType)) { 9677 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9678 FirstField->getSourceRange(), FieldTypeName) 9679 << FirstII << FirstType; 9680 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9681 SecondField->getSourceRange(), FieldTypeName) 9682 << SecondII << SecondType; 9683 9684 return true; 9685 } 9686 9687 const bool IsFirstBitField = FirstField->isBitField(); 9688 const bool IsSecondBitField = SecondField->isBitField(); 9689 if (IsFirstBitField != IsSecondBitField) { 9690 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9691 FirstField->getSourceRange(), FieldSingleBitField) 9692 << FirstII << IsFirstBitField; 9693 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9694 SecondField->getSourceRange(), FieldSingleBitField) 9695 << SecondII << IsSecondBitField; 9696 return true; 9697 } 9698 9699 if (IsFirstBitField && IsSecondBitField) { 9700 unsigned FirstBitWidthHash = 9701 ComputeODRHash(FirstField->getBitWidth()); 9702 unsigned SecondBitWidthHash = 9703 ComputeODRHash(SecondField->getBitWidth()); 9704 if (FirstBitWidthHash != SecondBitWidthHash) { 9705 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9706 FirstField->getSourceRange(), 9707 FieldDifferentWidthBitField) 9708 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9709 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9710 SecondField->getSourceRange(), 9711 FieldDifferentWidthBitField) 9712 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9713 return true; 9714 } 9715 } 9716 9717 if (!PP.getLangOpts().CPlusPlus) 9718 return false; 9719 9720 const bool IsFirstMutable = FirstField->isMutable(); 9721 const bool IsSecondMutable = SecondField->isMutable(); 9722 if (IsFirstMutable != IsSecondMutable) { 9723 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9724 FirstField->getSourceRange(), FieldSingleMutable) 9725 << FirstII << IsFirstMutable; 9726 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9727 SecondField->getSourceRange(), FieldSingleMutable) 9728 << SecondII << IsSecondMutable; 9729 return true; 9730 } 9731 9732 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9733 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9734 if ((!FirstInitializer && SecondInitializer) || 9735 (FirstInitializer && !SecondInitializer)) { 9736 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9737 FirstField->getSourceRange(), FieldSingleInitializer) 9738 << FirstII << (FirstInitializer != nullptr); 9739 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9740 SecondField->getSourceRange(), FieldSingleInitializer) 9741 << SecondII << (SecondInitializer != nullptr); 9742 return true; 9743 } 9744 9745 if (FirstInitializer && SecondInitializer) { 9746 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9747 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9748 if (FirstInitHash != SecondInitHash) { 9749 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9750 FirstField->getSourceRange(), 9751 FieldDifferentInitializers) 9752 << FirstII << FirstInitializer->getSourceRange(); 9753 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9754 SecondField->getSourceRange(), 9755 FieldDifferentInitializers) 9756 << SecondII << SecondInitializer->getSourceRange(); 9757 return true; 9758 } 9759 } 9760 9761 return false; 9762 }; 9763 9764 auto ODRDiagTypeDefOrAlias = 9765 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9766 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9767 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9768 bool IsTypeAlias) { 9769 auto FirstName = FirstTD->getDeclName(); 9770 auto SecondName = SecondTD->getDeclName(); 9771 if (FirstName != SecondName) { 9772 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9773 FirstTD->getSourceRange(), TypedefName) 9774 << IsTypeAlias << FirstName; 9775 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9776 SecondTD->getSourceRange(), TypedefName) 9777 << IsTypeAlias << SecondName; 9778 return true; 9779 } 9780 9781 QualType FirstType = FirstTD->getUnderlyingType(); 9782 QualType SecondType = SecondTD->getUnderlyingType(); 9783 if (ComputeQualTypeODRHash(FirstType) != 9784 ComputeQualTypeODRHash(SecondType)) { 9785 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9786 FirstTD->getSourceRange(), TypedefType) 9787 << IsTypeAlias << FirstName << FirstType; 9788 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9789 SecondTD->getSourceRange(), TypedefType) 9790 << IsTypeAlias << SecondName << SecondType; 9791 return true; 9792 } 9793 9794 return false; 9795 }; 9796 9797 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9798 &ComputeQualTypeODRHash, &ComputeODRHash, 9799 this](NamedDecl *FirstRecord, StringRef FirstModule, 9800 StringRef SecondModule, VarDecl *FirstVD, 9801 VarDecl *SecondVD) { 9802 auto FirstName = FirstVD->getDeclName(); 9803 auto SecondName = SecondVD->getDeclName(); 9804 if (FirstName != SecondName) { 9805 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9806 FirstVD->getSourceRange(), VarName) 9807 << FirstName; 9808 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9809 SecondVD->getSourceRange(), VarName) 9810 << SecondName; 9811 return true; 9812 } 9813 9814 QualType FirstType = FirstVD->getType(); 9815 QualType SecondType = SecondVD->getType(); 9816 if (ComputeQualTypeODRHash(FirstType) != 9817 ComputeQualTypeODRHash(SecondType)) { 9818 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9819 FirstVD->getSourceRange(), VarType) 9820 << FirstName << FirstType; 9821 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9822 SecondVD->getSourceRange(), VarType) 9823 << SecondName << SecondType; 9824 return true; 9825 } 9826 9827 if (!PP.getLangOpts().CPlusPlus) 9828 return false; 9829 9830 const Expr *FirstInit = FirstVD->getInit(); 9831 const Expr *SecondInit = SecondVD->getInit(); 9832 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9833 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9834 FirstVD->getSourceRange(), VarSingleInitializer) 9835 << FirstName << (FirstInit == nullptr) 9836 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9837 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9838 SecondVD->getSourceRange(), VarSingleInitializer) 9839 << SecondName << (SecondInit == nullptr) 9840 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9841 return true; 9842 } 9843 9844 if (FirstInit && SecondInit && 9845 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9846 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9847 FirstVD->getSourceRange(), VarDifferentInitializer) 9848 << FirstName << FirstInit->getSourceRange(); 9849 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9850 SecondVD->getSourceRange(), VarDifferentInitializer) 9851 << SecondName << SecondInit->getSourceRange(); 9852 return true; 9853 } 9854 9855 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9856 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9857 if (FirstIsConstexpr != SecondIsConstexpr) { 9858 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9859 FirstVD->getSourceRange(), VarConstexpr) 9860 << FirstName << FirstIsConstexpr; 9861 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9862 SecondVD->getSourceRange(), VarConstexpr) 9863 << SecondName << SecondIsConstexpr; 9864 return true; 9865 } 9866 return false; 9867 }; 9868 9869 auto DifferenceSelector = [](Decl *D) { 9870 assert(D && "valid Decl required"); 9871 switch (D->getKind()) { 9872 default: 9873 return Other; 9874 case Decl::AccessSpec: 9875 switch (D->getAccess()) { 9876 case AS_public: 9877 return PublicSpecifer; 9878 case AS_private: 9879 return PrivateSpecifer; 9880 case AS_protected: 9881 return ProtectedSpecifer; 9882 case AS_none: 9883 break; 9884 } 9885 llvm_unreachable("Invalid access specifier"); 9886 case Decl::StaticAssert: 9887 return StaticAssert; 9888 case Decl::Field: 9889 return Field; 9890 case Decl::CXXMethod: 9891 case Decl::CXXConstructor: 9892 case Decl::CXXDestructor: 9893 return CXXMethod; 9894 case Decl::TypeAlias: 9895 return TypeAlias; 9896 case Decl::Typedef: 9897 return TypeDef; 9898 case Decl::Var: 9899 return Var; 9900 case Decl::Friend: 9901 return Friend; 9902 case Decl::FunctionTemplate: 9903 return FunctionTemplate; 9904 } 9905 }; 9906 9907 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9908 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9909 RecordDecl *Record, 9910 const DeclContext *DC) { 9911 for (auto *D : Record->decls()) { 9912 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9913 continue; 9914 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9915 } 9916 }; 9917 9918 struct DiffResult { 9919 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9920 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9921 }; 9922 9923 // If there is a diagnoseable difference, FirstDiffType and 9924 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9925 // filled in if not EndOfClass. 9926 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9927 DeclHashes &SecondHashes) { 9928 DiffResult DR; 9929 auto FirstIt = FirstHashes.begin(); 9930 auto SecondIt = SecondHashes.begin(); 9931 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9932 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9933 FirstIt->second == SecondIt->second) { 9934 ++FirstIt; 9935 ++SecondIt; 9936 continue; 9937 } 9938 9939 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9940 DR.SecondDecl = 9941 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9942 9943 DR.FirstDiffType = 9944 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9945 DR.SecondDiffType = 9946 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9947 return DR; 9948 } 9949 return DR; 9950 }; 9951 9952 // Use this to diagnose that an unexpected Decl was encountered 9953 // or no difference was detected. This causes a generic error 9954 // message to be emitted. 9955 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9956 StringRef FirstModule, 9957 NamedDecl *SecondRecord, 9958 StringRef SecondModule) { 9959 Diag(FirstRecord->getLocation(), 9960 diag::err_module_odr_violation_different_definitions) 9961 << FirstRecord << FirstModule.empty() << FirstModule; 9962 9963 if (DR.FirstDecl) { 9964 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9965 << FirstRecord << DR.FirstDecl->getSourceRange(); 9966 } 9967 9968 Diag(SecondRecord->getLocation(), 9969 diag::note_module_odr_violation_different_definitions) 9970 << SecondModule; 9971 9972 if (DR.SecondDecl) { 9973 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9974 << DR.SecondDecl->getSourceRange(); 9975 } 9976 }; 9977 9978 auto DiagnoseODRMismatch = 9979 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9980 NamedDecl *SecondRecord, StringRef SecondModule) { 9981 SourceLocation FirstLoc; 9982 SourceRange FirstRange; 9983 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9984 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9985 FirstLoc = FirstTag->getBraceRange().getEnd(); 9986 } else { 9987 FirstLoc = DR.FirstDecl->getLocation(); 9988 FirstRange = DR.FirstDecl->getSourceRange(); 9989 } 9990 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 9991 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 9992 << DR.FirstDiffType; 9993 9994 SourceLocation SecondLoc; 9995 SourceRange SecondRange; 9996 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 9997 if (DR.SecondDiffType == EndOfClass && SecondTag) { 9998 SecondLoc = SecondTag->getBraceRange().getEnd(); 9999 } else { 10000 SecondLoc = DR.SecondDecl->getLocation(); 10001 SecondRange = DR.SecondDecl->getSourceRange(); 10002 } 10003 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 10004 << SecondModule << SecondRange << DR.SecondDiffType; 10005 }; 10006 10007 // Issue any pending ODR-failure diagnostics. 10008 for (auto &Merge : OdrMergeFailures) { 10009 // If we've already pointed out a specific problem with this class, don't 10010 // bother issuing a general "something's different" diagnostic. 10011 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 10012 continue; 10013 10014 bool Diagnosed = false; 10015 CXXRecordDecl *FirstRecord = Merge.first; 10016 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 10017 for (auto &RecordPair : Merge.second) { 10018 CXXRecordDecl *SecondRecord = RecordPair.first; 10019 // Multiple different declarations got merged together; tell the user 10020 // where they came from. 10021 if (FirstRecord == SecondRecord) 10022 continue; 10023 10024 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10025 10026 auto *FirstDD = FirstRecord->DefinitionData; 10027 auto *SecondDD = RecordPair.second; 10028 10029 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10030 10031 // Diagnostics from DefinitionData are emitted here. 10032 if (FirstDD != SecondDD) { 10033 enum ODRDefinitionDataDifference { 10034 NumBases, 10035 NumVBases, 10036 BaseType, 10037 BaseVirtual, 10038 BaseAccess, 10039 }; 10040 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10041 this](SourceLocation Loc, SourceRange Range, 10042 ODRDefinitionDataDifference DiffType) { 10043 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10044 << FirstRecord << FirstModule.empty() << FirstModule << Range 10045 << DiffType; 10046 }; 10047 auto ODRDiagBaseNote = [&SecondModule, 10048 this](SourceLocation Loc, SourceRange Range, 10049 ODRDefinitionDataDifference DiffType) { 10050 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10051 << SecondModule << Range << DiffType; 10052 }; 10053 10054 unsigned FirstNumBases = FirstDD->NumBases; 10055 unsigned FirstNumVBases = FirstDD->NumVBases; 10056 unsigned SecondNumBases = SecondDD->NumBases; 10057 unsigned SecondNumVBases = SecondDD->NumVBases; 10058 10059 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10060 unsigned NumBases = DD->NumBases; 10061 if (NumBases == 0) return SourceRange(); 10062 auto bases = DD->bases(); 10063 return SourceRange(bases[0].getBeginLoc(), 10064 bases[NumBases - 1].getEndLoc()); 10065 }; 10066 10067 if (FirstNumBases != SecondNumBases) { 10068 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10069 NumBases) 10070 << FirstNumBases; 10071 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10072 NumBases) 10073 << SecondNumBases; 10074 Diagnosed = true; 10075 break; 10076 } 10077 10078 if (FirstNumVBases != SecondNumVBases) { 10079 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10080 NumVBases) 10081 << FirstNumVBases; 10082 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10083 NumVBases) 10084 << SecondNumVBases; 10085 Diagnosed = true; 10086 break; 10087 } 10088 10089 auto FirstBases = FirstDD->bases(); 10090 auto SecondBases = SecondDD->bases(); 10091 unsigned i = 0; 10092 for (i = 0; i < FirstNumBases; ++i) { 10093 auto FirstBase = FirstBases[i]; 10094 auto SecondBase = SecondBases[i]; 10095 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10096 ComputeQualTypeODRHash(SecondBase.getType())) { 10097 ODRDiagBaseError(FirstRecord->getLocation(), 10098 FirstBase.getSourceRange(), BaseType) 10099 << (i + 1) << FirstBase.getType(); 10100 ODRDiagBaseNote(SecondRecord->getLocation(), 10101 SecondBase.getSourceRange(), BaseType) 10102 << (i + 1) << SecondBase.getType(); 10103 break; 10104 } 10105 10106 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10107 ODRDiagBaseError(FirstRecord->getLocation(), 10108 FirstBase.getSourceRange(), BaseVirtual) 10109 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10110 ODRDiagBaseNote(SecondRecord->getLocation(), 10111 SecondBase.getSourceRange(), BaseVirtual) 10112 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10113 break; 10114 } 10115 10116 if (FirstBase.getAccessSpecifierAsWritten() != 10117 SecondBase.getAccessSpecifierAsWritten()) { 10118 ODRDiagBaseError(FirstRecord->getLocation(), 10119 FirstBase.getSourceRange(), BaseAccess) 10120 << (i + 1) << FirstBase.getType() 10121 << (int)FirstBase.getAccessSpecifierAsWritten(); 10122 ODRDiagBaseNote(SecondRecord->getLocation(), 10123 SecondBase.getSourceRange(), BaseAccess) 10124 << (i + 1) << SecondBase.getType() 10125 << (int)SecondBase.getAccessSpecifierAsWritten(); 10126 break; 10127 } 10128 } 10129 10130 if (i != FirstNumBases) { 10131 Diagnosed = true; 10132 break; 10133 } 10134 } 10135 10136 const ClassTemplateDecl *FirstTemplate = 10137 FirstRecord->getDescribedClassTemplate(); 10138 const ClassTemplateDecl *SecondTemplate = 10139 SecondRecord->getDescribedClassTemplate(); 10140 10141 assert(!FirstTemplate == !SecondTemplate && 10142 "Both pointers should be null or non-null"); 10143 10144 if (FirstTemplate && SecondTemplate) { 10145 DeclHashes FirstTemplateHashes; 10146 DeclHashes SecondTemplateHashes; 10147 10148 auto PopulateTemplateParameterHashs = 10149 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10150 const ClassTemplateDecl *TD) { 10151 for (auto *D : TD->getTemplateParameters()->asArray()) { 10152 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10153 } 10154 }; 10155 10156 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10157 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10158 10159 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10160 "Number of template parameters should be equal."); 10161 10162 auto FirstIt = FirstTemplateHashes.begin(); 10163 auto FirstEnd = FirstTemplateHashes.end(); 10164 auto SecondIt = SecondTemplateHashes.begin(); 10165 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10166 if (FirstIt->second == SecondIt->second) 10167 continue; 10168 10169 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10170 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10171 10172 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10173 "Parameter Decl's should be the same kind."); 10174 10175 enum ODRTemplateDifference { 10176 ParamEmptyName, 10177 ParamName, 10178 ParamSingleDefaultArgument, 10179 ParamDifferentDefaultArgument, 10180 }; 10181 10182 auto hasDefaultArg = [](const NamedDecl *D) { 10183 if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 10184 return TTP->hasDefaultArgument() && 10185 !TTP->defaultArgumentWasInherited(); 10186 if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 10187 return NTTP->hasDefaultArgument() && 10188 !NTTP->defaultArgumentWasInherited(); 10189 auto *TTP = cast<TemplateTemplateParmDecl>(D); 10190 return TTP->hasDefaultArgument() && 10191 !TTP->defaultArgumentWasInherited(); 10192 }; 10193 bool hasFirstArg = hasDefaultArg(FirstDecl); 10194 bool hasSecondArg = hasDefaultArg(SecondDecl); 10195 10196 ODRTemplateDifference ErrDiffType; 10197 ODRTemplateDifference NoteDiffType; 10198 10199 DeclarationName FirstName = FirstDecl->getDeclName(); 10200 DeclarationName SecondName = SecondDecl->getDeclName(); 10201 10202 if (FirstName != SecondName) { 10203 bool FirstNameEmpty = 10204 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10205 bool SecondNameEmpty = SecondName.isIdentifier() && 10206 !SecondName.getAsIdentifierInfo(); 10207 ErrDiffType = FirstNameEmpty ? ParamEmptyName : ParamName; 10208 NoteDiffType = SecondNameEmpty ? ParamEmptyName : ParamName; 10209 } else if (hasFirstArg == hasSecondArg) 10210 ErrDiffType = NoteDiffType = ParamDifferentDefaultArgument; 10211 else 10212 ErrDiffType = NoteDiffType = ParamSingleDefaultArgument; 10213 10214 Diag(FirstDecl->getLocation(), 10215 diag::err_module_odr_violation_template_parameter) 10216 << FirstRecord << FirstModule.empty() << FirstModule 10217 << FirstDecl->getSourceRange() << ErrDiffType << hasFirstArg 10218 << FirstName; 10219 Diag(SecondDecl->getLocation(), 10220 diag::note_module_odr_violation_template_parameter) 10221 << SecondModule << SecondDecl->getSourceRange() << NoteDiffType 10222 << hasSecondArg << SecondName; 10223 break; 10224 } 10225 10226 if (FirstIt != FirstEnd) { 10227 Diagnosed = true; 10228 break; 10229 } 10230 } 10231 10232 DeclHashes FirstHashes; 10233 DeclHashes SecondHashes; 10234 const DeclContext *DC = FirstRecord; 10235 PopulateHashes(FirstHashes, FirstRecord, DC); 10236 PopulateHashes(SecondHashes, SecondRecord, DC); 10237 10238 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10239 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10240 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10241 Decl *FirstDecl = DR.FirstDecl; 10242 Decl *SecondDecl = DR.SecondDecl; 10243 10244 if (FirstDiffType == Other || SecondDiffType == Other) { 10245 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10246 SecondModule); 10247 Diagnosed = true; 10248 break; 10249 } 10250 10251 if (FirstDiffType != SecondDiffType) { 10252 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10253 SecondModule); 10254 Diagnosed = true; 10255 break; 10256 } 10257 10258 assert(FirstDiffType == SecondDiffType); 10259 10260 switch (FirstDiffType) { 10261 case Other: 10262 case EndOfClass: 10263 case PublicSpecifer: 10264 case PrivateSpecifer: 10265 case ProtectedSpecifer: 10266 llvm_unreachable("Invalid diff type"); 10267 10268 case StaticAssert: { 10269 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10270 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10271 10272 Expr *FirstExpr = FirstSA->getAssertExpr(); 10273 Expr *SecondExpr = SecondSA->getAssertExpr(); 10274 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10275 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10276 if (FirstODRHash != SecondODRHash) { 10277 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10278 FirstExpr->getSourceRange(), StaticAssertCondition); 10279 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10280 SecondExpr->getSourceRange(), StaticAssertCondition); 10281 Diagnosed = true; 10282 break; 10283 } 10284 10285 StringLiteral *FirstStr = FirstSA->getMessage(); 10286 StringLiteral *SecondStr = SecondSA->getMessage(); 10287 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10288 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10289 SourceLocation FirstLoc, SecondLoc; 10290 SourceRange FirstRange, SecondRange; 10291 if (FirstStr) { 10292 FirstLoc = FirstStr->getBeginLoc(); 10293 FirstRange = FirstStr->getSourceRange(); 10294 } else { 10295 FirstLoc = FirstSA->getBeginLoc(); 10296 FirstRange = FirstSA->getSourceRange(); 10297 } 10298 if (SecondStr) { 10299 SecondLoc = SecondStr->getBeginLoc(); 10300 SecondRange = SecondStr->getSourceRange(); 10301 } else { 10302 SecondLoc = SecondSA->getBeginLoc(); 10303 SecondRange = SecondSA->getSourceRange(); 10304 } 10305 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10306 StaticAssertOnlyMessage) 10307 << (FirstStr == nullptr); 10308 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10309 StaticAssertOnlyMessage) 10310 << (SecondStr == nullptr); 10311 Diagnosed = true; 10312 break; 10313 } 10314 10315 if (FirstStr && SecondStr && 10316 FirstStr->getString() != SecondStr->getString()) { 10317 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10318 FirstStr->getSourceRange(), StaticAssertMessage); 10319 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10320 SecondStr->getSourceRange(), StaticAssertMessage); 10321 Diagnosed = true; 10322 break; 10323 } 10324 break; 10325 } 10326 case Field: { 10327 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10328 cast<FieldDecl>(FirstDecl), 10329 cast<FieldDecl>(SecondDecl)); 10330 break; 10331 } 10332 case CXXMethod: { 10333 enum { 10334 DiagMethod, 10335 DiagConstructor, 10336 DiagDestructor, 10337 } FirstMethodType, 10338 SecondMethodType; 10339 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10340 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10341 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10342 return DiagMethod; 10343 }; 10344 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10345 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10346 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10347 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10348 auto FirstName = FirstMethod->getDeclName(); 10349 auto SecondName = SecondMethod->getDeclName(); 10350 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10351 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10352 FirstMethod->getSourceRange(), MethodName) 10353 << FirstMethodType << FirstName; 10354 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10355 SecondMethod->getSourceRange(), MethodName) 10356 << SecondMethodType << SecondName; 10357 10358 Diagnosed = true; 10359 break; 10360 } 10361 10362 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10363 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10364 if (FirstDeleted != SecondDeleted) { 10365 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10366 FirstMethod->getSourceRange(), MethodDeleted) 10367 << FirstMethodType << FirstName << FirstDeleted; 10368 10369 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10370 SecondMethod->getSourceRange(), MethodDeleted) 10371 << SecondMethodType << SecondName << SecondDeleted; 10372 Diagnosed = true; 10373 break; 10374 } 10375 10376 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10377 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10378 if (FirstDefaulted != SecondDefaulted) { 10379 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10380 FirstMethod->getSourceRange(), MethodDefaulted) 10381 << FirstMethodType << FirstName << FirstDefaulted; 10382 10383 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10384 SecondMethod->getSourceRange(), MethodDefaulted) 10385 << SecondMethodType << SecondName << SecondDefaulted; 10386 Diagnosed = true; 10387 break; 10388 } 10389 10390 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10391 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10392 const bool FirstPure = FirstMethod->isPure(); 10393 const bool SecondPure = SecondMethod->isPure(); 10394 if ((FirstVirtual || SecondVirtual) && 10395 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10396 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10397 FirstMethod->getSourceRange(), MethodVirtual) 10398 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10399 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10400 SecondMethod->getSourceRange(), MethodVirtual) 10401 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10402 Diagnosed = true; 10403 break; 10404 } 10405 10406 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10407 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10408 // class needs to be checked instead. 10409 const auto FirstStorage = FirstMethod->getStorageClass(); 10410 const auto SecondStorage = SecondMethod->getStorageClass(); 10411 const bool FirstStatic = FirstStorage == SC_Static; 10412 const bool SecondStatic = SecondStorage == SC_Static; 10413 if (FirstStatic != SecondStatic) { 10414 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10415 FirstMethod->getSourceRange(), MethodStatic) 10416 << FirstMethodType << FirstName << FirstStatic; 10417 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10418 SecondMethod->getSourceRange(), MethodStatic) 10419 << SecondMethodType << SecondName << SecondStatic; 10420 Diagnosed = true; 10421 break; 10422 } 10423 10424 const bool FirstVolatile = FirstMethod->isVolatile(); 10425 const bool SecondVolatile = SecondMethod->isVolatile(); 10426 if (FirstVolatile != SecondVolatile) { 10427 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10428 FirstMethod->getSourceRange(), MethodVolatile) 10429 << FirstMethodType << FirstName << FirstVolatile; 10430 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10431 SecondMethod->getSourceRange(), MethodVolatile) 10432 << SecondMethodType << SecondName << SecondVolatile; 10433 Diagnosed = true; 10434 break; 10435 } 10436 10437 const bool FirstConst = FirstMethod->isConst(); 10438 const bool SecondConst = SecondMethod->isConst(); 10439 if (FirstConst != SecondConst) { 10440 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10441 FirstMethod->getSourceRange(), MethodConst) 10442 << FirstMethodType << FirstName << FirstConst; 10443 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10444 SecondMethod->getSourceRange(), MethodConst) 10445 << SecondMethodType << SecondName << SecondConst; 10446 Diagnosed = true; 10447 break; 10448 } 10449 10450 const bool FirstInline = FirstMethod->isInlineSpecified(); 10451 const bool SecondInline = SecondMethod->isInlineSpecified(); 10452 if (FirstInline != SecondInline) { 10453 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10454 FirstMethod->getSourceRange(), MethodInline) 10455 << FirstMethodType << FirstName << FirstInline; 10456 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10457 SecondMethod->getSourceRange(), MethodInline) 10458 << SecondMethodType << SecondName << SecondInline; 10459 Diagnosed = true; 10460 break; 10461 } 10462 10463 const unsigned FirstNumParameters = FirstMethod->param_size(); 10464 const unsigned SecondNumParameters = SecondMethod->param_size(); 10465 if (FirstNumParameters != SecondNumParameters) { 10466 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10467 FirstMethod->getSourceRange(), 10468 MethodNumberParameters) 10469 << FirstMethodType << FirstName << FirstNumParameters; 10470 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10471 SecondMethod->getSourceRange(), 10472 MethodNumberParameters) 10473 << SecondMethodType << SecondName << SecondNumParameters; 10474 Diagnosed = true; 10475 break; 10476 } 10477 10478 // Need this status boolean to know when break out of the switch. 10479 bool ParameterMismatch = false; 10480 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10481 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10482 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10483 10484 QualType FirstParamType = FirstParam->getType(); 10485 QualType SecondParamType = SecondParam->getType(); 10486 if (FirstParamType != SecondParamType && 10487 ComputeQualTypeODRHash(FirstParamType) != 10488 ComputeQualTypeODRHash(SecondParamType)) { 10489 if (const DecayedType *ParamDecayedType = 10490 FirstParamType->getAs<DecayedType>()) { 10491 ODRDiagDeclError( 10492 FirstRecord, FirstModule, FirstMethod->getLocation(), 10493 FirstMethod->getSourceRange(), MethodParameterType) 10494 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10495 << true << ParamDecayedType->getOriginalType(); 10496 } else { 10497 ODRDiagDeclError( 10498 FirstRecord, FirstModule, FirstMethod->getLocation(), 10499 FirstMethod->getSourceRange(), MethodParameterType) 10500 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10501 << false; 10502 } 10503 10504 if (const DecayedType *ParamDecayedType = 10505 SecondParamType->getAs<DecayedType>()) { 10506 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10507 SecondMethod->getSourceRange(), 10508 MethodParameterType) 10509 << SecondMethodType << SecondName << (I + 1) 10510 << SecondParamType << true 10511 << ParamDecayedType->getOriginalType(); 10512 } else { 10513 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10514 SecondMethod->getSourceRange(), 10515 MethodParameterType) 10516 << SecondMethodType << SecondName << (I + 1) 10517 << SecondParamType << false; 10518 } 10519 ParameterMismatch = true; 10520 break; 10521 } 10522 10523 DeclarationName FirstParamName = FirstParam->getDeclName(); 10524 DeclarationName SecondParamName = SecondParam->getDeclName(); 10525 if (FirstParamName != SecondParamName) { 10526 ODRDiagDeclError(FirstRecord, FirstModule, 10527 FirstMethod->getLocation(), 10528 FirstMethod->getSourceRange(), MethodParameterName) 10529 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10530 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10531 SecondMethod->getSourceRange(), MethodParameterName) 10532 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10533 ParameterMismatch = true; 10534 break; 10535 } 10536 10537 const Expr *FirstInit = FirstParam->getInit(); 10538 const Expr *SecondInit = SecondParam->getInit(); 10539 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10540 ODRDiagDeclError(FirstRecord, FirstModule, 10541 FirstMethod->getLocation(), 10542 FirstMethod->getSourceRange(), 10543 MethodParameterSingleDefaultArgument) 10544 << FirstMethodType << FirstName << (I + 1) 10545 << (FirstInit == nullptr) 10546 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10547 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10548 SecondMethod->getSourceRange(), 10549 MethodParameterSingleDefaultArgument) 10550 << SecondMethodType << SecondName << (I + 1) 10551 << (SecondInit == nullptr) 10552 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10553 ParameterMismatch = true; 10554 break; 10555 } 10556 10557 if (FirstInit && SecondInit && 10558 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10559 ODRDiagDeclError(FirstRecord, FirstModule, 10560 FirstMethod->getLocation(), 10561 FirstMethod->getSourceRange(), 10562 MethodParameterDifferentDefaultArgument) 10563 << FirstMethodType << FirstName << (I + 1) 10564 << FirstInit->getSourceRange(); 10565 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10566 SecondMethod->getSourceRange(), 10567 MethodParameterDifferentDefaultArgument) 10568 << SecondMethodType << SecondName << (I + 1) 10569 << SecondInit->getSourceRange(); 10570 ParameterMismatch = true; 10571 break; 10572 10573 } 10574 } 10575 10576 if (ParameterMismatch) { 10577 Diagnosed = true; 10578 break; 10579 } 10580 10581 const auto *FirstTemplateArgs = 10582 FirstMethod->getTemplateSpecializationArgs(); 10583 const auto *SecondTemplateArgs = 10584 SecondMethod->getTemplateSpecializationArgs(); 10585 10586 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10587 (!FirstTemplateArgs && SecondTemplateArgs)) { 10588 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10589 FirstMethod->getSourceRange(), 10590 MethodNoTemplateArguments) 10591 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10592 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10593 SecondMethod->getSourceRange(), 10594 MethodNoTemplateArguments) 10595 << SecondMethodType << SecondName 10596 << (SecondTemplateArgs != nullptr); 10597 10598 Diagnosed = true; 10599 break; 10600 } 10601 10602 if (FirstTemplateArgs && SecondTemplateArgs) { 10603 // Remove pack expansions from argument list. 10604 auto ExpandTemplateArgumentList = 10605 [](const TemplateArgumentList *TAL) { 10606 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10607 for (const TemplateArgument &TA : TAL->asArray()) { 10608 if (TA.getKind() != TemplateArgument::Pack) { 10609 ExpandedList.push_back(&TA); 10610 continue; 10611 } 10612 llvm::append_range(ExpandedList, llvm::make_pointer_range( 10613 TA.getPackAsArray())); 10614 } 10615 return ExpandedList; 10616 }; 10617 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10618 ExpandTemplateArgumentList(FirstTemplateArgs); 10619 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10620 ExpandTemplateArgumentList(SecondTemplateArgs); 10621 10622 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10623 ODRDiagDeclError(FirstRecord, FirstModule, 10624 FirstMethod->getLocation(), 10625 FirstMethod->getSourceRange(), 10626 MethodDifferentNumberTemplateArguments) 10627 << FirstMethodType << FirstName 10628 << (unsigned)FirstExpandedList.size(); 10629 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10630 SecondMethod->getSourceRange(), 10631 MethodDifferentNumberTemplateArguments) 10632 << SecondMethodType << SecondName 10633 << (unsigned)SecondExpandedList.size(); 10634 10635 Diagnosed = true; 10636 break; 10637 } 10638 10639 bool TemplateArgumentMismatch = false; 10640 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10641 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10642 &SecondTA = *SecondExpandedList[i]; 10643 if (ComputeTemplateArgumentODRHash(FirstTA) == 10644 ComputeTemplateArgumentODRHash(SecondTA)) { 10645 continue; 10646 } 10647 10648 ODRDiagDeclError( 10649 FirstRecord, FirstModule, FirstMethod->getLocation(), 10650 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10651 << FirstMethodType << FirstName << FirstTA << i + 1; 10652 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10653 SecondMethod->getSourceRange(), 10654 MethodDifferentTemplateArgument) 10655 << SecondMethodType << SecondName << SecondTA << i + 1; 10656 10657 TemplateArgumentMismatch = true; 10658 break; 10659 } 10660 10661 if (TemplateArgumentMismatch) { 10662 Diagnosed = true; 10663 break; 10664 } 10665 } 10666 10667 // Compute the hash of the method as if it has no body. 10668 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10669 Hash.clear(); 10670 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10671 return Hash.CalculateHash(); 10672 }; 10673 10674 // Compare the hash generated to the hash stored. A difference means 10675 // that a body was present in the original source. Due to merging, 10676 // the stardard way of detecting a body will not work. 10677 const bool HasFirstBody = 10678 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10679 const bool HasSecondBody = 10680 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10681 10682 if (HasFirstBody != HasSecondBody) { 10683 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10684 FirstMethod->getSourceRange(), MethodSingleBody) 10685 << FirstMethodType << FirstName << HasFirstBody; 10686 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10687 SecondMethod->getSourceRange(), MethodSingleBody) 10688 << SecondMethodType << SecondName << HasSecondBody; 10689 Diagnosed = true; 10690 break; 10691 } 10692 10693 if (HasFirstBody && HasSecondBody) { 10694 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10695 FirstMethod->getSourceRange(), MethodDifferentBody) 10696 << FirstMethodType << FirstName; 10697 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10698 SecondMethod->getSourceRange(), MethodDifferentBody) 10699 << SecondMethodType << SecondName; 10700 Diagnosed = true; 10701 break; 10702 } 10703 10704 break; 10705 } 10706 case TypeAlias: 10707 case TypeDef: { 10708 Diagnosed = ODRDiagTypeDefOrAlias( 10709 FirstRecord, FirstModule, SecondModule, 10710 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10711 FirstDiffType == TypeAlias); 10712 break; 10713 } 10714 case Var: { 10715 Diagnosed = 10716 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10717 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10718 break; 10719 } 10720 case Friend: { 10721 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10722 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10723 10724 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10725 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10726 10727 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10728 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10729 10730 if (FirstND && SecondND) { 10731 ODRDiagDeclError(FirstRecord, FirstModule, 10732 FirstFriend->getFriendLoc(), 10733 FirstFriend->getSourceRange(), FriendFunction) 10734 << FirstND; 10735 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10736 SecondFriend->getSourceRange(), FriendFunction) 10737 << SecondND; 10738 10739 Diagnosed = true; 10740 break; 10741 } 10742 10743 if (FirstTSI && SecondTSI) { 10744 QualType FirstFriendType = FirstTSI->getType(); 10745 QualType SecondFriendType = SecondTSI->getType(); 10746 assert(ComputeQualTypeODRHash(FirstFriendType) != 10747 ComputeQualTypeODRHash(SecondFriendType)); 10748 ODRDiagDeclError(FirstRecord, FirstModule, 10749 FirstFriend->getFriendLoc(), 10750 FirstFriend->getSourceRange(), FriendType) 10751 << FirstFriendType; 10752 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10753 SecondFriend->getSourceRange(), FriendType) 10754 << SecondFriendType; 10755 Diagnosed = true; 10756 break; 10757 } 10758 10759 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10760 FirstFriend->getSourceRange(), FriendTypeFunction) 10761 << (FirstTSI == nullptr); 10762 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10763 SecondFriend->getSourceRange(), FriendTypeFunction) 10764 << (SecondTSI == nullptr); 10765 10766 Diagnosed = true; 10767 break; 10768 } 10769 case FunctionTemplate: { 10770 FunctionTemplateDecl *FirstTemplate = 10771 cast<FunctionTemplateDecl>(FirstDecl); 10772 FunctionTemplateDecl *SecondTemplate = 10773 cast<FunctionTemplateDecl>(SecondDecl); 10774 10775 TemplateParameterList *FirstTPL = 10776 FirstTemplate->getTemplateParameters(); 10777 TemplateParameterList *SecondTPL = 10778 SecondTemplate->getTemplateParameters(); 10779 10780 if (FirstTPL->size() != SecondTPL->size()) { 10781 ODRDiagDeclError(FirstRecord, FirstModule, 10782 FirstTemplate->getLocation(), 10783 FirstTemplate->getSourceRange(), 10784 FunctionTemplateDifferentNumberParameters) 10785 << FirstTemplate << FirstTPL->size(); 10786 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10787 SecondTemplate->getSourceRange(), 10788 FunctionTemplateDifferentNumberParameters) 10789 << SecondTemplate << SecondTPL->size(); 10790 10791 Diagnosed = true; 10792 break; 10793 } 10794 10795 bool ParameterMismatch = false; 10796 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10797 NamedDecl *FirstParam = FirstTPL->getParam(i); 10798 NamedDecl *SecondParam = SecondTPL->getParam(i); 10799 10800 if (FirstParam->getKind() != SecondParam->getKind()) { 10801 enum { 10802 TemplateTypeParameter, 10803 NonTypeTemplateParameter, 10804 TemplateTemplateParameter, 10805 }; 10806 auto GetParamType = [](NamedDecl *D) { 10807 switch (D->getKind()) { 10808 default: 10809 llvm_unreachable("Unexpected template parameter type"); 10810 case Decl::TemplateTypeParm: 10811 return TemplateTypeParameter; 10812 case Decl::NonTypeTemplateParm: 10813 return NonTypeTemplateParameter; 10814 case Decl::TemplateTemplateParm: 10815 return TemplateTemplateParameter; 10816 } 10817 }; 10818 10819 ODRDiagDeclError(FirstRecord, FirstModule, 10820 FirstTemplate->getLocation(), 10821 FirstTemplate->getSourceRange(), 10822 FunctionTemplateParameterDifferentKind) 10823 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10824 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10825 SecondTemplate->getSourceRange(), 10826 FunctionTemplateParameterDifferentKind) 10827 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10828 10829 ParameterMismatch = true; 10830 break; 10831 } 10832 10833 if (FirstParam->getName() != SecondParam->getName()) { 10834 ODRDiagDeclError( 10835 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10836 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10837 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10838 << FirstParam; 10839 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10840 SecondTemplate->getSourceRange(), 10841 FunctionTemplateParameterName) 10842 << SecondTemplate << (i + 1) 10843 << (bool)SecondParam->getIdentifier() << SecondParam; 10844 ParameterMismatch = true; 10845 break; 10846 } 10847 10848 if (isa<TemplateTypeParmDecl>(FirstParam) && 10849 isa<TemplateTypeParmDecl>(SecondParam)) { 10850 TemplateTypeParmDecl *FirstTTPD = 10851 cast<TemplateTypeParmDecl>(FirstParam); 10852 TemplateTypeParmDecl *SecondTTPD = 10853 cast<TemplateTypeParmDecl>(SecondParam); 10854 bool HasFirstDefaultArgument = 10855 FirstTTPD->hasDefaultArgument() && 10856 !FirstTTPD->defaultArgumentWasInherited(); 10857 bool HasSecondDefaultArgument = 10858 SecondTTPD->hasDefaultArgument() && 10859 !SecondTTPD->defaultArgumentWasInherited(); 10860 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10861 ODRDiagDeclError(FirstRecord, FirstModule, 10862 FirstTemplate->getLocation(), 10863 FirstTemplate->getSourceRange(), 10864 FunctionTemplateParameterSingleDefaultArgument) 10865 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10866 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10867 SecondTemplate->getSourceRange(), 10868 FunctionTemplateParameterSingleDefaultArgument) 10869 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10870 ParameterMismatch = true; 10871 break; 10872 } 10873 10874 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10875 QualType FirstType = FirstTTPD->getDefaultArgument(); 10876 QualType SecondType = SecondTTPD->getDefaultArgument(); 10877 if (ComputeQualTypeODRHash(FirstType) != 10878 ComputeQualTypeODRHash(SecondType)) { 10879 ODRDiagDeclError( 10880 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10881 FirstTemplate->getSourceRange(), 10882 FunctionTemplateParameterDifferentDefaultArgument) 10883 << FirstTemplate << (i + 1) << FirstType; 10884 ODRDiagDeclNote( 10885 SecondModule, SecondTemplate->getLocation(), 10886 SecondTemplate->getSourceRange(), 10887 FunctionTemplateParameterDifferentDefaultArgument) 10888 << SecondTemplate << (i + 1) << SecondType; 10889 ParameterMismatch = true; 10890 break; 10891 } 10892 } 10893 10894 if (FirstTTPD->isParameterPack() != 10895 SecondTTPD->isParameterPack()) { 10896 ODRDiagDeclError(FirstRecord, FirstModule, 10897 FirstTemplate->getLocation(), 10898 FirstTemplate->getSourceRange(), 10899 FunctionTemplatePackParameter) 10900 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10901 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10902 SecondTemplate->getSourceRange(), 10903 FunctionTemplatePackParameter) 10904 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10905 ParameterMismatch = true; 10906 break; 10907 } 10908 } 10909 10910 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10911 isa<TemplateTemplateParmDecl>(SecondParam)) { 10912 TemplateTemplateParmDecl *FirstTTPD = 10913 cast<TemplateTemplateParmDecl>(FirstParam); 10914 TemplateTemplateParmDecl *SecondTTPD = 10915 cast<TemplateTemplateParmDecl>(SecondParam); 10916 10917 TemplateParameterList *FirstTPL = 10918 FirstTTPD->getTemplateParameters(); 10919 TemplateParameterList *SecondTPL = 10920 SecondTTPD->getTemplateParameters(); 10921 10922 if (ComputeTemplateParameterListODRHash(FirstTPL) != 10923 ComputeTemplateParameterListODRHash(SecondTPL)) { 10924 ODRDiagDeclError(FirstRecord, FirstModule, 10925 FirstTemplate->getLocation(), 10926 FirstTemplate->getSourceRange(), 10927 FunctionTemplateParameterDifferentType) 10928 << FirstTemplate << (i + 1); 10929 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10930 SecondTemplate->getSourceRange(), 10931 FunctionTemplateParameterDifferentType) 10932 << SecondTemplate << (i + 1); 10933 ParameterMismatch = true; 10934 break; 10935 } 10936 10937 bool HasFirstDefaultArgument = 10938 FirstTTPD->hasDefaultArgument() && 10939 !FirstTTPD->defaultArgumentWasInherited(); 10940 bool HasSecondDefaultArgument = 10941 SecondTTPD->hasDefaultArgument() && 10942 !SecondTTPD->defaultArgumentWasInherited(); 10943 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10944 ODRDiagDeclError(FirstRecord, FirstModule, 10945 FirstTemplate->getLocation(), 10946 FirstTemplate->getSourceRange(), 10947 FunctionTemplateParameterSingleDefaultArgument) 10948 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10949 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10950 SecondTemplate->getSourceRange(), 10951 FunctionTemplateParameterSingleDefaultArgument) 10952 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10953 ParameterMismatch = true; 10954 break; 10955 } 10956 10957 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10958 TemplateArgument FirstTA = 10959 FirstTTPD->getDefaultArgument().getArgument(); 10960 TemplateArgument SecondTA = 10961 SecondTTPD->getDefaultArgument().getArgument(); 10962 if (ComputeTemplateArgumentODRHash(FirstTA) != 10963 ComputeTemplateArgumentODRHash(SecondTA)) { 10964 ODRDiagDeclError( 10965 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10966 FirstTemplate->getSourceRange(), 10967 FunctionTemplateParameterDifferentDefaultArgument) 10968 << FirstTemplate << (i + 1) << FirstTA; 10969 ODRDiagDeclNote( 10970 SecondModule, SecondTemplate->getLocation(), 10971 SecondTemplate->getSourceRange(), 10972 FunctionTemplateParameterDifferentDefaultArgument) 10973 << SecondTemplate << (i + 1) << SecondTA; 10974 ParameterMismatch = true; 10975 break; 10976 } 10977 } 10978 10979 if (FirstTTPD->isParameterPack() != 10980 SecondTTPD->isParameterPack()) { 10981 ODRDiagDeclError(FirstRecord, FirstModule, 10982 FirstTemplate->getLocation(), 10983 FirstTemplate->getSourceRange(), 10984 FunctionTemplatePackParameter) 10985 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10986 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10987 SecondTemplate->getSourceRange(), 10988 FunctionTemplatePackParameter) 10989 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10990 ParameterMismatch = true; 10991 break; 10992 } 10993 } 10994 10995 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 10996 isa<NonTypeTemplateParmDecl>(SecondParam)) { 10997 NonTypeTemplateParmDecl *FirstNTTPD = 10998 cast<NonTypeTemplateParmDecl>(FirstParam); 10999 NonTypeTemplateParmDecl *SecondNTTPD = 11000 cast<NonTypeTemplateParmDecl>(SecondParam); 11001 11002 QualType FirstType = FirstNTTPD->getType(); 11003 QualType SecondType = SecondNTTPD->getType(); 11004 if (ComputeQualTypeODRHash(FirstType) != 11005 ComputeQualTypeODRHash(SecondType)) { 11006 ODRDiagDeclError(FirstRecord, FirstModule, 11007 FirstTemplate->getLocation(), 11008 FirstTemplate->getSourceRange(), 11009 FunctionTemplateParameterDifferentType) 11010 << FirstTemplate << (i + 1); 11011 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11012 SecondTemplate->getSourceRange(), 11013 FunctionTemplateParameterDifferentType) 11014 << SecondTemplate << (i + 1); 11015 ParameterMismatch = true; 11016 break; 11017 } 11018 11019 bool HasFirstDefaultArgument = 11020 FirstNTTPD->hasDefaultArgument() && 11021 !FirstNTTPD->defaultArgumentWasInherited(); 11022 bool HasSecondDefaultArgument = 11023 SecondNTTPD->hasDefaultArgument() && 11024 !SecondNTTPD->defaultArgumentWasInherited(); 11025 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11026 ODRDiagDeclError(FirstRecord, FirstModule, 11027 FirstTemplate->getLocation(), 11028 FirstTemplate->getSourceRange(), 11029 FunctionTemplateParameterSingleDefaultArgument) 11030 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11031 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11032 SecondTemplate->getSourceRange(), 11033 FunctionTemplateParameterSingleDefaultArgument) 11034 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11035 ParameterMismatch = true; 11036 break; 11037 } 11038 11039 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11040 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11041 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11042 if (ComputeODRHash(FirstDefaultArgument) != 11043 ComputeODRHash(SecondDefaultArgument)) { 11044 ODRDiagDeclError( 11045 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11046 FirstTemplate->getSourceRange(), 11047 FunctionTemplateParameterDifferentDefaultArgument) 11048 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11049 ODRDiagDeclNote( 11050 SecondModule, SecondTemplate->getLocation(), 11051 SecondTemplate->getSourceRange(), 11052 FunctionTemplateParameterDifferentDefaultArgument) 11053 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11054 ParameterMismatch = true; 11055 break; 11056 } 11057 } 11058 11059 if (FirstNTTPD->isParameterPack() != 11060 SecondNTTPD->isParameterPack()) { 11061 ODRDiagDeclError(FirstRecord, FirstModule, 11062 FirstTemplate->getLocation(), 11063 FirstTemplate->getSourceRange(), 11064 FunctionTemplatePackParameter) 11065 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11066 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11067 SecondTemplate->getSourceRange(), 11068 FunctionTemplatePackParameter) 11069 << SecondTemplate << (i + 1) 11070 << SecondNTTPD->isParameterPack(); 11071 ParameterMismatch = true; 11072 break; 11073 } 11074 } 11075 } 11076 11077 if (ParameterMismatch) { 11078 Diagnosed = true; 11079 break; 11080 } 11081 11082 break; 11083 } 11084 } 11085 11086 if (Diagnosed) 11087 continue; 11088 11089 Diag(FirstDecl->getLocation(), 11090 diag::err_module_odr_violation_mismatch_decl_unknown) 11091 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11092 << FirstDecl->getSourceRange(); 11093 Diag(SecondDecl->getLocation(), 11094 diag::note_module_odr_violation_mismatch_decl_unknown) 11095 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11096 Diagnosed = true; 11097 } 11098 11099 if (!Diagnosed) { 11100 // All definitions are updates to the same declaration. This happens if a 11101 // module instantiates the declaration of a class template specialization 11102 // and two or more other modules instantiate its definition. 11103 // 11104 // FIXME: Indicate which modules had instantiations of this definition. 11105 // FIXME: How can this even happen? 11106 Diag(Merge.first->getLocation(), 11107 diag::err_module_odr_violation_different_instantiations) 11108 << Merge.first; 11109 } 11110 } 11111 11112 // Issue ODR failures diagnostics for functions. 11113 for (auto &Merge : FunctionOdrMergeFailures) { 11114 enum ODRFunctionDifference { 11115 ReturnType, 11116 ParameterName, 11117 ParameterType, 11118 ParameterSingleDefaultArgument, 11119 ParameterDifferentDefaultArgument, 11120 FunctionBody, 11121 }; 11122 11123 FunctionDecl *FirstFunction = Merge.first; 11124 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11125 11126 bool Diagnosed = false; 11127 for (auto &SecondFunction : Merge.second) { 11128 11129 if (FirstFunction == SecondFunction) 11130 continue; 11131 11132 std::string SecondModule = 11133 getOwningModuleNameForDiagnostic(SecondFunction); 11134 11135 auto ODRDiagError = [FirstFunction, &FirstModule, 11136 this](SourceLocation Loc, SourceRange Range, 11137 ODRFunctionDifference DiffType) { 11138 return Diag(Loc, diag::err_module_odr_violation_function) 11139 << FirstFunction << FirstModule.empty() << FirstModule << Range 11140 << DiffType; 11141 }; 11142 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11143 SourceRange Range, 11144 ODRFunctionDifference DiffType) { 11145 return Diag(Loc, diag::note_module_odr_violation_function) 11146 << SecondModule << Range << DiffType; 11147 }; 11148 11149 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11150 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11151 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11152 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11153 << FirstFunction->getReturnType(); 11154 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11155 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11156 << SecondFunction->getReturnType(); 11157 Diagnosed = true; 11158 break; 11159 } 11160 11161 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11162 "Merged functions with different number of parameters"); 11163 11164 auto ParamSize = FirstFunction->param_size(); 11165 bool ParameterMismatch = false; 11166 for (unsigned I = 0; I < ParamSize; ++I) { 11167 auto *FirstParam = FirstFunction->getParamDecl(I); 11168 auto *SecondParam = SecondFunction->getParamDecl(I); 11169 11170 assert(getContext().hasSameType(FirstParam->getType(), 11171 SecondParam->getType()) && 11172 "Merged function has different parameter types."); 11173 11174 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11175 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11176 ParameterName) 11177 << I + 1 << FirstParam->getDeclName(); 11178 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11179 ParameterName) 11180 << I + 1 << SecondParam->getDeclName(); 11181 ParameterMismatch = true; 11182 break; 11183 }; 11184 11185 QualType FirstParamType = FirstParam->getType(); 11186 QualType SecondParamType = SecondParam->getType(); 11187 if (FirstParamType != SecondParamType && 11188 ComputeQualTypeODRHash(FirstParamType) != 11189 ComputeQualTypeODRHash(SecondParamType)) { 11190 if (const DecayedType *ParamDecayedType = 11191 FirstParamType->getAs<DecayedType>()) { 11192 ODRDiagError(FirstParam->getLocation(), 11193 FirstParam->getSourceRange(), ParameterType) 11194 << (I + 1) << FirstParamType << true 11195 << ParamDecayedType->getOriginalType(); 11196 } else { 11197 ODRDiagError(FirstParam->getLocation(), 11198 FirstParam->getSourceRange(), ParameterType) 11199 << (I + 1) << FirstParamType << false; 11200 } 11201 11202 if (const DecayedType *ParamDecayedType = 11203 SecondParamType->getAs<DecayedType>()) { 11204 ODRDiagNote(SecondParam->getLocation(), 11205 SecondParam->getSourceRange(), ParameterType) 11206 << (I + 1) << SecondParamType << true 11207 << ParamDecayedType->getOriginalType(); 11208 } else { 11209 ODRDiagNote(SecondParam->getLocation(), 11210 SecondParam->getSourceRange(), ParameterType) 11211 << (I + 1) << SecondParamType << false; 11212 } 11213 ParameterMismatch = true; 11214 break; 11215 } 11216 11217 const Expr *FirstInit = FirstParam->getInit(); 11218 const Expr *SecondInit = SecondParam->getInit(); 11219 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11220 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11221 ParameterSingleDefaultArgument) 11222 << (I + 1) << (FirstInit == nullptr) 11223 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11224 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11225 ParameterSingleDefaultArgument) 11226 << (I + 1) << (SecondInit == nullptr) 11227 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11228 ParameterMismatch = true; 11229 break; 11230 } 11231 11232 if (FirstInit && SecondInit && 11233 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11234 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11235 ParameterDifferentDefaultArgument) 11236 << (I + 1) << FirstInit->getSourceRange(); 11237 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11238 ParameterDifferentDefaultArgument) 11239 << (I + 1) << SecondInit->getSourceRange(); 11240 ParameterMismatch = true; 11241 break; 11242 } 11243 11244 assert(ComputeSubDeclODRHash(FirstParam) == 11245 ComputeSubDeclODRHash(SecondParam) && 11246 "Undiagnosed parameter difference."); 11247 } 11248 11249 if (ParameterMismatch) { 11250 Diagnosed = true; 11251 break; 11252 } 11253 11254 // If no error has been generated before now, assume the problem is in 11255 // the body and generate a message. 11256 ODRDiagError(FirstFunction->getLocation(), 11257 FirstFunction->getSourceRange(), FunctionBody); 11258 ODRDiagNote(SecondFunction->getLocation(), 11259 SecondFunction->getSourceRange(), FunctionBody); 11260 Diagnosed = true; 11261 break; 11262 } 11263 (void)Diagnosed; 11264 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11265 } 11266 11267 // Issue ODR failures diagnostics for enums. 11268 for (auto &Merge : EnumOdrMergeFailures) { 11269 enum ODREnumDifference { 11270 SingleScopedEnum, 11271 EnumTagKeywordMismatch, 11272 SingleSpecifiedType, 11273 DifferentSpecifiedTypes, 11274 DifferentNumberEnumConstants, 11275 EnumConstantName, 11276 EnumConstantSingleInitilizer, 11277 EnumConstantDifferentInitilizer, 11278 }; 11279 11280 // If we've already pointed out a specific problem with this enum, don't 11281 // bother issuing a general "something's different" diagnostic. 11282 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11283 continue; 11284 11285 EnumDecl *FirstEnum = Merge.first; 11286 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11287 11288 using DeclHashes = 11289 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11290 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11291 DeclHashes &Hashes, EnumDecl *Enum) { 11292 for (auto *D : Enum->decls()) { 11293 // Due to decl merging, the first EnumDecl is the parent of 11294 // Decls in both records. 11295 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11296 continue; 11297 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11298 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11299 ComputeSubDeclODRHash(D)); 11300 } 11301 }; 11302 DeclHashes FirstHashes; 11303 PopulateHashes(FirstHashes, FirstEnum); 11304 bool Diagnosed = false; 11305 for (auto &SecondEnum : Merge.second) { 11306 11307 if (FirstEnum == SecondEnum) 11308 continue; 11309 11310 std::string SecondModule = 11311 getOwningModuleNameForDiagnostic(SecondEnum); 11312 11313 auto ODRDiagError = [FirstEnum, &FirstModule, 11314 this](SourceLocation Loc, SourceRange Range, 11315 ODREnumDifference DiffType) { 11316 return Diag(Loc, diag::err_module_odr_violation_enum) 11317 << FirstEnum << FirstModule.empty() << FirstModule << Range 11318 << DiffType; 11319 }; 11320 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11321 SourceRange Range, 11322 ODREnumDifference DiffType) { 11323 return Diag(Loc, diag::note_module_odr_violation_enum) 11324 << SecondModule << Range << DiffType; 11325 }; 11326 11327 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11328 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11329 SingleScopedEnum) 11330 << FirstEnum->isScoped(); 11331 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11332 SingleScopedEnum) 11333 << SecondEnum->isScoped(); 11334 Diagnosed = true; 11335 continue; 11336 } 11337 11338 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11339 if (FirstEnum->isScopedUsingClassTag() != 11340 SecondEnum->isScopedUsingClassTag()) { 11341 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11342 EnumTagKeywordMismatch) 11343 << FirstEnum->isScopedUsingClassTag(); 11344 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11345 EnumTagKeywordMismatch) 11346 << SecondEnum->isScopedUsingClassTag(); 11347 Diagnosed = true; 11348 continue; 11349 } 11350 } 11351 11352 QualType FirstUnderlyingType = 11353 FirstEnum->getIntegerTypeSourceInfo() 11354 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11355 : QualType(); 11356 QualType SecondUnderlyingType = 11357 SecondEnum->getIntegerTypeSourceInfo() 11358 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11359 : QualType(); 11360 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11361 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11362 SingleSpecifiedType) 11363 << !FirstUnderlyingType.isNull(); 11364 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11365 SingleSpecifiedType) 11366 << !SecondUnderlyingType.isNull(); 11367 Diagnosed = true; 11368 continue; 11369 } 11370 11371 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11372 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11373 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11374 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11375 DifferentSpecifiedTypes) 11376 << FirstUnderlyingType; 11377 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11378 DifferentSpecifiedTypes) 11379 << SecondUnderlyingType; 11380 Diagnosed = true; 11381 continue; 11382 } 11383 } 11384 11385 DeclHashes SecondHashes; 11386 PopulateHashes(SecondHashes, SecondEnum); 11387 11388 if (FirstHashes.size() != SecondHashes.size()) { 11389 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11390 DifferentNumberEnumConstants) 11391 << (int)FirstHashes.size(); 11392 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11393 DifferentNumberEnumConstants) 11394 << (int)SecondHashes.size(); 11395 Diagnosed = true; 11396 continue; 11397 } 11398 11399 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11400 if (FirstHashes[I].second == SecondHashes[I].second) 11401 continue; 11402 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11403 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11404 11405 if (FirstEnumConstant->getDeclName() != 11406 SecondEnumConstant->getDeclName()) { 11407 11408 ODRDiagError(FirstEnumConstant->getLocation(), 11409 FirstEnumConstant->getSourceRange(), EnumConstantName) 11410 << I + 1 << FirstEnumConstant; 11411 ODRDiagNote(SecondEnumConstant->getLocation(), 11412 SecondEnumConstant->getSourceRange(), EnumConstantName) 11413 << I + 1 << SecondEnumConstant; 11414 Diagnosed = true; 11415 break; 11416 } 11417 11418 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11419 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11420 if (!FirstInit && !SecondInit) 11421 continue; 11422 11423 if (!FirstInit || !SecondInit) { 11424 ODRDiagError(FirstEnumConstant->getLocation(), 11425 FirstEnumConstant->getSourceRange(), 11426 EnumConstantSingleInitilizer) 11427 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11428 ODRDiagNote(SecondEnumConstant->getLocation(), 11429 SecondEnumConstant->getSourceRange(), 11430 EnumConstantSingleInitilizer) 11431 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11432 Diagnosed = true; 11433 break; 11434 } 11435 11436 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11437 ODRDiagError(FirstEnumConstant->getLocation(), 11438 FirstEnumConstant->getSourceRange(), 11439 EnumConstantDifferentInitilizer) 11440 << I + 1 << FirstEnumConstant; 11441 ODRDiagNote(SecondEnumConstant->getLocation(), 11442 SecondEnumConstant->getSourceRange(), 11443 EnumConstantDifferentInitilizer) 11444 << I + 1 << SecondEnumConstant; 11445 Diagnosed = true; 11446 break; 11447 } 11448 } 11449 } 11450 11451 (void)Diagnosed; 11452 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11453 } 11454 } 11455 11456 void ASTReader::StartedDeserializing() { 11457 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11458 ReadTimer->startTimer(); 11459 } 11460 11461 void ASTReader::FinishedDeserializing() { 11462 assert(NumCurrentElementsDeserializing && 11463 "FinishedDeserializing not paired with StartedDeserializing"); 11464 if (NumCurrentElementsDeserializing == 1) { 11465 // We decrease NumCurrentElementsDeserializing only after pending actions 11466 // are finished, to avoid recursively re-calling finishPendingActions(). 11467 finishPendingActions(); 11468 } 11469 --NumCurrentElementsDeserializing; 11470 11471 if (NumCurrentElementsDeserializing == 0) { 11472 // Propagate exception specification and deduced type updates along 11473 // redeclaration chains. 11474 // 11475 // We do this now rather than in finishPendingActions because we want to 11476 // be able to walk the complete redeclaration chains of the updated decls. 11477 while (!PendingExceptionSpecUpdates.empty() || 11478 !PendingDeducedTypeUpdates.empty()) { 11479 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11480 PendingExceptionSpecUpdates.clear(); 11481 for (auto Update : ESUpdates) { 11482 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11483 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11484 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11485 if (auto *Listener = getContext().getASTMutationListener()) 11486 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11487 for (auto *Redecl : Update.second->redecls()) 11488 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11489 } 11490 11491 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11492 PendingDeducedTypeUpdates.clear(); 11493 for (auto Update : DTUpdates) { 11494 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11495 // FIXME: If the return type is already deduced, check that it matches. 11496 getContext().adjustDeducedFunctionResultType(Update.first, 11497 Update.second); 11498 } 11499 } 11500 11501 if (ReadTimer) 11502 ReadTimer->stopTimer(); 11503 11504 diagnoseOdrViolations(); 11505 11506 // We are not in recursive loading, so it's safe to pass the "interesting" 11507 // decls to the consumer. 11508 if (Consumer) 11509 PassInterestingDeclsToConsumer(); 11510 } 11511 } 11512 11513 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11514 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11515 // Remove any fake results before adding any real ones. 11516 auto It = PendingFakeLookupResults.find(II); 11517 if (It != PendingFakeLookupResults.end()) { 11518 for (auto *ND : It->second) 11519 SemaObj->IdResolver.RemoveDecl(ND); 11520 // FIXME: this works around module+PCH performance issue. 11521 // Rather than erase the result from the map, which is O(n), just clear 11522 // the vector of NamedDecls. 11523 It->second.clear(); 11524 } 11525 } 11526 11527 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11528 SemaObj->TUScope->AddDecl(D); 11529 } else if (SemaObj->TUScope) { 11530 // Adding the decl to IdResolver may have failed because it was already in 11531 // (even though it was not added in scope). If it is already in, make sure 11532 // it gets in the scope as well. 11533 if (std::find(SemaObj->IdResolver.begin(Name), 11534 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11535 SemaObj->TUScope->AddDecl(D); 11536 } 11537 } 11538 11539 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11540 ASTContext *Context, 11541 const PCHContainerReader &PCHContainerRdr, 11542 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11543 StringRef isysroot, 11544 DisableValidationForModuleKind DisableValidationKind, 11545 bool AllowASTWithCompilerErrors, 11546 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11547 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11548 std::unique_ptr<llvm::Timer> ReadTimer) 11549 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH) 11550 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11551 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11552 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11553 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11554 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11555 PCHContainerRdr, PP.getHeaderSearchInfo()), 11556 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11557 DisableValidationKind(DisableValidationKind), 11558 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11559 AllowConfigurationMismatch(AllowConfigurationMismatch), 11560 ValidateSystemInputs(ValidateSystemInputs), 11561 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11562 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11563 SourceMgr.setExternalSLocEntrySource(this); 11564 11565 for (const auto &Ext : Extensions) { 11566 auto BlockName = Ext->getExtensionMetadata().BlockName; 11567 auto Known = ModuleFileExtensions.find(BlockName); 11568 if (Known != ModuleFileExtensions.end()) { 11569 Diags.Report(diag::warn_duplicate_module_file_extension) 11570 << BlockName; 11571 continue; 11572 } 11573 11574 ModuleFileExtensions.insert({BlockName, Ext}); 11575 } 11576 } 11577 11578 ASTReader::~ASTReader() { 11579 if (OwnsDeserializationListener) 11580 delete DeserializationListener; 11581 } 11582 11583 IdentifierResolver &ASTReader::getIdResolver() { 11584 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11585 } 11586 11587 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11588 unsigned AbbrevID) { 11589 Idx = 0; 11590 Record.clear(); 11591 return Cursor.readRecord(AbbrevID, Record); 11592 } 11593 //===----------------------------------------------------------------------===// 11594 //// OMPClauseReader implementation 11595 ////===----------------------------------------------------------------------===// 11596 11597 // This has to be in namespace clang because it's friended by all 11598 // of the OMP clauses. 11599 namespace clang { 11600 11601 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11602 ASTRecordReader &Record; 11603 ASTContext &Context; 11604 11605 public: 11606 OMPClauseReader(ASTRecordReader &Record) 11607 : Record(Record), Context(Record.getContext()) {} 11608 #define GEN_CLANG_CLAUSE_CLASS 11609 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11610 #include "llvm/Frontend/OpenMP/OMP.inc" 11611 OMPClause *readClause(); 11612 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11613 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11614 }; 11615 11616 } // end namespace clang 11617 11618 OMPClause *ASTRecordReader::readOMPClause() { 11619 return OMPClauseReader(*this).readClause(); 11620 } 11621 11622 OMPClause *OMPClauseReader::readClause() { 11623 OMPClause *C = nullptr; 11624 switch (llvm::omp::Clause(Record.readInt())) { 11625 case llvm::omp::OMPC_if: 11626 C = new (Context) OMPIfClause(); 11627 break; 11628 case llvm::omp::OMPC_final: 11629 C = new (Context) OMPFinalClause(); 11630 break; 11631 case llvm::omp::OMPC_num_threads: 11632 C = new (Context) OMPNumThreadsClause(); 11633 break; 11634 case llvm::omp::OMPC_safelen: 11635 C = new (Context) OMPSafelenClause(); 11636 break; 11637 case llvm::omp::OMPC_simdlen: 11638 C = new (Context) OMPSimdlenClause(); 11639 break; 11640 case llvm::omp::OMPC_sizes: { 11641 unsigned NumSizes = Record.readInt(); 11642 C = OMPSizesClause::CreateEmpty(Context, NumSizes); 11643 break; 11644 } 11645 case llvm::omp::OMPC_full: 11646 C = OMPFullClause::CreateEmpty(Context); 11647 break; 11648 case llvm::omp::OMPC_partial: 11649 C = OMPPartialClause::CreateEmpty(Context); 11650 break; 11651 case llvm::omp::OMPC_allocator: 11652 C = new (Context) OMPAllocatorClause(); 11653 break; 11654 case llvm::omp::OMPC_collapse: 11655 C = new (Context) OMPCollapseClause(); 11656 break; 11657 case llvm::omp::OMPC_default: 11658 C = new (Context) OMPDefaultClause(); 11659 break; 11660 case llvm::omp::OMPC_proc_bind: 11661 C = new (Context) OMPProcBindClause(); 11662 break; 11663 case llvm::omp::OMPC_schedule: 11664 C = new (Context) OMPScheduleClause(); 11665 break; 11666 case llvm::omp::OMPC_ordered: 11667 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11668 break; 11669 case llvm::omp::OMPC_nowait: 11670 C = new (Context) OMPNowaitClause(); 11671 break; 11672 case llvm::omp::OMPC_untied: 11673 C = new (Context) OMPUntiedClause(); 11674 break; 11675 case llvm::omp::OMPC_mergeable: 11676 C = new (Context) OMPMergeableClause(); 11677 break; 11678 case llvm::omp::OMPC_read: 11679 C = new (Context) OMPReadClause(); 11680 break; 11681 case llvm::omp::OMPC_write: 11682 C = new (Context) OMPWriteClause(); 11683 break; 11684 case llvm::omp::OMPC_update: 11685 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11686 break; 11687 case llvm::omp::OMPC_capture: 11688 C = new (Context) OMPCaptureClause(); 11689 break; 11690 case llvm::omp::OMPC_compare: 11691 C = new (Context) OMPCompareClause(); 11692 break; 11693 case llvm::omp::OMPC_seq_cst: 11694 C = new (Context) OMPSeqCstClause(); 11695 break; 11696 case llvm::omp::OMPC_acq_rel: 11697 C = new (Context) OMPAcqRelClause(); 11698 break; 11699 case llvm::omp::OMPC_acquire: 11700 C = new (Context) OMPAcquireClause(); 11701 break; 11702 case llvm::omp::OMPC_release: 11703 C = new (Context) OMPReleaseClause(); 11704 break; 11705 case llvm::omp::OMPC_relaxed: 11706 C = new (Context) OMPRelaxedClause(); 11707 break; 11708 case llvm::omp::OMPC_threads: 11709 C = new (Context) OMPThreadsClause(); 11710 break; 11711 case llvm::omp::OMPC_simd: 11712 C = new (Context) OMPSIMDClause(); 11713 break; 11714 case llvm::omp::OMPC_nogroup: 11715 C = new (Context) OMPNogroupClause(); 11716 break; 11717 case llvm::omp::OMPC_unified_address: 11718 C = new (Context) OMPUnifiedAddressClause(); 11719 break; 11720 case llvm::omp::OMPC_unified_shared_memory: 11721 C = new (Context) OMPUnifiedSharedMemoryClause(); 11722 break; 11723 case llvm::omp::OMPC_reverse_offload: 11724 C = new (Context) OMPReverseOffloadClause(); 11725 break; 11726 case llvm::omp::OMPC_dynamic_allocators: 11727 C = new (Context) OMPDynamicAllocatorsClause(); 11728 break; 11729 case llvm::omp::OMPC_atomic_default_mem_order: 11730 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11731 break; 11732 case llvm::omp::OMPC_private: 11733 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11734 break; 11735 case llvm::omp::OMPC_firstprivate: 11736 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11737 break; 11738 case llvm::omp::OMPC_lastprivate: 11739 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11740 break; 11741 case llvm::omp::OMPC_shared: 11742 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11743 break; 11744 case llvm::omp::OMPC_reduction: { 11745 unsigned N = Record.readInt(); 11746 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11747 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11748 break; 11749 } 11750 case llvm::omp::OMPC_task_reduction: 11751 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11752 break; 11753 case llvm::omp::OMPC_in_reduction: 11754 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11755 break; 11756 case llvm::omp::OMPC_linear: 11757 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11758 break; 11759 case llvm::omp::OMPC_aligned: 11760 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11761 break; 11762 case llvm::omp::OMPC_copyin: 11763 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11764 break; 11765 case llvm::omp::OMPC_copyprivate: 11766 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11767 break; 11768 case llvm::omp::OMPC_flush: 11769 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11770 break; 11771 case llvm::omp::OMPC_depobj: 11772 C = OMPDepobjClause::CreateEmpty(Context); 11773 break; 11774 case llvm::omp::OMPC_depend: { 11775 unsigned NumVars = Record.readInt(); 11776 unsigned NumLoops = Record.readInt(); 11777 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11778 break; 11779 } 11780 case llvm::omp::OMPC_device: 11781 C = new (Context) OMPDeviceClause(); 11782 break; 11783 case llvm::omp::OMPC_map: { 11784 OMPMappableExprListSizeTy Sizes; 11785 Sizes.NumVars = Record.readInt(); 11786 Sizes.NumUniqueDeclarations = Record.readInt(); 11787 Sizes.NumComponentLists = Record.readInt(); 11788 Sizes.NumComponents = Record.readInt(); 11789 C = OMPMapClause::CreateEmpty(Context, Sizes); 11790 break; 11791 } 11792 case llvm::omp::OMPC_num_teams: 11793 C = new (Context) OMPNumTeamsClause(); 11794 break; 11795 case llvm::omp::OMPC_thread_limit: 11796 C = new (Context) OMPThreadLimitClause(); 11797 break; 11798 case llvm::omp::OMPC_priority: 11799 C = new (Context) OMPPriorityClause(); 11800 break; 11801 case llvm::omp::OMPC_grainsize: 11802 C = new (Context) OMPGrainsizeClause(); 11803 break; 11804 case llvm::omp::OMPC_num_tasks: 11805 C = new (Context) OMPNumTasksClause(); 11806 break; 11807 case llvm::omp::OMPC_hint: 11808 C = new (Context) OMPHintClause(); 11809 break; 11810 case llvm::omp::OMPC_dist_schedule: 11811 C = new (Context) OMPDistScheduleClause(); 11812 break; 11813 case llvm::omp::OMPC_defaultmap: 11814 C = new (Context) OMPDefaultmapClause(); 11815 break; 11816 case llvm::omp::OMPC_to: { 11817 OMPMappableExprListSizeTy Sizes; 11818 Sizes.NumVars = Record.readInt(); 11819 Sizes.NumUniqueDeclarations = Record.readInt(); 11820 Sizes.NumComponentLists = Record.readInt(); 11821 Sizes.NumComponents = Record.readInt(); 11822 C = OMPToClause::CreateEmpty(Context, Sizes); 11823 break; 11824 } 11825 case llvm::omp::OMPC_from: { 11826 OMPMappableExprListSizeTy Sizes; 11827 Sizes.NumVars = Record.readInt(); 11828 Sizes.NumUniqueDeclarations = Record.readInt(); 11829 Sizes.NumComponentLists = Record.readInt(); 11830 Sizes.NumComponents = Record.readInt(); 11831 C = OMPFromClause::CreateEmpty(Context, Sizes); 11832 break; 11833 } 11834 case llvm::omp::OMPC_use_device_ptr: { 11835 OMPMappableExprListSizeTy Sizes; 11836 Sizes.NumVars = Record.readInt(); 11837 Sizes.NumUniqueDeclarations = Record.readInt(); 11838 Sizes.NumComponentLists = Record.readInt(); 11839 Sizes.NumComponents = Record.readInt(); 11840 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11841 break; 11842 } 11843 case llvm::omp::OMPC_use_device_addr: { 11844 OMPMappableExprListSizeTy Sizes; 11845 Sizes.NumVars = Record.readInt(); 11846 Sizes.NumUniqueDeclarations = Record.readInt(); 11847 Sizes.NumComponentLists = Record.readInt(); 11848 Sizes.NumComponents = Record.readInt(); 11849 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11850 break; 11851 } 11852 case llvm::omp::OMPC_is_device_ptr: { 11853 OMPMappableExprListSizeTy Sizes; 11854 Sizes.NumVars = Record.readInt(); 11855 Sizes.NumUniqueDeclarations = Record.readInt(); 11856 Sizes.NumComponentLists = Record.readInt(); 11857 Sizes.NumComponents = Record.readInt(); 11858 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11859 break; 11860 } 11861 case llvm::omp::OMPC_allocate: 11862 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11863 break; 11864 case llvm::omp::OMPC_nontemporal: 11865 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11866 break; 11867 case llvm::omp::OMPC_inclusive: 11868 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11869 break; 11870 case llvm::omp::OMPC_exclusive: 11871 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11872 break; 11873 case llvm::omp::OMPC_order: 11874 C = new (Context) OMPOrderClause(); 11875 break; 11876 case llvm::omp::OMPC_init: 11877 C = OMPInitClause::CreateEmpty(Context, Record.readInt()); 11878 break; 11879 case llvm::omp::OMPC_use: 11880 C = new (Context) OMPUseClause(); 11881 break; 11882 case llvm::omp::OMPC_destroy: 11883 C = new (Context) OMPDestroyClause(); 11884 break; 11885 case llvm::omp::OMPC_novariants: 11886 C = new (Context) OMPNovariantsClause(); 11887 break; 11888 case llvm::omp::OMPC_nocontext: 11889 C = new (Context) OMPNocontextClause(); 11890 break; 11891 case llvm::omp::OMPC_detach: 11892 C = new (Context) OMPDetachClause(); 11893 break; 11894 case llvm::omp::OMPC_uses_allocators: 11895 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11896 break; 11897 case llvm::omp::OMPC_affinity: 11898 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11899 break; 11900 case llvm::omp::OMPC_filter: 11901 C = new (Context) OMPFilterClause(); 11902 break; 11903 case llvm::omp::OMPC_bind: 11904 C = OMPBindClause::CreateEmpty(Context); 11905 break; 11906 case llvm::omp::OMPC_align: 11907 C = new (Context) OMPAlignClause(); 11908 break; 11909 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11910 case llvm::omp::Enum: \ 11911 break; 11912 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11913 default: 11914 break; 11915 } 11916 assert(C && "Unknown OMPClause type"); 11917 11918 Visit(C); 11919 C->setLocStart(Record.readSourceLocation()); 11920 C->setLocEnd(Record.readSourceLocation()); 11921 11922 return C; 11923 } 11924 11925 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11926 C->setPreInitStmt(Record.readSubStmt(), 11927 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11928 } 11929 11930 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11931 VisitOMPClauseWithPreInit(C); 11932 C->setPostUpdateExpr(Record.readSubExpr()); 11933 } 11934 11935 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 11936 VisitOMPClauseWithPreInit(C); 11937 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 11938 C->setNameModifierLoc(Record.readSourceLocation()); 11939 C->setColonLoc(Record.readSourceLocation()); 11940 C->setCondition(Record.readSubExpr()); 11941 C->setLParenLoc(Record.readSourceLocation()); 11942 } 11943 11944 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 11945 VisitOMPClauseWithPreInit(C); 11946 C->setCondition(Record.readSubExpr()); 11947 C->setLParenLoc(Record.readSourceLocation()); 11948 } 11949 11950 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 11951 VisitOMPClauseWithPreInit(C); 11952 C->setNumThreads(Record.readSubExpr()); 11953 C->setLParenLoc(Record.readSourceLocation()); 11954 } 11955 11956 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 11957 C->setSafelen(Record.readSubExpr()); 11958 C->setLParenLoc(Record.readSourceLocation()); 11959 } 11960 11961 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 11962 C->setSimdlen(Record.readSubExpr()); 11963 C->setLParenLoc(Record.readSourceLocation()); 11964 } 11965 11966 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) { 11967 for (Expr *&E : C->getSizesRefs()) 11968 E = Record.readSubExpr(); 11969 C->setLParenLoc(Record.readSourceLocation()); 11970 } 11971 11972 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {} 11973 11974 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) { 11975 C->setFactor(Record.readSubExpr()); 11976 C->setLParenLoc(Record.readSourceLocation()); 11977 } 11978 11979 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 11980 C->setAllocator(Record.readExpr()); 11981 C->setLParenLoc(Record.readSourceLocation()); 11982 } 11983 11984 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 11985 C->setNumForLoops(Record.readSubExpr()); 11986 C->setLParenLoc(Record.readSourceLocation()); 11987 } 11988 11989 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 11990 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 11991 C->setLParenLoc(Record.readSourceLocation()); 11992 C->setDefaultKindKwLoc(Record.readSourceLocation()); 11993 } 11994 11995 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 11996 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 11997 C->setLParenLoc(Record.readSourceLocation()); 11998 C->setProcBindKindKwLoc(Record.readSourceLocation()); 11999 } 12000 12001 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12002 VisitOMPClauseWithPreInit(C); 12003 C->setScheduleKind( 12004 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12005 C->setFirstScheduleModifier( 12006 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12007 C->setSecondScheduleModifier( 12008 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12009 C->setChunkSize(Record.readSubExpr()); 12010 C->setLParenLoc(Record.readSourceLocation()); 12011 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12012 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12013 C->setScheduleKindLoc(Record.readSourceLocation()); 12014 C->setCommaLoc(Record.readSourceLocation()); 12015 } 12016 12017 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12018 C->setNumForLoops(Record.readSubExpr()); 12019 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12020 C->setLoopNumIterations(I, Record.readSubExpr()); 12021 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12022 C->setLoopCounter(I, Record.readSubExpr()); 12023 C->setLParenLoc(Record.readSourceLocation()); 12024 } 12025 12026 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12027 C->setEventHandler(Record.readSubExpr()); 12028 C->setLParenLoc(Record.readSourceLocation()); 12029 } 12030 12031 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12032 12033 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12034 12035 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12036 12037 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12038 12039 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12040 12041 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12042 if (C->isExtended()) { 12043 C->setLParenLoc(Record.readSourceLocation()); 12044 C->setArgumentLoc(Record.readSourceLocation()); 12045 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12046 } 12047 } 12048 12049 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12050 12051 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {} 12052 12053 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12054 12055 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12056 12057 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12058 12059 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12060 12061 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12062 12063 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12064 12065 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12066 12067 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12068 12069 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) { 12070 unsigned NumVars = C->varlist_size(); 12071 SmallVector<Expr *, 16> Vars; 12072 Vars.reserve(NumVars); 12073 for (unsigned I = 0; I != NumVars; ++I) 12074 Vars.push_back(Record.readSubExpr()); 12075 C->setVarRefs(Vars); 12076 C->setIsTarget(Record.readBool()); 12077 C->setIsTargetSync(Record.readBool()); 12078 C->setLParenLoc(Record.readSourceLocation()); 12079 C->setVarLoc(Record.readSourceLocation()); 12080 } 12081 12082 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) { 12083 C->setInteropVar(Record.readSubExpr()); 12084 C->setLParenLoc(Record.readSourceLocation()); 12085 C->setVarLoc(Record.readSourceLocation()); 12086 } 12087 12088 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) { 12089 C->setInteropVar(Record.readSubExpr()); 12090 C->setLParenLoc(Record.readSourceLocation()); 12091 C->setVarLoc(Record.readSourceLocation()); 12092 } 12093 12094 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) { 12095 VisitOMPClauseWithPreInit(C); 12096 C->setCondition(Record.readSubExpr()); 12097 C->setLParenLoc(Record.readSourceLocation()); 12098 } 12099 12100 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) { 12101 VisitOMPClauseWithPreInit(C); 12102 C->setCondition(Record.readSubExpr()); 12103 C->setLParenLoc(Record.readSourceLocation()); 12104 } 12105 12106 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12107 12108 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12109 OMPUnifiedSharedMemoryClause *) {} 12110 12111 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12112 12113 void 12114 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12115 } 12116 12117 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12118 OMPAtomicDefaultMemOrderClause *C) { 12119 C->setAtomicDefaultMemOrderKind( 12120 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12121 C->setLParenLoc(Record.readSourceLocation()); 12122 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12123 } 12124 12125 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12126 C->setLParenLoc(Record.readSourceLocation()); 12127 unsigned NumVars = C->varlist_size(); 12128 SmallVector<Expr *, 16> Vars; 12129 Vars.reserve(NumVars); 12130 for (unsigned i = 0; i != NumVars; ++i) 12131 Vars.push_back(Record.readSubExpr()); 12132 C->setVarRefs(Vars); 12133 Vars.clear(); 12134 for (unsigned i = 0; i != NumVars; ++i) 12135 Vars.push_back(Record.readSubExpr()); 12136 C->setPrivateCopies(Vars); 12137 } 12138 12139 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12140 VisitOMPClauseWithPreInit(C); 12141 C->setLParenLoc(Record.readSourceLocation()); 12142 unsigned NumVars = C->varlist_size(); 12143 SmallVector<Expr *, 16> Vars; 12144 Vars.reserve(NumVars); 12145 for (unsigned i = 0; i != NumVars; ++i) 12146 Vars.push_back(Record.readSubExpr()); 12147 C->setVarRefs(Vars); 12148 Vars.clear(); 12149 for (unsigned i = 0; i != NumVars; ++i) 12150 Vars.push_back(Record.readSubExpr()); 12151 C->setPrivateCopies(Vars); 12152 Vars.clear(); 12153 for (unsigned i = 0; i != NumVars; ++i) 12154 Vars.push_back(Record.readSubExpr()); 12155 C->setInits(Vars); 12156 } 12157 12158 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12159 VisitOMPClauseWithPostUpdate(C); 12160 C->setLParenLoc(Record.readSourceLocation()); 12161 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12162 C->setKindLoc(Record.readSourceLocation()); 12163 C->setColonLoc(Record.readSourceLocation()); 12164 unsigned NumVars = C->varlist_size(); 12165 SmallVector<Expr *, 16> Vars; 12166 Vars.reserve(NumVars); 12167 for (unsigned i = 0; i != NumVars; ++i) 12168 Vars.push_back(Record.readSubExpr()); 12169 C->setVarRefs(Vars); 12170 Vars.clear(); 12171 for (unsigned i = 0; i != NumVars; ++i) 12172 Vars.push_back(Record.readSubExpr()); 12173 C->setPrivateCopies(Vars); 12174 Vars.clear(); 12175 for (unsigned i = 0; i != NumVars; ++i) 12176 Vars.push_back(Record.readSubExpr()); 12177 C->setSourceExprs(Vars); 12178 Vars.clear(); 12179 for (unsigned i = 0; i != NumVars; ++i) 12180 Vars.push_back(Record.readSubExpr()); 12181 C->setDestinationExprs(Vars); 12182 Vars.clear(); 12183 for (unsigned i = 0; i != NumVars; ++i) 12184 Vars.push_back(Record.readSubExpr()); 12185 C->setAssignmentOps(Vars); 12186 } 12187 12188 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12189 C->setLParenLoc(Record.readSourceLocation()); 12190 unsigned NumVars = C->varlist_size(); 12191 SmallVector<Expr *, 16> Vars; 12192 Vars.reserve(NumVars); 12193 for (unsigned i = 0; i != NumVars; ++i) 12194 Vars.push_back(Record.readSubExpr()); 12195 C->setVarRefs(Vars); 12196 } 12197 12198 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12199 VisitOMPClauseWithPostUpdate(C); 12200 C->setLParenLoc(Record.readSourceLocation()); 12201 C->setModifierLoc(Record.readSourceLocation()); 12202 C->setColonLoc(Record.readSourceLocation()); 12203 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12204 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12205 C->setQualifierLoc(NNSL); 12206 C->setNameInfo(DNI); 12207 12208 unsigned NumVars = C->varlist_size(); 12209 SmallVector<Expr *, 16> Vars; 12210 Vars.reserve(NumVars); 12211 for (unsigned i = 0; i != NumVars; ++i) 12212 Vars.push_back(Record.readSubExpr()); 12213 C->setVarRefs(Vars); 12214 Vars.clear(); 12215 for (unsigned i = 0; i != NumVars; ++i) 12216 Vars.push_back(Record.readSubExpr()); 12217 C->setPrivates(Vars); 12218 Vars.clear(); 12219 for (unsigned i = 0; i != NumVars; ++i) 12220 Vars.push_back(Record.readSubExpr()); 12221 C->setLHSExprs(Vars); 12222 Vars.clear(); 12223 for (unsigned i = 0; i != NumVars; ++i) 12224 Vars.push_back(Record.readSubExpr()); 12225 C->setRHSExprs(Vars); 12226 Vars.clear(); 12227 for (unsigned i = 0; i != NumVars; ++i) 12228 Vars.push_back(Record.readSubExpr()); 12229 C->setReductionOps(Vars); 12230 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12231 Vars.clear(); 12232 for (unsigned i = 0; i != NumVars; ++i) 12233 Vars.push_back(Record.readSubExpr()); 12234 C->setInscanCopyOps(Vars); 12235 Vars.clear(); 12236 for (unsigned i = 0; i != NumVars; ++i) 12237 Vars.push_back(Record.readSubExpr()); 12238 C->setInscanCopyArrayTemps(Vars); 12239 Vars.clear(); 12240 for (unsigned i = 0; i != NumVars; ++i) 12241 Vars.push_back(Record.readSubExpr()); 12242 C->setInscanCopyArrayElems(Vars); 12243 } 12244 } 12245 12246 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12247 VisitOMPClauseWithPostUpdate(C); 12248 C->setLParenLoc(Record.readSourceLocation()); 12249 C->setColonLoc(Record.readSourceLocation()); 12250 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12251 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12252 C->setQualifierLoc(NNSL); 12253 C->setNameInfo(DNI); 12254 12255 unsigned NumVars = C->varlist_size(); 12256 SmallVector<Expr *, 16> Vars; 12257 Vars.reserve(NumVars); 12258 for (unsigned I = 0; I != NumVars; ++I) 12259 Vars.push_back(Record.readSubExpr()); 12260 C->setVarRefs(Vars); 12261 Vars.clear(); 12262 for (unsigned I = 0; I != NumVars; ++I) 12263 Vars.push_back(Record.readSubExpr()); 12264 C->setPrivates(Vars); 12265 Vars.clear(); 12266 for (unsigned I = 0; I != NumVars; ++I) 12267 Vars.push_back(Record.readSubExpr()); 12268 C->setLHSExprs(Vars); 12269 Vars.clear(); 12270 for (unsigned I = 0; I != NumVars; ++I) 12271 Vars.push_back(Record.readSubExpr()); 12272 C->setRHSExprs(Vars); 12273 Vars.clear(); 12274 for (unsigned I = 0; I != NumVars; ++I) 12275 Vars.push_back(Record.readSubExpr()); 12276 C->setReductionOps(Vars); 12277 } 12278 12279 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12280 VisitOMPClauseWithPostUpdate(C); 12281 C->setLParenLoc(Record.readSourceLocation()); 12282 C->setColonLoc(Record.readSourceLocation()); 12283 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12284 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12285 C->setQualifierLoc(NNSL); 12286 C->setNameInfo(DNI); 12287 12288 unsigned NumVars = C->varlist_size(); 12289 SmallVector<Expr *, 16> Vars; 12290 Vars.reserve(NumVars); 12291 for (unsigned I = 0; I != NumVars; ++I) 12292 Vars.push_back(Record.readSubExpr()); 12293 C->setVarRefs(Vars); 12294 Vars.clear(); 12295 for (unsigned I = 0; I != NumVars; ++I) 12296 Vars.push_back(Record.readSubExpr()); 12297 C->setPrivates(Vars); 12298 Vars.clear(); 12299 for (unsigned I = 0; I != NumVars; ++I) 12300 Vars.push_back(Record.readSubExpr()); 12301 C->setLHSExprs(Vars); 12302 Vars.clear(); 12303 for (unsigned I = 0; I != NumVars; ++I) 12304 Vars.push_back(Record.readSubExpr()); 12305 C->setRHSExprs(Vars); 12306 Vars.clear(); 12307 for (unsigned I = 0; I != NumVars; ++I) 12308 Vars.push_back(Record.readSubExpr()); 12309 C->setReductionOps(Vars); 12310 Vars.clear(); 12311 for (unsigned I = 0; I != NumVars; ++I) 12312 Vars.push_back(Record.readSubExpr()); 12313 C->setTaskgroupDescriptors(Vars); 12314 } 12315 12316 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12317 VisitOMPClauseWithPostUpdate(C); 12318 C->setLParenLoc(Record.readSourceLocation()); 12319 C->setColonLoc(Record.readSourceLocation()); 12320 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12321 C->setModifierLoc(Record.readSourceLocation()); 12322 unsigned NumVars = C->varlist_size(); 12323 SmallVector<Expr *, 16> Vars; 12324 Vars.reserve(NumVars); 12325 for (unsigned i = 0; i != NumVars; ++i) 12326 Vars.push_back(Record.readSubExpr()); 12327 C->setVarRefs(Vars); 12328 Vars.clear(); 12329 for (unsigned i = 0; i != NumVars; ++i) 12330 Vars.push_back(Record.readSubExpr()); 12331 C->setPrivates(Vars); 12332 Vars.clear(); 12333 for (unsigned i = 0; i != NumVars; ++i) 12334 Vars.push_back(Record.readSubExpr()); 12335 C->setInits(Vars); 12336 Vars.clear(); 12337 for (unsigned i = 0; i != NumVars; ++i) 12338 Vars.push_back(Record.readSubExpr()); 12339 C->setUpdates(Vars); 12340 Vars.clear(); 12341 for (unsigned i = 0; i != NumVars; ++i) 12342 Vars.push_back(Record.readSubExpr()); 12343 C->setFinals(Vars); 12344 C->setStep(Record.readSubExpr()); 12345 C->setCalcStep(Record.readSubExpr()); 12346 Vars.clear(); 12347 for (unsigned I = 0; I != NumVars + 1; ++I) 12348 Vars.push_back(Record.readSubExpr()); 12349 C->setUsedExprs(Vars); 12350 } 12351 12352 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12353 C->setLParenLoc(Record.readSourceLocation()); 12354 C->setColonLoc(Record.readSourceLocation()); 12355 unsigned NumVars = C->varlist_size(); 12356 SmallVector<Expr *, 16> Vars; 12357 Vars.reserve(NumVars); 12358 for (unsigned i = 0; i != NumVars; ++i) 12359 Vars.push_back(Record.readSubExpr()); 12360 C->setVarRefs(Vars); 12361 C->setAlignment(Record.readSubExpr()); 12362 } 12363 12364 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12365 C->setLParenLoc(Record.readSourceLocation()); 12366 unsigned NumVars = C->varlist_size(); 12367 SmallVector<Expr *, 16> Exprs; 12368 Exprs.reserve(NumVars); 12369 for (unsigned i = 0; i != NumVars; ++i) 12370 Exprs.push_back(Record.readSubExpr()); 12371 C->setVarRefs(Exprs); 12372 Exprs.clear(); 12373 for (unsigned i = 0; i != NumVars; ++i) 12374 Exprs.push_back(Record.readSubExpr()); 12375 C->setSourceExprs(Exprs); 12376 Exprs.clear(); 12377 for (unsigned i = 0; i != NumVars; ++i) 12378 Exprs.push_back(Record.readSubExpr()); 12379 C->setDestinationExprs(Exprs); 12380 Exprs.clear(); 12381 for (unsigned i = 0; i != NumVars; ++i) 12382 Exprs.push_back(Record.readSubExpr()); 12383 C->setAssignmentOps(Exprs); 12384 } 12385 12386 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12387 C->setLParenLoc(Record.readSourceLocation()); 12388 unsigned NumVars = C->varlist_size(); 12389 SmallVector<Expr *, 16> Exprs; 12390 Exprs.reserve(NumVars); 12391 for (unsigned i = 0; i != NumVars; ++i) 12392 Exprs.push_back(Record.readSubExpr()); 12393 C->setVarRefs(Exprs); 12394 Exprs.clear(); 12395 for (unsigned i = 0; i != NumVars; ++i) 12396 Exprs.push_back(Record.readSubExpr()); 12397 C->setSourceExprs(Exprs); 12398 Exprs.clear(); 12399 for (unsigned i = 0; i != NumVars; ++i) 12400 Exprs.push_back(Record.readSubExpr()); 12401 C->setDestinationExprs(Exprs); 12402 Exprs.clear(); 12403 for (unsigned i = 0; i != NumVars; ++i) 12404 Exprs.push_back(Record.readSubExpr()); 12405 C->setAssignmentOps(Exprs); 12406 } 12407 12408 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12409 C->setLParenLoc(Record.readSourceLocation()); 12410 unsigned NumVars = C->varlist_size(); 12411 SmallVector<Expr *, 16> Vars; 12412 Vars.reserve(NumVars); 12413 for (unsigned i = 0; i != NumVars; ++i) 12414 Vars.push_back(Record.readSubExpr()); 12415 C->setVarRefs(Vars); 12416 } 12417 12418 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12419 C->setDepobj(Record.readSubExpr()); 12420 C->setLParenLoc(Record.readSourceLocation()); 12421 } 12422 12423 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12424 C->setLParenLoc(Record.readSourceLocation()); 12425 C->setModifier(Record.readSubExpr()); 12426 C->setDependencyKind( 12427 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12428 C->setDependencyLoc(Record.readSourceLocation()); 12429 C->setColonLoc(Record.readSourceLocation()); 12430 unsigned NumVars = C->varlist_size(); 12431 SmallVector<Expr *, 16> Vars; 12432 Vars.reserve(NumVars); 12433 for (unsigned I = 0; I != NumVars; ++I) 12434 Vars.push_back(Record.readSubExpr()); 12435 C->setVarRefs(Vars); 12436 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12437 C->setLoopData(I, Record.readSubExpr()); 12438 } 12439 12440 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12441 VisitOMPClauseWithPreInit(C); 12442 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12443 C->setDevice(Record.readSubExpr()); 12444 C->setModifierLoc(Record.readSourceLocation()); 12445 C->setLParenLoc(Record.readSourceLocation()); 12446 } 12447 12448 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12449 C->setLParenLoc(Record.readSourceLocation()); 12450 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12451 C->setMapTypeModifier( 12452 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12453 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12454 } 12455 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12456 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12457 C->setMapType( 12458 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12459 C->setMapLoc(Record.readSourceLocation()); 12460 C->setColonLoc(Record.readSourceLocation()); 12461 auto NumVars = C->varlist_size(); 12462 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12463 auto TotalLists = C->getTotalComponentListNum(); 12464 auto TotalComponents = C->getTotalComponentsNum(); 12465 12466 SmallVector<Expr *, 16> Vars; 12467 Vars.reserve(NumVars); 12468 for (unsigned i = 0; i != NumVars; ++i) 12469 Vars.push_back(Record.readExpr()); 12470 C->setVarRefs(Vars); 12471 12472 SmallVector<Expr *, 16> UDMappers; 12473 UDMappers.reserve(NumVars); 12474 for (unsigned I = 0; I < NumVars; ++I) 12475 UDMappers.push_back(Record.readExpr()); 12476 C->setUDMapperRefs(UDMappers); 12477 12478 SmallVector<ValueDecl *, 16> Decls; 12479 Decls.reserve(UniqueDecls); 12480 for (unsigned i = 0; i < UniqueDecls; ++i) 12481 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12482 C->setUniqueDecls(Decls); 12483 12484 SmallVector<unsigned, 16> ListsPerDecl; 12485 ListsPerDecl.reserve(UniqueDecls); 12486 for (unsigned i = 0; i < UniqueDecls; ++i) 12487 ListsPerDecl.push_back(Record.readInt()); 12488 C->setDeclNumLists(ListsPerDecl); 12489 12490 SmallVector<unsigned, 32> ListSizes; 12491 ListSizes.reserve(TotalLists); 12492 for (unsigned i = 0; i < TotalLists; ++i) 12493 ListSizes.push_back(Record.readInt()); 12494 C->setComponentListSizes(ListSizes); 12495 12496 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12497 Components.reserve(TotalComponents); 12498 for (unsigned i = 0; i < TotalComponents; ++i) { 12499 Expr *AssociatedExprPr = Record.readExpr(); 12500 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12501 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12502 /*IsNonContiguous=*/false); 12503 } 12504 C->setComponents(Components, ListSizes); 12505 } 12506 12507 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12508 C->setLParenLoc(Record.readSourceLocation()); 12509 C->setColonLoc(Record.readSourceLocation()); 12510 C->setAllocator(Record.readSubExpr()); 12511 unsigned NumVars = C->varlist_size(); 12512 SmallVector<Expr *, 16> Vars; 12513 Vars.reserve(NumVars); 12514 for (unsigned i = 0; i != NumVars; ++i) 12515 Vars.push_back(Record.readSubExpr()); 12516 C->setVarRefs(Vars); 12517 } 12518 12519 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12520 VisitOMPClauseWithPreInit(C); 12521 C->setNumTeams(Record.readSubExpr()); 12522 C->setLParenLoc(Record.readSourceLocation()); 12523 } 12524 12525 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12526 VisitOMPClauseWithPreInit(C); 12527 C->setThreadLimit(Record.readSubExpr()); 12528 C->setLParenLoc(Record.readSourceLocation()); 12529 } 12530 12531 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12532 VisitOMPClauseWithPreInit(C); 12533 C->setPriority(Record.readSubExpr()); 12534 C->setLParenLoc(Record.readSourceLocation()); 12535 } 12536 12537 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12538 VisitOMPClauseWithPreInit(C); 12539 C->setGrainsize(Record.readSubExpr()); 12540 C->setLParenLoc(Record.readSourceLocation()); 12541 } 12542 12543 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12544 VisitOMPClauseWithPreInit(C); 12545 C->setNumTasks(Record.readSubExpr()); 12546 C->setLParenLoc(Record.readSourceLocation()); 12547 } 12548 12549 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12550 C->setHint(Record.readSubExpr()); 12551 C->setLParenLoc(Record.readSourceLocation()); 12552 } 12553 12554 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12555 VisitOMPClauseWithPreInit(C); 12556 C->setDistScheduleKind( 12557 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12558 C->setChunkSize(Record.readSubExpr()); 12559 C->setLParenLoc(Record.readSourceLocation()); 12560 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12561 C->setCommaLoc(Record.readSourceLocation()); 12562 } 12563 12564 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12565 C->setDefaultmapKind( 12566 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12567 C->setDefaultmapModifier( 12568 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12569 C->setLParenLoc(Record.readSourceLocation()); 12570 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12571 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12572 } 12573 12574 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12575 C->setLParenLoc(Record.readSourceLocation()); 12576 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12577 C->setMotionModifier( 12578 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12579 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12580 } 12581 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12582 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12583 C->setColonLoc(Record.readSourceLocation()); 12584 auto NumVars = C->varlist_size(); 12585 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12586 auto TotalLists = C->getTotalComponentListNum(); 12587 auto TotalComponents = C->getTotalComponentsNum(); 12588 12589 SmallVector<Expr *, 16> Vars; 12590 Vars.reserve(NumVars); 12591 for (unsigned i = 0; i != NumVars; ++i) 12592 Vars.push_back(Record.readSubExpr()); 12593 C->setVarRefs(Vars); 12594 12595 SmallVector<Expr *, 16> UDMappers; 12596 UDMappers.reserve(NumVars); 12597 for (unsigned I = 0; I < NumVars; ++I) 12598 UDMappers.push_back(Record.readSubExpr()); 12599 C->setUDMapperRefs(UDMappers); 12600 12601 SmallVector<ValueDecl *, 16> Decls; 12602 Decls.reserve(UniqueDecls); 12603 for (unsigned i = 0; i < UniqueDecls; ++i) 12604 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12605 C->setUniqueDecls(Decls); 12606 12607 SmallVector<unsigned, 16> ListsPerDecl; 12608 ListsPerDecl.reserve(UniqueDecls); 12609 for (unsigned i = 0; i < UniqueDecls; ++i) 12610 ListsPerDecl.push_back(Record.readInt()); 12611 C->setDeclNumLists(ListsPerDecl); 12612 12613 SmallVector<unsigned, 32> ListSizes; 12614 ListSizes.reserve(TotalLists); 12615 for (unsigned i = 0; i < TotalLists; ++i) 12616 ListSizes.push_back(Record.readInt()); 12617 C->setComponentListSizes(ListSizes); 12618 12619 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12620 Components.reserve(TotalComponents); 12621 for (unsigned i = 0; i < TotalComponents; ++i) { 12622 Expr *AssociatedExprPr = Record.readSubExpr(); 12623 bool IsNonContiguous = Record.readBool(); 12624 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12625 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12626 } 12627 C->setComponents(Components, ListSizes); 12628 } 12629 12630 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12631 C->setLParenLoc(Record.readSourceLocation()); 12632 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12633 C->setMotionModifier( 12634 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12635 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12636 } 12637 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12638 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12639 C->setColonLoc(Record.readSourceLocation()); 12640 auto NumVars = C->varlist_size(); 12641 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12642 auto TotalLists = C->getTotalComponentListNum(); 12643 auto TotalComponents = C->getTotalComponentsNum(); 12644 12645 SmallVector<Expr *, 16> Vars; 12646 Vars.reserve(NumVars); 12647 for (unsigned i = 0; i != NumVars; ++i) 12648 Vars.push_back(Record.readSubExpr()); 12649 C->setVarRefs(Vars); 12650 12651 SmallVector<Expr *, 16> UDMappers; 12652 UDMappers.reserve(NumVars); 12653 for (unsigned I = 0; I < NumVars; ++I) 12654 UDMappers.push_back(Record.readSubExpr()); 12655 C->setUDMapperRefs(UDMappers); 12656 12657 SmallVector<ValueDecl *, 16> Decls; 12658 Decls.reserve(UniqueDecls); 12659 for (unsigned i = 0; i < UniqueDecls; ++i) 12660 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12661 C->setUniqueDecls(Decls); 12662 12663 SmallVector<unsigned, 16> ListsPerDecl; 12664 ListsPerDecl.reserve(UniqueDecls); 12665 for (unsigned i = 0; i < UniqueDecls; ++i) 12666 ListsPerDecl.push_back(Record.readInt()); 12667 C->setDeclNumLists(ListsPerDecl); 12668 12669 SmallVector<unsigned, 32> ListSizes; 12670 ListSizes.reserve(TotalLists); 12671 for (unsigned i = 0; i < TotalLists; ++i) 12672 ListSizes.push_back(Record.readInt()); 12673 C->setComponentListSizes(ListSizes); 12674 12675 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12676 Components.reserve(TotalComponents); 12677 for (unsigned i = 0; i < TotalComponents; ++i) { 12678 Expr *AssociatedExprPr = Record.readSubExpr(); 12679 bool IsNonContiguous = Record.readBool(); 12680 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12681 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12682 } 12683 C->setComponents(Components, ListSizes); 12684 } 12685 12686 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12687 C->setLParenLoc(Record.readSourceLocation()); 12688 auto NumVars = C->varlist_size(); 12689 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12690 auto TotalLists = C->getTotalComponentListNum(); 12691 auto TotalComponents = C->getTotalComponentsNum(); 12692 12693 SmallVector<Expr *, 16> Vars; 12694 Vars.reserve(NumVars); 12695 for (unsigned i = 0; i != NumVars; ++i) 12696 Vars.push_back(Record.readSubExpr()); 12697 C->setVarRefs(Vars); 12698 Vars.clear(); 12699 for (unsigned i = 0; i != NumVars; ++i) 12700 Vars.push_back(Record.readSubExpr()); 12701 C->setPrivateCopies(Vars); 12702 Vars.clear(); 12703 for (unsigned i = 0; i != NumVars; ++i) 12704 Vars.push_back(Record.readSubExpr()); 12705 C->setInits(Vars); 12706 12707 SmallVector<ValueDecl *, 16> Decls; 12708 Decls.reserve(UniqueDecls); 12709 for (unsigned i = 0; i < UniqueDecls; ++i) 12710 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12711 C->setUniqueDecls(Decls); 12712 12713 SmallVector<unsigned, 16> ListsPerDecl; 12714 ListsPerDecl.reserve(UniqueDecls); 12715 for (unsigned i = 0; i < UniqueDecls; ++i) 12716 ListsPerDecl.push_back(Record.readInt()); 12717 C->setDeclNumLists(ListsPerDecl); 12718 12719 SmallVector<unsigned, 32> ListSizes; 12720 ListSizes.reserve(TotalLists); 12721 for (unsigned i = 0; i < TotalLists; ++i) 12722 ListSizes.push_back(Record.readInt()); 12723 C->setComponentListSizes(ListSizes); 12724 12725 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12726 Components.reserve(TotalComponents); 12727 for (unsigned i = 0; i < TotalComponents; ++i) { 12728 auto *AssociatedExprPr = Record.readSubExpr(); 12729 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12730 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12731 /*IsNonContiguous=*/false); 12732 } 12733 C->setComponents(Components, ListSizes); 12734 } 12735 12736 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12737 C->setLParenLoc(Record.readSourceLocation()); 12738 auto NumVars = C->varlist_size(); 12739 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12740 auto TotalLists = C->getTotalComponentListNum(); 12741 auto TotalComponents = C->getTotalComponentsNum(); 12742 12743 SmallVector<Expr *, 16> Vars; 12744 Vars.reserve(NumVars); 12745 for (unsigned i = 0; i != NumVars; ++i) 12746 Vars.push_back(Record.readSubExpr()); 12747 C->setVarRefs(Vars); 12748 12749 SmallVector<ValueDecl *, 16> Decls; 12750 Decls.reserve(UniqueDecls); 12751 for (unsigned i = 0; i < UniqueDecls; ++i) 12752 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12753 C->setUniqueDecls(Decls); 12754 12755 SmallVector<unsigned, 16> ListsPerDecl; 12756 ListsPerDecl.reserve(UniqueDecls); 12757 for (unsigned i = 0; i < UniqueDecls; ++i) 12758 ListsPerDecl.push_back(Record.readInt()); 12759 C->setDeclNumLists(ListsPerDecl); 12760 12761 SmallVector<unsigned, 32> ListSizes; 12762 ListSizes.reserve(TotalLists); 12763 for (unsigned i = 0; i < TotalLists; ++i) 12764 ListSizes.push_back(Record.readInt()); 12765 C->setComponentListSizes(ListSizes); 12766 12767 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12768 Components.reserve(TotalComponents); 12769 for (unsigned i = 0; i < TotalComponents; ++i) { 12770 Expr *AssociatedExpr = Record.readSubExpr(); 12771 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12772 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12773 /*IsNonContiguous*/ false); 12774 } 12775 C->setComponents(Components, ListSizes); 12776 } 12777 12778 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12779 C->setLParenLoc(Record.readSourceLocation()); 12780 auto NumVars = C->varlist_size(); 12781 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12782 auto TotalLists = C->getTotalComponentListNum(); 12783 auto TotalComponents = C->getTotalComponentsNum(); 12784 12785 SmallVector<Expr *, 16> Vars; 12786 Vars.reserve(NumVars); 12787 for (unsigned i = 0; i != NumVars; ++i) 12788 Vars.push_back(Record.readSubExpr()); 12789 C->setVarRefs(Vars); 12790 Vars.clear(); 12791 12792 SmallVector<ValueDecl *, 16> Decls; 12793 Decls.reserve(UniqueDecls); 12794 for (unsigned i = 0; i < UniqueDecls; ++i) 12795 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12796 C->setUniqueDecls(Decls); 12797 12798 SmallVector<unsigned, 16> ListsPerDecl; 12799 ListsPerDecl.reserve(UniqueDecls); 12800 for (unsigned i = 0; i < UniqueDecls; ++i) 12801 ListsPerDecl.push_back(Record.readInt()); 12802 C->setDeclNumLists(ListsPerDecl); 12803 12804 SmallVector<unsigned, 32> ListSizes; 12805 ListSizes.reserve(TotalLists); 12806 for (unsigned i = 0; i < TotalLists; ++i) 12807 ListSizes.push_back(Record.readInt()); 12808 C->setComponentListSizes(ListSizes); 12809 12810 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12811 Components.reserve(TotalComponents); 12812 for (unsigned i = 0; i < TotalComponents; ++i) { 12813 Expr *AssociatedExpr = Record.readSubExpr(); 12814 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12815 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12816 /*IsNonContiguous=*/false); 12817 } 12818 C->setComponents(Components, ListSizes); 12819 } 12820 12821 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12822 C->setLParenLoc(Record.readSourceLocation()); 12823 unsigned NumVars = C->varlist_size(); 12824 SmallVector<Expr *, 16> Vars; 12825 Vars.reserve(NumVars); 12826 for (unsigned i = 0; i != NumVars; ++i) 12827 Vars.push_back(Record.readSubExpr()); 12828 C->setVarRefs(Vars); 12829 Vars.clear(); 12830 Vars.reserve(NumVars); 12831 for (unsigned i = 0; i != NumVars; ++i) 12832 Vars.push_back(Record.readSubExpr()); 12833 C->setPrivateRefs(Vars); 12834 } 12835 12836 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12837 C->setLParenLoc(Record.readSourceLocation()); 12838 unsigned NumVars = C->varlist_size(); 12839 SmallVector<Expr *, 16> Vars; 12840 Vars.reserve(NumVars); 12841 for (unsigned i = 0; i != NumVars; ++i) 12842 Vars.push_back(Record.readSubExpr()); 12843 C->setVarRefs(Vars); 12844 } 12845 12846 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12847 C->setLParenLoc(Record.readSourceLocation()); 12848 unsigned NumVars = C->varlist_size(); 12849 SmallVector<Expr *, 16> Vars; 12850 Vars.reserve(NumVars); 12851 for (unsigned i = 0; i != NumVars; ++i) 12852 Vars.push_back(Record.readSubExpr()); 12853 C->setVarRefs(Vars); 12854 } 12855 12856 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12857 C->setLParenLoc(Record.readSourceLocation()); 12858 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12859 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12860 Data.reserve(NumOfAllocators); 12861 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12862 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12863 D.Allocator = Record.readSubExpr(); 12864 D.AllocatorTraits = Record.readSubExpr(); 12865 D.LParenLoc = Record.readSourceLocation(); 12866 D.RParenLoc = Record.readSourceLocation(); 12867 } 12868 C->setAllocatorsData(Data); 12869 } 12870 12871 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12872 C->setLParenLoc(Record.readSourceLocation()); 12873 C->setModifier(Record.readSubExpr()); 12874 C->setColonLoc(Record.readSourceLocation()); 12875 unsigned NumOfLocators = C->varlist_size(); 12876 SmallVector<Expr *, 4> Locators; 12877 Locators.reserve(NumOfLocators); 12878 for (unsigned I = 0; I != NumOfLocators; ++I) 12879 Locators.push_back(Record.readSubExpr()); 12880 C->setVarRefs(Locators); 12881 } 12882 12883 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12884 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12885 C->setLParenLoc(Record.readSourceLocation()); 12886 C->setKindKwLoc(Record.readSourceLocation()); 12887 } 12888 12889 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) { 12890 VisitOMPClauseWithPreInit(C); 12891 C->setThreadID(Record.readSubExpr()); 12892 C->setLParenLoc(Record.readSourceLocation()); 12893 } 12894 12895 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) { 12896 C->setBindKind(Record.readEnum<OpenMPBindClauseKind>()); 12897 C->setLParenLoc(Record.readSourceLocation()); 12898 C->setBindKindLoc(Record.readSourceLocation()); 12899 } 12900 12901 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) { 12902 C->setAlignment(Record.readExpr()); 12903 C->setLParenLoc(Record.readSourceLocation()); 12904 } 12905 12906 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12907 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12908 TI.Sets.resize(readUInt32()); 12909 for (auto &Set : TI.Sets) { 12910 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12911 Set.Selectors.resize(readUInt32()); 12912 for (auto &Selector : Set.Selectors) { 12913 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12914 Selector.ScoreOrCondition = nullptr; 12915 if (readBool()) 12916 Selector.ScoreOrCondition = readExprRef(); 12917 Selector.Properties.resize(readUInt32()); 12918 for (auto &Property : Selector.Properties) 12919 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12920 } 12921 } 12922 return &TI; 12923 } 12924 12925 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12926 if (!Data) 12927 return; 12928 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12929 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12930 skipInts(3); 12931 } 12932 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12933 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12934 Clauses[I] = readOMPClause(); 12935 Data->setClauses(Clauses); 12936 if (Data->hasAssociatedStmt()) 12937 Data->setAssociatedStmt(readStmt()); 12938 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12939 Data->getChildren()[I] = readStmt(); 12940 } 12941