xref: /freebsd/contrib/llvm-project/clang/lib/Serialization/ASTReader.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/ASTUnresolvedSet.h"
19 #include "clang/AST/AbstractTypeReader.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclFriend.h"
24 #include "clang/AST/DeclGroup.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/ExternalASTSource.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/ODRHash.h"
33 #include "clang/AST/OpenMPClause.h"
34 #include "clang/AST/RawCommentList.h"
35 #include "clang/AST/TemplateBase.h"
36 #include "clang/AST/TemplateName.h"
37 #include "clang/AST/Type.h"
38 #include "clang/AST/TypeLoc.h"
39 #include "clang/AST/TypeLocVisitor.h"
40 #include "clang/AST/UnresolvedSet.h"
41 #include "clang/Basic/CommentOptions.h"
42 #include "clang/Basic/Diagnostic.h"
43 #include "clang/Basic/DiagnosticError.h"
44 #include "clang/Basic/DiagnosticOptions.h"
45 #include "clang/Basic/ExceptionSpecificationType.h"
46 #include "clang/Basic/FileManager.h"
47 #include "clang/Basic/FileSystemOptions.h"
48 #include "clang/Basic/IdentifierTable.h"
49 #include "clang/Basic/LLVM.h"
50 #include "clang/Basic/LangOptions.h"
51 #include "clang/Basic/Module.h"
52 #include "clang/Basic/ObjCRuntime.h"
53 #include "clang/Basic/OpenMPKinds.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ASTRecordReader.h"
80 #include "clang/Serialization/ContinuousRangeMap.h"
81 #include "clang/Serialization/GlobalModuleIndex.h"
82 #include "clang/Serialization/InMemoryModuleCache.h"
83 #include "clang/Serialization/ModuleFile.h"
84 #include "clang/Serialization/ModuleFileExtension.h"
85 #include "clang/Serialization/ModuleManager.h"
86 #include "clang/Serialization/PCHContainerOperations.h"
87 #include "clang/Serialization/SerializationDiagnostic.h"
88 #include "llvm/ADT/APFloat.h"
89 #include "llvm/ADT/APInt.h"
90 #include "llvm/ADT/APSInt.h"
91 #include "llvm/ADT/ArrayRef.h"
92 #include "llvm/ADT/DenseMap.h"
93 #include "llvm/ADT/FloatingPointMode.h"
94 #include "llvm/ADT/FoldingSet.h"
95 #include "llvm/ADT/Hashing.h"
96 #include "llvm/ADT/IntrusiveRefCntPtr.h"
97 #include "llvm/ADT/None.h"
98 #include "llvm/ADT/Optional.h"
99 #include "llvm/ADT/STLExtras.h"
100 #include "llvm/ADT/ScopeExit.h"
101 #include "llvm/ADT/SmallPtrSet.h"
102 #include "llvm/ADT/SmallString.h"
103 #include "llvm/ADT/SmallVector.h"
104 #include "llvm/ADT/StringExtras.h"
105 #include "llvm/ADT/StringMap.h"
106 #include "llvm/ADT/StringRef.h"
107 #include "llvm/ADT/Triple.h"
108 #include "llvm/ADT/iterator_range.h"
109 #include "llvm/Bitstream/BitstreamReader.h"
110 #include "llvm/Support/Casting.h"
111 #include "llvm/Support/Compiler.h"
112 #include "llvm/Support/Compression.h"
113 #include "llvm/Support/DJB.h"
114 #include "llvm/Support/Endian.h"
115 #include "llvm/Support/Error.h"
116 #include "llvm/Support/ErrorHandling.h"
117 #include "llvm/Support/FileSystem.h"
118 #include "llvm/Support/LEB128.h"
119 #include "llvm/Support/MemoryBuffer.h"
120 #include "llvm/Support/Path.h"
121 #include "llvm/Support/SaveAndRestore.h"
122 #include "llvm/Support/Timer.h"
123 #include "llvm/Support/VersionTuple.h"
124 #include "llvm/Support/raw_ostream.h"
125 #include <algorithm>
126 #include <cassert>
127 #include <cstddef>
128 #include <cstdint>
129 #include <cstdio>
130 #include <ctime>
131 #include <iterator>
132 #include <limits>
133 #include <map>
134 #include <memory>
135 #include <string>
136 #include <system_error>
137 #include <tuple>
138 #include <utility>
139 #include <vector>
140 
141 using namespace clang;
142 using namespace clang::serialization;
143 using namespace clang::serialization::reader;
144 using llvm::BitstreamCursor;
145 using llvm::RoundingMode;
146 
147 //===----------------------------------------------------------------------===//
148 // ChainedASTReaderListener implementation
149 //===----------------------------------------------------------------------===//
150 
151 bool
152 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
153   return First->ReadFullVersionInformation(FullVersion) ||
154          Second->ReadFullVersionInformation(FullVersion);
155 }
156 
157 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
158   First->ReadModuleName(ModuleName);
159   Second->ReadModuleName(ModuleName);
160 }
161 
162 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
163   First->ReadModuleMapFile(ModuleMapPath);
164   Second->ReadModuleMapFile(ModuleMapPath);
165 }
166 
167 bool
168 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
169                                               bool Complain,
170                                               bool AllowCompatibleDifferences) {
171   return First->ReadLanguageOptions(LangOpts, Complain,
172                                     AllowCompatibleDifferences) ||
173          Second->ReadLanguageOptions(LangOpts, Complain,
174                                      AllowCompatibleDifferences);
175 }
176 
177 bool ChainedASTReaderListener::ReadTargetOptions(
178     const TargetOptions &TargetOpts, bool Complain,
179     bool AllowCompatibleDifferences) {
180   return First->ReadTargetOptions(TargetOpts, Complain,
181                                   AllowCompatibleDifferences) ||
182          Second->ReadTargetOptions(TargetOpts, Complain,
183                                    AllowCompatibleDifferences);
184 }
185 
186 bool ChainedASTReaderListener::ReadDiagnosticOptions(
187     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
188   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
189          Second->ReadDiagnosticOptions(DiagOpts, Complain);
190 }
191 
192 bool
193 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
194                                                 bool Complain) {
195   return First->ReadFileSystemOptions(FSOpts, Complain) ||
196          Second->ReadFileSystemOptions(FSOpts, Complain);
197 }
198 
199 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
200     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
201     bool Complain) {
202   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
203                                         Complain) ||
204          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
205                                          Complain);
206 }
207 
208 bool ChainedASTReaderListener::ReadPreprocessorOptions(
209     const PreprocessorOptions &PPOpts, bool Complain,
210     std::string &SuggestedPredefines) {
211   return First->ReadPreprocessorOptions(PPOpts, Complain,
212                                         SuggestedPredefines) ||
213          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
214 }
215 
216 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
217                                            unsigned Value) {
218   First->ReadCounter(M, Value);
219   Second->ReadCounter(M, Value);
220 }
221 
222 bool ChainedASTReaderListener::needsInputFileVisitation() {
223   return First->needsInputFileVisitation() ||
224          Second->needsInputFileVisitation();
225 }
226 
227 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
228   return First->needsSystemInputFileVisitation() ||
229   Second->needsSystemInputFileVisitation();
230 }
231 
232 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
233                                                ModuleKind Kind) {
234   First->visitModuleFile(Filename, Kind);
235   Second->visitModuleFile(Filename, Kind);
236 }
237 
238 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
239                                               bool isSystem,
240                                               bool isOverridden,
241                                               bool isExplicitModule) {
242   bool Continue = false;
243   if (First->needsInputFileVisitation() &&
244       (!isSystem || First->needsSystemInputFileVisitation()))
245     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
246                                       isExplicitModule);
247   if (Second->needsInputFileVisitation() &&
248       (!isSystem || Second->needsSystemInputFileVisitation()))
249     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
250                                        isExplicitModule);
251   return Continue;
252 }
253 
254 void ChainedASTReaderListener::readModuleFileExtension(
255        const ModuleFileExtensionMetadata &Metadata) {
256   First->readModuleFileExtension(Metadata);
257   Second->readModuleFileExtension(Metadata);
258 }
259 
260 //===----------------------------------------------------------------------===//
261 // PCH validator implementation
262 //===----------------------------------------------------------------------===//
263 
264 ASTReaderListener::~ASTReaderListener() = default;
265 
266 /// Compare the given set of language options against an existing set of
267 /// language options.
268 ///
269 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
270 /// \param AllowCompatibleDifferences If true, differences between compatible
271 ///        language options will be permitted.
272 ///
273 /// \returns true if the languagae options mis-match, false otherwise.
274 static bool checkLanguageOptions(const LangOptions &LangOpts,
275                                  const LangOptions &ExistingLangOpts,
276                                  DiagnosticsEngine *Diags,
277                                  bool AllowCompatibleDifferences = true) {
278 #define LANGOPT(Name, Bits, Default, Description)                 \
279   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
280     if (Diags)                                                    \
281       Diags->Report(diag::err_pch_langopt_mismatch)               \
282         << Description << LangOpts.Name << ExistingLangOpts.Name; \
283     return true;                                                  \
284   }
285 
286 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
287   if (ExistingLangOpts.Name != LangOpts.Name) {           \
288     if (Diags)                                            \
289       Diags->Report(diag::err_pch_langopt_value_mismatch) \
290         << Description;                                   \
291     return true;                                          \
292   }
293 
294 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
295   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
296     if (Diags)                                                 \
297       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
298         << Description;                                        \
299     return true;                                               \
300   }
301 
302 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
303   if (!AllowCompatibleDifferences)                            \
304     LANGOPT(Name, Bits, Default, Description)
305 
306 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
307   if (!AllowCompatibleDifferences)                                 \
308     ENUM_LANGOPT(Name, Bits, Default, Description)
309 
310 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
311   if (!AllowCompatibleDifferences)                                 \
312     VALUE_LANGOPT(Name, Bits, Default, Description)
313 
314 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
315 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
316 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
317 #include "clang/Basic/LangOptions.def"
318 
319   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
320     if (Diags)
321       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
322     return true;
323   }
324 
325   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
326     if (Diags)
327       Diags->Report(diag::err_pch_langopt_value_mismatch)
328       << "target Objective-C runtime";
329     return true;
330   }
331 
332   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
333       LangOpts.CommentOpts.BlockCommandNames) {
334     if (Diags)
335       Diags->Report(diag::err_pch_langopt_value_mismatch)
336         << "block command names";
337     return true;
338   }
339 
340   // Sanitizer feature mismatches are treated as compatible differences. If
341   // compatible differences aren't allowed, we still only want to check for
342   // mismatches of non-modular sanitizers (the only ones which can affect AST
343   // generation).
344   if (!AllowCompatibleDifferences) {
345     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
346     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
347     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
348     ExistingSanitizers.clear(ModularSanitizers);
349     ImportedSanitizers.clear(ModularSanitizers);
350     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
351       const std::string Flag = "-fsanitize=";
352       if (Diags) {
353 #define SANITIZER(NAME, ID)                                                    \
354   {                                                                            \
355     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
356     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
357     if (InExistingModule != InImportedModule)                                  \
358       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
359           << InExistingModule << (Flag + NAME);                                \
360   }
361 #include "clang/Basic/Sanitizers.def"
362       }
363       return true;
364     }
365   }
366 
367   return false;
368 }
369 
370 /// Compare the given set of target options against an existing set of
371 /// target options.
372 ///
373 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
374 ///
375 /// \returns true if the target options mis-match, false otherwise.
376 static bool checkTargetOptions(const TargetOptions &TargetOpts,
377                                const TargetOptions &ExistingTargetOpts,
378                                DiagnosticsEngine *Diags,
379                                bool AllowCompatibleDifferences = true) {
380 #define CHECK_TARGET_OPT(Field, Name)                             \
381   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
382     if (Diags)                                                    \
383       Diags->Report(diag::err_pch_targetopt_mismatch)             \
384         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
385     return true;                                                  \
386   }
387 
388   // The triple and ABI must match exactly.
389   CHECK_TARGET_OPT(Triple, "target");
390   CHECK_TARGET_OPT(ABI, "target ABI");
391 
392   // We can tolerate different CPUs in many cases, notably when one CPU
393   // supports a strict superset of another. When allowing compatible
394   // differences skip this check.
395   if (!AllowCompatibleDifferences) {
396     CHECK_TARGET_OPT(CPU, "target CPU");
397     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
398   }
399 
400 #undef CHECK_TARGET_OPT
401 
402   // Compare feature sets.
403   SmallVector<StringRef, 4> ExistingFeatures(
404                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
405                                              ExistingTargetOpts.FeaturesAsWritten.end());
406   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
407                                          TargetOpts.FeaturesAsWritten.end());
408   llvm::sort(ExistingFeatures);
409   llvm::sort(ReadFeatures);
410 
411   // We compute the set difference in both directions explicitly so that we can
412   // diagnose the differences differently.
413   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
414   std::set_difference(
415       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
416       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
417   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
418                       ExistingFeatures.begin(), ExistingFeatures.end(),
419                       std::back_inserter(UnmatchedReadFeatures));
420 
421   // If we are allowing compatible differences and the read feature set is
422   // a strict subset of the existing feature set, there is nothing to diagnose.
423   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
424     return false;
425 
426   if (Diags) {
427     for (StringRef Feature : UnmatchedReadFeatures)
428       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
429           << /* is-existing-feature */ false << Feature;
430     for (StringRef Feature : UnmatchedExistingFeatures)
431       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
432           << /* is-existing-feature */ true << Feature;
433   }
434 
435   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
436 }
437 
438 bool
439 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
440                                   bool Complain,
441                                   bool AllowCompatibleDifferences) {
442   const LangOptions &ExistingLangOpts = PP.getLangOpts();
443   return checkLanguageOptions(LangOpts, ExistingLangOpts,
444                               Complain ? &Reader.Diags : nullptr,
445                               AllowCompatibleDifferences);
446 }
447 
448 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
449                                      bool Complain,
450                                      bool AllowCompatibleDifferences) {
451   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
452   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
453                             Complain ? &Reader.Diags : nullptr,
454                             AllowCompatibleDifferences);
455 }
456 
457 namespace {
458 
459 using MacroDefinitionsMap =
460     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
461 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
462 
463 } // namespace
464 
465 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
466                                          DiagnosticsEngine &Diags,
467                                          bool Complain) {
468   using Level = DiagnosticsEngine::Level;
469 
470   // Check current mappings for new -Werror mappings, and the stored mappings
471   // for cases that were explicitly mapped to *not* be errors that are now
472   // errors because of options like -Werror.
473   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
474 
475   for (DiagnosticsEngine *MappingSource : MappingSources) {
476     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
477       diag::kind DiagID = DiagIDMappingPair.first;
478       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
479       if (CurLevel < DiagnosticsEngine::Error)
480         continue; // not significant
481       Level StoredLevel =
482           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
483       if (StoredLevel < DiagnosticsEngine::Error) {
484         if (Complain)
485           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
486               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
487         return true;
488       }
489     }
490   }
491 
492   return false;
493 }
494 
495 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
496   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
497   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
498     return true;
499   return Ext >= diag::Severity::Error;
500 }
501 
502 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
503                                     DiagnosticsEngine &Diags,
504                                     bool IsSystem, bool Complain) {
505   // Top-level options
506   if (IsSystem) {
507     if (Diags.getSuppressSystemWarnings())
508       return false;
509     // If -Wsystem-headers was not enabled before, be conservative
510     if (StoredDiags.getSuppressSystemWarnings()) {
511       if (Complain)
512         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
513       return true;
514     }
515   }
516 
517   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
518     if (Complain)
519       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
520     return true;
521   }
522 
523   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
524       !StoredDiags.getEnableAllWarnings()) {
525     if (Complain)
526       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
527     return true;
528   }
529 
530   if (isExtHandlingFromDiagsError(Diags) &&
531       !isExtHandlingFromDiagsError(StoredDiags)) {
532     if (Complain)
533       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
534     return true;
535   }
536 
537   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
538 }
539 
540 /// Return the top import module if it is implicit, nullptr otherwise.
541 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
542                                           Preprocessor &PP) {
543   // If the original import came from a file explicitly generated by the user,
544   // don't check the diagnostic mappings.
545   // FIXME: currently this is approximated by checking whether this is not a
546   // module import of an implicitly-loaded module file.
547   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
548   // the transitive closure of its imports, since unrelated modules cannot be
549   // imported until after this module finishes validation.
550   ModuleFile *TopImport = &*ModuleMgr.rbegin();
551   while (!TopImport->ImportedBy.empty())
552     TopImport = TopImport->ImportedBy[0];
553   if (TopImport->Kind != MK_ImplicitModule)
554     return nullptr;
555 
556   StringRef ModuleName = TopImport->ModuleName;
557   assert(!ModuleName.empty() && "diagnostic options read before module name");
558 
559   Module *M =
560       PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
561   assert(M && "missing module");
562   return M;
563 }
564 
565 bool PCHValidator::ReadDiagnosticOptions(
566     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
567   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
568   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
569   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
570       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
571   // This should never fail, because we would have processed these options
572   // before writing them to an ASTFile.
573   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
574 
575   ModuleManager &ModuleMgr = Reader.getModuleManager();
576   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
577 
578   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
579   if (!TopM)
580     return false;
581 
582   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
583   // contains the union of their flags.
584   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
585                                  Complain);
586 }
587 
588 /// Collect the macro definitions provided by the given preprocessor
589 /// options.
590 static void
591 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
592                         MacroDefinitionsMap &Macros,
593                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
594   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
595     StringRef Macro = PPOpts.Macros[I].first;
596     bool IsUndef = PPOpts.Macros[I].second;
597 
598     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
599     StringRef MacroName = MacroPair.first;
600     StringRef MacroBody = MacroPair.second;
601 
602     // For an #undef'd macro, we only care about the name.
603     if (IsUndef) {
604       if (MacroNames && !Macros.count(MacroName))
605         MacroNames->push_back(MacroName);
606 
607       Macros[MacroName] = std::make_pair("", true);
608       continue;
609     }
610 
611     // For a #define'd macro, figure out the actual definition.
612     if (MacroName.size() == Macro.size())
613       MacroBody = "1";
614     else {
615       // Note: GCC drops anything following an end-of-line character.
616       StringRef::size_type End = MacroBody.find_first_of("\n\r");
617       MacroBody = MacroBody.substr(0, End);
618     }
619 
620     if (MacroNames && !Macros.count(MacroName))
621       MacroNames->push_back(MacroName);
622     Macros[MacroName] = std::make_pair(MacroBody, false);
623   }
624 }
625 
626 /// Check the preprocessor options deserialized from the control block
627 /// against the preprocessor options in an existing preprocessor.
628 ///
629 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
630 /// \param Validate If true, validate preprocessor options. If false, allow
631 ///        macros defined by \p ExistingPPOpts to override those defined by
632 ///        \p PPOpts in SuggestedPredefines.
633 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
634                                      const PreprocessorOptions &ExistingPPOpts,
635                                      DiagnosticsEngine *Diags,
636                                      FileManager &FileMgr,
637                                      std::string &SuggestedPredefines,
638                                      const LangOptions &LangOpts,
639                                      bool Validate = true) {
640   // Check macro definitions.
641   MacroDefinitionsMap ASTFileMacros;
642   collectMacroDefinitions(PPOpts, ASTFileMacros);
643   MacroDefinitionsMap ExistingMacros;
644   SmallVector<StringRef, 4> ExistingMacroNames;
645   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
646 
647   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
648     // Dig out the macro definition in the existing preprocessor options.
649     StringRef MacroName = ExistingMacroNames[I];
650     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
651 
652     // Check whether we know anything about this macro name or not.
653     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
654         ASTFileMacros.find(MacroName);
655     if (!Validate || Known == ASTFileMacros.end()) {
656       // FIXME: Check whether this identifier was referenced anywhere in the
657       // AST file. If so, we should reject the AST file. Unfortunately, this
658       // information isn't in the control block. What shall we do about it?
659 
660       if (Existing.second) {
661         SuggestedPredefines += "#undef ";
662         SuggestedPredefines += MacroName.str();
663         SuggestedPredefines += '\n';
664       } else {
665         SuggestedPredefines += "#define ";
666         SuggestedPredefines += MacroName.str();
667         SuggestedPredefines += ' ';
668         SuggestedPredefines += Existing.first.str();
669         SuggestedPredefines += '\n';
670       }
671       continue;
672     }
673 
674     // If the macro was defined in one but undef'd in the other, we have a
675     // conflict.
676     if (Existing.second != Known->second.second) {
677       if (Diags) {
678         Diags->Report(diag::err_pch_macro_def_undef)
679           << MacroName << Known->second.second;
680       }
681       return true;
682     }
683 
684     // If the macro was #undef'd in both, or if the macro bodies are identical,
685     // it's fine.
686     if (Existing.second || Existing.first == Known->second.first)
687       continue;
688 
689     // The macro bodies differ; complain.
690     if (Diags) {
691       Diags->Report(diag::err_pch_macro_def_conflict)
692         << MacroName << Known->second.first << Existing.first;
693     }
694     return true;
695   }
696 
697   // Check whether we're using predefines.
698   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
699     if (Diags) {
700       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
701     }
702     return true;
703   }
704 
705   // Detailed record is important since it is used for the module cache hash.
706   if (LangOpts.Modules &&
707       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
708     if (Diags) {
709       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
710     }
711     return true;
712   }
713 
714   // Compute the #include and #include_macros lines we need.
715   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
716     StringRef File = ExistingPPOpts.Includes[I];
717 
718     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
719         !ExistingPPOpts.PCHThroughHeader.empty()) {
720       // In case the through header is an include, we must add all the includes
721       // to the predefines so the start point can be determined.
722       SuggestedPredefines += "#include \"";
723       SuggestedPredefines += File;
724       SuggestedPredefines += "\"\n";
725       continue;
726     }
727 
728     if (File == ExistingPPOpts.ImplicitPCHInclude)
729       continue;
730 
731     if (llvm::is_contained(PPOpts.Includes, File))
732       continue;
733 
734     SuggestedPredefines += "#include \"";
735     SuggestedPredefines += File;
736     SuggestedPredefines += "\"\n";
737   }
738 
739   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
740     StringRef File = ExistingPPOpts.MacroIncludes[I];
741     if (llvm::is_contained(PPOpts.MacroIncludes, File))
742       continue;
743 
744     SuggestedPredefines += "#__include_macros \"";
745     SuggestedPredefines += File;
746     SuggestedPredefines += "\"\n##\n";
747   }
748 
749   return false;
750 }
751 
752 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
753                                            bool Complain,
754                                            std::string &SuggestedPredefines) {
755   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
756 
757   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
758                                   Complain? &Reader.Diags : nullptr,
759                                   PP.getFileManager(),
760                                   SuggestedPredefines,
761                                   PP.getLangOpts());
762 }
763 
764 bool SimpleASTReaderListener::ReadPreprocessorOptions(
765                                   const PreprocessorOptions &PPOpts,
766                                   bool Complain,
767                                   std::string &SuggestedPredefines) {
768   return checkPreprocessorOptions(PPOpts,
769                                   PP.getPreprocessorOpts(),
770                                   nullptr,
771                                   PP.getFileManager(),
772                                   SuggestedPredefines,
773                                   PP.getLangOpts(),
774                                   false);
775 }
776 
777 /// Check the header search options deserialized from the control block
778 /// against the header search options in an existing preprocessor.
779 ///
780 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
781 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
782                                      StringRef SpecificModuleCachePath,
783                                      StringRef ExistingModuleCachePath,
784                                      DiagnosticsEngine *Diags,
785                                      const LangOptions &LangOpts,
786                                      const PreprocessorOptions &PPOpts) {
787   if (LangOpts.Modules) {
788     if (SpecificModuleCachePath != ExistingModuleCachePath &&
789         !PPOpts.AllowPCHWithDifferentModulesCachePath) {
790       if (Diags)
791         Diags->Report(diag::err_pch_modulecache_mismatch)
792           << SpecificModuleCachePath << ExistingModuleCachePath;
793       return true;
794     }
795   }
796 
797   return false;
798 }
799 
800 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
801                                            StringRef SpecificModuleCachePath,
802                                            bool Complain) {
803   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
804                                   PP.getHeaderSearchInfo().getModuleCachePath(),
805                                   Complain ? &Reader.Diags : nullptr,
806                                   PP.getLangOpts(), PP.getPreprocessorOpts());
807 }
808 
809 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
810   PP.setCounterValue(Value);
811 }
812 
813 //===----------------------------------------------------------------------===//
814 // AST reader implementation
815 //===----------------------------------------------------------------------===//
816 
817 static uint64_t readULEB(const unsigned char *&P) {
818   unsigned Length = 0;
819   const char *Error = nullptr;
820 
821   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
822   if (Error)
823     llvm::report_fatal_error(Error);
824   P += Length;
825   return Val;
826 }
827 
828 /// Read ULEB-encoded key length and data length.
829 static std::pair<unsigned, unsigned>
830 readULEBKeyDataLength(const unsigned char *&P) {
831   unsigned KeyLen = readULEB(P);
832   if ((unsigned)KeyLen != KeyLen)
833     llvm::report_fatal_error("key too large");
834 
835   unsigned DataLen = readULEB(P);
836   if ((unsigned)DataLen != DataLen)
837     llvm::report_fatal_error("data too large");
838 
839   return std::make_pair(KeyLen, DataLen);
840 }
841 
842 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
843                                            bool TakeOwnership) {
844   DeserializationListener = Listener;
845   OwnsDeserializationListener = TakeOwnership;
846 }
847 
848 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
849   return serialization::ComputeHash(Sel);
850 }
851 
852 std::pair<unsigned, unsigned>
853 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
854   return readULEBKeyDataLength(d);
855 }
856 
857 ASTSelectorLookupTrait::internal_key_type
858 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
859   using namespace llvm::support;
860 
861   SelectorTable &SelTable = Reader.getContext().Selectors;
862   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
863   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
864       F, endian::readNext<uint32_t, little, unaligned>(d));
865   if (N == 0)
866     return SelTable.getNullarySelector(FirstII);
867   else if (N == 1)
868     return SelTable.getUnarySelector(FirstII);
869 
870   SmallVector<IdentifierInfo *, 16> Args;
871   Args.push_back(FirstII);
872   for (unsigned I = 1; I != N; ++I)
873     Args.push_back(Reader.getLocalIdentifier(
874         F, endian::readNext<uint32_t, little, unaligned>(d)));
875 
876   return SelTable.getSelector(N, Args.data());
877 }
878 
879 ASTSelectorLookupTrait::data_type
880 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
881                                  unsigned DataLen) {
882   using namespace llvm::support;
883 
884   data_type Result;
885 
886   Result.ID = Reader.getGlobalSelectorID(
887       F, endian::readNext<uint32_t, little, unaligned>(d));
888   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
889   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
890   Result.InstanceBits = FullInstanceBits & 0x3;
891   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
892   Result.FactoryBits = FullFactoryBits & 0x3;
893   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
894   unsigned NumInstanceMethods = FullInstanceBits >> 3;
895   unsigned NumFactoryMethods = FullFactoryBits >> 3;
896 
897   // Load instance methods
898   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
899     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
900             F, endian::readNext<uint32_t, little, unaligned>(d)))
901       Result.Instance.push_back(Method);
902   }
903 
904   // Load factory methods
905   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
906     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
907             F, endian::readNext<uint32_t, little, unaligned>(d)))
908       Result.Factory.push_back(Method);
909   }
910 
911   return Result;
912 }
913 
914 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
915   return llvm::djbHash(a);
916 }
917 
918 std::pair<unsigned, unsigned>
919 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
920   return readULEBKeyDataLength(d);
921 }
922 
923 ASTIdentifierLookupTraitBase::internal_key_type
924 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
925   assert(n >= 2 && d[n-1] == '\0');
926   return StringRef((const char*) d, n-1);
927 }
928 
929 /// Whether the given identifier is "interesting".
930 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
931                                     bool IsModule) {
932   return II.hadMacroDefinition() || II.isPoisoned() ||
933          (!IsModule && II.getObjCOrBuiltinID()) ||
934          II.hasRevertedTokenIDToIdentifier() ||
935          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
936           II.getFETokenInfo());
937 }
938 
939 static bool readBit(unsigned &Bits) {
940   bool Value = Bits & 0x1;
941   Bits >>= 1;
942   return Value;
943 }
944 
945 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
946   using namespace llvm::support;
947 
948   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
949   return Reader.getGlobalIdentifierID(F, RawID >> 1);
950 }
951 
952 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
953   if (!II.isFromAST()) {
954     II.setIsFromAST();
955     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
956     if (isInterestingIdentifier(Reader, II, IsModule))
957       II.setChangedSinceDeserialization();
958   }
959 }
960 
961 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
962                                                    const unsigned char* d,
963                                                    unsigned DataLen) {
964   using namespace llvm::support;
965 
966   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
967   bool IsInteresting = RawID & 0x01;
968 
969   // Wipe out the "is interesting" bit.
970   RawID = RawID >> 1;
971 
972   // Build the IdentifierInfo and link the identifier ID with it.
973   IdentifierInfo *II = KnownII;
974   if (!II) {
975     II = &Reader.getIdentifierTable().getOwn(k);
976     KnownII = II;
977   }
978   markIdentifierFromAST(Reader, *II);
979   Reader.markIdentifierUpToDate(II);
980 
981   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
982   if (!IsInteresting) {
983     // For uninteresting identifiers, there's nothing else to do. Just notify
984     // the reader that we've finished loading this identifier.
985     Reader.SetIdentifierInfo(ID, II);
986     return II;
987   }
988 
989   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
990   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
991   bool CPlusPlusOperatorKeyword = readBit(Bits);
992   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
993   bool Poisoned = readBit(Bits);
994   bool ExtensionToken = readBit(Bits);
995   bool HadMacroDefinition = readBit(Bits);
996 
997   assert(Bits == 0 && "Extra bits in the identifier?");
998   DataLen -= 8;
999 
1000   // Set or check the various bits in the IdentifierInfo structure.
1001   // Token IDs are read-only.
1002   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1003     II->revertTokenIDToIdentifier();
1004   if (!F.isModule())
1005     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1006   assert(II->isExtensionToken() == ExtensionToken &&
1007          "Incorrect extension token flag");
1008   (void)ExtensionToken;
1009   if (Poisoned)
1010     II->setIsPoisoned(true);
1011   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1012          "Incorrect C++ operator keyword flag");
1013   (void)CPlusPlusOperatorKeyword;
1014 
1015   // If this identifier is a macro, deserialize the macro
1016   // definition.
1017   if (HadMacroDefinition) {
1018     uint32_t MacroDirectivesOffset =
1019         endian::readNext<uint32_t, little, unaligned>(d);
1020     DataLen -= 4;
1021 
1022     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1023   }
1024 
1025   Reader.SetIdentifierInfo(ID, II);
1026 
1027   // Read all of the declarations visible at global scope with this
1028   // name.
1029   if (DataLen > 0) {
1030     SmallVector<uint32_t, 4> DeclIDs;
1031     for (; DataLen > 0; DataLen -= 4)
1032       DeclIDs.push_back(Reader.getGlobalDeclID(
1033           F, endian::readNext<uint32_t, little, unaligned>(d)));
1034     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1035   }
1036 
1037   return II;
1038 }
1039 
1040 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1041     : Kind(Name.getNameKind()) {
1042   switch (Kind) {
1043   case DeclarationName::Identifier:
1044     Data = (uint64_t)Name.getAsIdentifierInfo();
1045     break;
1046   case DeclarationName::ObjCZeroArgSelector:
1047   case DeclarationName::ObjCOneArgSelector:
1048   case DeclarationName::ObjCMultiArgSelector:
1049     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1050     break;
1051   case DeclarationName::CXXOperatorName:
1052     Data = Name.getCXXOverloadedOperator();
1053     break;
1054   case DeclarationName::CXXLiteralOperatorName:
1055     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1056     break;
1057   case DeclarationName::CXXDeductionGuideName:
1058     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1059                ->getDeclName().getAsIdentifierInfo();
1060     break;
1061   case DeclarationName::CXXConstructorName:
1062   case DeclarationName::CXXDestructorName:
1063   case DeclarationName::CXXConversionFunctionName:
1064   case DeclarationName::CXXUsingDirective:
1065     Data = 0;
1066     break;
1067   }
1068 }
1069 
1070 unsigned DeclarationNameKey::getHash() const {
1071   llvm::FoldingSetNodeID ID;
1072   ID.AddInteger(Kind);
1073 
1074   switch (Kind) {
1075   case DeclarationName::Identifier:
1076   case DeclarationName::CXXLiteralOperatorName:
1077   case DeclarationName::CXXDeductionGuideName:
1078     ID.AddString(((IdentifierInfo*)Data)->getName());
1079     break;
1080   case DeclarationName::ObjCZeroArgSelector:
1081   case DeclarationName::ObjCOneArgSelector:
1082   case DeclarationName::ObjCMultiArgSelector:
1083     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1084     break;
1085   case DeclarationName::CXXOperatorName:
1086     ID.AddInteger((OverloadedOperatorKind)Data);
1087     break;
1088   case DeclarationName::CXXConstructorName:
1089   case DeclarationName::CXXDestructorName:
1090   case DeclarationName::CXXConversionFunctionName:
1091   case DeclarationName::CXXUsingDirective:
1092     break;
1093   }
1094 
1095   return ID.ComputeHash();
1096 }
1097 
1098 ModuleFile *
1099 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1100   using namespace llvm::support;
1101 
1102   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1103   return Reader.getLocalModuleFile(F, ModuleFileID);
1104 }
1105 
1106 std::pair<unsigned, unsigned>
1107 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1108   return readULEBKeyDataLength(d);
1109 }
1110 
1111 ASTDeclContextNameLookupTrait::internal_key_type
1112 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1113   using namespace llvm::support;
1114 
1115   auto Kind = (DeclarationName::NameKind)*d++;
1116   uint64_t Data;
1117   switch (Kind) {
1118   case DeclarationName::Identifier:
1119   case DeclarationName::CXXLiteralOperatorName:
1120   case DeclarationName::CXXDeductionGuideName:
1121     Data = (uint64_t)Reader.getLocalIdentifier(
1122         F, endian::readNext<uint32_t, little, unaligned>(d));
1123     break;
1124   case DeclarationName::ObjCZeroArgSelector:
1125   case DeclarationName::ObjCOneArgSelector:
1126   case DeclarationName::ObjCMultiArgSelector:
1127     Data =
1128         (uint64_t)Reader.getLocalSelector(
1129                              F, endian::readNext<uint32_t, little, unaligned>(
1130                                     d)).getAsOpaquePtr();
1131     break;
1132   case DeclarationName::CXXOperatorName:
1133     Data = *d++; // OverloadedOperatorKind
1134     break;
1135   case DeclarationName::CXXConstructorName:
1136   case DeclarationName::CXXDestructorName:
1137   case DeclarationName::CXXConversionFunctionName:
1138   case DeclarationName::CXXUsingDirective:
1139     Data = 0;
1140     break;
1141   }
1142 
1143   return DeclarationNameKey(Kind, Data);
1144 }
1145 
1146 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1147                                                  const unsigned char *d,
1148                                                  unsigned DataLen,
1149                                                  data_type_builder &Val) {
1150   using namespace llvm::support;
1151 
1152   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1153     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1154     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1155   }
1156 }
1157 
1158 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1159                                               BitstreamCursor &Cursor,
1160                                               uint64_t Offset,
1161                                               DeclContext *DC) {
1162   assert(Offset != 0);
1163 
1164   SavedStreamPosition SavedPosition(Cursor);
1165   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1166     Error(std::move(Err));
1167     return true;
1168   }
1169 
1170   RecordData Record;
1171   StringRef Blob;
1172   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1173   if (!MaybeCode) {
1174     Error(MaybeCode.takeError());
1175     return true;
1176   }
1177   unsigned Code = MaybeCode.get();
1178 
1179   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1180   if (!MaybeRecCode) {
1181     Error(MaybeRecCode.takeError());
1182     return true;
1183   }
1184   unsigned RecCode = MaybeRecCode.get();
1185   if (RecCode != DECL_CONTEXT_LEXICAL) {
1186     Error("Expected lexical block");
1187     return true;
1188   }
1189 
1190   assert(!isa<TranslationUnitDecl>(DC) &&
1191          "expected a TU_UPDATE_LEXICAL record for TU");
1192   // If we are handling a C++ class template instantiation, we can see multiple
1193   // lexical updates for the same record. It's important that we select only one
1194   // of them, so that field numbering works properly. Just pick the first one we
1195   // see.
1196   auto &Lex = LexicalDecls[DC];
1197   if (!Lex.first) {
1198     Lex = std::make_pair(
1199         &M, llvm::makeArrayRef(
1200                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1201                     Blob.data()),
1202                 Blob.size() / 4));
1203   }
1204   DC->setHasExternalLexicalStorage(true);
1205   return false;
1206 }
1207 
1208 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1209                                               BitstreamCursor &Cursor,
1210                                               uint64_t Offset,
1211                                               DeclID ID) {
1212   assert(Offset != 0);
1213 
1214   SavedStreamPosition SavedPosition(Cursor);
1215   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1216     Error(std::move(Err));
1217     return true;
1218   }
1219 
1220   RecordData Record;
1221   StringRef Blob;
1222   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1223   if (!MaybeCode) {
1224     Error(MaybeCode.takeError());
1225     return true;
1226   }
1227   unsigned Code = MaybeCode.get();
1228 
1229   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1230   if (!MaybeRecCode) {
1231     Error(MaybeRecCode.takeError());
1232     return true;
1233   }
1234   unsigned RecCode = MaybeRecCode.get();
1235   if (RecCode != DECL_CONTEXT_VISIBLE) {
1236     Error("Expected visible lookup table block");
1237     return true;
1238   }
1239 
1240   // We can't safely determine the primary context yet, so delay attaching the
1241   // lookup table until we're done with recursive deserialization.
1242   auto *Data = (const unsigned char*)Blob.data();
1243   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1244   return false;
1245 }
1246 
1247 void ASTReader::Error(StringRef Msg) const {
1248   Error(diag::err_fe_pch_malformed, Msg);
1249   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1250       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1251     Diag(diag::note_module_cache_path)
1252       << PP.getHeaderSearchInfo().getModuleCachePath();
1253   }
1254 }
1255 
1256 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1257                       StringRef Arg3) const {
1258   if (Diags.isDiagnosticInFlight())
1259     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1260   else
1261     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1262 }
1263 
1264 void ASTReader::Error(llvm::Error &&Err) const {
1265   llvm::Error RemainingErr =
1266       handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1267         auto Diag = E.getDiagnostic().second;
1268 
1269         // Ideally we'd just emit it, but have to handle a possible in-flight
1270         // diagnostic. Note that the location is currently ignored as well.
1271         auto NumArgs = Diag.getStorage()->NumDiagArgs;
1272         assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1273         StringRef Arg1, Arg2, Arg3;
1274         switch (NumArgs) {
1275         case 3:
1276           Arg3 = Diag.getStringArg(2);
1277           LLVM_FALLTHROUGH;
1278         case 2:
1279           Arg2 = Diag.getStringArg(1);
1280           LLVM_FALLTHROUGH;
1281         case 1:
1282           Arg1 = Diag.getStringArg(0);
1283         }
1284         Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1285       });
1286   if (RemainingErr)
1287     Error(toString(std::move(RemainingErr)));
1288 }
1289 
1290 //===----------------------------------------------------------------------===//
1291 // Source Manager Deserialization
1292 //===----------------------------------------------------------------------===//
1293 
1294 /// Read the line table in the source manager block.
1295 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1296   unsigned Idx = 0;
1297   LineTableInfo &LineTable = SourceMgr.getLineTable();
1298 
1299   // Parse the file names
1300   std::map<int, int> FileIDs;
1301   FileIDs[-1] = -1; // For unspecified filenames.
1302   for (unsigned I = 0; Record[Idx]; ++I) {
1303     // Extract the file name
1304     auto Filename = ReadPath(F, Record, Idx);
1305     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1306   }
1307   ++Idx;
1308 
1309   // Parse the line entries
1310   std::vector<LineEntry> Entries;
1311   while (Idx < Record.size()) {
1312     int FID = Record[Idx++];
1313     assert(FID >= 0 && "Serialized line entries for non-local file.");
1314     // Remap FileID from 1-based old view.
1315     FID += F.SLocEntryBaseID - 1;
1316 
1317     // Extract the line entries
1318     unsigned NumEntries = Record[Idx++];
1319     assert(NumEntries && "no line entries for file ID");
1320     Entries.clear();
1321     Entries.reserve(NumEntries);
1322     for (unsigned I = 0; I != NumEntries; ++I) {
1323       unsigned FileOffset = Record[Idx++];
1324       unsigned LineNo = Record[Idx++];
1325       int FilenameID = FileIDs[Record[Idx++]];
1326       SrcMgr::CharacteristicKind FileKind
1327         = (SrcMgr::CharacteristicKind)Record[Idx++];
1328       unsigned IncludeOffset = Record[Idx++];
1329       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1330                                        FileKind, IncludeOffset));
1331     }
1332     LineTable.AddEntry(FileID::get(FID), Entries);
1333   }
1334 }
1335 
1336 /// Read a source manager block
1337 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1338   using namespace SrcMgr;
1339 
1340   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1341 
1342   // Set the source-location entry cursor to the current position in
1343   // the stream. This cursor will be used to read the contents of the
1344   // source manager block initially, and then lazily read
1345   // source-location entries as needed.
1346   SLocEntryCursor = F.Stream;
1347 
1348   // The stream itself is going to skip over the source manager block.
1349   if (llvm::Error Err = F.Stream.SkipBlock())
1350     return Err;
1351 
1352   // Enter the source manager block.
1353   if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1354     return Err;
1355   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1356 
1357   RecordData Record;
1358   while (true) {
1359     Expected<llvm::BitstreamEntry> MaybeE =
1360         SLocEntryCursor.advanceSkippingSubblocks();
1361     if (!MaybeE)
1362       return MaybeE.takeError();
1363     llvm::BitstreamEntry E = MaybeE.get();
1364 
1365     switch (E.Kind) {
1366     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1367     case llvm::BitstreamEntry::Error:
1368       return llvm::createStringError(std::errc::illegal_byte_sequence,
1369                                      "malformed block record in AST file");
1370     case llvm::BitstreamEntry::EndBlock:
1371       return llvm::Error::success();
1372     case llvm::BitstreamEntry::Record:
1373       // The interesting case.
1374       break;
1375     }
1376 
1377     // Read a record.
1378     Record.clear();
1379     StringRef Blob;
1380     Expected<unsigned> MaybeRecord =
1381         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1382     if (!MaybeRecord)
1383       return MaybeRecord.takeError();
1384     switch (MaybeRecord.get()) {
1385     default:  // Default behavior: ignore.
1386       break;
1387 
1388     case SM_SLOC_FILE_ENTRY:
1389     case SM_SLOC_BUFFER_ENTRY:
1390     case SM_SLOC_EXPANSION_ENTRY:
1391       // Once we hit one of the source location entries, we're done.
1392       return llvm::Error::success();
1393     }
1394   }
1395 }
1396 
1397 /// If a header file is not found at the path that we expect it to be
1398 /// and the PCH file was moved from its original location, try to resolve the
1399 /// file by assuming that header+PCH were moved together and the header is in
1400 /// the same place relative to the PCH.
1401 static std::string
1402 resolveFileRelativeToOriginalDir(const std::string &Filename,
1403                                  const std::string &OriginalDir,
1404                                  const std::string &CurrDir) {
1405   assert(OriginalDir != CurrDir &&
1406          "No point trying to resolve the file if the PCH dir didn't change");
1407 
1408   using namespace llvm::sys;
1409 
1410   SmallString<128> filePath(Filename);
1411   fs::make_absolute(filePath);
1412   assert(path::is_absolute(OriginalDir));
1413   SmallString<128> currPCHPath(CurrDir);
1414 
1415   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1416                        fileDirE = path::end(path::parent_path(filePath));
1417   path::const_iterator origDirI = path::begin(OriginalDir),
1418                        origDirE = path::end(OriginalDir);
1419   // Skip the common path components from filePath and OriginalDir.
1420   while (fileDirI != fileDirE && origDirI != origDirE &&
1421          *fileDirI == *origDirI) {
1422     ++fileDirI;
1423     ++origDirI;
1424   }
1425   for (; origDirI != origDirE; ++origDirI)
1426     path::append(currPCHPath, "..");
1427   path::append(currPCHPath, fileDirI, fileDirE);
1428   path::append(currPCHPath, path::filename(Filename));
1429   return std::string(currPCHPath.str());
1430 }
1431 
1432 bool ASTReader::ReadSLocEntry(int ID) {
1433   if (ID == 0)
1434     return false;
1435 
1436   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1437     Error("source location entry ID out-of-range for AST file");
1438     return true;
1439   }
1440 
1441   // Local helper to read the (possibly-compressed) buffer data following the
1442   // entry record.
1443   auto ReadBuffer = [this](
1444       BitstreamCursor &SLocEntryCursor,
1445       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1446     RecordData Record;
1447     StringRef Blob;
1448     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1449     if (!MaybeCode) {
1450       Error(MaybeCode.takeError());
1451       return nullptr;
1452     }
1453     unsigned Code = MaybeCode.get();
1454 
1455     Expected<unsigned> MaybeRecCode =
1456         SLocEntryCursor.readRecord(Code, Record, &Blob);
1457     if (!MaybeRecCode) {
1458       Error(MaybeRecCode.takeError());
1459       return nullptr;
1460     }
1461     unsigned RecCode = MaybeRecCode.get();
1462 
1463     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1464       if (!llvm::zlib::isAvailable()) {
1465         Error("zlib is not available");
1466         return nullptr;
1467       }
1468       SmallString<0> Uncompressed;
1469       if (llvm::Error E =
1470               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1471         Error("could not decompress embedded file contents: " +
1472               llvm::toString(std::move(E)));
1473         return nullptr;
1474       }
1475       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1476     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1477       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1478     } else {
1479       Error("AST record has invalid code");
1480       return nullptr;
1481     }
1482   };
1483 
1484   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1485   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1486           F->SLocEntryOffsetsBase +
1487           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1488     Error(std::move(Err));
1489     return true;
1490   }
1491 
1492   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1493   SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1494 
1495   ++NumSLocEntriesRead;
1496   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1497   if (!MaybeEntry) {
1498     Error(MaybeEntry.takeError());
1499     return true;
1500   }
1501   llvm::BitstreamEntry Entry = MaybeEntry.get();
1502 
1503   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1504     Error("incorrectly-formatted source location entry in AST file");
1505     return true;
1506   }
1507 
1508   RecordData Record;
1509   StringRef Blob;
1510   Expected<unsigned> MaybeSLOC =
1511       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1512   if (!MaybeSLOC) {
1513     Error(MaybeSLOC.takeError());
1514     return true;
1515   }
1516   switch (MaybeSLOC.get()) {
1517   default:
1518     Error("incorrectly-formatted source location entry in AST file");
1519     return true;
1520 
1521   case SM_SLOC_FILE_ENTRY: {
1522     // We will detect whether a file changed and return 'Failure' for it, but
1523     // we will also try to fail gracefully by setting up the SLocEntry.
1524     unsigned InputID = Record[4];
1525     InputFile IF = getInputFile(*F, InputID);
1526     Optional<FileEntryRef> File = IF.getFile();
1527     bool OverriddenBuffer = IF.isOverridden();
1528 
1529     // Note that we only check if a File was returned. If it was out-of-date
1530     // we have complained but we will continue creating a FileID to recover
1531     // gracefully.
1532     if (!File)
1533       return true;
1534 
1535     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1536     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1537       // This is the module's main file.
1538       IncludeLoc = getImportLocation(F);
1539     }
1540     SrcMgr::CharacteristicKind
1541       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1542     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1543                                         BaseOffset + Record[0]);
1544     SrcMgr::FileInfo &FileInfo =
1545           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1546     FileInfo.NumCreatedFIDs = Record[5];
1547     if (Record[3])
1548       FileInfo.setHasLineDirectives();
1549 
1550     unsigned NumFileDecls = Record[7];
1551     if (NumFileDecls && ContextObj) {
1552       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1553       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1554       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1555                                                              NumFileDecls));
1556     }
1557 
1558     const SrcMgr::ContentCache &ContentCache =
1559         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1560     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1561         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1562         !ContentCache.getBufferIfLoaded()) {
1563       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1564       if (!Buffer)
1565         return true;
1566       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1567     }
1568 
1569     break;
1570   }
1571 
1572   case SM_SLOC_BUFFER_ENTRY: {
1573     const char *Name = Blob.data();
1574     unsigned Offset = Record[0];
1575     SrcMgr::CharacteristicKind
1576       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1577     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1578     if (IncludeLoc.isInvalid() && F->isModule()) {
1579       IncludeLoc = getImportLocation(F);
1580     }
1581 
1582     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1583     if (!Buffer)
1584       return true;
1585     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1586                            BaseOffset + Offset, IncludeLoc);
1587     break;
1588   }
1589 
1590   case SM_SLOC_EXPANSION_ENTRY: {
1591     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1592     SourceMgr.createExpansionLoc(SpellingLoc,
1593                                      ReadSourceLocation(*F, Record[2]),
1594                                      ReadSourceLocation(*F, Record[3]),
1595                                      Record[5],
1596                                      Record[4],
1597                                      ID,
1598                                      BaseOffset + Record[0]);
1599     break;
1600   }
1601   }
1602 
1603   return false;
1604 }
1605 
1606 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1607   if (ID == 0)
1608     return std::make_pair(SourceLocation(), "");
1609 
1610   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1611     Error("source location entry ID out-of-range for AST file");
1612     return std::make_pair(SourceLocation(), "");
1613   }
1614 
1615   // Find which module file this entry lands in.
1616   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1617   if (!M->isModule())
1618     return std::make_pair(SourceLocation(), "");
1619 
1620   // FIXME: Can we map this down to a particular submodule? That would be
1621   // ideal.
1622   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1623 }
1624 
1625 /// Find the location where the module F is imported.
1626 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1627   if (F->ImportLoc.isValid())
1628     return F->ImportLoc;
1629 
1630   // Otherwise we have a PCH. It's considered to be "imported" at the first
1631   // location of its includer.
1632   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1633     // Main file is the importer.
1634     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1635     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1636   }
1637   return F->ImportedBy[0]->FirstLoc;
1638 }
1639 
1640 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1641 /// the abbreviations that are at the top of the block and then leave the cursor
1642 /// pointing into the block.
1643 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1644                                         unsigned BlockID,
1645                                         uint64_t *StartOfBlockOffset) {
1646   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1647     return Err;
1648 
1649   if (StartOfBlockOffset)
1650     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1651 
1652   while (true) {
1653     uint64_t Offset = Cursor.GetCurrentBitNo();
1654     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1655     if (!MaybeCode)
1656       return MaybeCode.takeError();
1657     unsigned Code = MaybeCode.get();
1658 
1659     // We expect all abbrevs to be at the start of the block.
1660     if (Code != llvm::bitc::DEFINE_ABBREV) {
1661       if (llvm::Error Err = Cursor.JumpToBit(Offset))
1662         return Err;
1663       return llvm::Error::success();
1664     }
1665     if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1666       return Err;
1667   }
1668 }
1669 
1670 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1671                            unsigned &Idx) {
1672   Token Tok;
1673   Tok.startToken();
1674   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1675   Tok.setLength(Record[Idx++]);
1676   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1677     Tok.setIdentifierInfo(II);
1678   Tok.setKind((tok::TokenKind)Record[Idx++]);
1679   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1680   return Tok;
1681 }
1682 
1683 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1684   BitstreamCursor &Stream = F.MacroCursor;
1685 
1686   // Keep track of where we are in the stream, then jump back there
1687   // after reading this macro.
1688   SavedStreamPosition SavedPosition(Stream);
1689 
1690   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1691     // FIXME this drops errors on the floor.
1692     consumeError(std::move(Err));
1693     return nullptr;
1694   }
1695   RecordData Record;
1696   SmallVector<IdentifierInfo*, 16> MacroParams;
1697   MacroInfo *Macro = nullptr;
1698 
1699   while (true) {
1700     // Advance to the next record, but if we get to the end of the block, don't
1701     // pop it (removing all the abbreviations from the cursor) since we want to
1702     // be able to reseek within the block and read entries.
1703     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1704     Expected<llvm::BitstreamEntry> MaybeEntry =
1705         Stream.advanceSkippingSubblocks(Flags);
1706     if (!MaybeEntry) {
1707       Error(MaybeEntry.takeError());
1708       return Macro;
1709     }
1710     llvm::BitstreamEntry Entry = MaybeEntry.get();
1711 
1712     switch (Entry.Kind) {
1713     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1714     case llvm::BitstreamEntry::Error:
1715       Error("malformed block record in AST file");
1716       return Macro;
1717     case llvm::BitstreamEntry::EndBlock:
1718       return Macro;
1719     case llvm::BitstreamEntry::Record:
1720       // The interesting case.
1721       break;
1722     }
1723 
1724     // Read a record.
1725     Record.clear();
1726     PreprocessorRecordTypes RecType;
1727     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1728       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1729     else {
1730       Error(MaybeRecType.takeError());
1731       return Macro;
1732     }
1733     switch (RecType) {
1734     case PP_MODULE_MACRO:
1735     case PP_MACRO_DIRECTIVE_HISTORY:
1736       return Macro;
1737 
1738     case PP_MACRO_OBJECT_LIKE:
1739     case PP_MACRO_FUNCTION_LIKE: {
1740       // If we already have a macro, that means that we've hit the end
1741       // of the definition of the macro we were looking for. We're
1742       // done.
1743       if (Macro)
1744         return Macro;
1745 
1746       unsigned NextIndex = 1; // Skip identifier ID.
1747       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1748       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1749       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1750       MI->setIsUsed(Record[NextIndex++]);
1751       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1752 
1753       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1754         // Decode function-like macro info.
1755         bool isC99VarArgs = Record[NextIndex++];
1756         bool isGNUVarArgs = Record[NextIndex++];
1757         bool hasCommaPasting = Record[NextIndex++];
1758         MacroParams.clear();
1759         unsigned NumArgs = Record[NextIndex++];
1760         for (unsigned i = 0; i != NumArgs; ++i)
1761           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1762 
1763         // Install function-like macro info.
1764         MI->setIsFunctionLike();
1765         if (isC99VarArgs) MI->setIsC99Varargs();
1766         if (isGNUVarArgs) MI->setIsGNUVarargs();
1767         if (hasCommaPasting) MI->setHasCommaPasting();
1768         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1769       }
1770 
1771       // Remember that we saw this macro last so that we add the tokens that
1772       // form its body to it.
1773       Macro = MI;
1774 
1775       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1776           Record[NextIndex]) {
1777         // We have a macro definition. Register the association
1778         PreprocessedEntityID
1779             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1780         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1781         PreprocessingRecord::PPEntityID PPID =
1782             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1783         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1784             PPRec.getPreprocessedEntity(PPID));
1785         if (PPDef)
1786           PPRec.RegisterMacroDefinition(Macro, PPDef);
1787       }
1788 
1789       ++NumMacrosRead;
1790       break;
1791     }
1792 
1793     case PP_TOKEN: {
1794       // If we see a TOKEN before a PP_MACRO_*, then the file is
1795       // erroneous, just pretend we didn't see this.
1796       if (!Macro) break;
1797 
1798       unsigned Idx = 0;
1799       Token Tok = ReadToken(F, Record, Idx);
1800       Macro->AddTokenToBody(Tok);
1801       break;
1802     }
1803     }
1804   }
1805 }
1806 
1807 PreprocessedEntityID
1808 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1809                                          unsigned LocalID) const {
1810   if (!M.ModuleOffsetMap.empty())
1811     ReadModuleOffsetMap(M);
1812 
1813   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1814     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1815   assert(I != M.PreprocessedEntityRemap.end()
1816          && "Invalid index into preprocessed entity index remap");
1817 
1818   return LocalID + I->second;
1819 }
1820 
1821 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1822   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1823 }
1824 
1825 HeaderFileInfoTrait::internal_key_type
1826 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1827   internal_key_type ikey = {FE->getSize(),
1828                             M.HasTimestamps ? FE->getModificationTime() : 0,
1829                             FE->getName(), /*Imported*/ false};
1830   return ikey;
1831 }
1832 
1833 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1834   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1835     return false;
1836 
1837   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1838     return true;
1839 
1840   // Determine whether the actual files are equivalent.
1841   FileManager &FileMgr = Reader.getFileManager();
1842   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1843     if (!Key.Imported) {
1844       if (auto File = FileMgr.getFile(Key.Filename))
1845         return *File;
1846       return nullptr;
1847     }
1848 
1849     std::string Resolved = std::string(Key.Filename);
1850     Reader.ResolveImportedPath(M, Resolved);
1851     if (auto File = FileMgr.getFile(Resolved))
1852       return *File;
1853     return nullptr;
1854   };
1855 
1856   const FileEntry *FEA = GetFile(a);
1857   const FileEntry *FEB = GetFile(b);
1858   return FEA && FEA == FEB;
1859 }
1860 
1861 std::pair<unsigned, unsigned>
1862 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1863   return readULEBKeyDataLength(d);
1864 }
1865 
1866 HeaderFileInfoTrait::internal_key_type
1867 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1868   using namespace llvm::support;
1869 
1870   internal_key_type ikey;
1871   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1872   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1873   ikey.Filename = (const char *)d;
1874   ikey.Imported = true;
1875   return ikey;
1876 }
1877 
1878 HeaderFileInfoTrait::data_type
1879 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1880                               unsigned DataLen) {
1881   using namespace llvm::support;
1882 
1883   const unsigned char *End = d + DataLen;
1884   HeaderFileInfo HFI;
1885   unsigned Flags = *d++;
1886   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1887   HFI.isImport |= (Flags >> 5) & 0x01;
1888   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1889   HFI.DirInfo = (Flags >> 1) & 0x07;
1890   HFI.IndexHeaderMapHeader = Flags & 0x01;
1891   // FIXME: Find a better way to handle this. Maybe just store a
1892   // "has been included" flag?
1893   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1894                              HFI.NumIncludes);
1895   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1896       M, endian::readNext<uint32_t, little, unaligned>(d));
1897   if (unsigned FrameworkOffset =
1898           endian::readNext<uint32_t, little, unaligned>(d)) {
1899     // The framework offset is 1 greater than the actual offset,
1900     // since 0 is used as an indicator for "no framework name".
1901     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1902     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1903   }
1904 
1905   assert((End - d) % 4 == 0 &&
1906          "Wrong data length in HeaderFileInfo deserialization");
1907   while (d != End) {
1908     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1909     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1910     LocalSMID >>= 2;
1911 
1912     // This header is part of a module. Associate it with the module to enable
1913     // implicit module import.
1914     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1915     Module *Mod = Reader.getSubmodule(GlobalSMID);
1916     FileManager &FileMgr = Reader.getFileManager();
1917     ModuleMap &ModMap =
1918         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1919 
1920     std::string Filename = std::string(key.Filename);
1921     if (key.Imported)
1922       Reader.ResolveImportedPath(M, Filename);
1923     // FIXME: NameAsWritten
1924     Module::Header H = {std::string(key.Filename), "",
1925                         *FileMgr.getFile(Filename)};
1926     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1927     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1928   }
1929 
1930   // This HeaderFileInfo was externally loaded.
1931   HFI.External = true;
1932   HFI.IsValid = true;
1933   return HFI;
1934 }
1935 
1936 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1937                                 uint32_t MacroDirectivesOffset) {
1938   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1939   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1940 }
1941 
1942 void ASTReader::ReadDefinedMacros() {
1943   // Note that we are loading defined macros.
1944   Deserializing Macros(this);
1945 
1946   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1947     BitstreamCursor &MacroCursor = I.MacroCursor;
1948 
1949     // If there was no preprocessor block, skip this file.
1950     if (MacroCursor.getBitcodeBytes().empty())
1951       continue;
1952 
1953     BitstreamCursor Cursor = MacroCursor;
1954     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1955       Error(std::move(Err));
1956       return;
1957     }
1958 
1959     RecordData Record;
1960     while (true) {
1961       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1962       if (!MaybeE) {
1963         Error(MaybeE.takeError());
1964         return;
1965       }
1966       llvm::BitstreamEntry E = MaybeE.get();
1967 
1968       switch (E.Kind) {
1969       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1970       case llvm::BitstreamEntry::Error:
1971         Error("malformed block record in AST file");
1972         return;
1973       case llvm::BitstreamEntry::EndBlock:
1974         goto NextCursor;
1975 
1976       case llvm::BitstreamEntry::Record: {
1977         Record.clear();
1978         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1979         if (!MaybeRecord) {
1980           Error(MaybeRecord.takeError());
1981           return;
1982         }
1983         switch (MaybeRecord.get()) {
1984         default:  // Default behavior: ignore.
1985           break;
1986 
1987         case PP_MACRO_OBJECT_LIKE:
1988         case PP_MACRO_FUNCTION_LIKE: {
1989           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1990           if (II->isOutOfDate())
1991             updateOutOfDateIdentifier(*II);
1992           break;
1993         }
1994 
1995         case PP_TOKEN:
1996           // Ignore tokens.
1997           break;
1998         }
1999         break;
2000       }
2001       }
2002     }
2003     NextCursor:  ;
2004   }
2005 }
2006 
2007 namespace {
2008 
2009   /// Visitor class used to look up identifirs in an AST file.
2010   class IdentifierLookupVisitor {
2011     StringRef Name;
2012     unsigned NameHash;
2013     unsigned PriorGeneration;
2014     unsigned &NumIdentifierLookups;
2015     unsigned &NumIdentifierLookupHits;
2016     IdentifierInfo *Found = nullptr;
2017 
2018   public:
2019     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2020                             unsigned &NumIdentifierLookups,
2021                             unsigned &NumIdentifierLookupHits)
2022       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2023         PriorGeneration(PriorGeneration),
2024         NumIdentifierLookups(NumIdentifierLookups),
2025         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2026 
2027     bool operator()(ModuleFile &M) {
2028       // If we've already searched this module file, skip it now.
2029       if (M.Generation <= PriorGeneration)
2030         return true;
2031 
2032       ASTIdentifierLookupTable *IdTable
2033         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2034       if (!IdTable)
2035         return false;
2036 
2037       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2038                                      Found);
2039       ++NumIdentifierLookups;
2040       ASTIdentifierLookupTable::iterator Pos =
2041           IdTable->find_hashed(Name, NameHash, &Trait);
2042       if (Pos == IdTable->end())
2043         return false;
2044 
2045       // Dereferencing the iterator has the effect of building the
2046       // IdentifierInfo node and populating it with the various
2047       // declarations it needs.
2048       ++NumIdentifierLookupHits;
2049       Found = *Pos;
2050       return true;
2051     }
2052 
2053     // Retrieve the identifier info found within the module
2054     // files.
2055     IdentifierInfo *getIdentifierInfo() const { return Found; }
2056   };
2057 
2058 } // namespace
2059 
2060 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2061   // Note that we are loading an identifier.
2062   Deserializing AnIdentifier(this);
2063 
2064   unsigned PriorGeneration = 0;
2065   if (getContext().getLangOpts().Modules)
2066     PriorGeneration = IdentifierGeneration[&II];
2067 
2068   // If there is a global index, look there first to determine which modules
2069   // provably do not have any results for this identifier.
2070   GlobalModuleIndex::HitSet Hits;
2071   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2072   if (!loadGlobalIndex()) {
2073     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2074       HitsPtr = &Hits;
2075     }
2076   }
2077 
2078   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2079                                   NumIdentifierLookups,
2080                                   NumIdentifierLookupHits);
2081   ModuleMgr.visit(Visitor, HitsPtr);
2082   markIdentifierUpToDate(&II);
2083 }
2084 
2085 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2086   if (!II)
2087     return;
2088 
2089   II->setOutOfDate(false);
2090 
2091   // Update the generation for this identifier.
2092   if (getContext().getLangOpts().Modules)
2093     IdentifierGeneration[II] = getGeneration();
2094 }
2095 
2096 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2097                                     const PendingMacroInfo &PMInfo) {
2098   ModuleFile &M = *PMInfo.M;
2099 
2100   BitstreamCursor &Cursor = M.MacroCursor;
2101   SavedStreamPosition SavedPosition(Cursor);
2102   if (llvm::Error Err =
2103           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2104     Error(std::move(Err));
2105     return;
2106   }
2107 
2108   struct ModuleMacroRecord {
2109     SubmoduleID SubModID;
2110     MacroInfo *MI;
2111     SmallVector<SubmoduleID, 8> Overrides;
2112   };
2113   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2114 
2115   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2116   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2117   // macro histroy.
2118   RecordData Record;
2119   while (true) {
2120     Expected<llvm::BitstreamEntry> MaybeEntry =
2121         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2122     if (!MaybeEntry) {
2123       Error(MaybeEntry.takeError());
2124       return;
2125     }
2126     llvm::BitstreamEntry Entry = MaybeEntry.get();
2127 
2128     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2129       Error("malformed block record in AST file");
2130       return;
2131     }
2132 
2133     Record.clear();
2134     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2135     if (!MaybePP) {
2136       Error(MaybePP.takeError());
2137       return;
2138     }
2139     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2140     case PP_MACRO_DIRECTIVE_HISTORY:
2141       break;
2142 
2143     case PP_MODULE_MACRO: {
2144       ModuleMacros.push_back(ModuleMacroRecord());
2145       auto &Info = ModuleMacros.back();
2146       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2147       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2148       for (int I = 2, N = Record.size(); I != N; ++I)
2149         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2150       continue;
2151     }
2152 
2153     default:
2154       Error("malformed block record in AST file");
2155       return;
2156     }
2157 
2158     // We found the macro directive history; that's the last record
2159     // for this macro.
2160     break;
2161   }
2162 
2163   // Module macros are listed in reverse dependency order.
2164   {
2165     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2166     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2167     for (auto &MMR : ModuleMacros) {
2168       Overrides.clear();
2169       for (unsigned ModID : MMR.Overrides) {
2170         Module *Mod = getSubmodule(ModID);
2171         auto *Macro = PP.getModuleMacro(Mod, II);
2172         assert(Macro && "missing definition for overridden macro");
2173         Overrides.push_back(Macro);
2174       }
2175 
2176       bool Inserted = false;
2177       Module *Owner = getSubmodule(MMR.SubModID);
2178       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2179     }
2180   }
2181 
2182   // Don't read the directive history for a module; we don't have anywhere
2183   // to put it.
2184   if (M.isModule())
2185     return;
2186 
2187   // Deserialize the macro directives history in reverse source-order.
2188   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2189   unsigned Idx = 0, N = Record.size();
2190   while (Idx < N) {
2191     MacroDirective *MD = nullptr;
2192     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2193     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2194     switch (K) {
2195     case MacroDirective::MD_Define: {
2196       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2197       MD = PP.AllocateDefMacroDirective(MI, Loc);
2198       break;
2199     }
2200     case MacroDirective::MD_Undefine:
2201       MD = PP.AllocateUndefMacroDirective(Loc);
2202       break;
2203     case MacroDirective::MD_Visibility:
2204       bool isPublic = Record[Idx++];
2205       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2206       break;
2207     }
2208 
2209     if (!Latest)
2210       Latest = MD;
2211     if (Earliest)
2212       Earliest->setPrevious(MD);
2213     Earliest = MD;
2214   }
2215 
2216   if (Latest)
2217     PP.setLoadedMacroDirective(II, Earliest, Latest);
2218 }
2219 
2220 bool ASTReader::shouldDisableValidationForFile(
2221     const serialization::ModuleFile &M) const {
2222   if (DisableValidationKind == DisableValidationForModuleKind::None)
2223     return false;
2224 
2225   // If a PCH is loaded and validation is disabled for PCH then disable
2226   // validation for the PCH and the modules it loads.
2227   ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2228 
2229   switch (K) {
2230   case MK_MainFile:
2231   case MK_Preamble:
2232   case MK_PCH:
2233     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2234   case MK_ImplicitModule:
2235   case MK_ExplicitModule:
2236   case MK_PrebuiltModule:
2237     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2238   }
2239 
2240   return false;
2241 }
2242 
2243 ASTReader::InputFileInfo
2244 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2245   // Go find this input file.
2246   BitstreamCursor &Cursor = F.InputFilesCursor;
2247   SavedStreamPosition SavedPosition(Cursor);
2248   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2249     // FIXME this drops errors on the floor.
2250     consumeError(std::move(Err));
2251   }
2252 
2253   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2254   if (!MaybeCode) {
2255     // FIXME this drops errors on the floor.
2256     consumeError(MaybeCode.takeError());
2257   }
2258   unsigned Code = MaybeCode.get();
2259   RecordData Record;
2260   StringRef Blob;
2261 
2262   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2263     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2264            "invalid record type for input file");
2265   else {
2266     // FIXME this drops errors on the floor.
2267     consumeError(Maybe.takeError());
2268   }
2269 
2270   assert(Record[0] == ID && "Bogus stored ID or offset");
2271   InputFileInfo R;
2272   R.StoredSize = static_cast<off_t>(Record[1]);
2273   R.StoredTime = static_cast<time_t>(Record[2]);
2274   R.Overridden = static_cast<bool>(Record[3]);
2275   R.Transient = static_cast<bool>(Record[4]);
2276   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2277   R.Filename = std::string(Blob);
2278   ResolveImportedPath(F, R.Filename);
2279 
2280   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2281   if (!MaybeEntry) // FIXME this drops errors on the floor.
2282     consumeError(MaybeEntry.takeError());
2283   llvm::BitstreamEntry Entry = MaybeEntry.get();
2284   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2285          "expected record type for input file hash");
2286 
2287   Record.clear();
2288   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2289     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2290            "invalid record type for input file hash");
2291   else {
2292     // FIXME this drops errors on the floor.
2293     consumeError(Maybe.takeError());
2294   }
2295   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2296                   static_cast<uint64_t>(Record[0]);
2297   return R;
2298 }
2299 
2300 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2301 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2302   // If this ID is bogus, just return an empty input file.
2303   if (ID == 0 || ID > F.InputFilesLoaded.size())
2304     return InputFile();
2305 
2306   // If we've already loaded this input file, return it.
2307   if (F.InputFilesLoaded[ID-1].getFile())
2308     return F.InputFilesLoaded[ID-1];
2309 
2310   if (F.InputFilesLoaded[ID-1].isNotFound())
2311     return InputFile();
2312 
2313   // Go find this input file.
2314   BitstreamCursor &Cursor = F.InputFilesCursor;
2315   SavedStreamPosition SavedPosition(Cursor);
2316   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2317     // FIXME this drops errors on the floor.
2318     consumeError(std::move(Err));
2319   }
2320 
2321   InputFileInfo FI = readInputFileInfo(F, ID);
2322   off_t StoredSize = FI.StoredSize;
2323   time_t StoredTime = FI.StoredTime;
2324   bool Overridden = FI.Overridden;
2325   bool Transient = FI.Transient;
2326   StringRef Filename = FI.Filename;
2327   uint64_t StoredContentHash = FI.ContentHash;
2328 
2329   OptionalFileEntryRefDegradesToFileEntryPtr File =
2330       expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2331 
2332   // If we didn't find the file, resolve it relative to the
2333   // original directory from which this AST file was created.
2334   if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2335       F.OriginalDir != F.BaseDirectory) {
2336     std::string Resolved = resolveFileRelativeToOriginalDir(
2337         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2338     if (!Resolved.empty())
2339       File = expectedToOptional(FileMgr.getFileRef(Resolved));
2340   }
2341 
2342   // For an overridden file, create a virtual file with the stored
2343   // size/timestamp.
2344   if ((Overridden || Transient) && !File)
2345     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2346 
2347   if (!File) {
2348     if (Complain) {
2349       std::string ErrorStr = "could not find file '";
2350       ErrorStr += Filename;
2351       ErrorStr += "' referenced by AST file '";
2352       ErrorStr += F.FileName;
2353       ErrorStr += "'";
2354       Error(ErrorStr);
2355     }
2356     // Record that we didn't find the file.
2357     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2358     return InputFile();
2359   }
2360 
2361   // Check if there was a request to override the contents of the file
2362   // that was part of the precompiled header. Overriding such a file
2363   // can lead to problems when lexing using the source locations from the
2364   // PCH.
2365   SourceManager &SM = getSourceManager();
2366   // FIXME: Reject if the overrides are different.
2367   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2368     if (Complain)
2369       Error(diag::err_fe_pch_file_overridden, Filename);
2370 
2371     // After emitting the diagnostic, bypass the overriding file to recover
2372     // (this creates a separate FileEntry).
2373     File = SM.bypassFileContentsOverride(*File);
2374     if (!File) {
2375       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2376       return InputFile();
2377     }
2378   }
2379 
2380   struct Change {
2381     enum ModificationKind {
2382       Size,
2383       ModTime,
2384       Content,
2385       None,
2386     } Kind;
2387     llvm::Optional<int64_t> Old = llvm::None;
2388     llvm::Optional<int64_t> New = llvm::None;
2389   };
2390   auto HasInputFileChanged = [&]() {
2391     if (StoredSize != File->getSize())
2392       return Change{Change::Size, StoredSize, File->getSize()};
2393     if (!shouldDisableValidationForFile(F) && StoredTime &&
2394         StoredTime != File->getModificationTime()) {
2395       Change MTimeChange = {Change::ModTime, StoredTime,
2396                             File->getModificationTime()};
2397 
2398       // In case the modification time changes but not the content,
2399       // accept the cached file as legit.
2400       if (ValidateASTInputFilesContent &&
2401           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2402         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2403         if (!MemBuffOrError) {
2404           if (!Complain)
2405             return MTimeChange;
2406           std::string ErrorStr = "could not get buffer for file '";
2407           ErrorStr += File->getName();
2408           ErrorStr += "'";
2409           Error(ErrorStr);
2410           return MTimeChange;
2411         }
2412 
2413         // FIXME: hash_value is not guaranteed to be stable!
2414         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2415         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2416           return Change{Change::None};
2417 
2418         return Change{Change::Content};
2419       }
2420       return MTimeChange;
2421     }
2422     return Change{Change::None};
2423   };
2424 
2425   bool IsOutOfDate = false;
2426   auto FileChange = HasInputFileChanged();
2427   // For an overridden file, there is nothing to validate.
2428   if (!Overridden && FileChange.Kind != Change::None) {
2429     if (Complain && !Diags.isDiagnosticInFlight()) {
2430       // Build a list of the PCH imports that got us here (in reverse).
2431       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2432       while (!ImportStack.back()->ImportedBy.empty())
2433         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2434 
2435       // The top-level PCH is stale.
2436       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2437       Diag(diag::err_fe_ast_file_modified)
2438           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2439           << TopLevelPCHName << FileChange.Kind
2440           << (FileChange.Old && FileChange.New)
2441           << llvm::itostr(FileChange.Old.getValueOr(0))
2442           << llvm::itostr(FileChange.New.getValueOr(0));
2443 
2444       // Print the import stack.
2445       if (ImportStack.size() > 1) {
2446         Diag(diag::note_pch_required_by)
2447           << Filename << ImportStack[0]->FileName;
2448         for (unsigned I = 1; I < ImportStack.size(); ++I)
2449           Diag(diag::note_pch_required_by)
2450             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2451       }
2452 
2453       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2454     }
2455 
2456     IsOutOfDate = true;
2457   }
2458   // FIXME: If the file is overridden and we've already opened it,
2459   // issue an error (or split it into a separate FileEntry).
2460 
2461   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2462 
2463   // Note that we've loaded this input file.
2464   F.InputFilesLoaded[ID-1] = IF;
2465   return IF;
2466 }
2467 
2468 /// If we are loading a relocatable PCH or module file, and the filename
2469 /// is not an absolute path, add the system or module root to the beginning of
2470 /// the file name.
2471 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2472   // Resolve relative to the base directory, if we have one.
2473   if (!M.BaseDirectory.empty())
2474     return ResolveImportedPath(Filename, M.BaseDirectory);
2475 }
2476 
2477 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2478   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2479     return;
2480 
2481   SmallString<128> Buffer;
2482   llvm::sys::path::append(Buffer, Prefix, Filename);
2483   Filename.assign(Buffer.begin(), Buffer.end());
2484 }
2485 
2486 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2487   switch (ARR) {
2488   case ASTReader::Failure: return true;
2489   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2490   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2491   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2492   case ASTReader::ConfigurationMismatch:
2493     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2494   case ASTReader::HadErrors: return true;
2495   case ASTReader::Success: return false;
2496   }
2497 
2498   llvm_unreachable("unknown ASTReadResult");
2499 }
2500 
2501 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2502     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2503     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2504     std::string &SuggestedPredefines) {
2505   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2506     // FIXME this drops errors on the floor.
2507     consumeError(std::move(Err));
2508     return Failure;
2509   }
2510 
2511   // Read all of the records in the options block.
2512   RecordData Record;
2513   ASTReadResult Result = Success;
2514   while (true) {
2515     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2516     if (!MaybeEntry) {
2517       // FIXME this drops errors on the floor.
2518       consumeError(MaybeEntry.takeError());
2519       return Failure;
2520     }
2521     llvm::BitstreamEntry Entry = MaybeEntry.get();
2522 
2523     switch (Entry.Kind) {
2524     case llvm::BitstreamEntry::Error:
2525     case llvm::BitstreamEntry::SubBlock:
2526       return Failure;
2527 
2528     case llvm::BitstreamEntry::EndBlock:
2529       return Result;
2530 
2531     case llvm::BitstreamEntry::Record:
2532       // The interesting case.
2533       break;
2534     }
2535 
2536     // Read and process a record.
2537     Record.clear();
2538     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2539     if (!MaybeRecordType) {
2540       // FIXME this drops errors on the floor.
2541       consumeError(MaybeRecordType.takeError());
2542       return Failure;
2543     }
2544     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2545     case LANGUAGE_OPTIONS: {
2546       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2547       if (ParseLanguageOptions(Record, Complain, Listener,
2548                                AllowCompatibleConfigurationMismatch))
2549         Result = ConfigurationMismatch;
2550       break;
2551     }
2552 
2553     case TARGET_OPTIONS: {
2554       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2555       if (ParseTargetOptions(Record, Complain, Listener,
2556                              AllowCompatibleConfigurationMismatch))
2557         Result = ConfigurationMismatch;
2558       break;
2559     }
2560 
2561     case FILE_SYSTEM_OPTIONS: {
2562       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2563       if (!AllowCompatibleConfigurationMismatch &&
2564           ParseFileSystemOptions(Record, Complain, Listener))
2565         Result = ConfigurationMismatch;
2566       break;
2567     }
2568 
2569     case HEADER_SEARCH_OPTIONS: {
2570       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2571       if (!AllowCompatibleConfigurationMismatch &&
2572           ParseHeaderSearchOptions(Record, Complain, Listener))
2573         Result = ConfigurationMismatch;
2574       break;
2575     }
2576 
2577     case PREPROCESSOR_OPTIONS:
2578       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2579       if (!AllowCompatibleConfigurationMismatch &&
2580           ParsePreprocessorOptions(Record, Complain, Listener,
2581                                    SuggestedPredefines))
2582         Result = ConfigurationMismatch;
2583       break;
2584     }
2585   }
2586 }
2587 
2588 ASTReader::ASTReadResult
2589 ASTReader::ReadControlBlock(ModuleFile &F,
2590                             SmallVectorImpl<ImportedModule> &Loaded,
2591                             const ModuleFile *ImportedBy,
2592                             unsigned ClientLoadCapabilities) {
2593   BitstreamCursor &Stream = F.Stream;
2594 
2595   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2596     Error(std::move(Err));
2597     return Failure;
2598   }
2599 
2600   // Lambda to read the unhashed control block the first time it's called.
2601   //
2602   // For PCM files, the unhashed control block cannot be read until after the
2603   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2604   // need to look ahead before reading the IMPORTS record.  For consistency,
2605   // this block is always read somehow (see BitstreamEntry::EndBlock).
2606   bool HasReadUnhashedControlBlock = false;
2607   auto readUnhashedControlBlockOnce = [&]() {
2608     if (!HasReadUnhashedControlBlock) {
2609       HasReadUnhashedControlBlock = true;
2610       if (ASTReadResult Result =
2611               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2612         return Result;
2613     }
2614     return Success;
2615   };
2616 
2617   bool DisableValidation = shouldDisableValidationForFile(F);
2618 
2619   // Read all of the records and blocks in the control block.
2620   RecordData Record;
2621   unsigned NumInputs = 0;
2622   unsigned NumUserInputs = 0;
2623   StringRef BaseDirectoryAsWritten;
2624   while (true) {
2625     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2626     if (!MaybeEntry) {
2627       Error(MaybeEntry.takeError());
2628       return Failure;
2629     }
2630     llvm::BitstreamEntry Entry = MaybeEntry.get();
2631 
2632     switch (Entry.Kind) {
2633     case llvm::BitstreamEntry::Error:
2634       Error("malformed block record in AST file");
2635       return Failure;
2636     case llvm::BitstreamEntry::EndBlock: {
2637       // Validate the module before returning.  This call catches an AST with
2638       // no module name and no imports.
2639       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2640         return Result;
2641 
2642       // Validate input files.
2643       const HeaderSearchOptions &HSOpts =
2644           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2645 
2646       // All user input files reside at the index range [0, NumUserInputs), and
2647       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2648       // loaded module files, ignore missing inputs.
2649       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2650           F.Kind != MK_PrebuiltModule) {
2651         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2652 
2653         // If we are reading a module, we will create a verification timestamp,
2654         // so we verify all input files.  Otherwise, verify only user input
2655         // files.
2656 
2657         unsigned N = NumUserInputs;
2658         if (ValidateSystemInputs ||
2659             (HSOpts.ModulesValidateOncePerBuildSession &&
2660              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2661              F.Kind == MK_ImplicitModule))
2662           N = NumInputs;
2663 
2664         for (unsigned I = 0; I < N; ++I) {
2665           InputFile IF = getInputFile(F, I+1, Complain);
2666           if (!IF.getFile() || IF.isOutOfDate())
2667             return OutOfDate;
2668         }
2669       }
2670 
2671       if (Listener)
2672         Listener->visitModuleFile(F.FileName, F.Kind);
2673 
2674       if (Listener && Listener->needsInputFileVisitation()) {
2675         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2676                                                                 : NumUserInputs;
2677         for (unsigned I = 0; I < N; ++I) {
2678           bool IsSystem = I >= NumUserInputs;
2679           InputFileInfo FI = readInputFileInfo(F, I+1);
2680           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2681                                    F.Kind == MK_ExplicitModule ||
2682                                    F.Kind == MK_PrebuiltModule);
2683         }
2684       }
2685 
2686       return Success;
2687     }
2688 
2689     case llvm::BitstreamEntry::SubBlock:
2690       switch (Entry.ID) {
2691       case INPUT_FILES_BLOCK_ID:
2692         F.InputFilesCursor = Stream;
2693         if (llvm::Error Err = Stream.SkipBlock()) {
2694           Error(std::move(Err));
2695           return Failure;
2696         }
2697         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2698           Error("malformed block record in AST file");
2699           return Failure;
2700         }
2701         continue;
2702 
2703       case OPTIONS_BLOCK_ID:
2704         // If we're reading the first module for this group, check its options
2705         // are compatible with ours. For modules it imports, no further checking
2706         // is required, because we checked them when we built it.
2707         if (Listener && !ImportedBy) {
2708           // Should we allow the configuration of the module file to differ from
2709           // the configuration of the current translation unit in a compatible
2710           // way?
2711           //
2712           // FIXME: Allow this for files explicitly specified with -include-pch.
2713           bool AllowCompatibleConfigurationMismatch =
2714               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2715 
2716           ASTReadResult Result =
2717               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2718                                AllowCompatibleConfigurationMismatch, *Listener,
2719                                SuggestedPredefines);
2720           if (Result == Failure) {
2721             Error("malformed block record in AST file");
2722             return Result;
2723           }
2724 
2725           if (DisableValidation ||
2726               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2727             Result = Success;
2728 
2729           // If we can't load the module, exit early since we likely
2730           // will rebuild the module anyway. The stream may be in the
2731           // middle of a block.
2732           if (Result != Success)
2733             return Result;
2734         } else if (llvm::Error Err = Stream.SkipBlock()) {
2735           Error(std::move(Err));
2736           return Failure;
2737         }
2738         continue;
2739 
2740       default:
2741         if (llvm::Error Err = Stream.SkipBlock()) {
2742           Error(std::move(Err));
2743           return Failure;
2744         }
2745         continue;
2746       }
2747 
2748     case llvm::BitstreamEntry::Record:
2749       // The interesting case.
2750       break;
2751     }
2752 
2753     // Read and process a record.
2754     Record.clear();
2755     StringRef Blob;
2756     Expected<unsigned> MaybeRecordType =
2757         Stream.readRecord(Entry.ID, Record, &Blob);
2758     if (!MaybeRecordType) {
2759       Error(MaybeRecordType.takeError());
2760       return Failure;
2761     }
2762     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2763     case METADATA: {
2764       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2765         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2766           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2767                                         : diag::err_pch_version_too_new);
2768         return VersionMismatch;
2769       }
2770 
2771       bool hasErrors = Record[6];
2772       if (hasErrors && !DisableValidation) {
2773         // If requested by the caller and the module hasn't already been read
2774         // or compiled, mark modules on error as out-of-date.
2775         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2776             canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2777           return OutOfDate;
2778 
2779         if (!AllowASTWithCompilerErrors) {
2780           Diag(diag::err_pch_with_compiler_errors);
2781           return HadErrors;
2782         }
2783       }
2784       if (hasErrors) {
2785         Diags.ErrorOccurred = true;
2786         Diags.UncompilableErrorOccurred = true;
2787         Diags.UnrecoverableErrorOccurred = true;
2788       }
2789 
2790       F.RelocatablePCH = Record[4];
2791       // Relative paths in a relocatable PCH are relative to our sysroot.
2792       if (F.RelocatablePCH)
2793         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2794 
2795       F.HasTimestamps = Record[5];
2796 
2797       const std::string &CurBranch = getClangFullRepositoryVersion();
2798       StringRef ASTBranch = Blob;
2799       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2800         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2801           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2802         return VersionMismatch;
2803       }
2804       break;
2805     }
2806 
2807     case IMPORTS: {
2808       // Validate the AST before processing any imports (otherwise, untangling
2809       // them can be error-prone and expensive).  A module will have a name and
2810       // will already have been validated, but this catches the PCH case.
2811       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2812         return Result;
2813 
2814       // Load each of the imported PCH files.
2815       unsigned Idx = 0, N = Record.size();
2816       while (Idx < N) {
2817         // Read information about the AST file.
2818         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2819         // The import location will be the local one for now; we will adjust
2820         // all import locations of module imports after the global source
2821         // location info are setup, in ReadAST.
2822         SourceLocation ImportLoc =
2823             ReadUntranslatedSourceLocation(Record[Idx++]);
2824         off_t StoredSize = (off_t)Record[Idx++];
2825         time_t StoredModTime = (time_t)Record[Idx++];
2826         auto FirstSignatureByte = Record.begin() + Idx;
2827         ASTFileSignature StoredSignature = ASTFileSignature::create(
2828             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2829         Idx += ASTFileSignature::size;
2830 
2831         std::string ImportedName = ReadString(Record, Idx);
2832         std::string ImportedFile;
2833 
2834         // For prebuilt and explicit modules first consult the file map for
2835         // an override. Note that here we don't search prebuilt module
2836         // directories, only the explicit name to file mappings. Also, we will
2837         // still verify the size/signature making sure it is essentially the
2838         // same file but perhaps in a different location.
2839         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2840           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2841             ImportedName, /*FileMapOnly*/ true);
2842 
2843         if (ImportedFile.empty())
2844           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2845           // ModuleCache as when writing.
2846           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2847         else
2848           SkipPath(Record, Idx);
2849 
2850         // If our client can't cope with us being out of date, we can't cope with
2851         // our dependency being missing.
2852         unsigned Capabilities = ClientLoadCapabilities;
2853         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2854           Capabilities &= ~ARR_Missing;
2855 
2856         // Load the AST file.
2857         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2858                                   Loaded, StoredSize, StoredModTime,
2859                                   StoredSignature, Capabilities);
2860 
2861         // If we diagnosed a problem, produce a backtrace.
2862         bool recompilingFinalized =
2863             Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
2864             getModuleManager().getModuleCache().isPCMFinal(F.FileName);
2865         if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
2866           Diag(diag::note_module_file_imported_by)
2867               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2868         if (recompilingFinalized)
2869           Diag(diag::note_module_file_conflict);
2870 
2871         switch (Result) {
2872         case Failure: return Failure;
2873           // If we have to ignore the dependency, we'll have to ignore this too.
2874         case Missing:
2875         case OutOfDate: return OutOfDate;
2876         case VersionMismatch: return VersionMismatch;
2877         case ConfigurationMismatch: return ConfigurationMismatch;
2878         case HadErrors: return HadErrors;
2879         case Success: break;
2880         }
2881       }
2882       break;
2883     }
2884 
2885     case ORIGINAL_FILE:
2886       F.OriginalSourceFileID = FileID::get(Record[0]);
2887       F.ActualOriginalSourceFileName = std::string(Blob);
2888       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2889       ResolveImportedPath(F, F.OriginalSourceFileName);
2890       break;
2891 
2892     case ORIGINAL_FILE_ID:
2893       F.OriginalSourceFileID = FileID::get(Record[0]);
2894       break;
2895 
2896     case ORIGINAL_PCH_DIR:
2897       F.OriginalDir = std::string(Blob);
2898       break;
2899 
2900     case MODULE_NAME:
2901       F.ModuleName = std::string(Blob);
2902       Diag(diag::remark_module_import)
2903           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2904           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2905       if (Listener)
2906         Listener->ReadModuleName(F.ModuleName);
2907 
2908       // Validate the AST as soon as we have a name so we can exit early on
2909       // failure.
2910       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2911         return Result;
2912 
2913       break;
2914 
2915     case MODULE_DIRECTORY: {
2916       // Save the BaseDirectory as written in the PCM for computing the module
2917       // filename for the ModuleCache.
2918       BaseDirectoryAsWritten = Blob;
2919       assert(!F.ModuleName.empty() &&
2920              "MODULE_DIRECTORY found before MODULE_NAME");
2921       // If we've already loaded a module map file covering this module, we may
2922       // have a better path for it (relative to the current build).
2923       Module *M = PP.getHeaderSearchInfo().lookupModule(
2924           F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
2925           /*AllowExtraModuleMapSearch*/ true);
2926       if (M && M->Directory) {
2927         // If we're implicitly loading a module, the base directory can't
2928         // change between the build and use.
2929         // Don't emit module relocation error if we have -fno-validate-pch
2930         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2931                   DisableValidationForModuleKind::Module) &&
2932             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2933           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2934           if (!BuildDir || *BuildDir != M->Directory) {
2935             if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2936               Diag(diag::err_imported_module_relocated)
2937                   << F.ModuleName << Blob << M->Directory->getName();
2938             return OutOfDate;
2939           }
2940         }
2941         F.BaseDirectory = std::string(M->Directory->getName());
2942       } else {
2943         F.BaseDirectory = std::string(Blob);
2944       }
2945       break;
2946     }
2947 
2948     case MODULE_MAP_FILE:
2949       if (ASTReadResult Result =
2950               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2951         return Result;
2952       break;
2953 
2954     case INPUT_FILE_OFFSETS:
2955       NumInputs = Record[0];
2956       NumUserInputs = Record[1];
2957       F.InputFileOffsets =
2958           (const llvm::support::unaligned_uint64_t *)Blob.data();
2959       F.InputFilesLoaded.resize(NumInputs);
2960       F.NumUserInputFiles = NumUserInputs;
2961       break;
2962     }
2963   }
2964 }
2965 
2966 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
2967                                     unsigned ClientLoadCapabilities) {
2968   BitstreamCursor &Stream = F.Stream;
2969 
2970   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
2971     return Err;
2972   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2973 
2974   // Read all of the records and blocks for the AST file.
2975   RecordData Record;
2976   while (true) {
2977     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2978     if (!MaybeEntry)
2979       return MaybeEntry.takeError();
2980     llvm::BitstreamEntry Entry = MaybeEntry.get();
2981 
2982     switch (Entry.Kind) {
2983     case llvm::BitstreamEntry::Error:
2984       return llvm::createStringError(
2985           std::errc::illegal_byte_sequence,
2986           "error at end of module block in AST file");
2987     case llvm::BitstreamEntry::EndBlock:
2988       // Outside of C++, we do not store a lookup map for the translation unit.
2989       // Instead, mark it as needing a lookup map to be built if this module
2990       // contains any declarations lexically within it (which it always does!).
2991       // This usually has no cost, since we very rarely need the lookup map for
2992       // the translation unit outside C++.
2993       if (ASTContext *Ctx = ContextObj) {
2994         DeclContext *DC = Ctx->getTranslationUnitDecl();
2995         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2996           DC->setMustBuildLookupTable();
2997       }
2998 
2999       return llvm::Error::success();
3000     case llvm::BitstreamEntry::SubBlock:
3001       switch (Entry.ID) {
3002       case DECLTYPES_BLOCK_ID:
3003         // We lazily load the decls block, but we want to set up the
3004         // DeclsCursor cursor to point into it.  Clone our current bitcode
3005         // cursor to it, enter the block and read the abbrevs in that block.
3006         // With the main cursor, we just skip over it.
3007         F.DeclsCursor = Stream;
3008         if (llvm::Error Err = Stream.SkipBlock())
3009           return Err;
3010         if (llvm::Error Err = ReadBlockAbbrevs(
3011                 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))
3012           return Err;
3013         break;
3014 
3015       case PREPROCESSOR_BLOCK_ID:
3016         F.MacroCursor = Stream;
3017         if (!PP.getExternalSource())
3018           PP.setExternalSource(this);
3019 
3020         if (llvm::Error Err = Stream.SkipBlock())
3021           return Err;
3022         if (llvm::Error Err =
3023                 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3024           return Err;
3025         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3026         break;
3027 
3028       case PREPROCESSOR_DETAIL_BLOCK_ID:
3029         F.PreprocessorDetailCursor = Stream;
3030 
3031         if (llvm::Error Err = Stream.SkipBlock()) {
3032           return Err;
3033         }
3034         if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3035                                                PREPROCESSOR_DETAIL_BLOCK_ID))
3036           return Err;
3037         F.PreprocessorDetailStartOffset
3038         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3039 
3040         if (!PP.getPreprocessingRecord())
3041           PP.createPreprocessingRecord();
3042         if (!PP.getPreprocessingRecord()->getExternalSource())
3043           PP.getPreprocessingRecord()->SetExternalSource(*this);
3044         break;
3045 
3046       case SOURCE_MANAGER_BLOCK_ID:
3047         if (llvm::Error Err = ReadSourceManagerBlock(F))
3048           return Err;
3049         break;
3050 
3051       case SUBMODULE_BLOCK_ID:
3052         if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3053           return Err;
3054         break;
3055 
3056       case COMMENTS_BLOCK_ID: {
3057         BitstreamCursor C = Stream;
3058 
3059         if (llvm::Error Err = Stream.SkipBlock())
3060           return Err;
3061         if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3062           return Err;
3063         CommentsCursors.push_back(std::make_pair(C, &F));
3064         break;
3065       }
3066 
3067       default:
3068         if (llvm::Error Err = Stream.SkipBlock())
3069           return Err;
3070         break;
3071       }
3072       continue;
3073 
3074     case llvm::BitstreamEntry::Record:
3075       // The interesting case.
3076       break;
3077     }
3078 
3079     // Read and process a record.
3080     Record.clear();
3081     StringRef Blob;
3082     Expected<unsigned> MaybeRecordType =
3083         Stream.readRecord(Entry.ID, Record, &Blob);
3084     if (!MaybeRecordType)
3085       return MaybeRecordType.takeError();
3086     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3087 
3088     // If we're not loading an AST context, we don't care about most records.
3089     if (!ContextObj) {
3090       switch (RecordType) {
3091       case IDENTIFIER_TABLE:
3092       case IDENTIFIER_OFFSET:
3093       case INTERESTING_IDENTIFIERS:
3094       case STATISTICS:
3095       case PP_CONDITIONAL_STACK:
3096       case PP_COUNTER_VALUE:
3097       case SOURCE_LOCATION_OFFSETS:
3098       case MODULE_OFFSET_MAP:
3099       case SOURCE_MANAGER_LINE_TABLE:
3100       case SOURCE_LOCATION_PRELOADS:
3101       case PPD_ENTITIES_OFFSETS:
3102       case HEADER_SEARCH_TABLE:
3103       case IMPORTED_MODULES:
3104       case MACRO_OFFSET:
3105         break;
3106       default:
3107         continue;
3108       }
3109     }
3110 
3111     switch (RecordType) {
3112     default:  // Default behavior: ignore.
3113       break;
3114 
3115     case TYPE_OFFSET: {
3116       if (F.LocalNumTypes != 0)
3117         return llvm::createStringError(
3118             std::errc::illegal_byte_sequence,
3119             "duplicate TYPE_OFFSET record in AST file");
3120       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3121       F.LocalNumTypes = Record[0];
3122       unsigned LocalBaseTypeIndex = Record[1];
3123       F.BaseTypeIndex = getTotalNumTypes();
3124 
3125       if (F.LocalNumTypes > 0) {
3126         // Introduce the global -> local mapping for types within this module.
3127         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3128 
3129         // Introduce the local -> global mapping for types within this module.
3130         F.TypeRemap.insertOrReplace(
3131           std::make_pair(LocalBaseTypeIndex,
3132                          F.BaseTypeIndex - LocalBaseTypeIndex));
3133 
3134         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3135       }
3136       break;
3137     }
3138 
3139     case DECL_OFFSET: {
3140       if (F.LocalNumDecls != 0)
3141         return llvm::createStringError(
3142             std::errc::illegal_byte_sequence,
3143             "duplicate DECL_OFFSET record in AST file");
3144       F.DeclOffsets = (const DeclOffset *)Blob.data();
3145       F.LocalNumDecls = Record[0];
3146       unsigned LocalBaseDeclID = Record[1];
3147       F.BaseDeclID = getTotalNumDecls();
3148 
3149       if (F.LocalNumDecls > 0) {
3150         // Introduce the global -> local mapping for declarations within this
3151         // module.
3152         GlobalDeclMap.insert(
3153           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3154 
3155         // Introduce the local -> global mapping for declarations within this
3156         // module.
3157         F.DeclRemap.insertOrReplace(
3158           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3159 
3160         // Introduce the global -> local mapping for declarations within this
3161         // module.
3162         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3163 
3164         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3165       }
3166       break;
3167     }
3168 
3169     case TU_UPDATE_LEXICAL: {
3170       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3171       LexicalContents Contents(
3172           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3173               Blob.data()),
3174           static_cast<unsigned int>(Blob.size() / 4));
3175       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3176       TU->setHasExternalLexicalStorage(true);
3177       break;
3178     }
3179 
3180     case UPDATE_VISIBLE: {
3181       unsigned Idx = 0;
3182       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3183       auto *Data = (const unsigned char*)Blob.data();
3184       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3185       // If we've already loaded the decl, perform the updates when we finish
3186       // loading this block.
3187       if (Decl *D = GetExistingDecl(ID))
3188         PendingUpdateRecords.push_back(
3189             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3190       break;
3191     }
3192 
3193     case IDENTIFIER_TABLE:
3194       F.IdentifierTableData =
3195           reinterpret_cast<const unsigned char *>(Blob.data());
3196       if (Record[0]) {
3197         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3198             F.IdentifierTableData + Record[0],
3199             F.IdentifierTableData + sizeof(uint32_t),
3200             F.IdentifierTableData,
3201             ASTIdentifierLookupTrait(*this, F));
3202 
3203         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3204       }
3205       break;
3206 
3207     case IDENTIFIER_OFFSET: {
3208       if (F.LocalNumIdentifiers != 0)
3209         return llvm::createStringError(
3210             std::errc::illegal_byte_sequence,
3211             "duplicate IDENTIFIER_OFFSET record in AST file");
3212       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3213       F.LocalNumIdentifiers = Record[0];
3214       unsigned LocalBaseIdentifierID = Record[1];
3215       F.BaseIdentifierID = getTotalNumIdentifiers();
3216 
3217       if (F.LocalNumIdentifiers > 0) {
3218         // Introduce the global -> local mapping for identifiers within this
3219         // module.
3220         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3221                                                   &F));
3222 
3223         // Introduce the local -> global mapping for identifiers within this
3224         // module.
3225         F.IdentifierRemap.insertOrReplace(
3226           std::make_pair(LocalBaseIdentifierID,
3227                          F.BaseIdentifierID - LocalBaseIdentifierID));
3228 
3229         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3230                                  + F.LocalNumIdentifiers);
3231       }
3232       break;
3233     }
3234 
3235     case INTERESTING_IDENTIFIERS:
3236       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3237       break;
3238 
3239     case EAGERLY_DESERIALIZED_DECLS:
3240       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3241       // about "interesting" decls (for instance, if we're building a module).
3242       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3243         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3244       break;
3245 
3246     case MODULAR_CODEGEN_DECLS:
3247       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3248       // them (ie: if we're not codegenerating this module).
3249       if (F.Kind == MK_MainFile ||
3250           getContext().getLangOpts().BuildingPCHWithObjectFile)
3251         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3252           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3253       break;
3254 
3255     case SPECIAL_TYPES:
3256       if (SpecialTypes.empty()) {
3257         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3258           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3259         break;
3260       }
3261 
3262       if (SpecialTypes.size() != Record.size())
3263         return llvm::createStringError(std::errc::illegal_byte_sequence,
3264                                        "invalid special-types record");
3265 
3266       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3267         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3268         if (!SpecialTypes[I])
3269           SpecialTypes[I] = ID;
3270         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3271         // merge step?
3272       }
3273       break;
3274 
3275     case STATISTICS:
3276       TotalNumStatements += Record[0];
3277       TotalNumMacros += Record[1];
3278       TotalLexicalDeclContexts += Record[2];
3279       TotalVisibleDeclContexts += Record[3];
3280       break;
3281 
3282     case UNUSED_FILESCOPED_DECLS:
3283       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3284         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3285       break;
3286 
3287     case DELEGATING_CTORS:
3288       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3289         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3290       break;
3291 
3292     case WEAK_UNDECLARED_IDENTIFIERS:
3293       if (Record.size() % 4 != 0)
3294         return llvm::createStringError(std::errc::illegal_byte_sequence,
3295                                        "invalid weak identifiers record");
3296 
3297       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3298       // files. This isn't the way to do it :)
3299       WeakUndeclaredIdentifiers.clear();
3300 
3301       // Translate the weak, undeclared identifiers into global IDs.
3302       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3303         WeakUndeclaredIdentifiers.push_back(
3304           getGlobalIdentifierID(F, Record[I++]));
3305         WeakUndeclaredIdentifiers.push_back(
3306           getGlobalIdentifierID(F, Record[I++]));
3307         WeakUndeclaredIdentifiers.push_back(
3308           ReadSourceLocation(F, Record, I).getRawEncoding());
3309         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3310       }
3311       break;
3312 
3313     case SELECTOR_OFFSETS: {
3314       F.SelectorOffsets = (const uint32_t *)Blob.data();
3315       F.LocalNumSelectors = Record[0];
3316       unsigned LocalBaseSelectorID = Record[1];
3317       F.BaseSelectorID = getTotalNumSelectors();
3318 
3319       if (F.LocalNumSelectors > 0) {
3320         // Introduce the global -> local mapping for selectors within this
3321         // module.
3322         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3323 
3324         // Introduce the local -> global mapping for selectors within this
3325         // module.
3326         F.SelectorRemap.insertOrReplace(
3327           std::make_pair(LocalBaseSelectorID,
3328                          F.BaseSelectorID - LocalBaseSelectorID));
3329 
3330         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3331       }
3332       break;
3333     }
3334 
3335     case METHOD_POOL:
3336       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3337       if (Record[0])
3338         F.SelectorLookupTable
3339           = ASTSelectorLookupTable::Create(
3340                         F.SelectorLookupTableData + Record[0],
3341                         F.SelectorLookupTableData,
3342                         ASTSelectorLookupTrait(*this, F));
3343       TotalNumMethodPoolEntries += Record[1];
3344       break;
3345 
3346     case REFERENCED_SELECTOR_POOL:
3347       if (!Record.empty()) {
3348         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3349           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3350                                                                 Record[Idx++]));
3351           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3352                                               getRawEncoding());
3353         }
3354       }
3355       break;
3356 
3357     case PP_CONDITIONAL_STACK:
3358       if (!Record.empty()) {
3359         unsigned Idx = 0, End = Record.size() - 1;
3360         bool ReachedEOFWhileSkipping = Record[Idx++];
3361         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3362         if (ReachedEOFWhileSkipping) {
3363           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3364           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3365           bool FoundNonSkipPortion = Record[Idx++];
3366           bool FoundElse = Record[Idx++];
3367           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3368           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3369                            FoundElse, ElseLoc);
3370         }
3371         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3372         while (Idx < End) {
3373           auto Loc = ReadSourceLocation(F, Record, Idx);
3374           bool WasSkipping = Record[Idx++];
3375           bool FoundNonSkip = Record[Idx++];
3376           bool FoundElse = Record[Idx++];
3377           ConditionalStack.push_back(
3378               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3379         }
3380         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3381       }
3382       break;
3383 
3384     case PP_COUNTER_VALUE:
3385       if (!Record.empty() && Listener)
3386         Listener->ReadCounter(F, Record[0]);
3387       break;
3388 
3389     case FILE_SORTED_DECLS:
3390       F.FileSortedDecls = (const DeclID *)Blob.data();
3391       F.NumFileSortedDecls = Record[0];
3392       break;
3393 
3394     case SOURCE_LOCATION_OFFSETS: {
3395       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3396       F.LocalNumSLocEntries = Record[0];
3397       SourceLocation::UIntTy SLocSpaceSize = Record[1];
3398       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3399       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3400           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3401                                               SLocSpaceSize);
3402       if (!F.SLocEntryBaseID)
3403         return llvm::createStringError(std::errc::invalid_argument,
3404                                        "ran out of source locations");
3405       // Make our entry in the range map. BaseID is negative and growing, so
3406       // we invert it. Because we invert it, though, we need the other end of
3407       // the range.
3408       unsigned RangeStart =
3409           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3410       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3411       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3412 
3413       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3414       assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3415       GlobalSLocOffsetMap.insert(
3416           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3417                            - SLocSpaceSize,&F));
3418 
3419       // Initialize the remapping table.
3420       // Invalid stays invalid.
3421       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3422       // This module. Base was 2 when being compiled.
3423       F.SLocRemap.insertOrReplace(std::make_pair(
3424           2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3425 
3426       TotalNumSLocEntries += F.LocalNumSLocEntries;
3427       break;
3428     }
3429 
3430     case MODULE_OFFSET_MAP:
3431       F.ModuleOffsetMap = Blob;
3432       break;
3433 
3434     case SOURCE_MANAGER_LINE_TABLE:
3435       ParseLineTable(F, Record);
3436       break;
3437 
3438     case SOURCE_LOCATION_PRELOADS: {
3439       // Need to transform from the local view (1-based IDs) to the global view,
3440       // which is based off F.SLocEntryBaseID.
3441       if (!F.PreloadSLocEntries.empty())
3442         return llvm::createStringError(
3443             std::errc::illegal_byte_sequence,
3444             "Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3445 
3446       F.PreloadSLocEntries.swap(Record);
3447       break;
3448     }
3449 
3450     case EXT_VECTOR_DECLS:
3451       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3452         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3453       break;
3454 
3455     case VTABLE_USES:
3456       if (Record.size() % 3 != 0)
3457         return llvm::createStringError(std::errc::illegal_byte_sequence,
3458                                        "Invalid VTABLE_USES record");
3459 
3460       // Later tables overwrite earlier ones.
3461       // FIXME: Modules will have some trouble with this. This is clearly not
3462       // the right way to do this.
3463       VTableUses.clear();
3464 
3465       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3466         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3467         VTableUses.push_back(
3468           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3469         VTableUses.push_back(Record[Idx++]);
3470       }
3471       break;
3472 
3473     case PENDING_IMPLICIT_INSTANTIATIONS:
3474       if (PendingInstantiations.size() % 2 != 0)
3475         return llvm::createStringError(
3476             std::errc::illegal_byte_sequence,
3477             "Invalid existing PendingInstantiations");
3478 
3479       if (Record.size() % 2 != 0)
3480         return llvm::createStringError(
3481             std::errc::illegal_byte_sequence,
3482             "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3483 
3484       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3485         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3486         PendingInstantiations.push_back(
3487           ReadSourceLocation(F, Record, I).getRawEncoding());
3488       }
3489       break;
3490 
3491     case SEMA_DECL_REFS:
3492       if (Record.size() != 3)
3493         return llvm::createStringError(std::errc::illegal_byte_sequence,
3494                                        "Invalid SEMA_DECL_REFS block");
3495       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3496         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3497       break;
3498 
3499     case PPD_ENTITIES_OFFSETS: {
3500       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3501       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3502       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3503 
3504       unsigned LocalBasePreprocessedEntityID = Record[0];
3505 
3506       unsigned StartingID;
3507       if (!PP.getPreprocessingRecord())
3508         PP.createPreprocessingRecord();
3509       if (!PP.getPreprocessingRecord()->getExternalSource())
3510         PP.getPreprocessingRecord()->SetExternalSource(*this);
3511       StartingID
3512         = PP.getPreprocessingRecord()
3513             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3514       F.BasePreprocessedEntityID = StartingID;
3515 
3516       if (F.NumPreprocessedEntities > 0) {
3517         // Introduce the global -> local mapping for preprocessed entities in
3518         // this module.
3519         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3520 
3521         // Introduce the local -> global mapping for preprocessed entities in
3522         // this module.
3523         F.PreprocessedEntityRemap.insertOrReplace(
3524           std::make_pair(LocalBasePreprocessedEntityID,
3525             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3526       }
3527 
3528       break;
3529     }
3530 
3531     case PPD_SKIPPED_RANGES: {
3532       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3533       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3534       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3535 
3536       if (!PP.getPreprocessingRecord())
3537         PP.createPreprocessingRecord();
3538       if (!PP.getPreprocessingRecord()->getExternalSource())
3539         PP.getPreprocessingRecord()->SetExternalSource(*this);
3540       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3541           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3542 
3543       if (F.NumPreprocessedSkippedRanges > 0)
3544         GlobalSkippedRangeMap.insert(
3545             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3546       break;
3547     }
3548 
3549     case DECL_UPDATE_OFFSETS:
3550       if (Record.size() % 2 != 0)
3551         return llvm::createStringError(
3552             std::errc::illegal_byte_sequence,
3553             "invalid DECL_UPDATE_OFFSETS block in AST file");
3554       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3555         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3556         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3557 
3558         // If we've already loaded the decl, perform the updates when we finish
3559         // loading this block.
3560         if (Decl *D = GetExistingDecl(ID))
3561           PendingUpdateRecords.push_back(
3562               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3563       }
3564       break;
3565 
3566     case OBJC_CATEGORIES_MAP:
3567       if (F.LocalNumObjCCategoriesInMap != 0)
3568         return llvm::createStringError(
3569             std::errc::illegal_byte_sequence,
3570             "duplicate OBJC_CATEGORIES_MAP record in AST file");
3571 
3572       F.LocalNumObjCCategoriesInMap = Record[0];
3573       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3574       break;
3575 
3576     case OBJC_CATEGORIES:
3577       F.ObjCCategories.swap(Record);
3578       break;
3579 
3580     case CUDA_SPECIAL_DECL_REFS:
3581       // Later tables overwrite earlier ones.
3582       // FIXME: Modules will have trouble with this.
3583       CUDASpecialDeclRefs.clear();
3584       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3585         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3586       break;
3587 
3588     case HEADER_SEARCH_TABLE:
3589       F.HeaderFileInfoTableData = Blob.data();
3590       F.LocalNumHeaderFileInfos = Record[1];
3591       if (Record[0]) {
3592         F.HeaderFileInfoTable
3593           = HeaderFileInfoLookupTable::Create(
3594                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3595                    (const unsigned char *)F.HeaderFileInfoTableData,
3596                    HeaderFileInfoTrait(*this, F,
3597                                        &PP.getHeaderSearchInfo(),
3598                                        Blob.data() + Record[2]));
3599 
3600         PP.getHeaderSearchInfo().SetExternalSource(this);
3601         if (!PP.getHeaderSearchInfo().getExternalLookup())
3602           PP.getHeaderSearchInfo().SetExternalLookup(this);
3603       }
3604       break;
3605 
3606     case FP_PRAGMA_OPTIONS:
3607       // Later tables overwrite earlier ones.
3608       FPPragmaOptions.swap(Record);
3609       break;
3610 
3611     case OPENCL_EXTENSIONS:
3612       for (unsigned I = 0, E = Record.size(); I != E; ) {
3613         auto Name = ReadString(Record, I);
3614         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3615         OptInfo.Supported = Record[I++] != 0;
3616         OptInfo.Enabled = Record[I++] != 0;
3617         OptInfo.WithPragma = Record[I++] != 0;
3618         OptInfo.Avail = Record[I++];
3619         OptInfo.Core = Record[I++];
3620         OptInfo.Opt = Record[I++];
3621       }
3622       break;
3623 
3624     case TENTATIVE_DEFINITIONS:
3625       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3626         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3627       break;
3628 
3629     case KNOWN_NAMESPACES:
3630       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3631         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3632       break;
3633 
3634     case UNDEFINED_BUT_USED:
3635       if (UndefinedButUsed.size() % 2 != 0)
3636         return llvm::createStringError(std::errc::illegal_byte_sequence,
3637                                        "Invalid existing UndefinedButUsed");
3638 
3639       if (Record.size() % 2 != 0)
3640         return llvm::createStringError(std::errc::illegal_byte_sequence,
3641                                        "invalid undefined-but-used record");
3642       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3643         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3644         UndefinedButUsed.push_back(
3645             ReadSourceLocation(F, Record, I).getRawEncoding());
3646       }
3647       break;
3648 
3649     case DELETE_EXPRS_TO_ANALYZE:
3650       for (unsigned I = 0, N = Record.size(); I != N;) {
3651         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3652         const uint64_t Count = Record[I++];
3653         DelayedDeleteExprs.push_back(Count);
3654         for (uint64_t C = 0; C < Count; ++C) {
3655           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3656           bool IsArrayForm = Record[I++] == 1;
3657           DelayedDeleteExprs.push_back(IsArrayForm);
3658         }
3659       }
3660       break;
3661 
3662     case IMPORTED_MODULES:
3663       if (!F.isModule()) {
3664         // If we aren't loading a module (which has its own exports), make
3665         // all of the imported modules visible.
3666         // FIXME: Deal with macros-only imports.
3667         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3668           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3669           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3670           if (GlobalID) {
3671             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3672             if (DeserializationListener)
3673               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3674           }
3675         }
3676       }
3677       break;
3678 
3679     case MACRO_OFFSET: {
3680       if (F.LocalNumMacros != 0)
3681         return llvm::createStringError(
3682             std::errc::illegal_byte_sequence,
3683             "duplicate MACRO_OFFSET record in AST file");
3684       F.MacroOffsets = (const uint32_t *)Blob.data();
3685       F.LocalNumMacros = Record[0];
3686       unsigned LocalBaseMacroID = Record[1];
3687       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3688       F.BaseMacroID = getTotalNumMacros();
3689 
3690       if (F.LocalNumMacros > 0) {
3691         // Introduce the global -> local mapping for macros within this module.
3692         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3693 
3694         // Introduce the local -> global mapping for macros within this module.
3695         F.MacroRemap.insertOrReplace(
3696           std::make_pair(LocalBaseMacroID,
3697                          F.BaseMacroID - LocalBaseMacroID));
3698 
3699         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3700       }
3701       break;
3702     }
3703 
3704     case LATE_PARSED_TEMPLATE:
3705       LateParsedTemplates.emplace_back(
3706           std::piecewise_construct, std::forward_as_tuple(&F),
3707           std::forward_as_tuple(Record.begin(), Record.end()));
3708       break;
3709 
3710     case OPTIMIZE_PRAGMA_OPTIONS:
3711       if (Record.size() != 1)
3712         return llvm::createStringError(std::errc::illegal_byte_sequence,
3713                                        "invalid pragma optimize record");
3714       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3715       break;
3716 
3717     case MSSTRUCT_PRAGMA_OPTIONS:
3718       if (Record.size() != 1)
3719         return llvm::createStringError(std::errc::illegal_byte_sequence,
3720                                        "invalid pragma ms_struct record");
3721       PragmaMSStructState = Record[0];
3722       break;
3723 
3724     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3725       if (Record.size() != 2)
3726         return llvm::createStringError(
3727             std::errc::illegal_byte_sequence,
3728             "invalid pragma pointers to members record");
3729       PragmaMSPointersToMembersState = Record[0];
3730       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3731       break;
3732 
3733     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3734       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3735         UnusedLocalTypedefNameCandidates.push_back(
3736             getGlobalDeclID(F, Record[I]));
3737       break;
3738 
3739     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3740       if (Record.size() != 1)
3741         return llvm::createStringError(std::errc::illegal_byte_sequence,
3742                                        "invalid cuda pragma options record");
3743       ForceCUDAHostDeviceDepth = Record[0];
3744       break;
3745 
3746     case ALIGN_PACK_PRAGMA_OPTIONS: {
3747       if (Record.size() < 3)
3748         return llvm::createStringError(std::errc::illegal_byte_sequence,
3749                                        "invalid pragma pack record");
3750       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3751       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3752       unsigned NumStackEntries = Record[2];
3753       unsigned Idx = 3;
3754       // Reset the stack when importing a new module.
3755       PragmaAlignPackStack.clear();
3756       for (unsigned I = 0; I < NumStackEntries; ++I) {
3757         PragmaAlignPackStackEntry Entry;
3758         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3759         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3760         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3761         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3762         Entry.SlotLabel = PragmaAlignPackStrings.back();
3763         PragmaAlignPackStack.push_back(Entry);
3764       }
3765       break;
3766     }
3767 
3768     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3769       if (Record.size() < 3)
3770         return llvm::createStringError(std::errc::illegal_byte_sequence,
3771                                        "invalid pragma float control record");
3772       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3773       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3774       unsigned NumStackEntries = Record[2];
3775       unsigned Idx = 3;
3776       // Reset the stack when importing a new module.
3777       FpPragmaStack.clear();
3778       for (unsigned I = 0; I < NumStackEntries; ++I) {
3779         FpPragmaStackEntry Entry;
3780         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3781         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3782         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3783         FpPragmaStrings.push_back(ReadString(Record, Idx));
3784         Entry.SlotLabel = FpPragmaStrings.back();
3785         FpPragmaStack.push_back(Entry);
3786       }
3787       break;
3788     }
3789 
3790     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3791       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3792         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3793       break;
3794     }
3795   }
3796 }
3797 
3798 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3799   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3800 
3801   // Additional remapping information.
3802   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3803   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3804   F.ModuleOffsetMap = StringRef();
3805 
3806   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3807   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3808     F.SLocRemap.insert(std::make_pair(0U, 0));
3809     F.SLocRemap.insert(std::make_pair(2U, 1));
3810   }
3811 
3812   // Continuous range maps we may be updating in our module.
3813   using SLocRemapBuilder =
3814       ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
3815                          2>::Builder;
3816   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3817   SLocRemapBuilder SLocRemap(F.SLocRemap);
3818   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3819   RemapBuilder MacroRemap(F.MacroRemap);
3820   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3821   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3822   RemapBuilder SelectorRemap(F.SelectorRemap);
3823   RemapBuilder DeclRemap(F.DeclRemap);
3824   RemapBuilder TypeRemap(F.TypeRemap);
3825 
3826   while (Data < DataEnd) {
3827     // FIXME: Looking up dependency modules by filename is horrible. Let's
3828     // start fixing this with prebuilt, explicit and implicit modules and see
3829     // how it goes...
3830     using namespace llvm::support;
3831     ModuleKind Kind = static_cast<ModuleKind>(
3832       endian::readNext<uint8_t, little, unaligned>(Data));
3833     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3834     StringRef Name = StringRef((const char*)Data, Len);
3835     Data += Len;
3836     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3837                               Kind == MK_ImplicitModule
3838                           ? ModuleMgr.lookupByModuleName(Name)
3839                           : ModuleMgr.lookupByFileName(Name));
3840     if (!OM) {
3841       std::string Msg =
3842           "SourceLocation remap refers to unknown module, cannot find ";
3843       Msg.append(std::string(Name));
3844       Error(Msg);
3845       return;
3846     }
3847 
3848     SourceLocation::UIntTy SLocOffset =
3849         endian::readNext<uint32_t, little, unaligned>(Data);
3850     uint32_t IdentifierIDOffset =
3851         endian::readNext<uint32_t, little, unaligned>(Data);
3852     uint32_t MacroIDOffset =
3853         endian::readNext<uint32_t, little, unaligned>(Data);
3854     uint32_t PreprocessedEntityIDOffset =
3855         endian::readNext<uint32_t, little, unaligned>(Data);
3856     uint32_t SubmoduleIDOffset =
3857         endian::readNext<uint32_t, little, unaligned>(Data);
3858     uint32_t SelectorIDOffset =
3859         endian::readNext<uint32_t, little, unaligned>(Data);
3860     uint32_t DeclIDOffset =
3861         endian::readNext<uint32_t, little, unaligned>(Data);
3862     uint32_t TypeIndexOffset =
3863         endian::readNext<uint32_t, little, unaligned>(Data);
3864 
3865     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3866                          RemapBuilder &Remap) {
3867       constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
3868       if (Offset != None)
3869         Remap.insert(std::make_pair(Offset,
3870                                     static_cast<int>(BaseOffset - Offset)));
3871     };
3872 
3873     constexpr SourceLocation::UIntTy SLocNone =
3874         std::numeric_limits<SourceLocation::UIntTy>::max();
3875     if (SLocOffset != SLocNone)
3876       SLocRemap.insert(std::make_pair(
3877           SLocOffset, static_cast<SourceLocation::IntTy>(
3878                           OM->SLocEntryBaseOffset - SLocOffset)));
3879 
3880     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3881     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3882     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3883               PreprocessedEntityRemap);
3884     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3885     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3886     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3887     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3888 
3889     // Global -> local mappings.
3890     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3891   }
3892 }
3893 
3894 ASTReader::ASTReadResult
3895 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3896                                   const ModuleFile *ImportedBy,
3897                                   unsigned ClientLoadCapabilities) {
3898   unsigned Idx = 0;
3899   F.ModuleMapPath = ReadPath(F, Record, Idx);
3900 
3901   // Try to resolve ModuleName in the current header search context and
3902   // verify that it is found in the same module map file as we saved. If the
3903   // top-level AST file is a main file, skip this check because there is no
3904   // usable header search context.
3905   assert(!F.ModuleName.empty() &&
3906          "MODULE_NAME should come before MODULE_MAP_FILE");
3907   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3908     // An implicitly-loaded module file should have its module listed in some
3909     // module map file that we've already loaded.
3910     Module *M =
3911         PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
3912     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3913     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3914     // Don't emit module relocation error if we have -fno-validate-pch
3915     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3916               DisableValidationForModuleKind::Module) &&
3917         !ModMap) {
3918       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
3919         if (auto ASTFE = M ? M->getASTFile() : None) {
3920           // This module was defined by an imported (explicit) module.
3921           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3922                                                << ASTFE->getName();
3923         } else {
3924           // This module was built with a different module map.
3925           Diag(diag::err_imported_module_not_found)
3926               << F.ModuleName << F.FileName
3927               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3928               << !ImportedBy;
3929           // In case it was imported by a PCH, there's a chance the user is
3930           // just missing to include the search path to the directory containing
3931           // the modulemap.
3932           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3933             Diag(diag::note_imported_by_pch_module_not_found)
3934                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3935         }
3936       }
3937       return OutOfDate;
3938     }
3939 
3940     assert(M && M->Name == F.ModuleName && "found module with different name");
3941 
3942     // Check the primary module map file.
3943     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3944     if (!StoredModMap || *StoredModMap != ModMap) {
3945       assert(ModMap && "found module is missing module map file");
3946       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3947              "top-level import should be verified");
3948       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3949       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3950         Diag(diag::err_imported_module_modmap_changed)
3951             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3952             << ModMap->getName() << F.ModuleMapPath << NotImported;
3953       return OutOfDate;
3954     }
3955 
3956     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3957     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3958       // FIXME: we should use input files rather than storing names.
3959       std::string Filename = ReadPath(F, Record, Idx);
3960       auto SF = FileMgr.getFile(Filename, false, false);
3961       if (!SF) {
3962         if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3963           Error("could not find file '" + Filename +"' referenced by AST file");
3964         return OutOfDate;
3965       }
3966       AdditionalStoredMaps.insert(*SF);
3967     }
3968 
3969     // Check any additional module map files (e.g. module.private.modulemap)
3970     // that are not in the pcm.
3971     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3972       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3973         // Remove files that match
3974         // Note: SmallPtrSet::erase is really remove
3975         if (!AdditionalStoredMaps.erase(ModMap)) {
3976           if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3977             Diag(diag::err_module_different_modmap)
3978               << F.ModuleName << /*new*/0 << ModMap->getName();
3979           return OutOfDate;
3980         }
3981       }
3982     }
3983 
3984     // Check any additional module map files that are in the pcm, but not
3985     // found in header search. Cases that match are already removed.
3986     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3987       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3988         Diag(diag::err_module_different_modmap)
3989           << F.ModuleName << /*not new*/1 << ModMap->getName();
3990       return OutOfDate;
3991     }
3992   }
3993 
3994   if (Listener)
3995     Listener->ReadModuleMapFile(F.ModuleMapPath);
3996   return Success;
3997 }
3998 
3999 /// Move the given method to the back of the global list of methods.
4000 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4001   // Find the entry for this selector in the method pool.
4002   Sema::GlobalMethodPool::iterator Known
4003     = S.MethodPool.find(Method->getSelector());
4004   if (Known == S.MethodPool.end())
4005     return;
4006 
4007   // Retrieve the appropriate method list.
4008   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4009                                                     : Known->second.second;
4010   bool Found = false;
4011   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4012     if (!Found) {
4013       if (List->getMethod() == Method) {
4014         Found = true;
4015       } else {
4016         // Keep searching.
4017         continue;
4018       }
4019     }
4020 
4021     if (List->getNext())
4022       List->setMethod(List->getNext()->getMethod());
4023     else
4024       List->setMethod(Method);
4025   }
4026 }
4027 
4028 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4029   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4030   for (Decl *D : Names) {
4031     bool wasHidden = !D->isUnconditionallyVisible();
4032     D->setVisibleDespiteOwningModule();
4033 
4034     if (wasHidden && SemaObj) {
4035       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4036         moveMethodToBackOfGlobalList(*SemaObj, Method);
4037       }
4038     }
4039   }
4040 }
4041 
4042 void ASTReader::makeModuleVisible(Module *Mod,
4043                                   Module::NameVisibilityKind NameVisibility,
4044                                   SourceLocation ImportLoc) {
4045   llvm::SmallPtrSet<Module *, 4> Visited;
4046   SmallVector<Module *, 4> Stack;
4047   Stack.push_back(Mod);
4048   while (!Stack.empty()) {
4049     Mod = Stack.pop_back_val();
4050 
4051     if (NameVisibility <= Mod->NameVisibility) {
4052       // This module already has this level of visibility (or greater), so
4053       // there is nothing more to do.
4054       continue;
4055     }
4056 
4057     if (Mod->isUnimportable()) {
4058       // Modules that aren't importable cannot be made visible.
4059       continue;
4060     }
4061 
4062     // Update the module's name visibility.
4063     Mod->NameVisibility = NameVisibility;
4064 
4065     // If we've already deserialized any names from this module,
4066     // mark them as visible.
4067     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4068     if (Hidden != HiddenNamesMap.end()) {
4069       auto HiddenNames = std::move(*Hidden);
4070       HiddenNamesMap.erase(Hidden);
4071       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4072       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4073              "making names visible added hidden names");
4074     }
4075 
4076     // Push any exported modules onto the stack to be marked as visible.
4077     SmallVector<Module *, 16> Exports;
4078     Mod->getExportedModules(Exports);
4079     for (SmallVectorImpl<Module *>::iterator
4080            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4081       Module *Exported = *I;
4082       if (Visited.insert(Exported).second)
4083         Stack.push_back(Exported);
4084     }
4085   }
4086 }
4087 
4088 /// We've merged the definition \p MergedDef into the existing definition
4089 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4090 /// visible.
4091 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4092                                           NamedDecl *MergedDef) {
4093   if (!Def->isUnconditionallyVisible()) {
4094     // If MergedDef is visible or becomes visible, make the definition visible.
4095     if (MergedDef->isUnconditionallyVisible())
4096       Def->setVisibleDespiteOwningModule();
4097     else {
4098       getContext().mergeDefinitionIntoModule(
4099           Def, MergedDef->getImportedOwningModule(),
4100           /*NotifyListeners*/ false);
4101       PendingMergedDefinitionsToDeduplicate.insert(Def);
4102     }
4103   }
4104 }
4105 
4106 bool ASTReader::loadGlobalIndex() {
4107   if (GlobalIndex)
4108     return false;
4109 
4110   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4111       !PP.getLangOpts().Modules)
4112     return true;
4113 
4114   // Try to load the global index.
4115   TriedLoadingGlobalIndex = true;
4116   StringRef ModuleCachePath
4117     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4118   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4119       GlobalModuleIndex::readIndex(ModuleCachePath);
4120   if (llvm::Error Err = std::move(Result.second)) {
4121     assert(!Result.first);
4122     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4123     return true;
4124   }
4125 
4126   GlobalIndex.reset(Result.first);
4127   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4128   return false;
4129 }
4130 
4131 bool ASTReader::isGlobalIndexUnavailable() const {
4132   return PP.getLangOpts().Modules && UseGlobalIndex &&
4133          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4134 }
4135 
4136 static void updateModuleTimestamp(ModuleFile &MF) {
4137   // Overwrite the timestamp file contents so that file's mtime changes.
4138   std::string TimestampFilename = MF.getTimestampFilename();
4139   std::error_code EC;
4140   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4141                           llvm::sys::fs::OF_TextWithCRLF);
4142   if (EC)
4143     return;
4144   OS << "Timestamp file\n";
4145   OS.close();
4146   OS.clear_error(); // Avoid triggering a fatal error.
4147 }
4148 
4149 /// Given a cursor at the start of an AST file, scan ahead and drop the
4150 /// cursor into the start of the given block ID, returning false on success and
4151 /// true on failure.
4152 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4153   while (true) {
4154     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4155     if (!MaybeEntry) {
4156       // FIXME this drops errors on the floor.
4157       consumeError(MaybeEntry.takeError());
4158       return true;
4159     }
4160     llvm::BitstreamEntry Entry = MaybeEntry.get();
4161 
4162     switch (Entry.Kind) {
4163     case llvm::BitstreamEntry::Error:
4164     case llvm::BitstreamEntry::EndBlock:
4165       return true;
4166 
4167     case llvm::BitstreamEntry::Record:
4168       // Ignore top-level records.
4169       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4170         break;
4171       else {
4172         // FIXME this drops errors on the floor.
4173         consumeError(Skipped.takeError());
4174         return true;
4175       }
4176 
4177     case llvm::BitstreamEntry::SubBlock:
4178       if (Entry.ID == BlockID) {
4179         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4180           // FIXME this drops the error on the floor.
4181           consumeError(std::move(Err));
4182           return true;
4183         }
4184         // Found it!
4185         return false;
4186       }
4187 
4188       if (llvm::Error Err = Cursor.SkipBlock()) {
4189         // FIXME this drops the error on the floor.
4190         consumeError(std::move(Err));
4191         return true;
4192       }
4193     }
4194   }
4195 }
4196 
4197 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4198                                             ModuleKind Type,
4199                                             SourceLocation ImportLoc,
4200                                             unsigned ClientLoadCapabilities,
4201                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4202   llvm::SaveAndRestore<SourceLocation>
4203     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4204   llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4205       CurrentDeserializingModuleKind, Type);
4206 
4207   // Defer any pending actions until we get to the end of reading the AST file.
4208   Deserializing AnASTFile(this);
4209 
4210   // Bump the generation number.
4211   unsigned PreviousGeneration = 0;
4212   if (ContextObj)
4213     PreviousGeneration = incrementGeneration(*ContextObj);
4214 
4215   unsigned NumModules = ModuleMgr.size();
4216   SmallVector<ImportedModule, 4> Loaded;
4217   if (ASTReadResult ReadResult =
4218           ReadASTCore(FileName, Type, ImportLoc,
4219                       /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4220                       ClientLoadCapabilities)) {
4221     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4222                             PP.getLangOpts().Modules
4223                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4224                                 : nullptr);
4225 
4226     // If we find that any modules are unusable, the global index is going
4227     // to be out-of-date. Just remove it.
4228     GlobalIndex.reset();
4229     ModuleMgr.setGlobalIndex(nullptr);
4230     return ReadResult;
4231   }
4232 
4233   // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4234   // remove modules from this point. Various fields are updated during reading
4235   // the AST block and removing the modules would result in dangling pointers.
4236   // They are generally only incidentally dereferenced, ie. a binary search
4237   // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4238   // be dereferenced but it wouldn't actually be used.
4239 
4240   // Load the AST blocks of all of the modules that we loaded. We can still
4241   // hit errors parsing the ASTs at this point.
4242   for (ImportedModule &M : Loaded) {
4243     ModuleFile &F = *M.Mod;
4244 
4245     // Read the AST block.
4246     if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4247       Error(std::move(Err));
4248       return Failure;
4249     }
4250 
4251     // The AST block should always have a definition for the main module.
4252     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4253       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4254       return Failure;
4255     }
4256 
4257     // Read the extension blocks.
4258     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4259       if (llvm::Error Err = ReadExtensionBlock(F)) {
4260         Error(std::move(Err));
4261         return Failure;
4262       }
4263     }
4264 
4265     // Once read, set the ModuleFile bit base offset and update the size in
4266     // bits of all files we've seen.
4267     F.GlobalBitOffset = TotalModulesSizeInBits;
4268     TotalModulesSizeInBits += F.SizeInBits;
4269     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4270   }
4271 
4272   // Preload source locations and interesting indentifiers.
4273   for (ImportedModule &M : Loaded) {
4274     ModuleFile &F = *M.Mod;
4275 
4276     // Preload SLocEntries.
4277     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4278       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4279       // Load it through the SourceManager and don't call ReadSLocEntry()
4280       // directly because the entry may have already been loaded in which case
4281       // calling ReadSLocEntry() directly would trigger an assertion in
4282       // SourceManager.
4283       SourceMgr.getLoadedSLocEntryByID(Index);
4284     }
4285 
4286     // Map the original source file ID into the ID space of the current
4287     // compilation.
4288     if (F.OriginalSourceFileID.isValid()) {
4289       F.OriginalSourceFileID = FileID::get(
4290           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4291     }
4292 
4293     // Preload all the pending interesting identifiers by marking them out of
4294     // date.
4295     for (auto Offset : F.PreloadIdentifierOffsets) {
4296       const unsigned char *Data = F.IdentifierTableData + Offset;
4297 
4298       ASTIdentifierLookupTrait Trait(*this, F);
4299       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4300       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4301       auto &II = PP.getIdentifierTable().getOwn(Key);
4302       II.setOutOfDate(true);
4303 
4304       // Mark this identifier as being from an AST file so that we can track
4305       // whether we need to serialize it.
4306       markIdentifierFromAST(*this, II);
4307 
4308       // Associate the ID with the identifier so that the writer can reuse it.
4309       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4310       SetIdentifierInfo(ID, &II);
4311     }
4312   }
4313 
4314   // Setup the import locations and notify the module manager that we've
4315   // committed to these module files.
4316   for (ImportedModule &M : Loaded) {
4317     ModuleFile &F = *M.Mod;
4318 
4319     ModuleMgr.moduleFileAccepted(&F);
4320 
4321     // Set the import location.
4322     F.DirectImportLoc = ImportLoc;
4323     // FIXME: We assume that locations from PCH / preamble do not need
4324     // any translation.
4325     if (!M.ImportedBy)
4326       F.ImportLoc = M.ImportLoc;
4327     else
4328       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4329   }
4330 
4331   if (!PP.getLangOpts().CPlusPlus ||
4332       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4333        Type != MK_PrebuiltModule)) {
4334     // Mark all of the identifiers in the identifier table as being out of date,
4335     // so that various accessors know to check the loaded modules when the
4336     // identifier is used.
4337     //
4338     // For C++ modules, we don't need information on many identifiers (just
4339     // those that provide macros or are poisoned), so we mark all of
4340     // the interesting ones via PreloadIdentifierOffsets.
4341     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4342                                 IdEnd = PP.getIdentifierTable().end();
4343          Id != IdEnd; ++Id)
4344       Id->second->setOutOfDate(true);
4345   }
4346   // Mark selectors as out of date.
4347   for (auto Sel : SelectorGeneration)
4348     SelectorOutOfDate[Sel.first] = true;
4349 
4350   // Resolve any unresolved module exports.
4351   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4352     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4353     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4354     Module *ResolvedMod = getSubmodule(GlobalID);
4355 
4356     switch (Unresolved.Kind) {
4357     case UnresolvedModuleRef::Conflict:
4358       if (ResolvedMod) {
4359         Module::Conflict Conflict;
4360         Conflict.Other = ResolvedMod;
4361         Conflict.Message = Unresolved.String.str();
4362         Unresolved.Mod->Conflicts.push_back(Conflict);
4363       }
4364       continue;
4365 
4366     case UnresolvedModuleRef::Import:
4367       if (ResolvedMod)
4368         Unresolved.Mod->Imports.insert(ResolvedMod);
4369       continue;
4370 
4371     case UnresolvedModuleRef::Export:
4372       if (ResolvedMod || Unresolved.IsWildcard)
4373         Unresolved.Mod->Exports.push_back(
4374           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4375       continue;
4376     }
4377   }
4378   UnresolvedModuleRefs.clear();
4379 
4380   if (Imported)
4381     Imported->append(ImportedModules.begin(),
4382                      ImportedModules.end());
4383 
4384   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4385   // Might be unnecessary as use declarations are only used to build the
4386   // module itself.
4387 
4388   if (ContextObj)
4389     InitializeContext();
4390 
4391   if (SemaObj)
4392     UpdateSema();
4393 
4394   if (DeserializationListener)
4395     DeserializationListener->ReaderInitialized(this);
4396 
4397   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4398   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4399     // If this AST file is a precompiled preamble, then set the
4400     // preamble file ID of the source manager to the file source file
4401     // from which the preamble was built.
4402     if (Type == MK_Preamble) {
4403       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4404     } else if (Type == MK_MainFile) {
4405       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4406     }
4407   }
4408 
4409   // For any Objective-C class definitions we have already loaded, make sure
4410   // that we load any additional categories.
4411   if (ContextObj) {
4412     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4413       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4414                          ObjCClassesLoaded[I],
4415                          PreviousGeneration);
4416     }
4417   }
4418 
4419   if (PP.getHeaderSearchInfo()
4420           .getHeaderSearchOpts()
4421           .ModulesValidateOncePerBuildSession) {
4422     // Now we are certain that the module and all modules it depends on are
4423     // up to date.  Create or update timestamp files for modules that are
4424     // located in the module cache (not for PCH files that could be anywhere
4425     // in the filesystem).
4426     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4427       ImportedModule &M = Loaded[I];
4428       if (M.Mod->Kind == MK_ImplicitModule) {
4429         updateModuleTimestamp(*M.Mod);
4430       }
4431     }
4432   }
4433 
4434   return Success;
4435 }
4436 
4437 static ASTFileSignature readASTFileSignature(StringRef PCH);
4438 
4439 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4440 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4441   // FIXME checking magic headers is done in other places such as
4442   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4443   // always done the same. Unify it all with a helper.
4444   if (!Stream.canSkipToPos(4))
4445     return llvm::createStringError(std::errc::illegal_byte_sequence,
4446                                    "file too small to contain AST file magic");
4447   for (unsigned C : {'C', 'P', 'C', 'H'})
4448     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4449       if (Res.get() != C)
4450         return llvm::createStringError(
4451             std::errc::illegal_byte_sequence,
4452             "file doesn't start with AST file magic");
4453     } else
4454       return Res.takeError();
4455   return llvm::Error::success();
4456 }
4457 
4458 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4459   switch (Kind) {
4460   case MK_PCH:
4461     return 0; // PCH
4462   case MK_ImplicitModule:
4463   case MK_ExplicitModule:
4464   case MK_PrebuiltModule:
4465     return 1; // module
4466   case MK_MainFile:
4467   case MK_Preamble:
4468     return 2; // main source file
4469   }
4470   llvm_unreachable("unknown module kind");
4471 }
4472 
4473 ASTReader::ASTReadResult
4474 ASTReader::ReadASTCore(StringRef FileName,
4475                        ModuleKind Type,
4476                        SourceLocation ImportLoc,
4477                        ModuleFile *ImportedBy,
4478                        SmallVectorImpl<ImportedModule> &Loaded,
4479                        off_t ExpectedSize, time_t ExpectedModTime,
4480                        ASTFileSignature ExpectedSignature,
4481                        unsigned ClientLoadCapabilities) {
4482   ModuleFile *M;
4483   std::string ErrorStr;
4484   ModuleManager::AddModuleResult AddResult
4485     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4486                           getGeneration(), ExpectedSize, ExpectedModTime,
4487                           ExpectedSignature, readASTFileSignature,
4488                           M, ErrorStr);
4489 
4490   switch (AddResult) {
4491   case ModuleManager::AlreadyLoaded:
4492     Diag(diag::remark_module_import)
4493         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4494         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4495     return Success;
4496 
4497   case ModuleManager::NewlyLoaded:
4498     // Load module file below.
4499     break;
4500 
4501   case ModuleManager::Missing:
4502     // The module file was missing; if the client can handle that, return
4503     // it.
4504     if (ClientLoadCapabilities & ARR_Missing)
4505       return Missing;
4506 
4507     // Otherwise, return an error.
4508     Diag(diag::err_ast_file_not_found)
4509         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4510         << ErrorStr;
4511     return Failure;
4512 
4513   case ModuleManager::OutOfDate:
4514     // We couldn't load the module file because it is out-of-date. If the
4515     // client can handle out-of-date, return it.
4516     if (ClientLoadCapabilities & ARR_OutOfDate)
4517       return OutOfDate;
4518 
4519     // Otherwise, return an error.
4520     Diag(diag::err_ast_file_out_of_date)
4521         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4522         << ErrorStr;
4523     return Failure;
4524   }
4525 
4526   assert(M && "Missing module file");
4527 
4528   bool ShouldFinalizePCM = false;
4529   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4530     auto &MC = getModuleManager().getModuleCache();
4531     if (ShouldFinalizePCM)
4532       MC.finalizePCM(FileName);
4533     else
4534       MC.tryToDropPCM(FileName);
4535   });
4536   ModuleFile &F = *M;
4537   BitstreamCursor &Stream = F.Stream;
4538   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4539   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4540 
4541   // Sniff for the signature.
4542   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4543     Diag(diag::err_ast_file_invalid)
4544         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4545     return Failure;
4546   }
4547 
4548   // This is used for compatibility with older PCH formats.
4549   bool HaveReadControlBlock = false;
4550   while (true) {
4551     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4552     if (!MaybeEntry) {
4553       Error(MaybeEntry.takeError());
4554       return Failure;
4555     }
4556     llvm::BitstreamEntry Entry = MaybeEntry.get();
4557 
4558     switch (Entry.Kind) {
4559     case llvm::BitstreamEntry::Error:
4560     case llvm::BitstreamEntry::Record:
4561     case llvm::BitstreamEntry::EndBlock:
4562       Error("invalid record at top-level of AST file");
4563       return Failure;
4564 
4565     case llvm::BitstreamEntry::SubBlock:
4566       break;
4567     }
4568 
4569     switch (Entry.ID) {
4570     case CONTROL_BLOCK_ID:
4571       HaveReadControlBlock = true;
4572       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4573       case Success:
4574         // Check that we didn't try to load a non-module AST file as a module.
4575         //
4576         // FIXME: Should we also perform the converse check? Loading a module as
4577         // a PCH file sort of works, but it's a bit wonky.
4578         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4579              Type == MK_PrebuiltModule) &&
4580             F.ModuleName.empty()) {
4581           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4582           if (Result != OutOfDate ||
4583               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4584             Diag(diag::err_module_file_not_module) << FileName;
4585           return Result;
4586         }
4587         break;
4588 
4589       case Failure: return Failure;
4590       case Missing: return Missing;
4591       case OutOfDate: return OutOfDate;
4592       case VersionMismatch: return VersionMismatch;
4593       case ConfigurationMismatch: return ConfigurationMismatch;
4594       case HadErrors: return HadErrors;
4595       }
4596       break;
4597 
4598     case AST_BLOCK_ID:
4599       if (!HaveReadControlBlock) {
4600         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4601           Diag(diag::err_pch_version_too_old);
4602         return VersionMismatch;
4603       }
4604 
4605       // Record that we've loaded this module.
4606       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4607       ShouldFinalizePCM = true;
4608       return Success;
4609 
4610     case UNHASHED_CONTROL_BLOCK_ID:
4611       // This block is handled using look-ahead during ReadControlBlock.  We
4612       // shouldn't get here!
4613       Error("malformed block record in AST file");
4614       return Failure;
4615 
4616     default:
4617       if (llvm::Error Err = Stream.SkipBlock()) {
4618         Error(std::move(Err));
4619         return Failure;
4620       }
4621       break;
4622     }
4623   }
4624 
4625   llvm_unreachable("unexpected break; expected return");
4626 }
4627 
4628 ASTReader::ASTReadResult
4629 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4630                                     unsigned ClientLoadCapabilities) {
4631   const HeaderSearchOptions &HSOpts =
4632       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4633   bool AllowCompatibleConfigurationMismatch =
4634       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4635   bool DisableValidation = shouldDisableValidationForFile(F);
4636 
4637   ASTReadResult Result = readUnhashedControlBlockImpl(
4638       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4639       Listener.get(),
4640       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4641 
4642   // If F was directly imported by another module, it's implicitly validated by
4643   // the importing module.
4644   if (DisableValidation || WasImportedBy ||
4645       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4646     return Success;
4647 
4648   if (Result == Failure) {
4649     Error("malformed block record in AST file");
4650     return Failure;
4651   }
4652 
4653   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4654     // If this module has already been finalized in the ModuleCache, we're stuck
4655     // with it; we can only load a single version of each module.
4656     //
4657     // This can happen when a module is imported in two contexts: in one, as a
4658     // user module; in another, as a system module (due to an import from
4659     // another module marked with the [system] flag).  It usually indicates a
4660     // bug in the module map: this module should also be marked with [system].
4661     //
4662     // If -Wno-system-headers (the default), and the first import is as a
4663     // system module, then validation will fail during the as-user import,
4664     // since -Werror flags won't have been validated.  However, it's reasonable
4665     // to treat this consistently as a system module.
4666     //
4667     // If -Wsystem-headers, the PCM on disk was built with
4668     // -Wno-system-headers, and the first import is as a user module, then
4669     // validation will fail during the as-system import since the PCM on disk
4670     // doesn't guarantee that -Werror was respected.  However, the -Werror
4671     // flags were checked during the initial as-user import.
4672     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4673       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4674       return Success;
4675     }
4676   }
4677 
4678   return Result;
4679 }
4680 
4681 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4682     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4683     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4684     bool ValidateDiagnosticOptions) {
4685   // Initialize a stream.
4686   BitstreamCursor Stream(StreamData);
4687 
4688   // Sniff for the signature.
4689   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4690     // FIXME this drops the error on the floor.
4691     consumeError(std::move(Err));
4692     return Failure;
4693   }
4694 
4695   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4696   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4697     return Failure;
4698 
4699   // Read all of the records in the options block.
4700   RecordData Record;
4701   ASTReadResult Result = Success;
4702   while (true) {
4703     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4704     if (!MaybeEntry) {
4705       // FIXME this drops the error on the floor.
4706       consumeError(MaybeEntry.takeError());
4707       return Failure;
4708     }
4709     llvm::BitstreamEntry Entry = MaybeEntry.get();
4710 
4711     switch (Entry.Kind) {
4712     case llvm::BitstreamEntry::Error:
4713     case llvm::BitstreamEntry::SubBlock:
4714       return Failure;
4715 
4716     case llvm::BitstreamEntry::EndBlock:
4717       return Result;
4718 
4719     case llvm::BitstreamEntry::Record:
4720       // The interesting case.
4721       break;
4722     }
4723 
4724     // Read and process a record.
4725     Record.clear();
4726     StringRef Blob;
4727     Expected<unsigned> MaybeRecordType =
4728         Stream.readRecord(Entry.ID, Record, &Blob);
4729     if (!MaybeRecordType) {
4730       // FIXME this drops the error.
4731       return Failure;
4732     }
4733     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4734     case SIGNATURE:
4735       if (F)
4736         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4737       break;
4738     case AST_BLOCK_HASH:
4739       if (F)
4740         F->ASTBlockHash =
4741             ASTFileSignature::create(Record.begin(), Record.end());
4742       break;
4743     case DIAGNOSTIC_OPTIONS: {
4744       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4745       if (Listener && ValidateDiagnosticOptions &&
4746           !AllowCompatibleConfigurationMismatch &&
4747           ParseDiagnosticOptions(Record, Complain, *Listener))
4748         Result = OutOfDate; // Don't return early.  Read the signature.
4749       break;
4750     }
4751     case DIAG_PRAGMA_MAPPINGS:
4752       if (!F)
4753         break;
4754       if (F->PragmaDiagMappings.empty())
4755         F->PragmaDiagMappings.swap(Record);
4756       else
4757         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4758                                      Record.begin(), Record.end());
4759       break;
4760     case HEADER_SEARCH_ENTRY_USAGE:
4761       if (!F)
4762         break;
4763       unsigned Count = Record[0];
4764       const char *Byte = Blob.data();
4765       F->SearchPathUsage = llvm::BitVector(Count, 0);
4766       for (unsigned I = 0; I < Count; ++Byte)
4767         for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
4768           if (*Byte & (1 << Bit))
4769             F->SearchPathUsage[I] = 1;
4770       break;
4771     }
4772   }
4773 }
4774 
4775 /// Parse a record and blob containing module file extension metadata.
4776 static bool parseModuleFileExtensionMetadata(
4777               const SmallVectorImpl<uint64_t> &Record,
4778               StringRef Blob,
4779               ModuleFileExtensionMetadata &Metadata) {
4780   if (Record.size() < 4) return true;
4781 
4782   Metadata.MajorVersion = Record[0];
4783   Metadata.MinorVersion = Record[1];
4784 
4785   unsigned BlockNameLen = Record[2];
4786   unsigned UserInfoLen = Record[3];
4787 
4788   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4789 
4790   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4791   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4792                                   Blob.data() + BlockNameLen + UserInfoLen);
4793   return false;
4794 }
4795 
4796 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
4797   BitstreamCursor &Stream = F.Stream;
4798 
4799   RecordData Record;
4800   while (true) {
4801     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4802     if (!MaybeEntry)
4803       return MaybeEntry.takeError();
4804     llvm::BitstreamEntry Entry = MaybeEntry.get();
4805 
4806     switch (Entry.Kind) {
4807     case llvm::BitstreamEntry::SubBlock:
4808       if (llvm::Error Err = Stream.SkipBlock())
4809         return Err;
4810       continue;
4811     case llvm::BitstreamEntry::EndBlock:
4812       return llvm::Error::success();
4813     case llvm::BitstreamEntry::Error:
4814       return llvm::createStringError(std::errc::illegal_byte_sequence,
4815                                      "malformed block record in AST file");
4816     case llvm::BitstreamEntry::Record:
4817       break;
4818     }
4819 
4820     Record.clear();
4821     StringRef Blob;
4822     Expected<unsigned> MaybeRecCode =
4823         Stream.readRecord(Entry.ID, Record, &Blob);
4824     if (!MaybeRecCode)
4825       return MaybeRecCode.takeError();
4826     switch (MaybeRecCode.get()) {
4827     case EXTENSION_METADATA: {
4828       ModuleFileExtensionMetadata Metadata;
4829       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4830         return llvm::createStringError(
4831             std::errc::illegal_byte_sequence,
4832             "malformed EXTENSION_METADATA in AST file");
4833 
4834       // Find a module file extension with this block name.
4835       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4836       if (Known == ModuleFileExtensions.end()) break;
4837 
4838       // Form a reader.
4839       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4840                                                              F, Stream)) {
4841         F.ExtensionReaders.push_back(std::move(Reader));
4842       }
4843 
4844       break;
4845     }
4846     }
4847   }
4848 
4849   return llvm::Error::success();
4850 }
4851 
4852 void ASTReader::InitializeContext() {
4853   assert(ContextObj && "no context to initialize");
4854   ASTContext &Context = *ContextObj;
4855 
4856   // If there's a listener, notify them that we "read" the translation unit.
4857   if (DeserializationListener)
4858     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4859                                       Context.getTranslationUnitDecl());
4860 
4861   // FIXME: Find a better way to deal with collisions between these
4862   // built-in types. Right now, we just ignore the problem.
4863 
4864   // Load the special types.
4865   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4866     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4867       if (!Context.CFConstantStringTypeDecl)
4868         Context.setCFConstantStringType(GetType(String));
4869     }
4870 
4871     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4872       QualType FileType = GetType(File);
4873       if (FileType.isNull()) {
4874         Error("FILE type is NULL");
4875         return;
4876       }
4877 
4878       if (!Context.FILEDecl) {
4879         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4880           Context.setFILEDecl(Typedef->getDecl());
4881         else {
4882           const TagType *Tag = FileType->getAs<TagType>();
4883           if (!Tag) {
4884             Error("Invalid FILE type in AST file");
4885             return;
4886           }
4887           Context.setFILEDecl(Tag->getDecl());
4888         }
4889       }
4890     }
4891 
4892     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4893       QualType Jmp_bufType = GetType(Jmp_buf);
4894       if (Jmp_bufType.isNull()) {
4895         Error("jmp_buf type is NULL");
4896         return;
4897       }
4898 
4899       if (!Context.jmp_bufDecl) {
4900         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4901           Context.setjmp_bufDecl(Typedef->getDecl());
4902         else {
4903           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4904           if (!Tag) {
4905             Error("Invalid jmp_buf type in AST file");
4906             return;
4907           }
4908           Context.setjmp_bufDecl(Tag->getDecl());
4909         }
4910       }
4911     }
4912 
4913     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4914       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4915       if (Sigjmp_bufType.isNull()) {
4916         Error("sigjmp_buf type is NULL");
4917         return;
4918       }
4919 
4920       if (!Context.sigjmp_bufDecl) {
4921         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4922           Context.setsigjmp_bufDecl(Typedef->getDecl());
4923         else {
4924           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4925           assert(Tag && "Invalid sigjmp_buf type in AST file");
4926           Context.setsigjmp_bufDecl(Tag->getDecl());
4927         }
4928       }
4929     }
4930 
4931     if (unsigned ObjCIdRedef
4932           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4933       if (Context.ObjCIdRedefinitionType.isNull())
4934         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4935     }
4936 
4937     if (unsigned ObjCClassRedef
4938           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4939       if (Context.ObjCClassRedefinitionType.isNull())
4940         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4941     }
4942 
4943     if (unsigned ObjCSelRedef
4944           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4945       if (Context.ObjCSelRedefinitionType.isNull())
4946         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4947     }
4948 
4949     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4950       QualType Ucontext_tType = GetType(Ucontext_t);
4951       if (Ucontext_tType.isNull()) {
4952         Error("ucontext_t type is NULL");
4953         return;
4954       }
4955 
4956       if (!Context.ucontext_tDecl) {
4957         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4958           Context.setucontext_tDecl(Typedef->getDecl());
4959         else {
4960           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4961           assert(Tag && "Invalid ucontext_t type in AST file");
4962           Context.setucontext_tDecl(Tag->getDecl());
4963         }
4964       }
4965     }
4966   }
4967 
4968   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4969 
4970   // If there were any CUDA special declarations, deserialize them.
4971   if (!CUDASpecialDeclRefs.empty()) {
4972     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4973     Context.setcudaConfigureCallDecl(
4974                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4975   }
4976 
4977   // Re-export any modules that were imported by a non-module AST file.
4978   // FIXME: This does not make macro-only imports visible again.
4979   for (auto &Import : ImportedModules) {
4980     if (Module *Imported = getSubmodule(Import.ID)) {
4981       makeModuleVisible(Imported, Module::AllVisible,
4982                         /*ImportLoc=*/Import.ImportLoc);
4983       if (Import.ImportLoc.isValid())
4984         PP.makeModuleVisible(Imported, Import.ImportLoc);
4985       // This updates visibility for Preprocessor only. For Sema, which can be
4986       // nullptr here, we do the same later, in UpdateSema().
4987     }
4988   }
4989 }
4990 
4991 void ASTReader::finalizeForWriting() {
4992   // Nothing to do for now.
4993 }
4994 
4995 /// Reads and return the signature record from \p PCH's control block, or
4996 /// else returns 0.
4997 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4998   BitstreamCursor Stream(PCH);
4999   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5000     // FIXME this drops the error on the floor.
5001     consumeError(std::move(Err));
5002     return ASTFileSignature();
5003   }
5004 
5005   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5006   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5007     return ASTFileSignature();
5008 
5009   // Scan for SIGNATURE inside the diagnostic options block.
5010   ASTReader::RecordData Record;
5011   while (true) {
5012     Expected<llvm::BitstreamEntry> MaybeEntry =
5013         Stream.advanceSkippingSubblocks();
5014     if (!MaybeEntry) {
5015       // FIXME this drops the error on the floor.
5016       consumeError(MaybeEntry.takeError());
5017       return ASTFileSignature();
5018     }
5019     llvm::BitstreamEntry Entry = MaybeEntry.get();
5020 
5021     if (Entry.Kind != llvm::BitstreamEntry::Record)
5022       return ASTFileSignature();
5023 
5024     Record.clear();
5025     StringRef Blob;
5026     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5027     if (!MaybeRecord) {
5028       // FIXME this drops the error on the floor.
5029       consumeError(MaybeRecord.takeError());
5030       return ASTFileSignature();
5031     }
5032     if (SIGNATURE == MaybeRecord.get())
5033       return ASTFileSignature::create(Record.begin(),
5034                                       Record.begin() + ASTFileSignature::size);
5035   }
5036 }
5037 
5038 /// Retrieve the name of the original source file name
5039 /// directly from the AST file, without actually loading the AST
5040 /// file.
5041 std::string ASTReader::getOriginalSourceFile(
5042     const std::string &ASTFileName, FileManager &FileMgr,
5043     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5044   // Open the AST file.
5045   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5046   if (!Buffer) {
5047     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5048         << ASTFileName << Buffer.getError().message();
5049     return std::string();
5050   }
5051 
5052   // Initialize the stream
5053   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5054 
5055   // Sniff for the signature.
5056   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5057     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5058     return std::string();
5059   }
5060 
5061   // Scan for the CONTROL_BLOCK_ID block.
5062   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5063     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5064     return std::string();
5065   }
5066 
5067   // Scan for ORIGINAL_FILE inside the control block.
5068   RecordData Record;
5069   while (true) {
5070     Expected<llvm::BitstreamEntry> MaybeEntry =
5071         Stream.advanceSkippingSubblocks();
5072     if (!MaybeEntry) {
5073       // FIXME this drops errors on the floor.
5074       consumeError(MaybeEntry.takeError());
5075       return std::string();
5076     }
5077     llvm::BitstreamEntry Entry = MaybeEntry.get();
5078 
5079     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5080       return std::string();
5081 
5082     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5083       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5084       return std::string();
5085     }
5086 
5087     Record.clear();
5088     StringRef Blob;
5089     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5090     if (!MaybeRecord) {
5091       // FIXME this drops the errors on the floor.
5092       consumeError(MaybeRecord.takeError());
5093       return std::string();
5094     }
5095     if (ORIGINAL_FILE == MaybeRecord.get())
5096       return Blob.str();
5097   }
5098 }
5099 
5100 namespace {
5101 
5102   class SimplePCHValidator : public ASTReaderListener {
5103     const LangOptions &ExistingLangOpts;
5104     const TargetOptions &ExistingTargetOpts;
5105     const PreprocessorOptions &ExistingPPOpts;
5106     std::string ExistingModuleCachePath;
5107     FileManager &FileMgr;
5108 
5109   public:
5110     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5111                        const TargetOptions &ExistingTargetOpts,
5112                        const PreprocessorOptions &ExistingPPOpts,
5113                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5114         : ExistingLangOpts(ExistingLangOpts),
5115           ExistingTargetOpts(ExistingTargetOpts),
5116           ExistingPPOpts(ExistingPPOpts),
5117           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5118 
5119     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5120                              bool AllowCompatibleDifferences) override {
5121       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5122                                   AllowCompatibleDifferences);
5123     }
5124 
5125     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5126                            bool AllowCompatibleDifferences) override {
5127       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5128                                 AllowCompatibleDifferences);
5129     }
5130 
5131     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5132                                  StringRef SpecificModuleCachePath,
5133                                  bool Complain) override {
5134       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5135                                       ExistingModuleCachePath, nullptr,
5136                                       ExistingLangOpts, ExistingPPOpts);
5137     }
5138 
5139     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5140                                  bool Complain,
5141                                  std::string &SuggestedPredefines) override {
5142       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5143                                       SuggestedPredefines, ExistingLangOpts);
5144     }
5145   };
5146 
5147 } // namespace
5148 
5149 bool ASTReader::readASTFileControlBlock(
5150     StringRef Filename, FileManager &FileMgr,
5151     const PCHContainerReader &PCHContainerRdr,
5152     bool FindModuleFileExtensions,
5153     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5154   // Open the AST file.
5155   // FIXME: This allows use of the VFS; we do not allow use of the
5156   // VFS when actually loading a module.
5157   auto Buffer = FileMgr.getBufferForFile(Filename);
5158   if (!Buffer) {
5159     return true;
5160   }
5161 
5162   // Initialize the stream
5163   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5164   BitstreamCursor Stream(Bytes);
5165 
5166   // Sniff for the signature.
5167   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5168     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5169     return true;
5170   }
5171 
5172   // Scan for the CONTROL_BLOCK_ID block.
5173   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5174     return true;
5175 
5176   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5177   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5178   bool NeedsImports = Listener.needsImportVisitation();
5179   BitstreamCursor InputFilesCursor;
5180 
5181   RecordData Record;
5182   std::string ModuleDir;
5183   bool DoneWithControlBlock = false;
5184   while (!DoneWithControlBlock) {
5185     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5186     if (!MaybeEntry) {
5187       // FIXME this drops the error on the floor.
5188       consumeError(MaybeEntry.takeError());
5189       return true;
5190     }
5191     llvm::BitstreamEntry Entry = MaybeEntry.get();
5192 
5193     switch (Entry.Kind) {
5194     case llvm::BitstreamEntry::SubBlock: {
5195       switch (Entry.ID) {
5196       case OPTIONS_BLOCK_ID: {
5197         std::string IgnoredSuggestedPredefines;
5198         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5199                              /*AllowCompatibleConfigurationMismatch*/ false,
5200                              Listener, IgnoredSuggestedPredefines) != Success)
5201           return true;
5202         break;
5203       }
5204 
5205       case INPUT_FILES_BLOCK_ID:
5206         InputFilesCursor = Stream;
5207         if (llvm::Error Err = Stream.SkipBlock()) {
5208           // FIXME this drops the error on the floor.
5209           consumeError(std::move(Err));
5210           return true;
5211         }
5212         if (NeedsInputFiles &&
5213             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5214           return true;
5215         break;
5216 
5217       default:
5218         if (llvm::Error Err = Stream.SkipBlock()) {
5219           // FIXME this drops the error on the floor.
5220           consumeError(std::move(Err));
5221           return true;
5222         }
5223         break;
5224       }
5225 
5226       continue;
5227     }
5228 
5229     case llvm::BitstreamEntry::EndBlock:
5230       DoneWithControlBlock = true;
5231       break;
5232 
5233     case llvm::BitstreamEntry::Error:
5234       return true;
5235 
5236     case llvm::BitstreamEntry::Record:
5237       break;
5238     }
5239 
5240     if (DoneWithControlBlock) break;
5241 
5242     Record.clear();
5243     StringRef Blob;
5244     Expected<unsigned> MaybeRecCode =
5245         Stream.readRecord(Entry.ID, Record, &Blob);
5246     if (!MaybeRecCode) {
5247       // FIXME this drops the error.
5248       return Failure;
5249     }
5250     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5251     case METADATA:
5252       if (Record[0] != VERSION_MAJOR)
5253         return true;
5254       if (Listener.ReadFullVersionInformation(Blob))
5255         return true;
5256       break;
5257     case MODULE_NAME:
5258       Listener.ReadModuleName(Blob);
5259       break;
5260     case MODULE_DIRECTORY:
5261       ModuleDir = std::string(Blob);
5262       break;
5263     case MODULE_MAP_FILE: {
5264       unsigned Idx = 0;
5265       auto Path = ReadString(Record, Idx);
5266       ResolveImportedPath(Path, ModuleDir);
5267       Listener.ReadModuleMapFile(Path);
5268       break;
5269     }
5270     case INPUT_FILE_OFFSETS: {
5271       if (!NeedsInputFiles)
5272         break;
5273 
5274       unsigned NumInputFiles = Record[0];
5275       unsigned NumUserFiles = Record[1];
5276       const llvm::support::unaligned_uint64_t *InputFileOffs =
5277           (const llvm::support::unaligned_uint64_t *)Blob.data();
5278       for (unsigned I = 0; I != NumInputFiles; ++I) {
5279         // Go find this input file.
5280         bool isSystemFile = I >= NumUserFiles;
5281 
5282         if (isSystemFile && !NeedsSystemInputFiles)
5283           break; // the rest are system input files
5284 
5285         BitstreamCursor &Cursor = InputFilesCursor;
5286         SavedStreamPosition SavedPosition(Cursor);
5287         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5288           // FIXME this drops errors on the floor.
5289           consumeError(std::move(Err));
5290         }
5291 
5292         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5293         if (!MaybeCode) {
5294           // FIXME this drops errors on the floor.
5295           consumeError(MaybeCode.takeError());
5296         }
5297         unsigned Code = MaybeCode.get();
5298 
5299         RecordData Record;
5300         StringRef Blob;
5301         bool shouldContinue = false;
5302         Expected<unsigned> MaybeRecordType =
5303             Cursor.readRecord(Code, Record, &Blob);
5304         if (!MaybeRecordType) {
5305           // FIXME this drops errors on the floor.
5306           consumeError(MaybeRecordType.takeError());
5307         }
5308         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5309         case INPUT_FILE_HASH:
5310           break;
5311         case INPUT_FILE:
5312           bool Overridden = static_cast<bool>(Record[3]);
5313           std::string Filename = std::string(Blob);
5314           ResolveImportedPath(Filename, ModuleDir);
5315           shouldContinue = Listener.visitInputFile(
5316               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5317           break;
5318         }
5319         if (!shouldContinue)
5320           break;
5321       }
5322       break;
5323     }
5324 
5325     case IMPORTS: {
5326       if (!NeedsImports)
5327         break;
5328 
5329       unsigned Idx = 0, N = Record.size();
5330       while (Idx < N) {
5331         // Read information about the AST file.
5332         Idx +=
5333             1 + 1 + 1 + 1 +
5334             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5335         std::string ModuleName = ReadString(Record, Idx);
5336         std::string Filename = ReadString(Record, Idx);
5337         ResolveImportedPath(Filename, ModuleDir);
5338         Listener.visitImport(ModuleName, Filename);
5339       }
5340       break;
5341     }
5342 
5343     default:
5344       // No other validation to perform.
5345       break;
5346     }
5347   }
5348 
5349   // Look for module file extension blocks, if requested.
5350   if (FindModuleFileExtensions) {
5351     BitstreamCursor SavedStream = Stream;
5352     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5353       bool DoneWithExtensionBlock = false;
5354       while (!DoneWithExtensionBlock) {
5355         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5356         if (!MaybeEntry) {
5357           // FIXME this drops the error.
5358           return true;
5359         }
5360         llvm::BitstreamEntry Entry = MaybeEntry.get();
5361 
5362         switch (Entry.Kind) {
5363         case llvm::BitstreamEntry::SubBlock:
5364           if (llvm::Error Err = Stream.SkipBlock()) {
5365             // FIXME this drops the error on the floor.
5366             consumeError(std::move(Err));
5367             return true;
5368           }
5369           continue;
5370 
5371         case llvm::BitstreamEntry::EndBlock:
5372           DoneWithExtensionBlock = true;
5373           continue;
5374 
5375         case llvm::BitstreamEntry::Error:
5376           return true;
5377 
5378         case llvm::BitstreamEntry::Record:
5379           break;
5380         }
5381 
5382        Record.clear();
5383        StringRef Blob;
5384        Expected<unsigned> MaybeRecCode =
5385            Stream.readRecord(Entry.ID, Record, &Blob);
5386        if (!MaybeRecCode) {
5387          // FIXME this drops the error.
5388          return true;
5389        }
5390        switch (MaybeRecCode.get()) {
5391        case EXTENSION_METADATA: {
5392          ModuleFileExtensionMetadata Metadata;
5393          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5394            return true;
5395 
5396          Listener.readModuleFileExtension(Metadata);
5397          break;
5398        }
5399        }
5400       }
5401     }
5402     Stream = SavedStream;
5403   }
5404 
5405   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5406   if (readUnhashedControlBlockImpl(
5407           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5408           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5409           ValidateDiagnosticOptions) != Success)
5410     return true;
5411 
5412   return false;
5413 }
5414 
5415 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5416                                     const PCHContainerReader &PCHContainerRdr,
5417                                     const LangOptions &LangOpts,
5418                                     const TargetOptions &TargetOpts,
5419                                     const PreprocessorOptions &PPOpts,
5420                                     StringRef ExistingModuleCachePath) {
5421   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5422                                ExistingModuleCachePath, FileMgr);
5423   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5424                                   /*FindModuleFileExtensions=*/false,
5425                                   validator,
5426                                   /*ValidateDiagnosticOptions=*/true);
5427 }
5428 
5429 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5430                                           unsigned ClientLoadCapabilities) {
5431   // Enter the submodule block.
5432   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))
5433     return Err;
5434 
5435   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5436   bool First = true;
5437   Module *CurrentModule = nullptr;
5438   RecordData Record;
5439   while (true) {
5440     Expected<llvm::BitstreamEntry> MaybeEntry =
5441         F.Stream.advanceSkippingSubblocks();
5442     if (!MaybeEntry)
5443       return MaybeEntry.takeError();
5444     llvm::BitstreamEntry Entry = MaybeEntry.get();
5445 
5446     switch (Entry.Kind) {
5447     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5448     case llvm::BitstreamEntry::Error:
5449       return llvm::createStringError(std::errc::illegal_byte_sequence,
5450                                      "malformed block record in AST file");
5451     case llvm::BitstreamEntry::EndBlock:
5452       return llvm::Error::success();
5453     case llvm::BitstreamEntry::Record:
5454       // The interesting case.
5455       break;
5456     }
5457 
5458     // Read a record.
5459     StringRef Blob;
5460     Record.clear();
5461     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5462     if (!MaybeKind)
5463       return MaybeKind.takeError();
5464     unsigned Kind = MaybeKind.get();
5465 
5466     if ((Kind == SUBMODULE_METADATA) != First)
5467       return llvm::createStringError(
5468           std::errc::illegal_byte_sequence,
5469           "submodule metadata record should be at beginning of block");
5470     First = false;
5471 
5472     // Submodule information is only valid if we have a current module.
5473     // FIXME: Should we error on these cases?
5474     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5475         Kind != SUBMODULE_DEFINITION)
5476       continue;
5477 
5478     switch (Kind) {
5479     default:  // Default behavior: ignore.
5480       break;
5481 
5482     case SUBMODULE_DEFINITION: {
5483       if (Record.size() < 12)
5484         return llvm::createStringError(std::errc::illegal_byte_sequence,
5485                                        "malformed module definition");
5486 
5487       StringRef Name = Blob;
5488       unsigned Idx = 0;
5489       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5490       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5491       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5492       bool IsFramework = Record[Idx++];
5493       bool IsExplicit = Record[Idx++];
5494       bool IsSystem = Record[Idx++];
5495       bool IsExternC = Record[Idx++];
5496       bool InferSubmodules = Record[Idx++];
5497       bool InferExplicitSubmodules = Record[Idx++];
5498       bool InferExportWildcard = Record[Idx++];
5499       bool ConfigMacrosExhaustive = Record[Idx++];
5500       bool ModuleMapIsPrivate = Record[Idx++];
5501 
5502       Module *ParentModule = nullptr;
5503       if (Parent)
5504         ParentModule = getSubmodule(Parent);
5505 
5506       // Retrieve this (sub)module from the module map, creating it if
5507       // necessary.
5508       CurrentModule =
5509           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5510               .first;
5511 
5512       // FIXME: set the definition loc for CurrentModule, or call
5513       // ModMap.setInferredModuleAllowedBy()
5514 
5515       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5516       if (GlobalIndex >= SubmodulesLoaded.size() ||
5517           SubmodulesLoaded[GlobalIndex])
5518         return llvm::createStringError(std::errc::invalid_argument,
5519                                        "too many submodules");
5520 
5521       if (!ParentModule) {
5522         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5523           // Don't emit module relocation error if we have -fno-validate-pch
5524           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5525                     DisableValidationForModuleKind::Module) &&
5526               CurFile != F.File) {
5527             auto ConflictError =
5528                 PartialDiagnostic(diag::err_module_file_conflict,
5529                                   ContextObj->DiagAllocator)
5530                 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5531                 << F.File->getName();
5532             return DiagnosticError::create(CurrentImportLoc, ConflictError);
5533           }
5534         }
5535 
5536         F.DidReadTopLevelSubmodule = true;
5537         CurrentModule->setASTFile(F.File);
5538         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5539       }
5540 
5541       CurrentModule->Kind = Kind;
5542       CurrentModule->Signature = F.Signature;
5543       CurrentModule->IsFromModuleFile = true;
5544       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5545       CurrentModule->IsExternC = IsExternC;
5546       CurrentModule->InferSubmodules = InferSubmodules;
5547       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5548       CurrentModule->InferExportWildcard = InferExportWildcard;
5549       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5550       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5551       if (DeserializationListener)
5552         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5553 
5554       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5555 
5556       // Clear out data that will be replaced by what is in the module file.
5557       CurrentModule->LinkLibraries.clear();
5558       CurrentModule->ConfigMacros.clear();
5559       CurrentModule->UnresolvedConflicts.clear();
5560       CurrentModule->Conflicts.clear();
5561 
5562       // The module is available unless it's missing a requirement; relevant
5563       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5564       // Missing headers that were present when the module was built do not
5565       // make it unavailable -- if we got this far, this must be an explicitly
5566       // imported module file.
5567       CurrentModule->Requirements.clear();
5568       CurrentModule->MissingHeaders.clear();
5569       CurrentModule->IsUnimportable =
5570           ParentModule && ParentModule->IsUnimportable;
5571       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5572       break;
5573     }
5574 
5575     case SUBMODULE_UMBRELLA_HEADER: {
5576       // FIXME: This doesn't work for framework modules as `Filename` is the
5577       //        name as written in the module file and does not include
5578       //        `Headers/`, so this path will never exist.
5579       std::string Filename = std::string(Blob);
5580       ResolveImportedPath(F, Filename);
5581       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5582         if (!CurrentModule->getUmbrellaHeader()) {
5583           // FIXME: NameAsWritten
5584           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5585         }
5586         // Note that it's too late at this point to return out of date if the
5587         // name from the PCM doesn't match up with the one in the module map,
5588         // but also quite unlikely since we will have already checked the
5589         // modification time and size of the module map file itself.
5590       }
5591       break;
5592     }
5593 
5594     case SUBMODULE_HEADER:
5595     case SUBMODULE_EXCLUDED_HEADER:
5596     case SUBMODULE_PRIVATE_HEADER:
5597       // We lazily associate headers with their modules via the HeaderInfo table.
5598       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5599       // of complete filenames or remove it entirely.
5600       break;
5601 
5602     case SUBMODULE_TEXTUAL_HEADER:
5603     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5604       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5605       // them here.
5606       break;
5607 
5608     case SUBMODULE_TOPHEADER:
5609       CurrentModule->addTopHeaderFilename(Blob);
5610       break;
5611 
5612     case SUBMODULE_UMBRELLA_DIR: {
5613       // See comments in SUBMODULE_UMBRELLA_HEADER
5614       std::string Dirname = std::string(Blob);
5615       ResolveImportedPath(F, Dirname);
5616       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5617         if (!CurrentModule->getUmbrellaDir()) {
5618           // FIXME: NameAsWritten
5619           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5620         }
5621       }
5622       break;
5623     }
5624 
5625     case SUBMODULE_METADATA: {
5626       F.BaseSubmoduleID = getTotalNumSubmodules();
5627       F.LocalNumSubmodules = Record[0];
5628       unsigned LocalBaseSubmoduleID = Record[1];
5629       if (F.LocalNumSubmodules > 0) {
5630         // Introduce the global -> local mapping for submodules within this
5631         // module.
5632         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5633 
5634         // Introduce the local -> global mapping for submodules within this
5635         // module.
5636         F.SubmoduleRemap.insertOrReplace(
5637           std::make_pair(LocalBaseSubmoduleID,
5638                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5639 
5640         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5641       }
5642       break;
5643     }
5644 
5645     case SUBMODULE_IMPORTS:
5646       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5647         UnresolvedModuleRef Unresolved;
5648         Unresolved.File = &F;
5649         Unresolved.Mod = CurrentModule;
5650         Unresolved.ID = Record[Idx];
5651         Unresolved.Kind = UnresolvedModuleRef::Import;
5652         Unresolved.IsWildcard = false;
5653         UnresolvedModuleRefs.push_back(Unresolved);
5654       }
5655       break;
5656 
5657     case SUBMODULE_EXPORTS:
5658       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5659         UnresolvedModuleRef Unresolved;
5660         Unresolved.File = &F;
5661         Unresolved.Mod = CurrentModule;
5662         Unresolved.ID = Record[Idx];
5663         Unresolved.Kind = UnresolvedModuleRef::Export;
5664         Unresolved.IsWildcard = Record[Idx + 1];
5665         UnresolvedModuleRefs.push_back(Unresolved);
5666       }
5667 
5668       // Once we've loaded the set of exports, there's no reason to keep
5669       // the parsed, unresolved exports around.
5670       CurrentModule->UnresolvedExports.clear();
5671       break;
5672 
5673     case SUBMODULE_REQUIRES:
5674       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5675                                     PP.getTargetInfo());
5676       break;
5677 
5678     case SUBMODULE_LINK_LIBRARY:
5679       ModMap.resolveLinkAsDependencies(CurrentModule);
5680       CurrentModule->LinkLibraries.push_back(
5681           Module::LinkLibrary(std::string(Blob), Record[0]));
5682       break;
5683 
5684     case SUBMODULE_CONFIG_MACRO:
5685       CurrentModule->ConfigMacros.push_back(Blob.str());
5686       break;
5687 
5688     case SUBMODULE_CONFLICT: {
5689       UnresolvedModuleRef Unresolved;
5690       Unresolved.File = &F;
5691       Unresolved.Mod = CurrentModule;
5692       Unresolved.ID = Record[0];
5693       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5694       Unresolved.IsWildcard = false;
5695       Unresolved.String = Blob;
5696       UnresolvedModuleRefs.push_back(Unresolved);
5697       break;
5698     }
5699 
5700     case SUBMODULE_INITIALIZERS: {
5701       if (!ContextObj)
5702         break;
5703       SmallVector<uint32_t, 16> Inits;
5704       for (auto &ID : Record)
5705         Inits.push_back(getGlobalDeclID(F, ID));
5706       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5707       break;
5708     }
5709 
5710     case SUBMODULE_EXPORT_AS:
5711       CurrentModule->ExportAsModule = Blob.str();
5712       ModMap.addLinkAsDependency(CurrentModule);
5713       break;
5714     }
5715   }
5716 }
5717 
5718 /// Parse the record that corresponds to a LangOptions data
5719 /// structure.
5720 ///
5721 /// This routine parses the language options from the AST file and then gives
5722 /// them to the AST listener if one is set.
5723 ///
5724 /// \returns true if the listener deems the file unacceptable, false otherwise.
5725 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5726                                      bool Complain,
5727                                      ASTReaderListener &Listener,
5728                                      bool AllowCompatibleDifferences) {
5729   LangOptions LangOpts;
5730   unsigned Idx = 0;
5731 #define LANGOPT(Name, Bits, Default, Description) \
5732   LangOpts.Name = Record[Idx++];
5733 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5734   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5735 #include "clang/Basic/LangOptions.def"
5736 #define SANITIZER(NAME, ID)                                                    \
5737   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5738 #include "clang/Basic/Sanitizers.def"
5739 
5740   for (unsigned N = Record[Idx++]; N; --N)
5741     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5742 
5743   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5744   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5745   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5746 
5747   LangOpts.CurrentModule = ReadString(Record, Idx);
5748 
5749   // Comment options.
5750   for (unsigned N = Record[Idx++]; N; --N) {
5751     LangOpts.CommentOpts.BlockCommandNames.push_back(
5752       ReadString(Record, Idx));
5753   }
5754   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5755 
5756   // OpenMP offloading options.
5757   for (unsigned N = Record[Idx++]; N; --N) {
5758     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5759   }
5760 
5761   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5762 
5763   return Listener.ReadLanguageOptions(LangOpts, Complain,
5764                                       AllowCompatibleDifferences);
5765 }
5766 
5767 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5768                                    ASTReaderListener &Listener,
5769                                    bool AllowCompatibleDifferences) {
5770   unsigned Idx = 0;
5771   TargetOptions TargetOpts;
5772   TargetOpts.Triple = ReadString(Record, Idx);
5773   TargetOpts.CPU = ReadString(Record, Idx);
5774   TargetOpts.TuneCPU = ReadString(Record, Idx);
5775   TargetOpts.ABI = ReadString(Record, Idx);
5776   for (unsigned N = Record[Idx++]; N; --N) {
5777     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5778   }
5779   for (unsigned N = Record[Idx++]; N; --N) {
5780     TargetOpts.Features.push_back(ReadString(Record, Idx));
5781   }
5782 
5783   return Listener.ReadTargetOptions(TargetOpts, Complain,
5784                                     AllowCompatibleDifferences);
5785 }
5786 
5787 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5788                                        ASTReaderListener &Listener) {
5789   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5790   unsigned Idx = 0;
5791 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5792 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5793   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5794 #include "clang/Basic/DiagnosticOptions.def"
5795 
5796   for (unsigned N = Record[Idx++]; N; --N)
5797     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5798   for (unsigned N = Record[Idx++]; N; --N)
5799     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5800 
5801   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5802 }
5803 
5804 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5805                                        ASTReaderListener &Listener) {
5806   FileSystemOptions FSOpts;
5807   unsigned Idx = 0;
5808   FSOpts.WorkingDir = ReadString(Record, Idx);
5809   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5810 }
5811 
5812 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5813                                          bool Complain,
5814                                          ASTReaderListener &Listener) {
5815   HeaderSearchOptions HSOpts;
5816   unsigned Idx = 0;
5817   HSOpts.Sysroot = ReadString(Record, Idx);
5818 
5819   // Include entries.
5820   for (unsigned N = Record[Idx++]; N; --N) {
5821     std::string Path = ReadString(Record, Idx);
5822     frontend::IncludeDirGroup Group
5823       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5824     bool IsFramework = Record[Idx++];
5825     bool IgnoreSysRoot = Record[Idx++];
5826     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5827                                     IgnoreSysRoot);
5828   }
5829 
5830   // System header prefixes.
5831   for (unsigned N = Record[Idx++]; N; --N) {
5832     std::string Prefix = ReadString(Record, Idx);
5833     bool IsSystemHeader = Record[Idx++];
5834     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5835   }
5836 
5837   HSOpts.ResourceDir = ReadString(Record, Idx);
5838   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5839   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5840   HSOpts.DisableModuleHash = Record[Idx++];
5841   HSOpts.ImplicitModuleMaps = Record[Idx++];
5842   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5843   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5844   HSOpts.UseBuiltinIncludes = Record[Idx++];
5845   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5846   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5847   HSOpts.UseLibcxx = Record[Idx++];
5848   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5849 
5850   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5851                                           Complain);
5852 }
5853 
5854 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5855                                          bool Complain,
5856                                          ASTReaderListener &Listener,
5857                                          std::string &SuggestedPredefines) {
5858   PreprocessorOptions PPOpts;
5859   unsigned Idx = 0;
5860 
5861   // Macro definitions/undefs
5862   for (unsigned N = Record[Idx++]; N; --N) {
5863     std::string Macro = ReadString(Record, Idx);
5864     bool IsUndef = Record[Idx++];
5865     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5866   }
5867 
5868   // Includes
5869   for (unsigned N = Record[Idx++]; N; --N) {
5870     PPOpts.Includes.push_back(ReadString(Record, Idx));
5871   }
5872 
5873   // Macro Includes
5874   for (unsigned N = Record[Idx++]; N; --N) {
5875     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5876   }
5877 
5878   PPOpts.UsePredefines = Record[Idx++];
5879   PPOpts.DetailedRecord = Record[Idx++];
5880   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5881   PPOpts.ObjCXXARCStandardLibrary =
5882     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5883   SuggestedPredefines.clear();
5884   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5885                                           SuggestedPredefines);
5886 }
5887 
5888 std::pair<ModuleFile *, unsigned>
5889 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5890   GlobalPreprocessedEntityMapType::iterator
5891   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5892   assert(I != GlobalPreprocessedEntityMap.end() &&
5893          "Corrupted global preprocessed entity map");
5894   ModuleFile *M = I->second;
5895   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5896   return std::make_pair(M, LocalIndex);
5897 }
5898 
5899 llvm::iterator_range<PreprocessingRecord::iterator>
5900 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5901   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5902     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5903                                              Mod.NumPreprocessedEntities);
5904 
5905   return llvm::make_range(PreprocessingRecord::iterator(),
5906                           PreprocessingRecord::iterator());
5907 }
5908 
5909 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
5910                                         unsigned int ClientLoadCapabilities) {
5911   return ClientLoadCapabilities & ARR_OutOfDate &&
5912          !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
5913 }
5914 
5915 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5916 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5917   return llvm::make_range(
5918       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5919       ModuleDeclIterator(this, &Mod,
5920                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5921 }
5922 
5923 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5924   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5925   assert(I != GlobalSkippedRangeMap.end() &&
5926     "Corrupted global skipped range map");
5927   ModuleFile *M = I->second;
5928   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5929   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5930   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5931   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5932                     TranslateSourceLocation(*M, RawRange.getEnd()));
5933   assert(Range.isValid());
5934   return Range;
5935 }
5936 
5937 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5938   PreprocessedEntityID PPID = Index+1;
5939   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5940   ModuleFile &M = *PPInfo.first;
5941   unsigned LocalIndex = PPInfo.second;
5942   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5943 
5944   if (!PP.getPreprocessingRecord()) {
5945     Error("no preprocessing record");
5946     return nullptr;
5947   }
5948 
5949   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5950   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5951           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5952     Error(std::move(Err));
5953     return nullptr;
5954   }
5955 
5956   Expected<llvm::BitstreamEntry> MaybeEntry =
5957       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5958   if (!MaybeEntry) {
5959     Error(MaybeEntry.takeError());
5960     return nullptr;
5961   }
5962   llvm::BitstreamEntry Entry = MaybeEntry.get();
5963 
5964   if (Entry.Kind != llvm::BitstreamEntry::Record)
5965     return nullptr;
5966 
5967   // Read the record.
5968   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5969                     TranslateSourceLocation(M, PPOffs.getEnd()));
5970   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5971   StringRef Blob;
5972   RecordData Record;
5973   Expected<unsigned> MaybeRecType =
5974       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5975   if (!MaybeRecType) {
5976     Error(MaybeRecType.takeError());
5977     return nullptr;
5978   }
5979   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5980   case PPD_MACRO_EXPANSION: {
5981     bool isBuiltin = Record[0];
5982     IdentifierInfo *Name = nullptr;
5983     MacroDefinitionRecord *Def = nullptr;
5984     if (isBuiltin)
5985       Name = getLocalIdentifier(M, Record[1]);
5986     else {
5987       PreprocessedEntityID GlobalID =
5988           getGlobalPreprocessedEntityID(M, Record[1]);
5989       Def = cast<MacroDefinitionRecord>(
5990           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5991     }
5992 
5993     MacroExpansion *ME;
5994     if (isBuiltin)
5995       ME = new (PPRec) MacroExpansion(Name, Range);
5996     else
5997       ME = new (PPRec) MacroExpansion(Def, Range);
5998 
5999     return ME;
6000   }
6001 
6002   case PPD_MACRO_DEFINITION: {
6003     // Decode the identifier info and then check again; if the macro is
6004     // still defined and associated with the identifier,
6005     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6006     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6007 
6008     if (DeserializationListener)
6009       DeserializationListener->MacroDefinitionRead(PPID, MD);
6010 
6011     return MD;
6012   }
6013 
6014   case PPD_INCLUSION_DIRECTIVE: {
6015     const char *FullFileNameStart = Blob.data() + Record[0];
6016     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6017     const FileEntry *File = nullptr;
6018     if (!FullFileName.empty())
6019       if (auto FE = PP.getFileManager().getFile(FullFileName))
6020         File = *FE;
6021 
6022     // FIXME: Stable encoding
6023     InclusionDirective::InclusionKind Kind
6024       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6025     InclusionDirective *ID
6026       = new (PPRec) InclusionDirective(PPRec, Kind,
6027                                        StringRef(Blob.data(), Record[0]),
6028                                        Record[1], Record[3],
6029                                        File,
6030                                        Range);
6031     return ID;
6032   }
6033   }
6034 
6035   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6036 }
6037 
6038 /// Find the next module that contains entities and return the ID
6039 /// of the first entry.
6040 ///
6041 /// \param SLocMapI points at a chunk of a module that contains no
6042 /// preprocessed entities or the entities it contains are not the ones we are
6043 /// looking for.
6044 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6045                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6046   ++SLocMapI;
6047   for (GlobalSLocOffsetMapType::const_iterator
6048          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6049     ModuleFile &M = *SLocMapI->second;
6050     if (M.NumPreprocessedEntities)
6051       return M.BasePreprocessedEntityID;
6052   }
6053 
6054   return getTotalNumPreprocessedEntities();
6055 }
6056 
6057 namespace {
6058 
6059 struct PPEntityComp {
6060   const ASTReader &Reader;
6061   ModuleFile &M;
6062 
6063   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6064 
6065   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6066     SourceLocation LHS = getLoc(L);
6067     SourceLocation RHS = getLoc(R);
6068     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6069   }
6070 
6071   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6072     SourceLocation LHS = getLoc(L);
6073     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6074   }
6075 
6076   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6077     SourceLocation RHS = getLoc(R);
6078     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6079   }
6080 
6081   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6082     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6083   }
6084 };
6085 
6086 } // namespace
6087 
6088 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6089                                                        bool EndsAfter) const {
6090   if (SourceMgr.isLocalSourceLocation(Loc))
6091     return getTotalNumPreprocessedEntities();
6092 
6093   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6094       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6095   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6096          "Corrupted global sloc offset map");
6097 
6098   if (SLocMapI->second->NumPreprocessedEntities == 0)
6099     return findNextPreprocessedEntity(SLocMapI);
6100 
6101   ModuleFile &M = *SLocMapI->second;
6102 
6103   using pp_iterator = const PPEntityOffset *;
6104 
6105   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6106   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6107 
6108   size_t Count = M.NumPreprocessedEntities;
6109   size_t Half;
6110   pp_iterator First = pp_begin;
6111   pp_iterator PPI;
6112 
6113   if (EndsAfter) {
6114     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6115                            PPEntityComp(*this, M));
6116   } else {
6117     // Do a binary search manually instead of using std::lower_bound because
6118     // The end locations of entities may be unordered (when a macro expansion
6119     // is inside another macro argument), but for this case it is not important
6120     // whether we get the first macro expansion or its containing macro.
6121     while (Count > 0) {
6122       Half = Count / 2;
6123       PPI = First;
6124       std::advance(PPI, Half);
6125       if (SourceMgr.isBeforeInTranslationUnit(
6126               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6127         First = PPI;
6128         ++First;
6129         Count = Count - Half - 1;
6130       } else
6131         Count = Half;
6132     }
6133   }
6134 
6135   if (PPI == pp_end)
6136     return findNextPreprocessedEntity(SLocMapI);
6137 
6138   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6139 }
6140 
6141 /// Returns a pair of [Begin, End) indices of preallocated
6142 /// preprocessed entities that \arg Range encompasses.
6143 std::pair<unsigned, unsigned>
6144     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6145   if (Range.isInvalid())
6146     return std::make_pair(0,0);
6147   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6148 
6149   PreprocessedEntityID BeginID =
6150       findPreprocessedEntity(Range.getBegin(), false);
6151   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6152   return std::make_pair(BeginID, EndID);
6153 }
6154 
6155 /// Optionally returns true or false if the preallocated preprocessed
6156 /// entity with index \arg Index came from file \arg FID.
6157 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6158                                                              FileID FID) {
6159   if (FID.isInvalid())
6160     return false;
6161 
6162   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6163   ModuleFile &M = *PPInfo.first;
6164   unsigned LocalIndex = PPInfo.second;
6165   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6166 
6167   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6168   if (Loc.isInvalid())
6169     return false;
6170 
6171   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6172     return true;
6173   else
6174     return false;
6175 }
6176 
6177 namespace {
6178 
6179   /// Visitor used to search for information about a header file.
6180   class HeaderFileInfoVisitor {
6181     const FileEntry *FE;
6182     Optional<HeaderFileInfo> HFI;
6183 
6184   public:
6185     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6186 
6187     bool operator()(ModuleFile &M) {
6188       HeaderFileInfoLookupTable *Table
6189         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6190       if (!Table)
6191         return false;
6192 
6193       // Look in the on-disk hash table for an entry for this file name.
6194       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6195       if (Pos == Table->end())
6196         return false;
6197 
6198       HFI = *Pos;
6199       return true;
6200     }
6201 
6202     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6203   };
6204 
6205 } // namespace
6206 
6207 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6208   HeaderFileInfoVisitor Visitor(FE);
6209   ModuleMgr.visit(Visitor);
6210   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6211     return *HFI;
6212 
6213   return HeaderFileInfo();
6214 }
6215 
6216 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6217   using DiagState = DiagnosticsEngine::DiagState;
6218   SmallVector<DiagState *, 32> DiagStates;
6219 
6220   for (ModuleFile &F : ModuleMgr) {
6221     unsigned Idx = 0;
6222     auto &Record = F.PragmaDiagMappings;
6223     if (Record.empty())
6224       continue;
6225 
6226     DiagStates.clear();
6227 
6228     auto ReadDiagState =
6229         [&](const DiagState &BasedOn, SourceLocation Loc,
6230             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6231       unsigned BackrefID = Record[Idx++];
6232       if (BackrefID != 0)
6233         return DiagStates[BackrefID - 1];
6234 
6235       // A new DiagState was created here.
6236       Diag.DiagStates.push_back(BasedOn);
6237       DiagState *NewState = &Diag.DiagStates.back();
6238       DiagStates.push_back(NewState);
6239       unsigned Size = Record[Idx++];
6240       assert(Idx + Size * 2 <= Record.size() &&
6241              "Invalid data, not enough diag/map pairs");
6242       while (Size--) {
6243         unsigned DiagID = Record[Idx++];
6244         DiagnosticMapping NewMapping =
6245             DiagnosticMapping::deserialize(Record[Idx++]);
6246         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6247           continue;
6248 
6249         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6250 
6251         // If this mapping was specified as a warning but the severity was
6252         // upgraded due to diagnostic settings, simulate the current diagnostic
6253         // settings (and use a warning).
6254         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6255           NewMapping.setSeverity(diag::Severity::Warning);
6256           NewMapping.setUpgradedFromWarning(false);
6257         }
6258 
6259         Mapping = NewMapping;
6260       }
6261       return NewState;
6262     };
6263 
6264     // Read the first state.
6265     DiagState *FirstState;
6266     if (F.Kind == MK_ImplicitModule) {
6267       // Implicitly-built modules are reused with different diagnostic
6268       // settings.  Use the initial diagnostic state from Diag to simulate this
6269       // compilation's diagnostic settings.
6270       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6271       DiagStates.push_back(FirstState);
6272 
6273       // Skip the initial diagnostic state from the serialized module.
6274       assert(Record[1] == 0 &&
6275              "Invalid data, unexpected backref in initial state");
6276       Idx = 3 + Record[2] * 2;
6277       assert(Idx < Record.size() &&
6278              "Invalid data, not enough state change pairs in initial state");
6279     } else if (F.isModule()) {
6280       // For an explicit module, preserve the flags from the module build
6281       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6282       // -Wblah flags.
6283       unsigned Flags = Record[Idx++];
6284       DiagState Initial;
6285       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6286       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6287       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6288       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6289       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6290       Initial.ExtBehavior = (diag::Severity)Flags;
6291       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6292 
6293       assert(F.OriginalSourceFileID.isValid());
6294 
6295       // Set up the root buffer of the module to start with the initial
6296       // diagnostic state of the module itself, to cover files that contain no
6297       // explicit transitions (for which we did not serialize anything).
6298       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6299           .StateTransitions.push_back({FirstState, 0});
6300     } else {
6301       // For prefix ASTs, start with whatever the user configured on the
6302       // command line.
6303       Idx++; // Skip flags.
6304       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6305                                  SourceLocation(), false);
6306     }
6307 
6308     // Read the state transitions.
6309     unsigned NumLocations = Record[Idx++];
6310     while (NumLocations--) {
6311       assert(Idx < Record.size() &&
6312              "Invalid data, missing pragma diagnostic states");
6313       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6314       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6315       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6316       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6317       unsigned Transitions = Record[Idx++];
6318 
6319       // Note that we don't need to set up Parent/ParentOffset here, because
6320       // we won't be changing the diagnostic state within imported FileIDs
6321       // (other than perhaps appending to the main source file, which has no
6322       // parent).
6323       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6324       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6325       for (unsigned I = 0; I != Transitions; ++I) {
6326         unsigned Offset = Record[Idx++];
6327         auto *State =
6328             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6329         F.StateTransitions.push_back({State, Offset});
6330       }
6331     }
6332 
6333     // Read the final state.
6334     assert(Idx < Record.size() &&
6335            "Invalid data, missing final pragma diagnostic state");
6336     SourceLocation CurStateLoc =
6337         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6338     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6339 
6340     if (!F.isModule()) {
6341       Diag.DiagStatesByLoc.CurDiagState = CurState;
6342       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6343 
6344       // Preserve the property that the imaginary root file describes the
6345       // current state.
6346       FileID NullFile;
6347       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6348       if (T.empty())
6349         T.push_back({CurState, 0});
6350       else
6351         T[0].State = CurState;
6352     }
6353 
6354     // Don't try to read these mappings again.
6355     Record.clear();
6356   }
6357 }
6358 
6359 /// Get the correct cursor and offset for loading a type.
6360 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6361   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6362   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6363   ModuleFile *M = I->second;
6364   return RecordLocation(
6365       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6366              M->DeclsBlockStartOffset);
6367 }
6368 
6369 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6370   switch (code) {
6371 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6372   case TYPE_##CODE_ID: return Type::CLASS_ID;
6373 #include "clang/Serialization/TypeBitCodes.def"
6374   default: return llvm::None;
6375   }
6376 }
6377 
6378 /// Read and return the type with the given index..
6379 ///
6380 /// The index is the type ID, shifted and minus the number of predefs. This
6381 /// routine actually reads the record corresponding to the type at the given
6382 /// location. It is a helper routine for GetType, which deals with reading type
6383 /// IDs.
6384 QualType ASTReader::readTypeRecord(unsigned Index) {
6385   assert(ContextObj && "reading type with no AST context");
6386   ASTContext &Context = *ContextObj;
6387   RecordLocation Loc = TypeCursorForIndex(Index);
6388   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6389 
6390   // Keep track of where we are in the stream, then jump back there
6391   // after reading this type.
6392   SavedStreamPosition SavedPosition(DeclsCursor);
6393 
6394   ReadingKindTracker ReadingKind(Read_Type, *this);
6395 
6396   // Note that we are loading a type record.
6397   Deserializing AType(this);
6398 
6399   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6400     Error(std::move(Err));
6401     return QualType();
6402   }
6403   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6404   if (!RawCode) {
6405     Error(RawCode.takeError());
6406     return QualType();
6407   }
6408 
6409   ASTRecordReader Record(*this, *Loc.F);
6410   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6411   if (!Code) {
6412     Error(Code.takeError());
6413     return QualType();
6414   }
6415   if (Code.get() == TYPE_EXT_QUAL) {
6416     QualType baseType = Record.readQualType();
6417     Qualifiers quals = Record.readQualifiers();
6418     return Context.getQualifiedType(baseType, quals);
6419   }
6420 
6421   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6422   if (!maybeClass) {
6423     Error("Unexpected code for type");
6424     return QualType();
6425   }
6426 
6427   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6428   return TypeReader.read(*maybeClass);
6429 }
6430 
6431 namespace clang {
6432 
6433 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6434   ASTRecordReader &Reader;
6435 
6436   SourceLocation readSourceLocation() {
6437     return Reader.readSourceLocation();
6438   }
6439 
6440   TypeSourceInfo *GetTypeSourceInfo() {
6441     return Reader.readTypeSourceInfo();
6442   }
6443 
6444   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6445     return Reader.readNestedNameSpecifierLoc();
6446   }
6447 
6448   Attr *ReadAttr() {
6449     return Reader.readAttr();
6450   }
6451 
6452 public:
6453   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6454 
6455   // We want compile-time assurance that we've enumerated all of
6456   // these, so unfortunately we have to declare them first, then
6457   // define them out-of-line.
6458 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6459 #define TYPELOC(CLASS, PARENT) \
6460   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6461 #include "clang/AST/TypeLocNodes.def"
6462 
6463   void VisitFunctionTypeLoc(FunctionTypeLoc);
6464   void VisitArrayTypeLoc(ArrayTypeLoc);
6465 };
6466 
6467 } // namespace clang
6468 
6469 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6470   // nothing to do
6471 }
6472 
6473 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6474   TL.setBuiltinLoc(readSourceLocation());
6475   if (TL.needsExtraLocalData()) {
6476     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6477     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6478     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6479     TL.setModeAttr(Reader.readInt());
6480   }
6481 }
6482 
6483 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6484   TL.setNameLoc(readSourceLocation());
6485 }
6486 
6487 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6488   TL.setStarLoc(readSourceLocation());
6489 }
6490 
6491 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6492   // nothing to do
6493 }
6494 
6495 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6496   // nothing to do
6497 }
6498 
6499 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6500   TL.setExpansionLoc(readSourceLocation());
6501 }
6502 
6503 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6504   TL.setCaretLoc(readSourceLocation());
6505 }
6506 
6507 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6508   TL.setAmpLoc(readSourceLocation());
6509 }
6510 
6511 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6512   TL.setAmpAmpLoc(readSourceLocation());
6513 }
6514 
6515 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6516   TL.setStarLoc(readSourceLocation());
6517   TL.setClassTInfo(GetTypeSourceInfo());
6518 }
6519 
6520 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6521   TL.setLBracketLoc(readSourceLocation());
6522   TL.setRBracketLoc(readSourceLocation());
6523   if (Reader.readBool())
6524     TL.setSizeExpr(Reader.readExpr());
6525   else
6526     TL.setSizeExpr(nullptr);
6527 }
6528 
6529 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6530   VisitArrayTypeLoc(TL);
6531 }
6532 
6533 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6534   VisitArrayTypeLoc(TL);
6535 }
6536 
6537 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6538   VisitArrayTypeLoc(TL);
6539 }
6540 
6541 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6542                                             DependentSizedArrayTypeLoc TL) {
6543   VisitArrayTypeLoc(TL);
6544 }
6545 
6546 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6547     DependentAddressSpaceTypeLoc TL) {
6548 
6549     TL.setAttrNameLoc(readSourceLocation());
6550     TL.setAttrOperandParensRange(Reader.readSourceRange());
6551     TL.setAttrExprOperand(Reader.readExpr());
6552 }
6553 
6554 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6555                                         DependentSizedExtVectorTypeLoc TL) {
6556   TL.setNameLoc(readSourceLocation());
6557 }
6558 
6559 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6560   TL.setNameLoc(readSourceLocation());
6561 }
6562 
6563 void TypeLocReader::VisitDependentVectorTypeLoc(
6564     DependentVectorTypeLoc TL) {
6565   TL.setNameLoc(readSourceLocation());
6566 }
6567 
6568 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6569   TL.setNameLoc(readSourceLocation());
6570 }
6571 
6572 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6573   TL.setAttrNameLoc(readSourceLocation());
6574   TL.setAttrOperandParensRange(Reader.readSourceRange());
6575   TL.setAttrRowOperand(Reader.readExpr());
6576   TL.setAttrColumnOperand(Reader.readExpr());
6577 }
6578 
6579 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6580     DependentSizedMatrixTypeLoc TL) {
6581   TL.setAttrNameLoc(readSourceLocation());
6582   TL.setAttrOperandParensRange(Reader.readSourceRange());
6583   TL.setAttrRowOperand(Reader.readExpr());
6584   TL.setAttrColumnOperand(Reader.readExpr());
6585 }
6586 
6587 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6588   TL.setLocalRangeBegin(readSourceLocation());
6589   TL.setLParenLoc(readSourceLocation());
6590   TL.setRParenLoc(readSourceLocation());
6591   TL.setExceptionSpecRange(Reader.readSourceRange());
6592   TL.setLocalRangeEnd(readSourceLocation());
6593   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6594     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6595   }
6596 }
6597 
6598 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6599   VisitFunctionTypeLoc(TL);
6600 }
6601 
6602 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6603   VisitFunctionTypeLoc(TL);
6604 }
6605 
6606 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6607   TL.setNameLoc(readSourceLocation());
6608 }
6609 
6610 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6611   TL.setNameLoc(readSourceLocation());
6612 }
6613 
6614 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6615   TL.setTypeofLoc(readSourceLocation());
6616   TL.setLParenLoc(readSourceLocation());
6617   TL.setRParenLoc(readSourceLocation());
6618 }
6619 
6620 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6621   TL.setTypeofLoc(readSourceLocation());
6622   TL.setLParenLoc(readSourceLocation());
6623   TL.setRParenLoc(readSourceLocation());
6624   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6625 }
6626 
6627 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6628   TL.setNameLoc(readSourceLocation());
6629 }
6630 
6631 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6632   TL.setKWLoc(readSourceLocation());
6633   TL.setLParenLoc(readSourceLocation());
6634   TL.setRParenLoc(readSourceLocation());
6635   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6636 }
6637 
6638 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6639   TL.setNameLoc(readSourceLocation());
6640   if (Reader.readBool()) {
6641     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6642     TL.setTemplateKWLoc(readSourceLocation());
6643     TL.setConceptNameLoc(readSourceLocation());
6644     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6645     TL.setLAngleLoc(readSourceLocation());
6646     TL.setRAngleLoc(readSourceLocation());
6647     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6648       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6649                               TL.getTypePtr()->getArg(i).getKind()));
6650   }
6651 }
6652 
6653 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6654     DeducedTemplateSpecializationTypeLoc TL) {
6655   TL.setTemplateNameLoc(readSourceLocation());
6656 }
6657 
6658 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6659   TL.setNameLoc(readSourceLocation());
6660 }
6661 
6662 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6663   TL.setNameLoc(readSourceLocation());
6664 }
6665 
6666 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6667   TL.setAttr(ReadAttr());
6668 }
6669 
6670 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6671   TL.setNameLoc(readSourceLocation());
6672 }
6673 
6674 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6675                                             SubstTemplateTypeParmTypeLoc TL) {
6676   TL.setNameLoc(readSourceLocation());
6677 }
6678 
6679 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6680                                           SubstTemplateTypeParmPackTypeLoc TL) {
6681   TL.setNameLoc(readSourceLocation());
6682 }
6683 
6684 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6685                                            TemplateSpecializationTypeLoc TL) {
6686   TL.setTemplateKeywordLoc(readSourceLocation());
6687   TL.setTemplateNameLoc(readSourceLocation());
6688   TL.setLAngleLoc(readSourceLocation());
6689   TL.setRAngleLoc(readSourceLocation());
6690   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6691     TL.setArgLocInfo(
6692         i,
6693         Reader.readTemplateArgumentLocInfo(
6694           TL.getTypePtr()->getArg(i).getKind()));
6695 }
6696 
6697 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6698   TL.setLParenLoc(readSourceLocation());
6699   TL.setRParenLoc(readSourceLocation());
6700 }
6701 
6702 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6703   TL.setElaboratedKeywordLoc(readSourceLocation());
6704   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6705 }
6706 
6707 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6708   TL.setNameLoc(readSourceLocation());
6709 }
6710 
6711 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6712   TL.setElaboratedKeywordLoc(readSourceLocation());
6713   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6714   TL.setNameLoc(readSourceLocation());
6715 }
6716 
6717 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6718        DependentTemplateSpecializationTypeLoc TL) {
6719   TL.setElaboratedKeywordLoc(readSourceLocation());
6720   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6721   TL.setTemplateKeywordLoc(readSourceLocation());
6722   TL.setTemplateNameLoc(readSourceLocation());
6723   TL.setLAngleLoc(readSourceLocation());
6724   TL.setRAngleLoc(readSourceLocation());
6725   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6726     TL.setArgLocInfo(
6727         I,
6728         Reader.readTemplateArgumentLocInfo(
6729             TL.getTypePtr()->getArg(I).getKind()));
6730 }
6731 
6732 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6733   TL.setEllipsisLoc(readSourceLocation());
6734 }
6735 
6736 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6737   TL.setNameLoc(readSourceLocation());
6738 }
6739 
6740 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6741   if (TL.getNumProtocols()) {
6742     TL.setProtocolLAngleLoc(readSourceLocation());
6743     TL.setProtocolRAngleLoc(readSourceLocation());
6744   }
6745   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6746     TL.setProtocolLoc(i, readSourceLocation());
6747 }
6748 
6749 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6750   TL.setHasBaseTypeAsWritten(Reader.readBool());
6751   TL.setTypeArgsLAngleLoc(readSourceLocation());
6752   TL.setTypeArgsRAngleLoc(readSourceLocation());
6753   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6754     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6755   TL.setProtocolLAngleLoc(readSourceLocation());
6756   TL.setProtocolRAngleLoc(readSourceLocation());
6757   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6758     TL.setProtocolLoc(i, readSourceLocation());
6759 }
6760 
6761 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6762   TL.setStarLoc(readSourceLocation());
6763 }
6764 
6765 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6766   TL.setKWLoc(readSourceLocation());
6767   TL.setLParenLoc(readSourceLocation());
6768   TL.setRParenLoc(readSourceLocation());
6769 }
6770 
6771 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6772   TL.setKWLoc(readSourceLocation());
6773 }
6774 
6775 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6776   TL.setNameLoc(readSourceLocation());
6777 }
6778 void TypeLocReader::VisitDependentExtIntTypeLoc(
6779     clang::DependentExtIntTypeLoc TL) {
6780   TL.setNameLoc(readSourceLocation());
6781 }
6782 
6783 
6784 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6785   TypeLocReader TLR(*this);
6786   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6787     TLR.Visit(TL);
6788 }
6789 
6790 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6791   QualType InfoTy = readType();
6792   if (InfoTy.isNull())
6793     return nullptr;
6794 
6795   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6796   readTypeLoc(TInfo->getTypeLoc());
6797   return TInfo;
6798 }
6799 
6800 QualType ASTReader::GetType(TypeID ID) {
6801   assert(ContextObj && "reading type with no AST context");
6802   ASTContext &Context = *ContextObj;
6803 
6804   unsigned FastQuals = ID & Qualifiers::FastMask;
6805   unsigned Index = ID >> Qualifiers::FastWidth;
6806 
6807   if (Index < NUM_PREDEF_TYPE_IDS) {
6808     QualType T;
6809     switch ((PredefinedTypeIDs)Index) {
6810     case PREDEF_TYPE_NULL_ID:
6811       return QualType();
6812     case PREDEF_TYPE_VOID_ID:
6813       T = Context.VoidTy;
6814       break;
6815     case PREDEF_TYPE_BOOL_ID:
6816       T = Context.BoolTy;
6817       break;
6818     case PREDEF_TYPE_CHAR_U_ID:
6819     case PREDEF_TYPE_CHAR_S_ID:
6820       // FIXME: Check that the signedness of CharTy is correct!
6821       T = Context.CharTy;
6822       break;
6823     case PREDEF_TYPE_UCHAR_ID:
6824       T = Context.UnsignedCharTy;
6825       break;
6826     case PREDEF_TYPE_USHORT_ID:
6827       T = Context.UnsignedShortTy;
6828       break;
6829     case PREDEF_TYPE_UINT_ID:
6830       T = Context.UnsignedIntTy;
6831       break;
6832     case PREDEF_TYPE_ULONG_ID:
6833       T = Context.UnsignedLongTy;
6834       break;
6835     case PREDEF_TYPE_ULONGLONG_ID:
6836       T = Context.UnsignedLongLongTy;
6837       break;
6838     case PREDEF_TYPE_UINT128_ID:
6839       T = Context.UnsignedInt128Ty;
6840       break;
6841     case PREDEF_TYPE_SCHAR_ID:
6842       T = Context.SignedCharTy;
6843       break;
6844     case PREDEF_TYPE_WCHAR_ID:
6845       T = Context.WCharTy;
6846       break;
6847     case PREDEF_TYPE_SHORT_ID:
6848       T = Context.ShortTy;
6849       break;
6850     case PREDEF_TYPE_INT_ID:
6851       T = Context.IntTy;
6852       break;
6853     case PREDEF_TYPE_LONG_ID:
6854       T = Context.LongTy;
6855       break;
6856     case PREDEF_TYPE_LONGLONG_ID:
6857       T = Context.LongLongTy;
6858       break;
6859     case PREDEF_TYPE_INT128_ID:
6860       T = Context.Int128Ty;
6861       break;
6862     case PREDEF_TYPE_BFLOAT16_ID:
6863       T = Context.BFloat16Ty;
6864       break;
6865     case PREDEF_TYPE_HALF_ID:
6866       T = Context.HalfTy;
6867       break;
6868     case PREDEF_TYPE_FLOAT_ID:
6869       T = Context.FloatTy;
6870       break;
6871     case PREDEF_TYPE_DOUBLE_ID:
6872       T = Context.DoubleTy;
6873       break;
6874     case PREDEF_TYPE_LONGDOUBLE_ID:
6875       T = Context.LongDoubleTy;
6876       break;
6877     case PREDEF_TYPE_SHORT_ACCUM_ID:
6878       T = Context.ShortAccumTy;
6879       break;
6880     case PREDEF_TYPE_ACCUM_ID:
6881       T = Context.AccumTy;
6882       break;
6883     case PREDEF_TYPE_LONG_ACCUM_ID:
6884       T = Context.LongAccumTy;
6885       break;
6886     case PREDEF_TYPE_USHORT_ACCUM_ID:
6887       T = Context.UnsignedShortAccumTy;
6888       break;
6889     case PREDEF_TYPE_UACCUM_ID:
6890       T = Context.UnsignedAccumTy;
6891       break;
6892     case PREDEF_TYPE_ULONG_ACCUM_ID:
6893       T = Context.UnsignedLongAccumTy;
6894       break;
6895     case PREDEF_TYPE_SHORT_FRACT_ID:
6896       T = Context.ShortFractTy;
6897       break;
6898     case PREDEF_TYPE_FRACT_ID:
6899       T = Context.FractTy;
6900       break;
6901     case PREDEF_TYPE_LONG_FRACT_ID:
6902       T = Context.LongFractTy;
6903       break;
6904     case PREDEF_TYPE_USHORT_FRACT_ID:
6905       T = Context.UnsignedShortFractTy;
6906       break;
6907     case PREDEF_TYPE_UFRACT_ID:
6908       T = Context.UnsignedFractTy;
6909       break;
6910     case PREDEF_TYPE_ULONG_FRACT_ID:
6911       T = Context.UnsignedLongFractTy;
6912       break;
6913     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6914       T = Context.SatShortAccumTy;
6915       break;
6916     case PREDEF_TYPE_SAT_ACCUM_ID:
6917       T = Context.SatAccumTy;
6918       break;
6919     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6920       T = Context.SatLongAccumTy;
6921       break;
6922     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6923       T = Context.SatUnsignedShortAccumTy;
6924       break;
6925     case PREDEF_TYPE_SAT_UACCUM_ID:
6926       T = Context.SatUnsignedAccumTy;
6927       break;
6928     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6929       T = Context.SatUnsignedLongAccumTy;
6930       break;
6931     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6932       T = Context.SatShortFractTy;
6933       break;
6934     case PREDEF_TYPE_SAT_FRACT_ID:
6935       T = Context.SatFractTy;
6936       break;
6937     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6938       T = Context.SatLongFractTy;
6939       break;
6940     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6941       T = Context.SatUnsignedShortFractTy;
6942       break;
6943     case PREDEF_TYPE_SAT_UFRACT_ID:
6944       T = Context.SatUnsignedFractTy;
6945       break;
6946     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6947       T = Context.SatUnsignedLongFractTy;
6948       break;
6949     case PREDEF_TYPE_FLOAT16_ID:
6950       T = Context.Float16Ty;
6951       break;
6952     case PREDEF_TYPE_FLOAT128_ID:
6953       T = Context.Float128Ty;
6954       break;
6955     case PREDEF_TYPE_IBM128_ID:
6956       T = Context.Ibm128Ty;
6957       break;
6958     case PREDEF_TYPE_OVERLOAD_ID:
6959       T = Context.OverloadTy;
6960       break;
6961     case PREDEF_TYPE_BOUND_MEMBER:
6962       T = Context.BoundMemberTy;
6963       break;
6964     case PREDEF_TYPE_PSEUDO_OBJECT:
6965       T = Context.PseudoObjectTy;
6966       break;
6967     case PREDEF_TYPE_DEPENDENT_ID:
6968       T = Context.DependentTy;
6969       break;
6970     case PREDEF_TYPE_UNKNOWN_ANY:
6971       T = Context.UnknownAnyTy;
6972       break;
6973     case PREDEF_TYPE_NULLPTR_ID:
6974       T = Context.NullPtrTy;
6975       break;
6976     case PREDEF_TYPE_CHAR8_ID:
6977       T = Context.Char8Ty;
6978       break;
6979     case PREDEF_TYPE_CHAR16_ID:
6980       T = Context.Char16Ty;
6981       break;
6982     case PREDEF_TYPE_CHAR32_ID:
6983       T = Context.Char32Ty;
6984       break;
6985     case PREDEF_TYPE_OBJC_ID:
6986       T = Context.ObjCBuiltinIdTy;
6987       break;
6988     case PREDEF_TYPE_OBJC_CLASS:
6989       T = Context.ObjCBuiltinClassTy;
6990       break;
6991     case PREDEF_TYPE_OBJC_SEL:
6992       T = Context.ObjCBuiltinSelTy;
6993       break;
6994 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6995     case PREDEF_TYPE_##Id##_ID: \
6996       T = Context.SingletonId; \
6997       break;
6998 #include "clang/Basic/OpenCLImageTypes.def"
6999 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7000     case PREDEF_TYPE_##Id##_ID: \
7001       T = Context.Id##Ty; \
7002       break;
7003 #include "clang/Basic/OpenCLExtensionTypes.def"
7004     case PREDEF_TYPE_SAMPLER_ID:
7005       T = Context.OCLSamplerTy;
7006       break;
7007     case PREDEF_TYPE_EVENT_ID:
7008       T = Context.OCLEventTy;
7009       break;
7010     case PREDEF_TYPE_CLK_EVENT_ID:
7011       T = Context.OCLClkEventTy;
7012       break;
7013     case PREDEF_TYPE_QUEUE_ID:
7014       T = Context.OCLQueueTy;
7015       break;
7016     case PREDEF_TYPE_RESERVE_ID_ID:
7017       T = Context.OCLReserveIDTy;
7018       break;
7019     case PREDEF_TYPE_AUTO_DEDUCT:
7020       T = Context.getAutoDeductType();
7021       break;
7022     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7023       T = Context.getAutoRRefDeductType();
7024       break;
7025     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7026       T = Context.ARCUnbridgedCastTy;
7027       break;
7028     case PREDEF_TYPE_BUILTIN_FN:
7029       T = Context.BuiltinFnTy;
7030       break;
7031     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7032       T = Context.IncompleteMatrixIdxTy;
7033       break;
7034     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7035       T = Context.OMPArraySectionTy;
7036       break;
7037     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7038       T = Context.OMPArraySectionTy;
7039       break;
7040     case PREDEF_TYPE_OMP_ITERATOR:
7041       T = Context.OMPIteratorTy;
7042       break;
7043 #define SVE_TYPE(Name, Id, SingletonId) \
7044     case PREDEF_TYPE_##Id##_ID: \
7045       T = Context.SingletonId; \
7046       break;
7047 #include "clang/Basic/AArch64SVEACLETypes.def"
7048 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7049     case PREDEF_TYPE_##Id##_ID: \
7050       T = Context.Id##Ty; \
7051       break;
7052 #include "clang/Basic/PPCTypes.def"
7053 #define RVV_TYPE(Name, Id, SingletonId) \
7054     case PREDEF_TYPE_##Id##_ID: \
7055       T = Context.SingletonId; \
7056       break;
7057 #include "clang/Basic/RISCVVTypes.def"
7058     }
7059 
7060     assert(!T.isNull() && "Unknown predefined type");
7061     return T.withFastQualifiers(FastQuals);
7062   }
7063 
7064   Index -= NUM_PREDEF_TYPE_IDS;
7065   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7066   if (TypesLoaded[Index].isNull()) {
7067     TypesLoaded[Index] = readTypeRecord(Index);
7068     if (TypesLoaded[Index].isNull())
7069       return QualType();
7070 
7071     TypesLoaded[Index]->setFromAST();
7072     if (DeserializationListener)
7073       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7074                                         TypesLoaded[Index]);
7075   }
7076 
7077   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7078 }
7079 
7080 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7081   return GetType(getGlobalTypeID(F, LocalID));
7082 }
7083 
7084 serialization::TypeID
7085 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7086   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7087   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7088 
7089   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7090     return LocalID;
7091 
7092   if (!F.ModuleOffsetMap.empty())
7093     ReadModuleOffsetMap(F);
7094 
7095   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7096     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7097   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7098 
7099   unsigned GlobalIndex = LocalIndex + I->second;
7100   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7101 }
7102 
7103 TemplateArgumentLocInfo
7104 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7105   switch (Kind) {
7106   case TemplateArgument::Expression:
7107     return readExpr();
7108   case TemplateArgument::Type:
7109     return readTypeSourceInfo();
7110   case TemplateArgument::Template: {
7111     NestedNameSpecifierLoc QualifierLoc =
7112       readNestedNameSpecifierLoc();
7113     SourceLocation TemplateNameLoc = readSourceLocation();
7114     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7115                                    TemplateNameLoc, SourceLocation());
7116   }
7117   case TemplateArgument::TemplateExpansion: {
7118     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7119     SourceLocation TemplateNameLoc = readSourceLocation();
7120     SourceLocation EllipsisLoc = readSourceLocation();
7121     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7122                                    TemplateNameLoc, EllipsisLoc);
7123   }
7124   case TemplateArgument::Null:
7125   case TemplateArgument::Integral:
7126   case TemplateArgument::Declaration:
7127   case TemplateArgument::NullPtr:
7128   case TemplateArgument::Pack:
7129     // FIXME: Is this right?
7130     return TemplateArgumentLocInfo();
7131   }
7132   llvm_unreachable("unexpected template argument loc");
7133 }
7134 
7135 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7136   TemplateArgument Arg = readTemplateArgument();
7137 
7138   if (Arg.getKind() == TemplateArgument::Expression) {
7139     if (readBool()) // bool InfoHasSameExpr.
7140       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7141   }
7142   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7143 }
7144 
7145 const ASTTemplateArgumentListInfo *
7146 ASTRecordReader::readASTTemplateArgumentListInfo() {
7147   SourceLocation LAngleLoc = readSourceLocation();
7148   SourceLocation RAngleLoc = readSourceLocation();
7149   unsigned NumArgsAsWritten = readInt();
7150   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7151   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7152     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7153   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7154 }
7155 
7156 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7157   return GetDecl(ID);
7158 }
7159 
7160 void ASTReader::CompleteRedeclChain(const Decl *D) {
7161   if (NumCurrentElementsDeserializing) {
7162     // We arrange to not care about the complete redeclaration chain while we're
7163     // deserializing. Just remember that the AST has marked this one as complete
7164     // but that it's not actually complete yet, so we know we still need to
7165     // complete it later.
7166     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7167     return;
7168   }
7169 
7170   if (!D->getDeclContext()) {
7171     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7172     return;
7173   }
7174 
7175   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7176 
7177   // If this is a named declaration, complete it by looking it up
7178   // within its context.
7179   //
7180   // FIXME: Merging a function definition should merge
7181   // all mergeable entities within it.
7182   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7183       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7184     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7185       if (!getContext().getLangOpts().CPlusPlus &&
7186           isa<TranslationUnitDecl>(DC)) {
7187         // Outside of C++, we don't have a lookup table for the TU, so update
7188         // the identifier instead. (For C++ modules, we don't store decls
7189         // in the serialized identifier table, so we do the lookup in the TU.)
7190         auto *II = Name.getAsIdentifierInfo();
7191         assert(II && "non-identifier name in C?");
7192         if (II->isOutOfDate())
7193           updateOutOfDateIdentifier(*II);
7194       } else
7195         DC->lookup(Name);
7196     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7197       // Find all declarations of this kind from the relevant context.
7198       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7199         auto *DC = cast<DeclContext>(DCDecl);
7200         SmallVector<Decl*, 8> Decls;
7201         FindExternalLexicalDecls(
7202             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7203       }
7204     }
7205   }
7206 
7207   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7208     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7209   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7210     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7211   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7212     if (auto *Template = FD->getPrimaryTemplate())
7213       Template->LoadLazySpecializations();
7214   }
7215 }
7216 
7217 CXXCtorInitializer **
7218 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7219   RecordLocation Loc = getLocalBitOffset(Offset);
7220   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7221   SavedStreamPosition SavedPosition(Cursor);
7222   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7223     Error(std::move(Err));
7224     return nullptr;
7225   }
7226   ReadingKindTracker ReadingKind(Read_Decl, *this);
7227 
7228   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7229   if (!MaybeCode) {
7230     Error(MaybeCode.takeError());
7231     return nullptr;
7232   }
7233   unsigned Code = MaybeCode.get();
7234 
7235   ASTRecordReader Record(*this, *Loc.F);
7236   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7237   if (!MaybeRecCode) {
7238     Error(MaybeRecCode.takeError());
7239     return nullptr;
7240   }
7241   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7242     Error("malformed AST file: missing C++ ctor initializers");
7243     return nullptr;
7244   }
7245 
7246   return Record.readCXXCtorInitializers();
7247 }
7248 
7249 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7250   assert(ContextObj && "reading base specifiers with no AST context");
7251   ASTContext &Context = *ContextObj;
7252 
7253   RecordLocation Loc = getLocalBitOffset(Offset);
7254   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7255   SavedStreamPosition SavedPosition(Cursor);
7256   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7257     Error(std::move(Err));
7258     return nullptr;
7259   }
7260   ReadingKindTracker ReadingKind(Read_Decl, *this);
7261 
7262   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7263   if (!MaybeCode) {
7264     Error(MaybeCode.takeError());
7265     return nullptr;
7266   }
7267   unsigned Code = MaybeCode.get();
7268 
7269   ASTRecordReader Record(*this, *Loc.F);
7270   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7271   if (!MaybeRecCode) {
7272     Error(MaybeCode.takeError());
7273     return nullptr;
7274   }
7275   unsigned RecCode = MaybeRecCode.get();
7276 
7277   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7278     Error("malformed AST file: missing C++ base specifiers");
7279     return nullptr;
7280   }
7281 
7282   unsigned NumBases = Record.readInt();
7283   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7284   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7285   for (unsigned I = 0; I != NumBases; ++I)
7286     Bases[I] = Record.readCXXBaseSpecifier();
7287   return Bases;
7288 }
7289 
7290 serialization::DeclID
7291 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7292   if (LocalID < NUM_PREDEF_DECL_IDS)
7293     return LocalID;
7294 
7295   if (!F.ModuleOffsetMap.empty())
7296     ReadModuleOffsetMap(F);
7297 
7298   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7299     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7300   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7301 
7302   return LocalID + I->second;
7303 }
7304 
7305 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7306                                    ModuleFile &M) const {
7307   // Predefined decls aren't from any module.
7308   if (ID < NUM_PREDEF_DECL_IDS)
7309     return false;
7310 
7311   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7312          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7313 }
7314 
7315 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7316   if (!D->isFromASTFile())
7317     return nullptr;
7318   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7319   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7320   return I->second;
7321 }
7322 
7323 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7324   if (ID < NUM_PREDEF_DECL_IDS)
7325     return SourceLocation();
7326 
7327   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7328 
7329   if (Index > DeclsLoaded.size()) {
7330     Error("declaration ID out-of-range for AST file");
7331     return SourceLocation();
7332   }
7333 
7334   if (Decl *D = DeclsLoaded[Index])
7335     return D->getLocation();
7336 
7337   SourceLocation Loc;
7338   DeclCursorForID(ID, Loc);
7339   return Loc;
7340 }
7341 
7342 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7343   switch (ID) {
7344   case PREDEF_DECL_NULL_ID:
7345     return nullptr;
7346 
7347   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7348     return Context.getTranslationUnitDecl();
7349 
7350   case PREDEF_DECL_OBJC_ID_ID:
7351     return Context.getObjCIdDecl();
7352 
7353   case PREDEF_DECL_OBJC_SEL_ID:
7354     return Context.getObjCSelDecl();
7355 
7356   case PREDEF_DECL_OBJC_CLASS_ID:
7357     return Context.getObjCClassDecl();
7358 
7359   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7360     return Context.getObjCProtocolDecl();
7361 
7362   case PREDEF_DECL_INT_128_ID:
7363     return Context.getInt128Decl();
7364 
7365   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7366     return Context.getUInt128Decl();
7367 
7368   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7369     return Context.getObjCInstanceTypeDecl();
7370 
7371   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7372     return Context.getBuiltinVaListDecl();
7373 
7374   case PREDEF_DECL_VA_LIST_TAG:
7375     return Context.getVaListTagDecl();
7376 
7377   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7378     return Context.getBuiltinMSVaListDecl();
7379 
7380   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7381     return Context.getMSGuidTagDecl();
7382 
7383   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7384     return Context.getExternCContextDecl();
7385 
7386   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7387     return Context.getMakeIntegerSeqDecl();
7388 
7389   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7390     return Context.getCFConstantStringDecl();
7391 
7392   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7393     return Context.getCFConstantStringTagDecl();
7394 
7395   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7396     return Context.getTypePackElementDecl();
7397   }
7398   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7399 }
7400 
7401 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7402   assert(ContextObj && "reading decl with no AST context");
7403   if (ID < NUM_PREDEF_DECL_IDS) {
7404     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7405     if (D) {
7406       // Track that we have merged the declaration with ID \p ID into the
7407       // pre-existing predefined declaration \p D.
7408       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7409       if (Merged.empty())
7410         Merged.push_back(ID);
7411     }
7412     return D;
7413   }
7414 
7415   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7416 
7417   if (Index >= DeclsLoaded.size()) {
7418     assert(0 && "declaration ID out-of-range for AST file");
7419     Error("declaration ID out-of-range for AST file");
7420     return nullptr;
7421   }
7422 
7423   return DeclsLoaded[Index];
7424 }
7425 
7426 Decl *ASTReader::GetDecl(DeclID ID) {
7427   if (ID < NUM_PREDEF_DECL_IDS)
7428     return GetExistingDecl(ID);
7429 
7430   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7431 
7432   if (Index >= DeclsLoaded.size()) {
7433     assert(0 && "declaration ID out-of-range for AST file");
7434     Error("declaration ID out-of-range for AST file");
7435     return nullptr;
7436   }
7437 
7438   if (!DeclsLoaded[Index]) {
7439     ReadDeclRecord(ID);
7440     if (DeserializationListener)
7441       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7442   }
7443 
7444   return DeclsLoaded[Index];
7445 }
7446 
7447 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7448                                                   DeclID GlobalID) {
7449   if (GlobalID < NUM_PREDEF_DECL_IDS)
7450     return GlobalID;
7451 
7452   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7453   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7454   ModuleFile *Owner = I->second;
7455 
7456   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7457     = M.GlobalToLocalDeclIDs.find(Owner);
7458   if (Pos == M.GlobalToLocalDeclIDs.end())
7459     return 0;
7460 
7461   return GlobalID - Owner->BaseDeclID + Pos->second;
7462 }
7463 
7464 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7465                                             const RecordData &Record,
7466                                             unsigned &Idx) {
7467   if (Idx >= Record.size()) {
7468     Error("Corrupted AST file");
7469     return 0;
7470   }
7471 
7472   return getGlobalDeclID(F, Record[Idx++]);
7473 }
7474 
7475 /// Resolve the offset of a statement into a statement.
7476 ///
7477 /// This operation will read a new statement from the external
7478 /// source each time it is called, and is meant to be used via a
7479 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7480 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7481   // Switch case IDs are per Decl.
7482   ClearSwitchCaseIDs();
7483 
7484   // Offset here is a global offset across the entire chain.
7485   RecordLocation Loc = getLocalBitOffset(Offset);
7486   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7487     Error(std::move(Err));
7488     return nullptr;
7489   }
7490   assert(NumCurrentElementsDeserializing == 0 &&
7491          "should not be called while already deserializing");
7492   Deserializing D(this);
7493   return ReadStmtFromStream(*Loc.F);
7494 }
7495 
7496 void ASTReader::FindExternalLexicalDecls(
7497     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7498     SmallVectorImpl<Decl *> &Decls) {
7499   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7500 
7501   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7502     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7503     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7504       auto K = (Decl::Kind)+LexicalDecls[I];
7505       if (!IsKindWeWant(K))
7506         continue;
7507 
7508       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7509 
7510       // Don't add predefined declarations to the lexical context more
7511       // than once.
7512       if (ID < NUM_PREDEF_DECL_IDS) {
7513         if (PredefsVisited[ID])
7514           continue;
7515 
7516         PredefsVisited[ID] = true;
7517       }
7518 
7519       if (Decl *D = GetLocalDecl(*M, ID)) {
7520         assert(D->getKind() == K && "wrong kind for lexical decl");
7521         if (!DC->isDeclInLexicalTraversal(D))
7522           Decls.push_back(D);
7523       }
7524     }
7525   };
7526 
7527   if (isa<TranslationUnitDecl>(DC)) {
7528     for (auto Lexical : TULexicalDecls)
7529       Visit(Lexical.first, Lexical.second);
7530   } else {
7531     auto I = LexicalDecls.find(DC);
7532     if (I != LexicalDecls.end())
7533       Visit(I->second.first, I->second.second);
7534   }
7535 
7536   ++NumLexicalDeclContextsRead;
7537 }
7538 
7539 namespace {
7540 
7541 class DeclIDComp {
7542   ASTReader &Reader;
7543   ModuleFile &Mod;
7544 
7545 public:
7546   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7547 
7548   bool operator()(LocalDeclID L, LocalDeclID R) const {
7549     SourceLocation LHS = getLocation(L);
7550     SourceLocation RHS = getLocation(R);
7551     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7552   }
7553 
7554   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7555     SourceLocation RHS = getLocation(R);
7556     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7557   }
7558 
7559   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7560     SourceLocation LHS = getLocation(L);
7561     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7562   }
7563 
7564   SourceLocation getLocation(LocalDeclID ID) const {
7565     return Reader.getSourceManager().getFileLoc(
7566             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7567   }
7568 };
7569 
7570 } // namespace
7571 
7572 void ASTReader::FindFileRegionDecls(FileID File,
7573                                     unsigned Offset, unsigned Length,
7574                                     SmallVectorImpl<Decl *> &Decls) {
7575   SourceManager &SM = getSourceManager();
7576 
7577   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7578   if (I == FileDeclIDs.end())
7579     return;
7580 
7581   FileDeclsInfo &DInfo = I->second;
7582   if (DInfo.Decls.empty())
7583     return;
7584 
7585   SourceLocation
7586     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7587   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7588 
7589   DeclIDComp DIDComp(*this, *DInfo.Mod);
7590   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7591       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7592   if (BeginIt != DInfo.Decls.begin())
7593     --BeginIt;
7594 
7595   // If we are pointing at a top-level decl inside an objc container, we need
7596   // to backtrack until we find it otherwise we will fail to report that the
7597   // region overlaps with an objc container.
7598   while (BeginIt != DInfo.Decls.begin() &&
7599          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7600              ->isTopLevelDeclInObjCContainer())
7601     --BeginIt;
7602 
7603   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7604       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7605   if (EndIt != DInfo.Decls.end())
7606     ++EndIt;
7607 
7608   for (ArrayRef<serialization::LocalDeclID>::iterator
7609          DIt = BeginIt; DIt != EndIt; ++DIt)
7610     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7611 }
7612 
7613 bool
7614 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7615                                           DeclarationName Name) {
7616   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7617          "DeclContext has no visible decls in storage");
7618   if (!Name)
7619     return false;
7620 
7621   auto It = Lookups.find(DC);
7622   if (It == Lookups.end())
7623     return false;
7624 
7625   Deserializing LookupResults(this);
7626 
7627   // Load the list of declarations.
7628   SmallVector<NamedDecl *, 64> Decls;
7629   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7630   for (DeclID ID : It->second.Table.find(Name)) {
7631     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7632     if (ND->getDeclName() == Name && Found.insert(ND).second)
7633       Decls.push_back(ND);
7634   }
7635 
7636   ++NumVisibleDeclContextsRead;
7637   SetExternalVisibleDeclsForName(DC, Name, Decls);
7638   return !Decls.empty();
7639 }
7640 
7641 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7642   if (!DC->hasExternalVisibleStorage())
7643     return;
7644 
7645   auto It = Lookups.find(DC);
7646   assert(It != Lookups.end() &&
7647          "have external visible storage but no lookup tables");
7648 
7649   DeclsMap Decls;
7650 
7651   for (DeclID ID : It->second.Table.findAll()) {
7652     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7653     Decls[ND->getDeclName()].push_back(ND);
7654   }
7655 
7656   ++NumVisibleDeclContextsRead;
7657 
7658   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7659     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7660   }
7661   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7662 }
7663 
7664 const serialization::reader::DeclContextLookupTable *
7665 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7666   auto I = Lookups.find(Primary);
7667   return I == Lookups.end() ? nullptr : &I->second;
7668 }
7669 
7670 /// Under non-PCH compilation the consumer receives the objc methods
7671 /// before receiving the implementation, and codegen depends on this.
7672 /// We simulate this by deserializing and passing to consumer the methods of the
7673 /// implementation before passing the deserialized implementation decl.
7674 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7675                                        ASTConsumer *Consumer) {
7676   assert(ImplD && Consumer);
7677 
7678   for (auto *I : ImplD->methods())
7679     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7680 
7681   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7682 }
7683 
7684 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7685   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7686     PassObjCImplDeclToConsumer(ImplD, Consumer);
7687   else
7688     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7689 }
7690 
7691 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7692   this->Consumer = Consumer;
7693 
7694   if (Consumer)
7695     PassInterestingDeclsToConsumer();
7696 
7697   if (DeserializationListener)
7698     DeserializationListener->ReaderInitialized(this);
7699 }
7700 
7701 void ASTReader::PrintStats() {
7702   std::fprintf(stderr, "*** AST File Statistics:\n");
7703 
7704   unsigned NumTypesLoaded =
7705       TypesLoaded.size() - llvm::count(TypesLoaded, QualType());
7706   unsigned NumDeclsLoaded =
7707       DeclsLoaded.size() - llvm::count(DeclsLoaded, (Decl *)nullptr);
7708   unsigned NumIdentifiersLoaded =
7709       IdentifiersLoaded.size() -
7710       llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);
7711   unsigned NumMacrosLoaded =
7712       MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr);
7713   unsigned NumSelectorsLoaded =
7714       SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector());
7715 
7716   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7717     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7718                  NumSLocEntriesRead, TotalNumSLocEntries,
7719                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7720   if (!TypesLoaded.empty())
7721     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7722                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7723                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7724   if (!DeclsLoaded.empty())
7725     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7726                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7727                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7728   if (!IdentifiersLoaded.empty())
7729     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7730                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7731                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7732   if (!MacrosLoaded.empty())
7733     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7734                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7735                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7736   if (!SelectorsLoaded.empty())
7737     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7738                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7739                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7740   if (TotalNumStatements)
7741     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7742                  NumStatementsRead, TotalNumStatements,
7743                  ((float)NumStatementsRead/TotalNumStatements * 100));
7744   if (TotalNumMacros)
7745     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7746                  NumMacrosRead, TotalNumMacros,
7747                  ((float)NumMacrosRead/TotalNumMacros * 100));
7748   if (TotalLexicalDeclContexts)
7749     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7750                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7751                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7752                   * 100));
7753   if (TotalVisibleDeclContexts)
7754     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7755                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7756                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7757                   * 100));
7758   if (TotalNumMethodPoolEntries)
7759     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7760                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7761                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7762                   * 100));
7763   if (NumMethodPoolLookups)
7764     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7765                  NumMethodPoolHits, NumMethodPoolLookups,
7766                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7767   if (NumMethodPoolTableLookups)
7768     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7769                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7770                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7771                   * 100.0));
7772   if (NumIdentifierLookupHits)
7773     std::fprintf(stderr,
7774                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7775                  NumIdentifierLookupHits, NumIdentifierLookups,
7776                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7777 
7778   if (GlobalIndex) {
7779     std::fprintf(stderr, "\n");
7780     GlobalIndex->printStats();
7781   }
7782 
7783   std::fprintf(stderr, "\n");
7784   dump();
7785   std::fprintf(stderr, "\n");
7786 }
7787 
7788 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7789 LLVM_DUMP_METHOD static void
7790 dumpModuleIDMap(StringRef Name,
7791                 const ContinuousRangeMap<Key, ModuleFile *,
7792                                          InitialCapacity> &Map) {
7793   if (Map.begin() == Map.end())
7794     return;
7795 
7796   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7797 
7798   llvm::errs() << Name << ":\n";
7799   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7800        I != IEnd; ++I) {
7801     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7802       << "\n";
7803   }
7804 }
7805 
7806 LLVM_DUMP_METHOD void ASTReader::dump() {
7807   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7808   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7809   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7810   dumpModuleIDMap("Global type map", GlobalTypeMap);
7811   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7812   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7813   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7814   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7815   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7816   dumpModuleIDMap("Global preprocessed entity map",
7817                   GlobalPreprocessedEntityMap);
7818 
7819   llvm::errs() << "\n*** PCH/Modules Loaded:";
7820   for (ModuleFile &M : ModuleMgr)
7821     M.dump();
7822 }
7823 
7824 /// Return the amount of memory used by memory buffers, breaking down
7825 /// by heap-backed versus mmap'ed memory.
7826 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7827   for (ModuleFile &I : ModuleMgr) {
7828     if (llvm::MemoryBuffer *buf = I.Buffer) {
7829       size_t bytes = buf->getBufferSize();
7830       switch (buf->getBufferKind()) {
7831         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7832           sizes.malloc_bytes += bytes;
7833           break;
7834         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7835           sizes.mmap_bytes += bytes;
7836           break;
7837       }
7838     }
7839   }
7840 }
7841 
7842 void ASTReader::InitializeSema(Sema &S) {
7843   SemaObj = &S;
7844   S.addExternalSource(this);
7845 
7846   // Makes sure any declarations that were deserialized "too early"
7847   // still get added to the identifier's declaration chains.
7848   for (uint64_t ID : PreloadedDeclIDs) {
7849     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7850     pushExternalDeclIntoScope(D, D->getDeclName());
7851   }
7852   PreloadedDeclIDs.clear();
7853 
7854   // FIXME: What happens if these are changed by a module import?
7855   if (!FPPragmaOptions.empty()) {
7856     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7857     FPOptionsOverride NewOverrides =
7858         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7859     SemaObj->CurFPFeatures =
7860         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7861   }
7862 
7863   SemaObj->OpenCLFeatures = OpenCLExtensions;
7864 
7865   UpdateSema();
7866 }
7867 
7868 void ASTReader::UpdateSema() {
7869   assert(SemaObj && "no Sema to update");
7870 
7871   // Load the offsets of the declarations that Sema references.
7872   // They will be lazily deserialized when needed.
7873   if (!SemaDeclRefs.empty()) {
7874     assert(SemaDeclRefs.size() % 3 == 0);
7875     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7876       if (!SemaObj->StdNamespace)
7877         SemaObj->StdNamespace = SemaDeclRefs[I];
7878       if (!SemaObj->StdBadAlloc)
7879         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7880       if (!SemaObj->StdAlignValT)
7881         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7882     }
7883     SemaDeclRefs.clear();
7884   }
7885 
7886   // Update the state of pragmas. Use the same API as if we had encountered the
7887   // pragma in the source.
7888   if(OptimizeOffPragmaLocation.isValid())
7889     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7890   if (PragmaMSStructState != -1)
7891     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7892   if (PointersToMembersPragmaLocation.isValid()) {
7893     SemaObj->ActOnPragmaMSPointersToMembers(
7894         (LangOptions::PragmaMSPointersToMembersKind)
7895             PragmaMSPointersToMembersState,
7896         PointersToMembersPragmaLocation);
7897   }
7898   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7899 
7900   if (PragmaAlignPackCurrentValue) {
7901     // The bottom of the stack might have a default value. It must be adjusted
7902     // to the current value to ensure that the packing state is preserved after
7903     // popping entries that were included/imported from a PCH/module.
7904     bool DropFirst = false;
7905     if (!PragmaAlignPackStack.empty() &&
7906         PragmaAlignPackStack.front().Location.isInvalid()) {
7907       assert(PragmaAlignPackStack.front().Value ==
7908                  SemaObj->AlignPackStack.DefaultValue &&
7909              "Expected a default alignment value");
7910       SemaObj->AlignPackStack.Stack.emplace_back(
7911           PragmaAlignPackStack.front().SlotLabel,
7912           SemaObj->AlignPackStack.CurrentValue,
7913           SemaObj->AlignPackStack.CurrentPragmaLocation,
7914           PragmaAlignPackStack.front().PushLocation);
7915       DropFirst = true;
7916     }
7917     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7918                                  .drop_front(DropFirst ? 1 : 0)) {
7919       SemaObj->AlignPackStack.Stack.emplace_back(
7920           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7921     }
7922     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7923       assert(*PragmaAlignPackCurrentValue ==
7924                  SemaObj->AlignPackStack.DefaultValue &&
7925              "Expected a default align and pack value");
7926       // Keep the current values.
7927     } else {
7928       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7929       SemaObj->AlignPackStack.CurrentPragmaLocation =
7930           PragmaAlignPackCurrentLocation;
7931     }
7932   }
7933   if (FpPragmaCurrentValue) {
7934     // The bottom of the stack might have a default value. It must be adjusted
7935     // to the current value to ensure that fp-pragma state is preserved after
7936     // popping entries that were included/imported from a PCH/module.
7937     bool DropFirst = false;
7938     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7939       assert(FpPragmaStack.front().Value ==
7940                  SemaObj->FpPragmaStack.DefaultValue &&
7941              "Expected a default pragma float_control value");
7942       SemaObj->FpPragmaStack.Stack.emplace_back(
7943           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7944           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7945           FpPragmaStack.front().PushLocation);
7946       DropFirst = true;
7947     }
7948     for (const auto &Entry :
7949          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7950       SemaObj->FpPragmaStack.Stack.emplace_back(
7951           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7952     if (FpPragmaCurrentLocation.isInvalid()) {
7953       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7954              "Expected a default pragma float_control value");
7955       // Keep the current values.
7956     } else {
7957       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7958       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7959     }
7960   }
7961 
7962   // For non-modular AST files, restore visiblity of modules.
7963   for (auto &Import : ImportedModules) {
7964     if (Import.ImportLoc.isInvalid())
7965       continue;
7966     if (Module *Imported = getSubmodule(Import.ID)) {
7967       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7968     }
7969   }
7970 }
7971 
7972 IdentifierInfo *ASTReader::get(StringRef Name) {
7973   // Note that we are loading an identifier.
7974   Deserializing AnIdentifier(this);
7975 
7976   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7977                                   NumIdentifierLookups,
7978                                   NumIdentifierLookupHits);
7979 
7980   // We don't need to do identifier table lookups in C++ modules (we preload
7981   // all interesting declarations, and don't need to use the scope for name
7982   // lookups). Perform the lookup in PCH files, though, since we don't build
7983   // a complete initial identifier table if we're carrying on from a PCH.
7984   if (PP.getLangOpts().CPlusPlus) {
7985     for (auto F : ModuleMgr.pch_modules())
7986       if (Visitor(*F))
7987         break;
7988   } else {
7989     // If there is a global index, look there first to determine which modules
7990     // provably do not have any results for this identifier.
7991     GlobalModuleIndex::HitSet Hits;
7992     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7993     if (!loadGlobalIndex()) {
7994       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7995         HitsPtr = &Hits;
7996       }
7997     }
7998 
7999     ModuleMgr.visit(Visitor, HitsPtr);
8000   }
8001 
8002   IdentifierInfo *II = Visitor.getIdentifierInfo();
8003   markIdentifierUpToDate(II);
8004   return II;
8005 }
8006 
8007 namespace clang {
8008 
8009   /// An identifier-lookup iterator that enumerates all of the
8010   /// identifiers stored within a set of AST files.
8011   class ASTIdentifierIterator : public IdentifierIterator {
8012     /// The AST reader whose identifiers are being enumerated.
8013     const ASTReader &Reader;
8014 
8015     /// The current index into the chain of AST files stored in
8016     /// the AST reader.
8017     unsigned Index;
8018 
8019     /// The current position within the identifier lookup table
8020     /// of the current AST file.
8021     ASTIdentifierLookupTable::key_iterator Current;
8022 
8023     /// The end position within the identifier lookup table of
8024     /// the current AST file.
8025     ASTIdentifierLookupTable::key_iterator End;
8026 
8027     /// Whether to skip any modules in the ASTReader.
8028     bool SkipModules;
8029 
8030   public:
8031     explicit ASTIdentifierIterator(const ASTReader &Reader,
8032                                    bool SkipModules = false);
8033 
8034     StringRef Next() override;
8035   };
8036 
8037 } // namespace clang
8038 
8039 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8040                                              bool SkipModules)
8041     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8042 }
8043 
8044 StringRef ASTIdentifierIterator::Next() {
8045   while (Current == End) {
8046     // If we have exhausted all of our AST files, we're done.
8047     if (Index == 0)
8048       return StringRef();
8049 
8050     --Index;
8051     ModuleFile &F = Reader.ModuleMgr[Index];
8052     if (SkipModules && F.isModule())
8053       continue;
8054 
8055     ASTIdentifierLookupTable *IdTable =
8056         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8057     Current = IdTable->key_begin();
8058     End = IdTable->key_end();
8059   }
8060 
8061   // We have any identifiers remaining in the current AST file; return
8062   // the next one.
8063   StringRef Result = *Current;
8064   ++Current;
8065   return Result;
8066 }
8067 
8068 namespace {
8069 
8070 /// A utility for appending two IdentifierIterators.
8071 class ChainedIdentifierIterator : public IdentifierIterator {
8072   std::unique_ptr<IdentifierIterator> Current;
8073   std::unique_ptr<IdentifierIterator> Queued;
8074 
8075 public:
8076   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8077                             std::unique_ptr<IdentifierIterator> Second)
8078       : Current(std::move(First)), Queued(std::move(Second)) {}
8079 
8080   StringRef Next() override {
8081     if (!Current)
8082       return StringRef();
8083 
8084     StringRef result = Current->Next();
8085     if (!result.empty())
8086       return result;
8087 
8088     // Try the queued iterator, which may itself be empty.
8089     Current.reset();
8090     std::swap(Current, Queued);
8091     return Next();
8092   }
8093 };
8094 
8095 } // namespace
8096 
8097 IdentifierIterator *ASTReader::getIdentifiers() {
8098   if (!loadGlobalIndex()) {
8099     std::unique_ptr<IdentifierIterator> ReaderIter(
8100         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8101     std::unique_ptr<IdentifierIterator> ModulesIter(
8102         GlobalIndex->createIdentifierIterator());
8103     return new ChainedIdentifierIterator(std::move(ReaderIter),
8104                                          std::move(ModulesIter));
8105   }
8106 
8107   return new ASTIdentifierIterator(*this);
8108 }
8109 
8110 namespace clang {
8111 namespace serialization {
8112 
8113   class ReadMethodPoolVisitor {
8114     ASTReader &Reader;
8115     Selector Sel;
8116     unsigned PriorGeneration;
8117     unsigned InstanceBits = 0;
8118     unsigned FactoryBits = 0;
8119     bool InstanceHasMoreThanOneDecl = false;
8120     bool FactoryHasMoreThanOneDecl = false;
8121     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8122     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8123 
8124   public:
8125     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8126                           unsigned PriorGeneration)
8127         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8128 
8129     bool operator()(ModuleFile &M) {
8130       if (!M.SelectorLookupTable)
8131         return false;
8132 
8133       // If we've already searched this module file, skip it now.
8134       if (M.Generation <= PriorGeneration)
8135         return true;
8136 
8137       ++Reader.NumMethodPoolTableLookups;
8138       ASTSelectorLookupTable *PoolTable
8139         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8140       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8141       if (Pos == PoolTable->end())
8142         return false;
8143 
8144       ++Reader.NumMethodPoolTableHits;
8145       ++Reader.NumSelectorsRead;
8146       // FIXME: Not quite happy with the statistics here. We probably should
8147       // disable this tracking when called via LoadSelector.
8148       // Also, should entries without methods count as misses?
8149       ++Reader.NumMethodPoolEntriesRead;
8150       ASTSelectorLookupTrait::data_type Data = *Pos;
8151       if (Reader.DeserializationListener)
8152         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8153 
8154       // Append methods in the reverse order, so that later we can process them
8155       // in the order they appear in the source code by iterating through
8156       // the vector in the reverse order.
8157       InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend());
8158       FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend());
8159       InstanceBits = Data.InstanceBits;
8160       FactoryBits = Data.FactoryBits;
8161       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8162       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8163       return false;
8164     }
8165 
8166     /// Retrieve the instance methods found by this visitor.
8167     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8168       return InstanceMethods;
8169     }
8170 
8171     /// Retrieve the instance methods found by this visitor.
8172     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8173       return FactoryMethods;
8174     }
8175 
8176     unsigned getInstanceBits() const { return InstanceBits; }
8177     unsigned getFactoryBits() const { return FactoryBits; }
8178 
8179     bool instanceHasMoreThanOneDecl() const {
8180       return InstanceHasMoreThanOneDecl;
8181     }
8182 
8183     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8184   };
8185 
8186 } // namespace serialization
8187 } // namespace clang
8188 
8189 /// Add the given set of methods to the method list.
8190 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8191                              ObjCMethodList &List) {
8192   for (auto I = Methods.rbegin(), E = Methods.rend(); I != E; ++I)
8193     S.addMethodToGlobalList(&List, *I);
8194 }
8195 
8196 void ASTReader::ReadMethodPool(Selector Sel) {
8197   // Get the selector generation and update it to the current generation.
8198   unsigned &Generation = SelectorGeneration[Sel];
8199   unsigned PriorGeneration = Generation;
8200   Generation = getGeneration();
8201   SelectorOutOfDate[Sel] = false;
8202 
8203   // Search for methods defined with this selector.
8204   ++NumMethodPoolLookups;
8205   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8206   ModuleMgr.visit(Visitor);
8207 
8208   if (Visitor.getInstanceMethods().empty() &&
8209       Visitor.getFactoryMethods().empty())
8210     return;
8211 
8212   ++NumMethodPoolHits;
8213 
8214   if (!getSema())
8215     return;
8216 
8217   Sema &S = *getSema();
8218   Sema::GlobalMethodPool::iterator Pos =
8219       S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists()))
8220           .first;
8221 
8222   Pos->second.first.setBits(Visitor.getInstanceBits());
8223   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8224   Pos->second.second.setBits(Visitor.getFactoryBits());
8225   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8226 
8227   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8228   // when building a module we keep every method individually and may need to
8229   // update hasMoreThanOneDecl as we add the methods.
8230   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8231   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8232 }
8233 
8234 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8235   if (SelectorOutOfDate[Sel])
8236     ReadMethodPool(Sel);
8237 }
8238 
8239 void ASTReader::ReadKnownNamespaces(
8240                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8241   Namespaces.clear();
8242 
8243   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8244     if (NamespaceDecl *Namespace
8245                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8246       Namespaces.push_back(Namespace);
8247   }
8248 }
8249 
8250 void ASTReader::ReadUndefinedButUsed(
8251     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8252   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8253     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8254     SourceLocation Loc =
8255         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8256     Undefined.insert(std::make_pair(D, Loc));
8257   }
8258 }
8259 
8260 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8261     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8262                                                      Exprs) {
8263   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8264     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8265     uint64_t Count = DelayedDeleteExprs[Idx++];
8266     for (uint64_t C = 0; C < Count; ++C) {
8267       SourceLocation DeleteLoc =
8268           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8269       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8270       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8271     }
8272   }
8273 }
8274 
8275 void ASTReader::ReadTentativeDefinitions(
8276                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8277   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8278     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8279     if (Var)
8280       TentativeDefs.push_back(Var);
8281   }
8282   TentativeDefinitions.clear();
8283 }
8284 
8285 void ASTReader::ReadUnusedFileScopedDecls(
8286                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8287   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8288     DeclaratorDecl *D
8289       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8290     if (D)
8291       Decls.push_back(D);
8292   }
8293   UnusedFileScopedDecls.clear();
8294 }
8295 
8296 void ASTReader::ReadDelegatingConstructors(
8297                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8298   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8299     CXXConstructorDecl *D
8300       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8301     if (D)
8302       Decls.push_back(D);
8303   }
8304   DelegatingCtorDecls.clear();
8305 }
8306 
8307 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8308   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8309     TypedefNameDecl *D
8310       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8311     if (D)
8312       Decls.push_back(D);
8313   }
8314   ExtVectorDecls.clear();
8315 }
8316 
8317 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8318     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8319   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8320        ++I) {
8321     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8322         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8323     if (D)
8324       Decls.insert(D);
8325   }
8326   UnusedLocalTypedefNameCandidates.clear();
8327 }
8328 
8329 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8330     llvm::SmallSetVector<Decl *, 4> &Decls) {
8331   for (auto I : DeclsToCheckForDeferredDiags) {
8332     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8333     if (D)
8334       Decls.insert(D);
8335   }
8336   DeclsToCheckForDeferredDiags.clear();
8337 }
8338 
8339 void ASTReader::ReadReferencedSelectors(
8340        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8341   if (ReferencedSelectorsData.empty())
8342     return;
8343 
8344   // If there are @selector references added them to its pool. This is for
8345   // implementation of -Wselector.
8346   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8347   unsigned I = 0;
8348   while (I < DataSize) {
8349     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8350     SourceLocation SelLoc
8351       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8352     Sels.push_back(std::make_pair(Sel, SelLoc));
8353   }
8354   ReferencedSelectorsData.clear();
8355 }
8356 
8357 void ASTReader::ReadWeakUndeclaredIdentifiers(
8358        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8359   if (WeakUndeclaredIdentifiers.empty())
8360     return;
8361 
8362   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8363     IdentifierInfo *WeakId
8364       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8365     IdentifierInfo *AliasId
8366       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8367     SourceLocation Loc
8368       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8369     bool Used = WeakUndeclaredIdentifiers[I++];
8370     WeakInfo WI(AliasId, Loc);
8371     WI.setUsed(Used);
8372     WeakIDs.push_back(std::make_pair(WeakId, WI));
8373   }
8374   WeakUndeclaredIdentifiers.clear();
8375 }
8376 
8377 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8378   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8379     ExternalVTableUse VT;
8380     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8381     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8382     VT.DefinitionRequired = VTableUses[Idx++];
8383     VTables.push_back(VT);
8384   }
8385 
8386   VTableUses.clear();
8387 }
8388 
8389 void ASTReader::ReadPendingInstantiations(
8390        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8391   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8392     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8393     SourceLocation Loc
8394       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8395 
8396     Pending.push_back(std::make_pair(D, Loc));
8397   }
8398   PendingInstantiations.clear();
8399 }
8400 
8401 void ASTReader::ReadLateParsedTemplates(
8402     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8403         &LPTMap) {
8404   for (auto &LPT : LateParsedTemplates) {
8405     ModuleFile *FMod = LPT.first;
8406     RecordDataImpl &LateParsed = LPT.second;
8407     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8408          /* In loop */) {
8409       FunctionDecl *FD =
8410           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8411 
8412       auto LT = std::make_unique<LateParsedTemplate>();
8413       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8414 
8415       ModuleFile *F = getOwningModuleFile(LT->D);
8416       assert(F && "No module");
8417 
8418       unsigned TokN = LateParsed[Idx++];
8419       LT->Toks.reserve(TokN);
8420       for (unsigned T = 0; T < TokN; ++T)
8421         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8422 
8423       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8424     }
8425   }
8426 
8427   LateParsedTemplates.clear();
8428 }
8429 
8430 void ASTReader::LoadSelector(Selector Sel) {
8431   // It would be complicated to avoid reading the methods anyway. So don't.
8432   ReadMethodPool(Sel);
8433 }
8434 
8435 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8436   assert(ID && "Non-zero identifier ID required");
8437   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8438   IdentifiersLoaded[ID - 1] = II;
8439   if (DeserializationListener)
8440     DeserializationListener->IdentifierRead(ID, II);
8441 }
8442 
8443 /// Set the globally-visible declarations associated with the given
8444 /// identifier.
8445 ///
8446 /// If the AST reader is currently in a state where the given declaration IDs
8447 /// cannot safely be resolved, they are queued until it is safe to resolve
8448 /// them.
8449 ///
8450 /// \param II an IdentifierInfo that refers to one or more globally-visible
8451 /// declarations.
8452 ///
8453 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8454 /// visible at global scope.
8455 ///
8456 /// \param Decls if non-null, this vector will be populated with the set of
8457 /// deserialized declarations. These declarations will not be pushed into
8458 /// scope.
8459 void
8460 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8461                               const SmallVectorImpl<uint32_t> &DeclIDs,
8462                                    SmallVectorImpl<Decl *> *Decls) {
8463   if (NumCurrentElementsDeserializing && !Decls) {
8464     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8465     return;
8466   }
8467 
8468   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8469     if (!SemaObj) {
8470       // Queue this declaration so that it will be added to the
8471       // translation unit scope and identifier's declaration chain
8472       // once a Sema object is known.
8473       PreloadedDeclIDs.push_back(DeclIDs[I]);
8474       continue;
8475     }
8476 
8477     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8478 
8479     // If we're simply supposed to record the declarations, do so now.
8480     if (Decls) {
8481       Decls->push_back(D);
8482       continue;
8483     }
8484 
8485     // Introduce this declaration into the translation-unit scope
8486     // and add it to the declaration chain for this identifier, so
8487     // that (unqualified) name lookup will find it.
8488     pushExternalDeclIntoScope(D, II);
8489   }
8490 }
8491 
8492 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8493   if (ID == 0)
8494     return nullptr;
8495 
8496   if (IdentifiersLoaded.empty()) {
8497     Error("no identifier table in AST file");
8498     return nullptr;
8499   }
8500 
8501   ID -= 1;
8502   if (!IdentifiersLoaded[ID]) {
8503     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8504     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8505     ModuleFile *M = I->second;
8506     unsigned Index = ID - M->BaseIdentifierID;
8507     const unsigned char *Data =
8508         M->IdentifierTableData + M->IdentifierOffsets[Index];
8509 
8510     ASTIdentifierLookupTrait Trait(*this, *M);
8511     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8512     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8513     auto &II = PP.getIdentifierTable().get(Key);
8514     IdentifiersLoaded[ID] = &II;
8515     markIdentifierFromAST(*this,  II);
8516     if (DeserializationListener)
8517       DeserializationListener->IdentifierRead(ID + 1, &II);
8518   }
8519 
8520   return IdentifiersLoaded[ID];
8521 }
8522 
8523 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8524   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8525 }
8526 
8527 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8528   if (LocalID < NUM_PREDEF_IDENT_IDS)
8529     return LocalID;
8530 
8531   if (!M.ModuleOffsetMap.empty())
8532     ReadModuleOffsetMap(M);
8533 
8534   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8535     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8536   assert(I != M.IdentifierRemap.end()
8537          && "Invalid index into identifier index remap");
8538 
8539   return LocalID + I->second;
8540 }
8541 
8542 MacroInfo *ASTReader::getMacro(MacroID ID) {
8543   if (ID == 0)
8544     return nullptr;
8545 
8546   if (MacrosLoaded.empty()) {
8547     Error("no macro table in AST file");
8548     return nullptr;
8549   }
8550 
8551   ID -= NUM_PREDEF_MACRO_IDS;
8552   if (!MacrosLoaded[ID]) {
8553     GlobalMacroMapType::iterator I
8554       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8555     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8556     ModuleFile *M = I->second;
8557     unsigned Index = ID - M->BaseMacroID;
8558     MacrosLoaded[ID] =
8559         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8560 
8561     if (DeserializationListener)
8562       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8563                                          MacrosLoaded[ID]);
8564   }
8565 
8566   return MacrosLoaded[ID];
8567 }
8568 
8569 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8570   if (LocalID < NUM_PREDEF_MACRO_IDS)
8571     return LocalID;
8572 
8573   if (!M.ModuleOffsetMap.empty())
8574     ReadModuleOffsetMap(M);
8575 
8576   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8577     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8578   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8579 
8580   return LocalID + I->second;
8581 }
8582 
8583 serialization::SubmoduleID
8584 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8585   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8586     return LocalID;
8587 
8588   if (!M.ModuleOffsetMap.empty())
8589     ReadModuleOffsetMap(M);
8590 
8591   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8592     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8593   assert(I != M.SubmoduleRemap.end()
8594          && "Invalid index into submodule index remap");
8595 
8596   return LocalID + I->second;
8597 }
8598 
8599 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8600   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8601     assert(GlobalID == 0 && "Unhandled global submodule ID");
8602     return nullptr;
8603   }
8604 
8605   if (GlobalID > SubmodulesLoaded.size()) {
8606     Error("submodule ID out of range in AST file");
8607     return nullptr;
8608   }
8609 
8610   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8611 }
8612 
8613 Module *ASTReader::getModule(unsigned ID) {
8614   return getSubmodule(ID);
8615 }
8616 
8617 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8618   if (ID & 1) {
8619     // It's a module, look it up by submodule ID.
8620     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8621     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8622   } else {
8623     // It's a prefix (preamble, PCH, ...). Look it up by index.
8624     unsigned IndexFromEnd = ID >> 1;
8625     assert(IndexFromEnd && "got reference to unknown module file");
8626     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8627   }
8628 }
8629 
8630 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8631   if (!F)
8632     return 1;
8633 
8634   // For a file representing a module, use the submodule ID of the top-level
8635   // module as the file ID. For any other kind of file, the number of such
8636   // files loaded beforehand will be the same on reload.
8637   // FIXME: Is this true even if we have an explicit module file and a PCH?
8638   if (F->isModule())
8639     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8640 
8641   auto PCHModules = getModuleManager().pch_modules();
8642   auto I = llvm::find(PCHModules, F);
8643   assert(I != PCHModules.end() && "emitting reference to unknown file");
8644   return (I - PCHModules.end()) << 1;
8645 }
8646 
8647 llvm::Optional<ASTSourceDescriptor>
8648 ASTReader::getSourceDescriptor(unsigned ID) {
8649   if (Module *M = getSubmodule(ID))
8650     return ASTSourceDescriptor(*M);
8651 
8652   // If there is only a single PCH, return it instead.
8653   // Chained PCH are not supported.
8654   const auto &PCHChain = ModuleMgr.pch_modules();
8655   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8656     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8657     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8658     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8659     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8660                                MF.Signature);
8661   }
8662   return None;
8663 }
8664 
8665 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8666   auto I = DefinitionSource.find(FD);
8667   if (I == DefinitionSource.end())
8668     return EK_ReplyHazy;
8669   return I->second ? EK_Never : EK_Always;
8670 }
8671 
8672 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8673   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8674 }
8675 
8676 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8677   if (ID == 0)
8678     return Selector();
8679 
8680   if (ID > SelectorsLoaded.size()) {
8681     Error("selector ID out of range in AST file");
8682     return Selector();
8683   }
8684 
8685   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8686     // Load this selector from the selector table.
8687     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8688     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8689     ModuleFile &M = *I->second;
8690     ASTSelectorLookupTrait Trait(*this, M);
8691     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8692     SelectorsLoaded[ID - 1] =
8693       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8694     if (DeserializationListener)
8695       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8696   }
8697 
8698   return SelectorsLoaded[ID - 1];
8699 }
8700 
8701 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8702   return DecodeSelector(ID);
8703 }
8704 
8705 uint32_t ASTReader::GetNumExternalSelectors() {
8706   // ID 0 (the null selector) is considered an external selector.
8707   return getTotalNumSelectors() + 1;
8708 }
8709 
8710 serialization::SelectorID
8711 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8712   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8713     return LocalID;
8714 
8715   if (!M.ModuleOffsetMap.empty())
8716     ReadModuleOffsetMap(M);
8717 
8718   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8719     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8720   assert(I != M.SelectorRemap.end()
8721          && "Invalid index into selector index remap");
8722 
8723   return LocalID + I->second;
8724 }
8725 
8726 DeclarationNameLoc
8727 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8728   switch (Name.getNameKind()) {
8729   case DeclarationName::CXXConstructorName:
8730   case DeclarationName::CXXDestructorName:
8731   case DeclarationName::CXXConversionFunctionName:
8732     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8733 
8734   case DeclarationName::CXXOperatorName:
8735     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8736 
8737   case DeclarationName::CXXLiteralOperatorName:
8738     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8739         readSourceLocation());
8740 
8741   case DeclarationName::Identifier:
8742   case DeclarationName::ObjCZeroArgSelector:
8743   case DeclarationName::ObjCOneArgSelector:
8744   case DeclarationName::ObjCMultiArgSelector:
8745   case DeclarationName::CXXUsingDirective:
8746   case DeclarationName::CXXDeductionGuideName:
8747     break;
8748   }
8749   return DeclarationNameLoc();
8750 }
8751 
8752 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8753   DeclarationNameInfo NameInfo;
8754   NameInfo.setName(readDeclarationName());
8755   NameInfo.setLoc(readSourceLocation());
8756   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8757   return NameInfo;
8758 }
8759 
8760 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8761   Info.QualifierLoc = readNestedNameSpecifierLoc();
8762   unsigned NumTPLists = readInt();
8763   Info.NumTemplParamLists = NumTPLists;
8764   if (NumTPLists) {
8765     Info.TemplParamLists =
8766         new (getContext()) TemplateParameterList *[NumTPLists];
8767     for (unsigned i = 0; i != NumTPLists; ++i)
8768       Info.TemplParamLists[i] = readTemplateParameterList();
8769   }
8770 }
8771 
8772 TemplateParameterList *
8773 ASTRecordReader::readTemplateParameterList() {
8774   SourceLocation TemplateLoc = readSourceLocation();
8775   SourceLocation LAngleLoc = readSourceLocation();
8776   SourceLocation RAngleLoc = readSourceLocation();
8777 
8778   unsigned NumParams = readInt();
8779   SmallVector<NamedDecl *, 16> Params;
8780   Params.reserve(NumParams);
8781   while (NumParams--)
8782     Params.push_back(readDeclAs<NamedDecl>());
8783 
8784   bool HasRequiresClause = readBool();
8785   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8786 
8787   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8788       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8789   return TemplateParams;
8790 }
8791 
8792 void ASTRecordReader::readTemplateArgumentList(
8793                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8794                         bool Canonicalize) {
8795   unsigned NumTemplateArgs = readInt();
8796   TemplArgs.reserve(NumTemplateArgs);
8797   while (NumTemplateArgs--)
8798     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8799 }
8800 
8801 /// Read a UnresolvedSet structure.
8802 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8803   unsigned NumDecls = readInt();
8804   Set.reserve(getContext(), NumDecls);
8805   while (NumDecls--) {
8806     DeclID ID = readDeclID();
8807     AccessSpecifier AS = (AccessSpecifier) readInt();
8808     Set.addLazyDecl(getContext(), ID, AS);
8809   }
8810 }
8811 
8812 CXXBaseSpecifier
8813 ASTRecordReader::readCXXBaseSpecifier() {
8814   bool isVirtual = readBool();
8815   bool isBaseOfClass = readBool();
8816   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8817   bool inheritConstructors = readBool();
8818   TypeSourceInfo *TInfo = readTypeSourceInfo();
8819   SourceRange Range = readSourceRange();
8820   SourceLocation EllipsisLoc = readSourceLocation();
8821   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8822                           EllipsisLoc);
8823   Result.setInheritConstructors(inheritConstructors);
8824   return Result;
8825 }
8826 
8827 CXXCtorInitializer **
8828 ASTRecordReader::readCXXCtorInitializers() {
8829   ASTContext &Context = getContext();
8830   unsigned NumInitializers = readInt();
8831   assert(NumInitializers && "wrote ctor initializers but have no inits");
8832   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8833   for (unsigned i = 0; i != NumInitializers; ++i) {
8834     TypeSourceInfo *TInfo = nullptr;
8835     bool IsBaseVirtual = false;
8836     FieldDecl *Member = nullptr;
8837     IndirectFieldDecl *IndirectMember = nullptr;
8838 
8839     CtorInitializerType Type = (CtorInitializerType) readInt();
8840     switch (Type) {
8841     case CTOR_INITIALIZER_BASE:
8842       TInfo = readTypeSourceInfo();
8843       IsBaseVirtual = readBool();
8844       break;
8845 
8846     case CTOR_INITIALIZER_DELEGATING:
8847       TInfo = readTypeSourceInfo();
8848       break;
8849 
8850      case CTOR_INITIALIZER_MEMBER:
8851       Member = readDeclAs<FieldDecl>();
8852       break;
8853 
8854      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8855       IndirectMember = readDeclAs<IndirectFieldDecl>();
8856       break;
8857     }
8858 
8859     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8860     Expr *Init = readExpr();
8861     SourceLocation LParenLoc = readSourceLocation();
8862     SourceLocation RParenLoc = readSourceLocation();
8863 
8864     CXXCtorInitializer *BOMInit;
8865     if (Type == CTOR_INITIALIZER_BASE)
8866       BOMInit = new (Context)
8867           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8868                              RParenLoc, MemberOrEllipsisLoc);
8869     else if (Type == CTOR_INITIALIZER_DELEGATING)
8870       BOMInit = new (Context)
8871           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8872     else if (Member)
8873       BOMInit = new (Context)
8874           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8875                              Init, RParenLoc);
8876     else
8877       BOMInit = new (Context)
8878           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8879                              LParenLoc, Init, RParenLoc);
8880 
8881     if (/*IsWritten*/readBool()) {
8882       unsigned SourceOrder = readInt();
8883       BOMInit->setSourceOrder(SourceOrder);
8884     }
8885 
8886     CtorInitializers[i] = BOMInit;
8887   }
8888 
8889   return CtorInitializers;
8890 }
8891 
8892 NestedNameSpecifierLoc
8893 ASTRecordReader::readNestedNameSpecifierLoc() {
8894   ASTContext &Context = getContext();
8895   unsigned N = readInt();
8896   NestedNameSpecifierLocBuilder Builder;
8897   for (unsigned I = 0; I != N; ++I) {
8898     auto Kind = readNestedNameSpecifierKind();
8899     switch (Kind) {
8900     case NestedNameSpecifier::Identifier: {
8901       IdentifierInfo *II = readIdentifier();
8902       SourceRange Range = readSourceRange();
8903       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8904       break;
8905     }
8906 
8907     case NestedNameSpecifier::Namespace: {
8908       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8909       SourceRange Range = readSourceRange();
8910       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8911       break;
8912     }
8913 
8914     case NestedNameSpecifier::NamespaceAlias: {
8915       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8916       SourceRange Range = readSourceRange();
8917       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8918       break;
8919     }
8920 
8921     case NestedNameSpecifier::TypeSpec:
8922     case NestedNameSpecifier::TypeSpecWithTemplate: {
8923       bool Template = readBool();
8924       TypeSourceInfo *T = readTypeSourceInfo();
8925       if (!T)
8926         return NestedNameSpecifierLoc();
8927       SourceLocation ColonColonLoc = readSourceLocation();
8928 
8929       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8930       Builder.Extend(Context,
8931                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8932                      T->getTypeLoc(), ColonColonLoc);
8933       break;
8934     }
8935 
8936     case NestedNameSpecifier::Global: {
8937       SourceLocation ColonColonLoc = readSourceLocation();
8938       Builder.MakeGlobal(Context, ColonColonLoc);
8939       break;
8940     }
8941 
8942     case NestedNameSpecifier::Super: {
8943       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8944       SourceRange Range = readSourceRange();
8945       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8946       break;
8947     }
8948     }
8949   }
8950 
8951   return Builder.getWithLocInContext(Context);
8952 }
8953 
8954 SourceRange
8955 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8956                            unsigned &Idx) {
8957   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8958   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8959   return SourceRange(beg, end);
8960 }
8961 
8962 /// Read a floating-point value
8963 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8964   return llvm::APFloat(Sem, readAPInt());
8965 }
8966 
8967 // Read a string
8968 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8969   unsigned Len = Record[Idx++];
8970   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8971   Idx += Len;
8972   return Result;
8973 }
8974 
8975 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8976                                 unsigned &Idx) {
8977   std::string Filename = ReadString(Record, Idx);
8978   ResolveImportedPath(F, Filename);
8979   return Filename;
8980 }
8981 
8982 std::string ASTReader::ReadPath(StringRef BaseDirectory,
8983                                 const RecordData &Record, unsigned &Idx) {
8984   std::string Filename = ReadString(Record, Idx);
8985   if (!BaseDirectory.empty())
8986     ResolveImportedPath(Filename, BaseDirectory);
8987   return Filename;
8988 }
8989 
8990 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8991                                          unsigned &Idx) {
8992   unsigned Major = Record[Idx++];
8993   unsigned Minor = Record[Idx++];
8994   unsigned Subminor = Record[Idx++];
8995   if (Minor == 0)
8996     return VersionTuple(Major);
8997   if (Subminor == 0)
8998     return VersionTuple(Major, Minor - 1);
8999   return VersionTuple(Major, Minor - 1, Subminor - 1);
9000 }
9001 
9002 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9003                                           const RecordData &Record,
9004                                           unsigned &Idx) {
9005   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9006   return CXXTemporary::Create(getContext(), Decl);
9007 }
9008 
9009 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9010   return Diag(CurrentImportLoc, DiagID);
9011 }
9012 
9013 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9014   return Diags.Report(Loc, DiagID);
9015 }
9016 
9017 /// Retrieve the identifier table associated with the
9018 /// preprocessor.
9019 IdentifierTable &ASTReader::getIdentifierTable() {
9020   return PP.getIdentifierTable();
9021 }
9022 
9023 /// Record that the given ID maps to the given switch-case
9024 /// statement.
9025 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9026   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9027          "Already have a SwitchCase with this ID");
9028   (*CurrSwitchCaseStmts)[ID] = SC;
9029 }
9030 
9031 /// Retrieve the switch-case statement with the given ID.
9032 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9033   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9034   return (*CurrSwitchCaseStmts)[ID];
9035 }
9036 
9037 void ASTReader::ClearSwitchCaseIDs() {
9038   CurrSwitchCaseStmts->clear();
9039 }
9040 
9041 void ASTReader::ReadComments() {
9042   ASTContext &Context = getContext();
9043   std::vector<RawComment *> Comments;
9044   for (SmallVectorImpl<std::pair<BitstreamCursor,
9045                                  serialization::ModuleFile *>>::iterator
9046        I = CommentsCursors.begin(),
9047        E = CommentsCursors.end();
9048        I != E; ++I) {
9049     Comments.clear();
9050     BitstreamCursor &Cursor = I->first;
9051     serialization::ModuleFile &F = *I->second;
9052     SavedStreamPosition SavedPosition(Cursor);
9053 
9054     RecordData Record;
9055     while (true) {
9056       Expected<llvm::BitstreamEntry> MaybeEntry =
9057           Cursor.advanceSkippingSubblocks(
9058               BitstreamCursor::AF_DontPopBlockAtEnd);
9059       if (!MaybeEntry) {
9060         Error(MaybeEntry.takeError());
9061         return;
9062       }
9063       llvm::BitstreamEntry Entry = MaybeEntry.get();
9064 
9065       switch (Entry.Kind) {
9066       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9067       case llvm::BitstreamEntry::Error:
9068         Error("malformed block record in AST file");
9069         return;
9070       case llvm::BitstreamEntry::EndBlock:
9071         goto NextCursor;
9072       case llvm::BitstreamEntry::Record:
9073         // The interesting case.
9074         break;
9075       }
9076 
9077       // Read a record.
9078       Record.clear();
9079       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9080       if (!MaybeComment) {
9081         Error(MaybeComment.takeError());
9082         return;
9083       }
9084       switch ((CommentRecordTypes)MaybeComment.get()) {
9085       case COMMENTS_RAW_COMMENT: {
9086         unsigned Idx = 0;
9087         SourceRange SR = ReadSourceRange(F, Record, Idx);
9088         RawComment::CommentKind Kind =
9089             (RawComment::CommentKind) Record[Idx++];
9090         bool IsTrailingComment = Record[Idx++];
9091         bool IsAlmostTrailingComment = Record[Idx++];
9092         Comments.push_back(new (Context) RawComment(
9093             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9094         break;
9095       }
9096       }
9097     }
9098   NextCursor:
9099     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9100         FileToOffsetToComment;
9101     for (RawComment *C : Comments) {
9102       SourceLocation CommentLoc = C->getBeginLoc();
9103       if (CommentLoc.isValid()) {
9104         std::pair<FileID, unsigned> Loc =
9105             SourceMgr.getDecomposedLoc(CommentLoc);
9106         if (Loc.first.isValid())
9107           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9108       }
9109     }
9110   }
9111 }
9112 
9113 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9114                                 bool IncludeSystem, bool Complain,
9115                     llvm::function_ref<void(const serialization::InputFile &IF,
9116                                             bool isSystem)> Visitor) {
9117   unsigned NumUserInputs = MF.NumUserInputFiles;
9118   unsigned NumInputs = MF.InputFilesLoaded.size();
9119   assert(NumUserInputs <= NumInputs);
9120   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9121   for (unsigned I = 0; I < N; ++I) {
9122     bool IsSystem = I >= NumUserInputs;
9123     InputFile IF = getInputFile(MF, I+1, Complain);
9124     Visitor(IF, IsSystem);
9125   }
9126 }
9127 
9128 void ASTReader::visitTopLevelModuleMaps(
9129     serialization::ModuleFile &MF,
9130     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9131   unsigned NumInputs = MF.InputFilesLoaded.size();
9132   for (unsigned I = 0; I < NumInputs; ++I) {
9133     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9134     if (IFI.TopLevelModuleMap)
9135       // FIXME: This unnecessarily re-reads the InputFileInfo.
9136       if (auto FE = getInputFile(MF, I + 1).getFile())
9137         Visitor(FE);
9138   }
9139 }
9140 
9141 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9142   // If we know the owning module, use it.
9143   if (Module *M = D->getImportedOwningModule())
9144     return M->getFullModuleName();
9145 
9146   // Otherwise, use the name of the top-level module the decl is within.
9147   if (ModuleFile *M = getOwningModuleFile(D))
9148     return M->ModuleName;
9149 
9150   // Not from a module.
9151   return {};
9152 }
9153 
9154 void ASTReader::finishPendingActions() {
9155   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9156          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9157          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9158          !PendingUpdateRecords.empty()) {
9159     // If any identifiers with corresponding top-level declarations have
9160     // been loaded, load those declarations now.
9161     using TopLevelDeclsMap =
9162         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9163     TopLevelDeclsMap TopLevelDecls;
9164 
9165     while (!PendingIdentifierInfos.empty()) {
9166       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9167       SmallVector<uint32_t, 4> DeclIDs =
9168           std::move(PendingIdentifierInfos.back().second);
9169       PendingIdentifierInfos.pop_back();
9170 
9171       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9172     }
9173 
9174     // Load each function type that we deferred loading because it was a
9175     // deduced type that might refer to a local type declared within itself.
9176     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9177       auto *FD = PendingFunctionTypes[I].first;
9178       FD->setType(GetType(PendingFunctionTypes[I].second));
9179 
9180       // If we gave a function a deduced return type, remember that we need to
9181       // propagate that along the redeclaration chain.
9182       auto *DT = FD->getReturnType()->getContainedDeducedType();
9183       if (DT && DT->isDeduced())
9184         PendingDeducedTypeUpdates.insert(
9185             {FD->getCanonicalDecl(), FD->getReturnType()});
9186     }
9187     PendingFunctionTypes.clear();
9188 
9189     // For each decl chain that we wanted to complete while deserializing, mark
9190     // it as "still needs to be completed".
9191     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9192       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9193     }
9194     PendingIncompleteDeclChains.clear();
9195 
9196     // Load pending declaration chains.
9197     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9198       loadPendingDeclChain(PendingDeclChains[I].first,
9199                            PendingDeclChains[I].second);
9200     PendingDeclChains.clear();
9201 
9202     // Make the most recent of the top-level declarations visible.
9203     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9204            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9205       IdentifierInfo *II = TLD->first;
9206       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9207         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9208       }
9209     }
9210 
9211     // Load any pending macro definitions.
9212     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9213       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9214       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9215       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9216       // Initialize the macro history from chained-PCHs ahead of module imports.
9217       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9218            ++IDIdx) {
9219         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9220         if (!Info.M->isModule())
9221           resolvePendingMacro(II, Info);
9222       }
9223       // Handle 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     }
9231     PendingMacroIDs.clear();
9232 
9233     // Wire up the DeclContexts for Decls that we delayed setting until
9234     // recursive loading is completed.
9235     while (!PendingDeclContextInfos.empty()) {
9236       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9237       PendingDeclContextInfos.pop_front();
9238       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9239       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9240       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9241     }
9242 
9243     // Perform any pending declaration updates.
9244     while (!PendingUpdateRecords.empty()) {
9245       auto Update = PendingUpdateRecords.pop_back_val();
9246       ReadingKindTracker ReadingKind(Read_Decl, *this);
9247       loadDeclUpdateRecords(Update);
9248     }
9249   }
9250 
9251   // At this point, all update records for loaded decls are in place, so any
9252   // fake class definitions should have become real.
9253   assert(PendingFakeDefinitionData.empty() &&
9254          "faked up a class definition but never saw the real one");
9255 
9256   // If we deserialized any C++ or Objective-C class definitions, any
9257   // Objective-C protocol definitions, or any redeclarable templates, make sure
9258   // that all redeclarations point to the definitions. Note that this can only
9259   // happen now, after the redeclaration chains have been fully wired.
9260   for (Decl *D : PendingDefinitions) {
9261     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9262       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9263         // Make sure that the TagType points at the definition.
9264         const_cast<TagType*>(TagT)->decl = TD;
9265       }
9266 
9267       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9268         for (auto *R = getMostRecentExistingDecl(RD); R;
9269              R = R->getPreviousDecl()) {
9270           assert((R == D) ==
9271                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9272                  "declaration thinks it's the definition but it isn't");
9273           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9274         }
9275       }
9276 
9277       continue;
9278     }
9279 
9280     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9281       // Make sure that the ObjCInterfaceType points at the definition.
9282       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9283         ->Decl = ID;
9284 
9285       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9286         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9287 
9288       continue;
9289     }
9290 
9291     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9292       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9293         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9294 
9295       continue;
9296     }
9297 
9298     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9299     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9300       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9301   }
9302   PendingDefinitions.clear();
9303 
9304   // Load the bodies of any functions or methods we've encountered. We do
9305   // this now (delayed) so that we can be sure that the declaration chains
9306   // have been fully wired up (hasBody relies on this).
9307   // FIXME: We shouldn't require complete redeclaration chains here.
9308   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9309                                PBEnd = PendingBodies.end();
9310        PB != PBEnd; ++PB) {
9311     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9312       // For a function defined inline within a class template, force the
9313       // canonical definition to be the one inside the canonical definition of
9314       // the template. This ensures that we instantiate from a correct view
9315       // of the template.
9316       //
9317       // Sadly we can't do this more generally: we can't be sure that all
9318       // copies of an arbitrary class definition will have the same members
9319       // defined (eg, some member functions may not be instantiated, and some
9320       // special members may or may not have been implicitly defined).
9321       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9322         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9323           continue;
9324 
9325       // FIXME: Check for =delete/=default?
9326       // FIXME: Complain about ODR violations here?
9327       const FunctionDecl *Defn = nullptr;
9328       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9329         FD->setLazyBody(PB->second);
9330       } else {
9331         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9332         mergeDefinitionVisibility(NonConstDefn, FD);
9333 
9334         if (!FD->isLateTemplateParsed() &&
9335             !NonConstDefn->isLateTemplateParsed() &&
9336             FD->getODRHash() != NonConstDefn->getODRHash()) {
9337           if (!isa<CXXMethodDecl>(FD)) {
9338             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9339           } else if (FD->getLexicalParent()->isFileContext() &&
9340                      NonConstDefn->getLexicalParent()->isFileContext()) {
9341             // Only diagnose out-of-line method definitions.  If they are
9342             // in class definitions, then an error will be generated when
9343             // processing the class bodies.
9344             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9345           }
9346         }
9347       }
9348       continue;
9349     }
9350 
9351     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9352     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9353       MD->setLazyBody(PB->second);
9354   }
9355   PendingBodies.clear();
9356 
9357   // Do some cleanup.
9358   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9359     getContext().deduplicateMergedDefinitonsFor(ND);
9360   PendingMergedDefinitionsToDeduplicate.clear();
9361 }
9362 
9363 void ASTReader::diagnoseOdrViolations() {
9364   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9365       PendingFunctionOdrMergeFailures.empty() &&
9366       PendingEnumOdrMergeFailures.empty())
9367     return;
9368 
9369   // Trigger the import of the full definition of each class that had any
9370   // odr-merging problems, so we can produce better diagnostics for them.
9371   // These updates may in turn find and diagnose some ODR failures, so take
9372   // ownership of the set first.
9373   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9374   PendingOdrMergeFailures.clear();
9375   for (auto &Merge : OdrMergeFailures) {
9376     Merge.first->buildLookup();
9377     Merge.first->decls_begin();
9378     Merge.first->bases_begin();
9379     Merge.first->vbases_begin();
9380     for (auto &RecordPair : Merge.second) {
9381       auto *RD = RecordPair.first;
9382       RD->decls_begin();
9383       RD->bases_begin();
9384       RD->vbases_begin();
9385     }
9386   }
9387 
9388   // Trigger the import of functions.
9389   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9390   PendingFunctionOdrMergeFailures.clear();
9391   for (auto &Merge : FunctionOdrMergeFailures) {
9392     Merge.first->buildLookup();
9393     Merge.first->decls_begin();
9394     Merge.first->getBody();
9395     for (auto &FD : Merge.second) {
9396       FD->buildLookup();
9397       FD->decls_begin();
9398       FD->getBody();
9399     }
9400   }
9401 
9402   // Trigger the import of enums.
9403   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9404   PendingEnumOdrMergeFailures.clear();
9405   for (auto &Merge : EnumOdrMergeFailures) {
9406     Merge.first->decls_begin();
9407     for (auto &Enum : Merge.second) {
9408       Enum->decls_begin();
9409     }
9410   }
9411 
9412   // For each declaration from a merged context, check that the canonical
9413   // definition of that context also contains a declaration of the same
9414   // entity.
9415   //
9416   // Caution: this loop does things that might invalidate iterators into
9417   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9418   while (!PendingOdrMergeChecks.empty()) {
9419     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9420 
9421     // FIXME: Skip over implicit declarations for now. This matters for things
9422     // like implicitly-declared special member functions. This isn't entirely
9423     // correct; we can end up with multiple unmerged declarations of the same
9424     // implicit entity.
9425     if (D->isImplicit())
9426       continue;
9427 
9428     DeclContext *CanonDef = D->getDeclContext();
9429 
9430     bool Found = false;
9431     const Decl *DCanon = D->getCanonicalDecl();
9432 
9433     for (auto RI : D->redecls()) {
9434       if (RI->getLexicalDeclContext() == CanonDef) {
9435         Found = true;
9436         break;
9437       }
9438     }
9439     if (Found)
9440       continue;
9441 
9442     // Quick check failed, time to do the slow thing. Note, we can't just
9443     // look up the name of D in CanonDef here, because the member that is
9444     // in CanonDef might not be found by name lookup (it might have been
9445     // replaced by a more recent declaration in the lookup table), and we
9446     // can't necessarily find it in the redeclaration chain because it might
9447     // be merely mergeable, not redeclarable.
9448     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9449     for (auto *CanonMember : CanonDef->decls()) {
9450       if (CanonMember->getCanonicalDecl() == DCanon) {
9451         // This can happen if the declaration is merely mergeable and not
9452         // actually redeclarable (we looked for redeclarations earlier).
9453         //
9454         // FIXME: We should be able to detect this more efficiently, without
9455         // pulling in all of the members of CanonDef.
9456         Found = true;
9457         break;
9458       }
9459       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9460         if (ND->getDeclName() == D->getDeclName())
9461           Candidates.push_back(ND);
9462     }
9463 
9464     if (!Found) {
9465       // The AST doesn't like TagDecls becoming invalid after they've been
9466       // completed. We only really need to mark FieldDecls as invalid here.
9467       if (!isa<TagDecl>(D))
9468         D->setInvalidDecl();
9469 
9470       // Ensure we don't accidentally recursively enter deserialization while
9471       // we're producing our diagnostic.
9472       Deserializing RecursionGuard(this);
9473 
9474       std::string CanonDefModule =
9475           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9476       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9477         << D << getOwningModuleNameForDiagnostic(D)
9478         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9479 
9480       if (Candidates.empty())
9481         Diag(cast<Decl>(CanonDef)->getLocation(),
9482              diag::note_module_odr_violation_no_possible_decls) << D;
9483       else {
9484         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9485           Diag(Candidates[I]->getLocation(),
9486                diag::note_module_odr_violation_possible_decl)
9487             << Candidates[I];
9488       }
9489 
9490       DiagnosedOdrMergeFailures.insert(CanonDef);
9491     }
9492   }
9493 
9494   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9495       EnumOdrMergeFailures.empty())
9496     return;
9497 
9498   // Ensure we don't accidentally recursively enter deserialization while
9499   // we're producing our diagnostics.
9500   Deserializing RecursionGuard(this);
9501 
9502   // Common code for hashing helpers.
9503   ODRHash Hash;
9504   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9505     Hash.clear();
9506     Hash.AddQualType(Ty);
9507     return Hash.CalculateHash();
9508   };
9509 
9510   auto ComputeODRHash = [&Hash](const Stmt *S) {
9511     assert(S);
9512     Hash.clear();
9513     Hash.AddStmt(S);
9514     return Hash.CalculateHash();
9515   };
9516 
9517   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9518     assert(D);
9519     Hash.clear();
9520     Hash.AddSubDecl(D);
9521     return Hash.CalculateHash();
9522   };
9523 
9524   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9525     Hash.clear();
9526     Hash.AddTemplateArgument(TA);
9527     return Hash.CalculateHash();
9528   };
9529 
9530   auto ComputeTemplateParameterListODRHash =
9531       [&Hash](const TemplateParameterList *TPL) {
9532         assert(TPL);
9533         Hash.clear();
9534         Hash.AddTemplateParameterList(TPL);
9535         return Hash.CalculateHash();
9536       };
9537 
9538   // Used with err_module_odr_violation_mismatch_decl and
9539   // note_module_odr_violation_mismatch_decl
9540   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9541   enum ODRMismatchDecl {
9542     EndOfClass,
9543     PublicSpecifer,
9544     PrivateSpecifer,
9545     ProtectedSpecifer,
9546     StaticAssert,
9547     Field,
9548     CXXMethod,
9549     TypeAlias,
9550     TypeDef,
9551     Var,
9552     Friend,
9553     FunctionTemplate,
9554     Other
9555   };
9556 
9557   // Used with err_module_odr_violation_mismatch_decl_diff and
9558   // note_module_odr_violation_mismatch_decl_diff
9559   enum ODRMismatchDeclDifference {
9560     StaticAssertCondition,
9561     StaticAssertMessage,
9562     StaticAssertOnlyMessage,
9563     FieldName,
9564     FieldTypeName,
9565     FieldSingleBitField,
9566     FieldDifferentWidthBitField,
9567     FieldSingleMutable,
9568     FieldSingleInitializer,
9569     FieldDifferentInitializers,
9570     MethodName,
9571     MethodDeleted,
9572     MethodDefaulted,
9573     MethodVirtual,
9574     MethodStatic,
9575     MethodVolatile,
9576     MethodConst,
9577     MethodInline,
9578     MethodNumberParameters,
9579     MethodParameterType,
9580     MethodParameterName,
9581     MethodParameterSingleDefaultArgument,
9582     MethodParameterDifferentDefaultArgument,
9583     MethodNoTemplateArguments,
9584     MethodDifferentNumberTemplateArguments,
9585     MethodDifferentTemplateArgument,
9586     MethodSingleBody,
9587     MethodDifferentBody,
9588     TypedefName,
9589     TypedefType,
9590     VarName,
9591     VarType,
9592     VarSingleInitializer,
9593     VarDifferentInitializer,
9594     VarConstexpr,
9595     FriendTypeFunction,
9596     FriendType,
9597     FriendFunction,
9598     FunctionTemplateDifferentNumberParameters,
9599     FunctionTemplateParameterDifferentKind,
9600     FunctionTemplateParameterName,
9601     FunctionTemplateParameterSingleDefaultArgument,
9602     FunctionTemplateParameterDifferentDefaultArgument,
9603     FunctionTemplateParameterDifferentType,
9604     FunctionTemplatePackParameter,
9605   };
9606 
9607   // These lambdas have the common portions of the ODR diagnostics.  This
9608   // has the same return as Diag(), so addition parameters can be passed
9609   // in with operator<<
9610   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9611                                  SourceLocation Loc, SourceRange Range,
9612                                  ODRMismatchDeclDifference DiffType) {
9613     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9614            << FirstRecord << FirstModule.empty() << FirstModule << Range
9615            << DiffType;
9616   };
9617   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9618                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9619     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9620            << SecondModule << Range << DiffType;
9621   };
9622 
9623   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9624                        &ComputeQualTypeODRHash, &ComputeODRHash](
9625                           NamedDecl *FirstRecord, StringRef FirstModule,
9626                           StringRef SecondModule, FieldDecl *FirstField,
9627                           FieldDecl *SecondField) {
9628     IdentifierInfo *FirstII = FirstField->getIdentifier();
9629     IdentifierInfo *SecondII = SecondField->getIdentifier();
9630     if (FirstII->getName() != SecondII->getName()) {
9631       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9632                        FirstField->getSourceRange(), FieldName)
9633           << FirstII;
9634       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9635                       SecondField->getSourceRange(), FieldName)
9636           << SecondII;
9637 
9638       return true;
9639     }
9640 
9641     assert(getContext().hasSameType(FirstField->getType(),
9642                                     SecondField->getType()));
9643 
9644     QualType FirstType = FirstField->getType();
9645     QualType SecondType = SecondField->getType();
9646     if (ComputeQualTypeODRHash(FirstType) !=
9647         ComputeQualTypeODRHash(SecondType)) {
9648       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9649                        FirstField->getSourceRange(), FieldTypeName)
9650           << FirstII << FirstType;
9651       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9652                       SecondField->getSourceRange(), FieldTypeName)
9653           << SecondII << SecondType;
9654 
9655       return true;
9656     }
9657 
9658     const bool IsFirstBitField = FirstField->isBitField();
9659     const bool IsSecondBitField = SecondField->isBitField();
9660     if (IsFirstBitField != IsSecondBitField) {
9661       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9662                        FirstField->getSourceRange(), FieldSingleBitField)
9663           << FirstII << IsFirstBitField;
9664       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9665                       SecondField->getSourceRange(), FieldSingleBitField)
9666           << SecondII << IsSecondBitField;
9667       return true;
9668     }
9669 
9670     if (IsFirstBitField && IsSecondBitField) {
9671       unsigned FirstBitWidthHash =
9672           ComputeODRHash(FirstField->getBitWidth());
9673       unsigned SecondBitWidthHash =
9674           ComputeODRHash(SecondField->getBitWidth());
9675       if (FirstBitWidthHash != SecondBitWidthHash) {
9676         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9677                          FirstField->getSourceRange(),
9678                          FieldDifferentWidthBitField)
9679             << FirstII << FirstField->getBitWidth()->getSourceRange();
9680         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9681                         SecondField->getSourceRange(),
9682                         FieldDifferentWidthBitField)
9683             << SecondII << SecondField->getBitWidth()->getSourceRange();
9684         return true;
9685       }
9686     }
9687 
9688     if (!PP.getLangOpts().CPlusPlus)
9689       return false;
9690 
9691     const bool IsFirstMutable = FirstField->isMutable();
9692     const bool IsSecondMutable = SecondField->isMutable();
9693     if (IsFirstMutable != IsSecondMutable) {
9694       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9695                        FirstField->getSourceRange(), FieldSingleMutable)
9696           << FirstII << IsFirstMutable;
9697       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9698                       SecondField->getSourceRange(), FieldSingleMutable)
9699           << SecondII << IsSecondMutable;
9700       return true;
9701     }
9702 
9703     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9704     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9705     if ((!FirstInitializer && SecondInitializer) ||
9706         (FirstInitializer && !SecondInitializer)) {
9707       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9708                        FirstField->getSourceRange(), FieldSingleInitializer)
9709           << FirstII << (FirstInitializer != nullptr);
9710       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9711                       SecondField->getSourceRange(), FieldSingleInitializer)
9712           << SecondII << (SecondInitializer != nullptr);
9713       return true;
9714     }
9715 
9716     if (FirstInitializer && SecondInitializer) {
9717       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9718       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9719       if (FirstInitHash != SecondInitHash) {
9720         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9721                          FirstField->getSourceRange(),
9722                          FieldDifferentInitializers)
9723             << FirstII << FirstInitializer->getSourceRange();
9724         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9725                         SecondField->getSourceRange(),
9726                         FieldDifferentInitializers)
9727             << SecondII << SecondInitializer->getSourceRange();
9728         return true;
9729       }
9730     }
9731 
9732     return false;
9733   };
9734 
9735   auto ODRDiagTypeDefOrAlias =
9736       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9737           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9738           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9739           bool IsTypeAlias) {
9740         auto FirstName = FirstTD->getDeclName();
9741         auto SecondName = SecondTD->getDeclName();
9742         if (FirstName != SecondName) {
9743           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9744                            FirstTD->getSourceRange(), TypedefName)
9745               << IsTypeAlias << FirstName;
9746           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9747                           SecondTD->getSourceRange(), TypedefName)
9748               << IsTypeAlias << SecondName;
9749           return true;
9750         }
9751 
9752         QualType FirstType = FirstTD->getUnderlyingType();
9753         QualType SecondType = SecondTD->getUnderlyingType();
9754         if (ComputeQualTypeODRHash(FirstType) !=
9755             ComputeQualTypeODRHash(SecondType)) {
9756           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9757                            FirstTD->getSourceRange(), TypedefType)
9758               << IsTypeAlias << FirstName << FirstType;
9759           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9760                           SecondTD->getSourceRange(), TypedefType)
9761               << IsTypeAlias << SecondName << SecondType;
9762           return true;
9763         }
9764 
9765         return false;
9766   };
9767 
9768   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9769                      &ComputeQualTypeODRHash, &ComputeODRHash,
9770                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9771                            StringRef SecondModule, VarDecl *FirstVD,
9772                            VarDecl *SecondVD) {
9773     auto FirstName = FirstVD->getDeclName();
9774     auto SecondName = SecondVD->getDeclName();
9775     if (FirstName != SecondName) {
9776       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9777                        FirstVD->getSourceRange(), VarName)
9778           << FirstName;
9779       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9780                       SecondVD->getSourceRange(), VarName)
9781           << SecondName;
9782       return true;
9783     }
9784 
9785     QualType FirstType = FirstVD->getType();
9786     QualType SecondType = SecondVD->getType();
9787     if (ComputeQualTypeODRHash(FirstType) !=
9788         ComputeQualTypeODRHash(SecondType)) {
9789       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9790                        FirstVD->getSourceRange(), VarType)
9791           << FirstName << FirstType;
9792       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9793                       SecondVD->getSourceRange(), VarType)
9794           << SecondName << SecondType;
9795       return true;
9796     }
9797 
9798     if (!PP.getLangOpts().CPlusPlus)
9799       return false;
9800 
9801     const Expr *FirstInit = FirstVD->getInit();
9802     const Expr *SecondInit = SecondVD->getInit();
9803     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9804       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9805                        FirstVD->getSourceRange(), VarSingleInitializer)
9806           << FirstName << (FirstInit == nullptr)
9807           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9808       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9809                       SecondVD->getSourceRange(), VarSingleInitializer)
9810           << SecondName << (SecondInit == nullptr)
9811           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9812       return true;
9813     }
9814 
9815     if (FirstInit && SecondInit &&
9816         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9817       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9818                        FirstVD->getSourceRange(), VarDifferentInitializer)
9819           << FirstName << FirstInit->getSourceRange();
9820       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9821                       SecondVD->getSourceRange(), VarDifferentInitializer)
9822           << SecondName << SecondInit->getSourceRange();
9823       return true;
9824     }
9825 
9826     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9827     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9828     if (FirstIsConstexpr != SecondIsConstexpr) {
9829       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9830                        FirstVD->getSourceRange(), VarConstexpr)
9831           << FirstName << FirstIsConstexpr;
9832       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9833                       SecondVD->getSourceRange(), VarConstexpr)
9834           << SecondName << SecondIsConstexpr;
9835       return true;
9836     }
9837     return false;
9838   };
9839 
9840   auto DifferenceSelector = [](Decl *D) {
9841     assert(D && "valid Decl required");
9842     switch (D->getKind()) {
9843     default:
9844       return Other;
9845     case Decl::AccessSpec:
9846       switch (D->getAccess()) {
9847       case AS_public:
9848         return PublicSpecifer;
9849       case AS_private:
9850         return PrivateSpecifer;
9851       case AS_protected:
9852         return ProtectedSpecifer;
9853       case AS_none:
9854         break;
9855       }
9856       llvm_unreachable("Invalid access specifier");
9857     case Decl::StaticAssert:
9858       return StaticAssert;
9859     case Decl::Field:
9860       return Field;
9861     case Decl::CXXMethod:
9862     case Decl::CXXConstructor:
9863     case Decl::CXXDestructor:
9864       return CXXMethod;
9865     case Decl::TypeAlias:
9866       return TypeAlias;
9867     case Decl::Typedef:
9868       return TypeDef;
9869     case Decl::Var:
9870       return Var;
9871     case Decl::Friend:
9872       return Friend;
9873     case Decl::FunctionTemplate:
9874       return FunctionTemplate;
9875     }
9876   };
9877 
9878   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9879   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9880                                                  RecordDecl *Record,
9881                                                  const DeclContext *DC) {
9882     for (auto *D : Record->decls()) {
9883       if (!ODRHash::isDeclToBeProcessed(D, DC))
9884         continue;
9885       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9886     }
9887   };
9888 
9889   struct DiffResult {
9890     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9891     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9892   };
9893 
9894   // If there is a diagnoseable difference, FirstDiffType and
9895   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9896   // filled in if not EndOfClass.
9897   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9898                                              DeclHashes &SecondHashes) {
9899     DiffResult DR;
9900     auto FirstIt = FirstHashes.begin();
9901     auto SecondIt = SecondHashes.begin();
9902     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9903       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9904           FirstIt->second == SecondIt->second) {
9905         ++FirstIt;
9906         ++SecondIt;
9907         continue;
9908       }
9909 
9910       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9911       DR.SecondDecl =
9912           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9913 
9914       DR.FirstDiffType =
9915           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9916       DR.SecondDiffType =
9917           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9918       return DR;
9919     }
9920     return DR;
9921   };
9922 
9923   // Use this to diagnose that an unexpected Decl was encountered
9924   // or no difference was detected. This causes a generic error
9925   // message to be emitted.
9926   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9927                                       StringRef FirstModule,
9928                                       NamedDecl *SecondRecord,
9929                                       StringRef SecondModule) {
9930     Diag(FirstRecord->getLocation(),
9931          diag::err_module_odr_violation_different_definitions)
9932         << FirstRecord << FirstModule.empty() << FirstModule;
9933 
9934     if (DR.FirstDecl) {
9935       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9936           << FirstRecord << DR.FirstDecl->getSourceRange();
9937     }
9938 
9939     Diag(SecondRecord->getLocation(),
9940          diag::note_module_odr_violation_different_definitions)
9941         << SecondModule;
9942 
9943     if (DR.SecondDecl) {
9944       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9945           << DR.SecondDecl->getSourceRange();
9946     }
9947   };
9948 
9949   auto DiagnoseODRMismatch =
9950       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9951              NamedDecl *SecondRecord, StringRef SecondModule) {
9952         SourceLocation FirstLoc;
9953         SourceRange FirstRange;
9954         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9955         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9956           FirstLoc = FirstTag->getBraceRange().getEnd();
9957         } else {
9958           FirstLoc = DR.FirstDecl->getLocation();
9959           FirstRange = DR.FirstDecl->getSourceRange();
9960         }
9961         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9962             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9963             << DR.FirstDiffType;
9964 
9965         SourceLocation SecondLoc;
9966         SourceRange SecondRange;
9967         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9968         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9969           SecondLoc = SecondTag->getBraceRange().getEnd();
9970         } else {
9971           SecondLoc = DR.SecondDecl->getLocation();
9972           SecondRange = DR.SecondDecl->getSourceRange();
9973         }
9974         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9975             << SecondModule << SecondRange << DR.SecondDiffType;
9976       };
9977 
9978   // Issue any pending ODR-failure diagnostics.
9979   for (auto &Merge : OdrMergeFailures) {
9980     // If we've already pointed out a specific problem with this class, don't
9981     // bother issuing a general "something's different" diagnostic.
9982     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9983       continue;
9984 
9985     bool Diagnosed = false;
9986     CXXRecordDecl *FirstRecord = Merge.first;
9987     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9988     for (auto &RecordPair : Merge.second) {
9989       CXXRecordDecl *SecondRecord = RecordPair.first;
9990       // Multiple different declarations got merged together; tell the user
9991       // where they came from.
9992       if (FirstRecord == SecondRecord)
9993         continue;
9994 
9995       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
9996 
9997       auto *FirstDD = FirstRecord->DefinitionData;
9998       auto *SecondDD = RecordPair.second;
9999 
10000       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10001 
10002       // Diagnostics from DefinitionData are emitted here.
10003       if (FirstDD != SecondDD) {
10004         enum ODRDefinitionDataDifference {
10005           NumBases,
10006           NumVBases,
10007           BaseType,
10008           BaseVirtual,
10009           BaseAccess,
10010         };
10011         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10012                                  this](SourceLocation Loc, SourceRange Range,
10013                                        ODRDefinitionDataDifference DiffType) {
10014           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10015                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10016                  << DiffType;
10017         };
10018         auto ODRDiagBaseNote = [&SecondModule,
10019                                 this](SourceLocation Loc, SourceRange Range,
10020                                       ODRDefinitionDataDifference DiffType) {
10021           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10022                  << SecondModule << Range << DiffType;
10023         };
10024 
10025         unsigned FirstNumBases = FirstDD->NumBases;
10026         unsigned FirstNumVBases = FirstDD->NumVBases;
10027         unsigned SecondNumBases = SecondDD->NumBases;
10028         unsigned SecondNumVBases = SecondDD->NumVBases;
10029 
10030         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10031           unsigned NumBases = DD->NumBases;
10032           if (NumBases == 0) return SourceRange();
10033           auto bases = DD->bases();
10034           return SourceRange(bases[0].getBeginLoc(),
10035                              bases[NumBases - 1].getEndLoc());
10036         };
10037 
10038         if (FirstNumBases != SecondNumBases) {
10039           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10040                            NumBases)
10041               << FirstNumBases;
10042           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10043                           NumBases)
10044               << SecondNumBases;
10045           Diagnosed = true;
10046           break;
10047         }
10048 
10049         if (FirstNumVBases != SecondNumVBases) {
10050           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10051                            NumVBases)
10052               << FirstNumVBases;
10053           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10054                           NumVBases)
10055               << SecondNumVBases;
10056           Diagnosed = true;
10057           break;
10058         }
10059 
10060         auto FirstBases = FirstDD->bases();
10061         auto SecondBases = SecondDD->bases();
10062         unsigned i = 0;
10063         for (i = 0; i < FirstNumBases; ++i) {
10064           auto FirstBase = FirstBases[i];
10065           auto SecondBase = SecondBases[i];
10066           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10067               ComputeQualTypeODRHash(SecondBase.getType())) {
10068             ODRDiagBaseError(FirstRecord->getLocation(),
10069                              FirstBase.getSourceRange(), BaseType)
10070                 << (i + 1) << FirstBase.getType();
10071             ODRDiagBaseNote(SecondRecord->getLocation(),
10072                             SecondBase.getSourceRange(), BaseType)
10073                 << (i + 1) << SecondBase.getType();
10074             break;
10075           }
10076 
10077           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10078             ODRDiagBaseError(FirstRecord->getLocation(),
10079                              FirstBase.getSourceRange(), BaseVirtual)
10080                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10081             ODRDiagBaseNote(SecondRecord->getLocation(),
10082                             SecondBase.getSourceRange(), BaseVirtual)
10083                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10084             break;
10085           }
10086 
10087           if (FirstBase.getAccessSpecifierAsWritten() !=
10088               SecondBase.getAccessSpecifierAsWritten()) {
10089             ODRDiagBaseError(FirstRecord->getLocation(),
10090                              FirstBase.getSourceRange(), BaseAccess)
10091                 << (i + 1) << FirstBase.getType()
10092                 << (int)FirstBase.getAccessSpecifierAsWritten();
10093             ODRDiagBaseNote(SecondRecord->getLocation(),
10094                             SecondBase.getSourceRange(), BaseAccess)
10095                 << (i + 1) << SecondBase.getType()
10096                 << (int)SecondBase.getAccessSpecifierAsWritten();
10097             break;
10098           }
10099         }
10100 
10101         if (i != FirstNumBases) {
10102           Diagnosed = true;
10103           break;
10104         }
10105       }
10106 
10107       const ClassTemplateDecl *FirstTemplate =
10108           FirstRecord->getDescribedClassTemplate();
10109       const ClassTemplateDecl *SecondTemplate =
10110           SecondRecord->getDescribedClassTemplate();
10111 
10112       assert(!FirstTemplate == !SecondTemplate &&
10113              "Both pointers should be null or non-null");
10114 
10115       enum ODRTemplateDifference {
10116         ParamEmptyName,
10117         ParamName,
10118         ParamSingleDefaultArgument,
10119         ParamDifferentDefaultArgument,
10120       };
10121 
10122       if (FirstTemplate && SecondTemplate) {
10123         DeclHashes FirstTemplateHashes;
10124         DeclHashes SecondTemplateHashes;
10125 
10126         auto PopulateTemplateParameterHashs =
10127             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10128                                      const ClassTemplateDecl *TD) {
10129               for (auto *D : TD->getTemplateParameters()->asArray()) {
10130                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10131               }
10132             };
10133 
10134         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10135         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10136 
10137         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10138                "Number of template parameters should be equal.");
10139 
10140         auto FirstIt = FirstTemplateHashes.begin();
10141         auto FirstEnd = FirstTemplateHashes.end();
10142         auto SecondIt = SecondTemplateHashes.begin();
10143         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10144           if (FirstIt->second == SecondIt->second)
10145             continue;
10146 
10147           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10148                                           SourceLocation Loc, SourceRange Range,
10149                                           ODRTemplateDifference DiffType) {
10150             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10151                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10152                    << DiffType;
10153           };
10154           auto ODRDiagTemplateNote = [&SecondModule, this](
10155                                          SourceLocation Loc, SourceRange Range,
10156                                          ODRTemplateDifference DiffType) {
10157             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10158                    << SecondModule << Range << DiffType;
10159           };
10160 
10161           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10162           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10163 
10164           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10165                  "Parameter Decl's should be the same kind.");
10166 
10167           DeclarationName FirstName = FirstDecl->getDeclName();
10168           DeclarationName SecondName = SecondDecl->getDeclName();
10169 
10170           if (FirstName != SecondName) {
10171             const bool FirstNameEmpty =
10172                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10173             const bool SecondNameEmpty =
10174                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10175             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10176                    "Both template parameters cannot be unnamed.");
10177             ODRDiagTemplateError(FirstDecl->getLocation(),
10178                                  FirstDecl->getSourceRange(),
10179                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10180                 << FirstName;
10181             ODRDiagTemplateNote(SecondDecl->getLocation(),
10182                                 SecondDecl->getSourceRange(),
10183                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10184                 << SecondName;
10185             break;
10186           }
10187 
10188           switch (FirstDecl->getKind()) {
10189           default:
10190             llvm_unreachable("Invalid template parameter type.");
10191           case Decl::TemplateTypeParm: {
10192             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10193             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10194             const bool HasFirstDefaultArgument =
10195                 FirstParam->hasDefaultArgument() &&
10196                 !FirstParam->defaultArgumentWasInherited();
10197             const bool HasSecondDefaultArgument =
10198                 SecondParam->hasDefaultArgument() &&
10199                 !SecondParam->defaultArgumentWasInherited();
10200 
10201             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10202               ODRDiagTemplateError(FirstDecl->getLocation(),
10203                                    FirstDecl->getSourceRange(),
10204                                    ParamSingleDefaultArgument)
10205                   << HasFirstDefaultArgument;
10206               ODRDiagTemplateNote(SecondDecl->getLocation(),
10207                                   SecondDecl->getSourceRange(),
10208                                   ParamSingleDefaultArgument)
10209                   << HasSecondDefaultArgument;
10210               break;
10211             }
10212 
10213             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10214                    "Expecting default arguments.");
10215 
10216             ODRDiagTemplateError(FirstDecl->getLocation(),
10217                                  FirstDecl->getSourceRange(),
10218                                  ParamDifferentDefaultArgument);
10219             ODRDiagTemplateNote(SecondDecl->getLocation(),
10220                                 SecondDecl->getSourceRange(),
10221                                 ParamDifferentDefaultArgument);
10222 
10223             break;
10224           }
10225           case Decl::NonTypeTemplateParm: {
10226             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10227             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10228             const bool HasFirstDefaultArgument =
10229                 FirstParam->hasDefaultArgument() &&
10230                 !FirstParam->defaultArgumentWasInherited();
10231             const bool HasSecondDefaultArgument =
10232                 SecondParam->hasDefaultArgument() &&
10233                 !SecondParam->defaultArgumentWasInherited();
10234 
10235             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10236               ODRDiagTemplateError(FirstDecl->getLocation(),
10237                                    FirstDecl->getSourceRange(),
10238                                    ParamSingleDefaultArgument)
10239                   << HasFirstDefaultArgument;
10240               ODRDiagTemplateNote(SecondDecl->getLocation(),
10241                                   SecondDecl->getSourceRange(),
10242                                   ParamSingleDefaultArgument)
10243                   << HasSecondDefaultArgument;
10244               break;
10245             }
10246 
10247             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10248                    "Expecting default arguments.");
10249 
10250             ODRDiagTemplateError(FirstDecl->getLocation(),
10251                                  FirstDecl->getSourceRange(),
10252                                  ParamDifferentDefaultArgument);
10253             ODRDiagTemplateNote(SecondDecl->getLocation(),
10254                                 SecondDecl->getSourceRange(),
10255                                 ParamDifferentDefaultArgument);
10256 
10257             break;
10258           }
10259           case Decl::TemplateTemplateParm: {
10260             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10261             const auto *SecondParam =
10262                 cast<TemplateTemplateParmDecl>(SecondDecl);
10263             const bool HasFirstDefaultArgument =
10264                 FirstParam->hasDefaultArgument() &&
10265                 !FirstParam->defaultArgumentWasInherited();
10266             const bool HasSecondDefaultArgument =
10267                 SecondParam->hasDefaultArgument() &&
10268                 !SecondParam->defaultArgumentWasInherited();
10269 
10270             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10271               ODRDiagTemplateError(FirstDecl->getLocation(),
10272                                    FirstDecl->getSourceRange(),
10273                                    ParamSingleDefaultArgument)
10274                   << HasFirstDefaultArgument;
10275               ODRDiagTemplateNote(SecondDecl->getLocation(),
10276                                   SecondDecl->getSourceRange(),
10277                                   ParamSingleDefaultArgument)
10278                   << HasSecondDefaultArgument;
10279               break;
10280             }
10281 
10282             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10283                    "Expecting default arguments.");
10284 
10285             ODRDiagTemplateError(FirstDecl->getLocation(),
10286                                  FirstDecl->getSourceRange(),
10287                                  ParamDifferentDefaultArgument);
10288             ODRDiagTemplateNote(SecondDecl->getLocation(),
10289                                 SecondDecl->getSourceRange(),
10290                                 ParamDifferentDefaultArgument);
10291 
10292             break;
10293           }
10294           }
10295 
10296           break;
10297         }
10298 
10299         if (FirstIt != FirstEnd) {
10300           Diagnosed = true;
10301           break;
10302         }
10303       }
10304 
10305       DeclHashes FirstHashes;
10306       DeclHashes SecondHashes;
10307       const DeclContext *DC = FirstRecord;
10308       PopulateHashes(FirstHashes, FirstRecord, DC);
10309       PopulateHashes(SecondHashes, SecondRecord, DC);
10310 
10311       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10312       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10313       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10314       Decl *FirstDecl = DR.FirstDecl;
10315       Decl *SecondDecl = DR.SecondDecl;
10316 
10317       if (FirstDiffType == Other || SecondDiffType == Other) {
10318         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10319                               SecondModule);
10320         Diagnosed = true;
10321         break;
10322       }
10323 
10324       if (FirstDiffType != SecondDiffType) {
10325         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10326                             SecondModule);
10327         Diagnosed = true;
10328         break;
10329       }
10330 
10331       assert(FirstDiffType == SecondDiffType);
10332 
10333       switch (FirstDiffType) {
10334       case Other:
10335       case EndOfClass:
10336       case PublicSpecifer:
10337       case PrivateSpecifer:
10338       case ProtectedSpecifer:
10339         llvm_unreachable("Invalid diff type");
10340 
10341       case StaticAssert: {
10342         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10343         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10344 
10345         Expr *FirstExpr = FirstSA->getAssertExpr();
10346         Expr *SecondExpr = SecondSA->getAssertExpr();
10347         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10348         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10349         if (FirstODRHash != SecondODRHash) {
10350           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10351                            FirstExpr->getSourceRange(), StaticAssertCondition);
10352           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10353                           SecondExpr->getSourceRange(), StaticAssertCondition);
10354           Diagnosed = true;
10355           break;
10356         }
10357 
10358         StringLiteral *FirstStr = FirstSA->getMessage();
10359         StringLiteral *SecondStr = SecondSA->getMessage();
10360         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10361         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10362           SourceLocation FirstLoc, SecondLoc;
10363           SourceRange FirstRange, SecondRange;
10364           if (FirstStr) {
10365             FirstLoc = FirstStr->getBeginLoc();
10366             FirstRange = FirstStr->getSourceRange();
10367           } else {
10368             FirstLoc = FirstSA->getBeginLoc();
10369             FirstRange = FirstSA->getSourceRange();
10370           }
10371           if (SecondStr) {
10372             SecondLoc = SecondStr->getBeginLoc();
10373             SecondRange = SecondStr->getSourceRange();
10374           } else {
10375             SecondLoc = SecondSA->getBeginLoc();
10376             SecondRange = SecondSA->getSourceRange();
10377           }
10378           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10379                            StaticAssertOnlyMessage)
10380               << (FirstStr == nullptr);
10381           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10382                           StaticAssertOnlyMessage)
10383               << (SecondStr == nullptr);
10384           Diagnosed = true;
10385           break;
10386         }
10387 
10388         if (FirstStr && SecondStr &&
10389             FirstStr->getString() != SecondStr->getString()) {
10390           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10391                            FirstStr->getSourceRange(), StaticAssertMessage);
10392           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10393                           SecondStr->getSourceRange(), StaticAssertMessage);
10394           Diagnosed = true;
10395           break;
10396         }
10397         break;
10398       }
10399       case Field: {
10400         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10401                                  cast<FieldDecl>(FirstDecl),
10402                                  cast<FieldDecl>(SecondDecl));
10403         break;
10404       }
10405       case CXXMethod: {
10406         enum {
10407           DiagMethod,
10408           DiagConstructor,
10409           DiagDestructor,
10410         } FirstMethodType,
10411             SecondMethodType;
10412         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10413           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10414           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10415           return DiagMethod;
10416         };
10417         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10418         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10419         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10420         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10421         auto FirstName = FirstMethod->getDeclName();
10422         auto SecondName = SecondMethod->getDeclName();
10423         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10424           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10425                            FirstMethod->getSourceRange(), MethodName)
10426               << FirstMethodType << FirstName;
10427           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10428                           SecondMethod->getSourceRange(), MethodName)
10429               << SecondMethodType << SecondName;
10430 
10431           Diagnosed = true;
10432           break;
10433         }
10434 
10435         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10436         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10437         if (FirstDeleted != SecondDeleted) {
10438           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10439                            FirstMethod->getSourceRange(), MethodDeleted)
10440               << FirstMethodType << FirstName << FirstDeleted;
10441 
10442           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10443                           SecondMethod->getSourceRange(), MethodDeleted)
10444               << SecondMethodType << SecondName << SecondDeleted;
10445           Diagnosed = true;
10446           break;
10447         }
10448 
10449         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10450         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10451         if (FirstDefaulted != SecondDefaulted) {
10452           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10453                            FirstMethod->getSourceRange(), MethodDefaulted)
10454               << FirstMethodType << FirstName << FirstDefaulted;
10455 
10456           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10457                           SecondMethod->getSourceRange(), MethodDefaulted)
10458               << SecondMethodType << SecondName << SecondDefaulted;
10459           Diagnosed = true;
10460           break;
10461         }
10462 
10463         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10464         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10465         const bool FirstPure = FirstMethod->isPure();
10466         const bool SecondPure = SecondMethod->isPure();
10467         if ((FirstVirtual || SecondVirtual) &&
10468             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10469           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10470                            FirstMethod->getSourceRange(), MethodVirtual)
10471               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10472           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10473                           SecondMethod->getSourceRange(), MethodVirtual)
10474               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10475           Diagnosed = true;
10476           break;
10477         }
10478 
10479         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10480         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10481         // class needs to be checked instead.
10482         const auto FirstStorage = FirstMethod->getStorageClass();
10483         const auto SecondStorage = SecondMethod->getStorageClass();
10484         const bool FirstStatic = FirstStorage == SC_Static;
10485         const bool SecondStatic = SecondStorage == SC_Static;
10486         if (FirstStatic != SecondStatic) {
10487           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10488                            FirstMethod->getSourceRange(), MethodStatic)
10489               << FirstMethodType << FirstName << FirstStatic;
10490           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10491                           SecondMethod->getSourceRange(), MethodStatic)
10492               << SecondMethodType << SecondName << SecondStatic;
10493           Diagnosed = true;
10494           break;
10495         }
10496 
10497         const bool FirstVolatile = FirstMethod->isVolatile();
10498         const bool SecondVolatile = SecondMethod->isVolatile();
10499         if (FirstVolatile != SecondVolatile) {
10500           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10501                            FirstMethod->getSourceRange(), MethodVolatile)
10502               << FirstMethodType << FirstName << FirstVolatile;
10503           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10504                           SecondMethod->getSourceRange(), MethodVolatile)
10505               << SecondMethodType << SecondName << SecondVolatile;
10506           Diagnosed = true;
10507           break;
10508         }
10509 
10510         const bool FirstConst = FirstMethod->isConst();
10511         const bool SecondConst = SecondMethod->isConst();
10512         if (FirstConst != SecondConst) {
10513           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10514                            FirstMethod->getSourceRange(), MethodConst)
10515               << FirstMethodType << FirstName << FirstConst;
10516           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10517                           SecondMethod->getSourceRange(), MethodConst)
10518               << SecondMethodType << SecondName << SecondConst;
10519           Diagnosed = true;
10520           break;
10521         }
10522 
10523         const bool FirstInline = FirstMethod->isInlineSpecified();
10524         const bool SecondInline = SecondMethod->isInlineSpecified();
10525         if (FirstInline != SecondInline) {
10526           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10527                            FirstMethod->getSourceRange(), MethodInline)
10528               << FirstMethodType << FirstName << FirstInline;
10529           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10530                           SecondMethod->getSourceRange(), MethodInline)
10531               << SecondMethodType << SecondName << SecondInline;
10532           Diagnosed = true;
10533           break;
10534         }
10535 
10536         const unsigned FirstNumParameters = FirstMethod->param_size();
10537         const unsigned SecondNumParameters = SecondMethod->param_size();
10538         if (FirstNumParameters != SecondNumParameters) {
10539           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10540                            FirstMethod->getSourceRange(),
10541                            MethodNumberParameters)
10542               << FirstMethodType << FirstName << FirstNumParameters;
10543           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10544                           SecondMethod->getSourceRange(),
10545                           MethodNumberParameters)
10546               << SecondMethodType << SecondName << SecondNumParameters;
10547           Diagnosed = true;
10548           break;
10549         }
10550 
10551         // Need this status boolean to know when break out of the switch.
10552         bool ParameterMismatch = false;
10553         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10554           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10555           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10556 
10557           QualType FirstParamType = FirstParam->getType();
10558           QualType SecondParamType = SecondParam->getType();
10559           if (FirstParamType != SecondParamType &&
10560               ComputeQualTypeODRHash(FirstParamType) !=
10561                   ComputeQualTypeODRHash(SecondParamType)) {
10562             if (const DecayedType *ParamDecayedType =
10563                     FirstParamType->getAs<DecayedType>()) {
10564               ODRDiagDeclError(
10565                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10566                   FirstMethod->getSourceRange(), MethodParameterType)
10567                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10568                   << true << ParamDecayedType->getOriginalType();
10569             } else {
10570               ODRDiagDeclError(
10571                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10572                   FirstMethod->getSourceRange(), MethodParameterType)
10573                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10574                   << false;
10575             }
10576 
10577             if (const DecayedType *ParamDecayedType =
10578                     SecondParamType->getAs<DecayedType>()) {
10579               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10580                               SecondMethod->getSourceRange(),
10581                               MethodParameterType)
10582                   << SecondMethodType << SecondName << (I + 1)
10583                   << SecondParamType << true
10584                   << ParamDecayedType->getOriginalType();
10585             } else {
10586               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10587                               SecondMethod->getSourceRange(),
10588                               MethodParameterType)
10589                   << SecondMethodType << SecondName << (I + 1)
10590                   << SecondParamType << false;
10591             }
10592             ParameterMismatch = true;
10593             break;
10594           }
10595 
10596           DeclarationName FirstParamName = FirstParam->getDeclName();
10597           DeclarationName SecondParamName = SecondParam->getDeclName();
10598           if (FirstParamName != SecondParamName) {
10599             ODRDiagDeclError(FirstRecord, FirstModule,
10600                              FirstMethod->getLocation(),
10601                              FirstMethod->getSourceRange(), MethodParameterName)
10602                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10603             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10604                             SecondMethod->getSourceRange(), MethodParameterName)
10605                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10606             ParameterMismatch = true;
10607             break;
10608           }
10609 
10610           const Expr *FirstInit = FirstParam->getInit();
10611           const Expr *SecondInit = SecondParam->getInit();
10612           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10613             ODRDiagDeclError(FirstRecord, FirstModule,
10614                              FirstMethod->getLocation(),
10615                              FirstMethod->getSourceRange(),
10616                              MethodParameterSingleDefaultArgument)
10617                 << FirstMethodType << FirstName << (I + 1)
10618                 << (FirstInit == nullptr)
10619                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10620             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10621                             SecondMethod->getSourceRange(),
10622                             MethodParameterSingleDefaultArgument)
10623                 << SecondMethodType << SecondName << (I + 1)
10624                 << (SecondInit == nullptr)
10625                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10626             ParameterMismatch = true;
10627             break;
10628           }
10629 
10630           if (FirstInit && SecondInit &&
10631               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10632             ODRDiagDeclError(FirstRecord, FirstModule,
10633                              FirstMethod->getLocation(),
10634                              FirstMethod->getSourceRange(),
10635                              MethodParameterDifferentDefaultArgument)
10636                 << FirstMethodType << FirstName << (I + 1)
10637                 << FirstInit->getSourceRange();
10638             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10639                             SecondMethod->getSourceRange(),
10640                             MethodParameterDifferentDefaultArgument)
10641                 << SecondMethodType << SecondName << (I + 1)
10642                 << SecondInit->getSourceRange();
10643             ParameterMismatch = true;
10644             break;
10645 
10646           }
10647         }
10648 
10649         if (ParameterMismatch) {
10650           Diagnosed = true;
10651           break;
10652         }
10653 
10654         const auto *FirstTemplateArgs =
10655             FirstMethod->getTemplateSpecializationArgs();
10656         const auto *SecondTemplateArgs =
10657             SecondMethod->getTemplateSpecializationArgs();
10658 
10659         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10660             (!FirstTemplateArgs && SecondTemplateArgs)) {
10661           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10662                            FirstMethod->getSourceRange(),
10663                            MethodNoTemplateArguments)
10664               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10665           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10666                           SecondMethod->getSourceRange(),
10667                           MethodNoTemplateArguments)
10668               << SecondMethodType << SecondName
10669               << (SecondTemplateArgs != nullptr);
10670 
10671           Diagnosed = true;
10672           break;
10673         }
10674 
10675         if (FirstTemplateArgs && SecondTemplateArgs) {
10676           // Remove pack expansions from argument list.
10677           auto ExpandTemplateArgumentList =
10678               [](const TemplateArgumentList *TAL) {
10679                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10680                 for (const TemplateArgument &TA : TAL->asArray()) {
10681                   if (TA.getKind() != TemplateArgument::Pack) {
10682                     ExpandedList.push_back(&TA);
10683                     continue;
10684                   }
10685                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10686                     ExpandedList.push_back(&PackTA);
10687                   }
10688                 }
10689                 return ExpandedList;
10690               };
10691           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10692               ExpandTemplateArgumentList(FirstTemplateArgs);
10693           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10694               ExpandTemplateArgumentList(SecondTemplateArgs);
10695 
10696           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10697             ODRDiagDeclError(FirstRecord, FirstModule,
10698                              FirstMethod->getLocation(),
10699                              FirstMethod->getSourceRange(),
10700                              MethodDifferentNumberTemplateArguments)
10701                 << FirstMethodType << FirstName
10702                 << (unsigned)FirstExpandedList.size();
10703             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10704                             SecondMethod->getSourceRange(),
10705                             MethodDifferentNumberTemplateArguments)
10706                 << SecondMethodType << SecondName
10707                 << (unsigned)SecondExpandedList.size();
10708 
10709             Diagnosed = true;
10710             break;
10711           }
10712 
10713           bool TemplateArgumentMismatch = false;
10714           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10715             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10716                                    &SecondTA = *SecondExpandedList[i];
10717             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10718                 ComputeTemplateArgumentODRHash(SecondTA)) {
10719               continue;
10720             }
10721 
10722             ODRDiagDeclError(
10723                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10724                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10725                 << FirstMethodType << FirstName << FirstTA << i + 1;
10726             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10727                             SecondMethod->getSourceRange(),
10728                             MethodDifferentTemplateArgument)
10729                 << SecondMethodType << SecondName << SecondTA << i + 1;
10730 
10731             TemplateArgumentMismatch = true;
10732             break;
10733           }
10734 
10735           if (TemplateArgumentMismatch) {
10736             Diagnosed = true;
10737             break;
10738           }
10739         }
10740 
10741         // Compute the hash of the method as if it has no body.
10742         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10743           Hash.clear();
10744           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10745           return Hash.CalculateHash();
10746         };
10747 
10748         // Compare the hash generated to the hash stored.  A difference means
10749         // that a body was present in the original source.  Due to merging,
10750         // the stardard way of detecting a body will not work.
10751         const bool HasFirstBody =
10752             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10753         const bool HasSecondBody =
10754             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10755 
10756         if (HasFirstBody != HasSecondBody) {
10757           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10758                            FirstMethod->getSourceRange(), MethodSingleBody)
10759               << FirstMethodType << FirstName << HasFirstBody;
10760           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10761                           SecondMethod->getSourceRange(), MethodSingleBody)
10762               << SecondMethodType << SecondName << HasSecondBody;
10763           Diagnosed = true;
10764           break;
10765         }
10766 
10767         if (HasFirstBody && HasSecondBody) {
10768           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10769                            FirstMethod->getSourceRange(), MethodDifferentBody)
10770               << FirstMethodType << FirstName;
10771           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10772                           SecondMethod->getSourceRange(), MethodDifferentBody)
10773               << SecondMethodType << SecondName;
10774           Diagnosed = true;
10775           break;
10776         }
10777 
10778         break;
10779       }
10780       case TypeAlias:
10781       case TypeDef: {
10782         Diagnosed = ODRDiagTypeDefOrAlias(
10783             FirstRecord, FirstModule, SecondModule,
10784             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10785             FirstDiffType == TypeAlias);
10786         break;
10787       }
10788       case Var: {
10789         Diagnosed =
10790             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10791                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10792         break;
10793       }
10794       case Friend: {
10795         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10796         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10797 
10798         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10799         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10800 
10801         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10802         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10803 
10804         if (FirstND && SecondND) {
10805           ODRDiagDeclError(FirstRecord, FirstModule,
10806                            FirstFriend->getFriendLoc(),
10807                            FirstFriend->getSourceRange(), FriendFunction)
10808               << FirstND;
10809           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10810                           SecondFriend->getSourceRange(), FriendFunction)
10811               << SecondND;
10812 
10813           Diagnosed = true;
10814           break;
10815         }
10816 
10817         if (FirstTSI && SecondTSI) {
10818           QualType FirstFriendType = FirstTSI->getType();
10819           QualType SecondFriendType = SecondTSI->getType();
10820           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10821                  ComputeQualTypeODRHash(SecondFriendType));
10822           ODRDiagDeclError(FirstRecord, FirstModule,
10823                            FirstFriend->getFriendLoc(),
10824                            FirstFriend->getSourceRange(), FriendType)
10825               << FirstFriendType;
10826           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10827                           SecondFriend->getSourceRange(), FriendType)
10828               << SecondFriendType;
10829           Diagnosed = true;
10830           break;
10831         }
10832 
10833         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10834                          FirstFriend->getSourceRange(), FriendTypeFunction)
10835             << (FirstTSI == nullptr);
10836         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10837                         SecondFriend->getSourceRange(), FriendTypeFunction)
10838             << (SecondTSI == nullptr);
10839 
10840         Diagnosed = true;
10841         break;
10842       }
10843       case FunctionTemplate: {
10844         FunctionTemplateDecl *FirstTemplate =
10845             cast<FunctionTemplateDecl>(FirstDecl);
10846         FunctionTemplateDecl *SecondTemplate =
10847             cast<FunctionTemplateDecl>(SecondDecl);
10848 
10849         TemplateParameterList *FirstTPL =
10850             FirstTemplate->getTemplateParameters();
10851         TemplateParameterList *SecondTPL =
10852             SecondTemplate->getTemplateParameters();
10853 
10854         if (FirstTPL->size() != SecondTPL->size()) {
10855           ODRDiagDeclError(FirstRecord, FirstModule,
10856                            FirstTemplate->getLocation(),
10857                            FirstTemplate->getSourceRange(),
10858                            FunctionTemplateDifferentNumberParameters)
10859               << FirstTemplate << FirstTPL->size();
10860           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10861                           SecondTemplate->getSourceRange(),
10862                           FunctionTemplateDifferentNumberParameters)
10863               << SecondTemplate << SecondTPL->size();
10864 
10865           Diagnosed = true;
10866           break;
10867         }
10868 
10869         bool ParameterMismatch = false;
10870         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10871           NamedDecl *FirstParam = FirstTPL->getParam(i);
10872           NamedDecl *SecondParam = SecondTPL->getParam(i);
10873 
10874           if (FirstParam->getKind() != SecondParam->getKind()) {
10875             enum {
10876               TemplateTypeParameter,
10877               NonTypeTemplateParameter,
10878               TemplateTemplateParameter,
10879             };
10880             auto GetParamType = [](NamedDecl *D) {
10881               switch (D->getKind()) {
10882                 default:
10883                   llvm_unreachable("Unexpected template parameter type");
10884                 case Decl::TemplateTypeParm:
10885                   return TemplateTypeParameter;
10886                 case Decl::NonTypeTemplateParm:
10887                   return NonTypeTemplateParameter;
10888                 case Decl::TemplateTemplateParm:
10889                   return TemplateTemplateParameter;
10890               }
10891             };
10892 
10893             ODRDiagDeclError(FirstRecord, FirstModule,
10894                              FirstTemplate->getLocation(),
10895                              FirstTemplate->getSourceRange(),
10896                              FunctionTemplateParameterDifferentKind)
10897                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10898             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10899                             SecondTemplate->getSourceRange(),
10900                             FunctionTemplateParameterDifferentKind)
10901                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10902 
10903             ParameterMismatch = true;
10904             break;
10905           }
10906 
10907           if (FirstParam->getName() != SecondParam->getName()) {
10908             ODRDiagDeclError(
10909                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10910                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10911                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10912                 << FirstParam;
10913             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10914                             SecondTemplate->getSourceRange(),
10915                             FunctionTemplateParameterName)
10916                 << SecondTemplate << (i + 1)
10917                 << (bool)SecondParam->getIdentifier() << SecondParam;
10918             ParameterMismatch = true;
10919             break;
10920           }
10921 
10922           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10923               isa<TemplateTypeParmDecl>(SecondParam)) {
10924             TemplateTypeParmDecl *FirstTTPD =
10925                 cast<TemplateTypeParmDecl>(FirstParam);
10926             TemplateTypeParmDecl *SecondTTPD =
10927                 cast<TemplateTypeParmDecl>(SecondParam);
10928             bool HasFirstDefaultArgument =
10929                 FirstTTPD->hasDefaultArgument() &&
10930                 !FirstTTPD->defaultArgumentWasInherited();
10931             bool HasSecondDefaultArgument =
10932                 SecondTTPD->hasDefaultArgument() &&
10933                 !SecondTTPD->defaultArgumentWasInherited();
10934             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10935               ODRDiagDeclError(FirstRecord, FirstModule,
10936                                FirstTemplate->getLocation(),
10937                                FirstTemplate->getSourceRange(),
10938                                FunctionTemplateParameterSingleDefaultArgument)
10939                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10940               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10941                               SecondTemplate->getSourceRange(),
10942                               FunctionTemplateParameterSingleDefaultArgument)
10943                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10944               ParameterMismatch = true;
10945               break;
10946             }
10947 
10948             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10949               QualType FirstType = FirstTTPD->getDefaultArgument();
10950               QualType SecondType = SecondTTPD->getDefaultArgument();
10951               if (ComputeQualTypeODRHash(FirstType) !=
10952                   ComputeQualTypeODRHash(SecondType)) {
10953                 ODRDiagDeclError(
10954                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10955                     FirstTemplate->getSourceRange(),
10956                     FunctionTemplateParameterDifferentDefaultArgument)
10957                     << FirstTemplate << (i + 1) << FirstType;
10958                 ODRDiagDeclNote(
10959                     SecondModule, SecondTemplate->getLocation(),
10960                     SecondTemplate->getSourceRange(),
10961                     FunctionTemplateParameterDifferentDefaultArgument)
10962                     << SecondTemplate << (i + 1) << SecondType;
10963                 ParameterMismatch = true;
10964                 break;
10965               }
10966             }
10967 
10968             if (FirstTTPD->isParameterPack() !=
10969                 SecondTTPD->isParameterPack()) {
10970               ODRDiagDeclError(FirstRecord, FirstModule,
10971                                FirstTemplate->getLocation(),
10972                                FirstTemplate->getSourceRange(),
10973                                FunctionTemplatePackParameter)
10974                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10975               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10976                               SecondTemplate->getSourceRange(),
10977                               FunctionTemplatePackParameter)
10978                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10979               ParameterMismatch = true;
10980               break;
10981             }
10982           }
10983 
10984           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10985               isa<TemplateTemplateParmDecl>(SecondParam)) {
10986             TemplateTemplateParmDecl *FirstTTPD =
10987                 cast<TemplateTemplateParmDecl>(FirstParam);
10988             TemplateTemplateParmDecl *SecondTTPD =
10989                 cast<TemplateTemplateParmDecl>(SecondParam);
10990 
10991             TemplateParameterList *FirstTPL =
10992                 FirstTTPD->getTemplateParameters();
10993             TemplateParameterList *SecondTPL =
10994                 SecondTTPD->getTemplateParameters();
10995 
10996             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10997                 ComputeTemplateParameterListODRHash(SecondTPL)) {
10998               ODRDiagDeclError(FirstRecord, FirstModule,
10999                                FirstTemplate->getLocation(),
11000                                FirstTemplate->getSourceRange(),
11001                                FunctionTemplateParameterDifferentType)
11002                   << FirstTemplate << (i + 1);
11003               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11004                               SecondTemplate->getSourceRange(),
11005                               FunctionTemplateParameterDifferentType)
11006                   << SecondTemplate << (i + 1);
11007               ParameterMismatch = true;
11008               break;
11009             }
11010 
11011             bool HasFirstDefaultArgument =
11012                 FirstTTPD->hasDefaultArgument() &&
11013                 !FirstTTPD->defaultArgumentWasInherited();
11014             bool HasSecondDefaultArgument =
11015                 SecondTTPD->hasDefaultArgument() &&
11016                 !SecondTTPD->defaultArgumentWasInherited();
11017             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11018               ODRDiagDeclError(FirstRecord, FirstModule,
11019                                FirstTemplate->getLocation(),
11020                                FirstTemplate->getSourceRange(),
11021                                FunctionTemplateParameterSingleDefaultArgument)
11022                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11023               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11024                               SecondTemplate->getSourceRange(),
11025                               FunctionTemplateParameterSingleDefaultArgument)
11026                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11027               ParameterMismatch = true;
11028               break;
11029             }
11030 
11031             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11032               TemplateArgument FirstTA =
11033                   FirstTTPD->getDefaultArgument().getArgument();
11034               TemplateArgument SecondTA =
11035                   SecondTTPD->getDefaultArgument().getArgument();
11036               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11037                   ComputeTemplateArgumentODRHash(SecondTA)) {
11038                 ODRDiagDeclError(
11039                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11040                     FirstTemplate->getSourceRange(),
11041                     FunctionTemplateParameterDifferentDefaultArgument)
11042                     << FirstTemplate << (i + 1) << FirstTA;
11043                 ODRDiagDeclNote(
11044                     SecondModule, SecondTemplate->getLocation(),
11045                     SecondTemplate->getSourceRange(),
11046                     FunctionTemplateParameterDifferentDefaultArgument)
11047                     << SecondTemplate << (i + 1) << SecondTA;
11048                 ParameterMismatch = true;
11049                 break;
11050               }
11051             }
11052 
11053             if (FirstTTPD->isParameterPack() !=
11054                 SecondTTPD->isParameterPack()) {
11055               ODRDiagDeclError(FirstRecord, FirstModule,
11056                                FirstTemplate->getLocation(),
11057                                FirstTemplate->getSourceRange(),
11058                                FunctionTemplatePackParameter)
11059                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11060               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11061                               SecondTemplate->getSourceRange(),
11062                               FunctionTemplatePackParameter)
11063                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11064               ParameterMismatch = true;
11065               break;
11066             }
11067           }
11068 
11069           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11070               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11071             NonTypeTemplateParmDecl *FirstNTTPD =
11072                 cast<NonTypeTemplateParmDecl>(FirstParam);
11073             NonTypeTemplateParmDecl *SecondNTTPD =
11074                 cast<NonTypeTemplateParmDecl>(SecondParam);
11075 
11076             QualType FirstType = FirstNTTPD->getType();
11077             QualType SecondType = SecondNTTPD->getType();
11078             if (ComputeQualTypeODRHash(FirstType) !=
11079                 ComputeQualTypeODRHash(SecondType)) {
11080               ODRDiagDeclError(FirstRecord, FirstModule,
11081                                FirstTemplate->getLocation(),
11082                                FirstTemplate->getSourceRange(),
11083                                FunctionTemplateParameterDifferentType)
11084                   << FirstTemplate << (i + 1);
11085               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11086                               SecondTemplate->getSourceRange(),
11087                               FunctionTemplateParameterDifferentType)
11088                   << SecondTemplate << (i + 1);
11089               ParameterMismatch = true;
11090               break;
11091             }
11092 
11093             bool HasFirstDefaultArgument =
11094                 FirstNTTPD->hasDefaultArgument() &&
11095                 !FirstNTTPD->defaultArgumentWasInherited();
11096             bool HasSecondDefaultArgument =
11097                 SecondNTTPD->hasDefaultArgument() &&
11098                 !SecondNTTPD->defaultArgumentWasInherited();
11099             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11100               ODRDiagDeclError(FirstRecord, FirstModule,
11101                                FirstTemplate->getLocation(),
11102                                FirstTemplate->getSourceRange(),
11103                                FunctionTemplateParameterSingleDefaultArgument)
11104                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11105               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11106                               SecondTemplate->getSourceRange(),
11107                               FunctionTemplateParameterSingleDefaultArgument)
11108                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11109               ParameterMismatch = true;
11110               break;
11111             }
11112 
11113             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11114               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11115               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11116               if (ComputeODRHash(FirstDefaultArgument) !=
11117                   ComputeODRHash(SecondDefaultArgument)) {
11118                 ODRDiagDeclError(
11119                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11120                     FirstTemplate->getSourceRange(),
11121                     FunctionTemplateParameterDifferentDefaultArgument)
11122                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11123                 ODRDiagDeclNote(
11124                     SecondModule, SecondTemplate->getLocation(),
11125                     SecondTemplate->getSourceRange(),
11126                     FunctionTemplateParameterDifferentDefaultArgument)
11127                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11128                 ParameterMismatch = true;
11129                 break;
11130               }
11131             }
11132 
11133             if (FirstNTTPD->isParameterPack() !=
11134                 SecondNTTPD->isParameterPack()) {
11135               ODRDiagDeclError(FirstRecord, FirstModule,
11136                                FirstTemplate->getLocation(),
11137                                FirstTemplate->getSourceRange(),
11138                                FunctionTemplatePackParameter)
11139                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11140               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11141                               SecondTemplate->getSourceRange(),
11142                               FunctionTemplatePackParameter)
11143                   << SecondTemplate << (i + 1)
11144                   << SecondNTTPD->isParameterPack();
11145               ParameterMismatch = true;
11146               break;
11147             }
11148           }
11149         }
11150 
11151         if (ParameterMismatch) {
11152           Diagnosed = true;
11153           break;
11154         }
11155 
11156         break;
11157       }
11158       }
11159 
11160       if (Diagnosed)
11161         continue;
11162 
11163       Diag(FirstDecl->getLocation(),
11164            diag::err_module_odr_violation_mismatch_decl_unknown)
11165           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11166           << FirstDecl->getSourceRange();
11167       Diag(SecondDecl->getLocation(),
11168            diag::note_module_odr_violation_mismatch_decl_unknown)
11169           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11170       Diagnosed = true;
11171     }
11172 
11173     if (!Diagnosed) {
11174       // All definitions are updates to the same declaration. This happens if a
11175       // module instantiates the declaration of a class template specialization
11176       // and two or more other modules instantiate its definition.
11177       //
11178       // FIXME: Indicate which modules had instantiations of this definition.
11179       // FIXME: How can this even happen?
11180       Diag(Merge.first->getLocation(),
11181            diag::err_module_odr_violation_different_instantiations)
11182         << Merge.first;
11183     }
11184   }
11185 
11186   // Issue ODR failures diagnostics for functions.
11187   for (auto &Merge : FunctionOdrMergeFailures) {
11188     enum ODRFunctionDifference {
11189       ReturnType,
11190       ParameterName,
11191       ParameterType,
11192       ParameterSingleDefaultArgument,
11193       ParameterDifferentDefaultArgument,
11194       FunctionBody,
11195     };
11196 
11197     FunctionDecl *FirstFunction = Merge.first;
11198     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11199 
11200     bool Diagnosed = false;
11201     for (auto &SecondFunction : Merge.second) {
11202 
11203       if (FirstFunction == SecondFunction)
11204         continue;
11205 
11206       std::string SecondModule =
11207           getOwningModuleNameForDiagnostic(SecondFunction);
11208 
11209       auto ODRDiagError = [FirstFunction, &FirstModule,
11210                            this](SourceLocation Loc, SourceRange Range,
11211                                  ODRFunctionDifference DiffType) {
11212         return Diag(Loc, diag::err_module_odr_violation_function)
11213                << FirstFunction << FirstModule.empty() << FirstModule << Range
11214                << DiffType;
11215       };
11216       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11217                                                SourceRange Range,
11218                                                ODRFunctionDifference DiffType) {
11219         return Diag(Loc, diag::note_module_odr_violation_function)
11220                << SecondModule << Range << DiffType;
11221       };
11222 
11223       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11224           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11225         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11226                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11227             << FirstFunction->getReturnType();
11228         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11229                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11230             << SecondFunction->getReturnType();
11231         Diagnosed = true;
11232         break;
11233       }
11234 
11235       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11236              "Merged functions with different number of parameters");
11237 
11238       auto ParamSize = FirstFunction->param_size();
11239       bool ParameterMismatch = false;
11240       for (unsigned I = 0; I < ParamSize; ++I) {
11241         auto *FirstParam = FirstFunction->getParamDecl(I);
11242         auto *SecondParam = SecondFunction->getParamDecl(I);
11243 
11244         assert(getContext().hasSameType(FirstParam->getType(),
11245                                       SecondParam->getType()) &&
11246                "Merged function has different parameter types.");
11247 
11248         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11249           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11250                        ParameterName)
11251               << I + 1 << FirstParam->getDeclName();
11252           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11253                       ParameterName)
11254               << I + 1 << SecondParam->getDeclName();
11255           ParameterMismatch = true;
11256           break;
11257         };
11258 
11259         QualType FirstParamType = FirstParam->getType();
11260         QualType SecondParamType = SecondParam->getType();
11261         if (FirstParamType != SecondParamType &&
11262             ComputeQualTypeODRHash(FirstParamType) !=
11263                 ComputeQualTypeODRHash(SecondParamType)) {
11264           if (const DecayedType *ParamDecayedType =
11265                   FirstParamType->getAs<DecayedType>()) {
11266             ODRDiagError(FirstParam->getLocation(),
11267                          FirstParam->getSourceRange(), ParameterType)
11268                 << (I + 1) << FirstParamType << true
11269                 << ParamDecayedType->getOriginalType();
11270           } else {
11271             ODRDiagError(FirstParam->getLocation(),
11272                          FirstParam->getSourceRange(), ParameterType)
11273                 << (I + 1) << FirstParamType << false;
11274           }
11275 
11276           if (const DecayedType *ParamDecayedType =
11277                   SecondParamType->getAs<DecayedType>()) {
11278             ODRDiagNote(SecondParam->getLocation(),
11279                         SecondParam->getSourceRange(), ParameterType)
11280                 << (I + 1) << SecondParamType << true
11281                 << ParamDecayedType->getOriginalType();
11282           } else {
11283             ODRDiagNote(SecondParam->getLocation(),
11284                         SecondParam->getSourceRange(), ParameterType)
11285                 << (I + 1) << SecondParamType << false;
11286           }
11287           ParameterMismatch = true;
11288           break;
11289         }
11290 
11291         const Expr *FirstInit = FirstParam->getInit();
11292         const Expr *SecondInit = SecondParam->getInit();
11293         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11294           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11295                        ParameterSingleDefaultArgument)
11296               << (I + 1) << (FirstInit == nullptr)
11297               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11298           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11299                       ParameterSingleDefaultArgument)
11300               << (I + 1) << (SecondInit == nullptr)
11301               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11302           ParameterMismatch = true;
11303           break;
11304         }
11305 
11306         if (FirstInit && SecondInit &&
11307             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11308           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11309                        ParameterDifferentDefaultArgument)
11310               << (I + 1) << FirstInit->getSourceRange();
11311           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11312                       ParameterDifferentDefaultArgument)
11313               << (I + 1) << SecondInit->getSourceRange();
11314           ParameterMismatch = true;
11315           break;
11316         }
11317 
11318         assert(ComputeSubDeclODRHash(FirstParam) ==
11319                    ComputeSubDeclODRHash(SecondParam) &&
11320                "Undiagnosed parameter difference.");
11321       }
11322 
11323       if (ParameterMismatch) {
11324         Diagnosed = true;
11325         break;
11326       }
11327 
11328       // If no error has been generated before now, assume the problem is in
11329       // the body and generate a message.
11330       ODRDiagError(FirstFunction->getLocation(),
11331                    FirstFunction->getSourceRange(), FunctionBody);
11332       ODRDiagNote(SecondFunction->getLocation(),
11333                   SecondFunction->getSourceRange(), FunctionBody);
11334       Diagnosed = true;
11335       break;
11336     }
11337     (void)Diagnosed;
11338     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11339   }
11340 
11341   // Issue ODR failures diagnostics for enums.
11342   for (auto &Merge : EnumOdrMergeFailures) {
11343     enum ODREnumDifference {
11344       SingleScopedEnum,
11345       EnumTagKeywordMismatch,
11346       SingleSpecifiedType,
11347       DifferentSpecifiedTypes,
11348       DifferentNumberEnumConstants,
11349       EnumConstantName,
11350       EnumConstantSingleInitilizer,
11351       EnumConstantDifferentInitilizer,
11352     };
11353 
11354     // If we've already pointed out a specific problem with this enum, don't
11355     // bother issuing a general "something's different" diagnostic.
11356     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11357       continue;
11358 
11359     EnumDecl *FirstEnum = Merge.first;
11360     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11361 
11362     using DeclHashes =
11363         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11364     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11365                               DeclHashes &Hashes, EnumDecl *Enum) {
11366       for (auto *D : Enum->decls()) {
11367         // Due to decl merging, the first EnumDecl is the parent of
11368         // Decls in both records.
11369         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11370           continue;
11371         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11372         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11373                             ComputeSubDeclODRHash(D));
11374       }
11375     };
11376     DeclHashes FirstHashes;
11377     PopulateHashes(FirstHashes, FirstEnum);
11378     bool Diagnosed = false;
11379     for (auto &SecondEnum : Merge.second) {
11380 
11381       if (FirstEnum == SecondEnum)
11382         continue;
11383 
11384       std::string SecondModule =
11385           getOwningModuleNameForDiagnostic(SecondEnum);
11386 
11387       auto ODRDiagError = [FirstEnum, &FirstModule,
11388                            this](SourceLocation Loc, SourceRange Range,
11389                                  ODREnumDifference DiffType) {
11390         return Diag(Loc, diag::err_module_odr_violation_enum)
11391                << FirstEnum << FirstModule.empty() << FirstModule << Range
11392                << DiffType;
11393       };
11394       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11395                                                SourceRange Range,
11396                                                ODREnumDifference DiffType) {
11397         return Diag(Loc, diag::note_module_odr_violation_enum)
11398                << SecondModule << Range << DiffType;
11399       };
11400 
11401       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11402         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11403                      SingleScopedEnum)
11404             << FirstEnum->isScoped();
11405         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11406                     SingleScopedEnum)
11407             << SecondEnum->isScoped();
11408         Diagnosed = true;
11409         continue;
11410       }
11411 
11412       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11413         if (FirstEnum->isScopedUsingClassTag() !=
11414             SecondEnum->isScopedUsingClassTag()) {
11415           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11416                        EnumTagKeywordMismatch)
11417               << FirstEnum->isScopedUsingClassTag();
11418           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11419                       EnumTagKeywordMismatch)
11420               << SecondEnum->isScopedUsingClassTag();
11421           Diagnosed = true;
11422           continue;
11423         }
11424       }
11425 
11426       QualType FirstUnderlyingType =
11427           FirstEnum->getIntegerTypeSourceInfo()
11428               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11429               : QualType();
11430       QualType SecondUnderlyingType =
11431           SecondEnum->getIntegerTypeSourceInfo()
11432               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11433               : QualType();
11434       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11435           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11436                        SingleSpecifiedType)
11437               << !FirstUnderlyingType.isNull();
11438           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11439                       SingleSpecifiedType)
11440               << !SecondUnderlyingType.isNull();
11441           Diagnosed = true;
11442           continue;
11443       }
11444 
11445       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11446         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11447             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11448           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11449                        DifferentSpecifiedTypes)
11450               << FirstUnderlyingType;
11451           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11452                       DifferentSpecifiedTypes)
11453               << SecondUnderlyingType;
11454           Diagnosed = true;
11455           continue;
11456         }
11457       }
11458 
11459       DeclHashes SecondHashes;
11460       PopulateHashes(SecondHashes, SecondEnum);
11461 
11462       if (FirstHashes.size() != SecondHashes.size()) {
11463         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11464                      DifferentNumberEnumConstants)
11465             << (int)FirstHashes.size();
11466         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11467                     DifferentNumberEnumConstants)
11468             << (int)SecondHashes.size();
11469         Diagnosed = true;
11470         continue;
11471       }
11472 
11473       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11474         if (FirstHashes[I].second == SecondHashes[I].second)
11475           continue;
11476         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11477         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11478 
11479         if (FirstEnumConstant->getDeclName() !=
11480             SecondEnumConstant->getDeclName()) {
11481 
11482           ODRDiagError(FirstEnumConstant->getLocation(),
11483                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11484               << I + 1 << FirstEnumConstant;
11485           ODRDiagNote(SecondEnumConstant->getLocation(),
11486                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11487               << I + 1 << SecondEnumConstant;
11488           Diagnosed = true;
11489           break;
11490         }
11491 
11492         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11493         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11494         if (!FirstInit && !SecondInit)
11495           continue;
11496 
11497         if (!FirstInit || !SecondInit) {
11498           ODRDiagError(FirstEnumConstant->getLocation(),
11499                        FirstEnumConstant->getSourceRange(),
11500                        EnumConstantSingleInitilizer)
11501               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11502           ODRDiagNote(SecondEnumConstant->getLocation(),
11503                       SecondEnumConstant->getSourceRange(),
11504                       EnumConstantSingleInitilizer)
11505               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11506           Diagnosed = true;
11507           break;
11508         }
11509 
11510         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11511           ODRDiagError(FirstEnumConstant->getLocation(),
11512                        FirstEnumConstant->getSourceRange(),
11513                        EnumConstantDifferentInitilizer)
11514               << I + 1 << FirstEnumConstant;
11515           ODRDiagNote(SecondEnumConstant->getLocation(),
11516                       SecondEnumConstant->getSourceRange(),
11517                       EnumConstantDifferentInitilizer)
11518               << I + 1 << SecondEnumConstant;
11519           Diagnosed = true;
11520           break;
11521         }
11522       }
11523     }
11524 
11525     (void)Diagnosed;
11526     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11527   }
11528 }
11529 
11530 void ASTReader::StartedDeserializing() {
11531   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11532     ReadTimer->startTimer();
11533 }
11534 
11535 void ASTReader::FinishedDeserializing() {
11536   assert(NumCurrentElementsDeserializing &&
11537          "FinishedDeserializing not paired with StartedDeserializing");
11538   if (NumCurrentElementsDeserializing == 1) {
11539     // We decrease NumCurrentElementsDeserializing only after pending actions
11540     // are finished, to avoid recursively re-calling finishPendingActions().
11541     finishPendingActions();
11542   }
11543   --NumCurrentElementsDeserializing;
11544 
11545   if (NumCurrentElementsDeserializing == 0) {
11546     // Propagate exception specification and deduced type updates along
11547     // redeclaration chains.
11548     //
11549     // We do this now rather than in finishPendingActions because we want to
11550     // be able to walk the complete redeclaration chains of the updated decls.
11551     while (!PendingExceptionSpecUpdates.empty() ||
11552            !PendingDeducedTypeUpdates.empty()) {
11553       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11554       PendingExceptionSpecUpdates.clear();
11555       for (auto Update : ESUpdates) {
11556         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11557         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11558         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11559         if (auto *Listener = getContext().getASTMutationListener())
11560           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11561         for (auto *Redecl : Update.second->redecls())
11562           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11563       }
11564 
11565       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11566       PendingDeducedTypeUpdates.clear();
11567       for (auto Update : DTUpdates) {
11568         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11569         // FIXME: If the return type is already deduced, check that it matches.
11570         getContext().adjustDeducedFunctionResultType(Update.first,
11571                                                      Update.second);
11572       }
11573     }
11574 
11575     if (ReadTimer)
11576       ReadTimer->stopTimer();
11577 
11578     diagnoseOdrViolations();
11579 
11580     // We are not in recursive loading, so it's safe to pass the "interesting"
11581     // decls to the consumer.
11582     if (Consumer)
11583       PassInterestingDeclsToConsumer();
11584   }
11585 }
11586 
11587 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11588   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11589     // Remove any fake results before adding any real ones.
11590     auto It = PendingFakeLookupResults.find(II);
11591     if (It != PendingFakeLookupResults.end()) {
11592       for (auto *ND : It->second)
11593         SemaObj->IdResolver.RemoveDecl(ND);
11594       // FIXME: this works around module+PCH performance issue.
11595       // Rather than erase the result from the map, which is O(n), just clear
11596       // the vector of NamedDecls.
11597       It->second.clear();
11598     }
11599   }
11600 
11601   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11602     SemaObj->TUScope->AddDecl(D);
11603   } else if (SemaObj->TUScope) {
11604     // Adding the decl to IdResolver may have failed because it was already in
11605     // (even though it was not added in scope). If it is already in, make sure
11606     // it gets in the scope as well.
11607     if (std::find(SemaObj->IdResolver.begin(Name),
11608                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11609       SemaObj->TUScope->AddDecl(D);
11610   }
11611 }
11612 
11613 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11614                      ASTContext *Context,
11615                      const PCHContainerReader &PCHContainerRdr,
11616                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11617                      StringRef isysroot,
11618                      DisableValidationForModuleKind DisableValidationKind,
11619                      bool AllowASTWithCompilerErrors,
11620                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11621                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11622                      std::unique_ptr<llvm::Timer> ReadTimer)
11623     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11624                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11625                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11626       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11627       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11628       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11629                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11630       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11631       DisableValidationKind(DisableValidationKind),
11632       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11633       AllowConfigurationMismatch(AllowConfigurationMismatch),
11634       ValidateSystemInputs(ValidateSystemInputs),
11635       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11636       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11637   SourceMgr.setExternalSLocEntrySource(this);
11638 
11639   for (const auto &Ext : Extensions) {
11640     auto BlockName = Ext->getExtensionMetadata().BlockName;
11641     auto Known = ModuleFileExtensions.find(BlockName);
11642     if (Known != ModuleFileExtensions.end()) {
11643       Diags.Report(diag::warn_duplicate_module_file_extension)
11644         << BlockName;
11645       continue;
11646     }
11647 
11648     ModuleFileExtensions.insert({BlockName, Ext});
11649   }
11650 }
11651 
11652 ASTReader::~ASTReader() {
11653   if (OwnsDeserializationListener)
11654     delete DeserializationListener;
11655 }
11656 
11657 IdentifierResolver &ASTReader::getIdResolver() {
11658   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11659 }
11660 
11661 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11662                                                unsigned AbbrevID) {
11663   Idx = 0;
11664   Record.clear();
11665   return Cursor.readRecord(AbbrevID, Record);
11666 }
11667 //===----------------------------------------------------------------------===//
11668 //// OMPClauseReader implementation
11669 ////===----------------------------------------------------------------------===//
11670 
11671 // This has to be in namespace clang because it's friended by all
11672 // of the OMP clauses.
11673 namespace clang {
11674 
11675 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11676   ASTRecordReader &Record;
11677   ASTContext &Context;
11678 
11679 public:
11680   OMPClauseReader(ASTRecordReader &Record)
11681       : Record(Record), Context(Record.getContext()) {}
11682 #define GEN_CLANG_CLAUSE_CLASS
11683 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11684 #include "llvm/Frontend/OpenMP/OMP.inc"
11685   OMPClause *readClause();
11686   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11687   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11688 };
11689 
11690 } // end namespace clang
11691 
11692 OMPClause *ASTRecordReader::readOMPClause() {
11693   return OMPClauseReader(*this).readClause();
11694 }
11695 
11696 OMPClause *OMPClauseReader::readClause() {
11697   OMPClause *C = nullptr;
11698   switch (llvm::omp::Clause(Record.readInt())) {
11699   case llvm::omp::OMPC_if:
11700     C = new (Context) OMPIfClause();
11701     break;
11702   case llvm::omp::OMPC_final:
11703     C = new (Context) OMPFinalClause();
11704     break;
11705   case llvm::omp::OMPC_num_threads:
11706     C = new (Context) OMPNumThreadsClause();
11707     break;
11708   case llvm::omp::OMPC_safelen:
11709     C = new (Context) OMPSafelenClause();
11710     break;
11711   case llvm::omp::OMPC_simdlen:
11712     C = new (Context) OMPSimdlenClause();
11713     break;
11714   case llvm::omp::OMPC_sizes: {
11715     unsigned NumSizes = Record.readInt();
11716     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11717     break;
11718   }
11719   case llvm::omp::OMPC_full:
11720     C = OMPFullClause::CreateEmpty(Context);
11721     break;
11722   case llvm::omp::OMPC_partial:
11723     C = OMPPartialClause::CreateEmpty(Context);
11724     break;
11725   case llvm::omp::OMPC_allocator:
11726     C = new (Context) OMPAllocatorClause();
11727     break;
11728   case llvm::omp::OMPC_collapse:
11729     C = new (Context) OMPCollapseClause();
11730     break;
11731   case llvm::omp::OMPC_default:
11732     C = new (Context) OMPDefaultClause();
11733     break;
11734   case llvm::omp::OMPC_proc_bind:
11735     C = new (Context) OMPProcBindClause();
11736     break;
11737   case llvm::omp::OMPC_schedule:
11738     C = new (Context) OMPScheduleClause();
11739     break;
11740   case llvm::omp::OMPC_ordered:
11741     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11742     break;
11743   case llvm::omp::OMPC_nowait:
11744     C = new (Context) OMPNowaitClause();
11745     break;
11746   case llvm::omp::OMPC_untied:
11747     C = new (Context) OMPUntiedClause();
11748     break;
11749   case llvm::omp::OMPC_mergeable:
11750     C = new (Context) OMPMergeableClause();
11751     break;
11752   case llvm::omp::OMPC_read:
11753     C = new (Context) OMPReadClause();
11754     break;
11755   case llvm::omp::OMPC_write:
11756     C = new (Context) OMPWriteClause();
11757     break;
11758   case llvm::omp::OMPC_update:
11759     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11760     break;
11761   case llvm::omp::OMPC_capture:
11762     C = new (Context) OMPCaptureClause();
11763     break;
11764   case llvm::omp::OMPC_seq_cst:
11765     C = new (Context) OMPSeqCstClause();
11766     break;
11767   case llvm::omp::OMPC_acq_rel:
11768     C = new (Context) OMPAcqRelClause();
11769     break;
11770   case llvm::omp::OMPC_acquire:
11771     C = new (Context) OMPAcquireClause();
11772     break;
11773   case llvm::omp::OMPC_release:
11774     C = new (Context) OMPReleaseClause();
11775     break;
11776   case llvm::omp::OMPC_relaxed:
11777     C = new (Context) OMPRelaxedClause();
11778     break;
11779   case llvm::omp::OMPC_threads:
11780     C = new (Context) OMPThreadsClause();
11781     break;
11782   case llvm::omp::OMPC_simd:
11783     C = new (Context) OMPSIMDClause();
11784     break;
11785   case llvm::omp::OMPC_nogroup:
11786     C = new (Context) OMPNogroupClause();
11787     break;
11788   case llvm::omp::OMPC_unified_address:
11789     C = new (Context) OMPUnifiedAddressClause();
11790     break;
11791   case llvm::omp::OMPC_unified_shared_memory:
11792     C = new (Context) OMPUnifiedSharedMemoryClause();
11793     break;
11794   case llvm::omp::OMPC_reverse_offload:
11795     C = new (Context) OMPReverseOffloadClause();
11796     break;
11797   case llvm::omp::OMPC_dynamic_allocators:
11798     C = new (Context) OMPDynamicAllocatorsClause();
11799     break;
11800   case llvm::omp::OMPC_atomic_default_mem_order:
11801     C = new (Context) OMPAtomicDefaultMemOrderClause();
11802     break;
11803  case llvm::omp::OMPC_private:
11804     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11805     break;
11806   case llvm::omp::OMPC_firstprivate:
11807     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11808     break;
11809   case llvm::omp::OMPC_lastprivate:
11810     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11811     break;
11812   case llvm::omp::OMPC_shared:
11813     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11814     break;
11815   case llvm::omp::OMPC_reduction: {
11816     unsigned N = Record.readInt();
11817     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11818     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11819     break;
11820   }
11821   case llvm::omp::OMPC_task_reduction:
11822     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11823     break;
11824   case llvm::omp::OMPC_in_reduction:
11825     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11826     break;
11827   case llvm::omp::OMPC_linear:
11828     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11829     break;
11830   case llvm::omp::OMPC_aligned:
11831     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11832     break;
11833   case llvm::omp::OMPC_copyin:
11834     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11835     break;
11836   case llvm::omp::OMPC_copyprivate:
11837     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11838     break;
11839   case llvm::omp::OMPC_flush:
11840     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11841     break;
11842   case llvm::omp::OMPC_depobj:
11843     C = OMPDepobjClause::CreateEmpty(Context);
11844     break;
11845   case llvm::omp::OMPC_depend: {
11846     unsigned NumVars = Record.readInt();
11847     unsigned NumLoops = Record.readInt();
11848     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11849     break;
11850   }
11851   case llvm::omp::OMPC_device:
11852     C = new (Context) OMPDeviceClause();
11853     break;
11854   case llvm::omp::OMPC_map: {
11855     OMPMappableExprListSizeTy Sizes;
11856     Sizes.NumVars = Record.readInt();
11857     Sizes.NumUniqueDeclarations = Record.readInt();
11858     Sizes.NumComponentLists = Record.readInt();
11859     Sizes.NumComponents = Record.readInt();
11860     C = OMPMapClause::CreateEmpty(Context, Sizes);
11861     break;
11862   }
11863   case llvm::omp::OMPC_num_teams:
11864     C = new (Context) OMPNumTeamsClause();
11865     break;
11866   case llvm::omp::OMPC_thread_limit:
11867     C = new (Context) OMPThreadLimitClause();
11868     break;
11869   case llvm::omp::OMPC_priority:
11870     C = new (Context) OMPPriorityClause();
11871     break;
11872   case llvm::omp::OMPC_grainsize:
11873     C = new (Context) OMPGrainsizeClause();
11874     break;
11875   case llvm::omp::OMPC_num_tasks:
11876     C = new (Context) OMPNumTasksClause();
11877     break;
11878   case llvm::omp::OMPC_hint:
11879     C = new (Context) OMPHintClause();
11880     break;
11881   case llvm::omp::OMPC_dist_schedule:
11882     C = new (Context) OMPDistScheduleClause();
11883     break;
11884   case llvm::omp::OMPC_defaultmap:
11885     C = new (Context) OMPDefaultmapClause();
11886     break;
11887   case llvm::omp::OMPC_to: {
11888     OMPMappableExprListSizeTy Sizes;
11889     Sizes.NumVars = Record.readInt();
11890     Sizes.NumUniqueDeclarations = Record.readInt();
11891     Sizes.NumComponentLists = Record.readInt();
11892     Sizes.NumComponents = Record.readInt();
11893     C = OMPToClause::CreateEmpty(Context, Sizes);
11894     break;
11895   }
11896   case llvm::omp::OMPC_from: {
11897     OMPMappableExprListSizeTy Sizes;
11898     Sizes.NumVars = Record.readInt();
11899     Sizes.NumUniqueDeclarations = Record.readInt();
11900     Sizes.NumComponentLists = Record.readInt();
11901     Sizes.NumComponents = Record.readInt();
11902     C = OMPFromClause::CreateEmpty(Context, Sizes);
11903     break;
11904   }
11905   case llvm::omp::OMPC_use_device_ptr: {
11906     OMPMappableExprListSizeTy Sizes;
11907     Sizes.NumVars = Record.readInt();
11908     Sizes.NumUniqueDeclarations = Record.readInt();
11909     Sizes.NumComponentLists = Record.readInt();
11910     Sizes.NumComponents = Record.readInt();
11911     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11912     break;
11913   }
11914   case llvm::omp::OMPC_use_device_addr: {
11915     OMPMappableExprListSizeTy Sizes;
11916     Sizes.NumVars = Record.readInt();
11917     Sizes.NumUniqueDeclarations = Record.readInt();
11918     Sizes.NumComponentLists = Record.readInt();
11919     Sizes.NumComponents = Record.readInt();
11920     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11921     break;
11922   }
11923   case llvm::omp::OMPC_is_device_ptr: {
11924     OMPMappableExprListSizeTy Sizes;
11925     Sizes.NumVars = Record.readInt();
11926     Sizes.NumUniqueDeclarations = Record.readInt();
11927     Sizes.NumComponentLists = Record.readInt();
11928     Sizes.NumComponents = Record.readInt();
11929     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11930     break;
11931   }
11932   case llvm::omp::OMPC_allocate:
11933     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11934     break;
11935   case llvm::omp::OMPC_nontemporal:
11936     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11937     break;
11938   case llvm::omp::OMPC_inclusive:
11939     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11940     break;
11941   case llvm::omp::OMPC_exclusive:
11942     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11943     break;
11944   case llvm::omp::OMPC_order:
11945     C = new (Context) OMPOrderClause();
11946     break;
11947   case llvm::omp::OMPC_init:
11948     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11949     break;
11950   case llvm::omp::OMPC_use:
11951     C = new (Context) OMPUseClause();
11952     break;
11953   case llvm::omp::OMPC_destroy:
11954     C = new (Context) OMPDestroyClause();
11955     break;
11956   case llvm::omp::OMPC_novariants:
11957     C = new (Context) OMPNovariantsClause();
11958     break;
11959   case llvm::omp::OMPC_nocontext:
11960     C = new (Context) OMPNocontextClause();
11961     break;
11962   case llvm::omp::OMPC_detach:
11963     C = new (Context) OMPDetachClause();
11964     break;
11965   case llvm::omp::OMPC_uses_allocators:
11966     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11967     break;
11968   case llvm::omp::OMPC_affinity:
11969     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11970     break;
11971   case llvm::omp::OMPC_filter:
11972     C = new (Context) OMPFilterClause();
11973     break;
11974   case llvm::omp::OMPC_bind:
11975     C = OMPBindClause::CreateEmpty(Context);
11976     break;
11977   case llvm::omp::OMPC_align:
11978     C = new (Context) OMPAlignClause();
11979     break;
11980 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11981   case llvm::omp::Enum:                                                        \
11982     break;
11983 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11984   default:
11985     break;
11986   }
11987   assert(C && "Unknown OMPClause type");
11988 
11989   Visit(C);
11990   C->setLocStart(Record.readSourceLocation());
11991   C->setLocEnd(Record.readSourceLocation());
11992 
11993   return C;
11994 }
11995 
11996 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11997   C->setPreInitStmt(Record.readSubStmt(),
11998                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11999 }
12000 
12001 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12002   VisitOMPClauseWithPreInit(C);
12003   C->setPostUpdateExpr(Record.readSubExpr());
12004 }
12005 
12006 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12007   VisitOMPClauseWithPreInit(C);
12008   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12009   C->setNameModifierLoc(Record.readSourceLocation());
12010   C->setColonLoc(Record.readSourceLocation());
12011   C->setCondition(Record.readSubExpr());
12012   C->setLParenLoc(Record.readSourceLocation());
12013 }
12014 
12015 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12016   VisitOMPClauseWithPreInit(C);
12017   C->setCondition(Record.readSubExpr());
12018   C->setLParenLoc(Record.readSourceLocation());
12019 }
12020 
12021 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12022   VisitOMPClauseWithPreInit(C);
12023   C->setNumThreads(Record.readSubExpr());
12024   C->setLParenLoc(Record.readSourceLocation());
12025 }
12026 
12027 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12028   C->setSafelen(Record.readSubExpr());
12029   C->setLParenLoc(Record.readSourceLocation());
12030 }
12031 
12032 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12033   C->setSimdlen(Record.readSubExpr());
12034   C->setLParenLoc(Record.readSourceLocation());
12035 }
12036 
12037 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12038   for (Expr *&E : C->getSizesRefs())
12039     E = Record.readSubExpr();
12040   C->setLParenLoc(Record.readSourceLocation());
12041 }
12042 
12043 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12044 
12045 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12046   C->setFactor(Record.readSubExpr());
12047   C->setLParenLoc(Record.readSourceLocation());
12048 }
12049 
12050 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12051   C->setAllocator(Record.readExpr());
12052   C->setLParenLoc(Record.readSourceLocation());
12053 }
12054 
12055 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12056   C->setNumForLoops(Record.readSubExpr());
12057   C->setLParenLoc(Record.readSourceLocation());
12058 }
12059 
12060 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12061   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12062   C->setLParenLoc(Record.readSourceLocation());
12063   C->setDefaultKindKwLoc(Record.readSourceLocation());
12064 }
12065 
12066 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12067   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12068   C->setLParenLoc(Record.readSourceLocation());
12069   C->setProcBindKindKwLoc(Record.readSourceLocation());
12070 }
12071 
12072 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12073   VisitOMPClauseWithPreInit(C);
12074   C->setScheduleKind(
12075        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12076   C->setFirstScheduleModifier(
12077       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12078   C->setSecondScheduleModifier(
12079       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12080   C->setChunkSize(Record.readSubExpr());
12081   C->setLParenLoc(Record.readSourceLocation());
12082   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12083   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12084   C->setScheduleKindLoc(Record.readSourceLocation());
12085   C->setCommaLoc(Record.readSourceLocation());
12086 }
12087 
12088 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12089   C->setNumForLoops(Record.readSubExpr());
12090   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12091     C->setLoopNumIterations(I, Record.readSubExpr());
12092   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12093     C->setLoopCounter(I, Record.readSubExpr());
12094   C->setLParenLoc(Record.readSourceLocation());
12095 }
12096 
12097 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12098   C->setEventHandler(Record.readSubExpr());
12099   C->setLParenLoc(Record.readSourceLocation());
12100 }
12101 
12102 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12103 
12104 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12105 
12106 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12107 
12108 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12109 
12110 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12111 
12112 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12113   if (C->isExtended()) {
12114     C->setLParenLoc(Record.readSourceLocation());
12115     C->setArgumentLoc(Record.readSourceLocation());
12116     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12117   }
12118 }
12119 
12120 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12121 
12122 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12123 
12124 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12125 
12126 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12127 
12128 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12129 
12130 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12131 
12132 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12133 
12134 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12135 
12136 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12137 
12138 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12139   unsigned NumVars = C->varlist_size();
12140   SmallVector<Expr *, 16> Vars;
12141   Vars.reserve(NumVars);
12142   for (unsigned I = 0; I != NumVars; ++I)
12143     Vars.push_back(Record.readSubExpr());
12144   C->setVarRefs(Vars);
12145   C->setIsTarget(Record.readBool());
12146   C->setIsTargetSync(Record.readBool());
12147   C->setLParenLoc(Record.readSourceLocation());
12148   C->setVarLoc(Record.readSourceLocation());
12149 }
12150 
12151 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12152   C->setInteropVar(Record.readSubExpr());
12153   C->setLParenLoc(Record.readSourceLocation());
12154   C->setVarLoc(Record.readSourceLocation());
12155 }
12156 
12157 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12158   C->setInteropVar(Record.readSubExpr());
12159   C->setLParenLoc(Record.readSourceLocation());
12160   C->setVarLoc(Record.readSourceLocation());
12161 }
12162 
12163 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12164   VisitOMPClauseWithPreInit(C);
12165   C->setCondition(Record.readSubExpr());
12166   C->setLParenLoc(Record.readSourceLocation());
12167 }
12168 
12169 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12170   VisitOMPClauseWithPreInit(C);
12171   C->setCondition(Record.readSubExpr());
12172   C->setLParenLoc(Record.readSourceLocation());
12173 }
12174 
12175 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12176 
12177 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12178     OMPUnifiedSharedMemoryClause *) {}
12179 
12180 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12181 
12182 void
12183 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12184 }
12185 
12186 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12187     OMPAtomicDefaultMemOrderClause *C) {
12188   C->setAtomicDefaultMemOrderKind(
12189       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12190   C->setLParenLoc(Record.readSourceLocation());
12191   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12192 }
12193 
12194 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12195   C->setLParenLoc(Record.readSourceLocation());
12196   unsigned NumVars = C->varlist_size();
12197   SmallVector<Expr *, 16> Vars;
12198   Vars.reserve(NumVars);
12199   for (unsigned i = 0; i != NumVars; ++i)
12200     Vars.push_back(Record.readSubExpr());
12201   C->setVarRefs(Vars);
12202   Vars.clear();
12203   for (unsigned i = 0; i != NumVars; ++i)
12204     Vars.push_back(Record.readSubExpr());
12205   C->setPrivateCopies(Vars);
12206 }
12207 
12208 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12209   VisitOMPClauseWithPreInit(C);
12210   C->setLParenLoc(Record.readSourceLocation());
12211   unsigned NumVars = C->varlist_size();
12212   SmallVector<Expr *, 16> Vars;
12213   Vars.reserve(NumVars);
12214   for (unsigned i = 0; i != NumVars; ++i)
12215     Vars.push_back(Record.readSubExpr());
12216   C->setVarRefs(Vars);
12217   Vars.clear();
12218   for (unsigned i = 0; i != NumVars; ++i)
12219     Vars.push_back(Record.readSubExpr());
12220   C->setPrivateCopies(Vars);
12221   Vars.clear();
12222   for (unsigned i = 0; i != NumVars; ++i)
12223     Vars.push_back(Record.readSubExpr());
12224   C->setInits(Vars);
12225 }
12226 
12227 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12228   VisitOMPClauseWithPostUpdate(C);
12229   C->setLParenLoc(Record.readSourceLocation());
12230   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12231   C->setKindLoc(Record.readSourceLocation());
12232   C->setColonLoc(Record.readSourceLocation());
12233   unsigned NumVars = C->varlist_size();
12234   SmallVector<Expr *, 16> Vars;
12235   Vars.reserve(NumVars);
12236   for (unsigned i = 0; i != NumVars; ++i)
12237     Vars.push_back(Record.readSubExpr());
12238   C->setVarRefs(Vars);
12239   Vars.clear();
12240   for (unsigned i = 0; i != NumVars; ++i)
12241     Vars.push_back(Record.readSubExpr());
12242   C->setPrivateCopies(Vars);
12243   Vars.clear();
12244   for (unsigned i = 0; i != NumVars; ++i)
12245     Vars.push_back(Record.readSubExpr());
12246   C->setSourceExprs(Vars);
12247   Vars.clear();
12248   for (unsigned i = 0; i != NumVars; ++i)
12249     Vars.push_back(Record.readSubExpr());
12250   C->setDestinationExprs(Vars);
12251   Vars.clear();
12252   for (unsigned i = 0; i != NumVars; ++i)
12253     Vars.push_back(Record.readSubExpr());
12254   C->setAssignmentOps(Vars);
12255 }
12256 
12257 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12258   C->setLParenLoc(Record.readSourceLocation());
12259   unsigned NumVars = C->varlist_size();
12260   SmallVector<Expr *, 16> Vars;
12261   Vars.reserve(NumVars);
12262   for (unsigned i = 0; i != NumVars; ++i)
12263     Vars.push_back(Record.readSubExpr());
12264   C->setVarRefs(Vars);
12265 }
12266 
12267 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12268   VisitOMPClauseWithPostUpdate(C);
12269   C->setLParenLoc(Record.readSourceLocation());
12270   C->setModifierLoc(Record.readSourceLocation());
12271   C->setColonLoc(Record.readSourceLocation());
12272   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12273   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12274   C->setQualifierLoc(NNSL);
12275   C->setNameInfo(DNI);
12276 
12277   unsigned NumVars = C->varlist_size();
12278   SmallVector<Expr *, 16> Vars;
12279   Vars.reserve(NumVars);
12280   for (unsigned i = 0; i != NumVars; ++i)
12281     Vars.push_back(Record.readSubExpr());
12282   C->setVarRefs(Vars);
12283   Vars.clear();
12284   for (unsigned i = 0; i != NumVars; ++i)
12285     Vars.push_back(Record.readSubExpr());
12286   C->setPrivates(Vars);
12287   Vars.clear();
12288   for (unsigned i = 0; i != NumVars; ++i)
12289     Vars.push_back(Record.readSubExpr());
12290   C->setLHSExprs(Vars);
12291   Vars.clear();
12292   for (unsigned i = 0; i != NumVars; ++i)
12293     Vars.push_back(Record.readSubExpr());
12294   C->setRHSExprs(Vars);
12295   Vars.clear();
12296   for (unsigned i = 0; i != NumVars; ++i)
12297     Vars.push_back(Record.readSubExpr());
12298   C->setReductionOps(Vars);
12299   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12300     Vars.clear();
12301     for (unsigned i = 0; i != NumVars; ++i)
12302       Vars.push_back(Record.readSubExpr());
12303     C->setInscanCopyOps(Vars);
12304     Vars.clear();
12305     for (unsigned i = 0; i != NumVars; ++i)
12306       Vars.push_back(Record.readSubExpr());
12307     C->setInscanCopyArrayTemps(Vars);
12308     Vars.clear();
12309     for (unsigned i = 0; i != NumVars; ++i)
12310       Vars.push_back(Record.readSubExpr());
12311     C->setInscanCopyArrayElems(Vars);
12312   }
12313 }
12314 
12315 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12316   VisitOMPClauseWithPostUpdate(C);
12317   C->setLParenLoc(Record.readSourceLocation());
12318   C->setColonLoc(Record.readSourceLocation());
12319   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12320   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12321   C->setQualifierLoc(NNSL);
12322   C->setNameInfo(DNI);
12323 
12324   unsigned NumVars = C->varlist_size();
12325   SmallVector<Expr *, 16> Vars;
12326   Vars.reserve(NumVars);
12327   for (unsigned I = 0; I != NumVars; ++I)
12328     Vars.push_back(Record.readSubExpr());
12329   C->setVarRefs(Vars);
12330   Vars.clear();
12331   for (unsigned I = 0; I != NumVars; ++I)
12332     Vars.push_back(Record.readSubExpr());
12333   C->setPrivates(Vars);
12334   Vars.clear();
12335   for (unsigned I = 0; I != NumVars; ++I)
12336     Vars.push_back(Record.readSubExpr());
12337   C->setLHSExprs(Vars);
12338   Vars.clear();
12339   for (unsigned I = 0; I != NumVars; ++I)
12340     Vars.push_back(Record.readSubExpr());
12341   C->setRHSExprs(Vars);
12342   Vars.clear();
12343   for (unsigned I = 0; I != NumVars; ++I)
12344     Vars.push_back(Record.readSubExpr());
12345   C->setReductionOps(Vars);
12346 }
12347 
12348 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12349   VisitOMPClauseWithPostUpdate(C);
12350   C->setLParenLoc(Record.readSourceLocation());
12351   C->setColonLoc(Record.readSourceLocation());
12352   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12353   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12354   C->setQualifierLoc(NNSL);
12355   C->setNameInfo(DNI);
12356 
12357   unsigned NumVars = C->varlist_size();
12358   SmallVector<Expr *, 16> Vars;
12359   Vars.reserve(NumVars);
12360   for (unsigned I = 0; I != NumVars; ++I)
12361     Vars.push_back(Record.readSubExpr());
12362   C->setVarRefs(Vars);
12363   Vars.clear();
12364   for (unsigned I = 0; I != NumVars; ++I)
12365     Vars.push_back(Record.readSubExpr());
12366   C->setPrivates(Vars);
12367   Vars.clear();
12368   for (unsigned I = 0; I != NumVars; ++I)
12369     Vars.push_back(Record.readSubExpr());
12370   C->setLHSExprs(Vars);
12371   Vars.clear();
12372   for (unsigned I = 0; I != NumVars; ++I)
12373     Vars.push_back(Record.readSubExpr());
12374   C->setRHSExprs(Vars);
12375   Vars.clear();
12376   for (unsigned I = 0; I != NumVars; ++I)
12377     Vars.push_back(Record.readSubExpr());
12378   C->setReductionOps(Vars);
12379   Vars.clear();
12380   for (unsigned I = 0; I != NumVars; ++I)
12381     Vars.push_back(Record.readSubExpr());
12382   C->setTaskgroupDescriptors(Vars);
12383 }
12384 
12385 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12386   VisitOMPClauseWithPostUpdate(C);
12387   C->setLParenLoc(Record.readSourceLocation());
12388   C->setColonLoc(Record.readSourceLocation());
12389   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12390   C->setModifierLoc(Record.readSourceLocation());
12391   unsigned NumVars = C->varlist_size();
12392   SmallVector<Expr *, 16> Vars;
12393   Vars.reserve(NumVars);
12394   for (unsigned i = 0; i != NumVars; ++i)
12395     Vars.push_back(Record.readSubExpr());
12396   C->setVarRefs(Vars);
12397   Vars.clear();
12398   for (unsigned i = 0; i != NumVars; ++i)
12399     Vars.push_back(Record.readSubExpr());
12400   C->setPrivates(Vars);
12401   Vars.clear();
12402   for (unsigned i = 0; i != NumVars; ++i)
12403     Vars.push_back(Record.readSubExpr());
12404   C->setInits(Vars);
12405   Vars.clear();
12406   for (unsigned i = 0; i != NumVars; ++i)
12407     Vars.push_back(Record.readSubExpr());
12408   C->setUpdates(Vars);
12409   Vars.clear();
12410   for (unsigned i = 0; i != NumVars; ++i)
12411     Vars.push_back(Record.readSubExpr());
12412   C->setFinals(Vars);
12413   C->setStep(Record.readSubExpr());
12414   C->setCalcStep(Record.readSubExpr());
12415   Vars.clear();
12416   for (unsigned I = 0; I != NumVars + 1; ++I)
12417     Vars.push_back(Record.readSubExpr());
12418   C->setUsedExprs(Vars);
12419 }
12420 
12421 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12422   C->setLParenLoc(Record.readSourceLocation());
12423   C->setColonLoc(Record.readSourceLocation());
12424   unsigned NumVars = C->varlist_size();
12425   SmallVector<Expr *, 16> Vars;
12426   Vars.reserve(NumVars);
12427   for (unsigned i = 0; i != NumVars; ++i)
12428     Vars.push_back(Record.readSubExpr());
12429   C->setVarRefs(Vars);
12430   C->setAlignment(Record.readSubExpr());
12431 }
12432 
12433 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12434   C->setLParenLoc(Record.readSourceLocation());
12435   unsigned NumVars = C->varlist_size();
12436   SmallVector<Expr *, 16> Exprs;
12437   Exprs.reserve(NumVars);
12438   for (unsigned i = 0; i != NumVars; ++i)
12439     Exprs.push_back(Record.readSubExpr());
12440   C->setVarRefs(Exprs);
12441   Exprs.clear();
12442   for (unsigned i = 0; i != NumVars; ++i)
12443     Exprs.push_back(Record.readSubExpr());
12444   C->setSourceExprs(Exprs);
12445   Exprs.clear();
12446   for (unsigned i = 0; i != NumVars; ++i)
12447     Exprs.push_back(Record.readSubExpr());
12448   C->setDestinationExprs(Exprs);
12449   Exprs.clear();
12450   for (unsigned i = 0; i != NumVars; ++i)
12451     Exprs.push_back(Record.readSubExpr());
12452   C->setAssignmentOps(Exprs);
12453 }
12454 
12455 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12456   C->setLParenLoc(Record.readSourceLocation());
12457   unsigned NumVars = C->varlist_size();
12458   SmallVector<Expr *, 16> Exprs;
12459   Exprs.reserve(NumVars);
12460   for (unsigned i = 0; i != NumVars; ++i)
12461     Exprs.push_back(Record.readSubExpr());
12462   C->setVarRefs(Exprs);
12463   Exprs.clear();
12464   for (unsigned i = 0; i != NumVars; ++i)
12465     Exprs.push_back(Record.readSubExpr());
12466   C->setSourceExprs(Exprs);
12467   Exprs.clear();
12468   for (unsigned i = 0; i != NumVars; ++i)
12469     Exprs.push_back(Record.readSubExpr());
12470   C->setDestinationExprs(Exprs);
12471   Exprs.clear();
12472   for (unsigned i = 0; i != NumVars; ++i)
12473     Exprs.push_back(Record.readSubExpr());
12474   C->setAssignmentOps(Exprs);
12475 }
12476 
12477 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12478   C->setLParenLoc(Record.readSourceLocation());
12479   unsigned NumVars = C->varlist_size();
12480   SmallVector<Expr *, 16> Vars;
12481   Vars.reserve(NumVars);
12482   for (unsigned i = 0; i != NumVars; ++i)
12483     Vars.push_back(Record.readSubExpr());
12484   C->setVarRefs(Vars);
12485 }
12486 
12487 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12488   C->setDepobj(Record.readSubExpr());
12489   C->setLParenLoc(Record.readSourceLocation());
12490 }
12491 
12492 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12493   C->setLParenLoc(Record.readSourceLocation());
12494   C->setModifier(Record.readSubExpr());
12495   C->setDependencyKind(
12496       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12497   C->setDependencyLoc(Record.readSourceLocation());
12498   C->setColonLoc(Record.readSourceLocation());
12499   unsigned NumVars = C->varlist_size();
12500   SmallVector<Expr *, 16> Vars;
12501   Vars.reserve(NumVars);
12502   for (unsigned I = 0; I != NumVars; ++I)
12503     Vars.push_back(Record.readSubExpr());
12504   C->setVarRefs(Vars);
12505   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12506     C->setLoopData(I, Record.readSubExpr());
12507 }
12508 
12509 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12510   VisitOMPClauseWithPreInit(C);
12511   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12512   C->setDevice(Record.readSubExpr());
12513   C->setModifierLoc(Record.readSourceLocation());
12514   C->setLParenLoc(Record.readSourceLocation());
12515 }
12516 
12517 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12518   C->setLParenLoc(Record.readSourceLocation());
12519   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12520     C->setMapTypeModifier(
12521         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12522     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12523   }
12524   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12525   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12526   C->setMapType(
12527      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12528   C->setMapLoc(Record.readSourceLocation());
12529   C->setColonLoc(Record.readSourceLocation());
12530   auto NumVars = C->varlist_size();
12531   auto UniqueDecls = C->getUniqueDeclarationsNum();
12532   auto TotalLists = C->getTotalComponentListNum();
12533   auto TotalComponents = C->getTotalComponentsNum();
12534 
12535   SmallVector<Expr *, 16> Vars;
12536   Vars.reserve(NumVars);
12537   for (unsigned i = 0; i != NumVars; ++i)
12538     Vars.push_back(Record.readExpr());
12539   C->setVarRefs(Vars);
12540 
12541   SmallVector<Expr *, 16> UDMappers;
12542   UDMappers.reserve(NumVars);
12543   for (unsigned I = 0; I < NumVars; ++I)
12544     UDMappers.push_back(Record.readExpr());
12545   C->setUDMapperRefs(UDMappers);
12546 
12547   SmallVector<ValueDecl *, 16> Decls;
12548   Decls.reserve(UniqueDecls);
12549   for (unsigned i = 0; i < UniqueDecls; ++i)
12550     Decls.push_back(Record.readDeclAs<ValueDecl>());
12551   C->setUniqueDecls(Decls);
12552 
12553   SmallVector<unsigned, 16> ListsPerDecl;
12554   ListsPerDecl.reserve(UniqueDecls);
12555   for (unsigned i = 0; i < UniqueDecls; ++i)
12556     ListsPerDecl.push_back(Record.readInt());
12557   C->setDeclNumLists(ListsPerDecl);
12558 
12559   SmallVector<unsigned, 32> ListSizes;
12560   ListSizes.reserve(TotalLists);
12561   for (unsigned i = 0; i < TotalLists; ++i)
12562     ListSizes.push_back(Record.readInt());
12563   C->setComponentListSizes(ListSizes);
12564 
12565   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12566   Components.reserve(TotalComponents);
12567   for (unsigned i = 0; i < TotalComponents; ++i) {
12568     Expr *AssociatedExprPr = Record.readExpr();
12569     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12570     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12571                             /*IsNonContiguous=*/false);
12572   }
12573   C->setComponents(Components, ListSizes);
12574 }
12575 
12576 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12577   C->setLParenLoc(Record.readSourceLocation());
12578   C->setColonLoc(Record.readSourceLocation());
12579   C->setAllocator(Record.readSubExpr());
12580   unsigned NumVars = C->varlist_size();
12581   SmallVector<Expr *, 16> Vars;
12582   Vars.reserve(NumVars);
12583   for (unsigned i = 0; i != NumVars; ++i)
12584     Vars.push_back(Record.readSubExpr());
12585   C->setVarRefs(Vars);
12586 }
12587 
12588 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12589   VisitOMPClauseWithPreInit(C);
12590   C->setNumTeams(Record.readSubExpr());
12591   C->setLParenLoc(Record.readSourceLocation());
12592 }
12593 
12594 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12595   VisitOMPClauseWithPreInit(C);
12596   C->setThreadLimit(Record.readSubExpr());
12597   C->setLParenLoc(Record.readSourceLocation());
12598 }
12599 
12600 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12601   VisitOMPClauseWithPreInit(C);
12602   C->setPriority(Record.readSubExpr());
12603   C->setLParenLoc(Record.readSourceLocation());
12604 }
12605 
12606 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12607   VisitOMPClauseWithPreInit(C);
12608   C->setGrainsize(Record.readSubExpr());
12609   C->setLParenLoc(Record.readSourceLocation());
12610 }
12611 
12612 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12613   VisitOMPClauseWithPreInit(C);
12614   C->setNumTasks(Record.readSubExpr());
12615   C->setLParenLoc(Record.readSourceLocation());
12616 }
12617 
12618 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12619   C->setHint(Record.readSubExpr());
12620   C->setLParenLoc(Record.readSourceLocation());
12621 }
12622 
12623 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12624   VisitOMPClauseWithPreInit(C);
12625   C->setDistScheduleKind(
12626       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12627   C->setChunkSize(Record.readSubExpr());
12628   C->setLParenLoc(Record.readSourceLocation());
12629   C->setDistScheduleKindLoc(Record.readSourceLocation());
12630   C->setCommaLoc(Record.readSourceLocation());
12631 }
12632 
12633 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12634   C->setDefaultmapKind(
12635        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12636   C->setDefaultmapModifier(
12637       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12638   C->setLParenLoc(Record.readSourceLocation());
12639   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12640   C->setDefaultmapKindLoc(Record.readSourceLocation());
12641 }
12642 
12643 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12644   C->setLParenLoc(Record.readSourceLocation());
12645   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12646     C->setMotionModifier(
12647         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12648     C->setMotionModifierLoc(I, Record.readSourceLocation());
12649   }
12650   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12651   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12652   C->setColonLoc(Record.readSourceLocation());
12653   auto NumVars = C->varlist_size();
12654   auto UniqueDecls = C->getUniqueDeclarationsNum();
12655   auto TotalLists = C->getTotalComponentListNum();
12656   auto TotalComponents = C->getTotalComponentsNum();
12657 
12658   SmallVector<Expr *, 16> Vars;
12659   Vars.reserve(NumVars);
12660   for (unsigned i = 0; i != NumVars; ++i)
12661     Vars.push_back(Record.readSubExpr());
12662   C->setVarRefs(Vars);
12663 
12664   SmallVector<Expr *, 16> UDMappers;
12665   UDMappers.reserve(NumVars);
12666   for (unsigned I = 0; I < NumVars; ++I)
12667     UDMappers.push_back(Record.readSubExpr());
12668   C->setUDMapperRefs(UDMappers);
12669 
12670   SmallVector<ValueDecl *, 16> Decls;
12671   Decls.reserve(UniqueDecls);
12672   for (unsigned i = 0; i < UniqueDecls; ++i)
12673     Decls.push_back(Record.readDeclAs<ValueDecl>());
12674   C->setUniqueDecls(Decls);
12675 
12676   SmallVector<unsigned, 16> ListsPerDecl;
12677   ListsPerDecl.reserve(UniqueDecls);
12678   for (unsigned i = 0; i < UniqueDecls; ++i)
12679     ListsPerDecl.push_back(Record.readInt());
12680   C->setDeclNumLists(ListsPerDecl);
12681 
12682   SmallVector<unsigned, 32> ListSizes;
12683   ListSizes.reserve(TotalLists);
12684   for (unsigned i = 0; i < TotalLists; ++i)
12685     ListSizes.push_back(Record.readInt());
12686   C->setComponentListSizes(ListSizes);
12687 
12688   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12689   Components.reserve(TotalComponents);
12690   for (unsigned i = 0; i < TotalComponents; ++i) {
12691     Expr *AssociatedExprPr = Record.readSubExpr();
12692     bool IsNonContiguous = Record.readBool();
12693     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12694     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12695   }
12696   C->setComponents(Components, ListSizes);
12697 }
12698 
12699 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12700   C->setLParenLoc(Record.readSourceLocation());
12701   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12702     C->setMotionModifier(
12703         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12704     C->setMotionModifierLoc(I, Record.readSourceLocation());
12705   }
12706   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12707   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12708   C->setColonLoc(Record.readSourceLocation());
12709   auto NumVars = C->varlist_size();
12710   auto UniqueDecls = C->getUniqueDeclarationsNum();
12711   auto TotalLists = C->getTotalComponentListNum();
12712   auto TotalComponents = C->getTotalComponentsNum();
12713 
12714   SmallVector<Expr *, 16> Vars;
12715   Vars.reserve(NumVars);
12716   for (unsigned i = 0; i != NumVars; ++i)
12717     Vars.push_back(Record.readSubExpr());
12718   C->setVarRefs(Vars);
12719 
12720   SmallVector<Expr *, 16> UDMappers;
12721   UDMappers.reserve(NumVars);
12722   for (unsigned I = 0; I < NumVars; ++I)
12723     UDMappers.push_back(Record.readSubExpr());
12724   C->setUDMapperRefs(UDMappers);
12725 
12726   SmallVector<ValueDecl *, 16> Decls;
12727   Decls.reserve(UniqueDecls);
12728   for (unsigned i = 0; i < UniqueDecls; ++i)
12729     Decls.push_back(Record.readDeclAs<ValueDecl>());
12730   C->setUniqueDecls(Decls);
12731 
12732   SmallVector<unsigned, 16> ListsPerDecl;
12733   ListsPerDecl.reserve(UniqueDecls);
12734   for (unsigned i = 0; i < UniqueDecls; ++i)
12735     ListsPerDecl.push_back(Record.readInt());
12736   C->setDeclNumLists(ListsPerDecl);
12737 
12738   SmallVector<unsigned, 32> ListSizes;
12739   ListSizes.reserve(TotalLists);
12740   for (unsigned i = 0; i < TotalLists; ++i)
12741     ListSizes.push_back(Record.readInt());
12742   C->setComponentListSizes(ListSizes);
12743 
12744   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12745   Components.reserve(TotalComponents);
12746   for (unsigned i = 0; i < TotalComponents; ++i) {
12747     Expr *AssociatedExprPr = Record.readSubExpr();
12748     bool IsNonContiguous = Record.readBool();
12749     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12750     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12751   }
12752   C->setComponents(Components, ListSizes);
12753 }
12754 
12755 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12756   C->setLParenLoc(Record.readSourceLocation());
12757   auto NumVars = C->varlist_size();
12758   auto UniqueDecls = C->getUniqueDeclarationsNum();
12759   auto TotalLists = C->getTotalComponentListNum();
12760   auto TotalComponents = C->getTotalComponentsNum();
12761 
12762   SmallVector<Expr *, 16> Vars;
12763   Vars.reserve(NumVars);
12764   for (unsigned i = 0; i != NumVars; ++i)
12765     Vars.push_back(Record.readSubExpr());
12766   C->setVarRefs(Vars);
12767   Vars.clear();
12768   for (unsigned i = 0; i != NumVars; ++i)
12769     Vars.push_back(Record.readSubExpr());
12770   C->setPrivateCopies(Vars);
12771   Vars.clear();
12772   for (unsigned i = 0; i != NumVars; ++i)
12773     Vars.push_back(Record.readSubExpr());
12774   C->setInits(Vars);
12775 
12776   SmallVector<ValueDecl *, 16> Decls;
12777   Decls.reserve(UniqueDecls);
12778   for (unsigned i = 0; i < UniqueDecls; ++i)
12779     Decls.push_back(Record.readDeclAs<ValueDecl>());
12780   C->setUniqueDecls(Decls);
12781 
12782   SmallVector<unsigned, 16> ListsPerDecl;
12783   ListsPerDecl.reserve(UniqueDecls);
12784   for (unsigned i = 0; i < UniqueDecls; ++i)
12785     ListsPerDecl.push_back(Record.readInt());
12786   C->setDeclNumLists(ListsPerDecl);
12787 
12788   SmallVector<unsigned, 32> ListSizes;
12789   ListSizes.reserve(TotalLists);
12790   for (unsigned i = 0; i < TotalLists; ++i)
12791     ListSizes.push_back(Record.readInt());
12792   C->setComponentListSizes(ListSizes);
12793 
12794   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12795   Components.reserve(TotalComponents);
12796   for (unsigned i = 0; i < TotalComponents; ++i) {
12797     auto *AssociatedExprPr = Record.readSubExpr();
12798     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12799     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12800                             /*IsNonContiguous=*/false);
12801   }
12802   C->setComponents(Components, ListSizes);
12803 }
12804 
12805 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12806   C->setLParenLoc(Record.readSourceLocation());
12807   auto NumVars = C->varlist_size();
12808   auto UniqueDecls = C->getUniqueDeclarationsNum();
12809   auto TotalLists = C->getTotalComponentListNum();
12810   auto TotalComponents = C->getTotalComponentsNum();
12811 
12812   SmallVector<Expr *, 16> Vars;
12813   Vars.reserve(NumVars);
12814   for (unsigned i = 0; i != NumVars; ++i)
12815     Vars.push_back(Record.readSubExpr());
12816   C->setVarRefs(Vars);
12817 
12818   SmallVector<ValueDecl *, 16> Decls;
12819   Decls.reserve(UniqueDecls);
12820   for (unsigned i = 0; i < UniqueDecls; ++i)
12821     Decls.push_back(Record.readDeclAs<ValueDecl>());
12822   C->setUniqueDecls(Decls);
12823 
12824   SmallVector<unsigned, 16> ListsPerDecl;
12825   ListsPerDecl.reserve(UniqueDecls);
12826   for (unsigned i = 0; i < UniqueDecls; ++i)
12827     ListsPerDecl.push_back(Record.readInt());
12828   C->setDeclNumLists(ListsPerDecl);
12829 
12830   SmallVector<unsigned, 32> ListSizes;
12831   ListSizes.reserve(TotalLists);
12832   for (unsigned i = 0; i < TotalLists; ++i)
12833     ListSizes.push_back(Record.readInt());
12834   C->setComponentListSizes(ListSizes);
12835 
12836   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12837   Components.reserve(TotalComponents);
12838   for (unsigned i = 0; i < TotalComponents; ++i) {
12839     Expr *AssociatedExpr = Record.readSubExpr();
12840     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12841     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12842                             /*IsNonContiguous*/ false);
12843   }
12844   C->setComponents(Components, ListSizes);
12845 }
12846 
12847 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12848   C->setLParenLoc(Record.readSourceLocation());
12849   auto NumVars = C->varlist_size();
12850   auto UniqueDecls = C->getUniqueDeclarationsNum();
12851   auto TotalLists = C->getTotalComponentListNum();
12852   auto TotalComponents = C->getTotalComponentsNum();
12853 
12854   SmallVector<Expr *, 16> Vars;
12855   Vars.reserve(NumVars);
12856   for (unsigned i = 0; i != NumVars; ++i)
12857     Vars.push_back(Record.readSubExpr());
12858   C->setVarRefs(Vars);
12859   Vars.clear();
12860 
12861   SmallVector<ValueDecl *, 16> Decls;
12862   Decls.reserve(UniqueDecls);
12863   for (unsigned i = 0; i < UniqueDecls; ++i)
12864     Decls.push_back(Record.readDeclAs<ValueDecl>());
12865   C->setUniqueDecls(Decls);
12866 
12867   SmallVector<unsigned, 16> ListsPerDecl;
12868   ListsPerDecl.reserve(UniqueDecls);
12869   for (unsigned i = 0; i < UniqueDecls; ++i)
12870     ListsPerDecl.push_back(Record.readInt());
12871   C->setDeclNumLists(ListsPerDecl);
12872 
12873   SmallVector<unsigned, 32> ListSizes;
12874   ListSizes.reserve(TotalLists);
12875   for (unsigned i = 0; i < TotalLists; ++i)
12876     ListSizes.push_back(Record.readInt());
12877   C->setComponentListSizes(ListSizes);
12878 
12879   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12880   Components.reserve(TotalComponents);
12881   for (unsigned i = 0; i < TotalComponents; ++i) {
12882     Expr *AssociatedExpr = Record.readSubExpr();
12883     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12884     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12885                             /*IsNonContiguous=*/false);
12886   }
12887   C->setComponents(Components, ListSizes);
12888 }
12889 
12890 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12891   C->setLParenLoc(Record.readSourceLocation());
12892   unsigned NumVars = C->varlist_size();
12893   SmallVector<Expr *, 16> Vars;
12894   Vars.reserve(NumVars);
12895   for (unsigned i = 0; i != NumVars; ++i)
12896     Vars.push_back(Record.readSubExpr());
12897   C->setVarRefs(Vars);
12898   Vars.clear();
12899   Vars.reserve(NumVars);
12900   for (unsigned i = 0; i != NumVars; ++i)
12901     Vars.push_back(Record.readSubExpr());
12902   C->setPrivateRefs(Vars);
12903 }
12904 
12905 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12906   C->setLParenLoc(Record.readSourceLocation());
12907   unsigned NumVars = C->varlist_size();
12908   SmallVector<Expr *, 16> Vars;
12909   Vars.reserve(NumVars);
12910   for (unsigned i = 0; i != NumVars; ++i)
12911     Vars.push_back(Record.readSubExpr());
12912   C->setVarRefs(Vars);
12913 }
12914 
12915 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12916   C->setLParenLoc(Record.readSourceLocation());
12917   unsigned NumVars = C->varlist_size();
12918   SmallVector<Expr *, 16> Vars;
12919   Vars.reserve(NumVars);
12920   for (unsigned i = 0; i != NumVars; ++i)
12921     Vars.push_back(Record.readSubExpr());
12922   C->setVarRefs(Vars);
12923 }
12924 
12925 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12926   C->setLParenLoc(Record.readSourceLocation());
12927   unsigned NumOfAllocators = C->getNumberOfAllocators();
12928   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12929   Data.reserve(NumOfAllocators);
12930   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12931     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12932     D.Allocator = Record.readSubExpr();
12933     D.AllocatorTraits = Record.readSubExpr();
12934     D.LParenLoc = Record.readSourceLocation();
12935     D.RParenLoc = Record.readSourceLocation();
12936   }
12937   C->setAllocatorsData(Data);
12938 }
12939 
12940 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12941   C->setLParenLoc(Record.readSourceLocation());
12942   C->setModifier(Record.readSubExpr());
12943   C->setColonLoc(Record.readSourceLocation());
12944   unsigned NumOfLocators = C->varlist_size();
12945   SmallVector<Expr *, 4> Locators;
12946   Locators.reserve(NumOfLocators);
12947   for (unsigned I = 0; I != NumOfLocators; ++I)
12948     Locators.push_back(Record.readSubExpr());
12949   C->setVarRefs(Locators);
12950 }
12951 
12952 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12953   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12954   C->setLParenLoc(Record.readSourceLocation());
12955   C->setKindKwLoc(Record.readSourceLocation());
12956 }
12957 
12958 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12959   VisitOMPClauseWithPreInit(C);
12960   C->setThreadID(Record.readSubExpr());
12961   C->setLParenLoc(Record.readSourceLocation());
12962 }
12963 
12964 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12965   C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12966   C->setLParenLoc(Record.readSourceLocation());
12967   C->setBindKindLoc(Record.readSourceLocation());
12968 }
12969 
12970 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12971   C->setAlignment(Record.readExpr());
12972   C->setLParenLoc(Record.readSourceLocation());
12973 }
12974 
12975 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12976   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12977   TI.Sets.resize(readUInt32());
12978   for (auto &Set : TI.Sets) {
12979     Set.Kind = readEnum<llvm::omp::TraitSet>();
12980     Set.Selectors.resize(readUInt32());
12981     for (auto &Selector : Set.Selectors) {
12982       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12983       Selector.ScoreOrCondition = nullptr;
12984       if (readBool())
12985         Selector.ScoreOrCondition = readExprRef();
12986       Selector.Properties.resize(readUInt32());
12987       for (auto &Property : Selector.Properties)
12988         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12989     }
12990   }
12991   return &TI;
12992 }
12993 
12994 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12995   if (!Data)
12996     return;
12997   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12998     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12999     skipInts(3);
13000   }
13001   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13002   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13003     Clauses[I] = readOMPClause();
13004   Data->setClauses(Clauses);
13005   if (Data->hasAssociatedStmt())
13006     Data->setAssociatedStmt(readStmt());
13007   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13008     Data->getChildren()[I] = readStmt();
13009 }
13010