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 using llvm::RoundingMode; 146 147 //===----------------------------------------------------------------------===// 148 // ChainedASTReaderListener implementation 149 //===----------------------------------------------------------------------===// 150 151 bool 152 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 153 return First->ReadFullVersionInformation(FullVersion) || 154 Second->ReadFullVersionInformation(FullVersion); 155 } 156 157 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 158 First->ReadModuleName(ModuleName); 159 Second->ReadModuleName(ModuleName); 160 } 161 162 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 163 First->ReadModuleMapFile(ModuleMapPath); 164 Second->ReadModuleMapFile(ModuleMapPath); 165 } 166 167 bool 168 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 169 bool Complain, 170 bool AllowCompatibleDifferences) { 171 return First->ReadLanguageOptions(LangOpts, Complain, 172 AllowCompatibleDifferences) || 173 Second->ReadLanguageOptions(LangOpts, Complain, 174 AllowCompatibleDifferences); 175 } 176 177 bool ChainedASTReaderListener::ReadTargetOptions( 178 const TargetOptions &TargetOpts, bool Complain, 179 bool AllowCompatibleDifferences) { 180 return First->ReadTargetOptions(TargetOpts, Complain, 181 AllowCompatibleDifferences) || 182 Second->ReadTargetOptions(TargetOpts, Complain, 183 AllowCompatibleDifferences); 184 } 185 186 bool ChainedASTReaderListener::ReadDiagnosticOptions( 187 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 188 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 189 Second->ReadDiagnosticOptions(DiagOpts, Complain); 190 } 191 192 bool 193 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 194 bool Complain) { 195 return First->ReadFileSystemOptions(FSOpts, Complain) || 196 Second->ReadFileSystemOptions(FSOpts, Complain); 197 } 198 199 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 200 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 201 bool Complain) { 202 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 203 Complain) || 204 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 205 Complain); 206 } 207 208 bool ChainedASTReaderListener::ReadPreprocessorOptions( 209 const PreprocessorOptions &PPOpts, bool Complain, 210 std::string &SuggestedPredefines) { 211 return First->ReadPreprocessorOptions(PPOpts, Complain, 212 SuggestedPredefines) || 213 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 214 } 215 216 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 217 unsigned Value) { 218 First->ReadCounter(M, Value); 219 Second->ReadCounter(M, Value); 220 } 221 222 bool ChainedASTReaderListener::needsInputFileVisitation() { 223 return First->needsInputFileVisitation() || 224 Second->needsInputFileVisitation(); 225 } 226 227 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 228 return First->needsSystemInputFileVisitation() || 229 Second->needsSystemInputFileVisitation(); 230 } 231 232 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 233 ModuleKind Kind) { 234 First->visitModuleFile(Filename, Kind); 235 Second->visitModuleFile(Filename, Kind); 236 } 237 238 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 239 bool isSystem, 240 bool isOverridden, 241 bool isExplicitModule) { 242 bool Continue = false; 243 if (First->needsInputFileVisitation() && 244 (!isSystem || First->needsSystemInputFileVisitation())) 245 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 246 isExplicitModule); 247 if (Second->needsInputFileVisitation() && 248 (!isSystem || Second->needsSystemInputFileVisitation())) 249 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 250 isExplicitModule); 251 return Continue; 252 } 253 254 void ChainedASTReaderListener::readModuleFileExtension( 255 const ModuleFileExtensionMetadata &Metadata) { 256 First->readModuleFileExtension(Metadata); 257 Second->readModuleFileExtension(Metadata); 258 } 259 260 //===----------------------------------------------------------------------===// 261 // PCH validator implementation 262 //===----------------------------------------------------------------------===// 263 264 ASTReaderListener::~ASTReaderListener() = default; 265 266 /// Compare the given set of language options against an existing set of 267 /// language options. 268 /// 269 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 270 /// \param AllowCompatibleDifferences If true, differences between compatible 271 /// language options will be permitted. 272 /// 273 /// \returns true if the languagae options mis-match, false otherwise. 274 static bool checkLanguageOptions(const LangOptions &LangOpts, 275 const LangOptions &ExistingLangOpts, 276 DiagnosticsEngine *Diags, 277 bool AllowCompatibleDifferences = true) { 278 #define LANGOPT(Name, Bits, Default, Description) \ 279 if (ExistingLangOpts.Name != LangOpts.Name) { \ 280 if (Diags) \ 281 Diags->Report(diag::err_pch_langopt_mismatch) \ 282 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 283 return true; \ 284 } 285 286 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 287 if (ExistingLangOpts.Name != LangOpts.Name) { \ 288 if (Diags) \ 289 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 290 << Description; \ 291 return true; \ 292 } 293 294 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 295 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 296 if (Diags) \ 297 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 298 << Description; \ 299 return true; \ 300 } 301 302 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 303 if (!AllowCompatibleDifferences) \ 304 LANGOPT(Name, Bits, Default, Description) 305 306 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 307 if (!AllowCompatibleDifferences) \ 308 ENUM_LANGOPT(Name, Bits, Default, Description) 309 310 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 311 if (!AllowCompatibleDifferences) \ 312 VALUE_LANGOPT(Name, Bits, Default, Description) 313 314 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 315 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 316 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 317 #include "clang/Basic/LangOptions.def" 318 319 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 320 if (Diags) 321 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 322 return true; 323 } 324 325 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 326 if (Diags) 327 Diags->Report(diag::err_pch_langopt_value_mismatch) 328 << "target Objective-C runtime"; 329 return true; 330 } 331 332 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 333 LangOpts.CommentOpts.BlockCommandNames) { 334 if (Diags) 335 Diags->Report(diag::err_pch_langopt_value_mismatch) 336 << "block command names"; 337 return true; 338 } 339 340 // Sanitizer feature mismatches are treated as compatible differences. If 341 // compatible differences aren't allowed, we still only want to check for 342 // mismatches of non-modular sanitizers (the only ones which can affect AST 343 // generation). 344 if (!AllowCompatibleDifferences) { 345 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 346 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 347 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 348 ExistingSanitizers.clear(ModularSanitizers); 349 ImportedSanitizers.clear(ModularSanitizers); 350 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 351 const std::string Flag = "-fsanitize="; 352 if (Diags) { 353 #define SANITIZER(NAME, ID) \ 354 { \ 355 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 356 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 357 if (InExistingModule != InImportedModule) \ 358 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 359 << InExistingModule << (Flag + NAME); \ 360 } 361 #include "clang/Basic/Sanitizers.def" 362 } 363 return true; 364 } 365 } 366 367 return false; 368 } 369 370 /// Compare the given set of target options against an existing set of 371 /// target options. 372 /// 373 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 374 /// 375 /// \returns true if the target options mis-match, false otherwise. 376 static bool checkTargetOptions(const TargetOptions &TargetOpts, 377 const TargetOptions &ExistingTargetOpts, 378 DiagnosticsEngine *Diags, 379 bool AllowCompatibleDifferences = true) { 380 #define CHECK_TARGET_OPT(Field, Name) \ 381 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 382 if (Diags) \ 383 Diags->Report(diag::err_pch_targetopt_mismatch) \ 384 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 385 return true; \ 386 } 387 388 // The triple and ABI must match exactly. 389 CHECK_TARGET_OPT(Triple, "target"); 390 CHECK_TARGET_OPT(ABI, "target ABI"); 391 392 // We can tolerate different CPUs in many cases, notably when one CPU 393 // supports a strict superset of another. When allowing compatible 394 // differences skip this check. 395 if (!AllowCompatibleDifferences) { 396 CHECK_TARGET_OPT(CPU, "target CPU"); 397 CHECK_TARGET_OPT(TuneCPU, "tune CPU"); 398 } 399 400 #undef CHECK_TARGET_OPT 401 402 // Compare feature sets. 403 SmallVector<StringRef, 4> ExistingFeatures( 404 ExistingTargetOpts.FeaturesAsWritten.begin(), 405 ExistingTargetOpts.FeaturesAsWritten.end()); 406 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 407 TargetOpts.FeaturesAsWritten.end()); 408 llvm::sort(ExistingFeatures); 409 llvm::sort(ReadFeatures); 410 411 // We compute the set difference in both directions explicitly so that we can 412 // diagnose the differences differently. 413 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 414 std::set_difference( 415 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 416 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 417 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 418 ExistingFeatures.begin(), ExistingFeatures.end(), 419 std::back_inserter(UnmatchedReadFeatures)); 420 421 // If we are allowing compatible differences and the read feature set is 422 // a strict subset of the existing feature set, there is nothing to diagnose. 423 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 424 return false; 425 426 if (Diags) { 427 for (StringRef Feature : UnmatchedReadFeatures) 428 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 429 << /* is-existing-feature */ false << Feature; 430 for (StringRef Feature : UnmatchedExistingFeatures) 431 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 432 << /* is-existing-feature */ true << Feature; 433 } 434 435 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 436 } 437 438 bool 439 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 440 bool Complain, 441 bool AllowCompatibleDifferences) { 442 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 443 return checkLanguageOptions(LangOpts, ExistingLangOpts, 444 Complain ? &Reader.Diags : nullptr, 445 AllowCompatibleDifferences); 446 } 447 448 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 449 bool Complain, 450 bool AllowCompatibleDifferences) { 451 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 452 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 453 Complain ? &Reader.Diags : nullptr, 454 AllowCompatibleDifferences); 455 } 456 457 namespace { 458 459 using MacroDefinitionsMap = 460 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 461 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 462 463 } // namespace 464 465 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 466 DiagnosticsEngine &Diags, 467 bool Complain) { 468 using Level = DiagnosticsEngine::Level; 469 470 // Check current mappings for new -Werror mappings, and the stored mappings 471 // for cases that were explicitly mapped to *not* be errors that are now 472 // errors because of options like -Werror. 473 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 474 475 for (DiagnosticsEngine *MappingSource : MappingSources) { 476 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 477 diag::kind DiagID = DiagIDMappingPair.first; 478 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 479 if (CurLevel < DiagnosticsEngine::Error) 480 continue; // not significant 481 Level StoredLevel = 482 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 483 if (StoredLevel < DiagnosticsEngine::Error) { 484 if (Complain) 485 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 486 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 487 return true; 488 } 489 } 490 } 491 492 return false; 493 } 494 495 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 496 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 497 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 498 return true; 499 return Ext >= diag::Severity::Error; 500 } 501 502 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 503 DiagnosticsEngine &Diags, 504 bool IsSystem, bool Complain) { 505 // Top-level options 506 if (IsSystem) { 507 if (Diags.getSuppressSystemWarnings()) 508 return false; 509 // If -Wsystem-headers was not enabled before, be conservative 510 if (StoredDiags.getSuppressSystemWarnings()) { 511 if (Complain) 512 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 513 return true; 514 } 515 } 516 517 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 518 if (Complain) 519 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 520 return true; 521 } 522 523 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 524 !StoredDiags.getEnableAllWarnings()) { 525 if (Complain) 526 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 527 return true; 528 } 529 530 if (isExtHandlingFromDiagsError(Diags) && 531 !isExtHandlingFromDiagsError(StoredDiags)) { 532 if (Complain) 533 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 534 return true; 535 } 536 537 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 538 } 539 540 /// Return the top import module if it is implicit, nullptr otherwise. 541 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 542 Preprocessor &PP) { 543 // If the original import came from a file explicitly generated by the user, 544 // don't check the diagnostic mappings. 545 // FIXME: currently this is approximated by checking whether this is not a 546 // module import of an implicitly-loaded module file. 547 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 548 // the transitive closure of its imports, since unrelated modules cannot be 549 // imported until after this module finishes validation. 550 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 551 while (!TopImport->ImportedBy.empty()) 552 TopImport = TopImport->ImportedBy[0]; 553 if (TopImport->Kind != MK_ImplicitModule) 554 return nullptr; 555 556 StringRef ModuleName = TopImport->ModuleName; 557 assert(!ModuleName.empty() && "diagnostic options read before module name"); 558 559 Module *M = 560 PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc); 561 assert(M && "missing module"); 562 return M; 563 } 564 565 bool PCHValidator::ReadDiagnosticOptions( 566 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 567 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 568 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 569 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 570 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 571 // This should never fail, because we would have processed these options 572 // before writing them to an ASTFile. 573 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 574 575 ModuleManager &ModuleMgr = Reader.getModuleManager(); 576 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 577 578 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 579 if (!TopM) 580 return false; 581 582 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 583 // contains the union of their flags. 584 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 585 Complain); 586 } 587 588 /// Collect the macro definitions provided by the given preprocessor 589 /// options. 590 static void 591 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 592 MacroDefinitionsMap &Macros, 593 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 594 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 595 StringRef Macro = PPOpts.Macros[I].first; 596 bool IsUndef = PPOpts.Macros[I].second; 597 598 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 599 StringRef MacroName = MacroPair.first; 600 StringRef MacroBody = MacroPair.second; 601 602 // For an #undef'd macro, we only care about the name. 603 if (IsUndef) { 604 if (MacroNames && !Macros.count(MacroName)) 605 MacroNames->push_back(MacroName); 606 607 Macros[MacroName] = std::make_pair("", true); 608 continue; 609 } 610 611 // For a #define'd macro, figure out the actual definition. 612 if (MacroName.size() == Macro.size()) 613 MacroBody = "1"; 614 else { 615 // Note: GCC drops anything following an end-of-line character. 616 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 617 MacroBody = MacroBody.substr(0, End); 618 } 619 620 if (MacroNames && !Macros.count(MacroName)) 621 MacroNames->push_back(MacroName); 622 Macros[MacroName] = std::make_pair(MacroBody, false); 623 } 624 } 625 626 /// Check the preprocessor options deserialized from the control block 627 /// against the preprocessor options in an existing preprocessor. 628 /// 629 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 630 /// \param Validate If true, validate preprocessor options. If false, allow 631 /// macros defined by \p ExistingPPOpts to override those defined by 632 /// \p PPOpts in SuggestedPredefines. 633 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 634 const PreprocessorOptions &ExistingPPOpts, 635 DiagnosticsEngine *Diags, 636 FileManager &FileMgr, 637 std::string &SuggestedPredefines, 638 const LangOptions &LangOpts, 639 bool Validate = true) { 640 // Check macro definitions. 641 MacroDefinitionsMap ASTFileMacros; 642 collectMacroDefinitions(PPOpts, ASTFileMacros); 643 MacroDefinitionsMap ExistingMacros; 644 SmallVector<StringRef, 4> ExistingMacroNames; 645 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 646 647 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 648 // Dig out the macro definition in the existing preprocessor options. 649 StringRef MacroName = ExistingMacroNames[I]; 650 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 651 652 // Check whether we know anything about this macro name or not. 653 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 654 ASTFileMacros.find(MacroName); 655 if (!Validate || Known == ASTFileMacros.end()) { 656 // FIXME: Check whether this identifier was referenced anywhere in the 657 // AST file. If so, we should reject the AST file. Unfortunately, this 658 // information isn't in the control block. What shall we do about it? 659 660 if (Existing.second) { 661 SuggestedPredefines += "#undef "; 662 SuggestedPredefines += MacroName.str(); 663 SuggestedPredefines += '\n'; 664 } else { 665 SuggestedPredefines += "#define "; 666 SuggestedPredefines += MacroName.str(); 667 SuggestedPredefines += ' '; 668 SuggestedPredefines += Existing.first.str(); 669 SuggestedPredefines += '\n'; 670 } 671 continue; 672 } 673 674 // If the macro was defined in one but undef'd in the other, we have a 675 // conflict. 676 if (Existing.second != Known->second.second) { 677 if (Diags) { 678 Diags->Report(diag::err_pch_macro_def_undef) 679 << MacroName << Known->second.second; 680 } 681 return true; 682 } 683 684 // If the macro was #undef'd in both, or if the macro bodies are identical, 685 // it's fine. 686 if (Existing.second || Existing.first == Known->second.first) 687 continue; 688 689 // The macro bodies differ; complain. 690 if (Diags) { 691 Diags->Report(diag::err_pch_macro_def_conflict) 692 << MacroName << Known->second.first << Existing.first; 693 } 694 return true; 695 } 696 697 // Check whether we're using predefines. 698 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 699 if (Diags) { 700 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 701 } 702 return true; 703 } 704 705 // Detailed record is important since it is used for the module cache hash. 706 if (LangOpts.Modules && 707 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 708 if (Diags) { 709 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 710 } 711 return true; 712 } 713 714 // Compute the #include and #include_macros lines we need. 715 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 716 StringRef File = ExistingPPOpts.Includes[I]; 717 718 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 719 !ExistingPPOpts.PCHThroughHeader.empty()) { 720 // In case the through header is an include, we must add all the includes 721 // to the predefines so the start point can be determined. 722 SuggestedPredefines += "#include \""; 723 SuggestedPredefines += File; 724 SuggestedPredefines += "\"\n"; 725 continue; 726 } 727 728 if (File == ExistingPPOpts.ImplicitPCHInclude) 729 continue; 730 731 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 732 != PPOpts.Includes.end()) 733 continue; 734 735 SuggestedPredefines += "#include \""; 736 SuggestedPredefines += File; 737 SuggestedPredefines += "\"\n"; 738 } 739 740 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 741 StringRef File = ExistingPPOpts.MacroIncludes[I]; 742 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 743 File) 744 != PPOpts.MacroIncludes.end()) 745 continue; 746 747 SuggestedPredefines += "#__include_macros \""; 748 SuggestedPredefines += File; 749 SuggestedPredefines += "\"\n##\n"; 750 } 751 752 return false; 753 } 754 755 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 756 bool Complain, 757 std::string &SuggestedPredefines) { 758 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 759 760 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 761 Complain? &Reader.Diags : nullptr, 762 PP.getFileManager(), 763 SuggestedPredefines, 764 PP.getLangOpts()); 765 } 766 767 bool SimpleASTReaderListener::ReadPreprocessorOptions( 768 const PreprocessorOptions &PPOpts, 769 bool Complain, 770 std::string &SuggestedPredefines) { 771 return checkPreprocessorOptions(PPOpts, 772 PP.getPreprocessorOpts(), 773 nullptr, 774 PP.getFileManager(), 775 SuggestedPredefines, 776 PP.getLangOpts(), 777 false); 778 } 779 780 /// Check the header search options deserialized from the control block 781 /// against the header search options in an existing preprocessor. 782 /// 783 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 784 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 785 StringRef SpecificModuleCachePath, 786 StringRef ExistingModuleCachePath, 787 DiagnosticsEngine *Diags, 788 const LangOptions &LangOpts, 789 const PreprocessorOptions &PPOpts) { 790 if (LangOpts.Modules) { 791 if (SpecificModuleCachePath != ExistingModuleCachePath && 792 !PPOpts.AllowPCHWithDifferentModulesCachePath) { 793 if (Diags) 794 Diags->Report(diag::err_pch_modulecache_mismatch) 795 << SpecificModuleCachePath << ExistingModuleCachePath; 796 return true; 797 } 798 } 799 800 return false; 801 } 802 803 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 804 StringRef SpecificModuleCachePath, 805 bool Complain) { 806 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 807 PP.getHeaderSearchInfo().getModuleCachePath(), 808 Complain ? &Reader.Diags : nullptr, 809 PP.getLangOpts(), PP.getPreprocessorOpts()); 810 } 811 812 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 813 PP.setCounterValue(Value); 814 } 815 816 //===----------------------------------------------------------------------===// 817 // AST reader implementation 818 //===----------------------------------------------------------------------===// 819 820 static uint64_t readULEB(const unsigned char *&P) { 821 unsigned Length = 0; 822 const char *Error = nullptr; 823 824 uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error); 825 if (Error) 826 llvm::report_fatal_error(Error); 827 P += Length; 828 return Val; 829 } 830 831 /// Read ULEB-encoded key length and data length. 832 static std::pair<unsigned, unsigned> 833 readULEBKeyDataLength(const unsigned char *&P) { 834 unsigned KeyLen = readULEB(P); 835 if ((unsigned)KeyLen != KeyLen) 836 llvm::report_fatal_error("key too large"); 837 838 unsigned DataLen = readULEB(P); 839 if ((unsigned)DataLen != DataLen) 840 llvm::report_fatal_error("data too large"); 841 842 return std::make_pair(KeyLen, DataLen); 843 } 844 845 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 846 bool TakeOwnership) { 847 DeserializationListener = Listener; 848 OwnsDeserializationListener = TakeOwnership; 849 } 850 851 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 852 return serialization::ComputeHash(Sel); 853 } 854 855 std::pair<unsigned, unsigned> 856 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 857 return readULEBKeyDataLength(d); 858 } 859 860 ASTSelectorLookupTrait::internal_key_type 861 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 862 using namespace llvm::support; 863 864 SelectorTable &SelTable = Reader.getContext().Selectors; 865 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 866 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 867 F, endian::readNext<uint32_t, little, unaligned>(d)); 868 if (N == 0) 869 return SelTable.getNullarySelector(FirstII); 870 else if (N == 1) 871 return SelTable.getUnarySelector(FirstII); 872 873 SmallVector<IdentifierInfo *, 16> Args; 874 Args.push_back(FirstII); 875 for (unsigned I = 1; I != N; ++I) 876 Args.push_back(Reader.getLocalIdentifier( 877 F, endian::readNext<uint32_t, little, unaligned>(d))); 878 879 return SelTable.getSelector(N, Args.data()); 880 } 881 882 ASTSelectorLookupTrait::data_type 883 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 884 unsigned DataLen) { 885 using namespace llvm::support; 886 887 data_type Result; 888 889 Result.ID = Reader.getGlobalSelectorID( 890 F, endian::readNext<uint32_t, little, unaligned>(d)); 891 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 892 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 893 Result.InstanceBits = FullInstanceBits & 0x3; 894 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 895 Result.FactoryBits = FullFactoryBits & 0x3; 896 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 897 unsigned NumInstanceMethods = FullInstanceBits >> 3; 898 unsigned NumFactoryMethods = FullFactoryBits >> 3; 899 900 // Load instance methods 901 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 902 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 903 F, endian::readNext<uint32_t, little, unaligned>(d))) 904 Result.Instance.push_back(Method); 905 } 906 907 // Load factory methods 908 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 909 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 910 F, endian::readNext<uint32_t, little, unaligned>(d))) 911 Result.Factory.push_back(Method); 912 } 913 914 return Result; 915 } 916 917 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 918 return llvm::djbHash(a); 919 } 920 921 std::pair<unsigned, unsigned> 922 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 923 return readULEBKeyDataLength(d); 924 } 925 926 ASTIdentifierLookupTraitBase::internal_key_type 927 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 928 assert(n >= 2 && d[n-1] == '\0'); 929 return StringRef((const char*) d, n-1); 930 } 931 932 /// Whether the given identifier is "interesting". 933 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 934 bool IsModule) { 935 return II.hadMacroDefinition() || II.isPoisoned() || 936 (!IsModule && II.getObjCOrBuiltinID()) || 937 II.hasRevertedTokenIDToIdentifier() || 938 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 939 II.getFETokenInfo()); 940 } 941 942 static bool readBit(unsigned &Bits) { 943 bool Value = Bits & 0x1; 944 Bits >>= 1; 945 return Value; 946 } 947 948 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 949 using namespace llvm::support; 950 951 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 952 return Reader.getGlobalIdentifierID(F, RawID >> 1); 953 } 954 955 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 956 if (!II.isFromAST()) { 957 II.setIsFromAST(); 958 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 959 if (isInterestingIdentifier(Reader, II, IsModule)) 960 II.setChangedSinceDeserialization(); 961 } 962 } 963 964 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 965 const unsigned char* d, 966 unsigned DataLen) { 967 using namespace llvm::support; 968 969 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 970 bool IsInteresting = RawID & 0x01; 971 972 // Wipe out the "is interesting" bit. 973 RawID = RawID >> 1; 974 975 // Build the IdentifierInfo and link the identifier ID with it. 976 IdentifierInfo *II = KnownII; 977 if (!II) { 978 II = &Reader.getIdentifierTable().getOwn(k); 979 KnownII = II; 980 } 981 markIdentifierFromAST(Reader, *II); 982 Reader.markIdentifierUpToDate(II); 983 984 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 985 if (!IsInteresting) { 986 // For uninteresting identifiers, there's nothing else to do. Just notify 987 // the reader that we've finished loading this identifier. 988 Reader.SetIdentifierInfo(ID, II); 989 return II; 990 } 991 992 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 993 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 994 bool CPlusPlusOperatorKeyword = readBit(Bits); 995 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 996 bool Poisoned = readBit(Bits); 997 bool ExtensionToken = readBit(Bits); 998 bool HadMacroDefinition = readBit(Bits); 999 1000 assert(Bits == 0 && "Extra bits in the identifier?"); 1001 DataLen -= 8; 1002 1003 // Set or check the various bits in the IdentifierInfo structure. 1004 // Token IDs are read-only. 1005 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 1006 II->revertTokenIDToIdentifier(); 1007 if (!F.isModule()) 1008 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 1009 assert(II->isExtensionToken() == ExtensionToken && 1010 "Incorrect extension token flag"); 1011 (void)ExtensionToken; 1012 if (Poisoned) 1013 II->setIsPoisoned(true); 1014 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 1015 "Incorrect C++ operator keyword flag"); 1016 (void)CPlusPlusOperatorKeyword; 1017 1018 // If this identifier is a macro, deserialize the macro 1019 // definition. 1020 if (HadMacroDefinition) { 1021 uint32_t MacroDirectivesOffset = 1022 endian::readNext<uint32_t, little, unaligned>(d); 1023 DataLen -= 4; 1024 1025 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1026 } 1027 1028 Reader.SetIdentifierInfo(ID, II); 1029 1030 // Read all of the declarations visible at global scope with this 1031 // name. 1032 if (DataLen > 0) { 1033 SmallVector<uint32_t, 4> DeclIDs; 1034 for (; DataLen > 0; DataLen -= 4) 1035 DeclIDs.push_back(Reader.getGlobalDeclID( 1036 F, endian::readNext<uint32_t, little, unaligned>(d))); 1037 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1038 } 1039 1040 return II; 1041 } 1042 1043 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1044 : Kind(Name.getNameKind()) { 1045 switch (Kind) { 1046 case DeclarationName::Identifier: 1047 Data = (uint64_t)Name.getAsIdentifierInfo(); 1048 break; 1049 case DeclarationName::ObjCZeroArgSelector: 1050 case DeclarationName::ObjCOneArgSelector: 1051 case DeclarationName::ObjCMultiArgSelector: 1052 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1053 break; 1054 case DeclarationName::CXXOperatorName: 1055 Data = Name.getCXXOverloadedOperator(); 1056 break; 1057 case DeclarationName::CXXLiteralOperatorName: 1058 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1059 break; 1060 case DeclarationName::CXXDeductionGuideName: 1061 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1062 ->getDeclName().getAsIdentifierInfo(); 1063 break; 1064 case DeclarationName::CXXConstructorName: 1065 case DeclarationName::CXXDestructorName: 1066 case DeclarationName::CXXConversionFunctionName: 1067 case DeclarationName::CXXUsingDirective: 1068 Data = 0; 1069 break; 1070 } 1071 } 1072 1073 unsigned DeclarationNameKey::getHash() const { 1074 llvm::FoldingSetNodeID ID; 1075 ID.AddInteger(Kind); 1076 1077 switch (Kind) { 1078 case DeclarationName::Identifier: 1079 case DeclarationName::CXXLiteralOperatorName: 1080 case DeclarationName::CXXDeductionGuideName: 1081 ID.AddString(((IdentifierInfo*)Data)->getName()); 1082 break; 1083 case DeclarationName::ObjCZeroArgSelector: 1084 case DeclarationName::ObjCOneArgSelector: 1085 case DeclarationName::ObjCMultiArgSelector: 1086 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1087 break; 1088 case DeclarationName::CXXOperatorName: 1089 ID.AddInteger((OverloadedOperatorKind)Data); 1090 break; 1091 case DeclarationName::CXXConstructorName: 1092 case DeclarationName::CXXDestructorName: 1093 case DeclarationName::CXXConversionFunctionName: 1094 case DeclarationName::CXXUsingDirective: 1095 break; 1096 } 1097 1098 return ID.ComputeHash(); 1099 } 1100 1101 ModuleFile * 1102 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1103 using namespace llvm::support; 1104 1105 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1106 return Reader.getLocalModuleFile(F, ModuleFileID); 1107 } 1108 1109 std::pair<unsigned, unsigned> 1110 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1111 return readULEBKeyDataLength(d); 1112 } 1113 1114 ASTDeclContextNameLookupTrait::internal_key_type 1115 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1116 using namespace llvm::support; 1117 1118 auto Kind = (DeclarationName::NameKind)*d++; 1119 uint64_t Data; 1120 switch (Kind) { 1121 case DeclarationName::Identifier: 1122 case DeclarationName::CXXLiteralOperatorName: 1123 case DeclarationName::CXXDeductionGuideName: 1124 Data = (uint64_t)Reader.getLocalIdentifier( 1125 F, endian::readNext<uint32_t, little, unaligned>(d)); 1126 break; 1127 case DeclarationName::ObjCZeroArgSelector: 1128 case DeclarationName::ObjCOneArgSelector: 1129 case DeclarationName::ObjCMultiArgSelector: 1130 Data = 1131 (uint64_t)Reader.getLocalSelector( 1132 F, endian::readNext<uint32_t, little, unaligned>( 1133 d)).getAsOpaquePtr(); 1134 break; 1135 case DeclarationName::CXXOperatorName: 1136 Data = *d++; // OverloadedOperatorKind 1137 break; 1138 case DeclarationName::CXXConstructorName: 1139 case DeclarationName::CXXDestructorName: 1140 case DeclarationName::CXXConversionFunctionName: 1141 case DeclarationName::CXXUsingDirective: 1142 Data = 0; 1143 break; 1144 } 1145 1146 return DeclarationNameKey(Kind, Data); 1147 } 1148 1149 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1150 const unsigned char *d, 1151 unsigned DataLen, 1152 data_type_builder &Val) { 1153 using namespace llvm::support; 1154 1155 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1156 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1157 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1158 } 1159 } 1160 1161 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1162 BitstreamCursor &Cursor, 1163 uint64_t Offset, 1164 DeclContext *DC) { 1165 assert(Offset != 0); 1166 1167 SavedStreamPosition SavedPosition(Cursor); 1168 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1169 Error(std::move(Err)); 1170 return true; 1171 } 1172 1173 RecordData Record; 1174 StringRef Blob; 1175 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1176 if (!MaybeCode) { 1177 Error(MaybeCode.takeError()); 1178 return true; 1179 } 1180 unsigned Code = MaybeCode.get(); 1181 1182 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1183 if (!MaybeRecCode) { 1184 Error(MaybeRecCode.takeError()); 1185 return true; 1186 } 1187 unsigned RecCode = MaybeRecCode.get(); 1188 if (RecCode != DECL_CONTEXT_LEXICAL) { 1189 Error("Expected lexical block"); 1190 return true; 1191 } 1192 1193 assert(!isa<TranslationUnitDecl>(DC) && 1194 "expected a TU_UPDATE_LEXICAL record for TU"); 1195 // If we are handling a C++ class template instantiation, we can see multiple 1196 // lexical updates for the same record. It's important that we select only one 1197 // of them, so that field numbering works properly. Just pick the first one we 1198 // see. 1199 auto &Lex = LexicalDecls[DC]; 1200 if (!Lex.first) { 1201 Lex = std::make_pair( 1202 &M, llvm::makeArrayRef( 1203 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1204 Blob.data()), 1205 Blob.size() / 4)); 1206 } 1207 DC->setHasExternalLexicalStorage(true); 1208 return false; 1209 } 1210 1211 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1212 BitstreamCursor &Cursor, 1213 uint64_t Offset, 1214 DeclID ID) { 1215 assert(Offset != 0); 1216 1217 SavedStreamPosition SavedPosition(Cursor); 1218 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1219 Error(std::move(Err)); 1220 return true; 1221 } 1222 1223 RecordData Record; 1224 StringRef Blob; 1225 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1226 if (!MaybeCode) { 1227 Error(MaybeCode.takeError()); 1228 return true; 1229 } 1230 unsigned Code = MaybeCode.get(); 1231 1232 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1233 if (!MaybeRecCode) { 1234 Error(MaybeRecCode.takeError()); 1235 return true; 1236 } 1237 unsigned RecCode = MaybeRecCode.get(); 1238 if (RecCode != DECL_CONTEXT_VISIBLE) { 1239 Error("Expected visible lookup table block"); 1240 return true; 1241 } 1242 1243 // We can't safely determine the primary context yet, so delay attaching the 1244 // lookup table until we're done with recursive deserialization. 1245 auto *Data = (const unsigned char*)Blob.data(); 1246 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1247 return false; 1248 } 1249 1250 void ASTReader::Error(StringRef Msg) const { 1251 Error(diag::err_fe_pch_malformed, Msg); 1252 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1253 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1254 Diag(diag::note_module_cache_path) 1255 << PP.getHeaderSearchInfo().getModuleCachePath(); 1256 } 1257 } 1258 1259 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1260 StringRef Arg3) const { 1261 if (Diags.isDiagnosticInFlight()) 1262 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1263 else 1264 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1265 } 1266 1267 void ASTReader::Error(llvm::Error &&Err) const { 1268 llvm::Error RemainingErr = 1269 handleErrors(std::move(Err), [this](const DiagnosticError &E) { 1270 auto Diag = E.getDiagnostic().second; 1271 1272 // Ideally we'd just emit it, but have to handle a possible in-flight 1273 // diagnostic. Note that the location is currently ignored as well. 1274 auto NumArgs = Diag.getStorage()->NumDiagArgs; 1275 assert(NumArgs <= 3 && "Can only have up to 3 arguments"); 1276 StringRef Arg1, Arg2, Arg3; 1277 switch (NumArgs) { 1278 case 3: 1279 Arg3 = Diag.getStringArg(2); 1280 LLVM_FALLTHROUGH; 1281 case 2: 1282 Arg2 = Diag.getStringArg(1); 1283 LLVM_FALLTHROUGH; 1284 case 1: 1285 Arg1 = Diag.getStringArg(0); 1286 } 1287 Error(Diag.getDiagID(), Arg1, Arg2, Arg3); 1288 }); 1289 if (RemainingErr) 1290 Error(toString(std::move(RemainingErr))); 1291 } 1292 1293 //===----------------------------------------------------------------------===// 1294 // Source Manager Deserialization 1295 //===----------------------------------------------------------------------===// 1296 1297 /// Read the line table in the source manager block. 1298 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) { 1299 unsigned Idx = 0; 1300 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1301 1302 // Parse the file names 1303 std::map<int, int> FileIDs; 1304 FileIDs[-1] = -1; // For unspecified filenames. 1305 for (unsigned I = 0; Record[Idx]; ++I) { 1306 // Extract the file name 1307 auto Filename = ReadPath(F, Record, Idx); 1308 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1309 } 1310 ++Idx; 1311 1312 // Parse the line entries 1313 std::vector<LineEntry> Entries; 1314 while (Idx < Record.size()) { 1315 int FID = Record[Idx++]; 1316 assert(FID >= 0 && "Serialized line entries for non-local file."); 1317 // Remap FileID from 1-based old view. 1318 FID += F.SLocEntryBaseID - 1; 1319 1320 // Extract the line entries 1321 unsigned NumEntries = Record[Idx++]; 1322 assert(NumEntries && "no line entries for file ID"); 1323 Entries.clear(); 1324 Entries.reserve(NumEntries); 1325 for (unsigned I = 0; I != NumEntries; ++I) { 1326 unsigned FileOffset = Record[Idx++]; 1327 unsigned LineNo = Record[Idx++]; 1328 int FilenameID = FileIDs[Record[Idx++]]; 1329 SrcMgr::CharacteristicKind FileKind 1330 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1331 unsigned IncludeOffset = Record[Idx++]; 1332 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1333 FileKind, IncludeOffset)); 1334 } 1335 LineTable.AddEntry(FileID::get(FID), Entries); 1336 } 1337 } 1338 1339 /// Read a source manager block 1340 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1341 using namespace SrcMgr; 1342 1343 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1344 1345 // Set the source-location entry cursor to the current position in 1346 // the stream. This cursor will be used to read the contents of the 1347 // source manager block initially, and then lazily read 1348 // source-location entries as needed. 1349 SLocEntryCursor = F.Stream; 1350 1351 // The stream itself is going to skip over the source manager block. 1352 if (llvm::Error Err = F.Stream.SkipBlock()) 1353 return Err; 1354 1355 // Enter the source manager block. 1356 if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) 1357 return Err; 1358 F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo(); 1359 1360 RecordData Record; 1361 while (true) { 1362 Expected<llvm::BitstreamEntry> MaybeE = 1363 SLocEntryCursor.advanceSkippingSubblocks(); 1364 if (!MaybeE) 1365 return MaybeE.takeError(); 1366 llvm::BitstreamEntry E = MaybeE.get(); 1367 1368 switch (E.Kind) { 1369 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1370 case llvm::BitstreamEntry::Error: 1371 return llvm::createStringError(std::errc::illegal_byte_sequence, 1372 "malformed block record in AST file"); 1373 case llvm::BitstreamEntry::EndBlock: 1374 return llvm::Error::success(); 1375 case llvm::BitstreamEntry::Record: 1376 // The interesting case. 1377 break; 1378 } 1379 1380 // Read a record. 1381 Record.clear(); 1382 StringRef Blob; 1383 Expected<unsigned> MaybeRecord = 1384 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1385 if (!MaybeRecord) 1386 return MaybeRecord.takeError(); 1387 switch (MaybeRecord.get()) { 1388 default: // Default behavior: ignore. 1389 break; 1390 1391 case SM_SLOC_FILE_ENTRY: 1392 case SM_SLOC_BUFFER_ENTRY: 1393 case SM_SLOC_EXPANSION_ENTRY: 1394 // Once we hit one of the source location entries, we're done. 1395 return llvm::Error::success(); 1396 } 1397 } 1398 } 1399 1400 /// If a header file is not found at the path that we expect it to be 1401 /// and the PCH file was moved from its original location, try to resolve the 1402 /// file by assuming that header+PCH were moved together and the header is in 1403 /// the same place relative to the PCH. 1404 static std::string 1405 resolveFileRelativeToOriginalDir(const std::string &Filename, 1406 const std::string &OriginalDir, 1407 const std::string &CurrDir) { 1408 assert(OriginalDir != CurrDir && 1409 "No point trying to resolve the file if the PCH dir didn't change"); 1410 1411 using namespace llvm::sys; 1412 1413 SmallString<128> filePath(Filename); 1414 fs::make_absolute(filePath); 1415 assert(path::is_absolute(OriginalDir)); 1416 SmallString<128> currPCHPath(CurrDir); 1417 1418 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1419 fileDirE = path::end(path::parent_path(filePath)); 1420 path::const_iterator origDirI = path::begin(OriginalDir), 1421 origDirE = path::end(OriginalDir); 1422 // Skip the common path components from filePath and OriginalDir. 1423 while (fileDirI != fileDirE && origDirI != origDirE && 1424 *fileDirI == *origDirI) { 1425 ++fileDirI; 1426 ++origDirI; 1427 } 1428 for (; origDirI != origDirE; ++origDirI) 1429 path::append(currPCHPath, ".."); 1430 path::append(currPCHPath, fileDirI, fileDirE); 1431 path::append(currPCHPath, path::filename(Filename)); 1432 return std::string(currPCHPath.str()); 1433 } 1434 1435 bool ASTReader::ReadSLocEntry(int ID) { 1436 if (ID == 0) 1437 return false; 1438 1439 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1440 Error("source location entry ID out-of-range for AST file"); 1441 return true; 1442 } 1443 1444 // Local helper to read the (possibly-compressed) buffer data following the 1445 // entry record. 1446 auto ReadBuffer = [this]( 1447 BitstreamCursor &SLocEntryCursor, 1448 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1449 RecordData Record; 1450 StringRef Blob; 1451 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1452 if (!MaybeCode) { 1453 Error(MaybeCode.takeError()); 1454 return nullptr; 1455 } 1456 unsigned Code = MaybeCode.get(); 1457 1458 Expected<unsigned> MaybeRecCode = 1459 SLocEntryCursor.readRecord(Code, Record, &Blob); 1460 if (!MaybeRecCode) { 1461 Error(MaybeRecCode.takeError()); 1462 return nullptr; 1463 } 1464 unsigned RecCode = MaybeRecCode.get(); 1465 1466 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1467 if (!llvm::zlib::isAvailable()) { 1468 Error("zlib is not available"); 1469 return nullptr; 1470 } 1471 SmallString<0> Uncompressed; 1472 if (llvm::Error E = 1473 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1474 Error("could not decompress embedded file contents: " + 1475 llvm::toString(std::move(E))); 1476 return nullptr; 1477 } 1478 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1479 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1480 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1481 } else { 1482 Error("AST record has invalid code"); 1483 return nullptr; 1484 } 1485 }; 1486 1487 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1488 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1489 F->SLocEntryOffsetsBase + 1490 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1491 Error(std::move(Err)); 1492 return true; 1493 } 1494 1495 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1496 SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset; 1497 1498 ++NumSLocEntriesRead; 1499 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1500 if (!MaybeEntry) { 1501 Error(MaybeEntry.takeError()); 1502 return true; 1503 } 1504 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1505 1506 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1507 Error("incorrectly-formatted source location entry in AST file"); 1508 return true; 1509 } 1510 1511 RecordData Record; 1512 StringRef Blob; 1513 Expected<unsigned> MaybeSLOC = 1514 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1515 if (!MaybeSLOC) { 1516 Error(MaybeSLOC.takeError()); 1517 return true; 1518 } 1519 switch (MaybeSLOC.get()) { 1520 default: 1521 Error("incorrectly-formatted source location entry in AST file"); 1522 return true; 1523 1524 case SM_SLOC_FILE_ENTRY: { 1525 // We will detect whether a file changed and return 'Failure' for it, but 1526 // we will also try to fail gracefully by setting up the SLocEntry. 1527 unsigned InputID = Record[4]; 1528 InputFile IF = getInputFile(*F, InputID); 1529 Optional<FileEntryRef> File = IF.getFile(); 1530 bool OverriddenBuffer = IF.isOverridden(); 1531 1532 // Note that we only check if a File was returned. If it was out-of-date 1533 // we have complained but we will continue creating a FileID to recover 1534 // gracefully. 1535 if (!File) 1536 return true; 1537 1538 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1539 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1540 // This is the module's main file. 1541 IncludeLoc = getImportLocation(F); 1542 } 1543 SrcMgr::CharacteristicKind 1544 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1545 FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID, 1546 BaseOffset + Record[0]); 1547 SrcMgr::FileInfo &FileInfo = 1548 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1549 FileInfo.NumCreatedFIDs = Record[5]; 1550 if (Record[3]) 1551 FileInfo.setHasLineDirectives(); 1552 1553 unsigned NumFileDecls = Record[7]; 1554 if (NumFileDecls && ContextObj) { 1555 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1556 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1557 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1558 NumFileDecls)); 1559 } 1560 1561 const SrcMgr::ContentCache &ContentCache = 1562 SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter)); 1563 if (OverriddenBuffer && !ContentCache.BufferOverridden && 1564 ContentCache.ContentsEntry == ContentCache.OrigEntry && 1565 !ContentCache.getBufferIfLoaded()) { 1566 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1567 if (!Buffer) 1568 return true; 1569 SourceMgr.overrideFileContents(*File, std::move(Buffer)); 1570 } 1571 1572 break; 1573 } 1574 1575 case SM_SLOC_BUFFER_ENTRY: { 1576 const char *Name = Blob.data(); 1577 unsigned Offset = Record[0]; 1578 SrcMgr::CharacteristicKind 1579 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1580 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1581 if (IncludeLoc.isInvalid() && F->isModule()) { 1582 IncludeLoc = getImportLocation(F); 1583 } 1584 1585 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1586 if (!Buffer) 1587 return true; 1588 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1589 BaseOffset + Offset, IncludeLoc); 1590 break; 1591 } 1592 1593 case SM_SLOC_EXPANSION_ENTRY: { 1594 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1595 SourceMgr.createExpansionLoc(SpellingLoc, 1596 ReadSourceLocation(*F, Record[2]), 1597 ReadSourceLocation(*F, Record[3]), 1598 Record[5], 1599 Record[4], 1600 ID, 1601 BaseOffset + Record[0]); 1602 break; 1603 } 1604 } 1605 1606 return false; 1607 } 1608 1609 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1610 if (ID == 0) 1611 return std::make_pair(SourceLocation(), ""); 1612 1613 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1614 Error("source location entry ID out-of-range for AST file"); 1615 return std::make_pair(SourceLocation(), ""); 1616 } 1617 1618 // Find which module file this entry lands in. 1619 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1620 if (!M->isModule()) 1621 return std::make_pair(SourceLocation(), ""); 1622 1623 // FIXME: Can we map this down to a particular submodule? That would be 1624 // ideal. 1625 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1626 } 1627 1628 /// Find the location where the module F is imported. 1629 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1630 if (F->ImportLoc.isValid()) 1631 return F->ImportLoc; 1632 1633 // Otherwise we have a PCH. It's considered to be "imported" at the first 1634 // location of its includer. 1635 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1636 // Main file is the importer. 1637 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1638 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1639 } 1640 return F->ImportedBy[0]->FirstLoc; 1641 } 1642 1643 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1644 /// the abbreviations that are at the top of the block and then leave the cursor 1645 /// pointing into the block. 1646 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, 1647 unsigned BlockID, 1648 uint64_t *StartOfBlockOffset) { 1649 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) 1650 return Err; 1651 1652 if (StartOfBlockOffset) 1653 *StartOfBlockOffset = Cursor.GetCurrentBitNo(); 1654 1655 while (true) { 1656 uint64_t Offset = Cursor.GetCurrentBitNo(); 1657 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1658 if (!MaybeCode) 1659 return MaybeCode.takeError(); 1660 unsigned Code = MaybeCode.get(); 1661 1662 // We expect all abbrevs to be at the start of the block. 1663 if (Code != llvm::bitc::DEFINE_ABBREV) { 1664 if (llvm::Error Err = Cursor.JumpToBit(Offset)) 1665 return Err; 1666 return llvm::Error::success(); 1667 } 1668 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) 1669 return Err; 1670 } 1671 } 1672 1673 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1674 unsigned &Idx) { 1675 Token Tok; 1676 Tok.startToken(); 1677 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1678 Tok.setLength(Record[Idx++]); 1679 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1680 Tok.setIdentifierInfo(II); 1681 Tok.setKind((tok::TokenKind)Record[Idx++]); 1682 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1683 return Tok; 1684 } 1685 1686 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1687 BitstreamCursor &Stream = F.MacroCursor; 1688 1689 // Keep track of where we are in the stream, then jump back there 1690 // after reading this macro. 1691 SavedStreamPosition SavedPosition(Stream); 1692 1693 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1694 // FIXME this drops errors on the floor. 1695 consumeError(std::move(Err)); 1696 return nullptr; 1697 } 1698 RecordData Record; 1699 SmallVector<IdentifierInfo*, 16> MacroParams; 1700 MacroInfo *Macro = nullptr; 1701 1702 while (true) { 1703 // Advance to the next record, but if we get to the end of the block, don't 1704 // pop it (removing all the abbreviations from the cursor) since we want to 1705 // be able to reseek within the block and read entries. 1706 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1707 Expected<llvm::BitstreamEntry> MaybeEntry = 1708 Stream.advanceSkippingSubblocks(Flags); 1709 if (!MaybeEntry) { 1710 Error(MaybeEntry.takeError()); 1711 return Macro; 1712 } 1713 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1714 1715 switch (Entry.Kind) { 1716 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1717 case llvm::BitstreamEntry::Error: 1718 Error("malformed block record in AST file"); 1719 return Macro; 1720 case llvm::BitstreamEntry::EndBlock: 1721 return Macro; 1722 case llvm::BitstreamEntry::Record: 1723 // The interesting case. 1724 break; 1725 } 1726 1727 // Read a record. 1728 Record.clear(); 1729 PreprocessorRecordTypes RecType; 1730 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1731 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1732 else { 1733 Error(MaybeRecType.takeError()); 1734 return Macro; 1735 } 1736 switch (RecType) { 1737 case PP_MODULE_MACRO: 1738 case PP_MACRO_DIRECTIVE_HISTORY: 1739 return Macro; 1740 1741 case PP_MACRO_OBJECT_LIKE: 1742 case PP_MACRO_FUNCTION_LIKE: { 1743 // If we already have a macro, that means that we've hit the end 1744 // of the definition of the macro we were looking for. We're 1745 // done. 1746 if (Macro) 1747 return Macro; 1748 1749 unsigned NextIndex = 1; // Skip identifier ID. 1750 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1751 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1752 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1753 MI->setIsUsed(Record[NextIndex++]); 1754 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1755 1756 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1757 // Decode function-like macro info. 1758 bool isC99VarArgs = Record[NextIndex++]; 1759 bool isGNUVarArgs = Record[NextIndex++]; 1760 bool hasCommaPasting = Record[NextIndex++]; 1761 MacroParams.clear(); 1762 unsigned NumArgs = Record[NextIndex++]; 1763 for (unsigned i = 0; i != NumArgs; ++i) 1764 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1765 1766 // Install function-like macro info. 1767 MI->setIsFunctionLike(); 1768 if (isC99VarArgs) MI->setIsC99Varargs(); 1769 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1770 if (hasCommaPasting) MI->setHasCommaPasting(); 1771 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1772 } 1773 1774 // Remember that we saw this macro last so that we add the tokens that 1775 // form its body to it. 1776 Macro = MI; 1777 1778 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1779 Record[NextIndex]) { 1780 // We have a macro definition. Register the association 1781 PreprocessedEntityID 1782 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1783 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1784 PreprocessingRecord::PPEntityID PPID = 1785 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1786 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1787 PPRec.getPreprocessedEntity(PPID)); 1788 if (PPDef) 1789 PPRec.RegisterMacroDefinition(Macro, PPDef); 1790 } 1791 1792 ++NumMacrosRead; 1793 break; 1794 } 1795 1796 case PP_TOKEN: { 1797 // If we see a TOKEN before a PP_MACRO_*, then the file is 1798 // erroneous, just pretend we didn't see this. 1799 if (!Macro) break; 1800 1801 unsigned Idx = 0; 1802 Token Tok = ReadToken(F, Record, Idx); 1803 Macro->AddTokenToBody(Tok); 1804 break; 1805 } 1806 } 1807 } 1808 } 1809 1810 PreprocessedEntityID 1811 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1812 unsigned LocalID) const { 1813 if (!M.ModuleOffsetMap.empty()) 1814 ReadModuleOffsetMap(M); 1815 1816 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1817 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1818 assert(I != M.PreprocessedEntityRemap.end() 1819 && "Invalid index into preprocessed entity index remap"); 1820 1821 return LocalID + I->second; 1822 } 1823 1824 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1825 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1826 } 1827 1828 HeaderFileInfoTrait::internal_key_type 1829 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1830 internal_key_type ikey = {FE->getSize(), 1831 M.HasTimestamps ? FE->getModificationTime() : 0, 1832 FE->getName(), /*Imported*/ false}; 1833 return ikey; 1834 } 1835 1836 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1837 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1838 return false; 1839 1840 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1841 return true; 1842 1843 // Determine whether the actual files are equivalent. 1844 FileManager &FileMgr = Reader.getFileManager(); 1845 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1846 if (!Key.Imported) { 1847 if (auto File = FileMgr.getFile(Key.Filename)) 1848 return *File; 1849 return nullptr; 1850 } 1851 1852 std::string Resolved = std::string(Key.Filename); 1853 Reader.ResolveImportedPath(M, Resolved); 1854 if (auto File = FileMgr.getFile(Resolved)) 1855 return *File; 1856 return nullptr; 1857 }; 1858 1859 const FileEntry *FEA = GetFile(a); 1860 const FileEntry *FEB = GetFile(b); 1861 return FEA && FEA == FEB; 1862 } 1863 1864 std::pair<unsigned, unsigned> 1865 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1866 return readULEBKeyDataLength(d); 1867 } 1868 1869 HeaderFileInfoTrait::internal_key_type 1870 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1871 using namespace llvm::support; 1872 1873 internal_key_type ikey; 1874 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1875 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1876 ikey.Filename = (const char *)d; 1877 ikey.Imported = true; 1878 return ikey; 1879 } 1880 1881 HeaderFileInfoTrait::data_type 1882 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1883 unsigned DataLen) { 1884 using namespace llvm::support; 1885 1886 const unsigned char *End = d + DataLen; 1887 HeaderFileInfo HFI; 1888 unsigned Flags = *d++; 1889 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1890 HFI.isImport |= (Flags >> 5) & 0x01; 1891 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1892 HFI.DirInfo = (Flags >> 1) & 0x07; 1893 HFI.IndexHeaderMapHeader = Flags & 0x01; 1894 // FIXME: Find a better way to handle this. Maybe just store a 1895 // "has been included" flag? 1896 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1897 HFI.NumIncludes); 1898 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1899 M, endian::readNext<uint32_t, little, unaligned>(d)); 1900 if (unsigned FrameworkOffset = 1901 endian::readNext<uint32_t, little, unaligned>(d)) { 1902 // The framework offset is 1 greater than the actual offset, 1903 // since 0 is used as an indicator for "no framework name". 1904 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1905 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1906 } 1907 1908 assert((End - d) % 4 == 0 && 1909 "Wrong data length in HeaderFileInfo deserialization"); 1910 while (d != End) { 1911 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1912 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1913 LocalSMID >>= 2; 1914 1915 // This header is part of a module. Associate it with the module to enable 1916 // implicit module import. 1917 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1918 Module *Mod = Reader.getSubmodule(GlobalSMID); 1919 FileManager &FileMgr = Reader.getFileManager(); 1920 ModuleMap &ModMap = 1921 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1922 1923 std::string Filename = std::string(key.Filename); 1924 if (key.Imported) 1925 Reader.ResolveImportedPath(M, Filename); 1926 // FIXME: NameAsWritten 1927 Module::Header H = {std::string(key.Filename), "", 1928 *FileMgr.getFile(Filename)}; 1929 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1930 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1931 } 1932 1933 // This HeaderFileInfo was externally loaded. 1934 HFI.External = true; 1935 HFI.IsValid = true; 1936 return HFI; 1937 } 1938 1939 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M, 1940 uint32_t MacroDirectivesOffset) { 1941 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1942 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1943 } 1944 1945 void ASTReader::ReadDefinedMacros() { 1946 // Note that we are loading defined macros. 1947 Deserializing Macros(this); 1948 1949 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1950 BitstreamCursor &MacroCursor = I.MacroCursor; 1951 1952 // If there was no preprocessor block, skip this file. 1953 if (MacroCursor.getBitcodeBytes().empty()) 1954 continue; 1955 1956 BitstreamCursor Cursor = MacroCursor; 1957 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1958 Error(std::move(Err)); 1959 return; 1960 } 1961 1962 RecordData Record; 1963 while (true) { 1964 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1965 if (!MaybeE) { 1966 Error(MaybeE.takeError()); 1967 return; 1968 } 1969 llvm::BitstreamEntry E = MaybeE.get(); 1970 1971 switch (E.Kind) { 1972 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1973 case llvm::BitstreamEntry::Error: 1974 Error("malformed block record in AST file"); 1975 return; 1976 case llvm::BitstreamEntry::EndBlock: 1977 goto NextCursor; 1978 1979 case llvm::BitstreamEntry::Record: { 1980 Record.clear(); 1981 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1982 if (!MaybeRecord) { 1983 Error(MaybeRecord.takeError()); 1984 return; 1985 } 1986 switch (MaybeRecord.get()) { 1987 default: // Default behavior: ignore. 1988 break; 1989 1990 case PP_MACRO_OBJECT_LIKE: 1991 case PP_MACRO_FUNCTION_LIKE: { 1992 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1993 if (II->isOutOfDate()) 1994 updateOutOfDateIdentifier(*II); 1995 break; 1996 } 1997 1998 case PP_TOKEN: 1999 // Ignore tokens. 2000 break; 2001 } 2002 break; 2003 } 2004 } 2005 } 2006 NextCursor: ; 2007 } 2008 } 2009 2010 namespace { 2011 2012 /// Visitor class used to look up identifirs in an AST file. 2013 class IdentifierLookupVisitor { 2014 StringRef Name; 2015 unsigned NameHash; 2016 unsigned PriorGeneration; 2017 unsigned &NumIdentifierLookups; 2018 unsigned &NumIdentifierLookupHits; 2019 IdentifierInfo *Found = nullptr; 2020 2021 public: 2022 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2023 unsigned &NumIdentifierLookups, 2024 unsigned &NumIdentifierLookupHits) 2025 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2026 PriorGeneration(PriorGeneration), 2027 NumIdentifierLookups(NumIdentifierLookups), 2028 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2029 2030 bool operator()(ModuleFile &M) { 2031 // If we've already searched this module file, skip it now. 2032 if (M.Generation <= PriorGeneration) 2033 return true; 2034 2035 ASTIdentifierLookupTable *IdTable 2036 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2037 if (!IdTable) 2038 return false; 2039 2040 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2041 Found); 2042 ++NumIdentifierLookups; 2043 ASTIdentifierLookupTable::iterator Pos = 2044 IdTable->find_hashed(Name, NameHash, &Trait); 2045 if (Pos == IdTable->end()) 2046 return false; 2047 2048 // Dereferencing the iterator has the effect of building the 2049 // IdentifierInfo node and populating it with the various 2050 // declarations it needs. 2051 ++NumIdentifierLookupHits; 2052 Found = *Pos; 2053 return true; 2054 } 2055 2056 // Retrieve the identifier info found within the module 2057 // files. 2058 IdentifierInfo *getIdentifierInfo() const { return Found; } 2059 }; 2060 2061 } // namespace 2062 2063 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2064 // Note that we are loading an identifier. 2065 Deserializing AnIdentifier(this); 2066 2067 unsigned PriorGeneration = 0; 2068 if (getContext().getLangOpts().Modules) 2069 PriorGeneration = IdentifierGeneration[&II]; 2070 2071 // If there is a global index, look there first to determine which modules 2072 // provably do not have any results for this identifier. 2073 GlobalModuleIndex::HitSet Hits; 2074 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2075 if (!loadGlobalIndex()) { 2076 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2077 HitsPtr = &Hits; 2078 } 2079 } 2080 2081 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2082 NumIdentifierLookups, 2083 NumIdentifierLookupHits); 2084 ModuleMgr.visit(Visitor, HitsPtr); 2085 markIdentifierUpToDate(&II); 2086 } 2087 2088 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2089 if (!II) 2090 return; 2091 2092 II->setOutOfDate(false); 2093 2094 // Update the generation for this identifier. 2095 if (getContext().getLangOpts().Modules) 2096 IdentifierGeneration[II] = getGeneration(); 2097 } 2098 2099 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2100 const PendingMacroInfo &PMInfo) { 2101 ModuleFile &M = *PMInfo.M; 2102 2103 BitstreamCursor &Cursor = M.MacroCursor; 2104 SavedStreamPosition SavedPosition(Cursor); 2105 if (llvm::Error Err = 2106 Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) { 2107 Error(std::move(Err)); 2108 return; 2109 } 2110 2111 struct ModuleMacroRecord { 2112 SubmoduleID SubModID; 2113 MacroInfo *MI; 2114 SmallVector<SubmoduleID, 8> Overrides; 2115 }; 2116 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2117 2118 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2119 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2120 // macro histroy. 2121 RecordData Record; 2122 while (true) { 2123 Expected<llvm::BitstreamEntry> MaybeEntry = 2124 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2125 if (!MaybeEntry) { 2126 Error(MaybeEntry.takeError()); 2127 return; 2128 } 2129 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2130 2131 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2132 Error("malformed block record in AST file"); 2133 return; 2134 } 2135 2136 Record.clear(); 2137 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2138 if (!MaybePP) { 2139 Error(MaybePP.takeError()); 2140 return; 2141 } 2142 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2143 case PP_MACRO_DIRECTIVE_HISTORY: 2144 break; 2145 2146 case PP_MODULE_MACRO: { 2147 ModuleMacros.push_back(ModuleMacroRecord()); 2148 auto &Info = ModuleMacros.back(); 2149 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2150 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2151 for (int I = 2, N = Record.size(); I != N; ++I) 2152 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2153 continue; 2154 } 2155 2156 default: 2157 Error("malformed block record in AST file"); 2158 return; 2159 } 2160 2161 // We found the macro directive history; that's the last record 2162 // for this macro. 2163 break; 2164 } 2165 2166 // Module macros are listed in reverse dependency order. 2167 { 2168 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2169 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2170 for (auto &MMR : ModuleMacros) { 2171 Overrides.clear(); 2172 for (unsigned ModID : MMR.Overrides) { 2173 Module *Mod = getSubmodule(ModID); 2174 auto *Macro = PP.getModuleMacro(Mod, II); 2175 assert(Macro && "missing definition for overridden macro"); 2176 Overrides.push_back(Macro); 2177 } 2178 2179 bool Inserted = false; 2180 Module *Owner = getSubmodule(MMR.SubModID); 2181 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2182 } 2183 } 2184 2185 // Don't read the directive history for a module; we don't have anywhere 2186 // to put it. 2187 if (M.isModule()) 2188 return; 2189 2190 // Deserialize the macro directives history in reverse source-order. 2191 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2192 unsigned Idx = 0, N = Record.size(); 2193 while (Idx < N) { 2194 MacroDirective *MD = nullptr; 2195 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2196 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2197 switch (K) { 2198 case MacroDirective::MD_Define: { 2199 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2200 MD = PP.AllocateDefMacroDirective(MI, Loc); 2201 break; 2202 } 2203 case MacroDirective::MD_Undefine: 2204 MD = PP.AllocateUndefMacroDirective(Loc); 2205 break; 2206 case MacroDirective::MD_Visibility: 2207 bool isPublic = Record[Idx++]; 2208 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2209 break; 2210 } 2211 2212 if (!Latest) 2213 Latest = MD; 2214 if (Earliest) 2215 Earliest->setPrevious(MD); 2216 Earliest = MD; 2217 } 2218 2219 if (Latest) 2220 PP.setLoadedMacroDirective(II, Earliest, Latest); 2221 } 2222 2223 bool ASTReader::shouldDisableValidationForFile( 2224 const serialization::ModuleFile &M) const { 2225 if (DisableValidationKind == DisableValidationForModuleKind::None) 2226 return false; 2227 2228 // If a PCH is loaded and validation is disabled for PCH then disable 2229 // validation for the PCH and the modules it loads. 2230 ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind); 2231 2232 switch (K) { 2233 case MK_MainFile: 2234 case MK_Preamble: 2235 case MK_PCH: 2236 return bool(DisableValidationKind & DisableValidationForModuleKind::PCH); 2237 case MK_ImplicitModule: 2238 case MK_ExplicitModule: 2239 case MK_PrebuiltModule: 2240 return bool(DisableValidationKind & DisableValidationForModuleKind::Module); 2241 } 2242 2243 return false; 2244 } 2245 2246 ASTReader::InputFileInfo 2247 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2248 // Go find this input file. 2249 BitstreamCursor &Cursor = F.InputFilesCursor; 2250 SavedStreamPosition SavedPosition(Cursor); 2251 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2252 // FIXME this drops errors on the floor. 2253 consumeError(std::move(Err)); 2254 } 2255 2256 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2257 if (!MaybeCode) { 2258 // FIXME this drops errors on the floor. 2259 consumeError(MaybeCode.takeError()); 2260 } 2261 unsigned Code = MaybeCode.get(); 2262 RecordData Record; 2263 StringRef Blob; 2264 2265 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2266 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2267 "invalid record type for input file"); 2268 else { 2269 // FIXME this drops errors on the floor. 2270 consumeError(Maybe.takeError()); 2271 } 2272 2273 assert(Record[0] == ID && "Bogus stored ID or offset"); 2274 InputFileInfo R; 2275 R.StoredSize = static_cast<off_t>(Record[1]); 2276 R.StoredTime = static_cast<time_t>(Record[2]); 2277 R.Overridden = static_cast<bool>(Record[3]); 2278 R.Transient = static_cast<bool>(Record[4]); 2279 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2280 R.Filename = std::string(Blob); 2281 ResolveImportedPath(F, R.Filename); 2282 2283 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2284 if (!MaybeEntry) // FIXME this drops errors on the floor. 2285 consumeError(MaybeEntry.takeError()); 2286 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2287 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2288 "expected record type for input file hash"); 2289 2290 Record.clear(); 2291 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2292 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2293 "invalid record type for input file hash"); 2294 else { 2295 // FIXME this drops errors on the floor. 2296 consumeError(Maybe.takeError()); 2297 } 2298 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2299 static_cast<uint64_t>(Record[0]); 2300 return R; 2301 } 2302 2303 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2304 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2305 // If this ID is bogus, just return an empty input file. 2306 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2307 return InputFile(); 2308 2309 // If we've already loaded this input file, return it. 2310 if (F.InputFilesLoaded[ID-1].getFile()) 2311 return F.InputFilesLoaded[ID-1]; 2312 2313 if (F.InputFilesLoaded[ID-1].isNotFound()) 2314 return InputFile(); 2315 2316 // Go find this input file. 2317 BitstreamCursor &Cursor = F.InputFilesCursor; 2318 SavedStreamPosition SavedPosition(Cursor); 2319 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2320 // FIXME this drops errors on the floor. 2321 consumeError(std::move(Err)); 2322 } 2323 2324 InputFileInfo FI = readInputFileInfo(F, ID); 2325 off_t StoredSize = FI.StoredSize; 2326 time_t StoredTime = FI.StoredTime; 2327 bool Overridden = FI.Overridden; 2328 bool Transient = FI.Transient; 2329 StringRef Filename = FI.Filename; 2330 uint64_t StoredContentHash = FI.ContentHash; 2331 2332 OptionalFileEntryRefDegradesToFileEntryPtr File = 2333 expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false)); 2334 2335 // If we didn't find the file, resolve it relative to the 2336 // original directory from which this AST file was created. 2337 if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2338 F.OriginalDir != F.BaseDirectory) { 2339 std::string Resolved = resolveFileRelativeToOriginalDir( 2340 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2341 if (!Resolved.empty()) 2342 File = expectedToOptional(FileMgr.getFileRef(Resolved)); 2343 } 2344 2345 // For an overridden file, create a virtual file with the stored 2346 // size/timestamp. 2347 if ((Overridden || Transient) && !File) 2348 File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime); 2349 2350 if (!File) { 2351 if (Complain) { 2352 std::string ErrorStr = "could not find file '"; 2353 ErrorStr += Filename; 2354 ErrorStr += "' referenced by AST file '"; 2355 ErrorStr += F.FileName; 2356 ErrorStr += "'"; 2357 Error(ErrorStr); 2358 } 2359 // Record that we didn't find the file. 2360 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2361 return InputFile(); 2362 } 2363 2364 // Check if there was a request to override the contents of the file 2365 // that was part of the precompiled header. Overriding such a file 2366 // can lead to problems when lexing using the source locations from the 2367 // PCH. 2368 SourceManager &SM = getSourceManager(); 2369 // FIXME: Reject if the overrides are different. 2370 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2371 if (Complain) 2372 Error(diag::err_fe_pch_file_overridden, Filename); 2373 2374 // After emitting the diagnostic, bypass the overriding file to recover 2375 // (this creates a separate FileEntry). 2376 File = SM.bypassFileContentsOverride(*File); 2377 if (!File) { 2378 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2379 return InputFile(); 2380 } 2381 } 2382 2383 struct Change { 2384 enum ModificationKind { 2385 Size, 2386 ModTime, 2387 Content, 2388 None, 2389 } Kind; 2390 llvm::Optional<int64_t> Old = llvm::None; 2391 llvm::Optional<int64_t> New = llvm::None; 2392 }; 2393 auto HasInputFileChanged = [&]() { 2394 if (StoredSize != File->getSize()) 2395 return Change{Change::Size, StoredSize, File->getSize()}; 2396 if (!shouldDisableValidationForFile(F) && StoredTime && 2397 StoredTime != File->getModificationTime()) { 2398 Change MTimeChange = {Change::ModTime, StoredTime, 2399 File->getModificationTime()}; 2400 2401 // In case the modification time changes but not the content, 2402 // accept the cached file as legit. 2403 if (ValidateASTInputFilesContent && 2404 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2405 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2406 if (!MemBuffOrError) { 2407 if (!Complain) 2408 return MTimeChange; 2409 std::string ErrorStr = "could not get buffer for file '"; 2410 ErrorStr += File->getName(); 2411 ErrorStr += "'"; 2412 Error(ErrorStr); 2413 return MTimeChange; 2414 } 2415 2416 // FIXME: hash_value is not guaranteed to be stable! 2417 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2418 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2419 return Change{Change::None}; 2420 2421 return Change{Change::Content}; 2422 } 2423 return MTimeChange; 2424 } 2425 return Change{Change::None}; 2426 }; 2427 2428 bool IsOutOfDate = false; 2429 auto FileChange = HasInputFileChanged(); 2430 // For an overridden file, there is nothing to validate. 2431 if (!Overridden && FileChange.Kind != Change::None) { 2432 if (Complain && !Diags.isDiagnosticInFlight()) { 2433 // Build a list of the PCH imports that got us here (in reverse). 2434 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2435 while (!ImportStack.back()->ImportedBy.empty()) 2436 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2437 2438 // The top-level PCH is stale. 2439 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2440 Diag(diag::err_fe_ast_file_modified) 2441 << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind) 2442 << TopLevelPCHName << FileChange.Kind 2443 << (FileChange.Old && FileChange.New) 2444 << llvm::itostr(FileChange.Old.getValueOr(0)) 2445 << llvm::itostr(FileChange.New.getValueOr(0)); 2446 2447 // Print the import stack. 2448 if (ImportStack.size() > 1) { 2449 Diag(diag::note_pch_required_by) 2450 << Filename << ImportStack[0]->FileName; 2451 for (unsigned I = 1; I < ImportStack.size(); ++I) 2452 Diag(diag::note_pch_required_by) 2453 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2454 } 2455 2456 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2457 } 2458 2459 IsOutOfDate = true; 2460 } 2461 // FIXME: If the file is overridden and we've already opened it, 2462 // issue an error (or split it into a separate FileEntry). 2463 2464 InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate); 2465 2466 // Note that we've loaded this input file. 2467 F.InputFilesLoaded[ID-1] = IF; 2468 return IF; 2469 } 2470 2471 /// If we are loading a relocatable PCH or module file, and the filename 2472 /// is not an absolute path, add the system or module root to the beginning of 2473 /// the file name. 2474 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2475 // Resolve relative to the base directory, if we have one. 2476 if (!M.BaseDirectory.empty()) 2477 return ResolveImportedPath(Filename, M.BaseDirectory); 2478 } 2479 2480 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2481 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2482 return; 2483 2484 SmallString<128> Buffer; 2485 llvm::sys::path::append(Buffer, Prefix, Filename); 2486 Filename.assign(Buffer.begin(), Buffer.end()); 2487 } 2488 2489 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2490 switch (ARR) { 2491 case ASTReader::Failure: return true; 2492 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2493 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2494 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2495 case ASTReader::ConfigurationMismatch: 2496 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2497 case ASTReader::HadErrors: return true; 2498 case ASTReader::Success: return false; 2499 } 2500 2501 llvm_unreachable("unknown ASTReadResult"); 2502 } 2503 2504 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2505 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2506 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2507 std::string &SuggestedPredefines) { 2508 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2509 // FIXME this drops errors on the floor. 2510 consumeError(std::move(Err)); 2511 return Failure; 2512 } 2513 2514 // Read all of the records in the options block. 2515 RecordData Record; 2516 ASTReadResult Result = Success; 2517 while (true) { 2518 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2519 if (!MaybeEntry) { 2520 // FIXME this drops errors on the floor. 2521 consumeError(MaybeEntry.takeError()); 2522 return Failure; 2523 } 2524 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2525 2526 switch (Entry.Kind) { 2527 case llvm::BitstreamEntry::Error: 2528 case llvm::BitstreamEntry::SubBlock: 2529 return Failure; 2530 2531 case llvm::BitstreamEntry::EndBlock: 2532 return Result; 2533 2534 case llvm::BitstreamEntry::Record: 2535 // The interesting case. 2536 break; 2537 } 2538 2539 // Read and process a record. 2540 Record.clear(); 2541 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2542 if (!MaybeRecordType) { 2543 // FIXME this drops errors on the floor. 2544 consumeError(MaybeRecordType.takeError()); 2545 return Failure; 2546 } 2547 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2548 case LANGUAGE_OPTIONS: { 2549 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2550 if (ParseLanguageOptions(Record, Complain, Listener, 2551 AllowCompatibleConfigurationMismatch)) 2552 Result = ConfigurationMismatch; 2553 break; 2554 } 2555 2556 case TARGET_OPTIONS: { 2557 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2558 if (ParseTargetOptions(Record, Complain, Listener, 2559 AllowCompatibleConfigurationMismatch)) 2560 Result = ConfigurationMismatch; 2561 break; 2562 } 2563 2564 case FILE_SYSTEM_OPTIONS: { 2565 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2566 if (!AllowCompatibleConfigurationMismatch && 2567 ParseFileSystemOptions(Record, Complain, Listener)) 2568 Result = ConfigurationMismatch; 2569 break; 2570 } 2571 2572 case HEADER_SEARCH_OPTIONS: { 2573 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2574 if (!AllowCompatibleConfigurationMismatch && 2575 ParseHeaderSearchOptions(Record, Complain, Listener)) 2576 Result = ConfigurationMismatch; 2577 break; 2578 } 2579 2580 case PREPROCESSOR_OPTIONS: 2581 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2582 if (!AllowCompatibleConfigurationMismatch && 2583 ParsePreprocessorOptions(Record, Complain, Listener, 2584 SuggestedPredefines)) 2585 Result = ConfigurationMismatch; 2586 break; 2587 } 2588 } 2589 } 2590 2591 ASTReader::ASTReadResult 2592 ASTReader::ReadControlBlock(ModuleFile &F, 2593 SmallVectorImpl<ImportedModule> &Loaded, 2594 const ModuleFile *ImportedBy, 2595 unsigned ClientLoadCapabilities) { 2596 BitstreamCursor &Stream = F.Stream; 2597 2598 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2599 Error(std::move(Err)); 2600 return Failure; 2601 } 2602 2603 // Lambda to read the unhashed control block the first time it's called. 2604 // 2605 // For PCM files, the unhashed control block cannot be read until after the 2606 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2607 // need to look ahead before reading the IMPORTS record. For consistency, 2608 // this block is always read somehow (see BitstreamEntry::EndBlock). 2609 bool HasReadUnhashedControlBlock = false; 2610 auto readUnhashedControlBlockOnce = [&]() { 2611 if (!HasReadUnhashedControlBlock) { 2612 HasReadUnhashedControlBlock = true; 2613 if (ASTReadResult Result = 2614 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2615 return Result; 2616 } 2617 return Success; 2618 }; 2619 2620 bool DisableValidation = shouldDisableValidationForFile(F); 2621 2622 // Read all of the records and blocks in the control block. 2623 RecordData Record; 2624 unsigned NumInputs = 0; 2625 unsigned NumUserInputs = 0; 2626 StringRef BaseDirectoryAsWritten; 2627 while (true) { 2628 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2629 if (!MaybeEntry) { 2630 Error(MaybeEntry.takeError()); 2631 return Failure; 2632 } 2633 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2634 2635 switch (Entry.Kind) { 2636 case llvm::BitstreamEntry::Error: 2637 Error("malformed block record in AST file"); 2638 return Failure; 2639 case llvm::BitstreamEntry::EndBlock: { 2640 // Validate the module before returning. This call catches an AST with 2641 // no module name and no imports. 2642 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2643 return Result; 2644 2645 // Validate input files. 2646 const HeaderSearchOptions &HSOpts = 2647 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2648 2649 // All user input files reside at the index range [0, NumUserInputs), and 2650 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2651 // loaded module files, ignore missing inputs. 2652 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2653 F.Kind != MK_PrebuiltModule) { 2654 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2655 2656 // If we are reading a module, we will create a verification timestamp, 2657 // so we verify all input files. Otherwise, verify only user input 2658 // files. 2659 2660 unsigned N = NumUserInputs; 2661 if (ValidateSystemInputs || 2662 (HSOpts.ModulesValidateOncePerBuildSession && 2663 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2664 F.Kind == MK_ImplicitModule)) 2665 N = NumInputs; 2666 2667 for (unsigned I = 0; I < N; ++I) { 2668 InputFile IF = getInputFile(F, I+1, Complain); 2669 if (!IF.getFile() || IF.isOutOfDate()) 2670 return OutOfDate; 2671 } 2672 } 2673 2674 if (Listener) 2675 Listener->visitModuleFile(F.FileName, F.Kind); 2676 2677 if (Listener && Listener->needsInputFileVisitation()) { 2678 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2679 : NumUserInputs; 2680 for (unsigned I = 0; I < N; ++I) { 2681 bool IsSystem = I >= NumUserInputs; 2682 InputFileInfo FI = readInputFileInfo(F, I+1); 2683 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2684 F.Kind == MK_ExplicitModule || 2685 F.Kind == MK_PrebuiltModule); 2686 } 2687 } 2688 2689 return Success; 2690 } 2691 2692 case llvm::BitstreamEntry::SubBlock: 2693 switch (Entry.ID) { 2694 case INPUT_FILES_BLOCK_ID: 2695 F.InputFilesCursor = Stream; 2696 if (llvm::Error Err = Stream.SkipBlock()) { 2697 Error(std::move(Err)); 2698 return Failure; 2699 } 2700 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2701 Error("malformed block record in AST file"); 2702 return Failure; 2703 } 2704 continue; 2705 2706 case OPTIONS_BLOCK_ID: 2707 // If we're reading the first module for this group, check its options 2708 // are compatible with ours. For modules it imports, no further checking 2709 // is required, because we checked them when we built it. 2710 if (Listener && !ImportedBy) { 2711 // Should we allow the configuration of the module file to differ from 2712 // the configuration of the current translation unit in a compatible 2713 // way? 2714 // 2715 // FIXME: Allow this for files explicitly specified with -include-pch. 2716 bool AllowCompatibleConfigurationMismatch = 2717 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2718 2719 ASTReadResult Result = 2720 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2721 AllowCompatibleConfigurationMismatch, *Listener, 2722 SuggestedPredefines); 2723 if (Result == Failure) { 2724 Error("malformed block record in AST file"); 2725 return Result; 2726 } 2727 2728 if (DisableValidation || 2729 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2730 Result = Success; 2731 2732 // If we can't load the module, exit early since we likely 2733 // will rebuild the module anyway. The stream may be in the 2734 // middle of a block. 2735 if (Result != Success) 2736 return Result; 2737 } else if (llvm::Error Err = Stream.SkipBlock()) { 2738 Error(std::move(Err)); 2739 return Failure; 2740 } 2741 continue; 2742 2743 default: 2744 if (llvm::Error Err = Stream.SkipBlock()) { 2745 Error(std::move(Err)); 2746 return Failure; 2747 } 2748 continue; 2749 } 2750 2751 case llvm::BitstreamEntry::Record: 2752 // The interesting case. 2753 break; 2754 } 2755 2756 // Read and process a record. 2757 Record.clear(); 2758 StringRef Blob; 2759 Expected<unsigned> MaybeRecordType = 2760 Stream.readRecord(Entry.ID, Record, &Blob); 2761 if (!MaybeRecordType) { 2762 Error(MaybeRecordType.takeError()); 2763 return Failure; 2764 } 2765 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2766 case METADATA: { 2767 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2768 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2769 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2770 : diag::err_pch_version_too_new); 2771 return VersionMismatch; 2772 } 2773 2774 bool hasErrors = Record[6]; 2775 if (hasErrors && !DisableValidation) { 2776 // If requested by the caller and the module hasn't already been read 2777 // or compiled, mark modules on error as out-of-date. 2778 if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) && 2779 canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2780 return OutOfDate; 2781 2782 if (!AllowASTWithCompilerErrors) { 2783 Diag(diag::err_pch_with_compiler_errors); 2784 return HadErrors; 2785 } 2786 } 2787 if (hasErrors) { 2788 Diags.ErrorOccurred = true; 2789 Diags.UncompilableErrorOccurred = true; 2790 Diags.UnrecoverableErrorOccurred = true; 2791 } 2792 2793 F.RelocatablePCH = Record[4]; 2794 // Relative paths in a relocatable PCH are relative to our sysroot. 2795 if (F.RelocatablePCH) 2796 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2797 2798 F.HasTimestamps = Record[5]; 2799 2800 const std::string &CurBranch = getClangFullRepositoryVersion(); 2801 StringRef ASTBranch = Blob; 2802 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2803 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2804 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2805 return VersionMismatch; 2806 } 2807 break; 2808 } 2809 2810 case IMPORTS: { 2811 // Validate the AST before processing any imports (otherwise, untangling 2812 // them can be error-prone and expensive). A module will have a name and 2813 // will already have been validated, but this catches the PCH case. 2814 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2815 return Result; 2816 2817 // Load each of the imported PCH files. 2818 unsigned Idx = 0, N = Record.size(); 2819 while (Idx < N) { 2820 // Read information about the AST file. 2821 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2822 // The import location will be the local one for now; we will adjust 2823 // all import locations of module imports after the global source 2824 // location info are setup, in ReadAST. 2825 SourceLocation ImportLoc = 2826 ReadUntranslatedSourceLocation(Record[Idx++]); 2827 off_t StoredSize = (off_t)Record[Idx++]; 2828 time_t StoredModTime = (time_t)Record[Idx++]; 2829 auto FirstSignatureByte = Record.begin() + Idx; 2830 ASTFileSignature StoredSignature = ASTFileSignature::create( 2831 FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 2832 Idx += ASTFileSignature::size; 2833 2834 std::string ImportedName = ReadString(Record, Idx); 2835 std::string ImportedFile; 2836 2837 // For prebuilt and explicit modules first consult the file map for 2838 // an override. Note that here we don't search prebuilt module 2839 // directories, only the explicit name to file mappings. Also, we will 2840 // still verify the size/signature making sure it is essentially the 2841 // same file but perhaps in a different location. 2842 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2843 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2844 ImportedName, /*FileMapOnly*/ true); 2845 2846 if (ImportedFile.empty()) 2847 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2848 // ModuleCache as when writing. 2849 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2850 else 2851 SkipPath(Record, Idx); 2852 2853 // If our client can't cope with us being out of date, we can't cope with 2854 // our dependency being missing. 2855 unsigned Capabilities = ClientLoadCapabilities; 2856 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2857 Capabilities &= ~ARR_Missing; 2858 2859 // Load the AST file. 2860 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2861 Loaded, StoredSize, StoredModTime, 2862 StoredSignature, Capabilities); 2863 2864 // If we diagnosed a problem, produce a backtrace. 2865 bool recompilingFinalized = 2866 Result == OutOfDate && (Capabilities & ARR_OutOfDate) && 2867 getModuleManager().getModuleCache().isPCMFinal(F.FileName); 2868 if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized) 2869 Diag(diag::note_module_file_imported_by) 2870 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2871 if (recompilingFinalized) 2872 Diag(diag::note_module_file_conflict); 2873 2874 switch (Result) { 2875 case Failure: return Failure; 2876 // If we have to ignore the dependency, we'll have to ignore this too. 2877 case Missing: 2878 case OutOfDate: return OutOfDate; 2879 case VersionMismatch: return VersionMismatch; 2880 case ConfigurationMismatch: return ConfigurationMismatch; 2881 case HadErrors: return HadErrors; 2882 case Success: break; 2883 } 2884 } 2885 break; 2886 } 2887 2888 case ORIGINAL_FILE: 2889 F.OriginalSourceFileID = FileID::get(Record[0]); 2890 F.ActualOriginalSourceFileName = std::string(Blob); 2891 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2892 ResolveImportedPath(F, F.OriginalSourceFileName); 2893 break; 2894 2895 case ORIGINAL_FILE_ID: 2896 F.OriginalSourceFileID = FileID::get(Record[0]); 2897 break; 2898 2899 case ORIGINAL_PCH_DIR: 2900 F.OriginalDir = std::string(Blob); 2901 break; 2902 2903 case MODULE_NAME: 2904 F.ModuleName = std::string(Blob); 2905 Diag(diag::remark_module_import) 2906 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2907 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2908 if (Listener) 2909 Listener->ReadModuleName(F.ModuleName); 2910 2911 // Validate the AST as soon as we have a name so we can exit early on 2912 // failure. 2913 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2914 return Result; 2915 2916 break; 2917 2918 case MODULE_DIRECTORY: { 2919 // Save the BaseDirectory as written in the PCM for computing the module 2920 // filename for the ModuleCache. 2921 BaseDirectoryAsWritten = Blob; 2922 assert(!F.ModuleName.empty() && 2923 "MODULE_DIRECTORY found before MODULE_NAME"); 2924 // If we've already loaded a module map file covering this module, we may 2925 // have a better path for it (relative to the current build). 2926 Module *M = PP.getHeaderSearchInfo().lookupModule( 2927 F.ModuleName, SourceLocation(), /*AllowSearch*/ true, 2928 /*AllowExtraModuleMapSearch*/ true); 2929 if (M && M->Directory) { 2930 // If we're implicitly loading a module, the base directory can't 2931 // change between the build and use. 2932 // Don't emit module relocation error if we have -fno-validate-pch 2933 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 2934 DisableValidationForModuleKind::Module) && 2935 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2936 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2937 if (!BuildDir || *BuildDir != M->Directory) { 2938 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 2939 Diag(diag::err_imported_module_relocated) 2940 << F.ModuleName << Blob << M->Directory->getName(); 2941 return OutOfDate; 2942 } 2943 } 2944 F.BaseDirectory = std::string(M->Directory->getName()); 2945 } else { 2946 F.BaseDirectory = std::string(Blob); 2947 } 2948 break; 2949 } 2950 2951 case MODULE_MAP_FILE: 2952 if (ASTReadResult Result = 2953 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2954 return Result; 2955 break; 2956 2957 case INPUT_FILE_OFFSETS: 2958 NumInputs = Record[0]; 2959 NumUserInputs = Record[1]; 2960 F.InputFileOffsets = 2961 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2962 F.InputFilesLoaded.resize(NumInputs); 2963 F.NumUserInputFiles = NumUserInputs; 2964 break; 2965 } 2966 } 2967 } 2968 2969 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, 2970 unsigned ClientLoadCapabilities) { 2971 BitstreamCursor &Stream = F.Stream; 2972 2973 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) 2974 return Err; 2975 F.ASTBlockStartOffset = Stream.GetCurrentBitNo(); 2976 2977 // Read all of the records and blocks for the AST file. 2978 RecordData Record; 2979 while (true) { 2980 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2981 if (!MaybeEntry) 2982 return MaybeEntry.takeError(); 2983 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2984 2985 switch (Entry.Kind) { 2986 case llvm::BitstreamEntry::Error: 2987 return llvm::createStringError( 2988 std::errc::illegal_byte_sequence, 2989 "error at end of module block in AST file"); 2990 case llvm::BitstreamEntry::EndBlock: 2991 // Outside of C++, we do not store a lookup map for the translation unit. 2992 // Instead, mark it as needing a lookup map to be built if this module 2993 // contains any declarations lexically within it (which it always does!). 2994 // This usually has no cost, since we very rarely need the lookup map for 2995 // the translation unit outside C++. 2996 if (ASTContext *Ctx = ContextObj) { 2997 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2998 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2999 DC->setMustBuildLookupTable(); 3000 } 3001 3002 return llvm::Error::success(); 3003 case llvm::BitstreamEntry::SubBlock: 3004 switch (Entry.ID) { 3005 case DECLTYPES_BLOCK_ID: 3006 // We lazily load the decls block, but we want to set up the 3007 // DeclsCursor cursor to point into it. Clone our current bitcode 3008 // cursor to it, enter the block and read the abbrevs in that block. 3009 // With the main cursor, we just skip over it. 3010 F.DeclsCursor = Stream; 3011 if (llvm::Error Err = Stream.SkipBlock()) 3012 return Err; 3013 if (llvm::Error Err = ReadBlockAbbrevs( 3014 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset)) 3015 return Err; 3016 break; 3017 3018 case PREPROCESSOR_BLOCK_ID: 3019 F.MacroCursor = Stream; 3020 if (!PP.getExternalSource()) 3021 PP.setExternalSource(this); 3022 3023 if (llvm::Error Err = Stream.SkipBlock()) 3024 return Err; 3025 if (llvm::Error Err = 3026 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) 3027 return Err; 3028 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 3029 break; 3030 3031 case PREPROCESSOR_DETAIL_BLOCK_ID: 3032 F.PreprocessorDetailCursor = Stream; 3033 3034 if (llvm::Error Err = Stream.SkipBlock()) { 3035 return Err; 3036 } 3037 if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3038 PREPROCESSOR_DETAIL_BLOCK_ID)) 3039 return Err; 3040 F.PreprocessorDetailStartOffset 3041 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3042 3043 if (!PP.getPreprocessingRecord()) 3044 PP.createPreprocessingRecord(); 3045 if (!PP.getPreprocessingRecord()->getExternalSource()) 3046 PP.getPreprocessingRecord()->SetExternalSource(*this); 3047 break; 3048 3049 case SOURCE_MANAGER_BLOCK_ID: 3050 if (llvm::Error Err = ReadSourceManagerBlock(F)) 3051 return Err; 3052 break; 3053 3054 case SUBMODULE_BLOCK_ID: 3055 if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3056 return Err; 3057 break; 3058 3059 case COMMENTS_BLOCK_ID: { 3060 BitstreamCursor C = Stream; 3061 3062 if (llvm::Error Err = Stream.SkipBlock()) 3063 return Err; 3064 if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) 3065 return Err; 3066 CommentsCursors.push_back(std::make_pair(C, &F)); 3067 break; 3068 } 3069 3070 default: 3071 if (llvm::Error Err = Stream.SkipBlock()) 3072 return Err; 3073 break; 3074 } 3075 continue; 3076 3077 case llvm::BitstreamEntry::Record: 3078 // The interesting case. 3079 break; 3080 } 3081 3082 // Read and process a record. 3083 Record.clear(); 3084 StringRef Blob; 3085 Expected<unsigned> MaybeRecordType = 3086 Stream.readRecord(Entry.ID, Record, &Blob); 3087 if (!MaybeRecordType) 3088 return MaybeRecordType.takeError(); 3089 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3090 3091 // If we're not loading an AST context, we don't care about most records. 3092 if (!ContextObj) { 3093 switch (RecordType) { 3094 case IDENTIFIER_TABLE: 3095 case IDENTIFIER_OFFSET: 3096 case INTERESTING_IDENTIFIERS: 3097 case STATISTICS: 3098 case PP_CONDITIONAL_STACK: 3099 case PP_COUNTER_VALUE: 3100 case SOURCE_LOCATION_OFFSETS: 3101 case MODULE_OFFSET_MAP: 3102 case SOURCE_MANAGER_LINE_TABLE: 3103 case SOURCE_LOCATION_PRELOADS: 3104 case PPD_ENTITIES_OFFSETS: 3105 case HEADER_SEARCH_TABLE: 3106 case IMPORTED_MODULES: 3107 case MACRO_OFFSET: 3108 break; 3109 default: 3110 continue; 3111 } 3112 } 3113 3114 switch (RecordType) { 3115 default: // Default behavior: ignore. 3116 break; 3117 3118 case TYPE_OFFSET: { 3119 if (F.LocalNumTypes != 0) 3120 return llvm::createStringError( 3121 std::errc::illegal_byte_sequence, 3122 "duplicate TYPE_OFFSET record in AST file"); 3123 F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data()); 3124 F.LocalNumTypes = Record[0]; 3125 unsigned LocalBaseTypeIndex = Record[1]; 3126 F.BaseTypeIndex = getTotalNumTypes(); 3127 3128 if (F.LocalNumTypes > 0) { 3129 // Introduce the global -> local mapping for types within this module. 3130 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3131 3132 // Introduce the local -> global mapping for types within this module. 3133 F.TypeRemap.insertOrReplace( 3134 std::make_pair(LocalBaseTypeIndex, 3135 F.BaseTypeIndex - LocalBaseTypeIndex)); 3136 3137 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3138 } 3139 break; 3140 } 3141 3142 case DECL_OFFSET: { 3143 if (F.LocalNumDecls != 0) 3144 return llvm::createStringError( 3145 std::errc::illegal_byte_sequence, 3146 "duplicate DECL_OFFSET record in AST file"); 3147 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3148 F.LocalNumDecls = Record[0]; 3149 unsigned LocalBaseDeclID = Record[1]; 3150 F.BaseDeclID = getTotalNumDecls(); 3151 3152 if (F.LocalNumDecls > 0) { 3153 // Introduce the global -> local mapping for declarations within this 3154 // module. 3155 GlobalDeclMap.insert( 3156 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3157 3158 // Introduce the local -> global mapping for declarations within this 3159 // module. 3160 F.DeclRemap.insertOrReplace( 3161 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3162 3163 // Introduce the global -> local mapping for declarations within this 3164 // module. 3165 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3166 3167 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3168 } 3169 break; 3170 } 3171 3172 case TU_UPDATE_LEXICAL: { 3173 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3174 LexicalContents Contents( 3175 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3176 Blob.data()), 3177 static_cast<unsigned int>(Blob.size() / 4)); 3178 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3179 TU->setHasExternalLexicalStorage(true); 3180 break; 3181 } 3182 3183 case UPDATE_VISIBLE: { 3184 unsigned Idx = 0; 3185 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3186 auto *Data = (const unsigned char*)Blob.data(); 3187 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3188 // If we've already loaded the decl, perform the updates when we finish 3189 // loading this block. 3190 if (Decl *D = GetExistingDecl(ID)) 3191 PendingUpdateRecords.push_back( 3192 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3193 break; 3194 } 3195 3196 case IDENTIFIER_TABLE: 3197 F.IdentifierTableData = 3198 reinterpret_cast<const unsigned char *>(Blob.data()); 3199 if (Record[0]) { 3200 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3201 F.IdentifierTableData + Record[0], 3202 F.IdentifierTableData + sizeof(uint32_t), 3203 F.IdentifierTableData, 3204 ASTIdentifierLookupTrait(*this, F)); 3205 3206 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3207 } 3208 break; 3209 3210 case IDENTIFIER_OFFSET: { 3211 if (F.LocalNumIdentifiers != 0) 3212 return llvm::createStringError( 3213 std::errc::illegal_byte_sequence, 3214 "duplicate IDENTIFIER_OFFSET record in AST file"); 3215 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3216 F.LocalNumIdentifiers = Record[0]; 3217 unsigned LocalBaseIdentifierID = Record[1]; 3218 F.BaseIdentifierID = getTotalNumIdentifiers(); 3219 3220 if (F.LocalNumIdentifiers > 0) { 3221 // Introduce the global -> local mapping for identifiers within this 3222 // module. 3223 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3224 &F)); 3225 3226 // Introduce the local -> global mapping for identifiers within this 3227 // module. 3228 F.IdentifierRemap.insertOrReplace( 3229 std::make_pair(LocalBaseIdentifierID, 3230 F.BaseIdentifierID - LocalBaseIdentifierID)); 3231 3232 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3233 + F.LocalNumIdentifiers); 3234 } 3235 break; 3236 } 3237 3238 case INTERESTING_IDENTIFIERS: 3239 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3240 break; 3241 3242 case EAGERLY_DESERIALIZED_DECLS: 3243 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3244 // about "interesting" decls (for instance, if we're building a module). 3245 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3246 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3247 break; 3248 3249 case MODULAR_CODEGEN_DECLS: 3250 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3251 // them (ie: if we're not codegenerating this module). 3252 if (F.Kind == MK_MainFile || 3253 getContext().getLangOpts().BuildingPCHWithObjectFile) 3254 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3255 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3256 break; 3257 3258 case SPECIAL_TYPES: 3259 if (SpecialTypes.empty()) { 3260 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3261 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3262 break; 3263 } 3264 3265 if (SpecialTypes.size() != Record.size()) 3266 return llvm::createStringError(std::errc::illegal_byte_sequence, 3267 "invalid special-types record"); 3268 3269 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3270 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3271 if (!SpecialTypes[I]) 3272 SpecialTypes[I] = ID; 3273 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3274 // merge step? 3275 } 3276 break; 3277 3278 case STATISTICS: 3279 TotalNumStatements += Record[0]; 3280 TotalNumMacros += Record[1]; 3281 TotalLexicalDeclContexts += Record[2]; 3282 TotalVisibleDeclContexts += Record[3]; 3283 break; 3284 3285 case UNUSED_FILESCOPED_DECLS: 3286 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3287 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3288 break; 3289 3290 case DELEGATING_CTORS: 3291 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3292 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3293 break; 3294 3295 case WEAK_UNDECLARED_IDENTIFIERS: 3296 if (Record.size() % 4 != 0) 3297 return llvm::createStringError(std::errc::illegal_byte_sequence, 3298 "invalid weak identifiers record"); 3299 3300 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3301 // files. This isn't the way to do it :) 3302 WeakUndeclaredIdentifiers.clear(); 3303 3304 // Translate the weak, undeclared identifiers into global IDs. 3305 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3306 WeakUndeclaredIdentifiers.push_back( 3307 getGlobalIdentifierID(F, Record[I++])); 3308 WeakUndeclaredIdentifiers.push_back( 3309 getGlobalIdentifierID(F, Record[I++])); 3310 WeakUndeclaredIdentifiers.push_back( 3311 ReadSourceLocation(F, Record, I).getRawEncoding()); 3312 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3313 } 3314 break; 3315 3316 case SELECTOR_OFFSETS: { 3317 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3318 F.LocalNumSelectors = Record[0]; 3319 unsigned LocalBaseSelectorID = Record[1]; 3320 F.BaseSelectorID = getTotalNumSelectors(); 3321 3322 if (F.LocalNumSelectors > 0) { 3323 // Introduce the global -> local mapping for selectors within this 3324 // module. 3325 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3326 3327 // Introduce the local -> global mapping for selectors within this 3328 // module. 3329 F.SelectorRemap.insertOrReplace( 3330 std::make_pair(LocalBaseSelectorID, 3331 F.BaseSelectorID - LocalBaseSelectorID)); 3332 3333 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3334 } 3335 break; 3336 } 3337 3338 case METHOD_POOL: 3339 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3340 if (Record[0]) 3341 F.SelectorLookupTable 3342 = ASTSelectorLookupTable::Create( 3343 F.SelectorLookupTableData + Record[0], 3344 F.SelectorLookupTableData, 3345 ASTSelectorLookupTrait(*this, F)); 3346 TotalNumMethodPoolEntries += Record[1]; 3347 break; 3348 3349 case REFERENCED_SELECTOR_POOL: 3350 if (!Record.empty()) { 3351 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3352 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3353 Record[Idx++])); 3354 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3355 getRawEncoding()); 3356 } 3357 } 3358 break; 3359 3360 case PP_CONDITIONAL_STACK: 3361 if (!Record.empty()) { 3362 unsigned Idx = 0, End = Record.size() - 1; 3363 bool ReachedEOFWhileSkipping = Record[Idx++]; 3364 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3365 if (ReachedEOFWhileSkipping) { 3366 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3367 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3368 bool FoundNonSkipPortion = Record[Idx++]; 3369 bool FoundElse = Record[Idx++]; 3370 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3371 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3372 FoundElse, ElseLoc); 3373 } 3374 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3375 while (Idx < End) { 3376 auto Loc = ReadSourceLocation(F, Record, Idx); 3377 bool WasSkipping = Record[Idx++]; 3378 bool FoundNonSkip = Record[Idx++]; 3379 bool FoundElse = Record[Idx++]; 3380 ConditionalStack.push_back( 3381 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3382 } 3383 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3384 } 3385 break; 3386 3387 case PP_COUNTER_VALUE: 3388 if (!Record.empty() && Listener) 3389 Listener->ReadCounter(F, Record[0]); 3390 break; 3391 3392 case FILE_SORTED_DECLS: 3393 F.FileSortedDecls = (const DeclID *)Blob.data(); 3394 F.NumFileSortedDecls = Record[0]; 3395 break; 3396 3397 case SOURCE_LOCATION_OFFSETS: { 3398 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3399 F.LocalNumSLocEntries = Record[0]; 3400 SourceLocation::UIntTy SLocSpaceSize = Record[1]; 3401 F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; 3402 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3403 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3404 SLocSpaceSize); 3405 if (!F.SLocEntryBaseID) 3406 return llvm::createStringError(std::errc::invalid_argument, 3407 "ran out of source locations"); 3408 // Make our entry in the range map. BaseID is negative and growing, so 3409 // we invert it. Because we invert it, though, we need the other end of 3410 // the range. 3411 unsigned RangeStart = 3412 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3413 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3414 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3415 3416 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3417 assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0); 3418 GlobalSLocOffsetMap.insert( 3419 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3420 - SLocSpaceSize,&F)); 3421 3422 // Initialize the remapping table. 3423 // Invalid stays invalid. 3424 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3425 // This module. Base was 2 when being compiled. 3426 F.SLocRemap.insertOrReplace(std::make_pair( 3427 2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2))); 3428 3429 TotalNumSLocEntries += F.LocalNumSLocEntries; 3430 break; 3431 } 3432 3433 case MODULE_OFFSET_MAP: 3434 F.ModuleOffsetMap = Blob; 3435 break; 3436 3437 case SOURCE_MANAGER_LINE_TABLE: 3438 ParseLineTable(F, Record); 3439 break; 3440 3441 case SOURCE_LOCATION_PRELOADS: { 3442 // Need to transform from the local view (1-based IDs) to the global view, 3443 // which is based off F.SLocEntryBaseID. 3444 if (!F.PreloadSLocEntries.empty()) 3445 return llvm::createStringError( 3446 std::errc::illegal_byte_sequence, 3447 "Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3448 3449 F.PreloadSLocEntries.swap(Record); 3450 break; 3451 } 3452 3453 case EXT_VECTOR_DECLS: 3454 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3455 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3456 break; 3457 3458 case VTABLE_USES: 3459 if (Record.size() % 3 != 0) 3460 return llvm::createStringError(std::errc::illegal_byte_sequence, 3461 "Invalid VTABLE_USES record"); 3462 3463 // Later tables overwrite earlier ones. 3464 // FIXME: Modules will have some trouble with this. This is clearly not 3465 // the right way to do this. 3466 VTableUses.clear(); 3467 3468 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3469 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3470 VTableUses.push_back( 3471 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3472 VTableUses.push_back(Record[Idx++]); 3473 } 3474 break; 3475 3476 case PENDING_IMPLICIT_INSTANTIATIONS: 3477 if (PendingInstantiations.size() % 2 != 0) 3478 return llvm::createStringError( 3479 std::errc::illegal_byte_sequence, 3480 "Invalid existing PendingInstantiations"); 3481 3482 if (Record.size() % 2 != 0) 3483 return llvm::createStringError( 3484 std::errc::illegal_byte_sequence, 3485 "Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3486 3487 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3488 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3489 PendingInstantiations.push_back( 3490 ReadSourceLocation(F, Record, I).getRawEncoding()); 3491 } 3492 break; 3493 3494 case SEMA_DECL_REFS: 3495 if (Record.size() != 3) 3496 return llvm::createStringError(std::errc::illegal_byte_sequence, 3497 "Invalid SEMA_DECL_REFS block"); 3498 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3499 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3500 break; 3501 3502 case PPD_ENTITIES_OFFSETS: { 3503 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3504 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3505 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3506 3507 unsigned LocalBasePreprocessedEntityID = Record[0]; 3508 3509 unsigned StartingID; 3510 if (!PP.getPreprocessingRecord()) 3511 PP.createPreprocessingRecord(); 3512 if (!PP.getPreprocessingRecord()->getExternalSource()) 3513 PP.getPreprocessingRecord()->SetExternalSource(*this); 3514 StartingID 3515 = PP.getPreprocessingRecord() 3516 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3517 F.BasePreprocessedEntityID = StartingID; 3518 3519 if (F.NumPreprocessedEntities > 0) { 3520 // Introduce the global -> local mapping for preprocessed entities in 3521 // this module. 3522 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3523 3524 // Introduce the local -> global mapping for preprocessed entities in 3525 // this module. 3526 F.PreprocessedEntityRemap.insertOrReplace( 3527 std::make_pair(LocalBasePreprocessedEntityID, 3528 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3529 } 3530 3531 break; 3532 } 3533 3534 case PPD_SKIPPED_RANGES: { 3535 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3536 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3537 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3538 3539 if (!PP.getPreprocessingRecord()) 3540 PP.createPreprocessingRecord(); 3541 if (!PP.getPreprocessingRecord()->getExternalSource()) 3542 PP.getPreprocessingRecord()->SetExternalSource(*this); 3543 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3544 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3545 3546 if (F.NumPreprocessedSkippedRanges > 0) 3547 GlobalSkippedRangeMap.insert( 3548 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3549 break; 3550 } 3551 3552 case DECL_UPDATE_OFFSETS: 3553 if (Record.size() % 2 != 0) 3554 return llvm::createStringError( 3555 std::errc::illegal_byte_sequence, 3556 "invalid DECL_UPDATE_OFFSETS block in AST file"); 3557 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3558 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3559 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3560 3561 // If we've already loaded the decl, perform the updates when we finish 3562 // loading this block. 3563 if (Decl *D = GetExistingDecl(ID)) 3564 PendingUpdateRecords.push_back( 3565 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3566 } 3567 break; 3568 3569 case OBJC_CATEGORIES_MAP: 3570 if (F.LocalNumObjCCategoriesInMap != 0) 3571 return llvm::createStringError( 3572 std::errc::illegal_byte_sequence, 3573 "duplicate OBJC_CATEGORIES_MAP record in AST file"); 3574 3575 F.LocalNumObjCCategoriesInMap = Record[0]; 3576 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3577 break; 3578 3579 case OBJC_CATEGORIES: 3580 F.ObjCCategories.swap(Record); 3581 break; 3582 3583 case CUDA_SPECIAL_DECL_REFS: 3584 // Later tables overwrite earlier ones. 3585 // FIXME: Modules will have trouble with this. 3586 CUDASpecialDeclRefs.clear(); 3587 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3588 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3589 break; 3590 3591 case HEADER_SEARCH_TABLE: 3592 F.HeaderFileInfoTableData = Blob.data(); 3593 F.LocalNumHeaderFileInfos = Record[1]; 3594 if (Record[0]) { 3595 F.HeaderFileInfoTable 3596 = HeaderFileInfoLookupTable::Create( 3597 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3598 (const unsigned char *)F.HeaderFileInfoTableData, 3599 HeaderFileInfoTrait(*this, F, 3600 &PP.getHeaderSearchInfo(), 3601 Blob.data() + Record[2])); 3602 3603 PP.getHeaderSearchInfo().SetExternalSource(this); 3604 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3605 PP.getHeaderSearchInfo().SetExternalLookup(this); 3606 } 3607 break; 3608 3609 case FP_PRAGMA_OPTIONS: 3610 // Later tables overwrite earlier ones. 3611 FPPragmaOptions.swap(Record); 3612 break; 3613 3614 case OPENCL_EXTENSIONS: 3615 for (unsigned I = 0, E = Record.size(); I != E; ) { 3616 auto Name = ReadString(Record, I); 3617 auto &OptInfo = OpenCLExtensions.OptMap[Name]; 3618 OptInfo.Supported = Record[I++] != 0; 3619 OptInfo.Enabled = Record[I++] != 0; 3620 OptInfo.WithPragma = Record[I++] != 0; 3621 OptInfo.Avail = Record[I++]; 3622 OptInfo.Core = Record[I++]; 3623 OptInfo.Opt = Record[I++]; 3624 } 3625 break; 3626 3627 case TENTATIVE_DEFINITIONS: 3628 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3629 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3630 break; 3631 3632 case KNOWN_NAMESPACES: 3633 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3634 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3635 break; 3636 3637 case UNDEFINED_BUT_USED: 3638 if (UndefinedButUsed.size() % 2 != 0) 3639 return llvm::createStringError(std::errc::illegal_byte_sequence, 3640 "Invalid existing UndefinedButUsed"); 3641 3642 if (Record.size() % 2 != 0) 3643 return llvm::createStringError(std::errc::illegal_byte_sequence, 3644 "invalid undefined-but-used record"); 3645 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3646 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3647 UndefinedButUsed.push_back( 3648 ReadSourceLocation(F, Record, I).getRawEncoding()); 3649 } 3650 break; 3651 3652 case DELETE_EXPRS_TO_ANALYZE: 3653 for (unsigned I = 0, N = Record.size(); I != N;) { 3654 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3655 const uint64_t Count = Record[I++]; 3656 DelayedDeleteExprs.push_back(Count); 3657 for (uint64_t C = 0; C < Count; ++C) { 3658 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3659 bool IsArrayForm = Record[I++] == 1; 3660 DelayedDeleteExprs.push_back(IsArrayForm); 3661 } 3662 } 3663 break; 3664 3665 case IMPORTED_MODULES: 3666 if (!F.isModule()) { 3667 // If we aren't loading a module (which has its own exports), make 3668 // all of the imported modules visible. 3669 // FIXME: Deal with macros-only imports. 3670 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3671 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3672 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3673 if (GlobalID) { 3674 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3675 if (DeserializationListener) 3676 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3677 } 3678 } 3679 } 3680 break; 3681 3682 case MACRO_OFFSET: { 3683 if (F.LocalNumMacros != 0) 3684 return llvm::createStringError( 3685 std::errc::illegal_byte_sequence, 3686 "duplicate MACRO_OFFSET record in AST file"); 3687 F.MacroOffsets = (const uint32_t *)Blob.data(); 3688 F.LocalNumMacros = Record[0]; 3689 unsigned LocalBaseMacroID = Record[1]; 3690 F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset; 3691 F.BaseMacroID = getTotalNumMacros(); 3692 3693 if (F.LocalNumMacros > 0) { 3694 // Introduce the global -> local mapping for macros within this module. 3695 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3696 3697 // Introduce the local -> global mapping for macros within this module. 3698 F.MacroRemap.insertOrReplace( 3699 std::make_pair(LocalBaseMacroID, 3700 F.BaseMacroID - LocalBaseMacroID)); 3701 3702 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3703 } 3704 break; 3705 } 3706 3707 case LATE_PARSED_TEMPLATE: 3708 LateParsedTemplates.emplace_back( 3709 std::piecewise_construct, std::forward_as_tuple(&F), 3710 std::forward_as_tuple(Record.begin(), Record.end())); 3711 break; 3712 3713 case OPTIMIZE_PRAGMA_OPTIONS: 3714 if (Record.size() != 1) 3715 return llvm::createStringError(std::errc::illegal_byte_sequence, 3716 "invalid pragma optimize record"); 3717 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3718 break; 3719 3720 case MSSTRUCT_PRAGMA_OPTIONS: 3721 if (Record.size() != 1) 3722 return llvm::createStringError(std::errc::illegal_byte_sequence, 3723 "invalid pragma ms_struct record"); 3724 PragmaMSStructState = Record[0]; 3725 break; 3726 3727 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3728 if (Record.size() != 2) 3729 return llvm::createStringError( 3730 std::errc::illegal_byte_sequence, 3731 "invalid pragma pointers to members record"); 3732 PragmaMSPointersToMembersState = Record[0]; 3733 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3734 break; 3735 3736 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3737 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3738 UnusedLocalTypedefNameCandidates.push_back( 3739 getGlobalDeclID(F, Record[I])); 3740 break; 3741 3742 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3743 if (Record.size() != 1) 3744 return llvm::createStringError(std::errc::illegal_byte_sequence, 3745 "invalid cuda pragma options record"); 3746 ForceCUDAHostDeviceDepth = Record[0]; 3747 break; 3748 3749 case ALIGN_PACK_PRAGMA_OPTIONS: { 3750 if (Record.size() < 3) 3751 return llvm::createStringError(std::errc::illegal_byte_sequence, 3752 "invalid pragma pack record"); 3753 PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]); 3754 PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3755 unsigned NumStackEntries = Record[2]; 3756 unsigned Idx = 3; 3757 // Reset the stack when importing a new module. 3758 PragmaAlignPackStack.clear(); 3759 for (unsigned I = 0; I < NumStackEntries; ++I) { 3760 PragmaAlignPackStackEntry Entry; 3761 Entry.Value = ReadAlignPackInfo(Record[Idx++]); 3762 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3763 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3764 PragmaAlignPackStrings.push_back(ReadString(Record, Idx)); 3765 Entry.SlotLabel = PragmaAlignPackStrings.back(); 3766 PragmaAlignPackStack.push_back(Entry); 3767 } 3768 break; 3769 } 3770 3771 case FLOAT_CONTROL_PRAGMA_OPTIONS: { 3772 if (Record.size() < 3) 3773 return llvm::createStringError(std::errc::illegal_byte_sequence, 3774 "invalid pragma float control record"); 3775 FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]); 3776 FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]); 3777 unsigned NumStackEntries = Record[2]; 3778 unsigned Idx = 3; 3779 // Reset the stack when importing a new module. 3780 FpPragmaStack.clear(); 3781 for (unsigned I = 0; I < NumStackEntries; ++I) { 3782 FpPragmaStackEntry Entry; 3783 Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]); 3784 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3785 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3786 FpPragmaStrings.push_back(ReadString(Record, Idx)); 3787 Entry.SlotLabel = FpPragmaStrings.back(); 3788 FpPragmaStack.push_back(Entry); 3789 } 3790 break; 3791 } 3792 3793 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3794 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3795 DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I])); 3796 break; 3797 } 3798 } 3799 } 3800 3801 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3802 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3803 3804 // Additional remapping information. 3805 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3806 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3807 F.ModuleOffsetMap = StringRef(); 3808 3809 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3810 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3811 F.SLocRemap.insert(std::make_pair(0U, 0)); 3812 F.SLocRemap.insert(std::make_pair(2U, 1)); 3813 } 3814 3815 // Continuous range maps we may be updating in our module. 3816 using SLocRemapBuilder = 3817 ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy, 3818 2>::Builder; 3819 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3820 SLocRemapBuilder SLocRemap(F.SLocRemap); 3821 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3822 RemapBuilder MacroRemap(F.MacroRemap); 3823 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3824 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3825 RemapBuilder SelectorRemap(F.SelectorRemap); 3826 RemapBuilder DeclRemap(F.DeclRemap); 3827 RemapBuilder TypeRemap(F.TypeRemap); 3828 3829 while (Data < DataEnd) { 3830 // FIXME: Looking up dependency modules by filename is horrible. Let's 3831 // start fixing this with prebuilt, explicit and implicit modules and see 3832 // how it goes... 3833 using namespace llvm::support; 3834 ModuleKind Kind = static_cast<ModuleKind>( 3835 endian::readNext<uint8_t, little, unaligned>(Data)); 3836 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3837 StringRef Name = StringRef((const char*)Data, Len); 3838 Data += Len; 3839 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule || 3840 Kind == MK_ImplicitModule 3841 ? ModuleMgr.lookupByModuleName(Name) 3842 : ModuleMgr.lookupByFileName(Name)); 3843 if (!OM) { 3844 std::string Msg = 3845 "SourceLocation remap refers to unknown module, cannot find "; 3846 Msg.append(std::string(Name)); 3847 Error(Msg); 3848 return; 3849 } 3850 3851 SourceLocation::UIntTy SLocOffset = 3852 endian::readNext<uint32_t, little, unaligned>(Data); 3853 uint32_t IdentifierIDOffset = 3854 endian::readNext<uint32_t, little, unaligned>(Data); 3855 uint32_t MacroIDOffset = 3856 endian::readNext<uint32_t, little, unaligned>(Data); 3857 uint32_t PreprocessedEntityIDOffset = 3858 endian::readNext<uint32_t, little, unaligned>(Data); 3859 uint32_t SubmoduleIDOffset = 3860 endian::readNext<uint32_t, little, unaligned>(Data); 3861 uint32_t SelectorIDOffset = 3862 endian::readNext<uint32_t, little, unaligned>(Data); 3863 uint32_t DeclIDOffset = 3864 endian::readNext<uint32_t, little, unaligned>(Data); 3865 uint32_t TypeIndexOffset = 3866 endian::readNext<uint32_t, little, unaligned>(Data); 3867 3868 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3869 RemapBuilder &Remap) { 3870 constexpr uint32_t None = std::numeric_limits<uint32_t>::max(); 3871 if (Offset != None) 3872 Remap.insert(std::make_pair(Offset, 3873 static_cast<int>(BaseOffset - Offset))); 3874 }; 3875 3876 constexpr SourceLocation::UIntTy SLocNone = 3877 std::numeric_limits<SourceLocation::UIntTy>::max(); 3878 if (SLocOffset != SLocNone) 3879 SLocRemap.insert(std::make_pair( 3880 SLocOffset, static_cast<SourceLocation::IntTy>( 3881 OM->SLocEntryBaseOffset - SLocOffset))); 3882 3883 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3884 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3885 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3886 PreprocessedEntityRemap); 3887 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3888 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3889 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3890 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3891 3892 // Global -> local mappings. 3893 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3894 } 3895 } 3896 3897 ASTReader::ASTReadResult 3898 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3899 const ModuleFile *ImportedBy, 3900 unsigned ClientLoadCapabilities) { 3901 unsigned Idx = 0; 3902 F.ModuleMapPath = ReadPath(F, Record, Idx); 3903 3904 // Try to resolve ModuleName in the current header search context and 3905 // verify that it is found in the same module map file as we saved. If the 3906 // top-level AST file is a main file, skip this check because there is no 3907 // usable header search context. 3908 assert(!F.ModuleName.empty() && 3909 "MODULE_NAME should come before MODULE_MAP_FILE"); 3910 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3911 // An implicitly-loaded module file should have its module listed in some 3912 // module map file that we've already loaded. 3913 Module *M = 3914 PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc); 3915 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3916 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3917 // Don't emit module relocation error if we have -fno-validate-pch 3918 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 3919 DisableValidationForModuleKind::Module) && 3920 !ModMap) { 3921 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) { 3922 if (auto ASTFE = M ? M->getASTFile() : None) { 3923 // This module was defined by an imported (explicit) module. 3924 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3925 << ASTFE->getName(); 3926 } else { 3927 // This module was built with a different module map. 3928 Diag(diag::err_imported_module_not_found) 3929 << F.ModuleName << F.FileName 3930 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3931 << !ImportedBy; 3932 // In case it was imported by a PCH, there's a chance the user is 3933 // just missing to include the search path to the directory containing 3934 // the modulemap. 3935 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3936 Diag(diag::note_imported_by_pch_module_not_found) 3937 << llvm::sys::path::parent_path(F.ModuleMapPath); 3938 } 3939 } 3940 return OutOfDate; 3941 } 3942 3943 assert(M && M->Name == F.ModuleName && "found module with different name"); 3944 3945 // Check the primary module map file. 3946 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3947 if (!StoredModMap || *StoredModMap != ModMap) { 3948 assert(ModMap && "found module is missing module map file"); 3949 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3950 "top-level import should be verified"); 3951 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3952 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3953 Diag(diag::err_imported_module_modmap_changed) 3954 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3955 << ModMap->getName() << F.ModuleMapPath << NotImported; 3956 return OutOfDate; 3957 } 3958 3959 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3960 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3961 // FIXME: we should use input files rather than storing names. 3962 std::string Filename = ReadPath(F, Record, Idx); 3963 auto SF = FileMgr.getFile(Filename, false, false); 3964 if (!SF) { 3965 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3966 Error("could not find file '" + Filename +"' referenced by AST file"); 3967 return OutOfDate; 3968 } 3969 AdditionalStoredMaps.insert(*SF); 3970 } 3971 3972 // Check any additional module map files (e.g. module.private.modulemap) 3973 // that are not in the pcm. 3974 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3975 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3976 // Remove files that match 3977 // Note: SmallPtrSet::erase is really remove 3978 if (!AdditionalStoredMaps.erase(ModMap)) { 3979 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3980 Diag(diag::err_module_different_modmap) 3981 << F.ModuleName << /*new*/0 << ModMap->getName(); 3982 return OutOfDate; 3983 } 3984 } 3985 } 3986 3987 // Check any additional module map files that are in the pcm, but not 3988 // found in header search. Cases that match are already removed. 3989 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3990 if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) 3991 Diag(diag::err_module_different_modmap) 3992 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3993 return OutOfDate; 3994 } 3995 } 3996 3997 if (Listener) 3998 Listener->ReadModuleMapFile(F.ModuleMapPath); 3999 return Success; 4000 } 4001 4002 /// Move the given method to the back of the global list of methods. 4003 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 4004 // Find the entry for this selector in the method pool. 4005 Sema::GlobalMethodPool::iterator Known 4006 = S.MethodPool.find(Method->getSelector()); 4007 if (Known == S.MethodPool.end()) 4008 return; 4009 4010 // Retrieve the appropriate method list. 4011 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 4012 : Known->second.second; 4013 bool Found = false; 4014 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 4015 if (!Found) { 4016 if (List->getMethod() == Method) { 4017 Found = true; 4018 } else { 4019 // Keep searching. 4020 continue; 4021 } 4022 } 4023 4024 if (List->getNext()) 4025 List->setMethod(List->getNext()->getMethod()); 4026 else 4027 List->setMethod(Method); 4028 } 4029 } 4030 4031 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4032 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4033 for (Decl *D : Names) { 4034 bool wasHidden = !D->isUnconditionallyVisible(); 4035 D->setVisibleDespiteOwningModule(); 4036 4037 if (wasHidden && SemaObj) { 4038 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4039 moveMethodToBackOfGlobalList(*SemaObj, Method); 4040 } 4041 } 4042 } 4043 } 4044 4045 void ASTReader::makeModuleVisible(Module *Mod, 4046 Module::NameVisibilityKind NameVisibility, 4047 SourceLocation ImportLoc) { 4048 llvm::SmallPtrSet<Module *, 4> Visited; 4049 SmallVector<Module *, 4> Stack; 4050 Stack.push_back(Mod); 4051 while (!Stack.empty()) { 4052 Mod = Stack.pop_back_val(); 4053 4054 if (NameVisibility <= Mod->NameVisibility) { 4055 // This module already has this level of visibility (or greater), so 4056 // there is nothing more to do. 4057 continue; 4058 } 4059 4060 if (Mod->isUnimportable()) { 4061 // Modules that aren't importable cannot be made visible. 4062 continue; 4063 } 4064 4065 // Update the module's name visibility. 4066 Mod->NameVisibility = NameVisibility; 4067 4068 // If we've already deserialized any names from this module, 4069 // mark them as visible. 4070 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4071 if (Hidden != HiddenNamesMap.end()) { 4072 auto HiddenNames = std::move(*Hidden); 4073 HiddenNamesMap.erase(Hidden); 4074 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4075 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4076 "making names visible added hidden names"); 4077 } 4078 4079 // Push any exported modules onto the stack to be marked as visible. 4080 SmallVector<Module *, 16> Exports; 4081 Mod->getExportedModules(Exports); 4082 for (SmallVectorImpl<Module *>::iterator 4083 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4084 Module *Exported = *I; 4085 if (Visited.insert(Exported).second) 4086 Stack.push_back(Exported); 4087 } 4088 } 4089 } 4090 4091 /// We've merged the definition \p MergedDef into the existing definition 4092 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4093 /// visible. 4094 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4095 NamedDecl *MergedDef) { 4096 if (!Def->isUnconditionallyVisible()) { 4097 // If MergedDef is visible or becomes visible, make the definition visible. 4098 if (MergedDef->isUnconditionallyVisible()) 4099 Def->setVisibleDespiteOwningModule(); 4100 else { 4101 getContext().mergeDefinitionIntoModule( 4102 Def, MergedDef->getImportedOwningModule(), 4103 /*NotifyListeners*/ false); 4104 PendingMergedDefinitionsToDeduplicate.insert(Def); 4105 } 4106 } 4107 } 4108 4109 bool ASTReader::loadGlobalIndex() { 4110 if (GlobalIndex) 4111 return false; 4112 4113 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4114 !PP.getLangOpts().Modules) 4115 return true; 4116 4117 // Try to load the global index. 4118 TriedLoadingGlobalIndex = true; 4119 StringRef ModuleCachePath 4120 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4121 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4122 GlobalModuleIndex::readIndex(ModuleCachePath); 4123 if (llvm::Error Err = std::move(Result.second)) { 4124 assert(!Result.first); 4125 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4126 return true; 4127 } 4128 4129 GlobalIndex.reset(Result.first); 4130 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4131 return false; 4132 } 4133 4134 bool ASTReader::isGlobalIndexUnavailable() const { 4135 return PP.getLangOpts().Modules && UseGlobalIndex && 4136 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4137 } 4138 4139 static void updateModuleTimestamp(ModuleFile &MF) { 4140 // Overwrite the timestamp file contents so that file's mtime changes. 4141 std::string TimestampFilename = MF.getTimestampFilename(); 4142 std::error_code EC; 4143 llvm::raw_fd_ostream OS(TimestampFilename, EC, 4144 llvm::sys::fs::OF_TextWithCRLF); 4145 if (EC) 4146 return; 4147 OS << "Timestamp file\n"; 4148 OS.close(); 4149 OS.clear_error(); // Avoid triggering a fatal error. 4150 } 4151 4152 /// Given a cursor at the start of an AST file, scan ahead and drop the 4153 /// cursor into the start of the given block ID, returning false on success and 4154 /// true on failure. 4155 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4156 while (true) { 4157 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4158 if (!MaybeEntry) { 4159 // FIXME this drops errors on the floor. 4160 consumeError(MaybeEntry.takeError()); 4161 return true; 4162 } 4163 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4164 4165 switch (Entry.Kind) { 4166 case llvm::BitstreamEntry::Error: 4167 case llvm::BitstreamEntry::EndBlock: 4168 return true; 4169 4170 case llvm::BitstreamEntry::Record: 4171 // Ignore top-level records. 4172 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4173 break; 4174 else { 4175 // FIXME this drops errors on the floor. 4176 consumeError(Skipped.takeError()); 4177 return true; 4178 } 4179 4180 case llvm::BitstreamEntry::SubBlock: 4181 if (Entry.ID == BlockID) { 4182 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4183 // FIXME this drops the error on the floor. 4184 consumeError(std::move(Err)); 4185 return true; 4186 } 4187 // Found it! 4188 return false; 4189 } 4190 4191 if (llvm::Error Err = Cursor.SkipBlock()) { 4192 // FIXME this drops the error on the floor. 4193 consumeError(std::move(Err)); 4194 return true; 4195 } 4196 } 4197 } 4198 } 4199 4200 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4201 ModuleKind Type, 4202 SourceLocation ImportLoc, 4203 unsigned ClientLoadCapabilities, 4204 SmallVectorImpl<ImportedSubmodule> *Imported) { 4205 llvm::SaveAndRestore<SourceLocation> 4206 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4207 llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII( 4208 CurrentDeserializingModuleKind, Type); 4209 4210 // Defer any pending actions until we get to the end of reading the AST file. 4211 Deserializing AnASTFile(this); 4212 4213 // Bump the generation number. 4214 unsigned PreviousGeneration = 0; 4215 if (ContextObj) 4216 PreviousGeneration = incrementGeneration(*ContextObj); 4217 4218 unsigned NumModules = ModuleMgr.size(); 4219 SmallVector<ImportedModule, 4> Loaded; 4220 if (ASTReadResult ReadResult = 4221 ReadASTCore(FileName, Type, ImportLoc, 4222 /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(), 4223 ClientLoadCapabilities)) { 4224 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4225 PP.getLangOpts().Modules 4226 ? &PP.getHeaderSearchInfo().getModuleMap() 4227 : nullptr); 4228 4229 // If we find that any modules are unusable, the global index is going 4230 // to be out-of-date. Just remove it. 4231 GlobalIndex.reset(); 4232 ModuleMgr.setGlobalIndex(nullptr); 4233 return ReadResult; 4234 } 4235 4236 // Here comes stuff that we only do once the entire chain is loaded. Do *not* 4237 // remove modules from this point. Various fields are updated during reading 4238 // the AST block and removing the modules would result in dangling pointers. 4239 // They are generally only incidentally dereferenced, ie. a binary search 4240 // runs over `GlobalSLocEntryMap`, which could cause an invalid module to 4241 // be dereferenced but it wouldn't actually be used. 4242 4243 // Load the AST blocks of all of the modules that we loaded. We can still 4244 // hit errors parsing the ASTs at this point. 4245 for (ImportedModule &M : Loaded) { 4246 ModuleFile &F = *M.Mod; 4247 4248 // Read the AST block. 4249 if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) { 4250 Error(std::move(Err)); 4251 return Failure; 4252 } 4253 4254 // The AST block should always have a definition for the main module. 4255 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4256 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4257 return Failure; 4258 } 4259 4260 // Read the extension blocks. 4261 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4262 if (llvm::Error Err = ReadExtensionBlock(F)) { 4263 Error(std::move(Err)); 4264 return Failure; 4265 } 4266 } 4267 4268 // Once read, set the ModuleFile bit base offset and update the size in 4269 // bits of all files we've seen. 4270 F.GlobalBitOffset = TotalModulesSizeInBits; 4271 TotalModulesSizeInBits += F.SizeInBits; 4272 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4273 } 4274 4275 // Preload source locations and interesting indentifiers. 4276 for (ImportedModule &M : Loaded) { 4277 ModuleFile &F = *M.Mod; 4278 4279 // Preload SLocEntries. 4280 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4281 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4282 // Load it through the SourceManager and don't call ReadSLocEntry() 4283 // directly because the entry may have already been loaded in which case 4284 // calling ReadSLocEntry() directly would trigger an assertion in 4285 // SourceManager. 4286 SourceMgr.getLoadedSLocEntryByID(Index); 4287 } 4288 4289 // Map the original source file ID into the ID space of the current 4290 // compilation. 4291 if (F.OriginalSourceFileID.isValid()) { 4292 F.OriginalSourceFileID = FileID::get( 4293 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4294 } 4295 4296 // Preload all the pending interesting identifiers by marking them out of 4297 // date. 4298 for (auto Offset : F.PreloadIdentifierOffsets) { 4299 const unsigned char *Data = F.IdentifierTableData + Offset; 4300 4301 ASTIdentifierLookupTrait Trait(*this, F); 4302 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4303 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4304 auto &II = PP.getIdentifierTable().getOwn(Key); 4305 II.setOutOfDate(true); 4306 4307 // Mark this identifier as being from an AST file so that we can track 4308 // whether we need to serialize it. 4309 markIdentifierFromAST(*this, II); 4310 4311 // Associate the ID with the identifier so that the writer can reuse it. 4312 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4313 SetIdentifierInfo(ID, &II); 4314 } 4315 } 4316 4317 // Setup the import locations and notify the module manager that we've 4318 // committed to these module files. 4319 for (ImportedModule &M : Loaded) { 4320 ModuleFile &F = *M.Mod; 4321 4322 ModuleMgr.moduleFileAccepted(&F); 4323 4324 // Set the import location. 4325 F.DirectImportLoc = ImportLoc; 4326 // FIXME: We assume that locations from PCH / preamble do not need 4327 // any translation. 4328 if (!M.ImportedBy) 4329 F.ImportLoc = M.ImportLoc; 4330 else 4331 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4332 } 4333 4334 if (!PP.getLangOpts().CPlusPlus || 4335 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4336 Type != MK_PrebuiltModule)) { 4337 // Mark all of the identifiers in the identifier table as being out of date, 4338 // so that various accessors know to check the loaded modules when the 4339 // identifier is used. 4340 // 4341 // For C++ modules, we don't need information on many identifiers (just 4342 // those that provide macros or are poisoned), so we mark all of 4343 // the interesting ones via PreloadIdentifierOffsets. 4344 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4345 IdEnd = PP.getIdentifierTable().end(); 4346 Id != IdEnd; ++Id) 4347 Id->second->setOutOfDate(true); 4348 } 4349 // Mark selectors as out of date. 4350 for (auto Sel : SelectorGeneration) 4351 SelectorOutOfDate[Sel.first] = true; 4352 4353 // Resolve any unresolved module exports. 4354 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4355 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4356 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4357 Module *ResolvedMod = getSubmodule(GlobalID); 4358 4359 switch (Unresolved.Kind) { 4360 case UnresolvedModuleRef::Conflict: 4361 if (ResolvedMod) { 4362 Module::Conflict Conflict; 4363 Conflict.Other = ResolvedMod; 4364 Conflict.Message = Unresolved.String.str(); 4365 Unresolved.Mod->Conflicts.push_back(Conflict); 4366 } 4367 continue; 4368 4369 case UnresolvedModuleRef::Import: 4370 if (ResolvedMod) 4371 Unresolved.Mod->Imports.insert(ResolvedMod); 4372 continue; 4373 4374 case UnresolvedModuleRef::Export: 4375 if (ResolvedMod || Unresolved.IsWildcard) 4376 Unresolved.Mod->Exports.push_back( 4377 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4378 continue; 4379 } 4380 } 4381 UnresolvedModuleRefs.clear(); 4382 4383 if (Imported) 4384 Imported->append(ImportedModules.begin(), 4385 ImportedModules.end()); 4386 4387 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4388 // Might be unnecessary as use declarations are only used to build the 4389 // module itself. 4390 4391 if (ContextObj) 4392 InitializeContext(); 4393 4394 if (SemaObj) 4395 UpdateSema(); 4396 4397 if (DeserializationListener) 4398 DeserializationListener->ReaderInitialized(this); 4399 4400 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4401 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4402 // If this AST file is a precompiled preamble, then set the 4403 // preamble file ID of the source manager to the file source file 4404 // from which the preamble was built. 4405 if (Type == MK_Preamble) { 4406 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4407 } else if (Type == MK_MainFile) { 4408 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4409 } 4410 } 4411 4412 // For any Objective-C class definitions we have already loaded, make sure 4413 // that we load any additional categories. 4414 if (ContextObj) { 4415 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4416 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4417 ObjCClassesLoaded[I], 4418 PreviousGeneration); 4419 } 4420 } 4421 4422 if (PP.getHeaderSearchInfo() 4423 .getHeaderSearchOpts() 4424 .ModulesValidateOncePerBuildSession) { 4425 // Now we are certain that the module and all modules it depends on are 4426 // up to date. Create or update timestamp files for modules that are 4427 // located in the module cache (not for PCH files that could be anywhere 4428 // in the filesystem). 4429 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4430 ImportedModule &M = Loaded[I]; 4431 if (M.Mod->Kind == MK_ImplicitModule) { 4432 updateModuleTimestamp(*M.Mod); 4433 } 4434 } 4435 } 4436 4437 return Success; 4438 } 4439 4440 static ASTFileSignature readASTFileSignature(StringRef PCH); 4441 4442 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4443 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4444 // FIXME checking magic headers is done in other places such as 4445 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4446 // always done the same. Unify it all with a helper. 4447 if (!Stream.canSkipToPos(4)) 4448 return llvm::createStringError(std::errc::illegal_byte_sequence, 4449 "file too small to contain AST file magic"); 4450 for (unsigned C : {'C', 'P', 'C', 'H'}) 4451 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4452 if (Res.get() != C) 4453 return llvm::createStringError( 4454 std::errc::illegal_byte_sequence, 4455 "file doesn't start with AST file magic"); 4456 } else 4457 return Res.takeError(); 4458 return llvm::Error::success(); 4459 } 4460 4461 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4462 switch (Kind) { 4463 case MK_PCH: 4464 return 0; // PCH 4465 case MK_ImplicitModule: 4466 case MK_ExplicitModule: 4467 case MK_PrebuiltModule: 4468 return 1; // module 4469 case MK_MainFile: 4470 case MK_Preamble: 4471 return 2; // main source file 4472 } 4473 llvm_unreachable("unknown module kind"); 4474 } 4475 4476 ASTReader::ASTReadResult 4477 ASTReader::ReadASTCore(StringRef FileName, 4478 ModuleKind Type, 4479 SourceLocation ImportLoc, 4480 ModuleFile *ImportedBy, 4481 SmallVectorImpl<ImportedModule> &Loaded, 4482 off_t ExpectedSize, time_t ExpectedModTime, 4483 ASTFileSignature ExpectedSignature, 4484 unsigned ClientLoadCapabilities) { 4485 ModuleFile *M; 4486 std::string ErrorStr; 4487 ModuleManager::AddModuleResult AddResult 4488 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4489 getGeneration(), ExpectedSize, ExpectedModTime, 4490 ExpectedSignature, readASTFileSignature, 4491 M, ErrorStr); 4492 4493 switch (AddResult) { 4494 case ModuleManager::AlreadyLoaded: 4495 Diag(diag::remark_module_import) 4496 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4497 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4498 return Success; 4499 4500 case ModuleManager::NewlyLoaded: 4501 // Load module file below. 4502 break; 4503 4504 case ModuleManager::Missing: 4505 // The module file was missing; if the client can handle that, return 4506 // it. 4507 if (ClientLoadCapabilities & ARR_Missing) 4508 return Missing; 4509 4510 // Otherwise, return an error. 4511 Diag(diag::err_ast_file_not_found) 4512 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4513 << ErrorStr; 4514 return Failure; 4515 4516 case ModuleManager::OutOfDate: 4517 // We couldn't load the module file because it is out-of-date. If the 4518 // client can handle out-of-date, return it. 4519 if (ClientLoadCapabilities & ARR_OutOfDate) 4520 return OutOfDate; 4521 4522 // Otherwise, return an error. 4523 Diag(diag::err_ast_file_out_of_date) 4524 << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty() 4525 << ErrorStr; 4526 return Failure; 4527 } 4528 4529 assert(M && "Missing module file"); 4530 4531 bool ShouldFinalizePCM = false; 4532 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4533 auto &MC = getModuleManager().getModuleCache(); 4534 if (ShouldFinalizePCM) 4535 MC.finalizePCM(FileName); 4536 else 4537 MC.tryToDropPCM(FileName); 4538 }); 4539 ModuleFile &F = *M; 4540 BitstreamCursor &Stream = F.Stream; 4541 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4542 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4543 4544 // Sniff for the signature. 4545 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4546 Diag(diag::err_ast_file_invalid) 4547 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4548 return Failure; 4549 } 4550 4551 // This is used for compatibility with older PCH formats. 4552 bool HaveReadControlBlock = false; 4553 while (true) { 4554 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4555 if (!MaybeEntry) { 4556 Error(MaybeEntry.takeError()); 4557 return Failure; 4558 } 4559 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4560 4561 switch (Entry.Kind) { 4562 case llvm::BitstreamEntry::Error: 4563 case llvm::BitstreamEntry::Record: 4564 case llvm::BitstreamEntry::EndBlock: 4565 Error("invalid record at top-level of AST file"); 4566 return Failure; 4567 4568 case llvm::BitstreamEntry::SubBlock: 4569 break; 4570 } 4571 4572 switch (Entry.ID) { 4573 case CONTROL_BLOCK_ID: 4574 HaveReadControlBlock = true; 4575 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4576 case Success: 4577 // Check that we didn't try to load a non-module AST file as a module. 4578 // 4579 // FIXME: Should we also perform the converse check? Loading a module as 4580 // a PCH file sort of works, but it's a bit wonky. 4581 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4582 Type == MK_PrebuiltModule) && 4583 F.ModuleName.empty()) { 4584 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4585 if (Result != OutOfDate || 4586 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4587 Diag(diag::err_module_file_not_module) << FileName; 4588 return Result; 4589 } 4590 break; 4591 4592 case Failure: return Failure; 4593 case Missing: return Missing; 4594 case OutOfDate: return OutOfDate; 4595 case VersionMismatch: return VersionMismatch; 4596 case ConfigurationMismatch: return ConfigurationMismatch; 4597 case HadErrors: return HadErrors; 4598 } 4599 break; 4600 4601 case AST_BLOCK_ID: 4602 if (!HaveReadControlBlock) { 4603 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4604 Diag(diag::err_pch_version_too_old); 4605 return VersionMismatch; 4606 } 4607 4608 // Record that we've loaded this module. 4609 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4610 ShouldFinalizePCM = true; 4611 return Success; 4612 4613 case UNHASHED_CONTROL_BLOCK_ID: 4614 // This block is handled using look-ahead during ReadControlBlock. We 4615 // shouldn't get here! 4616 Error("malformed block record in AST file"); 4617 return Failure; 4618 4619 default: 4620 if (llvm::Error Err = Stream.SkipBlock()) { 4621 Error(std::move(Err)); 4622 return Failure; 4623 } 4624 break; 4625 } 4626 } 4627 4628 llvm_unreachable("unexpected break; expected return"); 4629 } 4630 4631 ASTReader::ASTReadResult 4632 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4633 unsigned ClientLoadCapabilities) { 4634 const HeaderSearchOptions &HSOpts = 4635 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4636 bool AllowCompatibleConfigurationMismatch = 4637 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4638 bool DisableValidation = shouldDisableValidationForFile(F); 4639 4640 ASTReadResult Result = readUnhashedControlBlockImpl( 4641 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4642 Listener.get(), 4643 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4644 4645 // If F was directly imported by another module, it's implicitly validated by 4646 // the importing module. 4647 if (DisableValidation || WasImportedBy || 4648 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4649 return Success; 4650 4651 if (Result == Failure) { 4652 Error("malformed block record in AST file"); 4653 return Failure; 4654 } 4655 4656 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4657 // If this module has already been finalized in the ModuleCache, we're stuck 4658 // with it; we can only load a single version of each module. 4659 // 4660 // This can happen when a module is imported in two contexts: in one, as a 4661 // user module; in another, as a system module (due to an import from 4662 // another module marked with the [system] flag). It usually indicates a 4663 // bug in the module map: this module should also be marked with [system]. 4664 // 4665 // If -Wno-system-headers (the default), and the first import is as a 4666 // system module, then validation will fail during the as-user import, 4667 // since -Werror flags won't have been validated. However, it's reasonable 4668 // to treat this consistently as a system module. 4669 // 4670 // If -Wsystem-headers, the PCM on disk was built with 4671 // -Wno-system-headers, and the first import is as a user module, then 4672 // validation will fail during the as-system import since the PCM on disk 4673 // doesn't guarantee that -Werror was respected. However, the -Werror 4674 // flags were checked during the initial as-user import. 4675 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4676 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4677 return Success; 4678 } 4679 } 4680 4681 return Result; 4682 } 4683 4684 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4685 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4686 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4687 bool ValidateDiagnosticOptions) { 4688 // Initialize a stream. 4689 BitstreamCursor Stream(StreamData); 4690 4691 // Sniff for the signature. 4692 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4693 // FIXME this drops the error on the floor. 4694 consumeError(std::move(Err)); 4695 return Failure; 4696 } 4697 4698 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4699 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4700 return Failure; 4701 4702 // Read all of the records in the options block. 4703 RecordData Record; 4704 ASTReadResult Result = Success; 4705 while (true) { 4706 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4707 if (!MaybeEntry) { 4708 // FIXME this drops the error on the floor. 4709 consumeError(MaybeEntry.takeError()); 4710 return Failure; 4711 } 4712 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4713 4714 switch (Entry.Kind) { 4715 case llvm::BitstreamEntry::Error: 4716 case llvm::BitstreamEntry::SubBlock: 4717 return Failure; 4718 4719 case llvm::BitstreamEntry::EndBlock: 4720 return Result; 4721 4722 case llvm::BitstreamEntry::Record: 4723 // The interesting case. 4724 break; 4725 } 4726 4727 // Read and process a record. 4728 Record.clear(); 4729 StringRef Blob; 4730 Expected<unsigned> MaybeRecordType = 4731 Stream.readRecord(Entry.ID, Record, &Blob); 4732 if (!MaybeRecordType) { 4733 // FIXME this drops the error. 4734 return Failure; 4735 } 4736 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4737 case SIGNATURE: 4738 if (F) 4739 F->Signature = ASTFileSignature::create(Record.begin(), Record.end()); 4740 break; 4741 case AST_BLOCK_HASH: 4742 if (F) 4743 F->ASTBlockHash = 4744 ASTFileSignature::create(Record.begin(), Record.end()); 4745 break; 4746 case DIAGNOSTIC_OPTIONS: { 4747 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4748 if (Listener && ValidateDiagnosticOptions && 4749 !AllowCompatibleConfigurationMismatch && 4750 ParseDiagnosticOptions(Record, Complain, *Listener)) 4751 Result = OutOfDate; // Don't return early. Read the signature. 4752 break; 4753 } 4754 case DIAG_PRAGMA_MAPPINGS: 4755 if (!F) 4756 break; 4757 if (F->PragmaDiagMappings.empty()) 4758 F->PragmaDiagMappings.swap(Record); 4759 else 4760 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4761 Record.begin(), Record.end()); 4762 break; 4763 case HEADER_SEARCH_ENTRY_USAGE: 4764 if (!F) 4765 break; 4766 unsigned Count = Record[0]; 4767 const char *Byte = Blob.data(); 4768 F->SearchPathUsage = llvm::BitVector(Count, 0); 4769 for (unsigned I = 0; I < Count; ++Byte) 4770 for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I) 4771 if (*Byte & (1 << Bit)) 4772 F->SearchPathUsage[I] = 1; 4773 break; 4774 } 4775 } 4776 } 4777 4778 /// Parse a record and blob containing module file extension metadata. 4779 static bool parseModuleFileExtensionMetadata( 4780 const SmallVectorImpl<uint64_t> &Record, 4781 StringRef Blob, 4782 ModuleFileExtensionMetadata &Metadata) { 4783 if (Record.size() < 4) return true; 4784 4785 Metadata.MajorVersion = Record[0]; 4786 Metadata.MinorVersion = Record[1]; 4787 4788 unsigned BlockNameLen = Record[2]; 4789 unsigned UserInfoLen = Record[3]; 4790 4791 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4792 4793 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4794 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4795 Blob.data() + BlockNameLen + UserInfoLen); 4796 return false; 4797 } 4798 4799 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) { 4800 BitstreamCursor &Stream = F.Stream; 4801 4802 RecordData Record; 4803 while (true) { 4804 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4805 if (!MaybeEntry) 4806 return MaybeEntry.takeError(); 4807 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4808 4809 switch (Entry.Kind) { 4810 case llvm::BitstreamEntry::SubBlock: 4811 if (llvm::Error Err = Stream.SkipBlock()) 4812 return Err; 4813 continue; 4814 case llvm::BitstreamEntry::EndBlock: 4815 return llvm::Error::success(); 4816 case llvm::BitstreamEntry::Error: 4817 return llvm::createStringError(std::errc::illegal_byte_sequence, 4818 "malformed block record in AST file"); 4819 case llvm::BitstreamEntry::Record: 4820 break; 4821 } 4822 4823 Record.clear(); 4824 StringRef Blob; 4825 Expected<unsigned> MaybeRecCode = 4826 Stream.readRecord(Entry.ID, Record, &Blob); 4827 if (!MaybeRecCode) 4828 return MaybeRecCode.takeError(); 4829 switch (MaybeRecCode.get()) { 4830 case EXTENSION_METADATA: { 4831 ModuleFileExtensionMetadata Metadata; 4832 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 4833 return llvm::createStringError( 4834 std::errc::illegal_byte_sequence, 4835 "malformed EXTENSION_METADATA in AST file"); 4836 4837 // Find a module file extension with this block name. 4838 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4839 if (Known == ModuleFileExtensions.end()) break; 4840 4841 // Form a reader. 4842 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4843 F, Stream)) { 4844 F.ExtensionReaders.push_back(std::move(Reader)); 4845 } 4846 4847 break; 4848 } 4849 } 4850 } 4851 4852 return llvm::Error::success(); 4853 } 4854 4855 void ASTReader::InitializeContext() { 4856 assert(ContextObj && "no context to initialize"); 4857 ASTContext &Context = *ContextObj; 4858 4859 // If there's a listener, notify them that we "read" the translation unit. 4860 if (DeserializationListener) 4861 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4862 Context.getTranslationUnitDecl()); 4863 4864 // FIXME: Find a better way to deal with collisions between these 4865 // built-in types. Right now, we just ignore the problem. 4866 4867 // Load the special types. 4868 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4869 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4870 if (!Context.CFConstantStringTypeDecl) 4871 Context.setCFConstantStringType(GetType(String)); 4872 } 4873 4874 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4875 QualType FileType = GetType(File); 4876 if (FileType.isNull()) { 4877 Error("FILE type is NULL"); 4878 return; 4879 } 4880 4881 if (!Context.FILEDecl) { 4882 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4883 Context.setFILEDecl(Typedef->getDecl()); 4884 else { 4885 const TagType *Tag = FileType->getAs<TagType>(); 4886 if (!Tag) { 4887 Error("Invalid FILE type in AST file"); 4888 return; 4889 } 4890 Context.setFILEDecl(Tag->getDecl()); 4891 } 4892 } 4893 } 4894 4895 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4896 QualType Jmp_bufType = GetType(Jmp_buf); 4897 if (Jmp_bufType.isNull()) { 4898 Error("jmp_buf type is NULL"); 4899 return; 4900 } 4901 4902 if (!Context.jmp_bufDecl) { 4903 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4904 Context.setjmp_bufDecl(Typedef->getDecl()); 4905 else { 4906 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4907 if (!Tag) { 4908 Error("Invalid jmp_buf type in AST file"); 4909 return; 4910 } 4911 Context.setjmp_bufDecl(Tag->getDecl()); 4912 } 4913 } 4914 } 4915 4916 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4917 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4918 if (Sigjmp_bufType.isNull()) { 4919 Error("sigjmp_buf type is NULL"); 4920 return; 4921 } 4922 4923 if (!Context.sigjmp_bufDecl) { 4924 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4925 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4926 else { 4927 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4928 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4929 Context.setsigjmp_bufDecl(Tag->getDecl()); 4930 } 4931 } 4932 } 4933 4934 if (unsigned ObjCIdRedef 4935 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4936 if (Context.ObjCIdRedefinitionType.isNull()) 4937 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4938 } 4939 4940 if (unsigned ObjCClassRedef 4941 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4942 if (Context.ObjCClassRedefinitionType.isNull()) 4943 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4944 } 4945 4946 if (unsigned ObjCSelRedef 4947 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4948 if (Context.ObjCSelRedefinitionType.isNull()) 4949 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4950 } 4951 4952 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4953 QualType Ucontext_tType = GetType(Ucontext_t); 4954 if (Ucontext_tType.isNull()) { 4955 Error("ucontext_t type is NULL"); 4956 return; 4957 } 4958 4959 if (!Context.ucontext_tDecl) { 4960 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4961 Context.setucontext_tDecl(Typedef->getDecl()); 4962 else { 4963 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4964 assert(Tag && "Invalid ucontext_t type in AST file"); 4965 Context.setucontext_tDecl(Tag->getDecl()); 4966 } 4967 } 4968 } 4969 } 4970 4971 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4972 4973 // If there were any CUDA special declarations, deserialize them. 4974 if (!CUDASpecialDeclRefs.empty()) { 4975 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4976 Context.setcudaConfigureCallDecl( 4977 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4978 } 4979 4980 // Re-export any modules that were imported by a non-module AST file. 4981 // FIXME: This does not make macro-only imports visible again. 4982 for (auto &Import : ImportedModules) { 4983 if (Module *Imported = getSubmodule(Import.ID)) { 4984 makeModuleVisible(Imported, Module::AllVisible, 4985 /*ImportLoc=*/Import.ImportLoc); 4986 if (Import.ImportLoc.isValid()) 4987 PP.makeModuleVisible(Imported, Import.ImportLoc); 4988 // This updates visibility for Preprocessor only. For Sema, which can be 4989 // nullptr here, we do the same later, in UpdateSema(). 4990 } 4991 } 4992 } 4993 4994 void ASTReader::finalizeForWriting() { 4995 // Nothing to do for now. 4996 } 4997 4998 /// Reads and return the signature record from \p PCH's control block, or 4999 /// else returns 0. 5000 static ASTFileSignature readASTFileSignature(StringRef PCH) { 5001 BitstreamCursor Stream(PCH); 5002 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5003 // FIXME this drops the error on the floor. 5004 consumeError(std::move(Err)); 5005 return ASTFileSignature(); 5006 } 5007 5008 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5009 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 5010 return ASTFileSignature(); 5011 5012 // Scan for SIGNATURE inside the diagnostic options block. 5013 ASTReader::RecordData Record; 5014 while (true) { 5015 Expected<llvm::BitstreamEntry> MaybeEntry = 5016 Stream.advanceSkippingSubblocks(); 5017 if (!MaybeEntry) { 5018 // FIXME this drops the error on the floor. 5019 consumeError(MaybeEntry.takeError()); 5020 return ASTFileSignature(); 5021 } 5022 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5023 5024 if (Entry.Kind != llvm::BitstreamEntry::Record) 5025 return ASTFileSignature(); 5026 5027 Record.clear(); 5028 StringRef Blob; 5029 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5030 if (!MaybeRecord) { 5031 // FIXME this drops the error on the floor. 5032 consumeError(MaybeRecord.takeError()); 5033 return ASTFileSignature(); 5034 } 5035 if (SIGNATURE == MaybeRecord.get()) 5036 return ASTFileSignature::create(Record.begin(), 5037 Record.begin() + ASTFileSignature::size); 5038 } 5039 } 5040 5041 /// Retrieve the name of the original source file name 5042 /// directly from the AST file, without actually loading the AST 5043 /// file. 5044 std::string ASTReader::getOriginalSourceFile( 5045 const std::string &ASTFileName, FileManager &FileMgr, 5046 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5047 // Open the AST file. 5048 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5049 if (!Buffer) { 5050 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5051 << ASTFileName << Buffer.getError().message(); 5052 return std::string(); 5053 } 5054 5055 // Initialize the stream 5056 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5057 5058 // Sniff for the signature. 5059 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5060 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5061 return std::string(); 5062 } 5063 5064 // Scan for the CONTROL_BLOCK_ID block. 5065 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5066 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5067 return std::string(); 5068 } 5069 5070 // Scan for ORIGINAL_FILE inside the control block. 5071 RecordData Record; 5072 while (true) { 5073 Expected<llvm::BitstreamEntry> MaybeEntry = 5074 Stream.advanceSkippingSubblocks(); 5075 if (!MaybeEntry) { 5076 // FIXME this drops errors on the floor. 5077 consumeError(MaybeEntry.takeError()); 5078 return std::string(); 5079 } 5080 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5081 5082 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5083 return std::string(); 5084 5085 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5086 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5087 return std::string(); 5088 } 5089 5090 Record.clear(); 5091 StringRef Blob; 5092 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5093 if (!MaybeRecord) { 5094 // FIXME this drops the errors on the floor. 5095 consumeError(MaybeRecord.takeError()); 5096 return std::string(); 5097 } 5098 if (ORIGINAL_FILE == MaybeRecord.get()) 5099 return Blob.str(); 5100 } 5101 } 5102 5103 namespace { 5104 5105 class SimplePCHValidator : public ASTReaderListener { 5106 const LangOptions &ExistingLangOpts; 5107 const TargetOptions &ExistingTargetOpts; 5108 const PreprocessorOptions &ExistingPPOpts; 5109 std::string ExistingModuleCachePath; 5110 FileManager &FileMgr; 5111 5112 public: 5113 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5114 const TargetOptions &ExistingTargetOpts, 5115 const PreprocessorOptions &ExistingPPOpts, 5116 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5117 : ExistingLangOpts(ExistingLangOpts), 5118 ExistingTargetOpts(ExistingTargetOpts), 5119 ExistingPPOpts(ExistingPPOpts), 5120 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5121 5122 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5123 bool AllowCompatibleDifferences) override { 5124 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5125 AllowCompatibleDifferences); 5126 } 5127 5128 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5129 bool AllowCompatibleDifferences) override { 5130 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5131 AllowCompatibleDifferences); 5132 } 5133 5134 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5135 StringRef SpecificModuleCachePath, 5136 bool Complain) override { 5137 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5138 ExistingModuleCachePath, nullptr, 5139 ExistingLangOpts, ExistingPPOpts); 5140 } 5141 5142 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5143 bool Complain, 5144 std::string &SuggestedPredefines) override { 5145 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5146 SuggestedPredefines, ExistingLangOpts); 5147 } 5148 }; 5149 5150 } // namespace 5151 5152 bool ASTReader::readASTFileControlBlock( 5153 StringRef Filename, FileManager &FileMgr, 5154 const PCHContainerReader &PCHContainerRdr, 5155 bool FindModuleFileExtensions, 5156 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5157 // Open the AST file. 5158 // FIXME: This allows use of the VFS; we do not allow use of the 5159 // VFS when actually loading a module. 5160 auto Buffer = FileMgr.getBufferForFile(Filename); 5161 if (!Buffer) { 5162 return true; 5163 } 5164 5165 // Initialize the stream 5166 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5167 BitstreamCursor Stream(Bytes); 5168 5169 // Sniff for the signature. 5170 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5171 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5172 return true; 5173 } 5174 5175 // Scan for the CONTROL_BLOCK_ID block. 5176 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5177 return true; 5178 5179 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5180 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5181 bool NeedsImports = Listener.needsImportVisitation(); 5182 BitstreamCursor InputFilesCursor; 5183 5184 RecordData Record; 5185 std::string ModuleDir; 5186 bool DoneWithControlBlock = false; 5187 while (!DoneWithControlBlock) { 5188 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5189 if (!MaybeEntry) { 5190 // FIXME this drops the error on the floor. 5191 consumeError(MaybeEntry.takeError()); 5192 return true; 5193 } 5194 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5195 5196 switch (Entry.Kind) { 5197 case llvm::BitstreamEntry::SubBlock: { 5198 switch (Entry.ID) { 5199 case OPTIONS_BLOCK_ID: { 5200 std::string IgnoredSuggestedPredefines; 5201 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5202 /*AllowCompatibleConfigurationMismatch*/ false, 5203 Listener, IgnoredSuggestedPredefines) != Success) 5204 return true; 5205 break; 5206 } 5207 5208 case INPUT_FILES_BLOCK_ID: 5209 InputFilesCursor = Stream; 5210 if (llvm::Error Err = Stream.SkipBlock()) { 5211 // FIXME this drops the error on the floor. 5212 consumeError(std::move(Err)); 5213 return true; 5214 } 5215 if (NeedsInputFiles && 5216 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5217 return true; 5218 break; 5219 5220 default: 5221 if (llvm::Error Err = Stream.SkipBlock()) { 5222 // FIXME this drops the error on the floor. 5223 consumeError(std::move(Err)); 5224 return true; 5225 } 5226 break; 5227 } 5228 5229 continue; 5230 } 5231 5232 case llvm::BitstreamEntry::EndBlock: 5233 DoneWithControlBlock = true; 5234 break; 5235 5236 case llvm::BitstreamEntry::Error: 5237 return true; 5238 5239 case llvm::BitstreamEntry::Record: 5240 break; 5241 } 5242 5243 if (DoneWithControlBlock) break; 5244 5245 Record.clear(); 5246 StringRef Blob; 5247 Expected<unsigned> MaybeRecCode = 5248 Stream.readRecord(Entry.ID, Record, &Blob); 5249 if (!MaybeRecCode) { 5250 // FIXME this drops the error. 5251 return Failure; 5252 } 5253 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5254 case METADATA: 5255 if (Record[0] != VERSION_MAJOR) 5256 return true; 5257 if (Listener.ReadFullVersionInformation(Blob)) 5258 return true; 5259 break; 5260 case MODULE_NAME: 5261 Listener.ReadModuleName(Blob); 5262 break; 5263 case MODULE_DIRECTORY: 5264 ModuleDir = std::string(Blob); 5265 break; 5266 case MODULE_MAP_FILE: { 5267 unsigned Idx = 0; 5268 auto Path = ReadString(Record, Idx); 5269 ResolveImportedPath(Path, ModuleDir); 5270 Listener.ReadModuleMapFile(Path); 5271 break; 5272 } 5273 case INPUT_FILE_OFFSETS: { 5274 if (!NeedsInputFiles) 5275 break; 5276 5277 unsigned NumInputFiles = Record[0]; 5278 unsigned NumUserFiles = Record[1]; 5279 const llvm::support::unaligned_uint64_t *InputFileOffs = 5280 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5281 for (unsigned I = 0; I != NumInputFiles; ++I) { 5282 // Go find this input file. 5283 bool isSystemFile = I >= NumUserFiles; 5284 5285 if (isSystemFile && !NeedsSystemInputFiles) 5286 break; // the rest are system input files 5287 5288 BitstreamCursor &Cursor = InputFilesCursor; 5289 SavedStreamPosition SavedPosition(Cursor); 5290 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5291 // FIXME this drops errors on the floor. 5292 consumeError(std::move(Err)); 5293 } 5294 5295 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5296 if (!MaybeCode) { 5297 // FIXME this drops errors on the floor. 5298 consumeError(MaybeCode.takeError()); 5299 } 5300 unsigned Code = MaybeCode.get(); 5301 5302 RecordData Record; 5303 StringRef Blob; 5304 bool shouldContinue = false; 5305 Expected<unsigned> MaybeRecordType = 5306 Cursor.readRecord(Code, Record, &Blob); 5307 if (!MaybeRecordType) { 5308 // FIXME this drops errors on the floor. 5309 consumeError(MaybeRecordType.takeError()); 5310 } 5311 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5312 case INPUT_FILE_HASH: 5313 break; 5314 case INPUT_FILE: 5315 bool Overridden = static_cast<bool>(Record[3]); 5316 std::string Filename = std::string(Blob); 5317 ResolveImportedPath(Filename, ModuleDir); 5318 shouldContinue = Listener.visitInputFile( 5319 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5320 break; 5321 } 5322 if (!shouldContinue) 5323 break; 5324 } 5325 break; 5326 } 5327 5328 case IMPORTS: { 5329 if (!NeedsImports) 5330 break; 5331 5332 unsigned Idx = 0, N = Record.size(); 5333 while (Idx < N) { 5334 // Read information about the AST file. 5335 Idx += 5336 1 + 1 + 1 + 1 + 5337 ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature 5338 std::string ModuleName = ReadString(Record, Idx); 5339 std::string Filename = ReadString(Record, Idx); 5340 ResolveImportedPath(Filename, ModuleDir); 5341 Listener.visitImport(ModuleName, Filename); 5342 } 5343 break; 5344 } 5345 5346 default: 5347 // No other validation to perform. 5348 break; 5349 } 5350 } 5351 5352 // Look for module file extension blocks, if requested. 5353 if (FindModuleFileExtensions) { 5354 BitstreamCursor SavedStream = Stream; 5355 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5356 bool DoneWithExtensionBlock = false; 5357 while (!DoneWithExtensionBlock) { 5358 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5359 if (!MaybeEntry) { 5360 // FIXME this drops the error. 5361 return true; 5362 } 5363 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5364 5365 switch (Entry.Kind) { 5366 case llvm::BitstreamEntry::SubBlock: 5367 if (llvm::Error Err = Stream.SkipBlock()) { 5368 // FIXME this drops the error on the floor. 5369 consumeError(std::move(Err)); 5370 return true; 5371 } 5372 continue; 5373 5374 case llvm::BitstreamEntry::EndBlock: 5375 DoneWithExtensionBlock = true; 5376 continue; 5377 5378 case llvm::BitstreamEntry::Error: 5379 return true; 5380 5381 case llvm::BitstreamEntry::Record: 5382 break; 5383 } 5384 5385 Record.clear(); 5386 StringRef Blob; 5387 Expected<unsigned> MaybeRecCode = 5388 Stream.readRecord(Entry.ID, Record, &Blob); 5389 if (!MaybeRecCode) { 5390 // FIXME this drops the error. 5391 return true; 5392 } 5393 switch (MaybeRecCode.get()) { 5394 case EXTENSION_METADATA: { 5395 ModuleFileExtensionMetadata Metadata; 5396 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5397 return true; 5398 5399 Listener.readModuleFileExtension(Metadata); 5400 break; 5401 } 5402 } 5403 } 5404 } 5405 Stream = SavedStream; 5406 } 5407 5408 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5409 if (readUnhashedControlBlockImpl( 5410 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5411 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5412 ValidateDiagnosticOptions) != Success) 5413 return true; 5414 5415 return false; 5416 } 5417 5418 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5419 const PCHContainerReader &PCHContainerRdr, 5420 const LangOptions &LangOpts, 5421 const TargetOptions &TargetOpts, 5422 const PreprocessorOptions &PPOpts, 5423 StringRef ExistingModuleCachePath) { 5424 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5425 ExistingModuleCachePath, FileMgr); 5426 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5427 /*FindModuleFileExtensions=*/false, 5428 validator, 5429 /*ValidateDiagnosticOptions=*/true); 5430 } 5431 5432 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F, 5433 unsigned ClientLoadCapabilities) { 5434 // Enter the submodule block. 5435 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) 5436 return Err; 5437 5438 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5439 bool First = true; 5440 Module *CurrentModule = nullptr; 5441 RecordData Record; 5442 while (true) { 5443 Expected<llvm::BitstreamEntry> MaybeEntry = 5444 F.Stream.advanceSkippingSubblocks(); 5445 if (!MaybeEntry) 5446 return MaybeEntry.takeError(); 5447 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5448 5449 switch (Entry.Kind) { 5450 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5451 case llvm::BitstreamEntry::Error: 5452 return llvm::createStringError(std::errc::illegal_byte_sequence, 5453 "malformed block record in AST file"); 5454 case llvm::BitstreamEntry::EndBlock: 5455 return llvm::Error::success(); 5456 case llvm::BitstreamEntry::Record: 5457 // The interesting case. 5458 break; 5459 } 5460 5461 // Read a record. 5462 StringRef Blob; 5463 Record.clear(); 5464 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5465 if (!MaybeKind) 5466 return MaybeKind.takeError(); 5467 unsigned Kind = MaybeKind.get(); 5468 5469 if ((Kind == SUBMODULE_METADATA) != First) 5470 return llvm::createStringError( 5471 std::errc::illegal_byte_sequence, 5472 "submodule metadata record should be at beginning of block"); 5473 First = false; 5474 5475 // Submodule information is only valid if we have a current module. 5476 // FIXME: Should we error on these cases? 5477 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5478 Kind != SUBMODULE_DEFINITION) 5479 continue; 5480 5481 switch (Kind) { 5482 default: // Default behavior: ignore. 5483 break; 5484 5485 case SUBMODULE_DEFINITION: { 5486 if (Record.size() < 12) 5487 return llvm::createStringError(std::errc::illegal_byte_sequence, 5488 "malformed module definition"); 5489 5490 StringRef Name = Blob; 5491 unsigned Idx = 0; 5492 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5493 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5494 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5495 bool IsFramework = Record[Idx++]; 5496 bool IsExplicit = Record[Idx++]; 5497 bool IsSystem = Record[Idx++]; 5498 bool IsExternC = Record[Idx++]; 5499 bool InferSubmodules = Record[Idx++]; 5500 bool InferExplicitSubmodules = Record[Idx++]; 5501 bool InferExportWildcard = Record[Idx++]; 5502 bool ConfigMacrosExhaustive = Record[Idx++]; 5503 bool ModuleMapIsPrivate = Record[Idx++]; 5504 5505 Module *ParentModule = nullptr; 5506 if (Parent) 5507 ParentModule = getSubmodule(Parent); 5508 5509 // Retrieve this (sub)module from the module map, creating it if 5510 // necessary. 5511 CurrentModule = 5512 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5513 .first; 5514 5515 // FIXME: set the definition loc for CurrentModule, or call 5516 // ModMap.setInferredModuleAllowedBy() 5517 5518 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5519 if (GlobalIndex >= SubmodulesLoaded.size() || 5520 SubmodulesLoaded[GlobalIndex]) 5521 return llvm::createStringError(std::errc::invalid_argument, 5522 "too many submodules"); 5523 5524 if (!ParentModule) { 5525 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5526 // Don't emit module relocation error if we have -fno-validate-pch 5527 if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation & 5528 DisableValidationForModuleKind::Module) && 5529 CurFile != F.File) { 5530 auto ConflictError = 5531 PartialDiagnostic(diag::err_module_file_conflict, 5532 ContextObj->DiagAllocator) 5533 << CurrentModule->getTopLevelModuleName() << CurFile->getName() 5534 << F.File->getName(); 5535 return DiagnosticError::create(CurrentImportLoc, ConflictError); 5536 } 5537 } 5538 5539 F.DidReadTopLevelSubmodule = true; 5540 CurrentModule->setASTFile(F.File); 5541 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5542 } 5543 5544 CurrentModule->Kind = Kind; 5545 CurrentModule->Signature = F.Signature; 5546 CurrentModule->IsFromModuleFile = true; 5547 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5548 CurrentModule->IsExternC = IsExternC; 5549 CurrentModule->InferSubmodules = InferSubmodules; 5550 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5551 CurrentModule->InferExportWildcard = InferExportWildcard; 5552 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5553 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5554 if (DeserializationListener) 5555 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5556 5557 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5558 5559 // Clear out data that will be replaced by what is in the module file. 5560 CurrentModule->LinkLibraries.clear(); 5561 CurrentModule->ConfigMacros.clear(); 5562 CurrentModule->UnresolvedConflicts.clear(); 5563 CurrentModule->Conflicts.clear(); 5564 5565 // The module is available unless it's missing a requirement; relevant 5566 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5567 // Missing headers that were present when the module was built do not 5568 // make it unavailable -- if we got this far, this must be an explicitly 5569 // imported module file. 5570 CurrentModule->Requirements.clear(); 5571 CurrentModule->MissingHeaders.clear(); 5572 CurrentModule->IsUnimportable = 5573 ParentModule && ParentModule->IsUnimportable; 5574 CurrentModule->IsAvailable = !CurrentModule->IsUnimportable; 5575 break; 5576 } 5577 5578 case SUBMODULE_UMBRELLA_HEADER: { 5579 // FIXME: This doesn't work for framework modules as `Filename` is the 5580 // name as written in the module file and does not include 5581 // `Headers/`, so this path will never exist. 5582 std::string Filename = std::string(Blob); 5583 ResolveImportedPath(F, Filename); 5584 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5585 if (!CurrentModule->getUmbrellaHeader()) { 5586 // FIXME: NameAsWritten 5587 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, ""); 5588 } 5589 // Note that it's too late at this point to return out of date if the 5590 // name from the PCM doesn't match up with the one in the module map, 5591 // but also quite unlikely since we will have already checked the 5592 // modification time and size of the module map file itself. 5593 } 5594 break; 5595 } 5596 5597 case SUBMODULE_HEADER: 5598 case SUBMODULE_EXCLUDED_HEADER: 5599 case SUBMODULE_PRIVATE_HEADER: 5600 // We lazily associate headers with their modules via the HeaderInfo table. 5601 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5602 // of complete filenames or remove it entirely. 5603 break; 5604 5605 case SUBMODULE_TEXTUAL_HEADER: 5606 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5607 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5608 // them here. 5609 break; 5610 5611 case SUBMODULE_TOPHEADER: 5612 CurrentModule->addTopHeaderFilename(Blob); 5613 break; 5614 5615 case SUBMODULE_UMBRELLA_DIR: { 5616 // See comments in SUBMODULE_UMBRELLA_HEADER 5617 std::string Dirname = std::string(Blob); 5618 ResolveImportedPath(F, Dirname); 5619 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5620 if (!CurrentModule->getUmbrellaDir()) { 5621 // FIXME: NameAsWritten 5622 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, ""); 5623 } 5624 } 5625 break; 5626 } 5627 5628 case SUBMODULE_METADATA: { 5629 F.BaseSubmoduleID = getTotalNumSubmodules(); 5630 F.LocalNumSubmodules = Record[0]; 5631 unsigned LocalBaseSubmoduleID = Record[1]; 5632 if (F.LocalNumSubmodules > 0) { 5633 // Introduce the global -> local mapping for submodules within this 5634 // module. 5635 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5636 5637 // Introduce the local -> global mapping for submodules within this 5638 // module. 5639 F.SubmoduleRemap.insertOrReplace( 5640 std::make_pair(LocalBaseSubmoduleID, 5641 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5642 5643 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5644 } 5645 break; 5646 } 5647 5648 case SUBMODULE_IMPORTS: 5649 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5650 UnresolvedModuleRef Unresolved; 5651 Unresolved.File = &F; 5652 Unresolved.Mod = CurrentModule; 5653 Unresolved.ID = Record[Idx]; 5654 Unresolved.Kind = UnresolvedModuleRef::Import; 5655 Unresolved.IsWildcard = false; 5656 UnresolvedModuleRefs.push_back(Unresolved); 5657 } 5658 break; 5659 5660 case SUBMODULE_EXPORTS: 5661 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5662 UnresolvedModuleRef Unresolved; 5663 Unresolved.File = &F; 5664 Unresolved.Mod = CurrentModule; 5665 Unresolved.ID = Record[Idx]; 5666 Unresolved.Kind = UnresolvedModuleRef::Export; 5667 Unresolved.IsWildcard = Record[Idx + 1]; 5668 UnresolvedModuleRefs.push_back(Unresolved); 5669 } 5670 5671 // Once we've loaded the set of exports, there's no reason to keep 5672 // the parsed, unresolved exports around. 5673 CurrentModule->UnresolvedExports.clear(); 5674 break; 5675 5676 case SUBMODULE_REQUIRES: 5677 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5678 PP.getTargetInfo()); 5679 break; 5680 5681 case SUBMODULE_LINK_LIBRARY: 5682 ModMap.resolveLinkAsDependencies(CurrentModule); 5683 CurrentModule->LinkLibraries.push_back( 5684 Module::LinkLibrary(std::string(Blob), Record[0])); 5685 break; 5686 5687 case SUBMODULE_CONFIG_MACRO: 5688 CurrentModule->ConfigMacros.push_back(Blob.str()); 5689 break; 5690 5691 case SUBMODULE_CONFLICT: { 5692 UnresolvedModuleRef Unresolved; 5693 Unresolved.File = &F; 5694 Unresolved.Mod = CurrentModule; 5695 Unresolved.ID = Record[0]; 5696 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5697 Unresolved.IsWildcard = false; 5698 Unresolved.String = Blob; 5699 UnresolvedModuleRefs.push_back(Unresolved); 5700 break; 5701 } 5702 5703 case SUBMODULE_INITIALIZERS: { 5704 if (!ContextObj) 5705 break; 5706 SmallVector<uint32_t, 16> Inits; 5707 for (auto &ID : Record) 5708 Inits.push_back(getGlobalDeclID(F, ID)); 5709 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5710 break; 5711 } 5712 5713 case SUBMODULE_EXPORT_AS: 5714 CurrentModule->ExportAsModule = Blob.str(); 5715 ModMap.addLinkAsDependency(CurrentModule); 5716 break; 5717 } 5718 } 5719 } 5720 5721 /// Parse the record that corresponds to a LangOptions data 5722 /// structure. 5723 /// 5724 /// This routine parses the language options from the AST file and then gives 5725 /// them to the AST listener if one is set. 5726 /// 5727 /// \returns true if the listener deems the file unacceptable, false otherwise. 5728 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5729 bool Complain, 5730 ASTReaderListener &Listener, 5731 bool AllowCompatibleDifferences) { 5732 LangOptions LangOpts; 5733 unsigned Idx = 0; 5734 #define LANGOPT(Name, Bits, Default, Description) \ 5735 LangOpts.Name = Record[Idx++]; 5736 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5737 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5738 #include "clang/Basic/LangOptions.def" 5739 #define SANITIZER(NAME, ID) \ 5740 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5741 #include "clang/Basic/Sanitizers.def" 5742 5743 for (unsigned N = Record[Idx++]; N; --N) 5744 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5745 5746 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5747 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5748 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5749 5750 LangOpts.CurrentModule = ReadString(Record, Idx); 5751 5752 // Comment options. 5753 for (unsigned N = Record[Idx++]; N; --N) { 5754 LangOpts.CommentOpts.BlockCommandNames.push_back( 5755 ReadString(Record, Idx)); 5756 } 5757 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5758 5759 // OpenMP offloading options. 5760 for (unsigned N = Record[Idx++]; N; --N) { 5761 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5762 } 5763 5764 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5765 5766 return Listener.ReadLanguageOptions(LangOpts, Complain, 5767 AllowCompatibleDifferences); 5768 } 5769 5770 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5771 ASTReaderListener &Listener, 5772 bool AllowCompatibleDifferences) { 5773 unsigned Idx = 0; 5774 TargetOptions TargetOpts; 5775 TargetOpts.Triple = ReadString(Record, Idx); 5776 TargetOpts.CPU = ReadString(Record, Idx); 5777 TargetOpts.TuneCPU = ReadString(Record, Idx); 5778 TargetOpts.ABI = ReadString(Record, Idx); 5779 for (unsigned N = Record[Idx++]; N; --N) { 5780 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5781 } 5782 for (unsigned N = Record[Idx++]; N; --N) { 5783 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5784 } 5785 5786 return Listener.ReadTargetOptions(TargetOpts, Complain, 5787 AllowCompatibleDifferences); 5788 } 5789 5790 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5791 ASTReaderListener &Listener) { 5792 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5793 unsigned Idx = 0; 5794 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5795 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5796 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5797 #include "clang/Basic/DiagnosticOptions.def" 5798 5799 for (unsigned N = Record[Idx++]; N; --N) 5800 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5801 for (unsigned N = Record[Idx++]; N; --N) 5802 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5803 5804 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5805 } 5806 5807 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5808 ASTReaderListener &Listener) { 5809 FileSystemOptions FSOpts; 5810 unsigned Idx = 0; 5811 FSOpts.WorkingDir = ReadString(Record, Idx); 5812 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5813 } 5814 5815 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5816 bool Complain, 5817 ASTReaderListener &Listener) { 5818 HeaderSearchOptions HSOpts; 5819 unsigned Idx = 0; 5820 HSOpts.Sysroot = ReadString(Record, Idx); 5821 5822 // Include entries. 5823 for (unsigned N = Record[Idx++]; N; --N) { 5824 std::string Path = ReadString(Record, Idx); 5825 frontend::IncludeDirGroup Group 5826 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5827 bool IsFramework = Record[Idx++]; 5828 bool IgnoreSysRoot = Record[Idx++]; 5829 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5830 IgnoreSysRoot); 5831 } 5832 5833 // System header prefixes. 5834 for (unsigned N = Record[Idx++]; N; --N) { 5835 std::string Prefix = ReadString(Record, Idx); 5836 bool IsSystemHeader = Record[Idx++]; 5837 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5838 } 5839 5840 HSOpts.ResourceDir = ReadString(Record, Idx); 5841 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5842 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5843 HSOpts.DisableModuleHash = Record[Idx++]; 5844 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5845 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5846 HSOpts.EnablePrebuiltImplicitModules = Record[Idx++]; 5847 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5848 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5849 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5850 HSOpts.UseLibcxx = Record[Idx++]; 5851 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5852 5853 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5854 Complain); 5855 } 5856 5857 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5858 bool Complain, 5859 ASTReaderListener &Listener, 5860 std::string &SuggestedPredefines) { 5861 PreprocessorOptions PPOpts; 5862 unsigned Idx = 0; 5863 5864 // Macro definitions/undefs 5865 for (unsigned N = Record[Idx++]; N; --N) { 5866 std::string Macro = ReadString(Record, Idx); 5867 bool IsUndef = Record[Idx++]; 5868 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5869 } 5870 5871 // Includes 5872 for (unsigned N = Record[Idx++]; N; --N) { 5873 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5874 } 5875 5876 // Macro Includes 5877 for (unsigned N = Record[Idx++]; N; --N) { 5878 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5879 } 5880 5881 PPOpts.UsePredefines = Record[Idx++]; 5882 PPOpts.DetailedRecord = Record[Idx++]; 5883 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5884 PPOpts.ObjCXXARCStandardLibrary = 5885 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5886 SuggestedPredefines.clear(); 5887 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5888 SuggestedPredefines); 5889 } 5890 5891 std::pair<ModuleFile *, unsigned> 5892 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5893 GlobalPreprocessedEntityMapType::iterator 5894 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5895 assert(I != GlobalPreprocessedEntityMap.end() && 5896 "Corrupted global preprocessed entity map"); 5897 ModuleFile *M = I->second; 5898 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5899 return std::make_pair(M, LocalIndex); 5900 } 5901 5902 llvm::iterator_range<PreprocessingRecord::iterator> 5903 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5904 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5905 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5906 Mod.NumPreprocessedEntities); 5907 5908 return llvm::make_range(PreprocessingRecord::iterator(), 5909 PreprocessingRecord::iterator()); 5910 } 5911 5912 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName, 5913 unsigned int ClientLoadCapabilities) { 5914 return ClientLoadCapabilities & ARR_OutOfDate && 5915 !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName); 5916 } 5917 5918 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5919 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5920 return llvm::make_range( 5921 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5922 ModuleDeclIterator(this, &Mod, 5923 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5924 } 5925 5926 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5927 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5928 assert(I != GlobalSkippedRangeMap.end() && 5929 "Corrupted global skipped range map"); 5930 ModuleFile *M = I->second; 5931 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5932 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5933 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5934 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5935 TranslateSourceLocation(*M, RawRange.getEnd())); 5936 assert(Range.isValid()); 5937 return Range; 5938 } 5939 5940 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5941 PreprocessedEntityID PPID = Index+1; 5942 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5943 ModuleFile &M = *PPInfo.first; 5944 unsigned LocalIndex = PPInfo.second; 5945 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5946 5947 if (!PP.getPreprocessingRecord()) { 5948 Error("no preprocessing record"); 5949 return nullptr; 5950 } 5951 5952 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5953 if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit( 5954 M.MacroOffsetsBase + PPOffs.BitOffset)) { 5955 Error(std::move(Err)); 5956 return nullptr; 5957 } 5958 5959 Expected<llvm::BitstreamEntry> MaybeEntry = 5960 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5961 if (!MaybeEntry) { 5962 Error(MaybeEntry.takeError()); 5963 return nullptr; 5964 } 5965 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5966 5967 if (Entry.Kind != llvm::BitstreamEntry::Record) 5968 return nullptr; 5969 5970 // Read the record. 5971 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5972 TranslateSourceLocation(M, PPOffs.getEnd())); 5973 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5974 StringRef Blob; 5975 RecordData Record; 5976 Expected<unsigned> MaybeRecType = 5977 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5978 if (!MaybeRecType) { 5979 Error(MaybeRecType.takeError()); 5980 return nullptr; 5981 } 5982 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 5983 case PPD_MACRO_EXPANSION: { 5984 bool isBuiltin = Record[0]; 5985 IdentifierInfo *Name = nullptr; 5986 MacroDefinitionRecord *Def = nullptr; 5987 if (isBuiltin) 5988 Name = getLocalIdentifier(M, Record[1]); 5989 else { 5990 PreprocessedEntityID GlobalID = 5991 getGlobalPreprocessedEntityID(M, Record[1]); 5992 Def = cast<MacroDefinitionRecord>( 5993 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5994 } 5995 5996 MacroExpansion *ME; 5997 if (isBuiltin) 5998 ME = new (PPRec) MacroExpansion(Name, Range); 5999 else 6000 ME = new (PPRec) MacroExpansion(Def, Range); 6001 6002 return ME; 6003 } 6004 6005 case PPD_MACRO_DEFINITION: { 6006 // Decode the identifier info and then check again; if the macro is 6007 // still defined and associated with the identifier, 6008 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 6009 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 6010 6011 if (DeserializationListener) 6012 DeserializationListener->MacroDefinitionRead(PPID, MD); 6013 6014 return MD; 6015 } 6016 6017 case PPD_INCLUSION_DIRECTIVE: { 6018 const char *FullFileNameStart = Blob.data() + Record[0]; 6019 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 6020 const FileEntry *File = nullptr; 6021 if (!FullFileName.empty()) 6022 if (auto FE = PP.getFileManager().getFile(FullFileName)) 6023 File = *FE; 6024 6025 // FIXME: Stable encoding 6026 InclusionDirective::InclusionKind Kind 6027 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 6028 InclusionDirective *ID 6029 = new (PPRec) InclusionDirective(PPRec, Kind, 6030 StringRef(Blob.data(), Record[0]), 6031 Record[1], Record[3], 6032 File, 6033 Range); 6034 return ID; 6035 } 6036 } 6037 6038 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 6039 } 6040 6041 /// Find the next module that contains entities and return the ID 6042 /// of the first entry. 6043 /// 6044 /// \param SLocMapI points at a chunk of a module that contains no 6045 /// preprocessed entities or the entities it contains are not the ones we are 6046 /// looking for. 6047 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6048 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6049 ++SLocMapI; 6050 for (GlobalSLocOffsetMapType::const_iterator 6051 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6052 ModuleFile &M = *SLocMapI->second; 6053 if (M.NumPreprocessedEntities) 6054 return M.BasePreprocessedEntityID; 6055 } 6056 6057 return getTotalNumPreprocessedEntities(); 6058 } 6059 6060 namespace { 6061 6062 struct PPEntityComp { 6063 const ASTReader &Reader; 6064 ModuleFile &M; 6065 6066 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6067 6068 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6069 SourceLocation LHS = getLoc(L); 6070 SourceLocation RHS = getLoc(R); 6071 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6072 } 6073 6074 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6075 SourceLocation LHS = getLoc(L); 6076 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6077 } 6078 6079 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6080 SourceLocation RHS = getLoc(R); 6081 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6082 } 6083 6084 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6085 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6086 } 6087 }; 6088 6089 } // namespace 6090 6091 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6092 bool EndsAfter) const { 6093 if (SourceMgr.isLocalSourceLocation(Loc)) 6094 return getTotalNumPreprocessedEntities(); 6095 6096 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6097 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6098 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6099 "Corrupted global sloc offset map"); 6100 6101 if (SLocMapI->second->NumPreprocessedEntities == 0) 6102 return findNextPreprocessedEntity(SLocMapI); 6103 6104 ModuleFile &M = *SLocMapI->second; 6105 6106 using pp_iterator = const PPEntityOffset *; 6107 6108 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6109 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6110 6111 size_t Count = M.NumPreprocessedEntities; 6112 size_t Half; 6113 pp_iterator First = pp_begin; 6114 pp_iterator PPI; 6115 6116 if (EndsAfter) { 6117 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6118 PPEntityComp(*this, M)); 6119 } else { 6120 // Do a binary search manually instead of using std::lower_bound because 6121 // The end locations of entities may be unordered (when a macro expansion 6122 // is inside another macro argument), but for this case it is not important 6123 // whether we get the first macro expansion or its containing macro. 6124 while (Count > 0) { 6125 Half = Count / 2; 6126 PPI = First; 6127 std::advance(PPI, Half); 6128 if (SourceMgr.isBeforeInTranslationUnit( 6129 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6130 First = PPI; 6131 ++First; 6132 Count = Count - Half - 1; 6133 } else 6134 Count = Half; 6135 } 6136 } 6137 6138 if (PPI == pp_end) 6139 return findNextPreprocessedEntity(SLocMapI); 6140 6141 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6142 } 6143 6144 /// Returns a pair of [Begin, End) indices of preallocated 6145 /// preprocessed entities that \arg Range encompasses. 6146 std::pair<unsigned, unsigned> 6147 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6148 if (Range.isInvalid()) 6149 return std::make_pair(0,0); 6150 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6151 6152 PreprocessedEntityID BeginID = 6153 findPreprocessedEntity(Range.getBegin(), false); 6154 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6155 return std::make_pair(BeginID, EndID); 6156 } 6157 6158 /// Optionally returns true or false if the preallocated preprocessed 6159 /// entity with index \arg Index came from file \arg FID. 6160 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6161 FileID FID) { 6162 if (FID.isInvalid()) 6163 return false; 6164 6165 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6166 ModuleFile &M = *PPInfo.first; 6167 unsigned LocalIndex = PPInfo.second; 6168 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6169 6170 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6171 if (Loc.isInvalid()) 6172 return false; 6173 6174 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6175 return true; 6176 else 6177 return false; 6178 } 6179 6180 namespace { 6181 6182 /// Visitor used to search for information about a header file. 6183 class HeaderFileInfoVisitor { 6184 const FileEntry *FE; 6185 Optional<HeaderFileInfo> HFI; 6186 6187 public: 6188 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6189 6190 bool operator()(ModuleFile &M) { 6191 HeaderFileInfoLookupTable *Table 6192 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6193 if (!Table) 6194 return false; 6195 6196 // Look in the on-disk hash table for an entry for this file name. 6197 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6198 if (Pos == Table->end()) 6199 return false; 6200 6201 HFI = *Pos; 6202 return true; 6203 } 6204 6205 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6206 }; 6207 6208 } // namespace 6209 6210 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6211 HeaderFileInfoVisitor Visitor(FE); 6212 ModuleMgr.visit(Visitor); 6213 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6214 return *HFI; 6215 6216 return HeaderFileInfo(); 6217 } 6218 6219 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6220 using DiagState = DiagnosticsEngine::DiagState; 6221 SmallVector<DiagState *, 32> DiagStates; 6222 6223 for (ModuleFile &F : ModuleMgr) { 6224 unsigned Idx = 0; 6225 auto &Record = F.PragmaDiagMappings; 6226 if (Record.empty()) 6227 continue; 6228 6229 DiagStates.clear(); 6230 6231 auto ReadDiagState = 6232 [&](const DiagState &BasedOn, SourceLocation Loc, 6233 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6234 unsigned BackrefID = Record[Idx++]; 6235 if (BackrefID != 0) 6236 return DiagStates[BackrefID - 1]; 6237 6238 // A new DiagState was created here. 6239 Diag.DiagStates.push_back(BasedOn); 6240 DiagState *NewState = &Diag.DiagStates.back(); 6241 DiagStates.push_back(NewState); 6242 unsigned Size = Record[Idx++]; 6243 assert(Idx + Size * 2 <= Record.size() && 6244 "Invalid data, not enough diag/map pairs"); 6245 while (Size--) { 6246 unsigned DiagID = Record[Idx++]; 6247 DiagnosticMapping NewMapping = 6248 DiagnosticMapping::deserialize(Record[Idx++]); 6249 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6250 continue; 6251 6252 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6253 6254 // If this mapping was specified as a warning but the severity was 6255 // upgraded due to diagnostic settings, simulate the current diagnostic 6256 // settings (and use a warning). 6257 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6258 NewMapping.setSeverity(diag::Severity::Warning); 6259 NewMapping.setUpgradedFromWarning(false); 6260 } 6261 6262 Mapping = NewMapping; 6263 } 6264 return NewState; 6265 }; 6266 6267 // Read the first state. 6268 DiagState *FirstState; 6269 if (F.Kind == MK_ImplicitModule) { 6270 // Implicitly-built modules are reused with different diagnostic 6271 // settings. Use the initial diagnostic state from Diag to simulate this 6272 // compilation's diagnostic settings. 6273 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6274 DiagStates.push_back(FirstState); 6275 6276 // Skip the initial diagnostic state from the serialized module. 6277 assert(Record[1] == 0 && 6278 "Invalid data, unexpected backref in initial state"); 6279 Idx = 3 + Record[2] * 2; 6280 assert(Idx < Record.size() && 6281 "Invalid data, not enough state change pairs in initial state"); 6282 } else if (F.isModule()) { 6283 // For an explicit module, preserve the flags from the module build 6284 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6285 // -Wblah flags. 6286 unsigned Flags = Record[Idx++]; 6287 DiagState Initial; 6288 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6289 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6290 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6291 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6292 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6293 Initial.ExtBehavior = (diag::Severity)Flags; 6294 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6295 6296 assert(F.OriginalSourceFileID.isValid()); 6297 6298 // Set up the root buffer of the module to start with the initial 6299 // diagnostic state of the module itself, to cover files that contain no 6300 // explicit transitions (for which we did not serialize anything). 6301 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6302 .StateTransitions.push_back({FirstState, 0}); 6303 } else { 6304 // For prefix ASTs, start with whatever the user configured on the 6305 // command line. 6306 Idx++; // Skip flags. 6307 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6308 SourceLocation(), false); 6309 } 6310 6311 // Read the state transitions. 6312 unsigned NumLocations = Record[Idx++]; 6313 while (NumLocations--) { 6314 assert(Idx < Record.size() && 6315 "Invalid data, missing pragma diagnostic states"); 6316 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6317 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6318 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6319 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6320 unsigned Transitions = Record[Idx++]; 6321 6322 // Note that we don't need to set up Parent/ParentOffset here, because 6323 // we won't be changing the diagnostic state within imported FileIDs 6324 // (other than perhaps appending to the main source file, which has no 6325 // parent). 6326 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6327 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6328 for (unsigned I = 0; I != Transitions; ++I) { 6329 unsigned Offset = Record[Idx++]; 6330 auto *State = 6331 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6332 F.StateTransitions.push_back({State, Offset}); 6333 } 6334 } 6335 6336 // Read the final state. 6337 assert(Idx < Record.size() && 6338 "Invalid data, missing final pragma diagnostic state"); 6339 SourceLocation CurStateLoc = 6340 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6341 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6342 6343 if (!F.isModule()) { 6344 Diag.DiagStatesByLoc.CurDiagState = CurState; 6345 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6346 6347 // Preserve the property that the imaginary root file describes the 6348 // current state. 6349 FileID NullFile; 6350 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6351 if (T.empty()) 6352 T.push_back({CurState, 0}); 6353 else 6354 T[0].State = CurState; 6355 } 6356 6357 // Don't try to read these mappings again. 6358 Record.clear(); 6359 } 6360 } 6361 6362 /// Get the correct cursor and offset for loading a type. 6363 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6364 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6365 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6366 ModuleFile *M = I->second; 6367 return RecordLocation( 6368 M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() + 6369 M->DeclsBlockStartOffset); 6370 } 6371 6372 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6373 switch (code) { 6374 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6375 case TYPE_##CODE_ID: return Type::CLASS_ID; 6376 #include "clang/Serialization/TypeBitCodes.def" 6377 default: return llvm::None; 6378 } 6379 } 6380 6381 /// Read and return the type with the given index.. 6382 /// 6383 /// The index is the type ID, shifted and minus the number of predefs. This 6384 /// routine actually reads the record corresponding to the type at the given 6385 /// location. It is a helper routine for GetType, which deals with reading type 6386 /// IDs. 6387 QualType ASTReader::readTypeRecord(unsigned Index) { 6388 assert(ContextObj && "reading type with no AST context"); 6389 ASTContext &Context = *ContextObj; 6390 RecordLocation Loc = TypeCursorForIndex(Index); 6391 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6392 6393 // Keep track of where we are in the stream, then jump back there 6394 // after reading this type. 6395 SavedStreamPosition SavedPosition(DeclsCursor); 6396 6397 ReadingKindTracker ReadingKind(Read_Type, *this); 6398 6399 // Note that we are loading a type record. 6400 Deserializing AType(this); 6401 6402 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6403 Error(std::move(Err)); 6404 return QualType(); 6405 } 6406 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6407 if (!RawCode) { 6408 Error(RawCode.takeError()); 6409 return QualType(); 6410 } 6411 6412 ASTRecordReader Record(*this, *Loc.F); 6413 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6414 if (!Code) { 6415 Error(Code.takeError()); 6416 return QualType(); 6417 } 6418 if (Code.get() == TYPE_EXT_QUAL) { 6419 QualType baseType = Record.readQualType(); 6420 Qualifiers quals = Record.readQualifiers(); 6421 return Context.getQualifiedType(baseType, quals); 6422 } 6423 6424 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6425 if (!maybeClass) { 6426 Error("Unexpected code for type"); 6427 return QualType(); 6428 } 6429 6430 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6431 return TypeReader.read(*maybeClass); 6432 } 6433 6434 namespace clang { 6435 6436 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6437 ASTRecordReader &Reader; 6438 6439 SourceLocation readSourceLocation() { 6440 return Reader.readSourceLocation(); 6441 } 6442 6443 TypeSourceInfo *GetTypeSourceInfo() { 6444 return Reader.readTypeSourceInfo(); 6445 } 6446 6447 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6448 return Reader.readNestedNameSpecifierLoc(); 6449 } 6450 6451 Attr *ReadAttr() { 6452 return Reader.readAttr(); 6453 } 6454 6455 public: 6456 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6457 6458 // We want compile-time assurance that we've enumerated all of 6459 // these, so unfortunately we have to declare them first, then 6460 // define them out-of-line. 6461 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6462 #define TYPELOC(CLASS, PARENT) \ 6463 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6464 #include "clang/AST/TypeLocNodes.def" 6465 6466 void VisitFunctionTypeLoc(FunctionTypeLoc); 6467 void VisitArrayTypeLoc(ArrayTypeLoc); 6468 }; 6469 6470 } // namespace clang 6471 6472 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6473 // nothing to do 6474 } 6475 6476 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6477 TL.setBuiltinLoc(readSourceLocation()); 6478 if (TL.needsExtraLocalData()) { 6479 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6480 TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt())); 6481 TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt())); 6482 TL.setModeAttr(Reader.readInt()); 6483 } 6484 } 6485 6486 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6487 TL.setNameLoc(readSourceLocation()); 6488 } 6489 6490 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6491 TL.setStarLoc(readSourceLocation()); 6492 } 6493 6494 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6495 // nothing to do 6496 } 6497 6498 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6499 // nothing to do 6500 } 6501 6502 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6503 TL.setExpansionLoc(readSourceLocation()); 6504 } 6505 6506 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6507 TL.setCaretLoc(readSourceLocation()); 6508 } 6509 6510 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6511 TL.setAmpLoc(readSourceLocation()); 6512 } 6513 6514 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6515 TL.setAmpAmpLoc(readSourceLocation()); 6516 } 6517 6518 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6519 TL.setStarLoc(readSourceLocation()); 6520 TL.setClassTInfo(GetTypeSourceInfo()); 6521 } 6522 6523 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6524 TL.setLBracketLoc(readSourceLocation()); 6525 TL.setRBracketLoc(readSourceLocation()); 6526 if (Reader.readBool()) 6527 TL.setSizeExpr(Reader.readExpr()); 6528 else 6529 TL.setSizeExpr(nullptr); 6530 } 6531 6532 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6533 VisitArrayTypeLoc(TL); 6534 } 6535 6536 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6537 VisitArrayTypeLoc(TL); 6538 } 6539 6540 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6541 VisitArrayTypeLoc(TL); 6542 } 6543 6544 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6545 DependentSizedArrayTypeLoc TL) { 6546 VisitArrayTypeLoc(TL); 6547 } 6548 6549 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6550 DependentAddressSpaceTypeLoc TL) { 6551 6552 TL.setAttrNameLoc(readSourceLocation()); 6553 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6554 TL.setAttrExprOperand(Reader.readExpr()); 6555 } 6556 6557 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6558 DependentSizedExtVectorTypeLoc TL) { 6559 TL.setNameLoc(readSourceLocation()); 6560 } 6561 6562 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6563 TL.setNameLoc(readSourceLocation()); 6564 } 6565 6566 void TypeLocReader::VisitDependentVectorTypeLoc( 6567 DependentVectorTypeLoc TL) { 6568 TL.setNameLoc(readSourceLocation()); 6569 } 6570 6571 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6572 TL.setNameLoc(readSourceLocation()); 6573 } 6574 6575 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) { 6576 TL.setAttrNameLoc(readSourceLocation()); 6577 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6578 TL.setAttrRowOperand(Reader.readExpr()); 6579 TL.setAttrColumnOperand(Reader.readExpr()); 6580 } 6581 6582 void TypeLocReader::VisitDependentSizedMatrixTypeLoc( 6583 DependentSizedMatrixTypeLoc TL) { 6584 TL.setAttrNameLoc(readSourceLocation()); 6585 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6586 TL.setAttrRowOperand(Reader.readExpr()); 6587 TL.setAttrColumnOperand(Reader.readExpr()); 6588 } 6589 6590 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6591 TL.setLocalRangeBegin(readSourceLocation()); 6592 TL.setLParenLoc(readSourceLocation()); 6593 TL.setRParenLoc(readSourceLocation()); 6594 TL.setExceptionSpecRange(Reader.readSourceRange()); 6595 TL.setLocalRangeEnd(readSourceLocation()); 6596 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6597 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6598 } 6599 } 6600 6601 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6602 VisitFunctionTypeLoc(TL); 6603 } 6604 6605 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6606 VisitFunctionTypeLoc(TL); 6607 } 6608 6609 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6610 TL.setNameLoc(readSourceLocation()); 6611 } 6612 6613 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6614 TL.setNameLoc(readSourceLocation()); 6615 } 6616 6617 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6618 TL.setTypeofLoc(readSourceLocation()); 6619 TL.setLParenLoc(readSourceLocation()); 6620 TL.setRParenLoc(readSourceLocation()); 6621 } 6622 6623 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6624 TL.setTypeofLoc(readSourceLocation()); 6625 TL.setLParenLoc(readSourceLocation()); 6626 TL.setRParenLoc(readSourceLocation()); 6627 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6628 } 6629 6630 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6631 TL.setNameLoc(readSourceLocation()); 6632 } 6633 6634 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6635 TL.setKWLoc(readSourceLocation()); 6636 TL.setLParenLoc(readSourceLocation()); 6637 TL.setRParenLoc(readSourceLocation()); 6638 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6639 } 6640 6641 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6642 TL.setNameLoc(readSourceLocation()); 6643 if (Reader.readBool()) { 6644 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6645 TL.setTemplateKWLoc(readSourceLocation()); 6646 TL.setConceptNameLoc(readSourceLocation()); 6647 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6648 TL.setLAngleLoc(readSourceLocation()); 6649 TL.setRAngleLoc(readSourceLocation()); 6650 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6651 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6652 TL.getTypePtr()->getArg(i).getKind())); 6653 } 6654 } 6655 6656 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6657 DeducedTemplateSpecializationTypeLoc TL) { 6658 TL.setTemplateNameLoc(readSourceLocation()); 6659 } 6660 6661 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6662 TL.setNameLoc(readSourceLocation()); 6663 } 6664 6665 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6666 TL.setNameLoc(readSourceLocation()); 6667 } 6668 6669 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6670 TL.setAttr(ReadAttr()); 6671 } 6672 6673 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6674 TL.setNameLoc(readSourceLocation()); 6675 } 6676 6677 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6678 SubstTemplateTypeParmTypeLoc TL) { 6679 TL.setNameLoc(readSourceLocation()); 6680 } 6681 6682 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6683 SubstTemplateTypeParmPackTypeLoc TL) { 6684 TL.setNameLoc(readSourceLocation()); 6685 } 6686 6687 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6688 TemplateSpecializationTypeLoc TL) { 6689 TL.setTemplateKeywordLoc(readSourceLocation()); 6690 TL.setTemplateNameLoc(readSourceLocation()); 6691 TL.setLAngleLoc(readSourceLocation()); 6692 TL.setRAngleLoc(readSourceLocation()); 6693 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6694 TL.setArgLocInfo( 6695 i, 6696 Reader.readTemplateArgumentLocInfo( 6697 TL.getTypePtr()->getArg(i).getKind())); 6698 } 6699 6700 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6701 TL.setLParenLoc(readSourceLocation()); 6702 TL.setRParenLoc(readSourceLocation()); 6703 } 6704 6705 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6706 TL.setElaboratedKeywordLoc(readSourceLocation()); 6707 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6708 } 6709 6710 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6711 TL.setNameLoc(readSourceLocation()); 6712 } 6713 6714 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6715 TL.setElaboratedKeywordLoc(readSourceLocation()); 6716 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6717 TL.setNameLoc(readSourceLocation()); 6718 } 6719 6720 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6721 DependentTemplateSpecializationTypeLoc TL) { 6722 TL.setElaboratedKeywordLoc(readSourceLocation()); 6723 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6724 TL.setTemplateKeywordLoc(readSourceLocation()); 6725 TL.setTemplateNameLoc(readSourceLocation()); 6726 TL.setLAngleLoc(readSourceLocation()); 6727 TL.setRAngleLoc(readSourceLocation()); 6728 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6729 TL.setArgLocInfo( 6730 I, 6731 Reader.readTemplateArgumentLocInfo( 6732 TL.getTypePtr()->getArg(I).getKind())); 6733 } 6734 6735 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6736 TL.setEllipsisLoc(readSourceLocation()); 6737 } 6738 6739 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6740 TL.setNameLoc(readSourceLocation()); 6741 } 6742 6743 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6744 if (TL.getNumProtocols()) { 6745 TL.setProtocolLAngleLoc(readSourceLocation()); 6746 TL.setProtocolRAngleLoc(readSourceLocation()); 6747 } 6748 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6749 TL.setProtocolLoc(i, readSourceLocation()); 6750 } 6751 6752 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6753 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6754 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6755 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6756 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6757 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6758 TL.setProtocolLAngleLoc(readSourceLocation()); 6759 TL.setProtocolRAngleLoc(readSourceLocation()); 6760 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6761 TL.setProtocolLoc(i, readSourceLocation()); 6762 } 6763 6764 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6765 TL.setStarLoc(readSourceLocation()); 6766 } 6767 6768 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6769 TL.setKWLoc(readSourceLocation()); 6770 TL.setLParenLoc(readSourceLocation()); 6771 TL.setRParenLoc(readSourceLocation()); 6772 } 6773 6774 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6775 TL.setKWLoc(readSourceLocation()); 6776 } 6777 6778 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) { 6779 TL.setNameLoc(readSourceLocation()); 6780 } 6781 void TypeLocReader::VisitDependentExtIntTypeLoc( 6782 clang::DependentExtIntTypeLoc TL) { 6783 TL.setNameLoc(readSourceLocation()); 6784 } 6785 6786 6787 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6788 TypeLocReader TLR(*this); 6789 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6790 TLR.Visit(TL); 6791 } 6792 6793 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6794 QualType InfoTy = readType(); 6795 if (InfoTy.isNull()) 6796 return nullptr; 6797 6798 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6799 readTypeLoc(TInfo->getTypeLoc()); 6800 return TInfo; 6801 } 6802 6803 QualType ASTReader::GetType(TypeID ID) { 6804 assert(ContextObj && "reading type with no AST context"); 6805 ASTContext &Context = *ContextObj; 6806 6807 unsigned FastQuals = ID & Qualifiers::FastMask; 6808 unsigned Index = ID >> Qualifiers::FastWidth; 6809 6810 if (Index < NUM_PREDEF_TYPE_IDS) { 6811 QualType T; 6812 switch ((PredefinedTypeIDs)Index) { 6813 case PREDEF_TYPE_NULL_ID: 6814 return QualType(); 6815 case PREDEF_TYPE_VOID_ID: 6816 T = Context.VoidTy; 6817 break; 6818 case PREDEF_TYPE_BOOL_ID: 6819 T = Context.BoolTy; 6820 break; 6821 case PREDEF_TYPE_CHAR_U_ID: 6822 case PREDEF_TYPE_CHAR_S_ID: 6823 // FIXME: Check that the signedness of CharTy is correct! 6824 T = Context.CharTy; 6825 break; 6826 case PREDEF_TYPE_UCHAR_ID: 6827 T = Context.UnsignedCharTy; 6828 break; 6829 case PREDEF_TYPE_USHORT_ID: 6830 T = Context.UnsignedShortTy; 6831 break; 6832 case PREDEF_TYPE_UINT_ID: 6833 T = Context.UnsignedIntTy; 6834 break; 6835 case PREDEF_TYPE_ULONG_ID: 6836 T = Context.UnsignedLongTy; 6837 break; 6838 case PREDEF_TYPE_ULONGLONG_ID: 6839 T = Context.UnsignedLongLongTy; 6840 break; 6841 case PREDEF_TYPE_UINT128_ID: 6842 T = Context.UnsignedInt128Ty; 6843 break; 6844 case PREDEF_TYPE_SCHAR_ID: 6845 T = Context.SignedCharTy; 6846 break; 6847 case PREDEF_TYPE_WCHAR_ID: 6848 T = Context.WCharTy; 6849 break; 6850 case PREDEF_TYPE_SHORT_ID: 6851 T = Context.ShortTy; 6852 break; 6853 case PREDEF_TYPE_INT_ID: 6854 T = Context.IntTy; 6855 break; 6856 case PREDEF_TYPE_LONG_ID: 6857 T = Context.LongTy; 6858 break; 6859 case PREDEF_TYPE_LONGLONG_ID: 6860 T = Context.LongLongTy; 6861 break; 6862 case PREDEF_TYPE_INT128_ID: 6863 T = Context.Int128Ty; 6864 break; 6865 case PREDEF_TYPE_BFLOAT16_ID: 6866 T = Context.BFloat16Ty; 6867 break; 6868 case PREDEF_TYPE_HALF_ID: 6869 T = Context.HalfTy; 6870 break; 6871 case PREDEF_TYPE_FLOAT_ID: 6872 T = Context.FloatTy; 6873 break; 6874 case PREDEF_TYPE_DOUBLE_ID: 6875 T = Context.DoubleTy; 6876 break; 6877 case PREDEF_TYPE_LONGDOUBLE_ID: 6878 T = Context.LongDoubleTy; 6879 break; 6880 case PREDEF_TYPE_SHORT_ACCUM_ID: 6881 T = Context.ShortAccumTy; 6882 break; 6883 case PREDEF_TYPE_ACCUM_ID: 6884 T = Context.AccumTy; 6885 break; 6886 case PREDEF_TYPE_LONG_ACCUM_ID: 6887 T = Context.LongAccumTy; 6888 break; 6889 case PREDEF_TYPE_USHORT_ACCUM_ID: 6890 T = Context.UnsignedShortAccumTy; 6891 break; 6892 case PREDEF_TYPE_UACCUM_ID: 6893 T = Context.UnsignedAccumTy; 6894 break; 6895 case PREDEF_TYPE_ULONG_ACCUM_ID: 6896 T = Context.UnsignedLongAccumTy; 6897 break; 6898 case PREDEF_TYPE_SHORT_FRACT_ID: 6899 T = Context.ShortFractTy; 6900 break; 6901 case PREDEF_TYPE_FRACT_ID: 6902 T = Context.FractTy; 6903 break; 6904 case PREDEF_TYPE_LONG_FRACT_ID: 6905 T = Context.LongFractTy; 6906 break; 6907 case PREDEF_TYPE_USHORT_FRACT_ID: 6908 T = Context.UnsignedShortFractTy; 6909 break; 6910 case PREDEF_TYPE_UFRACT_ID: 6911 T = Context.UnsignedFractTy; 6912 break; 6913 case PREDEF_TYPE_ULONG_FRACT_ID: 6914 T = Context.UnsignedLongFractTy; 6915 break; 6916 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6917 T = Context.SatShortAccumTy; 6918 break; 6919 case PREDEF_TYPE_SAT_ACCUM_ID: 6920 T = Context.SatAccumTy; 6921 break; 6922 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6923 T = Context.SatLongAccumTy; 6924 break; 6925 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6926 T = Context.SatUnsignedShortAccumTy; 6927 break; 6928 case PREDEF_TYPE_SAT_UACCUM_ID: 6929 T = Context.SatUnsignedAccumTy; 6930 break; 6931 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6932 T = Context.SatUnsignedLongAccumTy; 6933 break; 6934 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6935 T = Context.SatShortFractTy; 6936 break; 6937 case PREDEF_TYPE_SAT_FRACT_ID: 6938 T = Context.SatFractTy; 6939 break; 6940 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6941 T = Context.SatLongFractTy; 6942 break; 6943 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6944 T = Context.SatUnsignedShortFractTy; 6945 break; 6946 case PREDEF_TYPE_SAT_UFRACT_ID: 6947 T = Context.SatUnsignedFractTy; 6948 break; 6949 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6950 T = Context.SatUnsignedLongFractTy; 6951 break; 6952 case PREDEF_TYPE_FLOAT16_ID: 6953 T = Context.Float16Ty; 6954 break; 6955 case PREDEF_TYPE_FLOAT128_ID: 6956 T = Context.Float128Ty; 6957 break; 6958 case PREDEF_TYPE_IBM128_ID: 6959 T = Context.Ibm128Ty; 6960 break; 6961 case PREDEF_TYPE_OVERLOAD_ID: 6962 T = Context.OverloadTy; 6963 break; 6964 case PREDEF_TYPE_BOUND_MEMBER: 6965 T = Context.BoundMemberTy; 6966 break; 6967 case PREDEF_TYPE_PSEUDO_OBJECT: 6968 T = Context.PseudoObjectTy; 6969 break; 6970 case PREDEF_TYPE_DEPENDENT_ID: 6971 T = Context.DependentTy; 6972 break; 6973 case PREDEF_TYPE_UNKNOWN_ANY: 6974 T = Context.UnknownAnyTy; 6975 break; 6976 case PREDEF_TYPE_NULLPTR_ID: 6977 T = Context.NullPtrTy; 6978 break; 6979 case PREDEF_TYPE_CHAR8_ID: 6980 T = Context.Char8Ty; 6981 break; 6982 case PREDEF_TYPE_CHAR16_ID: 6983 T = Context.Char16Ty; 6984 break; 6985 case PREDEF_TYPE_CHAR32_ID: 6986 T = Context.Char32Ty; 6987 break; 6988 case PREDEF_TYPE_OBJC_ID: 6989 T = Context.ObjCBuiltinIdTy; 6990 break; 6991 case PREDEF_TYPE_OBJC_CLASS: 6992 T = Context.ObjCBuiltinClassTy; 6993 break; 6994 case PREDEF_TYPE_OBJC_SEL: 6995 T = Context.ObjCBuiltinSelTy; 6996 break; 6997 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6998 case PREDEF_TYPE_##Id##_ID: \ 6999 T = Context.SingletonId; \ 7000 break; 7001 #include "clang/Basic/OpenCLImageTypes.def" 7002 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 7003 case PREDEF_TYPE_##Id##_ID: \ 7004 T = Context.Id##Ty; \ 7005 break; 7006 #include "clang/Basic/OpenCLExtensionTypes.def" 7007 case PREDEF_TYPE_SAMPLER_ID: 7008 T = Context.OCLSamplerTy; 7009 break; 7010 case PREDEF_TYPE_EVENT_ID: 7011 T = Context.OCLEventTy; 7012 break; 7013 case PREDEF_TYPE_CLK_EVENT_ID: 7014 T = Context.OCLClkEventTy; 7015 break; 7016 case PREDEF_TYPE_QUEUE_ID: 7017 T = Context.OCLQueueTy; 7018 break; 7019 case PREDEF_TYPE_RESERVE_ID_ID: 7020 T = Context.OCLReserveIDTy; 7021 break; 7022 case PREDEF_TYPE_AUTO_DEDUCT: 7023 T = Context.getAutoDeductType(); 7024 break; 7025 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 7026 T = Context.getAutoRRefDeductType(); 7027 break; 7028 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 7029 T = Context.ARCUnbridgedCastTy; 7030 break; 7031 case PREDEF_TYPE_BUILTIN_FN: 7032 T = Context.BuiltinFnTy; 7033 break; 7034 case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX: 7035 T = Context.IncompleteMatrixIdxTy; 7036 break; 7037 case PREDEF_TYPE_OMP_ARRAY_SECTION: 7038 T = Context.OMPArraySectionTy; 7039 break; 7040 case PREDEF_TYPE_OMP_ARRAY_SHAPING: 7041 T = Context.OMPArraySectionTy; 7042 break; 7043 case PREDEF_TYPE_OMP_ITERATOR: 7044 T = Context.OMPIteratorTy; 7045 break; 7046 #define SVE_TYPE(Name, Id, SingletonId) \ 7047 case PREDEF_TYPE_##Id##_ID: \ 7048 T = Context.SingletonId; \ 7049 break; 7050 #include "clang/Basic/AArch64SVEACLETypes.def" 7051 #define PPC_VECTOR_TYPE(Name, Id, Size) \ 7052 case PREDEF_TYPE_##Id##_ID: \ 7053 T = Context.Id##Ty; \ 7054 break; 7055 #include "clang/Basic/PPCTypes.def" 7056 #define RVV_TYPE(Name, Id, SingletonId) \ 7057 case PREDEF_TYPE_##Id##_ID: \ 7058 T = Context.SingletonId; \ 7059 break; 7060 #include "clang/Basic/RISCVVTypes.def" 7061 } 7062 7063 assert(!T.isNull() && "Unknown predefined type"); 7064 return T.withFastQualifiers(FastQuals); 7065 } 7066 7067 Index -= NUM_PREDEF_TYPE_IDS; 7068 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 7069 if (TypesLoaded[Index].isNull()) { 7070 TypesLoaded[Index] = readTypeRecord(Index); 7071 if (TypesLoaded[Index].isNull()) 7072 return QualType(); 7073 7074 TypesLoaded[Index]->setFromAST(); 7075 if (DeserializationListener) 7076 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 7077 TypesLoaded[Index]); 7078 } 7079 7080 return TypesLoaded[Index].withFastQualifiers(FastQuals); 7081 } 7082 7083 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 7084 return GetType(getGlobalTypeID(F, LocalID)); 7085 } 7086 7087 serialization::TypeID 7088 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 7089 unsigned FastQuals = LocalID & Qualifiers::FastMask; 7090 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 7091 7092 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 7093 return LocalID; 7094 7095 if (!F.ModuleOffsetMap.empty()) 7096 ReadModuleOffsetMap(F); 7097 7098 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7099 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7100 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7101 7102 unsigned GlobalIndex = LocalIndex + I->second; 7103 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7104 } 7105 7106 TemplateArgumentLocInfo 7107 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7108 switch (Kind) { 7109 case TemplateArgument::Expression: 7110 return readExpr(); 7111 case TemplateArgument::Type: 7112 return readTypeSourceInfo(); 7113 case TemplateArgument::Template: { 7114 NestedNameSpecifierLoc QualifierLoc = 7115 readNestedNameSpecifierLoc(); 7116 SourceLocation TemplateNameLoc = readSourceLocation(); 7117 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7118 TemplateNameLoc, SourceLocation()); 7119 } 7120 case TemplateArgument::TemplateExpansion: { 7121 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7122 SourceLocation TemplateNameLoc = readSourceLocation(); 7123 SourceLocation EllipsisLoc = readSourceLocation(); 7124 return TemplateArgumentLocInfo(getASTContext(), QualifierLoc, 7125 TemplateNameLoc, EllipsisLoc); 7126 } 7127 case TemplateArgument::Null: 7128 case TemplateArgument::Integral: 7129 case TemplateArgument::Declaration: 7130 case TemplateArgument::NullPtr: 7131 case TemplateArgument::Pack: 7132 // FIXME: Is this right? 7133 return TemplateArgumentLocInfo(); 7134 } 7135 llvm_unreachable("unexpected template argument loc"); 7136 } 7137 7138 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7139 TemplateArgument Arg = readTemplateArgument(); 7140 7141 if (Arg.getKind() == TemplateArgument::Expression) { 7142 if (readBool()) // bool InfoHasSameExpr. 7143 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7144 } 7145 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7146 } 7147 7148 const ASTTemplateArgumentListInfo * 7149 ASTRecordReader::readASTTemplateArgumentListInfo() { 7150 SourceLocation LAngleLoc = readSourceLocation(); 7151 SourceLocation RAngleLoc = readSourceLocation(); 7152 unsigned NumArgsAsWritten = readInt(); 7153 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7154 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7155 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7156 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7157 } 7158 7159 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7160 return GetDecl(ID); 7161 } 7162 7163 void ASTReader::CompleteRedeclChain(const Decl *D) { 7164 if (NumCurrentElementsDeserializing) { 7165 // We arrange to not care about the complete redeclaration chain while we're 7166 // deserializing. Just remember that the AST has marked this one as complete 7167 // but that it's not actually complete yet, so we know we still need to 7168 // complete it later. 7169 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7170 return; 7171 } 7172 7173 if (!D->getDeclContext()) { 7174 assert(isa<TranslationUnitDecl>(D) && "Not a TU?"); 7175 return; 7176 } 7177 7178 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7179 7180 // If this is a named declaration, complete it by looking it up 7181 // within its context. 7182 // 7183 // FIXME: Merging a function definition should merge 7184 // all mergeable entities within it. 7185 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7186 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7187 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7188 if (!getContext().getLangOpts().CPlusPlus && 7189 isa<TranslationUnitDecl>(DC)) { 7190 // Outside of C++, we don't have a lookup table for the TU, so update 7191 // the identifier instead. (For C++ modules, we don't store decls 7192 // in the serialized identifier table, so we do the lookup in the TU.) 7193 auto *II = Name.getAsIdentifierInfo(); 7194 assert(II && "non-identifier name in C?"); 7195 if (II->isOutOfDate()) 7196 updateOutOfDateIdentifier(*II); 7197 } else 7198 DC->lookup(Name); 7199 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7200 // Find all declarations of this kind from the relevant context. 7201 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7202 auto *DC = cast<DeclContext>(DCDecl); 7203 SmallVector<Decl*, 8> Decls; 7204 FindExternalLexicalDecls( 7205 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7206 } 7207 } 7208 } 7209 7210 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7211 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7212 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7213 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7214 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7215 if (auto *Template = FD->getPrimaryTemplate()) 7216 Template->LoadLazySpecializations(); 7217 } 7218 } 7219 7220 CXXCtorInitializer ** 7221 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7222 RecordLocation Loc = getLocalBitOffset(Offset); 7223 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7224 SavedStreamPosition SavedPosition(Cursor); 7225 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7226 Error(std::move(Err)); 7227 return nullptr; 7228 } 7229 ReadingKindTracker ReadingKind(Read_Decl, *this); 7230 7231 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7232 if (!MaybeCode) { 7233 Error(MaybeCode.takeError()); 7234 return nullptr; 7235 } 7236 unsigned Code = MaybeCode.get(); 7237 7238 ASTRecordReader Record(*this, *Loc.F); 7239 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7240 if (!MaybeRecCode) { 7241 Error(MaybeRecCode.takeError()); 7242 return nullptr; 7243 } 7244 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7245 Error("malformed AST file: missing C++ ctor initializers"); 7246 return nullptr; 7247 } 7248 7249 return Record.readCXXCtorInitializers(); 7250 } 7251 7252 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7253 assert(ContextObj && "reading base specifiers with no AST context"); 7254 ASTContext &Context = *ContextObj; 7255 7256 RecordLocation Loc = getLocalBitOffset(Offset); 7257 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7258 SavedStreamPosition SavedPosition(Cursor); 7259 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7260 Error(std::move(Err)); 7261 return nullptr; 7262 } 7263 ReadingKindTracker ReadingKind(Read_Decl, *this); 7264 7265 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7266 if (!MaybeCode) { 7267 Error(MaybeCode.takeError()); 7268 return nullptr; 7269 } 7270 unsigned Code = MaybeCode.get(); 7271 7272 ASTRecordReader Record(*this, *Loc.F); 7273 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7274 if (!MaybeRecCode) { 7275 Error(MaybeCode.takeError()); 7276 return nullptr; 7277 } 7278 unsigned RecCode = MaybeRecCode.get(); 7279 7280 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7281 Error("malformed AST file: missing C++ base specifiers"); 7282 return nullptr; 7283 } 7284 7285 unsigned NumBases = Record.readInt(); 7286 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7287 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7288 for (unsigned I = 0; I != NumBases; ++I) 7289 Bases[I] = Record.readCXXBaseSpecifier(); 7290 return Bases; 7291 } 7292 7293 serialization::DeclID 7294 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7295 if (LocalID < NUM_PREDEF_DECL_IDS) 7296 return LocalID; 7297 7298 if (!F.ModuleOffsetMap.empty()) 7299 ReadModuleOffsetMap(F); 7300 7301 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7302 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7303 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7304 7305 return LocalID + I->second; 7306 } 7307 7308 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7309 ModuleFile &M) const { 7310 // Predefined decls aren't from any module. 7311 if (ID < NUM_PREDEF_DECL_IDS) 7312 return false; 7313 7314 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7315 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7316 } 7317 7318 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7319 if (!D->isFromASTFile()) 7320 return nullptr; 7321 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7322 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7323 return I->second; 7324 } 7325 7326 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7327 if (ID < NUM_PREDEF_DECL_IDS) 7328 return SourceLocation(); 7329 7330 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7331 7332 if (Index > DeclsLoaded.size()) { 7333 Error("declaration ID out-of-range for AST file"); 7334 return SourceLocation(); 7335 } 7336 7337 if (Decl *D = DeclsLoaded[Index]) 7338 return D->getLocation(); 7339 7340 SourceLocation Loc; 7341 DeclCursorForID(ID, Loc); 7342 return Loc; 7343 } 7344 7345 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7346 switch (ID) { 7347 case PREDEF_DECL_NULL_ID: 7348 return nullptr; 7349 7350 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7351 return Context.getTranslationUnitDecl(); 7352 7353 case PREDEF_DECL_OBJC_ID_ID: 7354 return Context.getObjCIdDecl(); 7355 7356 case PREDEF_DECL_OBJC_SEL_ID: 7357 return Context.getObjCSelDecl(); 7358 7359 case PREDEF_DECL_OBJC_CLASS_ID: 7360 return Context.getObjCClassDecl(); 7361 7362 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7363 return Context.getObjCProtocolDecl(); 7364 7365 case PREDEF_DECL_INT_128_ID: 7366 return Context.getInt128Decl(); 7367 7368 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7369 return Context.getUInt128Decl(); 7370 7371 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7372 return Context.getObjCInstanceTypeDecl(); 7373 7374 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7375 return Context.getBuiltinVaListDecl(); 7376 7377 case PREDEF_DECL_VA_LIST_TAG: 7378 return Context.getVaListTagDecl(); 7379 7380 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7381 return Context.getBuiltinMSVaListDecl(); 7382 7383 case PREDEF_DECL_BUILTIN_MS_GUID_ID: 7384 return Context.getMSGuidTagDecl(); 7385 7386 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7387 return Context.getExternCContextDecl(); 7388 7389 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7390 return Context.getMakeIntegerSeqDecl(); 7391 7392 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7393 return Context.getCFConstantStringDecl(); 7394 7395 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7396 return Context.getCFConstantStringTagDecl(); 7397 7398 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7399 return Context.getTypePackElementDecl(); 7400 } 7401 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7402 } 7403 7404 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7405 assert(ContextObj && "reading decl with no AST context"); 7406 if (ID < NUM_PREDEF_DECL_IDS) { 7407 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7408 if (D) { 7409 // Track that we have merged the declaration with ID \p ID into the 7410 // pre-existing predefined declaration \p D. 7411 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7412 if (Merged.empty()) 7413 Merged.push_back(ID); 7414 } 7415 return D; 7416 } 7417 7418 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7419 7420 if (Index >= DeclsLoaded.size()) { 7421 assert(0 && "declaration ID out-of-range for AST file"); 7422 Error("declaration ID out-of-range for AST file"); 7423 return nullptr; 7424 } 7425 7426 return DeclsLoaded[Index]; 7427 } 7428 7429 Decl *ASTReader::GetDecl(DeclID ID) { 7430 if (ID < NUM_PREDEF_DECL_IDS) 7431 return GetExistingDecl(ID); 7432 7433 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7434 7435 if (Index >= DeclsLoaded.size()) { 7436 assert(0 && "declaration ID out-of-range for AST file"); 7437 Error("declaration ID out-of-range for AST file"); 7438 return nullptr; 7439 } 7440 7441 if (!DeclsLoaded[Index]) { 7442 ReadDeclRecord(ID); 7443 if (DeserializationListener) 7444 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7445 } 7446 7447 return DeclsLoaded[Index]; 7448 } 7449 7450 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7451 DeclID GlobalID) { 7452 if (GlobalID < NUM_PREDEF_DECL_IDS) 7453 return GlobalID; 7454 7455 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7456 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7457 ModuleFile *Owner = I->second; 7458 7459 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7460 = M.GlobalToLocalDeclIDs.find(Owner); 7461 if (Pos == M.GlobalToLocalDeclIDs.end()) 7462 return 0; 7463 7464 return GlobalID - Owner->BaseDeclID + Pos->second; 7465 } 7466 7467 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7468 const RecordData &Record, 7469 unsigned &Idx) { 7470 if (Idx >= Record.size()) { 7471 Error("Corrupted AST file"); 7472 return 0; 7473 } 7474 7475 return getGlobalDeclID(F, Record[Idx++]); 7476 } 7477 7478 /// Resolve the offset of a statement into a statement. 7479 /// 7480 /// This operation will read a new statement from the external 7481 /// source each time it is called, and is meant to be used via a 7482 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7483 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7484 // Switch case IDs are per Decl. 7485 ClearSwitchCaseIDs(); 7486 7487 // Offset here is a global offset across the entire chain. 7488 RecordLocation Loc = getLocalBitOffset(Offset); 7489 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7490 Error(std::move(Err)); 7491 return nullptr; 7492 } 7493 assert(NumCurrentElementsDeserializing == 0 && 7494 "should not be called while already deserializing"); 7495 Deserializing D(this); 7496 return ReadStmtFromStream(*Loc.F); 7497 } 7498 7499 void ASTReader::FindExternalLexicalDecls( 7500 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7501 SmallVectorImpl<Decl *> &Decls) { 7502 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7503 7504 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7505 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7506 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7507 auto K = (Decl::Kind)+LexicalDecls[I]; 7508 if (!IsKindWeWant(K)) 7509 continue; 7510 7511 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7512 7513 // Don't add predefined declarations to the lexical context more 7514 // than once. 7515 if (ID < NUM_PREDEF_DECL_IDS) { 7516 if (PredefsVisited[ID]) 7517 continue; 7518 7519 PredefsVisited[ID] = true; 7520 } 7521 7522 if (Decl *D = GetLocalDecl(*M, ID)) { 7523 assert(D->getKind() == K && "wrong kind for lexical decl"); 7524 if (!DC->isDeclInLexicalTraversal(D)) 7525 Decls.push_back(D); 7526 } 7527 } 7528 }; 7529 7530 if (isa<TranslationUnitDecl>(DC)) { 7531 for (auto Lexical : TULexicalDecls) 7532 Visit(Lexical.first, Lexical.second); 7533 } else { 7534 auto I = LexicalDecls.find(DC); 7535 if (I != LexicalDecls.end()) 7536 Visit(I->second.first, I->second.second); 7537 } 7538 7539 ++NumLexicalDeclContextsRead; 7540 } 7541 7542 namespace { 7543 7544 class DeclIDComp { 7545 ASTReader &Reader; 7546 ModuleFile &Mod; 7547 7548 public: 7549 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7550 7551 bool operator()(LocalDeclID L, LocalDeclID R) const { 7552 SourceLocation LHS = getLocation(L); 7553 SourceLocation RHS = getLocation(R); 7554 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7555 } 7556 7557 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7558 SourceLocation RHS = getLocation(R); 7559 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7560 } 7561 7562 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7563 SourceLocation LHS = getLocation(L); 7564 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7565 } 7566 7567 SourceLocation getLocation(LocalDeclID ID) const { 7568 return Reader.getSourceManager().getFileLoc( 7569 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7570 } 7571 }; 7572 7573 } // namespace 7574 7575 void ASTReader::FindFileRegionDecls(FileID File, 7576 unsigned Offset, unsigned Length, 7577 SmallVectorImpl<Decl *> &Decls) { 7578 SourceManager &SM = getSourceManager(); 7579 7580 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7581 if (I == FileDeclIDs.end()) 7582 return; 7583 7584 FileDeclsInfo &DInfo = I->second; 7585 if (DInfo.Decls.empty()) 7586 return; 7587 7588 SourceLocation 7589 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7590 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7591 7592 DeclIDComp DIDComp(*this, *DInfo.Mod); 7593 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7594 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7595 if (BeginIt != DInfo.Decls.begin()) 7596 --BeginIt; 7597 7598 // If we are pointing at a top-level decl inside an objc container, we need 7599 // to backtrack until we find it otherwise we will fail to report that the 7600 // region overlaps with an objc container. 7601 while (BeginIt != DInfo.Decls.begin() && 7602 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7603 ->isTopLevelDeclInObjCContainer()) 7604 --BeginIt; 7605 7606 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7607 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7608 if (EndIt != DInfo.Decls.end()) 7609 ++EndIt; 7610 7611 for (ArrayRef<serialization::LocalDeclID>::iterator 7612 DIt = BeginIt; DIt != EndIt; ++DIt) 7613 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7614 } 7615 7616 bool 7617 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7618 DeclarationName Name) { 7619 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7620 "DeclContext has no visible decls in storage"); 7621 if (!Name) 7622 return false; 7623 7624 auto It = Lookups.find(DC); 7625 if (It == Lookups.end()) 7626 return false; 7627 7628 Deserializing LookupResults(this); 7629 7630 // Load the list of declarations. 7631 SmallVector<NamedDecl *, 64> Decls; 7632 llvm::SmallPtrSet<NamedDecl *, 8> Found; 7633 for (DeclID ID : It->second.Table.find(Name)) { 7634 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7635 if (ND->getDeclName() == Name && Found.insert(ND).second) 7636 Decls.push_back(ND); 7637 } 7638 7639 ++NumVisibleDeclContextsRead; 7640 SetExternalVisibleDeclsForName(DC, Name, Decls); 7641 return !Decls.empty(); 7642 } 7643 7644 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7645 if (!DC->hasExternalVisibleStorage()) 7646 return; 7647 7648 auto It = Lookups.find(DC); 7649 assert(It != Lookups.end() && 7650 "have external visible storage but no lookup tables"); 7651 7652 DeclsMap Decls; 7653 7654 for (DeclID ID : It->second.Table.findAll()) { 7655 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7656 Decls[ND->getDeclName()].push_back(ND); 7657 } 7658 7659 ++NumVisibleDeclContextsRead; 7660 7661 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7662 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7663 } 7664 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7665 } 7666 7667 const serialization::reader::DeclContextLookupTable * 7668 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7669 auto I = Lookups.find(Primary); 7670 return I == Lookups.end() ? nullptr : &I->second; 7671 } 7672 7673 /// Under non-PCH compilation the consumer receives the objc methods 7674 /// before receiving the implementation, and codegen depends on this. 7675 /// We simulate this by deserializing and passing to consumer the methods of the 7676 /// implementation before passing the deserialized implementation decl. 7677 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7678 ASTConsumer *Consumer) { 7679 assert(ImplD && Consumer); 7680 7681 for (auto *I : ImplD->methods()) 7682 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7683 7684 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7685 } 7686 7687 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7688 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7689 PassObjCImplDeclToConsumer(ImplD, Consumer); 7690 else 7691 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7692 } 7693 7694 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7695 this->Consumer = Consumer; 7696 7697 if (Consumer) 7698 PassInterestingDeclsToConsumer(); 7699 7700 if (DeserializationListener) 7701 DeserializationListener->ReaderInitialized(this); 7702 } 7703 7704 void ASTReader::PrintStats() { 7705 std::fprintf(stderr, "*** AST File Statistics:\n"); 7706 7707 unsigned NumTypesLoaded 7708 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7709 QualType()); 7710 unsigned NumDeclsLoaded 7711 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7712 (Decl *)nullptr); 7713 unsigned NumIdentifiersLoaded 7714 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7715 IdentifiersLoaded.end(), 7716 (IdentifierInfo *)nullptr); 7717 unsigned NumMacrosLoaded 7718 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7719 MacrosLoaded.end(), 7720 (MacroInfo *)nullptr); 7721 unsigned NumSelectorsLoaded 7722 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7723 SelectorsLoaded.end(), 7724 Selector()); 7725 7726 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7727 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7728 NumSLocEntriesRead, TotalNumSLocEntries, 7729 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7730 if (!TypesLoaded.empty()) 7731 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7732 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7733 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7734 if (!DeclsLoaded.empty()) 7735 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7736 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7737 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7738 if (!IdentifiersLoaded.empty()) 7739 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7740 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7741 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7742 if (!MacrosLoaded.empty()) 7743 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7744 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7745 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7746 if (!SelectorsLoaded.empty()) 7747 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7748 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7749 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7750 if (TotalNumStatements) 7751 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7752 NumStatementsRead, TotalNumStatements, 7753 ((float)NumStatementsRead/TotalNumStatements * 100)); 7754 if (TotalNumMacros) 7755 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7756 NumMacrosRead, TotalNumMacros, 7757 ((float)NumMacrosRead/TotalNumMacros * 100)); 7758 if (TotalLexicalDeclContexts) 7759 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7760 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7761 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7762 * 100)); 7763 if (TotalVisibleDeclContexts) 7764 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7765 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7766 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7767 * 100)); 7768 if (TotalNumMethodPoolEntries) 7769 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7770 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7771 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7772 * 100)); 7773 if (NumMethodPoolLookups) 7774 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7775 NumMethodPoolHits, NumMethodPoolLookups, 7776 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7777 if (NumMethodPoolTableLookups) 7778 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7779 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7780 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7781 * 100.0)); 7782 if (NumIdentifierLookupHits) 7783 std::fprintf(stderr, 7784 " %u / %u identifier table lookups succeeded (%f%%)\n", 7785 NumIdentifierLookupHits, NumIdentifierLookups, 7786 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7787 7788 if (GlobalIndex) { 7789 std::fprintf(stderr, "\n"); 7790 GlobalIndex->printStats(); 7791 } 7792 7793 std::fprintf(stderr, "\n"); 7794 dump(); 7795 std::fprintf(stderr, "\n"); 7796 } 7797 7798 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7799 LLVM_DUMP_METHOD static void 7800 dumpModuleIDMap(StringRef Name, 7801 const ContinuousRangeMap<Key, ModuleFile *, 7802 InitialCapacity> &Map) { 7803 if (Map.begin() == Map.end()) 7804 return; 7805 7806 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7807 7808 llvm::errs() << Name << ":\n"; 7809 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7810 I != IEnd; ++I) { 7811 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7812 << "\n"; 7813 } 7814 } 7815 7816 LLVM_DUMP_METHOD void ASTReader::dump() { 7817 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7818 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7819 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7820 dumpModuleIDMap("Global type map", GlobalTypeMap); 7821 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7822 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7823 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7824 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7825 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7826 dumpModuleIDMap("Global preprocessed entity map", 7827 GlobalPreprocessedEntityMap); 7828 7829 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7830 for (ModuleFile &M : ModuleMgr) 7831 M.dump(); 7832 } 7833 7834 /// Return the amount of memory used by memory buffers, breaking down 7835 /// by heap-backed versus mmap'ed memory. 7836 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7837 for (ModuleFile &I : ModuleMgr) { 7838 if (llvm::MemoryBuffer *buf = I.Buffer) { 7839 size_t bytes = buf->getBufferSize(); 7840 switch (buf->getBufferKind()) { 7841 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7842 sizes.malloc_bytes += bytes; 7843 break; 7844 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7845 sizes.mmap_bytes += bytes; 7846 break; 7847 } 7848 } 7849 } 7850 } 7851 7852 void ASTReader::InitializeSema(Sema &S) { 7853 SemaObj = &S; 7854 S.addExternalSource(this); 7855 7856 // Makes sure any declarations that were deserialized "too early" 7857 // still get added to the identifier's declaration chains. 7858 for (uint64_t ID : PreloadedDeclIDs) { 7859 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7860 pushExternalDeclIntoScope(D, D->getDeclName()); 7861 } 7862 PreloadedDeclIDs.clear(); 7863 7864 // FIXME: What happens if these are changed by a module import? 7865 if (!FPPragmaOptions.empty()) { 7866 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7867 FPOptionsOverride NewOverrides = 7868 FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]); 7869 SemaObj->CurFPFeatures = 7870 NewOverrides.applyOverrides(SemaObj->getLangOpts()); 7871 } 7872 7873 SemaObj->OpenCLFeatures = OpenCLExtensions; 7874 7875 UpdateSema(); 7876 } 7877 7878 void ASTReader::UpdateSema() { 7879 assert(SemaObj && "no Sema to update"); 7880 7881 // Load the offsets of the declarations that Sema references. 7882 // They will be lazily deserialized when needed. 7883 if (!SemaDeclRefs.empty()) { 7884 assert(SemaDeclRefs.size() % 3 == 0); 7885 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7886 if (!SemaObj->StdNamespace) 7887 SemaObj->StdNamespace = SemaDeclRefs[I]; 7888 if (!SemaObj->StdBadAlloc) 7889 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7890 if (!SemaObj->StdAlignValT) 7891 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7892 } 7893 SemaDeclRefs.clear(); 7894 } 7895 7896 // Update the state of pragmas. Use the same API as if we had encountered the 7897 // pragma in the source. 7898 if(OptimizeOffPragmaLocation.isValid()) 7899 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7900 if (PragmaMSStructState != -1) 7901 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7902 if (PointersToMembersPragmaLocation.isValid()) { 7903 SemaObj->ActOnPragmaMSPointersToMembers( 7904 (LangOptions::PragmaMSPointersToMembersKind) 7905 PragmaMSPointersToMembersState, 7906 PointersToMembersPragmaLocation); 7907 } 7908 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7909 7910 if (PragmaAlignPackCurrentValue) { 7911 // The bottom of the stack might have a default value. It must be adjusted 7912 // to the current value to ensure that the packing state is preserved after 7913 // popping entries that were included/imported from a PCH/module. 7914 bool DropFirst = false; 7915 if (!PragmaAlignPackStack.empty() && 7916 PragmaAlignPackStack.front().Location.isInvalid()) { 7917 assert(PragmaAlignPackStack.front().Value == 7918 SemaObj->AlignPackStack.DefaultValue && 7919 "Expected a default alignment value"); 7920 SemaObj->AlignPackStack.Stack.emplace_back( 7921 PragmaAlignPackStack.front().SlotLabel, 7922 SemaObj->AlignPackStack.CurrentValue, 7923 SemaObj->AlignPackStack.CurrentPragmaLocation, 7924 PragmaAlignPackStack.front().PushLocation); 7925 DropFirst = true; 7926 } 7927 for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack) 7928 .drop_front(DropFirst ? 1 : 0)) { 7929 SemaObj->AlignPackStack.Stack.emplace_back( 7930 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7931 } 7932 if (PragmaAlignPackCurrentLocation.isInvalid()) { 7933 assert(*PragmaAlignPackCurrentValue == 7934 SemaObj->AlignPackStack.DefaultValue && 7935 "Expected a default align and pack value"); 7936 // Keep the current values. 7937 } else { 7938 SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue; 7939 SemaObj->AlignPackStack.CurrentPragmaLocation = 7940 PragmaAlignPackCurrentLocation; 7941 } 7942 } 7943 if (FpPragmaCurrentValue) { 7944 // The bottom of the stack might have a default value. It must be adjusted 7945 // to the current value to ensure that fp-pragma state is preserved after 7946 // popping entries that were included/imported from a PCH/module. 7947 bool DropFirst = false; 7948 if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) { 7949 assert(FpPragmaStack.front().Value == 7950 SemaObj->FpPragmaStack.DefaultValue && 7951 "Expected a default pragma float_control value"); 7952 SemaObj->FpPragmaStack.Stack.emplace_back( 7953 FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue, 7954 SemaObj->FpPragmaStack.CurrentPragmaLocation, 7955 FpPragmaStack.front().PushLocation); 7956 DropFirst = true; 7957 } 7958 for (const auto &Entry : 7959 llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0)) 7960 SemaObj->FpPragmaStack.Stack.emplace_back( 7961 Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation); 7962 if (FpPragmaCurrentLocation.isInvalid()) { 7963 assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue && 7964 "Expected a default pragma float_control value"); 7965 // Keep the current values. 7966 } else { 7967 SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue; 7968 SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation; 7969 } 7970 } 7971 7972 // For non-modular AST files, restore visiblity of modules. 7973 for (auto &Import : ImportedModules) { 7974 if (Import.ImportLoc.isInvalid()) 7975 continue; 7976 if (Module *Imported = getSubmodule(Import.ID)) { 7977 SemaObj->makeModuleVisible(Imported, Import.ImportLoc); 7978 } 7979 } 7980 } 7981 7982 IdentifierInfo *ASTReader::get(StringRef Name) { 7983 // Note that we are loading an identifier. 7984 Deserializing AnIdentifier(this); 7985 7986 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7987 NumIdentifierLookups, 7988 NumIdentifierLookupHits); 7989 7990 // We don't need to do identifier table lookups in C++ modules (we preload 7991 // all interesting declarations, and don't need to use the scope for name 7992 // lookups). Perform the lookup in PCH files, though, since we don't build 7993 // a complete initial identifier table if we're carrying on from a PCH. 7994 if (PP.getLangOpts().CPlusPlus) { 7995 for (auto F : ModuleMgr.pch_modules()) 7996 if (Visitor(*F)) 7997 break; 7998 } else { 7999 // If there is a global index, look there first to determine which modules 8000 // provably do not have any results for this identifier. 8001 GlobalModuleIndex::HitSet Hits; 8002 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 8003 if (!loadGlobalIndex()) { 8004 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 8005 HitsPtr = &Hits; 8006 } 8007 } 8008 8009 ModuleMgr.visit(Visitor, HitsPtr); 8010 } 8011 8012 IdentifierInfo *II = Visitor.getIdentifierInfo(); 8013 markIdentifierUpToDate(II); 8014 return II; 8015 } 8016 8017 namespace clang { 8018 8019 /// An identifier-lookup iterator that enumerates all of the 8020 /// identifiers stored within a set of AST files. 8021 class ASTIdentifierIterator : public IdentifierIterator { 8022 /// The AST reader whose identifiers are being enumerated. 8023 const ASTReader &Reader; 8024 8025 /// The current index into the chain of AST files stored in 8026 /// the AST reader. 8027 unsigned Index; 8028 8029 /// The current position within the identifier lookup table 8030 /// of the current AST file. 8031 ASTIdentifierLookupTable::key_iterator Current; 8032 8033 /// The end position within the identifier lookup table of 8034 /// the current AST file. 8035 ASTIdentifierLookupTable::key_iterator End; 8036 8037 /// Whether to skip any modules in the ASTReader. 8038 bool SkipModules; 8039 8040 public: 8041 explicit ASTIdentifierIterator(const ASTReader &Reader, 8042 bool SkipModules = false); 8043 8044 StringRef Next() override; 8045 }; 8046 8047 } // namespace clang 8048 8049 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 8050 bool SkipModules) 8051 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 8052 } 8053 8054 StringRef ASTIdentifierIterator::Next() { 8055 while (Current == End) { 8056 // If we have exhausted all of our AST files, we're done. 8057 if (Index == 0) 8058 return StringRef(); 8059 8060 --Index; 8061 ModuleFile &F = Reader.ModuleMgr[Index]; 8062 if (SkipModules && F.isModule()) 8063 continue; 8064 8065 ASTIdentifierLookupTable *IdTable = 8066 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 8067 Current = IdTable->key_begin(); 8068 End = IdTable->key_end(); 8069 } 8070 8071 // We have any identifiers remaining in the current AST file; return 8072 // the next one. 8073 StringRef Result = *Current; 8074 ++Current; 8075 return Result; 8076 } 8077 8078 namespace { 8079 8080 /// A utility for appending two IdentifierIterators. 8081 class ChainedIdentifierIterator : public IdentifierIterator { 8082 std::unique_ptr<IdentifierIterator> Current; 8083 std::unique_ptr<IdentifierIterator> Queued; 8084 8085 public: 8086 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 8087 std::unique_ptr<IdentifierIterator> Second) 8088 : Current(std::move(First)), Queued(std::move(Second)) {} 8089 8090 StringRef Next() override { 8091 if (!Current) 8092 return StringRef(); 8093 8094 StringRef result = Current->Next(); 8095 if (!result.empty()) 8096 return result; 8097 8098 // Try the queued iterator, which may itself be empty. 8099 Current.reset(); 8100 std::swap(Current, Queued); 8101 return Next(); 8102 } 8103 }; 8104 8105 } // namespace 8106 8107 IdentifierIterator *ASTReader::getIdentifiers() { 8108 if (!loadGlobalIndex()) { 8109 std::unique_ptr<IdentifierIterator> ReaderIter( 8110 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 8111 std::unique_ptr<IdentifierIterator> ModulesIter( 8112 GlobalIndex->createIdentifierIterator()); 8113 return new ChainedIdentifierIterator(std::move(ReaderIter), 8114 std::move(ModulesIter)); 8115 } 8116 8117 return new ASTIdentifierIterator(*this); 8118 } 8119 8120 namespace clang { 8121 namespace serialization { 8122 8123 class ReadMethodPoolVisitor { 8124 ASTReader &Reader; 8125 Selector Sel; 8126 unsigned PriorGeneration; 8127 unsigned InstanceBits = 0; 8128 unsigned FactoryBits = 0; 8129 bool InstanceHasMoreThanOneDecl = false; 8130 bool FactoryHasMoreThanOneDecl = false; 8131 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 8132 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 8133 8134 public: 8135 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 8136 unsigned PriorGeneration) 8137 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 8138 8139 bool operator()(ModuleFile &M) { 8140 if (!M.SelectorLookupTable) 8141 return false; 8142 8143 // If we've already searched this module file, skip it now. 8144 if (M.Generation <= PriorGeneration) 8145 return true; 8146 8147 ++Reader.NumMethodPoolTableLookups; 8148 ASTSelectorLookupTable *PoolTable 8149 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8150 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8151 if (Pos == PoolTable->end()) 8152 return false; 8153 8154 ++Reader.NumMethodPoolTableHits; 8155 ++Reader.NumSelectorsRead; 8156 // FIXME: Not quite happy with the statistics here. We probably should 8157 // disable this tracking when called via LoadSelector. 8158 // Also, should entries without methods count as misses? 8159 ++Reader.NumMethodPoolEntriesRead; 8160 ASTSelectorLookupTrait::data_type Data = *Pos; 8161 if (Reader.DeserializationListener) 8162 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8163 8164 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8165 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8166 InstanceBits = Data.InstanceBits; 8167 FactoryBits = Data.FactoryBits; 8168 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8169 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8170 return true; 8171 } 8172 8173 /// Retrieve the instance methods found by this visitor. 8174 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8175 return InstanceMethods; 8176 } 8177 8178 /// Retrieve the instance methods found by this visitor. 8179 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8180 return FactoryMethods; 8181 } 8182 8183 unsigned getInstanceBits() const { return InstanceBits; } 8184 unsigned getFactoryBits() const { return FactoryBits; } 8185 8186 bool instanceHasMoreThanOneDecl() const { 8187 return InstanceHasMoreThanOneDecl; 8188 } 8189 8190 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8191 }; 8192 8193 } // namespace serialization 8194 } // namespace clang 8195 8196 /// Add the given set of methods to the method list. 8197 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8198 ObjCMethodList &List) { 8199 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8200 S.addMethodToGlobalList(&List, Methods[I]); 8201 } 8202 } 8203 8204 void ASTReader::ReadMethodPool(Selector Sel) { 8205 // Get the selector generation and update it to the current generation. 8206 unsigned &Generation = SelectorGeneration[Sel]; 8207 unsigned PriorGeneration = Generation; 8208 Generation = getGeneration(); 8209 SelectorOutOfDate[Sel] = false; 8210 8211 // Search for methods defined with this selector. 8212 ++NumMethodPoolLookups; 8213 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8214 ModuleMgr.visit(Visitor); 8215 8216 if (Visitor.getInstanceMethods().empty() && 8217 Visitor.getFactoryMethods().empty()) 8218 return; 8219 8220 ++NumMethodPoolHits; 8221 8222 if (!getSema()) 8223 return; 8224 8225 Sema &S = *getSema(); 8226 Sema::GlobalMethodPool::iterator Pos = 8227 S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists())) 8228 .first; 8229 8230 Pos->second.first.setBits(Visitor.getInstanceBits()); 8231 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8232 Pos->second.second.setBits(Visitor.getFactoryBits()); 8233 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8234 8235 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8236 // when building a module we keep every method individually and may need to 8237 // update hasMoreThanOneDecl as we add the methods. 8238 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8239 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8240 } 8241 8242 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8243 if (SelectorOutOfDate[Sel]) 8244 ReadMethodPool(Sel); 8245 } 8246 8247 void ASTReader::ReadKnownNamespaces( 8248 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8249 Namespaces.clear(); 8250 8251 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8252 if (NamespaceDecl *Namespace 8253 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8254 Namespaces.push_back(Namespace); 8255 } 8256 } 8257 8258 void ASTReader::ReadUndefinedButUsed( 8259 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8260 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8261 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8262 SourceLocation Loc = 8263 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8264 Undefined.insert(std::make_pair(D, Loc)); 8265 } 8266 } 8267 8268 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8269 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8270 Exprs) { 8271 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8272 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8273 uint64_t Count = DelayedDeleteExprs[Idx++]; 8274 for (uint64_t C = 0; C < Count; ++C) { 8275 SourceLocation DeleteLoc = 8276 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8277 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8278 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8279 } 8280 } 8281 } 8282 8283 void ASTReader::ReadTentativeDefinitions( 8284 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8285 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8286 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8287 if (Var) 8288 TentativeDefs.push_back(Var); 8289 } 8290 TentativeDefinitions.clear(); 8291 } 8292 8293 void ASTReader::ReadUnusedFileScopedDecls( 8294 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8295 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8296 DeclaratorDecl *D 8297 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8298 if (D) 8299 Decls.push_back(D); 8300 } 8301 UnusedFileScopedDecls.clear(); 8302 } 8303 8304 void ASTReader::ReadDelegatingConstructors( 8305 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8306 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8307 CXXConstructorDecl *D 8308 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8309 if (D) 8310 Decls.push_back(D); 8311 } 8312 DelegatingCtorDecls.clear(); 8313 } 8314 8315 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8316 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8317 TypedefNameDecl *D 8318 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8319 if (D) 8320 Decls.push_back(D); 8321 } 8322 ExtVectorDecls.clear(); 8323 } 8324 8325 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8326 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8327 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8328 ++I) { 8329 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8330 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8331 if (D) 8332 Decls.insert(D); 8333 } 8334 UnusedLocalTypedefNameCandidates.clear(); 8335 } 8336 8337 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8338 llvm::SmallSetVector<Decl *, 4> &Decls) { 8339 for (auto I : DeclsToCheckForDeferredDiags) { 8340 auto *D = dyn_cast_or_null<Decl>(GetDecl(I)); 8341 if (D) 8342 Decls.insert(D); 8343 } 8344 DeclsToCheckForDeferredDiags.clear(); 8345 } 8346 8347 void ASTReader::ReadReferencedSelectors( 8348 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8349 if (ReferencedSelectorsData.empty()) 8350 return; 8351 8352 // If there are @selector references added them to its pool. This is for 8353 // implementation of -Wselector. 8354 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8355 unsigned I = 0; 8356 while (I < DataSize) { 8357 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8358 SourceLocation SelLoc 8359 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8360 Sels.push_back(std::make_pair(Sel, SelLoc)); 8361 } 8362 ReferencedSelectorsData.clear(); 8363 } 8364 8365 void ASTReader::ReadWeakUndeclaredIdentifiers( 8366 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8367 if (WeakUndeclaredIdentifiers.empty()) 8368 return; 8369 8370 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8371 IdentifierInfo *WeakId 8372 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8373 IdentifierInfo *AliasId 8374 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8375 SourceLocation Loc 8376 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8377 bool Used = WeakUndeclaredIdentifiers[I++]; 8378 WeakInfo WI(AliasId, Loc); 8379 WI.setUsed(Used); 8380 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8381 } 8382 WeakUndeclaredIdentifiers.clear(); 8383 } 8384 8385 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8386 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8387 ExternalVTableUse VT; 8388 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8389 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8390 VT.DefinitionRequired = VTableUses[Idx++]; 8391 VTables.push_back(VT); 8392 } 8393 8394 VTableUses.clear(); 8395 } 8396 8397 void ASTReader::ReadPendingInstantiations( 8398 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8399 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8400 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8401 SourceLocation Loc 8402 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8403 8404 Pending.push_back(std::make_pair(D, Loc)); 8405 } 8406 PendingInstantiations.clear(); 8407 } 8408 8409 void ASTReader::ReadLateParsedTemplates( 8410 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8411 &LPTMap) { 8412 for (auto &LPT : LateParsedTemplates) { 8413 ModuleFile *FMod = LPT.first; 8414 RecordDataImpl &LateParsed = LPT.second; 8415 for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; 8416 /* In loop */) { 8417 FunctionDecl *FD = 8418 cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++])); 8419 8420 auto LT = std::make_unique<LateParsedTemplate>(); 8421 LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]); 8422 8423 ModuleFile *F = getOwningModuleFile(LT->D); 8424 assert(F && "No module"); 8425 8426 unsigned TokN = LateParsed[Idx++]; 8427 LT->Toks.reserve(TokN); 8428 for (unsigned T = 0; T < TokN; ++T) 8429 LT->Toks.push_back(ReadToken(*F, LateParsed, Idx)); 8430 8431 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8432 } 8433 } 8434 8435 LateParsedTemplates.clear(); 8436 } 8437 8438 void ASTReader::LoadSelector(Selector Sel) { 8439 // It would be complicated to avoid reading the methods anyway. So don't. 8440 ReadMethodPool(Sel); 8441 } 8442 8443 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8444 assert(ID && "Non-zero identifier ID required"); 8445 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8446 IdentifiersLoaded[ID - 1] = II; 8447 if (DeserializationListener) 8448 DeserializationListener->IdentifierRead(ID, II); 8449 } 8450 8451 /// Set the globally-visible declarations associated with the given 8452 /// identifier. 8453 /// 8454 /// If the AST reader is currently in a state where the given declaration IDs 8455 /// cannot safely be resolved, they are queued until it is safe to resolve 8456 /// them. 8457 /// 8458 /// \param II an IdentifierInfo that refers to one or more globally-visible 8459 /// declarations. 8460 /// 8461 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8462 /// visible at global scope. 8463 /// 8464 /// \param Decls if non-null, this vector will be populated with the set of 8465 /// deserialized declarations. These declarations will not be pushed into 8466 /// scope. 8467 void 8468 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8469 const SmallVectorImpl<uint32_t> &DeclIDs, 8470 SmallVectorImpl<Decl *> *Decls) { 8471 if (NumCurrentElementsDeserializing && !Decls) { 8472 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8473 return; 8474 } 8475 8476 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8477 if (!SemaObj) { 8478 // Queue this declaration so that it will be added to the 8479 // translation unit scope and identifier's declaration chain 8480 // once a Sema object is known. 8481 PreloadedDeclIDs.push_back(DeclIDs[I]); 8482 continue; 8483 } 8484 8485 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8486 8487 // If we're simply supposed to record the declarations, do so now. 8488 if (Decls) { 8489 Decls->push_back(D); 8490 continue; 8491 } 8492 8493 // Introduce this declaration into the translation-unit scope 8494 // and add it to the declaration chain for this identifier, so 8495 // that (unqualified) name lookup will find it. 8496 pushExternalDeclIntoScope(D, II); 8497 } 8498 } 8499 8500 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8501 if (ID == 0) 8502 return nullptr; 8503 8504 if (IdentifiersLoaded.empty()) { 8505 Error("no identifier table in AST file"); 8506 return nullptr; 8507 } 8508 8509 ID -= 1; 8510 if (!IdentifiersLoaded[ID]) { 8511 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8512 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8513 ModuleFile *M = I->second; 8514 unsigned Index = ID - M->BaseIdentifierID; 8515 const unsigned char *Data = 8516 M->IdentifierTableData + M->IdentifierOffsets[Index]; 8517 8518 ASTIdentifierLookupTrait Trait(*this, *M); 8519 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 8520 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 8521 auto &II = PP.getIdentifierTable().get(Key); 8522 IdentifiersLoaded[ID] = &II; 8523 markIdentifierFromAST(*this, II); 8524 if (DeserializationListener) 8525 DeserializationListener->IdentifierRead(ID + 1, &II); 8526 } 8527 8528 return IdentifiersLoaded[ID]; 8529 } 8530 8531 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8532 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8533 } 8534 8535 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8536 if (LocalID < NUM_PREDEF_IDENT_IDS) 8537 return LocalID; 8538 8539 if (!M.ModuleOffsetMap.empty()) 8540 ReadModuleOffsetMap(M); 8541 8542 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8543 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8544 assert(I != M.IdentifierRemap.end() 8545 && "Invalid index into identifier index remap"); 8546 8547 return LocalID + I->second; 8548 } 8549 8550 MacroInfo *ASTReader::getMacro(MacroID ID) { 8551 if (ID == 0) 8552 return nullptr; 8553 8554 if (MacrosLoaded.empty()) { 8555 Error("no macro table in AST file"); 8556 return nullptr; 8557 } 8558 8559 ID -= NUM_PREDEF_MACRO_IDS; 8560 if (!MacrosLoaded[ID]) { 8561 GlobalMacroMapType::iterator I 8562 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8563 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8564 ModuleFile *M = I->second; 8565 unsigned Index = ID - M->BaseMacroID; 8566 MacrosLoaded[ID] = 8567 ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]); 8568 8569 if (DeserializationListener) 8570 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8571 MacrosLoaded[ID]); 8572 } 8573 8574 return MacrosLoaded[ID]; 8575 } 8576 8577 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8578 if (LocalID < NUM_PREDEF_MACRO_IDS) 8579 return LocalID; 8580 8581 if (!M.ModuleOffsetMap.empty()) 8582 ReadModuleOffsetMap(M); 8583 8584 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8585 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8586 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8587 8588 return LocalID + I->second; 8589 } 8590 8591 serialization::SubmoduleID 8592 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8593 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8594 return LocalID; 8595 8596 if (!M.ModuleOffsetMap.empty()) 8597 ReadModuleOffsetMap(M); 8598 8599 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8600 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8601 assert(I != M.SubmoduleRemap.end() 8602 && "Invalid index into submodule index remap"); 8603 8604 return LocalID + I->second; 8605 } 8606 8607 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8608 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8609 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8610 return nullptr; 8611 } 8612 8613 if (GlobalID > SubmodulesLoaded.size()) { 8614 Error("submodule ID out of range in AST file"); 8615 return nullptr; 8616 } 8617 8618 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8619 } 8620 8621 Module *ASTReader::getModule(unsigned ID) { 8622 return getSubmodule(ID); 8623 } 8624 8625 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8626 if (ID & 1) { 8627 // It's a module, look it up by submodule ID. 8628 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8629 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8630 } else { 8631 // It's a prefix (preamble, PCH, ...). Look it up by index. 8632 unsigned IndexFromEnd = ID >> 1; 8633 assert(IndexFromEnd && "got reference to unknown module file"); 8634 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8635 } 8636 } 8637 8638 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8639 if (!F) 8640 return 1; 8641 8642 // For a file representing a module, use the submodule ID of the top-level 8643 // module as the file ID. For any other kind of file, the number of such 8644 // files loaded beforehand will be the same on reload. 8645 // FIXME: Is this true even if we have an explicit module file and a PCH? 8646 if (F->isModule()) 8647 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8648 8649 auto PCHModules = getModuleManager().pch_modules(); 8650 auto I = llvm::find(PCHModules, F); 8651 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8652 return (I - PCHModules.end()) << 1; 8653 } 8654 8655 llvm::Optional<ASTSourceDescriptor> 8656 ASTReader::getSourceDescriptor(unsigned ID) { 8657 if (Module *M = getSubmodule(ID)) 8658 return ASTSourceDescriptor(*M); 8659 8660 // If there is only a single PCH, return it instead. 8661 // Chained PCH are not supported. 8662 const auto &PCHChain = ModuleMgr.pch_modules(); 8663 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8664 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8665 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8666 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8667 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8668 MF.Signature); 8669 } 8670 return None; 8671 } 8672 8673 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8674 auto I = DefinitionSource.find(FD); 8675 if (I == DefinitionSource.end()) 8676 return EK_ReplyHazy; 8677 return I->second ? EK_Never : EK_Always; 8678 } 8679 8680 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8681 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8682 } 8683 8684 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8685 if (ID == 0) 8686 return Selector(); 8687 8688 if (ID > SelectorsLoaded.size()) { 8689 Error("selector ID out of range in AST file"); 8690 return Selector(); 8691 } 8692 8693 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8694 // Load this selector from the selector table. 8695 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8696 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8697 ModuleFile &M = *I->second; 8698 ASTSelectorLookupTrait Trait(*this, M); 8699 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8700 SelectorsLoaded[ID - 1] = 8701 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8702 if (DeserializationListener) 8703 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8704 } 8705 8706 return SelectorsLoaded[ID - 1]; 8707 } 8708 8709 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8710 return DecodeSelector(ID); 8711 } 8712 8713 uint32_t ASTReader::GetNumExternalSelectors() { 8714 // ID 0 (the null selector) is considered an external selector. 8715 return getTotalNumSelectors() + 1; 8716 } 8717 8718 serialization::SelectorID 8719 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8720 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8721 return LocalID; 8722 8723 if (!M.ModuleOffsetMap.empty()) 8724 ReadModuleOffsetMap(M); 8725 8726 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8727 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8728 assert(I != M.SelectorRemap.end() 8729 && "Invalid index into selector index remap"); 8730 8731 return LocalID + I->second; 8732 } 8733 8734 DeclarationNameLoc 8735 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8736 switch (Name.getNameKind()) { 8737 case DeclarationName::CXXConstructorName: 8738 case DeclarationName::CXXDestructorName: 8739 case DeclarationName::CXXConversionFunctionName: 8740 return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo()); 8741 8742 case DeclarationName::CXXOperatorName: 8743 return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange()); 8744 8745 case DeclarationName::CXXLiteralOperatorName: 8746 return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc( 8747 readSourceLocation()); 8748 8749 case DeclarationName::Identifier: 8750 case DeclarationName::ObjCZeroArgSelector: 8751 case DeclarationName::ObjCOneArgSelector: 8752 case DeclarationName::ObjCMultiArgSelector: 8753 case DeclarationName::CXXUsingDirective: 8754 case DeclarationName::CXXDeductionGuideName: 8755 break; 8756 } 8757 return DeclarationNameLoc(); 8758 } 8759 8760 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8761 DeclarationNameInfo NameInfo; 8762 NameInfo.setName(readDeclarationName()); 8763 NameInfo.setLoc(readSourceLocation()); 8764 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8765 return NameInfo; 8766 } 8767 8768 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8769 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8770 unsigned NumTPLists = readInt(); 8771 Info.NumTemplParamLists = NumTPLists; 8772 if (NumTPLists) { 8773 Info.TemplParamLists = 8774 new (getContext()) TemplateParameterList *[NumTPLists]; 8775 for (unsigned i = 0; i != NumTPLists; ++i) 8776 Info.TemplParamLists[i] = readTemplateParameterList(); 8777 } 8778 } 8779 8780 TemplateParameterList * 8781 ASTRecordReader::readTemplateParameterList() { 8782 SourceLocation TemplateLoc = readSourceLocation(); 8783 SourceLocation LAngleLoc = readSourceLocation(); 8784 SourceLocation RAngleLoc = readSourceLocation(); 8785 8786 unsigned NumParams = readInt(); 8787 SmallVector<NamedDecl *, 16> Params; 8788 Params.reserve(NumParams); 8789 while (NumParams--) 8790 Params.push_back(readDeclAs<NamedDecl>()); 8791 8792 bool HasRequiresClause = readBool(); 8793 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8794 8795 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8796 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8797 return TemplateParams; 8798 } 8799 8800 void ASTRecordReader::readTemplateArgumentList( 8801 SmallVectorImpl<TemplateArgument> &TemplArgs, 8802 bool Canonicalize) { 8803 unsigned NumTemplateArgs = readInt(); 8804 TemplArgs.reserve(NumTemplateArgs); 8805 while (NumTemplateArgs--) 8806 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8807 } 8808 8809 /// Read a UnresolvedSet structure. 8810 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8811 unsigned NumDecls = readInt(); 8812 Set.reserve(getContext(), NumDecls); 8813 while (NumDecls--) { 8814 DeclID ID = readDeclID(); 8815 AccessSpecifier AS = (AccessSpecifier) readInt(); 8816 Set.addLazyDecl(getContext(), ID, AS); 8817 } 8818 } 8819 8820 CXXBaseSpecifier 8821 ASTRecordReader::readCXXBaseSpecifier() { 8822 bool isVirtual = readBool(); 8823 bool isBaseOfClass = readBool(); 8824 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8825 bool inheritConstructors = readBool(); 8826 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8827 SourceRange Range = readSourceRange(); 8828 SourceLocation EllipsisLoc = readSourceLocation(); 8829 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8830 EllipsisLoc); 8831 Result.setInheritConstructors(inheritConstructors); 8832 return Result; 8833 } 8834 8835 CXXCtorInitializer ** 8836 ASTRecordReader::readCXXCtorInitializers() { 8837 ASTContext &Context = getContext(); 8838 unsigned NumInitializers = readInt(); 8839 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8840 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8841 for (unsigned i = 0; i != NumInitializers; ++i) { 8842 TypeSourceInfo *TInfo = nullptr; 8843 bool IsBaseVirtual = false; 8844 FieldDecl *Member = nullptr; 8845 IndirectFieldDecl *IndirectMember = nullptr; 8846 8847 CtorInitializerType Type = (CtorInitializerType) readInt(); 8848 switch (Type) { 8849 case CTOR_INITIALIZER_BASE: 8850 TInfo = readTypeSourceInfo(); 8851 IsBaseVirtual = readBool(); 8852 break; 8853 8854 case CTOR_INITIALIZER_DELEGATING: 8855 TInfo = readTypeSourceInfo(); 8856 break; 8857 8858 case CTOR_INITIALIZER_MEMBER: 8859 Member = readDeclAs<FieldDecl>(); 8860 break; 8861 8862 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8863 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8864 break; 8865 } 8866 8867 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8868 Expr *Init = readExpr(); 8869 SourceLocation LParenLoc = readSourceLocation(); 8870 SourceLocation RParenLoc = readSourceLocation(); 8871 8872 CXXCtorInitializer *BOMInit; 8873 if (Type == CTOR_INITIALIZER_BASE) 8874 BOMInit = new (Context) 8875 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8876 RParenLoc, MemberOrEllipsisLoc); 8877 else if (Type == CTOR_INITIALIZER_DELEGATING) 8878 BOMInit = new (Context) 8879 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8880 else if (Member) 8881 BOMInit = new (Context) 8882 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8883 Init, RParenLoc); 8884 else 8885 BOMInit = new (Context) 8886 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8887 LParenLoc, Init, RParenLoc); 8888 8889 if (/*IsWritten*/readBool()) { 8890 unsigned SourceOrder = readInt(); 8891 BOMInit->setSourceOrder(SourceOrder); 8892 } 8893 8894 CtorInitializers[i] = BOMInit; 8895 } 8896 8897 return CtorInitializers; 8898 } 8899 8900 NestedNameSpecifierLoc 8901 ASTRecordReader::readNestedNameSpecifierLoc() { 8902 ASTContext &Context = getContext(); 8903 unsigned N = readInt(); 8904 NestedNameSpecifierLocBuilder Builder; 8905 for (unsigned I = 0; I != N; ++I) { 8906 auto Kind = readNestedNameSpecifierKind(); 8907 switch (Kind) { 8908 case NestedNameSpecifier::Identifier: { 8909 IdentifierInfo *II = readIdentifier(); 8910 SourceRange Range = readSourceRange(); 8911 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8912 break; 8913 } 8914 8915 case NestedNameSpecifier::Namespace: { 8916 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8917 SourceRange Range = readSourceRange(); 8918 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8919 break; 8920 } 8921 8922 case NestedNameSpecifier::NamespaceAlias: { 8923 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8924 SourceRange Range = readSourceRange(); 8925 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8926 break; 8927 } 8928 8929 case NestedNameSpecifier::TypeSpec: 8930 case NestedNameSpecifier::TypeSpecWithTemplate: { 8931 bool Template = readBool(); 8932 TypeSourceInfo *T = readTypeSourceInfo(); 8933 if (!T) 8934 return NestedNameSpecifierLoc(); 8935 SourceLocation ColonColonLoc = readSourceLocation(); 8936 8937 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8938 Builder.Extend(Context, 8939 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8940 T->getTypeLoc(), ColonColonLoc); 8941 break; 8942 } 8943 8944 case NestedNameSpecifier::Global: { 8945 SourceLocation ColonColonLoc = readSourceLocation(); 8946 Builder.MakeGlobal(Context, ColonColonLoc); 8947 break; 8948 } 8949 8950 case NestedNameSpecifier::Super: { 8951 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8952 SourceRange Range = readSourceRange(); 8953 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8954 break; 8955 } 8956 } 8957 } 8958 8959 return Builder.getWithLocInContext(Context); 8960 } 8961 8962 SourceRange 8963 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8964 unsigned &Idx) { 8965 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8966 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8967 return SourceRange(beg, end); 8968 } 8969 8970 /// Read a floating-point value 8971 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 8972 return llvm::APFloat(Sem, readAPInt()); 8973 } 8974 8975 // Read a string 8976 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 8977 unsigned Len = Record[Idx++]; 8978 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 8979 Idx += Len; 8980 return Result; 8981 } 8982 8983 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 8984 unsigned &Idx) { 8985 std::string Filename = ReadString(Record, Idx); 8986 ResolveImportedPath(F, Filename); 8987 return Filename; 8988 } 8989 8990 std::string ASTReader::ReadPath(StringRef BaseDirectory, 8991 const RecordData &Record, unsigned &Idx) { 8992 std::string Filename = ReadString(Record, Idx); 8993 if (!BaseDirectory.empty()) 8994 ResolveImportedPath(Filename, BaseDirectory); 8995 return Filename; 8996 } 8997 8998 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 8999 unsigned &Idx) { 9000 unsigned Major = Record[Idx++]; 9001 unsigned Minor = Record[Idx++]; 9002 unsigned Subminor = Record[Idx++]; 9003 if (Minor == 0) 9004 return VersionTuple(Major); 9005 if (Subminor == 0) 9006 return VersionTuple(Major, Minor - 1); 9007 return VersionTuple(Major, Minor - 1, Subminor - 1); 9008 } 9009 9010 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 9011 const RecordData &Record, 9012 unsigned &Idx) { 9013 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 9014 return CXXTemporary::Create(getContext(), Decl); 9015 } 9016 9017 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 9018 return Diag(CurrentImportLoc, DiagID); 9019 } 9020 9021 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 9022 return Diags.Report(Loc, DiagID); 9023 } 9024 9025 /// Retrieve the identifier table associated with the 9026 /// preprocessor. 9027 IdentifierTable &ASTReader::getIdentifierTable() { 9028 return PP.getIdentifierTable(); 9029 } 9030 9031 /// Record that the given ID maps to the given switch-case 9032 /// statement. 9033 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 9034 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 9035 "Already have a SwitchCase with this ID"); 9036 (*CurrSwitchCaseStmts)[ID] = SC; 9037 } 9038 9039 /// Retrieve the switch-case statement with the given ID. 9040 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 9041 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 9042 return (*CurrSwitchCaseStmts)[ID]; 9043 } 9044 9045 void ASTReader::ClearSwitchCaseIDs() { 9046 CurrSwitchCaseStmts->clear(); 9047 } 9048 9049 void ASTReader::ReadComments() { 9050 ASTContext &Context = getContext(); 9051 std::vector<RawComment *> Comments; 9052 for (SmallVectorImpl<std::pair<BitstreamCursor, 9053 serialization::ModuleFile *>>::iterator 9054 I = CommentsCursors.begin(), 9055 E = CommentsCursors.end(); 9056 I != E; ++I) { 9057 Comments.clear(); 9058 BitstreamCursor &Cursor = I->first; 9059 serialization::ModuleFile &F = *I->second; 9060 SavedStreamPosition SavedPosition(Cursor); 9061 9062 RecordData Record; 9063 while (true) { 9064 Expected<llvm::BitstreamEntry> MaybeEntry = 9065 Cursor.advanceSkippingSubblocks( 9066 BitstreamCursor::AF_DontPopBlockAtEnd); 9067 if (!MaybeEntry) { 9068 Error(MaybeEntry.takeError()); 9069 return; 9070 } 9071 llvm::BitstreamEntry Entry = MaybeEntry.get(); 9072 9073 switch (Entry.Kind) { 9074 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 9075 case llvm::BitstreamEntry::Error: 9076 Error("malformed block record in AST file"); 9077 return; 9078 case llvm::BitstreamEntry::EndBlock: 9079 goto NextCursor; 9080 case llvm::BitstreamEntry::Record: 9081 // The interesting case. 9082 break; 9083 } 9084 9085 // Read a record. 9086 Record.clear(); 9087 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9088 if (!MaybeComment) { 9089 Error(MaybeComment.takeError()); 9090 return; 9091 } 9092 switch ((CommentRecordTypes)MaybeComment.get()) { 9093 case COMMENTS_RAW_COMMENT: { 9094 unsigned Idx = 0; 9095 SourceRange SR = ReadSourceRange(F, Record, Idx); 9096 RawComment::CommentKind Kind = 9097 (RawComment::CommentKind) Record[Idx++]; 9098 bool IsTrailingComment = Record[Idx++]; 9099 bool IsAlmostTrailingComment = Record[Idx++]; 9100 Comments.push_back(new (Context) RawComment( 9101 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9102 break; 9103 } 9104 } 9105 } 9106 NextCursor: 9107 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9108 FileToOffsetToComment; 9109 for (RawComment *C : Comments) { 9110 SourceLocation CommentLoc = C->getBeginLoc(); 9111 if (CommentLoc.isValid()) { 9112 std::pair<FileID, unsigned> Loc = 9113 SourceMgr.getDecomposedLoc(CommentLoc); 9114 if (Loc.first.isValid()) 9115 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9116 } 9117 } 9118 } 9119 } 9120 9121 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9122 bool IncludeSystem, bool Complain, 9123 llvm::function_ref<void(const serialization::InputFile &IF, 9124 bool isSystem)> Visitor) { 9125 unsigned NumUserInputs = MF.NumUserInputFiles; 9126 unsigned NumInputs = MF.InputFilesLoaded.size(); 9127 assert(NumUserInputs <= NumInputs); 9128 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9129 for (unsigned I = 0; I < N; ++I) { 9130 bool IsSystem = I >= NumUserInputs; 9131 InputFile IF = getInputFile(MF, I+1, Complain); 9132 Visitor(IF, IsSystem); 9133 } 9134 } 9135 9136 void ASTReader::visitTopLevelModuleMaps( 9137 serialization::ModuleFile &MF, 9138 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9139 unsigned NumInputs = MF.InputFilesLoaded.size(); 9140 for (unsigned I = 0; I < NumInputs; ++I) { 9141 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9142 if (IFI.TopLevelModuleMap) 9143 // FIXME: This unnecessarily re-reads the InputFileInfo. 9144 if (auto FE = getInputFile(MF, I + 1).getFile()) 9145 Visitor(FE); 9146 } 9147 } 9148 9149 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9150 // If we know the owning module, use it. 9151 if (Module *M = D->getImportedOwningModule()) 9152 return M->getFullModuleName(); 9153 9154 // Otherwise, use the name of the top-level module the decl is within. 9155 if (ModuleFile *M = getOwningModuleFile(D)) 9156 return M->ModuleName; 9157 9158 // Not from a module. 9159 return {}; 9160 } 9161 9162 void ASTReader::finishPendingActions() { 9163 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9164 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9165 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9166 !PendingUpdateRecords.empty()) { 9167 // If any identifiers with corresponding top-level declarations have 9168 // been loaded, load those declarations now. 9169 using TopLevelDeclsMap = 9170 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9171 TopLevelDeclsMap TopLevelDecls; 9172 9173 while (!PendingIdentifierInfos.empty()) { 9174 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9175 SmallVector<uint32_t, 4> DeclIDs = 9176 std::move(PendingIdentifierInfos.back().second); 9177 PendingIdentifierInfos.pop_back(); 9178 9179 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9180 } 9181 9182 // Load each function type that we deferred loading because it was a 9183 // deduced type that might refer to a local type declared within itself. 9184 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9185 auto *FD = PendingFunctionTypes[I].first; 9186 FD->setType(GetType(PendingFunctionTypes[I].second)); 9187 9188 // If we gave a function a deduced return type, remember that we need to 9189 // propagate that along the redeclaration chain. 9190 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9191 if (DT && DT->isDeduced()) 9192 PendingDeducedTypeUpdates.insert( 9193 {FD->getCanonicalDecl(), FD->getReturnType()}); 9194 } 9195 PendingFunctionTypes.clear(); 9196 9197 // For each decl chain that we wanted to complete while deserializing, mark 9198 // it as "still needs to be completed". 9199 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9200 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9201 } 9202 PendingIncompleteDeclChains.clear(); 9203 9204 // Load pending declaration chains. 9205 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9206 loadPendingDeclChain(PendingDeclChains[I].first, 9207 PendingDeclChains[I].second); 9208 PendingDeclChains.clear(); 9209 9210 // Make the most recent of the top-level declarations visible. 9211 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9212 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9213 IdentifierInfo *II = TLD->first; 9214 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9215 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9216 } 9217 } 9218 9219 // Load any pending macro definitions. 9220 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9221 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9222 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9223 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9224 // Initialize the macro history from chained-PCHs ahead of module imports. 9225 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9226 ++IDIdx) { 9227 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9228 if (!Info.M->isModule()) 9229 resolvePendingMacro(II, Info); 9230 } 9231 // Handle module imports. 9232 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9233 ++IDIdx) { 9234 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9235 if (Info.M->isModule()) 9236 resolvePendingMacro(II, Info); 9237 } 9238 } 9239 PendingMacroIDs.clear(); 9240 9241 // Wire up the DeclContexts for Decls that we delayed setting until 9242 // recursive loading is completed. 9243 while (!PendingDeclContextInfos.empty()) { 9244 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9245 PendingDeclContextInfos.pop_front(); 9246 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9247 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9248 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9249 } 9250 9251 // Perform any pending declaration updates. 9252 while (!PendingUpdateRecords.empty()) { 9253 auto Update = PendingUpdateRecords.pop_back_val(); 9254 ReadingKindTracker ReadingKind(Read_Decl, *this); 9255 loadDeclUpdateRecords(Update); 9256 } 9257 } 9258 9259 // At this point, all update records for loaded decls are in place, so any 9260 // fake class definitions should have become real. 9261 assert(PendingFakeDefinitionData.empty() && 9262 "faked up a class definition but never saw the real one"); 9263 9264 // If we deserialized any C++ or Objective-C class definitions, any 9265 // Objective-C protocol definitions, or any redeclarable templates, make sure 9266 // that all redeclarations point to the definitions. Note that this can only 9267 // happen now, after the redeclaration chains have been fully wired. 9268 for (Decl *D : PendingDefinitions) { 9269 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9270 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9271 // Make sure that the TagType points at the definition. 9272 const_cast<TagType*>(TagT)->decl = TD; 9273 } 9274 9275 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9276 for (auto *R = getMostRecentExistingDecl(RD); R; 9277 R = R->getPreviousDecl()) { 9278 assert((R == D) == 9279 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9280 "declaration thinks it's the definition but it isn't"); 9281 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9282 } 9283 } 9284 9285 continue; 9286 } 9287 9288 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9289 // Make sure that the ObjCInterfaceType points at the definition. 9290 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9291 ->Decl = ID; 9292 9293 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9294 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9295 9296 continue; 9297 } 9298 9299 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9300 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9301 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9302 9303 continue; 9304 } 9305 9306 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9307 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9308 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9309 } 9310 PendingDefinitions.clear(); 9311 9312 // Load the bodies of any functions or methods we've encountered. We do 9313 // this now (delayed) so that we can be sure that the declaration chains 9314 // have been fully wired up (hasBody relies on this). 9315 // FIXME: We shouldn't require complete redeclaration chains here. 9316 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9317 PBEnd = PendingBodies.end(); 9318 PB != PBEnd; ++PB) { 9319 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9320 // For a function defined inline within a class template, force the 9321 // canonical definition to be the one inside the canonical definition of 9322 // the template. This ensures that we instantiate from a correct view 9323 // of the template. 9324 // 9325 // Sadly we can't do this more generally: we can't be sure that all 9326 // copies of an arbitrary class definition will have the same members 9327 // defined (eg, some member functions may not be instantiated, and some 9328 // special members may or may not have been implicitly defined). 9329 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9330 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9331 continue; 9332 9333 // FIXME: Check for =delete/=default? 9334 // FIXME: Complain about ODR violations here? 9335 const FunctionDecl *Defn = nullptr; 9336 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9337 FD->setLazyBody(PB->second); 9338 } else { 9339 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9340 mergeDefinitionVisibility(NonConstDefn, FD); 9341 9342 if (!FD->isLateTemplateParsed() && 9343 !NonConstDefn->isLateTemplateParsed() && 9344 FD->getODRHash() != NonConstDefn->getODRHash()) { 9345 if (!isa<CXXMethodDecl>(FD)) { 9346 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9347 } else if (FD->getLexicalParent()->isFileContext() && 9348 NonConstDefn->getLexicalParent()->isFileContext()) { 9349 // Only diagnose out-of-line method definitions. If they are 9350 // in class definitions, then an error will be generated when 9351 // processing the class bodies. 9352 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9353 } 9354 } 9355 } 9356 continue; 9357 } 9358 9359 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9360 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9361 MD->setLazyBody(PB->second); 9362 } 9363 PendingBodies.clear(); 9364 9365 // Do some cleanup. 9366 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9367 getContext().deduplicateMergedDefinitonsFor(ND); 9368 PendingMergedDefinitionsToDeduplicate.clear(); 9369 } 9370 9371 void ASTReader::diagnoseOdrViolations() { 9372 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9373 PendingFunctionOdrMergeFailures.empty() && 9374 PendingEnumOdrMergeFailures.empty()) 9375 return; 9376 9377 // Trigger the import of the full definition of each class that had any 9378 // odr-merging problems, so we can produce better diagnostics for them. 9379 // These updates may in turn find and diagnose some ODR failures, so take 9380 // ownership of the set first. 9381 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9382 PendingOdrMergeFailures.clear(); 9383 for (auto &Merge : OdrMergeFailures) { 9384 Merge.first->buildLookup(); 9385 Merge.first->decls_begin(); 9386 Merge.first->bases_begin(); 9387 Merge.first->vbases_begin(); 9388 for (auto &RecordPair : Merge.second) { 9389 auto *RD = RecordPair.first; 9390 RD->decls_begin(); 9391 RD->bases_begin(); 9392 RD->vbases_begin(); 9393 } 9394 } 9395 9396 // Trigger the import of functions. 9397 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9398 PendingFunctionOdrMergeFailures.clear(); 9399 for (auto &Merge : FunctionOdrMergeFailures) { 9400 Merge.first->buildLookup(); 9401 Merge.first->decls_begin(); 9402 Merge.first->getBody(); 9403 for (auto &FD : Merge.second) { 9404 FD->buildLookup(); 9405 FD->decls_begin(); 9406 FD->getBody(); 9407 } 9408 } 9409 9410 // Trigger the import of enums. 9411 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9412 PendingEnumOdrMergeFailures.clear(); 9413 for (auto &Merge : EnumOdrMergeFailures) { 9414 Merge.first->decls_begin(); 9415 for (auto &Enum : Merge.second) { 9416 Enum->decls_begin(); 9417 } 9418 } 9419 9420 // For each declaration from a merged context, check that the canonical 9421 // definition of that context also contains a declaration of the same 9422 // entity. 9423 // 9424 // Caution: this loop does things that might invalidate iterators into 9425 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9426 while (!PendingOdrMergeChecks.empty()) { 9427 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9428 9429 // FIXME: Skip over implicit declarations for now. This matters for things 9430 // like implicitly-declared special member functions. This isn't entirely 9431 // correct; we can end up with multiple unmerged declarations of the same 9432 // implicit entity. 9433 if (D->isImplicit()) 9434 continue; 9435 9436 DeclContext *CanonDef = D->getDeclContext(); 9437 9438 bool Found = false; 9439 const Decl *DCanon = D->getCanonicalDecl(); 9440 9441 for (auto RI : D->redecls()) { 9442 if (RI->getLexicalDeclContext() == CanonDef) { 9443 Found = true; 9444 break; 9445 } 9446 } 9447 if (Found) 9448 continue; 9449 9450 // Quick check failed, time to do the slow thing. Note, we can't just 9451 // look up the name of D in CanonDef here, because the member that is 9452 // in CanonDef might not be found by name lookup (it might have been 9453 // replaced by a more recent declaration in the lookup table), and we 9454 // can't necessarily find it in the redeclaration chain because it might 9455 // be merely mergeable, not redeclarable. 9456 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9457 for (auto *CanonMember : CanonDef->decls()) { 9458 if (CanonMember->getCanonicalDecl() == DCanon) { 9459 // This can happen if the declaration is merely mergeable and not 9460 // actually redeclarable (we looked for redeclarations earlier). 9461 // 9462 // FIXME: We should be able to detect this more efficiently, without 9463 // pulling in all of the members of CanonDef. 9464 Found = true; 9465 break; 9466 } 9467 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9468 if (ND->getDeclName() == D->getDeclName()) 9469 Candidates.push_back(ND); 9470 } 9471 9472 if (!Found) { 9473 // The AST doesn't like TagDecls becoming invalid after they've been 9474 // completed. We only really need to mark FieldDecls as invalid here. 9475 if (!isa<TagDecl>(D)) 9476 D->setInvalidDecl(); 9477 9478 // Ensure we don't accidentally recursively enter deserialization while 9479 // we're producing our diagnostic. 9480 Deserializing RecursionGuard(this); 9481 9482 std::string CanonDefModule = 9483 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9484 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9485 << D << getOwningModuleNameForDiagnostic(D) 9486 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9487 9488 if (Candidates.empty()) 9489 Diag(cast<Decl>(CanonDef)->getLocation(), 9490 diag::note_module_odr_violation_no_possible_decls) << D; 9491 else { 9492 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9493 Diag(Candidates[I]->getLocation(), 9494 diag::note_module_odr_violation_possible_decl) 9495 << Candidates[I]; 9496 } 9497 9498 DiagnosedOdrMergeFailures.insert(CanonDef); 9499 } 9500 } 9501 9502 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9503 EnumOdrMergeFailures.empty()) 9504 return; 9505 9506 // Ensure we don't accidentally recursively enter deserialization while 9507 // we're producing our diagnostics. 9508 Deserializing RecursionGuard(this); 9509 9510 // Common code for hashing helpers. 9511 ODRHash Hash; 9512 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9513 Hash.clear(); 9514 Hash.AddQualType(Ty); 9515 return Hash.CalculateHash(); 9516 }; 9517 9518 auto ComputeODRHash = [&Hash](const Stmt *S) { 9519 assert(S); 9520 Hash.clear(); 9521 Hash.AddStmt(S); 9522 return Hash.CalculateHash(); 9523 }; 9524 9525 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9526 assert(D); 9527 Hash.clear(); 9528 Hash.AddSubDecl(D); 9529 return Hash.CalculateHash(); 9530 }; 9531 9532 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9533 Hash.clear(); 9534 Hash.AddTemplateArgument(TA); 9535 return Hash.CalculateHash(); 9536 }; 9537 9538 auto ComputeTemplateParameterListODRHash = 9539 [&Hash](const TemplateParameterList *TPL) { 9540 assert(TPL); 9541 Hash.clear(); 9542 Hash.AddTemplateParameterList(TPL); 9543 return Hash.CalculateHash(); 9544 }; 9545 9546 // Used with err_module_odr_violation_mismatch_decl and 9547 // note_module_odr_violation_mismatch_decl 9548 // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed 9549 enum ODRMismatchDecl { 9550 EndOfClass, 9551 PublicSpecifer, 9552 PrivateSpecifer, 9553 ProtectedSpecifer, 9554 StaticAssert, 9555 Field, 9556 CXXMethod, 9557 TypeAlias, 9558 TypeDef, 9559 Var, 9560 Friend, 9561 FunctionTemplate, 9562 Other 9563 }; 9564 9565 // Used with err_module_odr_violation_mismatch_decl_diff and 9566 // note_module_odr_violation_mismatch_decl_diff 9567 enum ODRMismatchDeclDifference { 9568 StaticAssertCondition, 9569 StaticAssertMessage, 9570 StaticAssertOnlyMessage, 9571 FieldName, 9572 FieldTypeName, 9573 FieldSingleBitField, 9574 FieldDifferentWidthBitField, 9575 FieldSingleMutable, 9576 FieldSingleInitializer, 9577 FieldDifferentInitializers, 9578 MethodName, 9579 MethodDeleted, 9580 MethodDefaulted, 9581 MethodVirtual, 9582 MethodStatic, 9583 MethodVolatile, 9584 MethodConst, 9585 MethodInline, 9586 MethodNumberParameters, 9587 MethodParameterType, 9588 MethodParameterName, 9589 MethodParameterSingleDefaultArgument, 9590 MethodParameterDifferentDefaultArgument, 9591 MethodNoTemplateArguments, 9592 MethodDifferentNumberTemplateArguments, 9593 MethodDifferentTemplateArgument, 9594 MethodSingleBody, 9595 MethodDifferentBody, 9596 TypedefName, 9597 TypedefType, 9598 VarName, 9599 VarType, 9600 VarSingleInitializer, 9601 VarDifferentInitializer, 9602 VarConstexpr, 9603 FriendTypeFunction, 9604 FriendType, 9605 FriendFunction, 9606 FunctionTemplateDifferentNumberParameters, 9607 FunctionTemplateParameterDifferentKind, 9608 FunctionTemplateParameterName, 9609 FunctionTemplateParameterSingleDefaultArgument, 9610 FunctionTemplateParameterDifferentDefaultArgument, 9611 FunctionTemplateParameterDifferentType, 9612 FunctionTemplatePackParameter, 9613 }; 9614 9615 // These lambdas have the common portions of the ODR diagnostics. This 9616 // has the same return as Diag(), so addition parameters can be passed 9617 // in with operator<< 9618 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9619 SourceLocation Loc, SourceRange Range, 9620 ODRMismatchDeclDifference DiffType) { 9621 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9622 << FirstRecord << FirstModule.empty() << FirstModule << Range 9623 << DiffType; 9624 }; 9625 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9626 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9627 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9628 << SecondModule << Range << DiffType; 9629 }; 9630 9631 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9632 &ComputeQualTypeODRHash, &ComputeODRHash]( 9633 NamedDecl *FirstRecord, StringRef FirstModule, 9634 StringRef SecondModule, FieldDecl *FirstField, 9635 FieldDecl *SecondField) { 9636 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9637 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9638 if (FirstII->getName() != SecondII->getName()) { 9639 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9640 FirstField->getSourceRange(), FieldName) 9641 << FirstII; 9642 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9643 SecondField->getSourceRange(), FieldName) 9644 << SecondII; 9645 9646 return true; 9647 } 9648 9649 assert(getContext().hasSameType(FirstField->getType(), 9650 SecondField->getType())); 9651 9652 QualType FirstType = FirstField->getType(); 9653 QualType SecondType = SecondField->getType(); 9654 if (ComputeQualTypeODRHash(FirstType) != 9655 ComputeQualTypeODRHash(SecondType)) { 9656 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9657 FirstField->getSourceRange(), FieldTypeName) 9658 << FirstII << FirstType; 9659 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9660 SecondField->getSourceRange(), FieldTypeName) 9661 << SecondII << SecondType; 9662 9663 return true; 9664 } 9665 9666 const bool IsFirstBitField = FirstField->isBitField(); 9667 const bool IsSecondBitField = SecondField->isBitField(); 9668 if (IsFirstBitField != IsSecondBitField) { 9669 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9670 FirstField->getSourceRange(), FieldSingleBitField) 9671 << FirstII << IsFirstBitField; 9672 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9673 SecondField->getSourceRange(), FieldSingleBitField) 9674 << SecondII << IsSecondBitField; 9675 return true; 9676 } 9677 9678 if (IsFirstBitField && IsSecondBitField) { 9679 unsigned FirstBitWidthHash = 9680 ComputeODRHash(FirstField->getBitWidth()); 9681 unsigned SecondBitWidthHash = 9682 ComputeODRHash(SecondField->getBitWidth()); 9683 if (FirstBitWidthHash != SecondBitWidthHash) { 9684 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9685 FirstField->getSourceRange(), 9686 FieldDifferentWidthBitField) 9687 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9688 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9689 SecondField->getSourceRange(), 9690 FieldDifferentWidthBitField) 9691 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9692 return true; 9693 } 9694 } 9695 9696 if (!PP.getLangOpts().CPlusPlus) 9697 return false; 9698 9699 const bool IsFirstMutable = FirstField->isMutable(); 9700 const bool IsSecondMutable = SecondField->isMutable(); 9701 if (IsFirstMutable != IsSecondMutable) { 9702 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9703 FirstField->getSourceRange(), FieldSingleMutable) 9704 << FirstII << IsFirstMutable; 9705 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9706 SecondField->getSourceRange(), FieldSingleMutable) 9707 << SecondII << IsSecondMutable; 9708 return true; 9709 } 9710 9711 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9712 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9713 if ((!FirstInitializer && SecondInitializer) || 9714 (FirstInitializer && !SecondInitializer)) { 9715 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9716 FirstField->getSourceRange(), FieldSingleInitializer) 9717 << FirstII << (FirstInitializer != nullptr); 9718 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9719 SecondField->getSourceRange(), FieldSingleInitializer) 9720 << SecondII << (SecondInitializer != nullptr); 9721 return true; 9722 } 9723 9724 if (FirstInitializer && SecondInitializer) { 9725 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9726 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9727 if (FirstInitHash != SecondInitHash) { 9728 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9729 FirstField->getSourceRange(), 9730 FieldDifferentInitializers) 9731 << FirstII << FirstInitializer->getSourceRange(); 9732 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9733 SecondField->getSourceRange(), 9734 FieldDifferentInitializers) 9735 << SecondII << SecondInitializer->getSourceRange(); 9736 return true; 9737 } 9738 } 9739 9740 return false; 9741 }; 9742 9743 auto ODRDiagTypeDefOrAlias = 9744 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9745 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9746 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9747 bool IsTypeAlias) { 9748 auto FirstName = FirstTD->getDeclName(); 9749 auto SecondName = SecondTD->getDeclName(); 9750 if (FirstName != SecondName) { 9751 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9752 FirstTD->getSourceRange(), TypedefName) 9753 << IsTypeAlias << FirstName; 9754 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9755 SecondTD->getSourceRange(), TypedefName) 9756 << IsTypeAlias << SecondName; 9757 return true; 9758 } 9759 9760 QualType FirstType = FirstTD->getUnderlyingType(); 9761 QualType SecondType = SecondTD->getUnderlyingType(); 9762 if (ComputeQualTypeODRHash(FirstType) != 9763 ComputeQualTypeODRHash(SecondType)) { 9764 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9765 FirstTD->getSourceRange(), TypedefType) 9766 << IsTypeAlias << FirstName << FirstType; 9767 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9768 SecondTD->getSourceRange(), TypedefType) 9769 << IsTypeAlias << SecondName << SecondType; 9770 return true; 9771 } 9772 9773 return false; 9774 }; 9775 9776 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9777 &ComputeQualTypeODRHash, &ComputeODRHash, 9778 this](NamedDecl *FirstRecord, StringRef FirstModule, 9779 StringRef SecondModule, VarDecl *FirstVD, 9780 VarDecl *SecondVD) { 9781 auto FirstName = FirstVD->getDeclName(); 9782 auto SecondName = SecondVD->getDeclName(); 9783 if (FirstName != SecondName) { 9784 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9785 FirstVD->getSourceRange(), VarName) 9786 << FirstName; 9787 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9788 SecondVD->getSourceRange(), VarName) 9789 << SecondName; 9790 return true; 9791 } 9792 9793 QualType FirstType = FirstVD->getType(); 9794 QualType SecondType = SecondVD->getType(); 9795 if (ComputeQualTypeODRHash(FirstType) != 9796 ComputeQualTypeODRHash(SecondType)) { 9797 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9798 FirstVD->getSourceRange(), VarType) 9799 << FirstName << FirstType; 9800 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9801 SecondVD->getSourceRange(), VarType) 9802 << SecondName << SecondType; 9803 return true; 9804 } 9805 9806 if (!PP.getLangOpts().CPlusPlus) 9807 return false; 9808 9809 const Expr *FirstInit = FirstVD->getInit(); 9810 const Expr *SecondInit = SecondVD->getInit(); 9811 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9812 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9813 FirstVD->getSourceRange(), VarSingleInitializer) 9814 << FirstName << (FirstInit == nullptr) 9815 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9816 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9817 SecondVD->getSourceRange(), VarSingleInitializer) 9818 << SecondName << (SecondInit == nullptr) 9819 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9820 return true; 9821 } 9822 9823 if (FirstInit && SecondInit && 9824 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9825 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9826 FirstVD->getSourceRange(), VarDifferentInitializer) 9827 << FirstName << FirstInit->getSourceRange(); 9828 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9829 SecondVD->getSourceRange(), VarDifferentInitializer) 9830 << SecondName << SecondInit->getSourceRange(); 9831 return true; 9832 } 9833 9834 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9835 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9836 if (FirstIsConstexpr != SecondIsConstexpr) { 9837 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9838 FirstVD->getSourceRange(), VarConstexpr) 9839 << FirstName << FirstIsConstexpr; 9840 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9841 SecondVD->getSourceRange(), VarConstexpr) 9842 << SecondName << SecondIsConstexpr; 9843 return true; 9844 } 9845 return false; 9846 }; 9847 9848 auto DifferenceSelector = [](Decl *D) { 9849 assert(D && "valid Decl required"); 9850 switch (D->getKind()) { 9851 default: 9852 return Other; 9853 case Decl::AccessSpec: 9854 switch (D->getAccess()) { 9855 case AS_public: 9856 return PublicSpecifer; 9857 case AS_private: 9858 return PrivateSpecifer; 9859 case AS_protected: 9860 return ProtectedSpecifer; 9861 case AS_none: 9862 break; 9863 } 9864 llvm_unreachable("Invalid access specifier"); 9865 case Decl::StaticAssert: 9866 return StaticAssert; 9867 case Decl::Field: 9868 return Field; 9869 case Decl::CXXMethod: 9870 case Decl::CXXConstructor: 9871 case Decl::CXXDestructor: 9872 return CXXMethod; 9873 case Decl::TypeAlias: 9874 return TypeAlias; 9875 case Decl::Typedef: 9876 return TypeDef; 9877 case Decl::Var: 9878 return Var; 9879 case Decl::Friend: 9880 return Friend; 9881 case Decl::FunctionTemplate: 9882 return FunctionTemplate; 9883 } 9884 }; 9885 9886 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9887 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9888 RecordDecl *Record, 9889 const DeclContext *DC) { 9890 for (auto *D : Record->decls()) { 9891 if (!ODRHash::isDeclToBeProcessed(D, DC)) 9892 continue; 9893 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9894 } 9895 }; 9896 9897 struct DiffResult { 9898 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9899 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9900 }; 9901 9902 // If there is a diagnoseable difference, FirstDiffType and 9903 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9904 // filled in if not EndOfClass. 9905 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9906 DeclHashes &SecondHashes) { 9907 DiffResult DR; 9908 auto FirstIt = FirstHashes.begin(); 9909 auto SecondIt = SecondHashes.begin(); 9910 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9911 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9912 FirstIt->second == SecondIt->second) { 9913 ++FirstIt; 9914 ++SecondIt; 9915 continue; 9916 } 9917 9918 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9919 DR.SecondDecl = 9920 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9921 9922 DR.FirstDiffType = 9923 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9924 DR.SecondDiffType = 9925 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9926 return DR; 9927 } 9928 return DR; 9929 }; 9930 9931 // Use this to diagnose that an unexpected Decl was encountered 9932 // or no difference was detected. This causes a generic error 9933 // message to be emitted. 9934 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9935 StringRef FirstModule, 9936 NamedDecl *SecondRecord, 9937 StringRef SecondModule) { 9938 Diag(FirstRecord->getLocation(), 9939 diag::err_module_odr_violation_different_definitions) 9940 << FirstRecord << FirstModule.empty() << FirstModule; 9941 9942 if (DR.FirstDecl) { 9943 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9944 << FirstRecord << DR.FirstDecl->getSourceRange(); 9945 } 9946 9947 Diag(SecondRecord->getLocation(), 9948 diag::note_module_odr_violation_different_definitions) 9949 << SecondModule; 9950 9951 if (DR.SecondDecl) { 9952 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9953 << DR.SecondDecl->getSourceRange(); 9954 } 9955 }; 9956 9957 auto DiagnoseODRMismatch = 9958 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9959 NamedDecl *SecondRecord, StringRef SecondModule) { 9960 SourceLocation FirstLoc; 9961 SourceRange FirstRange; 9962 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9963 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9964 FirstLoc = FirstTag->getBraceRange().getEnd(); 9965 } else { 9966 FirstLoc = DR.FirstDecl->getLocation(); 9967 FirstRange = DR.FirstDecl->getSourceRange(); 9968 } 9969 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 9970 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 9971 << DR.FirstDiffType; 9972 9973 SourceLocation SecondLoc; 9974 SourceRange SecondRange; 9975 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 9976 if (DR.SecondDiffType == EndOfClass && SecondTag) { 9977 SecondLoc = SecondTag->getBraceRange().getEnd(); 9978 } else { 9979 SecondLoc = DR.SecondDecl->getLocation(); 9980 SecondRange = DR.SecondDecl->getSourceRange(); 9981 } 9982 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 9983 << SecondModule << SecondRange << DR.SecondDiffType; 9984 }; 9985 9986 // Issue any pending ODR-failure diagnostics. 9987 for (auto &Merge : OdrMergeFailures) { 9988 // If we've already pointed out a specific problem with this class, don't 9989 // bother issuing a general "something's different" diagnostic. 9990 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 9991 continue; 9992 9993 bool Diagnosed = false; 9994 CXXRecordDecl *FirstRecord = Merge.first; 9995 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 9996 for (auto &RecordPair : Merge.second) { 9997 CXXRecordDecl *SecondRecord = RecordPair.first; 9998 // Multiple different declarations got merged together; tell the user 9999 // where they came from. 10000 if (FirstRecord == SecondRecord) 10001 continue; 10002 10003 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 10004 10005 auto *FirstDD = FirstRecord->DefinitionData; 10006 auto *SecondDD = RecordPair.second; 10007 10008 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 10009 10010 // Diagnostics from DefinitionData are emitted here. 10011 if (FirstDD != SecondDD) { 10012 enum ODRDefinitionDataDifference { 10013 NumBases, 10014 NumVBases, 10015 BaseType, 10016 BaseVirtual, 10017 BaseAccess, 10018 }; 10019 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 10020 this](SourceLocation Loc, SourceRange Range, 10021 ODRDefinitionDataDifference DiffType) { 10022 return Diag(Loc, diag::err_module_odr_violation_definition_data) 10023 << FirstRecord << FirstModule.empty() << FirstModule << Range 10024 << DiffType; 10025 }; 10026 auto ODRDiagBaseNote = [&SecondModule, 10027 this](SourceLocation Loc, SourceRange Range, 10028 ODRDefinitionDataDifference DiffType) { 10029 return Diag(Loc, diag::note_module_odr_violation_definition_data) 10030 << SecondModule << Range << DiffType; 10031 }; 10032 10033 unsigned FirstNumBases = FirstDD->NumBases; 10034 unsigned FirstNumVBases = FirstDD->NumVBases; 10035 unsigned SecondNumBases = SecondDD->NumBases; 10036 unsigned SecondNumVBases = SecondDD->NumVBases; 10037 10038 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 10039 unsigned NumBases = DD->NumBases; 10040 if (NumBases == 0) return SourceRange(); 10041 auto bases = DD->bases(); 10042 return SourceRange(bases[0].getBeginLoc(), 10043 bases[NumBases - 1].getEndLoc()); 10044 }; 10045 10046 if (FirstNumBases != SecondNumBases) { 10047 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10048 NumBases) 10049 << FirstNumBases; 10050 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10051 NumBases) 10052 << SecondNumBases; 10053 Diagnosed = true; 10054 break; 10055 } 10056 10057 if (FirstNumVBases != SecondNumVBases) { 10058 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 10059 NumVBases) 10060 << FirstNumVBases; 10061 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 10062 NumVBases) 10063 << SecondNumVBases; 10064 Diagnosed = true; 10065 break; 10066 } 10067 10068 auto FirstBases = FirstDD->bases(); 10069 auto SecondBases = SecondDD->bases(); 10070 unsigned i = 0; 10071 for (i = 0; i < FirstNumBases; ++i) { 10072 auto FirstBase = FirstBases[i]; 10073 auto SecondBase = SecondBases[i]; 10074 if (ComputeQualTypeODRHash(FirstBase.getType()) != 10075 ComputeQualTypeODRHash(SecondBase.getType())) { 10076 ODRDiagBaseError(FirstRecord->getLocation(), 10077 FirstBase.getSourceRange(), BaseType) 10078 << (i + 1) << FirstBase.getType(); 10079 ODRDiagBaseNote(SecondRecord->getLocation(), 10080 SecondBase.getSourceRange(), BaseType) 10081 << (i + 1) << SecondBase.getType(); 10082 break; 10083 } 10084 10085 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10086 ODRDiagBaseError(FirstRecord->getLocation(), 10087 FirstBase.getSourceRange(), BaseVirtual) 10088 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10089 ODRDiagBaseNote(SecondRecord->getLocation(), 10090 SecondBase.getSourceRange(), BaseVirtual) 10091 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10092 break; 10093 } 10094 10095 if (FirstBase.getAccessSpecifierAsWritten() != 10096 SecondBase.getAccessSpecifierAsWritten()) { 10097 ODRDiagBaseError(FirstRecord->getLocation(), 10098 FirstBase.getSourceRange(), BaseAccess) 10099 << (i + 1) << FirstBase.getType() 10100 << (int)FirstBase.getAccessSpecifierAsWritten(); 10101 ODRDiagBaseNote(SecondRecord->getLocation(), 10102 SecondBase.getSourceRange(), BaseAccess) 10103 << (i + 1) << SecondBase.getType() 10104 << (int)SecondBase.getAccessSpecifierAsWritten(); 10105 break; 10106 } 10107 } 10108 10109 if (i != FirstNumBases) { 10110 Diagnosed = true; 10111 break; 10112 } 10113 } 10114 10115 const ClassTemplateDecl *FirstTemplate = 10116 FirstRecord->getDescribedClassTemplate(); 10117 const ClassTemplateDecl *SecondTemplate = 10118 SecondRecord->getDescribedClassTemplate(); 10119 10120 assert(!FirstTemplate == !SecondTemplate && 10121 "Both pointers should be null or non-null"); 10122 10123 enum ODRTemplateDifference { 10124 ParamEmptyName, 10125 ParamName, 10126 ParamSingleDefaultArgument, 10127 ParamDifferentDefaultArgument, 10128 }; 10129 10130 if (FirstTemplate && SecondTemplate) { 10131 DeclHashes FirstTemplateHashes; 10132 DeclHashes SecondTemplateHashes; 10133 10134 auto PopulateTemplateParameterHashs = 10135 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10136 const ClassTemplateDecl *TD) { 10137 for (auto *D : TD->getTemplateParameters()->asArray()) { 10138 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10139 } 10140 }; 10141 10142 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10143 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10144 10145 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10146 "Number of template parameters should be equal."); 10147 10148 auto FirstIt = FirstTemplateHashes.begin(); 10149 auto FirstEnd = FirstTemplateHashes.end(); 10150 auto SecondIt = SecondTemplateHashes.begin(); 10151 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10152 if (FirstIt->second == SecondIt->second) 10153 continue; 10154 10155 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10156 SourceLocation Loc, SourceRange Range, 10157 ODRTemplateDifference DiffType) { 10158 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10159 << FirstRecord << FirstModule.empty() << FirstModule << Range 10160 << DiffType; 10161 }; 10162 auto ODRDiagTemplateNote = [&SecondModule, this]( 10163 SourceLocation Loc, SourceRange Range, 10164 ODRTemplateDifference DiffType) { 10165 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10166 << SecondModule << Range << DiffType; 10167 }; 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 DeclarationName FirstName = FirstDecl->getDeclName(); 10176 DeclarationName SecondName = SecondDecl->getDeclName(); 10177 10178 if (FirstName != SecondName) { 10179 const bool FirstNameEmpty = 10180 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10181 const bool SecondNameEmpty = 10182 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10183 assert((!FirstNameEmpty || !SecondNameEmpty) && 10184 "Both template parameters cannot be unnamed."); 10185 ODRDiagTemplateError(FirstDecl->getLocation(), 10186 FirstDecl->getSourceRange(), 10187 FirstNameEmpty ? ParamEmptyName : ParamName) 10188 << FirstName; 10189 ODRDiagTemplateNote(SecondDecl->getLocation(), 10190 SecondDecl->getSourceRange(), 10191 SecondNameEmpty ? ParamEmptyName : ParamName) 10192 << SecondName; 10193 break; 10194 } 10195 10196 switch (FirstDecl->getKind()) { 10197 default: 10198 llvm_unreachable("Invalid template parameter type."); 10199 case Decl::TemplateTypeParm: { 10200 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10201 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10202 const bool HasFirstDefaultArgument = 10203 FirstParam->hasDefaultArgument() && 10204 !FirstParam->defaultArgumentWasInherited(); 10205 const bool HasSecondDefaultArgument = 10206 SecondParam->hasDefaultArgument() && 10207 !SecondParam->defaultArgumentWasInherited(); 10208 10209 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10210 ODRDiagTemplateError(FirstDecl->getLocation(), 10211 FirstDecl->getSourceRange(), 10212 ParamSingleDefaultArgument) 10213 << HasFirstDefaultArgument; 10214 ODRDiagTemplateNote(SecondDecl->getLocation(), 10215 SecondDecl->getSourceRange(), 10216 ParamSingleDefaultArgument) 10217 << HasSecondDefaultArgument; 10218 break; 10219 } 10220 10221 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10222 "Expecting default arguments."); 10223 10224 ODRDiagTemplateError(FirstDecl->getLocation(), 10225 FirstDecl->getSourceRange(), 10226 ParamDifferentDefaultArgument); 10227 ODRDiagTemplateNote(SecondDecl->getLocation(), 10228 SecondDecl->getSourceRange(), 10229 ParamDifferentDefaultArgument); 10230 10231 break; 10232 } 10233 case Decl::NonTypeTemplateParm: { 10234 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10235 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10236 const bool HasFirstDefaultArgument = 10237 FirstParam->hasDefaultArgument() && 10238 !FirstParam->defaultArgumentWasInherited(); 10239 const bool HasSecondDefaultArgument = 10240 SecondParam->hasDefaultArgument() && 10241 !SecondParam->defaultArgumentWasInherited(); 10242 10243 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10244 ODRDiagTemplateError(FirstDecl->getLocation(), 10245 FirstDecl->getSourceRange(), 10246 ParamSingleDefaultArgument) 10247 << HasFirstDefaultArgument; 10248 ODRDiagTemplateNote(SecondDecl->getLocation(), 10249 SecondDecl->getSourceRange(), 10250 ParamSingleDefaultArgument) 10251 << HasSecondDefaultArgument; 10252 break; 10253 } 10254 10255 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10256 "Expecting default arguments."); 10257 10258 ODRDiagTemplateError(FirstDecl->getLocation(), 10259 FirstDecl->getSourceRange(), 10260 ParamDifferentDefaultArgument); 10261 ODRDiagTemplateNote(SecondDecl->getLocation(), 10262 SecondDecl->getSourceRange(), 10263 ParamDifferentDefaultArgument); 10264 10265 break; 10266 } 10267 case Decl::TemplateTemplateParm: { 10268 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10269 const auto *SecondParam = 10270 cast<TemplateTemplateParmDecl>(SecondDecl); 10271 const bool HasFirstDefaultArgument = 10272 FirstParam->hasDefaultArgument() && 10273 !FirstParam->defaultArgumentWasInherited(); 10274 const bool HasSecondDefaultArgument = 10275 SecondParam->hasDefaultArgument() && 10276 !SecondParam->defaultArgumentWasInherited(); 10277 10278 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10279 ODRDiagTemplateError(FirstDecl->getLocation(), 10280 FirstDecl->getSourceRange(), 10281 ParamSingleDefaultArgument) 10282 << HasFirstDefaultArgument; 10283 ODRDiagTemplateNote(SecondDecl->getLocation(), 10284 SecondDecl->getSourceRange(), 10285 ParamSingleDefaultArgument) 10286 << HasSecondDefaultArgument; 10287 break; 10288 } 10289 10290 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10291 "Expecting default arguments."); 10292 10293 ODRDiagTemplateError(FirstDecl->getLocation(), 10294 FirstDecl->getSourceRange(), 10295 ParamDifferentDefaultArgument); 10296 ODRDiagTemplateNote(SecondDecl->getLocation(), 10297 SecondDecl->getSourceRange(), 10298 ParamDifferentDefaultArgument); 10299 10300 break; 10301 } 10302 } 10303 10304 break; 10305 } 10306 10307 if (FirstIt != FirstEnd) { 10308 Diagnosed = true; 10309 break; 10310 } 10311 } 10312 10313 DeclHashes FirstHashes; 10314 DeclHashes SecondHashes; 10315 const DeclContext *DC = FirstRecord; 10316 PopulateHashes(FirstHashes, FirstRecord, DC); 10317 PopulateHashes(SecondHashes, SecondRecord, DC); 10318 10319 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10320 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10321 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10322 Decl *FirstDecl = DR.FirstDecl; 10323 Decl *SecondDecl = DR.SecondDecl; 10324 10325 if (FirstDiffType == Other || SecondDiffType == Other) { 10326 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10327 SecondModule); 10328 Diagnosed = true; 10329 break; 10330 } 10331 10332 if (FirstDiffType != SecondDiffType) { 10333 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10334 SecondModule); 10335 Diagnosed = true; 10336 break; 10337 } 10338 10339 assert(FirstDiffType == SecondDiffType); 10340 10341 switch (FirstDiffType) { 10342 case Other: 10343 case EndOfClass: 10344 case PublicSpecifer: 10345 case PrivateSpecifer: 10346 case ProtectedSpecifer: 10347 llvm_unreachable("Invalid diff type"); 10348 10349 case StaticAssert: { 10350 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10351 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10352 10353 Expr *FirstExpr = FirstSA->getAssertExpr(); 10354 Expr *SecondExpr = SecondSA->getAssertExpr(); 10355 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10356 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10357 if (FirstODRHash != SecondODRHash) { 10358 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10359 FirstExpr->getSourceRange(), StaticAssertCondition); 10360 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10361 SecondExpr->getSourceRange(), StaticAssertCondition); 10362 Diagnosed = true; 10363 break; 10364 } 10365 10366 StringLiteral *FirstStr = FirstSA->getMessage(); 10367 StringLiteral *SecondStr = SecondSA->getMessage(); 10368 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10369 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10370 SourceLocation FirstLoc, SecondLoc; 10371 SourceRange FirstRange, SecondRange; 10372 if (FirstStr) { 10373 FirstLoc = FirstStr->getBeginLoc(); 10374 FirstRange = FirstStr->getSourceRange(); 10375 } else { 10376 FirstLoc = FirstSA->getBeginLoc(); 10377 FirstRange = FirstSA->getSourceRange(); 10378 } 10379 if (SecondStr) { 10380 SecondLoc = SecondStr->getBeginLoc(); 10381 SecondRange = SecondStr->getSourceRange(); 10382 } else { 10383 SecondLoc = SecondSA->getBeginLoc(); 10384 SecondRange = SecondSA->getSourceRange(); 10385 } 10386 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10387 StaticAssertOnlyMessage) 10388 << (FirstStr == nullptr); 10389 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10390 StaticAssertOnlyMessage) 10391 << (SecondStr == nullptr); 10392 Diagnosed = true; 10393 break; 10394 } 10395 10396 if (FirstStr && SecondStr && 10397 FirstStr->getString() != SecondStr->getString()) { 10398 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10399 FirstStr->getSourceRange(), StaticAssertMessage); 10400 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10401 SecondStr->getSourceRange(), StaticAssertMessage); 10402 Diagnosed = true; 10403 break; 10404 } 10405 break; 10406 } 10407 case Field: { 10408 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10409 cast<FieldDecl>(FirstDecl), 10410 cast<FieldDecl>(SecondDecl)); 10411 break; 10412 } 10413 case CXXMethod: { 10414 enum { 10415 DiagMethod, 10416 DiagConstructor, 10417 DiagDestructor, 10418 } FirstMethodType, 10419 SecondMethodType; 10420 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10421 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10422 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10423 return DiagMethod; 10424 }; 10425 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10426 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10427 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10428 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10429 auto FirstName = FirstMethod->getDeclName(); 10430 auto SecondName = SecondMethod->getDeclName(); 10431 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10432 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10433 FirstMethod->getSourceRange(), MethodName) 10434 << FirstMethodType << FirstName; 10435 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10436 SecondMethod->getSourceRange(), MethodName) 10437 << SecondMethodType << SecondName; 10438 10439 Diagnosed = true; 10440 break; 10441 } 10442 10443 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10444 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10445 if (FirstDeleted != SecondDeleted) { 10446 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10447 FirstMethod->getSourceRange(), MethodDeleted) 10448 << FirstMethodType << FirstName << FirstDeleted; 10449 10450 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10451 SecondMethod->getSourceRange(), MethodDeleted) 10452 << SecondMethodType << SecondName << SecondDeleted; 10453 Diagnosed = true; 10454 break; 10455 } 10456 10457 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10458 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10459 if (FirstDefaulted != SecondDefaulted) { 10460 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10461 FirstMethod->getSourceRange(), MethodDefaulted) 10462 << FirstMethodType << FirstName << FirstDefaulted; 10463 10464 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10465 SecondMethod->getSourceRange(), MethodDefaulted) 10466 << SecondMethodType << SecondName << SecondDefaulted; 10467 Diagnosed = true; 10468 break; 10469 } 10470 10471 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10472 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10473 const bool FirstPure = FirstMethod->isPure(); 10474 const bool SecondPure = SecondMethod->isPure(); 10475 if ((FirstVirtual || SecondVirtual) && 10476 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10477 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10478 FirstMethod->getSourceRange(), MethodVirtual) 10479 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10480 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10481 SecondMethod->getSourceRange(), MethodVirtual) 10482 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10483 Diagnosed = true; 10484 break; 10485 } 10486 10487 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10488 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10489 // class needs to be checked instead. 10490 const auto FirstStorage = FirstMethod->getStorageClass(); 10491 const auto SecondStorage = SecondMethod->getStorageClass(); 10492 const bool FirstStatic = FirstStorage == SC_Static; 10493 const bool SecondStatic = SecondStorage == SC_Static; 10494 if (FirstStatic != SecondStatic) { 10495 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10496 FirstMethod->getSourceRange(), MethodStatic) 10497 << FirstMethodType << FirstName << FirstStatic; 10498 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10499 SecondMethod->getSourceRange(), MethodStatic) 10500 << SecondMethodType << SecondName << SecondStatic; 10501 Diagnosed = true; 10502 break; 10503 } 10504 10505 const bool FirstVolatile = FirstMethod->isVolatile(); 10506 const bool SecondVolatile = SecondMethod->isVolatile(); 10507 if (FirstVolatile != SecondVolatile) { 10508 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10509 FirstMethod->getSourceRange(), MethodVolatile) 10510 << FirstMethodType << FirstName << FirstVolatile; 10511 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10512 SecondMethod->getSourceRange(), MethodVolatile) 10513 << SecondMethodType << SecondName << SecondVolatile; 10514 Diagnosed = true; 10515 break; 10516 } 10517 10518 const bool FirstConst = FirstMethod->isConst(); 10519 const bool SecondConst = SecondMethod->isConst(); 10520 if (FirstConst != SecondConst) { 10521 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10522 FirstMethod->getSourceRange(), MethodConst) 10523 << FirstMethodType << FirstName << FirstConst; 10524 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10525 SecondMethod->getSourceRange(), MethodConst) 10526 << SecondMethodType << SecondName << SecondConst; 10527 Diagnosed = true; 10528 break; 10529 } 10530 10531 const bool FirstInline = FirstMethod->isInlineSpecified(); 10532 const bool SecondInline = SecondMethod->isInlineSpecified(); 10533 if (FirstInline != SecondInline) { 10534 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10535 FirstMethod->getSourceRange(), MethodInline) 10536 << FirstMethodType << FirstName << FirstInline; 10537 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10538 SecondMethod->getSourceRange(), MethodInline) 10539 << SecondMethodType << SecondName << SecondInline; 10540 Diagnosed = true; 10541 break; 10542 } 10543 10544 const unsigned FirstNumParameters = FirstMethod->param_size(); 10545 const unsigned SecondNumParameters = SecondMethod->param_size(); 10546 if (FirstNumParameters != SecondNumParameters) { 10547 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10548 FirstMethod->getSourceRange(), 10549 MethodNumberParameters) 10550 << FirstMethodType << FirstName << FirstNumParameters; 10551 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10552 SecondMethod->getSourceRange(), 10553 MethodNumberParameters) 10554 << SecondMethodType << SecondName << SecondNumParameters; 10555 Diagnosed = true; 10556 break; 10557 } 10558 10559 // Need this status boolean to know when break out of the switch. 10560 bool ParameterMismatch = false; 10561 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10562 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10563 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10564 10565 QualType FirstParamType = FirstParam->getType(); 10566 QualType SecondParamType = SecondParam->getType(); 10567 if (FirstParamType != SecondParamType && 10568 ComputeQualTypeODRHash(FirstParamType) != 10569 ComputeQualTypeODRHash(SecondParamType)) { 10570 if (const DecayedType *ParamDecayedType = 10571 FirstParamType->getAs<DecayedType>()) { 10572 ODRDiagDeclError( 10573 FirstRecord, FirstModule, FirstMethod->getLocation(), 10574 FirstMethod->getSourceRange(), MethodParameterType) 10575 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10576 << true << ParamDecayedType->getOriginalType(); 10577 } else { 10578 ODRDiagDeclError( 10579 FirstRecord, FirstModule, FirstMethod->getLocation(), 10580 FirstMethod->getSourceRange(), MethodParameterType) 10581 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10582 << false; 10583 } 10584 10585 if (const DecayedType *ParamDecayedType = 10586 SecondParamType->getAs<DecayedType>()) { 10587 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10588 SecondMethod->getSourceRange(), 10589 MethodParameterType) 10590 << SecondMethodType << SecondName << (I + 1) 10591 << SecondParamType << true 10592 << ParamDecayedType->getOriginalType(); 10593 } else { 10594 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10595 SecondMethod->getSourceRange(), 10596 MethodParameterType) 10597 << SecondMethodType << SecondName << (I + 1) 10598 << SecondParamType << false; 10599 } 10600 ParameterMismatch = true; 10601 break; 10602 } 10603 10604 DeclarationName FirstParamName = FirstParam->getDeclName(); 10605 DeclarationName SecondParamName = SecondParam->getDeclName(); 10606 if (FirstParamName != SecondParamName) { 10607 ODRDiagDeclError(FirstRecord, FirstModule, 10608 FirstMethod->getLocation(), 10609 FirstMethod->getSourceRange(), MethodParameterName) 10610 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10611 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10612 SecondMethod->getSourceRange(), MethodParameterName) 10613 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10614 ParameterMismatch = true; 10615 break; 10616 } 10617 10618 const Expr *FirstInit = FirstParam->getInit(); 10619 const Expr *SecondInit = SecondParam->getInit(); 10620 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10621 ODRDiagDeclError(FirstRecord, FirstModule, 10622 FirstMethod->getLocation(), 10623 FirstMethod->getSourceRange(), 10624 MethodParameterSingleDefaultArgument) 10625 << FirstMethodType << FirstName << (I + 1) 10626 << (FirstInit == nullptr) 10627 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10628 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10629 SecondMethod->getSourceRange(), 10630 MethodParameterSingleDefaultArgument) 10631 << SecondMethodType << SecondName << (I + 1) 10632 << (SecondInit == nullptr) 10633 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10634 ParameterMismatch = true; 10635 break; 10636 } 10637 10638 if (FirstInit && SecondInit && 10639 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10640 ODRDiagDeclError(FirstRecord, FirstModule, 10641 FirstMethod->getLocation(), 10642 FirstMethod->getSourceRange(), 10643 MethodParameterDifferentDefaultArgument) 10644 << FirstMethodType << FirstName << (I + 1) 10645 << FirstInit->getSourceRange(); 10646 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10647 SecondMethod->getSourceRange(), 10648 MethodParameterDifferentDefaultArgument) 10649 << SecondMethodType << SecondName << (I + 1) 10650 << SecondInit->getSourceRange(); 10651 ParameterMismatch = true; 10652 break; 10653 10654 } 10655 } 10656 10657 if (ParameterMismatch) { 10658 Diagnosed = true; 10659 break; 10660 } 10661 10662 const auto *FirstTemplateArgs = 10663 FirstMethod->getTemplateSpecializationArgs(); 10664 const auto *SecondTemplateArgs = 10665 SecondMethod->getTemplateSpecializationArgs(); 10666 10667 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10668 (!FirstTemplateArgs && SecondTemplateArgs)) { 10669 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10670 FirstMethod->getSourceRange(), 10671 MethodNoTemplateArguments) 10672 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10673 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10674 SecondMethod->getSourceRange(), 10675 MethodNoTemplateArguments) 10676 << SecondMethodType << SecondName 10677 << (SecondTemplateArgs != nullptr); 10678 10679 Diagnosed = true; 10680 break; 10681 } 10682 10683 if (FirstTemplateArgs && SecondTemplateArgs) { 10684 // Remove pack expansions from argument list. 10685 auto ExpandTemplateArgumentList = 10686 [](const TemplateArgumentList *TAL) { 10687 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10688 for (const TemplateArgument &TA : TAL->asArray()) { 10689 if (TA.getKind() != TemplateArgument::Pack) { 10690 ExpandedList.push_back(&TA); 10691 continue; 10692 } 10693 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10694 ExpandedList.push_back(&PackTA); 10695 } 10696 } 10697 return ExpandedList; 10698 }; 10699 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10700 ExpandTemplateArgumentList(FirstTemplateArgs); 10701 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10702 ExpandTemplateArgumentList(SecondTemplateArgs); 10703 10704 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10705 ODRDiagDeclError(FirstRecord, FirstModule, 10706 FirstMethod->getLocation(), 10707 FirstMethod->getSourceRange(), 10708 MethodDifferentNumberTemplateArguments) 10709 << FirstMethodType << FirstName 10710 << (unsigned)FirstExpandedList.size(); 10711 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10712 SecondMethod->getSourceRange(), 10713 MethodDifferentNumberTemplateArguments) 10714 << SecondMethodType << SecondName 10715 << (unsigned)SecondExpandedList.size(); 10716 10717 Diagnosed = true; 10718 break; 10719 } 10720 10721 bool TemplateArgumentMismatch = false; 10722 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10723 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10724 &SecondTA = *SecondExpandedList[i]; 10725 if (ComputeTemplateArgumentODRHash(FirstTA) == 10726 ComputeTemplateArgumentODRHash(SecondTA)) { 10727 continue; 10728 } 10729 10730 ODRDiagDeclError( 10731 FirstRecord, FirstModule, FirstMethod->getLocation(), 10732 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10733 << FirstMethodType << FirstName << FirstTA << i + 1; 10734 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10735 SecondMethod->getSourceRange(), 10736 MethodDifferentTemplateArgument) 10737 << SecondMethodType << SecondName << SecondTA << i + 1; 10738 10739 TemplateArgumentMismatch = true; 10740 break; 10741 } 10742 10743 if (TemplateArgumentMismatch) { 10744 Diagnosed = true; 10745 break; 10746 } 10747 } 10748 10749 // Compute the hash of the method as if it has no body. 10750 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10751 Hash.clear(); 10752 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10753 return Hash.CalculateHash(); 10754 }; 10755 10756 // Compare the hash generated to the hash stored. A difference means 10757 // that a body was present in the original source. Due to merging, 10758 // the stardard way of detecting a body will not work. 10759 const bool HasFirstBody = 10760 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10761 const bool HasSecondBody = 10762 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10763 10764 if (HasFirstBody != HasSecondBody) { 10765 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10766 FirstMethod->getSourceRange(), MethodSingleBody) 10767 << FirstMethodType << FirstName << HasFirstBody; 10768 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10769 SecondMethod->getSourceRange(), MethodSingleBody) 10770 << SecondMethodType << SecondName << HasSecondBody; 10771 Diagnosed = true; 10772 break; 10773 } 10774 10775 if (HasFirstBody && HasSecondBody) { 10776 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10777 FirstMethod->getSourceRange(), MethodDifferentBody) 10778 << FirstMethodType << FirstName; 10779 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10780 SecondMethod->getSourceRange(), MethodDifferentBody) 10781 << SecondMethodType << SecondName; 10782 Diagnosed = true; 10783 break; 10784 } 10785 10786 break; 10787 } 10788 case TypeAlias: 10789 case TypeDef: { 10790 Diagnosed = ODRDiagTypeDefOrAlias( 10791 FirstRecord, FirstModule, SecondModule, 10792 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10793 FirstDiffType == TypeAlias); 10794 break; 10795 } 10796 case Var: { 10797 Diagnosed = 10798 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10799 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10800 break; 10801 } 10802 case Friend: { 10803 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10804 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10805 10806 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10807 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10808 10809 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10810 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10811 10812 if (FirstND && SecondND) { 10813 ODRDiagDeclError(FirstRecord, FirstModule, 10814 FirstFriend->getFriendLoc(), 10815 FirstFriend->getSourceRange(), FriendFunction) 10816 << FirstND; 10817 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10818 SecondFriend->getSourceRange(), FriendFunction) 10819 << SecondND; 10820 10821 Diagnosed = true; 10822 break; 10823 } 10824 10825 if (FirstTSI && SecondTSI) { 10826 QualType FirstFriendType = FirstTSI->getType(); 10827 QualType SecondFriendType = SecondTSI->getType(); 10828 assert(ComputeQualTypeODRHash(FirstFriendType) != 10829 ComputeQualTypeODRHash(SecondFriendType)); 10830 ODRDiagDeclError(FirstRecord, FirstModule, 10831 FirstFriend->getFriendLoc(), 10832 FirstFriend->getSourceRange(), FriendType) 10833 << FirstFriendType; 10834 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10835 SecondFriend->getSourceRange(), FriendType) 10836 << SecondFriendType; 10837 Diagnosed = true; 10838 break; 10839 } 10840 10841 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10842 FirstFriend->getSourceRange(), FriendTypeFunction) 10843 << (FirstTSI == nullptr); 10844 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10845 SecondFriend->getSourceRange(), FriendTypeFunction) 10846 << (SecondTSI == nullptr); 10847 10848 Diagnosed = true; 10849 break; 10850 } 10851 case FunctionTemplate: { 10852 FunctionTemplateDecl *FirstTemplate = 10853 cast<FunctionTemplateDecl>(FirstDecl); 10854 FunctionTemplateDecl *SecondTemplate = 10855 cast<FunctionTemplateDecl>(SecondDecl); 10856 10857 TemplateParameterList *FirstTPL = 10858 FirstTemplate->getTemplateParameters(); 10859 TemplateParameterList *SecondTPL = 10860 SecondTemplate->getTemplateParameters(); 10861 10862 if (FirstTPL->size() != SecondTPL->size()) { 10863 ODRDiagDeclError(FirstRecord, FirstModule, 10864 FirstTemplate->getLocation(), 10865 FirstTemplate->getSourceRange(), 10866 FunctionTemplateDifferentNumberParameters) 10867 << FirstTemplate << FirstTPL->size(); 10868 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10869 SecondTemplate->getSourceRange(), 10870 FunctionTemplateDifferentNumberParameters) 10871 << SecondTemplate << SecondTPL->size(); 10872 10873 Diagnosed = true; 10874 break; 10875 } 10876 10877 bool ParameterMismatch = false; 10878 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10879 NamedDecl *FirstParam = FirstTPL->getParam(i); 10880 NamedDecl *SecondParam = SecondTPL->getParam(i); 10881 10882 if (FirstParam->getKind() != SecondParam->getKind()) { 10883 enum { 10884 TemplateTypeParameter, 10885 NonTypeTemplateParameter, 10886 TemplateTemplateParameter, 10887 }; 10888 auto GetParamType = [](NamedDecl *D) { 10889 switch (D->getKind()) { 10890 default: 10891 llvm_unreachable("Unexpected template parameter type"); 10892 case Decl::TemplateTypeParm: 10893 return TemplateTypeParameter; 10894 case Decl::NonTypeTemplateParm: 10895 return NonTypeTemplateParameter; 10896 case Decl::TemplateTemplateParm: 10897 return TemplateTemplateParameter; 10898 } 10899 }; 10900 10901 ODRDiagDeclError(FirstRecord, FirstModule, 10902 FirstTemplate->getLocation(), 10903 FirstTemplate->getSourceRange(), 10904 FunctionTemplateParameterDifferentKind) 10905 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10906 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10907 SecondTemplate->getSourceRange(), 10908 FunctionTemplateParameterDifferentKind) 10909 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10910 10911 ParameterMismatch = true; 10912 break; 10913 } 10914 10915 if (FirstParam->getName() != SecondParam->getName()) { 10916 ODRDiagDeclError( 10917 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10918 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10919 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10920 << FirstParam; 10921 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10922 SecondTemplate->getSourceRange(), 10923 FunctionTemplateParameterName) 10924 << SecondTemplate << (i + 1) 10925 << (bool)SecondParam->getIdentifier() << SecondParam; 10926 ParameterMismatch = true; 10927 break; 10928 } 10929 10930 if (isa<TemplateTypeParmDecl>(FirstParam) && 10931 isa<TemplateTypeParmDecl>(SecondParam)) { 10932 TemplateTypeParmDecl *FirstTTPD = 10933 cast<TemplateTypeParmDecl>(FirstParam); 10934 TemplateTypeParmDecl *SecondTTPD = 10935 cast<TemplateTypeParmDecl>(SecondParam); 10936 bool HasFirstDefaultArgument = 10937 FirstTTPD->hasDefaultArgument() && 10938 !FirstTTPD->defaultArgumentWasInherited(); 10939 bool HasSecondDefaultArgument = 10940 SecondTTPD->hasDefaultArgument() && 10941 !SecondTTPD->defaultArgumentWasInherited(); 10942 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10943 ODRDiagDeclError(FirstRecord, FirstModule, 10944 FirstTemplate->getLocation(), 10945 FirstTemplate->getSourceRange(), 10946 FunctionTemplateParameterSingleDefaultArgument) 10947 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10948 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10949 SecondTemplate->getSourceRange(), 10950 FunctionTemplateParameterSingleDefaultArgument) 10951 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10952 ParameterMismatch = true; 10953 break; 10954 } 10955 10956 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10957 QualType FirstType = FirstTTPD->getDefaultArgument(); 10958 QualType SecondType = SecondTTPD->getDefaultArgument(); 10959 if (ComputeQualTypeODRHash(FirstType) != 10960 ComputeQualTypeODRHash(SecondType)) { 10961 ODRDiagDeclError( 10962 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10963 FirstTemplate->getSourceRange(), 10964 FunctionTemplateParameterDifferentDefaultArgument) 10965 << FirstTemplate << (i + 1) << FirstType; 10966 ODRDiagDeclNote( 10967 SecondModule, SecondTemplate->getLocation(), 10968 SecondTemplate->getSourceRange(), 10969 FunctionTemplateParameterDifferentDefaultArgument) 10970 << SecondTemplate << (i + 1) << SecondType; 10971 ParameterMismatch = true; 10972 break; 10973 } 10974 } 10975 10976 if (FirstTTPD->isParameterPack() != 10977 SecondTTPD->isParameterPack()) { 10978 ODRDiagDeclError(FirstRecord, FirstModule, 10979 FirstTemplate->getLocation(), 10980 FirstTemplate->getSourceRange(), 10981 FunctionTemplatePackParameter) 10982 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10983 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10984 SecondTemplate->getSourceRange(), 10985 FunctionTemplatePackParameter) 10986 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10987 ParameterMismatch = true; 10988 break; 10989 } 10990 } 10991 10992 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10993 isa<TemplateTemplateParmDecl>(SecondParam)) { 10994 TemplateTemplateParmDecl *FirstTTPD = 10995 cast<TemplateTemplateParmDecl>(FirstParam); 10996 TemplateTemplateParmDecl *SecondTTPD = 10997 cast<TemplateTemplateParmDecl>(SecondParam); 10998 10999 TemplateParameterList *FirstTPL = 11000 FirstTTPD->getTemplateParameters(); 11001 TemplateParameterList *SecondTPL = 11002 SecondTTPD->getTemplateParameters(); 11003 11004 if (ComputeTemplateParameterListODRHash(FirstTPL) != 11005 ComputeTemplateParameterListODRHash(SecondTPL)) { 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 FirstTTPD->hasDefaultArgument() && 11021 !FirstTTPD->defaultArgumentWasInherited(); 11022 bool HasSecondDefaultArgument = 11023 SecondTTPD->hasDefaultArgument() && 11024 !SecondTTPD->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 TemplateArgument FirstTA = 11041 FirstTTPD->getDefaultArgument().getArgument(); 11042 TemplateArgument SecondTA = 11043 SecondTTPD->getDefaultArgument().getArgument(); 11044 if (ComputeTemplateArgumentODRHash(FirstTA) != 11045 ComputeTemplateArgumentODRHash(SecondTA)) { 11046 ODRDiagDeclError( 11047 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11048 FirstTemplate->getSourceRange(), 11049 FunctionTemplateParameterDifferentDefaultArgument) 11050 << FirstTemplate << (i + 1) << FirstTA; 11051 ODRDiagDeclNote( 11052 SecondModule, SecondTemplate->getLocation(), 11053 SecondTemplate->getSourceRange(), 11054 FunctionTemplateParameterDifferentDefaultArgument) 11055 << SecondTemplate << (i + 1) << SecondTA; 11056 ParameterMismatch = true; 11057 break; 11058 } 11059 } 11060 11061 if (FirstTTPD->isParameterPack() != 11062 SecondTTPD->isParameterPack()) { 11063 ODRDiagDeclError(FirstRecord, FirstModule, 11064 FirstTemplate->getLocation(), 11065 FirstTemplate->getSourceRange(), 11066 FunctionTemplatePackParameter) 11067 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 11068 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11069 SecondTemplate->getSourceRange(), 11070 FunctionTemplatePackParameter) 11071 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 11072 ParameterMismatch = true; 11073 break; 11074 } 11075 } 11076 11077 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11078 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11079 NonTypeTemplateParmDecl *FirstNTTPD = 11080 cast<NonTypeTemplateParmDecl>(FirstParam); 11081 NonTypeTemplateParmDecl *SecondNTTPD = 11082 cast<NonTypeTemplateParmDecl>(SecondParam); 11083 11084 QualType FirstType = FirstNTTPD->getType(); 11085 QualType SecondType = SecondNTTPD->getType(); 11086 if (ComputeQualTypeODRHash(FirstType) != 11087 ComputeQualTypeODRHash(SecondType)) { 11088 ODRDiagDeclError(FirstRecord, FirstModule, 11089 FirstTemplate->getLocation(), 11090 FirstTemplate->getSourceRange(), 11091 FunctionTemplateParameterDifferentType) 11092 << FirstTemplate << (i + 1); 11093 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11094 SecondTemplate->getSourceRange(), 11095 FunctionTemplateParameterDifferentType) 11096 << SecondTemplate << (i + 1); 11097 ParameterMismatch = true; 11098 break; 11099 } 11100 11101 bool HasFirstDefaultArgument = 11102 FirstNTTPD->hasDefaultArgument() && 11103 !FirstNTTPD->defaultArgumentWasInherited(); 11104 bool HasSecondDefaultArgument = 11105 SecondNTTPD->hasDefaultArgument() && 11106 !SecondNTTPD->defaultArgumentWasInherited(); 11107 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11108 ODRDiagDeclError(FirstRecord, FirstModule, 11109 FirstTemplate->getLocation(), 11110 FirstTemplate->getSourceRange(), 11111 FunctionTemplateParameterSingleDefaultArgument) 11112 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11113 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11114 SecondTemplate->getSourceRange(), 11115 FunctionTemplateParameterSingleDefaultArgument) 11116 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11117 ParameterMismatch = true; 11118 break; 11119 } 11120 11121 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11122 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11123 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11124 if (ComputeODRHash(FirstDefaultArgument) != 11125 ComputeODRHash(SecondDefaultArgument)) { 11126 ODRDiagDeclError( 11127 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11128 FirstTemplate->getSourceRange(), 11129 FunctionTemplateParameterDifferentDefaultArgument) 11130 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11131 ODRDiagDeclNote( 11132 SecondModule, SecondTemplate->getLocation(), 11133 SecondTemplate->getSourceRange(), 11134 FunctionTemplateParameterDifferentDefaultArgument) 11135 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11136 ParameterMismatch = true; 11137 break; 11138 } 11139 } 11140 11141 if (FirstNTTPD->isParameterPack() != 11142 SecondNTTPD->isParameterPack()) { 11143 ODRDiagDeclError(FirstRecord, FirstModule, 11144 FirstTemplate->getLocation(), 11145 FirstTemplate->getSourceRange(), 11146 FunctionTemplatePackParameter) 11147 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11148 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11149 SecondTemplate->getSourceRange(), 11150 FunctionTemplatePackParameter) 11151 << SecondTemplate << (i + 1) 11152 << SecondNTTPD->isParameterPack(); 11153 ParameterMismatch = true; 11154 break; 11155 } 11156 } 11157 } 11158 11159 if (ParameterMismatch) { 11160 Diagnosed = true; 11161 break; 11162 } 11163 11164 break; 11165 } 11166 } 11167 11168 if (Diagnosed) 11169 continue; 11170 11171 Diag(FirstDecl->getLocation(), 11172 diag::err_module_odr_violation_mismatch_decl_unknown) 11173 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11174 << FirstDecl->getSourceRange(); 11175 Diag(SecondDecl->getLocation(), 11176 diag::note_module_odr_violation_mismatch_decl_unknown) 11177 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11178 Diagnosed = true; 11179 } 11180 11181 if (!Diagnosed) { 11182 // All definitions are updates to the same declaration. This happens if a 11183 // module instantiates the declaration of a class template specialization 11184 // and two or more other modules instantiate its definition. 11185 // 11186 // FIXME: Indicate which modules had instantiations of this definition. 11187 // FIXME: How can this even happen? 11188 Diag(Merge.first->getLocation(), 11189 diag::err_module_odr_violation_different_instantiations) 11190 << Merge.first; 11191 } 11192 } 11193 11194 // Issue ODR failures diagnostics for functions. 11195 for (auto &Merge : FunctionOdrMergeFailures) { 11196 enum ODRFunctionDifference { 11197 ReturnType, 11198 ParameterName, 11199 ParameterType, 11200 ParameterSingleDefaultArgument, 11201 ParameterDifferentDefaultArgument, 11202 FunctionBody, 11203 }; 11204 11205 FunctionDecl *FirstFunction = Merge.first; 11206 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11207 11208 bool Diagnosed = false; 11209 for (auto &SecondFunction : Merge.second) { 11210 11211 if (FirstFunction == SecondFunction) 11212 continue; 11213 11214 std::string SecondModule = 11215 getOwningModuleNameForDiagnostic(SecondFunction); 11216 11217 auto ODRDiagError = [FirstFunction, &FirstModule, 11218 this](SourceLocation Loc, SourceRange Range, 11219 ODRFunctionDifference DiffType) { 11220 return Diag(Loc, diag::err_module_odr_violation_function) 11221 << FirstFunction << FirstModule.empty() << FirstModule << Range 11222 << DiffType; 11223 }; 11224 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11225 SourceRange Range, 11226 ODRFunctionDifference DiffType) { 11227 return Diag(Loc, diag::note_module_odr_violation_function) 11228 << SecondModule << Range << DiffType; 11229 }; 11230 11231 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11232 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11233 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11234 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11235 << FirstFunction->getReturnType(); 11236 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11237 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11238 << SecondFunction->getReturnType(); 11239 Diagnosed = true; 11240 break; 11241 } 11242 11243 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11244 "Merged functions with different number of parameters"); 11245 11246 auto ParamSize = FirstFunction->param_size(); 11247 bool ParameterMismatch = false; 11248 for (unsigned I = 0; I < ParamSize; ++I) { 11249 auto *FirstParam = FirstFunction->getParamDecl(I); 11250 auto *SecondParam = SecondFunction->getParamDecl(I); 11251 11252 assert(getContext().hasSameType(FirstParam->getType(), 11253 SecondParam->getType()) && 11254 "Merged function has different parameter types."); 11255 11256 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11257 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11258 ParameterName) 11259 << I + 1 << FirstParam->getDeclName(); 11260 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11261 ParameterName) 11262 << I + 1 << SecondParam->getDeclName(); 11263 ParameterMismatch = true; 11264 break; 11265 }; 11266 11267 QualType FirstParamType = FirstParam->getType(); 11268 QualType SecondParamType = SecondParam->getType(); 11269 if (FirstParamType != SecondParamType && 11270 ComputeQualTypeODRHash(FirstParamType) != 11271 ComputeQualTypeODRHash(SecondParamType)) { 11272 if (const DecayedType *ParamDecayedType = 11273 FirstParamType->getAs<DecayedType>()) { 11274 ODRDiagError(FirstParam->getLocation(), 11275 FirstParam->getSourceRange(), ParameterType) 11276 << (I + 1) << FirstParamType << true 11277 << ParamDecayedType->getOriginalType(); 11278 } else { 11279 ODRDiagError(FirstParam->getLocation(), 11280 FirstParam->getSourceRange(), ParameterType) 11281 << (I + 1) << FirstParamType << false; 11282 } 11283 11284 if (const DecayedType *ParamDecayedType = 11285 SecondParamType->getAs<DecayedType>()) { 11286 ODRDiagNote(SecondParam->getLocation(), 11287 SecondParam->getSourceRange(), ParameterType) 11288 << (I + 1) << SecondParamType << true 11289 << ParamDecayedType->getOriginalType(); 11290 } else { 11291 ODRDiagNote(SecondParam->getLocation(), 11292 SecondParam->getSourceRange(), ParameterType) 11293 << (I + 1) << SecondParamType << false; 11294 } 11295 ParameterMismatch = true; 11296 break; 11297 } 11298 11299 const Expr *FirstInit = FirstParam->getInit(); 11300 const Expr *SecondInit = SecondParam->getInit(); 11301 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11302 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11303 ParameterSingleDefaultArgument) 11304 << (I + 1) << (FirstInit == nullptr) 11305 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11306 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11307 ParameterSingleDefaultArgument) 11308 << (I + 1) << (SecondInit == nullptr) 11309 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11310 ParameterMismatch = true; 11311 break; 11312 } 11313 11314 if (FirstInit && SecondInit && 11315 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11316 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11317 ParameterDifferentDefaultArgument) 11318 << (I + 1) << FirstInit->getSourceRange(); 11319 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11320 ParameterDifferentDefaultArgument) 11321 << (I + 1) << SecondInit->getSourceRange(); 11322 ParameterMismatch = true; 11323 break; 11324 } 11325 11326 assert(ComputeSubDeclODRHash(FirstParam) == 11327 ComputeSubDeclODRHash(SecondParam) && 11328 "Undiagnosed parameter difference."); 11329 } 11330 11331 if (ParameterMismatch) { 11332 Diagnosed = true; 11333 break; 11334 } 11335 11336 // If no error has been generated before now, assume the problem is in 11337 // the body and generate a message. 11338 ODRDiagError(FirstFunction->getLocation(), 11339 FirstFunction->getSourceRange(), FunctionBody); 11340 ODRDiagNote(SecondFunction->getLocation(), 11341 SecondFunction->getSourceRange(), FunctionBody); 11342 Diagnosed = true; 11343 break; 11344 } 11345 (void)Diagnosed; 11346 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11347 } 11348 11349 // Issue ODR failures diagnostics for enums. 11350 for (auto &Merge : EnumOdrMergeFailures) { 11351 enum ODREnumDifference { 11352 SingleScopedEnum, 11353 EnumTagKeywordMismatch, 11354 SingleSpecifiedType, 11355 DifferentSpecifiedTypes, 11356 DifferentNumberEnumConstants, 11357 EnumConstantName, 11358 EnumConstantSingleInitilizer, 11359 EnumConstantDifferentInitilizer, 11360 }; 11361 11362 // If we've already pointed out a specific problem with this enum, don't 11363 // bother issuing a general "something's different" diagnostic. 11364 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11365 continue; 11366 11367 EnumDecl *FirstEnum = Merge.first; 11368 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11369 11370 using DeclHashes = 11371 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11372 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11373 DeclHashes &Hashes, EnumDecl *Enum) { 11374 for (auto *D : Enum->decls()) { 11375 // Due to decl merging, the first EnumDecl is the parent of 11376 // Decls in both records. 11377 if (!ODRHash::isDeclToBeProcessed(D, FirstEnum)) 11378 continue; 11379 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11380 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11381 ComputeSubDeclODRHash(D)); 11382 } 11383 }; 11384 DeclHashes FirstHashes; 11385 PopulateHashes(FirstHashes, FirstEnum); 11386 bool Diagnosed = false; 11387 for (auto &SecondEnum : Merge.second) { 11388 11389 if (FirstEnum == SecondEnum) 11390 continue; 11391 11392 std::string SecondModule = 11393 getOwningModuleNameForDiagnostic(SecondEnum); 11394 11395 auto ODRDiagError = [FirstEnum, &FirstModule, 11396 this](SourceLocation Loc, SourceRange Range, 11397 ODREnumDifference DiffType) { 11398 return Diag(Loc, diag::err_module_odr_violation_enum) 11399 << FirstEnum << FirstModule.empty() << FirstModule << Range 11400 << DiffType; 11401 }; 11402 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11403 SourceRange Range, 11404 ODREnumDifference DiffType) { 11405 return Diag(Loc, diag::note_module_odr_violation_enum) 11406 << SecondModule << Range << DiffType; 11407 }; 11408 11409 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11410 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11411 SingleScopedEnum) 11412 << FirstEnum->isScoped(); 11413 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11414 SingleScopedEnum) 11415 << SecondEnum->isScoped(); 11416 Diagnosed = true; 11417 continue; 11418 } 11419 11420 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11421 if (FirstEnum->isScopedUsingClassTag() != 11422 SecondEnum->isScopedUsingClassTag()) { 11423 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11424 EnumTagKeywordMismatch) 11425 << FirstEnum->isScopedUsingClassTag(); 11426 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11427 EnumTagKeywordMismatch) 11428 << SecondEnum->isScopedUsingClassTag(); 11429 Diagnosed = true; 11430 continue; 11431 } 11432 } 11433 11434 QualType FirstUnderlyingType = 11435 FirstEnum->getIntegerTypeSourceInfo() 11436 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11437 : QualType(); 11438 QualType SecondUnderlyingType = 11439 SecondEnum->getIntegerTypeSourceInfo() 11440 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11441 : QualType(); 11442 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11443 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11444 SingleSpecifiedType) 11445 << !FirstUnderlyingType.isNull(); 11446 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11447 SingleSpecifiedType) 11448 << !SecondUnderlyingType.isNull(); 11449 Diagnosed = true; 11450 continue; 11451 } 11452 11453 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11454 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11455 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11456 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11457 DifferentSpecifiedTypes) 11458 << FirstUnderlyingType; 11459 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11460 DifferentSpecifiedTypes) 11461 << SecondUnderlyingType; 11462 Diagnosed = true; 11463 continue; 11464 } 11465 } 11466 11467 DeclHashes SecondHashes; 11468 PopulateHashes(SecondHashes, SecondEnum); 11469 11470 if (FirstHashes.size() != SecondHashes.size()) { 11471 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11472 DifferentNumberEnumConstants) 11473 << (int)FirstHashes.size(); 11474 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11475 DifferentNumberEnumConstants) 11476 << (int)SecondHashes.size(); 11477 Diagnosed = true; 11478 continue; 11479 } 11480 11481 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11482 if (FirstHashes[I].second == SecondHashes[I].second) 11483 continue; 11484 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11485 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11486 11487 if (FirstEnumConstant->getDeclName() != 11488 SecondEnumConstant->getDeclName()) { 11489 11490 ODRDiagError(FirstEnumConstant->getLocation(), 11491 FirstEnumConstant->getSourceRange(), EnumConstantName) 11492 << I + 1 << FirstEnumConstant; 11493 ODRDiagNote(SecondEnumConstant->getLocation(), 11494 SecondEnumConstant->getSourceRange(), EnumConstantName) 11495 << I + 1 << SecondEnumConstant; 11496 Diagnosed = true; 11497 break; 11498 } 11499 11500 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11501 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11502 if (!FirstInit && !SecondInit) 11503 continue; 11504 11505 if (!FirstInit || !SecondInit) { 11506 ODRDiagError(FirstEnumConstant->getLocation(), 11507 FirstEnumConstant->getSourceRange(), 11508 EnumConstantSingleInitilizer) 11509 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11510 ODRDiagNote(SecondEnumConstant->getLocation(), 11511 SecondEnumConstant->getSourceRange(), 11512 EnumConstantSingleInitilizer) 11513 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11514 Diagnosed = true; 11515 break; 11516 } 11517 11518 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11519 ODRDiagError(FirstEnumConstant->getLocation(), 11520 FirstEnumConstant->getSourceRange(), 11521 EnumConstantDifferentInitilizer) 11522 << I + 1 << FirstEnumConstant; 11523 ODRDiagNote(SecondEnumConstant->getLocation(), 11524 SecondEnumConstant->getSourceRange(), 11525 EnumConstantDifferentInitilizer) 11526 << I + 1 << SecondEnumConstant; 11527 Diagnosed = true; 11528 break; 11529 } 11530 } 11531 } 11532 11533 (void)Diagnosed; 11534 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11535 } 11536 } 11537 11538 void ASTReader::StartedDeserializing() { 11539 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11540 ReadTimer->startTimer(); 11541 } 11542 11543 void ASTReader::FinishedDeserializing() { 11544 assert(NumCurrentElementsDeserializing && 11545 "FinishedDeserializing not paired with StartedDeserializing"); 11546 if (NumCurrentElementsDeserializing == 1) { 11547 // We decrease NumCurrentElementsDeserializing only after pending actions 11548 // are finished, to avoid recursively re-calling finishPendingActions(). 11549 finishPendingActions(); 11550 } 11551 --NumCurrentElementsDeserializing; 11552 11553 if (NumCurrentElementsDeserializing == 0) { 11554 // Propagate exception specification and deduced type updates along 11555 // redeclaration chains. 11556 // 11557 // We do this now rather than in finishPendingActions because we want to 11558 // be able to walk the complete redeclaration chains of the updated decls. 11559 while (!PendingExceptionSpecUpdates.empty() || 11560 !PendingDeducedTypeUpdates.empty()) { 11561 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11562 PendingExceptionSpecUpdates.clear(); 11563 for (auto Update : ESUpdates) { 11564 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11565 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11566 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11567 if (auto *Listener = getContext().getASTMutationListener()) 11568 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11569 for (auto *Redecl : Update.second->redecls()) 11570 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11571 } 11572 11573 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11574 PendingDeducedTypeUpdates.clear(); 11575 for (auto Update : DTUpdates) { 11576 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11577 // FIXME: If the return type is already deduced, check that it matches. 11578 getContext().adjustDeducedFunctionResultType(Update.first, 11579 Update.second); 11580 } 11581 } 11582 11583 if (ReadTimer) 11584 ReadTimer->stopTimer(); 11585 11586 diagnoseOdrViolations(); 11587 11588 // We are not in recursive loading, so it's safe to pass the "interesting" 11589 // decls to the consumer. 11590 if (Consumer) 11591 PassInterestingDeclsToConsumer(); 11592 } 11593 } 11594 11595 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11596 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11597 // Remove any fake results before adding any real ones. 11598 auto It = PendingFakeLookupResults.find(II); 11599 if (It != PendingFakeLookupResults.end()) { 11600 for (auto *ND : It->second) 11601 SemaObj->IdResolver.RemoveDecl(ND); 11602 // FIXME: this works around module+PCH performance issue. 11603 // Rather than erase the result from the map, which is O(n), just clear 11604 // the vector of NamedDecls. 11605 It->second.clear(); 11606 } 11607 } 11608 11609 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11610 SemaObj->TUScope->AddDecl(D); 11611 } else if (SemaObj->TUScope) { 11612 // Adding the decl to IdResolver may have failed because it was already in 11613 // (even though it was not added in scope). If it is already in, make sure 11614 // it gets in the scope as well. 11615 if (std::find(SemaObj->IdResolver.begin(Name), 11616 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11617 SemaObj->TUScope->AddDecl(D); 11618 } 11619 } 11620 11621 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11622 ASTContext *Context, 11623 const PCHContainerReader &PCHContainerRdr, 11624 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11625 StringRef isysroot, 11626 DisableValidationForModuleKind DisableValidationKind, 11627 bool AllowASTWithCompilerErrors, 11628 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11629 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11630 std::unique_ptr<llvm::Timer> ReadTimer) 11631 : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH) 11632 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11633 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11634 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11635 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11636 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11637 PCHContainerRdr, PP.getHeaderSearchInfo()), 11638 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11639 DisableValidationKind(DisableValidationKind), 11640 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11641 AllowConfigurationMismatch(AllowConfigurationMismatch), 11642 ValidateSystemInputs(ValidateSystemInputs), 11643 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11644 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11645 SourceMgr.setExternalSLocEntrySource(this); 11646 11647 for (const auto &Ext : Extensions) { 11648 auto BlockName = Ext->getExtensionMetadata().BlockName; 11649 auto Known = ModuleFileExtensions.find(BlockName); 11650 if (Known != ModuleFileExtensions.end()) { 11651 Diags.Report(diag::warn_duplicate_module_file_extension) 11652 << BlockName; 11653 continue; 11654 } 11655 11656 ModuleFileExtensions.insert({BlockName, Ext}); 11657 } 11658 } 11659 11660 ASTReader::~ASTReader() { 11661 if (OwnsDeserializationListener) 11662 delete DeserializationListener; 11663 } 11664 11665 IdentifierResolver &ASTReader::getIdResolver() { 11666 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11667 } 11668 11669 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11670 unsigned AbbrevID) { 11671 Idx = 0; 11672 Record.clear(); 11673 return Cursor.readRecord(AbbrevID, Record); 11674 } 11675 //===----------------------------------------------------------------------===// 11676 //// OMPClauseReader implementation 11677 ////===----------------------------------------------------------------------===// 11678 11679 // This has to be in namespace clang because it's friended by all 11680 // of the OMP clauses. 11681 namespace clang { 11682 11683 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11684 ASTRecordReader &Record; 11685 ASTContext &Context; 11686 11687 public: 11688 OMPClauseReader(ASTRecordReader &Record) 11689 : Record(Record), Context(Record.getContext()) {} 11690 #define GEN_CLANG_CLAUSE_CLASS 11691 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C); 11692 #include "llvm/Frontend/OpenMP/OMP.inc" 11693 OMPClause *readClause(); 11694 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11695 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11696 }; 11697 11698 } // end namespace clang 11699 11700 OMPClause *ASTRecordReader::readOMPClause() { 11701 return OMPClauseReader(*this).readClause(); 11702 } 11703 11704 OMPClause *OMPClauseReader::readClause() { 11705 OMPClause *C = nullptr; 11706 switch (llvm::omp::Clause(Record.readInt())) { 11707 case llvm::omp::OMPC_if: 11708 C = new (Context) OMPIfClause(); 11709 break; 11710 case llvm::omp::OMPC_final: 11711 C = new (Context) OMPFinalClause(); 11712 break; 11713 case llvm::omp::OMPC_num_threads: 11714 C = new (Context) OMPNumThreadsClause(); 11715 break; 11716 case llvm::omp::OMPC_safelen: 11717 C = new (Context) OMPSafelenClause(); 11718 break; 11719 case llvm::omp::OMPC_simdlen: 11720 C = new (Context) OMPSimdlenClause(); 11721 break; 11722 case llvm::omp::OMPC_sizes: { 11723 unsigned NumSizes = Record.readInt(); 11724 C = OMPSizesClause::CreateEmpty(Context, NumSizes); 11725 break; 11726 } 11727 case llvm::omp::OMPC_full: 11728 C = OMPFullClause::CreateEmpty(Context); 11729 break; 11730 case llvm::omp::OMPC_partial: 11731 C = OMPPartialClause::CreateEmpty(Context); 11732 break; 11733 case llvm::omp::OMPC_allocator: 11734 C = new (Context) OMPAllocatorClause(); 11735 break; 11736 case llvm::omp::OMPC_collapse: 11737 C = new (Context) OMPCollapseClause(); 11738 break; 11739 case llvm::omp::OMPC_default: 11740 C = new (Context) OMPDefaultClause(); 11741 break; 11742 case llvm::omp::OMPC_proc_bind: 11743 C = new (Context) OMPProcBindClause(); 11744 break; 11745 case llvm::omp::OMPC_schedule: 11746 C = new (Context) OMPScheduleClause(); 11747 break; 11748 case llvm::omp::OMPC_ordered: 11749 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11750 break; 11751 case llvm::omp::OMPC_nowait: 11752 C = new (Context) OMPNowaitClause(); 11753 break; 11754 case llvm::omp::OMPC_untied: 11755 C = new (Context) OMPUntiedClause(); 11756 break; 11757 case llvm::omp::OMPC_mergeable: 11758 C = new (Context) OMPMergeableClause(); 11759 break; 11760 case llvm::omp::OMPC_read: 11761 C = new (Context) OMPReadClause(); 11762 break; 11763 case llvm::omp::OMPC_write: 11764 C = new (Context) OMPWriteClause(); 11765 break; 11766 case llvm::omp::OMPC_update: 11767 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11768 break; 11769 case llvm::omp::OMPC_capture: 11770 C = new (Context) OMPCaptureClause(); 11771 break; 11772 case llvm::omp::OMPC_seq_cst: 11773 C = new (Context) OMPSeqCstClause(); 11774 break; 11775 case llvm::omp::OMPC_acq_rel: 11776 C = new (Context) OMPAcqRelClause(); 11777 break; 11778 case llvm::omp::OMPC_acquire: 11779 C = new (Context) OMPAcquireClause(); 11780 break; 11781 case llvm::omp::OMPC_release: 11782 C = new (Context) OMPReleaseClause(); 11783 break; 11784 case llvm::omp::OMPC_relaxed: 11785 C = new (Context) OMPRelaxedClause(); 11786 break; 11787 case llvm::omp::OMPC_threads: 11788 C = new (Context) OMPThreadsClause(); 11789 break; 11790 case llvm::omp::OMPC_simd: 11791 C = new (Context) OMPSIMDClause(); 11792 break; 11793 case llvm::omp::OMPC_nogroup: 11794 C = new (Context) OMPNogroupClause(); 11795 break; 11796 case llvm::omp::OMPC_unified_address: 11797 C = new (Context) OMPUnifiedAddressClause(); 11798 break; 11799 case llvm::omp::OMPC_unified_shared_memory: 11800 C = new (Context) OMPUnifiedSharedMemoryClause(); 11801 break; 11802 case llvm::omp::OMPC_reverse_offload: 11803 C = new (Context) OMPReverseOffloadClause(); 11804 break; 11805 case llvm::omp::OMPC_dynamic_allocators: 11806 C = new (Context) OMPDynamicAllocatorsClause(); 11807 break; 11808 case llvm::omp::OMPC_atomic_default_mem_order: 11809 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11810 break; 11811 case llvm::omp::OMPC_private: 11812 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11813 break; 11814 case llvm::omp::OMPC_firstprivate: 11815 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11816 break; 11817 case llvm::omp::OMPC_lastprivate: 11818 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11819 break; 11820 case llvm::omp::OMPC_shared: 11821 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11822 break; 11823 case llvm::omp::OMPC_reduction: { 11824 unsigned N = Record.readInt(); 11825 auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>(); 11826 C = OMPReductionClause::CreateEmpty(Context, N, Modifier); 11827 break; 11828 } 11829 case llvm::omp::OMPC_task_reduction: 11830 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11831 break; 11832 case llvm::omp::OMPC_in_reduction: 11833 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11834 break; 11835 case llvm::omp::OMPC_linear: 11836 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11837 break; 11838 case llvm::omp::OMPC_aligned: 11839 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11840 break; 11841 case llvm::omp::OMPC_copyin: 11842 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11843 break; 11844 case llvm::omp::OMPC_copyprivate: 11845 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11846 break; 11847 case llvm::omp::OMPC_flush: 11848 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11849 break; 11850 case llvm::omp::OMPC_depobj: 11851 C = OMPDepobjClause::CreateEmpty(Context); 11852 break; 11853 case llvm::omp::OMPC_depend: { 11854 unsigned NumVars = Record.readInt(); 11855 unsigned NumLoops = Record.readInt(); 11856 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11857 break; 11858 } 11859 case llvm::omp::OMPC_device: 11860 C = new (Context) OMPDeviceClause(); 11861 break; 11862 case llvm::omp::OMPC_map: { 11863 OMPMappableExprListSizeTy Sizes; 11864 Sizes.NumVars = Record.readInt(); 11865 Sizes.NumUniqueDeclarations = Record.readInt(); 11866 Sizes.NumComponentLists = Record.readInt(); 11867 Sizes.NumComponents = Record.readInt(); 11868 C = OMPMapClause::CreateEmpty(Context, Sizes); 11869 break; 11870 } 11871 case llvm::omp::OMPC_num_teams: 11872 C = new (Context) OMPNumTeamsClause(); 11873 break; 11874 case llvm::omp::OMPC_thread_limit: 11875 C = new (Context) OMPThreadLimitClause(); 11876 break; 11877 case llvm::omp::OMPC_priority: 11878 C = new (Context) OMPPriorityClause(); 11879 break; 11880 case llvm::omp::OMPC_grainsize: 11881 C = new (Context) OMPGrainsizeClause(); 11882 break; 11883 case llvm::omp::OMPC_num_tasks: 11884 C = new (Context) OMPNumTasksClause(); 11885 break; 11886 case llvm::omp::OMPC_hint: 11887 C = new (Context) OMPHintClause(); 11888 break; 11889 case llvm::omp::OMPC_dist_schedule: 11890 C = new (Context) OMPDistScheduleClause(); 11891 break; 11892 case llvm::omp::OMPC_defaultmap: 11893 C = new (Context) OMPDefaultmapClause(); 11894 break; 11895 case llvm::omp::OMPC_to: { 11896 OMPMappableExprListSizeTy Sizes; 11897 Sizes.NumVars = Record.readInt(); 11898 Sizes.NumUniqueDeclarations = Record.readInt(); 11899 Sizes.NumComponentLists = Record.readInt(); 11900 Sizes.NumComponents = Record.readInt(); 11901 C = OMPToClause::CreateEmpty(Context, Sizes); 11902 break; 11903 } 11904 case llvm::omp::OMPC_from: { 11905 OMPMappableExprListSizeTy Sizes; 11906 Sizes.NumVars = Record.readInt(); 11907 Sizes.NumUniqueDeclarations = Record.readInt(); 11908 Sizes.NumComponentLists = Record.readInt(); 11909 Sizes.NumComponents = Record.readInt(); 11910 C = OMPFromClause::CreateEmpty(Context, Sizes); 11911 break; 11912 } 11913 case llvm::omp::OMPC_use_device_ptr: { 11914 OMPMappableExprListSizeTy Sizes; 11915 Sizes.NumVars = Record.readInt(); 11916 Sizes.NumUniqueDeclarations = Record.readInt(); 11917 Sizes.NumComponentLists = Record.readInt(); 11918 Sizes.NumComponents = Record.readInt(); 11919 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11920 break; 11921 } 11922 case llvm::omp::OMPC_use_device_addr: { 11923 OMPMappableExprListSizeTy Sizes; 11924 Sizes.NumVars = Record.readInt(); 11925 Sizes.NumUniqueDeclarations = Record.readInt(); 11926 Sizes.NumComponentLists = Record.readInt(); 11927 Sizes.NumComponents = Record.readInt(); 11928 C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes); 11929 break; 11930 } 11931 case llvm::omp::OMPC_is_device_ptr: { 11932 OMPMappableExprListSizeTy Sizes; 11933 Sizes.NumVars = Record.readInt(); 11934 Sizes.NumUniqueDeclarations = Record.readInt(); 11935 Sizes.NumComponentLists = Record.readInt(); 11936 Sizes.NumComponents = Record.readInt(); 11937 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11938 break; 11939 } 11940 case llvm::omp::OMPC_allocate: 11941 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11942 break; 11943 case llvm::omp::OMPC_nontemporal: 11944 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11945 break; 11946 case llvm::omp::OMPC_inclusive: 11947 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11948 break; 11949 case llvm::omp::OMPC_exclusive: 11950 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11951 break; 11952 case llvm::omp::OMPC_order: 11953 C = new (Context) OMPOrderClause(); 11954 break; 11955 case llvm::omp::OMPC_init: 11956 C = OMPInitClause::CreateEmpty(Context, Record.readInt()); 11957 break; 11958 case llvm::omp::OMPC_use: 11959 C = new (Context) OMPUseClause(); 11960 break; 11961 case llvm::omp::OMPC_destroy: 11962 C = new (Context) OMPDestroyClause(); 11963 break; 11964 case llvm::omp::OMPC_novariants: 11965 C = new (Context) OMPNovariantsClause(); 11966 break; 11967 case llvm::omp::OMPC_nocontext: 11968 C = new (Context) OMPNocontextClause(); 11969 break; 11970 case llvm::omp::OMPC_detach: 11971 C = new (Context) OMPDetachClause(); 11972 break; 11973 case llvm::omp::OMPC_uses_allocators: 11974 C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt()); 11975 break; 11976 case llvm::omp::OMPC_affinity: 11977 C = OMPAffinityClause::CreateEmpty(Context, Record.readInt()); 11978 break; 11979 case llvm::omp::OMPC_filter: 11980 C = new (Context) OMPFilterClause(); 11981 break; 11982 #define OMP_CLAUSE_NO_CLASS(Enum, Str) \ 11983 case llvm::omp::Enum: \ 11984 break; 11985 #include "llvm/Frontend/OpenMP/OMPKinds.def" 11986 default: 11987 break; 11988 } 11989 assert(C && "Unknown OMPClause type"); 11990 11991 Visit(C); 11992 C->setLocStart(Record.readSourceLocation()); 11993 C->setLocEnd(Record.readSourceLocation()); 11994 11995 return C; 11996 } 11997 11998 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11999 C->setPreInitStmt(Record.readSubStmt(), 12000 static_cast<OpenMPDirectiveKind>(Record.readInt())); 12001 } 12002 12003 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 12004 VisitOMPClauseWithPreInit(C); 12005 C->setPostUpdateExpr(Record.readSubExpr()); 12006 } 12007 12008 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 12009 VisitOMPClauseWithPreInit(C); 12010 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 12011 C->setNameModifierLoc(Record.readSourceLocation()); 12012 C->setColonLoc(Record.readSourceLocation()); 12013 C->setCondition(Record.readSubExpr()); 12014 C->setLParenLoc(Record.readSourceLocation()); 12015 } 12016 12017 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 12018 VisitOMPClauseWithPreInit(C); 12019 C->setCondition(Record.readSubExpr()); 12020 C->setLParenLoc(Record.readSourceLocation()); 12021 } 12022 12023 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 12024 VisitOMPClauseWithPreInit(C); 12025 C->setNumThreads(Record.readSubExpr()); 12026 C->setLParenLoc(Record.readSourceLocation()); 12027 } 12028 12029 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 12030 C->setSafelen(Record.readSubExpr()); 12031 C->setLParenLoc(Record.readSourceLocation()); 12032 } 12033 12034 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 12035 C->setSimdlen(Record.readSubExpr()); 12036 C->setLParenLoc(Record.readSourceLocation()); 12037 } 12038 12039 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) { 12040 for (Expr *&E : C->getSizesRefs()) 12041 E = Record.readSubExpr(); 12042 C->setLParenLoc(Record.readSourceLocation()); 12043 } 12044 12045 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {} 12046 12047 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) { 12048 C->setFactor(Record.readSubExpr()); 12049 C->setLParenLoc(Record.readSourceLocation()); 12050 } 12051 12052 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 12053 C->setAllocator(Record.readExpr()); 12054 C->setLParenLoc(Record.readSourceLocation()); 12055 } 12056 12057 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 12058 C->setNumForLoops(Record.readSubExpr()); 12059 C->setLParenLoc(Record.readSourceLocation()); 12060 } 12061 12062 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 12063 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 12064 C->setLParenLoc(Record.readSourceLocation()); 12065 C->setDefaultKindKwLoc(Record.readSourceLocation()); 12066 } 12067 12068 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 12069 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 12070 C->setLParenLoc(Record.readSourceLocation()); 12071 C->setProcBindKindKwLoc(Record.readSourceLocation()); 12072 } 12073 12074 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 12075 VisitOMPClauseWithPreInit(C); 12076 C->setScheduleKind( 12077 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 12078 C->setFirstScheduleModifier( 12079 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12080 C->setSecondScheduleModifier( 12081 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 12082 C->setChunkSize(Record.readSubExpr()); 12083 C->setLParenLoc(Record.readSourceLocation()); 12084 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 12085 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 12086 C->setScheduleKindLoc(Record.readSourceLocation()); 12087 C->setCommaLoc(Record.readSourceLocation()); 12088 } 12089 12090 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 12091 C->setNumForLoops(Record.readSubExpr()); 12092 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12093 C->setLoopNumIterations(I, Record.readSubExpr()); 12094 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 12095 C->setLoopCounter(I, Record.readSubExpr()); 12096 C->setLParenLoc(Record.readSourceLocation()); 12097 } 12098 12099 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 12100 C->setEventHandler(Record.readSubExpr()); 12101 C->setLParenLoc(Record.readSourceLocation()); 12102 } 12103 12104 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 12105 12106 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 12107 12108 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 12109 12110 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 12111 12112 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 12113 12114 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 12115 if (C->isExtended()) { 12116 C->setLParenLoc(Record.readSourceLocation()); 12117 C->setArgumentLoc(Record.readSourceLocation()); 12118 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 12119 } 12120 } 12121 12122 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 12123 12124 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 12125 12126 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 12127 12128 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 12129 12130 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 12131 12132 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 12133 12134 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 12135 12136 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 12137 12138 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 12139 12140 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) { 12141 unsigned NumVars = C->varlist_size(); 12142 SmallVector<Expr *, 16> Vars; 12143 Vars.reserve(NumVars); 12144 for (unsigned I = 0; I != NumVars; ++I) 12145 Vars.push_back(Record.readSubExpr()); 12146 C->setVarRefs(Vars); 12147 C->setIsTarget(Record.readBool()); 12148 C->setIsTargetSync(Record.readBool()); 12149 C->setLParenLoc(Record.readSourceLocation()); 12150 C->setVarLoc(Record.readSourceLocation()); 12151 } 12152 12153 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) { 12154 C->setInteropVar(Record.readSubExpr()); 12155 C->setLParenLoc(Record.readSourceLocation()); 12156 C->setVarLoc(Record.readSourceLocation()); 12157 } 12158 12159 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) { 12160 C->setInteropVar(Record.readSubExpr()); 12161 C->setLParenLoc(Record.readSourceLocation()); 12162 C->setVarLoc(Record.readSourceLocation()); 12163 } 12164 12165 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) { 12166 VisitOMPClauseWithPreInit(C); 12167 C->setCondition(Record.readSubExpr()); 12168 C->setLParenLoc(Record.readSourceLocation()); 12169 } 12170 12171 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) { 12172 VisitOMPClauseWithPreInit(C); 12173 C->setCondition(Record.readSubExpr()); 12174 C->setLParenLoc(Record.readSourceLocation()); 12175 } 12176 12177 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12178 12179 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12180 OMPUnifiedSharedMemoryClause *) {} 12181 12182 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12183 12184 void 12185 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12186 } 12187 12188 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12189 OMPAtomicDefaultMemOrderClause *C) { 12190 C->setAtomicDefaultMemOrderKind( 12191 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12192 C->setLParenLoc(Record.readSourceLocation()); 12193 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12194 } 12195 12196 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12197 C->setLParenLoc(Record.readSourceLocation()); 12198 unsigned NumVars = C->varlist_size(); 12199 SmallVector<Expr *, 16> Vars; 12200 Vars.reserve(NumVars); 12201 for (unsigned i = 0; i != NumVars; ++i) 12202 Vars.push_back(Record.readSubExpr()); 12203 C->setVarRefs(Vars); 12204 Vars.clear(); 12205 for (unsigned i = 0; i != NumVars; ++i) 12206 Vars.push_back(Record.readSubExpr()); 12207 C->setPrivateCopies(Vars); 12208 } 12209 12210 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12211 VisitOMPClauseWithPreInit(C); 12212 C->setLParenLoc(Record.readSourceLocation()); 12213 unsigned NumVars = C->varlist_size(); 12214 SmallVector<Expr *, 16> Vars; 12215 Vars.reserve(NumVars); 12216 for (unsigned i = 0; i != NumVars; ++i) 12217 Vars.push_back(Record.readSubExpr()); 12218 C->setVarRefs(Vars); 12219 Vars.clear(); 12220 for (unsigned i = 0; i != NumVars; ++i) 12221 Vars.push_back(Record.readSubExpr()); 12222 C->setPrivateCopies(Vars); 12223 Vars.clear(); 12224 for (unsigned i = 0; i != NumVars; ++i) 12225 Vars.push_back(Record.readSubExpr()); 12226 C->setInits(Vars); 12227 } 12228 12229 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12230 VisitOMPClauseWithPostUpdate(C); 12231 C->setLParenLoc(Record.readSourceLocation()); 12232 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12233 C->setKindLoc(Record.readSourceLocation()); 12234 C->setColonLoc(Record.readSourceLocation()); 12235 unsigned NumVars = C->varlist_size(); 12236 SmallVector<Expr *, 16> Vars; 12237 Vars.reserve(NumVars); 12238 for (unsigned i = 0; i != NumVars; ++i) 12239 Vars.push_back(Record.readSubExpr()); 12240 C->setVarRefs(Vars); 12241 Vars.clear(); 12242 for (unsigned i = 0; i != NumVars; ++i) 12243 Vars.push_back(Record.readSubExpr()); 12244 C->setPrivateCopies(Vars); 12245 Vars.clear(); 12246 for (unsigned i = 0; i != NumVars; ++i) 12247 Vars.push_back(Record.readSubExpr()); 12248 C->setSourceExprs(Vars); 12249 Vars.clear(); 12250 for (unsigned i = 0; i != NumVars; ++i) 12251 Vars.push_back(Record.readSubExpr()); 12252 C->setDestinationExprs(Vars); 12253 Vars.clear(); 12254 for (unsigned i = 0; i != NumVars; ++i) 12255 Vars.push_back(Record.readSubExpr()); 12256 C->setAssignmentOps(Vars); 12257 } 12258 12259 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12260 C->setLParenLoc(Record.readSourceLocation()); 12261 unsigned NumVars = C->varlist_size(); 12262 SmallVector<Expr *, 16> Vars; 12263 Vars.reserve(NumVars); 12264 for (unsigned i = 0; i != NumVars; ++i) 12265 Vars.push_back(Record.readSubExpr()); 12266 C->setVarRefs(Vars); 12267 } 12268 12269 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12270 VisitOMPClauseWithPostUpdate(C); 12271 C->setLParenLoc(Record.readSourceLocation()); 12272 C->setModifierLoc(Record.readSourceLocation()); 12273 C->setColonLoc(Record.readSourceLocation()); 12274 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12275 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12276 C->setQualifierLoc(NNSL); 12277 C->setNameInfo(DNI); 12278 12279 unsigned NumVars = C->varlist_size(); 12280 SmallVector<Expr *, 16> Vars; 12281 Vars.reserve(NumVars); 12282 for (unsigned i = 0; i != NumVars; ++i) 12283 Vars.push_back(Record.readSubExpr()); 12284 C->setVarRefs(Vars); 12285 Vars.clear(); 12286 for (unsigned i = 0; i != NumVars; ++i) 12287 Vars.push_back(Record.readSubExpr()); 12288 C->setPrivates(Vars); 12289 Vars.clear(); 12290 for (unsigned i = 0; i != NumVars; ++i) 12291 Vars.push_back(Record.readSubExpr()); 12292 C->setLHSExprs(Vars); 12293 Vars.clear(); 12294 for (unsigned i = 0; i != NumVars; ++i) 12295 Vars.push_back(Record.readSubExpr()); 12296 C->setRHSExprs(Vars); 12297 Vars.clear(); 12298 for (unsigned i = 0; i != NumVars; ++i) 12299 Vars.push_back(Record.readSubExpr()); 12300 C->setReductionOps(Vars); 12301 if (C->getModifier() == OMPC_REDUCTION_inscan) { 12302 Vars.clear(); 12303 for (unsigned i = 0; i != NumVars; ++i) 12304 Vars.push_back(Record.readSubExpr()); 12305 C->setInscanCopyOps(Vars); 12306 Vars.clear(); 12307 for (unsigned i = 0; i != NumVars; ++i) 12308 Vars.push_back(Record.readSubExpr()); 12309 C->setInscanCopyArrayTemps(Vars); 12310 Vars.clear(); 12311 for (unsigned i = 0; i != NumVars; ++i) 12312 Vars.push_back(Record.readSubExpr()); 12313 C->setInscanCopyArrayElems(Vars); 12314 } 12315 } 12316 12317 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12318 VisitOMPClauseWithPostUpdate(C); 12319 C->setLParenLoc(Record.readSourceLocation()); 12320 C->setColonLoc(Record.readSourceLocation()); 12321 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12322 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12323 C->setQualifierLoc(NNSL); 12324 C->setNameInfo(DNI); 12325 12326 unsigned NumVars = C->varlist_size(); 12327 SmallVector<Expr *, 16> Vars; 12328 Vars.reserve(NumVars); 12329 for (unsigned I = 0; I != NumVars; ++I) 12330 Vars.push_back(Record.readSubExpr()); 12331 C->setVarRefs(Vars); 12332 Vars.clear(); 12333 for (unsigned I = 0; I != NumVars; ++I) 12334 Vars.push_back(Record.readSubExpr()); 12335 C->setPrivates(Vars); 12336 Vars.clear(); 12337 for (unsigned I = 0; I != NumVars; ++I) 12338 Vars.push_back(Record.readSubExpr()); 12339 C->setLHSExprs(Vars); 12340 Vars.clear(); 12341 for (unsigned I = 0; I != NumVars; ++I) 12342 Vars.push_back(Record.readSubExpr()); 12343 C->setRHSExprs(Vars); 12344 Vars.clear(); 12345 for (unsigned I = 0; I != NumVars; ++I) 12346 Vars.push_back(Record.readSubExpr()); 12347 C->setReductionOps(Vars); 12348 } 12349 12350 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12351 VisitOMPClauseWithPostUpdate(C); 12352 C->setLParenLoc(Record.readSourceLocation()); 12353 C->setColonLoc(Record.readSourceLocation()); 12354 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12355 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12356 C->setQualifierLoc(NNSL); 12357 C->setNameInfo(DNI); 12358 12359 unsigned NumVars = C->varlist_size(); 12360 SmallVector<Expr *, 16> Vars; 12361 Vars.reserve(NumVars); 12362 for (unsigned I = 0; I != NumVars; ++I) 12363 Vars.push_back(Record.readSubExpr()); 12364 C->setVarRefs(Vars); 12365 Vars.clear(); 12366 for (unsigned I = 0; I != NumVars; ++I) 12367 Vars.push_back(Record.readSubExpr()); 12368 C->setPrivates(Vars); 12369 Vars.clear(); 12370 for (unsigned I = 0; I != NumVars; ++I) 12371 Vars.push_back(Record.readSubExpr()); 12372 C->setLHSExprs(Vars); 12373 Vars.clear(); 12374 for (unsigned I = 0; I != NumVars; ++I) 12375 Vars.push_back(Record.readSubExpr()); 12376 C->setRHSExprs(Vars); 12377 Vars.clear(); 12378 for (unsigned I = 0; I != NumVars; ++I) 12379 Vars.push_back(Record.readSubExpr()); 12380 C->setReductionOps(Vars); 12381 Vars.clear(); 12382 for (unsigned I = 0; I != NumVars; ++I) 12383 Vars.push_back(Record.readSubExpr()); 12384 C->setTaskgroupDescriptors(Vars); 12385 } 12386 12387 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12388 VisitOMPClauseWithPostUpdate(C); 12389 C->setLParenLoc(Record.readSourceLocation()); 12390 C->setColonLoc(Record.readSourceLocation()); 12391 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12392 C->setModifierLoc(Record.readSourceLocation()); 12393 unsigned NumVars = C->varlist_size(); 12394 SmallVector<Expr *, 16> Vars; 12395 Vars.reserve(NumVars); 12396 for (unsigned i = 0; i != NumVars; ++i) 12397 Vars.push_back(Record.readSubExpr()); 12398 C->setVarRefs(Vars); 12399 Vars.clear(); 12400 for (unsigned i = 0; i != NumVars; ++i) 12401 Vars.push_back(Record.readSubExpr()); 12402 C->setPrivates(Vars); 12403 Vars.clear(); 12404 for (unsigned i = 0; i != NumVars; ++i) 12405 Vars.push_back(Record.readSubExpr()); 12406 C->setInits(Vars); 12407 Vars.clear(); 12408 for (unsigned i = 0; i != NumVars; ++i) 12409 Vars.push_back(Record.readSubExpr()); 12410 C->setUpdates(Vars); 12411 Vars.clear(); 12412 for (unsigned i = 0; i != NumVars; ++i) 12413 Vars.push_back(Record.readSubExpr()); 12414 C->setFinals(Vars); 12415 C->setStep(Record.readSubExpr()); 12416 C->setCalcStep(Record.readSubExpr()); 12417 Vars.clear(); 12418 for (unsigned I = 0; I != NumVars + 1; ++I) 12419 Vars.push_back(Record.readSubExpr()); 12420 C->setUsedExprs(Vars); 12421 } 12422 12423 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12424 C->setLParenLoc(Record.readSourceLocation()); 12425 C->setColonLoc(Record.readSourceLocation()); 12426 unsigned NumVars = C->varlist_size(); 12427 SmallVector<Expr *, 16> Vars; 12428 Vars.reserve(NumVars); 12429 for (unsigned i = 0; i != NumVars; ++i) 12430 Vars.push_back(Record.readSubExpr()); 12431 C->setVarRefs(Vars); 12432 C->setAlignment(Record.readSubExpr()); 12433 } 12434 12435 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12436 C->setLParenLoc(Record.readSourceLocation()); 12437 unsigned NumVars = C->varlist_size(); 12438 SmallVector<Expr *, 16> Exprs; 12439 Exprs.reserve(NumVars); 12440 for (unsigned i = 0; i != NumVars; ++i) 12441 Exprs.push_back(Record.readSubExpr()); 12442 C->setVarRefs(Exprs); 12443 Exprs.clear(); 12444 for (unsigned i = 0; i != NumVars; ++i) 12445 Exprs.push_back(Record.readSubExpr()); 12446 C->setSourceExprs(Exprs); 12447 Exprs.clear(); 12448 for (unsigned i = 0; i != NumVars; ++i) 12449 Exprs.push_back(Record.readSubExpr()); 12450 C->setDestinationExprs(Exprs); 12451 Exprs.clear(); 12452 for (unsigned i = 0; i != NumVars; ++i) 12453 Exprs.push_back(Record.readSubExpr()); 12454 C->setAssignmentOps(Exprs); 12455 } 12456 12457 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12458 C->setLParenLoc(Record.readSourceLocation()); 12459 unsigned NumVars = C->varlist_size(); 12460 SmallVector<Expr *, 16> Exprs; 12461 Exprs.reserve(NumVars); 12462 for (unsigned i = 0; i != NumVars; ++i) 12463 Exprs.push_back(Record.readSubExpr()); 12464 C->setVarRefs(Exprs); 12465 Exprs.clear(); 12466 for (unsigned i = 0; i != NumVars; ++i) 12467 Exprs.push_back(Record.readSubExpr()); 12468 C->setSourceExprs(Exprs); 12469 Exprs.clear(); 12470 for (unsigned i = 0; i != NumVars; ++i) 12471 Exprs.push_back(Record.readSubExpr()); 12472 C->setDestinationExprs(Exprs); 12473 Exprs.clear(); 12474 for (unsigned i = 0; i != NumVars; ++i) 12475 Exprs.push_back(Record.readSubExpr()); 12476 C->setAssignmentOps(Exprs); 12477 } 12478 12479 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12480 C->setLParenLoc(Record.readSourceLocation()); 12481 unsigned NumVars = C->varlist_size(); 12482 SmallVector<Expr *, 16> Vars; 12483 Vars.reserve(NumVars); 12484 for (unsigned i = 0; i != NumVars; ++i) 12485 Vars.push_back(Record.readSubExpr()); 12486 C->setVarRefs(Vars); 12487 } 12488 12489 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12490 C->setDepobj(Record.readSubExpr()); 12491 C->setLParenLoc(Record.readSourceLocation()); 12492 } 12493 12494 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12495 C->setLParenLoc(Record.readSourceLocation()); 12496 C->setModifier(Record.readSubExpr()); 12497 C->setDependencyKind( 12498 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12499 C->setDependencyLoc(Record.readSourceLocation()); 12500 C->setColonLoc(Record.readSourceLocation()); 12501 unsigned NumVars = C->varlist_size(); 12502 SmallVector<Expr *, 16> Vars; 12503 Vars.reserve(NumVars); 12504 for (unsigned I = 0; I != NumVars; ++I) 12505 Vars.push_back(Record.readSubExpr()); 12506 C->setVarRefs(Vars); 12507 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12508 C->setLoopData(I, Record.readSubExpr()); 12509 } 12510 12511 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12512 VisitOMPClauseWithPreInit(C); 12513 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12514 C->setDevice(Record.readSubExpr()); 12515 C->setModifierLoc(Record.readSourceLocation()); 12516 C->setLParenLoc(Record.readSourceLocation()); 12517 } 12518 12519 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12520 C->setLParenLoc(Record.readSourceLocation()); 12521 for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) { 12522 C->setMapTypeModifier( 12523 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12524 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12525 } 12526 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12527 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12528 C->setMapType( 12529 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12530 C->setMapLoc(Record.readSourceLocation()); 12531 C->setColonLoc(Record.readSourceLocation()); 12532 auto NumVars = C->varlist_size(); 12533 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12534 auto TotalLists = C->getTotalComponentListNum(); 12535 auto TotalComponents = C->getTotalComponentsNum(); 12536 12537 SmallVector<Expr *, 16> Vars; 12538 Vars.reserve(NumVars); 12539 for (unsigned i = 0; i != NumVars; ++i) 12540 Vars.push_back(Record.readExpr()); 12541 C->setVarRefs(Vars); 12542 12543 SmallVector<Expr *, 16> UDMappers; 12544 UDMappers.reserve(NumVars); 12545 for (unsigned I = 0; I < NumVars; ++I) 12546 UDMappers.push_back(Record.readExpr()); 12547 C->setUDMapperRefs(UDMappers); 12548 12549 SmallVector<ValueDecl *, 16> Decls; 12550 Decls.reserve(UniqueDecls); 12551 for (unsigned i = 0; i < UniqueDecls; ++i) 12552 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12553 C->setUniqueDecls(Decls); 12554 12555 SmallVector<unsigned, 16> ListsPerDecl; 12556 ListsPerDecl.reserve(UniqueDecls); 12557 for (unsigned i = 0; i < UniqueDecls; ++i) 12558 ListsPerDecl.push_back(Record.readInt()); 12559 C->setDeclNumLists(ListsPerDecl); 12560 12561 SmallVector<unsigned, 32> ListSizes; 12562 ListSizes.reserve(TotalLists); 12563 for (unsigned i = 0; i < TotalLists; ++i) 12564 ListSizes.push_back(Record.readInt()); 12565 C->setComponentListSizes(ListSizes); 12566 12567 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12568 Components.reserve(TotalComponents); 12569 for (unsigned i = 0; i < TotalComponents; ++i) { 12570 Expr *AssociatedExprPr = Record.readExpr(); 12571 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12572 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12573 /*IsNonContiguous=*/false); 12574 } 12575 C->setComponents(Components, ListSizes); 12576 } 12577 12578 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12579 C->setLParenLoc(Record.readSourceLocation()); 12580 C->setColonLoc(Record.readSourceLocation()); 12581 C->setAllocator(Record.readSubExpr()); 12582 unsigned NumVars = C->varlist_size(); 12583 SmallVector<Expr *, 16> Vars; 12584 Vars.reserve(NumVars); 12585 for (unsigned i = 0; i != NumVars; ++i) 12586 Vars.push_back(Record.readSubExpr()); 12587 C->setVarRefs(Vars); 12588 } 12589 12590 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12591 VisitOMPClauseWithPreInit(C); 12592 C->setNumTeams(Record.readSubExpr()); 12593 C->setLParenLoc(Record.readSourceLocation()); 12594 } 12595 12596 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12597 VisitOMPClauseWithPreInit(C); 12598 C->setThreadLimit(Record.readSubExpr()); 12599 C->setLParenLoc(Record.readSourceLocation()); 12600 } 12601 12602 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12603 VisitOMPClauseWithPreInit(C); 12604 C->setPriority(Record.readSubExpr()); 12605 C->setLParenLoc(Record.readSourceLocation()); 12606 } 12607 12608 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12609 VisitOMPClauseWithPreInit(C); 12610 C->setGrainsize(Record.readSubExpr()); 12611 C->setLParenLoc(Record.readSourceLocation()); 12612 } 12613 12614 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12615 VisitOMPClauseWithPreInit(C); 12616 C->setNumTasks(Record.readSubExpr()); 12617 C->setLParenLoc(Record.readSourceLocation()); 12618 } 12619 12620 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12621 C->setHint(Record.readSubExpr()); 12622 C->setLParenLoc(Record.readSourceLocation()); 12623 } 12624 12625 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12626 VisitOMPClauseWithPreInit(C); 12627 C->setDistScheduleKind( 12628 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12629 C->setChunkSize(Record.readSubExpr()); 12630 C->setLParenLoc(Record.readSourceLocation()); 12631 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12632 C->setCommaLoc(Record.readSourceLocation()); 12633 } 12634 12635 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12636 C->setDefaultmapKind( 12637 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12638 C->setDefaultmapModifier( 12639 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12640 C->setLParenLoc(Record.readSourceLocation()); 12641 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12642 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12643 } 12644 12645 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12646 C->setLParenLoc(Record.readSourceLocation()); 12647 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12648 C->setMotionModifier( 12649 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12650 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12651 } 12652 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12653 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12654 C->setColonLoc(Record.readSourceLocation()); 12655 auto NumVars = C->varlist_size(); 12656 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12657 auto TotalLists = C->getTotalComponentListNum(); 12658 auto TotalComponents = C->getTotalComponentsNum(); 12659 12660 SmallVector<Expr *, 16> Vars; 12661 Vars.reserve(NumVars); 12662 for (unsigned i = 0; i != NumVars; ++i) 12663 Vars.push_back(Record.readSubExpr()); 12664 C->setVarRefs(Vars); 12665 12666 SmallVector<Expr *, 16> UDMappers; 12667 UDMappers.reserve(NumVars); 12668 for (unsigned I = 0; I < NumVars; ++I) 12669 UDMappers.push_back(Record.readSubExpr()); 12670 C->setUDMapperRefs(UDMappers); 12671 12672 SmallVector<ValueDecl *, 16> Decls; 12673 Decls.reserve(UniqueDecls); 12674 for (unsigned i = 0; i < UniqueDecls; ++i) 12675 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12676 C->setUniqueDecls(Decls); 12677 12678 SmallVector<unsigned, 16> ListsPerDecl; 12679 ListsPerDecl.reserve(UniqueDecls); 12680 for (unsigned i = 0; i < UniqueDecls; ++i) 12681 ListsPerDecl.push_back(Record.readInt()); 12682 C->setDeclNumLists(ListsPerDecl); 12683 12684 SmallVector<unsigned, 32> ListSizes; 12685 ListSizes.reserve(TotalLists); 12686 for (unsigned i = 0; i < TotalLists; ++i) 12687 ListSizes.push_back(Record.readInt()); 12688 C->setComponentListSizes(ListSizes); 12689 12690 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12691 Components.reserve(TotalComponents); 12692 for (unsigned i = 0; i < TotalComponents; ++i) { 12693 Expr *AssociatedExprPr = Record.readSubExpr(); 12694 bool IsNonContiguous = Record.readBool(); 12695 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12696 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12697 } 12698 C->setComponents(Components, ListSizes); 12699 } 12700 12701 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12702 C->setLParenLoc(Record.readSourceLocation()); 12703 for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) { 12704 C->setMotionModifier( 12705 I, static_cast<OpenMPMotionModifierKind>(Record.readInt())); 12706 C->setMotionModifierLoc(I, Record.readSourceLocation()); 12707 } 12708 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12709 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12710 C->setColonLoc(Record.readSourceLocation()); 12711 auto NumVars = C->varlist_size(); 12712 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12713 auto TotalLists = C->getTotalComponentListNum(); 12714 auto TotalComponents = C->getTotalComponentsNum(); 12715 12716 SmallVector<Expr *, 16> Vars; 12717 Vars.reserve(NumVars); 12718 for (unsigned i = 0; i != NumVars; ++i) 12719 Vars.push_back(Record.readSubExpr()); 12720 C->setVarRefs(Vars); 12721 12722 SmallVector<Expr *, 16> UDMappers; 12723 UDMappers.reserve(NumVars); 12724 for (unsigned I = 0; I < NumVars; ++I) 12725 UDMappers.push_back(Record.readSubExpr()); 12726 C->setUDMapperRefs(UDMappers); 12727 12728 SmallVector<ValueDecl *, 16> Decls; 12729 Decls.reserve(UniqueDecls); 12730 for (unsigned i = 0; i < UniqueDecls; ++i) 12731 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12732 C->setUniqueDecls(Decls); 12733 12734 SmallVector<unsigned, 16> ListsPerDecl; 12735 ListsPerDecl.reserve(UniqueDecls); 12736 for (unsigned i = 0; i < UniqueDecls; ++i) 12737 ListsPerDecl.push_back(Record.readInt()); 12738 C->setDeclNumLists(ListsPerDecl); 12739 12740 SmallVector<unsigned, 32> ListSizes; 12741 ListSizes.reserve(TotalLists); 12742 for (unsigned i = 0; i < TotalLists; ++i) 12743 ListSizes.push_back(Record.readInt()); 12744 C->setComponentListSizes(ListSizes); 12745 12746 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12747 Components.reserve(TotalComponents); 12748 for (unsigned i = 0; i < TotalComponents; ++i) { 12749 Expr *AssociatedExprPr = Record.readSubExpr(); 12750 bool IsNonContiguous = Record.readBool(); 12751 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12752 Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous); 12753 } 12754 C->setComponents(Components, ListSizes); 12755 } 12756 12757 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12758 C->setLParenLoc(Record.readSourceLocation()); 12759 auto NumVars = C->varlist_size(); 12760 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12761 auto TotalLists = C->getTotalComponentListNum(); 12762 auto TotalComponents = C->getTotalComponentsNum(); 12763 12764 SmallVector<Expr *, 16> Vars; 12765 Vars.reserve(NumVars); 12766 for (unsigned i = 0; i != NumVars; ++i) 12767 Vars.push_back(Record.readSubExpr()); 12768 C->setVarRefs(Vars); 12769 Vars.clear(); 12770 for (unsigned i = 0; i != NumVars; ++i) 12771 Vars.push_back(Record.readSubExpr()); 12772 C->setPrivateCopies(Vars); 12773 Vars.clear(); 12774 for (unsigned i = 0; i != NumVars; ++i) 12775 Vars.push_back(Record.readSubExpr()); 12776 C->setInits(Vars); 12777 12778 SmallVector<ValueDecl *, 16> Decls; 12779 Decls.reserve(UniqueDecls); 12780 for (unsigned i = 0; i < UniqueDecls; ++i) 12781 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12782 C->setUniqueDecls(Decls); 12783 12784 SmallVector<unsigned, 16> ListsPerDecl; 12785 ListsPerDecl.reserve(UniqueDecls); 12786 for (unsigned i = 0; i < UniqueDecls; ++i) 12787 ListsPerDecl.push_back(Record.readInt()); 12788 C->setDeclNumLists(ListsPerDecl); 12789 12790 SmallVector<unsigned, 32> ListSizes; 12791 ListSizes.reserve(TotalLists); 12792 for (unsigned i = 0; i < TotalLists; ++i) 12793 ListSizes.push_back(Record.readInt()); 12794 C->setComponentListSizes(ListSizes); 12795 12796 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12797 Components.reserve(TotalComponents); 12798 for (unsigned i = 0; i < TotalComponents; ++i) { 12799 auto *AssociatedExprPr = Record.readSubExpr(); 12800 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12801 Components.emplace_back(AssociatedExprPr, AssociatedDecl, 12802 /*IsNonContiguous=*/false); 12803 } 12804 C->setComponents(Components, ListSizes); 12805 } 12806 12807 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) { 12808 C->setLParenLoc(Record.readSourceLocation()); 12809 auto NumVars = C->varlist_size(); 12810 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12811 auto TotalLists = C->getTotalComponentListNum(); 12812 auto TotalComponents = C->getTotalComponentsNum(); 12813 12814 SmallVector<Expr *, 16> Vars; 12815 Vars.reserve(NumVars); 12816 for (unsigned i = 0; i != NumVars; ++i) 12817 Vars.push_back(Record.readSubExpr()); 12818 C->setVarRefs(Vars); 12819 12820 SmallVector<ValueDecl *, 16> Decls; 12821 Decls.reserve(UniqueDecls); 12822 for (unsigned i = 0; i < UniqueDecls; ++i) 12823 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12824 C->setUniqueDecls(Decls); 12825 12826 SmallVector<unsigned, 16> ListsPerDecl; 12827 ListsPerDecl.reserve(UniqueDecls); 12828 for (unsigned i = 0; i < UniqueDecls; ++i) 12829 ListsPerDecl.push_back(Record.readInt()); 12830 C->setDeclNumLists(ListsPerDecl); 12831 12832 SmallVector<unsigned, 32> ListSizes; 12833 ListSizes.reserve(TotalLists); 12834 for (unsigned i = 0; i < TotalLists; ++i) 12835 ListSizes.push_back(Record.readInt()); 12836 C->setComponentListSizes(ListSizes); 12837 12838 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12839 Components.reserve(TotalComponents); 12840 for (unsigned i = 0; i < TotalComponents; ++i) { 12841 Expr *AssociatedExpr = Record.readSubExpr(); 12842 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12843 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12844 /*IsNonContiguous*/ false); 12845 } 12846 C->setComponents(Components, ListSizes); 12847 } 12848 12849 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12850 C->setLParenLoc(Record.readSourceLocation()); 12851 auto NumVars = C->varlist_size(); 12852 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12853 auto TotalLists = C->getTotalComponentListNum(); 12854 auto TotalComponents = C->getTotalComponentsNum(); 12855 12856 SmallVector<Expr *, 16> Vars; 12857 Vars.reserve(NumVars); 12858 for (unsigned i = 0; i != NumVars; ++i) 12859 Vars.push_back(Record.readSubExpr()); 12860 C->setVarRefs(Vars); 12861 Vars.clear(); 12862 12863 SmallVector<ValueDecl *, 16> Decls; 12864 Decls.reserve(UniqueDecls); 12865 for (unsigned i = 0; i < UniqueDecls; ++i) 12866 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12867 C->setUniqueDecls(Decls); 12868 12869 SmallVector<unsigned, 16> ListsPerDecl; 12870 ListsPerDecl.reserve(UniqueDecls); 12871 for (unsigned i = 0; i < UniqueDecls; ++i) 12872 ListsPerDecl.push_back(Record.readInt()); 12873 C->setDeclNumLists(ListsPerDecl); 12874 12875 SmallVector<unsigned, 32> ListSizes; 12876 ListSizes.reserve(TotalLists); 12877 for (unsigned i = 0; i < TotalLists; ++i) 12878 ListSizes.push_back(Record.readInt()); 12879 C->setComponentListSizes(ListSizes); 12880 12881 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12882 Components.reserve(TotalComponents); 12883 for (unsigned i = 0; i < TotalComponents; ++i) { 12884 Expr *AssociatedExpr = Record.readSubExpr(); 12885 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12886 Components.emplace_back(AssociatedExpr, AssociatedDecl, 12887 /*IsNonContiguous=*/false); 12888 } 12889 C->setComponents(Components, ListSizes); 12890 } 12891 12892 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12893 C->setLParenLoc(Record.readSourceLocation()); 12894 unsigned NumVars = C->varlist_size(); 12895 SmallVector<Expr *, 16> Vars; 12896 Vars.reserve(NumVars); 12897 for (unsigned i = 0; i != NumVars; ++i) 12898 Vars.push_back(Record.readSubExpr()); 12899 C->setVarRefs(Vars); 12900 Vars.clear(); 12901 Vars.reserve(NumVars); 12902 for (unsigned i = 0; i != NumVars; ++i) 12903 Vars.push_back(Record.readSubExpr()); 12904 C->setPrivateRefs(Vars); 12905 } 12906 12907 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12908 C->setLParenLoc(Record.readSourceLocation()); 12909 unsigned NumVars = C->varlist_size(); 12910 SmallVector<Expr *, 16> Vars; 12911 Vars.reserve(NumVars); 12912 for (unsigned i = 0; i != NumVars; ++i) 12913 Vars.push_back(Record.readSubExpr()); 12914 C->setVarRefs(Vars); 12915 } 12916 12917 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12918 C->setLParenLoc(Record.readSourceLocation()); 12919 unsigned NumVars = C->varlist_size(); 12920 SmallVector<Expr *, 16> Vars; 12921 Vars.reserve(NumVars); 12922 for (unsigned i = 0; i != NumVars; ++i) 12923 Vars.push_back(Record.readSubExpr()); 12924 C->setVarRefs(Vars); 12925 } 12926 12927 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) { 12928 C->setLParenLoc(Record.readSourceLocation()); 12929 unsigned NumOfAllocators = C->getNumberOfAllocators(); 12930 SmallVector<OMPUsesAllocatorsClause::Data, 4> Data; 12931 Data.reserve(NumOfAllocators); 12932 for (unsigned I = 0; I != NumOfAllocators; ++I) { 12933 OMPUsesAllocatorsClause::Data &D = Data.emplace_back(); 12934 D.Allocator = Record.readSubExpr(); 12935 D.AllocatorTraits = Record.readSubExpr(); 12936 D.LParenLoc = Record.readSourceLocation(); 12937 D.RParenLoc = Record.readSourceLocation(); 12938 } 12939 C->setAllocatorsData(Data); 12940 } 12941 12942 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) { 12943 C->setLParenLoc(Record.readSourceLocation()); 12944 C->setModifier(Record.readSubExpr()); 12945 C->setColonLoc(Record.readSourceLocation()); 12946 unsigned NumOfLocators = C->varlist_size(); 12947 SmallVector<Expr *, 4> Locators; 12948 Locators.reserve(NumOfLocators); 12949 for (unsigned I = 0; I != NumOfLocators; ++I) 12950 Locators.push_back(Record.readSubExpr()); 12951 C->setVarRefs(Locators); 12952 } 12953 12954 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12955 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12956 C->setLParenLoc(Record.readSourceLocation()); 12957 C->setKindKwLoc(Record.readSourceLocation()); 12958 } 12959 12960 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) { 12961 VisitOMPClauseWithPreInit(C); 12962 C->setThreadID(Record.readSubExpr()); 12963 C->setLParenLoc(Record.readSourceLocation()); 12964 } 12965 12966 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12967 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12968 TI.Sets.resize(readUInt32()); 12969 for (auto &Set : TI.Sets) { 12970 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12971 Set.Selectors.resize(readUInt32()); 12972 for (auto &Selector : Set.Selectors) { 12973 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12974 Selector.ScoreOrCondition = nullptr; 12975 if (readBool()) 12976 Selector.ScoreOrCondition = readExprRef(); 12977 Selector.Properties.resize(readUInt32()); 12978 for (auto &Property : Selector.Properties) 12979 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12980 } 12981 } 12982 return &TI; 12983 } 12984 12985 void ASTRecordReader::readOMPChildren(OMPChildren *Data) { 12986 if (!Data) 12987 return; 12988 if (Reader->ReadingKind == ASTReader::Read_Stmt) { 12989 // Skip NumClauses, NumChildren and HasAssociatedStmt fields. 12990 skipInts(3); 12991 } 12992 SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses()); 12993 for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I) 12994 Clauses[I] = readOMPClause(); 12995 Data->setClauses(Clauses); 12996 if (Data->hasAssociatedStmt()) 12997 Data->setAssociatedStmt(readStmt()); 12998 for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I) 12999 Data->getChildren()[I] = readStmt(); 13000 } 13001