1//===- ASTReader.cpp - AST File Reader ------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//  This file defines the ASTReader class, which reads AST files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Basic/OpenMPKinds.h"
14#include "clang/Serialization/ASTRecordReader.h"
15#include "ASTCommon.h"
16#include "ASTReaderInternals.h"
17#include "clang/AST/AbstractTypeReader.h"
18#include "clang/AST/ASTConsumer.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/ASTMutationListener.h"
21#include "clang/AST/ASTUnresolvedSet.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/DeclBase.h"
24#include "clang/AST/DeclCXX.h"
25#include "clang/AST/DeclFriend.h"
26#include "clang/AST/DeclGroup.h"
27#include "clang/AST/DeclObjC.h"
28#include "clang/AST/DeclTemplate.h"
29#include "clang/AST/DeclarationName.h"
30#include "clang/AST/Expr.h"
31#include "clang/AST/ExprCXX.h"
32#include "clang/AST/ExternalASTSource.h"
33#include "clang/AST/NestedNameSpecifier.h"
34#include "clang/AST/OpenMPClause.h"
35#include "clang/AST/ODRHash.h"
36#include "clang/AST/RawCommentList.h"
37#include "clang/AST/TemplateBase.h"
38#include "clang/AST/TemplateName.h"
39#include "clang/AST/Type.h"
40#include "clang/AST/TypeLoc.h"
41#include "clang/AST/TypeLocVisitor.h"
42#include "clang/AST/UnresolvedSet.h"
43#include "clang/Basic/CommentOptions.h"
44#include "clang/Basic/Diagnostic.h"
45#include "clang/Basic/DiagnosticOptions.h"
46#include "clang/Basic/ExceptionSpecificationType.h"
47#include "clang/Basic/FileManager.h"
48#include "clang/Basic/FileSystemOptions.h"
49#include "clang/Basic/IdentifierTable.h"
50#include "clang/Basic/LLVM.h"
51#include "clang/Basic/LangOptions.h"
52#include "clang/Basic/Module.h"
53#include "clang/Basic/ObjCRuntime.h"
54#include "clang/Basic/OperatorKinds.h"
55#include "clang/Basic/PragmaKinds.h"
56#include "clang/Basic/Sanitizers.h"
57#include "clang/Basic/SourceLocation.h"
58#include "clang/Basic/SourceManager.h"
59#include "clang/Basic/SourceManagerInternals.h"
60#include "clang/Basic/Specifiers.h"
61#include "clang/Basic/TargetInfo.h"
62#include "clang/Basic/TargetOptions.h"
63#include "clang/Basic/TokenKinds.h"
64#include "clang/Basic/Version.h"
65#include "clang/Lex/HeaderSearch.h"
66#include "clang/Lex/HeaderSearchOptions.h"
67#include "clang/Lex/MacroInfo.h"
68#include "clang/Lex/ModuleMap.h"
69#include "clang/Lex/PreprocessingRecord.h"
70#include "clang/Lex/Preprocessor.h"
71#include "clang/Lex/PreprocessorOptions.h"
72#include "clang/Lex/Token.h"
73#include "clang/Sema/ObjCMethodList.h"
74#include "clang/Sema/Scope.h"
75#include "clang/Sema/Sema.h"
76#include "clang/Sema/Weak.h"
77#include "clang/Serialization/ASTBitCodes.h"
78#include "clang/Serialization/ASTDeserializationListener.h"
79#include "clang/Serialization/ContinuousRangeMap.h"
80#include "clang/Serialization/GlobalModuleIndex.h"
81#include "clang/Serialization/InMemoryModuleCache.h"
82#include "clang/Serialization/ModuleFile.h"
83#include "clang/Serialization/ModuleFileExtension.h"
84#include "clang/Serialization/ModuleManager.h"
85#include "clang/Serialization/PCHContainerOperations.h"
86#include "clang/Serialization/SerializationDiagnostic.h"
87#include "llvm/ADT/APFloat.h"
88#include "llvm/ADT/APInt.h"
89#include "llvm/ADT/APSInt.h"
90#include "llvm/ADT/ArrayRef.h"
91#include "llvm/ADT/DenseMap.h"
92#include "llvm/ADT/FloatingPointMode.h"
93#include "llvm/ADT/FoldingSet.h"
94#include "llvm/ADT/Hashing.h"
95#include "llvm/ADT/IntrusiveRefCntPtr.h"
96#include "llvm/ADT/None.h"
97#include "llvm/ADT/Optional.h"
98#include "llvm/ADT/STLExtras.h"
99#include "llvm/ADT/ScopeExit.h"
100#include "llvm/ADT/SmallPtrSet.h"
101#include "llvm/ADT/SmallString.h"
102#include "llvm/ADT/SmallVector.h"
103#include "llvm/ADT/StringExtras.h"
104#include "llvm/ADT/StringMap.h"
105#include "llvm/ADT/StringRef.h"
106#include "llvm/ADT/Triple.h"
107#include "llvm/ADT/iterator_range.h"
108#include "llvm/Bitstream/BitstreamReader.h"
109#include "llvm/Support/Casting.h"
110#include "llvm/Support/Compiler.h"
111#include "llvm/Support/Compression.h"
112#include "llvm/Support/DJB.h"
113#include "llvm/Support/Endian.h"
114#include "llvm/Support/Error.h"
115#include "llvm/Support/ErrorHandling.h"
116#include "llvm/Support/FileSystem.h"
117#include "llvm/Support/MemoryBuffer.h"
118#include "llvm/Support/Path.h"
119#include "llvm/Support/SaveAndRestore.h"
120#include "llvm/Support/Timer.h"
121#include "llvm/Support/VersionTuple.h"
122#include "llvm/Support/raw_ostream.h"
123#include <algorithm>
124#include <cassert>
125#include <cstddef>
126#include <cstdint>
127#include <cstdio>
128#include <ctime>
129#include <iterator>
130#include <limits>
131#include <map>
132#include <memory>
133#include <string>
134#include <system_error>
135#include <tuple>
136#include <utility>
137#include <vector>
138
139using namespace clang;
140using namespace clang::serialization;
141using namespace clang::serialization::reader;
142using llvm::BitstreamCursor;
143using llvm::RoundingMode;
144
145//===----------------------------------------------------------------------===//
146// ChainedASTReaderListener implementation
147//===----------------------------------------------------------------------===//
148
149bool
150ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
151  return First->ReadFullVersionInformation(FullVersion) ||
152         Second->ReadFullVersionInformation(FullVersion);
153}
154
155void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
156  First->ReadModuleName(ModuleName);
157  Second->ReadModuleName(ModuleName);
158}
159
160void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
161  First->ReadModuleMapFile(ModuleMapPath);
162  Second->ReadModuleMapFile(ModuleMapPath);
163}
164
165bool
166ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
167                                              bool Complain,
168                                              bool AllowCompatibleDifferences) {
169  return First->ReadLanguageOptions(LangOpts, Complain,
170                                    AllowCompatibleDifferences) ||
171         Second->ReadLanguageOptions(LangOpts, Complain,
172                                     AllowCompatibleDifferences);
173}
174
175bool ChainedASTReaderListener::ReadTargetOptions(
176    const TargetOptions &TargetOpts, bool Complain,
177    bool AllowCompatibleDifferences) {
178  return First->ReadTargetOptions(TargetOpts, Complain,
179                                  AllowCompatibleDifferences) ||
180         Second->ReadTargetOptions(TargetOpts, Complain,
181                                   AllowCompatibleDifferences);
182}
183
184bool ChainedASTReaderListener::ReadDiagnosticOptions(
185    IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
186  return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
187         Second->ReadDiagnosticOptions(DiagOpts, Complain);
188}
189
190bool
191ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
192                                                bool Complain) {
193  return First->ReadFileSystemOptions(FSOpts, Complain) ||
194         Second->ReadFileSystemOptions(FSOpts, Complain);
195}
196
197bool ChainedASTReaderListener::ReadHeaderSearchOptions(
198    const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
199    bool Complain) {
200  return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201                                        Complain) ||
202         Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
203                                         Complain);
204}
205
206bool ChainedASTReaderListener::ReadPreprocessorOptions(
207    const PreprocessorOptions &PPOpts, bool Complain,
208    std::string &SuggestedPredefines) {
209  return First->ReadPreprocessorOptions(PPOpts, Complain,
210                                        SuggestedPredefines) ||
211         Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
212}
213
214void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
215                                           unsigned Value) {
216  First->ReadCounter(M, Value);
217  Second->ReadCounter(M, Value);
218}
219
220bool ChainedASTReaderListener::needsInputFileVisitation() {
221  return First->needsInputFileVisitation() ||
222         Second->needsInputFileVisitation();
223}
224
225bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
226  return First->needsSystemInputFileVisitation() ||
227  Second->needsSystemInputFileVisitation();
228}
229
230void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
231                                               ModuleKind Kind) {
232  First->visitModuleFile(Filename, Kind);
233  Second->visitModuleFile(Filename, Kind);
234}
235
236bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
237                                              bool isSystem,
238                                              bool isOverridden,
239                                              bool isExplicitModule) {
240  bool Continue = false;
241  if (First->needsInputFileVisitation() &&
242      (!isSystem || First->needsSystemInputFileVisitation()))
243    Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
244                                      isExplicitModule);
245  if (Second->needsInputFileVisitation() &&
246      (!isSystem || Second->needsSystemInputFileVisitation()))
247    Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
248                                       isExplicitModule);
249  return Continue;
250}
251
252void ChainedASTReaderListener::readModuleFileExtension(
253       const ModuleFileExtensionMetadata &Metadata) {
254  First->readModuleFileExtension(Metadata);
255  Second->readModuleFileExtension(Metadata);
256}
257
258//===----------------------------------------------------------------------===//
259// PCH validator implementation
260//===----------------------------------------------------------------------===//
261
262ASTReaderListener::~ASTReaderListener() = default;
263
264/// Compare the given set of language options against an existing set of
265/// language options.
266///
267/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
268/// \param AllowCompatibleDifferences If true, differences between compatible
269///        language options will be permitted.
270///
271/// \returns true if the languagae options mis-match, false otherwise.
272static bool checkLanguageOptions(const LangOptions &LangOpts,
273                                 const LangOptions &ExistingLangOpts,
274                                 DiagnosticsEngine *Diags,
275                                 bool AllowCompatibleDifferences = true) {
276#define LANGOPT(Name, Bits, Default, Description)                 \
277  if (ExistingLangOpts.Name != LangOpts.Name) {                   \
278    if (Diags)                                                    \
279      Diags->Report(diag::err_pch_langopt_mismatch)               \
280        << Description << LangOpts.Name << ExistingLangOpts.Name; \
281    return true;                                                  \
282  }
283
284#define VALUE_LANGOPT(Name, Bits, Default, Description)   \
285  if (ExistingLangOpts.Name != LangOpts.Name) {           \
286    if (Diags)                                            \
287      Diags->Report(diag::err_pch_langopt_value_mismatch) \
288        << Description;                                   \
289    return true;                                          \
290  }
291
292#define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
293  if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
294    if (Diags)                                                 \
295      Diags->Report(diag::err_pch_langopt_value_mismatch)      \
296        << Description;                                        \
297    return true;                                               \
298  }
299
300#define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
301  if (!AllowCompatibleDifferences)                            \
302    LANGOPT(Name, Bits, Default, Description)
303
304#define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
305  if (!AllowCompatibleDifferences)                                 \
306    ENUM_LANGOPT(Name, Bits, Default, Description)
307
308#define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
309  if (!AllowCompatibleDifferences)                                 \
310    VALUE_LANGOPT(Name, Bits, Default, Description)
311
312#define BENIGN_LANGOPT(Name, Bits, Default, Description)
313#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
314#define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
315#include "clang/Basic/LangOptions.def"
316
317  if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
318    if (Diags)
319      Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
320    return true;
321  }
322
323  if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
324    if (Diags)
325      Diags->Report(diag::err_pch_langopt_value_mismatch)
326      << "target Objective-C runtime";
327    return true;
328  }
329
330  if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
331      LangOpts.CommentOpts.BlockCommandNames) {
332    if (Diags)
333      Diags->Report(diag::err_pch_langopt_value_mismatch)
334        << "block command names";
335    return true;
336  }
337
338  // Sanitizer feature mismatches are treated as compatible differences. If
339  // compatible differences aren't allowed, we still only want to check for
340  // mismatches of non-modular sanitizers (the only ones which can affect AST
341  // generation).
342  if (!AllowCompatibleDifferences) {
343    SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
344    SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
345    SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
346    ExistingSanitizers.clear(ModularSanitizers);
347    ImportedSanitizers.clear(ModularSanitizers);
348    if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
349      const std::string Flag = "-fsanitize=";
350      if (Diags) {
351#define SANITIZER(NAME, ID)                                                    \
352  {                                                                            \
353    bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
354    bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
355    if (InExistingModule != InImportedModule)                                  \
356      Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
357          << InExistingModule << (Flag + NAME);                                \
358  }
359#include "clang/Basic/Sanitizers.def"
360      }
361      return true;
362    }
363  }
364
365  return false;
366}
367
368/// Compare the given set of target options against an existing set of
369/// target options.
370///
371/// \param Diags If non-NULL, diagnostics will be emitted via this engine.
372///
373/// \returns true if the target options mis-match, false otherwise.
374static bool checkTargetOptions(const TargetOptions &TargetOpts,
375                               const TargetOptions &ExistingTargetOpts,
376                               DiagnosticsEngine *Diags,
377                               bool AllowCompatibleDifferences = true) {
378#define CHECK_TARGET_OPT(Field, Name)                             \
379  if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
380    if (Diags)                                                    \
381      Diags->Report(diag::err_pch_targetopt_mismatch)             \
382        << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
383    return true;                                                  \
384  }
385
386  // The triple and ABI must match exactly.
387  CHECK_TARGET_OPT(Triple, "target");
388  CHECK_TARGET_OPT(ABI, "target ABI");
389
390  // We can tolerate different CPUs in many cases, notably when one CPU
391  // supports a strict superset of another. When allowing compatible
392  // differences skip this check.
393  if (!AllowCompatibleDifferences)
394    CHECK_TARGET_OPT(CPU, "target CPU");
395
396#undef CHECK_TARGET_OPT
397
398  // Compare feature sets.
399  SmallVector<StringRef, 4> ExistingFeatures(
400                                             ExistingTargetOpts.FeaturesAsWritten.begin(),
401                                             ExistingTargetOpts.FeaturesAsWritten.end());
402  SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
403                                         TargetOpts.FeaturesAsWritten.end());
404  llvm::sort(ExistingFeatures);
405  llvm::sort(ReadFeatures);
406
407  // We compute the set difference in both directions explicitly so that we can
408  // diagnose the differences differently.
409  SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
410  std::set_difference(
411      ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
412      ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
413  std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
414                      ExistingFeatures.begin(), ExistingFeatures.end(),
415                      std::back_inserter(UnmatchedReadFeatures));
416
417  // If we are allowing compatible differences and the read feature set is
418  // a strict subset of the existing feature set, there is nothing to diagnose.
419  if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
420    return false;
421
422  if (Diags) {
423    for (StringRef Feature : UnmatchedReadFeatures)
424      Diags->Report(diag::err_pch_targetopt_feature_mismatch)
425          << /* is-existing-feature */ false << Feature;
426    for (StringRef Feature : UnmatchedExistingFeatures)
427      Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428          << /* is-existing-feature */ true << Feature;
429  }
430
431  return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
432}
433
434bool
435PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
436                                  bool Complain,
437                                  bool AllowCompatibleDifferences) {
438  const LangOptions &ExistingLangOpts = PP.getLangOpts();
439  return checkLanguageOptions(LangOpts, ExistingLangOpts,
440                              Complain ? &Reader.Diags : nullptr,
441                              AllowCompatibleDifferences);
442}
443
444bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
445                                     bool Complain,
446                                     bool AllowCompatibleDifferences) {
447  const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
448  return checkTargetOptions(TargetOpts, ExistingTargetOpts,
449                            Complain ? &Reader.Diags : nullptr,
450                            AllowCompatibleDifferences);
451}
452
453namespace {
454
455using MacroDefinitionsMap =
456    llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
457using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
458
459} // namespace
460
461static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
462                                         DiagnosticsEngine &Diags,
463                                         bool Complain) {
464  using Level = DiagnosticsEngine::Level;
465
466  // Check current mappings for new -Werror mappings, and the stored mappings
467  // for cases that were explicitly mapped to *not* be errors that are now
468  // errors because of options like -Werror.
469  DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
470
471  for (DiagnosticsEngine *MappingSource : MappingSources) {
472    for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
473      diag::kind DiagID = DiagIDMappingPair.first;
474      Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
475      if (CurLevel < DiagnosticsEngine::Error)
476        continue; // not significant
477      Level StoredLevel =
478          StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
479      if (StoredLevel < DiagnosticsEngine::Error) {
480        if (Complain)
481          Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
482              Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
483        return true;
484      }
485    }
486  }
487
488  return false;
489}
490
491static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
492  diag::Severity Ext = Diags.getExtensionHandlingBehavior();
493  if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
494    return true;
495  return Ext >= diag::Severity::Error;
496}
497
498static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
499                                    DiagnosticsEngine &Diags,
500                                    bool IsSystem, bool Complain) {
501  // Top-level options
502  if (IsSystem) {
503    if (Diags.getSuppressSystemWarnings())
504      return false;
505    // If -Wsystem-headers was not enabled before, be conservative
506    if (StoredDiags.getSuppressSystemWarnings()) {
507      if (Complain)
508        Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
509      return true;
510    }
511  }
512
513  if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
514    if (Complain)
515      Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
516    return true;
517  }
518
519  if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
520      !StoredDiags.getEnableAllWarnings()) {
521    if (Complain)
522      Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
523    return true;
524  }
525
526  if (isExtHandlingFromDiagsError(Diags) &&
527      !isExtHandlingFromDiagsError(StoredDiags)) {
528    if (Complain)
529      Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
530    return true;
531  }
532
533  return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
534}
535
536/// Return the top import module if it is implicit, nullptr otherwise.
537static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
538                                          Preprocessor &PP) {
539  // If the original import came from a file explicitly generated by the user,
540  // don't check the diagnostic mappings.
541  // FIXME: currently this is approximated by checking whether this is not a
542  // module import of an implicitly-loaded module file.
543  // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
544  // the transitive closure of its imports, since unrelated modules cannot be
545  // imported until after this module finishes validation.
546  ModuleFile *TopImport = &*ModuleMgr.rbegin();
547  while (!TopImport->ImportedBy.empty())
548    TopImport = TopImport->ImportedBy[0];
549  if (TopImport->Kind != MK_ImplicitModule)
550    return nullptr;
551
552  StringRef ModuleName = TopImport->ModuleName;
553  assert(!ModuleName.empty() && "diagnostic options read before module name");
554
555  Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
556  assert(M && "missing module");
557  return M;
558}
559
560bool PCHValidator::ReadDiagnosticOptions(
561    IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
562  DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
563  IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
564  IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
565      new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
566  // This should never fail, because we would have processed these options
567  // before writing them to an ASTFile.
568  ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
569
570  ModuleManager &ModuleMgr = Reader.getModuleManager();
571  assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
572
573  Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
574  if (!TopM)
575    return false;
576
577  // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
578  // contains the union of their flags.
579  return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
580                                 Complain);
581}
582
583/// Collect the macro definitions provided by the given preprocessor
584/// options.
585static void
586collectMacroDefinitions(const PreprocessorOptions &PPOpts,
587                        MacroDefinitionsMap &Macros,
588                        SmallVectorImpl<StringRef> *MacroNames = nullptr) {
589  for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
590    StringRef Macro = PPOpts.Macros[I].first;
591    bool IsUndef = PPOpts.Macros[I].second;
592
593    std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
594    StringRef MacroName = MacroPair.first;
595    StringRef MacroBody = MacroPair.second;
596
597    // For an #undef'd macro, we only care about the name.
598    if (IsUndef) {
599      if (MacroNames && !Macros.count(MacroName))
600        MacroNames->push_back(MacroName);
601
602      Macros[MacroName] = std::make_pair("", true);
603      continue;
604    }
605
606    // For a #define'd macro, figure out the actual definition.
607    if (MacroName.size() == Macro.size())
608      MacroBody = "1";
609    else {
610      // Note: GCC drops anything following an end-of-line character.
611      StringRef::size_type End = MacroBody.find_first_of("\n\r");
612      MacroBody = MacroBody.substr(0, End);
613    }
614
615    if (MacroNames && !Macros.count(MacroName))
616      MacroNames->push_back(MacroName);
617    Macros[MacroName] = std::make_pair(MacroBody, false);
618  }
619}
620
621/// Check the preprocessor options deserialized from the control block
622/// against the preprocessor options in an existing preprocessor.
623///
624/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
625/// \param Validate If true, validate preprocessor options. If false, allow
626///        macros defined by \p ExistingPPOpts to override those defined by
627///        \p PPOpts in SuggestedPredefines.
628static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
629                                     const PreprocessorOptions &ExistingPPOpts,
630                                     DiagnosticsEngine *Diags,
631                                     FileManager &FileMgr,
632                                     std::string &SuggestedPredefines,
633                                     const LangOptions &LangOpts,
634                                     bool Validate = true) {
635  // Check macro definitions.
636  MacroDefinitionsMap ASTFileMacros;
637  collectMacroDefinitions(PPOpts, ASTFileMacros);
638  MacroDefinitionsMap ExistingMacros;
639  SmallVector<StringRef, 4> ExistingMacroNames;
640  collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
641
642  for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
643    // Dig out the macro definition in the existing preprocessor options.
644    StringRef MacroName = ExistingMacroNames[I];
645    std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
646
647    // Check whether we know anything about this macro name or not.
648    llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
649        ASTFileMacros.find(MacroName);
650    if (!Validate || Known == ASTFileMacros.end()) {
651      // FIXME: Check whether this identifier was referenced anywhere in the
652      // AST file. If so, we should reject the AST file. Unfortunately, this
653      // information isn't in the control block. What shall we do about it?
654
655      if (Existing.second) {
656        SuggestedPredefines += "#undef ";
657        SuggestedPredefines += MacroName.str();
658        SuggestedPredefines += '\n';
659      } else {
660        SuggestedPredefines += "#define ";
661        SuggestedPredefines += MacroName.str();
662        SuggestedPredefines += ' ';
663        SuggestedPredefines += Existing.first.str();
664        SuggestedPredefines += '\n';
665      }
666      continue;
667    }
668
669    // If the macro was defined in one but undef'd in the other, we have a
670    // conflict.
671    if (Existing.second != Known->second.second) {
672      if (Diags) {
673        Diags->Report(diag::err_pch_macro_def_undef)
674          << MacroName << Known->second.second;
675      }
676      return true;
677    }
678
679    // If the macro was #undef'd in both, or if the macro bodies are identical,
680    // it's fine.
681    if (Existing.second || Existing.first == Known->second.first)
682      continue;
683
684    // The macro bodies differ; complain.
685    if (Diags) {
686      Diags->Report(diag::err_pch_macro_def_conflict)
687        << MacroName << Known->second.first << Existing.first;
688    }
689    return true;
690  }
691
692  // Check whether we're using predefines.
693  if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
694    if (Diags) {
695      Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
696    }
697    return true;
698  }
699
700  // Detailed record is important since it is used for the module cache hash.
701  if (LangOpts.Modules &&
702      PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
703    if (Diags) {
704      Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
705    }
706    return true;
707  }
708
709  // Compute the #include and #include_macros lines we need.
710  for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
711    StringRef File = ExistingPPOpts.Includes[I];
712
713    if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
714        !ExistingPPOpts.PCHThroughHeader.empty()) {
715      // In case the through header is an include, we must add all the includes
716      // to the predefines so the start point can be determined.
717      SuggestedPredefines += "#include \"";
718      SuggestedPredefines += File;
719      SuggestedPredefines += "\"\n";
720      continue;
721    }
722
723    if (File == ExistingPPOpts.ImplicitPCHInclude)
724      continue;
725
726    if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
727          != PPOpts.Includes.end())
728      continue;
729
730    SuggestedPredefines += "#include \"";
731    SuggestedPredefines += File;
732    SuggestedPredefines += "\"\n";
733  }
734
735  for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
736    StringRef File = ExistingPPOpts.MacroIncludes[I];
737    if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
738                  File)
739        != PPOpts.MacroIncludes.end())
740      continue;
741
742    SuggestedPredefines += "#__include_macros \"";
743    SuggestedPredefines += File;
744    SuggestedPredefines += "\"\n##\n";
745  }
746
747  return false;
748}
749
750bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
751                                           bool Complain,
752                                           std::string &SuggestedPredefines) {
753  const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
754
755  return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
756                                  Complain? &Reader.Diags : nullptr,
757                                  PP.getFileManager(),
758                                  SuggestedPredefines,
759                                  PP.getLangOpts());
760}
761
762bool SimpleASTReaderListener::ReadPreprocessorOptions(
763                                  const PreprocessorOptions &PPOpts,
764                                  bool Complain,
765                                  std::string &SuggestedPredefines) {
766  return checkPreprocessorOptions(PPOpts,
767                                  PP.getPreprocessorOpts(),
768                                  nullptr,
769                                  PP.getFileManager(),
770                                  SuggestedPredefines,
771                                  PP.getLangOpts(),
772                                  false);
773}
774
775/// Check the header search options deserialized from the control block
776/// against the header search options in an existing preprocessor.
777///
778/// \param Diags If non-null, produce diagnostics for any mismatches incurred.
779static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
780                                     StringRef SpecificModuleCachePath,
781                                     StringRef ExistingModuleCachePath,
782                                     DiagnosticsEngine *Diags,
783                                     const LangOptions &LangOpts) {
784  if (LangOpts.Modules) {
785    if (SpecificModuleCachePath != ExistingModuleCachePath) {
786      if (Diags)
787        Diags->Report(diag::err_pch_modulecache_mismatch)
788          << SpecificModuleCachePath << ExistingModuleCachePath;
789      return true;
790    }
791  }
792
793  return false;
794}
795
796bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
797                                           StringRef SpecificModuleCachePath,
798                                           bool Complain) {
799  return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
800                                  PP.getHeaderSearchInfo().getModuleCachePath(),
801                                  Complain ? &Reader.Diags : nullptr,
802                                  PP.getLangOpts());
803}
804
805void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
806  PP.setCounterValue(Value);
807}
808
809//===----------------------------------------------------------------------===//
810// AST reader implementation
811//===----------------------------------------------------------------------===//
812
813void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
814                                           bool TakeOwnership) {
815  DeserializationListener = Listener;
816  OwnsDeserializationListener = TakeOwnership;
817}
818
819unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
820  return serialization::ComputeHash(Sel);
821}
822
823std::pair<unsigned, unsigned>
824ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
825  using namespace llvm::support;
826
827  unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
828  unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
829  return std::make_pair(KeyLen, DataLen);
830}
831
832ASTSelectorLookupTrait::internal_key_type
833ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
834  using namespace llvm::support;
835
836  SelectorTable &SelTable = Reader.getContext().Selectors;
837  unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
838  IdentifierInfo *FirstII = Reader.getLocalIdentifier(
839      F, endian::readNext<uint32_t, little, unaligned>(d));
840  if (N == 0)
841    return SelTable.getNullarySelector(FirstII);
842  else if (N == 1)
843    return SelTable.getUnarySelector(FirstII);
844
845  SmallVector<IdentifierInfo *, 16> Args;
846  Args.push_back(FirstII);
847  for (unsigned I = 1; I != N; ++I)
848    Args.push_back(Reader.getLocalIdentifier(
849        F, endian::readNext<uint32_t, little, unaligned>(d)));
850
851  return SelTable.getSelector(N, Args.data());
852}
853
854ASTSelectorLookupTrait::data_type
855ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
856                                 unsigned DataLen) {
857  using namespace llvm::support;
858
859  data_type Result;
860
861  Result.ID = Reader.getGlobalSelectorID(
862      F, endian::readNext<uint32_t, little, unaligned>(d));
863  unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
864  unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
865  Result.InstanceBits = FullInstanceBits & 0x3;
866  Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
867  Result.FactoryBits = FullFactoryBits & 0x3;
868  Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
869  unsigned NumInstanceMethods = FullInstanceBits >> 3;
870  unsigned NumFactoryMethods = FullFactoryBits >> 3;
871
872  // Load instance methods
873  for (unsigned I = 0; I != NumInstanceMethods; ++I) {
874    if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
875            F, endian::readNext<uint32_t, little, unaligned>(d)))
876      Result.Instance.push_back(Method);
877  }
878
879  // Load factory methods
880  for (unsigned I = 0; I != NumFactoryMethods; ++I) {
881    if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
882            F, endian::readNext<uint32_t, little, unaligned>(d)))
883      Result.Factory.push_back(Method);
884  }
885
886  return Result;
887}
888
889unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
890  return llvm::djbHash(a);
891}
892
893std::pair<unsigned, unsigned>
894ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
895  using namespace llvm::support;
896
897  unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
898  unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
899  return std::make_pair(KeyLen, DataLen);
900}
901
902ASTIdentifierLookupTraitBase::internal_key_type
903ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
904  assert(n >= 2 && d[n-1] == '\0');
905  return StringRef((const char*) d, n-1);
906}
907
908/// Whether the given identifier is "interesting".
909static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
910                                    bool IsModule) {
911  return II.hadMacroDefinition() || II.isPoisoned() ||
912         (!IsModule && II.getObjCOrBuiltinID()) ||
913         II.hasRevertedTokenIDToIdentifier() ||
914         (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
915          II.getFETokenInfo());
916}
917
918static bool readBit(unsigned &Bits) {
919  bool Value = Bits & 0x1;
920  Bits >>= 1;
921  return Value;
922}
923
924IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
925  using namespace llvm::support;
926
927  unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
928  return Reader.getGlobalIdentifierID(F, RawID >> 1);
929}
930
931static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
932  if (!II.isFromAST()) {
933    II.setIsFromAST();
934    bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
935    if (isInterestingIdentifier(Reader, II, IsModule))
936      II.setChangedSinceDeserialization();
937  }
938}
939
940IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
941                                                   const unsigned char* d,
942                                                   unsigned DataLen) {
943  using namespace llvm::support;
944
945  unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
946  bool IsInteresting = RawID & 0x01;
947
948  // Wipe out the "is interesting" bit.
949  RawID = RawID >> 1;
950
951  // Build the IdentifierInfo and link the identifier ID with it.
952  IdentifierInfo *II = KnownII;
953  if (!II) {
954    II = &Reader.getIdentifierTable().getOwn(k);
955    KnownII = II;
956  }
957  markIdentifierFromAST(Reader, *II);
958  Reader.markIdentifierUpToDate(II);
959
960  IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
961  if (!IsInteresting) {
962    // For uninteresting identifiers, there's nothing else to do. Just notify
963    // the reader that we've finished loading this identifier.
964    Reader.SetIdentifierInfo(ID, II);
965    return II;
966  }
967
968  unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
969  unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
970  bool CPlusPlusOperatorKeyword = readBit(Bits);
971  bool HasRevertedTokenIDToIdentifier = readBit(Bits);
972  bool Poisoned = readBit(Bits);
973  bool ExtensionToken = readBit(Bits);
974  bool HadMacroDefinition = readBit(Bits);
975
976  assert(Bits == 0 && "Extra bits in the identifier?");
977  DataLen -= 8;
978
979  // Set or check the various bits in the IdentifierInfo structure.
980  // Token IDs are read-only.
981  if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
982    II->revertTokenIDToIdentifier();
983  if (!F.isModule())
984    II->setObjCOrBuiltinID(ObjCOrBuiltinID);
985  assert(II->isExtensionToken() == ExtensionToken &&
986         "Incorrect extension token flag");
987  (void)ExtensionToken;
988  if (Poisoned)
989    II->setIsPoisoned(true);
990  assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
991         "Incorrect C++ operator keyword flag");
992  (void)CPlusPlusOperatorKeyword;
993
994  // If this identifier is a macro, deserialize the macro
995  // definition.
996  if (HadMacroDefinition) {
997    uint32_t MacroDirectivesOffset =
998        endian::readNext<uint32_t, little, unaligned>(d);
999    DataLen -= 4;
1000
1001    Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1002  }
1003
1004  Reader.SetIdentifierInfo(ID, II);
1005
1006  // Read all of the declarations visible at global scope with this
1007  // name.
1008  if (DataLen > 0) {
1009    SmallVector<uint32_t, 4> DeclIDs;
1010    for (; DataLen > 0; DataLen -= 4)
1011      DeclIDs.push_back(Reader.getGlobalDeclID(
1012          F, endian::readNext<uint32_t, little, unaligned>(d)));
1013    Reader.SetGloballyVisibleDecls(II, DeclIDs);
1014  }
1015
1016  return II;
1017}
1018
1019DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1020    : Kind(Name.getNameKind()) {
1021  switch (Kind) {
1022  case DeclarationName::Identifier:
1023    Data = (uint64_t)Name.getAsIdentifierInfo();
1024    break;
1025  case DeclarationName::ObjCZeroArgSelector:
1026  case DeclarationName::ObjCOneArgSelector:
1027  case DeclarationName::ObjCMultiArgSelector:
1028    Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1029    break;
1030  case DeclarationName::CXXOperatorName:
1031    Data = Name.getCXXOverloadedOperator();
1032    break;
1033  case DeclarationName::CXXLiteralOperatorName:
1034    Data = (uint64_t)Name.getCXXLiteralIdentifier();
1035    break;
1036  case DeclarationName::CXXDeductionGuideName:
1037    Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1038               ->getDeclName().getAsIdentifierInfo();
1039    break;
1040  case DeclarationName::CXXConstructorName:
1041  case DeclarationName::CXXDestructorName:
1042  case DeclarationName::CXXConversionFunctionName:
1043  case DeclarationName::CXXUsingDirective:
1044    Data = 0;
1045    break;
1046  }
1047}
1048
1049unsigned DeclarationNameKey::getHash() const {
1050  llvm::FoldingSetNodeID ID;
1051  ID.AddInteger(Kind);
1052
1053  switch (Kind) {
1054  case DeclarationName::Identifier:
1055  case DeclarationName::CXXLiteralOperatorName:
1056  case DeclarationName::CXXDeductionGuideName:
1057    ID.AddString(((IdentifierInfo*)Data)->getName());
1058    break;
1059  case DeclarationName::ObjCZeroArgSelector:
1060  case DeclarationName::ObjCOneArgSelector:
1061  case DeclarationName::ObjCMultiArgSelector:
1062    ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1063    break;
1064  case DeclarationName::CXXOperatorName:
1065    ID.AddInteger((OverloadedOperatorKind)Data);
1066    break;
1067  case DeclarationName::CXXConstructorName:
1068  case DeclarationName::CXXDestructorName:
1069  case DeclarationName::CXXConversionFunctionName:
1070  case DeclarationName::CXXUsingDirective:
1071    break;
1072  }
1073
1074  return ID.ComputeHash();
1075}
1076
1077ModuleFile *
1078ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1079  using namespace llvm::support;
1080
1081  uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1082  return Reader.getLocalModuleFile(F, ModuleFileID);
1083}
1084
1085std::pair<unsigned, unsigned>
1086ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1087  using namespace llvm::support;
1088
1089  unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1090  unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1091  return std::make_pair(KeyLen, DataLen);
1092}
1093
1094ASTDeclContextNameLookupTrait::internal_key_type
1095ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1096  using namespace llvm::support;
1097
1098  auto Kind = (DeclarationName::NameKind)*d++;
1099  uint64_t Data;
1100  switch (Kind) {
1101  case DeclarationName::Identifier:
1102  case DeclarationName::CXXLiteralOperatorName:
1103  case DeclarationName::CXXDeductionGuideName:
1104    Data = (uint64_t)Reader.getLocalIdentifier(
1105        F, endian::readNext<uint32_t, little, unaligned>(d));
1106    break;
1107  case DeclarationName::ObjCZeroArgSelector:
1108  case DeclarationName::ObjCOneArgSelector:
1109  case DeclarationName::ObjCMultiArgSelector:
1110    Data =
1111        (uint64_t)Reader.getLocalSelector(
1112                             F, endian::readNext<uint32_t, little, unaligned>(
1113                                    d)).getAsOpaquePtr();
1114    break;
1115  case DeclarationName::CXXOperatorName:
1116    Data = *d++; // OverloadedOperatorKind
1117    break;
1118  case DeclarationName::CXXConstructorName:
1119  case DeclarationName::CXXDestructorName:
1120  case DeclarationName::CXXConversionFunctionName:
1121  case DeclarationName::CXXUsingDirective:
1122    Data = 0;
1123    break;
1124  }
1125
1126  return DeclarationNameKey(Kind, Data);
1127}
1128
1129void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1130                                                 const unsigned char *d,
1131                                                 unsigned DataLen,
1132                                                 data_type_builder &Val) {
1133  using namespace llvm::support;
1134
1135  for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1136    uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1137    Val.insert(Reader.getGlobalDeclID(F, LocalID));
1138  }
1139}
1140
1141bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1142                                              BitstreamCursor &Cursor,
1143                                              uint64_t Offset,
1144                                              DeclContext *DC) {
1145  assert(Offset != 0);
1146
1147  SavedStreamPosition SavedPosition(Cursor);
1148  if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1149    Error(std::move(Err));
1150    return true;
1151  }
1152
1153  RecordData Record;
1154  StringRef Blob;
1155  Expected<unsigned> MaybeCode = Cursor.ReadCode();
1156  if (!MaybeCode) {
1157    Error(MaybeCode.takeError());
1158    return true;
1159  }
1160  unsigned Code = MaybeCode.get();
1161
1162  Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1163  if (!MaybeRecCode) {
1164    Error(MaybeRecCode.takeError());
1165    return true;
1166  }
1167  unsigned RecCode = MaybeRecCode.get();
1168  if (RecCode != DECL_CONTEXT_LEXICAL) {
1169    Error("Expected lexical block");
1170    return true;
1171  }
1172
1173  assert(!isa<TranslationUnitDecl>(DC) &&
1174         "expected a TU_UPDATE_LEXICAL record for TU");
1175  // If we are handling a C++ class template instantiation, we can see multiple
1176  // lexical updates for the same record. It's important that we select only one
1177  // of them, so that field numbering works properly. Just pick the first one we
1178  // see.
1179  auto &Lex = LexicalDecls[DC];
1180  if (!Lex.first) {
1181    Lex = std::make_pair(
1182        &M, llvm::makeArrayRef(
1183                reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1184                    Blob.data()),
1185                Blob.size() / 4));
1186  }
1187  DC->setHasExternalLexicalStorage(true);
1188  return false;
1189}
1190
1191bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1192                                              BitstreamCursor &Cursor,
1193                                              uint64_t Offset,
1194                                              DeclID ID) {
1195  assert(Offset != 0);
1196
1197  SavedStreamPosition SavedPosition(Cursor);
1198  if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1199    Error(std::move(Err));
1200    return true;
1201  }
1202
1203  RecordData Record;
1204  StringRef Blob;
1205  Expected<unsigned> MaybeCode = Cursor.ReadCode();
1206  if (!MaybeCode) {
1207    Error(MaybeCode.takeError());
1208    return true;
1209  }
1210  unsigned Code = MaybeCode.get();
1211
1212  Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1213  if (!MaybeRecCode) {
1214    Error(MaybeRecCode.takeError());
1215    return true;
1216  }
1217  unsigned RecCode = MaybeRecCode.get();
1218  if (RecCode != DECL_CONTEXT_VISIBLE) {
1219    Error("Expected visible lookup table block");
1220    return true;
1221  }
1222
1223  // We can't safely determine the primary context yet, so delay attaching the
1224  // lookup table until we're done with recursive deserialization.
1225  auto *Data = (const unsigned char*)Blob.data();
1226  PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1227  return false;
1228}
1229
1230void ASTReader::Error(StringRef Msg) const {
1231  Error(diag::err_fe_pch_malformed, Msg);
1232  if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1233      !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1234    Diag(diag::note_module_cache_path)
1235      << PP.getHeaderSearchInfo().getModuleCachePath();
1236  }
1237}
1238
1239void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1240                      StringRef Arg3) const {
1241  if (Diags.isDiagnosticInFlight())
1242    Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1243  else
1244    Diag(DiagID) << Arg1 << Arg2 << Arg3;
1245}
1246
1247void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1248                      unsigned Select) const {
1249  if (!Diags.isDiagnosticInFlight())
1250    Diag(DiagID) << Arg1 << Arg2 << Select;
1251}
1252
1253void ASTReader::Error(llvm::Error &&Err) const {
1254  Error(toString(std::move(Err)));
1255}
1256
1257//===----------------------------------------------------------------------===//
1258// Source Manager Deserialization
1259//===----------------------------------------------------------------------===//
1260
1261/// Read the line table in the source manager block.
1262/// \returns true if there was an error.
1263bool ASTReader::ParseLineTable(ModuleFile &F,
1264                               const RecordData &Record) {
1265  unsigned Idx = 0;
1266  LineTableInfo &LineTable = SourceMgr.getLineTable();
1267
1268  // Parse the file names
1269  std::map<int, int> FileIDs;
1270  FileIDs[-1] = -1; // For unspecified filenames.
1271  for (unsigned I = 0; Record[Idx]; ++I) {
1272    // Extract the file name
1273    auto Filename = ReadPath(F, Record, Idx);
1274    FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1275  }
1276  ++Idx;
1277
1278  // Parse the line entries
1279  std::vector<LineEntry> Entries;
1280  while (Idx < Record.size()) {
1281    int FID = Record[Idx++];
1282    assert(FID >= 0 && "Serialized line entries for non-local file.");
1283    // Remap FileID from 1-based old view.
1284    FID += F.SLocEntryBaseID - 1;
1285
1286    // Extract the line entries
1287    unsigned NumEntries = Record[Idx++];
1288    assert(NumEntries && "no line entries for file ID");
1289    Entries.clear();
1290    Entries.reserve(NumEntries);
1291    for (unsigned I = 0; I != NumEntries; ++I) {
1292      unsigned FileOffset = Record[Idx++];
1293      unsigned LineNo = Record[Idx++];
1294      int FilenameID = FileIDs[Record[Idx++]];
1295      SrcMgr::CharacteristicKind FileKind
1296        = (SrcMgr::CharacteristicKind)Record[Idx++];
1297      unsigned IncludeOffset = Record[Idx++];
1298      Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1299                                       FileKind, IncludeOffset));
1300    }
1301    LineTable.AddEntry(FileID::get(FID), Entries);
1302  }
1303
1304  return false;
1305}
1306
1307/// Read a source manager block
1308bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1309  using namespace SrcMgr;
1310
1311  BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1312
1313  // Set the source-location entry cursor to the current position in
1314  // the stream. This cursor will be used to read the contents of the
1315  // source manager block initially, and then lazily read
1316  // source-location entries as needed.
1317  SLocEntryCursor = F.Stream;
1318
1319  // The stream itself is going to skip over the source manager block.
1320  if (llvm::Error Err = F.Stream.SkipBlock()) {
1321    Error(std::move(Err));
1322    return true;
1323  }
1324
1325  // Enter the source manager block.
1326  if (llvm::Error Err =
1327          SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1328    Error(std::move(Err));
1329    return true;
1330  }
1331  F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1332
1333  RecordData Record;
1334  while (true) {
1335    Expected<llvm::BitstreamEntry> MaybeE =
1336        SLocEntryCursor.advanceSkippingSubblocks();
1337    if (!MaybeE) {
1338      Error(MaybeE.takeError());
1339      return true;
1340    }
1341    llvm::BitstreamEntry E = MaybeE.get();
1342
1343    switch (E.Kind) {
1344    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1345    case llvm::BitstreamEntry::Error:
1346      Error("malformed block record in AST file");
1347      return true;
1348    case llvm::BitstreamEntry::EndBlock:
1349      return false;
1350    case llvm::BitstreamEntry::Record:
1351      // The interesting case.
1352      break;
1353    }
1354
1355    // Read a record.
1356    Record.clear();
1357    StringRef Blob;
1358    Expected<unsigned> MaybeRecord =
1359        SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1360    if (!MaybeRecord) {
1361      Error(MaybeRecord.takeError());
1362      return true;
1363    }
1364    switch (MaybeRecord.get()) {
1365    default:  // Default behavior: ignore.
1366      break;
1367
1368    case SM_SLOC_FILE_ENTRY:
1369    case SM_SLOC_BUFFER_ENTRY:
1370    case SM_SLOC_EXPANSION_ENTRY:
1371      // Once we hit one of the source location entries, we're done.
1372      return false;
1373    }
1374  }
1375}
1376
1377/// If a header file is not found at the path that we expect it to be
1378/// and the PCH file was moved from its original location, try to resolve the
1379/// file by assuming that header+PCH were moved together and the header is in
1380/// the same place relative to the PCH.
1381static std::string
1382resolveFileRelativeToOriginalDir(const std::string &Filename,
1383                                 const std::string &OriginalDir,
1384                                 const std::string &CurrDir) {
1385  assert(OriginalDir != CurrDir &&
1386         "No point trying to resolve the file if the PCH dir didn't change");
1387
1388  using namespace llvm::sys;
1389
1390  SmallString<128> filePath(Filename);
1391  fs::make_absolute(filePath);
1392  assert(path::is_absolute(OriginalDir));
1393  SmallString<128> currPCHPath(CurrDir);
1394
1395  path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1396                       fileDirE = path::end(path::parent_path(filePath));
1397  path::const_iterator origDirI = path::begin(OriginalDir),
1398                       origDirE = path::end(OriginalDir);
1399  // Skip the common path components from filePath and OriginalDir.
1400  while (fileDirI != fileDirE && origDirI != origDirE &&
1401         *fileDirI == *origDirI) {
1402    ++fileDirI;
1403    ++origDirI;
1404  }
1405  for (; origDirI != origDirE; ++origDirI)
1406    path::append(currPCHPath, "..");
1407  path::append(currPCHPath, fileDirI, fileDirE);
1408  path::append(currPCHPath, path::filename(Filename));
1409  return std::string(currPCHPath.str());
1410}
1411
1412bool ASTReader::ReadSLocEntry(int ID) {
1413  if (ID == 0)
1414    return false;
1415
1416  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1417    Error("source location entry ID out-of-range for AST file");
1418    return true;
1419  }
1420
1421  // Local helper to read the (possibly-compressed) buffer data following the
1422  // entry record.
1423  auto ReadBuffer = [this](
1424      BitstreamCursor &SLocEntryCursor,
1425      StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1426    RecordData Record;
1427    StringRef Blob;
1428    Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1429    if (!MaybeCode) {
1430      Error(MaybeCode.takeError());
1431      return nullptr;
1432    }
1433    unsigned Code = MaybeCode.get();
1434
1435    Expected<unsigned> MaybeRecCode =
1436        SLocEntryCursor.readRecord(Code, Record, &Blob);
1437    if (!MaybeRecCode) {
1438      Error(MaybeRecCode.takeError());
1439      return nullptr;
1440    }
1441    unsigned RecCode = MaybeRecCode.get();
1442
1443    if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1444      if (!llvm::zlib::isAvailable()) {
1445        Error("zlib is not available");
1446        return nullptr;
1447      }
1448      SmallString<0> Uncompressed;
1449      if (llvm::Error E =
1450              llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1451        Error("could not decompress embedded file contents: " +
1452              llvm::toString(std::move(E)));
1453        return nullptr;
1454      }
1455      return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1456    } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1457      return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1458    } else {
1459      Error("AST record has invalid code");
1460      return nullptr;
1461    }
1462  };
1463
1464  ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1465  if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1466          F->SLocEntryOffsetsBase +
1467          F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1468    Error(std::move(Err));
1469    return true;
1470  }
1471
1472  BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1473  unsigned BaseOffset = F->SLocEntryBaseOffset;
1474
1475  ++NumSLocEntriesRead;
1476  Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1477  if (!MaybeEntry) {
1478    Error(MaybeEntry.takeError());
1479    return true;
1480  }
1481  llvm::BitstreamEntry Entry = MaybeEntry.get();
1482
1483  if (Entry.Kind != llvm::BitstreamEntry::Record) {
1484    Error("incorrectly-formatted source location entry in AST file");
1485    return true;
1486  }
1487
1488  RecordData Record;
1489  StringRef Blob;
1490  Expected<unsigned> MaybeSLOC =
1491      SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1492  if (!MaybeSLOC) {
1493    Error(MaybeSLOC.takeError());
1494    return true;
1495  }
1496  switch (MaybeSLOC.get()) {
1497  default:
1498    Error("incorrectly-formatted source location entry in AST file");
1499    return true;
1500
1501  case SM_SLOC_FILE_ENTRY: {
1502    // We will detect whether a file changed and return 'Failure' for it, but
1503    // we will also try to fail gracefully by setting up the SLocEntry.
1504    unsigned InputID = Record[4];
1505    InputFile IF = getInputFile(*F, InputID);
1506    const FileEntry *File = IF.getFile();
1507    bool OverriddenBuffer = IF.isOverridden();
1508
1509    // Note that we only check if a File was returned. If it was out-of-date
1510    // we have complained but we will continue creating a FileID to recover
1511    // gracefully.
1512    if (!File)
1513      return true;
1514
1515    SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1516    if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1517      // This is the module's main file.
1518      IncludeLoc = getImportLocation(F);
1519    }
1520    SrcMgr::CharacteristicKind
1521      FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1522    // FIXME: The FileID should be created from the FileEntryRef.
1523    FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1524                                        ID, BaseOffset + Record[0]);
1525    SrcMgr::FileInfo &FileInfo =
1526          const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1527    FileInfo.NumCreatedFIDs = Record[5];
1528    if (Record[3])
1529      FileInfo.setHasLineDirectives();
1530
1531    unsigned NumFileDecls = Record[7];
1532    if (NumFileDecls && ContextObj) {
1533      const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1534      assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1535      FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1536                                                             NumFileDecls));
1537    }
1538
1539    const SrcMgr::ContentCache *ContentCache
1540      = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1541    if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1542        ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1543        !ContentCache->getRawBuffer()) {
1544      auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1545      if (!Buffer)
1546        return true;
1547      SourceMgr.overrideFileContents(File, std::move(Buffer));
1548    }
1549
1550    break;
1551  }
1552
1553  case SM_SLOC_BUFFER_ENTRY: {
1554    const char *Name = Blob.data();
1555    unsigned Offset = Record[0];
1556    SrcMgr::CharacteristicKind
1557      FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1558    SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1559    if (IncludeLoc.isInvalid() && F->isModule()) {
1560      IncludeLoc = getImportLocation(F);
1561    }
1562
1563    auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1564    if (!Buffer)
1565      return true;
1566    SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1567                           BaseOffset + Offset, IncludeLoc);
1568    break;
1569  }
1570
1571  case SM_SLOC_EXPANSION_ENTRY: {
1572    SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1573    SourceMgr.createExpansionLoc(SpellingLoc,
1574                                     ReadSourceLocation(*F, Record[2]),
1575                                     ReadSourceLocation(*F, Record[3]),
1576                                     Record[5],
1577                                     Record[4],
1578                                     ID,
1579                                     BaseOffset + Record[0]);
1580    break;
1581  }
1582  }
1583
1584  return false;
1585}
1586
1587std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1588  if (ID == 0)
1589    return std::make_pair(SourceLocation(), "");
1590
1591  if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1592    Error("source location entry ID out-of-range for AST file");
1593    return std::make_pair(SourceLocation(), "");
1594  }
1595
1596  // Find which module file this entry lands in.
1597  ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1598  if (!M->isModule())
1599    return std::make_pair(SourceLocation(), "");
1600
1601  // FIXME: Can we map this down to a particular submodule? That would be
1602  // ideal.
1603  return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1604}
1605
1606/// Find the location where the module F is imported.
1607SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1608  if (F->ImportLoc.isValid())
1609    return F->ImportLoc;
1610
1611  // Otherwise we have a PCH. It's considered to be "imported" at the first
1612  // location of its includer.
1613  if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1614    // Main file is the importer.
1615    assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1616    return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1617  }
1618  return F->ImportedBy[0]->FirstLoc;
1619}
1620
1621/// Enter a subblock of the specified BlockID with the specified cursor. Read
1622/// the abbreviations that are at the top of the block and then leave the cursor
1623/// pointing into the block.
1624bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1625                                 uint64_t *StartOfBlockOffset) {
1626  if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1627    // FIXME this drops errors on the floor.
1628    consumeError(std::move(Err));
1629    return true;
1630  }
1631
1632  if (StartOfBlockOffset)
1633    *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1634
1635  while (true) {
1636    uint64_t Offset = Cursor.GetCurrentBitNo();
1637    Expected<unsigned> MaybeCode = Cursor.ReadCode();
1638    if (!MaybeCode) {
1639      // FIXME this drops errors on the floor.
1640      consumeError(MaybeCode.takeError());
1641      return true;
1642    }
1643    unsigned Code = MaybeCode.get();
1644
1645    // We expect all abbrevs to be at the start of the block.
1646    if (Code != llvm::bitc::DEFINE_ABBREV) {
1647      if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1648        // FIXME this drops errors on the floor.
1649        consumeError(std::move(Err));
1650        return true;
1651      }
1652      return false;
1653    }
1654    if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1655      // FIXME this drops errors on the floor.
1656      consumeError(std::move(Err));
1657      return true;
1658    }
1659  }
1660}
1661
1662Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1663                           unsigned &Idx) {
1664  Token Tok;
1665  Tok.startToken();
1666  Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1667  Tok.setLength(Record[Idx++]);
1668  if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1669    Tok.setIdentifierInfo(II);
1670  Tok.setKind((tok::TokenKind)Record[Idx++]);
1671  Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1672  return Tok;
1673}
1674
1675MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1676  BitstreamCursor &Stream = F.MacroCursor;
1677
1678  // Keep track of where we are in the stream, then jump back there
1679  // after reading this macro.
1680  SavedStreamPosition SavedPosition(Stream);
1681
1682  if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1683    // FIXME this drops errors on the floor.
1684    consumeError(std::move(Err));
1685    return nullptr;
1686  }
1687  RecordData Record;
1688  SmallVector<IdentifierInfo*, 16> MacroParams;
1689  MacroInfo *Macro = nullptr;
1690
1691  while (true) {
1692    // Advance to the next record, but if we get to the end of the block, don't
1693    // pop it (removing all the abbreviations from the cursor) since we want to
1694    // be able to reseek within the block and read entries.
1695    unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1696    Expected<llvm::BitstreamEntry> MaybeEntry =
1697        Stream.advanceSkippingSubblocks(Flags);
1698    if (!MaybeEntry) {
1699      Error(MaybeEntry.takeError());
1700      return Macro;
1701    }
1702    llvm::BitstreamEntry Entry = MaybeEntry.get();
1703
1704    switch (Entry.Kind) {
1705    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1706    case llvm::BitstreamEntry::Error:
1707      Error("malformed block record in AST file");
1708      return Macro;
1709    case llvm::BitstreamEntry::EndBlock:
1710      return Macro;
1711    case llvm::BitstreamEntry::Record:
1712      // The interesting case.
1713      break;
1714    }
1715
1716    // Read a record.
1717    Record.clear();
1718    PreprocessorRecordTypes RecType;
1719    if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1720      RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1721    else {
1722      Error(MaybeRecType.takeError());
1723      return Macro;
1724    }
1725    switch (RecType) {
1726    case PP_MODULE_MACRO:
1727    case PP_MACRO_DIRECTIVE_HISTORY:
1728      return Macro;
1729
1730    case PP_MACRO_OBJECT_LIKE:
1731    case PP_MACRO_FUNCTION_LIKE: {
1732      // If we already have a macro, that means that we've hit the end
1733      // of the definition of the macro we were looking for. We're
1734      // done.
1735      if (Macro)
1736        return Macro;
1737
1738      unsigned NextIndex = 1; // Skip identifier ID.
1739      SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1740      MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1741      MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1742      MI->setIsUsed(Record[NextIndex++]);
1743      MI->setUsedForHeaderGuard(Record[NextIndex++]);
1744
1745      if (RecType == PP_MACRO_FUNCTION_LIKE) {
1746        // Decode function-like macro info.
1747        bool isC99VarArgs = Record[NextIndex++];
1748        bool isGNUVarArgs = Record[NextIndex++];
1749        bool hasCommaPasting = Record[NextIndex++];
1750        MacroParams.clear();
1751        unsigned NumArgs = Record[NextIndex++];
1752        for (unsigned i = 0; i != NumArgs; ++i)
1753          MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1754
1755        // Install function-like macro info.
1756        MI->setIsFunctionLike();
1757        if (isC99VarArgs) MI->setIsC99Varargs();
1758        if (isGNUVarArgs) MI->setIsGNUVarargs();
1759        if (hasCommaPasting) MI->setHasCommaPasting();
1760        MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1761      }
1762
1763      // Remember that we saw this macro last so that we add the tokens that
1764      // form its body to it.
1765      Macro = MI;
1766
1767      if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1768          Record[NextIndex]) {
1769        // We have a macro definition. Register the association
1770        PreprocessedEntityID
1771            GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1772        PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1773        PreprocessingRecord::PPEntityID PPID =
1774            PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1775        MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1776            PPRec.getPreprocessedEntity(PPID));
1777        if (PPDef)
1778          PPRec.RegisterMacroDefinition(Macro, PPDef);
1779      }
1780
1781      ++NumMacrosRead;
1782      break;
1783    }
1784
1785    case PP_TOKEN: {
1786      // If we see a TOKEN before a PP_MACRO_*, then the file is
1787      // erroneous, just pretend we didn't see this.
1788      if (!Macro) break;
1789
1790      unsigned Idx = 0;
1791      Token Tok = ReadToken(F, Record, Idx);
1792      Macro->AddTokenToBody(Tok);
1793      break;
1794    }
1795    }
1796  }
1797}
1798
1799PreprocessedEntityID
1800ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1801                                         unsigned LocalID) const {
1802  if (!M.ModuleOffsetMap.empty())
1803    ReadModuleOffsetMap(M);
1804
1805  ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1806    I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1807  assert(I != M.PreprocessedEntityRemap.end()
1808         && "Invalid index into preprocessed entity index remap");
1809
1810  return LocalID + I->second;
1811}
1812
1813unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1814  return llvm::hash_combine(ikey.Size, ikey.ModTime);
1815}
1816
1817HeaderFileInfoTrait::internal_key_type
1818HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1819  internal_key_type ikey = {FE->getSize(),
1820                            M.HasTimestamps ? FE->getModificationTime() : 0,
1821                            FE->getName(), /*Imported*/ false};
1822  return ikey;
1823}
1824
1825bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1826  if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1827    return false;
1828
1829  if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1830    return true;
1831
1832  // Determine whether the actual files are equivalent.
1833  FileManager &FileMgr = Reader.getFileManager();
1834  auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1835    if (!Key.Imported) {
1836      if (auto File = FileMgr.getFile(Key.Filename))
1837        return *File;
1838      return nullptr;
1839    }
1840
1841    std::string Resolved = std::string(Key.Filename);
1842    Reader.ResolveImportedPath(M, Resolved);
1843    if (auto File = FileMgr.getFile(Resolved))
1844      return *File;
1845    return nullptr;
1846  };
1847
1848  const FileEntry *FEA = GetFile(a);
1849  const FileEntry *FEB = GetFile(b);
1850  return FEA && FEA == FEB;
1851}
1852
1853std::pair<unsigned, unsigned>
1854HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1855  using namespace llvm::support;
1856
1857  unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1858  unsigned DataLen = (unsigned) *d++;
1859  return std::make_pair(KeyLen, DataLen);
1860}
1861
1862HeaderFileInfoTrait::internal_key_type
1863HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1864  using namespace llvm::support;
1865
1866  internal_key_type ikey;
1867  ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1868  ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1869  ikey.Filename = (const char *)d;
1870  ikey.Imported = true;
1871  return ikey;
1872}
1873
1874HeaderFileInfoTrait::data_type
1875HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1876                              unsigned DataLen) {
1877  using namespace llvm::support;
1878
1879  const unsigned char *End = d + DataLen;
1880  HeaderFileInfo HFI;
1881  unsigned Flags = *d++;
1882  // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1883  HFI.isImport |= (Flags >> 5) & 0x01;
1884  HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1885  HFI.DirInfo = (Flags >> 1) & 0x07;
1886  HFI.IndexHeaderMapHeader = Flags & 0x01;
1887  // FIXME: Find a better way to handle this. Maybe just store a
1888  // "has been included" flag?
1889  HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1890                             HFI.NumIncludes);
1891  HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1892      M, endian::readNext<uint32_t, little, unaligned>(d));
1893  if (unsigned FrameworkOffset =
1894          endian::readNext<uint32_t, little, unaligned>(d)) {
1895    // The framework offset is 1 greater than the actual offset,
1896    // since 0 is used as an indicator for "no framework name".
1897    StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1898    HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1899  }
1900
1901  assert((End - d) % 4 == 0 &&
1902         "Wrong data length in HeaderFileInfo deserialization");
1903  while (d != End) {
1904    uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1905    auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1906    LocalSMID >>= 2;
1907
1908    // This header is part of a module. Associate it with the module to enable
1909    // implicit module import.
1910    SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1911    Module *Mod = Reader.getSubmodule(GlobalSMID);
1912    FileManager &FileMgr = Reader.getFileManager();
1913    ModuleMap &ModMap =
1914        Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1915
1916    std::string Filename = std::string(key.Filename);
1917    if (key.Imported)
1918      Reader.ResolveImportedPath(M, Filename);
1919    // FIXME: This is not always the right filename-as-written, but we're not
1920    // going to use this information to rebuild the module, so it doesn't make
1921    // a lot of difference.
1922    Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
1923    ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1924    HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1925  }
1926
1927  // This HeaderFileInfo was externally loaded.
1928  HFI.External = true;
1929  HFI.IsValid = true;
1930  return HFI;
1931}
1932
1933void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1934                                uint32_t MacroDirectivesOffset) {
1935  assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1936  PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1937}
1938
1939void ASTReader::ReadDefinedMacros() {
1940  // Note that we are loading defined macros.
1941  Deserializing Macros(this);
1942
1943  for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1944    BitstreamCursor &MacroCursor = I.MacroCursor;
1945
1946    // If there was no preprocessor block, skip this file.
1947    if (MacroCursor.getBitcodeBytes().empty())
1948      continue;
1949
1950    BitstreamCursor Cursor = MacroCursor;
1951    if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1952      Error(std::move(Err));
1953      return;
1954    }
1955
1956    RecordData Record;
1957    while (true) {
1958      Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1959      if (!MaybeE) {
1960        Error(MaybeE.takeError());
1961        return;
1962      }
1963      llvm::BitstreamEntry E = MaybeE.get();
1964
1965      switch (E.Kind) {
1966      case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1967      case llvm::BitstreamEntry::Error:
1968        Error("malformed block record in AST file");
1969        return;
1970      case llvm::BitstreamEntry::EndBlock:
1971        goto NextCursor;
1972
1973      case llvm::BitstreamEntry::Record: {
1974        Record.clear();
1975        Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1976        if (!MaybeRecord) {
1977          Error(MaybeRecord.takeError());
1978          return;
1979        }
1980        switch (MaybeRecord.get()) {
1981        default:  // Default behavior: ignore.
1982          break;
1983
1984        case PP_MACRO_OBJECT_LIKE:
1985        case PP_MACRO_FUNCTION_LIKE: {
1986          IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1987          if (II->isOutOfDate())
1988            updateOutOfDateIdentifier(*II);
1989          break;
1990        }
1991
1992        case PP_TOKEN:
1993          // Ignore tokens.
1994          break;
1995        }
1996        break;
1997      }
1998      }
1999    }
2000    NextCursor:  ;
2001  }
2002}
2003
2004namespace {
2005
2006  /// Visitor class used to look up identifirs in an AST file.
2007  class IdentifierLookupVisitor {
2008    StringRef Name;
2009    unsigned NameHash;
2010    unsigned PriorGeneration;
2011    unsigned &NumIdentifierLookups;
2012    unsigned &NumIdentifierLookupHits;
2013    IdentifierInfo *Found = nullptr;
2014
2015  public:
2016    IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2017                            unsigned &NumIdentifierLookups,
2018                            unsigned &NumIdentifierLookupHits)
2019      : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2020        PriorGeneration(PriorGeneration),
2021        NumIdentifierLookups(NumIdentifierLookups),
2022        NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2023
2024    bool operator()(ModuleFile &M) {
2025      // If we've already searched this module file, skip it now.
2026      if (M.Generation <= PriorGeneration)
2027        return true;
2028
2029      ASTIdentifierLookupTable *IdTable
2030        = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2031      if (!IdTable)
2032        return false;
2033
2034      ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2035                                     Found);
2036      ++NumIdentifierLookups;
2037      ASTIdentifierLookupTable::iterator Pos =
2038          IdTable->find_hashed(Name, NameHash, &Trait);
2039      if (Pos == IdTable->end())
2040        return false;
2041
2042      // Dereferencing the iterator has the effect of building the
2043      // IdentifierInfo node and populating it with the various
2044      // declarations it needs.
2045      ++NumIdentifierLookupHits;
2046      Found = *Pos;
2047      return true;
2048    }
2049
2050    // Retrieve the identifier info found within the module
2051    // files.
2052    IdentifierInfo *getIdentifierInfo() const { return Found; }
2053  };
2054
2055} // namespace
2056
2057void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2058  // Note that we are loading an identifier.
2059  Deserializing AnIdentifier(this);
2060
2061  unsigned PriorGeneration = 0;
2062  if (getContext().getLangOpts().Modules)
2063    PriorGeneration = IdentifierGeneration[&II];
2064
2065  // If there is a global index, look there first to determine which modules
2066  // provably do not have any results for this identifier.
2067  GlobalModuleIndex::HitSet Hits;
2068  GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2069  if (!loadGlobalIndex()) {
2070    if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2071      HitsPtr = &Hits;
2072    }
2073  }
2074
2075  IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2076                                  NumIdentifierLookups,
2077                                  NumIdentifierLookupHits);
2078  ModuleMgr.visit(Visitor, HitsPtr);
2079  markIdentifierUpToDate(&II);
2080}
2081
2082void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2083  if (!II)
2084    return;
2085
2086  II->setOutOfDate(false);
2087
2088  // Update the generation for this identifier.
2089  if (getContext().getLangOpts().Modules)
2090    IdentifierGeneration[II] = getGeneration();
2091}
2092
2093void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2094                                    const PendingMacroInfo &PMInfo) {
2095  ModuleFile &M = *PMInfo.M;
2096
2097  BitstreamCursor &Cursor = M.MacroCursor;
2098  SavedStreamPosition SavedPosition(Cursor);
2099  if (llvm::Error Err =
2100          Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2101    Error(std::move(Err));
2102    return;
2103  }
2104
2105  struct ModuleMacroRecord {
2106    SubmoduleID SubModID;
2107    MacroInfo *MI;
2108    SmallVector<SubmoduleID, 8> Overrides;
2109  };
2110  llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2111
2112  // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2113  // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2114  // macro histroy.
2115  RecordData Record;
2116  while (true) {
2117    Expected<llvm::BitstreamEntry> MaybeEntry =
2118        Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2119    if (!MaybeEntry) {
2120      Error(MaybeEntry.takeError());
2121      return;
2122    }
2123    llvm::BitstreamEntry Entry = MaybeEntry.get();
2124
2125    if (Entry.Kind != llvm::BitstreamEntry::Record) {
2126      Error("malformed block record in AST file");
2127      return;
2128    }
2129
2130    Record.clear();
2131    Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2132    if (!MaybePP) {
2133      Error(MaybePP.takeError());
2134      return;
2135    }
2136    switch ((PreprocessorRecordTypes)MaybePP.get()) {
2137    case PP_MACRO_DIRECTIVE_HISTORY:
2138      break;
2139
2140    case PP_MODULE_MACRO: {
2141      ModuleMacros.push_back(ModuleMacroRecord());
2142      auto &Info = ModuleMacros.back();
2143      Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2144      Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2145      for (int I = 2, N = Record.size(); I != N; ++I)
2146        Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2147      continue;
2148    }
2149
2150    default:
2151      Error("malformed block record in AST file");
2152      return;
2153    }
2154
2155    // We found the macro directive history; that's the last record
2156    // for this macro.
2157    break;
2158  }
2159
2160  // Module macros are listed in reverse dependency order.
2161  {
2162    std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2163    llvm::SmallVector<ModuleMacro*, 8> Overrides;
2164    for (auto &MMR : ModuleMacros) {
2165      Overrides.clear();
2166      for (unsigned ModID : MMR.Overrides) {
2167        Module *Mod = getSubmodule(ModID);
2168        auto *Macro = PP.getModuleMacro(Mod, II);
2169        assert(Macro && "missing definition for overridden macro");
2170        Overrides.push_back(Macro);
2171      }
2172
2173      bool Inserted = false;
2174      Module *Owner = getSubmodule(MMR.SubModID);
2175      PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2176    }
2177  }
2178
2179  // Don't read the directive history for a module; we don't have anywhere
2180  // to put it.
2181  if (M.isModule())
2182    return;
2183
2184  // Deserialize the macro directives history in reverse source-order.
2185  MacroDirective *Latest = nullptr, *Earliest = nullptr;
2186  unsigned Idx = 0, N = Record.size();
2187  while (Idx < N) {
2188    MacroDirective *MD = nullptr;
2189    SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2190    MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2191    switch (K) {
2192    case MacroDirective::MD_Define: {
2193      MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2194      MD = PP.AllocateDefMacroDirective(MI, Loc);
2195      break;
2196    }
2197    case MacroDirective::MD_Undefine:
2198      MD = PP.AllocateUndefMacroDirective(Loc);
2199      break;
2200    case MacroDirective::MD_Visibility:
2201      bool isPublic = Record[Idx++];
2202      MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2203      break;
2204    }
2205
2206    if (!Latest)
2207      Latest = MD;
2208    if (Earliest)
2209      Earliest->setPrevious(MD);
2210    Earliest = MD;
2211  }
2212
2213  if (Latest)
2214    PP.setLoadedMacroDirective(II, Earliest, Latest);
2215}
2216
2217ASTReader::InputFileInfo
2218ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2219  // Go find this input file.
2220  BitstreamCursor &Cursor = F.InputFilesCursor;
2221  SavedStreamPosition SavedPosition(Cursor);
2222  if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2223    // FIXME this drops errors on the floor.
2224    consumeError(std::move(Err));
2225  }
2226
2227  Expected<unsigned> MaybeCode = Cursor.ReadCode();
2228  if (!MaybeCode) {
2229    // FIXME this drops errors on the floor.
2230    consumeError(MaybeCode.takeError());
2231  }
2232  unsigned Code = MaybeCode.get();
2233  RecordData Record;
2234  StringRef Blob;
2235
2236  if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2237    assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2238           "invalid record type for input file");
2239  else {
2240    // FIXME this drops errors on the floor.
2241    consumeError(Maybe.takeError());
2242  }
2243
2244  assert(Record[0] == ID && "Bogus stored ID or offset");
2245  InputFileInfo R;
2246  R.StoredSize = static_cast<off_t>(Record[1]);
2247  R.StoredTime = static_cast<time_t>(Record[2]);
2248  R.Overridden = static_cast<bool>(Record[3]);
2249  R.Transient = static_cast<bool>(Record[4]);
2250  R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2251  R.Filename = std::string(Blob);
2252  ResolveImportedPath(F, R.Filename);
2253
2254  Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2255  if (!MaybeEntry) // FIXME this drops errors on the floor.
2256    consumeError(MaybeEntry.takeError());
2257  llvm::BitstreamEntry Entry = MaybeEntry.get();
2258  assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2259         "expected record type for input file hash");
2260
2261  Record.clear();
2262  if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2263    assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2264           "invalid record type for input file hash");
2265  else {
2266    // FIXME this drops errors on the floor.
2267    consumeError(Maybe.takeError());
2268  }
2269  R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2270                  static_cast<uint64_t>(Record[0]);
2271  return R;
2272}
2273
2274static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2275InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2276  // If this ID is bogus, just return an empty input file.
2277  if (ID == 0 || ID > F.InputFilesLoaded.size())
2278    return InputFile();
2279
2280  // If we've already loaded this input file, return it.
2281  if (F.InputFilesLoaded[ID-1].getFile())
2282    return F.InputFilesLoaded[ID-1];
2283
2284  if (F.InputFilesLoaded[ID-1].isNotFound())
2285    return InputFile();
2286
2287  // Go find this input file.
2288  BitstreamCursor &Cursor = F.InputFilesCursor;
2289  SavedStreamPosition SavedPosition(Cursor);
2290  if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2291    // FIXME this drops errors on the floor.
2292    consumeError(std::move(Err));
2293  }
2294
2295  InputFileInfo FI = readInputFileInfo(F, ID);
2296  off_t StoredSize = FI.StoredSize;
2297  time_t StoredTime = FI.StoredTime;
2298  bool Overridden = FI.Overridden;
2299  bool Transient = FI.Transient;
2300  StringRef Filename = FI.Filename;
2301  uint64_t StoredContentHash = FI.ContentHash;
2302
2303  const FileEntry *File = nullptr;
2304  if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2305    File = *FE;
2306
2307  // If we didn't find the file, resolve it relative to the
2308  // original directory from which this AST file was created.
2309  if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2310      F.OriginalDir != F.BaseDirectory) {
2311    std::string Resolved = resolveFileRelativeToOriginalDir(
2312        std::string(Filename), F.OriginalDir, F.BaseDirectory);
2313    if (!Resolved.empty())
2314      if (auto FE = FileMgr.getFile(Resolved))
2315        File = *FE;
2316  }
2317
2318  // For an overridden file, create a virtual file with the stored
2319  // size/timestamp.
2320  if ((Overridden || Transient) && File == nullptr)
2321    File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2322
2323  if (File == nullptr) {
2324    if (Complain) {
2325      std::string ErrorStr = "could not find file '";
2326      ErrorStr += Filename;
2327      ErrorStr += "' referenced by AST file '";
2328      ErrorStr += F.FileName;
2329      ErrorStr += "'";
2330      Error(ErrorStr);
2331    }
2332    // Record that we didn't find the file.
2333    F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2334    return InputFile();
2335  }
2336
2337  // Check if there was a request to override the contents of the file
2338  // that was part of the precompiled header. Overriding such a file
2339  // can lead to problems when lexing using the source locations from the
2340  // PCH.
2341  SourceManager &SM = getSourceManager();
2342  // FIXME: Reject if the overrides are different.
2343  if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2344    if (Complain)
2345      Error(diag::err_fe_pch_file_overridden, Filename);
2346
2347    // After emitting the diagnostic, bypass the overriding file to recover
2348    // (this creates a separate FileEntry).
2349    File = SM.bypassFileContentsOverride(*File);
2350    if (!File) {
2351      F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2352      return InputFile();
2353    }
2354  }
2355
2356  enum ModificationType {
2357    Size,
2358    ModTime,
2359    Content,
2360    None,
2361  };
2362  auto HasInputFileChanged = [&]() {
2363    if (StoredSize != File->getSize())
2364      return ModificationType::Size;
2365    if (!DisableValidation && StoredTime &&
2366        StoredTime != File->getModificationTime()) {
2367      // In case the modification time changes but not the content,
2368      // accept the cached file as legit.
2369      if (ValidateASTInputFilesContent &&
2370          StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2371        auto MemBuffOrError = FileMgr.getBufferForFile(File);
2372        if (!MemBuffOrError) {
2373          if (!Complain)
2374            return ModificationType::ModTime;
2375          std::string ErrorStr = "could not get buffer for file '";
2376          ErrorStr += File->getName();
2377          ErrorStr += "'";
2378          Error(ErrorStr);
2379          return ModificationType::ModTime;
2380        }
2381
2382        auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2383        if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2384          return ModificationType::None;
2385        return ModificationType::Content;
2386      }
2387      return ModificationType::ModTime;
2388    }
2389    return ModificationType::None;
2390  };
2391
2392  bool IsOutOfDate = false;
2393  auto FileChange = HasInputFileChanged();
2394  // For an overridden file, there is nothing to validate.
2395  if (!Overridden && FileChange != ModificationType::None) {
2396    if (Complain) {
2397      // Build a list of the PCH imports that got us here (in reverse).
2398      SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2399      while (!ImportStack.back()->ImportedBy.empty())
2400        ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2401
2402      // The top-level PCH is stale.
2403      StringRef TopLevelPCHName(ImportStack.back()->FileName);
2404      unsigned DiagnosticKind =
2405          moduleKindForDiagnostic(ImportStack.back()->Kind);
2406      if (DiagnosticKind == 0)
2407        Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2408              (unsigned)FileChange);
2409      else if (DiagnosticKind == 1)
2410        Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2411              (unsigned)FileChange);
2412      else
2413        Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2414              (unsigned)FileChange);
2415
2416      // Print the import stack.
2417      if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2418        Diag(diag::note_pch_required_by)
2419          << Filename << ImportStack[0]->FileName;
2420        for (unsigned I = 1; I < ImportStack.size(); ++I)
2421          Diag(diag::note_pch_required_by)
2422            << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2423      }
2424
2425      if (!Diags.isDiagnosticInFlight())
2426        Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2427    }
2428
2429    IsOutOfDate = true;
2430  }
2431  // FIXME: If the file is overridden and we've already opened it,
2432  // issue an error (or split it into a separate FileEntry).
2433
2434  InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2435
2436  // Note that we've loaded this input file.
2437  F.InputFilesLoaded[ID-1] = IF;
2438  return IF;
2439}
2440
2441/// If we are loading a relocatable PCH or module file, and the filename
2442/// is not an absolute path, add the system or module root to the beginning of
2443/// the file name.
2444void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2445  // Resolve relative to the base directory, if we have one.
2446  if (!M.BaseDirectory.empty())
2447    return ResolveImportedPath(Filename, M.BaseDirectory);
2448}
2449
2450void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2451  if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2452    return;
2453
2454  SmallString<128> Buffer;
2455  llvm::sys::path::append(Buffer, Prefix, Filename);
2456  Filename.assign(Buffer.begin(), Buffer.end());
2457}
2458
2459static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2460  switch (ARR) {
2461  case ASTReader::Failure: return true;
2462  case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2463  case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2464  case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2465  case ASTReader::ConfigurationMismatch:
2466    return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2467  case ASTReader::HadErrors: return true;
2468  case ASTReader::Success: return false;
2469  }
2470
2471  llvm_unreachable("unknown ASTReadResult");
2472}
2473
2474ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2475    BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2476    bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2477    std::string &SuggestedPredefines) {
2478  if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2479    // FIXME this drops errors on the floor.
2480    consumeError(std::move(Err));
2481    return Failure;
2482  }
2483
2484  // Read all of the records in the options block.
2485  RecordData Record;
2486  ASTReadResult Result = Success;
2487  while (true) {
2488    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2489    if (!MaybeEntry) {
2490      // FIXME this drops errors on the floor.
2491      consumeError(MaybeEntry.takeError());
2492      return Failure;
2493    }
2494    llvm::BitstreamEntry Entry = MaybeEntry.get();
2495
2496    switch (Entry.Kind) {
2497    case llvm::BitstreamEntry::Error:
2498    case llvm::BitstreamEntry::SubBlock:
2499      return Failure;
2500
2501    case llvm::BitstreamEntry::EndBlock:
2502      return Result;
2503
2504    case llvm::BitstreamEntry::Record:
2505      // The interesting case.
2506      break;
2507    }
2508
2509    // Read and process a record.
2510    Record.clear();
2511    Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2512    if (!MaybeRecordType) {
2513      // FIXME this drops errors on the floor.
2514      consumeError(MaybeRecordType.takeError());
2515      return Failure;
2516    }
2517    switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2518    case LANGUAGE_OPTIONS: {
2519      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2520      if (ParseLanguageOptions(Record, Complain, Listener,
2521                               AllowCompatibleConfigurationMismatch))
2522        Result = ConfigurationMismatch;
2523      break;
2524    }
2525
2526    case TARGET_OPTIONS: {
2527      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2528      if (ParseTargetOptions(Record, Complain, Listener,
2529                             AllowCompatibleConfigurationMismatch))
2530        Result = ConfigurationMismatch;
2531      break;
2532    }
2533
2534    case FILE_SYSTEM_OPTIONS: {
2535      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2536      if (!AllowCompatibleConfigurationMismatch &&
2537          ParseFileSystemOptions(Record, Complain, Listener))
2538        Result = ConfigurationMismatch;
2539      break;
2540    }
2541
2542    case HEADER_SEARCH_OPTIONS: {
2543      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2544      if (!AllowCompatibleConfigurationMismatch &&
2545          ParseHeaderSearchOptions(Record, Complain, Listener))
2546        Result = ConfigurationMismatch;
2547      break;
2548    }
2549
2550    case PREPROCESSOR_OPTIONS:
2551      bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2552      if (!AllowCompatibleConfigurationMismatch &&
2553          ParsePreprocessorOptions(Record, Complain, Listener,
2554                                   SuggestedPredefines))
2555        Result = ConfigurationMismatch;
2556      break;
2557    }
2558  }
2559}
2560
2561ASTReader::ASTReadResult
2562ASTReader::ReadControlBlock(ModuleFile &F,
2563                            SmallVectorImpl<ImportedModule> &Loaded,
2564                            const ModuleFile *ImportedBy,
2565                            unsigned ClientLoadCapabilities) {
2566  BitstreamCursor &Stream = F.Stream;
2567
2568  if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2569    Error(std::move(Err));
2570    return Failure;
2571  }
2572
2573  // Lambda to read the unhashed control block the first time it's called.
2574  //
2575  // For PCM files, the unhashed control block cannot be read until after the
2576  // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2577  // need to look ahead before reading the IMPORTS record.  For consistency,
2578  // this block is always read somehow (see BitstreamEntry::EndBlock).
2579  bool HasReadUnhashedControlBlock = false;
2580  auto readUnhashedControlBlockOnce = [&]() {
2581    if (!HasReadUnhashedControlBlock) {
2582      HasReadUnhashedControlBlock = true;
2583      if (ASTReadResult Result =
2584              readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2585        return Result;
2586    }
2587    return Success;
2588  };
2589
2590  // Read all of the records and blocks in the control block.
2591  RecordData Record;
2592  unsigned NumInputs = 0;
2593  unsigned NumUserInputs = 0;
2594  StringRef BaseDirectoryAsWritten;
2595  while (true) {
2596    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2597    if (!MaybeEntry) {
2598      Error(MaybeEntry.takeError());
2599      return Failure;
2600    }
2601    llvm::BitstreamEntry Entry = MaybeEntry.get();
2602
2603    switch (Entry.Kind) {
2604    case llvm::BitstreamEntry::Error:
2605      Error("malformed block record in AST file");
2606      return Failure;
2607    case llvm::BitstreamEntry::EndBlock: {
2608      // Validate the module before returning.  This call catches an AST with
2609      // no module name and no imports.
2610      if (ASTReadResult Result = readUnhashedControlBlockOnce())
2611        return Result;
2612
2613      // Validate input files.
2614      const HeaderSearchOptions &HSOpts =
2615          PP.getHeaderSearchInfo().getHeaderSearchOpts();
2616
2617      // All user input files reside at the index range [0, NumUserInputs), and
2618      // system input files reside at [NumUserInputs, NumInputs). For explicitly
2619      // loaded module files, ignore missing inputs.
2620      if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2621          F.Kind != MK_PrebuiltModule) {
2622        bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2623
2624        // If we are reading a module, we will create a verification timestamp,
2625        // so we verify all input files.  Otherwise, verify only user input
2626        // files.
2627
2628        unsigned N = NumUserInputs;
2629        if (ValidateSystemInputs ||
2630            (HSOpts.ModulesValidateOncePerBuildSession &&
2631             F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2632             F.Kind == MK_ImplicitModule))
2633          N = NumInputs;
2634
2635        for (unsigned I = 0; I < N; ++I) {
2636          InputFile IF = getInputFile(F, I+1, Complain);
2637          if (!IF.getFile() || IF.isOutOfDate())
2638            return OutOfDate;
2639        }
2640      }
2641
2642      if (Listener)
2643        Listener->visitModuleFile(F.FileName, F.Kind);
2644
2645      if (Listener && Listener->needsInputFileVisitation()) {
2646        unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2647                                                                : NumUserInputs;
2648        for (unsigned I = 0; I < N; ++I) {
2649          bool IsSystem = I >= NumUserInputs;
2650          InputFileInfo FI = readInputFileInfo(F, I+1);
2651          Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2652                                   F.Kind == MK_ExplicitModule ||
2653                                   F.Kind == MK_PrebuiltModule);
2654        }
2655      }
2656
2657      return Success;
2658    }
2659
2660    case llvm::BitstreamEntry::SubBlock:
2661      switch (Entry.ID) {
2662      case INPUT_FILES_BLOCK_ID:
2663        F.InputFilesCursor = Stream;
2664        if (llvm::Error Err = Stream.SkipBlock()) {
2665          Error(std::move(Err));
2666          return Failure;
2667        }
2668        if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2669          Error("malformed block record in AST file");
2670          return Failure;
2671        }
2672        continue;
2673
2674      case OPTIONS_BLOCK_ID:
2675        // If we're reading the first module for this group, check its options
2676        // are compatible with ours. For modules it imports, no further checking
2677        // is required, because we checked them when we built it.
2678        if (Listener && !ImportedBy) {
2679          // Should we allow the configuration of the module file to differ from
2680          // the configuration of the current translation unit in a compatible
2681          // way?
2682          //
2683          // FIXME: Allow this for files explicitly specified with -include-pch.
2684          bool AllowCompatibleConfigurationMismatch =
2685              F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2686
2687          ASTReadResult Result =
2688              ReadOptionsBlock(Stream, ClientLoadCapabilities,
2689                               AllowCompatibleConfigurationMismatch, *Listener,
2690                               SuggestedPredefines);
2691          if (Result == Failure) {
2692            Error("malformed block record in AST file");
2693            return Result;
2694          }
2695
2696          if (DisableValidation ||
2697              (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2698            Result = Success;
2699
2700          // If we can't load the module, exit early since we likely
2701          // will rebuild the module anyway. The stream may be in the
2702          // middle of a block.
2703          if (Result != Success)
2704            return Result;
2705        } else if (llvm::Error Err = Stream.SkipBlock()) {
2706          Error(std::move(Err));
2707          return Failure;
2708        }
2709        continue;
2710
2711      default:
2712        if (llvm::Error Err = Stream.SkipBlock()) {
2713          Error(std::move(Err));
2714          return Failure;
2715        }
2716        continue;
2717      }
2718
2719    case llvm::BitstreamEntry::Record:
2720      // The interesting case.
2721      break;
2722    }
2723
2724    // Read and process a record.
2725    Record.clear();
2726    StringRef Blob;
2727    Expected<unsigned> MaybeRecordType =
2728        Stream.readRecord(Entry.ID, Record, &Blob);
2729    if (!MaybeRecordType) {
2730      Error(MaybeRecordType.takeError());
2731      return Failure;
2732    }
2733    switch ((ControlRecordTypes)MaybeRecordType.get()) {
2734    case METADATA: {
2735      if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2736        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2737          Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2738                                        : diag::err_pch_version_too_new);
2739        return VersionMismatch;
2740      }
2741
2742      bool hasErrors = Record[7];
2743      if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2744        Diag(diag::err_pch_with_compiler_errors);
2745        return HadErrors;
2746      }
2747      if (hasErrors) {
2748        Diags.ErrorOccurred = true;
2749        Diags.UncompilableErrorOccurred = true;
2750        Diags.UnrecoverableErrorOccurred = true;
2751      }
2752
2753      F.RelocatablePCH = Record[4];
2754      // Relative paths in a relocatable PCH are relative to our sysroot.
2755      if (F.RelocatablePCH)
2756        F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2757
2758      F.HasTimestamps = Record[5];
2759
2760      F.PCHHasObjectFile = Record[6];
2761
2762      const std::string &CurBranch = getClangFullRepositoryVersion();
2763      StringRef ASTBranch = Blob;
2764      if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2765        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2766          Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2767        return VersionMismatch;
2768      }
2769      break;
2770    }
2771
2772    case IMPORTS: {
2773      // Validate the AST before processing any imports (otherwise, untangling
2774      // them can be error-prone and expensive).  A module will have a name and
2775      // will already have been validated, but this catches the PCH case.
2776      if (ASTReadResult Result = readUnhashedControlBlockOnce())
2777        return Result;
2778
2779      // Load each of the imported PCH files.
2780      unsigned Idx = 0, N = Record.size();
2781      while (Idx < N) {
2782        // Read information about the AST file.
2783        ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2784        // The import location will be the local one for now; we will adjust
2785        // all import locations of module imports after the global source
2786        // location info are setup, in ReadAST.
2787        SourceLocation ImportLoc =
2788            ReadUntranslatedSourceLocation(Record[Idx++]);
2789        off_t StoredSize = (off_t)Record[Idx++];
2790        time_t StoredModTime = (time_t)Record[Idx++];
2791        auto FirstSignatureByte = Record.begin() + Idx;
2792        ASTFileSignature StoredSignature = ASTFileSignature::create(
2793            FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2794        Idx += ASTFileSignature::size;
2795
2796        std::string ImportedName = ReadString(Record, Idx);
2797        std::string ImportedFile;
2798
2799        // For prebuilt and explicit modules first consult the file map for
2800        // an override. Note that here we don't search prebuilt module
2801        // directories, only the explicit name to file mappings. Also, we will
2802        // still verify the size/signature making sure it is essentially the
2803        // same file but perhaps in a different location.
2804        if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2805          ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2806            ImportedName, /*FileMapOnly*/ true);
2807
2808        if (ImportedFile.empty())
2809          // Use BaseDirectoryAsWritten to ensure we use the same path in the
2810          // ModuleCache as when writing.
2811          ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2812        else
2813          SkipPath(Record, Idx);
2814
2815        // If our client can't cope with us being out of date, we can't cope with
2816        // our dependency being missing.
2817        unsigned Capabilities = ClientLoadCapabilities;
2818        if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2819          Capabilities &= ~ARR_Missing;
2820
2821        // Load the AST file.
2822        auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2823                                  Loaded, StoredSize, StoredModTime,
2824                                  StoredSignature, Capabilities);
2825
2826        // If we diagnosed a problem, produce a backtrace.
2827        if (isDiagnosedResult(Result, Capabilities))
2828          Diag(diag::note_module_file_imported_by)
2829              << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2830
2831        switch (Result) {
2832        case Failure: return Failure;
2833          // If we have to ignore the dependency, we'll have to ignore this too.
2834        case Missing:
2835        case OutOfDate: return OutOfDate;
2836        case VersionMismatch: return VersionMismatch;
2837        case ConfigurationMismatch: return ConfigurationMismatch;
2838        case HadErrors: return HadErrors;
2839        case Success: break;
2840        }
2841      }
2842      break;
2843    }
2844
2845    case ORIGINAL_FILE:
2846      F.OriginalSourceFileID = FileID::get(Record[0]);
2847      F.ActualOriginalSourceFileName = std::string(Blob);
2848      F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2849      ResolveImportedPath(F, F.OriginalSourceFileName);
2850      break;
2851
2852    case ORIGINAL_FILE_ID:
2853      F.OriginalSourceFileID = FileID::get(Record[0]);
2854      break;
2855
2856    case ORIGINAL_PCH_DIR:
2857      F.OriginalDir = std::string(Blob);
2858      break;
2859
2860    case MODULE_NAME:
2861      F.ModuleName = std::string(Blob);
2862      Diag(diag::remark_module_import)
2863          << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2864          << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2865      if (Listener)
2866        Listener->ReadModuleName(F.ModuleName);
2867
2868      // Validate the AST as soon as we have a name so we can exit early on
2869      // failure.
2870      if (ASTReadResult Result = readUnhashedControlBlockOnce())
2871        return Result;
2872
2873      break;
2874
2875    case MODULE_DIRECTORY: {
2876      // Save the BaseDirectory as written in the PCM for computing the module
2877      // filename for the ModuleCache.
2878      BaseDirectoryAsWritten = Blob;
2879      assert(!F.ModuleName.empty() &&
2880             "MODULE_DIRECTORY found before MODULE_NAME");
2881      // If we've already loaded a module map file covering this module, we may
2882      // have a better path for it (relative to the current build).
2883      Module *M = PP.getHeaderSearchInfo().lookupModule(
2884          F.ModuleName, /*AllowSearch*/ true,
2885          /*AllowExtraModuleMapSearch*/ true);
2886      if (M && M->Directory) {
2887        // If we're implicitly loading a module, the base directory can't
2888        // change between the build and use.
2889        // Don't emit module relocation error if we have -fno-validate-pch
2890        if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2891            F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2892          auto BuildDir = PP.getFileManager().getDirectory(Blob);
2893          if (!BuildDir || *BuildDir != M->Directory) {
2894            if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2895              Diag(diag::err_imported_module_relocated)
2896                  << F.ModuleName << Blob << M->Directory->getName();
2897            return OutOfDate;
2898          }
2899        }
2900        F.BaseDirectory = std::string(M->Directory->getName());
2901      } else {
2902        F.BaseDirectory = std::string(Blob);
2903      }
2904      break;
2905    }
2906
2907    case MODULE_MAP_FILE:
2908      if (ASTReadResult Result =
2909              ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2910        return Result;
2911      break;
2912
2913    case INPUT_FILE_OFFSETS:
2914      NumInputs = Record[0];
2915      NumUserInputs = Record[1];
2916      F.InputFileOffsets =
2917          (const llvm::support::unaligned_uint64_t *)Blob.data();
2918      F.InputFilesLoaded.resize(NumInputs);
2919      F.NumUserInputFiles = NumUserInputs;
2920      break;
2921    }
2922  }
2923}
2924
2925ASTReader::ASTReadResult
2926ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2927  BitstreamCursor &Stream = F.Stream;
2928
2929  if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2930    Error(std::move(Err));
2931    return Failure;
2932  }
2933  F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2934
2935  // Read all of the records and blocks for the AST file.
2936  RecordData Record;
2937  while (true) {
2938    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2939    if (!MaybeEntry) {
2940      Error(MaybeEntry.takeError());
2941      return Failure;
2942    }
2943    llvm::BitstreamEntry Entry = MaybeEntry.get();
2944
2945    switch (Entry.Kind) {
2946    case llvm::BitstreamEntry::Error:
2947      Error("error at end of module block in AST file");
2948      return Failure;
2949    case llvm::BitstreamEntry::EndBlock:
2950      // Outside of C++, we do not store a lookup map for the translation unit.
2951      // Instead, mark it as needing a lookup map to be built if this module
2952      // contains any declarations lexically within it (which it always does!).
2953      // This usually has no cost, since we very rarely need the lookup map for
2954      // the translation unit outside C++.
2955      if (ASTContext *Ctx = ContextObj) {
2956        DeclContext *DC = Ctx->getTranslationUnitDecl();
2957        if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2958          DC->setMustBuildLookupTable();
2959      }
2960
2961      return Success;
2962    case llvm::BitstreamEntry::SubBlock:
2963      switch (Entry.ID) {
2964      case DECLTYPES_BLOCK_ID:
2965        // We lazily load the decls block, but we want to set up the
2966        // DeclsCursor cursor to point into it.  Clone our current bitcode
2967        // cursor to it, enter the block and read the abbrevs in that block.
2968        // With the main cursor, we just skip over it.
2969        F.DeclsCursor = Stream;
2970        if (llvm::Error Err = Stream.SkipBlock()) {
2971          Error(std::move(Err));
2972          return Failure;
2973        }
2974        if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2975                             &F.DeclsBlockStartOffset)) {
2976          Error("malformed block record in AST file");
2977          return Failure;
2978        }
2979        break;
2980
2981      case PREPROCESSOR_BLOCK_ID:
2982        F.MacroCursor = Stream;
2983        if (!PP.getExternalSource())
2984          PP.setExternalSource(this);
2985
2986        if (llvm::Error Err = Stream.SkipBlock()) {
2987          Error(std::move(Err));
2988          return Failure;
2989        }
2990        if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2991          Error("malformed block record in AST file");
2992          return Failure;
2993        }
2994        F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
2995        break;
2996
2997      case PREPROCESSOR_DETAIL_BLOCK_ID:
2998        F.PreprocessorDetailCursor = Stream;
2999
3000        if (llvm::Error Err = Stream.SkipBlock()) {
3001          Error(std::move(Err));
3002          return Failure;
3003        }
3004        if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3005                             PREPROCESSOR_DETAIL_BLOCK_ID)) {
3006          Error("malformed preprocessor detail record in AST file");
3007          return Failure;
3008        }
3009        F.PreprocessorDetailStartOffset
3010        = F.PreprocessorDetailCursor.GetCurrentBitNo();
3011
3012        if (!PP.getPreprocessingRecord())
3013          PP.createPreprocessingRecord();
3014        if (!PP.getPreprocessingRecord()->getExternalSource())
3015          PP.getPreprocessingRecord()->SetExternalSource(*this);
3016        break;
3017
3018      case SOURCE_MANAGER_BLOCK_ID:
3019        if (ReadSourceManagerBlock(F))
3020          return Failure;
3021        break;
3022
3023      case SUBMODULE_BLOCK_ID:
3024        if (ASTReadResult Result =
3025                ReadSubmoduleBlock(F, ClientLoadCapabilities))
3026          return Result;
3027        break;
3028
3029      case COMMENTS_BLOCK_ID: {
3030        BitstreamCursor C = Stream;
3031
3032        if (llvm::Error Err = Stream.SkipBlock()) {
3033          Error(std::move(Err));
3034          return Failure;
3035        }
3036        if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3037          Error("malformed comments block in AST file");
3038          return Failure;
3039        }
3040        CommentsCursors.push_back(std::make_pair(C, &F));
3041        break;
3042      }
3043
3044      default:
3045        if (llvm::Error Err = Stream.SkipBlock()) {
3046          Error(std::move(Err));
3047          return Failure;
3048        }
3049        break;
3050      }
3051      continue;
3052
3053    case llvm::BitstreamEntry::Record:
3054      // The interesting case.
3055      break;
3056    }
3057
3058    // Read and process a record.
3059    Record.clear();
3060    StringRef Blob;
3061    Expected<unsigned> MaybeRecordType =
3062        Stream.readRecord(Entry.ID, Record, &Blob);
3063    if (!MaybeRecordType) {
3064      Error(MaybeRecordType.takeError());
3065      return Failure;
3066    }
3067    ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3068
3069    // If we're not loading an AST context, we don't care about most records.
3070    if (!ContextObj) {
3071      switch (RecordType) {
3072      case IDENTIFIER_TABLE:
3073      case IDENTIFIER_OFFSET:
3074      case INTERESTING_IDENTIFIERS:
3075      case STATISTICS:
3076      case PP_CONDITIONAL_STACK:
3077      case PP_COUNTER_VALUE:
3078      case SOURCE_LOCATION_OFFSETS:
3079      case MODULE_OFFSET_MAP:
3080      case SOURCE_MANAGER_LINE_TABLE:
3081      case SOURCE_LOCATION_PRELOADS:
3082      case PPD_ENTITIES_OFFSETS:
3083      case HEADER_SEARCH_TABLE:
3084      case IMPORTED_MODULES:
3085      case MACRO_OFFSET:
3086        break;
3087      default:
3088        continue;
3089      }
3090    }
3091
3092    switch (RecordType) {
3093    default:  // Default behavior: ignore.
3094      break;
3095
3096    case TYPE_OFFSET: {
3097      if (F.LocalNumTypes != 0) {
3098        Error("duplicate TYPE_OFFSET record in AST file");
3099        return Failure;
3100      }
3101      F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3102      F.LocalNumTypes = Record[0];
3103      unsigned LocalBaseTypeIndex = Record[1];
3104      F.BaseTypeIndex = getTotalNumTypes();
3105
3106      if (F.LocalNumTypes > 0) {
3107        // Introduce the global -> local mapping for types within this module.
3108        GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3109
3110        // Introduce the local -> global mapping for types within this module.
3111        F.TypeRemap.insertOrReplace(
3112          std::make_pair(LocalBaseTypeIndex,
3113                         F.BaseTypeIndex - LocalBaseTypeIndex));
3114
3115        TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3116      }
3117      break;
3118    }
3119
3120    case DECL_OFFSET: {
3121      if (F.LocalNumDecls != 0) {
3122        Error("duplicate DECL_OFFSET record in AST file");
3123        return Failure;
3124      }
3125      F.DeclOffsets = (const DeclOffset *)Blob.data();
3126      F.LocalNumDecls = Record[0];
3127      unsigned LocalBaseDeclID = Record[1];
3128      F.BaseDeclID = getTotalNumDecls();
3129
3130      if (F.LocalNumDecls > 0) {
3131        // Introduce the global -> local mapping for declarations within this
3132        // module.
3133        GlobalDeclMap.insert(
3134          std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3135
3136        // Introduce the local -> global mapping for declarations within this
3137        // module.
3138        F.DeclRemap.insertOrReplace(
3139          std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3140
3141        // Introduce the global -> local mapping for declarations within this
3142        // module.
3143        F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3144
3145        DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3146      }
3147      break;
3148    }
3149
3150    case TU_UPDATE_LEXICAL: {
3151      DeclContext *TU = ContextObj->getTranslationUnitDecl();
3152      LexicalContents Contents(
3153          reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3154              Blob.data()),
3155          static_cast<unsigned int>(Blob.size() / 4));
3156      TULexicalDecls.push_back(std::make_pair(&F, Contents));
3157      TU->setHasExternalLexicalStorage(true);
3158      break;
3159    }
3160
3161    case UPDATE_VISIBLE: {
3162      unsigned Idx = 0;
3163      serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3164      auto *Data = (const unsigned char*)Blob.data();
3165      PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3166      // If we've already loaded the decl, perform the updates when we finish
3167      // loading this block.
3168      if (Decl *D = GetExistingDecl(ID))
3169        PendingUpdateRecords.push_back(
3170            PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3171      break;
3172    }
3173
3174    case IDENTIFIER_TABLE:
3175      F.IdentifierTableData = Blob.data();
3176      if (Record[0]) {
3177        F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3178            (const unsigned char *)F.IdentifierTableData + Record[0],
3179            (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3180            (const unsigned char *)F.IdentifierTableData,
3181            ASTIdentifierLookupTrait(*this, F));
3182
3183        PP.getIdentifierTable().setExternalIdentifierLookup(this);
3184      }
3185      break;
3186
3187    case IDENTIFIER_OFFSET: {
3188      if (F.LocalNumIdentifiers != 0) {
3189        Error("duplicate IDENTIFIER_OFFSET record in AST file");
3190        return Failure;
3191      }
3192      F.IdentifierOffsets = (const uint32_t *)Blob.data();
3193      F.LocalNumIdentifiers = Record[0];
3194      unsigned LocalBaseIdentifierID = Record[1];
3195      F.BaseIdentifierID = getTotalNumIdentifiers();
3196
3197      if (F.LocalNumIdentifiers > 0) {
3198        // Introduce the global -> local mapping for identifiers within this
3199        // module.
3200        GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3201                                                  &F));
3202
3203        // Introduce the local -> global mapping for identifiers within this
3204        // module.
3205        F.IdentifierRemap.insertOrReplace(
3206          std::make_pair(LocalBaseIdentifierID,
3207                         F.BaseIdentifierID - LocalBaseIdentifierID));
3208
3209        IdentifiersLoaded.resize(IdentifiersLoaded.size()
3210                                 + F.LocalNumIdentifiers);
3211      }
3212      break;
3213    }
3214
3215    case INTERESTING_IDENTIFIERS:
3216      F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3217      break;
3218
3219    case EAGERLY_DESERIALIZED_DECLS:
3220      // FIXME: Skip reading this record if our ASTConsumer doesn't care
3221      // about "interesting" decls (for instance, if we're building a module).
3222      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3223        EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3224      break;
3225
3226    case MODULAR_CODEGEN_DECLS:
3227      // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3228      // them (ie: if we're not codegenerating this module).
3229      if (F.Kind == MK_MainFile ||
3230          getContext().getLangOpts().BuildingPCHWithObjectFile)
3231        for (unsigned I = 0, N = Record.size(); I != N; ++I)
3232          EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3233      break;
3234
3235    case SPECIAL_TYPES:
3236      if (SpecialTypes.empty()) {
3237        for (unsigned I = 0, N = Record.size(); I != N; ++I)
3238          SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3239        break;
3240      }
3241
3242      if (SpecialTypes.size() != Record.size()) {
3243        Error("invalid special-types record");
3244        return Failure;
3245      }
3246
3247      for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3248        serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3249        if (!SpecialTypes[I])
3250          SpecialTypes[I] = ID;
3251        // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3252        // merge step?
3253      }
3254      break;
3255
3256    case STATISTICS:
3257      TotalNumStatements += Record[0];
3258      TotalNumMacros += Record[1];
3259      TotalLexicalDeclContexts += Record[2];
3260      TotalVisibleDeclContexts += Record[3];
3261      break;
3262
3263    case UNUSED_FILESCOPED_DECLS:
3264      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3265        UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3266      break;
3267
3268    case DELEGATING_CTORS:
3269      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3270        DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3271      break;
3272
3273    case WEAK_UNDECLARED_IDENTIFIERS:
3274      if (Record.size() % 4 != 0) {
3275        Error("invalid weak identifiers record");
3276        return Failure;
3277      }
3278
3279      // FIXME: Ignore weak undeclared identifiers from non-original PCH
3280      // files. This isn't the way to do it :)
3281      WeakUndeclaredIdentifiers.clear();
3282
3283      // Translate the weak, undeclared identifiers into global IDs.
3284      for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3285        WeakUndeclaredIdentifiers.push_back(
3286          getGlobalIdentifierID(F, Record[I++]));
3287        WeakUndeclaredIdentifiers.push_back(
3288          getGlobalIdentifierID(F, Record[I++]));
3289        WeakUndeclaredIdentifiers.push_back(
3290          ReadSourceLocation(F, Record, I).getRawEncoding());
3291        WeakUndeclaredIdentifiers.push_back(Record[I++]);
3292      }
3293      break;
3294
3295    case SELECTOR_OFFSETS: {
3296      F.SelectorOffsets = (const uint32_t *)Blob.data();
3297      F.LocalNumSelectors = Record[0];
3298      unsigned LocalBaseSelectorID = Record[1];
3299      F.BaseSelectorID = getTotalNumSelectors();
3300
3301      if (F.LocalNumSelectors > 0) {
3302        // Introduce the global -> local mapping for selectors within this
3303        // module.
3304        GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3305
3306        // Introduce the local -> global mapping for selectors within this
3307        // module.
3308        F.SelectorRemap.insertOrReplace(
3309          std::make_pair(LocalBaseSelectorID,
3310                         F.BaseSelectorID - LocalBaseSelectorID));
3311
3312        SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3313      }
3314      break;
3315    }
3316
3317    case METHOD_POOL:
3318      F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3319      if (Record[0])
3320        F.SelectorLookupTable
3321          = ASTSelectorLookupTable::Create(
3322                        F.SelectorLookupTableData + Record[0],
3323                        F.SelectorLookupTableData,
3324                        ASTSelectorLookupTrait(*this, F));
3325      TotalNumMethodPoolEntries += Record[1];
3326      break;
3327
3328    case REFERENCED_SELECTOR_POOL:
3329      if (!Record.empty()) {
3330        for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3331          ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3332                                                                Record[Idx++]));
3333          ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3334                                              getRawEncoding());
3335        }
3336      }
3337      break;
3338
3339    case PP_CONDITIONAL_STACK:
3340      if (!Record.empty()) {
3341        unsigned Idx = 0, End = Record.size() - 1;
3342        bool ReachedEOFWhileSkipping = Record[Idx++];
3343        llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3344        if (ReachedEOFWhileSkipping) {
3345          SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3346          SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3347          bool FoundNonSkipPortion = Record[Idx++];
3348          bool FoundElse = Record[Idx++];
3349          SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3350          SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3351                           FoundElse, ElseLoc);
3352        }
3353        SmallVector<PPConditionalInfo, 4> ConditionalStack;
3354        while (Idx < End) {
3355          auto Loc = ReadSourceLocation(F, Record, Idx);
3356          bool WasSkipping = Record[Idx++];
3357          bool FoundNonSkip = Record[Idx++];
3358          bool FoundElse = Record[Idx++];
3359          ConditionalStack.push_back(
3360              {Loc, WasSkipping, FoundNonSkip, FoundElse});
3361        }
3362        PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3363      }
3364      break;
3365
3366    case PP_COUNTER_VALUE:
3367      if (!Record.empty() && Listener)
3368        Listener->ReadCounter(F, Record[0]);
3369      break;
3370
3371    case FILE_SORTED_DECLS:
3372      F.FileSortedDecls = (const DeclID *)Blob.data();
3373      F.NumFileSortedDecls = Record[0];
3374      break;
3375
3376    case SOURCE_LOCATION_OFFSETS: {
3377      F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3378      F.LocalNumSLocEntries = Record[0];
3379      unsigned SLocSpaceSize = Record[1];
3380      F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3381      std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3382          SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3383                                              SLocSpaceSize);
3384      if (!F.SLocEntryBaseID) {
3385        Error("ran out of source locations");
3386        break;
3387      }
3388      // Make our entry in the range map. BaseID is negative and growing, so
3389      // we invert it. Because we invert it, though, we need the other end of
3390      // the range.
3391      unsigned RangeStart =
3392          unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3393      GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3394      F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3395
3396      // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3397      assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3398      GlobalSLocOffsetMap.insert(
3399          std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3400                           - SLocSpaceSize,&F));
3401
3402      // Initialize the remapping table.
3403      // Invalid stays invalid.
3404      F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3405      // This module. Base was 2 when being compiled.
3406      F.SLocRemap.insertOrReplace(std::make_pair(2U,
3407                                  static_cast<int>(F.SLocEntryBaseOffset - 2)));
3408
3409      TotalNumSLocEntries += F.LocalNumSLocEntries;
3410      break;
3411    }
3412
3413    case MODULE_OFFSET_MAP:
3414      F.ModuleOffsetMap = Blob;
3415      break;
3416
3417    case SOURCE_MANAGER_LINE_TABLE:
3418      if (ParseLineTable(F, Record)) {
3419        Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3420        return Failure;
3421      }
3422      break;
3423
3424    case SOURCE_LOCATION_PRELOADS: {
3425      // Need to transform from the local view (1-based IDs) to the global view,
3426      // which is based off F.SLocEntryBaseID.
3427      if (!F.PreloadSLocEntries.empty()) {
3428        Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3429        return Failure;
3430      }
3431
3432      F.PreloadSLocEntries.swap(Record);
3433      break;
3434    }
3435
3436    case EXT_VECTOR_DECLS:
3437      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3438        ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3439      break;
3440
3441    case VTABLE_USES:
3442      if (Record.size() % 3 != 0) {
3443        Error("Invalid VTABLE_USES record");
3444        return Failure;
3445      }
3446
3447      // Later tables overwrite earlier ones.
3448      // FIXME: Modules will have some trouble with this. This is clearly not
3449      // the right way to do this.
3450      VTableUses.clear();
3451
3452      for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3453        VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3454        VTableUses.push_back(
3455          ReadSourceLocation(F, Record, Idx).getRawEncoding());
3456        VTableUses.push_back(Record[Idx++]);
3457      }
3458      break;
3459
3460    case PENDING_IMPLICIT_INSTANTIATIONS:
3461      if (PendingInstantiations.size() % 2 != 0) {
3462        Error("Invalid existing PendingInstantiations");
3463        return Failure;
3464      }
3465
3466      if (Record.size() % 2 != 0) {
3467        Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3468        return Failure;
3469      }
3470
3471      for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3472        PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3473        PendingInstantiations.push_back(
3474          ReadSourceLocation(F, Record, I).getRawEncoding());
3475      }
3476      break;
3477
3478    case SEMA_DECL_REFS:
3479      if (Record.size() != 3) {
3480        Error("Invalid SEMA_DECL_REFS block");
3481        return Failure;
3482      }
3483      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3484        SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3485      break;
3486
3487    case PPD_ENTITIES_OFFSETS: {
3488      F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3489      assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3490      F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3491
3492      unsigned LocalBasePreprocessedEntityID = Record[0];
3493
3494      unsigned StartingID;
3495      if (!PP.getPreprocessingRecord())
3496        PP.createPreprocessingRecord();
3497      if (!PP.getPreprocessingRecord()->getExternalSource())
3498        PP.getPreprocessingRecord()->SetExternalSource(*this);
3499      StartingID
3500        = PP.getPreprocessingRecord()
3501            ->allocateLoadedEntities(F.NumPreprocessedEntities);
3502      F.BasePreprocessedEntityID = StartingID;
3503
3504      if (F.NumPreprocessedEntities > 0) {
3505        // Introduce the global -> local mapping for preprocessed entities in
3506        // this module.
3507        GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3508
3509        // Introduce the local -> global mapping for preprocessed entities in
3510        // this module.
3511        F.PreprocessedEntityRemap.insertOrReplace(
3512          std::make_pair(LocalBasePreprocessedEntityID,
3513            F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3514      }
3515
3516      break;
3517    }
3518
3519    case PPD_SKIPPED_RANGES: {
3520      F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3521      assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3522      F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3523
3524      if (!PP.getPreprocessingRecord())
3525        PP.createPreprocessingRecord();
3526      if (!PP.getPreprocessingRecord()->getExternalSource())
3527        PP.getPreprocessingRecord()->SetExternalSource(*this);
3528      F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3529          ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3530
3531      if (F.NumPreprocessedSkippedRanges > 0)
3532        GlobalSkippedRangeMap.insert(
3533            std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3534      break;
3535    }
3536
3537    case DECL_UPDATE_OFFSETS:
3538      if (Record.size() % 2 != 0) {
3539        Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3540        return Failure;
3541      }
3542      for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3543        GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3544        DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3545
3546        // If we've already loaded the decl, perform the updates when we finish
3547        // loading this block.
3548        if (Decl *D = GetExistingDecl(ID))
3549          PendingUpdateRecords.push_back(
3550              PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3551      }
3552      break;
3553
3554    case OBJC_CATEGORIES_MAP:
3555      if (F.LocalNumObjCCategoriesInMap != 0) {
3556        Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3557        return Failure;
3558      }
3559
3560      F.LocalNumObjCCategoriesInMap = Record[0];
3561      F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3562      break;
3563
3564    case OBJC_CATEGORIES:
3565      F.ObjCCategories.swap(Record);
3566      break;
3567
3568    case CUDA_SPECIAL_DECL_REFS:
3569      // Later tables overwrite earlier ones.
3570      // FIXME: Modules will have trouble with this.
3571      CUDASpecialDeclRefs.clear();
3572      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3573        CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3574      break;
3575
3576    case HEADER_SEARCH_TABLE:
3577      F.HeaderFileInfoTableData = Blob.data();
3578      F.LocalNumHeaderFileInfos = Record[1];
3579      if (Record[0]) {
3580        F.HeaderFileInfoTable
3581          = HeaderFileInfoLookupTable::Create(
3582                   (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3583                   (const unsigned char *)F.HeaderFileInfoTableData,
3584                   HeaderFileInfoTrait(*this, F,
3585                                       &PP.getHeaderSearchInfo(),
3586                                       Blob.data() + Record[2]));
3587
3588        PP.getHeaderSearchInfo().SetExternalSource(this);
3589        if (!PP.getHeaderSearchInfo().getExternalLookup())
3590          PP.getHeaderSearchInfo().SetExternalLookup(this);
3591      }
3592      break;
3593
3594    case FP_PRAGMA_OPTIONS:
3595      // Later tables overwrite earlier ones.
3596      FPPragmaOptions.swap(Record);
3597      break;
3598
3599    case OPENCL_EXTENSIONS:
3600      for (unsigned I = 0, E = Record.size(); I != E; ) {
3601        auto Name = ReadString(Record, I);
3602        auto &Opt = OpenCLExtensions.OptMap[Name];
3603        Opt.Supported = Record[I++] != 0;
3604        Opt.Enabled = Record[I++] != 0;
3605        Opt.Avail = Record[I++];
3606        Opt.Core = Record[I++];
3607      }
3608      break;
3609
3610    case OPENCL_EXTENSION_TYPES:
3611      for (unsigned I = 0, E = Record.size(); I != E;) {
3612        auto TypeID = static_cast<::TypeID>(Record[I++]);
3613        auto *Type = GetType(TypeID).getTypePtr();
3614        auto NumExt = static_cast<unsigned>(Record[I++]);
3615        for (unsigned II = 0; II != NumExt; ++II) {
3616          auto Ext = ReadString(Record, I);
3617          OpenCLTypeExtMap[Type].insert(Ext);
3618        }
3619      }
3620      break;
3621
3622    case OPENCL_EXTENSION_DECLS:
3623      for (unsigned I = 0, E = Record.size(); I != E;) {
3624        auto DeclID = static_cast<::DeclID>(Record[I++]);
3625        auto *Decl = GetDecl(DeclID);
3626        auto NumExt = static_cast<unsigned>(Record[I++]);
3627        for (unsigned II = 0; II != NumExt; ++II) {
3628          auto Ext = ReadString(Record, I);
3629          OpenCLDeclExtMap[Decl].insert(Ext);
3630        }
3631      }
3632      break;
3633
3634    case TENTATIVE_DEFINITIONS:
3635      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3636        TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3637      break;
3638
3639    case KNOWN_NAMESPACES:
3640      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3641        KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3642      break;
3643
3644    case UNDEFINED_BUT_USED:
3645      if (UndefinedButUsed.size() % 2 != 0) {
3646        Error("Invalid existing UndefinedButUsed");
3647        return Failure;
3648      }
3649
3650      if (Record.size() % 2 != 0) {
3651        Error("invalid undefined-but-used record");
3652        return Failure;
3653      }
3654      for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3655        UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3656        UndefinedButUsed.push_back(
3657            ReadSourceLocation(F, Record, I).getRawEncoding());
3658      }
3659      break;
3660
3661    case DELETE_EXPRS_TO_ANALYZE:
3662      for (unsigned I = 0, N = Record.size(); I != N;) {
3663        DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3664        const uint64_t Count = Record[I++];
3665        DelayedDeleteExprs.push_back(Count);
3666        for (uint64_t C = 0; C < Count; ++C) {
3667          DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3668          bool IsArrayForm = Record[I++] == 1;
3669          DelayedDeleteExprs.push_back(IsArrayForm);
3670        }
3671      }
3672      break;
3673
3674    case IMPORTED_MODULES:
3675      if (!F.isModule()) {
3676        // If we aren't loading a module (which has its own exports), make
3677        // all of the imported modules visible.
3678        // FIXME: Deal with macros-only imports.
3679        for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3680          unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3681          SourceLocation Loc = ReadSourceLocation(F, Record, I);
3682          if (GlobalID) {
3683            ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3684            if (DeserializationListener)
3685              DeserializationListener->ModuleImportRead(GlobalID, Loc);
3686          }
3687        }
3688      }
3689      break;
3690
3691    case MACRO_OFFSET: {
3692      if (F.LocalNumMacros != 0) {
3693        Error("duplicate MACRO_OFFSET record in AST file");
3694        return Failure;
3695      }
3696      F.MacroOffsets = (const uint32_t *)Blob.data();
3697      F.LocalNumMacros = Record[0];
3698      unsigned LocalBaseMacroID = Record[1];
3699      F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3700      F.BaseMacroID = getTotalNumMacros();
3701
3702      if (F.LocalNumMacros > 0) {
3703        // Introduce the global -> local mapping for macros within this module.
3704        GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3705
3706        // Introduce the local -> global mapping for macros within this module.
3707        F.MacroRemap.insertOrReplace(
3708          std::make_pair(LocalBaseMacroID,
3709                         F.BaseMacroID - LocalBaseMacroID));
3710
3711        MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3712      }
3713      break;
3714    }
3715
3716    case LATE_PARSED_TEMPLATE:
3717      LateParsedTemplates.append(Record.begin(), Record.end());
3718      break;
3719
3720    case OPTIMIZE_PRAGMA_OPTIONS:
3721      if (Record.size() != 1) {
3722        Error("invalid pragma optimize record");
3723        return Failure;
3724      }
3725      OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3726      break;
3727
3728    case MSSTRUCT_PRAGMA_OPTIONS:
3729      if (Record.size() != 1) {
3730        Error("invalid pragma ms_struct record");
3731        return Failure;
3732      }
3733      PragmaMSStructState = Record[0];
3734      break;
3735
3736    case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3737      if (Record.size() != 2) {
3738        Error("invalid pragma ms_struct record");
3739        return Failure;
3740      }
3741      PragmaMSPointersToMembersState = Record[0];
3742      PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3743      break;
3744
3745    case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3746      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3747        UnusedLocalTypedefNameCandidates.push_back(
3748            getGlobalDeclID(F, Record[I]));
3749      break;
3750
3751    case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3752      if (Record.size() != 1) {
3753        Error("invalid cuda pragma options record");
3754        return Failure;
3755      }
3756      ForceCUDAHostDeviceDepth = Record[0];
3757      break;
3758
3759    case PACK_PRAGMA_OPTIONS: {
3760      if (Record.size() < 3) {
3761        Error("invalid pragma pack record");
3762        return Failure;
3763      }
3764      PragmaPackCurrentValue = Record[0];
3765      PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3766      unsigned NumStackEntries = Record[2];
3767      unsigned Idx = 3;
3768      // Reset the stack when importing a new module.
3769      PragmaPackStack.clear();
3770      for (unsigned I = 0; I < NumStackEntries; ++I) {
3771        PragmaPackStackEntry Entry;
3772        Entry.Value = Record[Idx++];
3773        Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3774        Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3775        PragmaPackStrings.push_back(ReadString(Record, Idx));
3776        Entry.SlotLabel = PragmaPackStrings.back();
3777        PragmaPackStack.push_back(Entry);
3778      }
3779      break;
3780    }
3781
3782    case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3783      if (Record.size() < 3) {
3784        Error("invalid pragma pack record");
3785        return Failure;
3786      }
3787      FpPragmaCurrentValue = Record[0];
3788      FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3789      unsigned NumStackEntries = Record[2];
3790      unsigned Idx = 3;
3791      // Reset the stack when importing a new module.
3792      FpPragmaStack.clear();
3793      for (unsigned I = 0; I < NumStackEntries; ++I) {
3794        FpPragmaStackEntry Entry;
3795        Entry.Value = Record[Idx++];
3796        Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3797        Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3798        FpPragmaStrings.push_back(ReadString(Record, Idx));
3799        Entry.SlotLabel = FpPragmaStrings.back();
3800        FpPragmaStack.push_back(Entry);
3801      }
3802      break;
3803    }
3804
3805    case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3806      for (unsigned I = 0, N = Record.size(); I != N; ++I)
3807        DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3808      break;
3809    }
3810  }
3811}
3812
3813void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3814  assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3815
3816  // Additional remapping information.
3817  const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3818  const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3819  F.ModuleOffsetMap = StringRef();
3820
3821  // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3822  if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3823    F.SLocRemap.insert(std::make_pair(0U, 0));
3824    F.SLocRemap.insert(std::make_pair(2U, 1));
3825  }
3826
3827  // Continuous range maps we may be updating in our module.
3828  using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3829  RemapBuilder SLocRemap(F.SLocRemap);
3830  RemapBuilder IdentifierRemap(F.IdentifierRemap);
3831  RemapBuilder MacroRemap(F.MacroRemap);
3832  RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3833  RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3834  RemapBuilder SelectorRemap(F.SelectorRemap);
3835  RemapBuilder DeclRemap(F.DeclRemap);
3836  RemapBuilder TypeRemap(F.TypeRemap);
3837
3838  while (Data < DataEnd) {
3839    // FIXME: Looking up dependency modules by filename is horrible. Let's
3840    // start fixing this with prebuilt, explicit and implicit modules and see
3841    // how it goes...
3842    using namespace llvm::support;
3843    ModuleKind Kind = static_cast<ModuleKind>(
3844      endian::readNext<uint8_t, little, unaligned>(Data));
3845    uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3846    StringRef Name = StringRef((const char*)Data, Len);
3847    Data += Len;
3848    ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3849                              Kind == MK_ImplicitModule
3850                          ? ModuleMgr.lookupByModuleName(Name)
3851                          : ModuleMgr.lookupByFileName(Name));
3852    if (!OM) {
3853      std::string Msg =
3854          "SourceLocation remap refers to unknown module, cannot find ";
3855      Msg.append(std::string(Name));
3856      Error(Msg);
3857      return;
3858    }
3859
3860    uint32_t SLocOffset =
3861        endian::readNext<uint32_t, little, unaligned>(Data);
3862    uint32_t IdentifierIDOffset =
3863        endian::readNext<uint32_t, little, unaligned>(Data);
3864    uint32_t MacroIDOffset =
3865        endian::readNext<uint32_t, little, unaligned>(Data);
3866    uint32_t PreprocessedEntityIDOffset =
3867        endian::readNext<uint32_t, little, unaligned>(Data);
3868    uint32_t SubmoduleIDOffset =
3869        endian::readNext<uint32_t, little, unaligned>(Data);
3870    uint32_t SelectorIDOffset =
3871        endian::readNext<uint32_t, little, unaligned>(Data);
3872    uint32_t DeclIDOffset =
3873        endian::readNext<uint32_t, little, unaligned>(Data);
3874    uint32_t TypeIndexOffset =
3875        endian::readNext<uint32_t, little, unaligned>(Data);
3876
3877    uint32_t None = std::numeric_limits<uint32_t>::max();
3878
3879    auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3880                         RemapBuilder &Remap) {
3881      if (Offset != None)
3882        Remap.insert(std::make_pair(Offset,
3883                                    static_cast<int>(BaseOffset - Offset)));
3884    };
3885    mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3886    mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3887    mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3888    mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3889              PreprocessedEntityRemap);
3890    mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3891    mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3892    mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3893    mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3894
3895    // Global -> local mappings.
3896    F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3897  }
3898}
3899
3900ASTReader::ASTReadResult
3901ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3902                                  const ModuleFile *ImportedBy,
3903                                  unsigned ClientLoadCapabilities) {
3904  unsigned Idx = 0;
3905  F.ModuleMapPath = ReadPath(F, Record, Idx);
3906
3907  // Try to resolve ModuleName in the current header search context and
3908  // verify that it is found in the same module map file as we saved. If the
3909  // top-level AST file is a main file, skip this check because there is no
3910  // usable header search context.
3911  assert(!F.ModuleName.empty() &&
3912         "MODULE_NAME should come before MODULE_MAP_FILE");
3913  if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3914    // An implicitly-loaded module file should have its module listed in some
3915    // module map file that we've already loaded.
3916    Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3917    auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3918    const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3919    // Don't emit module relocation error if we have -fno-validate-pch
3920    if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3921      if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3922        if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3923          // This module was defined by an imported (explicit) module.
3924          Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3925                                               << ASTFE->getName();
3926        } else {
3927          // This module was built with a different module map.
3928          Diag(diag::err_imported_module_not_found)
3929              << F.ModuleName << F.FileName
3930              << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3931              << !ImportedBy;
3932          // In case it was imported by a PCH, there's a chance the user is
3933          // just missing to include the search path to the directory containing
3934          // the modulemap.
3935          if (ImportedBy && ImportedBy->Kind == MK_PCH)
3936            Diag(diag::note_imported_by_pch_module_not_found)
3937                << llvm::sys::path::parent_path(F.ModuleMapPath);
3938        }
3939      }
3940      return OutOfDate;
3941    }
3942
3943    assert(M->Name == F.ModuleName && "found module with different name");
3944
3945    // Check the primary module map file.
3946    auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3947    if (!StoredModMap || *StoredModMap != ModMap) {
3948      assert(ModMap && "found module is missing module map file");
3949      assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3950             "top-level import should be verified");
3951      bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3952      if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3953        Diag(diag::err_imported_module_modmap_changed)
3954            << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3955            << ModMap->getName() << F.ModuleMapPath << NotImported;
3956      return OutOfDate;
3957    }
3958
3959    llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3960    for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3961      // FIXME: we should use input files rather than storing names.
3962      std::string Filename = ReadPath(F, Record, Idx);
3963      auto F = FileMgr.getFile(Filename, false, false);
3964      if (!F) {
3965        if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3966          Error("could not find file '" + Filename +"' referenced by AST file");
3967        return OutOfDate;
3968      }
3969      AdditionalStoredMaps.insert(*F);
3970    }
3971
3972    // Check any additional module map files (e.g. module.private.modulemap)
3973    // that are not in the pcm.
3974    if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3975      for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3976        // Remove files that match
3977        // Note: SmallPtrSet::erase is really remove
3978        if (!AdditionalStoredMaps.erase(ModMap)) {
3979          if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3980            Diag(diag::err_module_different_modmap)
3981              << F.ModuleName << /*new*/0 << ModMap->getName();
3982          return OutOfDate;
3983        }
3984      }
3985    }
3986
3987    // Check any additional module map files that are in the pcm, but not
3988    // found in header search. Cases that match are already removed.
3989    for (const FileEntry *ModMap : AdditionalStoredMaps) {
3990      if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3991        Diag(diag::err_module_different_modmap)
3992          << F.ModuleName << /*not new*/1 << ModMap->getName();
3993      return OutOfDate;
3994    }
3995  }
3996
3997  if (Listener)
3998    Listener->ReadModuleMapFile(F.ModuleMapPath);
3999  return Success;
4000}
4001
4002/// Move the given method to the back of the global list of methods.
4003static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4004  // Find the entry for this selector in the method pool.
4005  Sema::GlobalMethodPool::iterator Known
4006    = S.MethodPool.find(Method->getSelector());
4007  if (Known == S.MethodPool.end())
4008    return;
4009
4010  // Retrieve the appropriate method list.
4011  ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4012                                                    : Known->second.second;
4013  bool Found = false;
4014  for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4015    if (!Found) {
4016      if (List->getMethod() == Method) {
4017        Found = true;
4018      } else {
4019        // Keep searching.
4020        continue;
4021      }
4022    }
4023
4024    if (List->getNext())
4025      List->setMethod(List->getNext()->getMethod());
4026    else
4027      List->setMethod(Method);
4028  }
4029}
4030
4031void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4032  assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4033  for (Decl *D : Names) {
4034    bool wasHidden = !D->isUnconditionallyVisible();
4035    D->setVisibleDespiteOwningModule();
4036
4037    if (wasHidden && SemaObj) {
4038      if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4039        moveMethodToBackOfGlobalList(*SemaObj, Method);
4040      }
4041    }
4042  }
4043}
4044
4045void ASTReader::makeModuleVisible(Module *Mod,
4046                                  Module::NameVisibilityKind NameVisibility,
4047                                  SourceLocation ImportLoc) {
4048  llvm::SmallPtrSet<Module *, 4> Visited;
4049  SmallVector<Module *, 4> Stack;
4050  Stack.push_back(Mod);
4051  while (!Stack.empty()) {
4052    Mod = Stack.pop_back_val();
4053
4054    if (NameVisibility <= Mod->NameVisibility) {
4055      // This module already has this level of visibility (or greater), so
4056      // there is nothing more to do.
4057      continue;
4058    }
4059
4060    if (Mod->isUnimportable()) {
4061      // Modules that aren't importable cannot be made visible.
4062      continue;
4063    }
4064
4065    // Update the module's name visibility.
4066    Mod->NameVisibility = NameVisibility;
4067
4068    // If we've already deserialized any names from this module,
4069    // mark them as visible.
4070    HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4071    if (Hidden != HiddenNamesMap.end()) {
4072      auto HiddenNames = std::move(*Hidden);
4073      HiddenNamesMap.erase(Hidden);
4074      makeNamesVisible(HiddenNames.second, HiddenNames.first);
4075      assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4076             "making names visible added hidden names");
4077    }
4078
4079    // Push any exported modules onto the stack to be marked as visible.
4080    SmallVector<Module *, 16> Exports;
4081    Mod->getExportedModules(Exports);
4082    for (SmallVectorImpl<Module *>::iterator
4083           I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4084      Module *Exported = *I;
4085      if (Visited.insert(Exported).second)
4086        Stack.push_back(Exported);
4087    }
4088  }
4089}
4090
4091/// We've merged the definition \p MergedDef into the existing definition
4092/// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4093/// visible.
4094void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4095                                          NamedDecl *MergedDef) {
4096  if (!Def->isUnconditionallyVisible()) {
4097    // If MergedDef is visible or becomes visible, make the definition visible.
4098    if (MergedDef->isUnconditionallyVisible())
4099      Def->setVisibleDespiteOwningModule();
4100    else {
4101      getContext().mergeDefinitionIntoModule(
4102          Def, MergedDef->getImportedOwningModule(),
4103          /*NotifyListeners*/ false);
4104      PendingMergedDefinitionsToDeduplicate.insert(Def);
4105    }
4106  }
4107}
4108
4109bool ASTReader::loadGlobalIndex() {
4110  if (GlobalIndex)
4111    return false;
4112
4113  if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4114      !PP.getLangOpts().Modules)
4115    return true;
4116
4117  // Try to load the global index.
4118  TriedLoadingGlobalIndex = true;
4119  StringRef ModuleCachePath
4120    = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4121  std::pair<GlobalModuleIndex *, llvm::Error> Result =
4122      GlobalModuleIndex::readIndex(ModuleCachePath);
4123  if (llvm::Error Err = std::move(Result.second)) {
4124    assert(!Result.first);
4125    consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4126    return true;
4127  }
4128
4129  GlobalIndex.reset(Result.first);
4130  ModuleMgr.setGlobalIndex(GlobalIndex.get());
4131  return false;
4132}
4133
4134bool ASTReader::isGlobalIndexUnavailable() const {
4135  return PP.getLangOpts().Modules && UseGlobalIndex &&
4136         !hasGlobalIndex() && TriedLoadingGlobalIndex;
4137}
4138
4139static void updateModuleTimestamp(ModuleFile &MF) {
4140  // Overwrite the timestamp file contents so that file's mtime changes.
4141  std::string TimestampFilename = MF.getTimestampFilename();
4142  std::error_code EC;
4143  llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4144  if (EC)
4145    return;
4146  OS << "Timestamp file\n";
4147  OS.close();
4148  OS.clear_error(); // Avoid triggering a fatal error.
4149}
4150
4151/// Given a cursor at the start of an AST file, scan ahead and drop the
4152/// cursor into the start of the given block ID, returning false on success and
4153/// true on failure.
4154static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4155  while (true) {
4156    Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4157    if (!MaybeEntry) {
4158      // FIXME this drops errors on the floor.
4159      consumeError(MaybeEntry.takeError());
4160      return true;
4161    }
4162    llvm::BitstreamEntry Entry = MaybeEntry.get();
4163
4164    switch (Entry.Kind) {
4165    case llvm::BitstreamEntry::Error:
4166    case llvm::BitstreamEntry::EndBlock:
4167      return true;
4168
4169    case llvm::BitstreamEntry::Record:
4170      // Ignore top-level records.
4171      if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4172        break;
4173      else {
4174        // FIXME this drops errors on the floor.
4175        consumeError(Skipped.takeError());
4176        return true;
4177      }
4178
4179    case llvm::BitstreamEntry::SubBlock:
4180      if (Entry.ID == BlockID) {
4181        if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4182          // FIXME this drops the error on the floor.
4183          consumeError(std::move(Err));
4184          return true;
4185        }
4186        // Found it!
4187        return false;
4188      }
4189
4190      if (llvm::Error Err = Cursor.SkipBlock()) {
4191        // FIXME this drops the error on the floor.
4192        consumeError(std::move(Err));
4193        return true;
4194      }
4195    }
4196  }
4197}
4198
4199ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4200                                            ModuleKind Type,
4201                                            SourceLocation ImportLoc,
4202                                            unsigned ClientLoadCapabilities,
4203                                            SmallVectorImpl<ImportedSubmodule> *Imported) {
4204  llvm::SaveAndRestore<SourceLocation>
4205    SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4206
4207  // Defer any pending actions until we get to the end of reading the AST file.
4208  Deserializing AnASTFile(this);
4209
4210  // Bump the generation number.
4211  unsigned PreviousGeneration = 0;
4212  if (ContextObj)
4213    PreviousGeneration = incrementGeneration(*ContextObj);
4214
4215  unsigned NumModules = ModuleMgr.size();
4216  auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4217    assert(ReadResult && "expected to return error");
4218    ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4219                            PP.getLangOpts().Modules
4220                                ? &PP.getHeaderSearchInfo().getModuleMap()
4221                                : nullptr);
4222
4223    // If we find that any modules are unusable, the global index is going
4224    // to be out-of-date. Just remove it.
4225    GlobalIndex.reset();
4226    ModuleMgr.setGlobalIndex(nullptr);
4227    return ReadResult;
4228  };
4229
4230  SmallVector<ImportedModule, 4> Loaded;
4231  switch (ASTReadResult ReadResult =
4232              ReadASTCore(FileName, Type, ImportLoc,
4233                          /*ImportedBy=*/nullptr, Loaded, 0, 0,
4234                          ASTFileSignature(), ClientLoadCapabilities)) {
4235  case Failure:
4236  case Missing:
4237  case OutOfDate:
4238  case VersionMismatch:
4239  case ConfigurationMismatch:
4240  case HadErrors:
4241    return removeModulesAndReturn(ReadResult);
4242  case Success:
4243    break;
4244  }
4245
4246  // Here comes stuff that we only do once the entire chain is loaded.
4247
4248  // Load the AST blocks of all of the modules that we loaded.  We can still
4249  // hit errors parsing the ASTs at this point.
4250  for (ImportedModule &M : Loaded) {
4251    ModuleFile &F = *M.Mod;
4252
4253    // Read the AST block.
4254    if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4255      return removeModulesAndReturn(Result);
4256
4257    // The AST block should always have a definition for the main module.
4258    if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4259      Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4260      return removeModulesAndReturn(Failure);
4261    }
4262
4263    // Read the extension blocks.
4264    while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4265      if (ASTReadResult Result = ReadExtensionBlock(F))
4266        return removeModulesAndReturn(Result);
4267    }
4268
4269    // Once read, set the ModuleFile bit base offset and update the size in
4270    // bits of all files we've seen.
4271    F.GlobalBitOffset = TotalModulesSizeInBits;
4272    TotalModulesSizeInBits += F.SizeInBits;
4273    GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4274  }
4275
4276  // Preload source locations and interesting indentifiers.
4277  for (ImportedModule &M : Loaded) {
4278    ModuleFile &F = *M.Mod;
4279
4280    // Preload SLocEntries.
4281    for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4282      int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4283      // Load it through the SourceManager and don't call ReadSLocEntry()
4284      // directly because the entry may have already been loaded in which case
4285      // calling ReadSLocEntry() directly would trigger an assertion in
4286      // SourceManager.
4287      SourceMgr.getLoadedSLocEntryByID(Index);
4288    }
4289
4290    // Map the original source file ID into the ID space of the current
4291    // compilation.
4292    if (F.OriginalSourceFileID.isValid()) {
4293      F.OriginalSourceFileID = FileID::get(
4294          F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4295    }
4296
4297    // Preload all the pending interesting identifiers by marking them out of
4298    // date.
4299    for (auto Offset : F.PreloadIdentifierOffsets) {
4300      const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4301          F.IdentifierTableData + Offset);
4302
4303      ASTIdentifierLookupTrait Trait(*this, F);
4304      auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4305      auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4306      auto &II = PP.getIdentifierTable().getOwn(Key);
4307      II.setOutOfDate(true);
4308
4309      // Mark this identifier as being from an AST file so that we can track
4310      // whether we need to serialize it.
4311      markIdentifierFromAST(*this, II);
4312
4313      // Associate the ID with the identifier so that the writer can reuse it.
4314      auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4315      SetIdentifierInfo(ID, &II);
4316    }
4317  }
4318
4319  // Setup the import locations and notify the module manager that we've
4320  // committed to these module files.
4321  for (ImportedModule &M : Loaded) {
4322    ModuleFile &F = *M.Mod;
4323
4324    ModuleMgr.moduleFileAccepted(&F);
4325
4326    // Set the import location.
4327    F.DirectImportLoc = ImportLoc;
4328    // FIXME: We assume that locations from PCH / preamble do not need
4329    // any translation.
4330    if (!M.ImportedBy)
4331      F.ImportLoc = M.ImportLoc;
4332    else
4333      F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4334  }
4335
4336  if (!PP.getLangOpts().CPlusPlus ||
4337      (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4338       Type != MK_PrebuiltModule)) {
4339    // Mark all of the identifiers in the identifier table as being out of date,
4340    // so that various accessors know to check the loaded modules when the
4341    // identifier is used.
4342    //
4343    // For C++ modules, we don't need information on many identifiers (just
4344    // those that provide macros or are poisoned), so we mark all of
4345    // the interesting ones via PreloadIdentifierOffsets.
4346    for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4347                                IdEnd = PP.getIdentifierTable().end();
4348         Id != IdEnd; ++Id)
4349      Id->second->setOutOfDate(true);
4350  }
4351  // Mark selectors as out of date.
4352  for (auto Sel : SelectorGeneration)
4353    SelectorOutOfDate[Sel.first] = true;
4354
4355  // Resolve any unresolved module exports.
4356  for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4357    UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4358    SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4359    Module *ResolvedMod = getSubmodule(GlobalID);
4360
4361    switch (Unresolved.Kind) {
4362    case UnresolvedModuleRef::Conflict:
4363      if (ResolvedMod) {
4364        Module::Conflict Conflict;
4365        Conflict.Other = ResolvedMod;
4366        Conflict.Message = Unresolved.String.str();
4367        Unresolved.Mod->Conflicts.push_back(Conflict);
4368      }
4369      continue;
4370
4371    case UnresolvedModuleRef::Import:
4372      if (ResolvedMod)
4373        Unresolved.Mod->Imports.insert(ResolvedMod);
4374      continue;
4375
4376    case UnresolvedModuleRef::Export:
4377      if (ResolvedMod || Unresolved.IsWildcard)
4378        Unresolved.Mod->Exports.push_back(
4379          Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4380      continue;
4381    }
4382  }
4383  UnresolvedModuleRefs.clear();
4384
4385  if (Imported)
4386    Imported->append(ImportedModules.begin(),
4387                     ImportedModules.end());
4388
4389  // FIXME: How do we load the 'use'd modules? They may not be submodules.
4390  // Might be unnecessary as use declarations are only used to build the
4391  // module itself.
4392
4393  if (ContextObj)
4394    InitializeContext();
4395
4396  if (SemaObj)
4397    UpdateSema();
4398
4399  if (DeserializationListener)
4400    DeserializationListener->ReaderInitialized(this);
4401
4402  ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4403  if (PrimaryModule.OriginalSourceFileID.isValid()) {
4404    // If this AST file is a precompiled preamble, then set the
4405    // preamble file ID of the source manager to the file source file
4406    // from which the preamble was built.
4407    if (Type == MK_Preamble) {
4408      SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4409    } else if (Type == MK_MainFile) {
4410      SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4411    }
4412  }
4413
4414  // For any Objective-C class definitions we have already loaded, make sure
4415  // that we load any additional categories.
4416  if (ContextObj) {
4417    for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4418      loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4419                         ObjCClassesLoaded[I],
4420                         PreviousGeneration);
4421    }
4422  }
4423
4424  if (PP.getHeaderSearchInfo()
4425          .getHeaderSearchOpts()
4426          .ModulesValidateOncePerBuildSession) {
4427    // Now we are certain that the module and all modules it depends on are
4428    // up to date.  Create or update timestamp files for modules that are
4429    // located in the module cache (not for PCH files that could be anywhere
4430    // in the filesystem).
4431    for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4432      ImportedModule &M = Loaded[I];
4433      if (M.Mod->Kind == MK_ImplicitModule) {
4434        updateModuleTimestamp(*M.Mod);
4435      }
4436    }
4437  }
4438
4439  return Success;
4440}
4441
4442static ASTFileSignature readASTFileSignature(StringRef PCH);
4443
4444/// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4445static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4446  // FIXME checking magic headers is done in other places such as
4447  // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4448  // always done the same. Unify it all with a helper.
4449  if (!Stream.canSkipToPos(4))
4450    return llvm::createStringError(std::errc::illegal_byte_sequence,
4451                                   "file too small to contain AST file magic");
4452  for (unsigned C : {'C', 'P', 'C', 'H'})
4453    if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4454      if (Res.get() != C)
4455        return llvm::createStringError(
4456            std::errc::illegal_byte_sequence,
4457            "file doesn't start with AST file magic");
4458    } else
4459      return Res.takeError();
4460  return llvm::Error::success();
4461}
4462
4463static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4464  switch (Kind) {
4465  case MK_PCH:
4466    return 0; // PCH
4467  case MK_ImplicitModule:
4468  case MK_ExplicitModule:
4469  case MK_PrebuiltModule:
4470    return 1; // module
4471  case MK_MainFile:
4472  case MK_Preamble:
4473    return 2; // main source file
4474  }
4475  llvm_unreachable("unknown module kind");
4476}
4477
4478ASTReader::ASTReadResult
4479ASTReader::ReadASTCore(StringRef FileName,
4480                       ModuleKind Type,
4481                       SourceLocation ImportLoc,
4482                       ModuleFile *ImportedBy,
4483                       SmallVectorImpl<ImportedModule> &Loaded,
4484                       off_t ExpectedSize, time_t ExpectedModTime,
4485                       ASTFileSignature ExpectedSignature,
4486                       unsigned ClientLoadCapabilities) {
4487  ModuleFile *M;
4488  std::string ErrorStr;
4489  ModuleManager::AddModuleResult AddResult
4490    = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4491                          getGeneration(), ExpectedSize, ExpectedModTime,
4492                          ExpectedSignature, readASTFileSignature,
4493                          M, ErrorStr);
4494
4495  switch (AddResult) {
4496  case ModuleManager::AlreadyLoaded:
4497    Diag(diag::remark_module_import)
4498        << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4499        << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4500    return Success;
4501
4502  case ModuleManager::NewlyLoaded:
4503    // Load module file below.
4504    break;
4505
4506  case ModuleManager::Missing:
4507    // The module file was missing; if the client can handle that, return
4508    // it.
4509    if (ClientLoadCapabilities & ARR_Missing)
4510      return Missing;
4511
4512    // Otherwise, return an error.
4513    Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4514                                          << FileName << !ErrorStr.empty()
4515                                          << ErrorStr;
4516    return Failure;
4517
4518  case ModuleManager::OutOfDate:
4519    // We couldn't load the module file because it is out-of-date. If the
4520    // client can handle out-of-date, return it.
4521    if (ClientLoadCapabilities & ARR_OutOfDate)
4522      return OutOfDate;
4523
4524    // Otherwise, return an error.
4525    Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4526                                            << FileName << !ErrorStr.empty()
4527                                            << ErrorStr;
4528    return Failure;
4529  }
4530
4531  assert(M && "Missing module file");
4532
4533  bool ShouldFinalizePCM = false;
4534  auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4535    auto &MC = getModuleManager().getModuleCache();
4536    if (ShouldFinalizePCM)
4537      MC.finalizePCM(FileName);
4538    else
4539      MC.tryToDropPCM(FileName);
4540  });
4541  ModuleFile &F = *M;
4542  BitstreamCursor &Stream = F.Stream;
4543  Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4544  F.SizeInBits = F.Buffer->getBufferSize() * 8;
4545
4546  // Sniff for the signature.
4547  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4548    Diag(diag::err_module_file_invalid)
4549        << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4550    return Failure;
4551  }
4552
4553  // This is used for compatibility with older PCH formats.
4554  bool HaveReadControlBlock = false;
4555  while (true) {
4556    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4557    if (!MaybeEntry) {
4558      Error(MaybeEntry.takeError());
4559      return Failure;
4560    }
4561    llvm::BitstreamEntry Entry = MaybeEntry.get();
4562
4563    switch (Entry.Kind) {
4564    case llvm::BitstreamEntry::Error:
4565    case llvm::BitstreamEntry::Record:
4566    case llvm::BitstreamEntry::EndBlock:
4567      Error("invalid record at top-level of AST file");
4568      return Failure;
4569
4570    case llvm::BitstreamEntry::SubBlock:
4571      break;
4572    }
4573
4574    switch (Entry.ID) {
4575    case CONTROL_BLOCK_ID:
4576      HaveReadControlBlock = true;
4577      switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4578      case Success:
4579        // Check that we didn't try to load a non-module AST file as a module.
4580        //
4581        // FIXME: Should we also perform the converse check? Loading a module as
4582        // a PCH file sort of works, but it's a bit wonky.
4583        if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4584             Type == MK_PrebuiltModule) &&
4585            F.ModuleName.empty()) {
4586          auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4587          if (Result != OutOfDate ||
4588              (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4589            Diag(diag::err_module_file_not_module) << FileName;
4590          return Result;
4591        }
4592        break;
4593
4594      case Failure: return Failure;
4595      case Missing: return Missing;
4596      case OutOfDate: return OutOfDate;
4597      case VersionMismatch: return VersionMismatch;
4598      case ConfigurationMismatch: return ConfigurationMismatch;
4599      case HadErrors: return HadErrors;
4600      }
4601      break;
4602
4603    case AST_BLOCK_ID:
4604      if (!HaveReadControlBlock) {
4605        if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4606          Diag(diag::err_pch_version_too_old);
4607        return VersionMismatch;
4608      }
4609
4610      // Record that we've loaded this module.
4611      Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4612      ShouldFinalizePCM = true;
4613      return Success;
4614
4615    case UNHASHED_CONTROL_BLOCK_ID:
4616      // This block is handled using look-ahead during ReadControlBlock.  We
4617      // shouldn't get here!
4618      Error("malformed block record in AST file");
4619      return Failure;
4620
4621    default:
4622      if (llvm::Error Err = Stream.SkipBlock()) {
4623        Error(std::move(Err));
4624        return Failure;
4625      }
4626      break;
4627    }
4628  }
4629
4630  llvm_unreachable("unexpected break; expected return");
4631}
4632
4633ASTReader::ASTReadResult
4634ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4635                                    unsigned ClientLoadCapabilities) {
4636  const HeaderSearchOptions &HSOpts =
4637      PP.getHeaderSearchInfo().getHeaderSearchOpts();
4638  bool AllowCompatibleConfigurationMismatch =
4639      F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4640
4641  ASTReadResult Result = readUnhashedControlBlockImpl(
4642      &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4643      Listener.get(),
4644      WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4645
4646  // If F was directly imported by another module, it's implicitly validated by
4647  // the importing module.
4648  if (DisableValidation || WasImportedBy ||
4649      (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4650    return Success;
4651
4652  if (Result == Failure) {
4653    Error("malformed block record in AST file");
4654    return Failure;
4655  }
4656
4657  if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4658    // If this module has already been finalized in the ModuleCache, we're stuck
4659    // with it; we can only load a single version of each module.
4660    //
4661    // This can happen when a module is imported in two contexts: in one, as a
4662    // user module; in another, as a system module (due to an import from
4663    // another module marked with the [system] flag).  It usually indicates a
4664    // bug in the module map: this module should also be marked with [system].
4665    //
4666    // If -Wno-system-headers (the default), and the first import is as a
4667    // system module, then validation will fail during the as-user import,
4668    // since -Werror flags won't have been validated.  However, it's reasonable
4669    // to treat this consistently as a system module.
4670    //
4671    // If -Wsystem-headers, the PCM on disk was built with
4672    // -Wno-system-headers, and the first import is as a user module, then
4673    // validation will fail during the as-system import since the PCM on disk
4674    // doesn't guarantee that -Werror was respected.  However, the -Werror
4675    // flags were checked during the initial as-user import.
4676    if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4677      Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4678      return Success;
4679    }
4680  }
4681
4682  return Result;
4683}
4684
4685ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4686    ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4687    bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4688    bool ValidateDiagnosticOptions) {
4689  // Initialize a stream.
4690  BitstreamCursor Stream(StreamData);
4691
4692  // Sniff for the signature.
4693  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4694    // FIXME this drops the error on the floor.
4695    consumeError(std::move(Err));
4696    return Failure;
4697  }
4698
4699  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4700  if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4701    return Failure;
4702
4703  // Read all of the records in the options block.
4704  RecordData Record;
4705  ASTReadResult Result = Success;
4706  while (true) {
4707    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4708    if (!MaybeEntry) {
4709      // FIXME this drops the error on the floor.
4710      consumeError(MaybeEntry.takeError());
4711      return Failure;
4712    }
4713    llvm::BitstreamEntry Entry = MaybeEntry.get();
4714
4715    switch (Entry.Kind) {
4716    case llvm::BitstreamEntry::Error:
4717    case llvm::BitstreamEntry::SubBlock:
4718      return Failure;
4719
4720    case llvm::BitstreamEntry::EndBlock:
4721      return Result;
4722
4723    case llvm::BitstreamEntry::Record:
4724      // The interesting case.
4725      break;
4726    }
4727
4728    // Read and process a record.
4729    Record.clear();
4730    Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4731    if (!MaybeRecordType) {
4732      // FIXME this drops the error.
4733      return Failure;
4734    }
4735    switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4736    case SIGNATURE:
4737      if (F)
4738        F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4739      break;
4740    case AST_BLOCK_HASH:
4741      if (F)
4742        F->ASTBlockHash =
4743            ASTFileSignature::create(Record.begin(), Record.end());
4744      break;
4745    case DIAGNOSTIC_OPTIONS: {
4746      bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4747      if (Listener && ValidateDiagnosticOptions &&
4748          !AllowCompatibleConfigurationMismatch &&
4749          ParseDiagnosticOptions(Record, Complain, *Listener))
4750        Result = OutOfDate; // Don't return early.  Read the signature.
4751      break;
4752    }
4753    case DIAG_PRAGMA_MAPPINGS:
4754      if (!F)
4755        break;
4756      if (F->PragmaDiagMappings.empty())
4757        F->PragmaDiagMappings.swap(Record);
4758      else
4759        F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4760                                     Record.begin(), Record.end());
4761      break;
4762    }
4763  }
4764}
4765
4766/// Parse a record and blob containing module file extension metadata.
4767static bool parseModuleFileExtensionMetadata(
4768              const SmallVectorImpl<uint64_t> &Record,
4769              StringRef Blob,
4770              ModuleFileExtensionMetadata &Metadata) {
4771  if (Record.size() < 4) return true;
4772
4773  Metadata.MajorVersion = Record[0];
4774  Metadata.MinorVersion = Record[1];
4775
4776  unsigned BlockNameLen = Record[2];
4777  unsigned UserInfoLen = Record[3];
4778
4779  if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4780
4781  Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4782  Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4783                                  Blob.data() + BlockNameLen + UserInfoLen);
4784  return false;
4785}
4786
4787ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4788  BitstreamCursor &Stream = F.Stream;
4789
4790  RecordData Record;
4791  while (true) {
4792    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4793    if (!MaybeEntry) {
4794      Error(MaybeEntry.takeError());
4795      return Failure;
4796    }
4797    llvm::BitstreamEntry Entry = MaybeEntry.get();
4798
4799    switch (Entry.Kind) {
4800    case llvm::BitstreamEntry::SubBlock:
4801      if (llvm::Error Err = Stream.SkipBlock()) {
4802        Error(std::move(Err));
4803        return Failure;
4804      }
4805      continue;
4806
4807    case llvm::BitstreamEntry::EndBlock:
4808      return Success;
4809
4810    case llvm::BitstreamEntry::Error:
4811      return HadErrors;
4812
4813    case llvm::BitstreamEntry::Record:
4814      break;
4815    }
4816
4817    Record.clear();
4818    StringRef Blob;
4819    Expected<unsigned> MaybeRecCode =
4820        Stream.readRecord(Entry.ID, Record, &Blob);
4821    if (!MaybeRecCode) {
4822      Error(MaybeRecCode.takeError());
4823      return Failure;
4824    }
4825    switch (MaybeRecCode.get()) {
4826    case EXTENSION_METADATA: {
4827      ModuleFileExtensionMetadata Metadata;
4828      if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4829        Error("malformed EXTENSION_METADATA in AST file");
4830        return Failure;
4831      }
4832
4833      // Find a module file extension with this block name.
4834      auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4835      if (Known == ModuleFileExtensions.end()) break;
4836
4837      // Form a reader.
4838      if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4839                                                             F, Stream)) {
4840        F.ExtensionReaders.push_back(std::move(Reader));
4841      }
4842
4843      break;
4844    }
4845    }
4846  }
4847
4848  return Success;
4849}
4850
4851void ASTReader::InitializeContext() {
4852  assert(ContextObj && "no context to initialize");
4853  ASTContext &Context = *ContextObj;
4854
4855  // If there's a listener, notify them that we "read" the translation unit.
4856  if (DeserializationListener)
4857    DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4858                                      Context.getTranslationUnitDecl());
4859
4860  // FIXME: Find a better way to deal with collisions between these
4861  // built-in types. Right now, we just ignore the problem.
4862
4863  // Load the special types.
4864  if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4865    if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4866      if (!Context.CFConstantStringTypeDecl)
4867        Context.setCFConstantStringType(GetType(String));
4868    }
4869
4870    if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4871      QualType FileType = GetType(File);
4872      if (FileType.isNull()) {
4873        Error("FILE type is NULL");
4874        return;
4875      }
4876
4877      if (!Context.FILEDecl) {
4878        if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4879          Context.setFILEDecl(Typedef->getDecl());
4880        else {
4881          const TagType *Tag = FileType->getAs<TagType>();
4882          if (!Tag) {
4883            Error("Invalid FILE type in AST file");
4884            return;
4885          }
4886          Context.setFILEDecl(Tag->getDecl());
4887        }
4888      }
4889    }
4890
4891    if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4892      QualType Jmp_bufType = GetType(Jmp_buf);
4893      if (Jmp_bufType.isNull()) {
4894        Error("jmp_buf type is NULL");
4895        return;
4896      }
4897
4898      if (!Context.jmp_bufDecl) {
4899        if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4900          Context.setjmp_bufDecl(Typedef->getDecl());
4901        else {
4902          const TagType *Tag = Jmp_bufType->getAs<TagType>();
4903          if (!Tag) {
4904            Error("Invalid jmp_buf type in AST file");
4905            return;
4906          }
4907          Context.setjmp_bufDecl(Tag->getDecl());
4908        }
4909      }
4910    }
4911
4912    if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4913      QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4914      if (Sigjmp_bufType.isNull()) {
4915        Error("sigjmp_buf type is NULL");
4916        return;
4917      }
4918
4919      if (!Context.sigjmp_bufDecl) {
4920        if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4921          Context.setsigjmp_bufDecl(Typedef->getDecl());
4922        else {
4923          const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4924          assert(Tag && "Invalid sigjmp_buf type in AST file");
4925          Context.setsigjmp_bufDecl(Tag->getDecl());
4926        }
4927      }
4928    }
4929
4930    if (unsigned ObjCIdRedef
4931          = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4932      if (Context.ObjCIdRedefinitionType.isNull())
4933        Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4934    }
4935
4936    if (unsigned ObjCClassRedef
4937          = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4938      if (Context.ObjCClassRedefinitionType.isNull())
4939        Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4940    }
4941
4942    if (unsigned ObjCSelRedef
4943          = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4944      if (Context.ObjCSelRedefinitionType.isNull())
4945        Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4946    }
4947
4948    if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4949      QualType Ucontext_tType = GetType(Ucontext_t);
4950      if (Ucontext_tType.isNull()) {
4951        Error("ucontext_t type is NULL");
4952        return;
4953      }
4954
4955      if (!Context.ucontext_tDecl) {
4956        if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4957          Context.setucontext_tDecl(Typedef->getDecl());
4958        else {
4959          const TagType *Tag = Ucontext_tType->getAs<TagType>();
4960          assert(Tag && "Invalid ucontext_t type in AST file");
4961          Context.setucontext_tDecl(Tag->getDecl());
4962        }
4963      }
4964    }
4965  }
4966
4967  ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4968
4969  // If there were any CUDA special declarations, deserialize them.
4970  if (!CUDASpecialDeclRefs.empty()) {
4971    assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4972    Context.setcudaConfigureCallDecl(
4973                           cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4974  }
4975
4976  // Re-export any modules that were imported by a non-module AST file.
4977  // FIXME: This does not make macro-only imports visible again.
4978  for (auto &Import : ImportedModules) {
4979    if (Module *Imported = getSubmodule(Import.ID)) {
4980      makeModuleVisible(Imported, Module::AllVisible,
4981                        /*ImportLoc=*/Import.ImportLoc);
4982      if (Import.ImportLoc.isValid())
4983        PP.makeModuleVisible(Imported, Import.ImportLoc);
4984      // FIXME: should we tell Sema to make the module visible too?
4985    }
4986  }
4987  ImportedModules.clear();
4988}
4989
4990void ASTReader::finalizeForWriting() {
4991  // Nothing to do for now.
4992}
4993
4994/// Reads and return the signature record from \p PCH's control block, or
4995/// else returns 0.
4996static ASTFileSignature readASTFileSignature(StringRef PCH) {
4997  BitstreamCursor Stream(PCH);
4998  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4999    // FIXME this drops the error on the floor.
5000    consumeError(std::move(Err));
5001    return ASTFileSignature();
5002  }
5003
5004  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5005  if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5006    return ASTFileSignature();
5007
5008  // Scan for SIGNATURE inside the diagnostic options block.
5009  ASTReader::RecordData Record;
5010  while (true) {
5011    Expected<llvm::BitstreamEntry> MaybeEntry =
5012        Stream.advanceSkippingSubblocks();
5013    if (!MaybeEntry) {
5014      // FIXME this drops the error on the floor.
5015      consumeError(MaybeEntry.takeError());
5016      return ASTFileSignature();
5017    }
5018    llvm::BitstreamEntry Entry = MaybeEntry.get();
5019
5020    if (Entry.Kind != llvm::BitstreamEntry::Record)
5021      return ASTFileSignature();
5022
5023    Record.clear();
5024    StringRef Blob;
5025    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5026    if (!MaybeRecord) {
5027      // FIXME this drops the error on the floor.
5028      consumeError(MaybeRecord.takeError());
5029      return ASTFileSignature();
5030    }
5031    if (SIGNATURE == MaybeRecord.get())
5032      return ASTFileSignature::create(Record.begin(),
5033                                      Record.begin() + ASTFileSignature::size);
5034  }
5035}
5036
5037/// Retrieve the name of the original source file name
5038/// directly from the AST file, without actually loading the AST
5039/// file.
5040std::string ASTReader::getOriginalSourceFile(
5041    const std::string &ASTFileName, FileManager &FileMgr,
5042    const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5043  // Open the AST file.
5044  auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5045  if (!Buffer) {
5046    Diags.Report(diag::err_fe_unable_to_read_pch_file)
5047        << ASTFileName << Buffer.getError().message();
5048    return std::string();
5049  }
5050
5051  // Initialize the stream
5052  BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5053
5054  // Sniff for the signature.
5055  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5056    Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5057    return std::string();
5058  }
5059
5060  // Scan for the CONTROL_BLOCK_ID block.
5061  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5062    Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5063    return std::string();
5064  }
5065
5066  // Scan for ORIGINAL_FILE inside the control block.
5067  RecordData Record;
5068  while (true) {
5069    Expected<llvm::BitstreamEntry> MaybeEntry =
5070        Stream.advanceSkippingSubblocks();
5071    if (!MaybeEntry) {
5072      // FIXME this drops errors on the floor.
5073      consumeError(MaybeEntry.takeError());
5074      return std::string();
5075    }
5076    llvm::BitstreamEntry Entry = MaybeEntry.get();
5077
5078    if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5079      return std::string();
5080
5081    if (Entry.Kind != llvm::BitstreamEntry::Record) {
5082      Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5083      return std::string();
5084    }
5085
5086    Record.clear();
5087    StringRef Blob;
5088    Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5089    if (!MaybeRecord) {
5090      // FIXME this drops the errors on the floor.
5091      consumeError(MaybeRecord.takeError());
5092      return std::string();
5093    }
5094    if (ORIGINAL_FILE == MaybeRecord.get())
5095      return Blob.str();
5096  }
5097}
5098
5099namespace {
5100
5101  class SimplePCHValidator : public ASTReaderListener {
5102    const LangOptions &ExistingLangOpts;
5103    const TargetOptions &ExistingTargetOpts;
5104    const PreprocessorOptions &ExistingPPOpts;
5105    std::string ExistingModuleCachePath;
5106    FileManager &FileMgr;
5107
5108  public:
5109    SimplePCHValidator(const LangOptions &ExistingLangOpts,
5110                       const TargetOptions &ExistingTargetOpts,
5111                       const PreprocessorOptions &ExistingPPOpts,
5112                       StringRef ExistingModuleCachePath, FileManager &FileMgr)
5113        : ExistingLangOpts(ExistingLangOpts),
5114          ExistingTargetOpts(ExistingTargetOpts),
5115          ExistingPPOpts(ExistingPPOpts),
5116          ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5117
5118    bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5119                             bool AllowCompatibleDifferences) override {
5120      return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5121                                  AllowCompatibleDifferences);
5122    }
5123
5124    bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5125                           bool AllowCompatibleDifferences) override {
5126      return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5127                                AllowCompatibleDifferences);
5128    }
5129
5130    bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5131                                 StringRef SpecificModuleCachePath,
5132                                 bool Complain) override {
5133      return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5134                                      ExistingModuleCachePath,
5135                                      nullptr, ExistingLangOpts);
5136    }
5137
5138    bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5139                                 bool Complain,
5140                                 std::string &SuggestedPredefines) override {
5141      return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5142                                      SuggestedPredefines, ExistingLangOpts);
5143    }
5144  };
5145
5146} // namespace
5147
5148bool ASTReader::readASTFileControlBlock(
5149    StringRef Filename, FileManager &FileMgr,
5150    const PCHContainerReader &PCHContainerRdr,
5151    bool FindModuleFileExtensions,
5152    ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5153  // Open the AST file.
5154  // FIXME: This allows use of the VFS; we do not allow use of the
5155  // VFS when actually loading a module.
5156  auto Buffer = FileMgr.getBufferForFile(Filename);
5157  if (!Buffer) {
5158    return true;
5159  }
5160
5161  // Initialize the stream
5162  StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5163  BitstreamCursor Stream(Bytes);
5164
5165  // Sniff for the signature.
5166  if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5167    consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5168    return true;
5169  }
5170
5171  // Scan for the CONTROL_BLOCK_ID block.
5172  if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5173    return true;
5174
5175  bool NeedsInputFiles = Listener.needsInputFileVisitation();
5176  bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5177  bool NeedsImports = Listener.needsImportVisitation();
5178  BitstreamCursor InputFilesCursor;
5179
5180  RecordData Record;
5181  std::string ModuleDir;
5182  bool DoneWithControlBlock = false;
5183  while (!DoneWithControlBlock) {
5184    Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5185    if (!MaybeEntry) {
5186      // FIXME this drops the error on the floor.
5187      consumeError(MaybeEntry.takeError());
5188      return true;
5189    }
5190    llvm::BitstreamEntry Entry = MaybeEntry.get();
5191
5192    switch (Entry.Kind) {
5193    case llvm::BitstreamEntry::SubBlock: {
5194      switch (Entry.ID) {
5195      case OPTIONS_BLOCK_ID: {
5196        std::string IgnoredSuggestedPredefines;
5197        if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5198                             /*AllowCompatibleConfigurationMismatch*/ false,
5199                             Listener, IgnoredSuggestedPredefines) != Success)
5200          return true;
5201        break;
5202      }
5203
5204      case INPUT_FILES_BLOCK_ID:
5205        InputFilesCursor = Stream;
5206        if (llvm::Error Err = Stream.SkipBlock()) {
5207          // FIXME this drops the error on the floor.
5208          consumeError(std::move(Err));
5209          return true;
5210        }
5211        if (NeedsInputFiles &&
5212            ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5213          return true;
5214        break;
5215
5216      default:
5217        if (llvm::Error Err = Stream.SkipBlock()) {
5218          // FIXME this drops the error on the floor.
5219          consumeError(std::move(Err));
5220          return true;
5221        }
5222        break;
5223      }
5224
5225      continue;
5226    }
5227
5228    case llvm::BitstreamEntry::EndBlock:
5229      DoneWithControlBlock = true;
5230      break;
5231
5232    case llvm::BitstreamEntry::Error:
5233      return true;
5234
5235    case llvm::BitstreamEntry::Record:
5236      break;
5237    }
5238
5239    if (DoneWithControlBlock) break;
5240
5241    Record.clear();
5242    StringRef Blob;
5243    Expected<unsigned> MaybeRecCode =
5244        Stream.readRecord(Entry.ID, Record, &Blob);
5245    if (!MaybeRecCode) {
5246      // FIXME this drops the error.
5247      return Failure;
5248    }
5249    switch ((ControlRecordTypes)MaybeRecCode.get()) {
5250    case METADATA:
5251      if (Record[0] != VERSION_MAJOR)
5252        return true;
5253      if (Listener.ReadFullVersionInformation(Blob))
5254        return true;
5255      break;
5256    case MODULE_NAME:
5257      Listener.ReadModuleName(Blob);
5258      break;
5259    case MODULE_DIRECTORY:
5260      ModuleDir = std::string(Blob);
5261      break;
5262    case MODULE_MAP_FILE: {
5263      unsigned Idx = 0;
5264      auto Path = ReadString(Record, Idx);
5265      ResolveImportedPath(Path, ModuleDir);
5266      Listener.ReadModuleMapFile(Path);
5267      break;
5268    }
5269    case INPUT_FILE_OFFSETS: {
5270      if (!NeedsInputFiles)
5271        break;
5272
5273      unsigned NumInputFiles = Record[0];
5274      unsigned NumUserFiles = Record[1];
5275      const llvm::support::unaligned_uint64_t *InputFileOffs =
5276          (const llvm::support::unaligned_uint64_t *)Blob.data();
5277      for (unsigned I = 0; I != NumInputFiles; ++I) {
5278        // Go find this input file.
5279        bool isSystemFile = I >= NumUserFiles;
5280
5281        if (isSystemFile && !NeedsSystemInputFiles)
5282          break; // the rest are system input files
5283
5284        BitstreamCursor &Cursor = InputFilesCursor;
5285        SavedStreamPosition SavedPosition(Cursor);
5286        if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5287          // FIXME this drops errors on the floor.
5288          consumeError(std::move(Err));
5289        }
5290
5291        Expected<unsigned> MaybeCode = Cursor.ReadCode();
5292        if (!MaybeCode) {
5293          // FIXME this drops errors on the floor.
5294          consumeError(MaybeCode.takeError());
5295        }
5296        unsigned Code = MaybeCode.get();
5297
5298        RecordData Record;
5299        StringRef Blob;
5300        bool shouldContinue = false;
5301        Expected<unsigned> MaybeRecordType =
5302            Cursor.readRecord(Code, Record, &Blob);
5303        if (!MaybeRecordType) {
5304          // FIXME this drops errors on the floor.
5305          consumeError(MaybeRecordType.takeError());
5306        }
5307        switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5308        case INPUT_FILE_HASH:
5309          break;
5310        case INPUT_FILE:
5311          bool Overridden = static_cast<bool>(Record[3]);
5312          std::string Filename = std::string(Blob);
5313          ResolveImportedPath(Filename, ModuleDir);
5314          shouldContinue = Listener.visitInputFile(
5315              Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5316          break;
5317        }
5318        if (!shouldContinue)
5319          break;
5320      }
5321      break;
5322    }
5323
5324    case IMPORTS: {
5325      if (!NeedsImports)
5326        break;
5327
5328      unsigned Idx = 0, N = Record.size();
5329      while (Idx < N) {
5330        // Read information about the AST file.
5331        Idx +=
5332            1 + 1 + 1 + 1 +
5333            ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5334        std::string ModuleName = ReadString(Record, Idx);
5335        std::string Filename = ReadString(Record, Idx);
5336        ResolveImportedPath(Filename, ModuleDir);
5337        Listener.visitImport(ModuleName, Filename);
5338      }
5339      break;
5340    }
5341
5342    default:
5343      // No other validation to perform.
5344      break;
5345    }
5346  }
5347
5348  // Look for module file extension blocks, if requested.
5349  if (FindModuleFileExtensions) {
5350    BitstreamCursor SavedStream = Stream;
5351    while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5352      bool DoneWithExtensionBlock = false;
5353      while (!DoneWithExtensionBlock) {
5354        Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5355        if (!MaybeEntry) {
5356          // FIXME this drops the error.
5357          return true;
5358        }
5359        llvm::BitstreamEntry Entry = MaybeEntry.get();
5360
5361        switch (Entry.Kind) {
5362        case llvm::BitstreamEntry::SubBlock:
5363          if (llvm::Error Err = Stream.SkipBlock()) {
5364            // FIXME this drops the error on the floor.
5365            consumeError(std::move(Err));
5366            return true;
5367          }
5368          continue;
5369
5370        case llvm::BitstreamEntry::EndBlock:
5371          DoneWithExtensionBlock = true;
5372          continue;
5373
5374        case llvm::BitstreamEntry::Error:
5375          return true;
5376
5377        case llvm::BitstreamEntry::Record:
5378          break;
5379        }
5380
5381       Record.clear();
5382       StringRef Blob;
5383       Expected<unsigned> MaybeRecCode =
5384           Stream.readRecord(Entry.ID, Record, &Blob);
5385       if (!MaybeRecCode) {
5386         // FIXME this drops the error.
5387         return true;
5388       }
5389       switch (MaybeRecCode.get()) {
5390       case EXTENSION_METADATA: {
5391         ModuleFileExtensionMetadata Metadata;
5392         if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5393           return true;
5394
5395         Listener.readModuleFileExtension(Metadata);
5396         break;
5397       }
5398       }
5399      }
5400    }
5401    Stream = SavedStream;
5402  }
5403
5404  // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5405  if (readUnhashedControlBlockImpl(
5406          nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5407          /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5408          ValidateDiagnosticOptions) != Success)
5409    return true;
5410
5411  return false;
5412}
5413
5414bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5415                                    const PCHContainerReader &PCHContainerRdr,
5416                                    const LangOptions &LangOpts,
5417                                    const TargetOptions &TargetOpts,
5418                                    const PreprocessorOptions &PPOpts,
5419                                    StringRef ExistingModuleCachePath) {
5420  SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5421                               ExistingModuleCachePath, FileMgr);
5422  return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5423                                  /*FindModuleFileExtensions=*/false,
5424                                  validator,
5425                                  /*ValidateDiagnosticOptions=*/true);
5426}
5427
5428ASTReader::ASTReadResult
5429ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5430  // Enter the submodule block.
5431  if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5432    Error(std::move(Err));
5433    return Failure;
5434  }
5435
5436  ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5437  bool First = true;
5438  Module *CurrentModule = nullptr;
5439  RecordData Record;
5440  while (true) {
5441    Expected<llvm::BitstreamEntry> MaybeEntry =
5442        F.Stream.advanceSkippingSubblocks();
5443    if (!MaybeEntry) {
5444      Error(MaybeEntry.takeError());
5445      return Failure;
5446    }
5447    llvm::BitstreamEntry Entry = MaybeEntry.get();
5448
5449    switch (Entry.Kind) {
5450    case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5451    case llvm::BitstreamEntry::Error:
5452      Error("malformed block record in AST file");
5453      return Failure;
5454    case llvm::BitstreamEntry::EndBlock:
5455      return Success;
5456    case llvm::BitstreamEntry::Record:
5457      // The interesting case.
5458      break;
5459    }
5460
5461    // Read a record.
5462    StringRef Blob;
5463    Record.clear();
5464    Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5465    if (!MaybeKind) {
5466      Error(MaybeKind.takeError());
5467      return Failure;
5468    }
5469    unsigned Kind = MaybeKind.get();
5470
5471    if ((Kind == SUBMODULE_METADATA) != First) {
5472      Error("submodule metadata record should be at beginning of block");
5473      return Failure;
5474    }
5475    First = false;
5476
5477    // Submodule information is only valid if we have a current module.
5478    // FIXME: Should we error on these cases?
5479    if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5480        Kind != SUBMODULE_DEFINITION)
5481      continue;
5482
5483    switch (Kind) {
5484    default:  // Default behavior: ignore.
5485      break;
5486
5487    case SUBMODULE_DEFINITION: {
5488      if (Record.size() < 12) {
5489        Error("malformed module definition");
5490        return Failure;
5491      }
5492
5493      StringRef Name = Blob;
5494      unsigned Idx = 0;
5495      SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5496      SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5497      Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5498      bool IsFramework = Record[Idx++];
5499      bool IsExplicit = Record[Idx++];
5500      bool IsSystem = Record[Idx++];
5501      bool IsExternC = Record[Idx++];
5502      bool InferSubmodules = Record[Idx++];
5503      bool InferExplicitSubmodules = Record[Idx++];
5504      bool InferExportWildcard = Record[Idx++];
5505      bool ConfigMacrosExhaustive = Record[Idx++];
5506      bool ModuleMapIsPrivate = Record[Idx++];
5507
5508      Module *ParentModule = nullptr;
5509      if (Parent)
5510        ParentModule = getSubmodule(Parent);
5511
5512      // Retrieve this (sub)module from the module map, creating it if
5513      // necessary.
5514      CurrentModule =
5515          ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5516              .first;
5517
5518      // FIXME: set the definition loc for CurrentModule, or call
5519      // ModMap.setInferredModuleAllowedBy()
5520
5521      SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5522      if (GlobalIndex >= SubmodulesLoaded.size() ||
5523          SubmodulesLoaded[GlobalIndex]) {
5524        Error("too many submodules");
5525        return Failure;
5526      }
5527
5528      if (!ParentModule) {
5529        if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5530          // Don't emit module relocation error if we have -fno-validate-pch
5531          if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5532              CurFile != F.File) {
5533            Error(diag::err_module_file_conflict,
5534                  CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5535                  F.File->getName());
5536            return Failure;
5537          }
5538        }
5539
5540        F.DidReadTopLevelSubmodule = true;
5541        CurrentModule->setASTFile(F.File);
5542        CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5543      }
5544
5545      CurrentModule->Kind = Kind;
5546      CurrentModule->Signature = F.Signature;
5547      CurrentModule->IsFromModuleFile = true;
5548      CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5549      CurrentModule->IsExternC = IsExternC;
5550      CurrentModule->InferSubmodules = InferSubmodules;
5551      CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5552      CurrentModule->InferExportWildcard = InferExportWildcard;
5553      CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5554      CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5555      if (DeserializationListener)
5556        DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5557
5558      SubmodulesLoaded[GlobalIndex] = CurrentModule;
5559
5560      // Clear out data that will be replaced by what is in the module file.
5561      CurrentModule->LinkLibraries.clear();
5562      CurrentModule->ConfigMacros.clear();
5563      CurrentModule->UnresolvedConflicts.clear();
5564      CurrentModule->Conflicts.clear();
5565
5566      // The module is available unless it's missing a requirement; relevant
5567      // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5568      // Missing headers that were present when the module was built do not
5569      // make it unavailable -- if we got this far, this must be an explicitly
5570      // imported module file.
5571      CurrentModule->Requirements.clear();
5572      CurrentModule->MissingHeaders.clear();
5573      CurrentModule->IsUnimportable =
5574          ParentModule && ParentModule->IsUnimportable;
5575      CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5576      break;
5577    }
5578
5579    case SUBMODULE_UMBRELLA_HEADER: {
5580      std::string Filename = std::string(Blob);
5581      ResolveImportedPath(F, Filename);
5582      if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5583        if (!CurrentModule->getUmbrellaHeader())
5584          ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5585        else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5586          if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5587            Error("mismatched umbrella headers in submodule");
5588          return OutOfDate;
5589        }
5590      }
5591      break;
5592    }
5593
5594    case SUBMODULE_HEADER:
5595    case SUBMODULE_EXCLUDED_HEADER:
5596    case SUBMODULE_PRIVATE_HEADER:
5597      // We lazily associate headers with their modules via the HeaderInfo table.
5598      // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5599      // of complete filenames or remove it entirely.
5600      break;
5601
5602    case SUBMODULE_TEXTUAL_HEADER:
5603    case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5604      // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5605      // them here.
5606      break;
5607
5608    case SUBMODULE_TOPHEADER:
5609      CurrentModule->addTopHeaderFilename(Blob);
5610      break;
5611
5612    case SUBMODULE_UMBRELLA_DIR: {
5613      std::string Dirname = std::string(Blob);
5614      ResolveImportedPath(F, Dirname);
5615      if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5616        if (!CurrentModule->getUmbrellaDir())
5617          ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5618        else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5619          if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5620            Error("mismatched umbrella directories in submodule");
5621          return OutOfDate;
5622        }
5623      }
5624      break;
5625    }
5626
5627    case SUBMODULE_METADATA: {
5628      F.BaseSubmoduleID = getTotalNumSubmodules();
5629      F.LocalNumSubmodules = Record[0];
5630      unsigned LocalBaseSubmoduleID = Record[1];
5631      if (F.LocalNumSubmodules > 0) {
5632        // Introduce the global -> local mapping for submodules within this
5633        // module.
5634        GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5635
5636        // Introduce the local -> global mapping for submodules within this
5637        // module.
5638        F.SubmoduleRemap.insertOrReplace(
5639          std::make_pair(LocalBaseSubmoduleID,
5640                         F.BaseSubmoduleID - LocalBaseSubmoduleID));
5641
5642        SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5643      }
5644      break;
5645    }
5646
5647    case SUBMODULE_IMPORTS:
5648      for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5649        UnresolvedModuleRef Unresolved;
5650        Unresolved.File = &F;
5651        Unresolved.Mod = CurrentModule;
5652        Unresolved.ID = Record[Idx];
5653        Unresolved.Kind = UnresolvedModuleRef::Import;
5654        Unresolved.IsWildcard = false;
5655        UnresolvedModuleRefs.push_back(Unresolved);
5656      }
5657      break;
5658
5659    case SUBMODULE_EXPORTS:
5660      for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5661        UnresolvedModuleRef Unresolved;
5662        Unresolved.File = &F;
5663        Unresolved.Mod = CurrentModule;
5664        Unresolved.ID = Record[Idx];
5665        Unresolved.Kind = UnresolvedModuleRef::Export;
5666        Unresolved.IsWildcard = Record[Idx + 1];
5667        UnresolvedModuleRefs.push_back(Unresolved);
5668      }
5669
5670      // Once we've loaded the set of exports, there's no reason to keep
5671      // the parsed, unresolved exports around.
5672      CurrentModule->UnresolvedExports.clear();
5673      break;
5674
5675    case SUBMODULE_REQUIRES:
5676      CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5677                                    PP.getTargetInfo());
5678      break;
5679
5680    case SUBMODULE_LINK_LIBRARY:
5681      ModMap.resolveLinkAsDependencies(CurrentModule);
5682      CurrentModule->LinkLibraries.push_back(
5683          Module::LinkLibrary(std::string(Blob), Record[0]));
5684      break;
5685
5686    case SUBMODULE_CONFIG_MACRO:
5687      CurrentModule->ConfigMacros.push_back(Blob.str());
5688      break;
5689
5690    case SUBMODULE_CONFLICT: {
5691      UnresolvedModuleRef Unresolved;
5692      Unresolved.File = &F;
5693      Unresolved.Mod = CurrentModule;
5694      Unresolved.ID = Record[0];
5695      Unresolved.Kind = UnresolvedModuleRef::Conflict;
5696      Unresolved.IsWildcard = false;
5697      Unresolved.String = Blob;
5698      UnresolvedModuleRefs.push_back(Unresolved);
5699      break;
5700    }
5701
5702    case SUBMODULE_INITIALIZERS: {
5703      if (!ContextObj)
5704        break;
5705      SmallVector<uint32_t, 16> Inits;
5706      for (auto &ID : Record)
5707        Inits.push_back(getGlobalDeclID(F, ID));
5708      ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5709      break;
5710    }
5711
5712    case SUBMODULE_EXPORT_AS:
5713      CurrentModule->ExportAsModule = Blob.str();
5714      ModMap.addLinkAsDependency(CurrentModule);
5715      break;
5716    }
5717  }
5718}
5719
5720/// Parse the record that corresponds to a LangOptions data
5721/// structure.
5722///
5723/// This routine parses the language options from the AST file and then gives
5724/// them to the AST listener if one is set.
5725///
5726/// \returns true if the listener deems the file unacceptable, false otherwise.
5727bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5728                                     bool Complain,
5729                                     ASTReaderListener &Listener,
5730                                     bool AllowCompatibleDifferences) {
5731  LangOptions LangOpts;
5732  unsigned Idx = 0;
5733#define LANGOPT(Name, Bits, Default, Description) \
5734  LangOpts.Name = Record[Idx++];
5735#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5736  LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5737#include "clang/Basic/LangOptions.def"
5738#define SANITIZER(NAME, ID)                                                    \
5739  LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5740#include "clang/Basic/Sanitizers.def"
5741
5742  for (unsigned N = Record[Idx++]; N; --N)
5743    LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5744
5745  ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5746  VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5747  LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5748
5749  LangOpts.CurrentModule = ReadString(Record, Idx);
5750
5751  // Comment options.
5752  for (unsigned N = Record[Idx++]; N; --N) {
5753    LangOpts.CommentOpts.BlockCommandNames.push_back(
5754      ReadString(Record, Idx));
5755  }
5756  LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5757
5758  // OpenMP offloading options.
5759  for (unsigned N = Record[Idx++]; N; --N) {
5760    LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5761  }
5762
5763  LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5764
5765  return Listener.ReadLanguageOptions(LangOpts, Complain,
5766                                      AllowCompatibleDifferences);
5767}
5768
5769bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5770                                   ASTReaderListener &Listener,
5771                                   bool AllowCompatibleDifferences) {
5772  unsigned Idx = 0;
5773  TargetOptions TargetOpts;
5774  TargetOpts.Triple = ReadString(Record, Idx);
5775  TargetOpts.CPU = ReadString(Record, Idx);
5776  TargetOpts.ABI = ReadString(Record, Idx);
5777  for (unsigned N = Record[Idx++]; N; --N) {
5778    TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5779  }
5780  for (unsigned N = Record[Idx++]; N; --N) {
5781    TargetOpts.Features.push_back(ReadString(Record, Idx));
5782  }
5783
5784  return Listener.ReadTargetOptions(TargetOpts, Complain,
5785                                    AllowCompatibleDifferences);
5786}
5787
5788bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5789                                       ASTReaderListener &Listener) {
5790  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5791  unsigned Idx = 0;
5792#define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5793#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5794  DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5795#include "clang/Basic/DiagnosticOptions.def"
5796
5797  for (unsigned N = Record[Idx++]; N; --N)
5798    DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5799  for (unsigned N = Record[Idx++]; N; --N)
5800    DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5801
5802  return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5803}
5804
5805bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5806                                       ASTReaderListener &Listener) {
5807  FileSystemOptions FSOpts;
5808  unsigned Idx = 0;
5809  FSOpts.WorkingDir = ReadString(Record, Idx);
5810  return Listener.ReadFileSystemOptions(FSOpts, Complain);
5811}
5812
5813bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5814                                         bool Complain,
5815                                         ASTReaderListener &Listener) {
5816  HeaderSearchOptions HSOpts;
5817  unsigned Idx = 0;
5818  HSOpts.Sysroot = ReadString(Record, Idx);
5819
5820  // Include entries.
5821  for (unsigned N = Record[Idx++]; N; --N) {
5822    std::string Path = ReadString(Record, Idx);
5823    frontend::IncludeDirGroup Group
5824      = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5825    bool IsFramework = Record[Idx++];
5826    bool IgnoreSysRoot = Record[Idx++];
5827    HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5828                                    IgnoreSysRoot);
5829  }
5830
5831  // System header prefixes.
5832  for (unsigned N = Record[Idx++]; N; --N) {
5833    std::string Prefix = ReadString(Record, Idx);
5834    bool IsSystemHeader = Record[Idx++];
5835    HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5836  }
5837
5838  HSOpts.ResourceDir = ReadString(Record, Idx);
5839  HSOpts.ModuleCachePath = ReadString(Record, Idx);
5840  HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5841  HSOpts.DisableModuleHash = Record[Idx++];
5842  HSOpts.ImplicitModuleMaps = Record[Idx++];
5843  HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5844  HSOpts.UseBuiltinIncludes = Record[Idx++];
5845  HSOpts.UseStandardSystemIncludes = Record[Idx++];
5846  HSOpts.UseStandardCXXIncludes = Record[Idx++];
5847  HSOpts.UseLibcxx = Record[Idx++];
5848  std::string SpecificModuleCachePath = ReadString(Record, Idx);
5849
5850  return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5851                                          Complain);
5852}
5853
5854bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5855                                         bool Complain,
5856                                         ASTReaderListener &Listener,
5857                                         std::string &SuggestedPredefines) {
5858  PreprocessorOptions PPOpts;
5859  unsigned Idx = 0;
5860
5861  // Macro definitions/undefs
5862  for (unsigned N = Record[Idx++]; N; --N) {
5863    std::string Macro = ReadString(Record, Idx);
5864    bool IsUndef = Record[Idx++];
5865    PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5866  }
5867
5868  // Includes
5869  for (unsigned N = Record[Idx++]; N; --N) {
5870    PPOpts.Includes.push_back(ReadString(Record, Idx));
5871  }
5872
5873  // Macro Includes
5874  for (unsigned N = Record[Idx++]; N; --N) {
5875    PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5876  }
5877
5878  PPOpts.UsePredefines = Record[Idx++];
5879  PPOpts.DetailedRecord = Record[Idx++];
5880  PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5881  PPOpts.ObjCXXARCStandardLibrary =
5882    static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5883  SuggestedPredefines.clear();
5884  return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5885                                          SuggestedPredefines);
5886}
5887
5888std::pair<ModuleFile *, unsigned>
5889ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5890  GlobalPreprocessedEntityMapType::iterator
5891  I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5892  assert(I != GlobalPreprocessedEntityMap.end() &&
5893         "Corrupted global preprocessed entity map");
5894  ModuleFile *M = I->second;
5895  unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5896  return std::make_pair(M, LocalIndex);
5897}
5898
5899llvm::iterator_range<PreprocessingRecord::iterator>
5900ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5901  if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5902    return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5903                                             Mod.NumPreprocessedEntities);
5904
5905  return llvm::make_range(PreprocessingRecord::iterator(),
5906                          PreprocessingRecord::iterator());
5907}
5908
5909llvm::iterator_range<ASTReader::ModuleDeclIterator>
5910ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5911  return llvm::make_range(
5912      ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5913      ModuleDeclIterator(this, &Mod,
5914                         Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5915}
5916
5917SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5918  auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5919  assert(I != GlobalSkippedRangeMap.end() &&
5920    "Corrupted global skipped range map");
5921  ModuleFile *M = I->second;
5922  unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5923  assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5924  PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5925  SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5926                    TranslateSourceLocation(*M, RawRange.getEnd()));
5927  assert(Range.isValid());
5928  return Range;
5929}
5930
5931PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5932  PreprocessedEntityID PPID = Index+1;
5933  std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5934  ModuleFile &M = *PPInfo.first;
5935  unsigned LocalIndex = PPInfo.second;
5936  const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5937
5938  if (!PP.getPreprocessingRecord()) {
5939    Error("no preprocessing record");
5940    return nullptr;
5941  }
5942
5943  SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5944  if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5945          M.MacroOffsetsBase + PPOffs.BitOffset)) {
5946    Error(std::move(Err));
5947    return nullptr;
5948  }
5949
5950  Expected<llvm::BitstreamEntry> MaybeEntry =
5951      M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5952  if (!MaybeEntry) {
5953    Error(MaybeEntry.takeError());
5954    return nullptr;
5955  }
5956  llvm::BitstreamEntry Entry = MaybeEntry.get();
5957
5958  if (Entry.Kind != llvm::BitstreamEntry::Record)
5959    return nullptr;
5960
5961  // Read the record.
5962  SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5963                    TranslateSourceLocation(M, PPOffs.getEnd()));
5964  PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5965  StringRef Blob;
5966  RecordData Record;
5967  Expected<unsigned> MaybeRecType =
5968      M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5969  if (!MaybeRecType) {
5970    Error(MaybeRecType.takeError());
5971    return nullptr;
5972  }
5973  switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5974  case PPD_MACRO_EXPANSION: {
5975    bool isBuiltin = Record[0];
5976    IdentifierInfo *Name = nullptr;
5977    MacroDefinitionRecord *Def = nullptr;
5978    if (isBuiltin)
5979      Name = getLocalIdentifier(M, Record[1]);
5980    else {
5981      PreprocessedEntityID GlobalID =
5982          getGlobalPreprocessedEntityID(M, Record[1]);
5983      Def = cast<MacroDefinitionRecord>(
5984          PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5985    }
5986
5987    MacroExpansion *ME;
5988    if (isBuiltin)
5989      ME = new (PPRec) MacroExpansion(Name, Range);
5990    else
5991      ME = new (PPRec) MacroExpansion(Def, Range);
5992
5993    return ME;
5994  }
5995
5996  case PPD_MACRO_DEFINITION: {
5997    // Decode the identifier info and then check again; if the macro is
5998    // still defined and associated with the identifier,
5999    IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6000    MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6001
6002    if (DeserializationListener)
6003      DeserializationListener->MacroDefinitionRead(PPID, MD);
6004
6005    return MD;
6006  }
6007
6008  case PPD_INCLUSION_DIRECTIVE: {
6009    const char *FullFileNameStart = Blob.data() + Record[0];
6010    StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6011    const FileEntry *File = nullptr;
6012    if (!FullFileName.empty())
6013      if (auto FE = PP.getFileManager().getFile(FullFileName))
6014        File = *FE;
6015
6016    // FIXME: Stable encoding
6017    InclusionDirective::InclusionKind Kind
6018      = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6019    InclusionDirective *ID
6020      = new (PPRec) InclusionDirective(PPRec, Kind,
6021                                       StringRef(Blob.data(), Record[0]),
6022                                       Record[1], Record[3],
6023                                       File,
6024                                       Range);
6025    return ID;
6026  }
6027  }
6028
6029  llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6030}
6031
6032/// Find the next module that contains entities and return the ID
6033/// of the first entry.
6034///
6035/// \param SLocMapI points at a chunk of a module that contains no
6036/// preprocessed entities or the entities it contains are not the ones we are
6037/// looking for.
6038PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6039                       GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6040  ++SLocMapI;
6041  for (GlobalSLocOffsetMapType::const_iterator
6042         EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6043    ModuleFile &M = *SLocMapI->second;
6044    if (M.NumPreprocessedEntities)
6045      return M.BasePreprocessedEntityID;
6046  }
6047
6048  return getTotalNumPreprocessedEntities();
6049}
6050
6051namespace {
6052
6053struct PPEntityComp {
6054  const ASTReader &Reader;
6055  ModuleFile &M;
6056
6057  PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6058
6059  bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6060    SourceLocation LHS = getLoc(L);
6061    SourceLocation RHS = getLoc(R);
6062    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6063  }
6064
6065  bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6066    SourceLocation LHS = getLoc(L);
6067    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6068  }
6069
6070  bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6071    SourceLocation RHS = getLoc(R);
6072    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6073  }
6074
6075  SourceLocation getLoc(const PPEntityOffset &PPE) const {
6076    return Reader.TranslateSourceLocation(M, PPE.getBegin());
6077  }
6078};
6079
6080} // namespace
6081
6082PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6083                                                       bool EndsAfter) const {
6084  if (SourceMgr.isLocalSourceLocation(Loc))
6085    return getTotalNumPreprocessedEntities();
6086
6087  GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6088      SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6089  assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6090         "Corrupted global sloc offset map");
6091
6092  if (SLocMapI->second->NumPreprocessedEntities == 0)
6093    return findNextPreprocessedEntity(SLocMapI);
6094
6095  ModuleFile &M = *SLocMapI->second;
6096
6097  using pp_iterator = const PPEntityOffset *;
6098
6099  pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6100  pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6101
6102  size_t Count = M.NumPreprocessedEntities;
6103  size_t Half;
6104  pp_iterator First = pp_begin;
6105  pp_iterator PPI;
6106
6107  if (EndsAfter) {
6108    PPI = std::upper_bound(pp_begin, pp_end, Loc,
6109                           PPEntityComp(*this, M));
6110  } else {
6111    // Do a binary search manually instead of using std::lower_bound because
6112    // The end locations of entities may be unordered (when a macro expansion
6113    // is inside another macro argument), but for this case it is not important
6114    // whether we get the first macro expansion or its containing macro.
6115    while (Count > 0) {
6116      Half = Count / 2;
6117      PPI = First;
6118      std::advance(PPI, Half);
6119      if (SourceMgr.isBeforeInTranslationUnit(
6120              TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6121        First = PPI;
6122        ++First;
6123        Count = Count - Half - 1;
6124      } else
6125        Count = Half;
6126    }
6127  }
6128
6129  if (PPI == pp_end)
6130    return findNextPreprocessedEntity(SLocMapI);
6131
6132  return M.BasePreprocessedEntityID + (PPI - pp_begin);
6133}
6134
6135/// Returns a pair of [Begin, End) indices of preallocated
6136/// preprocessed entities that \arg Range encompasses.
6137std::pair<unsigned, unsigned>
6138    ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6139  if (Range.isInvalid())
6140    return std::make_pair(0,0);
6141  assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6142
6143  PreprocessedEntityID BeginID =
6144      findPreprocessedEntity(Range.getBegin(), false);
6145  PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6146  return std::make_pair(BeginID, EndID);
6147}
6148
6149/// Optionally returns true or false if the preallocated preprocessed
6150/// entity with index \arg Index came from file \arg FID.
6151Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6152                                                             FileID FID) {
6153  if (FID.isInvalid())
6154    return false;
6155
6156  std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6157  ModuleFile &M = *PPInfo.first;
6158  unsigned LocalIndex = PPInfo.second;
6159  const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6160
6161  SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6162  if (Loc.isInvalid())
6163    return false;
6164
6165  if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6166    return true;
6167  else
6168    return false;
6169}
6170
6171namespace {
6172
6173  /// Visitor used to search for information about a header file.
6174  class HeaderFileInfoVisitor {
6175    const FileEntry *FE;
6176    Optional<HeaderFileInfo> HFI;
6177
6178  public:
6179    explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6180
6181    bool operator()(ModuleFile &M) {
6182      HeaderFileInfoLookupTable *Table
6183        = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6184      if (!Table)
6185        return false;
6186
6187      // Look in the on-disk hash table for an entry for this file name.
6188      HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6189      if (Pos == Table->end())
6190        return false;
6191
6192      HFI = *Pos;
6193      return true;
6194    }
6195
6196    Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6197  };
6198
6199} // namespace
6200
6201HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6202  HeaderFileInfoVisitor Visitor(FE);
6203  ModuleMgr.visit(Visitor);
6204  if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6205    return *HFI;
6206
6207  return HeaderFileInfo();
6208}
6209
6210void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6211  using DiagState = DiagnosticsEngine::DiagState;
6212  SmallVector<DiagState *, 32> DiagStates;
6213
6214  for (ModuleFile &F : ModuleMgr) {
6215    unsigned Idx = 0;
6216    auto &Record = F.PragmaDiagMappings;
6217    if (Record.empty())
6218      continue;
6219
6220    DiagStates.clear();
6221
6222    auto ReadDiagState =
6223        [&](const DiagState &BasedOn, SourceLocation Loc,
6224            bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6225      unsigned BackrefID = Record[Idx++];
6226      if (BackrefID != 0)
6227        return DiagStates[BackrefID - 1];
6228
6229      // A new DiagState was created here.
6230      Diag.DiagStates.push_back(BasedOn);
6231      DiagState *NewState = &Diag.DiagStates.back();
6232      DiagStates.push_back(NewState);
6233      unsigned Size = Record[Idx++];
6234      assert(Idx + Size * 2 <= Record.size() &&
6235             "Invalid data, not enough diag/map pairs");
6236      while (Size--) {
6237        unsigned DiagID = Record[Idx++];
6238        DiagnosticMapping NewMapping =
6239            DiagnosticMapping::deserialize(Record[Idx++]);
6240        if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6241          continue;
6242
6243        DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6244
6245        // If this mapping was specified as a warning but the severity was
6246        // upgraded due to diagnostic settings, simulate the current diagnostic
6247        // settings (and use a warning).
6248        if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6249          NewMapping.setSeverity(diag::Severity::Warning);
6250          NewMapping.setUpgradedFromWarning(false);
6251        }
6252
6253        Mapping = NewMapping;
6254      }
6255      return NewState;
6256    };
6257
6258    // Read the first state.
6259    DiagState *FirstState;
6260    if (F.Kind == MK_ImplicitModule) {
6261      // Implicitly-built modules are reused with different diagnostic
6262      // settings.  Use the initial diagnostic state from Diag to simulate this
6263      // compilation's diagnostic settings.
6264      FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6265      DiagStates.push_back(FirstState);
6266
6267      // Skip the initial diagnostic state from the serialized module.
6268      assert(Record[1] == 0 &&
6269             "Invalid data, unexpected backref in initial state");
6270      Idx = 3 + Record[2] * 2;
6271      assert(Idx < Record.size() &&
6272             "Invalid data, not enough state change pairs in initial state");
6273    } else if (F.isModule()) {
6274      // For an explicit module, preserve the flags from the module build
6275      // command line (-w, -Weverything, -Werror, ...) along with any explicit
6276      // -Wblah flags.
6277      unsigned Flags = Record[Idx++];
6278      DiagState Initial;
6279      Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6280      Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6281      Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6282      Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6283      Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6284      Initial.ExtBehavior = (diag::Severity)Flags;
6285      FirstState = ReadDiagState(Initial, SourceLocation(), true);
6286
6287      assert(F.OriginalSourceFileID.isValid());
6288
6289      // Set up the root buffer of the module to start with the initial
6290      // diagnostic state of the module itself, to cover files that contain no
6291      // explicit transitions (for which we did not serialize anything).
6292      Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6293          .StateTransitions.push_back({FirstState, 0});
6294    } else {
6295      // For prefix ASTs, start with whatever the user configured on the
6296      // command line.
6297      Idx++; // Skip flags.
6298      FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6299                                 SourceLocation(), false);
6300    }
6301
6302    // Read the state transitions.
6303    unsigned NumLocations = Record[Idx++];
6304    while (NumLocations--) {
6305      assert(Idx < Record.size() &&
6306             "Invalid data, missing pragma diagnostic states");
6307      SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6308      auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6309      assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6310      assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6311      unsigned Transitions = Record[Idx++];
6312
6313      // Note that we don't need to set up Parent/ParentOffset here, because
6314      // we won't be changing the diagnostic state within imported FileIDs
6315      // (other than perhaps appending to the main source file, which has no
6316      // parent).
6317      auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6318      F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6319      for (unsigned I = 0; I != Transitions; ++I) {
6320        unsigned Offset = Record[Idx++];
6321        auto *State =
6322            ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6323        F.StateTransitions.push_back({State, Offset});
6324      }
6325    }
6326
6327    // Read the final state.
6328    assert(Idx < Record.size() &&
6329           "Invalid data, missing final pragma diagnostic state");
6330    SourceLocation CurStateLoc =
6331        ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6332    auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6333
6334    if (!F.isModule()) {
6335      Diag.DiagStatesByLoc.CurDiagState = CurState;
6336      Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6337
6338      // Preserve the property that the imaginary root file describes the
6339      // current state.
6340      FileID NullFile;
6341      auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6342      if (T.empty())
6343        T.push_back({CurState, 0});
6344      else
6345        T[0].State = CurState;
6346    }
6347
6348    // Don't try to read these mappings again.
6349    Record.clear();
6350  }
6351}
6352
6353/// Get the correct cursor and offset for loading a type.
6354ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6355  GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6356  assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6357  ModuleFile *M = I->second;
6358  return RecordLocation(
6359      M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6360             M->DeclsBlockStartOffset);
6361}
6362
6363static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6364  switch (code) {
6365#define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6366  case TYPE_##CODE_ID: return Type::CLASS_ID;
6367#include "clang/Serialization/TypeBitCodes.def"
6368  default: return llvm::None;
6369  }
6370}
6371
6372/// Read and return the type with the given index..
6373///
6374/// The index is the type ID, shifted and minus the number of predefs. This
6375/// routine actually reads the record corresponding to the type at the given
6376/// location. It is a helper routine for GetType, which deals with reading type
6377/// IDs.
6378QualType ASTReader::readTypeRecord(unsigned Index) {
6379  assert(ContextObj && "reading type with no AST context");
6380  ASTContext &Context = *ContextObj;
6381  RecordLocation Loc = TypeCursorForIndex(Index);
6382  BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6383
6384  // Keep track of where we are in the stream, then jump back there
6385  // after reading this type.
6386  SavedStreamPosition SavedPosition(DeclsCursor);
6387
6388  ReadingKindTracker ReadingKind(Read_Type, *this);
6389
6390  // Note that we are loading a type record.
6391  Deserializing AType(this);
6392
6393  if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6394    Error(std::move(Err));
6395    return QualType();
6396  }
6397  Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6398  if (!RawCode) {
6399    Error(RawCode.takeError());
6400    return QualType();
6401  }
6402
6403  ASTRecordReader Record(*this, *Loc.F);
6404  Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6405  if (!Code) {
6406    Error(Code.takeError());
6407    return QualType();
6408  }
6409  if (Code.get() == TYPE_EXT_QUAL) {
6410    QualType baseType = Record.readQualType();
6411    Qualifiers quals = Record.readQualifiers();
6412    return Context.getQualifiedType(baseType, quals);
6413  }
6414
6415  auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6416  if (!maybeClass) {
6417    Error("Unexpected code for type");
6418    return QualType();
6419  }
6420
6421  serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6422  return TypeReader.read(*maybeClass);
6423}
6424
6425namespace clang {
6426
6427class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6428  ASTRecordReader &Reader;
6429
6430  SourceLocation readSourceLocation() {
6431    return Reader.readSourceLocation();
6432  }
6433
6434  TypeSourceInfo *GetTypeSourceInfo() {
6435    return Reader.readTypeSourceInfo();
6436  }
6437
6438  NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6439    return Reader.readNestedNameSpecifierLoc();
6440  }
6441
6442  Attr *ReadAttr() {
6443    return Reader.readAttr();
6444  }
6445
6446public:
6447  TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6448
6449  // We want compile-time assurance that we've enumerated all of
6450  // these, so unfortunately we have to declare them first, then
6451  // define them out-of-line.
6452#define ABSTRACT_TYPELOC(CLASS, PARENT)
6453#define TYPELOC(CLASS, PARENT) \
6454  void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6455#include "clang/AST/TypeLocNodes.def"
6456
6457  void VisitFunctionTypeLoc(FunctionTypeLoc);
6458  void VisitArrayTypeLoc(ArrayTypeLoc);
6459};
6460
6461} // namespace clang
6462
6463void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6464  // nothing to do
6465}
6466
6467void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6468  TL.setBuiltinLoc(readSourceLocation());
6469  if (TL.needsExtraLocalData()) {
6470    TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6471    TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6472    TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6473    TL.setModeAttr(Reader.readInt());
6474  }
6475}
6476
6477void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6478  TL.setNameLoc(readSourceLocation());
6479}
6480
6481void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6482  TL.setStarLoc(readSourceLocation());
6483}
6484
6485void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6486  // nothing to do
6487}
6488
6489void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6490  // nothing to do
6491}
6492
6493void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6494  TL.setExpansionLoc(readSourceLocation());
6495}
6496
6497void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6498  TL.setCaretLoc(readSourceLocation());
6499}
6500
6501void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6502  TL.setAmpLoc(readSourceLocation());
6503}
6504
6505void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6506  TL.setAmpAmpLoc(readSourceLocation());
6507}
6508
6509void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6510  TL.setStarLoc(readSourceLocation());
6511  TL.setClassTInfo(GetTypeSourceInfo());
6512}
6513
6514void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6515  TL.setLBracketLoc(readSourceLocation());
6516  TL.setRBracketLoc(readSourceLocation());
6517  if (Reader.readBool())
6518    TL.setSizeExpr(Reader.readExpr());
6519  else
6520    TL.setSizeExpr(nullptr);
6521}
6522
6523void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6524  VisitArrayTypeLoc(TL);
6525}
6526
6527void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6528  VisitArrayTypeLoc(TL);
6529}
6530
6531void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6532  VisitArrayTypeLoc(TL);
6533}
6534
6535void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6536                                            DependentSizedArrayTypeLoc TL) {
6537  VisitArrayTypeLoc(TL);
6538}
6539
6540void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6541    DependentAddressSpaceTypeLoc TL) {
6542
6543    TL.setAttrNameLoc(readSourceLocation());
6544    TL.setAttrOperandParensRange(Reader.readSourceRange());
6545    TL.setAttrExprOperand(Reader.readExpr());
6546}
6547
6548void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6549                                        DependentSizedExtVectorTypeLoc TL) {
6550  TL.setNameLoc(readSourceLocation());
6551}
6552
6553void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6554  TL.setNameLoc(readSourceLocation());
6555}
6556
6557void TypeLocReader::VisitDependentVectorTypeLoc(
6558    DependentVectorTypeLoc TL) {
6559  TL.setNameLoc(readSourceLocation());
6560}
6561
6562void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6563  TL.setNameLoc(readSourceLocation());
6564}
6565
6566void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6567  TL.setAttrNameLoc(readSourceLocation());
6568  TL.setAttrOperandParensRange(Reader.readSourceRange());
6569  TL.setAttrRowOperand(Reader.readExpr());
6570  TL.setAttrColumnOperand(Reader.readExpr());
6571}
6572
6573void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6574    DependentSizedMatrixTypeLoc TL) {
6575  TL.setAttrNameLoc(readSourceLocation());
6576  TL.setAttrOperandParensRange(Reader.readSourceRange());
6577  TL.setAttrRowOperand(Reader.readExpr());
6578  TL.setAttrColumnOperand(Reader.readExpr());
6579}
6580
6581void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6582  TL.setLocalRangeBegin(readSourceLocation());
6583  TL.setLParenLoc(readSourceLocation());
6584  TL.setRParenLoc(readSourceLocation());
6585  TL.setExceptionSpecRange(Reader.readSourceRange());
6586  TL.setLocalRangeEnd(readSourceLocation());
6587  for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6588    TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6589  }
6590}
6591
6592void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6593  VisitFunctionTypeLoc(TL);
6594}
6595
6596void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6597  VisitFunctionTypeLoc(TL);
6598}
6599
6600void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6601  TL.setNameLoc(readSourceLocation());
6602}
6603
6604void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6605  TL.setNameLoc(readSourceLocation());
6606}
6607
6608void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6609  TL.setTypeofLoc(readSourceLocation());
6610  TL.setLParenLoc(readSourceLocation());
6611  TL.setRParenLoc(readSourceLocation());
6612}
6613
6614void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6615  TL.setTypeofLoc(readSourceLocation());
6616  TL.setLParenLoc(readSourceLocation());
6617  TL.setRParenLoc(readSourceLocation());
6618  TL.setUnderlyingTInfo(GetTypeSourceInfo());
6619}
6620
6621void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6622  TL.setNameLoc(readSourceLocation());
6623}
6624
6625void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6626  TL.setKWLoc(readSourceLocation());
6627  TL.setLParenLoc(readSourceLocation());
6628  TL.setRParenLoc(readSourceLocation());
6629  TL.setUnderlyingTInfo(GetTypeSourceInfo());
6630}
6631
6632void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6633  TL.setNameLoc(readSourceLocation());
6634  if (Reader.readBool()) {
6635    TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6636    TL.setTemplateKWLoc(readSourceLocation());
6637    TL.setConceptNameLoc(readSourceLocation());
6638    TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6639    TL.setLAngleLoc(readSourceLocation());
6640    TL.setRAngleLoc(readSourceLocation());
6641    for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6642      TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6643                              TL.getTypePtr()->getArg(i).getKind()));
6644  }
6645}
6646
6647void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6648    DeducedTemplateSpecializationTypeLoc TL) {
6649  TL.setTemplateNameLoc(readSourceLocation());
6650}
6651
6652void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6653  TL.setNameLoc(readSourceLocation());
6654}
6655
6656void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6657  TL.setNameLoc(readSourceLocation());
6658}
6659
6660void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6661  TL.setAttr(ReadAttr());
6662}
6663
6664void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6665  TL.setNameLoc(readSourceLocation());
6666}
6667
6668void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6669                                            SubstTemplateTypeParmTypeLoc TL) {
6670  TL.setNameLoc(readSourceLocation());
6671}
6672
6673void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6674                                          SubstTemplateTypeParmPackTypeLoc TL) {
6675  TL.setNameLoc(readSourceLocation());
6676}
6677
6678void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6679                                           TemplateSpecializationTypeLoc TL) {
6680  TL.setTemplateKeywordLoc(readSourceLocation());
6681  TL.setTemplateNameLoc(readSourceLocation());
6682  TL.setLAngleLoc(readSourceLocation());
6683  TL.setRAngleLoc(readSourceLocation());
6684  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6685    TL.setArgLocInfo(
6686        i,
6687        Reader.readTemplateArgumentLocInfo(
6688          TL.getTypePtr()->getArg(i).getKind()));
6689}
6690
6691void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6692  TL.setLParenLoc(readSourceLocation());
6693  TL.setRParenLoc(readSourceLocation());
6694}
6695
6696void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6697  TL.setElaboratedKeywordLoc(readSourceLocation());
6698  TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6699}
6700
6701void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6702  TL.setNameLoc(readSourceLocation());
6703}
6704
6705void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6706  TL.setElaboratedKeywordLoc(readSourceLocation());
6707  TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6708  TL.setNameLoc(readSourceLocation());
6709}
6710
6711void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6712       DependentTemplateSpecializationTypeLoc TL) {
6713  TL.setElaboratedKeywordLoc(readSourceLocation());
6714  TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6715  TL.setTemplateKeywordLoc(readSourceLocation());
6716  TL.setTemplateNameLoc(readSourceLocation());
6717  TL.setLAngleLoc(readSourceLocation());
6718  TL.setRAngleLoc(readSourceLocation());
6719  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6720    TL.setArgLocInfo(
6721        I,
6722        Reader.readTemplateArgumentLocInfo(
6723            TL.getTypePtr()->getArg(I).getKind()));
6724}
6725
6726void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6727  TL.setEllipsisLoc(readSourceLocation());
6728}
6729
6730void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6731  TL.setNameLoc(readSourceLocation());
6732}
6733
6734void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6735  if (TL.getNumProtocols()) {
6736    TL.setProtocolLAngleLoc(readSourceLocation());
6737    TL.setProtocolRAngleLoc(readSourceLocation());
6738  }
6739  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6740    TL.setProtocolLoc(i, readSourceLocation());
6741}
6742
6743void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6744  TL.setHasBaseTypeAsWritten(Reader.readBool());
6745  TL.setTypeArgsLAngleLoc(readSourceLocation());
6746  TL.setTypeArgsRAngleLoc(readSourceLocation());
6747  for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6748    TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6749  TL.setProtocolLAngleLoc(readSourceLocation());
6750  TL.setProtocolRAngleLoc(readSourceLocation());
6751  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6752    TL.setProtocolLoc(i, readSourceLocation());
6753}
6754
6755void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6756  TL.setStarLoc(readSourceLocation());
6757}
6758
6759void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6760  TL.setKWLoc(readSourceLocation());
6761  TL.setLParenLoc(readSourceLocation());
6762  TL.setRParenLoc(readSourceLocation());
6763}
6764
6765void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6766  TL.setKWLoc(readSourceLocation());
6767}
6768
6769void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6770  TL.setNameLoc(readSourceLocation());
6771}
6772void TypeLocReader::VisitDependentExtIntTypeLoc(
6773    clang::DependentExtIntTypeLoc TL) {
6774  TL.setNameLoc(readSourceLocation());
6775}
6776
6777
6778void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6779  TypeLocReader TLR(*this);
6780  for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6781    TLR.Visit(TL);
6782}
6783
6784TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6785  QualType InfoTy = readType();
6786  if (InfoTy.isNull())
6787    return nullptr;
6788
6789  TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6790  readTypeLoc(TInfo->getTypeLoc());
6791  return TInfo;
6792}
6793
6794QualType ASTReader::GetType(TypeID ID) {
6795  assert(ContextObj && "reading type with no AST context");
6796  ASTContext &Context = *ContextObj;
6797
6798  unsigned FastQuals = ID & Qualifiers::FastMask;
6799  unsigned Index = ID >> Qualifiers::FastWidth;
6800
6801  if (Index < NUM_PREDEF_TYPE_IDS) {
6802    QualType T;
6803    switch ((PredefinedTypeIDs)Index) {
6804    case PREDEF_TYPE_NULL_ID:
6805      return QualType();
6806    case PREDEF_TYPE_VOID_ID:
6807      T = Context.VoidTy;
6808      break;
6809    case PREDEF_TYPE_BOOL_ID:
6810      T = Context.BoolTy;
6811      break;
6812    case PREDEF_TYPE_CHAR_U_ID:
6813    case PREDEF_TYPE_CHAR_S_ID:
6814      // FIXME: Check that the signedness of CharTy is correct!
6815      T = Context.CharTy;
6816      break;
6817    case PREDEF_TYPE_UCHAR_ID:
6818      T = Context.UnsignedCharTy;
6819      break;
6820    case PREDEF_TYPE_USHORT_ID:
6821      T = Context.UnsignedShortTy;
6822      break;
6823    case PREDEF_TYPE_UINT_ID:
6824      T = Context.UnsignedIntTy;
6825      break;
6826    case PREDEF_TYPE_ULONG_ID:
6827      T = Context.UnsignedLongTy;
6828      break;
6829    case PREDEF_TYPE_ULONGLONG_ID:
6830      T = Context.UnsignedLongLongTy;
6831      break;
6832    case PREDEF_TYPE_UINT128_ID:
6833      T = Context.UnsignedInt128Ty;
6834      break;
6835    case PREDEF_TYPE_SCHAR_ID:
6836      T = Context.SignedCharTy;
6837      break;
6838    case PREDEF_TYPE_WCHAR_ID:
6839      T = Context.WCharTy;
6840      break;
6841    case PREDEF_TYPE_SHORT_ID:
6842      T = Context.ShortTy;
6843      break;
6844    case PREDEF_TYPE_INT_ID:
6845      T = Context.IntTy;
6846      break;
6847    case PREDEF_TYPE_LONG_ID:
6848      T = Context.LongTy;
6849      break;
6850    case PREDEF_TYPE_LONGLONG_ID:
6851      T = Context.LongLongTy;
6852      break;
6853    case PREDEF_TYPE_INT128_ID:
6854      T = Context.Int128Ty;
6855      break;
6856    case PREDEF_TYPE_BFLOAT16_ID:
6857      T = Context.BFloat16Ty;
6858      break;
6859    case PREDEF_TYPE_HALF_ID:
6860      T = Context.HalfTy;
6861      break;
6862    case PREDEF_TYPE_FLOAT_ID:
6863      T = Context.FloatTy;
6864      break;
6865    case PREDEF_TYPE_DOUBLE_ID:
6866      T = Context.DoubleTy;
6867      break;
6868    case PREDEF_TYPE_LONGDOUBLE_ID:
6869      T = Context.LongDoubleTy;
6870      break;
6871    case PREDEF_TYPE_SHORT_ACCUM_ID:
6872      T = Context.ShortAccumTy;
6873      break;
6874    case PREDEF_TYPE_ACCUM_ID:
6875      T = Context.AccumTy;
6876      break;
6877    case PREDEF_TYPE_LONG_ACCUM_ID:
6878      T = Context.LongAccumTy;
6879      break;
6880    case PREDEF_TYPE_USHORT_ACCUM_ID:
6881      T = Context.UnsignedShortAccumTy;
6882      break;
6883    case PREDEF_TYPE_UACCUM_ID:
6884      T = Context.UnsignedAccumTy;
6885      break;
6886    case PREDEF_TYPE_ULONG_ACCUM_ID:
6887      T = Context.UnsignedLongAccumTy;
6888      break;
6889    case PREDEF_TYPE_SHORT_FRACT_ID:
6890      T = Context.ShortFractTy;
6891      break;
6892    case PREDEF_TYPE_FRACT_ID:
6893      T = Context.FractTy;
6894      break;
6895    case PREDEF_TYPE_LONG_FRACT_ID:
6896      T = Context.LongFractTy;
6897      break;
6898    case PREDEF_TYPE_USHORT_FRACT_ID:
6899      T = Context.UnsignedShortFractTy;
6900      break;
6901    case PREDEF_TYPE_UFRACT_ID:
6902      T = Context.UnsignedFractTy;
6903      break;
6904    case PREDEF_TYPE_ULONG_FRACT_ID:
6905      T = Context.UnsignedLongFractTy;
6906      break;
6907    case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6908      T = Context.SatShortAccumTy;
6909      break;
6910    case PREDEF_TYPE_SAT_ACCUM_ID:
6911      T = Context.SatAccumTy;
6912      break;
6913    case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6914      T = Context.SatLongAccumTy;
6915      break;
6916    case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6917      T = Context.SatUnsignedShortAccumTy;
6918      break;
6919    case PREDEF_TYPE_SAT_UACCUM_ID:
6920      T = Context.SatUnsignedAccumTy;
6921      break;
6922    case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6923      T = Context.SatUnsignedLongAccumTy;
6924      break;
6925    case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6926      T = Context.SatShortFractTy;
6927      break;
6928    case PREDEF_TYPE_SAT_FRACT_ID:
6929      T = Context.SatFractTy;
6930      break;
6931    case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6932      T = Context.SatLongFractTy;
6933      break;
6934    case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6935      T = Context.SatUnsignedShortFractTy;
6936      break;
6937    case PREDEF_TYPE_SAT_UFRACT_ID:
6938      T = Context.SatUnsignedFractTy;
6939      break;
6940    case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6941      T = Context.SatUnsignedLongFractTy;
6942      break;
6943    case PREDEF_TYPE_FLOAT16_ID:
6944      T = Context.Float16Ty;
6945      break;
6946    case PREDEF_TYPE_FLOAT128_ID:
6947      T = Context.Float128Ty;
6948      break;
6949    case PREDEF_TYPE_OVERLOAD_ID:
6950      T = Context.OverloadTy;
6951      break;
6952    case PREDEF_TYPE_BOUND_MEMBER:
6953      T = Context.BoundMemberTy;
6954      break;
6955    case PREDEF_TYPE_PSEUDO_OBJECT:
6956      T = Context.PseudoObjectTy;
6957      break;
6958    case PREDEF_TYPE_DEPENDENT_ID:
6959      T = Context.DependentTy;
6960      break;
6961    case PREDEF_TYPE_UNKNOWN_ANY:
6962      T = Context.UnknownAnyTy;
6963      break;
6964    case PREDEF_TYPE_NULLPTR_ID:
6965      T = Context.NullPtrTy;
6966      break;
6967    case PREDEF_TYPE_CHAR8_ID:
6968      T = Context.Char8Ty;
6969      break;
6970    case PREDEF_TYPE_CHAR16_ID:
6971      T = Context.Char16Ty;
6972      break;
6973    case PREDEF_TYPE_CHAR32_ID:
6974      T = Context.Char32Ty;
6975      break;
6976    case PREDEF_TYPE_OBJC_ID:
6977      T = Context.ObjCBuiltinIdTy;
6978      break;
6979    case PREDEF_TYPE_OBJC_CLASS:
6980      T = Context.ObjCBuiltinClassTy;
6981      break;
6982    case PREDEF_TYPE_OBJC_SEL:
6983      T = Context.ObjCBuiltinSelTy;
6984      break;
6985#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6986    case PREDEF_TYPE_##Id##_ID: \
6987      T = Context.SingletonId; \
6988      break;
6989#include "clang/Basic/OpenCLImageTypes.def"
6990#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6991    case PREDEF_TYPE_##Id##_ID: \
6992      T = Context.Id##Ty; \
6993      break;
6994#include "clang/Basic/OpenCLExtensionTypes.def"
6995    case PREDEF_TYPE_SAMPLER_ID:
6996      T = Context.OCLSamplerTy;
6997      break;
6998    case PREDEF_TYPE_EVENT_ID:
6999      T = Context.OCLEventTy;
7000      break;
7001    case PREDEF_TYPE_CLK_EVENT_ID:
7002      T = Context.OCLClkEventTy;
7003      break;
7004    case PREDEF_TYPE_QUEUE_ID:
7005      T = Context.OCLQueueTy;
7006      break;
7007    case PREDEF_TYPE_RESERVE_ID_ID:
7008      T = Context.OCLReserveIDTy;
7009      break;
7010    case PREDEF_TYPE_AUTO_DEDUCT:
7011      T = Context.getAutoDeductType();
7012      break;
7013    case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7014      T = Context.getAutoRRefDeductType();
7015      break;
7016    case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7017      T = Context.ARCUnbridgedCastTy;
7018      break;
7019    case PREDEF_TYPE_BUILTIN_FN:
7020      T = Context.BuiltinFnTy;
7021      break;
7022    case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7023      T = Context.IncompleteMatrixIdxTy;
7024      break;
7025    case PREDEF_TYPE_OMP_ARRAY_SECTION:
7026      T = Context.OMPArraySectionTy;
7027      break;
7028    case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7029      T = Context.OMPArraySectionTy;
7030      break;
7031    case PREDEF_TYPE_OMP_ITERATOR:
7032      T = Context.OMPIteratorTy;
7033      break;
7034#define SVE_TYPE(Name, Id, SingletonId) \
7035    case PREDEF_TYPE_##Id##_ID: \
7036      T = Context.SingletonId; \
7037      break;
7038#include "clang/Basic/AArch64SVEACLETypes.def"
7039    }
7040
7041    assert(!T.isNull() && "Unknown predefined type");
7042    return T.withFastQualifiers(FastQuals);
7043  }
7044
7045  Index -= NUM_PREDEF_TYPE_IDS;
7046  assert(Index < TypesLoaded.size() && "Type index out-of-range");
7047  if (TypesLoaded[Index].isNull()) {
7048    TypesLoaded[Index] = readTypeRecord(Index);
7049    if (TypesLoaded[Index].isNull())
7050      return QualType();
7051
7052    TypesLoaded[Index]->setFromAST();
7053    if (DeserializationListener)
7054      DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7055                                        TypesLoaded[Index]);
7056  }
7057
7058  return TypesLoaded[Index].withFastQualifiers(FastQuals);
7059}
7060
7061QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7062  return GetType(getGlobalTypeID(F, LocalID));
7063}
7064
7065serialization::TypeID
7066ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7067  unsigned FastQuals = LocalID & Qualifiers::FastMask;
7068  unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7069
7070  if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7071    return LocalID;
7072
7073  if (!F.ModuleOffsetMap.empty())
7074    ReadModuleOffsetMap(F);
7075
7076  ContinuousRangeMap<uint32_t, int, 2>::iterator I
7077    = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7078  assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7079
7080  unsigned GlobalIndex = LocalIndex + I->second;
7081  return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7082}
7083
7084TemplateArgumentLocInfo
7085ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7086  switch (Kind) {
7087  case TemplateArgument::Expression:
7088    return readExpr();
7089  case TemplateArgument::Type:
7090    return readTypeSourceInfo();
7091  case TemplateArgument::Template: {
7092    NestedNameSpecifierLoc QualifierLoc =
7093      readNestedNameSpecifierLoc();
7094    SourceLocation TemplateNameLoc = readSourceLocation();
7095    return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7096                                   SourceLocation());
7097  }
7098  case TemplateArgument::TemplateExpansion: {
7099    NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7100    SourceLocation TemplateNameLoc = readSourceLocation();
7101    SourceLocation EllipsisLoc = readSourceLocation();
7102    return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7103                                   EllipsisLoc);
7104  }
7105  case TemplateArgument::Null:
7106  case TemplateArgument::Integral:
7107  case TemplateArgument::Declaration:
7108  case TemplateArgument::NullPtr:
7109  case TemplateArgument::Pack:
7110    // FIXME: Is this right?
7111    return TemplateArgumentLocInfo();
7112  }
7113  llvm_unreachable("unexpected template argument loc");
7114}
7115
7116TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7117  TemplateArgument Arg = readTemplateArgument();
7118
7119  if (Arg.getKind() == TemplateArgument::Expression) {
7120    if (readBool()) // bool InfoHasSameExpr.
7121      return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7122  }
7123  return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7124}
7125
7126const ASTTemplateArgumentListInfo *
7127ASTRecordReader::readASTTemplateArgumentListInfo() {
7128  SourceLocation LAngleLoc = readSourceLocation();
7129  SourceLocation RAngleLoc = readSourceLocation();
7130  unsigned NumArgsAsWritten = readInt();
7131  TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7132  for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7133    TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7134  return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7135}
7136
7137Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7138  return GetDecl(ID);
7139}
7140
7141void ASTReader::CompleteRedeclChain(const Decl *D) {
7142  if (NumCurrentElementsDeserializing) {
7143    // We arrange to not care about the complete redeclaration chain while we're
7144    // deserializing. Just remember that the AST has marked this one as complete
7145    // but that it's not actually complete yet, so we know we still need to
7146    // complete it later.
7147    PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7148    return;
7149  }
7150
7151  const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7152
7153  // If this is a named declaration, complete it by looking it up
7154  // within its context.
7155  //
7156  // FIXME: Merging a function definition should merge
7157  // all mergeable entities within it.
7158  if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7159      isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7160    if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7161      if (!getContext().getLangOpts().CPlusPlus &&
7162          isa<TranslationUnitDecl>(DC)) {
7163        // Outside of C++, we don't have a lookup table for the TU, so update
7164        // the identifier instead. (For C++ modules, we don't store decls
7165        // in the serialized identifier table, so we do the lookup in the TU.)
7166        auto *II = Name.getAsIdentifierInfo();
7167        assert(II && "non-identifier name in C?");
7168        if (II->isOutOfDate())
7169          updateOutOfDateIdentifier(*II);
7170      } else
7171        DC->lookup(Name);
7172    } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7173      // Find all declarations of this kind from the relevant context.
7174      for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7175        auto *DC = cast<DeclContext>(DCDecl);
7176        SmallVector<Decl*, 8> Decls;
7177        FindExternalLexicalDecls(
7178            DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7179      }
7180    }
7181  }
7182
7183  if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7184    CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7185  if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7186    VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7187  if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7188    if (auto *Template = FD->getPrimaryTemplate())
7189      Template->LoadLazySpecializations();
7190  }
7191}
7192
7193CXXCtorInitializer **
7194ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7195  RecordLocation Loc = getLocalBitOffset(Offset);
7196  BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7197  SavedStreamPosition SavedPosition(Cursor);
7198  if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7199    Error(std::move(Err));
7200    return nullptr;
7201  }
7202  ReadingKindTracker ReadingKind(Read_Decl, *this);
7203
7204  Expected<unsigned> MaybeCode = Cursor.ReadCode();
7205  if (!MaybeCode) {
7206    Error(MaybeCode.takeError());
7207    return nullptr;
7208  }
7209  unsigned Code = MaybeCode.get();
7210
7211  ASTRecordReader Record(*this, *Loc.F);
7212  Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7213  if (!MaybeRecCode) {
7214    Error(MaybeRecCode.takeError());
7215    return nullptr;
7216  }
7217  if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7218    Error("malformed AST file: missing C++ ctor initializers");
7219    return nullptr;
7220  }
7221
7222  return Record.readCXXCtorInitializers();
7223}
7224
7225CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7226  assert(ContextObj && "reading base specifiers with no AST context");
7227  ASTContext &Context = *ContextObj;
7228
7229  RecordLocation Loc = getLocalBitOffset(Offset);
7230  BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7231  SavedStreamPosition SavedPosition(Cursor);
7232  if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7233    Error(std::move(Err));
7234    return nullptr;
7235  }
7236  ReadingKindTracker ReadingKind(Read_Decl, *this);
7237
7238  Expected<unsigned> MaybeCode = Cursor.ReadCode();
7239  if (!MaybeCode) {
7240    Error(MaybeCode.takeError());
7241    return nullptr;
7242  }
7243  unsigned Code = MaybeCode.get();
7244
7245  ASTRecordReader Record(*this, *Loc.F);
7246  Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7247  if (!MaybeRecCode) {
7248    Error(MaybeCode.takeError());
7249    return nullptr;
7250  }
7251  unsigned RecCode = MaybeRecCode.get();
7252
7253  if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7254    Error("malformed AST file: missing C++ base specifiers");
7255    return nullptr;
7256  }
7257
7258  unsigned NumBases = Record.readInt();
7259  void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7260  CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7261  for (unsigned I = 0; I != NumBases; ++I)
7262    Bases[I] = Record.readCXXBaseSpecifier();
7263  return Bases;
7264}
7265
7266serialization::DeclID
7267ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7268  if (LocalID < NUM_PREDEF_DECL_IDS)
7269    return LocalID;
7270
7271  if (!F.ModuleOffsetMap.empty())
7272    ReadModuleOffsetMap(F);
7273
7274  ContinuousRangeMap<uint32_t, int, 2>::iterator I
7275    = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7276  assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7277
7278  return LocalID + I->second;
7279}
7280
7281bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7282                                   ModuleFile &M) const {
7283  // Predefined decls aren't from any module.
7284  if (ID < NUM_PREDEF_DECL_IDS)
7285    return false;
7286
7287  return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7288         ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7289}
7290
7291ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7292  if (!D->isFromASTFile())
7293    return nullptr;
7294  GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7295  assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7296  return I->second;
7297}
7298
7299SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7300  if (ID < NUM_PREDEF_DECL_IDS)
7301    return SourceLocation();
7302
7303  unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7304
7305  if (Index > DeclsLoaded.size()) {
7306    Error("declaration ID out-of-range for AST file");
7307    return SourceLocation();
7308  }
7309
7310  if (Decl *D = DeclsLoaded[Index])
7311    return D->getLocation();
7312
7313  SourceLocation Loc;
7314  DeclCursorForID(ID, Loc);
7315  return Loc;
7316}
7317
7318static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7319  switch (ID) {
7320  case PREDEF_DECL_NULL_ID:
7321    return nullptr;
7322
7323  case PREDEF_DECL_TRANSLATION_UNIT_ID:
7324    return Context.getTranslationUnitDecl();
7325
7326  case PREDEF_DECL_OBJC_ID_ID:
7327    return Context.getObjCIdDecl();
7328
7329  case PREDEF_DECL_OBJC_SEL_ID:
7330    return Context.getObjCSelDecl();
7331
7332  case PREDEF_DECL_OBJC_CLASS_ID:
7333    return Context.getObjCClassDecl();
7334
7335  case PREDEF_DECL_OBJC_PROTOCOL_ID:
7336    return Context.getObjCProtocolDecl();
7337
7338  case PREDEF_DECL_INT_128_ID:
7339    return Context.getInt128Decl();
7340
7341  case PREDEF_DECL_UNSIGNED_INT_128_ID:
7342    return Context.getUInt128Decl();
7343
7344  case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7345    return Context.getObjCInstanceTypeDecl();
7346
7347  case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7348    return Context.getBuiltinVaListDecl();
7349
7350  case PREDEF_DECL_VA_LIST_TAG:
7351    return Context.getVaListTagDecl();
7352
7353  case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7354    return Context.getBuiltinMSVaListDecl();
7355
7356  case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7357    return Context.getMSGuidTagDecl();
7358
7359  case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7360    return Context.getExternCContextDecl();
7361
7362  case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7363    return Context.getMakeIntegerSeqDecl();
7364
7365  case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7366    return Context.getCFConstantStringDecl();
7367
7368  case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7369    return Context.getCFConstantStringTagDecl();
7370
7371  case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7372    return Context.getTypePackElementDecl();
7373  }
7374  llvm_unreachable("PredefinedDeclIDs unknown enum value");
7375}
7376
7377Decl *ASTReader::GetExistingDecl(DeclID ID) {
7378  assert(ContextObj && "reading decl with no AST context");
7379  if (ID < NUM_PREDEF_DECL_IDS) {
7380    Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7381    if (D) {
7382      // Track that we have merged the declaration with ID \p ID into the
7383      // pre-existing predefined declaration \p D.
7384      auto &Merged = KeyDecls[D->getCanonicalDecl()];
7385      if (Merged.empty())
7386        Merged.push_back(ID);
7387    }
7388    return D;
7389  }
7390
7391  unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7392
7393  if (Index >= DeclsLoaded.size()) {
7394    assert(0 && "declaration ID out-of-range for AST file");
7395    Error("declaration ID out-of-range for AST file");
7396    return nullptr;
7397  }
7398
7399  return DeclsLoaded[Index];
7400}
7401
7402Decl *ASTReader::GetDecl(DeclID ID) {
7403  if (ID < NUM_PREDEF_DECL_IDS)
7404    return GetExistingDecl(ID);
7405
7406  unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7407
7408  if (Index >= DeclsLoaded.size()) {
7409    assert(0 && "declaration ID out-of-range for AST file");
7410    Error("declaration ID out-of-range for AST file");
7411    return nullptr;
7412  }
7413
7414  if (!DeclsLoaded[Index]) {
7415    ReadDeclRecord(ID);
7416    if (DeserializationListener)
7417      DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7418  }
7419
7420  return DeclsLoaded[Index];
7421}
7422
7423DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7424                                                  DeclID GlobalID) {
7425  if (GlobalID < NUM_PREDEF_DECL_IDS)
7426    return GlobalID;
7427
7428  GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7429  assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7430  ModuleFile *Owner = I->second;
7431
7432  llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7433    = M.GlobalToLocalDeclIDs.find(Owner);
7434  if (Pos == M.GlobalToLocalDeclIDs.end())
7435    return 0;
7436
7437  return GlobalID - Owner->BaseDeclID + Pos->second;
7438}
7439
7440serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7441                                            const RecordData &Record,
7442                                            unsigned &Idx) {
7443  if (Idx >= Record.size()) {
7444    Error("Corrupted AST file");
7445    return 0;
7446  }
7447
7448  return getGlobalDeclID(F, Record[Idx++]);
7449}
7450
7451/// Resolve the offset of a statement into a statement.
7452///
7453/// This operation will read a new statement from the external
7454/// source each time it is called, and is meant to be used via a
7455/// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7456Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7457  // Switch case IDs are per Decl.
7458  ClearSwitchCaseIDs();
7459
7460  // Offset here is a global offset across the entire chain.
7461  RecordLocation Loc = getLocalBitOffset(Offset);
7462  if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7463    Error(std::move(Err));
7464    return nullptr;
7465  }
7466  assert(NumCurrentElementsDeserializing == 0 &&
7467         "should not be called while already deserializing");
7468  Deserializing D(this);
7469  return ReadStmtFromStream(*Loc.F);
7470}
7471
7472void ASTReader::FindExternalLexicalDecls(
7473    const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7474    SmallVectorImpl<Decl *> &Decls) {
7475  bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7476
7477  auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7478    assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7479    for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7480      auto K = (Decl::Kind)+LexicalDecls[I];
7481      if (!IsKindWeWant(K))
7482        continue;
7483
7484      auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7485
7486      // Don't add predefined declarations to the lexical context more
7487      // than once.
7488      if (ID < NUM_PREDEF_DECL_IDS) {
7489        if (PredefsVisited[ID])
7490          continue;
7491
7492        PredefsVisited[ID] = true;
7493      }
7494
7495      if (Decl *D = GetLocalDecl(*M, ID)) {
7496        assert(D->getKind() == K && "wrong kind for lexical decl");
7497        if (!DC->isDeclInLexicalTraversal(D))
7498          Decls.push_back(D);
7499      }
7500    }
7501  };
7502
7503  if (isa<TranslationUnitDecl>(DC)) {
7504    for (auto Lexical : TULexicalDecls)
7505      Visit(Lexical.first, Lexical.second);
7506  } else {
7507    auto I = LexicalDecls.find(DC);
7508    if (I != LexicalDecls.end())
7509      Visit(I->second.first, I->second.second);
7510  }
7511
7512  ++NumLexicalDeclContextsRead;
7513}
7514
7515namespace {
7516
7517class DeclIDComp {
7518  ASTReader &Reader;
7519  ModuleFile &Mod;
7520
7521public:
7522  DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7523
7524  bool operator()(LocalDeclID L, LocalDeclID R) const {
7525    SourceLocation LHS = getLocation(L);
7526    SourceLocation RHS = getLocation(R);
7527    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7528  }
7529
7530  bool operator()(SourceLocation LHS, LocalDeclID R) const {
7531    SourceLocation RHS = getLocation(R);
7532    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7533  }
7534
7535  bool operator()(LocalDeclID L, SourceLocation RHS) const {
7536    SourceLocation LHS = getLocation(L);
7537    return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7538  }
7539
7540  SourceLocation getLocation(LocalDeclID ID) const {
7541    return Reader.getSourceManager().getFileLoc(
7542            Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7543  }
7544};
7545
7546} // namespace
7547
7548void ASTReader::FindFileRegionDecls(FileID File,
7549                                    unsigned Offset, unsigned Length,
7550                                    SmallVectorImpl<Decl *> &Decls) {
7551  SourceManager &SM = getSourceManager();
7552
7553  llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7554  if (I == FileDeclIDs.end())
7555    return;
7556
7557  FileDeclsInfo &DInfo = I->second;
7558  if (DInfo.Decls.empty())
7559    return;
7560
7561  SourceLocation
7562    BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7563  SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7564
7565  DeclIDComp DIDComp(*this, *DInfo.Mod);
7566  ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7567      llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7568  if (BeginIt != DInfo.Decls.begin())
7569    --BeginIt;
7570
7571  // If we are pointing at a top-level decl inside an objc container, we need
7572  // to backtrack until we find it otherwise we will fail to report that the
7573  // region overlaps with an objc container.
7574  while (BeginIt != DInfo.Decls.begin() &&
7575         GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7576             ->isTopLevelDeclInObjCContainer())
7577    --BeginIt;
7578
7579  ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7580      llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7581  if (EndIt != DInfo.Decls.end())
7582    ++EndIt;
7583
7584  for (ArrayRef<serialization::LocalDeclID>::iterator
7585         DIt = BeginIt; DIt != EndIt; ++DIt)
7586    Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7587}
7588
7589bool
7590ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7591                                          DeclarationName Name) {
7592  assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7593         "DeclContext has no visible decls in storage");
7594  if (!Name)
7595    return false;
7596
7597  auto It = Lookups.find(DC);
7598  if (It == Lookups.end())
7599    return false;
7600
7601  Deserializing LookupResults(this);
7602
7603  // Load the list of declarations.
7604  SmallVector<NamedDecl *, 64> Decls;
7605  for (DeclID ID : It->second.Table.find(Name)) {
7606    NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7607    if (ND->getDeclName() == Name)
7608      Decls.push_back(ND);
7609  }
7610
7611  ++NumVisibleDeclContextsRead;
7612  SetExternalVisibleDeclsForName(DC, Name, Decls);
7613  return !Decls.empty();
7614}
7615
7616void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7617  if (!DC->hasExternalVisibleStorage())
7618    return;
7619
7620  auto It = Lookups.find(DC);
7621  assert(It != Lookups.end() &&
7622         "have external visible storage but no lookup tables");
7623
7624  DeclsMap Decls;
7625
7626  for (DeclID ID : It->second.Table.findAll()) {
7627    NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7628    Decls[ND->getDeclName()].push_back(ND);
7629  }
7630
7631  ++NumVisibleDeclContextsRead;
7632
7633  for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7634    SetExternalVisibleDeclsForName(DC, I->first, I->second);
7635  }
7636  const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7637}
7638
7639const serialization::reader::DeclContextLookupTable *
7640ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7641  auto I = Lookups.find(Primary);
7642  return I == Lookups.end() ? nullptr : &I->second;
7643}
7644
7645/// Under non-PCH compilation the consumer receives the objc methods
7646/// before receiving the implementation, and codegen depends on this.
7647/// We simulate this by deserializing and passing to consumer the methods of the
7648/// implementation before passing the deserialized implementation decl.
7649static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7650                                       ASTConsumer *Consumer) {
7651  assert(ImplD && Consumer);
7652
7653  for (auto *I : ImplD->methods())
7654    Consumer->HandleInterestingDecl(DeclGroupRef(I));
7655
7656  Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7657}
7658
7659void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7660  if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7661    PassObjCImplDeclToConsumer(ImplD, Consumer);
7662  else
7663    Consumer->HandleInterestingDecl(DeclGroupRef(D));
7664}
7665
7666void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7667  this->Consumer = Consumer;
7668
7669  if (Consumer)
7670    PassInterestingDeclsToConsumer();
7671
7672  if (DeserializationListener)
7673    DeserializationListener->ReaderInitialized(this);
7674}
7675
7676void ASTReader::PrintStats() {
7677  std::fprintf(stderr, "*** AST File Statistics:\n");
7678
7679  unsigned NumTypesLoaded
7680    = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7681                                      QualType());
7682  unsigned NumDeclsLoaded
7683    = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7684                                      (Decl *)nullptr);
7685  unsigned NumIdentifiersLoaded
7686    = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7687                                            IdentifiersLoaded.end(),
7688                                            (IdentifierInfo *)nullptr);
7689  unsigned NumMacrosLoaded
7690    = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7691                                       MacrosLoaded.end(),
7692                                       (MacroInfo *)nullptr);
7693  unsigned NumSelectorsLoaded
7694    = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7695                                          SelectorsLoaded.end(),
7696                                          Selector());
7697
7698  if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7699    std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7700                 NumSLocEntriesRead, TotalNumSLocEntries,
7701                 ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7702  if (!TypesLoaded.empty())
7703    std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7704                 NumTypesLoaded, (unsigned)TypesLoaded.size(),
7705                 ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7706  if (!DeclsLoaded.empty())
7707    std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7708                 NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7709                 ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7710  if (!IdentifiersLoaded.empty())
7711    std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7712                 NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7713                 ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7714  if (!MacrosLoaded.empty())
7715    std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7716                 NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7717                 ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7718  if (!SelectorsLoaded.empty())
7719    std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7720                 NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7721                 ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7722  if (TotalNumStatements)
7723    std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7724                 NumStatementsRead, TotalNumStatements,
7725                 ((float)NumStatementsRead/TotalNumStatements * 100));
7726  if (TotalNumMacros)
7727    std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7728                 NumMacrosRead, TotalNumMacros,
7729                 ((float)NumMacrosRead/TotalNumMacros * 100));
7730  if (TotalLexicalDeclContexts)
7731    std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7732                 NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7733                 ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7734                  * 100));
7735  if (TotalVisibleDeclContexts)
7736    std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7737                 NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7738                 ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7739                  * 100));
7740  if (TotalNumMethodPoolEntries)
7741    std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7742                 NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7743                 ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7744                  * 100));
7745  if (NumMethodPoolLookups)
7746    std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7747                 NumMethodPoolHits, NumMethodPoolLookups,
7748                 ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7749  if (NumMethodPoolTableLookups)
7750    std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7751                 NumMethodPoolTableHits, NumMethodPoolTableLookups,
7752                 ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7753                  * 100.0));
7754  if (NumIdentifierLookupHits)
7755    std::fprintf(stderr,
7756                 "  %u / %u identifier table lookups succeeded (%f%%)\n",
7757                 NumIdentifierLookupHits, NumIdentifierLookups,
7758                 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7759
7760  if (GlobalIndex) {
7761    std::fprintf(stderr, "\n");
7762    GlobalIndex->printStats();
7763  }
7764
7765  std::fprintf(stderr, "\n");
7766  dump();
7767  std::fprintf(stderr, "\n");
7768}
7769
7770template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7771LLVM_DUMP_METHOD static void
7772dumpModuleIDMap(StringRef Name,
7773                const ContinuousRangeMap<Key, ModuleFile *,
7774                                         InitialCapacity> &Map) {
7775  if (Map.begin() == Map.end())
7776    return;
7777
7778  using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7779
7780  llvm::errs() << Name << ":\n";
7781  for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7782       I != IEnd; ++I) {
7783    llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7784      << "\n";
7785  }
7786}
7787
7788LLVM_DUMP_METHOD void ASTReader::dump() {
7789  llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7790  dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7791  dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7792  dumpModuleIDMap("Global type map", GlobalTypeMap);
7793  dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7794  dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7795  dumpModuleIDMap("Global macro map", GlobalMacroMap);
7796  dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7797  dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7798  dumpModuleIDMap("Global preprocessed entity map",
7799                  GlobalPreprocessedEntityMap);
7800
7801  llvm::errs() << "\n*** PCH/Modules Loaded:";
7802  for (ModuleFile &M : ModuleMgr)
7803    M.dump();
7804}
7805
7806/// Return the amount of memory used by memory buffers, breaking down
7807/// by heap-backed versus mmap'ed memory.
7808void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7809  for (ModuleFile &I : ModuleMgr) {
7810    if (llvm::MemoryBuffer *buf = I.Buffer) {
7811      size_t bytes = buf->getBufferSize();
7812      switch (buf->getBufferKind()) {
7813        case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7814          sizes.malloc_bytes += bytes;
7815          break;
7816        case llvm::MemoryBuffer::MemoryBuffer_MMap:
7817          sizes.mmap_bytes += bytes;
7818          break;
7819      }
7820    }
7821  }
7822}
7823
7824void ASTReader::InitializeSema(Sema &S) {
7825  SemaObj = &S;
7826  S.addExternalSource(this);
7827
7828  // Makes sure any declarations that were deserialized "too early"
7829  // still get added to the identifier's declaration chains.
7830  for (uint64_t ID : PreloadedDeclIDs) {
7831    NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7832    pushExternalDeclIntoScope(D, D->getDeclName());
7833  }
7834  PreloadedDeclIDs.clear();
7835
7836  // FIXME: What happens if these are changed by a module import?
7837  if (!FPPragmaOptions.empty()) {
7838    assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7839    FPOptionsOverride NewOverrides(FPPragmaOptions[0]);
7840    SemaObj->CurFPFeatures =
7841        NewOverrides.applyOverrides(SemaObj->getLangOpts());
7842  }
7843
7844  SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7845  SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7846  SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7847
7848  UpdateSema();
7849}
7850
7851void ASTReader::UpdateSema() {
7852  assert(SemaObj && "no Sema to update");
7853
7854  // Load the offsets of the declarations that Sema references.
7855  // They will be lazily deserialized when needed.
7856  if (!SemaDeclRefs.empty()) {
7857    assert(SemaDeclRefs.size() % 3 == 0);
7858    for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7859      if (!SemaObj->StdNamespace)
7860        SemaObj->StdNamespace = SemaDeclRefs[I];
7861      if (!SemaObj->StdBadAlloc)
7862        SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7863      if (!SemaObj->StdAlignValT)
7864        SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7865    }
7866    SemaDeclRefs.clear();
7867  }
7868
7869  // Update the state of pragmas. Use the same API as if we had encountered the
7870  // pragma in the source.
7871  if(OptimizeOffPragmaLocation.isValid())
7872    SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7873  if (PragmaMSStructState != -1)
7874    SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7875  if (PointersToMembersPragmaLocation.isValid()) {
7876    SemaObj->ActOnPragmaMSPointersToMembers(
7877        (LangOptions::PragmaMSPointersToMembersKind)
7878            PragmaMSPointersToMembersState,
7879        PointersToMembersPragmaLocation);
7880  }
7881  SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7882
7883  if (PragmaPackCurrentValue) {
7884    // The bottom of the stack might have a default value. It must be adjusted
7885    // to the current value to ensure that the packing state is preserved after
7886    // popping entries that were included/imported from a PCH/module.
7887    bool DropFirst = false;
7888    if (!PragmaPackStack.empty() &&
7889        PragmaPackStack.front().Location.isInvalid()) {
7890      assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7891             "Expected a default alignment value");
7892      SemaObj->PackStack.Stack.emplace_back(
7893          PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7894          SemaObj->PackStack.CurrentPragmaLocation,
7895          PragmaPackStack.front().PushLocation);
7896      DropFirst = true;
7897    }
7898    for (const auto &Entry :
7899         llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7900      SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7901                                            Entry.Location, Entry.PushLocation);
7902    if (PragmaPackCurrentLocation.isInvalid()) {
7903      assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7904             "Expected a default alignment value");
7905      // Keep the current values.
7906    } else {
7907      SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7908      SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7909    }
7910  }
7911  if (FpPragmaCurrentValue) {
7912    // The bottom of the stack might have a default value. It must be adjusted
7913    // to the current value to ensure that fp-pragma state is preserved after
7914    // popping entries that were included/imported from a PCH/module.
7915    bool DropFirst = false;
7916    if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7917      assert(FpPragmaStack.front().Value ==
7918                 SemaObj->FpPragmaStack.DefaultValue &&
7919             "Expected a default pragma float_control value");
7920      SemaObj->FpPragmaStack.Stack.emplace_back(
7921          FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7922          SemaObj->FpPragmaStack.CurrentPragmaLocation,
7923          FpPragmaStack.front().PushLocation);
7924      DropFirst = true;
7925    }
7926    for (const auto &Entry :
7927         llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7928      SemaObj->FpPragmaStack.Stack.emplace_back(
7929          Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7930    if (FpPragmaCurrentLocation.isInvalid()) {
7931      assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7932             "Expected a default pragma float_control value");
7933      // Keep the current values.
7934    } else {
7935      SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7936      SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7937    }
7938  }
7939}
7940
7941IdentifierInfo *ASTReader::get(StringRef Name) {
7942  // Note that we are loading an identifier.
7943  Deserializing AnIdentifier(this);
7944
7945  IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7946                                  NumIdentifierLookups,
7947                                  NumIdentifierLookupHits);
7948
7949  // We don't need to do identifier table lookups in C++ modules (we preload
7950  // all interesting declarations, and don't need to use the scope for name
7951  // lookups). Perform the lookup in PCH files, though, since we don't build
7952  // a complete initial identifier table if we're carrying on from a PCH.
7953  if (PP.getLangOpts().CPlusPlus) {
7954    for (auto F : ModuleMgr.pch_modules())
7955      if (Visitor(*F))
7956        break;
7957  } else {
7958    // If there is a global index, look there first to determine which modules
7959    // provably do not have any results for this identifier.
7960    GlobalModuleIndex::HitSet Hits;
7961    GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7962    if (!loadGlobalIndex()) {
7963      if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7964        HitsPtr = &Hits;
7965      }
7966    }
7967
7968    ModuleMgr.visit(Visitor, HitsPtr);
7969  }
7970
7971  IdentifierInfo *II = Visitor.getIdentifierInfo();
7972  markIdentifierUpToDate(II);
7973  return II;
7974}
7975
7976namespace clang {
7977
7978  /// An identifier-lookup iterator that enumerates all of the
7979  /// identifiers stored within a set of AST files.
7980  class ASTIdentifierIterator : public IdentifierIterator {
7981    /// The AST reader whose identifiers are being enumerated.
7982    const ASTReader &Reader;
7983
7984    /// The current index into the chain of AST files stored in
7985    /// the AST reader.
7986    unsigned Index;
7987
7988    /// The current position within the identifier lookup table
7989    /// of the current AST file.
7990    ASTIdentifierLookupTable::key_iterator Current;
7991
7992    /// The end position within the identifier lookup table of
7993    /// the current AST file.
7994    ASTIdentifierLookupTable::key_iterator End;
7995
7996    /// Whether to skip any modules in the ASTReader.
7997    bool SkipModules;
7998
7999  public:
8000    explicit ASTIdentifierIterator(const ASTReader &Reader,
8001                                   bool SkipModules = false);
8002
8003    StringRef Next() override;
8004  };
8005
8006} // namespace clang
8007
8008ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8009                                             bool SkipModules)
8010    : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8011}
8012
8013StringRef ASTIdentifierIterator::Next() {
8014  while (Current == End) {
8015    // If we have exhausted all of our AST files, we're done.
8016    if (Index == 0)
8017      return StringRef();
8018
8019    --Index;
8020    ModuleFile &F = Reader.ModuleMgr[Index];
8021    if (SkipModules && F.isModule())
8022      continue;
8023
8024    ASTIdentifierLookupTable *IdTable =
8025        (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8026    Current = IdTable->key_begin();
8027    End = IdTable->key_end();
8028  }
8029
8030  // We have any identifiers remaining in the current AST file; return
8031  // the next one.
8032  StringRef Result = *Current;
8033  ++Current;
8034  return Result;
8035}
8036
8037namespace {
8038
8039/// A utility for appending two IdentifierIterators.
8040class ChainedIdentifierIterator : public IdentifierIterator {
8041  std::unique_ptr<IdentifierIterator> Current;
8042  std::unique_ptr<IdentifierIterator> Queued;
8043
8044public:
8045  ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8046                            std::unique_ptr<IdentifierIterator> Second)
8047      : Current(std::move(First)), Queued(std::move(Second)) {}
8048
8049  StringRef Next() override {
8050    if (!Current)
8051      return StringRef();
8052
8053    StringRef result = Current->Next();
8054    if (!result.empty())
8055      return result;
8056
8057    // Try the queued iterator, which may itself be empty.
8058    Current.reset();
8059    std::swap(Current, Queued);
8060    return Next();
8061  }
8062};
8063
8064} // namespace
8065
8066IdentifierIterator *ASTReader::getIdentifiers() {
8067  if (!loadGlobalIndex()) {
8068    std::unique_ptr<IdentifierIterator> ReaderIter(
8069        new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8070    std::unique_ptr<IdentifierIterator> ModulesIter(
8071        GlobalIndex->createIdentifierIterator());
8072    return new ChainedIdentifierIterator(std::move(ReaderIter),
8073                                         std::move(ModulesIter));
8074  }
8075
8076  return new ASTIdentifierIterator(*this);
8077}
8078
8079namespace clang {
8080namespace serialization {
8081
8082  class ReadMethodPoolVisitor {
8083    ASTReader &Reader;
8084    Selector Sel;
8085    unsigned PriorGeneration;
8086    unsigned InstanceBits = 0;
8087    unsigned FactoryBits = 0;
8088    bool InstanceHasMoreThanOneDecl = false;
8089    bool FactoryHasMoreThanOneDecl = false;
8090    SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8091    SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8092
8093  public:
8094    ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8095                          unsigned PriorGeneration)
8096        : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8097
8098    bool operator()(ModuleFile &M) {
8099      if (!M.SelectorLookupTable)
8100        return false;
8101
8102      // If we've already searched this module file, skip it now.
8103      if (M.Generation <= PriorGeneration)
8104        return true;
8105
8106      ++Reader.NumMethodPoolTableLookups;
8107      ASTSelectorLookupTable *PoolTable
8108        = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8109      ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8110      if (Pos == PoolTable->end())
8111        return false;
8112
8113      ++Reader.NumMethodPoolTableHits;
8114      ++Reader.NumSelectorsRead;
8115      // FIXME: Not quite happy with the statistics here. We probably should
8116      // disable this tracking when called via LoadSelector.
8117      // Also, should entries without methods count as misses?
8118      ++Reader.NumMethodPoolEntriesRead;
8119      ASTSelectorLookupTrait::data_type Data = *Pos;
8120      if (Reader.DeserializationListener)
8121        Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8122
8123      InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8124      FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8125      InstanceBits = Data.InstanceBits;
8126      FactoryBits = Data.FactoryBits;
8127      InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8128      FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8129      return true;
8130    }
8131
8132    /// Retrieve the instance methods found by this visitor.
8133    ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8134      return InstanceMethods;
8135    }
8136
8137    /// Retrieve the instance methods found by this visitor.
8138    ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8139      return FactoryMethods;
8140    }
8141
8142    unsigned getInstanceBits() const { return InstanceBits; }
8143    unsigned getFactoryBits() const { return FactoryBits; }
8144
8145    bool instanceHasMoreThanOneDecl() const {
8146      return InstanceHasMoreThanOneDecl;
8147    }
8148
8149    bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8150  };
8151
8152} // namespace serialization
8153} // namespace clang
8154
8155/// Add the given set of methods to the method list.
8156static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8157                             ObjCMethodList &List) {
8158  for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8159    S.addMethodToGlobalList(&List, Methods[I]);
8160  }
8161}
8162
8163void ASTReader::ReadMethodPool(Selector Sel) {
8164  // Get the selector generation and update it to the current generation.
8165  unsigned &Generation = SelectorGeneration[Sel];
8166  unsigned PriorGeneration = Generation;
8167  Generation = getGeneration();
8168  SelectorOutOfDate[Sel] = false;
8169
8170  // Search for methods defined with this selector.
8171  ++NumMethodPoolLookups;
8172  ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8173  ModuleMgr.visit(Visitor);
8174
8175  if (Visitor.getInstanceMethods().empty() &&
8176      Visitor.getFactoryMethods().empty())
8177    return;
8178
8179  ++NumMethodPoolHits;
8180
8181  if (!getSema())
8182    return;
8183
8184  Sema &S = *getSema();
8185  Sema::GlobalMethodPool::iterator Pos
8186    = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8187
8188  Pos->second.first.setBits(Visitor.getInstanceBits());
8189  Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8190  Pos->second.second.setBits(Visitor.getFactoryBits());
8191  Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8192
8193  // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8194  // when building a module we keep every method individually and may need to
8195  // update hasMoreThanOneDecl as we add the methods.
8196  addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8197  addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8198}
8199
8200void ASTReader::updateOutOfDateSelector(Selector Sel) {
8201  if (SelectorOutOfDate[Sel])
8202    ReadMethodPool(Sel);
8203}
8204
8205void ASTReader::ReadKnownNamespaces(
8206                          SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8207  Namespaces.clear();
8208
8209  for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8210    if (NamespaceDecl *Namespace
8211                = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8212      Namespaces.push_back(Namespace);
8213  }
8214}
8215
8216void ASTReader::ReadUndefinedButUsed(
8217    llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8218  for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8219    NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8220    SourceLocation Loc =
8221        SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8222    Undefined.insert(std::make_pair(D, Loc));
8223  }
8224}
8225
8226void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8227    FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8228                                                     Exprs) {
8229  for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8230    FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8231    uint64_t Count = DelayedDeleteExprs[Idx++];
8232    for (uint64_t C = 0; C < Count; ++C) {
8233      SourceLocation DeleteLoc =
8234          SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8235      const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8236      Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8237    }
8238  }
8239}
8240
8241void ASTReader::ReadTentativeDefinitions(
8242                  SmallVectorImpl<VarDecl *> &TentativeDefs) {
8243  for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8244    VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8245    if (Var)
8246      TentativeDefs.push_back(Var);
8247  }
8248  TentativeDefinitions.clear();
8249}
8250
8251void ASTReader::ReadUnusedFileScopedDecls(
8252                               SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8253  for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8254    DeclaratorDecl *D
8255      = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8256    if (D)
8257      Decls.push_back(D);
8258  }
8259  UnusedFileScopedDecls.clear();
8260}
8261
8262void ASTReader::ReadDelegatingConstructors(
8263                                 SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8264  for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8265    CXXConstructorDecl *D
8266      = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8267    if (D)
8268      Decls.push_back(D);
8269  }
8270  DelegatingCtorDecls.clear();
8271}
8272
8273void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8274  for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8275    TypedefNameDecl *D
8276      = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8277    if (D)
8278      Decls.push_back(D);
8279  }
8280  ExtVectorDecls.clear();
8281}
8282
8283void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8284    llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8285  for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8286       ++I) {
8287    TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8288        GetDecl(UnusedLocalTypedefNameCandidates[I]));
8289    if (D)
8290      Decls.insert(D);
8291  }
8292  UnusedLocalTypedefNameCandidates.clear();
8293}
8294
8295void ASTReader::ReadDeclsToCheckForDeferredDiags(
8296    llvm::SmallVector<Decl *, 4> &Decls) {
8297  for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8298       ++I) {
8299    auto *D = dyn_cast_or_null<Decl>(
8300        GetDecl(DeclsToCheckForDeferredDiags[I]));
8301    if (D)
8302      Decls.push_back(D);
8303  }
8304  DeclsToCheckForDeferredDiags.clear();
8305}
8306
8307
8308void ASTReader::ReadReferencedSelectors(
8309       SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8310  if (ReferencedSelectorsData.empty())
8311    return;
8312
8313  // If there are @selector references added them to its pool. This is for
8314  // implementation of -Wselector.
8315  unsigned int DataSize = ReferencedSelectorsData.size()-1;
8316  unsigned I = 0;
8317  while (I < DataSize) {
8318    Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8319    SourceLocation SelLoc
8320      = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8321    Sels.push_back(std::make_pair(Sel, SelLoc));
8322  }
8323  ReferencedSelectorsData.clear();
8324}
8325
8326void ASTReader::ReadWeakUndeclaredIdentifiers(
8327       SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8328  if (WeakUndeclaredIdentifiers.empty())
8329    return;
8330
8331  for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8332    IdentifierInfo *WeakId
8333      = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8334    IdentifierInfo *AliasId
8335      = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8336    SourceLocation Loc
8337      = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8338    bool Used = WeakUndeclaredIdentifiers[I++];
8339    WeakInfo WI(AliasId, Loc);
8340    WI.setUsed(Used);
8341    WeakIDs.push_back(std::make_pair(WeakId, WI));
8342  }
8343  WeakUndeclaredIdentifiers.clear();
8344}
8345
8346void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8347  for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8348    ExternalVTableUse VT;
8349    VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8350    VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8351    VT.DefinitionRequired = VTableUses[Idx++];
8352    VTables.push_back(VT);
8353  }
8354
8355  VTableUses.clear();
8356}
8357
8358void ASTReader::ReadPendingInstantiations(
8359       SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8360  for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8361    ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8362    SourceLocation Loc
8363      = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8364
8365    Pending.push_back(std::make_pair(D, Loc));
8366  }
8367  PendingInstantiations.clear();
8368}
8369
8370void ASTReader::ReadLateParsedTemplates(
8371    llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8372        &LPTMap) {
8373  for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8374       /* In loop */) {
8375    FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8376
8377    auto LT = std::make_unique<LateParsedTemplate>();
8378    LT->D = GetDecl(LateParsedTemplates[Idx++]);
8379
8380    ModuleFile *F = getOwningModuleFile(LT->D);
8381    assert(F && "No module");
8382
8383    unsigned TokN = LateParsedTemplates[Idx++];
8384    LT->Toks.reserve(TokN);
8385    for (unsigned T = 0; T < TokN; ++T)
8386      LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8387
8388    LPTMap.insert(std::make_pair(FD, std::move(LT)));
8389  }
8390
8391  LateParsedTemplates.clear();
8392}
8393
8394void ASTReader::LoadSelector(Selector Sel) {
8395  // It would be complicated to avoid reading the methods anyway. So don't.
8396  ReadMethodPool(Sel);
8397}
8398
8399void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8400  assert(ID && "Non-zero identifier ID required");
8401  assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8402  IdentifiersLoaded[ID - 1] = II;
8403  if (DeserializationListener)
8404    DeserializationListener->IdentifierRead(ID, II);
8405}
8406
8407/// Set the globally-visible declarations associated with the given
8408/// identifier.
8409///
8410/// If the AST reader is currently in a state where the given declaration IDs
8411/// cannot safely be resolved, they are queued until it is safe to resolve
8412/// them.
8413///
8414/// \param II an IdentifierInfo that refers to one or more globally-visible
8415/// declarations.
8416///
8417/// \param DeclIDs the set of declaration IDs with the name @p II that are
8418/// visible at global scope.
8419///
8420/// \param Decls if non-null, this vector will be populated with the set of
8421/// deserialized declarations. These declarations will not be pushed into
8422/// scope.
8423void
8424ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8425                              const SmallVectorImpl<uint32_t> &DeclIDs,
8426                                   SmallVectorImpl<Decl *> *Decls) {
8427  if (NumCurrentElementsDeserializing && !Decls) {
8428    PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8429    return;
8430  }
8431
8432  for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8433    if (!SemaObj) {
8434      // Queue this declaration so that it will be added to the
8435      // translation unit scope and identifier's declaration chain
8436      // once a Sema object is known.
8437      PreloadedDeclIDs.push_back(DeclIDs[I]);
8438      continue;
8439    }
8440
8441    NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8442
8443    // If we're simply supposed to record the declarations, do so now.
8444    if (Decls) {
8445      Decls->push_back(D);
8446      continue;
8447    }
8448
8449    // Introduce this declaration into the translation-unit scope
8450    // and add it to the declaration chain for this identifier, so
8451    // that (unqualified) name lookup will find it.
8452    pushExternalDeclIntoScope(D, II);
8453  }
8454}
8455
8456IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8457  if (ID == 0)
8458    return nullptr;
8459
8460  if (IdentifiersLoaded.empty()) {
8461    Error("no identifier table in AST file");
8462    return nullptr;
8463  }
8464
8465  ID -= 1;
8466  if (!IdentifiersLoaded[ID]) {
8467    GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8468    assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8469    ModuleFile *M = I->second;
8470    unsigned Index = ID - M->BaseIdentifierID;
8471    const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8472
8473    // All of the strings in the AST file are preceded by a 16-bit length.
8474    // Extract that 16-bit length to avoid having to execute strlen().
8475    // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8476    //  unsigned integers.  This is important to avoid integer overflow when
8477    //  we cast them to 'unsigned'.
8478    const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8479    unsigned StrLen = (((unsigned) StrLenPtr[0])
8480                       | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8481    auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8482    IdentifiersLoaded[ID] = &II;
8483    markIdentifierFromAST(*this,  II);
8484    if (DeserializationListener)
8485      DeserializationListener->IdentifierRead(ID + 1, &II);
8486  }
8487
8488  return IdentifiersLoaded[ID];
8489}
8490
8491IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8492  return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8493}
8494
8495IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8496  if (LocalID < NUM_PREDEF_IDENT_IDS)
8497    return LocalID;
8498
8499  if (!M.ModuleOffsetMap.empty())
8500    ReadModuleOffsetMap(M);
8501
8502  ContinuousRangeMap<uint32_t, int, 2>::iterator I
8503    = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8504  assert(I != M.IdentifierRemap.end()
8505         && "Invalid index into identifier index remap");
8506
8507  return LocalID + I->second;
8508}
8509
8510MacroInfo *ASTReader::getMacro(MacroID ID) {
8511  if (ID == 0)
8512    return nullptr;
8513
8514  if (MacrosLoaded.empty()) {
8515    Error("no macro table in AST file");
8516    return nullptr;
8517  }
8518
8519  ID -= NUM_PREDEF_MACRO_IDS;
8520  if (!MacrosLoaded[ID]) {
8521    GlobalMacroMapType::iterator I
8522      = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8523    assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8524    ModuleFile *M = I->second;
8525    unsigned Index = ID - M->BaseMacroID;
8526    MacrosLoaded[ID] =
8527        ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8528
8529    if (DeserializationListener)
8530      DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8531                                         MacrosLoaded[ID]);
8532  }
8533
8534  return MacrosLoaded[ID];
8535}
8536
8537MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8538  if (LocalID < NUM_PREDEF_MACRO_IDS)
8539    return LocalID;
8540
8541  if (!M.ModuleOffsetMap.empty())
8542    ReadModuleOffsetMap(M);
8543
8544  ContinuousRangeMap<uint32_t, int, 2>::iterator I
8545    = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8546  assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8547
8548  return LocalID + I->second;
8549}
8550
8551serialization::SubmoduleID
8552ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8553  if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8554    return LocalID;
8555
8556  if (!M.ModuleOffsetMap.empty())
8557    ReadModuleOffsetMap(M);
8558
8559  ContinuousRangeMap<uint32_t, int, 2>::iterator I
8560    = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8561  assert(I != M.SubmoduleRemap.end()
8562         && "Invalid index into submodule index remap");
8563
8564  return LocalID + I->second;
8565}
8566
8567Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8568  if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8569    assert(GlobalID == 0 && "Unhandled global submodule ID");
8570    return nullptr;
8571  }
8572
8573  if (GlobalID > SubmodulesLoaded.size()) {
8574    Error("submodule ID out of range in AST file");
8575    return nullptr;
8576  }
8577
8578  return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8579}
8580
8581Module *ASTReader::getModule(unsigned ID) {
8582  return getSubmodule(ID);
8583}
8584
8585bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8586  ModuleFile *MF = getOwningModuleFile(D);
8587  return MF && MF->PCHHasObjectFile;
8588}
8589
8590ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8591  if (ID & 1) {
8592    // It's a module, look it up by submodule ID.
8593    auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8594    return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8595  } else {
8596    // It's a prefix (preamble, PCH, ...). Look it up by index.
8597    unsigned IndexFromEnd = ID >> 1;
8598    assert(IndexFromEnd && "got reference to unknown module file");
8599    return getModuleManager().pch_modules().end()[-IndexFromEnd];
8600  }
8601}
8602
8603unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8604  if (!F)
8605    return 1;
8606
8607  // For a file representing a module, use the submodule ID of the top-level
8608  // module as the file ID. For any other kind of file, the number of such
8609  // files loaded beforehand will be the same on reload.
8610  // FIXME: Is this true even if we have an explicit module file and a PCH?
8611  if (F->isModule())
8612    return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8613
8614  auto PCHModules = getModuleManager().pch_modules();
8615  auto I = llvm::find(PCHModules, F);
8616  assert(I != PCHModules.end() && "emitting reference to unknown file");
8617  return (I - PCHModules.end()) << 1;
8618}
8619
8620llvm::Optional<ASTSourceDescriptor>
8621ASTReader::getSourceDescriptor(unsigned ID) {
8622  if (Module *M = getSubmodule(ID))
8623    return ASTSourceDescriptor(*M);
8624
8625  // If there is only a single PCH, return it instead.
8626  // Chained PCH are not supported.
8627  const auto &PCHChain = ModuleMgr.pch_modules();
8628  if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8629    ModuleFile &MF = ModuleMgr.getPrimaryModule();
8630    StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8631    StringRef FileName = llvm::sys::path::filename(MF.FileName);
8632    return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8633                               MF.Signature);
8634  }
8635  return None;
8636}
8637
8638ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8639  auto I = DefinitionSource.find(FD);
8640  if (I == DefinitionSource.end())
8641    return EK_ReplyHazy;
8642  return I->second ? EK_Never : EK_Always;
8643}
8644
8645Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8646  return DecodeSelector(getGlobalSelectorID(M, LocalID));
8647}
8648
8649Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8650  if (ID == 0)
8651    return Selector();
8652
8653  if (ID > SelectorsLoaded.size()) {
8654    Error("selector ID out of range in AST file");
8655    return Selector();
8656  }
8657
8658  if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8659    // Load this selector from the selector table.
8660    GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8661    assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8662    ModuleFile &M = *I->second;
8663    ASTSelectorLookupTrait Trait(*this, M);
8664    unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8665    SelectorsLoaded[ID - 1] =
8666      Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8667    if (DeserializationListener)
8668      DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8669  }
8670
8671  return SelectorsLoaded[ID - 1];
8672}
8673
8674Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8675  return DecodeSelector(ID);
8676}
8677
8678uint32_t ASTReader::GetNumExternalSelectors() {
8679  // ID 0 (the null selector) is considered an external selector.
8680  return getTotalNumSelectors() + 1;
8681}
8682
8683serialization::SelectorID
8684ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8685  if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8686    return LocalID;
8687
8688  if (!M.ModuleOffsetMap.empty())
8689    ReadModuleOffsetMap(M);
8690
8691  ContinuousRangeMap<uint32_t, int, 2>::iterator I
8692    = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8693  assert(I != M.SelectorRemap.end()
8694         && "Invalid index into selector index remap");
8695
8696  return LocalID + I->second;
8697}
8698
8699DeclarationNameLoc
8700ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8701  DeclarationNameLoc DNLoc;
8702  switch (Name.getNameKind()) {
8703  case DeclarationName::CXXConstructorName:
8704  case DeclarationName::CXXDestructorName:
8705  case DeclarationName::CXXConversionFunctionName:
8706    DNLoc.NamedType.TInfo = readTypeSourceInfo();
8707    break;
8708
8709  case DeclarationName::CXXOperatorName:
8710    DNLoc.CXXOperatorName.BeginOpNameLoc
8711      = readSourceLocation().getRawEncoding();
8712    DNLoc.CXXOperatorName.EndOpNameLoc
8713      = readSourceLocation().getRawEncoding();
8714    break;
8715
8716  case DeclarationName::CXXLiteralOperatorName:
8717    DNLoc.CXXLiteralOperatorName.OpNameLoc
8718      = readSourceLocation().getRawEncoding();
8719    break;
8720
8721  case DeclarationName::Identifier:
8722  case DeclarationName::ObjCZeroArgSelector:
8723  case DeclarationName::ObjCOneArgSelector:
8724  case DeclarationName::ObjCMultiArgSelector:
8725  case DeclarationName::CXXUsingDirective:
8726  case DeclarationName::CXXDeductionGuideName:
8727    break;
8728  }
8729  return DNLoc;
8730}
8731
8732DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8733  DeclarationNameInfo NameInfo;
8734  NameInfo.setName(readDeclarationName());
8735  NameInfo.setLoc(readSourceLocation());
8736  NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8737  return NameInfo;
8738}
8739
8740void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8741  Info.QualifierLoc = readNestedNameSpecifierLoc();
8742  unsigned NumTPLists = readInt();
8743  Info.NumTemplParamLists = NumTPLists;
8744  if (NumTPLists) {
8745    Info.TemplParamLists =
8746        new (getContext()) TemplateParameterList *[NumTPLists];
8747    for (unsigned i = 0; i != NumTPLists; ++i)
8748      Info.TemplParamLists[i] = readTemplateParameterList();
8749  }
8750}
8751
8752TemplateParameterList *
8753ASTRecordReader::readTemplateParameterList() {
8754  SourceLocation TemplateLoc = readSourceLocation();
8755  SourceLocation LAngleLoc = readSourceLocation();
8756  SourceLocation RAngleLoc = readSourceLocation();
8757
8758  unsigned NumParams = readInt();
8759  SmallVector<NamedDecl *, 16> Params;
8760  Params.reserve(NumParams);
8761  while (NumParams--)
8762    Params.push_back(readDeclAs<NamedDecl>());
8763
8764  bool HasRequiresClause = readBool();
8765  Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8766
8767  TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8768      getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8769  return TemplateParams;
8770}
8771
8772void ASTRecordReader::readTemplateArgumentList(
8773                        SmallVectorImpl<TemplateArgument> &TemplArgs,
8774                        bool Canonicalize) {
8775  unsigned NumTemplateArgs = readInt();
8776  TemplArgs.reserve(NumTemplateArgs);
8777  while (NumTemplateArgs--)
8778    TemplArgs.push_back(readTemplateArgument(Canonicalize));
8779}
8780
8781/// Read a UnresolvedSet structure.
8782void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8783  unsigned NumDecls = readInt();
8784  Set.reserve(getContext(), NumDecls);
8785  while (NumDecls--) {
8786    DeclID ID = readDeclID();
8787    AccessSpecifier AS = (AccessSpecifier) readInt();
8788    Set.addLazyDecl(getContext(), ID, AS);
8789  }
8790}
8791
8792CXXBaseSpecifier
8793ASTRecordReader::readCXXBaseSpecifier() {
8794  bool isVirtual = readBool();
8795  bool isBaseOfClass = readBool();
8796  AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8797  bool inheritConstructors = readBool();
8798  TypeSourceInfo *TInfo = readTypeSourceInfo();
8799  SourceRange Range = readSourceRange();
8800  SourceLocation EllipsisLoc = readSourceLocation();
8801  CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8802                          EllipsisLoc);
8803  Result.setInheritConstructors(inheritConstructors);
8804  return Result;
8805}
8806
8807CXXCtorInitializer **
8808ASTRecordReader::readCXXCtorInitializers() {
8809  ASTContext &Context = getContext();
8810  unsigned NumInitializers = readInt();
8811  assert(NumInitializers && "wrote ctor initializers but have no inits");
8812  auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8813  for (unsigned i = 0; i != NumInitializers; ++i) {
8814    TypeSourceInfo *TInfo = nullptr;
8815    bool IsBaseVirtual = false;
8816    FieldDecl *Member = nullptr;
8817    IndirectFieldDecl *IndirectMember = nullptr;
8818
8819    CtorInitializerType Type = (CtorInitializerType) readInt();
8820    switch (Type) {
8821    case CTOR_INITIALIZER_BASE:
8822      TInfo = readTypeSourceInfo();
8823      IsBaseVirtual = readBool();
8824      break;
8825
8826    case CTOR_INITIALIZER_DELEGATING:
8827      TInfo = readTypeSourceInfo();
8828      break;
8829
8830     case CTOR_INITIALIZER_MEMBER:
8831      Member = readDeclAs<FieldDecl>();
8832      break;
8833
8834     case CTOR_INITIALIZER_INDIRECT_MEMBER:
8835      IndirectMember = readDeclAs<IndirectFieldDecl>();
8836      break;
8837    }
8838
8839    SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8840    Expr *Init = readExpr();
8841    SourceLocation LParenLoc = readSourceLocation();
8842    SourceLocation RParenLoc = readSourceLocation();
8843
8844    CXXCtorInitializer *BOMInit;
8845    if (Type == CTOR_INITIALIZER_BASE)
8846      BOMInit = new (Context)
8847          CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8848                             RParenLoc, MemberOrEllipsisLoc);
8849    else if (Type == CTOR_INITIALIZER_DELEGATING)
8850      BOMInit = new (Context)
8851          CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8852    else if (Member)
8853      BOMInit = new (Context)
8854          CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8855                             Init, RParenLoc);
8856    else
8857      BOMInit = new (Context)
8858          CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8859                             LParenLoc, Init, RParenLoc);
8860
8861    if (/*IsWritten*/readBool()) {
8862      unsigned SourceOrder = readInt();
8863      BOMInit->setSourceOrder(SourceOrder);
8864    }
8865
8866    CtorInitializers[i] = BOMInit;
8867  }
8868
8869  return CtorInitializers;
8870}
8871
8872NestedNameSpecifierLoc
8873ASTRecordReader::readNestedNameSpecifierLoc() {
8874  ASTContext &Context = getContext();
8875  unsigned N = readInt();
8876  NestedNameSpecifierLocBuilder Builder;
8877  for (unsigned I = 0; I != N; ++I) {
8878    auto Kind = readNestedNameSpecifierKind();
8879    switch (Kind) {
8880    case NestedNameSpecifier::Identifier: {
8881      IdentifierInfo *II = readIdentifier();
8882      SourceRange Range = readSourceRange();
8883      Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8884      break;
8885    }
8886
8887    case NestedNameSpecifier::Namespace: {
8888      NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8889      SourceRange Range = readSourceRange();
8890      Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8891      break;
8892    }
8893
8894    case NestedNameSpecifier::NamespaceAlias: {
8895      NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8896      SourceRange Range = readSourceRange();
8897      Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8898      break;
8899    }
8900
8901    case NestedNameSpecifier::TypeSpec:
8902    case NestedNameSpecifier::TypeSpecWithTemplate: {
8903      bool Template = readBool();
8904      TypeSourceInfo *T = readTypeSourceInfo();
8905      if (!T)
8906        return NestedNameSpecifierLoc();
8907      SourceLocation ColonColonLoc = readSourceLocation();
8908
8909      // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8910      Builder.Extend(Context,
8911                     Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8912                     T->getTypeLoc(), ColonColonLoc);
8913      break;
8914    }
8915
8916    case NestedNameSpecifier::Global: {
8917      SourceLocation ColonColonLoc = readSourceLocation();
8918      Builder.MakeGlobal(Context, ColonColonLoc);
8919      break;
8920    }
8921
8922    case NestedNameSpecifier::Super: {
8923      CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8924      SourceRange Range = readSourceRange();
8925      Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8926      break;
8927    }
8928    }
8929  }
8930
8931  return Builder.getWithLocInContext(Context);
8932}
8933
8934SourceRange
8935ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8936                           unsigned &Idx) {
8937  SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8938  SourceLocation end = ReadSourceLocation(F, Record, Idx);
8939  return SourceRange(beg, end);
8940}
8941
8942static FixedPointSemantics
8943ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8944                        unsigned &Idx) {
8945  unsigned Width = Record[Idx++];
8946  unsigned Scale = Record[Idx++];
8947  uint64_t Tmp = Record[Idx++];
8948  bool IsSigned = Tmp & 0x1;
8949  bool IsSaturated = Tmp & 0x2;
8950  bool HasUnsignedPadding = Tmp & 0x4;
8951  return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8952                             HasUnsignedPadding);
8953}
8954
8955static const llvm::fltSemantics &
8956readAPFloatSemantics(ASTRecordReader &reader) {
8957  return llvm::APFloatBase::EnumToSemantics(
8958    static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8959}
8960
8961APValue ASTRecordReader::readAPValue() {
8962  unsigned Kind = readInt();
8963  switch ((APValue::ValueKind) Kind) {
8964  case APValue::None:
8965    return APValue();
8966  case APValue::Indeterminate:
8967    return APValue::IndeterminateValue();
8968  case APValue::Int:
8969    return APValue(readAPSInt());
8970  case APValue::Float: {
8971    const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8972    return APValue(readAPFloat(FloatSema));
8973  }
8974  case APValue::FixedPoint: {
8975    FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8976    return APValue(APFixedPoint(readAPInt(), FPSema));
8977  }
8978  case APValue::ComplexInt: {
8979    llvm::APSInt First = readAPSInt();
8980    return APValue(std::move(First), readAPSInt());
8981  }
8982  case APValue::ComplexFloat: {
8983    const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8984    llvm::APFloat First = readAPFloat(FloatSema1);
8985    const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8986    return APValue(std::move(First), readAPFloat(FloatSema2));
8987  }
8988  case APValue::LValue:
8989  case APValue::Vector:
8990  case APValue::Array:
8991  case APValue::Struct:
8992  case APValue::Union:
8993  case APValue::MemberPointer:
8994  case APValue::AddrLabelDiff:
8995    // TODO : Handle all these APValue::ValueKind.
8996    return APValue();
8997  }
8998  llvm_unreachable("Invalid APValue::ValueKind");
8999}
9000
9001/// Read a floating-point value
9002llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9003  return llvm::APFloat(Sem, readAPInt());
9004}
9005
9006// Read a string
9007std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9008  unsigned Len = Record[Idx++];
9009  std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9010  Idx += Len;
9011  return Result;
9012}
9013
9014std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9015                                unsigned &Idx) {
9016  std::string Filename = ReadString(Record, Idx);
9017  ResolveImportedPath(F, Filename);
9018  return Filename;
9019}
9020
9021std::string ASTReader::ReadPath(StringRef BaseDirectory,
9022                                const RecordData &Record, unsigned &Idx) {
9023  std::string Filename = ReadString(Record, Idx);
9024  if (!BaseDirectory.empty())
9025    ResolveImportedPath(Filename, BaseDirectory);
9026  return Filename;
9027}
9028
9029VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9030                                         unsigned &Idx) {
9031  unsigned Major = Record[Idx++];
9032  unsigned Minor = Record[Idx++];
9033  unsigned Subminor = Record[Idx++];
9034  if (Minor == 0)
9035    return VersionTuple(Major);
9036  if (Subminor == 0)
9037    return VersionTuple(Major, Minor - 1);
9038  return VersionTuple(Major, Minor - 1, Subminor - 1);
9039}
9040
9041CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9042                                          const RecordData &Record,
9043                                          unsigned &Idx) {
9044  CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9045  return CXXTemporary::Create(getContext(), Decl);
9046}
9047
9048DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9049  return Diag(CurrentImportLoc, DiagID);
9050}
9051
9052DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9053  return Diags.Report(Loc, DiagID);
9054}
9055
9056/// Retrieve the identifier table associated with the
9057/// preprocessor.
9058IdentifierTable &ASTReader::getIdentifierTable() {
9059  return PP.getIdentifierTable();
9060}
9061
9062/// Record that the given ID maps to the given switch-case
9063/// statement.
9064void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9065  assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9066         "Already have a SwitchCase with this ID");
9067  (*CurrSwitchCaseStmts)[ID] = SC;
9068}
9069
9070/// Retrieve the switch-case statement with the given ID.
9071SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9072  assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9073  return (*CurrSwitchCaseStmts)[ID];
9074}
9075
9076void ASTReader::ClearSwitchCaseIDs() {
9077  CurrSwitchCaseStmts->clear();
9078}
9079
9080void ASTReader::ReadComments() {
9081  ASTContext &Context = getContext();
9082  std::vector<RawComment *> Comments;
9083  for (SmallVectorImpl<std::pair<BitstreamCursor,
9084                                 serialization::ModuleFile *>>::iterator
9085       I = CommentsCursors.begin(),
9086       E = CommentsCursors.end();
9087       I != E; ++I) {
9088    Comments.clear();
9089    BitstreamCursor &Cursor = I->first;
9090    serialization::ModuleFile &F = *I->second;
9091    SavedStreamPosition SavedPosition(Cursor);
9092
9093    RecordData Record;
9094    while (true) {
9095      Expected<llvm::BitstreamEntry> MaybeEntry =
9096          Cursor.advanceSkippingSubblocks(
9097              BitstreamCursor::AF_DontPopBlockAtEnd);
9098      if (!MaybeEntry) {
9099        Error(MaybeEntry.takeError());
9100        return;
9101      }
9102      llvm::BitstreamEntry Entry = MaybeEntry.get();
9103
9104      switch (Entry.Kind) {
9105      case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9106      case llvm::BitstreamEntry::Error:
9107        Error("malformed block record in AST file");
9108        return;
9109      case llvm::BitstreamEntry::EndBlock:
9110        goto NextCursor;
9111      case llvm::BitstreamEntry::Record:
9112        // The interesting case.
9113        break;
9114      }
9115
9116      // Read a record.
9117      Record.clear();
9118      Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9119      if (!MaybeComment) {
9120        Error(MaybeComment.takeError());
9121        return;
9122      }
9123      switch ((CommentRecordTypes)MaybeComment.get()) {
9124      case COMMENTS_RAW_COMMENT: {
9125        unsigned Idx = 0;
9126        SourceRange SR = ReadSourceRange(F, Record, Idx);
9127        RawComment::CommentKind Kind =
9128            (RawComment::CommentKind) Record[Idx++];
9129        bool IsTrailingComment = Record[Idx++];
9130        bool IsAlmostTrailingComment = Record[Idx++];
9131        Comments.push_back(new (Context) RawComment(
9132            SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9133        break;
9134      }
9135      }
9136    }
9137  NextCursor:
9138    llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9139        FileToOffsetToComment;
9140    for (RawComment *C : Comments) {
9141      SourceLocation CommentLoc = C->getBeginLoc();
9142      if (CommentLoc.isValid()) {
9143        std::pair<FileID, unsigned> Loc =
9144            SourceMgr.getDecomposedLoc(CommentLoc);
9145        if (Loc.first.isValid())
9146          Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9147      }
9148    }
9149  }
9150}
9151
9152void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9153                                bool IncludeSystem, bool Complain,
9154                    llvm::function_ref<void(const serialization::InputFile &IF,
9155                                            bool isSystem)> Visitor) {
9156  unsigned NumUserInputs = MF.NumUserInputFiles;
9157  unsigned NumInputs = MF.InputFilesLoaded.size();
9158  assert(NumUserInputs <= NumInputs);
9159  unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9160  for (unsigned I = 0; I < N; ++I) {
9161    bool IsSystem = I >= NumUserInputs;
9162    InputFile IF = getInputFile(MF, I+1, Complain);
9163    Visitor(IF, IsSystem);
9164  }
9165}
9166
9167void ASTReader::visitTopLevelModuleMaps(
9168    serialization::ModuleFile &MF,
9169    llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9170  unsigned NumInputs = MF.InputFilesLoaded.size();
9171  for (unsigned I = 0; I < NumInputs; ++I) {
9172    InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9173    if (IFI.TopLevelModuleMap)
9174      // FIXME: This unnecessarily re-reads the InputFileInfo.
9175      if (auto *FE = getInputFile(MF, I + 1).getFile())
9176        Visitor(FE);
9177  }
9178}
9179
9180std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9181  // If we know the owning module, use it.
9182  if (Module *M = D->getImportedOwningModule())
9183    return M->getFullModuleName();
9184
9185  // Otherwise, use the name of the top-level module the decl is within.
9186  if (ModuleFile *M = getOwningModuleFile(D))
9187    return M->ModuleName;
9188
9189  // Not from a module.
9190  return {};
9191}
9192
9193void ASTReader::finishPendingActions() {
9194  while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9195         !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9196         !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9197         !PendingUpdateRecords.empty()) {
9198    // If any identifiers with corresponding top-level declarations have
9199    // been loaded, load those declarations now.
9200    using TopLevelDeclsMap =
9201        llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9202    TopLevelDeclsMap TopLevelDecls;
9203
9204    while (!PendingIdentifierInfos.empty()) {
9205      IdentifierInfo *II = PendingIdentifierInfos.back().first;
9206      SmallVector<uint32_t, 4> DeclIDs =
9207          std::move(PendingIdentifierInfos.back().second);
9208      PendingIdentifierInfos.pop_back();
9209
9210      SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9211    }
9212
9213    // Load each function type that we deferred loading because it was a
9214    // deduced type that might refer to a local type declared within itself.
9215    for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9216      auto *FD = PendingFunctionTypes[I].first;
9217      FD->setType(GetType(PendingFunctionTypes[I].second));
9218
9219      // If we gave a function a deduced return type, remember that we need to
9220      // propagate that along the redeclaration chain.
9221      auto *DT = FD->getReturnType()->getContainedDeducedType();
9222      if (DT && DT->isDeduced())
9223        PendingDeducedTypeUpdates.insert(
9224            {FD->getCanonicalDecl(), FD->getReturnType()});
9225    }
9226    PendingFunctionTypes.clear();
9227
9228    // For each decl chain that we wanted to complete while deserializing, mark
9229    // it as "still needs to be completed".
9230    for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9231      markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9232    }
9233    PendingIncompleteDeclChains.clear();
9234
9235    // Load pending declaration chains.
9236    for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9237      loadPendingDeclChain(PendingDeclChains[I].first,
9238                           PendingDeclChains[I].second);
9239    PendingDeclChains.clear();
9240
9241    // Make the most recent of the top-level declarations visible.
9242    for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9243           TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9244      IdentifierInfo *II = TLD->first;
9245      for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9246        pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9247      }
9248    }
9249
9250    // Load any pending macro definitions.
9251    for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9252      IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9253      SmallVector<PendingMacroInfo, 2> GlobalIDs;
9254      GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9255      // Initialize the macro history from chained-PCHs ahead of module imports.
9256      for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9257           ++IDIdx) {
9258        const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9259        if (!Info.M->isModule())
9260          resolvePendingMacro(II, Info);
9261      }
9262      // Handle module imports.
9263      for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9264           ++IDIdx) {
9265        const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9266        if (Info.M->isModule())
9267          resolvePendingMacro(II, Info);
9268      }
9269    }
9270    PendingMacroIDs.clear();
9271
9272    // Wire up the DeclContexts for Decls that we delayed setting until
9273    // recursive loading is completed.
9274    while (!PendingDeclContextInfos.empty()) {
9275      PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9276      PendingDeclContextInfos.pop_front();
9277      DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9278      DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9279      Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9280    }
9281
9282    // Perform any pending declaration updates.
9283    while (!PendingUpdateRecords.empty()) {
9284      auto Update = PendingUpdateRecords.pop_back_val();
9285      ReadingKindTracker ReadingKind(Read_Decl, *this);
9286      loadDeclUpdateRecords(Update);
9287    }
9288  }
9289
9290  // At this point, all update records for loaded decls are in place, so any
9291  // fake class definitions should have become real.
9292  assert(PendingFakeDefinitionData.empty() &&
9293         "faked up a class definition but never saw the real one");
9294
9295  // If we deserialized any C++ or Objective-C class definitions, any
9296  // Objective-C protocol definitions, or any redeclarable templates, make sure
9297  // that all redeclarations point to the definitions. Note that this can only
9298  // happen now, after the redeclaration chains have been fully wired.
9299  for (Decl *D : PendingDefinitions) {
9300    if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9301      if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9302        // Make sure that the TagType points at the definition.
9303        const_cast<TagType*>(TagT)->decl = TD;
9304      }
9305
9306      if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9307        for (auto *R = getMostRecentExistingDecl(RD); R;
9308             R = R->getPreviousDecl()) {
9309          assert((R == D) ==
9310                     cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9311                 "declaration thinks it's the definition but it isn't");
9312          cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9313        }
9314      }
9315
9316      continue;
9317    }
9318
9319    if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9320      // Make sure that the ObjCInterfaceType points at the definition.
9321      const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9322        ->Decl = ID;
9323
9324      for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9325        cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9326
9327      continue;
9328    }
9329
9330    if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9331      for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9332        cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9333
9334      continue;
9335    }
9336
9337    auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9338    for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9339      cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9340  }
9341  PendingDefinitions.clear();
9342
9343  // Load the bodies of any functions or methods we've encountered. We do
9344  // this now (delayed) so that we can be sure that the declaration chains
9345  // have been fully wired up (hasBody relies on this).
9346  // FIXME: We shouldn't require complete redeclaration chains here.
9347  for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9348                               PBEnd = PendingBodies.end();
9349       PB != PBEnd; ++PB) {
9350    if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9351      // For a function defined inline within a class template, force the
9352      // canonical definition to be the one inside the canonical definition of
9353      // the template. This ensures that we instantiate from a correct view
9354      // of the template.
9355      //
9356      // Sadly we can't do this more generally: we can't be sure that all
9357      // copies of an arbitrary class definition will have the same members
9358      // defined (eg, some member functions may not be instantiated, and some
9359      // special members may or may not have been implicitly defined).
9360      if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9361        if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9362          continue;
9363
9364      // FIXME: Check for =delete/=default?
9365      // FIXME: Complain about ODR violations here?
9366      const FunctionDecl *Defn = nullptr;
9367      if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9368        FD->setLazyBody(PB->second);
9369      } else {
9370        auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9371        mergeDefinitionVisibility(NonConstDefn, FD);
9372
9373        if (!FD->isLateTemplateParsed() &&
9374            !NonConstDefn->isLateTemplateParsed() &&
9375            FD->getODRHash() != NonConstDefn->getODRHash()) {
9376          if (!isa<CXXMethodDecl>(FD)) {
9377            PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9378          } else if (FD->getLexicalParent()->isFileContext() &&
9379                     NonConstDefn->getLexicalParent()->isFileContext()) {
9380            // Only diagnose out-of-line method definitions.  If they are
9381            // in class definitions, then an error will be generated when
9382            // processing the class bodies.
9383            PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9384          }
9385        }
9386      }
9387      continue;
9388    }
9389
9390    ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9391    if (!getContext().getLangOpts().Modules || !MD->hasBody())
9392      MD->setLazyBody(PB->second);
9393  }
9394  PendingBodies.clear();
9395
9396  // Do some cleanup.
9397  for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9398    getContext().deduplicateMergedDefinitonsFor(ND);
9399  PendingMergedDefinitionsToDeduplicate.clear();
9400}
9401
9402void ASTReader::diagnoseOdrViolations() {
9403  if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9404      PendingFunctionOdrMergeFailures.empty() &&
9405      PendingEnumOdrMergeFailures.empty())
9406    return;
9407
9408  // Trigger the import of the full definition of each class that had any
9409  // odr-merging problems, so we can produce better diagnostics for them.
9410  // These updates may in turn find and diagnose some ODR failures, so take
9411  // ownership of the set first.
9412  auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9413  PendingOdrMergeFailures.clear();
9414  for (auto &Merge : OdrMergeFailures) {
9415    Merge.first->buildLookup();
9416    Merge.first->decls_begin();
9417    Merge.first->bases_begin();
9418    Merge.first->vbases_begin();
9419    for (auto &RecordPair : Merge.second) {
9420      auto *RD = RecordPair.first;
9421      RD->decls_begin();
9422      RD->bases_begin();
9423      RD->vbases_begin();
9424    }
9425  }
9426
9427  // Trigger the import of functions.
9428  auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9429  PendingFunctionOdrMergeFailures.clear();
9430  for (auto &Merge : FunctionOdrMergeFailures) {
9431    Merge.first->buildLookup();
9432    Merge.first->decls_begin();
9433    Merge.first->getBody();
9434    for (auto &FD : Merge.second) {
9435      FD->buildLookup();
9436      FD->decls_begin();
9437      FD->getBody();
9438    }
9439  }
9440
9441  // Trigger the import of enums.
9442  auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9443  PendingEnumOdrMergeFailures.clear();
9444  for (auto &Merge : EnumOdrMergeFailures) {
9445    Merge.first->decls_begin();
9446    for (auto &Enum : Merge.second) {
9447      Enum->decls_begin();
9448    }
9449  }
9450
9451  // For each declaration from a merged context, check that the canonical
9452  // definition of that context also contains a declaration of the same
9453  // entity.
9454  //
9455  // Caution: this loop does things that might invalidate iterators into
9456  // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9457  while (!PendingOdrMergeChecks.empty()) {
9458    NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9459
9460    // FIXME: Skip over implicit declarations for now. This matters for things
9461    // like implicitly-declared special member functions. This isn't entirely
9462    // correct; we can end up with multiple unmerged declarations of the same
9463    // implicit entity.
9464    if (D->isImplicit())
9465      continue;
9466
9467    DeclContext *CanonDef = D->getDeclContext();
9468
9469    bool Found = false;
9470    const Decl *DCanon = D->getCanonicalDecl();
9471
9472    for (auto RI : D->redecls()) {
9473      if (RI->getLexicalDeclContext() == CanonDef) {
9474        Found = true;
9475        break;
9476      }
9477    }
9478    if (Found)
9479      continue;
9480
9481    // Quick check failed, time to do the slow thing. Note, we can't just
9482    // look up the name of D in CanonDef here, because the member that is
9483    // in CanonDef might not be found by name lookup (it might have been
9484    // replaced by a more recent declaration in the lookup table), and we
9485    // can't necessarily find it in the redeclaration chain because it might
9486    // be merely mergeable, not redeclarable.
9487    llvm::SmallVector<const NamedDecl*, 4> Candidates;
9488    for (auto *CanonMember : CanonDef->decls()) {
9489      if (CanonMember->getCanonicalDecl() == DCanon) {
9490        // This can happen if the declaration is merely mergeable and not
9491        // actually redeclarable (we looked for redeclarations earlier).
9492        //
9493        // FIXME: We should be able to detect this more efficiently, without
9494        // pulling in all of the members of CanonDef.
9495        Found = true;
9496        break;
9497      }
9498      if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9499        if (ND->getDeclName() == D->getDeclName())
9500          Candidates.push_back(ND);
9501    }
9502
9503    if (!Found) {
9504      // The AST doesn't like TagDecls becoming invalid after they've been
9505      // completed. We only really need to mark FieldDecls as invalid here.
9506      if (!isa<TagDecl>(D))
9507        D->setInvalidDecl();
9508
9509      // Ensure we don't accidentally recursively enter deserialization while
9510      // we're producing our diagnostic.
9511      Deserializing RecursionGuard(this);
9512
9513      std::string CanonDefModule =
9514          getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9515      Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9516        << D << getOwningModuleNameForDiagnostic(D)
9517        << CanonDef << CanonDefModule.empty() << CanonDefModule;
9518
9519      if (Candidates.empty())
9520        Diag(cast<Decl>(CanonDef)->getLocation(),
9521             diag::note_module_odr_violation_no_possible_decls) << D;
9522      else {
9523        for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9524          Diag(Candidates[I]->getLocation(),
9525               diag::note_module_odr_violation_possible_decl)
9526            << Candidates[I];
9527      }
9528
9529      DiagnosedOdrMergeFailures.insert(CanonDef);
9530    }
9531  }
9532
9533  if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9534      EnumOdrMergeFailures.empty())
9535    return;
9536
9537  // Ensure we don't accidentally recursively enter deserialization while
9538  // we're producing our diagnostics.
9539  Deserializing RecursionGuard(this);
9540
9541  // Common code for hashing helpers.
9542  ODRHash Hash;
9543  auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9544    Hash.clear();
9545    Hash.AddQualType(Ty);
9546    return Hash.CalculateHash();
9547  };
9548
9549  auto ComputeODRHash = [&Hash](const Stmt *S) {
9550    assert(S);
9551    Hash.clear();
9552    Hash.AddStmt(S);
9553    return Hash.CalculateHash();
9554  };
9555
9556  auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9557    assert(D);
9558    Hash.clear();
9559    Hash.AddSubDecl(D);
9560    return Hash.CalculateHash();
9561  };
9562
9563  auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9564    Hash.clear();
9565    Hash.AddTemplateArgument(TA);
9566    return Hash.CalculateHash();
9567  };
9568
9569  auto ComputeTemplateParameterListODRHash =
9570      [&Hash](const TemplateParameterList *TPL) {
9571        assert(TPL);
9572        Hash.clear();
9573        Hash.AddTemplateParameterList(TPL);
9574        return Hash.CalculateHash();
9575      };
9576
9577  // Used with err_module_odr_violation_mismatch_decl and
9578  // note_module_odr_violation_mismatch_decl
9579  // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9580  enum ODRMismatchDecl {
9581    EndOfClass,
9582    PublicSpecifer,
9583    PrivateSpecifer,
9584    ProtectedSpecifer,
9585    StaticAssert,
9586    Field,
9587    CXXMethod,
9588    TypeAlias,
9589    TypeDef,
9590    Var,
9591    Friend,
9592    FunctionTemplate,
9593    Other
9594  };
9595
9596  // Used with err_module_odr_violation_mismatch_decl_diff and
9597  // note_module_odr_violation_mismatch_decl_diff
9598  enum ODRMismatchDeclDifference {
9599    StaticAssertCondition,
9600    StaticAssertMessage,
9601    StaticAssertOnlyMessage,
9602    FieldName,
9603    FieldTypeName,
9604    FieldSingleBitField,
9605    FieldDifferentWidthBitField,
9606    FieldSingleMutable,
9607    FieldSingleInitializer,
9608    FieldDifferentInitializers,
9609    MethodName,
9610    MethodDeleted,
9611    MethodDefaulted,
9612    MethodVirtual,
9613    MethodStatic,
9614    MethodVolatile,
9615    MethodConst,
9616    MethodInline,
9617    MethodNumberParameters,
9618    MethodParameterType,
9619    MethodParameterName,
9620    MethodParameterSingleDefaultArgument,
9621    MethodParameterDifferentDefaultArgument,
9622    MethodNoTemplateArguments,
9623    MethodDifferentNumberTemplateArguments,
9624    MethodDifferentTemplateArgument,
9625    MethodSingleBody,
9626    MethodDifferentBody,
9627    TypedefName,
9628    TypedefType,
9629    VarName,
9630    VarType,
9631    VarSingleInitializer,
9632    VarDifferentInitializer,
9633    VarConstexpr,
9634    FriendTypeFunction,
9635    FriendType,
9636    FriendFunction,
9637    FunctionTemplateDifferentNumberParameters,
9638    FunctionTemplateParameterDifferentKind,
9639    FunctionTemplateParameterName,
9640    FunctionTemplateParameterSingleDefaultArgument,
9641    FunctionTemplateParameterDifferentDefaultArgument,
9642    FunctionTemplateParameterDifferentType,
9643    FunctionTemplatePackParameter,
9644  };
9645
9646  // These lambdas have the common portions of the ODR diagnostics.  This
9647  // has the same return as Diag(), so addition parameters can be passed
9648  // in with operator<<
9649  auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9650                                 SourceLocation Loc, SourceRange Range,
9651                                 ODRMismatchDeclDifference DiffType) {
9652    return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9653           << FirstRecord << FirstModule.empty() << FirstModule << Range
9654           << DiffType;
9655  };
9656  auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9657                                SourceRange Range, ODRMismatchDeclDifference DiffType) {
9658    return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9659           << SecondModule << Range << DiffType;
9660  };
9661
9662  auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9663                       &ComputeQualTypeODRHash, &ComputeODRHash](
9664                          NamedDecl *FirstRecord, StringRef FirstModule,
9665                          StringRef SecondModule, FieldDecl *FirstField,
9666                          FieldDecl *SecondField) {
9667    IdentifierInfo *FirstII = FirstField->getIdentifier();
9668    IdentifierInfo *SecondII = SecondField->getIdentifier();
9669    if (FirstII->getName() != SecondII->getName()) {
9670      ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9671                       FirstField->getSourceRange(), FieldName)
9672          << FirstII;
9673      ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9674                      SecondField->getSourceRange(), FieldName)
9675          << SecondII;
9676
9677      return true;
9678    }
9679
9680    assert(getContext().hasSameType(FirstField->getType(),
9681                                    SecondField->getType()));
9682
9683    QualType FirstType = FirstField->getType();
9684    QualType SecondType = SecondField->getType();
9685    if (ComputeQualTypeODRHash(FirstType) !=
9686        ComputeQualTypeODRHash(SecondType)) {
9687      ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9688                       FirstField->getSourceRange(), FieldTypeName)
9689          << FirstII << FirstType;
9690      ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9691                      SecondField->getSourceRange(), FieldTypeName)
9692          << SecondII << SecondType;
9693
9694      return true;
9695    }
9696
9697    const bool IsFirstBitField = FirstField->isBitField();
9698    const bool IsSecondBitField = SecondField->isBitField();
9699    if (IsFirstBitField != IsSecondBitField) {
9700      ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9701                       FirstField->getSourceRange(), FieldSingleBitField)
9702          << FirstII << IsFirstBitField;
9703      ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9704                      SecondField->getSourceRange(), FieldSingleBitField)
9705          << SecondII << IsSecondBitField;
9706      return true;
9707    }
9708
9709    if (IsFirstBitField && IsSecondBitField) {
9710      unsigned FirstBitWidthHash =
9711          ComputeODRHash(FirstField->getBitWidth());
9712      unsigned SecondBitWidthHash =
9713          ComputeODRHash(SecondField->getBitWidth());
9714      if (FirstBitWidthHash != SecondBitWidthHash) {
9715        ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9716                         FirstField->getSourceRange(),
9717                         FieldDifferentWidthBitField)
9718            << FirstII << FirstField->getBitWidth()->getSourceRange();
9719        ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9720                        SecondField->getSourceRange(),
9721                        FieldDifferentWidthBitField)
9722            << SecondII << SecondField->getBitWidth()->getSourceRange();
9723        return true;
9724      }
9725    }
9726
9727    if (!PP.getLangOpts().CPlusPlus)
9728      return false;
9729
9730    const bool IsFirstMutable = FirstField->isMutable();
9731    const bool IsSecondMutable = SecondField->isMutable();
9732    if (IsFirstMutable != IsSecondMutable) {
9733      ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9734                       FirstField->getSourceRange(), FieldSingleMutable)
9735          << FirstII << IsFirstMutable;
9736      ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9737                      SecondField->getSourceRange(), FieldSingleMutable)
9738          << SecondII << IsSecondMutable;
9739      return true;
9740    }
9741
9742    const Expr *FirstInitializer = FirstField->getInClassInitializer();
9743    const Expr *SecondInitializer = SecondField->getInClassInitializer();
9744    if ((!FirstInitializer && SecondInitializer) ||
9745        (FirstInitializer && !SecondInitializer)) {
9746      ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9747                       FirstField->getSourceRange(), FieldSingleInitializer)
9748          << FirstII << (FirstInitializer != nullptr);
9749      ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9750                      SecondField->getSourceRange(), FieldSingleInitializer)
9751          << SecondII << (SecondInitializer != nullptr);
9752      return true;
9753    }
9754
9755    if (FirstInitializer && SecondInitializer) {
9756      unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9757      unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9758      if (FirstInitHash != SecondInitHash) {
9759        ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9760                         FirstField->getSourceRange(),
9761                         FieldDifferentInitializers)
9762            << FirstII << FirstInitializer->getSourceRange();
9763        ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9764                        SecondField->getSourceRange(),
9765                        FieldDifferentInitializers)
9766            << SecondII << SecondInitializer->getSourceRange();
9767        return true;
9768      }
9769    }
9770
9771    return false;
9772  };
9773
9774  auto ODRDiagTypeDefOrAlias =
9775      [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9776          NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9777          TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9778          bool IsTypeAlias) {
9779        auto FirstName = FirstTD->getDeclName();
9780        auto SecondName = SecondTD->getDeclName();
9781        if (FirstName != SecondName) {
9782          ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9783                           FirstTD->getSourceRange(), TypedefName)
9784              << IsTypeAlias << FirstName;
9785          ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9786                          SecondTD->getSourceRange(), TypedefName)
9787              << IsTypeAlias << SecondName;
9788          return true;
9789        }
9790
9791        QualType FirstType = FirstTD->getUnderlyingType();
9792        QualType SecondType = SecondTD->getUnderlyingType();
9793        if (ComputeQualTypeODRHash(FirstType) !=
9794            ComputeQualTypeODRHash(SecondType)) {
9795          ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9796                           FirstTD->getSourceRange(), TypedefType)
9797              << IsTypeAlias << FirstName << FirstType;
9798          ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9799                          SecondTD->getSourceRange(), TypedefType)
9800              << IsTypeAlias << SecondName << SecondType;
9801          return true;
9802        }
9803
9804        return false;
9805  };
9806
9807  auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9808                     &ComputeQualTypeODRHash, &ComputeODRHash,
9809                     this](NamedDecl *FirstRecord, StringRef FirstModule,
9810                           StringRef SecondModule, VarDecl *FirstVD,
9811                           VarDecl *SecondVD) {
9812    auto FirstName = FirstVD->getDeclName();
9813    auto SecondName = SecondVD->getDeclName();
9814    if (FirstName != SecondName) {
9815      ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9816                       FirstVD->getSourceRange(), VarName)
9817          << FirstName;
9818      ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9819                      SecondVD->getSourceRange(), VarName)
9820          << SecondName;
9821      return true;
9822    }
9823
9824    QualType FirstType = FirstVD->getType();
9825    QualType SecondType = SecondVD->getType();
9826    if (ComputeQualTypeODRHash(FirstType) !=
9827        ComputeQualTypeODRHash(SecondType)) {
9828      ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9829                       FirstVD->getSourceRange(), VarType)
9830          << FirstName << FirstType;
9831      ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9832                      SecondVD->getSourceRange(), VarType)
9833          << SecondName << SecondType;
9834      return true;
9835    }
9836
9837    if (!PP.getLangOpts().CPlusPlus)
9838      return false;
9839
9840    const Expr *FirstInit = FirstVD->getInit();
9841    const Expr *SecondInit = SecondVD->getInit();
9842    if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9843      ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9844                       FirstVD->getSourceRange(), VarSingleInitializer)
9845          << FirstName << (FirstInit == nullptr)
9846          << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9847      ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9848                      SecondVD->getSourceRange(), VarSingleInitializer)
9849          << SecondName << (SecondInit == nullptr)
9850          << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9851      return true;
9852    }
9853
9854    if (FirstInit && SecondInit &&
9855        ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9856      ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9857                       FirstVD->getSourceRange(), VarDifferentInitializer)
9858          << FirstName << FirstInit->getSourceRange();
9859      ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9860                      SecondVD->getSourceRange(), VarDifferentInitializer)
9861          << SecondName << SecondInit->getSourceRange();
9862      return true;
9863    }
9864
9865    const bool FirstIsConstexpr = FirstVD->isConstexpr();
9866    const bool SecondIsConstexpr = SecondVD->isConstexpr();
9867    if (FirstIsConstexpr != SecondIsConstexpr) {
9868      ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9869                       FirstVD->getSourceRange(), VarConstexpr)
9870          << FirstName << FirstIsConstexpr;
9871      ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9872                      SecondVD->getSourceRange(), VarConstexpr)
9873          << SecondName << SecondIsConstexpr;
9874      return true;
9875    }
9876    return false;
9877  };
9878
9879  auto DifferenceSelector = [](Decl *D) {
9880    assert(D && "valid Decl required");
9881    switch (D->getKind()) {
9882    default:
9883      return Other;
9884    case Decl::AccessSpec:
9885      switch (D->getAccess()) {
9886      case AS_public:
9887        return PublicSpecifer;
9888      case AS_private:
9889        return PrivateSpecifer;
9890      case AS_protected:
9891        return ProtectedSpecifer;
9892      case AS_none:
9893        break;
9894      }
9895      llvm_unreachable("Invalid access specifier");
9896    case Decl::StaticAssert:
9897      return StaticAssert;
9898    case Decl::Field:
9899      return Field;
9900    case Decl::CXXMethod:
9901    case Decl::CXXConstructor:
9902    case Decl::CXXDestructor:
9903      return CXXMethod;
9904    case Decl::TypeAlias:
9905      return TypeAlias;
9906    case Decl::Typedef:
9907      return TypeDef;
9908    case Decl::Var:
9909      return Var;
9910    case Decl::Friend:
9911      return Friend;
9912    case Decl::FunctionTemplate:
9913      return FunctionTemplate;
9914    }
9915  };
9916
9917  using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9918  auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9919                                                 RecordDecl *Record,
9920                                                 const DeclContext *DC) {
9921    for (auto *D : Record->decls()) {
9922      if (!ODRHash::isDeclToBeProcessed(D, DC))
9923        continue;
9924      Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9925    }
9926  };
9927
9928  struct DiffResult {
9929    Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9930    ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9931  };
9932
9933  // If there is a diagnoseable difference, FirstDiffType and
9934  // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9935  // filled in if not EndOfClass.
9936  auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9937                                             DeclHashes &SecondHashes) {
9938    DiffResult DR;
9939    auto FirstIt = FirstHashes.begin();
9940    auto SecondIt = SecondHashes.begin();
9941    while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9942      if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9943          FirstIt->second == SecondIt->second) {
9944        ++FirstIt;
9945        ++SecondIt;
9946        continue;
9947      }
9948
9949      DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9950      DR.SecondDecl =
9951          SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9952
9953      DR.FirstDiffType =
9954          DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9955      DR.SecondDiffType =
9956          DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9957      return DR;
9958    }
9959    return DR;
9960  };
9961
9962  // Use this to diagnose that an unexpected Decl was encountered
9963  // or no difference was detected. This causes a generic error
9964  // message to be emitted.
9965  auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9966                                      StringRef FirstModule,
9967                                      NamedDecl *SecondRecord,
9968                                      StringRef SecondModule) {
9969    Diag(FirstRecord->getLocation(),
9970         diag::err_module_odr_violation_different_definitions)
9971        << FirstRecord << FirstModule.empty() << FirstModule;
9972
9973    if (DR.FirstDecl) {
9974      Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9975          << FirstRecord << DR.FirstDecl->getSourceRange();
9976    }
9977
9978    Diag(SecondRecord->getLocation(),
9979         diag::note_module_odr_violation_different_definitions)
9980        << SecondModule;
9981
9982    if (DR.SecondDecl) {
9983      Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9984          << DR.SecondDecl->getSourceRange();
9985    }
9986  };
9987
9988  auto DiagnoseODRMismatch =
9989      [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9990             NamedDecl *SecondRecord, StringRef SecondModule) {
9991        SourceLocation FirstLoc;
9992        SourceRange FirstRange;
9993        auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9994        if (DR.FirstDiffType == EndOfClass && FirstTag) {
9995          FirstLoc = FirstTag->getBraceRange().getEnd();
9996        } else {
9997          FirstLoc = DR.FirstDecl->getLocation();
9998          FirstRange = DR.FirstDecl->getSourceRange();
9999        }
10000        Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10001            << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10002            << DR.FirstDiffType;
10003
10004        SourceLocation SecondLoc;
10005        SourceRange SecondRange;
10006        auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10007        if (DR.SecondDiffType == EndOfClass && SecondTag) {
10008          SecondLoc = SecondTag->getBraceRange().getEnd();
10009        } else {
10010          SecondLoc = DR.SecondDecl->getLocation();
10011          SecondRange = DR.SecondDecl->getSourceRange();
10012        }
10013        Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10014            << SecondModule << SecondRange << DR.SecondDiffType;
10015      };
10016
10017  // Issue any pending ODR-failure diagnostics.
10018  for (auto &Merge : OdrMergeFailures) {
10019    // If we've already pointed out a specific problem with this class, don't
10020    // bother issuing a general "something's different" diagnostic.
10021    if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10022      continue;
10023
10024    bool Diagnosed = false;
10025    CXXRecordDecl *FirstRecord = Merge.first;
10026    std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10027    for (auto &RecordPair : Merge.second) {
10028      CXXRecordDecl *SecondRecord = RecordPair.first;
10029      // Multiple different declarations got merged together; tell the user
10030      // where they came from.
10031      if (FirstRecord == SecondRecord)
10032        continue;
10033
10034      std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10035
10036      auto *FirstDD = FirstRecord->DefinitionData;
10037      auto *SecondDD = RecordPair.second;
10038
10039      assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10040
10041      // Diagnostics from DefinitionData are emitted here.
10042      if (FirstDD != SecondDD) {
10043        enum ODRDefinitionDataDifference {
10044          NumBases,
10045          NumVBases,
10046          BaseType,
10047          BaseVirtual,
10048          BaseAccess,
10049        };
10050        auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10051                                 this](SourceLocation Loc, SourceRange Range,
10052                                       ODRDefinitionDataDifference DiffType) {
10053          return Diag(Loc, diag::err_module_odr_violation_definition_data)
10054                 << FirstRecord << FirstModule.empty() << FirstModule << Range
10055                 << DiffType;
10056        };
10057        auto ODRDiagBaseNote = [&SecondModule,
10058                                this](SourceLocation Loc, SourceRange Range,
10059                                      ODRDefinitionDataDifference DiffType) {
10060          return Diag(Loc, diag::note_module_odr_violation_definition_data)
10061                 << SecondModule << Range << DiffType;
10062        };
10063
10064        unsigned FirstNumBases = FirstDD->NumBases;
10065        unsigned FirstNumVBases = FirstDD->NumVBases;
10066        unsigned SecondNumBases = SecondDD->NumBases;
10067        unsigned SecondNumVBases = SecondDD->NumVBases;
10068
10069        auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10070          unsigned NumBases = DD->NumBases;
10071          if (NumBases == 0) return SourceRange();
10072          auto bases = DD->bases();
10073          return SourceRange(bases[0].getBeginLoc(),
10074                             bases[NumBases - 1].getEndLoc());
10075        };
10076
10077        if (FirstNumBases != SecondNumBases) {
10078          ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10079                           NumBases)
10080              << FirstNumBases;
10081          ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10082                          NumBases)
10083              << SecondNumBases;
10084          Diagnosed = true;
10085          break;
10086        }
10087
10088        if (FirstNumVBases != SecondNumVBases) {
10089          ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10090                           NumVBases)
10091              << FirstNumVBases;
10092          ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10093                          NumVBases)
10094              << SecondNumVBases;
10095          Diagnosed = true;
10096          break;
10097        }
10098
10099        auto FirstBases = FirstDD->bases();
10100        auto SecondBases = SecondDD->bases();
10101        unsigned i = 0;
10102        for (i = 0; i < FirstNumBases; ++i) {
10103          auto FirstBase = FirstBases[i];
10104          auto SecondBase = SecondBases[i];
10105          if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10106              ComputeQualTypeODRHash(SecondBase.getType())) {
10107            ODRDiagBaseError(FirstRecord->getLocation(),
10108                             FirstBase.getSourceRange(), BaseType)
10109                << (i + 1) << FirstBase.getType();
10110            ODRDiagBaseNote(SecondRecord->getLocation(),
10111                            SecondBase.getSourceRange(), BaseType)
10112                << (i + 1) << SecondBase.getType();
10113            break;
10114          }
10115
10116          if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10117            ODRDiagBaseError(FirstRecord->getLocation(),
10118                             FirstBase.getSourceRange(), BaseVirtual)
10119                << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10120            ODRDiagBaseNote(SecondRecord->getLocation(),
10121                            SecondBase.getSourceRange(), BaseVirtual)
10122                << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10123            break;
10124          }
10125
10126          if (FirstBase.getAccessSpecifierAsWritten() !=
10127              SecondBase.getAccessSpecifierAsWritten()) {
10128            ODRDiagBaseError(FirstRecord->getLocation(),
10129                             FirstBase.getSourceRange(), BaseAccess)
10130                << (i + 1) << FirstBase.getType()
10131                << (int)FirstBase.getAccessSpecifierAsWritten();
10132            ODRDiagBaseNote(SecondRecord->getLocation(),
10133                            SecondBase.getSourceRange(), BaseAccess)
10134                << (i + 1) << SecondBase.getType()
10135                << (int)SecondBase.getAccessSpecifierAsWritten();
10136            break;
10137          }
10138        }
10139
10140        if (i != FirstNumBases) {
10141          Diagnosed = true;
10142          break;
10143        }
10144      }
10145
10146      const ClassTemplateDecl *FirstTemplate =
10147          FirstRecord->getDescribedClassTemplate();
10148      const ClassTemplateDecl *SecondTemplate =
10149          SecondRecord->getDescribedClassTemplate();
10150
10151      assert(!FirstTemplate == !SecondTemplate &&
10152             "Both pointers should be null or non-null");
10153
10154      enum ODRTemplateDifference {
10155        ParamEmptyName,
10156        ParamName,
10157        ParamSingleDefaultArgument,
10158        ParamDifferentDefaultArgument,
10159      };
10160
10161      if (FirstTemplate && SecondTemplate) {
10162        DeclHashes FirstTemplateHashes;
10163        DeclHashes SecondTemplateHashes;
10164
10165        auto PopulateTemplateParameterHashs =
10166            [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10167                                     const ClassTemplateDecl *TD) {
10168              for (auto *D : TD->getTemplateParameters()->asArray()) {
10169                Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10170              }
10171            };
10172
10173        PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10174        PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10175
10176        assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10177               "Number of template parameters should be equal.");
10178
10179        auto FirstIt = FirstTemplateHashes.begin();
10180        auto FirstEnd = FirstTemplateHashes.end();
10181        auto SecondIt = SecondTemplateHashes.begin();
10182        for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10183          if (FirstIt->second == SecondIt->second)
10184            continue;
10185
10186          auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10187                                          SourceLocation Loc, SourceRange Range,
10188                                          ODRTemplateDifference DiffType) {
10189            return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10190                   << FirstRecord << FirstModule.empty() << FirstModule << Range
10191                   << DiffType;
10192          };
10193          auto ODRDiagTemplateNote = [&SecondModule, this](
10194                                         SourceLocation Loc, SourceRange Range,
10195                                         ODRTemplateDifference DiffType) {
10196            return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10197                   << SecondModule << Range << DiffType;
10198          };
10199
10200          const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10201          const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10202
10203          assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10204                 "Parameter Decl's should be the same kind.");
10205
10206          DeclarationName FirstName = FirstDecl->getDeclName();
10207          DeclarationName SecondName = SecondDecl->getDeclName();
10208
10209          if (FirstName != SecondName) {
10210            const bool FirstNameEmpty =
10211                FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10212            const bool SecondNameEmpty =
10213                SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10214            assert((!FirstNameEmpty || !SecondNameEmpty) &&
10215                   "Both template parameters cannot be unnamed.");
10216            ODRDiagTemplateError(FirstDecl->getLocation(),
10217                                 FirstDecl->getSourceRange(),
10218                                 FirstNameEmpty ? ParamEmptyName : ParamName)
10219                << FirstName;
10220            ODRDiagTemplateNote(SecondDecl->getLocation(),
10221                                SecondDecl->getSourceRange(),
10222                                SecondNameEmpty ? ParamEmptyName : ParamName)
10223                << SecondName;
10224            break;
10225          }
10226
10227          switch (FirstDecl->getKind()) {
10228          default:
10229            llvm_unreachable("Invalid template parameter type.");
10230          case Decl::TemplateTypeParm: {
10231            const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10232            const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10233            const bool HasFirstDefaultArgument =
10234                FirstParam->hasDefaultArgument() &&
10235                !FirstParam->defaultArgumentWasInherited();
10236            const bool HasSecondDefaultArgument =
10237                SecondParam->hasDefaultArgument() &&
10238                !SecondParam->defaultArgumentWasInherited();
10239
10240            if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10241              ODRDiagTemplateError(FirstDecl->getLocation(),
10242                                   FirstDecl->getSourceRange(),
10243                                   ParamSingleDefaultArgument)
10244                  << HasFirstDefaultArgument;
10245              ODRDiagTemplateNote(SecondDecl->getLocation(),
10246                                  SecondDecl->getSourceRange(),
10247                                  ParamSingleDefaultArgument)
10248                  << HasSecondDefaultArgument;
10249              break;
10250            }
10251
10252            assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10253                   "Expecting default arguments.");
10254
10255            ODRDiagTemplateError(FirstDecl->getLocation(),
10256                                 FirstDecl->getSourceRange(),
10257                                 ParamDifferentDefaultArgument);
10258            ODRDiagTemplateNote(SecondDecl->getLocation(),
10259                                SecondDecl->getSourceRange(),
10260                                ParamDifferentDefaultArgument);
10261
10262            break;
10263          }
10264          case Decl::NonTypeTemplateParm: {
10265            const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10266            const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10267            const bool HasFirstDefaultArgument =
10268                FirstParam->hasDefaultArgument() &&
10269                !FirstParam->defaultArgumentWasInherited();
10270            const bool HasSecondDefaultArgument =
10271                SecondParam->hasDefaultArgument() &&
10272                !SecondParam->defaultArgumentWasInherited();
10273
10274            if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10275              ODRDiagTemplateError(FirstDecl->getLocation(),
10276                                   FirstDecl->getSourceRange(),
10277                                   ParamSingleDefaultArgument)
10278                  << HasFirstDefaultArgument;
10279              ODRDiagTemplateNote(SecondDecl->getLocation(),
10280                                  SecondDecl->getSourceRange(),
10281                                  ParamSingleDefaultArgument)
10282                  << HasSecondDefaultArgument;
10283              break;
10284            }
10285
10286            assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10287                   "Expecting default arguments.");
10288
10289            ODRDiagTemplateError(FirstDecl->getLocation(),
10290                                 FirstDecl->getSourceRange(),
10291                                 ParamDifferentDefaultArgument);
10292            ODRDiagTemplateNote(SecondDecl->getLocation(),
10293                                SecondDecl->getSourceRange(),
10294                                ParamDifferentDefaultArgument);
10295
10296            break;
10297          }
10298          case Decl::TemplateTemplateParm: {
10299            const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10300            const auto *SecondParam =
10301                cast<TemplateTemplateParmDecl>(SecondDecl);
10302            const bool HasFirstDefaultArgument =
10303                FirstParam->hasDefaultArgument() &&
10304                !FirstParam->defaultArgumentWasInherited();
10305            const bool HasSecondDefaultArgument =
10306                SecondParam->hasDefaultArgument() &&
10307                !SecondParam->defaultArgumentWasInherited();
10308
10309            if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10310              ODRDiagTemplateError(FirstDecl->getLocation(),
10311                                   FirstDecl->getSourceRange(),
10312                                   ParamSingleDefaultArgument)
10313                  << HasFirstDefaultArgument;
10314              ODRDiagTemplateNote(SecondDecl->getLocation(),
10315                                  SecondDecl->getSourceRange(),
10316                                  ParamSingleDefaultArgument)
10317                  << HasSecondDefaultArgument;
10318              break;
10319            }
10320
10321            assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10322                   "Expecting default arguments.");
10323
10324            ODRDiagTemplateError(FirstDecl->getLocation(),
10325                                 FirstDecl->getSourceRange(),
10326                                 ParamDifferentDefaultArgument);
10327            ODRDiagTemplateNote(SecondDecl->getLocation(),
10328                                SecondDecl->getSourceRange(),
10329                                ParamDifferentDefaultArgument);
10330
10331            break;
10332          }
10333          }
10334
10335          break;
10336        }
10337
10338        if (FirstIt != FirstEnd) {
10339          Diagnosed = true;
10340          break;
10341        }
10342      }
10343
10344      DeclHashes FirstHashes;
10345      DeclHashes SecondHashes;
10346      const DeclContext *DC = FirstRecord;
10347      PopulateHashes(FirstHashes, FirstRecord, DC);
10348      PopulateHashes(SecondHashes, SecondRecord, DC);
10349
10350      auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10351      ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10352      ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10353      Decl *FirstDecl = DR.FirstDecl;
10354      Decl *SecondDecl = DR.SecondDecl;
10355
10356      if (FirstDiffType == Other || SecondDiffType == Other) {
10357        DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10358                              SecondModule);
10359        Diagnosed = true;
10360        break;
10361      }
10362
10363      if (FirstDiffType != SecondDiffType) {
10364        DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10365                            SecondModule);
10366        Diagnosed = true;
10367        break;
10368      }
10369
10370      assert(FirstDiffType == SecondDiffType);
10371
10372      switch (FirstDiffType) {
10373      case Other:
10374      case EndOfClass:
10375      case PublicSpecifer:
10376      case PrivateSpecifer:
10377      case ProtectedSpecifer:
10378        llvm_unreachable("Invalid diff type");
10379
10380      case StaticAssert: {
10381        StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10382        StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10383
10384        Expr *FirstExpr = FirstSA->getAssertExpr();
10385        Expr *SecondExpr = SecondSA->getAssertExpr();
10386        unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10387        unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10388        if (FirstODRHash != SecondODRHash) {
10389          ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10390                           FirstExpr->getSourceRange(), StaticAssertCondition);
10391          ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10392                          SecondExpr->getSourceRange(), StaticAssertCondition);
10393          Diagnosed = true;
10394          break;
10395        }
10396
10397        StringLiteral *FirstStr = FirstSA->getMessage();
10398        StringLiteral *SecondStr = SecondSA->getMessage();
10399        assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10400        if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10401          SourceLocation FirstLoc, SecondLoc;
10402          SourceRange FirstRange, SecondRange;
10403          if (FirstStr) {
10404            FirstLoc = FirstStr->getBeginLoc();
10405            FirstRange = FirstStr->getSourceRange();
10406          } else {
10407            FirstLoc = FirstSA->getBeginLoc();
10408            FirstRange = FirstSA->getSourceRange();
10409          }
10410          if (SecondStr) {
10411            SecondLoc = SecondStr->getBeginLoc();
10412            SecondRange = SecondStr->getSourceRange();
10413          } else {
10414            SecondLoc = SecondSA->getBeginLoc();
10415            SecondRange = SecondSA->getSourceRange();
10416          }
10417          ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10418                           StaticAssertOnlyMessage)
10419              << (FirstStr == nullptr);
10420          ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10421                          StaticAssertOnlyMessage)
10422              << (SecondStr == nullptr);
10423          Diagnosed = true;
10424          break;
10425        }
10426
10427        if (FirstStr && SecondStr &&
10428            FirstStr->getString() != SecondStr->getString()) {
10429          ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10430                           FirstStr->getSourceRange(), StaticAssertMessage);
10431          ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10432                          SecondStr->getSourceRange(), StaticAssertMessage);
10433          Diagnosed = true;
10434          break;
10435        }
10436        break;
10437      }
10438      case Field: {
10439        Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10440                                 cast<FieldDecl>(FirstDecl),
10441                                 cast<FieldDecl>(SecondDecl));
10442        break;
10443      }
10444      case CXXMethod: {
10445        enum {
10446          DiagMethod,
10447          DiagConstructor,
10448          DiagDestructor,
10449        } FirstMethodType,
10450            SecondMethodType;
10451        auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10452          if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10453          if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10454          return DiagMethod;
10455        };
10456        const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10457        const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10458        FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10459        SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10460        auto FirstName = FirstMethod->getDeclName();
10461        auto SecondName = SecondMethod->getDeclName();
10462        if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10463          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10464                           FirstMethod->getSourceRange(), MethodName)
10465              << FirstMethodType << FirstName;
10466          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10467                          SecondMethod->getSourceRange(), MethodName)
10468              << SecondMethodType << SecondName;
10469
10470          Diagnosed = true;
10471          break;
10472        }
10473
10474        const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10475        const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10476        if (FirstDeleted != SecondDeleted) {
10477          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10478                           FirstMethod->getSourceRange(), MethodDeleted)
10479              << FirstMethodType << FirstName << FirstDeleted;
10480
10481          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10482                          SecondMethod->getSourceRange(), MethodDeleted)
10483              << SecondMethodType << SecondName << SecondDeleted;
10484          Diagnosed = true;
10485          break;
10486        }
10487
10488        const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10489        const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10490        if (FirstDefaulted != SecondDefaulted) {
10491          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10492                           FirstMethod->getSourceRange(), MethodDefaulted)
10493              << FirstMethodType << FirstName << FirstDefaulted;
10494
10495          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10496                          SecondMethod->getSourceRange(), MethodDefaulted)
10497              << SecondMethodType << SecondName << SecondDefaulted;
10498          Diagnosed = true;
10499          break;
10500        }
10501
10502        const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10503        const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10504        const bool FirstPure = FirstMethod->isPure();
10505        const bool SecondPure = SecondMethod->isPure();
10506        if ((FirstVirtual || SecondVirtual) &&
10507            (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10508          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10509                           FirstMethod->getSourceRange(), MethodVirtual)
10510              << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10511          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10512                          SecondMethod->getSourceRange(), MethodVirtual)
10513              << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10514          Diagnosed = true;
10515          break;
10516        }
10517
10518        // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10519        // FirstDecl is the canonical Decl of SecondDecl, so the storage
10520        // class needs to be checked instead.
10521        const auto FirstStorage = FirstMethod->getStorageClass();
10522        const auto SecondStorage = SecondMethod->getStorageClass();
10523        const bool FirstStatic = FirstStorage == SC_Static;
10524        const bool SecondStatic = SecondStorage == SC_Static;
10525        if (FirstStatic != SecondStatic) {
10526          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10527                           FirstMethod->getSourceRange(), MethodStatic)
10528              << FirstMethodType << FirstName << FirstStatic;
10529          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10530                          SecondMethod->getSourceRange(), MethodStatic)
10531              << SecondMethodType << SecondName << SecondStatic;
10532          Diagnosed = true;
10533          break;
10534        }
10535
10536        const bool FirstVolatile = FirstMethod->isVolatile();
10537        const bool SecondVolatile = SecondMethod->isVolatile();
10538        if (FirstVolatile != SecondVolatile) {
10539          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10540                           FirstMethod->getSourceRange(), MethodVolatile)
10541              << FirstMethodType << FirstName << FirstVolatile;
10542          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10543                          SecondMethod->getSourceRange(), MethodVolatile)
10544              << SecondMethodType << SecondName << SecondVolatile;
10545          Diagnosed = true;
10546          break;
10547        }
10548
10549        const bool FirstConst = FirstMethod->isConst();
10550        const bool SecondConst = SecondMethod->isConst();
10551        if (FirstConst != SecondConst) {
10552          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10553                           FirstMethod->getSourceRange(), MethodConst)
10554              << FirstMethodType << FirstName << FirstConst;
10555          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10556                          SecondMethod->getSourceRange(), MethodConst)
10557              << SecondMethodType << SecondName << SecondConst;
10558          Diagnosed = true;
10559          break;
10560        }
10561
10562        const bool FirstInline = FirstMethod->isInlineSpecified();
10563        const bool SecondInline = SecondMethod->isInlineSpecified();
10564        if (FirstInline != SecondInline) {
10565          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10566                           FirstMethod->getSourceRange(), MethodInline)
10567              << FirstMethodType << FirstName << FirstInline;
10568          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10569                          SecondMethod->getSourceRange(), MethodInline)
10570              << SecondMethodType << SecondName << SecondInline;
10571          Diagnosed = true;
10572          break;
10573        }
10574
10575        const unsigned FirstNumParameters = FirstMethod->param_size();
10576        const unsigned SecondNumParameters = SecondMethod->param_size();
10577        if (FirstNumParameters != SecondNumParameters) {
10578          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10579                           FirstMethod->getSourceRange(),
10580                           MethodNumberParameters)
10581              << FirstMethodType << FirstName << FirstNumParameters;
10582          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10583                          SecondMethod->getSourceRange(),
10584                          MethodNumberParameters)
10585              << SecondMethodType << SecondName << SecondNumParameters;
10586          Diagnosed = true;
10587          break;
10588        }
10589
10590        // Need this status boolean to know when break out of the switch.
10591        bool ParameterMismatch = false;
10592        for (unsigned I = 0; I < FirstNumParameters; ++I) {
10593          const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10594          const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10595
10596          QualType FirstParamType = FirstParam->getType();
10597          QualType SecondParamType = SecondParam->getType();
10598          if (FirstParamType != SecondParamType &&
10599              ComputeQualTypeODRHash(FirstParamType) !=
10600                  ComputeQualTypeODRHash(SecondParamType)) {
10601            if (const DecayedType *ParamDecayedType =
10602                    FirstParamType->getAs<DecayedType>()) {
10603              ODRDiagDeclError(
10604                  FirstRecord, FirstModule, FirstMethod->getLocation(),
10605                  FirstMethod->getSourceRange(), MethodParameterType)
10606                  << FirstMethodType << FirstName << (I + 1) << FirstParamType
10607                  << true << ParamDecayedType->getOriginalType();
10608            } else {
10609              ODRDiagDeclError(
10610                  FirstRecord, FirstModule, FirstMethod->getLocation(),
10611                  FirstMethod->getSourceRange(), MethodParameterType)
10612                  << FirstMethodType << FirstName << (I + 1) << FirstParamType
10613                  << false;
10614            }
10615
10616            if (const DecayedType *ParamDecayedType =
10617                    SecondParamType->getAs<DecayedType>()) {
10618              ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10619                              SecondMethod->getSourceRange(),
10620                              MethodParameterType)
10621                  << SecondMethodType << SecondName << (I + 1)
10622                  << SecondParamType << true
10623                  << ParamDecayedType->getOriginalType();
10624            } else {
10625              ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10626                              SecondMethod->getSourceRange(),
10627                              MethodParameterType)
10628                  << SecondMethodType << SecondName << (I + 1)
10629                  << SecondParamType << false;
10630            }
10631            ParameterMismatch = true;
10632            break;
10633          }
10634
10635          DeclarationName FirstParamName = FirstParam->getDeclName();
10636          DeclarationName SecondParamName = SecondParam->getDeclName();
10637          if (FirstParamName != SecondParamName) {
10638            ODRDiagDeclError(FirstRecord, FirstModule,
10639                             FirstMethod->getLocation(),
10640                             FirstMethod->getSourceRange(), MethodParameterName)
10641                << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10642            ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10643                            SecondMethod->getSourceRange(), MethodParameterName)
10644                << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10645            ParameterMismatch = true;
10646            break;
10647          }
10648
10649          const Expr *FirstInit = FirstParam->getInit();
10650          const Expr *SecondInit = SecondParam->getInit();
10651          if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10652            ODRDiagDeclError(FirstRecord, FirstModule,
10653                             FirstMethod->getLocation(),
10654                             FirstMethod->getSourceRange(),
10655                             MethodParameterSingleDefaultArgument)
10656                << FirstMethodType << FirstName << (I + 1)
10657                << (FirstInit == nullptr)
10658                << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10659            ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10660                            SecondMethod->getSourceRange(),
10661                            MethodParameterSingleDefaultArgument)
10662                << SecondMethodType << SecondName << (I + 1)
10663                << (SecondInit == nullptr)
10664                << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10665            ParameterMismatch = true;
10666            break;
10667          }
10668
10669          if (FirstInit && SecondInit &&
10670              ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10671            ODRDiagDeclError(FirstRecord, FirstModule,
10672                             FirstMethod->getLocation(),
10673                             FirstMethod->getSourceRange(),
10674                             MethodParameterDifferentDefaultArgument)
10675                << FirstMethodType << FirstName << (I + 1)
10676                << FirstInit->getSourceRange();
10677            ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10678                            SecondMethod->getSourceRange(),
10679                            MethodParameterDifferentDefaultArgument)
10680                << SecondMethodType << SecondName << (I + 1)
10681                << SecondInit->getSourceRange();
10682            ParameterMismatch = true;
10683            break;
10684
10685          }
10686        }
10687
10688        if (ParameterMismatch) {
10689          Diagnosed = true;
10690          break;
10691        }
10692
10693        const auto *FirstTemplateArgs =
10694            FirstMethod->getTemplateSpecializationArgs();
10695        const auto *SecondTemplateArgs =
10696            SecondMethod->getTemplateSpecializationArgs();
10697
10698        if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10699            (!FirstTemplateArgs && SecondTemplateArgs)) {
10700          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10701                           FirstMethod->getSourceRange(),
10702                           MethodNoTemplateArguments)
10703              << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10704          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10705                          SecondMethod->getSourceRange(),
10706                          MethodNoTemplateArguments)
10707              << SecondMethodType << SecondName
10708              << (SecondTemplateArgs != nullptr);
10709
10710          Diagnosed = true;
10711          break;
10712        }
10713
10714        if (FirstTemplateArgs && SecondTemplateArgs) {
10715          // Remove pack expansions from argument list.
10716          auto ExpandTemplateArgumentList =
10717              [](const TemplateArgumentList *TAL) {
10718                llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10719                for (const TemplateArgument &TA : TAL->asArray()) {
10720                  if (TA.getKind() != TemplateArgument::Pack) {
10721                    ExpandedList.push_back(&TA);
10722                    continue;
10723                  }
10724                  for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10725                    ExpandedList.push_back(&PackTA);
10726                  }
10727                }
10728                return ExpandedList;
10729              };
10730          llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10731              ExpandTemplateArgumentList(FirstTemplateArgs);
10732          llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10733              ExpandTemplateArgumentList(SecondTemplateArgs);
10734
10735          if (FirstExpandedList.size() != SecondExpandedList.size()) {
10736            ODRDiagDeclError(FirstRecord, FirstModule,
10737                             FirstMethod->getLocation(),
10738                             FirstMethod->getSourceRange(),
10739                             MethodDifferentNumberTemplateArguments)
10740                << FirstMethodType << FirstName
10741                << (unsigned)FirstExpandedList.size();
10742            ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10743                            SecondMethod->getSourceRange(),
10744                            MethodDifferentNumberTemplateArguments)
10745                << SecondMethodType << SecondName
10746                << (unsigned)SecondExpandedList.size();
10747
10748            Diagnosed = true;
10749            break;
10750          }
10751
10752          bool TemplateArgumentMismatch = false;
10753          for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10754            const TemplateArgument &FirstTA = *FirstExpandedList[i],
10755                                   &SecondTA = *SecondExpandedList[i];
10756            if (ComputeTemplateArgumentODRHash(FirstTA) ==
10757                ComputeTemplateArgumentODRHash(SecondTA)) {
10758              continue;
10759            }
10760
10761            ODRDiagDeclError(
10762                FirstRecord, FirstModule, FirstMethod->getLocation(),
10763                FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10764                << FirstMethodType << FirstName << FirstTA << i + 1;
10765            ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10766                            SecondMethod->getSourceRange(),
10767                            MethodDifferentTemplateArgument)
10768                << SecondMethodType << SecondName << SecondTA << i + 1;
10769
10770            TemplateArgumentMismatch = true;
10771            break;
10772          }
10773
10774          if (TemplateArgumentMismatch) {
10775            Diagnosed = true;
10776            break;
10777          }
10778        }
10779
10780        // Compute the hash of the method as if it has no body.
10781        auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10782          Hash.clear();
10783          Hash.AddFunctionDecl(D, true /*SkipBody*/);
10784          return Hash.CalculateHash();
10785        };
10786
10787        // Compare the hash generated to the hash stored.  A difference means
10788        // that a body was present in the original source.  Due to merging,
10789        // the stardard way of detecting a body will not work.
10790        const bool HasFirstBody =
10791            ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10792        const bool HasSecondBody =
10793            ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10794
10795        if (HasFirstBody != HasSecondBody) {
10796          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10797                           FirstMethod->getSourceRange(), MethodSingleBody)
10798              << FirstMethodType << FirstName << HasFirstBody;
10799          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10800                          SecondMethod->getSourceRange(), MethodSingleBody)
10801              << SecondMethodType << SecondName << HasSecondBody;
10802          Diagnosed = true;
10803          break;
10804        }
10805
10806        if (HasFirstBody && HasSecondBody) {
10807          ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10808                           FirstMethod->getSourceRange(), MethodDifferentBody)
10809              << FirstMethodType << FirstName;
10810          ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10811                          SecondMethod->getSourceRange(), MethodDifferentBody)
10812              << SecondMethodType << SecondName;
10813          Diagnosed = true;
10814          break;
10815        }
10816
10817        break;
10818      }
10819      case TypeAlias:
10820      case TypeDef: {
10821        Diagnosed = ODRDiagTypeDefOrAlias(
10822            FirstRecord, FirstModule, SecondModule,
10823            cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10824            FirstDiffType == TypeAlias);
10825        break;
10826      }
10827      case Var: {
10828        Diagnosed =
10829            ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10830                       cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10831        break;
10832      }
10833      case Friend: {
10834        FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10835        FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10836
10837        NamedDecl *FirstND = FirstFriend->getFriendDecl();
10838        NamedDecl *SecondND = SecondFriend->getFriendDecl();
10839
10840        TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10841        TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10842
10843        if (FirstND && SecondND) {
10844          ODRDiagDeclError(FirstRecord, FirstModule,
10845                           FirstFriend->getFriendLoc(),
10846                           FirstFriend->getSourceRange(), FriendFunction)
10847              << FirstND;
10848          ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10849                          SecondFriend->getSourceRange(), FriendFunction)
10850              << SecondND;
10851
10852          Diagnosed = true;
10853          break;
10854        }
10855
10856        if (FirstTSI && SecondTSI) {
10857          QualType FirstFriendType = FirstTSI->getType();
10858          QualType SecondFriendType = SecondTSI->getType();
10859          assert(ComputeQualTypeODRHash(FirstFriendType) !=
10860                 ComputeQualTypeODRHash(SecondFriendType));
10861          ODRDiagDeclError(FirstRecord, FirstModule,
10862                           FirstFriend->getFriendLoc(),
10863                           FirstFriend->getSourceRange(), FriendType)
10864              << FirstFriendType;
10865          ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10866                          SecondFriend->getSourceRange(), FriendType)
10867              << SecondFriendType;
10868          Diagnosed = true;
10869          break;
10870        }
10871
10872        ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10873                         FirstFriend->getSourceRange(), FriendTypeFunction)
10874            << (FirstTSI == nullptr);
10875        ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10876                        SecondFriend->getSourceRange(), FriendTypeFunction)
10877            << (SecondTSI == nullptr);
10878
10879        Diagnosed = true;
10880        break;
10881      }
10882      case FunctionTemplate: {
10883        FunctionTemplateDecl *FirstTemplate =
10884            cast<FunctionTemplateDecl>(FirstDecl);
10885        FunctionTemplateDecl *SecondTemplate =
10886            cast<FunctionTemplateDecl>(SecondDecl);
10887
10888        TemplateParameterList *FirstTPL =
10889            FirstTemplate->getTemplateParameters();
10890        TemplateParameterList *SecondTPL =
10891            SecondTemplate->getTemplateParameters();
10892
10893        if (FirstTPL->size() != SecondTPL->size()) {
10894          ODRDiagDeclError(FirstRecord, FirstModule,
10895                           FirstTemplate->getLocation(),
10896                           FirstTemplate->getSourceRange(),
10897                           FunctionTemplateDifferentNumberParameters)
10898              << FirstTemplate << FirstTPL->size();
10899          ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10900                          SecondTemplate->getSourceRange(),
10901                          FunctionTemplateDifferentNumberParameters)
10902              << SecondTemplate << SecondTPL->size();
10903
10904          Diagnosed = true;
10905          break;
10906        }
10907
10908        bool ParameterMismatch = false;
10909        for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10910          NamedDecl *FirstParam = FirstTPL->getParam(i);
10911          NamedDecl *SecondParam = SecondTPL->getParam(i);
10912
10913          if (FirstParam->getKind() != SecondParam->getKind()) {
10914            enum {
10915              TemplateTypeParameter,
10916              NonTypeTemplateParameter,
10917              TemplateTemplateParameter,
10918            };
10919            auto GetParamType = [](NamedDecl *D) {
10920              switch (D->getKind()) {
10921                default:
10922                  llvm_unreachable("Unexpected template parameter type");
10923                case Decl::TemplateTypeParm:
10924                  return TemplateTypeParameter;
10925                case Decl::NonTypeTemplateParm:
10926                  return NonTypeTemplateParameter;
10927                case Decl::TemplateTemplateParm:
10928                  return TemplateTemplateParameter;
10929              }
10930            };
10931
10932            ODRDiagDeclError(FirstRecord, FirstModule,
10933                             FirstTemplate->getLocation(),
10934                             FirstTemplate->getSourceRange(),
10935                             FunctionTemplateParameterDifferentKind)
10936                << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10937            ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10938                            SecondTemplate->getSourceRange(),
10939                            FunctionTemplateParameterDifferentKind)
10940                << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10941
10942            ParameterMismatch = true;
10943            break;
10944          }
10945
10946          if (FirstParam->getName() != SecondParam->getName()) {
10947            ODRDiagDeclError(
10948                FirstRecord, FirstModule, FirstTemplate->getLocation(),
10949                FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10950                << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10951                << FirstParam;
10952            ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10953                            SecondTemplate->getSourceRange(),
10954                            FunctionTemplateParameterName)
10955                << SecondTemplate << (i + 1)
10956                << (bool)SecondParam->getIdentifier() << SecondParam;
10957            ParameterMismatch = true;
10958            break;
10959          }
10960
10961          if (isa<TemplateTypeParmDecl>(FirstParam) &&
10962              isa<TemplateTypeParmDecl>(SecondParam)) {
10963            TemplateTypeParmDecl *FirstTTPD =
10964                cast<TemplateTypeParmDecl>(FirstParam);
10965            TemplateTypeParmDecl *SecondTTPD =
10966                cast<TemplateTypeParmDecl>(SecondParam);
10967            bool HasFirstDefaultArgument =
10968                FirstTTPD->hasDefaultArgument() &&
10969                !FirstTTPD->defaultArgumentWasInherited();
10970            bool HasSecondDefaultArgument =
10971                SecondTTPD->hasDefaultArgument() &&
10972                !SecondTTPD->defaultArgumentWasInherited();
10973            if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10974              ODRDiagDeclError(FirstRecord, FirstModule,
10975                               FirstTemplate->getLocation(),
10976                               FirstTemplate->getSourceRange(),
10977                               FunctionTemplateParameterSingleDefaultArgument)
10978                  << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10979              ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10980                              SecondTemplate->getSourceRange(),
10981                              FunctionTemplateParameterSingleDefaultArgument)
10982                  << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10983              ParameterMismatch = true;
10984              break;
10985            }
10986
10987            if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10988              QualType FirstType = FirstTTPD->getDefaultArgument();
10989              QualType SecondType = SecondTTPD->getDefaultArgument();
10990              if (ComputeQualTypeODRHash(FirstType) !=
10991                  ComputeQualTypeODRHash(SecondType)) {
10992                ODRDiagDeclError(
10993                    FirstRecord, FirstModule, FirstTemplate->getLocation(),
10994                    FirstTemplate->getSourceRange(),
10995                    FunctionTemplateParameterDifferentDefaultArgument)
10996                    << FirstTemplate << (i + 1) << FirstType;
10997                ODRDiagDeclNote(
10998                    SecondModule, SecondTemplate->getLocation(),
10999                    SecondTemplate->getSourceRange(),
11000                    FunctionTemplateParameterDifferentDefaultArgument)
11001                    << SecondTemplate << (i + 1) << SecondType;
11002                ParameterMismatch = true;
11003                break;
11004              }
11005            }
11006
11007            if (FirstTTPD->isParameterPack() !=
11008                SecondTTPD->isParameterPack()) {
11009              ODRDiagDeclError(FirstRecord, FirstModule,
11010                               FirstTemplate->getLocation(),
11011                               FirstTemplate->getSourceRange(),
11012                               FunctionTemplatePackParameter)
11013                  << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11014              ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11015                              SecondTemplate->getSourceRange(),
11016                              FunctionTemplatePackParameter)
11017                  << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11018              ParameterMismatch = true;
11019              break;
11020            }
11021          }
11022
11023          if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11024              isa<TemplateTemplateParmDecl>(SecondParam)) {
11025            TemplateTemplateParmDecl *FirstTTPD =
11026                cast<TemplateTemplateParmDecl>(FirstParam);
11027            TemplateTemplateParmDecl *SecondTTPD =
11028                cast<TemplateTemplateParmDecl>(SecondParam);
11029
11030            TemplateParameterList *FirstTPL =
11031                FirstTTPD->getTemplateParameters();
11032            TemplateParameterList *SecondTPL =
11033                SecondTTPD->getTemplateParameters();
11034
11035            if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11036                ComputeTemplateParameterListODRHash(SecondTPL)) {
11037              ODRDiagDeclError(FirstRecord, FirstModule,
11038                               FirstTemplate->getLocation(),
11039                               FirstTemplate->getSourceRange(),
11040                               FunctionTemplateParameterDifferentType)
11041                  << FirstTemplate << (i + 1);
11042              ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11043                              SecondTemplate->getSourceRange(),
11044                              FunctionTemplateParameterDifferentType)
11045                  << SecondTemplate << (i + 1);
11046              ParameterMismatch = true;
11047              break;
11048            }
11049
11050            bool HasFirstDefaultArgument =
11051                FirstTTPD->hasDefaultArgument() &&
11052                !FirstTTPD->defaultArgumentWasInherited();
11053            bool HasSecondDefaultArgument =
11054                SecondTTPD->hasDefaultArgument() &&
11055                !SecondTTPD->defaultArgumentWasInherited();
11056            if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11057              ODRDiagDeclError(FirstRecord, FirstModule,
11058                               FirstTemplate->getLocation(),
11059                               FirstTemplate->getSourceRange(),
11060                               FunctionTemplateParameterSingleDefaultArgument)
11061                  << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11062              ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11063                              SecondTemplate->getSourceRange(),
11064                              FunctionTemplateParameterSingleDefaultArgument)
11065                  << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11066              ParameterMismatch = true;
11067              break;
11068            }
11069
11070            if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11071              TemplateArgument FirstTA =
11072                  FirstTTPD->getDefaultArgument().getArgument();
11073              TemplateArgument SecondTA =
11074                  SecondTTPD->getDefaultArgument().getArgument();
11075              if (ComputeTemplateArgumentODRHash(FirstTA) !=
11076                  ComputeTemplateArgumentODRHash(SecondTA)) {
11077                ODRDiagDeclError(
11078                    FirstRecord, FirstModule, FirstTemplate->getLocation(),
11079                    FirstTemplate->getSourceRange(),
11080                    FunctionTemplateParameterDifferentDefaultArgument)
11081                    << FirstTemplate << (i + 1) << FirstTA;
11082                ODRDiagDeclNote(
11083                    SecondModule, SecondTemplate->getLocation(),
11084                    SecondTemplate->getSourceRange(),
11085                    FunctionTemplateParameterDifferentDefaultArgument)
11086                    << SecondTemplate << (i + 1) << SecondTA;
11087                ParameterMismatch = true;
11088                break;
11089              }
11090            }
11091
11092            if (FirstTTPD->isParameterPack() !=
11093                SecondTTPD->isParameterPack()) {
11094              ODRDiagDeclError(FirstRecord, FirstModule,
11095                               FirstTemplate->getLocation(),
11096                               FirstTemplate->getSourceRange(),
11097                               FunctionTemplatePackParameter)
11098                  << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11099              ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11100                              SecondTemplate->getSourceRange(),
11101                              FunctionTemplatePackParameter)
11102                  << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11103              ParameterMismatch = true;
11104              break;
11105            }
11106          }
11107
11108          if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11109              isa<NonTypeTemplateParmDecl>(SecondParam)) {
11110            NonTypeTemplateParmDecl *FirstNTTPD =
11111                cast<NonTypeTemplateParmDecl>(FirstParam);
11112            NonTypeTemplateParmDecl *SecondNTTPD =
11113                cast<NonTypeTemplateParmDecl>(SecondParam);
11114
11115            QualType FirstType = FirstNTTPD->getType();
11116            QualType SecondType = SecondNTTPD->getType();
11117            if (ComputeQualTypeODRHash(FirstType) !=
11118                ComputeQualTypeODRHash(SecondType)) {
11119              ODRDiagDeclError(FirstRecord, FirstModule,
11120                               FirstTemplate->getLocation(),
11121                               FirstTemplate->getSourceRange(),
11122                               FunctionTemplateParameterDifferentType)
11123                  << FirstTemplate << (i + 1);
11124              ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11125                              SecondTemplate->getSourceRange(),
11126                              FunctionTemplateParameterDifferentType)
11127                  << SecondTemplate << (i + 1);
11128              ParameterMismatch = true;
11129              break;
11130            }
11131
11132            bool HasFirstDefaultArgument =
11133                FirstNTTPD->hasDefaultArgument() &&
11134                !FirstNTTPD->defaultArgumentWasInherited();
11135            bool HasSecondDefaultArgument =
11136                SecondNTTPD->hasDefaultArgument() &&
11137                !SecondNTTPD->defaultArgumentWasInherited();
11138            if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11139              ODRDiagDeclError(FirstRecord, FirstModule,
11140                               FirstTemplate->getLocation(),
11141                               FirstTemplate->getSourceRange(),
11142                               FunctionTemplateParameterSingleDefaultArgument)
11143                  << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11144              ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11145                              SecondTemplate->getSourceRange(),
11146                              FunctionTemplateParameterSingleDefaultArgument)
11147                  << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11148              ParameterMismatch = true;
11149              break;
11150            }
11151
11152            if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11153              Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11154              Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11155              if (ComputeODRHash(FirstDefaultArgument) !=
11156                  ComputeODRHash(SecondDefaultArgument)) {
11157                ODRDiagDeclError(
11158                    FirstRecord, FirstModule, FirstTemplate->getLocation(),
11159                    FirstTemplate->getSourceRange(),
11160                    FunctionTemplateParameterDifferentDefaultArgument)
11161                    << FirstTemplate << (i + 1) << FirstDefaultArgument;
11162                ODRDiagDeclNote(
11163                    SecondModule, SecondTemplate->getLocation(),
11164                    SecondTemplate->getSourceRange(),
11165                    FunctionTemplateParameterDifferentDefaultArgument)
11166                    << SecondTemplate << (i + 1) << SecondDefaultArgument;
11167                ParameterMismatch = true;
11168                break;
11169              }
11170            }
11171
11172            if (FirstNTTPD->isParameterPack() !=
11173                SecondNTTPD->isParameterPack()) {
11174              ODRDiagDeclError(FirstRecord, FirstModule,
11175                               FirstTemplate->getLocation(),
11176                               FirstTemplate->getSourceRange(),
11177                               FunctionTemplatePackParameter)
11178                  << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11179              ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11180                              SecondTemplate->getSourceRange(),
11181                              FunctionTemplatePackParameter)
11182                  << SecondTemplate << (i + 1)
11183                  << SecondNTTPD->isParameterPack();
11184              ParameterMismatch = true;
11185              break;
11186            }
11187          }
11188        }
11189
11190        if (ParameterMismatch) {
11191          Diagnosed = true;
11192          break;
11193        }
11194
11195        break;
11196      }
11197      }
11198
11199      if (Diagnosed)
11200        continue;
11201
11202      Diag(FirstDecl->getLocation(),
11203           diag::err_module_odr_violation_mismatch_decl_unknown)
11204          << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11205          << FirstDecl->getSourceRange();
11206      Diag(SecondDecl->getLocation(),
11207           diag::note_module_odr_violation_mismatch_decl_unknown)
11208          << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11209      Diagnosed = true;
11210    }
11211
11212    if (!Diagnosed) {
11213      // All definitions are updates to the same declaration. This happens if a
11214      // module instantiates the declaration of a class template specialization
11215      // and two or more other modules instantiate its definition.
11216      //
11217      // FIXME: Indicate which modules had instantiations of this definition.
11218      // FIXME: How can this even happen?
11219      Diag(Merge.first->getLocation(),
11220           diag::err_module_odr_violation_different_instantiations)
11221        << Merge.first;
11222    }
11223  }
11224
11225  // Issue ODR failures diagnostics for functions.
11226  for (auto &Merge : FunctionOdrMergeFailures) {
11227    enum ODRFunctionDifference {
11228      ReturnType,
11229      ParameterName,
11230      ParameterType,
11231      ParameterSingleDefaultArgument,
11232      ParameterDifferentDefaultArgument,
11233      FunctionBody,
11234    };
11235
11236    FunctionDecl *FirstFunction = Merge.first;
11237    std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11238
11239    bool Diagnosed = false;
11240    for (auto &SecondFunction : Merge.second) {
11241
11242      if (FirstFunction == SecondFunction)
11243        continue;
11244
11245      std::string SecondModule =
11246          getOwningModuleNameForDiagnostic(SecondFunction);
11247
11248      auto ODRDiagError = [FirstFunction, &FirstModule,
11249                           this](SourceLocation Loc, SourceRange Range,
11250                                 ODRFunctionDifference DiffType) {
11251        return Diag(Loc, diag::err_module_odr_violation_function)
11252               << FirstFunction << FirstModule.empty() << FirstModule << Range
11253               << DiffType;
11254      };
11255      auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11256                                               SourceRange Range,
11257                                               ODRFunctionDifference DiffType) {
11258        return Diag(Loc, diag::note_module_odr_violation_function)
11259               << SecondModule << Range << DiffType;
11260      };
11261
11262      if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11263          ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11264        ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11265                     FirstFunction->getReturnTypeSourceRange(), ReturnType)
11266            << FirstFunction->getReturnType();
11267        ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11268                    SecondFunction->getReturnTypeSourceRange(), ReturnType)
11269            << SecondFunction->getReturnType();
11270        Diagnosed = true;
11271        break;
11272      }
11273
11274      assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11275             "Merged functions with different number of parameters");
11276
11277      auto ParamSize = FirstFunction->param_size();
11278      bool ParameterMismatch = false;
11279      for (unsigned I = 0; I < ParamSize; ++I) {
11280        auto *FirstParam = FirstFunction->getParamDecl(I);
11281        auto *SecondParam = SecondFunction->getParamDecl(I);
11282
11283        assert(getContext().hasSameType(FirstParam->getType(),
11284                                      SecondParam->getType()) &&
11285               "Merged function has different parameter types.");
11286
11287        if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11288          ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11289                       ParameterName)
11290              << I + 1 << FirstParam->getDeclName();
11291          ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11292                      ParameterName)
11293              << I + 1 << SecondParam->getDeclName();
11294          ParameterMismatch = true;
11295          break;
11296        };
11297
11298        QualType FirstParamType = FirstParam->getType();
11299        QualType SecondParamType = SecondParam->getType();
11300        if (FirstParamType != SecondParamType &&
11301            ComputeQualTypeODRHash(FirstParamType) !=
11302                ComputeQualTypeODRHash(SecondParamType)) {
11303          if (const DecayedType *ParamDecayedType =
11304                  FirstParamType->getAs<DecayedType>()) {
11305            ODRDiagError(FirstParam->getLocation(),
11306                         FirstParam->getSourceRange(), ParameterType)
11307                << (I + 1) << FirstParamType << true
11308                << ParamDecayedType->getOriginalType();
11309          } else {
11310            ODRDiagError(FirstParam->getLocation(),
11311                         FirstParam->getSourceRange(), ParameterType)
11312                << (I + 1) << FirstParamType << false;
11313          }
11314
11315          if (const DecayedType *ParamDecayedType =
11316                  SecondParamType->getAs<DecayedType>()) {
11317            ODRDiagNote(SecondParam->getLocation(),
11318                        SecondParam->getSourceRange(), ParameterType)
11319                << (I + 1) << SecondParamType << true
11320                << ParamDecayedType->getOriginalType();
11321          } else {
11322            ODRDiagNote(SecondParam->getLocation(),
11323                        SecondParam->getSourceRange(), ParameterType)
11324                << (I + 1) << SecondParamType << false;
11325          }
11326          ParameterMismatch = true;
11327          break;
11328        }
11329
11330        const Expr *FirstInit = FirstParam->getInit();
11331        const Expr *SecondInit = SecondParam->getInit();
11332        if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11333          ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11334                       ParameterSingleDefaultArgument)
11335              << (I + 1) << (FirstInit == nullptr)
11336              << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11337          ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11338                      ParameterSingleDefaultArgument)
11339              << (I + 1) << (SecondInit == nullptr)
11340              << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11341          ParameterMismatch = true;
11342          break;
11343        }
11344
11345        if (FirstInit && SecondInit &&
11346            ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11347          ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11348                       ParameterDifferentDefaultArgument)
11349              << (I + 1) << FirstInit->getSourceRange();
11350          ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11351                      ParameterDifferentDefaultArgument)
11352              << (I + 1) << SecondInit->getSourceRange();
11353          ParameterMismatch = true;
11354          break;
11355        }
11356
11357        assert(ComputeSubDeclODRHash(FirstParam) ==
11358                   ComputeSubDeclODRHash(SecondParam) &&
11359               "Undiagnosed parameter difference.");
11360      }
11361
11362      if (ParameterMismatch) {
11363        Diagnosed = true;
11364        break;
11365      }
11366
11367      // If no error has been generated before now, assume the problem is in
11368      // the body and generate a message.
11369      ODRDiagError(FirstFunction->getLocation(),
11370                   FirstFunction->getSourceRange(), FunctionBody);
11371      ODRDiagNote(SecondFunction->getLocation(),
11372                  SecondFunction->getSourceRange(), FunctionBody);
11373      Diagnosed = true;
11374      break;
11375    }
11376    (void)Diagnosed;
11377    assert(Diagnosed && "Unable to emit ODR diagnostic.");
11378  }
11379
11380  // Issue ODR failures diagnostics for enums.
11381  for (auto &Merge : EnumOdrMergeFailures) {
11382    enum ODREnumDifference {
11383      SingleScopedEnum,
11384      EnumTagKeywordMismatch,
11385      SingleSpecifiedType,
11386      DifferentSpecifiedTypes,
11387      DifferentNumberEnumConstants,
11388      EnumConstantName,
11389      EnumConstantSingleInitilizer,
11390      EnumConstantDifferentInitilizer,
11391    };
11392
11393    // If we've already pointed out a specific problem with this enum, don't
11394    // bother issuing a general "something's different" diagnostic.
11395    if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11396      continue;
11397
11398    EnumDecl *FirstEnum = Merge.first;
11399    std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11400
11401    using DeclHashes =
11402        llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11403    auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11404                              DeclHashes &Hashes, EnumDecl *Enum) {
11405      for (auto *D : Enum->decls()) {
11406        // Due to decl merging, the first EnumDecl is the parent of
11407        // Decls in both records.
11408        if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11409          continue;
11410        assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11411        Hashes.emplace_back(cast<EnumConstantDecl>(D),
11412                            ComputeSubDeclODRHash(D));
11413      }
11414    };
11415    DeclHashes FirstHashes;
11416    PopulateHashes(FirstHashes, FirstEnum);
11417    bool Diagnosed = false;
11418    for (auto &SecondEnum : Merge.second) {
11419
11420      if (FirstEnum == SecondEnum)
11421        continue;
11422
11423      std::string SecondModule =
11424          getOwningModuleNameForDiagnostic(SecondEnum);
11425
11426      auto ODRDiagError = [FirstEnum, &FirstModule,
11427                           this](SourceLocation Loc, SourceRange Range,
11428                                 ODREnumDifference DiffType) {
11429        return Diag(Loc, diag::err_module_odr_violation_enum)
11430               << FirstEnum << FirstModule.empty() << FirstModule << Range
11431               << DiffType;
11432      };
11433      auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11434                                               SourceRange Range,
11435                                               ODREnumDifference DiffType) {
11436        return Diag(Loc, diag::note_module_odr_violation_enum)
11437               << SecondModule << Range << DiffType;
11438      };
11439
11440      if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11441        ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11442                     SingleScopedEnum)
11443            << FirstEnum->isScoped();
11444        ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11445                    SingleScopedEnum)
11446            << SecondEnum->isScoped();
11447        Diagnosed = true;
11448        continue;
11449      }
11450
11451      if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11452        if (FirstEnum->isScopedUsingClassTag() !=
11453            SecondEnum->isScopedUsingClassTag()) {
11454          ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11455                       EnumTagKeywordMismatch)
11456              << FirstEnum->isScopedUsingClassTag();
11457          ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11458                      EnumTagKeywordMismatch)
11459              << SecondEnum->isScopedUsingClassTag();
11460          Diagnosed = true;
11461          continue;
11462        }
11463      }
11464
11465      QualType FirstUnderlyingType =
11466          FirstEnum->getIntegerTypeSourceInfo()
11467              ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11468              : QualType();
11469      QualType SecondUnderlyingType =
11470          SecondEnum->getIntegerTypeSourceInfo()
11471              ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11472              : QualType();
11473      if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11474          ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11475                       SingleSpecifiedType)
11476              << !FirstUnderlyingType.isNull();
11477          ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11478                      SingleSpecifiedType)
11479              << !SecondUnderlyingType.isNull();
11480          Diagnosed = true;
11481          continue;
11482      }
11483
11484      if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11485        if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11486            ComputeQualTypeODRHash(SecondUnderlyingType)) {
11487          ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11488                       DifferentSpecifiedTypes)
11489              << FirstUnderlyingType;
11490          ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11491                      DifferentSpecifiedTypes)
11492              << SecondUnderlyingType;
11493          Diagnosed = true;
11494          continue;
11495        }
11496      }
11497
11498      DeclHashes SecondHashes;
11499      PopulateHashes(SecondHashes, SecondEnum);
11500
11501      if (FirstHashes.size() != SecondHashes.size()) {
11502        ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11503                     DifferentNumberEnumConstants)
11504            << (int)FirstHashes.size();
11505        ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11506                    DifferentNumberEnumConstants)
11507            << (int)SecondHashes.size();
11508        Diagnosed = true;
11509        continue;
11510      }
11511
11512      for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11513        if (FirstHashes[I].second == SecondHashes[I].second)
11514          continue;
11515        const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11516        const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11517
11518        if (FirstEnumConstant->getDeclName() !=
11519            SecondEnumConstant->getDeclName()) {
11520
11521          ODRDiagError(FirstEnumConstant->getLocation(),
11522                       FirstEnumConstant->getSourceRange(), EnumConstantName)
11523              << I + 1 << FirstEnumConstant;
11524          ODRDiagNote(SecondEnumConstant->getLocation(),
11525                      SecondEnumConstant->getSourceRange(), EnumConstantName)
11526              << I + 1 << SecondEnumConstant;
11527          Diagnosed = true;
11528          break;
11529        }
11530
11531        const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11532        const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11533        if (!FirstInit && !SecondInit)
11534          continue;
11535
11536        if (!FirstInit || !SecondInit) {
11537          ODRDiagError(FirstEnumConstant->getLocation(),
11538                       FirstEnumConstant->getSourceRange(),
11539                       EnumConstantSingleInitilizer)
11540              << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11541          ODRDiagNote(SecondEnumConstant->getLocation(),
11542                      SecondEnumConstant->getSourceRange(),
11543                      EnumConstantSingleInitilizer)
11544              << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11545          Diagnosed = true;
11546          break;
11547        }
11548
11549        if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11550          ODRDiagError(FirstEnumConstant->getLocation(),
11551                       FirstEnumConstant->getSourceRange(),
11552                       EnumConstantDifferentInitilizer)
11553              << I + 1 << FirstEnumConstant;
11554          ODRDiagNote(SecondEnumConstant->getLocation(),
11555                      SecondEnumConstant->getSourceRange(),
11556                      EnumConstantDifferentInitilizer)
11557              << I + 1 << SecondEnumConstant;
11558          Diagnosed = true;
11559          break;
11560        }
11561      }
11562    }
11563
11564    (void)Diagnosed;
11565    assert(Diagnosed && "Unable to emit ODR diagnostic.");
11566  }
11567}
11568
11569void ASTReader::StartedDeserializing() {
11570  if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11571    ReadTimer->startTimer();
11572}
11573
11574void ASTReader::FinishedDeserializing() {
11575  assert(NumCurrentElementsDeserializing &&
11576         "FinishedDeserializing not paired with StartedDeserializing");
11577  if (NumCurrentElementsDeserializing == 1) {
11578    // We decrease NumCurrentElementsDeserializing only after pending actions
11579    // are finished, to avoid recursively re-calling finishPendingActions().
11580    finishPendingActions();
11581  }
11582  --NumCurrentElementsDeserializing;
11583
11584  if (NumCurrentElementsDeserializing == 0) {
11585    // Propagate exception specification and deduced type updates along
11586    // redeclaration chains.
11587    //
11588    // We do this now rather than in finishPendingActions because we want to
11589    // be able to walk the complete redeclaration chains of the updated decls.
11590    while (!PendingExceptionSpecUpdates.empty() ||
11591           !PendingDeducedTypeUpdates.empty()) {
11592      auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11593      PendingExceptionSpecUpdates.clear();
11594      for (auto Update : ESUpdates) {
11595        ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11596        auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11597        auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11598        if (auto *Listener = getContext().getASTMutationListener())
11599          Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11600        for (auto *Redecl : Update.second->redecls())
11601          getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11602      }
11603
11604      auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11605      PendingDeducedTypeUpdates.clear();
11606      for (auto Update : DTUpdates) {
11607        ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11608        // FIXME: If the return type is already deduced, check that it matches.
11609        getContext().adjustDeducedFunctionResultType(Update.first,
11610                                                     Update.second);
11611      }
11612    }
11613
11614    if (ReadTimer)
11615      ReadTimer->stopTimer();
11616
11617    diagnoseOdrViolations();
11618
11619    // We are not in recursive loading, so it's safe to pass the "interesting"
11620    // decls to the consumer.
11621    if (Consumer)
11622      PassInterestingDeclsToConsumer();
11623  }
11624}
11625
11626void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11627  if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11628    // Remove any fake results before adding any real ones.
11629    auto It = PendingFakeLookupResults.find(II);
11630    if (It != PendingFakeLookupResults.end()) {
11631      for (auto *ND : It->second)
11632        SemaObj->IdResolver.RemoveDecl(ND);
11633      // FIXME: this works around module+PCH performance issue.
11634      // Rather than erase the result from the map, which is O(n), just clear
11635      // the vector of NamedDecls.
11636      It->second.clear();
11637    }
11638  }
11639
11640  if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11641    SemaObj->TUScope->AddDecl(D);
11642  } else if (SemaObj->TUScope) {
11643    // Adding the decl to IdResolver may have failed because it was already in
11644    // (even though it was not added in scope). If it is already in, make sure
11645    // it gets in the scope as well.
11646    if (std::find(SemaObj->IdResolver.begin(Name),
11647                  SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11648      SemaObj->TUScope->AddDecl(D);
11649  }
11650}
11651
11652ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11653                     ASTContext *Context,
11654                     const PCHContainerReader &PCHContainerRdr,
11655                     ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11656                     StringRef isysroot, bool DisableValidation,
11657                     bool AllowASTWithCompilerErrors,
11658                     bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11659                     bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11660                     std::unique_ptr<llvm::Timer> ReadTimer)
11661    : Listener(DisableValidation
11662                   ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11663                   : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11664      SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11665      PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11666      ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11667                                     PCHContainerRdr, PP.getHeaderSearchInfo()),
11668      DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11669      DisableValidation(DisableValidation),
11670      AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11671      AllowConfigurationMismatch(AllowConfigurationMismatch),
11672      ValidateSystemInputs(ValidateSystemInputs),
11673      ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11674      UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11675  SourceMgr.setExternalSLocEntrySource(this);
11676
11677  for (const auto &Ext : Extensions) {
11678    auto BlockName = Ext->getExtensionMetadata().BlockName;
11679    auto Known = ModuleFileExtensions.find(BlockName);
11680    if (Known != ModuleFileExtensions.end()) {
11681      Diags.Report(diag::warn_duplicate_module_file_extension)
11682        << BlockName;
11683      continue;
11684    }
11685
11686    ModuleFileExtensions.insert({BlockName, Ext});
11687  }
11688}
11689
11690ASTReader::~ASTReader() {
11691  if (OwnsDeserializationListener)
11692    delete DeserializationListener;
11693}
11694
11695IdentifierResolver &ASTReader::getIdResolver() {
11696  return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11697}
11698
11699Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11700                                               unsigned AbbrevID) {
11701  Idx = 0;
11702  Record.clear();
11703  return Cursor.readRecord(AbbrevID, Record);
11704}
11705//===----------------------------------------------------------------------===//
11706//// OMPClauseReader implementation
11707////===----------------------------------------------------------------------===//
11708
11709// This has to be in namespace clang because it's friended by all
11710// of the OMP clauses.
11711namespace clang {
11712
11713class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11714  ASTRecordReader &Record;
11715  ASTContext &Context;
11716
11717public:
11718  OMPClauseReader(ASTRecordReader &Record)
11719      : Record(Record), Context(Record.getContext()) {}
11720
11721#define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11722#include "llvm/Frontend/OpenMP/OMPKinds.def"
11723  OMPClause *readClause();
11724  void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11725  void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11726};
11727
11728} // end namespace clang
11729
11730OMPClause *ASTRecordReader::readOMPClause() {
11731  return OMPClauseReader(*this).readClause();
11732}
11733
11734OMPClause *OMPClauseReader::readClause() {
11735  OMPClause *C = nullptr;
11736  switch (llvm::omp::Clause(Record.readInt())) {
11737  case llvm::omp::OMPC_if:
11738    C = new (Context) OMPIfClause();
11739    break;
11740  case llvm::omp::OMPC_final:
11741    C = new (Context) OMPFinalClause();
11742    break;
11743  case llvm::omp::OMPC_num_threads:
11744    C = new (Context) OMPNumThreadsClause();
11745    break;
11746  case llvm::omp::OMPC_safelen:
11747    C = new (Context) OMPSafelenClause();
11748    break;
11749  case llvm::omp::OMPC_simdlen:
11750    C = new (Context) OMPSimdlenClause();
11751    break;
11752  case llvm::omp::OMPC_allocator:
11753    C = new (Context) OMPAllocatorClause();
11754    break;
11755  case llvm::omp::OMPC_collapse:
11756    C = new (Context) OMPCollapseClause();
11757    break;
11758  case llvm::omp::OMPC_default:
11759    C = new (Context) OMPDefaultClause();
11760    break;
11761  case llvm::omp::OMPC_proc_bind:
11762    C = new (Context) OMPProcBindClause();
11763    break;
11764  case llvm::omp::OMPC_schedule:
11765    C = new (Context) OMPScheduleClause();
11766    break;
11767  case llvm::omp::OMPC_ordered:
11768    C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11769    break;
11770  case llvm::omp::OMPC_nowait:
11771    C = new (Context) OMPNowaitClause();
11772    break;
11773  case llvm::omp::OMPC_untied:
11774    C = new (Context) OMPUntiedClause();
11775    break;
11776  case llvm::omp::OMPC_mergeable:
11777    C = new (Context) OMPMergeableClause();
11778    break;
11779  case llvm::omp::OMPC_read:
11780    C = new (Context) OMPReadClause();
11781    break;
11782  case llvm::omp::OMPC_write:
11783    C = new (Context) OMPWriteClause();
11784    break;
11785  case llvm::omp::OMPC_update:
11786    C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11787    break;
11788  case llvm::omp::OMPC_capture:
11789    C = new (Context) OMPCaptureClause();
11790    break;
11791  case llvm::omp::OMPC_seq_cst:
11792    C = new (Context) OMPSeqCstClause();
11793    break;
11794  case llvm::omp::OMPC_acq_rel:
11795    C = new (Context) OMPAcqRelClause();
11796    break;
11797  case llvm::omp::OMPC_acquire:
11798    C = new (Context) OMPAcquireClause();
11799    break;
11800  case llvm::omp::OMPC_release:
11801    C = new (Context) OMPReleaseClause();
11802    break;
11803  case llvm::omp::OMPC_relaxed:
11804    C = new (Context) OMPRelaxedClause();
11805    break;
11806  case llvm::omp::OMPC_threads:
11807    C = new (Context) OMPThreadsClause();
11808    break;
11809  case llvm::omp::OMPC_simd:
11810    C = new (Context) OMPSIMDClause();
11811    break;
11812  case llvm::omp::OMPC_nogroup:
11813    C = new (Context) OMPNogroupClause();
11814    break;
11815  case llvm::omp::OMPC_unified_address:
11816    C = new (Context) OMPUnifiedAddressClause();
11817    break;
11818  case llvm::omp::OMPC_unified_shared_memory:
11819    C = new (Context) OMPUnifiedSharedMemoryClause();
11820    break;
11821  case llvm::omp::OMPC_reverse_offload:
11822    C = new (Context) OMPReverseOffloadClause();
11823    break;
11824  case llvm::omp::OMPC_dynamic_allocators:
11825    C = new (Context) OMPDynamicAllocatorsClause();
11826    break;
11827  case llvm::omp::OMPC_atomic_default_mem_order:
11828    C = new (Context) OMPAtomicDefaultMemOrderClause();
11829    break;
11830 case llvm::omp::OMPC_private:
11831    C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11832    break;
11833  case llvm::omp::OMPC_firstprivate:
11834    C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11835    break;
11836  case llvm::omp::OMPC_lastprivate:
11837    C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11838    break;
11839  case llvm::omp::OMPC_shared:
11840    C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11841    break;
11842  case llvm::omp::OMPC_reduction: {
11843    unsigned N = Record.readInt();
11844    auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11845    C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11846    break;
11847  }
11848  case llvm::omp::OMPC_task_reduction:
11849    C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11850    break;
11851  case llvm::omp::OMPC_in_reduction:
11852    C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11853    break;
11854  case llvm::omp::OMPC_linear:
11855    C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11856    break;
11857  case llvm::omp::OMPC_aligned:
11858    C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11859    break;
11860  case llvm::omp::OMPC_copyin:
11861    C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11862    break;
11863  case llvm::omp::OMPC_copyprivate:
11864    C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11865    break;
11866  case llvm::omp::OMPC_flush:
11867    C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11868    break;
11869  case llvm::omp::OMPC_depobj:
11870    C = OMPDepobjClause::CreateEmpty(Context);
11871    break;
11872  case llvm::omp::OMPC_depend: {
11873    unsigned NumVars = Record.readInt();
11874    unsigned NumLoops = Record.readInt();
11875    C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11876    break;
11877  }
11878  case llvm::omp::OMPC_device:
11879    C = new (Context) OMPDeviceClause();
11880    break;
11881  case llvm::omp::OMPC_map: {
11882    OMPMappableExprListSizeTy Sizes;
11883    Sizes.NumVars = Record.readInt();
11884    Sizes.NumUniqueDeclarations = Record.readInt();
11885    Sizes.NumComponentLists = Record.readInt();
11886    Sizes.NumComponents = Record.readInt();
11887    C = OMPMapClause::CreateEmpty(Context, Sizes);
11888    break;
11889  }
11890  case llvm::omp::OMPC_num_teams:
11891    C = new (Context) OMPNumTeamsClause();
11892    break;
11893  case llvm::omp::OMPC_thread_limit:
11894    C = new (Context) OMPThreadLimitClause();
11895    break;
11896  case llvm::omp::OMPC_priority:
11897    C = new (Context) OMPPriorityClause();
11898    break;
11899  case llvm::omp::OMPC_grainsize:
11900    C = new (Context) OMPGrainsizeClause();
11901    break;
11902  case llvm::omp::OMPC_num_tasks:
11903    C = new (Context) OMPNumTasksClause();
11904    break;
11905  case llvm::omp::OMPC_hint:
11906    C = new (Context) OMPHintClause();
11907    break;
11908  case llvm::omp::OMPC_dist_schedule:
11909    C = new (Context) OMPDistScheduleClause();
11910    break;
11911  case llvm::omp::OMPC_defaultmap:
11912    C = new (Context) OMPDefaultmapClause();
11913    break;
11914  case llvm::omp::OMPC_to: {
11915    OMPMappableExprListSizeTy Sizes;
11916    Sizes.NumVars = Record.readInt();
11917    Sizes.NumUniqueDeclarations = Record.readInt();
11918    Sizes.NumComponentLists = Record.readInt();
11919    Sizes.NumComponents = Record.readInt();
11920    C = OMPToClause::CreateEmpty(Context, Sizes);
11921    break;
11922  }
11923  case llvm::omp::OMPC_from: {
11924    OMPMappableExprListSizeTy Sizes;
11925    Sizes.NumVars = Record.readInt();
11926    Sizes.NumUniqueDeclarations = Record.readInt();
11927    Sizes.NumComponentLists = Record.readInt();
11928    Sizes.NumComponents = Record.readInt();
11929    C = OMPFromClause::CreateEmpty(Context, Sizes);
11930    break;
11931  }
11932  case llvm::omp::OMPC_use_device_ptr: {
11933    OMPMappableExprListSizeTy Sizes;
11934    Sizes.NumVars = Record.readInt();
11935    Sizes.NumUniqueDeclarations = Record.readInt();
11936    Sizes.NumComponentLists = Record.readInt();
11937    Sizes.NumComponents = Record.readInt();
11938    C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11939    break;
11940  }
11941  case llvm::omp::OMPC_use_device_addr: {
11942    OMPMappableExprListSizeTy Sizes;
11943    Sizes.NumVars = Record.readInt();
11944    Sizes.NumUniqueDeclarations = Record.readInt();
11945    Sizes.NumComponentLists = Record.readInt();
11946    Sizes.NumComponents = Record.readInt();
11947    C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11948    break;
11949  }
11950  case llvm::omp::OMPC_is_device_ptr: {
11951    OMPMappableExprListSizeTy Sizes;
11952    Sizes.NumVars = Record.readInt();
11953    Sizes.NumUniqueDeclarations = Record.readInt();
11954    Sizes.NumComponentLists = Record.readInt();
11955    Sizes.NumComponents = Record.readInt();
11956    C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11957    break;
11958  }
11959  case llvm::omp::OMPC_allocate:
11960    C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11961    break;
11962  case llvm::omp::OMPC_nontemporal:
11963    C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11964    break;
11965  case llvm::omp::OMPC_inclusive:
11966    C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11967    break;
11968  case llvm::omp::OMPC_exclusive:
11969    C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11970    break;
11971  case llvm::omp::OMPC_order:
11972    C = new (Context) OMPOrderClause();
11973    break;
11974  case llvm::omp::OMPC_destroy:
11975    C = new (Context) OMPDestroyClause();
11976    break;
11977  case llvm::omp::OMPC_detach:
11978    C = new (Context) OMPDetachClause();
11979    break;
11980  case llvm::omp::OMPC_uses_allocators:
11981    C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11982    break;
11983  case llvm::omp::OMPC_affinity:
11984    C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11985    break;
11986#define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11987  case llvm::omp::Enum:                                                        \
11988    break;
11989#include "llvm/Frontend/OpenMP/OMPKinds.def"
11990  default:
11991    break;
11992  }
11993  assert(C && "Unknown OMPClause type");
11994
11995  Visit(C);
11996  C->setLocStart(Record.readSourceLocation());
11997  C->setLocEnd(Record.readSourceLocation());
11998
11999  return C;
12000}
12001
12002void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12003  C->setPreInitStmt(Record.readSubStmt(),
12004                    static_cast<OpenMPDirectiveKind>(Record.readInt()));
12005}
12006
12007void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12008  VisitOMPClauseWithPreInit(C);
12009  C->setPostUpdateExpr(Record.readSubExpr());
12010}
12011
12012void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12013  VisitOMPClauseWithPreInit(C);
12014  C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12015  C->setNameModifierLoc(Record.readSourceLocation());
12016  C->setColonLoc(Record.readSourceLocation());
12017  C->setCondition(Record.readSubExpr());
12018  C->setLParenLoc(Record.readSourceLocation());
12019}
12020
12021void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12022  VisitOMPClauseWithPreInit(C);
12023  C->setCondition(Record.readSubExpr());
12024  C->setLParenLoc(Record.readSourceLocation());
12025}
12026
12027void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12028  VisitOMPClauseWithPreInit(C);
12029  C->setNumThreads(Record.readSubExpr());
12030  C->setLParenLoc(Record.readSourceLocation());
12031}
12032
12033void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12034  C->setSafelen(Record.readSubExpr());
12035  C->setLParenLoc(Record.readSourceLocation());
12036}
12037
12038void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12039  C->setSimdlen(Record.readSubExpr());
12040  C->setLParenLoc(Record.readSourceLocation());
12041}
12042
12043void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12044  C->setAllocator(Record.readExpr());
12045  C->setLParenLoc(Record.readSourceLocation());
12046}
12047
12048void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12049  C->setNumForLoops(Record.readSubExpr());
12050  C->setLParenLoc(Record.readSourceLocation());
12051}
12052
12053void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12054  C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12055  C->setLParenLoc(Record.readSourceLocation());
12056  C->setDefaultKindKwLoc(Record.readSourceLocation());
12057}
12058
12059void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12060  C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12061  C->setLParenLoc(Record.readSourceLocation());
12062  C->setProcBindKindKwLoc(Record.readSourceLocation());
12063}
12064
12065void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12066  VisitOMPClauseWithPreInit(C);
12067  C->setScheduleKind(
12068       static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12069  C->setFirstScheduleModifier(
12070      static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12071  C->setSecondScheduleModifier(
12072      static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12073  C->setChunkSize(Record.readSubExpr());
12074  C->setLParenLoc(Record.readSourceLocation());
12075  C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12076  C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12077  C->setScheduleKindLoc(Record.readSourceLocation());
12078  C->setCommaLoc(Record.readSourceLocation());
12079}
12080
12081void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12082  C->setNumForLoops(Record.readSubExpr());
12083  for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12084    C->setLoopNumIterations(I, Record.readSubExpr());
12085  for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12086    C->setLoopCounter(I, Record.readSubExpr());
12087  C->setLParenLoc(Record.readSourceLocation());
12088}
12089
12090void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12091  C->setEventHandler(Record.readSubExpr());
12092  C->setLParenLoc(Record.readSourceLocation());
12093}
12094
12095void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12096
12097void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12098
12099void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12100
12101void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12102
12103void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12104
12105void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12106  if (C->isExtended()) {
12107    C->setLParenLoc(Record.readSourceLocation());
12108    C->setArgumentLoc(Record.readSourceLocation());
12109    C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12110  }
12111}
12112
12113void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12114
12115void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12116
12117void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12118
12119void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12120
12121void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12122
12123void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12124
12125void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12126
12127void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12128
12129void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12130
12131void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12132
12133void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12134
12135void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12136    OMPUnifiedSharedMemoryClause *) {}
12137
12138void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12139
12140void
12141OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12142}
12143
12144void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12145    OMPAtomicDefaultMemOrderClause *C) {
12146  C->setAtomicDefaultMemOrderKind(
12147      static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12148  C->setLParenLoc(Record.readSourceLocation());
12149  C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12150}
12151
12152void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12153  C->setLParenLoc(Record.readSourceLocation());
12154  unsigned NumVars = C->varlist_size();
12155  SmallVector<Expr *, 16> Vars;
12156  Vars.reserve(NumVars);
12157  for (unsigned i = 0; i != NumVars; ++i)
12158    Vars.push_back(Record.readSubExpr());
12159  C->setVarRefs(Vars);
12160  Vars.clear();
12161  for (unsigned i = 0; i != NumVars; ++i)
12162    Vars.push_back(Record.readSubExpr());
12163  C->setPrivateCopies(Vars);
12164}
12165
12166void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12167  VisitOMPClauseWithPreInit(C);
12168  C->setLParenLoc(Record.readSourceLocation());
12169  unsigned NumVars = C->varlist_size();
12170  SmallVector<Expr *, 16> Vars;
12171  Vars.reserve(NumVars);
12172  for (unsigned i = 0; i != NumVars; ++i)
12173    Vars.push_back(Record.readSubExpr());
12174  C->setVarRefs(Vars);
12175  Vars.clear();
12176  for (unsigned i = 0; i != NumVars; ++i)
12177    Vars.push_back(Record.readSubExpr());
12178  C->setPrivateCopies(Vars);
12179  Vars.clear();
12180  for (unsigned i = 0; i != NumVars; ++i)
12181    Vars.push_back(Record.readSubExpr());
12182  C->setInits(Vars);
12183}
12184
12185void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12186  VisitOMPClauseWithPostUpdate(C);
12187  C->setLParenLoc(Record.readSourceLocation());
12188  C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12189  C->setKindLoc(Record.readSourceLocation());
12190  C->setColonLoc(Record.readSourceLocation());
12191  unsigned NumVars = C->varlist_size();
12192  SmallVector<Expr *, 16> Vars;
12193  Vars.reserve(NumVars);
12194  for (unsigned i = 0; i != NumVars; ++i)
12195    Vars.push_back(Record.readSubExpr());
12196  C->setVarRefs(Vars);
12197  Vars.clear();
12198  for (unsigned i = 0; i != NumVars; ++i)
12199    Vars.push_back(Record.readSubExpr());
12200  C->setPrivateCopies(Vars);
12201  Vars.clear();
12202  for (unsigned i = 0; i != NumVars; ++i)
12203    Vars.push_back(Record.readSubExpr());
12204  C->setSourceExprs(Vars);
12205  Vars.clear();
12206  for (unsigned i = 0; i != NumVars; ++i)
12207    Vars.push_back(Record.readSubExpr());
12208  C->setDestinationExprs(Vars);
12209  Vars.clear();
12210  for (unsigned i = 0; i != NumVars; ++i)
12211    Vars.push_back(Record.readSubExpr());
12212  C->setAssignmentOps(Vars);
12213}
12214
12215void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12216  C->setLParenLoc(Record.readSourceLocation());
12217  unsigned NumVars = C->varlist_size();
12218  SmallVector<Expr *, 16> Vars;
12219  Vars.reserve(NumVars);
12220  for (unsigned i = 0; i != NumVars; ++i)
12221    Vars.push_back(Record.readSubExpr());
12222  C->setVarRefs(Vars);
12223}
12224
12225void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12226  VisitOMPClauseWithPostUpdate(C);
12227  C->setLParenLoc(Record.readSourceLocation());
12228  C->setModifierLoc(Record.readSourceLocation());
12229  C->setColonLoc(Record.readSourceLocation());
12230  NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12231  DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12232  C->setQualifierLoc(NNSL);
12233  C->setNameInfo(DNI);
12234
12235  unsigned NumVars = C->varlist_size();
12236  SmallVector<Expr *, 16> Vars;
12237  Vars.reserve(NumVars);
12238  for (unsigned i = 0; i != NumVars; ++i)
12239    Vars.push_back(Record.readSubExpr());
12240  C->setVarRefs(Vars);
12241  Vars.clear();
12242  for (unsigned i = 0; i != NumVars; ++i)
12243    Vars.push_back(Record.readSubExpr());
12244  C->setPrivates(Vars);
12245  Vars.clear();
12246  for (unsigned i = 0; i != NumVars; ++i)
12247    Vars.push_back(Record.readSubExpr());
12248  C->setLHSExprs(Vars);
12249  Vars.clear();
12250  for (unsigned i = 0; i != NumVars; ++i)
12251    Vars.push_back(Record.readSubExpr());
12252  C->setRHSExprs(Vars);
12253  Vars.clear();
12254  for (unsigned i = 0; i != NumVars; ++i)
12255    Vars.push_back(Record.readSubExpr());
12256  C->setReductionOps(Vars);
12257  if (C->getModifier() == OMPC_REDUCTION_inscan) {
12258    Vars.clear();
12259    for (unsigned i = 0; i != NumVars; ++i)
12260      Vars.push_back(Record.readSubExpr());
12261    C->setInscanCopyOps(Vars);
12262    Vars.clear();
12263    for (unsigned i = 0; i != NumVars; ++i)
12264      Vars.push_back(Record.readSubExpr());
12265    C->setInscanCopyArrayTemps(Vars);
12266    Vars.clear();
12267    for (unsigned i = 0; i != NumVars; ++i)
12268      Vars.push_back(Record.readSubExpr());
12269    C->setInscanCopyArrayElems(Vars);
12270  }
12271}
12272
12273void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12274  VisitOMPClauseWithPostUpdate(C);
12275  C->setLParenLoc(Record.readSourceLocation());
12276  C->setColonLoc(Record.readSourceLocation());
12277  NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12278  DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12279  C->setQualifierLoc(NNSL);
12280  C->setNameInfo(DNI);
12281
12282  unsigned NumVars = C->varlist_size();
12283  SmallVector<Expr *, 16> Vars;
12284  Vars.reserve(NumVars);
12285  for (unsigned I = 0; I != NumVars; ++I)
12286    Vars.push_back(Record.readSubExpr());
12287  C->setVarRefs(Vars);
12288  Vars.clear();
12289  for (unsigned I = 0; I != NumVars; ++I)
12290    Vars.push_back(Record.readSubExpr());
12291  C->setPrivates(Vars);
12292  Vars.clear();
12293  for (unsigned I = 0; I != NumVars; ++I)
12294    Vars.push_back(Record.readSubExpr());
12295  C->setLHSExprs(Vars);
12296  Vars.clear();
12297  for (unsigned I = 0; I != NumVars; ++I)
12298    Vars.push_back(Record.readSubExpr());
12299  C->setRHSExprs(Vars);
12300  Vars.clear();
12301  for (unsigned I = 0; I != NumVars; ++I)
12302    Vars.push_back(Record.readSubExpr());
12303  C->setReductionOps(Vars);
12304}
12305
12306void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12307  VisitOMPClauseWithPostUpdate(C);
12308  C->setLParenLoc(Record.readSourceLocation());
12309  C->setColonLoc(Record.readSourceLocation());
12310  NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12311  DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12312  C->setQualifierLoc(NNSL);
12313  C->setNameInfo(DNI);
12314
12315  unsigned NumVars = C->varlist_size();
12316  SmallVector<Expr *, 16> Vars;
12317  Vars.reserve(NumVars);
12318  for (unsigned I = 0; I != NumVars; ++I)
12319    Vars.push_back(Record.readSubExpr());
12320  C->setVarRefs(Vars);
12321  Vars.clear();
12322  for (unsigned I = 0; I != NumVars; ++I)
12323    Vars.push_back(Record.readSubExpr());
12324  C->setPrivates(Vars);
12325  Vars.clear();
12326  for (unsigned I = 0; I != NumVars; ++I)
12327    Vars.push_back(Record.readSubExpr());
12328  C->setLHSExprs(Vars);
12329  Vars.clear();
12330  for (unsigned I = 0; I != NumVars; ++I)
12331    Vars.push_back(Record.readSubExpr());
12332  C->setRHSExprs(Vars);
12333  Vars.clear();
12334  for (unsigned I = 0; I != NumVars; ++I)
12335    Vars.push_back(Record.readSubExpr());
12336  C->setReductionOps(Vars);
12337  Vars.clear();
12338  for (unsigned I = 0; I != NumVars; ++I)
12339    Vars.push_back(Record.readSubExpr());
12340  C->setTaskgroupDescriptors(Vars);
12341}
12342
12343void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12344  VisitOMPClauseWithPostUpdate(C);
12345  C->setLParenLoc(Record.readSourceLocation());
12346  C->setColonLoc(Record.readSourceLocation());
12347  C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12348  C->setModifierLoc(Record.readSourceLocation());
12349  unsigned NumVars = C->varlist_size();
12350  SmallVector<Expr *, 16> Vars;
12351  Vars.reserve(NumVars);
12352  for (unsigned i = 0; i != NumVars; ++i)
12353    Vars.push_back(Record.readSubExpr());
12354  C->setVarRefs(Vars);
12355  Vars.clear();
12356  for (unsigned i = 0; i != NumVars; ++i)
12357    Vars.push_back(Record.readSubExpr());
12358  C->setPrivates(Vars);
12359  Vars.clear();
12360  for (unsigned i = 0; i != NumVars; ++i)
12361    Vars.push_back(Record.readSubExpr());
12362  C->setInits(Vars);
12363  Vars.clear();
12364  for (unsigned i = 0; i != NumVars; ++i)
12365    Vars.push_back(Record.readSubExpr());
12366  C->setUpdates(Vars);
12367  Vars.clear();
12368  for (unsigned i = 0; i != NumVars; ++i)
12369    Vars.push_back(Record.readSubExpr());
12370  C->setFinals(Vars);
12371  C->setStep(Record.readSubExpr());
12372  C->setCalcStep(Record.readSubExpr());
12373  Vars.clear();
12374  for (unsigned I = 0; I != NumVars + 1; ++I)
12375    Vars.push_back(Record.readSubExpr());
12376  C->setUsedExprs(Vars);
12377}
12378
12379void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12380  C->setLParenLoc(Record.readSourceLocation());
12381  C->setColonLoc(Record.readSourceLocation());
12382  unsigned NumVars = C->varlist_size();
12383  SmallVector<Expr *, 16> Vars;
12384  Vars.reserve(NumVars);
12385  for (unsigned i = 0; i != NumVars; ++i)
12386    Vars.push_back(Record.readSubExpr());
12387  C->setVarRefs(Vars);
12388  C->setAlignment(Record.readSubExpr());
12389}
12390
12391void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12392  C->setLParenLoc(Record.readSourceLocation());
12393  unsigned NumVars = C->varlist_size();
12394  SmallVector<Expr *, 16> Exprs;
12395  Exprs.reserve(NumVars);
12396  for (unsigned i = 0; i != NumVars; ++i)
12397    Exprs.push_back(Record.readSubExpr());
12398  C->setVarRefs(Exprs);
12399  Exprs.clear();
12400  for (unsigned i = 0; i != NumVars; ++i)
12401    Exprs.push_back(Record.readSubExpr());
12402  C->setSourceExprs(Exprs);
12403  Exprs.clear();
12404  for (unsigned i = 0; i != NumVars; ++i)
12405    Exprs.push_back(Record.readSubExpr());
12406  C->setDestinationExprs(Exprs);
12407  Exprs.clear();
12408  for (unsigned i = 0; i != NumVars; ++i)
12409    Exprs.push_back(Record.readSubExpr());
12410  C->setAssignmentOps(Exprs);
12411}
12412
12413void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12414  C->setLParenLoc(Record.readSourceLocation());
12415  unsigned NumVars = C->varlist_size();
12416  SmallVector<Expr *, 16> Exprs;
12417  Exprs.reserve(NumVars);
12418  for (unsigned i = 0; i != NumVars; ++i)
12419    Exprs.push_back(Record.readSubExpr());
12420  C->setVarRefs(Exprs);
12421  Exprs.clear();
12422  for (unsigned i = 0; i != NumVars; ++i)
12423    Exprs.push_back(Record.readSubExpr());
12424  C->setSourceExprs(Exprs);
12425  Exprs.clear();
12426  for (unsigned i = 0; i != NumVars; ++i)
12427    Exprs.push_back(Record.readSubExpr());
12428  C->setDestinationExprs(Exprs);
12429  Exprs.clear();
12430  for (unsigned i = 0; i != NumVars; ++i)
12431    Exprs.push_back(Record.readSubExpr());
12432  C->setAssignmentOps(Exprs);
12433}
12434
12435void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12436  C->setLParenLoc(Record.readSourceLocation());
12437  unsigned NumVars = C->varlist_size();
12438  SmallVector<Expr *, 16> Vars;
12439  Vars.reserve(NumVars);
12440  for (unsigned i = 0; i != NumVars; ++i)
12441    Vars.push_back(Record.readSubExpr());
12442  C->setVarRefs(Vars);
12443}
12444
12445void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12446  C->setDepobj(Record.readSubExpr());
12447  C->setLParenLoc(Record.readSourceLocation());
12448}
12449
12450void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12451  C->setLParenLoc(Record.readSourceLocation());
12452  C->setModifier(Record.readSubExpr());
12453  C->setDependencyKind(
12454      static_cast<OpenMPDependClauseKind>(Record.readInt()));
12455  C->setDependencyLoc(Record.readSourceLocation());
12456  C->setColonLoc(Record.readSourceLocation());
12457  unsigned NumVars = C->varlist_size();
12458  SmallVector<Expr *, 16> Vars;
12459  Vars.reserve(NumVars);
12460  for (unsigned I = 0; I != NumVars; ++I)
12461    Vars.push_back(Record.readSubExpr());
12462  C->setVarRefs(Vars);
12463  for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12464    C->setLoopData(I, Record.readSubExpr());
12465}
12466
12467void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12468  VisitOMPClauseWithPreInit(C);
12469  C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12470  C->setDevice(Record.readSubExpr());
12471  C->setModifierLoc(Record.readSourceLocation());
12472  C->setLParenLoc(Record.readSourceLocation());
12473}
12474
12475void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12476  C->setLParenLoc(Record.readSourceLocation());
12477  for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12478    C->setMapTypeModifier(
12479        I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12480    C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12481  }
12482  C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12483  C->setMapperIdInfo(Record.readDeclarationNameInfo());
12484  C->setMapType(
12485     static_cast<OpenMPMapClauseKind>(Record.readInt()));
12486  C->setMapLoc(Record.readSourceLocation());
12487  C->setColonLoc(Record.readSourceLocation());
12488  auto NumVars = C->varlist_size();
12489  auto UniqueDecls = C->getUniqueDeclarationsNum();
12490  auto TotalLists = C->getTotalComponentListNum();
12491  auto TotalComponents = C->getTotalComponentsNum();
12492
12493  SmallVector<Expr *, 16> Vars;
12494  Vars.reserve(NumVars);
12495  for (unsigned i = 0; i != NumVars; ++i)
12496    Vars.push_back(Record.readExpr());
12497  C->setVarRefs(Vars);
12498
12499  SmallVector<Expr *, 16> UDMappers;
12500  UDMappers.reserve(NumVars);
12501  for (unsigned I = 0; I < NumVars; ++I)
12502    UDMappers.push_back(Record.readExpr());
12503  C->setUDMapperRefs(UDMappers);
12504
12505  SmallVector<ValueDecl *, 16> Decls;
12506  Decls.reserve(UniqueDecls);
12507  for (unsigned i = 0; i < UniqueDecls; ++i)
12508    Decls.push_back(Record.readDeclAs<ValueDecl>());
12509  C->setUniqueDecls(Decls);
12510
12511  SmallVector<unsigned, 16> ListsPerDecl;
12512  ListsPerDecl.reserve(UniqueDecls);
12513  for (unsigned i = 0; i < UniqueDecls; ++i)
12514    ListsPerDecl.push_back(Record.readInt());
12515  C->setDeclNumLists(ListsPerDecl);
12516
12517  SmallVector<unsigned, 32> ListSizes;
12518  ListSizes.reserve(TotalLists);
12519  for (unsigned i = 0; i < TotalLists; ++i)
12520    ListSizes.push_back(Record.readInt());
12521  C->setComponentListSizes(ListSizes);
12522
12523  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12524  Components.reserve(TotalComponents);
12525  for (unsigned i = 0; i < TotalComponents; ++i) {
12526    Expr *AssociatedExpr = Record.readExpr();
12527    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12528    Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12529        AssociatedExpr, AssociatedDecl));
12530  }
12531  C->setComponents(Components, ListSizes);
12532}
12533
12534void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12535  C->setLParenLoc(Record.readSourceLocation());
12536  C->setColonLoc(Record.readSourceLocation());
12537  C->setAllocator(Record.readSubExpr());
12538  unsigned NumVars = C->varlist_size();
12539  SmallVector<Expr *, 16> Vars;
12540  Vars.reserve(NumVars);
12541  for (unsigned i = 0; i != NumVars; ++i)
12542    Vars.push_back(Record.readSubExpr());
12543  C->setVarRefs(Vars);
12544}
12545
12546void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12547  VisitOMPClauseWithPreInit(C);
12548  C->setNumTeams(Record.readSubExpr());
12549  C->setLParenLoc(Record.readSourceLocation());
12550}
12551
12552void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12553  VisitOMPClauseWithPreInit(C);
12554  C->setThreadLimit(Record.readSubExpr());
12555  C->setLParenLoc(Record.readSourceLocation());
12556}
12557
12558void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12559  VisitOMPClauseWithPreInit(C);
12560  C->setPriority(Record.readSubExpr());
12561  C->setLParenLoc(Record.readSourceLocation());
12562}
12563
12564void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12565  VisitOMPClauseWithPreInit(C);
12566  C->setGrainsize(Record.readSubExpr());
12567  C->setLParenLoc(Record.readSourceLocation());
12568}
12569
12570void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12571  VisitOMPClauseWithPreInit(C);
12572  C->setNumTasks(Record.readSubExpr());
12573  C->setLParenLoc(Record.readSourceLocation());
12574}
12575
12576void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12577  C->setHint(Record.readSubExpr());
12578  C->setLParenLoc(Record.readSourceLocation());
12579}
12580
12581void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12582  VisitOMPClauseWithPreInit(C);
12583  C->setDistScheduleKind(
12584      static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12585  C->setChunkSize(Record.readSubExpr());
12586  C->setLParenLoc(Record.readSourceLocation());
12587  C->setDistScheduleKindLoc(Record.readSourceLocation());
12588  C->setCommaLoc(Record.readSourceLocation());
12589}
12590
12591void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12592  C->setDefaultmapKind(
12593       static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12594  C->setDefaultmapModifier(
12595      static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12596  C->setLParenLoc(Record.readSourceLocation());
12597  C->setDefaultmapModifierLoc(Record.readSourceLocation());
12598  C->setDefaultmapKindLoc(Record.readSourceLocation());
12599}
12600
12601void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12602  C->setLParenLoc(Record.readSourceLocation());
12603  C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12604  C->setMapperIdInfo(Record.readDeclarationNameInfo());
12605  auto NumVars = C->varlist_size();
12606  auto UniqueDecls = C->getUniqueDeclarationsNum();
12607  auto TotalLists = C->getTotalComponentListNum();
12608  auto TotalComponents = C->getTotalComponentsNum();
12609
12610  SmallVector<Expr *, 16> Vars;
12611  Vars.reserve(NumVars);
12612  for (unsigned i = 0; i != NumVars; ++i)
12613    Vars.push_back(Record.readSubExpr());
12614  C->setVarRefs(Vars);
12615
12616  SmallVector<Expr *, 16> UDMappers;
12617  UDMappers.reserve(NumVars);
12618  for (unsigned I = 0; I < NumVars; ++I)
12619    UDMappers.push_back(Record.readSubExpr());
12620  C->setUDMapperRefs(UDMappers);
12621
12622  SmallVector<ValueDecl *, 16> Decls;
12623  Decls.reserve(UniqueDecls);
12624  for (unsigned i = 0; i < UniqueDecls; ++i)
12625    Decls.push_back(Record.readDeclAs<ValueDecl>());
12626  C->setUniqueDecls(Decls);
12627
12628  SmallVector<unsigned, 16> ListsPerDecl;
12629  ListsPerDecl.reserve(UniqueDecls);
12630  for (unsigned i = 0; i < UniqueDecls; ++i)
12631    ListsPerDecl.push_back(Record.readInt());
12632  C->setDeclNumLists(ListsPerDecl);
12633
12634  SmallVector<unsigned, 32> ListSizes;
12635  ListSizes.reserve(TotalLists);
12636  for (unsigned i = 0; i < TotalLists; ++i)
12637    ListSizes.push_back(Record.readInt());
12638  C->setComponentListSizes(ListSizes);
12639
12640  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12641  Components.reserve(TotalComponents);
12642  for (unsigned i = 0; i < TotalComponents; ++i) {
12643    Expr *AssociatedExpr = Record.readSubExpr();
12644    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12645    Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12646        AssociatedExpr, AssociatedDecl));
12647  }
12648  C->setComponents(Components, ListSizes);
12649}
12650
12651void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12652  C->setLParenLoc(Record.readSourceLocation());
12653  C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12654  C->setMapperIdInfo(Record.readDeclarationNameInfo());
12655  auto NumVars = C->varlist_size();
12656  auto UniqueDecls = C->getUniqueDeclarationsNum();
12657  auto TotalLists = C->getTotalComponentListNum();
12658  auto TotalComponents = C->getTotalComponentsNum();
12659
12660  SmallVector<Expr *, 16> Vars;
12661  Vars.reserve(NumVars);
12662  for (unsigned i = 0; i != NumVars; ++i)
12663    Vars.push_back(Record.readSubExpr());
12664  C->setVarRefs(Vars);
12665
12666  SmallVector<Expr *, 16> UDMappers;
12667  UDMappers.reserve(NumVars);
12668  for (unsigned I = 0; I < NumVars; ++I)
12669    UDMappers.push_back(Record.readSubExpr());
12670  C->setUDMapperRefs(UDMappers);
12671
12672  SmallVector<ValueDecl *, 16> Decls;
12673  Decls.reserve(UniqueDecls);
12674  for (unsigned i = 0; i < UniqueDecls; ++i)
12675    Decls.push_back(Record.readDeclAs<ValueDecl>());
12676  C->setUniqueDecls(Decls);
12677
12678  SmallVector<unsigned, 16> ListsPerDecl;
12679  ListsPerDecl.reserve(UniqueDecls);
12680  for (unsigned i = 0; i < UniqueDecls; ++i)
12681    ListsPerDecl.push_back(Record.readInt());
12682  C->setDeclNumLists(ListsPerDecl);
12683
12684  SmallVector<unsigned, 32> ListSizes;
12685  ListSizes.reserve(TotalLists);
12686  for (unsigned i = 0; i < TotalLists; ++i)
12687    ListSizes.push_back(Record.readInt());
12688  C->setComponentListSizes(ListSizes);
12689
12690  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12691  Components.reserve(TotalComponents);
12692  for (unsigned i = 0; i < TotalComponents; ++i) {
12693    Expr *AssociatedExpr = Record.readSubExpr();
12694    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12695    Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12696        AssociatedExpr, AssociatedDecl));
12697  }
12698  C->setComponents(Components, ListSizes);
12699}
12700
12701void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12702  C->setLParenLoc(Record.readSourceLocation());
12703  auto NumVars = C->varlist_size();
12704  auto UniqueDecls = C->getUniqueDeclarationsNum();
12705  auto TotalLists = C->getTotalComponentListNum();
12706  auto TotalComponents = C->getTotalComponentsNum();
12707
12708  SmallVector<Expr *, 16> Vars;
12709  Vars.reserve(NumVars);
12710  for (unsigned i = 0; i != NumVars; ++i)
12711    Vars.push_back(Record.readSubExpr());
12712  C->setVarRefs(Vars);
12713  Vars.clear();
12714  for (unsigned i = 0; i != NumVars; ++i)
12715    Vars.push_back(Record.readSubExpr());
12716  C->setPrivateCopies(Vars);
12717  Vars.clear();
12718  for (unsigned i = 0; i != NumVars; ++i)
12719    Vars.push_back(Record.readSubExpr());
12720  C->setInits(Vars);
12721
12722  SmallVector<ValueDecl *, 16> Decls;
12723  Decls.reserve(UniqueDecls);
12724  for (unsigned i = 0; i < UniqueDecls; ++i)
12725    Decls.push_back(Record.readDeclAs<ValueDecl>());
12726  C->setUniqueDecls(Decls);
12727
12728  SmallVector<unsigned, 16> ListsPerDecl;
12729  ListsPerDecl.reserve(UniqueDecls);
12730  for (unsigned i = 0; i < UniqueDecls; ++i)
12731    ListsPerDecl.push_back(Record.readInt());
12732  C->setDeclNumLists(ListsPerDecl);
12733
12734  SmallVector<unsigned, 32> ListSizes;
12735  ListSizes.reserve(TotalLists);
12736  for (unsigned i = 0; i < TotalLists; ++i)
12737    ListSizes.push_back(Record.readInt());
12738  C->setComponentListSizes(ListSizes);
12739
12740  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12741  Components.reserve(TotalComponents);
12742  for (unsigned i = 0; i < TotalComponents; ++i) {
12743    Expr *AssociatedExpr = Record.readSubExpr();
12744    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12745    Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12746        AssociatedExpr, AssociatedDecl));
12747  }
12748  C->setComponents(Components, ListSizes);
12749}
12750
12751void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12752  C->setLParenLoc(Record.readSourceLocation());
12753  auto NumVars = C->varlist_size();
12754  auto UniqueDecls = C->getUniqueDeclarationsNum();
12755  auto TotalLists = C->getTotalComponentListNum();
12756  auto TotalComponents = C->getTotalComponentsNum();
12757
12758  SmallVector<Expr *, 16> Vars;
12759  Vars.reserve(NumVars);
12760  for (unsigned i = 0; i != NumVars; ++i)
12761    Vars.push_back(Record.readSubExpr());
12762  C->setVarRefs(Vars);
12763
12764  SmallVector<ValueDecl *, 16> Decls;
12765  Decls.reserve(UniqueDecls);
12766  for (unsigned i = 0; i < UniqueDecls; ++i)
12767    Decls.push_back(Record.readDeclAs<ValueDecl>());
12768  C->setUniqueDecls(Decls);
12769
12770  SmallVector<unsigned, 16> ListsPerDecl;
12771  ListsPerDecl.reserve(UniqueDecls);
12772  for (unsigned i = 0; i < UniqueDecls; ++i)
12773    ListsPerDecl.push_back(Record.readInt());
12774  C->setDeclNumLists(ListsPerDecl);
12775
12776  SmallVector<unsigned, 32> ListSizes;
12777  ListSizes.reserve(TotalLists);
12778  for (unsigned i = 0; i < TotalLists; ++i)
12779    ListSizes.push_back(Record.readInt());
12780  C->setComponentListSizes(ListSizes);
12781
12782  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12783  Components.reserve(TotalComponents);
12784  for (unsigned i = 0; i < TotalComponents; ++i) {
12785    Expr *AssociatedExpr = Record.readSubExpr();
12786    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12787    Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12788        AssociatedExpr, AssociatedDecl));
12789  }
12790  C->setComponents(Components, ListSizes);
12791}
12792
12793void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12794  C->setLParenLoc(Record.readSourceLocation());
12795  auto NumVars = C->varlist_size();
12796  auto UniqueDecls = C->getUniqueDeclarationsNum();
12797  auto TotalLists = C->getTotalComponentListNum();
12798  auto TotalComponents = C->getTotalComponentsNum();
12799
12800  SmallVector<Expr *, 16> Vars;
12801  Vars.reserve(NumVars);
12802  for (unsigned i = 0; i != NumVars; ++i)
12803    Vars.push_back(Record.readSubExpr());
12804  C->setVarRefs(Vars);
12805  Vars.clear();
12806
12807  SmallVector<ValueDecl *, 16> Decls;
12808  Decls.reserve(UniqueDecls);
12809  for (unsigned i = 0; i < UniqueDecls; ++i)
12810    Decls.push_back(Record.readDeclAs<ValueDecl>());
12811  C->setUniqueDecls(Decls);
12812
12813  SmallVector<unsigned, 16> ListsPerDecl;
12814  ListsPerDecl.reserve(UniqueDecls);
12815  for (unsigned i = 0; i < UniqueDecls; ++i)
12816    ListsPerDecl.push_back(Record.readInt());
12817  C->setDeclNumLists(ListsPerDecl);
12818
12819  SmallVector<unsigned, 32> ListSizes;
12820  ListSizes.reserve(TotalLists);
12821  for (unsigned i = 0; i < TotalLists; ++i)
12822    ListSizes.push_back(Record.readInt());
12823  C->setComponentListSizes(ListSizes);
12824
12825  SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12826  Components.reserve(TotalComponents);
12827  for (unsigned i = 0; i < TotalComponents; ++i) {
12828    Expr *AssociatedExpr = Record.readSubExpr();
12829    auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12830    Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12831        AssociatedExpr, AssociatedDecl));
12832  }
12833  C->setComponents(Components, ListSizes);
12834}
12835
12836void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12837  C->setLParenLoc(Record.readSourceLocation());
12838  unsigned NumVars = C->varlist_size();
12839  SmallVector<Expr *, 16> Vars;
12840  Vars.reserve(NumVars);
12841  for (unsigned i = 0; i != NumVars; ++i)
12842    Vars.push_back(Record.readSubExpr());
12843  C->setVarRefs(Vars);
12844  Vars.clear();
12845  Vars.reserve(NumVars);
12846  for (unsigned i = 0; i != NumVars; ++i)
12847    Vars.push_back(Record.readSubExpr());
12848  C->setPrivateRefs(Vars);
12849}
12850
12851void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12852  C->setLParenLoc(Record.readSourceLocation());
12853  unsigned NumVars = C->varlist_size();
12854  SmallVector<Expr *, 16> Vars;
12855  Vars.reserve(NumVars);
12856  for (unsigned i = 0; i != NumVars; ++i)
12857    Vars.push_back(Record.readSubExpr());
12858  C->setVarRefs(Vars);
12859}
12860
12861void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12862  C->setLParenLoc(Record.readSourceLocation());
12863  unsigned NumVars = C->varlist_size();
12864  SmallVector<Expr *, 16> Vars;
12865  Vars.reserve(NumVars);
12866  for (unsigned i = 0; i != NumVars; ++i)
12867    Vars.push_back(Record.readSubExpr());
12868  C->setVarRefs(Vars);
12869}
12870
12871void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12872  C->setLParenLoc(Record.readSourceLocation());
12873  unsigned NumOfAllocators = C->getNumberOfAllocators();
12874  SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12875  Data.reserve(NumOfAllocators);
12876  for (unsigned I = 0; I != NumOfAllocators; ++I) {
12877    OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12878    D.Allocator = Record.readSubExpr();
12879    D.AllocatorTraits = Record.readSubExpr();
12880    D.LParenLoc = Record.readSourceLocation();
12881    D.RParenLoc = Record.readSourceLocation();
12882  }
12883  C->setAllocatorsData(Data);
12884}
12885
12886void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12887  C->setLParenLoc(Record.readSourceLocation());
12888  C->setModifier(Record.readSubExpr());
12889  C->setColonLoc(Record.readSourceLocation());
12890  unsigned NumOfLocators = C->varlist_size();
12891  SmallVector<Expr *, 4> Locators;
12892  Locators.reserve(NumOfLocators);
12893  for (unsigned I = 0; I != NumOfLocators; ++I)
12894    Locators.push_back(Record.readSubExpr());
12895  C->setVarRefs(Locators);
12896}
12897
12898void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12899  C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12900  C->setLParenLoc(Record.readSourceLocation());
12901  C->setKindKwLoc(Record.readSourceLocation());
12902}
12903
12904OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12905  OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12906  TI.Sets.resize(readUInt32());
12907  for (auto &Set : TI.Sets) {
12908    Set.Kind = readEnum<llvm::omp::TraitSet>();
12909    Set.Selectors.resize(readUInt32());
12910    for (auto &Selector : Set.Selectors) {
12911      Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12912      Selector.ScoreOrCondition = nullptr;
12913      if (readBool())
12914        Selector.ScoreOrCondition = readExprRef();
12915      Selector.Properties.resize(readUInt32());
12916      for (auto &Property : Selector.Properties)
12917        Property.Kind = readEnum<llvm::omp::TraitProperty>();
12918    }
12919  }
12920  return &TI;
12921}
12922