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 
146 //===----------------------------------------------------------------------===//
147 // ChainedASTReaderListener implementation
148 //===----------------------------------------------------------------------===//
149 
150 bool
151 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
152   return First->ReadFullVersionInformation(FullVersion) ||
153          Second->ReadFullVersionInformation(FullVersion);
154 }
155 
156 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
157   First->ReadModuleName(ModuleName);
158   Second->ReadModuleName(ModuleName);
159 }
160 
161 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
162   First->ReadModuleMapFile(ModuleMapPath);
163   Second->ReadModuleMapFile(ModuleMapPath);
164 }
165 
166 bool
167 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
168                                               bool Complain,
169                                               bool AllowCompatibleDifferences) {
170   return First->ReadLanguageOptions(LangOpts, Complain,
171                                     AllowCompatibleDifferences) ||
172          Second->ReadLanguageOptions(LangOpts, Complain,
173                                      AllowCompatibleDifferences);
174 }
175 
176 bool ChainedASTReaderListener::ReadTargetOptions(
177     const TargetOptions &TargetOpts, bool Complain,
178     bool AllowCompatibleDifferences) {
179   return First->ReadTargetOptions(TargetOpts, Complain,
180                                   AllowCompatibleDifferences) ||
181          Second->ReadTargetOptions(TargetOpts, Complain,
182                                    AllowCompatibleDifferences);
183 }
184 
185 bool ChainedASTReaderListener::ReadDiagnosticOptions(
186     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
187   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
188          Second->ReadDiagnosticOptions(DiagOpts, Complain);
189 }
190 
191 bool
192 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
193                                                 bool Complain) {
194   return First->ReadFileSystemOptions(FSOpts, Complain) ||
195          Second->ReadFileSystemOptions(FSOpts, Complain);
196 }
197 
198 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
199     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
200     bool Complain) {
201   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
202                                         Complain) ||
203          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
204                                          Complain);
205 }
206 
207 bool ChainedASTReaderListener::ReadPreprocessorOptions(
208     const PreprocessorOptions &PPOpts, bool Complain,
209     std::string &SuggestedPredefines) {
210   return First->ReadPreprocessorOptions(PPOpts, Complain,
211                                         SuggestedPredefines) ||
212          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
213 }
214 
215 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
216                                            unsigned Value) {
217   First->ReadCounter(M, Value);
218   Second->ReadCounter(M, Value);
219 }
220 
221 bool ChainedASTReaderListener::needsInputFileVisitation() {
222   return First->needsInputFileVisitation() ||
223          Second->needsInputFileVisitation();
224 }
225 
226 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
227   return First->needsSystemInputFileVisitation() ||
228   Second->needsSystemInputFileVisitation();
229 }
230 
231 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
232                                                ModuleKind Kind) {
233   First->visitModuleFile(Filename, Kind);
234   Second->visitModuleFile(Filename, Kind);
235 }
236 
237 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
238                                               bool isSystem,
239                                               bool isOverridden,
240                                               bool isExplicitModule) {
241   bool Continue = false;
242   if (First->needsInputFileVisitation() &&
243       (!isSystem || First->needsSystemInputFileVisitation()))
244     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
245                                       isExplicitModule);
246   if (Second->needsInputFileVisitation() &&
247       (!isSystem || Second->needsSystemInputFileVisitation()))
248     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
249                                        isExplicitModule);
250   return Continue;
251 }
252 
253 void ChainedASTReaderListener::readModuleFileExtension(
254        const ModuleFileExtensionMetadata &Metadata) {
255   First->readModuleFileExtension(Metadata);
256   Second->readModuleFileExtension(Metadata);
257 }
258 
259 //===----------------------------------------------------------------------===//
260 // PCH validator implementation
261 //===----------------------------------------------------------------------===//
262 
263 ASTReaderListener::~ASTReaderListener() = default;
264 
265 /// Compare the given set of language options against an existing set of
266 /// language options.
267 ///
268 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
269 /// \param AllowCompatibleDifferences If true, differences between compatible
270 ///        language options will be permitted.
271 ///
272 /// \returns true if the languagae options mis-match, false otherwise.
273 static bool checkLanguageOptions(const LangOptions &LangOpts,
274                                  const LangOptions &ExistingLangOpts,
275                                  DiagnosticsEngine *Diags,
276                                  bool AllowCompatibleDifferences = true) {
277 #define LANGOPT(Name, Bits, Default, Description)                 \
278   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
279     if (Diags)                                                    \
280       Diags->Report(diag::err_pch_langopt_mismatch)               \
281         << Description << LangOpts.Name << ExistingLangOpts.Name; \
282     return true;                                                  \
283   }
284 
285 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
286   if (ExistingLangOpts.Name != LangOpts.Name) {           \
287     if (Diags)                                            \
288       Diags->Report(diag::err_pch_langopt_value_mismatch) \
289         << Description;                                   \
290     return true;                                          \
291   }
292 
293 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
294   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
295     if (Diags)                                                 \
296       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
297         << Description;                                        \
298     return true;                                               \
299   }
300 
301 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
302   if (!AllowCompatibleDifferences)                            \
303     LANGOPT(Name, Bits, Default, Description)
304 
305 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
306   if (!AllowCompatibleDifferences)                                 \
307     ENUM_LANGOPT(Name, Bits, Default, Description)
308 
309 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
310   if (!AllowCompatibleDifferences)                                 \
311     VALUE_LANGOPT(Name, Bits, Default, Description)
312 
313 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
314 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
315 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
316 #include "clang/Basic/LangOptions.def"
317 
318   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
321     return true;
322   }
323 
324   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
325     if (Diags)
326       Diags->Report(diag::err_pch_langopt_value_mismatch)
327       << "target Objective-C runtime";
328     return true;
329   }
330 
331   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
332       LangOpts.CommentOpts.BlockCommandNames) {
333     if (Diags)
334       Diags->Report(diag::err_pch_langopt_value_mismatch)
335         << "block command names";
336     return true;
337   }
338 
339   // Sanitizer feature mismatches are treated as compatible differences. If
340   // compatible differences aren't allowed, we still only want to check for
341   // mismatches of non-modular sanitizers (the only ones which can affect AST
342   // generation).
343   if (!AllowCompatibleDifferences) {
344     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
345     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
346     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
347     ExistingSanitizers.clear(ModularSanitizers);
348     ImportedSanitizers.clear(ModularSanitizers);
349     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
350       const std::string Flag = "-fsanitize=";
351       if (Diags) {
352 #define SANITIZER(NAME, ID)                                                    \
353   {                                                                            \
354     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
355     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
356     if (InExistingModule != InImportedModule)                                  \
357       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
358           << InExistingModule << (Flag + NAME);                                \
359   }
360 #include "clang/Basic/Sanitizers.def"
361       }
362       return true;
363     }
364   }
365 
366   return false;
367 }
368 
369 /// Compare the given set of target options against an existing set of
370 /// target options.
371 ///
372 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
373 ///
374 /// \returns true if the target options mis-match, false otherwise.
375 static bool checkTargetOptions(const TargetOptions &TargetOpts,
376                                const TargetOptions &ExistingTargetOpts,
377                                DiagnosticsEngine *Diags,
378                                bool AllowCompatibleDifferences = true) {
379 #define CHECK_TARGET_OPT(Field, Name)                             \
380   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
381     if (Diags)                                                    \
382       Diags->Report(diag::err_pch_targetopt_mismatch)             \
383         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
384     return true;                                                  \
385   }
386 
387   // The triple and ABI must match exactly.
388   CHECK_TARGET_OPT(Triple, "target");
389   CHECK_TARGET_OPT(ABI, "target ABI");
390 
391   // We can tolerate different CPUs in many cases, notably when one CPU
392   // supports a strict superset of another. When allowing compatible
393   // differences skip this check.
394   if (!AllowCompatibleDifferences) {
395     CHECK_TARGET_OPT(CPU, "target CPU");
396     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
397   }
398 
399 #undef CHECK_TARGET_OPT
400 
401   // Compare feature sets.
402   SmallVector<StringRef, 4> ExistingFeatures(
403                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
404                                              ExistingTargetOpts.FeaturesAsWritten.end());
405   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
406                                          TargetOpts.FeaturesAsWritten.end());
407   llvm::sort(ExistingFeatures);
408   llvm::sort(ReadFeatures);
409 
410   // We compute the set difference in both directions explicitly so that we can
411   // diagnose the differences differently.
412   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
413   std::set_difference(
414       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
415       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
416   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
417                       ExistingFeatures.begin(), ExistingFeatures.end(),
418                       std::back_inserter(UnmatchedReadFeatures));
419 
420   // If we are allowing compatible differences and the read feature set is
421   // a strict subset of the existing feature set, there is nothing to diagnose.
422   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
423     return false;
424 
425   if (Diags) {
426     for (StringRef Feature : UnmatchedReadFeatures)
427       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428           << /* is-existing-feature */ false << Feature;
429     for (StringRef Feature : UnmatchedExistingFeatures)
430       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
431           << /* is-existing-feature */ true << Feature;
432   }
433 
434   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
435 }
436 
437 bool
438 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
439                                   bool Complain,
440                                   bool AllowCompatibleDifferences) {
441   const LangOptions &ExistingLangOpts = PP.getLangOpts();
442   return checkLanguageOptions(LangOpts, ExistingLangOpts,
443                               Complain ? &Reader.Diags : nullptr,
444                               AllowCompatibleDifferences);
445 }
446 
447 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
448                                      bool Complain,
449                                      bool AllowCompatibleDifferences) {
450   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
451   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
452                             Complain ? &Reader.Diags : nullptr,
453                             AllowCompatibleDifferences);
454 }
455 
456 namespace {
457 
458 using MacroDefinitionsMap =
459     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
460 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
461 
462 } // namespace
463 
464 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
465                                          DiagnosticsEngine &Diags,
466                                          bool Complain) {
467   using Level = DiagnosticsEngine::Level;
468 
469   // Check current mappings for new -Werror mappings, and the stored mappings
470   // for cases that were explicitly mapped to *not* be errors that are now
471   // errors because of options like -Werror.
472   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
473 
474   for (DiagnosticsEngine *MappingSource : MappingSources) {
475     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
476       diag::kind DiagID = DiagIDMappingPair.first;
477       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
478       if (CurLevel < DiagnosticsEngine::Error)
479         continue; // not significant
480       Level StoredLevel =
481           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
482       if (StoredLevel < DiagnosticsEngine::Error) {
483         if (Complain)
484           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
485               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
486         return true;
487       }
488     }
489   }
490 
491   return false;
492 }
493 
494 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
495   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
496   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
497     return true;
498   return Ext >= diag::Severity::Error;
499 }
500 
501 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
502                                     DiagnosticsEngine &Diags,
503                                     bool IsSystem, bool Complain) {
504   // Top-level options
505   if (IsSystem) {
506     if (Diags.getSuppressSystemWarnings())
507       return false;
508     // If -Wsystem-headers was not enabled before, be conservative
509     if (StoredDiags.getSuppressSystemWarnings()) {
510       if (Complain)
511         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
512       return true;
513     }
514   }
515 
516   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
517     if (Complain)
518       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
519     return true;
520   }
521 
522   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
523       !StoredDiags.getEnableAllWarnings()) {
524     if (Complain)
525       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
526     return true;
527   }
528 
529   if (isExtHandlingFromDiagsError(Diags) &&
530       !isExtHandlingFromDiagsError(StoredDiags)) {
531     if (Complain)
532       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
533     return true;
534   }
535 
536   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
537 }
538 
539 /// Return the top import module if it is implicit, nullptr otherwise.
540 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
541                                           Preprocessor &PP) {
542   // If the original import came from a file explicitly generated by the user,
543   // don't check the diagnostic mappings.
544   // FIXME: currently this is approximated by checking whether this is not a
545   // module import of an implicitly-loaded module file.
546   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
547   // the transitive closure of its imports, since unrelated modules cannot be
548   // imported until after this module finishes validation.
549   ModuleFile *TopImport = &*ModuleMgr.rbegin();
550   while (!TopImport->ImportedBy.empty())
551     TopImport = TopImport->ImportedBy[0];
552   if (TopImport->Kind != MK_ImplicitModule)
553     return nullptr;
554 
555   StringRef ModuleName = TopImport->ModuleName;
556   assert(!ModuleName.empty() && "diagnostic options read before module name");
557 
558   Module *M =
559       PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
560   assert(M && "missing module");
561   return M;
562 }
563 
564 bool PCHValidator::ReadDiagnosticOptions(
565     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
566   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
567   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
568   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
569       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
570   // This should never fail, because we would have processed these options
571   // before writing them to an ASTFile.
572   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
573 
574   ModuleManager &ModuleMgr = Reader.getModuleManager();
575   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
576 
577   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
578   if (!TopM)
579     return false;
580 
581   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
582   // contains the union of their flags.
583   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
584                                  Complain);
585 }
586 
587 /// Collect the macro definitions provided by the given preprocessor
588 /// options.
589 static void
590 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
591                         MacroDefinitionsMap &Macros,
592                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
593   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
594     StringRef Macro = PPOpts.Macros[I].first;
595     bool IsUndef = PPOpts.Macros[I].second;
596 
597     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
598     StringRef MacroName = MacroPair.first;
599     StringRef MacroBody = MacroPair.second;
600 
601     // For an #undef'd macro, we only care about the name.
602     if (IsUndef) {
603       if (MacroNames && !Macros.count(MacroName))
604         MacroNames->push_back(MacroName);
605 
606       Macros[MacroName] = std::make_pair("", true);
607       continue;
608     }
609 
610     // For a #define'd macro, figure out the actual definition.
611     if (MacroName.size() == Macro.size())
612       MacroBody = "1";
613     else {
614       // Note: GCC drops anything following an end-of-line character.
615       StringRef::size_type End = MacroBody.find_first_of("\n\r");
616       MacroBody = MacroBody.substr(0, End);
617     }
618 
619     if (MacroNames && !Macros.count(MacroName))
620       MacroNames->push_back(MacroName);
621     Macros[MacroName] = std::make_pair(MacroBody, false);
622   }
623 }
624 
625 /// Check the preprocessor options deserialized from the control block
626 /// against the preprocessor options in an existing preprocessor.
627 ///
628 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
629 /// \param Validate If true, validate preprocessor options. If false, allow
630 ///        macros defined by \p ExistingPPOpts to override those defined by
631 ///        \p PPOpts in SuggestedPredefines.
632 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
633                                      const PreprocessorOptions &ExistingPPOpts,
634                                      DiagnosticsEngine *Diags,
635                                      FileManager &FileMgr,
636                                      std::string &SuggestedPredefines,
637                                      const LangOptions &LangOpts,
638                                      bool Validate = true) {
639   // Check macro definitions.
640   MacroDefinitionsMap ASTFileMacros;
641   collectMacroDefinitions(PPOpts, ASTFileMacros);
642   MacroDefinitionsMap ExistingMacros;
643   SmallVector<StringRef, 4> ExistingMacroNames;
644   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
645 
646   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
647     // Dig out the macro definition in the existing preprocessor options.
648     StringRef MacroName = ExistingMacroNames[I];
649     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
650 
651     // Check whether we know anything about this macro name or not.
652     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
653         ASTFileMacros.find(MacroName);
654     if (!Validate || Known == ASTFileMacros.end()) {
655       // FIXME: Check whether this identifier was referenced anywhere in the
656       // AST file. If so, we should reject the AST file. Unfortunately, this
657       // information isn't in the control block. What shall we do about it?
658 
659       if (Existing.second) {
660         SuggestedPredefines += "#undef ";
661         SuggestedPredefines += MacroName.str();
662         SuggestedPredefines += '\n';
663       } else {
664         SuggestedPredefines += "#define ";
665         SuggestedPredefines += MacroName.str();
666         SuggestedPredefines += ' ';
667         SuggestedPredefines += Existing.first.str();
668         SuggestedPredefines += '\n';
669       }
670       continue;
671     }
672 
673     // If the macro was defined in one but undef'd in the other, we have a
674     // conflict.
675     if (Existing.second != Known->second.second) {
676       if (Diags) {
677         Diags->Report(diag::err_pch_macro_def_undef)
678           << MacroName << Known->second.second;
679       }
680       return true;
681     }
682 
683     // If the macro was #undef'd in both, or if the macro bodies are identical,
684     // it's fine.
685     if (Existing.second || Existing.first == Known->second.first)
686       continue;
687 
688     // The macro bodies differ; complain.
689     if (Diags) {
690       Diags->Report(diag::err_pch_macro_def_conflict)
691         << MacroName << Known->second.first << Existing.first;
692     }
693     return true;
694   }
695 
696   // Check whether we're using predefines.
697   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
698     if (Diags) {
699       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
700     }
701     return true;
702   }
703 
704   // Detailed record is important since it is used for the module cache hash.
705   if (LangOpts.Modules &&
706       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
707     if (Diags) {
708       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
709     }
710     return true;
711   }
712 
713   // Compute the #include and #include_macros lines we need.
714   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
715     StringRef File = ExistingPPOpts.Includes[I];
716 
717     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
718         !ExistingPPOpts.PCHThroughHeader.empty()) {
719       // In case the through header is an include, we must add all the includes
720       // to the predefines so the start point can be determined.
721       SuggestedPredefines += "#include \"";
722       SuggestedPredefines += File;
723       SuggestedPredefines += "\"\n";
724       continue;
725     }
726 
727     if (File == ExistingPPOpts.ImplicitPCHInclude)
728       continue;
729 
730     if (llvm::is_contained(PPOpts.Includes, File))
731       continue;
732 
733     SuggestedPredefines += "#include \"";
734     SuggestedPredefines += File;
735     SuggestedPredefines += "\"\n";
736   }
737 
738   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
739     StringRef File = ExistingPPOpts.MacroIncludes[I];
740     if (llvm::is_contained(PPOpts.MacroIncludes, File))
741       continue;
742 
743     SuggestedPredefines += "#__include_macros \"";
744     SuggestedPredefines += File;
745     SuggestedPredefines += "\"\n##\n";
746   }
747 
748   return false;
749 }
750 
751 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
752                                            bool Complain,
753                                            std::string &SuggestedPredefines) {
754   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
755 
756   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
757                                   Complain? &Reader.Diags : nullptr,
758                                   PP.getFileManager(),
759                                   SuggestedPredefines,
760                                   PP.getLangOpts());
761 }
762 
763 bool SimpleASTReaderListener::ReadPreprocessorOptions(
764                                   const PreprocessorOptions &PPOpts,
765                                   bool Complain,
766                                   std::string &SuggestedPredefines) {
767   return checkPreprocessorOptions(PPOpts,
768                                   PP.getPreprocessorOpts(),
769                                   nullptr,
770                                   PP.getFileManager(),
771                                   SuggestedPredefines,
772                                   PP.getLangOpts(),
773                                   false);
774 }
775 
776 /// Check the header search options deserialized from the control block
777 /// against the header search options in an existing preprocessor.
778 ///
779 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
780 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
781                                      StringRef SpecificModuleCachePath,
782                                      StringRef ExistingModuleCachePath,
783                                      DiagnosticsEngine *Diags,
784                                      const LangOptions &LangOpts,
785                                      const PreprocessorOptions &PPOpts) {
786   if (LangOpts.Modules) {
787     if (SpecificModuleCachePath != ExistingModuleCachePath &&
788         !PPOpts.AllowPCHWithDifferentModulesCachePath) {
789       if (Diags)
790         Diags->Report(diag::err_pch_modulecache_mismatch)
791           << SpecificModuleCachePath << ExistingModuleCachePath;
792       return true;
793     }
794   }
795 
796   return false;
797 }
798 
799 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
800                                            StringRef SpecificModuleCachePath,
801                                            bool Complain) {
802   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
803                                   PP.getHeaderSearchInfo().getModuleCachePath(),
804                                   Complain ? &Reader.Diags : nullptr,
805                                   PP.getLangOpts(), PP.getPreprocessorOpts());
806 }
807 
808 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
809   PP.setCounterValue(Value);
810 }
811 
812 //===----------------------------------------------------------------------===//
813 // AST reader implementation
814 //===----------------------------------------------------------------------===//
815 
816 static uint64_t readULEB(const unsigned char *&P) {
817   unsigned Length = 0;
818   const char *Error = nullptr;
819 
820   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
821   if (Error)
822     llvm::report_fatal_error(Error);
823   P += Length;
824   return Val;
825 }
826 
827 /// Read ULEB-encoded key length and data length.
828 static std::pair<unsigned, unsigned>
829 readULEBKeyDataLength(const unsigned char *&P) {
830   unsigned KeyLen = readULEB(P);
831   if ((unsigned)KeyLen != KeyLen)
832     llvm::report_fatal_error("key too large");
833 
834   unsigned DataLen = readULEB(P);
835   if ((unsigned)DataLen != DataLen)
836     llvm::report_fatal_error("data too large");
837 
838   return std::make_pair(KeyLen, DataLen);
839 }
840 
841 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
842                                            bool TakeOwnership) {
843   DeserializationListener = Listener;
844   OwnsDeserializationListener = TakeOwnership;
845 }
846 
847 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
848   return serialization::ComputeHash(Sel);
849 }
850 
851 std::pair<unsigned, unsigned>
852 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
853   return readULEBKeyDataLength(d);
854 }
855 
856 ASTSelectorLookupTrait::internal_key_type
857 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
858   using namespace llvm::support;
859 
860   SelectorTable &SelTable = Reader.getContext().Selectors;
861   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
862   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
863       F, endian::readNext<uint32_t, little, unaligned>(d));
864   if (N == 0)
865     return SelTable.getNullarySelector(FirstII);
866   else if (N == 1)
867     return SelTable.getUnarySelector(FirstII);
868 
869   SmallVector<IdentifierInfo *, 16> Args;
870   Args.push_back(FirstII);
871   for (unsigned I = 1; I != N; ++I)
872     Args.push_back(Reader.getLocalIdentifier(
873         F, endian::readNext<uint32_t, little, unaligned>(d)));
874 
875   return SelTable.getSelector(N, Args.data());
876 }
877 
878 ASTSelectorLookupTrait::data_type
879 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
880                                  unsigned DataLen) {
881   using namespace llvm::support;
882 
883   data_type Result;
884 
885   Result.ID = Reader.getGlobalSelectorID(
886       F, endian::readNext<uint32_t, little, unaligned>(d));
887   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
888   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
889   Result.InstanceBits = FullInstanceBits & 0x3;
890   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
891   Result.FactoryBits = FullFactoryBits & 0x3;
892   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
893   unsigned NumInstanceMethods = FullInstanceBits >> 3;
894   unsigned NumFactoryMethods = FullFactoryBits >> 3;
895 
896   // Load instance methods
897   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
898     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
899             F, endian::readNext<uint32_t, little, unaligned>(d)))
900       Result.Instance.push_back(Method);
901   }
902 
903   // Load factory methods
904   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
905     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
906             F, endian::readNext<uint32_t, little, unaligned>(d)))
907       Result.Factory.push_back(Method);
908   }
909 
910   return Result;
911 }
912 
913 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
914   return llvm::djbHash(a);
915 }
916 
917 std::pair<unsigned, unsigned>
918 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
919   return readULEBKeyDataLength(d);
920 }
921 
922 ASTIdentifierLookupTraitBase::internal_key_type
923 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
924   assert(n >= 2 && d[n-1] == '\0');
925   return StringRef((const char*) d, n-1);
926 }
927 
928 /// Whether the given identifier is "interesting".
929 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
930                                     bool IsModule) {
931   return II.hadMacroDefinition() || II.isPoisoned() ||
932          (!IsModule && II.getObjCOrBuiltinID()) ||
933          II.hasRevertedTokenIDToIdentifier() ||
934          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
935           II.getFETokenInfo());
936 }
937 
938 static bool readBit(unsigned &Bits) {
939   bool Value = Bits & 0x1;
940   Bits >>= 1;
941   return Value;
942 }
943 
944 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
945   using namespace llvm::support;
946 
947   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
948   return Reader.getGlobalIdentifierID(F, RawID >> 1);
949 }
950 
951 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
952   if (!II.isFromAST()) {
953     II.setIsFromAST();
954     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
955     if (isInterestingIdentifier(Reader, II, IsModule))
956       II.setChangedSinceDeserialization();
957   }
958 }
959 
960 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
961                                                    const unsigned char* d,
962                                                    unsigned DataLen) {
963   using namespace llvm::support;
964 
965   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
966   bool IsInteresting = RawID & 0x01;
967 
968   // Wipe out the "is interesting" bit.
969   RawID = RawID >> 1;
970 
971   // Build the IdentifierInfo and link the identifier ID with it.
972   IdentifierInfo *II = KnownII;
973   if (!II) {
974     II = &Reader.getIdentifierTable().getOwn(k);
975     KnownII = II;
976   }
977   markIdentifierFromAST(Reader, *II);
978   Reader.markIdentifierUpToDate(II);
979 
980   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
981   if (!IsInteresting) {
982     // For uninteresting identifiers, there's nothing else to do. Just notify
983     // the reader that we've finished loading this identifier.
984     Reader.SetIdentifierInfo(ID, II);
985     return II;
986   }
987 
988   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
989   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
990   bool CPlusPlusOperatorKeyword = readBit(Bits);
991   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
992   bool Poisoned = readBit(Bits);
993   bool ExtensionToken = readBit(Bits);
994   bool HadMacroDefinition = readBit(Bits);
995 
996   assert(Bits == 0 && "Extra bits in the identifier?");
997   DataLen -= 8;
998 
999   // Set or check the various bits in the IdentifierInfo structure.
1000   // Token IDs are read-only.
1001   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1002     II->revertTokenIDToIdentifier();
1003   if (!F.isModule())
1004     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1005   assert(II->isExtensionToken() == ExtensionToken &&
1006          "Incorrect extension token flag");
1007   (void)ExtensionToken;
1008   if (Poisoned)
1009     II->setIsPoisoned(true);
1010   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1011          "Incorrect C++ operator keyword flag");
1012   (void)CPlusPlusOperatorKeyword;
1013 
1014   // If this identifier is a macro, deserialize the macro
1015   // definition.
1016   if (HadMacroDefinition) {
1017     uint32_t MacroDirectivesOffset =
1018         endian::readNext<uint32_t, little, unaligned>(d);
1019     DataLen -= 4;
1020 
1021     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1022   }
1023 
1024   Reader.SetIdentifierInfo(ID, II);
1025 
1026   // Read all of the declarations visible at global scope with this
1027   // name.
1028   if (DataLen > 0) {
1029     SmallVector<uint32_t, 4> DeclIDs;
1030     for (; DataLen > 0; DataLen -= 4)
1031       DeclIDs.push_back(Reader.getGlobalDeclID(
1032           F, endian::readNext<uint32_t, little, unaligned>(d)));
1033     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1034   }
1035 
1036   return II;
1037 }
1038 
1039 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1040     : Kind(Name.getNameKind()) {
1041   switch (Kind) {
1042   case DeclarationName::Identifier:
1043     Data = (uint64_t)Name.getAsIdentifierInfo();
1044     break;
1045   case DeclarationName::ObjCZeroArgSelector:
1046   case DeclarationName::ObjCOneArgSelector:
1047   case DeclarationName::ObjCMultiArgSelector:
1048     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1049     break;
1050   case DeclarationName::CXXOperatorName:
1051     Data = Name.getCXXOverloadedOperator();
1052     break;
1053   case DeclarationName::CXXLiteralOperatorName:
1054     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1055     break;
1056   case DeclarationName::CXXDeductionGuideName:
1057     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1058                ->getDeclName().getAsIdentifierInfo();
1059     break;
1060   case DeclarationName::CXXConstructorName:
1061   case DeclarationName::CXXDestructorName:
1062   case DeclarationName::CXXConversionFunctionName:
1063   case DeclarationName::CXXUsingDirective:
1064     Data = 0;
1065     break;
1066   }
1067 }
1068 
1069 unsigned DeclarationNameKey::getHash() const {
1070   llvm::FoldingSetNodeID ID;
1071   ID.AddInteger(Kind);
1072 
1073   switch (Kind) {
1074   case DeclarationName::Identifier:
1075   case DeclarationName::CXXLiteralOperatorName:
1076   case DeclarationName::CXXDeductionGuideName:
1077     ID.AddString(((IdentifierInfo*)Data)->getName());
1078     break;
1079   case DeclarationName::ObjCZeroArgSelector:
1080   case DeclarationName::ObjCOneArgSelector:
1081   case DeclarationName::ObjCMultiArgSelector:
1082     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1083     break;
1084   case DeclarationName::CXXOperatorName:
1085     ID.AddInteger((OverloadedOperatorKind)Data);
1086     break;
1087   case DeclarationName::CXXConstructorName:
1088   case DeclarationName::CXXDestructorName:
1089   case DeclarationName::CXXConversionFunctionName:
1090   case DeclarationName::CXXUsingDirective:
1091     break;
1092   }
1093 
1094   return ID.ComputeHash();
1095 }
1096 
1097 ModuleFile *
1098 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1099   using namespace llvm::support;
1100 
1101   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1102   return Reader.getLocalModuleFile(F, ModuleFileID);
1103 }
1104 
1105 std::pair<unsigned, unsigned>
1106 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1107   return readULEBKeyDataLength(d);
1108 }
1109 
1110 ASTDeclContextNameLookupTrait::internal_key_type
1111 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1112   using namespace llvm::support;
1113 
1114   auto Kind = (DeclarationName::NameKind)*d++;
1115   uint64_t Data;
1116   switch (Kind) {
1117   case DeclarationName::Identifier:
1118   case DeclarationName::CXXLiteralOperatorName:
1119   case DeclarationName::CXXDeductionGuideName:
1120     Data = (uint64_t)Reader.getLocalIdentifier(
1121         F, endian::readNext<uint32_t, little, unaligned>(d));
1122     break;
1123   case DeclarationName::ObjCZeroArgSelector:
1124   case DeclarationName::ObjCOneArgSelector:
1125   case DeclarationName::ObjCMultiArgSelector:
1126     Data =
1127         (uint64_t)Reader.getLocalSelector(
1128                              F, endian::readNext<uint32_t, little, unaligned>(
1129                                     d)).getAsOpaquePtr();
1130     break;
1131   case DeclarationName::CXXOperatorName:
1132     Data = *d++; // OverloadedOperatorKind
1133     break;
1134   case DeclarationName::CXXConstructorName:
1135   case DeclarationName::CXXDestructorName:
1136   case DeclarationName::CXXConversionFunctionName:
1137   case DeclarationName::CXXUsingDirective:
1138     Data = 0;
1139     break;
1140   }
1141 
1142   return DeclarationNameKey(Kind, Data);
1143 }
1144 
1145 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1146                                                  const unsigned char *d,
1147                                                  unsigned DataLen,
1148                                                  data_type_builder &Val) {
1149   using namespace llvm::support;
1150 
1151   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1152     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1153     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1154   }
1155 }
1156 
1157 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1158                                               BitstreamCursor &Cursor,
1159                                               uint64_t Offset,
1160                                               DeclContext *DC) {
1161   assert(Offset != 0);
1162 
1163   SavedStreamPosition SavedPosition(Cursor);
1164   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1165     Error(std::move(Err));
1166     return true;
1167   }
1168 
1169   RecordData Record;
1170   StringRef Blob;
1171   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1172   if (!MaybeCode) {
1173     Error(MaybeCode.takeError());
1174     return true;
1175   }
1176   unsigned Code = MaybeCode.get();
1177 
1178   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1179   if (!MaybeRecCode) {
1180     Error(MaybeRecCode.takeError());
1181     return true;
1182   }
1183   unsigned RecCode = MaybeRecCode.get();
1184   if (RecCode != DECL_CONTEXT_LEXICAL) {
1185     Error("Expected lexical block");
1186     return true;
1187   }
1188 
1189   assert(!isa<TranslationUnitDecl>(DC) &&
1190          "expected a TU_UPDATE_LEXICAL record for TU");
1191   // If we are handling a C++ class template instantiation, we can see multiple
1192   // lexical updates for the same record. It's important that we select only one
1193   // of them, so that field numbering works properly. Just pick the first one we
1194   // see.
1195   auto &Lex = LexicalDecls[DC];
1196   if (!Lex.first) {
1197     Lex = std::make_pair(
1198         &M, llvm::makeArrayRef(
1199                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1200                     Blob.data()),
1201                 Blob.size() / 4));
1202   }
1203   DC->setHasExternalLexicalStorage(true);
1204   return false;
1205 }
1206 
1207 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1208                                               BitstreamCursor &Cursor,
1209                                               uint64_t Offset,
1210                                               DeclID ID) {
1211   assert(Offset != 0);
1212 
1213   SavedStreamPosition SavedPosition(Cursor);
1214   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1215     Error(std::move(Err));
1216     return true;
1217   }
1218 
1219   RecordData Record;
1220   StringRef Blob;
1221   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1222   if (!MaybeCode) {
1223     Error(MaybeCode.takeError());
1224     return true;
1225   }
1226   unsigned Code = MaybeCode.get();
1227 
1228   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1229   if (!MaybeRecCode) {
1230     Error(MaybeRecCode.takeError());
1231     return true;
1232   }
1233   unsigned RecCode = MaybeRecCode.get();
1234   if (RecCode != DECL_CONTEXT_VISIBLE) {
1235     Error("Expected visible lookup table block");
1236     return true;
1237   }
1238 
1239   // We can't safely determine the primary context yet, so delay attaching the
1240   // lookup table until we're done with recursive deserialization.
1241   auto *Data = (const unsigned char*)Blob.data();
1242   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1243   return false;
1244 }
1245 
1246 void ASTReader::Error(StringRef Msg) const {
1247   Error(diag::err_fe_pch_malformed, Msg);
1248   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1249       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1250     Diag(diag::note_module_cache_path)
1251       << PP.getHeaderSearchInfo().getModuleCachePath();
1252   }
1253 }
1254 
1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1256                       StringRef Arg3) const {
1257   if (Diags.isDiagnosticInFlight())
1258     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1259   else
1260     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1261 }
1262 
1263 void ASTReader::Error(llvm::Error &&Err) const {
1264   llvm::Error RemainingErr =
1265       handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1266         auto Diag = E.getDiagnostic().second;
1267 
1268         // Ideally we'd just emit it, but have to handle a possible in-flight
1269         // diagnostic. Note that the location is currently ignored as well.
1270         auto NumArgs = Diag.getStorage()->NumDiagArgs;
1271         assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1272         StringRef Arg1, Arg2, Arg3;
1273         switch (NumArgs) {
1274         case 3:
1275           Arg3 = Diag.getStringArg(2);
1276           LLVM_FALLTHROUGH;
1277         case 2:
1278           Arg2 = Diag.getStringArg(1);
1279           LLVM_FALLTHROUGH;
1280         case 1:
1281           Arg1 = Diag.getStringArg(0);
1282         }
1283         Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1284       });
1285   if (RemainingErr)
1286     Error(toString(std::move(RemainingErr)));
1287 }
1288 
1289 //===----------------------------------------------------------------------===//
1290 // Source Manager Deserialization
1291 //===----------------------------------------------------------------------===//
1292 
1293 /// Read the line table in the source manager block.
1294 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1295   unsigned Idx = 0;
1296   LineTableInfo &LineTable = SourceMgr.getLineTable();
1297 
1298   // Parse the file names
1299   std::map<int, int> FileIDs;
1300   FileIDs[-1] = -1; // For unspecified filenames.
1301   for (unsigned I = 0; Record[Idx]; ++I) {
1302     // Extract the file name
1303     auto Filename = ReadPath(F, Record, Idx);
1304     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1305   }
1306   ++Idx;
1307 
1308   // Parse the line entries
1309   std::vector<LineEntry> Entries;
1310   while (Idx < Record.size()) {
1311     int FID = Record[Idx++];
1312     assert(FID >= 0 && "Serialized line entries for non-local file.");
1313     // Remap FileID from 1-based old view.
1314     FID += F.SLocEntryBaseID - 1;
1315 
1316     // Extract the line entries
1317     unsigned NumEntries = Record[Idx++];
1318     assert(NumEntries && "no line entries for file ID");
1319     Entries.clear();
1320     Entries.reserve(NumEntries);
1321     for (unsigned I = 0; I != NumEntries; ++I) {
1322       unsigned FileOffset = Record[Idx++];
1323       unsigned LineNo = Record[Idx++];
1324       int FilenameID = FileIDs[Record[Idx++]];
1325       SrcMgr::CharacteristicKind FileKind
1326         = (SrcMgr::CharacteristicKind)Record[Idx++];
1327       unsigned IncludeOffset = Record[Idx++];
1328       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1329                                        FileKind, IncludeOffset));
1330     }
1331     LineTable.AddEntry(FileID::get(FID), Entries);
1332   }
1333 }
1334 
1335 /// Read a source manager block
1336 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1337   using namespace SrcMgr;
1338 
1339   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1340 
1341   // Set the source-location entry cursor to the current position in
1342   // the stream. This cursor will be used to read the contents of the
1343   // source manager block initially, and then lazily read
1344   // source-location entries as needed.
1345   SLocEntryCursor = F.Stream;
1346 
1347   // The stream itself is going to skip over the source manager block.
1348   if (llvm::Error Err = F.Stream.SkipBlock())
1349     return Err;
1350 
1351   // Enter the source manager block.
1352   if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1353     return Err;
1354   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1355 
1356   RecordData Record;
1357   while (true) {
1358     Expected<llvm::BitstreamEntry> MaybeE =
1359         SLocEntryCursor.advanceSkippingSubblocks();
1360     if (!MaybeE)
1361       return MaybeE.takeError();
1362     llvm::BitstreamEntry E = MaybeE.get();
1363 
1364     switch (E.Kind) {
1365     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1366     case llvm::BitstreamEntry::Error:
1367       return llvm::createStringError(std::errc::illegal_byte_sequence,
1368                                      "malformed block record in AST file");
1369     case llvm::BitstreamEntry::EndBlock:
1370       return llvm::Error::success();
1371     case llvm::BitstreamEntry::Record:
1372       // The interesting case.
1373       break;
1374     }
1375 
1376     // Read a record.
1377     Record.clear();
1378     StringRef Blob;
1379     Expected<unsigned> MaybeRecord =
1380         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1381     if (!MaybeRecord)
1382       return MaybeRecord.takeError();
1383     switch (MaybeRecord.get()) {
1384     default:  // Default behavior: ignore.
1385       break;
1386 
1387     case SM_SLOC_FILE_ENTRY:
1388     case SM_SLOC_BUFFER_ENTRY:
1389     case SM_SLOC_EXPANSION_ENTRY:
1390       // Once we hit one of the source location entries, we're done.
1391       return llvm::Error::success();
1392     }
1393   }
1394 }
1395 
1396 /// If a header file is not found at the path that we expect it to be
1397 /// and the PCH file was moved from its original location, try to resolve the
1398 /// file by assuming that header+PCH were moved together and the header is in
1399 /// the same place relative to the PCH.
1400 static std::string
1401 resolveFileRelativeToOriginalDir(const std::string &Filename,
1402                                  const std::string &OriginalDir,
1403                                  const std::string &CurrDir) {
1404   assert(OriginalDir != CurrDir &&
1405          "No point trying to resolve the file if the PCH dir didn't change");
1406 
1407   using namespace llvm::sys;
1408 
1409   SmallString<128> filePath(Filename);
1410   fs::make_absolute(filePath);
1411   assert(path::is_absolute(OriginalDir));
1412   SmallString<128> currPCHPath(CurrDir);
1413 
1414   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1415                        fileDirE = path::end(path::parent_path(filePath));
1416   path::const_iterator origDirI = path::begin(OriginalDir),
1417                        origDirE = path::end(OriginalDir);
1418   // Skip the common path components from filePath and OriginalDir.
1419   while (fileDirI != fileDirE && origDirI != origDirE &&
1420          *fileDirI == *origDirI) {
1421     ++fileDirI;
1422     ++origDirI;
1423   }
1424   for (; origDirI != origDirE; ++origDirI)
1425     path::append(currPCHPath, "..");
1426   path::append(currPCHPath, fileDirI, fileDirE);
1427   path::append(currPCHPath, path::filename(Filename));
1428   return std::string(currPCHPath.str());
1429 }
1430 
1431 bool ASTReader::ReadSLocEntry(int ID) {
1432   if (ID == 0)
1433     return false;
1434 
1435   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1436     Error("source location entry ID out-of-range for AST file");
1437     return true;
1438   }
1439 
1440   // Local helper to read the (possibly-compressed) buffer data following the
1441   // entry record.
1442   auto ReadBuffer = [this](
1443       BitstreamCursor &SLocEntryCursor,
1444       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1445     RecordData Record;
1446     StringRef Blob;
1447     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1448     if (!MaybeCode) {
1449       Error(MaybeCode.takeError());
1450       return nullptr;
1451     }
1452     unsigned Code = MaybeCode.get();
1453 
1454     Expected<unsigned> MaybeRecCode =
1455         SLocEntryCursor.readRecord(Code, Record, &Blob);
1456     if (!MaybeRecCode) {
1457       Error(MaybeRecCode.takeError());
1458       return nullptr;
1459     }
1460     unsigned RecCode = MaybeRecCode.get();
1461 
1462     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1463       if (!llvm::zlib::isAvailable()) {
1464         Error("zlib is not available");
1465         return nullptr;
1466       }
1467       SmallString<0> Uncompressed;
1468       if (llvm::Error E =
1469               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1470         Error("could not decompress embedded file contents: " +
1471               llvm::toString(std::move(E)));
1472         return nullptr;
1473       }
1474       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1475     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1476       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1477     } else {
1478       Error("AST record has invalid code");
1479       return nullptr;
1480     }
1481   };
1482 
1483   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1484   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1485           F->SLocEntryOffsetsBase +
1486           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1487     Error(std::move(Err));
1488     return true;
1489   }
1490 
1491   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1492   SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1493 
1494   ++NumSLocEntriesRead;
1495   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1496   if (!MaybeEntry) {
1497     Error(MaybeEntry.takeError());
1498     return true;
1499   }
1500   llvm::BitstreamEntry Entry = MaybeEntry.get();
1501 
1502   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1503     Error("incorrectly-formatted source location entry in AST file");
1504     return true;
1505   }
1506 
1507   RecordData Record;
1508   StringRef Blob;
1509   Expected<unsigned> MaybeSLOC =
1510       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1511   if (!MaybeSLOC) {
1512     Error(MaybeSLOC.takeError());
1513     return true;
1514   }
1515   switch (MaybeSLOC.get()) {
1516   default:
1517     Error("incorrectly-formatted source location entry in AST file");
1518     return true;
1519 
1520   case SM_SLOC_FILE_ENTRY: {
1521     // We will detect whether a file changed and return 'Failure' for it, but
1522     // we will also try to fail gracefully by setting up the SLocEntry.
1523     unsigned InputID = Record[4];
1524     InputFile IF = getInputFile(*F, InputID);
1525     Optional<FileEntryRef> File = IF.getFile();
1526     bool OverriddenBuffer = IF.isOverridden();
1527 
1528     // Note that we only check if a File was returned. If it was out-of-date
1529     // we have complained but we will continue creating a FileID to recover
1530     // gracefully.
1531     if (!File)
1532       return true;
1533 
1534     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1535     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1536       // This is the module's main file.
1537       IncludeLoc = getImportLocation(F);
1538     }
1539     SrcMgr::CharacteristicKind
1540       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1541     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1542                                         BaseOffset + Record[0]);
1543     SrcMgr::FileInfo &FileInfo =
1544           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1545     FileInfo.NumCreatedFIDs = Record[5];
1546     if (Record[3])
1547       FileInfo.setHasLineDirectives();
1548 
1549     unsigned NumFileDecls = Record[7];
1550     if (NumFileDecls && ContextObj) {
1551       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1552       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1553       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1554                                                              NumFileDecls));
1555     }
1556 
1557     const SrcMgr::ContentCache &ContentCache =
1558         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1559     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1560         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1561         !ContentCache.getBufferIfLoaded()) {
1562       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1563       if (!Buffer)
1564         return true;
1565       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1566     }
1567 
1568     break;
1569   }
1570 
1571   case SM_SLOC_BUFFER_ENTRY: {
1572     const char *Name = Blob.data();
1573     unsigned Offset = Record[0];
1574     SrcMgr::CharacteristicKind
1575       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1576     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1577     if (IncludeLoc.isInvalid() && F->isModule()) {
1578       IncludeLoc = getImportLocation(F);
1579     }
1580 
1581     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1582     if (!Buffer)
1583       return true;
1584     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1585                            BaseOffset + Offset, IncludeLoc);
1586     break;
1587   }
1588 
1589   case SM_SLOC_EXPANSION_ENTRY: {
1590     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1591     SourceMgr.createExpansionLoc(SpellingLoc,
1592                                      ReadSourceLocation(*F, Record[2]),
1593                                      ReadSourceLocation(*F, Record[3]),
1594                                      Record[5],
1595                                      Record[4],
1596                                      ID,
1597                                      BaseOffset + Record[0]);
1598     break;
1599   }
1600   }
1601 
1602   return false;
1603 }
1604 
1605 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1606   if (ID == 0)
1607     return std::make_pair(SourceLocation(), "");
1608 
1609   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1610     Error("source location entry ID out-of-range for AST file");
1611     return std::make_pair(SourceLocation(), "");
1612   }
1613 
1614   // Find which module file this entry lands in.
1615   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1616   if (!M->isModule())
1617     return std::make_pair(SourceLocation(), "");
1618 
1619   // FIXME: Can we map this down to a particular submodule? That would be
1620   // ideal.
1621   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1622 }
1623 
1624 /// Find the location where the module F is imported.
1625 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1626   if (F->ImportLoc.isValid())
1627     return F->ImportLoc;
1628 
1629   // Otherwise we have a PCH. It's considered to be "imported" at the first
1630   // location of its includer.
1631   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1632     // Main file is the importer.
1633     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1634     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1635   }
1636   return F->ImportedBy[0]->FirstLoc;
1637 }
1638 
1639 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1640 /// the abbreviations that are at the top of the block and then leave the cursor
1641 /// pointing into the block.
1642 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1643                                         unsigned BlockID,
1644                                         uint64_t *StartOfBlockOffset) {
1645   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1646     return Err;
1647 
1648   if (StartOfBlockOffset)
1649     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1650 
1651   while (true) {
1652     uint64_t Offset = Cursor.GetCurrentBitNo();
1653     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1654     if (!MaybeCode)
1655       return MaybeCode.takeError();
1656     unsigned Code = MaybeCode.get();
1657 
1658     // We expect all abbrevs to be at the start of the block.
1659     if (Code != llvm::bitc::DEFINE_ABBREV) {
1660       if (llvm::Error Err = Cursor.JumpToBit(Offset))
1661         return Err;
1662       return llvm::Error::success();
1663     }
1664     if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1665       return Err;
1666   }
1667 }
1668 
1669 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1670                            unsigned &Idx) {
1671   Token Tok;
1672   Tok.startToken();
1673   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1674   Tok.setLength(Record[Idx++]);
1675   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1676     Tok.setIdentifierInfo(II);
1677   Tok.setKind((tok::TokenKind)Record[Idx++]);
1678   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1679   return Tok;
1680 }
1681 
1682 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1683   BitstreamCursor &Stream = F.MacroCursor;
1684 
1685   // Keep track of where we are in the stream, then jump back there
1686   // after reading this macro.
1687   SavedStreamPosition SavedPosition(Stream);
1688 
1689   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1690     // FIXME this drops errors on the floor.
1691     consumeError(std::move(Err));
1692     return nullptr;
1693   }
1694   RecordData Record;
1695   SmallVector<IdentifierInfo*, 16> MacroParams;
1696   MacroInfo *Macro = nullptr;
1697   llvm::MutableArrayRef<Token> MacroTokens;
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       MacroTokens = MI->allocateTokens(Record[NextIndex++],
1753                                        PP.getPreprocessorAllocator());
1754       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1755         // Decode function-like macro info.
1756         bool isC99VarArgs = Record[NextIndex++];
1757         bool isGNUVarArgs = Record[NextIndex++];
1758         bool hasCommaPasting = Record[NextIndex++];
1759         MacroParams.clear();
1760         unsigned NumArgs = Record[NextIndex++];
1761         for (unsigned i = 0; i != NumArgs; ++i)
1762           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1763 
1764         // Install function-like macro info.
1765         MI->setIsFunctionLike();
1766         if (isC99VarArgs) MI->setIsC99Varargs();
1767         if (isGNUVarArgs) MI->setIsGNUVarargs();
1768         if (hasCommaPasting) MI->setHasCommaPasting();
1769         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1770       }
1771 
1772       // Remember that we saw this macro last so that we add the tokens that
1773       // form its body to it.
1774       Macro = MI;
1775 
1776       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1777           Record[NextIndex]) {
1778         // We have a macro definition. Register the association
1779         PreprocessedEntityID
1780             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1781         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1782         PreprocessingRecord::PPEntityID PPID =
1783             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1784         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1785             PPRec.getPreprocessedEntity(PPID));
1786         if (PPDef)
1787           PPRec.RegisterMacroDefinition(Macro, PPDef);
1788       }
1789 
1790       ++NumMacrosRead;
1791       break;
1792     }
1793 
1794     case PP_TOKEN: {
1795       // If we see a TOKEN before a PP_MACRO_*, then the file is
1796       // erroneous, just pretend we didn't see this.
1797       if (!Macro) break;
1798       if (MacroTokens.empty()) {
1799         Error("unexpected number of macro tokens for a macro in AST file");
1800         return Macro;
1801       }
1802 
1803       unsigned Idx = 0;
1804       MacroTokens[0] = ReadToken(F, Record, Idx);
1805       MacroTokens = MacroTokens.drop_front();
1806       break;
1807     }
1808     }
1809   }
1810 }
1811 
1812 PreprocessedEntityID
1813 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1814                                          unsigned LocalID) const {
1815   if (!M.ModuleOffsetMap.empty())
1816     ReadModuleOffsetMap(M);
1817 
1818   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1819     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1820   assert(I != M.PreprocessedEntityRemap.end()
1821          && "Invalid index into preprocessed entity index remap");
1822 
1823   return LocalID + I->second;
1824 }
1825 
1826 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1827   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1828 }
1829 
1830 HeaderFileInfoTrait::internal_key_type
1831 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1832   internal_key_type ikey = {FE->getSize(),
1833                             M.HasTimestamps ? FE->getModificationTime() : 0,
1834                             FE->getName(), /*Imported*/ false};
1835   return ikey;
1836 }
1837 
1838 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1839   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1840     return false;
1841 
1842   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1843     return true;
1844 
1845   // Determine whether the actual files are equivalent.
1846   FileManager &FileMgr = Reader.getFileManager();
1847   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1848     if (!Key.Imported) {
1849       if (auto File = FileMgr.getFile(Key.Filename))
1850         return *File;
1851       return nullptr;
1852     }
1853 
1854     std::string Resolved = std::string(Key.Filename);
1855     Reader.ResolveImportedPath(M, Resolved);
1856     if (auto File = FileMgr.getFile(Resolved))
1857       return *File;
1858     return nullptr;
1859   };
1860 
1861   const FileEntry *FEA = GetFile(a);
1862   const FileEntry *FEB = GetFile(b);
1863   return FEA && FEA == FEB;
1864 }
1865 
1866 std::pair<unsigned, unsigned>
1867 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1868   return readULEBKeyDataLength(d);
1869 }
1870 
1871 HeaderFileInfoTrait::internal_key_type
1872 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1873   using namespace llvm::support;
1874 
1875   internal_key_type ikey;
1876   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1877   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1878   ikey.Filename = (const char *)d;
1879   ikey.Imported = true;
1880   return ikey;
1881 }
1882 
1883 HeaderFileInfoTrait::data_type
1884 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1885                               unsigned DataLen) {
1886   using namespace llvm::support;
1887 
1888   const unsigned char *End = d + DataLen;
1889   HeaderFileInfo HFI;
1890   unsigned Flags = *d++;
1891   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1892   HFI.isImport |= (Flags >> 5) & 0x01;
1893   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1894   HFI.DirInfo = (Flags >> 1) & 0x07;
1895   HFI.IndexHeaderMapHeader = Flags & 0x01;
1896   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1897       M, endian::readNext<uint32_t, little, unaligned>(d));
1898   if (unsigned FrameworkOffset =
1899           endian::readNext<uint32_t, little, unaligned>(d)) {
1900     // The framework offset is 1 greater than the actual offset,
1901     // since 0 is used as an indicator for "no framework name".
1902     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1903     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1904   }
1905 
1906   assert((End - d) % 4 == 0 &&
1907          "Wrong data length in HeaderFileInfo deserialization");
1908   while (d != End) {
1909     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1910     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1911     LocalSMID >>= 2;
1912 
1913     // This header is part of a module. Associate it with the module to enable
1914     // implicit module import.
1915     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1916     Module *Mod = Reader.getSubmodule(GlobalSMID);
1917     FileManager &FileMgr = Reader.getFileManager();
1918     ModuleMap &ModMap =
1919         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1920 
1921     std::string Filename = std::string(key.Filename);
1922     if (key.Imported)
1923       Reader.ResolveImportedPath(M, Filename);
1924     // FIXME: NameAsWritten
1925     Module::Header H = {std::string(key.Filename), "",
1926                         *FileMgr.getFile(Filename)};
1927     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1928     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1929   }
1930 
1931   // This HeaderFileInfo was externally loaded.
1932   HFI.External = true;
1933   HFI.IsValid = true;
1934   return HFI;
1935 }
1936 
1937 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1938                                 uint32_t MacroDirectivesOffset) {
1939   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1940   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1941 }
1942 
1943 void ASTReader::ReadDefinedMacros() {
1944   // Note that we are loading defined macros.
1945   Deserializing Macros(this);
1946 
1947   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1948     BitstreamCursor &MacroCursor = I.MacroCursor;
1949 
1950     // If there was no preprocessor block, skip this file.
1951     if (MacroCursor.getBitcodeBytes().empty())
1952       continue;
1953 
1954     BitstreamCursor Cursor = MacroCursor;
1955     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1956       Error(std::move(Err));
1957       return;
1958     }
1959 
1960     RecordData Record;
1961     while (true) {
1962       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1963       if (!MaybeE) {
1964         Error(MaybeE.takeError());
1965         return;
1966       }
1967       llvm::BitstreamEntry E = MaybeE.get();
1968 
1969       switch (E.Kind) {
1970       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1971       case llvm::BitstreamEntry::Error:
1972         Error("malformed block record in AST file");
1973         return;
1974       case llvm::BitstreamEntry::EndBlock:
1975         goto NextCursor;
1976 
1977       case llvm::BitstreamEntry::Record: {
1978         Record.clear();
1979         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1980         if (!MaybeRecord) {
1981           Error(MaybeRecord.takeError());
1982           return;
1983         }
1984         switch (MaybeRecord.get()) {
1985         default:  // Default behavior: ignore.
1986           break;
1987 
1988         case PP_MACRO_OBJECT_LIKE:
1989         case PP_MACRO_FUNCTION_LIKE: {
1990           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1991           if (II->isOutOfDate())
1992             updateOutOfDateIdentifier(*II);
1993           break;
1994         }
1995 
1996         case PP_TOKEN:
1997           // Ignore tokens.
1998           break;
1999         }
2000         break;
2001       }
2002       }
2003     }
2004     NextCursor:  ;
2005   }
2006 }
2007 
2008 namespace {
2009 
2010   /// Visitor class used to look up identifirs in an AST file.
2011   class IdentifierLookupVisitor {
2012     StringRef Name;
2013     unsigned NameHash;
2014     unsigned PriorGeneration;
2015     unsigned &NumIdentifierLookups;
2016     unsigned &NumIdentifierLookupHits;
2017     IdentifierInfo *Found = nullptr;
2018 
2019   public:
2020     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2021                             unsigned &NumIdentifierLookups,
2022                             unsigned &NumIdentifierLookupHits)
2023       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2024         PriorGeneration(PriorGeneration),
2025         NumIdentifierLookups(NumIdentifierLookups),
2026         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2027 
2028     bool operator()(ModuleFile &M) {
2029       // If we've already searched this module file, skip it now.
2030       if (M.Generation <= PriorGeneration)
2031         return true;
2032 
2033       ASTIdentifierLookupTable *IdTable
2034         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2035       if (!IdTable)
2036         return false;
2037 
2038       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2039                                      Found);
2040       ++NumIdentifierLookups;
2041       ASTIdentifierLookupTable::iterator Pos =
2042           IdTable->find_hashed(Name, NameHash, &Trait);
2043       if (Pos == IdTable->end())
2044         return false;
2045 
2046       // Dereferencing the iterator has the effect of building the
2047       // IdentifierInfo node and populating it with the various
2048       // declarations it needs.
2049       ++NumIdentifierLookupHits;
2050       Found = *Pos;
2051       return true;
2052     }
2053 
2054     // Retrieve the identifier info found within the module
2055     // files.
2056     IdentifierInfo *getIdentifierInfo() const { return Found; }
2057   };
2058 
2059 } // namespace
2060 
2061 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2062   // Note that we are loading an identifier.
2063   Deserializing AnIdentifier(this);
2064 
2065   unsigned PriorGeneration = 0;
2066   if (getContext().getLangOpts().Modules)
2067     PriorGeneration = IdentifierGeneration[&II];
2068 
2069   // If there is a global index, look there first to determine which modules
2070   // provably do not have any results for this identifier.
2071   GlobalModuleIndex::HitSet Hits;
2072   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2073   if (!loadGlobalIndex()) {
2074     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2075       HitsPtr = &Hits;
2076     }
2077   }
2078 
2079   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2080                                   NumIdentifierLookups,
2081                                   NumIdentifierLookupHits);
2082   ModuleMgr.visit(Visitor, HitsPtr);
2083   markIdentifierUpToDate(&II);
2084 }
2085 
2086 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2087   if (!II)
2088     return;
2089 
2090   II->setOutOfDate(false);
2091 
2092   // Update the generation for this identifier.
2093   if (getContext().getLangOpts().Modules)
2094     IdentifierGeneration[II] = getGeneration();
2095 }
2096 
2097 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2098                                     const PendingMacroInfo &PMInfo) {
2099   ModuleFile &M = *PMInfo.M;
2100 
2101   BitstreamCursor &Cursor = M.MacroCursor;
2102   SavedStreamPosition SavedPosition(Cursor);
2103   if (llvm::Error Err =
2104           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2105     Error(std::move(Err));
2106     return;
2107   }
2108 
2109   struct ModuleMacroRecord {
2110     SubmoduleID SubModID;
2111     MacroInfo *MI;
2112     SmallVector<SubmoduleID, 8> Overrides;
2113   };
2114   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2115 
2116   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2117   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2118   // macro histroy.
2119   RecordData Record;
2120   while (true) {
2121     Expected<llvm::BitstreamEntry> MaybeEntry =
2122         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2123     if (!MaybeEntry) {
2124       Error(MaybeEntry.takeError());
2125       return;
2126     }
2127     llvm::BitstreamEntry Entry = MaybeEntry.get();
2128 
2129     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2130       Error("malformed block record in AST file");
2131       return;
2132     }
2133 
2134     Record.clear();
2135     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2136     if (!MaybePP) {
2137       Error(MaybePP.takeError());
2138       return;
2139     }
2140     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2141     case PP_MACRO_DIRECTIVE_HISTORY:
2142       break;
2143 
2144     case PP_MODULE_MACRO: {
2145       ModuleMacros.push_back(ModuleMacroRecord());
2146       auto &Info = ModuleMacros.back();
2147       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2148       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2149       for (int I = 2, N = Record.size(); I != N; ++I)
2150         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2151       continue;
2152     }
2153 
2154     default:
2155       Error("malformed block record in AST file");
2156       return;
2157     }
2158 
2159     // We found the macro directive history; that's the last record
2160     // for this macro.
2161     break;
2162   }
2163 
2164   // Module macros are listed in reverse dependency order.
2165   {
2166     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2167     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2168     for (auto &MMR : ModuleMacros) {
2169       Overrides.clear();
2170       for (unsigned ModID : MMR.Overrides) {
2171         Module *Mod = getSubmodule(ModID);
2172         auto *Macro = PP.getModuleMacro(Mod, II);
2173         assert(Macro && "missing definition for overridden macro");
2174         Overrides.push_back(Macro);
2175       }
2176 
2177       bool Inserted = false;
2178       Module *Owner = getSubmodule(MMR.SubModID);
2179       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2180     }
2181   }
2182 
2183   // Don't read the directive history for a module; we don't have anywhere
2184   // to put it.
2185   if (M.isModule())
2186     return;
2187 
2188   // Deserialize the macro directives history in reverse source-order.
2189   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2190   unsigned Idx = 0, N = Record.size();
2191   while (Idx < N) {
2192     MacroDirective *MD = nullptr;
2193     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2194     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2195     switch (K) {
2196     case MacroDirective::MD_Define: {
2197       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2198       MD = PP.AllocateDefMacroDirective(MI, Loc);
2199       break;
2200     }
2201     case MacroDirective::MD_Undefine:
2202       MD = PP.AllocateUndefMacroDirective(Loc);
2203       break;
2204     case MacroDirective::MD_Visibility:
2205       bool isPublic = Record[Idx++];
2206       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2207       break;
2208     }
2209 
2210     if (!Latest)
2211       Latest = MD;
2212     if (Earliest)
2213       Earliest->setPrevious(MD);
2214     Earliest = MD;
2215   }
2216 
2217   if (Latest)
2218     PP.setLoadedMacroDirective(II, Earliest, Latest);
2219 }
2220 
2221 bool ASTReader::shouldDisableValidationForFile(
2222     const serialization::ModuleFile &M) const {
2223   if (DisableValidationKind == DisableValidationForModuleKind::None)
2224     return false;
2225 
2226   // If a PCH is loaded and validation is disabled for PCH then disable
2227   // validation for the PCH and the modules it loads.
2228   ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2229 
2230   switch (K) {
2231   case MK_MainFile:
2232   case MK_Preamble:
2233   case MK_PCH:
2234     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2235   case MK_ImplicitModule:
2236   case MK_ExplicitModule:
2237   case MK_PrebuiltModule:
2238     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2239   }
2240 
2241   return false;
2242 }
2243 
2244 ASTReader::InputFileInfo
2245 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2246   // Go find this input file.
2247   BitstreamCursor &Cursor = F.InputFilesCursor;
2248   SavedStreamPosition SavedPosition(Cursor);
2249   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2250     // FIXME this drops errors on the floor.
2251     consumeError(std::move(Err));
2252   }
2253 
2254   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2255   if (!MaybeCode) {
2256     // FIXME this drops errors on the floor.
2257     consumeError(MaybeCode.takeError());
2258   }
2259   unsigned Code = MaybeCode.get();
2260   RecordData Record;
2261   StringRef Blob;
2262 
2263   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2264     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2265            "invalid record type for input file");
2266   else {
2267     // FIXME this drops errors on the floor.
2268     consumeError(Maybe.takeError());
2269   }
2270 
2271   assert(Record[0] == ID && "Bogus stored ID or offset");
2272   InputFileInfo R;
2273   R.StoredSize = static_cast<off_t>(Record[1]);
2274   R.StoredTime = static_cast<time_t>(Record[2]);
2275   R.Overridden = static_cast<bool>(Record[3]);
2276   R.Transient = static_cast<bool>(Record[4]);
2277   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2278   R.Filename = std::string(Blob);
2279   ResolveImportedPath(F, R.Filename);
2280 
2281   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2282   if (!MaybeEntry) // FIXME this drops errors on the floor.
2283     consumeError(MaybeEntry.takeError());
2284   llvm::BitstreamEntry Entry = MaybeEntry.get();
2285   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2286          "expected record type for input file hash");
2287 
2288   Record.clear();
2289   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2290     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2291            "invalid record type for input file hash");
2292   else {
2293     // FIXME this drops errors on the floor.
2294     consumeError(Maybe.takeError());
2295   }
2296   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2297                   static_cast<uint64_t>(Record[0]);
2298   return R;
2299 }
2300 
2301 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2302 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2303   // If this ID is bogus, just return an empty input file.
2304   if (ID == 0 || ID > F.InputFilesLoaded.size())
2305     return InputFile();
2306 
2307   // If we've already loaded this input file, return it.
2308   if (F.InputFilesLoaded[ID-1].getFile())
2309     return F.InputFilesLoaded[ID-1];
2310 
2311   if (F.InputFilesLoaded[ID-1].isNotFound())
2312     return InputFile();
2313 
2314   // Go find this input file.
2315   BitstreamCursor &Cursor = F.InputFilesCursor;
2316   SavedStreamPosition SavedPosition(Cursor);
2317   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2318     // FIXME this drops errors on the floor.
2319     consumeError(std::move(Err));
2320   }
2321 
2322   InputFileInfo FI = readInputFileInfo(F, ID);
2323   off_t StoredSize = FI.StoredSize;
2324   time_t StoredTime = FI.StoredTime;
2325   bool Overridden = FI.Overridden;
2326   bool Transient = FI.Transient;
2327   StringRef Filename = FI.Filename;
2328   uint64_t StoredContentHash = FI.ContentHash;
2329 
2330   OptionalFileEntryRefDegradesToFileEntryPtr File =
2331       expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2332 
2333   // If we didn't find the file, resolve it relative to the
2334   // original directory from which this AST file was created.
2335   if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2336       F.OriginalDir != F.BaseDirectory) {
2337     std::string Resolved = resolveFileRelativeToOriginalDir(
2338         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2339     if (!Resolved.empty())
2340       File = expectedToOptional(FileMgr.getFileRef(Resolved));
2341   }
2342 
2343   // For an overridden file, create a virtual file with the stored
2344   // size/timestamp.
2345   if ((Overridden || Transient) && !File)
2346     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2347 
2348   if (!File) {
2349     if (Complain) {
2350       std::string ErrorStr = "could not find file '";
2351       ErrorStr += Filename;
2352       ErrorStr += "' referenced by AST file '";
2353       ErrorStr += F.FileName;
2354       ErrorStr += "'";
2355       Error(ErrorStr);
2356     }
2357     // Record that we didn't find the file.
2358     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2359     return InputFile();
2360   }
2361 
2362   // Check if there was a request to override the contents of the file
2363   // that was part of the precompiled header. Overriding such a file
2364   // can lead to problems when lexing using the source locations from the
2365   // PCH.
2366   SourceManager &SM = getSourceManager();
2367   // FIXME: Reject if the overrides are different.
2368   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2369     if (Complain)
2370       Error(diag::err_fe_pch_file_overridden, Filename);
2371 
2372     // After emitting the diagnostic, bypass the overriding file to recover
2373     // (this creates a separate FileEntry).
2374     File = SM.bypassFileContentsOverride(*File);
2375     if (!File) {
2376       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2377       return InputFile();
2378     }
2379   }
2380 
2381   struct Change {
2382     enum ModificationKind {
2383       Size,
2384       ModTime,
2385       Content,
2386       None,
2387     } Kind;
2388     llvm::Optional<int64_t> Old = llvm::None;
2389     llvm::Optional<int64_t> New = llvm::None;
2390   };
2391   auto HasInputFileChanged = [&]() {
2392     if (StoredSize != File->getSize())
2393       return Change{Change::Size, StoredSize, File->getSize()};
2394     if (!shouldDisableValidationForFile(F) && StoredTime &&
2395         StoredTime != File->getModificationTime()) {
2396       Change MTimeChange = {Change::ModTime, StoredTime,
2397                             File->getModificationTime()};
2398 
2399       // In case the modification time changes but not the content,
2400       // accept the cached file as legit.
2401       if (ValidateASTInputFilesContent &&
2402           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2403         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2404         if (!MemBuffOrError) {
2405           if (!Complain)
2406             return MTimeChange;
2407           std::string ErrorStr = "could not get buffer for file '";
2408           ErrorStr += File->getName();
2409           ErrorStr += "'";
2410           Error(ErrorStr);
2411           return MTimeChange;
2412         }
2413 
2414         // FIXME: hash_value is not guaranteed to be stable!
2415         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2416         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2417           return Change{Change::None};
2418 
2419         return Change{Change::Content};
2420       }
2421       return MTimeChange;
2422     }
2423     return Change{Change::None};
2424   };
2425 
2426   bool IsOutOfDate = false;
2427   auto FileChange = HasInputFileChanged();
2428   // For an overridden file, there is nothing to validate.
2429   if (!Overridden && FileChange.Kind != Change::None) {
2430     if (Complain && !Diags.isDiagnosticInFlight()) {
2431       // Build a list of the PCH imports that got us here (in reverse).
2432       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2433       while (!ImportStack.back()->ImportedBy.empty())
2434         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2435 
2436       // The top-level PCH is stale.
2437       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2438       Diag(diag::err_fe_ast_file_modified)
2439           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2440           << TopLevelPCHName << FileChange.Kind
2441           << (FileChange.Old && FileChange.New)
2442           << llvm::itostr(FileChange.Old.getValueOr(0))
2443           << llvm::itostr(FileChange.New.getValueOr(0));
2444 
2445       // Print the import stack.
2446       if (ImportStack.size() > 1) {
2447         Diag(diag::note_pch_required_by)
2448           << Filename << ImportStack[0]->FileName;
2449         for (unsigned I = 1; I < ImportStack.size(); ++I)
2450           Diag(diag::note_pch_required_by)
2451             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2452       }
2453 
2454       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2455     }
2456 
2457     IsOutOfDate = true;
2458   }
2459   // FIXME: If the file is overridden and we've already opened it,
2460   // issue an error (or split it into a separate FileEntry).
2461 
2462   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2463 
2464   // Note that we've loaded this input file.
2465   F.InputFilesLoaded[ID-1] = IF;
2466   return IF;
2467 }
2468 
2469 /// If we are loading a relocatable PCH or module file, and the filename
2470 /// is not an absolute path, add the system or module root to the beginning of
2471 /// the file name.
2472 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2473   // Resolve relative to the base directory, if we have one.
2474   if (!M.BaseDirectory.empty())
2475     return ResolveImportedPath(Filename, M.BaseDirectory);
2476 }
2477 
2478 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2479   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2480     return;
2481 
2482   SmallString<128> Buffer;
2483   llvm::sys::path::append(Buffer, Prefix, Filename);
2484   Filename.assign(Buffer.begin(), Buffer.end());
2485 }
2486 
2487 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2488   switch (ARR) {
2489   case ASTReader::Failure: return true;
2490   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2491   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2492   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2493   case ASTReader::ConfigurationMismatch:
2494     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2495   case ASTReader::HadErrors: return true;
2496   case ASTReader::Success: return false;
2497   }
2498 
2499   llvm_unreachable("unknown ASTReadResult");
2500 }
2501 
2502 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2503     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2504     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2505     std::string &SuggestedPredefines) {
2506   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2507     // FIXME this drops errors on the floor.
2508     consumeError(std::move(Err));
2509     return Failure;
2510   }
2511 
2512   // Read all of the records in the options block.
2513   RecordData Record;
2514   ASTReadResult Result = Success;
2515   while (true) {
2516     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2517     if (!MaybeEntry) {
2518       // FIXME this drops errors on the floor.
2519       consumeError(MaybeEntry.takeError());
2520       return Failure;
2521     }
2522     llvm::BitstreamEntry Entry = MaybeEntry.get();
2523 
2524     switch (Entry.Kind) {
2525     case llvm::BitstreamEntry::Error:
2526     case llvm::BitstreamEntry::SubBlock:
2527       return Failure;
2528 
2529     case llvm::BitstreamEntry::EndBlock:
2530       return Result;
2531 
2532     case llvm::BitstreamEntry::Record:
2533       // The interesting case.
2534       break;
2535     }
2536 
2537     // Read and process a record.
2538     Record.clear();
2539     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2540     if (!MaybeRecordType) {
2541       // FIXME this drops errors on the floor.
2542       consumeError(MaybeRecordType.takeError());
2543       return Failure;
2544     }
2545     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2546     case LANGUAGE_OPTIONS: {
2547       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2548       if (ParseLanguageOptions(Record, Complain, Listener,
2549                                AllowCompatibleConfigurationMismatch))
2550         Result = ConfigurationMismatch;
2551       break;
2552     }
2553 
2554     case TARGET_OPTIONS: {
2555       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2556       if (ParseTargetOptions(Record, Complain, Listener,
2557                              AllowCompatibleConfigurationMismatch))
2558         Result = ConfigurationMismatch;
2559       break;
2560     }
2561 
2562     case FILE_SYSTEM_OPTIONS: {
2563       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2564       if (!AllowCompatibleConfigurationMismatch &&
2565           ParseFileSystemOptions(Record, Complain, Listener))
2566         Result = ConfigurationMismatch;
2567       break;
2568     }
2569 
2570     case HEADER_SEARCH_OPTIONS: {
2571       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2572       if (!AllowCompatibleConfigurationMismatch &&
2573           ParseHeaderSearchOptions(Record, Complain, Listener))
2574         Result = ConfigurationMismatch;
2575       break;
2576     }
2577 
2578     case PREPROCESSOR_OPTIONS:
2579       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2580       if (!AllowCompatibleConfigurationMismatch &&
2581           ParsePreprocessorOptions(Record, Complain, Listener,
2582                                    SuggestedPredefines))
2583         Result = ConfigurationMismatch;
2584       break;
2585     }
2586   }
2587 }
2588 
2589 ASTReader::ASTReadResult
2590 ASTReader::ReadControlBlock(ModuleFile &F,
2591                             SmallVectorImpl<ImportedModule> &Loaded,
2592                             const ModuleFile *ImportedBy,
2593                             unsigned ClientLoadCapabilities) {
2594   BitstreamCursor &Stream = F.Stream;
2595 
2596   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2597     Error(std::move(Err));
2598     return Failure;
2599   }
2600 
2601   // Lambda to read the unhashed control block the first time it's called.
2602   //
2603   // For PCM files, the unhashed control block cannot be read until after the
2604   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2605   // need to look ahead before reading the IMPORTS record.  For consistency,
2606   // this block is always read somehow (see BitstreamEntry::EndBlock).
2607   bool HasReadUnhashedControlBlock = false;
2608   auto readUnhashedControlBlockOnce = [&]() {
2609     if (!HasReadUnhashedControlBlock) {
2610       HasReadUnhashedControlBlock = true;
2611       if (ASTReadResult Result =
2612               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2613         return Result;
2614     }
2615     return Success;
2616   };
2617 
2618   bool DisableValidation = shouldDisableValidationForFile(F);
2619 
2620   // Read all of the records and blocks in the control block.
2621   RecordData Record;
2622   unsigned NumInputs = 0;
2623   unsigned NumUserInputs = 0;
2624   StringRef BaseDirectoryAsWritten;
2625   while (true) {
2626     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2627     if (!MaybeEntry) {
2628       Error(MaybeEntry.takeError());
2629       return Failure;
2630     }
2631     llvm::BitstreamEntry Entry = MaybeEntry.get();
2632 
2633     switch (Entry.Kind) {
2634     case llvm::BitstreamEntry::Error:
2635       Error("malformed block record in AST file");
2636       return Failure;
2637     case llvm::BitstreamEntry::EndBlock: {
2638       // Validate the module before returning.  This call catches an AST with
2639       // no module name and no imports.
2640       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2641         return Result;
2642 
2643       // Validate input files.
2644       const HeaderSearchOptions &HSOpts =
2645           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2646 
2647       // All user input files reside at the index range [0, NumUserInputs), and
2648       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2649       // loaded module files, ignore missing inputs.
2650       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2651           F.Kind != MK_PrebuiltModule) {
2652         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2653 
2654         // If we are reading a module, we will create a verification timestamp,
2655         // so we verify all input files.  Otherwise, verify only user input
2656         // files.
2657 
2658         unsigned N = NumUserInputs;
2659         if (ValidateSystemInputs ||
2660             (HSOpts.ModulesValidateOncePerBuildSession &&
2661              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2662              F.Kind == MK_ImplicitModule))
2663           N = NumInputs;
2664 
2665         for (unsigned I = 0; I < N; ++I) {
2666           InputFile IF = getInputFile(F, I+1, Complain);
2667           if (!IF.getFile() || IF.isOutOfDate())
2668             return OutOfDate;
2669         }
2670       }
2671 
2672       if (Listener)
2673         Listener->visitModuleFile(F.FileName, F.Kind);
2674 
2675       if (Listener && Listener->needsInputFileVisitation()) {
2676         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2677                                                                 : NumUserInputs;
2678         for (unsigned I = 0; I < N; ++I) {
2679           bool IsSystem = I >= NumUserInputs;
2680           InputFileInfo FI = readInputFileInfo(F, I+1);
2681           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2682                                    F.Kind == MK_ExplicitModule ||
2683                                    F.Kind == MK_PrebuiltModule);
2684         }
2685       }
2686 
2687       return Success;
2688     }
2689 
2690     case llvm::BitstreamEntry::SubBlock:
2691       switch (Entry.ID) {
2692       case INPUT_FILES_BLOCK_ID:
2693         F.InputFilesCursor = Stream;
2694         if (llvm::Error Err = Stream.SkipBlock()) {
2695           Error(std::move(Err));
2696           return Failure;
2697         }
2698         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2699           Error("malformed block record in AST file");
2700           return Failure;
2701         }
2702         continue;
2703 
2704       case OPTIONS_BLOCK_ID:
2705         // If we're reading the first module for this group, check its options
2706         // are compatible with ours. For modules it imports, no further checking
2707         // is required, because we checked them when we built it.
2708         if (Listener && !ImportedBy) {
2709           // Should we allow the configuration of the module file to differ from
2710           // the configuration of the current translation unit in a compatible
2711           // way?
2712           //
2713           // FIXME: Allow this for files explicitly specified with -include-pch.
2714           bool AllowCompatibleConfigurationMismatch =
2715               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2716 
2717           ASTReadResult Result =
2718               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2719                                AllowCompatibleConfigurationMismatch, *Listener,
2720                                SuggestedPredefines);
2721           if (Result == Failure) {
2722             Error("malformed block record in AST file");
2723             return Result;
2724           }
2725 
2726           if (DisableValidation ||
2727               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2728             Result = Success;
2729 
2730           // If we can't load the module, exit early since we likely
2731           // will rebuild the module anyway. The stream may be in the
2732           // middle of a block.
2733           if (Result != Success)
2734             return Result;
2735         } else if (llvm::Error Err = Stream.SkipBlock()) {
2736           Error(std::move(Err));
2737           return Failure;
2738         }
2739         continue;
2740 
2741       default:
2742         if (llvm::Error Err = Stream.SkipBlock()) {
2743           Error(std::move(Err));
2744           return Failure;
2745         }
2746         continue;
2747       }
2748 
2749     case llvm::BitstreamEntry::Record:
2750       // The interesting case.
2751       break;
2752     }
2753 
2754     // Read and process a record.
2755     Record.clear();
2756     StringRef Blob;
2757     Expected<unsigned> MaybeRecordType =
2758         Stream.readRecord(Entry.ID, Record, &Blob);
2759     if (!MaybeRecordType) {
2760       Error(MaybeRecordType.takeError());
2761       return Failure;
2762     }
2763     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2764     case METADATA: {
2765       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2766         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2767           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2768                                         : diag::err_pch_version_too_new);
2769         return VersionMismatch;
2770       }
2771 
2772       bool hasErrors = Record[6];
2773       if (hasErrors && !DisableValidation) {
2774         // If requested by the caller and the module hasn't already been read
2775         // or compiled, mark modules on error as out-of-date.
2776         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2777             canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2778           return OutOfDate;
2779 
2780         if (!AllowASTWithCompilerErrors) {
2781           Diag(diag::err_pch_with_compiler_errors);
2782           return HadErrors;
2783         }
2784       }
2785       if (hasErrors) {
2786         Diags.ErrorOccurred = true;
2787         Diags.UncompilableErrorOccurred = true;
2788         Diags.UnrecoverableErrorOccurred = true;
2789       }
2790 
2791       F.RelocatablePCH = Record[4];
2792       // Relative paths in a relocatable PCH are relative to our sysroot.
2793       if (F.RelocatablePCH)
2794         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2795 
2796       F.HasTimestamps = Record[5];
2797 
2798       const std::string &CurBranch = getClangFullRepositoryVersion();
2799       StringRef ASTBranch = Blob;
2800       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2801         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2802           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2803         return VersionMismatch;
2804       }
2805       break;
2806     }
2807 
2808     case IMPORTS: {
2809       // Validate the AST before processing any imports (otherwise, untangling
2810       // them can be error-prone and expensive).  A module will have a name and
2811       // will already have been validated, but this catches the PCH case.
2812       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2813         return Result;
2814 
2815       // Load each of the imported PCH files.
2816       unsigned Idx = 0, N = Record.size();
2817       while (Idx < N) {
2818         // Read information about the AST file.
2819         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2820         // The import location will be the local one for now; we will adjust
2821         // all import locations of module imports after the global source
2822         // location info are setup, in ReadAST.
2823         SourceLocation ImportLoc =
2824             ReadUntranslatedSourceLocation(Record[Idx++]);
2825         off_t StoredSize = (off_t)Record[Idx++];
2826         time_t StoredModTime = (time_t)Record[Idx++];
2827         auto FirstSignatureByte = Record.begin() + Idx;
2828         ASTFileSignature StoredSignature = ASTFileSignature::create(
2829             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2830         Idx += ASTFileSignature::size;
2831 
2832         std::string ImportedName = ReadString(Record, Idx);
2833         std::string ImportedFile;
2834 
2835         // For prebuilt and explicit modules first consult the file map for
2836         // an override. Note that here we don't search prebuilt module
2837         // directories, only the explicit name to file mappings. Also, we will
2838         // still verify the size/signature making sure it is essentially the
2839         // same file but perhaps in a different location.
2840         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2841           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2842             ImportedName, /*FileMapOnly*/ true);
2843 
2844         if (ImportedFile.empty())
2845           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2846           // ModuleCache as when writing.
2847           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2848         else
2849           SkipPath(Record, Idx);
2850 
2851         // If our client can't cope with us being out of date, we can't cope with
2852         // our dependency being missing.
2853         unsigned Capabilities = ClientLoadCapabilities;
2854         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2855           Capabilities &= ~ARR_Missing;
2856 
2857         // Load the AST file.
2858         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2859                                   Loaded, StoredSize, StoredModTime,
2860                                   StoredSignature, Capabilities);
2861 
2862         // If we diagnosed a problem, produce a backtrace.
2863         bool recompilingFinalized =
2864             Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
2865             getModuleManager().getModuleCache().isPCMFinal(F.FileName);
2866         if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
2867           Diag(diag::note_module_file_imported_by)
2868               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2869         if (recompilingFinalized)
2870           Diag(diag::note_module_file_conflict);
2871 
2872         switch (Result) {
2873         case Failure: return Failure;
2874           // If we have to ignore the dependency, we'll have to ignore this too.
2875         case Missing:
2876         case OutOfDate: return OutOfDate;
2877         case VersionMismatch: return VersionMismatch;
2878         case ConfigurationMismatch: return ConfigurationMismatch;
2879         case HadErrors: return HadErrors;
2880         case Success: break;
2881         }
2882       }
2883       break;
2884     }
2885 
2886     case ORIGINAL_FILE:
2887       F.OriginalSourceFileID = FileID::get(Record[0]);
2888       F.ActualOriginalSourceFileName = std::string(Blob);
2889       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2890       ResolveImportedPath(F, F.OriginalSourceFileName);
2891       break;
2892 
2893     case ORIGINAL_FILE_ID:
2894       F.OriginalSourceFileID = FileID::get(Record[0]);
2895       break;
2896 
2897     case ORIGINAL_PCH_DIR:
2898       F.OriginalDir = std::string(Blob);
2899       break;
2900 
2901     case MODULE_NAME:
2902       F.ModuleName = std::string(Blob);
2903       Diag(diag::remark_module_import)
2904           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2905           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2906       if (Listener)
2907         Listener->ReadModuleName(F.ModuleName);
2908 
2909       // Validate the AST as soon as we have a name so we can exit early on
2910       // failure.
2911       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2912         return Result;
2913 
2914       break;
2915 
2916     case MODULE_DIRECTORY: {
2917       // Save the BaseDirectory as written in the PCM for computing the module
2918       // filename for the ModuleCache.
2919       BaseDirectoryAsWritten = Blob;
2920       assert(!F.ModuleName.empty() &&
2921              "MODULE_DIRECTORY found before MODULE_NAME");
2922       // If we've already loaded a module map file covering this module, we may
2923       // have a better path for it (relative to the current build).
2924       Module *M = PP.getHeaderSearchInfo().lookupModule(
2925           F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
2926           /*AllowExtraModuleMapSearch*/ true);
2927       if (M && M->Directory) {
2928         // If we're implicitly loading a module, the base directory can't
2929         // change between the build and use.
2930         // Don't emit module relocation error if we have -fno-validate-pch
2931         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2932                   DisableValidationForModuleKind::Module) &&
2933             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2934           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2935           if (!BuildDir || *BuildDir != M->Directory) {
2936             if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2937               Diag(diag::err_imported_module_relocated)
2938                   << F.ModuleName << Blob << M->Directory->getName();
2939             return OutOfDate;
2940           }
2941         }
2942         F.BaseDirectory = std::string(M->Directory->getName());
2943       } else {
2944         F.BaseDirectory = std::string(Blob);
2945       }
2946       break;
2947     }
2948 
2949     case MODULE_MAP_FILE:
2950       if (ASTReadResult Result =
2951               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2952         return Result;
2953       break;
2954 
2955     case INPUT_FILE_OFFSETS:
2956       NumInputs = Record[0];
2957       NumUserInputs = Record[1];
2958       F.InputFileOffsets =
2959           (const llvm::support::unaligned_uint64_t *)Blob.data();
2960       F.InputFilesLoaded.resize(NumInputs);
2961       F.NumUserInputFiles = NumUserInputs;
2962       break;
2963     }
2964   }
2965 }
2966 
2967 void ASTReader::readIncludedFiles(ModuleFile &F, StringRef Blob,
2968                                   Preprocessor &PP) {
2969   using namespace llvm::support;
2970 
2971   const unsigned char *D = (const unsigned char *)Blob.data();
2972   unsigned FileCount = endian::readNext<uint32_t, little, unaligned>(D);
2973 
2974   for (unsigned I = 0; I < FileCount; ++I) {
2975     size_t ID = endian::readNext<uint32_t, little, unaligned>(D);
2976     InputFileInfo IFI = readInputFileInfo(F, ID);
2977     if (llvm::ErrorOr<const FileEntry *> File =
2978             PP.getFileManager().getFile(IFI.Filename))
2979       PP.getIncludedFiles().insert(*File);
2980   }
2981 }
2982 
2983 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
2984                                     unsigned ClientLoadCapabilities) {
2985   BitstreamCursor &Stream = F.Stream;
2986 
2987   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
2988     return Err;
2989   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2990 
2991   // Read all of the records and blocks for the AST file.
2992   RecordData Record;
2993   while (true) {
2994     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2995     if (!MaybeEntry)
2996       return MaybeEntry.takeError();
2997     llvm::BitstreamEntry Entry = MaybeEntry.get();
2998 
2999     switch (Entry.Kind) {
3000     case llvm::BitstreamEntry::Error:
3001       return llvm::createStringError(
3002           std::errc::illegal_byte_sequence,
3003           "error at end of module block in AST file");
3004     case llvm::BitstreamEntry::EndBlock:
3005       // Outside of C++, we do not store a lookup map for the translation unit.
3006       // Instead, mark it as needing a lookup map to be built if this module
3007       // contains any declarations lexically within it (which it always does!).
3008       // This usually has no cost, since we very rarely need the lookup map for
3009       // the translation unit outside C++.
3010       if (ASTContext *Ctx = ContextObj) {
3011         DeclContext *DC = Ctx->getTranslationUnitDecl();
3012         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3013           DC->setMustBuildLookupTable();
3014       }
3015 
3016       return llvm::Error::success();
3017     case llvm::BitstreamEntry::SubBlock:
3018       switch (Entry.ID) {
3019       case DECLTYPES_BLOCK_ID:
3020         // We lazily load the decls block, but we want to set up the
3021         // DeclsCursor cursor to point into it.  Clone our current bitcode
3022         // cursor to it, enter the block and read the abbrevs in that block.
3023         // With the main cursor, we just skip over it.
3024         F.DeclsCursor = Stream;
3025         if (llvm::Error Err = Stream.SkipBlock())
3026           return Err;
3027         if (llvm::Error Err = ReadBlockAbbrevs(
3028                 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))
3029           return Err;
3030         break;
3031 
3032       case PREPROCESSOR_BLOCK_ID:
3033         F.MacroCursor = Stream;
3034         if (!PP.getExternalSource())
3035           PP.setExternalSource(this);
3036 
3037         if (llvm::Error Err = Stream.SkipBlock())
3038           return Err;
3039         if (llvm::Error Err =
3040                 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3041           return Err;
3042         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3043         break;
3044 
3045       case PREPROCESSOR_DETAIL_BLOCK_ID:
3046         F.PreprocessorDetailCursor = Stream;
3047 
3048         if (llvm::Error Err = Stream.SkipBlock()) {
3049           return Err;
3050         }
3051         if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3052                                                PREPROCESSOR_DETAIL_BLOCK_ID))
3053           return Err;
3054         F.PreprocessorDetailStartOffset
3055         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3056 
3057         if (!PP.getPreprocessingRecord())
3058           PP.createPreprocessingRecord();
3059         if (!PP.getPreprocessingRecord()->getExternalSource())
3060           PP.getPreprocessingRecord()->SetExternalSource(*this);
3061         break;
3062 
3063       case SOURCE_MANAGER_BLOCK_ID:
3064         if (llvm::Error Err = ReadSourceManagerBlock(F))
3065           return Err;
3066         break;
3067 
3068       case SUBMODULE_BLOCK_ID:
3069         if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3070           return Err;
3071         break;
3072 
3073       case COMMENTS_BLOCK_ID: {
3074         BitstreamCursor C = Stream;
3075 
3076         if (llvm::Error Err = Stream.SkipBlock())
3077           return Err;
3078         if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3079           return Err;
3080         CommentsCursors.push_back(std::make_pair(C, &F));
3081         break;
3082       }
3083 
3084       default:
3085         if (llvm::Error Err = Stream.SkipBlock())
3086           return Err;
3087         break;
3088       }
3089       continue;
3090 
3091     case llvm::BitstreamEntry::Record:
3092       // The interesting case.
3093       break;
3094     }
3095 
3096     // Read and process a record.
3097     Record.clear();
3098     StringRef Blob;
3099     Expected<unsigned> MaybeRecordType =
3100         Stream.readRecord(Entry.ID, Record, &Blob);
3101     if (!MaybeRecordType)
3102       return MaybeRecordType.takeError();
3103     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3104 
3105     // If we're not loading an AST context, we don't care about most records.
3106     if (!ContextObj) {
3107       switch (RecordType) {
3108       case IDENTIFIER_TABLE:
3109       case IDENTIFIER_OFFSET:
3110       case INTERESTING_IDENTIFIERS:
3111       case STATISTICS:
3112       case PP_CONDITIONAL_STACK:
3113       case PP_COUNTER_VALUE:
3114       case SOURCE_LOCATION_OFFSETS:
3115       case MODULE_OFFSET_MAP:
3116       case SOURCE_MANAGER_LINE_TABLE:
3117       case SOURCE_LOCATION_PRELOADS:
3118       case PPD_ENTITIES_OFFSETS:
3119       case HEADER_SEARCH_TABLE:
3120       case IMPORTED_MODULES:
3121       case MACRO_OFFSET:
3122         break;
3123       default:
3124         continue;
3125       }
3126     }
3127 
3128     switch (RecordType) {
3129     default:  // Default behavior: ignore.
3130       break;
3131 
3132     case TYPE_OFFSET: {
3133       if (F.LocalNumTypes != 0)
3134         return llvm::createStringError(
3135             std::errc::illegal_byte_sequence,
3136             "duplicate TYPE_OFFSET record in AST file");
3137       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3138       F.LocalNumTypes = Record[0];
3139       unsigned LocalBaseTypeIndex = Record[1];
3140       F.BaseTypeIndex = getTotalNumTypes();
3141 
3142       if (F.LocalNumTypes > 0) {
3143         // Introduce the global -> local mapping for types within this module.
3144         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3145 
3146         // Introduce the local -> global mapping for types within this module.
3147         F.TypeRemap.insertOrReplace(
3148           std::make_pair(LocalBaseTypeIndex,
3149                          F.BaseTypeIndex - LocalBaseTypeIndex));
3150 
3151         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3152       }
3153       break;
3154     }
3155 
3156     case DECL_OFFSET: {
3157       if (F.LocalNumDecls != 0)
3158         return llvm::createStringError(
3159             std::errc::illegal_byte_sequence,
3160             "duplicate DECL_OFFSET record in AST file");
3161       F.DeclOffsets = (const DeclOffset *)Blob.data();
3162       F.LocalNumDecls = Record[0];
3163       unsigned LocalBaseDeclID = Record[1];
3164       F.BaseDeclID = getTotalNumDecls();
3165 
3166       if (F.LocalNumDecls > 0) {
3167         // Introduce the global -> local mapping for declarations within this
3168         // module.
3169         GlobalDeclMap.insert(
3170           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3171 
3172         // Introduce the local -> global mapping for declarations within this
3173         // module.
3174         F.DeclRemap.insertOrReplace(
3175           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3176 
3177         // Introduce the global -> local mapping for declarations within this
3178         // module.
3179         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3180 
3181         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3182       }
3183       break;
3184     }
3185 
3186     case TU_UPDATE_LEXICAL: {
3187       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3188       LexicalContents Contents(
3189           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3190               Blob.data()),
3191           static_cast<unsigned int>(Blob.size() / 4));
3192       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3193       TU->setHasExternalLexicalStorage(true);
3194       break;
3195     }
3196 
3197     case UPDATE_VISIBLE: {
3198       unsigned Idx = 0;
3199       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3200       auto *Data = (const unsigned char*)Blob.data();
3201       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3202       // If we've already loaded the decl, perform the updates when we finish
3203       // loading this block.
3204       if (Decl *D = GetExistingDecl(ID))
3205         PendingUpdateRecords.push_back(
3206             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3207       break;
3208     }
3209 
3210     case IDENTIFIER_TABLE:
3211       F.IdentifierTableData =
3212           reinterpret_cast<const unsigned char *>(Blob.data());
3213       if (Record[0]) {
3214         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3215             F.IdentifierTableData + Record[0],
3216             F.IdentifierTableData + sizeof(uint32_t),
3217             F.IdentifierTableData,
3218             ASTIdentifierLookupTrait(*this, F));
3219 
3220         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3221       }
3222       break;
3223 
3224     case IDENTIFIER_OFFSET: {
3225       if (F.LocalNumIdentifiers != 0)
3226         return llvm::createStringError(
3227             std::errc::illegal_byte_sequence,
3228             "duplicate IDENTIFIER_OFFSET record in AST file");
3229       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3230       F.LocalNumIdentifiers = Record[0];
3231       unsigned LocalBaseIdentifierID = Record[1];
3232       F.BaseIdentifierID = getTotalNumIdentifiers();
3233 
3234       if (F.LocalNumIdentifiers > 0) {
3235         // Introduce the global -> local mapping for identifiers within this
3236         // module.
3237         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3238                                                   &F));
3239 
3240         // Introduce the local -> global mapping for identifiers within this
3241         // module.
3242         F.IdentifierRemap.insertOrReplace(
3243           std::make_pair(LocalBaseIdentifierID,
3244                          F.BaseIdentifierID - LocalBaseIdentifierID));
3245 
3246         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3247                                  + F.LocalNumIdentifiers);
3248       }
3249       break;
3250     }
3251 
3252     case INTERESTING_IDENTIFIERS:
3253       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3254       break;
3255 
3256     case EAGERLY_DESERIALIZED_DECLS:
3257       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3258       // about "interesting" decls (for instance, if we're building a module).
3259       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3260         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3261       break;
3262 
3263     case MODULAR_CODEGEN_DECLS:
3264       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3265       // them (ie: if we're not codegenerating this module).
3266       if (F.Kind == MK_MainFile ||
3267           getContext().getLangOpts().BuildingPCHWithObjectFile)
3268         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3269           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3270       break;
3271 
3272     case SPECIAL_TYPES:
3273       if (SpecialTypes.empty()) {
3274         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3275           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3276         break;
3277       }
3278 
3279       if (SpecialTypes.size() != Record.size())
3280         return llvm::createStringError(std::errc::illegal_byte_sequence,
3281                                        "invalid special-types record");
3282 
3283       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3284         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3285         if (!SpecialTypes[I])
3286           SpecialTypes[I] = ID;
3287         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3288         // merge step?
3289       }
3290       break;
3291 
3292     case STATISTICS:
3293       TotalNumStatements += Record[0];
3294       TotalNumMacros += Record[1];
3295       TotalLexicalDeclContexts += Record[2];
3296       TotalVisibleDeclContexts += Record[3];
3297       break;
3298 
3299     case UNUSED_FILESCOPED_DECLS:
3300       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3301         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3302       break;
3303 
3304     case DELEGATING_CTORS:
3305       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3306         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3307       break;
3308 
3309     case WEAK_UNDECLARED_IDENTIFIERS:
3310       if (Record.size() % 4 != 0)
3311         return llvm::createStringError(std::errc::illegal_byte_sequence,
3312                                        "invalid weak identifiers record");
3313 
3314       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3315       // files. This isn't the way to do it :)
3316       WeakUndeclaredIdentifiers.clear();
3317 
3318       // Translate the weak, undeclared identifiers into global IDs.
3319       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3320         WeakUndeclaredIdentifiers.push_back(
3321           getGlobalIdentifierID(F, Record[I++]));
3322         WeakUndeclaredIdentifiers.push_back(
3323           getGlobalIdentifierID(F, Record[I++]));
3324         WeakUndeclaredIdentifiers.push_back(
3325           ReadSourceLocation(F, Record, I).getRawEncoding());
3326         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3327       }
3328       break;
3329 
3330     case SELECTOR_OFFSETS: {
3331       F.SelectorOffsets = (const uint32_t *)Blob.data();
3332       F.LocalNumSelectors = Record[0];
3333       unsigned LocalBaseSelectorID = Record[1];
3334       F.BaseSelectorID = getTotalNumSelectors();
3335 
3336       if (F.LocalNumSelectors > 0) {
3337         // Introduce the global -> local mapping for selectors within this
3338         // module.
3339         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3340 
3341         // Introduce the local -> global mapping for selectors within this
3342         // module.
3343         F.SelectorRemap.insertOrReplace(
3344           std::make_pair(LocalBaseSelectorID,
3345                          F.BaseSelectorID - LocalBaseSelectorID));
3346 
3347         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3348       }
3349       break;
3350     }
3351 
3352     case METHOD_POOL:
3353       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3354       if (Record[0])
3355         F.SelectorLookupTable
3356           = ASTSelectorLookupTable::Create(
3357                         F.SelectorLookupTableData + Record[0],
3358                         F.SelectorLookupTableData,
3359                         ASTSelectorLookupTrait(*this, F));
3360       TotalNumMethodPoolEntries += Record[1];
3361       break;
3362 
3363     case REFERENCED_SELECTOR_POOL:
3364       if (!Record.empty()) {
3365         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3366           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3367                                                                 Record[Idx++]));
3368           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3369                                               getRawEncoding());
3370         }
3371       }
3372       break;
3373 
3374     case PP_CONDITIONAL_STACK:
3375       if (!Record.empty()) {
3376         unsigned Idx = 0, End = Record.size() - 1;
3377         bool ReachedEOFWhileSkipping = Record[Idx++];
3378         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3379         if (ReachedEOFWhileSkipping) {
3380           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3381           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3382           bool FoundNonSkipPortion = Record[Idx++];
3383           bool FoundElse = Record[Idx++];
3384           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3385           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3386                            FoundElse, ElseLoc);
3387         }
3388         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3389         while (Idx < End) {
3390           auto Loc = ReadSourceLocation(F, Record, Idx);
3391           bool WasSkipping = Record[Idx++];
3392           bool FoundNonSkip = Record[Idx++];
3393           bool FoundElse = Record[Idx++];
3394           ConditionalStack.push_back(
3395               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3396         }
3397         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3398       }
3399       break;
3400 
3401     case PP_COUNTER_VALUE:
3402       if (!Record.empty() && Listener)
3403         Listener->ReadCounter(F, Record[0]);
3404       break;
3405 
3406     case FILE_SORTED_DECLS:
3407       F.FileSortedDecls = (const DeclID *)Blob.data();
3408       F.NumFileSortedDecls = Record[0];
3409       break;
3410 
3411     case SOURCE_LOCATION_OFFSETS: {
3412       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3413       F.LocalNumSLocEntries = Record[0];
3414       SourceLocation::UIntTy SLocSpaceSize = Record[1];
3415       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3416       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3417           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3418                                               SLocSpaceSize);
3419       if (!F.SLocEntryBaseID)
3420         return llvm::createStringError(std::errc::invalid_argument,
3421                                        "ran out of source locations");
3422       // Make our entry in the range map. BaseID is negative and growing, so
3423       // we invert it. Because we invert it, though, we need the other end of
3424       // the range.
3425       unsigned RangeStart =
3426           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3427       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3428       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3429 
3430       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3431       assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3432       GlobalSLocOffsetMap.insert(
3433           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3434                            - SLocSpaceSize,&F));
3435 
3436       // Initialize the remapping table.
3437       // Invalid stays invalid.
3438       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3439       // This module. Base was 2 when being compiled.
3440       F.SLocRemap.insertOrReplace(std::make_pair(
3441           2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3442 
3443       TotalNumSLocEntries += F.LocalNumSLocEntries;
3444       break;
3445     }
3446 
3447     case MODULE_OFFSET_MAP:
3448       F.ModuleOffsetMap = Blob;
3449       break;
3450 
3451     case SOURCE_MANAGER_LINE_TABLE:
3452       ParseLineTable(F, Record);
3453       break;
3454 
3455     case SOURCE_LOCATION_PRELOADS: {
3456       // Need to transform from the local view (1-based IDs) to the global view,
3457       // which is based off F.SLocEntryBaseID.
3458       if (!F.PreloadSLocEntries.empty())
3459         return llvm::createStringError(
3460             std::errc::illegal_byte_sequence,
3461             "Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3462 
3463       F.PreloadSLocEntries.swap(Record);
3464       break;
3465     }
3466 
3467     case EXT_VECTOR_DECLS:
3468       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3469         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3470       break;
3471 
3472     case VTABLE_USES:
3473       if (Record.size() % 3 != 0)
3474         return llvm::createStringError(std::errc::illegal_byte_sequence,
3475                                        "Invalid VTABLE_USES record");
3476 
3477       // Later tables overwrite earlier ones.
3478       // FIXME: Modules will have some trouble with this. This is clearly not
3479       // the right way to do this.
3480       VTableUses.clear();
3481 
3482       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3483         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3484         VTableUses.push_back(
3485           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3486         VTableUses.push_back(Record[Idx++]);
3487       }
3488       break;
3489 
3490     case PENDING_IMPLICIT_INSTANTIATIONS:
3491       if (PendingInstantiations.size() % 2 != 0)
3492         return llvm::createStringError(
3493             std::errc::illegal_byte_sequence,
3494             "Invalid existing PendingInstantiations");
3495 
3496       if (Record.size() % 2 != 0)
3497         return llvm::createStringError(
3498             std::errc::illegal_byte_sequence,
3499             "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3500 
3501       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3502         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3503         PendingInstantiations.push_back(
3504           ReadSourceLocation(F, Record, I).getRawEncoding());
3505       }
3506       break;
3507 
3508     case SEMA_DECL_REFS:
3509       if (Record.size() != 3)
3510         return llvm::createStringError(std::errc::illegal_byte_sequence,
3511                                        "Invalid SEMA_DECL_REFS block");
3512       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3513         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3514       break;
3515 
3516     case PPD_ENTITIES_OFFSETS: {
3517       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3518       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3519       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3520 
3521       unsigned LocalBasePreprocessedEntityID = Record[0];
3522 
3523       unsigned StartingID;
3524       if (!PP.getPreprocessingRecord())
3525         PP.createPreprocessingRecord();
3526       if (!PP.getPreprocessingRecord()->getExternalSource())
3527         PP.getPreprocessingRecord()->SetExternalSource(*this);
3528       StartingID
3529         = PP.getPreprocessingRecord()
3530             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3531       F.BasePreprocessedEntityID = StartingID;
3532 
3533       if (F.NumPreprocessedEntities > 0) {
3534         // Introduce the global -> local mapping for preprocessed entities in
3535         // this module.
3536         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3537 
3538         // Introduce the local -> global mapping for preprocessed entities in
3539         // this module.
3540         F.PreprocessedEntityRemap.insertOrReplace(
3541           std::make_pair(LocalBasePreprocessedEntityID,
3542             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3543       }
3544 
3545       break;
3546     }
3547 
3548     case PPD_SKIPPED_RANGES: {
3549       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3550       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3551       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3552 
3553       if (!PP.getPreprocessingRecord())
3554         PP.createPreprocessingRecord();
3555       if (!PP.getPreprocessingRecord()->getExternalSource())
3556         PP.getPreprocessingRecord()->SetExternalSource(*this);
3557       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3558           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3559 
3560       if (F.NumPreprocessedSkippedRanges > 0)
3561         GlobalSkippedRangeMap.insert(
3562             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3563       break;
3564     }
3565 
3566     case DECL_UPDATE_OFFSETS:
3567       if (Record.size() % 2 != 0)
3568         return llvm::createStringError(
3569             std::errc::illegal_byte_sequence,
3570             "invalid DECL_UPDATE_OFFSETS block in AST file");
3571       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3572         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3573         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3574 
3575         // If we've already loaded the decl, perform the updates when we finish
3576         // loading this block.
3577         if (Decl *D = GetExistingDecl(ID))
3578           PendingUpdateRecords.push_back(
3579               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3580       }
3581       break;
3582 
3583     case OBJC_CATEGORIES_MAP:
3584       if (F.LocalNumObjCCategoriesInMap != 0)
3585         return llvm::createStringError(
3586             std::errc::illegal_byte_sequence,
3587             "duplicate OBJC_CATEGORIES_MAP record in AST file");
3588 
3589       F.LocalNumObjCCategoriesInMap = Record[0];
3590       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3591       break;
3592 
3593     case OBJC_CATEGORIES:
3594       F.ObjCCategories.swap(Record);
3595       break;
3596 
3597     case CUDA_SPECIAL_DECL_REFS:
3598       // Later tables overwrite earlier ones.
3599       // FIXME: Modules will have trouble with this.
3600       CUDASpecialDeclRefs.clear();
3601       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3602         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3603       break;
3604 
3605     case HEADER_SEARCH_TABLE:
3606       F.HeaderFileInfoTableData = Blob.data();
3607       F.LocalNumHeaderFileInfos = Record[1];
3608       if (Record[0]) {
3609         F.HeaderFileInfoTable
3610           = HeaderFileInfoLookupTable::Create(
3611                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3612                    (const unsigned char *)F.HeaderFileInfoTableData,
3613                    HeaderFileInfoTrait(*this, F,
3614                                        &PP.getHeaderSearchInfo(),
3615                                        Blob.data() + Record[2]));
3616 
3617         PP.getHeaderSearchInfo().SetExternalSource(this);
3618         if (!PP.getHeaderSearchInfo().getExternalLookup())
3619           PP.getHeaderSearchInfo().SetExternalLookup(this);
3620       }
3621       break;
3622 
3623     case FP_PRAGMA_OPTIONS:
3624       // Later tables overwrite earlier ones.
3625       FPPragmaOptions.swap(Record);
3626       break;
3627 
3628     case OPENCL_EXTENSIONS:
3629       for (unsigned I = 0, E = Record.size(); I != E; ) {
3630         auto Name = ReadString(Record, I);
3631         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3632         OptInfo.Supported = Record[I++] != 0;
3633         OptInfo.Enabled = Record[I++] != 0;
3634         OptInfo.WithPragma = Record[I++] != 0;
3635         OptInfo.Avail = Record[I++];
3636         OptInfo.Core = Record[I++];
3637         OptInfo.Opt = Record[I++];
3638       }
3639       break;
3640 
3641     case TENTATIVE_DEFINITIONS:
3642       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3643         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3644       break;
3645 
3646     case KNOWN_NAMESPACES:
3647       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3648         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3649       break;
3650 
3651     case UNDEFINED_BUT_USED:
3652       if (UndefinedButUsed.size() % 2 != 0)
3653         return llvm::createStringError(std::errc::illegal_byte_sequence,
3654                                        "Invalid existing UndefinedButUsed");
3655 
3656       if (Record.size() % 2 != 0)
3657         return llvm::createStringError(std::errc::illegal_byte_sequence,
3658                                        "invalid undefined-but-used record");
3659       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3660         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3661         UndefinedButUsed.push_back(
3662             ReadSourceLocation(F, Record, I).getRawEncoding());
3663       }
3664       break;
3665 
3666     case DELETE_EXPRS_TO_ANALYZE:
3667       for (unsigned I = 0, N = Record.size(); I != N;) {
3668         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3669         const uint64_t Count = Record[I++];
3670         DelayedDeleteExprs.push_back(Count);
3671         for (uint64_t C = 0; C < Count; ++C) {
3672           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3673           bool IsArrayForm = Record[I++] == 1;
3674           DelayedDeleteExprs.push_back(IsArrayForm);
3675         }
3676       }
3677       break;
3678 
3679     case IMPORTED_MODULES:
3680       if (!F.isModule()) {
3681         // If we aren't loading a module (which has its own exports), make
3682         // all of the imported modules visible.
3683         // FIXME: Deal with macros-only imports.
3684         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3685           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3686           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3687           if (GlobalID) {
3688             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3689             if (DeserializationListener)
3690               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3691           }
3692         }
3693       }
3694       break;
3695 
3696     case MACRO_OFFSET: {
3697       if (F.LocalNumMacros != 0)
3698         return llvm::createStringError(
3699             std::errc::illegal_byte_sequence,
3700             "duplicate MACRO_OFFSET record in AST file");
3701       F.MacroOffsets = (const uint32_t *)Blob.data();
3702       F.LocalNumMacros = Record[0];
3703       unsigned LocalBaseMacroID = Record[1];
3704       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3705       F.BaseMacroID = getTotalNumMacros();
3706 
3707       if (F.LocalNumMacros > 0) {
3708         // Introduce the global -> local mapping for macros within this module.
3709         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3710 
3711         // Introduce the local -> global mapping for macros within this module.
3712         F.MacroRemap.insertOrReplace(
3713           std::make_pair(LocalBaseMacroID,
3714                          F.BaseMacroID - LocalBaseMacroID));
3715 
3716         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3717       }
3718       break;
3719     }
3720 
3721     case PP_INCLUDED_FILES:
3722       readIncludedFiles(F, Blob, PP);
3723       break;
3724 
3725     case LATE_PARSED_TEMPLATE:
3726       LateParsedTemplates.emplace_back(
3727           std::piecewise_construct, std::forward_as_tuple(&F),
3728           std::forward_as_tuple(Record.begin(), Record.end()));
3729       break;
3730 
3731     case OPTIMIZE_PRAGMA_OPTIONS:
3732       if (Record.size() != 1)
3733         return llvm::createStringError(std::errc::illegal_byte_sequence,
3734                                        "invalid pragma optimize record");
3735       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3736       break;
3737 
3738     case MSSTRUCT_PRAGMA_OPTIONS:
3739       if (Record.size() != 1)
3740         return llvm::createStringError(std::errc::illegal_byte_sequence,
3741                                        "invalid pragma ms_struct record");
3742       PragmaMSStructState = Record[0];
3743       break;
3744 
3745     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3746       if (Record.size() != 2)
3747         return llvm::createStringError(
3748             std::errc::illegal_byte_sequence,
3749             "invalid pragma pointers to members record");
3750       PragmaMSPointersToMembersState = Record[0];
3751       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3752       break;
3753 
3754     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3755       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3756         UnusedLocalTypedefNameCandidates.push_back(
3757             getGlobalDeclID(F, Record[I]));
3758       break;
3759 
3760     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3761       if (Record.size() != 1)
3762         return llvm::createStringError(std::errc::illegal_byte_sequence,
3763                                        "invalid cuda pragma options record");
3764       ForceCUDAHostDeviceDepth = Record[0];
3765       break;
3766 
3767     case ALIGN_PACK_PRAGMA_OPTIONS: {
3768       if (Record.size() < 3)
3769         return llvm::createStringError(std::errc::illegal_byte_sequence,
3770                                        "invalid pragma pack record");
3771       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3772       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3773       unsigned NumStackEntries = Record[2];
3774       unsigned Idx = 3;
3775       // Reset the stack when importing a new module.
3776       PragmaAlignPackStack.clear();
3777       for (unsigned I = 0; I < NumStackEntries; ++I) {
3778         PragmaAlignPackStackEntry Entry;
3779         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3780         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3781         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3782         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3783         Entry.SlotLabel = PragmaAlignPackStrings.back();
3784         PragmaAlignPackStack.push_back(Entry);
3785       }
3786       break;
3787     }
3788 
3789     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3790       if (Record.size() < 3)
3791         return llvm::createStringError(std::errc::illegal_byte_sequence,
3792                                        "invalid pragma float control record");
3793       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3794       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3795       unsigned NumStackEntries = Record[2];
3796       unsigned Idx = 3;
3797       // Reset the stack when importing a new module.
3798       FpPragmaStack.clear();
3799       for (unsigned I = 0; I < NumStackEntries; ++I) {
3800         FpPragmaStackEntry Entry;
3801         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3802         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3803         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3804         FpPragmaStrings.push_back(ReadString(Record, Idx));
3805         Entry.SlotLabel = FpPragmaStrings.back();
3806         FpPragmaStack.push_back(Entry);
3807       }
3808       break;
3809     }
3810 
3811     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3812       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3813         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3814       break;
3815     }
3816   }
3817 }
3818 
3819 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3820   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3821 
3822   // Additional remapping information.
3823   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3824   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3825   F.ModuleOffsetMap = StringRef();
3826 
3827   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3828   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3829     F.SLocRemap.insert(std::make_pair(0U, 0));
3830     F.SLocRemap.insert(std::make_pair(2U, 1));
3831   }
3832 
3833   // Continuous range maps we may be updating in our module.
3834   using SLocRemapBuilder =
3835       ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
3836                          2>::Builder;
3837   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3838   SLocRemapBuilder SLocRemap(F.SLocRemap);
3839   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3840   RemapBuilder MacroRemap(F.MacroRemap);
3841   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3842   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3843   RemapBuilder SelectorRemap(F.SelectorRemap);
3844   RemapBuilder DeclRemap(F.DeclRemap);
3845   RemapBuilder TypeRemap(F.TypeRemap);
3846 
3847   while (Data < DataEnd) {
3848     // FIXME: Looking up dependency modules by filename is horrible. Let's
3849     // start fixing this with prebuilt, explicit and implicit modules and see
3850     // how it goes...
3851     using namespace llvm::support;
3852     ModuleKind Kind = static_cast<ModuleKind>(
3853       endian::readNext<uint8_t, little, unaligned>(Data));
3854     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3855     StringRef Name = StringRef((const char*)Data, Len);
3856     Data += Len;
3857     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3858                               Kind == MK_ImplicitModule
3859                           ? ModuleMgr.lookupByModuleName(Name)
3860                           : ModuleMgr.lookupByFileName(Name));
3861     if (!OM) {
3862       std::string Msg =
3863           "SourceLocation remap refers to unknown module, cannot find ";
3864       Msg.append(std::string(Name));
3865       Error(Msg);
3866       return;
3867     }
3868 
3869     SourceLocation::UIntTy SLocOffset =
3870         endian::readNext<uint32_t, little, unaligned>(Data);
3871     uint32_t IdentifierIDOffset =
3872         endian::readNext<uint32_t, little, unaligned>(Data);
3873     uint32_t MacroIDOffset =
3874         endian::readNext<uint32_t, little, unaligned>(Data);
3875     uint32_t PreprocessedEntityIDOffset =
3876         endian::readNext<uint32_t, little, unaligned>(Data);
3877     uint32_t SubmoduleIDOffset =
3878         endian::readNext<uint32_t, little, unaligned>(Data);
3879     uint32_t SelectorIDOffset =
3880         endian::readNext<uint32_t, little, unaligned>(Data);
3881     uint32_t DeclIDOffset =
3882         endian::readNext<uint32_t, little, unaligned>(Data);
3883     uint32_t TypeIndexOffset =
3884         endian::readNext<uint32_t, little, unaligned>(Data);
3885 
3886     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3887                          RemapBuilder &Remap) {
3888       constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
3889       if (Offset != None)
3890         Remap.insert(std::make_pair(Offset,
3891                                     static_cast<int>(BaseOffset - Offset)));
3892     };
3893 
3894     constexpr SourceLocation::UIntTy SLocNone =
3895         std::numeric_limits<SourceLocation::UIntTy>::max();
3896     if (SLocOffset != SLocNone)
3897       SLocRemap.insert(std::make_pair(
3898           SLocOffset, static_cast<SourceLocation::IntTy>(
3899                           OM->SLocEntryBaseOffset - SLocOffset)));
3900 
3901     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3902     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3903     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3904               PreprocessedEntityRemap);
3905     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3906     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3907     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3908     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3909 
3910     // Global -> local mappings.
3911     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3912   }
3913 }
3914 
3915 ASTReader::ASTReadResult
3916 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3917                                   const ModuleFile *ImportedBy,
3918                                   unsigned ClientLoadCapabilities) {
3919   unsigned Idx = 0;
3920   F.ModuleMapPath = ReadPath(F, Record, Idx);
3921 
3922   // Try to resolve ModuleName in the current header search context and
3923   // verify that it is found in the same module map file as we saved. If the
3924   // top-level AST file is a main file, skip this check because there is no
3925   // usable header search context.
3926   assert(!F.ModuleName.empty() &&
3927          "MODULE_NAME should come before MODULE_MAP_FILE");
3928   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3929     // An implicitly-loaded module file should have its module listed in some
3930     // module map file that we've already loaded.
3931     Module *M =
3932         PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
3933     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3934     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3935     // Don't emit module relocation error if we have -fno-validate-pch
3936     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3937               DisableValidationForModuleKind::Module) &&
3938         !ModMap) {
3939       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
3940         if (auto ASTFE = M ? M->getASTFile() : None) {
3941           // This module was defined by an imported (explicit) module.
3942           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3943                                                << ASTFE->getName();
3944         } else {
3945           // This module was built with a different module map.
3946           Diag(diag::err_imported_module_not_found)
3947               << F.ModuleName << F.FileName
3948               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3949               << !ImportedBy;
3950           // In case it was imported by a PCH, there's a chance the user is
3951           // just missing to include the search path to the directory containing
3952           // the modulemap.
3953           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3954             Diag(diag::note_imported_by_pch_module_not_found)
3955                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3956         }
3957       }
3958       return OutOfDate;
3959     }
3960 
3961     assert(M && M->Name == F.ModuleName && "found module with different name");
3962 
3963     // Check the primary module map file.
3964     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3965     if (!StoredModMap || *StoredModMap != ModMap) {
3966       assert(ModMap && "found module is missing module map file");
3967       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3968              "top-level import should be verified");
3969       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3970       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3971         Diag(diag::err_imported_module_modmap_changed)
3972             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3973             << ModMap->getName() << F.ModuleMapPath << NotImported;
3974       return OutOfDate;
3975     }
3976 
3977     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3978     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3979       // FIXME: we should use input files rather than storing names.
3980       std::string Filename = ReadPath(F, Record, Idx);
3981       auto SF = FileMgr.getFile(Filename, false, false);
3982       if (!SF) {
3983         if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3984           Error("could not find file '" + Filename +"' referenced by AST file");
3985         return OutOfDate;
3986       }
3987       AdditionalStoredMaps.insert(*SF);
3988     }
3989 
3990     // Check any additional module map files (e.g. module.private.modulemap)
3991     // that are not in the pcm.
3992     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3993       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3994         // Remove files that match
3995         // Note: SmallPtrSet::erase is really remove
3996         if (!AdditionalStoredMaps.erase(ModMap)) {
3997           if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3998             Diag(diag::err_module_different_modmap)
3999               << F.ModuleName << /*new*/0 << ModMap->getName();
4000           return OutOfDate;
4001         }
4002       }
4003     }
4004 
4005     // Check any additional module map files that are in the pcm, but not
4006     // found in header search. Cases that match are already removed.
4007     for (const FileEntry *ModMap : AdditionalStoredMaps) {
4008       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4009         Diag(diag::err_module_different_modmap)
4010           << F.ModuleName << /*not new*/1 << ModMap->getName();
4011       return OutOfDate;
4012     }
4013   }
4014 
4015   if (Listener)
4016     Listener->ReadModuleMapFile(F.ModuleMapPath);
4017   return Success;
4018 }
4019 
4020 /// Move the given method to the back of the global list of methods.
4021 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4022   // Find the entry for this selector in the method pool.
4023   Sema::GlobalMethodPool::iterator Known
4024     = S.MethodPool.find(Method->getSelector());
4025   if (Known == S.MethodPool.end())
4026     return;
4027 
4028   // Retrieve the appropriate method list.
4029   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4030                                                     : Known->second.second;
4031   bool Found = false;
4032   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4033     if (!Found) {
4034       if (List->getMethod() == Method) {
4035         Found = true;
4036       } else {
4037         // Keep searching.
4038         continue;
4039       }
4040     }
4041 
4042     if (List->getNext())
4043       List->setMethod(List->getNext()->getMethod());
4044     else
4045       List->setMethod(Method);
4046   }
4047 }
4048 
4049 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4050   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4051   for (Decl *D : Names) {
4052     bool wasHidden = !D->isUnconditionallyVisible();
4053     D->setVisibleDespiteOwningModule();
4054 
4055     if (wasHidden && SemaObj) {
4056       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4057         moveMethodToBackOfGlobalList(*SemaObj, Method);
4058       }
4059     }
4060   }
4061 }
4062 
4063 void ASTReader::makeModuleVisible(Module *Mod,
4064                                   Module::NameVisibilityKind NameVisibility,
4065                                   SourceLocation ImportLoc) {
4066   llvm::SmallPtrSet<Module *, 4> Visited;
4067   SmallVector<Module *, 4> Stack;
4068   Stack.push_back(Mod);
4069   while (!Stack.empty()) {
4070     Mod = Stack.pop_back_val();
4071 
4072     if (NameVisibility <= Mod->NameVisibility) {
4073       // This module already has this level of visibility (or greater), so
4074       // there is nothing more to do.
4075       continue;
4076     }
4077 
4078     if (Mod->isUnimportable()) {
4079       // Modules that aren't importable cannot be made visible.
4080       continue;
4081     }
4082 
4083     // Update the module's name visibility.
4084     Mod->NameVisibility = NameVisibility;
4085 
4086     // If we've already deserialized any names from this module,
4087     // mark them as visible.
4088     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4089     if (Hidden != HiddenNamesMap.end()) {
4090       auto HiddenNames = std::move(*Hidden);
4091       HiddenNamesMap.erase(Hidden);
4092       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4093       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4094              "making names visible added hidden names");
4095     }
4096 
4097     // Push any exported modules onto the stack to be marked as visible.
4098     SmallVector<Module *, 16> Exports;
4099     Mod->getExportedModules(Exports);
4100     for (SmallVectorImpl<Module *>::iterator
4101            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4102       Module *Exported = *I;
4103       if (Visited.insert(Exported).second)
4104         Stack.push_back(Exported);
4105     }
4106   }
4107 }
4108 
4109 /// We've merged the definition \p MergedDef into the existing definition
4110 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4111 /// visible.
4112 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4113                                           NamedDecl *MergedDef) {
4114   if (!Def->isUnconditionallyVisible()) {
4115     // If MergedDef is visible or becomes visible, make the definition visible.
4116     if (MergedDef->isUnconditionallyVisible())
4117       Def->setVisibleDespiteOwningModule();
4118     else {
4119       getContext().mergeDefinitionIntoModule(
4120           Def, MergedDef->getImportedOwningModule(),
4121           /*NotifyListeners*/ false);
4122       PendingMergedDefinitionsToDeduplicate.insert(Def);
4123     }
4124   }
4125 }
4126 
4127 bool ASTReader::loadGlobalIndex() {
4128   if (GlobalIndex)
4129     return false;
4130 
4131   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4132       !PP.getLangOpts().Modules)
4133     return true;
4134 
4135   // Try to load the global index.
4136   TriedLoadingGlobalIndex = true;
4137   StringRef ModuleCachePath
4138     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4139   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4140       GlobalModuleIndex::readIndex(ModuleCachePath);
4141   if (llvm::Error Err = std::move(Result.second)) {
4142     assert(!Result.first);
4143     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4144     return true;
4145   }
4146 
4147   GlobalIndex.reset(Result.first);
4148   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4149   return false;
4150 }
4151 
4152 bool ASTReader::isGlobalIndexUnavailable() const {
4153   return PP.getLangOpts().Modules && UseGlobalIndex &&
4154          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4155 }
4156 
4157 static void updateModuleTimestamp(ModuleFile &MF) {
4158   // Overwrite the timestamp file contents so that file's mtime changes.
4159   std::string TimestampFilename = MF.getTimestampFilename();
4160   std::error_code EC;
4161   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4162                           llvm::sys::fs::OF_TextWithCRLF);
4163   if (EC)
4164     return;
4165   OS << "Timestamp file\n";
4166   OS.close();
4167   OS.clear_error(); // Avoid triggering a fatal error.
4168 }
4169 
4170 /// Given a cursor at the start of an AST file, scan ahead and drop the
4171 /// cursor into the start of the given block ID, returning false on success and
4172 /// true on failure.
4173 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4174   while (true) {
4175     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4176     if (!MaybeEntry) {
4177       // FIXME this drops errors on the floor.
4178       consumeError(MaybeEntry.takeError());
4179       return true;
4180     }
4181     llvm::BitstreamEntry Entry = MaybeEntry.get();
4182 
4183     switch (Entry.Kind) {
4184     case llvm::BitstreamEntry::Error:
4185     case llvm::BitstreamEntry::EndBlock:
4186       return true;
4187 
4188     case llvm::BitstreamEntry::Record:
4189       // Ignore top-level records.
4190       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4191         break;
4192       else {
4193         // FIXME this drops errors on the floor.
4194         consumeError(Skipped.takeError());
4195         return true;
4196       }
4197 
4198     case llvm::BitstreamEntry::SubBlock:
4199       if (Entry.ID == BlockID) {
4200         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4201           // FIXME this drops the error on the floor.
4202           consumeError(std::move(Err));
4203           return true;
4204         }
4205         // Found it!
4206         return false;
4207       }
4208 
4209       if (llvm::Error Err = Cursor.SkipBlock()) {
4210         // FIXME this drops the error on the floor.
4211         consumeError(std::move(Err));
4212         return true;
4213       }
4214     }
4215   }
4216 }
4217 
4218 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4219                                             ModuleKind Type,
4220                                             SourceLocation ImportLoc,
4221                                             unsigned ClientLoadCapabilities,
4222                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4223   llvm::SaveAndRestore<SourceLocation>
4224     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4225   llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4226       CurrentDeserializingModuleKind, Type);
4227 
4228   // Defer any pending actions until we get to the end of reading the AST file.
4229   Deserializing AnASTFile(this);
4230 
4231   // Bump the generation number.
4232   unsigned PreviousGeneration = 0;
4233   if (ContextObj)
4234     PreviousGeneration = incrementGeneration(*ContextObj);
4235 
4236   unsigned NumModules = ModuleMgr.size();
4237   SmallVector<ImportedModule, 4> Loaded;
4238   if (ASTReadResult ReadResult =
4239           ReadASTCore(FileName, Type, ImportLoc,
4240                       /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4241                       ClientLoadCapabilities)) {
4242     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4243                             PP.getLangOpts().Modules
4244                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4245                                 : nullptr);
4246 
4247     // If we find that any modules are unusable, the global index is going
4248     // to be out-of-date. Just remove it.
4249     GlobalIndex.reset();
4250     ModuleMgr.setGlobalIndex(nullptr);
4251     return ReadResult;
4252   }
4253 
4254   // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4255   // remove modules from this point. Various fields are updated during reading
4256   // the AST block and removing the modules would result in dangling pointers.
4257   // They are generally only incidentally dereferenced, ie. a binary search
4258   // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4259   // be dereferenced but it wouldn't actually be used.
4260 
4261   // Load the AST blocks of all of the modules that we loaded. We can still
4262   // hit errors parsing the ASTs at this point.
4263   for (ImportedModule &M : Loaded) {
4264     ModuleFile &F = *M.Mod;
4265 
4266     // Read the AST block.
4267     if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4268       Error(std::move(Err));
4269       return Failure;
4270     }
4271 
4272     // The AST block should always have a definition for the main module.
4273     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4274       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4275       return Failure;
4276     }
4277 
4278     // Read the extension blocks.
4279     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4280       if (llvm::Error Err = ReadExtensionBlock(F)) {
4281         Error(std::move(Err));
4282         return Failure;
4283       }
4284     }
4285 
4286     // Once read, set the ModuleFile bit base offset and update the size in
4287     // bits of all files we've seen.
4288     F.GlobalBitOffset = TotalModulesSizeInBits;
4289     TotalModulesSizeInBits += F.SizeInBits;
4290     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4291   }
4292 
4293   // Preload source locations and interesting indentifiers.
4294   for (ImportedModule &M : Loaded) {
4295     ModuleFile &F = *M.Mod;
4296 
4297     // Preload SLocEntries.
4298     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4299       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4300       // Load it through the SourceManager and don't call ReadSLocEntry()
4301       // directly because the entry may have already been loaded in which case
4302       // calling ReadSLocEntry() directly would trigger an assertion in
4303       // SourceManager.
4304       SourceMgr.getLoadedSLocEntryByID(Index);
4305     }
4306 
4307     // Map the original source file ID into the ID space of the current
4308     // compilation.
4309     if (F.OriginalSourceFileID.isValid()) {
4310       F.OriginalSourceFileID = FileID::get(
4311           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4312     }
4313 
4314     // Preload all the pending interesting identifiers by marking them out of
4315     // date.
4316     for (auto Offset : F.PreloadIdentifierOffsets) {
4317       const unsigned char *Data = F.IdentifierTableData + Offset;
4318 
4319       ASTIdentifierLookupTrait Trait(*this, F);
4320       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4321       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4322       auto &II = PP.getIdentifierTable().getOwn(Key);
4323       II.setOutOfDate(true);
4324 
4325       // Mark this identifier as being from an AST file so that we can track
4326       // whether we need to serialize it.
4327       markIdentifierFromAST(*this, II);
4328 
4329       // Associate the ID with the identifier so that the writer can reuse it.
4330       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4331       SetIdentifierInfo(ID, &II);
4332     }
4333   }
4334 
4335   // Setup the import locations and notify the module manager that we've
4336   // committed to these module files.
4337   for (ImportedModule &M : Loaded) {
4338     ModuleFile &F = *M.Mod;
4339 
4340     ModuleMgr.moduleFileAccepted(&F);
4341 
4342     // Set the import location.
4343     F.DirectImportLoc = ImportLoc;
4344     // FIXME: We assume that locations from PCH / preamble do not need
4345     // any translation.
4346     if (!M.ImportedBy)
4347       F.ImportLoc = M.ImportLoc;
4348     else
4349       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4350   }
4351 
4352   if (!PP.getLangOpts().CPlusPlus ||
4353       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4354        Type != MK_PrebuiltModule)) {
4355     // Mark all of the identifiers in the identifier table as being out of date,
4356     // so that various accessors know to check the loaded modules when the
4357     // identifier is used.
4358     //
4359     // For C++ modules, we don't need information on many identifiers (just
4360     // those that provide macros or are poisoned), so we mark all of
4361     // the interesting ones via PreloadIdentifierOffsets.
4362     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4363                                 IdEnd = PP.getIdentifierTable().end();
4364          Id != IdEnd; ++Id)
4365       Id->second->setOutOfDate(true);
4366   }
4367   // Mark selectors as out of date.
4368   for (auto Sel : SelectorGeneration)
4369     SelectorOutOfDate[Sel.first] = true;
4370 
4371   // Resolve any unresolved module exports.
4372   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4373     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4374     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4375     Module *ResolvedMod = getSubmodule(GlobalID);
4376 
4377     switch (Unresolved.Kind) {
4378     case UnresolvedModuleRef::Conflict:
4379       if (ResolvedMod) {
4380         Module::Conflict Conflict;
4381         Conflict.Other = ResolvedMod;
4382         Conflict.Message = Unresolved.String.str();
4383         Unresolved.Mod->Conflicts.push_back(Conflict);
4384       }
4385       continue;
4386 
4387     case UnresolvedModuleRef::Import:
4388       if (ResolvedMod)
4389         Unresolved.Mod->Imports.insert(ResolvedMod);
4390       continue;
4391 
4392     case UnresolvedModuleRef::Export:
4393       if (ResolvedMod || Unresolved.IsWildcard)
4394         Unresolved.Mod->Exports.push_back(
4395           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4396       continue;
4397     }
4398   }
4399   UnresolvedModuleRefs.clear();
4400 
4401   if (Imported)
4402     Imported->append(ImportedModules.begin(),
4403                      ImportedModules.end());
4404 
4405   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4406   // Might be unnecessary as use declarations are only used to build the
4407   // module itself.
4408 
4409   if (ContextObj)
4410     InitializeContext();
4411 
4412   if (SemaObj)
4413     UpdateSema();
4414 
4415   if (DeserializationListener)
4416     DeserializationListener->ReaderInitialized(this);
4417 
4418   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4419   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4420     // If this AST file is a precompiled preamble, then set the
4421     // preamble file ID of the source manager to the file source file
4422     // from which the preamble was built.
4423     if (Type == MK_Preamble) {
4424       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4425     } else if (Type == MK_MainFile) {
4426       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4427     }
4428   }
4429 
4430   // For any Objective-C class definitions we have already loaded, make sure
4431   // that we load any additional categories.
4432   if (ContextObj) {
4433     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4434       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4435                          ObjCClassesLoaded[I],
4436                          PreviousGeneration);
4437     }
4438   }
4439 
4440   if (PP.getHeaderSearchInfo()
4441           .getHeaderSearchOpts()
4442           .ModulesValidateOncePerBuildSession) {
4443     // Now we are certain that the module and all modules it depends on are
4444     // up to date.  Create or update timestamp files for modules that are
4445     // located in the module cache (not for PCH files that could be anywhere
4446     // in the filesystem).
4447     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4448       ImportedModule &M = Loaded[I];
4449       if (M.Mod->Kind == MK_ImplicitModule) {
4450         updateModuleTimestamp(*M.Mod);
4451       }
4452     }
4453   }
4454 
4455   return Success;
4456 }
4457 
4458 static ASTFileSignature readASTFileSignature(StringRef PCH);
4459 
4460 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4461 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4462   // FIXME checking magic headers is done in other places such as
4463   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4464   // always done the same. Unify it all with a helper.
4465   if (!Stream.canSkipToPos(4))
4466     return llvm::createStringError(std::errc::illegal_byte_sequence,
4467                                    "file too small to contain AST file magic");
4468   for (unsigned C : {'C', 'P', 'C', 'H'})
4469     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4470       if (Res.get() != C)
4471         return llvm::createStringError(
4472             std::errc::illegal_byte_sequence,
4473             "file doesn't start with AST file magic");
4474     } else
4475       return Res.takeError();
4476   return llvm::Error::success();
4477 }
4478 
4479 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4480   switch (Kind) {
4481   case MK_PCH:
4482     return 0; // PCH
4483   case MK_ImplicitModule:
4484   case MK_ExplicitModule:
4485   case MK_PrebuiltModule:
4486     return 1; // module
4487   case MK_MainFile:
4488   case MK_Preamble:
4489     return 2; // main source file
4490   }
4491   llvm_unreachable("unknown module kind");
4492 }
4493 
4494 ASTReader::ASTReadResult
4495 ASTReader::ReadASTCore(StringRef FileName,
4496                        ModuleKind Type,
4497                        SourceLocation ImportLoc,
4498                        ModuleFile *ImportedBy,
4499                        SmallVectorImpl<ImportedModule> &Loaded,
4500                        off_t ExpectedSize, time_t ExpectedModTime,
4501                        ASTFileSignature ExpectedSignature,
4502                        unsigned ClientLoadCapabilities) {
4503   ModuleFile *M;
4504   std::string ErrorStr;
4505   ModuleManager::AddModuleResult AddResult
4506     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4507                           getGeneration(), ExpectedSize, ExpectedModTime,
4508                           ExpectedSignature, readASTFileSignature,
4509                           M, ErrorStr);
4510 
4511   switch (AddResult) {
4512   case ModuleManager::AlreadyLoaded:
4513     Diag(diag::remark_module_import)
4514         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4515         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4516     return Success;
4517 
4518   case ModuleManager::NewlyLoaded:
4519     // Load module file below.
4520     break;
4521 
4522   case ModuleManager::Missing:
4523     // The module file was missing; if the client can handle that, return
4524     // it.
4525     if (ClientLoadCapabilities & ARR_Missing)
4526       return Missing;
4527 
4528     // Otherwise, return an error.
4529     Diag(diag::err_ast_file_not_found)
4530         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4531         << ErrorStr;
4532     return Failure;
4533 
4534   case ModuleManager::OutOfDate:
4535     // We couldn't load the module file because it is out-of-date. If the
4536     // client can handle out-of-date, return it.
4537     if (ClientLoadCapabilities & ARR_OutOfDate)
4538       return OutOfDate;
4539 
4540     // Otherwise, return an error.
4541     Diag(diag::err_ast_file_out_of_date)
4542         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4543         << ErrorStr;
4544     return Failure;
4545   }
4546 
4547   assert(M && "Missing module file");
4548 
4549   bool ShouldFinalizePCM = false;
4550   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4551     auto &MC = getModuleManager().getModuleCache();
4552     if (ShouldFinalizePCM)
4553       MC.finalizePCM(FileName);
4554     else
4555       MC.tryToDropPCM(FileName);
4556   });
4557   ModuleFile &F = *M;
4558   BitstreamCursor &Stream = F.Stream;
4559   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4560   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4561 
4562   // Sniff for the signature.
4563   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4564     Diag(diag::err_ast_file_invalid)
4565         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4566     return Failure;
4567   }
4568 
4569   // This is used for compatibility with older PCH formats.
4570   bool HaveReadControlBlock = false;
4571   while (true) {
4572     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4573     if (!MaybeEntry) {
4574       Error(MaybeEntry.takeError());
4575       return Failure;
4576     }
4577     llvm::BitstreamEntry Entry = MaybeEntry.get();
4578 
4579     switch (Entry.Kind) {
4580     case llvm::BitstreamEntry::Error:
4581     case llvm::BitstreamEntry::Record:
4582     case llvm::BitstreamEntry::EndBlock:
4583       Error("invalid record at top-level of AST file");
4584       return Failure;
4585 
4586     case llvm::BitstreamEntry::SubBlock:
4587       break;
4588     }
4589 
4590     switch (Entry.ID) {
4591     case CONTROL_BLOCK_ID:
4592       HaveReadControlBlock = true;
4593       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4594       case Success:
4595         // Check that we didn't try to load a non-module AST file as a module.
4596         //
4597         // FIXME: Should we also perform the converse check? Loading a module as
4598         // a PCH file sort of works, but it's a bit wonky.
4599         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4600              Type == MK_PrebuiltModule) &&
4601             F.ModuleName.empty()) {
4602           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4603           if (Result != OutOfDate ||
4604               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4605             Diag(diag::err_module_file_not_module) << FileName;
4606           return Result;
4607         }
4608         break;
4609 
4610       case Failure: return Failure;
4611       case Missing: return Missing;
4612       case OutOfDate: return OutOfDate;
4613       case VersionMismatch: return VersionMismatch;
4614       case ConfigurationMismatch: return ConfigurationMismatch;
4615       case HadErrors: return HadErrors;
4616       }
4617       break;
4618 
4619     case AST_BLOCK_ID:
4620       if (!HaveReadControlBlock) {
4621         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4622           Diag(diag::err_pch_version_too_old);
4623         return VersionMismatch;
4624       }
4625 
4626       // Record that we've loaded this module.
4627       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4628       ShouldFinalizePCM = true;
4629       return Success;
4630 
4631     case UNHASHED_CONTROL_BLOCK_ID:
4632       // This block is handled using look-ahead during ReadControlBlock.  We
4633       // shouldn't get here!
4634       Error("malformed block record in AST file");
4635       return Failure;
4636 
4637     default:
4638       if (llvm::Error Err = Stream.SkipBlock()) {
4639         Error(std::move(Err));
4640         return Failure;
4641       }
4642       break;
4643     }
4644   }
4645 
4646   llvm_unreachable("unexpected break; expected return");
4647 }
4648 
4649 ASTReader::ASTReadResult
4650 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4651                                     unsigned ClientLoadCapabilities) {
4652   const HeaderSearchOptions &HSOpts =
4653       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4654   bool AllowCompatibleConfigurationMismatch =
4655       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4656   bool DisableValidation = shouldDisableValidationForFile(F);
4657 
4658   ASTReadResult Result = readUnhashedControlBlockImpl(
4659       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4660       Listener.get(),
4661       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4662 
4663   // If F was directly imported by another module, it's implicitly validated by
4664   // the importing module.
4665   if (DisableValidation || WasImportedBy ||
4666       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4667     return Success;
4668 
4669   if (Result == Failure) {
4670     Error("malformed block record in AST file");
4671     return Failure;
4672   }
4673 
4674   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4675     // If this module has already been finalized in the ModuleCache, we're stuck
4676     // with it; we can only load a single version of each module.
4677     //
4678     // This can happen when a module is imported in two contexts: in one, as a
4679     // user module; in another, as a system module (due to an import from
4680     // another module marked with the [system] flag).  It usually indicates a
4681     // bug in the module map: this module should also be marked with [system].
4682     //
4683     // If -Wno-system-headers (the default), and the first import is as a
4684     // system module, then validation will fail during the as-user import,
4685     // since -Werror flags won't have been validated.  However, it's reasonable
4686     // to treat this consistently as a system module.
4687     //
4688     // If -Wsystem-headers, the PCM on disk was built with
4689     // -Wno-system-headers, and the first import is as a user module, then
4690     // validation will fail during the as-system import since the PCM on disk
4691     // doesn't guarantee that -Werror was respected.  However, the -Werror
4692     // flags were checked during the initial as-user import.
4693     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4694       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4695       return Success;
4696     }
4697   }
4698 
4699   return Result;
4700 }
4701 
4702 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4703     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4704     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4705     bool ValidateDiagnosticOptions) {
4706   // Initialize a stream.
4707   BitstreamCursor Stream(StreamData);
4708 
4709   // Sniff for the signature.
4710   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4711     // FIXME this drops the error on the floor.
4712     consumeError(std::move(Err));
4713     return Failure;
4714   }
4715 
4716   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4717   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4718     return Failure;
4719 
4720   // Read all of the records in the options block.
4721   RecordData Record;
4722   ASTReadResult Result = Success;
4723   while (true) {
4724     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4725     if (!MaybeEntry) {
4726       // FIXME this drops the error on the floor.
4727       consumeError(MaybeEntry.takeError());
4728       return Failure;
4729     }
4730     llvm::BitstreamEntry Entry = MaybeEntry.get();
4731 
4732     switch (Entry.Kind) {
4733     case llvm::BitstreamEntry::Error:
4734     case llvm::BitstreamEntry::SubBlock:
4735       return Failure;
4736 
4737     case llvm::BitstreamEntry::EndBlock:
4738       return Result;
4739 
4740     case llvm::BitstreamEntry::Record:
4741       // The interesting case.
4742       break;
4743     }
4744 
4745     // Read and process a record.
4746     Record.clear();
4747     StringRef Blob;
4748     Expected<unsigned> MaybeRecordType =
4749         Stream.readRecord(Entry.ID, Record, &Blob);
4750     if (!MaybeRecordType) {
4751       // FIXME this drops the error.
4752       return Failure;
4753     }
4754     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4755     case SIGNATURE:
4756       if (F)
4757         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4758       break;
4759     case AST_BLOCK_HASH:
4760       if (F)
4761         F->ASTBlockHash =
4762             ASTFileSignature::create(Record.begin(), Record.end());
4763       break;
4764     case DIAGNOSTIC_OPTIONS: {
4765       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4766       if (Listener && ValidateDiagnosticOptions &&
4767           !AllowCompatibleConfigurationMismatch &&
4768           ParseDiagnosticOptions(Record, Complain, *Listener))
4769         Result = OutOfDate; // Don't return early.  Read the signature.
4770       break;
4771     }
4772     case DIAG_PRAGMA_MAPPINGS:
4773       if (!F)
4774         break;
4775       if (F->PragmaDiagMappings.empty())
4776         F->PragmaDiagMappings.swap(Record);
4777       else
4778         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4779                                      Record.begin(), Record.end());
4780       break;
4781     case HEADER_SEARCH_ENTRY_USAGE:
4782       if (!F)
4783         break;
4784       unsigned Count = Record[0];
4785       const char *Byte = Blob.data();
4786       F->SearchPathUsage = llvm::BitVector(Count, false);
4787       for (unsigned I = 0; I < Count; ++Byte)
4788         for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
4789           if (*Byte & (1 << Bit))
4790             F->SearchPathUsage[I] = true;
4791       break;
4792     }
4793   }
4794 }
4795 
4796 /// Parse a record and blob containing module file extension metadata.
4797 static bool parseModuleFileExtensionMetadata(
4798               const SmallVectorImpl<uint64_t> &Record,
4799               StringRef Blob,
4800               ModuleFileExtensionMetadata &Metadata) {
4801   if (Record.size() < 4) return true;
4802 
4803   Metadata.MajorVersion = Record[0];
4804   Metadata.MinorVersion = Record[1];
4805 
4806   unsigned BlockNameLen = Record[2];
4807   unsigned UserInfoLen = Record[3];
4808 
4809   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4810 
4811   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4812   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4813                                   Blob.data() + BlockNameLen + UserInfoLen);
4814   return false;
4815 }
4816 
4817 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
4818   BitstreamCursor &Stream = F.Stream;
4819 
4820   RecordData Record;
4821   while (true) {
4822     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4823     if (!MaybeEntry)
4824       return MaybeEntry.takeError();
4825     llvm::BitstreamEntry Entry = MaybeEntry.get();
4826 
4827     switch (Entry.Kind) {
4828     case llvm::BitstreamEntry::SubBlock:
4829       if (llvm::Error Err = Stream.SkipBlock())
4830         return Err;
4831       continue;
4832     case llvm::BitstreamEntry::EndBlock:
4833       return llvm::Error::success();
4834     case llvm::BitstreamEntry::Error:
4835       return llvm::createStringError(std::errc::illegal_byte_sequence,
4836                                      "malformed block record in AST file");
4837     case llvm::BitstreamEntry::Record:
4838       break;
4839     }
4840 
4841     Record.clear();
4842     StringRef Blob;
4843     Expected<unsigned> MaybeRecCode =
4844         Stream.readRecord(Entry.ID, Record, &Blob);
4845     if (!MaybeRecCode)
4846       return MaybeRecCode.takeError();
4847     switch (MaybeRecCode.get()) {
4848     case EXTENSION_METADATA: {
4849       ModuleFileExtensionMetadata Metadata;
4850       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4851         return llvm::createStringError(
4852             std::errc::illegal_byte_sequence,
4853             "malformed EXTENSION_METADATA in AST file");
4854 
4855       // Find a module file extension with this block name.
4856       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4857       if (Known == ModuleFileExtensions.end()) break;
4858 
4859       // Form a reader.
4860       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4861                                                              F, Stream)) {
4862         F.ExtensionReaders.push_back(std::move(Reader));
4863       }
4864 
4865       break;
4866     }
4867     }
4868   }
4869 
4870   return llvm::Error::success();
4871 }
4872 
4873 void ASTReader::InitializeContext() {
4874   assert(ContextObj && "no context to initialize");
4875   ASTContext &Context = *ContextObj;
4876 
4877   // If there's a listener, notify them that we "read" the translation unit.
4878   if (DeserializationListener)
4879     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4880                                       Context.getTranslationUnitDecl());
4881 
4882   // FIXME: Find a better way to deal with collisions between these
4883   // built-in types. Right now, we just ignore the problem.
4884 
4885   // Load the special types.
4886   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4887     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4888       if (!Context.CFConstantStringTypeDecl)
4889         Context.setCFConstantStringType(GetType(String));
4890     }
4891 
4892     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4893       QualType FileType = GetType(File);
4894       if (FileType.isNull()) {
4895         Error("FILE type is NULL");
4896         return;
4897       }
4898 
4899       if (!Context.FILEDecl) {
4900         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4901           Context.setFILEDecl(Typedef->getDecl());
4902         else {
4903           const TagType *Tag = FileType->getAs<TagType>();
4904           if (!Tag) {
4905             Error("Invalid FILE type in AST file");
4906             return;
4907           }
4908           Context.setFILEDecl(Tag->getDecl());
4909         }
4910       }
4911     }
4912 
4913     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4914       QualType Jmp_bufType = GetType(Jmp_buf);
4915       if (Jmp_bufType.isNull()) {
4916         Error("jmp_buf type is NULL");
4917         return;
4918       }
4919 
4920       if (!Context.jmp_bufDecl) {
4921         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4922           Context.setjmp_bufDecl(Typedef->getDecl());
4923         else {
4924           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4925           if (!Tag) {
4926             Error("Invalid jmp_buf type in AST file");
4927             return;
4928           }
4929           Context.setjmp_bufDecl(Tag->getDecl());
4930         }
4931       }
4932     }
4933 
4934     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4935       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4936       if (Sigjmp_bufType.isNull()) {
4937         Error("sigjmp_buf type is NULL");
4938         return;
4939       }
4940 
4941       if (!Context.sigjmp_bufDecl) {
4942         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4943           Context.setsigjmp_bufDecl(Typedef->getDecl());
4944         else {
4945           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4946           assert(Tag && "Invalid sigjmp_buf type in AST file");
4947           Context.setsigjmp_bufDecl(Tag->getDecl());
4948         }
4949       }
4950     }
4951 
4952     if (unsigned ObjCIdRedef
4953           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4954       if (Context.ObjCIdRedefinitionType.isNull())
4955         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4956     }
4957 
4958     if (unsigned ObjCClassRedef
4959           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4960       if (Context.ObjCClassRedefinitionType.isNull())
4961         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4962     }
4963 
4964     if (unsigned ObjCSelRedef
4965           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4966       if (Context.ObjCSelRedefinitionType.isNull())
4967         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4968     }
4969 
4970     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4971       QualType Ucontext_tType = GetType(Ucontext_t);
4972       if (Ucontext_tType.isNull()) {
4973         Error("ucontext_t type is NULL");
4974         return;
4975       }
4976 
4977       if (!Context.ucontext_tDecl) {
4978         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4979           Context.setucontext_tDecl(Typedef->getDecl());
4980         else {
4981           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4982           assert(Tag && "Invalid ucontext_t type in AST file");
4983           Context.setucontext_tDecl(Tag->getDecl());
4984         }
4985       }
4986     }
4987   }
4988 
4989   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4990 
4991   // If there were any CUDA special declarations, deserialize them.
4992   if (!CUDASpecialDeclRefs.empty()) {
4993     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4994     Context.setcudaConfigureCallDecl(
4995                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4996   }
4997 
4998   // Re-export any modules that were imported by a non-module AST file.
4999   // FIXME: This does not make macro-only imports visible again.
5000   for (auto &Import : ImportedModules) {
5001     if (Module *Imported = getSubmodule(Import.ID)) {
5002       makeModuleVisible(Imported, Module::AllVisible,
5003                         /*ImportLoc=*/Import.ImportLoc);
5004       if (Import.ImportLoc.isValid())
5005         PP.makeModuleVisible(Imported, Import.ImportLoc);
5006       // This updates visibility for Preprocessor only. For Sema, which can be
5007       // nullptr here, we do the same later, in UpdateSema().
5008     }
5009   }
5010 }
5011 
5012 void ASTReader::finalizeForWriting() {
5013   // Nothing to do for now.
5014 }
5015 
5016 /// Reads and return the signature record from \p PCH's control block, or
5017 /// else returns 0.
5018 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5019   BitstreamCursor Stream(PCH);
5020   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5021     // FIXME this drops the error on the floor.
5022     consumeError(std::move(Err));
5023     return ASTFileSignature();
5024   }
5025 
5026   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5027   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5028     return ASTFileSignature();
5029 
5030   // Scan for SIGNATURE inside the diagnostic options block.
5031   ASTReader::RecordData Record;
5032   while (true) {
5033     Expected<llvm::BitstreamEntry> MaybeEntry =
5034         Stream.advanceSkippingSubblocks();
5035     if (!MaybeEntry) {
5036       // FIXME this drops the error on the floor.
5037       consumeError(MaybeEntry.takeError());
5038       return ASTFileSignature();
5039     }
5040     llvm::BitstreamEntry Entry = MaybeEntry.get();
5041 
5042     if (Entry.Kind != llvm::BitstreamEntry::Record)
5043       return ASTFileSignature();
5044 
5045     Record.clear();
5046     StringRef Blob;
5047     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5048     if (!MaybeRecord) {
5049       // FIXME this drops the error on the floor.
5050       consumeError(MaybeRecord.takeError());
5051       return ASTFileSignature();
5052     }
5053     if (SIGNATURE == MaybeRecord.get())
5054       return ASTFileSignature::create(Record.begin(),
5055                                       Record.begin() + ASTFileSignature::size);
5056   }
5057 }
5058 
5059 /// Retrieve the name of the original source file name
5060 /// directly from the AST file, without actually loading the AST
5061 /// file.
5062 std::string ASTReader::getOriginalSourceFile(
5063     const std::string &ASTFileName, FileManager &FileMgr,
5064     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5065   // Open the AST file.
5066   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5067   if (!Buffer) {
5068     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5069         << ASTFileName << Buffer.getError().message();
5070     return std::string();
5071   }
5072 
5073   // Initialize the stream
5074   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5075 
5076   // Sniff for the signature.
5077   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5078     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5079     return std::string();
5080   }
5081 
5082   // Scan for the CONTROL_BLOCK_ID block.
5083   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5084     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5085     return std::string();
5086   }
5087 
5088   // Scan for ORIGINAL_FILE inside the control block.
5089   RecordData Record;
5090   while (true) {
5091     Expected<llvm::BitstreamEntry> MaybeEntry =
5092         Stream.advanceSkippingSubblocks();
5093     if (!MaybeEntry) {
5094       // FIXME this drops errors on the floor.
5095       consumeError(MaybeEntry.takeError());
5096       return std::string();
5097     }
5098     llvm::BitstreamEntry Entry = MaybeEntry.get();
5099 
5100     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5101       return std::string();
5102 
5103     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5104       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5105       return std::string();
5106     }
5107 
5108     Record.clear();
5109     StringRef Blob;
5110     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5111     if (!MaybeRecord) {
5112       // FIXME this drops the errors on the floor.
5113       consumeError(MaybeRecord.takeError());
5114       return std::string();
5115     }
5116     if (ORIGINAL_FILE == MaybeRecord.get())
5117       return Blob.str();
5118   }
5119 }
5120 
5121 namespace {
5122 
5123   class SimplePCHValidator : public ASTReaderListener {
5124     const LangOptions &ExistingLangOpts;
5125     const TargetOptions &ExistingTargetOpts;
5126     const PreprocessorOptions &ExistingPPOpts;
5127     std::string ExistingModuleCachePath;
5128     FileManager &FileMgr;
5129 
5130   public:
5131     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5132                        const TargetOptions &ExistingTargetOpts,
5133                        const PreprocessorOptions &ExistingPPOpts,
5134                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5135         : ExistingLangOpts(ExistingLangOpts),
5136           ExistingTargetOpts(ExistingTargetOpts),
5137           ExistingPPOpts(ExistingPPOpts),
5138           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5139 
5140     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5141                              bool AllowCompatibleDifferences) override {
5142       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5143                                   AllowCompatibleDifferences);
5144     }
5145 
5146     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5147                            bool AllowCompatibleDifferences) override {
5148       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5149                                 AllowCompatibleDifferences);
5150     }
5151 
5152     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5153                                  StringRef SpecificModuleCachePath,
5154                                  bool Complain) override {
5155       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5156                                       ExistingModuleCachePath, nullptr,
5157                                       ExistingLangOpts, ExistingPPOpts);
5158     }
5159 
5160     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5161                                  bool Complain,
5162                                  std::string &SuggestedPredefines) override {
5163       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5164                                       SuggestedPredefines, ExistingLangOpts);
5165     }
5166   };
5167 
5168 } // namespace
5169 
5170 bool ASTReader::readASTFileControlBlock(
5171     StringRef Filename, FileManager &FileMgr,
5172     const PCHContainerReader &PCHContainerRdr,
5173     bool FindModuleFileExtensions,
5174     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5175   // Open the AST file.
5176   // FIXME: This allows use of the VFS; we do not allow use of the
5177   // VFS when actually loading a module.
5178   auto Buffer = FileMgr.getBufferForFile(Filename);
5179   if (!Buffer) {
5180     return true;
5181   }
5182 
5183   // Initialize the stream
5184   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5185   BitstreamCursor Stream(Bytes);
5186 
5187   // Sniff for the signature.
5188   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5189     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5190     return true;
5191   }
5192 
5193   // Scan for the CONTROL_BLOCK_ID block.
5194   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5195     return true;
5196 
5197   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5198   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5199   bool NeedsImports = Listener.needsImportVisitation();
5200   BitstreamCursor InputFilesCursor;
5201 
5202   RecordData Record;
5203   std::string ModuleDir;
5204   bool DoneWithControlBlock = false;
5205   while (!DoneWithControlBlock) {
5206     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5207     if (!MaybeEntry) {
5208       // FIXME this drops the error on the floor.
5209       consumeError(MaybeEntry.takeError());
5210       return true;
5211     }
5212     llvm::BitstreamEntry Entry = MaybeEntry.get();
5213 
5214     switch (Entry.Kind) {
5215     case llvm::BitstreamEntry::SubBlock: {
5216       switch (Entry.ID) {
5217       case OPTIONS_BLOCK_ID: {
5218         std::string IgnoredSuggestedPredefines;
5219         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5220                              /*AllowCompatibleConfigurationMismatch*/ false,
5221                              Listener, IgnoredSuggestedPredefines) != Success)
5222           return true;
5223         break;
5224       }
5225 
5226       case INPUT_FILES_BLOCK_ID:
5227         InputFilesCursor = Stream;
5228         if (llvm::Error Err = Stream.SkipBlock()) {
5229           // FIXME this drops the error on the floor.
5230           consumeError(std::move(Err));
5231           return true;
5232         }
5233         if (NeedsInputFiles &&
5234             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5235           return true;
5236         break;
5237 
5238       default:
5239         if (llvm::Error Err = Stream.SkipBlock()) {
5240           // FIXME this drops the error on the floor.
5241           consumeError(std::move(Err));
5242           return true;
5243         }
5244         break;
5245       }
5246 
5247       continue;
5248     }
5249 
5250     case llvm::BitstreamEntry::EndBlock:
5251       DoneWithControlBlock = true;
5252       break;
5253 
5254     case llvm::BitstreamEntry::Error:
5255       return true;
5256 
5257     case llvm::BitstreamEntry::Record:
5258       break;
5259     }
5260 
5261     if (DoneWithControlBlock) break;
5262 
5263     Record.clear();
5264     StringRef Blob;
5265     Expected<unsigned> MaybeRecCode =
5266         Stream.readRecord(Entry.ID, Record, &Blob);
5267     if (!MaybeRecCode) {
5268       // FIXME this drops the error.
5269       return Failure;
5270     }
5271     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5272     case METADATA:
5273       if (Record[0] != VERSION_MAJOR)
5274         return true;
5275       if (Listener.ReadFullVersionInformation(Blob))
5276         return true;
5277       break;
5278     case MODULE_NAME:
5279       Listener.ReadModuleName(Blob);
5280       break;
5281     case MODULE_DIRECTORY:
5282       ModuleDir = std::string(Blob);
5283       break;
5284     case MODULE_MAP_FILE: {
5285       unsigned Idx = 0;
5286       auto Path = ReadString(Record, Idx);
5287       ResolveImportedPath(Path, ModuleDir);
5288       Listener.ReadModuleMapFile(Path);
5289       break;
5290     }
5291     case INPUT_FILE_OFFSETS: {
5292       if (!NeedsInputFiles)
5293         break;
5294 
5295       unsigned NumInputFiles = Record[0];
5296       unsigned NumUserFiles = Record[1];
5297       const llvm::support::unaligned_uint64_t *InputFileOffs =
5298           (const llvm::support::unaligned_uint64_t *)Blob.data();
5299       for (unsigned I = 0; I != NumInputFiles; ++I) {
5300         // Go find this input file.
5301         bool isSystemFile = I >= NumUserFiles;
5302 
5303         if (isSystemFile && !NeedsSystemInputFiles)
5304           break; // the rest are system input files
5305 
5306         BitstreamCursor &Cursor = InputFilesCursor;
5307         SavedStreamPosition SavedPosition(Cursor);
5308         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5309           // FIXME this drops errors on the floor.
5310           consumeError(std::move(Err));
5311         }
5312 
5313         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5314         if (!MaybeCode) {
5315           // FIXME this drops errors on the floor.
5316           consumeError(MaybeCode.takeError());
5317         }
5318         unsigned Code = MaybeCode.get();
5319 
5320         RecordData Record;
5321         StringRef Blob;
5322         bool shouldContinue = false;
5323         Expected<unsigned> MaybeRecordType =
5324             Cursor.readRecord(Code, Record, &Blob);
5325         if (!MaybeRecordType) {
5326           // FIXME this drops errors on the floor.
5327           consumeError(MaybeRecordType.takeError());
5328         }
5329         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5330         case INPUT_FILE_HASH:
5331           break;
5332         case INPUT_FILE:
5333           bool Overridden = static_cast<bool>(Record[3]);
5334           std::string Filename = std::string(Blob);
5335           ResolveImportedPath(Filename, ModuleDir);
5336           shouldContinue = Listener.visitInputFile(
5337               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5338           break;
5339         }
5340         if (!shouldContinue)
5341           break;
5342       }
5343       break;
5344     }
5345 
5346     case IMPORTS: {
5347       if (!NeedsImports)
5348         break;
5349 
5350       unsigned Idx = 0, N = Record.size();
5351       while (Idx < N) {
5352         // Read information about the AST file.
5353         Idx +=
5354             1 + 1 + 1 + 1 +
5355             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5356         std::string ModuleName = ReadString(Record, Idx);
5357         std::string Filename = ReadString(Record, Idx);
5358         ResolveImportedPath(Filename, ModuleDir);
5359         Listener.visitImport(ModuleName, Filename);
5360       }
5361       break;
5362     }
5363 
5364     default:
5365       // No other validation to perform.
5366       break;
5367     }
5368   }
5369 
5370   // Look for module file extension blocks, if requested.
5371   if (FindModuleFileExtensions) {
5372     BitstreamCursor SavedStream = Stream;
5373     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5374       bool DoneWithExtensionBlock = false;
5375       while (!DoneWithExtensionBlock) {
5376         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5377         if (!MaybeEntry) {
5378           // FIXME this drops the error.
5379           return true;
5380         }
5381         llvm::BitstreamEntry Entry = MaybeEntry.get();
5382 
5383         switch (Entry.Kind) {
5384         case llvm::BitstreamEntry::SubBlock:
5385           if (llvm::Error Err = Stream.SkipBlock()) {
5386             // FIXME this drops the error on the floor.
5387             consumeError(std::move(Err));
5388             return true;
5389           }
5390           continue;
5391 
5392         case llvm::BitstreamEntry::EndBlock:
5393           DoneWithExtensionBlock = true;
5394           continue;
5395 
5396         case llvm::BitstreamEntry::Error:
5397           return true;
5398 
5399         case llvm::BitstreamEntry::Record:
5400           break;
5401         }
5402 
5403        Record.clear();
5404        StringRef Blob;
5405        Expected<unsigned> MaybeRecCode =
5406            Stream.readRecord(Entry.ID, Record, &Blob);
5407        if (!MaybeRecCode) {
5408          // FIXME this drops the error.
5409          return true;
5410        }
5411        switch (MaybeRecCode.get()) {
5412        case EXTENSION_METADATA: {
5413          ModuleFileExtensionMetadata Metadata;
5414          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5415            return true;
5416 
5417          Listener.readModuleFileExtension(Metadata);
5418          break;
5419        }
5420        }
5421       }
5422     }
5423     Stream = SavedStream;
5424   }
5425 
5426   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5427   if (readUnhashedControlBlockImpl(
5428           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5429           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5430           ValidateDiagnosticOptions) != Success)
5431     return true;
5432 
5433   return false;
5434 }
5435 
5436 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5437                                     const PCHContainerReader &PCHContainerRdr,
5438                                     const LangOptions &LangOpts,
5439                                     const TargetOptions &TargetOpts,
5440                                     const PreprocessorOptions &PPOpts,
5441                                     StringRef ExistingModuleCachePath) {
5442   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5443                                ExistingModuleCachePath, FileMgr);
5444   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5445                                   /*FindModuleFileExtensions=*/false,
5446                                   validator,
5447                                   /*ValidateDiagnosticOptions=*/true);
5448 }
5449 
5450 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5451                                           unsigned ClientLoadCapabilities) {
5452   // Enter the submodule block.
5453   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))
5454     return Err;
5455 
5456   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5457   bool First = true;
5458   Module *CurrentModule = nullptr;
5459   RecordData Record;
5460   while (true) {
5461     Expected<llvm::BitstreamEntry> MaybeEntry =
5462         F.Stream.advanceSkippingSubblocks();
5463     if (!MaybeEntry)
5464       return MaybeEntry.takeError();
5465     llvm::BitstreamEntry Entry = MaybeEntry.get();
5466 
5467     switch (Entry.Kind) {
5468     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5469     case llvm::BitstreamEntry::Error:
5470       return llvm::createStringError(std::errc::illegal_byte_sequence,
5471                                      "malformed block record in AST file");
5472     case llvm::BitstreamEntry::EndBlock:
5473       return llvm::Error::success();
5474     case llvm::BitstreamEntry::Record:
5475       // The interesting case.
5476       break;
5477     }
5478 
5479     // Read a record.
5480     StringRef Blob;
5481     Record.clear();
5482     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5483     if (!MaybeKind)
5484       return MaybeKind.takeError();
5485     unsigned Kind = MaybeKind.get();
5486 
5487     if ((Kind == SUBMODULE_METADATA) != First)
5488       return llvm::createStringError(
5489           std::errc::illegal_byte_sequence,
5490           "submodule metadata record should be at beginning of block");
5491     First = false;
5492 
5493     // Submodule information is only valid if we have a current module.
5494     // FIXME: Should we error on these cases?
5495     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5496         Kind != SUBMODULE_DEFINITION)
5497       continue;
5498 
5499     switch (Kind) {
5500     default:  // Default behavior: ignore.
5501       break;
5502 
5503     case SUBMODULE_DEFINITION: {
5504       if (Record.size() < 12)
5505         return llvm::createStringError(std::errc::illegal_byte_sequence,
5506                                        "malformed module definition");
5507 
5508       StringRef Name = Blob;
5509       unsigned Idx = 0;
5510       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5511       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5512       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5513       bool IsFramework = Record[Idx++];
5514       bool IsExplicit = Record[Idx++];
5515       bool IsSystem = Record[Idx++];
5516       bool IsExternC = Record[Idx++];
5517       bool InferSubmodules = Record[Idx++];
5518       bool InferExplicitSubmodules = Record[Idx++];
5519       bool InferExportWildcard = Record[Idx++];
5520       bool ConfigMacrosExhaustive = Record[Idx++];
5521       bool ModuleMapIsPrivate = Record[Idx++];
5522 
5523       Module *ParentModule = nullptr;
5524       if (Parent)
5525         ParentModule = getSubmodule(Parent);
5526 
5527       // Retrieve this (sub)module from the module map, creating it if
5528       // necessary.
5529       CurrentModule =
5530           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5531               .first;
5532 
5533       // FIXME: set the definition loc for CurrentModule, or call
5534       // ModMap.setInferredModuleAllowedBy()
5535 
5536       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5537       if (GlobalIndex >= SubmodulesLoaded.size() ||
5538           SubmodulesLoaded[GlobalIndex])
5539         return llvm::createStringError(std::errc::invalid_argument,
5540                                        "too many submodules");
5541 
5542       if (!ParentModule) {
5543         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5544           // Don't emit module relocation error if we have -fno-validate-pch
5545           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5546                     DisableValidationForModuleKind::Module) &&
5547               CurFile != F.File) {
5548             auto ConflictError =
5549                 PartialDiagnostic(diag::err_module_file_conflict,
5550                                   ContextObj->DiagAllocator)
5551                 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5552                 << F.File->getName();
5553             return DiagnosticError::create(CurrentImportLoc, ConflictError);
5554           }
5555         }
5556 
5557         F.DidReadTopLevelSubmodule = true;
5558         CurrentModule->setASTFile(F.File);
5559         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5560       }
5561 
5562       CurrentModule->Kind = Kind;
5563       CurrentModule->Signature = F.Signature;
5564       CurrentModule->IsFromModuleFile = true;
5565       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5566       CurrentModule->IsExternC = IsExternC;
5567       CurrentModule->InferSubmodules = InferSubmodules;
5568       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5569       CurrentModule->InferExportWildcard = InferExportWildcard;
5570       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5571       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5572       if (DeserializationListener)
5573         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5574 
5575       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5576 
5577       // Clear out data that will be replaced by what is in the module file.
5578       CurrentModule->LinkLibraries.clear();
5579       CurrentModule->ConfigMacros.clear();
5580       CurrentModule->UnresolvedConflicts.clear();
5581       CurrentModule->Conflicts.clear();
5582 
5583       // The module is available unless it's missing a requirement; relevant
5584       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5585       // Missing headers that were present when the module was built do not
5586       // make it unavailable -- if we got this far, this must be an explicitly
5587       // imported module file.
5588       CurrentModule->Requirements.clear();
5589       CurrentModule->MissingHeaders.clear();
5590       CurrentModule->IsUnimportable =
5591           ParentModule && ParentModule->IsUnimportable;
5592       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5593       break;
5594     }
5595 
5596     case SUBMODULE_UMBRELLA_HEADER: {
5597       // FIXME: This doesn't work for framework modules as `Filename` is the
5598       //        name as written in the module file and does not include
5599       //        `Headers/`, so this path will never exist.
5600       std::string Filename = std::string(Blob);
5601       ResolveImportedPath(F, Filename);
5602       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5603         if (!CurrentModule->getUmbrellaHeader()) {
5604           // FIXME: NameAsWritten
5605           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5606         }
5607         // Note that it's too late at this point to return out of date if the
5608         // name from the PCM doesn't match up with the one in the module map,
5609         // but also quite unlikely since we will have already checked the
5610         // modification time and size of the module map file itself.
5611       }
5612       break;
5613     }
5614 
5615     case SUBMODULE_HEADER:
5616     case SUBMODULE_EXCLUDED_HEADER:
5617     case SUBMODULE_PRIVATE_HEADER:
5618       // We lazily associate headers with their modules via the HeaderInfo table.
5619       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5620       // of complete filenames or remove it entirely.
5621       break;
5622 
5623     case SUBMODULE_TEXTUAL_HEADER:
5624     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5625       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5626       // them here.
5627       break;
5628 
5629     case SUBMODULE_TOPHEADER:
5630       CurrentModule->addTopHeaderFilename(Blob);
5631       break;
5632 
5633     case SUBMODULE_UMBRELLA_DIR: {
5634       // See comments in SUBMODULE_UMBRELLA_HEADER
5635       std::string Dirname = std::string(Blob);
5636       ResolveImportedPath(F, Dirname);
5637       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5638         if (!CurrentModule->getUmbrellaDir()) {
5639           // FIXME: NameAsWritten
5640           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5641         }
5642       }
5643       break;
5644     }
5645 
5646     case SUBMODULE_METADATA: {
5647       F.BaseSubmoduleID = getTotalNumSubmodules();
5648       F.LocalNumSubmodules = Record[0];
5649       unsigned LocalBaseSubmoduleID = Record[1];
5650       if (F.LocalNumSubmodules > 0) {
5651         // Introduce the global -> local mapping for submodules within this
5652         // module.
5653         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5654 
5655         // Introduce the local -> global mapping for submodules within this
5656         // module.
5657         F.SubmoduleRemap.insertOrReplace(
5658           std::make_pair(LocalBaseSubmoduleID,
5659                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5660 
5661         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5662       }
5663       break;
5664     }
5665 
5666     case SUBMODULE_IMPORTS:
5667       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5668         UnresolvedModuleRef Unresolved;
5669         Unresolved.File = &F;
5670         Unresolved.Mod = CurrentModule;
5671         Unresolved.ID = Record[Idx];
5672         Unresolved.Kind = UnresolvedModuleRef::Import;
5673         Unresolved.IsWildcard = false;
5674         UnresolvedModuleRefs.push_back(Unresolved);
5675       }
5676       break;
5677 
5678     case SUBMODULE_EXPORTS:
5679       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5680         UnresolvedModuleRef Unresolved;
5681         Unresolved.File = &F;
5682         Unresolved.Mod = CurrentModule;
5683         Unresolved.ID = Record[Idx];
5684         Unresolved.Kind = UnresolvedModuleRef::Export;
5685         Unresolved.IsWildcard = Record[Idx + 1];
5686         UnresolvedModuleRefs.push_back(Unresolved);
5687       }
5688 
5689       // Once we've loaded the set of exports, there's no reason to keep
5690       // the parsed, unresolved exports around.
5691       CurrentModule->UnresolvedExports.clear();
5692       break;
5693 
5694     case SUBMODULE_REQUIRES:
5695       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5696                                     PP.getTargetInfo());
5697       break;
5698 
5699     case SUBMODULE_LINK_LIBRARY:
5700       ModMap.resolveLinkAsDependencies(CurrentModule);
5701       CurrentModule->LinkLibraries.push_back(
5702           Module::LinkLibrary(std::string(Blob), Record[0]));
5703       break;
5704 
5705     case SUBMODULE_CONFIG_MACRO:
5706       CurrentModule->ConfigMacros.push_back(Blob.str());
5707       break;
5708 
5709     case SUBMODULE_CONFLICT: {
5710       UnresolvedModuleRef Unresolved;
5711       Unresolved.File = &F;
5712       Unresolved.Mod = CurrentModule;
5713       Unresolved.ID = Record[0];
5714       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5715       Unresolved.IsWildcard = false;
5716       Unresolved.String = Blob;
5717       UnresolvedModuleRefs.push_back(Unresolved);
5718       break;
5719     }
5720 
5721     case SUBMODULE_INITIALIZERS: {
5722       if (!ContextObj)
5723         break;
5724       SmallVector<uint32_t, 16> Inits;
5725       for (auto &ID : Record)
5726         Inits.push_back(getGlobalDeclID(F, ID));
5727       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5728       break;
5729     }
5730 
5731     case SUBMODULE_EXPORT_AS:
5732       CurrentModule->ExportAsModule = Blob.str();
5733       ModMap.addLinkAsDependency(CurrentModule);
5734       break;
5735     }
5736   }
5737 }
5738 
5739 /// Parse the record that corresponds to a LangOptions data
5740 /// structure.
5741 ///
5742 /// This routine parses the language options from the AST file and then gives
5743 /// them to the AST listener if one is set.
5744 ///
5745 /// \returns true if the listener deems the file unacceptable, false otherwise.
5746 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5747                                      bool Complain,
5748                                      ASTReaderListener &Listener,
5749                                      bool AllowCompatibleDifferences) {
5750   LangOptions LangOpts;
5751   unsigned Idx = 0;
5752 #define LANGOPT(Name, Bits, Default, Description) \
5753   LangOpts.Name = Record[Idx++];
5754 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5755   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5756 #include "clang/Basic/LangOptions.def"
5757 #define SANITIZER(NAME, ID)                                                    \
5758   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5759 #include "clang/Basic/Sanitizers.def"
5760 
5761   for (unsigned N = Record[Idx++]; N; --N)
5762     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5763 
5764   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5765   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5766   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5767 
5768   LangOpts.CurrentModule = ReadString(Record, Idx);
5769 
5770   // Comment options.
5771   for (unsigned N = Record[Idx++]; N; --N) {
5772     LangOpts.CommentOpts.BlockCommandNames.push_back(
5773       ReadString(Record, Idx));
5774   }
5775   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5776 
5777   // OpenMP offloading options.
5778   for (unsigned N = Record[Idx++]; N; --N) {
5779     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5780   }
5781 
5782   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5783 
5784   return Listener.ReadLanguageOptions(LangOpts, Complain,
5785                                       AllowCompatibleDifferences);
5786 }
5787 
5788 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5789                                    ASTReaderListener &Listener,
5790                                    bool AllowCompatibleDifferences) {
5791   unsigned Idx = 0;
5792   TargetOptions TargetOpts;
5793   TargetOpts.Triple = ReadString(Record, Idx);
5794   TargetOpts.CPU = ReadString(Record, Idx);
5795   TargetOpts.TuneCPU = ReadString(Record, Idx);
5796   TargetOpts.ABI = ReadString(Record, Idx);
5797   for (unsigned N = Record[Idx++]; N; --N) {
5798     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5799   }
5800   for (unsigned N = Record[Idx++]; N; --N) {
5801     TargetOpts.Features.push_back(ReadString(Record, Idx));
5802   }
5803 
5804   return Listener.ReadTargetOptions(TargetOpts, Complain,
5805                                     AllowCompatibleDifferences);
5806 }
5807 
5808 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5809                                        ASTReaderListener &Listener) {
5810   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5811   unsigned Idx = 0;
5812 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5813 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5814   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5815 #include "clang/Basic/DiagnosticOptions.def"
5816 
5817   for (unsigned N = Record[Idx++]; N; --N)
5818     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5819   for (unsigned N = Record[Idx++]; N; --N)
5820     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5821 
5822   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5823 }
5824 
5825 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5826                                        ASTReaderListener &Listener) {
5827   FileSystemOptions FSOpts;
5828   unsigned Idx = 0;
5829   FSOpts.WorkingDir = ReadString(Record, Idx);
5830   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5831 }
5832 
5833 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5834                                          bool Complain,
5835                                          ASTReaderListener &Listener) {
5836   HeaderSearchOptions HSOpts;
5837   unsigned Idx = 0;
5838   HSOpts.Sysroot = ReadString(Record, Idx);
5839 
5840   // Include entries.
5841   for (unsigned N = Record[Idx++]; N; --N) {
5842     std::string Path = ReadString(Record, Idx);
5843     frontend::IncludeDirGroup Group
5844       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5845     bool IsFramework = Record[Idx++];
5846     bool IgnoreSysRoot = Record[Idx++];
5847     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5848                                     IgnoreSysRoot);
5849   }
5850 
5851   // System header prefixes.
5852   for (unsigned N = Record[Idx++]; N; --N) {
5853     std::string Prefix = ReadString(Record, Idx);
5854     bool IsSystemHeader = Record[Idx++];
5855     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5856   }
5857 
5858   HSOpts.ResourceDir = ReadString(Record, Idx);
5859   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5860   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5861   HSOpts.DisableModuleHash = Record[Idx++];
5862   HSOpts.ImplicitModuleMaps = Record[Idx++];
5863   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5864   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5865   HSOpts.UseBuiltinIncludes = Record[Idx++];
5866   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5867   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5868   HSOpts.UseLibcxx = Record[Idx++];
5869   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5870 
5871   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5872                                           Complain);
5873 }
5874 
5875 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5876                                          bool Complain,
5877                                          ASTReaderListener &Listener,
5878                                          std::string &SuggestedPredefines) {
5879   PreprocessorOptions PPOpts;
5880   unsigned Idx = 0;
5881 
5882   // Macro definitions/undefs
5883   for (unsigned N = Record[Idx++]; N; --N) {
5884     std::string Macro = ReadString(Record, Idx);
5885     bool IsUndef = Record[Idx++];
5886     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5887   }
5888 
5889   // Includes
5890   for (unsigned N = Record[Idx++]; N; --N) {
5891     PPOpts.Includes.push_back(ReadString(Record, Idx));
5892   }
5893 
5894   // Macro Includes
5895   for (unsigned N = Record[Idx++]; N; --N) {
5896     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5897   }
5898 
5899   PPOpts.UsePredefines = Record[Idx++];
5900   PPOpts.DetailedRecord = Record[Idx++];
5901   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5902   PPOpts.ObjCXXARCStandardLibrary =
5903     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5904   SuggestedPredefines.clear();
5905   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5906                                           SuggestedPredefines);
5907 }
5908 
5909 std::pair<ModuleFile *, unsigned>
5910 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5911   GlobalPreprocessedEntityMapType::iterator
5912   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5913   assert(I != GlobalPreprocessedEntityMap.end() &&
5914          "Corrupted global preprocessed entity map");
5915   ModuleFile *M = I->second;
5916   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5917   return std::make_pair(M, LocalIndex);
5918 }
5919 
5920 llvm::iterator_range<PreprocessingRecord::iterator>
5921 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5922   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5923     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5924                                              Mod.NumPreprocessedEntities);
5925 
5926   return llvm::make_range(PreprocessingRecord::iterator(),
5927                           PreprocessingRecord::iterator());
5928 }
5929 
5930 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
5931                                         unsigned int ClientLoadCapabilities) {
5932   return ClientLoadCapabilities & ARR_OutOfDate &&
5933          !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
5934 }
5935 
5936 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5937 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5938   return llvm::make_range(
5939       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5940       ModuleDeclIterator(this, &Mod,
5941                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5942 }
5943 
5944 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5945   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5946   assert(I != GlobalSkippedRangeMap.end() &&
5947     "Corrupted global skipped range map");
5948   ModuleFile *M = I->second;
5949   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5950   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5951   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5952   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5953                     TranslateSourceLocation(*M, RawRange.getEnd()));
5954   assert(Range.isValid());
5955   return Range;
5956 }
5957 
5958 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5959   PreprocessedEntityID PPID = Index+1;
5960   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5961   ModuleFile &M = *PPInfo.first;
5962   unsigned LocalIndex = PPInfo.second;
5963   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5964 
5965   if (!PP.getPreprocessingRecord()) {
5966     Error("no preprocessing record");
5967     return nullptr;
5968   }
5969 
5970   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5971   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5972           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5973     Error(std::move(Err));
5974     return nullptr;
5975   }
5976 
5977   Expected<llvm::BitstreamEntry> MaybeEntry =
5978       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5979   if (!MaybeEntry) {
5980     Error(MaybeEntry.takeError());
5981     return nullptr;
5982   }
5983   llvm::BitstreamEntry Entry = MaybeEntry.get();
5984 
5985   if (Entry.Kind != llvm::BitstreamEntry::Record)
5986     return nullptr;
5987 
5988   // Read the record.
5989   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5990                     TranslateSourceLocation(M, PPOffs.getEnd()));
5991   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5992   StringRef Blob;
5993   RecordData Record;
5994   Expected<unsigned> MaybeRecType =
5995       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5996   if (!MaybeRecType) {
5997     Error(MaybeRecType.takeError());
5998     return nullptr;
5999   }
6000   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6001   case PPD_MACRO_EXPANSION: {
6002     bool isBuiltin = Record[0];
6003     IdentifierInfo *Name = nullptr;
6004     MacroDefinitionRecord *Def = nullptr;
6005     if (isBuiltin)
6006       Name = getLocalIdentifier(M, Record[1]);
6007     else {
6008       PreprocessedEntityID GlobalID =
6009           getGlobalPreprocessedEntityID(M, Record[1]);
6010       Def = cast<MacroDefinitionRecord>(
6011           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6012     }
6013 
6014     MacroExpansion *ME;
6015     if (isBuiltin)
6016       ME = new (PPRec) MacroExpansion(Name, Range);
6017     else
6018       ME = new (PPRec) MacroExpansion(Def, Range);
6019 
6020     return ME;
6021   }
6022 
6023   case PPD_MACRO_DEFINITION: {
6024     // Decode the identifier info and then check again; if the macro is
6025     // still defined and associated with the identifier,
6026     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6027     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6028 
6029     if (DeserializationListener)
6030       DeserializationListener->MacroDefinitionRead(PPID, MD);
6031 
6032     return MD;
6033   }
6034 
6035   case PPD_INCLUSION_DIRECTIVE: {
6036     const char *FullFileNameStart = Blob.data() + Record[0];
6037     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6038     const FileEntry *File = nullptr;
6039     if (!FullFileName.empty())
6040       if (auto FE = PP.getFileManager().getFile(FullFileName))
6041         File = *FE;
6042 
6043     // FIXME: Stable encoding
6044     InclusionDirective::InclusionKind Kind
6045       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6046     InclusionDirective *ID
6047       = new (PPRec) InclusionDirective(PPRec, Kind,
6048                                        StringRef(Blob.data(), Record[0]),
6049                                        Record[1], Record[3],
6050                                        File,
6051                                        Range);
6052     return ID;
6053   }
6054   }
6055 
6056   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6057 }
6058 
6059 /// Find the next module that contains entities and return the ID
6060 /// of the first entry.
6061 ///
6062 /// \param SLocMapI points at a chunk of a module that contains no
6063 /// preprocessed entities or the entities it contains are not the ones we are
6064 /// looking for.
6065 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6066                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6067   ++SLocMapI;
6068   for (GlobalSLocOffsetMapType::const_iterator
6069          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6070     ModuleFile &M = *SLocMapI->second;
6071     if (M.NumPreprocessedEntities)
6072       return M.BasePreprocessedEntityID;
6073   }
6074 
6075   return getTotalNumPreprocessedEntities();
6076 }
6077 
6078 namespace {
6079 
6080 struct PPEntityComp {
6081   const ASTReader &Reader;
6082   ModuleFile &M;
6083 
6084   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6085 
6086   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6087     SourceLocation LHS = getLoc(L);
6088     SourceLocation RHS = getLoc(R);
6089     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6090   }
6091 
6092   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6093     SourceLocation LHS = getLoc(L);
6094     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6095   }
6096 
6097   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6098     SourceLocation RHS = getLoc(R);
6099     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6100   }
6101 
6102   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6103     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6104   }
6105 };
6106 
6107 } // namespace
6108 
6109 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6110                                                        bool EndsAfter) const {
6111   if (SourceMgr.isLocalSourceLocation(Loc))
6112     return getTotalNumPreprocessedEntities();
6113 
6114   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6115       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6116   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6117          "Corrupted global sloc offset map");
6118 
6119   if (SLocMapI->second->NumPreprocessedEntities == 0)
6120     return findNextPreprocessedEntity(SLocMapI);
6121 
6122   ModuleFile &M = *SLocMapI->second;
6123 
6124   using pp_iterator = const PPEntityOffset *;
6125 
6126   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6127   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6128 
6129   size_t Count = M.NumPreprocessedEntities;
6130   size_t Half;
6131   pp_iterator First = pp_begin;
6132   pp_iterator PPI;
6133 
6134   if (EndsAfter) {
6135     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6136                            PPEntityComp(*this, M));
6137   } else {
6138     // Do a binary search manually instead of using std::lower_bound because
6139     // The end locations of entities may be unordered (when a macro expansion
6140     // is inside another macro argument), but for this case it is not important
6141     // whether we get the first macro expansion or its containing macro.
6142     while (Count > 0) {
6143       Half = Count / 2;
6144       PPI = First;
6145       std::advance(PPI, Half);
6146       if (SourceMgr.isBeforeInTranslationUnit(
6147               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6148         First = PPI;
6149         ++First;
6150         Count = Count - Half - 1;
6151       } else
6152         Count = Half;
6153     }
6154   }
6155 
6156   if (PPI == pp_end)
6157     return findNextPreprocessedEntity(SLocMapI);
6158 
6159   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6160 }
6161 
6162 /// Returns a pair of [Begin, End) indices of preallocated
6163 /// preprocessed entities that \arg Range encompasses.
6164 std::pair<unsigned, unsigned>
6165     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6166   if (Range.isInvalid())
6167     return std::make_pair(0,0);
6168   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6169 
6170   PreprocessedEntityID BeginID =
6171       findPreprocessedEntity(Range.getBegin(), false);
6172   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6173   return std::make_pair(BeginID, EndID);
6174 }
6175 
6176 /// Optionally returns true or false if the preallocated preprocessed
6177 /// entity with index \arg Index came from file \arg FID.
6178 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6179                                                              FileID FID) {
6180   if (FID.isInvalid())
6181     return false;
6182 
6183   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6184   ModuleFile &M = *PPInfo.first;
6185   unsigned LocalIndex = PPInfo.second;
6186   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6187 
6188   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6189   if (Loc.isInvalid())
6190     return false;
6191 
6192   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6193     return true;
6194   else
6195     return false;
6196 }
6197 
6198 namespace {
6199 
6200   /// Visitor used to search for information about a header file.
6201   class HeaderFileInfoVisitor {
6202     const FileEntry *FE;
6203     Optional<HeaderFileInfo> HFI;
6204 
6205   public:
6206     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6207 
6208     bool operator()(ModuleFile &M) {
6209       HeaderFileInfoLookupTable *Table
6210         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6211       if (!Table)
6212         return false;
6213 
6214       // Look in the on-disk hash table for an entry for this file name.
6215       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6216       if (Pos == Table->end())
6217         return false;
6218 
6219       HFI = *Pos;
6220       return true;
6221     }
6222 
6223     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6224   };
6225 
6226 } // namespace
6227 
6228 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6229   HeaderFileInfoVisitor Visitor(FE);
6230   ModuleMgr.visit(Visitor);
6231   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6232     return *HFI;
6233 
6234   return HeaderFileInfo();
6235 }
6236 
6237 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6238   using DiagState = DiagnosticsEngine::DiagState;
6239   SmallVector<DiagState *, 32> DiagStates;
6240 
6241   for (ModuleFile &F : ModuleMgr) {
6242     unsigned Idx = 0;
6243     auto &Record = F.PragmaDiagMappings;
6244     if (Record.empty())
6245       continue;
6246 
6247     DiagStates.clear();
6248 
6249     auto ReadDiagState =
6250         [&](const DiagState &BasedOn, SourceLocation Loc,
6251             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6252       unsigned BackrefID = Record[Idx++];
6253       if (BackrefID != 0)
6254         return DiagStates[BackrefID - 1];
6255 
6256       // A new DiagState was created here.
6257       Diag.DiagStates.push_back(BasedOn);
6258       DiagState *NewState = &Diag.DiagStates.back();
6259       DiagStates.push_back(NewState);
6260       unsigned Size = Record[Idx++];
6261       assert(Idx + Size * 2 <= Record.size() &&
6262              "Invalid data, not enough diag/map pairs");
6263       while (Size--) {
6264         unsigned DiagID = Record[Idx++];
6265         DiagnosticMapping NewMapping =
6266             DiagnosticMapping::deserialize(Record[Idx++]);
6267         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6268           continue;
6269 
6270         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6271 
6272         // If this mapping was specified as a warning but the severity was
6273         // upgraded due to diagnostic settings, simulate the current diagnostic
6274         // settings (and use a warning).
6275         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6276           NewMapping.setSeverity(diag::Severity::Warning);
6277           NewMapping.setUpgradedFromWarning(false);
6278         }
6279 
6280         Mapping = NewMapping;
6281       }
6282       return NewState;
6283     };
6284 
6285     // Read the first state.
6286     DiagState *FirstState;
6287     if (F.Kind == MK_ImplicitModule) {
6288       // Implicitly-built modules are reused with different diagnostic
6289       // settings.  Use the initial diagnostic state from Diag to simulate this
6290       // compilation's diagnostic settings.
6291       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6292       DiagStates.push_back(FirstState);
6293 
6294       // Skip the initial diagnostic state from the serialized module.
6295       assert(Record[1] == 0 &&
6296              "Invalid data, unexpected backref in initial state");
6297       Idx = 3 + Record[2] * 2;
6298       assert(Idx < Record.size() &&
6299              "Invalid data, not enough state change pairs in initial state");
6300     } else if (F.isModule()) {
6301       // For an explicit module, preserve the flags from the module build
6302       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6303       // -Wblah flags.
6304       unsigned Flags = Record[Idx++];
6305       DiagState Initial;
6306       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6307       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6308       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6309       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6310       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6311       Initial.ExtBehavior = (diag::Severity)Flags;
6312       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6313 
6314       assert(F.OriginalSourceFileID.isValid());
6315 
6316       // Set up the root buffer of the module to start with the initial
6317       // diagnostic state of the module itself, to cover files that contain no
6318       // explicit transitions (for which we did not serialize anything).
6319       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6320           .StateTransitions.push_back({FirstState, 0});
6321     } else {
6322       // For prefix ASTs, start with whatever the user configured on the
6323       // command line.
6324       Idx++; // Skip flags.
6325       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6326                                  SourceLocation(), false);
6327     }
6328 
6329     // Read the state transitions.
6330     unsigned NumLocations = Record[Idx++];
6331     while (NumLocations--) {
6332       assert(Idx < Record.size() &&
6333              "Invalid data, missing pragma diagnostic states");
6334       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6335       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6336       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6337       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6338       unsigned Transitions = Record[Idx++];
6339 
6340       // Note that we don't need to set up Parent/ParentOffset here, because
6341       // we won't be changing the diagnostic state within imported FileIDs
6342       // (other than perhaps appending to the main source file, which has no
6343       // parent).
6344       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6345       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6346       for (unsigned I = 0; I != Transitions; ++I) {
6347         unsigned Offset = Record[Idx++];
6348         auto *State =
6349             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6350         F.StateTransitions.push_back({State, Offset});
6351       }
6352     }
6353 
6354     // Read the final state.
6355     assert(Idx < Record.size() &&
6356            "Invalid data, missing final pragma diagnostic state");
6357     SourceLocation CurStateLoc =
6358         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6359     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6360 
6361     if (!F.isModule()) {
6362       Diag.DiagStatesByLoc.CurDiagState = CurState;
6363       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6364 
6365       // Preserve the property that the imaginary root file describes the
6366       // current state.
6367       FileID NullFile;
6368       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6369       if (T.empty())
6370         T.push_back({CurState, 0});
6371       else
6372         T[0].State = CurState;
6373     }
6374 
6375     // Don't try to read these mappings again.
6376     Record.clear();
6377   }
6378 }
6379 
6380 /// Get the correct cursor and offset for loading a type.
6381 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6382   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6383   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6384   ModuleFile *M = I->second;
6385   return RecordLocation(
6386       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6387              M->DeclsBlockStartOffset);
6388 }
6389 
6390 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6391   switch (code) {
6392 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6393   case TYPE_##CODE_ID: return Type::CLASS_ID;
6394 #include "clang/Serialization/TypeBitCodes.def"
6395   default: return llvm::None;
6396   }
6397 }
6398 
6399 /// Read and return the type with the given index..
6400 ///
6401 /// The index is the type ID, shifted and minus the number of predefs. This
6402 /// routine actually reads the record corresponding to the type at the given
6403 /// location. It is a helper routine for GetType, which deals with reading type
6404 /// IDs.
6405 QualType ASTReader::readTypeRecord(unsigned Index) {
6406   assert(ContextObj && "reading type with no AST context");
6407   ASTContext &Context = *ContextObj;
6408   RecordLocation Loc = TypeCursorForIndex(Index);
6409   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6410 
6411   // Keep track of where we are in the stream, then jump back there
6412   // after reading this type.
6413   SavedStreamPosition SavedPosition(DeclsCursor);
6414 
6415   ReadingKindTracker ReadingKind(Read_Type, *this);
6416 
6417   // Note that we are loading a type record.
6418   Deserializing AType(this);
6419 
6420   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6421     Error(std::move(Err));
6422     return QualType();
6423   }
6424   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6425   if (!RawCode) {
6426     Error(RawCode.takeError());
6427     return QualType();
6428   }
6429 
6430   ASTRecordReader Record(*this, *Loc.F);
6431   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6432   if (!Code) {
6433     Error(Code.takeError());
6434     return QualType();
6435   }
6436   if (Code.get() == TYPE_EXT_QUAL) {
6437     QualType baseType = Record.readQualType();
6438     Qualifiers quals = Record.readQualifiers();
6439     return Context.getQualifiedType(baseType, quals);
6440   }
6441 
6442   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6443   if (!maybeClass) {
6444     Error("Unexpected code for type");
6445     return QualType();
6446   }
6447 
6448   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6449   return TypeReader.read(*maybeClass);
6450 }
6451 
6452 namespace clang {
6453 
6454 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6455   ASTRecordReader &Reader;
6456 
6457   SourceLocation readSourceLocation() {
6458     return Reader.readSourceLocation();
6459   }
6460 
6461   TypeSourceInfo *GetTypeSourceInfo() {
6462     return Reader.readTypeSourceInfo();
6463   }
6464 
6465   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6466     return Reader.readNestedNameSpecifierLoc();
6467   }
6468 
6469   Attr *ReadAttr() {
6470     return Reader.readAttr();
6471   }
6472 
6473 public:
6474   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6475 
6476   // We want compile-time assurance that we've enumerated all of
6477   // these, so unfortunately we have to declare them first, then
6478   // define them out-of-line.
6479 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6480 #define TYPELOC(CLASS, PARENT) \
6481   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6482 #include "clang/AST/TypeLocNodes.def"
6483 
6484   void VisitFunctionTypeLoc(FunctionTypeLoc);
6485   void VisitArrayTypeLoc(ArrayTypeLoc);
6486 };
6487 
6488 } // namespace clang
6489 
6490 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6491   // nothing to do
6492 }
6493 
6494 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6495   TL.setBuiltinLoc(readSourceLocation());
6496   if (TL.needsExtraLocalData()) {
6497     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6498     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6499     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6500     TL.setModeAttr(Reader.readInt());
6501   }
6502 }
6503 
6504 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6505   TL.setNameLoc(readSourceLocation());
6506 }
6507 
6508 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6509   TL.setStarLoc(readSourceLocation());
6510 }
6511 
6512 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6513   // nothing to do
6514 }
6515 
6516 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6517   // nothing to do
6518 }
6519 
6520 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6521   TL.setExpansionLoc(readSourceLocation());
6522 }
6523 
6524 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6525   TL.setCaretLoc(readSourceLocation());
6526 }
6527 
6528 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6529   TL.setAmpLoc(readSourceLocation());
6530 }
6531 
6532 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6533   TL.setAmpAmpLoc(readSourceLocation());
6534 }
6535 
6536 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6537   TL.setStarLoc(readSourceLocation());
6538   TL.setClassTInfo(GetTypeSourceInfo());
6539 }
6540 
6541 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6542   TL.setLBracketLoc(readSourceLocation());
6543   TL.setRBracketLoc(readSourceLocation());
6544   if (Reader.readBool())
6545     TL.setSizeExpr(Reader.readExpr());
6546   else
6547     TL.setSizeExpr(nullptr);
6548 }
6549 
6550 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6551   VisitArrayTypeLoc(TL);
6552 }
6553 
6554 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6555   VisitArrayTypeLoc(TL);
6556 }
6557 
6558 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6559   VisitArrayTypeLoc(TL);
6560 }
6561 
6562 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6563                                             DependentSizedArrayTypeLoc TL) {
6564   VisitArrayTypeLoc(TL);
6565 }
6566 
6567 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6568     DependentAddressSpaceTypeLoc TL) {
6569 
6570     TL.setAttrNameLoc(readSourceLocation());
6571     TL.setAttrOperandParensRange(Reader.readSourceRange());
6572     TL.setAttrExprOperand(Reader.readExpr());
6573 }
6574 
6575 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6576                                         DependentSizedExtVectorTypeLoc TL) {
6577   TL.setNameLoc(readSourceLocation());
6578 }
6579 
6580 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6581   TL.setNameLoc(readSourceLocation());
6582 }
6583 
6584 void TypeLocReader::VisitDependentVectorTypeLoc(
6585     DependentVectorTypeLoc TL) {
6586   TL.setNameLoc(readSourceLocation());
6587 }
6588 
6589 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6590   TL.setNameLoc(readSourceLocation());
6591 }
6592 
6593 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6594   TL.setAttrNameLoc(readSourceLocation());
6595   TL.setAttrOperandParensRange(Reader.readSourceRange());
6596   TL.setAttrRowOperand(Reader.readExpr());
6597   TL.setAttrColumnOperand(Reader.readExpr());
6598 }
6599 
6600 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6601     DependentSizedMatrixTypeLoc TL) {
6602   TL.setAttrNameLoc(readSourceLocation());
6603   TL.setAttrOperandParensRange(Reader.readSourceRange());
6604   TL.setAttrRowOperand(Reader.readExpr());
6605   TL.setAttrColumnOperand(Reader.readExpr());
6606 }
6607 
6608 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6609   TL.setLocalRangeBegin(readSourceLocation());
6610   TL.setLParenLoc(readSourceLocation());
6611   TL.setRParenLoc(readSourceLocation());
6612   TL.setExceptionSpecRange(Reader.readSourceRange());
6613   TL.setLocalRangeEnd(readSourceLocation());
6614   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6615     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6616   }
6617 }
6618 
6619 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6620   VisitFunctionTypeLoc(TL);
6621 }
6622 
6623 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6624   VisitFunctionTypeLoc(TL);
6625 }
6626 
6627 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6628   TL.setNameLoc(readSourceLocation());
6629 }
6630 
6631 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
6632   TL.setNameLoc(readSourceLocation());
6633 }
6634 
6635 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6636   TL.setNameLoc(readSourceLocation());
6637 }
6638 
6639 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6640   TL.setTypeofLoc(readSourceLocation());
6641   TL.setLParenLoc(readSourceLocation());
6642   TL.setRParenLoc(readSourceLocation());
6643 }
6644 
6645 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6646   TL.setTypeofLoc(readSourceLocation());
6647   TL.setLParenLoc(readSourceLocation());
6648   TL.setRParenLoc(readSourceLocation());
6649   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6650 }
6651 
6652 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6653   TL.setDecltypeLoc(readSourceLocation());
6654   TL.setRParenLoc(readSourceLocation());
6655 }
6656 
6657 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6658   TL.setKWLoc(readSourceLocation());
6659   TL.setLParenLoc(readSourceLocation());
6660   TL.setRParenLoc(readSourceLocation());
6661   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6662 }
6663 
6664 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6665   TL.setNameLoc(readSourceLocation());
6666   if (Reader.readBool()) {
6667     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6668     TL.setTemplateKWLoc(readSourceLocation());
6669     TL.setConceptNameLoc(readSourceLocation());
6670     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6671     TL.setLAngleLoc(readSourceLocation());
6672     TL.setRAngleLoc(readSourceLocation());
6673     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6674       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6675                               TL.getTypePtr()->getArg(i).getKind()));
6676   }
6677   if (Reader.readBool())
6678     TL.setRParenLoc(readSourceLocation());
6679 }
6680 
6681 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6682     DeducedTemplateSpecializationTypeLoc TL) {
6683   TL.setTemplateNameLoc(readSourceLocation());
6684 }
6685 
6686 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6687   TL.setNameLoc(readSourceLocation());
6688 }
6689 
6690 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6691   TL.setNameLoc(readSourceLocation());
6692 }
6693 
6694 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6695   TL.setAttr(ReadAttr());
6696 }
6697 
6698 void TypeLocReader::VisitBTFTagAttributedTypeLoc(BTFTagAttributedTypeLoc TL) {
6699   // Nothing to do.
6700 }
6701 
6702 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6703   TL.setNameLoc(readSourceLocation());
6704 }
6705 
6706 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6707                                             SubstTemplateTypeParmTypeLoc TL) {
6708   TL.setNameLoc(readSourceLocation());
6709 }
6710 
6711 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6712                                           SubstTemplateTypeParmPackTypeLoc TL) {
6713   TL.setNameLoc(readSourceLocation());
6714 }
6715 
6716 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6717                                            TemplateSpecializationTypeLoc TL) {
6718   TL.setTemplateKeywordLoc(readSourceLocation());
6719   TL.setTemplateNameLoc(readSourceLocation());
6720   TL.setLAngleLoc(readSourceLocation());
6721   TL.setRAngleLoc(readSourceLocation());
6722   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6723     TL.setArgLocInfo(
6724         i,
6725         Reader.readTemplateArgumentLocInfo(
6726           TL.getTypePtr()->getArg(i).getKind()));
6727 }
6728 
6729 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6730   TL.setLParenLoc(readSourceLocation());
6731   TL.setRParenLoc(readSourceLocation());
6732 }
6733 
6734 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6735   TL.setElaboratedKeywordLoc(readSourceLocation());
6736   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6737 }
6738 
6739 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6740   TL.setNameLoc(readSourceLocation());
6741 }
6742 
6743 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6744   TL.setElaboratedKeywordLoc(readSourceLocation());
6745   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6746   TL.setNameLoc(readSourceLocation());
6747 }
6748 
6749 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6750        DependentTemplateSpecializationTypeLoc TL) {
6751   TL.setElaboratedKeywordLoc(readSourceLocation());
6752   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6753   TL.setTemplateKeywordLoc(readSourceLocation());
6754   TL.setTemplateNameLoc(readSourceLocation());
6755   TL.setLAngleLoc(readSourceLocation());
6756   TL.setRAngleLoc(readSourceLocation());
6757   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6758     TL.setArgLocInfo(
6759         I,
6760         Reader.readTemplateArgumentLocInfo(
6761             TL.getTypePtr()->getArg(I).getKind()));
6762 }
6763 
6764 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6765   TL.setEllipsisLoc(readSourceLocation());
6766 }
6767 
6768 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6769   TL.setNameLoc(readSourceLocation());
6770 }
6771 
6772 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6773   if (TL.getNumProtocols()) {
6774     TL.setProtocolLAngleLoc(readSourceLocation());
6775     TL.setProtocolRAngleLoc(readSourceLocation());
6776   }
6777   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6778     TL.setProtocolLoc(i, readSourceLocation());
6779 }
6780 
6781 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6782   TL.setHasBaseTypeAsWritten(Reader.readBool());
6783   TL.setTypeArgsLAngleLoc(readSourceLocation());
6784   TL.setTypeArgsRAngleLoc(readSourceLocation());
6785   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6786     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6787   TL.setProtocolLAngleLoc(readSourceLocation());
6788   TL.setProtocolRAngleLoc(readSourceLocation());
6789   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6790     TL.setProtocolLoc(i, readSourceLocation());
6791 }
6792 
6793 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6794   TL.setStarLoc(readSourceLocation());
6795 }
6796 
6797 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6798   TL.setKWLoc(readSourceLocation());
6799   TL.setLParenLoc(readSourceLocation());
6800   TL.setRParenLoc(readSourceLocation());
6801 }
6802 
6803 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6804   TL.setKWLoc(readSourceLocation());
6805 }
6806 
6807 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
6808   TL.setNameLoc(readSourceLocation());
6809 }
6810 void TypeLocReader::VisitDependentBitIntTypeLoc(
6811     clang::DependentBitIntTypeLoc TL) {
6812   TL.setNameLoc(readSourceLocation());
6813 }
6814 
6815 
6816 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6817   TypeLocReader TLR(*this);
6818   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6819     TLR.Visit(TL);
6820 }
6821 
6822 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6823   QualType InfoTy = readType();
6824   if (InfoTy.isNull())
6825     return nullptr;
6826 
6827   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6828   readTypeLoc(TInfo->getTypeLoc());
6829   return TInfo;
6830 }
6831 
6832 QualType ASTReader::GetType(TypeID ID) {
6833   assert(ContextObj && "reading type with no AST context");
6834   ASTContext &Context = *ContextObj;
6835 
6836   unsigned FastQuals = ID & Qualifiers::FastMask;
6837   unsigned Index = ID >> Qualifiers::FastWidth;
6838 
6839   if (Index < NUM_PREDEF_TYPE_IDS) {
6840     QualType T;
6841     switch ((PredefinedTypeIDs)Index) {
6842     case PREDEF_TYPE_NULL_ID:
6843       return QualType();
6844     case PREDEF_TYPE_VOID_ID:
6845       T = Context.VoidTy;
6846       break;
6847     case PREDEF_TYPE_BOOL_ID:
6848       T = Context.BoolTy;
6849       break;
6850     case PREDEF_TYPE_CHAR_U_ID:
6851     case PREDEF_TYPE_CHAR_S_ID:
6852       // FIXME: Check that the signedness of CharTy is correct!
6853       T = Context.CharTy;
6854       break;
6855     case PREDEF_TYPE_UCHAR_ID:
6856       T = Context.UnsignedCharTy;
6857       break;
6858     case PREDEF_TYPE_USHORT_ID:
6859       T = Context.UnsignedShortTy;
6860       break;
6861     case PREDEF_TYPE_UINT_ID:
6862       T = Context.UnsignedIntTy;
6863       break;
6864     case PREDEF_TYPE_ULONG_ID:
6865       T = Context.UnsignedLongTy;
6866       break;
6867     case PREDEF_TYPE_ULONGLONG_ID:
6868       T = Context.UnsignedLongLongTy;
6869       break;
6870     case PREDEF_TYPE_UINT128_ID:
6871       T = Context.UnsignedInt128Ty;
6872       break;
6873     case PREDEF_TYPE_SCHAR_ID:
6874       T = Context.SignedCharTy;
6875       break;
6876     case PREDEF_TYPE_WCHAR_ID:
6877       T = Context.WCharTy;
6878       break;
6879     case PREDEF_TYPE_SHORT_ID:
6880       T = Context.ShortTy;
6881       break;
6882     case PREDEF_TYPE_INT_ID:
6883       T = Context.IntTy;
6884       break;
6885     case PREDEF_TYPE_LONG_ID:
6886       T = Context.LongTy;
6887       break;
6888     case PREDEF_TYPE_LONGLONG_ID:
6889       T = Context.LongLongTy;
6890       break;
6891     case PREDEF_TYPE_INT128_ID:
6892       T = Context.Int128Ty;
6893       break;
6894     case PREDEF_TYPE_BFLOAT16_ID:
6895       T = Context.BFloat16Ty;
6896       break;
6897     case PREDEF_TYPE_HALF_ID:
6898       T = Context.HalfTy;
6899       break;
6900     case PREDEF_TYPE_FLOAT_ID:
6901       T = Context.FloatTy;
6902       break;
6903     case PREDEF_TYPE_DOUBLE_ID:
6904       T = Context.DoubleTy;
6905       break;
6906     case PREDEF_TYPE_LONGDOUBLE_ID:
6907       T = Context.LongDoubleTy;
6908       break;
6909     case PREDEF_TYPE_SHORT_ACCUM_ID:
6910       T = Context.ShortAccumTy;
6911       break;
6912     case PREDEF_TYPE_ACCUM_ID:
6913       T = Context.AccumTy;
6914       break;
6915     case PREDEF_TYPE_LONG_ACCUM_ID:
6916       T = Context.LongAccumTy;
6917       break;
6918     case PREDEF_TYPE_USHORT_ACCUM_ID:
6919       T = Context.UnsignedShortAccumTy;
6920       break;
6921     case PREDEF_TYPE_UACCUM_ID:
6922       T = Context.UnsignedAccumTy;
6923       break;
6924     case PREDEF_TYPE_ULONG_ACCUM_ID:
6925       T = Context.UnsignedLongAccumTy;
6926       break;
6927     case PREDEF_TYPE_SHORT_FRACT_ID:
6928       T = Context.ShortFractTy;
6929       break;
6930     case PREDEF_TYPE_FRACT_ID:
6931       T = Context.FractTy;
6932       break;
6933     case PREDEF_TYPE_LONG_FRACT_ID:
6934       T = Context.LongFractTy;
6935       break;
6936     case PREDEF_TYPE_USHORT_FRACT_ID:
6937       T = Context.UnsignedShortFractTy;
6938       break;
6939     case PREDEF_TYPE_UFRACT_ID:
6940       T = Context.UnsignedFractTy;
6941       break;
6942     case PREDEF_TYPE_ULONG_FRACT_ID:
6943       T = Context.UnsignedLongFractTy;
6944       break;
6945     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6946       T = Context.SatShortAccumTy;
6947       break;
6948     case PREDEF_TYPE_SAT_ACCUM_ID:
6949       T = Context.SatAccumTy;
6950       break;
6951     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6952       T = Context.SatLongAccumTy;
6953       break;
6954     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6955       T = Context.SatUnsignedShortAccumTy;
6956       break;
6957     case PREDEF_TYPE_SAT_UACCUM_ID:
6958       T = Context.SatUnsignedAccumTy;
6959       break;
6960     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6961       T = Context.SatUnsignedLongAccumTy;
6962       break;
6963     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6964       T = Context.SatShortFractTy;
6965       break;
6966     case PREDEF_TYPE_SAT_FRACT_ID:
6967       T = Context.SatFractTy;
6968       break;
6969     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6970       T = Context.SatLongFractTy;
6971       break;
6972     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6973       T = Context.SatUnsignedShortFractTy;
6974       break;
6975     case PREDEF_TYPE_SAT_UFRACT_ID:
6976       T = Context.SatUnsignedFractTy;
6977       break;
6978     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6979       T = Context.SatUnsignedLongFractTy;
6980       break;
6981     case PREDEF_TYPE_FLOAT16_ID:
6982       T = Context.Float16Ty;
6983       break;
6984     case PREDEF_TYPE_FLOAT128_ID:
6985       T = Context.Float128Ty;
6986       break;
6987     case PREDEF_TYPE_IBM128_ID:
6988       T = Context.Ibm128Ty;
6989       break;
6990     case PREDEF_TYPE_OVERLOAD_ID:
6991       T = Context.OverloadTy;
6992       break;
6993     case PREDEF_TYPE_BOUND_MEMBER:
6994       T = Context.BoundMemberTy;
6995       break;
6996     case PREDEF_TYPE_PSEUDO_OBJECT:
6997       T = Context.PseudoObjectTy;
6998       break;
6999     case PREDEF_TYPE_DEPENDENT_ID:
7000       T = Context.DependentTy;
7001       break;
7002     case PREDEF_TYPE_UNKNOWN_ANY:
7003       T = Context.UnknownAnyTy;
7004       break;
7005     case PREDEF_TYPE_NULLPTR_ID:
7006       T = Context.NullPtrTy;
7007       break;
7008     case PREDEF_TYPE_CHAR8_ID:
7009       T = Context.Char8Ty;
7010       break;
7011     case PREDEF_TYPE_CHAR16_ID:
7012       T = Context.Char16Ty;
7013       break;
7014     case PREDEF_TYPE_CHAR32_ID:
7015       T = Context.Char32Ty;
7016       break;
7017     case PREDEF_TYPE_OBJC_ID:
7018       T = Context.ObjCBuiltinIdTy;
7019       break;
7020     case PREDEF_TYPE_OBJC_CLASS:
7021       T = Context.ObjCBuiltinClassTy;
7022       break;
7023     case PREDEF_TYPE_OBJC_SEL:
7024       T = Context.ObjCBuiltinSelTy;
7025       break;
7026 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7027     case PREDEF_TYPE_##Id##_ID: \
7028       T = Context.SingletonId; \
7029       break;
7030 #include "clang/Basic/OpenCLImageTypes.def"
7031 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7032     case PREDEF_TYPE_##Id##_ID: \
7033       T = Context.Id##Ty; \
7034       break;
7035 #include "clang/Basic/OpenCLExtensionTypes.def"
7036     case PREDEF_TYPE_SAMPLER_ID:
7037       T = Context.OCLSamplerTy;
7038       break;
7039     case PREDEF_TYPE_EVENT_ID:
7040       T = Context.OCLEventTy;
7041       break;
7042     case PREDEF_TYPE_CLK_EVENT_ID:
7043       T = Context.OCLClkEventTy;
7044       break;
7045     case PREDEF_TYPE_QUEUE_ID:
7046       T = Context.OCLQueueTy;
7047       break;
7048     case PREDEF_TYPE_RESERVE_ID_ID:
7049       T = Context.OCLReserveIDTy;
7050       break;
7051     case PREDEF_TYPE_AUTO_DEDUCT:
7052       T = Context.getAutoDeductType();
7053       break;
7054     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7055       T = Context.getAutoRRefDeductType();
7056       break;
7057     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7058       T = Context.ARCUnbridgedCastTy;
7059       break;
7060     case PREDEF_TYPE_BUILTIN_FN:
7061       T = Context.BuiltinFnTy;
7062       break;
7063     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7064       T = Context.IncompleteMatrixIdxTy;
7065       break;
7066     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7067       T = Context.OMPArraySectionTy;
7068       break;
7069     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7070       T = Context.OMPArraySectionTy;
7071       break;
7072     case PREDEF_TYPE_OMP_ITERATOR:
7073       T = Context.OMPIteratorTy;
7074       break;
7075 #define SVE_TYPE(Name, Id, SingletonId) \
7076     case PREDEF_TYPE_##Id##_ID: \
7077       T = Context.SingletonId; \
7078       break;
7079 #include "clang/Basic/AArch64SVEACLETypes.def"
7080 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7081     case PREDEF_TYPE_##Id##_ID: \
7082       T = Context.Id##Ty; \
7083       break;
7084 #include "clang/Basic/PPCTypes.def"
7085 #define RVV_TYPE(Name, Id, SingletonId) \
7086     case PREDEF_TYPE_##Id##_ID: \
7087       T = Context.SingletonId; \
7088       break;
7089 #include "clang/Basic/RISCVVTypes.def"
7090     }
7091 
7092     assert(!T.isNull() && "Unknown predefined type");
7093     return T.withFastQualifiers(FastQuals);
7094   }
7095 
7096   Index -= NUM_PREDEF_TYPE_IDS;
7097   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7098   if (TypesLoaded[Index].isNull()) {
7099     TypesLoaded[Index] = readTypeRecord(Index);
7100     if (TypesLoaded[Index].isNull())
7101       return QualType();
7102 
7103     TypesLoaded[Index]->setFromAST();
7104     if (DeserializationListener)
7105       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7106                                         TypesLoaded[Index]);
7107   }
7108 
7109   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7110 }
7111 
7112 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7113   return GetType(getGlobalTypeID(F, LocalID));
7114 }
7115 
7116 serialization::TypeID
7117 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7118   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7119   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7120 
7121   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7122     return LocalID;
7123 
7124   if (!F.ModuleOffsetMap.empty())
7125     ReadModuleOffsetMap(F);
7126 
7127   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7128     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7129   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7130 
7131   unsigned GlobalIndex = LocalIndex + I->second;
7132   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7133 }
7134 
7135 TemplateArgumentLocInfo
7136 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7137   switch (Kind) {
7138   case TemplateArgument::Expression:
7139     return readExpr();
7140   case TemplateArgument::Type:
7141     return readTypeSourceInfo();
7142   case TemplateArgument::Template: {
7143     NestedNameSpecifierLoc QualifierLoc =
7144       readNestedNameSpecifierLoc();
7145     SourceLocation TemplateNameLoc = readSourceLocation();
7146     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7147                                    TemplateNameLoc, SourceLocation());
7148   }
7149   case TemplateArgument::TemplateExpansion: {
7150     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7151     SourceLocation TemplateNameLoc = readSourceLocation();
7152     SourceLocation EllipsisLoc = readSourceLocation();
7153     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7154                                    TemplateNameLoc, EllipsisLoc);
7155   }
7156   case TemplateArgument::Null:
7157   case TemplateArgument::Integral:
7158   case TemplateArgument::Declaration:
7159   case TemplateArgument::NullPtr:
7160   case TemplateArgument::Pack:
7161     // FIXME: Is this right?
7162     return TemplateArgumentLocInfo();
7163   }
7164   llvm_unreachable("unexpected template argument loc");
7165 }
7166 
7167 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7168   TemplateArgument Arg = readTemplateArgument();
7169 
7170   if (Arg.getKind() == TemplateArgument::Expression) {
7171     if (readBool()) // bool InfoHasSameExpr.
7172       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7173   }
7174   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7175 }
7176 
7177 const ASTTemplateArgumentListInfo *
7178 ASTRecordReader::readASTTemplateArgumentListInfo() {
7179   SourceLocation LAngleLoc = readSourceLocation();
7180   SourceLocation RAngleLoc = readSourceLocation();
7181   unsigned NumArgsAsWritten = readInt();
7182   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7183   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7184     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7185   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7186 }
7187 
7188 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7189   return GetDecl(ID);
7190 }
7191 
7192 void ASTReader::CompleteRedeclChain(const Decl *D) {
7193   if (NumCurrentElementsDeserializing) {
7194     // We arrange to not care about the complete redeclaration chain while we're
7195     // deserializing. Just remember that the AST has marked this one as complete
7196     // but that it's not actually complete yet, so we know we still need to
7197     // complete it later.
7198     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7199     return;
7200   }
7201 
7202   if (!D->getDeclContext()) {
7203     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7204     return;
7205   }
7206 
7207   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7208 
7209   // If this is a named declaration, complete it by looking it up
7210   // within its context.
7211   //
7212   // FIXME: Merging a function definition should merge
7213   // all mergeable entities within it.
7214   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7215       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7216     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7217       if (!getContext().getLangOpts().CPlusPlus &&
7218           isa<TranslationUnitDecl>(DC)) {
7219         // Outside of C++, we don't have a lookup table for the TU, so update
7220         // the identifier instead. (For C++ modules, we don't store decls
7221         // in the serialized identifier table, so we do the lookup in the TU.)
7222         auto *II = Name.getAsIdentifierInfo();
7223         assert(II && "non-identifier name in C?");
7224         if (II->isOutOfDate())
7225           updateOutOfDateIdentifier(*II);
7226       } else
7227         DC->lookup(Name);
7228     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7229       // Find all declarations of this kind from the relevant context.
7230       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7231         auto *DC = cast<DeclContext>(DCDecl);
7232         SmallVector<Decl*, 8> Decls;
7233         FindExternalLexicalDecls(
7234             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7235       }
7236     }
7237   }
7238 
7239   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7240     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7241   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7242     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7243   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7244     if (auto *Template = FD->getPrimaryTemplate())
7245       Template->LoadLazySpecializations();
7246   }
7247 }
7248 
7249 CXXCtorInitializer **
7250 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7251   RecordLocation Loc = getLocalBitOffset(Offset);
7252   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7253   SavedStreamPosition SavedPosition(Cursor);
7254   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7255     Error(std::move(Err));
7256     return nullptr;
7257   }
7258   ReadingKindTracker ReadingKind(Read_Decl, *this);
7259 
7260   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7261   if (!MaybeCode) {
7262     Error(MaybeCode.takeError());
7263     return nullptr;
7264   }
7265   unsigned Code = MaybeCode.get();
7266 
7267   ASTRecordReader Record(*this, *Loc.F);
7268   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7269   if (!MaybeRecCode) {
7270     Error(MaybeRecCode.takeError());
7271     return nullptr;
7272   }
7273   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7274     Error("malformed AST file: missing C++ ctor initializers");
7275     return nullptr;
7276   }
7277 
7278   return Record.readCXXCtorInitializers();
7279 }
7280 
7281 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7282   assert(ContextObj && "reading base specifiers with no AST context");
7283   ASTContext &Context = *ContextObj;
7284 
7285   RecordLocation Loc = getLocalBitOffset(Offset);
7286   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7287   SavedStreamPosition SavedPosition(Cursor);
7288   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7289     Error(std::move(Err));
7290     return nullptr;
7291   }
7292   ReadingKindTracker ReadingKind(Read_Decl, *this);
7293 
7294   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7295   if (!MaybeCode) {
7296     Error(MaybeCode.takeError());
7297     return nullptr;
7298   }
7299   unsigned Code = MaybeCode.get();
7300 
7301   ASTRecordReader Record(*this, *Loc.F);
7302   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7303   if (!MaybeRecCode) {
7304     Error(MaybeCode.takeError());
7305     return nullptr;
7306   }
7307   unsigned RecCode = MaybeRecCode.get();
7308 
7309   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7310     Error("malformed AST file: missing C++ base specifiers");
7311     return nullptr;
7312   }
7313 
7314   unsigned NumBases = Record.readInt();
7315   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7316   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7317   for (unsigned I = 0; I != NumBases; ++I)
7318     Bases[I] = Record.readCXXBaseSpecifier();
7319   return Bases;
7320 }
7321 
7322 serialization::DeclID
7323 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7324   if (LocalID < NUM_PREDEF_DECL_IDS)
7325     return LocalID;
7326 
7327   if (!F.ModuleOffsetMap.empty())
7328     ReadModuleOffsetMap(F);
7329 
7330   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7331     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7332   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7333 
7334   return LocalID + I->second;
7335 }
7336 
7337 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7338                                    ModuleFile &M) const {
7339   // Predefined decls aren't from any module.
7340   if (ID < NUM_PREDEF_DECL_IDS)
7341     return false;
7342 
7343   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7344          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7345 }
7346 
7347 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7348   if (!D->isFromASTFile())
7349     return nullptr;
7350   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7351   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7352   return I->second;
7353 }
7354 
7355 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7356   if (ID < NUM_PREDEF_DECL_IDS)
7357     return SourceLocation();
7358 
7359   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7360 
7361   if (Index > DeclsLoaded.size()) {
7362     Error("declaration ID out-of-range for AST file");
7363     return SourceLocation();
7364   }
7365 
7366   if (Decl *D = DeclsLoaded[Index])
7367     return D->getLocation();
7368 
7369   SourceLocation Loc;
7370   DeclCursorForID(ID, Loc);
7371   return Loc;
7372 }
7373 
7374 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7375   switch (ID) {
7376   case PREDEF_DECL_NULL_ID:
7377     return nullptr;
7378 
7379   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7380     return Context.getTranslationUnitDecl();
7381 
7382   case PREDEF_DECL_OBJC_ID_ID:
7383     return Context.getObjCIdDecl();
7384 
7385   case PREDEF_DECL_OBJC_SEL_ID:
7386     return Context.getObjCSelDecl();
7387 
7388   case PREDEF_DECL_OBJC_CLASS_ID:
7389     return Context.getObjCClassDecl();
7390 
7391   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7392     return Context.getObjCProtocolDecl();
7393 
7394   case PREDEF_DECL_INT_128_ID:
7395     return Context.getInt128Decl();
7396 
7397   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7398     return Context.getUInt128Decl();
7399 
7400   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7401     return Context.getObjCInstanceTypeDecl();
7402 
7403   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7404     return Context.getBuiltinVaListDecl();
7405 
7406   case PREDEF_DECL_VA_LIST_TAG:
7407     return Context.getVaListTagDecl();
7408 
7409   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7410     return Context.getBuiltinMSVaListDecl();
7411 
7412   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7413     return Context.getMSGuidTagDecl();
7414 
7415   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7416     return Context.getExternCContextDecl();
7417 
7418   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7419     return Context.getMakeIntegerSeqDecl();
7420 
7421   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7422     return Context.getCFConstantStringDecl();
7423 
7424   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7425     return Context.getCFConstantStringTagDecl();
7426 
7427   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7428     return Context.getTypePackElementDecl();
7429   }
7430   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7431 }
7432 
7433 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7434   assert(ContextObj && "reading decl with no AST context");
7435   if (ID < NUM_PREDEF_DECL_IDS) {
7436     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7437     if (D) {
7438       // Track that we have merged the declaration with ID \p ID into the
7439       // pre-existing predefined declaration \p D.
7440       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7441       if (Merged.empty())
7442         Merged.push_back(ID);
7443     }
7444     return D;
7445   }
7446 
7447   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7448 
7449   if (Index >= DeclsLoaded.size()) {
7450     assert(0 && "declaration ID out-of-range for AST file");
7451     Error("declaration ID out-of-range for AST file");
7452     return nullptr;
7453   }
7454 
7455   return DeclsLoaded[Index];
7456 }
7457 
7458 Decl *ASTReader::GetDecl(DeclID ID) {
7459   if (ID < NUM_PREDEF_DECL_IDS)
7460     return GetExistingDecl(ID);
7461 
7462   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7463 
7464   if (Index >= DeclsLoaded.size()) {
7465     assert(0 && "declaration ID out-of-range for AST file");
7466     Error("declaration ID out-of-range for AST file");
7467     return nullptr;
7468   }
7469 
7470   if (!DeclsLoaded[Index]) {
7471     ReadDeclRecord(ID);
7472     if (DeserializationListener)
7473       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7474   }
7475 
7476   return DeclsLoaded[Index];
7477 }
7478 
7479 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7480                                                   DeclID GlobalID) {
7481   if (GlobalID < NUM_PREDEF_DECL_IDS)
7482     return GlobalID;
7483 
7484   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7485   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7486   ModuleFile *Owner = I->second;
7487 
7488   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7489     = M.GlobalToLocalDeclIDs.find(Owner);
7490   if (Pos == M.GlobalToLocalDeclIDs.end())
7491     return 0;
7492 
7493   return GlobalID - Owner->BaseDeclID + Pos->second;
7494 }
7495 
7496 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7497                                             const RecordData &Record,
7498                                             unsigned &Idx) {
7499   if (Idx >= Record.size()) {
7500     Error("Corrupted AST file");
7501     return 0;
7502   }
7503 
7504   return getGlobalDeclID(F, Record[Idx++]);
7505 }
7506 
7507 /// Resolve the offset of a statement into a statement.
7508 ///
7509 /// This operation will read a new statement from the external
7510 /// source each time it is called, and is meant to be used via a
7511 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7512 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7513   // Switch case IDs are per Decl.
7514   ClearSwitchCaseIDs();
7515 
7516   // Offset here is a global offset across the entire chain.
7517   RecordLocation Loc = getLocalBitOffset(Offset);
7518   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7519     Error(std::move(Err));
7520     return nullptr;
7521   }
7522   assert(NumCurrentElementsDeserializing == 0 &&
7523          "should not be called while already deserializing");
7524   Deserializing D(this);
7525   return ReadStmtFromStream(*Loc.F);
7526 }
7527 
7528 void ASTReader::FindExternalLexicalDecls(
7529     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7530     SmallVectorImpl<Decl *> &Decls) {
7531   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7532 
7533   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7534     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7535     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7536       auto K = (Decl::Kind)+LexicalDecls[I];
7537       if (!IsKindWeWant(K))
7538         continue;
7539 
7540       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7541 
7542       // Don't add predefined declarations to the lexical context more
7543       // than once.
7544       if (ID < NUM_PREDEF_DECL_IDS) {
7545         if (PredefsVisited[ID])
7546           continue;
7547 
7548         PredefsVisited[ID] = true;
7549       }
7550 
7551       if (Decl *D = GetLocalDecl(*M, ID)) {
7552         assert(D->getKind() == K && "wrong kind for lexical decl");
7553         if (!DC->isDeclInLexicalTraversal(D))
7554           Decls.push_back(D);
7555       }
7556     }
7557   };
7558 
7559   if (isa<TranslationUnitDecl>(DC)) {
7560     for (auto Lexical : TULexicalDecls)
7561       Visit(Lexical.first, Lexical.second);
7562   } else {
7563     auto I = LexicalDecls.find(DC);
7564     if (I != LexicalDecls.end())
7565       Visit(I->second.first, I->second.second);
7566   }
7567 
7568   ++NumLexicalDeclContextsRead;
7569 }
7570 
7571 namespace {
7572 
7573 class DeclIDComp {
7574   ASTReader &Reader;
7575   ModuleFile &Mod;
7576 
7577 public:
7578   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7579 
7580   bool operator()(LocalDeclID L, LocalDeclID R) const {
7581     SourceLocation LHS = getLocation(L);
7582     SourceLocation RHS = getLocation(R);
7583     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7584   }
7585 
7586   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7587     SourceLocation RHS = getLocation(R);
7588     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7589   }
7590 
7591   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7592     SourceLocation LHS = getLocation(L);
7593     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7594   }
7595 
7596   SourceLocation getLocation(LocalDeclID ID) const {
7597     return Reader.getSourceManager().getFileLoc(
7598             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7599   }
7600 };
7601 
7602 } // namespace
7603 
7604 void ASTReader::FindFileRegionDecls(FileID File,
7605                                     unsigned Offset, unsigned Length,
7606                                     SmallVectorImpl<Decl *> &Decls) {
7607   SourceManager &SM = getSourceManager();
7608 
7609   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7610   if (I == FileDeclIDs.end())
7611     return;
7612 
7613   FileDeclsInfo &DInfo = I->second;
7614   if (DInfo.Decls.empty())
7615     return;
7616 
7617   SourceLocation
7618     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7619   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7620 
7621   DeclIDComp DIDComp(*this, *DInfo.Mod);
7622   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7623       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7624   if (BeginIt != DInfo.Decls.begin())
7625     --BeginIt;
7626 
7627   // If we are pointing at a top-level decl inside an objc container, we need
7628   // to backtrack until we find it otherwise we will fail to report that the
7629   // region overlaps with an objc container.
7630   while (BeginIt != DInfo.Decls.begin() &&
7631          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7632              ->isTopLevelDeclInObjCContainer())
7633     --BeginIt;
7634 
7635   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7636       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7637   if (EndIt != DInfo.Decls.end())
7638     ++EndIt;
7639 
7640   for (ArrayRef<serialization::LocalDeclID>::iterator
7641          DIt = BeginIt; DIt != EndIt; ++DIt)
7642     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7643 }
7644 
7645 bool
7646 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7647                                           DeclarationName Name) {
7648   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7649          "DeclContext has no visible decls in storage");
7650   if (!Name)
7651     return false;
7652 
7653   auto It = Lookups.find(DC);
7654   if (It == Lookups.end())
7655     return false;
7656 
7657   Deserializing LookupResults(this);
7658 
7659   // Load the list of declarations.
7660   SmallVector<NamedDecl *, 64> Decls;
7661   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7662   for (DeclID ID : It->second.Table.find(Name)) {
7663     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7664     if (ND->getDeclName() == Name && Found.insert(ND).second)
7665       Decls.push_back(ND);
7666   }
7667 
7668   ++NumVisibleDeclContextsRead;
7669   SetExternalVisibleDeclsForName(DC, Name, Decls);
7670   return !Decls.empty();
7671 }
7672 
7673 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7674   if (!DC->hasExternalVisibleStorage())
7675     return;
7676 
7677   auto It = Lookups.find(DC);
7678   assert(It != Lookups.end() &&
7679          "have external visible storage but no lookup tables");
7680 
7681   DeclsMap Decls;
7682 
7683   for (DeclID ID : It->second.Table.findAll()) {
7684     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7685     Decls[ND->getDeclName()].push_back(ND);
7686   }
7687 
7688   ++NumVisibleDeclContextsRead;
7689 
7690   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7691     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7692   }
7693   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7694 }
7695 
7696 const serialization::reader::DeclContextLookupTable *
7697 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7698   auto I = Lookups.find(Primary);
7699   return I == Lookups.end() ? nullptr : &I->second;
7700 }
7701 
7702 /// Under non-PCH compilation the consumer receives the objc methods
7703 /// before receiving the implementation, and codegen depends on this.
7704 /// We simulate this by deserializing and passing to consumer the methods of the
7705 /// implementation before passing the deserialized implementation decl.
7706 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7707                                        ASTConsumer *Consumer) {
7708   assert(ImplD && Consumer);
7709 
7710   for (auto *I : ImplD->methods())
7711     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7712 
7713   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7714 }
7715 
7716 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7717   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7718     PassObjCImplDeclToConsumer(ImplD, Consumer);
7719   else
7720     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7721 }
7722 
7723 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7724   this->Consumer = Consumer;
7725 
7726   if (Consumer)
7727     PassInterestingDeclsToConsumer();
7728 
7729   if (DeserializationListener)
7730     DeserializationListener->ReaderInitialized(this);
7731 }
7732 
7733 void ASTReader::PrintStats() {
7734   std::fprintf(stderr, "*** AST File Statistics:\n");
7735 
7736   unsigned NumTypesLoaded =
7737       TypesLoaded.size() - llvm::count(TypesLoaded, QualType());
7738   unsigned NumDeclsLoaded =
7739       DeclsLoaded.size() - llvm::count(DeclsLoaded, (Decl *)nullptr);
7740   unsigned NumIdentifiersLoaded =
7741       IdentifiersLoaded.size() -
7742       llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);
7743   unsigned NumMacrosLoaded =
7744       MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr);
7745   unsigned NumSelectorsLoaded =
7746       SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector());
7747 
7748   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7749     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7750                  NumSLocEntriesRead, TotalNumSLocEntries,
7751                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7752   if (!TypesLoaded.empty())
7753     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7754                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7755                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7756   if (!DeclsLoaded.empty())
7757     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7758                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7759                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7760   if (!IdentifiersLoaded.empty())
7761     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7762                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7763                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7764   if (!MacrosLoaded.empty())
7765     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7766                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7767                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7768   if (!SelectorsLoaded.empty())
7769     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7770                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7771                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7772   if (TotalNumStatements)
7773     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7774                  NumStatementsRead, TotalNumStatements,
7775                  ((float)NumStatementsRead/TotalNumStatements * 100));
7776   if (TotalNumMacros)
7777     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7778                  NumMacrosRead, TotalNumMacros,
7779                  ((float)NumMacrosRead/TotalNumMacros * 100));
7780   if (TotalLexicalDeclContexts)
7781     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7782                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7783                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7784                   * 100));
7785   if (TotalVisibleDeclContexts)
7786     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7787                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7788                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7789                   * 100));
7790   if (TotalNumMethodPoolEntries)
7791     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7792                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7793                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7794                   * 100));
7795   if (NumMethodPoolLookups)
7796     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7797                  NumMethodPoolHits, NumMethodPoolLookups,
7798                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7799   if (NumMethodPoolTableLookups)
7800     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7801                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7802                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7803                   * 100.0));
7804   if (NumIdentifierLookupHits)
7805     std::fprintf(stderr,
7806                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7807                  NumIdentifierLookupHits, NumIdentifierLookups,
7808                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7809 
7810   if (GlobalIndex) {
7811     std::fprintf(stderr, "\n");
7812     GlobalIndex->printStats();
7813   }
7814 
7815   std::fprintf(stderr, "\n");
7816   dump();
7817   std::fprintf(stderr, "\n");
7818 }
7819 
7820 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7821 LLVM_DUMP_METHOD static void
7822 dumpModuleIDMap(StringRef Name,
7823                 const ContinuousRangeMap<Key, ModuleFile *,
7824                                          InitialCapacity> &Map) {
7825   if (Map.begin() == Map.end())
7826     return;
7827 
7828   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7829 
7830   llvm::errs() << Name << ":\n";
7831   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7832        I != IEnd; ++I) {
7833     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7834       << "\n";
7835   }
7836 }
7837 
7838 LLVM_DUMP_METHOD void ASTReader::dump() {
7839   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7840   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7841   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7842   dumpModuleIDMap("Global type map", GlobalTypeMap);
7843   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7844   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7845   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7846   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7847   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7848   dumpModuleIDMap("Global preprocessed entity map",
7849                   GlobalPreprocessedEntityMap);
7850 
7851   llvm::errs() << "\n*** PCH/Modules Loaded:";
7852   for (ModuleFile &M : ModuleMgr)
7853     M.dump();
7854 }
7855 
7856 /// Return the amount of memory used by memory buffers, breaking down
7857 /// by heap-backed versus mmap'ed memory.
7858 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7859   for (ModuleFile &I : ModuleMgr) {
7860     if (llvm::MemoryBuffer *buf = I.Buffer) {
7861       size_t bytes = buf->getBufferSize();
7862       switch (buf->getBufferKind()) {
7863         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7864           sizes.malloc_bytes += bytes;
7865           break;
7866         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7867           sizes.mmap_bytes += bytes;
7868           break;
7869       }
7870     }
7871   }
7872 }
7873 
7874 void ASTReader::InitializeSema(Sema &S) {
7875   SemaObj = &S;
7876   S.addExternalSource(this);
7877 
7878   // Makes sure any declarations that were deserialized "too early"
7879   // still get added to the identifier's declaration chains.
7880   for (uint64_t ID : PreloadedDeclIDs) {
7881     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7882     pushExternalDeclIntoScope(D, D->getDeclName());
7883   }
7884   PreloadedDeclIDs.clear();
7885 
7886   // FIXME: What happens if these are changed by a module import?
7887   if (!FPPragmaOptions.empty()) {
7888     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7889     FPOptionsOverride NewOverrides =
7890         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7891     SemaObj->CurFPFeatures =
7892         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7893   }
7894 
7895   SemaObj->OpenCLFeatures = OpenCLExtensions;
7896 
7897   UpdateSema();
7898 }
7899 
7900 void ASTReader::UpdateSema() {
7901   assert(SemaObj && "no Sema to update");
7902 
7903   // Load the offsets of the declarations that Sema references.
7904   // They will be lazily deserialized when needed.
7905   if (!SemaDeclRefs.empty()) {
7906     assert(SemaDeclRefs.size() % 3 == 0);
7907     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7908       if (!SemaObj->StdNamespace)
7909         SemaObj->StdNamespace = SemaDeclRefs[I];
7910       if (!SemaObj->StdBadAlloc)
7911         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7912       if (!SemaObj->StdAlignValT)
7913         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7914     }
7915     SemaDeclRefs.clear();
7916   }
7917 
7918   // Update the state of pragmas. Use the same API as if we had encountered the
7919   // pragma in the source.
7920   if(OptimizeOffPragmaLocation.isValid())
7921     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7922   if (PragmaMSStructState != -1)
7923     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7924   if (PointersToMembersPragmaLocation.isValid()) {
7925     SemaObj->ActOnPragmaMSPointersToMembers(
7926         (LangOptions::PragmaMSPointersToMembersKind)
7927             PragmaMSPointersToMembersState,
7928         PointersToMembersPragmaLocation);
7929   }
7930   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7931 
7932   if (PragmaAlignPackCurrentValue) {
7933     // The bottom of the stack might have a default value. It must be adjusted
7934     // to the current value to ensure that the packing state is preserved after
7935     // popping entries that were included/imported from a PCH/module.
7936     bool DropFirst = false;
7937     if (!PragmaAlignPackStack.empty() &&
7938         PragmaAlignPackStack.front().Location.isInvalid()) {
7939       assert(PragmaAlignPackStack.front().Value ==
7940                  SemaObj->AlignPackStack.DefaultValue &&
7941              "Expected a default alignment value");
7942       SemaObj->AlignPackStack.Stack.emplace_back(
7943           PragmaAlignPackStack.front().SlotLabel,
7944           SemaObj->AlignPackStack.CurrentValue,
7945           SemaObj->AlignPackStack.CurrentPragmaLocation,
7946           PragmaAlignPackStack.front().PushLocation);
7947       DropFirst = true;
7948     }
7949     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7950                                  .drop_front(DropFirst ? 1 : 0)) {
7951       SemaObj->AlignPackStack.Stack.emplace_back(
7952           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7953     }
7954     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7955       assert(*PragmaAlignPackCurrentValue ==
7956                  SemaObj->AlignPackStack.DefaultValue &&
7957              "Expected a default align and pack value");
7958       // Keep the current values.
7959     } else {
7960       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7961       SemaObj->AlignPackStack.CurrentPragmaLocation =
7962           PragmaAlignPackCurrentLocation;
7963     }
7964   }
7965   if (FpPragmaCurrentValue) {
7966     // The bottom of the stack might have a default value. It must be adjusted
7967     // to the current value to ensure that fp-pragma state is preserved after
7968     // popping entries that were included/imported from a PCH/module.
7969     bool DropFirst = false;
7970     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7971       assert(FpPragmaStack.front().Value ==
7972                  SemaObj->FpPragmaStack.DefaultValue &&
7973              "Expected a default pragma float_control value");
7974       SemaObj->FpPragmaStack.Stack.emplace_back(
7975           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7976           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7977           FpPragmaStack.front().PushLocation);
7978       DropFirst = true;
7979     }
7980     for (const auto &Entry :
7981          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7982       SemaObj->FpPragmaStack.Stack.emplace_back(
7983           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7984     if (FpPragmaCurrentLocation.isInvalid()) {
7985       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7986              "Expected a default pragma float_control value");
7987       // Keep the current values.
7988     } else {
7989       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7990       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7991     }
7992   }
7993 
7994   // For non-modular AST files, restore visiblity of modules.
7995   for (auto &Import : ImportedModules) {
7996     if (Import.ImportLoc.isInvalid())
7997       continue;
7998     if (Module *Imported = getSubmodule(Import.ID)) {
7999       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
8000     }
8001   }
8002 }
8003 
8004 IdentifierInfo *ASTReader::get(StringRef Name) {
8005   // Note that we are loading an identifier.
8006   Deserializing AnIdentifier(this);
8007 
8008   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8009                                   NumIdentifierLookups,
8010                                   NumIdentifierLookupHits);
8011 
8012   // We don't need to do identifier table lookups in C++ modules (we preload
8013   // all interesting declarations, and don't need to use the scope for name
8014   // lookups). Perform the lookup in PCH files, though, since we don't build
8015   // a complete initial identifier table if we're carrying on from a PCH.
8016   if (PP.getLangOpts().CPlusPlus) {
8017     for (auto F : ModuleMgr.pch_modules())
8018       if (Visitor(*F))
8019         break;
8020   } else {
8021     // If there is a global index, look there first to determine which modules
8022     // provably do not have any results for this identifier.
8023     GlobalModuleIndex::HitSet Hits;
8024     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8025     if (!loadGlobalIndex()) {
8026       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8027         HitsPtr = &Hits;
8028       }
8029     }
8030 
8031     ModuleMgr.visit(Visitor, HitsPtr);
8032   }
8033 
8034   IdentifierInfo *II = Visitor.getIdentifierInfo();
8035   markIdentifierUpToDate(II);
8036   return II;
8037 }
8038 
8039 namespace clang {
8040 
8041   /// An identifier-lookup iterator that enumerates all of the
8042   /// identifiers stored within a set of AST files.
8043   class ASTIdentifierIterator : public IdentifierIterator {
8044     /// The AST reader whose identifiers are being enumerated.
8045     const ASTReader &Reader;
8046 
8047     /// The current index into the chain of AST files stored in
8048     /// the AST reader.
8049     unsigned Index;
8050 
8051     /// The current position within the identifier lookup table
8052     /// of the current AST file.
8053     ASTIdentifierLookupTable::key_iterator Current;
8054 
8055     /// The end position within the identifier lookup table of
8056     /// the current AST file.
8057     ASTIdentifierLookupTable::key_iterator End;
8058 
8059     /// Whether to skip any modules in the ASTReader.
8060     bool SkipModules;
8061 
8062   public:
8063     explicit ASTIdentifierIterator(const ASTReader &Reader,
8064                                    bool SkipModules = false);
8065 
8066     StringRef Next() override;
8067   };
8068 
8069 } // namespace clang
8070 
8071 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8072                                              bool SkipModules)
8073     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8074 }
8075 
8076 StringRef ASTIdentifierIterator::Next() {
8077   while (Current == End) {
8078     // If we have exhausted all of our AST files, we're done.
8079     if (Index == 0)
8080       return StringRef();
8081 
8082     --Index;
8083     ModuleFile &F = Reader.ModuleMgr[Index];
8084     if (SkipModules && F.isModule())
8085       continue;
8086 
8087     ASTIdentifierLookupTable *IdTable =
8088         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8089     Current = IdTable->key_begin();
8090     End = IdTable->key_end();
8091   }
8092 
8093   // We have any identifiers remaining in the current AST file; return
8094   // the next one.
8095   StringRef Result = *Current;
8096   ++Current;
8097   return Result;
8098 }
8099 
8100 namespace {
8101 
8102 /// A utility for appending two IdentifierIterators.
8103 class ChainedIdentifierIterator : public IdentifierIterator {
8104   std::unique_ptr<IdentifierIterator> Current;
8105   std::unique_ptr<IdentifierIterator> Queued;
8106 
8107 public:
8108   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8109                             std::unique_ptr<IdentifierIterator> Second)
8110       : Current(std::move(First)), Queued(std::move(Second)) {}
8111 
8112   StringRef Next() override {
8113     if (!Current)
8114       return StringRef();
8115 
8116     StringRef result = Current->Next();
8117     if (!result.empty())
8118       return result;
8119 
8120     // Try the queued iterator, which may itself be empty.
8121     Current.reset();
8122     std::swap(Current, Queued);
8123     return Next();
8124   }
8125 };
8126 
8127 } // namespace
8128 
8129 IdentifierIterator *ASTReader::getIdentifiers() {
8130   if (!loadGlobalIndex()) {
8131     std::unique_ptr<IdentifierIterator> ReaderIter(
8132         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8133     std::unique_ptr<IdentifierIterator> ModulesIter(
8134         GlobalIndex->createIdentifierIterator());
8135     return new ChainedIdentifierIterator(std::move(ReaderIter),
8136                                          std::move(ModulesIter));
8137   }
8138 
8139   return new ASTIdentifierIterator(*this);
8140 }
8141 
8142 namespace clang {
8143 namespace serialization {
8144 
8145   class ReadMethodPoolVisitor {
8146     ASTReader &Reader;
8147     Selector Sel;
8148     unsigned PriorGeneration;
8149     unsigned InstanceBits = 0;
8150     unsigned FactoryBits = 0;
8151     bool InstanceHasMoreThanOneDecl = false;
8152     bool FactoryHasMoreThanOneDecl = false;
8153     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8154     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8155 
8156   public:
8157     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8158                           unsigned PriorGeneration)
8159         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8160 
8161     bool operator()(ModuleFile &M) {
8162       if (!M.SelectorLookupTable)
8163         return false;
8164 
8165       // If we've already searched this module file, skip it now.
8166       if (M.Generation <= PriorGeneration)
8167         return true;
8168 
8169       ++Reader.NumMethodPoolTableLookups;
8170       ASTSelectorLookupTable *PoolTable
8171         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8172       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8173       if (Pos == PoolTable->end())
8174         return false;
8175 
8176       ++Reader.NumMethodPoolTableHits;
8177       ++Reader.NumSelectorsRead;
8178       // FIXME: Not quite happy with the statistics here. We probably should
8179       // disable this tracking when called via LoadSelector.
8180       // Also, should entries without methods count as misses?
8181       ++Reader.NumMethodPoolEntriesRead;
8182       ASTSelectorLookupTrait::data_type Data = *Pos;
8183       if (Reader.DeserializationListener)
8184         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8185 
8186       // Append methods in the reverse order, so that later we can process them
8187       // in the order they appear in the source code by iterating through
8188       // the vector in the reverse order.
8189       InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend());
8190       FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend());
8191       InstanceBits = Data.InstanceBits;
8192       FactoryBits = Data.FactoryBits;
8193       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8194       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8195       return false;
8196     }
8197 
8198     /// Retrieve the instance methods found by this visitor.
8199     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8200       return InstanceMethods;
8201     }
8202 
8203     /// Retrieve the instance methods found by this visitor.
8204     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8205       return FactoryMethods;
8206     }
8207 
8208     unsigned getInstanceBits() const { return InstanceBits; }
8209     unsigned getFactoryBits() const { return FactoryBits; }
8210 
8211     bool instanceHasMoreThanOneDecl() const {
8212       return InstanceHasMoreThanOneDecl;
8213     }
8214 
8215     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8216   };
8217 
8218 } // namespace serialization
8219 } // namespace clang
8220 
8221 /// Add the given set of methods to the method list.
8222 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8223                              ObjCMethodList &List) {
8224   for (auto I = Methods.rbegin(), E = Methods.rend(); I != E; ++I)
8225     S.addMethodToGlobalList(&List, *I);
8226 }
8227 
8228 void ASTReader::ReadMethodPool(Selector Sel) {
8229   // Get the selector generation and update it to the current generation.
8230   unsigned &Generation = SelectorGeneration[Sel];
8231   unsigned PriorGeneration = Generation;
8232   Generation = getGeneration();
8233   SelectorOutOfDate[Sel] = false;
8234 
8235   // Search for methods defined with this selector.
8236   ++NumMethodPoolLookups;
8237   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8238   ModuleMgr.visit(Visitor);
8239 
8240   if (Visitor.getInstanceMethods().empty() &&
8241       Visitor.getFactoryMethods().empty())
8242     return;
8243 
8244   ++NumMethodPoolHits;
8245 
8246   if (!getSema())
8247     return;
8248 
8249   Sema &S = *getSema();
8250   Sema::GlobalMethodPool::iterator Pos =
8251       S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists()))
8252           .first;
8253 
8254   Pos->second.first.setBits(Visitor.getInstanceBits());
8255   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8256   Pos->second.second.setBits(Visitor.getFactoryBits());
8257   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8258 
8259   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8260   // when building a module we keep every method individually and may need to
8261   // update hasMoreThanOneDecl as we add the methods.
8262   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8263   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8264 }
8265 
8266 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8267   if (SelectorOutOfDate[Sel])
8268     ReadMethodPool(Sel);
8269 }
8270 
8271 void ASTReader::ReadKnownNamespaces(
8272                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8273   Namespaces.clear();
8274 
8275   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8276     if (NamespaceDecl *Namespace
8277                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8278       Namespaces.push_back(Namespace);
8279   }
8280 }
8281 
8282 void ASTReader::ReadUndefinedButUsed(
8283     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8284   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8285     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8286     SourceLocation Loc =
8287         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8288     Undefined.insert(std::make_pair(D, Loc));
8289   }
8290 }
8291 
8292 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8293     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8294                                                      Exprs) {
8295   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8296     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8297     uint64_t Count = DelayedDeleteExprs[Idx++];
8298     for (uint64_t C = 0; C < Count; ++C) {
8299       SourceLocation DeleteLoc =
8300           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8301       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8302       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8303     }
8304   }
8305 }
8306 
8307 void ASTReader::ReadTentativeDefinitions(
8308                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8309   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8310     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8311     if (Var)
8312       TentativeDefs.push_back(Var);
8313   }
8314   TentativeDefinitions.clear();
8315 }
8316 
8317 void ASTReader::ReadUnusedFileScopedDecls(
8318                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8319   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8320     DeclaratorDecl *D
8321       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8322     if (D)
8323       Decls.push_back(D);
8324   }
8325   UnusedFileScopedDecls.clear();
8326 }
8327 
8328 void ASTReader::ReadDelegatingConstructors(
8329                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8330   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8331     CXXConstructorDecl *D
8332       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8333     if (D)
8334       Decls.push_back(D);
8335   }
8336   DelegatingCtorDecls.clear();
8337 }
8338 
8339 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8340   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8341     TypedefNameDecl *D
8342       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8343     if (D)
8344       Decls.push_back(D);
8345   }
8346   ExtVectorDecls.clear();
8347 }
8348 
8349 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8350     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8351   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8352        ++I) {
8353     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8354         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8355     if (D)
8356       Decls.insert(D);
8357   }
8358   UnusedLocalTypedefNameCandidates.clear();
8359 }
8360 
8361 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8362     llvm::SmallSetVector<Decl *, 4> &Decls) {
8363   for (auto I : DeclsToCheckForDeferredDiags) {
8364     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8365     if (D)
8366       Decls.insert(D);
8367   }
8368   DeclsToCheckForDeferredDiags.clear();
8369 }
8370 
8371 void ASTReader::ReadReferencedSelectors(
8372        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8373   if (ReferencedSelectorsData.empty())
8374     return;
8375 
8376   // If there are @selector references added them to its pool. This is for
8377   // implementation of -Wselector.
8378   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8379   unsigned I = 0;
8380   while (I < DataSize) {
8381     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8382     SourceLocation SelLoc
8383       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8384     Sels.push_back(std::make_pair(Sel, SelLoc));
8385   }
8386   ReferencedSelectorsData.clear();
8387 }
8388 
8389 void ASTReader::ReadWeakUndeclaredIdentifiers(
8390        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8391   if (WeakUndeclaredIdentifiers.empty())
8392     return;
8393 
8394   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8395     IdentifierInfo *WeakId
8396       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8397     IdentifierInfo *AliasId
8398       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8399     SourceLocation Loc
8400       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8401     bool Used = WeakUndeclaredIdentifiers[I++];
8402     WeakInfo WI(AliasId, Loc);
8403     WI.setUsed(Used);
8404     WeakIDs.push_back(std::make_pair(WeakId, WI));
8405   }
8406   WeakUndeclaredIdentifiers.clear();
8407 }
8408 
8409 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8410   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8411     ExternalVTableUse VT;
8412     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8413     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8414     VT.DefinitionRequired = VTableUses[Idx++];
8415     VTables.push_back(VT);
8416   }
8417 
8418   VTableUses.clear();
8419 }
8420 
8421 void ASTReader::ReadPendingInstantiations(
8422        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8423   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8424     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8425     SourceLocation Loc
8426       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8427 
8428     Pending.push_back(std::make_pair(D, Loc));
8429   }
8430   PendingInstantiations.clear();
8431 }
8432 
8433 void ASTReader::ReadLateParsedTemplates(
8434     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8435         &LPTMap) {
8436   for (auto &LPT : LateParsedTemplates) {
8437     ModuleFile *FMod = LPT.first;
8438     RecordDataImpl &LateParsed = LPT.second;
8439     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8440          /* In loop */) {
8441       FunctionDecl *FD =
8442           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8443 
8444       auto LT = std::make_unique<LateParsedTemplate>();
8445       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8446 
8447       ModuleFile *F = getOwningModuleFile(LT->D);
8448       assert(F && "No module");
8449 
8450       unsigned TokN = LateParsed[Idx++];
8451       LT->Toks.reserve(TokN);
8452       for (unsigned T = 0; T < TokN; ++T)
8453         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8454 
8455       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8456     }
8457   }
8458 
8459   LateParsedTemplates.clear();
8460 }
8461 
8462 void ASTReader::LoadSelector(Selector Sel) {
8463   // It would be complicated to avoid reading the methods anyway. So don't.
8464   ReadMethodPool(Sel);
8465 }
8466 
8467 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8468   assert(ID && "Non-zero identifier ID required");
8469   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8470   IdentifiersLoaded[ID - 1] = II;
8471   if (DeserializationListener)
8472     DeserializationListener->IdentifierRead(ID, II);
8473 }
8474 
8475 /// Set the globally-visible declarations associated with the given
8476 /// identifier.
8477 ///
8478 /// If the AST reader is currently in a state where the given declaration IDs
8479 /// cannot safely be resolved, they are queued until it is safe to resolve
8480 /// them.
8481 ///
8482 /// \param II an IdentifierInfo that refers to one or more globally-visible
8483 /// declarations.
8484 ///
8485 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8486 /// visible at global scope.
8487 ///
8488 /// \param Decls if non-null, this vector will be populated with the set of
8489 /// deserialized declarations. These declarations will not be pushed into
8490 /// scope.
8491 void
8492 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8493                               const SmallVectorImpl<uint32_t> &DeclIDs,
8494                                    SmallVectorImpl<Decl *> *Decls) {
8495   if (NumCurrentElementsDeserializing && !Decls) {
8496     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8497     return;
8498   }
8499 
8500   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8501     if (!SemaObj) {
8502       // Queue this declaration so that it will be added to the
8503       // translation unit scope and identifier's declaration chain
8504       // once a Sema object is known.
8505       PreloadedDeclIDs.push_back(DeclIDs[I]);
8506       continue;
8507     }
8508 
8509     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8510 
8511     // If we're simply supposed to record the declarations, do so now.
8512     if (Decls) {
8513       Decls->push_back(D);
8514       continue;
8515     }
8516 
8517     // Introduce this declaration into the translation-unit scope
8518     // and add it to the declaration chain for this identifier, so
8519     // that (unqualified) name lookup will find it.
8520     pushExternalDeclIntoScope(D, II);
8521   }
8522 }
8523 
8524 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8525   if (ID == 0)
8526     return nullptr;
8527 
8528   if (IdentifiersLoaded.empty()) {
8529     Error("no identifier table in AST file");
8530     return nullptr;
8531   }
8532 
8533   ID -= 1;
8534   if (!IdentifiersLoaded[ID]) {
8535     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8536     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8537     ModuleFile *M = I->second;
8538     unsigned Index = ID - M->BaseIdentifierID;
8539     const unsigned char *Data =
8540         M->IdentifierTableData + M->IdentifierOffsets[Index];
8541 
8542     ASTIdentifierLookupTrait Trait(*this, *M);
8543     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8544     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8545     auto &II = PP.getIdentifierTable().get(Key);
8546     IdentifiersLoaded[ID] = &II;
8547     markIdentifierFromAST(*this,  II);
8548     if (DeserializationListener)
8549       DeserializationListener->IdentifierRead(ID + 1, &II);
8550   }
8551 
8552   return IdentifiersLoaded[ID];
8553 }
8554 
8555 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8556   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8557 }
8558 
8559 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8560   if (LocalID < NUM_PREDEF_IDENT_IDS)
8561     return LocalID;
8562 
8563   if (!M.ModuleOffsetMap.empty())
8564     ReadModuleOffsetMap(M);
8565 
8566   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8567     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8568   assert(I != M.IdentifierRemap.end()
8569          && "Invalid index into identifier index remap");
8570 
8571   return LocalID + I->second;
8572 }
8573 
8574 MacroInfo *ASTReader::getMacro(MacroID ID) {
8575   if (ID == 0)
8576     return nullptr;
8577 
8578   if (MacrosLoaded.empty()) {
8579     Error("no macro table in AST file");
8580     return nullptr;
8581   }
8582 
8583   ID -= NUM_PREDEF_MACRO_IDS;
8584   if (!MacrosLoaded[ID]) {
8585     GlobalMacroMapType::iterator I
8586       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8587     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8588     ModuleFile *M = I->second;
8589     unsigned Index = ID - M->BaseMacroID;
8590     MacrosLoaded[ID] =
8591         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8592 
8593     if (DeserializationListener)
8594       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8595                                          MacrosLoaded[ID]);
8596   }
8597 
8598   return MacrosLoaded[ID];
8599 }
8600 
8601 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8602   if (LocalID < NUM_PREDEF_MACRO_IDS)
8603     return LocalID;
8604 
8605   if (!M.ModuleOffsetMap.empty())
8606     ReadModuleOffsetMap(M);
8607 
8608   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8609     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8610   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8611 
8612   return LocalID + I->second;
8613 }
8614 
8615 serialization::SubmoduleID
8616 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8617   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8618     return LocalID;
8619 
8620   if (!M.ModuleOffsetMap.empty())
8621     ReadModuleOffsetMap(M);
8622 
8623   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8624     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8625   assert(I != M.SubmoduleRemap.end()
8626          && "Invalid index into submodule index remap");
8627 
8628   return LocalID + I->second;
8629 }
8630 
8631 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8632   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8633     assert(GlobalID == 0 && "Unhandled global submodule ID");
8634     return nullptr;
8635   }
8636 
8637   if (GlobalID > SubmodulesLoaded.size()) {
8638     Error("submodule ID out of range in AST file");
8639     return nullptr;
8640   }
8641 
8642   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8643 }
8644 
8645 Module *ASTReader::getModule(unsigned ID) {
8646   return getSubmodule(ID);
8647 }
8648 
8649 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8650   if (ID & 1) {
8651     // It's a module, look it up by submodule ID.
8652     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8653     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8654   } else {
8655     // It's a prefix (preamble, PCH, ...). Look it up by index.
8656     unsigned IndexFromEnd = ID >> 1;
8657     assert(IndexFromEnd && "got reference to unknown module file");
8658     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8659   }
8660 }
8661 
8662 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8663   if (!F)
8664     return 1;
8665 
8666   // For a file representing a module, use the submodule ID of the top-level
8667   // module as the file ID. For any other kind of file, the number of such
8668   // files loaded beforehand will be the same on reload.
8669   // FIXME: Is this true even if we have an explicit module file and a PCH?
8670   if (F->isModule())
8671     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8672 
8673   auto PCHModules = getModuleManager().pch_modules();
8674   auto I = llvm::find(PCHModules, F);
8675   assert(I != PCHModules.end() && "emitting reference to unknown file");
8676   return (I - PCHModules.end()) << 1;
8677 }
8678 
8679 llvm::Optional<ASTSourceDescriptor>
8680 ASTReader::getSourceDescriptor(unsigned ID) {
8681   if (Module *M = getSubmodule(ID))
8682     return ASTSourceDescriptor(*M);
8683 
8684   // If there is only a single PCH, return it instead.
8685   // Chained PCH are not supported.
8686   const auto &PCHChain = ModuleMgr.pch_modules();
8687   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8688     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8689     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8690     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8691     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8692                                MF.Signature);
8693   }
8694   return None;
8695 }
8696 
8697 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8698   auto I = DefinitionSource.find(FD);
8699   if (I == DefinitionSource.end())
8700     return EK_ReplyHazy;
8701   return I->second ? EK_Never : EK_Always;
8702 }
8703 
8704 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8705   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8706 }
8707 
8708 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8709   if (ID == 0)
8710     return Selector();
8711 
8712   if (ID > SelectorsLoaded.size()) {
8713     Error("selector ID out of range in AST file");
8714     return Selector();
8715   }
8716 
8717   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8718     // Load this selector from the selector table.
8719     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8720     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8721     ModuleFile &M = *I->second;
8722     ASTSelectorLookupTrait Trait(*this, M);
8723     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8724     SelectorsLoaded[ID - 1] =
8725       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8726     if (DeserializationListener)
8727       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8728   }
8729 
8730   return SelectorsLoaded[ID - 1];
8731 }
8732 
8733 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8734   return DecodeSelector(ID);
8735 }
8736 
8737 uint32_t ASTReader::GetNumExternalSelectors() {
8738   // ID 0 (the null selector) is considered an external selector.
8739   return getTotalNumSelectors() + 1;
8740 }
8741 
8742 serialization::SelectorID
8743 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8744   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8745     return LocalID;
8746 
8747   if (!M.ModuleOffsetMap.empty())
8748     ReadModuleOffsetMap(M);
8749 
8750   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8751     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8752   assert(I != M.SelectorRemap.end()
8753          && "Invalid index into selector index remap");
8754 
8755   return LocalID + I->second;
8756 }
8757 
8758 DeclarationNameLoc
8759 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8760   switch (Name.getNameKind()) {
8761   case DeclarationName::CXXConstructorName:
8762   case DeclarationName::CXXDestructorName:
8763   case DeclarationName::CXXConversionFunctionName:
8764     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8765 
8766   case DeclarationName::CXXOperatorName:
8767     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8768 
8769   case DeclarationName::CXXLiteralOperatorName:
8770     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8771         readSourceLocation());
8772 
8773   case DeclarationName::Identifier:
8774   case DeclarationName::ObjCZeroArgSelector:
8775   case DeclarationName::ObjCOneArgSelector:
8776   case DeclarationName::ObjCMultiArgSelector:
8777   case DeclarationName::CXXUsingDirective:
8778   case DeclarationName::CXXDeductionGuideName:
8779     break;
8780   }
8781   return DeclarationNameLoc();
8782 }
8783 
8784 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8785   DeclarationNameInfo NameInfo;
8786   NameInfo.setName(readDeclarationName());
8787   NameInfo.setLoc(readSourceLocation());
8788   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8789   return NameInfo;
8790 }
8791 
8792 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8793   Info.QualifierLoc = readNestedNameSpecifierLoc();
8794   unsigned NumTPLists = readInt();
8795   Info.NumTemplParamLists = NumTPLists;
8796   if (NumTPLists) {
8797     Info.TemplParamLists =
8798         new (getContext()) TemplateParameterList *[NumTPLists];
8799     for (unsigned i = 0; i != NumTPLists; ++i)
8800       Info.TemplParamLists[i] = readTemplateParameterList();
8801   }
8802 }
8803 
8804 TemplateParameterList *
8805 ASTRecordReader::readTemplateParameterList() {
8806   SourceLocation TemplateLoc = readSourceLocation();
8807   SourceLocation LAngleLoc = readSourceLocation();
8808   SourceLocation RAngleLoc = readSourceLocation();
8809 
8810   unsigned NumParams = readInt();
8811   SmallVector<NamedDecl *, 16> Params;
8812   Params.reserve(NumParams);
8813   while (NumParams--)
8814     Params.push_back(readDeclAs<NamedDecl>());
8815 
8816   bool HasRequiresClause = readBool();
8817   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8818 
8819   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8820       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8821   return TemplateParams;
8822 }
8823 
8824 void ASTRecordReader::readTemplateArgumentList(
8825                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8826                         bool Canonicalize) {
8827   unsigned NumTemplateArgs = readInt();
8828   TemplArgs.reserve(NumTemplateArgs);
8829   while (NumTemplateArgs--)
8830     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8831 }
8832 
8833 /// Read a UnresolvedSet structure.
8834 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8835   unsigned NumDecls = readInt();
8836   Set.reserve(getContext(), NumDecls);
8837   while (NumDecls--) {
8838     DeclID ID = readDeclID();
8839     AccessSpecifier AS = (AccessSpecifier) readInt();
8840     Set.addLazyDecl(getContext(), ID, AS);
8841   }
8842 }
8843 
8844 CXXBaseSpecifier
8845 ASTRecordReader::readCXXBaseSpecifier() {
8846   bool isVirtual = readBool();
8847   bool isBaseOfClass = readBool();
8848   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8849   bool inheritConstructors = readBool();
8850   TypeSourceInfo *TInfo = readTypeSourceInfo();
8851   SourceRange Range = readSourceRange();
8852   SourceLocation EllipsisLoc = readSourceLocation();
8853   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8854                           EllipsisLoc);
8855   Result.setInheritConstructors(inheritConstructors);
8856   return Result;
8857 }
8858 
8859 CXXCtorInitializer **
8860 ASTRecordReader::readCXXCtorInitializers() {
8861   ASTContext &Context = getContext();
8862   unsigned NumInitializers = readInt();
8863   assert(NumInitializers && "wrote ctor initializers but have no inits");
8864   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8865   for (unsigned i = 0; i != NumInitializers; ++i) {
8866     TypeSourceInfo *TInfo = nullptr;
8867     bool IsBaseVirtual = false;
8868     FieldDecl *Member = nullptr;
8869     IndirectFieldDecl *IndirectMember = nullptr;
8870 
8871     CtorInitializerType Type = (CtorInitializerType) readInt();
8872     switch (Type) {
8873     case CTOR_INITIALIZER_BASE:
8874       TInfo = readTypeSourceInfo();
8875       IsBaseVirtual = readBool();
8876       break;
8877 
8878     case CTOR_INITIALIZER_DELEGATING:
8879       TInfo = readTypeSourceInfo();
8880       break;
8881 
8882      case CTOR_INITIALIZER_MEMBER:
8883       Member = readDeclAs<FieldDecl>();
8884       break;
8885 
8886      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8887       IndirectMember = readDeclAs<IndirectFieldDecl>();
8888       break;
8889     }
8890 
8891     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8892     Expr *Init = readExpr();
8893     SourceLocation LParenLoc = readSourceLocation();
8894     SourceLocation RParenLoc = readSourceLocation();
8895 
8896     CXXCtorInitializer *BOMInit;
8897     if (Type == CTOR_INITIALIZER_BASE)
8898       BOMInit = new (Context)
8899           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8900                              RParenLoc, MemberOrEllipsisLoc);
8901     else if (Type == CTOR_INITIALIZER_DELEGATING)
8902       BOMInit = new (Context)
8903           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8904     else if (Member)
8905       BOMInit = new (Context)
8906           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8907                              Init, RParenLoc);
8908     else
8909       BOMInit = new (Context)
8910           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8911                              LParenLoc, Init, RParenLoc);
8912 
8913     if (/*IsWritten*/readBool()) {
8914       unsigned SourceOrder = readInt();
8915       BOMInit->setSourceOrder(SourceOrder);
8916     }
8917 
8918     CtorInitializers[i] = BOMInit;
8919   }
8920 
8921   return CtorInitializers;
8922 }
8923 
8924 NestedNameSpecifierLoc
8925 ASTRecordReader::readNestedNameSpecifierLoc() {
8926   ASTContext &Context = getContext();
8927   unsigned N = readInt();
8928   NestedNameSpecifierLocBuilder Builder;
8929   for (unsigned I = 0; I != N; ++I) {
8930     auto Kind = readNestedNameSpecifierKind();
8931     switch (Kind) {
8932     case NestedNameSpecifier::Identifier: {
8933       IdentifierInfo *II = readIdentifier();
8934       SourceRange Range = readSourceRange();
8935       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8936       break;
8937     }
8938 
8939     case NestedNameSpecifier::Namespace: {
8940       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8941       SourceRange Range = readSourceRange();
8942       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8943       break;
8944     }
8945 
8946     case NestedNameSpecifier::NamespaceAlias: {
8947       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8948       SourceRange Range = readSourceRange();
8949       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8950       break;
8951     }
8952 
8953     case NestedNameSpecifier::TypeSpec:
8954     case NestedNameSpecifier::TypeSpecWithTemplate: {
8955       bool Template = readBool();
8956       TypeSourceInfo *T = readTypeSourceInfo();
8957       if (!T)
8958         return NestedNameSpecifierLoc();
8959       SourceLocation ColonColonLoc = readSourceLocation();
8960 
8961       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8962       Builder.Extend(Context,
8963                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8964                      T->getTypeLoc(), ColonColonLoc);
8965       break;
8966     }
8967 
8968     case NestedNameSpecifier::Global: {
8969       SourceLocation ColonColonLoc = readSourceLocation();
8970       Builder.MakeGlobal(Context, ColonColonLoc);
8971       break;
8972     }
8973 
8974     case NestedNameSpecifier::Super: {
8975       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8976       SourceRange Range = readSourceRange();
8977       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8978       break;
8979     }
8980     }
8981   }
8982 
8983   return Builder.getWithLocInContext(Context);
8984 }
8985 
8986 SourceRange
8987 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8988                            unsigned &Idx) {
8989   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8990   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8991   return SourceRange(beg, end);
8992 }
8993 
8994 /// Read a floating-point value
8995 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8996   return llvm::APFloat(Sem, readAPInt());
8997 }
8998 
8999 // Read a string
9000 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9001   unsigned Len = Record[Idx++];
9002   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9003   Idx += Len;
9004   return Result;
9005 }
9006 
9007 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9008                                 unsigned &Idx) {
9009   std::string Filename = ReadString(Record, Idx);
9010   ResolveImportedPath(F, Filename);
9011   return Filename;
9012 }
9013 
9014 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9015                                 const RecordData &Record, unsigned &Idx) {
9016   std::string Filename = ReadString(Record, Idx);
9017   if (!BaseDirectory.empty())
9018     ResolveImportedPath(Filename, BaseDirectory);
9019   return Filename;
9020 }
9021 
9022 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9023                                          unsigned &Idx) {
9024   unsigned Major = Record[Idx++];
9025   unsigned Minor = Record[Idx++];
9026   unsigned Subminor = Record[Idx++];
9027   if (Minor == 0)
9028     return VersionTuple(Major);
9029   if (Subminor == 0)
9030     return VersionTuple(Major, Minor - 1);
9031   return VersionTuple(Major, Minor - 1, Subminor - 1);
9032 }
9033 
9034 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9035                                           const RecordData &Record,
9036                                           unsigned &Idx) {
9037   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9038   return CXXTemporary::Create(getContext(), Decl);
9039 }
9040 
9041 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9042   return Diag(CurrentImportLoc, DiagID);
9043 }
9044 
9045 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9046   return Diags.Report(Loc, DiagID);
9047 }
9048 
9049 /// Retrieve the identifier table associated with the
9050 /// preprocessor.
9051 IdentifierTable &ASTReader::getIdentifierTable() {
9052   return PP.getIdentifierTable();
9053 }
9054 
9055 /// Record that the given ID maps to the given switch-case
9056 /// statement.
9057 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9058   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9059          "Already have a SwitchCase with this ID");
9060   (*CurrSwitchCaseStmts)[ID] = SC;
9061 }
9062 
9063 /// Retrieve the switch-case statement with the given ID.
9064 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9065   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9066   return (*CurrSwitchCaseStmts)[ID];
9067 }
9068 
9069 void ASTReader::ClearSwitchCaseIDs() {
9070   CurrSwitchCaseStmts->clear();
9071 }
9072 
9073 void ASTReader::ReadComments() {
9074   ASTContext &Context = getContext();
9075   std::vector<RawComment *> Comments;
9076   for (SmallVectorImpl<std::pair<BitstreamCursor,
9077                                  serialization::ModuleFile *>>::iterator
9078        I = CommentsCursors.begin(),
9079        E = CommentsCursors.end();
9080        I != E; ++I) {
9081     Comments.clear();
9082     BitstreamCursor &Cursor = I->first;
9083     serialization::ModuleFile &F = *I->second;
9084     SavedStreamPosition SavedPosition(Cursor);
9085 
9086     RecordData Record;
9087     while (true) {
9088       Expected<llvm::BitstreamEntry> MaybeEntry =
9089           Cursor.advanceSkippingSubblocks(
9090               BitstreamCursor::AF_DontPopBlockAtEnd);
9091       if (!MaybeEntry) {
9092         Error(MaybeEntry.takeError());
9093         return;
9094       }
9095       llvm::BitstreamEntry Entry = MaybeEntry.get();
9096 
9097       switch (Entry.Kind) {
9098       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9099       case llvm::BitstreamEntry::Error:
9100         Error("malformed block record in AST file");
9101         return;
9102       case llvm::BitstreamEntry::EndBlock:
9103         goto NextCursor;
9104       case llvm::BitstreamEntry::Record:
9105         // The interesting case.
9106         break;
9107       }
9108 
9109       // Read a record.
9110       Record.clear();
9111       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9112       if (!MaybeComment) {
9113         Error(MaybeComment.takeError());
9114         return;
9115       }
9116       switch ((CommentRecordTypes)MaybeComment.get()) {
9117       case COMMENTS_RAW_COMMENT: {
9118         unsigned Idx = 0;
9119         SourceRange SR = ReadSourceRange(F, Record, Idx);
9120         RawComment::CommentKind Kind =
9121             (RawComment::CommentKind) Record[Idx++];
9122         bool IsTrailingComment = Record[Idx++];
9123         bool IsAlmostTrailingComment = Record[Idx++];
9124         Comments.push_back(new (Context) RawComment(
9125             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9126         break;
9127       }
9128       }
9129     }
9130   NextCursor:
9131     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9132         FileToOffsetToComment;
9133     for (RawComment *C : Comments) {
9134       SourceLocation CommentLoc = C->getBeginLoc();
9135       if (CommentLoc.isValid()) {
9136         std::pair<FileID, unsigned> Loc =
9137             SourceMgr.getDecomposedLoc(CommentLoc);
9138         if (Loc.first.isValid())
9139           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9140       }
9141     }
9142   }
9143 }
9144 
9145 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9146                                 bool IncludeSystem, bool Complain,
9147                     llvm::function_ref<void(const serialization::InputFile &IF,
9148                                             bool isSystem)> Visitor) {
9149   unsigned NumUserInputs = MF.NumUserInputFiles;
9150   unsigned NumInputs = MF.InputFilesLoaded.size();
9151   assert(NumUserInputs <= NumInputs);
9152   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9153   for (unsigned I = 0; I < N; ++I) {
9154     bool IsSystem = I >= NumUserInputs;
9155     InputFile IF = getInputFile(MF, I+1, Complain);
9156     Visitor(IF, IsSystem);
9157   }
9158 }
9159 
9160 void ASTReader::visitTopLevelModuleMaps(
9161     serialization::ModuleFile &MF,
9162     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9163   unsigned NumInputs = MF.InputFilesLoaded.size();
9164   for (unsigned I = 0; I < NumInputs; ++I) {
9165     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9166     if (IFI.TopLevelModuleMap)
9167       // FIXME: This unnecessarily re-reads the InputFileInfo.
9168       if (auto FE = getInputFile(MF, I + 1).getFile())
9169         Visitor(FE);
9170   }
9171 }
9172 
9173 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9174   // If we know the owning module, use it.
9175   if (Module *M = D->getImportedOwningModule())
9176     return M->getFullModuleName();
9177 
9178   // Otherwise, use the name of the top-level module the decl is within.
9179   if (ModuleFile *M = getOwningModuleFile(D))
9180     return M->ModuleName;
9181 
9182   // Not from a module.
9183   return {};
9184 }
9185 
9186 void ASTReader::finishPendingActions() {
9187   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9188          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9189          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9190          !PendingUpdateRecords.empty()) {
9191     // If any identifiers with corresponding top-level declarations have
9192     // been loaded, load those declarations now.
9193     using TopLevelDeclsMap =
9194         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9195     TopLevelDeclsMap TopLevelDecls;
9196 
9197     while (!PendingIdentifierInfos.empty()) {
9198       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9199       SmallVector<uint32_t, 4> DeclIDs =
9200           std::move(PendingIdentifierInfos.back().second);
9201       PendingIdentifierInfos.pop_back();
9202 
9203       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9204     }
9205 
9206     // Load each function type that we deferred loading because it was a
9207     // deduced type that might refer to a local type declared within itself.
9208     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9209       auto *FD = PendingFunctionTypes[I].first;
9210       FD->setType(GetType(PendingFunctionTypes[I].second));
9211 
9212       // If we gave a function a deduced return type, remember that we need to
9213       // propagate that along the redeclaration chain.
9214       auto *DT = FD->getReturnType()->getContainedDeducedType();
9215       if (DT && DT->isDeduced())
9216         PendingDeducedTypeUpdates.insert(
9217             {FD->getCanonicalDecl(), FD->getReturnType()});
9218     }
9219     PendingFunctionTypes.clear();
9220 
9221     // For each decl chain that we wanted to complete while deserializing, mark
9222     // it as "still needs to be completed".
9223     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9224       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9225     }
9226     PendingIncompleteDeclChains.clear();
9227 
9228     // Load pending declaration chains.
9229     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9230       loadPendingDeclChain(PendingDeclChains[I].first,
9231                            PendingDeclChains[I].second);
9232     PendingDeclChains.clear();
9233 
9234     // Make the most recent of the top-level declarations visible.
9235     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9236            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9237       IdentifierInfo *II = TLD->first;
9238       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9239         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9240       }
9241     }
9242 
9243     // Load any pending macro definitions.
9244     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9245       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9246       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9247       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9248       // Initialize the macro history from chained-PCHs ahead of module imports.
9249       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9250            ++IDIdx) {
9251         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9252         if (!Info.M->isModule())
9253           resolvePendingMacro(II, Info);
9254       }
9255       // Handle module imports.
9256       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9257            ++IDIdx) {
9258         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9259         if (Info.M->isModule())
9260           resolvePendingMacro(II, Info);
9261       }
9262     }
9263     PendingMacroIDs.clear();
9264 
9265     // Wire up the DeclContexts for Decls that we delayed setting until
9266     // recursive loading is completed.
9267     while (!PendingDeclContextInfos.empty()) {
9268       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9269       PendingDeclContextInfos.pop_front();
9270       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9271       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9272       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9273     }
9274 
9275     // Perform any pending declaration updates.
9276     while (!PendingUpdateRecords.empty()) {
9277       auto Update = PendingUpdateRecords.pop_back_val();
9278       ReadingKindTracker ReadingKind(Read_Decl, *this);
9279       loadDeclUpdateRecords(Update);
9280     }
9281   }
9282 
9283   // At this point, all update records for loaded decls are in place, so any
9284   // fake class definitions should have become real.
9285   assert(PendingFakeDefinitionData.empty() &&
9286          "faked up a class definition but never saw the real one");
9287 
9288   // If we deserialized any C++ or Objective-C class definitions, any
9289   // Objective-C protocol definitions, or any redeclarable templates, make sure
9290   // that all redeclarations point to the definitions. Note that this can only
9291   // happen now, after the redeclaration chains have been fully wired.
9292   for (Decl *D : PendingDefinitions) {
9293     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9294       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9295         // Make sure that the TagType points at the definition.
9296         const_cast<TagType*>(TagT)->decl = TD;
9297       }
9298 
9299       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9300         for (auto *R = getMostRecentExistingDecl(RD); R;
9301              R = R->getPreviousDecl()) {
9302           assert((R == D) ==
9303                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9304                  "declaration thinks it's the definition but it isn't");
9305           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9306         }
9307       }
9308 
9309       continue;
9310     }
9311 
9312     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9313       // Make sure that the ObjCInterfaceType points at the definition.
9314       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9315         ->Decl = ID;
9316 
9317       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9318         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9319 
9320       continue;
9321     }
9322 
9323     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9324       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9325         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9326 
9327       continue;
9328     }
9329 
9330     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9331     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9332       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9333   }
9334   PendingDefinitions.clear();
9335 
9336   // Load the bodies of any functions or methods we've encountered. We do
9337   // this now (delayed) so that we can be sure that the declaration chains
9338   // have been fully wired up (hasBody relies on this).
9339   // FIXME: We shouldn't require complete redeclaration chains here.
9340   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9341                                PBEnd = PendingBodies.end();
9342        PB != PBEnd; ++PB) {
9343     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9344       // For a function defined inline within a class template, force the
9345       // canonical definition to be the one inside the canonical definition of
9346       // the template. This ensures that we instantiate from a correct view
9347       // of the template.
9348       //
9349       // Sadly we can't do this more generally: we can't be sure that all
9350       // copies of an arbitrary class definition will have the same members
9351       // defined (eg, some member functions may not be instantiated, and some
9352       // special members may or may not have been implicitly defined).
9353       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9354         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9355           continue;
9356 
9357       // FIXME: Check for =delete/=default?
9358       // FIXME: Complain about ODR violations here?
9359       const FunctionDecl *Defn = nullptr;
9360       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9361         FD->setLazyBody(PB->second);
9362       } else {
9363         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9364         mergeDefinitionVisibility(NonConstDefn, FD);
9365 
9366         if (!FD->isLateTemplateParsed() &&
9367             !NonConstDefn->isLateTemplateParsed() &&
9368             FD->getODRHash() != NonConstDefn->getODRHash()) {
9369           if (!isa<CXXMethodDecl>(FD)) {
9370             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9371           } else if (FD->getLexicalParent()->isFileContext() &&
9372                      NonConstDefn->getLexicalParent()->isFileContext()) {
9373             // Only diagnose out-of-line method definitions.  If they are
9374             // in class definitions, then an error will be generated when
9375             // processing the class bodies.
9376             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9377           }
9378         }
9379       }
9380       continue;
9381     }
9382 
9383     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9384     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9385       MD->setLazyBody(PB->second);
9386   }
9387   PendingBodies.clear();
9388 
9389   // Do some cleanup.
9390   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9391     getContext().deduplicateMergedDefinitonsFor(ND);
9392   PendingMergedDefinitionsToDeduplicate.clear();
9393 }
9394 
9395 void ASTReader::diagnoseOdrViolations() {
9396   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9397       PendingFunctionOdrMergeFailures.empty() &&
9398       PendingEnumOdrMergeFailures.empty())
9399     return;
9400 
9401   // Trigger the import of the full definition of each class that had any
9402   // odr-merging problems, so we can produce better diagnostics for them.
9403   // These updates may in turn find and diagnose some ODR failures, so take
9404   // ownership of the set first.
9405   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9406   PendingOdrMergeFailures.clear();
9407   for (auto &Merge : OdrMergeFailures) {
9408     Merge.first->buildLookup();
9409     Merge.first->decls_begin();
9410     Merge.first->bases_begin();
9411     Merge.first->vbases_begin();
9412     for (auto &RecordPair : Merge.second) {
9413       auto *RD = RecordPair.first;
9414       RD->decls_begin();
9415       RD->bases_begin();
9416       RD->vbases_begin();
9417     }
9418   }
9419 
9420   // Trigger the import of functions.
9421   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9422   PendingFunctionOdrMergeFailures.clear();
9423   for (auto &Merge : FunctionOdrMergeFailures) {
9424     Merge.first->buildLookup();
9425     Merge.first->decls_begin();
9426     Merge.first->getBody();
9427     for (auto &FD : Merge.second) {
9428       FD->buildLookup();
9429       FD->decls_begin();
9430       FD->getBody();
9431     }
9432   }
9433 
9434   // Trigger the import of enums.
9435   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9436   PendingEnumOdrMergeFailures.clear();
9437   for (auto &Merge : EnumOdrMergeFailures) {
9438     Merge.first->decls_begin();
9439     for (auto &Enum : Merge.second) {
9440       Enum->decls_begin();
9441     }
9442   }
9443 
9444   // For each declaration from a merged context, check that the canonical
9445   // definition of that context also contains a declaration of the same
9446   // entity.
9447   //
9448   // Caution: this loop does things that might invalidate iterators into
9449   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9450   while (!PendingOdrMergeChecks.empty()) {
9451     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9452 
9453     // FIXME: Skip over implicit declarations for now. This matters for things
9454     // like implicitly-declared special member functions. This isn't entirely
9455     // correct; we can end up with multiple unmerged declarations of the same
9456     // implicit entity.
9457     if (D->isImplicit())
9458       continue;
9459 
9460     DeclContext *CanonDef = D->getDeclContext();
9461 
9462     bool Found = false;
9463     const Decl *DCanon = D->getCanonicalDecl();
9464 
9465     for (auto RI : D->redecls()) {
9466       if (RI->getLexicalDeclContext() == CanonDef) {
9467         Found = true;
9468         break;
9469       }
9470     }
9471     if (Found)
9472       continue;
9473 
9474     // Quick check failed, time to do the slow thing. Note, we can't just
9475     // look up the name of D in CanonDef here, because the member that is
9476     // in CanonDef might not be found by name lookup (it might have been
9477     // replaced by a more recent declaration in the lookup table), and we
9478     // can't necessarily find it in the redeclaration chain because it might
9479     // be merely mergeable, not redeclarable.
9480     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9481     for (auto *CanonMember : CanonDef->decls()) {
9482       if (CanonMember->getCanonicalDecl() == DCanon) {
9483         // This can happen if the declaration is merely mergeable and not
9484         // actually redeclarable (we looked for redeclarations earlier).
9485         //
9486         // FIXME: We should be able to detect this more efficiently, without
9487         // pulling in all of the members of CanonDef.
9488         Found = true;
9489         break;
9490       }
9491       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9492         if (ND->getDeclName() == D->getDeclName())
9493           Candidates.push_back(ND);
9494     }
9495 
9496     if (!Found) {
9497       // The AST doesn't like TagDecls becoming invalid after they've been
9498       // completed. We only really need to mark FieldDecls as invalid here.
9499       if (!isa<TagDecl>(D))
9500         D->setInvalidDecl();
9501 
9502       // Ensure we don't accidentally recursively enter deserialization while
9503       // we're producing our diagnostic.
9504       Deserializing RecursionGuard(this);
9505 
9506       std::string CanonDefModule =
9507           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9508       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9509         << D << getOwningModuleNameForDiagnostic(D)
9510         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9511 
9512       if (Candidates.empty())
9513         Diag(cast<Decl>(CanonDef)->getLocation(),
9514              diag::note_module_odr_violation_no_possible_decls) << D;
9515       else {
9516         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9517           Diag(Candidates[I]->getLocation(),
9518                diag::note_module_odr_violation_possible_decl)
9519             << Candidates[I];
9520       }
9521 
9522       DiagnosedOdrMergeFailures.insert(CanonDef);
9523     }
9524   }
9525 
9526   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9527       EnumOdrMergeFailures.empty())
9528     return;
9529 
9530   // Ensure we don't accidentally recursively enter deserialization while
9531   // we're producing our diagnostics.
9532   Deserializing RecursionGuard(this);
9533 
9534   // Common code for hashing helpers.
9535   ODRHash Hash;
9536   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9537     Hash.clear();
9538     Hash.AddQualType(Ty);
9539     return Hash.CalculateHash();
9540   };
9541 
9542   auto ComputeODRHash = [&Hash](const Stmt *S) {
9543     assert(S);
9544     Hash.clear();
9545     Hash.AddStmt(S);
9546     return Hash.CalculateHash();
9547   };
9548 
9549   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9550     assert(D);
9551     Hash.clear();
9552     Hash.AddSubDecl(D);
9553     return Hash.CalculateHash();
9554   };
9555 
9556   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9557     Hash.clear();
9558     Hash.AddTemplateArgument(TA);
9559     return Hash.CalculateHash();
9560   };
9561 
9562   auto ComputeTemplateParameterListODRHash =
9563       [&Hash](const TemplateParameterList *TPL) {
9564         assert(TPL);
9565         Hash.clear();
9566         Hash.AddTemplateParameterList(TPL);
9567         return Hash.CalculateHash();
9568       };
9569 
9570   // Used with err_module_odr_violation_mismatch_decl and
9571   // note_module_odr_violation_mismatch_decl
9572   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9573   enum ODRMismatchDecl {
9574     EndOfClass,
9575     PublicSpecifer,
9576     PrivateSpecifer,
9577     ProtectedSpecifer,
9578     StaticAssert,
9579     Field,
9580     CXXMethod,
9581     TypeAlias,
9582     TypeDef,
9583     Var,
9584     Friend,
9585     FunctionTemplate,
9586     Other
9587   };
9588 
9589   // Used with err_module_odr_violation_mismatch_decl_diff and
9590   // note_module_odr_violation_mismatch_decl_diff
9591   enum ODRMismatchDeclDifference {
9592     StaticAssertCondition,
9593     StaticAssertMessage,
9594     StaticAssertOnlyMessage,
9595     FieldName,
9596     FieldTypeName,
9597     FieldSingleBitField,
9598     FieldDifferentWidthBitField,
9599     FieldSingleMutable,
9600     FieldSingleInitializer,
9601     FieldDifferentInitializers,
9602     MethodName,
9603     MethodDeleted,
9604     MethodDefaulted,
9605     MethodVirtual,
9606     MethodStatic,
9607     MethodVolatile,
9608     MethodConst,
9609     MethodInline,
9610     MethodNumberParameters,
9611     MethodParameterType,
9612     MethodParameterName,
9613     MethodParameterSingleDefaultArgument,
9614     MethodParameterDifferentDefaultArgument,
9615     MethodNoTemplateArguments,
9616     MethodDifferentNumberTemplateArguments,
9617     MethodDifferentTemplateArgument,
9618     MethodSingleBody,
9619     MethodDifferentBody,
9620     TypedefName,
9621     TypedefType,
9622     VarName,
9623     VarType,
9624     VarSingleInitializer,
9625     VarDifferentInitializer,
9626     VarConstexpr,
9627     FriendTypeFunction,
9628     FriendType,
9629     FriendFunction,
9630     FunctionTemplateDifferentNumberParameters,
9631     FunctionTemplateParameterDifferentKind,
9632     FunctionTemplateParameterName,
9633     FunctionTemplateParameterSingleDefaultArgument,
9634     FunctionTemplateParameterDifferentDefaultArgument,
9635     FunctionTemplateParameterDifferentType,
9636     FunctionTemplatePackParameter,
9637   };
9638 
9639   // These lambdas have the common portions of the ODR diagnostics.  This
9640   // has the same return as Diag(), so addition parameters can be passed
9641   // in with operator<<
9642   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9643                                  SourceLocation Loc, SourceRange Range,
9644                                  ODRMismatchDeclDifference DiffType) {
9645     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9646            << FirstRecord << FirstModule.empty() << FirstModule << Range
9647            << DiffType;
9648   };
9649   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9650                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9651     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9652            << SecondModule << Range << DiffType;
9653   };
9654 
9655   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9656                        &ComputeQualTypeODRHash, &ComputeODRHash](
9657                           NamedDecl *FirstRecord, StringRef FirstModule,
9658                           StringRef SecondModule, FieldDecl *FirstField,
9659                           FieldDecl *SecondField) {
9660     IdentifierInfo *FirstII = FirstField->getIdentifier();
9661     IdentifierInfo *SecondII = SecondField->getIdentifier();
9662     if (FirstII->getName() != SecondII->getName()) {
9663       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9664                        FirstField->getSourceRange(), FieldName)
9665           << FirstII;
9666       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9667                       SecondField->getSourceRange(), FieldName)
9668           << SecondII;
9669 
9670       return true;
9671     }
9672 
9673     assert(getContext().hasSameType(FirstField->getType(),
9674                                     SecondField->getType()));
9675 
9676     QualType FirstType = FirstField->getType();
9677     QualType SecondType = SecondField->getType();
9678     if (ComputeQualTypeODRHash(FirstType) !=
9679         ComputeQualTypeODRHash(SecondType)) {
9680       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9681                        FirstField->getSourceRange(), FieldTypeName)
9682           << FirstII << FirstType;
9683       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9684                       SecondField->getSourceRange(), FieldTypeName)
9685           << SecondII << SecondType;
9686 
9687       return true;
9688     }
9689 
9690     const bool IsFirstBitField = FirstField->isBitField();
9691     const bool IsSecondBitField = SecondField->isBitField();
9692     if (IsFirstBitField != IsSecondBitField) {
9693       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9694                        FirstField->getSourceRange(), FieldSingleBitField)
9695           << FirstII << IsFirstBitField;
9696       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9697                       SecondField->getSourceRange(), FieldSingleBitField)
9698           << SecondII << IsSecondBitField;
9699       return true;
9700     }
9701 
9702     if (IsFirstBitField && IsSecondBitField) {
9703       unsigned FirstBitWidthHash =
9704           ComputeODRHash(FirstField->getBitWidth());
9705       unsigned SecondBitWidthHash =
9706           ComputeODRHash(SecondField->getBitWidth());
9707       if (FirstBitWidthHash != SecondBitWidthHash) {
9708         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9709                          FirstField->getSourceRange(),
9710                          FieldDifferentWidthBitField)
9711             << FirstII << FirstField->getBitWidth()->getSourceRange();
9712         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9713                         SecondField->getSourceRange(),
9714                         FieldDifferentWidthBitField)
9715             << SecondII << SecondField->getBitWidth()->getSourceRange();
9716         return true;
9717       }
9718     }
9719 
9720     if (!PP.getLangOpts().CPlusPlus)
9721       return false;
9722 
9723     const bool IsFirstMutable = FirstField->isMutable();
9724     const bool IsSecondMutable = SecondField->isMutable();
9725     if (IsFirstMutable != IsSecondMutable) {
9726       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9727                        FirstField->getSourceRange(), FieldSingleMutable)
9728           << FirstII << IsFirstMutable;
9729       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9730                       SecondField->getSourceRange(), FieldSingleMutable)
9731           << SecondII << IsSecondMutable;
9732       return true;
9733     }
9734 
9735     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9736     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9737     if ((!FirstInitializer && SecondInitializer) ||
9738         (FirstInitializer && !SecondInitializer)) {
9739       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9740                        FirstField->getSourceRange(), FieldSingleInitializer)
9741           << FirstII << (FirstInitializer != nullptr);
9742       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9743                       SecondField->getSourceRange(), FieldSingleInitializer)
9744           << SecondII << (SecondInitializer != nullptr);
9745       return true;
9746     }
9747 
9748     if (FirstInitializer && SecondInitializer) {
9749       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9750       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9751       if (FirstInitHash != SecondInitHash) {
9752         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9753                          FirstField->getSourceRange(),
9754                          FieldDifferentInitializers)
9755             << FirstII << FirstInitializer->getSourceRange();
9756         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9757                         SecondField->getSourceRange(),
9758                         FieldDifferentInitializers)
9759             << SecondII << SecondInitializer->getSourceRange();
9760         return true;
9761       }
9762     }
9763 
9764     return false;
9765   };
9766 
9767   auto ODRDiagTypeDefOrAlias =
9768       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9769           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9770           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9771           bool IsTypeAlias) {
9772         auto FirstName = FirstTD->getDeclName();
9773         auto SecondName = SecondTD->getDeclName();
9774         if (FirstName != SecondName) {
9775           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9776                            FirstTD->getSourceRange(), TypedefName)
9777               << IsTypeAlias << FirstName;
9778           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9779                           SecondTD->getSourceRange(), TypedefName)
9780               << IsTypeAlias << SecondName;
9781           return true;
9782         }
9783 
9784         QualType FirstType = FirstTD->getUnderlyingType();
9785         QualType SecondType = SecondTD->getUnderlyingType();
9786         if (ComputeQualTypeODRHash(FirstType) !=
9787             ComputeQualTypeODRHash(SecondType)) {
9788           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9789                            FirstTD->getSourceRange(), TypedefType)
9790               << IsTypeAlias << FirstName << FirstType;
9791           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9792                           SecondTD->getSourceRange(), TypedefType)
9793               << IsTypeAlias << SecondName << SecondType;
9794           return true;
9795         }
9796 
9797         return false;
9798   };
9799 
9800   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9801                      &ComputeQualTypeODRHash, &ComputeODRHash,
9802                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9803                            StringRef SecondModule, VarDecl *FirstVD,
9804                            VarDecl *SecondVD) {
9805     auto FirstName = FirstVD->getDeclName();
9806     auto SecondName = SecondVD->getDeclName();
9807     if (FirstName != SecondName) {
9808       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9809                        FirstVD->getSourceRange(), VarName)
9810           << FirstName;
9811       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9812                       SecondVD->getSourceRange(), VarName)
9813           << SecondName;
9814       return true;
9815     }
9816 
9817     QualType FirstType = FirstVD->getType();
9818     QualType SecondType = SecondVD->getType();
9819     if (ComputeQualTypeODRHash(FirstType) !=
9820         ComputeQualTypeODRHash(SecondType)) {
9821       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9822                        FirstVD->getSourceRange(), VarType)
9823           << FirstName << FirstType;
9824       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9825                       SecondVD->getSourceRange(), VarType)
9826           << SecondName << SecondType;
9827       return true;
9828     }
9829 
9830     if (!PP.getLangOpts().CPlusPlus)
9831       return false;
9832 
9833     const Expr *FirstInit = FirstVD->getInit();
9834     const Expr *SecondInit = SecondVD->getInit();
9835     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9836       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9837                        FirstVD->getSourceRange(), VarSingleInitializer)
9838           << FirstName << (FirstInit == nullptr)
9839           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9840       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9841                       SecondVD->getSourceRange(), VarSingleInitializer)
9842           << SecondName << (SecondInit == nullptr)
9843           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9844       return true;
9845     }
9846 
9847     if (FirstInit && SecondInit &&
9848         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9849       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9850                        FirstVD->getSourceRange(), VarDifferentInitializer)
9851           << FirstName << FirstInit->getSourceRange();
9852       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9853                       SecondVD->getSourceRange(), VarDifferentInitializer)
9854           << SecondName << SecondInit->getSourceRange();
9855       return true;
9856     }
9857 
9858     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9859     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9860     if (FirstIsConstexpr != SecondIsConstexpr) {
9861       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9862                        FirstVD->getSourceRange(), VarConstexpr)
9863           << FirstName << FirstIsConstexpr;
9864       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9865                       SecondVD->getSourceRange(), VarConstexpr)
9866           << SecondName << SecondIsConstexpr;
9867       return true;
9868     }
9869     return false;
9870   };
9871 
9872   auto DifferenceSelector = [](Decl *D) {
9873     assert(D && "valid Decl required");
9874     switch (D->getKind()) {
9875     default:
9876       return Other;
9877     case Decl::AccessSpec:
9878       switch (D->getAccess()) {
9879       case AS_public:
9880         return PublicSpecifer;
9881       case AS_private:
9882         return PrivateSpecifer;
9883       case AS_protected:
9884         return ProtectedSpecifer;
9885       case AS_none:
9886         break;
9887       }
9888       llvm_unreachable("Invalid access specifier");
9889     case Decl::StaticAssert:
9890       return StaticAssert;
9891     case Decl::Field:
9892       return Field;
9893     case Decl::CXXMethod:
9894     case Decl::CXXConstructor:
9895     case Decl::CXXDestructor:
9896       return CXXMethod;
9897     case Decl::TypeAlias:
9898       return TypeAlias;
9899     case Decl::Typedef:
9900       return TypeDef;
9901     case Decl::Var:
9902       return Var;
9903     case Decl::Friend:
9904       return Friend;
9905     case Decl::FunctionTemplate:
9906       return FunctionTemplate;
9907     }
9908   };
9909 
9910   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9911   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9912                                                  RecordDecl *Record,
9913                                                  const DeclContext *DC) {
9914     for (auto *D : Record->decls()) {
9915       if (!ODRHash::isDeclToBeProcessed(D, DC))
9916         continue;
9917       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9918     }
9919   };
9920 
9921   struct DiffResult {
9922     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9923     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9924   };
9925 
9926   // If there is a diagnoseable difference, FirstDiffType and
9927   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9928   // filled in if not EndOfClass.
9929   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9930                                              DeclHashes &SecondHashes) {
9931     DiffResult DR;
9932     auto FirstIt = FirstHashes.begin();
9933     auto SecondIt = SecondHashes.begin();
9934     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9935       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9936           FirstIt->second == SecondIt->second) {
9937         ++FirstIt;
9938         ++SecondIt;
9939         continue;
9940       }
9941 
9942       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9943       DR.SecondDecl =
9944           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9945 
9946       DR.FirstDiffType =
9947           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9948       DR.SecondDiffType =
9949           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9950       return DR;
9951     }
9952     return DR;
9953   };
9954 
9955   // Use this to diagnose that an unexpected Decl was encountered
9956   // or no difference was detected. This causes a generic error
9957   // message to be emitted.
9958   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9959                                       StringRef FirstModule,
9960                                       NamedDecl *SecondRecord,
9961                                       StringRef SecondModule) {
9962     Diag(FirstRecord->getLocation(),
9963          diag::err_module_odr_violation_different_definitions)
9964         << FirstRecord << FirstModule.empty() << FirstModule;
9965 
9966     if (DR.FirstDecl) {
9967       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9968           << FirstRecord << DR.FirstDecl->getSourceRange();
9969     }
9970 
9971     Diag(SecondRecord->getLocation(),
9972          diag::note_module_odr_violation_different_definitions)
9973         << SecondModule;
9974 
9975     if (DR.SecondDecl) {
9976       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9977           << DR.SecondDecl->getSourceRange();
9978     }
9979   };
9980 
9981   auto DiagnoseODRMismatch =
9982       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9983              NamedDecl *SecondRecord, StringRef SecondModule) {
9984         SourceLocation FirstLoc;
9985         SourceRange FirstRange;
9986         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9987         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9988           FirstLoc = FirstTag->getBraceRange().getEnd();
9989         } else {
9990           FirstLoc = DR.FirstDecl->getLocation();
9991           FirstRange = DR.FirstDecl->getSourceRange();
9992         }
9993         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9994             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9995             << DR.FirstDiffType;
9996 
9997         SourceLocation SecondLoc;
9998         SourceRange SecondRange;
9999         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10000         if (DR.SecondDiffType == EndOfClass && SecondTag) {
10001           SecondLoc = SecondTag->getBraceRange().getEnd();
10002         } else {
10003           SecondLoc = DR.SecondDecl->getLocation();
10004           SecondRange = DR.SecondDecl->getSourceRange();
10005         }
10006         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10007             << SecondModule << SecondRange << DR.SecondDiffType;
10008       };
10009 
10010   // Issue any pending ODR-failure diagnostics.
10011   for (auto &Merge : OdrMergeFailures) {
10012     // If we've already pointed out a specific problem with this class, don't
10013     // bother issuing a general "something's different" diagnostic.
10014     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10015       continue;
10016 
10017     bool Diagnosed = false;
10018     CXXRecordDecl *FirstRecord = Merge.first;
10019     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10020     for (auto &RecordPair : Merge.second) {
10021       CXXRecordDecl *SecondRecord = RecordPair.first;
10022       // Multiple different declarations got merged together; tell the user
10023       // where they came from.
10024       if (FirstRecord == SecondRecord)
10025         continue;
10026 
10027       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10028 
10029       auto *FirstDD = FirstRecord->DefinitionData;
10030       auto *SecondDD = RecordPair.second;
10031 
10032       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10033 
10034       // Diagnostics from DefinitionData are emitted here.
10035       if (FirstDD != SecondDD) {
10036         enum ODRDefinitionDataDifference {
10037           NumBases,
10038           NumVBases,
10039           BaseType,
10040           BaseVirtual,
10041           BaseAccess,
10042         };
10043         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10044                                  this](SourceLocation Loc, SourceRange Range,
10045                                        ODRDefinitionDataDifference DiffType) {
10046           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10047                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10048                  << DiffType;
10049         };
10050         auto ODRDiagBaseNote = [&SecondModule,
10051                                 this](SourceLocation Loc, SourceRange Range,
10052                                       ODRDefinitionDataDifference DiffType) {
10053           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10054                  << SecondModule << Range << DiffType;
10055         };
10056 
10057         unsigned FirstNumBases = FirstDD->NumBases;
10058         unsigned FirstNumVBases = FirstDD->NumVBases;
10059         unsigned SecondNumBases = SecondDD->NumBases;
10060         unsigned SecondNumVBases = SecondDD->NumVBases;
10061 
10062         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10063           unsigned NumBases = DD->NumBases;
10064           if (NumBases == 0) return SourceRange();
10065           auto bases = DD->bases();
10066           return SourceRange(bases[0].getBeginLoc(),
10067                              bases[NumBases - 1].getEndLoc());
10068         };
10069 
10070         if (FirstNumBases != SecondNumBases) {
10071           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10072                            NumBases)
10073               << FirstNumBases;
10074           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10075                           NumBases)
10076               << SecondNumBases;
10077           Diagnosed = true;
10078           break;
10079         }
10080 
10081         if (FirstNumVBases != SecondNumVBases) {
10082           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10083                            NumVBases)
10084               << FirstNumVBases;
10085           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10086                           NumVBases)
10087               << SecondNumVBases;
10088           Diagnosed = true;
10089           break;
10090         }
10091 
10092         auto FirstBases = FirstDD->bases();
10093         auto SecondBases = SecondDD->bases();
10094         unsigned i = 0;
10095         for (i = 0; i < FirstNumBases; ++i) {
10096           auto FirstBase = FirstBases[i];
10097           auto SecondBase = SecondBases[i];
10098           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10099               ComputeQualTypeODRHash(SecondBase.getType())) {
10100             ODRDiagBaseError(FirstRecord->getLocation(),
10101                              FirstBase.getSourceRange(), BaseType)
10102                 << (i + 1) << FirstBase.getType();
10103             ODRDiagBaseNote(SecondRecord->getLocation(),
10104                             SecondBase.getSourceRange(), BaseType)
10105                 << (i + 1) << SecondBase.getType();
10106             break;
10107           }
10108 
10109           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10110             ODRDiagBaseError(FirstRecord->getLocation(),
10111                              FirstBase.getSourceRange(), BaseVirtual)
10112                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10113             ODRDiagBaseNote(SecondRecord->getLocation(),
10114                             SecondBase.getSourceRange(), BaseVirtual)
10115                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10116             break;
10117           }
10118 
10119           if (FirstBase.getAccessSpecifierAsWritten() !=
10120               SecondBase.getAccessSpecifierAsWritten()) {
10121             ODRDiagBaseError(FirstRecord->getLocation(),
10122                              FirstBase.getSourceRange(), BaseAccess)
10123                 << (i + 1) << FirstBase.getType()
10124                 << (int)FirstBase.getAccessSpecifierAsWritten();
10125             ODRDiagBaseNote(SecondRecord->getLocation(),
10126                             SecondBase.getSourceRange(), BaseAccess)
10127                 << (i + 1) << SecondBase.getType()
10128                 << (int)SecondBase.getAccessSpecifierAsWritten();
10129             break;
10130           }
10131         }
10132 
10133         if (i != FirstNumBases) {
10134           Diagnosed = true;
10135           break;
10136         }
10137       }
10138 
10139       const ClassTemplateDecl *FirstTemplate =
10140           FirstRecord->getDescribedClassTemplate();
10141       const ClassTemplateDecl *SecondTemplate =
10142           SecondRecord->getDescribedClassTemplate();
10143 
10144       assert(!FirstTemplate == !SecondTemplate &&
10145              "Both pointers should be null or non-null");
10146 
10147       if (FirstTemplate && SecondTemplate) {
10148         DeclHashes FirstTemplateHashes;
10149         DeclHashes SecondTemplateHashes;
10150 
10151         auto PopulateTemplateParameterHashs =
10152             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10153                                      const ClassTemplateDecl *TD) {
10154               for (auto *D : TD->getTemplateParameters()->asArray()) {
10155                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10156               }
10157             };
10158 
10159         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10160         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10161 
10162         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10163                "Number of template parameters should be equal.");
10164 
10165         auto FirstIt = FirstTemplateHashes.begin();
10166         auto FirstEnd = FirstTemplateHashes.end();
10167         auto SecondIt = SecondTemplateHashes.begin();
10168         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10169           if (FirstIt->second == SecondIt->second)
10170             continue;
10171 
10172           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10173           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10174 
10175           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10176                  "Parameter Decl's should be the same kind.");
10177 
10178           enum ODRTemplateDifference {
10179             ParamEmptyName,
10180             ParamName,
10181             ParamSingleDefaultArgument,
10182             ParamDifferentDefaultArgument,
10183           };
10184 
10185           auto hasDefaultArg = [](const NamedDecl *D) {
10186             if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(D))
10187               return TTP->hasDefaultArgument() &&
10188                       !TTP->defaultArgumentWasInherited();
10189             if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
10190               return NTTP->hasDefaultArgument() &&
10191                       !NTTP->defaultArgumentWasInherited();
10192             auto *TTP = cast<TemplateTemplateParmDecl>(D);
10193             return TTP->hasDefaultArgument() &&
10194                     !TTP->defaultArgumentWasInherited();
10195           };
10196           bool hasFirstArg = hasDefaultArg(FirstDecl);
10197           bool hasSecondArg = hasDefaultArg(SecondDecl);
10198 
10199           ODRTemplateDifference ErrDiffType;
10200           ODRTemplateDifference NoteDiffType;
10201 
10202           DeclarationName FirstName = FirstDecl->getDeclName();
10203           DeclarationName SecondName = SecondDecl->getDeclName();
10204 
10205           if (FirstName != SecondName) {
10206             bool FirstNameEmpty =
10207                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10208             bool SecondNameEmpty = SecondName.isIdentifier() &&
10209                                     !SecondName.getAsIdentifierInfo();
10210             ErrDiffType = FirstNameEmpty ? ParamEmptyName : ParamName;
10211             NoteDiffType = SecondNameEmpty ? ParamEmptyName : ParamName;
10212           } else if (hasFirstArg == hasSecondArg)
10213             ErrDiffType = NoteDiffType = ParamDifferentDefaultArgument;
10214           else
10215             ErrDiffType = NoteDiffType = ParamSingleDefaultArgument;
10216 
10217           Diag(FirstDecl->getLocation(),
10218                 diag::err_module_odr_violation_template_parameter)
10219               << FirstRecord << FirstModule.empty() << FirstModule
10220               << FirstDecl->getSourceRange() << ErrDiffType << hasFirstArg
10221               << FirstName;
10222           Diag(SecondDecl->getLocation(),
10223                 diag::note_module_odr_violation_template_parameter)
10224               << SecondModule << SecondDecl->getSourceRange() << NoteDiffType
10225               << hasSecondArg << SecondName;
10226           break;
10227         }
10228 
10229         if (FirstIt != FirstEnd) {
10230           Diagnosed = true;
10231           break;
10232         }
10233       }
10234 
10235       DeclHashes FirstHashes;
10236       DeclHashes SecondHashes;
10237       const DeclContext *DC = FirstRecord;
10238       PopulateHashes(FirstHashes, FirstRecord, DC);
10239       PopulateHashes(SecondHashes, SecondRecord, DC);
10240 
10241       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10242       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10243       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10244       Decl *FirstDecl = DR.FirstDecl;
10245       Decl *SecondDecl = DR.SecondDecl;
10246 
10247       if (FirstDiffType == Other || SecondDiffType == Other) {
10248         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10249                               SecondModule);
10250         Diagnosed = true;
10251         break;
10252       }
10253 
10254       if (FirstDiffType != SecondDiffType) {
10255         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10256                             SecondModule);
10257         Diagnosed = true;
10258         break;
10259       }
10260 
10261       assert(FirstDiffType == SecondDiffType);
10262 
10263       switch (FirstDiffType) {
10264       case Other:
10265       case EndOfClass:
10266       case PublicSpecifer:
10267       case PrivateSpecifer:
10268       case ProtectedSpecifer:
10269         llvm_unreachable("Invalid diff type");
10270 
10271       case StaticAssert: {
10272         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10273         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10274 
10275         Expr *FirstExpr = FirstSA->getAssertExpr();
10276         Expr *SecondExpr = SecondSA->getAssertExpr();
10277         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10278         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10279         if (FirstODRHash != SecondODRHash) {
10280           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10281                            FirstExpr->getSourceRange(), StaticAssertCondition);
10282           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10283                           SecondExpr->getSourceRange(), StaticAssertCondition);
10284           Diagnosed = true;
10285           break;
10286         }
10287 
10288         StringLiteral *FirstStr = FirstSA->getMessage();
10289         StringLiteral *SecondStr = SecondSA->getMessage();
10290         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10291         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10292           SourceLocation FirstLoc, SecondLoc;
10293           SourceRange FirstRange, SecondRange;
10294           if (FirstStr) {
10295             FirstLoc = FirstStr->getBeginLoc();
10296             FirstRange = FirstStr->getSourceRange();
10297           } else {
10298             FirstLoc = FirstSA->getBeginLoc();
10299             FirstRange = FirstSA->getSourceRange();
10300           }
10301           if (SecondStr) {
10302             SecondLoc = SecondStr->getBeginLoc();
10303             SecondRange = SecondStr->getSourceRange();
10304           } else {
10305             SecondLoc = SecondSA->getBeginLoc();
10306             SecondRange = SecondSA->getSourceRange();
10307           }
10308           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10309                            StaticAssertOnlyMessage)
10310               << (FirstStr == nullptr);
10311           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10312                           StaticAssertOnlyMessage)
10313               << (SecondStr == nullptr);
10314           Diagnosed = true;
10315           break;
10316         }
10317 
10318         if (FirstStr && SecondStr &&
10319             FirstStr->getString() != SecondStr->getString()) {
10320           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10321                            FirstStr->getSourceRange(), StaticAssertMessage);
10322           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10323                           SecondStr->getSourceRange(), StaticAssertMessage);
10324           Diagnosed = true;
10325           break;
10326         }
10327         break;
10328       }
10329       case Field: {
10330         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10331                                  cast<FieldDecl>(FirstDecl),
10332                                  cast<FieldDecl>(SecondDecl));
10333         break;
10334       }
10335       case CXXMethod: {
10336         enum {
10337           DiagMethod,
10338           DiagConstructor,
10339           DiagDestructor,
10340         } FirstMethodType,
10341             SecondMethodType;
10342         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10343           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10344           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10345           return DiagMethod;
10346         };
10347         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10348         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10349         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10350         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10351         auto FirstName = FirstMethod->getDeclName();
10352         auto SecondName = SecondMethod->getDeclName();
10353         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10354           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10355                            FirstMethod->getSourceRange(), MethodName)
10356               << FirstMethodType << FirstName;
10357           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10358                           SecondMethod->getSourceRange(), MethodName)
10359               << SecondMethodType << SecondName;
10360 
10361           Diagnosed = true;
10362           break;
10363         }
10364 
10365         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10366         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10367         if (FirstDeleted != SecondDeleted) {
10368           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10369                            FirstMethod->getSourceRange(), MethodDeleted)
10370               << FirstMethodType << FirstName << FirstDeleted;
10371 
10372           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10373                           SecondMethod->getSourceRange(), MethodDeleted)
10374               << SecondMethodType << SecondName << SecondDeleted;
10375           Diagnosed = true;
10376           break;
10377         }
10378 
10379         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10380         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10381         if (FirstDefaulted != SecondDefaulted) {
10382           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10383                            FirstMethod->getSourceRange(), MethodDefaulted)
10384               << FirstMethodType << FirstName << FirstDefaulted;
10385 
10386           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10387                           SecondMethod->getSourceRange(), MethodDefaulted)
10388               << SecondMethodType << SecondName << SecondDefaulted;
10389           Diagnosed = true;
10390           break;
10391         }
10392 
10393         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10394         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10395         const bool FirstPure = FirstMethod->isPure();
10396         const bool SecondPure = SecondMethod->isPure();
10397         if ((FirstVirtual || SecondVirtual) &&
10398             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10399           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10400                            FirstMethod->getSourceRange(), MethodVirtual)
10401               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10402           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10403                           SecondMethod->getSourceRange(), MethodVirtual)
10404               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10405           Diagnosed = true;
10406           break;
10407         }
10408 
10409         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10410         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10411         // class needs to be checked instead.
10412         const auto FirstStorage = FirstMethod->getStorageClass();
10413         const auto SecondStorage = SecondMethod->getStorageClass();
10414         const bool FirstStatic = FirstStorage == SC_Static;
10415         const bool SecondStatic = SecondStorage == SC_Static;
10416         if (FirstStatic != SecondStatic) {
10417           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10418                            FirstMethod->getSourceRange(), MethodStatic)
10419               << FirstMethodType << FirstName << FirstStatic;
10420           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10421                           SecondMethod->getSourceRange(), MethodStatic)
10422               << SecondMethodType << SecondName << SecondStatic;
10423           Diagnosed = true;
10424           break;
10425         }
10426 
10427         const bool FirstVolatile = FirstMethod->isVolatile();
10428         const bool SecondVolatile = SecondMethod->isVolatile();
10429         if (FirstVolatile != SecondVolatile) {
10430           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10431                            FirstMethod->getSourceRange(), MethodVolatile)
10432               << FirstMethodType << FirstName << FirstVolatile;
10433           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10434                           SecondMethod->getSourceRange(), MethodVolatile)
10435               << SecondMethodType << SecondName << SecondVolatile;
10436           Diagnosed = true;
10437           break;
10438         }
10439 
10440         const bool FirstConst = FirstMethod->isConst();
10441         const bool SecondConst = SecondMethod->isConst();
10442         if (FirstConst != SecondConst) {
10443           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10444                            FirstMethod->getSourceRange(), MethodConst)
10445               << FirstMethodType << FirstName << FirstConst;
10446           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10447                           SecondMethod->getSourceRange(), MethodConst)
10448               << SecondMethodType << SecondName << SecondConst;
10449           Diagnosed = true;
10450           break;
10451         }
10452 
10453         const bool FirstInline = FirstMethod->isInlineSpecified();
10454         const bool SecondInline = SecondMethod->isInlineSpecified();
10455         if (FirstInline != SecondInline) {
10456           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10457                            FirstMethod->getSourceRange(), MethodInline)
10458               << FirstMethodType << FirstName << FirstInline;
10459           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10460                           SecondMethod->getSourceRange(), MethodInline)
10461               << SecondMethodType << SecondName << SecondInline;
10462           Diagnosed = true;
10463           break;
10464         }
10465 
10466         const unsigned FirstNumParameters = FirstMethod->param_size();
10467         const unsigned SecondNumParameters = SecondMethod->param_size();
10468         if (FirstNumParameters != SecondNumParameters) {
10469           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10470                            FirstMethod->getSourceRange(),
10471                            MethodNumberParameters)
10472               << FirstMethodType << FirstName << FirstNumParameters;
10473           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10474                           SecondMethod->getSourceRange(),
10475                           MethodNumberParameters)
10476               << SecondMethodType << SecondName << SecondNumParameters;
10477           Diagnosed = true;
10478           break;
10479         }
10480 
10481         // Need this status boolean to know when break out of the switch.
10482         bool ParameterMismatch = false;
10483         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10484           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10485           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10486 
10487           QualType FirstParamType = FirstParam->getType();
10488           QualType SecondParamType = SecondParam->getType();
10489           if (FirstParamType != SecondParamType &&
10490               ComputeQualTypeODRHash(FirstParamType) !=
10491                   ComputeQualTypeODRHash(SecondParamType)) {
10492             if (const DecayedType *ParamDecayedType =
10493                     FirstParamType->getAs<DecayedType>()) {
10494               ODRDiagDeclError(
10495                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10496                   FirstMethod->getSourceRange(), MethodParameterType)
10497                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10498                   << true << ParamDecayedType->getOriginalType();
10499             } else {
10500               ODRDiagDeclError(
10501                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10502                   FirstMethod->getSourceRange(), MethodParameterType)
10503                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10504                   << false;
10505             }
10506 
10507             if (const DecayedType *ParamDecayedType =
10508                     SecondParamType->getAs<DecayedType>()) {
10509               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10510                               SecondMethod->getSourceRange(),
10511                               MethodParameterType)
10512                   << SecondMethodType << SecondName << (I + 1)
10513                   << SecondParamType << true
10514                   << ParamDecayedType->getOriginalType();
10515             } else {
10516               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10517                               SecondMethod->getSourceRange(),
10518                               MethodParameterType)
10519                   << SecondMethodType << SecondName << (I + 1)
10520                   << SecondParamType << false;
10521             }
10522             ParameterMismatch = true;
10523             break;
10524           }
10525 
10526           DeclarationName FirstParamName = FirstParam->getDeclName();
10527           DeclarationName SecondParamName = SecondParam->getDeclName();
10528           if (FirstParamName != SecondParamName) {
10529             ODRDiagDeclError(FirstRecord, FirstModule,
10530                              FirstMethod->getLocation(),
10531                              FirstMethod->getSourceRange(), MethodParameterName)
10532                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10533             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10534                             SecondMethod->getSourceRange(), MethodParameterName)
10535                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10536             ParameterMismatch = true;
10537             break;
10538           }
10539 
10540           const Expr *FirstInit = FirstParam->getInit();
10541           const Expr *SecondInit = SecondParam->getInit();
10542           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10543             ODRDiagDeclError(FirstRecord, FirstModule,
10544                              FirstMethod->getLocation(),
10545                              FirstMethod->getSourceRange(),
10546                              MethodParameterSingleDefaultArgument)
10547                 << FirstMethodType << FirstName << (I + 1)
10548                 << (FirstInit == nullptr)
10549                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10550             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10551                             SecondMethod->getSourceRange(),
10552                             MethodParameterSingleDefaultArgument)
10553                 << SecondMethodType << SecondName << (I + 1)
10554                 << (SecondInit == nullptr)
10555                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10556             ParameterMismatch = true;
10557             break;
10558           }
10559 
10560           if (FirstInit && SecondInit &&
10561               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10562             ODRDiagDeclError(FirstRecord, FirstModule,
10563                              FirstMethod->getLocation(),
10564                              FirstMethod->getSourceRange(),
10565                              MethodParameterDifferentDefaultArgument)
10566                 << FirstMethodType << FirstName << (I + 1)
10567                 << FirstInit->getSourceRange();
10568             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10569                             SecondMethod->getSourceRange(),
10570                             MethodParameterDifferentDefaultArgument)
10571                 << SecondMethodType << SecondName << (I + 1)
10572                 << SecondInit->getSourceRange();
10573             ParameterMismatch = true;
10574             break;
10575 
10576           }
10577         }
10578 
10579         if (ParameterMismatch) {
10580           Diagnosed = true;
10581           break;
10582         }
10583 
10584         const auto *FirstTemplateArgs =
10585             FirstMethod->getTemplateSpecializationArgs();
10586         const auto *SecondTemplateArgs =
10587             SecondMethod->getTemplateSpecializationArgs();
10588 
10589         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10590             (!FirstTemplateArgs && SecondTemplateArgs)) {
10591           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10592                            FirstMethod->getSourceRange(),
10593                            MethodNoTemplateArguments)
10594               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10595           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10596                           SecondMethod->getSourceRange(),
10597                           MethodNoTemplateArguments)
10598               << SecondMethodType << SecondName
10599               << (SecondTemplateArgs != nullptr);
10600 
10601           Diagnosed = true;
10602           break;
10603         }
10604 
10605         if (FirstTemplateArgs && SecondTemplateArgs) {
10606           // Remove pack expansions from argument list.
10607           auto ExpandTemplateArgumentList =
10608               [](const TemplateArgumentList *TAL) {
10609                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10610                 for (const TemplateArgument &TA : TAL->asArray()) {
10611                   if (TA.getKind() != TemplateArgument::Pack) {
10612                     ExpandedList.push_back(&TA);
10613                     continue;
10614                   }
10615                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10616                     ExpandedList.push_back(&PackTA);
10617                   }
10618                 }
10619                 return ExpandedList;
10620               };
10621           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10622               ExpandTemplateArgumentList(FirstTemplateArgs);
10623           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10624               ExpandTemplateArgumentList(SecondTemplateArgs);
10625 
10626           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10627             ODRDiagDeclError(FirstRecord, FirstModule,
10628                              FirstMethod->getLocation(),
10629                              FirstMethod->getSourceRange(),
10630                              MethodDifferentNumberTemplateArguments)
10631                 << FirstMethodType << FirstName
10632                 << (unsigned)FirstExpandedList.size();
10633             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10634                             SecondMethod->getSourceRange(),
10635                             MethodDifferentNumberTemplateArguments)
10636                 << SecondMethodType << SecondName
10637                 << (unsigned)SecondExpandedList.size();
10638 
10639             Diagnosed = true;
10640             break;
10641           }
10642 
10643           bool TemplateArgumentMismatch = false;
10644           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10645             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10646                                    &SecondTA = *SecondExpandedList[i];
10647             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10648                 ComputeTemplateArgumentODRHash(SecondTA)) {
10649               continue;
10650             }
10651 
10652             ODRDiagDeclError(
10653                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10654                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10655                 << FirstMethodType << FirstName << FirstTA << i + 1;
10656             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10657                             SecondMethod->getSourceRange(),
10658                             MethodDifferentTemplateArgument)
10659                 << SecondMethodType << SecondName << SecondTA << i + 1;
10660 
10661             TemplateArgumentMismatch = true;
10662             break;
10663           }
10664 
10665           if (TemplateArgumentMismatch) {
10666             Diagnosed = true;
10667             break;
10668           }
10669         }
10670 
10671         // Compute the hash of the method as if it has no body.
10672         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10673           Hash.clear();
10674           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10675           return Hash.CalculateHash();
10676         };
10677 
10678         // Compare the hash generated to the hash stored.  A difference means
10679         // that a body was present in the original source.  Due to merging,
10680         // the stardard way of detecting a body will not work.
10681         const bool HasFirstBody =
10682             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10683         const bool HasSecondBody =
10684             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10685 
10686         if (HasFirstBody != HasSecondBody) {
10687           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10688                            FirstMethod->getSourceRange(), MethodSingleBody)
10689               << FirstMethodType << FirstName << HasFirstBody;
10690           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10691                           SecondMethod->getSourceRange(), MethodSingleBody)
10692               << SecondMethodType << SecondName << HasSecondBody;
10693           Diagnosed = true;
10694           break;
10695         }
10696 
10697         if (HasFirstBody && HasSecondBody) {
10698           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10699                            FirstMethod->getSourceRange(), MethodDifferentBody)
10700               << FirstMethodType << FirstName;
10701           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10702                           SecondMethod->getSourceRange(), MethodDifferentBody)
10703               << SecondMethodType << SecondName;
10704           Diagnosed = true;
10705           break;
10706         }
10707 
10708         break;
10709       }
10710       case TypeAlias:
10711       case TypeDef: {
10712         Diagnosed = ODRDiagTypeDefOrAlias(
10713             FirstRecord, FirstModule, SecondModule,
10714             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10715             FirstDiffType == TypeAlias);
10716         break;
10717       }
10718       case Var: {
10719         Diagnosed =
10720             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10721                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10722         break;
10723       }
10724       case Friend: {
10725         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10726         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10727 
10728         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10729         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10730 
10731         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10732         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10733 
10734         if (FirstND && SecondND) {
10735           ODRDiagDeclError(FirstRecord, FirstModule,
10736                            FirstFriend->getFriendLoc(),
10737                            FirstFriend->getSourceRange(), FriendFunction)
10738               << FirstND;
10739           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10740                           SecondFriend->getSourceRange(), FriendFunction)
10741               << SecondND;
10742 
10743           Diagnosed = true;
10744           break;
10745         }
10746 
10747         if (FirstTSI && SecondTSI) {
10748           QualType FirstFriendType = FirstTSI->getType();
10749           QualType SecondFriendType = SecondTSI->getType();
10750           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10751                  ComputeQualTypeODRHash(SecondFriendType));
10752           ODRDiagDeclError(FirstRecord, FirstModule,
10753                            FirstFriend->getFriendLoc(),
10754                            FirstFriend->getSourceRange(), FriendType)
10755               << FirstFriendType;
10756           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10757                           SecondFriend->getSourceRange(), FriendType)
10758               << SecondFriendType;
10759           Diagnosed = true;
10760           break;
10761         }
10762 
10763         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10764                          FirstFriend->getSourceRange(), FriendTypeFunction)
10765             << (FirstTSI == nullptr);
10766         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10767                         SecondFriend->getSourceRange(), FriendTypeFunction)
10768             << (SecondTSI == nullptr);
10769 
10770         Diagnosed = true;
10771         break;
10772       }
10773       case FunctionTemplate: {
10774         FunctionTemplateDecl *FirstTemplate =
10775             cast<FunctionTemplateDecl>(FirstDecl);
10776         FunctionTemplateDecl *SecondTemplate =
10777             cast<FunctionTemplateDecl>(SecondDecl);
10778 
10779         TemplateParameterList *FirstTPL =
10780             FirstTemplate->getTemplateParameters();
10781         TemplateParameterList *SecondTPL =
10782             SecondTemplate->getTemplateParameters();
10783 
10784         if (FirstTPL->size() != SecondTPL->size()) {
10785           ODRDiagDeclError(FirstRecord, FirstModule,
10786                            FirstTemplate->getLocation(),
10787                            FirstTemplate->getSourceRange(),
10788                            FunctionTemplateDifferentNumberParameters)
10789               << FirstTemplate << FirstTPL->size();
10790           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10791                           SecondTemplate->getSourceRange(),
10792                           FunctionTemplateDifferentNumberParameters)
10793               << SecondTemplate << SecondTPL->size();
10794 
10795           Diagnosed = true;
10796           break;
10797         }
10798 
10799         bool ParameterMismatch = false;
10800         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10801           NamedDecl *FirstParam = FirstTPL->getParam(i);
10802           NamedDecl *SecondParam = SecondTPL->getParam(i);
10803 
10804           if (FirstParam->getKind() != SecondParam->getKind()) {
10805             enum {
10806               TemplateTypeParameter,
10807               NonTypeTemplateParameter,
10808               TemplateTemplateParameter,
10809             };
10810             auto GetParamType = [](NamedDecl *D) {
10811               switch (D->getKind()) {
10812                 default:
10813                   llvm_unreachable("Unexpected template parameter type");
10814                 case Decl::TemplateTypeParm:
10815                   return TemplateTypeParameter;
10816                 case Decl::NonTypeTemplateParm:
10817                   return NonTypeTemplateParameter;
10818                 case Decl::TemplateTemplateParm:
10819                   return TemplateTemplateParameter;
10820               }
10821             };
10822 
10823             ODRDiagDeclError(FirstRecord, FirstModule,
10824                              FirstTemplate->getLocation(),
10825                              FirstTemplate->getSourceRange(),
10826                              FunctionTemplateParameterDifferentKind)
10827                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10828             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10829                             SecondTemplate->getSourceRange(),
10830                             FunctionTemplateParameterDifferentKind)
10831                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10832 
10833             ParameterMismatch = true;
10834             break;
10835           }
10836 
10837           if (FirstParam->getName() != SecondParam->getName()) {
10838             ODRDiagDeclError(
10839                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10840                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10841                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10842                 << FirstParam;
10843             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10844                             SecondTemplate->getSourceRange(),
10845                             FunctionTemplateParameterName)
10846                 << SecondTemplate << (i + 1)
10847                 << (bool)SecondParam->getIdentifier() << SecondParam;
10848             ParameterMismatch = true;
10849             break;
10850           }
10851 
10852           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10853               isa<TemplateTypeParmDecl>(SecondParam)) {
10854             TemplateTypeParmDecl *FirstTTPD =
10855                 cast<TemplateTypeParmDecl>(FirstParam);
10856             TemplateTypeParmDecl *SecondTTPD =
10857                 cast<TemplateTypeParmDecl>(SecondParam);
10858             bool HasFirstDefaultArgument =
10859                 FirstTTPD->hasDefaultArgument() &&
10860                 !FirstTTPD->defaultArgumentWasInherited();
10861             bool HasSecondDefaultArgument =
10862                 SecondTTPD->hasDefaultArgument() &&
10863                 !SecondTTPD->defaultArgumentWasInherited();
10864             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10865               ODRDiagDeclError(FirstRecord, FirstModule,
10866                                FirstTemplate->getLocation(),
10867                                FirstTemplate->getSourceRange(),
10868                                FunctionTemplateParameterSingleDefaultArgument)
10869                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10870               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10871                               SecondTemplate->getSourceRange(),
10872                               FunctionTemplateParameterSingleDefaultArgument)
10873                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10874               ParameterMismatch = true;
10875               break;
10876             }
10877 
10878             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10879               QualType FirstType = FirstTTPD->getDefaultArgument();
10880               QualType SecondType = SecondTTPD->getDefaultArgument();
10881               if (ComputeQualTypeODRHash(FirstType) !=
10882                   ComputeQualTypeODRHash(SecondType)) {
10883                 ODRDiagDeclError(
10884                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10885                     FirstTemplate->getSourceRange(),
10886                     FunctionTemplateParameterDifferentDefaultArgument)
10887                     << FirstTemplate << (i + 1) << FirstType;
10888                 ODRDiagDeclNote(
10889                     SecondModule, SecondTemplate->getLocation(),
10890                     SecondTemplate->getSourceRange(),
10891                     FunctionTemplateParameterDifferentDefaultArgument)
10892                     << SecondTemplate << (i + 1) << SecondType;
10893                 ParameterMismatch = true;
10894                 break;
10895               }
10896             }
10897 
10898             if (FirstTTPD->isParameterPack() !=
10899                 SecondTTPD->isParameterPack()) {
10900               ODRDiagDeclError(FirstRecord, FirstModule,
10901                                FirstTemplate->getLocation(),
10902                                FirstTemplate->getSourceRange(),
10903                                FunctionTemplatePackParameter)
10904                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10905               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10906                               SecondTemplate->getSourceRange(),
10907                               FunctionTemplatePackParameter)
10908                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10909               ParameterMismatch = true;
10910               break;
10911             }
10912           }
10913 
10914           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10915               isa<TemplateTemplateParmDecl>(SecondParam)) {
10916             TemplateTemplateParmDecl *FirstTTPD =
10917                 cast<TemplateTemplateParmDecl>(FirstParam);
10918             TemplateTemplateParmDecl *SecondTTPD =
10919                 cast<TemplateTemplateParmDecl>(SecondParam);
10920 
10921             TemplateParameterList *FirstTPL =
10922                 FirstTTPD->getTemplateParameters();
10923             TemplateParameterList *SecondTPL =
10924                 SecondTTPD->getTemplateParameters();
10925 
10926             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10927                 ComputeTemplateParameterListODRHash(SecondTPL)) {
10928               ODRDiagDeclError(FirstRecord, FirstModule,
10929                                FirstTemplate->getLocation(),
10930                                FirstTemplate->getSourceRange(),
10931                                FunctionTemplateParameterDifferentType)
10932                   << FirstTemplate << (i + 1);
10933               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10934                               SecondTemplate->getSourceRange(),
10935                               FunctionTemplateParameterDifferentType)
10936                   << SecondTemplate << (i + 1);
10937               ParameterMismatch = true;
10938               break;
10939             }
10940 
10941             bool HasFirstDefaultArgument =
10942                 FirstTTPD->hasDefaultArgument() &&
10943                 !FirstTTPD->defaultArgumentWasInherited();
10944             bool HasSecondDefaultArgument =
10945                 SecondTTPD->hasDefaultArgument() &&
10946                 !SecondTTPD->defaultArgumentWasInherited();
10947             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10948               ODRDiagDeclError(FirstRecord, FirstModule,
10949                                FirstTemplate->getLocation(),
10950                                FirstTemplate->getSourceRange(),
10951                                FunctionTemplateParameterSingleDefaultArgument)
10952                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10953               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10954                               SecondTemplate->getSourceRange(),
10955                               FunctionTemplateParameterSingleDefaultArgument)
10956                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10957               ParameterMismatch = true;
10958               break;
10959             }
10960 
10961             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10962               TemplateArgument FirstTA =
10963                   FirstTTPD->getDefaultArgument().getArgument();
10964               TemplateArgument SecondTA =
10965                   SecondTTPD->getDefaultArgument().getArgument();
10966               if (ComputeTemplateArgumentODRHash(FirstTA) !=
10967                   ComputeTemplateArgumentODRHash(SecondTA)) {
10968                 ODRDiagDeclError(
10969                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10970                     FirstTemplate->getSourceRange(),
10971                     FunctionTemplateParameterDifferentDefaultArgument)
10972                     << FirstTemplate << (i + 1) << FirstTA;
10973                 ODRDiagDeclNote(
10974                     SecondModule, SecondTemplate->getLocation(),
10975                     SecondTemplate->getSourceRange(),
10976                     FunctionTemplateParameterDifferentDefaultArgument)
10977                     << SecondTemplate << (i + 1) << SecondTA;
10978                 ParameterMismatch = true;
10979                 break;
10980               }
10981             }
10982 
10983             if (FirstTTPD->isParameterPack() !=
10984                 SecondTTPD->isParameterPack()) {
10985               ODRDiagDeclError(FirstRecord, FirstModule,
10986                                FirstTemplate->getLocation(),
10987                                FirstTemplate->getSourceRange(),
10988                                FunctionTemplatePackParameter)
10989                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10990               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10991                               SecondTemplate->getSourceRange(),
10992                               FunctionTemplatePackParameter)
10993                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10994               ParameterMismatch = true;
10995               break;
10996             }
10997           }
10998 
10999           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11000               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11001             NonTypeTemplateParmDecl *FirstNTTPD =
11002                 cast<NonTypeTemplateParmDecl>(FirstParam);
11003             NonTypeTemplateParmDecl *SecondNTTPD =
11004                 cast<NonTypeTemplateParmDecl>(SecondParam);
11005 
11006             QualType FirstType = FirstNTTPD->getType();
11007             QualType SecondType = SecondNTTPD->getType();
11008             if (ComputeQualTypeODRHash(FirstType) !=
11009                 ComputeQualTypeODRHash(SecondType)) {
11010               ODRDiagDeclError(FirstRecord, FirstModule,
11011                                FirstTemplate->getLocation(),
11012                                FirstTemplate->getSourceRange(),
11013                                FunctionTemplateParameterDifferentType)
11014                   << FirstTemplate << (i + 1);
11015               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11016                               SecondTemplate->getSourceRange(),
11017                               FunctionTemplateParameterDifferentType)
11018                   << SecondTemplate << (i + 1);
11019               ParameterMismatch = true;
11020               break;
11021             }
11022 
11023             bool HasFirstDefaultArgument =
11024                 FirstNTTPD->hasDefaultArgument() &&
11025                 !FirstNTTPD->defaultArgumentWasInherited();
11026             bool HasSecondDefaultArgument =
11027                 SecondNTTPD->hasDefaultArgument() &&
11028                 !SecondNTTPD->defaultArgumentWasInherited();
11029             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11030               ODRDiagDeclError(FirstRecord, FirstModule,
11031                                FirstTemplate->getLocation(),
11032                                FirstTemplate->getSourceRange(),
11033                                FunctionTemplateParameterSingleDefaultArgument)
11034                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11035               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11036                               SecondTemplate->getSourceRange(),
11037                               FunctionTemplateParameterSingleDefaultArgument)
11038                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11039               ParameterMismatch = true;
11040               break;
11041             }
11042 
11043             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11044               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11045               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11046               if (ComputeODRHash(FirstDefaultArgument) !=
11047                   ComputeODRHash(SecondDefaultArgument)) {
11048                 ODRDiagDeclError(
11049                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11050                     FirstTemplate->getSourceRange(),
11051                     FunctionTemplateParameterDifferentDefaultArgument)
11052                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11053                 ODRDiagDeclNote(
11054                     SecondModule, SecondTemplate->getLocation(),
11055                     SecondTemplate->getSourceRange(),
11056                     FunctionTemplateParameterDifferentDefaultArgument)
11057                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11058                 ParameterMismatch = true;
11059                 break;
11060               }
11061             }
11062 
11063             if (FirstNTTPD->isParameterPack() !=
11064                 SecondNTTPD->isParameterPack()) {
11065               ODRDiagDeclError(FirstRecord, FirstModule,
11066                                FirstTemplate->getLocation(),
11067                                FirstTemplate->getSourceRange(),
11068                                FunctionTemplatePackParameter)
11069                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11070               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11071                               SecondTemplate->getSourceRange(),
11072                               FunctionTemplatePackParameter)
11073                   << SecondTemplate << (i + 1)
11074                   << SecondNTTPD->isParameterPack();
11075               ParameterMismatch = true;
11076               break;
11077             }
11078           }
11079         }
11080 
11081         if (ParameterMismatch) {
11082           Diagnosed = true;
11083           break;
11084         }
11085 
11086         break;
11087       }
11088       }
11089 
11090       if (Diagnosed)
11091         continue;
11092 
11093       Diag(FirstDecl->getLocation(),
11094            diag::err_module_odr_violation_mismatch_decl_unknown)
11095           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11096           << FirstDecl->getSourceRange();
11097       Diag(SecondDecl->getLocation(),
11098            diag::note_module_odr_violation_mismatch_decl_unknown)
11099           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11100       Diagnosed = true;
11101     }
11102 
11103     if (!Diagnosed) {
11104       // All definitions are updates to the same declaration. This happens if a
11105       // module instantiates the declaration of a class template specialization
11106       // and two or more other modules instantiate its definition.
11107       //
11108       // FIXME: Indicate which modules had instantiations of this definition.
11109       // FIXME: How can this even happen?
11110       Diag(Merge.first->getLocation(),
11111            diag::err_module_odr_violation_different_instantiations)
11112         << Merge.first;
11113     }
11114   }
11115 
11116   // Issue ODR failures diagnostics for functions.
11117   for (auto &Merge : FunctionOdrMergeFailures) {
11118     enum ODRFunctionDifference {
11119       ReturnType,
11120       ParameterName,
11121       ParameterType,
11122       ParameterSingleDefaultArgument,
11123       ParameterDifferentDefaultArgument,
11124       FunctionBody,
11125     };
11126 
11127     FunctionDecl *FirstFunction = Merge.first;
11128     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11129 
11130     bool Diagnosed = false;
11131     for (auto &SecondFunction : Merge.second) {
11132 
11133       if (FirstFunction == SecondFunction)
11134         continue;
11135 
11136       std::string SecondModule =
11137           getOwningModuleNameForDiagnostic(SecondFunction);
11138 
11139       auto ODRDiagError = [FirstFunction, &FirstModule,
11140                            this](SourceLocation Loc, SourceRange Range,
11141                                  ODRFunctionDifference DiffType) {
11142         return Diag(Loc, diag::err_module_odr_violation_function)
11143                << FirstFunction << FirstModule.empty() << FirstModule << Range
11144                << DiffType;
11145       };
11146       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11147                                                SourceRange Range,
11148                                                ODRFunctionDifference DiffType) {
11149         return Diag(Loc, diag::note_module_odr_violation_function)
11150                << SecondModule << Range << DiffType;
11151       };
11152 
11153       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11154           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11155         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11156                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11157             << FirstFunction->getReturnType();
11158         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11159                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11160             << SecondFunction->getReturnType();
11161         Diagnosed = true;
11162         break;
11163       }
11164 
11165       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11166              "Merged functions with different number of parameters");
11167 
11168       auto ParamSize = FirstFunction->param_size();
11169       bool ParameterMismatch = false;
11170       for (unsigned I = 0; I < ParamSize; ++I) {
11171         auto *FirstParam = FirstFunction->getParamDecl(I);
11172         auto *SecondParam = SecondFunction->getParamDecl(I);
11173 
11174         assert(getContext().hasSameType(FirstParam->getType(),
11175                                       SecondParam->getType()) &&
11176                "Merged function has different parameter types.");
11177 
11178         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11179           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11180                        ParameterName)
11181               << I + 1 << FirstParam->getDeclName();
11182           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11183                       ParameterName)
11184               << I + 1 << SecondParam->getDeclName();
11185           ParameterMismatch = true;
11186           break;
11187         };
11188 
11189         QualType FirstParamType = FirstParam->getType();
11190         QualType SecondParamType = SecondParam->getType();
11191         if (FirstParamType != SecondParamType &&
11192             ComputeQualTypeODRHash(FirstParamType) !=
11193                 ComputeQualTypeODRHash(SecondParamType)) {
11194           if (const DecayedType *ParamDecayedType =
11195                   FirstParamType->getAs<DecayedType>()) {
11196             ODRDiagError(FirstParam->getLocation(),
11197                          FirstParam->getSourceRange(), ParameterType)
11198                 << (I + 1) << FirstParamType << true
11199                 << ParamDecayedType->getOriginalType();
11200           } else {
11201             ODRDiagError(FirstParam->getLocation(),
11202                          FirstParam->getSourceRange(), ParameterType)
11203                 << (I + 1) << FirstParamType << false;
11204           }
11205 
11206           if (const DecayedType *ParamDecayedType =
11207                   SecondParamType->getAs<DecayedType>()) {
11208             ODRDiagNote(SecondParam->getLocation(),
11209                         SecondParam->getSourceRange(), ParameterType)
11210                 << (I + 1) << SecondParamType << true
11211                 << ParamDecayedType->getOriginalType();
11212           } else {
11213             ODRDiagNote(SecondParam->getLocation(),
11214                         SecondParam->getSourceRange(), ParameterType)
11215                 << (I + 1) << SecondParamType << false;
11216           }
11217           ParameterMismatch = true;
11218           break;
11219         }
11220 
11221         const Expr *FirstInit = FirstParam->getInit();
11222         const Expr *SecondInit = SecondParam->getInit();
11223         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11224           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11225                        ParameterSingleDefaultArgument)
11226               << (I + 1) << (FirstInit == nullptr)
11227               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11228           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11229                       ParameterSingleDefaultArgument)
11230               << (I + 1) << (SecondInit == nullptr)
11231               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11232           ParameterMismatch = true;
11233           break;
11234         }
11235 
11236         if (FirstInit && SecondInit &&
11237             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11238           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11239                        ParameterDifferentDefaultArgument)
11240               << (I + 1) << FirstInit->getSourceRange();
11241           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11242                       ParameterDifferentDefaultArgument)
11243               << (I + 1) << SecondInit->getSourceRange();
11244           ParameterMismatch = true;
11245           break;
11246         }
11247 
11248         assert(ComputeSubDeclODRHash(FirstParam) ==
11249                    ComputeSubDeclODRHash(SecondParam) &&
11250                "Undiagnosed parameter difference.");
11251       }
11252 
11253       if (ParameterMismatch) {
11254         Diagnosed = true;
11255         break;
11256       }
11257 
11258       // If no error has been generated before now, assume the problem is in
11259       // the body and generate a message.
11260       ODRDiagError(FirstFunction->getLocation(),
11261                    FirstFunction->getSourceRange(), FunctionBody);
11262       ODRDiagNote(SecondFunction->getLocation(),
11263                   SecondFunction->getSourceRange(), FunctionBody);
11264       Diagnosed = true;
11265       break;
11266     }
11267     (void)Diagnosed;
11268     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11269   }
11270 
11271   // Issue ODR failures diagnostics for enums.
11272   for (auto &Merge : EnumOdrMergeFailures) {
11273     enum ODREnumDifference {
11274       SingleScopedEnum,
11275       EnumTagKeywordMismatch,
11276       SingleSpecifiedType,
11277       DifferentSpecifiedTypes,
11278       DifferentNumberEnumConstants,
11279       EnumConstantName,
11280       EnumConstantSingleInitilizer,
11281       EnumConstantDifferentInitilizer,
11282     };
11283 
11284     // If we've already pointed out a specific problem with this enum, don't
11285     // bother issuing a general "something's different" diagnostic.
11286     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11287       continue;
11288 
11289     EnumDecl *FirstEnum = Merge.first;
11290     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11291 
11292     using DeclHashes =
11293         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11294     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11295                               DeclHashes &Hashes, EnumDecl *Enum) {
11296       for (auto *D : Enum->decls()) {
11297         // Due to decl merging, the first EnumDecl is the parent of
11298         // Decls in both records.
11299         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11300           continue;
11301         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11302         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11303                             ComputeSubDeclODRHash(D));
11304       }
11305     };
11306     DeclHashes FirstHashes;
11307     PopulateHashes(FirstHashes, FirstEnum);
11308     bool Diagnosed = false;
11309     for (auto &SecondEnum : Merge.second) {
11310 
11311       if (FirstEnum == SecondEnum)
11312         continue;
11313 
11314       std::string SecondModule =
11315           getOwningModuleNameForDiagnostic(SecondEnum);
11316 
11317       auto ODRDiagError = [FirstEnum, &FirstModule,
11318                            this](SourceLocation Loc, SourceRange Range,
11319                                  ODREnumDifference DiffType) {
11320         return Diag(Loc, diag::err_module_odr_violation_enum)
11321                << FirstEnum << FirstModule.empty() << FirstModule << Range
11322                << DiffType;
11323       };
11324       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11325                                                SourceRange Range,
11326                                                ODREnumDifference DiffType) {
11327         return Diag(Loc, diag::note_module_odr_violation_enum)
11328                << SecondModule << Range << DiffType;
11329       };
11330 
11331       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11332         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11333                      SingleScopedEnum)
11334             << FirstEnum->isScoped();
11335         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11336                     SingleScopedEnum)
11337             << SecondEnum->isScoped();
11338         Diagnosed = true;
11339         continue;
11340       }
11341 
11342       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11343         if (FirstEnum->isScopedUsingClassTag() !=
11344             SecondEnum->isScopedUsingClassTag()) {
11345           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11346                        EnumTagKeywordMismatch)
11347               << FirstEnum->isScopedUsingClassTag();
11348           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11349                       EnumTagKeywordMismatch)
11350               << SecondEnum->isScopedUsingClassTag();
11351           Diagnosed = true;
11352           continue;
11353         }
11354       }
11355 
11356       QualType FirstUnderlyingType =
11357           FirstEnum->getIntegerTypeSourceInfo()
11358               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11359               : QualType();
11360       QualType SecondUnderlyingType =
11361           SecondEnum->getIntegerTypeSourceInfo()
11362               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11363               : QualType();
11364       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11365           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11366                        SingleSpecifiedType)
11367               << !FirstUnderlyingType.isNull();
11368           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11369                       SingleSpecifiedType)
11370               << !SecondUnderlyingType.isNull();
11371           Diagnosed = true;
11372           continue;
11373       }
11374 
11375       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11376         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11377             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11378           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11379                        DifferentSpecifiedTypes)
11380               << FirstUnderlyingType;
11381           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11382                       DifferentSpecifiedTypes)
11383               << SecondUnderlyingType;
11384           Diagnosed = true;
11385           continue;
11386         }
11387       }
11388 
11389       DeclHashes SecondHashes;
11390       PopulateHashes(SecondHashes, SecondEnum);
11391 
11392       if (FirstHashes.size() != SecondHashes.size()) {
11393         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11394                      DifferentNumberEnumConstants)
11395             << (int)FirstHashes.size();
11396         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11397                     DifferentNumberEnumConstants)
11398             << (int)SecondHashes.size();
11399         Diagnosed = true;
11400         continue;
11401       }
11402 
11403       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11404         if (FirstHashes[I].second == SecondHashes[I].second)
11405           continue;
11406         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11407         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11408 
11409         if (FirstEnumConstant->getDeclName() !=
11410             SecondEnumConstant->getDeclName()) {
11411 
11412           ODRDiagError(FirstEnumConstant->getLocation(),
11413                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11414               << I + 1 << FirstEnumConstant;
11415           ODRDiagNote(SecondEnumConstant->getLocation(),
11416                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11417               << I + 1 << SecondEnumConstant;
11418           Diagnosed = true;
11419           break;
11420         }
11421 
11422         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11423         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11424         if (!FirstInit && !SecondInit)
11425           continue;
11426 
11427         if (!FirstInit || !SecondInit) {
11428           ODRDiagError(FirstEnumConstant->getLocation(),
11429                        FirstEnumConstant->getSourceRange(),
11430                        EnumConstantSingleInitilizer)
11431               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11432           ODRDiagNote(SecondEnumConstant->getLocation(),
11433                       SecondEnumConstant->getSourceRange(),
11434                       EnumConstantSingleInitilizer)
11435               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11436           Diagnosed = true;
11437           break;
11438         }
11439 
11440         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11441           ODRDiagError(FirstEnumConstant->getLocation(),
11442                        FirstEnumConstant->getSourceRange(),
11443                        EnumConstantDifferentInitilizer)
11444               << I + 1 << FirstEnumConstant;
11445           ODRDiagNote(SecondEnumConstant->getLocation(),
11446                       SecondEnumConstant->getSourceRange(),
11447                       EnumConstantDifferentInitilizer)
11448               << I + 1 << SecondEnumConstant;
11449           Diagnosed = true;
11450           break;
11451         }
11452       }
11453     }
11454 
11455     (void)Diagnosed;
11456     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11457   }
11458 }
11459 
11460 void ASTReader::StartedDeserializing() {
11461   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11462     ReadTimer->startTimer();
11463 }
11464 
11465 void ASTReader::FinishedDeserializing() {
11466   assert(NumCurrentElementsDeserializing &&
11467          "FinishedDeserializing not paired with StartedDeserializing");
11468   if (NumCurrentElementsDeserializing == 1) {
11469     // We decrease NumCurrentElementsDeserializing only after pending actions
11470     // are finished, to avoid recursively re-calling finishPendingActions().
11471     finishPendingActions();
11472   }
11473   --NumCurrentElementsDeserializing;
11474 
11475   if (NumCurrentElementsDeserializing == 0) {
11476     // Propagate exception specification and deduced type updates along
11477     // redeclaration chains.
11478     //
11479     // We do this now rather than in finishPendingActions because we want to
11480     // be able to walk the complete redeclaration chains of the updated decls.
11481     while (!PendingExceptionSpecUpdates.empty() ||
11482            !PendingDeducedTypeUpdates.empty()) {
11483       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11484       PendingExceptionSpecUpdates.clear();
11485       for (auto Update : ESUpdates) {
11486         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11487         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11488         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11489         if (auto *Listener = getContext().getASTMutationListener())
11490           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11491         for (auto *Redecl : Update.second->redecls())
11492           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11493       }
11494 
11495       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11496       PendingDeducedTypeUpdates.clear();
11497       for (auto Update : DTUpdates) {
11498         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11499         // FIXME: If the return type is already deduced, check that it matches.
11500         getContext().adjustDeducedFunctionResultType(Update.first,
11501                                                      Update.second);
11502       }
11503     }
11504 
11505     if (ReadTimer)
11506       ReadTimer->stopTimer();
11507 
11508     diagnoseOdrViolations();
11509 
11510     // We are not in recursive loading, so it's safe to pass the "interesting"
11511     // decls to the consumer.
11512     if (Consumer)
11513       PassInterestingDeclsToConsumer();
11514   }
11515 }
11516 
11517 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11518   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11519     // Remove any fake results before adding any real ones.
11520     auto It = PendingFakeLookupResults.find(II);
11521     if (It != PendingFakeLookupResults.end()) {
11522       for (auto *ND : It->second)
11523         SemaObj->IdResolver.RemoveDecl(ND);
11524       // FIXME: this works around module+PCH performance issue.
11525       // Rather than erase the result from the map, which is O(n), just clear
11526       // the vector of NamedDecls.
11527       It->second.clear();
11528     }
11529   }
11530 
11531   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11532     SemaObj->TUScope->AddDecl(D);
11533   } else if (SemaObj->TUScope) {
11534     // Adding the decl to IdResolver may have failed because it was already in
11535     // (even though it was not added in scope). If it is already in, make sure
11536     // it gets in the scope as well.
11537     if (std::find(SemaObj->IdResolver.begin(Name),
11538                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11539       SemaObj->TUScope->AddDecl(D);
11540   }
11541 }
11542 
11543 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11544                      ASTContext *Context,
11545                      const PCHContainerReader &PCHContainerRdr,
11546                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11547                      StringRef isysroot,
11548                      DisableValidationForModuleKind DisableValidationKind,
11549                      bool AllowASTWithCompilerErrors,
11550                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11551                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11552                      std::unique_ptr<llvm::Timer> ReadTimer)
11553     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11554                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11555                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11556       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11557       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11558       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11559                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11560       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11561       DisableValidationKind(DisableValidationKind),
11562       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11563       AllowConfigurationMismatch(AllowConfigurationMismatch),
11564       ValidateSystemInputs(ValidateSystemInputs),
11565       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11566       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11567   SourceMgr.setExternalSLocEntrySource(this);
11568 
11569   for (const auto &Ext : Extensions) {
11570     auto BlockName = Ext->getExtensionMetadata().BlockName;
11571     auto Known = ModuleFileExtensions.find(BlockName);
11572     if (Known != ModuleFileExtensions.end()) {
11573       Diags.Report(diag::warn_duplicate_module_file_extension)
11574         << BlockName;
11575       continue;
11576     }
11577 
11578     ModuleFileExtensions.insert({BlockName, Ext});
11579   }
11580 }
11581 
11582 ASTReader::~ASTReader() {
11583   if (OwnsDeserializationListener)
11584     delete DeserializationListener;
11585 }
11586 
11587 IdentifierResolver &ASTReader::getIdResolver() {
11588   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11589 }
11590 
11591 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11592                                                unsigned AbbrevID) {
11593   Idx = 0;
11594   Record.clear();
11595   return Cursor.readRecord(AbbrevID, Record);
11596 }
11597 //===----------------------------------------------------------------------===//
11598 //// OMPClauseReader implementation
11599 ////===----------------------------------------------------------------------===//
11600 
11601 // This has to be in namespace clang because it's friended by all
11602 // of the OMP clauses.
11603 namespace clang {
11604 
11605 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11606   ASTRecordReader &Record;
11607   ASTContext &Context;
11608 
11609 public:
11610   OMPClauseReader(ASTRecordReader &Record)
11611       : Record(Record), Context(Record.getContext()) {}
11612 #define GEN_CLANG_CLAUSE_CLASS
11613 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11614 #include "llvm/Frontend/OpenMP/OMP.inc"
11615   OMPClause *readClause();
11616   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11617   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11618 };
11619 
11620 } // end namespace clang
11621 
11622 OMPClause *ASTRecordReader::readOMPClause() {
11623   return OMPClauseReader(*this).readClause();
11624 }
11625 
11626 OMPClause *OMPClauseReader::readClause() {
11627   OMPClause *C = nullptr;
11628   switch (llvm::omp::Clause(Record.readInt())) {
11629   case llvm::omp::OMPC_if:
11630     C = new (Context) OMPIfClause();
11631     break;
11632   case llvm::omp::OMPC_final:
11633     C = new (Context) OMPFinalClause();
11634     break;
11635   case llvm::omp::OMPC_num_threads:
11636     C = new (Context) OMPNumThreadsClause();
11637     break;
11638   case llvm::omp::OMPC_safelen:
11639     C = new (Context) OMPSafelenClause();
11640     break;
11641   case llvm::omp::OMPC_simdlen:
11642     C = new (Context) OMPSimdlenClause();
11643     break;
11644   case llvm::omp::OMPC_sizes: {
11645     unsigned NumSizes = Record.readInt();
11646     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11647     break;
11648   }
11649   case llvm::omp::OMPC_full:
11650     C = OMPFullClause::CreateEmpty(Context);
11651     break;
11652   case llvm::omp::OMPC_partial:
11653     C = OMPPartialClause::CreateEmpty(Context);
11654     break;
11655   case llvm::omp::OMPC_allocator:
11656     C = new (Context) OMPAllocatorClause();
11657     break;
11658   case llvm::omp::OMPC_collapse:
11659     C = new (Context) OMPCollapseClause();
11660     break;
11661   case llvm::omp::OMPC_default:
11662     C = new (Context) OMPDefaultClause();
11663     break;
11664   case llvm::omp::OMPC_proc_bind:
11665     C = new (Context) OMPProcBindClause();
11666     break;
11667   case llvm::omp::OMPC_schedule:
11668     C = new (Context) OMPScheduleClause();
11669     break;
11670   case llvm::omp::OMPC_ordered:
11671     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11672     break;
11673   case llvm::omp::OMPC_nowait:
11674     C = new (Context) OMPNowaitClause();
11675     break;
11676   case llvm::omp::OMPC_untied:
11677     C = new (Context) OMPUntiedClause();
11678     break;
11679   case llvm::omp::OMPC_mergeable:
11680     C = new (Context) OMPMergeableClause();
11681     break;
11682   case llvm::omp::OMPC_read:
11683     C = new (Context) OMPReadClause();
11684     break;
11685   case llvm::omp::OMPC_write:
11686     C = new (Context) OMPWriteClause();
11687     break;
11688   case llvm::omp::OMPC_update:
11689     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11690     break;
11691   case llvm::omp::OMPC_capture:
11692     C = new (Context) OMPCaptureClause();
11693     break;
11694   case llvm::omp::OMPC_compare:
11695     C = new (Context) OMPCompareClause();
11696     break;
11697   case llvm::omp::OMPC_seq_cst:
11698     C = new (Context) OMPSeqCstClause();
11699     break;
11700   case llvm::omp::OMPC_acq_rel:
11701     C = new (Context) OMPAcqRelClause();
11702     break;
11703   case llvm::omp::OMPC_acquire:
11704     C = new (Context) OMPAcquireClause();
11705     break;
11706   case llvm::omp::OMPC_release:
11707     C = new (Context) OMPReleaseClause();
11708     break;
11709   case llvm::omp::OMPC_relaxed:
11710     C = new (Context) OMPRelaxedClause();
11711     break;
11712   case llvm::omp::OMPC_threads:
11713     C = new (Context) OMPThreadsClause();
11714     break;
11715   case llvm::omp::OMPC_simd:
11716     C = new (Context) OMPSIMDClause();
11717     break;
11718   case llvm::omp::OMPC_nogroup:
11719     C = new (Context) OMPNogroupClause();
11720     break;
11721   case llvm::omp::OMPC_unified_address:
11722     C = new (Context) OMPUnifiedAddressClause();
11723     break;
11724   case llvm::omp::OMPC_unified_shared_memory:
11725     C = new (Context) OMPUnifiedSharedMemoryClause();
11726     break;
11727   case llvm::omp::OMPC_reverse_offload:
11728     C = new (Context) OMPReverseOffloadClause();
11729     break;
11730   case llvm::omp::OMPC_dynamic_allocators:
11731     C = new (Context) OMPDynamicAllocatorsClause();
11732     break;
11733   case llvm::omp::OMPC_atomic_default_mem_order:
11734     C = new (Context) OMPAtomicDefaultMemOrderClause();
11735     break;
11736  case llvm::omp::OMPC_private:
11737     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11738     break;
11739   case llvm::omp::OMPC_firstprivate:
11740     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11741     break;
11742   case llvm::omp::OMPC_lastprivate:
11743     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11744     break;
11745   case llvm::omp::OMPC_shared:
11746     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11747     break;
11748   case llvm::omp::OMPC_reduction: {
11749     unsigned N = Record.readInt();
11750     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11751     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11752     break;
11753   }
11754   case llvm::omp::OMPC_task_reduction:
11755     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11756     break;
11757   case llvm::omp::OMPC_in_reduction:
11758     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11759     break;
11760   case llvm::omp::OMPC_linear:
11761     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11762     break;
11763   case llvm::omp::OMPC_aligned:
11764     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11765     break;
11766   case llvm::omp::OMPC_copyin:
11767     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11768     break;
11769   case llvm::omp::OMPC_copyprivate:
11770     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11771     break;
11772   case llvm::omp::OMPC_flush:
11773     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11774     break;
11775   case llvm::omp::OMPC_depobj:
11776     C = OMPDepobjClause::CreateEmpty(Context);
11777     break;
11778   case llvm::omp::OMPC_depend: {
11779     unsigned NumVars = Record.readInt();
11780     unsigned NumLoops = Record.readInt();
11781     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11782     break;
11783   }
11784   case llvm::omp::OMPC_device:
11785     C = new (Context) OMPDeviceClause();
11786     break;
11787   case llvm::omp::OMPC_map: {
11788     OMPMappableExprListSizeTy Sizes;
11789     Sizes.NumVars = Record.readInt();
11790     Sizes.NumUniqueDeclarations = Record.readInt();
11791     Sizes.NumComponentLists = Record.readInt();
11792     Sizes.NumComponents = Record.readInt();
11793     C = OMPMapClause::CreateEmpty(Context, Sizes);
11794     break;
11795   }
11796   case llvm::omp::OMPC_num_teams:
11797     C = new (Context) OMPNumTeamsClause();
11798     break;
11799   case llvm::omp::OMPC_thread_limit:
11800     C = new (Context) OMPThreadLimitClause();
11801     break;
11802   case llvm::omp::OMPC_priority:
11803     C = new (Context) OMPPriorityClause();
11804     break;
11805   case llvm::omp::OMPC_grainsize:
11806     C = new (Context) OMPGrainsizeClause();
11807     break;
11808   case llvm::omp::OMPC_num_tasks:
11809     C = new (Context) OMPNumTasksClause();
11810     break;
11811   case llvm::omp::OMPC_hint:
11812     C = new (Context) OMPHintClause();
11813     break;
11814   case llvm::omp::OMPC_dist_schedule:
11815     C = new (Context) OMPDistScheduleClause();
11816     break;
11817   case llvm::omp::OMPC_defaultmap:
11818     C = new (Context) OMPDefaultmapClause();
11819     break;
11820   case llvm::omp::OMPC_to: {
11821     OMPMappableExprListSizeTy Sizes;
11822     Sizes.NumVars = Record.readInt();
11823     Sizes.NumUniqueDeclarations = Record.readInt();
11824     Sizes.NumComponentLists = Record.readInt();
11825     Sizes.NumComponents = Record.readInt();
11826     C = OMPToClause::CreateEmpty(Context, Sizes);
11827     break;
11828   }
11829   case llvm::omp::OMPC_from: {
11830     OMPMappableExprListSizeTy Sizes;
11831     Sizes.NumVars = Record.readInt();
11832     Sizes.NumUniqueDeclarations = Record.readInt();
11833     Sizes.NumComponentLists = Record.readInt();
11834     Sizes.NumComponents = Record.readInt();
11835     C = OMPFromClause::CreateEmpty(Context, Sizes);
11836     break;
11837   }
11838   case llvm::omp::OMPC_use_device_ptr: {
11839     OMPMappableExprListSizeTy Sizes;
11840     Sizes.NumVars = Record.readInt();
11841     Sizes.NumUniqueDeclarations = Record.readInt();
11842     Sizes.NumComponentLists = Record.readInt();
11843     Sizes.NumComponents = Record.readInt();
11844     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11845     break;
11846   }
11847   case llvm::omp::OMPC_use_device_addr: {
11848     OMPMappableExprListSizeTy Sizes;
11849     Sizes.NumVars = Record.readInt();
11850     Sizes.NumUniqueDeclarations = Record.readInt();
11851     Sizes.NumComponentLists = Record.readInt();
11852     Sizes.NumComponents = Record.readInt();
11853     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11854     break;
11855   }
11856   case llvm::omp::OMPC_is_device_ptr: {
11857     OMPMappableExprListSizeTy Sizes;
11858     Sizes.NumVars = Record.readInt();
11859     Sizes.NumUniqueDeclarations = Record.readInt();
11860     Sizes.NumComponentLists = Record.readInt();
11861     Sizes.NumComponents = Record.readInt();
11862     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11863     break;
11864   }
11865   case llvm::omp::OMPC_allocate:
11866     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11867     break;
11868   case llvm::omp::OMPC_nontemporal:
11869     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11870     break;
11871   case llvm::omp::OMPC_inclusive:
11872     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11873     break;
11874   case llvm::omp::OMPC_exclusive:
11875     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11876     break;
11877   case llvm::omp::OMPC_order:
11878     C = new (Context) OMPOrderClause();
11879     break;
11880   case llvm::omp::OMPC_init:
11881     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11882     break;
11883   case llvm::omp::OMPC_use:
11884     C = new (Context) OMPUseClause();
11885     break;
11886   case llvm::omp::OMPC_destroy:
11887     C = new (Context) OMPDestroyClause();
11888     break;
11889   case llvm::omp::OMPC_novariants:
11890     C = new (Context) OMPNovariantsClause();
11891     break;
11892   case llvm::omp::OMPC_nocontext:
11893     C = new (Context) OMPNocontextClause();
11894     break;
11895   case llvm::omp::OMPC_detach:
11896     C = new (Context) OMPDetachClause();
11897     break;
11898   case llvm::omp::OMPC_uses_allocators:
11899     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11900     break;
11901   case llvm::omp::OMPC_affinity:
11902     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11903     break;
11904   case llvm::omp::OMPC_filter:
11905     C = new (Context) OMPFilterClause();
11906     break;
11907   case llvm::omp::OMPC_bind:
11908     C = OMPBindClause::CreateEmpty(Context);
11909     break;
11910   case llvm::omp::OMPC_align:
11911     C = new (Context) OMPAlignClause();
11912     break;
11913 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11914   case llvm::omp::Enum:                                                        \
11915     break;
11916 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11917   default:
11918     break;
11919   }
11920   assert(C && "Unknown OMPClause type");
11921 
11922   Visit(C);
11923   C->setLocStart(Record.readSourceLocation());
11924   C->setLocEnd(Record.readSourceLocation());
11925 
11926   return C;
11927 }
11928 
11929 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11930   C->setPreInitStmt(Record.readSubStmt(),
11931                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11932 }
11933 
11934 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11935   VisitOMPClauseWithPreInit(C);
11936   C->setPostUpdateExpr(Record.readSubExpr());
11937 }
11938 
11939 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11940   VisitOMPClauseWithPreInit(C);
11941   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11942   C->setNameModifierLoc(Record.readSourceLocation());
11943   C->setColonLoc(Record.readSourceLocation());
11944   C->setCondition(Record.readSubExpr());
11945   C->setLParenLoc(Record.readSourceLocation());
11946 }
11947 
11948 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11949   VisitOMPClauseWithPreInit(C);
11950   C->setCondition(Record.readSubExpr());
11951   C->setLParenLoc(Record.readSourceLocation());
11952 }
11953 
11954 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11955   VisitOMPClauseWithPreInit(C);
11956   C->setNumThreads(Record.readSubExpr());
11957   C->setLParenLoc(Record.readSourceLocation());
11958 }
11959 
11960 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11961   C->setSafelen(Record.readSubExpr());
11962   C->setLParenLoc(Record.readSourceLocation());
11963 }
11964 
11965 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11966   C->setSimdlen(Record.readSubExpr());
11967   C->setLParenLoc(Record.readSourceLocation());
11968 }
11969 
11970 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11971   for (Expr *&E : C->getSizesRefs())
11972     E = Record.readSubExpr();
11973   C->setLParenLoc(Record.readSourceLocation());
11974 }
11975 
11976 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11977 
11978 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11979   C->setFactor(Record.readSubExpr());
11980   C->setLParenLoc(Record.readSourceLocation());
11981 }
11982 
11983 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11984   C->setAllocator(Record.readExpr());
11985   C->setLParenLoc(Record.readSourceLocation());
11986 }
11987 
11988 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11989   C->setNumForLoops(Record.readSubExpr());
11990   C->setLParenLoc(Record.readSourceLocation());
11991 }
11992 
11993 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11994   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11995   C->setLParenLoc(Record.readSourceLocation());
11996   C->setDefaultKindKwLoc(Record.readSourceLocation());
11997 }
11998 
11999 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12000   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12001   C->setLParenLoc(Record.readSourceLocation());
12002   C->setProcBindKindKwLoc(Record.readSourceLocation());
12003 }
12004 
12005 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12006   VisitOMPClauseWithPreInit(C);
12007   C->setScheduleKind(
12008        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12009   C->setFirstScheduleModifier(
12010       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12011   C->setSecondScheduleModifier(
12012       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12013   C->setChunkSize(Record.readSubExpr());
12014   C->setLParenLoc(Record.readSourceLocation());
12015   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12016   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12017   C->setScheduleKindLoc(Record.readSourceLocation());
12018   C->setCommaLoc(Record.readSourceLocation());
12019 }
12020 
12021 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12022   C->setNumForLoops(Record.readSubExpr());
12023   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12024     C->setLoopNumIterations(I, Record.readSubExpr());
12025   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12026     C->setLoopCounter(I, Record.readSubExpr());
12027   C->setLParenLoc(Record.readSourceLocation());
12028 }
12029 
12030 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12031   C->setEventHandler(Record.readSubExpr());
12032   C->setLParenLoc(Record.readSourceLocation());
12033 }
12034 
12035 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12036 
12037 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12038 
12039 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12040 
12041 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12042 
12043 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12044 
12045 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12046   if (C->isExtended()) {
12047     C->setLParenLoc(Record.readSourceLocation());
12048     C->setArgumentLoc(Record.readSourceLocation());
12049     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12050   }
12051 }
12052 
12053 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12054 
12055 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12056 
12057 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12058 
12059 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12060 
12061 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12062 
12063 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12064 
12065 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12066 
12067 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12068 
12069 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12070 
12071 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12072 
12073 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12074   unsigned NumVars = C->varlist_size();
12075   SmallVector<Expr *, 16> Vars;
12076   Vars.reserve(NumVars);
12077   for (unsigned I = 0; I != NumVars; ++I)
12078     Vars.push_back(Record.readSubExpr());
12079   C->setVarRefs(Vars);
12080   C->setIsTarget(Record.readBool());
12081   C->setIsTargetSync(Record.readBool());
12082   C->setLParenLoc(Record.readSourceLocation());
12083   C->setVarLoc(Record.readSourceLocation());
12084 }
12085 
12086 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12087   C->setInteropVar(Record.readSubExpr());
12088   C->setLParenLoc(Record.readSourceLocation());
12089   C->setVarLoc(Record.readSourceLocation());
12090 }
12091 
12092 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12093   C->setInteropVar(Record.readSubExpr());
12094   C->setLParenLoc(Record.readSourceLocation());
12095   C->setVarLoc(Record.readSourceLocation());
12096 }
12097 
12098 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12099   VisitOMPClauseWithPreInit(C);
12100   C->setCondition(Record.readSubExpr());
12101   C->setLParenLoc(Record.readSourceLocation());
12102 }
12103 
12104 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12105   VisitOMPClauseWithPreInit(C);
12106   C->setCondition(Record.readSubExpr());
12107   C->setLParenLoc(Record.readSourceLocation());
12108 }
12109 
12110 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12111 
12112 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12113     OMPUnifiedSharedMemoryClause *) {}
12114 
12115 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12116 
12117 void
12118 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12119 }
12120 
12121 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12122     OMPAtomicDefaultMemOrderClause *C) {
12123   C->setAtomicDefaultMemOrderKind(
12124       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12125   C->setLParenLoc(Record.readSourceLocation());
12126   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12127 }
12128 
12129 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12130   C->setLParenLoc(Record.readSourceLocation());
12131   unsigned NumVars = C->varlist_size();
12132   SmallVector<Expr *, 16> Vars;
12133   Vars.reserve(NumVars);
12134   for (unsigned i = 0; i != NumVars; ++i)
12135     Vars.push_back(Record.readSubExpr());
12136   C->setVarRefs(Vars);
12137   Vars.clear();
12138   for (unsigned i = 0; i != NumVars; ++i)
12139     Vars.push_back(Record.readSubExpr());
12140   C->setPrivateCopies(Vars);
12141 }
12142 
12143 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12144   VisitOMPClauseWithPreInit(C);
12145   C->setLParenLoc(Record.readSourceLocation());
12146   unsigned NumVars = C->varlist_size();
12147   SmallVector<Expr *, 16> Vars;
12148   Vars.reserve(NumVars);
12149   for (unsigned i = 0; i != NumVars; ++i)
12150     Vars.push_back(Record.readSubExpr());
12151   C->setVarRefs(Vars);
12152   Vars.clear();
12153   for (unsigned i = 0; i != NumVars; ++i)
12154     Vars.push_back(Record.readSubExpr());
12155   C->setPrivateCopies(Vars);
12156   Vars.clear();
12157   for (unsigned i = 0; i != NumVars; ++i)
12158     Vars.push_back(Record.readSubExpr());
12159   C->setInits(Vars);
12160 }
12161 
12162 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12163   VisitOMPClauseWithPostUpdate(C);
12164   C->setLParenLoc(Record.readSourceLocation());
12165   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12166   C->setKindLoc(Record.readSourceLocation());
12167   C->setColonLoc(Record.readSourceLocation());
12168   unsigned NumVars = C->varlist_size();
12169   SmallVector<Expr *, 16> Vars;
12170   Vars.reserve(NumVars);
12171   for (unsigned i = 0; i != NumVars; ++i)
12172     Vars.push_back(Record.readSubExpr());
12173   C->setVarRefs(Vars);
12174   Vars.clear();
12175   for (unsigned i = 0; i != NumVars; ++i)
12176     Vars.push_back(Record.readSubExpr());
12177   C->setPrivateCopies(Vars);
12178   Vars.clear();
12179   for (unsigned i = 0; i != NumVars; ++i)
12180     Vars.push_back(Record.readSubExpr());
12181   C->setSourceExprs(Vars);
12182   Vars.clear();
12183   for (unsigned i = 0; i != NumVars; ++i)
12184     Vars.push_back(Record.readSubExpr());
12185   C->setDestinationExprs(Vars);
12186   Vars.clear();
12187   for (unsigned i = 0; i != NumVars; ++i)
12188     Vars.push_back(Record.readSubExpr());
12189   C->setAssignmentOps(Vars);
12190 }
12191 
12192 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12193   C->setLParenLoc(Record.readSourceLocation());
12194   unsigned NumVars = C->varlist_size();
12195   SmallVector<Expr *, 16> Vars;
12196   Vars.reserve(NumVars);
12197   for (unsigned i = 0; i != NumVars; ++i)
12198     Vars.push_back(Record.readSubExpr());
12199   C->setVarRefs(Vars);
12200 }
12201 
12202 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12203   VisitOMPClauseWithPostUpdate(C);
12204   C->setLParenLoc(Record.readSourceLocation());
12205   C->setModifierLoc(Record.readSourceLocation());
12206   C->setColonLoc(Record.readSourceLocation());
12207   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12208   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12209   C->setQualifierLoc(NNSL);
12210   C->setNameInfo(DNI);
12211 
12212   unsigned NumVars = C->varlist_size();
12213   SmallVector<Expr *, 16> Vars;
12214   Vars.reserve(NumVars);
12215   for (unsigned i = 0; i != NumVars; ++i)
12216     Vars.push_back(Record.readSubExpr());
12217   C->setVarRefs(Vars);
12218   Vars.clear();
12219   for (unsigned i = 0; i != NumVars; ++i)
12220     Vars.push_back(Record.readSubExpr());
12221   C->setPrivates(Vars);
12222   Vars.clear();
12223   for (unsigned i = 0; i != NumVars; ++i)
12224     Vars.push_back(Record.readSubExpr());
12225   C->setLHSExprs(Vars);
12226   Vars.clear();
12227   for (unsigned i = 0; i != NumVars; ++i)
12228     Vars.push_back(Record.readSubExpr());
12229   C->setRHSExprs(Vars);
12230   Vars.clear();
12231   for (unsigned i = 0; i != NumVars; ++i)
12232     Vars.push_back(Record.readSubExpr());
12233   C->setReductionOps(Vars);
12234   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12235     Vars.clear();
12236     for (unsigned i = 0; i != NumVars; ++i)
12237       Vars.push_back(Record.readSubExpr());
12238     C->setInscanCopyOps(Vars);
12239     Vars.clear();
12240     for (unsigned i = 0; i != NumVars; ++i)
12241       Vars.push_back(Record.readSubExpr());
12242     C->setInscanCopyArrayTemps(Vars);
12243     Vars.clear();
12244     for (unsigned i = 0; i != NumVars; ++i)
12245       Vars.push_back(Record.readSubExpr());
12246     C->setInscanCopyArrayElems(Vars);
12247   }
12248 }
12249 
12250 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12251   VisitOMPClauseWithPostUpdate(C);
12252   C->setLParenLoc(Record.readSourceLocation());
12253   C->setColonLoc(Record.readSourceLocation());
12254   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12255   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12256   C->setQualifierLoc(NNSL);
12257   C->setNameInfo(DNI);
12258 
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   Vars.clear();
12266   for (unsigned I = 0; I != NumVars; ++I)
12267     Vars.push_back(Record.readSubExpr());
12268   C->setPrivates(Vars);
12269   Vars.clear();
12270   for (unsigned I = 0; I != NumVars; ++I)
12271     Vars.push_back(Record.readSubExpr());
12272   C->setLHSExprs(Vars);
12273   Vars.clear();
12274   for (unsigned I = 0; I != NumVars; ++I)
12275     Vars.push_back(Record.readSubExpr());
12276   C->setRHSExprs(Vars);
12277   Vars.clear();
12278   for (unsigned I = 0; I != NumVars; ++I)
12279     Vars.push_back(Record.readSubExpr());
12280   C->setReductionOps(Vars);
12281 }
12282 
12283 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12284   VisitOMPClauseWithPostUpdate(C);
12285   C->setLParenLoc(Record.readSourceLocation());
12286   C->setColonLoc(Record.readSourceLocation());
12287   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12288   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12289   C->setQualifierLoc(NNSL);
12290   C->setNameInfo(DNI);
12291 
12292   unsigned NumVars = C->varlist_size();
12293   SmallVector<Expr *, 16> Vars;
12294   Vars.reserve(NumVars);
12295   for (unsigned I = 0; I != NumVars; ++I)
12296     Vars.push_back(Record.readSubExpr());
12297   C->setVarRefs(Vars);
12298   Vars.clear();
12299   for (unsigned I = 0; I != NumVars; ++I)
12300     Vars.push_back(Record.readSubExpr());
12301   C->setPrivates(Vars);
12302   Vars.clear();
12303   for (unsigned I = 0; I != NumVars; ++I)
12304     Vars.push_back(Record.readSubExpr());
12305   C->setLHSExprs(Vars);
12306   Vars.clear();
12307   for (unsigned I = 0; I != NumVars; ++I)
12308     Vars.push_back(Record.readSubExpr());
12309   C->setRHSExprs(Vars);
12310   Vars.clear();
12311   for (unsigned I = 0; I != NumVars; ++I)
12312     Vars.push_back(Record.readSubExpr());
12313   C->setReductionOps(Vars);
12314   Vars.clear();
12315   for (unsigned I = 0; I != NumVars; ++I)
12316     Vars.push_back(Record.readSubExpr());
12317   C->setTaskgroupDescriptors(Vars);
12318 }
12319 
12320 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12321   VisitOMPClauseWithPostUpdate(C);
12322   C->setLParenLoc(Record.readSourceLocation());
12323   C->setColonLoc(Record.readSourceLocation());
12324   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12325   C->setModifierLoc(Record.readSourceLocation());
12326   unsigned NumVars = C->varlist_size();
12327   SmallVector<Expr *, 16> Vars;
12328   Vars.reserve(NumVars);
12329   for (unsigned i = 0; i != NumVars; ++i)
12330     Vars.push_back(Record.readSubExpr());
12331   C->setVarRefs(Vars);
12332   Vars.clear();
12333   for (unsigned i = 0; i != NumVars; ++i)
12334     Vars.push_back(Record.readSubExpr());
12335   C->setPrivates(Vars);
12336   Vars.clear();
12337   for (unsigned i = 0; i != NumVars; ++i)
12338     Vars.push_back(Record.readSubExpr());
12339   C->setInits(Vars);
12340   Vars.clear();
12341   for (unsigned i = 0; i != NumVars; ++i)
12342     Vars.push_back(Record.readSubExpr());
12343   C->setUpdates(Vars);
12344   Vars.clear();
12345   for (unsigned i = 0; i != NumVars; ++i)
12346     Vars.push_back(Record.readSubExpr());
12347   C->setFinals(Vars);
12348   C->setStep(Record.readSubExpr());
12349   C->setCalcStep(Record.readSubExpr());
12350   Vars.clear();
12351   for (unsigned I = 0; I != NumVars + 1; ++I)
12352     Vars.push_back(Record.readSubExpr());
12353   C->setUsedExprs(Vars);
12354 }
12355 
12356 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12357   C->setLParenLoc(Record.readSourceLocation());
12358   C->setColonLoc(Record.readSourceLocation());
12359   unsigned NumVars = C->varlist_size();
12360   SmallVector<Expr *, 16> Vars;
12361   Vars.reserve(NumVars);
12362   for (unsigned i = 0; i != NumVars; ++i)
12363     Vars.push_back(Record.readSubExpr());
12364   C->setVarRefs(Vars);
12365   C->setAlignment(Record.readSubExpr());
12366 }
12367 
12368 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12369   C->setLParenLoc(Record.readSourceLocation());
12370   unsigned NumVars = C->varlist_size();
12371   SmallVector<Expr *, 16> Exprs;
12372   Exprs.reserve(NumVars);
12373   for (unsigned i = 0; i != NumVars; ++i)
12374     Exprs.push_back(Record.readSubExpr());
12375   C->setVarRefs(Exprs);
12376   Exprs.clear();
12377   for (unsigned i = 0; i != NumVars; ++i)
12378     Exprs.push_back(Record.readSubExpr());
12379   C->setSourceExprs(Exprs);
12380   Exprs.clear();
12381   for (unsigned i = 0; i != NumVars; ++i)
12382     Exprs.push_back(Record.readSubExpr());
12383   C->setDestinationExprs(Exprs);
12384   Exprs.clear();
12385   for (unsigned i = 0; i != NumVars; ++i)
12386     Exprs.push_back(Record.readSubExpr());
12387   C->setAssignmentOps(Exprs);
12388 }
12389 
12390 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12391   C->setLParenLoc(Record.readSourceLocation());
12392   unsigned NumVars = C->varlist_size();
12393   SmallVector<Expr *, 16> Exprs;
12394   Exprs.reserve(NumVars);
12395   for (unsigned i = 0; i != NumVars; ++i)
12396     Exprs.push_back(Record.readSubExpr());
12397   C->setVarRefs(Exprs);
12398   Exprs.clear();
12399   for (unsigned i = 0; i != NumVars; ++i)
12400     Exprs.push_back(Record.readSubExpr());
12401   C->setSourceExprs(Exprs);
12402   Exprs.clear();
12403   for (unsigned i = 0; i != NumVars; ++i)
12404     Exprs.push_back(Record.readSubExpr());
12405   C->setDestinationExprs(Exprs);
12406   Exprs.clear();
12407   for (unsigned i = 0; i != NumVars; ++i)
12408     Exprs.push_back(Record.readSubExpr());
12409   C->setAssignmentOps(Exprs);
12410 }
12411 
12412 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12413   C->setLParenLoc(Record.readSourceLocation());
12414   unsigned NumVars = C->varlist_size();
12415   SmallVector<Expr *, 16> Vars;
12416   Vars.reserve(NumVars);
12417   for (unsigned i = 0; i != NumVars; ++i)
12418     Vars.push_back(Record.readSubExpr());
12419   C->setVarRefs(Vars);
12420 }
12421 
12422 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12423   C->setDepobj(Record.readSubExpr());
12424   C->setLParenLoc(Record.readSourceLocation());
12425 }
12426 
12427 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12428   C->setLParenLoc(Record.readSourceLocation());
12429   C->setModifier(Record.readSubExpr());
12430   C->setDependencyKind(
12431       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12432   C->setDependencyLoc(Record.readSourceLocation());
12433   C->setColonLoc(Record.readSourceLocation());
12434   unsigned NumVars = C->varlist_size();
12435   SmallVector<Expr *, 16> Vars;
12436   Vars.reserve(NumVars);
12437   for (unsigned I = 0; I != NumVars; ++I)
12438     Vars.push_back(Record.readSubExpr());
12439   C->setVarRefs(Vars);
12440   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12441     C->setLoopData(I, Record.readSubExpr());
12442 }
12443 
12444 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12445   VisitOMPClauseWithPreInit(C);
12446   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12447   C->setDevice(Record.readSubExpr());
12448   C->setModifierLoc(Record.readSourceLocation());
12449   C->setLParenLoc(Record.readSourceLocation());
12450 }
12451 
12452 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12453   C->setLParenLoc(Record.readSourceLocation());
12454   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12455     C->setMapTypeModifier(
12456         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12457     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12458   }
12459   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12460   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12461   C->setMapType(
12462      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12463   C->setMapLoc(Record.readSourceLocation());
12464   C->setColonLoc(Record.readSourceLocation());
12465   auto NumVars = C->varlist_size();
12466   auto UniqueDecls = C->getUniqueDeclarationsNum();
12467   auto TotalLists = C->getTotalComponentListNum();
12468   auto TotalComponents = C->getTotalComponentsNum();
12469 
12470   SmallVector<Expr *, 16> Vars;
12471   Vars.reserve(NumVars);
12472   for (unsigned i = 0; i != NumVars; ++i)
12473     Vars.push_back(Record.readExpr());
12474   C->setVarRefs(Vars);
12475 
12476   SmallVector<Expr *, 16> UDMappers;
12477   UDMappers.reserve(NumVars);
12478   for (unsigned I = 0; I < NumVars; ++I)
12479     UDMappers.push_back(Record.readExpr());
12480   C->setUDMapperRefs(UDMappers);
12481 
12482   SmallVector<ValueDecl *, 16> Decls;
12483   Decls.reserve(UniqueDecls);
12484   for (unsigned i = 0; i < UniqueDecls; ++i)
12485     Decls.push_back(Record.readDeclAs<ValueDecl>());
12486   C->setUniqueDecls(Decls);
12487 
12488   SmallVector<unsigned, 16> ListsPerDecl;
12489   ListsPerDecl.reserve(UniqueDecls);
12490   for (unsigned i = 0; i < UniqueDecls; ++i)
12491     ListsPerDecl.push_back(Record.readInt());
12492   C->setDeclNumLists(ListsPerDecl);
12493 
12494   SmallVector<unsigned, 32> ListSizes;
12495   ListSizes.reserve(TotalLists);
12496   for (unsigned i = 0; i < TotalLists; ++i)
12497     ListSizes.push_back(Record.readInt());
12498   C->setComponentListSizes(ListSizes);
12499 
12500   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12501   Components.reserve(TotalComponents);
12502   for (unsigned i = 0; i < TotalComponents; ++i) {
12503     Expr *AssociatedExprPr = Record.readExpr();
12504     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12505     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12506                             /*IsNonContiguous=*/false);
12507   }
12508   C->setComponents(Components, ListSizes);
12509 }
12510 
12511 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12512   C->setLParenLoc(Record.readSourceLocation());
12513   C->setColonLoc(Record.readSourceLocation());
12514   C->setAllocator(Record.readSubExpr());
12515   unsigned NumVars = C->varlist_size();
12516   SmallVector<Expr *, 16> Vars;
12517   Vars.reserve(NumVars);
12518   for (unsigned i = 0; i != NumVars; ++i)
12519     Vars.push_back(Record.readSubExpr());
12520   C->setVarRefs(Vars);
12521 }
12522 
12523 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12524   VisitOMPClauseWithPreInit(C);
12525   C->setNumTeams(Record.readSubExpr());
12526   C->setLParenLoc(Record.readSourceLocation());
12527 }
12528 
12529 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12530   VisitOMPClauseWithPreInit(C);
12531   C->setThreadLimit(Record.readSubExpr());
12532   C->setLParenLoc(Record.readSourceLocation());
12533 }
12534 
12535 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12536   VisitOMPClauseWithPreInit(C);
12537   C->setPriority(Record.readSubExpr());
12538   C->setLParenLoc(Record.readSourceLocation());
12539 }
12540 
12541 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12542   VisitOMPClauseWithPreInit(C);
12543   C->setGrainsize(Record.readSubExpr());
12544   C->setLParenLoc(Record.readSourceLocation());
12545 }
12546 
12547 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12548   VisitOMPClauseWithPreInit(C);
12549   C->setNumTasks(Record.readSubExpr());
12550   C->setLParenLoc(Record.readSourceLocation());
12551 }
12552 
12553 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12554   C->setHint(Record.readSubExpr());
12555   C->setLParenLoc(Record.readSourceLocation());
12556 }
12557 
12558 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12559   VisitOMPClauseWithPreInit(C);
12560   C->setDistScheduleKind(
12561       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12562   C->setChunkSize(Record.readSubExpr());
12563   C->setLParenLoc(Record.readSourceLocation());
12564   C->setDistScheduleKindLoc(Record.readSourceLocation());
12565   C->setCommaLoc(Record.readSourceLocation());
12566 }
12567 
12568 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12569   C->setDefaultmapKind(
12570        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12571   C->setDefaultmapModifier(
12572       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12573   C->setLParenLoc(Record.readSourceLocation());
12574   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12575   C->setDefaultmapKindLoc(Record.readSourceLocation());
12576 }
12577 
12578 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12579   C->setLParenLoc(Record.readSourceLocation());
12580   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12581     C->setMotionModifier(
12582         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12583     C->setMotionModifierLoc(I, Record.readSourceLocation());
12584   }
12585   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12586   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12587   C->setColonLoc(Record.readSourceLocation());
12588   auto NumVars = C->varlist_size();
12589   auto UniqueDecls = C->getUniqueDeclarationsNum();
12590   auto TotalLists = C->getTotalComponentListNum();
12591   auto TotalComponents = C->getTotalComponentsNum();
12592 
12593   SmallVector<Expr *, 16> Vars;
12594   Vars.reserve(NumVars);
12595   for (unsigned i = 0; i != NumVars; ++i)
12596     Vars.push_back(Record.readSubExpr());
12597   C->setVarRefs(Vars);
12598 
12599   SmallVector<Expr *, 16> UDMappers;
12600   UDMappers.reserve(NumVars);
12601   for (unsigned I = 0; I < NumVars; ++I)
12602     UDMappers.push_back(Record.readSubExpr());
12603   C->setUDMapperRefs(UDMappers);
12604 
12605   SmallVector<ValueDecl *, 16> Decls;
12606   Decls.reserve(UniqueDecls);
12607   for (unsigned i = 0; i < UniqueDecls; ++i)
12608     Decls.push_back(Record.readDeclAs<ValueDecl>());
12609   C->setUniqueDecls(Decls);
12610 
12611   SmallVector<unsigned, 16> ListsPerDecl;
12612   ListsPerDecl.reserve(UniqueDecls);
12613   for (unsigned i = 0; i < UniqueDecls; ++i)
12614     ListsPerDecl.push_back(Record.readInt());
12615   C->setDeclNumLists(ListsPerDecl);
12616 
12617   SmallVector<unsigned, 32> ListSizes;
12618   ListSizes.reserve(TotalLists);
12619   for (unsigned i = 0; i < TotalLists; ++i)
12620     ListSizes.push_back(Record.readInt());
12621   C->setComponentListSizes(ListSizes);
12622 
12623   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12624   Components.reserve(TotalComponents);
12625   for (unsigned i = 0; i < TotalComponents; ++i) {
12626     Expr *AssociatedExprPr = Record.readSubExpr();
12627     bool IsNonContiguous = Record.readBool();
12628     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12629     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12630   }
12631   C->setComponents(Components, ListSizes);
12632 }
12633 
12634 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12635   C->setLParenLoc(Record.readSourceLocation());
12636   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12637     C->setMotionModifier(
12638         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12639     C->setMotionModifierLoc(I, Record.readSourceLocation());
12640   }
12641   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12642   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12643   C->setColonLoc(Record.readSourceLocation());
12644   auto NumVars = C->varlist_size();
12645   auto UniqueDecls = C->getUniqueDeclarationsNum();
12646   auto TotalLists = C->getTotalComponentListNum();
12647   auto TotalComponents = C->getTotalComponentsNum();
12648 
12649   SmallVector<Expr *, 16> Vars;
12650   Vars.reserve(NumVars);
12651   for (unsigned i = 0; i != NumVars; ++i)
12652     Vars.push_back(Record.readSubExpr());
12653   C->setVarRefs(Vars);
12654 
12655   SmallVector<Expr *, 16> UDMappers;
12656   UDMappers.reserve(NumVars);
12657   for (unsigned I = 0; I < NumVars; ++I)
12658     UDMappers.push_back(Record.readSubExpr());
12659   C->setUDMapperRefs(UDMappers);
12660 
12661   SmallVector<ValueDecl *, 16> Decls;
12662   Decls.reserve(UniqueDecls);
12663   for (unsigned i = 0; i < UniqueDecls; ++i)
12664     Decls.push_back(Record.readDeclAs<ValueDecl>());
12665   C->setUniqueDecls(Decls);
12666 
12667   SmallVector<unsigned, 16> ListsPerDecl;
12668   ListsPerDecl.reserve(UniqueDecls);
12669   for (unsigned i = 0; i < UniqueDecls; ++i)
12670     ListsPerDecl.push_back(Record.readInt());
12671   C->setDeclNumLists(ListsPerDecl);
12672 
12673   SmallVector<unsigned, 32> ListSizes;
12674   ListSizes.reserve(TotalLists);
12675   for (unsigned i = 0; i < TotalLists; ++i)
12676     ListSizes.push_back(Record.readInt());
12677   C->setComponentListSizes(ListSizes);
12678 
12679   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12680   Components.reserve(TotalComponents);
12681   for (unsigned i = 0; i < TotalComponents; ++i) {
12682     Expr *AssociatedExprPr = Record.readSubExpr();
12683     bool IsNonContiguous = Record.readBool();
12684     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12685     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12686   }
12687   C->setComponents(Components, ListSizes);
12688 }
12689 
12690 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12691   C->setLParenLoc(Record.readSourceLocation());
12692   auto NumVars = C->varlist_size();
12693   auto UniqueDecls = C->getUniqueDeclarationsNum();
12694   auto TotalLists = C->getTotalComponentListNum();
12695   auto TotalComponents = C->getTotalComponentsNum();
12696 
12697   SmallVector<Expr *, 16> Vars;
12698   Vars.reserve(NumVars);
12699   for (unsigned i = 0; i != NumVars; ++i)
12700     Vars.push_back(Record.readSubExpr());
12701   C->setVarRefs(Vars);
12702   Vars.clear();
12703   for (unsigned i = 0; i != NumVars; ++i)
12704     Vars.push_back(Record.readSubExpr());
12705   C->setPrivateCopies(Vars);
12706   Vars.clear();
12707   for (unsigned i = 0; i != NumVars; ++i)
12708     Vars.push_back(Record.readSubExpr());
12709   C->setInits(Vars);
12710 
12711   SmallVector<ValueDecl *, 16> Decls;
12712   Decls.reserve(UniqueDecls);
12713   for (unsigned i = 0; i < UniqueDecls; ++i)
12714     Decls.push_back(Record.readDeclAs<ValueDecl>());
12715   C->setUniqueDecls(Decls);
12716 
12717   SmallVector<unsigned, 16> ListsPerDecl;
12718   ListsPerDecl.reserve(UniqueDecls);
12719   for (unsigned i = 0; i < UniqueDecls; ++i)
12720     ListsPerDecl.push_back(Record.readInt());
12721   C->setDeclNumLists(ListsPerDecl);
12722 
12723   SmallVector<unsigned, 32> ListSizes;
12724   ListSizes.reserve(TotalLists);
12725   for (unsigned i = 0; i < TotalLists; ++i)
12726     ListSizes.push_back(Record.readInt());
12727   C->setComponentListSizes(ListSizes);
12728 
12729   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12730   Components.reserve(TotalComponents);
12731   for (unsigned i = 0; i < TotalComponents; ++i) {
12732     auto *AssociatedExprPr = Record.readSubExpr();
12733     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12734     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12735                             /*IsNonContiguous=*/false);
12736   }
12737   C->setComponents(Components, ListSizes);
12738 }
12739 
12740 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12741   C->setLParenLoc(Record.readSourceLocation());
12742   auto NumVars = C->varlist_size();
12743   auto UniqueDecls = C->getUniqueDeclarationsNum();
12744   auto TotalLists = C->getTotalComponentListNum();
12745   auto TotalComponents = C->getTotalComponentsNum();
12746 
12747   SmallVector<Expr *, 16> Vars;
12748   Vars.reserve(NumVars);
12749   for (unsigned i = 0; i != NumVars; ++i)
12750     Vars.push_back(Record.readSubExpr());
12751   C->setVarRefs(Vars);
12752 
12753   SmallVector<ValueDecl *, 16> Decls;
12754   Decls.reserve(UniqueDecls);
12755   for (unsigned i = 0; i < UniqueDecls; ++i)
12756     Decls.push_back(Record.readDeclAs<ValueDecl>());
12757   C->setUniqueDecls(Decls);
12758 
12759   SmallVector<unsigned, 16> ListsPerDecl;
12760   ListsPerDecl.reserve(UniqueDecls);
12761   for (unsigned i = 0; i < UniqueDecls; ++i)
12762     ListsPerDecl.push_back(Record.readInt());
12763   C->setDeclNumLists(ListsPerDecl);
12764 
12765   SmallVector<unsigned, 32> ListSizes;
12766   ListSizes.reserve(TotalLists);
12767   for (unsigned i = 0; i < TotalLists; ++i)
12768     ListSizes.push_back(Record.readInt());
12769   C->setComponentListSizes(ListSizes);
12770 
12771   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12772   Components.reserve(TotalComponents);
12773   for (unsigned i = 0; i < TotalComponents; ++i) {
12774     Expr *AssociatedExpr = Record.readSubExpr();
12775     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12776     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12777                             /*IsNonContiguous*/ false);
12778   }
12779   C->setComponents(Components, ListSizes);
12780 }
12781 
12782 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12783   C->setLParenLoc(Record.readSourceLocation());
12784   auto NumVars = C->varlist_size();
12785   auto UniqueDecls = C->getUniqueDeclarationsNum();
12786   auto TotalLists = C->getTotalComponentListNum();
12787   auto TotalComponents = C->getTotalComponentsNum();
12788 
12789   SmallVector<Expr *, 16> Vars;
12790   Vars.reserve(NumVars);
12791   for (unsigned i = 0; i != NumVars; ++i)
12792     Vars.push_back(Record.readSubExpr());
12793   C->setVarRefs(Vars);
12794   Vars.clear();
12795 
12796   SmallVector<ValueDecl *, 16> Decls;
12797   Decls.reserve(UniqueDecls);
12798   for (unsigned i = 0; i < UniqueDecls; ++i)
12799     Decls.push_back(Record.readDeclAs<ValueDecl>());
12800   C->setUniqueDecls(Decls);
12801 
12802   SmallVector<unsigned, 16> ListsPerDecl;
12803   ListsPerDecl.reserve(UniqueDecls);
12804   for (unsigned i = 0; i < UniqueDecls; ++i)
12805     ListsPerDecl.push_back(Record.readInt());
12806   C->setDeclNumLists(ListsPerDecl);
12807 
12808   SmallVector<unsigned, 32> ListSizes;
12809   ListSizes.reserve(TotalLists);
12810   for (unsigned i = 0; i < TotalLists; ++i)
12811     ListSizes.push_back(Record.readInt());
12812   C->setComponentListSizes(ListSizes);
12813 
12814   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12815   Components.reserve(TotalComponents);
12816   for (unsigned i = 0; i < TotalComponents; ++i) {
12817     Expr *AssociatedExpr = Record.readSubExpr();
12818     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12819     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12820                             /*IsNonContiguous=*/false);
12821   }
12822   C->setComponents(Components, ListSizes);
12823 }
12824 
12825 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12826   C->setLParenLoc(Record.readSourceLocation());
12827   unsigned NumVars = C->varlist_size();
12828   SmallVector<Expr *, 16> Vars;
12829   Vars.reserve(NumVars);
12830   for (unsigned i = 0; i != NumVars; ++i)
12831     Vars.push_back(Record.readSubExpr());
12832   C->setVarRefs(Vars);
12833   Vars.clear();
12834   Vars.reserve(NumVars);
12835   for (unsigned i = 0; i != NumVars; ++i)
12836     Vars.push_back(Record.readSubExpr());
12837   C->setPrivateRefs(Vars);
12838 }
12839 
12840 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12841   C->setLParenLoc(Record.readSourceLocation());
12842   unsigned NumVars = C->varlist_size();
12843   SmallVector<Expr *, 16> Vars;
12844   Vars.reserve(NumVars);
12845   for (unsigned i = 0; i != NumVars; ++i)
12846     Vars.push_back(Record.readSubExpr());
12847   C->setVarRefs(Vars);
12848 }
12849 
12850 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12851   C->setLParenLoc(Record.readSourceLocation());
12852   unsigned NumVars = C->varlist_size();
12853   SmallVector<Expr *, 16> Vars;
12854   Vars.reserve(NumVars);
12855   for (unsigned i = 0; i != NumVars; ++i)
12856     Vars.push_back(Record.readSubExpr());
12857   C->setVarRefs(Vars);
12858 }
12859 
12860 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12861   C->setLParenLoc(Record.readSourceLocation());
12862   unsigned NumOfAllocators = C->getNumberOfAllocators();
12863   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12864   Data.reserve(NumOfAllocators);
12865   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12866     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12867     D.Allocator = Record.readSubExpr();
12868     D.AllocatorTraits = Record.readSubExpr();
12869     D.LParenLoc = Record.readSourceLocation();
12870     D.RParenLoc = Record.readSourceLocation();
12871   }
12872   C->setAllocatorsData(Data);
12873 }
12874 
12875 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12876   C->setLParenLoc(Record.readSourceLocation());
12877   C->setModifier(Record.readSubExpr());
12878   C->setColonLoc(Record.readSourceLocation());
12879   unsigned NumOfLocators = C->varlist_size();
12880   SmallVector<Expr *, 4> Locators;
12881   Locators.reserve(NumOfLocators);
12882   for (unsigned I = 0; I != NumOfLocators; ++I)
12883     Locators.push_back(Record.readSubExpr());
12884   C->setVarRefs(Locators);
12885 }
12886 
12887 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12888   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12889   C->setLParenLoc(Record.readSourceLocation());
12890   C->setKindKwLoc(Record.readSourceLocation());
12891 }
12892 
12893 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12894   VisitOMPClauseWithPreInit(C);
12895   C->setThreadID(Record.readSubExpr());
12896   C->setLParenLoc(Record.readSourceLocation());
12897 }
12898 
12899 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12900   C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12901   C->setLParenLoc(Record.readSourceLocation());
12902   C->setBindKindLoc(Record.readSourceLocation());
12903 }
12904 
12905 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12906   C->setAlignment(Record.readExpr());
12907   C->setLParenLoc(Record.readSourceLocation());
12908 }
12909 
12910 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12911   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12912   TI.Sets.resize(readUInt32());
12913   for (auto &Set : TI.Sets) {
12914     Set.Kind = readEnum<llvm::omp::TraitSet>();
12915     Set.Selectors.resize(readUInt32());
12916     for (auto &Selector : Set.Selectors) {
12917       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12918       Selector.ScoreOrCondition = nullptr;
12919       if (readBool())
12920         Selector.ScoreOrCondition = readExprRef();
12921       Selector.Properties.resize(readUInt32());
12922       for (auto &Property : Selector.Properties)
12923         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12924     }
12925   }
12926   return &TI;
12927 }
12928 
12929 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12930   if (!Data)
12931     return;
12932   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12933     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12934     skipInts(3);
12935   }
12936   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12937   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12938     Clauses[I] = readOMPClause();
12939   Data->setClauses(Clauses);
12940   if (Data->hasAssociatedStmt())
12941     Data->setAssociatedStmt(readStmt());
12942   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12943     Data->getChildren()[I] = readStmt();
12944 }
12945