1 //===- ASTReader.cpp - AST File Reader ------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the ASTReader class, which reads AST files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/OpenMPKinds.h" 14 #include "clang/Serialization/ASTRecordReader.h" 15 #include "ASTCommon.h" 16 #include "ASTReaderInternals.h" 17 #include "clang/AST/AbstractTypeReader.h" 18 #include "clang/AST/ASTConsumer.h" 19 #include "clang/AST/ASTContext.h" 20 #include "clang/AST/ASTMutationListener.h" 21 #include "clang/AST/ASTUnresolvedSet.h" 22 #include "clang/AST/Decl.h" 23 #include "clang/AST/DeclBase.h" 24 #include "clang/AST/DeclCXX.h" 25 #include "clang/AST/DeclFriend.h" 26 #include "clang/AST/DeclGroup.h" 27 #include "clang/AST/DeclObjC.h" 28 #include "clang/AST/DeclTemplate.h" 29 #include "clang/AST/DeclarationName.h" 30 #include "clang/AST/Expr.h" 31 #include "clang/AST/ExprCXX.h" 32 #include "clang/AST/ExternalASTSource.h" 33 #include "clang/AST/NestedNameSpecifier.h" 34 #include "clang/AST/OpenMPClause.h" 35 #include "clang/AST/ODRHash.h" 36 #include "clang/AST/RawCommentList.h" 37 #include "clang/AST/TemplateBase.h" 38 #include "clang/AST/TemplateName.h" 39 #include "clang/AST/Type.h" 40 #include "clang/AST/TypeLoc.h" 41 #include "clang/AST/TypeLocVisitor.h" 42 #include "clang/AST/UnresolvedSet.h" 43 #include "clang/Basic/CommentOptions.h" 44 #include "clang/Basic/Diagnostic.h" 45 #include "clang/Basic/DiagnosticOptions.h" 46 #include "clang/Basic/ExceptionSpecificationType.h" 47 #include "clang/Basic/FileManager.h" 48 #include "clang/Basic/FileSystemOptions.h" 49 #include "clang/Basic/IdentifierTable.h" 50 #include "clang/Basic/LLVM.h" 51 #include "clang/Basic/LangOptions.h" 52 #include "clang/Basic/Module.h" 53 #include "clang/Basic/ObjCRuntime.h" 54 #include "clang/Basic/OperatorKinds.h" 55 #include "clang/Basic/PragmaKinds.h" 56 #include "clang/Basic/Sanitizers.h" 57 #include "clang/Basic/SourceLocation.h" 58 #include "clang/Basic/SourceManager.h" 59 #include "clang/Basic/SourceManagerInternals.h" 60 #include "clang/Basic/Specifiers.h" 61 #include "clang/Basic/TargetInfo.h" 62 #include "clang/Basic/TargetOptions.h" 63 #include "clang/Basic/TokenKinds.h" 64 #include "clang/Basic/Version.h" 65 #include "clang/Lex/HeaderSearch.h" 66 #include "clang/Lex/HeaderSearchOptions.h" 67 #include "clang/Lex/MacroInfo.h" 68 #include "clang/Lex/ModuleMap.h" 69 #include "clang/Lex/PreprocessingRecord.h" 70 #include "clang/Lex/Preprocessor.h" 71 #include "clang/Lex/PreprocessorOptions.h" 72 #include "clang/Lex/Token.h" 73 #include "clang/Sema/ObjCMethodList.h" 74 #include "clang/Sema/Scope.h" 75 #include "clang/Sema/Sema.h" 76 #include "clang/Sema/Weak.h" 77 #include "clang/Serialization/ASTBitCodes.h" 78 #include "clang/Serialization/ASTDeserializationListener.h" 79 #include "clang/Serialization/ContinuousRangeMap.h" 80 #include "clang/Serialization/GlobalModuleIndex.h" 81 #include "clang/Serialization/InMemoryModuleCache.h" 82 #include "clang/Serialization/ModuleFile.h" 83 #include "clang/Serialization/ModuleFileExtension.h" 84 #include "clang/Serialization/ModuleManager.h" 85 #include "clang/Serialization/PCHContainerOperations.h" 86 #include "clang/Serialization/SerializationDiagnostic.h" 87 #include "llvm/ADT/APFloat.h" 88 #include "llvm/ADT/APInt.h" 89 #include "llvm/ADT/APSInt.h" 90 #include "llvm/ADT/ArrayRef.h" 91 #include "llvm/ADT/DenseMap.h" 92 #include "llvm/ADT/FoldingSet.h" 93 #include "llvm/ADT/Hashing.h" 94 #include "llvm/ADT/IntrusiveRefCntPtr.h" 95 #include "llvm/ADT/None.h" 96 #include "llvm/ADT/Optional.h" 97 #include "llvm/ADT/STLExtras.h" 98 #include "llvm/ADT/ScopeExit.h" 99 #include "llvm/ADT/SmallPtrSet.h" 100 #include "llvm/ADT/SmallString.h" 101 #include "llvm/ADT/SmallVector.h" 102 #include "llvm/ADT/StringExtras.h" 103 #include "llvm/ADT/StringMap.h" 104 #include "llvm/ADT/StringRef.h" 105 #include "llvm/ADT/Triple.h" 106 #include "llvm/ADT/iterator_range.h" 107 #include "llvm/Bitstream/BitstreamReader.h" 108 #include "llvm/Support/Casting.h" 109 #include "llvm/Support/Compiler.h" 110 #include "llvm/Support/Compression.h" 111 #include "llvm/Support/DJB.h" 112 #include "llvm/Support/Endian.h" 113 #include "llvm/Support/Error.h" 114 #include "llvm/Support/ErrorHandling.h" 115 #include "llvm/Support/FileSystem.h" 116 #include "llvm/Support/MemoryBuffer.h" 117 #include "llvm/Support/Path.h" 118 #include "llvm/Support/SaveAndRestore.h" 119 #include "llvm/Support/Timer.h" 120 #include "llvm/Support/VersionTuple.h" 121 #include "llvm/Support/raw_ostream.h" 122 #include <algorithm> 123 #include <cassert> 124 #include <cstddef> 125 #include <cstdint> 126 #include <cstdio> 127 #include <ctime> 128 #include <iterator> 129 #include <limits> 130 #include <map> 131 #include <memory> 132 #include <string> 133 #include <system_error> 134 #include <tuple> 135 #include <utility> 136 #include <vector> 137 138 using namespace clang; 139 using namespace clang::serialization; 140 using namespace clang::serialization::reader; 141 using llvm::BitstreamCursor; 142 143 //===----------------------------------------------------------------------===// 144 // ChainedASTReaderListener implementation 145 //===----------------------------------------------------------------------===// 146 147 bool 148 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) { 149 return First->ReadFullVersionInformation(FullVersion) || 150 Second->ReadFullVersionInformation(FullVersion); 151 } 152 153 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) { 154 First->ReadModuleName(ModuleName); 155 Second->ReadModuleName(ModuleName); 156 } 157 158 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) { 159 First->ReadModuleMapFile(ModuleMapPath); 160 Second->ReadModuleMapFile(ModuleMapPath); 161 } 162 163 bool 164 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts, 165 bool Complain, 166 bool AllowCompatibleDifferences) { 167 return First->ReadLanguageOptions(LangOpts, Complain, 168 AllowCompatibleDifferences) || 169 Second->ReadLanguageOptions(LangOpts, Complain, 170 AllowCompatibleDifferences); 171 } 172 173 bool ChainedASTReaderListener::ReadTargetOptions( 174 const TargetOptions &TargetOpts, bool Complain, 175 bool AllowCompatibleDifferences) { 176 return First->ReadTargetOptions(TargetOpts, Complain, 177 AllowCompatibleDifferences) || 178 Second->ReadTargetOptions(TargetOpts, Complain, 179 AllowCompatibleDifferences); 180 } 181 182 bool ChainedASTReaderListener::ReadDiagnosticOptions( 183 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 184 return First->ReadDiagnosticOptions(DiagOpts, Complain) || 185 Second->ReadDiagnosticOptions(DiagOpts, Complain); 186 } 187 188 bool 189 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts, 190 bool Complain) { 191 return First->ReadFileSystemOptions(FSOpts, Complain) || 192 Second->ReadFileSystemOptions(FSOpts, Complain); 193 } 194 195 bool ChainedASTReaderListener::ReadHeaderSearchOptions( 196 const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath, 197 bool Complain) { 198 return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 199 Complain) || 200 Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 201 Complain); 202 } 203 204 bool ChainedASTReaderListener::ReadPreprocessorOptions( 205 const PreprocessorOptions &PPOpts, bool Complain, 206 std::string &SuggestedPredefines) { 207 return First->ReadPreprocessorOptions(PPOpts, Complain, 208 SuggestedPredefines) || 209 Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines); 210 } 211 212 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M, 213 unsigned Value) { 214 First->ReadCounter(M, Value); 215 Second->ReadCounter(M, Value); 216 } 217 218 bool ChainedASTReaderListener::needsInputFileVisitation() { 219 return First->needsInputFileVisitation() || 220 Second->needsInputFileVisitation(); 221 } 222 223 bool ChainedASTReaderListener::needsSystemInputFileVisitation() { 224 return First->needsSystemInputFileVisitation() || 225 Second->needsSystemInputFileVisitation(); 226 } 227 228 void ChainedASTReaderListener::visitModuleFile(StringRef Filename, 229 ModuleKind Kind) { 230 First->visitModuleFile(Filename, Kind); 231 Second->visitModuleFile(Filename, Kind); 232 } 233 234 bool ChainedASTReaderListener::visitInputFile(StringRef Filename, 235 bool isSystem, 236 bool isOverridden, 237 bool isExplicitModule) { 238 bool Continue = false; 239 if (First->needsInputFileVisitation() && 240 (!isSystem || First->needsSystemInputFileVisitation())) 241 Continue |= First->visitInputFile(Filename, isSystem, isOverridden, 242 isExplicitModule); 243 if (Second->needsInputFileVisitation() && 244 (!isSystem || Second->needsSystemInputFileVisitation())) 245 Continue |= Second->visitInputFile(Filename, isSystem, isOverridden, 246 isExplicitModule); 247 return Continue; 248 } 249 250 void ChainedASTReaderListener::readModuleFileExtension( 251 const ModuleFileExtensionMetadata &Metadata) { 252 First->readModuleFileExtension(Metadata); 253 Second->readModuleFileExtension(Metadata); 254 } 255 256 //===----------------------------------------------------------------------===// 257 // PCH validator implementation 258 //===----------------------------------------------------------------------===// 259 260 ASTReaderListener::~ASTReaderListener() = default; 261 262 /// Compare the given set of language options against an existing set of 263 /// language options. 264 /// 265 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 266 /// \param AllowCompatibleDifferences If true, differences between compatible 267 /// language options will be permitted. 268 /// 269 /// \returns true if the languagae options mis-match, false otherwise. 270 static bool checkLanguageOptions(const LangOptions &LangOpts, 271 const LangOptions &ExistingLangOpts, 272 DiagnosticsEngine *Diags, 273 bool AllowCompatibleDifferences = true) { 274 #define LANGOPT(Name, Bits, Default, Description) \ 275 if (ExistingLangOpts.Name != LangOpts.Name) { \ 276 if (Diags) \ 277 Diags->Report(diag::err_pch_langopt_mismatch) \ 278 << Description << LangOpts.Name << ExistingLangOpts.Name; \ 279 return true; \ 280 } 281 282 #define VALUE_LANGOPT(Name, Bits, Default, Description) \ 283 if (ExistingLangOpts.Name != LangOpts.Name) { \ 284 if (Diags) \ 285 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 286 << Description; \ 287 return true; \ 288 } 289 290 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 291 if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) { \ 292 if (Diags) \ 293 Diags->Report(diag::err_pch_langopt_value_mismatch) \ 294 << Description; \ 295 return true; \ 296 } 297 298 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description) \ 299 if (!AllowCompatibleDifferences) \ 300 LANGOPT(Name, Bits, Default, Description) 301 302 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description) \ 303 if (!AllowCompatibleDifferences) \ 304 ENUM_LANGOPT(Name, Bits, Default, Description) 305 306 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \ 307 if (!AllowCompatibleDifferences) \ 308 VALUE_LANGOPT(Name, Bits, Default, Description) 309 310 #define BENIGN_LANGOPT(Name, Bits, Default, Description) 311 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description) 312 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description) 313 #include "clang/Basic/LangOptions.def" 314 315 if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) { 316 if (Diags) 317 Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features"; 318 return true; 319 } 320 321 if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) { 322 if (Diags) 323 Diags->Report(diag::err_pch_langopt_value_mismatch) 324 << "target Objective-C runtime"; 325 return true; 326 } 327 328 if (ExistingLangOpts.CommentOpts.BlockCommandNames != 329 LangOpts.CommentOpts.BlockCommandNames) { 330 if (Diags) 331 Diags->Report(diag::err_pch_langopt_value_mismatch) 332 << "block command names"; 333 return true; 334 } 335 336 // Sanitizer feature mismatches are treated as compatible differences. If 337 // compatible differences aren't allowed, we still only want to check for 338 // mismatches of non-modular sanitizers (the only ones which can affect AST 339 // generation). 340 if (!AllowCompatibleDifferences) { 341 SanitizerMask ModularSanitizers = getPPTransparentSanitizers(); 342 SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize; 343 SanitizerSet ImportedSanitizers = LangOpts.Sanitize; 344 ExistingSanitizers.clear(ModularSanitizers); 345 ImportedSanitizers.clear(ModularSanitizers); 346 if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) { 347 const std::string Flag = "-fsanitize="; 348 if (Diags) { 349 #define SANITIZER(NAME, ID) \ 350 { \ 351 bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID); \ 352 bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID); \ 353 if (InExistingModule != InImportedModule) \ 354 Diags->Report(diag::err_pch_targetopt_feature_mismatch) \ 355 << InExistingModule << (Flag + NAME); \ 356 } 357 #include "clang/Basic/Sanitizers.def" 358 } 359 return true; 360 } 361 } 362 363 return false; 364 } 365 366 /// Compare the given set of target options against an existing set of 367 /// target options. 368 /// 369 /// \param Diags If non-NULL, diagnostics will be emitted via this engine. 370 /// 371 /// \returns true if the target options mis-match, false otherwise. 372 static bool checkTargetOptions(const TargetOptions &TargetOpts, 373 const TargetOptions &ExistingTargetOpts, 374 DiagnosticsEngine *Diags, 375 bool AllowCompatibleDifferences = true) { 376 #define CHECK_TARGET_OPT(Field, Name) \ 377 if (TargetOpts.Field != ExistingTargetOpts.Field) { \ 378 if (Diags) \ 379 Diags->Report(diag::err_pch_targetopt_mismatch) \ 380 << Name << TargetOpts.Field << ExistingTargetOpts.Field; \ 381 return true; \ 382 } 383 384 // The triple and ABI must match exactly. 385 CHECK_TARGET_OPT(Triple, "target"); 386 CHECK_TARGET_OPT(ABI, "target ABI"); 387 388 // We can tolerate different CPUs in many cases, notably when one CPU 389 // supports a strict superset of another. When allowing compatible 390 // differences skip this check. 391 if (!AllowCompatibleDifferences) 392 CHECK_TARGET_OPT(CPU, "target CPU"); 393 394 #undef CHECK_TARGET_OPT 395 396 // Compare feature sets. 397 SmallVector<StringRef, 4> ExistingFeatures( 398 ExistingTargetOpts.FeaturesAsWritten.begin(), 399 ExistingTargetOpts.FeaturesAsWritten.end()); 400 SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(), 401 TargetOpts.FeaturesAsWritten.end()); 402 llvm::sort(ExistingFeatures); 403 llvm::sort(ReadFeatures); 404 405 // We compute the set difference in both directions explicitly so that we can 406 // diagnose the differences differently. 407 SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures; 408 std::set_difference( 409 ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(), 410 ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures)); 411 std::set_difference(ReadFeatures.begin(), ReadFeatures.end(), 412 ExistingFeatures.begin(), ExistingFeatures.end(), 413 std::back_inserter(UnmatchedReadFeatures)); 414 415 // If we are allowing compatible differences and the read feature set is 416 // a strict subset of the existing feature set, there is nothing to diagnose. 417 if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty()) 418 return false; 419 420 if (Diags) { 421 for (StringRef Feature : UnmatchedReadFeatures) 422 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 423 << /* is-existing-feature */ false << Feature; 424 for (StringRef Feature : UnmatchedExistingFeatures) 425 Diags->Report(diag::err_pch_targetopt_feature_mismatch) 426 << /* is-existing-feature */ true << Feature; 427 } 428 429 return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty(); 430 } 431 432 bool 433 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, 434 bool Complain, 435 bool AllowCompatibleDifferences) { 436 const LangOptions &ExistingLangOpts = PP.getLangOpts(); 437 return checkLanguageOptions(LangOpts, ExistingLangOpts, 438 Complain ? &Reader.Diags : nullptr, 439 AllowCompatibleDifferences); 440 } 441 442 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, 443 bool Complain, 444 bool AllowCompatibleDifferences) { 445 const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts(); 446 return checkTargetOptions(TargetOpts, ExistingTargetOpts, 447 Complain ? &Reader.Diags : nullptr, 448 AllowCompatibleDifferences); 449 } 450 451 namespace { 452 453 using MacroDefinitionsMap = 454 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>; 455 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>; 456 457 } // namespace 458 459 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags, 460 DiagnosticsEngine &Diags, 461 bool Complain) { 462 using Level = DiagnosticsEngine::Level; 463 464 // Check current mappings for new -Werror mappings, and the stored mappings 465 // for cases that were explicitly mapped to *not* be errors that are now 466 // errors because of options like -Werror. 467 DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags }; 468 469 for (DiagnosticsEngine *MappingSource : MappingSources) { 470 for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) { 471 diag::kind DiagID = DiagIDMappingPair.first; 472 Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation()); 473 if (CurLevel < DiagnosticsEngine::Error) 474 continue; // not significant 475 Level StoredLevel = 476 StoredDiags.getDiagnosticLevel(DiagID, SourceLocation()); 477 if (StoredLevel < DiagnosticsEngine::Error) { 478 if (Complain) 479 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" + 480 Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str(); 481 return true; 482 } 483 } 484 } 485 486 return false; 487 } 488 489 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) { 490 diag::Severity Ext = Diags.getExtensionHandlingBehavior(); 491 if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors()) 492 return true; 493 return Ext >= diag::Severity::Error; 494 } 495 496 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags, 497 DiagnosticsEngine &Diags, 498 bool IsSystem, bool Complain) { 499 // Top-level options 500 if (IsSystem) { 501 if (Diags.getSuppressSystemWarnings()) 502 return false; 503 // If -Wsystem-headers was not enabled before, be conservative 504 if (StoredDiags.getSuppressSystemWarnings()) { 505 if (Complain) 506 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers"; 507 return true; 508 } 509 } 510 511 if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) { 512 if (Complain) 513 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror"; 514 return true; 515 } 516 517 if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() && 518 !StoredDiags.getEnableAllWarnings()) { 519 if (Complain) 520 Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror"; 521 return true; 522 } 523 524 if (isExtHandlingFromDiagsError(Diags) && 525 !isExtHandlingFromDiagsError(StoredDiags)) { 526 if (Complain) 527 Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors"; 528 return true; 529 } 530 531 return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain); 532 } 533 534 /// Return the top import module if it is implicit, nullptr otherwise. 535 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 536 Preprocessor &PP) { 537 // If the original import came from a file explicitly generated by the user, 538 // don't check the diagnostic mappings. 539 // FIXME: currently this is approximated by checking whether this is not a 540 // module import of an implicitly-loaded module file. 541 // Note: ModuleMgr.rbegin() may not be the current module, but it must be in 542 // the transitive closure of its imports, since unrelated modules cannot be 543 // imported until after this module finishes validation. 544 ModuleFile *TopImport = &*ModuleMgr.rbegin(); 545 while (!TopImport->ImportedBy.empty()) 546 TopImport = TopImport->ImportedBy[0]; 547 if (TopImport->Kind != MK_ImplicitModule) 548 return nullptr; 549 550 StringRef ModuleName = TopImport->ModuleName; 551 assert(!ModuleName.empty() && "diagnostic options read before module name"); 552 553 Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); 554 assert(M && "missing module"); 555 return M; 556 } 557 558 bool PCHValidator::ReadDiagnosticOptions( 559 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) { 560 DiagnosticsEngine &ExistingDiags = PP.getDiagnostics(); 561 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs()); 562 IntrusiveRefCntPtr<DiagnosticsEngine> Diags( 563 new DiagnosticsEngine(DiagIDs, DiagOpts.get())); 564 // This should never fail, because we would have processed these options 565 // before writing them to an ASTFile. 566 ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false); 567 568 ModuleManager &ModuleMgr = Reader.getModuleManager(); 569 assert(ModuleMgr.size() >= 1 && "what ASTFile is this then"); 570 571 Module *TopM = getTopImportImplicitModule(ModuleMgr, PP); 572 if (!TopM) 573 return false; 574 575 // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that 576 // contains the union of their flags. 577 return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem, 578 Complain); 579 } 580 581 /// Collect the macro definitions provided by the given preprocessor 582 /// options. 583 static void 584 collectMacroDefinitions(const PreprocessorOptions &PPOpts, 585 MacroDefinitionsMap &Macros, 586 SmallVectorImpl<StringRef> *MacroNames = nullptr) { 587 for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) { 588 StringRef Macro = PPOpts.Macros[I].first; 589 bool IsUndef = PPOpts.Macros[I].second; 590 591 std::pair<StringRef, StringRef> MacroPair = Macro.split('='); 592 StringRef MacroName = MacroPair.first; 593 StringRef MacroBody = MacroPair.second; 594 595 // For an #undef'd macro, we only care about the name. 596 if (IsUndef) { 597 if (MacroNames && !Macros.count(MacroName)) 598 MacroNames->push_back(MacroName); 599 600 Macros[MacroName] = std::make_pair("", true); 601 continue; 602 } 603 604 // For a #define'd macro, figure out the actual definition. 605 if (MacroName.size() == Macro.size()) 606 MacroBody = "1"; 607 else { 608 // Note: GCC drops anything following an end-of-line character. 609 StringRef::size_type End = MacroBody.find_first_of("\n\r"); 610 MacroBody = MacroBody.substr(0, End); 611 } 612 613 if (MacroNames && !Macros.count(MacroName)) 614 MacroNames->push_back(MacroName); 615 Macros[MacroName] = std::make_pair(MacroBody, false); 616 } 617 } 618 619 /// Check the preprocessor options deserialized from the control block 620 /// against the preprocessor options in an existing preprocessor. 621 /// 622 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 623 /// \param Validate If true, validate preprocessor options. If false, allow 624 /// macros defined by \p ExistingPPOpts to override those defined by 625 /// \p PPOpts in SuggestedPredefines. 626 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts, 627 const PreprocessorOptions &ExistingPPOpts, 628 DiagnosticsEngine *Diags, 629 FileManager &FileMgr, 630 std::string &SuggestedPredefines, 631 const LangOptions &LangOpts, 632 bool Validate = true) { 633 // Check macro definitions. 634 MacroDefinitionsMap ASTFileMacros; 635 collectMacroDefinitions(PPOpts, ASTFileMacros); 636 MacroDefinitionsMap ExistingMacros; 637 SmallVector<StringRef, 4> ExistingMacroNames; 638 collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames); 639 640 for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) { 641 // Dig out the macro definition in the existing preprocessor options. 642 StringRef MacroName = ExistingMacroNames[I]; 643 std::pair<StringRef, bool> Existing = ExistingMacros[MacroName]; 644 645 // Check whether we know anything about this macro name or not. 646 llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known = 647 ASTFileMacros.find(MacroName); 648 if (!Validate || Known == ASTFileMacros.end()) { 649 // FIXME: Check whether this identifier was referenced anywhere in the 650 // AST file. If so, we should reject the AST file. Unfortunately, this 651 // information isn't in the control block. What shall we do about it? 652 653 if (Existing.second) { 654 SuggestedPredefines += "#undef "; 655 SuggestedPredefines += MacroName.str(); 656 SuggestedPredefines += '\n'; 657 } else { 658 SuggestedPredefines += "#define "; 659 SuggestedPredefines += MacroName.str(); 660 SuggestedPredefines += ' '; 661 SuggestedPredefines += Existing.first.str(); 662 SuggestedPredefines += '\n'; 663 } 664 continue; 665 } 666 667 // If the macro was defined in one but undef'd in the other, we have a 668 // conflict. 669 if (Existing.second != Known->second.second) { 670 if (Diags) { 671 Diags->Report(diag::err_pch_macro_def_undef) 672 << MacroName << Known->second.second; 673 } 674 return true; 675 } 676 677 // If the macro was #undef'd in both, or if the macro bodies are identical, 678 // it's fine. 679 if (Existing.second || Existing.first == Known->second.first) 680 continue; 681 682 // The macro bodies differ; complain. 683 if (Diags) { 684 Diags->Report(diag::err_pch_macro_def_conflict) 685 << MacroName << Known->second.first << Existing.first; 686 } 687 return true; 688 } 689 690 // Check whether we're using predefines. 691 if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) { 692 if (Diags) { 693 Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines; 694 } 695 return true; 696 } 697 698 // Detailed record is important since it is used for the module cache hash. 699 if (LangOpts.Modules && 700 PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) { 701 if (Diags) { 702 Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord; 703 } 704 return true; 705 } 706 707 // Compute the #include and #include_macros lines we need. 708 for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) { 709 StringRef File = ExistingPPOpts.Includes[I]; 710 711 if (!ExistingPPOpts.ImplicitPCHInclude.empty() && 712 !ExistingPPOpts.PCHThroughHeader.empty()) { 713 // In case the through header is an include, we must add all the includes 714 // to the predefines so the start point can be determined. 715 SuggestedPredefines += "#include \""; 716 SuggestedPredefines += File; 717 SuggestedPredefines += "\"\n"; 718 continue; 719 } 720 721 if (File == ExistingPPOpts.ImplicitPCHInclude) 722 continue; 723 724 if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File) 725 != PPOpts.Includes.end()) 726 continue; 727 728 SuggestedPredefines += "#include \""; 729 SuggestedPredefines += File; 730 SuggestedPredefines += "\"\n"; 731 } 732 733 for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) { 734 StringRef File = ExistingPPOpts.MacroIncludes[I]; 735 if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(), 736 File) 737 != PPOpts.MacroIncludes.end()) 738 continue; 739 740 SuggestedPredefines += "#__include_macros \""; 741 SuggestedPredefines += File; 742 SuggestedPredefines += "\"\n##\n"; 743 } 744 745 return false; 746 } 747 748 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 749 bool Complain, 750 std::string &SuggestedPredefines) { 751 const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts(); 752 753 return checkPreprocessorOptions(PPOpts, ExistingPPOpts, 754 Complain? &Reader.Diags : nullptr, 755 PP.getFileManager(), 756 SuggestedPredefines, 757 PP.getLangOpts()); 758 } 759 760 bool SimpleASTReaderListener::ReadPreprocessorOptions( 761 const PreprocessorOptions &PPOpts, 762 bool Complain, 763 std::string &SuggestedPredefines) { 764 return checkPreprocessorOptions(PPOpts, 765 PP.getPreprocessorOpts(), 766 nullptr, 767 PP.getFileManager(), 768 SuggestedPredefines, 769 PP.getLangOpts(), 770 false); 771 } 772 773 /// Check the header search options deserialized from the control block 774 /// against the header search options in an existing preprocessor. 775 /// 776 /// \param Diags If non-null, produce diagnostics for any mismatches incurred. 777 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 778 StringRef SpecificModuleCachePath, 779 StringRef ExistingModuleCachePath, 780 DiagnosticsEngine *Diags, 781 const LangOptions &LangOpts) { 782 if (LangOpts.Modules) { 783 if (SpecificModuleCachePath != ExistingModuleCachePath) { 784 if (Diags) 785 Diags->Report(diag::err_pch_modulecache_mismatch) 786 << SpecificModuleCachePath << ExistingModuleCachePath; 787 return true; 788 } 789 } 790 791 return false; 792 } 793 794 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 795 StringRef SpecificModuleCachePath, 796 bool Complain) { 797 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 798 PP.getHeaderSearchInfo().getModuleCachePath(), 799 Complain ? &Reader.Diags : nullptr, 800 PP.getLangOpts()); 801 } 802 803 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) { 804 PP.setCounterValue(Value); 805 } 806 807 //===----------------------------------------------------------------------===// 808 // AST reader implementation 809 //===----------------------------------------------------------------------===// 810 811 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener, 812 bool TakeOwnership) { 813 DeserializationListener = Listener; 814 OwnsDeserializationListener = TakeOwnership; 815 } 816 817 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) { 818 return serialization::ComputeHash(Sel); 819 } 820 821 std::pair<unsigned, unsigned> 822 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) { 823 using namespace llvm::support; 824 825 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 826 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 827 return std::make_pair(KeyLen, DataLen); 828 } 829 830 ASTSelectorLookupTrait::internal_key_type 831 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) { 832 using namespace llvm::support; 833 834 SelectorTable &SelTable = Reader.getContext().Selectors; 835 unsigned N = endian::readNext<uint16_t, little, unaligned>(d); 836 IdentifierInfo *FirstII = Reader.getLocalIdentifier( 837 F, endian::readNext<uint32_t, little, unaligned>(d)); 838 if (N == 0) 839 return SelTable.getNullarySelector(FirstII); 840 else if (N == 1) 841 return SelTable.getUnarySelector(FirstII); 842 843 SmallVector<IdentifierInfo *, 16> Args; 844 Args.push_back(FirstII); 845 for (unsigned I = 1; I != N; ++I) 846 Args.push_back(Reader.getLocalIdentifier( 847 F, endian::readNext<uint32_t, little, unaligned>(d))); 848 849 return SelTable.getSelector(N, Args.data()); 850 } 851 852 ASTSelectorLookupTrait::data_type 853 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d, 854 unsigned DataLen) { 855 using namespace llvm::support; 856 857 data_type Result; 858 859 Result.ID = Reader.getGlobalSelectorID( 860 F, endian::readNext<uint32_t, little, unaligned>(d)); 861 unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d); 862 unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d); 863 Result.InstanceBits = FullInstanceBits & 0x3; 864 Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1; 865 Result.FactoryBits = FullFactoryBits & 0x3; 866 Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1; 867 unsigned NumInstanceMethods = FullInstanceBits >> 3; 868 unsigned NumFactoryMethods = FullFactoryBits >> 3; 869 870 // Load instance methods 871 for (unsigned I = 0; I != NumInstanceMethods; ++I) { 872 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 873 F, endian::readNext<uint32_t, little, unaligned>(d))) 874 Result.Instance.push_back(Method); 875 } 876 877 // Load factory methods 878 for (unsigned I = 0; I != NumFactoryMethods; ++I) { 879 if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>( 880 F, endian::readNext<uint32_t, little, unaligned>(d))) 881 Result.Factory.push_back(Method); 882 } 883 884 return Result; 885 } 886 887 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) { 888 return llvm::djbHash(a); 889 } 890 891 std::pair<unsigned, unsigned> 892 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) { 893 using namespace llvm::support; 894 895 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 896 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 897 return std::make_pair(KeyLen, DataLen); 898 } 899 900 ASTIdentifierLookupTraitBase::internal_key_type 901 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) { 902 assert(n >= 2 && d[n-1] == '\0'); 903 return StringRef((const char*) d, n-1); 904 } 905 906 /// Whether the given identifier is "interesting". 907 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II, 908 bool IsModule) { 909 return II.hadMacroDefinition() || 910 II.isPoisoned() || 911 (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) || 912 II.hasRevertedTokenIDToIdentifier() || 913 (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) && 914 II.getFETokenInfo()); 915 } 916 917 static bool readBit(unsigned &Bits) { 918 bool Value = Bits & 0x1; 919 Bits >>= 1; 920 return Value; 921 } 922 923 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) { 924 using namespace llvm::support; 925 926 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 927 return Reader.getGlobalIdentifierID(F, RawID >> 1); 928 } 929 930 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) { 931 if (!II.isFromAST()) { 932 II.setIsFromAST(); 933 bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr; 934 if (isInterestingIdentifier(Reader, II, IsModule)) 935 II.setChangedSinceDeserialization(); 936 } 937 } 938 939 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, 940 const unsigned char* d, 941 unsigned DataLen) { 942 using namespace llvm::support; 943 944 unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 945 bool IsInteresting = RawID & 0x01; 946 947 // Wipe out the "is interesting" bit. 948 RawID = RawID >> 1; 949 950 // Build the IdentifierInfo and link the identifier ID with it. 951 IdentifierInfo *II = KnownII; 952 if (!II) { 953 II = &Reader.getIdentifierTable().getOwn(k); 954 KnownII = II; 955 } 956 markIdentifierFromAST(Reader, *II); 957 Reader.markIdentifierUpToDate(II); 958 959 IdentID ID = Reader.getGlobalIdentifierID(F, RawID); 960 if (!IsInteresting) { 961 // For uninteresting identifiers, there's nothing else to do. Just notify 962 // the reader that we've finished loading this identifier. 963 Reader.SetIdentifierInfo(ID, II); 964 return II; 965 } 966 967 unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d); 968 unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d); 969 bool CPlusPlusOperatorKeyword = readBit(Bits); 970 bool HasRevertedTokenIDToIdentifier = readBit(Bits); 971 bool HasRevertedBuiltin = readBit(Bits); 972 bool Poisoned = readBit(Bits); 973 bool ExtensionToken = readBit(Bits); 974 bool HadMacroDefinition = readBit(Bits); 975 976 assert(Bits == 0 && "Extra bits in the identifier?"); 977 DataLen -= 8; 978 979 // Set or check the various bits in the IdentifierInfo structure. 980 // Token IDs are read-only. 981 if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier) 982 II->revertTokenIDToIdentifier(); 983 if (!F.isModule()) 984 II->setObjCOrBuiltinID(ObjCOrBuiltinID); 985 else if (HasRevertedBuiltin && II->getBuiltinID()) { 986 II->revertBuiltin(); 987 assert((II->hasRevertedBuiltin() || 988 II->getObjCOrBuiltinID() == ObjCOrBuiltinID) && 989 "Incorrect ObjC keyword or builtin ID"); 990 } 991 assert(II->isExtensionToken() == ExtensionToken && 992 "Incorrect extension token flag"); 993 (void)ExtensionToken; 994 if (Poisoned) 995 II->setIsPoisoned(true); 996 assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword && 997 "Incorrect C++ operator keyword flag"); 998 (void)CPlusPlusOperatorKeyword; 999 1000 // If this identifier is a macro, deserialize the macro 1001 // definition. 1002 if (HadMacroDefinition) { 1003 uint32_t MacroDirectivesOffset = 1004 endian::readNext<uint32_t, little, unaligned>(d); 1005 DataLen -= 4; 1006 1007 Reader.addPendingMacro(II, &F, MacroDirectivesOffset); 1008 } 1009 1010 Reader.SetIdentifierInfo(ID, II); 1011 1012 // Read all of the declarations visible at global scope with this 1013 // name. 1014 if (DataLen > 0) { 1015 SmallVector<uint32_t, 4> DeclIDs; 1016 for (; DataLen > 0; DataLen -= 4) 1017 DeclIDs.push_back(Reader.getGlobalDeclID( 1018 F, endian::readNext<uint32_t, little, unaligned>(d))); 1019 Reader.SetGloballyVisibleDecls(II, DeclIDs); 1020 } 1021 1022 return II; 1023 } 1024 1025 DeclarationNameKey::DeclarationNameKey(DeclarationName Name) 1026 : Kind(Name.getNameKind()) { 1027 switch (Kind) { 1028 case DeclarationName::Identifier: 1029 Data = (uint64_t)Name.getAsIdentifierInfo(); 1030 break; 1031 case DeclarationName::ObjCZeroArgSelector: 1032 case DeclarationName::ObjCOneArgSelector: 1033 case DeclarationName::ObjCMultiArgSelector: 1034 Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr(); 1035 break; 1036 case DeclarationName::CXXOperatorName: 1037 Data = Name.getCXXOverloadedOperator(); 1038 break; 1039 case DeclarationName::CXXLiteralOperatorName: 1040 Data = (uint64_t)Name.getCXXLiteralIdentifier(); 1041 break; 1042 case DeclarationName::CXXDeductionGuideName: 1043 Data = (uint64_t)Name.getCXXDeductionGuideTemplate() 1044 ->getDeclName().getAsIdentifierInfo(); 1045 break; 1046 case DeclarationName::CXXConstructorName: 1047 case DeclarationName::CXXDestructorName: 1048 case DeclarationName::CXXConversionFunctionName: 1049 case DeclarationName::CXXUsingDirective: 1050 Data = 0; 1051 break; 1052 } 1053 } 1054 1055 unsigned DeclarationNameKey::getHash() const { 1056 llvm::FoldingSetNodeID ID; 1057 ID.AddInteger(Kind); 1058 1059 switch (Kind) { 1060 case DeclarationName::Identifier: 1061 case DeclarationName::CXXLiteralOperatorName: 1062 case DeclarationName::CXXDeductionGuideName: 1063 ID.AddString(((IdentifierInfo*)Data)->getName()); 1064 break; 1065 case DeclarationName::ObjCZeroArgSelector: 1066 case DeclarationName::ObjCOneArgSelector: 1067 case DeclarationName::ObjCMultiArgSelector: 1068 ID.AddInteger(serialization::ComputeHash(Selector(Data))); 1069 break; 1070 case DeclarationName::CXXOperatorName: 1071 ID.AddInteger((OverloadedOperatorKind)Data); 1072 break; 1073 case DeclarationName::CXXConstructorName: 1074 case DeclarationName::CXXDestructorName: 1075 case DeclarationName::CXXConversionFunctionName: 1076 case DeclarationName::CXXUsingDirective: 1077 break; 1078 } 1079 1080 return ID.ComputeHash(); 1081 } 1082 1083 ModuleFile * 1084 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) { 1085 using namespace llvm::support; 1086 1087 uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d); 1088 return Reader.getLocalModuleFile(F, ModuleFileID); 1089 } 1090 1091 std::pair<unsigned, unsigned> 1092 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) { 1093 using namespace llvm::support; 1094 1095 unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 1096 unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 1097 return std::make_pair(KeyLen, DataLen); 1098 } 1099 1100 ASTDeclContextNameLookupTrait::internal_key_type 1101 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) { 1102 using namespace llvm::support; 1103 1104 auto Kind = (DeclarationName::NameKind)*d++; 1105 uint64_t Data; 1106 switch (Kind) { 1107 case DeclarationName::Identifier: 1108 case DeclarationName::CXXLiteralOperatorName: 1109 case DeclarationName::CXXDeductionGuideName: 1110 Data = (uint64_t)Reader.getLocalIdentifier( 1111 F, endian::readNext<uint32_t, little, unaligned>(d)); 1112 break; 1113 case DeclarationName::ObjCZeroArgSelector: 1114 case DeclarationName::ObjCOneArgSelector: 1115 case DeclarationName::ObjCMultiArgSelector: 1116 Data = 1117 (uint64_t)Reader.getLocalSelector( 1118 F, endian::readNext<uint32_t, little, unaligned>( 1119 d)).getAsOpaquePtr(); 1120 break; 1121 case DeclarationName::CXXOperatorName: 1122 Data = *d++; // OverloadedOperatorKind 1123 break; 1124 case DeclarationName::CXXConstructorName: 1125 case DeclarationName::CXXDestructorName: 1126 case DeclarationName::CXXConversionFunctionName: 1127 case DeclarationName::CXXUsingDirective: 1128 Data = 0; 1129 break; 1130 } 1131 1132 return DeclarationNameKey(Kind, Data); 1133 } 1134 1135 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type, 1136 const unsigned char *d, 1137 unsigned DataLen, 1138 data_type_builder &Val) { 1139 using namespace llvm::support; 1140 1141 for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) { 1142 uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d); 1143 Val.insert(Reader.getGlobalDeclID(F, LocalID)); 1144 } 1145 } 1146 1147 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, 1148 BitstreamCursor &Cursor, 1149 uint64_t Offset, 1150 DeclContext *DC) { 1151 assert(Offset != 0); 1152 1153 SavedStreamPosition SavedPosition(Cursor); 1154 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1155 Error(std::move(Err)); 1156 return true; 1157 } 1158 1159 RecordData Record; 1160 StringRef Blob; 1161 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1162 if (!MaybeCode) { 1163 Error(MaybeCode.takeError()); 1164 return true; 1165 } 1166 unsigned Code = MaybeCode.get(); 1167 1168 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1169 if (!MaybeRecCode) { 1170 Error(MaybeRecCode.takeError()); 1171 return true; 1172 } 1173 unsigned RecCode = MaybeRecCode.get(); 1174 if (RecCode != DECL_CONTEXT_LEXICAL) { 1175 Error("Expected lexical block"); 1176 return true; 1177 } 1178 1179 assert(!isa<TranslationUnitDecl>(DC) && 1180 "expected a TU_UPDATE_LEXICAL record for TU"); 1181 // If we are handling a C++ class template instantiation, we can see multiple 1182 // lexical updates for the same record. It's important that we select only one 1183 // of them, so that field numbering works properly. Just pick the first one we 1184 // see. 1185 auto &Lex = LexicalDecls[DC]; 1186 if (!Lex.first) { 1187 Lex = std::make_pair( 1188 &M, llvm::makeArrayRef( 1189 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 1190 Blob.data()), 1191 Blob.size() / 4)); 1192 } 1193 DC->setHasExternalLexicalStorage(true); 1194 return false; 1195 } 1196 1197 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, 1198 BitstreamCursor &Cursor, 1199 uint64_t Offset, 1200 DeclID ID) { 1201 assert(Offset != 0); 1202 1203 SavedStreamPosition SavedPosition(Cursor); 1204 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1205 Error(std::move(Err)); 1206 return true; 1207 } 1208 1209 RecordData Record; 1210 StringRef Blob; 1211 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1212 if (!MaybeCode) { 1213 Error(MaybeCode.takeError()); 1214 return true; 1215 } 1216 unsigned Code = MaybeCode.get(); 1217 1218 Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob); 1219 if (!MaybeRecCode) { 1220 Error(MaybeRecCode.takeError()); 1221 return true; 1222 } 1223 unsigned RecCode = MaybeRecCode.get(); 1224 if (RecCode != DECL_CONTEXT_VISIBLE) { 1225 Error("Expected visible lookup table block"); 1226 return true; 1227 } 1228 1229 // We can't safely determine the primary context yet, so delay attaching the 1230 // lookup table until we're done with recursive deserialization. 1231 auto *Data = (const unsigned char*)Blob.data(); 1232 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data}); 1233 return false; 1234 } 1235 1236 void ASTReader::Error(StringRef Msg) const { 1237 Error(diag::err_fe_pch_malformed, Msg); 1238 if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() && 1239 !PP.getHeaderSearchInfo().getModuleCachePath().empty()) { 1240 Diag(diag::note_module_cache_path) 1241 << PP.getHeaderSearchInfo().getModuleCachePath(); 1242 } 1243 } 1244 1245 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1246 StringRef Arg3) const { 1247 if (Diags.isDiagnosticInFlight()) 1248 Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3); 1249 else 1250 Diag(DiagID) << Arg1 << Arg2 << Arg3; 1251 } 1252 1253 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2, 1254 unsigned Select) const { 1255 if (!Diags.isDiagnosticInFlight()) 1256 Diag(DiagID) << Arg1 << Arg2 << Select; 1257 } 1258 1259 void ASTReader::Error(llvm::Error &&Err) const { 1260 Error(toString(std::move(Err))); 1261 } 1262 1263 //===----------------------------------------------------------------------===// 1264 // Source Manager Deserialization 1265 //===----------------------------------------------------------------------===// 1266 1267 /// Read the line table in the source manager block. 1268 /// \returns true if there was an error. 1269 bool ASTReader::ParseLineTable(ModuleFile &F, 1270 const RecordData &Record) { 1271 unsigned Idx = 0; 1272 LineTableInfo &LineTable = SourceMgr.getLineTable(); 1273 1274 // Parse the file names 1275 std::map<int, int> FileIDs; 1276 FileIDs[-1] = -1; // For unspecified filenames. 1277 for (unsigned I = 0; Record[Idx]; ++I) { 1278 // Extract the file name 1279 auto Filename = ReadPath(F, Record, Idx); 1280 FileIDs[I] = LineTable.getLineTableFilenameID(Filename); 1281 } 1282 ++Idx; 1283 1284 // Parse the line entries 1285 std::vector<LineEntry> Entries; 1286 while (Idx < Record.size()) { 1287 int FID = Record[Idx++]; 1288 assert(FID >= 0 && "Serialized line entries for non-local file."); 1289 // Remap FileID from 1-based old view. 1290 FID += F.SLocEntryBaseID - 1; 1291 1292 // Extract the line entries 1293 unsigned NumEntries = Record[Idx++]; 1294 assert(NumEntries && "no line entries for file ID"); 1295 Entries.clear(); 1296 Entries.reserve(NumEntries); 1297 for (unsigned I = 0; I != NumEntries; ++I) { 1298 unsigned FileOffset = Record[Idx++]; 1299 unsigned LineNo = Record[Idx++]; 1300 int FilenameID = FileIDs[Record[Idx++]]; 1301 SrcMgr::CharacteristicKind FileKind 1302 = (SrcMgr::CharacteristicKind)Record[Idx++]; 1303 unsigned IncludeOffset = Record[Idx++]; 1304 Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID, 1305 FileKind, IncludeOffset)); 1306 } 1307 LineTable.AddEntry(FileID::get(FID), Entries); 1308 } 1309 1310 return false; 1311 } 1312 1313 /// Read a source manager block 1314 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) { 1315 using namespace SrcMgr; 1316 1317 BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor; 1318 1319 // Set the source-location entry cursor to the current position in 1320 // the stream. This cursor will be used to read the contents of the 1321 // source manager block initially, and then lazily read 1322 // source-location entries as needed. 1323 SLocEntryCursor = F.Stream; 1324 1325 // The stream itself is going to skip over the source manager block. 1326 if (llvm::Error Err = F.Stream.SkipBlock()) { 1327 Error(std::move(Err)); 1328 return true; 1329 } 1330 1331 // Enter the source manager block. 1332 if (llvm::Error Err = 1333 SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) { 1334 Error(std::move(Err)); 1335 return true; 1336 } 1337 1338 RecordData Record; 1339 while (true) { 1340 Expected<llvm::BitstreamEntry> MaybeE = 1341 SLocEntryCursor.advanceSkippingSubblocks(); 1342 if (!MaybeE) { 1343 Error(MaybeE.takeError()); 1344 return true; 1345 } 1346 llvm::BitstreamEntry E = MaybeE.get(); 1347 1348 switch (E.Kind) { 1349 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1350 case llvm::BitstreamEntry::Error: 1351 Error("malformed block record in AST file"); 1352 return true; 1353 case llvm::BitstreamEntry::EndBlock: 1354 return false; 1355 case llvm::BitstreamEntry::Record: 1356 // The interesting case. 1357 break; 1358 } 1359 1360 // Read a record. 1361 Record.clear(); 1362 StringRef Blob; 1363 Expected<unsigned> MaybeRecord = 1364 SLocEntryCursor.readRecord(E.ID, Record, &Blob); 1365 if (!MaybeRecord) { 1366 Error(MaybeRecord.takeError()); 1367 return true; 1368 } 1369 switch (MaybeRecord.get()) { 1370 default: // Default behavior: ignore. 1371 break; 1372 1373 case SM_SLOC_FILE_ENTRY: 1374 case SM_SLOC_BUFFER_ENTRY: 1375 case SM_SLOC_EXPANSION_ENTRY: 1376 // Once we hit one of the source location entries, we're done. 1377 return false; 1378 } 1379 } 1380 } 1381 1382 /// If a header file is not found at the path that we expect it to be 1383 /// and the PCH file was moved from its original location, try to resolve the 1384 /// file by assuming that header+PCH were moved together and the header is in 1385 /// the same place relative to the PCH. 1386 static std::string 1387 resolveFileRelativeToOriginalDir(const std::string &Filename, 1388 const std::string &OriginalDir, 1389 const std::string &CurrDir) { 1390 assert(OriginalDir != CurrDir && 1391 "No point trying to resolve the file if the PCH dir didn't change"); 1392 1393 using namespace llvm::sys; 1394 1395 SmallString<128> filePath(Filename); 1396 fs::make_absolute(filePath); 1397 assert(path::is_absolute(OriginalDir)); 1398 SmallString<128> currPCHPath(CurrDir); 1399 1400 path::const_iterator fileDirI = path::begin(path::parent_path(filePath)), 1401 fileDirE = path::end(path::parent_path(filePath)); 1402 path::const_iterator origDirI = path::begin(OriginalDir), 1403 origDirE = path::end(OriginalDir); 1404 // Skip the common path components from filePath and OriginalDir. 1405 while (fileDirI != fileDirE && origDirI != origDirE && 1406 *fileDirI == *origDirI) { 1407 ++fileDirI; 1408 ++origDirI; 1409 } 1410 for (; origDirI != origDirE; ++origDirI) 1411 path::append(currPCHPath, ".."); 1412 path::append(currPCHPath, fileDirI, fileDirE); 1413 path::append(currPCHPath, path::filename(Filename)); 1414 return std::string(currPCHPath.str()); 1415 } 1416 1417 bool ASTReader::ReadSLocEntry(int ID) { 1418 if (ID == 0) 1419 return false; 1420 1421 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1422 Error("source location entry ID out-of-range for AST file"); 1423 return true; 1424 } 1425 1426 // Local helper to read the (possibly-compressed) buffer data following the 1427 // entry record. 1428 auto ReadBuffer = [this]( 1429 BitstreamCursor &SLocEntryCursor, 1430 StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> { 1431 RecordData Record; 1432 StringRef Blob; 1433 Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode(); 1434 if (!MaybeCode) { 1435 Error(MaybeCode.takeError()); 1436 return nullptr; 1437 } 1438 unsigned Code = MaybeCode.get(); 1439 1440 Expected<unsigned> MaybeRecCode = 1441 SLocEntryCursor.readRecord(Code, Record, &Blob); 1442 if (!MaybeRecCode) { 1443 Error(MaybeRecCode.takeError()); 1444 return nullptr; 1445 } 1446 unsigned RecCode = MaybeRecCode.get(); 1447 1448 if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) { 1449 if (!llvm::zlib::isAvailable()) { 1450 Error("zlib is not available"); 1451 return nullptr; 1452 } 1453 SmallString<0> Uncompressed; 1454 if (llvm::Error E = 1455 llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) { 1456 Error("could not decompress embedded file contents: " + 1457 llvm::toString(std::move(E))); 1458 return nullptr; 1459 } 1460 return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name); 1461 } else if (RecCode == SM_SLOC_BUFFER_BLOB) { 1462 return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true); 1463 } else { 1464 Error("AST record has invalid code"); 1465 return nullptr; 1466 } 1467 }; 1468 1469 ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second; 1470 if (llvm::Error Err = F->SLocEntryCursor.JumpToBit( 1471 F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) { 1472 Error(std::move(Err)); 1473 return true; 1474 } 1475 1476 BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; 1477 unsigned BaseOffset = F->SLocEntryBaseOffset; 1478 1479 ++NumSLocEntriesRead; 1480 Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); 1481 if (!MaybeEntry) { 1482 Error(MaybeEntry.takeError()); 1483 return true; 1484 } 1485 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1486 1487 if (Entry.Kind != llvm::BitstreamEntry::Record) { 1488 Error("incorrectly-formatted source location entry in AST file"); 1489 return true; 1490 } 1491 1492 RecordData Record; 1493 StringRef Blob; 1494 Expected<unsigned> MaybeSLOC = 1495 SLocEntryCursor.readRecord(Entry.ID, Record, &Blob); 1496 if (!MaybeSLOC) { 1497 Error(MaybeSLOC.takeError()); 1498 return true; 1499 } 1500 switch (MaybeSLOC.get()) { 1501 default: 1502 Error("incorrectly-formatted source location entry in AST file"); 1503 return true; 1504 1505 case SM_SLOC_FILE_ENTRY: { 1506 // We will detect whether a file changed and return 'Failure' for it, but 1507 // we will also try to fail gracefully by setting up the SLocEntry. 1508 unsigned InputID = Record[4]; 1509 InputFile IF = getInputFile(*F, InputID); 1510 const FileEntry *File = IF.getFile(); 1511 bool OverriddenBuffer = IF.isOverridden(); 1512 1513 // Note that we only check if a File was returned. If it was out-of-date 1514 // we have complained but we will continue creating a FileID to recover 1515 // gracefully. 1516 if (!File) 1517 return true; 1518 1519 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1520 if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) { 1521 // This is the module's main file. 1522 IncludeLoc = getImportLocation(F); 1523 } 1524 SrcMgr::CharacteristicKind 1525 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1526 // FIXME: The FileID should be created from the FileEntryRef. 1527 FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter, 1528 ID, BaseOffset + Record[0]); 1529 SrcMgr::FileInfo &FileInfo = 1530 const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile()); 1531 FileInfo.NumCreatedFIDs = Record[5]; 1532 if (Record[3]) 1533 FileInfo.setHasLineDirectives(); 1534 1535 unsigned NumFileDecls = Record[7]; 1536 if (NumFileDecls && ContextObj) { 1537 const DeclID *FirstDecl = F->FileSortedDecls + Record[6]; 1538 assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?"); 1539 FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl, 1540 NumFileDecls)); 1541 } 1542 1543 const SrcMgr::ContentCache *ContentCache 1544 = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter)); 1545 if (OverriddenBuffer && !ContentCache->BufferOverridden && 1546 ContentCache->ContentsEntry == ContentCache->OrigEntry && 1547 !ContentCache->getRawBuffer()) { 1548 auto Buffer = ReadBuffer(SLocEntryCursor, File->getName()); 1549 if (!Buffer) 1550 return true; 1551 SourceMgr.overrideFileContents(File, std::move(Buffer)); 1552 } 1553 1554 break; 1555 } 1556 1557 case SM_SLOC_BUFFER_ENTRY: { 1558 const char *Name = Blob.data(); 1559 unsigned Offset = Record[0]; 1560 SrcMgr::CharacteristicKind 1561 FileCharacter = (SrcMgr::CharacteristicKind)Record[2]; 1562 SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]); 1563 if (IncludeLoc.isInvalid() && F->isModule()) { 1564 IncludeLoc = getImportLocation(F); 1565 } 1566 1567 auto Buffer = ReadBuffer(SLocEntryCursor, Name); 1568 if (!Buffer) 1569 return true; 1570 SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID, 1571 BaseOffset + Offset, IncludeLoc); 1572 break; 1573 } 1574 1575 case SM_SLOC_EXPANSION_ENTRY: { 1576 SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]); 1577 SourceMgr.createExpansionLoc(SpellingLoc, 1578 ReadSourceLocation(*F, Record[2]), 1579 ReadSourceLocation(*F, Record[3]), 1580 Record[5], 1581 Record[4], 1582 ID, 1583 BaseOffset + Record[0]); 1584 break; 1585 } 1586 } 1587 1588 return false; 1589 } 1590 1591 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) { 1592 if (ID == 0) 1593 return std::make_pair(SourceLocation(), ""); 1594 1595 if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) { 1596 Error("source location entry ID out-of-range for AST file"); 1597 return std::make_pair(SourceLocation(), ""); 1598 } 1599 1600 // Find which module file this entry lands in. 1601 ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second; 1602 if (!M->isModule()) 1603 return std::make_pair(SourceLocation(), ""); 1604 1605 // FIXME: Can we map this down to a particular submodule? That would be 1606 // ideal. 1607 return std::make_pair(M->ImportLoc, StringRef(M->ModuleName)); 1608 } 1609 1610 /// Find the location where the module F is imported. 1611 SourceLocation ASTReader::getImportLocation(ModuleFile *F) { 1612 if (F->ImportLoc.isValid()) 1613 return F->ImportLoc; 1614 1615 // Otherwise we have a PCH. It's considered to be "imported" at the first 1616 // location of its includer. 1617 if (F->ImportedBy.empty() || !F->ImportedBy[0]) { 1618 // Main file is the importer. 1619 assert(SourceMgr.getMainFileID().isValid() && "missing main file"); 1620 return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 1621 } 1622 return F->ImportedBy[0]->FirstLoc; 1623 } 1624 1625 /// Enter a subblock of the specified BlockID with the specified cursor. Read 1626 /// the abbreviations that are at the top of the block and then leave the cursor 1627 /// pointing into the block. 1628 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID) { 1629 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 1630 // FIXME this drops errors on the floor. 1631 consumeError(std::move(Err)); 1632 return true; 1633 } 1634 1635 while (true) { 1636 uint64_t Offset = Cursor.GetCurrentBitNo(); 1637 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 1638 if (!MaybeCode) { 1639 // FIXME this drops errors on the floor. 1640 consumeError(MaybeCode.takeError()); 1641 return true; 1642 } 1643 unsigned Code = MaybeCode.get(); 1644 1645 // We expect all abbrevs to be at the start of the block. 1646 if (Code != llvm::bitc::DEFINE_ABBREV) { 1647 if (llvm::Error Err = Cursor.JumpToBit(Offset)) { 1648 // FIXME this drops errors on the floor. 1649 consumeError(std::move(Err)); 1650 return true; 1651 } 1652 return false; 1653 } 1654 if (llvm::Error Err = Cursor.ReadAbbrevRecord()) { 1655 // FIXME this drops errors on the floor. 1656 consumeError(std::move(Err)); 1657 return true; 1658 } 1659 } 1660 } 1661 1662 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record, 1663 unsigned &Idx) { 1664 Token Tok; 1665 Tok.startToken(); 1666 Tok.setLocation(ReadSourceLocation(F, Record, Idx)); 1667 Tok.setLength(Record[Idx++]); 1668 if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++])) 1669 Tok.setIdentifierInfo(II); 1670 Tok.setKind((tok::TokenKind)Record[Idx++]); 1671 Tok.setFlag((Token::TokenFlags)Record[Idx++]); 1672 return Tok; 1673 } 1674 1675 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) { 1676 BitstreamCursor &Stream = F.MacroCursor; 1677 1678 // Keep track of where we are in the stream, then jump back there 1679 // after reading this macro. 1680 SavedStreamPosition SavedPosition(Stream); 1681 1682 if (llvm::Error Err = Stream.JumpToBit(Offset)) { 1683 // FIXME this drops errors on the floor. 1684 consumeError(std::move(Err)); 1685 return nullptr; 1686 } 1687 RecordData Record; 1688 SmallVector<IdentifierInfo*, 16> MacroParams; 1689 MacroInfo *Macro = nullptr; 1690 1691 while (true) { 1692 // Advance to the next record, but if we get to the end of the block, don't 1693 // pop it (removing all the abbreviations from the cursor) since we want to 1694 // be able to reseek within the block and read entries. 1695 unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd; 1696 Expected<llvm::BitstreamEntry> MaybeEntry = 1697 Stream.advanceSkippingSubblocks(Flags); 1698 if (!MaybeEntry) { 1699 Error(MaybeEntry.takeError()); 1700 return Macro; 1701 } 1702 llvm::BitstreamEntry Entry = MaybeEntry.get(); 1703 1704 switch (Entry.Kind) { 1705 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1706 case llvm::BitstreamEntry::Error: 1707 Error("malformed block record in AST file"); 1708 return Macro; 1709 case llvm::BitstreamEntry::EndBlock: 1710 return Macro; 1711 case llvm::BitstreamEntry::Record: 1712 // The interesting case. 1713 break; 1714 } 1715 1716 // Read a record. 1717 Record.clear(); 1718 PreprocessorRecordTypes RecType; 1719 if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record)) 1720 RecType = (PreprocessorRecordTypes)MaybeRecType.get(); 1721 else { 1722 Error(MaybeRecType.takeError()); 1723 return Macro; 1724 } 1725 switch (RecType) { 1726 case PP_MODULE_MACRO: 1727 case PP_MACRO_DIRECTIVE_HISTORY: 1728 return Macro; 1729 1730 case PP_MACRO_OBJECT_LIKE: 1731 case PP_MACRO_FUNCTION_LIKE: { 1732 // If we already have a macro, that means that we've hit the end 1733 // of the definition of the macro we were looking for. We're 1734 // done. 1735 if (Macro) 1736 return Macro; 1737 1738 unsigned NextIndex = 1; // Skip identifier ID. 1739 SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex); 1740 MacroInfo *MI = PP.AllocateMacroInfo(Loc); 1741 MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex)); 1742 MI->setIsUsed(Record[NextIndex++]); 1743 MI->setUsedForHeaderGuard(Record[NextIndex++]); 1744 1745 if (RecType == PP_MACRO_FUNCTION_LIKE) { 1746 // Decode function-like macro info. 1747 bool isC99VarArgs = Record[NextIndex++]; 1748 bool isGNUVarArgs = Record[NextIndex++]; 1749 bool hasCommaPasting = Record[NextIndex++]; 1750 MacroParams.clear(); 1751 unsigned NumArgs = Record[NextIndex++]; 1752 for (unsigned i = 0; i != NumArgs; ++i) 1753 MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++])); 1754 1755 // Install function-like macro info. 1756 MI->setIsFunctionLike(); 1757 if (isC99VarArgs) MI->setIsC99Varargs(); 1758 if (isGNUVarArgs) MI->setIsGNUVarargs(); 1759 if (hasCommaPasting) MI->setHasCommaPasting(); 1760 MI->setParameterList(MacroParams, PP.getPreprocessorAllocator()); 1761 } 1762 1763 // Remember that we saw this macro last so that we add the tokens that 1764 // form its body to it. 1765 Macro = MI; 1766 1767 if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() && 1768 Record[NextIndex]) { 1769 // We have a macro definition. Register the association 1770 PreprocessedEntityID 1771 GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]); 1772 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 1773 PreprocessingRecord::PPEntityID PPID = 1774 PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true); 1775 MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>( 1776 PPRec.getPreprocessedEntity(PPID)); 1777 if (PPDef) 1778 PPRec.RegisterMacroDefinition(Macro, PPDef); 1779 } 1780 1781 ++NumMacrosRead; 1782 break; 1783 } 1784 1785 case PP_TOKEN: { 1786 // If we see a TOKEN before a PP_MACRO_*, then the file is 1787 // erroneous, just pretend we didn't see this. 1788 if (!Macro) break; 1789 1790 unsigned Idx = 0; 1791 Token Tok = ReadToken(F, Record, Idx); 1792 Macro->AddTokenToBody(Tok); 1793 break; 1794 } 1795 } 1796 } 1797 } 1798 1799 PreprocessedEntityID 1800 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M, 1801 unsigned LocalID) const { 1802 if (!M.ModuleOffsetMap.empty()) 1803 ReadModuleOffsetMap(M); 1804 1805 ContinuousRangeMap<uint32_t, int, 2>::const_iterator 1806 I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS); 1807 assert(I != M.PreprocessedEntityRemap.end() 1808 && "Invalid index into preprocessed entity index remap"); 1809 1810 return LocalID + I->second; 1811 } 1812 1813 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) { 1814 return llvm::hash_combine(ikey.Size, ikey.ModTime); 1815 } 1816 1817 HeaderFileInfoTrait::internal_key_type 1818 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) { 1819 internal_key_type ikey = {FE->getSize(), 1820 M.HasTimestamps ? FE->getModificationTime() : 0, 1821 FE->getName(), /*Imported*/ false}; 1822 return ikey; 1823 } 1824 1825 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) { 1826 if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime)) 1827 return false; 1828 1829 if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename) 1830 return true; 1831 1832 // Determine whether the actual files are equivalent. 1833 FileManager &FileMgr = Reader.getFileManager(); 1834 auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* { 1835 if (!Key.Imported) { 1836 if (auto File = FileMgr.getFile(Key.Filename)) 1837 return *File; 1838 return nullptr; 1839 } 1840 1841 std::string Resolved = std::string(Key.Filename); 1842 Reader.ResolveImportedPath(M, Resolved); 1843 if (auto File = FileMgr.getFile(Resolved)) 1844 return *File; 1845 return nullptr; 1846 }; 1847 1848 const FileEntry *FEA = GetFile(a); 1849 const FileEntry *FEB = GetFile(b); 1850 return FEA && FEA == FEB; 1851 } 1852 1853 std::pair<unsigned, unsigned> 1854 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) { 1855 using namespace llvm::support; 1856 1857 unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d); 1858 unsigned DataLen = (unsigned) *d++; 1859 return std::make_pair(KeyLen, DataLen); 1860 } 1861 1862 HeaderFileInfoTrait::internal_key_type 1863 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) { 1864 using namespace llvm::support; 1865 1866 internal_key_type ikey; 1867 ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d)); 1868 ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d)); 1869 ikey.Filename = (const char *)d; 1870 ikey.Imported = true; 1871 return ikey; 1872 } 1873 1874 HeaderFileInfoTrait::data_type 1875 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d, 1876 unsigned DataLen) { 1877 using namespace llvm::support; 1878 1879 const unsigned char *End = d + DataLen; 1880 HeaderFileInfo HFI; 1881 unsigned Flags = *d++; 1882 // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp. 1883 HFI.isImport |= (Flags >> 5) & 0x01; 1884 HFI.isPragmaOnce |= (Flags >> 4) & 0x01; 1885 HFI.DirInfo = (Flags >> 1) & 0x07; 1886 HFI.IndexHeaderMapHeader = Flags & 0x01; 1887 // FIXME: Find a better way to handle this. Maybe just store a 1888 // "has been included" flag? 1889 HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d), 1890 HFI.NumIncludes); 1891 HFI.ControllingMacroID = Reader.getGlobalIdentifierID( 1892 M, endian::readNext<uint32_t, little, unaligned>(d)); 1893 if (unsigned FrameworkOffset = 1894 endian::readNext<uint32_t, little, unaligned>(d)) { 1895 // The framework offset is 1 greater than the actual offset, 1896 // since 0 is used as an indicator for "no framework name". 1897 StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1); 1898 HFI.Framework = HS->getUniqueFrameworkName(FrameworkName); 1899 } 1900 1901 assert((End - d) % 4 == 0 && 1902 "Wrong data length in HeaderFileInfo deserialization"); 1903 while (d != End) { 1904 uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d); 1905 auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3); 1906 LocalSMID >>= 2; 1907 1908 // This header is part of a module. Associate it with the module to enable 1909 // implicit module import. 1910 SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID); 1911 Module *Mod = Reader.getSubmodule(GlobalSMID); 1912 FileManager &FileMgr = Reader.getFileManager(); 1913 ModuleMap &ModMap = 1914 Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap(); 1915 1916 std::string Filename = std::string(key.Filename); 1917 if (key.Imported) 1918 Reader.ResolveImportedPath(M, Filename); 1919 // FIXME: This is not always the right filename-as-written, but we're not 1920 // going to use this information to rebuild the module, so it doesn't make 1921 // a lot of difference. 1922 Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)}; 1923 ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true); 1924 HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader); 1925 } 1926 1927 // This HeaderFileInfo was externally loaded. 1928 HFI.External = true; 1929 HFI.IsValid = true; 1930 return HFI; 1931 } 1932 1933 void ASTReader::addPendingMacro(IdentifierInfo *II, 1934 ModuleFile *M, 1935 uint64_t MacroDirectivesOffset) { 1936 assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard"); 1937 PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset)); 1938 } 1939 1940 void ASTReader::ReadDefinedMacros() { 1941 // Note that we are loading defined macros. 1942 Deserializing Macros(this); 1943 1944 for (ModuleFile &I : llvm::reverse(ModuleMgr)) { 1945 BitstreamCursor &MacroCursor = I.MacroCursor; 1946 1947 // If there was no preprocessor block, skip this file. 1948 if (MacroCursor.getBitcodeBytes().empty()) 1949 continue; 1950 1951 BitstreamCursor Cursor = MacroCursor; 1952 if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) { 1953 Error(std::move(Err)); 1954 return; 1955 } 1956 1957 RecordData Record; 1958 while (true) { 1959 Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks(); 1960 if (!MaybeE) { 1961 Error(MaybeE.takeError()); 1962 return; 1963 } 1964 llvm::BitstreamEntry E = MaybeE.get(); 1965 1966 switch (E.Kind) { 1967 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 1968 case llvm::BitstreamEntry::Error: 1969 Error("malformed block record in AST file"); 1970 return; 1971 case llvm::BitstreamEntry::EndBlock: 1972 goto NextCursor; 1973 1974 case llvm::BitstreamEntry::Record: { 1975 Record.clear(); 1976 Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record); 1977 if (!MaybeRecord) { 1978 Error(MaybeRecord.takeError()); 1979 return; 1980 } 1981 switch (MaybeRecord.get()) { 1982 default: // Default behavior: ignore. 1983 break; 1984 1985 case PP_MACRO_OBJECT_LIKE: 1986 case PP_MACRO_FUNCTION_LIKE: { 1987 IdentifierInfo *II = getLocalIdentifier(I, Record[0]); 1988 if (II->isOutOfDate()) 1989 updateOutOfDateIdentifier(*II); 1990 break; 1991 } 1992 1993 case PP_TOKEN: 1994 // Ignore tokens. 1995 break; 1996 } 1997 break; 1998 } 1999 } 2000 } 2001 NextCursor: ; 2002 } 2003 } 2004 2005 namespace { 2006 2007 /// Visitor class used to look up identifirs in an AST file. 2008 class IdentifierLookupVisitor { 2009 StringRef Name; 2010 unsigned NameHash; 2011 unsigned PriorGeneration; 2012 unsigned &NumIdentifierLookups; 2013 unsigned &NumIdentifierLookupHits; 2014 IdentifierInfo *Found = nullptr; 2015 2016 public: 2017 IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration, 2018 unsigned &NumIdentifierLookups, 2019 unsigned &NumIdentifierLookupHits) 2020 : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)), 2021 PriorGeneration(PriorGeneration), 2022 NumIdentifierLookups(NumIdentifierLookups), 2023 NumIdentifierLookupHits(NumIdentifierLookupHits) {} 2024 2025 bool operator()(ModuleFile &M) { 2026 // If we've already searched this module file, skip it now. 2027 if (M.Generation <= PriorGeneration) 2028 return true; 2029 2030 ASTIdentifierLookupTable *IdTable 2031 = (ASTIdentifierLookupTable *)M.IdentifierLookupTable; 2032 if (!IdTable) 2033 return false; 2034 2035 ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M, 2036 Found); 2037 ++NumIdentifierLookups; 2038 ASTIdentifierLookupTable::iterator Pos = 2039 IdTable->find_hashed(Name, NameHash, &Trait); 2040 if (Pos == IdTable->end()) 2041 return false; 2042 2043 // Dereferencing the iterator has the effect of building the 2044 // IdentifierInfo node and populating it with the various 2045 // declarations it needs. 2046 ++NumIdentifierLookupHits; 2047 Found = *Pos; 2048 return true; 2049 } 2050 2051 // Retrieve the identifier info found within the module 2052 // files. 2053 IdentifierInfo *getIdentifierInfo() const { return Found; } 2054 }; 2055 2056 } // namespace 2057 2058 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) { 2059 // Note that we are loading an identifier. 2060 Deserializing AnIdentifier(this); 2061 2062 unsigned PriorGeneration = 0; 2063 if (getContext().getLangOpts().Modules) 2064 PriorGeneration = IdentifierGeneration[&II]; 2065 2066 // If there is a global index, look there first to determine which modules 2067 // provably do not have any results for this identifier. 2068 GlobalModuleIndex::HitSet Hits; 2069 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 2070 if (!loadGlobalIndex()) { 2071 if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) { 2072 HitsPtr = &Hits; 2073 } 2074 } 2075 2076 IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration, 2077 NumIdentifierLookups, 2078 NumIdentifierLookupHits); 2079 ModuleMgr.visit(Visitor, HitsPtr); 2080 markIdentifierUpToDate(&II); 2081 } 2082 2083 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) { 2084 if (!II) 2085 return; 2086 2087 II->setOutOfDate(false); 2088 2089 // Update the generation for this identifier. 2090 if (getContext().getLangOpts().Modules) 2091 IdentifierGeneration[II] = getGeneration(); 2092 } 2093 2094 void ASTReader::resolvePendingMacro(IdentifierInfo *II, 2095 const PendingMacroInfo &PMInfo) { 2096 ModuleFile &M = *PMInfo.M; 2097 2098 BitstreamCursor &Cursor = M.MacroCursor; 2099 SavedStreamPosition SavedPosition(Cursor); 2100 if (llvm::Error Err = Cursor.JumpToBit(PMInfo.MacroDirectivesOffset)) { 2101 Error(std::move(Err)); 2102 return; 2103 } 2104 2105 struct ModuleMacroRecord { 2106 SubmoduleID SubModID; 2107 MacroInfo *MI; 2108 SmallVector<SubmoduleID, 8> Overrides; 2109 }; 2110 llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros; 2111 2112 // We expect to see a sequence of PP_MODULE_MACRO records listing exported 2113 // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete 2114 // macro histroy. 2115 RecordData Record; 2116 while (true) { 2117 Expected<llvm::BitstreamEntry> MaybeEntry = 2118 Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 2119 if (!MaybeEntry) { 2120 Error(MaybeEntry.takeError()); 2121 return; 2122 } 2123 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2124 2125 if (Entry.Kind != llvm::BitstreamEntry::Record) { 2126 Error("malformed block record in AST file"); 2127 return; 2128 } 2129 2130 Record.clear(); 2131 Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record); 2132 if (!MaybePP) { 2133 Error(MaybePP.takeError()); 2134 return; 2135 } 2136 switch ((PreprocessorRecordTypes)MaybePP.get()) { 2137 case PP_MACRO_DIRECTIVE_HISTORY: 2138 break; 2139 2140 case PP_MODULE_MACRO: { 2141 ModuleMacros.push_back(ModuleMacroRecord()); 2142 auto &Info = ModuleMacros.back(); 2143 Info.SubModID = getGlobalSubmoduleID(M, Record[0]); 2144 Info.MI = getMacro(getGlobalMacroID(M, Record[1])); 2145 for (int I = 2, N = Record.size(); I != N; ++I) 2146 Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I])); 2147 continue; 2148 } 2149 2150 default: 2151 Error("malformed block record in AST file"); 2152 return; 2153 } 2154 2155 // We found the macro directive history; that's the last record 2156 // for this macro. 2157 break; 2158 } 2159 2160 // Module macros are listed in reverse dependency order. 2161 { 2162 std::reverse(ModuleMacros.begin(), ModuleMacros.end()); 2163 llvm::SmallVector<ModuleMacro*, 8> Overrides; 2164 for (auto &MMR : ModuleMacros) { 2165 Overrides.clear(); 2166 for (unsigned ModID : MMR.Overrides) { 2167 Module *Mod = getSubmodule(ModID); 2168 auto *Macro = PP.getModuleMacro(Mod, II); 2169 assert(Macro && "missing definition for overridden macro"); 2170 Overrides.push_back(Macro); 2171 } 2172 2173 bool Inserted = false; 2174 Module *Owner = getSubmodule(MMR.SubModID); 2175 PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted); 2176 } 2177 } 2178 2179 // Don't read the directive history for a module; we don't have anywhere 2180 // to put it. 2181 if (M.isModule()) 2182 return; 2183 2184 // Deserialize the macro directives history in reverse source-order. 2185 MacroDirective *Latest = nullptr, *Earliest = nullptr; 2186 unsigned Idx = 0, N = Record.size(); 2187 while (Idx < N) { 2188 MacroDirective *MD = nullptr; 2189 SourceLocation Loc = ReadSourceLocation(M, Record, Idx); 2190 MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++]; 2191 switch (K) { 2192 case MacroDirective::MD_Define: { 2193 MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++])); 2194 MD = PP.AllocateDefMacroDirective(MI, Loc); 2195 break; 2196 } 2197 case MacroDirective::MD_Undefine: 2198 MD = PP.AllocateUndefMacroDirective(Loc); 2199 break; 2200 case MacroDirective::MD_Visibility: 2201 bool isPublic = Record[Idx++]; 2202 MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic); 2203 break; 2204 } 2205 2206 if (!Latest) 2207 Latest = MD; 2208 if (Earliest) 2209 Earliest->setPrevious(MD); 2210 Earliest = MD; 2211 } 2212 2213 if (Latest) 2214 PP.setLoadedMacroDirective(II, Earliest, Latest); 2215 } 2216 2217 ASTReader::InputFileInfo 2218 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) { 2219 // Go find this input file. 2220 BitstreamCursor &Cursor = F.InputFilesCursor; 2221 SavedStreamPosition SavedPosition(Cursor); 2222 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2223 // FIXME this drops errors on the floor. 2224 consumeError(std::move(Err)); 2225 } 2226 2227 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 2228 if (!MaybeCode) { 2229 // FIXME this drops errors on the floor. 2230 consumeError(MaybeCode.takeError()); 2231 } 2232 unsigned Code = MaybeCode.get(); 2233 RecordData Record; 2234 StringRef Blob; 2235 2236 if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob)) 2237 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE && 2238 "invalid record type for input file"); 2239 else { 2240 // FIXME this drops errors on the floor. 2241 consumeError(Maybe.takeError()); 2242 } 2243 2244 assert(Record[0] == ID && "Bogus stored ID or offset"); 2245 InputFileInfo R; 2246 R.StoredSize = static_cast<off_t>(Record[1]); 2247 R.StoredTime = static_cast<time_t>(Record[2]); 2248 R.Overridden = static_cast<bool>(Record[3]); 2249 R.Transient = static_cast<bool>(Record[4]); 2250 R.TopLevelModuleMap = static_cast<bool>(Record[5]); 2251 R.Filename = std::string(Blob); 2252 ResolveImportedPath(F, R.Filename); 2253 2254 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 2255 if (!MaybeEntry) // FIXME this drops errors on the floor. 2256 consumeError(MaybeEntry.takeError()); 2257 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2258 assert(Entry.Kind == llvm::BitstreamEntry::Record && 2259 "expected record type for input file hash"); 2260 2261 Record.clear(); 2262 if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record)) 2263 assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH && 2264 "invalid record type for input file hash"); 2265 else { 2266 // FIXME this drops errors on the floor. 2267 consumeError(Maybe.takeError()); 2268 } 2269 R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) | 2270 static_cast<uint64_t>(Record[0]); 2271 return R; 2272 } 2273 2274 static unsigned moduleKindForDiagnostic(ModuleKind Kind); 2275 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) { 2276 // If this ID is bogus, just return an empty input file. 2277 if (ID == 0 || ID > F.InputFilesLoaded.size()) 2278 return InputFile(); 2279 2280 // If we've already loaded this input file, return it. 2281 if (F.InputFilesLoaded[ID-1].getFile()) 2282 return F.InputFilesLoaded[ID-1]; 2283 2284 if (F.InputFilesLoaded[ID-1].isNotFound()) 2285 return InputFile(); 2286 2287 // Go find this input file. 2288 BitstreamCursor &Cursor = F.InputFilesCursor; 2289 SavedStreamPosition SavedPosition(Cursor); 2290 if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) { 2291 // FIXME this drops errors on the floor. 2292 consumeError(std::move(Err)); 2293 } 2294 2295 InputFileInfo FI = readInputFileInfo(F, ID); 2296 off_t StoredSize = FI.StoredSize; 2297 time_t StoredTime = FI.StoredTime; 2298 bool Overridden = FI.Overridden; 2299 bool Transient = FI.Transient; 2300 StringRef Filename = FI.Filename; 2301 uint64_t StoredContentHash = FI.ContentHash; 2302 2303 const FileEntry *File = nullptr; 2304 if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false)) 2305 File = *FE; 2306 2307 // If we didn't find the file, resolve it relative to the 2308 // original directory from which this AST file was created. 2309 if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() && 2310 F.OriginalDir != F.BaseDirectory) { 2311 std::string Resolved = resolveFileRelativeToOriginalDir( 2312 std::string(Filename), F.OriginalDir, F.BaseDirectory); 2313 if (!Resolved.empty()) 2314 if (auto FE = FileMgr.getFile(Resolved)) 2315 File = *FE; 2316 } 2317 2318 // For an overridden file, create a virtual file with the stored 2319 // size/timestamp. 2320 if ((Overridden || Transient) && File == nullptr) 2321 File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime); 2322 2323 if (File == nullptr) { 2324 if (Complain) { 2325 std::string ErrorStr = "could not find file '"; 2326 ErrorStr += Filename; 2327 ErrorStr += "' referenced by AST file '"; 2328 ErrorStr += F.FileName; 2329 ErrorStr += "'"; 2330 Error(ErrorStr); 2331 } 2332 // Record that we didn't find the file. 2333 F.InputFilesLoaded[ID-1] = InputFile::getNotFound(); 2334 return InputFile(); 2335 } 2336 2337 // Check if there was a request to override the contents of the file 2338 // that was part of the precompiled header. Overriding such a file 2339 // can lead to problems when lexing using the source locations from the 2340 // PCH. 2341 SourceManager &SM = getSourceManager(); 2342 // FIXME: Reject if the overrides are different. 2343 if ((!Overridden && !Transient) && SM.isFileOverridden(File)) { 2344 if (Complain) 2345 Error(diag::err_fe_pch_file_overridden, Filename); 2346 2347 // After emitting the diagnostic, bypass the overriding file to recover 2348 // (this creates a separate FileEntry). 2349 File = SM.bypassFileContentsOverride(*File); 2350 if (!File) { 2351 F.InputFilesLoaded[ID - 1] = InputFile::getNotFound(); 2352 return InputFile(); 2353 } 2354 } 2355 2356 enum ModificationType { 2357 Size, 2358 ModTime, 2359 Content, 2360 None, 2361 }; 2362 auto HasInputFileChanged = [&]() { 2363 if (StoredSize != File->getSize()) 2364 return ModificationType::Size; 2365 if (!DisableValidation && StoredTime && 2366 StoredTime != File->getModificationTime()) { 2367 // In case the modification time changes but not the content, 2368 // accept the cached file as legit. 2369 if (ValidateASTInputFilesContent && 2370 StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) { 2371 auto MemBuffOrError = FileMgr.getBufferForFile(File); 2372 if (!MemBuffOrError) { 2373 if (!Complain) 2374 return ModificationType::ModTime; 2375 std::string ErrorStr = "could not get buffer for file '"; 2376 ErrorStr += File->getName(); 2377 ErrorStr += "'"; 2378 Error(ErrorStr); 2379 return ModificationType::ModTime; 2380 } 2381 2382 auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer()); 2383 if (StoredContentHash == static_cast<uint64_t>(ContentHash)) 2384 return ModificationType::None; 2385 return ModificationType::Content; 2386 } 2387 return ModificationType::ModTime; 2388 } 2389 return ModificationType::None; 2390 }; 2391 2392 bool IsOutOfDate = false; 2393 auto FileChange = HasInputFileChanged(); 2394 // For an overridden file, there is nothing to validate. 2395 if (!Overridden && FileChange != ModificationType::None) { 2396 if (Complain) { 2397 // Build a list of the PCH imports that got us here (in reverse). 2398 SmallVector<ModuleFile *, 4> ImportStack(1, &F); 2399 while (!ImportStack.back()->ImportedBy.empty()) 2400 ImportStack.push_back(ImportStack.back()->ImportedBy[0]); 2401 2402 // The top-level PCH is stale. 2403 StringRef TopLevelPCHName(ImportStack.back()->FileName); 2404 unsigned DiagnosticKind = 2405 moduleKindForDiagnostic(ImportStack.back()->Kind); 2406 if (DiagnosticKind == 0) 2407 Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName, 2408 (unsigned)FileChange); 2409 else if (DiagnosticKind == 1) 2410 Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName, 2411 (unsigned)FileChange); 2412 else 2413 Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName, 2414 (unsigned)FileChange); 2415 2416 // Print the import stack. 2417 if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) { 2418 Diag(diag::note_pch_required_by) 2419 << Filename << ImportStack[0]->FileName; 2420 for (unsigned I = 1; I < ImportStack.size(); ++I) 2421 Diag(diag::note_pch_required_by) 2422 << ImportStack[I-1]->FileName << ImportStack[I]->FileName; 2423 } 2424 2425 if (!Diags.isDiagnosticInFlight()) 2426 Diag(diag::note_pch_rebuild_required) << TopLevelPCHName; 2427 } 2428 2429 IsOutOfDate = true; 2430 } 2431 // FIXME: If the file is overridden and we've already opened it, 2432 // issue an error (or split it into a separate FileEntry). 2433 2434 InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate); 2435 2436 // Note that we've loaded this input file. 2437 F.InputFilesLoaded[ID-1] = IF; 2438 return IF; 2439 } 2440 2441 /// If we are loading a relocatable PCH or module file, and the filename 2442 /// is not an absolute path, add the system or module root to the beginning of 2443 /// the file name. 2444 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) { 2445 // Resolve relative to the base directory, if we have one. 2446 if (!M.BaseDirectory.empty()) 2447 return ResolveImportedPath(Filename, M.BaseDirectory); 2448 } 2449 2450 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) { 2451 if (Filename.empty() || llvm::sys::path::is_absolute(Filename)) 2452 return; 2453 2454 SmallString<128> Buffer; 2455 llvm::sys::path::append(Buffer, Prefix, Filename); 2456 Filename.assign(Buffer.begin(), Buffer.end()); 2457 } 2458 2459 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) { 2460 switch (ARR) { 2461 case ASTReader::Failure: return true; 2462 case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing); 2463 case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate); 2464 case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch); 2465 case ASTReader::ConfigurationMismatch: 2466 return !(Caps & ASTReader::ARR_ConfigurationMismatch); 2467 case ASTReader::HadErrors: return true; 2468 case ASTReader::Success: return false; 2469 } 2470 2471 llvm_unreachable("unknown ASTReadResult"); 2472 } 2473 2474 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( 2475 BitstreamCursor &Stream, unsigned ClientLoadCapabilities, 2476 bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener, 2477 std::string &SuggestedPredefines) { 2478 if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) { 2479 // FIXME this drops errors on the floor. 2480 consumeError(std::move(Err)); 2481 return Failure; 2482 } 2483 2484 // Read all of the records in the options block. 2485 RecordData Record; 2486 ASTReadResult Result = Success; 2487 while (true) { 2488 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2489 if (!MaybeEntry) { 2490 // FIXME this drops errors on the floor. 2491 consumeError(MaybeEntry.takeError()); 2492 return Failure; 2493 } 2494 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2495 2496 switch (Entry.Kind) { 2497 case llvm::BitstreamEntry::Error: 2498 case llvm::BitstreamEntry::SubBlock: 2499 return Failure; 2500 2501 case llvm::BitstreamEntry::EndBlock: 2502 return Result; 2503 2504 case llvm::BitstreamEntry::Record: 2505 // The interesting case. 2506 break; 2507 } 2508 2509 // Read and process a record. 2510 Record.clear(); 2511 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 2512 if (!MaybeRecordType) { 2513 // FIXME this drops errors on the floor. 2514 consumeError(MaybeRecordType.takeError()); 2515 return Failure; 2516 } 2517 switch ((OptionsRecordTypes)MaybeRecordType.get()) { 2518 case LANGUAGE_OPTIONS: { 2519 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2520 if (ParseLanguageOptions(Record, Complain, Listener, 2521 AllowCompatibleConfigurationMismatch)) 2522 Result = ConfigurationMismatch; 2523 break; 2524 } 2525 2526 case TARGET_OPTIONS: { 2527 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2528 if (ParseTargetOptions(Record, Complain, Listener, 2529 AllowCompatibleConfigurationMismatch)) 2530 Result = ConfigurationMismatch; 2531 break; 2532 } 2533 2534 case FILE_SYSTEM_OPTIONS: { 2535 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2536 if (!AllowCompatibleConfigurationMismatch && 2537 ParseFileSystemOptions(Record, Complain, Listener)) 2538 Result = ConfigurationMismatch; 2539 break; 2540 } 2541 2542 case HEADER_SEARCH_OPTIONS: { 2543 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2544 if (!AllowCompatibleConfigurationMismatch && 2545 ParseHeaderSearchOptions(Record, Complain, Listener)) 2546 Result = ConfigurationMismatch; 2547 break; 2548 } 2549 2550 case PREPROCESSOR_OPTIONS: 2551 bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; 2552 if (!AllowCompatibleConfigurationMismatch && 2553 ParsePreprocessorOptions(Record, Complain, Listener, 2554 SuggestedPredefines)) 2555 Result = ConfigurationMismatch; 2556 break; 2557 } 2558 } 2559 } 2560 2561 ASTReader::ASTReadResult 2562 ASTReader::ReadControlBlock(ModuleFile &F, 2563 SmallVectorImpl<ImportedModule> &Loaded, 2564 const ModuleFile *ImportedBy, 2565 unsigned ClientLoadCapabilities) { 2566 BitstreamCursor &Stream = F.Stream; 2567 2568 if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) { 2569 Error(std::move(Err)); 2570 return Failure; 2571 } 2572 2573 // Lambda to read the unhashed control block the first time it's called. 2574 // 2575 // For PCM files, the unhashed control block cannot be read until after the 2576 // MODULE_NAME record. However, PCH files have no MODULE_NAME, and yet still 2577 // need to look ahead before reading the IMPORTS record. For consistency, 2578 // this block is always read somehow (see BitstreamEntry::EndBlock). 2579 bool HasReadUnhashedControlBlock = false; 2580 auto readUnhashedControlBlockOnce = [&]() { 2581 if (!HasReadUnhashedControlBlock) { 2582 HasReadUnhashedControlBlock = true; 2583 if (ASTReadResult Result = 2584 readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities)) 2585 return Result; 2586 } 2587 return Success; 2588 }; 2589 2590 // Read all of the records and blocks in the control block. 2591 RecordData Record; 2592 unsigned NumInputs = 0; 2593 unsigned NumUserInputs = 0; 2594 StringRef BaseDirectoryAsWritten; 2595 while (true) { 2596 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2597 if (!MaybeEntry) { 2598 Error(MaybeEntry.takeError()); 2599 return Failure; 2600 } 2601 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2602 2603 switch (Entry.Kind) { 2604 case llvm::BitstreamEntry::Error: 2605 Error("malformed block record in AST file"); 2606 return Failure; 2607 case llvm::BitstreamEntry::EndBlock: { 2608 // Validate the module before returning. This call catches an AST with 2609 // no module name and no imports. 2610 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2611 return Result; 2612 2613 // Validate input files. 2614 const HeaderSearchOptions &HSOpts = 2615 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 2616 2617 // All user input files reside at the index range [0, NumUserInputs), and 2618 // system input files reside at [NumUserInputs, NumInputs). For explicitly 2619 // loaded module files, ignore missing inputs. 2620 if (!DisableValidation && F.Kind != MK_ExplicitModule && 2621 F.Kind != MK_PrebuiltModule) { 2622 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 2623 2624 // If we are reading a module, we will create a verification timestamp, 2625 // so we verify all input files. Otherwise, verify only user input 2626 // files. 2627 2628 unsigned N = NumUserInputs; 2629 if (ValidateSystemInputs || 2630 (HSOpts.ModulesValidateOncePerBuildSession && 2631 F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp && 2632 F.Kind == MK_ImplicitModule)) 2633 N = NumInputs; 2634 2635 for (unsigned I = 0; I < N; ++I) { 2636 InputFile IF = getInputFile(F, I+1, Complain); 2637 if (!IF.getFile() || IF.isOutOfDate()) 2638 return OutOfDate; 2639 } 2640 } 2641 2642 if (Listener) 2643 Listener->visitModuleFile(F.FileName, F.Kind); 2644 2645 if (Listener && Listener->needsInputFileVisitation()) { 2646 unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs 2647 : NumUserInputs; 2648 for (unsigned I = 0; I < N; ++I) { 2649 bool IsSystem = I >= NumUserInputs; 2650 InputFileInfo FI = readInputFileInfo(F, I+1); 2651 Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden, 2652 F.Kind == MK_ExplicitModule || 2653 F.Kind == MK_PrebuiltModule); 2654 } 2655 } 2656 2657 return Success; 2658 } 2659 2660 case llvm::BitstreamEntry::SubBlock: 2661 switch (Entry.ID) { 2662 case INPUT_FILES_BLOCK_ID: 2663 F.InputFilesCursor = Stream; 2664 if (llvm::Error Err = Stream.SkipBlock()) { 2665 Error(std::move(Err)); 2666 return Failure; 2667 } 2668 if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) { 2669 Error("malformed block record in AST file"); 2670 return Failure; 2671 } 2672 continue; 2673 2674 case OPTIONS_BLOCK_ID: 2675 // If we're reading the first module for this group, check its options 2676 // are compatible with ours. For modules it imports, no further checking 2677 // is required, because we checked them when we built it. 2678 if (Listener && !ImportedBy) { 2679 // Should we allow the configuration of the module file to differ from 2680 // the configuration of the current translation unit in a compatible 2681 // way? 2682 // 2683 // FIXME: Allow this for files explicitly specified with -include-pch. 2684 bool AllowCompatibleConfigurationMismatch = 2685 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 2686 2687 ASTReadResult Result = 2688 ReadOptionsBlock(Stream, ClientLoadCapabilities, 2689 AllowCompatibleConfigurationMismatch, *Listener, 2690 SuggestedPredefines); 2691 if (Result == Failure) { 2692 Error("malformed block record in AST file"); 2693 return Result; 2694 } 2695 2696 if (DisableValidation || 2697 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 2698 Result = Success; 2699 2700 // If we can't load the module, exit early since we likely 2701 // will rebuild the module anyway. The stream may be in the 2702 // middle of a block. 2703 if (Result != Success) 2704 return Result; 2705 } else if (llvm::Error Err = Stream.SkipBlock()) { 2706 Error(std::move(Err)); 2707 return Failure; 2708 } 2709 continue; 2710 2711 default: 2712 if (llvm::Error Err = Stream.SkipBlock()) { 2713 Error(std::move(Err)); 2714 return Failure; 2715 } 2716 continue; 2717 } 2718 2719 case llvm::BitstreamEntry::Record: 2720 // The interesting case. 2721 break; 2722 } 2723 2724 // Read and process a record. 2725 Record.clear(); 2726 StringRef Blob; 2727 Expected<unsigned> MaybeRecordType = 2728 Stream.readRecord(Entry.ID, Record, &Blob); 2729 if (!MaybeRecordType) { 2730 Error(MaybeRecordType.takeError()); 2731 return Failure; 2732 } 2733 switch ((ControlRecordTypes)MaybeRecordType.get()) { 2734 case METADATA: { 2735 if (Record[0] != VERSION_MAJOR && !DisableValidation) { 2736 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2737 Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old 2738 : diag::err_pch_version_too_new); 2739 return VersionMismatch; 2740 } 2741 2742 bool hasErrors = Record[7]; 2743 if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) { 2744 Diag(diag::err_pch_with_compiler_errors); 2745 return HadErrors; 2746 } 2747 if (hasErrors) { 2748 Diags.ErrorOccurred = true; 2749 Diags.UncompilableErrorOccurred = true; 2750 Diags.UnrecoverableErrorOccurred = true; 2751 } 2752 2753 F.RelocatablePCH = Record[4]; 2754 // Relative paths in a relocatable PCH are relative to our sysroot. 2755 if (F.RelocatablePCH) 2756 F.BaseDirectory = isysroot.empty() ? "/" : isysroot; 2757 2758 F.HasTimestamps = Record[5]; 2759 2760 F.PCHHasObjectFile = Record[6]; 2761 2762 const std::string &CurBranch = getClangFullRepositoryVersion(); 2763 StringRef ASTBranch = Blob; 2764 if (StringRef(CurBranch) != ASTBranch && !DisableValidation) { 2765 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 2766 Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch; 2767 return VersionMismatch; 2768 } 2769 break; 2770 } 2771 2772 case IMPORTS: { 2773 // Validate the AST before processing any imports (otherwise, untangling 2774 // them can be error-prone and expensive). A module will have a name and 2775 // will already have been validated, but this catches the PCH case. 2776 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2777 return Result; 2778 2779 // Load each of the imported PCH files. 2780 unsigned Idx = 0, N = Record.size(); 2781 while (Idx < N) { 2782 // Read information about the AST file. 2783 ModuleKind ImportedKind = (ModuleKind)Record[Idx++]; 2784 // The import location will be the local one for now; we will adjust 2785 // all import locations of module imports after the global source 2786 // location info are setup, in ReadAST. 2787 SourceLocation ImportLoc = 2788 ReadUntranslatedSourceLocation(Record[Idx++]); 2789 off_t StoredSize = (off_t)Record[Idx++]; 2790 time_t StoredModTime = (time_t)Record[Idx++]; 2791 ASTFileSignature StoredSignature = { 2792 {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++], 2793 (uint32_t)Record[Idx++], (uint32_t)Record[Idx++], 2794 (uint32_t)Record[Idx++]}}}; 2795 2796 std::string ImportedName = ReadString(Record, Idx); 2797 std::string ImportedFile; 2798 2799 // For prebuilt and explicit modules first consult the file map for 2800 // an override. Note that here we don't search prebuilt module 2801 // directories, only the explicit name to file mappings. Also, we will 2802 // still verify the size/signature making sure it is essentially the 2803 // same file but perhaps in a different location. 2804 if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule) 2805 ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName( 2806 ImportedName, /*FileMapOnly*/ true); 2807 2808 if (ImportedFile.empty()) 2809 // Use BaseDirectoryAsWritten to ensure we use the same path in the 2810 // ModuleCache as when writing. 2811 ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx); 2812 else 2813 SkipPath(Record, Idx); 2814 2815 // If our client can't cope with us being out of date, we can't cope with 2816 // our dependency being missing. 2817 unsigned Capabilities = ClientLoadCapabilities; 2818 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2819 Capabilities &= ~ARR_Missing; 2820 2821 // Load the AST file. 2822 auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F, 2823 Loaded, StoredSize, StoredModTime, 2824 StoredSignature, Capabilities); 2825 2826 // If we diagnosed a problem, produce a backtrace. 2827 if (isDiagnosedResult(Result, Capabilities)) 2828 Diag(diag::note_module_file_imported_by) 2829 << F.FileName << !F.ModuleName.empty() << F.ModuleName; 2830 2831 switch (Result) { 2832 case Failure: return Failure; 2833 // If we have to ignore the dependency, we'll have to ignore this too. 2834 case Missing: 2835 case OutOfDate: return OutOfDate; 2836 case VersionMismatch: return VersionMismatch; 2837 case ConfigurationMismatch: return ConfigurationMismatch; 2838 case HadErrors: return HadErrors; 2839 case Success: break; 2840 } 2841 } 2842 break; 2843 } 2844 2845 case ORIGINAL_FILE: 2846 F.OriginalSourceFileID = FileID::get(Record[0]); 2847 F.ActualOriginalSourceFileName = std::string(Blob); 2848 F.OriginalSourceFileName = F.ActualOriginalSourceFileName; 2849 ResolveImportedPath(F, F.OriginalSourceFileName); 2850 break; 2851 2852 case ORIGINAL_FILE_ID: 2853 F.OriginalSourceFileID = FileID::get(Record[0]); 2854 break; 2855 2856 case ORIGINAL_PCH_DIR: 2857 F.OriginalDir = std::string(Blob); 2858 break; 2859 2860 case MODULE_NAME: 2861 F.ModuleName = std::string(Blob); 2862 Diag(diag::remark_module_import) 2863 << F.ModuleName << F.FileName << (ImportedBy ? true : false) 2864 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 2865 if (Listener) 2866 Listener->ReadModuleName(F.ModuleName); 2867 2868 // Validate the AST as soon as we have a name so we can exit early on 2869 // failure. 2870 if (ASTReadResult Result = readUnhashedControlBlockOnce()) 2871 return Result; 2872 2873 break; 2874 2875 case MODULE_DIRECTORY: { 2876 // Save the BaseDirectory as written in the PCM for computing the module 2877 // filename for the ModuleCache. 2878 BaseDirectoryAsWritten = Blob; 2879 assert(!F.ModuleName.empty() && 2880 "MODULE_DIRECTORY found before MODULE_NAME"); 2881 // If we've already loaded a module map file covering this module, we may 2882 // have a better path for it (relative to the current build). 2883 Module *M = PP.getHeaderSearchInfo().lookupModule( 2884 F.ModuleName, /*AllowSearch*/ true, 2885 /*AllowExtraModuleMapSearch*/ true); 2886 if (M && M->Directory) { 2887 // If we're implicitly loading a module, the base directory can't 2888 // change between the build and use. 2889 // Don't emit module relocation error if we have -fno-validate-pch 2890 if (!PP.getPreprocessorOpts().DisablePCHValidation && 2891 F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) { 2892 auto BuildDir = PP.getFileManager().getDirectory(Blob); 2893 if (!BuildDir || *BuildDir != M->Directory) { 2894 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 2895 Diag(diag::err_imported_module_relocated) 2896 << F.ModuleName << Blob << M->Directory->getName(); 2897 return OutOfDate; 2898 } 2899 } 2900 F.BaseDirectory = std::string(M->Directory->getName()); 2901 } else { 2902 F.BaseDirectory = std::string(Blob); 2903 } 2904 break; 2905 } 2906 2907 case MODULE_MAP_FILE: 2908 if (ASTReadResult Result = 2909 ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities)) 2910 return Result; 2911 break; 2912 2913 case INPUT_FILE_OFFSETS: 2914 NumInputs = Record[0]; 2915 NumUserInputs = Record[1]; 2916 F.InputFileOffsets = 2917 (const llvm::support::unaligned_uint64_t *)Blob.data(); 2918 F.InputFilesLoaded.resize(NumInputs); 2919 F.NumUserInputFiles = NumUserInputs; 2920 break; 2921 } 2922 } 2923 } 2924 2925 ASTReader::ASTReadResult 2926 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 2927 BitstreamCursor &Stream = F.Stream; 2928 2929 if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) { 2930 Error(std::move(Err)); 2931 return Failure; 2932 } 2933 2934 // Read all of the records and blocks for the AST file. 2935 RecordData Record; 2936 while (true) { 2937 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 2938 if (!MaybeEntry) { 2939 Error(MaybeEntry.takeError()); 2940 return Failure; 2941 } 2942 llvm::BitstreamEntry Entry = MaybeEntry.get(); 2943 2944 switch (Entry.Kind) { 2945 case llvm::BitstreamEntry::Error: 2946 Error("error at end of module block in AST file"); 2947 return Failure; 2948 case llvm::BitstreamEntry::EndBlock: 2949 // Outside of C++, we do not store a lookup map for the translation unit. 2950 // Instead, mark it as needing a lookup map to be built if this module 2951 // contains any declarations lexically within it (which it always does!). 2952 // This usually has no cost, since we very rarely need the lookup map for 2953 // the translation unit outside C++. 2954 if (ASTContext *Ctx = ContextObj) { 2955 DeclContext *DC = Ctx->getTranslationUnitDecl(); 2956 if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus) 2957 DC->setMustBuildLookupTable(); 2958 } 2959 2960 return Success; 2961 case llvm::BitstreamEntry::SubBlock: 2962 switch (Entry.ID) { 2963 case DECLTYPES_BLOCK_ID: 2964 // We lazily load the decls block, but we want to set up the 2965 // DeclsCursor cursor to point into it. Clone our current bitcode 2966 // cursor to it, enter the block and read the abbrevs in that block. 2967 // With the main cursor, we just skip over it. 2968 F.DeclsCursor = Stream; 2969 if (llvm::Error Err = Stream.SkipBlock()) { 2970 Error(std::move(Err)); 2971 return Failure; 2972 } 2973 if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID)) { 2974 Error("malformed block record in AST file"); 2975 return Failure; 2976 } 2977 break; 2978 2979 case PREPROCESSOR_BLOCK_ID: 2980 F.MacroCursor = Stream; 2981 if (!PP.getExternalSource()) 2982 PP.setExternalSource(this); 2983 2984 if (llvm::Error Err = Stream.SkipBlock()) { 2985 Error(std::move(Err)); 2986 return Failure; 2987 } 2988 if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) { 2989 Error("malformed block record in AST file"); 2990 return Failure; 2991 } 2992 F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo(); 2993 break; 2994 2995 case PREPROCESSOR_DETAIL_BLOCK_ID: 2996 F.PreprocessorDetailCursor = Stream; 2997 2998 if (llvm::Error Err = Stream.SkipBlock()) { 2999 Error(std::move(Err)); 3000 return Failure; 3001 } 3002 if (ReadBlockAbbrevs(F.PreprocessorDetailCursor, 3003 PREPROCESSOR_DETAIL_BLOCK_ID)) { 3004 Error("malformed preprocessor detail record in AST file"); 3005 return Failure; 3006 } 3007 F.PreprocessorDetailStartOffset 3008 = F.PreprocessorDetailCursor.GetCurrentBitNo(); 3009 3010 if (!PP.getPreprocessingRecord()) 3011 PP.createPreprocessingRecord(); 3012 if (!PP.getPreprocessingRecord()->getExternalSource()) 3013 PP.getPreprocessingRecord()->SetExternalSource(*this); 3014 break; 3015 3016 case SOURCE_MANAGER_BLOCK_ID: 3017 if (ReadSourceManagerBlock(F)) 3018 return Failure; 3019 break; 3020 3021 case SUBMODULE_BLOCK_ID: 3022 if (ASTReadResult Result = 3023 ReadSubmoduleBlock(F, ClientLoadCapabilities)) 3024 return Result; 3025 break; 3026 3027 case COMMENTS_BLOCK_ID: { 3028 BitstreamCursor C = Stream; 3029 3030 if (llvm::Error Err = Stream.SkipBlock()) { 3031 Error(std::move(Err)); 3032 return Failure; 3033 } 3034 if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) { 3035 Error("malformed comments block in AST file"); 3036 return Failure; 3037 } 3038 CommentsCursors.push_back(std::make_pair(C, &F)); 3039 break; 3040 } 3041 3042 default: 3043 if (llvm::Error Err = Stream.SkipBlock()) { 3044 Error(std::move(Err)); 3045 return Failure; 3046 } 3047 break; 3048 } 3049 continue; 3050 3051 case llvm::BitstreamEntry::Record: 3052 // The interesting case. 3053 break; 3054 } 3055 3056 // Read and process a record. 3057 Record.clear(); 3058 StringRef Blob; 3059 Expected<unsigned> MaybeRecordType = 3060 Stream.readRecord(Entry.ID, Record, &Blob); 3061 if (!MaybeRecordType) { 3062 Error(MaybeRecordType.takeError()); 3063 return Failure; 3064 } 3065 ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get(); 3066 3067 // If we're not loading an AST context, we don't care about most records. 3068 if (!ContextObj) { 3069 switch (RecordType) { 3070 case IDENTIFIER_TABLE: 3071 case IDENTIFIER_OFFSET: 3072 case INTERESTING_IDENTIFIERS: 3073 case STATISTICS: 3074 case PP_CONDITIONAL_STACK: 3075 case PP_COUNTER_VALUE: 3076 case SOURCE_LOCATION_OFFSETS: 3077 case MODULE_OFFSET_MAP: 3078 case SOURCE_MANAGER_LINE_TABLE: 3079 case SOURCE_LOCATION_PRELOADS: 3080 case PPD_ENTITIES_OFFSETS: 3081 case HEADER_SEARCH_TABLE: 3082 case IMPORTED_MODULES: 3083 case MACRO_OFFSET: 3084 break; 3085 default: 3086 continue; 3087 } 3088 } 3089 3090 switch (RecordType) { 3091 default: // Default behavior: ignore. 3092 break; 3093 3094 case TYPE_OFFSET: { 3095 if (F.LocalNumTypes != 0) { 3096 Error("duplicate TYPE_OFFSET record in AST file"); 3097 return Failure; 3098 } 3099 F.TypeOffsets = (const uint32_t *)Blob.data(); 3100 F.LocalNumTypes = Record[0]; 3101 unsigned LocalBaseTypeIndex = Record[1]; 3102 F.BaseTypeIndex = getTotalNumTypes(); 3103 3104 if (F.LocalNumTypes > 0) { 3105 // Introduce the global -> local mapping for types within this module. 3106 GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F)); 3107 3108 // Introduce the local -> global mapping for types within this module. 3109 F.TypeRemap.insertOrReplace( 3110 std::make_pair(LocalBaseTypeIndex, 3111 F.BaseTypeIndex - LocalBaseTypeIndex)); 3112 3113 TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes); 3114 } 3115 break; 3116 } 3117 3118 case DECL_OFFSET: { 3119 if (F.LocalNumDecls != 0) { 3120 Error("duplicate DECL_OFFSET record in AST file"); 3121 return Failure; 3122 } 3123 F.DeclOffsets = (const DeclOffset *)Blob.data(); 3124 F.LocalNumDecls = Record[0]; 3125 unsigned LocalBaseDeclID = Record[1]; 3126 F.BaseDeclID = getTotalNumDecls(); 3127 3128 if (F.LocalNumDecls > 0) { 3129 // Introduce the global -> local mapping for declarations within this 3130 // module. 3131 GlobalDeclMap.insert( 3132 std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); 3133 3134 // Introduce the local -> global mapping for declarations within this 3135 // module. 3136 F.DeclRemap.insertOrReplace( 3137 std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID)); 3138 3139 // Introduce the global -> local mapping for declarations within this 3140 // module. 3141 F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID; 3142 3143 DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls); 3144 } 3145 break; 3146 } 3147 3148 case TU_UPDATE_LEXICAL: { 3149 DeclContext *TU = ContextObj->getTranslationUnitDecl(); 3150 LexicalContents Contents( 3151 reinterpret_cast<const llvm::support::unaligned_uint32_t *>( 3152 Blob.data()), 3153 static_cast<unsigned int>(Blob.size() / 4)); 3154 TULexicalDecls.push_back(std::make_pair(&F, Contents)); 3155 TU->setHasExternalLexicalStorage(true); 3156 break; 3157 } 3158 3159 case UPDATE_VISIBLE: { 3160 unsigned Idx = 0; 3161 serialization::DeclID ID = ReadDeclID(F, Record, Idx); 3162 auto *Data = (const unsigned char*)Blob.data(); 3163 PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); 3164 // If we've already loaded the decl, perform the updates when we finish 3165 // loading this block. 3166 if (Decl *D = GetExistingDecl(ID)) 3167 PendingUpdateRecords.push_back( 3168 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3169 break; 3170 } 3171 3172 case IDENTIFIER_TABLE: 3173 F.IdentifierTableData = Blob.data(); 3174 if (Record[0]) { 3175 F.IdentifierLookupTable = ASTIdentifierLookupTable::Create( 3176 (const unsigned char *)F.IdentifierTableData + Record[0], 3177 (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t), 3178 (const unsigned char *)F.IdentifierTableData, 3179 ASTIdentifierLookupTrait(*this, F)); 3180 3181 PP.getIdentifierTable().setExternalIdentifierLookup(this); 3182 } 3183 break; 3184 3185 case IDENTIFIER_OFFSET: { 3186 if (F.LocalNumIdentifiers != 0) { 3187 Error("duplicate IDENTIFIER_OFFSET record in AST file"); 3188 return Failure; 3189 } 3190 F.IdentifierOffsets = (const uint32_t *)Blob.data(); 3191 F.LocalNumIdentifiers = Record[0]; 3192 unsigned LocalBaseIdentifierID = Record[1]; 3193 F.BaseIdentifierID = getTotalNumIdentifiers(); 3194 3195 if (F.LocalNumIdentifiers > 0) { 3196 // Introduce the global -> local mapping for identifiers within this 3197 // module. 3198 GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1, 3199 &F)); 3200 3201 // Introduce the local -> global mapping for identifiers within this 3202 // module. 3203 F.IdentifierRemap.insertOrReplace( 3204 std::make_pair(LocalBaseIdentifierID, 3205 F.BaseIdentifierID - LocalBaseIdentifierID)); 3206 3207 IdentifiersLoaded.resize(IdentifiersLoaded.size() 3208 + F.LocalNumIdentifiers); 3209 } 3210 break; 3211 } 3212 3213 case INTERESTING_IDENTIFIERS: 3214 F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end()); 3215 break; 3216 3217 case EAGERLY_DESERIALIZED_DECLS: 3218 // FIXME: Skip reading this record if our ASTConsumer doesn't care 3219 // about "interesting" decls (for instance, if we're building a module). 3220 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3221 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3222 break; 3223 3224 case MODULAR_CODEGEN_DECLS: 3225 // FIXME: Skip reading this record if our ASTConsumer doesn't care about 3226 // them (ie: if we're not codegenerating this module). 3227 if (F.Kind == MK_MainFile) 3228 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3229 EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I])); 3230 break; 3231 3232 case SPECIAL_TYPES: 3233 if (SpecialTypes.empty()) { 3234 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3235 SpecialTypes.push_back(getGlobalTypeID(F, Record[I])); 3236 break; 3237 } 3238 3239 if (SpecialTypes.size() != Record.size()) { 3240 Error("invalid special-types record"); 3241 return Failure; 3242 } 3243 3244 for (unsigned I = 0, N = Record.size(); I != N; ++I) { 3245 serialization::TypeID ID = getGlobalTypeID(F, Record[I]); 3246 if (!SpecialTypes[I]) 3247 SpecialTypes[I] = ID; 3248 // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate 3249 // merge step? 3250 } 3251 break; 3252 3253 case STATISTICS: 3254 TotalNumStatements += Record[0]; 3255 TotalNumMacros += Record[1]; 3256 TotalLexicalDeclContexts += Record[2]; 3257 TotalVisibleDeclContexts += Record[3]; 3258 break; 3259 3260 case UNUSED_FILESCOPED_DECLS: 3261 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3262 UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I])); 3263 break; 3264 3265 case DELEGATING_CTORS: 3266 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3267 DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I])); 3268 break; 3269 3270 case WEAK_UNDECLARED_IDENTIFIERS: 3271 if (Record.size() % 4 != 0) { 3272 Error("invalid weak identifiers record"); 3273 return Failure; 3274 } 3275 3276 // FIXME: Ignore weak undeclared identifiers from non-original PCH 3277 // files. This isn't the way to do it :) 3278 WeakUndeclaredIdentifiers.clear(); 3279 3280 // Translate the weak, undeclared identifiers into global IDs. 3281 for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) { 3282 WeakUndeclaredIdentifiers.push_back( 3283 getGlobalIdentifierID(F, Record[I++])); 3284 WeakUndeclaredIdentifiers.push_back( 3285 getGlobalIdentifierID(F, Record[I++])); 3286 WeakUndeclaredIdentifiers.push_back( 3287 ReadSourceLocation(F, Record, I).getRawEncoding()); 3288 WeakUndeclaredIdentifiers.push_back(Record[I++]); 3289 } 3290 break; 3291 3292 case SELECTOR_OFFSETS: { 3293 F.SelectorOffsets = (const uint32_t *)Blob.data(); 3294 F.LocalNumSelectors = Record[0]; 3295 unsigned LocalBaseSelectorID = Record[1]; 3296 F.BaseSelectorID = getTotalNumSelectors(); 3297 3298 if (F.LocalNumSelectors > 0) { 3299 // Introduce the global -> local mapping for selectors within this 3300 // module. 3301 GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F)); 3302 3303 // Introduce the local -> global mapping for selectors within this 3304 // module. 3305 F.SelectorRemap.insertOrReplace( 3306 std::make_pair(LocalBaseSelectorID, 3307 F.BaseSelectorID - LocalBaseSelectorID)); 3308 3309 SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors); 3310 } 3311 break; 3312 } 3313 3314 case METHOD_POOL: 3315 F.SelectorLookupTableData = (const unsigned char *)Blob.data(); 3316 if (Record[0]) 3317 F.SelectorLookupTable 3318 = ASTSelectorLookupTable::Create( 3319 F.SelectorLookupTableData + Record[0], 3320 F.SelectorLookupTableData, 3321 ASTSelectorLookupTrait(*this, F)); 3322 TotalNumMethodPoolEntries += Record[1]; 3323 break; 3324 3325 case REFERENCED_SELECTOR_POOL: 3326 if (!Record.empty()) { 3327 for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) { 3328 ReferencedSelectorsData.push_back(getGlobalSelectorID(F, 3329 Record[Idx++])); 3330 ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx). 3331 getRawEncoding()); 3332 } 3333 } 3334 break; 3335 3336 case PP_CONDITIONAL_STACK: 3337 if (!Record.empty()) { 3338 unsigned Idx = 0, End = Record.size() - 1; 3339 bool ReachedEOFWhileSkipping = Record[Idx++]; 3340 llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo; 3341 if (ReachedEOFWhileSkipping) { 3342 SourceLocation HashToken = ReadSourceLocation(F, Record, Idx); 3343 SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx); 3344 bool FoundNonSkipPortion = Record[Idx++]; 3345 bool FoundElse = Record[Idx++]; 3346 SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx); 3347 SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion, 3348 FoundElse, ElseLoc); 3349 } 3350 SmallVector<PPConditionalInfo, 4> ConditionalStack; 3351 while (Idx < End) { 3352 auto Loc = ReadSourceLocation(F, Record, Idx); 3353 bool WasSkipping = Record[Idx++]; 3354 bool FoundNonSkip = Record[Idx++]; 3355 bool FoundElse = Record[Idx++]; 3356 ConditionalStack.push_back( 3357 {Loc, WasSkipping, FoundNonSkip, FoundElse}); 3358 } 3359 PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo); 3360 } 3361 break; 3362 3363 case PP_COUNTER_VALUE: 3364 if (!Record.empty() && Listener) 3365 Listener->ReadCounter(F, Record[0]); 3366 break; 3367 3368 case FILE_SORTED_DECLS: 3369 F.FileSortedDecls = (const DeclID *)Blob.data(); 3370 F.NumFileSortedDecls = Record[0]; 3371 break; 3372 3373 case SOURCE_LOCATION_OFFSETS: { 3374 F.SLocEntryOffsets = (const uint32_t *)Blob.data(); 3375 F.LocalNumSLocEntries = Record[0]; 3376 unsigned SLocSpaceSize = Record[1]; 3377 std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = 3378 SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, 3379 SLocSpaceSize); 3380 if (!F.SLocEntryBaseID) { 3381 Error("ran out of source locations"); 3382 break; 3383 } 3384 // Make our entry in the range map. BaseID is negative and growing, so 3385 // we invert it. Because we invert it, though, we need the other end of 3386 // the range. 3387 unsigned RangeStart = 3388 unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1; 3389 GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F)); 3390 F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); 3391 3392 // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. 3393 assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); 3394 GlobalSLocOffsetMap.insert( 3395 std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset 3396 - SLocSpaceSize,&F)); 3397 3398 // Initialize the remapping table. 3399 // Invalid stays invalid. 3400 F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); 3401 // This module. Base was 2 when being compiled. 3402 F.SLocRemap.insertOrReplace(std::make_pair(2U, 3403 static_cast<int>(F.SLocEntryBaseOffset - 2))); 3404 3405 TotalNumSLocEntries += F.LocalNumSLocEntries; 3406 break; 3407 } 3408 3409 case MODULE_OFFSET_MAP: 3410 F.ModuleOffsetMap = Blob; 3411 break; 3412 3413 case SOURCE_MANAGER_LINE_TABLE: 3414 if (ParseLineTable(F, Record)) { 3415 Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file"); 3416 return Failure; 3417 } 3418 break; 3419 3420 case SOURCE_LOCATION_PRELOADS: { 3421 // Need to transform from the local view (1-based IDs) to the global view, 3422 // which is based off F.SLocEntryBaseID. 3423 if (!F.PreloadSLocEntries.empty()) { 3424 Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file"); 3425 return Failure; 3426 } 3427 3428 F.PreloadSLocEntries.swap(Record); 3429 break; 3430 } 3431 3432 case EXT_VECTOR_DECLS: 3433 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3434 ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I])); 3435 break; 3436 3437 case VTABLE_USES: 3438 if (Record.size() % 3 != 0) { 3439 Error("Invalid VTABLE_USES record"); 3440 return Failure; 3441 } 3442 3443 // Later tables overwrite earlier ones. 3444 // FIXME: Modules will have some trouble with this. This is clearly not 3445 // the right way to do this. 3446 VTableUses.clear(); 3447 3448 for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { 3449 VTableUses.push_back(getGlobalDeclID(F, Record[Idx++])); 3450 VTableUses.push_back( 3451 ReadSourceLocation(F, Record, Idx).getRawEncoding()); 3452 VTableUses.push_back(Record[Idx++]); 3453 } 3454 break; 3455 3456 case PENDING_IMPLICIT_INSTANTIATIONS: 3457 if (PendingInstantiations.size() % 2 != 0) { 3458 Error("Invalid existing PendingInstantiations"); 3459 return Failure; 3460 } 3461 3462 if (Record.size() % 2 != 0) { 3463 Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); 3464 return Failure; 3465 } 3466 3467 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3468 PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++])); 3469 PendingInstantiations.push_back( 3470 ReadSourceLocation(F, Record, I).getRawEncoding()); 3471 } 3472 break; 3473 3474 case SEMA_DECL_REFS: 3475 if (Record.size() != 3) { 3476 Error("Invalid SEMA_DECL_REFS block"); 3477 return Failure; 3478 } 3479 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3480 SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3481 break; 3482 3483 case PPD_ENTITIES_OFFSETS: { 3484 F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data(); 3485 assert(Blob.size() % sizeof(PPEntityOffset) == 0); 3486 F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset); 3487 3488 unsigned LocalBasePreprocessedEntityID = Record[0]; 3489 3490 unsigned StartingID; 3491 if (!PP.getPreprocessingRecord()) 3492 PP.createPreprocessingRecord(); 3493 if (!PP.getPreprocessingRecord()->getExternalSource()) 3494 PP.getPreprocessingRecord()->SetExternalSource(*this); 3495 StartingID 3496 = PP.getPreprocessingRecord() 3497 ->allocateLoadedEntities(F.NumPreprocessedEntities); 3498 F.BasePreprocessedEntityID = StartingID; 3499 3500 if (F.NumPreprocessedEntities > 0) { 3501 // Introduce the global -> local mapping for preprocessed entities in 3502 // this module. 3503 GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F)); 3504 3505 // Introduce the local -> global mapping for preprocessed entities in 3506 // this module. 3507 F.PreprocessedEntityRemap.insertOrReplace( 3508 std::make_pair(LocalBasePreprocessedEntityID, 3509 F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID)); 3510 } 3511 3512 break; 3513 } 3514 3515 case PPD_SKIPPED_RANGES: { 3516 F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data(); 3517 assert(Blob.size() % sizeof(PPSkippedRange) == 0); 3518 F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange); 3519 3520 if (!PP.getPreprocessingRecord()) 3521 PP.createPreprocessingRecord(); 3522 if (!PP.getPreprocessingRecord()->getExternalSource()) 3523 PP.getPreprocessingRecord()->SetExternalSource(*this); 3524 F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord() 3525 ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges); 3526 3527 if (F.NumPreprocessedSkippedRanges > 0) 3528 GlobalSkippedRangeMap.insert( 3529 std::make_pair(F.BasePreprocessedSkippedRangeID, &F)); 3530 break; 3531 } 3532 3533 case DECL_UPDATE_OFFSETS: 3534 if (Record.size() % 2 != 0) { 3535 Error("invalid DECL_UPDATE_OFFSETS block in AST file"); 3536 return Failure; 3537 } 3538 for (unsigned I = 0, N = Record.size(); I != N; I += 2) { 3539 GlobalDeclID ID = getGlobalDeclID(F, Record[I]); 3540 DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1])); 3541 3542 // If we've already loaded the decl, perform the updates when we finish 3543 // loading this block. 3544 if (Decl *D = GetExistingDecl(ID)) 3545 PendingUpdateRecords.push_back( 3546 PendingUpdateRecord(ID, D, /*JustLoaded=*/false)); 3547 } 3548 break; 3549 3550 case OBJC_CATEGORIES_MAP: 3551 if (F.LocalNumObjCCategoriesInMap != 0) { 3552 Error("duplicate OBJC_CATEGORIES_MAP record in AST file"); 3553 return Failure; 3554 } 3555 3556 F.LocalNumObjCCategoriesInMap = Record[0]; 3557 F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data(); 3558 break; 3559 3560 case OBJC_CATEGORIES: 3561 F.ObjCCategories.swap(Record); 3562 break; 3563 3564 case CUDA_SPECIAL_DECL_REFS: 3565 // Later tables overwrite earlier ones. 3566 // FIXME: Modules will have trouble with this. 3567 CUDASpecialDeclRefs.clear(); 3568 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3569 CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I])); 3570 break; 3571 3572 case HEADER_SEARCH_TABLE: 3573 F.HeaderFileInfoTableData = Blob.data(); 3574 F.LocalNumHeaderFileInfos = Record[1]; 3575 if (Record[0]) { 3576 F.HeaderFileInfoTable 3577 = HeaderFileInfoLookupTable::Create( 3578 (const unsigned char *)F.HeaderFileInfoTableData + Record[0], 3579 (const unsigned char *)F.HeaderFileInfoTableData, 3580 HeaderFileInfoTrait(*this, F, 3581 &PP.getHeaderSearchInfo(), 3582 Blob.data() + Record[2])); 3583 3584 PP.getHeaderSearchInfo().SetExternalSource(this); 3585 if (!PP.getHeaderSearchInfo().getExternalLookup()) 3586 PP.getHeaderSearchInfo().SetExternalLookup(this); 3587 } 3588 break; 3589 3590 case FP_PRAGMA_OPTIONS: 3591 // Later tables overwrite earlier ones. 3592 FPPragmaOptions.swap(Record); 3593 break; 3594 3595 case OPENCL_EXTENSIONS: 3596 for (unsigned I = 0, E = Record.size(); I != E; ) { 3597 auto Name = ReadString(Record, I); 3598 auto &Opt = OpenCLExtensions.OptMap[Name]; 3599 Opt.Supported = Record[I++] != 0; 3600 Opt.Enabled = Record[I++] != 0; 3601 Opt.Avail = Record[I++]; 3602 Opt.Core = Record[I++]; 3603 } 3604 break; 3605 3606 case OPENCL_EXTENSION_TYPES: 3607 for (unsigned I = 0, E = Record.size(); I != E;) { 3608 auto TypeID = static_cast<::TypeID>(Record[I++]); 3609 auto *Type = GetType(TypeID).getTypePtr(); 3610 auto NumExt = static_cast<unsigned>(Record[I++]); 3611 for (unsigned II = 0; II != NumExt; ++II) { 3612 auto Ext = ReadString(Record, I); 3613 OpenCLTypeExtMap[Type].insert(Ext); 3614 } 3615 } 3616 break; 3617 3618 case OPENCL_EXTENSION_DECLS: 3619 for (unsigned I = 0, E = Record.size(); I != E;) { 3620 auto DeclID = static_cast<::DeclID>(Record[I++]); 3621 auto *Decl = GetDecl(DeclID); 3622 auto NumExt = static_cast<unsigned>(Record[I++]); 3623 for (unsigned II = 0; II != NumExt; ++II) { 3624 auto Ext = ReadString(Record, I); 3625 OpenCLDeclExtMap[Decl].insert(Ext); 3626 } 3627 } 3628 break; 3629 3630 case TENTATIVE_DEFINITIONS: 3631 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3632 TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I])); 3633 break; 3634 3635 case KNOWN_NAMESPACES: 3636 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3637 KnownNamespaces.push_back(getGlobalDeclID(F, Record[I])); 3638 break; 3639 3640 case UNDEFINED_BUT_USED: 3641 if (UndefinedButUsed.size() % 2 != 0) { 3642 Error("Invalid existing UndefinedButUsed"); 3643 return Failure; 3644 } 3645 3646 if (Record.size() % 2 != 0) { 3647 Error("invalid undefined-but-used record"); 3648 return Failure; 3649 } 3650 for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { 3651 UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++])); 3652 UndefinedButUsed.push_back( 3653 ReadSourceLocation(F, Record, I).getRawEncoding()); 3654 } 3655 break; 3656 3657 case DELETE_EXPRS_TO_ANALYZE: 3658 for (unsigned I = 0, N = Record.size(); I != N;) { 3659 DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++])); 3660 const uint64_t Count = Record[I++]; 3661 DelayedDeleteExprs.push_back(Count); 3662 for (uint64_t C = 0; C < Count; ++C) { 3663 DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding()); 3664 bool IsArrayForm = Record[I++] == 1; 3665 DelayedDeleteExprs.push_back(IsArrayForm); 3666 } 3667 } 3668 break; 3669 3670 case IMPORTED_MODULES: 3671 if (!F.isModule()) { 3672 // If we aren't loading a module (which has its own exports), make 3673 // all of the imported modules visible. 3674 // FIXME: Deal with macros-only imports. 3675 for (unsigned I = 0, N = Record.size(); I != N; /**/) { 3676 unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]); 3677 SourceLocation Loc = ReadSourceLocation(F, Record, I); 3678 if (GlobalID) { 3679 ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc)); 3680 if (DeserializationListener) 3681 DeserializationListener->ModuleImportRead(GlobalID, Loc); 3682 } 3683 } 3684 } 3685 break; 3686 3687 case MACRO_OFFSET: { 3688 if (F.LocalNumMacros != 0) { 3689 Error("duplicate MACRO_OFFSET record in AST file"); 3690 return Failure; 3691 } 3692 F.MacroOffsets = (const uint32_t *)Blob.data(); 3693 F.LocalNumMacros = Record[0]; 3694 unsigned LocalBaseMacroID = Record[1]; 3695 F.BaseMacroID = getTotalNumMacros(); 3696 3697 if (F.LocalNumMacros > 0) { 3698 // Introduce the global -> local mapping for macros within this module. 3699 GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F)); 3700 3701 // Introduce the local -> global mapping for macros within this module. 3702 F.MacroRemap.insertOrReplace( 3703 std::make_pair(LocalBaseMacroID, 3704 F.BaseMacroID - LocalBaseMacroID)); 3705 3706 MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros); 3707 } 3708 break; 3709 } 3710 3711 case LATE_PARSED_TEMPLATE: 3712 LateParsedTemplates.append(Record.begin(), Record.end()); 3713 break; 3714 3715 case OPTIMIZE_PRAGMA_OPTIONS: 3716 if (Record.size() != 1) { 3717 Error("invalid pragma optimize record"); 3718 return Failure; 3719 } 3720 OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]); 3721 break; 3722 3723 case MSSTRUCT_PRAGMA_OPTIONS: 3724 if (Record.size() != 1) { 3725 Error("invalid pragma ms_struct record"); 3726 return Failure; 3727 } 3728 PragmaMSStructState = Record[0]; 3729 break; 3730 3731 case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS: 3732 if (Record.size() != 2) { 3733 Error("invalid pragma ms_struct record"); 3734 return Failure; 3735 } 3736 PragmaMSPointersToMembersState = Record[0]; 3737 PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]); 3738 break; 3739 3740 case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES: 3741 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3742 UnusedLocalTypedefNameCandidates.push_back( 3743 getGlobalDeclID(F, Record[I])); 3744 break; 3745 3746 case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH: 3747 if (Record.size() != 1) { 3748 Error("invalid cuda pragma options record"); 3749 return Failure; 3750 } 3751 ForceCUDAHostDeviceDepth = Record[0]; 3752 break; 3753 3754 case PACK_PRAGMA_OPTIONS: { 3755 if (Record.size() < 3) { 3756 Error("invalid pragma pack record"); 3757 return Failure; 3758 } 3759 PragmaPackCurrentValue = Record[0]; 3760 PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]); 3761 unsigned NumStackEntries = Record[2]; 3762 unsigned Idx = 3; 3763 // Reset the stack when importing a new module. 3764 PragmaPackStack.clear(); 3765 for (unsigned I = 0; I < NumStackEntries; ++I) { 3766 PragmaPackStackEntry Entry; 3767 Entry.Value = Record[Idx++]; 3768 Entry.Location = ReadSourceLocation(F, Record[Idx++]); 3769 Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]); 3770 PragmaPackStrings.push_back(ReadString(Record, Idx)); 3771 Entry.SlotLabel = PragmaPackStrings.back(); 3772 PragmaPackStack.push_back(Entry); 3773 } 3774 break; 3775 } 3776 3777 case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS: 3778 for (unsigned I = 0, N = Record.size(); I != N; ++I) 3779 DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I])); 3780 break; 3781 } 3782 } 3783 } 3784 3785 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const { 3786 assert(!F.ModuleOffsetMap.empty() && "no module offset map to read"); 3787 3788 // Additional remapping information. 3789 const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data(); 3790 const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size(); 3791 F.ModuleOffsetMap = StringRef(); 3792 3793 // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders. 3794 if (F.SLocRemap.find(0) == F.SLocRemap.end()) { 3795 F.SLocRemap.insert(std::make_pair(0U, 0)); 3796 F.SLocRemap.insert(std::make_pair(2U, 1)); 3797 } 3798 3799 // Continuous range maps we may be updating in our module. 3800 using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; 3801 RemapBuilder SLocRemap(F.SLocRemap); 3802 RemapBuilder IdentifierRemap(F.IdentifierRemap); 3803 RemapBuilder MacroRemap(F.MacroRemap); 3804 RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); 3805 RemapBuilder SubmoduleRemap(F.SubmoduleRemap); 3806 RemapBuilder SelectorRemap(F.SelectorRemap); 3807 RemapBuilder DeclRemap(F.DeclRemap); 3808 RemapBuilder TypeRemap(F.TypeRemap); 3809 3810 while (Data < DataEnd) { 3811 // FIXME: Looking up dependency modules by filename is horrible. Let's 3812 // start fixing this with prebuilt and explicit modules and see how it 3813 // goes... 3814 using namespace llvm::support; 3815 ModuleKind Kind = static_cast<ModuleKind>( 3816 endian::readNext<uint8_t, little, unaligned>(Data)); 3817 uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data); 3818 StringRef Name = StringRef((const char*)Data, Len); 3819 Data += Len; 3820 ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule 3821 ? ModuleMgr.lookupByModuleName(Name) 3822 : ModuleMgr.lookupByFileName(Name)); 3823 if (!OM) { 3824 std::string Msg = 3825 "SourceLocation remap refers to unknown module, cannot find "; 3826 Msg.append(std::string(Name)); 3827 Error(Msg); 3828 return; 3829 } 3830 3831 uint32_t SLocOffset = 3832 endian::readNext<uint32_t, little, unaligned>(Data); 3833 uint32_t IdentifierIDOffset = 3834 endian::readNext<uint32_t, little, unaligned>(Data); 3835 uint32_t MacroIDOffset = 3836 endian::readNext<uint32_t, little, unaligned>(Data); 3837 uint32_t PreprocessedEntityIDOffset = 3838 endian::readNext<uint32_t, little, unaligned>(Data); 3839 uint32_t SubmoduleIDOffset = 3840 endian::readNext<uint32_t, little, unaligned>(Data); 3841 uint32_t SelectorIDOffset = 3842 endian::readNext<uint32_t, little, unaligned>(Data); 3843 uint32_t DeclIDOffset = 3844 endian::readNext<uint32_t, little, unaligned>(Data); 3845 uint32_t TypeIndexOffset = 3846 endian::readNext<uint32_t, little, unaligned>(Data); 3847 3848 uint32_t None = std::numeric_limits<uint32_t>::max(); 3849 3850 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, 3851 RemapBuilder &Remap) { 3852 if (Offset != None) 3853 Remap.insert(std::make_pair(Offset, 3854 static_cast<int>(BaseOffset - Offset))); 3855 }; 3856 mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); 3857 mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); 3858 mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); 3859 mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, 3860 PreprocessedEntityRemap); 3861 mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap); 3862 mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap); 3863 mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap); 3864 mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap); 3865 3866 // Global -> local mappings. 3867 F.GlobalToLocalDeclIDs[OM] = DeclIDOffset; 3868 } 3869 } 3870 3871 ASTReader::ASTReadResult 3872 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 3873 const ModuleFile *ImportedBy, 3874 unsigned ClientLoadCapabilities) { 3875 unsigned Idx = 0; 3876 F.ModuleMapPath = ReadPath(F, Record, Idx); 3877 3878 // Try to resolve ModuleName in the current header search context and 3879 // verify that it is found in the same module map file as we saved. If the 3880 // top-level AST file is a main file, skip this check because there is no 3881 // usable header search context. 3882 assert(!F.ModuleName.empty() && 3883 "MODULE_NAME should come before MODULE_MAP_FILE"); 3884 if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { 3885 // An implicitly-loaded module file should have its module listed in some 3886 // module map file that we've already loaded. 3887 Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); 3888 auto &Map = PP.getHeaderSearchInfo().getModuleMap(); 3889 const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; 3890 // Don't emit module relocation error if we have -fno-validate-pch 3891 if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) { 3892 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) { 3893 if (auto *ASTFE = M ? M->getASTFile() : nullptr) { 3894 // This module was defined by an imported (explicit) module. 3895 Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName 3896 << ASTFE->getName(); 3897 } else { 3898 // This module was built with a different module map. 3899 Diag(diag::err_imported_module_not_found) 3900 << F.ModuleName << F.FileName 3901 << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath 3902 << !ImportedBy; 3903 // In case it was imported by a PCH, there's a chance the user is 3904 // just missing to include the search path to the directory containing 3905 // the modulemap. 3906 if (ImportedBy && ImportedBy->Kind == MK_PCH) 3907 Diag(diag::note_imported_by_pch_module_not_found) 3908 << llvm::sys::path::parent_path(F.ModuleMapPath); 3909 } 3910 } 3911 return OutOfDate; 3912 } 3913 3914 assert(M->Name == F.ModuleName && "found module with different name"); 3915 3916 // Check the primary module map file. 3917 auto StoredModMap = FileMgr.getFile(F.ModuleMapPath); 3918 if (!StoredModMap || *StoredModMap != ModMap) { 3919 assert(ModMap && "found module is missing module map file"); 3920 assert((ImportedBy || F.Kind == MK_ImplicitModule) && 3921 "top-level import should be verified"); 3922 bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy; 3923 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3924 Diag(diag::err_imported_module_modmap_changed) 3925 << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName) 3926 << ModMap->getName() << F.ModuleMapPath << NotImported; 3927 return OutOfDate; 3928 } 3929 3930 llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps; 3931 for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) { 3932 // FIXME: we should use input files rather than storing names. 3933 std::string Filename = ReadPath(F, Record, Idx); 3934 auto F = FileMgr.getFile(Filename, false, false); 3935 if (!F) { 3936 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3937 Error("could not find file '" + Filename +"' referenced by AST file"); 3938 return OutOfDate; 3939 } 3940 AdditionalStoredMaps.insert(*F); 3941 } 3942 3943 // Check any additional module map files (e.g. module.private.modulemap) 3944 // that are not in the pcm. 3945 if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) { 3946 for (const FileEntry *ModMap : *AdditionalModuleMaps) { 3947 // Remove files that match 3948 // Note: SmallPtrSet::erase is really remove 3949 if (!AdditionalStoredMaps.erase(ModMap)) { 3950 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3951 Diag(diag::err_module_different_modmap) 3952 << F.ModuleName << /*new*/0 << ModMap->getName(); 3953 return OutOfDate; 3954 } 3955 } 3956 } 3957 3958 // Check any additional module map files that are in the pcm, but not 3959 // found in header search. Cases that match are already removed. 3960 for (const FileEntry *ModMap : AdditionalStoredMaps) { 3961 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 3962 Diag(diag::err_module_different_modmap) 3963 << F.ModuleName << /*not new*/1 << ModMap->getName(); 3964 return OutOfDate; 3965 } 3966 } 3967 3968 if (Listener) 3969 Listener->ReadModuleMapFile(F.ModuleMapPath); 3970 return Success; 3971 } 3972 3973 /// Move the given method to the back of the global list of methods. 3974 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) { 3975 // Find the entry for this selector in the method pool. 3976 Sema::GlobalMethodPool::iterator Known 3977 = S.MethodPool.find(Method->getSelector()); 3978 if (Known == S.MethodPool.end()) 3979 return; 3980 3981 // Retrieve the appropriate method list. 3982 ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first 3983 : Known->second.second; 3984 bool Found = false; 3985 for (ObjCMethodList *List = &Start; List; List = List->getNext()) { 3986 if (!Found) { 3987 if (List->getMethod() == Method) { 3988 Found = true; 3989 } else { 3990 // Keep searching. 3991 continue; 3992 } 3993 } 3994 3995 if (List->getNext()) 3996 List->setMethod(List->getNext()->getMethod()); 3997 else 3998 List->setMethod(Method); 3999 } 4000 } 4001 4002 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) { 4003 assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?"); 4004 for (Decl *D : Names) { 4005 bool wasHidden = D->isHidden(); 4006 D->setVisibleDespiteOwningModule(); 4007 4008 if (wasHidden && SemaObj) { 4009 if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) { 4010 moveMethodToBackOfGlobalList(*SemaObj, Method); 4011 } 4012 } 4013 } 4014 } 4015 4016 void ASTReader::makeModuleVisible(Module *Mod, 4017 Module::NameVisibilityKind NameVisibility, 4018 SourceLocation ImportLoc) { 4019 llvm::SmallPtrSet<Module *, 4> Visited; 4020 SmallVector<Module *, 4> Stack; 4021 Stack.push_back(Mod); 4022 while (!Stack.empty()) { 4023 Mod = Stack.pop_back_val(); 4024 4025 if (NameVisibility <= Mod->NameVisibility) { 4026 // This module already has this level of visibility (or greater), so 4027 // there is nothing more to do. 4028 continue; 4029 } 4030 4031 if (!Mod->isAvailable()) { 4032 // Modules that aren't available cannot be made visible. 4033 continue; 4034 } 4035 4036 // Update the module's name visibility. 4037 Mod->NameVisibility = NameVisibility; 4038 4039 // If we've already deserialized any names from this module, 4040 // mark them as visible. 4041 HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod); 4042 if (Hidden != HiddenNamesMap.end()) { 4043 auto HiddenNames = std::move(*Hidden); 4044 HiddenNamesMap.erase(Hidden); 4045 makeNamesVisible(HiddenNames.second, HiddenNames.first); 4046 assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() && 4047 "making names visible added hidden names"); 4048 } 4049 4050 // Push any exported modules onto the stack to be marked as visible. 4051 SmallVector<Module *, 16> Exports; 4052 Mod->getExportedModules(Exports); 4053 for (SmallVectorImpl<Module *>::iterator 4054 I = Exports.begin(), E = Exports.end(); I != E; ++I) { 4055 Module *Exported = *I; 4056 if (Visited.insert(Exported).second) 4057 Stack.push_back(Exported); 4058 } 4059 } 4060 } 4061 4062 /// We've merged the definition \p MergedDef into the existing definition 4063 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made 4064 /// visible. 4065 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def, 4066 NamedDecl *MergedDef) { 4067 if (Def->isHidden()) { 4068 // If MergedDef is visible or becomes visible, make the definition visible. 4069 if (!MergedDef->isHidden()) 4070 Def->setVisibleDespiteOwningModule(); 4071 else { 4072 getContext().mergeDefinitionIntoModule( 4073 Def, MergedDef->getImportedOwningModule(), 4074 /*NotifyListeners*/ false); 4075 PendingMergedDefinitionsToDeduplicate.insert(Def); 4076 } 4077 } 4078 } 4079 4080 bool ASTReader::loadGlobalIndex() { 4081 if (GlobalIndex) 4082 return false; 4083 4084 if (TriedLoadingGlobalIndex || !UseGlobalIndex || 4085 !PP.getLangOpts().Modules) 4086 return true; 4087 4088 // Try to load the global index. 4089 TriedLoadingGlobalIndex = true; 4090 StringRef ModuleCachePath 4091 = getPreprocessor().getHeaderSearchInfo().getModuleCachePath(); 4092 std::pair<GlobalModuleIndex *, llvm::Error> Result = 4093 GlobalModuleIndex::readIndex(ModuleCachePath); 4094 if (llvm::Error Err = std::move(Result.second)) { 4095 assert(!Result.first); 4096 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 4097 return true; 4098 } 4099 4100 GlobalIndex.reset(Result.first); 4101 ModuleMgr.setGlobalIndex(GlobalIndex.get()); 4102 return false; 4103 } 4104 4105 bool ASTReader::isGlobalIndexUnavailable() const { 4106 return PP.getLangOpts().Modules && UseGlobalIndex && 4107 !hasGlobalIndex() && TriedLoadingGlobalIndex; 4108 } 4109 4110 static void updateModuleTimestamp(ModuleFile &MF) { 4111 // Overwrite the timestamp file contents so that file's mtime changes. 4112 std::string TimestampFilename = MF.getTimestampFilename(); 4113 std::error_code EC; 4114 llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text); 4115 if (EC) 4116 return; 4117 OS << "Timestamp file\n"; 4118 OS.close(); 4119 OS.clear_error(); // Avoid triggering a fatal error. 4120 } 4121 4122 /// Given a cursor at the start of an AST file, scan ahead and drop the 4123 /// cursor into the start of the given block ID, returning false on success and 4124 /// true on failure. 4125 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) { 4126 while (true) { 4127 Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance(); 4128 if (!MaybeEntry) { 4129 // FIXME this drops errors on the floor. 4130 consumeError(MaybeEntry.takeError()); 4131 return true; 4132 } 4133 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4134 4135 switch (Entry.Kind) { 4136 case llvm::BitstreamEntry::Error: 4137 case llvm::BitstreamEntry::EndBlock: 4138 return true; 4139 4140 case llvm::BitstreamEntry::Record: 4141 // Ignore top-level records. 4142 if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID)) 4143 break; 4144 else { 4145 // FIXME this drops errors on the floor. 4146 consumeError(Skipped.takeError()); 4147 return true; 4148 } 4149 4150 case llvm::BitstreamEntry::SubBlock: 4151 if (Entry.ID == BlockID) { 4152 if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) { 4153 // FIXME this drops the error on the floor. 4154 consumeError(std::move(Err)); 4155 return true; 4156 } 4157 // Found it! 4158 return false; 4159 } 4160 4161 if (llvm::Error Err = Cursor.SkipBlock()) { 4162 // FIXME this drops the error on the floor. 4163 consumeError(std::move(Err)); 4164 return true; 4165 } 4166 } 4167 } 4168 } 4169 4170 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, 4171 ModuleKind Type, 4172 SourceLocation ImportLoc, 4173 unsigned ClientLoadCapabilities, 4174 SmallVectorImpl<ImportedSubmodule> *Imported) { 4175 llvm::SaveAndRestore<SourceLocation> 4176 SetCurImportLocRAII(CurrentImportLoc, ImportLoc); 4177 4178 // Defer any pending actions until we get to the end of reading the AST file. 4179 Deserializing AnASTFile(this); 4180 4181 // Bump the generation number. 4182 unsigned PreviousGeneration = 0; 4183 if (ContextObj) 4184 PreviousGeneration = incrementGeneration(*ContextObj); 4185 4186 unsigned NumModules = ModuleMgr.size(); 4187 auto removeModulesAndReturn = [&](ASTReadResult ReadResult) { 4188 assert(ReadResult && "expected to return error"); 4189 ModuleMgr.removeModules(ModuleMgr.begin() + NumModules, 4190 PP.getLangOpts().Modules 4191 ? &PP.getHeaderSearchInfo().getModuleMap() 4192 : nullptr); 4193 4194 // If we find that any modules are unusable, the global index is going 4195 // to be out-of-date. Just remove it. 4196 GlobalIndex.reset(); 4197 ModuleMgr.setGlobalIndex(nullptr); 4198 return ReadResult; 4199 }; 4200 4201 SmallVector<ImportedModule, 4> Loaded; 4202 switch (ASTReadResult ReadResult = 4203 ReadASTCore(FileName, Type, ImportLoc, 4204 /*ImportedBy=*/nullptr, Loaded, 0, 0, 4205 ASTFileSignature(), ClientLoadCapabilities)) { 4206 case Failure: 4207 case Missing: 4208 case OutOfDate: 4209 case VersionMismatch: 4210 case ConfigurationMismatch: 4211 case HadErrors: 4212 return removeModulesAndReturn(ReadResult); 4213 case Success: 4214 break; 4215 } 4216 4217 // Here comes stuff that we only do once the entire chain is loaded. 4218 4219 // Load the AST blocks of all of the modules that we loaded. We can still 4220 // hit errors parsing the ASTs at this point. 4221 for (ImportedModule &M : Loaded) { 4222 ModuleFile &F = *M.Mod; 4223 4224 // Read the AST block. 4225 if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities)) 4226 return removeModulesAndReturn(Result); 4227 4228 // The AST block should always have a definition for the main module. 4229 if (F.isModule() && !F.DidReadTopLevelSubmodule) { 4230 Error(diag::err_module_file_missing_top_level_submodule, F.FileName); 4231 return removeModulesAndReturn(Failure); 4232 } 4233 4234 // Read the extension blocks. 4235 while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) { 4236 if (ASTReadResult Result = ReadExtensionBlock(F)) 4237 return removeModulesAndReturn(Result); 4238 } 4239 4240 // Once read, set the ModuleFile bit base offset and update the size in 4241 // bits of all files we've seen. 4242 F.GlobalBitOffset = TotalModulesSizeInBits; 4243 TotalModulesSizeInBits += F.SizeInBits; 4244 GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F)); 4245 } 4246 4247 // Preload source locations and interesting indentifiers. 4248 for (ImportedModule &M : Loaded) { 4249 ModuleFile &F = *M.Mod; 4250 4251 // Preload SLocEntries. 4252 for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) { 4253 int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID; 4254 // Load it through the SourceManager and don't call ReadSLocEntry() 4255 // directly because the entry may have already been loaded in which case 4256 // calling ReadSLocEntry() directly would trigger an assertion in 4257 // SourceManager. 4258 SourceMgr.getLoadedSLocEntryByID(Index); 4259 } 4260 4261 // Map the original source file ID into the ID space of the current 4262 // compilation. 4263 if (F.OriginalSourceFileID.isValid()) { 4264 F.OriginalSourceFileID = FileID::get( 4265 F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1); 4266 } 4267 4268 // Preload all the pending interesting identifiers by marking them out of 4269 // date. 4270 for (auto Offset : F.PreloadIdentifierOffsets) { 4271 const unsigned char *Data = reinterpret_cast<const unsigned char *>( 4272 F.IdentifierTableData + Offset); 4273 4274 ASTIdentifierLookupTrait Trait(*this, F); 4275 auto KeyDataLen = Trait.ReadKeyDataLength(Data); 4276 auto Key = Trait.ReadKey(Data, KeyDataLen.first); 4277 auto &II = PP.getIdentifierTable().getOwn(Key); 4278 II.setOutOfDate(true); 4279 4280 // Mark this identifier as being from an AST file so that we can track 4281 // whether we need to serialize it. 4282 markIdentifierFromAST(*this, II); 4283 4284 // Associate the ID with the identifier so that the writer can reuse it. 4285 auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first); 4286 SetIdentifierInfo(ID, &II); 4287 } 4288 } 4289 4290 // Setup the import locations and notify the module manager that we've 4291 // committed to these module files. 4292 for (ImportedModule &M : Loaded) { 4293 ModuleFile &F = *M.Mod; 4294 4295 ModuleMgr.moduleFileAccepted(&F); 4296 4297 // Set the import location. 4298 F.DirectImportLoc = ImportLoc; 4299 // FIXME: We assume that locations from PCH / preamble do not need 4300 // any translation. 4301 if (!M.ImportedBy) 4302 F.ImportLoc = M.ImportLoc; 4303 else 4304 F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc); 4305 } 4306 4307 if (!PP.getLangOpts().CPlusPlus || 4308 (Type != MK_ImplicitModule && Type != MK_ExplicitModule && 4309 Type != MK_PrebuiltModule)) { 4310 // Mark all of the identifiers in the identifier table as being out of date, 4311 // so that various accessors know to check the loaded modules when the 4312 // identifier is used. 4313 // 4314 // For C++ modules, we don't need information on many identifiers (just 4315 // those that provide macros or are poisoned), so we mark all of 4316 // the interesting ones via PreloadIdentifierOffsets. 4317 for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(), 4318 IdEnd = PP.getIdentifierTable().end(); 4319 Id != IdEnd; ++Id) 4320 Id->second->setOutOfDate(true); 4321 } 4322 // Mark selectors as out of date. 4323 for (auto Sel : SelectorGeneration) 4324 SelectorOutOfDate[Sel.first] = true; 4325 4326 // Resolve any unresolved module exports. 4327 for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) { 4328 UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I]; 4329 SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID); 4330 Module *ResolvedMod = getSubmodule(GlobalID); 4331 4332 switch (Unresolved.Kind) { 4333 case UnresolvedModuleRef::Conflict: 4334 if (ResolvedMod) { 4335 Module::Conflict Conflict; 4336 Conflict.Other = ResolvedMod; 4337 Conflict.Message = Unresolved.String.str(); 4338 Unresolved.Mod->Conflicts.push_back(Conflict); 4339 } 4340 continue; 4341 4342 case UnresolvedModuleRef::Import: 4343 if (ResolvedMod) 4344 Unresolved.Mod->Imports.insert(ResolvedMod); 4345 continue; 4346 4347 case UnresolvedModuleRef::Export: 4348 if (ResolvedMod || Unresolved.IsWildcard) 4349 Unresolved.Mod->Exports.push_back( 4350 Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard)); 4351 continue; 4352 } 4353 } 4354 UnresolvedModuleRefs.clear(); 4355 4356 if (Imported) 4357 Imported->append(ImportedModules.begin(), 4358 ImportedModules.end()); 4359 4360 // FIXME: How do we load the 'use'd modules? They may not be submodules. 4361 // Might be unnecessary as use declarations are only used to build the 4362 // module itself. 4363 4364 if (ContextObj) 4365 InitializeContext(); 4366 4367 if (SemaObj) 4368 UpdateSema(); 4369 4370 if (DeserializationListener) 4371 DeserializationListener->ReaderInitialized(this); 4372 4373 ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule(); 4374 if (PrimaryModule.OriginalSourceFileID.isValid()) { 4375 // If this AST file is a precompiled preamble, then set the 4376 // preamble file ID of the source manager to the file source file 4377 // from which the preamble was built. 4378 if (Type == MK_Preamble) { 4379 SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID); 4380 } else if (Type == MK_MainFile) { 4381 SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID); 4382 } 4383 } 4384 4385 // For any Objective-C class definitions we have already loaded, make sure 4386 // that we load any additional categories. 4387 if (ContextObj) { 4388 for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { 4389 loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), 4390 ObjCClassesLoaded[I], 4391 PreviousGeneration); 4392 } 4393 } 4394 4395 if (PP.getHeaderSearchInfo() 4396 .getHeaderSearchOpts() 4397 .ModulesValidateOncePerBuildSession) { 4398 // Now we are certain that the module and all modules it depends on are 4399 // up to date. Create or update timestamp files for modules that are 4400 // located in the module cache (not for PCH files that could be anywhere 4401 // in the filesystem). 4402 for (unsigned I = 0, N = Loaded.size(); I != N; ++I) { 4403 ImportedModule &M = Loaded[I]; 4404 if (M.Mod->Kind == MK_ImplicitModule) { 4405 updateModuleTimestamp(*M.Mod); 4406 } 4407 } 4408 } 4409 4410 return Success; 4411 } 4412 4413 static ASTFileSignature readASTFileSignature(StringRef PCH); 4414 4415 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'. 4416 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) { 4417 // FIXME checking magic headers is done in other places such as 4418 // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't 4419 // always done the same. Unify it all with a helper. 4420 if (!Stream.canSkipToPos(4)) 4421 return llvm::createStringError(std::errc::illegal_byte_sequence, 4422 "file too small to contain AST file magic"); 4423 for (unsigned C : {'C', 'P', 'C', 'H'}) 4424 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) { 4425 if (Res.get() != C) 4426 return llvm::createStringError( 4427 std::errc::illegal_byte_sequence, 4428 "file doesn't start with AST file magic"); 4429 } else 4430 return Res.takeError(); 4431 return llvm::Error::success(); 4432 } 4433 4434 static unsigned moduleKindForDiagnostic(ModuleKind Kind) { 4435 switch (Kind) { 4436 case MK_PCH: 4437 return 0; // PCH 4438 case MK_ImplicitModule: 4439 case MK_ExplicitModule: 4440 case MK_PrebuiltModule: 4441 return 1; // module 4442 case MK_MainFile: 4443 case MK_Preamble: 4444 return 2; // main source file 4445 } 4446 llvm_unreachable("unknown module kind"); 4447 } 4448 4449 ASTReader::ASTReadResult 4450 ASTReader::ReadASTCore(StringRef FileName, 4451 ModuleKind Type, 4452 SourceLocation ImportLoc, 4453 ModuleFile *ImportedBy, 4454 SmallVectorImpl<ImportedModule> &Loaded, 4455 off_t ExpectedSize, time_t ExpectedModTime, 4456 ASTFileSignature ExpectedSignature, 4457 unsigned ClientLoadCapabilities) { 4458 ModuleFile *M; 4459 std::string ErrorStr; 4460 ModuleManager::AddModuleResult AddResult 4461 = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy, 4462 getGeneration(), ExpectedSize, ExpectedModTime, 4463 ExpectedSignature, readASTFileSignature, 4464 M, ErrorStr); 4465 4466 switch (AddResult) { 4467 case ModuleManager::AlreadyLoaded: 4468 Diag(diag::remark_module_import) 4469 << M->ModuleName << M->FileName << (ImportedBy ? true : false) 4470 << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef()); 4471 return Success; 4472 4473 case ModuleManager::NewlyLoaded: 4474 // Load module file below. 4475 break; 4476 4477 case ModuleManager::Missing: 4478 // The module file was missing; if the client can handle that, return 4479 // it. 4480 if (ClientLoadCapabilities & ARR_Missing) 4481 return Missing; 4482 4483 // Otherwise, return an error. 4484 Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type) 4485 << FileName << !ErrorStr.empty() 4486 << ErrorStr; 4487 return Failure; 4488 4489 case ModuleManager::OutOfDate: 4490 // We couldn't load the module file because it is out-of-date. If the 4491 // client can handle out-of-date, return it. 4492 if (ClientLoadCapabilities & ARR_OutOfDate) 4493 return OutOfDate; 4494 4495 // Otherwise, return an error. 4496 Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type) 4497 << FileName << !ErrorStr.empty() 4498 << ErrorStr; 4499 return Failure; 4500 } 4501 4502 assert(M && "Missing module file"); 4503 4504 bool ShouldFinalizePCM = false; 4505 auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() { 4506 auto &MC = getModuleManager().getModuleCache(); 4507 if (ShouldFinalizePCM) 4508 MC.finalizePCM(FileName); 4509 else 4510 MC.tryToDropPCM(FileName); 4511 }); 4512 ModuleFile &F = *M; 4513 BitstreamCursor &Stream = F.Stream; 4514 Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer)); 4515 F.SizeInBits = F.Buffer->getBufferSize() * 8; 4516 4517 // Sniff for the signature. 4518 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4519 Diag(diag::err_module_file_invalid) 4520 << moduleKindForDiagnostic(Type) << FileName << std::move(Err); 4521 return Failure; 4522 } 4523 4524 // This is used for compatibility with older PCH formats. 4525 bool HaveReadControlBlock = false; 4526 while (true) { 4527 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4528 if (!MaybeEntry) { 4529 Error(MaybeEntry.takeError()); 4530 return Failure; 4531 } 4532 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4533 4534 switch (Entry.Kind) { 4535 case llvm::BitstreamEntry::Error: 4536 case llvm::BitstreamEntry::Record: 4537 case llvm::BitstreamEntry::EndBlock: 4538 Error("invalid record at top-level of AST file"); 4539 return Failure; 4540 4541 case llvm::BitstreamEntry::SubBlock: 4542 break; 4543 } 4544 4545 switch (Entry.ID) { 4546 case CONTROL_BLOCK_ID: 4547 HaveReadControlBlock = true; 4548 switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) { 4549 case Success: 4550 // Check that we didn't try to load a non-module AST file as a module. 4551 // 4552 // FIXME: Should we also perform the converse check? Loading a module as 4553 // a PCH file sort of works, but it's a bit wonky. 4554 if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule || 4555 Type == MK_PrebuiltModule) && 4556 F.ModuleName.empty()) { 4557 auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure; 4558 if (Result != OutOfDate || 4559 (ClientLoadCapabilities & ARR_OutOfDate) == 0) 4560 Diag(diag::err_module_file_not_module) << FileName; 4561 return Result; 4562 } 4563 break; 4564 4565 case Failure: return Failure; 4566 case Missing: return Missing; 4567 case OutOfDate: return OutOfDate; 4568 case VersionMismatch: return VersionMismatch; 4569 case ConfigurationMismatch: return ConfigurationMismatch; 4570 case HadErrors: return HadErrors; 4571 } 4572 break; 4573 4574 case AST_BLOCK_ID: 4575 if (!HaveReadControlBlock) { 4576 if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0) 4577 Diag(diag::err_pch_version_too_old); 4578 return VersionMismatch; 4579 } 4580 4581 // Record that we've loaded this module. 4582 Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc)); 4583 ShouldFinalizePCM = true; 4584 return Success; 4585 4586 case UNHASHED_CONTROL_BLOCK_ID: 4587 // This block is handled using look-ahead during ReadControlBlock. We 4588 // shouldn't get here! 4589 Error("malformed block record in AST file"); 4590 return Failure; 4591 4592 default: 4593 if (llvm::Error Err = Stream.SkipBlock()) { 4594 Error(std::move(Err)); 4595 return Failure; 4596 } 4597 break; 4598 } 4599 } 4600 4601 llvm_unreachable("unexpected break; expected return"); 4602 } 4603 4604 ASTReader::ASTReadResult 4605 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy, 4606 unsigned ClientLoadCapabilities) { 4607 const HeaderSearchOptions &HSOpts = 4608 PP.getHeaderSearchInfo().getHeaderSearchOpts(); 4609 bool AllowCompatibleConfigurationMismatch = 4610 F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule; 4611 4612 ASTReadResult Result = readUnhashedControlBlockImpl( 4613 &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch, 4614 Listener.get(), 4615 WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions); 4616 4617 // If F was directly imported by another module, it's implicitly validated by 4618 // the importing module. 4619 if (DisableValidation || WasImportedBy || 4620 (AllowConfigurationMismatch && Result == ConfigurationMismatch)) 4621 return Success; 4622 4623 if (Result == Failure) { 4624 Error("malformed block record in AST file"); 4625 return Failure; 4626 } 4627 4628 if (Result == OutOfDate && F.Kind == MK_ImplicitModule) { 4629 // If this module has already been finalized in the ModuleCache, we're stuck 4630 // with it; we can only load a single version of each module. 4631 // 4632 // This can happen when a module is imported in two contexts: in one, as a 4633 // user module; in another, as a system module (due to an import from 4634 // another module marked with the [system] flag). It usually indicates a 4635 // bug in the module map: this module should also be marked with [system]. 4636 // 4637 // If -Wno-system-headers (the default), and the first import is as a 4638 // system module, then validation will fail during the as-user import, 4639 // since -Werror flags won't have been validated. However, it's reasonable 4640 // to treat this consistently as a system module. 4641 // 4642 // If -Wsystem-headers, the PCM on disk was built with 4643 // -Wno-system-headers, and the first import is as a user module, then 4644 // validation will fail during the as-system import since the PCM on disk 4645 // doesn't guarantee that -Werror was respected. However, the -Werror 4646 // flags were checked during the initial as-user import. 4647 if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) { 4648 Diag(diag::warn_module_system_bit_conflict) << F.FileName; 4649 return Success; 4650 } 4651 } 4652 4653 return Result; 4654 } 4655 4656 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl( 4657 ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities, 4658 bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener, 4659 bool ValidateDiagnosticOptions) { 4660 // Initialize a stream. 4661 BitstreamCursor Stream(StreamData); 4662 4663 // Sniff for the signature. 4664 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4665 // FIXME this drops the error on the floor. 4666 consumeError(std::move(Err)); 4667 return Failure; 4668 } 4669 4670 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4671 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4672 return Failure; 4673 4674 // Read all of the records in the options block. 4675 RecordData Record; 4676 ASTReadResult Result = Success; 4677 while (true) { 4678 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4679 if (!MaybeEntry) { 4680 // FIXME this drops the error on the floor. 4681 consumeError(MaybeEntry.takeError()); 4682 return Failure; 4683 } 4684 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4685 4686 switch (Entry.Kind) { 4687 case llvm::BitstreamEntry::Error: 4688 case llvm::BitstreamEntry::SubBlock: 4689 return Failure; 4690 4691 case llvm::BitstreamEntry::EndBlock: 4692 return Result; 4693 4694 case llvm::BitstreamEntry::Record: 4695 // The interesting case. 4696 break; 4697 } 4698 4699 // Read and process a record. 4700 Record.clear(); 4701 Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record); 4702 if (!MaybeRecordType) { 4703 // FIXME this drops the error. 4704 return Failure; 4705 } 4706 switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) { 4707 case SIGNATURE: 4708 if (F) 4709 std::copy(Record.begin(), Record.end(), F->Signature.data()); 4710 break; 4711 case DIAGNOSTIC_OPTIONS: { 4712 bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0; 4713 if (Listener && ValidateDiagnosticOptions && 4714 !AllowCompatibleConfigurationMismatch && 4715 ParseDiagnosticOptions(Record, Complain, *Listener)) 4716 Result = OutOfDate; // Don't return early. Read the signature. 4717 break; 4718 } 4719 case DIAG_PRAGMA_MAPPINGS: 4720 if (!F) 4721 break; 4722 if (F->PragmaDiagMappings.empty()) 4723 F->PragmaDiagMappings.swap(Record); 4724 else 4725 F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(), 4726 Record.begin(), Record.end()); 4727 break; 4728 } 4729 } 4730 } 4731 4732 /// Parse a record and blob containing module file extension metadata. 4733 static bool parseModuleFileExtensionMetadata( 4734 const SmallVectorImpl<uint64_t> &Record, 4735 StringRef Blob, 4736 ModuleFileExtensionMetadata &Metadata) { 4737 if (Record.size() < 4) return true; 4738 4739 Metadata.MajorVersion = Record[0]; 4740 Metadata.MinorVersion = Record[1]; 4741 4742 unsigned BlockNameLen = Record[2]; 4743 unsigned UserInfoLen = Record[3]; 4744 4745 if (BlockNameLen + UserInfoLen > Blob.size()) return true; 4746 4747 Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen); 4748 Metadata.UserInfo = std::string(Blob.data() + BlockNameLen, 4749 Blob.data() + BlockNameLen + UserInfoLen); 4750 return false; 4751 } 4752 4753 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) { 4754 BitstreamCursor &Stream = F.Stream; 4755 4756 RecordData Record; 4757 while (true) { 4758 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 4759 if (!MaybeEntry) { 4760 Error(MaybeEntry.takeError()); 4761 return Failure; 4762 } 4763 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4764 4765 switch (Entry.Kind) { 4766 case llvm::BitstreamEntry::SubBlock: 4767 if (llvm::Error Err = Stream.SkipBlock()) { 4768 Error(std::move(Err)); 4769 return Failure; 4770 } 4771 continue; 4772 4773 case llvm::BitstreamEntry::EndBlock: 4774 return Success; 4775 4776 case llvm::BitstreamEntry::Error: 4777 return HadErrors; 4778 4779 case llvm::BitstreamEntry::Record: 4780 break; 4781 } 4782 4783 Record.clear(); 4784 StringRef Blob; 4785 Expected<unsigned> MaybeRecCode = 4786 Stream.readRecord(Entry.ID, Record, &Blob); 4787 if (!MaybeRecCode) { 4788 Error(MaybeRecCode.takeError()); 4789 return Failure; 4790 } 4791 switch (MaybeRecCode.get()) { 4792 case EXTENSION_METADATA: { 4793 ModuleFileExtensionMetadata Metadata; 4794 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) { 4795 Error("malformed EXTENSION_METADATA in AST file"); 4796 return Failure; 4797 } 4798 4799 // Find a module file extension with this block name. 4800 auto Known = ModuleFileExtensions.find(Metadata.BlockName); 4801 if (Known == ModuleFileExtensions.end()) break; 4802 4803 // Form a reader. 4804 if (auto Reader = Known->second->createExtensionReader(Metadata, *this, 4805 F, Stream)) { 4806 F.ExtensionReaders.push_back(std::move(Reader)); 4807 } 4808 4809 break; 4810 } 4811 } 4812 } 4813 4814 return Success; 4815 } 4816 4817 void ASTReader::InitializeContext() { 4818 assert(ContextObj && "no context to initialize"); 4819 ASTContext &Context = *ContextObj; 4820 4821 // If there's a listener, notify them that we "read" the translation unit. 4822 if (DeserializationListener) 4823 DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID, 4824 Context.getTranslationUnitDecl()); 4825 4826 // FIXME: Find a better way to deal with collisions between these 4827 // built-in types. Right now, we just ignore the problem. 4828 4829 // Load the special types. 4830 if (SpecialTypes.size() >= NumSpecialTypeIDs) { 4831 if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) { 4832 if (!Context.CFConstantStringTypeDecl) 4833 Context.setCFConstantStringType(GetType(String)); 4834 } 4835 4836 if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) { 4837 QualType FileType = GetType(File); 4838 if (FileType.isNull()) { 4839 Error("FILE type is NULL"); 4840 return; 4841 } 4842 4843 if (!Context.FILEDecl) { 4844 if (const TypedefType *Typedef = FileType->getAs<TypedefType>()) 4845 Context.setFILEDecl(Typedef->getDecl()); 4846 else { 4847 const TagType *Tag = FileType->getAs<TagType>(); 4848 if (!Tag) { 4849 Error("Invalid FILE type in AST file"); 4850 return; 4851 } 4852 Context.setFILEDecl(Tag->getDecl()); 4853 } 4854 } 4855 } 4856 4857 if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) { 4858 QualType Jmp_bufType = GetType(Jmp_buf); 4859 if (Jmp_bufType.isNull()) { 4860 Error("jmp_buf type is NULL"); 4861 return; 4862 } 4863 4864 if (!Context.jmp_bufDecl) { 4865 if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>()) 4866 Context.setjmp_bufDecl(Typedef->getDecl()); 4867 else { 4868 const TagType *Tag = Jmp_bufType->getAs<TagType>(); 4869 if (!Tag) { 4870 Error("Invalid jmp_buf type in AST file"); 4871 return; 4872 } 4873 Context.setjmp_bufDecl(Tag->getDecl()); 4874 } 4875 } 4876 } 4877 4878 if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) { 4879 QualType Sigjmp_bufType = GetType(Sigjmp_buf); 4880 if (Sigjmp_bufType.isNull()) { 4881 Error("sigjmp_buf type is NULL"); 4882 return; 4883 } 4884 4885 if (!Context.sigjmp_bufDecl) { 4886 if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>()) 4887 Context.setsigjmp_bufDecl(Typedef->getDecl()); 4888 else { 4889 const TagType *Tag = Sigjmp_bufType->getAs<TagType>(); 4890 assert(Tag && "Invalid sigjmp_buf type in AST file"); 4891 Context.setsigjmp_bufDecl(Tag->getDecl()); 4892 } 4893 } 4894 } 4895 4896 if (unsigned ObjCIdRedef 4897 = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) { 4898 if (Context.ObjCIdRedefinitionType.isNull()) 4899 Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef); 4900 } 4901 4902 if (unsigned ObjCClassRedef 4903 = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) { 4904 if (Context.ObjCClassRedefinitionType.isNull()) 4905 Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef); 4906 } 4907 4908 if (unsigned ObjCSelRedef 4909 = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) { 4910 if (Context.ObjCSelRedefinitionType.isNull()) 4911 Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef); 4912 } 4913 4914 if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) { 4915 QualType Ucontext_tType = GetType(Ucontext_t); 4916 if (Ucontext_tType.isNull()) { 4917 Error("ucontext_t type is NULL"); 4918 return; 4919 } 4920 4921 if (!Context.ucontext_tDecl) { 4922 if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>()) 4923 Context.setucontext_tDecl(Typedef->getDecl()); 4924 else { 4925 const TagType *Tag = Ucontext_tType->getAs<TagType>(); 4926 assert(Tag && "Invalid ucontext_t type in AST file"); 4927 Context.setucontext_tDecl(Tag->getDecl()); 4928 } 4929 } 4930 } 4931 } 4932 4933 ReadPragmaDiagnosticMappings(Context.getDiagnostics()); 4934 4935 // If there were any CUDA special declarations, deserialize them. 4936 if (!CUDASpecialDeclRefs.empty()) { 4937 assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!"); 4938 Context.setcudaConfigureCallDecl( 4939 cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0]))); 4940 } 4941 4942 // Re-export any modules that were imported by a non-module AST file. 4943 // FIXME: This does not make macro-only imports visible again. 4944 for (auto &Import : ImportedModules) { 4945 if (Module *Imported = getSubmodule(Import.ID)) { 4946 makeModuleVisible(Imported, Module::AllVisible, 4947 /*ImportLoc=*/Import.ImportLoc); 4948 if (Import.ImportLoc.isValid()) 4949 PP.makeModuleVisible(Imported, Import.ImportLoc); 4950 // FIXME: should we tell Sema to make the module visible too? 4951 } 4952 } 4953 ImportedModules.clear(); 4954 } 4955 4956 void ASTReader::finalizeForWriting() { 4957 // Nothing to do for now. 4958 } 4959 4960 /// Reads and return the signature record from \p PCH's control block, or 4961 /// else returns 0. 4962 static ASTFileSignature readASTFileSignature(StringRef PCH) { 4963 BitstreamCursor Stream(PCH); 4964 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 4965 // FIXME this drops the error on the floor. 4966 consumeError(std::move(Err)); 4967 return ASTFileSignature(); 4968 } 4969 4970 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 4971 if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID)) 4972 return ASTFileSignature(); 4973 4974 // Scan for SIGNATURE inside the diagnostic options block. 4975 ASTReader::RecordData Record; 4976 while (true) { 4977 Expected<llvm::BitstreamEntry> MaybeEntry = 4978 Stream.advanceSkippingSubblocks(); 4979 if (!MaybeEntry) { 4980 // FIXME this drops the error on the floor. 4981 consumeError(MaybeEntry.takeError()); 4982 return ASTFileSignature(); 4983 } 4984 llvm::BitstreamEntry Entry = MaybeEntry.get(); 4985 4986 if (Entry.Kind != llvm::BitstreamEntry::Record) 4987 return ASTFileSignature(); 4988 4989 Record.clear(); 4990 StringRef Blob; 4991 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 4992 if (!MaybeRecord) { 4993 // FIXME this drops the error on the floor. 4994 consumeError(MaybeRecord.takeError()); 4995 return ASTFileSignature(); 4996 } 4997 if (SIGNATURE == MaybeRecord.get()) 4998 return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2], 4999 (uint32_t)Record[3], (uint32_t)Record[4]}}}; 5000 } 5001 } 5002 5003 /// Retrieve the name of the original source file name 5004 /// directly from the AST file, without actually loading the AST 5005 /// file. 5006 std::string ASTReader::getOriginalSourceFile( 5007 const std::string &ASTFileName, FileManager &FileMgr, 5008 const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) { 5009 // Open the AST file. 5010 auto Buffer = FileMgr.getBufferForFile(ASTFileName); 5011 if (!Buffer) { 5012 Diags.Report(diag::err_fe_unable_to_read_pch_file) 5013 << ASTFileName << Buffer.getError().message(); 5014 return std::string(); 5015 } 5016 5017 // Initialize the stream 5018 BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer)); 5019 5020 // Sniff for the signature. 5021 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5022 Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err); 5023 return std::string(); 5024 } 5025 5026 // Scan for the CONTROL_BLOCK_ID block. 5027 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) { 5028 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5029 return std::string(); 5030 } 5031 5032 // Scan for ORIGINAL_FILE inside the control block. 5033 RecordData Record; 5034 while (true) { 5035 Expected<llvm::BitstreamEntry> MaybeEntry = 5036 Stream.advanceSkippingSubblocks(); 5037 if (!MaybeEntry) { 5038 // FIXME this drops errors on the floor. 5039 consumeError(MaybeEntry.takeError()); 5040 return std::string(); 5041 } 5042 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5043 5044 if (Entry.Kind == llvm::BitstreamEntry::EndBlock) 5045 return std::string(); 5046 5047 if (Entry.Kind != llvm::BitstreamEntry::Record) { 5048 Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName; 5049 return std::string(); 5050 } 5051 5052 Record.clear(); 5053 StringRef Blob; 5054 Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob); 5055 if (!MaybeRecord) { 5056 // FIXME this drops the errors on the floor. 5057 consumeError(MaybeRecord.takeError()); 5058 return std::string(); 5059 } 5060 if (ORIGINAL_FILE == MaybeRecord.get()) 5061 return Blob.str(); 5062 } 5063 } 5064 5065 namespace { 5066 5067 class SimplePCHValidator : public ASTReaderListener { 5068 const LangOptions &ExistingLangOpts; 5069 const TargetOptions &ExistingTargetOpts; 5070 const PreprocessorOptions &ExistingPPOpts; 5071 std::string ExistingModuleCachePath; 5072 FileManager &FileMgr; 5073 5074 public: 5075 SimplePCHValidator(const LangOptions &ExistingLangOpts, 5076 const TargetOptions &ExistingTargetOpts, 5077 const PreprocessorOptions &ExistingPPOpts, 5078 StringRef ExistingModuleCachePath, FileManager &FileMgr) 5079 : ExistingLangOpts(ExistingLangOpts), 5080 ExistingTargetOpts(ExistingTargetOpts), 5081 ExistingPPOpts(ExistingPPOpts), 5082 ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {} 5083 5084 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, 5085 bool AllowCompatibleDifferences) override { 5086 return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr, 5087 AllowCompatibleDifferences); 5088 } 5089 5090 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, 5091 bool AllowCompatibleDifferences) override { 5092 return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr, 5093 AllowCompatibleDifferences); 5094 } 5095 5096 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, 5097 StringRef SpecificModuleCachePath, 5098 bool Complain) override { 5099 return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5100 ExistingModuleCachePath, 5101 nullptr, ExistingLangOpts); 5102 } 5103 5104 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts, 5105 bool Complain, 5106 std::string &SuggestedPredefines) override { 5107 return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr, 5108 SuggestedPredefines, ExistingLangOpts); 5109 } 5110 }; 5111 5112 } // namespace 5113 5114 bool ASTReader::readASTFileControlBlock( 5115 StringRef Filename, FileManager &FileMgr, 5116 const PCHContainerReader &PCHContainerRdr, 5117 bool FindModuleFileExtensions, 5118 ASTReaderListener &Listener, bool ValidateDiagnosticOptions) { 5119 // Open the AST file. 5120 // FIXME: This allows use of the VFS; we do not allow use of the 5121 // VFS when actually loading a module. 5122 auto Buffer = FileMgr.getBufferForFile(Filename); 5123 if (!Buffer) { 5124 return true; 5125 } 5126 5127 // Initialize the stream 5128 StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer); 5129 BitstreamCursor Stream(Bytes); 5130 5131 // Sniff for the signature. 5132 if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) { 5133 consumeError(std::move(Err)); // FIXME this drops errors on the floor. 5134 return true; 5135 } 5136 5137 // Scan for the CONTROL_BLOCK_ID block. 5138 if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) 5139 return true; 5140 5141 bool NeedsInputFiles = Listener.needsInputFileVisitation(); 5142 bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation(); 5143 bool NeedsImports = Listener.needsImportVisitation(); 5144 BitstreamCursor InputFilesCursor; 5145 5146 RecordData Record; 5147 std::string ModuleDir; 5148 bool DoneWithControlBlock = false; 5149 while (!DoneWithControlBlock) { 5150 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5151 if (!MaybeEntry) { 5152 // FIXME this drops the error on the floor. 5153 consumeError(MaybeEntry.takeError()); 5154 return true; 5155 } 5156 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5157 5158 switch (Entry.Kind) { 5159 case llvm::BitstreamEntry::SubBlock: { 5160 switch (Entry.ID) { 5161 case OPTIONS_BLOCK_ID: { 5162 std::string IgnoredSuggestedPredefines; 5163 if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate, 5164 /*AllowCompatibleConfigurationMismatch*/ false, 5165 Listener, IgnoredSuggestedPredefines) != Success) 5166 return true; 5167 break; 5168 } 5169 5170 case INPUT_FILES_BLOCK_ID: 5171 InputFilesCursor = Stream; 5172 if (llvm::Error Err = Stream.SkipBlock()) { 5173 // FIXME this drops the error on the floor. 5174 consumeError(std::move(Err)); 5175 return true; 5176 } 5177 if (NeedsInputFiles && 5178 ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID)) 5179 return true; 5180 break; 5181 5182 default: 5183 if (llvm::Error Err = Stream.SkipBlock()) { 5184 // FIXME this drops the error on the floor. 5185 consumeError(std::move(Err)); 5186 return true; 5187 } 5188 break; 5189 } 5190 5191 continue; 5192 } 5193 5194 case llvm::BitstreamEntry::EndBlock: 5195 DoneWithControlBlock = true; 5196 break; 5197 5198 case llvm::BitstreamEntry::Error: 5199 return true; 5200 5201 case llvm::BitstreamEntry::Record: 5202 break; 5203 } 5204 5205 if (DoneWithControlBlock) break; 5206 5207 Record.clear(); 5208 StringRef Blob; 5209 Expected<unsigned> MaybeRecCode = 5210 Stream.readRecord(Entry.ID, Record, &Blob); 5211 if (!MaybeRecCode) { 5212 // FIXME this drops the error. 5213 return Failure; 5214 } 5215 switch ((ControlRecordTypes)MaybeRecCode.get()) { 5216 case METADATA: 5217 if (Record[0] != VERSION_MAJOR) 5218 return true; 5219 if (Listener.ReadFullVersionInformation(Blob)) 5220 return true; 5221 break; 5222 case MODULE_NAME: 5223 Listener.ReadModuleName(Blob); 5224 break; 5225 case MODULE_DIRECTORY: 5226 ModuleDir = std::string(Blob); 5227 break; 5228 case MODULE_MAP_FILE: { 5229 unsigned Idx = 0; 5230 auto Path = ReadString(Record, Idx); 5231 ResolveImportedPath(Path, ModuleDir); 5232 Listener.ReadModuleMapFile(Path); 5233 break; 5234 } 5235 case INPUT_FILE_OFFSETS: { 5236 if (!NeedsInputFiles) 5237 break; 5238 5239 unsigned NumInputFiles = Record[0]; 5240 unsigned NumUserFiles = Record[1]; 5241 const llvm::support::unaligned_uint64_t *InputFileOffs = 5242 (const llvm::support::unaligned_uint64_t *)Blob.data(); 5243 for (unsigned I = 0; I != NumInputFiles; ++I) { 5244 // Go find this input file. 5245 bool isSystemFile = I >= NumUserFiles; 5246 5247 if (isSystemFile && !NeedsSystemInputFiles) 5248 break; // the rest are system input files 5249 5250 BitstreamCursor &Cursor = InputFilesCursor; 5251 SavedStreamPosition SavedPosition(Cursor); 5252 if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) { 5253 // FIXME this drops errors on the floor. 5254 consumeError(std::move(Err)); 5255 } 5256 5257 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 5258 if (!MaybeCode) { 5259 // FIXME this drops errors on the floor. 5260 consumeError(MaybeCode.takeError()); 5261 } 5262 unsigned Code = MaybeCode.get(); 5263 5264 RecordData Record; 5265 StringRef Blob; 5266 bool shouldContinue = false; 5267 Expected<unsigned> MaybeRecordType = 5268 Cursor.readRecord(Code, Record, &Blob); 5269 if (!MaybeRecordType) { 5270 // FIXME this drops errors on the floor. 5271 consumeError(MaybeRecordType.takeError()); 5272 } 5273 switch ((InputFileRecordTypes)MaybeRecordType.get()) { 5274 case INPUT_FILE_HASH: 5275 break; 5276 case INPUT_FILE: 5277 bool Overridden = static_cast<bool>(Record[3]); 5278 std::string Filename = std::string(Blob); 5279 ResolveImportedPath(Filename, ModuleDir); 5280 shouldContinue = Listener.visitInputFile( 5281 Filename, isSystemFile, Overridden, /*IsExplicitModule*/false); 5282 break; 5283 } 5284 if (!shouldContinue) 5285 break; 5286 } 5287 break; 5288 } 5289 5290 case IMPORTS: { 5291 if (!NeedsImports) 5292 break; 5293 5294 unsigned Idx = 0, N = Record.size(); 5295 while (Idx < N) { 5296 // Read information about the AST file. 5297 Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature 5298 std::string ModuleName = ReadString(Record, Idx); 5299 std::string Filename = ReadString(Record, Idx); 5300 ResolveImportedPath(Filename, ModuleDir); 5301 Listener.visitImport(ModuleName, Filename); 5302 } 5303 break; 5304 } 5305 5306 default: 5307 // No other validation to perform. 5308 break; 5309 } 5310 } 5311 5312 // Look for module file extension blocks, if requested. 5313 if (FindModuleFileExtensions) { 5314 BitstreamCursor SavedStream = Stream; 5315 while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) { 5316 bool DoneWithExtensionBlock = false; 5317 while (!DoneWithExtensionBlock) { 5318 Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); 5319 if (!MaybeEntry) { 5320 // FIXME this drops the error. 5321 return true; 5322 } 5323 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5324 5325 switch (Entry.Kind) { 5326 case llvm::BitstreamEntry::SubBlock: 5327 if (llvm::Error Err = Stream.SkipBlock()) { 5328 // FIXME this drops the error on the floor. 5329 consumeError(std::move(Err)); 5330 return true; 5331 } 5332 continue; 5333 5334 case llvm::BitstreamEntry::EndBlock: 5335 DoneWithExtensionBlock = true; 5336 continue; 5337 5338 case llvm::BitstreamEntry::Error: 5339 return true; 5340 5341 case llvm::BitstreamEntry::Record: 5342 break; 5343 } 5344 5345 Record.clear(); 5346 StringRef Blob; 5347 Expected<unsigned> MaybeRecCode = 5348 Stream.readRecord(Entry.ID, Record, &Blob); 5349 if (!MaybeRecCode) { 5350 // FIXME this drops the error. 5351 return true; 5352 } 5353 switch (MaybeRecCode.get()) { 5354 case EXTENSION_METADATA: { 5355 ModuleFileExtensionMetadata Metadata; 5356 if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) 5357 return true; 5358 5359 Listener.readModuleFileExtension(Metadata); 5360 break; 5361 } 5362 } 5363 } 5364 } 5365 Stream = SavedStream; 5366 } 5367 5368 // Scan for the UNHASHED_CONTROL_BLOCK_ID block. 5369 if (readUnhashedControlBlockImpl( 5370 nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate, 5371 /*AllowCompatibleConfigurationMismatch*/ false, &Listener, 5372 ValidateDiagnosticOptions) != Success) 5373 return true; 5374 5375 return false; 5376 } 5377 5378 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, 5379 const PCHContainerReader &PCHContainerRdr, 5380 const LangOptions &LangOpts, 5381 const TargetOptions &TargetOpts, 5382 const PreprocessorOptions &PPOpts, 5383 StringRef ExistingModuleCachePath) { 5384 SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, 5385 ExistingModuleCachePath, FileMgr); 5386 return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr, 5387 /*FindModuleFileExtensions=*/false, 5388 validator, 5389 /*ValidateDiagnosticOptions=*/true); 5390 } 5391 5392 ASTReader::ASTReadResult 5393 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) { 5394 // Enter the submodule block. 5395 if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) { 5396 Error(std::move(Err)); 5397 return Failure; 5398 } 5399 5400 ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap(); 5401 bool First = true; 5402 Module *CurrentModule = nullptr; 5403 RecordData Record; 5404 while (true) { 5405 Expected<llvm::BitstreamEntry> MaybeEntry = 5406 F.Stream.advanceSkippingSubblocks(); 5407 if (!MaybeEntry) { 5408 Error(MaybeEntry.takeError()); 5409 return Failure; 5410 } 5411 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5412 5413 switch (Entry.Kind) { 5414 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 5415 case llvm::BitstreamEntry::Error: 5416 Error("malformed block record in AST file"); 5417 return Failure; 5418 case llvm::BitstreamEntry::EndBlock: 5419 return Success; 5420 case llvm::BitstreamEntry::Record: 5421 // The interesting case. 5422 break; 5423 } 5424 5425 // Read a record. 5426 StringRef Blob; 5427 Record.clear(); 5428 Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob); 5429 if (!MaybeKind) { 5430 Error(MaybeKind.takeError()); 5431 return Failure; 5432 } 5433 unsigned Kind = MaybeKind.get(); 5434 5435 if ((Kind == SUBMODULE_METADATA) != First) { 5436 Error("submodule metadata record should be at beginning of block"); 5437 return Failure; 5438 } 5439 First = false; 5440 5441 // Submodule information is only valid if we have a current module. 5442 // FIXME: Should we error on these cases? 5443 if (!CurrentModule && Kind != SUBMODULE_METADATA && 5444 Kind != SUBMODULE_DEFINITION) 5445 continue; 5446 5447 switch (Kind) { 5448 default: // Default behavior: ignore. 5449 break; 5450 5451 case SUBMODULE_DEFINITION: { 5452 if (Record.size() < 12) { 5453 Error("malformed module definition"); 5454 return Failure; 5455 } 5456 5457 StringRef Name = Blob; 5458 unsigned Idx = 0; 5459 SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]); 5460 SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]); 5461 Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++]; 5462 bool IsFramework = Record[Idx++]; 5463 bool IsExplicit = Record[Idx++]; 5464 bool IsSystem = Record[Idx++]; 5465 bool IsExternC = Record[Idx++]; 5466 bool InferSubmodules = Record[Idx++]; 5467 bool InferExplicitSubmodules = Record[Idx++]; 5468 bool InferExportWildcard = Record[Idx++]; 5469 bool ConfigMacrosExhaustive = Record[Idx++]; 5470 bool ModuleMapIsPrivate = Record[Idx++]; 5471 5472 Module *ParentModule = nullptr; 5473 if (Parent) 5474 ParentModule = getSubmodule(Parent); 5475 5476 // Retrieve this (sub)module from the module map, creating it if 5477 // necessary. 5478 CurrentModule = 5479 ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit) 5480 .first; 5481 5482 // FIXME: set the definition loc for CurrentModule, or call 5483 // ModMap.setInferredModuleAllowedBy() 5484 5485 SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS; 5486 if (GlobalIndex >= SubmodulesLoaded.size() || 5487 SubmodulesLoaded[GlobalIndex]) { 5488 Error("too many submodules"); 5489 return Failure; 5490 } 5491 5492 if (!ParentModule) { 5493 if (const FileEntry *CurFile = CurrentModule->getASTFile()) { 5494 // Don't emit module relocation error if we have -fno-validate-pch 5495 if (!PP.getPreprocessorOpts().DisablePCHValidation && 5496 CurFile != F.File) { 5497 Error(diag::err_module_file_conflict, 5498 CurrentModule->getTopLevelModuleName(), CurFile->getName(), 5499 F.File->getName()); 5500 return Failure; 5501 } 5502 } 5503 5504 F.DidReadTopLevelSubmodule = true; 5505 CurrentModule->setASTFile(F.File); 5506 CurrentModule->PresumedModuleMapFile = F.ModuleMapPath; 5507 } 5508 5509 CurrentModule->Kind = Kind; 5510 CurrentModule->Signature = F.Signature; 5511 CurrentModule->IsFromModuleFile = true; 5512 CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem; 5513 CurrentModule->IsExternC = IsExternC; 5514 CurrentModule->InferSubmodules = InferSubmodules; 5515 CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules; 5516 CurrentModule->InferExportWildcard = InferExportWildcard; 5517 CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive; 5518 CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate; 5519 if (DeserializationListener) 5520 DeserializationListener->ModuleRead(GlobalID, CurrentModule); 5521 5522 SubmodulesLoaded[GlobalIndex] = CurrentModule; 5523 5524 // Clear out data that will be replaced by what is in the module file. 5525 CurrentModule->LinkLibraries.clear(); 5526 CurrentModule->ConfigMacros.clear(); 5527 CurrentModule->UnresolvedConflicts.clear(); 5528 CurrentModule->Conflicts.clear(); 5529 5530 // The module is available unless it's missing a requirement; relevant 5531 // requirements will be (re-)added by SUBMODULE_REQUIRES records. 5532 // Missing headers that were present when the module was built do not 5533 // make it unavailable -- if we got this far, this must be an explicitly 5534 // imported module file. 5535 CurrentModule->Requirements.clear(); 5536 CurrentModule->MissingHeaders.clear(); 5537 CurrentModule->IsMissingRequirement = 5538 ParentModule && ParentModule->IsMissingRequirement; 5539 CurrentModule->IsAvailable = !CurrentModule->IsMissingRequirement; 5540 break; 5541 } 5542 5543 case SUBMODULE_UMBRELLA_HEADER: { 5544 std::string Filename = std::string(Blob); 5545 ResolveImportedPath(F, Filename); 5546 if (auto Umbrella = PP.getFileManager().getFile(Filename)) { 5547 if (!CurrentModule->getUmbrellaHeader()) 5548 ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob); 5549 else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) { 5550 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5551 Error("mismatched umbrella headers in submodule"); 5552 return OutOfDate; 5553 } 5554 } 5555 break; 5556 } 5557 5558 case SUBMODULE_HEADER: 5559 case SUBMODULE_EXCLUDED_HEADER: 5560 case SUBMODULE_PRIVATE_HEADER: 5561 // We lazily associate headers with their modules via the HeaderInfo table. 5562 // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead 5563 // of complete filenames or remove it entirely. 5564 break; 5565 5566 case SUBMODULE_TEXTUAL_HEADER: 5567 case SUBMODULE_PRIVATE_TEXTUAL_HEADER: 5568 // FIXME: Textual headers are not marked in the HeaderInfo table. Load 5569 // them here. 5570 break; 5571 5572 case SUBMODULE_TOPHEADER: 5573 CurrentModule->addTopHeaderFilename(Blob); 5574 break; 5575 5576 case SUBMODULE_UMBRELLA_DIR: { 5577 std::string Dirname = std::string(Blob); 5578 ResolveImportedPath(F, Dirname); 5579 if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) { 5580 if (!CurrentModule->getUmbrellaDir()) 5581 ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob); 5582 else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) { 5583 if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) 5584 Error("mismatched umbrella directories in submodule"); 5585 return OutOfDate; 5586 } 5587 } 5588 break; 5589 } 5590 5591 case SUBMODULE_METADATA: { 5592 F.BaseSubmoduleID = getTotalNumSubmodules(); 5593 F.LocalNumSubmodules = Record[0]; 5594 unsigned LocalBaseSubmoduleID = Record[1]; 5595 if (F.LocalNumSubmodules > 0) { 5596 // Introduce the global -> local mapping for submodules within this 5597 // module. 5598 GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F)); 5599 5600 // Introduce the local -> global mapping for submodules within this 5601 // module. 5602 F.SubmoduleRemap.insertOrReplace( 5603 std::make_pair(LocalBaseSubmoduleID, 5604 F.BaseSubmoduleID - LocalBaseSubmoduleID)); 5605 5606 SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules); 5607 } 5608 break; 5609 } 5610 5611 case SUBMODULE_IMPORTS: 5612 for (unsigned Idx = 0; Idx != Record.size(); ++Idx) { 5613 UnresolvedModuleRef Unresolved; 5614 Unresolved.File = &F; 5615 Unresolved.Mod = CurrentModule; 5616 Unresolved.ID = Record[Idx]; 5617 Unresolved.Kind = UnresolvedModuleRef::Import; 5618 Unresolved.IsWildcard = false; 5619 UnresolvedModuleRefs.push_back(Unresolved); 5620 } 5621 break; 5622 5623 case SUBMODULE_EXPORTS: 5624 for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) { 5625 UnresolvedModuleRef Unresolved; 5626 Unresolved.File = &F; 5627 Unresolved.Mod = CurrentModule; 5628 Unresolved.ID = Record[Idx]; 5629 Unresolved.Kind = UnresolvedModuleRef::Export; 5630 Unresolved.IsWildcard = Record[Idx + 1]; 5631 UnresolvedModuleRefs.push_back(Unresolved); 5632 } 5633 5634 // Once we've loaded the set of exports, there's no reason to keep 5635 // the parsed, unresolved exports around. 5636 CurrentModule->UnresolvedExports.clear(); 5637 break; 5638 5639 case SUBMODULE_REQUIRES: 5640 CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(), 5641 PP.getTargetInfo()); 5642 break; 5643 5644 case SUBMODULE_LINK_LIBRARY: 5645 ModMap.resolveLinkAsDependencies(CurrentModule); 5646 CurrentModule->LinkLibraries.push_back( 5647 Module::LinkLibrary(std::string(Blob), Record[0])); 5648 break; 5649 5650 case SUBMODULE_CONFIG_MACRO: 5651 CurrentModule->ConfigMacros.push_back(Blob.str()); 5652 break; 5653 5654 case SUBMODULE_CONFLICT: { 5655 UnresolvedModuleRef Unresolved; 5656 Unresolved.File = &F; 5657 Unresolved.Mod = CurrentModule; 5658 Unresolved.ID = Record[0]; 5659 Unresolved.Kind = UnresolvedModuleRef::Conflict; 5660 Unresolved.IsWildcard = false; 5661 Unresolved.String = Blob; 5662 UnresolvedModuleRefs.push_back(Unresolved); 5663 break; 5664 } 5665 5666 case SUBMODULE_INITIALIZERS: { 5667 if (!ContextObj) 5668 break; 5669 SmallVector<uint32_t, 16> Inits; 5670 for (auto &ID : Record) 5671 Inits.push_back(getGlobalDeclID(F, ID)); 5672 ContextObj->addLazyModuleInitializers(CurrentModule, Inits); 5673 break; 5674 } 5675 5676 case SUBMODULE_EXPORT_AS: 5677 CurrentModule->ExportAsModule = Blob.str(); 5678 ModMap.addLinkAsDependency(CurrentModule); 5679 break; 5680 } 5681 } 5682 } 5683 5684 /// Parse the record that corresponds to a LangOptions data 5685 /// structure. 5686 /// 5687 /// This routine parses the language options from the AST file and then gives 5688 /// them to the AST listener if one is set. 5689 /// 5690 /// \returns true if the listener deems the file unacceptable, false otherwise. 5691 bool ASTReader::ParseLanguageOptions(const RecordData &Record, 5692 bool Complain, 5693 ASTReaderListener &Listener, 5694 bool AllowCompatibleDifferences) { 5695 LangOptions LangOpts; 5696 unsigned Idx = 0; 5697 #define LANGOPT(Name, Bits, Default, Description) \ 5698 LangOpts.Name = Record[Idx++]; 5699 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \ 5700 LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++])); 5701 #include "clang/Basic/LangOptions.def" 5702 #define SANITIZER(NAME, ID) \ 5703 LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]); 5704 #include "clang/Basic/Sanitizers.def" 5705 5706 for (unsigned N = Record[Idx++]; N; --N) 5707 LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx)); 5708 5709 ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++]; 5710 VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx); 5711 LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion); 5712 5713 LangOpts.CurrentModule = ReadString(Record, Idx); 5714 5715 // Comment options. 5716 for (unsigned N = Record[Idx++]; N; --N) { 5717 LangOpts.CommentOpts.BlockCommandNames.push_back( 5718 ReadString(Record, Idx)); 5719 } 5720 LangOpts.CommentOpts.ParseAllComments = Record[Idx++]; 5721 5722 // OpenMP offloading options. 5723 for (unsigned N = Record[Idx++]; N; --N) { 5724 LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx))); 5725 } 5726 5727 LangOpts.OMPHostIRFile = ReadString(Record, Idx); 5728 5729 return Listener.ReadLanguageOptions(LangOpts, Complain, 5730 AllowCompatibleDifferences); 5731 } 5732 5733 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain, 5734 ASTReaderListener &Listener, 5735 bool AllowCompatibleDifferences) { 5736 unsigned Idx = 0; 5737 TargetOptions TargetOpts; 5738 TargetOpts.Triple = ReadString(Record, Idx); 5739 TargetOpts.CPU = ReadString(Record, Idx); 5740 TargetOpts.ABI = ReadString(Record, Idx); 5741 for (unsigned N = Record[Idx++]; N; --N) { 5742 TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx)); 5743 } 5744 for (unsigned N = Record[Idx++]; N; --N) { 5745 TargetOpts.Features.push_back(ReadString(Record, Idx)); 5746 } 5747 5748 return Listener.ReadTargetOptions(TargetOpts, Complain, 5749 AllowCompatibleDifferences); 5750 } 5751 5752 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain, 5753 ASTReaderListener &Listener) { 5754 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions); 5755 unsigned Idx = 0; 5756 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++]; 5757 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \ 5758 DiagOpts->set##Name(static_cast<Type>(Record[Idx++])); 5759 #include "clang/Basic/DiagnosticOptions.def" 5760 5761 for (unsigned N = Record[Idx++]; N; --N) 5762 DiagOpts->Warnings.push_back(ReadString(Record, Idx)); 5763 for (unsigned N = Record[Idx++]; N; --N) 5764 DiagOpts->Remarks.push_back(ReadString(Record, Idx)); 5765 5766 return Listener.ReadDiagnosticOptions(DiagOpts, Complain); 5767 } 5768 5769 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain, 5770 ASTReaderListener &Listener) { 5771 FileSystemOptions FSOpts; 5772 unsigned Idx = 0; 5773 FSOpts.WorkingDir = ReadString(Record, Idx); 5774 return Listener.ReadFileSystemOptions(FSOpts, Complain); 5775 } 5776 5777 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record, 5778 bool Complain, 5779 ASTReaderListener &Listener) { 5780 HeaderSearchOptions HSOpts; 5781 unsigned Idx = 0; 5782 HSOpts.Sysroot = ReadString(Record, Idx); 5783 5784 // Include entries. 5785 for (unsigned N = Record[Idx++]; N; --N) { 5786 std::string Path = ReadString(Record, Idx); 5787 frontend::IncludeDirGroup Group 5788 = static_cast<frontend::IncludeDirGroup>(Record[Idx++]); 5789 bool IsFramework = Record[Idx++]; 5790 bool IgnoreSysRoot = Record[Idx++]; 5791 HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework, 5792 IgnoreSysRoot); 5793 } 5794 5795 // System header prefixes. 5796 for (unsigned N = Record[Idx++]; N; --N) { 5797 std::string Prefix = ReadString(Record, Idx); 5798 bool IsSystemHeader = Record[Idx++]; 5799 HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader); 5800 } 5801 5802 HSOpts.ResourceDir = ReadString(Record, Idx); 5803 HSOpts.ModuleCachePath = ReadString(Record, Idx); 5804 HSOpts.ModuleUserBuildPath = ReadString(Record, Idx); 5805 HSOpts.DisableModuleHash = Record[Idx++]; 5806 HSOpts.ImplicitModuleMaps = Record[Idx++]; 5807 HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++]; 5808 HSOpts.UseBuiltinIncludes = Record[Idx++]; 5809 HSOpts.UseStandardSystemIncludes = Record[Idx++]; 5810 HSOpts.UseStandardCXXIncludes = Record[Idx++]; 5811 HSOpts.UseLibcxx = Record[Idx++]; 5812 std::string SpecificModuleCachePath = ReadString(Record, Idx); 5813 5814 return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath, 5815 Complain); 5816 } 5817 5818 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record, 5819 bool Complain, 5820 ASTReaderListener &Listener, 5821 std::string &SuggestedPredefines) { 5822 PreprocessorOptions PPOpts; 5823 unsigned Idx = 0; 5824 5825 // Macro definitions/undefs 5826 for (unsigned N = Record[Idx++]; N; --N) { 5827 std::string Macro = ReadString(Record, Idx); 5828 bool IsUndef = Record[Idx++]; 5829 PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef)); 5830 } 5831 5832 // Includes 5833 for (unsigned N = Record[Idx++]; N; --N) { 5834 PPOpts.Includes.push_back(ReadString(Record, Idx)); 5835 } 5836 5837 // Macro Includes 5838 for (unsigned N = Record[Idx++]; N; --N) { 5839 PPOpts.MacroIncludes.push_back(ReadString(Record, Idx)); 5840 } 5841 5842 PPOpts.UsePredefines = Record[Idx++]; 5843 PPOpts.DetailedRecord = Record[Idx++]; 5844 PPOpts.ImplicitPCHInclude = ReadString(Record, Idx); 5845 PPOpts.ObjCXXARCStandardLibrary = 5846 static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]); 5847 SuggestedPredefines.clear(); 5848 return Listener.ReadPreprocessorOptions(PPOpts, Complain, 5849 SuggestedPredefines); 5850 } 5851 5852 std::pair<ModuleFile *, unsigned> 5853 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) { 5854 GlobalPreprocessedEntityMapType::iterator 5855 I = GlobalPreprocessedEntityMap.find(GlobalIndex); 5856 assert(I != GlobalPreprocessedEntityMap.end() && 5857 "Corrupted global preprocessed entity map"); 5858 ModuleFile *M = I->second; 5859 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID; 5860 return std::make_pair(M, LocalIndex); 5861 } 5862 5863 llvm::iterator_range<PreprocessingRecord::iterator> 5864 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const { 5865 if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord()) 5866 return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID, 5867 Mod.NumPreprocessedEntities); 5868 5869 return llvm::make_range(PreprocessingRecord::iterator(), 5870 PreprocessingRecord::iterator()); 5871 } 5872 5873 llvm::iterator_range<ASTReader::ModuleDeclIterator> 5874 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) { 5875 return llvm::make_range( 5876 ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls), 5877 ModuleDeclIterator(this, &Mod, 5878 Mod.FileSortedDecls + Mod.NumFileSortedDecls)); 5879 } 5880 5881 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) { 5882 auto I = GlobalSkippedRangeMap.find(GlobalIndex); 5883 assert(I != GlobalSkippedRangeMap.end() && 5884 "Corrupted global skipped range map"); 5885 ModuleFile *M = I->second; 5886 unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID; 5887 assert(LocalIndex < M->NumPreprocessedSkippedRanges); 5888 PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex]; 5889 SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()), 5890 TranslateSourceLocation(*M, RawRange.getEnd())); 5891 assert(Range.isValid()); 5892 return Range; 5893 } 5894 5895 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) { 5896 PreprocessedEntityID PPID = Index+1; 5897 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 5898 ModuleFile &M = *PPInfo.first; 5899 unsigned LocalIndex = PPInfo.second; 5900 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 5901 5902 if (!PP.getPreprocessingRecord()) { 5903 Error("no preprocessing record"); 5904 return nullptr; 5905 } 5906 5907 SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor); 5908 if (llvm::Error Err = 5909 M.PreprocessorDetailCursor.JumpToBit(PPOffs.BitOffset)) { 5910 Error(std::move(Err)); 5911 return nullptr; 5912 } 5913 5914 Expected<llvm::BitstreamEntry> MaybeEntry = 5915 M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd); 5916 if (!MaybeEntry) { 5917 Error(MaybeEntry.takeError()); 5918 return nullptr; 5919 } 5920 llvm::BitstreamEntry Entry = MaybeEntry.get(); 5921 5922 if (Entry.Kind != llvm::BitstreamEntry::Record) 5923 return nullptr; 5924 5925 // Read the record. 5926 SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()), 5927 TranslateSourceLocation(M, PPOffs.getEnd())); 5928 PreprocessingRecord &PPRec = *PP.getPreprocessingRecord(); 5929 StringRef Blob; 5930 RecordData Record; 5931 Expected<unsigned> MaybeRecType = 5932 M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob); 5933 if (!MaybeRecType) { 5934 Error(MaybeRecType.takeError()); 5935 return nullptr; 5936 } 5937 switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) { 5938 case PPD_MACRO_EXPANSION: { 5939 bool isBuiltin = Record[0]; 5940 IdentifierInfo *Name = nullptr; 5941 MacroDefinitionRecord *Def = nullptr; 5942 if (isBuiltin) 5943 Name = getLocalIdentifier(M, Record[1]); 5944 else { 5945 PreprocessedEntityID GlobalID = 5946 getGlobalPreprocessedEntityID(M, Record[1]); 5947 Def = cast<MacroDefinitionRecord>( 5948 PPRec.getLoadedPreprocessedEntity(GlobalID - 1)); 5949 } 5950 5951 MacroExpansion *ME; 5952 if (isBuiltin) 5953 ME = new (PPRec) MacroExpansion(Name, Range); 5954 else 5955 ME = new (PPRec) MacroExpansion(Def, Range); 5956 5957 return ME; 5958 } 5959 5960 case PPD_MACRO_DEFINITION: { 5961 // Decode the identifier info and then check again; if the macro is 5962 // still defined and associated with the identifier, 5963 IdentifierInfo *II = getLocalIdentifier(M, Record[0]); 5964 MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range); 5965 5966 if (DeserializationListener) 5967 DeserializationListener->MacroDefinitionRead(PPID, MD); 5968 5969 return MD; 5970 } 5971 5972 case PPD_INCLUSION_DIRECTIVE: { 5973 const char *FullFileNameStart = Blob.data() + Record[0]; 5974 StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]); 5975 const FileEntry *File = nullptr; 5976 if (!FullFileName.empty()) 5977 if (auto FE = PP.getFileManager().getFile(FullFileName)) 5978 File = *FE; 5979 5980 // FIXME: Stable encoding 5981 InclusionDirective::InclusionKind Kind 5982 = static_cast<InclusionDirective::InclusionKind>(Record[2]); 5983 InclusionDirective *ID 5984 = new (PPRec) InclusionDirective(PPRec, Kind, 5985 StringRef(Blob.data(), Record[0]), 5986 Record[1], Record[3], 5987 File, 5988 Range); 5989 return ID; 5990 } 5991 } 5992 5993 llvm_unreachable("Invalid PreprocessorDetailRecordTypes"); 5994 } 5995 5996 /// Find the next module that contains entities and return the ID 5997 /// of the first entry. 5998 /// 5999 /// \param SLocMapI points at a chunk of a module that contains no 6000 /// preprocessed entities or the entities it contains are not the ones we are 6001 /// looking for. 6002 PreprocessedEntityID ASTReader::findNextPreprocessedEntity( 6003 GlobalSLocOffsetMapType::const_iterator SLocMapI) const { 6004 ++SLocMapI; 6005 for (GlobalSLocOffsetMapType::const_iterator 6006 EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) { 6007 ModuleFile &M = *SLocMapI->second; 6008 if (M.NumPreprocessedEntities) 6009 return M.BasePreprocessedEntityID; 6010 } 6011 6012 return getTotalNumPreprocessedEntities(); 6013 } 6014 6015 namespace { 6016 6017 struct PPEntityComp { 6018 const ASTReader &Reader; 6019 ModuleFile &M; 6020 6021 PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {} 6022 6023 bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const { 6024 SourceLocation LHS = getLoc(L); 6025 SourceLocation RHS = getLoc(R); 6026 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6027 } 6028 6029 bool operator()(const PPEntityOffset &L, SourceLocation RHS) const { 6030 SourceLocation LHS = getLoc(L); 6031 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6032 } 6033 6034 bool operator()(SourceLocation LHS, const PPEntityOffset &R) const { 6035 SourceLocation RHS = getLoc(R); 6036 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 6037 } 6038 6039 SourceLocation getLoc(const PPEntityOffset &PPE) const { 6040 return Reader.TranslateSourceLocation(M, PPE.getBegin()); 6041 } 6042 }; 6043 6044 } // namespace 6045 6046 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc, 6047 bool EndsAfter) const { 6048 if (SourceMgr.isLocalSourceLocation(Loc)) 6049 return getTotalNumPreprocessedEntities(); 6050 6051 GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find( 6052 SourceManager::MaxLoadedOffset - Loc.getOffset() - 1); 6053 assert(SLocMapI != GlobalSLocOffsetMap.end() && 6054 "Corrupted global sloc offset map"); 6055 6056 if (SLocMapI->second->NumPreprocessedEntities == 0) 6057 return findNextPreprocessedEntity(SLocMapI); 6058 6059 ModuleFile &M = *SLocMapI->second; 6060 6061 using pp_iterator = const PPEntityOffset *; 6062 6063 pp_iterator pp_begin = M.PreprocessedEntityOffsets; 6064 pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities; 6065 6066 size_t Count = M.NumPreprocessedEntities; 6067 size_t Half; 6068 pp_iterator First = pp_begin; 6069 pp_iterator PPI; 6070 6071 if (EndsAfter) { 6072 PPI = std::upper_bound(pp_begin, pp_end, Loc, 6073 PPEntityComp(*this, M)); 6074 } else { 6075 // Do a binary search manually instead of using std::lower_bound because 6076 // The end locations of entities may be unordered (when a macro expansion 6077 // is inside another macro argument), but for this case it is not important 6078 // whether we get the first macro expansion or its containing macro. 6079 while (Count > 0) { 6080 Half = Count / 2; 6081 PPI = First; 6082 std::advance(PPI, Half); 6083 if (SourceMgr.isBeforeInTranslationUnit( 6084 TranslateSourceLocation(M, PPI->getEnd()), Loc)) { 6085 First = PPI; 6086 ++First; 6087 Count = Count - Half - 1; 6088 } else 6089 Count = Half; 6090 } 6091 } 6092 6093 if (PPI == pp_end) 6094 return findNextPreprocessedEntity(SLocMapI); 6095 6096 return M.BasePreprocessedEntityID + (PPI - pp_begin); 6097 } 6098 6099 /// Returns a pair of [Begin, End) indices of preallocated 6100 /// preprocessed entities that \arg Range encompasses. 6101 std::pair<unsigned, unsigned> 6102 ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) { 6103 if (Range.isInvalid()) 6104 return std::make_pair(0,0); 6105 assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin())); 6106 6107 PreprocessedEntityID BeginID = 6108 findPreprocessedEntity(Range.getBegin(), false); 6109 PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true); 6110 return std::make_pair(BeginID, EndID); 6111 } 6112 6113 /// Optionally returns true or false if the preallocated preprocessed 6114 /// entity with index \arg Index came from file \arg FID. 6115 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, 6116 FileID FID) { 6117 if (FID.isInvalid()) 6118 return false; 6119 6120 std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index); 6121 ModuleFile &M = *PPInfo.first; 6122 unsigned LocalIndex = PPInfo.second; 6123 const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex]; 6124 6125 SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin()); 6126 if (Loc.isInvalid()) 6127 return false; 6128 6129 if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID)) 6130 return true; 6131 else 6132 return false; 6133 } 6134 6135 namespace { 6136 6137 /// Visitor used to search for information about a header file. 6138 class HeaderFileInfoVisitor { 6139 const FileEntry *FE; 6140 Optional<HeaderFileInfo> HFI; 6141 6142 public: 6143 explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {} 6144 6145 bool operator()(ModuleFile &M) { 6146 HeaderFileInfoLookupTable *Table 6147 = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); 6148 if (!Table) 6149 return false; 6150 6151 // Look in the on-disk hash table for an entry for this file name. 6152 HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); 6153 if (Pos == Table->end()) 6154 return false; 6155 6156 HFI = *Pos; 6157 return true; 6158 } 6159 6160 Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } 6161 }; 6162 6163 } // namespace 6164 6165 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { 6166 HeaderFileInfoVisitor Visitor(FE); 6167 ModuleMgr.visit(Visitor); 6168 if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) 6169 return *HFI; 6170 6171 return HeaderFileInfo(); 6172 } 6173 6174 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { 6175 using DiagState = DiagnosticsEngine::DiagState; 6176 SmallVector<DiagState *, 32> DiagStates; 6177 6178 for (ModuleFile &F : ModuleMgr) { 6179 unsigned Idx = 0; 6180 auto &Record = F.PragmaDiagMappings; 6181 if (Record.empty()) 6182 continue; 6183 6184 DiagStates.clear(); 6185 6186 auto ReadDiagState = 6187 [&](const DiagState &BasedOn, SourceLocation Loc, 6188 bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * { 6189 unsigned BackrefID = Record[Idx++]; 6190 if (BackrefID != 0) 6191 return DiagStates[BackrefID - 1]; 6192 6193 // A new DiagState was created here. 6194 Diag.DiagStates.push_back(BasedOn); 6195 DiagState *NewState = &Diag.DiagStates.back(); 6196 DiagStates.push_back(NewState); 6197 unsigned Size = Record[Idx++]; 6198 assert(Idx + Size * 2 <= Record.size() && 6199 "Invalid data, not enough diag/map pairs"); 6200 while (Size--) { 6201 unsigned DiagID = Record[Idx++]; 6202 DiagnosticMapping NewMapping = 6203 DiagnosticMapping::deserialize(Record[Idx++]); 6204 if (!NewMapping.isPragma() && !IncludeNonPragmaStates) 6205 continue; 6206 6207 DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID); 6208 6209 // If this mapping was specified as a warning but the severity was 6210 // upgraded due to diagnostic settings, simulate the current diagnostic 6211 // settings (and use a warning). 6212 if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) { 6213 NewMapping.setSeverity(diag::Severity::Warning); 6214 NewMapping.setUpgradedFromWarning(false); 6215 } 6216 6217 Mapping = NewMapping; 6218 } 6219 return NewState; 6220 }; 6221 6222 // Read the first state. 6223 DiagState *FirstState; 6224 if (F.Kind == MK_ImplicitModule) { 6225 // Implicitly-built modules are reused with different diagnostic 6226 // settings. Use the initial diagnostic state from Diag to simulate this 6227 // compilation's diagnostic settings. 6228 FirstState = Diag.DiagStatesByLoc.FirstDiagState; 6229 DiagStates.push_back(FirstState); 6230 6231 // Skip the initial diagnostic state from the serialized module. 6232 assert(Record[1] == 0 && 6233 "Invalid data, unexpected backref in initial state"); 6234 Idx = 3 + Record[2] * 2; 6235 assert(Idx < Record.size() && 6236 "Invalid data, not enough state change pairs in initial state"); 6237 } else if (F.isModule()) { 6238 // For an explicit module, preserve the flags from the module build 6239 // command line (-w, -Weverything, -Werror, ...) along with any explicit 6240 // -Wblah flags. 6241 unsigned Flags = Record[Idx++]; 6242 DiagState Initial; 6243 Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1; 6244 Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1; 6245 Initial.WarningsAsErrors = Flags & 1; Flags >>= 1; 6246 Initial.EnableAllWarnings = Flags & 1; Flags >>= 1; 6247 Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1; 6248 Initial.ExtBehavior = (diag::Severity)Flags; 6249 FirstState = ReadDiagState(Initial, SourceLocation(), true); 6250 6251 assert(F.OriginalSourceFileID.isValid()); 6252 6253 // Set up the root buffer of the module to start with the initial 6254 // diagnostic state of the module itself, to cover files that contain no 6255 // explicit transitions (for which we did not serialize anything). 6256 Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID] 6257 .StateTransitions.push_back({FirstState, 0}); 6258 } else { 6259 // For prefix ASTs, start with whatever the user configured on the 6260 // command line. 6261 Idx++; // Skip flags. 6262 FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, 6263 SourceLocation(), false); 6264 } 6265 6266 // Read the state transitions. 6267 unsigned NumLocations = Record[Idx++]; 6268 while (NumLocations--) { 6269 assert(Idx < Record.size() && 6270 "Invalid data, missing pragma diagnostic states"); 6271 SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]); 6272 auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc); 6273 assert(IDAndOffset.first.isValid() && "invalid FileID for transition"); 6274 assert(IDAndOffset.second == 0 && "not a start location for a FileID"); 6275 unsigned Transitions = Record[Idx++]; 6276 6277 // Note that we don't need to set up Parent/ParentOffset here, because 6278 // we won't be changing the diagnostic state within imported FileIDs 6279 // (other than perhaps appending to the main source file, which has no 6280 // parent). 6281 auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first]; 6282 F.StateTransitions.reserve(F.StateTransitions.size() + Transitions); 6283 for (unsigned I = 0; I != Transitions; ++I) { 6284 unsigned Offset = Record[Idx++]; 6285 auto *State = 6286 ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false); 6287 F.StateTransitions.push_back({State, Offset}); 6288 } 6289 } 6290 6291 // Read the final state. 6292 assert(Idx < Record.size() && 6293 "Invalid data, missing final pragma diagnostic state"); 6294 SourceLocation CurStateLoc = 6295 ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]); 6296 auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false); 6297 6298 if (!F.isModule()) { 6299 Diag.DiagStatesByLoc.CurDiagState = CurState; 6300 Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc; 6301 6302 // Preserve the property that the imaginary root file describes the 6303 // current state. 6304 FileID NullFile; 6305 auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions; 6306 if (T.empty()) 6307 T.push_back({CurState, 0}); 6308 else 6309 T[0].State = CurState; 6310 } 6311 6312 // Don't try to read these mappings again. 6313 Record.clear(); 6314 } 6315 } 6316 6317 /// Get the correct cursor and offset for loading a type. 6318 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) { 6319 GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index); 6320 assert(I != GlobalTypeMap.end() && "Corrupted global type map"); 6321 ModuleFile *M = I->second; 6322 return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex]); 6323 } 6324 6325 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) { 6326 switch (code) { 6327 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \ 6328 case TYPE_##CODE_ID: return Type::CLASS_ID; 6329 #include "clang/Serialization/TypeBitCodes.def" 6330 default: return llvm::None; 6331 } 6332 } 6333 6334 /// Read and return the type with the given index.. 6335 /// 6336 /// The index is the type ID, shifted and minus the number of predefs. This 6337 /// routine actually reads the record corresponding to the type at the given 6338 /// location. It is a helper routine for GetType, which deals with reading type 6339 /// IDs. 6340 QualType ASTReader::readTypeRecord(unsigned Index) { 6341 assert(ContextObj && "reading type with no AST context"); 6342 ASTContext &Context = *ContextObj; 6343 RecordLocation Loc = TypeCursorForIndex(Index); 6344 BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; 6345 6346 // Keep track of where we are in the stream, then jump back there 6347 // after reading this type. 6348 SavedStreamPosition SavedPosition(DeclsCursor); 6349 6350 ReadingKindTracker ReadingKind(Read_Type, *this); 6351 6352 // Note that we are loading a type record. 6353 Deserializing AType(this); 6354 6355 if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) { 6356 Error(std::move(Err)); 6357 return QualType(); 6358 } 6359 Expected<unsigned> RawCode = DeclsCursor.ReadCode(); 6360 if (!RawCode) { 6361 Error(RawCode.takeError()); 6362 return QualType(); 6363 } 6364 6365 ASTRecordReader Record(*this, *Loc.F); 6366 Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get()); 6367 if (!Code) { 6368 Error(Code.takeError()); 6369 return QualType(); 6370 } 6371 if (Code.get() == TYPE_EXT_QUAL) { 6372 QualType baseType = Record.readQualType(); 6373 Qualifiers quals = Record.readQualifiers(); 6374 return Context.getQualifiedType(baseType, quals); 6375 } 6376 6377 auto maybeClass = getTypeClassForCode((TypeCode) Code.get()); 6378 if (!maybeClass) { 6379 Error("Unexpected code for type"); 6380 return QualType(); 6381 } 6382 6383 serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record); 6384 return TypeReader.read(*maybeClass); 6385 } 6386 6387 namespace clang { 6388 6389 class TypeLocReader : public TypeLocVisitor<TypeLocReader> { 6390 ASTRecordReader &Reader; 6391 6392 SourceLocation readSourceLocation() { 6393 return Reader.readSourceLocation(); 6394 } 6395 6396 TypeSourceInfo *GetTypeSourceInfo() { 6397 return Reader.readTypeSourceInfo(); 6398 } 6399 6400 NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() { 6401 return Reader.readNestedNameSpecifierLoc(); 6402 } 6403 6404 Attr *ReadAttr() { 6405 return Reader.readAttr(); 6406 } 6407 6408 public: 6409 TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {} 6410 6411 // We want compile-time assurance that we've enumerated all of 6412 // these, so unfortunately we have to declare them first, then 6413 // define them out-of-line. 6414 #define ABSTRACT_TYPELOC(CLASS, PARENT) 6415 #define TYPELOC(CLASS, PARENT) \ 6416 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc); 6417 #include "clang/AST/TypeLocNodes.def" 6418 6419 void VisitFunctionTypeLoc(FunctionTypeLoc); 6420 void VisitArrayTypeLoc(ArrayTypeLoc); 6421 }; 6422 6423 } // namespace clang 6424 6425 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { 6426 // nothing to do 6427 } 6428 6429 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { 6430 TL.setBuiltinLoc(readSourceLocation()); 6431 if (TL.needsExtraLocalData()) { 6432 TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt())); 6433 TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt())); 6434 TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt())); 6435 TL.setModeAttr(Reader.readInt()); 6436 } 6437 } 6438 6439 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) { 6440 TL.setNameLoc(readSourceLocation()); 6441 } 6442 6443 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) { 6444 TL.setStarLoc(readSourceLocation()); 6445 } 6446 6447 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) { 6448 // nothing to do 6449 } 6450 6451 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) { 6452 // nothing to do 6453 } 6454 6455 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) { 6456 TL.setExpansionLoc(readSourceLocation()); 6457 } 6458 6459 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { 6460 TL.setCaretLoc(readSourceLocation()); 6461 } 6462 6463 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { 6464 TL.setAmpLoc(readSourceLocation()); 6465 } 6466 6467 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { 6468 TL.setAmpAmpLoc(readSourceLocation()); 6469 } 6470 6471 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { 6472 TL.setStarLoc(readSourceLocation()); 6473 TL.setClassTInfo(GetTypeSourceInfo()); 6474 } 6475 6476 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { 6477 TL.setLBracketLoc(readSourceLocation()); 6478 TL.setRBracketLoc(readSourceLocation()); 6479 if (Reader.readBool()) 6480 TL.setSizeExpr(Reader.readExpr()); 6481 else 6482 TL.setSizeExpr(nullptr); 6483 } 6484 6485 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) { 6486 VisitArrayTypeLoc(TL); 6487 } 6488 6489 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) { 6490 VisitArrayTypeLoc(TL); 6491 } 6492 6493 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) { 6494 VisitArrayTypeLoc(TL); 6495 } 6496 6497 void TypeLocReader::VisitDependentSizedArrayTypeLoc( 6498 DependentSizedArrayTypeLoc TL) { 6499 VisitArrayTypeLoc(TL); 6500 } 6501 6502 void TypeLocReader::VisitDependentAddressSpaceTypeLoc( 6503 DependentAddressSpaceTypeLoc TL) { 6504 6505 TL.setAttrNameLoc(readSourceLocation()); 6506 TL.setAttrOperandParensRange(Reader.readSourceRange()); 6507 TL.setAttrExprOperand(Reader.readExpr()); 6508 } 6509 6510 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc( 6511 DependentSizedExtVectorTypeLoc TL) { 6512 TL.setNameLoc(readSourceLocation()); 6513 } 6514 6515 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) { 6516 TL.setNameLoc(readSourceLocation()); 6517 } 6518 6519 void TypeLocReader::VisitDependentVectorTypeLoc( 6520 DependentVectorTypeLoc TL) { 6521 TL.setNameLoc(readSourceLocation()); 6522 } 6523 6524 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) { 6525 TL.setNameLoc(readSourceLocation()); 6526 } 6527 6528 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) { 6529 TL.setLocalRangeBegin(readSourceLocation()); 6530 TL.setLParenLoc(readSourceLocation()); 6531 TL.setRParenLoc(readSourceLocation()); 6532 TL.setExceptionSpecRange(Reader.readSourceRange()); 6533 TL.setLocalRangeEnd(readSourceLocation()); 6534 for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) { 6535 TL.setParam(i, Reader.readDeclAs<ParmVarDecl>()); 6536 } 6537 } 6538 6539 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) { 6540 VisitFunctionTypeLoc(TL); 6541 } 6542 6543 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) { 6544 VisitFunctionTypeLoc(TL); 6545 } 6546 6547 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { 6548 TL.setNameLoc(readSourceLocation()); 6549 } 6550 6551 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) { 6552 TL.setNameLoc(readSourceLocation()); 6553 } 6554 6555 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { 6556 TL.setTypeofLoc(readSourceLocation()); 6557 TL.setLParenLoc(readSourceLocation()); 6558 TL.setRParenLoc(readSourceLocation()); 6559 } 6560 6561 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { 6562 TL.setTypeofLoc(readSourceLocation()); 6563 TL.setLParenLoc(readSourceLocation()); 6564 TL.setRParenLoc(readSourceLocation()); 6565 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6566 } 6567 6568 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) { 6569 TL.setNameLoc(readSourceLocation()); 6570 } 6571 6572 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { 6573 TL.setKWLoc(readSourceLocation()); 6574 TL.setLParenLoc(readSourceLocation()); 6575 TL.setRParenLoc(readSourceLocation()); 6576 TL.setUnderlyingTInfo(GetTypeSourceInfo()); 6577 } 6578 6579 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) { 6580 TL.setNameLoc(readSourceLocation()); 6581 if (Reader.readBool()) { 6582 TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc()); 6583 TL.setTemplateKWLoc(readSourceLocation()); 6584 TL.setConceptNameLoc(readSourceLocation()); 6585 TL.setFoundDecl(Reader.readDeclAs<NamedDecl>()); 6586 TL.setLAngleLoc(readSourceLocation()); 6587 TL.setRAngleLoc(readSourceLocation()); 6588 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6589 TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo( 6590 TL.getTypePtr()->getArg(i).getKind())); 6591 } 6592 } 6593 6594 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc( 6595 DeducedTemplateSpecializationTypeLoc TL) { 6596 TL.setTemplateNameLoc(readSourceLocation()); 6597 } 6598 6599 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) { 6600 TL.setNameLoc(readSourceLocation()); 6601 } 6602 6603 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { 6604 TL.setNameLoc(readSourceLocation()); 6605 } 6606 6607 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) { 6608 TL.setAttr(ReadAttr()); 6609 } 6610 6611 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { 6612 TL.setNameLoc(readSourceLocation()); 6613 } 6614 6615 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc( 6616 SubstTemplateTypeParmTypeLoc TL) { 6617 TL.setNameLoc(readSourceLocation()); 6618 } 6619 6620 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc( 6621 SubstTemplateTypeParmPackTypeLoc TL) { 6622 TL.setNameLoc(readSourceLocation()); 6623 } 6624 6625 void TypeLocReader::VisitTemplateSpecializationTypeLoc( 6626 TemplateSpecializationTypeLoc TL) { 6627 TL.setTemplateKeywordLoc(readSourceLocation()); 6628 TL.setTemplateNameLoc(readSourceLocation()); 6629 TL.setLAngleLoc(readSourceLocation()); 6630 TL.setRAngleLoc(readSourceLocation()); 6631 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) 6632 TL.setArgLocInfo( 6633 i, 6634 Reader.readTemplateArgumentLocInfo( 6635 TL.getTypePtr()->getArg(i).getKind())); 6636 } 6637 6638 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) { 6639 TL.setLParenLoc(readSourceLocation()); 6640 TL.setRParenLoc(readSourceLocation()); 6641 } 6642 6643 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { 6644 TL.setElaboratedKeywordLoc(readSourceLocation()); 6645 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6646 } 6647 6648 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) { 6649 TL.setNameLoc(readSourceLocation()); 6650 } 6651 6652 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { 6653 TL.setElaboratedKeywordLoc(readSourceLocation()); 6654 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6655 TL.setNameLoc(readSourceLocation()); 6656 } 6657 6658 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( 6659 DependentTemplateSpecializationTypeLoc TL) { 6660 TL.setElaboratedKeywordLoc(readSourceLocation()); 6661 TL.setQualifierLoc(ReadNestedNameSpecifierLoc()); 6662 TL.setTemplateKeywordLoc(readSourceLocation()); 6663 TL.setTemplateNameLoc(readSourceLocation()); 6664 TL.setLAngleLoc(readSourceLocation()); 6665 TL.setRAngleLoc(readSourceLocation()); 6666 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) 6667 TL.setArgLocInfo( 6668 I, 6669 Reader.readTemplateArgumentLocInfo( 6670 TL.getTypePtr()->getArg(I).getKind())); 6671 } 6672 6673 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) { 6674 TL.setEllipsisLoc(readSourceLocation()); 6675 } 6676 6677 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { 6678 TL.setNameLoc(readSourceLocation()); 6679 } 6680 6681 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) { 6682 if (TL.getNumProtocols()) { 6683 TL.setProtocolLAngleLoc(readSourceLocation()); 6684 TL.setProtocolRAngleLoc(readSourceLocation()); 6685 } 6686 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6687 TL.setProtocolLoc(i, readSourceLocation()); 6688 } 6689 6690 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { 6691 TL.setHasBaseTypeAsWritten(Reader.readBool()); 6692 TL.setTypeArgsLAngleLoc(readSourceLocation()); 6693 TL.setTypeArgsRAngleLoc(readSourceLocation()); 6694 for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i) 6695 TL.setTypeArgTInfo(i, GetTypeSourceInfo()); 6696 TL.setProtocolLAngleLoc(readSourceLocation()); 6697 TL.setProtocolRAngleLoc(readSourceLocation()); 6698 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i) 6699 TL.setProtocolLoc(i, readSourceLocation()); 6700 } 6701 6702 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { 6703 TL.setStarLoc(readSourceLocation()); 6704 } 6705 6706 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) { 6707 TL.setKWLoc(readSourceLocation()); 6708 TL.setLParenLoc(readSourceLocation()); 6709 TL.setRParenLoc(readSourceLocation()); 6710 } 6711 6712 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) { 6713 TL.setKWLoc(readSourceLocation()); 6714 } 6715 6716 void ASTRecordReader::readTypeLoc(TypeLoc TL) { 6717 TypeLocReader TLR(*this); 6718 for (; !TL.isNull(); TL = TL.getNextTypeLoc()) 6719 TLR.Visit(TL); 6720 } 6721 6722 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() { 6723 QualType InfoTy = readType(); 6724 if (InfoTy.isNull()) 6725 return nullptr; 6726 6727 TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy); 6728 readTypeLoc(TInfo->getTypeLoc()); 6729 return TInfo; 6730 } 6731 6732 QualType ASTReader::GetType(TypeID ID) { 6733 assert(ContextObj && "reading type with no AST context"); 6734 ASTContext &Context = *ContextObj; 6735 6736 unsigned FastQuals = ID & Qualifiers::FastMask; 6737 unsigned Index = ID >> Qualifiers::FastWidth; 6738 6739 if (Index < NUM_PREDEF_TYPE_IDS) { 6740 QualType T; 6741 switch ((PredefinedTypeIDs)Index) { 6742 case PREDEF_TYPE_NULL_ID: 6743 return QualType(); 6744 case PREDEF_TYPE_VOID_ID: 6745 T = Context.VoidTy; 6746 break; 6747 case PREDEF_TYPE_BOOL_ID: 6748 T = Context.BoolTy; 6749 break; 6750 case PREDEF_TYPE_CHAR_U_ID: 6751 case PREDEF_TYPE_CHAR_S_ID: 6752 // FIXME: Check that the signedness of CharTy is correct! 6753 T = Context.CharTy; 6754 break; 6755 case PREDEF_TYPE_UCHAR_ID: 6756 T = Context.UnsignedCharTy; 6757 break; 6758 case PREDEF_TYPE_USHORT_ID: 6759 T = Context.UnsignedShortTy; 6760 break; 6761 case PREDEF_TYPE_UINT_ID: 6762 T = Context.UnsignedIntTy; 6763 break; 6764 case PREDEF_TYPE_ULONG_ID: 6765 T = Context.UnsignedLongTy; 6766 break; 6767 case PREDEF_TYPE_ULONGLONG_ID: 6768 T = Context.UnsignedLongLongTy; 6769 break; 6770 case PREDEF_TYPE_UINT128_ID: 6771 T = Context.UnsignedInt128Ty; 6772 break; 6773 case PREDEF_TYPE_SCHAR_ID: 6774 T = Context.SignedCharTy; 6775 break; 6776 case PREDEF_TYPE_WCHAR_ID: 6777 T = Context.WCharTy; 6778 break; 6779 case PREDEF_TYPE_SHORT_ID: 6780 T = Context.ShortTy; 6781 break; 6782 case PREDEF_TYPE_INT_ID: 6783 T = Context.IntTy; 6784 break; 6785 case PREDEF_TYPE_LONG_ID: 6786 T = Context.LongTy; 6787 break; 6788 case PREDEF_TYPE_LONGLONG_ID: 6789 T = Context.LongLongTy; 6790 break; 6791 case PREDEF_TYPE_INT128_ID: 6792 T = Context.Int128Ty; 6793 break; 6794 case PREDEF_TYPE_HALF_ID: 6795 T = Context.HalfTy; 6796 break; 6797 case PREDEF_TYPE_FLOAT_ID: 6798 T = Context.FloatTy; 6799 break; 6800 case PREDEF_TYPE_DOUBLE_ID: 6801 T = Context.DoubleTy; 6802 break; 6803 case PREDEF_TYPE_LONGDOUBLE_ID: 6804 T = Context.LongDoubleTy; 6805 break; 6806 case PREDEF_TYPE_SHORT_ACCUM_ID: 6807 T = Context.ShortAccumTy; 6808 break; 6809 case PREDEF_TYPE_ACCUM_ID: 6810 T = Context.AccumTy; 6811 break; 6812 case PREDEF_TYPE_LONG_ACCUM_ID: 6813 T = Context.LongAccumTy; 6814 break; 6815 case PREDEF_TYPE_USHORT_ACCUM_ID: 6816 T = Context.UnsignedShortAccumTy; 6817 break; 6818 case PREDEF_TYPE_UACCUM_ID: 6819 T = Context.UnsignedAccumTy; 6820 break; 6821 case PREDEF_TYPE_ULONG_ACCUM_ID: 6822 T = Context.UnsignedLongAccumTy; 6823 break; 6824 case PREDEF_TYPE_SHORT_FRACT_ID: 6825 T = Context.ShortFractTy; 6826 break; 6827 case PREDEF_TYPE_FRACT_ID: 6828 T = Context.FractTy; 6829 break; 6830 case PREDEF_TYPE_LONG_FRACT_ID: 6831 T = Context.LongFractTy; 6832 break; 6833 case PREDEF_TYPE_USHORT_FRACT_ID: 6834 T = Context.UnsignedShortFractTy; 6835 break; 6836 case PREDEF_TYPE_UFRACT_ID: 6837 T = Context.UnsignedFractTy; 6838 break; 6839 case PREDEF_TYPE_ULONG_FRACT_ID: 6840 T = Context.UnsignedLongFractTy; 6841 break; 6842 case PREDEF_TYPE_SAT_SHORT_ACCUM_ID: 6843 T = Context.SatShortAccumTy; 6844 break; 6845 case PREDEF_TYPE_SAT_ACCUM_ID: 6846 T = Context.SatAccumTy; 6847 break; 6848 case PREDEF_TYPE_SAT_LONG_ACCUM_ID: 6849 T = Context.SatLongAccumTy; 6850 break; 6851 case PREDEF_TYPE_SAT_USHORT_ACCUM_ID: 6852 T = Context.SatUnsignedShortAccumTy; 6853 break; 6854 case PREDEF_TYPE_SAT_UACCUM_ID: 6855 T = Context.SatUnsignedAccumTy; 6856 break; 6857 case PREDEF_TYPE_SAT_ULONG_ACCUM_ID: 6858 T = Context.SatUnsignedLongAccumTy; 6859 break; 6860 case PREDEF_TYPE_SAT_SHORT_FRACT_ID: 6861 T = Context.SatShortFractTy; 6862 break; 6863 case PREDEF_TYPE_SAT_FRACT_ID: 6864 T = Context.SatFractTy; 6865 break; 6866 case PREDEF_TYPE_SAT_LONG_FRACT_ID: 6867 T = Context.SatLongFractTy; 6868 break; 6869 case PREDEF_TYPE_SAT_USHORT_FRACT_ID: 6870 T = Context.SatUnsignedShortFractTy; 6871 break; 6872 case PREDEF_TYPE_SAT_UFRACT_ID: 6873 T = Context.SatUnsignedFractTy; 6874 break; 6875 case PREDEF_TYPE_SAT_ULONG_FRACT_ID: 6876 T = Context.SatUnsignedLongFractTy; 6877 break; 6878 case PREDEF_TYPE_FLOAT16_ID: 6879 T = Context.Float16Ty; 6880 break; 6881 case PREDEF_TYPE_FLOAT128_ID: 6882 T = Context.Float128Ty; 6883 break; 6884 case PREDEF_TYPE_OVERLOAD_ID: 6885 T = Context.OverloadTy; 6886 break; 6887 case PREDEF_TYPE_BOUND_MEMBER: 6888 T = Context.BoundMemberTy; 6889 break; 6890 case PREDEF_TYPE_PSEUDO_OBJECT: 6891 T = Context.PseudoObjectTy; 6892 break; 6893 case PREDEF_TYPE_DEPENDENT_ID: 6894 T = Context.DependentTy; 6895 break; 6896 case PREDEF_TYPE_UNKNOWN_ANY: 6897 T = Context.UnknownAnyTy; 6898 break; 6899 case PREDEF_TYPE_NULLPTR_ID: 6900 T = Context.NullPtrTy; 6901 break; 6902 case PREDEF_TYPE_CHAR8_ID: 6903 T = Context.Char8Ty; 6904 break; 6905 case PREDEF_TYPE_CHAR16_ID: 6906 T = Context.Char16Ty; 6907 break; 6908 case PREDEF_TYPE_CHAR32_ID: 6909 T = Context.Char32Ty; 6910 break; 6911 case PREDEF_TYPE_OBJC_ID: 6912 T = Context.ObjCBuiltinIdTy; 6913 break; 6914 case PREDEF_TYPE_OBJC_CLASS: 6915 T = Context.ObjCBuiltinClassTy; 6916 break; 6917 case PREDEF_TYPE_OBJC_SEL: 6918 T = Context.ObjCBuiltinSelTy; 6919 break; 6920 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 6921 case PREDEF_TYPE_##Id##_ID: \ 6922 T = Context.SingletonId; \ 6923 break; 6924 #include "clang/Basic/OpenCLImageTypes.def" 6925 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 6926 case PREDEF_TYPE_##Id##_ID: \ 6927 T = Context.Id##Ty; \ 6928 break; 6929 #include "clang/Basic/OpenCLExtensionTypes.def" 6930 case PREDEF_TYPE_SAMPLER_ID: 6931 T = Context.OCLSamplerTy; 6932 break; 6933 case PREDEF_TYPE_EVENT_ID: 6934 T = Context.OCLEventTy; 6935 break; 6936 case PREDEF_TYPE_CLK_EVENT_ID: 6937 T = Context.OCLClkEventTy; 6938 break; 6939 case PREDEF_TYPE_QUEUE_ID: 6940 T = Context.OCLQueueTy; 6941 break; 6942 case PREDEF_TYPE_RESERVE_ID_ID: 6943 T = Context.OCLReserveIDTy; 6944 break; 6945 case PREDEF_TYPE_AUTO_DEDUCT: 6946 T = Context.getAutoDeductType(); 6947 break; 6948 case PREDEF_TYPE_AUTO_RREF_DEDUCT: 6949 T = Context.getAutoRRefDeductType(); 6950 break; 6951 case PREDEF_TYPE_ARC_UNBRIDGED_CAST: 6952 T = Context.ARCUnbridgedCastTy; 6953 break; 6954 case PREDEF_TYPE_BUILTIN_FN: 6955 T = Context.BuiltinFnTy; 6956 break; 6957 case PREDEF_TYPE_OMP_ARRAY_SECTION: 6958 T = Context.OMPArraySectionTy; 6959 break; 6960 #define SVE_TYPE(Name, Id, SingletonId) \ 6961 case PREDEF_TYPE_##Id##_ID: \ 6962 T = Context.SingletonId; \ 6963 break; 6964 #include "clang/Basic/AArch64SVEACLETypes.def" 6965 } 6966 6967 assert(!T.isNull() && "Unknown predefined type"); 6968 return T.withFastQualifiers(FastQuals); 6969 } 6970 6971 Index -= NUM_PREDEF_TYPE_IDS; 6972 assert(Index < TypesLoaded.size() && "Type index out-of-range"); 6973 if (TypesLoaded[Index].isNull()) { 6974 TypesLoaded[Index] = readTypeRecord(Index); 6975 if (TypesLoaded[Index].isNull()) 6976 return QualType(); 6977 6978 TypesLoaded[Index]->setFromAST(); 6979 if (DeserializationListener) 6980 DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID), 6981 TypesLoaded[Index]); 6982 } 6983 6984 return TypesLoaded[Index].withFastQualifiers(FastQuals); 6985 } 6986 6987 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) { 6988 return GetType(getGlobalTypeID(F, LocalID)); 6989 } 6990 6991 serialization::TypeID 6992 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const { 6993 unsigned FastQuals = LocalID & Qualifiers::FastMask; 6994 unsigned LocalIndex = LocalID >> Qualifiers::FastWidth; 6995 6996 if (LocalIndex < NUM_PREDEF_TYPE_IDS) 6997 return LocalID; 6998 6999 if (!F.ModuleOffsetMap.empty()) 7000 ReadModuleOffsetMap(F); 7001 7002 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7003 = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS); 7004 assert(I != F.TypeRemap.end() && "Invalid index into type index remap"); 7005 7006 unsigned GlobalIndex = LocalIndex + I->second; 7007 return (GlobalIndex << Qualifiers::FastWidth) | FastQuals; 7008 } 7009 7010 TemplateArgumentLocInfo 7011 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) { 7012 switch (Kind) { 7013 case TemplateArgument::Expression: 7014 return readExpr(); 7015 case TemplateArgument::Type: 7016 return readTypeSourceInfo(); 7017 case TemplateArgument::Template: { 7018 NestedNameSpecifierLoc QualifierLoc = 7019 readNestedNameSpecifierLoc(); 7020 SourceLocation TemplateNameLoc = readSourceLocation(); 7021 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7022 SourceLocation()); 7023 } 7024 case TemplateArgument::TemplateExpansion: { 7025 NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc(); 7026 SourceLocation TemplateNameLoc = readSourceLocation(); 7027 SourceLocation EllipsisLoc = readSourceLocation(); 7028 return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc, 7029 EllipsisLoc); 7030 } 7031 case TemplateArgument::Null: 7032 case TemplateArgument::Integral: 7033 case TemplateArgument::Declaration: 7034 case TemplateArgument::NullPtr: 7035 case TemplateArgument::Pack: 7036 // FIXME: Is this right? 7037 return TemplateArgumentLocInfo(); 7038 } 7039 llvm_unreachable("unexpected template argument loc"); 7040 } 7041 7042 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() { 7043 TemplateArgument Arg = readTemplateArgument(); 7044 7045 if (Arg.getKind() == TemplateArgument::Expression) { 7046 if (readBool()) // bool InfoHasSameExpr. 7047 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); 7048 } 7049 return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind())); 7050 } 7051 7052 const ASTTemplateArgumentListInfo * 7053 ASTRecordReader::readASTTemplateArgumentListInfo() { 7054 SourceLocation LAngleLoc = readSourceLocation(); 7055 SourceLocation RAngleLoc = readSourceLocation(); 7056 unsigned NumArgsAsWritten = readInt(); 7057 TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc); 7058 for (unsigned i = 0; i != NumArgsAsWritten; ++i) 7059 TemplArgsInfo.addArgument(readTemplateArgumentLoc()); 7060 return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo); 7061 } 7062 7063 Decl *ASTReader::GetExternalDecl(uint32_t ID) { 7064 return GetDecl(ID); 7065 } 7066 7067 void ASTReader::CompleteRedeclChain(const Decl *D) { 7068 if (NumCurrentElementsDeserializing) { 7069 // We arrange to not care about the complete redeclaration chain while we're 7070 // deserializing. Just remember that the AST has marked this one as complete 7071 // but that it's not actually complete yet, so we know we still need to 7072 // complete it later. 7073 PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D)); 7074 return; 7075 } 7076 7077 const DeclContext *DC = D->getDeclContext()->getRedeclContext(); 7078 7079 // If this is a named declaration, complete it by looking it up 7080 // within its context. 7081 // 7082 // FIXME: Merging a function definition should merge 7083 // all mergeable entities within it. 7084 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) || 7085 isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) { 7086 if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) { 7087 if (!getContext().getLangOpts().CPlusPlus && 7088 isa<TranslationUnitDecl>(DC)) { 7089 // Outside of C++, we don't have a lookup table for the TU, so update 7090 // the identifier instead. (For C++ modules, we don't store decls 7091 // in the serialized identifier table, so we do the lookup in the TU.) 7092 auto *II = Name.getAsIdentifierInfo(); 7093 assert(II && "non-identifier name in C?"); 7094 if (II->isOutOfDate()) 7095 updateOutOfDateIdentifier(*II); 7096 } else 7097 DC->lookup(Name); 7098 } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) { 7099 // Find all declarations of this kind from the relevant context. 7100 for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) { 7101 auto *DC = cast<DeclContext>(DCDecl); 7102 SmallVector<Decl*, 8> Decls; 7103 FindExternalLexicalDecls( 7104 DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls); 7105 } 7106 } 7107 } 7108 7109 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) 7110 CTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7111 if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) 7112 VTSD->getSpecializedTemplate()->LoadLazySpecializations(); 7113 if (auto *FD = dyn_cast<FunctionDecl>(D)) { 7114 if (auto *Template = FD->getPrimaryTemplate()) 7115 Template->LoadLazySpecializations(); 7116 } 7117 } 7118 7119 CXXCtorInitializer ** 7120 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) { 7121 RecordLocation Loc = getLocalBitOffset(Offset); 7122 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7123 SavedStreamPosition SavedPosition(Cursor); 7124 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7125 Error(std::move(Err)); 7126 return nullptr; 7127 } 7128 ReadingKindTracker ReadingKind(Read_Decl, *this); 7129 7130 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7131 if (!MaybeCode) { 7132 Error(MaybeCode.takeError()); 7133 return nullptr; 7134 } 7135 unsigned Code = MaybeCode.get(); 7136 7137 ASTRecordReader Record(*this, *Loc.F); 7138 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7139 if (!MaybeRecCode) { 7140 Error(MaybeRecCode.takeError()); 7141 return nullptr; 7142 } 7143 if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) { 7144 Error("malformed AST file: missing C++ ctor initializers"); 7145 return nullptr; 7146 } 7147 7148 return Record.readCXXCtorInitializers(); 7149 } 7150 7151 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { 7152 assert(ContextObj && "reading base specifiers with no AST context"); 7153 ASTContext &Context = *ContextObj; 7154 7155 RecordLocation Loc = getLocalBitOffset(Offset); 7156 BitstreamCursor &Cursor = Loc.F->DeclsCursor; 7157 SavedStreamPosition SavedPosition(Cursor); 7158 if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) { 7159 Error(std::move(Err)); 7160 return nullptr; 7161 } 7162 ReadingKindTracker ReadingKind(Read_Decl, *this); 7163 7164 Expected<unsigned> MaybeCode = Cursor.ReadCode(); 7165 if (!MaybeCode) { 7166 Error(MaybeCode.takeError()); 7167 return nullptr; 7168 } 7169 unsigned Code = MaybeCode.get(); 7170 7171 ASTRecordReader Record(*this, *Loc.F); 7172 Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code); 7173 if (!MaybeRecCode) { 7174 Error(MaybeCode.takeError()); 7175 return nullptr; 7176 } 7177 unsigned RecCode = MaybeRecCode.get(); 7178 7179 if (RecCode != DECL_CXX_BASE_SPECIFIERS) { 7180 Error("malformed AST file: missing C++ base specifiers"); 7181 return nullptr; 7182 } 7183 7184 unsigned NumBases = Record.readInt(); 7185 void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases); 7186 CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases]; 7187 for (unsigned I = 0; I != NumBases; ++I) 7188 Bases[I] = Record.readCXXBaseSpecifier(); 7189 return Bases; 7190 } 7191 7192 serialization::DeclID 7193 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { 7194 if (LocalID < NUM_PREDEF_DECL_IDS) 7195 return LocalID; 7196 7197 if (!F.ModuleOffsetMap.empty()) 7198 ReadModuleOffsetMap(F); 7199 7200 ContinuousRangeMap<uint32_t, int, 2>::iterator I 7201 = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS); 7202 assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); 7203 7204 return LocalID + I->second; 7205 } 7206 7207 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, 7208 ModuleFile &M) const { 7209 // Predefined decls aren't from any module. 7210 if (ID < NUM_PREDEF_DECL_IDS) 7211 return false; 7212 7213 return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && 7214 ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; 7215 } 7216 7217 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { 7218 if (!D->isFromASTFile()) 7219 return nullptr; 7220 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); 7221 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7222 return I->second; 7223 } 7224 7225 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { 7226 if (ID < NUM_PREDEF_DECL_IDS) 7227 return SourceLocation(); 7228 7229 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7230 7231 if (Index > DeclsLoaded.size()) { 7232 Error("declaration ID out-of-range for AST file"); 7233 return SourceLocation(); 7234 } 7235 7236 if (Decl *D = DeclsLoaded[Index]) 7237 return D->getLocation(); 7238 7239 SourceLocation Loc; 7240 DeclCursorForID(ID, Loc); 7241 return Loc; 7242 } 7243 7244 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { 7245 switch (ID) { 7246 case PREDEF_DECL_NULL_ID: 7247 return nullptr; 7248 7249 case PREDEF_DECL_TRANSLATION_UNIT_ID: 7250 return Context.getTranslationUnitDecl(); 7251 7252 case PREDEF_DECL_OBJC_ID_ID: 7253 return Context.getObjCIdDecl(); 7254 7255 case PREDEF_DECL_OBJC_SEL_ID: 7256 return Context.getObjCSelDecl(); 7257 7258 case PREDEF_DECL_OBJC_CLASS_ID: 7259 return Context.getObjCClassDecl(); 7260 7261 case PREDEF_DECL_OBJC_PROTOCOL_ID: 7262 return Context.getObjCProtocolDecl(); 7263 7264 case PREDEF_DECL_INT_128_ID: 7265 return Context.getInt128Decl(); 7266 7267 case PREDEF_DECL_UNSIGNED_INT_128_ID: 7268 return Context.getUInt128Decl(); 7269 7270 case PREDEF_DECL_OBJC_INSTANCETYPE_ID: 7271 return Context.getObjCInstanceTypeDecl(); 7272 7273 case PREDEF_DECL_BUILTIN_VA_LIST_ID: 7274 return Context.getBuiltinVaListDecl(); 7275 7276 case PREDEF_DECL_VA_LIST_TAG: 7277 return Context.getVaListTagDecl(); 7278 7279 case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID: 7280 return Context.getBuiltinMSVaListDecl(); 7281 7282 case PREDEF_DECL_EXTERN_C_CONTEXT_ID: 7283 return Context.getExternCContextDecl(); 7284 7285 case PREDEF_DECL_MAKE_INTEGER_SEQ_ID: 7286 return Context.getMakeIntegerSeqDecl(); 7287 7288 case PREDEF_DECL_CF_CONSTANT_STRING_ID: 7289 return Context.getCFConstantStringDecl(); 7290 7291 case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID: 7292 return Context.getCFConstantStringTagDecl(); 7293 7294 case PREDEF_DECL_TYPE_PACK_ELEMENT_ID: 7295 return Context.getTypePackElementDecl(); 7296 } 7297 llvm_unreachable("PredefinedDeclIDs unknown enum value"); 7298 } 7299 7300 Decl *ASTReader::GetExistingDecl(DeclID ID) { 7301 assert(ContextObj && "reading decl with no AST context"); 7302 if (ID < NUM_PREDEF_DECL_IDS) { 7303 Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); 7304 if (D) { 7305 // Track that we have merged the declaration with ID \p ID into the 7306 // pre-existing predefined declaration \p D. 7307 auto &Merged = KeyDecls[D->getCanonicalDecl()]; 7308 if (Merged.empty()) 7309 Merged.push_back(ID); 7310 } 7311 return D; 7312 } 7313 7314 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7315 7316 if (Index >= DeclsLoaded.size()) { 7317 assert(0 && "declaration ID out-of-range for AST file"); 7318 Error("declaration ID out-of-range for AST file"); 7319 return nullptr; 7320 } 7321 7322 return DeclsLoaded[Index]; 7323 } 7324 7325 Decl *ASTReader::GetDecl(DeclID ID) { 7326 if (ID < NUM_PREDEF_DECL_IDS) 7327 return GetExistingDecl(ID); 7328 7329 unsigned Index = ID - NUM_PREDEF_DECL_IDS; 7330 7331 if (Index >= DeclsLoaded.size()) { 7332 assert(0 && "declaration ID out-of-range for AST file"); 7333 Error("declaration ID out-of-range for AST file"); 7334 return nullptr; 7335 } 7336 7337 if (!DeclsLoaded[Index]) { 7338 ReadDeclRecord(ID); 7339 if (DeserializationListener) 7340 DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); 7341 } 7342 7343 return DeclsLoaded[Index]; 7344 } 7345 7346 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, 7347 DeclID GlobalID) { 7348 if (GlobalID < NUM_PREDEF_DECL_IDS) 7349 return GlobalID; 7350 7351 GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); 7352 assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); 7353 ModuleFile *Owner = I->second; 7354 7355 llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos 7356 = M.GlobalToLocalDeclIDs.find(Owner); 7357 if (Pos == M.GlobalToLocalDeclIDs.end()) 7358 return 0; 7359 7360 return GlobalID - Owner->BaseDeclID + Pos->second; 7361 } 7362 7363 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, 7364 const RecordData &Record, 7365 unsigned &Idx) { 7366 if (Idx >= Record.size()) { 7367 Error("Corrupted AST file"); 7368 return 0; 7369 } 7370 7371 return getGlobalDeclID(F, Record[Idx++]); 7372 } 7373 7374 /// Resolve the offset of a statement into a statement. 7375 /// 7376 /// This operation will read a new statement from the external 7377 /// source each time it is called, and is meant to be used via a 7378 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). 7379 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) { 7380 // Switch case IDs are per Decl. 7381 ClearSwitchCaseIDs(); 7382 7383 // Offset here is a global offset across the entire chain. 7384 RecordLocation Loc = getLocalBitOffset(Offset); 7385 if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) { 7386 Error(std::move(Err)); 7387 return nullptr; 7388 } 7389 assert(NumCurrentElementsDeserializing == 0 && 7390 "should not be called while already deserializing"); 7391 Deserializing D(this); 7392 return ReadStmtFromStream(*Loc.F); 7393 } 7394 7395 void ASTReader::FindExternalLexicalDecls( 7396 const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant, 7397 SmallVectorImpl<Decl *> &Decls) { 7398 bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; 7399 7400 auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { 7401 assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); 7402 for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { 7403 auto K = (Decl::Kind)+LexicalDecls[I]; 7404 if (!IsKindWeWant(K)) 7405 continue; 7406 7407 auto ID = (serialization::DeclID)+LexicalDecls[I + 1]; 7408 7409 // Don't add predefined declarations to the lexical context more 7410 // than once. 7411 if (ID < NUM_PREDEF_DECL_IDS) { 7412 if (PredefsVisited[ID]) 7413 continue; 7414 7415 PredefsVisited[ID] = true; 7416 } 7417 7418 if (Decl *D = GetLocalDecl(*M, ID)) { 7419 assert(D->getKind() == K && "wrong kind for lexical decl"); 7420 if (!DC->isDeclInLexicalTraversal(D)) 7421 Decls.push_back(D); 7422 } 7423 } 7424 }; 7425 7426 if (isa<TranslationUnitDecl>(DC)) { 7427 for (auto Lexical : TULexicalDecls) 7428 Visit(Lexical.first, Lexical.second); 7429 } else { 7430 auto I = LexicalDecls.find(DC); 7431 if (I != LexicalDecls.end()) 7432 Visit(I->second.first, I->second.second); 7433 } 7434 7435 ++NumLexicalDeclContextsRead; 7436 } 7437 7438 namespace { 7439 7440 class DeclIDComp { 7441 ASTReader &Reader; 7442 ModuleFile &Mod; 7443 7444 public: 7445 DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {} 7446 7447 bool operator()(LocalDeclID L, LocalDeclID R) const { 7448 SourceLocation LHS = getLocation(L); 7449 SourceLocation RHS = getLocation(R); 7450 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7451 } 7452 7453 bool operator()(SourceLocation LHS, LocalDeclID R) const { 7454 SourceLocation RHS = getLocation(R); 7455 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7456 } 7457 7458 bool operator()(LocalDeclID L, SourceLocation RHS) const { 7459 SourceLocation LHS = getLocation(L); 7460 return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS); 7461 } 7462 7463 SourceLocation getLocation(LocalDeclID ID) const { 7464 return Reader.getSourceManager().getFileLoc( 7465 Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID))); 7466 } 7467 }; 7468 7469 } // namespace 7470 7471 void ASTReader::FindFileRegionDecls(FileID File, 7472 unsigned Offset, unsigned Length, 7473 SmallVectorImpl<Decl *> &Decls) { 7474 SourceManager &SM = getSourceManager(); 7475 7476 llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File); 7477 if (I == FileDeclIDs.end()) 7478 return; 7479 7480 FileDeclsInfo &DInfo = I->second; 7481 if (DInfo.Decls.empty()) 7482 return; 7483 7484 SourceLocation 7485 BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset); 7486 SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length); 7487 7488 DeclIDComp DIDComp(*this, *DInfo.Mod); 7489 ArrayRef<serialization::LocalDeclID>::iterator BeginIt = 7490 llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp); 7491 if (BeginIt != DInfo.Decls.begin()) 7492 --BeginIt; 7493 7494 // If we are pointing at a top-level decl inside an objc container, we need 7495 // to backtrack until we find it otherwise we will fail to report that the 7496 // region overlaps with an objc container. 7497 while (BeginIt != DInfo.Decls.begin() && 7498 GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt)) 7499 ->isTopLevelDeclInObjCContainer()) 7500 --BeginIt; 7501 7502 ArrayRef<serialization::LocalDeclID>::iterator EndIt = 7503 llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp); 7504 if (EndIt != DInfo.Decls.end()) 7505 ++EndIt; 7506 7507 for (ArrayRef<serialization::LocalDeclID>::iterator 7508 DIt = BeginIt; DIt != EndIt; ++DIt) 7509 Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt))); 7510 } 7511 7512 bool 7513 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, 7514 DeclarationName Name) { 7515 assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() && 7516 "DeclContext has no visible decls in storage"); 7517 if (!Name) 7518 return false; 7519 7520 auto It = Lookups.find(DC); 7521 if (It == Lookups.end()) 7522 return false; 7523 7524 Deserializing LookupResults(this); 7525 7526 // Load the list of declarations. 7527 SmallVector<NamedDecl *, 64> Decls; 7528 for (DeclID ID : It->second.Table.find(Name)) { 7529 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7530 if (ND->getDeclName() == Name) 7531 Decls.push_back(ND); 7532 } 7533 7534 ++NumVisibleDeclContextsRead; 7535 SetExternalVisibleDeclsForName(DC, Name, Decls); 7536 return !Decls.empty(); 7537 } 7538 7539 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { 7540 if (!DC->hasExternalVisibleStorage()) 7541 return; 7542 7543 auto It = Lookups.find(DC); 7544 assert(It != Lookups.end() && 7545 "have external visible storage but no lookup tables"); 7546 7547 DeclsMap Decls; 7548 7549 for (DeclID ID : It->second.Table.findAll()) { 7550 NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); 7551 Decls[ND->getDeclName()].push_back(ND); 7552 } 7553 7554 ++NumVisibleDeclContextsRead; 7555 7556 for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 7557 SetExternalVisibleDeclsForName(DC, I->first, I->second); 7558 } 7559 const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false); 7560 } 7561 7562 const serialization::reader::DeclContextLookupTable * 7563 ASTReader::getLoadedLookupTables(DeclContext *Primary) const { 7564 auto I = Lookups.find(Primary); 7565 return I == Lookups.end() ? nullptr : &I->second; 7566 } 7567 7568 /// Under non-PCH compilation the consumer receives the objc methods 7569 /// before receiving the implementation, and codegen depends on this. 7570 /// We simulate this by deserializing and passing to consumer the methods of the 7571 /// implementation before passing the deserialized implementation decl. 7572 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD, 7573 ASTConsumer *Consumer) { 7574 assert(ImplD && Consumer); 7575 7576 for (auto *I : ImplD->methods()) 7577 Consumer->HandleInterestingDecl(DeclGroupRef(I)); 7578 7579 Consumer->HandleInterestingDecl(DeclGroupRef(ImplD)); 7580 } 7581 7582 void ASTReader::PassInterestingDeclToConsumer(Decl *D) { 7583 if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D)) 7584 PassObjCImplDeclToConsumer(ImplD, Consumer); 7585 else 7586 Consumer->HandleInterestingDecl(DeclGroupRef(D)); 7587 } 7588 7589 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) { 7590 this->Consumer = Consumer; 7591 7592 if (Consumer) 7593 PassInterestingDeclsToConsumer(); 7594 7595 if (DeserializationListener) 7596 DeserializationListener->ReaderInitialized(this); 7597 } 7598 7599 void ASTReader::PrintStats() { 7600 std::fprintf(stderr, "*** AST File Statistics:\n"); 7601 7602 unsigned NumTypesLoaded 7603 = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(), 7604 QualType()); 7605 unsigned NumDeclsLoaded 7606 = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(), 7607 (Decl *)nullptr); 7608 unsigned NumIdentifiersLoaded 7609 = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(), 7610 IdentifiersLoaded.end(), 7611 (IdentifierInfo *)nullptr); 7612 unsigned NumMacrosLoaded 7613 = MacrosLoaded.size() - std::count(MacrosLoaded.begin(), 7614 MacrosLoaded.end(), 7615 (MacroInfo *)nullptr); 7616 unsigned NumSelectorsLoaded 7617 = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(), 7618 SelectorsLoaded.end(), 7619 Selector()); 7620 7621 if (unsigned TotalNumSLocEntries = getTotalNumSLocs()) 7622 std::fprintf(stderr, " %u/%u source location entries read (%f%%)\n", 7623 NumSLocEntriesRead, TotalNumSLocEntries, 7624 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100)); 7625 if (!TypesLoaded.empty()) 7626 std::fprintf(stderr, " %u/%u types read (%f%%)\n", 7627 NumTypesLoaded, (unsigned)TypesLoaded.size(), 7628 ((float)NumTypesLoaded/TypesLoaded.size() * 100)); 7629 if (!DeclsLoaded.empty()) 7630 std::fprintf(stderr, " %u/%u declarations read (%f%%)\n", 7631 NumDeclsLoaded, (unsigned)DeclsLoaded.size(), 7632 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100)); 7633 if (!IdentifiersLoaded.empty()) 7634 std::fprintf(stderr, " %u/%u identifiers read (%f%%)\n", 7635 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(), 7636 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100)); 7637 if (!MacrosLoaded.empty()) 7638 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7639 NumMacrosLoaded, (unsigned)MacrosLoaded.size(), 7640 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100)); 7641 if (!SelectorsLoaded.empty()) 7642 std::fprintf(stderr, " %u/%u selectors read (%f%%)\n", 7643 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(), 7644 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100)); 7645 if (TotalNumStatements) 7646 std::fprintf(stderr, " %u/%u statements read (%f%%)\n", 7647 NumStatementsRead, TotalNumStatements, 7648 ((float)NumStatementsRead/TotalNumStatements * 100)); 7649 if (TotalNumMacros) 7650 std::fprintf(stderr, " %u/%u macros read (%f%%)\n", 7651 NumMacrosRead, TotalNumMacros, 7652 ((float)NumMacrosRead/TotalNumMacros * 100)); 7653 if (TotalLexicalDeclContexts) 7654 std::fprintf(stderr, " %u/%u lexical declcontexts read (%f%%)\n", 7655 NumLexicalDeclContextsRead, TotalLexicalDeclContexts, 7656 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts 7657 * 100)); 7658 if (TotalVisibleDeclContexts) 7659 std::fprintf(stderr, " %u/%u visible declcontexts read (%f%%)\n", 7660 NumVisibleDeclContextsRead, TotalVisibleDeclContexts, 7661 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts 7662 * 100)); 7663 if (TotalNumMethodPoolEntries) 7664 std::fprintf(stderr, " %u/%u method pool entries read (%f%%)\n", 7665 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries, 7666 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries 7667 * 100)); 7668 if (NumMethodPoolLookups) 7669 std::fprintf(stderr, " %u/%u method pool lookups succeeded (%f%%)\n", 7670 NumMethodPoolHits, NumMethodPoolLookups, 7671 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0)); 7672 if (NumMethodPoolTableLookups) 7673 std::fprintf(stderr, " %u/%u method pool table lookups succeeded (%f%%)\n", 7674 NumMethodPoolTableHits, NumMethodPoolTableLookups, 7675 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups 7676 * 100.0)); 7677 if (NumIdentifierLookupHits) 7678 std::fprintf(stderr, 7679 " %u / %u identifier table lookups succeeded (%f%%)\n", 7680 NumIdentifierLookupHits, NumIdentifierLookups, 7681 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 7682 7683 if (GlobalIndex) { 7684 std::fprintf(stderr, "\n"); 7685 GlobalIndex->printStats(); 7686 } 7687 7688 std::fprintf(stderr, "\n"); 7689 dump(); 7690 std::fprintf(stderr, "\n"); 7691 } 7692 7693 template<typename Key, typename ModuleFile, unsigned InitialCapacity> 7694 LLVM_DUMP_METHOD static void 7695 dumpModuleIDMap(StringRef Name, 7696 const ContinuousRangeMap<Key, ModuleFile *, 7697 InitialCapacity> &Map) { 7698 if (Map.begin() == Map.end()) 7699 return; 7700 7701 using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>; 7702 7703 llvm::errs() << Name << ":\n"; 7704 for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); 7705 I != IEnd; ++I) { 7706 llvm::errs() << " " << I->first << " -> " << I->second->FileName 7707 << "\n"; 7708 } 7709 } 7710 7711 LLVM_DUMP_METHOD void ASTReader::dump() { 7712 llvm::errs() << "*** PCH/ModuleFile Remappings:\n"; 7713 dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap); 7714 dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap); 7715 dumpModuleIDMap("Global type map", GlobalTypeMap); 7716 dumpModuleIDMap("Global declaration map", GlobalDeclMap); 7717 dumpModuleIDMap("Global identifier map", GlobalIdentifierMap); 7718 dumpModuleIDMap("Global macro map", GlobalMacroMap); 7719 dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap); 7720 dumpModuleIDMap("Global selector map", GlobalSelectorMap); 7721 dumpModuleIDMap("Global preprocessed entity map", 7722 GlobalPreprocessedEntityMap); 7723 7724 llvm::errs() << "\n*** PCH/Modules Loaded:"; 7725 for (ModuleFile &M : ModuleMgr) 7726 M.dump(); 7727 } 7728 7729 /// Return the amount of memory used by memory buffers, breaking down 7730 /// by heap-backed versus mmap'ed memory. 7731 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const { 7732 for (ModuleFile &I : ModuleMgr) { 7733 if (llvm::MemoryBuffer *buf = I.Buffer) { 7734 size_t bytes = buf->getBufferSize(); 7735 switch (buf->getBufferKind()) { 7736 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 7737 sizes.malloc_bytes += bytes; 7738 break; 7739 case llvm::MemoryBuffer::MemoryBuffer_MMap: 7740 sizes.mmap_bytes += bytes; 7741 break; 7742 } 7743 } 7744 } 7745 } 7746 7747 void ASTReader::InitializeSema(Sema &S) { 7748 SemaObj = &S; 7749 S.addExternalSource(this); 7750 7751 // Makes sure any declarations that were deserialized "too early" 7752 // still get added to the identifier's declaration chains. 7753 for (uint64_t ID : PreloadedDeclIDs) { 7754 NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); 7755 pushExternalDeclIntoScope(D, D->getDeclName()); 7756 } 7757 PreloadedDeclIDs.clear(); 7758 7759 // FIXME: What happens if these are changed by a module import? 7760 if (!FPPragmaOptions.empty()) { 7761 assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS"); 7762 SemaObj->FPFeatures = FPOptions(FPPragmaOptions[0]); 7763 } 7764 7765 SemaObj->OpenCLFeatures.copy(OpenCLExtensions); 7766 SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap; 7767 SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap; 7768 7769 UpdateSema(); 7770 } 7771 7772 void ASTReader::UpdateSema() { 7773 assert(SemaObj && "no Sema to update"); 7774 7775 // Load the offsets of the declarations that Sema references. 7776 // They will be lazily deserialized when needed. 7777 if (!SemaDeclRefs.empty()) { 7778 assert(SemaDeclRefs.size() % 3 == 0); 7779 for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { 7780 if (!SemaObj->StdNamespace) 7781 SemaObj->StdNamespace = SemaDeclRefs[I]; 7782 if (!SemaObj->StdBadAlloc) 7783 SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; 7784 if (!SemaObj->StdAlignValT) 7785 SemaObj->StdAlignValT = SemaDeclRefs[I+2]; 7786 } 7787 SemaDeclRefs.clear(); 7788 } 7789 7790 // Update the state of pragmas. Use the same API as if we had encountered the 7791 // pragma in the source. 7792 if(OptimizeOffPragmaLocation.isValid()) 7793 SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation); 7794 if (PragmaMSStructState != -1) 7795 SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState); 7796 if (PointersToMembersPragmaLocation.isValid()) { 7797 SemaObj->ActOnPragmaMSPointersToMembers( 7798 (LangOptions::PragmaMSPointersToMembersKind) 7799 PragmaMSPointersToMembersState, 7800 PointersToMembersPragmaLocation); 7801 } 7802 SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth; 7803 7804 if (PragmaPackCurrentValue) { 7805 // The bottom of the stack might have a default value. It must be adjusted 7806 // to the current value to ensure that the packing state is preserved after 7807 // popping entries that were included/imported from a PCH/module. 7808 bool DropFirst = false; 7809 if (!PragmaPackStack.empty() && 7810 PragmaPackStack.front().Location.isInvalid()) { 7811 assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue && 7812 "Expected a default alignment value"); 7813 SemaObj->PackStack.Stack.emplace_back( 7814 PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue, 7815 SemaObj->PackStack.CurrentPragmaLocation, 7816 PragmaPackStack.front().PushLocation); 7817 DropFirst = true; 7818 } 7819 for (const auto &Entry : 7820 llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0)) 7821 SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value, 7822 Entry.Location, Entry.PushLocation); 7823 if (PragmaPackCurrentLocation.isInvalid()) { 7824 assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue && 7825 "Expected a default alignment value"); 7826 // Keep the current values. 7827 } else { 7828 SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue; 7829 SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation; 7830 } 7831 } 7832 } 7833 7834 IdentifierInfo *ASTReader::get(StringRef Name) { 7835 // Note that we are loading an identifier. 7836 Deserializing AnIdentifier(this); 7837 7838 IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0, 7839 NumIdentifierLookups, 7840 NumIdentifierLookupHits); 7841 7842 // We don't need to do identifier table lookups in C++ modules (we preload 7843 // all interesting declarations, and don't need to use the scope for name 7844 // lookups). Perform the lookup in PCH files, though, since we don't build 7845 // a complete initial identifier table if we're carrying on from a PCH. 7846 if (PP.getLangOpts().CPlusPlus) { 7847 for (auto F : ModuleMgr.pch_modules()) 7848 if (Visitor(*F)) 7849 break; 7850 } else { 7851 // If there is a global index, look there first to determine which modules 7852 // provably do not have any results for this identifier. 7853 GlobalModuleIndex::HitSet Hits; 7854 GlobalModuleIndex::HitSet *HitsPtr = nullptr; 7855 if (!loadGlobalIndex()) { 7856 if (GlobalIndex->lookupIdentifier(Name, Hits)) { 7857 HitsPtr = &Hits; 7858 } 7859 } 7860 7861 ModuleMgr.visit(Visitor, HitsPtr); 7862 } 7863 7864 IdentifierInfo *II = Visitor.getIdentifierInfo(); 7865 markIdentifierUpToDate(II); 7866 return II; 7867 } 7868 7869 namespace clang { 7870 7871 /// An identifier-lookup iterator that enumerates all of the 7872 /// identifiers stored within a set of AST files. 7873 class ASTIdentifierIterator : public IdentifierIterator { 7874 /// The AST reader whose identifiers are being enumerated. 7875 const ASTReader &Reader; 7876 7877 /// The current index into the chain of AST files stored in 7878 /// the AST reader. 7879 unsigned Index; 7880 7881 /// The current position within the identifier lookup table 7882 /// of the current AST file. 7883 ASTIdentifierLookupTable::key_iterator Current; 7884 7885 /// The end position within the identifier lookup table of 7886 /// the current AST file. 7887 ASTIdentifierLookupTable::key_iterator End; 7888 7889 /// Whether to skip any modules in the ASTReader. 7890 bool SkipModules; 7891 7892 public: 7893 explicit ASTIdentifierIterator(const ASTReader &Reader, 7894 bool SkipModules = false); 7895 7896 StringRef Next() override; 7897 }; 7898 7899 } // namespace clang 7900 7901 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader, 7902 bool SkipModules) 7903 : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) { 7904 } 7905 7906 StringRef ASTIdentifierIterator::Next() { 7907 while (Current == End) { 7908 // If we have exhausted all of our AST files, we're done. 7909 if (Index == 0) 7910 return StringRef(); 7911 7912 --Index; 7913 ModuleFile &F = Reader.ModuleMgr[Index]; 7914 if (SkipModules && F.isModule()) 7915 continue; 7916 7917 ASTIdentifierLookupTable *IdTable = 7918 (ASTIdentifierLookupTable *)F.IdentifierLookupTable; 7919 Current = IdTable->key_begin(); 7920 End = IdTable->key_end(); 7921 } 7922 7923 // We have any identifiers remaining in the current AST file; return 7924 // the next one. 7925 StringRef Result = *Current; 7926 ++Current; 7927 return Result; 7928 } 7929 7930 namespace { 7931 7932 /// A utility for appending two IdentifierIterators. 7933 class ChainedIdentifierIterator : public IdentifierIterator { 7934 std::unique_ptr<IdentifierIterator> Current; 7935 std::unique_ptr<IdentifierIterator> Queued; 7936 7937 public: 7938 ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First, 7939 std::unique_ptr<IdentifierIterator> Second) 7940 : Current(std::move(First)), Queued(std::move(Second)) {} 7941 7942 StringRef Next() override { 7943 if (!Current) 7944 return StringRef(); 7945 7946 StringRef result = Current->Next(); 7947 if (!result.empty()) 7948 return result; 7949 7950 // Try the queued iterator, which may itself be empty. 7951 Current.reset(); 7952 std::swap(Current, Queued); 7953 return Next(); 7954 } 7955 }; 7956 7957 } // namespace 7958 7959 IdentifierIterator *ASTReader::getIdentifiers() { 7960 if (!loadGlobalIndex()) { 7961 std::unique_ptr<IdentifierIterator> ReaderIter( 7962 new ASTIdentifierIterator(*this, /*SkipModules=*/true)); 7963 std::unique_ptr<IdentifierIterator> ModulesIter( 7964 GlobalIndex->createIdentifierIterator()); 7965 return new ChainedIdentifierIterator(std::move(ReaderIter), 7966 std::move(ModulesIter)); 7967 } 7968 7969 return new ASTIdentifierIterator(*this); 7970 } 7971 7972 namespace clang { 7973 namespace serialization { 7974 7975 class ReadMethodPoolVisitor { 7976 ASTReader &Reader; 7977 Selector Sel; 7978 unsigned PriorGeneration; 7979 unsigned InstanceBits = 0; 7980 unsigned FactoryBits = 0; 7981 bool InstanceHasMoreThanOneDecl = false; 7982 bool FactoryHasMoreThanOneDecl = false; 7983 SmallVector<ObjCMethodDecl *, 4> InstanceMethods; 7984 SmallVector<ObjCMethodDecl *, 4> FactoryMethods; 7985 7986 public: 7987 ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel, 7988 unsigned PriorGeneration) 7989 : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {} 7990 7991 bool operator()(ModuleFile &M) { 7992 if (!M.SelectorLookupTable) 7993 return false; 7994 7995 // If we've already searched this module file, skip it now. 7996 if (M.Generation <= PriorGeneration) 7997 return true; 7998 7999 ++Reader.NumMethodPoolTableLookups; 8000 ASTSelectorLookupTable *PoolTable 8001 = (ASTSelectorLookupTable*)M.SelectorLookupTable; 8002 ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel); 8003 if (Pos == PoolTable->end()) 8004 return false; 8005 8006 ++Reader.NumMethodPoolTableHits; 8007 ++Reader.NumSelectorsRead; 8008 // FIXME: Not quite happy with the statistics here. We probably should 8009 // disable this tracking when called via LoadSelector. 8010 // Also, should entries without methods count as misses? 8011 ++Reader.NumMethodPoolEntriesRead; 8012 ASTSelectorLookupTrait::data_type Data = *Pos; 8013 if (Reader.DeserializationListener) 8014 Reader.DeserializationListener->SelectorRead(Data.ID, Sel); 8015 8016 InstanceMethods.append(Data.Instance.begin(), Data.Instance.end()); 8017 FactoryMethods.append(Data.Factory.begin(), Data.Factory.end()); 8018 InstanceBits = Data.InstanceBits; 8019 FactoryBits = Data.FactoryBits; 8020 InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl; 8021 FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl; 8022 return true; 8023 } 8024 8025 /// Retrieve the instance methods found by this visitor. 8026 ArrayRef<ObjCMethodDecl *> getInstanceMethods() const { 8027 return InstanceMethods; 8028 } 8029 8030 /// Retrieve the instance methods found by this visitor. 8031 ArrayRef<ObjCMethodDecl *> getFactoryMethods() const { 8032 return FactoryMethods; 8033 } 8034 8035 unsigned getInstanceBits() const { return InstanceBits; } 8036 unsigned getFactoryBits() const { return FactoryBits; } 8037 8038 bool instanceHasMoreThanOneDecl() const { 8039 return InstanceHasMoreThanOneDecl; 8040 } 8041 8042 bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; } 8043 }; 8044 8045 } // namespace serialization 8046 } // namespace clang 8047 8048 /// Add the given set of methods to the method list. 8049 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods, 8050 ObjCMethodList &List) { 8051 for (unsigned I = 0, N = Methods.size(); I != N; ++I) { 8052 S.addMethodToGlobalList(&List, Methods[I]); 8053 } 8054 } 8055 8056 void ASTReader::ReadMethodPool(Selector Sel) { 8057 // Get the selector generation and update it to the current generation. 8058 unsigned &Generation = SelectorGeneration[Sel]; 8059 unsigned PriorGeneration = Generation; 8060 Generation = getGeneration(); 8061 SelectorOutOfDate[Sel] = false; 8062 8063 // Search for methods defined with this selector. 8064 ++NumMethodPoolLookups; 8065 ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration); 8066 ModuleMgr.visit(Visitor); 8067 8068 if (Visitor.getInstanceMethods().empty() && 8069 Visitor.getFactoryMethods().empty()) 8070 return; 8071 8072 ++NumMethodPoolHits; 8073 8074 if (!getSema()) 8075 return; 8076 8077 Sema &S = *getSema(); 8078 Sema::GlobalMethodPool::iterator Pos 8079 = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first; 8080 8081 Pos->second.first.setBits(Visitor.getInstanceBits()); 8082 Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl()); 8083 Pos->second.second.setBits(Visitor.getFactoryBits()); 8084 Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl()); 8085 8086 // Add methods to the global pool *after* setting hasMoreThanOneDecl, since 8087 // when building a module we keep every method individually and may need to 8088 // update hasMoreThanOneDecl as we add the methods. 8089 addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first); 8090 addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second); 8091 } 8092 8093 void ASTReader::updateOutOfDateSelector(Selector Sel) { 8094 if (SelectorOutOfDate[Sel]) 8095 ReadMethodPool(Sel); 8096 } 8097 8098 void ASTReader::ReadKnownNamespaces( 8099 SmallVectorImpl<NamespaceDecl *> &Namespaces) { 8100 Namespaces.clear(); 8101 8102 for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) { 8103 if (NamespaceDecl *Namespace 8104 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I]))) 8105 Namespaces.push_back(Namespace); 8106 } 8107 } 8108 8109 void ASTReader::ReadUndefinedButUsed( 8110 llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { 8111 for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { 8112 NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); 8113 SourceLocation Loc = 8114 SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); 8115 Undefined.insert(std::make_pair(D, Loc)); 8116 } 8117 } 8118 8119 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< 8120 FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & 8121 Exprs) { 8122 for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { 8123 FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); 8124 uint64_t Count = DelayedDeleteExprs[Idx++]; 8125 for (uint64_t C = 0; C < Count; ++C) { 8126 SourceLocation DeleteLoc = 8127 SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]); 8128 const bool IsArrayForm = DelayedDeleteExprs[Idx++]; 8129 Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm)); 8130 } 8131 } 8132 } 8133 8134 void ASTReader::ReadTentativeDefinitions( 8135 SmallVectorImpl<VarDecl *> &TentativeDefs) { 8136 for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) { 8137 VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I])); 8138 if (Var) 8139 TentativeDefs.push_back(Var); 8140 } 8141 TentativeDefinitions.clear(); 8142 } 8143 8144 void ASTReader::ReadUnusedFileScopedDecls( 8145 SmallVectorImpl<const DeclaratorDecl *> &Decls) { 8146 for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) { 8147 DeclaratorDecl *D 8148 = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I])); 8149 if (D) 8150 Decls.push_back(D); 8151 } 8152 UnusedFileScopedDecls.clear(); 8153 } 8154 8155 void ASTReader::ReadDelegatingConstructors( 8156 SmallVectorImpl<CXXConstructorDecl *> &Decls) { 8157 for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) { 8158 CXXConstructorDecl *D 8159 = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I])); 8160 if (D) 8161 Decls.push_back(D); 8162 } 8163 DelegatingCtorDecls.clear(); 8164 } 8165 8166 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) { 8167 for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) { 8168 TypedefNameDecl *D 8169 = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I])); 8170 if (D) 8171 Decls.push_back(D); 8172 } 8173 ExtVectorDecls.clear(); 8174 } 8175 8176 void ASTReader::ReadUnusedLocalTypedefNameCandidates( 8177 llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) { 8178 for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N; 8179 ++I) { 8180 TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>( 8181 GetDecl(UnusedLocalTypedefNameCandidates[I])); 8182 if (D) 8183 Decls.insert(D); 8184 } 8185 UnusedLocalTypedefNameCandidates.clear(); 8186 } 8187 8188 void ASTReader::ReadDeclsToCheckForDeferredDiags( 8189 llvm::SmallVector<Decl *, 4> &Decls) { 8190 for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N; 8191 ++I) { 8192 auto *D = dyn_cast_or_null<Decl>( 8193 GetDecl(DeclsToCheckForDeferredDiags[I])); 8194 if (D) 8195 Decls.push_back(D); 8196 } 8197 DeclsToCheckForDeferredDiags.clear(); 8198 } 8199 8200 8201 void ASTReader::ReadReferencedSelectors( 8202 SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) { 8203 if (ReferencedSelectorsData.empty()) 8204 return; 8205 8206 // If there are @selector references added them to its pool. This is for 8207 // implementation of -Wselector. 8208 unsigned int DataSize = ReferencedSelectorsData.size()-1; 8209 unsigned I = 0; 8210 while (I < DataSize) { 8211 Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]); 8212 SourceLocation SelLoc 8213 = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]); 8214 Sels.push_back(std::make_pair(Sel, SelLoc)); 8215 } 8216 ReferencedSelectorsData.clear(); 8217 } 8218 8219 void ASTReader::ReadWeakUndeclaredIdentifiers( 8220 SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) { 8221 if (WeakUndeclaredIdentifiers.empty()) 8222 return; 8223 8224 for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) { 8225 IdentifierInfo *WeakId 8226 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8227 IdentifierInfo *AliasId 8228 = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]); 8229 SourceLocation Loc 8230 = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]); 8231 bool Used = WeakUndeclaredIdentifiers[I++]; 8232 WeakInfo WI(AliasId, Loc); 8233 WI.setUsed(Used); 8234 WeakIDs.push_back(std::make_pair(WeakId, WI)); 8235 } 8236 WeakUndeclaredIdentifiers.clear(); 8237 } 8238 8239 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { 8240 for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { 8241 ExternalVTableUse VT; 8242 VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); 8243 VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); 8244 VT.DefinitionRequired = VTableUses[Idx++]; 8245 VTables.push_back(VT); 8246 } 8247 8248 VTableUses.clear(); 8249 } 8250 8251 void ASTReader::ReadPendingInstantiations( 8252 SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { 8253 for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { 8254 ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); 8255 SourceLocation Loc 8256 = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); 8257 8258 Pending.push_back(std::make_pair(D, Loc)); 8259 } 8260 PendingInstantiations.clear(); 8261 } 8262 8263 void ASTReader::ReadLateParsedTemplates( 8264 llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>> 8265 &LPTMap) { 8266 for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N; 8267 /* In loop */) { 8268 FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++])); 8269 8270 auto LT = std::make_unique<LateParsedTemplate>(); 8271 LT->D = GetDecl(LateParsedTemplates[Idx++]); 8272 8273 ModuleFile *F = getOwningModuleFile(LT->D); 8274 assert(F && "No module"); 8275 8276 unsigned TokN = LateParsedTemplates[Idx++]; 8277 LT->Toks.reserve(TokN); 8278 for (unsigned T = 0; T < TokN; ++T) 8279 LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx)); 8280 8281 LPTMap.insert(std::make_pair(FD, std::move(LT))); 8282 } 8283 8284 LateParsedTemplates.clear(); 8285 } 8286 8287 void ASTReader::LoadSelector(Selector Sel) { 8288 // It would be complicated to avoid reading the methods anyway. So don't. 8289 ReadMethodPool(Sel); 8290 } 8291 8292 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { 8293 assert(ID && "Non-zero identifier ID required"); 8294 assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); 8295 IdentifiersLoaded[ID - 1] = II; 8296 if (DeserializationListener) 8297 DeserializationListener->IdentifierRead(ID, II); 8298 } 8299 8300 /// Set the globally-visible declarations associated with the given 8301 /// identifier. 8302 /// 8303 /// If the AST reader is currently in a state where the given declaration IDs 8304 /// cannot safely be resolved, they are queued until it is safe to resolve 8305 /// them. 8306 /// 8307 /// \param II an IdentifierInfo that refers to one or more globally-visible 8308 /// declarations. 8309 /// 8310 /// \param DeclIDs the set of declaration IDs with the name @p II that are 8311 /// visible at global scope. 8312 /// 8313 /// \param Decls if non-null, this vector will be populated with the set of 8314 /// deserialized declarations. These declarations will not be pushed into 8315 /// scope. 8316 void 8317 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, 8318 const SmallVectorImpl<uint32_t> &DeclIDs, 8319 SmallVectorImpl<Decl *> *Decls) { 8320 if (NumCurrentElementsDeserializing && !Decls) { 8321 PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); 8322 return; 8323 } 8324 8325 for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) { 8326 if (!SemaObj) { 8327 // Queue this declaration so that it will be added to the 8328 // translation unit scope and identifier's declaration chain 8329 // once a Sema object is known. 8330 PreloadedDeclIDs.push_back(DeclIDs[I]); 8331 continue; 8332 } 8333 8334 NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I])); 8335 8336 // If we're simply supposed to record the declarations, do so now. 8337 if (Decls) { 8338 Decls->push_back(D); 8339 continue; 8340 } 8341 8342 // Introduce this declaration into the translation-unit scope 8343 // and add it to the declaration chain for this identifier, so 8344 // that (unqualified) name lookup will find it. 8345 pushExternalDeclIntoScope(D, II); 8346 } 8347 } 8348 8349 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { 8350 if (ID == 0) 8351 return nullptr; 8352 8353 if (IdentifiersLoaded.empty()) { 8354 Error("no identifier table in AST file"); 8355 return nullptr; 8356 } 8357 8358 ID -= 1; 8359 if (!IdentifiersLoaded[ID]) { 8360 GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1); 8361 assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map"); 8362 ModuleFile *M = I->second; 8363 unsigned Index = ID - M->BaseIdentifierID; 8364 const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index]; 8365 8366 // All of the strings in the AST file are preceded by a 16-bit length. 8367 // Extract that 16-bit length to avoid having to execute strlen(). 8368 // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as 8369 // unsigned integers. This is important to avoid integer overflow when 8370 // we cast them to 'unsigned'. 8371 const unsigned char *StrLenPtr = (const unsigned char*) Str - 2; 8372 unsigned StrLen = (((unsigned) StrLenPtr[0]) 8373 | (((unsigned) StrLenPtr[1]) << 8)) - 1; 8374 auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen)); 8375 IdentifiersLoaded[ID] = &II; 8376 markIdentifierFromAST(*this, II); 8377 if (DeserializationListener) 8378 DeserializationListener->IdentifierRead(ID + 1, &II); 8379 } 8380 8381 return IdentifiersLoaded[ID]; 8382 } 8383 8384 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) { 8385 return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); 8386 } 8387 8388 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) { 8389 if (LocalID < NUM_PREDEF_IDENT_IDS) 8390 return LocalID; 8391 8392 if (!M.ModuleOffsetMap.empty()) 8393 ReadModuleOffsetMap(M); 8394 8395 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8396 = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS); 8397 assert(I != M.IdentifierRemap.end() 8398 && "Invalid index into identifier index remap"); 8399 8400 return LocalID + I->second; 8401 } 8402 8403 MacroInfo *ASTReader::getMacro(MacroID ID) { 8404 if (ID == 0) 8405 return nullptr; 8406 8407 if (MacrosLoaded.empty()) { 8408 Error("no macro table in AST file"); 8409 return nullptr; 8410 } 8411 8412 ID -= NUM_PREDEF_MACRO_IDS; 8413 if (!MacrosLoaded[ID]) { 8414 GlobalMacroMapType::iterator I 8415 = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS); 8416 assert(I != GlobalMacroMap.end() && "Corrupted global macro map"); 8417 ModuleFile *M = I->second; 8418 unsigned Index = ID - M->BaseMacroID; 8419 MacrosLoaded[ID] = ReadMacroRecord(*M, M->MacroOffsets[Index]); 8420 8421 if (DeserializationListener) 8422 DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS, 8423 MacrosLoaded[ID]); 8424 } 8425 8426 return MacrosLoaded[ID]; 8427 } 8428 8429 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) { 8430 if (LocalID < NUM_PREDEF_MACRO_IDS) 8431 return LocalID; 8432 8433 if (!M.ModuleOffsetMap.empty()) 8434 ReadModuleOffsetMap(M); 8435 8436 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8437 = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS); 8438 assert(I != M.MacroRemap.end() && "Invalid index into macro index remap"); 8439 8440 return LocalID + I->second; 8441 } 8442 8443 serialization::SubmoduleID 8444 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) { 8445 if (LocalID < NUM_PREDEF_SUBMODULE_IDS) 8446 return LocalID; 8447 8448 if (!M.ModuleOffsetMap.empty()) 8449 ReadModuleOffsetMap(M); 8450 8451 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8452 = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS); 8453 assert(I != M.SubmoduleRemap.end() 8454 && "Invalid index into submodule index remap"); 8455 8456 return LocalID + I->second; 8457 } 8458 8459 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) { 8460 if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) { 8461 assert(GlobalID == 0 && "Unhandled global submodule ID"); 8462 return nullptr; 8463 } 8464 8465 if (GlobalID > SubmodulesLoaded.size()) { 8466 Error("submodule ID out of range in AST file"); 8467 return nullptr; 8468 } 8469 8470 return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS]; 8471 } 8472 8473 Module *ASTReader::getModule(unsigned ID) { 8474 return getSubmodule(ID); 8475 } 8476 8477 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) { 8478 ModuleFile *MF = getOwningModuleFile(D); 8479 return MF && MF->PCHHasObjectFile; 8480 } 8481 8482 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) { 8483 if (ID & 1) { 8484 // It's a module, look it up by submodule ID. 8485 auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1)); 8486 return I == GlobalSubmoduleMap.end() ? nullptr : I->second; 8487 } else { 8488 // It's a prefix (preamble, PCH, ...). Look it up by index. 8489 unsigned IndexFromEnd = ID >> 1; 8490 assert(IndexFromEnd && "got reference to unknown module file"); 8491 return getModuleManager().pch_modules().end()[-IndexFromEnd]; 8492 } 8493 } 8494 8495 unsigned ASTReader::getModuleFileID(ModuleFile *F) { 8496 if (!F) 8497 return 1; 8498 8499 // For a file representing a module, use the submodule ID of the top-level 8500 // module as the file ID. For any other kind of file, the number of such 8501 // files loaded beforehand will be the same on reload. 8502 // FIXME: Is this true even if we have an explicit module file and a PCH? 8503 if (F->isModule()) 8504 return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1; 8505 8506 auto PCHModules = getModuleManager().pch_modules(); 8507 auto I = llvm::find(PCHModules, F); 8508 assert(I != PCHModules.end() && "emitting reference to unknown file"); 8509 return (I - PCHModules.end()) << 1; 8510 } 8511 8512 llvm::Optional<ASTSourceDescriptor> 8513 ASTReader::getSourceDescriptor(unsigned ID) { 8514 if (const Module *M = getSubmodule(ID)) 8515 return ASTSourceDescriptor(*M); 8516 8517 // If there is only a single PCH, return it instead. 8518 // Chained PCH are not supported. 8519 const auto &PCHChain = ModuleMgr.pch_modules(); 8520 if (std::distance(std::begin(PCHChain), std::end(PCHChain))) { 8521 ModuleFile &MF = ModuleMgr.getPrimaryModule(); 8522 StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName); 8523 StringRef FileName = llvm::sys::path::filename(MF.FileName); 8524 return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName, 8525 MF.Signature); 8526 } 8527 return None; 8528 } 8529 8530 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) { 8531 auto I = DefinitionSource.find(FD); 8532 if (I == DefinitionSource.end()) 8533 return EK_ReplyHazy; 8534 return I->second ? EK_Never : EK_Always; 8535 } 8536 8537 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) { 8538 return DecodeSelector(getGlobalSelectorID(M, LocalID)); 8539 } 8540 8541 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) { 8542 if (ID == 0) 8543 return Selector(); 8544 8545 if (ID > SelectorsLoaded.size()) { 8546 Error("selector ID out of range in AST file"); 8547 return Selector(); 8548 } 8549 8550 if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) { 8551 // Load this selector from the selector table. 8552 GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID); 8553 assert(I != GlobalSelectorMap.end() && "Corrupted global selector map"); 8554 ModuleFile &M = *I->second; 8555 ASTSelectorLookupTrait Trait(*this, M); 8556 unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS; 8557 SelectorsLoaded[ID - 1] = 8558 Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0); 8559 if (DeserializationListener) 8560 DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]); 8561 } 8562 8563 return SelectorsLoaded[ID - 1]; 8564 } 8565 8566 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) { 8567 return DecodeSelector(ID); 8568 } 8569 8570 uint32_t ASTReader::GetNumExternalSelectors() { 8571 // ID 0 (the null selector) is considered an external selector. 8572 return getTotalNumSelectors() + 1; 8573 } 8574 8575 serialization::SelectorID 8576 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const { 8577 if (LocalID < NUM_PREDEF_SELECTOR_IDS) 8578 return LocalID; 8579 8580 if (!M.ModuleOffsetMap.empty()) 8581 ReadModuleOffsetMap(M); 8582 8583 ContinuousRangeMap<uint32_t, int, 2>::iterator I 8584 = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS); 8585 assert(I != M.SelectorRemap.end() 8586 && "Invalid index into selector index remap"); 8587 8588 return LocalID + I->second; 8589 } 8590 8591 DeclarationNameLoc 8592 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) { 8593 DeclarationNameLoc DNLoc; 8594 switch (Name.getNameKind()) { 8595 case DeclarationName::CXXConstructorName: 8596 case DeclarationName::CXXDestructorName: 8597 case DeclarationName::CXXConversionFunctionName: 8598 DNLoc.NamedType.TInfo = readTypeSourceInfo(); 8599 break; 8600 8601 case DeclarationName::CXXOperatorName: 8602 DNLoc.CXXOperatorName.BeginOpNameLoc 8603 = readSourceLocation().getRawEncoding(); 8604 DNLoc.CXXOperatorName.EndOpNameLoc 8605 = readSourceLocation().getRawEncoding(); 8606 break; 8607 8608 case DeclarationName::CXXLiteralOperatorName: 8609 DNLoc.CXXLiteralOperatorName.OpNameLoc 8610 = readSourceLocation().getRawEncoding(); 8611 break; 8612 8613 case DeclarationName::Identifier: 8614 case DeclarationName::ObjCZeroArgSelector: 8615 case DeclarationName::ObjCOneArgSelector: 8616 case DeclarationName::ObjCMultiArgSelector: 8617 case DeclarationName::CXXUsingDirective: 8618 case DeclarationName::CXXDeductionGuideName: 8619 break; 8620 } 8621 return DNLoc; 8622 } 8623 8624 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() { 8625 DeclarationNameInfo NameInfo; 8626 NameInfo.setName(readDeclarationName()); 8627 NameInfo.setLoc(readSourceLocation()); 8628 NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName())); 8629 return NameInfo; 8630 } 8631 8632 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) { 8633 Info.QualifierLoc = readNestedNameSpecifierLoc(); 8634 unsigned NumTPLists = readInt(); 8635 Info.NumTemplParamLists = NumTPLists; 8636 if (NumTPLists) { 8637 Info.TemplParamLists = 8638 new (getContext()) TemplateParameterList *[NumTPLists]; 8639 for (unsigned i = 0; i != NumTPLists; ++i) 8640 Info.TemplParamLists[i] = readTemplateParameterList(); 8641 } 8642 } 8643 8644 TemplateParameterList * 8645 ASTRecordReader::readTemplateParameterList() { 8646 SourceLocation TemplateLoc = readSourceLocation(); 8647 SourceLocation LAngleLoc = readSourceLocation(); 8648 SourceLocation RAngleLoc = readSourceLocation(); 8649 8650 unsigned NumParams = readInt(); 8651 SmallVector<NamedDecl *, 16> Params; 8652 Params.reserve(NumParams); 8653 while (NumParams--) 8654 Params.push_back(readDeclAs<NamedDecl>()); 8655 8656 bool HasRequiresClause = readBool(); 8657 Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr; 8658 8659 TemplateParameterList *TemplateParams = TemplateParameterList::Create( 8660 getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause); 8661 return TemplateParams; 8662 } 8663 8664 void ASTRecordReader::readTemplateArgumentList( 8665 SmallVectorImpl<TemplateArgument> &TemplArgs, 8666 bool Canonicalize) { 8667 unsigned NumTemplateArgs = readInt(); 8668 TemplArgs.reserve(NumTemplateArgs); 8669 while (NumTemplateArgs--) 8670 TemplArgs.push_back(readTemplateArgument(Canonicalize)); 8671 } 8672 8673 /// Read a UnresolvedSet structure. 8674 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { 8675 unsigned NumDecls = readInt(); 8676 Set.reserve(getContext(), NumDecls); 8677 while (NumDecls--) { 8678 DeclID ID = readDeclID(); 8679 AccessSpecifier AS = (AccessSpecifier) readInt(); 8680 Set.addLazyDecl(getContext(), ID, AS); 8681 } 8682 } 8683 8684 CXXBaseSpecifier 8685 ASTRecordReader::readCXXBaseSpecifier() { 8686 bool isVirtual = readBool(); 8687 bool isBaseOfClass = readBool(); 8688 AccessSpecifier AS = static_cast<AccessSpecifier>(readInt()); 8689 bool inheritConstructors = readBool(); 8690 TypeSourceInfo *TInfo = readTypeSourceInfo(); 8691 SourceRange Range = readSourceRange(); 8692 SourceLocation EllipsisLoc = readSourceLocation(); 8693 CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo, 8694 EllipsisLoc); 8695 Result.setInheritConstructors(inheritConstructors); 8696 return Result; 8697 } 8698 8699 CXXCtorInitializer ** 8700 ASTRecordReader::readCXXCtorInitializers() { 8701 ASTContext &Context = getContext(); 8702 unsigned NumInitializers = readInt(); 8703 assert(NumInitializers && "wrote ctor initializers but have no inits"); 8704 auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers]; 8705 for (unsigned i = 0; i != NumInitializers; ++i) { 8706 TypeSourceInfo *TInfo = nullptr; 8707 bool IsBaseVirtual = false; 8708 FieldDecl *Member = nullptr; 8709 IndirectFieldDecl *IndirectMember = nullptr; 8710 8711 CtorInitializerType Type = (CtorInitializerType) readInt(); 8712 switch (Type) { 8713 case CTOR_INITIALIZER_BASE: 8714 TInfo = readTypeSourceInfo(); 8715 IsBaseVirtual = readBool(); 8716 break; 8717 8718 case CTOR_INITIALIZER_DELEGATING: 8719 TInfo = readTypeSourceInfo(); 8720 break; 8721 8722 case CTOR_INITIALIZER_MEMBER: 8723 Member = readDeclAs<FieldDecl>(); 8724 break; 8725 8726 case CTOR_INITIALIZER_INDIRECT_MEMBER: 8727 IndirectMember = readDeclAs<IndirectFieldDecl>(); 8728 break; 8729 } 8730 8731 SourceLocation MemberOrEllipsisLoc = readSourceLocation(); 8732 Expr *Init = readExpr(); 8733 SourceLocation LParenLoc = readSourceLocation(); 8734 SourceLocation RParenLoc = readSourceLocation(); 8735 8736 CXXCtorInitializer *BOMInit; 8737 if (Type == CTOR_INITIALIZER_BASE) 8738 BOMInit = new (Context) 8739 CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init, 8740 RParenLoc, MemberOrEllipsisLoc); 8741 else if (Type == CTOR_INITIALIZER_DELEGATING) 8742 BOMInit = new (Context) 8743 CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc); 8744 else if (Member) 8745 BOMInit = new (Context) 8746 CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc, 8747 Init, RParenLoc); 8748 else 8749 BOMInit = new (Context) 8750 CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc, 8751 LParenLoc, Init, RParenLoc); 8752 8753 if (/*IsWritten*/readBool()) { 8754 unsigned SourceOrder = readInt(); 8755 BOMInit->setSourceOrder(SourceOrder); 8756 } 8757 8758 CtorInitializers[i] = BOMInit; 8759 } 8760 8761 return CtorInitializers; 8762 } 8763 8764 NestedNameSpecifierLoc 8765 ASTRecordReader::readNestedNameSpecifierLoc() { 8766 ASTContext &Context = getContext(); 8767 unsigned N = readInt(); 8768 NestedNameSpecifierLocBuilder Builder; 8769 for (unsigned I = 0; I != N; ++I) { 8770 auto Kind = readNestedNameSpecifierKind(); 8771 switch (Kind) { 8772 case NestedNameSpecifier::Identifier: { 8773 IdentifierInfo *II = readIdentifier(); 8774 SourceRange Range = readSourceRange(); 8775 Builder.Extend(Context, II, Range.getBegin(), Range.getEnd()); 8776 break; 8777 } 8778 8779 case NestedNameSpecifier::Namespace: { 8780 NamespaceDecl *NS = readDeclAs<NamespaceDecl>(); 8781 SourceRange Range = readSourceRange(); 8782 Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd()); 8783 break; 8784 } 8785 8786 case NestedNameSpecifier::NamespaceAlias: { 8787 NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>(); 8788 SourceRange Range = readSourceRange(); 8789 Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd()); 8790 break; 8791 } 8792 8793 case NestedNameSpecifier::TypeSpec: 8794 case NestedNameSpecifier::TypeSpecWithTemplate: { 8795 bool Template = readBool(); 8796 TypeSourceInfo *T = readTypeSourceInfo(); 8797 if (!T) 8798 return NestedNameSpecifierLoc(); 8799 SourceLocation ColonColonLoc = readSourceLocation(); 8800 8801 // FIXME: 'template' keyword location not saved anywhere, so we fake it. 8802 Builder.Extend(Context, 8803 Template? T->getTypeLoc().getBeginLoc() : SourceLocation(), 8804 T->getTypeLoc(), ColonColonLoc); 8805 break; 8806 } 8807 8808 case NestedNameSpecifier::Global: { 8809 SourceLocation ColonColonLoc = readSourceLocation(); 8810 Builder.MakeGlobal(Context, ColonColonLoc); 8811 break; 8812 } 8813 8814 case NestedNameSpecifier::Super: { 8815 CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>(); 8816 SourceRange Range = readSourceRange(); 8817 Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd()); 8818 break; 8819 } 8820 } 8821 } 8822 8823 return Builder.getWithLocInContext(Context); 8824 } 8825 8826 SourceRange 8827 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record, 8828 unsigned &Idx) { 8829 SourceLocation beg = ReadSourceLocation(F, Record, Idx); 8830 SourceLocation end = ReadSourceLocation(F, Record, Idx); 8831 return SourceRange(beg, end); 8832 } 8833 8834 static FixedPointSemantics 8835 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record, 8836 unsigned &Idx) { 8837 unsigned Width = Record[Idx++]; 8838 unsigned Scale = Record[Idx++]; 8839 uint64_t Tmp = Record[Idx++]; 8840 bool IsSigned = Tmp & 0x1; 8841 bool IsSaturated = Tmp & 0x2; 8842 bool HasUnsignedPadding = Tmp & 0x4; 8843 return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated, 8844 HasUnsignedPadding); 8845 } 8846 8847 static const llvm::fltSemantics & 8848 readAPFloatSemantics(ASTRecordReader &reader) { 8849 return llvm::APFloatBase::EnumToSemantics( 8850 static_cast<llvm::APFloatBase::Semantics>(reader.readInt())); 8851 } 8852 8853 APValue ASTRecordReader::readAPValue() { 8854 unsigned Kind = readInt(); 8855 switch ((APValue::ValueKind) Kind) { 8856 case APValue::None: 8857 return APValue(); 8858 case APValue::Indeterminate: 8859 return APValue::IndeterminateValue(); 8860 case APValue::Int: 8861 return APValue(readAPSInt()); 8862 case APValue::Float: { 8863 const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this); 8864 return APValue(readAPFloat(FloatSema)); 8865 } 8866 case APValue::FixedPoint: { 8867 FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx); 8868 return APValue(APFixedPoint(readAPInt(), FPSema)); 8869 } 8870 case APValue::ComplexInt: { 8871 llvm::APSInt First = readAPSInt(); 8872 return APValue(std::move(First), readAPSInt()); 8873 } 8874 case APValue::ComplexFloat: { 8875 const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this); 8876 llvm::APFloat First = readAPFloat(FloatSema1); 8877 const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this); 8878 return APValue(std::move(First), readAPFloat(FloatSema2)); 8879 } 8880 case APValue::LValue: 8881 case APValue::Vector: 8882 case APValue::Array: 8883 case APValue::Struct: 8884 case APValue::Union: 8885 case APValue::MemberPointer: 8886 case APValue::AddrLabelDiff: 8887 // TODO : Handle all these APValue::ValueKind. 8888 return APValue(); 8889 } 8890 llvm_unreachable("Invalid APValue::ValueKind"); 8891 } 8892 8893 /// Read a floating-point value 8894 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) { 8895 return llvm::APFloat(Sem, readAPInt()); 8896 } 8897 8898 // Read a string 8899 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) { 8900 unsigned Len = Record[Idx++]; 8901 std::string Result(Record.data() + Idx, Record.data() + Idx + Len); 8902 Idx += Len; 8903 return Result; 8904 } 8905 8906 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record, 8907 unsigned &Idx) { 8908 std::string Filename = ReadString(Record, Idx); 8909 ResolveImportedPath(F, Filename); 8910 return Filename; 8911 } 8912 8913 std::string ASTReader::ReadPath(StringRef BaseDirectory, 8914 const RecordData &Record, unsigned &Idx) { 8915 std::string Filename = ReadString(Record, Idx); 8916 if (!BaseDirectory.empty()) 8917 ResolveImportedPath(Filename, BaseDirectory); 8918 return Filename; 8919 } 8920 8921 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record, 8922 unsigned &Idx) { 8923 unsigned Major = Record[Idx++]; 8924 unsigned Minor = Record[Idx++]; 8925 unsigned Subminor = Record[Idx++]; 8926 if (Minor == 0) 8927 return VersionTuple(Major); 8928 if (Subminor == 0) 8929 return VersionTuple(Major, Minor - 1); 8930 return VersionTuple(Major, Minor - 1, Subminor - 1); 8931 } 8932 8933 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F, 8934 const RecordData &Record, 8935 unsigned &Idx) { 8936 CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx); 8937 return CXXTemporary::Create(getContext(), Decl); 8938 } 8939 8940 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const { 8941 return Diag(CurrentImportLoc, DiagID); 8942 } 8943 8944 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const { 8945 return Diags.Report(Loc, DiagID); 8946 } 8947 8948 /// Retrieve the identifier table associated with the 8949 /// preprocessor. 8950 IdentifierTable &ASTReader::getIdentifierTable() { 8951 return PP.getIdentifierTable(); 8952 } 8953 8954 /// Record that the given ID maps to the given switch-case 8955 /// statement. 8956 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) { 8957 assert((*CurrSwitchCaseStmts)[ID] == nullptr && 8958 "Already have a SwitchCase with this ID"); 8959 (*CurrSwitchCaseStmts)[ID] = SC; 8960 } 8961 8962 /// Retrieve the switch-case statement with the given ID. 8963 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) { 8964 assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID"); 8965 return (*CurrSwitchCaseStmts)[ID]; 8966 } 8967 8968 void ASTReader::ClearSwitchCaseIDs() { 8969 CurrSwitchCaseStmts->clear(); 8970 } 8971 8972 void ASTReader::ReadComments() { 8973 ASTContext &Context = getContext(); 8974 std::vector<RawComment *> Comments; 8975 for (SmallVectorImpl<std::pair<BitstreamCursor, 8976 serialization::ModuleFile *>>::iterator 8977 I = CommentsCursors.begin(), 8978 E = CommentsCursors.end(); 8979 I != E; ++I) { 8980 Comments.clear(); 8981 BitstreamCursor &Cursor = I->first; 8982 serialization::ModuleFile &F = *I->second; 8983 SavedStreamPosition SavedPosition(Cursor); 8984 8985 RecordData Record; 8986 while (true) { 8987 Expected<llvm::BitstreamEntry> MaybeEntry = 8988 Cursor.advanceSkippingSubblocks( 8989 BitstreamCursor::AF_DontPopBlockAtEnd); 8990 if (!MaybeEntry) { 8991 Error(MaybeEntry.takeError()); 8992 return; 8993 } 8994 llvm::BitstreamEntry Entry = MaybeEntry.get(); 8995 8996 switch (Entry.Kind) { 8997 case llvm::BitstreamEntry::SubBlock: // Handled for us already. 8998 case llvm::BitstreamEntry::Error: 8999 Error("malformed block record in AST file"); 9000 return; 9001 case llvm::BitstreamEntry::EndBlock: 9002 goto NextCursor; 9003 case llvm::BitstreamEntry::Record: 9004 // The interesting case. 9005 break; 9006 } 9007 9008 // Read a record. 9009 Record.clear(); 9010 Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record); 9011 if (!MaybeComment) { 9012 Error(MaybeComment.takeError()); 9013 return; 9014 } 9015 switch ((CommentRecordTypes)MaybeComment.get()) { 9016 case COMMENTS_RAW_COMMENT: { 9017 unsigned Idx = 0; 9018 SourceRange SR = ReadSourceRange(F, Record, Idx); 9019 RawComment::CommentKind Kind = 9020 (RawComment::CommentKind) Record[Idx++]; 9021 bool IsTrailingComment = Record[Idx++]; 9022 bool IsAlmostTrailingComment = Record[Idx++]; 9023 Comments.push_back(new (Context) RawComment( 9024 SR, Kind, IsTrailingComment, IsAlmostTrailingComment)); 9025 break; 9026 } 9027 } 9028 } 9029 NextCursor: 9030 llvm::DenseMap<FileID, std::map<unsigned, RawComment *>> 9031 FileToOffsetToComment; 9032 for (RawComment *C : Comments) { 9033 SourceLocation CommentLoc = C->getBeginLoc(); 9034 if (CommentLoc.isValid()) { 9035 std::pair<FileID, unsigned> Loc = 9036 SourceMgr.getDecomposedLoc(CommentLoc); 9037 if (Loc.first.isValid()) 9038 Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C); 9039 } 9040 } 9041 } 9042 } 9043 9044 void ASTReader::visitInputFiles(serialization::ModuleFile &MF, 9045 bool IncludeSystem, bool Complain, 9046 llvm::function_ref<void(const serialization::InputFile &IF, 9047 bool isSystem)> Visitor) { 9048 unsigned NumUserInputs = MF.NumUserInputFiles; 9049 unsigned NumInputs = MF.InputFilesLoaded.size(); 9050 assert(NumUserInputs <= NumInputs); 9051 unsigned N = IncludeSystem ? NumInputs : NumUserInputs; 9052 for (unsigned I = 0; I < N; ++I) { 9053 bool IsSystem = I >= NumUserInputs; 9054 InputFile IF = getInputFile(MF, I+1, Complain); 9055 Visitor(IF, IsSystem); 9056 } 9057 } 9058 9059 void ASTReader::visitTopLevelModuleMaps( 9060 serialization::ModuleFile &MF, 9061 llvm::function_ref<void(const FileEntry *FE)> Visitor) { 9062 unsigned NumInputs = MF.InputFilesLoaded.size(); 9063 for (unsigned I = 0; I < NumInputs; ++I) { 9064 InputFileInfo IFI = readInputFileInfo(MF, I + 1); 9065 if (IFI.TopLevelModuleMap) 9066 // FIXME: This unnecessarily re-reads the InputFileInfo. 9067 if (auto *FE = getInputFile(MF, I + 1).getFile()) 9068 Visitor(FE); 9069 } 9070 } 9071 9072 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) { 9073 // If we know the owning module, use it. 9074 if (Module *M = D->getImportedOwningModule()) 9075 return M->getFullModuleName(); 9076 9077 // Otherwise, use the name of the top-level module the decl is within. 9078 if (ModuleFile *M = getOwningModuleFile(D)) 9079 return M->ModuleName; 9080 9081 // Not from a module. 9082 return {}; 9083 } 9084 9085 void ASTReader::finishPendingActions() { 9086 while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() || 9087 !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() || 9088 !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() || 9089 !PendingUpdateRecords.empty()) { 9090 // If any identifiers with corresponding top-level declarations have 9091 // been loaded, load those declarations now. 9092 using TopLevelDeclsMap = 9093 llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>; 9094 TopLevelDeclsMap TopLevelDecls; 9095 9096 while (!PendingIdentifierInfos.empty()) { 9097 IdentifierInfo *II = PendingIdentifierInfos.back().first; 9098 SmallVector<uint32_t, 4> DeclIDs = 9099 std::move(PendingIdentifierInfos.back().second); 9100 PendingIdentifierInfos.pop_back(); 9101 9102 SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]); 9103 } 9104 9105 // Load each function type that we deferred loading because it was a 9106 // deduced type that might refer to a local type declared within itself. 9107 for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) { 9108 auto *FD = PendingFunctionTypes[I].first; 9109 FD->setType(GetType(PendingFunctionTypes[I].second)); 9110 9111 // If we gave a function a deduced return type, remember that we need to 9112 // propagate that along the redeclaration chain. 9113 auto *DT = FD->getReturnType()->getContainedDeducedType(); 9114 if (DT && DT->isDeduced()) 9115 PendingDeducedTypeUpdates.insert( 9116 {FD->getCanonicalDecl(), FD->getReturnType()}); 9117 } 9118 PendingFunctionTypes.clear(); 9119 9120 // For each decl chain that we wanted to complete while deserializing, mark 9121 // it as "still needs to be completed". 9122 for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) { 9123 markIncompleteDeclChain(PendingIncompleteDeclChains[I]); 9124 } 9125 PendingIncompleteDeclChains.clear(); 9126 9127 // Load pending declaration chains. 9128 for (unsigned I = 0; I != PendingDeclChains.size(); ++I) 9129 loadPendingDeclChain(PendingDeclChains[I].first, 9130 PendingDeclChains[I].second); 9131 PendingDeclChains.clear(); 9132 9133 // Make the most recent of the top-level declarations visible. 9134 for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(), 9135 TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) { 9136 IdentifierInfo *II = TLD->first; 9137 for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) { 9138 pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II); 9139 } 9140 } 9141 9142 // Load any pending macro definitions. 9143 for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) { 9144 IdentifierInfo *II = PendingMacroIDs.begin()[I].first; 9145 SmallVector<PendingMacroInfo, 2> GlobalIDs; 9146 GlobalIDs.swap(PendingMacroIDs.begin()[I].second); 9147 // Initialize the macro history from chained-PCHs ahead of module imports. 9148 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9149 ++IDIdx) { 9150 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9151 if (!Info.M->isModule()) 9152 resolvePendingMacro(II, Info); 9153 } 9154 // Handle module imports. 9155 for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs; 9156 ++IDIdx) { 9157 const PendingMacroInfo &Info = GlobalIDs[IDIdx]; 9158 if (Info.M->isModule()) 9159 resolvePendingMacro(II, Info); 9160 } 9161 } 9162 PendingMacroIDs.clear(); 9163 9164 // Wire up the DeclContexts for Decls that we delayed setting until 9165 // recursive loading is completed. 9166 while (!PendingDeclContextInfos.empty()) { 9167 PendingDeclContextInfo Info = PendingDeclContextInfos.front(); 9168 PendingDeclContextInfos.pop_front(); 9169 DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC)); 9170 DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC)); 9171 Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext()); 9172 } 9173 9174 // Perform any pending declaration updates. 9175 while (!PendingUpdateRecords.empty()) { 9176 auto Update = PendingUpdateRecords.pop_back_val(); 9177 ReadingKindTracker ReadingKind(Read_Decl, *this); 9178 loadDeclUpdateRecords(Update); 9179 } 9180 } 9181 9182 // At this point, all update records for loaded decls are in place, so any 9183 // fake class definitions should have become real. 9184 assert(PendingFakeDefinitionData.empty() && 9185 "faked up a class definition but never saw the real one"); 9186 9187 // If we deserialized any C++ or Objective-C class definitions, any 9188 // Objective-C protocol definitions, or any redeclarable templates, make sure 9189 // that all redeclarations point to the definitions. Note that this can only 9190 // happen now, after the redeclaration chains have been fully wired. 9191 for (Decl *D : PendingDefinitions) { 9192 if (TagDecl *TD = dyn_cast<TagDecl>(D)) { 9193 if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) { 9194 // Make sure that the TagType points at the definition. 9195 const_cast<TagType*>(TagT)->decl = TD; 9196 } 9197 9198 if (auto RD = dyn_cast<CXXRecordDecl>(D)) { 9199 for (auto *R = getMostRecentExistingDecl(RD); R; 9200 R = R->getPreviousDecl()) { 9201 assert((R == D) == 9202 cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() && 9203 "declaration thinks it's the definition but it isn't"); 9204 cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData; 9205 } 9206 } 9207 9208 continue; 9209 } 9210 9211 if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) { 9212 // Make sure that the ObjCInterfaceType points at the definition. 9213 const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl)) 9214 ->Decl = ID; 9215 9216 for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl()) 9217 cast<ObjCInterfaceDecl>(R)->Data = ID->Data; 9218 9219 continue; 9220 } 9221 9222 if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) { 9223 for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl()) 9224 cast<ObjCProtocolDecl>(R)->Data = PD->Data; 9225 9226 continue; 9227 } 9228 9229 auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl(); 9230 for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl()) 9231 cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common; 9232 } 9233 PendingDefinitions.clear(); 9234 9235 // Load the bodies of any functions or methods we've encountered. We do 9236 // this now (delayed) so that we can be sure that the declaration chains 9237 // have been fully wired up (hasBody relies on this). 9238 // FIXME: We shouldn't require complete redeclaration chains here. 9239 for (PendingBodiesMap::iterator PB = PendingBodies.begin(), 9240 PBEnd = PendingBodies.end(); 9241 PB != PBEnd; ++PB) { 9242 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) { 9243 // For a function defined inline within a class template, force the 9244 // canonical definition to be the one inside the canonical definition of 9245 // the template. This ensures that we instantiate from a correct view 9246 // of the template. 9247 // 9248 // Sadly we can't do this more generally: we can't be sure that all 9249 // copies of an arbitrary class definition will have the same members 9250 // defined (eg, some member functions may not be instantiated, and some 9251 // special members may or may not have been implicitly defined). 9252 if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent())) 9253 if (RD->isDependentContext() && !RD->isThisDeclarationADefinition()) 9254 continue; 9255 9256 // FIXME: Check for =delete/=default? 9257 // FIXME: Complain about ODR violations here? 9258 const FunctionDecl *Defn = nullptr; 9259 if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) { 9260 FD->setLazyBody(PB->second); 9261 } else { 9262 auto *NonConstDefn = const_cast<FunctionDecl*>(Defn); 9263 mergeDefinitionVisibility(NonConstDefn, FD); 9264 9265 if (!FD->isLateTemplateParsed() && 9266 !NonConstDefn->isLateTemplateParsed() && 9267 FD->getODRHash() != NonConstDefn->getODRHash()) { 9268 if (!isa<CXXMethodDecl>(FD)) { 9269 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9270 } else if (FD->getLexicalParent()->isFileContext() && 9271 NonConstDefn->getLexicalParent()->isFileContext()) { 9272 // Only diagnose out-of-line method definitions. If they are 9273 // in class definitions, then an error will be generated when 9274 // processing the class bodies. 9275 PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn); 9276 } 9277 } 9278 } 9279 continue; 9280 } 9281 9282 ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first); 9283 if (!getContext().getLangOpts().Modules || !MD->hasBody()) 9284 MD->setLazyBody(PB->second); 9285 } 9286 PendingBodies.clear(); 9287 9288 // Do some cleanup. 9289 for (auto *ND : PendingMergedDefinitionsToDeduplicate) 9290 getContext().deduplicateMergedDefinitonsFor(ND); 9291 PendingMergedDefinitionsToDeduplicate.clear(); 9292 } 9293 9294 void ASTReader::diagnoseOdrViolations() { 9295 if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() && 9296 PendingFunctionOdrMergeFailures.empty() && 9297 PendingEnumOdrMergeFailures.empty()) 9298 return; 9299 9300 // Trigger the import of the full definition of each class that had any 9301 // odr-merging problems, so we can produce better diagnostics for them. 9302 // These updates may in turn find and diagnose some ODR failures, so take 9303 // ownership of the set first. 9304 auto OdrMergeFailures = std::move(PendingOdrMergeFailures); 9305 PendingOdrMergeFailures.clear(); 9306 for (auto &Merge : OdrMergeFailures) { 9307 Merge.first->buildLookup(); 9308 Merge.first->decls_begin(); 9309 Merge.first->bases_begin(); 9310 Merge.first->vbases_begin(); 9311 for (auto &RecordPair : Merge.second) { 9312 auto *RD = RecordPair.first; 9313 RD->decls_begin(); 9314 RD->bases_begin(); 9315 RD->vbases_begin(); 9316 } 9317 } 9318 9319 // Trigger the import of functions. 9320 auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures); 9321 PendingFunctionOdrMergeFailures.clear(); 9322 for (auto &Merge : FunctionOdrMergeFailures) { 9323 Merge.first->buildLookup(); 9324 Merge.first->decls_begin(); 9325 Merge.first->getBody(); 9326 for (auto &FD : Merge.second) { 9327 FD->buildLookup(); 9328 FD->decls_begin(); 9329 FD->getBody(); 9330 } 9331 } 9332 9333 // Trigger the import of enums. 9334 auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures); 9335 PendingEnumOdrMergeFailures.clear(); 9336 for (auto &Merge : EnumOdrMergeFailures) { 9337 Merge.first->decls_begin(); 9338 for (auto &Enum : Merge.second) { 9339 Enum->decls_begin(); 9340 } 9341 } 9342 9343 // For each declaration from a merged context, check that the canonical 9344 // definition of that context also contains a declaration of the same 9345 // entity. 9346 // 9347 // Caution: this loop does things that might invalidate iterators into 9348 // PendingOdrMergeChecks. Don't turn this into a range-based for loop! 9349 while (!PendingOdrMergeChecks.empty()) { 9350 NamedDecl *D = PendingOdrMergeChecks.pop_back_val(); 9351 9352 // FIXME: Skip over implicit declarations for now. This matters for things 9353 // like implicitly-declared special member functions. This isn't entirely 9354 // correct; we can end up with multiple unmerged declarations of the same 9355 // implicit entity. 9356 if (D->isImplicit()) 9357 continue; 9358 9359 DeclContext *CanonDef = D->getDeclContext(); 9360 9361 bool Found = false; 9362 const Decl *DCanon = D->getCanonicalDecl(); 9363 9364 for (auto RI : D->redecls()) { 9365 if (RI->getLexicalDeclContext() == CanonDef) { 9366 Found = true; 9367 break; 9368 } 9369 } 9370 if (Found) 9371 continue; 9372 9373 // Quick check failed, time to do the slow thing. Note, we can't just 9374 // look up the name of D in CanonDef here, because the member that is 9375 // in CanonDef might not be found by name lookup (it might have been 9376 // replaced by a more recent declaration in the lookup table), and we 9377 // can't necessarily find it in the redeclaration chain because it might 9378 // be merely mergeable, not redeclarable. 9379 llvm::SmallVector<const NamedDecl*, 4> Candidates; 9380 for (auto *CanonMember : CanonDef->decls()) { 9381 if (CanonMember->getCanonicalDecl() == DCanon) { 9382 // This can happen if the declaration is merely mergeable and not 9383 // actually redeclarable (we looked for redeclarations earlier). 9384 // 9385 // FIXME: We should be able to detect this more efficiently, without 9386 // pulling in all of the members of CanonDef. 9387 Found = true; 9388 break; 9389 } 9390 if (auto *ND = dyn_cast<NamedDecl>(CanonMember)) 9391 if (ND->getDeclName() == D->getDeclName()) 9392 Candidates.push_back(ND); 9393 } 9394 9395 if (!Found) { 9396 // The AST doesn't like TagDecls becoming invalid after they've been 9397 // completed. We only really need to mark FieldDecls as invalid here. 9398 if (!isa<TagDecl>(D)) 9399 D->setInvalidDecl(); 9400 9401 // Ensure we don't accidentally recursively enter deserialization while 9402 // we're producing our diagnostic. 9403 Deserializing RecursionGuard(this); 9404 9405 std::string CanonDefModule = 9406 getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef)); 9407 Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl) 9408 << D << getOwningModuleNameForDiagnostic(D) 9409 << CanonDef << CanonDefModule.empty() << CanonDefModule; 9410 9411 if (Candidates.empty()) 9412 Diag(cast<Decl>(CanonDef)->getLocation(), 9413 diag::note_module_odr_violation_no_possible_decls) << D; 9414 else { 9415 for (unsigned I = 0, N = Candidates.size(); I != N; ++I) 9416 Diag(Candidates[I]->getLocation(), 9417 diag::note_module_odr_violation_possible_decl) 9418 << Candidates[I]; 9419 } 9420 9421 DiagnosedOdrMergeFailures.insert(CanonDef); 9422 } 9423 } 9424 9425 if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() && 9426 EnumOdrMergeFailures.empty()) 9427 return; 9428 9429 // Ensure we don't accidentally recursively enter deserialization while 9430 // we're producing our diagnostics. 9431 Deserializing RecursionGuard(this); 9432 9433 // Common code for hashing helpers. 9434 ODRHash Hash; 9435 auto ComputeQualTypeODRHash = [&Hash](QualType Ty) { 9436 Hash.clear(); 9437 Hash.AddQualType(Ty); 9438 return Hash.CalculateHash(); 9439 }; 9440 9441 auto ComputeODRHash = [&Hash](const Stmt *S) { 9442 assert(S); 9443 Hash.clear(); 9444 Hash.AddStmt(S); 9445 return Hash.CalculateHash(); 9446 }; 9447 9448 auto ComputeSubDeclODRHash = [&Hash](const Decl *D) { 9449 assert(D); 9450 Hash.clear(); 9451 Hash.AddSubDecl(D); 9452 return Hash.CalculateHash(); 9453 }; 9454 9455 auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) { 9456 Hash.clear(); 9457 Hash.AddTemplateArgument(TA); 9458 return Hash.CalculateHash(); 9459 }; 9460 9461 auto ComputeTemplateParameterListODRHash = 9462 [&Hash](const TemplateParameterList *TPL) { 9463 assert(TPL); 9464 Hash.clear(); 9465 Hash.AddTemplateParameterList(TPL); 9466 return Hash.CalculateHash(); 9467 }; 9468 9469 // Used with err_module_odr_violation_mismatch_decl and 9470 // note_module_odr_violation_mismatch_decl 9471 // This list should be the same Decl's as in ODRHash::isWhiteListedDecl 9472 enum ODRMismatchDecl { 9473 EndOfClass, 9474 PublicSpecifer, 9475 PrivateSpecifer, 9476 ProtectedSpecifer, 9477 StaticAssert, 9478 Field, 9479 CXXMethod, 9480 TypeAlias, 9481 TypeDef, 9482 Var, 9483 Friend, 9484 FunctionTemplate, 9485 Other 9486 }; 9487 9488 // Used with err_module_odr_violation_mismatch_decl_diff and 9489 // note_module_odr_violation_mismatch_decl_diff 9490 enum ODRMismatchDeclDifference { 9491 StaticAssertCondition, 9492 StaticAssertMessage, 9493 StaticAssertOnlyMessage, 9494 FieldName, 9495 FieldTypeName, 9496 FieldSingleBitField, 9497 FieldDifferentWidthBitField, 9498 FieldSingleMutable, 9499 FieldSingleInitializer, 9500 FieldDifferentInitializers, 9501 MethodName, 9502 MethodDeleted, 9503 MethodDefaulted, 9504 MethodVirtual, 9505 MethodStatic, 9506 MethodVolatile, 9507 MethodConst, 9508 MethodInline, 9509 MethodNumberParameters, 9510 MethodParameterType, 9511 MethodParameterName, 9512 MethodParameterSingleDefaultArgument, 9513 MethodParameterDifferentDefaultArgument, 9514 MethodNoTemplateArguments, 9515 MethodDifferentNumberTemplateArguments, 9516 MethodDifferentTemplateArgument, 9517 MethodSingleBody, 9518 MethodDifferentBody, 9519 TypedefName, 9520 TypedefType, 9521 VarName, 9522 VarType, 9523 VarSingleInitializer, 9524 VarDifferentInitializer, 9525 VarConstexpr, 9526 FriendTypeFunction, 9527 FriendType, 9528 FriendFunction, 9529 FunctionTemplateDifferentNumberParameters, 9530 FunctionTemplateParameterDifferentKind, 9531 FunctionTemplateParameterName, 9532 FunctionTemplateParameterSingleDefaultArgument, 9533 FunctionTemplateParameterDifferentDefaultArgument, 9534 FunctionTemplateParameterDifferentType, 9535 FunctionTemplatePackParameter, 9536 }; 9537 9538 // These lambdas have the common portions of the ODR diagnostics. This 9539 // has the same return as Diag(), so addition parameters can be passed 9540 // in with operator<< 9541 auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule, 9542 SourceLocation Loc, SourceRange Range, 9543 ODRMismatchDeclDifference DiffType) { 9544 return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff) 9545 << FirstRecord << FirstModule.empty() << FirstModule << Range 9546 << DiffType; 9547 }; 9548 auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc, 9549 SourceRange Range, ODRMismatchDeclDifference DiffType) { 9550 return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff) 9551 << SecondModule << Range << DiffType; 9552 }; 9553 9554 auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote, 9555 &ComputeQualTypeODRHash, &ComputeODRHash]( 9556 NamedDecl *FirstRecord, StringRef FirstModule, 9557 StringRef SecondModule, FieldDecl *FirstField, 9558 FieldDecl *SecondField) { 9559 IdentifierInfo *FirstII = FirstField->getIdentifier(); 9560 IdentifierInfo *SecondII = SecondField->getIdentifier(); 9561 if (FirstII->getName() != SecondII->getName()) { 9562 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9563 FirstField->getSourceRange(), FieldName) 9564 << FirstII; 9565 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9566 SecondField->getSourceRange(), FieldName) 9567 << SecondII; 9568 9569 return true; 9570 } 9571 9572 assert(getContext().hasSameType(FirstField->getType(), 9573 SecondField->getType())); 9574 9575 QualType FirstType = FirstField->getType(); 9576 QualType SecondType = SecondField->getType(); 9577 if (ComputeQualTypeODRHash(FirstType) != 9578 ComputeQualTypeODRHash(SecondType)) { 9579 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9580 FirstField->getSourceRange(), FieldTypeName) 9581 << FirstII << FirstType; 9582 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9583 SecondField->getSourceRange(), FieldTypeName) 9584 << SecondII << SecondType; 9585 9586 return true; 9587 } 9588 9589 const bool IsFirstBitField = FirstField->isBitField(); 9590 const bool IsSecondBitField = SecondField->isBitField(); 9591 if (IsFirstBitField != IsSecondBitField) { 9592 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9593 FirstField->getSourceRange(), FieldSingleBitField) 9594 << FirstII << IsFirstBitField; 9595 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9596 SecondField->getSourceRange(), FieldSingleBitField) 9597 << SecondII << IsSecondBitField; 9598 return true; 9599 } 9600 9601 if (IsFirstBitField && IsSecondBitField) { 9602 unsigned FirstBitWidthHash = 9603 ComputeODRHash(FirstField->getBitWidth()); 9604 unsigned SecondBitWidthHash = 9605 ComputeODRHash(SecondField->getBitWidth()); 9606 if (FirstBitWidthHash != SecondBitWidthHash) { 9607 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9608 FirstField->getSourceRange(), 9609 FieldDifferentWidthBitField) 9610 << FirstII << FirstField->getBitWidth()->getSourceRange(); 9611 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9612 SecondField->getSourceRange(), 9613 FieldDifferentWidthBitField) 9614 << SecondII << SecondField->getBitWidth()->getSourceRange(); 9615 return true; 9616 } 9617 } 9618 9619 if (!PP.getLangOpts().CPlusPlus) 9620 return false; 9621 9622 const bool IsFirstMutable = FirstField->isMutable(); 9623 const bool IsSecondMutable = SecondField->isMutable(); 9624 if (IsFirstMutable != IsSecondMutable) { 9625 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9626 FirstField->getSourceRange(), FieldSingleMutable) 9627 << FirstII << IsFirstMutable; 9628 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9629 SecondField->getSourceRange(), FieldSingleMutable) 9630 << SecondII << IsSecondMutable; 9631 return true; 9632 } 9633 9634 const Expr *FirstInitializer = FirstField->getInClassInitializer(); 9635 const Expr *SecondInitializer = SecondField->getInClassInitializer(); 9636 if ((!FirstInitializer && SecondInitializer) || 9637 (FirstInitializer && !SecondInitializer)) { 9638 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9639 FirstField->getSourceRange(), FieldSingleInitializer) 9640 << FirstII << (FirstInitializer != nullptr); 9641 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9642 SecondField->getSourceRange(), FieldSingleInitializer) 9643 << SecondII << (SecondInitializer != nullptr); 9644 return true; 9645 } 9646 9647 if (FirstInitializer && SecondInitializer) { 9648 unsigned FirstInitHash = ComputeODRHash(FirstInitializer); 9649 unsigned SecondInitHash = ComputeODRHash(SecondInitializer); 9650 if (FirstInitHash != SecondInitHash) { 9651 ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(), 9652 FirstField->getSourceRange(), 9653 FieldDifferentInitializers) 9654 << FirstII << FirstInitializer->getSourceRange(); 9655 ODRDiagDeclNote(SecondModule, SecondField->getLocation(), 9656 SecondField->getSourceRange(), 9657 FieldDifferentInitializers) 9658 << SecondII << SecondInitializer->getSourceRange(); 9659 return true; 9660 } 9661 } 9662 9663 return false; 9664 }; 9665 9666 auto ODRDiagTypeDefOrAlias = 9667 [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash]( 9668 NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule, 9669 TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD, 9670 bool IsTypeAlias) { 9671 auto FirstName = FirstTD->getDeclName(); 9672 auto SecondName = SecondTD->getDeclName(); 9673 if (FirstName != SecondName) { 9674 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9675 FirstTD->getSourceRange(), TypedefName) 9676 << IsTypeAlias << FirstName; 9677 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9678 SecondTD->getSourceRange(), TypedefName) 9679 << IsTypeAlias << SecondName; 9680 return true; 9681 } 9682 9683 QualType FirstType = FirstTD->getUnderlyingType(); 9684 QualType SecondType = SecondTD->getUnderlyingType(); 9685 if (ComputeQualTypeODRHash(FirstType) != 9686 ComputeQualTypeODRHash(SecondType)) { 9687 ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(), 9688 FirstTD->getSourceRange(), TypedefType) 9689 << IsTypeAlias << FirstName << FirstType; 9690 ODRDiagDeclNote(SecondModule, SecondTD->getLocation(), 9691 SecondTD->getSourceRange(), TypedefType) 9692 << IsTypeAlias << SecondName << SecondType; 9693 return true; 9694 } 9695 9696 return false; 9697 }; 9698 9699 auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote, 9700 &ComputeQualTypeODRHash, &ComputeODRHash, 9701 this](NamedDecl *FirstRecord, StringRef FirstModule, 9702 StringRef SecondModule, VarDecl *FirstVD, 9703 VarDecl *SecondVD) { 9704 auto FirstName = FirstVD->getDeclName(); 9705 auto SecondName = SecondVD->getDeclName(); 9706 if (FirstName != SecondName) { 9707 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9708 FirstVD->getSourceRange(), VarName) 9709 << FirstName; 9710 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9711 SecondVD->getSourceRange(), VarName) 9712 << SecondName; 9713 return true; 9714 } 9715 9716 QualType FirstType = FirstVD->getType(); 9717 QualType SecondType = SecondVD->getType(); 9718 if (ComputeQualTypeODRHash(FirstType) != 9719 ComputeQualTypeODRHash(SecondType)) { 9720 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9721 FirstVD->getSourceRange(), VarType) 9722 << FirstName << FirstType; 9723 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9724 SecondVD->getSourceRange(), VarType) 9725 << SecondName << SecondType; 9726 return true; 9727 } 9728 9729 if (!PP.getLangOpts().CPlusPlus) 9730 return false; 9731 9732 const Expr *FirstInit = FirstVD->getInit(); 9733 const Expr *SecondInit = SecondVD->getInit(); 9734 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 9735 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9736 FirstVD->getSourceRange(), VarSingleInitializer) 9737 << FirstName << (FirstInit == nullptr) 9738 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 9739 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9740 SecondVD->getSourceRange(), VarSingleInitializer) 9741 << SecondName << (SecondInit == nullptr) 9742 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 9743 return true; 9744 } 9745 9746 if (FirstInit && SecondInit && 9747 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 9748 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9749 FirstVD->getSourceRange(), VarDifferentInitializer) 9750 << FirstName << FirstInit->getSourceRange(); 9751 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9752 SecondVD->getSourceRange(), VarDifferentInitializer) 9753 << SecondName << SecondInit->getSourceRange(); 9754 return true; 9755 } 9756 9757 const bool FirstIsConstexpr = FirstVD->isConstexpr(); 9758 const bool SecondIsConstexpr = SecondVD->isConstexpr(); 9759 if (FirstIsConstexpr != SecondIsConstexpr) { 9760 ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(), 9761 FirstVD->getSourceRange(), VarConstexpr) 9762 << FirstName << FirstIsConstexpr; 9763 ODRDiagDeclNote(SecondModule, SecondVD->getLocation(), 9764 SecondVD->getSourceRange(), VarConstexpr) 9765 << SecondName << SecondIsConstexpr; 9766 return true; 9767 } 9768 return false; 9769 }; 9770 9771 auto DifferenceSelector = [](Decl *D) { 9772 assert(D && "valid Decl required"); 9773 switch (D->getKind()) { 9774 default: 9775 return Other; 9776 case Decl::AccessSpec: 9777 switch (D->getAccess()) { 9778 case AS_public: 9779 return PublicSpecifer; 9780 case AS_private: 9781 return PrivateSpecifer; 9782 case AS_protected: 9783 return ProtectedSpecifer; 9784 case AS_none: 9785 break; 9786 } 9787 llvm_unreachable("Invalid access specifier"); 9788 case Decl::StaticAssert: 9789 return StaticAssert; 9790 case Decl::Field: 9791 return Field; 9792 case Decl::CXXMethod: 9793 case Decl::CXXConstructor: 9794 case Decl::CXXDestructor: 9795 return CXXMethod; 9796 case Decl::TypeAlias: 9797 return TypeAlias; 9798 case Decl::Typedef: 9799 return TypeDef; 9800 case Decl::Var: 9801 return Var; 9802 case Decl::Friend: 9803 return Friend; 9804 case Decl::FunctionTemplate: 9805 return FunctionTemplate; 9806 } 9807 }; 9808 9809 using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>; 9810 auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes, 9811 RecordDecl *Record, 9812 const DeclContext *DC) { 9813 for (auto *D : Record->decls()) { 9814 if (!ODRHash::isWhitelistedDecl(D, DC)) 9815 continue; 9816 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 9817 } 9818 }; 9819 9820 struct DiffResult { 9821 Decl *FirstDecl = nullptr, *SecondDecl = nullptr; 9822 ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other; 9823 }; 9824 9825 // If there is a diagnoseable difference, FirstDiffType and 9826 // SecondDiffType will not be Other and FirstDecl and SecondDecl will be 9827 // filled in if not EndOfClass. 9828 auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes, 9829 DeclHashes &SecondHashes) { 9830 DiffResult DR; 9831 auto FirstIt = FirstHashes.begin(); 9832 auto SecondIt = SecondHashes.begin(); 9833 while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) { 9834 if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() && 9835 FirstIt->second == SecondIt->second) { 9836 ++FirstIt; 9837 ++SecondIt; 9838 continue; 9839 } 9840 9841 DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first; 9842 DR.SecondDecl = 9843 SecondIt == SecondHashes.end() ? nullptr : SecondIt->first; 9844 9845 DR.FirstDiffType = 9846 DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass; 9847 DR.SecondDiffType = 9848 DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass; 9849 return DR; 9850 } 9851 return DR; 9852 }; 9853 9854 // Use this to diagnose that an unexpected Decl was encountered 9855 // or no difference was detected. This causes a generic error 9856 // message to be emitted. 9857 auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord, 9858 StringRef FirstModule, 9859 NamedDecl *SecondRecord, 9860 StringRef SecondModule) { 9861 Diag(FirstRecord->getLocation(), 9862 diag::err_module_odr_violation_different_definitions) 9863 << FirstRecord << FirstModule.empty() << FirstModule; 9864 9865 if (DR.FirstDecl) { 9866 Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference) 9867 << FirstRecord << DR.FirstDecl->getSourceRange(); 9868 } 9869 9870 Diag(SecondRecord->getLocation(), 9871 diag::note_module_odr_violation_different_definitions) 9872 << SecondModule; 9873 9874 if (DR.SecondDecl) { 9875 Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference) 9876 << DR.SecondDecl->getSourceRange(); 9877 } 9878 }; 9879 9880 auto DiagnoseODRMismatch = 9881 [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule, 9882 NamedDecl *SecondRecord, StringRef SecondModule) { 9883 SourceLocation FirstLoc; 9884 SourceRange FirstRange; 9885 auto *FirstTag = dyn_cast<TagDecl>(FirstRecord); 9886 if (DR.FirstDiffType == EndOfClass && FirstTag) { 9887 FirstLoc = FirstTag->getBraceRange().getEnd(); 9888 } else { 9889 FirstLoc = DR.FirstDecl->getLocation(); 9890 FirstRange = DR.FirstDecl->getSourceRange(); 9891 } 9892 Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl) 9893 << FirstRecord << FirstModule.empty() << FirstModule << FirstRange 9894 << DR.FirstDiffType; 9895 9896 SourceLocation SecondLoc; 9897 SourceRange SecondRange; 9898 auto *SecondTag = dyn_cast<TagDecl>(SecondRecord); 9899 if (DR.SecondDiffType == EndOfClass && SecondTag) { 9900 SecondLoc = SecondTag->getBraceRange().getEnd(); 9901 } else { 9902 SecondLoc = DR.SecondDecl->getLocation(); 9903 SecondRange = DR.SecondDecl->getSourceRange(); 9904 } 9905 Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl) 9906 << SecondModule << SecondRange << DR.SecondDiffType; 9907 }; 9908 9909 // Issue any pending ODR-failure diagnostics. 9910 for (auto &Merge : OdrMergeFailures) { 9911 // If we've already pointed out a specific problem with this class, don't 9912 // bother issuing a general "something's different" diagnostic. 9913 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 9914 continue; 9915 9916 bool Diagnosed = false; 9917 CXXRecordDecl *FirstRecord = Merge.first; 9918 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord); 9919 for (auto &RecordPair : Merge.second) { 9920 CXXRecordDecl *SecondRecord = RecordPair.first; 9921 // Multiple different declarations got merged together; tell the user 9922 // where they came from. 9923 if (FirstRecord == SecondRecord) 9924 continue; 9925 9926 std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord); 9927 9928 auto *FirstDD = FirstRecord->DefinitionData; 9929 auto *SecondDD = RecordPair.second; 9930 9931 assert(FirstDD && SecondDD && "Definitions without DefinitionData"); 9932 9933 // Diagnostics from DefinitionData are emitted here. 9934 if (FirstDD != SecondDD) { 9935 enum ODRDefinitionDataDifference { 9936 NumBases, 9937 NumVBases, 9938 BaseType, 9939 BaseVirtual, 9940 BaseAccess, 9941 }; 9942 auto ODRDiagBaseError = [FirstRecord, &FirstModule, 9943 this](SourceLocation Loc, SourceRange Range, 9944 ODRDefinitionDataDifference DiffType) { 9945 return Diag(Loc, diag::err_module_odr_violation_definition_data) 9946 << FirstRecord << FirstModule.empty() << FirstModule << Range 9947 << DiffType; 9948 }; 9949 auto ODRDiagBaseNote = [&SecondModule, 9950 this](SourceLocation Loc, SourceRange Range, 9951 ODRDefinitionDataDifference DiffType) { 9952 return Diag(Loc, diag::note_module_odr_violation_definition_data) 9953 << SecondModule << Range << DiffType; 9954 }; 9955 9956 unsigned FirstNumBases = FirstDD->NumBases; 9957 unsigned FirstNumVBases = FirstDD->NumVBases; 9958 unsigned SecondNumBases = SecondDD->NumBases; 9959 unsigned SecondNumVBases = SecondDD->NumVBases; 9960 9961 auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) { 9962 unsigned NumBases = DD->NumBases; 9963 if (NumBases == 0) return SourceRange(); 9964 auto bases = DD->bases(); 9965 return SourceRange(bases[0].getBeginLoc(), 9966 bases[NumBases - 1].getEndLoc()); 9967 }; 9968 9969 if (FirstNumBases != SecondNumBases) { 9970 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 9971 NumBases) 9972 << FirstNumBases; 9973 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 9974 NumBases) 9975 << SecondNumBases; 9976 Diagnosed = true; 9977 break; 9978 } 9979 9980 if (FirstNumVBases != SecondNumVBases) { 9981 ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD), 9982 NumVBases) 9983 << FirstNumVBases; 9984 ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD), 9985 NumVBases) 9986 << SecondNumVBases; 9987 Diagnosed = true; 9988 break; 9989 } 9990 9991 auto FirstBases = FirstDD->bases(); 9992 auto SecondBases = SecondDD->bases(); 9993 unsigned i = 0; 9994 for (i = 0; i < FirstNumBases; ++i) { 9995 auto FirstBase = FirstBases[i]; 9996 auto SecondBase = SecondBases[i]; 9997 if (ComputeQualTypeODRHash(FirstBase.getType()) != 9998 ComputeQualTypeODRHash(SecondBase.getType())) { 9999 ODRDiagBaseError(FirstRecord->getLocation(), 10000 FirstBase.getSourceRange(), BaseType) 10001 << (i + 1) << FirstBase.getType(); 10002 ODRDiagBaseNote(SecondRecord->getLocation(), 10003 SecondBase.getSourceRange(), BaseType) 10004 << (i + 1) << SecondBase.getType(); 10005 break; 10006 } 10007 10008 if (FirstBase.isVirtual() != SecondBase.isVirtual()) { 10009 ODRDiagBaseError(FirstRecord->getLocation(), 10010 FirstBase.getSourceRange(), BaseVirtual) 10011 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType(); 10012 ODRDiagBaseNote(SecondRecord->getLocation(), 10013 SecondBase.getSourceRange(), BaseVirtual) 10014 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType(); 10015 break; 10016 } 10017 10018 if (FirstBase.getAccessSpecifierAsWritten() != 10019 SecondBase.getAccessSpecifierAsWritten()) { 10020 ODRDiagBaseError(FirstRecord->getLocation(), 10021 FirstBase.getSourceRange(), BaseAccess) 10022 << (i + 1) << FirstBase.getType() 10023 << (int)FirstBase.getAccessSpecifierAsWritten(); 10024 ODRDiagBaseNote(SecondRecord->getLocation(), 10025 SecondBase.getSourceRange(), BaseAccess) 10026 << (i + 1) << SecondBase.getType() 10027 << (int)SecondBase.getAccessSpecifierAsWritten(); 10028 break; 10029 } 10030 } 10031 10032 if (i != FirstNumBases) { 10033 Diagnosed = true; 10034 break; 10035 } 10036 } 10037 10038 const ClassTemplateDecl *FirstTemplate = 10039 FirstRecord->getDescribedClassTemplate(); 10040 const ClassTemplateDecl *SecondTemplate = 10041 SecondRecord->getDescribedClassTemplate(); 10042 10043 assert(!FirstTemplate == !SecondTemplate && 10044 "Both pointers should be null or non-null"); 10045 10046 enum ODRTemplateDifference { 10047 ParamEmptyName, 10048 ParamName, 10049 ParamSingleDefaultArgument, 10050 ParamDifferentDefaultArgument, 10051 }; 10052 10053 if (FirstTemplate && SecondTemplate) { 10054 DeclHashes FirstTemplateHashes; 10055 DeclHashes SecondTemplateHashes; 10056 10057 auto PopulateTemplateParameterHashs = 10058 [&ComputeSubDeclODRHash](DeclHashes &Hashes, 10059 const ClassTemplateDecl *TD) { 10060 for (auto *D : TD->getTemplateParameters()->asArray()) { 10061 Hashes.emplace_back(D, ComputeSubDeclODRHash(D)); 10062 } 10063 }; 10064 10065 PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate); 10066 PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate); 10067 10068 assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() && 10069 "Number of template parameters should be equal."); 10070 10071 auto FirstIt = FirstTemplateHashes.begin(); 10072 auto FirstEnd = FirstTemplateHashes.end(); 10073 auto SecondIt = SecondTemplateHashes.begin(); 10074 for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) { 10075 if (FirstIt->second == SecondIt->second) 10076 continue; 10077 10078 auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this]( 10079 SourceLocation Loc, SourceRange Range, 10080 ODRTemplateDifference DiffType) { 10081 return Diag(Loc, diag::err_module_odr_violation_template_parameter) 10082 << FirstRecord << FirstModule.empty() << FirstModule << Range 10083 << DiffType; 10084 }; 10085 auto ODRDiagTemplateNote = [&SecondModule, this]( 10086 SourceLocation Loc, SourceRange Range, 10087 ODRTemplateDifference DiffType) { 10088 return Diag(Loc, diag::note_module_odr_violation_template_parameter) 10089 << SecondModule << Range << DiffType; 10090 }; 10091 10092 const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first); 10093 const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first); 10094 10095 assert(FirstDecl->getKind() == SecondDecl->getKind() && 10096 "Parameter Decl's should be the same kind."); 10097 10098 DeclarationName FirstName = FirstDecl->getDeclName(); 10099 DeclarationName SecondName = SecondDecl->getDeclName(); 10100 10101 if (FirstName != SecondName) { 10102 const bool FirstNameEmpty = 10103 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo(); 10104 const bool SecondNameEmpty = 10105 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo(); 10106 assert((!FirstNameEmpty || !SecondNameEmpty) && 10107 "Both template parameters cannot be unnamed."); 10108 ODRDiagTemplateError(FirstDecl->getLocation(), 10109 FirstDecl->getSourceRange(), 10110 FirstNameEmpty ? ParamEmptyName : ParamName) 10111 << FirstName; 10112 ODRDiagTemplateNote(SecondDecl->getLocation(), 10113 SecondDecl->getSourceRange(), 10114 SecondNameEmpty ? ParamEmptyName : ParamName) 10115 << SecondName; 10116 break; 10117 } 10118 10119 switch (FirstDecl->getKind()) { 10120 default: 10121 llvm_unreachable("Invalid template parameter type."); 10122 case Decl::TemplateTypeParm: { 10123 const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl); 10124 const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl); 10125 const bool HasFirstDefaultArgument = 10126 FirstParam->hasDefaultArgument() && 10127 !FirstParam->defaultArgumentWasInherited(); 10128 const bool HasSecondDefaultArgument = 10129 SecondParam->hasDefaultArgument() && 10130 !SecondParam->defaultArgumentWasInherited(); 10131 10132 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10133 ODRDiagTemplateError(FirstDecl->getLocation(), 10134 FirstDecl->getSourceRange(), 10135 ParamSingleDefaultArgument) 10136 << HasFirstDefaultArgument; 10137 ODRDiagTemplateNote(SecondDecl->getLocation(), 10138 SecondDecl->getSourceRange(), 10139 ParamSingleDefaultArgument) 10140 << HasSecondDefaultArgument; 10141 break; 10142 } 10143 10144 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10145 "Expecting default arguments."); 10146 10147 ODRDiagTemplateError(FirstDecl->getLocation(), 10148 FirstDecl->getSourceRange(), 10149 ParamDifferentDefaultArgument); 10150 ODRDiagTemplateNote(SecondDecl->getLocation(), 10151 SecondDecl->getSourceRange(), 10152 ParamDifferentDefaultArgument); 10153 10154 break; 10155 } 10156 case Decl::NonTypeTemplateParm: { 10157 const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl); 10158 const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl); 10159 const bool HasFirstDefaultArgument = 10160 FirstParam->hasDefaultArgument() && 10161 !FirstParam->defaultArgumentWasInherited(); 10162 const bool HasSecondDefaultArgument = 10163 SecondParam->hasDefaultArgument() && 10164 !SecondParam->defaultArgumentWasInherited(); 10165 10166 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10167 ODRDiagTemplateError(FirstDecl->getLocation(), 10168 FirstDecl->getSourceRange(), 10169 ParamSingleDefaultArgument) 10170 << HasFirstDefaultArgument; 10171 ODRDiagTemplateNote(SecondDecl->getLocation(), 10172 SecondDecl->getSourceRange(), 10173 ParamSingleDefaultArgument) 10174 << HasSecondDefaultArgument; 10175 break; 10176 } 10177 10178 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10179 "Expecting default arguments."); 10180 10181 ODRDiagTemplateError(FirstDecl->getLocation(), 10182 FirstDecl->getSourceRange(), 10183 ParamDifferentDefaultArgument); 10184 ODRDiagTemplateNote(SecondDecl->getLocation(), 10185 SecondDecl->getSourceRange(), 10186 ParamDifferentDefaultArgument); 10187 10188 break; 10189 } 10190 case Decl::TemplateTemplateParm: { 10191 const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl); 10192 const auto *SecondParam = 10193 cast<TemplateTemplateParmDecl>(SecondDecl); 10194 const bool HasFirstDefaultArgument = 10195 FirstParam->hasDefaultArgument() && 10196 !FirstParam->defaultArgumentWasInherited(); 10197 const bool HasSecondDefaultArgument = 10198 SecondParam->hasDefaultArgument() && 10199 !SecondParam->defaultArgumentWasInherited(); 10200 10201 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10202 ODRDiagTemplateError(FirstDecl->getLocation(), 10203 FirstDecl->getSourceRange(), 10204 ParamSingleDefaultArgument) 10205 << HasFirstDefaultArgument; 10206 ODRDiagTemplateNote(SecondDecl->getLocation(), 10207 SecondDecl->getSourceRange(), 10208 ParamSingleDefaultArgument) 10209 << HasSecondDefaultArgument; 10210 break; 10211 } 10212 10213 assert(HasFirstDefaultArgument && HasSecondDefaultArgument && 10214 "Expecting default arguments."); 10215 10216 ODRDiagTemplateError(FirstDecl->getLocation(), 10217 FirstDecl->getSourceRange(), 10218 ParamDifferentDefaultArgument); 10219 ODRDiagTemplateNote(SecondDecl->getLocation(), 10220 SecondDecl->getSourceRange(), 10221 ParamDifferentDefaultArgument); 10222 10223 break; 10224 } 10225 } 10226 10227 break; 10228 } 10229 10230 if (FirstIt != FirstEnd) { 10231 Diagnosed = true; 10232 break; 10233 } 10234 } 10235 10236 DeclHashes FirstHashes; 10237 DeclHashes SecondHashes; 10238 const DeclContext *DC = FirstRecord; 10239 PopulateHashes(FirstHashes, FirstRecord, DC); 10240 PopulateHashes(SecondHashes, SecondRecord, DC); 10241 10242 auto DR = FindTypeDiffs(FirstHashes, SecondHashes); 10243 ODRMismatchDecl FirstDiffType = DR.FirstDiffType; 10244 ODRMismatchDecl SecondDiffType = DR.SecondDiffType; 10245 Decl *FirstDecl = DR.FirstDecl; 10246 Decl *SecondDecl = DR.SecondDecl; 10247 10248 if (FirstDiffType == Other || SecondDiffType == Other) { 10249 DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord, 10250 SecondModule); 10251 Diagnosed = true; 10252 break; 10253 } 10254 10255 if (FirstDiffType != SecondDiffType) { 10256 DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord, 10257 SecondModule); 10258 Diagnosed = true; 10259 break; 10260 } 10261 10262 assert(FirstDiffType == SecondDiffType); 10263 10264 switch (FirstDiffType) { 10265 case Other: 10266 case EndOfClass: 10267 case PublicSpecifer: 10268 case PrivateSpecifer: 10269 case ProtectedSpecifer: 10270 llvm_unreachable("Invalid diff type"); 10271 10272 case StaticAssert: { 10273 StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl); 10274 StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl); 10275 10276 Expr *FirstExpr = FirstSA->getAssertExpr(); 10277 Expr *SecondExpr = SecondSA->getAssertExpr(); 10278 unsigned FirstODRHash = ComputeODRHash(FirstExpr); 10279 unsigned SecondODRHash = ComputeODRHash(SecondExpr); 10280 if (FirstODRHash != SecondODRHash) { 10281 ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(), 10282 FirstExpr->getSourceRange(), StaticAssertCondition); 10283 ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(), 10284 SecondExpr->getSourceRange(), StaticAssertCondition); 10285 Diagnosed = true; 10286 break; 10287 } 10288 10289 StringLiteral *FirstStr = FirstSA->getMessage(); 10290 StringLiteral *SecondStr = SecondSA->getMessage(); 10291 assert((FirstStr || SecondStr) && "Both messages cannot be empty"); 10292 if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) { 10293 SourceLocation FirstLoc, SecondLoc; 10294 SourceRange FirstRange, SecondRange; 10295 if (FirstStr) { 10296 FirstLoc = FirstStr->getBeginLoc(); 10297 FirstRange = FirstStr->getSourceRange(); 10298 } else { 10299 FirstLoc = FirstSA->getBeginLoc(); 10300 FirstRange = FirstSA->getSourceRange(); 10301 } 10302 if (SecondStr) { 10303 SecondLoc = SecondStr->getBeginLoc(); 10304 SecondRange = SecondStr->getSourceRange(); 10305 } else { 10306 SecondLoc = SecondSA->getBeginLoc(); 10307 SecondRange = SecondSA->getSourceRange(); 10308 } 10309 ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange, 10310 StaticAssertOnlyMessage) 10311 << (FirstStr == nullptr); 10312 ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange, 10313 StaticAssertOnlyMessage) 10314 << (SecondStr == nullptr); 10315 Diagnosed = true; 10316 break; 10317 } 10318 10319 if (FirstStr && SecondStr && 10320 FirstStr->getString() != SecondStr->getString()) { 10321 ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(), 10322 FirstStr->getSourceRange(), StaticAssertMessage); 10323 ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(), 10324 SecondStr->getSourceRange(), StaticAssertMessage); 10325 Diagnosed = true; 10326 break; 10327 } 10328 break; 10329 } 10330 case Field: { 10331 Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule, 10332 cast<FieldDecl>(FirstDecl), 10333 cast<FieldDecl>(SecondDecl)); 10334 break; 10335 } 10336 case CXXMethod: { 10337 enum { 10338 DiagMethod, 10339 DiagConstructor, 10340 DiagDestructor, 10341 } FirstMethodType, 10342 SecondMethodType; 10343 auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) { 10344 if (isa<CXXConstructorDecl>(D)) return DiagConstructor; 10345 if (isa<CXXDestructorDecl>(D)) return DiagDestructor; 10346 return DiagMethod; 10347 }; 10348 const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl); 10349 const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl); 10350 FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod); 10351 SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod); 10352 auto FirstName = FirstMethod->getDeclName(); 10353 auto SecondName = SecondMethod->getDeclName(); 10354 if (FirstMethodType != SecondMethodType || FirstName != SecondName) { 10355 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10356 FirstMethod->getSourceRange(), MethodName) 10357 << FirstMethodType << FirstName; 10358 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10359 SecondMethod->getSourceRange(), MethodName) 10360 << SecondMethodType << SecondName; 10361 10362 Diagnosed = true; 10363 break; 10364 } 10365 10366 const bool FirstDeleted = FirstMethod->isDeletedAsWritten(); 10367 const bool SecondDeleted = SecondMethod->isDeletedAsWritten(); 10368 if (FirstDeleted != SecondDeleted) { 10369 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10370 FirstMethod->getSourceRange(), MethodDeleted) 10371 << FirstMethodType << FirstName << FirstDeleted; 10372 10373 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10374 SecondMethod->getSourceRange(), MethodDeleted) 10375 << SecondMethodType << SecondName << SecondDeleted; 10376 Diagnosed = true; 10377 break; 10378 } 10379 10380 const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted(); 10381 const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted(); 10382 if (FirstDefaulted != SecondDefaulted) { 10383 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10384 FirstMethod->getSourceRange(), MethodDefaulted) 10385 << FirstMethodType << FirstName << FirstDefaulted; 10386 10387 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10388 SecondMethod->getSourceRange(), MethodDefaulted) 10389 << SecondMethodType << SecondName << SecondDefaulted; 10390 Diagnosed = true; 10391 break; 10392 } 10393 10394 const bool FirstVirtual = FirstMethod->isVirtualAsWritten(); 10395 const bool SecondVirtual = SecondMethod->isVirtualAsWritten(); 10396 const bool FirstPure = FirstMethod->isPure(); 10397 const bool SecondPure = SecondMethod->isPure(); 10398 if ((FirstVirtual || SecondVirtual) && 10399 (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) { 10400 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10401 FirstMethod->getSourceRange(), MethodVirtual) 10402 << FirstMethodType << FirstName << FirstPure << FirstVirtual; 10403 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10404 SecondMethod->getSourceRange(), MethodVirtual) 10405 << SecondMethodType << SecondName << SecondPure << SecondVirtual; 10406 Diagnosed = true; 10407 break; 10408 } 10409 10410 // CXXMethodDecl::isStatic uses the canonical Decl. With Decl merging, 10411 // FirstDecl is the canonical Decl of SecondDecl, so the storage 10412 // class needs to be checked instead. 10413 const auto FirstStorage = FirstMethod->getStorageClass(); 10414 const auto SecondStorage = SecondMethod->getStorageClass(); 10415 const bool FirstStatic = FirstStorage == SC_Static; 10416 const bool SecondStatic = SecondStorage == SC_Static; 10417 if (FirstStatic != SecondStatic) { 10418 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10419 FirstMethod->getSourceRange(), MethodStatic) 10420 << FirstMethodType << FirstName << FirstStatic; 10421 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10422 SecondMethod->getSourceRange(), MethodStatic) 10423 << SecondMethodType << SecondName << SecondStatic; 10424 Diagnosed = true; 10425 break; 10426 } 10427 10428 const bool FirstVolatile = FirstMethod->isVolatile(); 10429 const bool SecondVolatile = SecondMethod->isVolatile(); 10430 if (FirstVolatile != SecondVolatile) { 10431 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10432 FirstMethod->getSourceRange(), MethodVolatile) 10433 << FirstMethodType << FirstName << FirstVolatile; 10434 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10435 SecondMethod->getSourceRange(), MethodVolatile) 10436 << SecondMethodType << SecondName << SecondVolatile; 10437 Diagnosed = true; 10438 break; 10439 } 10440 10441 const bool FirstConst = FirstMethod->isConst(); 10442 const bool SecondConst = SecondMethod->isConst(); 10443 if (FirstConst != SecondConst) { 10444 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10445 FirstMethod->getSourceRange(), MethodConst) 10446 << FirstMethodType << FirstName << FirstConst; 10447 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10448 SecondMethod->getSourceRange(), MethodConst) 10449 << SecondMethodType << SecondName << SecondConst; 10450 Diagnosed = true; 10451 break; 10452 } 10453 10454 const bool FirstInline = FirstMethod->isInlineSpecified(); 10455 const bool SecondInline = SecondMethod->isInlineSpecified(); 10456 if (FirstInline != SecondInline) { 10457 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10458 FirstMethod->getSourceRange(), MethodInline) 10459 << FirstMethodType << FirstName << FirstInline; 10460 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10461 SecondMethod->getSourceRange(), MethodInline) 10462 << SecondMethodType << SecondName << SecondInline; 10463 Diagnosed = true; 10464 break; 10465 } 10466 10467 const unsigned FirstNumParameters = FirstMethod->param_size(); 10468 const unsigned SecondNumParameters = SecondMethod->param_size(); 10469 if (FirstNumParameters != SecondNumParameters) { 10470 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10471 FirstMethod->getSourceRange(), 10472 MethodNumberParameters) 10473 << FirstMethodType << FirstName << FirstNumParameters; 10474 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10475 SecondMethod->getSourceRange(), 10476 MethodNumberParameters) 10477 << SecondMethodType << SecondName << SecondNumParameters; 10478 Diagnosed = true; 10479 break; 10480 } 10481 10482 // Need this status boolean to know when break out of the switch. 10483 bool ParameterMismatch = false; 10484 for (unsigned I = 0; I < FirstNumParameters; ++I) { 10485 const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I); 10486 const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I); 10487 10488 QualType FirstParamType = FirstParam->getType(); 10489 QualType SecondParamType = SecondParam->getType(); 10490 if (FirstParamType != SecondParamType && 10491 ComputeQualTypeODRHash(FirstParamType) != 10492 ComputeQualTypeODRHash(SecondParamType)) { 10493 if (const DecayedType *ParamDecayedType = 10494 FirstParamType->getAs<DecayedType>()) { 10495 ODRDiagDeclError( 10496 FirstRecord, FirstModule, FirstMethod->getLocation(), 10497 FirstMethod->getSourceRange(), MethodParameterType) 10498 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10499 << true << ParamDecayedType->getOriginalType(); 10500 } else { 10501 ODRDiagDeclError( 10502 FirstRecord, FirstModule, FirstMethod->getLocation(), 10503 FirstMethod->getSourceRange(), MethodParameterType) 10504 << FirstMethodType << FirstName << (I + 1) << FirstParamType 10505 << false; 10506 } 10507 10508 if (const DecayedType *ParamDecayedType = 10509 SecondParamType->getAs<DecayedType>()) { 10510 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10511 SecondMethod->getSourceRange(), 10512 MethodParameterType) 10513 << SecondMethodType << SecondName << (I + 1) 10514 << SecondParamType << true 10515 << ParamDecayedType->getOriginalType(); 10516 } else { 10517 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10518 SecondMethod->getSourceRange(), 10519 MethodParameterType) 10520 << SecondMethodType << SecondName << (I + 1) 10521 << SecondParamType << false; 10522 } 10523 ParameterMismatch = true; 10524 break; 10525 } 10526 10527 DeclarationName FirstParamName = FirstParam->getDeclName(); 10528 DeclarationName SecondParamName = SecondParam->getDeclName(); 10529 if (FirstParamName != SecondParamName) { 10530 ODRDiagDeclError(FirstRecord, FirstModule, 10531 FirstMethod->getLocation(), 10532 FirstMethod->getSourceRange(), MethodParameterName) 10533 << FirstMethodType << FirstName << (I + 1) << FirstParamName; 10534 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10535 SecondMethod->getSourceRange(), MethodParameterName) 10536 << SecondMethodType << SecondName << (I + 1) << SecondParamName; 10537 ParameterMismatch = true; 10538 break; 10539 } 10540 10541 const Expr *FirstInit = FirstParam->getInit(); 10542 const Expr *SecondInit = SecondParam->getInit(); 10543 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 10544 ODRDiagDeclError(FirstRecord, FirstModule, 10545 FirstMethod->getLocation(), 10546 FirstMethod->getSourceRange(), 10547 MethodParameterSingleDefaultArgument) 10548 << FirstMethodType << FirstName << (I + 1) 10549 << (FirstInit == nullptr) 10550 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 10551 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10552 SecondMethod->getSourceRange(), 10553 MethodParameterSingleDefaultArgument) 10554 << SecondMethodType << SecondName << (I + 1) 10555 << (SecondInit == nullptr) 10556 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 10557 ParameterMismatch = true; 10558 break; 10559 } 10560 10561 if (FirstInit && SecondInit && 10562 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 10563 ODRDiagDeclError(FirstRecord, FirstModule, 10564 FirstMethod->getLocation(), 10565 FirstMethod->getSourceRange(), 10566 MethodParameterDifferentDefaultArgument) 10567 << FirstMethodType << FirstName << (I + 1) 10568 << FirstInit->getSourceRange(); 10569 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10570 SecondMethod->getSourceRange(), 10571 MethodParameterDifferentDefaultArgument) 10572 << SecondMethodType << SecondName << (I + 1) 10573 << SecondInit->getSourceRange(); 10574 ParameterMismatch = true; 10575 break; 10576 10577 } 10578 } 10579 10580 if (ParameterMismatch) { 10581 Diagnosed = true; 10582 break; 10583 } 10584 10585 const auto *FirstTemplateArgs = 10586 FirstMethod->getTemplateSpecializationArgs(); 10587 const auto *SecondTemplateArgs = 10588 SecondMethod->getTemplateSpecializationArgs(); 10589 10590 if ((FirstTemplateArgs && !SecondTemplateArgs) || 10591 (!FirstTemplateArgs && SecondTemplateArgs)) { 10592 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10593 FirstMethod->getSourceRange(), 10594 MethodNoTemplateArguments) 10595 << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr); 10596 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10597 SecondMethod->getSourceRange(), 10598 MethodNoTemplateArguments) 10599 << SecondMethodType << SecondName 10600 << (SecondTemplateArgs != nullptr); 10601 10602 Diagnosed = true; 10603 break; 10604 } 10605 10606 if (FirstTemplateArgs && SecondTemplateArgs) { 10607 // Remove pack expansions from argument list. 10608 auto ExpandTemplateArgumentList = 10609 [](const TemplateArgumentList *TAL) { 10610 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList; 10611 for (const TemplateArgument &TA : TAL->asArray()) { 10612 if (TA.getKind() != TemplateArgument::Pack) { 10613 ExpandedList.push_back(&TA); 10614 continue; 10615 } 10616 for (const TemplateArgument &PackTA : TA.getPackAsArray()) { 10617 ExpandedList.push_back(&PackTA); 10618 } 10619 } 10620 return ExpandedList; 10621 }; 10622 llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList = 10623 ExpandTemplateArgumentList(FirstTemplateArgs); 10624 llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList = 10625 ExpandTemplateArgumentList(SecondTemplateArgs); 10626 10627 if (FirstExpandedList.size() != SecondExpandedList.size()) { 10628 ODRDiagDeclError(FirstRecord, FirstModule, 10629 FirstMethod->getLocation(), 10630 FirstMethod->getSourceRange(), 10631 MethodDifferentNumberTemplateArguments) 10632 << FirstMethodType << FirstName 10633 << (unsigned)FirstExpandedList.size(); 10634 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10635 SecondMethod->getSourceRange(), 10636 MethodDifferentNumberTemplateArguments) 10637 << SecondMethodType << SecondName 10638 << (unsigned)SecondExpandedList.size(); 10639 10640 Diagnosed = true; 10641 break; 10642 } 10643 10644 bool TemplateArgumentMismatch = false; 10645 for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) { 10646 const TemplateArgument &FirstTA = *FirstExpandedList[i], 10647 &SecondTA = *SecondExpandedList[i]; 10648 if (ComputeTemplateArgumentODRHash(FirstTA) == 10649 ComputeTemplateArgumentODRHash(SecondTA)) { 10650 continue; 10651 } 10652 10653 ODRDiagDeclError( 10654 FirstRecord, FirstModule, FirstMethod->getLocation(), 10655 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument) 10656 << FirstMethodType << FirstName << FirstTA << i + 1; 10657 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10658 SecondMethod->getSourceRange(), 10659 MethodDifferentTemplateArgument) 10660 << SecondMethodType << SecondName << SecondTA << i + 1; 10661 10662 TemplateArgumentMismatch = true; 10663 break; 10664 } 10665 10666 if (TemplateArgumentMismatch) { 10667 Diagnosed = true; 10668 break; 10669 } 10670 } 10671 10672 // Compute the hash of the method as if it has no body. 10673 auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) { 10674 Hash.clear(); 10675 Hash.AddFunctionDecl(D, true /*SkipBody*/); 10676 return Hash.CalculateHash(); 10677 }; 10678 10679 // Compare the hash generated to the hash stored. A difference means 10680 // that a body was present in the original source. Due to merging, 10681 // the stardard way of detecting a body will not work. 10682 const bool HasFirstBody = 10683 ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash(); 10684 const bool HasSecondBody = 10685 ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash(); 10686 10687 if (HasFirstBody != HasSecondBody) { 10688 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10689 FirstMethod->getSourceRange(), MethodSingleBody) 10690 << FirstMethodType << FirstName << HasFirstBody; 10691 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10692 SecondMethod->getSourceRange(), MethodSingleBody) 10693 << SecondMethodType << SecondName << HasSecondBody; 10694 Diagnosed = true; 10695 break; 10696 } 10697 10698 if (HasFirstBody && HasSecondBody) { 10699 ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(), 10700 FirstMethod->getSourceRange(), MethodDifferentBody) 10701 << FirstMethodType << FirstName; 10702 ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(), 10703 SecondMethod->getSourceRange(), MethodDifferentBody) 10704 << SecondMethodType << SecondName; 10705 Diagnosed = true; 10706 break; 10707 } 10708 10709 break; 10710 } 10711 case TypeAlias: 10712 case TypeDef: { 10713 Diagnosed = ODRDiagTypeDefOrAlias( 10714 FirstRecord, FirstModule, SecondModule, 10715 cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl), 10716 FirstDiffType == TypeAlias); 10717 break; 10718 } 10719 case Var: { 10720 Diagnosed = 10721 ODRDiagVar(FirstRecord, FirstModule, SecondModule, 10722 cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl)); 10723 break; 10724 } 10725 case Friend: { 10726 FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl); 10727 FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl); 10728 10729 NamedDecl *FirstND = FirstFriend->getFriendDecl(); 10730 NamedDecl *SecondND = SecondFriend->getFriendDecl(); 10731 10732 TypeSourceInfo *FirstTSI = FirstFriend->getFriendType(); 10733 TypeSourceInfo *SecondTSI = SecondFriend->getFriendType(); 10734 10735 if (FirstND && SecondND) { 10736 ODRDiagDeclError(FirstRecord, FirstModule, 10737 FirstFriend->getFriendLoc(), 10738 FirstFriend->getSourceRange(), FriendFunction) 10739 << FirstND; 10740 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10741 SecondFriend->getSourceRange(), FriendFunction) 10742 << SecondND; 10743 10744 Diagnosed = true; 10745 break; 10746 } 10747 10748 if (FirstTSI && SecondTSI) { 10749 QualType FirstFriendType = FirstTSI->getType(); 10750 QualType SecondFriendType = SecondTSI->getType(); 10751 assert(ComputeQualTypeODRHash(FirstFriendType) != 10752 ComputeQualTypeODRHash(SecondFriendType)); 10753 ODRDiagDeclError(FirstRecord, FirstModule, 10754 FirstFriend->getFriendLoc(), 10755 FirstFriend->getSourceRange(), FriendType) 10756 << FirstFriendType; 10757 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10758 SecondFriend->getSourceRange(), FriendType) 10759 << SecondFriendType; 10760 Diagnosed = true; 10761 break; 10762 } 10763 10764 ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(), 10765 FirstFriend->getSourceRange(), FriendTypeFunction) 10766 << (FirstTSI == nullptr); 10767 ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(), 10768 SecondFriend->getSourceRange(), FriendTypeFunction) 10769 << (SecondTSI == nullptr); 10770 10771 Diagnosed = true; 10772 break; 10773 } 10774 case FunctionTemplate: { 10775 FunctionTemplateDecl *FirstTemplate = 10776 cast<FunctionTemplateDecl>(FirstDecl); 10777 FunctionTemplateDecl *SecondTemplate = 10778 cast<FunctionTemplateDecl>(SecondDecl); 10779 10780 TemplateParameterList *FirstTPL = 10781 FirstTemplate->getTemplateParameters(); 10782 TemplateParameterList *SecondTPL = 10783 SecondTemplate->getTemplateParameters(); 10784 10785 if (FirstTPL->size() != SecondTPL->size()) { 10786 ODRDiagDeclError(FirstRecord, FirstModule, 10787 FirstTemplate->getLocation(), 10788 FirstTemplate->getSourceRange(), 10789 FunctionTemplateDifferentNumberParameters) 10790 << FirstTemplate << FirstTPL->size(); 10791 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10792 SecondTemplate->getSourceRange(), 10793 FunctionTemplateDifferentNumberParameters) 10794 << SecondTemplate << SecondTPL->size(); 10795 10796 Diagnosed = true; 10797 break; 10798 } 10799 10800 bool ParameterMismatch = false; 10801 for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) { 10802 NamedDecl *FirstParam = FirstTPL->getParam(i); 10803 NamedDecl *SecondParam = SecondTPL->getParam(i); 10804 10805 if (FirstParam->getKind() != SecondParam->getKind()) { 10806 enum { 10807 TemplateTypeParameter, 10808 NonTypeTemplateParameter, 10809 TemplateTemplateParameter, 10810 }; 10811 auto GetParamType = [](NamedDecl *D) { 10812 switch (D->getKind()) { 10813 default: 10814 llvm_unreachable("Unexpected template parameter type"); 10815 case Decl::TemplateTypeParm: 10816 return TemplateTypeParameter; 10817 case Decl::NonTypeTemplateParm: 10818 return NonTypeTemplateParameter; 10819 case Decl::TemplateTemplateParm: 10820 return TemplateTemplateParameter; 10821 } 10822 }; 10823 10824 ODRDiagDeclError(FirstRecord, FirstModule, 10825 FirstTemplate->getLocation(), 10826 FirstTemplate->getSourceRange(), 10827 FunctionTemplateParameterDifferentKind) 10828 << FirstTemplate << (i + 1) << GetParamType(FirstParam); 10829 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10830 SecondTemplate->getSourceRange(), 10831 FunctionTemplateParameterDifferentKind) 10832 << SecondTemplate << (i + 1) << GetParamType(SecondParam); 10833 10834 ParameterMismatch = true; 10835 break; 10836 } 10837 10838 if (FirstParam->getName() != SecondParam->getName()) { 10839 ODRDiagDeclError( 10840 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10841 FirstTemplate->getSourceRange(), FunctionTemplateParameterName) 10842 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier() 10843 << FirstParam; 10844 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10845 SecondTemplate->getSourceRange(), 10846 FunctionTemplateParameterName) 10847 << SecondTemplate << (i + 1) 10848 << (bool)SecondParam->getIdentifier() << SecondParam; 10849 ParameterMismatch = true; 10850 break; 10851 } 10852 10853 if (isa<TemplateTypeParmDecl>(FirstParam) && 10854 isa<TemplateTypeParmDecl>(SecondParam)) { 10855 TemplateTypeParmDecl *FirstTTPD = 10856 cast<TemplateTypeParmDecl>(FirstParam); 10857 TemplateTypeParmDecl *SecondTTPD = 10858 cast<TemplateTypeParmDecl>(SecondParam); 10859 bool HasFirstDefaultArgument = 10860 FirstTTPD->hasDefaultArgument() && 10861 !FirstTTPD->defaultArgumentWasInherited(); 10862 bool HasSecondDefaultArgument = 10863 SecondTTPD->hasDefaultArgument() && 10864 !SecondTTPD->defaultArgumentWasInherited(); 10865 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10866 ODRDiagDeclError(FirstRecord, FirstModule, 10867 FirstTemplate->getLocation(), 10868 FirstTemplate->getSourceRange(), 10869 FunctionTemplateParameterSingleDefaultArgument) 10870 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10871 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10872 SecondTemplate->getSourceRange(), 10873 FunctionTemplateParameterSingleDefaultArgument) 10874 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10875 ParameterMismatch = true; 10876 break; 10877 } 10878 10879 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10880 QualType FirstType = FirstTTPD->getDefaultArgument(); 10881 QualType SecondType = SecondTTPD->getDefaultArgument(); 10882 if (ComputeQualTypeODRHash(FirstType) != 10883 ComputeQualTypeODRHash(SecondType)) { 10884 ODRDiagDeclError( 10885 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10886 FirstTemplate->getSourceRange(), 10887 FunctionTemplateParameterDifferentDefaultArgument) 10888 << FirstTemplate << (i + 1) << FirstType; 10889 ODRDiagDeclNote( 10890 SecondModule, SecondTemplate->getLocation(), 10891 SecondTemplate->getSourceRange(), 10892 FunctionTemplateParameterDifferentDefaultArgument) 10893 << SecondTemplate << (i + 1) << SecondType; 10894 ParameterMismatch = true; 10895 break; 10896 } 10897 } 10898 10899 if (FirstTTPD->isParameterPack() != 10900 SecondTTPD->isParameterPack()) { 10901 ODRDiagDeclError(FirstRecord, FirstModule, 10902 FirstTemplate->getLocation(), 10903 FirstTemplate->getSourceRange(), 10904 FunctionTemplatePackParameter) 10905 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10906 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10907 SecondTemplate->getSourceRange(), 10908 FunctionTemplatePackParameter) 10909 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10910 ParameterMismatch = true; 10911 break; 10912 } 10913 } 10914 10915 if (isa<TemplateTemplateParmDecl>(FirstParam) && 10916 isa<TemplateTemplateParmDecl>(SecondParam)) { 10917 TemplateTemplateParmDecl *FirstTTPD = 10918 cast<TemplateTemplateParmDecl>(FirstParam); 10919 TemplateTemplateParmDecl *SecondTTPD = 10920 cast<TemplateTemplateParmDecl>(SecondParam); 10921 10922 TemplateParameterList *FirstTPL = 10923 FirstTTPD->getTemplateParameters(); 10924 TemplateParameterList *SecondTPL = 10925 SecondTTPD->getTemplateParameters(); 10926 10927 if (ComputeTemplateParameterListODRHash(FirstTPL) != 10928 ComputeTemplateParameterListODRHash(SecondTPL)) { 10929 ODRDiagDeclError(FirstRecord, FirstModule, 10930 FirstTemplate->getLocation(), 10931 FirstTemplate->getSourceRange(), 10932 FunctionTemplateParameterDifferentType) 10933 << FirstTemplate << (i + 1); 10934 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10935 SecondTemplate->getSourceRange(), 10936 FunctionTemplateParameterDifferentType) 10937 << SecondTemplate << (i + 1); 10938 ParameterMismatch = true; 10939 break; 10940 } 10941 10942 bool HasFirstDefaultArgument = 10943 FirstTTPD->hasDefaultArgument() && 10944 !FirstTTPD->defaultArgumentWasInherited(); 10945 bool HasSecondDefaultArgument = 10946 SecondTTPD->hasDefaultArgument() && 10947 !SecondTTPD->defaultArgumentWasInherited(); 10948 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 10949 ODRDiagDeclError(FirstRecord, FirstModule, 10950 FirstTemplate->getLocation(), 10951 FirstTemplate->getSourceRange(), 10952 FunctionTemplateParameterSingleDefaultArgument) 10953 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 10954 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10955 SecondTemplate->getSourceRange(), 10956 FunctionTemplateParameterSingleDefaultArgument) 10957 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 10958 ParameterMismatch = true; 10959 break; 10960 } 10961 10962 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 10963 TemplateArgument FirstTA = 10964 FirstTTPD->getDefaultArgument().getArgument(); 10965 TemplateArgument SecondTA = 10966 SecondTTPD->getDefaultArgument().getArgument(); 10967 if (ComputeTemplateArgumentODRHash(FirstTA) != 10968 ComputeTemplateArgumentODRHash(SecondTA)) { 10969 ODRDiagDeclError( 10970 FirstRecord, FirstModule, FirstTemplate->getLocation(), 10971 FirstTemplate->getSourceRange(), 10972 FunctionTemplateParameterDifferentDefaultArgument) 10973 << FirstTemplate << (i + 1) << FirstTA; 10974 ODRDiagDeclNote( 10975 SecondModule, SecondTemplate->getLocation(), 10976 SecondTemplate->getSourceRange(), 10977 FunctionTemplateParameterDifferentDefaultArgument) 10978 << SecondTemplate << (i + 1) << SecondTA; 10979 ParameterMismatch = true; 10980 break; 10981 } 10982 } 10983 10984 if (FirstTTPD->isParameterPack() != 10985 SecondTTPD->isParameterPack()) { 10986 ODRDiagDeclError(FirstRecord, FirstModule, 10987 FirstTemplate->getLocation(), 10988 FirstTemplate->getSourceRange(), 10989 FunctionTemplatePackParameter) 10990 << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack(); 10991 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 10992 SecondTemplate->getSourceRange(), 10993 FunctionTemplatePackParameter) 10994 << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack(); 10995 ParameterMismatch = true; 10996 break; 10997 } 10998 } 10999 11000 if (isa<NonTypeTemplateParmDecl>(FirstParam) && 11001 isa<NonTypeTemplateParmDecl>(SecondParam)) { 11002 NonTypeTemplateParmDecl *FirstNTTPD = 11003 cast<NonTypeTemplateParmDecl>(FirstParam); 11004 NonTypeTemplateParmDecl *SecondNTTPD = 11005 cast<NonTypeTemplateParmDecl>(SecondParam); 11006 11007 QualType FirstType = FirstNTTPD->getType(); 11008 QualType SecondType = SecondNTTPD->getType(); 11009 if (ComputeQualTypeODRHash(FirstType) != 11010 ComputeQualTypeODRHash(SecondType)) { 11011 ODRDiagDeclError(FirstRecord, FirstModule, 11012 FirstTemplate->getLocation(), 11013 FirstTemplate->getSourceRange(), 11014 FunctionTemplateParameterDifferentType) 11015 << FirstTemplate << (i + 1); 11016 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11017 SecondTemplate->getSourceRange(), 11018 FunctionTemplateParameterDifferentType) 11019 << SecondTemplate << (i + 1); 11020 ParameterMismatch = true; 11021 break; 11022 } 11023 11024 bool HasFirstDefaultArgument = 11025 FirstNTTPD->hasDefaultArgument() && 11026 !FirstNTTPD->defaultArgumentWasInherited(); 11027 bool HasSecondDefaultArgument = 11028 SecondNTTPD->hasDefaultArgument() && 11029 !SecondNTTPD->defaultArgumentWasInherited(); 11030 if (HasFirstDefaultArgument != HasSecondDefaultArgument) { 11031 ODRDiagDeclError(FirstRecord, FirstModule, 11032 FirstTemplate->getLocation(), 11033 FirstTemplate->getSourceRange(), 11034 FunctionTemplateParameterSingleDefaultArgument) 11035 << FirstTemplate << (i + 1) << HasFirstDefaultArgument; 11036 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11037 SecondTemplate->getSourceRange(), 11038 FunctionTemplateParameterSingleDefaultArgument) 11039 << SecondTemplate << (i + 1) << HasSecondDefaultArgument; 11040 ParameterMismatch = true; 11041 break; 11042 } 11043 11044 if (HasFirstDefaultArgument && HasSecondDefaultArgument) { 11045 Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument(); 11046 Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument(); 11047 if (ComputeODRHash(FirstDefaultArgument) != 11048 ComputeODRHash(SecondDefaultArgument)) { 11049 ODRDiagDeclError( 11050 FirstRecord, FirstModule, FirstTemplate->getLocation(), 11051 FirstTemplate->getSourceRange(), 11052 FunctionTemplateParameterDifferentDefaultArgument) 11053 << FirstTemplate << (i + 1) << FirstDefaultArgument; 11054 ODRDiagDeclNote( 11055 SecondModule, SecondTemplate->getLocation(), 11056 SecondTemplate->getSourceRange(), 11057 FunctionTemplateParameterDifferentDefaultArgument) 11058 << SecondTemplate << (i + 1) << SecondDefaultArgument; 11059 ParameterMismatch = true; 11060 break; 11061 } 11062 } 11063 11064 if (FirstNTTPD->isParameterPack() != 11065 SecondNTTPD->isParameterPack()) { 11066 ODRDiagDeclError(FirstRecord, FirstModule, 11067 FirstTemplate->getLocation(), 11068 FirstTemplate->getSourceRange(), 11069 FunctionTemplatePackParameter) 11070 << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack(); 11071 ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(), 11072 SecondTemplate->getSourceRange(), 11073 FunctionTemplatePackParameter) 11074 << SecondTemplate << (i + 1) 11075 << SecondNTTPD->isParameterPack(); 11076 ParameterMismatch = true; 11077 break; 11078 } 11079 } 11080 } 11081 11082 if (ParameterMismatch) { 11083 Diagnosed = true; 11084 break; 11085 } 11086 11087 break; 11088 } 11089 } 11090 11091 if (Diagnosed) 11092 continue; 11093 11094 Diag(FirstDecl->getLocation(), 11095 diag::err_module_odr_violation_mismatch_decl_unknown) 11096 << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType 11097 << FirstDecl->getSourceRange(); 11098 Diag(SecondDecl->getLocation(), 11099 diag::note_module_odr_violation_mismatch_decl_unknown) 11100 << SecondModule << FirstDiffType << SecondDecl->getSourceRange(); 11101 Diagnosed = true; 11102 } 11103 11104 if (!Diagnosed) { 11105 // All definitions are updates to the same declaration. This happens if a 11106 // module instantiates the declaration of a class template specialization 11107 // and two or more other modules instantiate its definition. 11108 // 11109 // FIXME: Indicate which modules had instantiations of this definition. 11110 // FIXME: How can this even happen? 11111 Diag(Merge.first->getLocation(), 11112 diag::err_module_odr_violation_different_instantiations) 11113 << Merge.first; 11114 } 11115 } 11116 11117 // Issue ODR failures diagnostics for functions. 11118 for (auto &Merge : FunctionOdrMergeFailures) { 11119 enum ODRFunctionDifference { 11120 ReturnType, 11121 ParameterName, 11122 ParameterType, 11123 ParameterSingleDefaultArgument, 11124 ParameterDifferentDefaultArgument, 11125 FunctionBody, 11126 }; 11127 11128 FunctionDecl *FirstFunction = Merge.first; 11129 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction); 11130 11131 bool Diagnosed = false; 11132 for (auto &SecondFunction : Merge.second) { 11133 11134 if (FirstFunction == SecondFunction) 11135 continue; 11136 11137 std::string SecondModule = 11138 getOwningModuleNameForDiagnostic(SecondFunction); 11139 11140 auto ODRDiagError = [FirstFunction, &FirstModule, 11141 this](SourceLocation Loc, SourceRange Range, 11142 ODRFunctionDifference DiffType) { 11143 return Diag(Loc, diag::err_module_odr_violation_function) 11144 << FirstFunction << FirstModule.empty() << FirstModule << Range 11145 << DiffType; 11146 }; 11147 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11148 SourceRange Range, 11149 ODRFunctionDifference DiffType) { 11150 return Diag(Loc, diag::note_module_odr_violation_function) 11151 << SecondModule << Range << DiffType; 11152 }; 11153 11154 if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) != 11155 ComputeQualTypeODRHash(SecondFunction->getReturnType())) { 11156 ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(), 11157 FirstFunction->getReturnTypeSourceRange(), ReturnType) 11158 << FirstFunction->getReturnType(); 11159 ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(), 11160 SecondFunction->getReturnTypeSourceRange(), ReturnType) 11161 << SecondFunction->getReturnType(); 11162 Diagnosed = true; 11163 break; 11164 } 11165 11166 assert(FirstFunction->param_size() == SecondFunction->param_size() && 11167 "Merged functions with different number of parameters"); 11168 11169 auto ParamSize = FirstFunction->param_size(); 11170 bool ParameterMismatch = false; 11171 for (unsigned I = 0; I < ParamSize; ++I) { 11172 auto *FirstParam = FirstFunction->getParamDecl(I); 11173 auto *SecondParam = SecondFunction->getParamDecl(I); 11174 11175 assert(getContext().hasSameType(FirstParam->getType(), 11176 SecondParam->getType()) && 11177 "Merged function has different parameter types."); 11178 11179 if (FirstParam->getDeclName() != SecondParam->getDeclName()) { 11180 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11181 ParameterName) 11182 << I + 1 << FirstParam->getDeclName(); 11183 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11184 ParameterName) 11185 << I + 1 << SecondParam->getDeclName(); 11186 ParameterMismatch = true; 11187 break; 11188 }; 11189 11190 QualType FirstParamType = FirstParam->getType(); 11191 QualType SecondParamType = SecondParam->getType(); 11192 if (FirstParamType != SecondParamType && 11193 ComputeQualTypeODRHash(FirstParamType) != 11194 ComputeQualTypeODRHash(SecondParamType)) { 11195 if (const DecayedType *ParamDecayedType = 11196 FirstParamType->getAs<DecayedType>()) { 11197 ODRDiagError(FirstParam->getLocation(), 11198 FirstParam->getSourceRange(), ParameterType) 11199 << (I + 1) << FirstParamType << true 11200 << ParamDecayedType->getOriginalType(); 11201 } else { 11202 ODRDiagError(FirstParam->getLocation(), 11203 FirstParam->getSourceRange(), ParameterType) 11204 << (I + 1) << FirstParamType << false; 11205 } 11206 11207 if (const DecayedType *ParamDecayedType = 11208 SecondParamType->getAs<DecayedType>()) { 11209 ODRDiagNote(SecondParam->getLocation(), 11210 SecondParam->getSourceRange(), ParameterType) 11211 << (I + 1) << SecondParamType << true 11212 << ParamDecayedType->getOriginalType(); 11213 } else { 11214 ODRDiagNote(SecondParam->getLocation(), 11215 SecondParam->getSourceRange(), ParameterType) 11216 << (I + 1) << SecondParamType << false; 11217 } 11218 ParameterMismatch = true; 11219 break; 11220 } 11221 11222 const Expr *FirstInit = FirstParam->getInit(); 11223 const Expr *SecondInit = SecondParam->getInit(); 11224 if ((FirstInit == nullptr) != (SecondInit == nullptr)) { 11225 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11226 ParameterSingleDefaultArgument) 11227 << (I + 1) << (FirstInit == nullptr) 11228 << (FirstInit ? FirstInit->getSourceRange() : SourceRange()); 11229 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11230 ParameterSingleDefaultArgument) 11231 << (I + 1) << (SecondInit == nullptr) 11232 << (SecondInit ? SecondInit->getSourceRange() : SourceRange()); 11233 ParameterMismatch = true; 11234 break; 11235 } 11236 11237 if (FirstInit && SecondInit && 11238 ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11239 ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(), 11240 ParameterDifferentDefaultArgument) 11241 << (I + 1) << FirstInit->getSourceRange(); 11242 ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(), 11243 ParameterDifferentDefaultArgument) 11244 << (I + 1) << SecondInit->getSourceRange(); 11245 ParameterMismatch = true; 11246 break; 11247 } 11248 11249 assert(ComputeSubDeclODRHash(FirstParam) == 11250 ComputeSubDeclODRHash(SecondParam) && 11251 "Undiagnosed parameter difference."); 11252 } 11253 11254 if (ParameterMismatch) { 11255 Diagnosed = true; 11256 break; 11257 } 11258 11259 // If no error has been generated before now, assume the problem is in 11260 // the body and generate a message. 11261 ODRDiagError(FirstFunction->getLocation(), 11262 FirstFunction->getSourceRange(), FunctionBody); 11263 ODRDiagNote(SecondFunction->getLocation(), 11264 SecondFunction->getSourceRange(), FunctionBody); 11265 Diagnosed = true; 11266 break; 11267 } 11268 (void)Diagnosed; 11269 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11270 } 11271 11272 // Issue ODR failures diagnostics for enums. 11273 for (auto &Merge : EnumOdrMergeFailures) { 11274 enum ODREnumDifference { 11275 SingleScopedEnum, 11276 EnumTagKeywordMismatch, 11277 SingleSpecifiedType, 11278 DifferentSpecifiedTypes, 11279 DifferentNumberEnumConstants, 11280 EnumConstantName, 11281 EnumConstantSingleInitilizer, 11282 EnumConstantDifferentInitilizer, 11283 }; 11284 11285 // If we've already pointed out a specific problem with this enum, don't 11286 // bother issuing a general "something's different" diagnostic. 11287 if (!DiagnosedOdrMergeFailures.insert(Merge.first).second) 11288 continue; 11289 11290 EnumDecl *FirstEnum = Merge.first; 11291 std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum); 11292 11293 using DeclHashes = 11294 llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>; 11295 auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum]( 11296 DeclHashes &Hashes, EnumDecl *Enum) { 11297 for (auto *D : Enum->decls()) { 11298 // Due to decl merging, the first EnumDecl is the parent of 11299 // Decls in both records. 11300 if (!ODRHash::isWhitelistedDecl(D, FirstEnum)) 11301 continue; 11302 assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind"); 11303 Hashes.emplace_back(cast<EnumConstantDecl>(D), 11304 ComputeSubDeclODRHash(D)); 11305 } 11306 }; 11307 DeclHashes FirstHashes; 11308 PopulateHashes(FirstHashes, FirstEnum); 11309 bool Diagnosed = false; 11310 for (auto &SecondEnum : Merge.second) { 11311 11312 if (FirstEnum == SecondEnum) 11313 continue; 11314 11315 std::string SecondModule = 11316 getOwningModuleNameForDiagnostic(SecondEnum); 11317 11318 auto ODRDiagError = [FirstEnum, &FirstModule, 11319 this](SourceLocation Loc, SourceRange Range, 11320 ODREnumDifference DiffType) { 11321 return Diag(Loc, diag::err_module_odr_violation_enum) 11322 << FirstEnum << FirstModule.empty() << FirstModule << Range 11323 << DiffType; 11324 }; 11325 auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc, 11326 SourceRange Range, 11327 ODREnumDifference DiffType) { 11328 return Diag(Loc, diag::note_module_odr_violation_enum) 11329 << SecondModule << Range << DiffType; 11330 }; 11331 11332 if (FirstEnum->isScoped() != SecondEnum->isScoped()) { 11333 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11334 SingleScopedEnum) 11335 << FirstEnum->isScoped(); 11336 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11337 SingleScopedEnum) 11338 << SecondEnum->isScoped(); 11339 Diagnosed = true; 11340 continue; 11341 } 11342 11343 if (FirstEnum->isScoped() && SecondEnum->isScoped()) { 11344 if (FirstEnum->isScopedUsingClassTag() != 11345 SecondEnum->isScopedUsingClassTag()) { 11346 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11347 EnumTagKeywordMismatch) 11348 << FirstEnum->isScopedUsingClassTag(); 11349 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11350 EnumTagKeywordMismatch) 11351 << SecondEnum->isScopedUsingClassTag(); 11352 Diagnosed = true; 11353 continue; 11354 } 11355 } 11356 11357 QualType FirstUnderlyingType = 11358 FirstEnum->getIntegerTypeSourceInfo() 11359 ? FirstEnum->getIntegerTypeSourceInfo()->getType() 11360 : QualType(); 11361 QualType SecondUnderlyingType = 11362 SecondEnum->getIntegerTypeSourceInfo() 11363 ? SecondEnum->getIntegerTypeSourceInfo()->getType() 11364 : QualType(); 11365 if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) { 11366 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11367 SingleSpecifiedType) 11368 << !FirstUnderlyingType.isNull(); 11369 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11370 SingleSpecifiedType) 11371 << !SecondUnderlyingType.isNull(); 11372 Diagnosed = true; 11373 continue; 11374 } 11375 11376 if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) { 11377 if (ComputeQualTypeODRHash(FirstUnderlyingType) != 11378 ComputeQualTypeODRHash(SecondUnderlyingType)) { 11379 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11380 DifferentSpecifiedTypes) 11381 << FirstUnderlyingType; 11382 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11383 DifferentSpecifiedTypes) 11384 << SecondUnderlyingType; 11385 Diagnosed = true; 11386 continue; 11387 } 11388 } 11389 11390 DeclHashes SecondHashes; 11391 PopulateHashes(SecondHashes, SecondEnum); 11392 11393 if (FirstHashes.size() != SecondHashes.size()) { 11394 ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(), 11395 DifferentNumberEnumConstants) 11396 << (int)FirstHashes.size(); 11397 ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(), 11398 DifferentNumberEnumConstants) 11399 << (int)SecondHashes.size(); 11400 Diagnosed = true; 11401 continue; 11402 } 11403 11404 for (unsigned I = 0; I < FirstHashes.size(); ++I) { 11405 if (FirstHashes[I].second == SecondHashes[I].second) 11406 continue; 11407 const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first; 11408 const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first; 11409 11410 if (FirstEnumConstant->getDeclName() != 11411 SecondEnumConstant->getDeclName()) { 11412 11413 ODRDiagError(FirstEnumConstant->getLocation(), 11414 FirstEnumConstant->getSourceRange(), EnumConstantName) 11415 << I + 1 << FirstEnumConstant; 11416 ODRDiagNote(SecondEnumConstant->getLocation(), 11417 SecondEnumConstant->getSourceRange(), EnumConstantName) 11418 << I + 1 << SecondEnumConstant; 11419 Diagnosed = true; 11420 break; 11421 } 11422 11423 const Expr *FirstInit = FirstEnumConstant->getInitExpr(); 11424 const Expr *SecondInit = SecondEnumConstant->getInitExpr(); 11425 if (!FirstInit && !SecondInit) 11426 continue; 11427 11428 if (!FirstInit || !SecondInit) { 11429 ODRDiagError(FirstEnumConstant->getLocation(), 11430 FirstEnumConstant->getSourceRange(), 11431 EnumConstantSingleInitilizer) 11432 << I + 1 << FirstEnumConstant << (FirstInit != nullptr); 11433 ODRDiagNote(SecondEnumConstant->getLocation(), 11434 SecondEnumConstant->getSourceRange(), 11435 EnumConstantSingleInitilizer) 11436 << I + 1 << SecondEnumConstant << (SecondInit != nullptr); 11437 Diagnosed = true; 11438 break; 11439 } 11440 11441 if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) { 11442 ODRDiagError(FirstEnumConstant->getLocation(), 11443 FirstEnumConstant->getSourceRange(), 11444 EnumConstantDifferentInitilizer) 11445 << I + 1 << FirstEnumConstant; 11446 ODRDiagNote(SecondEnumConstant->getLocation(), 11447 SecondEnumConstant->getSourceRange(), 11448 EnumConstantDifferentInitilizer) 11449 << I + 1 << SecondEnumConstant; 11450 Diagnosed = true; 11451 break; 11452 } 11453 } 11454 } 11455 11456 (void)Diagnosed; 11457 assert(Diagnosed && "Unable to emit ODR diagnostic."); 11458 } 11459 } 11460 11461 void ASTReader::StartedDeserializing() { 11462 if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get()) 11463 ReadTimer->startTimer(); 11464 } 11465 11466 void ASTReader::FinishedDeserializing() { 11467 assert(NumCurrentElementsDeserializing && 11468 "FinishedDeserializing not paired with StartedDeserializing"); 11469 if (NumCurrentElementsDeserializing == 1) { 11470 // We decrease NumCurrentElementsDeserializing only after pending actions 11471 // are finished, to avoid recursively re-calling finishPendingActions(). 11472 finishPendingActions(); 11473 } 11474 --NumCurrentElementsDeserializing; 11475 11476 if (NumCurrentElementsDeserializing == 0) { 11477 // Propagate exception specification and deduced type updates along 11478 // redeclaration chains. 11479 // 11480 // We do this now rather than in finishPendingActions because we want to 11481 // be able to walk the complete redeclaration chains of the updated decls. 11482 while (!PendingExceptionSpecUpdates.empty() || 11483 !PendingDeducedTypeUpdates.empty()) { 11484 auto ESUpdates = std::move(PendingExceptionSpecUpdates); 11485 PendingExceptionSpecUpdates.clear(); 11486 for (auto Update : ESUpdates) { 11487 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11488 auto *FPT = Update.second->getType()->castAs<FunctionProtoType>(); 11489 auto ESI = FPT->getExtProtoInfo().ExceptionSpec; 11490 if (auto *Listener = getContext().getASTMutationListener()) 11491 Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second)); 11492 for (auto *Redecl : Update.second->redecls()) 11493 getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI); 11494 } 11495 11496 auto DTUpdates = std::move(PendingDeducedTypeUpdates); 11497 PendingDeducedTypeUpdates.clear(); 11498 for (auto Update : DTUpdates) { 11499 ProcessingUpdatesRAIIObj ProcessingUpdates(*this); 11500 // FIXME: If the return type is already deduced, check that it matches. 11501 getContext().adjustDeducedFunctionResultType(Update.first, 11502 Update.second); 11503 } 11504 } 11505 11506 if (ReadTimer) 11507 ReadTimer->stopTimer(); 11508 11509 diagnoseOdrViolations(); 11510 11511 // We are not in recursive loading, so it's safe to pass the "interesting" 11512 // decls to the consumer. 11513 if (Consumer) 11514 PassInterestingDeclsToConsumer(); 11515 } 11516 } 11517 11518 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { 11519 if (IdentifierInfo *II = Name.getAsIdentifierInfo()) { 11520 // Remove any fake results before adding any real ones. 11521 auto It = PendingFakeLookupResults.find(II); 11522 if (It != PendingFakeLookupResults.end()) { 11523 for (auto *ND : It->second) 11524 SemaObj->IdResolver.RemoveDecl(ND); 11525 // FIXME: this works around module+PCH performance issue. 11526 // Rather than erase the result from the map, which is O(n), just clear 11527 // the vector of NamedDecls. 11528 It->second.clear(); 11529 } 11530 } 11531 11532 if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { 11533 SemaObj->TUScope->AddDecl(D); 11534 } else if (SemaObj->TUScope) { 11535 // Adding the decl to IdResolver may have failed because it was already in 11536 // (even though it was not added in scope). If it is already in, make sure 11537 // it gets in the scope as well. 11538 if (std::find(SemaObj->IdResolver.begin(Name), 11539 SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end()) 11540 SemaObj->TUScope->AddDecl(D); 11541 } 11542 } 11543 11544 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache, 11545 ASTContext *Context, 11546 const PCHContainerReader &PCHContainerRdr, 11547 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 11548 StringRef isysroot, bool DisableValidation, 11549 bool AllowASTWithCompilerErrors, 11550 bool AllowConfigurationMismatch, bool ValidateSystemInputs, 11551 bool ValidateASTInputFilesContent, bool UseGlobalIndex, 11552 std::unique_ptr<llvm::Timer> ReadTimer) 11553 : Listener(DisableValidation 11554 ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP)) 11555 : cast<ASTReaderListener>(new PCHValidator(PP, *this))), 11556 SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), 11557 PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP), 11558 ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache, 11559 PCHContainerRdr, PP.getHeaderSearchInfo()), 11560 DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), 11561 DisableValidation(DisableValidation), 11562 AllowASTWithCompilerErrors(AllowASTWithCompilerErrors), 11563 AllowConfigurationMismatch(AllowConfigurationMismatch), 11564 ValidateSystemInputs(ValidateSystemInputs), 11565 ValidateASTInputFilesContent(ValidateASTInputFilesContent), 11566 UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) { 11567 SourceMgr.setExternalSLocEntrySource(this); 11568 11569 for (const auto &Ext : Extensions) { 11570 auto BlockName = Ext->getExtensionMetadata().BlockName; 11571 auto Known = ModuleFileExtensions.find(BlockName); 11572 if (Known != ModuleFileExtensions.end()) { 11573 Diags.Report(diag::warn_duplicate_module_file_extension) 11574 << BlockName; 11575 continue; 11576 } 11577 11578 ModuleFileExtensions.insert({BlockName, Ext}); 11579 } 11580 } 11581 11582 ASTReader::~ASTReader() { 11583 if (OwnsDeserializationListener) 11584 delete DeserializationListener; 11585 } 11586 11587 IdentifierResolver &ASTReader::getIdResolver() { 11588 return SemaObj ? SemaObj->IdResolver : DummyIdResolver; 11589 } 11590 11591 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor, 11592 unsigned AbbrevID) { 11593 Idx = 0; 11594 Record.clear(); 11595 return Cursor.readRecord(AbbrevID, Record); 11596 } 11597 //===----------------------------------------------------------------------===// 11598 //// OMPClauseReader implementation 11599 ////===----------------------------------------------------------------------===// 11600 11601 // This has to be in namespace clang because it's friended by all 11602 // of the OMP clauses. 11603 namespace clang { 11604 11605 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> { 11606 ASTRecordReader &Record; 11607 ASTContext &Context; 11608 11609 public: 11610 OMPClauseReader(ASTRecordReader &Record) 11611 : Record(Record), Context(Record.getContext()) {} 11612 11613 #define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *C); 11614 #include "clang/Basic/OpenMPKinds.def" 11615 OMPClause *readClause(); 11616 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 11617 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 11618 }; 11619 11620 } // end namespace clang 11621 11622 OMPClause *ASTRecordReader::readOMPClause() { 11623 return OMPClauseReader(*this).readClause(); 11624 } 11625 11626 OMPClause *OMPClauseReader::readClause() { 11627 OMPClause *C = nullptr; 11628 switch (Record.readInt()) { 11629 case OMPC_if: 11630 C = new (Context) OMPIfClause(); 11631 break; 11632 case OMPC_final: 11633 C = new (Context) OMPFinalClause(); 11634 break; 11635 case OMPC_num_threads: 11636 C = new (Context) OMPNumThreadsClause(); 11637 break; 11638 case OMPC_safelen: 11639 C = new (Context) OMPSafelenClause(); 11640 break; 11641 case OMPC_simdlen: 11642 C = new (Context) OMPSimdlenClause(); 11643 break; 11644 case OMPC_allocator: 11645 C = new (Context) OMPAllocatorClause(); 11646 break; 11647 case OMPC_collapse: 11648 C = new (Context) OMPCollapseClause(); 11649 break; 11650 case OMPC_default: 11651 C = new (Context) OMPDefaultClause(); 11652 break; 11653 case OMPC_proc_bind: 11654 C = new (Context) OMPProcBindClause(); 11655 break; 11656 case OMPC_schedule: 11657 C = new (Context) OMPScheduleClause(); 11658 break; 11659 case OMPC_ordered: 11660 C = OMPOrderedClause::CreateEmpty(Context, Record.readInt()); 11661 break; 11662 case OMPC_nowait: 11663 C = new (Context) OMPNowaitClause(); 11664 break; 11665 case OMPC_untied: 11666 C = new (Context) OMPUntiedClause(); 11667 break; 11668 case OMPC_mergeable: 11669 C = new (Context) OMPMergeableClause(); 11670 break; 11671 case OMPC_read: 11672 C = new (Context) OMPReadClause(); 11673 break; 11674 case OMPC_write: 11675 C = new (Context) OMPWriteClause(); 11676 break; 11677 case OMPC_update: 11678 C = OMPUpdateClause::CreateEmpty(Context, Record.readInt()); 11679 break; 11680 case OMPC_capture: 11681 C = new (Context) OMPCaptureClause(); 11682 break; 11683 case OMPC_seq_cst: 11684 C = new (Context) OMPSeqCstClause(); 11685 break; 11686 case OMPC_acq_rel: 11687 C = new (Context) OMPAcqRelClause(); 11688 break; 11689 case OMPC_acquire: 11690 C = new (Context) OMPAcquireClause(); 11691 break; 11692 case OMPC_release: 11693 C = new (Context) OMPReleaseClause(); 11694 break; 11695 case OMPC_relaxed: 11696 C = new (Context) OMPRelaxedClause(); 11697 break; 11698 case OMPC_threads: 11699 C = new (Context) OMPThreadsClause(); 11700 break; 11701 case OMPC_simd: 11702 C = new (Context) OMPSIMDClause(); 11703 break; 11704 case OMPC_nogroup: 11705 C = new (Context) OMPNogroupClause(); 11706 break; 11707 case OMPC_unified_address: 11708 C = new (Context) OMPUnifiedAddressClause(); 11709 break; 11710 case OMPC_unified_shared_memory: 11711 C = new (Context) OMPUnifiedSharedMemoryClause(); 11712 break; 11713 case OMPC_reverse_offload: 11714 C = new (Context) OMPReverseOffloadClause(); 11715 break; 11716 case OMPC_dynamic_allocators: 11717 C = new (Context) OMPDynamicAllocatorsClause(); 11718 break; 11719 case OMPC_atomic_default_mem_order: 11720 C = new (Context) OMPAtomicDefaultMemOrderClause(); 11721 break; 11722 case OMPC_private: 11723 C = OMPPrivateClause::CreateEmpty(Context, Record.readInt()); 11724 break; 11725 case OMPC_firstprivate: 11726 C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt()); 11727 break; 11728 case OMPC_lastprivate: 11729 C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt()); 11730 break; 11731 case OMPC_shared: 11732 C = OMPSharedClause::CreateEmpty(Context, Record.readInt()); 11733 break; 11734 case OMPC_reduction: 11735 C = OMPReductionClause::CreateEmpty(Context, Record.readInt()); 11736 break; 11737 case OMPC_task_reduction: 11738 C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt()); 11739 break; 11740 case OMPC_in_reduction: 11741 C = OMPInReductionClause::CreateEmpty(Context, Record.readInt()); 11742 break; 11743 case OMPC_linear: 11744 C = OMPLinearClause::CreateEmpty(Context, Record.readInt()); 11745 break; 11746 case OMPC_aligned: 11747 C = OMPAlignedClause::CreateEmpty(Context, Record.readInt()); 11748 break; 11749 case OMPC_copyin: 11750 C = OMPCopyinClause::CreateEmpty(Context, Record.readInt()); 11751 break; 11752 case OMPC_copyprivate: 11753 C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt()); 11754 break; 11755 case OMPC_flush: 11756 C = OMPFlushClause::CreateEmpty(Context, Record.readInt()); 11757 break; 11758 case OMPC_depobj: 11759 C = OMPDepobjClause::CreateEmpty(Context); 11760 break; 11761 case OMPC_depend: { 11762 unsigned NumVars = Record.readInt(); 11763 unsigned NumLoops = Record.readInt(); 11764 C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops); 11765 break; 11766 } 11767 case OMPC_device: 11768 C = new (Context) OMPDeviceClause(); 11769 break; 11770 case OMPC_map: { 11771 OMPMappableExprListSizeTy Sizes; 11772 Sizes.NumVars = Record.readInt(); 11773 Sizes.NumUniqueDeclarations = Record.readInt(); 11774 Sizes.NumComponentLists = Record.readInt(); 11775 Sizes.NumComponents = Record.readInt(); 11776 C = OMPMapClause::CreateEmpty(Context, Sizes); 11777 break; 11778 } 11779 case OMPC_num_teams: 11780 C = new (Context) OMPNumTeamsClause(); 11781 break; 11782 case OMPC_thread_limit: 11783 C = new (Context) OMPThreadLimitClause(); 11784 break; 11785 case OMPC_priority: 11786 C = new (Context) OMPPriorityClause(); 11787 break; 11788 case OMPC_grainsize: 11789 C = new (Context) OMPGrainsizeClause(); 11790 break; 11791 case OMPC_num_tasks: 11792 C = new (Context) OMPNumTasksClause(); 11793 break; 11794 case OMPC_hint: 11795 C = new (Context) OMPHintClause(); 11796 break; 11797 case OMPC_dist_schedule: 11798 C = new (Context) OMPDistScheduleClause(); 11799 break; 11800 case OMPC_defaultmap: 11801 C = new (Context) OMPDefaultmapClause(); 11802 break; 11803 case OMPC_to: { 11804 OMPMappableExprListSizeTy Sizes; 11805 Sizes.NumVars = Record.readInt(); 11806 Sizes.NumUniqueDeclarations = Record.readInt(); 11807 Sizes.NumComponentLists = Record.readInt(); 11808 Sizes.NumComponents = Record.readInt(); 11809 C = OMPToClause::CreateEmpty(Context, Sizes); 11810 break; 11811 } 11812 case OMPC_from: { 11813 OMPMappableExprListSizeTy Sizes; 11814 Sizes.NumVars = Record.readInt(); 11815 Sizes.NumUniqueDeclarations = Record.readInt(); 11816 Sizes.NumComponentLists = Record.readInt(); 11817 Sizes.NumComponents = Record.readInt(); 11818 C = OMPFromClause::CreateEmpty(Context, Sizes); 11819 break; 11820 } 11821 case OMPC_use_device_ptr: { 11822 OMPMappableExprListSizeTy Sizes; 11823 Sizes.NumVars = Record.readInt(); 11824 Sizes.NumUniqueDeclarations = Record.readInt(); 11825 Sizes.NumComponentLists = Record.readInt(); 11826 Sizes.NumComponents = Record.readInt(); 11827 C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes); 11828 break; 11829 } 11830 case OMPC_is_device_ptr: { 11831 OMPMappableExprListSizeTy Sizes; 11832 Sizes.NumVars = Record.readInt(); 11833 Sizes.NumUniqueDeclarations = Record.readInt(); 11834 Sizes.NumComponentLists = Record.readInt(); 11835 Sizes.NumComponents = Record.readInt(); 11836 C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes); 11837 break; 11838 } 11839 case OMPC_allocate: 11840 C = OMPAllocateClause::CreateEmpty(Context, Record.readInt()); 11841 break; 11842 case OMPC_nontemporal: 11843 C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt()); 11844 break; 11845 case OMPC_inclusive: 11846 C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt()); 11847 break; 11848 case OMPC_exclusive: 11849 C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt()); 11850 break; 11851 case OMPC_order: 11852 C = new (Context) OMPOrderClause(); 11853 break; 11854 case OMPC_destroy: 11855 C = new (Context) OMPDestroyClause(); 11856 break; 11857 case OMPC_detach: 11858 C = new (Context) OMPDetachClause(); 11859 break; 11860 } 11861 assert(C && "Unknown OMPClause type"); 11862 11863 Visit(C); 11864 C->setLocStart(Record.readSourceLocation()); 11865 C->setLocEnd(Record.readSourceLocation()); 11866 11867 return C; 11868 } 11869 11870 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) { 11871 C->setPreInitStmt(Record.readSubStmt(), 11872 static_cast<OpenMPDirectiveKind>(Record.readInt())); 11873 } 11874 11875 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) { 11876 VisitOMPClauseWithPreInit(C); 11877 C->setPostUpdateExpr(Record.readSubExpr()); 11878 } 11879 11880 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) { 11881 VisitOMPClauseWithPreInit(C); 11882 C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt())); 11883 C->setNameModifierLoc(Record.readSourceLocation()); 11884 C->setColonLoc(Record.readSourceLocation()); 11885 C->setCondition(Record.readSubExpr()); 11886 C->setLParenLoc(Record.readSourceLocation()); 11887 } 11888 11889 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) { 11890 VisitOMPClauseWithPreInit(C); 11891 C->setCondition(Record.readSubExpr()); 11892 C->setLParenLoc(Record.readSourceLocation()); 11893 } 11894 11895 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) { 11896 VisitOMPClauseWithPreInit(C); 11897 C->setNumThreads(Record.readSubExpr()); 11898 C->setLParenLoc(Record.readSourceLocation()); 11899 } 11900 11901 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) { 11902 C->setSafelen(Record.readSubExpr()); 11903 C->setLParenLoc(Record.readSourceLocation()); 11904 } 11905 11906 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) { 11907 C->setSimdlen(Record.readSubExpr()); 11908 C->setLParenLoc(Record.readSourceLocation()); 11909 } 11910 11911 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) { 11912 C->setAllocator(Record.readExpr()); 11913 C->setLParenLoc(Record.readSourceLocation()); 11914 } 11915 11916 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) { 11917 C->setNumForLoops(Record.readSubExpr()); 11918 C->setLParenLoc(Record.readSourceLocation()); 11919 } 11920 11921 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) { 11922 C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt())); 11923 C->setLParenLoc(Record.readSourceLocation()); 11924 C->setDefaultKindKwLoc(Record.readSourceLocation()); 11925 } 11926 11927 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) { 11928 C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt())); 11929 C->setLParenLoc(Record.readSourceLocation()); 11930 C->setProcBindKindKwLoc(Record.readSourceLocation()); 11931 } 11932 11933 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) { 11934 VisitOMPClauseWithPreInit(C); 11935 C->setScheduleKind( 11936 static_cast<OpenMPScheduleClauseKind>(Record.readInt())); 11937 C->setFirstScheduleModifier( 11938 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 11939 C->setSecondScheduleModifier( 11940 static_cast<OpenMPScheduleClauseModifier>(Record.readInt())); 11941 C->setChunkSize(Record.readSubExpr()); 11942 C->setLParenLoc(Record.readSourceLocation()); 11943 C->setFirstScheduleModifierLoc(Record.readSourceLocation()); 11944 C->setSecondScheduleModifierLoc(Record.readSourceLocation()); 11945 C->setScheduleKindLoc(Record.readSourceLocation()); 11946 C->setCommaLoc(Record.readSourceLocation()); 11947 } 11948 11949 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) { 11950 C->setNumForLoops(Record.readSubExpr()); 11951 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 11952 C->setLoopNumIterations(I, Record.readSubExpr()); 11953 for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I) 11954 C->setLoopCounter(I, Record.readSubExpr()); 11955 C->setLParenLoc(Record.readSourceLocation()); 11956 } 11957 11958 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) { 11959 C->setEventHandler(Record.readSubExpr()); 11960 C->setLParenLoc(Record.readSourceLocation()); 11961 } 11962 11963 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {} 11964 11965 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {} 11966 11967 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {} 11968 11969 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {} 11970 11971 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {} 11972 11973 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) { 11974 if (C->isExtended()) { 11975 C->setLParenLoc(Record.readSourceLocation()); 11976 C->setArgumentLoc(Record.readSourceLocation()); 11977 C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>()); 11978 } 11979 } 11980 11981 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {} 11982 11983 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {} 11984 11985 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {} 11986 11987 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {} 11988 11989 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {} 11990 11991 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {} 11992 11993 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {} 11994 11995 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {} 11996 11997 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {} 11998 11999 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {} 12000 12001 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {} 12002 12003 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause( 12004 OMPUnifiedSharedMemoryClause *) {} 12005 12006 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {} 12007 12008 void 12009 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) { 12010 } 12011 12012 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause( 12013 OMPAtomicDefaultMemOrderClause *C) { 12014 C->setAtomicDefaultMemOrderKind( 12015 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt())); 12016 C->setLParenLoc(Record.readSourceLocation()); 12017 C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation()); 12018 } 12019 12020 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) { 12021 C->setLParenLoc(Record.readSourceLocation()); 12022 unsigned NumVars = C->varlist_size(); 12023 SmallVector<Expr *, 16> Vars; 12024 Vars.reserve(NumVars); 12025 for (unsigned i = 0; i != NumVars; ++i) 12026 Vars.push_back(Record.readSubExpr()); 12027 C->setVarRefs(Vars); 12028 Vars.clear(); 12029 for (unsigned i = 0; i != NumVars; ++i) 12030 Vars.push_back(Record.readSubExpr()); 12031 C->setPrivateCopies(Vars); 12032 } 12033 12034 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) { 12035 VisitOMPClauseWithPreInit(C); 12036 C->setLParenLoc(Record.readSourceLocation()); 12037 unsigned NumVars = C->varlist_size(); 12038 SmallVector<Expr *, 16> Vars; 12039 Vars.reserve(NumVars); 12040 for (unsigned i = 0; i != NumVars; ++i) 12041 Vars.push_back(Record.readSubExpr()); 12042 C->setVarRefs(Vars); 12043 Vars.clear(); 12044 for (unsigned i = 0; i != NumVars; ++i) 12045 Vars.push_back(Record.readSubExpr()); 12046 C->setPrivateCopies(Vars); 12047 Vars.clear(); 12048 for (unsigned i = 0; i != NumVars; ++i) 12049 Vars.push_back(Record.readSubExpr()); 12050 C->setInits(Vars); 12051 } 12052 12053 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) { 12054 VisitOMPClauseWithPostUpdate(C); 12055 C->setLParenLoc(Record.readSourceLocation()); 12056 C->setKind(Record.readEnum<OpenMPLastprivateModifier>()); 12057 C->setKindLoc(Record.readSourceLocation()); 12058 C->setColonLoc(Record.readSourceLocation()); 12059 unsigned NumVars = C->varlist_size(); 12060 SmallVector<Expr *, 16> Vars; 12061 Vars.reserve(NumVars); 12062 for (unsigned i = 0; i != NumVars; ++i) 12063 Vars.push_back(Record.readSubExpr()); 12064 C->setVarRefs(Vars); 12065 Vars.clear(); 12066 for (unsigned i = 0; i != NumVars; ++i) 12067 Vars.push_back(Record.readSubExpr()); 12068 C->setPrivateCopies(Vars); 12069 Vars.clear(); 12070 for (unsigned i = 0; i != NumVars; ++i) 12071 Vars.push_back(Record.readSubExpr()); 12072 C->setSourceExprs(Vars); 12073 Vars.clear(); 12074 for (unsigned i = 0; i != NumVars; ++i) 12075 Vars.push_back(Record.readSubExpr()); 12076 C->setDestinationExprs(Vars); 12077 Vars.clear(); 12078 for (unsigned i = 0; i != NumVars; ++i) 12079 Vars.push_back(Record.readSubExpr()); 12080 C->setAssignmentOps(Vars); 12081 } 12082 12083 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) { 12084 C->setLParenLoc(Record.readSourceLocation()); 12085 unsigned NumVars = C->varlist_size(); 12086 SmallVector<Expr *, 16> Vars; 12087 Vars.reserve(NumVars); 12088 for (unsigned i = 0; i != NumVars; ++i) 12089 Vars.push_back(Record.readSubExpr()); 12090 C->setVarRefs(Vars); 12091 } 12092 12093 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) { 12094 VisitOMPClauseWithPostUpdate(C); 12095 C->setLParenLoc(Record.readSourceLocation()); 12096 C->setModifierLoc(Record.readSourceLocation()); 12097 C->setColonLoc(Record.readSourceLocation()); 12098 C->setModifier(Record.readEnum<OpenMPReductionClauseModifier>()); 12099 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12100 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12101 C->setQualifierLoc(NNSL); 12102 C->setNameInfo(DNI); 12103 12104 unsigned NumVars = C->varlist_size(); 12105 SmallVector<Expr *, 16> Vars; 12106 Vars.reserve(NumVars); 12107 for (unsigned i = 0; i != NumVars; ++i) 12108 Vars.push_back(Record.readSubExpr()); 12109 C->setVarRefs(Vars); 12110 Vars.clear(); 12111 for (unsigned i = 0; i != NumVars; ++i) 12112 Vars.push_back(Record.readSubExpr()); 12113 C->setPrivates(Vars); 12114 Vars.clear(); 12115 for (unsigned i = 0; i != NumVars; ++i) 12116 Vars.push_back(Record.readSubExpr()); 12117 C->setLHSExprs(Vars); 12118 Vars.clear(); 12119 for (unsigned i = 0; i != NumVars; ++i) 12120 Vars.push_back(Record.readSubExpr()); 12121 C->setRHSExprs(Vars); 12122 Vars.clear(); 12123 for (unsigned i = 0; i != NumVars; ++i) 12124 Vars.push_back(Record.readSubExpr()); 12125 C->setReductionOps(Vars); 12126 } 12127 12128 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) { 12129 VisitOMPClauseWithPostUpdate(C); 12130 C->setLParenLoc(Record.readSourceLocation()); 12131 C->setColonLoc(Record.readSourceLocation()); 12132 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12133 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12134 C->setQualifierLoc(NNSL); 12135 C->setNameInfo(DNI); 12136 12137 unsigned NumVars = C->varlist_size(); 12138 SmallVector<Expr *, 16> Vars; 12139 Vars.reserve(NumVars); 12140 for (unsigned I = 0; I != NumVars; ++I) 12141 Vars.push_back(Record.readSubExpr()); 12142 C->setVarRefs(Vars); 12143 Vars.clear(); 12144 for (unsigned I = 0; I != NumVars; ++I) 12145 Vars.push_back(Record.readSubExpr()); 12146 C->setPrivates(Vars); 12147 Vars.clear(); 12148 for (unsigned I = 0; I != NumVars; ++I) 12149 Vars.push_back(Record.readSubExpr()); 12150 C->setLHSExprs(Vars); 12151 Vars.clear(); 12152 for (unsigned I = 0; I != NumVars; ++I) 12153 Vars.push_back(Record.readSubExpr()); 12154 C->setRHSExprs(Vars); 12155 Vars.clear(); 12156 for (unsigned I = 0; I != NumVars; ++I) 12157 Vars.push_back(Record.readSubExpr()); 12158 C->setReductionOps(Vars); 12159 } 12160 12161 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) { 12162 VisitOMPClauseWithPostUpdate(C); 12163 C->setLParenLoc(Record.readSourceLocation()); 12164 C->setColonLoc(Record.readSourceLocation()); 12165 NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc(); 12166 DeclarationNameInfo DNI = Record.readDeclarationNameInfo(); 12167 C->setQualifierLoc(NNSL); 12168 C->setNameInfo(DNI); 12169 12170 unsigned NumVars = C->varlist_size(); 12171 SmallVector<Expr *, 16> Vars; 12172 Vars.reserve(NumVars); 12173 for (unsigned I = 0; I != NumVars; ++I) 12174 Vars.push_back(Record.readSubExpr()); 12175 C->setVarRefs(Vars); 12176 Vars.clear(); 12177 for (unsigned I = 0; I != NumVars; ++I) 12178 Vars.push_back(Record.readSubExpr()); 12179 C->setPrivates(Vars); 12180 Vars.clear(); 12181 for (unsigned I = 0; I != NumVars; ++I) 12182 Vars.push_back(Record.readSubExpr()); 12183 C->setLHSExprs(Vars); 12184 Vars.clear(); 12185 for (unsigned I = 0; I != NumVars; ++I) 12186 Vars.push_back(Record.readSubExpr()); 12187 C->setRHSExprs(Vars); 12188 Vars.clear(); 12189 for (unsigned I = 0; I != NumVars; ++I) 12190 Vars.push_back(Record.readSubExpr()); 12191 C->setReductionOps(Vars); 12192 Vars.clear(); 12193 for (unsigned I = 0; I != NumVars; ++I) 12194 Vars.push_back(Record.readSubExpr()); 12195 C->setTaskgroupDescriptors(Vars); 12196 } 12197 12198 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) { 12199 VisitOMPClauseWithPostUpdate(C); 12200 C->setLParenLoc(Record.readSourceLocation()); 12201 C->setColonLoc(Record.readSourceLocation()); 12202 C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt())); 12203 C->setModifierLoc(Record.readSourceLocation()); 12204 unsigned NumVars = C->varlist_size(); 12205 SmallVector<Expr *, 16> Vars; 12206 Vars.reserve(NumVars); 12207 for (unsigned i = 0; i != NumVars; ++i) 12208 Vars.push_back(Record.readSubExpr()); 12209 C->setVarRefs(Vars); 12210 Vars.clear(); 12211 for (unsigned i = 0; i != NumVars; ++i) 12212 Vars.push_back(Record.readSubExpr()); 12213 C->setPrivates(Vars); 12214 Vars.clear(); 12215 for (unsigned i = 0; i != NumVars; ++i) 12216 Vars.push_back(Record.readSubExpr()); 12217 C->setInits(Vars); 12218 Vars.clear(); 12219 for (unsigned i = 0; i != NumVars; ++i) 12220 Vars.push_back(Record.readSubExpr()); 12221 C->setUpdates(Vars); 12222 Vars.clear(); 12223 for (unsigned i = 0; i != NumVars; ++i) 12224 Vars.push_back(Record.readSubExpr()); 12225 C->setFinals(Vars); 12226 C->setStep(Record.readSubExpr()); 12227 C->setCalcStep(Record.readSubExpr()); 12228 Vars.clear(); 12229 for (unsigned I = 0; I != NumVars + 1; ++I) 12230 Vars.push_back(Record.readSubExpr()); 12231 C->setUsedExprs(Vars); 12232 } 12233 12234 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) { 12235 C->setLParenLoc(Record.readSourceLocation()); 12236 C->setColonLoc(Record.readSourceLocation()); 12237 unsigned NumVars = C->varlist_size(); 12238 SmallVector<Expr *, 16> Vars; 12239 Vars.reserve(NumVars); 12240 for (unsigned i = 0; i != NumVars; ++i) 12241 Vars.push_back(Record.readSubExpr()); 12242 C->setVarRefs(Vars); 12243 C->setAlignment(Record.readSubExpr()); 12244 } 12245 12246 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) { 12247 C->setLParenLoc(Record.readSourceLocation()); 12248 unsigned NumVars = C->varlist_size(); 12249 SmallVector<Expr *, 16> Exprs; 12250 Exprs.reserve(NumVars); 12251 for (unsigned i = 0; i != NumVars; ++i) 12252 Exprs.push_back(Record.readSubExpr()); 12253 C->setVarRefs(Exprs); 12254 Exprs.clear(); 12255 for (unsigned i = 0; i != NumVars; ++i) 12256 Exprs.push_back(Record.readSubExpr()); 12257 C->setSourceExprs(Exprs); 12258 Exprs.clear(); 12259 for (unsigned i = 0; i != NumVars; ++i) 12260 Exprs.push_back(Record.readSubExpr()); 12261 C->setDestinationExprs(Exprs); 12262 Exprs.clear(); 12263 for (unsigned i = 0; i != NumVars; ++i) 12264 Exprs.push_back(Record.readSubExpr()); 12265 C->setAssignmentOps(Exprs); 12266 } 12267 12268 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) { 12269 C->setLParenLoc(Record.readSourceLocation()); 12270 unsigned NumVars = C->varlist_size(); 12271 SmallVector<Expr *, 16> Exprs; 12272 Exprs.reserve(NumVars); 12273 for (unsigned i = 0; i != NumVars; ++i) 12274 Exprs.push_back(Record.readSubExpr()); 12275 C->setVarRefs(Exprs); 12276 Exprs.clear(); 12277 for (unsigned i = 0; i != NumVars; ++i) 12278 Exprs.push_back(Record.readSubExpr()); 12279 C->setSourceExprs(Exprs); 12280 Exprs.clear(); 12281 for (unsigned i = 0; i != NumVars; ++i) 12282 Exprs.push_back(Record.readSubExpr()); 12283 C->setDestinationExprs(Exprs); 12284 Exprs.clear(); 12285 for (unsigned i = 0; i != NumVars; ++i) 12286 Exprs.push_back(Record.readSubExpr()); 12287 C->setAssignmentOps(Exprs); 12288 } 12289 12290 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) { 12291 C->setLParenLoc(Record.readSourceLocation()); 12292 unsigned NumVars = C->varlist_size(); 12293 SmallVector<Expr *, 16> Vars; 12294 Vars.reserve(NumVars); 12295 for (unsigned i = 0; i != NumVars; ++i) 12296 Vars.push_back(Record.readSubExpr()); 12297 C->setVarRefs(Vars); 12298 } 12299 12300 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) { 12301 C->setDepobj(Record.readSubExpr()); 12302 C->setLParenLoc(Record.readSourceLocation()); 12303 } 12304 12305 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) { 12306 C->setLParenLoc(Record.readSourceLocation()); 12307 C->setDependencyKind( 12308 static_cast<OpenMPDependClauseKind>(Record.readInt())); 12309 C->setDependencyLoc(Record.readSourceLocation()); 12310 C->setColonLoc(Record.readSourceLocation()); 12311 unsigned NumVars = C->varlist_size(); 12312 SmallVector<Expr *, 16> Vars; 12313 Vars.reserve(NumVars); 12314 for (unsigned I = 0; I != NumVars; ++I) 12315 Vars.push_back(Record.readSubExpr()); 12316 C->setVarRefs(Vars); 12317 for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I) 12318 C->setLoopData(I, Record.readSubExpr()); 12319 } 12320 12321 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) { 12322 VisitOMPClauseWithPreInit(C); 12323 C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>()); 12324 C->setDevice(Record.readSubExpr()); 12325 C->setModifierLoc(Record.readSourceLocation()); 12326 C->setLParenLoc(Record.readSourceLocation()); 12327 } 12328 12329 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) { 12330 C->setLParenLoc(Record.readSourceLocation()); 12331 for (unsigned I = 0; I < OMPMapClause::NumberOfModifiers; ++I) { 12332 C->setMapTypeModifier( 12333 I, static_cast<OpenMPMapModifierKind>(Record.readInt())); 12334 C->setMapTypeModifierLoc(I, Record.readSourceLocation()); 12335 } 12336 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12337 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12338 C->setMapType( 12339 static_cast<OpenMPMapClauseKind>(Record.readInt())); 12340 C->setMapLoc(Record.readSourceLocation()); 12341 C->setColonLoc(Record.readSourceLocation()); 12342 auto NumVars = C->varlist_size(); 12343 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12344 auto TotalLists = C->getTotalComponentListNum(); 12345 auto TotalComponents = C->getTotalComponentsNum(); 12346 12347 SmallVector<Expr *, 16> Vars; 12348 Vars.reserve(NumVars); 12349 for (unsigned i = 0; i != NumVars; ++i) 12350 Vars.push_back(Record.readExpr()); 12351 C->setVarRefs(Vars); 12352 12353 SmallVector<Expr *, 16> UDMappers; 12354 UDMappers.reserve(NumVars); 12355 for (unsigned I = 0; I < NumVars; ++I) 12356 UDMappers.push_back(Record.readExpr()); 12357 C->setUDMapperRefs(UDMappers); 12358 12359 SmallVector<ValueDecl *, 16> Decls; 12360 Decls.reserve(UniqueDecls); 12361 for (unsigned i = 0; i < UniqueDecls; ++i) 12362 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12363 C->setUniqueDecls(Decls); 12364 12365 SmallVector<unsigned, 16> ListsPerDecl; 12366 ListsPerDecl.reserve(UniqueDecls); 12367 for (unsigned i = 0; i < UniqueDecls; ++i) 12368 ListsPerDecl.push_back(Record.readInt()); 12369 C->setDeclNumLists(ListsPerDecl); 12370 12371 SmallVector<unsigned, 32> ListSizes; 12372 ListSizes.reserve(TotalLists); 12373 for (unsigned i = 0; i < TotalLists; ++i) 12374 ListSizes.push_back(Record.readInt()); 12375 C->setComponentListSizes(ListSizes); 12376 12377 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12378 Components.reserve(TotalComponents); 12379 for (unsigned i = 0; i < TotalComponents; ++i) { 12380 Expr *AssociatedExpr = Record.readExpr(); 12381 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12382 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12383 AssociatedExpr, AssociatedDecl)); 12384 } 12385 C->setComponents(Components, ListSizes); 12386 } 12387 12388 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) { 12389 C->setLParenLoc(Record.readSourceLocation()); 12390 C->setColonLoc(Record.readSourceLocation()); 12391 C->setAllocator(Record.readSubExpr()); 12392 unsigned NumVars = C->varlist_size(); 12393 SmallVector<Expr *, 16> Vars; 12394 Vars.reserve(NumVars); 12395 for (unsigned i = 0; i != NumVars; ++i) 12396 Vars.push_back(Record.readSubExpr()); 12397 C->setVarRefs(Vars); 12398 } 12399 12400 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) { 12401 VisitOMPClauseWithPreInit(C); 12402 C->setNumTeams(Record.readSubExpr()); 12403 C->setLParenLoc(Record.readSourceLocation()); 12404 } 12405 12406 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) { 12407 VisitOMPClauseWithPreInit(C); 12408 C->setThreadLimit(Record.readSubExpr()); 12409 C->setLParenLoc(Record.readSourceLocation()); 12410 } 12411 12412 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) { 12413 VisitOMPClauseWithPreInit(C); 12414 C->setPriority(Record.readSubExpr()); 12415 C->setLParenLoc(Record.readSourceLocation()); 12416 } 12417 12418 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) { 12419 VisitOMPClauseWithPreInit(C); 12420 C->setGrainsize(Record.readSubExpr()); 12421 C->setLParenLoc(Record.readSourceLocation()); 12422 } 12423 12424 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) { 12425 VisitOMPClauseWithPreInit(C); 12426 C->setNumTasks(Record.readSubExpr()); 12427 C->setLParenLoc(Record.readSourceLocation()); 12428 } 12429 12430 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) { 12431 C->setHint(Record.readSubExpr()); 12432 C->setLParenLoc(Record.readSourceLocation()); 12433 } 12434 12435 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) { 12436 VisitOMPClauseWithPreInit(C); 12437 C->setDistScheduleKind( 12438 static_cast<OpenMPDistScheduleClauseKind>(Record.readInt())); 12439 C->setChunkSize(Record.readSubExpr()); 12440 C->setLParenLoc(Record.readSourceLocation()); 12441 C->setDistScheduleKindLoc(Record.readSourceLocation()); 12442 C->setCommaLoc(Record.readSourceLocation()); 12443 } 12444 12445 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) { 12446 C->setDefaultmapKind( 12447 static_cast<OpenMPDefaultmapClauseKind>(Record.readInt())); 12448 C->setDefaultmapModifier( 12449 static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt())); 12450 C->setLParenLoc(Record.readSourceLocation()); 12451 C->setDefaultmapModifierLoc(Record.readSourceLocation()); 12452 C->setDefaultmapKindLoc(Record.readSourceLocation()); 12453 } 12454 12455 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) { 12456 C->setLParenLoc(Record.readSourceLocation()); 12457 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12458 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12459 auto NumVars = C->varlist_size(); 12460 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12461 auto TotalLists = C->getTotalComponentListNum(); 12462 auto TotalComponents = C->getTotalComponentsNum(); 12463 12464 SmallVector<Expr *, 16> Vars; 12465 Vars.reserve(NumVars); 12466 for (unsigned i = 0; i != NumVars; ++i) 12467 Vars.push_back(Record.readSubExpr()); 12468 C->setVarRefs(Vars); 12469 12470 SmallVector<Expr *, 16> UDMappers; 12471 UDMappers.reserve(NumVars); 12472 for (unsigned I = 0; I < NumVars; ++I) 12473 UDMappers.push_back(Record.readSubExpr()); 12474 C->setUDMapperRefs(UDMappers); 12475 12476 SmallVector<ValueDecl *, 16> Decls; 12477 Decls.reserve(UniqueDecls); 12478 for (unsigned i = 0; i < UniqueDecls; ++i) 12479 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12480 C->setUniqueDecls(Decls); 12481 12482 SmallVector<unsigned, 16> ListsPerDecl; 12483 ListsPerDecl.reserve(UniqueDecls); 12484 for (unsigned i = 0; i < UniqueDecls; ++i) 12485 ListsPerDecl.push_back(Record.readInt()); 12486 C->setDeclNumLists(ListsPerDecl); 12487 12488 SmallVector<unsigned, 32> ListSizes; 12489 ListSizes.reserve(TotalLists); 12490 for (unsigned i = 0; i < TotalLists; ++i) 12491 ListSizes.push_back(Record.readInt()); 12492 C->setComponentListSizes(ListSizes); 12493 12494 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12495 Components.reserve(TotalComponents); 12496 for (unsigned i = 0; i < TotalComponents; ++i) { 12497 Expr *AssociatedExpr = Record.readSubExpr(); 12498 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12499 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12500 AssociatedExpr, AssociatedDecl)); 12501 } 12502 C->setComponents(Components, ListSizes); 12503 } 12504 12505 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) { 12506 C->setLParenLoc(Record.readSourceLocation()); 12507 C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc()); 12508 C->setMapperIdInfo(Record.readDeclarationNameInfo()); 12509 auto NumVars = C->varlist_size(); 12510 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12511 auto TotalLists = C->getTotalComponentListNum(); 12512 auto TotalComponents = C->getTotalComponentsNum(); 12513 12514 SmallVector<Expr *, 16> Vars; 12515 Vars.reserve(NumVars); 12516 for (unsigned i = 0; i != NumVars; ++i) 12517 Vars.push_back(Record.readSubExpr()); 12518 C->setVarRefs(Vars); 12519 12520 SmallVector<Expr *, 16> UDMappers; 12521 UDMappers.reserve(NumVars); 12522 for (unsigned I = 0; I < NumVars; ++I) 12523 UDMappers.push_back(Record.readSubExpr()); 12524 C->setUDMapperRefs(UDMappers); 12525 12526 SmallVector<ValueDecl *, 16> Decls; 12527 Decls.reserve(UniqueDecls); 12528 for (unsigned i = 0; i < UniqueDecls; ++i) 12529 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12530 C->setUniqueDecls(Decls); 12531 12532 SmallVector<unsigned, 16> ListsPerDecl; 12533 ListsPerDecl.reserve(UniqueDecls); 12534 for (unsigned i = 0; i < UniqueDecls; ++i) 12535 ListsPerDecl.push_back(Record.readInt()); 12536 C->setDeclNumLists(ListsPerDecl); 12537 12538 SmallVector<unsigned, 32> ListSizes; 12539 ListSizes.reserve(TotalLists); 12540 for (unsigned i = 0; i < TotalLists; ++i) 12541 ListSizes.push_back(Record.readInt()); 12542 C->setComponentListSizes(ListSizes); 12543 12544 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12545 Components.reserve(TotalComponents); 12546 for (unsigned i = 0; i < TotalComponents; ++i) { 12547 Expr *AssociatedExpr = Record.readSubExpr(); 12548 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12549 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12550 AssociatedExpr, AssociatedDecl)); 12551 } 12552 C->setComponents(Components, ListSizes); 12553 } 12554 12555 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) { 12556 C->setLParenLoc(Record.readSourceLocation()); 12557 auto NumVars = C->varlist_size(); 12558 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12559 auto TotalLists = C->getTotalComponentListNum(); 12560 auto TotalComponents = C->getTotalComponentsNum(); 12561 12562 SmallVector<Expr *, 16> Vars; 12563 Vars.reserve(NumVars); 12564 for (unsigned i = 0; i != NumVars; ++i) 12565 Vars.push_back(Record.readSubExpr()); 12566 C->setVarRefs(Vars); 12567 Vars.clear(); 12568 for (unsigned i = 0; i != NumVars; ++i) 12569 Vars.push_back(Record.readSubExpr()); 12570 C->setPrivateCopies(Vars); 12571 Vars.clear(); 12572 for (unsigned i = 0; i != NumVars; ++i) 12573 Vars.push_back(Record.readSubExpr()); 12574 C->setInits(Vars); 12575 12576 SmallVector<ValueDecl *, 16> Decls; 12577 Decls.reserve(UniqueDecls); 12578 for (unsigned i = 0; i < UniqueDecls; ++i) 12579 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12580 C->setUniqueDecls(Decls); 12581 12582 SmallVector<unsigned, 16> ListsPerDecl; 12583 ListsPerDecl.reserve(UniqueDecls); 12584 for (unsigned i = 0; i < UniqueDecls; ++i) 12585 ListsPerDecl.push_back(Record.readInt()); 12586 C->setDeclNumLists(ListsPerDecl); 12587 12588 SmallVector<unsigned, 32> ListSizes; 12589 ListSizes.reserve(TotalLists); 12590 for (unsigned i = 0; i < TotalLists; ++i) 12591 ListSizes.push_back(Record.readInt()); 12592 C->setComponentListSizes(ListSizes); 12593 12594 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12595 Components.reserve(TotalComponents); 12596 for (unsigned i = 0; i < TotalComponents; ++i) { 12597 Expr *AssociatedExpr = Record.readSubExpr(); 12598 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12599 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12600 AssociatedExpr, AssociatedDecl)); 12601 } 12602 C->setComponents(Components, ListSizes); 12603 } 12604 12605 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) { 12606 C->setLParenLoc(Record.readSourceLocation()); 12607 auto NumVars = C->varlist_size(); 12608 auto UniqueDecls = C->getUniqueDeclarationsNum(); 12609 auto TotalLists = C->getTotalComponentListNum(); 12610 auto TotalComponents = C->getTotalComponentsNum(); 12611 12612 SmallVector<Expr *, 16> Vars; 12613 Vars.reserve(NumVars); 12614 for (unsigned i = 0; i != NumVars; ++i) 12615 Vars.push_back(Record.readSubExpr()); 12616 C->setVarRefs(Vars); 12617 Vars.clear(); 12618 12619 SmallVector<ValueDecl *, 16> Decls; 12620 Decls.reserve(UniqueDecls); 12621 for (unsigned i = 0; i < UniqueDecls; ++i) 12622 Decls.push_back(Record.readDeclAs<ValueDecl>()); 12623 C->setUniqueDecls(Decls); 12624 12625 SmallVector<unsigned, 16> ListsPerDecl; 12626 ListsPerDecl.reserve(UniqueDecls); 12627 for (unsigned i = 0; i < UniqueDecls; ++i) 12628 ListsPerDecl.push_back(Record.readInt()); 12629 C->setDeclNumLists(ListsPerDecl); 12630 12631 SmallVector<unsigned, 32> ListSizes; 12632 ListSizes.reserve(TotalLists); 12633 for (unsigned i = 0; i < TotalLists; ++i) 12634 ListSizes.push_back(Record.readInt()); 12635 C->setComponentListSizes(ListSizes); 12636 12637 SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components; 12638 Components.reserve(TotalComponents); 12639 for (unsigned i = 0; i < TotalComponents; ++i) { 12640 Expr *AssociatedExpr = Record.readSubExpr(); 12641 auto *AssociatedDecl = Record.readDeclAs<ValueDecl>(); 12642 Components.push_back(OMPClauseMappableExprCommon::MappableComponent( 12643 AssociatedExpr, AssociatedDecl)); 12644 } 12645 C->setComponents(Components, ListSizes); 12646 } 12647 12648 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) { 12649 C->setLParenLoc(Record.readSourceLocation()); 12650 unsigned NumVars = C->varlist_size(); 12651 SmallVector<Expr *, 16> Vars; 12652 Vars.reserve(NumVars); 12653 for (unsigned i = 0; i != NumVars; ++i) 12654 Vars.push_back(Record.readSubExpr()); 12655 C->setVarRefs(Vars); 12656 Vars.clear(); 12657 Vars.reserve(NumVars); 12658 for (unsigned i = 0; i != NumVars; ++i) 12659 Vars.push_back(Record.readSubExpr()); 12660 C->setPrivateRefs(Vars); 12661 } 12662 12663 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) { 12664 C->setLParenLoc(Record.readSourceLocation()); 12665 unsigned NumVars = C->varlist_size(); 12666 SmallVector<Expr *, 16> Vars; 12667 Vars.reserve(NumVars); 12668 for (unsigned i = 0; i != NumVars; ++i) 12669 Vars.push_back(Record.readSubExpr()); 12670 C->setVarRefs(Vars); 12671 } 12672 12673 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) { 12674 C->setLParenLoc(Record.readSourceLocation()); 12675 unsigned NumVars = C->varlist_size(); 12676 SmallVector<Expr *, 16> Vars; 12677 Vars.reserve(NumVars); 12678 for (unsigned i = 0; i != NumVars; ++i) 12679 Vars.push_back(Record.readSubExpr()); 12680 C->setVarRefs(Vars); 12681 } 12682 12683 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) { 12684 C->setKind(Record.readEnum<OpenMPOrderClauseKind>()); 12685 C->setLParenLoc(Record.readSourceLocation()); 12686 C->setKindKwLoc(Record.readSourceLocation()); 12687 } 12688 12689 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() { 12690 OMPTraitInfo &TI = getContext().getNewOMPTraitInfo(); 12691 TI.Sets.resize(readUInt32()); 12692 for (auto &Set : TI.Sets) { 12693 Set.Kind = readEnum<llvm::omp::TraitSet>(); 12694 Set.Selectors.resize(readUInt32()); 12695 for (auto &Selector : Set.Selectors) { 12696 Selector.Kind = readEnum<llvm::omp::TraitSelector>(); 12697 Selector.ScoreOrCondition = nullptr; 12698 if (readBool()) 12699 Selector.ScoreOrCondition = readExprRef(); 12700 Selector.Properties.resize(readUInt32()); 12701 for (auto &Property : Selector.Properties) 12702 Property.Kind = readEnum<llvm::omp::TraitProperty>(); 12703 } 12704 } 12705 return &TI; 12706 } 12707