brintos

brintos / llvm-project-archived public Read only

0
0
Text · 470.4 KiB · aec6132 Raw
13224 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9//  This file defines the ASTReader class, which reads AST files.10//11//===----------------------------------------------------------------------===//12 13#include "ASTCommon.h"14#include "ASTReaderInternals.h"15#include "TemplateArgumentHasher.h"16#include "clang/AST/ASTConsumer.h"17#include "clang/AST/ASTContext.h"18#include "clang/AST/ASTMutationListener.h"19#include "clang/AST/ASTStructuralEquivalence.h"20#include "clang/AST/ASTUnresolvedSet.h"21#include "clang/AST/AbstractTypeReader.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/ODRDiagsEmitter.h"35#include "clang/AST/OpenACCClause.h"36#include "clang/AST/OpenMPClause.h"37#include "clang/AST/RawCommentList.h"38#include "clang/AST/TemplateBase.h"39#include "clang/AST/TemplateName.h"40#include "clang/AST/Type.h"41#include "clang/AST/TypeLoc.h"42#include "clang/AST/TypeLocVisitor.h"43#include "clang/AST/UnresolvedSet.h"44#include "clang/Basic/ASTSourceDescriptor.h"45#include "clang/Basic/CommentOptions.h"46#include "clang/Basic/Diagnostic.h"47#include "clang/Basic/DiagnosticIDs.h"48#include "clang/Basic/DiagnosticOptions.h"49#include "clang/Basic/DiagnosticSema.h"50#include "clang/Basic/FileManager.h"51#include "clang/Basic/FileSystemOptions.h"52#include "clang/Basic/IdentifierTable.h"53#include "clang/Basic/LLVM.h"54#include "clang/Basic/LangOptions.h"55#include "clang/Basic/Module.h"56#include "clang/Basic/ObjCRuntime.h"57#include "clang/Basic/OpenACCKinds.h"58#include "clang/Basic/OpenMPKinds.h"59#include "clang/Basic/OperatorKinds.h"60#include "clang/Basic/PragmaKinds.h"61#include "clang/Basic/Sanitizers.h"62#include "clang/Basic/SourceLocation.h"63#include "clang/Basic/SourceManager.h"64#include "clang/Basic/SourceManagerInternals.h"65#include "clang/Basic/Specifiers.h"66#include "clang/Basic/TargetInfo.h"67#include "clang/Basic/TargetOptions.h"68#include "clang/Basic/TokenKinds.h"69#include "clang/Basic/Version.h"70#include "clang/Lex/HeaderSearch.h"71#include "clang/Lex/HeaderSearchOptions.h"72#include "clang/Lex/MacroInfo.h"73#include "clang/Lex/ModuleMap.h"74#include "clang/Lex/PreprocessingRecord.h"75#include "clang/Lex/Preprocessor.h"76#include "clang/Lex/PreprocessorOptions.h"77#include "clang/Lex/Token.h"78#include "clang/Sema/ObjCMethodList.h"79#include "clang/Sema/Scope.h"80#include "clang/Sema/Sema.h"81#include "clang/Sema/SemaCUDA.h"82#include "clang/Sema/SemaObjC.h"83#include "clang/Sema/Weak.h"84#include "clang/Serialization/ASTBitCodes.h"85#include "clang/Serialization/ASTDeserializationListener.h"86#include "clang/Serialization/ASTRecordReader.h"87#include "clang/Serialization/ContinuousRangeMap.h"88#include "clang/Serialization/GlobalModuleIndex.h"89#include "clang/Serialization/InMemoryModuleCache.h"90#include "clang/Serialization/ModuleCache.h"91#include "clang/Serialization/ModuleFile.h"92#include "clang/Serialization/ModuleFileExtension.h"93#include "clang/Serialization/ModuleManager.h"94#include "clang/Serialization/PCHContainerOperations.h"95#include "clang/Serialization/SerializationDiagnostic.h"96#include "llvm/ADT/APFloat.h"97#include "llvm/ADT/APInt.h"98#include "llvm/ADT/ArrayRef.h"99#include "llvm/ADT/DenseMap.h"100#include "llvm/ADT/FoldingSet.h"101#include "llvm/ADT/IntrusiveRefCntPtr.h"102#include "llvm/ADT/STLExtras.h"103#include "llvm/ADT/ScopeExit.h"104#include "llvm/ADT/Sequence.h"105#include "llvm/ADT/SmallPtrSet.h"106#include "llvm/ADT/SmallVector.h"107#include "llvm/ADT/StringExtras.h"108#include "llvm/ADT/StringMap.h"109#include "llvm/ADT/StringRef.h"110#include "llvm/ADT/iterator_range.h"111#include "llvm/Bitstream/BitstreamReader.h"112#include "llvm/Support/Compiler.h"113#include "llvm/Support/Compression.h"114#include "llvm/Support/DJB.h"115#include "llvm/Support/Endian.h"116#include "llvm/Support/Error.h"117#include "llvm/Support/ErrorHandling.h"118#include "llvm/Support/LEB128.h"119#include "llvm/Support/MemoryBuffer.h"120#include "llvm/Support/Path.h"121#include "llvm/Support/SaveAndRestore.h"122#include "llvm/Support/TimeProfiler.h"123#include "llvm/Support/Timer.h"124#include "llvm/Support/VersionTuple.h"125#include "llvm/Support/raw_ostream.h"126#include "llvm/TargetParser/Triple.h"127#include <algorithm>128#include <cassert>129#include <cstddef>130#include <cstdint>131#include <cstdio>132#include <ctime>133#include <iterator>134#include <limits>135#include <map>136#include <memory>137#include <optional>138#include <string>139#include <system_error>140#include <tuple>141#include <utility>142#include <vector>143 144using namespace clang;145using namespace clang::serialization;146using namespace clang::serialization::reader;147using llvm::BitstreamCursor;148 149//===----------------------------------------------------------------------===//150// ChainedASTReaderListener implementation151//===----------------------------------------------------------------------===//152 153bool154ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {155  return First->ReadFullVersionInformation(FullVersion) ||156         Second->ReadFullVersionInformation(FullVersion);157}158 159void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {160  First->ReadModuleName(ModuleName);161  Second->ReadModuleName(ModuleName);162}163 164void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {165  First->ReadModuleMapFile(ModuleMapPath);166  Second->ReadModuleMapFile(ModuleMapPath);167}168 169bool ChainedASTReaderListener::ReadLanguageOptions(170    const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain,171    bool AllowCompatibleDifferences) {172  return First->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,173                                    AllowCompatibleDifferences) ||174         Second->ReadLanguageOptions(LangOpts, ModuleFilename, Complain,175                                     AllowCompatibleDifferences);176}177 178bool ChainedASTReaderListener::ReadCodeGenOptions(179    const CodeGenOptions &CGOpts, StringRef ModuleFilename, bool Complain,180    bool AllowCompatibleDifferences) {181  return First->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,182                                   AllowCompatibleDifferences) ||183         Second->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,184                                    AllowCompatibleDifferences);185}186 187bool ChainedASTReaderListener::ReadTargetOptions(188    const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain,189    bool AllowCompatibleDifferences) {190  return First->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,191                                  AllowCompatibleDifferences) ||192         Second->ReadTargetOptions(TargetOpts, ModuleFilename, Complain,193                                   AllowCompatibleDifferences);194}195 196bool ChainedASTReaderListener::ReadDiagnosticOptions(197    DiagnosticOptions &DiagOpts, StringRef ModuleFilename, bool Complain) {198  return First->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain) ||199         Second->ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);200}201 202bool203ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,204                                                bool Complain) {205  return First->ReadFileSystemOptions(FSOpts, Complain) ||206         Second->ReadFileSystemOptions(FSOpts, Complain);207}208 209bool ChainedASTReaderListener::ReadHeaderSearchOptions(210    const HeaderSearchOptions &HSOpts, StringRef ModuleFilename,211    StringRef SpecificModuleCachePath, bool Complain) {212  return First->ReadHeaderSearchOptions(HSOpts, ModuleFilename,213                                        SpecificModuleCachePath, Complain) ||214         Second->ReadHeaderSearchOptions(HSOpts, ModuleFilename,215                                         SpecificModuleCachePath, Complain);216}217 218bool ChainedASTReaderListener::ReadPreprocessorOptions(219    const PreprocessorOptions &PPOpts, StringRef ModuleFilename,220    bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {221  return First->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,222                                        Complain, SuggestedPredefines) ||223         Second->ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,224                                         Complain, SuggestedPredefines);225}226 227void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,228                                           uint32_t Value) {229  First->ReadCounter(M, Value);230  Second->ReadCounter(M, Value);231}232 233bool ChainedASTReaderListener::needsInputFileVisitation() {234  return First->needsInputFileVisitation() ||235         Second->needsInputFileVisitation();236}237 238bool ChainedASTReaderListener::needsSystemInputFileVisitation() {239  return First->needsSystemInputFileVisitation() ||240  Second->needsSystemInputFileVisitation();241}242 243void ChainedASTReaderListener::visitModuleFile(StringRef Filename,244                                               ModuleKind Kind) {245  First->visitModuleFile(Filename, Kind);246  Second->visitModuleFile(Filename, Kind);247}248 249bool ChainedASTReaderListener::visitInputFile(StringRef Filename,250                                              bool isSystem,251                                              bool isOverridden,252                                              bool isExplicitModule) {253  bool Continue = false;254  if (First->needsInputFileVisitation() &&255      (!isSystem || First->needsSystemInputFileVisitation()))256    Continue |= First->visitInputFile(Filename, isSystem, isOverridden,257                                      isExplicitModule);258  if (Second->needsInputFileVisitation() &&259      (!isSystem || Second->needsSystemInputFileVisitation()))260    Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,261                                       isExplicitModule);262  return Continue;263}264 265void ChainedASTReaderListener::readModuleFileExtension(266       const ModuleFileExtensionMetadata &Metadata) {267  First->readModuleFileExtension(Metadata);268  Second->readModuleFileExtension(Metadata);269}270 271//===----------------------------------------------------------------------===//272// PCH validator implementation273//===----------------------------------------------------------------------===//274 275ASTReaderListener::~ASTReaderListener() = default;276 277/// Compare the given set of language options against an existing set of278/// language options.279///280/// \param Diags If non-NULL, diagnostics will be emitted via this engine.281/// \param AllowCompatibleDifferences If true, differences between compatible282///        language options will be permitted.283///284/// \returns true if the languagae options mis-match, false otherwise.285static bool checkLanguageOptions(const LangOptions &LangOpts,286                                 const LangOptions &ExistingLangOpts,287                                 StringRef ModuleFilename,288                                 DiagnosticsEngine *Diags,289                                 bool AllowCompatibleDifferences = true) {290  // FIXME: Replace with C++20 `using enum LangOptions::CompatibilityKind`.291  using CK = LangOptions::CompatibilityKind;292 293#define LANGOPT(Name, Bits, Default, Compatibility, Description)               \294  if constexpr (CK::Compatibility != CK::Benign) {                             \295    if ((CK::Compatibility == CK::NotCompatible) ||                            \296        (CK::Compatibility == CK::Compatible &&                                \297         !AllowCompatibleDifferences)) {                                       \298      if (ExistingLangOpts.Name != LangOpts.Name) {                            \299        if (Diags) {                                                           \300          if (Bits == 1)                                                       \301            Diags->Report(diag::err_ast_file_langopt_mismatch)                 \302                << Description << LangOpts.Name << ExistingLangOpts.Name       \303                << ModuleFilename;                                             \304          else                                                                 \305            Diags->Report(diag::err_ast_file_langopt_value_mismatch)           \306                << Description << ModuleFilename;                              \307        }                                                                      \308        return true;                                                           \309      }                                                                        \310    }                                                                          \311  }312 313#define VALUE_LANGOPT(Name, Bits, Default, Compatibility, Description)         \314  if constexpr (CK::Compatibility != CK::Benign) {                             \315    if ((CK::Compatibility == CK::NotCompatible) ||                            \316        (CK::Compatibility == CK::Compatible &&                                \317         !AllowCompatibleDifferences)) {                                       \318      if (ExistingLangOpts.Name != LangOpts.Name) {                            \319        if (Diags)                                                             \320          Diags->Report(diag::err_ast_file_langopt_value_mismatch)             \321              << Description << ModuleFilename;                                \322        return true;                                                           \323      }                                                                        \324    }                                                                          \325  }326 327#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description)    \328  if constexpr (CK::Compatibility != CK::Benign) {                             \329    if ((CK::Compatibility == CK::NotCompatible) ||                            \330        (CK::Compatibility == CK::Compatible &&                                \331         !AllowCompatibleDifferences)) {                                       \332      if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {              \333        if (Diags)                                                             \334          Diags->Report(diag::err_ast_file_langopt_value_mismatch)             \335              << Description << ModuleFilename;                                \336        return true;                                                           \337      }                                                                        \338    }                                                                          \339  }340 341#include "clang/Basic/LangOptions.def"342 343  if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {344    if (Diags)345      Diags->Report(diag::err_ast_file_langopt_value_mismatch)346          << "module features" << ModuleFilename;347    return true;348  }349 350  if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {351    if (Diags)352      Diags->Report(diag::err_ast_file_langopt_value_mismatch)353          << "target Objective-C runtime" << ModuleFilename;354    return true;355  }356 357  if (ExistingLangOpts.CommentOpts.BlockCommandNames !=358      LangOpts.CommentOpts.BlockCommandNames) {359    if (Diags)360      Diags->Report(diag::err_ast_file_langopt_value_mismatch)361          << "block command names" << ModuleFilename;362    return true;363  }364 365  // Sanitizer feature mismatches are treated as compatible differences. If366  // compatible differences aren't allowed, we still only want to check for367  // mismatches of non-modular sanitizers (the only ones which can affect AST368  // generation).369  if (!AllowCompatibleDifferences) {370    SanitizerMask ModularSanitizers = getPPTransparentSanitizers();371    SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;372    SanitizerSet ImportedSanitizers = LangOpts.Sanitize;373    ExistingSanitizers.clear(ModularSanitizers);374    ImportedSanitizers.clear(ModularSanitizers);375    if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {376      const std::string Flag = "-fsanitize=";377      if (Diags) {378#define SANITIZER(NAME, ID)                                                    \379  {                                                                            \380    bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \381    bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \382    if (InExistingModule != InImportedModule)                                  \383      Diags->Report(diag::err_ast_file_targetopt_feature_mismatch)             \384          << InExistingModule << ModuleFilename << (Flag + NAME);              \385  }386#include "clang/Basic/Sanitizers.def"387      }388      return true;389    }390  }391 392  return false;393}394 395static bool checkCodegenOptions(const CodeGenOptions &CGOpts,396                                const CodeGenOptions &ExistingCGOpts,397                                StringRef ModuleFilename,398                                DiagnosticsEngine *Diags,399                                bool AllowCompatibleDifferences = true) {400  // FIXME: Specify and print a description for each option instead of the name.401  // FIXME: Replace with C++20 `using enum CodeGenOptions::CompatibilityKind`.402  using CK = CodeGenOptions::CompatibilityKind;403#define CODEGENOPT(Name, Bits, Default, Compatibility)                         \404  if constexpr (CK::Compatibility != CK::Benign) {                             \405    if ((CK::Compatibility == CK::NotCompatible) ||                            \406        (CK::Compatibility == CK::Compatible &&                                \407         !AllowCompatibleDifferences)) {                                       \408      if (ExistingCGOpts.Name != CGOpts.Name) {                                \409        if (Diags) {                                                           \410          if (Bits == 1)                                                       \411            Diags->Report(diag::err_ast_file_codegenopt_mismatch)              \412                << #Name << CGOpts.Name << ExistingCGOpts.Name                 \413                << ModuleFilename;                                             \414          else                                                                 \415            Diags->Report(diag::err_ast_file_codegenopt_value_mismatch)        \416                << #Name << ModuleFilename;                                    \417        }                                                                      \418        return true;                                                           \419      }                                                                        \420    }                                                                          \421  }422 423#define VALUE_CODEGENOPT(Name, Bits, Default, Compatibility)                   \424  if constexpr (CK::Compatibility != CK::Benign) {                             \425    if ((CK::Compatibility == CK::NotCompatible) ||                            \426        (CK::Compatibility == CK::Compatible &&                                \427         !AllowCompatibleDifferences)) {                                       \428      if (ExistingCGOpts.Name != CGOpts.Name) {                                \429        if (Diags)                                                             \430          Diags->Report(diag::err_ast_file_codegenopt_value_mismatch)          \431              << #Name << ModuleFilename;                                      \432        return true;                                                           \433      }                                                                        \434    }                                                                          \435  }436#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility)              \437  if constexpr (CK::Compatibility != CK::Benign) {                             \438    if ((CK::Compatibility == CK::NotCompatible) ||                            \439        (CK::Compatibility == CK::Compatible &&                                \440         !AllowCompatibleDifferences)) {                                       \441      if (ExistingCGOpts.get##Name() != CGOpts.get##Name()) {                  \442        if (Diags)                                                             \443          Diags->Report(diag::err_ast_file_codegenopt_value_mismatch)          \444              << #Name << ModuleFilename;                                      \445        return true;                                                           \446      }                                                                        \447    }                                                                          \448  }449#define DEBUGOPT(Name, Bits, Default, Compatibility)450#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)451#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)452#include "clang/Basic/CodeGenOptions.def"453 454  return false;455}456 457/// Compare the given set of target options against an existing set of458/// target options.459///460/// \param Diags If non-NULL, diagnostics will be emitted via this engine.461///462/// \returns true if the target options mis-match, false otherwise.463static bool checkTargetOptions(const TargetOptions &TargetOpts,464                               const TargetOptions &ExistingTargetOpts,465                               StringRef ModuleFilename,466                               DiagnosticsEngine *Diags,467                               bool AllowCompatibleDifferences = true) {468#define CHECK_TARGET_OPT(Field, Name)                                          \469  if (TargetOpts.Field != ExistingTargetOpts.Field) {                          \470    if (Diags)                                                                 \471      Diags->Report(diag::err_ast_file_targetopt_mismatch)                     \472          << ModuleFilename << Name << TargetOpts.Field                        \473          << ExistingTargetOpts.Field;                                         \474    return true;                                                               \475  }476 477  // The triple and ABI must match exactly.478  CHECK_TARGET_OPT(Triple, "target");479  CHECK_TARGET_OPT(ABI, "target ABI");480 481  // We can tolerate different CPUs in many cases, notably when one CPU482  // supports a strict superset of another. When allowing compatible483  // differences skip this check.484  if (!AllowCompatibleDifferences) {485    CHECK_TARGET_OPT(CPU, "target CPU");486    CHECK_TARGET_OPT(TuneCPU, "tune CPU");487  }488 489#undef CHECK_TARGET_OPT490 491  // Compare feature sets.492  SmallVector<StringRef, 4> ExistingFeatures(493                                             ExistingTargetOpts.FeaturesAsWritten.begin(),494                                             ExistingTargetOpts.FeaturesAsWritten.end());495  SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),496                                         TargetOpts.FeaturesAsWritten.end());497  llvm::sort(ExistingFeatures);498  llvm::sort(ReadFeatures);499 500  // We compute the set difference in both directions explicitly so that we can501  // diagnose the differences differently.502  SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;503  std::set_difference(504      ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),505      ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));506  std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),507                      ExistingFeatures.begin(), ExistingFeatures.end(),508                      std::back_inserter(UnmatchedReadFeatures));509 510  // If we are allowing compatible differences and the read feature set is511  // a strict subset of the existing feature set, there is nothing to diagnose.512  if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())513    return false;514 515  if (Diags) {516    for (StringRef Feature : UnmatchedReadFeatures)517      Diags->Report(diag::err_ast_file_targetopt_feature_mismatch)518          << /* is-existing-feature */ false << ModuleFilename << Feature;519    for (StringRef Feature : UnmatchedExistingFeatures)520      Diags->Report(diag::err_ast_file_targetopt_feature_mismatch)521          << /* is-existing-feature */ true << ModuleFilename << Feature;522  }523 524  return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();525}526 527bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,528                                       StringRef ModuleFilename, bool Complain,529                                       bool AllowCompatibleDifferences) {530  const LangOptions &ExistingLangOpts = PP.getLangOpts();531  return checkLanguageOptions(LangOpts, ExistingLangOpts, ModuleFilename,532                              Complain ? &Reader.Diags : nullptr,533                              AllowCompatibleDifferences);534}535 536bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts,537                                      StringRef ModuleFilename, bool Complain,538                                      bool AllowCompatibleDifferences) {539  const CodeGenOptions &ExistingCGOpts = Reader.getCodeGenOpts();540  return checkCodegenOptions(ExistingCGOpts, CGOpts, ModuleFilename,541                             Complain ? &Reader.Diags : nullptr,542                             AllowCompatibleDifferences);543}544 545bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,546                                     StringRef ModuleFilename, bool Complain,547                                     bool AllowCompatibleDifferences) {548  const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();549  return checkTargetOptions(TargetOpts, ExistingTargetOpts, ModuleFilename,550                            Complain ? &Reader.Diags : nullptr,551                            AllowCompatibleDifferences);552}553 554namespace {555 556using MacroDefinitionsMap =557    llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;558using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;559 560} // namespace561 562static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,563                                         DiagnosticsEngine &Diags,564                                         StringRef ModuleFilename,565                                         bool Complain) {566  using Level = DiagnosticsEngine::Level;567 568  // Check current mappings for new -Werror mappings, and the stored mappings569  // for cases that were explicitly mapped to *not* be errors that are now570  // errors because of options like -Werror.571  DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };572 573  for (DiagnosticsEngine *MappingSource : MappingSources) {574    for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {575      diag::kind DiagID = DiagIDMappingPair.first;576      Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());577      if (CurLevel < DiagnosticsEngine::Error)578        continue; // not significant579      Level StoredLevel =580          StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());581      if (StoredLevel < DiagnosticsEngine::Error) {582        if (Complain)583          Diags.Report(diag::err_ast_file_diagopt_mismatch)584              << "-Werror=" + Diags.getDiagnosticIDs()585                                  ->getWarningOptionForDiag(DiagID)586                                  .str()587              << ModuleFilename;588        return true;589      }590    }591  }592 593  return false;594}595 596static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {597  diag::Severity Ext = Diags.getExtensionHandlingBehavior();598  if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())599    return true;600  return Ext >= diag::Severity::Error;601}602 603static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,604                                    DiagnosticsEngine &Diags,605                                    StringRef ModuleFilename, bool IsSystem,606                                    bool SystemHeaderWarningsInModule,607                                    bool Complain) {608  // Top-level options609  if (IsSystem) {610    if (Diags.getSuppressSystemWarnings())611      return false;612    // If -Wsystem-headers was not enabled before, and it was not explicit,613    // be conservative614    if (StoredDiags.getSuppressSystemWarnings() &&615        !SystemHeaderWarningsInModule) {616      if (Complain)617        Diags.Report(diag::err_ast_file_diagopt_mismatch)618            << "-Wsystem-headers" << ModuleFilename;619      return true;620    }621  }622 623  if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {624    if (Complain)625      Diags.Report(diag::err_ast_file_diagopt_mismatch)626          << "-Werror" << ModuleFilename;627    return true;628  }629 630  if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&631      !StoredDiags.getEnableAllWarnings()) {632    if (Complain)633      Diags.Report(diag::err_ast_file_diagopt_mismatch)634          << "-Weverything -Werror" << ModuleFilename;635    return true;636  }637 638  if (isExtHandlingFromDiagsError(Diags) &&639      !isExtHandlingFromDiagsError(StoredDiags)) {640    if (Complain)641      Diags.Report(diag::err_ast_file_diagopt_mismatch)642          << "-pedantic-errors" << ModuleFilename;643    return true;644  }645 646  return checkDiagnosticGroupMappings(StoredDiags, Diags, ModuleFilename,647                                      Complain);648}649 650/// Return the top import module if it is implicit, nullptr otherwise.651static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,652                                          Preprocessor &PP) {653  // If the original import came from a file explicitly generated by the user,654  // don't check the diagnostic mappings.655  // FIXME: currently this is approximated by checking whether this is not a656  // module import of an implicitly-loaded module file.657  // Note: ModuleMgr.rbegin() may not be the current module, but it must be in658  // the transitive closure of its imports, since unrelated modules cannot be659  // imported until after this module finishes validation.660  ModuleFile *TopImport = &*ModuleMgr.rbegin();661  while (!TopImport->ImportedBy.empty())662    TopImport = TopImport->ImportedBy[0];663  if (TopImport->Kind != MK_ImplicitModule)664    return nullptr;665 666  StringRef ModuleName = TopImport->ModuleName;667  assert(!ModuleName.empty() && "diagnostic options read before module name");668 669  Module *M =670      PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);671  assert(M && "missing module");672  return M;673}674 675bool PCHValidator::ReadDiagnosticOptions(DiagnosticOptions &DiagOpts,676                                         StringRef ModuleFilename,677                                         bool Complain) {678  DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();679  IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());680  auto Diags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(DiagIDs, DiagOpts);681  // This should never fail, because we would have processed these options682  // before writing them to an ASTFile.683  ProcessWarningOptions(*Diags, DiagOpts,684                        PP.getFileManager().getVirtualFileSystem(),685                        /*Report*/ false);686 687  ModuleManager &ModuleMgr = Reader.getModuleManager();688  assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");689 690  Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);691  if (!TopM)692    return false;693 694  Module *Importer = PP.getCurrentModule();695 696  DiagnosticOptions &ExistingOpts = ExistingDiags.getDiagnosticOptions();697  bool SystemHeaderWarningsInModule =698      Importer && llvm::is_contained(ExistingOpts.SystemHeaderWarningsModules,699                                     Importer->Name);700 701  // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that702  // contains the union of their flags.703  return checkDiagnosticMappings(*Diags, ExistingDiags, ModuleFilename,704                                 TopM->IsSystem, SystemHeaderWarningsInModule,705                                 Complain);706}707 708/// Collect the macro definitions provided by the given preprocessor709/// options.710static void711collectMacroDefinitions(const PreprocessorOptions &PPOpts,712                        MacroDefinitionsMap &Macros,713                        SmallVectorImpl<StringRef> *MacroNames = nullptr) {714  for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {715    StringRef Macro = PPOpts.Macros[I].first;716    bool IsUndef = PPOpts.Macros[I].second;717 718    std::pair<StringRef, StringRef> MacroPair = Macro.split('=');719    StringRef MacroName = MacroPair.first;720    StringRef MacroBody = MacroPair.second;721 722    // For an #undef'd macro, we only care about the name.723    if (IsUndef) {724      auto [It, Inserted] = Macros.try_emplace(MacroName);725      if (MacroNames && Inserted)726        MacroNames->push_back(MacroName);727 728      It->second = std::make_pair("", true);729      continue;730    }731 732    // For a #define'd macro, figure out the actual definition.733    if (MacroName.size() == Macro.size())734      MacroBody = "1";735    else {736      // Note: GCC drops anything following an end-of-line character.737      StringRef::size_type End = MacroBody.find_first_of("\n\r");738      MacroBody = MacroBody.substr(0, End);739    }740 741    auto [It, Inserted] = Macros.try_emplace(MacroName);742    if (MacroNames && Inserted)743      MacroNames->push_back(MacroName);744    It->second = std::make_pair(MacroBody, false);745  }746}747 748enum OptionValidation {749  OptionValidateNone,750  OptionValidateContradictions,751  OptionValidateStrictMatches,752};753 754/// Check the preprocessor options deserialized from the control block755/// against the preprocessor options in an existing preprocessor.756///757/// \param Diags If non-null, produce diagnostics for any mismatches incurred.758/// \param Validation If set to OptionValidateNone, ignore differences in759///        preprocessor options. If set to OptionValidateContradictions,760///        require that options passed both in the AST file and on the command761///        line (-D or -U) match, but tolerate options missing in one or the762///        other. If set to OptionValidateContradictions, require that there763///        are no differences in the options between the two.764static bool checkPreprocessorOptions(765    const PreprocessorOptions &PPOpts,766    const PreprocessorOptions &ExistingPPOpts, StringRef ModuleFilename,767    bool ReadMacros, DiagnosticsEngine *Diags, FileManager &FileMgr,768    std::string &SuggestedPredefines, const LangOptions &LangOpts,769    OptionValidation Validation = OptionValidateContradictions) {770  if (ReadMacros) {771    // Check macro definitions.772    MacroDefinitionsMap ASTFileMacros;773    collectMacroDefinitions(PPOpts, ASTFileMacros);774    MacroDefinitionsMap ExistingMacros;775    SmallVector<StringRef, 4> ExistingMacroNames;776    collectMacroDefinitions(ExistingPPOpts, ExistingMacros,777                            &ExistingMacroNames);778 779    // Use a line marker to enter the <command line> file, as the defines and780    // undefines here will have come from the command line.781    SuggestedPredefines += "# 1 \"<command line>\" 1\n";782 783    for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {784      // Dig out the macro definition in the existing preprocessor options.785      StringRef MacroName = ExistingMacroNames[I];786      std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];787 788      // Check whether we know anything about this macro name or not.789      llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =790          ASTFileMacros.find(MacroName);791      if (Validation == OptionValidateNone || Known == ASTFileMacros.end()) {792        if (Validation == OptionValidateStrictMatches) {793          // If strict matches are requested, don't tolerate any extra defines794          // on the command line that are missing in the AST file.795          if (Diags) {796            Diags->Report(diag::err_ast_file_macro_def_undef)797                << MacroName << true << ModuleFilename;798          }799          return true;800        }801        // FIXME: Check whether this identifier was referenced anywhere in the802        // AST file. If so, we should reject the AST file. Unfortunately, this803        // information isn't in the control block. What shall we do about it?804 805        if (Existing.second) {806          SuggestedPredefines += "#undef ";807          SuggestedPredefines += MacroName.str();808          SuggestedPredefines += '\n';809        } else {810          SuggestedPredefines += "#define ";811          SuggestedPredefines += MacroName.str();812          SuggestedPredefines += ' ';813          SuggestedPredefines += Existing.first.str();814          SuggestedPredefines += '\n';815        }816        continue;817      }818 819      // If the macro was defined in one but undef'd in the other, we have a820      // conflict.821      if (Existing.second != Known->second.second) {822        if (Diags) {823          Diags->Report(diag::err_ast_file_macro_def_undef)824              << MacroName << Known->second.second << ModuleFilename;825        }826        return true;827      }828 829      // If the macro was #undef'd in both, or if the macro bodies are830      // identical, it's fine.831      if (Existing.second || Existing.first == Known->second.first) {832        ASTFileMacros.erase(Known);833        continue;834      }835 836      // The macro bodies differ; complain.837      if (Diags) {838        Diags->Report(diag::err_ast_file_macro_def_conflict)839            << MacroName << Known->second.first << Existing.first840            << ModuleFilename;841      }842      return true;843    }844 845    // Leave the <command line> file and return to <built-in>.846    SuggestedPredefines += "# 1 \"<built-in>\" 2\n";847 848    if (Validation == OptionValidateStrictMatches) {849      // If strict matches are requested, don't tolerate any extra defines in850      // the AST file that are missing on the command line.851      for (const auto &MacroName : ASTFileMacros.keys()) {852        if (Diags) {853          Diags->Report(diag::err_ast_file_macro_def_undef)854              << MacroName << false << ModuleFilename;855        }856        return true;857      }858    }859  }860 861  // Check whether we're using predefines.862  if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines &&863      Validation != OptionValidateNone) {864    if (Diags) {865      Diags->Report(diag::err_ast_file_undef)866          << ExistingPPOpts.UsePredefines << ModuleFilename;867    }868    return true;869  }870 871  // Detailed record is important since it is used for the module cache hash.872  if (LangOpts.Modules &&873      PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord &&874      Validation != OptionValidateNone) {875    if (Diags) {876      Diags->Report(diag::err_ast_file_pp_detailed_record)877          << PPOpts.DetailedRecord << ModuleFilename;878    }879    return true;880  }881 882  // Compute the #include and #include_macros lines we need.883  for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {884    StringRef File = ExistingPPOpts.Includes[I];885 886    if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&887        !ExistingPPOpts.PCHThroughHeader.empty()) {888      // In case the through header is an include, we must add all the includes889      // to the predefines so the start point can be determined.890      SuggestedPredefines += "#include \"";891      SuggestedPredefines += File;892      SuggestedPredefines += "\"\n";893      continue;894    }895 896    if (File == ExistingPPOpts.ImplicitPCHInclude)897      continue;898 899    if (llvm::is_contained(PPOpts.Includes, File))900      continue;901 902    SuggestedPredefines += "#include \"";903    SuggestedPredefines += File;904    SuggestedPredefines += "\"\n";905  }906 907  for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {908    StringRef File = ExistingPPOpts.MacroIncludes[I];909    if (llvm::is_contained(PPOpts.MacroIncludes, File))910      continue;911 912    SuggestedPredefines += "#__include_macros \"";913    SuggestedPredefines += File;914    SuggestedPredefines += "\"\n##\n";915  }916 917  return false;918}919 920bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,921                                           StringRef ModuleFilename,922                                           bool ReadMacros, bool Complain,923                                           std::string &SuggestedPredefines) {924  const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();925 926  return checkPreprocessorOptions(927      PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros,928      Complain ? &Reader.Diags : nullptr, PP.getFileManager(),929      SuggestedPredefines, PP.getLangOpts());930}931 932bool SimpleASTReaderListener::ReadPreprocessorOptions(933    const PreprocessorOptions &PPOpts, StringRef ModuleFilename,934    bool ReadMacros, bool Complain, std::string &SuggestedPredefines) {935  return checkPreprocessorOptions(PPOpts, PP.getPreprocessorOpts(),936                                  ModuleFilename, ReadMacros, nullptr,937                                  PP.getFileManager(), SuggestedPredefines,938                                  PP.getLangOpts(), OptionValidateNone);939}940 941/// Check that the specified and the existing module cache paths are equivalent.942///943/// \param Diags If non-null, produce diagnostics for any mismatches incurred.944/// \returns true when the module cache paths differ.945static bool checkModuleCachePath(llvm::vfs::FileSystem &VFS,946                                 StringRef SpecificModuleCachePath,947                                 StringRef ExistingModuleCachePath,948                                 StringRef ModuleFilename,949                                 DiagnosticsEngine *Diags,950                                 const LangOptions &LangOpts,951                                 const PreprocessorOptions &PPOpts) {952  if (!LangOpts.Modules || PPOpts.AllowPCHWithDifferentModulesCachePath ||953      SpecificModuleCachePath == ExistingModuleCachePath)954    return false;955  auto EqualOrErr =956      VFS.equivalent(SpecificModuleCachePath, ExistingModuleCachePath);957  if (EqualOrErr && *EqualOrErr)958    return false;959  if (Diags)960    Diags->Report(diag::err_ast_file_modulecache_mismatch)961        << SpecificModuleCachePath << ExistingModuleCachePath << ModuleFilename;962  return true;963}964 965bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,966                                           StringRef ModuleFilename,967                                           StringRef SpecificModuleCachePath,968                                           bool Complain) {969  return checkModuleCachePath(970      Reader.getFileManager().getVirtualFileSystem(), SpecificModuleCachePath,971      PP.getHeaderSearchInfo().getModuleCachePath(), ModuleFilename,972      Complain ? &Reader.Diags : nullptr, PP.getLangOpts(),973      PP.getPreprocessorOpts());974}975 976void PCHValidator::ReadCounter(const ModuleFile &M, uint32_t Value) {977  PP.setCounterValue(Value);978}979 980//===----------------------------------------------------------------------===//981// AST reader implementation982//===----------------------------------------------------------------------===//983 984static uint64_t readULEB(const unsigned char *&P) {985  unsigned Length = 0;986  const char *Error = nullptr;987 988  uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);989  if (Error)990    llvm::report_fatal_error(Error);991  P += Length;992  return Val;993}994 995/// Read ULEB-encoded key length and data length.996static std::pair<unsigned, unsigned>997readULEBKeyDataLength(const unsigned char *&P) {998  unsigned KeyLen = readULEB(P);999  if ((unsigned)KeyLen != KeyLen)1000    llvm::report_fatal_error("key too large");1001 1002  unsigned DataLen = readULEB(P);1003  if ((unsigned)DataLen != DataLen)1004    llvm::report_fatal_error("data too large");1005 1006  return std::make_pair(KeyLen, DataLen);1007}1008 1009void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,1010                                           bool TakeOwnership) {1011  DeserializationListener = Listener;1012  OwnsDeserializationListener = TakeOwnership;1013}1014 1015unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {1016  return serialization::ComputeHash(Sel);1017}1018 1019LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) {1020  LocalDeclID ID(Value);1021#ifndef NDEBUG1022  if (!MF.ModuleOffsetMap.empty())1023    Reader.ReadModuleOffsetMap(MF);1024 1025  unsigned ModuleFileIndex = ID.getModuleFileIndex();1026  unsigned LocalDeclID = ID.getLocalDeclIndex();1027 1028  assert(ModuleFileIndex <= MF.TransitiveImports.size());1029 1030  ModuleFile *OwningModuleFile =1031      ModuleFileIndex == 0 ? &MF : MF.TransitiveImports[ModuleFileIndex - 1];1032  assert(OwningModuleFile);1033 1034  unsigned LocalNumDecls = OwningModuleFile->LocalNumDecls;1035 1036  if (!ModuleFileIndex)1037    LocalNumDecls += NUM_PREDEF_DECL_IDS;1038 1039  assert(LocalDeclID < LocalNumDecls);1040#endif1041  (void)Reader;1042  (void)MF;1043  return ID;1044}1045 1046LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF,1047                             unsigned ModuleFileIndex, unsigned LocalDeclID) {1048  DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID;1049  return LocalDeclID::get(Reader, MF, Value);1050}1051 1052std::pair<unsigned, unsigned>1053ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {1054  return readULEBKeyDataLength(d);1055}1056 1057ASTSelectorLookupTrait::internal_key_type1058ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {1059  using namespace llvm::support;1060 1061  SelectorTable &SelTable = Reader.getContext().Selectors;1062  unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(d);1063  const IdentifierInfo *FirstII = Reader.getLocalIdentifier(1064      F, endian::readNext<IdentifierID, llvm::endianness::little>(d));1065  if (N == 0)1066    return SelTable.getNullarySelector(FirstII);1067  else if (N == 1)1068    return SelTable.getUnarySelector(FirstII);1069 1070  SmallVector<const IdentifierInfo *, 16> Args;1071  Args.push_back(FirstII);1072  for (unsigned I = 1; I != N; ++I)1073    Args.push_back(Reader.getLocalIdentifier(1074        F, endian::readNext<IdentifierID, llvm::endianness::little>(d)));1075 1076  return SelTable.getSelector(N, Args.data());1077}1078 1079ASTSelectorLookupTrait::data_type1080ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,1081                                 unsigned DataLen) {1082  using namespace llvm::support;1083 1084  data_type Result;1085 1086  Result.ID = Reader.getGlobalSelectorID(1087      F, endian::readNext<uint32_t, llvm::endianness::little>(d));1088  unsigned FullInstanceBits =1089      endian::readNext<uint16_t, llvm::endianness::little>(d);1090  unsigned FullFactoryBits =1091      endian::readNext<uint16_t, llvm::endianness::little>(d);1092  Result.InstanceBits = FullInstanceBits & 0x3;1093  Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;1094  Result.FactoryBits = FullFactoryBits & 0x3;1095  Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;1096  unsigned NumInstanceMethods = FullInstanceBits >> 3;1097  unsigned NumFactoryMethods = FullFactoryBits >> 3;1098 1099  // Load instance methods1100  for (unsigned I = 0; I != NumInstanceMethods; ++I) {1101    if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(1102            F, LocalDeclID::get(1103                   Reader, F,1104                   endian::readNext<DeclID, llvm::endianness::little>(d))))1105      Result.Instance.push_back(Method);1106  }1107 1108  // Load factory methods1109  for (unsigned I = 0; I != NumFactoryMethods; ++I) {1110    if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(1111            F, LocalDeclID::get(1112                   Reader, F,1113                   endian::readNext<DeclID, llvm::endianness::little>(d))))1114      Result.Factory.push_back(Method);1115  }1116 1117  return Result;1118}1119 1120unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {1121  return llvm::djbHash(a);1122}1123 1124std::pair<unsigned, unsigned>1125ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {1126  return readULEBKeyDataLength(d);1127}1128 1129ASTIdentifierLookupTraitBase::internal_key_type1130ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {1131  assert(n >= 2 && d[n-1] == '\0');1132  return StringRef((const char*) d, n-1);1133}1134 1135/// Whether the given identifier is "interesting".1136static bool isInterestingIdentifier(ASTReader &Reader, const IdentifierInfo &II,1137                                    bool IsModule) {1138  bool IsInteresting =1139      II.getNotableIdentifierID() != tok::NotableIdentifierKind::not_notable ||1140      II.getBuiltinID() != Builtin::ID::NotBuiltin ||1141      II.getObjCKeywordID() != tok::ObjCKeywordKind::objc_not_keyword;1142  return II.hadMacroDefinition() || II.isPoisoned() ||1143         (!IsModule && IsInteresting) || II.hasRevertedTokenIDToIdentifier() ||1144         (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&1145          II.getFETokenInfo());1146}1147 1148static bool readBit(unsigned &Bits) {1149  bool Value = Bits & 0x1;1150  Bits >>= 1;1151  return Value;1152}1153 1154IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {1155  using namespace llvm::support;1156 1157  IdentifierID RawID =1158      endian::readNext<IdentifierID, llvm::endianness::little>(d);1159  return Reader.getGlobalIdentifierID(F, RawID >> 1);1160}1161 1162static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,1163                                  bool IsModule) {1164  if (!II.isFromAST()) {1165    II.setIsFromAST();1166    if (isInterestingIdentifier(Reader, II, IsModule))1167      II.setChangedSinceDeserialization();1168  }1169}1170 1171IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,1172                                                   const unsigned char* d,1173                                                   unsigned DataLen) {1174  using namespace llvm::support;1175 1176  IdentifierID RawID =1177      endian::readNext<IdentifierID, llvm::endianness::little>(d);1178  bool IsInteresting = RawID & 0x01;1179 1180  DataLen -= sizeof(IdentifierID);1181 1182  // Wipe out the "is interesting" bit.1183  RawID = RawID >> 1;1184 1185  // Build the IdentifierInfo and link the identifier ID with it.1186  IdentifierInfo *II = KnownII;1187  if (!II) {1188    II = &Reader.getIdentifierTable().getOwn(k);1189    KnownII = II;1190  }1191  bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;1192  markIdentifierFromAST(Reader, *II, IsModule);1193  Reader.markIdentifierUpToDate(II);1194 1195  IdentifierID ID = Reader.getGlobalIdentifierID(F, RawID);1196  if (!IsInteresting) {1197    // For uninteresting identifiers, there's nothing else to do. Just notify1198    // the reader that we've finished loading this identifier.1199    Reader.SetIdentifierInfo(ID, II);1200    return II;1201  }1202 1203  unsigned ObjCOrBuiltinID =1204      endian::readNext<uint16_t, llvm::endianness::little>(d);1205  unsigned Bits = endian::readNext<uint16_t, llvm::endianness::little>(d);1206  bool CPlusPlusOperatorKeyword = readBit(Bits);1207  bool HasRevertedTokenIDToIdentifier = readBit(Bits);1208  bool Poisoned = readBit(Bits);1209  bool ExtensionToken = readBit(Bits);1210  bool HasMacroDefinition = readBit(Bits);1211 1212  assert(Bits == 0 && "Extra bits in the identifier?");1213  DataLen -= sizeof(uint16_t) * 2;1214 1215  // Set or check the various bits in the IdentifierInfo structure.1216  // Token IDs are read-only.1217  if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)1218    II->revertTokenIDToIdentifier();1219  if (!F.isModule())1220    II->setObjCOrBuiltinID(ObjCOrBuiltinID);1221  assert(II->isExtensionToken() == ExtensionToken &&1222         "Incorrect extension token flag");1223  (void)ExtensionToken;1224  if (Poisoned)1225    II->setIsPoisoned(true);1226  assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&1227         "Incorrect C++ operator keyword flag");1228  (void)CPlusPlusOperatorKeyword;1229 1230  // If this identifier has a macro definition, deserialize it or notify the1231  // visitor the actual definition is in a different module.1232  if (HasMacroDefinition) {1233    uint32_t MacroDirectivesOffset =1234        endian::readNext<uint32_t, llvm::endianness::little>(d);1235    DataLen -= 4;1236 1237    if (MacroDirectivesOffset)1238      Reader.addPendingMacro(II, &F, MacroDirectivesOffset);1239    else1240      hasMacroDefinitionInDependencies = true;1241  }1242 1243  Reader.SetIdentifierInfo(ID, II);1244 1245  // Read all of the declarations visible at global scope with this1246  // name.1247  if (DataLen > 0) {1248    SmallVector<GlobalDeclID, 4> DeclIDs;1249    for (; DataLen > 0; DataLen -= sizeof(DeclID))1250      DeclIDs.push_back(Reader.getGlobalDeclID(1251          F, LocalDeclID::get(1252                 Reader, F,1253                 endian::readNext<DeclID, llvm::endianness::little>(d))));1254    Reader.SetGloballyVisibleDecls(II, DeclIDs);1255  }1256 1257  return II;1258}1259 1260DeclarationNameKey::DeclarationNameKey(DeclarationName Name)1261    : Kind(Name.getNameKind()) {1262  switch (Kind) {1263  case DeclarationName::Identifier:1264    Data = (uint64_t)Name.getAsIdentifierInfo();1265    break;1266  case DeclarationName::ObjCZeroArgSelector:1267  case DeclarationName::ObjCOneArgSelector:1268  case DeclarationName::ObjCMultiArgSelector:1269    Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();1270    break;1271  case DeclarationName::CXXOperatorName:1272    Data = Name.getCXXOverloadedOperator();1273    break;1274  case DeclarationName::CXXLiteralOperatorName:1275    Data = (uint64_t)Name.getCXXLiteralIdentifier();1276    break;1277  case DeclarationName::CXXDeductionGuideName:1278    Data = (uint64_t)Name.getCXXDeductionGuideTemplate()1279               ->getDeclName().getAsIdentifierInfo();1280    break;1281  case DeclarationName::CXXConstructorName:1282  case DeclarationName::CXXDestructorName:1283  case DeclarationName::CXXConversionFunctionName:1284  case DeclarationName::CXXUsingDirective:1285    Data = 0;1286    break;1287  }1288}1289 1290unsigned DeclarationNameKey::getHash() const {1291  llvm::FoldingSetNodeID ID;1292  ID.AddInteger(Kind);1293 1294  switch (Kind) {1295  case DeclarationName::Identifier:1296  case DeclarationName::CXXLiteralOperatorName:1297  case DeclarationName::CXXDeductionGuideName:1298    ID.AddString(((IdentifierInfo*)Data)->getName());1299    break;1300  case DeclarationName::ObjCZeroArgSelector:1301  case DeclarationName::ObjCOneArgSelector:1302  case DeclarationName::ObjCMultiArgSelector:1303    ID.AddInteger(serialization::ComputeHash(Selector(Data)));1304    break;1305  case DeclarationName::CXXOperatorName:1306    ID.AddInteger((OverloadedOperatorKind)Data);1307    break;1308  case DeclarationName::CXXConstructorName:1309  case DeclarationName::CXXDestructorName:1310  case DeclarationName::CXXConversionFunctionName:1311  case DeclarationName::CXXUsingDirective:1312    break;1313  }1314 1315  return ID.computeStableHash();1316}1317 1318ModuleFile *1319ASTDeclContextNameLookupTraitBase::ReadFileRef(const unsigned char *&d) {1320  using namespace llvm::support;1321 1322  uint32_t ModuleFileID =1323      endian::readNext<uint32_t, llvm::endianness::little>(d);1324  return Reader.getLocalModuleFile(F, ModuleFileID);1325}1326 1327std::pair<unsigned, unsigned>1328ASTDeclContextNameLookupTraitBase::ReadKeyDataLength(const unsigned char *&d) {1329  return readULEBKeyDataLength(d);1330}1331 1332DeclarationNameKey1333ASTDeclContextNameLookupTraitBase::ReadKeyBase(const unsigned char *&d) {1334  using namespace llvm::support;1335 1336  auto Kind = (DeclarationName::NameKind)*d++;1337  uint64_t Data;1338  switch (Kind) {1339  case DeclarationName::Identifier:1340  case DeclarationName::CXXLiteralOperatorName:1341  case DeclarationName::CXXDeductionGuideName:1342    Data = (uint64_t)Reader.getLocalIdentifier(1343        F, endian::readNext<IdentifierID, llvm::endianness::little>(d));1344    break;1345  case DeclarationName::ObjCZeroArgSelector:1346  case DeclarationName::ObjCOneArgSelector:1347  case DeclarationName::ObjCMultiArgSelector:1348    Data = (uint64_t)Reader1349               .getLocalSelector(1350                   F, endian::readNext<uint32_t, llvm::endianness::little>(d))1351               .getAsOpaquePtr();1352    break;1353  case DeclarationName::CXXOperatorName:1354    Data = *d++; // OverloadedOperatorKind1355    break;1356  case DeclarationName::CXXConstructorName:1357  case DeclarationName::CXXDestructorName:1358  case DeclarationName::CXXConversionFunctionName:1359  case DeclarationName::CXXUsingDirective:1360    Data = 0;1361    break;1362  }1363 1364  return DeclarationNameKey(Kind, Data);1365}1366 1367ASTDeclContextNameLookupTrait::internal_key_type1368ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {1369  return ReadKeyBase(d);1370}1371 1372void ASTDeclContextNameLookupTraitBase::ReadDataIntoImpl(1373    const unsigned char *d, unsigned DataLen, data_type_builder &Val) {1374  using namespace llvm::support;1375 1376  for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {1377    LocalDeclID ID = LocalDeclID::get(1378        Reader, F, endian::readNext<DeclID, llvm::endianness::little>(d));1379    Val.insert(Reader.getGlobalDeclID(F, ID));1380  }1381}1382 1383void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,1384                                                 const unsigned char *d,1385                                                 unsigned DataLen,1386                                                 data_type_builder &Val) {1387  ReadDataIntoImpl(d, DataLen, Val);1388}1389 1390ModuleLocalNameLookupTrait::hash_value_type1391ModuleLocalNameLookupTrait::ComputeHash(const internal_key_type &Key) {1392  llvm::FoldingSetNodeID ID;1393  ID.AddInteger(Key.first.getHash());1394  ID.AddInteger(Key.second);1395  return ID.computeStableHash();1396}1397 1398ModuleLocalNameLookupTrait::internal_key_type1399ModuleLocalNameLookupTrait::GetInternalKey(const external_key_type &Key) {1400  DeclarationNameKey Name(Key.first);1401 1402  UnsignedOrNone ModuleHash = getPrimaryModuleHash(Key.second);1403  if (!ModuleHash)1404    return {Name, 0};1405 1406  return {Name, *ModuleHash};1407}1408 1409ModuleLocalNameLookupTrait::internal_key_type1410ModuleLocalNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {1411  DeclarationNameKey Name = ReadKeyBase(d);1412  unsigned PrimaryModuleHash =1413      llvm::support::endian::readNext<uint32_t, llvm::endianness::little>(d);1414  return {Name, PrimaryModuleHash};1415}1416 1417void ModuleLocalNameLookupTrait::ReadDataInto(internal_key_type,1418                                              const unsigned char *d,1419                                              unsigned DataLen,1420                                              data_type_builder &Val) {1421  ReadDataIntoImpl(d, DataLen, Val);1422}1423 1424ModuleFile *1425LazySpecializationInfoLookupTrait::ReadFileRef(const unsigned char *&d) {1426  using namespace llvm::support;1427 1428  uint32_t ModuleFileID =1429      endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);1430  return Reader.getLocalModuleFile(F, ModuleFileID);1431}1432 1433LazySpecializationInfoLookupTrait::internal_key_type1434LazySpecializationInfoLookupTrait::ReadKey(const unsigned char *d, unsigned) {1435  using namespace llvm::support;1436  return endian::readNext<uint32_t, llvm::endianness::little, unaligned>(d);1437}1438 1439std::pair<unsigned, unsigned>1440LazySpecializationInfoLookupTrait::ReadKeyDataLength(const unsigned char *&d) {1441  return readULEBKeyDataLength(d);1442}1443 1444void LazySpecializationInfoLookupTrait::ReadDataInto(internal_key_type,1445                                                     const unsigned char *d,1446                                                     unsigned DataLen,1447                                                     data_type_builder &Val) {1448  using namespace llvm::support;1449 1450  for (unsigned NumDecls =1451           DataLen / sizeof(serialization::reader::LazySpecializationInfo);1452       NumDecls; --NumDecls) {1453    LocalDeclID LocalID = LocalDeclID::get(1454        Reader, F,1455        endian::readNext<DeclID, llvm::endianness::little, unaligned>(d));1456    Val.insert(Reader.getGlobalDeclID(F, LocalID));1457  }1458}1459 1460bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,1461                                              BitstreamCursor &Cursor,1462                                              uint64_t Offset,1463                                              DeclContext *DC) {1464  assert(Offset != 0);1465 1466  SavedStreamPosition SavedPosition(Cursor);1467  if (llvm::Error Err = Cursor.JumpToBit(Offset)) {1468    Error(std::move(Err));1469    return true;1470  }1471 1472  RecordData Record;1473  StringRef Blob;1474  Expected<unsigned> MaybeCode = Cursor.ReadCode();1475  if (!MaybeCode) {1476    Error(MaybeCode.takeError());1477    return true;1478  }1479  unsigned Code = MaybeCode.get();1480 1481  Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);1482  if (!MaybeRecCode) {1483    Error(MaybeRecCode.takeError());1484    return true;1485  }1486  unsigned RecCode = MaybeRecCode.get();1487  if (RecCode != DECL_CONTEXT_LEXICAL) {1488    Error("Expected lexical block");1489    return true;1490  }1491 1492  assert(!isa<TranslationUnitDecl>(DC) &&1493         "expected a TU_UPDATE_LEXICAL record for TU");1494  // If we are handling a C++ class template instantiation, we can see multiple1495  // lexical updates for the same record. It's important that we select only one1496  // of them, so that field numbering works properly. Just pick the first one we1497  // see.1498  auto &Lex = LexicalDecls[DC];1499  if (!Lex.first) {1500    Lex = std::make_pair(1501        &M, llvm::ArrayRef(1502                reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),1503                Blob.size() / sizeof(DeclID)));1504  }1505  DC->setHasExternalLexicalStorage(true);1506  return false;1507}1508 1509bool ASTReader::ReadVisibleDeclContextStorage(1510    ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, GlobalDeclID ID,1511    ASTReader::VisibleDeclContextStorageKind VisibleKind) {1512  assert(Offset != 0);1513 1514  SavedStreamPosition SavedPosition(Cursor);1515  if (llvm::Error Err = Cursor.JumpToBit(Offset)) {1516    Error(std::move(Err));1517    return true;1518  }1519 1520  RecordData Record;1521  StringRef Blob;1522  Expected<unsigned> MaybeCode = Cursor.ReadCode();1523  if (!MaybeCode) {1524    Error(MaybeCode.takeError());1525    return true;1526  }1527  unsigned Code = MaybeCode.get();1528 1529  Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);1530  if (!MaybeRecCode) {1531    Error(MaybeRecCode.takeError());1532    return true;1533  }1534  unsigned RecCode = MaybeRecCode.get();1535  switch (VisibleKind) {1536  case VisibleDeclContextStorageKind::GenerallyVisible:1537    if (RecCode != DECL_CONTEXT_VISIBLE) {1538      Error("Expected visible lookup table block");1539      return true;1540    }1541    break;1542  case VisibleDeclContextStorageKind::ModuleLocalVisible:1543    if (RecCode != DECL_CONTEXT_MODULE_LOCAL_VISIBLE) {1544      Error("Expected module local visible lookup table block");1545      return true;1546    }1547    break;1548  case VisibleDeclContextStorageKind::TULocalVisible:1549    if (RecCode != DECL_CONTEXT_TU_LOCAL_VISIBLE) {1550      Error("Expected TU local lookup table block");1551      return true;1552    }1553    break;1554  }1555 1556  // We can't safely determine the primary context yet, so delay attaching the1557  // lookup table until we're done with recursive deserialization.1558  auto *Data = (const unsigned char*)Blob.data();1559  switch (VisibleKind) {1560  case VisibleDeclContextStorageKind::GenerallyVisible:1561    PendingVisibleUpdates[ID].push_back(UpdateData{&M, Data});1562    break;1563  case VisibleDeclContextStorageKind::ModuleLocalVisible:1564    PendingModuleLocalVisibleUpdates[ID].push_back(UpdateData{&M, Data});1565    break;1566  case VisibleDeclContextStorageKind::TULocalVisible:1567    if (M.Kind == MK_MainFile)1568      TULocalUpdates[ID].push_back(UpdateData{&M, Data});1569    break;1570  }1571  return false;1572}1573 1574void ASTReader::AddSpecializations(const Decl *D, const unsigned char *Data,1575                                   ModuleFile &M, bool IsPartial) {1576  D = D->getCanonicalDecl();1577  auto &SpecLookups =1578      IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;1579  SpecLookups[D].Table.add(&M, Data,1580                           reader::LazySpecializationInfoLookupTrait(*this, M));1581}1582 1583bool ASTReader::ReadSpecializations(ModuleFile &M, BitstreamCursor &Cursor,1584                                    uint64_t Offset, Decl *D, bool IsPartial) {1585  assert(Offset != 0);1586 1587  SavedStreamPosition SavedPosition(Cursor);1588  if (llvm::Error Err = Cursor.JumpToBit(Offset)) {1589    Error(std::move(Err));1590    return true;1591  }1592 1593  RecordData Record;1594  StringRef Blob;1595  Expected<unsigned> MaybeCode = Cursor.ReadCode();1596  if (!MaybeCode) {1597    Error(MaybeCode.takeError());1598    return true;1599  }1600  unsigned Code = MaybeCode.get();1601 1602  Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);1603  if (!MaybeRecCode) {1604    Error(MaybeRecCode.takeError());1605    return true;1606  }1607  unsigned RecCode = MaybeRecCode.get();1608  if (RecCode != DECL_SPECIALIZATIONS &&1609      RecCode != DECL_PARTIAL_SPECIALIZATIONS) {1610    Error("Expected decl specs block");1611    return true;1612  }1613 1614  auto *Data = (const unsigned char *)Blob.data();1615  AddSpecializations(D, Data, M, IsPartial);1616  return false;1617}1618 1619void ASTReader::Error(StringRef Msg) const {1620  Error(diag::err_fe_ast_file_malformed, Msg);1621  if (PP.getLangOpts().Modules &&1622      !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {1623    Diag(diag::note_module_cache_path)1624      << PP.getHeaderSearchInfo().getModuleCachePath();1625  }1626}1627 1628void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,1629                      StringRef Arg3) const {1630  Diag(DiagID) << Arg1 << Arg2 << Arg3;1631}1632 1633namespace {1634struct AlreadyReportedDiagnosticError1635    : llvm::ErrorInfo<AlreadyReportedDiagnosticError> {1636  static char ID;1637 1638  void log(raw_ostream &OS) const override {1639    llvm_unreachable("reporting an already-reported diagnostic error");1640  }1641 1642  std::error_code convertToErrorCode() const override {1643    return llvm::inconvertibleErrorCode();1644  }1645};1646 1647char AlreadyReportedDiagnosticError::ID = 0;1648} // namespace1649 1650void ASTReader::Error(llvm::Error &&Err) const {1651  handleAllErrors(1652      std::move(Err), [](AlreadyReportedDiagnosticError &) {},1653      [&](llvm::ErrorInfoBase &E) { return Error(E.message()); });1654}1655 1656//===----------------------------------------------------------------------===//1657// Source Manager Deserialization1658//===----------------------------------------------------------------------===//1659 1660/// Read the line table in the source manager block.1661void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {1662  unsigned Idx = 0;1663  LineTableInfo &LineTable = SourceMgr.getLineTable();1664 1665  // Parse the file names1666  std::map<int, int> FileIDs;1667  FileIDs[-1] = -1; // For unspecified filenames.1668  for (unsigned I = 0; Record[Idx]; ++I) {1669    // Extract the file name1670    auto Filename = ReadPath(F, Record, Idx);1671    FileIDs[I] = LineTable.getLineTableFilenameID(Filename);1672  }1673  ++Idx;1674 1675  // Parse the line entries1676  std::vector<LineEntry> Entries;1677  while (Idx < Record.size()) {1678    FileID FID = ReadFileID(F, Record, Idx);1679 1680    // Extract the line entries1681    unsigned NumEntries = Record[Idx++];1682    assert(NumEntries && "no line entries for file ID");1683    Entries.clear();1684    Entries.reserve(NumEntries);1685    for (unsigned I = 0; I != NumEntries; ++I) {1686      unsigned FileOffset = Record[Idx++];1687      unsigned LineNo = Record[Idx++];1688      int FilenameID = FileIDs[Record[Idx++]];1689      SrcMgr::CharacteristicKind FileKind1690        = (SrcMgr::CharacteristicKind)Record[Idx++];1691      unsigned IncludeOffset = Record[Idx++];1692      Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,1693                                       FileKind, IncludeOffset));1694    }1695    LineTable.AddEntry(FID, Entries);1696  }1697}1698 1699/// Read a source manager block1700llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {1701  using namespace SrcMgr;1702 1703  BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;1704 1705  // Set the source-location entry cursor to the current position in1706  // the stream. This cursor will be used to read the contents of the1707  // source manager block initially, and then lazily read1708  // source-location entries as needed.1709  SLocEntryCursor = F.Stream;1710 1711  // The stream itself is going to skip over the source manager block.1712  if (llvm::Error Err = F.Stream.SkipBlock())1713    return Err;1714 1715  // Enter the source manager block.1716  if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))1717    return Err;1718  F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();1719 1720  RecordData Record;1721  while (true) {1722    Expected<llvm::BitstreamEntry> MaybeE =1723        SLocEntryCursor.advanceSkippingSubblocks();1724    if (!MaybeE)1725      return MaybeE.takeError();1726    llvm::BitstreamEntry E = MaybeE.get();1727 1728    switch (E.Kind) {1729    case llvm::BitstreamEntry::SubBlock: // Handled for us already.1730    case llvm::BitstreamEntry::Error:1731      return llvm::createStringError(std::errc::illegal_byte_sequence,1732                                     "malformed block record in AST file");1733    case llvm::BitstreamEntry::EndBlock:1734      return llvm::Error::success();1735    case llvm::BitstreamEntry::Record:1736      // The interesting case.1737      break;1738    }1739 1740    // Read a record.1741    Record.clear();1742    StringRef Blob;1743    Expected<unsigned> MaybeRecord =1744        SLocEntryCursor.readRecord(E.ID, Record, &Blob);1745    if (!MaybeRecord)1746      return MaybeRecord.takeError();1747    switch (MaybeRecord.get()) {1748    default:  // Default behavior: ignore.1749      break;1750 1751    case SM_SLOC_FILE_ENTRY:1752    case SM_SLOC_BUFFER_ENTRY:1753    case SM_SLOC_EXPANSION_ENTRY:1754      // Once we hit one of the source location entries, we're done.1755      return llvm::Error::success();1756    }1757  }1758}1759 1760llvm::Expected<SourceLocation::UIntTy>1761ASTReader::readSLocOffset(ModuleFile *F, unsigned Index) {1762  BitstreamCursor &Cursor = F->SLocEntryCursor;1763  SavedStreamPosition SavedPosition(Cursor);1764  if (llvm::Error Err = Cursor.JumpToBit(F->SLocEntryOffsetsBase +1765                                         F->SLocEntryOffsets[Index]))1766    return std::move(Err);1767 1768  Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();1769  if (!MaybeEntry)1770    return MaybeEntry.takeError();1771 1772  llvm::BitstreamEntry Entry = MaybeEntry.get();1773  if (Entry.Kind != llvm::BitstreamEntry::Record)1774    return llvm::createStringError(1775        std::errc::illegal_byte_sequence,1776        "incorrectly-formatted source location entry in AST file");1777 1778  RecordData Record;1779  StringRef Blob;1780  Expected<unsigned> MaybeSLOC = Cursor.readRecord(Entry.ID, Record, &Blob);1781  if (!MaybeSLOC)1782    return MaybeSLOC.takeError();1783 1784  switch (MaybeSLOC.get()) {1785  default:1786    return llvm::createStringError(1787        std::errc::illegal_byte_sequence,1788        "incorrectly-formatted source location entry in AST file");1789  case SM_SLOC_FILE_ENTRY:1790  case SM_SLOC_BUFFER_ENTRY:1791  case SM_SLOC_EXPANSION_ENTRY:1792    return F->SLocEntryBaseOffset + Record[0];1793  }1794}1795 1796int ASTReader::getSLocEntryID(SourceLocation::UIntTy SLocOffset) {1797  auto SLocMapI =1798      GlobalSLocOffsetMap.find(SourceManager::MaxLoadedOffset - SLocOffset - 1);1799  assert(SLocMapI != GlobalSLocOffsetMap.end() &&1800         "Corrupted global sloc offset map");1801  ModuleFile *F = SLocMapI->second;1802 1803  bool Invalid = false;1804 1805  auto It = llvm::upper_bound(1806      llvm::index_range(0, F->LocalNumSLocEntries), SLocOffset,1807      [&](SourceLocation::UIntTy Offset, std::size_t LocalIndex) {1808        int ID = F->SLocEntryBaseID + LocalIndex;1809        std::size_t Index = -ID - 2;1810        if (!SourceMgr.SLocEntryOffsetLoaded[Index]) {1811          assert(!SourceMgr.SLocEntryLoaded[Index]);1812          auto MaybeEntryOffset = readSLocOffset(F, LocalIndex);1813          if (!MaybeEntryOffset) {1814            Error(MaybeEntryOffset.takeError());1815            Invalid = true;1816            return true;1817          }1818          SourceMgr.LoadedSLocEntryTable[Index] =1819              SrcMgr::SLocEntry::getOffsetOnly(*MaybeEntryOffset);1820          SourceMgr.SLocEntryOffsetLoaded[Index] = true;1821        }1822        return Offset < SourceMgr.LoadedSLocEntryTable[Index].getOffset();1823      });1824 1825  if (Invalid)1826    return 0;1827 1828  // The iterator points to the first entry with start offset greater than the1829  // offset of interest. The previous entry must contain the offset of interest.1830  return F->SLocEntryBaseID + *std::prev(It);1831}1832 1833bool ASTReader::ReadSLocEntry(int ID) {1834  if (ID == 0)1835    return false;1836 1837  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {1838    Error("source location entry ID out-of-range for AST file");1839    return true;1840  }1841 1842  // Local helper to read the (possibly-compressed) buffer data following the1843  // entry record.1844  auto ReadBuffer = [this](1845      BitstreamCursor &SLocEntryCursor,1846      StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {1847    RecordData Record;1848    StringRef Blob;1849    Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();1850    if (!MaybeCode) {1851      Error(MaybeCode.takeError());1852      return nullptr;1853    }1854    unsigned Code = MaybeCode.get();1855 1856    Expected<unsigned> MaybeRecCode =1857        SLocEntryCursor.readRecord(Code, Record, &Blob);1858    if (!MaybeRecCode) {1859      Error(MaybeRecCode.takeError());1860      return nullptr;1861    }1862    unsigned RecCode = MaybeRecCode.get();1863 1864    if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {1865      // Inspect the first byte to differentiate zlib (\x78) and zstd1866      // (little-endian 0xFD2FB528).1867      const llvm::compression::Format F =1868          Blob.size() > 0 && Blob.data()[0] == 0x781869              ? llvm::compression::Format::Zlib1870              : llvm::compression::Format::Zstd;1871      if (const char *Reason = llvm::compression::getReasonIfUnsupported(F)) {1872        Error(Reason);1873        return nullptr;1874      }1875      SmallVector<uint8_t, 0> Decompressed;1876      if (llvm::Error E = llvm::compression::decompress(1877              F, llvm::arrayRefFromStringRef(Blob), Decompressed, Record[0])) {1878        Error("could not decompress embedded file contents: " +1879              llvm::toString(std::move(E)));1880        return nullptr;1881      }1882      return llvm::MemoryBuffer::getMemBufferCopy(1883          llvm::toStringRef(Decompressed), Name);1884    } else if (RecCode == SM_SLOC_BUFFER_BLOB) {1885      return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);1886    } else {1887      Error("AST record has invalid code");1888      return nullptr;1889    }1890  };1891 1892  ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;1893  if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(1894          F->SLocEntryOffsetsBase +1895          F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {1896    Error(std::move(Err));1897    return true;1898  }1899 1900  BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;1901  SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;1902 1903  ++NumSLocEntriesRead;1904  Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();1905  if (!MaybeEntry) {1906    Error(MaybeEntry.takeError());1907    return true;1908  }1909  llvm::BitstreamEntry Entry = MaybeEntry.get();1910 1911  if (Entry.Kind != llvm::BitstreamEntry::Record) {1912    Error("incorrectly-formatted source location entry in AST file");1913    return true;1914  }1915 1916  RecordData Record;1917  StringRef Blob;1918  Expected<unsigned> MaybeSLOC =1919      SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);1920  if (!MaybeSLOC) {1921    Error(MaybeSLOC.takeError());1922    return true;1923  }1924  switch (MaybeSLOC.get()) {1925  default:1926    Error("incorrectly-formatted source location entry in AST file");1927    return true;1928 1929  case SM_SLOC_FILE_ENTRY: {1930    // We will detect whether a file changed and return 'Failure' for it, but1931    // we will also try to fail gracefully by setting up the SLocEntry.1932    unsigned InputID = Record[4];1933    InputFile IF = getInputFile(*F, InputID);1934    OptionalFileEntryRef File = IF.getFile();1935    bool OverriddenBuffer = IF.isOverridden();1936 1937    // Note that we only check if a File was returned. If it was out-of-date1938    // we have complained but we will continue creating a FileID to recover1939    // gracefully.1940    if (!File)1941      return true;1942 1943    SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);1944    if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {1945      // This is the module's main file.1946      IncludeLoc = getImportLocation(F);1947    }1948    SrcMgr::CharacteristicKind1949      FileCharacter = (SrcMgr::CharacteristicKind)Record[2];1950    FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,1951                                        BaseOffset + Record[0]);1952    SrcMgr::FileInfo &FileInfo = SourceMgr.getSLocEntry(FID).getFile();1953    FileInfo.NumCreatedFIDs = Record[5];1954    if (Record[3])1955      FileInfo.setHasLineDirectives();1956 1957    unsigned NumFileDecls = Record[7];1958    if (NumFileDecls && ContextObj) {1959      const unaligned_decl_id_t *FirstDecl = F->FileSortedDecls + Record[6];1960      assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");1961      FileDeclIDs[FID] =1962          FileDeclsInfo(F, llvm::ArrayRef(FirstDecl, NumFileDecls));1963    }1964 1965    const SrcMgr::ContentCache &ContentCache =1966        SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));1967    if (OverriddenBuffer && !ContentCache.BufferOverridden &&1968        ContentCache.ContentsEntry == ContentCache.OrigEntry &&1969        !ContentCache.getBufferIfLoaded()) {1970      auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());1971      if (!Buffer)1972        return true;1973      SourceMgr.overrideFileContents(*File, std::move(Buffer));1974    }1975 1976    break;1977  }1978 1979  case SM_SLOC_BUFFER_ENTRY: {1980    const char *Name = Blob.data();1981    unsigned Offset = Record[0];1982    SrcMgr::CharacteristicKind1983      FileCharacter = (SrcMgr::CharacteristicKind)Record[2];1984    SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);1985    if (IncludeLoc.isInvalid() && F->isModule()) {1986      IncludeLoc = getImportLocation(F);1987    }1988 1989    auto Buffer = ReadBuffer(SLocEntryCursor, Name);1990    if (!Buffer)1991      return true;1992    FileID FID = SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,1993                                        BaseOffset + Offset, IncludeLoc);1994    if (Record[3]) {1995      auto &FileInfo = SourceMgr.getSLocEntry(FID).getFile();1996      FileInfo.setHasLineDirectives();1997    }1998    break;1999  }2000 2001  case SM_SLOC_EXPANSION_ENTRY: {2002    SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);2003    SourceLocation ExpansionBegin = ReadSourceLocation(*F, Record[2]);2004    SourceLocation ExpansionEnd = ReadSourceLocation(*F, Record[3]);2005    SourceMgr.createExpansionLoc(SpellingLoc, ExpansionBegin, ExpansionEnd,2006                                 Record[5], Record[4], ID,2007                                 BaseOffset + Record[0]);2008    break;2009  }2010  }2011 2012  return false;2013}2014 2015std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {2016  if (ID == 0)2017    return std::make_pair(SourceLocation(), "");2018 2019  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {2020    Error("source location entry ID out-of-range for AST file");2021    return std::make_pair(SourceLocation(), "");2022  }2023 2024  // Find which module file this entry lands in.2025  ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;2026  if (!M->isModule())2027    return std::make_pair(SourceLocation(), "");2028 2029  // FIXME: Can we map this down to a particular submodule? That would be2030  // ideal.2031  return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));2032}2033 2034/// Find the location where the module F is imported.2035SourceLocation ASTReader::getImportLocation(ModuleFile *F) {2036  if (F->ImportLoc.isValid())2037    return F->ImportLoc;2038 2039  // Otherwise we have a PCH. It's considered to be "imported" at the first2040  // location of its includer.2041  if (F->ImportedBy.empty() || !F->ImportedBy[0]) {2042    // Main file is the importer.2043    assert(SourceMgr.getMainFileID().isValid() && "missing main file");2044    return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());2045  }2046  return F->ImportedBy[0]->FirstLoc;2047}2048 2049/// Enter a subblock of the specified BlockID with the specified cursor. Read2050/// the abbreviations that are at the top of the block and then leave the cursor2051/// pointing into the block.2052llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,2053                                        unsigned BlockID,2054                                        uint64_t *StartOfBlockOffset) {2055  if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))2056    return Err;2057 2058  if (StartOfBlockOffset)2059    *StartOfBlockOffset = Cursor.GetCurrentBitNo();2060 2061  while (true) {2062    uint64_t Offset = Cursor.GetCurrentBitNo();2063    Expected<unsigned> MaybeCode = Cursor.ReadCode();2064    if (!MaybeCode)2065      return MaybeCode.takeError();2066    unsigned Code = MaybeCode.get();2067 2068    // We expect all abbrevs to be at the start of the block.2069    if (Code != llvm::bitc::DEFINE_ABBREV) {2070      if (llvm::Error Err = Cursor.JumpToBit(Offset))2071        return Err;2072      return llvm::Error::success();2073    }2074    if (llvm::Error Err = Cursor.ReadAbbrevRecord())2075      return Err;2076  }2077}2078 2079Token ASTReader::ReadToken(ModuleFile &M, const RecordDataImpl &Record,2080                           unsigned &Idx) {2081  Token Tok;2082  Tok.startToken();2083  Tok.setLocation(ReadSourceLocation(M, Record, Idx));2084  Tok.setKind((tok::TokenKind)Record[Idx++]);2085  Tok.setFlag((Token::TokenFlags)Record[Idx++]);2086 2087  if (Tok.isAnnotation()) {2088    Tok.setAnnotationEndLoc(ReadSourceLocation(M, Record, Idx));2089    switch (Tok.getKind()) {2090    case tok::annot_pragma_loop_hint: {2091      auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;2092      Info->PragmaName = ReadToken(M, Record, Idx);2093      Info->Option = ReadToken(M, Record, Idx);2094      unsigned NumTokens = Record[Idx++];2095      SmallVector<Token, 4> Toks;2096      Toks.reserve(NumTokens);2097      for (unsigned I = 0; I < NumTokens; ++I)2098        Toks.push_back(ReadToken(M, Record, Idx));2099      Info->Toks = llvm::ArrayRef(Toks).copy(PP.getPreprocessorAllocator());2100      Tok.setAnnotationValue(static_cast<void *>(Info));2101      break;2102    }2103    case tok::annot_pragma_pack: {2104      auto *Info = new (PP.getPreprocessorAllocator()) Sema::PragmaPackInfo;2105      Info->Action = static_cast<Sema::PragmaMsStackAction>(Record[Idx++]);2106      auto SlotLabel = ReadString(Record, Idx);2107      Info->SlotLabel =2108          llvm::StringRef(SlotLabel).copy(PP.getPreprocessorAllocator());2109      Info->Alignment = ReadToken(M, Record, Idx);2110      Tok.setAnnotationValue(static_cast<void *>(Info));2111      break;2112    }2113    // Some annotation tokens do not use the PtrData field.2114    case tok::annot_pragma_openmp:2115    case tok::annot_pragma_openmp_end:2116    case tok::annot_pragma_unused:2117    case tok::annot_pragma_openacc:2118    case tok::annot_pragma_openacc_end:2119    case tok::annot_repl_input_end:2120      break;2121    default:2122      llvm_unreachable("missing deserialization code for annotation token");2123    }2124  } else {2125    Tok.setLength(Record[Idx++]);2126    if (IdentifierInfo *II = getLocalIdentifier(M, Record[Idx++]))2127      Tok.setIdentifierInfo(II);2128  }2129  return Tok;2130}2131 2132MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {2133  BitstreamCursor &Stream = F.MacroCursor;2134 2135  // Keep track of where we are in the stream, then jump back there2136  // after reading this macro.2137  SavedStreamPosition SavedPosition(Stream);2138 2139  if (llvm::Error Err = Stream.JumpToBit(Offset)) {2140    // FIXME this drops errors on the floor.2141    consumeError(std::move(Err));2142    return nullptr;2143  }2144  RecordData Record;2145  SmallVector<IdentifierInfo*, 16> MacroParams;2146  MacroInfo *Macro = nullptr;2147  llvm::MutableArrayRef<Token> MacroTokens;2148 2149  while (true) {2150    // Advance to the next record, but if we get to the end of the block, don't2151    // pop it (removing all the abbreviations from the cursor) since we want to2152    // be able to reseek within the block and read entries.2153    unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;2154    Expected<llvm::BitstreamEntry> MaybeEntry =2155        Stream.advanceSkippingSubblocks(Flags);2156    if (!MaybeEntry) {2157      Error(MaybeEntry.takeError());2158      return Macro;2159    }2160    llvm::BitstreamEntry Entry = MaybeEntry.get();2161 2162    switch (Entry.Kind) {2163    case llvm::BitstreamEntry::SubBlock: // Handled for us already.2164    case llvm::BitstreamEntry::Error:2165      Error("malformed block record in AST file");2166      return Macro;2167    case llvm::BitstreamEntry::EndBlock:2168      return Macro;2169    case llvm::BitstreamEntry::Record:2170      // The interesting case.2171      break;2172    }2173 2174    // Read a record.2175    Record.clear();2176    PreprocessorRecordTypes RecType;2177    if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))2178      RecType = (PreprocessorRecordTypes)MaybeRecType.get();2179    else {2180      Error(MaybeRecType.takeError());2181      return Macro;2182    }2183    switch (RecType) {2184    case PP_MODULE_MACRO:2185    case PP_MACRO_DIRECTIVE_HISTORY:2186      return Macro;2187 2188    case PP_MACRO_OBJECT_LIKE:2189    case PP_MACRO_FUNCTION_LIKE: {2190      // If we already have a macro, that means that we've hit the end2191      // of the definition of the macro we were looking for. We're2192      // done.2193      if (Macro)2194        return Macro;2195 2196      unsigned NextIndex = 1; // Skip identifier ID.2197      SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);2198      MacroInfo *MI = PP.AllocateMacroInfo(Loc);2199      MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));2200      MI->setIsUsed(Record[NextIndex++]);2201      MI->setUsedForHeaderGuard(Record[NextIndex++]);2202      MacroTokens = MI->allocateTokens(Record[NextIndex++],2203                                       PP.getPreprocessorAllocator());2204      if (RecType == PP_MACRO_FUNCTION_LIKE) {2205        // Decode function-like macro info.2206        bool isC99VarArgs = Record[NextIndex++];2207        bool isGNUVarArgs = Record[NextIndex++];2208        bool hasCommaPasting = Record[NextIndex++];2209        MacroParams.clear();2210        unsigned NumArgs = Record[NextIndex++];2211        for (unsigned i = 0; i != NumArgs; ++i)2212          MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));2213 2214        // Install function-like macro info.2215        MI->setIsFunctionLike();2216        if (isC99VarArgs) MI->setIsC99Varargs();2217        if (isGNUVarArgs) MI->setIsGNUVarargs();2218        if (hasCommaPasting) MI->setHasCommaPasting();2219        MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());2220      }2221 2222      // Remember that we saw this macro last so that we add the tokens that2223      // form its body to it.2224      Macro = MI;2225 2226      if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&2227          Record[NextIndex]) {2228        // We have a macro definition. Register the association2229        PreprocessedEntityID2230            GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);2231        unsigned Index = translatePreprocessedEntityIDToIndex(GlobalID);2232        PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();2233        PreprocessingRecord::PPEntityID PPID =2234            PPRec.getPPEntityID(Index, /*isLoaded=*/true);2235        MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(2236            PPRec.getPreprocessedEntity(PPID));2237        if (PPDef)2238          PPRec.RegisterMacroDefinition(Macro, PPDef);2239      }2240 2241      ++NumMacrosRead;2242      break;2243    }2244 2245    case PP_TOKEN: {2246      // If we see a TOKEN before a PP_MACRO_*, then the file is2247      // erroneous, just pretend we didn't see this.2248      if (!Macro) break;2249      if (MacroTokens.empty()) {2250        Error("unexpected number of macro tokens for a macro in AST file");2251        return Macro;2252      }2253 2254      unsigned Idx = 0;2255      MacroTokens[0] = ReadToken(F, Record, Idx);2256      MacroTokens = MacroTokens.drop_front();2257      break;2258    }2259    }2260  }2261}2262 2263PreprocessedEntityID2264ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,2265                                         PreprocessedEntityID LocalID) const {2266  if (!M.ModuleOffsetMap.empty())2267    ReadModuleOffsetMap(M);2268 2269  unsigned ModuleFileIndex = LocalID >> 32;2270  LocalID &= llvm::maskTrailingOnes<PreprocessedEntityID>(32);2271  ModuleFile *MF =2272      ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;2273  assert(MF && "malformed identifier ID encoding?");2274 2275  if (!ModuleFileIndex) {2276    assert(LocalID >= NUM_PREDEF_PP_ENTITY_IDS);2277    LocalID -= NUM_PREDEF_PP_ENTITY_IDS;2278  }2279 2280  return (static_cast<PreprocessedEntityID>(MF->Index + 1) << 32) | LocalID;2281}2282 2283OptionalFileEntryRef2284HeaderFileInfoTrait::getFile(const internal_key_type &Key) {2285  FileManager &FileMgr = Reader.getFileManager();2286  if (!Key.Imported)2287    return FileMgr.getOptionalFileRef(Key.Filename);2288 2289  auto Resolved =2290      ASTReader::ResolveImportedPath(Reader.getPathBuf(), Key.Filename, M);2291  return FileMgr.getOptionalFileRef(*Resolved);2292}2293 2294unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {2295  uint8_t buf[sizeof(ikey.Size) + sizeof(ikey.ModTime)];2296  memcpy(buf, &ikey.Size, sizeof(ikey.Size));2297  memcpy(buf + sizeof(ikey.Size), &ikey.ModTime, sizeof(ikey.ModTime));2298  return llvm::xxh3_64bits(buf);2299}2300 2301HeaderFileInfoTrait::internal_key_type2302HeaderFileInfoTrait::GetInternalKey(external_key_type ekey) {2303  internal_key_type ikey = {ekey.getSize(),2304                            M.HasTimestamps ? ekey.getModificationTime() : 0,2305                            ekey.getName(), /*Imported*/ false};2306  return ikey;2307}2308 2309bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {2310  if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))2311    return false;2312 2313  if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)2314    return true;2315 2316  // Determine whether the actual files are equivalent.2317  OptionalFileEntryRef FEA = getFile(a);2318  OptionalFileEntryRef FEB = getFile(b);2319  return FEA && FEA == FEB;2320}2321 2322std::pair<unsigned, unsigned>2323HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {2324  return readULEBKeyDataLength(d);2325}2326 2327HeaderFileInfoTrait::internal_key_type2328HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {2329  using namespace llvm::support;2330 2331  internal_key_type ikey;2332  ikey.Size = off_t(endian::readNext<uint64_t, llvm::endianness::little>(d));2333  ikey.ModTime =2334      time_t(endian::readNext<uint64_t, llvm::endianness::little>(d));2335  ikey.Filename = (const char *)d;2336  ikey.Imported = true;2337  return ikey;2338}2339 2340HeaderFileInfoTrait::data_type2341HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,2342                              unsigned DataLen) {2343  using namespace llvm::support;2344 2345  const unsigned char *End = d + DataLen;2346  HeaderFileInfo HFI;2347  unsigned Flags = *d++;2348 2349  OptionalFileEntryRef FE;2350  bool Included = (Flags >> 6) & 0x01;2351  if (Included)2352    if ((FE = getFile(key)))2353      // Not using \c Preprocessor::markIncluded(), since that would attempt to2354      // deserialize this header file info again.2355      Reader.getPreprocessor().getIncludedFiles().insert(*FE);2356 2357  // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.2358  HFI.isImport |= (Flags >> 5) & 0x01;2359  HFI.isPragmaOnce |= (Flags >> 4) & 0x01;2360  HFI.DirInfo = (Flags >> 1) & 0x07;2361  HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(2362      M, endian::readNext<IdentifierID, llvm::endianness::little>(d));2363 2364  assert((End - d) % 4 == 0 &&2365         "Wrong data length in HeaderFileInfo deserialization");2366  while (d != End) {2367    uint32_t LocalSMID =2368        endian::readNext<uint32_t, llvm::endianness::little>(d);2369    auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 7);2370    LocalSMID >>= 3;2371 2372    // This header is part of a module. Associate it with the module to enable2373    // implicit module import.2374    SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);2375    Module *Mod = Reader.getSubmodule(GlobalSMID);2376    ModuleMap &ModMap =2377        Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();2378 2379    if (FE || (FE = getFile(key))) {2380      // FIXME: NameAsWritten2381      Module::Header H = {std::string(key.Filename), "", *FE};2382      ModMap.addHeader(Mod, H, HeaderRole, /*Imported=*/true);2383    }2384    HFI.mergeModuleMembership(HeaderRole);2385  }2386 2387  // This HeaderFileInfo was externally loaded.2388  HFI.External = true;2389  HFI.IsValid = true;2390  return HFI;2391}2392 2393void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,2394                                uint32_t MacroDirectivesOffset) {2395  assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");2396  PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));2397}2398 2399void ASTReader::ReadDefinedMacros() {2400  // Note that we are loading defined macros.2401  Deserializing Macros(this);2402 2403  for (ModuleFile &I : llvm::reverse(ModuleMgr)) {2404    BitstreamCursor &MacroCursor = I.MacroCursor;2405 2406    // If there was no preprocessor block, skip this file.2407    if (MacroCursor.getBitcodeBytes().empty())2408      continue;2409 2410    BitstreamCursor Cursor = MacroCursor;2411    if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {2412      Error(std::move(Err));2413      return;2414    }2415 2416    RecordData Record;2417    while (true) {2418      Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();2419      if (!MaybeE) {2420        Error(MaybeE.takeError());2421        return;2422      }2423      llvm::BitstreamEntry E = MaybeE.get();2424 2425      switch (E.Kind) {2426      case llvm::BitstreamEntry::SubBlock: // Handled for us already.2427      case llvm::BitstreamEntry::Error:2428        Error("malformed block record in AST file");2429        return;2430      case llvm::BitstreamEntry::EndBlock:2431        goto NextCursor;2432 2433      case llvm::BitstreamEntry::Record: {2434        Record.clear();2435        Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);2436        if (!MaybeRecord) {2437          Error(MaybeRecord.takeError());2438          return;2439        }2440        switch (MaybeRecord.get()) {2441        default:  // Default behavior: ignore.2442          break;2443 2444        case PP_MACRO_OBJECT_LIKE:2445        case PP_MACRO_FUNCTION_LIKE: {2446          IdentifierInfo *II = getLocalIdentifier(I, Record[0]);2447          if (II->isOutOfDate())2448            updateOutOfDateIdentifier(*II);2449          break;2450        }2451 2452        case PP_TOKEN:2453          // Ignore tokens.2454          break;2455        }2456        break;2457      }2458      }2459    }2460    NextCursor:  ;2461  }2462}2463 2464namespace {2465 2466  /// Visitor class used to look up identifirs in an AST file.2467  class IdentifierLookupVisitor {2468    StringRef Name;2469    unsigned NameHash;2470    unsigned PriorGeneration;2471    unsigned &NumIdentifierLookups;2472    unsigned &NumIdentifierLookupHits;2473    IdentifierInfo *Found = nullptr;2474 2475  public:2476    IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,2477                            unsigned &NumIdentifierLookups,2478                            unsigned &NumIdentifierLookupHits)2479      : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),2480        PriorGeneration(PriorGeneration),2481        NumIdentifierLookups(NumIdentifierLookups),2482        NumIdentifierLookupHits(NumIdentifierLookupHits) {}2483 2484    bool operator()(ModuleFile &M) {2485      // If we've already searched this module file, skip it now.2486      if (M.Generation <= PriorGeneration)2487        return true;2488 2489      ASTIdentifierLookupTable *IdTable2490        = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;2491      if (!IdTable)2492        return false;2493 2494      ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,2495                                     Found);2496      ++NumIdentifierLookups;2497      ASTIdentifierLookupTable::iterator Pos =2498          IdTable->find_hashed(Name, NameHash, &Trait);2499      if (Pos == IdTable->end())2500        return false;2501 2502      // Dereferencing the iterator has the effect of building the2503      // IdentifierInfo node and populating it with the various2504      // declarations it needs.2505      ++NumIdentifierLookupHits;2506      Found = *Pos;2507      if (Trait.hasMoreInformationInDependencies()) {2508        // Look for the identifier in extra modules as they contain more info.2509        return false;2510      }2511      return true;2512    }2513 2514    // Retrieve the identifier info found within the module2515    // files.2516    IdentifierInfo *getIdentifierInfo() const { return Found; }2517  };2518 2519} // namespace2520 2521void ASTReader::updateOutOfDateIdentifier(const IdentifierInfo &II) {2522  // Note that we are loading an identifier.2523  Deserializing AnIdentifier(this);2524 2525  unsigned PriorGeneration = 0;2526  if (getContext().getLangOpts().Modules)2527    PriorGeneration = IdentifierGeneration[&II];2528 2529  // If there is a global index, look there first to determine which modules2530  // provably do not have any results for this identifier.2531  GlobalModuleIndex::HitSet Hits;2532  GlobalModuleIndex::HitSet *HitsPtr = nullptr;2533  if (!loadGlobalIndex()) {2534    if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {2535      HitsPtr = &Hits;2536    }2537  }2538 2539  IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,2540                                  NumIdentifierLookups,2541                                  NumIdentifierLookupHits);2542  ModuleMgr.visit(Visitor, HitsPtr);2543  markIdentifierUpToDate(&II);2544}2545 2546void ASTReader::markIdentifierUpToDate(const IdentifierInfo *II) {2547  if (!II)2548    return;2549 2550  const_cast<IdentifierInfo *>(II)->setOutOfDate(false);2551 2552  // Update the generation for this identifier.2553  if (getContext().getLangOpts().Modules)2554    IdentifierGeneration[II] = getGeneration();2555}2556 2557MacroID ASTReader::ReadMacroID(ModuleFile &F, const RecordDataImpl &Record,2558                               unsigned &Idx) {2559  uint64_t ModuleFileIndex = Record[Idx++] << 32;2560  uint64_t LocalIndex = Record[Idx++];2561  return getGlobalMacroID(F, (ModuleFileIndex | LocalIndex));2562}2563 2564void ASTReader::resolvePendingMacro(IdentifierInfo *II,2565                                    const PendingMacroInfo &PMInfo) {2566  ModuleFile &M = *PMInfo.M;2567 2568  BitstreamCursor &Cursor = M.MacroCursor;2569  SavedStreamPosition SavedPosition(Cursor);2570  if (llvm::Error Err =2571          Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {2572    Error(std::move(Err));2573    return;2574  }2575 2576  struct ModuleMacroRecord {2577    SubmoduleID SubModID;2578    MacroInfo *MI;2579    SmallVector<SubmoduleID, 8> Overrides;2580  };2581  llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;2582 2583  // We expect to see a sequence of PP_MODULE_MACRO records listing exported2584  // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete2585  // macro histroy.2586  RecordData Record;2587  while (true) {2588    Expected<llvm::BitstreamEntry> MaybeEntry =2589        Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);2590    if (!MaybeEntry) {2591      Error(MaybeEntry.takeError());2592      return;2593    }2594    llvm::BitstreamEntry Entry = MaybeEntry.get();2595 2596    if (Entry.Kind != llvm::BitstreamEntry::Record) {2597      Error("malformed block record in AST file");2598      return;2599    }2600 2601    Record.clear();2602    Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);2603    if (!MaybePP) {2604      Error(MaybePP.takeError());2605      return;2606    }2607    switch ((PreprocessorRecordTypes)MaybePP.get()) {2608    case PP_MACRO_DIRECTIVE_HISTORY:2609      break;2610 2611    case PP_MODULE_MACRO: {2612      ModuleMacros.push_back(ModuleMacroRecord());2613      auto &Info = ModuleMacros.back();2614      unsigned Idx = 0;2615      Info.SubModID = getGlobalSubmoduleID(M, Record[Idx++]);2616      Info.MI = getMacro(ReadMacroID(M, Record, Idx));2617      for (int I = Idx, N = Record.size(); I != N; ++I)2618        Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));2619      continue;2620    }2621 2622    default:2623      Error("malformed block record in AST file");2624      return;2625    }2626 2627    // We found the macro directive history; that's the last record2628    // for this macro.2629    break;2630  }2631 2632  // Module macros are listed in reverse dependency order.2633  {2634    std::reverse(ModuleMacros.begin(), ModuleMacros.end());2635    llvm::SmallVector<ModuleMacro*, 8> Overrides;2636    for (auto &MMR : ModuleMacros) {2637      Overrides.clear();2638      for (unsigned ModID : MMR.Overrides) {2639        Module *Mod = getSubmodule(ModID);2640        auto *Macro = PP.getModuleMacro(Mod, II);2641        assert(Macro && "missing definition for overridden macro");2642        Overrides.push_back(Macro);2643      }2644 2645      bool Inserted = false;2646      Module *Owner = getSubmodule(MMR.SubModID);2647      PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);2648    }2649  }2650 2651  // Don't read the directive history for a module; we don't have anywhere2652  // to put it.2653  if (M.isModule())2654    return;2655 2656  // Deserialize the macro directives history in reverse source-order.2657  MacroDirective *Latest = nullptr, *Earliest = nullptr;2658  unsigned Idx = 0, N = Record.size();2659  while (Idx < N) {2660    MacroDirective *MD = nullptr;2661    SourceLocation Loc = ReadSourceLocation(M, Record, Idx);2662    MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];2663    switch (K) {2664    case MacroDirective::MD_Define: {2665      MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));2666      MD = PP.AllocateDefMacroDirective(MI, Loc);2667      break;2668    }2669    case MacroDirective::MD_Undefine:2670      MD = PP.AllocateUndefMacroDirective(Loc);2671      break;2672    case MacroDirective::MD_Visibility:2673      bool isPublic = Record[Idx++];2674      MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);2675      break;2676    }2677 2678    if (!Latest)2679      Latest = MD;2680    if (Earliest)2681      Earliest->setPrevious(MD);2682    Earliest = MD;2683  }2684 2685  if (Latest)2686    PP.setLoadedMacroDirective(II, Earliest, Latest);2687}2688 2689bool ASTReader::shouldDisableValidationForFile(2690    const serialization::ModuleFile &M) const {2691  if (DisableValidationKind == DisableValidationForModuleKind::None)2692    return false;2693 2694  // If a PCH is loaded and validation is disabled for PCH then disable2695  // validation for the PCH and the modules it loads.2696  ModuleKind K = CurrentDeserializingModuleKind.value_or(M.Kind);2697 2698  switch (K) {2699  case MK_MainFile:2700  case MK_Preamble:2701  case MK_PCH:2702    return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);2703  case MK_ImplicitModule:2704  case MK_ExplicitModule:2705  case MK_PrebuiltModule:2706    return bool(DisableValidationKind & DisableValidationForModuleKind::Module);2707  }2708 2709  return false;2710}2711 2712static std::pair<StringRef, StringRef>2713getUnresolvedInputFilenames(const ASTReader::RecordData &Record,2714                            const StringRef InputBlob) {2715  uint16_t AsRequestedLength = Record[7];2716  return {InputBlob.substr(0, AsRequestedLength),2717          InputBlob.substr(AsRequestedLength)};2718}2719 2720InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {2721  // If this ID is bogus, just return an empty input file.2722  if (ID == 0 || ID > F.InputFileInfosLoaded.size())2723    return InputFileInfo();2724 2725  // If we've already loaded this input file, return it.2726  if (F.InputFileInfosLoaded[ID - 1].isValid())2727    return F.InputFileInfosLoaded[ID - 1];2728 2729  // Go find this input file.2730  BitstreamCursor &Cursor = F.InputFilesCursor;2731  SavedStreamPosition SavedPosition(Cursor);2732  if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +2733                                         F.InputFileOffsets[ID - 1])) {2734    // FIXME this drops errors on the floor.2735    consumeError(std::move(Err));2736  }2737 2738  Expected<unsigned> MaybeCode = Cursor.ReadCode();2739  if (!MaybeCode) {2740    // FIXME this drops errors on the floor.2741    consumeError(MaybeCode.takeError());2742  }2743  unsigned Code = MaybeCode.get();2744  RecordData Record;2745  StringRef Blob;2746 2747  if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))2748    assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&2749           "invalid record type for input file");2750  else {2751    // FIXME this drops errors on the floor.2752    consumeError(Maybe.takeError());2753  }2754 2755  assert(Record[0] == ID && "Bogus stored ID or offset");2756  InputFileInfo R;2757  R.StoredSize = static_cast<off_t>(Record[1]);2758  R.StoredTime = static_cast<time_t>(Record[2]);2759  R.Overridden = static_cast<bool>(Record[3]);2760  R.Transient = static_cast<bool>(Record[4]);2761  R.TopLevel = static_cast<bool>(Record[5]);2762  R.ModuleMap = static_cast<bool>(Record[6]);2763  auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =2764      getUnresolvedInputFilenames(Record, Blob);2765  R.UnresolvedImportedFilenameAsRequested = UnresolvedFilenameAsRequested;2766  R.UnresolvedImportedFilename = UnresolvedFilename.empty()2767                                     ? UnresolvedFilenameAsRequested2768                                     : UnresolvedFilename;2769 2770  Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();2771  if (!MaybeEntry) // FIXME this drops errors on the floor.2772    consumeError(MaybeEntry.takeError());2773  llvm::BitstreamEntry Entry = MaybeEntry.get();2774  assert(Entry.Kind == llvm::BitstreamEntry::Record &&2775         "expected record type for input file hash");2776 2777  Record.clear();2778  if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))2779    assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&2780           "invalid record type for input file hash");2781  else {2782    // FIXME this drops errors on the floor.2783    consumeError(Maybe.takeError());2784  }2785  R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |2786                  static_cast<uint64_t>(Record[0]);2787 2788  // Note that we've loaded this input file info.2789  F.InputFileInfosLoaded[ID - 1] = R;2790  return R;2791}2792 2793static unsigned moduleKindForDiagnostic(ModuleKind Kind);2794InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {2795  // If this ID is bogus, just return an empty input file.2796  if (ID == 0 || ID > F.InputFilesLoaded.size())2797    return InputFile();2798 2799  // If we've already loaded this input file, return it.2800  if (F.InputFilesLoaded[ID-1].getFile())2801    return F.InputFilesLoaded[ID-1];2802 2803  if (F.InputFilesLoaded[ID-1].isNotFound())2804    return InputFile();2805 2806  // Go find this input file.2807  BitstreamCursor &Cursor = F.InputFilesCursor;2808  SavedStreamPosition SavedPosition(Cursor);2809  if (llvm::Error Err = Cursor.JumpToBit(F.InputFilesOffsetBase +2810                                         F.InputFileOffsets[ID - 1])) {2811    // FIXME this drops errors on the floor.2812    consumeError(std::move(Err));2813  }2814 2815  InputFileInfo FI = getInputFileInfo(F, ID);2816  off_t StoredSize = FI.StoredSize;2817  time_t StoredTime = FI.StoredTime;2818  bool Overridden = FI.Overridden;2819  bool Transient = FI.Transient;2820  auto Filename =2821      ResolveImportedPath(PathBuf, FI.UnresolvedImportedFilenameAsRequested, F);2822  uint64_t StoredContentHash = FI.ContentHash;2823 2824  // For standard C++ modules, we don't need to check the inputs.2825  bool SkipChecks = F.StandardCXXModule;2826 2827  const HeaderSearchOptions &HSOpts =2828      PP.getHeaderSearchInfo().getHeaderSearchOpts();2829 2830  // The option ForceCheckCXX20ModulesInputFiles is only meaningful for C++202831  // modules.2832  if (F.StandardCXXModule && HSOpts.ForceCheckCXX20ModulesInputFiles) {2833    SkipChecks = false;2834    Overridden = false;2835  }2836 2837  auto File = FileMgr.getOptionalFileRef(*Filename, /*OpenFile=*/false);2838 2839  // For an overridden file, create a virtual file with the stored2840  // size/timestamp.2841  if ((Overridden || Transient || SkipChecks) && !File)2842    File = FileMgr.getVirtualFileRef(*Filename, StoredSize, StoredTime);2843 2844  if (!File) {2845    if (Complain) {2846      std::string ErrorStr = "could not find file '";2847      ErrorStr += *Filename;2848      ErrorStr += "' referenced by AST file '";2849      ErrorStr += F.FileName;2850      ErrorStr += "'";2851      Error(ErrorStr);2852    }2853    // Record that we didn't find the file.2854    F.InputFilesLoaded[ID-1] = InputFile::getNotFound();2855    return InputFile();2856  }2857 2858  // Check if there was a request to override the contents of the file2859  // that was part of the precompiled header. Overriding such a file2860  // can lead to problems when lexing using the source locations from the2861  // PCH.2862  SourceManager &SM = getSourceManager();2863  // FIXME: Reject if the overrides are different.2864  if ((!Overridden && !Transient) && !SkipChecks &&2865      SM.isFileOverridden(*File)) {2866    if (Complain)2867      Error(diag::err_fe_pch_file_overridden, *Filename);2868 2869    // After emitting the diagnostic, bypass the overriding file to recover2870    // (this creates a separate FileEntry).2871    File = SM.bypassFileContentsOverride(*File);2872    if (!File) {2873      F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();2874      return InputFile();2875    }2876  }2877 2878  struct Change {2879    enum ModificationKind {2880      Size,2881      ModTime,2882      Content,2883      None,2884    } Kind;2885    std::optional<int64_t> Old = std::nullopt;2886    std::optional<int64_t> New = std::nullopt;2887  };2888  auto HasInputContentChanged = [&](Change OriginalChange) {2889    assert(ValidateASTInputFilesContent &&2890           "We should only check the content of the inputs with "2891           "ValidateASTInputFilesContent enabled.");2892 2893    if (StoredContentHash == 0)2894      return OriginalChange;2895 2896    auto MemBuffOrError = FileMgr.getBufferForFile(*File);2897    if (!MemBuffOrError) {2898      if (!Complain)2899        return OriginalChange;2900      std::string ErrorStr = "could not get buffer for file '";2901      ErrorStr += File->getName();2902      ErrorStr += "'";2903      Error(ErrorStr);2904      return OriginalChange;2905    }2906 2907    auto ContentHash = xxh3_64bits(MemBuffOrError.get()->getBuffer());2908    if (StoredContentHash == static_cast<uint64_t>(ContentHash))2909      return Change{Change::None};2910 2911    return Change{Change::Content};2912  };2913  auto HasInputFileChanged = [&]() {2914    if (StoredSize != File->getSize())2915      return Change{Change::Size, StoredSize, File->getSize()};2916    if (!shouldDisableValidationForFile(F) && StoredTime &&2917        StoredTime != File->getModificationTime()) {2918      Change MTimeChange = {Change::ModTime, StoredTime,2919                            File->getModificationTime()};2920 2921      // In case the modification time changes but not the content,2922      // accept the cached file as legit.2923      if (ValidateASTInputFilesContent)2924        return HasInputContentChanged(MTimeChange);2925 2926      return MTimeChange;2927    }2928    return Change{Change::None};2929  };2930 2931  bool IsOutOfDate = false;2932  auto FileChange = SkipChecks ? Change{Change::None} : HasInputFileChanged();2933  // When ForceCheckCXX20ModulesInputFiles and ValidateASTInputFilesContent2934  // enabled, it is better to check the contents of the inputs. Since we can't2935  // get correct modified time information for inputs from overriden inputs.2936  if (HSOpts.ForceCheckCXX20ModulesInputFiles && ValidateASTInputFilesContent &&2937      F.StandardCXXModule && FileChange.Kind == Change::None)2938    FileChange = HasInputContentChanged(FileChange);2939 2940  // When we have StoredTime equal to zero and ValidateASTInputFilesContent,2941  // it is better to check the content of the input files because we cannot rely2942  // on the file modification time, which will be the same (zero) for these2943  // files.2944  if (!StoredTime && ValidateASTInputFilesContent &&2945      FileChange.Kind == Change::None)2946    FileChange = HasInputContentChanged(FileChange);2947 2948  // For an overridden file, there is nothing to validate.2949  if (!Overridden && FileChange.Kind != Change::None) {2950    if (Complain) {2951      // Build a list of the PCH imports that got us here (in reverse).2952      SmallVector<ModuleFile *, 4> ImportStack(1, &F);2953      while (!ImportStack.back()->ImportedBy.empty())2954        ImportStack.push_back(ImportStack.back()->ImportedBy[0]);2955 2956      // The top-level AST file is stale.2957      StringRef TopLevelASTFileName(ImportStack.back()->FileName);2958      Diag(diag::err_fe_ast_file_modified)2959          << *Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)2960          << TopLevelASTFileName << FileChange.Kind2961          << (FileChange.Old && FileChange.New)2962          << llvm::itostr(FileChange.Old.value_or(0))2963          << llvm::itostr(FileChange.New.value_or(0));2964 2965      // Print the import stack.2966      if (ImportStack.size() > 1) {2967        Diag(diag::note_ast_file_required_by)2968            << *Filename << ImportStack[0]->FileName;2969        for (unsigned I = 1; I < ImportStack.size(); ++I)2970          Diag(diag::note_ast_file_required_by)2971              << ImportStack[I - 1]->FileName << ImportStack[I]->FileName;2972      }2973 2974      Diag(diag::note_ast_file_rebuild_required) << TopLevelASTFileName;2975    }2976 2977    IsOutOfDate = true;2978  }2979  // FIXME: If the file is overridden and we've already opened it,2980  // issue an error (or split it into a separate FileEntry).2981 2982  InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);2983 2984  // Note that we've loaded this input file.2985  F.InputFilesLoaded[ID-1] = IF;2986  return IF;2987}2988 2989ASTReader::TemporarilyOwnedStringRef2990ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,2991                               ModuleFile &ModF) {2992  return ResolveImportedPath(Buf, Path, ModF.BaseDirectory);2993}2994 2995ASTReader::TemporarilyOwnedStringRef2996ASTReader::ResolveImportedPath(SmallString<0> &Buf, StringRef Path,2997                               StringRef Prefix) {2998  assert(Buf.capacity() != 0 && "Overlapping ResolveImportedPath calls");2999 3000  if (Prefix.empty() || Path.empty() || llvm::sys::path::is_absolute(Path) ||3001      Path == "<built-in>" || Path == "<command line>")3002    return {Path, Buf};3003 3004  Buf.clear();3005  llvm::sys::path::append(Buf, Prefix, Path);3006  StringRef ResolvedPath{Buf.data(), Buf.size()};3007  return {ResolvedPath, Buf};3008}3009 3010std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,3011                                                      StringRef P,3012                                                      ModuleFile &ModF) {3013  return ResolveImportedPathAndAllocate(Buf, P, ModF.BaseDirectory);3014}3015 3016std::string ASTReader::ResolveImportedPathAndAllocate(SmallString<0> &Buf,3017                                                      StringRef P,3018                                                      StringRef Prefix) {3019  auto ResolvedPath = ResolveImportedPath(Buf, P, Prefix);3020  return ResolvedPath->str();3021}3022 3023static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {3024  switch (ARR) {3025  case ASTReader::Failure: return true;3026  case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);3027  case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);3028  case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);3029  case ASTReader::ConfigurationMismatch:3030    return !(Caps & ASTReader::ARR_ConfigurationMismatch);3031  case ASTReader::HadErrors: return true;3032  case ASTReader::Success: return false;3033  }3034 3035  llvm_unreachable("unknown ASTReadResult");3036}3037 3038ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(3039    BitstreamCursor &Stream, StringRef Filename,3040    unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,3041    ASTReaderListener &Listener, std::string &SuggestedPredefines) {3042  if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {3043    // FIXME this drops errors on the floor.3044    consumeError(std::move(Err));3045    return Failure;3046  }3047 3048  // Read all of the records in the options block.3049  RecordData Record;3050  ASTReadResult Result = Success;3051  while (true) {3052    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();3053    if (!MaybeEntry) {3054      // FIXME this drops errors on the floor.3055      consumeError(MaybeEntry.takeError());3056      return Failure;3057    }3058    llvm::BitstreamEntry Entry = MaybeEntry.get();3059 3060    switch (Entry.Kind) {3061    case llvm::BitstreamEntry::Error:3062    case llvm::BitstreamEntry::SubBlock:3063      return Failure;3064 3065    case llvm::BitstreamEntry::EndBlock:3066      return Result;3067 3068    case llvm::BitstreamEntry::Record:3069      // The interesting case.3070      break;3071    }3072 3073    // Read and process a record.3074    Record.clear();3075    Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);3076    if (!MaybeRecordType) {3077      // FIXME this drops errors on the floor.3078      consumeError(MaybeRecordType.takeError());3079      return Failure;3080    }3081    switch ((OptionsRecordTypes)MaybeRecordType.get()) {3082    case LANGUAGE_OPTIONS: {3083      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;3084      if (ParseLanguageOptions(Record, Filename, Complain, Listener,3085                               AllowCompatibleConfigurationMismatch))3086        Result = ConfigurationMismatch;3087      break;3088    }3089 3090    case CODEGEN_OPTIONS: {3091      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;3092      if (ParseCodeGenOptions(Record, Filename, Complain, Listener,3093                              AllowCompatibleConfigurationMismatch))3094        Result = ConfigurationMismatch;3095      break;3096    }3097 3098    case TARGET_OPTIONS: {3099      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;3100      if (ParseTargetOptions(Record, Filename, Complain, Listener,3101                             AllowCompatibleConfigurationMismatch))3102        Result = ConfigurationMismatch;3103      break;3104    }3105 3106    case FILE_SYSTEM_OPTIONS: {3107      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;3108      if (!AllowCompatibleConfigurationMismatch &&3109          ParseFileSystemOptions(Record, Complain, Listener))3110        Result = ConfigurationMismatch;3111      break;3112    }3113 3114    case HEADER_SEARCH_OPTIONS: {3115      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;3116      if (!AllowCompatibleConfigurationMismatch &&3117          ParseHeaderSearchOptions(Record, Filename, Complain, Listener))3118        Result = ConfigurationMismatch;3119      break;3120    }3121 3122    case PREPROCESSOR_OPTIONS:3123      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;3124      if (!AllowCompatibleConfigurationMismatch &&3125          ParsePreprocessorOptions(Record, Filename, Complain, Listener,3126                                   SuggestedPredefines))3127        Result = ConfigurationMismatch;3128      break;3129    }3130  }3131}3132 3133ASTReader::ASTReadResult3134ASTReader::ReadControlBlock(ModuleFile &F,3135                            SmallVectorImpl<ImportedModule> &Loaded,3136                            const ModuleFile *ImportedBy,3137                            unsigned ClientLoadCapabilities) {3138  BitstreamCursor &Stream = F.Stream;3139 3140  if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {3141    Error(std::move(Err));3142    return Failure;3143  }3144 3145  // Lambda to read the unhashed control block the first time it's called.3146  //3147  // For PCM files, the unhashed control block cannot be read until after the3148  // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still3149  // need to look ahead before reading the IMPORTS record.  For consistency,3150  // this block is always read somehow (see BitstreamEntry::EndBlock).3151  bool HasReadUnhashedControlBlock = false;3152  auto readUnhashedControlBlockOnce = [&]() {3153    if (!HasReadUnhashedControlBlock) {3154      HasReadUnhashedControlBlock = true;3155      if (ASTReadResult Result =3156              readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))3157        return Result;3158    }3159    return Success;3160  };3161 3162  bool DisableValidation = shouldDisableValidationForFile(F);3163 3164  // Read all of the records and blocks in the control block.3165  RecordData Record;3166  unsigned NumInputs = 0;3167  unsigned NumUserInputs = 0;3168  StringRef BaseDirectoryAsWritten;3169  while (true) {3170    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();3171    if (!MaybeEntry) {3172      Error(MaybeEntry.takeError());3173      return Failure;3174    }3175    llvm::BitstreamEntry Entry = MaybeEntry.get();3176 3177    switch (Entry.Kind) {3178    case llvm::BitstreamEntry::Error:3179      Error("malformed block record in AST file");3180      return Failure;3181    case llvm::BitstreamEntry::EndBlock: {3182      // Validate the module before returning.  This call catches an AST with3183      // no module name and no imports.3184      if (ASTReadResult Result = readUnhashedControlBlockOnce())3185        return Result;3186 3187      // Validate input files.3188      const HeaderSearchOptions &HSOpts =3189          PP.getHeaderSearchInfo().getHeaderSearchOpts();3190 3191      // All user input files reside at the index range [0, NumUserInputs), and3192      // system input files reside at [NumUserInputs, NumInputs). For explicitly3193      // loaded module files, ignore missing inputs.3194      if (!DisableValidation && F.Kind != MK_ExplicitModule &&3195          F.Kind != MK_PrebuiltModule) {3196        bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;3197 3198        // If we are reading a module, we will create a verification timestamp,3199        // so we verify all input files.  Otherwise, verify only user input3200        // files.3201 3202        unsigned N = ValidateSystemInputs ? NumInputs : NumUserInputs;3203        if (HSOpts.ModulesValidateOncePerBuildSession &&3204            F.InputFilesValidationTimestamp > HSOpts.BuildSessionTimestamp &&3205            F.Kind == MK_ImplicitModule)3206          N = ForceValidateUserInputs ? NumUserInputs : 0;3207 3208        if (N != 0)3209          Diag(diag::remark_module_validation)3210              << N << F.ModuleName << F.FileName;3211 3212        for (unsigned I = 0; I < N; ++I) {3213          InputFile IF = getInputFile(F, I+1, Complain);3214          if (!IF.getFile() || IF.isOutOfDate())3215            return OutOfDate;3216        }3217      }3218 3219      if (Listener)3220        Listener->visitModuleFile(F.FileName, F.Kind);3221 3222      if (Listener && Listener->needsInputFileVisitation()) {3223        unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs3224                                                                : NumUserInputs;3225        for (unsigned I = 0; I < N; ++I) {3226          bool IsSystem = I >= NumUserInputs;3227          InputFileInfo FI = getInputFileInfo(F, I + 1);3228          auto FilenameAsRequested = ResolveImportedPath(3229              PathBuf, FI.UnresolvedImportedFilenameAsRequested, F);3230          Listener->visitInputFile(3231              *FilenameAsRequested, IsSystem, FI.Overridden,3232              F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule);3233        }3234      }3235 3236      return Success;3237    }3238 3239    case llvm::BitstreamEntry::SubBlock:3240      switch (Entry.ID) {3241      case INPUT_FILES_BLOCK_ID:3242        F.InputFilesCursor = Stream;3243        if (llvm::Error Err = Stream.SkipBlock()) {3244          Error(std::move(Err));3245          return Failure;3246        }3247        if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {3248          Error("malformed block record in AST file");3249          return Failure;3250        }3251        F.InputFilesOffsetBase = F.InputFilesCursor.GetCurrentBitNo();3252        continue;3253 3254      case OPTIONS_BLOCK_ID:3255        // If we're reading the first module for this group, check its options3256        // are compatible with ours. For modules it imports, no further checking3257        // is required, because we checked them when we built it.3258        if (Listener && !ImportedBy) {3259          // Should we allow the configuration of the module file to differ from3260          // the configuration of the current translation unit in a compatible3261          // way?3262          //3263          // FIXME: Allow this for files explicitly specified with -include-pch.3264          bool AllowCompatibleConfigurationMismatch =3265              F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;3266 3267          ASTReadResult Result =3268              ReadOptionsBlock(Stream, F.FileName, ClientLoadCapabilities,3269                               AllowCompatibleConfigurationMismatch, *Listener,3270                               SuggestedPredefines);3271          if (Result == Failure) {3272            Error("malformed block record in AST file");3273            return Result;3274          }3275 3276          if (DisableValidation ||3277              (AllowConfigurationMismatch && Result == ConfigurationMismatch))3278            Result = Success;3279 3280          // If we can't load the module, exit early since we likely3281          // will rebuild the module anyway. The stream may be in the3282          // middle of a block.3283          if (Result != Success)3284            return Result;3285        } else if (llvm::Error Err = Stream.SkipBlock()) {3286          Error(std::move(Err));3287          return Failure;3288        }3289        continue;3290 3291      default:3292        if (llvm::Error Err = Stream.SkipBlock()) {3293          Error(std::move(Err));3294          return Failure;3295        }3296        continue;3297      }3298 3299    case llvm::BitstreamEntry::Record:3300      // The interesting case.3301      break;3302    }3303 3304    // Read and process a record.3305    Record.clear();3306    StringRef Blob;3307    Expected<unsigned> MaybeRecordType =3308        Stream.readRecord(Entry.ID, Record, &Blob);3309    if (!MaybeRecordType) {3310      Error(MaybeRecordType.takeError());3311      return Failure;3312    }3313    switch ((ControlRecordTypes)MaybeRecordType.get()) {3314    case METADATA: {3315      if (Record[0] != VERSION_MAJOR && !DisableValidation) {3316        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)3317          Diag(Record[0] < VERSION_MAJOR ? diag::err_ast_file_version_too_old3318                                         : diag::err_ast_file_version_too_new)3319              << moduleKindForDiagnostic(F.Kind) << F.FileName;3320        return VersionMismatch;3321      }3322 3323      bool hasErrors = Record[7];3324      if (hasErrors && !DisableValidation) {3325        // If requested by the caller and the module hasn't already been read3326        // or compiled, mark modules on error as out-of-date.3327        if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&3328            canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))3329          return OutOfDate;3330 3331        if (!AllowASTWithCompilerErrors) {3332          Diag(diag::err_ast_file_with_compiler_errors)3333              << moduleKindForDiagnostic(F.Kind) << F.FileName;3334          return HadErrors;3335        }3336      }3337      if (hasErrors) {3338        Diags.ErrorOccurred = true;3339        Diags.UncompilableErrorOccurred = true;3340        Diags.UnrecoverableErrorOccurred = true;3341      }3342 3343      F.RelocatablePCH = Record[4];3344      // Relative paths in a relocatable PCH are relative to our sysroot.3345      if (F.RelocatablePCH)3346        F.BaseDirectory = isysroot.empty() ? "/" : isysroot;3347 3348      F.StandardCXXModule = Record[5];3349 3350      F.HasTimestamps = Record[6];3351 3352      const std::string &CurBranch = getClangFullRepositoryVersion();3353      StringRef ASTBranch = Blob;3354      if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {3355        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)3356          Diag(diag::err_ast_file_different_branch)3357              << moduleKindForDiagnostic(F.Kind) << F.FileName << ASTBranch3358              << CurBranch;3359        return VersionMismatch;3360      }3361      break;3362    }3363 3364    case IMPORT: {3365      // Validate the AST before processing any imports (otherwise, untangling3366      // them can be error-prone and expensive).  A module will have a name and3367      // will already have been validated, but this catches the PCH case.3368      if (ASTReadResult Result = readUnhashedControlBlockOnce())3369        return Result;3370 3371      unsigned Idx = 0;3372      // Read information about the AST file.3373      ModuleKind ImportedKind = (ModuleKind)Record[Idx++];3374 3375      // The import location will be the local one for now; we will adjust3376      // all import locations of module imports after the global source3377      // location info are setup, in ReadAST.3378      auto [ImportLoc, ImportModuleFileIndex] =3379          ReadUntranslatedSourceLocation(Record[Idx++]);3380      // The import location must belong to the current module file itself.3381      assert(ImportModuleFileIndex == 0);3382 3383      StringRef ImportedName = ReadStringBlob(Record, Idx, Blob);3384 3385      bool IsImportingStdCXXModule = Record[Idx++];3386 3387      off_t StoredSize = 0;3388      time_t StoredModTime = 0;3389      ASTFileSignature StoredSignature;3390      std::string ImportedFile;3391      std::string StoredFile;3392      bool IgnoreImportedByNote = false;3393 3394      // For prebuilt and explicit modules first consult the file map for3395      // an override. Note that here we don't search prebuilt module3396      // directories if we're not importing standard c++ module, only the3397      // explicit name to file mappings. Also, we will still verify the3398      // size/signature making sure it is essentially the same file but3399      // perhaps in a different location.3400      if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)3401        ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(3402            ImportedName, /*FileMapOnly*/ !IsImportingStdCXXModule);3403 3404      if (IsImportingStdCXXModule && ImportedFile.empty()) {3405        Diag(diag::err_failed_to_find_module_file) << ImportedName;3406        return Missing;3407      }3408 3409      if (!IsImportingStdCXXModule) {3410        StoredSize = (off_t)Record[Idx++];3411        StoredModTime = (time_t)Record[Idx++];3412 3413        StringRef SignatureBytes = Blob.substr(0, ASTFileSignature::size);3414        StoredSignature = ASTFileSignature::create(SignatureBytes.begin(),3415                                                   SignatureBytes.end());3416        Blob = Blob.substr(ASTFileSignature::size);3417 3418        // Use BaseDirectoryAsWritten to ensure we use the same path in the3419        // ModuleCache as when writing.3420        StoredFile = ReadPathBlob(BaseDirectoryAsWritten, Record, Idx, Blob);3421        if (ImportedFile.empty()) {3422          ImportedFile = StoredFile;3423        } else if (!getDiags().isIgnored(3424                       diag::warn_module_file_mapping_mismatch,3425                       CurrentImportLoc)) {3426          auto ImportedFileRef =3427              PP.getFileManager().getOptionalFileRef(ImportedFile);3428          auto StoredFileRef =3429              PP.getFileManager().getOptionalFileRef(StoredFile);3430          if ((ImportedFileRef && StoredFileRef) &&3431              (*ImportedFileRef != *StoredFileRef)) {3432            Diag(diag::warn_module_file_mapping_mismatch)3433                << ImportedFile << StoredFile;3434            Diag(diag::note_module_file_imported_by)3435                << F.FileName << !F.ModuleName.empty() << F.ModuleName;3436            IgnoreImportedByNote = true;3437          }3438        }3439      }3440 3441      // If our client can't cope with us being out of date, we can't cope with3442      // our dependency being missing.3443      unsigned Capabilities = ClientLoadCapabilities;3444      if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)3445        Capabilities &= ~ARR_Missing;3446 3447      // Load the AST file.3448      auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,3449                                Loaded, StoredSize, StoredModTime,3450                                StoredSignature, Capabilities);3451 3452      // Check the AST we just read from ImportedFile contains a different3453      // module than we expected (ImportedName). This can occur for C++203454      // Modules when given a mismatch via -fmodule-file=<name>=<file>3455      if (IsImportingStdCXXModule) {3456        if (const auto *Imported =3457                getModuleManager().lookupByFileName(ImportedFile);3458            Imported != nullptr && Imported->ModuleName != ImportedName) {3459          Diag(diag::err_failed_to_find_module_file) << ImportedName;3460          Result = Missing;3461        }3462      }3463 3464      // If we diagnosed a problem, produce a backtrace.3465      bool recompilingFinalized = Result == OutOfDate &&3466                                  (Capabilities & ARR_OutOfDate) &&3467                                  getModuleManager()3468                                      .getModuleCache()3469                                      .getInMemoryModuleCache()3470                                      .isPCMFinal(F.FileName);3471      if (!IgnoreImportedByNote &&3472          (isDiagnosedResult(Result, Capabilities) || recompilingFinalized))3473        Diag(diag::note_module_file_imported_by)3474            << F.FileName << !F.ModuleName.empty() << F.ModuleName;3475 3476      switch (Result) {3477      case Failure: return Failure;3478        // If we have to ignore the dependency, we'll have to ignore this too.3479      case Missing:3480      case OutOfDate: return OutOfDate;3481      case VersionMismatch: return VersionMismatch;3482      case ConfigurationMismatch: return ConfigurationMismatch;3483      case HadErrors: return HadErrors;3484      case Success: break;3485      }3486      break;3487    }3488 3489    case ORIGINAL_FILE:3490      F.OriginalSourceFileID = FileID::get(Record[0]);3491      F.ActualOriginalSourceFileName = std::string(Blob);3492      F.OriginalSourceFileName = ResolveImportedPathAndAllocate(3493          PathBuf, F.ActualOriginalSourceFileName, F);3494      break;3495 3496    case ORIGINAL_FILE_ID:3497      F.OriginalSourceFileID = FileID::get(Record[0]);3498      break;3499 3500    case MODULE_NAME:3501      F.ModuleName = std::string(Blob);3502      Diag(diag::remark_module_import)3503          << F.ModuleName << F.FileName << (ImportedBy ? true : false)3504          << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());3505      if (Listener)3506        Listener->ReadModuleName(F.ModuleName);3507 3508      // Validate the AST as soon as we have a name so we can exit early on3509      // failure.3510      if (ASTReadResult Result = readUnhashedControlBlockOnce())3511        return Result;3512 3513      break;3514 3515    case MODULE_DIRECTORY: {3516      // Save the BaseDirectory as written in the PCM for computing the module3517      // filename for the ModuleCache.3518      BaseDirectoryAsWritten = Blob;3519      assert(!F.ModuleName.empty() &&3520             "MODULE_DIRECTORY found before MODULE_NAME");3521      F.BaseDirectory = std::string(Blob);3522      if (!PP.getPreprocessorOpts().ModulesCheckRelocated)3523        break;3524      // If we've already loaded a module map file covering this module, we may3525      // have a better path for it (relative to the current build).3526      Module *M = PP.getHeaderSearchInfo().lookupModule(3527          F.ModuleName, SourceLocation(), /*AllowSearch*/ true,3528          /*AllowExtraModuleMapSearch*/ true);3529      if (M && M->Directory) {3530        // If we're implicitly loading a module, the base directory can't3531        // change between the build and use.3532        // Don't emit module relocation error if we have -fno-validate-pch3533        if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &3534                  DisableValidationForModuleKind::Module) &&3535            F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {3536          auto BuildDir = PP.getFileManager().getOptionalDirectoryRef(Blob);3537          if (!BuildDir || *BuildDir != M->Directory) {3538            if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))3539              Diag(diag::err_imported_module_relocated)3540                  << F.ModuleName << Blob << M->Directory->getName();3541            return OutOfDate;3542          }3543        }3544        F.BaseDirectory = std::string(M->Directory->getName());3545      }3546      break;3547    }3548 3549    case MODULE_MAP_FILE:3550      if (ASTReadResult Result =3551              ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))3552        return Result;3553      break;3554 3555    case INPUT_FILE_OFFSETS:3556      NumInputs = Record[0];3557      NumUserInputs = Record[1];3558      F.InputFileOffsets =3559          (const llvm::support::unaligned_uint64_t *)Blob.data();3560      F.InputFilesLoaded.resize(NumInputs);3561      F.InputFileInfosLoaded.resize(NumInputs);3562      F.NumUserInputFiles = NumUserInputs;3563      break;3564    }3565  }3566}3567 3568llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,3569                                    unsigned ClientLoadCapabilities) {3570  BitstreamCursor &Stream = F.Stream;3571 3572  if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))3573    return Err;3574  F.ASTBlockStartOffset = Stream.GetCurrentBitNo();3575 3576  // Read all of the records and blocks for the AST file.3577  RecordData Record;3578  while (true) {3579    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();3580    if (!MaybeEntry)3581      return MaybeEntry.takeError();3582    llvm::BitstreamEntry Entry = MaybeEntry.get();3583 3584    switch (Entry.Kind) {3585    case llvm::BitstreamEntry::Error:3586      return llvm::createStringError(3587          std::errc::illegal_byte_sequence,3588          "error at end of module block in AST file");3589    case llvm::BitstreamEntry::EndBlock:3590      // Outside of C++, we do not store a lookup map for the translation unit.3591      // Instead, mark it as needing a lookup map to be built if this module3592      // contains any declarations lexically within it (which it always does!).3593      // This usually has no cost, since we very rarely need the lookup map for3594      // the translation unit outside C++.3595      if (ASTContext *Ctx = ContextObj) {3596        DeclContext *DC = Ctx->getTranslationUnitDecl();3597        if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)3598          DC->setMustBuildLookupTable();3599      }3600 3601      return llvm::Error::success();3602    case llvm::BitstreamEntry::SubBlock:3603      switch (Entry.ID) {3604      case DECLTYPES_BLOCK_ID:3605        // We lazily load the decls block, but we want to set up the3606        // DeclsCursor cursor to point into it.  Clone our current bitcode3607        // cursor to it, enter the block and read the abbrevs in that block.3608        // With the main cursor, we just skip over it.3609        F.DeclsCursor = Stream;3610        if (llvm::Error Err = Stream.SkipBlock())3611          return Err;3612        if (llvm::Error Err = ReadBlockAbbrevs(3613                F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))3614          return Err;3615        break;3616 3617      case PREPROCESSOR_BLOCK_ID:3618        F.MacroCursor = Stream;3619        if (!PP.getExternalSource())3620          PP.setExternalSource(this);3621 3622        if (llvm::Error Err = Stream.SkipBlock())3623          return Err;3624        if (llvm::Error Err =3625                ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))3626          return Err;3627        F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();3628        break;3629 3630      case PREPROCESSOR_DETAIL_BLOCK_ID:3631        F.PreprocessorDetailCursor = Stream;3632 3633        if (llvm::Error Err = Stream.SkipBlock()) {3634          return Err;3635        }3636        if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,3637                                               PREPROCESSOR_DETAIL_BLOCK_ID))3638          return Err;3639        F.PreprocessorDetailStartOffset3640        = F.PreprocessorDetailCursor.GetCurrentBitNo();3641 3642        if (!PP.getPreprocessingRecord())3643          PP.createPreprocessingRecord();3644        if (!PP.getPreprocessingRecord()->getExternalSource())3645          PP.getPreprocessingRecord()->SetExternalSource(*this);3646        break;3647 3648      case SOURCE_MANAGER_BLOCK_ID:3649        if (llvm::Error Err = ReadSourceManagerBlock(F))3650          return Err;3651        break;3652 3653      case SUBMODULE_BLOCK_ID:3654        if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))3655          return Err;3656        break;3657 3658      case COMMENTS_BLOCK_ID: {3659        BitstreamCursor C = Stream;3660 3661        if (llvm::Error Err = Stream.SkipBlock())3662          return Err;3663        if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))3664          return Err;3665        CommentsCursors.push_back(std::make_pair(C, &F));3666        break;3667      }3668 3669      default:3670        if (llvm::Error Err = Stream.SkipBlock())3671          return Err;3672        break;3673      }3674      continue;3675 3676    case llvm::BitstreamEntry::Record:3677      // The interesting case.3678      break;3679    }3680 3681    // Read and process a record.3682    Record.clear();3683    StringRef Blob;3684    Expected<unsigned> MaybeRecordType =3685        Stream.readRecord(Entry.ID, Record, &Blob);3686    if (!MaybeRecordType)3687      return MaybeRecordType.takeError();3688    ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();3689 3690    // If we're not loading an AST context, we don't care about most records.3691    if (!ContextObj) {3692      switch (RecordType) {3693      case IDENTIFIER_TABLE:3694      case IDENTIFIER_OFFSET:3695      case INTERESTING_IDENTIFIERS:3696      case STATISTICS:3697      case PP_ASSUME_NONNULL_LOC:3698      case PP_CONDITIONAL_STACK:3699      case PP_COUNTER_VALUE:3700      case SOURCE_LOCATION_OFFSETS:3701      case MODULE_OFFSET_MAP:3702      case SOURCE_MANAGER_LINE_TABLE:3703      case PPD_ENTITIES_OFFSETS:3704      case HEADER_SEARCH_TABLE:3705      case IMPORTED_MODULES:3706      case MACRO_OFFSET:3707        break;3708      default:3709        continue;3710      }3711    }3712 3713    switch (RecordType) {3714    default:  // Default behavior: ignore.3715      break;3716 3717    case TYPE_OFFSET: {3718      if (F.LocalNumTypes != 0)3719        return llvm::createStringError(3720            std::errc::illegal_byte_sequence,3721            "duplicate TYPE_OFFSET record in AST file");3722      F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());3723      F.LocalNumTypes = Record[0];3724      F.BaseTypeIndex = getTotalNumTypes();3725 3726      if (F.LocalNumTypes > 0)3727        TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);3728 3729      break;3730    }3731 3732    case DECL_OFFSET: {3733      if (F.LocalNumDecls != 0)3734        return llvm::createStringError(3735            std::errc::illegal_byte_sequence,3736            "duplicate DECL_OFFSET record in AST file");3737      F.DeclOffsets = (const DeclOffset *)Blob.data();3738      F.LocalNumDecls = Record[0];3739      F.BaseDeclIndex = getTotalNumDecls();3740 3741      if (F.LocalNumDecls > 0)3742        DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);3743 3744      break;3745    }3746 3747    case TU_UPDATE_LEXICAL: {3748      DeclContext *TU = ContextObj->getTranslationUnitDecl();3749      LexicalContents Contents(3750          reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),3751          static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));3752      TULexicalDecls.push_back(std::make_pair(&F, Contents));3753      TU->setHasExternalLexicalStorage(true);3754      break;3755    }3756 3757    case UPDATE_VISIBLE: {3758      unsigned Idx = 0;3759      GlobalDeclID ID = ReadDeclID(F, Record, Idx);3760      auto *Data = (const unsigned char*)Blob.data();3761      PendingVisibleUpdates[ID].push_back(UpdateData{&F, Data});3762      // If we've already loaded the decl, perform the updates when we finish3763      // loading this block.3764      if (Decl *D = GetExistingDecl(ID))3765        PendingUpdateRecords.push_back(3766            PendingUpdateRecord(ID, D, /*JustLoaded=*/false));3767      break;3768    }3769 3770    case UPDATE_MODULE_LOCAL_VISIBLE: {3771      unsigned Idx = 0;3772      GlobalDeclID ID = ReadDeclID(F, Record, Idx);3773      auto *Data = (const unsigned char *)Blob.data();3774      PendingModuleLocalVisibleUpdates[ID].push_back(UpdateData{&F, Data});3775      // If we've already loaded the decl, perform the updates when we finish3776      // loading this block.3777      if (Decl *D = GetExistingDecl(ID))3778        PendingUpdateRecords.push_back(3779            PendingUpdateRecord(ID, D, /*JustLoaded=*/false));3780      break;3781    }3782 3783    case UPDATE_TU_LOCAL_VISIBLE: {3784      if (F.Kind != MK_MainFile)3785        break;3786      unsigned Idx = 0;3787      GlobalDeclID ID = ReadDeclID(F, Record, Idx);3788      auto *Data = (const unsigned char *)Blob.data();3789      TULocalUpdates[ID].push_back(UpdateData{&F, Data});3790      // If we've already loaded the decl, perform the updates when we finish3791      // loading this block.3792      if (Decl *D = GetExistingDecl(ID))3793        PendingUpdateRecords.push_back(3794            PendingUpdateRecord(ID, D, /*JustLoaded=*/false));3795      break;3796    }3797 3798    case CXX_ADDED_TEMPLATE_SPECIALIZATION: {3799      unsigned Idx = 0;3800      GlobalDeclID ID = ReadDeclID(F, Record, Idx);3801      auto *Data = (const unsigned char *)Blob.data();3802      PendingSpecializationsUpdates[ID].push_back(UpdateData{&F, Data});3803      // If we've already loaded the decl, perform the updates when we finish3804      // loading this block.3805      if (Decl *D = GetExistingDecl(ID))3806        PendingUpdateRecords.push_back(3807            PendingUpdateRecord(ID, D, /*JustLoaded=*/false));3808      break;3809    }3810 3811    case CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION: {3812      unsigned Idx = 0;3813      GlobalDeclID ID = ReadDeclID(F, Record, Idx);3814      auto *Data = (const unsigned char *)Blob.data();3815      PendingPartialSpecializationsUpdates[ID].push_back(UpdateData{&F, Data});3816      // If we've already loaded the decl, perform the updates when we finish3817      // loading this block.3818      if (Decl *D = GetExistingDecl(ID))3819        PendingUpdateRecords.push_back(3820            PendingUpdateRecord(ID, D, /*JustLoaded=*/false));3821      break;3822    }3823 3824    case IDENTIFIER_TABLE:3825      F.IdentifierTableData =3826          reinterpret_cast<const unsigned char *>(Blob.data());3827      if (Record[0]) {3828        F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(3829            F.IdentifierTableData + Record[0],3830            F.IdentifierTableData + sizeof(uint32_t),3831            F.IdentifierTableData,3832            ASTIdentifierLookupTrait(*this, F));3833 3834        PP.getIdentifierTable().setExternalIdentifierLookup(this);3835      }3836      break;3837 3838    case IDENTIFIER_OFFSET: {3839      if (F.LocalNumIdentifiers != 0)3840        return llvm::createStringError(3841            std::errc::illegal_byte_sequence,3842            "duplicate IDENTIFIER_OFFSET record in AST file");3843      F.IdentifierOffsets = (const uint32_t *)Blob.data();3844      F.LocalNumIdentifiers = Record[0];3845      F.BaseIdentifierID = getTotalNumIdentifiers();3846 3847      if (F.LocalNumIdentifiers > 0)3848        IdentifiersLoaded.resize(IdentifiersLoaded.size()3849                                 + F.LocalNumIdentifiers);3850      break;3851    }3852 3853    case INTERESTING_IDENTIFIERS:3854      F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());3855      break;3856 3857    case EAGERLY_DESERIALIZED_DECLS:3858      // FIXME: Skip reading this record if our ASTConsumer doesn't care3859      // about "interesting" decls (for instance, if we're building a module).3860      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)3861        EagerlyDeserializedDecls.push_back(ReadDeclID(F, Record, I));3862      break;3863 3864    case MODULAR_CODEGEN_DECLS:3865      // FIXME: Skip reading this record if our ASTConsumer doesn't care about3866      // them (ie: if we're not codegenerating this module).3867      if (F.Kind == MK_MainFile ||3868          getContext().getLangOpts().BuildingPCHWithObjectFile)3869        for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)3870          EagerlyDeserializedDecls.push_back(ReadDeclID(F, Record, I));3871      break;3872 3873    case SPECIAL_TYPES:3874      if (SpecialTypes.empty()) {3875        for (unsigned I = 0, N = Record.size(); I != N; ++I)3876          SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));3877        break;3878      }3879 3880      if (Record.empty())3881        break;3882 3883      if (SpecialTypes.size() != Record.size())3884        return llvm::createStringError(std::errc::illegal_byte_sequence,3885                                       "invalid special-types record");3886 3887      for (unsigned I = 0, N = Record.size(); I != N; ++I) {3888        serialization::TypeID ID = getGlobalTypeID(F, Record[I]);3889        if (!SpecialTypes[I])3890          SpecialTypes[I] = ID;3891        // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate3892        // merge step?3893      }3894      break;3895 3896    case STATISTICS:3897      TotalNumStatements += Record[0];3898      TotalNumMacros += Record[1];3899      TotalLexicalDeclContexts += Record[2];3900      TotalVisibleDeclContexts += Record[3];3901      TotalModuleLocalVisibleDeclContexts += Record[4];3902      TotalTULocalVisibleDeclContexts += Record[5];3903      break;3904 3905    case UNUSED_FILESCOPED_DECLS:3906      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)3907        UnusedFileScopedDecls.push_back(ReadDeclID(F, Record, I));3908      break;3909 3910    case DELEGATING_CTORS:3911      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)3912        DelegatingCtorDecls.push_back(ReadDeclID(F, Record, I));3913      break;3914 3915    case WEAK_UNDECLARED_IDENTIFIERS:3916      if (Record.size() % 3 != 0)3917        return llvm::createStringError(std::errc::illegal_byte_sequence,3918                                       "invalid weak identifiers record");3919 3920      // FIXME: Ignore weak undeclared identifiers from non-original PCH3921      // files. This isn't the way to do it :)3922      WeakUndeclaredIdentifiers.clear();3923 3924      // Translate the weak, undeclared identifiers into global IDs.3925      for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {3926        WeakUndeclaredIdentifiers.push_back(3927          getGlobalIdentifierID(F, Record[I++]));3928        WeakUndeclaredIdentifiers.push_back(3929          getGlobalIdentifierID(F, Record[I++]));3930        WeakUndeclaredIdentifiers.push_back(3931            ReadSourceLocation(F, Record, I).getRawEncoding());3932      }3933      break;3934 3935    case SELECTOR_OFFSETS: {3936      F.SelectorOffsets = (const uint32_t *)Blob.data();3937      F.LocalNumSelectors = Record[0];3938      unsigned LocalBaseSelectorID = Record[1];3939      F.BaseSelectorID = getTotalNumSelectors();3940 3941      if (F.LocalNumSelectors > 0) {3942        // Introduce the global -> local mapping for selectors within this3943        // module.3944        GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));3945 3946        // Introduce the local -> global mapping for selectors within this3947        // module.3948        F.SelectorRemap.insertOrReplace(3949          std::make_pair(LocalBaseSelectorID,3950                         F.BaseSelectorID - LocalBaseSelectorID));3951 3952        SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);3953      }3954      break;3955    }3956 3957    case METHOD_POOL:3958      F.SelectorLookupTableData = (const unsigned char *)Blob.data();3959      if (Record[0])3960        F.SelectorLookupTable3961          = ASTSelectorLookupTable::Create(3962                        F.SelectorLookupTableData + Record[0],3963                        F.SelectorLookupTableData,3964                        ASTSelectorLookupTrait(*this, F));3965      TotalNumMethodPoolEntries += Record[1];3966      break;3967 3968    case REFERENCED_SELECTOR_POOL:3969      if (!Record.empty()) {3970        for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {3971          ReferencedSelectorsData.push_back(getGlobalSelectorID(F,3972                                                                Record[Idx++]));3973          ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).3974                                              getRawEncoding());3975        }3976      }3977      break;3978 3979    case PP_ASSUME_NONNULL_LOC: {3980      unsigned Idx = 0;3981      if (!Record.empty())3982        PP.setPreambleRecordedPragmaAssumeNonNullLoc(3983            ReadSourceLocation(F, Record, Idx));3984      break;3985    }3986 3987    case PP_UNSAFE_BUFFER_USAGE: {3988      if (!Record.empty()) {3989        SmallVector<SourceLocation, 64> SrcLocs;3990        unsigned Idx = 0;3991        while (Idx < Record.size())3992          SrcLocs.push_back(ReadSourceLocation(F, Record, Idx));3993        PP.setDeserializedSafeBufferOptOutMap(SrcLocs);3994      }3995      break;3996    }3997 3998    case PP_CONDITIONAL_STACK:3999      if (!Record.empty()) {4000        unsigned Idx = 0, End = Record.size() - 1;4001        bool ReachedEOFWhileSkipping = Record[Idx++];4002        std::optional<Preprocessor::PreambleSkipInfo> SkipInfo;4003        if (ReachedEOFWhileSkipping) {4004          SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);4005          SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);4006          bool FoundNonSkipPortion = Record[Idx++];4007          bool FoundElse = Record[Idx++];4008          SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);4009          SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,4010                           FoundElse, ElseLoc);4011        }4012        SmallVector<PPConditionalInfo, 4> ConditionalStack;4013        while (Idx < End) {4014          auto Loc = ReadSourceLocation(F, Record, Idx);4015          bool WasSkipping = Record[Idx++];4016          bool FoundNonSkip = Record[Idx++];4017          bool FoundElse = Record[Idx++];4018          ConditionalStack.push_back(4019              {Loc, WasSkipping, FoundNonSkip, FoundElse});4020        }4021        PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);4022      }4023      break;4024 4025    case PP_COUNTER_VALUE:4026      if (!Record.empty() && Listener)4027        Listener->ReadCounter(F, Record[0]);4028      break;4029 4030    case FILE_SORTED_DECLS:4031      F.FileSortedDecls = (const unaligned_decl_id_t *)Blob.data();4032      F.NumFileSortedDecls = Record[0];4033      break;4034 4035    case SOURCE_LOCATION_OFFSETS: {4036      F.SLocEntryOffsets = (const uint32_t *)Blob.data();4037      F.LocalNumSLocEntries = Record[0];4038      SourceLocation::UIntTy SLocSpaceSize = Record[1];4039      F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;4040      std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =4041          SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,4042                                              SLocSpaceSize);4043      if (!F.SLocEntryBaseID) {4044        Diags.Report(SourceLocation(), diag::remark_sloc_usage);4045        SourceMgr.noteSLocAddressSpaceUsage(Diags);4046        return llvm::createStringError(std::errc::invalid_argument,4047                                       "ran out of source locations");4048      }4049      // Make our entry in the range map. BaseID is negative and growing, so4050      // we invert it. Because we invert it, though, we need the other end of4051      // the range.4052      unsigned RangeStart =4053          unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;4054      GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));4055      F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);4056 4057      // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.4058      assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);4059      GlobalSLocOffsetMap.insert(4060          std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset4061                           - SLocSpaceSize,&F));4062 4063      TotalNumSLocEntries += F.LocalNumSLocEntries;4064      break;4065    }4066 4067    case MODULE_OFFSET_MAP:4068      F.ModuleOffsetMap = Blob;4069      break;4070 4071    case SOURCE_MANAGER_LINE_TABLE:4072      ParseLineTable(F, Record);4073      break;4074 4075    case EXT_VECTOR_DECLS:4076      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)4077        ExtVectorDecls.push_back(ReadDeclID(F, Record, I));4078      break;4079 4080    case VTABLE_USES:4081      if (Record.size() % 3 != 0)4082        return llvm::createStringError(std::errc::illegal_byte_sequence,4083                                       "Invalid VTABLE_USES record");4084 4085      // Later tables overwrite earlier ones.4086      // FIXME: Modules will have some trouble with this. This is clearly not4087      // the right way to do this.4088      VTableUses.clear();4089 4090      for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {4091        VTableUses.push_back(4092            {ReadDeclID(F, Record, Idx),4093             ReadSourceLocation(F, Record, Idx).getRawEncoding(),4094             (bool)Record[Idx++]});4095      }4096      break;4097 4098    case PENDING_IMPLICIT_INSTANTIATIONS:4099 4100      if (Record.size() % 2 != 0)4101        return llvm::createStringError(4102            std::errc::illegal_byte_sequence,4103            "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");4104 4105      // For standard C++20 module, we will only reads the instantiations4106      // if it is the main file.4107      if (!F.StandardCXXModule || F.Kind == MK_MainFile) {4108        for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {4109          PendingInstantiations.push_back(4110              {ReadDeclID(F, Record, I),4111               ReadSourceLocation(F, Record, I).getRawEncoding()});4112        }4113      }4114      break;4115 4116    case SEMA_DECL_REFS:4117      if (Record.size() != 3)4118        return llvm::createStringError(std::errc::illegal_byte_sequence,4119                                       "Invalid SEMA_DECL_REFS block");4120      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)4121        SemaDeclRefs.push_back(ReadDeclID(F, Record, I));4122      break;4123 4124    case PPD_ENTITIES_OFFSETS: {4125      F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();4126      assert(Blob.size() % sizeof(PPEntityOffset) == 0);4127      F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);4128 4129      unsigned StartingID;4130      if (!PP.getPreprocessingRecord())4131        PP.createPreprocessingRecord();4132      if (!PP.getPreprocessingRecord()->getExternalSource())4133        PP.getPreprocessingRecord()->SetExternalSource(*this);4134      StartingID4135        = PP.getPreprocessingRecord()4136            ->allocateLoadedEntities(F.NumPreprocessedEntities);4137      F.BasePreprocessedEntityID = StartingID;4138 4139      if (F.NumPreprocessedEntities > 0) {4140        // Introduce the global -> local mapping for preprocessed entities in4141        // this module.4142        GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));4143      }4144 4145      break;4146    }4147 4148    case PPD_SKIPPED_RANGES: {4149      F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();4150      assert(Blob.size() % sizeof(PPSkippedRange) == 0);4151      F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);4152 4153      if (!PP.getPreprocessingRecord())4154        PP.createPreprocessingRecord();4155      if (!PP.getPreprocessingRecord()->getExternalSource())4156        PP.getPreprocessingRecord()->SetExternalSource(*this);4157      F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()4158          ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);4159 4160      if (F.NumPreprocessedSkippedRanges > 0)4161        GlobalSkippedRangeMap.insert(4162            std::make_pair(F.BasePreprocessedSkippedRangeID, &F));4163      break;4164    }4165 4166    case DECL_UPDATE_OFFSETS:4167      if (Record.size() % 2 != 0)4168        return llvm::createStringError(4169            std::errc::illegal_byte_sequence,4170            "invalid DECL_UPDATE_OFFSETS block in AST file");4171      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {4172        GlobalDeclID ID = ReadDeclID(F, Record, I);4173        DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I++]));4174 4175        // If we've already loaded the decl, perform the updates when we finish4176        // loading this block.4177        if (Decl *D = GetExistingDecl(ID))4178          PendingUpdateRecords.push_back(4179              PendingUpdateRecord(ID, D, /*JustLoaded=*/false));4180      }4181      break;4182 4183    case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {4184      if (Record.size() % 5 != 0)4185        return llvm::createStringError(4186            std::errc::illegal_byte_sequence,4187            "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "4188            "file");4189      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {4190        GlobalDeclID ID = ReadDeclID(F, Record, I);4191 4192        uint64_t BaseOffset = F.DeclsBlockStartOffset;4193        assert(BaseOffset && "Invalid DeclsBlockStartOffset for module file!");4194        uint64_t LocalLexicalOffset = Record[I++];4195        uint64_t LexicalOffset =4196            LocalLexicalOffset ? BaseOffset + LocalLexicalOffset : 0;4197        uint64_t LocalVisibleOffset = Record[I++];4198        uint64_t VisibleOffset =4199            LocalVisibleOffset ? BaseOffset + LocalVisibleOffset : 0;4200        uint64_t LocalModuleLocalOffset = Record[I++];4201        uint64_t ModuleLocalOffset =4202            LocalModuleLocalOffset ? BaseOffset + LocalModuleLocalOffset : 0;4203        uint64_t TULocalLocalOffset = Record[I++];4204        uint64_t TULocalOffset =4205            TULocalLocalOffset ? BaseOffset + TULocalLocalOffset : 0;4206 4207        DelayedNamespaceOffsetMap[ID] = {4208            {VisibleOffset, TULocalOffset, ModuleLocalOffset}, LexicalOffset};4209 4210        assert(!GetExistingDecl(ID) &&4211               "We shouldn't load the namespace in the front of delayed "4212               "namespace lexical and visible block");4213      }4214      break;4215    }4216 4217    case RELATED_DECLS_MAP:4218      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) {4219        GlobalDeclID ID = ReadDeclID(F, Record, I);4220        auto &RelatedDecls = RelatedDeclsMap[ID];4221        unsigned NN = Record[I++];4222        RelatedDecls.reserve(NN);4223        for (unsigned II = 0; II < NN; II++)4224          RelatedDecls.push_back(ReadDeclID(F, Record, I));4225      }4226      break;4227 4228    case OBJC_CATEGORIES_MAP:4229      if (F.LocalNumObjCCategoriesInMap != 0)4230        return llvm::createStringError(4231            std::errc::illegal_byte_sequence,4232            "duplicate OBJC_CATEGORIES_MAP record in AST file");4233 4234      F.LocalNumObjCCategoriesInMap = Record[0];4235      F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();4236      break;4237 4238    case OBJC_CATEGORIES:4239      F.ObjCCategories.swap(Record);4240      break;4241 4242    case CUDA_SPECIAL_DECL_REFS:4243      // Later tables overwrite earlier ones.4244      // FIXME: Modules will have trouble with this.4245      CUDASpecialDeclRefs.clear();4246      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)4247        CUDASpecialDeclRefs.push_back(ReadDeclID(F, Record, I));4248      break;4249 4250    case HEADER_SEARCH_TABLE:4251      F.HeaderFileInfoTableData = Blob.data();4252      F.LocalNumHeaderFileInfos = Record[1];4253      if (Record[0]) {4254        F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create(4255            (const unsigned char *)F.HeaderFileInfoTableData + Record[0],4256            (const unsigned char *)F.HeaderFileInfoTableData,4257            HeaderFileInfoTrait(*this, F));4258 4259        PP.getHeaderSearchInfo().SetExternalSource(this);4260        if (!PP.getHeaderSearchInfo().getExternalLookup())4261          PP.getHeaderSearchInfo().SetExternalLookup(this);4262      }4263      break;4264 4265    case FP_PRAGMA_OPTIONS:4266      // Later tables overwrite earlier ones.4267      FPPragmaOptions.swap(Record);4268      break;4269 4270    case DECLS_WITH_EFFECTS_TO_VERIFY:4271      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)4272        DeclsWithEffectsToVerify.push_back(ReadDeclID(F, Record, I));4273      break;4274 4275    case OPENCL_EXTENSIONS:4276      for (unsigned I = 0, E = Record.size(); I != E; ) {4277        auto Name = ReadString(Record, I);4278        auto &OptInfo = OpenCLExtensions.OptMap[Name];4279        OptInfo.Supported = Record[I++] != 0;4280        OptInfo.Enabled = Record[I++] != 0;4281        OptInfo.WithPragma = Record[I++] != 0;4282        OptInfo.Avail = Record[I++];4283        OptInfo.Core = Record[I++];4284        OptInfo.Opt = Record[I++];4285      }4286      break;4287 4288    case TENTATIVE_DEFINITIONS:4289      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)4290        TentativeDefinitions.push_back(ReadDeclID(F, Record, I));4291      break;4292 4293    case KNOWN_NAMESPACES:4294      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)4295        KnownNamespaces.push_back(ReadDeclID(F, Record, I));4296      break;4297 4298    case UNDEFINED_BUT_USED:4299      if (Record.size() % 2 != 0)4300        return llvm::createStringError(std::errc::illegal_byte_sequence,4301                                       "invalid undefined-but-used record");4302      for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {4303        UndefinedButUsed.push_back(4304            {ReadDeclID(F, Record, I),4305             ReadSourceLocation(F, Record, I).getRawEncoding()});4306      }4307      break;4308 4309    case DELETE_EXPRS_TO_ANALYZE:4310      for (unsigned I = 0, N = Record.size(); I != N;) {4311        DelayedDeleteExprs.push_back(ReadDeclID(F, Record, I).getRawValue());4312        const uint64_t Count = Record[I++];4313        DelayedDeleteExprs.push_back(Count);4314        for (uint64_t C = 0; C < Count; ++C) {4315          DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());4316          bool IsArrayForm = Record[I++] == 1;4317          DelayedDeleteExprs.push_back(IsArrayForm);4318        }4319      }4320      break;4321 4322    case VTABLES_TO_EMIT:4323      if (F.Kind == MK_MainFile ||4324          getContext().getLangOpts().BuildingPCHWithObjectFile)4325        for (unsigned I = 0, N = Record.size(); I != N;)4326          VTablesToEmit.push_back(ReadDeclID(F, Record, I));4327      break;4328 4329    case IMPORTED_MODULES:4330      if (!F.isModule()) {4331        // If we aren't loading a module (which has its own exports), make4332        // all of the imported modules visible.4333        // FIXME: Deal with macros-only imports.4334        for (unsigned I = 0, N = Record.size(); I != N; /**/) {4335          unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);4336          SourceLocation Loc = ReadSourceLocation(F, Record, I);4337          if (GlobalID) {4338            PendingImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));4339            if (DeserializationListener)4340              DeserializationListener->ModuleImportRead(GlobalID, Loc);4341          }4342        }4343      }4344      break;4345 4346    case MACRO_OFFSET: {4347      if (F.LocalNumMacros != 0)4348        return llvm::createStringError(4349            std::errc::illegal_byte_sequence,4350            "duplicate MACRO_OFFSET record in AST file");4351      F.MacroOffsets = (const uint32_t *)Blob.data();4352      F.LocalNumMacros = Record[0];4353      F.MacroOffsetsBase = Record[1] + F.ASTBlockStartOffset;4354      F.BaseMacroID = getTotalNumMacros();4355 4356      if (F.LocalNumMacros > 0)4357        MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);4358      break;4359    }4360 4361    case LATE_PARSED_TEMPLATE:4362      LateParsedTemplates.emplace_back(4363          std::piecewise_construct, std::forward_as_tuple(&F),4364          std::forward_as_tuple(Record.begin(), Record.end()));4365      break;4366 4367    case OPTIMIZE_PRAGMA_OPTIONS:4368      if (Record.size() != 1)4369        return llvm::createStringError(std::errc::illegal_byte_sequence,4370                                       "invalid pragma optimize record");4371      OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);4372      break;4373 4374    case MSSTRUCT_PRAGMA_OPTIONS:4375      if (Record.size() != 1)4376        return llvm::createStringError(std::errc::illegal_byte_sequence,4377                                       "invalid pragma ms_struct record");4378      PragmaMSStructState = Record[0];4379      break;4380 4381    case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:4382      if (Record.size() != 2)4383        return llvm::createStringError(4384            std::errc::illegal_byte_sequence,4385            "invalid pragma pointers to members record");4386      PragmaMSPointersToMembersState = Record[0];4387      PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);4388      break;4389 4390    case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:4391      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)4392        UnusedLocalTypedefNameCandidates.push_back(ReadDeclID(F, Record, I));4393      break;4394 4395    case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:4396      if (Record.size() != 1)4397        return llvm::createStringError(std::errc::illegal_byte_sequence,4398                                       "invalid cuda pragma options record");4399      ForceHostDeviceDepth = Record[0];4400      break;4401 4402    case ALIGN_PACK_PRAGMA_OPTIONS: {4403      if (Record.size() < 3)4404        return llvm::createStringError(std::errc::illegal_byte_sequence,4405                                       "invalid pragma pack record");4406      PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);4407      PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);4408      unsigned NumStackEntries = Record[2];4409      unsigned Idx = 3;4410      // Reset the stack when importing a new module.4411      PragmaAlignPackStack.clear();4412      for (unsigned I = 0; I < NumStackEntries; ++I) {4413        PragmaAlignPackStackEntry Entry;4414        Entry.Value = ReadAlignPackInfo(Record[Idx++]);4415        Entry.Location = ReadSourceLocation(F, Record[Idx++]);4416        Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);4417        PragmaAlignPackStrings.push_back(ReadString(Record, Idx));4418        Entry.SlotLabel = PragmaAlignPackStrings.back();4419        PragmaAlignPackStack.push_back(Entry);4420      }4421      break;4422    }4423 4424    case FLOAT_CONTROL_PRAGMA_OPTIONS: {4425      if (Record.size() < 3)4426        return llvm::createStringError(std::errc::illegal_byte_sequence,4427                                       "invalid pragma float control record");4428      FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);4429      FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);4430      unsigned NumStackEntries = Record[2];4431      unsigned Idx = 3;4432      // Reset the stack when importing a new module.4433      FpPragmaStack.clear();4434      for (unsigned I = 0; I < NumStackEntries; ++I) {4435        FpPragmaStackEntry Entry;4436        Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);4437        Entry.Location = ReadSourceLocation(F, Record[Idx++]);4438        Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);4439        FpPragmaStrings.push_back(ReadString(Record, Idx));4440        Entry.SlotLabel = FpPragmaStrings.back();4441        FpPragmaStack.push_back(Entry);4442      }4443      break;4444    }4445 4446    case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:4447      for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)4448        DeclsToCheckForDeferredDiags.insert(ReadDeclID(F, Record, I));4449      break;4450    }4451  }4452}4453 4454void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {4455  assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");4456 4457  // Additional remapping information.4458  const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();4459  const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();4460  F.ModuleOffsetMap = StringRef();4461 4462  using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;4463  RemapBuilder SubmoduleRemap(F.SubmoduleRemap);4464  RemapBuilder SelectorRemap(F.SelectorRemap);4465 4466  auto &ImportedModuleVector = F.TransitiveImports;4467  assert(ImportedModuleVector.empty());4468 4469  while (Data < DataEnd) {4470    // FIXME: Looking up dependency modules by filename is horrible. Let's4471    // start fixing this with prebuilt, explicit and implicit modules and see4472    // how it goes...4473    using namespace llvm::support;4474    ModuleKind Kind = static_cast<ModuleKind>(4475        endian::readNext<uint8_t, llvm::endianness::little>(Data));4476    uint16_t Len = endian::readNext<uint16_t, llvm::endianness::little>(Data);4477    StringRef Name = StringRef((const char*)Data, Len);4478    Data += Len;4479    ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||4480                              Kind == MK_ImplicitModule4481                          ? ModuleMgr.lookupByModuleName(Name)4482                          : ModuleMgr.lookupByFileName(Name));4483    if (!OM) {4484      std::string Msg = "refers to unknown module, cannot find ";4485      Msg.append(std::string(Name));4486      Error(Msg);4487      return;4488    }4489 4490    ImportedModuleVector.push_back(OM);4491 4492    uint32_t SubmoduleIDOffset =4493        endian::readNext<uint32_t, llvm::endianness::little>(Data);4494    uint32_t SelectorIDOffset =4495        endian::readNext<uint32_t, llvm::endianness::little>(Data);4496 4497    auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,4498                         RemapBuilder &Remap) {4499      constexpr uint32_t None = std::numeric_limits<uint32_t>::max();4500      if (Offset != None)4501        Remap.insert(std::make_pair(Offset,4502                                    static_cast<int>(BaseOffset - Offset)));4503    };4504 4505    mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);4506    mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);4507  }4508}4509 4510ASTReader::ASTReadResult4511ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,4512                                  const ModuleFile *ImportedBy,4513                                  unsigned ClientLoadCapabilities) {4514  unsigned Idx = 0;4515  F.ModuleMapPath = ReadPath(F, Record, Idx);4516 4517  // Try to resolve ModuleName in the current header search context and4518  // verify that it is found in the same module map file as we saved. If the4519  // top-level AST file is a main file, skip this check because there is no4520  // usable header search context.4521  assert(!F.ModuleName.empty() &&4522         "MODULE_NAME should come before MODULE_MAP_FILE");4523  if (PP.getPreprocessorOpts().ModulesCheckRelocated &&4524      F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {4525    // An implicitly-loaded module file should have its module listed in some4526    // module map file that we've already loaded.4527    Module *M =4528        PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);4529    auto &Map = PP.getHeaderSearchInfo().getModuleMap();4530    OptionalFileEntryRef ModMap =4531        M ? Map.getModuleMapFileForUniquing(M) : std::nullopt;4532    // Don't emit module relocation error if we have -fno-validate-pch4533    if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &4534              DisableValidationForModuleKind::Module) &&4535        !ModMap) {4536      if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {4537        if (auto ASTFE = M ? M->getASTFile() : std::nullopt) {4538          // This module was defined by an imported (explicit) module.4539          Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName4540                                               << ASTFE->getName();4541          // TODO: Add a note with the module map paths if they differ.4542        } else {4543          // This module was built with a different module map.4544          Diag(diag::err_imported_module_not_found)4545              << F.ModuleName << F.FileName4546              << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath4547              << !ImportedBy;4548          // In case it was imported by a PCH, there's a chance the user is4549          // just missing to include the search path to the directory containing4550          // the modulemap.4551          if (ImportedBy && ImportedBy->Kind == MK_PCH)4552            Diag(diag::note_imported_by_pch_module_not_found)4553                << llvm::sys::path::parent_path(F.ModuleMapPath);4554        }4555      }4556      return OutOfDate;4557    }4558 4559    assert(M && M->Name == F.ModuleName && "found module with different name");4560 4561    // Check the primary module map file.4562    auto StoredModMap = FileMgr.getOptionalFileRef(F.ModuleMapPath);4563    if (!StoredModMap || *StoredModMap != ModMap) {4564      assert(ModMap && "found module is missing module map file");4565      assert((ImportedBy || F.Kind == MK_ImplicitModule) &&4566             "top-level import should be verified");4567      bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;4568      if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))4569        Diag(diag::err_imported_module_modmap_changed)4570            << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)4571            << ModMap->getName() << F.ModuleMapPath << NotImported;4572      return OutOfDate;4573    }4574 4575    ModuleMap::AdditionalModMapsSet AdditionalStoredMaps;4576    for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {4577      // FIXME: we should use input files rather than storing names.4578      std::string Filename = ReadPath(F, Record, Idx);4579      auto SF = FileMgr.getOptionalFileRef(Filename, false, false);4580      if (!SF) {4581        if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))4582          Error("could not find file '" + Filename +"' referenced by AST file");4583        return OutOfDate;4584      }4585      AdditionalStoredMaps.insert(*SF);4586    }4587 4588    // Check any additional module map files (e.g. module.private.modulemap)4589    // that are not in the pcm.4590    if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {4591      for (FileEntryRef ModMap : *AdditionalModuleMaps) {4592        // Remove files that match4593        // Note: SmallPtrSet::erase is really remove4594        if (!AdditionalStoredMaps.erase(ModMap)) {4595          if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))4596            Diag(diag::err_module_different_modmap)4597              << F.ModuleName << /*new*/0 << ModMap.getName();4598          return OutOfDate;4599        }4600      }4601    }4602 4603    // Check any additional module map files that are in the pcm, but not4604    // found in header search. Cases that match are already removed.4605    for (FileEntryRef ModMap : AdditionalStoredMaps) {4606      if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))4607        Diag(diag::err_module_different_modmap)4608          << F.ModuleName << /*not new*/1 << ModMap.getName();4609      return OutOfDate;4610    }4611  }4612 4613  if (Listener)4614    Listener->ReadModuleMapFile(F.ModuleMapPath);4615  return Success;4616}4617 4618/// Move the given method to the back of the global list of methods.4619static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {4620  // Find the entry for this selector in the method pool.4621  SemaObjC::GlobalMethodPool::iterator Known =4622      S.ObjC().MethodPool.find(Method->getSelector());4623  if (Known == S.ObjC().MethodPool.end())4624    return;4625 4626  // Retrieve the appropriate method list.4627  ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first4628                                                    : Known->second.second;4629  bool Found = false;4630  for (ObjCMethodList *List = &Start; List; List = List->getNext()) {4631    if (!Found) {4632      if (List->getMethod() == Method) {4633        Found = true;4634      } else {4635        // Keep searching.4636        continue;4637      }4638    }4639 4640    if (List->getNext())4641      List->setMethod(List->getNext()->getMethod());4642    else4643      List->setMethod(Method);4644  }4645}4646 4647void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {4648  assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");4649  for (Decl *D : Names) {4650    bool wasHidden = !D->isUnconditionallyVisible();4651    D->setVisibleDespiteOwningModule();4652 4653    if (wasHidden && SemaObj) {4654      if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {4655        moveMethodToBackOfGlobalList(*SemaObj, Method);4656      }4657    }4658  }4659}4660 4661void ASTReader::makeModuleVisible(Module *Mod,4662                                  Module::NameVisibilityKind NameVisibility,4663                                  SourceLocation ImportLoc) {4664  llvm::SmallPtrSet<Module *, 4> Visited;4665  SmallVector<Module *, 4> Stack;4666  Stack.push_back(Mod);4667  while (!Stack.empty()) {4668    Mod = Stack.pop_back_val();4669 4670    if (NameVisibility <= Mod->NameVisibility) {4671      // This module already has this level of visibility (or greater), so4672      // there is nothing more to do.4673      continue;4674    }4675 4676    if (Mod->isUnimportable()) {4677      // Modules that aren't importable cannot be made visible.4678      continue;4679    }4680 4681    // Update the module's name visibility.4682    Mod->NameVisibility = NameVisibility;4683 4684    // If we've already deserialized any names from this module,4685    // mark them as visible.4686    HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);4687    if (Hidden != HiddenNamesMap.end()) {4688      auto HiddenNames = std::move(*Hidden);4689      HiddenNamesMap.erase(Hidden);4690      makeNamesVisible(HiddenNames.second, HiddenNames.first);4691      assert(!HiddenNamesMap.contains(Mod) &&4692             "making names visible added hidden names");4693    }4694 4695    // Push any exported modules onto the stack to be marked as visible.4696    SmallVector<Module *, 16> Exports;4697    Mod->getExportedModules(Exports);4698    for (SmallVectorImpl<Module *>::iterator4699           I = Exports.begin(), E = Exports.end(); I != E; ++I) {4700      Module *Exported = *I;4701      if (Visited.insert(Exported).second)4702        Stack.push_back(Exported);4703    }4704  }4705}4706 4707/// We've merged the definition \p MergedDef into the existing definition4708/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made4709/// visible.4710void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,4711                                          NamedDecl *MergedDef) {4712  if (!Def->isUnconditionallyVisible()) {4713    // If MergedDef is visible or becomes visible, make the definition visible.4714    if (MergedDef->isUnconditionallyVisible())4715      Def->setVisibleDespiteOwningModule();4716    else {4717      getContext().mergeDefinitionIntoModule(4718          Def, MergedDef->getImportedOwningModule(),4719          /*NotifyListeners*/ false);4720      PendingMergedDefinitionsToDeduplicate.insert(Def);4721    }4722  }4723}4724 4725bool ASTReader::loadGlobalIndex() {4726  if (GlobalIndex)4727    return false;4728 4729  if (TriedLoadingGlobalIndex || !UseGlobalIndex ||4730      !PP.getLangOpts().Modules)4731    return true;4732 4733  // Try to load the global index.4734  TriedLoadingGlobalIndex = true;4735  StringRef ModuleCachePath4736    = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();4737  std::pair<GlobalModuleIndex *, llvm::Error> Result =4738      GlobalModuleIndex::readIndex(ModuleCachePath);4739  if (llvm::Error Err = std::move(Result.second)) {4740    assert(!Result.first);4741    consumeError(std::move(Err)); // FIXME this drops errors on the floor.4742    return true;4743  }4744 4745  GlobalIndex.reset(Result.first);4746  ModuleMgr.setGlobalIndex(GlobalIndex.get());4747  return false;4748}4749 4750bool ASTReader::isGlobalIndexUnavailable() const {4751  return PP.getLangOpts().Modules && UseGlobalIndex &&4752         !hasGlobalIndex() && TriedLoadingGlobalIndex;4753}4754 4755/// Given a cursor at the start of an AST file, scan ahead and drop the4756/// cursor into the start of the given block ID, returning false on success and4757/// true on failure.4758static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {4759  while (true) {4760    Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();4761    if (!MaybeEntry) {4762      // FIXME this drops errors on the floor.4763      consumeError(MaybeEntry.takeError());4764      return true;4765    }4766    llvm::BitstreamEntry Entry = MaybeEntry.get();4767 4768    switch (Entry.Kind) {4769    case llvm::BitstreamEntry::Error:4770    case llvm::BitstreamEntry::EndBlock:4771      return true;4772 4773    case llvm::BitstreamEntry::Record:4774      // Ignore top-level records.4775      if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))4776        break;4777      else {4778        // FIXME this drops errors on the floor.4779        consumeError(Skipped.takeError());4780        return true;4781      }4782 4783    case llvm::BitstreamEntry::SubBlock:4784      if (Entry.ID == BlockID) {4785        if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {4786          // FIXME this drops the error on the floor.4787          consumeError(std::move(Err));4788          return true;4789        }4790        // Found it!4791        return false;4792      }4793 4794      if (llvm::Error Err = Cursor.SkipBlock()) {4795        // FIXME this drops the error on the floor.4796        consumeError(std::move(Err));4797        return true;4798      }4799    }4800  }4801}4802 4803ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,4804                                            SourceLocation ImportLoc,4805                                            unsigned ClientLoadCapabilities,4806                                            ModuleFile **NewLoadedModuleFile) {4807  llvm::TimeTraceScope scope("ReadAST", FileName);4808 4809  llvm::SaveAndRestore SetCurImportLocRAII(CurrentImportLoc, ImportLoc);4810  llvm::SaveAndRestore<std::optional<ModuleKind>> SetCurModuleKindRAII(4811      CurrentDeserializingModuleKind, Type);4812 4813  // Defer any pending actions until we get to the end of reading the AST file.4814  Deserializing AnASTFile(this);4815 4816  // Bump the generation number.4817  unsigned PreviousGeneration = 0;4818  if (ContextObj)4819    PreviousGeneration = incrementGeneration(*ContextObj);4820 4821  unsigned NumModules = ModuleMgr.size();4822  SmallVector<ImportedModule, 4> Loaded;4823  if (ASTReadResult ReadResult =4824          ReadASTCore(FileName, Type, ImportLoc,4825                      /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),4826                      ClientLoadCapabilities)) {4827    ModuleMgr.removeModules(ModuleMgr.begin() + NumModules);4828 4829    // If we find that any modules are unusable, the global index is going4830    // to be out-of-date. Just remove it.4831    GlobalIndex.reset();4832    ModuleMgr.setGlobalIndex(nullptr);4833    return ReadResult;4834  }4835 4836  if (NewLoadedModuleFile && !Loaded.empty())4837    *NewLoadedModuleFile = Loaded.back().Mod;4838 4839  // Here comes stuff that we only do once the entire chain is loaded. Do *not*4840  // remove modules from this point. Various fields are updated during reading4841  // the AST block and removing the modules would result in dangling pointers.4842  // They are generally only incidentally dereferenced, ie. a binary search4843  // runs over `GlobalSLocEntryMap`, which could cause an invalid module to4844  // be dereferenced but it wouldn't actually be used.4845 4846  // Load the AST blocks of all of the modules that we loaded. We can still4847  // hit errors parsing the ASTs at this point.4848  for (ImportedModule &M : Loaded) {4849    ModuleFile &F = *M.Mod;4850    llvm::TimeTraceScope Scope2("Read Loaded AST", F.ModuleName);4851 4852    // Read the AST block.4853    if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {4854      Error(std::move(Err));4855      return Failure;4856    }4857 4858    // The AST block should always have a definition for the main module.4859    if (F.isModule() && !F.DidReadTopLevelSubmodule) {4860      Error(diag::err_module_file_missing_top_level_submodule, F.FileName);4861      return Failure;4862    }4863 4864    // Read the extension blocks.4865    while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {4866      if (llvm::Error Err = ReadExtensionBlock(F)) {4867        Error(std::move(Err));4868        return Failure;4869      }4870    }4871 4872    // Once read, set the ModuleFile bit base offset and update the size in4873    // bits of all files we've seen.4874    F.GlobalBitOffset = TotalModulesSizeInBits;4875    TotalModulesSizeInBits += F.SizeInBits;4876    GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));4877  }4878 4879  // Preload source locations and interesting indentifiers.4880  for (ImportedModule &M : Loaded) {4881    ModuleFile &F = *M.Mod;4882 4883    // Map the original source file ID into the ID space of the current4884    // compilation.4885    if (F.OriginalSourceFileID.isValid())4886      F.OriginalSourceFileID = TranslateFileID(F, F.OriginalSourceFileID);4887 4888    for (auto Offset : F.PreloadIdentifierOffsets) {4889      const unsigned char *Data = F.IdentifierTableData + Offset;4890 4891      ASTIdentifierLookupTrait Trait(*this, F);4892      auto KeyDataLen = Trait.ReadKeyDataLength(Data);4893      auto Key = Trait.ReadKey(Data, KeyDataLen.first);4894 4895      IdentifierInfo *II;4896      if (!PP.getLangOpts().CPlusPlus) {4897        // Identifiers present in both the module file and the importing4898        // instance are marked out-of-date so that they can be deserialized4899        // on next use via ASTReader::updateOutOfDateIdentifier().4900        // Identifiers present in the module file but not in the importing4901        // instance are ignored for now, preventing growth of the identifier4902        // table. They will be deserialized on first use via ASTReader::get().4903        auto It = PP.getIdentifierTable().find(Key);4904        if (It == PP.getIdentifierTable().end())4905          continue;4906        II = It->second;4907      } else {4908        // With C++ modules, not many identifiers are considered interesting.4909        // All identifiers in the module file can be placed into the identifier4910        // table of the importing instance and marked as out-of-date. This makes4911        // ASTReader::get() a no-op, and deserialization will take place on4912        // first/next use via ASTReader::updateOutOfDateIdentifier().4913        II = &PP.getIdentifierTable().getOwn(Key);4914      }4915 4916      II->setOutOfDate(true);4917 4918      // Mark this identifier as being from an AST file so that we can track4919      // whether we need to serialize it.4920      markIdentifierFromAST(*this, *II, /*IsModule=*/true);4921 4922      // Associate the ID with the identifier so that the writer can reuse it.4923      auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);4924      SetIdentifierInfo(ID, II);4925    }4926  }4927 4928  // Builtins and library builtins have already been initialized. Mark all4929  // identifiers as out-of-date, so that they are deserialized on first use.4930  if (Type == MK_PCH || Type == MK_Preamble || Type == MK_MainFile)4931    for (auto &Id : PP.getIdentifierTable())4932      Id.second->setOutOfDate(true);4933 4934  // Mark selectors as out of date.4935  for (const auto &Sel : SelectorGeneration)4936    SelectorOutOfDate[Sel.first] = true;4937 4938  // Setup the import locations and notify the module manager that we've4939  // committed to these module files.4940  for (ImportedModule &M : Loaded) {4941    ModuleFile &F = *M.Mod;4942 4943    ModuleMgr.moduleFileAccepted(&F);4944 4945    // Set the import location.4946    F.DirectImportLoc = ImportLoc;4947    // FIXME: We assume that locations from PCH / preamble do not need4948    // any translation.4949    if (!M.ImportedBy)4950      F.ImportLoc = M.ImportLoc;4951    else4952      F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);4953  }4954 4955  // Resolve any unresolved module exports.4956  for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {4957    UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];4958    SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);4959    Module *ResolvedMod = getSubmodule(GlobalID);4960 4961    switch (Unresolved.Kind) {4962    case UnresolvedModuleRef::Conflict:4963      if (ResolvedMod) {4964        Module::Conflict Conflict;4965        Conflict.Other = ResolvedMod;4966        Conflict.Message = Unresolved.String.str();4967        Unresolved.Mod->Conflicts.push_back(Conflict);4968      }4969      continue;4970 4971    case UnresolvedModuleRef::Import:4972      if (ResolvedMod)4973        Unresolved.Mod->Imports.insert(ResolvedMod);4974      continue;4975 4976    case UnresolvedModuleRef::Affecting:4977      if (ResolvedMod)4978        Unresolved.Mod->AffectingClangModules.insert(ResolvedMod);4979      continue;4980 4981    case UnresolvedModuleRef::Export:4982      if (ResolvedMod || Unresolved.IsWildcard)4983        Unresolved.Mod->Exports.push_back(4984          Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));4985      continue;4986    }4987  }4988  UnresolvedModuleRefs.clear();4989 4990  // FIXME: How do we load the 'use'd modules? They may not be submodules.4991  // Might be unnecessary as use declarations are only used to build the4992  // module itself.4993 4994  if (ContextObj)4995    InitializeContext();4996 4997  if (SemaObj)4998    UpdateSema();4999 5000  if (DeserializationListener)5001    DeserializationListener->ReaderInitialized(this);5002 5003  ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();5004  if (PrimaryModule.OriginalSourceFileID.isValid()) {5005    // If this AST file is a precompiled preamble, then set the5006    // preamble file ID of the source manager to the file source file5007    // from which the preamble was built.5008    if (Type == MK_Preamble) {5009      SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);5010    } else if (Type == MK_MainFile) {5011      SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);5012    }5013  }5014 5015  // For any Objective-C class definitions we have already loaded, make sure5016  // that we load any additional categories.5017  if (ContextObj) {5018    for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {5019      loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),5020                         ObjCClassesLoaded[I], PreviousGeneration);5021    }5022  }5023 5024  const HeaderSearchOptions &HSOpts =5025      PP.getHeaderSearchInfo().getHeaderSearchOpts();5026  if (HSOpts.ModulesValidateOncePerBuildSession) {5027    // Now we are certain that the module and all modules it depends on are5028    // up-to-date. For implicitly-built module files, ensure the corresponding5029    // timestamp files are up-to-date in this build session.5030    for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {5031      ImportedModule &M = Loaded[I];5032      if (M.Mod->Kind == MK_ImplicitModule &&5033          M.Mod->InputFilesValidationTimestamp < HSOpts.BuildSessionTimestamp)5034        getModuleManager().getModuleCache().updateModuleTimestamp(5035            M.Mod->FileName);5036    }5037  }5038 5039  return Success;5040}5041 5042static ASTFileSignature readASTFileSignature(StringRef PCH);5043 5044/// Whether \p Stream doesn't start with the AST file magic number 'CPCH'.5045static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {5046  // FIXME checking magic headers is done in other places such as5047  // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't5048  // always done the same. Unify it all with a helper.5049  if (!Stream.canSkipToPos(4))5050    return llvm::createStringError(5051        std::errc::illegal_byte_sequence,5052        "file too small to contain precompiled file magic");5053  for (unsigned C : {'C', 'P', 'C', 'H'})5054    if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {5055      if (Res.get() != C)5056        return llvm::createStringError(5057            std::errc::illegal_byte_sequence,5058            "file doesn't start with precompiled file magic");5059    } else5060      return Res.takeError();5061  return llvm::Error::success();5062}5063 5064static unsigned moduleKindForDiagnostic(ModuleKind Kind) {5065  switch (Kind) {5066  case MK_PCH:5067    return 0; // PCH5068  case MK_ImplicitModule:5069  case MK_ExplicitModule:5070  case MK_PrebuiltModule:5071    return 1; // module5072  case MK_MainFile:5073  case MK_Preamble:5074    return 2; // main source file5075  }5076  llvm_unreachable("unknown module kind");5077}5078 5079ASTReader::ASTReadResult5080ASTReader::ReadASTCore(StringRef FileName,5081                       ModuleKind Type,5082                       SourceLocation ImportLoc,5083                       ModuleFile *ImportedBy,5084                       SmallVectorImpl<ImportedModule> &Loaded,5085                       off_t ExpectedSize, time_t ExpectedModTime,5086                       ASTFileSignature ExpectedSignature,5087                       unsigned ClientLoadCapabilities) {5088  ModuleFile *M;5089  std::string ErrorStr;5090  ModuleManager::AddModuleResult AddResult5091    = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,5092                          getGeneration(), ExpectedSize, ExpectedModTime,5093                          ExpectedSignature, readASTFileSignature,5094                          M, ErrorStr);5095 5096  switch (AddResult) {5097  case ModuleManager::AlreadyLoaded:5098    Diag(diag::remark_module_import)5099        << M->ModuleName << M->FileName << (ImportedBy ? true : false)5100        << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());5101    return Success;5102 5103  case ModuleManager::NewlyLoaded:5104    // Load module file below.5105    break;5106 5107  case ModuleManager::Missing:5108    // The module file was missing; if the client can handle that, return5109    // it.5110    if (ClientLoadCapabilities & ARR_Missing)5111      return Missing;5112 5113    // Otherwise, return an error.5114    Diag(diag::err_ast_file_not_found)5115        << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()5116        << ErrorStr;5117    return Failure;5118 5119  case ModuleManager::OutOfDate:5120    // We couldn't load the module file because it is out-of-date. If the5121    // client can handle out-of-date, return it.5122    if (ClientLoadCapabilities & ARR_OutOfDate)5123      return OutOfDate;5124 5125    // Otherwise, return an error.5126    Diag(diag::err_ast_file_out_of_date)5127        << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()5128        << ErrorStr;5129    return Failure;5130  }5131 5132  assert(M && "Missing module file");5133 5134  bool ShouldFinalizePCM = false;5135  auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {5136    auto &MC = getModuleManager().getModuleCache().getInMemoryModuleCache();5137    if (ShouldFinalizePCM)5138      MC.finalizePCM(FileName);5139    else5140      MC.tryToDropPCM(FileName);5141  });5142  ModuleFile &F = *M;5143  BitstreamCursor &Stream = F.Stream;5144  Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));5145  F.SizeInBits = F.Buffer->getBufferSize() * 8;5146 5147  // Sniff for the signature.5148  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {5149    Diag(diag::err_ast_file_invalid)5150        << moduleKindForDiagnostic(Type) << FileName << std::move(Err);5151    return Failure;5152  }5153 5154  // This is used for compatibility with older PCH formats.5155  bool HaveReadControlBlock = false;5156  while (true) {5157    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();5158    if (!MaybeEntry) {5159      Error(MaybeEntry.takeError());5160      return Failure;5161    }5162    llvm::BitstreamEntry Entry = MaybeEntry.get();5163 5164    switch (Entry.Kind) {5165    case llvm::BitstreamEntry::Error:5166    case llvm::BitstreamEntry::Record:5167    case llvm::BitstreamEntry::EndBlock:5168      Error("invalid record at top-level of AST file");5169      return Failure;5170 5171    case llvm::BitstreamEntry::SubBlock:5172      break;5173    }5174 5175    switch (Entry.ID) {5176    case CONTROL_BLOCK_ID:5177      HaveReadControlBlock = true;5178      switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {5179      case Success:5180        // Check that we didn't try to load a non-module AST file as a module.5181        //5182        // FIXME: Should we also perform the converse check? Loading a module as5183        // a PCH file sort of works, but it's a bit wonky.5184        if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||5185             Type == MK_PrebuiltModule) &&5186            F.ModuleName.empty()) {5187          auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;5188          if (Result != OutOfDate ||5189              (ClientLoadCapabilities & ARR_OutOfDate) == 0)5190            Diag(diag::err_module_file_not_module) << FileName;5191          return Result;5192        }5193        break;5194 5195      case Failure: return Failure;5196      case Missing: return Missing;5197      case OutOfDate: return OutOfDate;5198      case VersionMismatch: return VersionMismatch;5199      case ConfigurationMismatch: return ConfigurationMismatch;5200      case HadErrors: return HadErrors;5201      }5202      break;5203 5204    case AST_BLOCK_ID:5205      if (!HaveReadControlBlock) {5206        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)5207          Diag(diag::err_ast_file_version_too_old)5208              << moduleKindForDiagnostic(Type) << FileName;5209        return VersionMismatch;5210      }5211 5212      // Record that we've loaded this module.5213      Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));5214      ShouldFinalizePCM = true;5215      return Success;5216 5217    default:5218      if (llvm::Error Err = Stream.SkipBlock()) {5219        Error(std::move(Err));5220        return Failure;5221      }5222      break;5223    }5224  }5225 5226  llvm_unreachable("unexpected break; expected return");5227}5228 5229ASTReader::ASTReadResult5230ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,5231                                    unsigned ClientLoadCapabilities) {5232  const HeaderSearchOptions &HSOpts =5233      PP.getHeaderSearchInfo().getHeaderSearchOpts();5234  bool AllowCompatibleConfigurationMismatch =5235      F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;5236  bool DisableValidation = shouldDisableValidationForFile(F);5237 5238  ASTReadResult Result = readUnhashedControlBlockImpl(5239      &F, F.Data, F.FileName, ClientLoadCapabilities,5240      AllowCompatibleConfigurationMismatch, Listener.get(),5241      WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);5242 5243  // If F was directly imported by another module, it's implicitly validated by5244  // the importing module.5245  if (DisableValidation || WasImportedBy ||5246      (AllowConfigurationMismatch && Result == ConfigurationMismatch))5247    return Success;5248 5249  if (Result == Failure) {5250    Error("malformed block record in AST file");5251    return Failure;5252  }5253 5254  if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {5255    // If this module has already been finalized in the ModuleCache, we're stuck5256    // with it; we can only load a single version of each module.5257    //5258    // This can happen when a module is imported in two contexts: in one, as a5259    // user module; in another, as a system module (due to an import from5260    // another module marked with the [system] flag).  It usually indicates a5261    // bug in the module map: this module should also be marked with [system].5262    //5263    // If -Wno-system-headers (the default), and the first import is as a5264    // system module, then validation will fail during the as-user import,5265    // since -Werror flags won't have been validated.  However, it's reasonable5266    // to treat this consistently as a system module.5267    //5268    // If -Wsystem-headers, the PCM on disk was built with5269    // -Wno-system-headers, and the first import is as a user module, then5270    // validation will fail during the as-system import since the PCM on disk5271    // doesn't guarantee that -Werror was respected.  However, the -Werror5272    // flags were checked during the initial as-user import.5273    if (getModuleManager().getModuleCache().getInMemoryModuleCache().isPCMFinal(5274            F.FileName)) {5275      Diag(diag::warn_module_system_bit_conflict) << F.FileName;5276      return Success;5277    }5278  }5279 5280  return Result;5281}5282 5283ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(5284    ModuleFile *F, llvm::StringRef StreamData, StringRef Filename,5285    unsigned ClientLoadCapabilities, bool AllowCompatibleConfigurationMismatch,5286    ASTReaderListener *Listener, bool ValidateDiagnosticOptions) {5287  // Initialize a stream.5288  BitstreamCursor Stream(StreamData);5289 5290  // Sniff for the signature.5291  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {5292    // FIXME this drops the error on the floor.5293    consumeError(std::move(Err));5294    return Failure;5295  }5296 5297  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.5298  if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))5299    return Failure;5300 5301  // Read all of the records in the options block.5302  RecordData Record;5303  ASTReadResult Result = Success;5304  while (true) {5305    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();5306    if (!MaybeEntry) {5307      // FIXME this drops the error on the floor.5308      consumeError(MaybeEntry.takeError());5309      return Failure;5310    }5311    llvm::BitstreamEntry Entry = MaybeEntry.get();5312 5313    switch (Entry.Kind) {5314    case llvm::BitstreamEntry::Error:5315    case llvm::BitstreamEntry::SubBlock:5316      return Failure;5317 5318    case llvm::BitstreamEntry::EndBlock:5319      return Result;5320 5321    case llvm::BitstreamEntry::Record:5322      // The interesting case.5323      break;5324    }5325 5326    // Read and process a record.5327    Record.clear();5328    StringRef Blob;5329    Expected<unsigned> MaybeRecordType =5330        Stream.readRecord(Entry.ID, Record, &Blob);5331    if (!MaybeRecordType) {5332      // FIXME this drops the error.5333      return Failure;5334    }5335    switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {5336    case SIGNATURE:5337      if (F) {5338        F->Signature = ASTFileSignature::create(Blob.begin(), Blob.end());5339        assert(F->Signature != ASTFileSignature::createDummy() &&5340               "Dummy AST file signature not backpatched in ASTWriter.");5341      }5342      break;5343    case AST_BLOCK_HASH:5344      if (F) {5345        F->ASTBlockHash = ASTFileSignature::create(Blob.begin(), Blob.end());5346        assert(F->ASTBlockHash != ASTFileSignature::createDummy() &&5347               "Dummy AST block hash not backpatched in ASTWriter.");5348      }5349      break;5350    case DIAGNOSTIC_OPTIONS: {5351      bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;5352      if (Listener && ValidateDiagnosticOptions &&5353          !AllowCompatibleConfigurationMismatch &&5354          ParseDiagnosticOptions(Record, Filename, Complain, *Listener))5355        Result = OutOfDate; // Don't return early.  Read the signature.5356      break;5357    }5358    case HEADER_SEARCH_PATHS: {5359      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;5360      if (Listener && !AllowCompatibleConfigurationMismatch &&5361          ParseHeaderSearchPaths(Record, Complain, *Listener))5362        Result = ConfigurationMismatch;5363      break;5364    }5365    case DIAG_PRAGMA_MAPPINGS:5366      if (!F)5367        break;5368      if (F->PragmaDiagMappings.empty())5369        F->PragmaDiagMappings.swap(Record);5370      else5371        F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),5372                                     Record.begin(), Record.end());5373      break;5374    case HEADER_SEARCH_ENTRY_USAGE:5375      if (F)5376        F->SearchPathUsage = ReadBitVector(Record, Blob);5377      break;5378    case VFS_USAGE:5379      if (F)5380        F->VFSUsage = ReadBitVector(Record, Blob);5381      break;5382    }5383  }5384}5385 5386/// Parse a record and blob containing module file extension metadata.5387static bool parseModuleFileExtensionMetadata(5388              const SmallVectorImpl<uint64_t> &Record,5389              StringRef Blob,5390              ModuleFileExtensionMetadata &Metadata) {5391  if (Record.size() < 4) return true;5392 5393  Metadata.MajorVersion = Record[0];5394  Metadata.MinorVersion = Record[1];5395 5396  unsigned BlockNameLen = Record[2];5397  unsigned UserInfoLen = Record[3];5398 5399  if (BlockNameLen + UserInfoLen > Blob.size()) return true;5400 5401  Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);5402  Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,5403                                  Blob.data() + BlockNameLen + UserInfoLen);5404  return false;5405}5406 5407llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {5408  BitstreamCursor &Stream = F.Stream;5409 5410  RecordData Record;5411  while (true) {5412    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();5413    if (!MaybeEntry)5414      return MaybeEntry.takeError();5415    llvm::BitstreamEntry Entry = MaybeEntry.get();5416 5417    switch (Entry.Kind) {5418    case llvm::BitstreamEntry::SubBlock:5419      if (llvm::Error Err = Stream.SkipBlock())5420        return Err;5421      continue;5422    case llvm::BitstreamEntry::EndBlock:5423      return llvm::Error::success();5424    case llvm::BitstreamEntry::Error:5425      return llvm::createStringError(std::errc::illegal_byte_sequence,5426                                     "malformed block record in AST file");5427    case llvm::BitstreamEntry::Record:5428      break;5429    }5430 5431    Record.clear();5432    StringRef Blob;5433    Expected<unsigned> MaybeRecCode =5434        Stream.readRecord(Entry.ID, Record, &Blob);5435    if (!MaybeRecCode)5436      return MaybeRecCode.takeError();5437    switch (MaybeRecCode.get()) {5438    case EXTENSION_METADATA: {5439      ModuleFileExtensionMetadata Metadata;5440      if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))5441        return llvm::createStringError(5442            std::errc::illegal_byte_sequence,5443            "malformed EXTENSION_METADATA in AST file");5444 5445      // Find a module file extension with this block name.5446      auto Known = ModuleFileExtensions.find(Metadata.BlockName);5447      if (Known == ModuleFileExtensions.end()) break;5448 5449      // Form a reader.5450      if (auto Reader = Known->second->createExtensionReader(Metadata, *this,5451                                                             F, Stream)) {5452        F.ExtensionReaders.push_back(std::move(Reader));5453      }5454 5455      break;5456    }5457    }5458  }5459 5460  llvm_unreachable("ReadExtensionBlock should return from while loop");5461}5462 5463void ASTReader::InitializeContext() {5464  assert(ContextObj && "no context to initialize");5465  ASTContext &Context = *ContextObj;5466 5467  // If there's a listener, notify them that we "read" the translation unit.5468  if (DeserializationListener)5469    DeserializationListener->DeclRead(5470        GlobalDeclID(PREDEF_DECL_TRANSLATION_UNIT_ID),5471        Context.getTranslationUnitDecl());5472 5473  // FIXME: Find a better way to deal with collisions between these5474  // built-in types. Right now, we just ignore the problem.5475 5476  // Load the special types.5477  if (SpecialTypes.size() >= NumSpecialTypeIDs) {5478    if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {5479      if (!Context.CFConstantStringTypeDecl)5480        Context.setCFConstantStringType(GetType(String));5481    }5482 5483    if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {5484      QualType FileType = GetType(File);5485      if (FileType.isNull()) {5486        Error("FILE type is NULL");5487        return;5488      }5489 5490      if (!Context.FILEDecl) {5491        if (const TypedefType *Typedef = FileType->getAs<TypedefType>())5492          Context.setFILEDecl(Typedef->getDecl());5493        else {5494          const TagType *Tag = FileType->getAs<TagType>();5495          if (!Tag) {5496            Error("Invalid FILE type in AST file");5497            return;5498          }5499          Context.setFILEDecl(Tag->getDecl());5500        }5501      }5502    }5503 5504    if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {5505      QualType Jmp_bufType = GetType(Jmp_buf);5506      if (Jmp_bufType.isNull()) {5507        Error("jmp_buf type is NULL");5508        return;5509      }5510 5511      if (!Context.jmp_bufDecl) {5512        if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())5513          Context.setjmp_bufDecl(Typedef->getDecl());5514        else {5515          const TagType *Tag = Jmp_bufType->getAs<TagType>();5516          if (!Tag) {5517            Error("Invalid jmp_buf type in AST file");5518            return;5519          }5520          Context.setjmp_bufDecl(Tag->getDecl());5521        }5522      }5523    }5524 5525    if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {5526      QualType Sigjmp_bufType = GetType(Sigjmp_buf);5527      if (Sigjmp_bufType.isNull()) {5528        Error("sigjmp_buf type is NULL");5529        return;5530      }5531 5532      if (!Context.sigjmp_bufDecl) {5533        if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())5534          Context.setsigjmp_bufDecl(Typedef->getDecl());5535        else {5536          const TagType *Tag = Sigjmp_bufType->getAs<TagType>();5537          assert(Tag && "Invalid sigjmp_buf type in AST file");5538          Context.setsigjmp_bufDecl(Tag->getDecl());5539        }5540      }5541    }5542 5543    if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {5544      if (Context.ObjCIdRedefinitionType.isNull())5545        Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);5546    }5547 5548    if (TypeID ObjCClassRedef =5549            SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {5550      if (Context.ObjCClassRedefinitionType.isNull())5551        Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);5552    }5553 5554    if (TypeID ObjCSelRedef =5555            SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {5556      if (Context.ObjCSelRedefinitionType.isNull())5557        Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);5558    }5559 5560    if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {5561      QualType Ucontext_tType = GetType(Ucontext_t);5562      if (Ucontext_tType.isNull()) {5563        Error("ucontext_t type is NULL");5564        return;5565      }5566 5567      if (!Context.ucontext_tDecl) {5568        if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())5569          Context.setucontext_tDecl(Typedef->getDecl());5570        else {5571          const TagType *Tag = Ucontext_tType->getAs<TagType>();5572          assert(Tag && "Invalid ucontext_t type in AST file");5573          Context.setucontext_tDecl(Tag->getDecl());5574        }5575      }5576    }5577  }5578 5579  ReadPragmaDiagnosticMappings(Context.getDiagnostics());5580 5581  // If there were any CUDA special declarations, deserialize them.5582  if (!CUDASpecialDeclRefs.empty()) {5583    assert(CUDASpecialDeclRefs.size() == 3 && "More decl refs than expected!");5584    Context.setcudaConfigureCallDecl(5585        cast_or_null<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));5586    Context.setcudaGetParameterBufferDecl(5587        cast_or_null<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[1])));5588    Context.setcudaLaunchDeviceDecl(5589        cast_or_null<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[2])));5590  }5591 5592  // Re-export any modules that were imported by a non-module AST file.5593  // FIXME: This does not make macro-only imports visible again.5594  for (auto &Import : PendingImportedModules) {5595    if (Module *Imported = getSubmodule(Import.ID)) {5596      makeModuleVisible(Imported, Module::AllVisible,5597                        /*ImportLoc=*/Import.ImportLoc);5598      if (Import.ImportLoc.isValid())5599        PP.makeModuleVisible(Imported, Import.ImportLoc);5600      // This updates visibility for Preprocessor only. For Sema, which can be5601      // nullptr here, we do the same later, in UpdateSema().5602    }5603  }5604 5605  // Hand off these modules to Sema.5606  PendingImportedModulesSema.append(PendingImportedModules);5607  PendingImportedModules.clear();5608}5609 5610void ASTReader::finalizeForWriting() {5611  // Nothing to do for now.5612}5613 5614/// Reads and return the signature record from \p PCH's control block, or5615/// else returns 0.5616static ASTFileSignature readASTFileSignature(StringRef PCH) {5617  BitstreamCursor Stream(PCH);5618  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {5619    // FIXME this drops the error on the floor.5620    consumeError(std::move(Err));5621    return ASTFileSignature();5622  }5623 5624  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.5625  if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))5626    return ASTFileSignature();5627 5628  // Scan for SIGNATURE inside the diagnostic options block.5629  ASTReader::RecordData Record;5630  while (true) {5631    Expected<llvm::BitstreamEntry> MaybeEntry =5632        Stream.advanceSkippingSubblocks();5633    if (!MaybeEntry) {5634      // FIXME this drops the error on the floor.5635      consumeError(MaybeEntry.takeError());5636      return ASTFileSignature();5637    }5638    llvm::BitstreamEntry Entry = MaybeEntry.get();5639 5640    if (Entry.Kind != llvm::BitstreamEntry::Record)5641      return ASTFileSignature();5642 5643    Record.clear();5644    StringRef Blob;5645    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);5646    if (!MaybeRecord) {5647      // FIXME this drops the error on the floor.5648      consumeError(MaybeRecord.takeError());5649      return ASTFileSignature();5650    }5651    if (SIGNATURE == MaybeRecord.get()) {5652      auto Signature = ASTFileSignature::create(Blob.begin(), Blob.end());5653      assert(Signature != ASTFileSignature::createDummy() &&5654             "Dummy AST file signature not backpatched in ASTWriter.");5655      return Signature;5656    }5657  }5658}5659 5660/// Retrieve the name of the original source file name5661/// directly from the AST file, without actually loading the AST5662/// file.5663std::string ASTReader::getOriginalSourceFile(5664    const std::string &ASTFileName, FileManager &FileMgr,5665    const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {5666  // Open the AST file.5667  auto Buffer = FileMgr.getBufferForFile(ASTFileName, /*IsVolatile=*/false,5668                                         /*RequiresNullTerminator=*/false,5669                                         /*MaybeLimit=*/std::nullopt,5670                                         /*IsText=*/false);5671  if (!Buffer) {5672    Diags.Report(diag::err_fe_unable_to_read_pch_file)5673        << ASTFileName << Buffer.getError().message();5674    return std::string();5675  }5676 5677  // Initialize the stream5678  BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));5679 5680  // Sniff for the signature.5681  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {5682    Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);5683    return std::string();5684  }5685 5686  // Scan for the CONTROL_BLOCK_ID block.5687  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {5688    Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;5689    return std::string();5690  }5691 5692  // Scan for ORIGINAL_FILE inside the control block.5693  RecordData Record;5694  while (true) {5695    Expected<llvm::BitstreamEntry> MaybeEntry =5696        Stream.advanceSkippingSubblocks();5697    if (!MaybeEntry) {5698      // FIXME this drops errors on the floor.5699      consumeError(MaybeEntry.takeError());5700      return std::string();5701    }5702    llvm::BitstreamEntry Entry = MaybeEntry.get();5703 5704    if (Entry.Kind == llvm::BitstreamEntry::EndBlock)5705      return std::string();5706 5707    if (Entry.Kind != llvm::BitstreamEntry::Record) {5708      Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;5709      return std::string();5710    }5711 5712    Record.clear();5713    StringRef Blob;5714    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);5715    if (!MaybeRecord) {5716      // FIXME this drops the errors on the floor.5717      consumeError(MaybeRecord.takeError());5718      return std::string();5719    }5720    if (ORIGINAL_FILE == MaybeRecord.get())5721      return Blob.str();5722  }5723}5724 5725namespace {5726 5727  class SimplePCHValidator : public ASTReaderListener {5728    const LangOptions &ExistingLangOpts;5729    const CodeGenOptions &ExistingCGOpts;5730    const TargetOptions &ExistingTargetOpts;5731    const PreprocessorOptions &ExistingPPOpts;5732    std::string ExistingModuleCachePath;5733    FileManager &FileMgr;5734    bool StrictOptionMatches;5735 5736  public:5737    SimplePCHValidator(const LangOptions &ExistingLangOpts,5738                       const CodeGenOptions &ExistingCGOpts,5739                       const TargetOptions &ExistingTargetOpts,5740                       const PreprocessorOptions &ExistingPPOpts,5741                       StringRef ExistingModuleCachePath, FileManager &FileMgr,5742                       bool StrictOptionMatches)5743        : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts),5744          ExistingTargetOpts(ExistingTargetOpts),5745          ExistingPPOpts(ExistingPPOpts),5746          ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr),5747          StrictOptionMatches(StrictOptionMatches) {}5748 5749    bool ReadLanguageOptions(const LangOptions &LangOpts,5750                             StringRef ModuleFilename, bool Complain,5751                             bool AllowCompatibleDifferences) override {5752      return checkLanguageOptions(ExistingLangOpts, LangOpts, ModuleFilename,5753                                  nullptr, AllowCompatibleDifferences);5754    }5755 5756    bool ReadCodeGenOptions(const CodeGenOptions &CGOpts,5757                            StringRef ModuleFilename, bool Complain,5758                            bool AllowCompatibleDifferences) override {5759      return checkCodegenOptions(ExistingCGOpts, CGOpts, ModuleFilename,5760                                 nullptr, AllowCompatibleDifferences);5761    }5762 5763    bool ReadTargetOptions(const TargetOptions &TargetOpts,5764                           StringRef ModuleFilename, bool Complain,5765                           bool AllowCompatibleDifferences) override {5766      return checkTargetOptions(ExistingTargetOpts, TargetOpts, ModuleFilename,5767                                nullptr, AllowCompatibleDifferences);5768    }5769 5770    bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,5771                                 StringRef ModuleFilename,5772                                 StringRef SpecificModuleCachePath,5773                                 bool Complain) override {5774      return checkModuleCachePath(FileMgr.getVirtualFileSystem(),5775                                  SpecificModuleCachePath,5776                                  ExistingModuleCachePath, ModuleFilename,5777                                  nullptr, ExistingLangOpts, ExistingPPOpts);5778    }5779 5780    bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,5781                                 StringRef ModuleFilename, bool ReadMacros,5782                                 bool Complain,5783                                 std::string &SuggestedPredefines) override {5784      return checkPreprocessorOptions(5785          PPOpts, ExistingPPOpts, ModuleFilename, ReadMacros, /*Diags=*/nullptr,5786          FileMgr, SuggestedPredefines, ExistingLangOpts,5787          StrictOptionMatches ? OptionValidateStrictMatches5788                              : OptionValidateContradictions);5789    }5790  };5791 5792} // namespace5793 5794bool ASTReader::readASTFileControlBlock(5795    StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,5796    const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions,5797    ASTReaderListener &Listener, bool ValidateDiagnosticOptions,5798    unsigned ClientLoadCapabilities) {5799  // Open the AST file.5800  std::unique_ptr<llvm::MemoryBuffer> OwnedBuffer;5801  llvm::MemoryBuffer *Buffer =5802      ModCache.getInMemoryModuleCache().lookupPCM(Filename);5803  if (!Buffer) {5804    // FIXME: We should add the pcm to the InMemoryModuleCache if it could be5805    // read again later, but we do not have the context here to determine if it5806    // is safe to change the result of InMemoryModuleCache::getPCMState().5807 5808    // FIXME: This allows use of the VFS; we do not allow use of the5809    // VFS when actually loading a module.5810    auto Entry =5811        Filename == "-" ? FileMgr.getSTDIN() : FileMgr.getFileRef(Filename);5812    if (!Entry) {5813      llvm::consumeError(Entry.takeError());5814      return true;5815    }5816    auto BufferOrErr = FileMgr.getBufferForFile(*Entry);5817    if (!BufferOrErr)5818      return true;5819    OwnedBuffer = std::move(*BufferOrErr);5820    Buffer = OwnedBuffer.get();5821  }5822 5823  // Initialize the stream5824  StringRef Bytes = PCHContainerRdr.ExtractPCH(*Buffer);5825  BitstreamCursor Stream(Bytes);5826 5827  // Sniff for the signature.5828  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {5829    consumeError(std::move(Err)); // FIXME this drops errors on the floor.5830    return true;5831  }5832 5833  // Scan for the CONTROL_BLOCK_ID block.5834  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))5835    return true;5836 5837  bool NeedsInputFiles = Listener.needsInputFileVisitation();5838  bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();5839  bool NeedsImports = Listener.needsImportVisitation();5840  BitstreamCursor InputFilesCursor;5841  uint64_t InputFilesOffsetBase = 0;5842 5843  RecordData Record;5844  std::string ModuleDir;5845  bool DoneWithControlBlock = false;5846  SmallString<0> PathBuf;5847  PathBuf.reserve(256);5848  // Additional path buffer to use when multiple paths need to be resolved.5849  // For example, when deserializing input files that contains a path that was5850  // resolved from a vfs overlay and an external location.5851  SmallString<0> AdditionalPathBuf;5852  AdditionalPathBuf.reserve(256);5853  while (!DoneWithControlBlock) {5854    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();5855    if (!MaybeEntry) {5856      // FIXME this drops the error on the floor.5857      consumeError(MaybeEntry.takeError());5858      return true;5859    }5860    llvm::BitstreamEntry Entry = MaybeEntry.get();5861 5862    switch (Entry.Kind) {5863    case llvm::BitstreamEntry::SubBlock: {5864      switch (Entry.ID) {5865      case OPTIONS_BLOCK_ID: {5866        std::string IgnoredSuggestedPredefines;5867        if (ReadOptionsBlock(Stream, Filename, ClientLoadCapabilities,5868                             /*AllowCompatibleConfigurationMismatch*/ false,5869                             Listener, IgnoredSuggestedPredefines) != Success)5870          return true;5871        break;5872      }5873 5874      case INPUT_FILES_BLOCK_ID:5875        InputFilesCursor = Stream;5876        if (llvm::Error Err = Stream.SkipBlock()) {5877          // FIXME this drops the error on the floor.5878          consumeError(std::move(Err));5879          return true;5880        }5881        if (NeedsInputFiles &&5882            ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))5883          return true;5884        InputFilesOffsetBase = InputFilesCursor.GetCurrentBitNo();5885        break;5886 5887      default:5888        if (llvm::Error Err = Stream.SkipBlock()) {5889          // FIXME this drops the error on the floor.5890          consumeError(std::move(Err));5891          return true;5892        }5893        break;5894      }5895 5896      continue;5897    }5898 5899    case llvm::BitstreamEntry::EndBlock:5900      DoneWithControlBlock = true;5901      break;5902 5903    case llvm::BitstreamEntry::Error:5904      return true;5905 5906    case llvm::BitstreamEntry::Record:5907      break;5908    }5909 5910    if (DoneWithControlBlock) break;5911 5912    Record.clear();5913    StringRef Blob;5914    Expected<unsigned> MaybeRecCode =5915        Stream.readRecord(Entry.ID, Record, &Blob);5916    if (!MaybeRecCode) {5917      // FIXME this drops the error.5918      return Failure;5919    }5920    switch ((ControlRecordTypes)MaybeRecCode.get()) {5921    case METADATA:5922      if (Record[0] != VERSION_MAJOR)5923        return true;5924      if (Listener.ReadFullVersionInformation(Blob))5925        return true;5926      break;5927    case MODULE_NAME:5928      Listener.ReadModuleName(Blob);5929      break;5930    case MODULE_DIRECTORY:5931      ModuleDir = std::string(Blob);5932      break;5933    case MODULE_MAP_FILE: {5934      unsigned Idx = 0;5935      std::string PathStr = ReadString(Record, Idx);5936      auto Path = ResolveImportedPath(PathBuf, PathStr, ModuleDir);5937      Listener.ReadModuleMapFile(*Path);5938      break;5939    }5940    case INPUT_FILE_OFFSETS: {5941      if (!NeedsInputFiles)5942        break;5943 5944      unsigned NumInputFiles = Record[0];5945      unsigned NumUserFiles = Record[1];5946      const llvm::support::unaligned_uint64_t *InputFileOffs =5947          (const llvm::support::unaligned_uint64_t *)Blob.data();5948      for (unsigned I = 0; I != NumInputFiles; ++I) {5949        // Go find this input file.5950        bool isSystemFile = I >= NumUserFiles;5951 5952        if (isSystemFile && !NeedsSystemInputFiles)5953          break; // the rest are system input files5954 5955        BitstreamCursor &Cursor = InputFilesCursor;5956        SavedStreamPosition SavedPosition(Cursor);5957        if (llvm::Error Err =5958                Cursor.JumpToBit(InputFilesOffsetBase + InputFileOffs[I])) {5959          // FIXME this drops errors on the floor.5960          consumeError(std::move(Err));5961        }5962 5963        Expected<unsigned> MaybeCode = Cursor.ReadCode();5964        if (!MaybeCode) {5965          // FIXME this drops errors on the floor.5966          consumeError(MaybeCode.takeError());5967        }5968        unsigned Code = MaybeCode.get();5969 5970        RecordData Record;5971        StringRef Blob;5972        bool shouldContinue = false;5973        Expected<unsigned> MaybeRecordType =5974            Cursor.readRecord(Code, Record, &Blob);5975        if (!MaybeRecordType) {5976          // FIXME this drops errors on the floor.5977          consumeError(MaybeRecordType.takeError());5978        }5979        switch ((InputFileRecordTypes)MaybeRecordType.get()) {5980        case INPUT_FILE_HASH:5981          break;5982        case INPUT_FILE:5983          bool Overridden = static_cast<bool>(Record[3]);5984          auto [UnresolvedFilenameAsRequested, UnresolvedFilename] =5985              getUnresolvedInputFilenames(Record, Blob);5986          auto FilenameAsRequestedBuf = ResolveImportedPath(5987              PathBuf, UnresolvedFilenameAsRequested, ModuleDir);5988          StringRef Filename;5989          if (UnresolvedFilename.empty())5990            Filename = *FilenameAsRequestedBuf;5991          else {5992            auto FilenameBuf = ResolveImportedPath(5993                AdditionalPathBuf, UnresolvedFilename, ModuleDir);5994            Filename = *FilenameBuf;5995          }5996          shouldContinue = Listener.visitInputFileAsRequested(5997              *FilenameAsRequestedBuf, Filename, isSystemFile, Overridden,5998              /*IsExplicitModule=*/false);5999          break;6000        }6001        if (!shouldContinue)6002          break;6003      }6004      break;6005    }6006 6007    case IMPORT: {6008      if (!NeedsImports)6009        break;6010 6011      unsigned Idx = 0;6012      // Read information about the AST file.6013 6014      // Skip Kind6015      Idx++;6016 6017      // Skip ImportLoc6018      Idx++;6019 6020      StringRef ModuleName = ReadStringBlob(Record, Idx, Blob);6021 6022      bool IsStandardCXXModule = Record[Idx++];6023 6024      // In C++20 Modules, we don't record the path to imported6025      // modules in the BMI files.6026      if (IsStandardCXXModule) {6027        Listener.visitImport(ModuleName, /*Filename=*/"");6028        continue;6029      }6030 6031      // Skip Size and ModTime.6032      Idx += 1 + 1;6033      // Skip signature.6034      Blob = Blob.substr(ASTFileSignature::size);6035 6036      StringRef FilenameStr = ReadStringBlob(Record, Idx, Blob);6037      auto Filename = ResolveImportedPath(PathBuf, FilenameStr, ModuleDir);6038      Listener.visitImport(ModuleName, *Filename);6039      break;6040    }6041 6042    default:6043      // No other validation to perform.6044      break;6045    }6046  }6047 6048  // Look for module file extension blocks, if requested.6049  if (FindModuleFileExtensions) {6050    BitstreamCursor SavedStream = Stream;6051    while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {6052      bool DoneWithExtensionBlock = false;6053      while (!DoneWithExtensionBlock) {6054        Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();6055        if (!MaybeEntry) {6056          // FIXME this drops the error.6057          return true;6058        }6059        llvm::BitstreamEntry Entry = MaybeEntry.get();6060 6061        switch (Entry.Kind) {6062        case llvm::BitstreamEntry::SubBlock:6063          if (llvm::Error Err = Stream.SkipBlock()) {6064            // FIXME this drops the error on the floor.6065            consumeError(std::move(Err));6066            return true;6067          }6068          continue;6069 6070        case llvm::BitstreamEntry::EndBlock:6071          DoneWithExtensionBlock = true;6072          continue;6073 6074        case llvm::BitstreamEntry::Error:6075          return true;6076 6077        case llvm::BitstreamEntry::Record:6078          break;6079        }6080 6081       Record.clear();6082       StringRef Blob;6083       Expected<unsigned> MaybeRecCode =6084           Stream.readRecord(Entry.ID, Record, &Blob);6085       if (!MaybeRecCode) {6086         // FIXME this drops the error.6087         return true;6088       }6089       switch (MaybeRecCode.get()) {6090       case EXTENSION_METADATA: {6091         ModuleFileExtensionMetadata Metadata;6092         if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))6093           return true;6094 6095         Listener.readModuleFileExtension(Metadata);6096         break;6097       }6098       }6099      }6100    }6101    Stream = std::move(SavedStream);6102  }6103 6104  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.6105  if (readUnhashedControlBlockImpl(6106          nullptr, Bytes, Filename, ClientLoadCapabilities,6107          /*AllowCompatibleConfigurationMismatch*/ false, &Listener,6108          ValidateDiagnosticOptions) != Success)6109    return true;6110 6111  return false;6112}6113 6114bool ASTReader::isAcceptableASTFile(6115    StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache,6116    const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts,6117    const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts,6118    const PreprocessorOptions &PPOpts, StringRef ExistingModuleCachePath,6119    bool RequireStrictOptionMatches) {6120  SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts,6121                               ExistingModuleCachePath, FileMgr,6122                               RequireStrictOptionMatches);6123  return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr,6124                                  /*FindModuleFileExtensions=*/false, validator,6125                                  /*ValidateDiagnosticOptions=*/true);6126}6127 6128llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,6129                                          unsigned ClientLoadCapabilities) {6130  // Enter the submodule block.6131  if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))6132    return Err;6133 6134  ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();6135  bool KnowsTopLevelModule = ModMap.findModule(F.ModuleName) != nullptr;6136  // If we don't know the top-level module, there's no point in doing qualified6137  // lookup of its submodules; it won't find anything anywhere within this tree.6138  // Let's skip that and avoid some string lookups.6139  auto CreateModule = !KnowsTopLevelModule6140                          ? &ModuleMap::createModule6141                          : &ModuleMap::findOrCreateModuleFirst;6142 6143  bool First = true;6144  Module *CurrentModule = nullptr;6145  RecordData Record;6146  while (true) {6147    Expected<llvm::BitstreamEntry> MaybeEntry =6148        F.Stream.advanceSkippingSubblocks();6149    if (!MaybeEntry)6150      return MaybeEntry.takeError();6151    llvm::BitstreamEntry Entry = MaybeEntry.get();6152 6153    switch (Entry.Kind) {6154    case llvm::BitstreamEntry::SubBlock: // Handled for us already.6155    case llvm::BitstreamEntry::Error:6156      return llvm::createStringError(std::errc::illegal_byte_sequence,6157                                     "malformed block record in AST file");6158    case llvm::BitstreamEntry::EndBlock:6159      return llvm::Error::success();6160    case llvm::BitstreamEntry::Record:6161      // The interesting case.6162      break;6163    }6164 6165    // Read a record.6166    StringRef Blob;6167    Record.clear();6168    Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);6169    if (!MaybeKind)6170      return MaybeKind.takeError();6171    unsigned Kind = MaybeKind.get();6172 6173    if ((Kind == SUBMODULE_METADATA) != First)6174      return llvm::createStringError(6175          std::errc::illegal_byte_sequence,6176          "submodule metadata record should be at beginning of block");6177    First = false;6178 6179    // Submodule information is only valid if we have a current module.6180    // FIXME: Should we error on these cases?6181    if (!CurrentModule && Kind != SUBMODULE_METADATA &&6182        Kind != SUBMODULE_DEFINITION)6183      continue;6184 6185    switch (Kind) {6186    default:  // Default behavior: ignore.6187      break;6188 6189    case SUBMODULE_DEFINITION: {6190      if (Record.size() < 13)6191        return llvm::createStringError(std::errc::illegal_byte_sequence,6192                                       "malformed module definition");6193 6194      StringRef Name = Blob;6195      unsigned Idx = 0;6196      SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);6197      SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);6198      Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];6199      SourceLocation DefinitionLoc = ReadSourceLocation(F, Record[Idx++]);6200      FileID InferredAllowedBy = ReadFileID(F, Record, Idx);6201      bool IsFramework = Record[Idx++];6202      bool IsExplicit = Record[Idx++];6203      bool IsSystem = Record[Idx++];6204      bool IsExternC = Record[Idx++];6205      bool InferSubmodules = Record[Idx++];6206      bool InferExplicitSubmodules = Record[Idx++];6207      bool InferExportWildcard = Record[Idx++];6208      bool ConfigMacrosExhaustive = Record[Idx++];6209      bool ModuleMapIsPrivate = Record[Idx++];6210      bool NamedModuleHasInit = Record[Idx++];6211 6212      Module *ParentModule = nullptr;6213      if (Parent)6214        ParentModule = getSubmodule(Parent);6215 6216      CurrentModule = std::invoke(CreateModule, &ModMap, Name, ParentModule,6217                                  IsFramework, IsExplicit);6218 6219      SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;6220      if (GlobalIndex >= SubmodulesLoaded.size() ||6221          SubmodulesLoaded[GlobalIndex])6222        return llvm::createStringError(std::errc::invalid_argument,6223                                       "too many submodules");6224 6225      if (!ParentModule) {6226        if (OptionalFileEntryRef CurFile = CurrentModule->getASTFile()) {6227          // Don't emit module relocation error if we have -fno-validate-pch6228          if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &6229                    DisableValidationForModuleKind::Module)) {6230            assert(CurFile != F.File && "ModuleManager did not de-duplicate");6231 6232            Diag(diag::err_module_file_conflict)6233                << CurrentModule->getTopLevelModuleName() << CurFile->getName()6234                << F.File.getName();6235 6236            auto CurModMapFile =6237                ModMap.getContainingModuleMapFile(CurrentModule);6238            auto ModMapFile = FileMgr.getOptionalFileRef(F.ModuleMapPath);6239            if (CurModMapFile && ModMapFile && CurModMapFile != ModMapFile)6240              Diag(diag::note_module_file_conflict)6241                  << CurModMapFile->getName() << ModMapFile->getName();6242 6243            return llvm::make_error<AlreadyReportedDiagnosticError>();6244          }6245        }6246 6247        F.DidReadTopLevelSubmodule = true;6248        CurrentModule->setASTFile(F.File);6249        CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;6250      }6251 6252      CurrentModule->Kind = Kind;6253      // Note that we may be rewriting an existing location and it is important6254      // to keep doing that. In particular, we would like to prefer a6255      // `DefinitionLoc` loaded from the module file instead of the location6256      // created in the current source manager, because it allows the new6257      // location to be marked as "unaffecting" when writing and avoid creating6258      // duplicate locations for the same module map file.6259      CurrentModule->DefinitionLoc = DefinitionLoc;6260      CurrentModule->Signature = F.Signature;6261      CurrentModule->IsFromModuleFile = true;6262      if (InferredAllowedBy.isValid())6263        ModMap.setInferredModuleAllowedBy(CurrentModule, InferredAllowedBy);6264      CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;6265      CurrentModule->IsExternC = IsExternC;6266      CurrentModule->InferSubmodules = InferSubmodules;6267      CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;6268      CurrentModule->InferExportWildcard = InferExportWildcard;6269      CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;6270      CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;6271      CurrentModule->NamedModuleHasInit = NamedModuleHasInit;6272      if (DeserializationListener)6273        DeserializationListener->ModuleRead(GlobalID, CurrentModule);6274 6275      SubmodulesLoaded[GlobalIndex] = CurrentModule;6276 6277      // Clear out data that will be replaced by what is in the module file.6278      CurrentModule->LinkLibraries.clear();6279      CurrentModule->ConfigMacros.clear();6280      CurrentModule->UnresolvedConflicts.clear();6281      CurrentModule->Conflicts.clear();6282 6283      // The module is available unless it's missing a requirement; relevant6284      // requirements will be (re-)added by SUBMODULE_REQUIRES records.6285      // Missing headers that were present when the module was built do not6286      // make it unavailable -- if we got this far, this must be an explicitly6287      // imported module file.6288      CurrentModule->Requirements.clear();6289      CurrentModule->MissingHeaders.clear();6290      CurrentModule->IsUnimportable =6291          ParentModule && ParentModule->IsUnimportable;6292      CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;6293      break;6294    }6295 6296    case SUBMODULE_UMBRELLA_HEADER: {6297      // FIXME: This doesn't work for framework modules as `Filename` is the6298      //        name as written in the module file and does not include6299      //        `Headers/`, so this path will never exist.6300      auto Filename = ResolveImportedPath(PathBuf, Blob, F);6301      if (auto Umbrella = PP.getFileManager().getOptionalFileRef(*Filename)) {6302        if (!CurrentModule->getUmbrellaHeaderAsWritten()) {6303          // FIXME: NameAsWritten6304          ModMap.setUmbrellaHeaderAsWritten(CurrentModule, *Umbrella, Blob, "");6305        }6306        // Note that it's too late at this point to return out of date if the6307        // name from the PCM doesn't match up with the one in the module map,6308        // but also quite unlikely since we will have already checked the6309        // modification time and size of the module map file itself.6310      }6311      break;6312    }6313 6314    case SUBMODULE_HEADER:6315    case SUBMODULE_EXCLUDED_HEADER:6316    case SUBMODULE_PRIVATE_HEADER:6317      // We lazily associate headers with their modules via the HeaderInfo table.6318      // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead6319      // of complete filenames or remove it entirely.6320      break;6321 6322    case SUBMODULE_TEXTUAL_HEADER:6323    case SUBMODULE_PRIVATE_TEXTUAL_HEADER:6324      // FIXME: Textual headers are not marked in the HeaderInfo table. Load6325      // them here.6326      break;6327 6328    case SUBMODULE_TOPHEADER: {6329      auto HeaderName = ResolveImportedPath(PathBuf, Blob, F);6330      CurrentModule->addTopHeaderFilename(*HeaderName);6331      break;6332    }6333 6334    case SUBMODULE_UMBRELLA_DIR: {6335      // See comments in SUBMODULE_UMBRELLA_HEADER6336      auto Dirname = ResolveImportedPath(PathBuf, Blob, F);6337      if (auto Umbrella =6338              PP.getFileManager().getOptionalDirectoryRef(*Dirname)) {6339        if (!CurrentModule->getUmbrellaDirAsWritten()) {6340          // FIXME: NameAsWritten6341          ModMap.setUmbrellaDirAsWritten(CurrentModule, *Umbrella, Blob, "");6342        }6343      }6344      break;6345    }6346 6347    case SUBMODULE_METADATA: {6348      F.BaseSubmoduleID = getTotalNumSubmodules();6349      F.LocalNumSubmodules = Record[0];6350      unsigned LocalBaseSubmoduleID = Record[1];6351      if (F.LocalNumSubmodules > 0) {6352        // Introduce the global -> local mapping for submodules within this6353        // module.6354        GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));6355 6356        // Introduce the local -> global mapping for submodules within this6357        // module.6358        F.SubmoduleRemap.insertOrReplace(6359          std::make_pair(LocalBaseSubmoduleID,6360                         F.BaseSubmoduleID - LocalBaseSubmoduleID));6361 6362        SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);6363      }6364      break;6365    }6366 6367    case SUBMODULE_IMPORTS:6368      for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {6369        UnresolvedModuleRef Unresolved;6370        Unresolved.File = &F;6371        Unresolved.Mod = CurrentModule;6372        Unresolved.ID = Record[Idx];6373        Unresolved.Kind = UnresolvedModuleRef::Import;6374        Unresolved.IsWildcard = false;6375        UnresolvedModuleRefs.push_back(Unresolved);6376      }6377      break;6378 6379    case SUBMODULE_AFFECTING_MODULES:6380      for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {6381        UnresolvedModuleRef Unresolved;6382        Unresolved.File = &F;6383        Unresolved.Mod = CurrentModule;6384        Unresolved.ID = Record[Idx];6385        Unresolved.Kind = UnresolvedModuleRef::Affecting;6386        Unresolved.IsWildcard = false;6387        UnresolvedModuleRefs.push_back(Unresolved);6388      }6389      break;6390 6391    case SUBMODULE_EXPORTS:6392      for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {6393        UnresolvedModuleRef Unresolved;6394        Unresolved.File = &F;6395        Unresolved.Mod = CurrentModule;6396        Unresolved.ID = Record[Idx];6397        Unresolved.Kind = UnresolvedModuleRef::Export;6398        Unresolved.IsWildcard = Record[Idx + 1];6399        UnresolvedModuleRefs.push_back(Unresolved);6400      }6401 6402      // Once we've loaded the set of exports, there's no reason to keep6403      // the parsed, unresolved exports around.6404      CurrentModule->UnresolvedExports.clear();6405      break;6406 6407    case SUBMODULE_REQUIRES:6408      CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),6409                                    PP.getTargetInfo());6410      break;6411 6412    case SUBMODULE_LINK_LIBRARY:6413      ModMap.resolveLinkAsDependencies(CurrentModule);6414      CurrentModule->LinkLibraries.push_back(6415          Module::LinkLibrary(std::string(Blob), Record[0]));6416      break;6417 6418    case SUBMODULE_CONFIG_MACRO:6419      CurrentModule->ConfigMacros.push_back(Blob.str());6420      break;6421 6422    case SUBMODULE_CONFLICT: {6423      UnresolvedModuleRef Unresolved;6424      Unresolved.File = &F;6425      Unresolved.Mod = CurrentModule;6426      Unresolved.ID = Record[0];6427      Unresolved.Kind = UnresolvedModuleRef::Conflict;6428      Unresolved.IsWildcard = false;6429      Unresolved.String = Blob;6430      UnresolvedModuleRefs.push_back(Unresolved);6431      break;6432    }6433 6434    case SUBMODULE_INITIALIZERS: {6435      if (!ContextObj)6436        break;6437      // Standard C++ module has its own way to initialize variables.6438      if (!F.StandardCXXModule || F.Kind == MK_MainFile) {6439        SmallVector<GlobalDeclID, 16> Inits;6440        for (unsigned I = 0; I < Record.size(); /*in loop*/)6441          Inits.push_back(ReadDeclID(F, Record, I));6442        ContextObj->addLazyModuleInitializers(CurrentModule, Inits);6443      }6444      break;6445    }6446 6447    case SUBMODULE_EXPORT_AS:6448      CurrentModule->ExportAsModule = Blob.str();6449      ModMap.addLinkAsDependency(CurrentModule);6450      break;6451    }6452  }6453}6454 6455/// Parse the record that corresponds to a LangOptions data6456/// structure.6457///6458/// This routine parses the language options from the AST file and then gives6459/// them to the AST listener if one is set.6460///6461/// \returns true if the listener deems the file unacceptable, false otherwise.6462bool ASTReader::ParseLanguageOptions(const RecordData &Record,6463                                     StringRef ModuleFilename, bool Complain,6464                                     ASTReaderListener &Listener,6465                                     bool AllowCompatibleDifferences) {6466  LangOptions LangOpts;6467  unsigned Idx = 0;6468#define LANGOPT(Name, Bits, Default, Compatibility, Description)               \6469  LangOpts.Name = Record[Idx++];6470#define ENUM_LANGOPT(Name, Type, Bits, Default, Compatibility, Description)    \6471  LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));6472#include "clang/Basic/LangOptions.def"6473#define SANITIZER(NAME, ID)                                                    \6474  LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);6475#include "clang/Basic/Sanitizers.def"6476 6477  for (unsigned N = Record[Idx++]; N; --N)6478    LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));6479 6480  ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];6481  VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);6482  LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);6483 6484  LangOpts.CurrentModule = ReadString(Record, Idx);6485 6486  // Comment options.6487  for (unsigned N = Record[Idx++]; N; --N) {6488    LangOpts.CommentOpts.BlockCommandNames.push_back(6489      ReadString(Record, Idx));6490  }6491  LangOpts.CommentOpts.ParseAllComments = Record[Idx++];6492 6493  // OpenMP offloading options.6494  for (unsigned N = Record[Idx++]; N; --N) {6495    LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));6496  }6497 6498  LangOpts.OMPHostIRFile = ReadString(Record, Idx);6499 6500  return Listener.ReadLanguageOptions(LangOpts, ModuleFilename, Complain,6501                                      AllowCompatibleDifferences);6502}6503 6504bool ASTReader::ParseCodeGenOptions(const RecordData &Record,6505                                    StringRef ModuleFilename, bool Complain,6506                                    ASTReaderListener &Listener,6507                                    bool AllowCompatibleDifferences) {6508  unsigned Idx = 0;6509  CodeGenOptions CGOpts;6510  using CK = CodeGenOptions::CompatibilityKind;6511#define CODEGENOPT(Name, Bits, Default, Compatibility)                         \6512  if constexpr (CK::Compatibility != CK::Benign)                               \6513    CGOpts.Name = static_cast<unsigned>(Record[Idx++]);6514#define ENUM_CODEGENOPT(Name, Type, Bits, Default, Compatibility)              \6515  if constexpr (CK::Compatibility != CK::Benign)                               \6516    CGOpts.set##Name(static_cast<clang::CodeGenOptions::Type>(Record[Idx++]));6517#define DEBUGOPT(Name, Bits, Default, Compatibility)6518#define VALUE_DEBUGOPT(Name, Bits, Default, Compatibility)6519#define ENUM_DEBUGOPT(Name, Type, Bits, Default, Compatibility)6520#include "clang/Basic/CodeGenOptions.def"6521 6522  return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain,6523                                     AllowCompatibleDifferences);6524}6525 6526bool ASTReader::ParseTargetOptions(const RecordData &Record,6527                                   StringRef ModuleFilename, bool Complain,6528                                   ASTReaderListener &Listener,6529                                   bool AllowCompatibleDifferences) {6530  unsigned Idx = 0;6531  TargetOptions TargetOpts;6532  TargetOpts.Triple = ReadString(Record, Idx);6533  TargetOpts.CPU = ReadString(Record, Idx);6534  TargetOpts.TuneCPU = ReadString(Record, Idx);6535  TargetOpts.ABI = ReadString(Record, Idx);6536  for (unsigned N = Record[Idx++]; N; --N) {6537    TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));6538  }6539  for (unsigned N = Record[Idx++]; N; --N) {6540    TargetOpts.Features.push_back(ReadString(Record, Idx));6541  }6542 6543  return Listener.ReadTargetOptions(TargetOpts, ModuleFilename, Complain,6544                                    AllowCompatibleDifferences);6545}6546 6547bool ASTReader::ParseDiagnosticOptions(const RecordData &Record,6548                                       StringRef ModuleFilename, bool Complain,6549                                       ASTReaderListener &Listener) {6550  DiagnosticOptions DiagOpts;6551  unsigned Idx = 0;6552#define DIAGOPT(Name, Bits, Default) DiagOpts.Name = Record[Idx++];6553#define ENUM_DIAGOPT(Name, Type, Bits, Default)                                \6554  DiagOpts.set##Name(static_cast<Type>(Record[Idx++]));6555#include "clang/Basic/DiagnosticOptions.def"6556 6557  for (unsigned N = Record[Idx++]; N; --N)6558    DiagOpts.Warnings.push_back(ReadString(Record, Idx));6559  for (unsigned N = Record[Idx++]; N; --N)6560    DiagOpts.Remarks.push_back(ReadString(Record, Idx));6561 6562  return Listener.ReadDiagnosticOptions(DiagOpts, ModuleFilename, Complain);6563}6564 6565bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,6566                                       ASTReaderListener &Listener) {6567  FileSystemOptions FSOpts;6568  unsigned Idx = 0;6569  FSOpts.WorkingDir = ReadString(Record, Idx);6570  return Listener.ReadFileSystemOptions(FSOpts, Complain);6571}6572 6573bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,6574                                         StringRef ModuleFilename,6575                                         bool Complain,6576                                         ASTReaderListener &Listener) {6577  HeaderSearchOptions HSOpts;6578  unsigned Idx = 0;6579  HSOpts.Sysroot = ReadString(Record, Idx);6580 6581  HSOpts.ResourceDir = ReadString(Record, Idx);6582  HSOpts.ModuleCachePath = ReadString(Record, Idx);6583  HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);6584  HSOpts.DisableModuleHash = Record[Idx++];6585  HSOpts.ImplicitModuleMaps = Record[Idx++];6586  HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];6587  HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];6588  HSOpts.UseBuiltinIncludes = Record[Idx++];6589  HSOpts.UseStandardSystemIncludes = Record[Idx++];6590  HSOpts.UseStandardCXXIncludes = Record[Idx++];6591  HSOpts.UseLibcxx = Record[Idx++];6592  std::string SpecificModuleCachePath = ReadString(Record, Idx);6593 6594  return Listener.ReadHeaderSearchOptions(HSOpts, ModuleFilename,6595                                          SpecificModuleCachePath, Complain);6596}6597 6598bool ASTReader::ParseHeaderSearchPaths(const RecordData &Record, bool Complain,6599                                       ASTReaderListener &Listener) {6600  HeaderSearchOptions HSOpts;6601  unsigned Idx = 0;6602 6603  // Include entries.6604  for (unsigned N = Record[Idx++]; N; --N) {6605    std::string Path = ReadString(Record, Idx);6606    frontend::IncludeDirGroup Group6607      = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);6608    bool IsFramework = Record[Idx++];6609    bool IgnoreSysRoot = Record[Idx++];6610    HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,6611                                    IgnoreSysRoot);6612  }6613 6614  // System header prefixes.6615  for (unsigned N = Record[Idx++]; N; --N) {6616    std::string Prefix = ReadString(Record, Idx);6617    bool IsSystemHeader = Record[Idx++];6618    HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);6619  }6620 6621  // VFS overlay files.6622  for (unsigned N = Record[Idx++]; N; --N) {6623    std::string VFSOverlayFile = ReadString(Record, Idx);6624    HSOpts.VFSOverlayFiles.emplace_back(std::move(VFSOverlayFile));6625  }6626 6627  return Listener.ReadHeaderSearchPaths(HSOpts, Complain);6628}6629 6630bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,6631                                         StringRef ModuleFilename,6632                                         bool Complain,6633                                         ASTReaderListener &Listener,6634                                         std::string &SuggestedPredefines) {6635  PreprocessorOptions PPOpts;6636  unsigned Idx = 0;6637 6638  // Macro definitions/undefs6639  bool ReadMacros = Record[Idx++];6640  if (ReadMacros) {6641    for (unsigned N = Record[Idx++]; N; --N) {6642      std::string Macro = ReadString(Record, Idx);6643      bool IsUndef = Record[Idx++];6644      PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));6645    }6646  }6647 6648  // Includes6649  for (unsigned N = Record[Idx++]; N; --N) {6650    PPOpts.Includes.push_back(ReadString(Record, Idx));6651  }6652 6653  // Macro Includes6654  for (unsigned N = Record[Idx++]; N; --N) {6655    PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));6656  }6657 6658  PPOpts.UsePredefines = Record[Idx++];6659  PPOpts.DetailedRecord = Record[Idx++];6660  PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);6661  PPOpts.ObjCXXARCStandardLibrary =6662    static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);6663  SuggestedPredefines.clear();6664  return Listener.ReadPreprocessorOptions(PPOpts, ModuleFilename, ReadMacros,6665                                          Complain, SuggestedPredefines);6666}6667 6668std::pair<ModuleFile *, unsigned>6669ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {6670  GlobalPreprocessedEntityMapType::iterator6671  I = GlobalPreprocessedEntityMap.find(GlobalIndex);6672  assert(I != GlobalPreprocessedEntityMap.end() &&6673         "Corrupted global preprocessed entity map");6674  ModuleFile *M = I->second;6675  unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;6676  return std::make_pair(M, LocalIndex);6677}6678 6679llvm::iterator_range<PreprocessingRecord::iterator>6680ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {6681  if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())6682    return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,6683                                             Mod.NumPreprocessedEntities);6684 6685  return llvm::make_range(PreprocessingRecord::iterator(),6686                          PreprocessingRecord::iterator());6687}6688 6689bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,6690                                        unsigned int ClientLoadCapabilities) {6691  return ClientLoadCapabilities & ARR_OutOfDate &&6692         !getModuleManager()6693              .getModuleCache()6694              .getInMemoryModuleCache()6695              .isPCMFinal(ModuleFileName);6696}6697 6698llvm::iterator_range<ASTReader::ModuleDeclIterator>6699ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {6700  return llvm::make_range(6701      ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),6702      ModuleDeclIterator(this, &Mod,6703                         Mod.FileSortedDecls + Mod.NumFileSortedDecls));6704}6705 6706SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {6707  auto I = GlobalSkippedRangeMap.find(GlobalIndex);6708  assert(I != GlobalSkippedRangeMap.end() &&6709    "Corrupted global skipped range map");6710  ModuleFile *M = I->second;6711  unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;6712  assert(LocalIndex < M->NumPreprocessedSkippedRanges);6713  PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];6714  SourceRange Range(ReadSourceLocation(*M, RawRange.getBegin()),6715                    ReadSourceLocation(*M, RawRange.getEnd()));6716  assert(Range.isValid());6717  return Range;6718}6719 6720unsigned6721ASTReader::translatePreprocessedEntityIDToIndex(PreprocessedEntityID ID) const {6722  unsigned ModuleFileIndex = ID >> 32;6723  assert(ModuleFileIndex && "not translating loaded MacroID?");6724  assert(getModuleManager().size() > ModuleFileIndex - 1);6725  ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];6726 6727  ID &= llvm::maskTrailingOnes<PreprocessedEntityID>(32);6728  return MF.BasePreprocessedEntityID + ID;6729}6730 6731PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {6732  std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);6733  ModuleFile &M = *PPInfo.first;6734  unsigned LocalIndex = PPInfo.second;6735  PreprocessedEntityID PPID =6736      (static_cast<PreprocessedEntityID>(M.Index + 1) << 32) | LocalIndex;6737  const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];6738 6739  if (!PP.getPreprocessingRecord()) {6740    Error("no preprocessing record");6741    return nullptr;6742  }6743 6744  SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);6745  if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(6746          M.MacroOffsetsBase + PPOffs.getOffset())) {6747    Error(std::move(Err));6748    return nullptr;6749  }6750 6751  Expected<llvm::BitstreamEntry> MaybeEntry =6752      M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);6753  if (!MaybeEntry) {6754    Error(MaybeEntry.takeError());6755    return nullptr;6756  }6757  llvm::BitstreamEntry Entry = MaybeEntry.get();6758 6759  if (Entry.Kind != llvm::BitstreamEntry::Record)6760    return nullptr;6761 6762  // Read the record.6763  SourceRange Range(ReadSourceLocation(M, PPOffs.getBegin()),6764                    ReadSourceLocation(M, PPOffs.getEnd()));6765  PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();6766  StringRef Blob;6767  RecordData Record;6768  Expected<unsigned> MaybeRecType =6769      M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);6770  if (!MaybeRecType) {6771    Error(MaybeRecType.takeError());6772    return nullptr;6773  }6774  switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {6775  case PPD_MACRO_EXPANSION: {6776    bool isBuiltin = Record[0];6777    IdentifierInfo *Name = nullptr;6778    MacroDefinitionRecord *Def = nullptr;6779    if (isBuiltin)6780      Name = getLocalIdentifier(M, Record[1]);6781    else {6782      PreprocessedEntityID GlobalID =6783          getGlobalPreprocessedEntityID(M, Record[1]);6784      unsigned Index = translatePreprocessedEntityIDToIndex(GlobalID);6785      Def =6786          cast<MacroDefinitionRecord>(PPRec.getLoadedPreprocessedEntity(Index));6787    }6788 6789    MacroExpansion *ME;6790    if (isBuiltin)6791      ME = new (PPRec) MacroExpansion(Name, Range);6792    else6793      ME = new (PPRec) MacroExpansion(Def, Range);6794 6795    return ME;6796  }6797 6798  case PPD_MACRO_DEFINITION: {6799    // Decode the identifier info and then check again; if the macro is6800    // still defined and associated with the identifier,6801    IdentifierInfo *II = getLocalIdentifier(M, Record[0]);6802    MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);6803 6804    if (DeserializationListener)6805      DeserializationListener->MacroDefinitionRead(PPID, MD);6806 6807    return MD;6808  }6809 6810  case PPD_INCLUSION_DIRECTIVE: {6811    const char *FullFileNameStart = Blob.data() + Record[0];6812    StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);6813    OptionalFileEntryRef File;6814    if (!FullFileName.empty())6815      File = PP.getFileManager().getOptionalFileRef(FullFileName);6816 6817    // FIXME: Stable encoding6818    InclusionDirective::InclusionKind Kind6819      = static_cast<InclusionDirective::InclusionKind>(Record[2]);6820    InclusionDirective *ID6821      = new (PPRec) InclusionDirective(PPRec, Kind,6822                                       StringRef(Blob.data(), Record[0]),6823                                       Record[1], Record[3],6824                                       File,6825                                       Range);6826    return ID;6827  }6828  }6829 6830  llvm_unreachable("Invalid PreprocessorDetailRecordTypes");6831}6832 6833/// Find the next module that contains entities and return the ID6834/// of the first entry.6835///6836/// \param SLocMapI points at a chunk of a module that contains no6837/// preprocessed entities or the entities it contains are not the ones we are6838/// looking for.6839unsigned ASTReader::findNextPreprocessedEntity(6840    GlobalSLocOffsetMapType::const_iterator SLocMapI) const {6841  ++SLocMapI;6842  for (GlobalSLocOffsetMapType::const_iterator6843         EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {6844    ModuleFile &M = *SLocMapI->second;6845    if (M.NumPreprocessedEntities)6846      return M.BasePreprocessedEntityID;6847  }6848 6849  return getTotalNumPreprocessedEntities();6850}6851 6852namespace {6853 6854struct PPEntityComp {6855  const ASTReader &Reader;6856  ModuleFile &M;6857 6858  PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}6859 6860  bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {6861    SourceLocation LHS = getLoc(L);6862    SourceLocation RHS = getLoc(R);6863    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);6864  }6865 6866  bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {6867    SourceLocation LHS = getLoc(L);6868    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);6869  }6870 6871  bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {6872    SourceLocation RHS = getLoc(R);6873    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);6874  }6875 6876  SourceLocation getLoc(const PPEntityOffset &PPE) const {6877    return Reader.ReadSourceLocation(M, PPE.getBegin());6878  }6879};6880 6881} // namespace6882 6883unsigned ASTReader::findPreprocessedEntity(SourceLocation Loc,6884                                           bool EndsAfter) const {6885  if (SourceMgr.isLocalSourceLocation(Loc))6886    return getTotalNumPreprocessedEntities();6887 6888  GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(6889      SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);6890  assert(SLocMapI != GlobalSLocOffsetMap.end() &&6891         "Corrupted global sloc offset map");6892 6893  if (SLocMapI->second->NumPreprocessedEntities == 0)6894    return findNextPreprocessedEntity(SLocMapI);6895 6896  ModuleFile &M = *SLocMapI->second;6897 6898  using pp_iterator = const PPEntityOffset *;6899 6900  pp_iterator pp_begin = M.PreprocessedEntityOffsets;6901  pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;6902 6903  size_t Count = M.NumPreprocessedEntities;6904  size_t Half;6905  pp_iterator First = pp_begin;6906  pp_iterator PPI;6907 6908  if (EndsAfter) {6909    PPI = std::upper_bound(pp_begin, pp_end, Loc,6910                           PPEntityComp(*this, M));6911  } else {6912    // Do a binary search manually instead of using std::lower_bound because6913    // The end locations of entities may be unordered (when a macro expansion6914    // is inside another macro argument), but for this case it is not important6915    // whether we get the first macro expansion or its containing macro.6916    while (Count > 0) {6917      Half = Count / 2;6918      PPI = First;6919      std::advance(PPI, Half);6920      if (SourceMgr.isBeforeInTranslationUnit(6921              ReadSourceLocation(M, PPI->getEnd()), Loc)) {6922        First = PPI;6923        ++First;6924        Count = Count - Half - 1;6925      } else6926        Count = Half;6927    }6928  }6929 6930  if (PPI == pp_end)6931    return findNextPreprocessedEntity(SLocMapI);6932 6933  return M.BasePreprocessedEntityID + (PPI - pp_begin);6934}6935 6936/// Returns a pair of [Begin, End) indices of preallocated6937/// preprocessed entities that \arg Range encompasses.6938std::pair<unsigned, unsigned>6939    ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {6940  if (Range.isInvalid())6941    return std::make_pair(0,0);6942  assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));6943 6944  unsigned BeginID = findPreprocessedEntity(Range.getBegin(), false);6945  unsigned EndID = findPreprocessedEntity(Range.getEnd(), true);6946  return std::make_pair(BeginID, EndID);6947}6948 6949/// Optionally returns true or false if the preallocated preprocessed6950/// entity with index \arg Index came from file \arg FID.6951std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,6952                                                            FileID FID) {6953  if (FID.isInvalid())6954    return false;6955 6956  std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);6957  ModuleFile &M = *PPInfo.first;6958  unsigned LocalIndex = PPInfo.second;6959  const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];6960 6961  SourceLocation Loc = ReadSourceLocation(M, PPOffs.getBegin());6962  if (Loc.isInvalid())6963    return false;6964 6965  if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))6966    return true;6967  else6968    return false;6969}6970 6971namespace {6972 6973  /// Visitor used to search for information about a header file.6974  class HeaderFileInfoVisitor {6975  FileEntryRef FE;6976    std::optional<HeaderFileInfo> HFI;6977 6978  public:6979    explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {}6980 6981    bool operator()(ModuleFile &M) {6982      HeaderFileInfoLookupTable *Table6983        = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);6984      if (!Table)6985        return false;6986 6987      // Look in the on-disk hash table for an entry for this file name.6988      HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);6989      if (Pos == Table->end())6990        return false;6991 6992      HFI = *Pos;6993      return true;6994    }6995 6996    std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }6997  };6998 6999} // namespace7000 7001HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) {7002  HeaderFileInfoVisitor Visitor(FE);7003  ModuleMgr.visit(Visitor);7004  if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())7005      return *HFI;7006 7007  return HeaderFileInfo();7008}7009 7010void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {7011  using DiagState = DiagnosticsEngine::DiagState;7012  SmallVector<DiagState *, 32> DiagStates;7013 7014  for (ModuleFile &F : ModuleMgr) {7015    unsigned Idx = 0;7016    auto &Record = F.PragmaDiagMappings;7017    if (Record.empty())7018      continue;7019 7020    DiagStates.clear();7021 7022    auto ReadDiagState = [&](const DiagState &BasedOn,7023                             bool IncludeNonPragmaStates) {7024      unsigned BackrefID = Record[Idx++];7025      if (BackrefID != 0)7026        return DiagStates[BackrefID - 1];7027 7028      // A new DiagState was created here.7029      Diag.DiagStates.push_back(BasedOn);7030      DiagState *NewState = &Diag.DiagStates.back();7031      DiagStates.push_back(NewState);7032      unsigned Size = Record[Idx++];7033      assert(Idx + Size * 2 <= Record.size() &&7034             "Invalid data, not enough diag/map pairs");7035      while (Size--) {7036        unsigned DiagID = Record[Idx++];7037        DiagnosticMapping NewMapping =7038            DiagnosticMapping::deserialize(Record[Idx++]);7039        if (!NewMapping.isPragma() && !IncludeNonPragmaStates)7040          continue;7041 7042        DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);7043 7044        // If this mapping was specified as a warning but the severity was7045        // upgraded due to diagnostic settings, simulate the current diagnostic7046        // settings (and use a warning).7047        if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {7048          NewMapping.setSeverity(diag::Severity::Warning);7049          NewMapping.setUpgradedFromWarning(false);7050        }7051 7052        Mapping = NewMapping;7053      }7054      return NewState;7055    };7056 7057    // Read the first state.7058    DiagState *FirstState;7059    if (F.Kind == MK_ImplicitModule) {7060      // Implicitly-built modules are reused with different diagnostic7061      // settings.  Use the initial diagnostic state from Diag to simulate this7062      // compilation's diagnostic settings.7063      FirstState = Diag.DiagStatesByLoc.FirstDiagState;7064      DiagStates.push_back(FirstState);7065 7066      // Skip the initial diagnostic state from the serialized module.7067      assert(Record[1] == 0 &&7068             "Invalid data, unexpected backref in initial state");7069      Idx = 3 + Record[2] * 2;7070      assert(Idx < Record.size() &&7071             "Invalid data, not enough state change pairs in initial state");7072    } else if (F.isModule()) {7073      // For an explicit module, preserve the flags from the module build7074      // command line (-w, -Weverything, -Werror, ...) along with any explicit7075      // -Wblah flags.7076      unsigned Flags = Record[Idx++];7077      DiagState Initial(*Diag.getDiagnosticIDs());7078      Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;7079      Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;7080      Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;7081      Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;7082      Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;7083      Initial.ExtBehavior = (diag::Severity)Flags;7084      FirstState = ReadDiagState(Initial, true);7085 7086      assert(F.OriginalSourceFileID.isValid());7087 7088      // Set up the root buffer of the module to start with the initial7089      // diagnostic state of the module itself, to cover files that contain no7090      // explicit transitions (for which we did not serialize anything).7091      Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]7092          .StateTransitions.push_back({FirstState, 0});7093    } else {7094      // For prefix ASTs, start with whatever the user configured on the7095      // command line.7096      Idx++; // Skip flags.7097      FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState, false);7098    }7099 7100    // Read the state transitions.7101    unsigned NumLocations = Record[Idx++];7102    while (NumLocations--) {7103      assert(Idx < Record.size() &&7104             "Invalid data, missing pragma diagnostic states");7105      FileID FID = ReadFileID(F, Record, Idx);7106      assert(FID.isValid() && "invalid FileID for transition");7107      unsigned Transitions = Record[Idx++];7108 7109      // Note that we don't need to set up Parent/ParentOffset here, because7110      // we won't be changing the diagnostic state within imported FileIDs7111      // (other than perhaps appending to the main source file, which has no7112      // parent).7113      auto &F = Diag.DiagStatesByLoc.Files[FID];7114      F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);7115      for (unsigned I = 0; I != Transitions; ++I) {7116        unsigned Offset = Record[Idx++];7117        auto *State = ReadDiagState(*FirstState, false);7118        F.StateTransitions.push_back({State, Offset});7119      }7120    }7121 7122    // Read the final state.7123    assert(Idx < Record.size() &&7124           "Invalid data, missing final pragma diagnostic state");7125    SourceLocation CurStateLoc = ReadSourceLocation(F, Record[Idx++]);7126    auto *CurState = ReadDiagState(*FirstState, false);7127 7128    if (!F.isModule()) {7129      Diag.DiagStatesByLoc.CurDiagState = CurState;7130      Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;7131 7132      // Preserve the property that the imaginary root file describes the7133      // current state.7134      FileID NullFile;7135      auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;7136      if (T.empty())7137        T.push_back({CurState, 0});7138      else7139        T[0].State = CurState;7140    }7141 7142    // Don't try to read these mappings again.7143    Record.clear();7144  }7145}7146 7147/// Get the correct cursor and offset for loading a type.7148ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {7149  auto [M, Index] = translateTypeIDToIndex(ID);7150  return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +7151                               M->DeclsBlockStartOffset);7152}7153 7154static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {7155  switch (code) {7156#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \7157  case TYPE_##CODE_ID: return Type::CLASS_ID;7158#include "clang/Serialization/TypeBitCodes.def"7159  default:7160    return std::nullopt;7161  }7162}7163 7164/// Read and return the type with the given index..7165///7166/// The index is the type ID, shifted and minus the number of predefs. This7167/// routine actually reads the record corresponding to the type at the given7168/// location. It is a helper routine for GetType, which deals with reading type7169/// IDs.7170QualType ASTReader::readTypeRecord(TypeID ID) {7171  assert(ContextObj && "reading type with no AST context");7172  ASTContext &Context = *ContextObj;7173  RecordLocation Loc = TypeCursorForIndex(ID);7174  BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;7175 7176  // Keep track of where we are in the stream, then jump back there7177  // after reading this type.7178  SavedStreamPosition SavedPosition(DeclsCursor);7179 7180  ReadingKindTracker ReadingKind(Read_Type, *this);7181 7182  // Note that we are loading a type record.7183  Deserializing AType(this);7184 7185  if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {7186    Error(std::move(Err));7187    return QualType();7188  }7189  Expected<unsigned> RawCode = DeclsCursor.ReadCode();7190  if (!RawCode) {7191    Error(RawCode.takeError());7192    return QualType();7193  }7194 7195  ASTRecordReader Record(*this, *Loc.F);7196  Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());7197  if (!Code) {7198    Error(Code.takeError());7199    return QualType();7200  }7201  if (Code.get() == TYPE_EXT_QUAL) {7202    QualType baseType = Record.readQualType();7203    Qualifiers quals = Record.readQualifiers();7204    return Context.getQualifiedType(baseType, quals);7205  }7206 7207  auto maybeClass = getTypeClassForCode((TypeCode) Code.get());7208  if (!maybeClass) {7209    Error("Unexpected code for type");7210    return QualType();7211  }7212 7213  serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);7214  return TypeReader.read(*maybeClass);7215}7216 7217namespace clang {7218 7219class TypeLocReader : public TypeLocVisitor<TypeLocReader> {7220  ASTRecordReader &Reader;7221 7222  SourceLocation readSourceLocation() { return Reader.readSourceLocation(); }7223  SourceRange readSourceRange() { return Reader.readSourceRange(); }7224 7225  TypeSourceInfo *GetTypeSourceInfo() {7226    return Reader.readTypeSourceInfo();7227  }7228 7229  NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {7230    return Reader.readNestedNameSpecifierLoc();7231  }7232 7233  Attr *ReadAttr() {7234    return Reader.readAttr();7235  }7236 7237public:7238  TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}7239 7240  // We want compile-time assurance that we've enumerated all of7241  // these, so unfortunately we have to declare them first, then7242  // define them out-of-line.7243#define ABSTRACT_TYPELOC(CLASS, PARENT)7244#define TYPELOC(CLASS, PARENT) \7245  void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);7246#include "clang/AST/TypeLocNodes.def"7247 7248  void VisitFunctionTypeLoc(FunctionTypeLoc);7249  void VisitArrayTypeLoc(ArrayTypeLoc);7250  void VisitTagTypeLoc(TagTypeLoc TL);7251};7252 7253} // namespace clang7254 7255void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {7256  // nothing to do7257}7258 7259void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {7260  TL.setBuiltinLoc(readSourceLocation());7261  if (TL.needsExtraLocalData()) {7262    TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));7263    TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));7264    TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));7265    TL.setModeAttr(Reader.readInt());7266  }7267}7268 7269void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {7270  TL.setNameLoc(readSourceLocation());7271}7272 7273void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {7274  TL.setStarLoc(readSourceLocation());7275}7276 7277void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {7278  // nothing to do7279}7280 7281void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {7282  // nothing to do7283}7284 7285void TypeLocReader::VisitArrayParameterTypeLoc(ArrayParameterTypeLoc TL) {7286  // nothing to do7287}7288 7289void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {7290  TL.setExpansionLoc(readSourceLocation());7291}7292 7293void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {7294  TL.setCaretLoc(readSourceLocation());7295}7296 7297void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {7298  TL.setAmpLoc(readSourceLocation());7299}7300 7301void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {7302  TL.setAmpAmpLoc(readSourceLocation());7303}7304 7305void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {7306  TL.setStarLoc(readSourceLocation());7307  TL.setQualifierLoc(ReadNestedNameSpecifierLoc());7308}7309 7310void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {7311  TL.setLBracketLoc(readSourceLocation());7312  TL.setRBracketLoc(readSourceLocation());7313  if (Reader.readBool())7314    TL.setSizeExpr(Reader.readExpr());7315  else7316    TL.setSizeExpr(nullptr);7317}7318 7319void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {7320  VisitArrayTypeLoc(TL);7321}7322 7323void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {7324  VisitArrayTypeLoc(TL);7325}7326 7327void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {7328  VisitArrayTypeLoc(TL);7329}7330 7331void TypeLocReader::VisitDependentSizedArrayTypeLoc(7332                                            DependentSizedArrayTypeLoc TL) {7333  VisitArrayTypeLoc(TL);7334}7335 7336void TypeLocReader::VisitDependentAddressSpaceTypeLoc(7337    DependentAddressSpaceTypeLoc TL) {7338 7339    TL.setAttrNameLoc(readSourceLocation());7340    TL.setAttrOperandParensRange(readSourceRange());7341    TL.setAttrExprOperand(Reader.readExpr());7342}7343 7344void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(7345                                        DependentSizedExtVectorTypeLoc TL) {7346  TL.setNameLoc(readSourceLocation());7347}7348 7349void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {7350  TL.setNameLoc(readSourceLocation());7351}7352 7353void TypeLocReader::VisitDependentVectorTypeLoc(7354    DependentVectorTypeLoc TL) {7355  TL.setNameLoc(readSourceLocation());7356}7357 7358void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {7359  TL.setNameLoc(readSourceLocation());7360}7361 7362void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {7363  TL.setAttrNameLoc(readSourceLocation());7364  TL.setAttrOperandParensRange(readSourceRange());7365  TL.setAttrRowOperand(Reader.readExpr());7366  TL.setAttrColumnOperand(Reader.readExpr());7367}7368 7369void TypeLocReader::VisitDependentSizedMatrixTypeLoc(7370    DependentSizedMatrixTypeLoc TL) {7371  TL.setAttrNameLoc(readSourceLocation());7372  TL.setAttrOperandParensRange(readSourceRange());7373  TL.setAttrRowOperand(Reader.readExpr());7374  TL.setAttrColumnOperand(Reader.readExpr());7375}7376 7377void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {7378  TL.setLocalRangeBegin(readSourceLocation());7379  TL.setLParenLoc(readSourceLocation());7380  TL.setRParenLoc(readSourceLocation());7381  TL.setExceptionSpecRange(readSourceRange());7382  TL.setLocalRangeEnd(readSourceLocation());7383  for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {7384    TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());7385  }7386}7387 7388void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {7389  VisitFunctionTypeLoc(TL);7390}7391 7392void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {7393  VisitFunctionTypeLoc(TL);7394}7395 7396void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {7397  SourceLocation ElaboratedKeywordLoc = readSourceLocation();7398  NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();7399  SourceLocation NameLoc = readSourceLocation();7400  TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);7401}7402 7403void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {7404  SourceLocation ElaboratedKeywordLoc = readSourceLocation();7405  NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();7406  SourceLocation NameLoc = readSourceLocation();7407  TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);7408}7409 7410void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {7411  SourceLocation ElaboratedKeywordLoc = readSourceLocation();7412  NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();7413  SourceLocation NameLoc = readSourceLocation();7414  TL.set(ElaboratedKeywordLoc, QualifierLoc, NameLoc);7415}7416 7417void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {7418  TL.setTypeofLoc(readSourceLocation());7419  TL.setLParenLoc(readSourceLocation());7420  TL.setRParenLoc(readSourceLocation());7421}7422 7423void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {7424  TL.setTypeofLoc(readSourceLocation());7425  TL.setLParenLoc(readSourceLocation());7426  TL.setRParenLoc(readSourceLocation());7427  TL.setUnmodifiedTInfo(GetTypeSourceInfo());7428}7429 7430void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {7431  TL.setDecltypeLoc(readSourceLocation());7432  TL.setRParenLoc(readSourceLocation());7433}7434 7435void TypeLocReader::VisitPackIndexingTypeLoc(PackIndexingTypeLoc TL) {7436  TL.setEllipsisLoc(readSourceLocation());7437}7438 7439void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {7440  TL.setKWLoc(readSourceLocation());7441  TL.setLParenLoc(readSourceLocation());7442  TL.setRParenLoc(readSourceLocation());7443  TL.setUnderlyingTInfo(GetTypeSourceInfo());7444}7445 7446ConceptReference *ASTRecordReader::readConceptReference() {7447  auto NNS = readNestedNameSpecifierLoc();7448  auto TemplateKWLoc = readSourceLocation();7449  auto ConceptNameLoc = readDeclarationNameInfo();7450  auto FoundDecl = readDeclAs<NamedDecl>();7451  auto NamedConcept = readDeclAs<ConceptDecl>();7452  auto *CR = ConceptReference::Create(7453      getContext(), NNS, TemplateKWLoc, ConceptNameLoc, FoundDecl, NamedConcept,7454      (readBool() ? readASTTemplateArgumentListInfo() : nullptr));7455  return CR;7456}7457 7458void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {7459  TL.setNameLoc(readSourceLocation());7460  if (Reader.readBool())7461    TL.setConceptReference(Reader.readConceptReference());7462  if (Reader.readBool())7463    TL.setRParenLoc(readSourceLocation());7464}7465 7466void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(7467    DeducedTemplateSpecializationTypeLoc TL) {7468  TL.setElaboratedKeywordLoc(readSourceLocation());7469  TL.setQualifierLoc(ReadNestedNameSpecifierLoc());7470  TL.setTemplateNameLoc(readSourceLocation());7471}7472 7473void TypeLocReader::VisitTagTypeLoc(TagTypeLoc TL) {7474  TL.setElaboratedKeywordLoc(readSourceLocation());7475  TL.setQualifierLoc(ReadNestedNameSpecifierLoc());7476  TL.setNameLoc(readSourceLocation());7477}7478 7479void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {7480  VisitTagTypeLoc(TL);7481}7482 7483void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {7484  VisitTagTypeLoc(TL);7485}7486 7487void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) { VisitTagTypeLoc(TL); }7488 7489void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {7490  TL.setAttr(ReadAttr());7491}7492 7493void TypeLocReader::VisitCountAttributedTypeLoc(CountAttributedTypeLoc TL) {7494  // Nothing to do7495}7496 7497void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {7498  // Nothing to do.7499}7500 7501void TypeLocReader::VisitHLSLAttributedResourceTypeLoc(7502    HLSLAttributedResourceTypeLoc TL) {7503  // Nothing to do.7504}7505 7506void TypeLocReader::VisitHLSLInlineSpirvTypeLoc(HLSLInlineSpirvTypeLoc TL) {7507  // Nothing to do.7508}7509 7510void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {7511  TL.setNameLoc(readSourceLocation());7512}7513 7514void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(7515                                            SubstTemplateTypeParmTypeLoc TL) {7516  TL.setNameLoc(readSourceLocation());7517}7518 7519void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(7520                                          SubstTemplateTypeParmPackTypeLoc TL) {7521  TL.setNameLoc(readSourceLocation());7522}7523 7524void TypeLocReader::VisitSubstBuiltinTemplatePackTypeLoc(7525    SubstBuiltinTemplatePackTypeLoc TL) {7526  TL.setNameLoc(readSourceLocation());7527}7528 7529void TypeLocReader::VisitTemplateSpecializationTypeLoc(7530                                           TemplateSpecializationTypeLoc TL) {7531  SourceLocation ElaboratedKeywordLoc = readSourceLocation();7532  NestedNameSpecifierLoc QualifierLoc = ReadNestedNameSpecifierLoc();7533  SourceLocation TemplateKeywordLoc = readSourceLocation();7534  SourceLocation NameLoc = readSourceLocation();7535  SourceLocation LAngleLoc = readSourceLocation();7536  SourceLocation RAngleLoc = readSourceLocation();7537  TL.set(ElaboratedKeywordLoc, QualifierLoc, TemplateKeywordLoc, NameLoc,7538         LAngleLoc, RAngleLoc);7539  MutableArrayRef<TemplateArgumentLocInfo> Args = TL.getArgLocInfos();7540  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)7541    Args[I] = Reader.readTemplateArgumentLocInfo(7542        TL.getTypePtr()->template_arguments()[I].getKind());7543}7544 7545void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {7546  TL.setLParenLoc(readSourceLocation());7547  TL.setRParenLoc(readSourceLocation());7548}7549 7550void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {7551  TL.setElaboratedKeywordLoc(readSourceLocation());7552  TL.setQualifierLoc(ReadNestedNameSpecifierLoc());7553  TL.setNameLoc(readSourceLocation());7554}7555 7556void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {7557  TL.setEllipsisLoc(readSourceLocation());7558}7559 7560void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {7561  TL.setNameLoc(readSourceLocation());7562  TL.setNameEndLoc(readSourceLocation());7563}7564 7565void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {7566  if (TL.getNumProtocols()) {7567    TL.setProtocolLAngleLoc(readSourceLocation());7568    TL.setProtocolRAngleLoc(readSourceLocation());7569  }7570  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)7571    TL.setProtocolLoc(i, readSourceLocation());7572}7573 7574void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {7575  TL.setHasBaseTypeAsWritten(Reader.readBool());7576  TL.setTypeArgsLAngleLoc(readSourceLocation());7577  TL.setTypeArgsRAngleLoc(readSourceLocation());7578  for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)7579    TL.setTypeArgTInfo(i, GetTypeSourceInfo());7580  TL.setProtocolLAngleLoc(readSourceLocation());7581  TL.setProtocolRAngleLoc(readSourceLocation());7582  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)7583    TL.setProtocolLoc(i, readSourceLocation());7584}7585 7586void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {7587  TL.setStarLoc(readSourceLocation());7588}7589 7590void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {7591  TL.setKWLoc(readSourceLocation());7592  TL.setLParenLoc(readSourceLocation());7593  TL.setRParenLoc(readSourceLocation());7594}7595 7596void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {7597  TL.setKWLoc(readSourceLocation());7598}7599 7600void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {7601  TL.setNameLoc(readSourceLocation());7602}7603 7604void TypeLocReader::VisitDependentBitIntTypeLoc(7605    clang::DependentBitIntTypeLoc TL) {7606  TL.setNameLoc(readSourceLocation());7607}7608 7609void TypeLocReader::VisitPredefinedSugarTypeLoc(PredefinedSugarTypeLoc TL) {7610  // Nothing to do.7611}7612 7613void ASTRecordReader::readTypeLoc(TypeLoc TL) {7614  TypeLocReader TLR(*this);7615  for (; !TL.isNull(); TL = TL.getNextTypeLoc())7616    TLR.Visit(TL);7617}7618 7619TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {7620  QualType InfoTy = readType();7621  if (InfoTy.isNull())7622    return nullptr;7623 7624  TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);7625  readTypeLoc(TInfo->getTypeLoc());7626  return TInfo;7627}7628 7629static unsigned getIndexForTypeID(serialization::TypeID ID) {7630  return (ID & llvm::maskTrailingOnes<TypeID>(32)) >> Qualifiers::FastWidth;7631}7632 7633static unsigned getModuleFileIndexForTypeID(serialization::TypeID ID) {7634  return ID >> 32;7635}7636 7637static bool isPredefinedType(serialization::TypeID ID) {7638  // We don't need to erase the higher bits since if these bits are not 0,7639  // it must be larger than NUM_PREDEF_TYPE_IDS.7640  return (ID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS;7641}7642 7643std::pair<ModuleFile *, unsigned>7644ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {7645  assert(!isPredefinedType(ID) &&7646         "Predefined type shouldn't be in TypesLoaded");7647  unsigned ModuleFileIndex = getModuleFileIndexForTypeID(ID);7648  assert(ModuleFileIndex && "Untranslated Local Decl?");7649 7650  ModuleFile *OwningModuleFile = &getModuleManager()[ModuleFileIndex - 1];7651  assert(OwningModuleFile &&7652         "untranslated type ID or local type ID shouldn't be in TypesLoaded");7653 7654  return {OwningModuleFile,7655          OwningModuleFile->BaseTypeIndex + getIndexForTypeID(ID)};7656}7657 7658QualType ASTReader::GetType(TypeID ID) {7659  assert(ContextObj && "reading type with no AST context");7660  ASTContext &Context = *ContextObj;7661 7662  unsigned FastQuals = ID & Qualifiers::FastMask;7663 7664  if (isPredefinedType(ID)) {7665    QualType T;7666    unsigned Index = getIndexForTypeID(ID);7667    switch ((PredefinedTypeIDs)Index) {7668    case PREDEF_TYPE_LAST_ID:7669      // We should never use this one.7670      llvm_unreachable("Invalid predefined type");7671      break;7672    case PREDEF_TYPE_NULL_ID:7673      return QualType();7674    case PREDEF_TYPE_VOID_ID:7675      T = Context.VoidTy;7676      break;7677    case PREDEF_TYPE_BOOL_ID:7678      T = Context.BoolTy;7679      break;7680    case PREDEF_TYPE_CHAR_U_ID:7681    case PREDEF_TYPE_CHAR_S_ID:7682      // FIXME: Check that the signedness of CharTy is correct!7683      T = Context.CharTy;7684      break;7685    case PREDEF_TYPE_UCHAR_ID:7686      T = Context.UnsignedCharTy;7687      break;7688    case PREDEF_TYPE_USHORT_ID:7689      T = Context.UnsignedShortTy;7690      break;7691    case PREDEF_TYPE_UINT_ID:7692      T = Context.UnsignedIntTy;7693      break;7694    case PREDEF_TYPE_ULONG_ID:7695      T = Context.UnsignedLongTy;7696      break;7697    case PREDEF_TYPE_ULONGLONG_ID:7698      T = Context.UnsignedLongLongTy;7699      break;7700    case PREDEF_TYPE_UINT128_ID:7701      T = Context.UnsignedInt128Ty;7702      break;7703    case PREDEF_TYPE_SCHAR_ID:7704      T = Context.SignedCharTy;7705      break;7706    case PREDEF_TYPE_WCHAR_ID:7707      T = Context.WCharTy;7708      break;7709    case PREDEF_TYPE_SHORT_ID:7710      T = Context.ShortTy;7711      break;7712    case PREDEF_TYPE_INT_ID:7713      T = Context.IntTy;7714      break;7715    case PREDEF_TYPE_LONG_ID:7716      T = Context.LongTy;7717      break;7718    case PREDEF_TYPE_LONGLONG_ID:7719      T = Context.LongLongTy;7720      break;7721    case PREDEF_TYPE_INT128_ID:7722      T = Context.Int128Ty;7723      break;7724    case PREDEF_TYPE_BFLOAT16_ID:7725      T = Context.BFloat16Ty;7726      break;7727    case PREDEF_TYPE_HALF_ID:7728      T = Context.HalfTy;7729      break;7730    case PREDEF_TYPE_FLOAT_ID:7731      T = Context.FloatTy;7732      break;7733    case PREDEF_TYPE_DOUBLE_ID:7734      T = Context.DoubleTy;7735      break;7736    case PREDEF_TYPE_LONGDOUBLE_ID:7737      T = Context.LongDoubleTy;7738      break;7739    case PREDEF_TYPE_SHORT_ACCUM_ID:7740      T = Context.ShortAccumTy;7741      break;7742    case PREDEF_TYPE_ACCUM_ID:7743      T = Context.AccumTy;7744      break;7745    case PREDEF_TYPE_LONG_ACCUM_ID:7746      T = Context.LongAccumTy;7747      break;7748    case PREDEF_TYPE_USHORT_ACCUM_ID:7749      T = Context.UnsignedShortAccumTy;7750      break;7751    case PREDEF_TYPE_UACCUM_ID:7752      T = Context.UnsignedAccumTy;7753      break;7754    case PREDEF_TYPE_ULONG_ACCUM_ID:7755      T = Context.UnsignedLongAccumTy;7756      break;7757    case PREDEF_TYPE_SHORT_FRACT_ID:7758      T = Context.ShortFractTy;7759      break;7760    case PREDEF_TYPE_FRACT_ID:7761      T = Context.FractTy;7762      break;7763    case PREDEF_TYPE_LONG_FRACT_ID:7764      T = Context.LongFractTy;7765      break;7766    case PREDEF_TYPE_USHORT_FRACT_ID:7767      T = Context.UnsignedShortFractTy;7768      break;7769    case PREDEF_TYPE_UFRACT_ID:7770      T = Context.UnsignedFractTy;7771      break;7772    case PREDEF_TYPE_ULONG_FRACT_ID:7773      T = Context.UnsignedLongFractTy;7774      break;7775    case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:7776      T = Context.SatShortAccumTy;7777      break;7778    case PREDEF_TYPE_SAT_ACCUM_ID:7779      T = Context.SatAccumTy;7780      break;7781    case PREDEF_TYPE_SAT_LONG_ACCUM_ID:7782      T = Context.SatLongAccumTy;7783      break;7784    case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:7785      T = Context.SatUnsignedShortAccumTy;7786      break;7787    case PREDEF_TYPE_SAT_UACCUM_ID:7788      T = Context.SatUnsignedAccumTy;7789      break;7790    case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:7791      T = Context.SatUnsignedLongAccumTy;7792      break;7793    case PREDEF_TYPE_SAT_SHORT_FRACT_ID:7794      T = Context.SatShortFractTy;7795      break;7796    case PREDEF_TYPE_SAT_FRACT_ID:7797      T = Context.SatFractTy;7798      break;7799    case PREDEF_TYPE_SAT_LONG_FRACT_ID:7800      T = Context.SatLongFractTy;7801      break;7802    case PREDEF_TYPE_SAT_USHORT_FRACT_ID:7803      T = Context.SatUnsignedShortFractTy;7804      break;7805    case PREDEF_TYPE_SAT_UFRACT_ID:7806      T = Context.SatUnsignedFractTy;7807      break;7808    case PREDEF_TYPE_SAT_ULONG_FRACT_ID:7809      T = Context.SatUnsignedLongFractTy;7810      break;7811    case PREDEF_TYPE_FLOAT16_ID:7812      T = Context.Float16Ty;7813      break;7814    case PREDEF_TYPE_FLOAT128_ID:7815      T = Context.Float128Ty;7816      break;7817    case PREDEF_TYPE_IBM128_ID:7818      T = Context.Ibm128Ty;7819      break;7820    case PREDEF_TYPE_OVERLOAD_ID:7821      T = Context.OverloadTy;7822      break;7823    case PREDEF_TYPE_UNRESOLVED_TEMPLATE:7824      T = Context.UnresolvedTemplateTy;7825      break;7826    case PREDEF_TYPE_BOUND_MEMBER:7827      T = Context.BoundMemberTy;7828      break;7829    case PREDEF_TYPE_PSEUDO_OBJECT:7830      T = Context.PseudoObjectTy;7831      break;7832    case PREDEF_TYPE_DEPENDENT_ID:7833      T = Context.DependentTy;7834      break;7835    case PREDEF_TYPE_UNKNOWN_ANY:7836      T = Context.UnknownAnyTy;7837      break;7838    case PREDEF_TYPE_NULLPTR_ID:7839      T = Context.NullPtrTy;7840      break;7841    case PREDEF_TYPE_CHAR8_ID:7842      T = Context.Char8Ty;7843      break;7844    case PREDEF_TYPE_CHAR16_ID:7845      T = Context.Char16Ty;7846      break;7847    case PREDEF_TYPE_CHAR32_ID:7848      T = Context.Char32Ty;7849      break;7850    case PREDEF_TYPE_OBJC_ID:7851      T = Context.ObjCBuiltinIdTy;7852      break;7853    case PREDEF_TYPE_OBJC_CLASS:7854      T = Context.ObjCBuiltinClassTy;7855      break;7856    case PREDEF_TYPE_OBJC_SEL:7857      T = Context.ObjCBuiltinSelTy;7858      break;7859#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \7860    case PREDEF_TYPE_##Id##_ID: \7861      T = Context.SingletonId; \7862      break;7863#include "clang/Basic/OpenCLImageTypes.def"7864#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \7865    case PREDEF_TYPE_##Id##_ID: \7866      T = Context.Id##Ty; \7867      break;7868#include "clang/Basic/OpenCLExtensionTypes.def"7869    case PREDEF_TYPE_SAMPLER_ID:7870      T = Context.OCLSamplerTy;7871      break;7872    case PREDEF_TYPE_EVENT_ID:7873      T = Context.OCLEventTy;7874      break;7875    case PREDEF_TYPE_CLK_EVENT_ID:7876      T = Context.OCLClkEventTy;7877      break;7878    case PREDEF_TYPE_QUEUE_ID:7879      T = Context.OCLQueueTy;7880      break;7881    case PREDEF_TYPE_RESERVE_ID_ID:7882      T = Context.OCLReserveIDTy;7883      break;7884    case PREDEF_TYPE_AUTO_DEDUCT:7885      T = Context.getAutoDeductType();7886      break;7887    case PREDEF_TYPE_AUTO_RREF_DEDUCT:7888      T = Context.getAutoRRefDeductType();7889      break;7890    case PREDEF_TYPE_ARC_UNBRIDGED_CAST:7891      T = Context.ARCUnbridgedCastTy;7892      break;7893    case PREDEF_TYPE_BUILTIN_FN:7894      T = Context.BuiltinFnTy;7895      break;7896    case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:7897      T = Context.IncompleteMatrixIdxTy;7898      break;7899    case PREDEF_TYPE_ARRAY_SECTION:7900      T = Context.ArraySectionTy;7901      break;7902    case PREDEF_TYPE_OMP_ARRAY_SHAPING:7903      T = Context.OMPArrayShapingTy;7904      break;7905    case PREDEF_TYPE_OMP_ITERATOR:7906      T = Context.OMPIteratorTy;7907      break;7908#define SVE_TYPE(Name, Id, SingletonId) \7909    case PREDEF_TYPE_##Id##_ID: \7910      T = Context.SingletonId; \7911      break;7912#include "clang/Basic/AArch64ACLETypes.def"7913#define PPC_VECTOR_TYPE(Name, Id, Size) \7914    case PREDEF_TYPE_##Id##_ID: \7915      T = Context.Id##Ty; \7916      break;7917#include "clang/Basic/PPCTypes.def"7918#define RVV_TYPE(Name, Id, SingletonId) \7919    case PREDEF_TYPE_##Id##_ID: \7920      T = Context.SingletonId; \7921      break;7922#include "clang/Basic/RISCVVTypes.def"7923#define WASM_TYPE(Name, Id, SingletonId)                                       \7924  case PREDEF_TYPE_##Id##_ID:                                                  \7925    T = Context.SingletonId;                                                   \7926    break;7927#include "clang/Basic/WebAssemblyReferenceTypes.def"7928#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align)                       \7929  case PREDEF_TYPE_##Id##_ID:                                                  \7930    T = Context.SingletonId;                                                   \7931    break;7932#include "clang/Basic/AMDGPUTypes.def"7933#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId)                            \7934  case PREDEF_TYPE_##Id##_ID:                                                  \7935    T = Context.SingletonId;                                                   \7936    break;7937#include "clang/Basic/HLSLIntangibleTypes.def"7938    }7939 7940    assert(!T.isNull() && "Unknown predefined type");7941    return T.withFastQualifiers(FastQuals);7942  }7943 7944  unsigned Index = translateTypeIDToIndex(ID).second;7945 7946  assert(Index < TypesLoaded.size() && "Type index out-of-range");7947  if (TypesLoaded[Index].isNull()) {7948    TypesLoaded[Index] = readTypeRecord(ID);7949    if (TypesLoaded[Index].isNull())7950      return QualType();7951 7952    TypesLoaded[Index]->setFromAST();7953    if (DeserializationListener)7954      DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),7955                                        TypesLoaded[Index]);7956  }7957 7958  return TypesLoaded[Index].withFastQualifiers(FastQuals);7959}7960 7961QualType ASTReader::getLocalType(ModuleFile &F, LocalTypeID LocalID) {7962  return GetType(getGlobalTypeID(F, LocalID));7963}7964 7965serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,7966                                                 LocalTypeID LocalID) const {7967  if (isPredefinedType(LocalID))7968    return LocalID;7969 7970  if (!F.ModuleOffsetMap.empty())7971    ReadModuleOffsetMap(F);7972 7973  unsigned ModuleFileIndex = getModuleFileIndexForTypeID(LocalID);7974  LocalID &= llvm::maskTrailingOnes<TypeID>(32);7975 7976  if (ModuleFileIndex == 0)7977    LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;7978 7979  ModuleFile &MF =7980      ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;7981  ModuleFileIndex = MF.Index + 1;7982  return ((uint64_t)ModuleFileIndex << 32) | LocalID;7983}7984 7985TemplateArgumentLocInfo7986ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {7987  switch (Kind) {7988  case TemplateArgument::Expression:7989    return readExpr();7990  case TemplateArgument::Type:7991    return readTypeSourceInfo();7992  case TemplateArgument::Template:7993  case TemplateArgument::TemplateExpansion: {7994    SourceLocation TemplateKWLoc = readSourceLocation();7995    NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();7996    SourceLocation TemplateNameLoc = readSourceLocation();7997    SourceLocation EllipsisLoc = Kind == TemplateArgument::TemplateExpansion7998                                     ? readSourceLocation()7999                                     : SourceLocation();8000    return TemplateArgumentLocInfo(getASTContext(), TemplateKWLoc, QualifierLoc,8001                                   TemplateNameLoc, EllipsisLoc);8002  }8003  case TemplateArgument::Null:8004  case TemplateArgument::Integral:8005  case TemplateArgument::Declaration:8006  case TemplateArgument::NullPtr:8007  case TemplateArgument::StructuralValue:8008  case TemplateArgument::Pack:8009    // FIXME: Is this right?8010    return TemplateArgumentLocInfo();8011  }8012  llvm_unreachable("unexpected template argument loc");8013}8014 8015TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {8016  TemplateArgument Arg = readTemplateArgument();8017 8018  if (Arg.getKind() == TemplateArgument::Expression) {8019    if (readBool()) // bool InfoHasSameExpr.8020      return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));8021  }8022  return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));8023}8024 8025void ASTRecordReader::readTemplateArgumentListInfo(8026    TemplateArgumentListInfo &Result) {8027  Result.setLAngleLoc(readSourceLocation());8028  Result.setRAngleLoc(readSourceLocation());8029  unsigned NumArgsAsWritten = readInt();8030  for (unsigned i = 0; i != NumArgsAsWritten; ++i)8031    Result.addArgument(readTemplateArgumentLoc());8032}8033 8034const ASTTemplateArgumentListInfo *8035ASTRecordReader::readASTTemplateArgumentListInfo() {8036  TemplateArgumentListInfo Result;8037  readTemplateArgumentListInfo(Result);8038  return ASTTemplateArgumentListInfo::Create(getContext(), Result);8039}8040 8041Decl *ASTReader::GetExternalDecl(GlobalDeclID ID) { return GetDecl(ID); }8042 8043void ASTReader::CompleteRedeclChain(const Decl *D) {8044  if (NumCurrentElementsDeserializing) {8045    // We arrange to not care about the complete redeclaration chain while we're8046    // deserializing. Just remember that the AST has marked this one as complete8047    // but that it's not actually complete yet, so we know we still need to8048    // complete it later.8049    PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));8050    return;8051  }8052 8053  if (!D->getDeclContext()) {8054    assert(isa<TranslationUnitDecl>(D) && "Not a TU?");8055    return;8056  }8057 8058  const DeclContext *DC = D->getDeclContext()->getRedeclContext();8059 8060  // If this is a named declaration, complete it by looking it up8061  // within its context.8062  //8063  // FIXME: Merging a function definition should merge8064  // all mergeable entities within it.8065  if (isa<TranslationUnitDecl, NamespaceDecl, RecordDecl, EnumDecl>(DC)) {8066    if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {8067      if (!getContext().getLangOpts().CPlusPlus &&8068          isa<TranslationUnitDecl>(DC)) {8069        // Outside of C++, we don't have a lookup table for the TU, so update8070        // the identifier instead. (For C++ modules, we don't store decls8071        // in the serialized identifier table, so we do the lookup in the TU.)8072        auto *II = Name.getAsIdentifierInfo();8073        assert(II && "non-identifier name in C?");8074        if (II->isOutOfDate())8075          updateOutOfDateIdentifier(*II);8076      } else8077        DC->lookup(Name);8078    } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {8079      // Find all declarations of this kind from the relevant context.8080      for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {8081        auto *DC = cast<DeclContext>(DCDecl);8082        SmallVector<Decl*, 8> Decls;8083        FindExternalLexicalDecls(8084            DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);8085      }8086    }8087  }8088 8089  RedeclarableTemplateDecl *Template = nullptr;8090  ArrayRef<TemplateArgument> Args;8091  if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {8092    Template = CTSD->getSpecializedTemplate();8093    Args = CTSD->getTemplateArgs().asArray();8094  } else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) {8095    Template = VTSD->getSpecializedTemplate();8096    Args = VTSD->getTemplateArgs().asArray();8097  } else if (auto *FD = dyn_cast<FunctionDecl>(D)) {8098    if (auto *Tmplt = FD->getPrimaryTemplate()) {8099      Template = Tmplt;8100      Args = FD->getTemplateSpecializationArgs()->asArray();8101    }8102  }8103 8104  if (Template) {8105    // For partitial specialization, load all the specializations for safety.8106    if (isa<ClassTemplatePartialSpecializationDecl,8107            VarTemplatePartialSpecializationDecl>(D))8108      Template->loadLazySpecializationsImpl();8109    else8110      Template->loadLazySpecializationsImpl(Args);8111  }8112}8113 8114CXXCtorInitializer **8115ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {8116  RecordLocation Loc = getLocalBitOffset(Offset);8117  BitstreamCursor &Cursor = Loc.F->DeclsCursor;8118  SavedStreamPosition SavedPosition(Cursor);8119  if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {8120    Error(std::move(Err));8121    return nullptr;8122  }8123  ReadingKindTracker ReadingKind(Read_Decl, *this);8124  Deserializing D(this);8125 8126  Expected<unsigned> MaybeCode = Cursor.ReadCode();8127  if (!MaybeCode) {8128    Error(MaybeCode.takeError());8129    return nullptr;8130  }8131  unsigned Code = MaybeCode.get();8132 8133  ASTRecordReader Record(*this, *Loc.F);8134  Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);8135  if (!MaybeRecCode) {8136    Error(MaybeRecCode.takeError());8137    return nullptr;8138  }8139  if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {8140    Error("malformed AST file: missing C++ ctor initializers");8141    return nullptr;8142  }8143 8144  return Record.readCXXCtorInitializers();8145}8146 8147CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {8148  assert(ContextObj && "reading base specifiers with no AST context");8149  ASTContext &Context = *ContextObj;8150 8151  RecordLocation Loc = getLocalBitOffset(Offset);8152  BitstreamCursor &Cursor = Loc.F->DeclsCursor;8153  SavedStreamPosition SavedPosition(Cursor);8154  if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {8155    Error(std::move(Err));8156    return nullptr;8157  }8158  ReadingKindTracker ReadingKind(Read_Decl, *this);8159  Deserializing D(this);8160 8161  Expected<unsigned> MaybeCode = Cursor.ReadCode();8162  if (!MaybeCode) {8163    Error(MaybeCode.takeError());8164    return nullptr;8165  }8166  unsigned Code = MaybeCode.get();8167 8168  ASTRecordReader Record(*this, *Loc.F);8169  Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);8170  if (!MaybeRecCode) {8171    Error(MaybeCode.takeError());8172    return nullptr;8173  }8174  unsigned RecCode = MaybeRecCode.get();8175 8176  if (RecCode != DECL_CXX_BASE_SPECIFIERS) {8177    Error("malformed AST file: missing C++ base specifiers");8178    return nullptr;8179  }8180 8181  unsigned NumBases = Record.readInt();8182  void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);8183  CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];8184  for (unsigned I = 0; I != NumBases; ++I)8185    Bases[I] = Record.readCXXBaseSpecifier();8186  return Bases;8187}8188 8189GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F,8190                                        LocalDeclID LocalID) const {8191  if (LocalID < NUM_PREDEF_DECL_IDS)8192    return GlobalDeclID(LocalID.getRawValue());8193 8194  unsigned OwningModuleFileIndex = LocalID.getModuleFileIndex();8195  DeclID ID = LocalID.getLocalDeclIndex();8196 8197  if (!F.ModuleOffsetMap.empty())8198    ReadModuleOffsetMap(F);8199 8200  ModuleFile *OwningModuleFile =8201      OwningModuleFileIndex == 08202          ? &F8203          : F.TransitiveImports[OwningModuleFileIndex - 1];8204 8205  if (OwningModuleFileIndex == 0)8206    ID -= NUM_PREDEF_DECL_IDS;8207 8208  uint64_t NewModuleFileIndex = OwningModuleFile->Index + 1;8209  return GlobalDeclID(NewModuleFileIndex, ID);8210}8211 8212bool ASTReader::isDeclIDFromModule(GlobalDeclID ID, ModuleFile &M) const {8213  // Predefined decls aren't from any module.8214  if (ID < NUM_PREDEF_DECL_IDS)8215    return false;8216 8217  unsigned ModuleFileIndex = ID.getModuleFileIndex();8218  return M.Index == ModuleFileIndex - 1;8219}8220 8221ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {8222  // Predefined decls aren't from any module.8223  if (ID < NUM_PREDEF_DECL_IDS)8224    return nullptr;8225 8226  uint64_t ModuleFileIndex = ID.getModuleFileIndex();8227  assert(ModuleFileIndex && "Untranslated Local Decl?");8228 8229  return &getModuleManager()[ModuleFileIndex - 1];8230}8231 8232ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {8233  if (!D->isFromASTFile())8234    return nullptr;8235 8236  return getOwningModuleFile(D->getGlobalID());8237}8238 8239SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {8240  if (ID < NUM_PREDEF_DECL_IDS)8241    return SourceLocation();8242 8243  if (Decl *D = GetExistingDecl(ID))8244    return D->getLocation();8245 8246  SourceLocation Loc;8247  DeclCursorForID(ID, Loc);8248  return Loc;8249}8250 8251Decl *ASTReader::getPredefinedDecl(PredefinedDeclIDs ID) {8252  assert(ContextObj && "reading predefined decl without AST context");8253  ASTContext &Context = *ContextObj;8254  Decl *NewLoaded = nullptr;8255  switch (ID) {8256  case PREDEF_DECL_NULL_ID:8257    return nullptr;8258 8259  case PREDEF_DECL_TRANSLATION_UNIT_ID:8260    return Context.getTranslationUnitDecl();8261 8262  case PREDEF_DECL_OBJC_ID_ID:8263    if (Context.ObjCIdDecl)8264      return Context.ObjCIdDecl;8265    NewLoaded = Context.getObjCIdDecl();8266    break;8267 8268  case PREDEF_DECL_OBJC_SEL_ID:8269    if (Context.ObjCSelDecl)8270      return Context.ObjCSelDecl;8271    NewLoaded = Context.getObjCSelDecl();8272    break;8273 8274  case PREDEF_DECL_OBJC_CLASS_ID:8275    if (Context.ObjCClassDecl)8276      return Context.ObjCClassDecl;8277    NewLoaded = Context.getObjCClassDecl();8278    break;8279 8280  case PREDEF_DECL_OBJC_PROTOCOL_ID:8281    if (Context.ObjCProtocolClassDecl)8282      return Context.ObjCProtocolClassDecl;8283    NewLoaded = Context.getObjCProtocolDecl();8284    break;8285 8286  case PREDEF_DECL_INT_128_ID:8287    if (Context.Int128Decl)8288      return Context.Int128Decl;8289    NewLoaded = Context.getInt128Decl();8290    break;8291 8292  case PREDEF_DECL_UNSIGNED_INT_128_ID:8293    if (Context.UInt128Decl)8294      return Context.UInt128Decl;8295    NewLoaded = Context.getUInt128Decl();8296    break;8297 8298  case PREDEF_DECL_OBJC_INSTANCETYPE_ID:8299    if (Context.ObjCInstanceTypeDecl)8300      return Context.ObjCInstanceTypeDecl;8301    NewLoaded = Context.getObjCInstanceTypeDecl();8302    break;8303 8304  case PREDEF_DECL_BUILTIN_VA_LIST_ID:8305    if (Context.BuiltinVaListDecl)8306      return Context.BuiltinVaListDecl;8307    NewLoaded = Context.getBuiltinVaListDecl();8308    break;8309 8310  case PREDEF_DECL_VA_LIST_TAG:8311    if (Context.VaListTagDecl)8312      return Context.VaListTagDecl;8313    NewLoaded = Context.getVaListTagDecl();8314    break;8315 8316  case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:8317    if (Context.BuiltinMSVaListDecl)8318      return Context.BuiltinMSVaListDecl;8319    NewLoaded = Context.getBuiltinMSVaListDecl();8320    break;8321 8322  case PREDEF_DECL_BUILTIN_MS_GUID_ID:8323    // ASTContext::getMSGuidTagDecl won't create MSGuidTagDecl conditionally.8324    return Context.getMSGuidTagDecl();8325 8326  case PREDEF_DECL_EXTERN_C_CONTEXT_ID:8327    if (Context.ExternCContext)8328      return Context.ExternCContext;8329    NewLoaded = Context.getExternCContextDecl();8330    break;8331 8332  case PREDEF_DECL_CF_CONSTANT_STRING_ID:8333    if (Context.CFConstantStringTypeDecl)8334      return Context.CFConstantStringTypeDecl;8335    NewLoaded = Context.getCFConstantStringDecl();8336    break;8337 8338  case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:8339    if (Context.CFConstantStringTagDecl)8340      return Context.CFConstantStringTagDecl;8341    NewLoaded = Context.getCFConstantStringTagDecl();8342    break;8343 8344  case PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID:8345    return Context.getMSTypeInfoTagDecl();8346 8347#define BuiltinTemplate(BTName)                                                \8348  case PREDEF_DECL##BTName##_ID:                                               \8349    if (Context.Decl##BTName)                                                  \8350      return Context.Decl##BTName;                                             \8351    NewLoaded = Context.get##BTName##Decl();                                   \8352    break;8353#include "clang/Basic/BuiltinTemplates.inc"8354 8355  case NUM_PREDEF_DECL_IDS:8356    llvm_unreachable("Invalid decl ID");8357    break;8358  }8359 8360  assert(NewLoaded && "Failed to load predefined decl?");8361 8362  if (DeserializationListener)8363    DeserializationListener->PredefinedDeclBuilt(ID, NewLoaded);8364 8365  return NewLoaded;8366}8367 8368unsigned ASTReader::translateGlobalDeclIDToIndex(GlobalDeclID GlobalID) const {8369  ModuleFile *OwningModuleFile = getOwningModuleFile(GlobalID);8370  if (!OwningModuleFile) {8371    assert(GlobalID < NUM_PREDEF_DECL_IDS && "Untransalted Global ID?");8372    return GlobalID.getRawValue();8373  }8374 8375  return OwningModuleFile->BaseDeclIndex + GlobalID.getLocalDeclIndex();8376}8377 8378Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) {8379  assert(ContextObj && "reading decl with no AST context");8380 8381  if (ID < NUM_PREDEF_DECL_IDS) {8382    Decl *D = getPredefinedDecl((PredefinedDeclIDs)ID);8383    if (D) {8384      // Track that we have merged the declaration with ID \p ID into the8385      // pre-existing predefined declaration \p D.8386      auto &Merged = KeyDecls[D->getCanonicalDecl()];8387      if (Merged.empty())8388        Merged.push_back(ID);8389    }8390    return D;8391  }8392 8393  unsigned Index = translateGlobalDeclIDToIndex(ID);8394 8395  if (Index >= DeclsLoaded.size()) {8396    assert(0 && "declaration ID out-of-range for AST file");8397    Error("declaration ID out-of-range for AST file");8398    return nullptr;8399  }8400 8401  return DeclsLoaded[Index];8402}8403 8404Decl *ASTReader::GetDecl(GlobalDeclID ID) {8405  if (ID < NUM_PREDEF_DECL_IDS)8406    return GetExistingDecl(ID);8407 8408  unsigned Index = translateGlobalDeclIDToIndex(ID);8409 8410  if (Index >= DeclsLoaded.size()) {8411    assert(0 && "declaration ID out-of-range for AST file");8412    Error("declaration ID out-of-range for AST file");8413    return nullptr;8414  }8415 8416  if (!DeclsLoaded[Index]) {8417    ReadDeclRecord(ID);8418    if (DeserializationListener)8419      DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);8420  }8421 8422  return DeclsLoaded[Index];8423}8424 8425LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,8426                                                       GlobalDeclID GlobalID) {8427  if (GlobalID < NUM_PREDEF_DECL_IDS)8428    return LocalDeclID::get(*this, M, GlobalID.getRawValue());8429 8430  if (!M.ModuleOffsetMap.empty())8431    ReadModuleOffsetMap(M);8432 8433  ModuleFile *Owner = getOwningModuleFile(GlobalID);8434  DeclID ID = GlobalID.getLocalDeclIndex();8435 8436  if (Owner == &M) {8437    ID += NUM_PREDEF_DECL_IDS;8438    return LocalDeclID::get(*this, M, ID);8439  }8440 8441  uint64_t OrignalModuleFileIndex = 0;8442  for (unsigned I = 0; I < M.TransitiveImports.size(); I++)8443    if (M.TransitiveImports[I] == Owner) {8444      OrignalModuleFileIndex = I + 1;8445      break;8446    }8447 8448  if (!OrignalModuleFileIndex)8449    return LocalDeclID();8450 8451  return LocalDeclID::get(*this, M, OrignalModuleFileIndex, ID);8452}8453 8454GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,8455                                   unsigned &Idx) {8456  if (Idx >= Record.size()) {8457    Error("Corrupted AST file");8458    return GlobalDeclID(0);8459  }8460 8461  return getGlobalDeclID(F, LocalDeclID::get(*this, F, Record[Idx++]));8462}8463 8464/// Resolve the offset of a statement into a statement.8465///8466/// This operation will read a new statement from the external8467/// source each time it is called, and is meant to be used via a8468/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).8469Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {8470  // Switch case IDs are per Decl.8471  ClearSwitchCaseIDs();8472 8473  // Offset here is a global offset across the entire chain.8474  RecordLocation Loc = getLocalBitOffset(Offset);8475  if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {8476    Error(std::move(Err));8477    return nullptr;8478  }8479  assert(NumCurrentElementsDeserializing == 0 &&8480         "should not be called while already deserializing");8481  Deserializing D(this);8482  return ReadStmtFromStream(*Loc.F);8483}8484 8485bool ASTReader::LoadExternalSpecializationsImpl(SpecLookupTableTy &SpecLookups,8486                                                const Decl *D) {8487  assert(D);8488 8489  auto It = SpecLookups.find(D);8490  if (It == SpecLookups.end())8491    return false;8492 8493  // Get Decl may violate the iterator from SpecializationsLookups so we store8494  // the DeclIDs in ahead.8495  llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =8496      It->second.Table.findAll();8497 8498  // Since we've loaded all the specializations, we can erase it from8499  // the lookup table.8500  SpecLookups.erase(It);8501 8502  bool NewSpecsFound = false;8503  Deserializing LookupResults(this);8504  for (auto &Info : Infos) {8505    if (GetExistingDecl(Info))8506      continue;8507    NewSpecsFound = true;8508    GetDecl(Info);8509  }8510 8511  return NewSpecsFound;8512}8513 8514bool ASTReader::LoadExternalSpecializations(const Decl *D, bool OnlyPartial) {8515  assert(D);8516 8517  CompleteRedeclChain(D);8518  bool NewSpecsFound =8519      LoadExternalSpecializationsImpl(PartialSpecializationsLookups, D);8520  if (OnlyPartial)8521    return NewSpecsFound;8522 8523  NewSpecsFound |= LoadExternalSpecializationsImpl(SpecializationsLookups, D);8524  return NewSpecsFound;8525}8526 8527bool ASTReader::LoadExternalSpecializationsImpl(8528    SpecLookupTableTy &SpecLookups, const Decl *D,8529    ArrayRef<TemplateArgument> TemplateArgs) {8530  assert(D);8531 8532  auto It = SpecLookups.find(D);8533  if (It == SpecLookups.end())8534    return false;8535 8536  llvm::TimeTraceScope TimeScope("Load External Specializations for ", [&] {8537    std::string Name;8538    llvm::raw_string_ostream OS(Name);8539    auto *ND = cast<NamedDecl>(D);8540    ND->getNameForDiagnostic(OS, ND->getASTContext().getPrintingPolicy(),8541                             /*Qualified=*/true);8542    return Name;8543  });8544 8545  Deserializing LookupResults(this);8546  auto HashValue = StableHashForTemplateArguments(TemplateArgs);8547 8548  // Get Decl may violate the iterator from SpecLookups8549  llvm::SmallVector<serialization::reader::LazySpecializationInfo, 8> Infos =8550      It->second.Table.find(HashValue);8551 8552  bool NewSpecsFound = false;8553  for (auto &Info : Infos) {8554    if (GetExistingDecl(Info))8555      continue;8556    NewSpecsFound = true;8557    GetDecl(Info);8558  }8559 8560  return NewSpecsFound;8561}8562 8563bool ASTReader::LoadExternalSpecializations(8564    const Decl *D, ArrayRef<TemplateArgument> TemplateArgs) {8565  assert(D);8566 8567  bool NewDeclsFound = LoadExternalSpecializationsImpl(8568      PartialSpecializationsLookups, D, TemplateArgs);8569  NewDeclsFound |=8570      LoadExternalSpecializationsImpl(SpecializationsLookups, D, TemplateArgs);8571 8572  return NewDeclsFound;8573}8574 8575void ASTReader::FindExternalLexicalDecls(8576    const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,8577    SmallVectorImpl<Decl *> &Decls) {8578  bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};8579 8580  auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {8581    assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");8582    for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {8583      auto K = (Decl::Kind)+LexicalDecls[I];8584      if (!IsKindWeWant(K))8585        continue;8586 8587      auto ID = (DeclID) + LexicalDecls[I + 1];8588 8589      // Don't add predefined declarations to the lexical context more8590      // than once.8591      if (ID < NUM_PREDEF_DECL_IDS) {8592        if (PredefsVisited[ID])8593          continue;8594 8595        PredefsVisited[ID] = true;8596      }8597 8598      if (Decl *D = GetLocalDecl(*M, LocalDeclID::get(*this, *M, ID))) {8599        assert(D->getKind() == K && "wrong kind for lexical decl");8600        if (!DC->isDeclInLexicalTraversal(D))8601          Decls.push_back(D);8602      }8603    }8604  };8605 8606  if (isa<TranslationUnitDecl>(DC)) {8607    for (const auto &Lexical : TULexicalDecls)8608      Visit(Lexical.first, Lexical.second);8609  } else {8610    auto I = LexicalDecls.find(DC);8611    if (I != LexicalDecls.end())8612      Visit(I->second.first, I->second.second);8613  }8614 8615  ++NumLexicalDeclContextsRead;8616}8617 8618namespace {8619 8620class UnalignedDeclIDComp {8621  ASTReader &Reader;8622  ModuleFile &Mod;8623 8624public:8625  UnalignedDeclIDComp(ASTReader &Reader, ModuleFile &M)8626      : Reader(Reader), Mod(M) {}8627 8628  bool operator()(unaligned_decl_id_t L, unaligned_decl_id_t R) const {8629    SourceLocation LHS = getLocation(L);8630    SourceLocation RHS = getLocation(R);8631    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);8632  }8633 8634  bool operator()(SourceLocation LHS, unaligned_decl_id_t R) const {8635    SourceLocation RHS = getLocation(R);8636    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);8637  }8638 8639  bool operator()(unaligned_decl_id_t L, SourceLocation RHS) const {8640    SourceLocation LHS = getLocation(L);8641    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);8642  }8643 8644  SourceLocation getLocation(unaligned_decl_id_t ID) const {8645    return Reader.getSourceManager().getFileLoc(8646        Reader.getSourceLocationForDeclID(8647            Reader.getGlobalDeclID(Mod, LocalDeclID::get(Reader, Mod, ID))));8648  }8649};8650 8651} // namespace8652 8653void ASTReader::FindFileRegionDecls(FileID File,8654                                    unsigned Offset, unsigned Length,8655                                    SmallVectorImpl<Decl *> &Decls) {8656  SourceManager &SM = getSourceManager();8657 8658  llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);8659  if (I == FileDeclIDs.end())8660    return;8661 8662  FileDeclsInfo &DInfo = I->second;8663  if (DInfo.Decls.empty())8664    return;8665 8666  SourceLocation8667    BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);8668  SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);8669 8670  UnalignedDeclIDComp DIDComp(*this, *DInfo.Mod);8671  ArrayRef<unaligned_decl_id_t>::iterator BeginIt =8672      llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);8673  if (BeginIt != DInfo.Decls.begin())8674    --BeginIt;8675 8676  // If we are pointing at a top-level decl inside an objc container, we need8677  // to backtrack until we find it otherwise we will fail to report that the8678  // region overlaps with an objc container.8679  while (BeginIt != DInfo.Decls.begin() &&8680         GetDecl(getGlobalDeclID(*DInfo.Mod,8681                                 LocalDeclID::get(*this, *DInfo.Mod, *BeginIt)))8682             ->isTopLevelDeclInObjCContainer())8683    --BeginIt;8684 8685  ArrayRef<unaligned_decl_id_t>::iterator EndIt =8686      llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);8687  if (EndIt != DInfo.Decls.end())8688    ++EndIt;8689 8690  for (ArrayRef<unaligned_decl_id_t>::iterator DIt = BeginIt; DIt != EndIt;8691       ++DIt)8692    Decls.push_back(GetDecl(getGlobalDeclID(8693        *DInfo.Mod, LocalDeclID::get(*this, *DInfo.Mod, *DIt))));8694}8695 8696bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,8697                                               DeclarationName Name,8698                                               const DeclContext *OriginalDC) {8699  assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&8700         "DeclContext has no visible decls in storage");8701  if (!Name)8702    return false;8703 8704  // Load the list of declarations.8705  SmallVector<NamedDecl *, 64> Decls;8706  llvm::SmallPtrSet<NamedDecl *, 8> Found;8707 8708  auto Find = [&, this](auto &&Table, auto &&Key) {8709    for (GlobalDeclID ID : Table.find(Key)) {8710      NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));8711      if (ND->getDeclName() == Name && Found.insert(ND).second)8712        Decls.push_back(ND);8713    }8714  };8715 8716  Deserializing LookupResults(this);8717 8718  // FIXME: Clear the redundancy with templated lambda in C++20 when that's8719  // available.8720  if (auto It = Lookups.find(DC); It != Lookups.end()) {8721    ++NumVisibleDeclContextsRead;8722    Find(It->second.Table, Name);8723  }8724 8725  auto FindModuleLocalLookup = [&, this](Module *NamedModule) {8726    if (auto It = ModuleLocalLookups.find(DC); It != ModuleLocalLookups.end()) {8727      ++NumModuleLocalVisibleDeclContexts;8728      Find(It->second.Table, std::make_pair(Name, NamedModule));8729    }8730  };8731  if (auto *NamedModule =8732          OriginalDC ? cast<Decl>(OriginalDC)->getTopLevelOwningNamedModule()8733                     : nullptr)8734    FindModuleLocalLookup(NamedModule);8735  // See clang/test/Modules/ModulesLocalNamespace.cppm for the motiviation case.8736  // We're going to find a decl but the decl context of the lookup is8737  // unspecified. In this case, the OriginalDC may be the decl context in other8738  // module.8739  if (ContextObj && ContextObj->getCurrentNamedModule())8740    FindModuleLocalLookup(ContextObj->getCurrentNamedModule());8741 8742  if (auto It = TULocalLookups.find(DC); It != TULocalLookups.end()) {8743    ++NumTULocalVisibleDeclContexts;8744    Find(It->second.Table, Name);8745  }8746 8747  SetExternalVisibleDeclsForName(DC, Name, Decls);8748  return !Decls.empty();8749}8750 8751void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {8752  if (!DC->hasExternalVisibleStorage())8753    return;8754 8755  DeclsMap Decls;8756 8757  auto findAll = [&](auto &LookupTables, unsigned &NumRead) {8758    auto It = LookupTables.find(DC);8759    if (It == LookupTables.end())8760      return;8761 8762    NumRead++;8763 8764    for (GlobalDeclID ID : It->second.Table.findAll()) {8765      NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));8766      Decls[ND->getDeclName()].push_back(ND);8767    }8768 8769    // FIXME: Why a PCH test is failing if we remove the iterator after findAll?8770  };8771 8772  findAll(Lookups, NumVisibleDeclContextsRead);8773  findAll(ModuleLocalLookups, NumModuleLocalVisibleDeclContexts);8774  findAll(TULocalLookups, NumTULocalVisibleDeclContexts);8775 8776  for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {8777    SetExternalVisibleDeclsForName(DC, I->first, I->second);8778  }8779  const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);8780}8781 8782const serialization::reader::DeclContextLookupTable *8783ASTReader::getLoadedLookupTables(DeclContext *Primary) const {8784  auto I = Lookups.find(Primary);8785  return I == Lookups.end() ? nullptr : &I->second;8786}8787 8788const serialization::reader::ModuleLocalLookupTable *8789ASTReader::getModuleLocalLookupTables(DeclContext *Primary) const {8790  auto I = ModuleLocalLookups.find(Primary);8791  return I == ModuleLocalLookups.end() ? nullptr : &I->second;8792}8793 8794const serialization::reader::DeclContextLookupTable *8795ASTReader::getTULocalLookupTables(DeclContext *Primary) const {8796  auto I = TULocalLookups.find(Primary);8797  return I == TULocalLookups.end() ? nullptr : &I->second;8798}8799 8800serialization::reader::LazySpecializationInfoLookupTable *8801ASTReader::getLoadedSpecializationsLookupTables(const Decl *D, bool IsPartial) {8802  assert(D->isCanonicalDecl());8803  auto &LookupTable =8804      IsPartial ? PartialSpecializationsLookups : SpecializationsLookups;8805  auto I = LookupTable.find(D);8806  return I == LookupTable.end() ? nullptr : &I->second;8807}8808 8809bool ASTReader::haveUnloadedSpecializations(const Decl *D) const {8810  assert(D->isCanonicalDecl());8811  return PartialSpecializationsLookups.contains(D) ||8812         SpecializationsLookups.contains(D);8813}8814 8815/// Under non-PCH compilation the consumer receives the objc methods8816/// before receiving the implementation, and codegen depends on this.8817/// We simulate this by deserializing and passing to consumer the methods of the8818/// implementation before passing the deserialized implementation decl.8819static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,8820                                       ASTConsumer *Consumer) {8821  assert(ImplD && Consumer);8822 8823  for (auto *I : ImplD->methods())8824    Consumer->HandleInterestingDecl(DeclGroupRef(I));8825 8826  Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));8827}8828 8829void ASTReader::PassInterestingDeclToConsumer(Decl *D) {8830  if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))8831    PassObjCImplDeclToConsumer(ImplD, Consumer);8832  else8833    Consumer->HandleInterestingDecl(DeclGroupRef(D));8834}8835 8836void ASTReader::PassVTableToConsumer(CXXRecordDecl *RD) {8837  Consumer->HandleVTable(RD);8838}8839 8840void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {8841  this->Consumer = Consumer;8842 8843  if (Consumer)8844    PassInterestingDeclsToConsumer();8845 8846  if (DeserializationListener)8847    DeserializationListener->ReaderInitialized(this);8848}8849 8850void ASTReader::PrintStats() {8851  std::fprintf(stderr, "*** AST File Statistics:\n");8852 8853  unsigned NumTypesLoaded =8854      TypesLoaded.size() - llvm::count(TypesLoaded.materialized(), QualType());8855  unsigned NumDeclsLoaded =8856      DeclsLoaded.size() -8857      llvm::count(DeclsLoaded.materialized(), (Decl *)nullptr);8858  unsigned NumIdentifiersLoaded =8859      IdentifiersLoaded.size() -8860      llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);8861  unsigned NumMacrosLoaded =8862      MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr);8863  unsigned NumSelectorsLoaded =8864      SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector());8865 8866  if (unsigned TotalNumSLocEntries = getTotalNumSLocs())8867    std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",8868                 NumSLocEntriesRead, TotalNumSLocEntries,8869                 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));8870  if (!TypesLoaded.empty())8871    std::fprintf(stderr, "  %u/%u types read (%f%%)\n",8872                 NumTypesLoaded, (unsigned)TypesLoaded.size(),8873                 ((float)NumTypesLoaded/TypesLoaded.size() * 100));8874  if (!DeclsLoaded.empty())8875    std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",8876                 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),8877                 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));8878  if (!IdentifiersLoaded.empty())8879    std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",8880                 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),8881                 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));8882  if (!MacrosLoaded.empty())8883    std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",8884                 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),8885                 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));8886  if (!SelectorsLoaded.empty())8887    std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",8888                 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),8889                 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));8890  if (TotalNumStatements)8891    std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",8892                 NumStatementsRead, TotalNumStatements,8893                 ((float)NumStatementsRead/TotalNumStatements * 100));8894  if (TotalNumMacros)8895    std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",8896                 NumMacrosRead, TotalNumMacros,8897                 ((float)NumMacrosRead/TotalNumMacros * 100));8898  if (TotalLexicalDeclContexts)8899    std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",8900                 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,8901                 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts8902                  * 100));8903  if (TotalVisibleDeclContexts)8904    std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",8905                 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,8906                 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts8907                  * 100));8908  if (TotalModuleLocalVisibleDeclContexts)8909    std::fprintf(8910        stderr, "  %u/%u module local visible declcontexts read (%f%%)\n",8911        NumModuleLocalVisibleDeclContexts, TotalModuleLocalVisibleDeclContexts,8912        ((float)NumModuleLocalVisibleDeclContexts /8913         TotalModuleLocalVisibleDeclContexts * 100));8914  if (TotalTULocalVisibleDeclContexts)8915    std::fprintf(stderr, "  %u/%u visible declcontexts in GMF read (%f%%)\n",8916                 NumTULocalVisibleDeclContexts, TotalTULocalVisibleDeclContexts,8917                 ((float)NumTULocalVisibleDeclContexts /8918                  TotalTULocalVisibleDeclContexts * 100));8919  if (TotalNumMethodPoolEntries)8920    std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",8921                 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,8922                 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries8923                  * 100));8924  if (NumMethodPoolLookups)8925    std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",8926                 NumMethodPoolHits, NumMethodPoolLookups,8927                 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));8928  if (NumMethodPoolTableLookups)8929    std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",8930                 NumMethodPoolTableHits, NumMethodPoolTableLookups,8931                 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups8932                  * 100.0));8933  if (NumIdentifierLookupHits)8934    std::fprintf(stderr,8935                 "  %u / %u identifier table lookups succeeded (%f%%)\n",8936                 NumIdentifierLookupHits, NumIdentifierLookups,8937                 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);8938 8939  if (GlobalIndex) {8940    std::fprintf(stderr, "\n");8941    GlobalIndex->printStats();8942  }8943 8944  std::fprintf(stderr, "\n");8945  dump();8946  std::fprintf(stderr, "\n");8947}8948 8949template<typename Key, typename ModuleFile, unsigned InitialCapacity>8950LLVM_DUMP_METHOD static void8951dumpModuleIDMap(StringRef Name,8952                const ContinuousRangeMap<Key, ModuleFile *,8953                                         InitialCapacity> &Map) {8954  if (Map.begin() == Map.end())8955    return;8956 8957  using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;8958 8959  llvm::errs() << Name << ":\n";8960  for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();8961       I != IEnd; ++I)8962    llvm::errs() << "  " << (DeclID)I->first << " -> " << I->second->FileName8963                 << "\n";8964}8965 8966LLVM_DUMP_METHOD void ASTReader::dump() {8967  llvm::errs() << "*** PCH/ModuleFile Remappings:\n";8968  dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);8969  dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);8970  dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);8971  dumpModuleIDMap("Global selector map", GlobalSelectorMap);8972  dumpModuleIDMap("Global preprocessed entity map",8973                  GlobalPreprocessedEntityMap);8974 8975  llvm::errs() << "\n*** PCH/Modules Loaded:";8976  for (ModuleFile &M : ModuleMgr)8977    M.dump();8978}8979 8980/// Return the amount of memory used by memory buffers, breaking down8981/// by heap-backed versus mmap'ed memory.8982void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {8983  for (ModuleFile &I : ModuleMgr) {8984    if (llvm::MemoryBuffer *buf = I.Buffer) {8985      size_t bytes = buf->getBufferSize();8986      switch (buf->getBufferKind()) {8987        case llvm::MemoryBuffer::MemoryBuffer_Malloc:8988          sizes.malloc_bytes += bytes;8989          break;8990        case llvm::MemoryBuffer::MemoryBuffer_MMap:8991          sizes.mmap_bytes += bytes;8992          break;8993      }8994    }8995  }8996}8997 8998void ASTReader::InitializeSema(Sema &S) {8999  SemaObj = &S;9000  S.addExternalSource(this);9001 9002  // Makes sure any declarations that were deserialized "too early"9003  // still get added to the identifier's declaration chains.9004  for (GlobalDeclID ID : PreloadedDeclIDs) {9005    NamedDecl *D = cast<NamedDecl>(GetDecl(ID));9006    pushExternalDeclIntoScope(D, D->getDeclName());9007  }9008  PreloadedDeclIDs.clear();9009 9010  // FIXME: What happens if these are changed by a module import?9011  if (!FPPragmaOptions.empty()) {9012    assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");9013    FPOptionsOverride NewOverrides =9014        FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);9015    SemaObj->CurFPFeatures =9016        NewOverrides.applyOverrides(SemaObj->getLangOpts());9017  }9018 9019  for (GlobalDeclID ID : DeclsWithEffectsToVerify) {9020    Decl *D = GetDecl(ID);9021    if (auto *FD = dyn_cast<FunctionDecl>(D))9022      SemaObj->addDeclWithEffects(FD, FD->getFunctionEffects());9023    else if (auto *BD = dyn_cast<BlockDecl>(D))9024      SemaObj->addDeclWithEffects(BD, BD->getFunctionEffects());9025    else9026      llvm_unreachable("unexpected Decl type in DeclsWithEffectsToVerify");9027  }9028  DeclsWithEffectsToVerify.clear();9029 9030  SemaObj->OpenCLFeatures = OpenCLExtensions;9031 9032  UpdateSema();9033}9034 9035void ASTReader::UpdateSema() {9036  assert(SemaObj && "no Sema to update");9037 9038  // Load the offsets of the declarations that Sema references.9039  // They will be lazily deserialized when needed.9040  if (!SemaDeclRefs.empty()) {9041    assert(SemaDeclRefs.size() % 3 == 0);9042    for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {9043      if (!SemaObj->StdNamespace)9044        SemaObj->StdNamespace = SemaDeclRefs[I].getRawValue();9045      if (!SemaObj->StdBadAlloc)9046        SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].getRawValue();9047      if (!SemaObj->StdAlignValT)9048        SemaObj->StdAlignValT = SemaDeclRefs[I + 2].getRawValue();9049    }9050    SemaDeclRefs.clear();9051  }9052 9053  // Update the state of pragmas. Use the same API as if we had encountered the9054  // pragma in the source.9055  if(OptimizeOffPragmaLocation.isValid())9056    SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);9057  if (PragmaMSStructState != -1)9058    SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);9059  if (PointersToMembersPragmaLocation.isValid()) {9060    SemaObj->ActOnPragmaMSPointersToMembers(9061        (LangOptions::PragmaMSPointersToMembersKind)9062            PragmaMSPointersToMembersState,9063        PointersToMembersPragmaLocation);9064  }9065  SemaObj->CUDA().ForceHostDeviceDepth = ForceHostDeviceDepth;9066 9067  if (PragmaAlignPackCurrentValue) {9068    // The bottom of the stack might have a default value. It must be adjusted9069    // to the current value to ensure that the packing state is preserved after9070    // popping entries that were included/imported from a PCH/module.9071    bool DropFirst = false;9072    if (!PragmaAlignPackStack.empty() &&9073        PragmaAlignPackStack.front().Location.isInvalid()) {9074      assert(PragmaAlignPackStack.front().Value ==9075                 SemaObj->AlignPackStack.DefaultValue &&9076             "Expected a default alignment value");9077      SemaObj->AlignPackStack.Stack.emplace_back(9078          PragmaAlignPackStack.front().SlotLabel,9079          SemaObj->AlignPackStack.CurrentValue,9080          SemaObj->AlignPackStack.CurrentPragmaLocation,9081          PragmaAlignPackStack.front().PushLocation);9082      DropFirst = true;9083    }9084    for (const auto &Entry :9085         llvm::ArrayRef(PragmaAlignPackStack).drop_front(DropFirst ? 1 : 0)) {9086      SemaObj->AlignPackStack.Stack.emplace_back(9087          Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);9088    }9089    if (PragmaAlignPackCurrentLocation.isInvalid()) {9090      assert(*PragmaAlignPackCurrentValue ==9091                 SemaObj->AlignPackStack.DefaultValue &&9092             "Expected a default align and pack value");9093      // Keep the current values.9094    } else {9095      SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;9096      SemaObj->AlignPackStack.CurrentPragmaLocation =9097          PragmaAlignPackCurrentLocation;9098    }9099  }9100  if (FpPragmaCurrentValue) {9101    // The bottom of the stack might have a default value. It must be adjusted9102    // to the current value to ensure that fp-pragma state is preserved after9103    // popping entries that were included/imported from a PCH/module.9104    bool DropFirst = false;9105    if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {9106      assert(FpPragmaStack.front().Value ==9107                 SemaObj->FpPragmaStack.DefaultValue &&9108             "Expected a default pragma float_control value");9109      SemaObj->FpPragmaStack.Stack.emplace_back(9110          FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,9111          SemaObj->FpPragmaStack.CurrentPragmaLocation,9112          FpPragmaStack.front().PushLocation);9113      DropFirst = true;9114    }9115    for (const auto &Entry :9116         llvm::ArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))9117      SemaObj->FpPragmaStack.Stack.emplace_back(9118          Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);9119    if (FpPragmaCurrentLocation.isInvalid()) {9120      assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&9121             "Expected a default pragma float_control value");9122      // Keep the current values.9123    } else {9124      SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;9125      SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;9126    }9127  }9128 9129  // For non-modular AST files, restore visiblity of modules.9130  for (auto &Import : PendingImportedModulesSema) {9131    if (Import.ImportLoc.isInvalid())9132      continue;9133    if (Module *Imported = getSubmodule(Import.ID)) {9134      SemaObj->makeModuleVisible(Imported, Import.ImportLoc);9135    }9136  }9137  PendingImportedModulesSema.clear();9138}9139 9140IdentifierInfo *ASTReader::get(StringRef Name) {9141  // Note that we are loading an identifier.9142  Deserializing AnIdentifier(this);9143 9144  IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,9145                                  NumIdentifierLookups,9146                                  NumIdentifierLookupHits);9147 9148  // We don't need to do identifier table lookups in C++ modules (we preload9149  // all interesting declarations, and don't need to use the scope for name9150  // lookups). Perform the lookup in PCH files, though, since we don't build9151  // a complete initial identifier table if we're carrying on from a PCH.9152  if (PP.getLangOpts().CPlusPlus) {9153    for (auto *F : ModuleMgr.pch_modules())9154      if (Visitor(*F))9155        break;9156  } else {9157    // If there is a global index, look there first to determine which modules9158    // provably do not have any results for this identifier.9159    GlobalModuleIndex::HitSet Hits;9160    GlobalModuleIndex::HitSet *HitsPtr = nullptr;9161    if (!loadGlobalIndex()) {9162      if (GlobalIndex->lookupIdentifier(Name, Hits)) {9163        HitsPtr = &Hits;9164      }9165    }9166 9167    ModuleMgr.visit(Visitor, HitsPtr);9168  }9169 9170  IdentifierInfo *II = Visitor.getIdentifierInfo();9171  markIdentifierUpToDate(II);9172  return II;9173}9174 9175namespace clang {9176 9177  /// An identifier-lookup iterator that enumerates all of the9178  /// identifiers stored within a set of AST files.9179  class ASTIdentifierIterator : public IdentifierIterator {9180    /// The AST reader whose identifiers are being enumerated.9181    const ASTReader &Reader;9182 9183    /// The current index into the chain of AST files stored in9184    /// the AST reader.9185    unsigned Index;9186 9187    /// The current position within the identifier lookup table9188    /// of the current AST file.9189    ASTIdentifierLookupTable::key_iterator Current;9190 9191    /// The end position within the identifier lookup table of9192    /// the current AST file.9193    ASTIdentifierLookupTable::key_iterator End;9194 9195    /// Whether to skip any modules in the ASTReader.9196    bool SkipModules;9197 9198  public:9199    explicit ASTIdentifierIterator(const ASTReader &Reader,9200                                   bool SkipModules = false);9201 9202    StringRef Next() override;9203  };9204 9205} // namespace clang9206 9207ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,9208                                             bool SkipModules)9209    : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {9210}9211 9212StringRef ASTIdentifierIterator::Next() {9213  while (Current == End) {9214    // If we have exhausted all of our AST files, we're done.9215    if (Index == 0)9216      return StringRef();9217 9218    --Index;9219    ModuleFile &F = Reader.ModuleMgr[Index];9220    if (SkipModules && F.isModule())9221      continue;9222 9223    ASTIdentifierLookupTable *IdTable =9224        (ASTIdentifierLookupTable *)F.IdentifierLookupTable;9225    Current = IdTable->key_begin();9226    End = IdTable->key_end();9227  }9228 9229  // We have any identifiers remaining in the current AST file; return9230  // the next one.9231  StringRef Result = *Current;9232  ++Current;9233  return Result;9234}9235 9236namespace {9237 9238/// A utility for appending two IdentifierIterators.9239class ChainedIdentifierIterator : public IdentifierIterator {9240  std::unique_ptr<IdentifierIterator> Current;9241  std::unique_ptr<IdentifierIterator> Queued;9242 9243public:9244  ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,9245                            std::unique_ptr<IdentifierIterator> Second)9246      : Current(std::move(First)), Queued(std::move(Second)) {}9247 9248  StringRef Next() override {9249    if (!Current)9250      return StringRef();9251 9252    StringRef result = Current->Next();9253    if (!result.empty())9254      return result;9255 9256    // Try the queued iterator, which may itself be empty.9257    Current.reset();9258    std::swap(Current, Queued);9259    return Next();9260  }9261};9262 9263} // namespace9264 9265IdentifierIterator *ASTReader::getIdentifiers() {9266  if (!loadGlobalIndex()) {9267    std::unique_ptr<IdentifierIterator> ReaderIter(9268        new ASTIdentifierIterator(*this, /*SkipModules=*/true));9269    std::unique_ptr<IdentifierIterator> ModulesIter(9270        GlobalIndex->createIdentifierIterator());9271    return new ChainedIdentifierIterator(std::move(ReaderIter),9272                                         std::move(ModulesIter));9273  }9274 9275  return new ASTIdentifierIterator(*this);9276}9277 9278namespace clang {9279namespace serialization {9280 9281  class ReadMethodPoolVisitor {9282    ASTReader &Reader;9283    Selector Sel;9284    unsigned PriorGeneration;9285    unsigned InstanceBits = 0;9286    unsigned FactoryBits = 0;9287    bool InstanceHasMoreThanOneDecl = false;9288    bool FactoryHasMoreThanOneDecl = false;9289    SmallVector<ObjCMethodDecl *, 4> InstanceMethods;9290    SmallVector<ObjCMethodDecl *, 4> FactoryMethods;9291 9292  public:9293    ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,9294                          unsigned PriorGeneration)9295        : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}9296 9297    bool operator()(ModuleFile &M) {9298      if (!M.SelectorLookupTable)9299        return false;9300 9301      // If we've already searched this module file, skip it now.9302      if (M.Generation <= PriorGeneration)9303        return true;9304 9305      ++Reader.NumMethodPoolTableLookups;9306      ASTSelectorLookupTable *PoolTable9307        = (ASTSelectorLookupTable*)M.SelectorLookupTable;9308      ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);9309      if (Pos == PoolTable->end())9310        return false;9311 9312      ++Reader.NumMethodPoolTableHits;9313      ++Reader.NumSelectorsRead;9314      // FIXME: Not quite happy with the statistics here. We probably should9315      // disable this tracking when called via LoadSelector.9316      // Also, should entries without methods count as misses?9317      ++Reader.NumMethodPoolEntriesRead;9318      ASTSelectorLookupTrait::data_type Data = *Pos;9319      if (Reader.DeserializationListener)9320        Reader.DeserializationListener->SelectorRead(Data.ID, Sel);9321 9322      // Append methods in the reverse order, so that later we can process them9323      // in the order they appear in the source code by iterating through9324      // the vector in the reverse order.9325      InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend());9326      FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend());9327      InstanceBits = Data.InstanceBits;9328      FactoryBits = Data.FactoryBits;9329      InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;9330      FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;9331      return false;9332    }9333 9334    /// Retrieve the instance methods found by this visitor.9335    ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {9336      return InstanceMethods;9337    }9338 9339    /// Retrieve the instance methods found by this visitor.9340    ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {9341      return FactoryMethods;9342    }9343 9344    unsigned getInstanceBits() const { return InstanceBits; }9345    unsigned getFactoryBits() const { return FactoryBits; }9346 9347    bool instanceHasMoreThanOneDecl() const {9348      return InstanceHasMoreThanOneDecl;9349    }9350 9351    bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }9352  };9353 9354} // namespace serialization9355} // namespace clang9356 9357/// Add the given set of methods to the method list.9358static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,9359                             ObjCMethodList &List) {9360  for (ObjCMethodDecl *M : llvm::reverse(Methods))9361    S.ObjC().addMethodToGlobalList(&List, M);9362}9363 9364void ASTReader::ReadMethodPool(Selector Sel) {9365  // Get the selector generation and update it to the current generation.9366  unsigned &Generation = SelectorGeneration[Sel];9367  unsigned PriorGeneration = Generation;9368  Generation = getGeneration();9369  SelectorOutOfDate[Sel] = false;9370 9371  // Search for methods defined with this selector.9372  ++NumMethodPoolLookups;9373  ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);9374  ModuleMgr.visit(Visitor);9375 9376  if (Visitor.getInstanceMethods().empty() &&9377      Visitor.getFactoryMethods().empty())9378    return;9379 9380  ++NumMethodPoolHits;9381 9382  if (!getSema())9383    return;9384 9385  Sema &S = *getSema();9386  auto &Methods = S.ObjC().MethodPool[Sel];9387 9388  Methods.first.setBits(Visitor.getInstanceBits());9389  Methods.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());9390  Methods.second.setBits(Visitor.getFactoryBits());9391  Methods.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());9392 9393  // Add methods to the global pool *after* setting hasMoreThanOneDecl, since9394  // when building a module we keep every method individually and may need to9395  // update hasMoreThanOneDecl as we add the methods.9396  addMethodsToPool(S, Visitor.getInstanceMethods(), Methods.first);9397  addMethodsToPool(S, Visitor.getFactoryMethods(), Methods.second);9398}9399 9400void ASTReader::updateOutOfDateSelector(Selector Sel) {9401  if (SelectorOutOfDate[Sel])9402    ReadMethodPool(Sel);9403}9404 9405void ASTReader::ReadKnownNamespaces(9406                          SmallVectorImpl<NamespaceDecl *> &Namespaces) {9407  Namespaces.clear();9408 9409  for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {9410    if (NamespaceDecl *Namespace9411                = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))9412      Namespaces.push_back(Namespace);9413  }9414}9415 9416void ASTReader::ReadUndefinedButUsed(9417    llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {9418  for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {9419    UndefinedButUsedDecl &U = UndefinedButUsed[Idx++];9420    NamedDecl *D = cast<NamedDecl>(GetDecl(U.ID));9421    SourceLocation Loc = SourceLocation::getFromRawEncoding(U.RawLoc);9422    Undefined.insert(std::make_pair(D, Loc));9423  }9424  UndefinedButUsed.clear();9425}9426 9427void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<9428    FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &9429                                                     Exprs) {9430  for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {9431    FieldDecl *FD =9432        cast<FieldDecl>(GetDecl(GlobalDeclID(DelayedDeleteExprs[Idx++])));9433    uint64_t Count = DelayedDeleteExprs[Idx++];9434    for (uint64_t C = 0; C < Count; ++C) {9435      SourceLocation DeleteLoc =9436          SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);9437      const bool IsArrayForm = DelayedDeleteExprs[Idx++];9438      Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));9439    }9440  }9441}9442 9443void ASTReader::ReadTentativeDefinitions(9444                  SmallVectorImpl<VarDecl *> &TentativeDefs) {9445  for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {9446    VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));9447    if (Var)9448      TentativeDefs.push_back(Var);9449  }9450  TentativeDefinitions.clear();9451}9452 9453void ASTReader::ReadUnusedFileScopedDecls(9454                               SmallVectorImpl<const DeclaratorDecl *> &Decls) {9455  for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {9456    DeclaratorDecl *D9457      = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));9458    if (D)9459      Decls.push_back(D);9460  }9461  UnusedFileScopedDecls.clear();9462}9463 9464void ASTReader::ReadDelegatingConstructors(9465                                 SmallVectorImpl<CXXConstructorDecl *> &Decls) {9466  for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {9467    CXXConstructorDecl *D9468      = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));9469    if (D)9470      Decls.push_back(D);9471  }9472  DelegatingCtorDecls.clear();9473}9474 9475void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {9476  for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {9477    TypedefNameDecl *D9478      = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));9479    if (D)9480      Decls.push_back(D);9481  }9482  ExtVectorDecls.clear();9483}9484 9485void ASTReader::ReadUnusedLocalTypedefNameCandidates(9486    llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {9487  for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;9488       ++I) {9489    TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(9490        GetDecl(UnusedLocalTypedefNameCandidates[I]));9491    if (D)9492      Decls.insert(D);9493  }9494  UnusedLocalTypedefNameCandidates.clear();9495}9496 9497void ASTReader::ReadDeclsToCheckForDeferredDiags(9498    llvm::SmallSetVector<Decl *, 4> &Decls) {9499  for (auto I : DeclsToCheckForDeferredDiags) {9500    auto *D = dyn_cast_or_null<Decl>(GetDecl(I));9501    if (D)9502      Decls.insert(D);9503  }9504  DeclsToCheckForDeferredDiags.clear();9505}9506 9507void ASTReader::ReadReferencedSelectors(9508       SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {9509  if (ReferencedSelectorsData.empty())9510    return;9511 9512  // If there are @selector references added them to its pool. This is for9513  // implementation of -Wselector.9514  unsigned int DataSize = ReferencedSelectorsData.size()-1;9515  unsigned I = 0;9516  while (I < DataSize) {9517    Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);9518    SourceLocation SelLoc9519      = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);9520    Sels.push_back(std::make_pair(Sel, SelLoc));9521  }9522  ReferencedSelectorsData.clear();9523}9524 9525void ASTReader::ReadWeakUndeclaredIdentifiers(9526       SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {9527  if (WeakUndeclaredIdentifiers.empty())9528    return;9529 9530  for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {9531    IdentifierInfo *WeakId9532      = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);9533    IdentifierInfo *AliasId9534      = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);9535    SourceLocation Loc =9536        SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);9537    WeakInfo WI(AliasId, Loc);9538    WeakIDs.push_back(std::make_pair(WeakId, WI));9539  }9540  WeakUndeclaredIdentifiers.clear();9541}9542 9543void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {9544  for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {9545    ExternalVTableUse VT;9546    VTableUse &TableInfo = VTableUses[Idx++];9547    VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(TableInfo.ID));9548    VT.Location = SourceLocation::getFromRawEncoding(TableInfo.RawLoc);9549    VT.DefinitionRequired = TableInfo.Used;9550    VTables.push_back(VT);9551  }9552 9553  VTableUses.clear();9554}9555 9556void ASTReader::ReadPendingInstantiations(9557       SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {9558  for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {9559    PendingInstantiation &Inst = PendingInstantiations[Idx++];9560    ValueDecl *D = cast<ValueDecl>(GetDecl(Inst.ID));9561    SourceLocation Loc = SourceLocation::getFromRawEncoding(Inst.RawLoc);9562 9563    Pending.push_back(std::make_pair(D, Loc));9564  }9565  PendingInstantiations.clear();9566}9567 9568void ASTReader::ReadLateParsedTemplates(9569    llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>9570        &LPTMap) {9571  for (auto &LPT : LateParsedTemplates) {9572    ModuleFile *FMod = LPT.first;9573    RecordDataImpl &LateParsed = LPT.second;9574    for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;9575         /* In loop */) {9576      FunctionDecl *FD = ReadDeclAs<FunctionDecl>(*FMod, LateParsed, Idx);9577 9578      auto LT = std::make_unique<LateParsedTemplate>();9579      LT->D = ReadDecl(*FMod, LateParsed, Idx);9580      LT->FPO = FPOptions::getFromOpaqueInt(LateParsed[Idx++]);9581 9582      ModuleFile *F = getOwningModuleFile(LT->D);9583      assert(F && "No module");9584 9585      unsigned TokN = LateParsed[Idx++];9586      LT->Toks.reserve(TokN);9587      for (unsigned T = 0; T < TokN; ++T)9588        LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));9589 9590      LPTMap.insert(std::make_pair(FD, std::move(LT)));9591    }9592  }9593 9594  LateParsedTemplates.clear();9595}9596 9597void ASTReader::AssignedLambdaNumbering(CXXRecordDecl *Lambda) {9598  if (!Lambda->getLambdaContextDecl())9599    return;9600 9601  auto LambdaInfo =9602      std::make_pair(Lambda->getLambdaContextDecl()->getCanonicalDecl(),9603                     Lambda->getLambdaIndexInContext());9604 9605  // Handle the import and then include case for lambdas.9606  if (auto Iter = LambdaDeclarationsForMerging.find(LambdaInfo);9607      Iter != LambdaDeclarationsForMerging.end() &&9608      Iter->second->isFromASTFile() && Lambda->getFirstDecl() == Lambda) {9609    CXXRecordDecl *Previous =9610        cast<CXXRecordDecl>(Iter->second)->getMostRecentDecl();9611    Lambda->setPreviousDecl(Previous);9612    return;9613  }9614 9615  // Keep track of this lambda so it can be merged with another lambda that9616  // is loaded later.9617  LambdaDeclarationsForMerging.insert({LambdaInfo, Lambda});9618}9619 9620void ASTReader::LoadSelector(Selector Sel) {9621  // It would be complicated to avoid reading the methods anyway. So don't.9622  ReadMethodPool(Sel);9623}9624 9625void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {9626  assert(ID && "Non-zero identifier ID required");9627  unsigned Index = translateIdentifierIDToIndex(ID).second;9628  assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");9629  IdentifiersLoaded[Index] = II;9630  if (DeserializationListener)9631    DeserializationListener->IdentifierRead(ID, II);9632}9633 9634/// Set the globally-visible declarations associated with the given9635/// identifier.9636///9637/// If the AST reader is currently in a state where the given declaration IDs9638/// cannot safely be resolved, they are queued until it is safe to resolve9639/// them.9640///9641/// \param II an IdentifierInfo that refers to one or more globally-visible9642/// declarations.9643///9644/// \param DeclIDs the set of declaration IDs with the name @p II that are9645/// visible at global scope.9646///9647/// \param Decls if non-null, this vector will be populated with the set of9648/// deserialized declarations. These declarations will not be pushed into9649/// scope.9650void ASTReader::SetGloballyVisibleDecls(9651    IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs,9652    SmallVectorImpl<Decl *> *Decls) {9653  if (NumCurrentElementsDeserializing && !Decls) {9654    PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());9655    return;9656  }9657 9658  for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {9659    if (!SemaObj) {9660      // Queue this declaration so that it will be added to the9661      // translation unit scope and identifier's declaration chain9662      // once a Sema object is known.9663      PreloadedDeclIDs.push_back(DeclIDs[I]);9664      continue;9665    }9666 9667    NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));9668 9669    // If we're simply supposed to record the declarations, do so now.9670    if (Decls) {9671      Decls->push_back(D);9672      continue;9673    }9674 9675    // Introduce this declaration into the translation-unit scope9676    // and add it to the declaration chain for this identifier, so9677    // that (unqualified) name lookup will find it.9678    pushExternalDeclIntoScope(D, II);9679  }9680}9681 9682std::pair<ModuleFile *, unsigned>9683ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {9684  if (ID == 0)9685    return {nullptr, 0};9686 9687  unsigned ModuleFileIndex = ID >> 32;9688  unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(32);9689 9690  assert(ModuleFileIndex && "not translating loaded IdentifierID?");9691  assert(getModuleManager().size() > ModuleFileIndex - 1);9692 9693  ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];9694  assert(LocalID < MF.LocalNumIdentifiers);9695  return {&MF, MF.BaseIdentifierID + LocalID};9696}9697 9698IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {9699  if (ID == 0)9700    return nullptr;9701 9702  if (IdentifiersLoaded.empty()) {9703    Error("no identifier table in AST file");9704    return nullptr;9705  }9706 9707  auto [M, Index] = translateIdentifierIDToIndex(ID);9708  if (!IdentifiersLoaded[Index]) {9709    assert(M != nullptr && "Untranslated Identifier ID?");9710    assert(Index >= M->BaseIdentifierID);9711    unsigned LocalIndex = Index - M->BaseIdentifierID;9712    const unsigned char *Data =9713        M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];9714 9715    ASTIdentifierLookupTrait Trait(*this, *M);9716    auto KeyDataLen = Trait.ReadKeyDataLength(Data);9717    auto Key = Trait.ReadKey(Data, KeyDataLen.first);9718    auto &II = PP.getIdentifierTable().get(Key);9719    IdentifiersLoaded[Index] = &II;9720    bool IsModule = getPreprocessor().getCurrentModule() != nullptr;9721    markIdentifierFromAST(*this, II, IsModule);9722    if (DeserializationListener)9723      DeserializationListener->IdentifierRead(ID, &II);9724  }9725 9726  return IdentifiersLoaded[Index];9727}9728 9729IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {9730  return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));9731}9732 9733IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {9734  if (LocalID < NUM_PREDEF_IDENT_IDS)9735    return LocalID;9736 9737  if (!M.ModuleOffsetMap.empty())9738    ReadModuleOffsetMap(M);9739 9740  unsigned ModuleFileIndex = LocalID >> 32;9741  LocalID &= llvm::maskTrailingOnes<IdentifierID>(32);9742  ModuleFile *MF =9743      ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;9744  assert(MF && "malformed identifier ID encoding?");9745 9746  if (!ModuleFileIndex)9747    LocalID -= NUM_PREDEF_IDENT_IDS;9748 9749  return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;9750}9751 9752std::pair<ModuleFile *, unsigned>9753ASTReader::translateMacroIDToIndex(MacroID ID) const {9754  if (ID == 0)9755    return {nullptr, 0};9756 9757  unsigned ModuleFileIndex = ID >> 32;9758  assert(ModuleFileIndex && "not translating loaded MacroID?");9759  assert(getModuleManager().size() > ModuleFileIndex - 1);9760  ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];9761 9762  unsigned LocalID = ID & llvm::maskTrailingOnes<MacroID>(32);9763  assert(LocalID < MF.LocalNumMacros);9764  return {&MF, MF.BaseMacroID + LocalID};9765}9766 9767MacroInfo *ASTReader::getMacro(MacroID ID) {9768  if (ID == 0)9769    return nullptr;9770 9771  if (MacrosLoaded.empty()) {9772    Error("no macro table in AST file");9773    return nullptr;9774  }9775 9776  auto [M, Index] = translateMacroIDToIndex(ID);9777  if (!MacrosLoaded[Index]) {9778    assert(M != nullptr && "Untranslated Macro ID?");9779    assert(Index >= M->BaseMacroID);9780    unsigned LocalIndex = Index - M->BaseMacroID;9781    uint64_t DataOffset = M->MacroOffsetsBase + M->MacroOffsets[LocalIndex];9782    MacrosLoaded[Index] = ReadMacroRecord(*M, DataOffset);9783 9784    if (DeserializationListener)9785      DeserializationListener->MacroRead(ID, MacrosLoaded[Index]);9786  }9787 9788  return MacrosLoaded[Index];9789}9790 9791MacroID ASTReader::getGlobalMacroID(ModuleFile &M, MacroID LocalID) {9792  if (LocalID < NUM_PREDEF_MACRO_IDS)9793    return LocalID;9794 9795  if (!M.ModuleOffsetMap.empty())9796    ReadModuleOffsetMap(M);9797 9798  unsigned ModuleFileIndex = LocalID >> 32;9799  LocalID &= llvm::maskTrailingOnes<MacroID>(32);9800  ModuleFile *MF =9801      ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;9802  assert(MF && "malformed identifier ID encoding?");9803 9804  if (!ModuleFileIndex) {9805    assert(LocalID >= NUM_PREDEF_MACRO_IDS);9806    LocalID -= NUM_PREDEF_MACRO_IDS;9807  }9808 9809  return (static_cast<MacroID>(MF->Index + 1) << 32) | LocalID;9810}9811 9812serialization::SubmoduleID9813ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) const {9814  if (LocalID < NUM_PREDEF_SUBMODULE_IDS)9815    return LocalID;9816 9817  if (!M.ModuleOffsetMap.empty())9818    ReadModuleOffsetMap(M);9819 9820  ContinuousRangeMap<uint32_t, int, 2>::iterator I9821    = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);9822  assert(I != M.SubmoduleRemap.end()9823         && "Invalid index into submodule index remap");9824 9825  return LocalID + I->second;9826}9827 9828Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {9829  if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {9830    assert(GlobalID == 0 && "Unhandled global submodule ID");9831    return nullptr;9832  }9833 9834  if (GlobalID > SubmodulesLoaded.size()) {9835    Error("submodule ID out of range in AST file");9836    return nullptr;9837  }9838 9839  return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];9840}9841 9842Module *ASTReader::getModule(unsigned ID) {9843  return getSubmodule(ID);9844}9845 9846ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &M, unsigned ID) const {9847  if (ID & 1) {9848    // It's a module, look it up by submodule ID.9849    auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(M, ID >> 1));9850    return I == GlobalSubmoduleMap.end() ? nullptr : I->second;9851  } else {9852    // It's a prefix (preamble, PCH, ...). Look it up by index.9853   int IndexFromEnd = static_cast<int>(ID >> 1);9854    assert(IndexFromEnd && "got reference to unknown module file");9855    return getModuleManager().pch_modules().end()[-IndexFromEnd];9856  }9857}9858 9859unsigned ASTReader::getModuleFileID(ModuleFile *M) {9860  if (!M)9861    return 1;9862 9863  // For a file representing a module, use the submodule ID of the top-level9864  // module as the file ID. For any other kind of file, the number of such9865  // files loaded beforehand will be the same on reload.9866  // FIXME: Is this true even if we have an explicit module file and a PCH?9867  if (M->isModule())9868    return ((M->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;9869 9870  auto PCHModules = getModuleManager().pch_modules();9871  auto I = llvm::find(PCHModules, M);9872  assert(I != PCHModules.end() && "emitting reference to unknown file");9873  return std::distance(I, PCHModules.end()) << 1;9874}9875 9876std::optional<ASTSourceDescriptor> ASTReader::getSourceDescriptor(unsigned ID) {9877  if (Module *M = getSubmodule(ID))9878    return ASTSourceDescriptor(*M);9879 9880  // If there is only a single PCH, return it instead.9881  // Chained PCH are not supported.9882  const auto &PCHChain = ModuleMgr.pch_modules();9883  if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {9884    ModuleFile &MF = ModuleMgr.getPrimaryModule();9885    StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);9886    StringRef FileName = llvm::sys::path::filename(MF.FileName);9887    return ASTSourceDescriptor(ModuleName,9888                               llvm::sys::path::parent_path(MF.FileName),9889                               FileName, MF.Signature);9890  }9891  return std::nullopt;9892}9893 9894ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {9895  auto I = DefinitionSource.find(FD);9896  if (I == DefinitionSource.end())9897    return EK_ReplyHazy;9898  return I->second ? EK_Never : EK_Always;9899}9900 9901bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {9902  return ThisDeclarationWasADefinitionSet.contains(FD);9903}9904 9905Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {9906  return DecodeSelector(getGlobalSelectorID(M, LocalID));9907}9908 9909Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {9910  if (ID == 0)9911    return Selector();9912 9913  if (ID > SelectorsLoaded.size()) {9914    Error("selector ID out of range in AST file");9915    return Selector();9916  }9917 9918  if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {9919    // Load this selector from the selector table.9920    GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);9921    assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");9922    ModuleFile &M = *I->second;9923    ASTSelectorLookupTrait Trait(*this, M);9924    unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;9925    SelectorsLoaded[ID - 1] =9926      Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);9927    if (DeserializationListener)9928      DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);9929  }9930 9931  return SelectorsLoaded[ID - 1];9932}9933 9934Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {9935  return DecodeSelector(ID);9936}9937 9938uint32_t ASTReader::GetNumExternalSelectors() {9939  // ID 0 (the null selector) is considered an external selector.9940  return getTotalNumSelectors() + 1;9941}9942 9943serialization::SelectorID9944ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {9945  if (LocalID < NUM_PREDEF_SELECTOR_IDS)9946    return LocalID;9947 9948  if (!M.ModuleOffsetMap.empty())9949    ReadModuleOffsetMap(M);9950 9951  ContinuousRangeMap<uint32_t, int, 2>::iterator I9952    = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);9953  assert(I != M.SelectorRemap.end()9954         && "Invalid index into selector index remap");9955 9956  return LocalID + I->second;9957}9958 9959DeclarationNameLoc9960ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {9961  switch (Name.getNameKind()) {9962  case DeclarationName::CXXConstructorName:9963  case DeclarationName::CXXDestructorName:9964  case DeclarationName::CXXConversionFunctionName:9965    return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());9966 9967  case DeclarationName::CXXOperatorName:9968    return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());9969 9970  case DeclarationName::CXXLiteralOperatorName:9971    return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(9972        readSourceLocation());9973 9974  case DeclarationName::Identifier:9975  case DeclarationName::ObjCZeroArgSelector:9976  case DeclarationName::ObjCOneArgSelector:9977  case DeclarationName::ObjCMultiArgSelector:9978  case DeclarationName::CXXUsingDirective:9979  case DeclarationName::CXXDeductionGuideName:9980    break;9981  }9982  return DeclarationNameLoc();9983}9984 9985DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {9986  DeclarationNameInfo NameInfo;9987  NameInfo.setName(readDeclarationName());9988  NameInfo.setLoc(readSourceLocation());9989  NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));9990  return NameInfo;9991}9992 9993TypeCoupledDeclRefInfo ASTRecordReader::readTypeCoupledDeclRefInfo() {9994  return TypeCoupledDeclRefInfo(readDeclAs<ValueDecl>(), readBool());9995}9996 9997SpirvOperand ASTRecordReader::readHLSLSpirvOperand() {9998  auto Kind = readInt();9999  auto ResultType = readQualType();10000  auto Value = readAPInt();10001  SpirvOperand Op(SpirvOperand::SpirvOperandKind(Kind), ResultType, Value);10002  assert(Op.isValid());10003  return Op;10004}10005 10006void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {10007  Info.QualifierLoc = readNestedNameSpecifierLoc();10008  unsigned NumTPLists = readInt();10009  Info.NumTemplParamLists = NumTPLists;10010  if (NumTPLists) {10011    Info.TemplParamLists =10012        new (getContext()) TemplateParameterList *[NumTPLists];10013    for (unsigned i = 0; i != NumTPLists; ++i)10014      Info.TemplParamLists[i] = readTemplateParameterList();10015  }10016}10017 10018TemplateParameterList *10019ASTRecordReader::readTemplateParameterList() {10020  SourceLocation TemplateLoc = readSourceLocation();10021  SourceLocation LAngleLoc = readSourceLocation();10022  SourceLocation RAngleLoc = readSourceLocation();10023 10024  unsigned NumParams = readInt();10025  SmallVector<NamedDecl *, 16> Params;10026  Params.reserve(NumParams);10027  while (NumParams--)10028    Params.push_back(readDeclAs<NamedDecl>());10029 10030  bool HasRequiresClause = readBool();10031  Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;10032 10033  TemplateParameterList *TemplateParams = TemplateParameterList::Create(10034      getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);10035  return TemplateParams;10036}10037 10038void ASTRecordReader::readTemplateArgumentList(10039                        SmallVectorImpl<TemplateArgument> &TemplArgs,10040                        bool Canonicalize) {10041  unsigned NumTemplateArgs = readInt();10042  TemplArgs.reserve(NumTemplateArgs);10043  while (NumTemplateArgs--)10044    TemplArgs.push_back(readTemplateArgument(Canonicalize));10045}10046 10047/// Read a UnresolvedSet structure.10048void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {10049  unsigned NumDecls = readInt();10050  Set.reserve(getContext(), NumDecls);10051  while (NumDecls--) {10052    GlobalDeclID ID = readDeclID();10053    AccessSpecifier AS = (AccessSpecifier) readInt();10054    Set.addLazyDecl(getContext(), ID, AS);10055  }10056}10057 10058CXXBaseSpecifier10059ASTRecordReader::readCXXBaseSpecifier() {10060  bool isVirtual = readBool();10061  bool isBaseOfClass = readBool();10062  AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());10063  bool inheritConstructors = readBool();10064  TypeSourceInfo *TInfo = readTypeSourceInfo();10065  SourceRange Range = readSourceRange();10066  SourceLocation EllipsisLoc = readSourceLocation();10067  CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,10068                          EllipsisLoc);10069  Result.setInheritConstructors(inheritConstructors);10070  return Result;10071}10072 10073CXXCtorInitializer **10074ASTRecordReader::readCXXCtorInitializers() {10075  ASTContext &Context = getContext();10076  unsigned NumInitializers = readInt();10077  assert(NumInitializers && "wrote ctor initializers but have no inits");10078  auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];10079  for (unsigned i = 0; i != NumInitializers; ++i) {10080    TypeSourceInfo *TInfo = nullptr;10081    bool IsBaseVirtual = false;10082    FieldDecl *Member = nullptr;10083    IndirectFieldDecl *IndirectMember = nullptr;10084 10085    CtorInitializerType Type = (CtorInitializerType) readInt();10086    switch (Type) {10087    case CTOR_INITIALIZER_BASE:10088      TInfo = readTypeSourceInfo();10089      IsBaseVirtual = readBool();10090      break;10091 10092    case CTOR_INITIALIZER_DELEGATING:10093      TInfo = readTypeSourceInfo();10094      break;10095 10096     case CTOR_INITIALIZER_MEMBER:10097      Member = readDeclAs<FieldDecl>();10098      break;10099 10100     case CTOR_INITIALIZER_INDIRECT_MEMBER:10101      IndirectMember = readDeclAs<IndirectFieldDecl>();10102      break;10103    }10104 10105    SourceLocation MemberOrEllipsisLoc = readSourceLocation();10106    Expr *Init = readExpr();10107    SourceLocation LParenLoc = readSourceLocation();10108    SourceLocation RParenLoc = readSourceLocation();10109 10110    CXXCtorInitializer *BOMInit;10111    if (Type == CTOR_INITIALIZER_BASE)10112      BOMInit = new (Context)10113          CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,10114                             RParenLoc, MemberOrEllipsisLoc);10115    else if (Type == CTOR_INITIALIZER_DELEGATING)10116      BOMInit = new (Context)10117          CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);10118    else if (Member)10119      BOMInit = new (Context)10120          CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,10121                             Init, RParenLoc);10122    else10123      BOMInit = new (Context)10124          CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,10125                             LParenLoc, Init, RParenLoc);10126 10127    if (/*IsWritten*/readBool()) {10128      unsigned SourceOrder = readInt();10129      BOMInit->setSourceOrder(SourceOrder);10130    }10131 10132    CtorInitializers[i] = BOMInit;10133  }10134 10135  return CtorInitializers;10136}10137 10138NestedNameSpecifierLoc10139ASTRecordReader::readNestedNameSpecifierLoc() {10140  ASTContext &Context = getContext();10141  unsigned N = readInt();10142  NestedNameSpecifierLocBuilder Builder;10143  for (unsigned I = 0; I != N; ++I) {10144    auto Kind = readNestedNameSpecifierKind();10145    switch (Kind) {10146    case NestedNameSpecifier::Kind::Namespace: {10147      auto *NS = readDeclAs<NamespaceBaseDecl>();10148      SourceRange Range = readSourceRange();10149      Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());10150      break;10151    }10152 10153    case NestedNameSpecifier::Kind::Type: {10154      TypeSourceInfo *T = readTypeSourceInfo();10155      if (!T)10156        return NestedNameSpecifierLoc();10157      SourceLocation ColonColonLoc = readSourceLocation();10158      Builder.Make(Context, T->getTypeLoc(), ColonColonLoc);10159      break;10160    }10161 10162    case NestedNameSpecifier::Kind::Global: {10163      SourceLocation ColonColonLoc = readSourceLocation();10164      Builder.MakeGlobal(Context, ColonColonLoc);10165      break;10166    }10167 10168    case NestedNameSpecifier::Kind::MicrosoftSuper: {10169      CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();10170      SourceRange Range = readSourceRange();10171      Builder.MakeMicrosoftSuper(Context, RD, Range.getBegin(), Range.getEnd());10172      break;10173    }10174 10175    case NestedNameSpecifier::Kind::Null:10176      llvm_unreachable("unexpected null nested name specifier");10177    }10178  }10179 10180  return Builder.getWithLocInContext(Context);10181}10182 10183SourceRange ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,10184                                       unsigned &Idx) {10185  SourceLocation beg = ReadSourceLocation(F, Record, Idx);10186  SourceLocation end = ReadSourceLocation(F, Record, Idx);10187  return SourceRange(beg, end);10188}10189 10190llvm::BitVector ASTReader::ReadBitVector(const RecordData &Record,10191                                         const StringRef Blob) {10192  unsigned Count = Record[0];10193  const char *Byte = Blob.data();10194  llvm::BitVector Ret = llvm::BitVector(Count, false);10195  for (unsigned I = 0; I < Count; ++Byte)10196    for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)10197      if (*Byte & (1 << Bit))10198        Ret[I] = true;10199  return Ret;10200}10201 10202/// Read a floating-point value10203llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {10204  return llvm::APFloat(Sem, readAPInt());10205}10206 10207// Read a string10208std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {10209  unsigned Len = Record[Idx++];10210  std::string Result(Record.data() + Idx, Record.data() + Idx + Len);10211  Idx += Len;10212  return Result;10213}10214 10215StringRef ASTReader::ReadStringBlob(const RecordDataImpl &Record, unsigned &Idx,10216                                    StringRef &Blob) {10217  unsigned Len = Record[Idx++];10218  StringRef Result = Blob.substr(0, Len);10219  Blob = Blob.substr(Len);10220  return Result;10221}10222 10223std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,10224                                unsigned &Idx) {10225  return ReadPath(F.BaseDirectory, Record, Idx);10226}10227 10228std::string ASTReader::ReadPath(StringRef BaseDirectory,10229                                const RecordData &Record, unsigned &Idx) {10230  std::string Filename = ReadString(Record, Idx);10231  return ResolveImportedPathAndAllocate(PathBuf, Filename, BaseDirectory);10232}10233 10234std::string ASTReader::ReadPathBlob(StringRef BaseDirectory,10235                                    const RecordData &Record, unsigned &Idx,10236                                    StringRef &Blob) {10237  StringRef Filename = ReadStringBlob(Record, Idx, Blob);10238  return ResolveImportedPathAndAllocate(PathBuf, Filename, BaseDirectory);10239}10240 10241VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,10242                                         unsigned &Idx) {10243  unsigned Major = Record[Idx++];10244  unsigned Minor = Record[Idx++];10245  unsigned Subminor = Record[Idx++];10246  if (Minor == 0)10247    return VersionTuple(Major);10248  if (Subminor == 0)10249    return VersionTuple(Major, Minor - 1);10250  return VersionTuple(Major, Minor - 1, Subminor - 1);10251}10252 10253CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,10254                                          const RecordData &Record,10255                                          unsigned &Idx) {10256  CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);10257  return CXXTemporary::Create(getContext(), Decl);10258}10259 10260DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {10261  return Diag(CurrentImportLoc, DiagID);10262}10263 10264DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {10265  return Diags.Report(Loc, DiagID);10266}10267 10268void ASTReader::runWithSufficientStackSpace(SourceLocation Loc,10269                                            llvm::function_ref<void()> Fn) {10270  // When Sema is available, avoid duplicate errors.10271  if (SemaObj) {10272    SemaObj->runWithSufficientStackSpace(Loc, Fn);10273    return;10274  }10275 10276  StackHandler.runWithSufficientStackSpace(Loc, Fn);10277}10278 10279/// Retrieve the identifier table associated with the10280/// preprocessor.10281IdentifierTable &ASTReader::getIdentifierTable() {10282  return PP.getIdentifierTable();10283}10284 10285/// Record that the given ID maps to the given switch-case10286/// statement.10287void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {10288  assert((*CurrSwitchCaseStmts)[ID] == nullptr &&10289         "Already have a SwitchCase with this ID");10290  (*CurrSwitchCaseStmts)[ID] = SC;10291}10292 10293/// Retrieve the switch-case statement with the given ID.10294SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {10295  assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");10296  return (*CurrSwitchCaseStmts)[ID];10297}10298 10299void ASTReader::ClearSwitchCaseIDs() {10300  CurrSwitchCaseStmts->clear();10301}10302 10303void ASTReader::ReadComments() {10304  ASTContext &Context = getContext();10305  std::vector<RawComment *> Comments;10306  for (SmallVectorImpl<std::pair<BitstreamCursor,10307                                 serialization::ModuleFile *>>::iterator10308       I = CommentsCursors.begin(),10309       E = CommentsCursors.end();10310       I != E; ++I) {10311    Comments.clear();10312    BitstreamCursor &Cursor = I->first;10313    serialization::ModuleFile &F = *I->second;10314    SavedStreamPosition SavedPosition(Cursor);10315 10316    RecordData Record;10317    while (true) {10318      Expected<llvm::BitstreamEntry> MaybeEntry =10319          Cursor.advanceSkippingSubblocks(10320              BitstreamCursor::AF_DontPopBlockAtEnd);10321      if (!MaybeEntry) {10322        Error(MaybeEntry.takeError());10323        return;10324      }10325      llvm::BitstreamEntry Entry = MaybeEntry.get();10326 10327      switch (Entry.Kind) {10328      case llvm::BitstreamEntry::SubBlock: // Handled for us already.10329      case llvm::BitstreamEntry::Error:10330        Error("malformed block record in AST file");10331        return;10332      case llvm::BitstreamEntry::EndBlock:10333        goto NextCursor;10334      case llvm::BitstreamEntry::Record:10335        // The interesting case.10336        break;10337      }10338 10339      // Read a record.10340      Record.clear();10341      Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);10342      if (!MaybeComment) {10343        Error(MaybeComment.takeError());10344        return;10345      }10346      switch ((CommentRecordTypes)MaybeComment.get()) {10347      case COMMENTS_RAW_COMMENT: {10348        unsigned Idx = 0;10349        SourceRange SR = ReadSourceRange(F, Record, Idx);10350        RawComment::CommentKind Kind =10351            (RawComment::CommentKind) Record[Idx++];10352        bool IsTrailingComment = Record[Idx++];10353        bool IsAlmostTrailingComment = Record[Idx++];10354        Comments.push_back(new (Context) RawComment(10355            SR, Kind, IsTrailingComment, IsAlmostTrailingComment));10356        break;10357      }10358      }10359    }10360  NextCursor:10361    for (RawComment *C : Comments) {10362      SourceLocation CommentLoc = C->getBeginLoc();10363      if (CommentLoc.isValid()) {10364        FileIDAndOffset Loc = SourceMgr.getDecomposedLoc(CommentLoc);10365        if (Loc.first.isValid())10366          Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);10367      }10368    }10369  }10370}10371 10372void ASTReader::visitInputFileInfos(10373    serialization::ModuleFile &MF, bool IncludeSystem,10374    llvm::function_ref<void(const serialization::InputFileInfo &IFI,10375                            bool IsSystem)>10376        Visitor) {10377  unsigned NumUserInputs = MF.NumUserInputFiles;10378  unsigned NumInputs = MF.InputFilesLoaded.size();10379  assert(NumUserInputs <= NumInputs);10380  unsigned N = IncludeSystem ? NumInputs : NumUserInputs;10381  for (unsigned I = 0; I < N; ++I) {10382    bool IsSystem = I >= NumUserInputs;10383    InputFileInfo IFI = getInputFileInfo(MF, I+1);10384    Visitor(IFI, IsSystem);10385  }10386}10387 10388void ASTReader::visitInputFiles(serialization::ModuleFile &MF,10389                                bool IncludeSystem, bool Complain,10390                    llvm::function_ref<void(const serialization::InputFile &IF,10391                                            bool isSystem)> Visitor) {10392  unsigned NumUserInputs = MF.NumUserInputFiles;10393  unsigned NumInputs = MF.InputFilesLoaded.size();10394  assert(NumUserInputs <= NumInputs);10395  unsigned N = IncludeSystem ? NumInputs : NumUserInputs;10396  for (unsigned I = 0; I < N; ++I) {10397    bool IsSystem = I >= NumUserInputs;10398    InputFile IF = getInputFile(MF, I+1, Complain);10399    Visitor(IF, IsSystem);10400  }10401}10402 10403void ASTReader::visitTopLevelModuleMaps(10404    serialization::ModuleFile &MF,10405    llvm::function_ref<void(FileEntryRef FE)> Visitor) {10406  unsigned NumInputs = MF.InputFilesLoaded.size();10407  for (unsigned I = 0; I < NumInputs; ++I) {10408    InputFileInfo IFI = getInputFileInfo(MF, I + 1);10409    if (IFI.TopLevel && IFI.ModuleMap)10410      if (auto FE = getInputFile(MF, I + 1).getFile())10411        Visitor(*FE);10412  }10413}10414 10415void ASTReader::finishPendingActions() {10416  while (!PendingIdentifierInfos.empty() ||10417         !PendingDeducedFunctionTypes.empty() ||10418         !PendingDeducedVarTypes.empty() || !PendingDeclChains.empty() ||10419         !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||10420         !PendingUpdateRecords.empty() ||10421         !PendingObjCExtensionIvarRedeclarations.empty()) {10422    // If any identifiers with corresponding top-level declarations have10423    // been loaded, load those declarations now.10424    using TopLevelDeclsMap =10425        llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;10426    TopLevelDeclsMap TopLevelDecls;10427 10428    while (!PendingIdentifierInfos.empty()) {10429      IdentifierInfo *II = PendingIdentifierInfos.back().first;10430      SmallVector<GlobalDeclID, 4> DeclIDs =10431          std::move(PendingIdentifierInfos.back().second);10432      PendingIdentifierInfos.pop_back();10433 10434      SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);10435    }10436 10437    // Load each function type that we deferred loading because it was a10438    // deduced type that might refer to a local type declared within itself.10439    for (unsigned I = 0; I != PendingDeducedFunctionTypes.size(); ++I) {10440      auto *FD = PendingDeducedFunctionTypes[I].first;10441      FD->setType(GetType(PendingDeducedFunctionTypes[I].second));10442 10443      if (auto *DT = FD->getReturnType()->getContainedDeducedType()) {10444        // If we gave a function a deduced return type, remember that we need to10445        // propagate that along the redeclaration chain.10446        if (DT->isDeduced()) {10447          PendingDeducedTypeUpdates.insert(10448              {FD->getCanonicalDecl(), FD->getReturnType()});10449          continue;10450        }10451 10452        // The function has undeduced DeduceType return type. We hope we can10453        // find the deduced type by iterating the redecls in other modules10454        // later.10455        PendingUndeducedFunctionDecls.push_back(FD);10456        continue;10457      }10458    }10459    PendingDeducedFunctionTypes.clear();10460 10461    // Load each variable type that we deferred loading because it was a10462    // deduced type that might refer to a local type declared within itself.10463    for (unsigned I = 0; I != PendingDeducedVarTypes.size(); ++I) {10464      auto *VD = PendingDeducedVarTypes[I].first;10465      VD->setType(GetType(PendingDeducedVarTypes[I].second));10466    }10467    PendingDeducedVarTypes.clear();10468 10469    // Load pending declaration chains.10470    for (unsigned I = 0; I != PendingDeclChains.size(); ++I)10471      loadPendingDeclChain(PendingDeclChains[I].first,10472                           PendingDeclChains[I].second);10473    PendingDeclChains.clear();10474 10475    // Make the most recent of the top-level declarations visible.10476    for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),10477           TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {10478      IdentifierInfo *II = TLD->first;10479      for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {10480        pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);10481      }10482    }10483 10484    // Load any pending macro definitions.10485    for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {10486      IdentifierInfo *II = PendingMacroIDs.begin()[I].first;10487      SmallVector<PendingMacroInfo, 2> GlobalIDs;10488      GlobalIDs.swap(PendingMacroIDs.begin()[I].second);10489      // Initialize the macro history from chained-PCHs ahead of module imports.10490      for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;10491           ++IDIdx) {10492        const PendingMacroInfo &Info = GlobalIDs[IDIdx];10493        if (!Info.M->isModule())10494          resolvePendingMacro(II, Info);10495      }10496      // Handle module imports.10497      for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;10498           ++IDIdx) {10499        const PendingMacroInfo &Info = GlobalIDs[IDIdx];10500        if (Info.M->isModule())10501          resolvePendingMacro(II, Info);10502      }10503    }10504    PendingMacroIDs.clear();10505 10506    // Wire up the DeclContexts for Decls that we delayed setting until10507    // recursive loading is completed.10508    while (!PendingDeclContextInfos.empty()) {10509      PendingDeclContextInfo Info = PendingDeclContextInfos.front();10510      PendingDeclContextInfos.pop_front();10511      DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));10512      DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));10513      Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());10514    }10515 10516    // Perform any pending declaration updates.10517    while (!PendingUpdateRecords.empty()) {10518      auto Update = PendingUpdateRecords.pop_back_val();10519      ReadingKindTracker ReadingKind(Read_Decl, *this);10520      loadDeclUpdateRecords(Update);10521    }10522 10523    while (!PendingObjCExtensionIvarRedeclarations.empty()) {10524      auto ExtensionsPair = PendingObjCExtensionIvarRedeclarations.back().first;10525      auto DuplicateIvars =10526          PendingObjCExtensionIvarRedeclarations.back().second;10527      StructuralEquivalenceContext::NonEquivalentDeclSet NonEquivalentDecls;10528      StructuralEquivalenceContext Ctx(10529          ContextObj->getLangOpts(), ExtensionsPair.first->getASTContext(),10530          ExtensionsPair.second->getASTContext(), NonEquivalentDecls,10531          StructuralEquivalenceKind::Default, /*StrictTypeSpelling =*/false,10532          /*Complain =*/false,10533          /*ErrorOnTagTypeMismatch =*/true);10534      if (Ctx.IsEquivalent(ExtensionsPair.first, ExtensionsPair.second)) {10535        // Merge redeclared ivars with their predecessors.10536        for (auto IvarPair : DuplicateIvars) {10537          ObjCIvarDecl *Ivar = IvarPair.first, *PrevIvar = IvarPair.second;10538          // Change semantic DeclContext but keep the lexical one.10539          Ivar->setDeclContextsImpl(PrevIvar->getDeclContext(),10540                                    Ivar->getLexicalDeclContext(),10541                                    getContext());10542          getContext().setPrimaryMergedDecl(Ivar, PrevIvar->getCanonicalDecl());10543        }10544        // Invalidate duplicate extension and the cached ivar list.10545        ExtensionsPair.first->setInvalidDecl();10546        ExtensionsPair.second->getClassInterface()10547            ->getDefinition()10548            ->setIvarList(nullptr);10549      } else {10550        for (auto IvarPair : DuplicateIvars) {10551          Diag(IvarPair.first->getLocation(),10552               diag::err_duplicate_ivar_declaration)10553              << IvarPair.first->getIdentifier();10554          Diag(IvarPair.second->getLocation(), diag::note_previous_definition);10555        }10556      }10557      PendingObjCExtensionIvarRedeclarations.pop_back();10558    }10559  }10560 10561  // At this point, all update records for loaded decls are in place, so any10562  // fake class definitions should have become real.10563  assert(PendingFakeDefinitionData.empty() &&10564         "faked up a class definition but never saw the real one");10565 10566  // If we deserialized any C++ or Objective-C class definitions, any10567  // Objective-C protocol definitions, or any redeclarable templates, make sure10568  // that all redeclarations point to the definitions. Note that this can only10569  // happen now, after the redeclaration chains have been fully wired.10570  for (Decl *D : PendingDefinitions) {10571    if (TagDecl *TD = dyn_cast<TagDecl>(D)) {10572      if (auto *RD = dyn_cast<CXXRecordDecl>(TD)) {10573        for (auto *R = getMostRecentExistingDecl(RD); R;10574             R = R->getPreviousDecl()) {10575          assert((R == D) ==10576                     cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&10577                 "declaration thinks it's the definition but it isn't");10578          cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;10579        }10580      }10581 10582      continue;10583    }10584 10585    if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {10586      // Make sure that the ObjCInterfaceType points at the definition.10587      const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))10588        ->Decl = ID;10589 10590      for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())10591        cast<ObjCInterfaceDecl>(R)->Data = ID->Data;10592 10593      continue;10594    }10595 10596    if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {10597      for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())10598        cast<ObjCProtocolDecl>(R)->Data = PD->Data;10599 10600      continue;10601    }10602 10603    auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();10604    for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())10605      cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;10606  }10607  PendingDefinitions.clear();10608 10609  for (auto [D, Previous] : PendingWarningForDuplicatedDefsInModuleUnits) {10610    auto hasDefinitionImpl = [this](Decl *D, auto hasDefinitionImpl) {10611      if (auto *VD = dyn_cast<VarDecl>(D))10612        return VD->isThisDeclarationADefinition() ||10613               VD->isThisDeclarationADemotedDefinition();10614 10615      if (auto *TD = dyn_cast<TagDecl>(D))10616        return TD->isThisDeclarationADefinition() ||10617               TD->isThisDeclarationADemotedDefinition();10618 10619      if (auto *FD = dyn_cast<FunctionDecl>(D))10620        return FD->isThisDeclarationADefinition() || PendingBodies.count(FD);10621 10622      if (auto *RTD = dyn_cast<RedeclarableTemplateDecl>(D))10623        return hasDefinitionImpl(RTD->getTemplatedDecl(), hasDefinitionImpl);10624 10625      // Conservatively return false here.10626      return false;10627    };10628 10629    auto hasDefinition = [&hasDefinitionImpl](Decl *D) {10630      return hasDefinitionImpl(D, hasDefinitionImpl);10631    };10632 10633    // It is not good to prevent multiple declarations since the forward10634    // declaration is common. Let's try to avoid duplicated definitions10635    // only.10636    if (!hasDefinition(D) || !hasDefinition(Previous))10637      continue;10638 10639    Module *PM = Previous->getOwningModule();10640    Module *DM = D->getOwningModule();10641    Diag(D->getLocation(), diag::warn_decls_in_multiple_modules)10642        << cast<NamedDecl>(Previous) << PM->getTopLevelModuleName()10643        << (DM ? DM->getTopLevelModuleName() : "global module");10644    Diag(Previous->getLocation(), diag::note_also_found);10645  }10646  PendingWarningForDuplicatedDefsInModuleUnits.clear();10647 10648  // Load the bodies of any functions or methods we've encountered. We do10649  // this now (delayed) so that we can be sure that the declaration chains10650  // have been fully wired up (hasBody relies on this).10651  // FIXME: We shouldn't require complete redeclaration chains here.10652  for (PendingBodiesMap::iterator PB = PendingBodies.begin(),10653                               PBEnd = PendingBodies.end();10654       PB != PBEnd; ++PB) {10655    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {10656      // FIXME: Check for =delete/=default?10657      const FunctionDecl *Defn = nullptr;10658      if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {10659        FD->setLazyBody(PB->second);10660      } else {10661        auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);10662        mergeDefinitionVisibility(NonConstDefn, FD);10663 10664        if (!FD->isLateTemplateParsed() &&10665            !NonConstDefn->isLateTemplateParsed() &&10666            // We only perform ODR checks for decls not in the explicit10667            // global module fragment.10668            !shouldSkipCheckingODR(FD) &&10669            !shouldSkipCheckingODR(NonConstDefn) &&10670            FD->getODRHash() != NonConstDefn->getODRHash()) {10671          if (!isa<CXXMethodDecl>(FD)) {10672            PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);10673          } else if (FD->getLexicalParent()->isFileContext() &&10674                     NonConstDefn->getLexicalParent()->isFileContext()) {10675            // Only diagnose out-of-line method definitions.  If they are10676            // in class definitions, then an error will be generated when10677            // processing the class bodies.10678            PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);10679          }10680        }10681      }10682      continue;10683    }10684 10685    ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);10686    if (!getContext().getLangOpts().Modules || !MD->hasBody())10687      MD->setLazyBody(PB->second);10688  }10689  PendingBodies.clear();10690 10691  // Inform any classes that had members added that they now have more members.10692  for (auto [RD, MD] : PendingAddedClassMembers) {10693    RD->addedMember(MD);10694  }10695  PendingAddedClassMembers.clear();10696 10697  // Do some cleanup.10698  for (auto *ND : PendingMergedDefinitionsToDeduplicate)10699    getContext().deduplicateMergedDefinitionsFor(ND);10700  PendingMergedDefinitionsToDeduplicate.clear();10701 10702  // For each decl chain that we wanted to complete while deserializing, mark10703  // it as "still needs to be completed".10704  for (Decl *D : PendingIncompleteDeclChains)10705    markIncompleteDeclChain(D);10706  PendingIncompleteDeclChains.clear();10707 10708  assert(PendingIdentifierInfos.empty() &&10709         "Should be empty at the end of finishPendingActions");10710  assert(PendingDeducedFunctionTypes.empty() &&10711         "Should be empty at the end of finishPendingActions");10712  assert(PendingDeducedVarTypes.empty() &&10713         "Should be empty at the end of finishPendingActions");10714  assert(PendingDeclChains.empty() &&10715         "Should be empty at the end of finishPendingActions");10716  assert(PendingMacroIDs.empty() &&10717         "Should be empty at the end of finishPendingActions");10718  assert(PendingDeclContextInfos.empty() &&10719         "Should be empty at the end of finishPendingActions");10720  assert(PendingUpdateRecords.empty() &&10721         "Should be empty at the end of finishPendingActions");10722  assert(PendingObjCExtensionIvarRedeclarations.empty() &&10723         "Should be empty at the end of finishPendingActions");10724  assert(PendingFakeDefinitionData.empty() &&10725         "Should be empty at the end of finishPendingActions");10726  assert(PendingDefinitions.empty() &&10727         "Should be empty at the end of finishPendingActions");10728  assert(PendingWarningForDuplicatedDefsInModuleUnits.empty() &&10729         "Should be empty at the end of finishPendingActions");10730  assert(PendingBodies.empty() &&10731         "Should be empty at the end of finishPendingActions");10732  assert(PendingAddedClassMembers.empty() &&10733         "Should be empty at the end of finishPendingActions");10734  assert(PendingMergedDefinitionsToDeduplicate.empty() &&10735         "Should be empty at the end of finishPendingActions");10736  assert(PendingIncompleteDeclChains.empty() &&10737         "Should be empty at the end of finishPendingActions");10738}10739 10740void ASTReader::diagnoseOdrViolations() {10741  if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&10742      PendingRecordOdrMergeFailures.empty() &&10743      PendingFunctionOdrMergeFailures.empty() &&10744      PendingEnumOdrMergeFailures.empty() &&10745      PendingObjCInterfaceOdrMergeFailures.empty() &&10746      PendingObjCProtocolOdrMergeFailures.empty())10747    return;10748 10749  // Trigger the import of the full definition of each class that had any10750  // odr-merging problems, so we can produce better diagnostics for them.10751  // These updates may in turn find and diagnose some ODR failures, so take10752  // ownership of the set first.10753  auto OdrMergeFailures = std::move(PendingOdrMergeFailures);10754  PendingOdrMergeFailures.clear();10755  for (auto &Merge : OdrMergeFailures) {10756    Merge.first->buildLookup();10757    Merge.first->decls_begin();10758    Merge.first->bases_begin();10759    Merge.first->vbases_begin();10760    for (auto &RecordPair : Merge.second) {10761      auto *RD = RecordPair.first;10762      RD->decls_begin();10763      RD->bases_begin();10764      RD->vbases_begin();10765    }10766  }10767 10768  // Trigger the import of the full definition of each record in C/ObjC.10769  auto RecordOdrMergeFailures = std::move(PendingRecordOdrMergeFailures);10770  PendingRecordOdrMergeFailures.clear();10771  for (auto &Merge : RecordOdrMergeFailures) {10772    Merge.first->decls_begin();10773    for (auto &D : Merge.second)10774      D->decls_begin();10775  }10776 10777  // Trigger the import of the full interface definition.10778  auto ObjCInterfaceOdrMergeFailures =10779      std::move(PendingObjCInterfaceOdrMergeFailures);10780  PendingObjCInterfaceOdrMergeFailures.clear();10781  for (auto &Merge : ObjCInterfaceOdrMergeFailures) {10782    Merge.first->decls_begin();10783    for (auto &InterfacePair : Merge.second)10784      InterfacePair.first->decls_begin();10785  }10786 10787  // Trigger the import of functions.10788  auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);10789  PendingFunctionOdrMergeFailures.clear();10790  for (auto &Merge : FunctionOdrMergeFailures) {10791    Merge.first->buildLookup();10792    Merge.first->decls_begin();10793    Merge.first->getBody();10794    for (auto &FD : Merge.second) {10795      FD->buildLookup();10796      FD->decls_begin();10797      FD->getBody();10798    }10799  }10800 10801  // Trigger the import of enums.10802  auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);10803  PendingEnumOdrMergeFailures.clear();10804  for (auto &Merge : EnumOdrMergeFailures) {10805    Merge.first->decls_begin();10806    for (auto &Enum : Merge.second) {10807      Enum->decls_begin();10808    }10809  }10810 10811  // Trigger the import of the full protocol definition.10812  auto ObjCProtocolOdrMergeFailures =10813      std::move(PendingObjCProtocolOdrMergeFailures);10814  PendingObjCProtocolOdrMergeFailures.clear();10815  for (auto &Merge : ObjCProtocolOdrMergeFailures) {10816    Merge.first->decls_begin();10817    for (auto &ProtocolPair : Merge.second)10818      ProtocolPair.first->decls_begin();10819  }10820 10821  // For each declaration from a merged context, check that the canonical10822  // definition of that context also contains a declaration of the same10823  // entity.10824  //10825  // Caution: this loop does things that might invalidate iterators into10826  // PendingOdrMergeChecks. Don't turn this into a range-based for loop!10827  while (!PendingOdrMergeChecks.empty()) {10828    NamedDecl *D = PendingOdrMergeChecks.pop_back_val();10829 10830    // FIXME: Skip over implicit declarations for now. This matters for things10831    // like implicitly-declared special member functions. This isn't entirely10832    // correct; we can end up with multiple unmerged declarations of the same10833    // implicit entity.10834    if (D->isImplicit())10835      continue;10836 10837    DeclContext *CanonDef = D->getDeclContext();10838 10839    bool Found = false;10840    const Decl *DCanon = D->getCanonicalDecl();10841 10842    for (auto *RI : D->redecls()) {10843      if (RI->getLexicalDeclContext() == CanonDef) {10844        Found = true;10845        break;10846      }10847    }10848    if (Found)10849      continue;10850 10851    // Quick check failed, time to do the slow thing. Note, we can't just10852    // look up the name of D in CanonDef here, because the member that is10853    // in CanonDef might not be found by name lookup (it might have been10854    // replaced by a more recent declaration in the lookup table), and we10855    // can't necessarily find it in the redeclaration chain because it might10856    // be merely mergeable, not redeclarable.10857    llvm::SmallVector<const NamedDecl*, 4> Candidates;10858    for (auto *CanonMember : CanonDef->decls()) {10859      if (CanonMember->getCanonicalDecl() == DCanon) {10860        // This can happen if the declaration is merely mergeable and not10861        // actually redeclarable (we looked for redeclarations earlier).10862        //10863        // FIXME: We should be able to detect this more efficiently, without10864        // pulling in all of the members of CanonDef.10865        Found = true;10866        break;10867      }10868      if (auto *ND = dyn_cast<NamedDecl>(CanonMember))10869        if (ND->getDeclName() == D->getDeclName())10870          Candidates.push_back(ND);10871    }10872 10873    if (!Found) {10874      // The AST doesn't like TagDecls becoming invalid after they've been10875      // completed. We only really need to mark FieldDecls as invalid here.10876      if (!isa<TagDecl>(D))10877        D->setInvalidDecl();10878 10879      // Ensure we don't accidentally recursively enter deserialization while10880      // we're producing our diagnostic.10881      Deserializing RecursionGuard(this);10882 10883      std::string CanonDefModule =10884          ODRDiagsEmitter::getOwningModuleNameForDiagnostic(10885              cast<Decl>(CanonDef));10886      Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)10887        << D << ODRDiagsEmitter::getOwningModuleNameForDiagnostic(D)10888        << CanonDef << CanonDefModule.empty() << CanonDefModule;10889 10890      if (Candidates.empty())10891        Diag(cast<Decl>(CanonDef)->getLocation(),10892             diag::note_module_odr_violation_no_possible_decls) << D;10893      else {10894        for (unsigned I = 0, N = Candidates.size(); I != N; ++I)10895          Diag(Candidates[I]->getLocation(),10896               diag::note_module_odr_violation_possible_decl)10897            << Candidates[I];10898      }10899 10900      DiagnosedOdrMergeFailures.insert(CanonDef);10901    }10902  }10903 10904  if (OdrMergeFailures.empty() && RecordOdrMergeFailures.empty() &&10905      FunctionOdrMergeFailures.empty() && EnumOdrMergeFailures.empty() &&10906      ObjCInterfaceOdrMergeFailures.empty() &&10907      ObjCProtocolOdrMergeFailures.empty())10908    return;10909 10910  ODRDiagsEmitter DiagsEmitter(Diags, getContext(),10911                               getPreprocessor().getLangOpts());10912 10913  // Issue any pending ODR-failure diagnostics.10914  for (auto &Merge : OdrMergeFailures) {10915    // If we've already pointed out a specific problem with this class, don't10916    // bother issuing a general "something's different" diagnostic.10917    if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)10918      continue;10919 10920    bool Diagnosed = false;10921    CXXRecordDecl *FirstRecord = Merge.first;10922    for (auto &RecordPair : Merge.second) {10923      if (DiagsEmitter.diagnoseMismatch(FirstRecord, RecordPair.first,10924                                        RecordPair.second)) {10925        Diagnosed = true;10926        break;10927      }10928    }10929 10930    if (!Diagnosed) {10931      // All definitions are updates to the same declaration. This happens if a10932      // module instantiates the declaration of a class template specialization10933      // and two or more other modules instantiate its definition.10934      //10935      // FIXME: Indicate which modules had instantiations of this definition.10936      // FIXME: How can this even happen?10937      Diag(Merge.first->getLocation(),10938           diag::err_module_odr_violation_different_instantiations)10939          << Merge.first;10940    }10941  }10942 10943  // Issue any pending ODR-failure diagnostics for RecordDecl in C/ObjC. Note10944  // that in C++ this is done as a part of CXXRecordDecl ODR checking.10945  for (auto &Merge : RecordOdrMergeFailures) {10946    // If we've already pointed out a specific problem with this class, don't10947    // bother issuing a general "something's different" diagnostic.10948    if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)10949      continue;10950 10951    RecordDecl *FirstRecord = Merge.first;10952    bool Diagnosed = false;10953    for (auto *SecondRecord : Merge.second) {10954      if (DiagsEmitter.diagnoseMismatch(FirstRecord, SecondRecord)) {10955        Diagnosed = true;10956        break;10957      }10958    }10959    (void)Diagnosed;10960    assert(Diagnosed && "Unable to emit ODR diagnostic.");10961  }10962 10963  // Issue ODR failures diagnostics for functions.10964  for (auto &Merge : FunctionOdrMergeFailures) {10965    FunctionDecl *FirstFunction = Merge.first;10966    bool Diagnosed = false;10967    for (auto &SecondFunction : Merge.second) {10968      if (DiagsEmitter.diagnoseMismatch(FirstFunction, SecondFunction)) {10969        Diagnosed = true;10970        break;10971      }10972    }10973    (void)Diagnosed;10974    assert(Diagnosed && "Unable to emit ODR diagnostic.");10975  }10976 10977  // Issue ODR failures diagnostics for enums.10978  for (auto &Merge : EnumOdrMergeFailures) {10979    // If we've already pointed out a specific problem with this enum, don't10980    // bother issuing a general "something's different" diagnostic.10981    if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)10982      continue;10983 10984    EnumDecl *FirstEnum = Merge.first;10985    bool Diagnosed = false;10986    for (auto &SecondEnum : Merge.second) {10987      if (DiagsEmitter.diagnoseMismatch(FirstEnum, SecondEnum)) {10988        Diagnosed = true;10989        break;10990      }10991    }10992    (void)Diagnosed;10993    assert(Diagnosed && "Unable to emit ODR diagnostic.");10994  }10995 10996  for (auto &Merge : ObjCInterfaceOdrMergeFailures) {10997    // If we've already pointed out a specific problem with this interface,10998    // don't bother issuing a general "something's different" diagnostic.10999    if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)11000      continue;11001 11002    bool Diagnosed = false;11003    ObjCInterfaceDecl *FirstID = Merge.first;11004    for (auto &InterfacePair : Merge.second) {11005      if (DiagsEmitter.diagnoseMismatch(FirstID, InterfacePair.first,11006                                        InterfacePair.second)) {11007        Diagnosed = true;11008        break;11009      }11010    }11011    (void)Diagnosed;11012    assert(Diagnosed && "Unable to emit ODR diagnostic.");11013  }11014 11015  for (auto &Merge : ObjCProtocolOdrMergeFailures) {11016    // If we've already pointed out a specific problem with this protocol,11017    // don't bother issuing a general "something's different" diagnostic.11018    if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)11019      continue;11020 11021    ObjCProtocolDecl *FirstProtocol = Merge.first;11022    bool Diagnosed = false;11023    for (auto &ProtocolPair : Merge.second) {11024      if (DiagsEmitter.diagnoseMismatch(FirstProtocol, ProtocolPair.first,11025                                        ProtocolPair.second)) {11026        Diagnosed = true;11027        break;11028      }11029    }11030    (void)Diagnosed;11031    assert(Diagnosed && "Unable to emit ODR diagnostic.");11032  }11033}11034 11035void ASTReader::StartedDeserializing() {11036  if (llvm::Timer *T = ReadTimer.get();11037      ++NumCurrentElementsDeserializing == 1 && T)11038    ReadTimeRegion.emplace(T);11039}11040 11041void ASTReader::FinishedDeserializing() {11042  assert(NumCurrentElementsDeserializing &&11043         "FinishedDeserializing not paired with StartedDeserializing");11044  if (NumCurrentElementsDeserializing == 1) {11045    // We decrease NumCurrentElementsDeserializing only after pending actions11046    // are finished, to avoid recursively re-calling finishPendingActions().11047    finishPendingActions();11048  }11049  --NumCurrentElementsDeserializing;11050 11051  if (NumCurrentElementsDeserializing == 0) {11052    {11053      // Guard variable to avoid recursively entering the process of passing11054      // decls to consumer.11055      SaveAndRestore GuardPassingDeclsToConsumer(CanPassDeclsToConsumer,11056                                                 /*NewValue=*/false);11057 11058      // Propagate exception specification and deduced type updates along11059      // redeclaration chains.11060      //11061      // We do this now rather than in finishPendingActions because we want to11062      // be able to walk the complete redeclaration chains of the updated decls.11063      while (!PendingExceptionSpecUpdates.empty() ||11064             !PendingDeducedTypeUpdates.empty() ||11065             !PendingUndeducedFunctionDecls.empty()) {11066        auto ESUpdates = std::move(PendingExceptionSpecUpdates);11067        PendingExceptionSpecUpdates.clear();11068        for (auto Update : ESUpdates) {11069          ProcessingUpdatesRAIIObj ProcessingUpdates(*this);11070          auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();11071          auto ESI = FPT->getExtProtoInfo().ExceptionSpec;11072          if (auto *Listener = getContext().getASTMutationListener())11073            Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));11074          for (auto *Redecl : Update.second->redecls())11075            getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);11076        }11077 11078        auto DTUpdates = std::move(PendingDeducedTypeUpdates);11079        PendingDeducedTypeUpdates.clear();11080        for (auto Update : DTUpdates) {11081          ProcessingUpdatesRAIIObj ProcessingUpdates(*this);11082          // FIXME: If the return type is already deduced, check that it11083          // matches.11084          getContext().adjustDeducedFunctionResultType(Update.first,11085                                                       Update.second);11086        }11087 11088        auto UDTUpdates = std::move(PendingUndeducedFunctionDecls);11089        PendingUndeducedFunctionDecls.clear();11090        // We hope we can find the deduced type for the functions by iterating11091        // redeclarations in other modules.11092        for (FunctionDecl *UndeducedFD : UDTUpdates)11093          (void)UndeducedFD->getMostRecentDecl();11094      }11095 11096      ReadTimeRegion.reset();11097 11098      diagnoseOdrViolations();11099    }11100 11101    // We are not in recursive loading, so it's safe to pass the "interesting"11102    // decls to the consumer.11103    if (Consumer)11104      PassInterestingDeclsToConsumer();11105  }11106}11107 11108void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {11109  if (const IdentifierInfo *II = Name.getAsIdentifierInfo()) {11110    // Remove any fake results before adding any real ones.11111    auto It = PendingFakeLookupResults.find(II);11112    if (It != PendingFakeLookupResults.end()) {11113      for (auto *ND : It->second)11114        SemaObj->IdResolver.RemoveDecl(ND);11115      // FIXME: this works around module+PCH performance issue.11116      // Rather than erase the result from the map, which is O(n), just clear11117      // the vector of NamedDecls.11118      It->second.clear();11119    }11120  }11121 11122  if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {11123    SemaObj->TUScope->AddDecl(D);11124  } else if (SemaObj->TUScope) {11125    // Adding the decl to IdResolver may have failed because it was already in11126    // (even though it was not added in scope). If it is already in, make sure11127    // it gets in the scope as well.11128    if (llvm::is_contained(SemaObj->IdResolver.decls(Name), D))11129      SemaObj->TUScope->AddDecl(D);11130  }11131}11132 11133ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache,11134                     ASTContext *Context,11135                     const PCHContainerReader &PCHContainerRdr,11136                     const CodeGenOptions &CodeGenOpts,11137                     ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,11138                     StringRef isysroot,11139                     DisableValidationForModuleKind DisableValidationKind,11140                     bool AllowASTWithCompilerErrors,11141                     bool AllowConfigurationMismatch, bool ValidateSystemInputs,11142                     bool ForceValidateUserInputs,11143                     bool ValidateASTInputFilesContent, bool UseGlobalIndex,11144                     std::unique_ptr<llvm::Timer> ReadTimer)11145    : Listener(bool(DisableValidationKind & DisableValidationForModuleKind::PCH)11146                   ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))11147                   : cast<ASTReaderListener>(new PCHValidator(PP, *this))),11148      SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),11149      PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()),11150      StackHandler(Diags), PP(PP), ContextObj(Context),11151      CodeGenOpts(CodeGenOpts),11152      ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr,11153                PP.getHeaderSearchInfo()),11154      DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),11155      DisableValidationKind(DisableValidationKind),11156      AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),11157      AllowConfigurationMismatch(AllowConfigurationMismatch),11158      ValidateSystemInputs(ValidateSystemInputs),11159      ForceValidateUserInputs(ForceValidateUserInputs),11160      ValidateASTInputFilesContent(ValidateASTInputFilesContent),11161      UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {11162  SourceMgr.setExternalSLocEntrySource(this);11163 11164  PathBuf.reserve(256);11165 11166  for (const auto &Ext : Extensions) {11167    auto BlockName = Ext->getExtensionMetadata().BlockName;11168    auto Known = ModuleFileExtensions.find(BlockName);11169    if (Known != ModuleFileExtensions.end()) {11170      Diags.Report(diag::warn_duplicate_module_file_extension)11171        << BlockName;11172      continue;11173    }11174 11175    ModuleFileExtensions.insert({BlockName, Ext});11176  }11177}11178 11179ASTReader::~ASTReader() {11180  if (OwnsDeserializationListener)11181    delete DeserializationListener;11182}11183 11184IdentifierResolver &ASTReader::getIdResolver() {11185  return SemaObj ? SemaObj->IdResolver : DummyIdResolver;11186}11187 11188Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,11189                                               unsigned AbbrevID) {11190  Idx = 0;11191  Record.clear();11192  return Cursor.readRecord(AbbrevID, Record);11193}11194//===----------------------------------------------------------------------===//11195//// OMPClauseReader implementation11196////===----------------------------------------------------------------------===//11197 11198// This has to be in namespace clang because it's friended by all11199// of the OMP clauses.11200namespace clang {11201 11202class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {11203  ASTRecordReader &Record;11204  ASTContext &Context;11205 11206public:11207  OMPClauseReader(ASTRecordReader &Record)11208      : Record(Record), Context(Record.getContext()) {}11209#define GEN_CLANG_CLAUSE_CLASS11210#define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);11211#include "llvm/Frontend/OpenMP/OMP.inc"11212  OMPClause *readClause();11213  void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);11214  void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);11215};11216 11217} // end namespace clang11218 11219OMPClause *ASTRecordReader::readOMPClause() {11220  return OMPClauseReader(*this).readClause();11221}11222 11223OMPClause *OMPClauseReader::readClause() {11224  OMPClause *C = nullptr;11225  switch (llvm::omp::Clause(Record.readInt())) {11226  case llvm::omp::OMPC_if:11227    C = new (Context) OMPIfClause();11228    break;11229  case llvm::omp::OMPC_final:11230    C = new (Context) OMPFinalClause();11231    break;11232  case llvm::omp::OMPC_num_threads:11233    C = new (Context) OMPNumThreadsClause();11234    break;11235  case llvm::omp::OMPC_safelen:11236    C = new (Context) OMPSafelenClause();11237    break;11238  case llvm::omp::OMPC_simdlen:11239    C = new (Context) OMPSimdlenClause();11240    break;11241  case llvm::omp::OMPC_sizes: {11242    unsigned NumSizes = Record.readInt();11243    C = OMPSizesClause::CreateEmpty(Context, NumSizes);11244    break;11245  }11246  case llvm::omp::OMPC_permutation: {11247    unsigned NumLoops = Record.readInt();11248    C = OMPPermutationClause::CreateEmpty(Context, NumLoops);11249    break;11250  }11251  case llvm::omp::OMPC_full:11252    C = OMPFullClause::CreateEmpty(Context);11253    break;11254  case llvm::omp::OMPC_partial:11255    C = OMPPartialClause::CreateEmpty(Context);11256    break;11257  case llvm::omp::OMPC_looprange:11258    C = OMPLoopRangeClause::CreateEmpty(Context);11259    break;11260  case llvm::omp::OMPC_allocator:11261    C = new (Context) OMPAllocatorClause();11262    break;11263  case llvm::omp::OMPC_collapse:11264    C = new (Context) OMPCollapseClause();11265    break;11266  case llvm::omp::OMPC_default:11267    C = new (Context) OMPDefaultClause();11268    break;11269  case llvm::omp::OMPC_proc_bind:11270    C = new (Context) OMPProcBindClause();11271    break;11272  case llvm::omp::OMPC_schedule:11273    C = new (Context) OMPScheduleClause();11274    break;11275  case llvm::omp::OMPC_ordered:11276    C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());11277    break;11278  case llvm::omp::OMPC_nowait:11279    C = new (Context) OMPNowaitClause();11280    break;11281  case llvm::omp::OMPC_untied:11282    C = new (Context) OMPUntiedClause();11283    break;11284  case llvm::omp::OMPC_mergeable:11285    C = new (Context) OMPMergeableClause();11286    break;11287  case llvm::omp::OMPC_threadset:11288    C = new (Context) OMPThreadsetClause();11289    break;11290  case llvm::omp::OMPC_read:11291    C = new (Context) OMPReadClause();11292    break;11293  case llvm::omp::OMPC_write:11294    C = new (Context) OMPWriteClause();11295    break;11296  case llvm::omp::OMPC_update:11297    C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());11298    break;11299  case llvm::omp::OMPC_capture:11300    C = new (Context) OMPCaptureClause();11301    break;11302  case llvm::omp::OMPC_compare:11303    C = new (Context) OMPCompareClause();11304    break;11305  case llvm::omp::OMPC_fail:11306    C = new (Context) OMPFailClause();11307    break;11308  case llvm::omp::OMPC_seq_cst:11309    C = new (Context) OMPSeqCstClause();11310    break;11311  case llvm::omp::OMPC_acq_rel:11312    C = new (Context) OMPAcqRelClause();11313    break;11314  case llvm::omp::OMPC_absent: {11315    unsigned NumKinds = Record.readInt();11316    C = OMPAbsentClause::CreateEmpty(Context, NumKinds);11317    break;11318  }11319  case llvm::omp::OMPC_holds:11320    C = new (Context) OMPHoldsClause();11321    break;11322  case llvm::omp::OMPC_contains: {11323    unsigned NumKinds = Record.readInt();11324    C = OMPContainsClause::CreateEmpty(Context, NumKinds);11325    break;11326  }11327  case llvm::omp::OMPC_no_openmp:11328    C = new (Context) OMPNoOpenMPClause();11329    break;11330  case llvm::omp::OMPC_no_openmp_routines:11331    C = new (Context) OMPNoOpenMPRoutinesClause();11332    break;11333  case llvm::omp::OMPC_no_openmp_constructs:11334    C = new (Context) OMPNoOpenMPConstructsClause();11335    break;11336  case llvm::omp::OMPC_no_parallelism:11337    C = new (Context) OMPNoParallelismClause();11338    break;11339  case llvm::omp::OMPC_acquire:11340    C = new (Context) OMPAcquireClause();11341    break;11342  case llvm::omp::OMPC_release:11343    C = new (Context) OMPReleaseClause();11344    break;11345  case llvm::omp::OMPC_relaxed:11346    C = new (Context) OMPRelaxedClause();11347    break;11348  case llvm::omp::OMPC_weak:11349    C = new (Context) OMPWeakClause();11350    break;11351  case llvm::omp::OMPC_threads:11352    C = new (Context) OMPThreadsClause();11353    break;11354  case llvm::omp::OMPC_simd:11355    C = new (Context) OMPSIMDClause();11356    break;11357  case llvm::omp::OMPC_nogroup:11358    C = new (Context) OMPNogroupClause();11359    break;11360  case llvm::omp::OMPC_unified_address:11361    C = new (Context) OMPUnifiedAddressClause();11362    break;11363  case llvm::omp::OMPC_unified_shared_memory:11364    C = new (Context) OMPUnifiedSharedMemoryClause();11365    break;11366  case llvm::omp::OMPC_reverse_offload:11367    C = new (Context) OMPReverseOffloadClause();11368    break;11369  case llvm::omp::OMPC_dynamic_allocators:11370    C = new (Context) OMPDynamicAllocatorsClause();11371    break;11372  case llvm::omp::OMPC_atomic_default_mem_order:11373    C = new (Context) OMPAtomicDefaultMemOrderClause();11374    break;11375  case llvm::omp::OMPC_self_maps:11376    C = new (Context) OMPSelfMapsClause();11377    break;11378  case llvm::omp::OMPC_at:11379    C = new (Context) OMPAtClause();11380    break;11381  case llvm::omp::OMPC_severity:11382    C = new (Context) OMPSeverityClause();11383    break;11384  case llvm::omp::OMPC_message:11385    C = new (Context) OMPMessageClause();11386    break;11387  case llvm::omp::OMPC_private:11388    C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());11389    break;11390  case llvm::omp::OMPC_firstprivate:11391    C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());11392    break;11393  case llvm::omp::OMPC_lastprivate:11394    C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());11395    break;11396  case llvm::omp::OMPC_shared:11397    C = OMPSharedClause::CreateEmpty(Context, Record.readInt());11398    break;11399  case llvm::omp::OMPC_reduction: {11400    unsigned N = Record.readInt();11401    auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();11402    C = OMPReductionClause::CreateEmpty(Context, N, Modifier);11403    break;11404  }11405  case llvm::omp::OMPC_task_reduction:11406    C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());11407    break;11408  case llvm::omp::OMPC_in_reduction:11409    C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());11410    break;11411  case llvm::omp::OMPC_linear:11412    C = OMPLinearClause::CreateEmpty(Context, Record.readInt());11413    break;11414  case llvm::omp::OMPC_aligned:11415    C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());11416    break;11417  case llvm::omp::OMPC_copyin:11418    C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());11419    break;11420  case llvm::omp::OMPC_copyprivate:11421    C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());11422    break;11423  case llvm::omp::OMPC_flush:11424    C = OMPFlushClause::CreateEmpty(Context, Record.readInt());11425    break;11426  case llvm::omp::OMPC_depobj:11427    C = OMPDepobjClause::CreateEmpty(Context);11428    break;11429  case llvm::omp::OMPC_depend: {11430    unsigned NumVars = Record.readInt();11431    unsigned NumLoops = Record.readInt();11432    C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);11433    break;11434  }11435  case llvm::omp::OMPC_device:11436    C = new (Context) OMPDeviceClause();11437    break;11438  case llvm::omp::OMPC_map: {11439    OMPMappableExprListSizeTy Sizes;11440    Sizes.NumVars = Record.readInt();11441    Sizes.NumUniqueDeclarations = Record.readInt();11442    Sizes.NumComponentLists = Record.readInt();11443    Sizes.NumComponents = Record.readInt();11444    C = OMPMapClause::CreateEmpty(Context, Sizes);11445    break;11446  }11447  case llvm::omp::OMPC_num_teams:11448    C = OMPNumTeamsClause::CreateEmpty(Context, Record.readInt());11449    break;11450  case llvm::omp::OMPC_thread_limit:11451    C = OMPThreadLimitClause::CreateEmpty(Context, Record.readInt());11452    break;11453  case llvm::omp::OMPC_priority:11454    C = new (Context) OMPPriorityClause();11455    break;11456  case llvm::omp::OMPC_grainsize:11457    C = new (Context) OMPGrainsizeClause();11458    break;11459  case llvm::omp::OMPC_num_tasks:11460    C = new (Context) OMPNumTasksClause();11461    break;11462  case llvm::omp::OMPC_hint:11463    C = new (Context) OMPHintClause();11464    break;11465  case llvm::omp::OMPC_dist_schedule:11466    C = new (Context) OMPDistScheduleClause();11467    break;11468  case llvm::omp::OMPC_defaultmap:11469    C = new (Context) OMPDefaultmapClause();11470    break;11471  case llvm::omp::OMPC_to: {11472    OMPMappableExprListSizeTy Sizes;11473    Sizes.NumVars = Record.readInt();11474    Sizes.NumUniqueDeclarations = Record.readInt();11475    Sizes.NumComponentLists = Record.readInt();11476    Sizes.NumComponents = Record.readInt();11477    C = OMPToClause::CreateEmpty(Context, Sizes);11478    break;11479  }11480  case llvm::omp::OMPC_from: {11481    OMPMappableExprListSizeTy Sizes;11482    Sizes.NumVars = Record.readInt();11483    Sizes.NumUniqueDeclarations = Record.readInt();11484    Sizes.NumComponentLists = Record.readInt();11485    Sizes.NumComponents = Record.readInt();11486    C = OMPFromClause::CreateEmpty(Context, Sizes);11487    break;11488  }11489  case llvm::omp::OMPC_use_device_ptr: {11490    OMPMappableExprListSizeTy Sizes;11491    Sizes.NumVars = Record.readInt();11492    Sizes.NumUniqueDeclarations = Record.readInt();11493    Sizes.NumComponentLists = Record.readInt();11494    Sizes.NumComponents = Record.readInt();11495    C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);11496    break;11497  }11498  case llvm::omp::OMPC_use_device_addr: {11499    OMPMappableExprListSizeTy Sizes;11500    Sizes.NumVars = Record.readInt();11501    Sizes.NumUniqueDeclarations = Record.readInt();11502    Sizes.NumComponentLists = Record.readInt();11503    Sizes.NumComponents = Record.readInt();11504    C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);11505    break;11506  }11507  case llvm::omp::OMPC_is_device_ptr: {11508    OMPMappableExprListSizeTy Sizes;11509    Sizes.NumVars = Record.readInt();11510    Sizes.NumUniqueDeclarations = Record.readInt();11511    Sizes.NumComponentLists = Record.readInt();11512    Sizes.NumComponents = Record.readInt();11513    C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);11514    break;11515  }11516  case llvm::omp::OMPC_has_device_addr: {11517    OMPMappableExprListSizeTy Sizes;11518    Sizes.NumVars = Record.readInt();11519    Sizes.NumUniqueDeclarations = Record.readInt();11520    Sizes.NumComponentLists = Record.readInt();11521    Sizes.NumComponents = Record.readInt();11522    C = OMPHasDeviceAddrClause::CreateEmpty(Context, Sizes);11523    break;11524  }11525  case llvm::omp::OMPC_allocate:11526    C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());11527    break;11528  case llvm::omp::OMPC_nontemporal:11529    C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());11530    break;11531  case llvm::omp::OMPC_inclusive:11532    C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());11533    break;11534  case llvm::omp::OMPC_exclusive:11535    C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());11536    break;11537  case llvm::omp::OMPC_order:11538    C = new (Context) OMPOrderClause();11539    break;11540  case llvm::omp::OMPC_init:11541    C = OMPInitClause::CreateEmpty(Context, Record.readInt());11542    break;11543  case llvm::omp::OMPC_use:11544    C = new (Context) OMPUseClause();11545    break;11546  case llvm::omp::OMPC_destroy:11547    C = new (Context) OMPDestroyClause();11548    break;11549  case llvm::omp::OMPC_novariants:11550    C = new (Context) OMPNovariantsClause();11551    break;11552  case llvm::omp::OMPC_nocontext:11553    C = new (Context) OMPNocontextClause();11554    break;11555  case llvm::omp::OMPC_detach:11556    C = new (Context) OMPDetachClause();11557    break;11558  case llvm::omp::OMPC_uses_allocators:11559    C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());11560    break;11561  case llvm::omp::OMPC_affinity:11562    C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());11563    break;11564  case llvm::omp::OMPC_filter:11565    C = new (Context) OMPFilterClause();11566    break;11567  case llvm::omp::OMPC_bind:11568    C = OMPBindClause::CreateEmpty(Context);11569    break;11570  case llvm::omp::OMPC_align:11571    C = new (Context) OMPAlignClause();11572    break;11573  case llvm::omp::OMPC_ompx_dyn_cgroup_mem:11574    C = new (Context) OMPXDynCGroupMemClause();11575    break;11576  case llvm::omp::OMPC_dyn_groupprivate:11577    C = new (Context) OMPDynGroupprivateClause();11578    break;11579  case llvm::omp::OMPC_doacross: {11580    unsigned NumVars = Record.readInt();11581    unsigned NumLoops = Record.readInt();11582    C = OMPDoacrossClause::CreateEmpty(Context, NumVars, NumLoops);11583    break;11584  }11585  case llvm::omp::OMPC_ompx_attribute:11586    C = new (Context) OMPXAttributeClause();11587    break;11588  case llvm::omp::OMPC_ompx_bare:11589    C = new (Context) OMPXBareClause();11590    break;11591#define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \11592  case llvm::omp::Enum:                                                        \11593    break;11594#include "llvm/Frontend/OpenMP/OMPKinds.def"11595  default:11596    break;11597  }11598  assert(C && "Unknown OMPClause type");11599 11600  Visit(C);11601  C->setLocStart(Record.readSourceLocation());11602  C->setLocEnd(Record.readSourceLocation());11603 11604  return C;11605}11606 11607void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {11608  C->setPreInitStmt(Record.readSubStmt(),11609                    static_cast<OpenMPDirectiveKind>(Record.readInt()));11610}11611 11612void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {11613  VisitOMPClauseWithPreInit(C);11614  C->setPostUpdateExpr(Record.readSubExpr());11615}11616 11617void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {11618  VisitOMPClauseWithPreInit(C);11619  C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));11620  C->setNameModifierLoc(Record.readSourceLocation());11621  C->setColonLoc(Record.readSourceLocation());11622  C->setCondition(Record.readSubExpr());11623  C->setLParenLoc(Record.readSourceLocation());11624}11625 11626void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {11627  VisitOMPClauseWithPreInit(C);11628  C->setCondition(Record.readSubExpr());11629  C->setLParenLoc(Record.readSourceLocation());11630}11631 11632void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {11633  VisitOMPClauseWithPreInit(C);11634  C->setModifier(Record.readEnum<OpenMPNumThreadsClauseModifier>());11635  C->setNumThreads(Record.readSubExpr());11636  C->setModifierLoc(Record.readSourceLocation());11637  C->setLParenLoc(Record.readSourceLocation());11638}11639 11640void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {11641  C->setSafelen(Record.readSubExpr());11642  C->setLParenLoc(Record.readSourceLocation());11643}11644 11645void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {11646  C->setSimdlen(Record.readSubExpr());11647  C->setLParenLoc(Record.readSourceLocation());11648}11649 11650void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {11651  for (Expr *&E : C->getSizesRefs())11652    E = Record.readSubExpr();11653  C->setLParenLoc(Record.readSourceLocation());11654}11655 11656void OMPClauseReader::VisitOMPPermutationClause(OMPPermutationClause *C) {11657  for (Expr *&E : C->getArgsRefs())11658    E = Record.readSubExpr();11659  C->setLParenLoc(Record.readSourceLocation());11660}11661 11662void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}11663 11664void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {11665  C->setFactor(Record.readSubExpr());11666  C->setLParenLoc(Record.readSourceLocation());11667}11668 11669void OMPClauseReader::VisitOMPLoopRangeClause(OMPLoopRangeClause *C) {11670  C->setFirst(Record.readSubExpr());11671  C->setCount(Record.readSubExpr());11672  C->setLParenLoc(Record.readSourceLocation());11673  C->setFirstLoc(Record.readSourceLocation());11674  C->setCountLoc(Record.readSourceLocation());11675}11676 11677void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {11678  C->setAllocator(Record.readExpr());11679  C->setLParenLoc(Record.readSourceLocation());11680}11681 11682void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {11683  C->setNumForLoops(Record.readSubExpr());11684  C->setLParenLoc(Record.readSourceLocation());11685}11686 11687void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {11688  C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));11689  C->setLParenLoc(Record.readSourceLocation());11690  C->setDefaultKindKwLoc(Record.readSourceLocation());11691  C->setDefaultVariableCategory(11692      Record.readEnum<OpenMPDefaultClauseVariableCategory>());11693  C->setDefaultVariableCategoryLocation(Record.readSourceLocation());11694}11695 11696// Read the parameter of threadset clause. This will have been saved when11697// OMPClauseWriter is called.11698void OMPClauseReader::VisitOMPThreadsetClause(OMPThreadsetClause *C) {11699  C->setLParenLoc(Record.readSourceLocation());11700  SourceLocation ThreadsetKindLoc = Record.readSourceLocation();11701  C->setThreadsetKindLoc(ThreadsetKindLoc);11702  OpenMPThreadsetKind TKind =11703      static_cast<OpenMPThreadsetKind>(Record.readInt());11704  C->setThreadsetKind(TKind);11705}11706 11707void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {11708  C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));11709  C->setLParenLoc(Record.readSourceLocation());11710  C->setProcBindKindKwLoc(Record.readSourceLocation());11711}11712 11713void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {11714  VisitOMPClauseWithPreInit(C);11715  C->setScheduleKind(11716       static_cast<OpenMPScheduleClauseKind>(Record.readInt()));11717  C->setFirstScheduleModifier(11718      static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));11719  C->setSecondScheduleModifier(11720      static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));11721  C->setChunkSize(Record.readSubExpr());11722  C->setLParenLoc(Record.readSourceLocation());11723  C->setFirstScheduleModifierLoc(Record.readSourceLocation());11724  C->setSecondScheduleModifierLoc(Record.readSourceLocation());11725  C->setScheduleKindLoc(Record.readSourceLocation());11726  C->setCommaLoc(Record.readSourceLocation());11727}11728 11729void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {11730  C->setNumForLoops(Record.readSubExpr());11731  for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)11732    C->setLoopNumIterations(I, Record.readSubExpr());11733  for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)11734    C->setLoopCounter(I, Record.readSubExpr());11735  C->setLParenLoc(Record.readSourceLocation());11736}11737 11738void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {11739  C->setEventHandler(Record.readSubExpr());11740  C->setLParenLoc(Record.readSourceLocation());11741}11742 11743void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *C) {11744  C->setCondition(Record.readSubExpr());11745  C->setLParenLoc(Record.readSourceLocation());11746}11747 11748void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}11749 11750void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}11751 11752void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}11753 11754void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}11755 11756void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {11757  if (C->isExtended()) {11758    C->setLParenLoc(Record.readSourceLocation());11759    C->setArgumentLoc(Record.readSourceLocation());11760    C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());11761  }11762}11763 11764void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}11765 11766void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}11767 11768// Read the parameter of fail clause. This will have been saved when11769// OMPClauseWriter is called.11770void OMPClauseReader::VisitOMPFailClause(OMPFailClause *C) {11771  C->setLParenLoc(Record.readSourceLocation());11772  SourceLocation FailParameterLoc = Record.readSourceLocation();11773  C->setFailParameterLoc(FailParameterLoc);11774  OpenMPClauseKind CKind = Record.readEnum<OpenMPClauseKind>();11775  C->setFailParameter(CKind);11776}11777 11778void OMPClauseReader::VisitOMPAbsentClause(OMPAbsentClause *C) {11779  unsigned Count = C->getDirectiveKinds().size();11780  C->setLParenLoc(Record.readSourceLocation());11781  llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;11782  DKVec.reserve(Count);11783  for (unsigned I = 0; I < Count; I++) {11784    DKVec.push_back(Record.readEnum<OpenMPDirectiveKind>());11785  }11786  C->setDirectiveKinds(DKVec);11787}11788 11789void OMPClauseReader::VisitOMPHoldsClause(OMPHoldsClause *C) {11790  C->setExpr(Record.readExpr());11791  C->setLParenLoc(Record.readSourceLocation());11792}11793 11794void OMPClauseReader::VisitOMPContainsClause(OMPContainsClause *C) {11795  unsigned Count = C->getDirectiveKinds().size();11796  C->setLParenLoc(Record.readSourceLocation());11797  llvm::SmallVector<OpenMPDirectiveKind, 4> DKVec;11798  DKVec.reserve(Count);11799  for (unsigned I = 0; I < Count; I++) {11800    DKVec.push_back(Record.readEnum<OpenMPDirectiveKind>());11801  }11802  C->setDirectiveKinds(DKVec);11803}11804 11805void OMPClauseReader::VisitOMPNoOpenMPClause(OMPNoOpenMPClause *) {}11806 11807void OMPClauseReader::VisitOMPNoOpenMPRoutinesClause(11808    OMPNoOpenMPRoutinesClause *) {}11809 11810void OMPClauseReader::VisitOMPNoOpenMPConstructsClause(11811    OMPNoOpenMPConstructsClause *) {}11812 11813void OMPClauseReader::VisitOMPNoParallelismClause(OMPNoParallelismClause *) {}11814 11815void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}11816 11817void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}11818 11819void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}11820 11821void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}11822 11823void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}11824 11825void OMPClauseReader::VisitOMPWeakClause(OMPWeakClause *) {}11826 11827void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}11828 11829void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}11830 11831void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}11832 11833void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {11834  unsigned NumVars = C->varlist_size();11835  SmallVector<Expr *, 16> Vars;11836  Vars.reserve(NumVars);11837  for (unsigned I = 0; I != NumVars; ++I)11838    Vars.push_back(Record.readSubExpr());11839  C->setVarRefs(Vars);11840  C->setIsTarget(Record.readBool());11841  C->setIsTargetSync(Record.readBool());11842  C->setLParenLoc(Record.readSourceLocation());11843  C->setVarLoc(Record.readSourceLocation());11844}11845 11846void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {11847  C->setInteropVar(Record.readSubExpr());11848  C->setLParenLoc(Record.readSourceLocation());11849  C->setVarLoc(Record.readSourceLocation());11850}11851 11852void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {11853  C->setInteropVar(Record.readSubExpr());11854  C->setLParenLoc(Record.readSourceLocation());11855  C->setVarLoc(Record.readSourceLocation());11856}11857 11858void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {11859  VisitOMPClauseWithPreInit(C);11860  C->setCondition(Record.readSubExpr());11861  C->setLParenLoc(Record.readSourceLocation());11862}11863 11864void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {11865  VisitOMPClauseWithPreInit(C);11866  C->setCondition(Record.readSubExpr());11867  C->setLParenLoc(Record.readSourceLocation());11868}11869 11870void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}11871 11872void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(11873    OMPUnifiedSharedMemoryClause *) {}11874 11875void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}11876 11877void11878OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {11879}11880 11881void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(11882    OMPAtomicDefaultMemOrderClause *C) {11883  C->setAtomicDefaultMemOrderKind(11884      static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));11885  C->setLParenLoc(Record.readSourceLocation());11886  C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());11887}11888 11889void OMPClauseReader::VisitOMPSelfMapsClause(OMPSelfMapsClause *) {}11890 11891void OMPClauseReader::VisitOMPAtClause(OMPAtClause *C) {11892  C->setAtKind(static_cast<OpenMPAtClauseKind>(Record.readInt()));11893  C->setLParenLoc(Record.readSourceLocation());11894  C->setAtKindKwLoc(Record.readSourceLocation());11895}11896 11897void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {11898  C->setSeverityKind(static_cast<OpenMPSeverityClauseKind>(Record.readInt()));11899  C->setLParenLoc(Record.readSourceLocation());11900  C->setSeverityKindKwLoc(Record.readSourceLocation());11901}11902 11903void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {11904  VisitOMPClauseWithPreInit(C);11905  C->setMessageString(Record.readSubExpr());11906  C->setLParenLoc(Record.readSourceLocation());11907}11908 11909void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {11910  C->setLParenLoc(Record.readSourceLocation());11911  unsigned NumVars = C->varlist_size();11912  SmallVector<Expr *, 16> Vars;11913  Vars.reserve(NumVars);11914  for (unsigned i = 0; i != NumVars; ++i)11915    Vars.push_back(Record.readSubExpr());11916  C->setVarRefs(Vars);11917  Vars.clear();11918  for (unsigned i = 0; i != NumVars; ++i)11919    Vars.push_back(Record.readSubExpr());11920  C->setPrivateCopies(Vars);11921}11922 11923void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {11924  VisitOMPClauseWithPreInit(C);11925  C->setLParenLoc(Record.readSourceLocation());11926  unsigned NumVars = C->varlist_size();11927  SmallVector<Expr *, 16> Vars;11928  Vars.reserve(NumVars);11929  for (unsigned i = 0; i != NumVars; ++i)11930    Vars.push_back(Record.readSubExpr());11931  C->setVarRefs(Vars);11932  Vars.clear();11933  for (unsigned i = 0; i != NumVars; ++i)11934    Vars.push_back(Record.readSubExpr());11935  C->setPrivateCopies(Vars);11936  Vars.clear();11937  for (unsigned i = 0; i != NumVars; ++i)11938    Vars.push_back(Record.readSubExpr());11939  C->setInits(Vars);11940}11941 11942void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {11943  VisitOMPClauseWithPostUpdate(C);11944  C->setLParenLoc(Record.readSourceLocation());11945  C->setKind(Record.readEnum<OpenMPLastprivateModifier>());11946  C->setKindLoc(Record.readSourceLocation());11947  C->setColonLoc(Record.readSourceLocation());11948  unsigned NumVars = C->varlist_size();11949  SmallVector<Expr *, 16> Vars;11950  Vars.reserve(NumVars);11951  for (unsigned i = 0; i != NumVars; ++i)11952    Vars.push_back(Record.readSubExpr());11953  C->setVarRefs(Vars);11954  Vars.clear();11955  for (unsigned i = 0; i != NumVars; ++i)11956    Vars.push_back(Record.readSubExpr());11957  C->setPrivateCopies(Vars);11958  Vars.clear();11959  for (unsigned i = 0; i != NumVars; ++i)11960    Vars.push_back(Record.readSubExpr());11961  C->setSourceExprs(Vars);11962  Vars.clear();11963  for (unsigned i = 0; i != NumVars; ++i)11964    Vars.push_back(Record.readSubExpr());11965  C->setDestinationExprs(Vars);11966  Vars.clear();11967  for (unsigned i = 0; i != NumVars; ++i)11968    Vars.push_back(Record.readSubExpr());11969  C->setAssignmentOps(Vars);11970}11971 11972void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {11973  C->setLParenLoc(Record.readSourceLocation());11974  unsigned NumVars = C->varlist_size();11975  SmallVector<Expr *, 16> Vars;11976  Vars.reserve(NumVars);11977  for (unsigned i = 0; i != NumVars; ++i)11978    Vars.push_back(Record.readSubExpr());11979  C->setVarRefs(Vars);11980}11981 11982void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {11983  VisitOMPClauseWithPostUpdate(C);11984  C->setLParenLoc(Record.readSourceLocation());11985  C->setModifierLoc(Record.readSourceLocation());11986  C->setColonLoc(Record.readSourceLocation());11987  NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();11988  DeclarationNameInfo DNI = Record.readDeclarationNameInfo();11989  C->setQualifierLoc(NNSL);11990  C->setNameInfo(DNI);11991 11992  unsigned NumVars = C->varlist_size();11993  SmallVector<Expr *, 16> Vars;11994  Vars.reserve(NumVars);11995  for (unsigned i = 0; i != NumVars; ++i)11996    Vars.push_back(Record.readSubExpr());11997  C->setVarRefs(Vars);11998  Vars.clear();11999  for (unsigned i = 0; i != NumVars; ++i)12000    Vars.push_back(Record.readSubExpr());12001  C->setPrivates(Vars);12002  Vars.clear();12003  for (unsigned i = 0; i != NumVars; ++i)12004    Vars.push_back(Record.readSubExpr());12005  C->setLHSExprs(Vars);12006  Vars.clear();12007  for (unsigned i = 0; i != NumVars; ++i)12008    Vars.push_back(Record.readSubExpr());12009  C->setRHSExprs(Vars);12010  Vars.clear();12011  for (unsigned i = 0; i != NumVars; ++i)12012    Vars.push_back(Record.readSubExpr());12013  C->setReductionOps(Vars);12014  if (C->getModifier() == OMPC_REDUCTION_inscan) {12015    Vars.clear();12016    for (unsigned i = 0; i != NumVars; ++i)12017      Vars.push_back(Record.readSubExpr());12018    C->setInscanCopyOps(Vars);12019    Vars.clear();12020    for (unsigned i = 0; i != NumVars; ++i)12021      Vars.push_back(Record.readSubExpr());12022    C->setInscanCopyArrayTemps(Vars);12023    Vars.clear();12024    for (unsigned i = 0; i != NumVars; ++i)12025      Vars.push_back(Record.readSubExpr());12026    C->setInscanCopyArrayElems(Vars);12027  }12028  unsigned NumFlags = Record.readInt();12029  SmallVector<bool, 16> Flags;12030  Flags.reserve(NumFlags);12031  for ([[maybe_unused]] unsigned I : llvm::seq<unsigned>(NumFlags))12032    Flags.push_back(Record.readInt());12033  C->setPrivateVariableReductionFlags(Flags);12034}12035 12036void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {12037  VisitOMPClauseWithPostUpdate(C);12038  C->setLParenLoc(Record.readSourceLocation());12039  C->setColonLoc(Record.readSourceLocation());12040  NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();12041  DeclarationNameInfo DNI = Record.readDeclarationNameInfo();12042  C->setQualifierLoc(NNSL);12043  C->setNameInfo(DNI);12044 12045  unsigned NumVars = C->varlist_size();12046  SmallVector<Expr *, 16> Vars;12047  Vars.reserve(NumVars);12048  for (unsigned I = 0; I != NumVars; ++I)12049    Vars.push_back(Record.readSubExpr());12050  C->setVarRefs(Vars);12051  Vars.clear();12052  for (unsigned I = 0; I != NumVars; ++I)12053    Vars.push_back(Record.readSubExpr());12054  C->setPrivates(Vars);12055  Vars.clear();12056  for (unsigned I = 0; I != NumVars; ++I)12057    Vars.push_back(Record.readSubExpr());12058  C->setLHSExprs(Vars);12059  Vars.clear();12060  for (unsigned I = 0; I != NumVars; ++I)12061    Vars.push_back(Record.readSubExpr());12062  C->setRHSExprs(Vars);12063  Vars.clear();12064  for (unsigned I = 0; I != NumVars; ++I)12065    Vars.push_back(Record.readSubExpr());12066  C->setReductionOps(Vars);12067}12068 12069void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {12070  VisitOMPClauseWithPostUpdate(C);12071  C->setLParenLoc(Record.readSourceLocation());12072  C->setColonLoc(Record.readSourceLocation());12073  NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();12074  DeclarationNameInfo DNI = Record.readDeclarationNameInfo();12075  C->setQualifierLoc(NNSL);12076  C->setNameInfo(DNI);12077 12078  unsigned NumVars = C->varlist_size();12079  SmallVector<Expr *, 16> Vars;12080  Vars.reserve(NumVars);12081  for (unsigned I = 0; I != NumVars; ++I)12082    Vars.push_back(Record.readSubExpr());12083  C->setVarRefs(Vars);12084  Vars.clear();12085  for (unsigned I = 0; I != NumVars; ++I)12086    Vars.push_back(Record.readSubExpr());12087  C->setPrivates(Vars);12088  Vars.clear();12089  for (unsigned I = 0; I != NumVars; ++I)12090    Vars.push_back(Record.readSubExpr());12091  C->setLHSExprs(Vars);12092  Vars.clear();12093  for (unsigned I = 0; I != NumVars; ++I)12094    Vars.push_back(Record.readSubExpr());12095  C->setRHSExprs(Vars);12096  Vars.clear();12097  for (unsigned I = 0; I != NumVars; ++I)12098    Vars.push_back(Record.readSubExpr());12099  C->setReductionOps(Vars);12100  Vars.clear();12101  for (unsigned I = 0; I != NumVars; ++I)12102    Vars.push_back(Record.readSubExpr());12103  C->setTaskgroupDescriptors(Vars);12104}12105 12106void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {12107  VisitOMPClauseWithPostUpdate(C);12108  C->setLParenLoc(Record.readSourceLocation());12109  C->setColonLoc(Record.readSourceLocation());12110  C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));12111  C->setModifierLoc(Record.readSourceLocation());12112  unsigned NumVars = C->varlist_size();12113  SmallVector<Expr *, 16> Vars;12114  Vars.reserve(NumVars);12115  for (unsigned i = 0; i != NumVars; ++i)12116    Vars.push_back(Record.readSubExpr());12117  C->setVarRefs(Vars);12118  Vars.clear();12119  for (unsigned i = 0; i != NumVars; ++i)12120    Vars.push_back(Record.readSubExpr());12121  C->setPrivates(Vars);12122  Vars.clear();12123  for (unsigned i = 0; i != NumVars; ++i)12124    Vars.push_back(Record.readSubExpr());12125  C->setInits(Vars);12126  Vars.clear();12127  for (unsigned i = 0; i != NumVars; ++i)12128    Vars.push_back(Record.readSubExpr());12129  C->setUpdates(Vars);12130  Vars.clear();12131  for (unsigned i = 0; i != NumVars; ++i)12132    Vars.push_back(Record.readSubExpr());12133  C->setFinals(Vars);12134  C->setStep(Record.readSubExpr());12135  C->setCalcStep(Record.readSubExpr());12136  Vars.clear();12137  for (unsigned I = 0; I != NumVars + 1; ++I)12138    Vars.push_back(Record.readSubExpr());12139  C->setUsedExprs(Vars);12140}12141 12142void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {12143  C->setLParenLoc(Record.readSourceLocation());12144  C->setColonLoc(Record.readSourceLocation());12145  unsigned NumVars = C->varlist_size();12146  SmallVector<Expr *, 16> Vars;12147  Vars.reserve(NumVars);12148  for (unsigned i = 0; i != NumVars; ++i)12149    Vars.push_back(Record.readSubExpr());12150  C->setVarRefs(Vars);12151  C->setAlignment(Record.readSubExpr());12152}12153 12154void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {12155  C->setLParenLoc(Record.readSourceLocation());12156  unsigned NumVars = C->varlist_size();12157  SmallVector<Expr *, 16> Exprs;12158  Exprs.reserve(NumVars);12159  for (unsigned i = 0; i != NumVars; ++i)12160    Exprs.push_back(Record.readSubExpr());12161  C->setVarRefs(Exprs);12162  Exprs.clear();12163  for (unsigned i = 0; i != NumVars; ++i)12164    Exprs.push_back(Record.readSubExpr());12165  C->setSourceExprs(Exprs);12166  Exprs.clear();12167  for (unsigned i = 0; i != NumVars; ++i)12168    Exprs.push_back(Record.readSubExpr());12169  C->setDestinationExprs(Exprs);12170  Exprs.clear();12171  for (unsigned i = 0; i != NumVars; ++i)12172    Exprs.push_back(Record.readSubExpr());12173  C->setAssignmentOps(Exprs);12174}12175 12176void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {12177  C->setLParenLoc(Record.readSourceLocation());12178  unsigned NumVars = C->varlist_size();12179  SmallVector<Expr *, 16> Exprs;12180  Exprs.reserve(NumVars);12181  for (unsigned i = 0; i != NumVars; ++i)12182    Exprs.push_back(Record.readSubExpr());12183  C->setVarRefs(Exprs);12184  Exprs.clear();12185  for (unsigned i = 0; i != NumVars; ++i)12186    Exprs.push_back(Record.readSubExpr());12187  C->setSourceExprs(Exprs);12188  Exprs.clear();12189  for (unsigned i = 0; i != NumVars; ++i)12190    Exprs.push_back(Record.readSubExpr());12191  C->setDestinationExprs(Exprs);12192  Exprs.clear();12193  for (unsigned i = 0; i != NumVars; ++i)12194    Exprs.push_back(Record.readSubExpr());12195  C->setAssignmentOps(Exprs);12196}12197 12198void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {12199  C->setLParenLoc(Record.readSourceLocation());12200  unsigned NumVars = C->varlist_size();12201  SmallVector<Expr *, 16> Vars;12202  Vars.reserve(NumVars);12203  for (unsigned i = 0; i != NumVars; ++i)12204    Vars.push_back(Record.readSubExpr());12205  C->setVarRefs(Vars);12206}12207 12208void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {12209  C->setDepobj(Record.readSubExpr());12210  C->setLParenLoc(Record.readSourceLocation());12211}12212 12213void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {12214  C->setLParenLoc(Record.readSourceLocation());12215  C->setModifier(Record.readSubExpr());12216  C->setDependencyKind(12217      static_cast<OpenMPDependClauseKind>(Record.readInt()));12218  C->setDependencyLoc(Record.readSourceLocation());12219  C->setColonLoc(Record.readSourceLocation());12220  C->setOmpAllMemoryLoc(Record.readSourceLocation());12221  unsigned NumVars = C->varlist_size();12222  SmallVector<Expr *, 16> Vars;12223  Vars.reserve(NumVars);12224  for (unsigned I = 0; I != NumVars; ++I)12225    Vars.push_back(Record.readSubExpr());12226  C->setVarRefs(Vars);12227  for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)12228    C->setLoopData(I, Record.readSubExpr());12229}12230 12231void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {12232  VisitOMPClauseWithPreInit(C);12233  C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());12234  C->setDevice(Record.readSubExpr());12235  C->setModifierLoc(Record.readSourceLocation());12236  C->setLParenLoc(Record.readSourceLocation());12237}12238 12239void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {12240  C->setLParenLoc(Record.readSourceLocation());12241  bool HasIteratorModifier = false;12242  for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {12243    C->setMapTypeModifier(12244        I, static_cast<OpenMPMapModifierKind>(Record.readInt()));12245    C->setMapTypeModifierLoc(I, Record.readSourceLocation());12246    if (C->getMapTypeModifier(I) == OMPC_MAP_MODIFIER_iterator)12247      HasIteratorModifier = true;12248  }12249  C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());12250  C->setMapperIdInfo(Record.readDeclarationNameInfo());12251  C->setMapType(12252     static_cast<OpenMPMapClauseKind>(Record.readInt()));12253  C->setMapLoc(Record.readSourceLocation());12254  C->setColonLoc(Record.readSourceLocation());12255  auto NumVars = C->varlist_size();12256  auto UniqueDecls = C->getUniqueDeclarationsNum();12257  auto TotalLists = C->getTotalComponentListNum();12258  auto TotalComponents = C->getTotalComponentsNum();12259 12260  SmallVector<Expr *, 16> Vars;12261  Vars.reserve(NumVars);12262  for (unsigned i = 0; i != NumVars; ++i)12263    Vars.push_back(Record.readExpr());12264  C->setVarRefs(Vars);12265 12266  SmallVector<Expr *, 16> UDMappers;12267  UDMappers.reserve(NumVars);12268  for (unsigned I = 0; I < NumVars; ++I)12269    UDMappers.push_back(Record.readExpr());12270  C->setUDMapperRefs(UDMappers);12271 12272  if (HasIteratorModifier)12273    C->setIteratorModifier(Record.readExpr());12274 12275  SmallVector<ValueDecl *, 16> Decls;12276  Decls.reserve(UniqueDecls);12277  for (unsigned i = 0; i < UniqueDecls; ++i)12278    Decls.push_back(Record.readDeclAs<ValueDecl>());12279  C->setUniqueDecls(Decls);12280 12281  SmallVector<unsigned, 16> ListsPerDecl;12282  ListsPerDecl.reserve(UniqueDecls);12283  for (unsigned i = 0; i < UniqueDecls; ++i)12284    ListsPerDecl.push_back(Record.readInt());12285  C->setDeclNumLists(ListsPerDecl);12286 12287  SmallVector<unsigned, 32> ListSizes;12288  ListSizes.reserve(TotalLists);12289  for (unsigned i = 0; i < TotalLists; ++i)12290    ListSizes.push_back(Record.readInt());12291  C->setComponentListSizes(ListSizes);12292 12293  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;12294  Components.reserve(TotalComponents);12295  for (unsigned i = 0; i < TotalComponents; ++i) {12296    Expr *AssociatedExprPr = Record.readExpr();12297    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();12298    Components.emplace_back(AssociatedExprPr, AssociatedDecl,12299                            /*IsNonContiguous=*/false);12300  }12301  C->setComponents(Components, ListSizes);12302}12303 12304void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {12305  C->setFirstAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());12306  C->setSecondAllocateModifier(Record.readEnum<OpenMPAllocateClauseModifier>());12307  C->setLParenLoc(Record.readSourceLocation());12308  C->setColonLoc(Record.readSourceLocation());12309  C->setAllocator(Record.readSubExpr());12310  C->setAlignment(Record.readSubExpr());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}12318 12319void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {12320  VisitOMPClauseWithPreInit(C);12321  C->setLParenLoc(Record.readSourceLocation());12322  unsigned NumVars = C->varlist_size();12323  SmallVector<Expr *, 16> Vars;12324  Vars.reserve(NumVars);12325  for (unsigned I = 0; I != NumVars; ++I)12326    Vars.push_back(Record.readSubExpr());12327  C->setVarRefs(Vars);12328}12329 12330void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {12331  VisitOMPClauseWithPreInit(C);12332  C->setLParenLoc(Record.readSourceLocation());12333  unsigned NumVars = C->varlist_size();12334  SmallVector<Expr *, 16> Vars;12335  Vars.reserve(NumVars);12336  for (unsigned I = 0; I != NumVars; ++I)12337    Vars.push_back(Record.readSubExpr());12338  C->setVarRefs(Vars);12339}12340 12341void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {12342  VisitOMPClauseWithPreInit(C);12343  C->setPriority(Record.readSubExpr());12344  C->setLParenLoc(Record.readSourceLocation());12345}12346 12347void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {12348  VisitOMPClauseWithPreInit(C);12349  C->setModifier(Record.readEnum<OpenMPGrainsizeClauseModifier>());12350  C->setGrainsize(Record.readSubExpr());12351  C->setModifierLoc(Record.readSourceLocation());12352  C->setLParenLoc(Record.readSourceLocation());12353}12354 12355void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {12356  VisitOMPClauseWithPreInit(C);12357  C->setModifier(Record.readEnum<OpenMPNumTasksClauseModifier>());12358  C->setNumTasks(Record.readSubExpr());12359  C->setModifierLoc(Record.readSourceLocation());12360  C->setLParenLoc(Record.readSourceLocation());12361}12362 12363void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {12364  C->setHint(Record.readSubExpr());12365  C->setLParenLoc(Record.readSourceLocation());12366}12367 12368void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {12369  VisitOMPClauseWithPreInit(C);12370  C->setDistScheduleKind(12371      static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));12372  C->setChunkSize(Record.readSubExpr());12373  C->setLParenLoc(Record.readSourceLocation());12374  C->setDistScheduleKindLoc(Record.readSourceLocation());12375  C->setCommaLoc(Record.readSourceLocation());12376}12377 12378void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {12379  C->setDefaultmapKind(12380       static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));12381  C->setDefaultmapModifier(12382      static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));12383  C->setLParenLoc(Record.readSourceLocation());12384  C->setDefaultmapModifierLoc(Record.readSourceLocation());12385  C->setDefaultmapKindLoc(Record.readSourceLocation());12386}12387 12388void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {12389  C->setLParenLoc(Record.readSourceLocation());12390  for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {12391    C->setMotionModifier(12392        I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));12393    C->setMotionModifierLoc(I, Record.readSourceLocation());12394    if (C->getMotionModifier(I) == OMPC_MOTION_MODIFIER_iterator)12395      C->setIteratorModifier(Record.readExpr());12396  }12397  C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());12398  C->setMapperIdInfo(Record.readDeclarationNameInfo());12399  C->setColonLoc(Record.readSourceLocation());12400  auto NumVars = C->varlist_size();12401  auto UniqueDecls = C->getUniqueDeclarationsNum();12402  auto TotalLists = C->getTotalComponentListNum();12403  auto TotalComponents = C->getTotalComponentsNum();12404 12405  SmallVector<Expr *, 16> Vars;12406  Vars.reserve(NumVars);12407  for (unsigned i = 0; i != NumVars; ++i)12408    Vars.push_back(Record.readSubExpr());12409  C->setVarRefs(Vars);12410 12411  SmallVector<Expr *, 16> UDMappers;12412  UDMappers.reserve(NumVars);12413  for (unsigned I = 0; I < NumVars; ++I)12414    UDMappers.push_back(Record.readSubExpr());12415  C->setUDMapperRefs(UDMappers);12416 12417  SmallVector<ValueDecl *, 16> Decls;12418  Decls.reserve(UniqueDecls);12419  for (unsigned i = 0; i < UniqueDecls; ++i)12420    Decls.push_back(Record.readDeclAs<ValueDecl>());12421  C->setUniqueDecls(Decls);12422 12423  SmallVector<unsigned, 16> ListsPerDecl;12424  ListsPerDecl.reserve(UniqueDecls);12425  for (unsigned i = 0; i < UniqueDecls; ++i)12426    ListsPerDecl.push_back(Record.readInt());12427  C->setDeclNumLists(ListsPerDecl);12428 12429  SmallVector<unsigned, 32> ListSizes;12430  ListSizes.reserve(TotalLists);12431  for (unsigned i = 0; i < TotalLists; ++i)12432    ListSizes.push_back(Record.readInt());12433  C->setComponentListSizes(ListSizes);12434 12435  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;12436  Components.reserve(TotalComponents);12437  for (unsigned i = 0; i < TotalComponents; ++i) {12438    Expr *AssociatedExprPr = Record.readSubExpr();12439    bool IsNonContiguous = Record.readBool();12440    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();12441    Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);12442  }12443  C->setComponents(Components, ListSizes);12444}12445 12446void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {12447  C->setLParenLoc(Record.readSourceLocation());12448  for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {12449    C->setMotionModifier(12450        I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));12451    C->setMotionModifierLoc(I, Record.readSourceLocation());12452    if (C->getMotionModifier(I) == OMPC_MOTION_MODIFIER_iterator)12453      C->setIteratorModifier(Record.readExpr());12454  }12455  C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());12456  C->setMapperIdInfo(Record.readDeclarationNameInfo());12457  C->setColonLoc(Record.readSourceLocation());12458  auto NumVars = C->varlist_size();12459  auto UniqueDecls = C->getUniqueDeclarationsNum();12460  auto TotalLists = C->getTotalComponentListNum();12461  auto TotalComponents = C->getTotalComponentsNum();12462 12463  SmallVector<Expr *, 16> Vars;12464  Vars.reserve(NumVars);12465  for (unsigned i = 0; i != NumVars; ++i)12466    Vars.push_back(Record.readSubExpr());12467  C->setVarRefs(Vars);12468 12469  SmallVector<Expr *, 16> UDMappers;12470  UDMappers.reserve(NumVars);12471  for (unsigned I = 0; I < NumVars; ++I)12472    UDMappers.push_back(Record.readSubExpr());12473  C->setUDMapperRefs(UDMappers);12474 12475  SmallVector<ValueDecl *, 16> Decls;12476  Decls.reserve(UniqueDecls);12477  for (unsigned i = 0; i < UniqueDecls; ++i)12478    Decls.push_back(Record.readDeclAs<ValueDecl>());12479  C->setUniqueDecls(Decls);12480 12481  SmallVector<unsigned, 16> ListsPerDecl;12482  ListsPerDecl.reserve(UniqueDecls);12483  for (unsigned i = 0; i < UniqueDecls; ++i)12484    ListsPerDecl.push_back(Record.readInt());12485  C->setDeclNumLists(ListsPerDecl);12486 12487  SmallVector<unsigned, 32> ListSizes;12488  ListSizes.reserve(TotalLists);12489  for (unsigned i = 0; i < TotalLists; ++i)12490    ListSizes.push_back(Record.readInt());12491  C->setComponentListSizes(ListSizes);12492 12493  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;12494  Components.reserve(TotalComponents);12495  for (unsigned i = 0; i < TotalComponents; ++i) {12496    Expr *AssociatedExprPr = Record.readSubExpr();12497    bool IsNonContiguous = Record.readBool();12498    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();12499    Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);12500  }12501  C->setComponents(Components, ListSizes);12502}12503 12504void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {12505  C->setLParenLoc(Record.readSourceLocation());12506  auto NumVars = C->varlist_size();12507  auto UniqueDecls = C->getUniqueDeclarationsNum();12508  auto TotalLists = C->getTotalComponentListNum();12509  auto TotalComponents = C->getTotalComponentsNum();12510 12511  SmallVector<Expr *, 16> Vars;12512  Vars.reserve(NumVars);12513  for (unsigned i = 0; i != NumVars; ++i)12514    Vars.push_back(Record.readSubExpr());12515  C->setVarRefs(Vars);12516  Vars.clear();12517  for (unsigned i = 0; i != NumVars; ++i)12518    Vars.push_back(Record.readSubExpr());12519  C->setPrivateCopies(Vars);12520  Vars.clear();12521  for (unsigned i = 0; i != NumVars; ++i)12522    Vars.push_back(Record.readSubExpr());12523  C->setInits(Vars);12524 12525  SmallVector<ValueDecl *, 16> Decls;12526  Decls.reserve(UniqueDecls);12527  for (unsigned i = 0; i < UniqueDecls; ++i)12528    Decls.push_back(Record.readDeclAs<ValueDecl>());12529  C->setUniqueDecls(Decls);12530 12531  SmallVector<unsigned, 16> ListsPerDecl;12532  ListsPerDecl.reserve(UniqueDecls);12533  for (unsigned i = 0; i < UniqueDecls; ++i)12534    ListsPerDecl.push_back(Record.readInt());12535  C->setDeclNumLists(ListsPerDecl);12536 12537  SmallVector<unsigned, 32> ListSizes;12538  ListSizes.reserve(TotalLists);12539  for (unsigned i = 0; i < TotalLists; ++i)12540    ListSizes.push_back(Record.readInt());12541  C->setComponentListSizes(ListSizes);12542 12543  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;12544  Components.reserve(TotalComponents);12545  for (unsigned i = 0; i < TotalComponents; ++i) {12546    auto *AssociatedExprPr = Record.readSubExpr();12547    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();12548    Components.emplace_back(AssociatedExprPr, AssociatedDecl,12549                            /*IsNonContiguous=*/false);12550  }12551  C->setComponents(Components, ListSizes);12552}12553 12554void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {12555  C->setLParenLoc(Record.readSourceLocation());12556  auto NumVars = C->varlist_size();12557  auto UniqueDecls = C->getUniqueDeclarationsNum();12558  auto TotalLists = C->getTotalComponentListNum();12559  auto TotalComponents = C->getTotalComponentsNum();12560 12561  SmallVector<Expr *, 16> Vars;12562  Vars.reserve(NumVars);12563  for (unsigned i = 0; i != NumVars; ++i)12564    Vars.push_back(Record.readSubExpr());12565  C->setVarRefs(Vars);12566 12567  SmallVector<ValueDecl *, 16> Decls;12568  Decls.reserve(UniqueDecls);12569  for (unsigned i = 0; i < UniqueDecls; ++i)12570    Decls.push_back(Record.readDeclAs<ValueDecl>());12571  C->setUniqueDecls(Decls);12572 12573  SmallVector<unsigned, 16> ListsPerDecl;12574  ListsPerDecl.reserve(UniqueDecls);12575  for (unsigned i = 0; i < UniqueDecls; ++i)12576    ListsPerDecl.push_back(Record.readInt());12577  C->setDeclNumLists(ListsPerDecl);12578 12579  SmallVector<unsigned, 32> ListSizes;12580  ListSizes.reserve(TotalLists);12581  for (unsigned i = 0; i < TotalLists; ++i)12582    ListSizes.push_back(Record.readInt());12583  C->setComponentListSizes(ListSizes);12584 12585  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;12586  Components.reserve(TotalComponents);12587  for (unsigned i = 0; i < TotalComponents; ++i) {12588    Expr *AssociatedExpr = Record.readSubExpr();12589    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();12590    Components.emplace_back(AssociatedExpr, AssociatedDecl,12591                            /*IsNonContiguous*/ false);12592  }12593  C->setComponents(Components, ListSizes);12594}12595 12596void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {12597  C->setLParenLoc(Record.readSourceLocation());12598  auto NumVars = C->varlist_size();12599  auto UniqueDecls = C->getUniqueDeclarationsNum();12600  auto TotalLists = C->getTotalComponentListNum();12601  auto TotalComponents = C->getTotalComponentsNum();12602 12603  SmallVector<Expr *, 16> Vars;12604  Vars.reserve(NumVars);12605  for (unsigned i = 0; i != NumVars; ++i)12606    Vars.push_back(Record.readSubExpr());12607  C->setVarRefs(Vars);12608  Vars.clear();12609 12610  SmallVector<ValueDecl *, 16> Decls;12611  Decls.reserve(UniqueDecls);12612  for (unsigned i = 0; i < UniqueDecls; ++i)12613    Decls.push_back(Record.readDeclAs<ValueDecl>());12614  C->setUniqueDecls(Decls);12615 12616  SmallVector<unsigned, 16> ListsPerDecl;12617  ListsPerDecl.reserve(UniqueDecls);12618  for (unsigned i = 0; i < UniqueDecls; ++i)12619    ListsPerDecl.push_back(Record.readInt());12620  C->setDeclNumLists(ListsPerDecl);12621 12622  SmallVector<unsigned, 32> ListSizes;12623  ListSizes.reserve(TotalLists);12624  for (unsigned i = 0; i < TotalLists; ++i)12625    ListSizes.push_back(Record.readInt());12626  C->setComponentListSizes(ListSizes);12627 12628  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;12629  Components.reserve(TotalComponents);12630  for (unsigned i = 0; i < TotalComponents; ++i) {12631    Expr *AssociatedExpr = Record.readSubExpr();12632    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();12633    Components.emplace_back(AssociatedExpr, AssociatedDecl,12634                            /*IsNonContiguous=*/false);12635  }12636  C->setComponents(Components, ListSizes);12637}12638 12639void OMPClauseReader::VisitOMPHasDeviceAddrClause(OMPHasDeviceAddrClause *C) {12640  C->setLParenLoc(Record.readSourceLocation());12641  auto NumVars = C->varlist_size();12642  auto UniqueDecls = C->getUniqueDeclarationsNum();12643  auto TotalLists = C->getTotalComponentListNum();12644  auto TotalComponents = C->getTotalComponentsNum();12645 12646  SmallVector<Expr *, 16> Vars;12647  Vars.reserve(NumVars);12648  for (unsigned I = 0; I != NumVars; ++I)12649    Vars.push_back(Record.readSubExpr());12650  C->setVarRefs(Vars);12651  Vars.clear();12652 12653  SmallVector<ValueDecl *, 16> Decls;12654  Decls.reserve(UniqueDecls);12655  for (unsigned I = 0; I < UniqueDecls; ++I)12656    Decls.push_back(Record.readDeclAs<ValueDecl>());12657  C->setUniqueDecls(Decls);12658 12659  SmallVector<unsigned, 16> ListsPerDecl;12660  ListsPerDecl.reserve(UniqueDecls);12661  for (unsigned I = 0; I < UniqueDecls; ++I)12662    ListsPerDecl.push_back(Record.readInt());12663  C->setDeclNumLists(ListsPerDecl);12664 12665  SmallVector<unsigned, 32> ListSizes;12666  ListSizes.reserve(TotalLists);12667  for (unsigned i = 0; i < TotalLists; ++i)12668    ListSizes.push_back(Record.readInt());12669  C->setComponentListSizes(ListSizes);12670 12671  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;12672  Components.reserve(TotalComponents);12673  for (unsigned I = 0; I < TotalComponents; ++I) {12674    Expr *AssociatedExpr = Record.readSubExpr();12675    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();12676    Components.emplace_back(AssociatedExpr, AssociatedDecl,12677                            /*IsNonContiguous=*/false);12678  }12679  C->setComponents(Components, ListSizes);12680}12681 12682void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {12683  C->setLParenLoc(Record.readSourceLocation());12684  unsigned NumVars = C->varlist_size();12685  SmallVector<Expr *, 16> Vars;12686  Vars.reserve(NumVars);12687  for (unsigned i = 0; i != NumVars; ++i)12688    Vars.push_back(Record.readSubExpr());12689  C->setVarRefs(Vars);12690  Vars.clear();12691  Vars.reserve(NumVars);12692  for (unsigned i = 0; i != NumVars; ++i)12693    Vars.push_back(Record.readSubExpr());12694  C->setPrivateRefs(Vars);12695}12696 12697void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {12698  C->setLParenLoc(Record.readSourceLocation());12699  unsigned NumVars = C->varlist_size();12700  SmallVector<Expr *, 16> Vars;12701  Vars.reserve(NumVars);12702  for (unsigned i = 0; i != NumVars; ++i)12703    Vars.push_back(Record.readSubExpr());12704  C->setVarRefs(Vars);12705}12706 12707void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {12708  C->setLParenLoc(Record.readSourceLocation());12709  unsigned NumVars = C->varlist_size();12710  SmallVector<Expr *, 16> Vars;12711  Vars.reserve(NumVars);12712  for (unsigned i = 0; i != NumVars; ++i)12713    Vars.push_back(Record.readSubExpr());12714  C->setVarRefs(Vars);12715}12716 12717void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {12718  C->setLParenLoc(Record.readSourceLocation());12719  unsigned NumOfAllocators = C->getNumberOfAllocators();12720  SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;12721  Data.reserve(NumOfAllocators);12722  for (unsigned I = 0; I != NumOfAllocators; ++I) {12723    OMPUsesAllocatorsClause::Data &D = Data.emplace_back();12724    D.Allocator = Record.readSubExpr();12725    D.AllocatorTraits = Record.readSubExpr();12726    D.LParenLoc = Record.readSourceLocation();12727    D.RParenLoc = Record.readSourceLocation();12728  }12729  C->setAllocatorsData(Data);12730}12731 12732void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {12733  C->setLParenLoc(Record.readSourceLocation());12734  C->setModifier(Record.readSubExpr());12735  C->setColonLoc(Record.readSourceLocation());12736  unsigned NumOfLocators = C->varlist_size();12737  SmallVector<Expr *, 4> Locators;12738  Locators.reserve(NumOfLocators);12739  for (unsigned I = 0; I != NumOfLocators; ++I)12740    Locators.push_back(Record.readSubExpr());12741  C->setVarRefs(Locators);12742}12743 12744void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {12745  C->setKind(Record.readEnum<OpenMPOrderClauseKind>());12746  C->setModifier(Record.readEnum<OpenMPOrderClauseModifier>());12747  C->setLParenLoc(Record.readSourceLocation());12748  C->setKindKwLoc(Record.readSourceLocation());12749  C->setModifierKwLoc(Record.readSourceLocation());12750}12751 12752void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {12753  VisitOMPClauseWithPreInit(C);12754  C->setThreadID(Record.readSubExpr());12755  C->setLParenLoc(Record.readSourceLocation());12756}12757 12758void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {12759  C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());12760  C->setLParenLoc(Record.readSourceLocation());12761  C->setBindKindLoc(Record.readSourceLocation());12762}12763 12764void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {12765  C->setAlignment(Record.readExpr());12766  C->setLParenLoc(Record.readSourceLocation());12767}12768 12769void OMPClauseReader::VisitOMPXDynCGroupMemClause(OMPXDynCGroupMemClause *C) {12770  VisitOMPClauseWithPreInit(C);12771  C->setSize(Record.readSubExpr());12772  C->setLParenLoc(Record.readSourceLocation());12773}12774 12775void OMPClauseReader::VisitOMPDynGroupprivateClause(12776    OMPDynGroupprivateClause *C) {12777  VisitOMPClauseWithPreInit(C);12778  C->setDynGroupprivateModifier(12779      Record.readEnum<OpenMPDynGroupprivateClauseModifier>());12780  C->setDynGroupprivateFallbackModifier(12781      Record.readEnum<OpenMPDynGroupprivateClauseFallbackModifier>());12782  C->setSize(Record.readSubExpr());12783  C->setLParenLoc(Record.readSourceLocation());12784  C->setDynGroupprivateModifierLoc(Record.readSourceLocation());12785  C->setDynGroupprivateFallbackModifierLoc(Record.readSourceLocation());12786}12787 12788void OMPClauseReader::VisitOMPDoacrossClause(OMPDoacrossClause *C) {12789  C->setLParenLoc(Record.readSourceLocation());12790  C->setDependenceType(12791      static_cast<OpenMPDoacrossClauseModifier>(Record.readInt()));12792  C->setDependenceLoc(Record.readSourceLocation());12793  C->setColonLoc(Record.readSourceLocation());12794  unsigned NumVars = C->varlist_size();12795  SmallVector<Expr *, 16> Vars;12796  Vars.reserve(NumVars);12797  for (unsigned I = 0; I != NumVars; ++I)12798    Vars.push_back(Record.readSubExpr());12799  C->setVarRefs(Vars);12800  for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)12801    C->setLoopData(I, Record.readSubExpr());12802}12803 12804void OMPClauseReader::VisitOMPXAttributeClause(OMPXAttributeClause *C) {12805  AttrVec Attrs;12806  Record.readAttributes(Attrs);12807  C->setAttrs(Attrs);12808  C->setLocStart(Record.readSourceLocation());12809  C->setLParenLoc(Record.readSourceLocation());12810  C->setLocEnd(Record.readSourceLocation());12811}12812 12813void OMPClauseReader::VisitOMPXBareClause(OMPXBareClause *C) {}12814 12815OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {12816  OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();12817  TI.Sets.resize(readUInt32());12818  for (auto &Set : TI.Sets) {12819    Set.Kind = readEnum<llvm::omp::TraitSet>();12820    Set.Selectors.resize(readUInt32());12821    for (auto &Selector : Set.Selectors) {12822      Selector.Kind = readEnum<llvm::omp::TraitSelector>();12823      Selector.ScoreOrCondition = nullptr;12824      if (readBool())12825        Selector.ScoreOrCondition = readExprRef();12826      Selector.Properties.resize(readUInt32());12827      for (auto &Property : Selector.Properties)12828        Property.Kind = readEnum<llvm::omp::TraitProperty>();12829    }12830  }12831  return &TI;12832}12833 12834void ASTRecordReader::readOMPChildren(OMPChildren *Data) {12835  if (!Data)12836    return;12837  if (Reader->ReadingKind == ASTReader::Read_Stmt) {12838    // Skip NumClauses, NumChildren and HasAssociatedStmt fields.12839    skipInts(3);12840  }12841  SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());12842  for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)12843    Clauses[I] = readOMPClause();12844  Data->setClauses(Clauses);12845  if (Data->hasAssociatedStmt())12846    Data->setAssociatedStmt(readStmt());12847  for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)12848    Data->getChildren()[I] = readStmt();12849}12850 12851SmallVector<Expr *> ASTRecordReader::readOpenACCVarList() {12852  unsigned NumVars = readInt();12853  llvm::SmallVector<Expr *> VarList;12854  for (unsigned I = 0; I < NumVars; ++I)12855    VarList.push_back(readExpr());12856  return VarList;12857}12858 12859SmallVector<Expr *> ASTRecordReader::readOpenACCIntExprList() {12860  unsigned NumExprs = readInt();12861  llvm::SmallVector<Expr *> ExprList;12862  for (unsigned I = 0; I < NumExprs; ++I)12863    ExprList.push_back(readSubExpr());12864  return ExprList;12865}12866 12867OpenACCClause *ASTRecordReader::readOpenACCClause() {12868  OpenACCClauseKind ClauseKind = readEnum<OpenACCClauseKind>();12869  SourceLocation BeginLoc = readSourceLocation();12870  SourceLocation EndLoc = readSourceLocation();12871 12872  switch (ClauseKind) {12873  case OpenACCClauseKind::Default: {12874    SourceLocation LParenLoc = readSourceLocation();12875    OpenACCDefaultClauseKind DCK = readEnum<OpenACCDefaultClauseKind>();12876    return OpenACCDefaultClause::Create(getContext(), DCK, BeginLoc, LParenLoc,12877                                        EndLoc);12878  }12879  case OpenACCClauseKind::If: {12880    SourceLocation LParenLoc = readSourceLocation();12881    Expr *CondExpr = readSubExpr();12882    return OpenACCIfClause::Create(getContext(), BeginLoc, LParenLoc, CondExpr,12883                                   EndLoc);12884  }12885  case OpenACCClauseKind::Self: {12886    SourceLocation LParenLoc = readSourceLocation();12887    bool isConditionExprClause = readBool();12888    if (isConditionExprClause) {12889      Expr *CondExpr = readBool() ? readSubExpr() : nullptr;12890      return OpenACCSelfClause::Create(getContext(), BeginLoc, LParenLoc,12891                                       CondExpr, EndLoc);12892    }12893    unsigned NumVars = readInt();12894    llvm::SmallVector<Expr *> VarList;12895    for (unsigned I = 0; I < NumVars; ++I)12896      VarList.push_back(readSubExpr());12897    return OpenACCSelfClause::Create(getContext(), BeginLoc, LParenLoc, VarList,12898                                     EndLoc);12899  }12900  case OpenACCClauseKind::NumGangs: {12901    SourceLocation LParenLoc = readSourceLocation();12902    unsigned NumClauses = readInt();12903    llvm::SmallVector<Expr *> IntExprs;12904    for (unsigned I = 0; I < NumClauses; ++I)12905      IntExprs.push_back(readSubExpr());12906    return OpenACCNumGangsClause::Create(getContext(), BeginLoc, LParenLoc,12907                                         IntExprs, EndLoc);12908  }12909  case OpenACCClauseKind::NumWorkers: {12910    SourceLocation LParenLoc = readSourceLocation();12911    Expr *IntExpr = readSubExpr();12912    return OpenACCNumWorkersClause::Create(getContext(), BeginLoc, LParenLoc,12913                                           IntExpr, EndLoc);12914  }12915  case OpenACCClauseKind::DeviceNum: {12916    SourceLocation LParenLoc = readSourceLocation();12917    Expr *IntExpr = readSubExpr();12918    return OpenACCDeviceNumClause::Create(getContext(), BeginLoc, LParenLoc,12919                                           IntExpr, EndLoc);12920  }12921  case OpenACCClauseKind::DefaultAsync: {12922    SourceLocation LParenLoc = readSourceLocation();12923    Expr *IntExpr = readSubExpr();12924    return OpenACCDefaultAsyncClause::Create(getContext(), BeginLoc, LParenLoc,12925                                             IntExpr, EndLoc);12926  }12927  case OpenACCClauseKind::VectorLength: {12928    SourceLocation LParenLoc = readSourceLocation();12929    Expr *IntExpr = readSubExpr();12930    return OpenACCVectorLengthClause::Create(getContext(), BeginLoc, LParenLoc,12931                                             IntExpr, EndLoc);12932  }12933  case OpenACCClauseKind::Private: {12934    SourceLocation LParenLoc = readSourceLocation();12935    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();12936 12937    llvm::SmallVector<OpenACCPrivateRecipe> RecipeList;12938    for (unsigned I = 0; I < VarList.size(); ++I) {12939      static_assert(sizeof(OpenACCPrivateRecipe) == 1 * sizeof(int *));12940      VarDecl *Alloca = readDeclAs<VarDecl>();12941      RecipeList.push_back({Alloca});12942    }12943 12944    return OpenACCPrivateClause::Create(getContext(), BeginLoc, LParenLoc,12945                                        VarList, RecipeList, EndLoc);12946  }12947  case OpenACCClauseKind::Host: {12948    SourceLocation LParenLoc = readSourceLocation();12949    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();12950    return OpenACCHostClause::Create(getContext(), BeginLoc, LParenLoc, VarList,12951                                     EndLoc);12952  }12953  case OpenACCClauseKind::Device: {12954    SourceLocation LParenLoc = readSourceLocation();12955    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();12956    return OpenACCDeviceClause::Create(getContext(), BeginLoc, LParenLoc,12957                                       VarList, EndLoc);12958  }12959  case OpenACCClauseKind::FirstPrivate: {12960    SourceLocation LParenLoc = readSourceLocation();12961    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();12962    llvm::SmallVector<OpenACCFirstPrivateRecipe> RecipeList;12963    for (unsigned I = 0; I < VarList.size(); ++I) {12964      static_assert(sizeof(OpenACCFirstPrivateRecipe) == 2 * sizeof(int *));12965      VarDecl *Recipe = readDeclAs<VarDecl>();12966      VarDecl *RecipeTemp = readDeclAs<VarDecl>();12967      RecipeList.push_back({Recipe, RecipeTemp});12968    }12969 12970    return OpenACCFirstPrivateClause::Create(getContext(), BeginLoc, LParenLoc,12971                                             VarList, RecipeList, EndLoc);12972  }12973  case OpenACCClauseKind::Attach: {12974    SourceLocation LParenLoc = readSourceLocation();12975    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();12976    return OpenACCAttachClause::Create(getContext(), BeginLoc, LParenLoc,12977                                       VarList, EndLoc);12978  }12979  case OpenACCClauseKind::Detach: {12980    SourceLocation LParenLoc = readSourceLocation();12981    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();12982    return OpenACCDetachClause::Create(getContext(), BeginLoc, LParenLoc,12983                                       VarList, EndLoc);12984  }12985  case OpenACCClauseKind::Delete: {12986    SourceLocation LParenLoc = readSourceLocation();12987    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();12988    return OpenACCDeleteClause::Create(getContext(), BeginLoc, LParenLoc,12989                                       VarList, EndLoc);12990  }12991  case OpenACCClauseKind::UseDevice: {12992    SourceLocation LParenLoc = readSourceLocation();12993    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();12994    return OpenACCUseDeviceClause::Create(getContext(), BeginLoc, LParenLoc,12995                                          VarList, EndLoc);12996  }12997  case OpenACCClauseKind::DevicePtr: {12998    SourceLocation LParenLoc = readSourceLocation();12999    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13000    return OpenACCDevicePtrClause::Create(getContext(), BeginLoc, LParenLoc,13001                                          VarList, EndLoc);13002  }13003  case OpenACCClauseKind::NoCreate: {13004    SourceLocation LParenLoc = readSourceLocation();13005    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13006    return OpenACCNoCreateClause::Create(getContext(), BeginLoc, LParenLoc,13007                                         VarList, EndLoc);13008  }13009  case OpenACCClauseKind::Present: {13010    SourceLocation LParenLoc = readSourceLocation();13011    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13012    return OpenACCPresentClause::Create(getContext(), BeginLoc, LParenLoc,13013                                        VarList, EndLoc);13014  }13015  case OpenACCClauseKind::PCopy:13016  case OpenACCClauseKind::PresentOrCopy:13017  case OpenACCClauseKind::Copy: {13018    SourceLocation LParenLoc = readSourceLocation();13019    OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();13020    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13021    return OpenACCCopyClause::Create(getContext(), ClauseKind, BeginLoc,13022                                     LParenLoc, ModList, VarList, EndLoc);13023  }13024  case OpenACCClauseKind::CopyIn:13025  case OpenACCClauseKind::PCopyIn:13026  case OpenACCClauseKind::PresentOrCopyIn: {13027    SourceLocation LParenLoc = readSourceLocation();13028    OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();13029    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13030    return OpenACCCopyInClause::Create(getContext(), ClauseKind, BeginLoc,13031                                       LParenLoc, ModList, VarList, EndLoc);13032  }13033  case OpenACCClauseKind::CopyOut:13034  case OpenACCClauseKind::PCopyOut:13035  case OpenACCClauseKind::PresentOrCopyOut: {13036    SourceLocation LParenLoc = readSourceLocation();13037    OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();13038    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13039    return OpenACCCopyOutClause::Create(getContext(), ClauseKind, BeginLoc,13040                                        LParenLoc, ModList, VarList, EndLoc);13041  }13042  case OpenACCClauseKind::Create:13043  case OpenACCClauseKind::PCreate:13044  case OpenACCClauseKind::PresentOrCreate: {13045    SourceLocation LParenLoc = readSourceLocation();13046    OpenACCModifierKind ModList = readEnum<OpenACCModifierKind>();13047    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13048    return OpenACCCreateClause::Create(getContext(), ClauseKind, BeginLoc,13049                                       LParenLoc, ModList, VarList, EndLoc);13050  }13051  case OpenACCClauseKind::Async: {13052    SourceLocation LParenLoc = readSourceLocation();13053    Expr *AsyncExpr = readBool() ? readSubExpr() : nullptr;13054    return OpenACCAsyncClause::Create(getContext(), BeginLoc, LParenLoc,13055                                      AsyncExpr, EndLoc);13056  }13057  case OpenACCClauseKind::Wait: {13058    SourceLocation LParenLoc = readSourceLocation();13059    Expr *DevNumExpr = readBool() ? readSubExpr() : nullptr;13060    SourceLocation QueuesLoc = readSourceLocation();13061    llvm::SmallVector<Expr *> QueueIdExprs = readOpenACCIntExprList();13062    return OpenACCWaitClause::Create(getContext(), BeginLoc, LParenLoc,13063                                     DevNumExpr, QueuesLoc, QueueIdExprs,13064                                     EndLoc);13065  }13066  case OpenACCClauseKind::DeviceType:13067  case OpenACCClauseKind::DType: {13068    SourceLocation LParenLoc = readSourceLocation();13069    llvm::SmallVector<DeviceTypeArgument> Archs;13070    unsigned NumArchs = readInt();13071 13072    for (unsigned I = 0; I < NumArchs; ++I) {13073      IdentifierInfo *Ident = readBool() ? readIdentifier() : nullptr;13074      SourceLocation Loc = readSourceLocation();13075      Archs.emplace_back(Loc, Ident);13076    }13077 13078    return OpenACCDeviceTypeClause::Create(getContext(), ClauseKind, BeginLoc,13079                                           LParenLoc, Archs, EndLoc);13080  }13081  case OpenACCClauseKind::Reduction: {13082    SourceLocation LParenLoc = readSourceLocation();13083    OpenACCReductionOperator Op = readEnum<OpenACCReductionOperator>();13084    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13085    llvm::SmallVector<OpenACCReductionRecipeWithStorage> RecipeList;13086 13087    for (unsigned I = 0; I < VarList.size(); ++I) {13088      VarDecl *Recipe = readDeclAs<VarDecl>();13089 13090      static_assert(sizeof(OpenACCReductionRecipe::CombinerRecipe) ==13091                    3 * sizeof(int *));13092 13093      llvm::SmallVector<OpenACCReductionRecipe::CombinerRecipe> Combiners;13094      unsigned NumCombiners = readInt();13095      for (unsigned I = 0; I < NumCombiners; ++I) {13096        VarDecl *LHS = readDeclAs<VarDecl>();13097        VarDecl *RHS = readDeclAs<VarDecl>();13098        Expr *Op = readExpr();13099 13100        Combiners.push_back({LHS, RHS, Op});13101      }13102 13103      RecipeList.push_back({Recipe, Combiners});13104    }13105 13106    return OpenACCReductionClause::Create(getContext(), BeginLoc, LParenLoc, Op,13107                                          VarList, RecipeList, EndLoc);13108  }13109  case OpenACCClauseKind::Seq:13110    return OpenACCSeqClause::Create(getContext(), BeginLoc, EndLoc);13111  case OpenACCClauseKind::NoHost:13112    return OpenACCNoHostClause::Create(getContext(), BeginLoc, EndLoc);13113  case OpenACCClauseKind::Finalize:13114    return OpenACCFinalizeClause::Create(getContext(), BeginLoc, EndLoc);13115  case OpenACCClauseKind::IfPresent:13116    return OpenACCIfPresentClause::Create(getContext(), BeginLoc, EndLoc);13117  case OpenACCClauseKind::Independent:13118    return OpenACCIndependentClause::Create(getContext(), BeginLoc, EndLoc);13119  case OpenACCClauseKind::Auto:13120    return OpenACCAutoClause::Create(getContext(), BeginLoc, EndLoc);13121  case OpenACCClauseKind::Collapse: {13122    SourceLocation LParenLoc = readSourceLocation();13123    bool HasForce = readBool();13124    Expr *LoopCount = readSubExpr();13125    return OpenACCCollapseClause::Create(getContext(), BeginLoc, LParenLoc,13126                                         HasForce, LoopCount, EndLoc);13127  }13128  case OpenACCClauseKind::Tile: {13129    SourceLocation LParenLoc = readSourceLocation();13130    unsigned NumClauses = readInt();13131    llvm::SmallVector<Expr *> SizeExprs;13132    for (unsigned I = 0; I < NumClauses; ++I)13133      SizeExprs.push_back(readSubExpr());13134    return OpenACCTileClause::Create(getContext(), BeginLoc, LParenLoc,13135                                     SizeExprs, EndLoc);13136  }13137  case OpenACCClauseKind::Gang: {13138    SourceLocation LParenLoc = readSourceLocation();13139    unsigned NumExprs = readInt();13140    llvm::SmallVector<OpenACCGangKind> GangKinds;13141    llvm::SmallVector<Expr *> Exprs;13142    for (unsigned I = 0; I < NumExprs; ++I) {13143      GangKinds.push_back(readEnum<OpenACCGangKind>());13144      // Can't use `readSubExpr` because this is usable from a 'decl' construct.13145      Exprs.push_back(readExpr());13146    }13147    return OpenACCGangClause::Create(getContext(), BeginLoc, LParenLoc,13148                                     GangKinds, Exprs, EndLoc);13149  }13150  case OpenACCClauseKind::Worker: {13151    SourceLocation LParenLoc = readSourceLocation();13152    Expr *WorkerExpr = readBool() ? readSubExpr() : nullptr;13153    return OpenACCWorkerClause::Create(getContext(), BeginLoc, LParenLoc,13154                                       WorkerExpr, EndLoc);13155  }13156  case OpenACCClauseKind::Vector: {13157    SourceLocation LParenLoc = readSourceLocation();13158    Expr *VectorExpr = readBool() ? readSubExpr() : nullptr;13159    return OpenACCVectorClause::Create(getContext(), BeginLoc, LParenLoc,13160                                       VectorExpr, EndLoc);13161  }13162  case OpenACCClauseKind::Link: {13163    SourceLocation LParenLoc = readSourceLocation();13164    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13165    return OpenACCLinkClause::Create(getContext(), BeginLoc, LParenLoc, VarList,13166                                     EndLoc);13167  }13168  case OpenACCClauseKind::DeviceResident: {13169    SourceLocation LParenLoc = readSourceLocation();13170    llvm::SmallVector<Expr *> VarList = readOpenACCVarList();13171    return OpenACCDeviceResidentClause::Create(getContext(), BeginLoc,13172                                               LParenLoc, VarList, EndLoc);13173  }13174 13175  case OpenACCClauseKind::Bind: {13176    SourceLocation LParenLoc = readSourceLocation();13177    bool IsString = readBool();13178    if (IsString)13179      return OpenACCBindClause::Create(getContext(), BeginLoc, LParenLoc,13180                                       cast<StringLiteral>(readExpr()), EndLoc);13181    return OpenACCBindClause::Create(getContext(), BeginLoc, LParenLoc,13182                                     readIdentifier(), EndLoc);13183  }13184  case OpenACCClauseKind::Shortloop:13185  case OpenACCClauseKind::Invalid:13186    llvm_unreachable("Clause serialization not yet implemented");13187  }13188  llvm_unreachable("Invalid Clause Kind");13189}13190 13191void ASTRecordReader::readOpenACCClauseList(13192    MutableArrayRef<const OpenACCClause *> Clauses) {13193  for (unsigned I = 0; I < Clauses.size(); ++I)13194    Clauses[I] = readOpenACCClause();13195}13196 13197void ASTRecordReader::readOpenACCRoutineDeclAttr(OpenACCRoutineDeclAttr *A) {13198  unsigned NumVars = readInt();13199  A->Clauses.resize(NumVars);13200  readOpenACCClauseList(A->Clauses);13201}13202 13203static unsigned getStableHashForModuleName(StringRef PrimaryModuleName) {13204  // TODO: Maybe it is better to check PrimaryModuleName is a valid13205  // module name?13206  llvm::FoldingSetNodeID ID;13207  ID.AddString(PrimaryModuleName);13208  return ID.computeStableHash();13209}13210 13211UnsignedOrNone clang::getPrimaryModuleHash(const Module *M) {13212  if (!M)13213    return std::nullopt;13214 13215  if (M->isHeaderLikeModule())13216    return std::nullopt;13217 13218  if (M->isGlobalModule())13219    return std::nullopt;13220 13221  StringRef PrimaryModuleName = M->getPrimaryModuleInterfaceName();13222  return getStableHashForModuleName(PrimaryModuleName);13223}13224