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/LEB128.h"
118 #include "llvm/Support/MemoryBuffer.h"
119 #include "llvm/Support/Path.h"
120 #include "llvm/Support/SaveAndRestore.h"
121 #include "llvm/Support/Timer.h"
122 #include "llvm/Support/VersionTuple.h"
123 #include "llvm/Support/raw_ostream.h"
124 #include <algorithm>
125 #include <cassert>
126 #include <cstddef>
127 #include <cstdint>
128 #include <cstdio>
129 #include <ctime>
130 #include <iterator>
131 #include <limits>
132 #include <map>
133 #include <memory>
134 #include <string>
135 #include <system_error>
136 #include <tuple>
137 #include <utility>
138 #include <vector>
139 
140 using namespace clang;
141 using namespace clang::serialization;
142 using namespace clang::serialization::reader;
143 using llvm::BitstreamCursor;
144 using llvm::RoundingMode;
145 
146 //===----------------------------------------------------------------------===//
147 // ChainedASTReaderListener implementation
148 //===----------------------------------------------------------------------===//
149 
150 bool
151 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
152   return First->ReadFullVersionInformation(FullVersion) ||
153          Second->ReadFullVersionInformation(FullVersion);
154 }
155 
156 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
157   First->ReadModuleName(ModuleName);
158   Second->ReadModuleName(ModuleName);
159 }
160 
161 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
162   First->ReadModuleMapFile(ModuleMapPath);
163   Second->ReadModuleMapFile(ModuleMapPath);
164 }
165 
166 bool
167 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
168                                               bool Complain,
169                                               bool AllowCompatibleDifferences) {
170   return First->ReadLanguageOptions(LangOpts, Complain,
171                                     AllowCompatibleDifferences) ||
172          Second->ReadLanguageOptions(LangOpts, Complain,
173                                      AllowCompatibleDifferences);
174 }
175 
176 bool ChainedASTReaderListener::ReadTargetOptions(
177     const TargetOptions &TargetOpts, bool Complain,
178     bool AllowCompatibleDifferences) {
179   return First->ReadTargetOptions(TargetOpts, Complain,
180                                   AllowCompatibleDifferences) ||
181          Second->ReadTargetOptions(TargetOpts, Complain,
182                                    AllowCompatibleDifferences);
183 }
184 
185 bool ChainedASTReaderListener::ReadDiagnosticOptions(
186     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
187   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
188          Second->ReadDiagnosticOptions(DiagOpts, Complain);
189 }
190 
191 bool
192 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
193                                                 bool Complain) {
194   return First->ReadFileSystemOptions(FSOpts, Complain) ||
195          Second->ReadFileSystemOptions(FSOpts, Complain);
196 }
197 
198 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
199     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
200     bool Complain) {
201   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
202                                         Complain) ||
203          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
204                                          Complain);
205 }
206 
207 bool ChainedASTReaderListener::ReadPreprocessorOptions(
208     const PreprocessorOptions &PPOpts, bool Complain,
209     std::string &SuggestedPredefines) {
210   return First->ReadPreprocessorOptions(PPOpts, Complain,
211                                         SuggestedPredefines) ||
212          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
213 }
214 
215 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
216                                            unsigned Value) {
217   First->ReadCounter(M, Value);
218   Second->ReadCounter(M, Value);
219 }
220 
221 bool ChainedASTReaderListener::needsInputFileVisitation() {
222   return First->needsInputFileVisitation() ||
223          Second->needsInputFileVisitation();
224 }
225 
226 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
227   return First->needsSystemInputFileVisitation() ||
228   Second->needsSystemInputFileVisitation();
229 }
230 
231 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
232                                                ModuleKind Kind) {
233   First->visitModuleFile(Filename, Kind);
234   Second->visitModuleFile(Filename, Kind);
235 }
236 
237 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
238                                               bool isSystem,
239                                               bool isOverridden,
240                                               bool isExplicitModule) {
241   bool Continue = false;
242   if (First->needsInputFileVisitation() &&
243       (!isSystem || First->needsSystemInputFileVisitation()))
244     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
245                                       isExplicitModule);
246   if (Second->needsInputFileVisitation() &&
247       (!isSystem || Second->needsSystemInputFileVisitation()))
248     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
249                                        isExplicitModule);
250   return Continue;
251 }
252 
253 void ChainedASTReaderListener::readModuleFileExtension(
254        const ModuleFileExtensionMetadata &Metadata) {
255   First->readModuleFileExtension(Metadata);
256   Second->readModuleFileExtension(Metadata);
257 }
258 
259 //===----------------------------------------------------------------------===//
260 // PCH validator implementation
261 //===----------------------------------------------------------------------===//
262 
263 ASTReaderListener::~ASTReaderListener() = default;
264 
265 /// Compare the given set of language options against an existing set of
266 /// language options.
267 ///
268 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
269 /// \param AllowCompatibleDifferences If true, differences between compatible
270 ///        language options will be permitted.
271 ///
272 /// \returns true if the languagae options mis-match, false otherwise.
273 static bool checkLanguageOptions(const LangOptions &LangOpts,
274                                  const LangOptions &ExistingLangOpts,
275                                  DiagnosticsEngine *Diags,
276                                  bool AllowCompatibleDifferences = true) {
277 #define LANGOPT(Name, Bits, Default, Description)                 \
278   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
279     if (Diags)                                                    \
280       Diags->Report(diag::err_pch_langopt_mismatch)               \
281         << Description << LangOpts.Name << ExistingLangOpts.Name; \
282     return true;                                                  \
283   }
284 
285 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
286   if (ExistingLangOpts.Name != LangOpts.Name) {           \
287     if (Diags)                                            \
288       Diags->Report(diag::err_pch_langopt_value_mismatch) \
289         << Description;                                   \
290     return true;                                          \
291   }
292 
293 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
294   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
295     if (Diags)                                                 \
296       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
297         << Description;                                        \
298     return true;                                               \
299   }
300 
301 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
302   if (!AllowCompatibleDifferences)                            \
303     LANGOPT(Name, Bits, Default, Description)
304 
305 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
306   if (!AllowCompatibleDifferences)                                 \
307     ENUM_LANGOPT(Name, Bits, Default, Description)
308 
309 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
310   if (!AllowCompatibleDifferences)                                 \
311     VALUE_LANGOPT(Name, Bits, Default, Description)
312 
313 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
314 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
315 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
316 #include "clang/Basic/LangOptions.def"
317 
318   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
321     return true;
322   }
323 
324   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
325     if (Diags)
326       Diags->Report(diag::err_pch_langopt_value_mismatch)
327       << "target Objective-C runtime";
328     return true;
329   }
330 
331   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
332       LangOpts.CommentOpts.BlockCommandNames) {
333     if (Diags)
334       Diags->Report(diag::err_pch_langopt_value_mismatch)
335         << "block command names";
336     return true;
337   }
338 
339   // Sanitizer feature mismatches are treated as compatible differences. If
340   // compatible differences aren't allowed, we still only want to check for
341   // mismatches of non-modular sanitizers (the only ones which can affect AST
342   // generation).
343   if (!AllowCompatibleDifferences) {
344     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
345     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
346     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
347     ExistingSanitizers.clear(ModularSanitizers);
348     ImportedSanitizers.clear(ModularSanitizers);
349     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
350       const std::string Flag = "-fsanitize=";
351       if (Diags) {
352 #define SANITIZER(NAME, ID)                                                    \
353   {                                                                            \
354     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
355     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
356     if (InExistingModule != InImportedModule)                                  \
357       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
358           << InExistingModule << (Flag + NAME);                                \
359   }
360 #include "clang/Basic/Sanitizers.def"
361       }
362       return true;
363     }
364   }
365 
366   return false;
367 }
368 
369 /// Compare the given set of target options against an existing set of
370 /// target options.
371 ///
372 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
373 ///
374 /// \returns true if the target options mis-match, false otherwise.
375 static bool checkTargetOptions(const TargetOptions &TargetOpts,
376                                const TargetOptions &ExistingTargetOpts,
377                                DiagnosticsEngine *Diags,
378                                bool AllowCompatibleDifferences = true) {
379 #define CHECK_TARGET_OPT(Field, Name)                             \
380   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
381     if (Diags)                                                    \
382       Diags->Report(diag::err_pch_targetopt_mismatch)             \
383         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
384     return true;                                                  \
385   }
386 
387   // The triple and ABI must match exactly.
388   CHECK_TARGET_OPT(Triple, "target");
389   CHECK_TARGET_OPT(ABI, "target ABI");
390 
391   // We can tolerate different CPUs in many cases, notably when one CPU
392   // supports a strict superset of another. When allowing compatible
393   // differences skip this check.
394   if (!AllowCompatibleDifferences) {
395     CHECK_TARGET_OPT(CPU, "target CPU");
396     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
397   }
398 
399 #undef CHECK_TARGET_OPT
400 
401   // Compare feature sets.
402   SmallVector<StringRef, 4> ExistingFeatures(
403                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
404                                              ExistingTargetOpts.FeaturesAsWritten.end());
405   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
406                                          TargetOpts.FeaturesAsWritten.end());
407   llvm::sort(ExistingFeatures);
408   llvm::sort(ReadFeatures);
409 
410   // We compute the set difference in both directions explicitly so that we can
411   // diagnose the differences differently.
412   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
413   std::set_difference(
414       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
415       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
416   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
417                       ExistingFeatures.begin(), ExistingFeatures.end(),
418                       std::back_inserter(UnmatchedReadFeatures));
419 
420   // If we are allowing compatible differences and the read feature set is
421   // a strict subset of the existing feature set, there is nothing to diagnose.
422   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
423     return false;
424 
425   if (Diags) {
426     for (StringRef Feature : UnmatchedReadFeatures)
427       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428           << /* is-existing-feature */ false << Feature;
429     for (StringRef Feature : UnmatchedExistingFeatures)
430       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
431           << /* is-existing-feature */ true << Feature;
432   }
433 
434   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
435 }
436 
437 bool
438 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
439                                   bool Complain,
440                                   bool AllowCompatibleDifferences) {
441   const LangOptions &ExistingLangOpts = PP.getLangOpts();
442   return checkLanguageOptions(LangOpts, ExistingLangOpts,
443                               Complain ? &Reader.Diags : nullptr,
444                               AllowCompatibleDifferences);
445 }
446 
447 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
448                                      bool Complain,
449                                      bool AllowCompatibleDifferences) {
450   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
451   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
452                             Complain ? &Reader.Diags : nullptr,
453                             AllowCompatibleDifferences);
454 }
455 
456 namespace {
457 
458 using MacroDefinitionsMap =
459     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
460 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
461 
462 } // namespace
463 
464 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
465                                          DiagnosticsEngine &Diags,
466                                          bool Complain) {
467   using Level = DiagnosticsEngine::Level;
468 
469   // Check current mappings for new -Werror mappings, and the stored mappings
470   // for cases that were explicitly mapped to *not* be errors that are now
471   // errors because of options like -Werror.
472   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
473 
474   for (DiagnosticsEngine *MappingSource : MappingSources) {
475     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
476       diag::kind DiagID = DiagIDMappingPair.first;
477       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
478       if (CurLevel < DiagnosticsEngine::Error)
479         continue; // not significant
480       Level StoredLevel =
481           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
482       if (StoredLevel < DiagnosticsEngine::Error) {
483         if (Complain)
484           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
485               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
486         return true;
487       }
488     }
489   }
490 
491   return false;
492 }
493 
494 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
495   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
496   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
497     return true;
498   return Ext >= diag::Severity::Error;
499 }
500 
501 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
502                                     DiagnosticsEngine &Diags,
503                                     bool IsSystem, bool Complain) {
504   // Top-level options
505   if (IsSystem) {
506     if (Diags.getSuppressSystemWarnings())
507       return false;
508     // If -Wsystem-headers was not enabled before, be conservative
509     if (StoredDiags.getSuppressSystemWarnings()) {
510       if (Complain)
511         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
512       return true;
513     }
514   }
515 
516   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
517     if (Complain)
518       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
519     return true;
520   }
521 
522   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
523       !StoredDiags.getEnableAllWarnings()) {
524     if (Complain)
525       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
526     return true;
527   }
528 
529   if (isExtHandlingFromDiagsError(Diags) &&
530       !isExtHandlingFromDiagsError(StoredDiags)) {
531     if (Complain)
532       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
533     return true;
534   }
535 
536   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
537 }
538 
539 /// Return the top import module if it is implicit, nullptr otherwise.
540 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
541                                           Preprocessor &PP) {
542   // If the original import came from a file explicitly generated by the user,
543   // don't check the diagnostic mappings.
544   // FIXME: currently this is approximated by checking whether this is not a
545   // module import of an implicitly-loaded module file.
546   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
547   // the transitive closure of its imports, since unrelated modules cannot be
548   // imported until after this module finishes validation.
549   ModuleFile *TopImport = &*ModuleMgr.rbegin();
550   while (!TopImport->ImportedBy.empty())
551     TopImport = TopImport->ImportedBy[0];
552   if (TopImport->Kind != MK_ImplicitModule)
553     return nullptr;
554 
555   StringRef ModuleName = TopImport->ModuleName;
556   assert(!ModuleName.empty() && "diagnostic options read before module name");
557 
558   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
559   assert(M && "missing module");
560   return M;
561 }
562 
563 bool PCHValidator::ReadDiagnosticOptions(
564     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
565   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
566   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
567   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
568       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
569   // This should never fail, because we would have processed these options
570   // before writing them to an ASTFile.
571   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
572 
573   ModuleManager &ModuleMgr = Reader.getModuleManager();
574   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
575 
576   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
577   if (!TopM)
578     return false;
579 
580   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
581   // contains the union of their flags.
582   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
583                                  Complain);
584 }
585 
586 /// Collect the macro definitions provided by the given preprocessor
587 /// options.
588 static void
589 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
590                         MacroDefinitionsMap &Macros,
591                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
592   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
593     StringRef Macro = PPOpts.Macros[I].first;
594     bool IsUndef = PPOpts.Macros[I].second;
595 
596     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
597     StringRef MacroName = MacroPair.first;
598     StringRef MacroBody = MacroPair.second;
599 
600     // For an #undef'd macro, we only care about the name.
601     if (IsUndef) {
602       if (MacroNames && !Macros.count(MacroName))
603         MacroNames->push_back(MacroName);
604 
605       Macros[MacroName] = std::make_pair("", true);
606       continue;
607     }
608 
609     // For a #define'd macro, figure out the actual definition.
610     if (MacroName.size() == Macro.size())
611       MacroBody = "1";
612     else {
613       // Note: GCC drops anything following an end-of-line character.
614       StringRef::size_type End = MacroBody.find_first_of("\n\r");
615       MacroBody = MacroBody.substr(0, End);
616     }
617 
618     if (MacroNames && !Macros.count(MacroName))
619       MacroNames->push_back(MacroName);
620     Macros[MacroName] = std::make_pair(MacroBody, false);
621   }
622 }
623 
624 /// Check the preprocessor options deserialized from the control block
625 /// against the preprocessor options in an existing preprocessor.
626 ///
627 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
628 /// \param Validate If true, validate preprocessor options. If false, allow
629 ///        macros defined by \p ExistingPPOpts to override those defined by
630 ///        \p PPOpts in SuggestedPredefines.
631 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
632                                      const PreprocessorOptions &ExistingPPOpts,
633                                      DiagnosticsEngine *Diags,
634                                      FileManager &FileMgr,
635                                      std::string &SuggestedPredefines,
636                                      const LangOptions &LangOpts,
637                                      bool Validate = true) {
638   // Check macro definitions.
639   MacroDefinitionsMap ASTFileMacros;
640   collectMacroDefinitions(PPOpts, ASTFileMacros);
641   MacroDefinitionsMap ExistingMacros;
642   SmallVector<StringRef, 4> ExistingMacroNames;
643   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
644 
645   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
646     // Dig out the macro definition in the existing preprocessor options.
647     StringRef MacroName = ExistingMacroNames[I];
648     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
649 
650     // Check whether we know anything about this macro name or not.
651     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
652         ASTFileMacros.find(MacroName);
653     if (!Validate || Known == ASTFileMacros.end()) {
654       // FIXME: Check whether this identifier was referenced anywhere in the
655       // AST file. If so, we should reject the AST file. Unfortunately, this
656       // information isn't in the control block. What shall we do about it?
657 
658       if (Existing.second) {
659         SuggestedPredefines += "#undef ";
660         SuggestedPredefines += MacroName.str();
661         SuggestedPredefines += '\n';
662       } else {
663         SuggestedPredefines += "#define ";
664         SuggestedPredefines += MacroName.str();
665         SuggestedPredefines += ' ';
666         SuggestedPredefines += Existing.first.str();
667         SuggestedPredefines += '\n';
668       }
669       continue;
670     }
671 
672     // If the macro was defined in one but undef'd in the other, we have a
673     // conflict.
674     if (Existing.second != Known->second.second) {
675       if (Diags) {
676         Diags->Report(diag::err_pch_macro_def_undef)
677           << MacroName << Known->second.second;
678       }
679       return true;
680     }
681 
682     // If the macro was #undef'd in both, or if the macro bodies are identical,
683     // it's fine.
684     if (Existing.second || Existing.first == Known->second.first)
685       continue;
686 
687     // The macro bodies differ; complain.
688     if (Diags) {
689       Diags->Report(diag::err_pch_macro_def_conflict)
690         << MacroName << Known->second.first << Existing.first;
691     }
692     return true;
693   }
694 
695   // Check whether we're using predefines.
696   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
697     if (Diags) {
698       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
699     }
700     return true;
701   }
702 
703   // Detailed record is important since it is used for the module cache hash.
704   if (LangOpts.Modules &&
705       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
706     if (Diags) {
707       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
708     }
709     return true;
710   }
711 
712   // Compute the #include and #include_macros lines we need.
713   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
714     StringRef File = ExistingPPOpts.Includes[I];
715 
716     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
717         !ExistingPPOpts.PCHThroughHeader.empty()) {
718       // In case the through header is an include, we must add all the includes
719       // to the predefines so the start point can be determined.
720       SuggestedPredefines += "#include \"";
721       SuggestedPredefines += File;
722       SuggestedPredefines += "\"\n";
723       continue;
724     }
725 
726     if (File == ExistingPPOpts.ImplicitPCHInclude)
727       continue;
728 
729     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
730           != PPOpts.Includes.end())
731       continue;
732 
733     SuggestedPredefines += "#include \"";
734     SuggestedPredefines += File;
735     SuggestedPredefines += "\"\n";
736   }
737 
738   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
739     StringRef File = ExistingPPOpts.MacroIncludes[I];
740     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
741                   File)
742         != PPOpts.MacroIncludes.end())
743       continue;
744 
745     SuggestedPredefines += "#__include_macros \"";
746     SuggestedPredefines += File;
747     SuggestedPredefines += "\"\n##\n";
748   }
749 
750   return false;
751 }
752 
753 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
754                                            bool Complain,
755                                            std::string &SuggestedPredefines) {
756   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
757 
758   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
759                                   Complain? &Reader.Diags : nullptr,
760                                   PP.getFileManager(),
761                                   SuggestedPredefines,
762                                   PP.getLangOpts());
763 }
764 
765 bool SimpleASTReaderListener::ReadPreprocessorOptions(
766                                   const PreprocessorOptions &PPOpts,
767                                   bool Complain,
768                                   std::string &SuggestedPredefines) {
769   return checkPreprocessorOptions(PPOpts,
770                                   PP.getPreprocessorOpts(),
771                                   nullptr,
772                                   PP.getFileManager(),
773                                   SuggestedPredefines,
774                                   PP.getLangOpts(),
775                                   false);
776 }
777 
778 /// Check the header search options deserialized from the control block
779 /// against the header search options in an existing preprocessor.
780 ///
781 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
782 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
783                                      StringRef SpecificModuleCachePath,
784                                      StringRef ExistingModuleCachePath,
785                                      DiagnosticsEngine *Diags,
786                                      const LangOptions &LangOpts) {
787   if (LangOpts.Modules) {
788     if (SpecificModuleCachePath != ExistingModuleCachePath) {
789       if (Diags)
790         Diags->Report(diag::err_pch_modulecache_mismatch)
791           << SpecificModuleCachePath << ExistingModuleCachePath;
792       return true;
793     }
794   }
795 
796   return false;
797 }
798 
799 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
800                                            StringRef SpecificModuleCachePath,
801                                            bool Complain) {
802   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
803                                   PP.getHeaderSearchInfo().getModuleCachePath(),
804                                   Complain ? &Reader.Diags : nullptr,
805                                   PP.getLangOpts());
806 }
807 
808 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
809   PP.setCounterValue(Value);
810 }
811 
812 //===----------------------------------------------------------------------===//
813 // AST reader implementation
814 //===----------------------------------------------------------------------===//
815 
816 static uint64_t readULEB(const unsigned char *&P) {
817   unsigned Length = 0;
818   const char *Error = nullptr;
819 
820   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
821   if (Error)
822     llvm::report_fatal_error(Error);
823   P += Length;
824   return Val;
825 }
826 
827 /// Read ULEB-encoded key length and data length.
828 static std::pair<unsigned, unsigned>
829 readULEBKeyDataLength(const unsigned char *&P) {
830   unsigned KeyLen = readULEB(P);
831   if ((unsigned)KeyLen != KeyLen)
832     llvm::report_fatal_error("key too large");
833 
834   unsigned DataLen = readULEB(P);
835   if ((unsigned)DataLen != DataLen)
836     llvm::report_fatal_error("data too large");
837 
838   return std::make_pair(KeyLen, DataLen);
839 }
840 
841 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
842                                            bool TakeOwnership) {
843   DeserializationListener = Listener;
844   OwnsDeserializationListener = TakeOwnership;
845 }
846 
847 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
848   return serialization::ComputeHash(Sel);
849 }
850 
851 std::pair<unsigned, unsigned>
852 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
853   return readULEBKeyDataLength(d);
854 }
855 
856 ASTSelectorLookupTrait::internal_key_type
857 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
858   using namespace llvm::support;
859 
860   SelectorTable &SelTable = Reader.getContext().Selectors;
861   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
862   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
863       F, endian::readNext<uint32_t, little, unaligned>(d));
864   if (N == 0)
865     return SelTable.getNullarySelector(FirstII);
866   else if (N == 1)
867     return SelTable.getUnarySelector(FirstII);
868 
869   SmallVector<IdentifierInfo *, 16> Args;
870   Args.push_back(FirstII);
871   for (unsigned I = 1; I != N; ++I)
872     Args.push_back(Reader.getLocalIdentifier(
873         F, endian::readNext<uint32_t, little, unaligned>(d)));
874 
875   return SelTable.getSelector(N, Args.data());
876 }
877 
878 ASTSelectorLookupTrait::data_type
879 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
880                                  unsigned DataLen) {
881   using namespace llvm::support;
882 
883   data_type Result;
884 
885   Result.ID = Reader.getGlobalSelectorID(
886       F, endian::readNext<uint32_t, little, unaligned>(d));
887   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
888   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
889   Result.InstanceBits = FullInstanceBits & 0x3;
890   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
891   Result.FactoryBits = FullFactoryBits & 0x3;
892   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
893   unsigned NumInstanceMethods = FullInstanceBits >> 3;
894   unsigned NumFactoryMethods = FullFactoryBits >> 3;
895 
896   // Load instance methods
897   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
898     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
899             F, endian::readNext<uint32_t, little, unaligned>(d)))
900       Result.Instance.push_back(Method);
901   }
902 
903   // Load factory methods
904   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
905     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
906             F, endian::readNext<uint32_t, little, unaligned>(d)))
907       Result.Factory.push_back(Method);
908   }
909 
910   return Result;
911 }
912 
913 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
914   return llvm::djbHash(a);
915 }
916 
917 std::pair<unsigned, unsigned>
918 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
919   return readULEBKeyDataLength(d);
920 }
921 
922 ASTIdentifierLookupTraitBase::internal_key_type
923 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
924   assert(n >= 2 && d[n-1] == '\0');
925   return StringRef((const char*) d, n-1);
926 }
927 
928 /// Whether the given identifier is "interesting".
929 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
930                                     bool IsModule) {
931   return II.hadMacroDefinition() || II.isPoisoned() ||
932          (!IsModule && II.getObjCOrBuiltinID()) ||
933          II.hasRevertedTokenIDToIdentifier() ||
934          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
935           II.getFETokenInfo());
936 }
937 
938 static bool readBit(unsigned &Bits) {
939   bool Value = Bits & 0x1;
940   Bits >>= 1;
941   return Value;
942 }
943 
944 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
945   using namespace llvm::support;
946 
947   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
948   return Reader.getGlobalIdentifierID(F, RawID >> 1);
949 }
950 
951 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
952   if (!II.isFromAST()) {
953     II.setIsFromAST();
954     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
955     if (isInterestingIdentifier(Reader, II, IsModule))
956       II.setChangedSinceDeserialization();
957   }
958 }
959 
960 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
961                                                    const unsigned char* d,
962                                                    unsigned DataLen) {
963   using namespace llvm::support;
964 
965   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
966   bool IsInteresting = RawID & 0x01;
967 
968   // Wipe out the "is interesting" bit.
969   RawID = RawID >> 1;
970 
971   // Build the IdentifierInfo and link the identifier ID with it.
972   IdentifierInfo *II = KnownII;
973   if (!II) {
974     II = &Reader.getIdentifierTable().getOwn(k);
975     KnownII = II;
976   }
977   markIdentifierFromAST(Reader, *II);
978   Reader.markIdentifierUpToDate(II);
979 
980   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
981   if (!IsInteresting) {
982     // For uninteresting identifiers, there's nothing else to do. Just notify
983     // the reader that we've finished loading this identifier.
984     Reader.SetIdentifierInfo(ID, II);
985     return II;
986   }
987 
988   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
989   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
990   bool CPlusPlusOperatorKeyword = readBit(Bits);
991   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
992   bool Poisoned = readBit(Bits);
993   bool ExtensionToken = readBit(Bits);
994   bool HadMacroDefinition = readBit(Bits);
995 
996   assert(Bits == 0 && "Extra bits in the identifier?");
997   DataLen -= 8;
998 
999   // Set or check the various bits in the IdentifierInfo structure.
1000   // Token IDs are read-only.
1001   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1002     II->revertTokenIDToIdentifier();
1003   if (!F.isModule())
1004     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1005   assert(II->isExtensionToken() == ExtensionToken &&
1006          "Incorrect extension token flag");
1007   (void)ExtensionToken;
1008   if (Poisoned)
1009     II->setIsPoisoned(true);
1010   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1011          "Incorrect C++ operator keyword flag");
1012   (void)CPlusPlusOperatorKeyword;
1013 
1014   // If this identifier is a macro, deserialize the macro
1015   // definition.
1016   if (HadMacroDefinition) {
1017     uint32_t MacroDirectivesOffset =
1018         endian::readNext<uint32_t, little, unaligned>(d);
1019     DataLen -= 4;
1020 
1021     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1022   }
1023 
1024   Reader.SetIdentifierInfo(ID, II);
1025 
1026   // Read all of the declarations visible at global scope with this
1027   // name.
1028   if (DataLen > 0) {
1029     SmallVector<uint32_t, 4> DeclIDs;
1030     for (; DataLen > 0; DataLen -= 4)
1031       DeclIDs.push_back(Reader.getGlobalDeclID(
1032           F, endian::readNext<uint32_t, little, unaligned>(d)));
1033     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1034   }
1035 
1036   return II;
1037 }
1038 
1039 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1040     : Kind(Name.getNameKind()) {
1041   switch (Kind) {
1042   case DeclarationName::Identifier:
1043     Data = (uint64_t)Name.getAsIdentifierInfo();
1044     break;
1045   case DeclarationName::ObjCZeroArgSelector:
1046   case DeclarationName::ObjCOneArgSelector:
1047   case DeclarationName::ObjCMultiArgSelector:
1048     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1049     break;
1050   case DeclarationName::CXXOperatorName:
1051     Data = Name.getCXXOverloadedOperator();
1052     break;
1053   case DeclarationName::CXXLiteralOperatorName:
1054     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1055     break;
1056   case DeclarationName::CXXDeductionGuideName:
1057     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1058                ->getDeclName().getAsIdentifierInfo();
1059     break;
1060   case DeclarationName::CXXConstructorName:
1061   case DeclarationName::CXXDestructorName:
1062   case DeclarationName::CXXConversionFunctionName:
1063   case DeclarationName::CXXUsingDirective:
1064     Data = 0;
1065     break;
1066   }
1067 }
1068 
1069 unsigned DeclarationNameKey::getHash() const {
1070   llvm::FoldingSetNodeID ID;
1071   ID.AddInteger(Kind);
1072 
1073   switch (Kind) {
1074   case DeclarationName::Identifier:
1075   case DeclarationName::CXXLiteralOperatorName:
1076   case DeclarationName::CXXDeductionGuideName:
1077     ID.AddString(((IdentifierInfo*)Data)->getName());
1078     break;
1079   case DeclarationName::ObjCZeroArgSelector:
1080   case DeclarationName::ObjCOneArgSelector:
1081   case DeclarationName::ObjCMultiArgSelector:
1082     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1083     break;
1084   case DeclarationName::CXXOperatorName:
1085     ID.AddInteger((OverloadedOperatorKind)Data);
1086     break;
1087   case DeclarationName::CXXConstructorName:
1088   case DeclarationName::CXXDestructorName:
1089   case DeclarationName::CXXConversionFunctionName:
1090   case DeclarationName::CXXUsingDirective:
1091     break;
1092   }
1093 
1094   return ID.ComputeHash();
1095 }
1096 
1097 ModuleFile *
1098 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1099   using namespace llvm::support;
1100 
1101   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1102   return Reader.getLocalModuleFile(F, ModuleFileID);
1103 }
1104 
1105 std::pair<unsigned, unsigned>
1106 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1107   return readULEBKeyDataLength(d);
1108 }
1109 
1110 ASTDeclContextNameLookupTrait::internal_key_type
1111 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1112   using namespace llvm::support;
1113 
1114   auto Kind = (DeclarationName::NameKind)*d++;
1115   uint64_t Data;
1116   switch (Kind) {
1117   case DeclarationName::Identifier:
1118   case DeclarationName::CXXLiteralOperatorName:
1119   case DeclarationName::CXXDeductionGuideName:
1120     Data = (uint64_t)Reader.getLocalIdentifier(
1121         F, endian::readNext<uint32_t, little, unaligned>(d));
1122     break;
1123   case DeclarationName::ObjCZeroArgSelector:
1124   case DeclarationName::ObjCOneArgSelector:
1125   case DeclarationName::ObjCMultiArgSelector:
1126     Data =
1127         (uint64_t)Reader.getLocalSelector(
1128                              F, endian::readNext<uint32_t, little, unaligned>(
1129                                     d)).getAsOpaquePtr();
1130     break;
1131   case DeclarationName::CXXOperatorName:
1132     Data = *d++; // OverloadedOperatorKind
1133     break;
1134   case DeclarationName::CXXConstructorName:
1135   case DeclarationName::CXXDestructorName:
1136   case DeclarationName::CXXConversionFunctionName:
1137   case DeclarationName::CXXUsingDirective:
1138     Data = 0;
1139     break;
1140   }
1141 
1142   return DeclarationNameKey(Kind, Data);
1143 }
1144 
1145 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1146                                                  const unsigned char *d,
1147                                                  unsigned DataLen,
1148                                                  data_type_builder &Val) {
1149   using namespace llvm::support;
1150 
1151   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1152     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1153     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1154   }
1155 }
1156 
1157 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1158                                               BitstreamCursor &Cursor,
1159                                               uint64_t Offset,
1160                                               DeclContext *DC) {
1161   assert(Offset != 0);
1162 
1163   SavedStreamPosition SavedPosition(Cursor);
1164   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1165     Error(std::move(Err));
1166     return true;
1167   }
1168 
1169   RecordData Record;
1170   StringRef Blob;
1171   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1172   if (!MaybeCode) {
1173     Error(MaybeCode.takeError());
1174     return true;
1175   }
1176   unsigned Code = MaybeCode.get();
1177 
1178   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1179   if (!MaybeRecCode) {
1180     Error(MaybeRecCode.takeError());
1181     return true;
1182   }
1183   unsigned RecCode = MaybeRecCode.get();
1184   if (RecCode != DECL_CONTEXT_LEXICAL) {
1185     Error("Expected lexical block");
1186     return true;
1187   }
1188 
1189   assert(!isa<TranslationUnitDecl>(DC) &&
1190          "expected a TU_UPDATE_LEXICAL record for TU");
1191   // If we are handling a C++ class template instantiation, we can see multiple
1192   // lexical updates for the same record. It's important that we select only one
1193   // of them, so that field numbering works properly. Just pick the first one we
1194   // see.
1195   auto &Lex = LexicalDecls[DC];
1196   if (!Lex.first) {
1197     Lex = std::make_pair(
1198         &M, llvm::makeArrayRef(
1199                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1200                     Blob.data()),
1201                 Blob.size() / 4));
1202   }
1203   DC->setHasExternalLexicalStorage(true);
1204   return false;
1205 }
1206 
1207 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1208                                               BitstreamCursor &Cursor,
1209                                               uint64_t Offset,
1210                                               DeclID ID) {
1211   assert(Offset != 0);
1212 
1213   SavedStreamPosition SavedPosition(Cursor);
1214   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1215     Error(std::move(Err));
1216     return true;
1217   }
1218 
1219   RecordData Record;
1220   StringRef Blob;
1221   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1222   if (!MaybeCode) {
1223     Error(MaybeCode.takeError());
1224     return true;
1225   }
1226   unsigned Code = MaybeCode.get();
1227 
1228   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1229   if (!MaybeRecCode) {
1230     Error(MaybeRecCode.takeError());
1231     return true;
1232   }
1233   unsigned RecCode = MaybeRecCode.get();
1234   if (RecCode != DECL_CONTEXT_VISIBLE) {
1235     Error("Expected visible lookup table block");
1236     return true;
1237   }
1238 
1239   // We can't safely determine the primary context yet, so delay attaching the
1240   // lookup table until we're done with recursive deserialization.
1241   auto *Data = (const unsigned char*)Blob.data();
1242   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1243   return false;
1244 }
1245 
1246 void ASTReader::Error(StringRef Msg) const {
1247   Error(diag::err_fe_pch_malformed, Msg);
1248   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1249       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1250     Diag(diag::note_module_cache_path)
1251       << PP.getHeaderSearchInfo().getModuleCachePath();
1252   }
1253 }
1254 
1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1256                       StringRef Arg3) const {
1257   if (Diags.isDiagnosticInFlight())
1258     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1259   else
1260     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1261 }
1262 
1263 void ASTReader::Error(llvm::Error &&Err) const {
1264   Error(toString(std::move(Err)));
1265 }
1266 
1267 //===----------------------------------------------------------------------===//
1268 // Source Manager Deserialization
1269 //===----------------------------------------------------------------------===//
1270 
1271 /// Read the line table in the source manager block.
1272 /// \returns true if there was an error.
1273 bool ASTReader::ParseLineTable(ModuleFile &F,
1274                                const RecordData &Record) {
1275   unsigned Idx = 0;
1276   LineTableInfo &LineTable = SourceMgr.getLineTable();
1277 
1278   // Parse the file names
1279   std::map<int, int> FileIDs;
1280   FileIDs[-1] = -1; // For unspecified filenames.
1281   for (unsigned I = 0; Record[Idx]; ++I) {
1282     // Extract the file name
1283     auto Filename = ReadPath(F, Record, Idx);
1284     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1285   }
1286   ++Idx;
1287 
1288   // Parse the line entries
1289   std::vector<LineEntry> Entries;
1290   while (Idx < Record.size()) {
1291     int FID = Record[Idx++];
1292     assert(FID >= 0 && "Serialized line entries for non-local file.");
1293     // Remap FileID from 1-based old view.
1294     FID += F.SLocEntryBaseID - 1;
1295 
1296     // Extract the line entries
1297     unsigned NumEntries = Record[Idx++];
1298     assert(NumEntries && "no line entries for file ID");
1299     Entries.clear();
1300     Entries.reserve(NumEntries);
1301     for (unsigned I = 0; I != NumEntries; ++I) {
1302       unsigned FileOffset = Record[Idx++];
1303       unsigned LineNo = Record[Idx++];
1304       int FilenameID = FileIDs[Record[Idx++]];
1305       SrcMgr::CharacteristicKind FileKind
1306         = (SrcMgr::CharacteristicKind)Record[Idx++];
1307       unsigned IncludeOffset = Record[Idx++];
1308       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1309                                        FileKind, IncludeOffset));
1310     }
1311     LineTable.AddEntry(FileID::get(FID), Entries);
1312   }
1313 
1314   return false;
1315 }
1316 
1317 /// Read a source manager block
1318 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1319   using namespace SrcMgr;
1320 
1321   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1322 
1323   // Set the source-location entry cursor to the current position in
1324   // the stream. This cursor will be used to read the contents of the
1325   // source manager block initially, and then lazily read
1326   // source-location entries as needed.
1327   SLocEntryCursor = F.Stream;
1328 
1329   // The stream itself is going to skip over the source manager block.
1330   if (llvm::Error Err = F.Stream.SkipBlock()) {
1331     Error(std::move(Err));
1332     return true;
1333   }
1334 
1335   // Enter the source manager block.
1336   if (llvm::Error Err =
1337           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1338     Error(std::move(Err));
1339     return true;
1340   }
1341   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1342 
1343   RecordData Record;
1344   while (true) {
1345     Expected<llvm::BitstreamEntry> MaybeE =
1346         SLocEntryCursor.advanceSkippingSubblocks();
1347     if (!MaybeE) {
1348       Error(MaybeE.takeError());
1349       return true;
1350     }
1351     llvm::BitstreamEntry E = MaybeE.get();
1352 
1353     switch (E.Kind) {
1354     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1355     case llvm::BitstreamEntry::Error:
1356       Error("malformed block record in AST file");
1357       return true;
1358     case llvm::BitstreamEntry::EndBlock:
1359       return false;
1360     case llvm::BitstreamEntry::Record:
1361       // The interesting case.
1362       break;
1363     }
1364 
1365     // Read a record.
1366     Record.clear();
1367     StringRef Blob;
1368     Expected<unsigned> MaybeRecord =
1369         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1370     if (!MaybeRecord) {
1371       Error(MaybeRecord.takeError());
1372       return true;
1373     }
1374     switch (MaybeRecord.get()) {
1375     default:  // Default behavior: ignore.
1376       break;
1377 
1378     case SM_SLOC_FILE_ENTRY:
1379     case SM_SLOC_BUFFER_ENTRY:
1380     case SM_SLOC_EXPANSION_ENTRY:
1381       // Once we hit one of the source location entries, we're done.
1382       return false;
1383     }
1384   }
1385 }
1386 
1387 /// If a header file is not found at the path that we expect it to be
1388 /// and the PCH file was moved from its original location, try to resolve the
1389 /// file by assuming that header+PCH were moved together and the header is in
1390 /// the same place relative to the PCH.
1391 static std::string
1392 resolveFileRelativeToOriginalDir(const std::string &Filename,
1393                                  const std::string &OriginalDir,
1394                                  const std::string &CurrDir) {
1395   assert(OriginalDir != CurrDir &&
1396          "No point trying to resolve the file if the PCH dir didn't change");
1397 
1398   using namespace llvm::sys;
1399 
1400   SmallString<128> filePath(Filename);
1401   fs::make_absolute(filePath);
1402   assert(path::is_absolute(OriginalDir));
1403   SmallString<128> currPCHPath(CurrDir);
1404 
1405   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1406                        fileDirE = path::end(path::parent_path(filePath));
1407   path::const_iterator origDirI = path::begin(OriginalDir),
1408                        origDirE = path::end(OriginalDir);
1409   // Skip the common path components from filePath and OriginalDir.
1410   while (fileDirI != fileDirE && origDirI != origDirE &&
1411          *fileDirI == *origDirI) {
1412     ++fileDirI;
1413     ++origDirI;
1414   }
1415   for (; origDirI != origDirE; ++origDirI)
1416     path::append(currPCHPath, "..");
1417   path::append(currPCHPath, fileDirI, fileDirE);
1418   path::append(currPCHPath, path::filename(Filename));
1419   return std::string(currPCHPath.str());
1420 }
1421 
1422 bool ASTReader::ReadSLocEntry(int ID) {
1423   if (ID == 0)
1424     return false;
1425 
1426   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1427     Error("source location entry ID out-of-range for AST file");
1428     return true;
1429   }
1430 
1431   // Local helper to read the (possibly-compressed) buffer data following the
1432   // entry record.
1433   auto ReadBuffer = [this](
1434       BitstreamCursor &SLocEntryCursor,
1435       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1436     RecordData Record;
1437     StringRef Blob;
1438     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1439     if (!MaybeCode) {
1440       Error(MaybeCode.takeError());
1441       return nullptr;
1442     }
1443     unsigned Code = MaybeCode.get();
1444 
1445     Expected<unsigned> MaybeRecCode =
1446         SLocEntryCursor.readRecord(Code, Record, &Blob);
1447     if (!MaybeRecCode) {
1448       Error(MaybeRecCode.takeError());
1449       return nullptr;
1450     }
1451     unsigned RecCode = MaybeRecCode.get();
1452 
1453     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1454       if (!llvm::zlib::isAvailable()) {
1455         Error("zlib is not available");
1456         return nullptr;
1457       }
1458       SmallString<0> Uncompressed;
1459       if (llvm::Error E =
1460               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1461         Error("could not decompress embedded file contents: " +
1462               llvm::toString(std::move(E)));
1463         return nullptr;
1464       }
1465       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1466     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1467       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1468     } else {
1469       Error("AST record has invalid code");
1470       return nullptr;
1471     }
1472   };
1473 
1474   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1475   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1476           F->SLocEntryOffsetsBase +
1477           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1478     Error(std::move(Err));
1479     return true;
1480   }
1481 
1482   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1483   unsigned BaseOffset = F->SLocEntryBaseOffset;
1484 
1485   ++NumSLocEntriesRead;
1486   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1487   if (!MaybeEntry) {
1488     Error(MaybeEntry.takeError());
1489     return true;
1490   }
1491   llvm::BitstreamEntry Entry = MaybeEntry.get();
1492 
1493   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1494     Error("incorrectly-formatted source location entry in AST file");
1495     return true;
1496   }
1497 
1498   RecordData Record;
1499   StringRef Blob;
1500   Expected<unsigned> MaybeSLOC =
1501       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1502   if (!MaybeSLOC) {
1503     Error(MaybeSLOC.takeError());
1504     return true;
1505   }
1506   switch (MaybeSLOC.get()) {
1507   default:
1508     Error("incorrectly-formatted source location entry in AST file");
1509     return true;
1510 
1511   case SM_SLOC_FILE_ENTRY: {
1512     // We will detect whether a file changed and return 'Failure' for it, but
1513     // we will also try to fail gracefully by setting up the SLocEntry.
1514     unsigned InputID = Record[4];
1515     InputFile IF = getInputFile(*F, InputID);
1516     Optional<FileEntryRef> File = IF.getFile();
1517     bool OverriddenBuffer = IF.isOverridden();
1518 
1519     // Note that we only check if a File was returned. If it was out-of-date
1520     // we have complained but we will continue creating a FileID to recover
1521     // gracefully.
1522     if (!File)
1523       return true;
1524 
1525     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1526     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1527       // This is the module's main file.
1528       IncludeLoc = getImportLocation(F);
1529     }
1530     SrcMgr::CharacteristicKind
1531       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1532     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1533                                         BaseOffset + Record[0]);
1534     SrcMgr::FileInfo &FileInfo =
1535           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1536     FileInfo.NumCreatedFIDs = Record[5];
1537     if (Record[3])
1538       FileInfo.setHasLineDirectives();
1539 
1540     unsigned NumFileDecls = Record[7];
1541     if (NumFileDecls && ContextObj) {
1542       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1543       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1544       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1545                                                              NumFileDecls));
1546     }
1547 
1548     const SrcMgr::ContentCache &ContentCache =
1549         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1550     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1551         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1552         !ContentCache.getBufferIfLoaded()) {
1553       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1554       if (!Buffer)
1555         return true;
1556       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1557     }
1558 
1559     break;
1560   }
1561 
1562   case SM_SLOC_BUFFER_ENTRY: {
1563     const char *Name = Blob.data();
1564     unsigned Offset = Record[0];
1565     SrcMgr::CharacteristicKind
1566       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1567     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1568     if (IncludeLoc.isInvalid() && F->isModule()) {
1569       IncludeLoc = getImportLocation(F);
1570     }
1571 
1572     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1573     if (!Buffer)
1574       return true;
1575     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1576                            BaseOffset + Offset, IncludeLoc);
1577     break;
1578   }
1579 
1580   case SM_SLOC_EXPANSION_ENTRY: {
1581     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1582     SourceMgr.createExpansionLoc(SpellingLoc,
1583                                      ReadSourceLocation(*F, Record[2]),
1584                                      ReadSourceLocation(*F, Record[3]),
1585                                      Record[5],
1586                                      Record[4],
1587                                      ID,
1588                                      BaseOffset + Record[0]);
1589     break;
1590   }
1591   }
1592 
1593   return false;
1594 }
1595 
1596 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1597   if (ID == 0)
1598     return std::make_pair(SourceLocation(), "");
1599 
1600   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1601     Error("source location entry ID out-of-range for AST file");
1602     return std::make_pair(SourceLocation(), "");
1603   }
1604 
1605   // Find which module file this entry lands in.
1606   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1607   if (!M->isModule())
1608     return std::make_pair(SourceLocation(), "");
1609 
1610   // FIXME: Can we map this down to a particular submodule? That would be
1611   // ideal.
1612   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1613 }
1614 
1615 /// Find the location where the module F is imported.
1616 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1617   if (F->ImportLoc.isValid())
1618     return F->ImportLoc;
1619 
1620   // Otherwise we have a PCH. It's considered to be "imported" at the first
1621   // location of its includer.
1622   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1623     // Main file is the importer.
1624     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1625     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1626   }
1627   return F->ImportedBy[0]->FirstLoc;
1628 }
1629 
1630 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1631 /// the abbreviations that are at the top of the block and then leave the cursor
1632 /// pointing into the block.
1633 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1634                                  uint64_t *StartOfBlockOffset) {
1635   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1636     // FIXME this drops errors on the floor.
1637     consumeError(std::move(Err));
1638     return true;
1639   }
1640 
1641   if (StartOfBlockOffset)
1642     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1643 
1644   while (true) {
1645     uint64_t Offset = Cursor.GetCurrentBitNo();
1646     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1647     if (!MaybeCode) {
1648       // FIXME this drops errors on the floor.
1649       consumeError(MaybeCode.takeError());
1650       return true;
1651     }
1652     unsigned Code = MaybeCode.get();
1653 
1654     // We expect all abbrevs to be at the start of the block.
1655     if (Code != llvm::bitc::DEFINE_ABBREV) {
1656       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1657         // FIXME this drops errors on the floor.
1658         consumeError(std::move(Err));
1659         return true;
1660       }
1661       return false;
1662     }
1663     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1664       // FIXME this drops errors on the floor.
1665       consumeError(std::move(Err));
1666       return true;
1667     }
1668   }
1669 }
1670 
1671 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1672                            unsigned &Idx) {
1673   Token Tok;
1674   Tok.startToken();
1675   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1676   Tok.setLength(Record[Idx++]);
1677   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1678     Tok.setIdentifierInfo(II);
1679   Tok.setKind((tok::TokenKind)Record[Idx++]);
1680   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1681   return Tok;
1682 }
1683 
1684 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1685   BitstreamCursor &Stream = F.MacroCursor;
1686 
1687   // Keep track of where we are in the stream, then jump back there
1688   // after reading this macro.
1689   SavedStreamPosition SavedPosition(Stream);
1690 
1691   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1692     // FIXME this drops errors on the floor.
1693     consumeError(std::move(Err));
1694     return nullptr;
1695   }
1696   RecordData Record;
1697   SmallVector<IdentifierInfo*, 16> MacroParams;
1698   MacroInfo *Macro = nullptr;
1699 
1700   while (true) {
1701     // Advance to the next record, but if we get to the end of the block, don't
1702     // pop it (removing all the abbreviations from the cursor) since we want to
1703     // be able to reseek within the block and read entries.
1704     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1705     Expected<llvm::BitstreamEntry> MaybeEntry =
1706         Stream.advanceSkippingSubblocks(Flags);
1707     if (!MaybeEntry) {
1708       Error(MaybeEntry.takeError());
1709       return Macro;
1710     }
1711     llvm::BitstreamEntry Entry = MaybeEntry.get();
1712 
1713     switch (Entry.Kind) {
1714     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1715     case llvm::BitstreamEntry::Error:
1716       Error("malformed block record in AST file");
1717       return Macro;
1718     case llvm::BitstreamEntry::EndBlock:
1719       return Macro;
1720     case llvm::BitstreamEntry::Record:
1721       // The interesting case.
1722       break;
1723     }
1724 
1725     // Read a record.
1726     Record.clear();
1727     PreprocessorRecordTypes RecType;
1728     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1729       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1730     else {
1731       Error(MaybeRecType.takeError());
1732       return Macro;
1733     }
1734     switch (RecType) {
1735     case PP_MODULE_MACRO:
1736     case PP_MACRO_DIRECTIVE_HISTORY:
1737       return Macro;
1738 
1739     case PP_MACRO_OBJECT_LIKE:
1740     case PP_MACRO_FUNCTION_LIKE: {
1741       // If we already have a macro, that means that we've hit the end
1742       // of the definition of the macro we were looking for. We're
1743       // done.
1744       if (Macro)
1745         return Macro;
1746 
1747       unsigned NextIndex = 1; // Skip identifier ID.
1748       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1749       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1750       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1751       MI->setIsUsed(Record[NextIndex++]);
1752       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1753 
1754       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1755         // Decode function-like macro info.
1756         bool isC99VarArgs = Record[NextIndex++];
1757         bool isGNUVarArgs = Record[NextIndex++];
1758         bool hasCommaPasting = Record[NextIndex++];
1759         MacroParams.clear();
1760         unsigned NumArgs = Record[NextIndex++];
1761         for (unsigned i = 0; i != NumArgs; ++i)
1762           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1763 
1764         // Install function-like macro info.
1765         MI->setIsFunctionLike();
1766         if (isC99VarArgs) MI->setIsC99Varargs();
1767         if (isGNUVarArgs) MI->setIsGNUVarargs();
1768         if (hasCommaPasting) MI->setHasCommaPasting();
1769         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1770       }
1771 
1772       // Remember that we saw this macro last so that we add the tokens that
1773       // form its body to it.
1774       Macro = MI;
1775 
1776       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1777           Record[NextIndex]) {
1778         // We have a macro definition. Register the association
1779         PreprocessedEntityID
1780             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1781         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1782         PreprocessingRecord::PPEntityID PPID =
1783             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1784         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1785             PPRec.getPreprocessedEntity(PPID));
1786         if (PPDef)
1787           PPRec.RegisterMacroDefinition(Macro, PPDef);
1788       }
1789 
1790       ++NumMacrosRead;
1791       break;
1792     }
1793 
1794     case PP_TOKEN: {
1795       // If we see a TOKEN before a PP_MACRO_*, then the file is
1796       // erroneous, just pretend we didn't see this.
1797       if (!Macro) break;
1798 
1799       unsigned Idx = 0;
1800       Token Tok = ReadToken(F, Record, Idx);
1801       Macro->AddTokenToBody(Tok);
1802       break;
1803     }
1804     }
1805   }
1806 }
1807 
1808 PreprocessedEntityID
1809 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1810                                          unsigned LocalID) const {
1811   if (!M.ModuleOffsetMap.empty())
1812     ReadModuleOffsetMap(M);
1813 
1814   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1815     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1816   assert(I != M.PreprocessedEntityRemap.end()
1817          && "Invalid index into preprocessed entity index remap");
1818 
1819   return LocalID + I->second;
1820 }
1821 
1822 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1823   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1824 }
1825 
1826 HeaderFileInfoTrait::internal_key_type
1827 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1828   internal_key_type ikey = {FE->getSize(),
1829                             M.HasTimestamps ? FE->getModificationTime() : 0,
1830                             FE->getName(), /*Imported*/ false};
1831   return ikey;
1832 }
1833 
1834 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1835   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1836     return false;
1837 
1838   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1839     return true;
1840 
1841   // Determine whether the actual files are equivalent.
1842   FileManager &FileMgr = Reader.getFileManager();
1843   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1844     if (!Key.Imported) {
1845       if (auto File = FileMgr.getFile(Key.Filename))
1846         return *File;
1847       return nullptr;
1848     }
1849 
1850     std::string Resolved = std::string(Key.Filename);
1851     Reader.ResolveImportedPath(M, Resolved);
1852     if (auto File = FileMgr.getFile(Resolved))
1853       return *File;
1854     return nullptr;
1855   };
1856 
1857   const FileEntry *FEA = GetFile(a);
1858   const FileEntry *FEB = GetFile(b);
1859   return FEA && FEA == FEB;
1860 }
1861 
1862 std::pair<unsigned, unsigned>
1863 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1864   return readULEBKeyDataLength(d);
1865 }
1866 
1867 HeaderFileInfoTrait::internal_key_type
1868 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1869   using namespace llvm::support;
1870 
1871   internal_key_type ikey;
1872   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1873   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1874   ikey.Filename = (const char *)d;
1875   ikey.Imported = true;
1876   return ikey;
1877 }
1878 
1879 HeaderFileInfoTrait::data_type
1880 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1881                               unsigned DataLen) {
1882   using namespace llvm::support;
1883 
1884   const unsigned char *End = d + DataLen;
1885   HeaderFileInfo HFI;
1886   unsigned Flags = *d++;
1887   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1888   HFI.isImport |= (Flags >> 5) & 0x01;
1889   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1890   HFI.DirInfo = (Flags >> 1) & 0x07;
1891   HFI.IndexHeaderMapHeader = Flags & 0x01;
1892   // FIXME: Find a better way to handle this. Maybe just store a
1893   // "has been included" flag?
1894   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1895                              HFI.NumIncludes);
1896   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1897       M, endian::readNext<uint32_t, little, unaligned>(d));
1898   if (unsigned FrameworkOffset =
1899           endian::readNext<uint32_t, little, unaligned>(d)) {
1900     // The framework offset is 1 greater than the actual offset,
1901     // since 0 is used as an indicator for "no framework name".
1902     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1903     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1904   }
1905 
1906   assert((End - d) % 4 == 0 &&
1907          "Wrong data length in HeaderFileInfo deserialization");
1908   while (d != End) {
1909     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1910     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1911     LocalSMID >>= 2;
1912 
1913     // This header is part of a module. Associate it with the module to enable
1914     // implicit module import.
1915     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1916     Module *Mod = Reader.getSubmodule(GlobalSMID);
1917     FileManager &FileMgr = Reader.getFileManager();
1918     ModuleMap &ModMap =
1919         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1920 
1921     std::string Filename = std::string(key.Filename);
1922     if (key.Imported)
1923       Reader.ResolveImportedPath(M, Filename);
1924     // FIXME: NameAsWritten
1925     Module::Header H = {std::string(key.Filename), "",
1926                         *FileMgr.getFile(Filename)};
1927     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1928     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1929   }
1930 
1931   // This HeaderFileInfo was externally loaded.
1932   HFI.External = true;
1933   HFI.IsValid = true;
1934   return HFI;
1935 }
1936 
1937 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1938                                 uint32_t MacroDirectivesOffset) {
1939   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1940   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1941 }
1942 
1943 void ASTReader::ReadDefinedMacros() {
1944   // Note that we are loading defined macros.
1945   Deserializing Macros(this);
1946 
1947   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1948     BitstreamCursor &MacroCursor = I.MacroCursor;
1949 
1950     // If there was no preprocessor block, skip this file.
1951     if (MacroCursor.getBitcodeBytes().empty())
1952       continue;
1953 
1954     BitstreamCursor Cursor = MacroCursor;
1955     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1956       Error(std::move(Err));
1957       return;
1958     }
1959 
1960     RecordData Record;
1961     while (true) {
1962       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1963       if (!MaybeE) {
1964         Error(MaybeE.takeError());
1965         return;
1966       }
1967       llvm::BitstreamEntry E = MaybeE.get();
1968 
1969       switch (E.Kind) {
1970       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1971       case llvm::BitstreamEntry::Error:
1972         Error("malformed block record in AST file");
1973         return;
1974       case llvm::BitstreamEntry::EndBlock:
1975         goto NextCursor;
1976 
1977       case llvm::BitstreamEntry::Record: {
1978         Record.clear();
1979         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1980         if (!MaybeRecord) {
1981           Error(MaybeRecord.takeError());
1982           return;
1983         }
1984         switch (MaybeRecord.get()) {
1985         default:  // Default behavior: ignore.
1986           break;
1987 
1988         case PP_MACRO_OBJECT_LIKE:
1989         case PP_MACRO_FUNCTION_LIKE: {
1990           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1991           if (II->isOutOfDate())
1992             updateOutOfDateIdentifier(*II);
1993           break;
1994         }
1995 
1996         case PP_TOKEN:
1997           // Ignore tokens.
1998           break;
1999         }
2000         break;
2001       }
2002       }
2003     }
2004     NextCursor:  ;
2005   }
2006 }
2007 
2008 namespace {
2009 
2010   /// Visitor class used to look up identifirs in an AST file.
2011   class IdentifierLookupVisitor {
2012     StringRef Name;
2013     unsigned NameHash;
2014     unsigned PriorGeneration;
2015     unsigned &NumIdentifierLookups;
2016     unsigned &NumIdentifierLookupHits;
2017     IdentifierInfo *Found = nullptr;
2018 
2019   public:
2020     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2021                             unsigned &NumIdentifierLookups,
2022                             unsigned &NumIdentifierLookupHits)
2023       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2024         PriorGeneration(PriorGeneration),
2025         NumIdentifierLookups(NumIdentifierLookups),
2026         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2027 
2028     bool operator()(ModuleFile &M) {
2029       // If we've already searched this module file, skip it now.
2030       if (M.Generation <= PriorGeneration)
2031         return true;
2032 
2033       ASTIdentifierLookupTable *IdTable
2034         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2035       if (!IdTable)
2036         return false;
2037 
2038       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2039                                      Found);
2040       ++NumIdentifierLookups;
2041       ASTIdentifierLookupTable::iterator Pos =
2042           IdTable->find_hashed(Name, NameHash, &Trait);
2043       if (Pos == IdTable->end())
2044         return false;
2045 
2046       // Dereferencing the iterator has the effect of building the
2047       // IdentifierInfo node and populating it with the various
2048       // declarations it needs.
2049       ++NumIdentifierLookupHits;
2050       Found = *Pos;
2051       return true;
2052     }
2053 
2054     // Retrieve the identifier info found within the module
2055     // files.
2056     IdentifierInfo *getIdentifierInfo() const { return Found; }
2057   };
2058 
2059 } // namespace
2060 
2061 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2062   // Note that we are loading an identifier.
2063   Deserializing AnIdentifier(this);
2064 
2065   unsigned PriorGeneration = 0;
2066   if (getContext().getLangOpts().Modules)
2067     PriorGeneration = IdentifierGeneration[&II];
2068 
2069   // If there is a global index, look there first to determine which modules
2070   // provably do not have any results for this identifier.
2071   GlobalModuleIndex::HitSet Hits;
2072   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2073   if (!loadGlobalIndex()) {
2074     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2075       HitsPtr = &Hits;
2076     }
2077   }
2078 
2079   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2080                                   NumIdentifierLookups,
2081                                   NumIdentifierLookupHits);
2082   ModuleMgr.visit(Visitor, HitsPtr);
2083   markIdentifierUpToDate(&II);
2084 }
2085 
2086 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2087   if (!II)
2088     return;
2089 
2090   II->setOutOfDate(false);
2091 
2092   // Update the generation for this identifier.
2093   if (getContext().getLangOpts().Modules)
2094     IdentifierGeneration[II] = getGeneration();
2095 }
2096 
2097 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2098                                     const PendingMacroInfo &PMInfo) {
2099   ModuleFile &M = *PMInfo.M;
2100 
2101   BitstreamCursor &Cursor = M.MacroCursor;
2102   SavedStreamPosition SavedPosition(Cursor);
2103   if (llvm::Error Err =
2104           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2105     Error(std::move(Err));
2106     return;
2107   }
2108 
2109   struct ModuleMacroRecord {
2110     SubmoduleID SubModID;
2111     MacroInfo *MI;
2112     SmallVector<SubmoduleID, 8> Overrides;
2113   };
2114   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2115 
2116   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2117   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2118   // macro histroy.
2119   RecordData Record;
2120   while (true) {
2121     Expected<llvm::BitstreamEntry> MaybeEntry =
2122         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2123     if (!MaybeEntry) {
2124       Error(MaybeEntry.takeError());
2125       return;
2126     }
2127     llvm::BitstreamEntry Entry = MaybeEntry.get();
2128 
2129     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2130       Error("malformed block record in AST file");
2131       return;
2132     }
2133 
2134     Record.clear();
2135     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2136     if (!MaybePP) {
2137       Error(MaybePP.takeError());
2138       return;
2139     }
2140     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2141     case PP_MACRO_DIRECTIVE_HISTORY:
2142       break;
2143 
2144     case PP_MODULE_MACRO: {
2145       ModuleMacros.push_back(ModuleMacroRecord());
2146       auto &Info = ModuleMacros.back();
2147       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2148       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2149       for (int I = 2, N = Record.size(); I != N; ++I)
2150         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2151       continue;
2152     }
2153 
2154     default:
2155       Error("malformed block record in AST file");
2156       return;
2157     }
2158 
2159     // We found the macro directive history; that's the last record
2160     // for this macro.
2161     break;
2162   }
2163 
2164   // Module macros are listed in reverse dependency order.
2165   {
2166     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2167     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2168     for (auto &MMR : ModuleMacros) {
2169       Overrides.clear();
2170       for (unsigned ModID : MMR.Overrides) {
2171         Module *Mod = getSubmodule(ModID);
2172         auto *Macro = PP.getModuleMacro(Mod, II);
2173         assert(Macro && "missing definition for overridden macro");
2174         Overrides.push_back(Macro);
2175       }
2176 
2177       bool Inserted = false;
2178       Module *Owner = getSubmodule(MMR.SubModID);
2179       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2180     }
2181   }
2182 
2183   // Don't read the directive history for a module; we don't have anywhere
2184   // to put it.
2185   if (M.isModule())
2186     return;
2187 
2188   // Deserialize the macro directives history in reverse source-order.
2189   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2190   unsigned Idx = 0, N = Record.size();
2191   while (Idx < N) {
2192     MacroDirective *MD = nullptr;
2193     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2194     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2195     switch (K) {
2196     case MacroDirective::MD_Define: {
2197       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2198       MD = PP.AllocateDefMacroDirective(MI, Loc);
2199       break;
2200     }
2201     case MacroDirective::MD_Undefine:
2202       MD = PP.AllocateUndefMacroDirective(Loc);
2203       break;
2204     case MacroDirective::MD_Visibility:
2205       bool isPublic = Record[Idx++];
2206       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2207       break;
2208     }
2209 
2210     if (!Latest)
2211       Latest = MD;
2212     if (Earliest)
2213       Earliest->setPrevious(MD);
2214     Earliest = MD;
2215   }
2216 
2217   if (Latest)
2218     PP.setLoadedMacroDirective(II, Earliest, Latest);
2219 }
2220 
2221 bool ASTReader::shouldDisableValidationForFile(
2222     const serialization::ModuleFile &M) const {
2223   if (DisableValidationKind == DisableValidationForModuleKind::None)
2224     return false;
2225 
2226   // If a PCH is loaded and validation is disabled for PCH then disable
2227   // validation for the PCH and the modules it loads.
2228   ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2229 
2230   switch (K) {
2231   case MK_MainFile:
2232   case MK_Preamble:
2233   case MK_PCH:
2234     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2235   case MK_ImplicitModule:
2236   case MK_ExplicitModule:
2237   case MK_PrebuiltModule:
2238     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2239   }
2240 
2241   return false;
2242 }
2243 
2244 ASTReader::InputFileInfo
2245 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2246   // Go find this input file.
2247   BitstreamCursor &Cursor = F.InputFilesCursor;
2248   SavedStreamPosition SavedPosition(Cursor);
2249   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2250     // FIXME this drops errors on the floor.
2251     consumeError(std::move(Err));
2252   }
2253 
2254   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2255   if (!MaybeCode) {
2256     // FIXME this drops errors on the floor.
2257     consumeError(MaybeCode.takeError());
2258   }
2259   unsigned Code = MaybeCode.get();
2260   RecordData Record;
2261   StringRef Blob;
2262 
2263   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2264     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2265            "invalid record type for input file");
2266   else {
2267     // FIXME this drops errors on the floor.
2268     consumeError(Maybe.takeError());
2269   }
2270 
2271   assert(Record[0] == ID && "Bogus stored ID or offset");
2272   InputFileInfo R;
2273   R.StoredSize = static_cast<off_t>(Record[1]);
2274   R.StoredTime = static_cast<time_t>(Record[2]);
2275   R.Overridden = static_cast<bool>(Record[3]);
2276   R.Transient = static_cast<bool>(Record[4]);
2277   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2278   R.Filename = std::string(Blob);
2279   ResolveImportedPath(F, R.Filename);
2280 
2281   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2282   if (!MaybeEntry) // FIXME this drops errors on the floor.
2283     consumeError(MaybeEntry.takeError());
2284   llvm::BitstreamEntry Entry = MaybeEntry.get();
2285   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2286          "expected record type for input file hash");
2287 
2288   Record.clear();
2289   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2290     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2291            "invalid record type for input file hash");
2292   else {
2293     // FIXME this drops errors on the floor.
2294     consumeError(Maybe.takeError());
2295   }
2296   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2297                   static_cast<uint64_t>(Record[0]);
2298   return R;
2299 }
2300 
2301 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2302 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2303   // If this ID is bogus, just return an empty input file.
2304   if (ID == 0 || ID > F.InputFilesLoaded.size())
2305     return InputFile();
2306 
2307   // If we've already loaded this input file, return it.
2308   if (F.InputFilesLoaded[ID-1].getFile())
2309     return F.InputFilesLoaded[ID-1];
2310 
2311   if (F.InputFilesLoaded[ID-1].isNotFound())
2312     return InputFile();
2313 
2314   // Go find this input file.
2315   BitstreamCursor &Cursor = F.InputFilesCursor;
2316   SavedStreamPosition SavedPosition(Cursor);
2317   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2318     // FIXME this drops errors on the floor.
2319     consumeError(std::move(Err));
2320   }
2321 
2322   InputFileInfo FI = readInputFileInfo(F, ID);
2323   off_t StoredSize = FI.StoredSize;
2324   time_t StoredTime = FI.StoredTime;
2325   bool Overridden = FI.Overridden;
2326   bool Transient = FI.Transient;
2327   StringRef Filename = FI.Filename;
2328   uint64_t StoredContentHash = FI.ContentHash;
2329 
2330   OptionalFileEntryRefDegradesToFileEntryPtr File =
2331       expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2332 
2333   // If we didn't find the file, resolve it relative to the
2334   // original directory from which this AST file was created.
2335   if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2336       F.OriginalDir != F.BaseDirectory) {
2337     std::string Resolved = resolveFileRelativeToOriginalDir(
2338         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2339     if (!Resolved.empty())
2340       File = expectedToOptional(FileMgr.getFileRef(Resolved));
2341   }
2342 
2343   // For an overridden file, create a virtual file with the stored
2344   // size/timestamp.
2345   if ((Overridden || Transient) && !File)
2346     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2347 
2348   if (!File) {
2349     if (Complain) {
2350       std::string ErrorStr = "could not find file '";
2351       ErrorStr += Filename;
2352       ErrorStr += "' referenced by AST file '";
2353       ErrorStr += F.FileName;
2354       ErrorStr += "'";
2355       Error(ErrorStr);
2356     }
2357     // Record that we didn't find the file.
2358     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2359     return InputFile();
2360   }
2361 
2362   // Check if there was a request to override the contents of the file
2363   // that was part of the precompiled header. Overriding such a file
2364   // can lead to problems when lexing using the source locations from the
2365   // PCH.
2366   SourceManager &SM = getSourceManager();
2367   // FIXME: Reject if the overrides are different.
2368   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2369     if (Complain)
2370       Error(diag::err_fe_pch_file_overridden, Filename);
2371 
2372     // After emitting the diagnostic, bypass the overriding file to recover
2373     // (this creates a separate FileEntry).
2374     File = SM.bypassFileContentsOverride(*File);
2375     if (!File) {
2376       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2377       return InputFile();
2378     }
2379   }
2380 
2381   enum ModificationType {
2382     Size,
2383     ModTime,
2384     Content,
2385     None,
2386   };
2387   auto HasInputFileChanged = [&]() {
2388     if (StoredSize != File->getSize())
2389       return ModificationType::Size;
2390     if (!shouldDisableValidationForFile(F) && StoredTime &&
2391         StoredTime != File->getModificationTime()) {
2392       // In case the modification time changes but not the content,
2393       // accept the cached file as legit.
2394       if (ValidateASTInputFilesContent &&
2395           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2396         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2397         if (!MemBuffOrError) {
2398           if (!Complain)
2399             return ModificationType::ModTime;
2400           std::string ErrorStr = "could not get buffer for file '";
2401           ErrorStr += File->getName();
2402           ErrorStr += "'";
2403           Error(ErrorStr);
2404           return ModificationType::ModTime;
2405         }
2406 
2407         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2408         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2409           return ModificationType::None;
2410         return ModificationType::Content;
2411       }
2412       return ModificationType::ModTime;
2413     }
2414     return ModificationType::None;
2415   };
2416 
2417   bool IsOutOfDate = false;
2418   auto FileChange = HasInputFileChanged();
2419   // For an overridden file, there is nothing to validate.
2420   if (!Overridden && FileChange != ModificationType::None) {
2421     if (Complain && !Diags.isDiagnosticInFlight()) {
2422       // Build a list of the PCH imports that got us here (in reverse).
2423       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2424       while (!ImportStack.back()->ImportedBy.empty())
2425         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2426 
2427       // The top-level PCH is stale.
2428       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2429       Diag(diag::err_fe_ast_file_modified)
2430           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2431           << TopLevelPCHName << FileChange;
2432 
2433       // Print the import stack.
2434       if (ImportStack.size() > 1) {
2435         Diag(diag::note_pch_required_by)
2436           << Filename << ImportStack[0]->FileName;
2437         for (unsigned I = 1; I < ImportStack.size(); ++I)
2438           Diag(diag::note_pch_required_by)
2439             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2440       }
2441 
2442       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2443     }
2444 
2445     IsOutOfDate = true;
2446   }
2447   // FIXME: If the file is overridden and we've already opened it,
2448   // issue an error (or split it into a separate FileEntry).
2449 
2450   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2451 
2452   // Note that we've loaded this input file.
2453   F.InputFilesLoaded[ID-1] = IF;
2454   return IF;
2455 }
2456 
2457 /// If we are loading a relocatable PCH or module file, and the filename
2458 /// is not an absolute path, add the system or module root to the beginning of
2459 /// the file name.
2460 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2461   // Resolve relative to the base directory, if we have one.
2462   if (!M.BaseDirectory.empty())
2463     return ResolveImportedPath(Filename, M.BaseDirectory);
2464 }
2465 
2466 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2467   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2468     return;
2469 
2470   SmallString<128> Buffer;
2471   llvm::sys::path::append(Buffer, Prefix, Filename);
2472   Filename.assign(Buffer.begin(), Buffer.end());
2473 }
2474 
2475 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2476   switch (ARR) {
2477   case ASTReader::Failure: return true;
2478   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2479   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2480   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2481   case ASTReader::ConfigurationMismatch:
2482     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2483   case ASTReader::HadErrors: return true;
2484   case ASTReader::Success: return false;
2485   }
2486 
2487   llvm_unreachable("unknown ASTReadResult");
2488 }
2489 
2490 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2491     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2492     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2493     std::string &SuggestedPredefines) {
2494   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2495     // FIXME this drops errors on the floor.
2496     consumeError(std::move(Err));
2497     return Failure;
2498   }
2499 
2500   // Read all of the records in the options block.
2501   RecordData Record;
2502   ASTReadResult Result = Success;
2503   while (true) {
2504     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2505     if (!MaybeEntry) {
2506       // FIXME this drops errors on the floor.
2507       consumeError(MaybeEntry.takeError());
2508       return Failure;
2509     }
2510     llvm::BitstreamEntry Entry = MaybeEntry.get();
2511 
2512     switch (Entry.Kind) {
2513     case llvm::BitstreamEntry::Error:
2514     case llvm::BitstreamEntry::SubBlock:
2515       return Failure;
2516 
2517     case llvm::BitstreamEntry::EndBlock:
2518       return Result;
2519 
2520     case llvm::BitstreamEntry::Record:
2521       // The interesting case.
2522       break;
2523     }
2524 
2525     // Read and process a record.
2526     Record.clear();
2527     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2528     if (!MaybeRecordType) {
2529       // FIXME this drops errors on the floor.
2530       consumeError(MaybeRecordType.takeError());
2531       return Failure;
2532     }
2533     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2534     case LANGUAGE_OPTIONS: {
2535       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2536       if (ParseLanguageOptions(Record, Complain, Listener,
2537                                AllowCompatibleConfigurationMismatch))
2538         Result = ConfigurationMismatch;
2539       break;
2540     }
2541 
2542     case TARGET_OPTIONS: {
2543       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2544       if (ParseTargetOptions(Record, Complain, Listener,
2545                              AllowCompatibleConfigurationMismatch))
2546         Result = ConfigurationMismatch;
2547       break;
2548     }
2549 
2550     case FILE_SYSTEM_OPTIONS: {
2551       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2552       if (!AllowCompatibleConfigurationMismatch &&
2553           ParseFileSystemOptions(Record, Complain, Listener))
2554         Result = ConfigurationMismatch;
2555       break;
2556     }
2557 
2558     case HEADER_SEARCH_OPTIONS: {
2559       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2560       if (!AllowCompatibleConfigurationMismatch &&
2561           ParseHeaderSearchOptions(Record, Complain, Listener))
2562         Result = ConfigurationMismatch;
2563       break;
2564     }
2565 
2566     case PREPROCESSOR_OPTIONS:
2567       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2568       if (!AllowCompatibleConfigurationMismatch &&
2569           ParsePreprocessorOptions(Record, Complain, Listener,
2570                                    SuggestedPredefines))
2571         Result = ConfigurationMismatch;
2572       break;
2573     }
2574   }
2575 }
2576 
2577 ASTReader::ASTReadResult
2578 ASTReader::ReadControlBlock(ModuleFile &F,
2579                             SmallVectorImpl<ImportedModule> &Loaded,
2580                             const ModuleFile *ImportedBy,
2581                             unsigned ClientLoadCapabilities) {
2582   BitstreamCursor &Stream = F.Stream;
2583 
2584   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2585     Error(std::move(Err));
2586     return Failure;
2587   }
2588 
2589   // Lambda to read the unhashed control block the first time it's called.
2590   //
2591   // For PCM files, the unhashed control block cannot be read until after the
2592   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2593   // need to look ahead before reading the IMPORTS record.  For consistency,
2594   // this block is always read somehow (see BitstreamEntry::EndBlock).
2595   bool HasReadUnhashedControlBlock = false;
2596   auto readUnhashedControlBlockOnce = [&]() {
2597     if (!HasReadUnhashedControlBlock) {
2598       HasReadUnhashedControlBlock = true;
2599       if (ASTReadResult Result =
2600               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2601         return Result;
2602     }
2603     return Success;
2604   };
2605 
2606   bool DisableValidation = shouldDisableValidationForFile(F);
2607 
2608   // Read all of the records and blocks in the control block.
2609   RecordData Record;
2610   unsigned NumInputs = 0;
2611   unsigned NumUserInputs = 0;
2612   StringRef BaseDirectoryAsWritten;
2613   while (true) {
2614     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2615     if (!MaybeEntry) {
2616       Error(MaybeEntry.takeError());
2617       return Failure;
2618     }
2619     llvm::BitstreamEntry Entry = MaybeEntry.get();
2620 
2621     switch (Entry.Kind) {
2622     case llvm::BitstreamEntry::Error:
2623       Error("malformed block record in AST file");
2624       return Failure;
2625     case llvm::BitstreamEntry::EndBlock: {
2626       // Validate the module before returning.  This call catches an AST with
2627       // no module name and no imports.
2628       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2629         return Result;
2630 
2631       // Validate input files.
2632       const HeaderSearchOptions &HSOpts =
2633           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2634 
2635       // All user input files reside at the index range [0, NumUserInputs), and
2636       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2637       // loaded module files, ignore missing inputs.
2638       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2639           F.Kind != MK_PrebuiltModule) {
2640         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2641 
2642         // If we are reading a module, we will create a verification timestamp,
2643         // so we verify all input files.  Otherwise, verify only user input
2644         // files.
2645 
2646         unsigned N = NumUserInputs;
2647         if (ValidateSystemInputs ||
2648             (HSOpts.ModulesValidateOncePerBuildSession &&
2649              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2650              F.Kind == MK_ImplicitModule))
2651           N = NumInputs;
2652 
2653         for (unsigned I = 0; I < N; ++I) {
2654           InputFile IF = getInputFile(F, I+1, Complain);
2655           if (!IF.getFile() || IF.isOutOfDate())
2656             return OutOfDate;
2657         }
2658       }
2659 
2660       if (Listener)
2661         Listener->visitModuleFile(F.FileName, F.Kind);
2662 
2663       if (Listener && Listener->needsInputFileVisitation()) {
2664         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2665                                                                 : NumUserInputs;
2666         for (unsigned I = 0; I < N; ++I) {
2667           bool IsSystem = I >= NumUserInputs;
2668           InputFileInfo FI = readInputFileInfo(F, I+1);
2669           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2670                                    F.Kind == MK_ExplicitModule ||
2671                                    F.Kind == MK_PrebuiltModule);
2672         }
2673       }
2674 
2675       return Success;
2676     }
2677 
2678     case llvm::BitstreamEntry::SubBlock:
2679       switch (Entry.ID) {
2680       case INPUT_FILES_BLOCK_ID:
2681         F.InputFilesCursor = Stream;
2682         if (llvm::Error Err = Stream.SkipBlock()) {
2683           Error(std::move(Err));
2684           return Failure;
2685         }
2686         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2687           Error("malformed block record in AST file");
2688           return Failure;
2689         }
2690         continue;
2691 
2692       case OPTIONS_BLOCK_ID:
2693         // If we're reading the first module for this group, check its options
2694         // are compatible with ours. For modules it imports, no further checking
2695         // is required, because we checked them when we built it.
2696         if (Listener && !ImportedBy) {
2697           // Should we allow the configuration of the module file to differ from
2698           // the configuration of the current translation unit in a compatible
2699           // way?
2700           //
2701           // FIXME: Allow this for files explicitly specified with -include-pch.
2702           bool AllowCompatibleConfigurationMismatch =
2703               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2704 
2705           ASTReadResult Result =
2706               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2707                                AllowCompatibleConfigurationMismatch, *Listener,
2708                                SuggestedPredefines);
2709           if (Result == Failure) {
2710             Error("malformed block record in AST file");
2711             return Result;
2712           }
2713 
2714           if (DisableValidation ||
2715               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2716             Result = Success;
2717 
2718           // If we can't load the module, exit early since we likely
2719           // will rebuild the module anyway. The stream may be in the
2720           // middle of a block.
2721           if (Result != Success)
2722             return Result;
2723         } else if (llvm::Error Err = Stream.SkipBlock()) {
2724           Error(std::move(Err));
2725           return Failure;
2726         }
2727         continue;
2728 
2729       default:
2730         if (llvm::Error Err = Stream.SkipBlock()) {
2731           Error(std::move(Err));
2732           return Failure;
2733         }
2734         continue;
2735       }
2736 
2737     case llvm::BitstreamEntry::Record:
2738       // The interesting case.
2739       break;
2740     }
2741 
2742     // Read and process a record.
2743     Record.clear();
2744     StringRef Blob;
2745     Expected<unsigned> MaybeRecordType =
2746         Stream.readRecord(Entry.ID, Record, &Blob);
2747     if (!MaybeRecordType) {
2748       Error(MaybeRecordType.takeError());
2749       return Failure;
2750     }
2751     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2752     case METADATA: {
2753       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2754         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2755           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2756                                         : diag::err_pch_version_too_new);
2757         return VersionMismatch;
2758       }
2759 
2760       bool hasErrors = Record[6];
2761       if (hasErrors && !DisableValidation) {
2762         // If requested by the caller and the module hasn't already been read
2763         // or compiled, mark modules on error as out-of-date.
2764         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2765             !ModuleMgr.getModuleCache().isPCMFinal(F.FileName))
2766           return OutOfDate;
2767 
2768         if (!AllowASTWithCompilerErrors) {
2769           Diag(diag::err_pch_with_compiler_errors);
2770           return HadErrors;
2771         }
2772       }
2773       if (hasErrors) {
2774         Diags.ErrorOccurred = true;
2775         Diags.UncompilableErrorOccurred = true;
2776         Diags.UnrecoverableErrorOccurred = true;
2777       }
2778 
2779       F.RelocatablePCH = Record[4];
2780       // Relative paths in a relocatable PCH are relative to our sysroot.
2781       if (F.RelocatablePCH)
2782         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2783 
2784       F.HasTimestamps = Record[5];
2785 
2786       const std::string &CurBranch = getClangFullRepositoryVersion();
2787       StringRef ASTBranch = Blob;
2788       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2789         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2790           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2791         return VersionMismatch;
2792       }
2793       break;
2794     }
2795 
2796     case IMPORTS: {
2797       // Validate the AST before processing any imports (otherwise, untangling
2798       // them can be error-prone and expensive).  A module will have a name and
2799       // will already have been validated, but this catches the PCH case.
2800       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2801         return Result;
2802 
2803       // Load each of the imported PCH files.
2804       unsigned Idx = 0, N = Record.size();
2805       while (Idx < N) {
2806         // Read information about the AST file.
2807         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2808         // The import location will be the local one for now; we will adjust
2809         // all import locations of module imports after the global source
2810         // location info are setup, in ReadAST.
2811         SourceLocation ImportLoc =
2812             ReadUntranslatedSourceLocation(Record[Idx++]);
2813         off_t StoredSize = (off_t)Record[Idx++];
2814         time_t StoredModTime = (time_t)Record[Idx++];
2815         auto FirstSignatureByte = Record.begin() + Idx;
2816         ASTFileSignature StoredSignature = ASTFileSignature::create(
2817             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2818         Idx += ASTFileSignature::size;
2819 
2820         std::string ImportedName = ReadString(Record, Idx);
2821         std::string ImportedFile;
2822 
2823         // For prebuilt and explicit modules first consult the file map for
2824         // an override. Note that here we don't search prebuilt module
2825         // directories, only the explicit name to file mappings. Also, we will
2826         // still verify the size/signature making sure it is essentially the
2827         // same file but perhaps in a different location.
2828         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2829           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2830             ImportedName, /*FileMapOnly*/ true);
2831 
2832         if (ImportedFile.empty())
2833           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2834           // ModuleCache as when writing.
2835           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2836         else
2837           SkipPath(Record, Idx);
2838 
2839         // If our client can't cope with us being out of date, we can't cope with
2840         // our dependency being missing.
2841         unsigned Capabilities = ClientLoadCapabilities;
2842         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2843           Capabilities &= ~ARR_Missing;
2844 
2845         // Load the AST file.
2846         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2847                                   Loaded, StoredSize, StoredModTime,
2848                                   StoredSignature, Capabilities);
2849 
2850         // If we diagnosed a problem, produce a backtrace.
2851         if (isDiagnosedResult(Result, Capabilities))
2852           Diag(diag::note_module_file_imported_by)
2853               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2854 
2855         switch (Result) {
2856         case Failure: return Failure;
2857           // If we have to ignore the dependency, we'll have to ignore this too.
2858         case Missing:
2859         case OutOfDate: return OutOfDate;
2860         case VersionMismatch: return VersionMismatch;
2861         case ConfigurationMismatch: return ConfigurationMismatch;
2862         case HadErrors: return HadErrors;
2863         case Success: break;
2864         }
2865       }
2866       break;
2867     }
2868 
2869     case ORIGINAL_FILE:
2870       F.OriginalSourceFileID = FileID::get(Record[0]);
2871       F.ActualOriginalSourceFileName = std::string(Blob);
2872       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2873       ResolveImportedPath(F, F.OriginalSourceFileName);
2874       break;
2875 
2876     case ORIGINAL_FILE_ID:
2877       F.OriginalSourceFileID = FileID::get(Record[0]);
2878       break;
2879 
2880     case ORIGINAL_PCH_DIR:
2881       F.OriginalDir = std::string(Blob);
2882       break;
2883 
2884     case MODULE_NAME:
2885       F.ModuleName = std::string(Blob);
2886       Diag(diag::remark_module_import)
2887           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2888           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2889       if (Listener)
2890         Listener->ReadModuleName(F.ModuleName);
2891 
2892       // Validate the AST as soon as we have a name so we can exit early on
2893       // failure.
2894       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2895         return Result;
2896 
2897       break;
2898 
2899     case MODULE_DIRECTORY: {
2900       // Save the BaseDirectory as written in the PCM for computing the module
2901       // filename for the ModuleCache.
2902       BaseDirectoryAsWritten = Blob;
2903       assert(!F.ModuleName.empty() &&
2904              "MODULE_DIRECTORY found before MODULE_NAME");
2905       // If we've already loaded a module map file covering this module, we may
2906       // have a better path for it (relative to the current build).
2907       Module *M = PP.getHeaderSearchInfo().lookupModule(
2908           F.ModuleName, /*AllowSearch*/ true,
2909           /*AllowExtraModuleMapSearch*/ true);
2910       if (M && M->Directory) {
2911         // If we're implicitly loading a module, the base directory can't
2912         // change between the build and use.
2913         // Don't emit module relocation error if we have -fno-validate-pch
2914         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2915                   DisableValidationForModuleKind::Module) &&
2916             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2917           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2918           if (!BuildDir || *BuildDir != M->Directory) {
2919             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2920               Diag(diag::err_imported_module_relocated)
2921                   << F.ModuleName << Blob << M->Directory->getName();
2922             return OutOfDate;
2923           }
2924         }
2925         F.BaseDirectory = std::string(M->Directory->getName());
2926       } else {
2927         F.BaseDirectory = std::string(Blob);
2928       }
2929       break;
2930     }
2931 
2932     case MODULE_MAP_FILE:
2933       if (ASTReadResult Result =
2934               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2935         return Result;
2936       break;
2937 
2938     case INPUT_FILE_OFFSETS:
2939       NumInputs = Record[0];
2940       NumUserInputs = Record[1];
2941       F.InputFileOffsets =
2942           (const llvm::support::unaligned_uint64_t *)Blob.data();
2943       F.InputFilesLoaded.resize(NumInputs);
2944       F.NumUserInputFiles = NumUserInputs;
2945       break;
2946     }
2947   }
2948 }
2949 
2950 ASTReader::ASTReadResult
2951 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2952   BitstreamCursor &Stream = F.Stream;
2953 
2954   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2955     Error(std::move(Err));
2956     return Failure;
2957   }
2958   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2959 
2960   // Read all of the records and blocks for the AST file.
2961   RecordData Record;
2962   while (true) {
2963     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2964     if (!MaybeEntry) {
2965       Error(MaybeEntry.takeError());
2966       return Failure;
2967     }
2968     llvm::BitstreamEntry Entry = MaybeEntry.get();
2969 
2970     switch (Entry.Kind) {
2971     case llvm::BitstreamEntry::Error:
2972       Error("error at end of module block in AST file");
2973       return Failure;
2974     case llvm::BitstreamEntry::EndBlock:
2975       // Outside of C++, we do not store a lookup map for the translation unit.
2976       // Instead, mark it as needing a lookup map to be built if this module
2977       // contains any declarations lexically within it (which it always does!).
2978       // This usually has no cost, since we very rarely need the lookup map for
2979       // the translation unit outside C++.
2980       if (ASTContext *Ctx = ContextObj) {
2981         DeclContext *DC = Ctx->getTranslationUnitDecl();
2982         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2983           DC->setMustBuildLookupTable();
2984       }
2985 
2986       return Success;
2987     case llvm::BitstreamEntry::SubBlock:
2988       switch (Entry.ID) {
2989       case DECLTYPES_BLOCK_ID:
2990         // We lazily load the decls block, but we want to set up the
2991         // DeclsCursor cursor to point into it.  Clone our current bitcode
2992         // cursor to it, enter the block and read the abbrevs in that block.
2993         // With the main cursor, we just skip over it.
2994         F.DeclsCursor = Stream;
2995         if (llvm::Error Err = Stream.SkipBlock()) {
2996           Error(std::move(Err));
2997           return Failure;
2998         }
2999         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
3000                              &F.DeclsBlockStartOffset)) {
3001           Error("malformed block record in AST file");
3002           return Failure;
3003         }
3004         break;
3005 
3006       case PREPROCESSOR_BLOCK_ID:
3007         F.MacroCursor = Stream;
3008         if (!PP.getExternalSource())
3009           PP.setExternalSource(this);
3010 
3011         if (llvm::Error Err = Stream.SkipBlock()) {
3012           Error(std::move(Err));
3013           return Failure;
3014         }
3015         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
3016           Error("malformed block record in AST file");
3017           return Failure;
3018         }
3019         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3020         break;
3021 
3022       case PREPROCESSOR_DETAIL_BLOCK_ID:
3023         F.PreprocessorDetailCursor = Stream;
3024 
3025         if (llvm::Error Err = Stream.SkipBlock()) {
3026           Error(std::move(Err));
3027           return Failure;
3028         }
3029         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3030                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3031           Error("malformed preprocessor detail record in AST file");
3032           return Failure;
3033         }
3034         F.PreprocessorDetailStartOffset
3035         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3036 
3037         if (!PP.getPreprocessingRecord())
3038           PP.createPreprocessingRecord();
3039         if (!PP.getPreprocessingRecord()->getExternalSource())
3040           PP.getPreprocessingRecord()->SetExternalSource(*this);
3041         break;
3042 
3043       case SOURCE_MANAGER_BLOCK_ID:
3044         if (ReadSourceManagerBlock(F))
3045           return Failure;
3046         break;
3047 
3048       case SUBMODULE_BLOCK_ID:
3049         if (ASTReadResult Result =
3050                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3051           return Result;
3052         break;
3053 
3054       case COMMENTS_BLOCK_ID: {
3055         BitstreamCursor C = Stream;
3056 
3057         if (llvm::Error Err = Stream.SkipBlock()) {
3058           Error(std::move(Err));
3059           return Failure;
3060         }
3061         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3062           Error("malformed comments block in AST file");
3063           return Failure;
3064         }
3065         CommentsCursors.push_back(std::make_pair(C, &F));
3066         break;
3067       }
3068 
3069       default:
3070         if (llvm::Error Err = Stream.SkipBlock()) {
3071           Error(std::move(Err));
3072           return Failure;
3073         }
3074         break;
3075       }
3076       continue;
3077 
3078     case llvm::BitstreamEntry::Record:
3079       // The interesting case.
3080       break;
3081     }
3082 
3083     // Read and process a record.
3084     Record.clear();
3085     StringRef Blob;
3086     Expected<unsigned> MaybeRecordType =
3087         Stream.readRecord(Entry.ID, Record, &Blob);
3088     if (!MaybeRecordType) {
3089       Error(MaybeRecordType.takeError());
3090       return Failure;
3091     }
3092     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3093 
3094     // If we're not loading an AST context, we don't care about most records.
3095     if (!ContextObj) {
3096       switch (RecordType) {
3097       case IDENTIFIER_TABLE:
3098       case IDENTIFIER_OFFSET:
3099       case INTERESTING_IDENTIFIERS:
3100       case STATISTICS:
3101       case PP_CONDITIONAL_STACK:
3102       case PP_COUNTER_VALUE:
3103       case SOURCE_LOCATION_OFFSETS:
3104       case MODULE_OFFSET_MAP:
3105       case SOURCE_MANAGER_LINE_TABLE:
3106       case SOURCE_LOCATION_PRELOADS:
3107       case PPD_ENTITIES_OFFSETS:
3108       case HEADER_SEARCH_TABLE:
3109       case IMPORTED_MODULES:
3110       case MACRO_OFFSET:
3111         break;
3112       default:
3113         continue;
3114       }
3115     }
3116 
3117     switch (RecordType) {
3118     default:  // Default behavior: ignore.
3119       break;
3120 
3121     case TYPE_OFFSET: {
3122       if (F.LocalNumTypes != 0) {
3123         Error("duplicate TYPE_OFFSET record in AST file");
3124         return Failure;
3125       }
3126       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3127       F.LocalNumTypes = Record[0];
3128       unsigned LocalBaseTypeIndex = Record[1];
3129       F.BaseTypeIndex = getTotalNumTypes();
3130 
3131       if (F.LocalNumTypes > 0) {
3132         // Introduce the global -> local mapping for types within this module.
3133         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3134 
3135         // Introduce the local -> global mapping for types within this module.
3136         F.TypeRemap.insertOrReplace(
3137           std::make_pair(LocalBaseTypeIndex,
3138                          F.BaseTypeIndex - LocalBaseTypeIndex));
3139 
3140         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3141       }
3142       break;
3143     }
3144 
3145     case DECL_OFFSET: {
3146       if (F.LocalNumDecls != 0) {
3147         Error("duplicate DECL_OFFSET record in AST file");
3148         return Failure;
3149       }
3150       F.DeclOffsets = (const DeclOffset *)Blob.data();
3151       F.LocalNumDecls = Record[0];
3152       unsigned LocalBaseDeclID = Record[1];
3153       F.BaseDeclID = getTotalNumDecls();
3154 
3155       if (F.LocalNumDecls > 0) {
3156         // Introduce the global -> local mapping for declarations within this
3157         // module.
3158         GlobalDeclMap.insert(
3159           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3160 
3161         // Introduce the local -> global mapping for declarations within this
3162         // module.
3163         F.DeclRemap.insertOrReplace(
3164           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3165 
3166         // Introduce the global -> local mapping for declarations within this
3167         // module.
3168         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3169 
3170         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3171       }
3172       break;
3173     }
3174 
3175     case TU_UPDATE_LEXICAL: {
3176       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3177       LexicalContents Contents(
3178           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3179               Blob.data()),
3180           static_cast<unsigned int>(Blob.size() / 4));
3181       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3182       TU->setHasExternalLexicalStorage(true);
3183       break;
3184     }
3185 
3186     case UPDATE_VISIBLE: {
3187       unsigned Idx = 0;
3188       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3189       auto *Data = (const unsigned char*)Blob.data();
3190       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3191       // If we've already loaded the decl, perform the updates when we finish
3192       // loading this block.
3193       if (Decl *D = GetExistingDecl(ID))
3194         PendingUpdateRecords.push_back(
3195             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3196       break;
3197     }
3198 
3199     case IDENTIFIER_TABLE:
3200       F.IdentifierTableData =
3201           reinterpret_cast<const unsigned char *>(Blob.data());
3202       if (Record[0]) {
3203         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3204             F.IdentifierTableData + Record[0],
3205             F.IdentifierTableData + sizeof(uint32_t),
3206             F.IdentifierTableData,
3207             ASTIdentifierLookupTrait(*this, F));
3208 
3209         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3210       }
3211       break;
3212 
3213     case IDENTIFIER_OFFSET: {
3214       if (F.LocalNumIdentifiers != 0) {
3215         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3216         return Failure;
3217       }
3218       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3219       F.LocalNumIdentifiers = Record[0];
3220       unsigned LocalBaseIdentifierID = Record[1];
3221       F.BaseIdentifierID = getTotalNumIdentifiers();
3222 
3223       if (F.LocalNumIdentifiers > 0) {
3224         // Introduce the global -> local mapping for identifiers within this
3225         // module.
3226         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3227                                                   &F));
3228 
3229         // Introduce the local -> global mapping for identifiers within this
3230         // module.
3231         F.IdentifierRemap.insertOrReplace(
3232           std::make_pair(LocalBaseIdentifierID,
3233                          F.BaseIdentifierID - LocalBaseIdentifierID));
3234 
3235         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3236                                  + F.LocalNumIdentifiers);
3237       }
3238       break;
3239     }
3240 
3241     case INTERESTING_IDENTIFIERS:
3242       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3243       break;
3244 
3245     case EAGERLY_DESERIALIZED_DECLS:
3246       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3247       // about "interesting" decls (for instance, if we're building a module).
3248       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3249         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3250       break;
3251 
3252     case MODULAR_CODEGEN_DECLS:
3253       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3254       // them (ie: if we're not codegenerating this module).
3255       if (F.Kind == MK_MainFile ||
3256           getContext().getLangOpts().BuildingPCHWithObjectFile)
3257         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3258           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3259       break;
3260 
3261     case SPECIAL_TYPES:
3262       if (SpecialTypes.empty()) {
3263         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3264           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3265         break;
3266       }
3267 
3268       if (SpecialTypes.size() != Record.size()) {
3269         Error("invalid special-types record");
3270         return Failure;
3271       }
3272 
3273       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3274         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3275         if (!SpecialTypes[I])
3276           SpecialTypes[I] = ID;
3277         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3278         // merge step?
3279       }
3280       break;
3281 
3282     case STATISTICS:
3283       TotalNumStatements += Record[0];
3284       TotalNumMacros += Record[1];
3285       TotalLexicalDeclContexts += Record[2];
3286       TotalVisibleDeclContexts += Record[3];
3287       break;
3288 
3289     case UNUSED_FILESCOPED_DECLS:
3290       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3291         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3292       break;
3293 
3294     case DELEGATING_CTORS:
3295       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3296         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3297       break;
3298 
3299     case WEAK_UNDECLARED_IDENTIFIERS:
3300       if (Record.size() % 4 != 0) {
3301         Error("invalid weak identifiers record");
3302         return Failure;
3303       }
3304 
3305       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3306       // files. This isn't the way to do it :)
3307       WeakUndeclaredIdentifiers.clear();
3308 
3309       // Translate the weak, undeclared identifiers into global IDs.
3310       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3311         WeakUndeclaredIdentifiers.push_back(
3312           getGlobalIdentifierID(F, Record[I++]));
3313         WeakUndeclaredIdentifiers.push_back(
3314           getGlobalIdentifierID(F, Record[I++]));
3315         WeakUndeclaredIdentifiers.push_back(
3316           ReadSourceLocation(F, Record, I).getRawEncoding());
3317         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3318       }
3319       break;
3320 
3321     case SELECTOR_OFFSETS: {
3322       F.SelectorOffsets = (const uint32_t *)Blob.data();
3323       F.LocalNumSelectors = Record[0];
3324       unsigned LocalBaseSelectorID = Record[1];
3325       F.BaseSelectorID = getTotalNumSelectors();
3326 
3327       if (F.LocalNumSelectors > 0) {
3328         // Introduce the global -> local mapping for selectors within this
3329         // module.
3330         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3331 
3332         // Introduce the local -> global mapping for selectors within this
3333         // module.
3334         F.SelectorRemap.insertOrReplace(
3335           std::make_pair(LocalBaseSelectorID,
3336                          F.BaseSelectorID - LocalBaseSelectorID));
3337 
3338         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3339       }
3340       break;
3341     }
3342 
3343     case METHOD_POOL:
3344       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3345       if (Record[0])
3346         F.SelectorLookupTable
3347           = ASTSelectorLookupTable::Create(
3348                         F.SelectorLookupTableData + Record[0],
3349                         F.SelectorLookupTableData,
3350                         ASTSelectorLookupTrait(*this, F));
3351       TotalNumMethodPoolEntries += Record[1];
3352       break;
3353 
3354     case REFERENCED_SELECTOR_POOL:
3355       if (!Record.empty()) {
3356         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3357           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3358                                                                 Record[Idx++]));
3359           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3360                                               getRawEncoding());
3361         }
3362       }
3363       break;
3364 
3365     case PP_CONDITIONAL_STACK:
3366       if (!Record.empty()) {
3367         unsigned Idx = 0, End = Record.size() - 1;
3368         bool ReachedEOFWhileSkipping = Record[Idx++];
3369         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3370         if (ReachedEOFWhileSkipping) {
3371           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3372           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3373           bool FoundNonSkipPortion = Record[Idx++];
3374           bool FoundElse = Record[Idx++];
3375           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3376           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3377                            FoundElse, ElseLoc);
3378         }
3379         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3380         while (Idx < End) {
3381           auto Loc = ReadSourceLocation(F, Record, Idx);
3382           bool WasSkipping = Record[Idx++];
3383           bool FoundNonSkip = Record[Idx++];
3384           bool FoundElse = Record[Idx++];
3385           ConditionalStack.push_back(
3386               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3387         }
3388         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3389       }
3390       break;
3391 
3392     case PP_COUNTER_VALUE:
3393       if (!Record.empty() && Listener)
3394         Listener->ReadCounter(F, Record[0]);
3395       break;
3396 
3397     case FILE_SORTED_DECLS:
3398       F.FileSortedDecls = (const DeclID *)Blob.data();
3399       F.NumFileSortedDecls = Record[0];
3400       break;
3401 
3402     case SOURCE_LOCATION_OFFSETS: {
3403       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3404       F.LocalNumSLocEntries = Record[0];
3405       unsigned SLocSpaceSize = Record[1];
3406       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3407       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3408           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3409                                               SLocSpaceSize);
3410       if (!F.SLocEntryBaseID) {
3411         Error("ran out of source locations");
3412         break;
3413       }
3414       // Make our entry in the range map. BaseID is negative and growing, so
3415       // we invert it. Because we invert it, though, we need the other end of
3416       // the range.
3417       unsigned RangeStart =
3418           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3419       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3420       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3421 
3422       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3423       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3424       GlobalSLocOffsetMap.insert(
3425           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3426                            - SLocSpaceSize,&F));
3427 
3428       // Initialize the remapping table.
3429       // Invalid stays invalid.
3430       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3431       // This module. Base was 2 when being compiled.
3432       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3433                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3434 
3435       TotalNumSLocEntries += F.LocalNumSLocEntries;
3436       break;
3437     }
3438 
3439     case MODULE_OFFSET_MAP:
3440       F.ModuleOffsetMap = Blob;
3441       break;
3442 
3443     case SOURCE_MANAGER_LINE_TABLE:
3444       if (ParseLineTable(F, Record)) {
3445         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3446         return Failure;
3447       }
3448       break;
3449 
3450     case SOURCE_LOCATION_PRELOADS: {
3451       // Need to transform from the local view (1-based IDs) to the global view,
3452       // which is based off F.SLocEntryBaseID.
3453       if (!F.PreloadSLocEntries.empty()) {
3454         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3455         return Failure;
3456       }
3457 
3458       F.PreloadSLocEntries.swap(Record);
3459       break;
3460     }
3461 
3462     case EXT_VECTOR_DECLS:
3463       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3464         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3465       break;
3466 
3467     case VTABLE_USES:
3468       if (Record.size() % 3 != 0) {
3469         Error("Invalid VTABLE_USES record");
3470         return Failure;
3471       }
3472 
3473       // Later tables overwrite earlier ones.
3474       // FIXME: Modules will have some trouble with this. This is clearly not
3475       // the right way to do this.
3476       VTableUses.clear();
3477 
3478       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3479         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3480         VTableUses.push_back(
3481           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3482         VTableUses.push_back(Record[Idx++]);
3483       }
3484       break;
3485 
3486     case PENDING_IMPLICIT_INSTANTIATIONS:
3487       if (PendingInstantiations.size() % 2 != 0) {
3488         Error("Invalid existing PendingInstantiations");
3489         return Failure;
3490       }
3491 
3492       if (Record.size() % 2 != 0) {
3493         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3494         return Failure;
3495       }
3496 
3497       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3498         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3499         PendingInstantiations.push_back(
3500           ReadSourceLocation(F, Record, I).getRawEncoding());
3501       }
3502       break;
3503 
3504     case SEMA_DECL_REFS:
3505       if (Record.size() != 3) {
3506         Error("Invalid SEMA_DECL_REFS block");
3507         return Failure;
3508       }
3509       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3510         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3511       break;
3512 
3513     case PPD_ENTITIES_OFFSETS: {
3514       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3515       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3516       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3517 
3518       unsigned LocalBasePreprocessedEntityID = Record[0];
3519 
3520       unsigned StartingID;
3521       if (!PP.getPreprocessingRecord())
3522         PP.createPreprocessingRecord();
3523       if (!PP.getPreprocessingRecord()->getExternalSource())
3524         PP.getPreprocessingRecord()->SetExternalSource(*this);
3525       StartingID
3526         = PP.getPreprocessingRecord()
3527             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3528       F.BasePreprocessedEntityID = StartingID;
3529 
3530       if (F.NumPreprocessedEntities > 0) {
3531         // Introduce the global -> local mapping for preprocessed entities in
3532         // this module.
3533         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3534 
3535         // Introduce the local -> global mapping for preprocessed entities in
3536         // this module.
3537         F.PreprocessedEntityRemap.insertOrReplace(
3538           std::make_pair(LocalBasePreprocessedEntityID,
3539             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3540       }
3541 
3542       break;
3543     }
3544 
3545     case PPD_SKIPPED_RANGES: {
3546       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3547       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3548       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3549 
3550       if (!PP.getPreprocessingRecord())
3551         PP.createPreprocessingRecord();
3552       if (!PP.getPreprocessingRecord()->getExternalSource())
3553         PP.getPreprocessingRecord()->SetExternalSource(*this);
3554       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3555           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3556 
3557       if (F.NumPreprocessedSkippedRanges > 0)
3558         GlobalSkippedRangeMap.insert(
3559             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3560       break;
3561     }
3562 
3563     case DECL_UPDATE_OFFSETS:
3564       if (Record.size() % 2 != 0) {
3565         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3566         return Failure;
3567       }
3568       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3569         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3570         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3571 
3572         // If we've already loaded the decl, perform the updates when we finish
3573         // loading this block.
3574         if (Decl *D = GetExistingDecl(ID))
3575           PendingUpdateRecords.push_back(
3576               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3577       }
3578       break;
3579 
3580     case OBJC_CATEGORIES_MAP:
3581       if (F.LocalNumObjCCategoriesInMap != 0) {
3582         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3583         return Failure;
3584       }
3585 
3586       F.LocalNumObjCCategoriesInMap = Record[0];
3587       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3588       break;
3589 
3590     case OBJC_CATEGORIES:
3591       F.ObjCCategories.swap(Record);
3592       break;
3593 
3594     case CUDA_SPECIAL_DECL_REFS:
3595       // Later tables overwrite earlier ones.
3596       // FIXME: Modules will have trouble with this.
3597       CUDASpecialDeclRefs.clear();
3598       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3599         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3600       break;
3601 
3602     case HEADER_SEARCH_TABLE:
3603       F.HeaderFileInfoTableData = Blob.data();
3604       F.LocalNumHeaderFileInfos = Record[1];
3605       if (Record[0]) {
3606         F.HeaderFileInfoTable
3607           = HeaderFileInfoLookupTable::Create(
3608                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3609                    (const unsigned char *)F.HeaderFileInfoTableData,
3610                    HeaderFileInfoTrait(*this, F,
3611                                        &PP.getHeaderSearchInfo(),
3612                                        Blob.data() + Record[2]));
3613 
3614         PP.getHeaderSearchInfo().SetExternalSource(this);
3615         if (!PP.getHeaderSearchInfo().getExternalLookup())
3616           PP.getHeaderSearchInfo().SetExternalLookup(this);
3617       }
3618       break;
3619 
3620     case FP_PRAGMA_OPTIONS:
3621       // Later tables overwrite earlier ones.
3622       FPPragmaOptions.swap(Record);
3623       break;
3624 
3625     case OPENCL_EXTENSIONS:
3626       for (unsigned I = 0, E = Record.size(); I != E; ) {
3627         auto Name = ReadString(Record, I);
3628         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3629         OptInfo.Supported = Record[I++] != 0;
3630         OptInfo.Enabled = Record[I++] != 0;
3631         OptInfo.WithPragma = Record[I++] != 0;
3632         OptInfo.Avail = Record[I++];
3633         OptInfo.Core = Record[I++];
3634         OptInfo.Opt = Record[I++];
3635       }
3636       break;
3637 
3638     case TENTATIVE_DEFINITIONS:
3639       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3640         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3641       break;
3642 
3643     case KNOWN_NAMESPACES:
3644       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3645         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3646       break;
3647 
3648     case UNDEFINED_BUT_USED:
3649       if (UndefinedButUsed.size() % 2 != 0) {
3650         Error("Invalid existing UndefinedButUsed");
3651         return Failure;
3652       }
3653 
3654       if (Record.size() % 2 != 0) {
3655         Error("invalid undefined-but-used record");
3656         return Failure;
3657       }
3658       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3659         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3660         UndefinedButUsed.push_back(
3661             ReadSourceLocation(F, Record, I).getRawEncoding());
3662       }
3663       break;
3664 
3665     case DELETE_EXPRS_TO_ANALYZE:
3666       for (unsigned I = 0, N = Record.size(); I != N;) {
3667         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3668         const uint64_t Count = Record[I++];
3669         DelayedDeleteExprs.push_back(Count);
3670         for (uint64_t C = 0; C < Count; ++C) {
3671           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3672           bool IsArrayForm = Record[I++] == 1;
3673           DelayedDeleteExprs.push_back(IsArrayForm);
3674         }
3675       }
3676       break;
3677 
3678     case IMPORTED_MODULES:
3679       if (!F.isModule()) {
3680         // If we aren't loading a module (which has its own exports), make
3681         // all of the imported modules visible.
3682         // FIXME: Deal with macros-only imports.
3683         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3684           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3685           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3686           if (GlobalID) {
3687             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3688             if (DeserializationListener)
3689               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3690           }
3691         }
3692       }
3693       break;
3694 
3695     case MACRO_OFFSET: {
3696       if (F.LocalNumMacros != 0) {
3697         Error("duplicate MACRO_OFFSET record in AST file");
3698         return Failure;
3699       }
3700       F.MacroOffsets = (const uint32_t *)Blob.data();
3701       F.LocalNumMacros = Record[0];
3702       unsigned LocalBaseMacroID = Record[1];
3703       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3704       F.BaseMacroID = getTotalNumMacros();
3705 
3706       if (F.LocalNumMacros > 0) {
3707         // Introduce the global -> local mapping for macros within this module.
3708         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3709 
3710         // Introduce the local -> global mapping for macros within this module.
3711         F.MacroRemap.insertOrReplace(
3712           std::make_pair(LocalBaseMacroID,
3713                          F.BaseMacroID - LocalBaseMacroID));
3714 
3715         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3716       }
3717       break;
3718     }
3719 
3720     case LATE_PARSED_TEMPLATE:
3721       LateParsedTemplates.emplace_back(
3722           std::piecewise_construct, std::forward_as_tuple(&F),
3723           std::forward_as_tuple(Record.begin(), Record.end()));
3724       break;
3725 
3726     case OPTIMIZE_PRAGMA_OPTIONS:
3727       if (Record.size() != 1) {
3728         Error("invalid pragma optimize record");
3729         return Failure;
3730       }
3731       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3732       break;
3733 
3734     case MSSTRUCT_PRAGMA_OPTIONS:
3735       if (Record.size() != 1) {
3736         Error("invalid pragma ms_struct record");
3737         return Failure;
3738       }
3739       PragmaMSStructState = Record[0];
3740       break;
3741 
3742     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3743       if (Record.size() != 2) {
3744         Error("invalid pragma ms_struct record");
3745         return Failure;
3746       }
3747       PragmaMSPointersToMembersState = Record[0];
3748       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3749       break;
3750 
3751     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3752       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3753         UnusedLocalTypedefNameCandidates.push_back(
3754             getGlobalDeclID(F, Record[I]));
3755       break;
3756 
3757     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3758       if (Record.size() != 1) {
3759         Error("invalid cuda pragma options record");
3760         return Failure;
3761       }
3762       ForceCUDAHostDeviceDepth = Record[0];
3763       break;
3764 
3765     case ALIGN_PACK_PRAGMA_OPTIONS: {
3766       if (Record.size() < 3) {
3767         Error("invalid pragma pack record");
3768         return Failure;
3769       }
3770       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3771       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3772       unsigned NumStackEntries = Record[2];
3773       unsigned Idx = 3;
3774       // Reset the stack when importing a new module.
3775       PragmaAlignPackStack.clear();
3776       for (unsigned I = 0; I < NumStackEntries; ++I) {
3777         PragmaAlignPackStackEntry Entry;
3778         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3779         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3780         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3781         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3782         Entry.SlotLabel = PragmaAlignPackStrings.back();
3783         PragmaAlignPackStack.push_back(Entry);
3784       }
3785       break;
3786     }
3787 
3788     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3789       if (Record.size() < 3) {
3790         Error("invalid pragma pack record");
3791         return Failure;
3792       }
3793       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3794       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3795       unsigned NumStackEntries = Record[2];
3796       unsigned Idx = 3;
3797       // Reset the stack when importing a new module.
3798       FpPragmaStack.clear();
3799       for (unsigned I = 0; I < NumStackEntries; ++I) {
3800         FpPragmaStackEntry Entry;
3801         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3802         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3803         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3804         FpPragmaStrings.push_back(ReadString(Record, Idx));
3805         Entry.SlotLabel = FpPragmaStrings.back();
3806         FpPragmaStack.push_back(Entry);
3807       }
3808       break;
3809     }
3810 
3811     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3812       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3813         DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3814       break;
3815     }
3816   }
3817 }
3818 
3819 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3820   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3821 
3822   // Additional remapping information.
3823   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3824   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3825   F.ModuleOffsetMap = StringRef();
3826 
3827   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3828   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3829     F.SLocRemap.insert(std::make_pair(0U, 0));
3830     F.SLocRemap.insert(std::make_pair(2U, 1));
3831   }
3832 
3833   // Continuous range maps we may be updating in our module.
3834   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3835   RemapBuilder SLocRemap(F.SLocRemap);
3836   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3837   RemapBuilder MacroRemap(F.MacroRemap);
3838   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3839   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3840   RemapBuilder SelectorRemap(F.SelectorRemap);
3841   RemapBuilder DeclRemap(F.DeclRemap);
3842   RemapBuilder TypeRemap(F.TypeRemap);
3843 
3844   while (Data < DataEnd) {
3845     // FIXME: Looking up dependency modules by filename is horrible. Let's
3846     // start fixing this with prebuilt, explicit and implicit modules and see
3847     // how it goes...
3848     using namespace llvm::support;
3849     ModuleKind Kind = static_cast<ModuleKind>(
3850       endian::readNext<uint8_t, little, unaligned>(Data));
3851     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3852     StringRef Name = StringRef((const char*)Data, Len);
3853     Data += Len;
3854     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3855                               Kind == MK_ImplicitModule
3856                           ? ModuleMgr.lookupByModuleName(Name)
3857                           : ModuleMgr.lookupByFileName(Name));
3858     if (!OM) {
3859       std::string Msg =
3860           "SourceLocation remap refers to unknown module, cannot find ";
3861       Msg.append(std::string(Name));
3862       Error(Msg);
3863       return;
3864     }
3865 
3866     uint32_t SLocOffset =
3867         endian::readNext<uint32_t, little, unaligned>(Data);
3868     uint32_t IdentifierIDOffset =
3869         endian::readNext<uint32_t, little, unaligned>(Data);
3870     uint32_t MacroIDOffset =
3871         endian::readNext<uint32_t, little, unaligned>(Data);
3872     uint32_t PreprocessedEntityIDOffset =
3873         endian::readNext<uint32_t, little, unaligned>(Data);
3874     uint32_t SubmoduleIDOffset =
3875         endian::readNext<uint32_t, little, unaligned>(Data);
3876     uint32_t SelectorIDOffset =
3877         endian::readNext<uint32_t, little, unaligned>(Data);
3878     uint32_t DeclIDOffset =
3879         endian::readNext<uint32_t, little, unaligned>(Data);
3880     uint32_t TypeIndexOffset =
3881         endian::readNext<uint32_t, little, unaligned>(Data);
3882 
3883     uint32_t None = std::numeric_limits<uint32_t>::max();
3884 
3885     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3886                          RemapBuilder &Remap) {
3887       if (Offset != None)
3888         Remap.insert(std::make_pair(Offset,
3889                                     static_cast<int>(BaseOffset - Offset)));
3890     };
3891     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3892     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3893     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3894     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3895               PreprocessedEntityRemap);
3896     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3897     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3898     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3899     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3900 
3901     // Global -> local mappings.
3902     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3903   }
3904 }
3905 
3906 ASTReader::ASTReadResult
3907 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3908                                   const ModuleFile *ImportedBy,
3909                                   unsigned ClientLoadCapabilities) {
3910   unsigned Idx = 0;
3911   F.ModuleMapPath = ReadPath(F, Record, Idx);
3912 
3913   // Try to resolve ModuleName in the current header search context and
3914   // verify that it is found in the same module map file as we saved. If the
3915   // top-level AST file is a main file, skip this check because there is no
3916   // usable header search context.
3917   assert(!F.ModuleName.empty() &&
3918          "MODULE_NAME should come before MODULE_MAP_FILE");
3919   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3920     // An implicitly-loaded module file should have its module listed in some
3921     // module map file that we've already loaded.
3922     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3923     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3924     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3925     // Don't emit module relocation error if we have -fno-validate-pch
3926     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3927               DisableValidationForModuleKind::Module) &&
3928         !ModMap) {
3929       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3930         if (auto ASTFE = M ? M->getASTFile() : None) {
3931           // This module was defined by an imported (explicit) module.
3932           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3933                                                << ASTFE->getName();
3934         } else {
3935           // This module was built with a different module map.
3936           Diag(diag::err_imported_module_not_found)
3937               << F.ModuleName << F.FileName
3938               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3939               << !ImportedBy;
3940           // In case it was imported by a PCH, there's a chance the user is
3941           // just missing to include the search path to the directory containing
3942           // the modulemap.
3943           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3944             Diag(diag::note_imported_by_pch_module_not_found)
3945                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3946         }
3947       }
3948       return OutOfDate;
3949     }
3950 
3951     assert(M && M->Name == F.ModuleName && "found module with different name");
3952 
3953     // Check the primary module map file.
3954     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3955     if (!StoredModMap || *StoredModMap != ModMap) {
3956       assert(ModMap && "found module is missing module map file");
3957       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3958              "top-level import should be verified");
3959       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3960       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3961         Diag(diag::err_imported_module_modmap_changed)
3962             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3963             << ModMap->getName() << F.ModuleMapPath << NotImported;
3964       return OutOfDate;
3965     }
3966 
3967     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3968     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3969       // FIXME: we should use input files rather than storing names.
3970       std::string Filename = ReadPath(F, Record, Idx);
3971       auto F = FileMgr.getFile(Filename, false, false);
3972       if (!F) {
3973         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3974           Error("could not find file '" + Filename +"' referenced by AST file");
3975         return OutOfDate;
3976       }
3977       AdditionalStoredMaps.insert(*F);
3978     }
3979 
3980     // Check any additional module map files (e.g. module.private.modulemap)
3981     // that are not in the pcm.
3982     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3983       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3984         // Remove files that match
3985         // Note: SmallPtrSet::erase is really remove
3986         if (!AdditionalStoredMaps.erase(ModMap)) {
3987           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3988             Diag(diag::err_module_different_modmap)
3989               << F.ModuleName << /*new*/0 << ModMap->getName();
3990           return OutOfDate;
3991         }
3992       }
3993     }
3994 
3995     // Check any additional module map files that are in the pcm, but not
3996     // found in header search. Cases that match are already removed.
3997     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3998       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3999         Diag(diag::err_module_different_modmap)
4000           << F.ModuleName << /*not new*/1 << ModMap->getName();
4001       return OutOfDate;
4002     }
4003   }
4004 
4005   if (Listener)
4006     Listener->ReadModuleMapFile(F.ModuleMapPath);
4007   return Success;
4008 }
4009 
4010 /// Move the given method to the back of the global list of methods.
4011 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4012   // Find the entry for this selector in the method pool.
4013   Sema::GlobalMethodPool::iterator Known
4014     = S.MethodPool.find(Method->getSelector());
4015   if (Known == S.MethodPool.end())
4016     return;
4017 
4018   // Retrieve the appropriate method list.
4019   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4020                                                     : Known->second.second;
4021   bool Found = false;
4022   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4023     if (!Found) {
4024       if (List->getMethod() == Method) {
4025         Found = true;
4026       } else {
4027         // Keep searching.
4028         continue;
4029       }
4030     }
4031 
4032     if (List->getNext())
4033       List->setMethod(List->getNext()->getMethod());
4034     else
4035       List->setMethod(Method);
4036   }
4037 }
4038 
4039 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4040   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4041   for (Decl *D : Names) {
4042     bool wasHidden = !D->isUnconditionallyVisible();
4043     D->setVisibleDespiteOwningModule();
4044 
4045     if (wasHidden && SemaObj) {
4046       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4047         moveMethodToBackOfGlobalList(*SemaObj, Method);
4048       }
4049     }
4050   }
4051 }
4052 
4053 void ASTReader::makeModuleVisible(Module *Mod,
4054                                   Module::NameVisibilityKind NameVisibility,
4055                                   SourceLocation ImportLoc) {
4056   llvm::SmallPtrSet<Module *, 4> Visited;
4057   SmallVector<Module *, 4> Stack;
4058   Stack.push_back(Mod);
4059   while (!Stack.empty()) {
4060     Mod = Stack.pop_back_val();
4061 
4062     if (NameVisibility <= Mod->NameVisibility) {
4063       // This module already has this level of visibility (or greater), so
4064       // there is nothing more to do.
4065       continue;
4066     }
4067 
4068     if (Mod->isUnimportable()) {
4069       // Modules that aren't importable cannot be made visible.
4070       continue;
4071     }
4072 
4073     // Update the module's name visibility.
4074     Mod->NameVisibility = NameVisibility;
4075 
4076     // If we've already deserialized any names from this module,
4077     // mark them as visible.
4078     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4079     if (Hidden != HiddenNamesMap.end()) {
4080       auto HiddenNames = std::move(*Hidden);
4081       HiddenNamesMap.erase(Hidden);
4082       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4083       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4084              "making names visible added hidden names");
4085     }
4086 
4087     // Push any exported modules onto the stack to be marked as visible.
4088     SmallVector<Module *, 16> Exports;
4089     Mod->getExportedModules(Exports);
4090     for (SmallVectorImpl<Module *>::iterator
4091            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4092       Module *Exported = *I;
4093       if (Visited.insert(Exported).second)
4094         Stack.push_back(Exported);
4095     }
4096   }
4097 }
4098 
4099 /// We've merged the definition \p MergedDef into the existing definition
4100 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4101 /// visible.
4102 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4103                                           NamedDecl *MergedDef) {
4104   if (!Def->isUnconditionallyVisible()) {
4105     // If MergedDef is visible or becomes visible, make the definition visible.
4106     if (MergedDef->isUnconditionallyVisible())
4107       Def->setVisibleDespiteOwningModule();
4108     else {
4109       getContext().mergeDefinitionIntoModule(
4110           Def, MergedDef->getImportedOwningModule(),
4111           /*NotifyListeners*/ false);
4112       PendingMergedDefinitionsToDeduplicate.insert(Def);
4113     }
4114   }
4115 }
4116 
4117 bool ASTReader::loadGlobalIndex() {
4118   if (GlobalIndex)
4119     return false;
4120 
4121   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4122       !PP.getLangOpts().Modules)
4123     return true;
4124 
4125   // Try to load the global index.
4126   TriedLoadingGlobalIndex = true;
4127   StringRef ModuleCachePath
4128     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4129   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4130       GlobalModuleIndex::readIndex(ModuleCachePath);
4131   if (llvm::Error Err = std::move(Result.second)) {
4132     assert(!Result.first);
4133     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4134     return true;
4135   }
4136 
4137   GlobalIndex.reset(Result.first);
4138   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4139   return false;
4140 }
4141 
4142 bool ASTReader::isGlobalIndexUnavailable() const {
4143   return PP.getLangOpts().Modules && UseGlobalIndex &&
4144          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4145 }
4146 
4147 static void updateModuleTimestamp(ModuleFile &MF) {
4148   // Overwrite the timestamp file contents so that file's mtime changes.
4149   std::string TimestampFilename = MF.getTimestampFilename();
4150   std::error_code EC;
4151   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4152                           llvm::sys::fs::OF_TextWithCRLF);
4153   if (EC)
4154     return;
4155   OS << "Timestamp file\n";
4156   OS.close();
4157   OS.clear_error(); // Avoid triggering a fatal error.
4158 }
4159 
4160 /// Given a cursor at the start of an AST file, scan ahead and drop the
4161 /// cursor into the start of the given block ID, returning false on success and
4162 /// true on failure.
4163 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4164   while (true) {
4165     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4166     if (!MaybeEntry) {
4167       // FIXME this drops errors on the floor.
4168       consumeError(MaybeEntry.takeError());
4169       return true;
4170     }
4171     llvm::BitstreamEntry Entry = MaybeEntry.get();
4172 
4173     switch (Entry.Kind) {
4174     case llvm::BitstreamEntry::Error:
4175     case llvm::BitstreamEntry::EndBlock:
4176       return true;
4177 
4178     case llvm::BitstreamEntry::Record:
4179       // Ignore top-level records.
4180       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4181         break;
4182       else {
4183         // FIXME this drops errors on the floor.
4184         consumeError(Skipped.takeError());
4185         return true;
4186       }
4187 
4188     case llvm::BitstreamEntry::SubBlock:
4189       if (Entry.ID == BlockID) {
4190         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4191           // FIXME this drops the error on the floor.
4192           consumeError(std::move(Err));
4193           return true;
4194         }
4195         // Found it!
4196         return false;
4197       }
4198 
4199       if (llvm::Error Err = Cursor.SkipBlock()) {
4200         // FIXME this drops the error on the floor.
4201         consumeError(std::move(Err));
4202         return true;
4203       }
4204     }
4205   }
4206 }
4207 
4208 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4209                                             ModuleKind Type,
4210                                             SourceLocation ImportLoc,
4211                                             unsigned ClientLoadCapabilities,
4212                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4213   llvm::SaveAndRestore<SourceLocation>
4214     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4215   llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4216       CurrentDeserializingModuleKind, Type);
4217 
4218   // Defer any pending actions until we get to the end of reading the AST file.
4219   Deserializing AnASTFile(this);
4220 
4221   // Bump the generation number.
4222   unsigned PreviousGeneration = 0;
4223   if (ContextObj)
4224     PreviousGeneration = incrementGeneration(*ContextObj);
4225 
4226   unsigned NumModules = ModuleMgr.size();
4227   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4228     assert(ReadResult && "expected to return error");
4229     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4230                             PP.getLangOpts().Modules
4231                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4232                                 : nullptr);
4233 
4234     // If we find that any modules are unusable, the global index is going
4235     // to be out-of-date. Just remove it.
4236     GlobalIndex.reset();
4237     ModuleMgr.setGlobalIndex(nullptr);
4238     return ReadResult;
4239   };
4240 
4241   SmallVector<ImportedModule, 4> Loaded;
4242   switch (ASTReadResult ReadResult =
4243               ReadASTCore(FileName, Type, ImportLoc,
4244                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4245                           ASTFileSignature(), ClientLoadCapabilities)) {
4246   case Failure:
4247   case Missing:
4248   case OutOfDate:
4249   case VersionMismatch:
4250   case ConfigurationMismatch:
4251   case HadErrors:
4252     return removeModulesAndReturn(ReadResult);
4253   case Success:
4254     break;
4255   }
4256 
4257   // Here comes stuff that we only do once the entire chain is loaded.
4258 
4259   // Load the AST blocks of all of the modules that we loaded.  We can still
4260   // hit errors parsing the ASTs at this point.
4261   for (ImportedModule &M : Loaded) {
4262     ModuleFile &F = *M.Mod;
4263 
4264     // Read the AST block.
4265     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4266       return removeModulesAndReturn(Result);
4267 
4268     // The AST block should always have a definition for the main module.
4269     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4270       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4271       return removeModulesAndReturn(Failure);
4272     }
4273 
4274     // Read the extension blocks.
4275     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4276       if (ASTReadResult Result = ReadExtensionBlock(F))
4277         return removeModulesAndReturn(Result);
4278     }
4279 
4280     // Once read, set the ModuleFile bit base offset and update the size in
4281     // bits of all files we've seen.
4282     F.GlobalBitOffset = TotalModulesSizeInBits;
4283     TotalModulesSizeInBits += F.SizeInBits;
4284     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4285   }
4286 
4287   // Preload source locations and interesting indentifiers.
4288   for (ImportedModule &M : Loaded) {
4289     ModuleFile &F = *M.Mod;
4290 
4291     // Preload SLocEntries.
4292     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4293       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4294       // Load it through the SourceManager and don't call ReadSLocEntry()
4295       // directly because the entry may have already been loaded in which case
4296       // calling ReadSLocEntry() directly would trigger an assertion in
4297       // SourceManager.
4298       SourceMgr.getLoadedSLocEntryByID(Index);
4299     }
4300 
4301     // Map the original source file ID into the ID space of the current
4302     // compilation.
4303     if (F.OriginalSourceFileID.isValid()) {
4304       F.OriginalSourceFileID = FileID::get(
4305           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4306     }
4307 
4308     // Preload all the pending interesting identifiers by marking them out of
4309     // date.
4310     for (auto Offset : F.PreloadIdentifierOffsets) {
4311       const unsigned char *Data = F.IdentifierTableData + Offset;
4312 
4313       ASTIdentifierLookupTrait Trait(*this, F);
4314       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4315       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4316       auto &II = PP.getIdentifierTable().getOwn(Key);
4317       II.setOutOfDate(true);
4318 
4319       // Mark this identifier as being from an AST file so that we can track
4320       // whether we need to serialize it.
4321       markIdentifierFromAST(*this, II);
4322 
4323       // Associate the ID with the identifier so that the writer can reuse it.
4324       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4325       SetIdentifierInfo(ID, &II);
4326     }
4327   }
4328 
4329   // Setup the import locations and notify the module manager that we've
4330   // committed to these module files.
4331   for (ImportedModule &M : Loaded) {
4332     ModuleFile &F = *M.Mod;
4333 
4334     ModuleMgr.moduleFileAccepted(&F);
4335 
4336     // Set the import location.
4337     F.DirectImportLoc = ImportLoc;
4338     // FIXME: We assume that locations from PCH / preamble do not need
4339     // any translation.
4340     if (!M.ImportedBy)
4341       F.ImportLoc = M.ImportLoc;
4342     else
4343       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4344   }
4345 
4346   if (!PP.getLangOpts().CPlusPlus ||
4347       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4348        Type != MK_PrebuiltModule)) {
4349     // Mark all of the identifiers in the identifier table as being out of date,
4350     // so that various accessors know to check the loaded modules when the
4351     // identifier is used.
4352     //
4353     // For C++ modules, we don't need information on many identifiers (just
4354     // those that provide macros or are poisoned), so we mark all of
4355     // the interesting ones via PreloadIdentifierOffsets.
4356     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4357                                 IdEnd = PP.getIdentifierTable().end();
4358          Id != IdEnd; ++Id)
4359       Id->second->setOutOfDate(true);
4360   }
4361   // Mark selectors as out of date.
4362   for (auto Sel : SelectorGeneration)
4363     SelectorOutOfDate[Sel.first] = true;
4364 
4365   // Resolve any unresolved module exports.
4366   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4367     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4368     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4369     Module *ResolvedMod = getSubmodule(GlobalID);
4370 
4371     switch (Unresolved.Kind) {
4372     case UnresolvedModuleRef::Conflict:
4373       if (ResolvedMod) {
4374         Module::Conflict Conflict;
4375         Conflict.Other = ResolvedMod;
4376         Conflict.Message = Unresolved.String.str();
4377         Unresolved.Mod->Conflicts.push_back(Conflict);
4378       }
4379       continue;
4380 
4381     case UnresolvedModuleRef::Import:
4382       if (ResolvedMod)
4383         Unresolved.Mod->Imports.insert(ResolvedMod);
4384       continue;
4385 
4386     case UnresolvedModuleRef::Export:
4387       if (ResolvedMod || Unresolved.IsWildcard)
4388         Unresolved.Mod->Exports.push_back(
4389           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4390       continue;
4391     }
4392   }
4393   UnresolvedModuleRefs.clear();
4394 
4395   if (Imported)
4396     Imported->append(ImportedModules.begin(),
4397                      ImportedModules.end());
4398 
4399   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4400   // Might be unnecessary as use declarations are only used to build the
4401   // module itself.
4402 
4403   if (ContextObj)
4404     InitializeContext();
4405 
4406   if (SemaObj)
4407     UpdateSema();
4408 
4409   if (DeserializationListener)
4410     DeserializationListener->ReaderInitialized(this);
4411 
4412   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4413   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4414     // If this AST file is a precompiled preamble, then set the
4415     // preamble file ID of the source manager to the file source file
4416     // from which the preamble was built.
4417     if (Type == MK_Preamble) {
4418       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4419     } else if (Type == MK_MainFile) {
4420       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4421     }
4422   }
4423 
4424   // For any Objective-C class definitions we have already loaded, make sure
4425   // that we load any additional categories.
4426   if (ContextObj) {
4427     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4428       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4429                          ObjCClassesLoaded[I],
4430                          PreviousGeneration);
4431     }
4432   }
4433 
4434   if (PP.getHeaderSearchInfo()
4435           .getHeaderSearchOpts()
4436           .ModulesValidateOncePerBuildSession) {
4437     // Now we are certain that the module and all modules it depends on are
4438     // up to date.  Create or update timestamp files for modules that are
4439     // located in the module cache (not for PCH files that could be anywhere
4440     // in the filesystem).
4441     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4442       ImportedModule &M = Loaded[I];
4443       if (M.Mod->Kind == MK_ImplicitModule) {
4444         updateModuleTimestamp(*M.Mod);
4445       }
4446     }
4447   }
4448 
4449   return Success;
4450 }
4451 
4452 static ASTFileSignature readASTFileSignature(StringRef PCH);
4453 
4454 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4455 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4456   // FIXME checking magic headers is done in other places such as
4457   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4458   // always done the same. Unify it all with a helper.
4459   if (!Stream.canSkipToPos(4))
4460     return llvm::createStringError(std::errc::illegal_byte_sequence,
4461                                    "file too small to contain AST file magic");
4462   for (unsigned C : {'C', 'P', 'C', 'H'})
4463     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4464       if (Res.get() != C)
4465         return llvm::createStringError(
4466             std::errc::illegal_byte_sequence,
4467             "file doesn't start with AST file magic");
4468     } else
4469       return Res.takeError();
4470   return llvm::Error::success();
4471 }
4472 
4473 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4474   switch (Kind) {
4475   case MK_PCH:
4476     return 0; // PCH
4477   case MK_ImplicitModule:
4478   case MK_ExplicitModule:
4479   case MK_PrebuiltModule:
4480     return 1; // module
4481   case MK_MainFile:
4482   case MK_Preamble:
4483     return 2; // main source file
4484   }
4485   llvm_unreachable("unknown module kind");
4486 }
4487 
4488 ASTReader::ASTReadResult
4489 ASTReader::ReadASTCore(StringRef FileName,
4490                        ModuleKind Type,
4491                        SourceLocation ImportLoc,
4492                        ModuleFile *ImportedBy,
4493                        SmallVectorImpl<ImportedModule> &Loaded,
4494                        off_t ExpectedSize, time_t ExpectedModTime,
4495                        ASTFileSignature ExpectedSignature,
4496                        unsigned ClientLoadCapabilities) {
4497   ModuleFile *M;
4498   std::string ErrorStr;
4499   ModuleManager::AddModuleResult AddResult
4500     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4501                           getGeneration(), ExpectedSize, ExpectedModTime,
4502                           ExpectedSignature, readASTFileSignature,
4503                           M, ErrorStr);
4504 
4505   switch (AddResult) {
4506   case ModuleManager::AlreadyLoaded:
4507     Diag(diag::remark_module_import)
4508         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4509         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4510     return Success;
4511 
4512   case ModuleManager::NewlyLoaded:
4513     // Load module file below.
4514     break;
4515 
4516   case ModuleManager::Missing:
4517     // The module file was missing; if the client can handle that, return
4518     // it.
4519     if (ClientLoadCapabilities & ARR_Missing)
4520       return Missing;
4521 
4522     // Otherwise, return an error.
4523     Diag(diag::err_ast_file_not_found)
4524         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4525         << ErrorStr;
4526     return Failure;
4527 
4528   case ModuleManager::OutOfDate:
4529     // We couldn't load the module file because it is out-of-date. If the
4530     // client can handle out-of-date, return it.
4531     if (ClientLoadCapabilities & ARR_OutOfDate)
4532       return OutOfDate;
4533 
4534     // Otherwise, return an error.
4535     Diag(diag::err_ast_file_out_of_date)
4536         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4537         << ErrorStr;
4538     return Failure;
4539   }
4540 
4541   assert(M && "Missing module file");
4542 
4543   bool ShouldFinalizePCM = false;
4544   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4545     auto &MC = getModuleManager().getModuleCache();
4546     if (ShouldFinalizePCM)
4547       MC.finalizePCM(FileName);
4548     else
4549       MC.tryToDropPCM(FileName);
4550   });
4551   ModuleFile &F = *M;
4552   BitstreamCursor &Stream = F.Stream;
4553   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4554   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4555 
4556   // Sniff for the signature.
4557   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4558     Diag(diag::err_ast_file_invalid)
4559         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4560     return Failure;
4561   }
4562 
4563   // This is used for compatibility with older PCH formats.
4564   bool HaveReadControlBlock = false;
4565   while (true) {
4566     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4567     if (!MaybeEntry) {
4568       Error(MaybeEntry.takeError());
4569       return Failure;
4570     }
4571     llvm::BitstreamEntry Entry = MaybeEntry.get();
4572 
4573     switch (Entry.Kind) {
4574     case llvm::BitstreamEntry::Error:
4575     case llvm::BitstreamEntry::Record:
4576     case llvm::BitstreamEntry::EndBlock:
4577       Error("invalid record at top-level of AST file");
4578       return Failure;
4579 
4580     case llvm::BitstreamEntry::SubBlock:
4581       break;
4582     }
4583 
4584     switch (Entry.ID) {
4585     case CONTROL_BLOCK_ID:
4586       HaveReadControlBlock = true;
4587       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4588       case Success:
4589         // Check that we didn't try to load a non-module AST file as a module.
4590         //
4591         // FIXME: Should we also perform the converse check? Loading a module as
4592         // a PCH file sort of works, but it's a bit wonky.
4593         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4594              Type == MK_PrebuiltModule) &&
4595             F.ModuleName.empty()) {
4596           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4597           if (Result != OutOfDate ||
4598               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4599             Diag(diag::err_module_file_not_module) << FileName;
4600           return Result;
4601         }
4602         break;
4603 
4604       case Failure: return Failure;
4605       case Missing: return Missing;
4606       case OutOfDate: return OutOfDate;
4607       case VersionMismatch: return VersionMismatch;
4608       case ConfigurationMismatch: return ConfigurationMismatch;
4609       case HadErrors: return HadErrors;
4610       }
4611       break;
4612 
4613     case AST_BLOCK_ID:
4614       if (!HaveReadControlBlock) {
4615         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4616           Diag(diag::err_pch_version_too_old);
4617         return VersionMismatch;
4618       }
4619 
4620       // Record that we've loaded this module.
4621       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4622       ShouldFinalizePCM = true;
4623       return Success;
4624 
4625     case UNHASHED_CONTROL_BLOCK_ID:
4626       // This block is handled using look-ahead during ReadControlBlock.  We
4627       // shouldn't get here!
4628       Error("malformed block record in AST file");
4629       return Failure;
4630 
4631     default:
4632       if (llvm::Error Err = Stream.SkipBlock()) {
4633         Error(std::move(Err));
4634         return Failure;
4635       }
4636       break;
4637     }
4638   }
4639 
4640   llvm_unreachable("unexpected break; expected return");
4641 }
4642 
4643 ASTReader::ASTReadResult
4644 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4645                                     unsigned ClientLoadCapabilities) {
4646   const HeaderSearchOptions &HSOpts =
4647       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4648   bool AllowCompatibleConfigurationMismatch =
4649       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4650   bool DisableValidation = shouldDisableValidationForFile(F);
4651 
4652   ASTReadResult Result = readUnhashedControlBlockImpl(
4653       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4654       Listener.get(),
4655       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4656 
4657   // If F was directly imported by another module, it's implicitly validated by
4658   // the importing module.
4659   if (DisableValidation || WasImportedBy ||
4660       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4661     return Success;
4662 
4663   if (Result == Failure) {
4664     Error("malformed block record in AST file");
4665     return Failure;
4666   }
4667 
4668   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4669     // If this module has already been finalized in the ModuleCache, we're stuck
4670     // with it; we can only load a single version of each module.
4671     //
4672     // This can happen when a module is imported in two contexts: in one, as a
4673     // user module; in another, as a system module (due to an import from
4674     // another module marked with the [system] flag).  It usually indicates a
4675     // bug in the module map: this module should also be marked with [system].
4676     //
4677     // If -Wno-system-headers (the default), and the first import is as a
4678     // system module, then validation will fail during the as-user import,
4679     // since -Werror flags won't have been validated.  However, it's reasonable
4680     // to treat this consistently as a system module.
4681     //
4682     // If -Wsystem-headers, the PCM on disk was built with
4683     // -Wno-system-headers, and the first import is as a user module, then
4684     // validation will fail during the as-system import since the PCM on disk
4685     // doesn't guarantee that -Werror was respected.  However, the -Werror
4686     // flags were checked during the initial as-user import.
4687     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4688       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4689       return Success;
4690     }
4691   }
4692 
4693   return Result;
4694 }
4695 
4696 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4697     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4698     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4699     bool ValidateDiagnosticOptions) {
4700   // Initialize a stream.
4701   BitstreamCursor Stream(StreamData);
4702 
4703   // Sniff for the signature.
4704   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4705     // FIXME this drops the error on the floor.
4706     consumeError(std::move(Err));
4707     return Failure;
4708   }
4709 
4710   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4711   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4712     return Failure;
4713 
4714   // Read all of the records in the options block.
4715   RecordData Record;
4716   ASTReadResult Result = Success;
4717   while (true) {
4718     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4719     if (!MaybeEntry) {
4720       // FIXME this drops the error on the floor.
4721       consumeError(MaybeEntry.takeError());
4722       return Failure;
4723     }
4724     llvm::BitstreamEntry Entry = MaybeEntry.get();
4725 
4726     switch (Entry.Kind) {
4727     case llvm::BitstreamEntry::Error:
4728     case llvm::BitstreamEntry::SubBlock:
4729       return Failure;
4730 
4731     case llvm::BitstreamEntry::EndBlock:
4732       return Result;
4733 
4734     case llvm::BitstreamEntry::Record:
4735       // The interesting case.
4736       break;
4737     }
4738 
4739     // Read and process a record.
4740     Record.clear();
4741     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4742     if (!MaybeRecordType) {
4743       // FIXME this drops the error.
4744       return Failure;
4745     }
4746     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4747     case SIGNATURE:
4748       if (F)
4749         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4750       break;
4751     case AST_BLOCK_HASH:
4752       if (F)
4753         F->ASTBlockHash =
4754             ASTFileSignature::create(Record.begin(), Record.end());
4755       break;
4756     case DIAGNOSTIC_OPTIONS: {
4757       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4758       if (Listener && ValidateDiagnosticOptions &&
4759           !AllowCompatibleConfigurationMismatch &&
4760           ParseDiagnosticOptions(Record, Complain, *Listener))
4761         Result = OutOfDate; // Don't return early.  Read the signature.
4762       break;
4763     }
4764     case DIAG_PRAGMA_MAPPINGS:
4765       if (!F)
4766         break;
4767       if (F->PragmaDiagMappings.empty())
4768         F->PragmaDiagMappings.swap(Record);
4769       else
4770         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4771                                      Record.begin(), Record.end());
4772       break;
4773     }
4774   }
4775 }
4776 
4777 /// Parse a record and blob containing module file extension metadata.
4778 static bool parseModuleFileExtensionMetadata(
4779               const SmallVectorImpl<uint64_t> &Record,
4780               StringRef Blob,
4781               ModuleFileExtensionMetadata &Metadata) {
4782   if (Record.size() < 4) return true;
4783 
4784   Metadata.MajorVersion = Record[0];
4785   Metadata.MinorVersion = Record[1];
4786 
4787   unsigned BlockNameLen = Record[2];
4788   unsigned UserInfoLen = Record[3];
4789 
4790   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4791 
4792   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4793   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4794                                   Blob.data() + BlockNameLen + UserInfoLen);
4795   return false;
4796 }
4797 
4798 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4799   BitstreamCursor &Stream = F.Stream;
4800 
4801   RecordData Record;
4802   while (true) {
4803     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4804     if (!MaybeEntry) {
4805       Error(MaybeEntry.takeError());
4806       return Failure;
4807     }
4808     llvm::BitstreamEntry Entry = MaybeEntry.get();
4809 
4810     switch (Entry.Kind) {
4811     case llvm::BitstreamEntry::SubBlock:
4812       if (llvm::Error Err = Stream.SkipBlock()) {
4813         Error(std::move(Err));
4814         return Failure;
4815       }
4816       continue;
4817 
4818     case llvm::BitstreamEntry::EndBlock:
4819       return Success;
4820 
4821     case llvm::BitstreamEntry::Error:
4822       return HadErrors;
4823 
4824     case llvm::BitstreamEntry::Record:
4825       break;
4826     }
4827 
4828     Record.clear();
4829     StringRef Blob;
4830     Expected<unsigned> MaybeRecCode =
4831         Stream.readRecord(Entry.ID, Record, &Blob);
4832     if (!MaybeRecCode) {
4833       Error(MaybeRecCode.takeError());
4834       return Failure;
4835     }
4836     switch (MaybeRecCode.get()) {
4837     case EXTENSION_METADATA: {
4838       ModuleFileExtensionMetadata Metadata;
4839       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4840         Error("malformed EXTENSION_METADATA in AST file");
4841         return Failure;
4842       }
4843 
4844       // Find a module file extension with this block name.
4845       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4846       if (Known == ModuleFileExtensions.end()) break;
4847 
4848       // Form a reader.
4849       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4850                                                              F, Stream)) {
4851         F.ExtensionReaders.push_back(std::move(Reader));
4852       }
4853 
4854       break;
4855     }
4856     }
4857   }
4858 
4859   return Success;
4860 }
4861 
4862 void ASTReader::InitializeContext() {
4863   assert(ContextObj && "no context to initialize");
4864   ASTContext &Context = *ContextObj;
4865 
4866   // If there's a listener, notify them that we "read" the translation unit.
4867   if (DeserializationListener)
4868     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4869                                       Context.getTranslationUnitDecl());
4870 
4871   // FIXME: Find a better way to deal with collisions between these
4872   // built-in types. Right now, we just ignore the problem.
4873 
4874   // Load the special types.
4875   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4876     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4877       if (!Context.CFConstantStringTypeDecl)
4878         Context.setCFConstantStringType(GetType(String));
4879     }
4880 
4881     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4882       QualType FileType = GetType(File);
4883       if (FileType.isNull()) {
4884         Error("FILE type is NULL");
4885         return;
4886       }
4887 
4888       if (!Context.FILEDecl) {
4889         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4890           Context.setFILEDecl(Typedef->getDecl());
4891         else {
4892           const TagType *Tag = FileType->getAs<TagType>();
4893           if (!Tag) {
4894             Error("Invalid FILE type in AST file");
4895             return;
4896           }
4897           Context.setFILEDecl(Tag->getDecl());
4898         }
4899       }
4900     }
4901 
4902     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4903       QualType Jmp_bufType = GetType(Jmp_buf);
4904       if (Jmp_bufType.isNull()) {
4905         Error("jmp_buf type is NULL");
4906         return;
4907       }
4908 
4909       if (!Context.jmp_bufDecl) {
4910         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4911           Context.setjmp_bufDecl(Typedef->getDecl());
4912         else {
4913           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4914           if (!Tag) {
4915             Error("Invalid jmp_buf type in AST file");
4916             return;
4917           }
4918           Context.setjmp_bufDecl(Tag->getDecl());
4919         }
4920       }
4921     }
4922 
4923     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4924       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4925       if (Sigjmp_bufType.isNull()) {
4926         Error("sigjmp_buf type is NULL");
4927         return;
4928       }
4929 
4930       if (!Context.sigjmp_bufDecl) {
4931         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4932           Context.setsigjmp_bufDecl(Typedef->getDecl());
4933         else {
4934           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4935           assert(Tag && "Invalid sigjmp_buf type in AST file");
4936           Context.setsigjmp_bufDecl(Tag->getDecl());
4937         }
4938       }
4939     }
4940 
4941     if (unsigned ObjCIdRedef
4942           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4943       if (Context.ObjCIdRedefinitionType.isNull())
4944         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4945     }
4946 
4947     if (unsigned ObjCClassRedef
4948           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4949       if (Context.ObjCClassRedefinitionType.isNull())
4950         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4951     }
4952 
4953     if (unsigned ObjCSelRedef
4954           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4955       if (Context.ObjCSelRedefinitionType.isNull())
4956         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4957     }
4958 
4959     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4960       QualType Ucontext_tType = GetType(Ucontext_t);
4961       if (Ucontext_tType.isNull()) {
4962         Error("ucontext_t type is NULL");
4963         return;
4964       }
4965 
4966       if (!Context.ucontext_tDecl) {
4967         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4968           Context.setucontext_tDecl(Typedef->getDecl());
4969         else {
4970           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4971           assert(Tag && "Invalid ucontext_t type in AST file");
4972           Context.setucontext_tDecl(Tag->getDecl());
4973         }
4974       }
4975     }
4976   }
4977 
4978   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4979 
4980   // If there were any CUDA special declarations, deserialize them.
4981   if (!CUDASpecialDeclRefs.empty()) {
4982     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4983     Context.setcudaConfigureCallDecl(
4984                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4985   }
4986 
4987   // Re-export any modules that were imported by a non-module AST file.
4988   // FIXME: This does not make macro-only imports visible again.
4989   for (auto &Import : ImportedModules) {
4990     if (Module *Imported = getSubmodule(Import.ID)) {
4991       makeModuleVisible(Imported, Module::AllVisible,
4992                         /*ImportLoc=*/Import.ImportLoc);
4993       if (Import.ImportLoc.isValid())
4994         PP.makeModuleVisible(Imported, Import.ImportLoc);
4995       // This updates visibility for Preprocessor only. For Sema, which can be
4996       // nullptr here, we do the same later, in UpdateSema().
4997     }
4998   }
4999 }
5000 
5001 void ASTReader::finalizeForWriting() {
5002   // Nothing to do for now.
5003 }
5004 
5005 /// Reads and return the signature record from \p PCH's control block, or
5006 /// else returns 0.
5007 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5008   BitstreamCursor Stream(PCH);
5009   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5010     // FIXME this drops the error on the floor.
5011     consumeError(std::move(Err));
5012     return ASTFileSignature();
5013   }
5014 
5015   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5016   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5017     return ASTFileSignature();
5018 
5019   // Scan for SIGNATURE inside the diagnostic options block.
5020   ASTReader::RecordData Record;
5021   while (true) {
5022     Expected<llvm::BitstreamEntry> MaybeEntry =
5023         Stream.advanceSkippingSubblocks();
5024     if (!MaybeEntry) {
5025       // FIXME this drops the error on the floor.
5026       consumeError(MaybeEntry.takeError());
5027       return ASTFileSignature();
5028     }
5029     llvm::BitstreamEntry Entry = MaybeEntry.get();
5030 
5031     if (Entry.Kind != llvm::BitstreamEntry::Record)
5032       return ASTFileSignature();
5033 
5034     Record.clear();
5035     StringRef Blob;
5036     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5037     if (!MaybeRecord) {
5038       // FIXME this drops the error on the floor.
5039       consumeError(MaybeRecord.takeError());
5040       return ASTFileSignature();
5041     }
5042     if (SIGNATURE == MaybeRecord.get())
5043       return ASTFileSignature::create(Record.begin(),
5044                                       Record.begin() + ASTFileSignature::size);
5045   }
5046 }
5047 
5048 /// Retrieve the name of the original source file name
5049 /// directly from the AST file, without actually loading the AST
5050 /// file.
5051 std::string ASTReader::getOriginalSourceFile(
5052     const std::string &ASTFileName, FileManager &FileMgr,
5053     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5054   // Open the AST file.
5055   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5056   if (!Buffer) {
5057     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5058         << ASTFileName << Buffer.getError().message();
5059     return std::string();
5060   }
5061 
5062   // Initialize the stream
5063   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5064 
5065   // Sniff for the signature.
5066   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5067     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5068     return std::string();
5069   }
5070 
5071   // Scan for the CONTROL_BLOCK_ID block.
5072   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5073     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5074     return std::string();
5075   }
5076 
5077   // Scan for ORIGINAL_FILE inside the control block.
5078   RecordData Record;
5079   while (true) {
5080     Expected<llvm::BitstreamEntry> MaybeEntry =
5081         Stream.advanceSkippingSubblocks();
5082     if (!MaybeEntry) {
5083       // FIXME this drops errors on the floor.
5084       consumeError(MaybeEntry.takeError());
5085       return std::string();
5086     }
5087     llvm::BitstreamEntry Entry = MaybeEntry.get();
5088 
5089     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5090       return std::string();
5091 
5092     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5093       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5094       return std::string();
5095     }
5096 
5097     Record.clear();
5098     StringRef Blob;
5099     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5100     if (!MaybeRecord) {
5101       // FIXME this drops the errors on the floor.
5102       consumeError(MaybeRecord.takeError());
5103       return std::string();
5104     }
5105     if (ORIGINAL_FILE == MaybeRecord.get())
5106       return Blob.str();
5107   }
5108 }
5109 
5110 namespace {
5111 
5112   class SimplePCHValidator : public ASTReaderListener {
5113     const LangOptions &ExistingLangOpts;
5114     const TargetOptions &ExistingTargetOpts;
5115     const PreprocessorOptions &ExistingPPOpts;
5116     std::string ExistingModuleCachePath;
5117     FileManager &FileMgr;
5118 
5119   public:
5120     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5121                        const TargetOptions &ExistingTargetOpts,
5122                        const PreprocessorOptions &ExistingPPOpts,
5123                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5124         : ExistingLangOpts(ExistingLangOpts),
5125           ExistingTargetOpts(ExistingTargetOpts),
5126           ExistingPPOpts(ExistingPPOpts),
5127           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5128 
5129     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5130                              bool AllowCompatibleDifferences) override {
5131       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5132                                   AllowCompatibleDifferences);
5133     }
5134 
5135     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5136                            bool AllowCompatibleDifferences) override {
5137       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5138                                 AllowCompatibleDifferences);
5139     }
5140 
5141     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5142                                  StringRef SpecificModuleCachePath,
5143                                  bool Complain) override {
5144       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5145                                       ExistingModuleCachePath,
5146                                       nullptr, ExistingLangOpts);
5147     }
5148 
5149     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5150                                  bool Complain,
5151                                  std::string &SuggestedPredefines) override {
5152       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5153                                       SuggestedPredefines, ExistingLangOpts);
5154     }
5155   };
5156 
5157 } // namespace
5158 
5159 bool ASTReader::readASTFileControlBlock(
5160     StringRef Filename, FileManager &FileMgr,
5161     const PCHContainerReader &PCHContainerRdr,
5162     bool FindModuleFileExtensions,
5163     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5164   // Open the AST file.
5165   // FIXME: This allows use of the VFS; we do not allow use of the
5166   // VFS when actually loading a module.
5167   auto Buffer = FileMgr.getBufferForFile(Filename);
5168   if (!Buffer) {
5169     return true;
5170   }
5171 
5172   // Initialize the stream
5173   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5174   BitstreamCursor Stream(Bytes);
5175 
5176   // Sniff for the signature.
5177   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5178     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5179     return true;
5180   }
5181 
5182   // Scan for the CONTROL_BLOCK_ID block.
5183   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5184     return true;
5185 
5186   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5187   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5188   bool NeedsImports = Listener.needsImportVisitation();
5189   BitstreamCursor InputFilesCursor;
5190 
5191   RecordData Record;
5192   std::string ModuleDir;
5193   bool DoneWithControlBlock = false;
5194   while (!DoneWithControlBlock) {
5195     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5196     if (!MaybeEntry) {
5197       // FIXME this drops the error on the floor.
5198       consumeError(MaybeEntry.takeError());
5199       return true;
5200     }
5201     llvm::BitstreamEntry Entry = MaybeEntry.get();
5202 
5203     switch (Entry.Kind) {
5204     case llvm::BitstreamEntry::SubBlock: {
5205       switch (Entry.ID) {
5206       case OPTIONS_BLOCK_ID: {
5207         std::string IgnoredSuggestedPredefines;
5208         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5209                              /*AllowCompatibleConfigurationMismatch*/ false,
5210                              Listener, IgnoredSuggestedPredefines) != Success)
5211           return true;
5212         break;
5213       }
5214 
5215       case INPUT_FILES_BLOCK_ID:
5216         InputFilesCursor = Stream;
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         if (NeedsInputFiles &&
5223             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5224           return true;
5225         break;
5226 
5227       default:
5228         if (llvm::Error Err = Stream.SkipBlock()) {
5229           // FIXME this drops the error on the floor.
5230           consumeError(std::move(Err));
5231           return true;
5232         }
5233         break;
5234       }
5235 
5236       continue;
5237     }
5238 
5239     case llvm::BitstreamEntry::EndBlock:
5240       DoneWithControlBlock = true;
5241       break;
5242 
5243     case llvm::BitstreamEntry::Error:
5244       return true;
5245 
5246     case llvm::BitstreamEntry::Record:
5247       break;
5248     }
5249 
5250     if (DoneWithControlBlock) break;
5251 
5252     Record.clear();
5253     StringRef Blob;
5254     Expected<unsigned> MaybeRecCode =
5255         Stream.readRecord(Entry.ID, Record, &Blob);
5256     if (!MaybeRecCode) {
5257       // FIXME this drops the error.
5258       return Failure;
5259     }
5260     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5261     case METADATA:
5262       if (Record[0] != VERSION_MAJOR)
5263         return true;
5264       if (Listener.ReadFullVersionInformation(Blob))
5265         return true;
5266       break;
5267     case MODULE_NAME:
5268       Listener.ReadModuleName(Blob);
5269       break;
5270     case MODULE_DIRECTORY:
5271       ModuleDir = std::string(Blob);
5272       break;
5273     case MODULE_MAP_FILE: {
5274       unsigned Idx = 0;
5275       auto Path = ReadString(Record, Idx);
5276       ResolveImportedPath(Path, ModuleDir);
5277       Listener.ReadModuleMapFile(Path);
5278       break;
5279     }
5280     case INPUT_FILE_OFFSETS: {
5281       if (!NeedsInputFiles)
5282         break;
5283 
5284       unsigned NumInputFiles = Record[0];
5285       unsigned NumUserFiles = Record[1];
5286       const llvm::support::unaligned_uint64_t *InputFileOffs =
5287           (const llvm::support::unaligned_uint64_t *)Blob.data();
5288       for (unsigned I = 0; I != NumInputFiles; ++I) {
5289         // Go find this input file.
5290         bool isSystemFile = I >= NumUserFiles;
5291 
5292         if (isSystemFile && !NeedsSystemInputFiles)
5293           break; // the rest are system input files
5294 
5295         BitstreamCursor &Cursor = InputFilesCursor;
5296         SavedStreamPosition SavedPosition(Cursor);
5297         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5298           // FIXME this drops errors on the floor.
5299           consumeError(std::move(Err));
5300         }
5301 
5302         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5303         if (!MaybeCode) {
5304           // FIXME this drops errors on the floor.
5305           consumeError(MaybeCode.takeError());
5306         }
5307         unsigned Code = MaybeCode.get();
5308 
5309         RecordData Record;
5310         StringRef Blob;
5311         bool shouldContinue = false;
5312         Expected<unsigned> MaybeRecordType =
5313             Cursor.readRecord(Code, Record, &Blob);
5314         if (!MaybeRecordType) {
5315           // FIXME this drops errors on the floor.
5316           consumeError(MaybeRecordType.takeError());
5317         }
5318         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5319         case INPUT_FILE_HASH:
5320           break;
5321         case INPUT_FILE:
5322           bool Overridden = static_cast<bool>(Record[3]);
5323           std::string Filename = std::string(Blob);
5324           ResolveImportedPath(Filename, ModuleDir);
5325           shouldContinue = Listener.visitInputFile(
5326               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5327           break;
5328         }
5329         if (!shouldContinue)
5330           break;
5331       }
5332       break;
5333     }
5334 
5335     case IMPORTS: {
5336       if (!NeedsImports)
5337         break;
5338 
5339       unsigned Idx = 0, N = Record.size();
5340       while (Idx < N) {
5341         // Read information about the AST file.
5342         Idx +=
5343             1 + 1 + 1 + 1 +
5344             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5345         std::string ModuleName = ReadString(Record, Idx);
5346         std::string Filename = ReadString(Record, Idx);
5347         ResolveImportedPath(Filename, ModuleDir);
5348         Listener.visitImport(ModuleName, Filename);
5349       }
5350       break;
5351     }
5352 
5353     default:
5354       // No other validation to perform.
5355       break;
5356     }
5357   }
5358 
5359   // Look for module file extension blocks, if requested.
5360   if (FindModuleFileExtensions) {
5361     BitstreamCursor SavedStream = Stream;
5362     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5363       bool DoneWithExtensionBlock = false;
5364       while (!DoneWithExtensionBlock) {
5365         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5366         if (!MaybeEntry) {
5367           // FIXME this drops the error.
5368           return true;
5369         }
5370         llvm::BitstreamEntry Entry = MaybeEntry.get();
5371 
5372         switch (Entry.Kind) {
5373         case llvm::BitstreamEntry::SubBlock:
5374           if (llvm::Error Err = Stream.SkipBlock()) {
5375             // FIXME this drops the error on the floor.
5376             consumeError(std::move(Err));
5377             return true;
5378           }
5379           continue;
5380 
5381         case llvm::BitstreamEntry::EndBlock:
5382           DoneWithExtensionBlock = true;
5383           continue;
5384 
5385         case llvm::BitstreamEntry::Error:
5386           return true;
5387 
5388         case llvm::BitstreamEntry::Record:
5389           break;
5390         }
5391 
5392        Record.clear();
5393        StringRef Blob;
5394        Expected<unsigned> MaybeRecCode =
5395            Stream.readRecord(Entry.ID, Record, &Blob);
5396        if (!MaybeRecCode) {
5397          // FIXME this drops the error.
5398          return true;
5399        }
5400        switch (MaybeRecCode.get()) {
5401        case EXTENSION_METADATA: {
5402          ModuleFileExtensionMetadata Metadata;
5403          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5404            return true;
5405 
5406          Listener.readModuleFileExtension(Metadata);
5407          break;
5408        }
5409        }
5410       }
5411     }
5412     Stream = SavedStream;
5413   }
5414 
5415   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5416   if (readUnhashedControlBlockImpl(
5417           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5418           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5419           ValidateDiagnosticOptions) != Success)
5420     return true;
5421 
5422   return false;
5423 }
5424 
5425 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5426                                     const PCHContainerReader &PCHContainerRdr,
5427                                     const LangOptions &LangOpts,
5428                                     const TargetOptions &TargetOpts,
5429                                     const PreprocessorOptions &PPOpts,
5430                                     StringRef ExistingModuleCachePath) {
5431   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5432                                ExistingModuleCachePath, FileMgr);
5433   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5434                                   /*FindModuleFileExtensions=*/false,
5435                                   validator,
5436                                   /*ValidateDiagnosticOptions=*/true);
5437 }
5438 
5439 ASTReader::ASTReadResult
5440 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5441   // Enter the submodule block.
5442   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5443     Error(std::move(Err));
5444     return Failure;
5445   }
5446 
5447   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5448   bool First = true;
5449   Module *CurrentModule = nullptr;
5450   RecordData Record;
5451   while (true) {
5452     Expected<llvm::BitstreamEntry> MaybeEntry =
5453         F.Stream.advanceSkippingSubblocks();
5454     if (!MaybeEntry) {
5455       Error(MaybeEntry.takeError());
5456       return Failure;
5457     }
5458     llvm::BitstreamEntry Entry = MaybeEntry.get();
5459 
5460     switch (Entry.Kind) {
5461     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5462     case llvm::BitstreamEntry::Error:
5463       Error("malformed block record in AST file");
5464       return Failure;
5465     case llvm::BitstreamEntry::EndBlock:
5466       return Success;
5467     case llvm::BitstreamEntry::Record:
5468       // The interesting case.
5469       break;
5470     }
5471 
5472     // Read a record.
5473     StringRef Blob;
5474     Record.clear();
5475     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5476     if (!MaybeKind) {
5477       Error(MaybeKind.takeError());
5478       return Failure;
5479     }
5480     unsigned Kind = MaybeKind.get();
5481 
5482     if ((Kind == SUBMODULE_METADATA) != First) {
5483       Error("submodule metadata record should be at beginning of block");
5484       return Failure;
5485     }
5486     First = false;
5487 
5488     // Submodule information is only valid if we have a current module.
5489     // FIXME: Should we error on these cases?
5490     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5491         Kind != SUBMODULE_DEFINITION)
5492       continue;
5493 
5494     switch (Kind) {
5495     default:  // Default behavior: ignore.
5496       break;
5497 
5498     case SUBMODULE_DEFINITION: {
5499       if (Record.size() < 12) {
5500         Error("malformed module definition");
5501         return Failure;
5502       }
5503 
5504       StringRef Name = Blob;
5505       unsigned Idx = 0;
5506       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5507       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5508       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5509       bool IsFramework = Record[Idx++];
5510       bool IsExplicit = Record[Idx++];
5511       bool IsSystem = Record[Idx++];
5512       bool IsExternC = Record[Idx++];
5513       bool InferSubmodules = Record[Idx++];
5514       bool InferExplicitSubmodules = Record[Idx++];
5515       bool InferExportWildcard = Record[Idx++];
5516       bool ConfigMacrosExhaustive = Record[Idx++];
5517       bool ModuleMapIsPrivate = Record[Idx++];
5518 
5519       Module *ParentModule = nullptr;
5520       if (Parent)
5521         ParentModule = getSubmodule(Parent);
5522 
5523       // Retrieve this (sub)module from the module map, creating it if
5524       // necessary.
5525       CurrentModule =
5526           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5527               .first;
5528 
5529       // FIXME: set the definition loc for CurrentModule, or call
5530       // ModMap.setInferredModuleAllowedBy()
5531 
5532       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5533       if (GlobalIndex >= SubmodulesLoaded.size() ||
5534           SubmodulesLoaded[GlobalIndex]) {
5535         Error("too many submodules");
5536         return Failure;
5537       }
5538 
5539       if (!ParentModule) {
5540         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5541           // Don't emit module relocation error if we have -fno-validate-pch
5542           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5543                     DisableValidationForModuleKind::Module) &&
5544               CurFile != F.File) {
5545             Error(diag::err_module_file_conflict,
5546                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5547                   F.File->getName());
5548             return Failure;
5549           }
5550         }
5551 
5552         F.DidReadTopLevelSubmodule = true;
5553         CurrentModule->setASTFile(F.File);
5554         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5555       }
5556 
5557       CurrentModule->Kind = Kind;
5558       CurrentModule->Signature = F.Signature;
5559       CurrentModule->IsFromModuleFile = true;
5560       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5561       CurrentModule->IsExternC = IsExternC;
5562       CurrentModule->InferSubmodules = InferSubmodules;
5563       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5564       CurrentModule->InferExportWildcard = InferExportWildcard;
5565       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5566       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5567       if (DeserializationListener)
5568         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5569 
5570       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5571 
5572       // Clear out data that will be replaced by what is in the module file.
5573       CurrentModule->LinkLibraries.clear();
5574       CurrentModule->ConfigMacros.clear();
5575       CurrentModule->UnresolvedConflicts.clear();
5576       CurrentModule->Conflicts.clear();
5577 
5578       // The module is available unless it's missing a requirement; relevant
5579       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5580       // Missing headers that were present when the module was built do not
5581       // make it unavailable -- if we got this far, this must be an explicitly
5582       // imported module file.
5583       CurrentModule->Requirements.clear();
5584       CurrentModule->MissingHeaders.clear();
5585       CurrentModule->IsUnimportable =
5586           ParentModule && ParentModule->IsUnimportable;
5587       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5588       break;
5589     }
5590 
5591     case SUBMODULE_UMBRELLA_HEADER: {
5592       std::string Filename = std::string(Blob);
5593       ResolveImportedPath(F, Filename);
5594       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5595         if (!CurrentModule->getUmbrellaHeader())
5596           // FIXME: NameAsWritten
5597           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5598         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5599           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5600             Error("mismatched umbrella headers in submodule");
5601           return OutOfDate;
5602         }
5603       }
5604       break;
5605     }
5606 
5607     case SUBMODULE_HEADER:
5608     case SUBMODULE_EXCLUDED_HEADER:
5609     case SUBMODULE_PRIVATE_HEADER:
5610       // We lazily associate headers with their modules via the HeaderInfo table.
5611       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5612       // of complete filenames or remove it entirely.
5613       break;
5614 
5615     case SUBMODULE_TEXTUAL_HEADER:
5616     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5617       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5618       // them here.
5619       break;
5620 
5621     case SUBMODULE_TOPHEADER:
5622       CurrentModule->addTopHeaderFilename(Blob);
5623       break;
5624 
5625     case SUBMODULE_UMBRELLA_DIR: {
5626       std::string Dirname = std::string(Blob);
5627       ResolveImportedPath(F, Dirname);
5628       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5629         if (!CurrentModule->getUmbrellaDir())
5630           // FIXME: NameAsWritten
5631           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5632         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5633           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5634             Error("mismatched umbrella directories in submodule");
5635           return OutOfDate;
5636         }
5637       }
5638       break;
5639     }
5640 
5641     case SUBMODULE_METADATA: {
5642       F.BaseSubmoduleID = getTotalNumSubmodules();
5643       F.LocalNumSubmodules = Record[0];
5644       unsigned LocalBaseSubmoduleID = Record[1];
5645       if (F.LocalNumSubmodules > 0) {
5646         // Introduce the global -> local mapping for submodules within this
5647         // module.
5648         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5649 
5650         // Introduce the local -> global mapping for submodules within this
5651         // module.
5652         F.SubmoduleRemap.insertOrReplace(
5653           std::make_pair(LocalBaseSubmoduleID,
5654                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5655 
5656         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5657       }
5658       break;
5659     }
5660 
5661     case SUBMODULE_IMPORTS:
5662       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5663         UnresolvedModuleRef Unresolved;
5664         Unresolved.File = &F;
5665         Unresolved.Mod = CurrentModule;
5666         Unresolved.ID = Record[Idx];
5667         Unresolved.Kind = UnresolvedModuleRef::Import;
5668         Unresolved.IsWildcard = false;
5669         UnresolvedModuleRefs.push_back(Unresolved);
5670       }
5671       break;
5672 
5673     case SUBMODULE_EXPORTS:
5674       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5675         UnresolvedModuleRef Unresolved;
5676         Unresolved.File = &F;
5677         Unresolved.Mod = CurrentModule;
5678         Unresolved.ID = Record[Idx];
5679         Unresolved.Kind = UnresolvedModuleRef::Export;
5680         Unresolved.IsWildcard = Record[Idx + 1];
5681         UnresolvedModuleRefs.push_back(Unresolved);
5682       }
5683 
5684       // Once we've loaded the set of exports, there's no reason to keep
5685       // the parsed, unresolved exports around.
5686       CurrentModule->UnresolvedExports.clear();
5687       break;
5688 
5689     case SUBMODULE_REQUIRES:
5690       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5691                                     PP.getTargetInfo());
5692       break;
5693 
5694     case SUBMODULE_LINK_LIBRARY:
5695       ModMap.resolveLinkAsDependencies(CurrentModule);
5696       CurrentModule->LinkLibraries.push_back(
5697           Module::LinkLibrary(std::string(Blob), Record[0]));
5698       break;
5699 
5700     case SUBMODULE_CONFIG_MACRO:
5701       CurrentModule->ConfigMacros.push_back(Blob.str());
5702       break;
5703 
5704     case SUBMODULE_CONFLICT: {
5705       UnresolvedModuleRef Unresolved;
5706       Unresolved.File = &F;
5707       Unresolved.Mod = CurrentModule;
5708       Unresolved.ID = Record[0];
5709       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5710       Unresolved.IsWildcard = false;
5711       Unresolved.String = Blob;
5712       UnresolvedModuleRefs.push_back(Unresolved);
5713       break;
5714     }
5715 
5716     case SUBMODULE_INITIALIZERS: {
5717       if (!ContextObj)
5718         break;
5719       SmallVector<uint32_t, 16> Inits;
5720       for (auto &ID : Record)
5721         Inits.push_back(getGlobalDeclID(F, ID));
5722       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5723       break;
5724     }
5725 
5726     case SUBMODULE_EXPORT_AS:
5727       CurrentModule->ExportAsModule = Blob.str();
5728       ModMap.addLinkAsDependency(CurrentModule);
5729       break;
5730     }
5731   }
5732 }
5733 
5734 /// Parse the record that corresponds to a LangOptions data
5735 /// structure.
5736 ///
5737 /// This routine parses the language options from the AST file and then gives
5738 /// them to the AST listener if one is set.
5739 ///
5740 /// \returns true if the listener deems the file unacceptable, false otherwise.
5741 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5742                                      bool Complain,
5743                                      ASTReaderListener &Listener,
5744                                      bool AllowCompatibleDifferences) {
5745   LangOptions LangOpts;
5746   unsigned Idx = 0;
5747 #define LANGOPT(Name, Bits, Default, Description) \
5748   LangOpts.Name = Record[Idx++];
5749 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5750   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5751 #include "clang/Basic/LangOptions.def"
5752 #define SANITIZER(NAME, ID)                                                    \
5753   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5754 #include "clang/Basic/Sanitizers.def"
5755 
5756   for (unsigned N = Record[Idx++]; N; --N)
5757     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5758 
5759   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5760   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5761   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5762 
5763   LangOpts.CurrentModule = ReadString(Record, Idx);
5764 
5765   // Comment options.
5766   for (unsigned N = Record[Idx++]; N; --N) {
5767     LangOpts.CommentOpts.BlockCommandNames.push_back(
5768       ReadString(Record, Idx));
5769   }
5770   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5771 
5772   // OpenMP offloading options.
5773   for (unsigned N = Record[Idx++]; N; --N) {
5774     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5775   }
5776 
5777   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5778 
5779   return Listener.ReadLanguageOptions(LangOpts, Complain,
5780                                       AllowCompatibleDifferences);
5781 }
5782 
5783 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5784                                    ASTReaderListener &Listener,
5785                                    bool AllowCompatibleDifferences) {
5786   unsigned Idx = 0;
5787   TargetOptions TargetOpts;
5788   TargetOpts.Triple = ReadString(Record, Idx);
5789   TargetOpts.CPU = ReadString(Record, Idx);
5790   TargetOpts.TuneCPU = ReadString(Record, Idx);
5791   TargetOpts.ABI = ReadString(Record, Idx);
5792   for (unsigned N = Record[Idx++]; N; --N) {
5793     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5794   }
5795   for (unsigned N = Record[Idx++]; N; --N) {
5796     TargetOpts.Features.push_back(ReadString(Record, Idx));
5797   }
5798 
5799   return Listener.ReadTargetOptions(TargetOpts, Complain,
5800                                     AllowCompatibleDifferences);
5801 }
5802 
5803 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5804                                        ASTReaderListener &Listener) {
5805   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5806   unsigned Idx = 0;
5807 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5808 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5809   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5810 #include "clang/Basic/DiagnosticOptions.def"
5811 
5812   for (unsigned N = Record[Idx++]; N; --N)
5813     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5814   for (unsigned N = Record[Idx++]; N; --N)
5815     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5816 
5817   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5818 }
5819 
5820 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5821                                        ASTReaderListener &Listener) {
5822   FileSystemOptions FSOpts;
5823   unsigned Idx = 0;
5824   FSOpts.WorkingDir = ReadString(Record, Idx);
5825   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5826 }
5827 
5828 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5829                                          bool Complain,
5830                                          ASTReaderListener &Listener) {
5831   HeaderSearchOptions HSOpts;
5832   unsigned Idx = 0;
5833   HSOpts.Sysroot = ReadString(Record, Idx);
5834 
5835   // Include entries.
5836   for (unsigned N = Record[Idx++]; N; --N) {
5837     std::string Path = ReadString(Record, Idx);
5838     frontend::IncludeDirGroup Group
5839       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5840     bool IsFramework = Record[Idx++];
5841     bool IgnoreSysRoot = Record[Idx++];
5842     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5843                                     IgnoreSysRoot);
5844   }
5845 
5846   // System header prefixes.
5847   for (unsigned N = Record[Idx++]; N; --N) {
5848     std::string Prefix = ReadString(Record, Idx);
5849     bool IsSystemHeader = Record[Idx++];
5850     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5851   }
5852 
5853   HSOpts.ResourceDir = ReadString(Record, Idx);
5854   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5855   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5856   HSOpts.DisableModuleHash = Record[Idx++];
5857   HSOpts.ImplicitModuleMaps = Record[Idx++];
5858   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5859   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5860   HSOpts.UseBuiltinIncludes = Record[Idx++];
5861   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5862   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5863   HSOpts.UseLibcxx = Record[Idx++];
5864   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5865 
5866   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5867                                           Complain);
5868 }
5869 
5870 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5871                                          bool Complain,
5872                                          ASTReaderListener &Listener,
5873                                          std::string &SuggestedPredefines) {
5874   PreprocessorOptions PPOpts;
5875   unsigned Idx = 0;
5876 
5877   // Macro definitions/undefs
5878   for (unsigned N = Record[Idx++]; N; --N) {
5879     std::string Macro = ReadString(Record, Idx);
5880     bool IsUndef = Record[Idx++];
5881     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5882   }
5883 
5884   // Includes
5885   for (unsigned N = Record[Idx++]; N; --N) {
5886     PPOpts.Includes.push_back(ReadString(Record, Idx));
5887   }
5888 
5889   // Macro Includes
5890   for (unsigned N = Record[Idx++]; N; --N) {
5891     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5892   }
5893 
5894   PPOpts.UsePredefines = Record[Idx++];
5895   PPOpts.DetailedRecord = Record[Idx++];
5896   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5897   PPOpts.ObjCXXARCStandardLibrary =
5898     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5899   SuggestedPredefines.clear();
5900   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5901                                           SuggestedPredefines);
5902 }
5903 
5904 std::pair<ModuleFile *, unsigned>
5905 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5906   GlobalPreprocessedEntityMapType::iterator
5907   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5908   assert(I != GlobalPreprocessedEntityMap.end() &&
5909          "Corrupted global preprocessed entity map");
5910   ModuleFile *M = I->second;
5911   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5912   return std::make_pair(M, LocalIndex);
5913 }
5914 
5915 llvm::iterator_range<PreprocessingRecord::iterator>
5916 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5917   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5918     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5919                                              Mod.NumPreprocessedEntities);
5920 
5921   return llvm::make_range(PreprocessingRecord::iterator(),
5922                           PreprocessingRecord::iterator());
5923 }
5924 
5925 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5926 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5927   return llvm::make_range(
5928       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5929       ModuleDeclIterator(this, &Mod,
5930                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5931 }
5932 
5933 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5934   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5935   assert(I != GlobalSkippedRangeMap.end() &&
5936     "Corrupted global skipped range map");
5937   ModuleFile *M = I->second;
5938   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5939   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5940   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5941   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5942                     TranslateSourceLocation(*M, RawRange.getEnd()));
5943   assert(Range.isValid());
5944   return Range;
5945 }
5946 
5947 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5948   PreprocessedEntityID PPID = Index+1;
5949   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5950   ModuleFile &M = *PPInfo.first;
5951   unsigned LocalIndex = PPInfo.second;
5952   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5953 
5954   if (!PP.getPreprocessingRecord()) {
5955     Error("no preprocessing record");
5956     return nullptr;
5957   }
5958 
5959   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5960   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5961           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5962     Error(std::move(Err));
5963     return nullptr;
5964   }
5965 
5966   Expected<llvm::BitstreamEntry> MaybeEntry =
5967       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5968   if (!MaybeEntry) {
5969     Error(MaybeEntry.takeError());
5970     return nullptr;
5971   }
5972   llvm::BitstreamEntry Entry = MaybeEntry.get();
5973 
5974   if (Entry.Kind != llvm::BitstreamEntry::Record)
5975     return nullptr;
5976 
5977   // Read the record.
5978   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5979                     TranslateSourceLocation(M, PPOffs.getEnd()));
5980   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5981   StringRef Blob;
5982   RecordData Record;
5983   Expected<unsigned> MaybeRecType =
5984       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5985   if (!MaybeRecType) {
5986     Error(MaybeRecType.takeError());
5987     return nullptr;
5988   }
5989   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5990   case PPD_MACRO_EXPANSION: {
5991     bool isBuiltin = Record[0];
5992     IdentifierInfo *Name = nullptr;
5993     MacroDefinitionRecord *Def = nullptr;
5994     if (isBuiltin)
5995       Name = getLocalIdentifier(M, Record[1]);
5996     else {
5997       PreprocessedEntityID GlobalID =
5998           getGlobalPreprocessedEntityID(M, Record[1]);
5999       Def = cast<MacroDefinitionRecord>(
6000           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6001     }
6002 
6003     MacroExpansion *ME;
6004     if (isBuiltin)
6005       ME = new (PPRec) MacroExpansion(Name, Range);
6006     else
6007       ME = new (PPRec) MacroExpansion(Def, Range);
6008 
6009     return ME;
6010   }
6011 
6012   case PPD_MACRO_DEFINITION: {
6013     // Decode the identifier info and then check again; if the macro is
6014     // still defined and associated with the identifier,
6015     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6016     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6017 
6018     if (DeserializationListener)
6019       DeserializationListener->MacroDefinitionRead(PPID, MD);
6020 
6021     return MD;
6022   }
6023 
6024   case PPD_INCLUSION_DIRECTIVE: {
6025     const char *FullFileNameStart = Blob.data() + Record[0];
6026     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6027     const FileEntry *File = nullptr;
6028     if (!FullFileName.empty())
6029       if (auto FE = PP.getFileManager().getFile(FullFileName))
6030         File = *FE;
6031 
6032     // FIXME: Stable encoding
6033     InclusionDirective::InclusionKind Kind
6034       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6035     InclusionDirective *ID
6036       = new (PPRec) InclusionDirective(PPRec, Kind,
6037                                        StringRef(Blob.data(), Record[0]),
6038                                        Record[1], Record[3],
6039                                        File,
6040                                        Range);
6041     return ID;
6042   }
6043   }
6044 
6045   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6046 }
6047 
6048 /// Find the next module that contains entities and return the ID
6049 /// of the first entry.
6050 ///
6051 /// \param SLocMapI points at a chunk of a module that contains no
6052 /// preprocessed entities or the entities it contains are not the ones we are
6053 /// looking for.
6054 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6055                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6056   ++SLocMapI;
6057   for (GlobalSLocOffsetMapType::const_iterator
6058          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6059     ModuleFile &M = *SLocMapI->second;
6060     if (M.NumPreprocessedEntities)
6061       return M.BasePreprocessedEntityID;
6062   }
6063 
6064   return getTotalNumPreprocessedEntities();
6065 }
6066 
6067 namespace {
6068 
6069 struct PPEntityComp {
6070   const ASTReader &Reader;
6071   ModuleFile &M;
6072 
6073   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6074 
6075   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6076     SourceLocation LHS = getLoc(L);
6077     SourceLocation RHS = getLoc(R);
6078     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6079   }
6080 
6081   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6082     SourceLocation LHS = getLoc(L);
6083     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6084   }
6085 
6086   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6087     SourceLocation RHS = getLoc(R);
6088     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6089   }
6090 
6091   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6092     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6093   }
6094 };
6095 
6096 } // namespace
6097 
6098 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6099                                                        bool EndsAfter) const {
6100   if (SourceMgr.isLocalSourceLocation(Loc))
6101     return getTotalNumPreprocessedEntities();
6102 
6103   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6104       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6105   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6106          "Corrupted global sloc offset map");
6107 
6108   if (SLocMapI->second->NumPreprocessedEntities == 0)
6109     return findNextPreprocessedEntity(SLocMapI);
6110 
6111   ModuleFile &M = *SLocMapI->second;
6112 
6113   using pp_iterator = const PPEntityOffset *;
6114 
6115   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6116   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6117 
6118   size_t Count = M.NumPreprocessedEntities;
6119   size_t Half;
6120   pp_iterator First = pp_begin;
6121   pp_iterator PPI;
6122 
6123   if (EndsAfter) {
6124     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6125                            PPEntityComp(*this, M));
6126   } else {
6127     // Do a binary search manually instead of using std::lower_bound because
6128     // The end locations of entities may be unordered (when a macro expansion
6129     // is inside another macro argument), but for this case it is not important
6130     // whether we get the first macro expansion or its containing macro.
6131     while (Count > 0) {
6132       Half = Count / 2;
6133       PPI = First;
6134       std::advance(PPI, Half);
6135       if (SourceMgr.isBeforeInTranslationUnit(
6136               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6137         First = PPI;
6138         ++First;
6139         Count = Count - Half - 1;
6140       } else
6141         Count = Half;
6142     }
6143   }
6144 
6145   if (PPI == pp_end)
6146     return findNextPreprocessedEntity(SLocMapI);
6147 
6148   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6149 }
6150 
6151 /// Returns a pair of [Begin, End) indices of preallocated
6152 /// preprocessed entities that \arg Range encompasses.
6153 std::pair<unsigned, unsigned>
6154     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6155   if (Range.isInvalid())
6156     return std::make_pair(0,0);
6157   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6158 
6159   PreprocessedEntityID BeginID =
6160       findPreprocessedEntity(Range.getBegin(), false);
6161   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6162   return std::make_pair(BeginID, EndID);
6163 }
6164 
6165 /// Optionally returns true or false if the preallocated preprocessed
6166 /// entity with index \arg Index came from file \arg FID.
6167 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6168                                                              FileID FID) {
6169   if (FID.isInvalid())
6170     return false;
6171 
6172   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6173   ModuleFile &M = *PPInfo.first;
6174   unsigned LocalIndex = PPInfo.second;
6175   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6176 
6177   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6178   if (Loc.isInvalid())
6179     return false;
6180 
6181   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6182     return true;
6183   else
6184     return false;
6185 }
6186 
6187 namespace {
6188 
6189   /// Visitor used to search for information about a header file.
6190   class HeaderFileInfoVisitor {
6191     const FileEntry *FE;
6192     Optional<HeaderFileInfo> HFI;
6193 
6194   public:
6195     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6196 
6197     bool operator()(ModuleFile &M) {
6198       HeaderFileInfoLookupTable *Table
6199         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6200       if (!Table)
6201         return false;
6202 
6203       // Look in the on-disk hash table for an entry for this file name.
6204       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6205       if (Pos == Table->end())
6206         return false;
6207 
6208       HFI = *Pos;
6209       return true;
6210     }
6211 
6212     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6213   };
6214 
6215 } // namespace
6216 
6217 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6218   HeaderFileInfoVisitor Visitor(FE);
6219   ModuleMgr.visit(Visitor);
6220   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6221     return *HFI;
6222 
6223   return HeaderFileInfo();
6224 }
6225 
6226 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6227   using DiagState = DiagnosticsEngine::DiagState;
6228   SmallVector<DiagState *, 32> DiagStates;
6229 
6230   for (ModuleFile &F : ModuleMgr) {
6231     unsigned Idx = 0;
6232     auto &Record = F.PragmaDiagMappings;
6233     if (Record.empty())
6234       continue;
6235 
6236     DiagStates.clear();
6237 
6238     auto ReadDiagState =
6239         [&](const DiagState &BasedOn, SourceLocation Loc,
6240             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6241       unsigned BackrefID = Record[Idx++];
6242       if (BackrefID != 0)
6243         return DiagStates[BackrefID - 1];
6244 
6245       // A new DiagState was created here.
6246       Diag.DiagStates.push_back(BasedOn);
6247       DiagState *NewState = &Diag.DiagStates.back();
6248       DiagStates.push_back(NewState);
6249       unsigned Size = Record[Idx++];
6250       assert(Idx + Size * 2 <= Record.size() &&
6251              "Invalid data, not enough diag/map pairs");
6252       while (Size--) {
6253         unsigned DiagID = Record[Idx++];
6254         DiagnosticMapping NewMapping =
6255             DiagnosticMapping::deserialize(Record[Idx++]);
6256         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6257           continue;
6258 
6259         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6260 
6261         // If this mapping was specified as a warning but the severity was
6262         // upgraded due to diagnostic settings, simulate the current diagnostic
6263         // settings (and use a warning).
6264         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6265           NewMapping.setSeverity(diag::Severity::Warning);
6266           NewMapping.setUpgradedFromWarning(false);
6267         }
6268 
6269         Mapping = NewMapping;
6270       }
6271       return NewState;
6272     };
6273 
6274     // Read the first state.
6275     DiagState *FirstState;
6276     if (F.Kind == MK_ImplicitModule) {
6277       // Implicitly-built modules are reused with different diagnostic
6278       // settings.  Use the initial diagnostic state from Diag to simulate this
6279       // compilation's diagnostic settings.
6280       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6281       DiagStates.push_back(FirstState);
6282 
6283       // Skip the initial diagnostic state from the serialized module.
6284       assert(Record[1] == 0 &&
6285              "Invalid data, unexpected backref in initial state");
6286       Idx = 3 + Record[2] * 2;
6287       assert(Idx < Record.size() &&
6288              "Invalid data, not enough state change pairs in initial state");
6289     } else if (F.isModule()) {
6290       // For an explicit module, preserve the flags from the module build
6291       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6292       // -Wblah flags.
6293       unsigned Flags = Record[Idx++];
6294       DiagState Initial;
6295       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6296       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6297       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6298       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6299       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6300       Initial.ExtBehavior = (diag::Severity)Flags;
6301       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6302 
6303       assert(F.OriginalSourceFileID.isValid());
6304 
6305       // Set up the root buffer of the module to start with the initial
6306       // diagnostic state of the module itself, to cover files that contain no
6307       // explicit transitions (for which we did not serialize anything).
6308       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6309           .StateTransitions.push_back({FirstState, 0});
6310     } else {
6311       // For prefix ASTs, start with whatever the user configured on the
6312       // command line.
6313       Idx++; // Skip flags.
6314       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6315                                  SourceLocation(), false);
6316     }
6317 
6318     // Read the state transitions.
6319     unsigned NumLocations = Record[Idx++];
6320     while (NumLocations--) {
6321       assert(Idx < Record.size() &&
6322              "Invalid data, missing pragma diagnostic states");
6323       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6324       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6325       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6326       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6327       unsigned Transitions = Record[Idx++];
6328 
6329       // Note that we don't need to set up Parent/ParentOffset here, because
6330       // we won't be changing the diagnostic state within imported FileIDs
6331       // (other than perhaps appending to the main source file, which has no
6332       // parent).
6333       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6334       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6335       for (unsigned I = 0; I != Transitions; ++I) {
6336         unsigned Offset = Record[Idx++];
6337         auto *State =
6338             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6339         F.StateTransitions.push_back({State, Offset});
6340       }
6341     }
6342 
6343     // Read the final state.
6344     assert(Idx < Record.size() &&
6345            "Invalid data, missing final pragma diagnostic state");
6346     SourceLocation CurStateLoc =
6347         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6348     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6349 
6350     if (!F.isModule()) {
6351       Diag.DiagStatesByLoc.CurDiagState = CurState;
6352       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6353 
6354       // Preserve the property that the imaginary root file describes the
6355       // current state.
6356       FileID NullFile;
6357       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6358       if (T.empty())
6359         T.push_back({CurState, 0});
6360       else
6361         T[0].State = CurState;
6362     }
6363 
6364     // Don't try to read these mappings again.
6365     Record.clear();
6366   }
6367 }
6368 
6369 /// Get the correct cursor and offset for loading a type.
6370 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6371   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6372   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6373   ModuleFile *M = I->second;
6374   return RecordLocation(
6375       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6376              M->DeclsBlockStartOffset);
6377 }
6378 
6379 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6380   switch (code) {
6381 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6382   case TYPE_##CODE_ID: return Type::CLASS_ID;
6383 #include "clang/Serialization/TypeBitCodes.def"
6384   default: return llvm::None;
6385   }
6386 }
6387 
6388 /// Read and return the type with the given index..
6389 ///
6390 /// The index is the type ID, shifted and minus the number of predefs. This
6391 /// routine actually reads the record corresponding to the type at the given
6392 /// location. It is a helper routine for GetType, which deals with reading type
6393 /// IDs.
6394 QualType ASTReader::readTypeRecord(unsigned Index) {
6395   assert(ContextObj && "reading type with no AST context");
6396   ASTContext &Context = *ContextObj;
6397   RecordLocation Loc = TypeCursorForIndex(Index);
6398   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6399 
6400   // Keep track of where we are in the stream, then jump back there
6401   // after reading this type.
6402   SavedStreamPosition SavedPosition(DeclsCursor);
6403 
6404   ReadingKindTracker ReadingKind(Read_Type, *this);
6405 
6406   // Note that we are loading a type record.
6407   Deserializing AType(this);
6408 
6409   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6410     Error(std::move(Err));
6411     return QualType();
6412   }
6413   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6414   if (!RawCode) {
6415     Error(RawCode.takeError());
6416     return QualType();
6417   }
6418 
6419   ASTRecordReader Record(*this, *Loc.F);
6420   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6421   if (!Code) {
6422     Error(Code.takeError());
6423     return QualType();
6424   }
6425   if (Code.get() == TYPE_EXT_QUAL) {
6426     QualType baseType = Record.readQualType();
6427     Qualifiers quals = Record.readQualifiers();
6428     return Context.getQualifiedType(baseType, quals);
6429   }
6430 
6431   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6432   if (!maybeClass) {
6433     Error("Unexpected code for type");
6434     return QualType();
6435   }
6436 
6437   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6438   return TypeReader.read(*maybeClass);
6439 }
6440 
6441 namespace clang {
6442 
6443 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6444   ASTRecordReader &Reader;
6445 
6446   SourceLocation readSourceLocation() {
6447     return Reader.readSourceLocation();
6448   }
6449 
6450   TypeSourceInfo *GetTypeSourceInfo() {
6451     return Reader.readTypeSourceInfo();
6452   }
6453 
6454   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6455     return Reader.readNestedNameSpecifierLoc();
6456   }
6457 
6458   Attr *ReadAttr() {
6459     return Reader.readAttr();
6460   }
6461 
6462 public:
6463   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6464 
6465   // We want compile-time assurance that we've enumerated all of
6466   // these, so unfortunately we have to declare them first, then
6467   // define them out-of-line.
6468 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6469 #define TYPELOC(CLASS, PARENT) \
6470   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6471 #include "clang/AST/TypeLocNodes.def"
6472 
6473   void VisitFunctionTypeLoc(FunctionTypeLoc);
6474   void VisitArrayTypeLoc(ArrayTypeLoc);
6475 };
6476 
6477 } // namespace clang
6478 
6479 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6480   // nothing to do
6481 }
6482 
6483 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6484   TL.setBuiltinLoc(readSourceLocation());
6485   if (TL.needsExtraLocalData()) {
6486     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6487     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6488     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6489     TL.setModeAttr(Reader.readInt());
6490   }
6491 }
6492 
6493 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6494   TL.setNameLoc(readSourceLocation());
6495 }
6496 
6497 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6498   TL.setStarLoc(readSourceLocation());
6499 }
6500 
6501 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6502   // nothing to do
6503 }
6504 
6505 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6506   // nothing to do
6507 }
6508 
6509 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6510   TL.setExpansionLoc(readSourceLocation());
6511 }
6512 
6513 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6514   TL.setCaretLoc(readSourceLocation());
6515 }
6516 
6517 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6518   TL.setAmpLoc(readSourceLocation());
6519 }
6520 
6521 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6522   TL.setAmpAmpLoc(readSourceLocation());
6523 }
6524 
6525 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6526   TL.setStarLoc(readSourceLocation());
6527   TL.setClassTInfo(GetTypeSourceInfo());
6528 }
6529 
6530 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6531   TL.setLBracketLoc(readSourceLocation());
6532   TL.setRBracketLoc(readSourceLocation());
6533   if (Reader.readBool())
6534     TL.setSizeExpr(Reader.readExpr());
6535   else
6536     TL.setSizeExpr(nullptr);
6537 }
6538 
6539 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6540   VisitArrayTypeLoc(TL);
6541 }
6542 
6543 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6544   VisitArrayTypeLoc(TL);
6545 }
6546 
6547 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6548   VisitArrayTypeLoc(TL);
6549 }
6550 
6551 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6552                                             DependentSizedArrayTypeLoc TL) {
6553   VisitArrayTypeLoc(TL);
6554 }
6555 
6556 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6557     DependentAddressSpaceTypeLoc TL) {
6558 
6559     TL.setAttrNameLoc(readSourceLocation());
6560     TL.setAttrOperandParensRange(Reader.readSourceRange());
6561     TL.setAttrExprOperand(Reader.readExpr());
6562 }
6563 
6564 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6565                                         DependentSizedExtVectorTypeLoc TL) {
6566   TL.setNameLoc(readSourceLocation());
6567 }
6568 
6569 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6570   TL.setNameLoc(readSourceLocation());
6571 }
6572 
6573 void TypeLocReader::VisitDependentVectorTypeLoc(
6574     DependentVectorTypeLoc TL) {
6575   TL.setNameLoc(readSourceLocation());
6576 }
6577 
6578 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6579   TL.setNameLoc(readSourceLocation());
6580 }
6581 
6582 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6583   TL.setAttrNameLoc(readSourceLocation());
6584   TL.setAttrOperandParensRange(Reader.readSourceRange());
6585   TL.setAttrRowOperand(Reader.readExpr());
6586   TL.setAttrColumnOperand(Reader.readExpr());
6587 }
6588 
6589 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6590     DependentSizedMatrixTypeLoc TL) {
6591   TL.setAttrNameLoc(readSourceLocation());
6592   TL.setAttrOperandParensRange(Reader.readSourceRange());
6593   TL.setAttrRowOperand(Reader.readExpr());
6594   TL.setAttrColumnOperand(Reader.readExpr());
6595 }
6596 
6597 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6598   TL.setLocalRangeBegin(readSourceLocation());
6599   TL.setLParenLoc(readSourceLocation());
6600   TL.setRParenLoc(readSourceLocation());
6601   TL.setExceptionSpecRange(Reader.readSourceRange());
6602   TL.setLocalRangeEnd(readSourceLocation());
6603   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6604     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6605   }
6606 }
6607 
6608 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6609   VisitFunctionTypeLoc(TL);
6610 }
6611 
6612 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6613   VisitFunctionTypeLoc(TL);
6614 }
6615 
6616 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6617   TL.setNameLoc(readSourceLocation());
6618 }
6619 
6620 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6621   TL.setNameLoc(readSourceLocation());
6622 }
6623 
6624 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6625   TL.setTypeofLoc(readSourceLocation());
6626   TL.setLParenLoc(readSourceLocation());
6627   TL.setRParenLoc(readSourceLocation());
6628 }
6629 
6630 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6631   TL.setTypeofLoc(readSourceLocation());
6632   TL.setLParenLoc(readSourceLocation());
6633   TL.setRParenLoc(readSourceLocation());
6634   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6635 }
6636 
6637 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6638   TL.setNameLoc(readSourceLocation());
6639 }
6640 
6641 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6642   TL.setKWLoc(readSourceLocation());
6643   TL.setLParenLoc(readSourceLocation());
6644   TL.setRParenLoc(readSourceLocation());
6645   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6646 }
6647 
6648 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6649   TL.setNameLoc(readSourceLocation());
6650   if (Reader.readBool()) {
6651     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6652     TL.setTemplateKWLoc(readSourceLocation());
6653     TL.setConceptNameLoc(readSourceLocation());
6654     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6655     TL.setLAngleLoc(readSourceLocation());
6656     TL.setRAngleLoc(readSourceLocation());
6657     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6658       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6659                               TL.getTypePtr()->getArg(i).getKind()));
6660   }
6661 }
6662 
6663 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6664     DeducedTemplateSpecializationTypeLoc TL) {
6665   TL.setTemplateNameLoc(readSourceLocation());
6666 }
6667 
6668 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6669   TL.setNameLoc(readSourceLocation());
6670 }
6671 
6672 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6673   TL.setNameLoc(readSourceLocation());
6674 }
6675 
6676 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6677   TL.setAttr(ReadAttr());
6678 }
6679 
6680 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6681   TL.setNameLoc(readSourceLocation());
6682 }
6683 
6684 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6685                                             SubstTemplateTypeParmTypeLoc TL) {
6686   TL.setNameLoc(readSourceLocation());
6687 }
6688 
6689 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6690                                           SubstTemplateTypeParmPackTypeLoc TL) {
6691   TL.setNameLoc(readSourceLocation());
6692 }
6693 
6694 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6695                                            TemplateSpecializationTypeLoc TL) {
6696   TL.setTemplateKeywordLoc(readSourceLocation());
6697   TL.setTemplateNameLoc(readSourceLocation());
6698   TL.setLAngleLoc(readSourceLocation());
6699   TL.setRAngleLoc(readSourceLocation());
6700   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6701     TL.setArgLocInfo(
6702         i,
6703         Reader.readTemplateArgumentLocInfo(
6704           TL.getTypePtr()->getArg(i).getKind()));
6705 }
6706 
6707 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6708   TL.setLParenLoc(readSourceLocation());
6709   TL.setRParenLoc(readSourceLocation());
6710 }
6711 
6712 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6713   TL.setElaboratedKeywordLoc(readSourceLocation());
6714   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6715 }
6716 
6717 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6718   TL.setNameLoc(readSourceLocation());
6719 }
6720 
6721 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6722   TL.setElaboratedKeywordLoc(readSourceLocation());
6723   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6724   TL.setNameLoc(readSourceLocation());
6725 }
6726 
6727 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6728        DependentTemplateSpecializationTypeLoc TL) {
6729   TL.setElaboratedKeywordLoc(readSourceLocation());
6730   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6731   TL.setTemplateKeywordLoc(readSourceLocation());
6732   TL.setTemplateNameLoc(readSourceLocation());
6733   TL.setLAngleLoc(readSourceLocation());
6734   TL.setRAngleLoc(readSourceLocation());
6735   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6736     TL.setArgLocInfo(
6737         I,
6738         Reader.readTemplateArgumentLocInfo(
6739             TL.getTypePtr()->getArg(I).getKind()));
6740 }
6741 
6742 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6743   TL.setEllipsisLoc(readSourceLocation());
6744 }
6745 
6746 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6747   TL.setNameLoc(readSourceLocation());
6748 }
6749 
6750 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6751   if (TL.getNumProtocols()) {
6752     TL.setProtocolLAngleLoc(readSourceLocation());
6753     TL.setProtocolRAngleLoc(readSourceLocation());
6754   }
6755   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6756     TL.setProtocolLoc(i, readSourceLocation());
6757 }
6758 
6759 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6760   TL.setHasBaseTypeAsWritten(Reader.readBool());
6761   TL.setTypeArgsLAngleLoc(readSourceLocation());
6762   TL.setTypeArgsRAngleLoc(readSourceLocation());
6763   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6764     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6765   TL.setProtocolLAngleLoc(readSourceLocation());
6766   TL.setProtocolRAngleLoc(readSourceLocation());
6767   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6768     TL.setProtocolLoc(i, readSourceLocation());
6769 }
6770 
6771 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6772   TL.setStarLoc(readSourceLocation());
6773 }
6774 
6775 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6776   TL.setKWLoc(readSourceLocation());
6777   TL.setLParenLoc(readSourceLocation());
6778   TL.setRParenLoc(readSourceLocation());
6779 }
6780 
6781 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6782   TL.setKWLoc(readSourceLocation());
6783 }
6784 
6785 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6786   TL.setNameLoc(readSourceLocation());
6787 }
6788 void TypeLocReader::VisitDependentExtIntTypeLoc(
6789     clang::DependentExtIntTypeLoc TL) {
6790   TL.setNameLoc(readSourceLocation());
6791 }
6792 
6793 
6794 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6795   TypeLocReader TLR(*this);
6796   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6797     TLR.Visit(TL);
6798 }
6799 
6800 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6801   QualType InfoTy = readType();
6802   if (InfoTy.isNull())
6803     return nullptr;
6804 
6805   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6806   readTypeLoc(TInfo->getTypeLoc());
6807   return TInfo;
6808 }
6809 
6810 QualType ASTReader::GetType(TypeID ID) {
6811   assert(ContextObj && "reading type with no AST context");
6812   ASTContext &Context = *ContextObj;
6813 
6814   unsigned FastQuals = ID & Qualifiers::FastMask;
6815   unsigned Index = ID >> Qualifiers::FastWidth;
6816 
6817   if (Index < NUM_PREDEF_TYPE_IDS) {
6818     QualType T;
6819     switch ((PredefinedTypeIDs)Index) {
6820     case PREDEF_TYPE_NULL_ID:
6821       return QualType();
6822     case PREDEF_TYPE_VOID_ID:
6823       T = Context.VoidTy;
6824       break;
6825     case PREDEF_TYPE_BOOL_ID:
6826       T = Context.BoolTy;
6827       break;
6828     case PREDEF_TYPE_CHAR_U_ID:
6829     case PREDEF_TYPE_CHAR_S_ID:
6830       // FIXME: Check that the signedness of CharTy is correct!
6831       T = Context.CharTy;
6832       break;
6833     case PREDEF_TYPE_UCHAR_ID:
6834       T = Context.UnsignedCharTy;
6835       break;
6836     case PREDEF_TYPE_USHORT_ID:
6837       T = Context.UnsignedShortTy;
6838       break;
6839     case PREDEF_TYPE_UINT_ID:
6840       T = Context.UnsignedIntTy;
6841       break;
6842     case PREDEF_TYPE_ULONG_ID:
6843       T = Context.UnsignedLongTy;
6844       break;
6845     case PREDEF_TYPE_ULONGLONG_ID:
6846       T = Context.UnsignedLongLongTy;
6847       break;
6848     case PREDEF_TYPE_UINT128_ID:
6849       T = Context.UnsignedInt128Ty;
6850       break;
6851     case PREDEF_TYPE_SCHAR_ID:
6852       T = Context.SignedCharTy;
6853       break;
6854     case PREDEF_TYPE_WCHAR_ID:
6855       T = Context.WCharTy;
6856       break;
6857     case PREDEF_TYPE_SHORT_ID:
6858       T = Context.ShortTy;
6859       break;
6860     case PREDEF_TYPE_INT_ID:
6861       T = Context.IntTy;
6862       break;
6863     case PREDEF_TYPE_LONG_ID:
6864       T = Context.LongTy;
6865       break;
6866     case PREDEF_TYPE_LONGLONG_ID:
6867       T = Context.LongLongTy;
6868       break;
6869     case PREDEF_TYPE_INT128_ID:
6870       T = Context.Int128Ty;
6871       break;
6872     case PREDEF_TYPE_BFLOAT16_ID:
6873       T = Context.BFloat16Ty;
6874       break;
6875     case PREDEF_TYPE_HALF_ID:
6876       T = Context.HalfTy;
6877       break;
6878     case PREDEF_TYPE_FLOAT_ID:
6879       T = Context.FloatTy;
6880       break;
6881     case PREDEF_TYPE_DOUBLE_ID:
6882       T = Context.DoubleTy;
6883       break;
6884     case PREDEF_TYPE_LONGDOUBLE_ID:
6885       T = Context.LongDoubleTy;
6886       break;
6887     case PREDEF_TYPE_SHORT_ACCUM_ID:
6888       T = Context.ShortAccumTy;
6889       break;
6890     case PREDEF_TYPE_ACCUM_ID:
6891       T = Context.AccumTy;
6892       break;
6893     case PREDEF_TYPE_LONG_ACCUM_ID:
6894       T = Context.LongAccumTy;
6895       break;
6896     case PREDEF_TYPE_USHORT_ACCUM_ID:
6897       T = Context.UnsignedShortAccumTy;
6898       break;
6899     case PREDEF_TYPE_UACCUM_ID:
6900       T = Context.UnsignedAccumTy;
6901       break;
6902     case PREDEF_TYPE_ULONG_ACCUM_ID:
6903       T = Context.UnsignedLongAccumTy;
6904       break;
6905     case PREDEF_TYPE_SHORT_FRACT_ID:
6906       T = Context.ShortFractTy;
6907       break;
6908     case PREDEF_TYPE_FRACT_ID:
6909       T = Context.FractTy;
6910       break;
6911     case PREDEF_TYPE_LONG_FRACT_ID:
6912       T = Context.LongFractTy;
6913       break;
6914     case PREDEF_TYPE_USHORT_FRACT_ID:
6915       T = Context.UnsignedShortFractTy;
6916       break;
6917     case PREDEF_TYPE_UFRACT_ID:
6918       T = Context.UnsignedFractTy;
6919       break;
6920     case PREDEF_TYPE_ULONG_FRACT_ID:
6921       T = Context.UnsignedLongFractTy;
6922       break;
6923     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6924       T = Context.SatShortAccumTy;
6925       break;
6926     case PREDEF_TYPE_SAT_ACCUM_ID:
6927       T = Context.SatAccumTy;
6928       break;
6929     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6930       T = Context.SatLongAccumTy;
6931       break;
6932     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6933       T = Context.SatUnsignedShortAccumTy;
6934       break;
6935     case PREDEF_TYPE_SAT_UACCUM_ID:
6936       T = Context.SatUnsignedAccumTy;
6937       break;
6938     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6939       T = Context.SatUnsignedLongAccumTy;
6940       break;
6941     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6942       T = Context.SatShortFractTy;
6943       break;
6944     case PREDEF_TYPE_SAT_FRACT_ID:
6945       T = Context.SatFractTy;
6946       break;
6947     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6948       T = Context.SatLongFractTy;
6949       break;
6950     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6951       T = Context.SatUnsignedShortFractTy;
6952       break;
6953     case PREDEF_TYPE_SAT_UFRACT_ID:
6954       T = Context.SatUnsignedFractTy;
6955       break;
6956     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6957       T = Context.SatUnsignedLongFractTy;
6958       break;
6959     case PREDEF_TYPE_FLOAT16_ID:
6960       T = Context.Float16Ty;
6961       break;
6962     case PREDEF_TYPE_FLOAT128_ID:
6963       T = Context.Float128Ty;
6964       break;
6965     case PREDEF_TYPE_OVERLOAD_ID:
6966       T = Context.OverloadTy;
6967       break;
6968     case PREDEF_TYPE_BOUND_MEMBER:
6969       T = Context.BoundMemberTy;
6970       break;
6971     case PREDEF_TYPE_PSEUDO_OBJECT:
6972       T = Context.PseudoObjectTy;
6973       break;
6974     case PREDEF_TYPE_DEPENDENT_ID:
6975       T = Context.DependentTy;
6976       break;
6977     case PREDEF_TYPE_UNKNOWN_ANY:
6978       T = Context.UnknownAnyTy;
6979       break;
6980     case PREDEF_TYPE_NULLPTR_ID:
6981       T = Context.NullPtrTy;
6982       break;
6983     case PREDEF_TYPE_CHAR8_ID:
6984       T = Context.Char8Ty;
6985       break;
6986     case PREDEF_TYPE_CHAR16_ID:
6987       T = Context.Char16Ty;
6988       break;
6989     case PREDEF_TYPE_CHAR32_ID:
6990       T = Context.Char32Ty;
6991       break;
6992     case PREDEF_TYPE_OBJC_ID:
6993       T = Context.ObjCBuiltinIdTy;
6994       break;
6995     case PREDEF_TYPE_OBJC_CLASS:
6996       T = Context.ObjCBuiltinClassTy;
6997       break;
6998     case PREDEF_TYPE_OBJC_SEL:
6999       T = Context.ObjCBuiltinSelTy;
7000       break;
7001 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7002     case PREDEF_TYPE_##Id##_ID: \
7003       T = Context.SingletonId; \
7004       break;
7005 #include "clang/Basic/OpenCLImageTypes.def"
7006 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7007     case PREDEF_TYPE_##Id##_ID: \
7008       T = Context.Id##Ty; \
7009       break;
7010 #include "clang/Basic/OpenCLExtensionTypes.def"
7011     case PREDEF_TYPE_SAMPLER_ID:
7012       T = Context.OCLSamplerTy;
7013       break;
7014     case PREDEF_TYPE_EVENT_ID:
7015       T = Context.OCLEventTy;
7016       break;
7017     case PREDEF_TYPE_CLK_EVENT_ID:
7018       T = Context.OCLClkEventTy;
7019       break;
7020     case PREDEF_TYPE_QUEUE_ID:
7021       T = Context.OCLQueueTy;
7022       break;
7023     case PREDEF_TYPE_RESERVE_ID_ID:
7024       T = Context.OCLReserveIDTy;
7025       break;
7026     case PREDEF_TYPE_AUTO_DEDUCT:
7027       T = Context.getAutoDeductType();
7028       break;
7029     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7030       T = Context.getAutoRRefDeductType();
7031       break;
7032     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7033       T = Context.ARCUnbridgedCastTy;
7034       break;
7035     case PREDEF_TYPE_BUILTIN_FN:
7036       T = Context.BuiltinFnTy;
7037       break;
7038     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7039       T = Context.IncompleteMatrixIdxTy;
7040       break;
7041     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7042       T = Context.OMPArraySectionTy;
7043       break;
7044     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7045       T = Context.OMPArraySectionTy;
7046       break;
7047     case PREDEF_TYPE_OMP_ITERATOR:
7048       T = Context.OMPIteratorTy;
7049       break;
7050 #define SVE_TYPE(Name, Id, SingletonId) \
7051     case PREDEF_TYPE_##Id##_ID: \
7052       T = Context.SingletonId; \
7053       break;
7054 #include "clang/Basic/AArch64SVEACLETypes.def"
7055 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7056     case PREDEF_TYPE_##Id##_ID: \
7057       T = Context.Id##Ty; \
7058       break;
7059 #include "clang/Basic/PPCTypes.def"
7060 #define RVV_TYPE(Name, Id, SingletonId) \
7061     case PREDEF_TYPE_##Id##_ID: \
7062       T = Context.SingletonId; \
7063       break;
7064 #include "clang/Basic/RISCVVTypes.def"
7065     }
7066 
7067     assert(!T.isNull() && "Unknown predefined type");
7068     return T.withFastQualifiers(FastQuals);
7069   }
7070 
7071   Index -= NUM_PREDEF_TYPE_IDS;
7072   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7073   if (TypesLoaded[Index].isNull()) {
7074     TypesLoaded[Index] = readTypeRecord(Index);
7075     if (TypesLoaded[Index].isNull())
7076       return QualType();
7077 
7078     TypesLoaded[Index]->setFromAST();
7079     if (DeserializationListener)
7080       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7081                                         TypesLoaded[Index]);
7082   }
7083 
7084   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7085 }
7086 
7087 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7088   return GetType(getGlobalTypeID(F, LocalID));
7089 }
7090 
7091 serialization::TypeID
7092 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7093   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7094   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7095 
7096   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7097     return LocalID;
7098 
7099   if (!F.ModuleOffsetMap.empty())
7100     ReadModuleOffsetMap(F);
7101 
7102   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7103     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7104   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7105 
7106   unsigned GlobalIndex = LocalIndex + I->second;
7107   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7108 }
7109 
7110 TemplateArgumentLocInfo
7111 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7112   switch (Kind) {
7113   case TemplateArgument::Expression:
7114     return readExpr();
7115   case TemplateArgument::Type:
7116     return readTypeSourceInfo();
7117   case TemplateArgument::Template: {
7118     NestedNameSpecifierLoc QualifierLoc =
7119       readNestedNameSpecifierLoc();
7120     SourceLocation TemplateNameLoc = readSourceLocation();
7121     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7122                                    TemplateNameLoc, SourceLocation());
7123   }
7124   case TemplateArgument::TemplateExpansion: {
7125     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7126     SourceLocation TemplateNameLoc = readSourceLocation();
7127     SourceLocation EllipsisLoc = readSourceLocation();
7128     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7129                                    TemplateNameLoc, EllipsisLoc);
7130   }
7131   case TemplateArgument::Null:
7132   case TemplateArgument::Integral:
7133   case TemplateArgument::Declaration:
7134   case TemplateArgument::NullPtr:
7135   case TemplateArgument::Pack:
7136     // FIXME: Is this right?
7137     return TemplateArgumentLocInfo();
7138   }
7139   llvm_unreachable("unexpected template argument loc");
7140 }
7141 
7142 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7143   TemplateArgument Arg = readTemplateArgument();
7144 
7145   if (Arg.getKind() == TemplateArgument::Expression) {
7146     if (readBool()) // bool InfoHasSameExpr.
7147       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7148   }
7149   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7150 }
7151 
7152 const ASTTemplateArgumentListInfo *
7153 ASTRecordReader::readASTTemplateArgumentListInfo() {
7154   SourceLocation LAngleLoc = readSourceLocation();
7155   SourceLocation RAngleLoc = readSourceLocation();
7156   unsigned NumArgsAsWritten = readInt();
7157   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7158   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7159     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7160   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7161 }
7162 
7163 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7164   return GetDecl(ID);
7165 }
7166 
7167 void ASTReader::CompleteRedeclChain(const Decl *D) {
7168   if (NumCurrentElementsDeserializing) {
7169     // We arrange to not care about the complete redeclaration chain while we're
7170     // deserializing. Just remember that the AST has marked this one as complete
7171     // but that it's not actually complete yet, so we know we still need to
7172     // complete it later.
7173     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7174     return;
7175   }
7176 
7177   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7178 
7179   // If this is a named declaration, complete it by looking it up
7180   // within its context.
7181   //
7182   // FIXME: Merging a function definition should merge
7183   // all mergeable entities within it.
7184   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7185       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7186     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7187       if (!getContext().getLangOpts().CPlusPlus &&
7188           isa<TranslationUnitDecl>(DC)) {
7189         // Outside of C++, we don't have a lookup table for the TU, so update
7190         // the identifier instead. (For C++ modules, we don't store decls
7191         // in the serialized identifier table, so we do the lookup in the TU.)
7192         auto *II = Name.getAsIdentifierInfo();
7193         assert(II && "non-identifier name in C?");
7194         if (II->isOutOfDate())
7195           updateOutOfDateIdentifier(*II);
7196       } else
7197         DC->lookup(Name);
7198     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7199       // Find all declarations of this kind from the relevant context.
7200       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7201         auto *DC = cast<DeclContext>(DCDecl);
7202         SmallVector<Decl*, 8> Decls;
7203         FindExternalLexicalDecls(
7204             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7205       }
7206     }
7207   }
7208 
7209   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7210     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7211   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7212     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7213   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7214     if (auto *Template = FD->getPrimaryTemplate())
7215       Template->LoadLazySpecializations();
7216   }
7217 }
7218 
7219 CXXCtorInitializer **
7220 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7221   RecordLocation Loc = getLocalBitOffset(Offset);
7222   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7223   SavedStreamPosition SavedPosition(Cursor);
7224   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7225     Error(std::move(Err));
7226     return nullptr;
7227   }
7228   ReadingKindTracker ReadingKind(Read_Decl, *this);
7229 
7230   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7231   if (!MaybeCode) {
7232     Error(MaybeCode.takeError());
7233     return nullptr;
7234   }
7235   unsigned Code = MaybeCode.get();
7236 
7237   ASTRecordReader Record(*this, *Loc.F);
7238   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7239   if (!MaybeRecCode) {
7240     Error(MaybeRecCode.takeError());
7241     return nullptr;
7242   }
7243   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7244     Error("malformed AST file: missing C++ ctor initializers");
7245     return nullptr;
7246   }
7247 
7248   return Record.readCXXCtorInitializers();
7249 }
7250 
7251 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7252   assert(ContextObj && "reading base specifiers with no AST context");
7253   ASTContext &Context = *ContextObj;
7254 
7255   RecordLocation Loc = getLocalBitOffset(Offset);
7256   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7257   SavedStreamPosition SavedPosition(Cursor);
7258   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7259     Error(std::move(Err));
7260     return nullptr;
7261   }
7262   ReadingKindTracker ReadingKind(Read_Decl, *this);
7263 
7264   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7265   if (!MaybeCode) {
7266     Error(MaybeCode.takeError());
7267     return nullptr;
7268   }
7269   unsigned Code = MaybeCode.get();
7270 
7271   ASTRecordReader Record(*this, *Loc.F);
7272   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7273   if (!MaybeRecCode) {
7274     Error(MaybeCode.takeError());
7275     return nullptr;
7276   }
7277   unsigned RecCode = MaybeRecCode.get();
7278 
7279   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7280     Error("malformed AST file: missing C++ base specifiers");
7281     return nullptr;
7282   }
7283 
7284   unsigned NumBases = Record.readInt();
7285   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7286   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7287   for (unsigned I = 0; I != NumBases; ++I)
7288     Bases[I] = Record.readCXXBaseSpecifier();
7289   return Bases;
7290 }
7291 
7292 serialization::DeclID
7293 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7294   if (LocalID < NUM_PREDEF_DECL_IDS)
7295     return LocalID;
7296 
7297   if (!F.ModuleOffsetMap.empty())
7298     ReadModuleOffsetMap(F);
7299 
7300   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7301     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7302   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7303 
7304   return LocalID + I->second;
7305 }
7306 
7307 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7308                                    ModuleFile &M) const {
7309   // Predefined decls aren't from any module.
7310   if (ID < NUM_PREDEF_DECL_IDS)
7311     return false;
7312 
7313   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7314          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7315 }
7316 
7317 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7318   if (!D->isFromASTFile())
7319     return nullptr;
7320   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7321   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7322   return I->second;
7323 }
7324 
7325 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7326   if (ID < NUM_PREDEF_DECL_IDS)
7327     return SourceLocation();
7328 
7329   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7330 
7331   if (Index > DeclsLoaded.size()) {
7332     Error("declaration ID out-of-range for AST file");
7333     return SourceLocation();
7334   }
7335 
7336   if (Decl *D = DeclsLoaded[Index])
7337     return D->getLocation();
7338 
7339   SourceLocation Loc;
7340   DeclCursorForID(ID, Loc);
7341   return Loc;
7342 }
7343 
7344 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7345   switch (ID) {
7346   case PREDEF_DECL_NULL_ID:
7347     return nullptr;
7348 
7349   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7350     return Context.getTranslationUnitDecl();
7351 
7352   case PREDEF_DECL_OBJC_ID_ID:
7353     return Context.getObjCIdDecl();
7354 
7355   case PREDEF_DECL_OBJC_SEL_ID:
7356     return Context.getObjCSelDecl();
7357 
7358   case PREDEF_DECL_OBJC_CLASS_ID:
7359     return Context.getObjCClassDecl();
7360 
7361   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7362     return Context.getObjCProtocolDecl();
7363 
7364   case PREDEF_DECL_INT_128_ID:
7365     return Context.getInt128Decl();
7366 
7367   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7368     return Context.getUInt128Decl();
7369 
7370   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7371     return Context.getObjCInstanceTypeDecl();
7372 
7373   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7374     return Context.getBuiltinVaListDecl();
7375 
7376   case PREDEF_DECL_VA_LIST_TAG:
7377     return Context.getVaListTagDecl();
7378 
7379   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7380     return Context.getBuiltinMSVaListDecl();
7381 
7382   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7383     return Context.getMSGuidTagDecl();
7384 
7385   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7386     return Context.getExternCContextDecl();
7387 
7388   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7389     return Context.getMakeIntegerSeqDecl();
7390 
7391   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7392     return Context.getCFConstantStringDecl();
7393 
7394   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7395     return Context.getCFConstantStringTagDecl();
7396 
7397   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7398     return Context.getTypePackElementDecl();
7399   }
7400   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7401 }
7402 
7403 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7404   assert(ContextObj && "reading decl with no AST context");
7405   if (ID < NUM_PREDEF_DECL_IDS) {
7406     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7407     if (D) {
7408       // Track that we have merged the declaration with ID \p ID into the
7409       // pre-existing predefined declaration \p D.
7410       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7411       if (Merged.empty())
7412         Merged.push_back(ID);
7413     }
7414     return D;
7415   }
7416 
7417   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7418 
7419   if (Index >= DeclsLoaded.size()) {
7420     assert(0 && "declaration ID out-of-range for AST file");
7421     Error("declaration ID out-of-range for AST file");
7422     return nullptr;
7423   }
7424 
7425   return DeclsLoaded[Index];
7426 }
7427 
7428 Decl *ASTReader::GetDecl(DeclID ID) {
7429   if (ID < NUM_PREDEF_DECL_IDS)
7430     return GetExistingDecl(ID);
7431 
7432   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7433 
7434   if (Index >= DeclsLoaded.size()) {
7435     assert(0 && "declaration ID out-of-range for AST file");
7436     Error("declaration ID out-of-range for AST file");
7437     return nullptr;
7438   }
7439 
7440   if (!DeclsLoaded[Index]) {
7441     ReadDeclRecord(ID);
7442     if (DeserializationListener)
7443       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7444   }
7445 
7446   return DeclsLoaded[Index];
7447 }
7448 
7449 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7450                                                   DeclID GlobalID) {
7451   if (GlobalID < NUM_PREDEF_DECL_IDS)
7452     return GlobalID;
7453 
7454   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7455   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7456   ModuleFile *Owner = I->second;
7457 
7458   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7459     = M.GlobalToLocalDeclIDs.find(Owner);
7460   if (Pos == M.GlobalToLocalDeclIDs.end())
7461     return 0;
7462 
7463   return GlobalID - Owner->BaseDeclID + Pos->second;
7464 }
7465 
7466 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7467                                             const RecordData &Record,
7468                                             unsigned &Idx) {
7469   if (Idx >= Record.size()) {
7470     Error("Corrupted AST file");
7471     return 0;
7472   }
7473 
7474   return getGlobalDeclID(F, Record[Idx++]);
7475 }
7476 
7477 /// Resolve the offset of a statement into a statement.
7478 ///
7479 /// This operation will read a new statement from the external
7480 /// source each time it is called, and is meant to be used via a
7481 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7482 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7483   // Switch case IDs are per Decl.
7484   ClearSwitchCaseIDs();
7485 
7486   // Offset here is a global offset across the entire chain.
7487   RecordLocation Loc = getLocalBitOffset(Offset);
7488   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7489     Error(std::move(Err));
7490     return nullptr;
7491   }
7492   assert(NumCurrentElementsDeserializing == 0 &&
7493          "should not be called while already deserializing");
7494   Deserializing D(this);
7495   return ReadStmtFromStream(*Loc.F);
7496 }
7497 
7498 void ASTReader::FindExternalLexicalDecls(
7499     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7500     SmallVectorImpl<Decl *> &Decls) {
7501   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7502 
7503   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7504     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7505     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7506       auto K = (Decl::Kind)+LexicalDecls[I];
7507       if (!IsKindWeWant(K))
7508         continue;
7509 
7510       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7511 
7512       // Don't add predefined declarations to the lexical context more
7513       // than once.
7514       if (ID < NUM_PREDEF_DECL_IDS) {
7515         if (PredefsVisited[ID])
7516           continue;
7517 
7518         PredefsVisited[ID] = true;
7519       }
7520 
7521       if (Decl *D = GetLocalDecl(*M, ID)) {
7522         assert(D->getKind() == K && "wrong kind for lexical decl");
7523         if (!DC->isDeclInLexicalTraversal(D))
7524           Decls.push_back(D);
7525       }
7526     }
7527   };
7528 
7529   if (isa<TranslationUnitDecl>(DC)) {
7530     for (auto Lexical : TULexicalDecls)
7531       Visit(Lexical.first, Lexical.second);
7532   } else {
7533     auto I = LexicalDecls.find(DC);
7534     if (I != LexicalDecls.end())
7535       Visit(I->second.first, I->second.second);
7536   }
7537 
7538   ++NumLexicalDeclContextsRead;
7539 }
7540 
7541 namespace {
7542 
7543 class DeclIDComp {
7544   ASTReader &Reader;
7545   ModuleFile &Mod;
7546 
7547 public:
7548   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7549 
7550   bool operator()(LocalDeclID L, LocalDeclID R) const {
7551     SourceLocation LHS = getLocation(L);
7552     SourceLocation RHS = getLocation(R);
7553     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7554   }
7555 
7556   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7557     SourceLocation RHS = getLocation(R);
7558     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7559   }
7560 
7561   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7562     SourceLocation LHS = getLocation(L);
7563     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7564   }
7565 
7566   SourceLocation getLocation(LocalDeclID ID) const {
7567     return Reader.getSourceManager().getFileLoc(
7568             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7569   }
7570 };
7571 
7572 } // namespace
7573 
7574 void ASTReader::FindFileRegionDecls(FileID File,
7575                                     unsigned Offset, unsigned Length,
7576                                     SmallVectorImpl<Decl *> &Decls) {
7577   SourceManager &SM = getSourceManager();
7578 
7579   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7580   if (I == FileDeclIDs.end())
7581     return;
7582 
7583   FileDeclsInfo &DInfo = I->second;
7584   if (DInfo.Decls.empty())
7585     return;
7586 
7587   SourceLocation
7588     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7589   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7590 
7591   DeclIDComp DIDComp(*this, *DInfo.Mod);
7592   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7593       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7594   if (BeginIt != DInfo.Decls.begin())
7595     --BeginIt;
7596 
7597   // If we are pointing at a top-level decl inside an objc container, we need
7598   // to backtrack until we find it otherwise we will fail to report that the
7599   // region overlaps with an objc container.
7600   while (BeginIt != DInfo.Decls.begin() &&
7601          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7602              ->isTopLevelDeclInObjCContainer())
7603     --BeginIt;
7604 
7605   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7606       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7607   if (EndIt != DInfo.Decls.end())
7608     ++EndIt;
7609 
7610   for (ArrayRef<serialization::LocalDeclID>::iterator
7611          DIt = BeginIt; DIt != EndIt; ++DIt)
7612     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7613 }
7614 
7615 bool
7616 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7617                                           DeclarationName Name) {
7618   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7619          "DeclContext has no visible decls in storage");
7620   if (!Name)
7621     return false;
7622 
7623   auto It = Lookups.find(DC);
7624   if (It == Lookups.end())
7625     return false;
7626 
7627   Deserializing LookupResults(this);
7628 
7629   // Load the list of declarations.
7630   SmallVector<NamedDecl *, 64> Decls;
7631   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7632   for (DeclID ID : It->second.Table.find(Name)) {
7633     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7634     if (ND->getDeclName() == Name && Found.insert(ND).second)
7635       Decls.push_back(ND);
7636   }
7637 
7638   ++NumVisibleDeclContextsRead;
7639   SetExternalVisibleDeclsForName(DC, Name, Decls);
7640   return !Decls.empty();
7641 }
7642 
7643 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7644   if (!DC->hasExternalVisibleStorage())
7645     return;
7646 
7647   auto It = Lookups.find(DC);
7648   assert(It != Lookups.end() &&
7649          "have external visible storage but no lookup tables");
7650 
7651   DeclsMap Decls;
7652 
7653   for (DeclID ID : It->second.Table.findAll()) {
7654     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7655     Decls[ND->getDeclName()].push_back(ND);
7656   }
7657 
7658   ++NumVisibleDeclContextsRead;
7659 
7660   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7661     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7662   }
7663   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7664 }
7665 
7666 const serialization::reader::DeclContextLookupTable *
7667 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7668   auto I = Lookups.find(Primary);
7669   return I == Lookups.end() ? nullptr : &I->second;
7670 }
7671 
7672 /// Under non-PCH compilation the consumer receives the objc methods
7673 /// before receiving the implementation, and codegen depends on this.
7674 /// We simulate this by deserializing and passing to consumer the methods of the
7675 /// implementation before passing the deserialized implementation decl.
7676 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7677                                        ASTConsumer *Consumer) {
7678   assert(ImplD && Consumer);
7679 
7680   for (auto *I : ImplD->methods())
7681     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7682 
7683   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7684 }
7685 
7686 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7687   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7688     PassObjCImplDeclToConsumer(ImplD, Consumer);
7689   else
7690     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7691 }
7692 
7693 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7694   this->Consumer = Consumer;
7695 
7696   if (Consumer)
7697     PassInterestingDeclsToConsumer();
7698 
7699   if (DeserializationListener)
7700     DeserializationListener->ReaderInitialized(this);
7701 }
7702 
7703 void ASTReader::PrintStats() {
7704   std::fprintf(stderr, "*** AST File Statistics:\n");
7705 
7706   unsigned NumTypesLoaded
7707     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7708                                       QualType());
7709   unsigned NumDeclsLoaded
7710     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7711                                       (Decl *)nullptr);
7712   unsigned NumIdentifiersLoaded
7713     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7714                                             IdentifiersLoaded.end(),
7715                                             (IdentifierInfo *)nullptr);
7716   unsigned NumMacrosLoaded
7717     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7718                                        MacrosLoaded.end(),
7719                                        (MacroInfo *)nullptr);
7720   unsigned NumSelectorsLoaded
7721     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7722                                           SelectorsLoaded.end(),
7723                                           Selector());
7724 
7725   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7726     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7727                  NumSLocEntriesRead, TotalNumSLocEntries,
7728                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7729   if (!TypesLoaded.empty())
7730     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7731                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7732                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7733   if (!DeclsLoaded.empty())
7734     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7735                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7736                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7737   if (!IdentifiersLoaded.empty())
7738     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7739                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7740                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7741   if (!MacrosLoaded.empty())
7742     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7743                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7744                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7745   if (!SelectorsLoaded.empty())
7746     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7747                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7748                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7749   if (TotalNumStatements)
7750     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7751                  NumStatementsRead, TotalNumStatements,
7752                  ((float)NumStatementsRead/TotalNumStatements * 100));
7753   if (TotalNumMacros)
7754     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7755                  NumMacrosRead, TotalNumMacros,
7756                  ((float)NumMacrosRead/TotalNumMacros * 100));
7757   if (TotalLexicalDeclContexts)
7758     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7759                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7760                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7761                   * 100));
7762   if (TotalVisibleDeclContexts)
7763     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7764                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7765                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7766                   * 100));
7767   if (TotalNumMethodPoolEntries)
7768     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7769                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7770                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7771                   * 100));
7772   if (NumMethodPoolLookups)
7773     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7774                  NumMethodPoolHits, NumMethodPoolLookups,
7775                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7776   if (NumMethodPoolTableLookups)
7777     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7778                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7779                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7780                   * 100.0));
7781   if (NumIdentifierLookupHits)
7782     std::fprintf(stderr,
7783                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7784                  NumIdentifierLookupHits, NumIdentifierLookups,
7785                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7786 
7787   if (GlobalIndex) {
7788     std::fprintf(stderr, "\n");
7789     GlobalIndex->printStats();
7790   }
7791 
7792   std::fprintf(stderr, "\n");
7793   dump();
7794   std::fprintf(stderr, "\n");
7795 }
7796 
7797 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7798 LLVM_DUMP_METHOD static void
7799 dumpModuleIDMap(StringRef Name,
7800                 const ContinuousRangeMap<Key, ModuleFile *,
7801                                          InitialCapacity> &Map) {
7802   if (Map.begin() == Map.end())
7803     return;
7804 
7805   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7806 
7807   llvm::errs() << Name << ":\n";
7808   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7809        I != IEnd; ++I) {
7810     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7811       << "\n";
7812   }
7813 }
7814 
7815 LLVM_DUMP_METHOD void ASTReader::dump() {
7816   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7817   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7818   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7819   dumpModuleIDMap("Global type map", GlobalTypeMap);
7820   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7821   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7822   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7823   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7824   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7825   dumpModuleIDMap("Global preprocessed entity map",
7826                   GlobalPreprocessedEntityMap);
7827 
7828   llvm::errs() << "\n*** PCH/Modules Loaded:";
7829   for (ModuleFile &M : ModuleMgr)
7830     M.dump();
7831 }
7832 
7833 /// Return the amount of memory used by memory buffers, breaking down
7834 /// by heap-backed versus mmap'ed memory.
7835 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7836   for (ModuleFile &I : ModuleMgr) {
7837     if (llvm::MemoryBuffer *buf = I.Buffer) {
7838       size_t bytes = buf->getBufferSize();
7839       switch (buf->getBufferKind()) {
7840         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7841           sizes.malloc_bytes += bytes;
7842           break;
7843         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7844           sizes.mmap_bytes += bytes;
7845           break;
7846       }
7847     }
7848   }
7849 }
7850 
7851 void ASTReader::InitializeSema(Sema &S) {
7852   SemaObj = &S;
7853   S.addExternalSource(this);
7854 
7855   // Makes sure any declarations that were deserialized "too early"
7856   // still get added to the identifier's declaration chains.
7857   for (uint64_t ID : PreloadedDeclIDs) {
7858     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7859     pushExternalDeclIntoScope(D, D->getDeclName());
7860   }
7861   PreloadedDeclIDs.clear();
7862 
7863   // FIXME: What happens if these are changed by a module import?
7864   if (!FPPragmaOptions.empty()) {
7865     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7866     FPOptionsOverride NewOverrides =
7867         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7868     SemaObj->CurFPFeatures =
7869         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7870   }
7871 
7872   SemaObj->OpenCLFeatures = OpenCLExtensions;
7873 
7874   UpdateSema();
7875 }
7876 
7877 void ASTReader::UpdateSema() {
7878   assert(SemaObj && "no Sema to update");
7879 
7880   // Load the offsets of the declarations that Sema references.
7881   // They will be lazily deserialized when needed.
7882   if (!SemaDeclRefs.empty()) {
7883     assert(SemaDeclRefs.size() % 3 == 0);
7884     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7885       if (!SemaObj->StdNamespace)
7886         SemaObj->StdNamespace = SemaDeclRefs[I];
7887       if (!SemaObj->StdBadAlloc)
7888         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7889       if (!SemaObj->StdAlignValT)
7890         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7891     }
7892     SemaDeclRefs.clear();
7893   }
7894 
7895   // Update the state of pragmas. Use the same API as if we had encountered the
7896   // pragma in the source.
7897   if(OptimizeOffPragmaLocation.isValid())
7898     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7899   if (PragmaMSStructState != -1)
7900     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7901   if (PointersToMembersPragmaLocation.isValid()) {
7902     SemaObj->ActOnPragmaMSPointersToMembers(
7903         (LangOptions::PragmaMSPointersToMembersKind)
7904             PragmaMSPointersToMembersState,
7905         PointersToMembersPragmaLocation);
7906   }
7907   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7908 
7909   if (PragmaAlignPackCurrentValue) {
7910     // The bottom of the stack might have a default value. It must be adjusted
7911     // to the current value to ensure that the packing state is preserved after
7912     // popping entries that were included/imported from a PCH/module.
7913     bool DropFirst = false;
7914     if (!PragmaAlignPackStack.empty() &&
7915         PragmaAlignPackStack.front().Location.isInvalid()) {
7916       assert(PragmaAlignPackStack.front().Value ==
7917                  SemaObj->AlignPackStack.DefaultValue &&
7918              "Expected a default alignment value");
7919       SemaObj->AlignPackStack.Stack.emplace_back(
7920           PragmaAlignPackStack.front().SlotLabel,
7921           SemaObj->AlignPackStack.CurrentValue,
7922           SemaObj->AlignPackStack.CurrentPragmaLocation,
7923           PragmaAlignPackStack.front().PushLocation);
7924       DropFirst = true;
7925     }
7926     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7927                                  .drop_front(DropFirst ? 1 : 0)) {
7928       SemaObj->AlignPackStack.Stack.emplace_back(
7929           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7930     }
7931     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7932       assert(*PragmaAlignPackCurrentValue ==
7933                  SemaObj->AlignPackStack.DefaultValue &&
7934              "Expected a default align and pack value");
7935       // Keep the current values.
7936     } else {
7937       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7938       SemaObj->AlignPackStack.CurrentPragmaLocation =
7939           PragmaAlignPackCurrentLocation;
7940     }
7941   }
7942   if (FpPragmaCurrentValue) {
7943     // The bottom of the stack might have a default value. It must be adjusted
7944     // to the current value to ensure that fp-pragma state is preserved after
7945     // popping entries that were included/imported from a PCH/module.
7946     bool DropFirst = false;
7947     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7948       assert(FpPragmaStack.front().Value ==
7949                  SemaObj->FpPragmaStack.DefaultValue &&
7950              "Expected a default pragma float_control value");
7951       SemaObj->FpPragmaStack.Stack.emplace_back(
7952           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7953           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7954           FpPragmaStack.front().PushLocation);
7955       DropFirst = true;
7956     }
7957     for (const auto &Entry :
7958          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7959       SemaObj->FpPragmaStack.Stack.emplace_back(
7960           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7961     if (FpPragmaCurrentLocation.isInvalid()) {
7962       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7963              "Expected a default pragma float_control value");
7964       // Keep the current values.
7965     } else {
7966       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7967       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7968     }
7969   }
7970 
7971   // For non-modular AST files, restore visiblity of modules.
7972   for (auto &Import : ImportedModules) {
7973     if (Import.ImportLoc.isInvalid())
7974       continue;
7975     if (Module *Imported = getSubmodule(Import.ID)) {
7976       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7977     }
7978   }
7979 }
7980 
7981 IdentifierInfo *ASTReader::get(StringRef Name) {
7982   // Note that we are loading an identifier.
7983   Deserializing AnIdentifier(this);
7984 
7985   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7986                                   NumIdentifierLookups,
7987                                   NumIdentifierLookupHits);
7988 
7989   // We don't need to do identifier table lookups in C++ modules (we preload
7990   // all interesting declarations, and don't need to use the scope for name
7991   // lookups). Perform the lookup in PCH files, though, since we don't build
7992   // a complete initial identifier table if we're carrying on from a PCH.
7993   if (PP.getLangOpts().CPlusPlus) {
7994     for (auto F : ModuleMgr.pch_modules())
7995       if (Visitor(*F))
7996         break;
7997   } else {
7998     // If there is a global index, look there first to determine which modules
7999     // provably do not have any results for this identifier.
8000     GlobalModuleIndex::HitSet Hits;
8001     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8002     if (!loadGlobalIndex()) {
8003       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8004         HitsPtr = &Hits;
8005       }
8006     }
8007 
8008     ModuleMgr.visit(Visitor, HitsPtr);
8009   }
8010 
8011   IdentifierInfo *II = Visitor.getIdentifierInfo();
8012   markIdentifierUpToDate(II);
8013   return II;
8014 }
8015 
8016 namespace clang {
8017 
8018   /// An identifier-lookup iterator that enumerates all of the
8019   /// identifiers stored within a set of AST files.
8020   class ASTIdentifierIterator : public IdentifierIterator {
8021     /// The AST reader whose identifiers are being enumerated.
8022     const ASTReader &Reader;
8023 
8024     /// The current index into the chain of AST files stored in
8025     /// the AST reader.
8026     unsigned Index;
8027 
8028     /// The current position within the identifier lookup table
8029     /// of the current AST file.
8030     ASTIdentifierLookupTable::key_iterator Current;
8031 
8032     /// The end position within the identifier lookup table of
8033     /// the current AST file.
8034     ASTIdentifierLookupTable::key_iterator End;
8035 
8036     /// Whether to skip any modules in the ASTReader.
8037     bool SkipModules;
8038 
8039   public:
8040     explicit ASTIdentifierIterator(const ASTReader &Reader,
8041                                    bool SkipModules = false);
8042 
8043     StringRef Next() override;
8044   };
8045 
8046 } // namespace clang
8047 
8048 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8049                                              bool SkipModules)
8050     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8051 }
8052 
8053 StringRef ASTIdentifierIterator::Next() {
8054   while (Current == End) {
8055     // If we have exhausted all of our AST files, we're done.
8056     if (Index == 0)
8057       return StringRef();
8058 
8059     --Index;
8060     ModuleFile &F = Reader.ModuleMgr[Index];
8061     if (SkipModules && F.isModule())
8062       continue;
8063 
8064     ASTIdentifierLookupTable *IdTable =
8065         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8066     Current = IdTable->key_begin();
8067     End = IdTable->key_end();
8068   }
8069 
8070   // We have any identifiers remaining in the current AST file; return
8071   // the next one.
8072   StringRef Result = *Current;
8073   ++Current;
8074   return Result;
8075 }
8076 
8077 namespace {
8078 
8079 /// A utility for appending two IdentifierIterators.
8080 class ChainedIdentifierIterator : public IdentifierIterator {
8081   std::unique_ptr<IdentifierIterator> Current;
8082   std::unique_ptr<IdentifierIterator> Queued;
8083 
8084 public:
8085   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8086                             std::unique_ptr<IdentifierIterator> Second)
8087       : Current(std::move(First)), Queued(std::move(Second)) {}
8088 
8089   StringRef Next() override {
8090     if (!Current)
8091       return StringRef();
8092 
8093     StringRef result = Current->Next();
8094     if (!result.empty())
8095       return result;
8096 
8097     // Try the queued iterator, which may itself be empty.
8098     Current.reset();
8099     std::swap(Current, Queued);
8100     return Next();
8101   }
8102 };
8103 
8104 } // namespace
8105 
8106 IdentifierIterator *ASTReader::getIdentifiers() {
8107   if (!loadGlobalIndex()) {
8108     std::unique_ptr<IdentifierIterator> ReaderIter(
8109         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8110     std::unique_ptr<IdentifierIterator> ModulesIter(
8111         GlobalIndex->createIdentifierIterator());
8112     return new ChainedIdentifierIterator(std::move(ReaderIter),
8113                                          std::move(ModulesIter));
8114   }
8115 
8116   return new ASTIdentifierIterator(*this);
8117 }
8118 
8119 namespace clang {
8120 namespace serialization {
8121 
8122   class ReadMethodPoolVisitor {
8123     ASTReader &Reader;
8124     Selector Sel;
8125     unsigned PriorGeneration;
8126     unsigned InstanceBits = 0;
8127     unsigned FactoryBits = 0;
8128     bool InstanceHasMoreThanOneDecl = false;
8129     bool FactoryHasMoreThanOneDecl = false;
8130     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8131     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8132 
8133   public:
8134     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8135                           unsigned PriorGeneration)
8136         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8137 
8138     bool operator()(ModuleFile &M) {
8139       if (!M.SelectorLookupTable)
8140         return false;
8141 
8142       // If we've already searched this module file, skip it now.
8143       if (M.Generation <= PriorGeneration)
8144         return true;
8145 
8146       ++Reader.NumMethodPoolTableLookups;
8147       ASTSelectorLookupTable *PoolTable
8148         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8149       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8150       if (Pos == PoolTable->end())
8151         return false;
8152 
8153       ++Reader.NumMethodPoolTableHits;
8154       ++Reader.NumSelectorsRead;
8155       // FIXME: Not quite happy with the statistics here. We probably should
8156       // disable this tracking when called via LoadSelector.
8157       // Also, should entries without methods count as misses?
8158       ++Reader.NumMethodPoolEntriesRead;
8159       ASTSelectorLookupTrait::data_type Data = *Pos;
8160       if (Reader.DeserializationListener)
8161         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8162 
8163       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8164       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8165       InstanceBits = Data.InstanceBits;
8166       FactoryBits = Data.FactoryBits;
8167       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8168       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8169       return true;
8170     }
8171 
8172     /// Retrieve the instance methods found by this visitor.
8173     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8174       return InstanceMethods;
8175     }
8176 
8177     /// Retrieve the instance methods found by this visitor.
8178     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8179       return FactoryMethods;
8180     }
8181 
8182     unsigned getInstanceBits() const { return InstanceBits; }
8183     unsigned getFactoryBits() const { return FactoryBits; }
8184 
8185     bool instanceHasMoreThanOneDecl() const {
8186       return InstanceHasMoreThanOneDecl;
8187     }
8188 
8189     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8190   };
8191 
8192 } // namespace serialization
8193 } // namespace clang
8194 
8195 /// Add the given set of methods to the method list.
8196 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8197                              ObjCMethodList &List) {
8198   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8199     S.addMethodToGlobalList(&List, Methods[I]);
8200   }
8201 }
8202 
8203 void ASTReader::ReadMethodPool(Selector Sel) {
8204   // Get the selector generation and update it to the current generation.
8205   unsigned &Generation = SelectorGeneration[Sel];
8206   unsigned PriorGeneration = Generation;
8207   Generation = getGeneration();
8208   SelectorOutOfDate[Sel] = false;
8209 
8210   // Search for methods defined with this selector.
8211   ++NumMethodPoolLookups;
8212   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8213   ModuleMgr.visit(Visitor);
8214 
8215   if (Visitor.getInstanceMethods().empty() &&
8216       Visitor.getFactoryMethods().empty())
8217     return;
8218 
8219   ++NumMethodPoolHits;
8220 
8221   if (!getSema())
8222     return;
8223 
8224   Sema &S = *getSema();
8225   Sema::GlobalMethodPool::iterator Pos
8226     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8227 
8228   Pos->second.first.setBits(Visitor.getInstanceBits());
8229   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8230   Pos->second.second.setBits(Visitor.getFactoryBits());
8231   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8232 
8233   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8234   // when building a module we keep every method individually and may need to
8235   // update hasMoreThanOneDecl as we add the methods.
8236   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8237   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8238 }
8239 
8240 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8241   if (SelectorOutOfDate[Sel])
8242     ReadMethodPool(Sel);
8243 }
8244 
8245 void ASTReader::ReadKnownNamespaces(
8246                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8247   Namespaces.clear();
8248 
8249   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8250     if (NamespaceDecl *Namespace
8251                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8252       Namespaces.push_back(Namespace);
8253   }
8254 }
8255 
8256 void ASTReader::ReadUndefinedButUsed(
8257     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8258   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8259     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8260     SourceLocation Loc =
8261         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8262     Undefined.insert(std::make_pair(D, Loc));
8263   }
8264 }
8265 
8266 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8267     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8268                                                      Exprs) {
8269   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8270     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8271     uint64_t Count = DelayedDeleteExprs[Idx++];
8272     for (uint64_t C = 0; C < Count; ++C) {
8273       SourceLocation DeleteLoc =
8274           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8275       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8276       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8277     }
8278   }
8279 }
8280 
8281 void ASTReader::ReadTentativeDefinitions(
8282                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8283   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8284     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8285     if (Var)
8286       TentativeDefs.push_back(Var);
8287   }
8288   TentativeDefinitions.clear();
8289 }
8290 
8291 void ASTReader::ReadUnusedFileScopedDecls(
8292                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8293   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8294     DeclaratorDecl *D
8295       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8296     if (D)
8297       Decls.push_back(D);
8298   }
8299   UnusedFileScopedDecls.clear();
8300 }
8301 
8302 void ASTReader::ReadDelegatingConstructors(
8303                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8304   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8305     CXXConstructorDecl *D
8306       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8307     if (D)
8308       Decls.push_back(D);
8309   }
8310   DelegatingCtorDecls.clear();
8311 }
8312 
8313 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8314   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8315     TypedefNameDecl *D
8316       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8317     if (D)
8318       Decls.push_back(D);
8319   }
8320   ExtVectorDecls.clear();
8321 }
8322 
8323 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8324     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8325   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8326        ++I) {
8327     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8328         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8329     if (D)
8330       Decls.insert(D);
8331   }
8332   UnusedLocalTypedefNameCandidates.clear();
8333 }
8334 
8335 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8336     llvm::SmallVector<Decl *, 4> &Decls) {
8337   for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8338        ++I) {
8339     auto *D = dyn_cast_or_null<Decl>(
8340         GetDecl(DeclsToCheckForDeferredDiags[I]));
8341     if (D)
8342       Decls.push_back(D);
8343   }
8344   DeclsToCheckForDeferredDiags.clear();
8345 }
8346 
8347 
8348 void ASTReader::ReadReferencedSelectors(
8349        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8350   if (ReferencedSelectorsData.empty())
8351     return;
8352 
8353   // If there are @selector references added them to its pool. This is for
8354   // implementation of -Wselector.
8355   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8356   unsigned I = 0;
8357   while (I < DataSize) {
8358     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8359     SourceLocation SelLoc
8360       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8361     Sels.push_back(std::make_pair(Sel, SelLoc));
8362   }
8363   ReferencedSelectorsData.clear();
8364 }
8365 
8366 void ASTReader::ReadWeakUndeclaredIdentifiers(
8367        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8368   if (WeakUndeclaredIdentifiers.empty())
8369     return;
8370 
8371   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8372     IdentifierInfo *WeakId
8373       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8374     IdentifierInfo *AliasId
8375       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8376     SourceLocation Loc
8377       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8378     bool Used = WeakUndeclaredIdentifiers[I++];
8379     WeakInfo WI(AliasId, Loc);
8380     WI.setUsed(Used);
8381     WeakIDs.push_back(std::make_pair(WeakId, WI));
8382   }
8383   WeakUndeclaredIdentifiers.clear();
8384 }
8385 
8386 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8387   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8388     ExternalVTableUse VT;
8389     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8390     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8391     VT.DefinitionRequired = VTableUses[Idx++];
8392     VTables.push_back(VT);
8393   }
8394 
8395   VTableUses.clear();
8396 }
8397 
8398 void ASTReader::ReadPendingInstantiations(
8399        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8400   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8401     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8402     SourceLocation Loc
8403       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8404 
8405     Pending.push_back(std::make_pair(D, Loc));
8406   }
8407   PendingInstantiations.clear();
8408 }
8409 
8410 void ASTReader::ReadLateParsedTemplates(
8411     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8412         &LPTMap) {
8413   for (auto &LPT : LateParsedTemplates) {
8414     ModuleFile *FMod = LPT.first;
8415     RecordDataImpl &LateParsed = LPT.second;
8416     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8417          /* In loop */) {
8418       FunctionDecl *FD =
8419           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8420 
8421       auto LT = std::make_unique<LateParsedTemplate>();
8422       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8423 
8424       ModuleFile *F = getOwningModuleFile(LT->D);
8425       assert(F && "No module");
8426 
8427       unsigned TokN = LateParsed[Idx++];
8428       LT->Toks.reserve(TokN);
8429       for (unsigned T = 0; T < TokN; ++T)
8430         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8431 
8432       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8433     }
8434   }
8435 }
8436 
8437 void ASTReader::LoadSelector(Selector Sel) {
8438   // It would be complicated to avoid reading the methods anyway. So don't.
8439   ReadMethodPool(Sel);
8440 }
8441 
8442 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8443   assert(ID && "Non-zero identifier ID required");
8444   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8445   IdentifiersLoaded[ID - 1] = II;
8446   if (DeserializationListener)
8447     DeserializationListener->IdentifierRead(ID, II);
8448 }
8449 
8450 /// Set the globally-visible declarations associated with the given
8451 /// identifier.
8452 ///
8453 /// If the AST reader is currently in a state where the given declaration IDs
8454 /// cannot safely be resolved, they are queued until it is safe to resolve
8455 /// them.
8456 ///
8457 /// \param II an IdentifierInfo that refers to one or more globally-visible
8458 /// declarations.
8459 ///
8460 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8461 /// visible at global scope.
8462 ///
8463 /// \param Decls if non-null, this vector will be populated with the set of
8464 /// deserialized declarations. These declarations will not be pushed into
8465 /// scope.
8466 void
8467 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8468                               const SmallVectorImpl<uint32_t> &DeclIDs,
8469                                    SmallVectorImpl<Decl *> *Decls) {
8470   if (NumCurrentElementsDeserializing && !Decls) {
8471     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8472     return;
8473   }
8474 
8475   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8476     if (!SemaObj) {
8477       // Queue this declaration so that it will be added to the
8478       // translation unit scope and identifier's declaration chain
8479       // once a Sema object is known.
8480       PreloadedDeclIDs.push_back(DeclIDs[I]);
8481       continue;
8482     }
8483 
8484     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8485 
8486     // If we're simply supposed to record the declarations, do so now.
8487     if (Decls) {
8488       Decls->push_back(D);
8489       continue;
8490     }
8491 
8492     // Introduce this declaration into the translation-unit scope
8493     // and add it to the declaration chain for this identifier, so
8494     // that (unqualified) name lookup will find it.
8495     pushExternalDeclIntoScope(D, II);
8496   }
8497 }
8498 
8499 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8500   if (ID == 0)
8501     return nullptr;
8502 
8503   if (IdentifiersLoaded.empty()) {
8504     Error("no identifier table in AST file");
8505     return nullptr;
8506   }
8507 
8508   ID -= 1;
8509   if (!IdentifiersLoaded[ID]) {
8510     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8511     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8512     ModuleFile *M = I->second;
8513     unsigned Index = ID - M->BaseIdentifierID;
8514     const unsigned char *Data =
8515         M->IdentifierTableData + M->IdentifierOffsets[Index];
8516 
8517     ASTIdentifierLookupTrait Trait(*this, *M);
8518     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8519     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8520     auto &II = PP.getIdentifierTable().get(Key);
8521     IdentifiersLoaded[ID] = &II;
8522     markIdentifierFromAST(*this,  II);
8523     if (DeserializationListener)
8524       DeserializationListener->IdentifierRead(ID + 1, &II);
8525   }
8526 
8527   return IdentifiersLoaded[ID];
8528 }
8529 
8530 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8531   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8532 }
8533 
8534 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8535   if (LocalID < NUM_PREDEF_IDENT_IDS)
8536     return LocalID;
8537 
8538   if (!M.ModuleOffsetMap.empty())
8539     ReadModuleOffsetMap(M);
8540 
8541   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8542     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8543   assert(I != M.IdentifierRemap.end()
8544          && "Invalid index into identifier index remap");
8545 
8546   return LocalID + I->second;
8547 }
8548 
8549 MacroInfo *ASTReader::getMacro(MacroID ID) {
8550   if (ID == 0)
8551     return nullptr;
8552 
8553   if (MacrosLoaded.empty()) {
8554     Error("no macro table in AST file");
8555     return nullptr;
8556   }
8557 
8558   ID -= NUM_PREDEF_MACRO_IDS;
8559   if (!MacrosLoaded[ID]) {
8560     GlobalMacroMapType::iterator I
8561       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8562     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8563     ModuleFile *M = I->second;
8564     unsigned Index = ID - M->BaseMacroID;
8565     MacrosLoaded[ID] =
8566         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8567 
8568     if (DeserializationListener)
8569       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8570                                          MacrosLoaded[ID]);
8571   }
8572 
8573   return MacrosLoaded[ID];
8574 }
8575 
8576 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8577   if (LocalID < NUM_PREDEF_MACRO_IDS)
8578     return LocalID;
8579 
8580   if (!M.ModuleOffsetMap.empty())
8581     ReadModuleOffsetMap(M);
8582 
8583   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8584     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8585   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8586 
8587   return LocalID + I->second;
8588 }
8589 
8590 serialization::SubmoduleID
8591 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8592   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8593     return LocalID;
8594 
8595   if (!M.ModuleOffsetMap.empty())
8596     ReadModuleOffsetMap(M);
8597 
8598   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8599     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8600   assert(I != M.SubmoduleRemap.end()
8601          && "Invalid index into submodule index remap");
8602 
8603   return LocalID + I->second;
8604 }
8605 
8606 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8607   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8608     assert(GlobalID == 0 && "Unhandled global submodule ID");
8609     return nullptr;
8610   }
8611 
8612   if (GlobalID > SubmodulesLoaded.size()) {
8613     Error("submodule ID out of range in AST file");
8614     return nullptr;
8615   }
8616 
8617   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8618 }
8619 
8620 Module *ASTReader::getModule(unsigned ID) {
8621   return getSubmodule(ID);
8622 }
8623 
8624 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8625   if (ID & 1) {
8626     // It's a module, look it up by submodule ID.
8627     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8628     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8629   } else {
8630     // It's a prefix (preamble, PCH, ...). Look it up by index.
8631     unsigned IndexFromEnd = ID >> 1;
8632     assert(IndexFromEnd && "got reference to unknown module file");
8633     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8634   }
8635 }
8636 
8637 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8638   if (!F)
8639     return 1;
8640 
8641   // For a file representing a module, use the submodule ID of the top-level
8642   // module as the file ID. For any other kind of file, the number of such
8643   // files loaded beforehand will be the same on reload.
8644   // FIXME: Is this true even if we have an explicit module file and a PCH?
8645   if (F->isModule())
8646     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8647 
8648   auto PCHModules = getModuleManager().pch_modules();
8649   auto I = llvm::find(PCHModules, F);
8650   assert(I != PCHModules.end() && "emitting reference to unknown file");
8651   return (I - PCHModules.end()) << 1;
8652 }
8653 
8654 llvm::Optional<ASTSourceDescriptor>
8655 ASTReader::getSourceDescriptor(unsigned ID) {
8656   if (Module *M = getSubmodule(ID))
8657     return ASTSourceDescriptor(*M);
8658 
8659   // If there is only a single PCH, return it instead.
8660   // Chained PCH are not supported.
8661   const auto &PCHChain = ModuleMgr.pch_modules();
8662   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8663     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8664     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8665     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8666     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8667                                MF.Signature);
8668   }
8669   return None;
8670 }
8671 
8672 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8673   auto I = DefinitionSource.find(FD);
8674   if (I == DefinitionSource.end())
8675     return EK_ReplyHazy;
8676   return I->second ? EK_Never : EK_Always;
8677 }
8678 
8679 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8680   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8681 }
8682 
8683 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8684   if (ID == 0)
8685     return Selector();
8686 
8687   if (ID > SelectorsLoaded.size()) {
8688     Error("selector ID out of range in AST file");
8689     return Selector();
8690   }
8691 
8692   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8693     // Load this selector from the selector table.
8694     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8695     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8696     ModuleFile &M = *I->second;
8697     ASTSelectorLookupTrait Trait(*this, M);
8698     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8699     SelectorsLoaded[ID - 1] =
8700       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8701     if (DeserializationListener)
8702       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8703   }
8704 
8705   return SelectorsLoaded[ID - 1];
8706 }
8707 
8708 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8709   return DecodeSelector(ID);
8710 }
8711 
8712 uint32_t ASTReader::GetNumExternalSelectors() {
8713   // ID 0 (the null selector) is considered an external selector.
8714   return getTotalNumSelectors() + 1;
8715 }
8716 
8717 serialization::SelectorID
8718 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8719   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8720     return LocalID;
8721 
8722   if (!M.ModuleOffsetMap.empty())
8723     ReadModuleOffsetMap(M);
8724 
8725   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8726     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8727   assert(I != M.SelectorRemap.end()
8728          && "Invalid index into selector index remap");
8729 
8730   return LocalID + I->second;
8731 }
8732 
8733 DeclarationNameLoc
8734 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8735   switch (Name.getNameKind()) {
8736   case DeclarationName::CXXConstructorName:
8737   case DeclarationName::CXXDestructorName:
8738   case DeclarationName::CXXConversionFunctionName:
8739     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8740 
8741   case DeclarationName::CXXOperatorName:
8742     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8743 
8744   case DeclarationName::CXXLiteralOperatorName:
8745     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8746         readSourceLocation());
8747 
8748   case DeclarationName::Identifier:
8749   case DeclarationName::ObjCZeroArgSelector:
8750   case DeclarationName::ObjCOneArgSelector:
8751   case DeclarationName::ObjCMultiArgSelector:
8752   case DeclarationName::CXXUsingDirective:
8753   case DeclarationName::CXXDeductionGuideName:
8754     break;
8755   }
8756   return DeclarationNameLoc();
8757 }
8758 
8759 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8760   DeclarationNameInfo NameInfo;
8761   NameInfo.setName(readDeclarationName());
8762   NameInfo.setLoc(readSourceLocation());
8763   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8764   return NameInfo;
8765 }
8766 
8767 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8768   Info.QualifierLoc = readNestedNameSpecifierLoc();
8769   unsigned NumTPLists = readInt();
8770   Info.NumTemplParamLists = NumTPLists;
8771   if (NumTPLists) {
8772     Info.TemplParamLists =
8773         new (getContext()) TemplateParameterList *[NumTPLists];
8774     for (unsigned i = 0; i != NumTPLists; ++i)
8775       Info.TemplParamLists[i] = readTemplateParameterList();
8776   }
8777 }
8778 
8779 TemplateParameterList *
8780 ASTRecordReader::readTemplateParameterList() {
8781   SourceLocation TemplateLoc = readSourceLocation();
8782   SourceLocation LAngleLoc = readSourceLocation();
8783   SourceLocation RAngleLoc = readSourceLocation();
8784 
8785   unsigned NumParams = readInt();
8786   SmallVector<NamedDecl *, 16> Params;
8787   Params.reserve(NumParams);
8788   while (NumParams--)
8789     Params.push_back(readDeclAs<NamedDecl>());
8790 
8791   bool HasRequiresClause = readBool();
8792   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8793 
8794   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8795       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8796   return TemplateParams;
8797 }
8798 
8799 void ASTRecordReader::readTemplateArgumentList(
8800                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8801                         bool Canonicalize) {
8802   unsigned NumTemplateArgs = readInt();
8803   TemplArgs.reserve(NumTemplateArgs);
8804   while (NumTemplateArgs--)
8805     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8806 }
8807 
8808 /// Read a UnresolvedSet structure.
8809 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8810   unsigned NumDecls = readInt();
8811   Set.reserve(getContext(), NumDecls);
8812   while (NumDecls--) {
8813     DeclID ID = readDeclID();
8814     AccessSpecifier AS = (AccessSpecifier) readInt();
8815     Set.addLazyDecl(getContext(), ID, AS);
8816   }
8817 }
8818 
8819 CXXBaseSpecifier
8820 ASTRecordReader::readCXXBaseSpecifier() {
8821   bool isVirtual = readBool();
8822   bool isBaseOfClass = readBool();
8823   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8824   bool inheritConstructors = readBool();
8825   TypeSourceInfo *TInfo = readTypeSourceInfo();
8826   SourceRange Range = readSourceRange();
8827   SourceLocation EllipsisLoc = readSourceLocation();
8828   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8829                           EllipsisLoc);
8830   Result.setInheritConstructors(inheritConstructors);
8831   return Result;
8832 }
8833 
8834 CXXCtorInitializer **
8835 ASTRecordReader::readCXXCtorInitializers() {
8836   ASTContext &Context = getContext();
8837   unsigned NumInitializers = readInt();
8838   assert(NumInitializers && "wrote ctor initializers but have no inits");
8839   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8840   for (unsigned i = 0; i != NumInitializers; ++i) {
8841     TypeSourceInfo *TInfo = nullptr;
8842     bool IsBaseVirtual = false;
8843     FieldDecl *Member = nullptr;
8844     IndirectFieldDecl *IndirectMember = nullptr;
8845 
8846     CtorInitializerType Type = (CtorInitializerType) readInt();
8847     switch (Type) {
8848     case CTOR_INITIALIZER_BASE:
8849       TInfo = readTypeSourceInfo();
8850       IsBaseVirtual = readBool();
8851       break;
8852 
8853     case CTOR_INITIALIZER_DELEGATING:
8854       TInfo = readTypeSourceInfo();
8855       break;
8856 
8857      case CTOR_INITIALIZER_MEMBER:
8858       Member = readDeclAs<FieldDecl>();
8859       break;
8860 
8861      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8862       IndirectMember = readDeclAs<IndirectFieldDecl>();
8863       break;
8864     }
8865 
8866     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8867     Expr *Init = readExpr();
8868     SourceLocation LParenLoc = readSourceLocation();
8869     SourceLocation RParenLoc = readSourceLocation();
8870 
8871     CXXCtorInitializer *BOMInit;
8872     if (Type == CTOR_INITIALIZER_BASE)
8873       BOMInit = new (Context)
8874           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8875                              RParenLoc, MemberOrEllipsisLoc);
8876     else if (Type == CTOR_INITIALIZER_DELEGATING)
8877       BOMInit = new (Context)
8878           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8879     else if (Member)
8880       BOMInit = new (Context)
8881           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8882                              Init, RParenLoc);
8883     else
8884       BOMInit = new (Context)
8885           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8886                              LParenLoc, Init, RParenLoc);
8887 
8888     if (/*IsWritten*/readBool()) {
8889       unsigned SourceOrder = readInt();
8890       BOMInit->setSourceOrder(SourceOrder);
8891     }
8892 
8893     CtorInitializers[i] = BOMInit;
8894   }
8895 
8896   return CtorInitializers;
8897 }
8898 
8899 NestedNameSpecifierLoc
8900 ASTRecordReader::readNestedNameSpecifierLoc() {
8901   ASTContext &Context = getContext();
8902   unsigned N = readInt();
8903   NestedNameSpecifierLocBuilder Builder;
8904   for (unsigned I = 0; I != N; ++I) {
8905     auto Kind = readNestedNameSpecifierKind();
8906     switch (Kind) {
8907     case NestedNameSpecifier::Identifier: {
8908       IdentifierInfo *II = readIdentifier();
8909       SourceRange Range = readSourceRange();
8910       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8911       break;
8912     }
8913 
8914     case NestedNameSpecifier::Namespace: {
8915       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8916       SourceRange Range = readSourceRange();
8917       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8918       break;
8919     }
8920 
8921     case NestedNameSpecifier::NamespaceAlias: {
8922       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8923       SourceRange Range = readSourceRange();
8924       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8925       break;
8926     }
8927 
8928     case NestedNameSpecifier::TypeSpec:
8929     case NestedNameSpecifier::TypeSpecWithTemplate: {
8930       bool Template = readBool();
8931       TypeSourceInfo *T = readTypeSourceInfo();
8932       if (!T)
8933         return NestedNameSpecifierLoc();
8934       SourceLocation ColonColonLoc = readSourceLocation();
8935 
8936       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8937       Builder.Extend(Context,
8938                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8939                      T->getTypeLoc(), ColonColonLoc);
8940       break;
8941     }
8942 
8943     case NestedNameSpecifier::Global: {
8944       SourceLocation ColonColonLoc = readSourceLocation();
8945       Builder.MakeGlobal(Context, ColonColonLoc);
8946       break;
8947     }
8948 
8949     case NestedNameSpecifier::Super: {
8950       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8951       SourceRange Range = readSourceRange();
8952       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8953       break;
8954     }
8955     }
8956   }
8957 
8958   return Builder.getWithLocInContext(Context);
8959 }
8960 
8961 SourceRange
8962 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8963                            unsigned &Idx) {
8964   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8965   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8966   return SourceRange(beg, end);
8967 }
8968 
8969 /// Read a floating-point value
8970 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8971   return llvm::APFloat(Sem, readAPInt());
8972 }
8973 
8974 // Read a string
8975 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8976   unsigned Len = Record[Idx++];
8977   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8978   Idx += Len;
8979   return Result;
8980 }
8981 
8982 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8983                                 unsigned &Idx) {
8984   std::string Filename = ReadString(Record, Idx);
8985   ResolveImportedPath(F, Filename);
8986   return Filename;
8987 }
8988 
8989 std::string ASTReader::ReadPath(StringRef BaseDirectory,
8990                                 const RecordData &Record, unsigned &Idx) {
8991   std::string Filename = ReadString(Record, Idx);
8992   if (!BaseDirectory.empty())
8993     ResolveImportedPath(Filename, BaseDirectory);
8994   return Filename;
8995 }
8996 
8997 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8998                                          unsigned &Idx) {
8999   unsigned Major = Record[Idx++];
9000   unsigned Minor = Record[Idx++];
9001   unsigned Subminor = Record[Idx++];
9002   if (Minor == 0)
9003     return VersionTuple(Major);
9004   if (Subminor == 0)
9005     return VersionTuple(Major, Minor - 1);
9006   return VersionTuple(Major, Minor - 1, Subminor - 1);
9007 }
9008 
9009 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9010                                           const RecordData &Record,
9011                                           unsigned &Idx) {
9012   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9013   return CXXTemporary::Create(getContext(), Decl);
9014 }
9015 
9016 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9017   return Diag(CurrentImportLoc, DiagID);
9018 }
9019 
9020 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9021   return Diags.Report(Loc, DiagID);
9022 }
9023 
9024 /// Retrieve the identifier table associated with the
9025 /// preprocessor.
9026 IdentifierTable &ASTReader::getIdentifierTable() {
9027   return PP.getIdentifierTable();
9028 }
9029 
9030 /// Record that the given ID maps to the given switch-case
9031 /// statement.
9032 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9033   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9034          "Already have a SwitchCase with this ID");
9035   (*CurrSwitchCaseStmts)[ID] = SC;
9036 }
9037 
9038 /// Retrieve the switch-case statement with the given ID.
9039 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9040   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9041   return (*CurrSwitchCaseStmts)[ID];
9042 }
9043 
9044 void ASTReader::ClearSwitchCaseIDs() {
9045   CurrSwitchCaseStmts->clear();
9046 }
9047 
9048 void ASTReader::ReadComments() {
9049   ASTContext &Context = getContext();
9050   std::vector<RawComment *> Comments;
9051   for (SmallVectorImpl<std::pair<BitstreamCursor,
9052                                  serialization::ModuleFile *>>::iterator
9053        I = CommentsCursors.begin(),
9054        E = CommentsCursors.end();
9055        I != E; ++I) {
9056     Comments.clear();
9057     BitstreamCursor &Cursor = I->first;
9058     serialization::ModuleFile &F = *I->second;
9059     SavedStreamPosition SavedPosition(Cursor);
9060 
9061     RecordData Record;
9062     while (true) {
9063       Expected<llvm::BitstreamEntry> MaybeEntry =
9064           Cursor.advanceSkippingSubblocks(
9065               BitstreamCursor::AF_DontPopBlockAtEnd);
9066       if (!MaybeEntry) {
9067         Error(MaybeEntry.takeError());
9068         return;
9069       }
9070       llvm::BitstreamEntry Entry = MaybeEntry.get();
9071 
9072       switch (Entry.Kind) {
9073       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9074       case llvm::BitstreamEntry::Error:
9075         Error("malformed block record in AST file");
9076         return;
9077       case llvm::BitstreamEntry::EndBlock:
9078         goto NextCursor;
9079       case llvm::BitstreamEntry::Record:
9080         // The interesting case.
9081         break;
9082       }
9083 
9084       // Read a record.
9085       Record.clear();
9086       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9087       if (!MaybeComment) {
9088         Error(MaybeComment.takeError());
9089         return;
9090       }
9091       switch ((CommentRecordTypes)MaybeComment.get()) {
9092       case COMMENTS_RAW_COMMENT: {
9093         unsigned Idx = 0;
9094         SourceRange SR = ReadSourceRange(F, Record, Idx);
9095         RawComment::CommentKind Kind =
9096             (RawComment::CommentKind) Record[Idx++];
9097         bool IsTrailingComment = Record[Idx++];
9098         bool IsAlmostTrailingComment = Record[Idx++];
9099         Comments.push_back(new (Context) RawComment(
9100             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9101         break;
9102       }
9103       }
9104     }
9105   NextCursor:
9106     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9107         FileToOffsetToComment;
9108     for (RawComment *C : Comments) {
9109       SourceLocation CommentLoc = C->getBeginLoc();
9110       if (CommentLoc.isValid()) {
9111         std::pair<FileID, unsigned> Loc =
9112             SourceMgr.getDecomposedLoc(CommentLoc);
9113         if (Loc.first.isValid())
9114           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9115       }
9116     }
9117   }
9118 }
9119 
9120 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9121                                 bool IncludeSystem, bool Complain,
9122                     llvm::function_ref<void(const serialization::InputFile &IF,
9123                                             bool isSystem)> Visitor) {
9124   unsigned NumUserInputs = MF.NumUserInputFiles;
9125   unsigned NumInputs = MF.InputFilesLoaded.size();
9126   assert(NumUserInputs <= NumInputs);
9127   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9128   for (unsigned I = 0; I < N; ++I) {
9129     bool IsSystem = I >= NumUserInputs;
9130     InputFile IF = getInputFile(MF, I+1, Complain);
9131     Visitor(IF, IsSystem);
9132   }
9133 }
9134 
9135 void ASTReader::visitTopLevelModuleMaps(
9136     serialization::ModuleFile &MF,
9137     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9138   unsigned NumInputs = MF.InputFilesLoaded.size();
9139   for (unsigned I = 0; I < NumInputs; ++I) {
9140     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9141     if (IFI.TopLevelModuleMap)
9142       // FIXME: This unnecessarily re-reads the InputFileInfo.
9143       if (auto FE = getInputFile(MF, I + 1).getFile())
9144         Visitor(FE);
9145   }
9146 }
9147 
9148 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9149   // If we know the owning module, use it.
9150   if (Module *M = D->getImportedOwningModule())
9151     return M->getFullModuleName();
9152 
9153   // Otherwise, use the name of the top-level module the decl is within.
9154   if (ModuleFile *M = getOwningModuleFile(D))
9155     return M->ModuleName;
9156 
9157   // Not from a module.
9158   return {};
9159 }
9160 
9161 void ASTReader::finishPendingActions() {
9162   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9163          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9164          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9165          !PendingUpdateRecords.empty()) {
9166     // If any identifiers with corresponding top-level declarations have
9167     // been loaded, load those declarations now.
9168     using TopLevelDeclsMap =
9169         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9170     TopLevelDeclsMap TopLevelDecls;
9171 
9172     while (!PendingIdentifierInfos.empty()) {
9173       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9174       SmallVector<uint32_t, 4> DeclIDs =
9175           std::move(PendingIdentifierInfos.back().second);
9176       PendingIdentifierInfos.pop_back();
9177 
9178       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9179     }
9180 
9181     // Load each function type that we deferred loading because it was a
9182     // deduced type that might refer to a local type declared within itself.
9183     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9184       auto *FD = PendingFunctionTypes[I].first;
9185       FD->setType(GetType(PendingFunctionTypes[I].second));
9186 
9187       // If we gave a function a deduced return type, remember that we need to
9188       // propagate that along the redeclaration chain.
9189       auto *DT = FD->getReturnType()->getContainedDeducedType();
9190       if (DT && DT->isDeduced())
9191         PendingDeducedTypeUpdates.insert(
9192             {FD->getCanonicalDecl(), FD->getReturnType()});
9193     }
9194     PendingFunctionTypes.clear();
9195 
9196     // For each decl chain that we wanted to complete while deserializing, mark
9197     // it as "still needs to be completed".
9198     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9199       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9200     }
9201     PendingIncompleteDeclChains.clear();
9202 
9203     // Load pending declaration chains.
9204     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9205       loadPendingDeclChain(PendingDeclChains[I].first,
9206                            PendingDeclChains[I].second);
9207     PendingDeclChains.clear();
9208 
9209     // Make the most recent of the top-level declarations visible.
9210     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9211            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9212       IdentifierInfo *II = TLD->first;
9213       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9214         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9215       }
9216     }
9217 
9218     // Load any pending macro definitions.
9219     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9220       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9221       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9222       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9223       // Initialize the macro history from chained-PCHs ahead of module imports.
9224       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9225            ++IDIdx) {
9226         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9227         if (!Info.M->isModule())
9228           resolvePendingMacro(II, Info);
9229       }
9230       // Handle module imports.
9231       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9232            ++IDIdx) {
9233         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9234         if (Info.M->isModule())
9235           resolvePendingMacro(II, Info);
9236       }
9237     }
9238     PendingMacroIDs.clear();
9239 
9240     // Wire up the DeclContexts for Decls that we delayed setting until
9241     // recursive loading is completed.
9242     while (!PendingDeclContextInfos.empty()) {
9243       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9244       PendingDeclContextInfos.pop_front();
9245       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9246       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9247       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9248     }
9249 
9250     // Perform any pending declaration updates.
9251     while (!PendingUpdateRecords.empty()) {
9252       auto Update = PendingUpdateRecords.pop_back_val();
9253       ReadingKindTracker ReadingKind(Read_Decl, *this);
9254       loadDeclUpdateRecords(Update);
9255     }
9256   }
9257 
9258   // At this point, all update records for loaded decls are in place, so any
9259   // fake class definitions should have become real.
9260   assert(PendingFakeDefinitionData.empty() &&
9261          "faked up a class definition but never saw the real one");
9262 
9263   // If we deserialized any C++ or Objective-C class definitions, any
9264   // Objective-C protocol definitions, or any redeclarable templates, make sure
9265   // that all redeclarations point to the definitions. Note that this can only
9266   // happen now, after the redeclaration chains have been fully wired.
9267   for (Decl *D : PendingDefinitions) {
9268     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9269       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9270         // Make sure that the TagType points at the definition.
9271         const_cast<TagType*>(TagT)->decl = TD;
9272       }
9273 
9274       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9275         for (auto *R = getMostRecentExistingDecl(RD); R;
9276              R = R->getPreviousDecl()) {
9277           assert((R == D) ==
9278                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9279                  "declaration thinks it's the definition but it isn't");
9280           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9281         }
9282       }
9283 
9284       continue;
9285     }
9286 
9287     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9288       // Make sure that the ObjCInterfaceType points at the definition.
9289       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9290         ->Decl = ID;
9291 
9292       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9293         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9294 
9295       continue;
9296     }
9297 
9298     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9299       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9300         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9301 
9302       continue;
9303     }
9304 
9305     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9306     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9307       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9308   }
9309   PendingDefinitions.clear();
9310 
9311   // Load the bodies of any functions or methods we've encountered. We do
9312   // this now (delayed) so that we can be sure that the declaration chains
9313   // have been fully wired up (hasBody relies on this).
9314   // FIXME: We shouldn't require complete redeclaration chains here.
9315   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9316                                PBEnd = PendingBodies.end();
9317        PB != PBEnd; ++PB) {
9318     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9319       // For a function defined inline within a class template, force the
9320       // canonical definition to be the one inside the canonical definition of
9321       // the template. This ensures that we instantiate from a correct view
9322       // of the template.
9323       //
9324       // Sadly we can't do this more generally: we can't be sure that all
9325       // copies of an arbitrary class definition will have the same members
9326       // defined (eg, some member functions may not be instantiated, and some
9327       // special members may or may not have been implicitly defined).
9328       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9329         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9330           continue;
9331 
9332       // FIXME: Check for =delete/=default?
9333       // FIXME: Complain about ODR violations here?
9334       const FunctionDecl *Defn = nullptr;
9335       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9336         FD->setLazyBody(PB->second);
9337       } else {
9338         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9339         mergeDefinitionVisibility(NonConstDefn, FD);
9340 
9341         if (!FD->isLateTemplateParsed() &&
9342             !NonConstDefn->isLateTemplateParsed() &&
9343             FD->getODRHash() != NonConstDefn->getODRHash()) {
9344           if (!isa<CXXMethodDecl>(FD)) {
9345             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9346           } else if (FD->getLexicalParent()->isFileContext() &&
9347                      NonConstDefn->getLexicalParent()->isFileContext()) {
9348             // Only diagnose out-of-line method definitions.  If they are
9349             // in class definitions, then an error will be generated when
9350             // processing the class bodies.
9351             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9352           }
9353         }
9354       }
9355       continue;
9356     }
9357 
9358     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9359     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9360       MD->setLazyBody(PB->second);
9361   }
9362   PendingBodies.clear();
9363 
9364   // Do some cleanup.
9365   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9366     getContext().deduplicateMergedDefinitonsFor(ND);
9367   PendingMergedDefinitionsToDeduplicate.clear();
9368 }
9369 
9370 void ASTReader::diagnoseOdrViolations() {
9371   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9372       PendingFunctionOdrMergeFailures.empty() &&
9373       PendingEnumOdrMergeFailures.empty())
9374     return;
9375 
9376   // Trigger the import of the full definition of each class that had any
9377   // odr-merging problems, so we can produce better diagnostics for them.
9378   // These updates may in turn find and diagnose some ODR failures, so take
9379   // ownership of the set first.
9380   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9381   PendingOdrMergeFailures.clear();
9382   for (auto &Merge : OdrMergeFailures) {
9383     Merge.first->buildLookup();
9384     Merge.first->decls_begin();
9385     Merge.first->bases_begin();
9386     Merge.first->vbases_begin();
9387     for (auto &RecordPair : Merge.second) {
9388       auto *RD = RecordPair.first;
9389       RD->decls_begin();
9390       RD->bases_begin();
9391       RD->vbases_begin();
9392     }
9393   }
9394 
9395   // Trigger the import of functions.
9396   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9397   PendingFunctionOdrMergeFailures.clear();
9398   for (auto &Merge : FunctionOdrMergeFailures) {
9399     Merge.first->buildLookup();
9400     Merge.first->decls_begin();
9401     Merge.first->getBody();
9402     for (auto &FD : Merge.second) {
9403       FD->buildLookup();
9404       FD->decls_begin();
9405       FD->getBody();
9406     }
9407   }
9408 
9409   // Trigger the import of enums.
9410   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9411   PendingEnumOdrMergeFailures.clear();
9412   for (auto &Merge : EnumOdrMergeFailures) {
9413     Merge.first->decls_begin();
9414     for (auto &Enum : Merge.second) {
9415       Enum->decls_begin();
9416     }
9417   }
9418 
9419   // For each declaration from a merged context, check that the canonical
9420   // definition of that context also contains a declaration of the same
9421   // entity.
9422   //
9423   // Caution: this loop does things that might invalidate iterators into
9424   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9425   while (!PendingOdrMergeChecks.empty()) {
9426     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9427 
9428     // FIXME: Skip over implicit declarations for now. This matters for things
9429     // like implicitly-declared special member functions. This isn't entirely
9430     // correct; we can end up with multiple unmerged declarations of the same
9431     // implicit entity.
9432     if (D->isImplicit())
9433       continue;
9434 
9435     DeclContext *CanonDef = D->getDeclContext();
9436 
9437     bool Found = false;
9438     const Decl *DCanon = D->getCanonicalDecl();
9439 
9440     for (auto RI : D->redecls()) {
9441       if (RI->getLexicalDeclContext() == CanonDef) {
9442         Found = true;
9443         break;
9444       }
9445     }
9446     if (Found)
9447       continue;
9448 
9449     // Quick check failed, time to do the slow thing. Note, we can't just
9450     // look up the name of D in CanonDef here, because the member that is
9451     // in CanonDef might not be found by name lookup (it might have been
9452     // replaced by a more recent declaration in the lookup table), and we
9453     // can't necessarily find it in the redeclaration chain because it might
9454     // be merely mergeable, not redeclarable.
9455     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9456     for (auto *CanonMember : CanonDef->decls()) {
9457       if (CanonMember->getCanonicalDecl() == DCanon) {
9458         // This can happen if the declaration is merely mergeable and not
9459         // actually redeclarable (we looked for redeclarations earlier).
9460         //
9461         // FIXME: We should be able to detect this more efficiently, without
9462         // pulling in all of the members of CanonDef.
9463         Found = true;
9464         break;
9465       }
9466       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9467         if (ND->getDeclName() == D->getDeclName())
9468           Candidates.push_back(ND);
9469     }
9470 
9471     if (!Found) {
9472       // The AST doesn't like TagDecls becoming invalid after they've been
9473       // completed. We only really need to mark FieldDecls as invalid here.
9474       if (!isa<TagDecl>(D))
9475         D->setInvalidDecl();
9476 
9477       // Ensure we don't accidentally recursively enter deserialization while
9478       // we're producing our diagnostic.
9479       Deserializing RecursionGuard(this);
9480 
9481       std::string CanonDefModule =
9482           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9483       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9484         << D << getOwningModuleNameForDiagnostic(D)
9485         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9486 
9487       if (Candidates.empty())
9488         Diag(cast<Decl>(CanonDef)->getLocation(),
9489              diag::note_module_odr_violation_no_possible_decls) << D;
9490       else {
9491         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9492           Diag(Candidates[I]->getLocation(),
9493                diag::note_module_odr_violation_possible_decl)
9494             << Candidates[I];
9495       }
9496 
9497       DiagnosedOdrMergeFailures.insert(CanonDef);
9498     }
9499   }
9500 
9501   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9502       EnumOdrMergeFailures.empty())
9503     return;
9504 
9505   // Ensure we don't accidentally recursively enter deserialization while
9506   // we're producing our diagnostics.
9507   Deserializing RecursionGuard(this);
9508 
9509   // Common code for hashing helpers.
9510   ODRHash Hash;
9511   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9512     Hash.clear();
9513     Hash.AddQualType(Ty);
9514     return Hash.CalculateHash();
9515   };
9516 
9517   auto ComputeODRHash = [&Hash](const Stmt *S) {
9518     assert(S);
9519     Hash.clear();
9520     Hash.AddStmt(S);
9521     return Hash.CalculateHash();
9522   };
9523 
9524   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9525     assert(D);
9526     Hash.clear();
9527     Hash.AddSubDecl(D);
9528     return Hash.CalculateHash();
9529   };
9530 
9531   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9532     Hash.clear();
9533     Hash.AddTemplateArgument(TA);
9534     return Hash.CalculateHash();
9535   };
9536 
9537   auto ComputeTemplateParameterListODRHash =
9538       [&Hash](const TemplateParameterList *TPL) {
9539         assert(TPL);
9540         Hash.clear();
9541         Hash.AddTemplateParameterList(TPL);
9542         return Hash.CalculateHash();
9543       };
9544 
9545   // Used with err_module_odr_violation_mismatch_decl and
9546   // note_module_odr_violation_mismatch_decl
9547   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9548   enum ODRMismatchDecl {
9549     EndOfClass,
9550     PublicSpecifer,
9551     PrivateSpecifer,
9552     ProtectedSpecifer,
9553     StaticAssert,
9554     Field,
9555     CXXMethod,
9556     TypeAlias,
9557     TypeDef,
9558     Var,
9559     Friend,
9560     FunctionTemplate,
9561     Other
9562   };
9563 
9564   // Used with err_module_odr_violation_mismatch_decl_diff and
9565   // note_module_odr_violation_mismatch_decl_diff
9566   enum ODRMismatchDeclDifference {
9567     StaticAssertCondition,
9568     StaticAssertMessage,
9569     StaticAssertOnlyMessage,
9570     FieldName,
9571     FieldTypeName,
9572     FieldSingleBitField,
9573     FieldDifferentWidthBitField,
9574     FieldSingleMutable,
9575     FieldSingleInitializer,
9576     FieldDifferentInitializers,
9577     MethodName,
9578     MethodDeleted,
9579     MethodDefaulted,
9580     MethodVirtual,
9581     MethodStatic,
9582     MethodVolatile,
9583     MethodConst,
9584     MethodInline,
9585     MethodNumberParameters,
9586     MethodParameterType,
9587     MethodParameterName,
9588     MethodParameterSingleDefaultArgument,
9589     MethodParameterDifferentDefaultArgument,
9590     MethodNoTemplateArguments,
9591     MethodDifferentNumberTemplateArguments,
9592     MethodDifferentTemplateArgument,
9593     MethodSingleBody,
9594     MethodDifferentBody,
9595     TypedefName,
9596     TypedefType,
9597     VarName,
9598     VarType,
9599     VarSingleInitializer,
9600     VarDifferentInitializer,
9601     VarConstexpr,
9602     FriendTypeFunction,
9603     FriendType,
9604     FriendFunction,
9605     FunctionTemplateDifferentNumberParameters,
9606     FunctionTemplateParameterDifferentKind,
9607     FunctionTemplateParameterName,
9608     FunctionTemplateParameterSingleDefaultArgument,
9609     FunctionTemplateParameterDifferentDefaultArgument,
9610     FunctionTemplateParameterDifferentType,
9611     FunctionTemplatePackParameter,
9612   };
9613 
9614   // These lambdas have the common portions of the ODR diagnostics.  This
9615   // has the same return as Diag(), so addition parameters can be passed
9616   // in with operator<<
9617   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9618                                  SourceLocation Loc, SourceRange Range,
9619                                  ODRMismatchDeclDifference DiffType) {
9620     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9621            << FirstRecord << FirstModule.empty() << FirstModule << Range
9622            << DiffType;
9623   };
9624   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9625                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9626     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9627            << SecondModule << Range << DiffType;
9628   };
9629 
9630   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9631                        &ComputeQualTypeODRHash, &ComputeODRHash](
9632                           NamedDecl *FirstRecord, StringRef FirstModule,
9633                           StringRef SecondModule, FieldDecl *FirstField,
9634                           FieldDecl *SecondField) {
9635     IdentifierInfo *FirstII = FirstField->getIdentifier();
9636     IdentifierInfo *SecondII = SecondField->getIdentifier();
9637     if (FirstII->getName() != SecondII->getName()) {
9638       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9639                        FirstField->getSourceRange(), FieldName)
9640           << FirstII;
9641       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9642                       SecondField->getSourceRange(), FieldName)
9643           << SecondII;
9644 
9645       return true;
9646     }
9647 
9648     assert(getContext().hasSameType(FirstField->getType(),
9649                                     SecondField->getType()));
9650 
9651     QualType FirstType = FirstField->getType();
9652     QualType SecondType = SecondField->getType();
9653     if (ComputeQualTypeODRHash(FirstType) !=
9654         ComputeQualTypeODRHash(SecondType)) {
9655       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9656                        FirstField->getSourceRange(), FieldTypeName)
9657           << FirstII << FirstType;
9658       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9659                       SecondField->getSourceRange(), FieldTypeName)
9660           << SecondII << SecondType;
9661 
9662       return true;
9663     }
9664 
9665     const bool IsFirstBitField = FirstField->isBitField();
9666     const bool IsSecondBitField = SecondField->isBitField();
9667     if (IsFirstBitField != IsSecondBitField) {
9668       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9669                        FirstField->getSourceRange(), FieldSingleBitField)
9670           << FirstII << IsFirstBitField;
9671       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9672                       SecondField->getSourceRange(), FieldSingleBitField)
9673           << SecondII << IsSecondBitField;
9674       return true;
9675     }
9676 
9677     if (IsFirstBitField && IsSecondBitField) {
9678       unsigned FirstBitWidthHash =
9679           ComputeODRHash(FirstField->getBitWidth());
9680       unsigned SecondBitWidthHash =
9681           ComputeODRHash(SecondField->getBitWidth());
9682       if (FirstBitWidthHash != SecondBitWidthHash) {
9683         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9684                          FirstField->getSourceRange(),
9685                          FieldDifferentWidthBitField)
9686             << FirstII << FirstField->getBitWidth()->getSourceRange();
9687         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9688                         SecondField->getSourceRange(),
9689                         FieldDifferentWidthBitField)
9690             << SecondII << SecondField->getBitWidth()->getSourceRange();
9691         return true;
9692       }
9693     }
9694 
9695     if (!PP.getLangOpts().CPlusPlus)
9696       return false;
9697 
9698     const bool IsFirstMutable = FirstField->isMutable();
9699     const bool IsSecondMutable = SecondField->isMutable();
9700     if (IsFirstMutable != IsSecondMutable) {
9701       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9702                        FirstField->getSourceRange(), FieldSingleMutable)
9703           << FirstII << IsFirstMutable;
9704       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9705                       SecondField->getSourceRange(), FieldSingleMutable)
9706           << SecondII << IsSecondMutable;
9707       return true;
9708     }
9709 
9710     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9711     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9712     if ((!FirstInitializer && SecondInitializer) ||
9713         (FirstInitializer && !SecondInitializer)) {
9714       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9715                        FirstField->getSourceRange(), FieldSingleInitializer)
9716           << FirstII << (FirstInitializer != nullptr);
9717       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9718                       SecondField->getSourceRange(), FieldSingleInitializer)
9719           << SecondII << (SecondInitializer != nullptr);
9720       return true;
9721     }
9722 
9723     if (FirstInitializer && SecondInitializer) {
9724       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9725       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9726       if (FirstInitHash != SecondInitHash) {
9727         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9728                          FirstField->getSourceRange(),
9729                          FieldDifferentInitializers)
9730             << FirstII << FirstInitializer->getSourceRange();
9731         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9732                         SecondField->getSourceRange(),
9733                         FieldDifferentInitializers)
9734             << SecondII << SecondInitializer->getSourceRange();
9735         return true;
9736       }
9737     }
9738 
9739     return false;
9740   };
9741 
9742   auto ODRDiagTypeDefOrAlias =
9743       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9744           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9745           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9746           bool IsTypeAlias) {
9747         auto FirstName = FirstTD->getDeclName();
9748         auto SecondName = SecondTD->getDeclName();
9749         if (FirstName != SecondName) {
9750           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9751                            FirstTD->getSourceRange(), TypedefName)
9752               << IsTypeAlias << FirstName;
9753           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9754                           SecondTD->getSourceRange(), TypedefName)
9755               << IsTypeAlias << SecondName;
9756           return true;
9757         }
9758 
9759         QualType FirstType = FirstTD->getUnderlyingType();
9760         QualType SecondType = SecondTD->getUnderlyingType();
9761         if (ComputeQualTypeODRHash(FirstType) !=
9762             ComputeQualTypeODRHash(SecondType)) {
9763           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9764                            FirstTD->getSourceRange(), TypedefType)
9765               << IsTypeAlias << FirstName << FirstType;
9766           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9767                           SecondTD->getSourceRange(), TypedefType)
9768               << IsTypeAlias << SecondName << SecondType;
9769           return true;
9770         }
9771 
9772         return false;
9773   };
9774 
9775   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9776                      &ComputeQualTypeODRHash, &ComputeODRHash,
9777                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9778                            StringRef SecondModule, VarDecl *FirstVD,
9779                            VarDecl *SecondVD) {
9780     auto FirstName = FirstVD->getDeclName();
9781     auto SecondName = SecondVD->getDeclName();
9782     if (FirstName != SecondName) {
9783       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9784                        FirstVD->getSourceRange(), VarName)
9785           << FirstName;
9786       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9787                       SecondVD->getSourceRange(), VarName)
9788           << SecondName;
9789       return true;
9790     }
9791 
9792     QualType FirstType = FirstVD->getType();
9793     QualType SecondType = SecondVD->getType();
9794     if (ComputeQualTypeODRHash(FirstType) !=
9795         ComputeQualTypeODRHash(SecondType)) {
9796       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9797                        FirstVD->getSourceRange(), VarType)
9798           << FirstName << FirstType;
9799       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9800                       SecondVD->getSourceRange(), VarType)
9801           << SecondName << SecondType;
9802       return true;
9803     }
9804 
9805     if (!PP.getLangOpts().CPlusPlus)
9806       return false;
9807 
9808     const Expr *FirstInit = FirstVD->getInit();
9809     const Expr *SecondInit = SecondVD->getInit();
9810     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9811       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9812                        FirstVD->getSourceRange(), VarSingleInitializer)
9813           << FirstName << (FirstInit == nullptr)
9814           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9815       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9816                       SecondVD->getSourceRange(), VarSingleInitializer)
9817           << SecondName << (SecondInit == nullptr)
9818           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9819       return true;
9820     }
9821 
9822     if (FirstInit && SecondInit &&
9823         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9824       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9825                        FirstVD->getSourceRange(), VarDifferentInitializer)
9826           << FirstName << FirstInit->getSourceRange();
9827       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9828                       SecondVD->getSourceRange(), VarDifferentInitializer)
9829           << SecondName << SecondInit->getSourceRange();
9830       return true;
9831     }
9832 
9833     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9834     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9835     if (FirstIsConstexpr != SecondIsConstexpr) {
9836       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9837                        FirstVD->getSourceRange(), VarConstexpr)
9838           << FirstName << FirstIsConstexpr;
9839       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9840                       SecondVD->getSourceRange(), VarConstexpr)
9841           << SecondName << SecondIsConstexpr;
9842       return true;
9843     }
9844     return false;
9845   };
9846 
9847   auto DifferenceSelector = [](Decl *D) {
9848     assert(D && "valid Decl required");
9849     switch (D->getKind()) {
9850     default:
9851       return Other;
9852     case Decl::AccessSpec:
9853       switch (D->getAccess()) {
9854       case AS_public:
9855         return PublicSpecifer;
9856       case AS_private:
9857         return PrivateSpecifer;
9858       case AS_protected:
9859         return ProtectedSpecifer;
9860       case AS_none:
9861         break;
9862       }
9863       llvm_unreachable("Invalid access specifier");
9864     case Decl::StaticAssert:
9865       return StaticAssert;
9866     case Decl::Field:
9867       return Field;
9868     case Decl::CXXMethod:
9869     case Decl::CXXConstructor:
9870     case Decl::CXXDestructor:
9871       return CXXMethod;
9872     case Decl::TypeAlias:
9873       return TypeAlias;
9874     case Decl::Typedef:
9875       return TypeDef;
9876     case Decl::Var:
9877       return Var;
9878     case Decl::Friend:
9879       return Friend;
9880     case Decl::FunctionTemplate:
9881       return FunctionTemplate;
9882     }
9883   };
9884 
9885   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9886   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9887                                                  RecordDecl *Record,
9888                                                  const DeclContext *DC) {
9889     for (auto *D : Record->decls()) {
9890       if (!ODRHash::isDeclToBeProcessed(D, DC))
9891         continue;
9892       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9893     }
9894   };
9895 
9896   struct DiffResult {
9897     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9898     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9899   };
9900 
9901   // If there is a diagnoseable difference, FirstDiffType and
9902   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9903   // filled in if not EndOfClass.
9904   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9905                                              DeclHashes &SecondHashes) {
9906     DiffResult DR;
9907     auto FirstIt = FirstHashes.begin();
9908     auto SecondIt = SecondHashes.begin();
9909     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9910       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9911           FirstIt->second == SecondIt->second) {
9912         ++FirstIt;
9913         ++SecondIt;
9914         continue;
9915       }
9916 
9917       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9918       DR.SecondDecl =
9919           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9920 
9921       DR.FirstDiffType =
9922           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9923       DR.SecondDiffType =
9924           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9925       return DR;
9926     }
9927     return DR;
9928   };
9929 
9930   // Use this to diagnose that an unexpected Decl was encountered
9931   // or no difference was detected. This causes a generic error
9932   // message to be emitted.
9933   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9934                                       StringRef FirstModule,
9935                                       NamedDecl *SecondRecord,
9936                                       StringRef SecondModule) {
9937     Diag(FirstRecord->getLocation(),
9938          diag::err_module_odr_violation_different_definitions)
9939         << FirstRecord << FirstModule.empty() << FirstModule;
9940 
9941     if (DR.FirstDecl) {
9942       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9943           << FirstRecord << DR.FirstDecl->getSourceRange();
9944     }
9945 
9946     Diag(SecondRecord->getLocation(),
9947          diag::note_module_odr_violation_different_definitions)
9948         << SecondModule;
9949 
9950     if (DR.SecondDecl) {
9951       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9952           << DR.SecondDecl->getSourceRange();
9953     }
9954   };
9955 
9956   auto DiagnoseODRMismatch =
9957       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9958              NamedDecl *SecondRecord, StringRef SecondModule) {
9959         SourceLocation FirstLoc;
9960         SourceRange FirstRange;
9961         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9962         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9963           FirstLoc = FirstTag->getBraceRange().getEnd();
9964         } else {
9965           FirstLoc = DR.FirstDecl->getLocation();
9966           FirstRange = DR.FirstDecl->getSourceRange();
9967         }
9968         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9969             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9970             << DR.FirstDiffType;
9971 
9972         SourceLocation SecondLoc;
9973         SourceRange SecondRange;
9974         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9975         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9976           SecondLoc = SecondTag->getBraceRange().getEnd();
9977         } else {
9978           SecondLoc = DR.SecondDecl->getLocation();
9979           SecondRange = DR.SecondDecl->getSourceRange();
9980         }
9981         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9982             << SecondModule << SecondRange << DR.SecondDiffType;
9983       };
9984 
9985   // Issue any pending ODR-failure diagnostics.
9986   for (auto &Merge : OdrMergeFailures) {
9987     // If we've already pointed out a specific problem with this class, don't
9988     // bother issuing a general "something's different" diagnostic.
9989     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9990       continue;
9991 
9992     bool Diagnosed = false;
9993     CXXRecordDecl *FirstRecord = Merge.first;
9994     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9995     for (auto &RecordPair : Merge.second) {
9996       CXXRecordDecl *SecondRecord = RecordPair.first;
9997       // Multiple different declarations got merged together; tell the user
9998       // where they came from.
9999       if (FirstRecord == SecondRecord)
10000         continue;
10001 
10002       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10003 
10004       auto *FirstDD = FirstRecord->DefinitionData;
10005       auto *SecondDD = RecordPair.second;
10006 
10007       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10008 
10009       // Diagnostics from DefinitionData are emitted here.
10010       if (FirstDD != SecondDD) {
10011         enum ODRDefinitionDataDifference {
10012           NumBases,
10013           NumVBases,
10014           BaseType,
10015           BaseVirtual,
10016           BaseAccess,
10017         };
10018         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10019                                  this](SourceLocation Loc, SourceRange Range,
10020                                        ODRDefinitionDataDifference DiffType) {
10021           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10022                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10023                  << DiffType;
10024         };
10025         auto ODRDiagBaseNote = [&SecondModule,
10026                                 this](SourceLocation Loc, SourceRange Range,
10027                                       ODRDefinitionDataDifference DiffType) {
10028           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10029                  << SecondModule << Range << DiffType;
10030         };
10031 
10032         unsigned FirstNumBases = FirstDD->NumBases;
10033         unsigned FirstNumVBases = FirstDD->NumVBases;
10034         unsigned SecondNumBases = SecondDD->NumBases;
10035         unsigned SecondNumVBases = SecondDD->NumVBases;
10036 
10037         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10038           unsigned NumBases = DD->NumBases;
10039           if (NumBases == 0) return SourceRange();
10040           auto bases = DD->bases();
10041           return SourceRange(bases[0].getBeginLoc(),
10042                              bases[NumBases - 1].getEndLoc());
10043         };
10044 
10045         if (FirstNumBases != SecondNumBases) {
10046           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10047                            NumBases)
10048               << FirstNumBases;
10049           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10050                           NumBases)
10051               << SecondNumBases;
10052           Diagnosed = true;
10053           break;
10054         }
10055 
10056         if (FirstNumVBases != SecondNumVBases) {
10057           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10058                            NumVBases)
10059               << FirstNumVBases;
10060           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10061                           NumVBases)
10062               << SecondNumVBases;
10063           Diagnosed = true;
10064           break;
10065         }
10066 
10067         auto FirstBases = FirstDD->bases();
10068         auto SecondBases = SecondDD->bases();
10069         unsigned i = 0;
10070         for (i = 0; i < FirstNumBases; ++i) {
10071           auto FirstBase = FirstBases[i];
10072           auto SecondBase = SecondBases[i];
10073           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10074               ComputeQualTypeODRHash(SecondBase.getType())) {
10075             ODRDiagBaseError(FirstRecord->getLocation(),
10076                              FirstBase.getSourceRange(), BaseType)
10077                 << (i + 1) << FirstBase.getType();
10078             ODRDiagBaseNote(SecondRecord->getLocation(),
10079                             SecondBase.getSourceRange(), BaseType)
10080                 << (i + 1) << SecondBase.getType();
10081             break;
10082           }
10083 
10084           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10085             ODRDiagBaseError(FirstRecord->getLocation(),
10086                              FirstBase.getSourceRange(), BaseVirtual)
10087                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10088             ODRDiagBaseNote(SecondRecord->getLocation(),
10089                             SecondBase.getSourceRange(), BaseVirtual)
10090                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10091             break;
10092           }
10093 
10094           if (FirstBase.getAccessSpecifierAsWritten() !=
10095               SecondBase.getAccessSpecifierAsWritten()) {
10096             ODRDiagBaseError(FirstRecord->getLocation(),
10097                              FirstBase.getSourceRange(), BaseAccess)
10098                 << (i + 1) << FirstBase.getType()
10099                 << (int)FirstBase.getAccessSpecifierAsWritten();
10100             ODRDiagBaseNote(SecondRecord->getLocation(),
10101                             SecondBase.getSourceRange(), BaseAccess)
10102                 << (i + 1) << SecondBase.getType()
10103                 << (int)SecondBase.getAccessSpecifierAsWritten();
10104             break;
10105           }
10106         }
10107 
10108         if (i != FirstNumBases) {
10109           Diagnosed = true;
10110           break;
10111         }
10112       }
10113 
10114       const ClassTemplateDecl *FirstTemplate =
10115           FirstRecord->getDescribedClassTemplate();
10116       const ClassTemplateDecl *SecondTemplate =
10117           SecondRecord->getDescribedClassTemplate();
10118 
10119       assert(!FirstTemplate == !SecondTemplate &&
10120              "Both pointers should be null or non-null");
10121 
10122       enum ODRTemplateDifference {
10123         ParamEmptyName,
10124         ParamName,
10125         ParamSingleDefaultArgument,
10126         ParamDifferentDefaultArgument,
10127       };
10128 
10129       if (FirstTemplate && SecondTemplate) {
10130         DeclHashes FirstTemplateHashes;
10131         DeclHashes SecondTemplateHashes;
10132 
10133         auto PopulateTemplateParameterHashs =
10134             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10135                                      const ClassTemplateDecl *TD) {
10136               for (auto *D : TD->getTemplateParameters()->asArray()) {
10137                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10138               }
10139             };
10140 
10141         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10142         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10143 
10144         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10145                "Number of template parameters should be equal.");
10146 
10147         auto FirstIt = FirstTemplateHashes.begin();
10148         auto FirstEnd = FirstTemplateHashes.end();
10149         auto SecondIt = SecondTemplateHashes.begin();
10150         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10151           if (FirstIt->second == SecondIt->second)
10152             continue;
10153 
10154           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10155                                           SourceLocation Loc, SourceRange Range,
10156                                           ODRTemplateDifference DiffType) {
10157             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10158                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10159                    << DiffType;
10160           };
10161           auto ODRDiagTemplateNote = [&SecondModule, this](
10162                                          SourceLocation Loc, SourceRange Range,
10163                                          ODRTemplateDifference DiffType) {
10164             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10165                    << SecondModule << Range << DiffType;
10166           };
10167 
10168           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10169           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10170 
10171           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10172                  "Parameter Decl's should be the same kind.");
10173 
10174           DeclarationName FirstName = FirstDecl->getDeclName();
10175           DeclarationName SecondName = SecondDecl->getDeclName();
10176 
10177           if (FirstName != SecondName) {
10178             const bool FirstNameEmpty =
10179                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10180             const bool SecondNameEmpty =
10181                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10182             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10183                    "Both template parameters cannot be unnamed.");
10184             ODRDiagTemplateError(FirstDecl->getLocation(),
10185                                  FirstDecl->getSourceRange(),
10186                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10187                 << FirstName;
10188             ODRDiagTemplateNote(SecondDecl->getLocation(),
10189                                 SecondDecl->getSourceRange(),
10190                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10191                 << SecondName;
10192             break;
10193           }
10194 
10195           switch (FirstDecl->getKind()) {
10196           default:
10197             llvm_unreachable("Invalid template parameter type.");
10198           case Decl::TemplateTypeParm: {
10199             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10200             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10201             const bool HasFirstDefaultArgument =
10202                 FirstParam->hasDefaultArgument() &&
10203                 !FirstParam->defaultArgumentWasInherited();
10204             const bool HasSecondDefaultArgument =
10205                 SecondParam->hasDefaultArgument() &&
10206                 !SecondParam->defaultArgumentWasInherited();
10207 
10208             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10209               ODRDiagTemplateError(FirstDecl->getLocation(),
10210                                    FirstDecl->getSourceRange(),
10211                                    ParamSingleDefaultArgument)
10212                   << HasFirstDefaultArgument;
10213               ODRDiagTemplateNote(SecondDecl->getLocation(),
10214                                   SecondDecl->getSourceRange(),
10215                                   ParamSingleDefaultArgument)
10216                   << HasSecondDefaultArgument;
10217               break;
10218             }
10219 
10220             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10221                    "Expecting default arguments.");
10222 
10223             ODRDiagTemplateError(FirstDecl->getLocation(),
10224                                  FirstDecl->getSourceRange(),
10225                                  ParamDifferentDefaultArgument);
10226             ODRDiagTemplateNote(SecondDecl->getLocation(),
10227                                 SecondDecl->getSourceRange(),
10228                                 ParamDifferentDefaultArgument);
10229 
10230             break;
10231           }
10232           case Decl::NonTypeTemplateParm: {
10233             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10234             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10235             const bool HasFirstDefaultArgument =
10236                 FirstParam->hasDefaultArgument() &&
10237                 !FirstParam->defaultArgumentWasInherited();
10238             const bool HasSecondDefaultArgument =
10239                 SecondParam->hasDefaultArgument() &&
10240                 !SecondParam->defaultArgumentWasInherited();
10241 
10242             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10243               ODRDiagTemplateError(FirstDecl->getLocation(),
10244                                    FirstDecl->getSourceRange(),
10245                                    ParamSingleDefaultArgument)
10246                   << HasFirstDefaultArgument;
10247               ODRDiagTemplateNote(SecondDecl->getLocation(),
10248                                   SecondDecl->getSourceRange(),
10249                                   ParamSingleDefaultArgument)
10250                   << HasSecondDefaultArgument;
10251               break;
10252             }
10253 
10254             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10255                    "Expecting default arguments.");
10256 
10257             ODRDiagTemplateError(FirstDecl->getLocation(),
10258                                  FirstDecl->getSourceRange(),
10259                                  ParamDifferentDefaultArgument);
10260             ODRDiagTemplateNote(SecondDecl->getLocation(),
10261                                 SecondDecl->getSourceRange(),
10262                                 ParamDifferentDefaultArgument);
10263 
10264             break;
10265           }
10266           case Decl::TemplateTemplateParm: {
10267             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10268             const auto *SecondParam =
10269                 cast<TemplateTemplateParmDecl>(SecondDecl);
10270             const bool HasFirstDefaultArgument =
10271                 FirstParam->hasDefaultArgument() &&
10272                 !FirstParam->defaultArgumentWasInherited();
10273             const bool HasSecondDefaultArgument =
10274                 SecondParam->hasDefaultArgument() &&
10275                 !SecondParam->defaultArgumentWasInherited();
10276 
10277             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10278               ODRDiagTemplateError(FirstDecl->getLocation(),
10279                                    FirstDecl->getSourceRange(),
10280                                    ParamSingleDefaultArgument)
10281                   << HasFirstDefaultArgument;
10282               ODRDiagTemplateNote(SecondDecl->getLocation(),
10283                                   SecondDecl->getSourceRange(),
10284                                   ParamSingleDefaultArgument)
10285                   << HasSecondDefaultArgument;
10286               break;
10287             }
10288 
10289             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10290                    "Expecting default arguments.");
10291 
10292             ODRDiagTemplateError(FirstDecl->getLocation(),
10293                                  FirstDecl->getSourceRange(),
10294                                  ParamDifferentDefaultArgument);
10295             ODRDiagTemplateNote(SecondDecl->getLocation(),
10296                                 SecondDecl->getSourceRange(),
10297                                 ParamDifferentDefaultArgument);
10298 
10299             break;
10300           }
10301           }
10302 
10303           break;
10304         }
10305 
10306         if (FirstIt != FirstEnd) {
10307           Diagnosed = true;
10308           break;
10309         }
10310       }
10311 
10312       DeclHashes FirstHashes;
10313       DeclHashes SecondHashes;
10314       const DeclContext *DC = FirstRecord;
10315       PopulateHashes(FirstHashes, FirstRecord, DC);
10316       PopulateHashes(SecondHashes, SecondRecord, DC);
10317 
10318       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10319       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10320       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10321       Decl *FirstDecl = DR.FirstDecl;
10322       Decl *SecondDecl = DR.SecondDecl;
10323 
10324       if (FirstDiffType == Other || SecondDiffType == Other) {
10325         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10326                               SecondModule);
10327         Diagnosed = true;
10328         break;
10329       }
10330 
10331       if (FirstDiffType != SecondDiffType) {
10332         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10333                             SecondModule);
10334         Diagnosed = true;
10335         break;
10336       }
10337 
10338       assert(FirstDiffType == SecondDiffType);
10339 
10340       switch (FirstDiffType) {
10341       case Other:
10342       case EndOfClass:
10343       case PublicSpecifer:
10344       case PrivateSpecifer:
10345       case ProtectedSpecifer:
10346         llvm_unreachable("Invalid diff type");
10347 
10348       case StaticAssert: {
10349         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10350         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10351 
10352         Expr *FirstExpr = FirstSA->getAssertExpr();
10353         Expr *SecondExpr = SecondSA->getAssertExpr();
10354         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10355         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10356         if (FirstODRHash != SecondODRHash) {
10357           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10358                            FirstExpr->getSourceRange(), StaticAssertCondition);
10359           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10360                           SecondExpr->getSourceRange(), StaticAssertCondition);
10361           Diagnosed = true;
10362           break;
10363         }
10364 
10365         StringLiteral *FirstStr = FirstSA->getMessage();
10366         StringLiteral *SecondStr = SecondSA->getMessage();
10367         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10368         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10369           SourceLocation FirstLoc, SecondLoc;
10370           SourceRange FirstRange, SecondRange;
10371           if (FirstStr) {
10372             FirstLoc = FirstStr->getBeginLoc();
10373             FirstRange = FirstStr->getSourceRange();
10374           } else {
10375             FirstLoc = FirstSA->getBeginLoc();
10376             FirstRange = FirstSA->getSourceRange();
10377           }
10378           if (SecondStr) {
10379             SecondLoc = SecondStr->getBeginLoc();
10380             SecondRange = SecondStr->getSourceRange();
10381           } else {
10382             SecondLoc = SecondSA->getBeginLoc();
10383             SecondRange = SecondSA->getSourceRange();
10384           }
10385           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10386                            StaticAssertOnlyMessage)
10387               << (FirstStr == nullptr);
10388           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10389                           StaticAssertOnlyMessage)
10390               << (SecondStr == nullptr);
10391           Diagnosed = true;
10392           break;
10393         }
10394 
10395         if (FirstStr && SecondStr &&
10396             FirstStr->getString() != SecondStr->getString()) {
10397           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10398                            FirstStr->getSourceRange(), StaticAssertMessage);
10399           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10400                           SecondStr->getSourceRange(), StaticAssertMessage);
10401           Diagnosed = true;
10402           break;
10403         }
10404         break;
10405       }
10406       case Field: {
10407         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10408                                  cast<FieldDecl>(FirstDecl),
10409                                  cast<FieldDecl>(SecondDecl));
10410         break;
10411       }
10412       case CXXMethod: {
10413         enum {
10414           DiagMethod,
10415           DiagConstructor,
10416           DiagDestructor,
10417         } FirstMethodType,
10418             SecondMethodType;
10419         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10420           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10421           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10422           return DiagMethod;
10423         };
10424         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10425         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10426         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10427         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10428         auto FirstName = FirstMethod->getDeclName();
10429         auto SecondName = SecondMethod->getDeclName();
10430         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10431           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10432                            FirstMethod->getSourceRange(), MethodName)
10433               << FirstMethodType << FirstName;
10434           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10435                           SecondMethod->getSourceRange(), MethodName)
10436               << SecondMethodType << SecondName;
10437 
10438           Diagnosed = true;
10439           break;
10440         }
10441 
10442         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10443         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10444         if (FirstDeleted != SecondDeleted) {
10445           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10446                            FirstMethod->getSourceRange(), MethodDeleted)
10447               << FirstMethodType << FirstName << FirstDeleted;
10448 
10449           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10450                           SecondMethod->getSourceRange(), MethodDeleted)
10451               << SecondMethodType << SecondName << SecondDeleted;
10452           Diagnosed = true;
10453           break;
10454         }
10455 
10456         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10457         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10458         if (FirstDefaulted != SecondDefaulted) {
10459           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10460                            FirstMethod->getSourceRange(), MethodDefaulted)
10461               << FirstMethodType << FirstName << FirstDefaulted;
10462 
10463           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10464                           SecondMethod->getSourceRange(), MethodDefaulted)
10465               << SecondMethodType << SecondName << SecondDefaulted;
10466           Diagnosed = true;
10467           break;
10468         }
10469 
10470         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10471         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10472         const bool FirstPure = FirstMethod->isPure();
10473         const bool SecondPure = SecondMethod->isPure();
10474         if ((FirstVirtual || SecondVirtual) &&
10475             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10476           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10477                            FirstMethod->getSourceRange(), MethodVirtual)
10478               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10479           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10480                           SecondMethod->getSourceRange(), MethodVirtual)
10481               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10482           Diagnosed = true;
10483           break;
10484         }
10485 
10486         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10487         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10488         // class needs to be checked instead.
10489         const auto FirstStorage = FirstMethod->getStorageClass();
10490         const auto SecondStorage = SecondMethod->getStorageClass();
10491         const bool FirstStatic = FirstStorage == SC_Static;
10492         const bool SecondStatic = SecondStorage == SC_Static;
10493         if (FirstStatic != SecondStatic) {
10494           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10495                            FirstMethod->getSourceRange(), MethodStatic)
10496               << FirstMethodType << FirstName << FirstStatic;
10497           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10498                           SecondMethod->getSourceRange(), MethodStatic)
10499               << SecondMethodType << SecondName << SecondStatic;
10500           Diagnosed = true;
10501           break;
10502         }
10503 
10504         const bool FirstVolatile = FirstMethod->isVolatile();
10505         const bool SecondVolatile = SecondMethod->isVolatile();
10506         if (FirstVolatile != SecondVolatile) {
10507           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10508                            FirstMethod->getSourceRange(), MethodVolatile)
10509               << FirstMethodType << FirstName << FirstVolatile;
10510           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10511                           SecondMethod->getSourceRange(), MethodVolatile)
10512               << SecondMethodType << SecondName << SecondVolatile;
10513           Diagnosed = true;
10514           break;
10515         }
10516 
10517         const bool FirstConst = FirstMethod->isConst();
10518         const bool SecondConst = SecondMethod->isConst();
10519         if (FirstConst != SecondConst) {
10520           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10521                            FirstMethod->getSourceRange(), MethodConst)
10522               << FirstMethodType << FirstName << FirstConst;
10523           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10524                           SecondMethod->getSourceRange(), MethodConst)
10525               << SecondMethodType << SecondName << SecondConst;
10526           Diagnosed = true;
10527           break;
10528         }
10529 
10530         const bool FirstInline = FirstMethod->isInlineSpecified();
10531         const bool SecondInline = SecondMethod->isInlineSpecified();
10532         if (FirstInline != SecondInline) {
10533           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10534                            FirstMethod->getSourceRange(), MethodInline)
10535               << FirstMethodType << FirstName << FirstInline;
10536           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10537                           SecondMethod->getSourceRange(), MethodInline)
10538               << SecondMethodType << SecondName << SecondInline;
10539           Diagnosed = true;
10540           break;
10541         }
10542 
10543         const unsigned FirstNumParameters = FirstMethod->param_size();
10544         const unsigned SecondNumParameters = SecondMethod->param_size();
10545         if (FirstNumParameters != SecondNumParameters) {
10546           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10547                            FirstMethod->getSourceRange(),
10548                            MethodNumberParameters)
10549               << FirstMethodType << FirstName << FirstNumParameters;
10550           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10551                           SecondMethod->getSourceRange(),
10552                           MethodNumberParameters)
10553               << SecondMethodType << SecondName << SecondNumParameters;
10554           Diagnosed = true;
10555           break;
10556         }
10557 
10558         // Need this status boolean to know when break out of the switch.
10559         bool ParameterMismatch = false;
10560         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10561           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10562           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10563 
10564           QualType FirstParamType = FirstParam->getType();
10565           QualType SecondParamType = SecondParam->getType();
10566           if (FirstParamType != SecondParamType &&
10567               ComputeQualTypeODRHash(FirstParamType) !=
10568                   ComputeQualTypeODRHash(SecondParamType)) {
10569             if (const DecayedType *ParamDecayedType =
10570                     FirstParamType->getAs<DecayedType>()) {
10571               ODRDiagDeclError(
10572                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10573                   FirstMethod->getSourceRange(), MethodParameterType)
10574                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10575                   << true << ParamDecayedType->getOriginalType();
10576             } else {
10577               ODRDiagDeclError(
10578                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10579                   FirstMethod->getSourceRange(), MethodParameterType)
10580                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10581                   << false;
10582             }
10583 
10584             if (const DecayedType *ParamDecayedType =
10585                     SecondParamType->getAs<DecayedType>()) {
10586               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10587                               SecondMethod->getSourceRange(),
10588                               MethodParameterType)
10589                   << SecondMethodType << SecondName << (I + 1)
10590                   << SecondParamType << true
10591                   << ParamDecayedType->getOriginalType();
10592             } else {
10593               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10594                               SecondMethod->getSourceRange(),
10595                               MethodParameterType)
10596                   << SecondMethodType << SecondName << (I + 1)
10597                   << SecondParamType << false;
10598             }
10599             ParameterMismatch = true;
10600             break;
10601           }
10602 
10603           DeclarationName FirstParamName = FirstParam->getDeclName();
10604           DeclarationName SecondParamName = SecondParam->getDeclName();
10605           if (FirstParamName != SecondParamName) {
10606             ODRDiagDeclError(FirstRecord, FirstModule,
10607                              FirstMethod->getLocation(),
10608                              FirstMethod->getSourceRange(), MethodParameterName)
10609                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10610             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10611                             SecondMethod->getSourceRange(), MethodParameterName)
10612                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10613             ParameterMismatch = true;
10614             break;
10615           }
10616 
10617           const Expr *FirstInit = FirstParam->getInit();
10618           const Expr *SecondInit = SecondParam->getInit();
10619           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10620             ODRDiagDeclError(FirstRecord, FirstModule,
10621                              FirstMethod->getLocation(),
10622                              FirstMethod->getSourceRange(),
10623                              MethodParameterSingleDefaultArgument)
10624                 << FirstMethodType << FirstName << (I + 1)
10625                 << (FirstInit == nullptr)
10626                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10627             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10628                             SecondMethod->getSourceRange(),
10629                             MethodParameterSingleDefaultArgument)
10630                 << SecondMethodType << SecondName << (I + 1)
10631                 << (SecondInit == nullptr)
10632                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10633             ParameterMismatch = true;
10634             break;
10635           }
10636 
10637           if (FirstInit && SecondInit &&
10638               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10639             ODRDiagDeclError(FirstRecord, FirstModule,
10640                              FirstMethod->getLocation(),
10641                              FirstMethod->getSourceRange(),
10642                              MethodParameterDifferentDefaultArgument)
10643                 << FirstMethodType << FirstName << (I + 1)
10644                 << FirstInit->getSourceRange();
10645             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10646                             SecondMethod->getSourceRange(),
10647                             MethodParameterDifferentDefaultArgument)
10648                 << SecondMethodType << SecondName << (I + 1)
10649                 << SecondInit->getSourceRange();
10650             ParameterMismatch = true;
10651             break;
10652 
10653           }
10654         }
10655 
10656         if (ParameterMismatch) {
10657           Diagnosed = true;
10658           break;
10659         }
10660 
10661         const auto *FirstTemplateArgs =
10662             FirstMethod->getTemplateSpecializationArgs();
10663         const auto *SecondTemplateArgs =
10664             SecondMethod->getTemplateSpecializationArgs();
10665 
10666         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10667             (!FirstTemplateArgs && SecondTemplateArgs)) {
10668           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10669                            FirstMethod->getSourceRange(),
10670                            MethodNoTemplateArguments)
10671               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10672           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10673                           SecondMethod->getSourceRange(),
10674                           MethodNoTemplateArguments)
10675               << SecondMethodType << SecondName
10676               << (SecondTemplateArgs != nullptr);
10677 
10678           Diagnosed = true;
10679           break;
10680         }
10681 
10682         if (FirstTemplateArgs && SecondTemplateArgs) {
10683           // Remove pack expansions from argument list.
10684           auto ExpandTemplateArgumentList =
10685               [](const TemplateArgumentList *TAL) {
10686                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10687                 for (const TemplateArgument &TA : TAL->asArray()) {
10688                   if (TA.getKind() != TemplateArgument::Pack) {
10689                     ExpandedList.push_back(&TA);
10690                     continue;
10691                   }
10692                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10693                     ExpandedList.push_back(&PackTA);
10694                   }
10695                 }
10696                 return ExpandedList;
10697               };
10698           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10699               ExpandTemplateArgumentList(FirstTemplateArgs);
10700           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10701               ExpandTemplateArgumentList(SecondTemplateArgs);
10702 
10703           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10704             ODRDiagDeclError(FirstRecord, FirstModule,
10705                              FirstMethod->getLocation(),
10706                              FirstMethod->getSourceRange(),
10707                              MethodDifferentNumberTemplateArguments)
10708                 << FirstMethodType << FirstName
10709                 << (unsigned)FirstExpandedList.size();
10710             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10711                             SecondMethod->getSourceRange(),
10712                             MethodDifferentNumberTemplateArguments)
10713                 << SecondMethodType << SecondName
10714                 << (unsigned)SecondExpandedList.size();
10715 
10716             Diagnosed = true;
10717             break;
10718           }
10719 
10720           bool TemplateArgumentMismatch = false;
10721           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10722             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10723                                    &SecondTA = *SecondExpandedList[i];
10724             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10725                 ComputeTemplateArgumentODRHash(SecondTA)) {
10726               continue;
10727             }
10728 
10729             ODRDiagDeclError(
10730                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10731                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10732                 << FirstMethodType << FirstName << FirstTA << i + 1;
10733             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10734                             SecondMethod->getSourceRange(),
10735                             MethodDifferentTemplateArgument)
10736                 << SecondMethodType << SecondName << SecondTA << i + 1;
10737 
10738             TemplateArgumentMismatch = true;
10739             break;
10740           }
10741 
10742           if (TemplateArgumentMismatch) {
10743             Diagnosed = true;
10744             break;
10745           }
10746         }
10747 
10748         // Compute the hash of the method as if it has no body.
10749         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10750           Hash.clear();
10751           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10752           return Hash.CalculateHash();
10753         };
10754 
10755         // Compare the hash generated to the hash stored.  A difference means
10756         // that a body was present in the original source.  Due to merging,
10757         // the stardard way of detecting a body will not work.
10758         const bool HasFirstBody =
10759             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10760         const bool HasSecondBody =
10761             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10762 
10763         if (HasFirstBody != HasSecondBody) {
10764           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10765                            FirstMethod->getSourceRange(), MethodSingleBody)
10766               << FirstMethodType << FirstName << HasFirstBody;
10767           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10768                           SecondMethod->getSourceRange(), MethodSingleBody)
10769               << SecondMethodType << SecondName << HasSecondBody;
10770           Diagnosed = true;
10771           break;
10772         }
10773 
10774         if (HasFirstBody && HasSecondBody) {
10775           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10776                            FirstMethod->getSourceRange(), MethodDifferentBody)
10777               << FirstMethodType << FirstName;
10778           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10779                           SecondMethod->getSourceRange(), MethodDifferentBody)
10780               << SecondMethodType << SecondName;
10781           Diagnosed = true;
10782           break;
10783         }
10784 
10785         break;
10786       }
10787       case TypeAlias:
10788       case TypeDef: {
10789         Diagnosed = ODRDiagTypeDefOrAlias(
10790             FirstRecord, FirstModule, SecondModule,
10791             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10792             FirstDiffType == TypeAlias);
10793         break;
10794       }
10795       case Var: {
10796         Diagnosed =
10797             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10798                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10799         break;
10800       }
10801       case Friend: {
10802         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10803         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10804 
10805         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10806         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10807 
10808         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10809         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10810 
10811         if (FirstND && SecondND) {
10812           ODRDiagDeclError(FirstRecord, FirstModule,
10813                            FirstFriend->getFriendLoc(),
10814                            FirstFriend->getSourceRange(), FriendFunction)
10815               << FirstND;
10816           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10817                           SecondFriend->getSourceRange(), FriendFunction)
10818               << SecondND;
10819 
10820           Diagnosed = true;
10821           break;
10822         }
10823 
10824         if (FirstTSI && SecondTSI) {
10825           QualType FirstFriendType = FirstTSI->getType();
10826           QualType SecondFriendType = SecondTSI->getType();
10827           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10828                  ComputeQualTypeODRHash(SecondFriendType));
10829           ODRDiagDeclError(FirstRecord, FirstModule,
10830                            FirstFriend->getFriendLoc(),
10831                            FirstFriend->getSourceRange(), FriendType)
10832               << FirstFriendType;
10833           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10834                           SecondFriend->getSourceRange(), FriendType)
10835               << SecondFriendType;
10836           Diagnosed = true;
10837           break;
10838         }
10839 
10840         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10841                          FirstFriend->getSourceRange(), FriendTypeFunction)
10842             << (FirstTSI == nullptr);
10843         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10844                         SecondFriend->getSourceRange(), FriendTypeFunction)
10845             << (SecondTSI == nullptr);
10846 
10847         Diagnosed = true;
10848         break;
10849       }
10850       case FunctionTemplate: {
10851         FunctionTemplateDecl *FirstTemplate =
10852             cast<FunctionTemplateDecl>(FirstDecl);
10853         FunctionTemplateDecl *SecondTemplate =
10854             cast<FunctionTemplateDecl>(SecondDecl);
10855 
10856         TemplateParameterList *FirstTPL =
10857             FirstTemplate->getTemplateParameters();
10858         TemplateParameterList *SecondTPL =
10859             SecondTemplate->getTemplateParameters();
10860 
10861         if (FirstTPL->size() != SecondTPL->size()) {
10862           ODRDiagDeclError(FirstRecord, FirstModule,
10863                            FirstTemplate->getLocation(),
10864                            FirstTemplate->getSourceRange(),
10865                            FunctionTemplateDifferentNumberParameters)
10866               << FirstTemplate << FirstTPL->size();
10867           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10868                           SecondTemplate->getSourceRange(),
10869                           FunctionTemplateDifferentNumberParameters)
10870               << SecondTemplate << SecondTPL->size();
10871 
10872           Diagnosed = true;
10873           break;
10874         }
10875 
10876         bool ParameterMismatch = false;
10877         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10878           NamedDecl *FirstParam = FirstTPL->getParam(i);
10879           NamedDecl *SecondParam = SecondTPL->getParam(i);
10880 
10881           if (FirstParam->getKind() != SecondParam->getKind()) {
10882             enum {
10883               TemplateTypeParameter,
10884               NonTypeTemplateParameter,
10885               TemplateTemplateParameter,
10886             };
10887             auto GetParamType = [](NamedDecl *D) {
10888               switch (D->getKind()) {
10889                 default:
10890                   llvm_unreachable("Unexpected template parameter type");
10891                 case Decl::TemplateTypeParm:
10892                   return TemplateTypeParameter;
10893                 case Decl::NonTypeTemplateParm:
10894                   return NonTypeTemplateParameter;
10895                 case Decl::TemplateTemplateParm:
10896                   return TemplateTemplateParameter;
10897               }
10898             };
10899 
10900             ODRDiagDeclError(FirstRecord, FirstModule,
10901                              FirstTemplate->getLocation(),
10902                              FirstTemplate->getSourceRange(),
10903                              FunctionTemplateParameterDifferentKind)
10904                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10905             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10906                             SecondTemplate->getSourceRange(),
10907                             FunctionTemplateParameterDifferentKind)
10908                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10909 
10910             ParameterMismatch = true;
10911             break;
10912           }
10913 
10914           if (FirstParam->getName() != SecondParam->getName()) {
10915             ODRDiagDeclError(
10916                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10917                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10918                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10919                 << FirstParam;
10920             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10921                             SecondTemplate->getSourceRange(),
10922                             FunctionTemplateParameterName)
10923                 << SecondTemplate << (i + 1)
10924                 << (bool)SecondParam->getIdentifier() << SecondParam;
10925             ParameterMismatch = true;
10926             break;
10927           }
10928 
10929           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10930               isa<TemplateTypeParmDecl>(SecondParam)) {
10931             TemplateTypeParmDecl *FirstTTPD =
10932                 cast<TemplateTypeParmDecl>(FirstParam);
10933             TemplateTypeParmDecl *SecondTTPD =
10934                 cast<TemplateTypeParmDecl>(SecondParam);
10935             bool HasFirstDefaultArgument =
10936                 FirstTTPD->hasDefaultArgument() &&
10937                 !FirstTTPD->defaultArgumentWasInherited();
10938             bool HasSecondDefaultArgument =
10939                 SecondTTPD->hasDefaultArgument() &&
10940                 !SecondTTPD->defaultArgumentWasInherited();
10941             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10942               ODRDiagDeclError(FirstRecord, FirstModule,
10943                                FirstTemplate->getLocation(),
10944                                FirstTemplate->getSourceRange(),
10945                                FunctionTemplateParameterSingleDefaultArgument)
10946                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10947               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10948                               SecondTemplate->getSourceRange(),
10949                               FunctionTemplateParameterSingleDefaultArgument)
10950                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10951               ParameterMismatch = true;
10952               break;
10953             }
10954 
10955             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10956               QualType FirstType = FirstTTPD->getDefaultArgument();
10957               QualType SecondType = SecondTTPD->getDefaultArgument();
10958               if (ComputeQualTypeODRHash(FirstType) !=
10959                   ComputeQualTypeODRHash(SecondType)) {
10960                 ODRDiagDeclError(
10961                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10962                     FirstTemplate->getSourceRange(),
10963                     FunctionTemplateParameterDifferentDefaultArgument)
10964                     << FirstTemplate << (i + 1) << FirstType;
10965                 ODRDiagDeclNote(
10966                     SecondModule, SecondTemplate->getLocation(),
10967                     SecondTemplate->getSourceRange(),
10968                     FunctionTemplateParameterDifferentDefaultArgument)
10969                     << SecondTemplate << (i + 1) << SecondType;
10970                 ParameterMismatch = true;
10971                 break;
10972               }
10973             }
10974 
10975             if (FirstTTPD->isParameterPack() !=
10976                 SecondTTPD->isParameterPack()) {
10977               ODRDiagDeclError(FirstRecord, FirstModule,
10978                                FirstTemplate->getLocation(),
10979                                FirstTemplate->getSourceRange(),
10980                                FunctionTemplatePackParameter)
10981                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10982               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10983                               SecondTemplate->getSourceRange(),
10984                               FunctionTemplatePackParameter)
10985                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10986               ParameterMismatch = true;
10987               break;
10988             }
10989           }
10990 
10991           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10992               isa<TemplateTemplateParmDecl>(SecondParam)) {
10993             TemplateTemplateParmDecl *FirstTTPD =
10994                 cast<TemplateTemplateParmDecl>(FirstParam);
10995             TemplateTemplateParmDecl *SecondTTPD =
10996                 cast<TemplateTemplateParmDecl>(SecondParam);
10997 
10998             TemplateParameterList *FirstTPL =
10999                 FirstTTPD->getTemplateParameters();
11000             TemplateParameterList *SecondTPL =
11001                 SecondTTPD->getTemplateParameters();
11002 
11003             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11004                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11005               ODRDiagDeclError(FirstRecord, FirstModule,
11006                                FirstTemplate->getLocation(),
11007                                FirstTemplate->getSourceRange(),
11008                                FunctionTemplateParameterDifferentType)
11009                   << FirstTemplate << (i + 1);
11010               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11011                               SecondTemplate->getSourceRange(),
11012                               FunctionTemplateParameterDifferentType)
11013                   << SecondTemplate << (i + 1);
11014               ParameterMismatch = true;
11015               break;
11016             }
11017 
11018             bool HasFirstDefaultArgument =
11019                 FirstTTPD->hasDefaultArgument() &&
11020                 !FirstTTPD->defaultArgumentWasInherited();
11021             bool HasSecondDefaultArgument =
11022                 SecondTTPD->hasDefaultArgument() &&
11023                 !SecondTTPD->defaultArgumentWasInherited();
11024             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11025               ODRDiagDeclError(FirstRecord, FirstModule,
11026                                FirstTemplate->getLocation(),
11027                                FirstTemplate->getSourceRange(),
11028                                FunctionTemplateParameterSingleDefaultArgument)
11029                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11030               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11031                               SecondTemplate->getSourceRange(),
11032                               FunctionTemplateParameterSingleDefaultArgument)
11033                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11034               ParameterMismatch = true;
11035               break;
11036             }
11037 
11038             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11039               TemplateArgument FirstTA =
11040                   FirstTTPD->getDefaultArgument().getArgument();
11041               TemplateArgument SecondTA =
11042                   SecondTTPD->getDefaultArgument().getArgument();
11043               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11044                   ComputeTemplateArgumentODRHash(SecondTA)) {
11045                 ODRDiagDeclError(
11046                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11047                     FirstTemplate->getSourceRange(),
11048                     FunctionTemplateParameterDifferentDefaultArgument)
11049                     << FirstTemplate << (i + 1) << FirstTA;
11050                 ODRDiagDeclNote(
11051                     SecondModule, SecondTemplate->getLocation(),
11052                     SecondTemplate->getSourceRange(),
11053                     FunctionTemplateParameterDifferentDefaultArgument)
11054                     << SecondTemplate << (i + 1) << SecondTA;
11055                 ParameterMismatch = true;
11056                 break;
11057               }
11058             }
11059 
11060             if (FirstTTPD->isParameterPack() !=
11061                 SecondTTPD->isParameterPack()) {
11062               ODRDiagDeclError(FirstRecord, FirstModule,
11063                                FirstTemplate->getLocation(),
11064                                FirstTemplate->getSourceRange(),
11065                                FunctionTemplatePackParameter)
11066                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11067               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11068                               SecondTemplate->getSourceRange(),
11069                               FunctionTemplatePackParameter)
11070                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11071               ParameterMismatch = true;
11072               break;
11073             }
11074           }
11075 
11076           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11077               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11078             NonTypeTemplateParmDecl *FirstNTTPD =
11079                 cast<NonTypeTemplateParmDecl>(FirstParam);
11080             NonTypeTemplateParmDecl *SecondNTTPD =
11081                 cast<NonTypeTemplateParmDecl>(SecondParam);
11082 
11083             QualType FirstType = FirstNTTPD->getType();
11084             QualType SecondType = SecondNTTPD->getType();
11085             if (ComputeQualTypeODRHash(FirstType) !=
11086                 ComputeQualTypeODRHash(SecondType)) {
11087               ODRDiagDeclError(FirstRecord, FirstModule,
11088                                FirstTemplate->getLocation(),
11089                                FirstTemplate->getSourceRange(),
11090                                FunctionTemplateParameterDifferentType)
11091                   << FirstTemplate << (i + 1);
11092               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11093                               SecondTemplate->getSourceRange(),
11094                               FunctionTemplateParameterDifferentType)
11095                   << SecondTemplate << (i + 1);
11096               ParameterMismatch = true;
11097               break;
11098             }
11099 
11100             bool HasFirstDefaultArgument =
11101                 FirstNTTPD->hasDefaultArgument() &&
11102                 !FirstNTTPD->defaultArgumentWasInherited();
11103             bool HasSecondDefaultArgument =
11104                 SecondNTTPD->hasDefaultArgument() &&
11105                 !SecondNTTPD->defaultArgumentWasInherited();
11106             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11107               ODRDiagDeclError(FirstRecord, FirstModule,
11108                                FirstTemplate->getLocation(),
11109                                FirstTemplate->getSourceRange(),
11110                                FunctionTemplateParameterSingleDefaultArgument)
11111                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11112               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11113                               SecondTemplate->getSourceRange(),
11114                               FunctionTemplateParameterSingleDefaultArgument)
11115                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11116               ParameterMismatch = true;
11117               break;
11118             }
11119 
11120             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11121               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11122               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11123               if (ComputeODRHash(FirstDefaultArgument) !=
11124                   ComputeODRHash(SecondDefaultArgument)) {
11125                 ODRDiagDeclError(
11126                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11127                     FirstTemplate->getSourceRange(),
11128                     FunctionTemplateParameterDifferentDefaultArgument)
11129                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11130                 ODRDiagDeclNote(
11131                     SecondModule, SecondTemplate->getLocation(),
11132                     SecondTemplate->getSourceRange(),
11133                     FunctionTemplateParameterDifferentDefaultArgument)
11134                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11135                 ParameterMismatch = true;
11136                 break;
11137               }
11138             }
11139 
11140             if (FirstNTTPD->isParameterPack() !=
11141                 SecondNTTPD->isParameterPack()) {
11142               ODRDiagDeclError(FirstRecord, FirstModule,
11143                                FirstTemplate->getLocation(),
11144                                FirstTemplate->getSourceRange(),
11145                                FunctionTemplatePackParameter)
11146                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11147               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11148                               SecondTemplate->getSourceRange(),
11149                               FunctionTemplatePackParameter)
11150                   << SecondTemplate << (i + 1)
11151                   << SecondNTTPD->isParameterPack();
11152               ParameterMismatch = true;
11153               break;
11154             }
11155           }
11156         }
11157 
11158         if (ParameterMismatch) {
11159           Diagnosed = true;
11160           break;
11161         }
11162 
11163         break;
11164       }
11165       }
11166 
11167       if (Diagnosed)
11168         continue;
11169 
11170       Diag(FirstDecl->getLocation(),
11171            diag::err_module_odr_violation_mismatch_decl_unknown)
11172           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11173           << FirstDecl->getSourceRange();
11174       Diag(SecondDecl->getLocation(),
11175            diag::note_module_odr_violation_mismatch_decl_unknown)
11176           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11177       Diagnosed = true;
11178     }
11179 
11180     if (!Diagnosed) {
11181       // All definitions are updates to the same declaration. This happens if a
11182       // module instantiates the declaration of a class template specialization
11183       // and two or more other modules instantiate its definition.
11184       //
11185       // FIXME: Indicate which modules had instantiations of this definition.
11186       // FIXME: How can this even happen?
11187       Diag(Merge.first->getLocation(),
11188            diag::err_module_odr_violation_different_instantiations)
11189         << Merge.first;
11190     }
11191   }
11192 
11193   // Issue ODR failures diagnostics for functions.
11194   for (auto &Merge : FunctionOdrMergeFailures) {
11195     enum ODRFunctionDifference {
11196       ReturnType,
11197       ParameterName,
11198       ParameterType,
11199       ParameterSingleDefaultArgument,
11200       ParameterDifferentDefaultArgument,
11201       FunctionBody,
11202     };
11203 
11204     FunctionDecl *FirstFunction = Merge.first;
11205     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11206 
11207     bool Diagnosed = false;
11208     for (auto &SecondFunction : Merge.second) {
11209 
11210       if (FirstFunction == SecondFunction)
11211         continue;
11212 
11213       std::string SecondModule =
11214           getOwningModuleNameForDiagnostic(SecondFunction);
11215 
11216       auto ODRDiagError = [FirstFunction, &FirstModule,
11217                            this](SourceLocation Loc, SourceRange Range,
11218                                  ODRFunctionDifference DiffType) {
11219         return Diag(Loc, diag::err_module_odr_violation_function)
11220                << FirstFunction << FirstModule.empty() << FirstModule << Range
11221                << DiffType;
11222       };
11223       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11224                                                SourceRange Range,
11225                                                ODRFunctionDifference DiffType) {
11226         return Diag(Loc, diag::note_module_odr_violation_function)
11227                << SecondModule << Range << DiffType;
11228       };
11229 
11230       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11231           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11232         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11233                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11234             << FirstFunction->getReturnType();
11235         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11236                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11237             << SecondFunction->getReturnType();
11238         Diagnosed = true;
11239         break;
11240       }
11241 
11242       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11243              "Merged functions with different number of parameters");
11244 
11245       auto ParamSize = FirstFunction->param_size();
11246       bool ParameterMismatch = false;
11247       for (unsigned I = 0; I < ParamSize; ++I) {
11248         auto *FirstParam = FirstFunction->getParamDecl(I);
11249         auto *SecondParam = SecondFunction->getParamDecl(I);
11250 
11251         assert(getContext().hasSameType(FirstParam->getType(),
11252                                       SecondParam->getType()) &&
11253                "Merged function has different parameter types.");
11254 
11255         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11256           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11257                        ParameterName)
11258               << I + 1 << FirstParam->getDeclName();
11259           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11260                       ParameterName)
11261               << I + 1 << SecondParam->getDeclName();
11262           ParameterMismatch = true;
11263           break;
11264         };
11265 
11266         QualType FirstParamType = FirstParam->getType();
11267         QualType SecondParamType = SecondParam->getType();
11268         if (FirstParamType != SecondParamType &&
11269             ComputeQualTypeODRHash(FirstParamType) !=
11270                 ComputeQualTypeODRHash(SecondParamType)) {
11271           if (const DecayedType *ParamDecayedType =
11272                   FirstParamType->getAs<DecayedType>()) {
11273             ODRDiagError(FirstParam->getLocation(),
11274                          FirstParam->getSourceRange(), ParameterType)
11275                 << (I + 1) << FirstParamType << true
11276                 << ParamDecayedType->getOriginalType();
11277           } else {
11278             ODRDiagError(FirstParam->getLocation(),
11279                          FirstParam->getSourceRange(), ParameterType)
11280                 << (I + 1) << FirstParamType << false;
11281           }
11282 
11283           if (const DecayedType *ParamDecayedType =
11284                   SecondParamType->getAs<DecayedType>()) {
11285             ODRDiagNote(SecondParam->getLocation(),
11286                         SecondParam->getSourceRange(), ParameterType)
11287                 << (I + 1) << SecondParamType << true
11288                 << ParamDecayedType->getOriginalType();
11289           } else {
11290             ODRDiagNote(SecondParam->getLocation(),
11291                         SecondParam->getSourceRange(), ParameterType)
11292                 << (I + 1) << SecondParamType << false;
11293           }
11294           ParameterMismatch = true;
11295           break;
11296         }
11297 
11298         const Expr *FirstInit = FirstParam->getInit();
11299         const Expr *SecondInit = SecondParam->getInit();
11300         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11301           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11302                        ParameterSingleDefaultArgument)
11303               << (I + 1) << (FirstInit == nullptr)
11304               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11305           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11306                       ParameterSingleDefaultArgument)
11307               << (I + 1) << (SecondInit == nullptr)
11308               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11309           ParameterMismatch = true;
11310           break;
11311         }
11312 
11313         if (FirstInit && SecondInit &&
11314             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11315           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11316                        ParameterDifferentDefaultArgument)
11317               << (I + 1) << FirstInit->getSourceRange();
11318           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11319                       ParameterDifferentDefaultArgument)
11320               << (I + 1) << SecondInit->getSourceRange();
11321           ParameterMismatch = true;
11322           break;
11323         }
11324 
11325         assert(ComputeSubDeclODRHash(FirstParam) ==
11326                    ComputeSubDeclODRHash(SecondParam) &&
11327                "Undiagnosed parameter difference.");
11328       }
11329 
11330       if (ParameterMismatch) {
11331         Diagnosed = true;
11332         break;
11333       }
11334 
11335       // If no error has been generated before now, assume the problem is in
11336       // the body and generate a message.
11337       ODRDiagError(FirstFunction->getLocation(),
11338                    FirstFunction->getSourceRange(), FunctionBody);
11339       ODRDiagNote(SecondFunction->getLocation(),
11340                   SecondFunction->getSourceRange(), FunctionBody);
11341       Diagnosed = true;
11342       break;
11343     }
11344     (void)Diagnosed;
11345     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11346   }
11347 
11348   // Issue ODR failures diagnostics for enums.
11349   for (auto &Merge : EnumOdrMergeFailures) {
11350     enum ODREnumDifference {
11351       SingleScopedEnum,
11352       EnumTagKeywordMismatch,
11353       SingleSpecifiedType,
11354       DifferentSpecifiedTypes,
11355       DifferentNumberEnumConstants,
11356       EnumConstantName,
11357       EnumConstantSingleInitilizer,
11358       EnumConstantDifferentInitilizer,
11359     };
11360 
11361     // If we've already pointed out a specific problem with this enum, don't
11362     // bother issuing a general "something's different" diagnostic.
11363     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11364       continue;
11365 
11366     EnumDecl *FirstEnum = Merge.first;
11367     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11368 
11369     using DeclHashes =
11370         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11371     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11372                               DeclHashes &Hashes, EnumDecl *Enum) {
11373       for (auto *D : Enum->decls()) {
11374         // Due to decl merging, the first EnumDecl is the parent of
11375         // Decls in both records.
11376         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11377           continue;
11378         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11379         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11380                             ComputeSubDeclODRHash(D));
11381       }
11382     };
11383     DeclHashes FirstHashes;
11384     PopulateHashes(FirstHashes, FirstEnum);
11385     bool Diagnosed = false;
11386     for (auto &SecondEnum : Merge.second) {
11387 
11388       if (FirstEnum == SecondEnum)
11389         continue;
11390 
11391       std::string SecondModule =
11392           getOwningModuleNameForDiagnostic(SecondEnum);
11393 
11394       auto ODRDiagError = [FirstEnum, &FirstModule,
11395                            this](SourceLocation Loc, SourceRange Range,
11396                                  ODREnumDifference DiffType) {
11397         return Diag(Loc, diag::err_module_odr_violation_enum)
11398                << FirstEnum << FirstModule.empty() << FirstModule << Range
11399                << DiffType;
11400       };
11401       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11402                                                SourceRange Range,
11403                                                ODREnumDifference DiffType) {
11404         return Diag(Loc, diag::note_module_odr_violation_enum)
11405                << SecondModule << Range << DiffType;
11406       };
11407 
11408       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11409         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11410                      SingleScopedEnum)
11411             << FirstEnum->isScoped();
11412         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11413                     SingleScopedEnum)
11414             << SecondEnum->isScoped();
11415         Diagnosed = true;
11416         continue;
11417       }
11418 
11419       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11420         if (FirstEnum->isScopedUsingClassTag() !=
11421             SecondEnum->isScopedUsingClassTag()) {
11422           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11423                        EnumTagKeywordMismatch)
11424               << FirstEnum->isScopedUsingClassTag();
11425           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11426                       EnumTagKeywordMismatch)
11427               << SecondEnum->isScopedUsingClassTag();
11428           Diagnosed = true;
11429           continue;
11430         }
11431       }
11432 
11433       QualType FirstUnderlyingType =
11434           FirstEnum->getIntegerTypeSourceInfo()
11435               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11436               : QualType();
11437       QualType SecondUnderlyingType =
11438           SecondEnum->getIntegerTypeSourceInfo()
11439               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11440               : QualType();
11441       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11442           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11443                        SingleSpecifiedType)
11444               << !FirstUnderlyingType.isNull();
11445           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11446                       SingleSpecifiedType)
11447               << !SecondUnderlyingType.isNull();
11448           Diagnosed = true;
11449           continue;
11450       }
11451 
11452       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11453         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11454             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11455           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11456                        DifferentSpecifiedTypes)
11457               << FirstUnderlyingType;
11458           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11459                       DifferentSpecifiedTypes)
11460               << SecondUnderlyingType;
11461           Diagnosed = true;
11462           continue;
11463         }
11464       }
11465 
11466       DeclHashes SecondHashes;
11467       PopulateHashes(SecondHashes, SecondEnum);
11468 
11469       if (FirstHashes.size() != SecondHashes.size()) {
11470         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11471                      DifferentNumberEnumConstants)
11472             << (int)FirstHashes.size();
11473         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11474                     DifferentNumberEnumConstants)
11475             << (int)SecondHashes.size();
11476         Diagnosed = true;
11477         continue;
11478       }
11479 
11480       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11481         if (FirstHashes[I].second == SecondHashes[I].second)
11482           continue;
11483         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11484         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11485 
11486         if (FirstEnumConstant->getDeclName() !=
11487             SecondEnumConstant->getDeclName()) {
11488 
11489           ODRDiagError(FirstEnumConstant->getLocation(),
11490                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11491               << I + 1 << FirstEnumConstant;
11492           ODRDiagNote(SecondEnumConstant->getLocation(),
11493                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11494               << I + 1 << SecondEnumConstant;
11495           Diagnosed = true;
11496           break;
11497         }
11498 
11499         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11500         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11501         if (!FirstInit && !SecondInit)
11502           continue;
11503 
11504         if (!FirstInit || !SecondInit) {
11505           ODRDiagError(FirstEnumConstant->getLocation(),
11506                        FirstEnumConstant->getSourceRange(),
11507                        EnumConstantSingleInitilizer)
11508               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11509           ODRDiagNote(SecondEnumConstant->getLocation(),
11510                       SecondEnumConstant->getSourceRange(),
11511                       EnumConstantSingleInitilizer)
11512               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11513           Diagnosed = true;
11514           break;
11515         }
11516 
11517         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11518           ODRDiagError(FirstEnumConstant->getLocation(),
11519                        FirstEnumConstant->getSourceRange(),
11520                        EnumConstantDifferentInitilizer)
11521               << I + 1 << FirstEnumConstant;
11522           ODRDiagNote(SecondEnumConstant->getLocation(),
11523                       SecondEnumConstant->getSourceRange(),
11524                       EnumConstantDifferentInitilizer)
11525               << I + 1 << SecondEnumConstant;
11526           Diagnosed = true;
11527           break;
11528         }
11529       }
11530     }
11531 
11532     (void)Diagnosed;
11533     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11534   }
11535 }
11536 
11537 void ASTReader::StartedDeserializing() {
11538   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11539     ReadTimer->startTimer();
11540 }
11541 
11542 void ASTReader::FinishedDeserializing() {
11543   assert(NumCurrentElementsDeserializing &&
11544          "FinishedDeserializing not paired with StartedDeserializing");
11545   if (NumCurrentElementsDeserializing == 1) {
11546     // We decrease NumCurrentElementsDeserializing only after pending actions
11547     // are finished, to avoid recursively re-calling finishPendingActions().
11548     finishPendingActions();
11549   }
11550   --NumCurrentElementsDeserializing;
11551 
11552   if (NumCurrentElementsDeserializing == 0) {
11553     // Propagate exception specification and deduced type updates along
11554     // redeclaration chains.
11555     //
11556     // We do this now rather than in finishPendingActions because we want to
11557     // be able to walk the complete redeclaration chains of the updated decls.
11558     while (!PendingExceptionSpecUpdates.empty() ||
11559            !PendingDeducedTypeUpdates.empty()) {
11560       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11561       PendingExceptionSpecUpdates.clear();
11562       for (auto Update : ESUpdates) {
11563         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11564         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11565         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11566         if (auto *Listener = getContext().getASTMutationListener())
11567           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11568         for (auto *Redecl : Update.second->redecls())
11569           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11570       }
11571 
11572       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11573       PendingDeducedTypeUpdates.clear();
11574       for (auto Update : DTUpdates) {
11575         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11576         // FIXME: If the return type is already deduced, check that it matches.
11577         getContext().adjustDeducedFunctionResultType(Update.first,
11578                                                      Update.second);
11579       }
11580     }
11581 
11582     if (ReadTimer)
11583       ReadTimer->stopTimer();
11584 
11585     diagnoseOdrViolations();
11586 
11587     // We are not in recursive loading, so it's safe to pass the "interesting"
11588     // decls to the consumer.
11589     if (Consumer)
11590       PassInterestingDeclsToConsumer();
11591   }
11592 }
11593 
11594 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11595   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11596     // Remove any fake results before adding any real ones.
11597     auto It = PendingFakeLookupResults.find(II);
11598     if (It != PendingFakeLookupResults.end()) {
11599       for (auto *ND : It->second)
11600         SemaObj->IdResolver.RemoveDecl(ND);
11601       // FIXME: this works around module+PCH performance issue.
11602       // Rather than erase the result from the map, which is O(n), just clear
11603       // the vector of NamedDecls.
11604       It->second.clear();
11605     }
11606   }
11607 
11608   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11609     SemaObj->TUScope->AddDecl(D);
11610   } else if (SemaObj->TUScope) {
11611     // Adding the decl to IdResolver may have failed because it was already in
11612     // (even though it was not added in scope). If it is already in, make sure
11613     // it gets in the scope as well.
11614     if (std::find(SemaObj->IdResolver.begin(Name),
11615                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11616       SemaObj->TUScope->AddDecl(D);
11617   }
11618 }
11619 
11620 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11621                      ASTContext *Context,
11622                      const PCHContainerReader &PCHContainerRdr,
11623                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11624                      StringRef isysroot,
11625                      DisableValidationForModuleKind DisableValidationKind,
11626                      bool AllowASTWithCompilerErrors,
11627                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11628                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11629                      std::unique_ptr<llvm::Timer> ReadTimer)
11630     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11631                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11632                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11633       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11634       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11635       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11636                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11637       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11638       DisableValidationKind(DisableValidationKind),
11639       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11640       AllowConfigurationMismatch(AllowConfigurationMismatch),
11641       ValidateSystemInputs(ValidateSystemInputs),
11642       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11643       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11644   SourceMgr.setExternalSLocEntrySource(this);
11645 
11646   for (const auto &Ext : Extensions) {
11647     auto BlockName = Ext->getExtensionMetadata().BlockName;
11648     auto Known = ModuleFileExtensions.find(BlockName);
11649     if (Known != ModuleFileExtensions.end()) {
11650       Diags.Report(diag::warn_duplicate_module_file_extension)
11651         << BlockName;
11652       continue;
11653     }
11654 
11655     ModuleFileExtensions.insert({BlockName, Ext});
11656   }
11657 }
11658 
11659 ASTReader::~ASTReader() {
11660   if (OwnsDeserializationListener)
11661     delete DeserializationListener;
11662 }
11663 
11664 IdentifierResolver &ASTReader::getIdResolver() {
11665   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11666 }
11667 
11668 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11669                                                unsigned AbbrevID) {
11670   Idx = 0;
11671   Record.clear();
11672   return Cursor.readRecord(AbbrevID, Record);
11673 }
11674 //===----------------------------------------------------------------------===//
11675 //// OMPClauseReader implementation
11676 ////===----------------------------------------------------------------------===//
11677 
11678 // This has to be in namespace clang because it's friended by all
11679 // of the OMP clauses.
11680 namespace clang {
11681 
11682 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11683   ASTRecordReader &Record;
11684   ASTContext &Context;
11685 
11686 public:
11687   OMPClauseReader(ASTRecordReader &Record)
11688       : Record(Record), Context(Record.getContext()) {}
11689 #define GEN_CLANG_CLAUSE_CLASS
11690 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11691 #include "llvm/Frontend/OpenMP/OMP.inc"
11692   OMPClause *readClause();
11693   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11694   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11695 };
11696 
11697 } // end namespace clang
11698 
11699 OMPClause *ASTRecordReader::readOMPClause() {
11700   return OMPClauseReader(*this).readClause();
11701 }
11702 
11703 OMPClause *OMPClauseReader::readClause() {
11704   OMPClause *C = nullptr;
11705   switch (llvm::omp::Clause(Record.readInt())) {
11706   case llvm::omp::OMPC_if:
11707     C = new (Context) OMPIfClause();
11708     break;
11709   case llvm::omp::OMPC_final:
11710     C = new (Context) OMPFinalClause();
11711     break;
11712   case llvm::omp::OMPC_num_threads:
11713     C = new (Context) OMPNumThreadsClause();
11714     break;
11715   case llvm::omp::OMPC_safelen:
11716     C = new (Context) OMPSafelenClause();
11717     break;
11718   case llvm::omp::OMPC_simdlen:
11719     C = new (Context) OMPSimdlenClause();
11720     break;
11721   case llvm::omp::OMPC_sizes: {
11722     unsigned NumSizes = Record.readInt();
11723     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11724     break;
11725   }
11726   case llvm::omp::OMPC_allocator:
11727     C = new (Context) OMPAllocatorClause();
11728     break;
11729   case llvm::omp::OMPC_collapse:
11730     C = new (Context) OMPCollapseClause();
11731     break;
11732   case llvm::omp::OMPC_default:
11733     C = new (Context) OMPDefaultClause();
11734     break;
11735   case llvm::omp::OMPC_proc_bind:
11736     C = new (Context) OMPProcBindClause();
11737     break;
11738   case llvm::omp::OMPC_schedule:
11739     C = new (Context) OMPScheduleClause();
11740     break;
11741   case llvm::omp::OMPC_ordered:
11742     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11743     break;
11744   case llvm::omp::OMPC_nowait:
11745     C = new (Context) OMPNowaitClause();
11746     break;
11747   case llvm::omp::OMPC_untied:
11748     C = new (Context) OMPUntiedClause();
11749     break;
11750   case llvm::omp::OMPC_mergeable:
11751     C = new (Context) OMPMergeableClause();
11752     break;
11753   case llvm::omp::OMPC_read:
11754     C = new (Context) OMPReadClause();
11755     break;
11756   case llvm::omp::OMPC_write:
11757     C = new (Context) OMPWriteClause();
11758     break;
11759   case llvm::omp::OMPC_update:
11760     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11761     break;
11762   case llvm::omp::OMPC_capture:
11763     C = new (Context) OMPCaptureClause();
11764     break;
11765   case llvm::omp::OMPC_seq_cst:
11766     C = new (Context) OMPSeqCstClause();
11767     break;
11768   case llvm::omp::OMPC_acq_rel:
11769     C = new (Context) OMPAcqRelClause();
11770     break;
11771   case llvm::omp::OMPC_acquire:
11772     C = new (Context) OMPAcquireClause();
11773     break;
11774   case llvm::omp::OMPC_release:
11775     C = new (Context) OMPReleaseClause();
11776     break;
11777   case llvm::omp::OMPC_relaxed:
11778     C = new (Context) OMPRelaxedClause();
11779     break;
11780   case llvm::omp::OMPC_threads:
11781     C = new (Context) OMPThreadsClause();
11782     break;
11783   case llvm::omp::OMPC_simd:
11784     C = new (Context) OMPSIMDClause();
11785     break;
11786   case llvm::omp::OMPC_nogroup:
11787     C = new (Context) OMPNogroupClause();
11788     break;
11789   case llvm::omp::OMPC_unified_address:
11790     C = new (Context) OMPUnifiedAddressClause();
11791     break;
11792   case llvm::omp::OMPC_unified_shared_memory:
11793     C = new (Context) OMPUnifiedSharedMemoryClause();
11794     break;
11795   case llvm::omp::OMPC_reverse_offload:
11796     C = new (Context) OMPReverseOffloadClause();
11797     break;
11798   case llvm::omp::OMPC_dynamic_allocators:
11799     C = new (Context) OMPDynamicAllocatorsClause();
11800     break;
11801   case llvm::omp::OMPC_atomic_default_mem_order:
11802     C = new (Context) OMPAtomicDefaultMemOrderClause();
11803     break;
11804  case llvm::omp::OMPC_private:
11805     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11806     break;
11807   case llvm::omp::OMPC_firstprivate:
11808     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11809     break;
11810   case llvm::omp::OMPC_lastprivate:
11811     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11812     break;
11813   case llvm::omp::OMPC_shared:
11814     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11815     break;
11816   case llvm::omp::OMPC_reduction: {
11817     unsigned N = Record.readInt();
11818     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11819     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11820     break;
11821   }
11822   case llvm::omp::OMPC_task_reduction:
11823     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11824     break;
11825   case llvm::omp::OMPC_in_reduction:
11826     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11827     break;
11828   case llvm::omp::OMPC_linear:
11829     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11830     break;
11831   case llvm::omp::OMPC_aligned:
11832     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11833     break;
11834   case llvm::omp::OMPC_copyin:
11835     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11836     break;
11837   case llvm::omp::OMPC_copyprivate:
11838     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11839     break;
11840   case llvm::omp::OMPC_flush:
11841     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11842     break;
11843   case llvm::omp::OMPC_depobj:
11844     C = OMPDepobjClause::CreateEmpty(Context);
11845     break;
11846   case llvm::omp::OMPC_depend: {
11847     unsigned NumVars = Record.readInt();
11848     unsigned NumLoops = Record.readInt();
11849     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11850     break;
11851   }
11852   case llvm::omp::OMPC_device:
11853     C = new (Context) OMPDeviceClause();
11854     break;
11855   case llvm::omp::OMPC_map: {
11856     OMPMappableExprListSizeTy Sizes;
11857     Sizes.NumVars = Record.readInt();
11858     Sizes.NumUniqueDeclarations = Record.readInt();
11859     Sizes.NumComponentLists = Record.readInt();
11860     Sizes.NumComponents = Record.readInt();
11861     C = OMPMapClause::CreateEmpty(Context, Sizes);
11862     break;
11863   }
11864   case llvm::omp::OMPC_num_teams:
11865     C = new (Context) OMPNumTeamsClause();
11866     break;
11867   case llvm::omp::OMPC_thread_limit:
11868     C = new (Context) OMPThreadLimitClause();
11869     break;
11870   case llvm::omp::OMPC_priority:
11871     C = new (Context) OMPPriorityClause();
11872     break;
11873   case llvm::omp::OMPC_grainsize:
11874     C = new (Context) OMPGrainsizeClause();
11875     break;
11876   case llvm::omp::OMPC_num_tasks:
11877     C = new (Context) OMPNumTasksClause();
11878     break;
11879   case llvm::omp::OMPC_hint:
11880     C = new (Context) OMPHintClause();
11881     break;
11882   case llvm::omp::OMPC_dist_schedule:
11883     C = new (Context) OMPDistScheduleClause();
11884     break;
11885   case llvm::omp::OMPC_defaultmap:
11886     C = new (Context) OMPDefaultmapClause();
11887     break;
11888   case llvm::omp::OMPC_to: {
11889     OMPMappableExprListSizeTy Sizes;
11890     Sizes.NumVars = Record.readInt();
11891     Sizes.NumUniqueDeclarations = Record.readInt();
11892     Sizes.NumComponentLists = Record.readInt();
11893     Sizes.NumComponents = Record.readInt();
11894     C = OMPToClause::CreateEmpty(Context, Sizes);
11895     break;
11896   }
11897   case llvm::omp::OMPC_from: {
11898     OMPMappableExprListSizeTy Sizes;
11899     Sizes.NumVars = Record.readInt();
11900     Sizes.NumUniqueDeclarations = Record.readInt();
11901     Sizes.NumComponentLists = Record.readInt();
11902     Sizes.NumComponents = Record.readInt();
11903     C = OMPFromClause::CreateEmpty(Context, Sizes);
11904     break;
11905   }
11906   case llvm::omp::OMPC_use_device_ptr: {
11907     OMPMappableExprListSizeTy Sizes;
11908     Sizes.NumVars = Record.readInt();
11909     Sizes.NumUniqueDeclarations = Record.readInt();
11910     Sizes.NumComponentLists = Record.readInt();
11911     Sizes.NumComponents = Record.readInt();
11912     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11913     break;
11914   }
11915   case llvm::omp::OMPC_use_device_addr: {
11916     OMPMappableExprListSizeTy Sizes;
11917     Sizes.NumVars = Record.readInt();
11918     Sizes.NumUniqueDeclarations = Record.readInt();
11919     Sizes.NumComponentLists = Record.readInt();
11920     Sizes.NumComponents = Record.readInt();
11921     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11922     break;
11923   }
11924   case llvm::omp::OMPC_is_device_ptr: {
11925     OMPMappableExprListSizeTy Sizes;
11926     Sizes.NumVars = Record.readInt();
11927     Sizes.NumUniqueDeclarations = Record.readInt();
11928     Sizes.NumComponentLists = Record.readInt();
11929     Sizes.NumComponents = Record.readInt();
11930     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11931     break;
11932   }
11933   case llvm::omp::OMPC_allocate:
11934     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11935     break;
11936   case llvm::omp::OMPC_nontemporal:
11937     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11938     break;
11939   case llvm::omp::OMPC_inclusive:
11940     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11941     break;
11942   case llvm::omp::OMPC_exclusive:
11943     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11944     break;
11945   case llvm::omp::OMPC_order:
11946     C = new (Context) OMPOrderClause();
11947     break;
11948   case llvm::omp::OMPC_init:
11949     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11950     break;
11951   case llvm::omp::OMPC_use:
11952     C = new (Context) OMPUseClause();
11953     break;
11954   case llvm::omp::OMPC_destroy:
11955     C = new (Context) OMPDestroyClause();
11956     break;
11957   case llvm::omp::OMPC_novariants:
11958     C = new (Context) OMPNovariantsClause();
11959     break;
11960   case llvm::omp::OMPC_nocontext:
11961     C = new (Context) OMPNocontextClause();
11962     break;
11963   case llvm::omp::OMPC_detach:
11964     C = new (Context) OMPDetachClause();
11965     break;
11966   case llvm::omp::OMPC_uses_allocators:
11967     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11968     break;
11969   case llvm::omp::OMPC_affinity:
11970     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11971     break;
11972   case llvm::omp::OMPC_filter:
11973     C = new (Context) OMPFilterClause();
11974     break;
11975 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11976   case llvm::omp::Enum:                                                        \
11977     break;
11978 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11979   default:
11980     break;
11981   }
11982   assert(C && "Unknown OMPClause type");
11983 
11984   Visit(C);
11985   C->setLocStart(Record.readSourceLocation());
11986   C->setLocEnd(Record.readSourceLocation());
11987 
11988   return C;
11989 }
11990 
11991 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11992   C->setPreInitStmt(Record.readSubStmt(),
11993                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11994 }
11995 
11996 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11997   VisitOMPClauseWithPreInit(C);
11998   C->setPostUpdateExpr(Record.readSubExpr());
11999 }
12000 
12001 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12002   VisitOMPClauseWithPreInit(C);
12003   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12004   C->setNameModifierLoc(Record.readSourceLocation());
12005   C->setColonLoc(Record.readSourceLocation());
12006   C->setCondition(Record.readSubExpr());
12007   C->setLParenLoc(Record.readSourceLocation());
12008 }
12009 
12010 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12011   VisitOMPClauseWithPreInit(C);
12012   C->setCondition(Record.readSubExpr());
12013   C->setLParenLoc(Record.readSourceLocation());
12014 }
12015 
12016 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12017   VisitOMPClauseWithPreInit(C);
12018   C->setNumThreads(Record.readSubExpr());
12019   C->setLParenLoc(Record.readSourceLocation());
12020 }
12021 
12022 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12023   C->setSafelen(Record.readSubExpr());
12024   C->setLParenLoc(Record.readSourceLocation());
12025 }
12026 
12027 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12028   C->setSimdlen(Record.readSubExpr());
12029   C->setLParenLoc(Record.readSourceLocation());
12030 }
12031 
12032 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12033   for (Expr *&E : C->getSizesRefs())
12034     E = Record.readSubExpr();
12035   C->setLParenLoc(Record.readSourceLocation());
12036 }
12037 
12038 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12039   C->setAllocator(Record.readExpr());
12040   C->setLParenLoc(Record.readSourceLocation());
12041 }
12042 
12043 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12044   C->setNumForLoops(Record.readSubExpr());
12045   C->setLParenLoc(Record.readSourceLocation());
12046 }
12047 
12048 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12049   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12050   C->setLParenLoc(Record.readSourceLocation());
12051   C->setDefaultKindKwLoc(Record.readSourceLocation());
12052 }
12053 
12054 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12055   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12056   C->setLParenLoc(Record.readSourceLocation());
12057   C->setProcBindKindKwLoc(Record.readSourceLocation());
12058 }
12059 
12060 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12061   VisitOMPClauseWithPreInit(C);
12062   C->setScheduleKind(
12063        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12064   C->setFirstScheduleModifier(
12065       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12066   C->setSecondScheduleModifier(
12067       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12068   C->setChunkSize(Record.readSubExpr());
12069   C->setLParenLoc(Record.readSourceLocation());
12070   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12071   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12072   C->setScheduleKindLoc(Record.readSourceLocation());
12073   C->setCommaLoc(Record.readSourceLocation());
12074 }
12075 
12076 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12077   C->setNumForLoops(Record.readSubExpr());
12078   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12079     C->setLoopNumIterations(I, Record.readSubExpr());
12080   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12081     C->setLoopCounter(I, Record.readSubExpr());
12082   C->setLParenLoc(Record.readSourceLocation());
12083 }
12084 
12085 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12086   C->setEventHandler(Record.readSubExpr());
12087   C->setLParenLoc(Record.readSourceLocation());
12088 }
12089 
12090 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12091 
12092 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12093 
12094 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12095 
12096 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12097 
12098 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12099 
12100 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12101   if (C->isExtended()) {
12102     C->setLParenLoc(Record.readSourceLocation());
12103     C->setArgumentLoc(Record.readSourceLocation());
12104     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12105   }
12106 }
12107 
12108 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12109 
12110 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12111 
12112 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12113 
12114 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12115 
12116 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12117 
12118 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12119 
12120 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12121 
12122 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12123 
12124 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12125 
12126 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12127   unsigned NumVars = C->varlist_size();
12128   SmallVector<Expr *, 16> Vars;
12129   Vars.reserve(NumVars);
12130   for (unsigned I = 0; I != NumVars; ++I)
12131     Vars.push_back(Record.readSubExpr());
12132   C->setVarRefs(Vars);
12133   C->setIsTarget(Record.readBool());
12134   C->setIsTargetSync(Record.readBool());
12135   C->setLParenLoc(Record.readSourceLocation());
12136   C->setVarLoc(Record.readSourceLocation());
12137 }
12138 
12139 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12140   C->setInteropVar(Record.readSubExpr());
12141   C->setLParenLoc(Record.readSourceLocation());
12142   C->setVarLoc(Record.readSourceLocation());
12143 }
12144 
12145 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12146   C->setInteropVar(Record.readSubExpr());
12147   C->setLParenLoc(Record.readSourceLocation());
12148   C->setVarLoc(Record.readSourceLocation());
12149 }
12150 
12151 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12152   VisitOMPClauseWithPreInit(C);
12153   C->setCondition(Record.readSubExpr());
12154   C->setLParenLoc(Record.readSourceLocation());
12155 }
12156 
12157 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12158   VisitOMPClauseWithPreInit(C);
12159   C->setCondition(Record.readSubExpr());
12160   C->setLParenLoc(Record.readSourceLocation());
12161 }
12162 
12163 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12164 
12165 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12166     OMPUnifiedSharedMemoryClause *) {}
12167 
12168 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12169 
12170 void
12171 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12172 }
12173 
12174 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12175     OMPAtomicDefaultMemOrderClause *C) {
12176   C->setAtomicDefaultMemOrderKind(
12177       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12178   C->setLParenLoc(Record.readSourceLocation());
12179   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12180 }
12181 
12182 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12183   C->setLParenLoc(Record.readSourceLocation());
12184   unsigned NumVars = C->varlist_size();
12185   SmallVector<Expr *, 16> Vars;
12186   Vars.reserve(NumVars);
12187   for (unsigned i = 0; i != NumVars; ++i)
12188     Vars.push_back(Record.readSubExpr());
12189   C->setVarRefs(Vars);
12190   Vars.clear();
12191   for (unsigned i = 0; i != NumVars; ++i)
12192     Vars.push_back(Record.readSubExpr());
12193   C->setPrivateCopies(Vars);
12194 }
12195 
12196 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12197   VisitOMPClauseWithPreInit(C);
12198   C->setLParenLoc(Record.readSourceLocation());
12199   unsigned NumVars = C->varlist_size();
12200   SmallVector<Expr *, 16> Vars;
12201   Vars.reserve(NumVars);
12202   for (unsigned i = 0; i != NumVars; ++i)
12203     Vars.push_back(Record.readSubExpr());
12204   C->setVarRefs(Vars);
12205   Vars.clear();
12206   for (unsigned i = 0; i != NumVars; ++i)
12207     Vars.push_back(Record.readSubExpr());
12208   C->setPrivateCopies(Vars);
12209   Vars.clear();
12210   for (unsigned i = 0; i != NumVars; ++i)
12211     Vars.push_back(Record.readSubExpr());
12212   C->setInits(Vars);
12213 }
12214 
12215 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12216   VisitOMPClauseWithPostUpdate(C);
12217   C->setLParenLoc(Record.readSourceLocation());
12218   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12219   C->setKindLoc(Record.readSourceLocation());
12220   C->setColonLoc(Record.readSourceLocation());
12221   unsigned NumVars = C->varlist_size();
12222   SmallVector<Expr *, 16> Vars;
12223   Vars.reserve(NumVars);
12224   for (unsigned i = 0; i != NumVars; ++i)
12225     Vars.push_back(Record.readSubExpr());
12226   C->setVarRefs(Vars);
12227   Vars.clear();
12228   for (unsigned i = 0; i != NumVars; ++i)
12229     Vars.push_back(Record.readSubExpr());
12230   C->setPrivateCopies(Vars);
12231   Vars.clear();
12232   for (unsigned i = 0; i != NumVars; ++i)
12233     Vars.push_back(Record.readSubExpr());
12234   C->setSourceExprs(Vars);
12235   Vars.clear();
12236   for (unsigned i = 0; i != NumVars; ++i)
12237     Vars.push_back(Record.readSubExpr());
12238   C->setDestinationExprs(Vars);
12239   Vars.clear();
12240   for (unsigned i = 0; i != NumVars; ++i)
12241     Vars.push_back(Record.readSubExpr());
12242   C->setAssignmentOps(Vars);
12243 }
12244 
12245 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12246   C->setLParenLoc(Record.readSourceLocation());
12247   unsigned NumVars = C->varlist_size();
12248   SmallVector<Expr *, 16> Vars;
12249   Vars.reserve(NumVars);
12250   for (unsigned i = 0; i != NumVars; ++i)
12251     Vars.push_back(Record.readSubExpr());
12252   C->setVarRefs(Vars);
12253 }
12254 
12255 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12256   VisitOMPClauseWithPostUpdate(C);
12257   C->setLParenLoc(Record.readSourceLocation());
12258   C->setModifierLoc(Record.readSourceLocation());
12259   C->setColonLoc(Record.readSourceLocation());
12260   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12261   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12262   C->setQualifierLoc(NNSL);
12263   C->setNameInfo(DNI);
12264 
12265   unsigned NumVars = C->varlist_size();
12266   SmallVector<Expr *, 16> Vars;
12267   Vars.reserve(NumVars);
12268   for (unsigned i = 0; i != NumVars; ++i)
12269     Vars.push_back(Record.readSubExpr());
12270   C->setVarRefs(Vars);
12271   Vars.clear();
12272   for (unsigned i = 0; i != NumVars; ++i)
12273     Vars.push_back(Record.readSubExpr());
12274   C->setPrivates(Vars);
12275   Vars.clear();
12276   for (unsigned i = 0; i != NumVars; ++i)
12277     Vars.push_back(Record.readSubExpr());
12278   C->setLHSExprs(Vars);
12279   Vars.clear();
12280   for (unsigned i = 0; i != NumVars; ++i)
12281     Vars.push_back(Record.readSubExpr());
12282   C->setRHSExprs(Vars);
12283   Vars.clear();
12284   for (unsigned i = 0; i != NumVars; ++i)
12285     Vars.push_back(Record.readSubExpr());
12286   C->setReductionOps(Vars);
12287   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12288     Vars.clear();
12289     for (unsigned i = 0; i != NumVars; ++i)
12290       Vars.push_back(Record.readSubExpr());
12291     C->setInscanCopyOps(Vars);
12292     Vars.clear();
12293     for (unsigned i = 0; i != NumVars; ++i)
12294       Vars.push_back(Record.readSubExpr());
12295     C->setInscanCopyArrayTemps(Vars);
12296     Vars.clear();
12297     for (unsigned i = 0; i != NumVars; ++i)
12298       Vars.push_back(Record.readSubExpr());
12299     C->setInscanCopyArrayElems(Vars);
12300   }
12301 }
12302 
12303 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12304   VisitOMPClauseWithPostUpdate(C);
12305   C->setLParenLoc(Record.readSourceLocation());
12306   C->setColonLoc(Record.readSourceLocation());
12307   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12308   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12309   C->setQualifierLoc(NNSL);
12310   C->setNameInfo(DNI);
12311 
12312   unsigned NumVars = C->varlist_size();
12313   SmallVector<Expr *, 16> Vars;
12314   Vars.reserve(NumVars);
12315   for (unsigned I = 0; I != NumVars; ++I)
12316     Vars.push_back(Record.readSubExpr());
12317   C->setVarRefs(Vars);
12318   Vars.clear();
12319   for (unsigned I = 0; I != NumVars; ++I)
12320     Vars.push_back(Record.readSubExpr());
12321   C->setPrivates(Vars);
12322   Vars.clear();
12323   for (unsigned I = 0; I != NumVars; ++I)
12324     Vars.push_back(Record.readSubExpr());
12325   C->setLHSExprs(Vars);
12326   Vars.clear();
12327   for (unsigned I = 0; I != NumVars; ++I)
12328     Vars.push_back(Record.readSubExpr());
12329   C->setRHSExprs(Vars);
12330   Vars.clear();
12331   for (unsigned I = 0; I != NumVars; ++I)
12332     Vars.push_back(Record.readSubExpr());
12333   C->setReductionOps(Vars);
12334 }
12335 
12336 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12337   VisitOMPClauseWithPostUpdate(C);
12338   C->setLParenLoc(Record.readSourceLocation());
12339   C->setColonLoc(Record.readSourceLocation());
12340   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12341   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12342   C->setQualifierLoc(NNSL);
12343   C->setNameInfo(DNI);
12344 
12345   unsigned NumVars = C->varlist_size();
12346   SmallVector<Expr *, 16> Vars;
12347   Vars.reserve(NumVars);
12348   for (unsigned I = 0; I != NumVars; ++I)
12349     Vars.push_back(Record.readSubExpr());
12350   C->setVarRefs(Vars);
12351   Vars.clear();
12352   for (unsigned I = 0; I != NumVars; ++I)
12353     Vars.push_back(Record.readSubExpr());
12354   C->setPrivates(Vars);
12355   Vars.clear();
12356   for (unsigned I = 0; I != NumVars; ++I)
12357     Vars.push_back(Record.readSubExpr());
12358   C->setLHSExprs(Vars);
12359   Vars.clear();
12360   for (unsigned I = 0; I != NumVars; ++I)
12361     Vars.push_back(Record.readSubExpr());
12362   C->setRHSExprs(Vars);
12363   Vars.clear();
12364   for (unsigned I = 0; I != NumVars; ++I)
12365     Vars.push_back(Record.readSubExpr());
12366   C->setReductionOps(Vars);
12367   Vars.clear();
12368   for (unsigned I = 0; I != NumVars; ++I)
12369     Vars.push_back(Record.readSubExpr());
12370   C->setTaskgroupDescriptors(Vars);
12371 }
12372 
12373 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12374   VisitOMPClauseWithPostUpdate(C);
12375   C->setLParenLoc(Record.readSourceLocation());
12376   C->setColonLoc(Record.readSourceLocation());
12377   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12378   C->setModifierLoc(Record.readSourceLocation());
12379   unsigned NumVars = C->varlist_size();
12380   SmallVector<Expr *, 16> Vars;
12381   Vars.reserve(NumVars);
12382   for (unsigned i = 0; i != NumVars; ++i)
12383     Vars.push_back(Record.readSubExpr());
12384   C->setVarRefs(Vars);
12385   Vars.clear();
12386   for (unsigned i = 0; i != NumVars; ++i)
12387     Vars.push_back(Record.readSubExpr());
12388   C->setPrivates(Vars);
12389   Vars.clear();
12390   for (unsigned i = 0; i != NumVars; ++i)
12391     Vars.push_back(Record.readSubExpr());
12392   C->setInits(Vars);
12393   Vars.clear();
12394   for (unsigned i = 0; i != NumVars; ++i)
12395     Vars.push_back(Record.readSubExpr());
12396   C->setUpdates(Vars);
12397   Vars.clear();
12398   for (unsigned i = 0; i != NumVars; ++i)
12399     Vars.push_back(Record.readSubExpr());
12400   C->setFinals(Vars);
12401   C->setStep(Record.readSubExpr());
12402   C->setCalcStep(Record.readSubExpr());
12403   Vars.clear();
12404   for (unsigned I = 0; I != NumVars + 1; ++I)
12405     Vars.push_back(Record.readSubExpr());
12406   C->setUsedExprs(Vars);
12407 }
12408 
12409 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12410   C->setLParenLoc(Record.readSourceLocation());
12411   C->setColonLoc(Record.readSourceLocation());
12412   unsigned NumVars = C->varlist_size();
12413   SmallVector<Expr *, 16> Vars;
12414   Vars.reserve(NumVars);
12415   for (unsigned i = 0; i != NumVars; ++i)
12416     Vars.push_back(Record.readSubExpr());
12417   C->setVarRefs(Vars);
12418   C->setAlignment(Record.readSubExpr());
12419 }
12420 
12421 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12422   C->setLParenLoc(Record.readSourceLocation());
12423   unsigned NumVars = C->varlist_size();
12424   SmallVector<Expr *, 16> Exprs;
12425   Exprs.reserve(NumVars);
12426   for (unsigned i = 0; i != NumVars; ++i)
12427     Exprs.push_back(Record.readSubExpr());
12428   C->setVarRefs(Exprs);
12429   Exprs.clear();
12430   for (unsigned i = 0; i != NumVars; ++i)
12431     Exprs.push_back(Record.readSubExpr());
12432   C->setSourceExprs(Exprs);
12433   Exprs.clear();
12434   for (unsigned i = 0; i != NumVars; ++i)
12435     Exprs.push_back(Record.readSubExpr());
12436   C->setDestinationExprs(Exprs);
12437   Exprs.clear();
12438   for (unsigned i = 0; i != NumVars; ++i)
12439     Exprs.push_back(Record.readSubExpr());
12440   C->setAssignmentOps(Exprs);
12441 }
12442 
12443 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12444   C->setLParenLoc(Record.readSourceLocation());
12445   unsigned NumVars = C->varlist_size();
12446   SmallVector<Expr *, 16> Exprs;
12447   Exprs.reserve(NumVars);
12448   for (unsigned i = 0; i != NumVars; ++i)
12449     Exprs.push_back(Record.readSubExpr());
12450   C->setVarRefs(Exprs);
12451   Exprs.clear();
12452   for (unsigned i = 0; i != NumVars; ++i)
12453     Exprs.push_back(Record.readSubExpr());
12454   C->setSourceExprs(Exprs);
12455   Exprs.clear();
12456   for (unsigned i = 0; i != NumVars; ++i)
12457     Exprs.push_back(Record.readSubExpr());
12458   C->setDestinationExprs(Exprs);
12459   Exprs.clear();
12460   for (unsigned i = 0; i != NumVars; ++i)
12461     Exprs.push_back(Record.readSubExpr());
12462   C->setAssignmentOps(Exprs);
12463 }
12464 
12465 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12466   C->setLParenLoc(Record.readSourceLocation());
12467   unsigned NumVars = C->varlist_size();
12468   SmallVector<Expr *, 16> Vars;
12469   Vars.reserve(NumVars);
12470   for (unsigned i = 0; i != NumVars; ++i)
12471     Vars.push_back(Record.readSubExpr());
12472   C->setVarRefs(Vars);
12473 }
12474 
12475 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12476   C->setDepobj(Record.readSubExpr());
12477   C->setLParenLoc(Record.readSourceLocation());
12478 }
12479 
12480 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12481   C->setLParenLoc(Record.readSourceLocation());
12482   C->setModifier(Record.readSubExpr());
12483   C->setDependencyKind(
12484       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12485   C->setDependencyLoc(Record.readSourceLocation());
12486   C->setColonLoc(Record.readSourceLocation());
12487   unsigned NumVars = C->varlist_size();
12488   SmallVector<Expr *, 16> Vars;
12489   Vars.reserve(NumVars);
12490   for (unsigned I = 0; I != NumVars; ++I)
12491     Vars.push_back(Record.readSubExpr());
12492   C->setVarRefs(Vars);
12493   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12494     C->setLoopData(I, Record.readSubExpr());
12495 }
12496 
12497 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12498   VisitOMPClauseWithPreInit(C);
12499   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12500   C->setDevice(Record.readSubExpr());
12501   C->setModifierLoc(Record.readSourceLocation());
12502   C->setLParenLoc(Record.readSourceLocation());
12503 }
12504 
12505 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12506   C->setLParenLoc(Record.readSourceLocation());
12507   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12508     C->setMapTypeModifier(
12509         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12510     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12511   }
12512   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12513   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12514   C->setMapType(
12515      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12516   C->setMapLoc(Record.readSourceLocation());
12517   C->setColonLoc(Record.readSourceLocation());
12518   auto NumVars = C->varlist_size();
12519   auto UniqueDecls = C->getUniqueDeclarationsNum();
12520   auto TotalLists = C->getTotalComponentListNum();
12521   auto TotalComponents = C->getTotalComponentsNum();
12522 
12523   SmallVector<Expr *, 16> Vars;
12524   Vars.reserve(NumVars);
12525   for (unsigned i = 0; i != NumVars; ++i)
12526     Vars.push_back(Record.readExpr());
12527   C->setVarRefs(Vars);
12528 
12529   SmallVector<Expr *, 16> UDMappers;
12530   UDMappers.reserve(NumVars);
12531   for (unsigned I = 0; I < NumVars; ++I)
12532     UDMappers.push_back(Record.readExpr());
12533   C->setUDMapperRefs(UDMappers);
12534 
12535   SmallVector<ValueDecl *, 16> Decls;
12536   Decls.reserve(UniqueDecls);
12537   for (unsigned i = 0; i < UniqueDecls; ++i)
12538     Decls.push_back(Record.readDeclAs<ValueDecl>());
12539   C->setUniqueDecls(Decls);
12540 
12541   SmallVector<unsigned, 16> ListsPerDecl;
12542   ListsPerDecl.reserve(UniqueDecls);
12543   for (unsigned i = 0; i < UniqueDecls; ++i)
12544     ListsPerDecl.push_back(Record.readInt());
12545   C->setDeclNumLists(ListsPerDecl);
12546 
12547   SmallVector<unsigned, 32> ListSizes;
12548   ListSizes.reserve(TotalLists);
12549   for (unsigned i = 0; i < TotalLists; ++i)
12550     ListSizes.push_back(Record.readInt());
12551   C->setComponentListSizes(ListSizes);
12552 
12553   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12554   Components.reserve(TotalComponents);
12555   for (unsigned i = 0; i < TotalComponents; ++i) {
12556     Expr *AssociatedExprPr = Record.readExpr();
12557     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12558     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12559                             /*IsNonContiguous=*/false);
12560   }
12561   C->setComponents(Components, ListSizes);
12562 }
12563 
12564 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12565   C->setLParenLoc(Record.readSourceLocation());
12566   C->setColonLoc(Record.readSourceLocation());
12567   C->setAllocator(Record.readSubExpr());
12568   unsigned NumVars = C->varlist_size();
12569   SmallVector<Expr *, 16> Vars;
12570   Vars.reserve(NumVars);
12571   for (unsigned i = 0; i != NumVars; ++i)
12572     Vars.push_back(Record.readSubExpr());
12573   C->setVarRefs(Vars);
12574 }
12575 
12576 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12577   VisitOMPClauseWithPreInit(C);
12578   C->setNumTeams(Record.readSubExpr());
12579   C->setLParenLoc(Record.readSourceLocation());
12580 }
12581 
12582 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12583   VisitOMPClauseWithPreInit(C);
12584   C->setThreadLimit(Record.readSubExpr());
12585   C->setLParenLoc(Record.readSourceLocation());
12586 }
12587 
12588 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12589   VisitOMPClauseWithPreInit(C);
12590   C->setPriority(Record.readSubExpr());
12591   C->setLParenLoc(Record.readSourceLocation());
12592 }
12593 
12594 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12595   VisitOMPClauseWithPreInit(C);
12596   C->setGrainsize(Record.readSubExpr());
12597   C->setLParenLoc(Record.readSourceLocation());
12598 }
12599 
12600 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12601   VisitOMPClauseWithPreInit(C);
12602   C->setNumTasks(Record.readSubExpr());
12603   C->setLParenLoc(Record.readSourceLocation());
12604 }
12605 
12606 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12607   C->setHint(Record.readSubExpr());
12608   C->setLParenLoc(Record.readSourceLocation());
12609 }
12610 
12611 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12612   VisitOMPClauseWithPreInit(C);
12613   C->setDistScheduleKind(
12614       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12615   C->setChunkSize(Record.readSubExpr());
12616   C->setLParenLoc(Record.readSourceLocation());
12617   C->setDistScheduleKindLoc(Record.readSourceLocation());
12618   C->setCommaLoc(Record.readSourceLocation());
12619 }
12620 
12621 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12622   C->setDefaultmapKind(
12623        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12624   C->setDefaultmapModifier(
12625       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12626   C->setLParenLoc(Record.readSourceLocation());
12627   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12628   C->setDefaultmapKindLoc(Record.readSourceLocation());
12629 }
12630 
12631 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12632   C->setLParenLoc(Record.readSourceLocation());
12633   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12634     C->setMotionModifier(
12635         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12636     C->setMotionModifierLoc(I, Record.readSourceLocation());
12637   }
12638   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12639   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12640   C->setColonLoc(Record.readSourceLocation());
12641   auto NumVars = C->varlist_size();
12642   auto UniqueDecls = C->getUniqueDeclarationsNum();
12643   auto TotalLists = C->getTotalComponentListNum();
12644   auto TotalComponents = C->getTotalComponentsNum();
12645 
12646   SmallVector<Expr *, 16> Vars;
12647   Vars.reserve(NumVars);
12648   for (unsigned i = 0; i != NumVars; ++i)
12649     Vars.push_back(Record.readSubExpr());
12650   C->setVarRefs(Vars);
12651 
12652   SmallVector<Expr *, 16> UDMappers;
12653   UDMappers.reserve(NumVars);
12654   for (unsigned I = 0; I < NumVars; ++I)
12655     UDMappers.push_back(Record.readSubExpr());
12656   C->setUDMapperRefs(UDMappers);
12657 
12658   SmallVector<ValueDecl *, 16> Decls;
12659   Decls.reserve(UniqueDecls);
12660   for (unsigned i = 0; i < UniqueDecls; ++i)
12661     Decls.push_back(Record.readDeclAs<ValueDecl>());
12662   C->setUniqueDecls(Decls);
12663 
12664   SmallVector<unsigned, 16> ListsPerDecl;
12665   ListsPerDecl.reserve(UniqueDecls);
12666   for (unsigned i = 0; i < UniqueDecls; ++i)
12667     ListsPerDecl.push_back(Record.readInt());
12668   C->setDeclNumLists(ListsPerDecl);
12669 
12670   SmallVector<unsigned, 32> ListSizes;
12671   ListSizes.reserve(TotalLists);
12672   for (unsigned i = 0; i < TotalLists; ++i)
12673     ListSizes.push_back(Record.readInt());
12674   C->setComponentListSizes(ListSizes);
12675 
12676   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12677   Components.reserve(TotalComponents);
12678   for (unsigned i = 0; i < TotalComponents; ++i) {
12679     Expr *AssociatedExprPr = Record.readSubExpr();
12680     bool IsNonContiguous = Record.readBool();
12681     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12682     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12683   }
12684   C->setComponents(Components, ListSizes);
12685 }
12686 
12687 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12688   C->setLParenLoc(Record.readSourceLocation());
12689   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12690     C->setMotionModifier(
12691         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12692     C->setMotionModifierLoc(I, Record.readSourceLocation());
12693   }
12694   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12695   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12696   C->setColonLoc(Record.readSourceLocation());
12697   auto NumVars = C->varlist_size();
12698   auto UniqueDecls = C->getUniqueDeclarationsNum();
12699   auto TotalLists = C->getTotalComponentListNum();
12700   auto TotalComponents = C->getTotalComponentsNum();
12701 
12702   SmallVector<Expr *, 16> Vars;
12703   Vars.reserve(NumVars);
12704   for (unsigned i = 0; i != NumVars; ++i)
12705     Vars.push_back(Record.readSubExpr());
12706   C->setVarRefs(Vars);
12707 
12708   SmallVector<Expr *, 16> UDMappers;
12709   UDMappers.reserve(NumVars);
12710   for (unsigned I = 0; I < NumVars; ++I)
12711     UDMappers.push_back(Record.readSubExpr());
12712   C->setUDMapperRefs(UDMappers);
12713 
12714   SmallVector<ValueDecl *, 16> Decls;
12715   Decls.reserve(UniqueDecls);
12716   for (unsigned i = 0; i < UniqueDecls; ++i)
12717     Decls.push_back(Record.readDeclAs<ValueDecl>());
12718   C->setUniqueDecls(Decls);
12719 
12720   SmallVector<unsigned, 16> ListsPerDecl;
12721   ListsPerDecl.reserve(UniqueDecls);
12722   for (unsigned i = 0; i < UniqueDecls; ++i)
12723     ListsPerDecl.push_back(Record.readInt());
12724   C->setDeclNumLists(ListsPerDecl);
12725 
12726   SmallVector<unsigned, 32> ListSizes;
12727   ListSizes.reserve(TotalLists);
12728   for (unsigned i = 0; i < TotalLists; ++i)
12729     ListSizes.push_back(Record.readInt());
12730   C->setComponentListSizes(ListSizes);
12731 
12732   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12733   Components.reserve(TotalComponents);
12734   for (unsigned i = 0; i < TotalComponents; ++i) {
12735     Expr *AssociatedExprPr = Record.readSubExpr();
12736     bool IsNonContiguous = Record.readBool();
12737     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12738     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12739   }
12740   C->setComponents(Components, ListSizes);
12741 }
12742 
12743 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12744   C->setLParenLoc(Record.readSourceLocation());
12745   auto NumVars = C->varlist_size();
12746   auto UniqueDecls = C->getUniqueDeclarationsNum();
12747   auto TotalLists = C->getTotalComponentListNum();
12748   auto TotalComponents = C->getTotalComponentsNum();
12749 
12750   SmallVector<Expr *, 16> Vars;
12751   Vars.reserve(NumVars);
12752   for (unsigned i = 0; i != NumVars; ++i)
12753     Vars.push_back(Record.readSubExpr());
12754   C->setVarRefs(Vars);
12755   Vars.clear();
12756   for (unsigned i = 0; i != NumVars; ++i)
12757     Vars.push_back(Record.readSubExpr());
12758   C->setPrivateCopies(Vars);
12759   Vars.clear();
12760   for (unsigned i = 0; i != NumVars; ++i)
12761     Vars.push_back(Record.readSubExpr());
12762   C->setInits(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     auto *AssociatedExprPr = Record.readSubExpr();
12786     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12787     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12788                             /*IsNonContiguous=*/false);
12789   }
12790   C->setComponents(Components, ListSizes);
12791 }
12792 
12793 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *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 
12806   SmallVector<ValueDecl *, 16> Decls;
12807   Decls.reserve(UniqueDecls);
12808   for (unsigned i = 0; i < UniqueDecls; ++i)
12809     Decls.push_back(Record.readDeclAs<ValueDecl>());
12810   C->setUniqueDecls(Decls);
12811 
12812   SmallVector<unsigned, 16> ListsPerDecl;
12813   ListsPerDecl.reserve(UniqueDecls);
12814   for (unsigned i = 0; i < UniqueDecls; ++i)
12815     ListsPerDecl.push_back(Record.readInt());
12816   C->setDeclNumLists(ListsPerDecl);
12817 
12818   SmallVector<unsigned, 32> ListSizes;
12819   ListSizes.reserve(TotalLists);
12820   for (unsigned i = 0; i < TotalLists; ++i)
12821     ListSizes.push_back(Record.readInt());
12822   C->setComponentListSizes(ListSizes);
12823 
12824   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12825   Components.reserve(TotalComponents);
12826   for (unsigned i = 0; i < TotalComponents; ++i) {
12827     Expr *AssociatedExpr = Record.readSubExpr();
12828     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12829     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12830                             /*IsNonContiguous*/ false);
12831   }
12832   C->setComponents(Components, ListSizes);
12833 }
12834 
12835 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12836   C->setLParenLoc(Record.readSourceLocation());
12837   auto NumVars = C->varlist_size();
12838   auto UniqueDecls = C->getUniqueDeclarationsNum();
12839   auto TotalLists = C->getTotalComponentListNum();
12840   auto TotalComponents = C->getTotalComponentsNum();
12841 
12842   SmallVector<Expr *, 16> Vars;
12843   Vars.reserve(NumVars);
12844   for (unsigned i = 0; i != NumVars; ++i)
12845     Vars.push_back(Record.readSubExpr());
12846   C->setVarRefs(Vars);
12847   Vars.clear();
12848 
12849   SmallVector<ValueDecl *, 16> Decls;
12850   Decls.reserve(UniqueDecls);
12851   for (unsigned i = 0; i < UniqueDecls; ++i)
12852     Decls.push_back(Record.readDeclAs<ValueDecl>());
12853   C->setUniqueDecls(Decls);
12854 
12855   SmallVector<unsigned, 16> ListsPerDecl;
12856   ListsPerDecl.reserve(UniqueDecls);
12857   for (unsigned i = 0; i < UniqueDecls; ++i)
12858     ListsPerDecl.push_back(Record.readInt());
12859   C->setDeclNumLists(ListsPerDecl);
12860 
12861   SmallVector<unsigned, 32> ListSizes;
12862   ListSizes.reserve(TotalLists);
12863   for (unsigned i = 0; i < TotalLists; ++i)
12864     ListSizes.push_back(Record.readInt());
12865   C->setComponentListSizes(ListSizes);
12866 
12867   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12868   Components.reserve(TotalComponents);
12869   for (unsigned i = 0; i < TotalComponents; ++i) {
12870     Expr *AssociatedExpr = Record.readSubExpr();
12871     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12872     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12873                             /*IsNonContiguous=*/false);
12874   }
12875   C->setComponents(Components, ListSizes);
12876 }
12877 
12878 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12879   C->setLParenLoc(Record.readSourceLocation());
12880   unsigned NumVars = C->varlist_size();
12881   SmallVector<Expr *, 16> Vars;
12882   Vars.reserve(NumVars);
12883   for (unsigned i = 0; i != NumVars; ++i)
12884     Vars.push_back(Record.readSubExpr());
12885   C->setVarRefs(Vars);
12886   Vars.clear();
12887   Vars.reserve(NumVars);
12888   for (unsigned i = 0; i != NumVars; ++i)
12889     Vars.push_back(Record.readSubExpr());
12890   C->setPrivateRefs(Vars);
12891 }
12892 
12893 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12894   C->setLParenLoc(Record.readSourceLocation());
12895   unsigned NumVars = C->varlist_size();
12896   SmallVector<Expr *, 16> Vars;
12897   Vars.reserve(NumVars);
12898   for (unsigned i = 0; i != NumVars; ++i)
12899     Vars.push_back(Record.readSubExpr());
12900   C->setVarRefs(Vars);
12901 }
12902 
12903 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12904   C->setLParenLoc(Record.readSourceLocation());
12905   unsigned NumVars = C->varlist_size();
12906   SmallVector<Expr *, 16> Vars;
12907   Vars.reserve(NumVars);
12908   for (unsigned i = 0; i != NumVars; ++i)
12909     Vars.push_back(Record.readSubExpr());
12910   C->setVarRefs(Vars);
12911 }
12912 
12913 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12914   C->setLParenLoc(Record.readSourceLocation());
12915   unsigned NumOfAllocators = C->getNumberOfAllocators();
12916   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12917   Data.reserve(NumOfAllocators);
12918   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12919     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12920     D.Allocator = Record.readSubExpr();
12921     D.AllocatorTraits = Record.readSubExpr();
12922     D.LParenLoc = Record.readSourceLocation();
12923     D.RParenLoc = Record.readSourceLocation();
12924   }
12925   C->setAllocatorsData(Data);
12926 }
12927 
12928 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12929   C->setLParenLoc(Record.readSourceLocation());
12930   C->setModifier(Record.readSubExpr());
12931   C->setColonLoc(Record.readSourceLocation());
12932   unsigned NumOfLocators = C->varlist_size();
12933   SmallVector<Expr *, 4> Locators;
12934   Locators.reserve(NumOfLocators);
12935   for (unsigned I = 0; I != NumOfLocators; ++I)
12936     Locators.push_back(Record.readSubExpr());
12937   C->setVarRefs(Locators);
12938 }
12939 
12940 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12941   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12942   C->setLParenLoc(Record.readSourceLocation());
12943   C->setKindKwLoc(Record.readSourceLocation());
12944 }
12945 
12946 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12947   VisitOMPClauseWithPreInit(C);
12948   C->setThreadID(Record.readSubExpr());
12949   C->setLParenLoc(Record.readSourceLocation());
12950 }
12951 
12952 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12953   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12954   TI.Sets.resize(readUInt32());
12955   for (auto &Set : TI.Sets) {
12956     Set.Kind = readEnum<llvm::omp::TraitSet>();
12957     Set.Selectors.resize(readUInt32());
12958     for (auto &Selector : Set.Selectors) {
12959       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12960       Selector.ScoreOrCondition = nullptr;
12961       if (readBool())
12962         Selector.ScoreOrCondition = readExprRef();
12963       Selector.Properties.resize(readUInt32());
12964       for (auto &Property : Selector.Properties)
12965         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12966     }
12967   }
12968   return &TI;
12969 }
12970 
12971 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12972   if (!Data)
12973     return;
12974   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12975     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12976     skipInts(3);
12977   }
12978   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12979   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12980     Clauses[I] = readOMPClause();
12981   Data->setClauses(Clauses);
12982   if (Data->hasAssociatedStmt())
12983     Data->setAssociatedStmt(readStmt());
12984   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12985     Data->getChildren()[I] = readStmt();
12986 }
12987