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::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6699   TL.setNameLoc(readSourceLocation());
6700 }
6701 
6702 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6703                                             SubstTemplateTypeParmTypeLoc TL) {
6704   TL.setNameLoc(readSourceLocation());
6705 }
6706 
6707 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6708                                           SubstTemplateTypeParmPackTypeLoc TL) {
6709   TL.setNameLoc(readSourceLocation());
6710 }
6711 
6712 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6713                                            TemplateSpecializationTypeLoc TL) {
6714   TL.setTemplateKeywordLoc(readSourceLocation());
6715   TL.setTemplateNameLoc(readSourceLocation());
6716   TL.setLAngleLoc(readSourceLocation());
6717   TL.setRAngleLoc(readSourceLocation());
6718   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6719     TL.setArgLocInfo(
6720         i,
6721         Reader.readTemplateArgumentLocInfo(
6722           TL.getTypePtr()->getArg(i).getKind()));
6723 }
6724 
6725 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6726   TL.setLParenLoc(readSourceLocation());
6727   TL.setRParenLoc(readSourceLocation());
6728 }
6729 
6730 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6731   TL.setElaboratedKeywordLoc(readSourceLocation());
6732   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6733 }
6734 
6735 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6736   TL.setNameLoc(readSourceLocation());
6737 }
6738 
6739 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6740   TL.setElaboratedKeywordLoc(readSourceLocation());
6741   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6742   TL.setNameLoc(readSourceLocation());
6743 }
6744 
6745 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6746        DependentTemplateSpecializationTypeLoc TL) {
6747   TL.setElaboratedKeywordLoc(readSourceLocation());
6748   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6749   TL.setTemplateKeywordLoc(readSourceLocation());
6750   TL.setTemplateNameLoc(readSourceLocation());
6751   TL.setLAngleLoc(readSourceLocation());
6752   TL.setRAngleLoc(readSourceLocation());
6753   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6754     TL.setArgLocInfo(
6755         I,
6756         Reader.readTemplateArgumentLocInfo(
6757             TL.getTypePtr()->getArg(I).getKind()));
6758 }
6759 
6760 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6761   TL.setEllipsisLoc(readSourceLocation());
6762 }
6763 
6764 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6765   TL.setNameLoc(readSourceLocation());
6766 }
6767 
6768 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6769   if (TL.getNumProtocols()) {
6770     TL.setProtocolLAngleLoc(readSourceLocation());
6771     TL.setProtocolRAngleLoc(readSourceLocation());
6772   }
6773   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6774     TL.setProtocolLoc(i, readSourceLocation());
6775 }
6776 
6777 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6778   TL.setHasBaseTypeAsWritten(Reader.readBool());
6779   TL.setTypeArgsLAngleLoc(readSourceLocation());
6780   TL.setTypeArgsRAngleLoc(readSourceLocation());
6781   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6782     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6783   TL.setProtocolLAngleLoc(readSourceLocation());
6784   TL.setProtocolRAngleLoc(readSourceLocation());
6785   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6786     TL.setProtocolLoc(i, readSourceLocation());
6787 }
6788 
6789 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6790   TL.setStarLoc(readSourceLocation());
6791 }
6792 
6793 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6794   TL.setKWLoc(readSourceLocation());
6795   TL.setLParenLoc(readSourceLocation());
6796   TL.setRParenLoc(readSourceLocation());
6797 }
6798 
6799 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6800   TL.setKWLoc(readSourceLocation());
6801 }
6802 
6803 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
6804   TL.setNameLoc(readSourceLocation());
6805 }
6806 void TypeLocReader::VisitDependentBitIntTypeLoc(
6807     clang::DependentBitIntTypeLoc TL) {
6808   TL.setNameLoc(readSourceLocation());
6809 }
6810 
6811 
6812 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6813   TypeLocReader TLR(*this);
6814   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6815     TLR.Visit(TL);
6816 }
6817 
6818 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6819   QualType InfoTy = readType();
6820   if (InfoTy.isNull())
6821     return nullptr;
6822 
6823   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6824   readTypeLoc(TInfo->getTypeLoc());
6825   return TInfo;
6826 }
6827 
6828 QualType ASTReader::GetType(TypeID ID) {
6829   assert(ContextObj && "reading type with no AST context");
6830   ASTContext &Context = *ContextObj;
6831 
6832   unsigned FastQuals = ID & Qualifiers::FastMask;
6833   unsigned Index = ID >> Qualifiers::FastWidth;
6834 
6835   if (Index < NUM_PREDEF_TYPE_IDS) {
6836     QualType T;
6837     switch ((PredefinedTypeIDs)Index) {
6838     case PREDEF_TYPE_NULL_ID:
6839       return QualType();
6840     case PREDEF_TYPE_VOID_ID:
6841       T = Context.VoidTy;
6842       break;
6843     case PREDEF_TYPE_BOOL_ID:
6844       T = Context.BoolTy;
6845       break;
6846     case PREDEF_TYPE_CHAR_U_ID:
6847     case PREDEF_TYPE_CHAR_S_ID:
6848       // FIXME: Check that the signedness of CharTy is correct!
6849       T = Context.CharTy;
6850       break;
6851     case PREDEF_TYPE_UCHAR_ID:
6852       T = Context.UnsignedCharTy;
6853       break;
6854     case PREDEF_TYPE_USHORT_ID:
6855       T = Context.UnsignedShortTy;
6856       break;
6857     case PREDEF_TYPE_UINT_ID:
6858       T = Context.UnsignedIntTy;
6859       break;
6860     case PREDEF_TYPE_ULONG_ID:
6861       T = Context.UnsignedLongTy;
6862       break;
6863     case PREDEF_TYPE_ULONGLONG_ID:
6864       T = Context.UnsignedLongLongTy;
6865       break;
6866     case PREDEF_TYPE_UINT128_ID:
6867       T = Context.UnsignedInt128Ty;
6868       break;
6869     case PREDEF_TYPE_SCHAR_ID:
6870       T = Context.SignedCharTy;
6871       break;
6872     case PREDEF_TYPE_WCHAR_ID:
6873       T = Context.WCharTy;
6874       break;
6875     case PREDEF_TYPE_SHORT_ID:
6876       T = Context.ShortTy;
6877       break;
6878     case PREDEF_TYPE_INT_ID:
6879       T = Context.IntTy;
6880       break;
6881     case PREDEF_TYPE_LONG_ID:
6882       T = Context.LongTy;
6883       break;
6884     case PREDEF_TYPE_LONGLONG_ID:
6885       T = Context.LongLongTy;
6886       break;
6887     case PREDEF_TYPE_INT128_ID:
6888       T = Context.Int128Ty;
6889       break;
6890     case PREDEF_TYPE_BFLOAT16_ID:
6891       T = Context.BFloat16Ty;
6892       break;
6893     case PREDEF_TYPE_HALF_ID:
6894       T = Context.HalfTy;
6895       break;
6896     case PREDEF_TYPE_FLOAT_ID:
6897       T = Context.FloatTy;
6898       break;
6899     case PREDEF_TYPE_DOUBLE_ID:
6900       T = Context.DoubleTy;
6901       break;
6902     case PREDEF_TYPE_LONGDOUBLE_ID:
6903       T = Context.LongDoubleTy;
6904       break;
6905     case PREDEF_TYPE_SHORT_ACCUM_ID:
6906       T = Context.ShortAccumTy;
6907       break;
6908     case PREDEF_TYPE_ACCUM_ID:
6909       T = Context.AccumTy;
6910       break;
6911     case PREDEF_TYPE_LONG_ACCUM_ID:
6912       T = Context.LongAccumTy;
6913       break;
6914     case PREDEF_TYPE_USHORT_ACCUM_ID:
6915       T = Context.UnsignedShortAccumTy;
6916       break;
6917     case PREDEF_TYPE_UACCUM_ID:
6918       T = Context.UnsignedAccumTy;
6919       break;
6920     case PREDEF_TYPE_ULONG_ACCUM_ID:
6921       T = Context.UnsignedLongAccumTy;
6922       break;
6923     case PREDEF_TYPE_SHORT_FRACT_ID:
6924       T = Context.ShortFractTy;
6925       break;
6926     case PREDEF_TYPE_FRACT_ID:
6927       T = Context.FractTy;
6928       break;
6929     case PREDEF_TYPE_LONG_FRACT_ID:
6930       T = Context.LongFractTy;
6931       break;
6932     case PREDEF_TYPE_USHORT_FRACT_ID:
6933       T = Context.UnsignedShortFractTy;
6934       break;
6935     case PREDEF_TYPE_UFRACT_ID:
6936       T = Context.UnsignedFractTy;
6937       break;
6938     case PREDEF_TYPE_ULONG_FRACT_ID:
6939       T = Context.UnsignedLongFractTy;
6940       break;
6941     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6942       T = Context.SatShortAccumTy;
6943       break;
6944     case PREDEF_TYPE_SAT_ACCUM_ID:
6945       T = Context.SatAccumTy;
6946       break;
6947     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6948       T = Context.SatLongAccumTy;
6949       break;
6950     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6951       T = Context.SatUnsignedShortAccumTy;
6952       break;
6953     case PREDEF_TYPE_SAT_UACCUM_ID:
6954       T = Context.SatUnsignedAccumTy;
6955       break;
6956     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6957       T = Context.SatUnsignedLongAccumTy;
6958       break;
6959     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6960       T = Context.SatShortFractTy;
6961       break;
6962     case PREDEF_TYPE_SAT_FRACT_ID:
6963       T = Context.SatFractTy;
6964       break;
6965     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6966       T = Context.SatLongFractTy;
6967       break;
6968     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6969       T = Context.SatUnsignedShortFractTy;
6970       break;
6971     case PREDEF_TYPE_SAT_UFRACT_ID:
6972       T = Context.SatUnsignedFractTy;
6973       break;
6974     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6975       T = Context.SatUnsignedLongFractTy;
6976       break;
6977     case PREDEF_TYPE_FLOAT16_ID:
6978       T = Context.Float16Ty;
6979       break;
6980     case PREDEF_TYPE_FLOAT128_ID:
6981       T = Context.Float128Ty;
6982       break;
6983     case PREDEF_TYPE_IBM128_ID:
6984       T = Context.Ibm128Ty;
6985       break;
6986     case PREDEF_TYPE_OVERLOAD_ID:
6987       T = Context.OverloadTy;
6988       break;
6989     case PREDEF_TYPE_BOUND_MEMBER:
6990       T = Context.BoundMemberTy;
6991       break;
6992     case PREDEF_TYPE_PSEUDO_OBJECT:
6993       T = Context.PseudoObjectTy;
6994       break;
6995     case PREDEF_TYPE_DEPENDENT_ID:
6996       T = Context.DependentTy;
6997       break;
6998     case PREDEF_TYPE_UNKNOWN_ANY:
6999       T = Context.UnknownAnyTy;
7000       break;
7001     case PREDEF_TYPE_NULLPTR_ID:
7002       T = Context.NullPtrTy;
7003       break;
7004     case PREDEF_TYPE_CHAR8_ID:
7005       T = Context.Char8Ty;
7006       break;
7007     case PREDEF_TYPE_CHAR16_ID:
7008       T = Context.Char16Ty;
7009       break;
7010     case PREDEF_TYPE_CHAR32_ID:
7011       T = Context.Char32Ty;
7012       break;
7013     case PREDEF_TYPE_OBJC_ID:
7014       T = Context.ObjCBuiltinIdTy;
7015       break;
7016     case PREDEF_TYPE_OBJC_CLASS:
7017       T = Context.ObjCBuiltinClassTy;
7018       break;
7019     case PREDEF_TYPE_OBJC_SEL:
7020       T = Context.ObjCBuiltinSelTy;
7021       break;
7022 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7023     case PREDEF_TYPE_##Id##_ID: \
7024       T = Context.SingletonId; \
7025       break;
7026 #include "clang/Basic/OpenCLImageTypes.def"
7027 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7028     case PREDEF_TYPE_##Id##_ID: \
7029       T = Context.Id##Ty; \
7030       break;
7031 #include "clang/Basic/OpenCLExtensionTypes.def"
7032     case PREDEF_TYPE_SAMPLER_ID:
7033       T = Context.OCLSamplerTy;
7034       break;
7035     case PREDEF_TYPE_EVENT_ID:
7036       T = Context.OCLEventTy;
7037       break;
7038     case PREDEF_TYPE_CLK_EVENT_ID:
7039       T = Context.OCLClkEventTy;
7040       break;
7041     case PREDEF_TYPE_QUEUE_ID:
7042       T = Context.OCLQueueTy;
7043       break;
7044     case PREDEF_TYPE_RESERVE_ID_ID:
7045       T = Context.OCLReserveIDTy;
7046       break;
7047     case PREDEF_TYPE_AUTO_DEDUCT:
7048       T = Context.getAutoDeductType();
7049       break;
7050     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7051       T = Context.getAutoRRefDeductType();
7052       break;
7053     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7054       T = Context.ARCUnbridgedCastTy;
7055       break;
7056     case PREDEF_TYPE_BUILTIN_FN:
7057       T = Context.BuiltinFnTy;
7058       break;
7059     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7060       T = Context.IncompleteMatrixIdxTy;
7061       break;
7062     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7063       T = Context.OMPArraySectionTy;
7064       break;
7065     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7066       T = Context.OMPArraySectionTy;
7067       break;
7068     case PREDEF_TYPE_OMP_ITERATOR:
7069       T = Context.OMPIteratorTy;
7070       break;
7071 #define SVE_TYPE(Name, Id, SingletonId) \
7072     case PREDEF_TYPE_##Id##_ID: \
7073       T = Context.SingletonId; \
7074       break;
7075 #include "clang/Basic/AArch64SVEACLETypes.def"
7076 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7077     case PREDEF_TYPE_##Id##_ID: \
7078       T = Context.Id##Ty; \
7079       break;
7080 #include "clang/Basic/PPCTypes.def"
7081 #define RVV_TYPE(Name, Id, SingletonId) \
7082     case PREDEF_TYPE_##Id##_ID: \
7083       T = Context.SingletonId; \
7084       break;
7085 #include "clang/Basic/RISCVVTypes.def"
7086     }
7087 
7088     assert(!T.isNull() && "Unknown predefined type");
7089     return T.withFastQualifiers(FastQuals);
7090   }
7091 
7092   Index -= NUM_PREDEF_TYPE_IDS;
7093   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7094   if (TypesLoaded[Index].isNull()) {
7095     TypesLoaded[Index] = readTypeRecord(Index);
7096     if (TypesLoaded[Index].isNull())
7097       return QualType();
7098 
7099     TypesLoaded[Index]->setFromAST();
7100     if (DeserializationListener)
7101       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7102                                         TypesLoaded[Index]);
7103   }
7104 
7105   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7106 }
7107 
7108 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7109   return GetType(getGlobalTypeID(F, LocalID));
7110 }
7111 
7112 serialization::TypeID
7113 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7114   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7115   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7116 
7117   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7118     return LocalID;
7119 
7120   if (!F.ModuleOffsetMap.empty())
7121     ReadModuleOffsetMap(F);
7122 
7123   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7124     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7125   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7126 
7127   unsigned GlobalIndex = LocalIndex + I->second;
7128   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7129 }
7130 
7131 TemplateArgumentLocInfo
7132 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7133   switch (Kind) {
7134   case TemplateArgument::Expression:
7135     return readExpr();
7136   case TemplateArgument::Type:
7137     return readTypeSourceInfo();
7138   case TemplateArgument::Template: {
7139     NestedNameSpecifierLoc QualifierLoc =
7140       readNestedNameSpecifierLoc();
7141     SourceLocation TemplateNameLoc = readSourceLocation();
7142     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7143                                    TemplateNameLoc, SourceLocation());
7144   }
7145   case TemplateArgument::TemplateExpansion: {
7146     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7147     SourceLocation TemplateNameLoc = readSourceLocation();
7148     SourceLocation EllipsisLoc = readSourceLocation();
7149     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7150                                    TemplateNameLoc, EllipsisLoc);
7151   }
7152   case TemplateArgument::Null:
7153   case TemplateArgument::Integral:
7154   case TemplateArgument::Declaration:
7155   case TemplateArgument::NullPtr:
7156   case TemplateArgument::Pack:
7157     // FIXME: Is this right?
7158     return TemplateArgumentLocInfo();
7159   }
7160   llvm_unreachable("unexpected template argument loc");
7161 }
7162 
7163 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7164   TemplateArgument Arg = readTemplateArgument();
7165 
7166   if (Arg.getKind() == TemplateArgument::Expression) {
7167     if (readBool()) // bool InfoHasSameExpr.
7168       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7169   }
7170   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7171 }
7172 
7173 const ASTTemplateArgumentListInfo *
7174 ASTRecordReader::readASTTemplateArgumentListInfo() {
7175   SourceLocation LAngleLoc = readSourceLocation();
7176   SourceLocation RAngleLoc = readSourceLocation();
7177   unsigned NumArgsAsWritten = readInt();
7178   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7179   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7180     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7181   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7182 }
7183 
7184 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7185   return GetDecl(ID);
7186 }
7187 
7188 void ASTReader::CompleteRedeclChain(const Decl *D) {
7189   if (NumCurrentElementsDeserializing) {
7190     // We arrange to not care about the complete redeclaration chain while we're
7191     // deserializing. Just remember that the AST has marked this one as complete
7192     // but that it's not actually complete yet, so we know we still need to
7193     // complete it later.
7194     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7195     return;
7196   }
7197 
7198   if (!D->getDeclContext()) {
7199     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7200     return;
7201   }
7202 
7203   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7204 
7205   // If this is a named declaration, complete it by looking it up
7206   // within its context.
7207   //
7208   // FIXME: Merging a function definition should merge
7209   // all mergeable entities within it.
7210   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7211       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7212     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7213       if (!getContext().getLangOpts().CPlusPlus &&
7214           isa<TranslationUnitDecl>(DC)) {
7215         // Outside of C++, we don't have a lookup table for the TU, so update
7216         // the identifier instead. (For C++ modules, we don't store decls
7217         // in the serialized identifier table, so we do the lookup in the TU.)
7218         auto *II = Name.getAsIdentifierInfo();
7219         assert(II && "non-identifier name in C?");
7220         if (II->isOutOfDate())
7221           updateOutOfDateIdentifier(*II);
7222       } else
7223         DC->lookup(Name);
7224     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7225       // Find all declarations of this kind from the relevant context.
7226       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7227         auto *DC = cast<DeclContext>(DCDecl);
7228         SmallVector<Decl*, 8> Decls;
7229         FindExternalLexicalDecls(
7230             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7231       }
7232     }
7233   }
7234 
7235   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7236     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7237   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7238     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7239   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7240     if (auto *Template = FD->getPrimaryTemplate())
7241       Template->LoadLazySpecializations();
7242   }
7243 }
7244 
7245 CXXCtorInitializer **
7246 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7247   RecordLocation Loc = getLocalBitOffset(Offset);
7248   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7249   SavedStreamPosition SavedPosition(Cursor);
7250   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7251     Error(std::move(Err));
7252     return nullptr;
7253   }
7254   ReadingKindTracker ReadingKind(Read_Decl, *this);
7255 
7256   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7257   if (!MaybeCode) {
7258     Error(MaybeCode.takeError());
7259     return nullptr;
7260   }
7261   unsigned Code = MaybeCode.get();
7262 
7263   ASTRecordReader Record(*this, *Loc.F);
7264   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7265   if (!MaybeRecCode) {
7266     Error(MaybeRecCode.takeError());
7267     return nullptr;
7268   }
7269   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7270     Error("malformed AST file: missing C++ ctor initializers");
7271     return nullptr;
7272   }
7273 
7274   return Record.readCXXCtorInitializers();
7275 }
7276 
7277 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7278   assert(ContextObj && "reading base specifiers with no AST context");
7279   ASTContext &Context = *ContextObj;
7280 
7281   RecordLocation Loc = getLocalBitOffset(Offset);
7282   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7283   SavedStreamPosition SavedPosition(Cursor);
7284   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7285     Error(std::move(Err));
7286     return nullptr;
7287   }
7288   ReadingKindTracker ReadingKind(Read_Decl, *this);
7289 
7290   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7291   if (!MaybeCode) {
7292     Error(MaybeCode.takeError());
7293     return nullptr;
7294   }
7295   unsigned Code = MaybeCode.get();
7296 
7297   ASTRecordReader Record(*this, *Loc.F);
7298   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7299   if (!MaybeRecCode) {
7300     Error(MaybeCode.takeError());
7301     return nullptr;
7302   }
7303   unsigned RecCode = MaybeRecCode.get();
7304 
7305   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7306     Error("malformed AST file: missing C++ base specifiers");
7307     return nullptr;
7308   }
7309 
7310   unsigned NumBases = Record.readInt();
7311   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7312   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7313   for (unsigned I = 0; I != NumBases; ++I)
7314     Bases[I] = Record.readCXXBaseSpecifier();
7315   return Bases;
7316 }
7317 
7318 serialization::DeclID
7319 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7320   if (LocalID < NUM_PREDEF_DECL_IDS)
7321     return LocalID;
7322 
7323   if (!F.ModuleOffsetMap.empty())
7324     ReadModuleOffsetMap(F);
7325 
7326   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7327     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7328   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7329 
7330   return LocalID + I->second;
7331 }
7332 
7333 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7334                                    ModuleFile &M) const {
7335   // Predefined decls aren't from any module.
7336   if (ID < NUM_PREDEF_DECL_IDS)
7337     return false;
7338 
7339   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7340          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7341 }
7342 
7343 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7344   if (!D->isFromASTFile())
7345     return nullptr;
7346   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7347   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7348   return I->second;
7349 }
7350 
7351 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7352   if (ID < NUM_PREDEF_DECL_IDS)
7353     return SourceLocation();
7354 
7355   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7356 
7357   if (Index > DeclsLoaded.size()) {
7358     Error("declaration ID out-of-range for AST file");
7359     return SourceLocation();
7360   }
7361 
7362   if (Decl *D = DeclsLoaded[Index])
7363     return D->getLocation();
7364 
7365   SourceLocation Loc;
7366   DeclCursorForID(ID, Loc);
7367   return Loc;
7368 }
7369 
7370 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7371   switch (ID) {
7372   case PREDEF_DECL_NULL_ID:
7373     return nullptr;
7374 
7375   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7376     return Context.getTranslationUnitDecl();
7377 
7378   case PREDEF_DECL_OBJC_ID_ID:
7379     return Context.getObjCIdDecl();
7380 
7381   case PREDEF_DECL_OBJC_SEL_ID:
7382     return Context.getObjCSelDecl();
7383 
7384   case PREDEF_DECL_OBJC_CLASS_ID:
7385     return Context.getObjCClassDecl();
7386 
7387   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7388     return Context.getObjCProtocolDecl();
7389 
7390   case PREDEF_DECL_INT_128_ID:
7391     return Context.getInt128Decl();
7392 
7393   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7394     return Context.getUInt128Decl();
7395 
7396   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7397     return Context.getObjCInstanceTypeDecl();
7398 
7399   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7400     return Context.getBuiltinVaListDecl();
7401 
7402   case PREDEF_DECL_VA_LIST_TAG:
7403     return Context.getVaListTagDecl();
7404 
7405   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7406     return Context.getBuiltinMSVaListDecl();
7407 
7408   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7409     return Context.getMSGuidTagDecl();
7410 
7411   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7412     return Context.getExternCContextDecl();
7413 
7414   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7415     return Context.getMakeIntegerSeqDecl();
7416 
7417   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7418     return Context.getCFConstantStringDecl();
7419 
7420   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7421     return Context.getCFConstantStringTagDecl();
7422 
7423   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7424     return Context.getTypePackElementDecl();
7425   }
7426   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7427 }
7428 
7429 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7430   assert(ContextObj && "reading decl with no AST context");
7431   if (ID < NUM_PREDEF_DECL_IDS) {
7432     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7433     if (D) {
7434       // Track that we have merged the declaration with ID \p ID into the
7435       // pre-existing predefined declaration \p D.
7436       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7437       if (Merged.empty())
7438         Merged.push_back(ID);
7439     }
7440     return D;
7441   }
7442 
7443   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7444 
7445   if (Index >= DeclsLoaded.size()) {
7446     assert(0 && "declaration ID out-of-range for AST file");
7447     Error("declaration ID out-of-range for AST file");
7448     return nullptr;
7449   }
7450 
7451   return DeclsLoaded[Index];
7452 }
7453 
7454 Decl *ASTReader::GetDecl(DeclID ID) {
7455   if (ID < NUM_PREDEF_DECL_IDS)
7456     return GetExistingDecl(ID);
7457 
7458   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7459 
7460   if (Index >= DeclsLoaded.size()) {
7461     assert(0 && "declaration ID out-of-range for AST file");
7462     Error("declaration ID out-of-range for AST file");
7463     return nullptr;
7464   }
7465 
7466   if (!DeclsLoaded[Index]) {
7467     ReadDeclRecord(ID);
7468     if (DeserializationListener)
7469       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7470   }
7471 
7472   return DeclsLoaded[Index];
7473 }
7474 
7475 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7476                                                   DeclID GlobalID) {
7477   if (GlobalID < NUM_PREDEF_DECL_IDS)
7478     return GlobalID;
7479 
7480   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7481   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7482   ModuleFile *Owner = I->second;
7483 
7484   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7485     = M.GlobalToLocalDeclIDs.find(Owner);
7486   if (Pos == M.GlobalToLocalDeclIDs.end())
7487     return 0;
7488 
7489   return GlobalID - Owner->BaseDeclID + Pos->second;
7490 }
7491 
7492 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7493                                             const RecordData &Record,
7494                                             unsigned &Idx) {
7495   if (Idx >= Record.size()) {
7496     Error("Corrupted AST file");
7497     return 0;
7498   }
7499 
7500   return getGlobalDeclID(F, Record[Idx++]);
7501 }
7502 
7503 /// Resolve the offset of a statement into a statement.
7504 ///
7505 /// This operation will read a new statement from the external
7506 /// source each time it is called, and is meant to be used via a
7507 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7508 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7509   // Switch case IDs are per Decl.
7510   ClearSwitchCaseIDs();
7511 
7512   // Offset here is a global offset across the entire chain.
7513   RecordLocation Loc = getLocalBitOffset(Offset);
7514   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7515     Error(std::move(Err));
7516     return nullptr;
7517   }
7518   assert(NumCurrentElementsDeserializing == 0 &&
7519          "should not be called while already deserializing");
7520   Deserializing D(this);
7521   return ReadStmtFromStream(*Loc.F);
7522 }
7523 
7524 void ASTReader::FindExternalLexicalDecls(
7525     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7526     SmallVectorImpl<Decl *> &Decls) {
7527   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7528 
7529   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7530     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7531     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7532       auto K = (Decl::Kind)+LexicalDecls[I];
7533       if (!IsKindWeWant(K))
7534         continue;
7535 
7536       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7537 
7538       // Don't add predefined declarations to the lexical context more
7539       // than once.
7540       if (ID < NUM_PREDEF_DECL_IDS) {
7541         if (PredefsVisited[ID])
7542           continue;
7543 
7544         PredefsVisited[ID] = true;
7545       }
7546 
7547       if (Decl *D = GetLocalDecl(*M, ID)) {
7548         assert(D->getKind() == K && "wrong kind for lexical decl");
7549         if (!DC->isDeclInLexicalTraversal(D))
7550           Decls.push_back(D);
7551       }
7552     }
7553   };
7554 
7555   if (isa<TranslationUnitDecl>(DC)) {
7556     for (auto Lexical : TULexicalDecls)
7557       Visit(Lexical.first, Lexical.second);
7558   } else {
7559     auto I = LexicalDecls.find(DC);
7560     if (I != LexicalDecls.end())
7561       Visit(I->second.first, I->second.second);
7562   }
7563 
7564   ++NumLexicalDeclContextsRead;
7565 }
7566 
7567 namespace {
7568 
7569 class DeclIDComp {
7570   ASTReader &Reader;
7571   ModuleFile &Mod;
7572 
7573 public:
7574   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7575 
7576   bool operator()(LocalDeclID L, LocalDeclID R) const {
7577     SourceLocation LHS = getLocation(L);
7578     SourceLocation RHS = getLocation(R);
7579     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7580   }
7581 
7582   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7583     SourceLocation RHS = getLocation(R);
7584     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7585   }
7586 
7587   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7588     SourceLocation LHS = getLocation(L);
7589     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7590   }
7591 
7592   SourceLocation getLocation(LocalDeclID ID) const {
7593     return Reader.getSourceManager().getFileLoc(
7594             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7595   }
7596 };
7597 
7598 } // namespace
7599 
7600 void ASTReader::FindFileRegionDecls(FileID File,
7601                                     unsigned Offset, unsigned Length,
7602                                     SmallVectorImpl<Decl *> &Decls) {
7603   SourceManager &SM = getSourceManager();
7604 
7605   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7606   if (I == FileDeclIDs.end())
7607     return;
7608 
7609   FileDeclsInfo &DInfo = I->second;
7610   if (DInfo.Decls.empty())
7611     return;
7612 
7613   SourceLocation
7614     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7615   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7616 
7617   DeclIDComp DIDComp(*this, *DInfo.Mod);
7618   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7619       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7620   if (BeginIt != DInfo.Decls.begin())
7621     --BeginIt;
7622 
7623   // If we are pointing at a top-level decl inside an objc container, we need
7624   // to backtrack until we find it otherwise we will fail to report that the
7625   // region overlaps with an objc container.
7626   while (BeginIt != DInfo.Decls.begin() &&
7627          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7628              ->isTopLevelDeclInObjCContainer())
7629     --BeginIt;
7630 
7631   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7632       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7633   if (EndIt != DInfo.Decls.end())
7634     ++EndIt;
7635 
7636   for (ArrayRef<serialization::LocalDeclID>::iterator
7637          DIt = BeginIt; DIt != EndIt; ++DIt)
7638     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7639 }
7640 
7641 bool
7642 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7643                                           DeclarationName Name) {
7644   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7645          "DeclContext has no visible decls in storage");
7646   if (!Name)
7647     return false;
7648 
7649   auto It = Lookups.find(DC);
7650   if (It == Lookups.end())
7651     return false;
7652 
7653   Deserializing LookupResults(this);
7654 
7655   // Load the list of declarations.
7656   SmallVector<NamedDecl *, 64> Decls;
7657   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7658   for (DeclID ID : It->second.Table.find(Name)) {
7659     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7660     if (ND->getDeclName() == Name && Found.insert(ND).second)
7661       Decls.push_back(ND);
7662   }
7663 
7664   ++NumVisibleDeclContextsRead;
7665   SetExternalVisibleDeclsForName(DC, Name, Decls);
7666   return !Decls.empty();
7667 }
7668 
7669 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7670   if (!DC->hasExternalVisibleStorage())
7671     return;
7672 
7673   auto It = Lookups.find(DC);
7674   assert(It != Lookups.end() &&
7675          "have external visible storage but no lookup tables");
7676 
7677   DeclsMap Decls;
7678 
7679   for (DeclID ID : It->second.Table.findAll()) {
7680     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7681     Decls[ND->getDeclName()].push_back(ND);
7682   }
7683 
7684   ++NumVisibleDeclContextsRead;
7685 
7686   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7687     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7688   }
7689   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7690 }
7691 
7692 const serialization::reader::DeclContextLookupTable *
7693 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7694   auto I = Lookups.find(Primary);
7695   return I == Lookups.end() ? nullptr : &I->second;
7696 }
7697 
7698 /// Under non-PCH compilation the consumer receives the objc methods
7699 /// before receiving the implementation, and codegen depends on this.
7700 /// We simulate this by deserializing and passing to consumer the methods of the
7701 /// implementation before passing the deserialized implementation decl.
7702 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7703                                        ASTConsumer *Consumer) {
7704   assert(ImplD && Consumer);
7705 
7706   for (auto *I : ImplD->methods())
7707     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7708 
7709   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7710 }
7711 
7712 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7713   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7714     PassObjCImplDeclToConsumer(ImplD, Consumer);
7715   else
7716     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7717 }
7718 
7719 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7720   this->Consumer = Consumer;
7721 
7722   if (Consumer)
7723     PassInterestingDeclsToConsumer();
7724 
7725   if (DeserializationListener)
7726     DeserializationListener->ReaderInitialized(this);
7727 }
7728 
7729 void ASTReader::PrintStats() {
7730   std::fprintf(stderr, "*** AST File Statistics:\n");
7731 
7732   unsigned NumTypesLoaded =
7733       TypesLoaded.size() - llvm::count(TypesLoaded, QualType());
7734   unsigned NumDeclsLoaded =
7735       DeclsLoaded.size() - llvm::count(DeclsLoaded, (Decl *)nullptr);
7736   unsigned NumIdentifiersLoaded =
7737       IdentifiersLoaded.size() -
7738       llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);
7739   unsigned NumMacrosLoaded =
7740       MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr);
7741   unsigned NumSelectorsLoaded =
7742       SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector());
7743 
7744   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7745     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7746                  NumSLocEntriesRead, TotalNumSLocEntries,
7747                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7748   if (!TypesLoaded.empty())
7749     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7750                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7751                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7752   if (!DeclsLoaded.empty())
7753     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7754                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7755                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7756   if (!IdentifiersLoaded.empty())
7757     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7758                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7759                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7760   if (!MacrosLoaded.empty())
7761     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7762                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7763                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7764   if (!SelectorsLoaded.empty())
7765     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7766                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7767                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7768   if (TotalNumStatements)
7769     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7770                  NumStatementsRead, TotalNumStatements,
7771                  ((float)NumStatementsRead/TotalNumStatements * 100));
7772   if (TotalNumMacros)
7773     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7774                  NumMacrosRead, TotalNumMacros,
7775                  ((float)NumMacrosRead/TotalNumMacros * 100));
7776   if (TotalLexicalDeclContexts)
7777     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7778                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7779                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7780                   * 100));
7781   if (TotalVisibleDeclContexts)
7782     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7783                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7784                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7785                   * 100));
7786   if (TotalNumMethodPoolEntries)
7787     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7788                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7789                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7790                   * 100));
7791   if (NumMethodPoolLookups)
7792     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7793                  NumMethodPoolHits, NumMethodPoolLookups,
7794                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7795   if (NumMethodPoolTableLookups)
7796     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7797                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7798                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7799                   * 100.0));
7800   if (NumIdentifierLookupHits)
7801     std::fprintf(stderr,
7802                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7803                  NumIdentifierLookupHits, NumIdentifierLookups,
7804                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7805 
7806   if (GlobalIndex) {
7807     std::fprintf(stderr, "\n");
7808     GlobalIndex->printStats();
7809   }
7810 
7811   std::fprintf(stderr, "\n");
7812   dump();
7813   std::fprintf(stderr, "\n");
7814 }
7815 
7816 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7817 LLVM_DUMP_METHOD static void
7818 dumpModuleIDMap(StringRef Name,
7819                 const ContinuousRangeMap<Key, ModuleFile *,
7820                                          InitialCapacity> &Map) {
7821   if (Map.begin() == Map.end())
7822     return;
7823 
7824   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7825 
7826   llvm::errs() << Name << ":\n";
7827   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7828        I != IEnd; ++I) {
7829     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7830       << "\n";
7831   }
7832 }
7833 
7834 LLVM_DUMP_METHOD void ASTReader::dump() {
7835   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7836   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7837   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7838   dumpModuleIDMap("Global type map", GlobalTypeMap);
7839   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7840   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7841   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7842   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7843   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7844   dumpModuleIDMap("Global preprocessed entity map",
7845                   GlobalPreprocessedEntityMap);
7846 
7847   llvm::errs() << "\n*** PCH/Modules Loaded:";
7848   for (ModuleFile &M : ModuleMgr)
7849     M.dump();
7850 }
7851 
7852 /// Return the amount of memory used by memory buffers, breaking down
7853 /// by heap-backed versus mmap'ed memory.
7854 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7855   for (ModuleFile &I : ModuleMgr) {
7856     if (llvm::MemoryBuffer *buf = I.Buffer) {
7857       size_t bytes = buf->getBufferSize();
7858       switch (buf->getBufferKind()) {
7859         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7860           sizes.malloc_bytes += bytes;
7861           break;
7862         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7863           sizes.mmap_bytes += bytes;
7864           break;
7865       }
7866     }
7867   }
7868 }
7869 
7870 void ASTReader::InitializeSema(Sema &S) {
7871   SemaObj = &S;
7872   S.addExternalSource(this);
7873 
7874   // Makes sure any declarations that were deserialized "too early"
7875   // still get added to the identifier's declaration chains.
7876   for (uint64_t ID : PreloadedDeclIDs) {
7877     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7878     pushExternalDeclIntoScope(D, D->getDeclName());
7879   }
7880   PreloadedDeclIDs.clear();
7881 
7882   // FIXME: What happens if these are changed by a module import?
7883   if (!FPPragmaOptions.empty()) {
7884     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7885     FPOptionsOverride NewOverrides =
7886         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7887     SemaObj->CurFPFeatures =
7888         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7889   }
7890 
7891   SemaObj->OpenCLFeatures = OpenCLExtensions;
7892 
7893   UpdateSema();
7894 }
7895 
7896 void ASTReader::UpdateSema() {
7897   assert(SemaObj && "no Sema to update");
7898 
7899   // Load the offsets of the declarations that Sema references.
7900   // They will be lazily deserialized when needed.
7901   if (!SemaDeclRefs.empty()) {
7902     assert(SemaDeclRefs.size() % 3 == 0);
7903     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7904       if (!SemaObj->StdNamespace)
7905         SemaObj->StdNamespace = SemaDeclRefs[I];
7906       if (!SemaObj->StdBadAlloc)
7907         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7908       if (!SemaObj->StdAlignValT)
7909         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7910     }
7911     SemaDeclRefs.clear();
7912   }
7913 
7914   // Update the state of pragmas. Use the same API as if we had encountered the
7915   // pragma in the source.
7916   if(OptimizeOffPragmaLocation.isValid())
7917     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7918   if (PragmaMSStructState != -1)
7919     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7920   if (PointersToMembersPragmaLocation.isValid()) {
7921     SemaObj->ActOnPragmaMSPointersToMembers(
7922         (LangOptions::PragmaMSPointersToMembersKind)
7923             PragmaMSPointersToMembersState,
7924         PointersToMembersPragmaLocation);
7925   }
7926   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7927 
7928   if (PragmaAlignPackCurrentValue) {
7929     // The bottom of the stack might have a default value. It must be adjusted
7930     // to the current value to ensure that the packing state is preserved after
7931     // popping entries that were included/imported from a PCH/module.
7932     bool DropFirst = false;
7933     if (!PragmaAlignPackStack.empty() &&
7934         PragmaAlignPackStack.front().Location.isInvalid()) {
7935       assert(PragmaAlignPackStack.front().Value ==
7936                  SemaObj->AlignPackStack.DefaultValue &&
7937              "Expected a default alignment value");
7938       SemaObj->AlignPackStack.Stack.emplace_back(
7939           PragmaAlignPackStack.front().SlotLabel,
7940           SemaObj->AlignPackStack.CurrentValue,
7941           SemaObj->AlignPackStack.CurrentPragmaLocation,
7942           PragmaAlignPackStack.front().PushLocation);
7943       DropFirst = true;
7944     }
7945     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7946                                  .drop_front(DropFirst ? 1 : 0)) {
7947       SemaObj->AlignPackStack.Stack.emplace_back(
7948           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7949     }
7950     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7951       assert(*PragmaAlignPackCurrentValue ==
7952                  SemaObj->AlignPackStack.DefaultValue &&
7953              "Expected a default align and pack value");
7954       // Keep the current values.
7955     } else {
7956       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7957       SemaObj->AlignPackStack.CurrentPragmaLocation =
7958           PragmaAlignPackCurrentLocation;
7959     }
7960   }
7961   if (FpPragmaCurrentValue) {
7962     // The bottom of the stack might have a default value. It must be adjusted
7963     // to the current value to ensure that fp-pragma state is preserved after
7964     // popping entries that were included/imported from a PCH/module.
7965     bool DropFirst = false;
7966     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7967       assert(FpPragmaStack.front().Value ==
7968                  SemaObj->FpPragmaStack.DefaultValue &&
7969              "Expected a default pragma float_control value");
7970       SemaObj->FpPragmaStack.Stack.emplace_back(
7971           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7972           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7973           FpPragmaStack.front().PushLocation);
7974       DropFirst = true;
7975     }
7976     for (const auto &Entry :
7977          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7978       SemaObj->FpPragmaStack.Stack.emplace_back(
7979           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7980     if (FpPragmaCurrentLocation.isInvalid()) {
7981       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7982              "Expected a default pragma float_control value");
7983       // Keep the current values.
7984     } else {
7985       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7986       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7987     }
7988   }
7989 
7990   // For non-modular AST files, restore visiblity of modules.
7991   for (auto &Import : ImportedModules) {
7992     if (Import.ImportLoc.isInvalid())
7993       continue;
7994     if (Module *Imported = getSubmodule(Import.ID)) {
7995       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7996     }
7997   }
7998 }
7999 
8000 IdentifierInfo *ASTReader::get(StringRef Name) {
8001   // Note that we are loading an identifier.
8002   Deserializing AnIdentifier(this);
8003 
8004   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8005                                   NumIdentifierLookups,
8006                                   NumIdentifierLookupHits);
8007 
8008   // We don't need to do identifier table lookups in C++ modules (we preload
8009   // all interesting declarations, and don't need to use the scope for name
8010   // lookups). Perform the lookup in PCH files, though, since we don't build
8011   // a complete initial identifier table if we're carrying on from a PCH.
8012   if (PP.getLangOpts().CPlusPlus) {
8013     for (auto F : ModuleMgr.pch_modules())
8014       if (Visitor(*F))
8015         break;
8016   } else {
8017     // If there is a global index, look there first to determine which modules
8018     // provably do not have any results for this identifier.
8019     GlobalModuleIndex::HitSet Hits;
8020     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8021     if (!loadGlobalIndex()) {
8022       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8023         HitsPtr = &Hits;
8024       }
8025     }
8026 
8027     ModuleMgr.visit(Visitor, HitsPtr);
8028   }
8029 
8030   IdentifierInfo *II = Visitor.getIdentifierInfo();
8031   markIdentifierUpToDate(II);
8032   return II;
8033 }
8034 
8035 namespace clang {
8036 
8037   /// An identifier-lookup iterator that enumerates all of the
8038   /// identifiers stored within a set of AST files.
8039   class ASTIdentifierIterator : public IdentifierIterator {
8040     /// The AST reader whose identifiers are being enumerated.
8041     const ASTReader &Reader;
8042 
8043     /// The current index into the chain of AST files stored in
8044     /// the AST reader.
8045     unsigned Index;
8046 
8047     /// The current position within the identifier lookup table
8048     /// of the current AST file.
8049     ASTIdentifierLookupTable::key_iterator Current;
8050 
8051     /// The end position within the identifier lookup table of
8052     /// the current AST file.
8053     ASTIdentifierLookupTable::key_iterator End;
8054 
8055     /// Whether to skip any modules in the ASTReader.
8056     bool SkipModules;
8057 
8058   public:
8059     explicit ASTIdentifierIterator(const ASTReader &Reader,
8060                                    bool SkipModules = false);
8061 
8062     StringRef Next() override;
8063   };
8064 
8065 } // namespace clang
8066 
8067 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8068                                              bool SkipModules)
8069     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8070 }
8071 
8072 StringRef ASTIdentifierIterator::Next() {
8073   while (Current == End) {
8074     // If we have exhausted all of our AST files, we're done.
8075     if (Index == 0)
8076       return StringRef();
8077 
8078     --Index;
8079     ModuleFile &F = Reader.ModuleMgr[Index];
8080     if (SkipModules && F.isModule())
8081       continue;
8082 
8083     ASTIdentifierLookupTable *IdTable =
8084         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8085     Current = IdTable->key_begin();
8086     End = IdTable->key_end();
8087   }
8088 
8089   // We have any identifiers remaining in the current AST file; return
8090   // the next one.
8091   StringRef Result = *Current;
8092   ++Current;
8093   return Result;
8094 }
8095 
8096 namespace {
8097 
8098 /// A utility for appending two IdentifierIterators.
8099 class ChainedIdentifierIterator : public IdentifierIterator {
8100   std::unique_ptr<IdentifierIterator> Current;
8101   std::unique_ptr<IdentifierIterator> Queued;
8102 
8103 public:
8104   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8105                             std::unique_ptr<IdentifierIterator> Second)
8106       : Current(std::move(First)), Queued(std::move(Second)) {}
8107 
8108   StringRef Next() override {
8109     if (!Current)
8110       return StringRef();
8111 
8112     StringRef result = Current->Next();
8113     if (!result.empty())
8114       return result;
8115 
8116     // Try the queued iterator, which may itself be empty.
8117     Current.reset();
8118     std::swap(Current, Queued);
8119     return Next();
8120   }
8121 };
8122 
8123 } // namespace
8124 
8125 IdentifierIterator *ASTReader::getIdentifiers() {
8126   if (!loadGlobalIndex()) {
8127     std::unique_ptr<IdentifierIterator> ReaderIter(
8128         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8129     std::unique_ptr<IdentifierIterator> ModulesIter(
8130         GlobalIndex->createIdentifierIterator());
8131     return new ChainedIdentifierIterator(std::move(ReaderIter),
8132                                          std::move(ModulesIter));
8133   }
8134 
8135   return new ASTIdentifierIterator(*this);
8136 }
8137 
8138 namespace clang {
8139 namespace serialization {
8140 
8141   class ReadMethodPoolVisitor {
8142     ASTReader &Reader;
8143     Selector Sel;
8144     unsigned PriorGeneration;
8145     unsigned InstanceBits = 0;
8146     unsigned FactoryBits = 0;
8147     bool InstanceHasMoreThanOneDecl = false;
8148     bool FactoryHasMoreThanOneDecl = false;
8149     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8150     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8151 
8152   public:
8153     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8154                           unsigned PriorGeneration)
8155         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8156 
8157     bool operator()(ModuleFile &M) {
8158       if (!M.SelectorLookupTable)
8159         return false;
8160 
8161       // If we've already searched this module file, skip it now.
8162       if (M.Generation <= PriorGeneration)
8163         return true;
8164 
8165       ++Reader.NumMethodPoolTableLookups;
8166       ASTSelectorLookupTable *PoolTable
8167         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8168       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8169       if (Pos == PoolTable->end())
8170         return false;
8171 
8172       ++Reader.NumMethodPoolTableHits;
8173       ++Reader.NumSelectorsRead;
8174       // FIXME: Not quite happy with the statistics here. We probably should
8175       // disable this tracking when called via LoadSelector.
8176       // Also, should entries without methods count as misses?
8177       ++Reader.NumMethodPoolEntriesRead;
8178       ASTSelectorLookupTrait::data_type Data = *Pos;
8179       if (Reader.DeserializationListener)
8180         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8181 
8182       // Append methods in the reverse order, so that later we can process them
8183       // in the order they appear in the source code by iterating through
8184       // the vector in the reverse order.
8185       InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend());
8186       FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend());
8187       InstanceBits = Data.InstanceBits;
8188       FactoryBits = Data.FactoryBits;
8189       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8190       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8191       return false;
8192     }
8193 
8194     /// Retrieve the instance methods found by this visitor.
8195     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8196       return InstanceMethods;
8197     }
8198 
8199     /// Retrieve the instance methods found by this visitor.
8200     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8201       return FactoryMethods;
8202     }
8203 
8204     unsigned getInstanceBits() const { return InstanceBits; }
8205     unsigned getFactoryBits() const { return FactoryBits; }
8206 
8207     bool instanceHasMoreThanOneDecl() const {
8208       return InstanceHasMoreThanOneDecl;
8209     }
8210 
8211     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8212   };
8213 
8214 } // namespace serialization
8215 } // namespace clang
8216 
8217 /// Add the given set of methods to the method list.
8218 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8219                              ObjCMethodList &List) {
8220   for (auto I = Methods.rbegin(), E = Methods.rend(); I != E; ++I)
8221     S.addMethodToGlobalList(&List, *I);
8222 }
8223 
8224 void ASTReader::ReadMethodPool(Selector Sel) {
8225   // Get the selector generation and update it to the current generation.
8226   unsigned &Generation = SelectorGeneration[Sel];
8227   unsigned PriorGeneration = Generation;
8228   Generation = getGeneration();
8229   SelectorOutOfDate[Sel] = false;
8230 
8231   // Search for methods defined with this selector.
8232   ++NumMethodPoolLookups;
8233   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8234   ModuleMgr.visit(Visitor);
8235 
8236   if (Visitor.getInstanceMethods().empty() &&
8237       Visitor.getFactoryMethods().empty())
8238     return;
8239 
8240   ++NumMethodPoolHits;
8241 
8242   if (!getSema())
8243     return;
8244 
8245   Sema &S = *getSema();
8246   Sema::GlobalMethodPool::iterator Pos =
8247       S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists()))
8248           .first;
8249 
8250   Pos->second.first.setBits(Visitor.getInstanceBits());
8251   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8252   Pos->second.second.setBits(Visitor.getFactoryBits());
8253   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8254 
8255   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8256   // when building a module we keep every method individually and may need to
8257   // update hasMoreThanOneDecl as we add the methods.
8258   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8259   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8260 }
8261 
8262 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8263   if (SelectorOutOfDate[Sel])
8264     ReadMethodPool(Sel);
8265 }
8266 
8267 void ASTReader::ReadKnownNamespaces(
8268                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8269   Namespaces.clear();
8270 
8271   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8272     if (NamespaceDecl *Namespace
8273                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8274       Namespaces.push_back(Namespace);
8275   }
8276 }
8277 
8278 void ASTReader::ReadUndefinedButUsed(
8279     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8280   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8281     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8282     SourceLocation Loc =
8283         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8284     Undefined.insert(std::make_pair(D, Loc));
8285   }
8286 }
8287 
8288 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8289     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8290                                                      Exprs) {
8291   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8292     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8293     uint64_t Count = DelayedDeleteExprs[Idx++];
8294     for (uint64_t C = 0; C < Count; ++C) {
8295       SourceLocation DeleteLoc =
8296           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8297       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8298       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8299     }
8300   }
8301 }
8302 
8303 void ASTReader::ReadTentativeDefinitions(
8304                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8305   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8306     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8307     if (Var)
8308       TentativeDefs.push_back(Var);
8309   }
8310   TentativeDefinitions.clear();
8311 }
8312 
8313 void ASTReader::ReadUnusedFileScopedDecls(
8314                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8315   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8316     DeclaratorDecl *D
8317       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8318     if (D)
8319       Decls.push_back(D);
8320   }
8321   UnusedFileScopedDecls.clear();
8322 }
8323 
8324 void ASTReader::ReadDelegatingConstructors(
8325                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8326   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8327     CXXConstructorDecl *D
8328       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8329     if (D)
8330       Decls.push_back(D);
8331   }
8332   DelegatingCtorDecls.clear();
8333 }
8334 
8335 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8336   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8337     TypedefNameDecl *D
8338       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8339     if (D)
8340       Decls.push_back(D);
8341   }
8342   ExtVectorDecls.clear();
8343 }
8344 
8345 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8346     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8347   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8348        ++I) {
8349     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8350         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8351     if (D)
8352       Decls.insert(D);
8353   }
8354   UnusedLocalTypedefNameCandidates.clear();
8355 }
8356 
8357 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8358     llvm::SmallSetVector<Decl *, 4> &Decls) {
8359   for (auto I : DeclsToCheckForDeferredDiags) {
8360     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8361     if (D)
8362       Decls.insert(D);
8363   }
8364   DeclsToCheckForDeferredDiags.clear();
8365 }
8366 
8367 void ASTReader::ReadReferencedSelectors(
8368        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8369   if (ReferencedSelectorsData.empty())
8370     return;
8371 
8372   // If there are @selector references added them to its pool. This is for
8373   // implementation of -Wselector.
8374   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8375   unsigned I = 0;
8376   while (I < DataSize) {
8377     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8378     SourceLocation SelLoc
8379       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8380     Sels.push_back(std::make_pair(Sel, SelLoc));
8381   }
8382   ReferencedSelectorsData.clear();
8383 }
8384 
8385 void ASTReader::ReadWeakUndeclaredIdentifiers(
8386        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8387   if (WeakUndeclaredIdentifiers.empty())
8388     return;
8389 
8390   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8391     IdentifierInfo *WeakId
8392       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8393     IdentifierInfo *AliasId
8394       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8395     SourceLocation Loc
8396       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8397     bool Used = WeakUndeclaredIdentifiers[I++];
8398     WeakInfo WI(AliasId, Loc);
8399     WI.setUsed(Used);
8400     WeakIDs.push_back(std::make_pair(WeakId, WI));
8401   }
8402   WeakUndeclaredIdentifiers.clear();
8403 }
8404 
8405 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8406   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8407     ExternalVTableUse VT;
8408     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8409     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8410     VT.DefinitionRequired = VTableUses[Idx++];
8411     VTables.push_back(VT);
8412   }
8413 
8414   VTableUses.clear();
8415 }
8416 
8417 void ASTReader::ReadPendingInstantiations(
8418        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8419   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8420     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8421     SourceLocation Loc
8422       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8423 
8424     Pending.push_back(std::make_pair(D, Loc));
8425   }
8426   PendingInstantiations.clear();
8427 }
8428 
8429 void ASTReader::ReadLateParsedTemplates(
8430     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8431         &LPTMap) {
8432   for (auto &LPT : LateParsedTemplates) {
8433     ModuleFile *FMod = LPT.first;
8434     RecordDataImpl &LateParsed = LPT.second;
8435     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8436          /* In loop */) {
8437       FunctionDecl *FD =
8438           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8439 
8440       auto LT = std::make_unique<LateParsedTemplate>();
8441       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8442 
8443       ModuleFile *F = getOwningModuleFile(LT->D);
8444       assert(F && "No module");
8445 
8446       unsigned TokN = LateParsed[Idx++];
8447       LT->Toks.reserve(TokN);
8448       for (unsigned T = 0; T < TokN; ++T)
8449         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8450 
8451       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8452     }
8453   }
8454 
8455   LateParsedTemplates.clear();
8456 }
8457 
8458 void ASTReader::LoadSelector(Selector Sel) {
8459   // It would be complicated to avoid reading the methods anyway. So don't.
8460   ReadMethodPool(Sel);
8461 }
8462 
8463 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8464   assert(ID && "Non-zero identifier ID required");
8465   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8466   IdentifiersLoaded[ID - 1] = II;
8467   if (DeserializationListener)
8468     DeserializationListener->IdentifierRead(ID, II);
8469 }
8470 
8471 /// Set the globally-visible declarations associated with the given
8472 /// identifier.
8473 ///
8474 /// If the AST reader is currently in a state where the given declaration IDs
8475 /// cannot safely be resolved, they are queued until it is safe to resolve
8476 /// them.
8477 ///
8478 /// \param II an IdentifierInfo that refers to one or more globally-visible
8479 /// declarations.
8480 ///
8481 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8482 /// visible at global scope.
8483 ///
8484 /// \param Decls if non-null, this vector will be populated with the set of
8485 /// deserialized declarations. These declarations will not be pushed into
8486 /// scope.
8487 void
8488 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8489                               const SmallVectorImpl<uint32_t> &DeclIDs,
8490                                    SmallVectorImpl<Decl *> *Decls) {
8491   if (NumCurrentElementsDeserializing && !Decls) {
8492     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8493     return;
8494   }
8495 
8496   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8497     if (!SemaObj) {
8498       // Queue this declaration so that it will be added to the
8499       // translation unit scope and identifier's declaration chain
8500       // once a Sema object is known.
8501       PreloadedDeclIDs.push_back(DeclIDs[I]);
8502       continue;
8503     }
8504 
8505     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8506 
8507     // If we're simply supposed to record the declarations, do so now.
8508     if (Decls) {
8509       Decls->push_back(D);
8510       continue;
8511     }
8512 
8513     // Introduce this declaration into the translation-unit scope
8514     // and add it to the declaration chain for this identifier, so
8515     // that (unqualified) name lookup will find it.
8516     pushExternalDeclIntoScope(D, II);
8517   }
8518 }
8519 
8520 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8521   if (ID == 0)
8522     return nullptr;
8523 
8524   if (IdentifiersLoaded.empty()) {
8525     Error("no identifier table in AST file");
8526     return nullptr;
8527   }
8528 
8529   ID -= 1;
8530   if (!IdentifiersLoaded[ID]) {
8531     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8532     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8533     ModuleFile *M = I->second;
8534     unsigned Index = ID - M->BaseIdentifierID;
8535     const unsigned char *Data =
8536         M->IdentifierTableData + M->IdentifierOffsets[Index];
8537 
8538     ASTIdentifierLookupTrait Trait(*this, *M);
8539     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8540     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8541     auto &II = PP.getIdentifierTable().get(Key);
8542     IdentifiersLoaded[ID] = &II;
8543     markIdentifierFromAST(*this,  II);
8544     if (DeserializationListener)
8545       DeserializationListener->IdentifierRead(ID + 1, &II);
8546   }
8547 
8548   return IdentifiersLoaded[ID];
8549 }
8550 
8551 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8552   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8553 }
8554 
8555 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8556   if (LocalID < NUM_PREDEF_IDENT_IDS)
8557     return LocalID;
8558 
8559   if (!M.ModuleOffsetMap.empty())
8560     ReadModuleOffsetMap(M);
8561 
8562   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8563     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8564   assert(I != M.IdentifierRemap.end()
8565          && "Invalid index into identifier index remap");
8566 
8567   return LocalID + I->second;
8568 }
8569 
8570 MacroInfo *ASTReader::getMacro(MacroID ID) {
8571   if (ID == 0)
8572     return nullptr;
8573 
8574   if (MacrosLoaded.empty()) {
8575     Error("no macro table in AST file");
8576     return nullptr;
8577   }
8578 
8579   ID -= NUM_PREDEF_MACRO_IDS;
8580   if (!MacrosLoaded[ID]) {
8581     GlobalMacroMapType::iterator I
8582       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8583     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8584     ModuleFile *M = I->second;
8585     unsigned Index = ID - M->BaseMacroID;
8586     MacrosLoaded[ID] =
8587         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8588 
8589     if (DeserializationListener)
8590       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8591                                          MacrosLoaded[ID]);
8592   }
8593 
8594   return MacrosLoaded[ID];
8595 }
8596 
8597 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8598   if (LocalID < NUM_PREDEF_MACRO_IDS)
8599     return LocalID;
8600 
8601   if (!M.ModuleOffsetMap.empty())
8602     ReadModuleOffsetMap(M);
8603 
8604   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8605     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8606   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8607 
8608   return LocalID + I->second;
8609 }
8610 
8611 serialization::SubmoduleID
8612 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8613   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8614     return LocalID;
8615 
8616   if (!M.ModuleOffsetMap.empty())
8617     ReadModuleOffsetMap(M);
8618 
8619   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8620     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8621   assert(I != M.SubmoduleRemap.end()
8622          && "Invalid index into submodule index remap");
8623 
8624   return LocalID + I->second;
8625 }
8626 
8627 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8628   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8629     assert(GlobalID == 0 && "Unhandled global submodule ID");
8630     return nullptr;
8631   }
8632 
8633   if (GlobalID > SubmodulesLoaded.size()) {
8634     Error("submodule ID out of range in AST file");
8635     return nullptr;
8636   }
8637 
8638   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8639 }
8640 
8641 Module *ASTReader::getModule(unsigned ID) {
8642   return getSubmodule(ID);
8643 }
8644 
8645 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8646   if (ID & 1) {
8647     // It's a module, look it up by submodule ID.
8648     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8649     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8650   } else {
8651     // It's a prefix (preamble, PCH, ...). Look it up by index.
8652     unsigned IndexFromEnd = ID >> 1;
8653     assert(IndexFromEnd && "got reference to unknown module file");
8654     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8655   }
8656 }
8657 
8658 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8659   if (!F)
8660     return 1;
8661 
8662   // For a file representing a module, use the submodule ID of the top-level
8663   // module as the file ID. For any other kind of file, the number of such
8664   // files loaded beforehand will be the same on reload.
8665   // FIXME: Is this true even if we have an explicit module file and a PCH?
8666   if (F->isModule())
8667     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8668 
8669   auto PCHModules = getModuleManager().pch_modules();
8670   auto I = llvm::find(PCHModules, F);
8671   assert(I != PCHModules.end() && "emitting reference to unknown file");
8672   return (I - PCHModules.end()) << 1;
8673 }
8674 
8675 llvm::Optional<ASTSourceDescriptor>
8676 ASTReader::getSourceDescriptor(unsigned ID) {
8677   if (Module *M = getSubmodule(ID))
8678     return ASTSourceDescriptor(*M);
8679 
8680   // If there is only a single PCH, return it instead.
8681   // Chained PCH are not supported.
8682   const auto &PCHChain = ModuleMgr.pch_modules();
8683   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8684     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8685     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8686     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8687     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8688                                MF.Signature);
8689   }
8690   return None;
8691 }
8692 
8693 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8694   auto I = DefinitionSource.find(FD);
8695   if (I == DefinitionSource.end())
8696     return EK_ReplyHazy;
8697   return I->second ? EK_Never : EK_Always;
8698 }
8699 
8700 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8701   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8702 }
8703 
8704 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8705   if (ID == 0)
8706     return Selector();
8707 
8708   if (ID > SelectorsLoaded.size()) {
8709     Error("selector ID out of range in AST file");
8710     return Selector();
8711   }
8712 
8713   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8714     // Load this selector from the selector table.
8715     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8716     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8717     ModuleFile &M = *I->second;
8718     ASTSelectorLookupTrait Trait(*this, M);
8719     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8720     SelectorsLoaded[ID - 1] =
8721       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8722     if (DeserializationListener)
8723       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8724   }
8725 
8726   return SelectorsLoaded[ID - 1];
8727 }
8728 
8729 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8730   return DecodeSelector(ID);
8731 }
8732 
8733 uint32_t ASTReader::GetNumExternalSelectors() {
8734   // ID 0 (the null selector) is considered an external selector.
8735   return getTotalNumSelectors() + 1;
8736 }
8737 
8738 serialization::SelectorID
8739 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8740   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8741     return LocalID;
8742 
8743   if (!M.ModuleOffsetMap.empty())
8744     ReadModuleOffsetMap(M);
8745 
8746   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8747     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8748   assert(I != M.SelectorRemap.end()
8749          && "Invalid index into selector index remap");
8750 
8751   return LocalID + I->second;
8752 }
8753 
8754 DeclarationNameLoc
8755 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8756   switch (Name.getNameKind()) {
8757   case DeclarationName::CXXConstructorName:
8758   case DeclarationName::CXXDestructorName:
8759   case DeclarationName::CXXConversionFunctionName:
8760     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8761 
8762   case DeclarationName::CXXOperatorName:
8763     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8764 
8765   case DeclarationName::CXXLiteralOperatorName:
8766     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8767         readSourceLocation());
8768 
8769   case DeclarationName::Identifier:
8770   case DeclarationName::ObjCZeroArgSelector:
8771   case DeclarationName::ObjCOneArgSelector:
8772   case DeclarationName::ObjCMultiArgSelector:
8773   case DeclarationName::CXXUsingDirective:
8774   case DeclarationName::CXXDeductionGuideName:
8775     break;
8776   }
8777   return DeclarationNameLoc();
8778 }
8779 
8780 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8781   DeclarationNameInfo NameInfo;
8782   NameInfo.setName(readDeclarationName());
8783   NameInfo.setLoc(readSourceLocation());
8784   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8785   return NameInfo;
8786 }
8787 
8788 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8789   Info.QualifierLoc = readNestedNameSpecifierLoc();
8790   unsigned NumTPLists = readInt();
8791   Info.NumTemplParamLists = NumTPLists;
8792   if (NumTPLists) {
8793     Info.TemplParamLists =
8794         new (getContext()) TemplateParameterList *[NumTPLists];
8795     for (unsigned i = 0; i != NumTPLists; ++i)
8796       Info.TemplParamLists[i] = readTemplateParameterList();
8797   }
8798 }
8799 
8800 TemplateParameterList *
8801 ASTRecordReader::readTemplateParameterList() {
8802   SourceLocation TemplateLoc = readSourceLocation();
8803   SourceLocation LAngleLoc = readSourceLocation();
8804   SourceLocation RAngleLoc = readSourceLocation();
8805 
8806   unsigned NumParams = readInt();
8807   SmallVector<NamedDecl *, 16> Params;
8808   Params.reserve(NumParams);
8809   while (NumParams--)
8810     Params.push_back(readDeclAs<NamedDecl>());
8811 
8812   bool HasRequiresClause = readBool();
8813   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8814 
8815   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8816       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8817   return TemplateParams;
8818 }
8819 
8820 void ASTRecordReader::readTemplateArgumentList(
8821                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8822                         bool Canonicalize) {
8823   unsigned NumTemplateArgs = readInt();
8824   TemplArgs.reserve(NumTemplateArgs);
8825   while (NumTemplateArgs--)
8826     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8827 }
8828 
8829 /// Read a UnresolvedSet structure.
8830 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8831   unsigned NumDecls = readInt();
8832   Set.reserve(getContext(), NumDecls);
8833   while (NumDecls--) {
8834     DeclID ID = readDeclID();
8835     AccessSpecifier AS = (AccessSpecifier) readInt();
8836     Set.addLazyDecl(getContext(), ID, AS);
8837   }
8838 }
8839 
8840 CXXBaseSpecifier
8841 ASTRecordReader::readCXXBaseSpecifier() {
8842   bool isVirtual = readBool();
8843   bool isBaseOfClass = readBool();
8844   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8845   bool inheritConstructors = readBool();
8846   TypeSourceInfo *TInfo = readTypeSourceInfo();
8847   SourceRange Range = readSourceRange();
8848   SourceLocation EllipsisLoc = readSourceLocation();
8849   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8850                           EllipsisLoc);
8851   Result.setInheritConstructors(inheritConstructors);
8852   return Result;
8853 }
8854 
8855 CXXCtorInitializer **
8856 ASTRecordReader::readCXXCtorInitializers() {
8857   ASTContext &Context = getContext();
8858   unsigned NumInitializers = readInt();
8859   assert(NumInitializers && "wrote ctor initializers but have no inits");
8860   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8861   for (unsigned i = 0; i != NumInitializers; ++i) {
8862     TypeSourceInfo *TInfo = nullptr;
8863     bool IsBaseVirtual = false;
8864     FieldDecl *Member = nullptr;
8865     IndirectFieldDecl *IndirectMember = nullptr;
8866 
8867     CtorInitializerType Type = (CtorInitializerType) readInt();
8868     switch (Type) {
8869     case CTOR_INITIALIZER_BASE:
8870       TInfo = readTypeSourceInfo();
8871       IsBaseVirtual = readBool();
8872       break;
8873 
8874     case CTOR_INITIALIZER_DELEGATING:
8875       TInfo = readTypeSourceInfo();
8876       break;
8877 
8878      case CTOR_INITIALIZER_MEMBER:
8879       Member = readDeclAs<FieldDecl>();
8880       break;
8881 
8882      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8883       IndirectMember = readDeclAs<IndirectFieldDecl>();
8884       break;
8885     }
8886 
8887     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8888     Expr *Init = readExpr();
8889     SourceLocation LParenLoc = readSourceLocation();
8890     SourceLocation RParenLoc = readSourceLocation();
8891 
8892     CXXCtorInitializer *BOMInit;
8893     if (Type == CTOR_INITIALIZER_BASE)
8894       BOMInit = new (Context)
8895           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8896                              RParenLoc, MemberOrEllipsisLoc);
8897     else if (Type == CTOR_INITIALIZER_DELEGATING)
8898       BOMInit = new (Context)
8899           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8900     else if (Member)
8901       BOMInit = new (Context)
8902           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8903                              Init, RParenLoc);
8904     else
8905       BOMInit = new (Context)
8906           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8907                              LParenLoc, Init, RParenLoc);
8908 
8909     if (/*IsWritten*/readBool()) {
8910       unsigned SourceOrder = readInt();
8911       BOMInit->setSourceOrder(SourceOrder);
8912     }
8913 
8914     CtorInitializers[i] = BOMInit;
8915   }
8916 
8917   return CtorInitializers;
8918 }
8919 
8920 NestedNameSpecifierLoc
8921 ASTRecordReader::readNestedNameSpecifierLoc() {
8922   ASTContext &Context = getContext();
8923   unsigned N = readInt();
8924   NestedNameSpecifierLocBuilder Builder;
8925   for (unsigned I = 0; I != N; ++I) {
8926     auto Kind = readNestedNameSpecifierKind();
8927     switch (Kind) {
8928     case NestedNameSpecifier::Identifier: {
8929       IdentifierInfo *II = readIdentifier();
8930       SourceRange Range = readSourceRange();
8931       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8932       break;
8933     }
8934 
8935     case NestedNameSpecifier::Namespace: {
8936       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8937       SourceRange Range = readSourceRange();
8938       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8939       break;
8940     }
8941 
8942     case NestedNameSpecifier::NamespaceAlias: {
8943       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8944       SourceRange Range = readSourceRange();
8945       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8946       break;
8947     }
8948 
8949     case NestedNameSpecifier::TypeSpec:
8950     case NestedNameSpecifier::TypeSpecWithTemplate: {
8951       bool Template = readBool();
8952       TypeSourceInfo *T = readTypeSourceInfo();
8953       if (!T)
8954         return NestedNameSpecifierLoc();
8955       SourceLocation ColonColonLoc = readSourceLocation();
8956 
8957       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8958       Builder.Extend(Context,
8959                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8960                      T->getTypeLoc(), ColonColonLoc);
8961       break;
8962     }
8963 
8964     case NestedNameSpecifier::Global: {
8965       SourceLocation ColonColonLoc = readSourceLocation();
8966       Builder.MakeGlobal(Context, ColonColonLoc);
8967       break;
8968     }
8969 
8970     case NestedNameSpecifier::Super: {
8971       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8972       SourceRange Range = readSourceRange();
8973       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8974       break;
8975     }
8976     }
8977   }
8978 
8979   return Builder.getWithLocInContext(Context);
8980 }
8981 
8982 SourceRange
8983 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8984                            unsigned &Idx) {
8985   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8986   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8987   return SourceRange(beg, end);
8988 }
8989 
8990 /// Read a floating-point value
8991 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8992   return llvm::APFloat(Sem, readAPInt());
8993 }
8994 
8995 // Read a string
8996 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8997   unsigned Len = Record[Idx++];
8998   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8999   Idx += Len;
9000   return Result;
9001 }
9002 
9003 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9004                                 unsigned &Idx) {
9005   std::string Filename = ReadString(Record, Idx);
9006   ResolveImportedPath(F, Filename);
9007   return Filename;
9008 }
9009 
9010 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9011                                 const RecordData &Record, unsigned &Idx) {
9012   std::string Filename = ReadString(Record, Idx);
9013   if (!BaseDirectory.empty())
9014     ResolveImportedPath(Filename, BaseDirectory);
9015   return Filename;
9016 }
9017 
9018 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9019                                          unsigned &Idx) {
9020   unsigned Major = Record[Idx++];
9021   unsigned Minor = Record[Idx++];
9022   unsigned Subminor = Record[Idx++];
9023   if (Minor == 0)
9024     return VersionTuple(Major);
9025   if (Subminor == 0)
9026     return VersionTuple(Major, Minor - 1);
9027   return VersionTuple(Major, Minor - 1, Subminor - 1);
9028 }
9029 
9030 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9031                                           const RecordData &Record,
9032                                           unsigned &Idx) {
9033   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9034   return CXXTemporary::Create(getContext(), Decl);
9035 }
9036 
9037 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9038   return Diag(CurrentImportLoc, DiagID);
9039 }
9040 
9041 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9042   return Diags.Report(Loc, DiagID);
9043 }
9044 
9045 /// Retrieve the identifier table associated with the
9046 /// preprocessor.
9047 IdentifierTable &ASTReader::getIdentifierTable() {
9048   return PP.getIdentifierTable();
9049 }
9050 
9051 /// Record that the given ID maps to the given switch-case
9052 /// statement.
9053 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9054   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9055          "Already have a SwitchCase with this ID");
9056   (*CurrSwitchCaseStmts)[ID] = SC;
9057 }
9058 
9059 /// Retrieve the switch-case statement with the given ID.
9060 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9061   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9062   return (*CurrSwitchCaseStmts)[ID];
9063 }
9064 
9065 void ASTReader::ClearSwitchCaseIDs() {
9066   CurrSwitchCaseStmts->clear();
9067 }
9068 
9069 void ASTReader::ReadComments() {
9070   ASTContext &Context = getContext();
9071   std::vector<RawComment *> Comments;
9072   for (SmallVectorImpl<std::pair<BitstreamCursor,
9073                                  serialization::ModuleFile *>>::iterator
9074        I = CommentsCursors.begin(),
9075        E = CommentsCursors.end();
9076        I != E; ++I) {
9077     Comments.clear();
9078     BitstreamCursor &Cursor = I->first;
9079     serialization::ModuleFile &F = *I->second;
9080     SavedStreamPosition SavedPosition(Cursor);
9081 
9082     RecordData Record;
9083     while (true) {
9084       Expected<llvm::BitstreamEntry> MaybeEntry =
9085           Cursor.advanceSkippingSubblocks(
9086               BitstreamCursor::AF_DontPopBlockAtEnd);
9087       if (!MaybeEntry) {
9088         Error(MaybeEntry.takeError());
9089         return;
9090       }
9091       llvm::BitstreamEntry Entry = MaybeEntry.get();
9092 
9093       switch (Entry.Kind) {
9094       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9095       case llvm::BitstreamEntry::Error:
9096         Error("malformed block record in AST file");
9097         return;
9098       case llvm::BitstreamEntry::EndBlock:
9099         goto NextCursor;
9100       case llvm::BitstreamEntry::Record:
9101         // The interesting case.
9102         break;
9103       }
9104 
9105       // Read a record.
9106       Record.clear();
9107       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9108       if (!MaybeComment) {
9109         Error(MaybeComment.takeError());
9110         return;
9111       }
9112       switch ((CommentRecordTypes)MaybeComment.get()) {
9113       case COMMENTS_RAW_COMMENT: {
9114         unsigned Idx = 0;
9115         SourceRange SR = ReadSourceRange(F, Record, Idx);
9116         RawComment::CommentKind Kind =
9117             (RawComment::CommentKind) Record[Idx++];
9118         bool IsTrailingComment = Record[Idx++];
9119         bool IsAlmostTrailingComment = Record[Idx++];
9120         Comments.push_back(new (Context) RawComment(
9121             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9122         break;
9123       }
9124       }
9125     }
9126   NextCursor:
9127     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9128         FileToOffsetToComment;
9129     for (RawComment *C : Comments) {
9130       SourceLocation CommentLoc = C->getBeginLoc();
9131       if (CommentLoc.isValid()) {
9132         std::pair<FileID, unsigned> Loc =
9133             SourceMgr.getDecomposedLoc(CommentLoc);
9134         if (Loc.first.isValid())
9135           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9136       }
9137     }
9138   }
9139 }
9140 
9141 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9142                                 bool IncludeSystem, bool Complain,
9143                     llvm::function_ref<void(const serialization::InputFile &IF,
9144                                             bool isSystem)> Visitor) {
9145   unsigned NumUserInputs = MF.NumUserInputFiles;
9146   unsigned NumInputs = MF.InputFilesLoaded.size();
9147   assert(NumUserInputs <= NumInputs);
9148   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9149   for (unsigned I = 0; I < N; ++I) {
9150     bool IsSystem = I >= NumUserInputs;
9151     InputFile IF = getInputFile(MF, I+1, Complain);
9152     Visitor(IF, IsSystem);
9153   }
9154 }
9155 
9156 void ASTReader::visitTopLevelModuleMaps(
9157     serialization::ModuleFile &MF,
9158     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9159   unsigned NumInputs = MF.InputFilesLoaded.size();
9160   for (unsigned I = 0; I < NumInputs; ++I) {
9161     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9162     if (IFI.TopLevelModuleMap)
9163       // FIXME: This unnecessarily re-reads the InputFileInfo.
9164       if (auto FE = getInputFile(MF, I + 1).getFile())
9165         Visitor(FE);
9166   }
9167 }
9168 
9169 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9170   // If we know the owning module, use it.
9171   if (Module *M = D->getImportedOwningModule())
9172     return M->getFullModuleName();
9173 
9174   // Otherwise, use the name of the top-level module the decl is within.
9175   if (ModuleFile *M = getOwningModuleFile(D))
9176     return M->ModuleName;
9177 
9178   // Not from a module.
9179   return {};
9180 }
9181 
9182 void ASTReader::finishPendingActions() {
9183   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9184          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9185          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9186          !PendingUpdateRecords.empty()) {
9187     // If any identifiers with corresponding top-level declarations have
9188     // been loaded, load those declarations now.
9189     using TopLevelDeclsMap =
9190         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9191     TopLevelDeclsMap TopLevelDecls;
9192 
9193     while (!PendingIdentifierInfos.empty()) {
9194       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9195       SmallVector<uint32_t, 4> DeclIDs =
9196           std::move(PendingIdentifierInfos.back().second);
9197       PendingIdentifierInfos.pop_back();
9198 
9199       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9200     }
9201 
9202     // Load each function type that we deferred loading because it was a
9203     // deduced type that might refer to a local type declared within itself.
9204     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9205       auto *FD = PendingFunctionTypes[I].first;
9206       FD->setType(GetType(PendingFunctionTypes[I].second));
9207 
9208       // If we gave a function a deduced return type, remember that we need to
9209       // propagate that along the redeclaration chain.
9210       auto *DT = FD->getReturnType()->getContainedDeducedType();
9211       if (DT && DT->isDeduced())
9212         PendingDeducedTypeUpdates.insert(
9213             {FD->getCanonicalDecl(), FD->getReturnType()});
9214     }
9215     PendingFunctionTypes.clear();
9216 
9217     // For each decl chain that we wanted to complete while deserializing, mark
9218     // it as "still needs to be completed".
9219     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9220       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9221     }
9222     PendingIncompleteDeclChains.clear();
9223 
9224     // Load pending declaration chains.
9225     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9226       loadPendingDeclChain(PendingDeclChains[I].first,
9227                            PendingDeclChains[I].second);
9228     PendingDeclChains.clear();
9229 
9230     // Make the most recent of the top-level declarations visible.
9231     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9232            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9233       IdentifierInfo *II = TLD->first;
9234       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9235         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9236       }
9237     }
9238 
9239     // Load any pending macro definitions.
9240     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9241       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9242       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9243       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9244       // Initialize the macro history from chained-PCHs ahead of module imports.
9245       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9246            ++IDIdx) {
9247         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9248         if (!Info.M->isModule())
9249           resolvePendingMacro(II, Info);
9250       }
9251       // Handle module imports.
9252       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9253            ++IDIdx) {
9254         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9255         if (Info.M->isModule())
9256           resolvePendingMacro(II, Info);
9257       }
9258     }
9259     PendingMacroIDs.clear();
9260 
9261     // Wire up the DeclContexts for Decls that we delayed setting until
9262     // recursive loading is completed.
9263     while (!PendingDeclContextInfos.empty()) {
9264       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9265       PendingDeclContextInfos.pop_front();
9266       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9267       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9268       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9269     }
9270 
9271     // Perform any pending declaration updates.
9272     while (!PendingUpdateRecords.empty()) {
9273       auto Update = PendingUpdateRecords.pop_back_val();
9274       ReadingKindTracker ReadingKind(Read_Decl, *this);
9275       loadDeclUpdateRecords(Update);
9276     }
9277   }
9278 
9279   // At this point, all update records for loaded decls are in place, so any
9280   // fake class definitions should have become real.
9281   assert(PendingFakeDefinitionData.empty() &&
9282          "faked up a class definition but never saw the real one");
9283 
9284   // If we deserialized any C++ or Objective-C class definitions, any
9285   // Objective-C protocol definitions, or any redeclarable templates, make sure
9286   // that all redeclarations point to the definitions. Note that this can only
9287   // happen now, after the redeclaration chains have been fully wired.
9288   for (Decl *D : PendingDefinitions) {
9289     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9290       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9291         // Make sure that the TagType points at the definition.
9292         const_cast<TagType*>(TagT)->decl = TD;
9293       }
9294 
9295       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9296         for (auto *R = getMostRecentExistingDecl(RD); R;
9297              R = R->getPreviousDecl()) {
9298           assert((R == D) ==
9299                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9300                  "declaration thinks it's the definition but it isn't");
9301           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9302         }
9303       }
9304 
9305       continue;
9306     }
9307 
9308     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9309       // Make sure that the ObjCInterfaceType points at the definition.
9310       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9311         ->Decl = ID;
9312 
9313       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9314         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9315 
9316       continue;
9317     }
9318 
9319     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9320       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9321         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9322 
9323       continue;
9324     }
9325 
9326     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9327     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9328       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9329   }
9330   PendingDefinitions.clear();
9331 
9332   // Load the bodies of any functions or methods we've encountered. We do
9333   // this now (delayed) so that we can be sure that the declaration chains
9334   // have been fully wired up (hasBody relies on this).
9335   // FIXME: We shouldn't require complete redeclaration chains here.
9336   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9337                                PBEnd = PendingBodies.end();
9338        PB != PBEnd; ++PB) {
9339     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9340       // For a function defined inline within a class template, force the
9341       // canonical definition to be the one inside the canonical definition of
9342       // the template. This ensures that we instantiate from a correct view
9343       // of the template.
9344       //
9345       // Sadly we can't do this more generally: we can't be sure that all
9346       // copies of an arbitrary class definition will have the same members
9347       // defined (eg, some member functions may not be instantiated, and some
9348       // special members may or may not have been implicitly defined).
9349       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9350         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9351           continue;
9352 
9353       // FIXME: Check for =delete/=default?
9354       // FIXME: Complain about ODR violations here?
9355       const FunctionDecl *Defn = nullptr;
9356       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9357         FD->setLazyBody(PB->second);
9358       } else {
9359         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9360         mergeDefinitionVisibility(NonConstDefn, FD);
9361 
9362         if (!FD->isLateTemplateParsed() &&
9363             !NonConstDefn->isLateTemplateParsed() &&
9364             FD->getODRHash() != NonConstDefn->getODRHash()) {
9365           if (!isa<CXXMethodDecl>(FD)) {
9366             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9367           } else if (FD->getLexicalParent()->isFileContext() &&
9368                      NonConstDefn->getLexicalParent()->isFileContext()) {
9369             // Only diagnose out-of-line method definitions.  If they are
9370             // in class definitions, then an error will be generated when
9371             // processing the class bodies.
9372             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9373           }
9374         }
9375       }
9376       continue;
9377     }
9378 
9379     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9380     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9381       MD->setLazyBody(PB->second);
9382   }
9383   PendingBodies.clear();
9384 
9385   // Do some cleanup.
9386   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9387     getContext().deduplicateMergedDefinitonsFor(ND);
9388   PendingMergedDefinitionsToDeduplicate.clear();
9389 }
9390 
9391 void ASTReader::diagnoseOdrViolations() {
9392   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9393       PendingFunctionOdrMergeFailures.empty() &&
9394       PendingEnumOdrMergeFailures.empty())
9395     return;
9396 
9397   // Trigger the import of the full definition of each class that had any
9398   // odr-merging problems, so we can produce better diagnostics for them.
9399   // These updates may in turn find and diagnose some ODR failures, so take
9400   // ownership of the set first.
9401   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9402   PendingOdrMergeFailures.clear();
9403   for (auto &Merge : OdrMergeFailures) {
9404     Merge.first->buildLookup();
9405     Merge.first->decls_begin();
9406     Merge.first->bases_begin();
9407     Merge.first->vbases_begin();
9408     for (auto &RecordPair : Merge.second) {
9409       auto *RD = RecordPair.first;
9410       RD->decls_begin();
9411       RD->bases_begin();
9412       RD->vbases_begin();
9413     }
9414   }
9415 
9416   // Trigger the import of functions.
9417   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9418   PendingFunctionOdrMergeFailures.clear();
9419   for (auto &Merge : FunctionOdrMergeFailures) {
9420     Merge.first->buildLookup();
9421     Merge.first->decls_begin();
9422     Merge.first->getBody();
9423     for (auto &FD : Merge.second) {
9424       FD->buildLookup();
9425       FD->decls_begin();
9426       FD->getBody();
9427     }
9428   }
9429 
9430   // Trigger the import of enums.
9431   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9432   PendingEnumOdrMergeFailures.clear();
9433   for (auto &Merge : EnumOdrMergeFailures) {
9434     Merge.first->decls_begin();
9435     for (auto &Enum : Merge.second) {
9436       Enum->decls_begin();
9437     }
9438   }
9439 
9440   // For each declaration from a merged context, check that the canonical
9441   // definition of that context also contains a declaration of the same
9442   // entity.
9443   //
9444   // Caution: this loop does things that might invalidate iterators into
9445   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9446   while (!PendingOdrMergeChecks.empty()) {
9447     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9448 
9449     // FIXME: Skip over implicit declarations for now. This matters for things
9450     // like implicitly-declared special member functions. This isn't entirely
9451     // correct; we can end up with multiple unmerged declarations of the same
9452     // implicit entity.
9453     if (D->isImplicit())
9454       continue;
9455 
9456     DeclContext *CanonDef = D->getDeclContext();
9457 
9458     bool Found = false;
9459     const Decl *DCanon = D->getCanonicalDecl();
9460 
9461     for (auto RI : D->redecls()) {
9462       if (RI->getLexicalDeclContext() == CanonDef) {
9463         Found = true;
9464         break;
9465       }
9466     }
9467     if (Found)
9468       continue;
9469 
9470     // Quick check failed, time to do the slow thing. Note, we can't just
9471     // look up the name of D in CanonDef here, because the member that is
9472     // in CanonDef might not be found by name lookup (it might have been
9473     // replaced by a more recent declaration in the lookup table), and we
9474     // can't necessarily find it in the redeclaration chain because it might
9475     // be merely mergeable, not redeclarable.
9476     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9477     for (auto *CanonMember : CanonDef->decls()) {
9478       if (CanonMember->getCanonicalDecl() == DCanon) {
9479         // This can happen if the declaration is merely mergeable and not
9480         // actually redeclarable (we looked for redeclarations earlier).
9481         //
9482         // FIXME: We should be able to detect this more efficiently, without
9483         // pulling in all of the members of CanonDef.
9484         Found = true;
9485         break;
9486       }
9487       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9488         if (ND->getDeclName() == D->getDeclName())
9489           Candidates.push_back(ND);
9490     }
9491 
9492     if (!Found) {
9493       // The AST doesn't like TagDecls becoming invalid after they've been
9494       // completed. We only really need to mark FieldDecls as invalid here.
9495       if (!isa<TagDecl>(D))
9496         D->setInvalidDecl();
9497 
9498       // Ensure we don't accidentally recursively enter deserialization while
9499       // we're producing our diagnostic.
9500       Deserializing RecursionGuard(this);
9501 
9502       std::string CanonDefModule =
9503           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9504       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9505         << D << getOwningModuleNameForDiagnostic(D)
9506         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9507 
9508       if (Candidates.empty())
9509         Diag(cast<Decl>(CanonDef)->getLocation(),
9510              diag::note_module_odr_violation_no_possible_decls) << D;
9511       else {
9512         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9513           Diag(Candidates[I]->getLocation(),
9514                diag::note_module_odr_violation_possible_decl)
9515             << Candidates[I];
9516       }
9517 
9518       DiagnosedOdrMergeFailures.insert(CanonDef);
9519     }
9520   }
9521 
9522   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9523       EnumOdrMergeFailures.empty())
9524     return;
9525 
9526   // Ensure we don't accidentally recursively enter deserialization while
9527   // we're producing our diagnostics.
9528   Deserializing RecursionGuard(this);
9529 
9530   // Common code for hashing helpers.
9531   ODRHash Hash;
9532   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9533     Hash.clear();
9534     Hash.AddQualType(Ty);
9535     return Hash.CalculateHash();
9536   };
9537 
9538   auto ComputeODRHash = [&Hash](const Stmt *S) {
9539     assert(S);
9540     Hash.clear();
9541     Hash.AddStmt(S);
9542     return Hash.CalculateHash();
9543   };
9544 
9545   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9546     assert(D);
9547     Hash.clear();
9548     Hash.AddSubDecl(D);
9549     return Hash.CalculateHash();
9550   };
9551 
9552   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9553     Hash.clear();
9554     Hash.AddTemplateArgument(TA);
9555     return Hash.CalculateHash();
9556   };
9557 
9558   auto ComputeTemplateParameterListODRHash =
9559       [&Hash](const TemplateParameterList *TPL) {
9560         assert(TPL);
9561         Hash.clear();
9562         Hash.AddTemplateParameterList(TPL);
9563         return Hash.CalculateHash();
9564       };
9565 
9566   // Used with err_module_odr_violation_mismatch_decl and
9567   // note_module_odr_violation_mismatch_decl
9568   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9569   enum ODRMismatchDecl {
9570     EndOfClass,
9571     PublicSpecifer,
9572     PrivateSpecifer,
9573     ProtectedSpecifer,
9574     StaticAssert,
9575     Field,
9576     CXXMethod,
9577     TypeAlias,
9578     TypeDef,
9579     Var,
9580     Friend,
9581     FunctionTemplate,
9582     Other
9583   };
9584 
9585   // Used with err_module_odr_violation_mismatch_decl_diff and
9586   // note_module_odr_violation_mismatch_decl_diff
9587   enum ODRMismatchDeclDifference {
9588     StaticAssertCondition,
9589     StaticAssertMessage,
9590     StaticAssertOnlyMessage,
9591     FieldName,
9592     FieldTypeName,
9593     FieldSingleBitField,
9594     FieldDifferentWidthBitField,
9595     FieldSingleMutable,
9596     FieldSingleInitializer,
9597     FieldDifferentInitializers,
9598     MethodName,
9599     MethodDeleted,
9600     MethodDefaulted,
9601     MethodVirtual,
9602     MethodStatic,
9603     MethodVolatile,
9604     MethodConst,
9605     MethodInline,
9606     MethodNumberParameters,
9607     MethodParameterType,
9608     MethodParameterName,
9609     MethodParameterSingleDefaultArgument,
9610     MethodParameterDifferentDefaultArgument,
9611     MethodNoTemplateArguments,
9612     MethodDifferentNumberTemplateArguments,
9613     MethodDifferentTemplateArgument,
9614     MethodSingleBody,
9615     MethodDifferentBody,
9616     TypedefName,
9617     TypedefType,
9618     VarName,
9619     VarType,
9620     VarSingleInitializer,
9621     VarDifferentInitializer,
9622     VarConstexpr,
9623     FriendTypeFunction,
9624     FriendType,
9625     FriendFunction,
9626     FunctionTemplateDifferentNumberParameters,
9627     FunctionTemplateParameterDifferentKind,
9628     FunctionTemplateParameterName,
9629     FunctionTemplateParameterSingleDefaultArgument,
9630     FunctionTemplateParameterDifferentDefaultArgument,
9631     FunctionTemplateParameterDifferentType,
9632     FunctionTemplatePackParameter,
9633   };
9634 
9635   // These lambdas have the common portions of the ODR diagnostics.  This
9636   // has the same return as Diag(), so addition parameters can be passed
9637   // in with operator<<
9638   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9639                                  SourceLocation Loc, SourceRange Range,
9640                                  ODRMismatchDeclDifference DiffType) {
9641     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9642            << FirstRecord << FirstModule.empty() << FirstModule << Range
9643            << DiffType;
9644   };
9645   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9646                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9647     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9648            << SecondModule << Range << DiffType;
9649   };
9650 
9651   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9652                        &ComputeQualTypeODRHash, &ComputeODRHash](
9653                           NamedDecl *FirstRecord, StringRef FirstModule,
9654                           StringRef SecondModule, FieldDecl *FirstField,
9655                           FieldDecl *SecondField) {
9656     IdentifierInfo *FirstII = FirstField->getIdentifier();
9657     IdentifierInfo *SecondII = SecondField->getIdentifier();
9658     if (FirstII->getName() != SecondII->getName()) {
9659       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9660                        FirstField->getSourceRange(), FieldName)
9661           << FirstII;
9662       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9663                       SecondField->getSourceRange(), FieldName)
9664           << SecondII;
9665 
9666       return true;
9667     }
9668 
9669     assert(getContext().hasSameType(FirstField->getType(),
9670                                     SecondField->getType()));
9671 
9672     QualType FirstType = FirstField->getType();
9673     QualType SecondType = SecondField->getType();
9674     if (ComputeQualTypeODRHash(FirstType) !=
9675         ComputeQualTypeODRHash(SecondType)) {
9676       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9677                        FirstField->getSourceRange(), FieldTypeName)
9678           << FirstII << FirstType;
9679       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9680                       SecondField->getSourceRange(), FieldTypeName)
9681           << SecondII << SecondType;
9682 
9683       return true;
9684     }
9685 
9686     const bool IsFirstBitField = FirstField->isBitField();
9687     const bool IsSecondBitField = SecondField->isBitField();
9688     if (IsFirstBitField != IsSecondBitField) {
9689       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9690                        FirstField->getSourceRange(), FieldSingleBitField)
9691           << FirstII << IsFirstBitField;
9692       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9693                       SecondField->getSourceRange(), FieldSingleBitField)
9694           << SecondII << IsSecondBitField;
9695       return true;
9696     }
9697 
9698     if (IsFirstBitField && IsSecondBitField) {
9699       unsigned FirstBitWidthHash =
9700           ComputeODRHash(FirstField->getBitWidth());
9701       unsigned SecondBitWidthHash =
9702           ComputeODRHash(SecondField->getBitWidth());
9703       if (FirstBitWidthHash != SecondBitWidthHash) {
9704         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9705                          FirstField->getSourceRange(),
9706                          FieldDifferentWidthBitField)
9707             << FirstII << FirstField->getBitWidth()->getSourceRange();
9708         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9709                         SecondField->getSourceRange(),
9710                         FieldDifferentWidthBitField)
9711             << SecondII << SecondField->getBitWidth()->getSourceRange();
9712         return true;
9713       }
9714     }
9715 
9716     if (!PP.getLangOpts().CPlusPlus)
9717       return false;
9718 
9719     const bool IsFirstMutable = FirstField->isMutable();
9720     const bool IsSecondMutable = SecondField->isMutable();
9721     if (IsFirstMutable != IsSecondMutable) {
9722       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9723                        FirstField->getSourceRange(), FieldSingleMutable)
9724           << FirstII << IsFirstMutable;
9725       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9726                       SecondField->getSourceRange(), FieldSingleMutable)
9727           << SecondII << IsSecondMutable;
9728       return true;
9729     }
9730 
9731     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9732     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9733     if ((!FirstInitializer && SecondInitializer) ||
9734         (FirstInitializer && !SecondInitializer)) {
9735       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9736                        FirstField->getSourceRange(), FieldSingleInitializer)
9737           << FirstII << (FirstInitializer != nullptr);
9738       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9739                       SecondField->getSourceRange(), FieldSingleInitializer)
9740           << SecondII << (SecondInitializer != nullptr);
9741       return true;
9742     }
9743 
9744     if (FirstInitializer && SecondInitializer) {
9745       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9746       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9747       if (FirstInitHash != SecondInitHash) {
9748         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9749                          FirstField->getSourceRange(),
9750                          FieldDifferentInitializers)
9751             << FirstII << FirstInitializer->getSourceRange();
9752         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9753                         SecondField->getSourceRange(),
9754                         FieldDifferentInitializers)
9755             << SecondII << SecondInitializer->getSourceRange();
9756         return true;
9757       }
9758     }
9759 
9760     return false;
9761   };
9762 
9763   auto ODRDiagTypeDefOrAlias =
9764       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9765           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9766           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9767           bool IsTypeAlias) {
9768         auto FirstName = FirstTD->getDeclName();
9769         auto SecondName = SecondTD->getDeclName();
9770         if (FirstName != SecondName) {
9771           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9772                            FirstTD->getSourceRange(), TypedefName)
9773               << IsTypeAlias << FirstName;
9774           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9775                           SecondTD->getSourceRange(), TypedefName)
9776               << IsTypeAlias << SecondName;
9777           return true;
9778         }
9779 
9780         QualType FirstType = FirstTD->getUnderlyingType();
9781         QualType SecondType = SecondTD->getUnderlyingType();
9782         if (ComputeQualTypeODRHash(FirstType) !=
9783             ComputeQualTypeODRHash(SecondType)) {
9784           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9785                            FirstTD->getSourceRange(), TypedefType)
9786               << IsTypeAlias << FirstName << FirstType;
9787           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9788                           SecondTD->getSourceRange(), TypedefType)
9789               << IsTypeAlias << SecondName << SecondType;
9790           return true;
9791         }
9792 
9793         return false;
9794   };
9795 
9796   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9797                      &ComputeQualTypeODRHash, &ComputeODRHash,
9798                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9799                            StringRef SecondModule, VarDecl *FirstVD,
9800                            VarDecl *SecondVD) {
9801     auto FirstName = FirstVD->getDeclName();
9802     auto SecondName = SecondVD->getDeclName();
9803     if (FirstName != SecondName) {
9804       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9805                        FirstVD->getSourceRange(), VarName)
9806           << FirstName;
9807       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9808                       SecondVD->getSourceRange(), VarName)
9809           << SecondName;
9810       return true;
9811     }
9812 
9813     QualType FirstType = FirstVD->getType();
9814     QualType SecondType = SecondVD->getType();
9815     if (ComputeQualTypeODRHash(FirstType) !=
9816         ComputeQualTypeODRHash(SecondType)) {
9817       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9818                        FirstVD->getSourceRange(), VarType)
9819           << FirstName << FirstType;
9820       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9821                       SecondVD->getSourceRange(), VarType)
9822           << SecondName << SecondType;
9823       return true;
9824     }
9825 
9826     if (!PP.getLangOpts().CPlusPlus)
9827       return false;
9828 
9829     const Expr *FirstInit = FirstVD->getInit();
9830     const Expr *SecondInit = SecondVD->getInit();
9831     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9832       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9833                        FirstVD->getSourceRange(), VarSingleInitializer)
9834           << FirstName << (FirstInit == nullptr)
9835           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9836       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9837                       SecondVD->getSourceRange(), VarSingleInitializer)
9838           << SecondName << (SecondInit == nullptr)
9839           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9840       return true;
9841     }
9842 
9843     if (FirstInit && SecondInit &&
9844         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9845       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9846                        FirstVD->getSourceRange(), VarDifferentInitializer)
9847           << FirstName << FirstInit->getSourceRange();
9848       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9849                       SecondVD->getSourceRange(), VarDifferentInitializer)
9850           << SecondName << SecondInit->getSourceRange();
9851       return true;
9852     }
9853 
9854     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9855     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9856     if (FirstIsConstexpr != SecondIsConstexpr) {
9857       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9858                        FirstVD->getSourceRange(), VarConstexpr)
9859           << FirstName << FirstIsConstexpr;
9860       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9861                       SecondVD->getSourceRange(), VarConstexpr)
9862           << SecondName << SecondIsConstexpr;
9863       return true;
9864     }
9865     return false;
9866   };
9867 
9868   auto DifferenceSelector = [](Decl *D) {
9869     assert(D && "valid Decl required");
9870     switch (D->getKind()) {
9871     default:
9872       return Other;
9873     case Decl::AccessSpec:
9874       switch (D->getAccess()) {
9875       case AS_public:
9876         return PublicSpecifer;
9877       case AS_private:
9878         return PrivateSpecifer;
9879       case AS_protected:
9880         return ProtectedSpecifer;
9881       case AS_none:
9882         break;
9883       }
9884       llvm_unreachable("Invalid access specifier");
9885     case Decl::StaticAssert:
9886       return StaticAssert;
9887     case Decl::Field:
9888       return Field;
9889     case Decl::CXXMethod:
9890     case Decl::CXXConstructor:
9891     case Decl::CXXDestructor:
9892       return CXXMethod;
9893     case Decl::TypeAlias:
9894       return TypeAlias;
9895     case Decl::Typedef:
9896       return TypeDef;
9897     case Decl::Var:
9898       return Var;
9899     case Decl::Friend:
9900       return Friend;
9901     case Decl::FunctionTemplate:
9902       return FunctionTemplate;
9903     }
9904   };
9905 
9906   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9907   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9908                                                  RecordDecl *Record,
9909                                                  const DeclContext *DC) {
9910     for (auto *D : Record->decls()) {
9911       if (!ODRHash::isDeclToBeProcessed(D, DC))
9912         continue;
9913       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9914     }
9915   };
9916 
9917   struct DiffResult {
9918     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9919     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9920   };
9921 
9922   // If there is a diagnoseable difference, FirstDiffType and
9923   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9924   // filled in if not EndOfClass.
9925   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9926                                              DeclHashes &SecondHashes) {
9927     DiffResult DR;
9928     auto FirstIt = FirstHashes.begin();
9929     auto SecondIt = SecondHashes.begin();
9930     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9931       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9932           FirstIt->second == SecondIt->second) {
9933         ++FirstIt;
9934         ++SecondIt;
9935         continue;
9936       }
9937 
9938       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9939       DR.SecondDecl =
9940           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9941 
9942       DR.FirstDiffType =
9943           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9944       DR.SecondDiffType =
9945           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9946       return DR;
9947     }
9948     return DR;
9949   };
9950 
9951   // Use this to diagnose that an unexpected Decl was encountered
9952   // or no difference was detected. This causes a generic error
9953   // message to be emitted.
9954   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9955                                       StringRef FirstModule,
9956                                       NamedDecl *SecondRecord,
9957                                       StringRef SecondModule) {
9958     Diag(FirstRecord->getLocation(),
9959          diag::err_module_odr_violation_different_definitions)
9960         << FirstRecord << FirstModule.empty() << FirstModule;
9961 
9962     if (DR.FirstDecl) {
9963       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9964           << FirstRecord << DR.FirstDecl->getSourceRange();
9965     }
9966 
9967     Diag(SecondRecord->getLocation(),
9968          diag::note_module_odr_violation_different_definitions)
9969         << SecondModule;
9970 
9971     if (DR.SecondDecl) {
9972       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9973           << DR.SecondDecl->getSourceRange();
9974     }
9975   };
9976 
9977   auto DiagnoseODRMismatch =
9978       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9979              NamedDecl *SecondRecord, StringRef SecondModule) {
9980         SourceLocation FirstLoc;
9981         SourceRange FirstRange;
9982         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9983         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9984           FirstLoc = FirstTag->getBraceRange().getEnd();
9985         } else {
9986           FirstLoc = DR.FirstDecl->getLocation();
9987           FirstRange = DR.FirstDecl->getSourceRange();
9988         }
9989         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9990             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9991             << DR.FirstDiffType;
9992 
9993         SourceLocation SecondLoc;
9994         SourceRange SecondRange;
9995         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9996         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9997           SecondLoc = SecondTag->getBraceRange().getEnd();
9998         } else {
9999           SecondLoc = DR.SecondDecl->getLocation();
10000           SecondRange = DR.SecondDecl->getSourceRange();
10001         }
10002         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10003             << SecondModule << SecondRange << DR.SecondDiffType;
10004       };
10005 
10006   // Issue any pending ODR-failure diagnostics.
10007   for (auto &Merge : OdrMergeFailures) {
10008     // If we've already pointed out a specific problem with this class, don't
10009     // bother issuing a general "something's different" diagnostic.
10010     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10011       continue;
10012 
10013     bool Diagnosed = false;
10014     CXXRecordDecl *FirstRecord = Merge.first;
10015     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10016     for (auto &RecordPair : Merge.second) {
10017       CXXRecordDecl *SecondRecord = RecordPair.first;
10018       // Multiple different declarations got merged together; tell the user
10019       // where they came from.
10020       if (FirstRecord == SecondRecord)
10021         continue;
10022 
10023       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10024 
10025       auto *FirstDD = FirstRecord->DefinitionData;
10026       auto *SecondDD = RecordPair.second;
10027 
10028       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10029 
10030       // Diagnostics from DefinitionData are emitted here.
10031       if (FirstDD != SecondDD) {
10032         enum ODRDefinitionDataDifference {
10033           NumBases,
10034           NumVBases,
10035           BaseType,
10036           BaseVirtual,
10037           BaseAccess,
10038         };
10039         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10040                                  this](SourceLocation Loc, SourceRange Range,
10041                                        ODRDefinitionDataDifference DiffType) {
10042           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10043                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10044                  << DiffType;
10045         };
10046         auto ODRDiagBaseNote = [&SecondModule,
10047                                 this](SourceLocation Loc, SourceRange Range,
10048                                       ODRDefinitionDataDifference DiffType) {
10049           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10050                  << SecondModule << Range << DiffType;
10051         };
10052 
10053         unsigned FirstNumBases = FirstDD->NumBases;
10054         unsigned FirstNumVBases = FirstDD->NumVBases;
10055         unsigned SecondNumBases = SecondDD->NumBases;
10056         unsigned SecondNumVBases = SecondDD->NumVBases;
10057 
10058         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10059           unsigned NumBases = DD->NumBases;
10060           if (NumBases == 0) return SourceRange();
10061           auto bases = DD->bases();
10062           return SourceRange(bases[0].getBeginLoc(),
10063                              bases[NumBases - 1].getEndLoc());
10064         };
10065 
10066         if (FirstNumBases != SecondNumBases) {
10067           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10068                            NumBases)
10069               << FirstNumBases;
10070           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10071                           NumBases)
10072               << SecondNumBases;
10073           Diagnosed = true;
10074           break;
10075         }
10076 
10077         if (FirstNumVBases != SecondNumVBases) {
10078           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10079                            NumVBases)
10080               << FirstNumVBases;
10081           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10082                           NumVBases)
10083               << SecondNumVBases;
10084           Diagnosed = true;
10085           break;
10086         }
10087 
10088         auto FirstBases = FirstDD->bases();
10089         auto SecondBases = SecondDD->bases();
10090         unsigned i = 0;
10091         for (i = 0; i < FirstNumBases; ++i) {
10092           auto FirstBase = FirstBases[i];
10093           auto SecondBase = SecondBases[i];
10094           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10095               ComputeQualTypeODRHash(SecondBase.getType())) {
10096             ODRDiagBaseError(FirstRecord->getLocation(),
10097                              FirstBase.getSourceRange(), BaseType)
10098                 << (i + 1) << FirstBase.getType();
10099             ODRDiagBaseNote(SecondRecord->getLocation(),
10100                             SecondBase.getSourceRange(), BaseType)
10101                 << (i + 1) << SecondBase.getType();
10102             break;
10103           }
10104 
10105           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10106             ODRDiagBaseError(FirstRecord->getLocation(),
10107                              FirstBase.getSourceRange(), BaseVirtual)
10108                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10109             ODRDiagBaseNote(SecondRecord->getLocation(),
10110                             SecondBase.getSourceRange(), BaseVirtual)
10111                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10112             break;
10113           }
10114 
10115           if (FirstBase.getAccessSpecifierAsWritten() !=
10116               SecondBase.getAccessSpecifierAsWritten()) {
10117             ODRDiagBaseError(FirstRecord->getLocation(),
10118                              FirstBase.getSourceRange(), BaseAccess)
10119                 << (i + 1) << FirstBase.getType()
10120                 << (int)FirstBase.getAccessSpecifierAsWritten();
10121             ODRDiagBaseNote(SecondRecord->getLocation(),
10122                             SecondBase.getSourceRange(), BaseAccess)
10123                 << (i + 1) << SecondBase.getType()
10124                 << (int)SecondBase.getAccessSpecifierAsWritten();
10125             break;
10126           }
10127         }
10128 
10129         if (i != FirstNumBases) {
10130           Diagnosed = true;
10131           break;
10132         }
10133       }
10134 
10135       const ClassTemplateDecl *FirstTemplate =
10136           FirstRecord->getDescribedClassTemplate();
10137       const ClassTemplateDecl *SecondTemplate =
10138           SecondRecord->getDescribedClassTemplate();
10139 
10140       assert(!FirstTemplate == !SecondTemplate &&
10141              "Both pointers should be null or non-null");
10142 
10143       if (FirstTemplate && SecondTemplate) {
10144         DeclHashes FirstTemplateHashes;
10145         DeclHashes SecondTemplateHashes;
10146 
10147         auto PopulateTemplateParameterHashs =
10148             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10149                                      const ClassTemplateDecl *TD) {
10150               for (auto *D : TD->getTemplateParameters()->asArray()) {
10151                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10152               }
10153             };
10154 
10155         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10156         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10157 
10158         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10159                "Number of template parameters should be equal.");
10160 
10161         auto FirstIt = FirstTemplateHashes.begin();
10162         auto FirstEnd = FirstTemplateHashes.end();
10163         auto SecondIt = SecondTemplateHashes.begin();
10164         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10165           if (FirstIt->second == SecondIt->second)
10166             continue;
10167 
10168           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10169           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10170 
10171           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10172                  "Parameter Decl's should be the same kind.");
10173 
10174           enum ODRTemplateDifference {
10175             ParamEmptyName,
10176             ParamName,
10177             ParamSingleDefaultArgument,
10178             ParamDifferentDefaultArgument,
10179           };
10180 
10181           auto hasDefaultArg = [](const NamedDecl *D) {
10182             if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(D))
10183               return TTP->hasDefaultArgument() &&
10184                       !TTP->defaultArgumentWasInherited();
10185             if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
10186               return NTTP->hasDefaultArgument() &&
10187                       !NTTP->defaultArgumentWasInherited();
10188             auto *TTP = cast<TemplateTemplateParmDecl>(D);
10189             return TTP->hasDefaultArgument() &&
10190                     !TTP->defaultArgumentWasInherited();
10191           };
10192           bool hasFirstArg = hasDefaultArg(FirstDecl);
10193           bool hasSecondArg = hasDefaultArg(SecondDecl);
10194 
10195           ODRTemplateDifference ErrDiffType;
10196           ODRTemplateDifference NoteDiffType;
10197 
10198           DeclarationName FirstName = FirstDecl->getDeclName();
10199           DeclarationName SecondName = SecondDecl->getDeclName();
10200 
10201           if (FirstName != SecondName) {
10202             bool FirstNameEmpty =
10203                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10204             bool SecondNameEmpty = SecondName.isIdentifier() &&
10205                                     !SecondName.getAsIdentifierInfo();
10206             ErrDiffType = FirstNameEmpty ? ParamEmptyName : ParamName;
10207             NoteDiffType = SecondNameEmpty ? ParamEmptyName : ParamName;
10208           } else if (hasFirstArg == hasSecondArg)
10209             ErrDiffType = NoteDiffType = ParamDifferentDefaultArgument;
10210           else
10211             ErrDiffType = NoteDiffType = ParamSingleDefaultArgument;
10212 
10213           Diag(FirstDecl->getLocation(),
10214                 diag::err_module_odr_violation_template_parameter)
10215               << FirstRecord << FirstModule.empty() << FirstModule
10216               << FirstDecl->getSourceRange() << ErrDiffType << hasFirstArg
10217               << FirstName;
10218           Diag(SecondDecl->getLocation(),
10219                 diag::note_module_odr_violation_template_parameter)
10220               << SecondModule << SecondDecl->getSourceRange() << NoteDiffType
10221               << hasSecondArg << SecondName;
10222           break;
10223         }
10224 
10225         if (FirstIt != FirstEnd) {
10226           Diagnosed = true;
10227           break;
10228         }
10229       }
10230 
10231       DeclHashes FirstHashes;
10232       DeclHashes SecondHashes;
10233       const DeclContext *DC = FirstRecord;
10234       PopulateHashes(FirstHashes, FirstRecord, DC);
10235       PopulateHashes(SecondHashes, SecondRecord, DC);
10236 
10237       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10238       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10239       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10240       Decl *FirstDecl = DR.FirstDecl;
10241       Decl *SecondDecl = DR.SecondDecl;
10242 
10243       if (FirstDiffType == Other || SecondDiffType == Other) {
10244         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10245                               SecondModule);
10246         Diagnosed = true;
10247         break;
10248       }
10249 
10250       if (FirstDiffType != SecondDiffType) {
10251         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10252                             SecondModule);
10253         Diagnosed = true;
10254         break;
10255       }
10256 
10257       assert(FirstDiffType == SecondDiffType);
10258 
10259       switch (FirstDiffType) {
10260       case Other:
10261       case EndOfClass:
10262       case PublicSpecifer:
10263       case PrivateSpecifer:
10264       case ProtectedSpecifer:
10265         llvm_unreachable("Invalid diff type");
10266 
10267       case StaticAssert: {
10268         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10269         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10270 
10271         Expr *FirstExpr = FirstSA->getAssertExpr();
10272         Expr *SecondExpr = SecondSA->getAssertExpr();
10273         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10274         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10275         if (FirstODRHash != SecondODRHash) {
10276           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10277                            FirstExpr->getSourceRange(), StaticAssertCondition);
10278           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10279                           SecondExpr->getSourceRange(), StaticAssertCondition);
10280           Diagnosed = true;
10281           break;
10282         }
10283 
10284         StringLiteral *FirstStr = FirstSA->getMessage();
10285         StringLiteral *SecondStr = SecondSA->getMessage();
10286         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10287         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10288           SourceLocation FirstLoc, SecondLoc;
10289           SourceRange FirstRange, SecondRange;
10290           if (FirstStr) {
10291             FirstLoc = FirstStr->getBeginLoc();
10292             FirstRange = FirstStr->getSourceRange();
10293           } else {
10294             FirstLoc = FirstSA->getBeginLoc();
10295             FirstRange = FirstSA->getSourceRange();
10296           }
10297           if (SecondStr) {
10298             SecondLoc = SecondStr->getBeginLoc();
10299             SecondRange = SecondStr->getSourceRange();
10300           } else {
10301             SecondLoc = SecondSA->getBeginLoc();
10302             SecondRange = SecondSA->getSourceRange();
10303           }
10304           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10305                            StaticAssertOnlyMessage)
10306               << (FirstStr == nullptr);
10307           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10308                           StaticAssertOnlyMessage)
10309               << (SecondStr == nullptr);
10310           Diagnosed = true;
10311           break;
10312         }
10313 
10314         if (FirstStr && SecondStr &&
10315             FirstStr->getString() != SecondStr->getString()) {
10316           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10317                            FirstStr->getSourceRange(), StaticAssertMessage);
10318           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10319                           SecondStr->getSourceRange(), StaticAssertMessage);
10320           Diagnosed = true;
10321           break;
10322         }
10323         break;
10324       }
10325       case Field: {
10326         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10327                                  cast<FieldDecl>(FirstDecl),
10328                                  cast<FieldDecl>(SecondDecl));
10329         break;
10330       }
10331       case CXXMethod: {
10332         enum {
10333           DiagMethod,
10334           DiagConstructor,
10335           DiagDestructor,
10336         } FirstMethodType,
10337             SecondMethodType;
10338         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10339           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10340           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10341           return DiagMethod;
10342         };
10343         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10344         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10345         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10346         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10347         auto FirstName = FirstMethod->getDeclName();
10348         auto SecondName = SecondMethod->getDeclName();
10349         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10350           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10351                            FirstMethod->getSourceRange(), MethodName)
10352               << FirstMethodType << FirstName;
10353           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10354                           SecondMethod->getSourceRange(), MethodName)
10355               << SecondMethodType << SecondName;
10356 
10357           Diagnosed = true;
10358           break;
10359         }
10360 
10361         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10362         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10363         if (FirstDeleted != SecondDeleted) {
10364           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10365                            FirstMethod->getSourceRange(), MethodDeleted)
10366               << FirstMethodType << FirstName << FirstDeleted;
10367 
10368           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10369                           SecondMethod->getSourceRange(), MethodDeleted)
10370               << SecondMethodType << SecondName << SecondDeleted;
10371           Diagnosed = true;
10372           break;
10373         }
10374 
10375         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10376         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10377         if (FirstDefaulted != SecondDefaulted) {
10378           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10379                            FirstMethod->getSourceRange(), MethodDefaulted)
10380               << FirstMethodType << FirstName << FirstDefaulted;
10381 
10382           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10383                           SecondMethod->getSourceRange(), MethodDefaulted)
10384               << SecondMethodType << SecondName << SecondDefaulted;
10385           Diagnosed = true;
10386           break;
10387         }
10388 
10389         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10390         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10391         const bool FirstPure = FirstMethod->isPure();
10392         const bool SecondPure = SecondMethod->isPure();
10393         if ((FirstVirtual || SecondVirtual) &&
10394             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10395           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10396                            FirstMethod->getSourceRange(), MethodVirtual)
10397               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10398           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10399                           SecondMethod->getSourceRange(), MethodVirtual)
10400               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10401           Diagnosed = true;
10402           break;
10403         }
10404 
10405         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10406         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10407         // class needs to be checked instead.
10408         const auto FirstStorage = FirstMethod->getStorageClass();
10409         const auto SecondStorage = SecondMethod->getStorageClass();
10410         const bool FirstStatic = FirstStorage == SC_Static;
10411         const bool SecondStatic = SecondStorage == SC_Static;
10412         if (FirstStatic != SecondStatic) {
10413           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10414                            FirstMethod->getSourceRange(), MethodStatic)
10415               << FirstMethodType << FirstName << FirstStatic;
10416           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10417                           SecondMethod->getSourceRange(), MethodStatic)
10418               << SecondMethodType << SecondName << SecondStatic;
10419           Diagnosed = true;
10420           break;
10421         }
10422 
10423         const bool FirstVolatile = FirstMethod->isVolatile();
10424         const bool SecondVolatile = SecondMethod->isVolatile();
10425         if (FirstVolatile != SecondVolatile) {
10426           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10427                            FirstMethod->getSourceRange(), MethodVolatile)
10428               << FirstMethodType << FirstName << FirstVolatile;
10429           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10430                           SecondMethod->getSourceRange(), MethodVolatile)
10431               << SecondMethodType << SecondName << SecondVolatile;
10432           Diagnosed = true;
10433           break;
10434         }
10435 
10436         const bool FirstConst = FirstMethod->isConst();
10437         const bool SecondConst = SecondMethod->isConst();
10438         if (FirstConst != SecondConst) {
10439           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10440                            FirstMethod->getSourceRange(), MethodConst)
10441               << FirstMethodType << FirstName << FirstConst;
10442           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10443                           SecondMethod->getSourceRange(), MethodConst)
10444               << SecondMethodType << SecondName << SecondConst;
10445           Diagnosed = true;
10446           break;
10447         }
10448 
10449         const bool FirstInline = FirstMethod->isInlineSpecified();
10450         const bool SecondInline = SecondMethod->isInlineSpecified();
10451         if (FirstInline != SecondInline) {
10452           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10453                            FirstMethod->getSourceRange(), MethodInline)
10454               << FirstMethodType << FirstName << FirstInline;
10455           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10456                           SecondMethod->getSourceRange(), MethodInline)
10457               << SecondMethodType << SecondName << SecondInline;
10458           Diagnosed = true;
10459           break;
10460         }
10461 
10462         const unsigned FirstNumParameters = FirstMethod->param_size();
10463         const unsigned SecondNumParameters = SecondMethod->param_size();
10464         if (FirstNumParameters != SecondNumParameters) {
10465           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10466                            FirstMethod->getSourceRange(),
10467                            MethodNumberParameters)
10468               << FirstMethodType << FirstName << FirstNumParameters;
10469           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10470                           SecondMethod->getSourceRange(),
10471                           MethodNumberParameters)
10472               << SecondMethodType << SecondName << SecondNumParameters;
10473           Diagnosed = true;
10474           break;
10475         }
10476 
10477         // Need this status boolean to know when break out of the switch.
10478         bool ParameterMismatch = false;
10479         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10480           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10481           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10482 
10483           QualType FirstParamType = FirstParam->getType();
10484           QualType SecondParamType = SecondParam->getType();
10485           if (FirstParamType != SecondParamType &&
10486               ComputeQualTypeODRHash(FirstParamType) !=
10487                   ComputeQualTypeODRHash(SecondParamType)) {
10488             if (const DecayedType *ParamDecayedType =
10489                     FirstParamType->getAs<DecayedType>()) {
10490               ODRDiagDeclError(
10491                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10492                   FirstMethod->getSourceRange(), MethodParameterType)
10493                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10494                   << true << ParamDecayedType->getOriginalType();
10495             } else {
10496               ODRDiagDeclError(
10497                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10498                   FirstMethod->getSourceRange(), MethodParameterType)
10499                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10500                   << false;
10501             }
10502 
10503             if (const DecayedType *ParamDecayedType =
10504                     SecondParamType->getAs<DecayedType>()) {
10505               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10506                               SecondMethod->getSourceRange(),
10507                               MethodParameterType)
10508                   << SecondMethodType << SecondName << (I + 1)
10509                   << SecondParamType << true
10510                   << ParamDecayedType->getOriginalType();
10511             } else {
10512               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10513                               SecondMethod->getSourceRange(),
10514                               MethodParameterType)
10515                   << SecondMethodType << SecondName << (I + 1)
10516                   << SecondParamType << false;
10517             }
10518             ParameterMismatch = true;
10519             break;
10520           }
10521 
10522           DeclarationName FirstParamName = FirstParam->getDeclName();
10523           DeclarationName SecondParamName = SecondParam->getDeclName();
10524           if (FirstParamName != SecondParamName) {
10525             ODRDiagDeclError(FirstRecord, FirstModule,
10526                              FirstMethod->getLocation(),
10527                              FirstMethod->getSourceRange(), MethodParameterName)
10528                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10529             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10530                             SecondMethod->getSourceRange(), MethodParameterName)
10531                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10532             ParameterMismatch = true;
10533             break;
10534           }
10535 
10536           const Expr *FirstInit = FirstParam->getInit();
10537           const Expr *SecondInit = SecondParam->getInit();
10538           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10539             ODRDiagDeclError(FirstRecord, FirstModule,
10540                              FirstMethod->getLocation(),
10541                              FirstMethod->getSourceRange(),
10542                              MethodParameterSingleDefaultArgument)
10543                 << FirstMethodType << FirstName << (I + 1)
10544                 << (FirstInit == nullptr)
10545                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10546             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10547                             SecondMethod->getSourceRange(),
10548                             MethodParameterSingleDefaultArgument)
10549                 << SecondMethodType << SecondName << (I + 1)
10550                 << (SecondInit == nullptr)
10551                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10552             ParameterMismatch = true;
10553             break;
10554           }
10555 
10556           if (FirstInit && SecondInit &&
10557               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10558             ODRDiagDeclError(FirstRecord, FirstModule,
10559                              FirstMethod->getLocation(),
10560                              FirstMethod->getSourceRange(),
10561                              MethodParameterDifferentDefaultArgument)
10562                 << FirstMethodType << FirstName << (I + 1)
10563                 << FirstInit->getSourceRange();
10564             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10565                             SecondMethod->getSourceRange(),
10566                             MethodParameterDifferentDefaultArgument)
10567                 << SecondMethodType << SecondName << (I + 1)
10568                 << SecondInit->getSourceRange();
10569             ParameterMismatch = true;
10570             break;
10571 
10572           }
10573         }
10574 
10575         if (ParameterMismatch) {
10576           Diagnosed = true;
10577           break;
10578         }
10579 
10580         const auto *FirstTemplateArgs =
10581             FirstMethod->getTemplateSpecializationArgs();
10582         const auto *SecondTemplateArgs =
10583             SecondMethod->getTemplateSpecializationArgs();
10584 
10585         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10586             (!FirstTemplateArgs && SecondTemplateArgs)) {
10587           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10588                            FirstMethod->getSourceRange(),
10589                            MethodNoTemplateArguments)
10590               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10591           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10592                           SecondMethod->getSourceRange(),
10593                           MethodNoTemplateArguments)
10594               << SecondMethodType << SecondName
10595               << (SecondTemplateArgs != nullptr);
10596 
10597           Diagnosed = true;
10598           break;
10599         }
10600 
10601         if (FirstTemplateArgs && SecondTemplateArgs) {
10602           // Remove pack expansions from argument list.
10603           auto ExpandTemplateArgumentList =
10604               [](const TemplateArgumentList *TAL) {
10605                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10606                 for (const TemplateArgument &TA : TAL->asArray()) {
10607                   if (TA.getKind() != TemplateArgument::Pack) {
10608                     ExpandedList.push_back(&TA);
10609                     continue;
10610                   }
10611                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10612                     ExpandedList.push_back(&PackTA);
10613                   }
10614                 }
10615                 return ExpandedList;
10616               };
10617           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10618               ExpandTemplateArgumentList(FirstTemplateArgs);
10619           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10620               ExpandTemplateArgumentList(SecondTemplateArgs);
10621 
10622           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10623             ODRDiagDeclError(FirstRecord, FirstModule,
10624                              FirstMethod->getLocation(),
10625                              FirstMethod->getSourceRange(),
10626                              MethodDifferentNumberTemplateArguments)
10627                 << FirstMethodType << FirstName
10628                 << (unsigned)FirstExpandedList.size();
10629             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10630                             SecondMethod->getSourceRange(),
10631                             MethodDifferentNumberTemplateArguments)
10632                 << SecondMethodType << SecondName
10633                 << (unsigned)SecondExpandedList.size();
10634 
10635             Diagnosed = true;
10636             break;
10637           }
10638 
10639           bool TemplateArgumentMismatch = false;
10640           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10641             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10642                                    &SecondTA = *SecondExpandedList[i];
10643             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10644                 ComputeTemplateArgumentODRHash(SecondTA)) {
10645               continue;
10646             }
10647 
10648             ODRDiagDeclError(
10649                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10650                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10651                 << FirstMethodType << FirstName << FirstTA << i + 1;
10652             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10653                             SecondMethod->getSourceRange(),
10654                             MethodDifferentTemplateArgument)
10655                 << SecondMethodType << SecondName << SecondTA << i + 1;
10656 
10657             TemplateArgumentMismatch = true;
10658             break;
10659           }
10660 
10661           if (TemplateArgumentMismatch) {
10662             Diagnosed = true;
10663             break;
10664           }
10665         }
10666 
10667         // Compute the hash of the method as if it has no body.
10668         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10669           Hash.clear();
10670           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10671           return Hash.CalculateHash();
10672         };
10673 
10674         // Compare the hash generated to the hash stored.  A difference means
10675         // that a body was present in the original source.  Due to merging,
10676         // the stardard way of detecting a body will not work.
10677         const bool HasFirstBody =
10678             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10679         const bool HasSecondBody =
10680             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10681 
10682         if (HasFirstBody != HasSecondBody) {
10683           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10684                            FirstMethod->getSourceRange(), MethodSingleBody)
10685               << FirstMethodType << FirstName << HasFirstBody;
10686           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10687                           SecondMethod->getSourceRange(), MethodSingleBody)
10688               << SecondMethodType << SecondName << HasSecondBody;
10689           Diagnosed = true;
10690           break;
10691         }
10692 
10693         if (HasFirstBody && HasSecondBody) {
10694           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10695                            FirstMethod->getSourceRange(), MethodDifferentBody)
10696               << FirstMethodType << FirstName;
10697           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10698                           SecondMethod->getSourceRange(), MethodDifferentBody)
10699               << SecondMethodType << SecondName;
10700           Diagnosed = true;
10701           break;
10702         }
10703 
10704         break;
10705       }
10706       case TypeAlias:
10707       case TypeDef: {
10708         Diagnosed = ODRDiagTypeDefOrAlias(
10709             FirstRecord, FirstModule, SecondModule,
10710             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10711             FirstDiffType == TypeAlias);
10712         break;
10713       }
10714       case Var: {
10715         Diagnosed =
10716             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10717                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10718         break;
10719       }
10720       case Friend: {
10721         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10722         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10723 
10724         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10725         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10726 
10727         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10728         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10729 
10730         if (FirstND && SecondND) {
10731           ODRDiagDeclError(FirstRecord, FirstModule,
10732                            FirstFriend->getFriendLoc(),
10733                            FirstFriend->getSourceRange(), FriendFunction)
10734               << FirstND;
10735           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10736                           SecondFriend->getSourceRange(), FriendFunction)
10737               << SecondND;
10738 
10739           Diagnosed = true;
10740           break;
10741         }
10742 
10743         if (FirstTSI && SecondTSI) {
10744           QualType FirstFriendType = FirstTSI->getType();
10745           QualType SecondFriendType = SecondTSI->getType();
10746           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10747                  ComputeQualTypeODRHash(SecondFriendType));
10748           ODRDiagDeclError(FirstRecord, FirstModule,
10749                            FirstFriend->getFriendLoc(),
10750                            FirstFriend->getSourceRange(), FriendType)
10751               << FirstFriendType;
10752           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10753                           SecondFriend->getSourceRange(), FriendType)
10754               << SecondFriendType;
10755           Diagnosed = true;
10756           break;
10757         }
10758 
10759         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10760                          FirstFriend->getSourceRange(), FriendTypeFunction)
10761             << (FirstTSI == nullptr);
10762         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10763                         SecondFriend->getSourceRange(), FriendTypeFunction)
10764             << (SecondTSI == nullptr);
10765 
10766         Diagnosed = true;
10767         break;
10768       }
10769       case FunctionTemplate: {
10770         FunctionTemplateDecl *FirstTemplate =
10771             cast<FunctionTemplateDecl>(FirstDecl);
10772         FunctionTemplateDecl *SecondTemplate =
10773             cast<FunctionTemplateDecl>(SecondDecl);
10774 
10775         TemplateParameterList *FirstTPL =
10776             FirstTemplate->getTemplateParameters();
10777         TemplateParameterList *SecondTPL =
10778             SecondTemplate->getTemplateParameters();
10779 
10780         if (FirstTPL->size() != SecondTPL->size()) {
10781           ODRDiagDeclError(FirstRecord, FirstModule,
10782                            FirstTemplate->getLocation(),
10783                            FirstTemplate->getSourceRange(),
10784                            FunctionTemplateDifferentNumberParameters)
10785               << FirstTemplate << FirstTPL->size();
10786           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10787                           SecondTemplate->getSourceRange(),
10788                           FunctionTemplateDifferentNumberParameters)
10789               << SecondTemplate << SecondTPL->size();
10790 
10791           Diagnosed = true;
10792           break;
10793         }
10794 
10795         bool ParameterMismatch = false;
10796         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10797           NamedDecl *FirstParam = FirstTPL->getParam(i);
10798           NamedDecl *SecondParam = SecondTPL->getParam(i);
10799 
10800           if (FirstParam->getKind() != SecondParam->getKind()) {
10801             enum {
10802               TemplateTypeParameter,
10803               NonTypeTemplateParameter,
10804               TemplateTemplateParameter,
10805             };
10806             auto GetParamType = [](NamedDecl *D) {
10807               switch (D->getKind()) {
10808                 default:
10809                   llvm_unreachable("Unexpected template parameter type");
10810                 case Decl::TemplateTypeParm:
10811                   return TemplateTypeParameter;
10812                 case Decl::NonTypeTemplateParm:
10813                   return NonTypeTemplateParameter;
10814                 case Decl::TemplateTemplateParm:
10815                   return TemplateTemplateParameter;
10816               }
10817             };
10818 
10819             ODRDiagDeclError(FirstRecord, FirstModule,
10820                              FirstTemplate->getLocation(),
10821                              FirstTemplate->getSourceRange(),
10822                              FunctionTemplateParameterDifferentKind)
10823                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10824             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10825                             SecondTemplate->getSourceRange(),
10826                             FunctionTemplateParameterDifferentKind)
10827                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10828 
10829             ParameterMismatch = true;
10830             break;
10831           }
10832 
10833           if (FirstParam->getName() != SecondParam->getName()) {
10834             ODRDiagDeclError(
10835                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10836                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10837                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10838                 << FirstParam;
10839             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10840                             SecondTemplate->getSourceRange(),
10841                             FunctionTemplateParameterName)
10842                 << SecondTemplate << (i + 1)
10843                 << (bool)SecondParam->getIdentifier() << SecondParam;
10844             ParameterMismatch = true;
10845             break;
10846           }
10847 
10848           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10849               isa<TemplateTypeParmDecl>(SecondParam)) {
10850             TemplateTypeParmDecl *FirstTTPD =
10851                 cast<TemplateTypeParmDecl>(FirstParam);
10852             TemplateTypeParmDecl *SecondTTPD =
10853                 cast<TemplateTypeParmDecl>(SecondParam);
10854             bool HasFirstDefaultArgument =
10855                 FirstTTPD->hasDefaultArgument() &&
10856                 !FirstTTPD->defaultArgumentWasInherited();
10857             bool HasSecondDefaultArgument =
10858                 SecondTTPD->hasDefaultArgument() &&
10859                 !SecondTTPD->defaultArgumentWasInherited();
10860             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10861               ODRDiagDeclError(FirstRecord, FirstModule,
10862                                FirstTemplate->getLocation(),
10863                                FirstTemplate->getSourceRange(),
10864                                FunctionTemplateParameterSingleDefaultArgument)
10865                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10866               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10867                               SecondTemplate->getSourceRange(),
10868                               FunctionTemplateParameterSingleDefaultArgument)
10869                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10870               ParameterMismatch = true;
10871               break;
10872             }
10873 
10874             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10875               QualType FirstType = FirstTTPD->getDefaultArgument();
10876               QualType SecondType = SecondTTPD->getDefaultArgument();
10877               if (ComputeQualTypeODRHash(FirstType) !=
10878                   ComputeQualTypeODRHash(SecondType)) {
10879                 ODRDiagDeclError(
10880                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10881                     FirstTemplate->getSourceRange(),
10882                     FunctionTemplateParameterDifferentDefaultArgument)
10883                     << FirstTemplate << (i + 1) << FirstType;
10884                 ODRDiagDeclNote(
10885                     SecondModule, SecondTemplate->getLocation(),
10886                     SecondTemplate->getSourceRange(),
10887                     FunctionTemplateParameterDifferentDefaultArgument)
10888                     << SecondTemplate << (i + 1) << SecondType;
10889                 ParameterMismatch = true;
10890                 break;
10891               }
10892             }
10893 
10894             if (FirstTTPD->isParameterPack() !=
10895                 SecondTTPD->isParameterPack()) {
10896               ODRDiagDeclError(FirstRecord, FirstModule,
10897                                FirstTemplate->getLocation(),
10898                                FirstTemplate->getSourceRange(),
10899                                FunctionTemplatePackParameter)
10900                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10901               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10902                               SecondTemplate->getSourceRange(),
10903                               FunctionTemplatePackParameter)
10904                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10905               ParameterMismatch = true;
10906               break;
10907             }
10908           }
10909 
10910           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10911               isa<TemplateTemplateParmDecl>(SecondParam)) {
10912             TemplateTemplateParmDecl *FirstTTPD =
10913                 cast<TemplateTemplateParmDecl>(FirstParam);
10914             TemplateTemplateParmDecl *SecondTTPD =
10915                 cast<TemplateTemplateParmDecl>(SecondParam);
10916 
10917             TemplateParameterList *FirstTPL =
10918                 FirstTTPD->getTemplateParameters();
10919             TemplateParameterList *SecondTPL =
10920                 SecondTTPD->getTemplateParameters();
10921 
10922             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
10923                 ComputeTemplateParameterListODRHash(SecondTPL)) {
10924               ODRDiagDeclError(FirstRecord, FirstModule,
10925                                FirstTemplate->getLocation(),
10926                                FirstTemplate->getSourceRange(),
10927                                FunctionTemplateParameterDifferentType)
10928                   << FirstTemplate << (i + 1);
10929               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10930                               SecondTemplate->getSourceRange(),
10931                               FunctionTemplateParameterDifferentType)
10932                   << SecondTemplate << (i + 1);
10933               ParameterMismatch = true;
10934               break;
10935             }
10936 
10937             bool HasFirstDefaultArgument =
10938                 FirstTTPD->hasDefaultArgument() &&
10939                 !FirstTTPD->defaultArgumentWasInherited();
10940             bool HasSecondDefaultArgument =
10941                 SecondTTPD->hasDefaultArgument() &&
10942                 !SecondTTPD->defaultArgumentWasInherited();
10943             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10944               ODRDiagDeclError(FirstRecord, FirstModule,
10945                                FirstTemplate->getLocation(),
10946                                FirstTemplate->getSourceRange(),
10947                                FunctionTemplateParameterSingleDefaultArgument)
10948                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10949               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10950                               SecondTemplate->getSourceRange(),
10951                               FunctionTemplateParameterSingleDefaultArgument)
10952                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10953               ParameterMismatch = true;
10954               break;
10955             }
10956 
10957             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10958               TemplateArgument FirstTA =
10959                   FirstTTPD->getDefaultArgument().getArgument();
10960               TemplateArgument SecondTA =
10961                   SecondTTPD->getDefaultArgument().getArgument();
10962               if (ComputeTemplateArgumentODRHash(FirstTA) !=
10963                   ComputeTemplateArgumentODRHash(SecondTA)) {
10964                 ODRDiagDeclError(
10965                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10966                     FirstTemplate->getSourceRange(),
10967                     FunctionTemplateParameterDifferentDefaultArgument)
10968                     << FirstTemplate << (i + 1) << FirstTA;
10969                 ODRDiagDeclNote(
10970                     SecondModule, SecondTemplate->getLocation(),
10971                     SecondTemplate->getSourceRange(),
10972                     FunctionTemplateParameterDifferentDefaultArgument)
10973                     << SecondTemplate << (i + 1) << SecondTA;
10974                 ParameterMismatch = true;
10975                 break;
10976               }
10977             }
10978 
10979             if (FirstTTPD->isParameterPack() !=
10980                 SecondTTPD->isParameterPack()) {
10981               ODRDiagDeclError(FirstRecord, FirstModule,
10982                                FirstTemplate->getLocation(),
10983                                FirstTemplate->getSourceRange(),
10984                                FunctionTemplatePackParameter)
10985                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10986               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10987                               SecondTemplate->getSourceRange(),
10988                               FunctionTemplatePackParameter)
10989                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10990               ParameterMismatch = true;
10991               break;
10992             }
10993           }
10994 
10995           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
10996               isa<NonTypeTemplateParmDecl>(SecondParam)) {
10997             NonTypeTemplateParmDecl *FirstNTTPD =
10998                 cast<NonTypeTemplateParmDecl>(FirstParam);
10999             NonTypeTemplateParmDecl *SecondNTTPD =
11000                 cast<NonTypeTemplateParmDecl>(SecondParam);
11001 
11002             QualType FirstType = FirstNTTPD->getType();
11003             QualType SecondType = SecondNTTPD->getType();
11004             if (ComputeQualTypeODRHash(FirstType) !=
11005                 ComputeQualTypeODRHash(SecondType)) {
11006               ODRDiagDeclError(FirstRecord, FirstModule,
11007                                FirstTemplate->getLocation(),
11008                                FirstTemplate->getSourceRange(),
11009                                FunctionTemplateParameterDifferentType)
11010                   << FirstTemplate << (i + 1);
11011               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11012                               SecondTemplate->getSourceRange(),
11013                               FunctionTemplateParameterDifferentType)
11014                   << SecondTemplate << (i + 1);
11015               ParameterMismatch = true;
11016               break;
11017             }
11018 
11019             bool HasFirstDefaultArgument =
11020                 FirstNTTPD->hasDefaultArgument() &&
11021                 !FirstNTTPD->defaultArgumentWasInherited();
11022             bool HasSecondDefaultArgument =
11023                 SecondNTTPD->hasDefaultArgument() &&
11024                 !SecondNTTPD->defaultArgumentWasInherited();
11025             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11026               ODRDiagDeclError(FirstRecord, FirstModule,
11027                                FirstTemplate->getLocation(),
11028                                FirstTemplate->getSourceRange(),
11029                                FunctionTemplateParameterSingleDefaultArgument)
11030                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11031               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11032                               SecondTemplate->getSourceRange(),
11033                               FunctionTemplateParameterSingleDefaultArgument)
11034                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11035               ParameterMismatch = true;
11036               break;
11037             }
11038 
11039             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11040               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11041               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11042               if (ComputeODRHash(FirstDefaultArgument) !=
11043                   ComputeODRHash(SecondDefaultArgument)) {
11044                 ODRDiagDeclError(
11045                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11046                     FirstTemplate->getSourceRange(),
11047                     FunctionTemplateParameterDifferentDefaultArgument)
11048                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11049                 ODRDiagDeclNote(
11050                     SecondModule, SecondTemplate->getLocation(),
11051                     SecondTemplate->getSourceRange(),
11052                     FunctionTemplateParameterDifferentDefaultArgument)
11053                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11054                 ParameterMismatch = true;
11055                 break;
11056               }
11057             }
11058 
11059             if (FirstNTTPD->isParameterPack() !=
11060                 SecondNTTPD->isParameterPack()) {
11061               ODRDiagDeclError(FirstRecord, FirstModule,
11062                                FirstTemplate->getLocation(),
11063                                FirstTemplate->getSourceRange(),
11064                                FunctionTemplatePackParameter)
11065                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11066               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11067                               SecondTemplate->getSourceRange(),
11068                               FunctionTemplatePackParameter)
11069                   << SecondTemplate << (i + 1)
11070                   << SecondNTTPD->isParameterPack();
11071               ParameterMismatch = true;
11072               break;
11073             }
11074           }
11075         }
11076 
11077         if (ParameterMismatch) {
11078           Diagnosed = true;
11079           break;
11080         }
11081 
11082         break;
11083       }
11084       }
11085 
11086       if (Diagnosed)
11087         continue;
11088 
11089       Diag(FirstDecl->getLocation(),
11090            diag::err_module_odr_violation_mismatch_decl_unknown)
11091           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11092           << FirstDecl->getSourceRange();
11093       Diag(SecondDecl->getLocation(),
11094            diag::note_module_odr_violation_mismatch_decl_unknown)
11095           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11096       Diagnosed = true;
11097     }
11098 
11099     if (!Diagnosed) {
11100       // All definitions are updates to the same declaration. This happens if a
11101       // module instantiates the declaration of a class template specialization
11102       // and two or more other modules instantiate its definition.
11103       //
11104       // FIXME: Indicate which modules had instantiations of this definition.
11105       // FIXME: How can this even happen?
11106       Diag(Merge.first->getLocation(),
11107            diag::err_module_odr_violation_different_instantiations)
11108         << Merge.first;
11109     }
11110   }
11111 
11112   // Issue ODR failures diagnostics for functions.
11113   for (auto &Merge : FunctionOdrMergeFailures) {
11114     enum ODRFunctionDifference {
11115       ReturnType,
11116       ParameterName,
11117       ParameterType,
11118       ParameterSingleDefaultArgument,
11119       ParameterDifferentDefaultArgument,
11120       FunctionBody,
11121     };
11122 
11123     FunctionDecl *FirstFunction = Merge.first;
11124     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11125 
11126     bool Diagnosed = false;
11127     for (auto &SecondFunction : Merge.second) {
11128 
11129       if (FirstFunction == SecondFunction)
11130         continue;
11131 
11132       std::string SecondModule =
11133           getOwningModuleNameForDiagnostic(SecondFunction);
11134 
11135       auto ODRDiagError = [FirstFunction, &FirstModule,
11136                            this](SourceLocation Loc, SourceRange Range,
11137                                  ODRFunctionDifference DiffType) {
11138         return Diag(Loc, diag::err_module_odr_violation_function)
11139                << FirstFunction << FirstModule.empty() << FirstModule << Range
11140                << DiffType;
11141       };
11142       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11143                                                SourceRange Range,
11144                                                ODRFunctionDifference DiffType) {
11145         return Diag(Loc, diag::note_module_odr_violation_function)
11146                << SecondModule << Range << DiffType;
11147       };
11148 
11149       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11150           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11151         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11152                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11153             << FirstFunction->getReturnType();
11154         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11155                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11156             << SecondFunction->getReturnType();
11157         Diagnosed = true;
11158         break;
11159       }
11160 
11161       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11162              "Merged functions with different number of parameters");
11163 
11164       auto ParamSize = FirstFunction->param_size();
11165       bool ParameterMismatch = false;
11166       for (unsigned I = 0; I < ParamSize; ++I) {
11167         auto *FirstParam = FirstFunction->getParamDecl(I);
11168         auto *SecondParam = SecondFunction->getParamDecl(I);
11169 
11170         assert(getContext().hasSameType(FirstParam->getType(),
11171                                       SecondParam->getType()) &&
11172                "Merged function has different parameter types.");
11173 
11174         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11175           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11176                        ParameterName)
11177               << I + 1 << FirstParam->getDeclName();
11178           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11179                       ParameterName)
11180               << I + 1 << SecondParam->getDeclName();
11181           ParameterMismatch = true;
11182           break;
11183         };
11184 
11185         QualType FirstParamType = FirstParam->getType();
11186         QualType SecondParamType = SecondParam->getType();
11187         if (FirstParamType != SecondParamType &&
11188             ComputeQualTypeODRHash(FirstParamType) !=
11189                 ComputeQualTypeODRHash(SecondParamType)) {
11190           if (const DecayedType *ParamDecayedType =
11191                   FirstParamType->getAs<DecayedType>()) {
11192             ODRDiagError(FirstParam->getLocation(),
11193                          FirstParam->getSourceRange(), ParameterType)
11194                 << (I + 1) << FirstParamType << true
11195                 << ParamDecayedType->getOriginalType();
11196           } else {
11197             ODRDiagError(FirstParam->getLocation(),
11198                          FirstParam->getSourceRange(), ParameterType)
11199                 << (I + 1) << FirstParamType << false;
11200           }
11201 
11202           if (const DecayedType *ParamDecayedType =
11203                   SecondParamType->getAs<DecayedType>()) {
11204             ODRDiagNote(SecondParam->getLocation(),
11205                         SecondParam->getSourceRange(), ParameterType)
11206                 << (I + 1) << SecondParamType << true
11207                 << ParamDecayedType->getOriginalType();
11208           } else {
11209             ODRDiagNote(SecondParam->getLocation(),
11210                         SecondParam->getSourceRange(), ParameterType)
11211                 << (I + 1) << SecondParamType << false;
11212           }
11213           ParameterMismatch = true;
11214           break;
11215         }
11216 
11217         const Expr *FirstInit = FirstParam->getInit();
11218         const Expr *SecondInit = SecondParam->getInit();
11219         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11220           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11221                        ParameterSingleDefaultArgument)
11222               << (I + 1) << (FirstInit == nullptr)
11223               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11224           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11225                       ParameterSingleDefaultArgument)
11226               << (I + 1) << (SecondInit == nullptr)
11227               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11228           ParameterMismatch = true;
11229           break;
11230         }
11231 
11232         if (FirstInit && SecondInit &&
11233             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11234           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11235                        ParameterDifferentDefaultArgument)
11236               << (I + 1) << FirstInit->getSourceRange();
11237           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11238                       ParameterDifferentDefaultArgument)
11239               << (I + 1) << SecondInit->getSourceRange();
11240           ParameterMismatch = true;
11241           break;
11242         }
11243 
11244         assert(ComputeSubDeclODRHash(FirstParam) ==
11245                    ComputeSubDeclODRHash(SecondParam) &&
11246                "Undiagnosed parameter difference.");
11247       }
11248 
11249       if (ParameterMismatch) {
11250         Diagnosed = true;
11251         break;
11252       }
11253 
11254       // If no error has been generated before now, assume the problem is in
11255       // the body and generate a message.
11256       ODRDiagError(FirstFunction->getLocation(),
11257                    FirstFunction->getSourceRange(), FunctionBody);
11258       ODRDiagNote(SecondFunction->getLocation(),
11259                   SecondFunction->getSourceRange(), FunctionBody);
11260       Diagnosed = true;
11261       break;
11262     }
11263     (void)Diagnosed;
11264     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11265   }
11266 
11267   // Issue ODR failures diagnostics for enums.
11268   for (auto &Merge : EnumOdrMergeFailures) {
11269     enum ODREnumDifference {
11270       SingleScopedEnum,
11271       EnumTagKeywordMismatch,
11272       SingleSpecifiedType,
11273       DifferentSpecifiedTypes,
11274       DifferentNumberEnumConstants,
11275       EnumConstantName,
11276       EnumConstantSingleInitilizer,
11277       EnumConstantDifferentInitilizer,
11278     };
11279 
11280     // If we've already pointed out a specific problem with this enum, don't
11281     // bother issuing a general "something's different" diagnostic.
11282     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11283       continue;
11284 
11285     EnumDecl *FirstEnum = Merge.first;
11286     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11287 
11288     using DeclHashes =
11289         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11290     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11291                               DeclHashes &Hashes, EnumDecl *Enum) {
11292       for (auto *D : Enum->decls()) {
11293         // Due to decl merging, the first EnumDecl is the parent of
11294         // Decls in both records.
11295         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11296           continue;
11297         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11298         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11299                             ComputeSubDeclODRHash(D));
11300       }
11301     };
11302     DeclHashes FirstHashes;
11303     PopulateHashes(FirstHashes, FirstEnum);
11304     bool Diagnosed = false;
11305     for (auto &SecondEnum : Merge.second) {
11306 
11307       if (FirstEnum == SecondEnum)
11308         continue;
11309 
11310       std::string SecondModule =
11311           getOwningModuleNameForDiagnostic(SecondEnum);
11312 
11313       auto ODRDiagError = [FirstEnum, &FirstModule,
11314                            this](SourceLocation Loc, SourceRange Range,
11315                                  ODREnumDifference DiffType) {
11316         return Diag(Loc, diag::err_module_odr_violation_enum)
11317                << FirstEnum << FirstModule.empty() << FirstModule << Range
11318                << DiffType;
11319       };
11320       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11321                                                SourceRange Range,
11322                                                ODREnumDifference DiffType) {
11323         return Diag(Loc, diag::note_module_odr_violation_enum)
11324                << SecondModule << Range << DiffType;
11325       };
11326 
11327       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11328         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11329                      SingleScopedEnum)
11330             << FirstEnum->isScoped();
11331         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11332                     SingleScopedEnum)
11333             << SecondEnum->isScoped();
11334         Diagnosed = true;
11335         continue;
11336       }
11337 
11338       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11339         if (FirstEnum->isScopedUsingClassTag() !=
11340             SecondEnum->isScopedUsingClassTag()) {
11341           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11342                        EnumTagKeywordMismatch)
11343               << FirstEnum->isScopedUsingClassTag();
11344           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11345                       EnumTagKeywordMismatch)
11346               << SecondEnum->isScopedUsingClassTag();
11347           Diagnosed = true;
11348           continue;
11349         }
11350       }
11351 
11352       QualType FirstUnderlyingType =
11353           FirstEnum->getIntegerTypeSourceInfo()
11354               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11355               : QualType();
11356       QualType SecondUnderlyingType =
11357           SecondEnum->getIntegerTypeSourceInfo()
11358               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11359               : QualType();
11360       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11361           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11362                        SingleSpecifiedType)
11363               << !FirstUnderlyingType.isNull();
11364           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11365                       SingleSpecifiedType)
11366               << !SecondUnderlyingType.isNull();
11367           Diagnosed = true;
11368           continue;
11369       }
11370 
11371       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11372         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11373             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11374           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11375                        DifferentSpecifiedTypes)
11376               << FirstUnderlyingType;
11377           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11378                       DifferentSpecifiedTypes)
11379               << SecondUnderlyingType;
11380           Diagnosed = true;
11381           continue;
11382         }
11383       }
11384 
11385       DeclHashes SecondHashes;
11386       PopulateHashes(SecondHashes, SecondEnum);
11387 
11388       if (FirstHashes.size() != SecondHashes.size()) {
11389         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11390                      DifferentNumberEnumConstants)
11391             << (int)FirstHashes.size();
11392         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11393                     DifferentNumberEnumConstants)
11394             << (int)SecondHashes.size();
11395         Diagnosed = true;
11396         continue;
11397       }
11398 
11399       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11400         if (FirstHashes[I].second == SecondHashes[I].second)
11401           continue;
11402         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11403         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11404 
11405         if (FirstEnumConstant->getDeclName() !=
11406             SecondEnumConstant->getDeclName()) {
11407 
11408           ODRDiagError(FirstEnumConstant->getLocation(),
11409                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11410               << I + 1 << FirstEnumConstant;
11411           ODRDiagNote(SecondEnumConstant->getLocation(),
11412                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11413               << I + 1 << SecondEnumConstant;
11414           Diagnosed = true;
11415           break;
11416         }
11417 
11418         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11419         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11420         if (!FirstInit && !SecondInit)
11421           continue;
11422 
11423         if (!FirstInit || !SecondInit) {
11424           ODRDiagError(FirstEnumConstant->getLocation(),
11425                        FirstEnumConstant->getSourceRange(),
11426                        EnumConstantSingleInitilizer)
11427               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11428           ODRDiagNote(SecondEnumConstant->getLocation(),
11429                       SecondEnumConstant->getSourceRange(),
11430                       EnumConstantSingleInitilizer)
11431               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11432           Diagnosed = true;
11433           break;
11434         }
11435 
11436         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11437           ODRDiagError(FirstEnumConstant->getLocation(),
11438                        FirstEnumConstant->getSourceRange(),
11439                        EnumConstantDifferentInitilizer)
11440               << I + 1 << FirstEnumConstant;
11441           ODRDiagNote(SecondEnumConstant->getLocation(),
11442                       SecondEnumConstant->getSourceRange(),
11443                       EnumConstantDifferentInitilizer)
11444               << I + 1 << SecondEnumConstant;
11445           Diagnosed = true;
11446           break;
11447         }
11448       }
11449     }
11450 
11451     (void)Diagnosed;
11452     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11453   }
11454 }
11455 
11456 void ASTReader::StartedDeserializing() {
11457   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11458     ReadTimer->startTimer();
11459 }
11460 
11461 void ASTReader::FinishedDeserializing() {
11462   assert(NumCurrentElementsDeserializing &&
11463          "FinishedDeserializing not paired with StartedDeserializing");
11464   if (NumCurrentElementsDeserializing == 1) {
11465     // We decrease NumCurrentElementsDeserializing only after pending actions
11466     // are finished, to avoid recursively re-calling finishPendingActions().
11467     finishPendingActions();
11468   }
11469   --NumCurrentElementsDeserializing;
11470 
11471   if (NumCurrentElementsDeserializing == 0) {
11472     // Propagate exception specification and deduced type updates along
11473     // redeclaration chains.
11474     //
11475     // We do this now rather than in finishPendingActions because we want to
11476     // be able to walk the complete redeclaration chains of the updated decls.
11477     while (!PendingExceptionSpecUpdates.empty() ||
11478            !PendingDeducedTypeUpdates.empty()) {
11479       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11480       PendingExceptionSpecUpdates.clear();
11481       for (auto Update : ESUpdates) {
11482         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11483         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11484         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11485         if (auto *Listener = getContext().getASTMutationListener())
11486           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11487         for (auto *Redecl : Update.second->redecls())
11488           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11489       }
11490 
11491       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11492       PendingDeducedTypeUpdates.clear();
11493       for (auto Update : DTUpdates) {
11494         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11495         // FIXME: If the return type is already deduced, check that it matches.
11496         getContext().adjustDeducedFunctionResultType(Update.first,
11497                                                      Update.second);
11498       }
11499     }
11500 
11501     if (ReadTimer)
11502       ReadTimer->stopTimer();
11503 
11504     diagnoseOdrViolations();
11505 
11506     // We are not in recursive loading, so it's safe to pass the "interesting"
11507     // decls to the consumer.
11508     if (Consumer)
11509       PassInterestingDeclsToConsumer();
11510   }
11511 }
11512 
11513 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11514   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11515     // Remove any fake results before adding any real ones.
11516     auto It = PendingFakeLookupResults.find(II);
11517     if (It != PendingFakeLookupResults.end()) {
11518       for (auto *ND : It->second)
11519         SemaObj->IdResolver.RemoveDecl(ND);
11520       // FIXME: this works around module+PCH performance issue.
11521       // Rather than erase the result from the map, which is O(n), just clear
11522       // the vector of NamedDecls.
11523       It->second.clear();
11524     }
11525   }
11526 
11527   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11528     SemaObj->TUScope->AddDecl(D);
11529   } else if (SemaObj->TUScope) {
11530     // Adding the decl to IdResolver may have failed because it was already in
11531     // (even though it was not added in scope). If it is already in, make sure
11532     // it gets in the scope as well.
11533     if (std::find(SemaObj->IdResolver.begin(Name),
11534                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11535       SemaObj->TUScope->AddDecl(D);
11536   }
11537 }
11538 
11539 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11540                      ASTContext *Context,
11541                      const PCHContainerReader &PCHContainerRdr,
11542                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11543                      StringRef isysroot,
11544                      DisableValidationForModuleKind DisableValidationKind,
11545                      bool AllowASTWithCompilerErrors,
11546                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11547                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11548                      std::unique_ptr<llvm::Timer> ReadTimer)
11549     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11550                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11551                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11552       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11553       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11554       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11555                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11556       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11557       DisableValidationKind(DisableValidationKind),
11558       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11559       AllowConfigurationMismatch(AllowConfigurationMismatch),
11560       ValidateSystemInputs(ValidateSystemInputs),
11561       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11562       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11563   SourceMgr.setExternalSLocEntrySource(this);
11564 
11565   for (const auto &Ext : Extensions) {
11566     auto BlockName = Ext->getExtensionMetadata().BlockName;
11567     auto Known = ModuleFileExtensions.find(BlockName);
11568     if (Known != ModuleFileExtensions.end()) {
11569       Diags.Report(diag::warn_duplicate_module_file_extension)
11570         << BlockName;
11571       continue;
11572     }
11573 
11574     ModuleFileExtensions.insert({BlockName, Ext});
11575   }
11576 }
11577 
11578 ASTReader::~ASTReader() {
11579   if (OwnsDeserializationListener)
11580     delete DeserializationListener;
11581 }
11582 
11583 IdentifierResolver &ASTReader::getIdResolver() {
11584   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11585 }
11586 
11587 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11588                                                unsigned AbbrevID) {
11589   Idx = 0;
11590   Record.clear();
11591   return Cursor.readRecord(AbbrevID, Record);
11592 }
11593 //===----------------------------------------------------------------------===//
11594 //// OMPClauseReader implementation
11595 ////===----------------------------------------------------------------------===//
11596 
11597 // This has to be in namespace clang because it's friended by all
11598 // of the OMP clauses.
11599 namespace clang {
11600 
11601 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11602   ASTRecordReader &Record;
11603   ASTContext &Context;
11604 
11605 public:
11606   OMPClauseReader(ASTRecordReader &Record)
11607       : Record(Record), Context(Record.getContext()) {}
11608 #define GEN_CLANG_CLAUSE_CLASS
11609 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11610 #include "llvm/Frontend/OpenMP/OMP.inc"
11611   OMPClause *readClause();
11612   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11613   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11614 };
11615 
11616 } // end namespace clang
11617 
11618 OMPClause *ASTRecordReader::readOMPClause() {
11619   return OMPClauseReader(*this).readClause();
11620 }
11621 
11622 OMPClause *OMPClauseReader::readClause() {
11623   OMPClause *C = nullptr;
11624   switch (llvm::omp::Clause(Record.readInt())) {
11625   case llvm::omp::OMPC_if:
11626     C = new (Context) OMPIfClause();
11627     break;
11628   case llvm::omp::OMPC_final:
11629     C = new (Context) OMPFinalClause();
11630     break;
11631   case llvm::omp::OMPC_num_threads:
11632     C = new (Context) OMPNumThreadsClause();
11633     break;
11634   case llvm::omp::OMPC_safelen:
11635     C = new (Context) OMPSafelenClause();
11636     break;
11637   case llvm::omp::OMPC_simdlen:
11638     C = new (Context) OMPSimdlenClause();
11639     break;
11640   case llvm::omp::OMPC_sizes: {
11641     unsigned NumSizes = Record.readInt();
11642     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11643     break;
11644   }
11645   case llvm::omp::OMPC_full:
11646     C = OMPFullClause::CreateEmpty(Context);
11647     break;
11648   case llvm::omp::OMPC_partial:
11649     C = OMPPartialClause::CreateEmpty(Context);
11650     break;
11651   case llvm::omp::OMPC_allocator:
11652     C = new (Context) OMPAllocatorClause();
11653     break;
11654   case llvm::omp::OMPC_collapse:
11655     C = new (Context) OMPCollapseClause();
11656     break;
11657   case llvm::omp::OMPC_default:
11658     C = new (Context) OMPDefaultClause();
11659     break;
11660   case llvm::omp::OMPC_proc_bind:
11661     C = new (Context) OMPProcBindClause();
11662     break;
11663   case llvm::omp::OMPC_schedule:
11664     C = new (Context) OMPScheduleClause();
11665     break;
11666   case llvm::omp::OMPC_ordered:
11667     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11668     break;
11669   case llvm::omp::OMPC_nowait:
11670     C = new (Context) OMPNowaitClause();
11671     break;
11672   case llvm::omp::OMPC_untied:
11673     C = new (Context) OMPUntiedClause();
11674     break;
11675   case llvm::omp::OMPC_mergeable:
11676     C = new (Context) OMPMergeableClause();
11677     break;
11678   case llvm::omp::OMPC_read:
11679     C = new (Context) OMPReadClause();
11680     break;
11681   case llvm::omp::OMPC_write:
11682     C = new (Context) OMPWriteClause();
11683     break;
11684   case llvm::omp::OMPC_update:
11685     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11686     break;
11687   case llvm::omp::OMPC_capture:
11688     C = new (Context) OMPCaptureClause();
11689     break;
11690   case llvm::omp::OMPC_compare:
11691     C = new (Context) OMPCompareClause();
11692     break;
11693   case llvm::omp::OMPC_seq_cst:
11694     C = new (Context) OMPSeqCstClause();
11695     break;
11696   case llvm::omp::OMPC_acq_rel:
11697     C = new (Context) OMPAcqRelClause();
11698     break;
11699   case llvm::omp::OMPC_acquire:
11700     C = new (Context) OMPAcquireClause();
11701     break;
11702   case llvm::omp::OMPC_release:
11703     C = new (Context) OMPReleaseClause();
11704     break;
11705   case llvm::omp::OMPC_relaxed:
11706     C = new (Context) OMPRelaxedClause();
11707     break;
11708   case llvm::omp::OMPC_threads:
11709     C = new (Context) OMPThreadsClause();
11710     break;
11711   case llvm::omp::OMPC_simd:
11712     C = new (Context) OMPSIMDClause();
11713     break;
11714   case llvm::omp::OMPC_nogroup:
11715     C = new (Context) OMPNogroupClause();
11716     break;
11717   case llvm::omp::OMPC_unified_address:
11718     C = new (Context) OMPUnifiedAddressClause();
11719     break;
11720   case llvm::omp::OMPC_unified_shared_memory:
11721     C = new (Context) OMPUnifiedSharedMemoryClause();
11722     break;
11723   case llvm::omp::OMPC_reverse_offload:
11724     C = new (Context) OMPReverseOffloadClause();
11725     break;
11726   case llvm::omp::OMPC_dynamic_allocators:
11727     C = new (Context) OMPDynamicAllocatorsClause();
11728     break;
11729   case llvm::omp::OMPC_atomic_default_mem_order:
11730     C = new (Context) OMPAtomicDefaultMemOrderClause();
11731     break;
11732  case llvm::omp::OMPC_private:
11733     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11734     break;
11735   case llvm::omp::OMPC_firstprivate:
11736     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11737     break;
11738   case llvm::omp::OMPC_lastprivate:
11739     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11740     break;
11741   case llvm::omp::OMPC_shared:
11742     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11743     break;
11744   case llvm::omp::OMPC_reduction: {
11745     unsigned N = Record.readInt();
11746     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11747     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11748     break;
11749   }
11750   case llvm::omp::OMPC_task_reduction:
11751     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11752     break;
11753   case llvm::omp::OMPC_in_reduction:
11754     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11755     break;
11756   case llvm::omp::OMPC_linear:
11757     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11758     break;
11759   case llvm::omp::OMPC_aligned:
11760     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11761     break;
11762   case llvm::omp::OMPC_copyin:
11763     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11764     break;
11765   case llvm::omp::OMPC_copyprivate:
11766     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11767     break;
11768   case llvm::omp::OMPC_flush:
11769     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11770     break;
11771   case llvm::omp::OMPC_depobj:
11772     C = OMPDepobjClause::CreateEmpty(Context);
11773     break;
11774   case llvm::omp::OMPC_depend: {
11775     unsigned NumVars = Record.readInt();
11776     unsigned NumLoops = Record.readInt();
11777     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11778     break;
11779   }
11780   case llvm::omp::OMPC_device:
11781     C = new (Context) OMPDeviceClause();
11782     break;
11783   case llvm::omp::OMPC_map: {
11784     OMPMappableExprListSizeTy Sizes;
11785     Sizes.NumVars = Record.readInt();
11786     Sizes.NumUniqueDeclarations = Record.readInt();
11787     Sizes.NumComponentLists = Record.readInt();
11788     Sizes.NumComponents = Record.readInt();
11789     C = OMPMapClause::CreateEmpty(Context, Sizes);
11790     break;
11791   }
11792   case llvm::omp::OMPC_num_teams:
11793     C = new (Context) OMPNumTeamsClause();
11794     break;
11795   case llvm::omp::OMPC_thread_limit:
11796     C = new (Context) OMPThreadLimitClause();
11797     break;
11798   case llvm::omp::OMPC_priority:
11799     C = new (Context) OMPPriorityClause();
11800     break;
11801   case llvm::omp::OMPC_grainsize:
11802     C = new (Context) OMPGrainsizeClause();
11803     break;
11804   case llvm::omp::OMPC_num_tasks:
11805     C = new (Context) OMPNumTasksClause();
11806     break;
11807   case llvm::omp::OMPC_hint:
11808     C = new (Context) OMPHintClause();
11809     break;
11810   case llvm::omp::OMPC_dist_schedule:
11811     C = new (Context) OMPDistScheduleClause();
11812     break;
11813   case llvm::omp::OMPC_defaultmap:
11814     C = new (Context) OMPDefaultmapClause();
11815     break;
11816   case llvm::omp::OMPC_to: {
11817     OMPMappableExprListSizeTy Sizes;
11818     Sizes.NumVars = Record.readInt();
11819     Sizes.NumUniqueDeclarations = Record.readInt();
11820     Sizes.NumComponentLists = Record.readInt();
11821     Sizes.NumComponents = Record.readInt();
11822     C = OMPToClause::CreateEmpty(Context, Sizes);
11823     break;
11824   }
11825   case llvm::omp::OMPC_from: {
11826     OMPMappableExprListSizeTy Sizes;
11827     Sizes.NumVars = Record.readInt();
11828     Sizes.NumUniqueDeclarations = Record.readInt();
11829     Sizes.NumComponentLists = Record.readInt();
11830     Sizes.NumComponents = Record.readInt();
11831     C = OMPFromClause::CreateEmpty(Context, Sizes);
11832     break;
11833   }
11834   case llvm::omp::OMPC_use_device_ptr: {
11835     OMPMappableExprListSizeTy Sizes;
11836     Sizes.NumVars = Record.readInt();
11837     Sizes.NumUniqueDeclarations = Record.readInt();
11838     Sizes.NumComponentLists = Record.readInt();
11839     Sizes.NumComponents = Record.readInt();
11840     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11841     break;
11842   }
11843   case llvm::omp::OMPC_use_device_addr: {
11844     OMPMappableExprListSizeTy Sizes;
11845     Sizes.NumVars = Record.readInt();
11846     Sizes.NumUniqueDeclarations = Record.readInt();
11847     Sizes.NumComponentLists = Record.readInt();
11848     Sizes.NumComponents = Record.readInt();
11849     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11850     break;
11851   }
11852   case llvm::omp::OMPC_is_device_ptr: {
11853     OMPMappableExprListSizeTy Sizes;
11854     Sizes.NumVars = Record.readInt();
11855     Sizes.NumUniqueDeclarations = Record.readInt();
11856     Sizes.NumComponentLists = Record.readInt();
11857     Sizes.NumComponents = Record.readInt();
11858     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11859     break;
11860   }
11861   case llvm::omp::OMPC_allocate:
11862     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11863     break;
11864   case llvm::omp::OMPC_nontemporal:
11865     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11866     break;
11867   case llvm::omp::OMPC_inclusive:
11868     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11869     break;
11870   case llvm::omp::OMPC_exclusive:
11871     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11872     break;
11873   case llvm::omp::OMPC_order:
11874     C = new (Context) OMPOrderClause();
11875     break;
11876   case llvm::omp::OMPC_init:
11877     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11878     break;
11879   case llvm::omp::OMPC_use:
11880     C = new (Context) OMPUseClause();
11881     break;
11882   case llvm::omp::OMPC_destroy:
11883     C = new (Context) OMPDestroyClause();
11884     break;
11885   case llvm::omp::OMPC_novariants:
11886     C = new (Context) OMPNovariantsClause();
11887     break;
11888   case llvm::omp::OMPC_nocontext:
11889     C = new (Context) OMPNocontextClause();
11890     break;
11891   case llvm::omp::OMPC_detach:
11892     C = new (Context) OMPDetachClause();
11893     break;
11894   case llvm::omp::OMPC_uses_allocators:
11895     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11896     break;
11897   case llvm::omp::OMPC_affinity:
11898     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11899     break;
11900   case llvm::omp::OMPC_filter:
11901     C = new (Context) OMPFilterClause();
11902     break;
11903   case llvm::omp::OMPC_bind:
11904     C = OMPBindClause::CreateEmpty(Context);
11905     break;
11906   case llvm::omp::OMPC_align:
11907     C = new (Context) OMPAlignClause();
11908     break;
11909 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11910   case llvm::omp::Enum:                                                        \
11911     break;
11912 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11913   default:
11914     break;
11915   }
11916   assert(C && "Unknown OMPClause type");
11917 
11918   Visit(C);
11919   C->setLocStart(Record.readSourceLocation());
11920   C->setLocEnd(Record.readSourceLocation());
11921 
11922   return C;
11923 }
11924 
11925 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11926   C->setPreInitStmt(Record.readSubStmt(),
11927                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11928 }
11929 
11930 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11931   VisitOMPClauseWithPreInit(C);
11932   C->setPostUpdateExpr(Record.readSubExpr());
11933 }
11934 
11935 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11936   VisitOMPClauseWithPreInit(C);
11937   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11938   C->setNameModifierLoc(Record.readSourceLocation());
11939   C->setColonLoc(Record.readSourceLocation());
11940   C->setCondition(Record.readSubExpr());
11941   C->setLParenLoc(Record.readSourceLocation());
11942 }
11943 
11944 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
11945   VisitOMPClauseWithPreInit(C);
11946   C->setCondition(Record.readSubExpr());
11947   C->setLParenLoc(Record.readSourceLocation());
11948 }
11949 
11950 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
11951   VisitOMPClauseWithPreInit(C);
11952   C->setNumThreads(Record.readSubExpr());
11953   C->setLParenLoc(Record.readSourceLocation());
11954 }
11955 
11956 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
11957   C->setSafelen(Record.readSubExpr());
11958   C->setLParenLoc(Record.readSourceLocation());
11959 }
11960 
11961 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
11962   C->setSimdlen(Record.readSubExpr());
11963   C->setLParenLoc(Record.readSourceLocation());
11964 }
11965 
11966 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
11967   for (Expr *&E : C->getSizesRefs())
11968     E = Record.readSubExpr();
11969   C->setLParenLoc(Record.readSourceLocation());
11970 }
11971 
11972 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
11973 
11974 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
11975   C->setFactor(Record.readSubExpr());
11976   C->setLParenLoc(Record.readSourceLocation());
11977 }
11978 
11979 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
11980   C->setAllocator(Record.readExpr());
11981   C->setLParenLoc(Record.readSourceLocation());
11982 }
11983 
11984 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
11985   C->setNumForLoops(Record.readSubExpr());
11986   C->setLParenLoc(Record.readSourceLocation());
11987 }
11988 
11989 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
11990   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
11991   C->setLParenLoc(Record.readSourceLocation());
11992   C->setDefaultKindKwLoc(Record.readSourceLocation());
11993 }
11994 
11995 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
11996   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
11997   C->setLParenLoc(Record.readSourceLocation());
11998   C->setProcBindKindKwLoc(Record.readSourceLocation());
11999 }
12000 
12001 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12002   VisitOMPClauseWithPreInit(C);
12003   C->setScheduleKind(
12004        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12005   C->setFirstScheduleModifier(
12006       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12007   C->setSecondScheduleModifier(
12008       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12009   C->setChunkSize(Record.readSubExpr());
12010   C->setLParenLoc(Record.readSourceLocation());
12011   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12012   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12013   C->setScheduleKindLoc(Record.readSourceLocation());
12014   C->setCommaLoc(Record.readSourceLocation());
12015 }
12016 
12017 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12018   C->setNumForLoops(Record.readSubExpr());
12019   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12020     C->setLoopNumIterations(I, Record.readSubExpr());
12021   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12022     C->setLoopCounter(I, Record.readSubExpr());
12023   C->setLParenLoc(Record.readSourceLocation());
12024 }
12025 
12026 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12027   C->setEventHandler(Record.readSubExpr());
12028   C->setLParenLoc(Record.readSourceLocation());
12029 }
12030 
12031 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12032 
12033 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12034 
12035 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12036 
12037 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12038 
12039 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12040 
12041 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12042   if (C->isExtended()) {
12043     C->setLParenLoc(Record.readSourceLocation());
12044     C->setArgumentLoc(Record.readSourceLocation());
12045     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12046   }
12047 }
12048 
12049 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12050 
12051 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12052 
12053 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12054 
12055 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12056 
12057 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12058 
12059 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12060 
12061 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12062 
12063 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12064 
12065 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12066 
12067 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12068 
12069 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12070   unsigned NumVars = C->varlist_size();
12071   SmallVector<Expr *, 16> Vars;
12072   Vars.reserve(NumVars);
12073   for (unsigned I = 0; I != NumVars; ++I)
12074     Vars.push_back(Record.readSubExpr());
12075   C->setVarRefs(Vars);
12076   C->setIsTarget(Record.readBool());
12077   C->setIsTargetSync(Record.readBool());
12078   C->setLParenLoc(Record.readSourceLocation());
12079   C->setVarLoc(Record.readSourceLocation());
12080 }
12081 
12082 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12083   C->setInteropVar(Record.readSubExpr());
12084   C->setLParenLoc(Record.readSourceLocation());
12085   C->setVarLoc(Record.readSourceLocation());
12086 }
12087 
12088 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12089   C->setInteropVar(Record.readSubExpr());
12090   C->setLParenLoc(Record.readSourceLocation());
12091   C->setVarLoc(Record.readSourceLocation());
12092 }
12093 
12094 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12095   VisitOMPClauseWithPreInit(C);
12096   C->setCondition(Record.readSubExpr());
12097   C->setLParenLoc(Record.readSourceLocation());
12098 }
12099 
12100 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12101   VisitOMPClauseWithPreInit(C);
12102   C->setCondition(Record.readSubExpr());
12103   C->setLParenLoc(Record.readSourceLocation());
12104 }
12105 
12106 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12107 
12108 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12109     OMPUnifiedSharedMemoryClause *) {}
12110 
12111 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12112 
12113 void
12114 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12115 }
12116 
12117 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12118     OMPAtomicDefaultMemOrderClause *C) {
12119   C->setAtomicDefaultMemOrderKind(
12120       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12121   C->setLParenLoc(Record.readSourceLocation());
12122   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12123 }
12124 
12125 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12126   C->setLParenLoc(Record.readSourceLocation());
12127   unsigned NumVars = C->varlist_size();
12128   SmallVector<Expr *, 16> Vars;
12129   Vars.reserve(NumVars);
12130   for (unsigned i = 0; i != NumVars; ++i)
12131     Vars.push_back(Record.readSubExpr());
12132   C->setVarRefs(Vars);
12133   Vars.clear();
12134   for (unsigned i = 0; i != NumVars; ++i)
12135     Vars.push_back(Record.readSubExpr());
12136   C->setPrivateCopies(Vars);
12137 }
12138 
12139 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12140   VisitOMPClauseWithPreInit(C);
12141   C->setLParenLoc(Record.readSourceLocation());
12142   unsigned NumVars = C->varlist_size();
12143   SmallVector<Expr *, 16> Vars;
12144   Vars.reserve(NumVars);
12145   for (unsigned i = 0; i != NumVars; ++i)
12146     Vars.push_back(Record.readSubExpr());
12147   C->setVarRefs(Vars);
12148   Vars.clear();
12149   for (unsigned i = 0; i != NumVars; ++i)
12150     Vars.push_back(Record.readSubExpr());
12151   C->setPrivateCopies(Vars);
12152   Vars.clear();
12153   for (unsigned i = 0; i != NumVars; ++i)
12154     Vars.push_back(Record.readSubExpr());
12155   C->setInits(Vars);
12156 }
12157 
12158 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12159   VisitOMPClauseWithPostUpdate(C);
12160   C->setLParenLoc(Record.readSourceLocation());
12161   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12162   C->setKindLoc(Record.readSourceLocation());
12163   C->setColonLoc(Record.readSourceLocation());
12164   unsigned NumVars = C->varlist_size();
12165   SmallVector<Expr *, 16> Vars;
12166   Vars.reserve(NumVars);
12167   for (unsigned i = 0; i != NumVars; ++i)
12168     Vars.push_back(Record.readSubExpr());
12169   C->setVarRefs(Vars);
12170   Vars.clear();
12171   for (unsigned i = 0; i != NumVars; ++i)
12172     Vars.push_back(Record.readSubExpr());
12173   C->setPrivateCopies(Vars);
12174   Vars.clear();
12175   for (unsigned i = 0; i != NumVars; ++i)
12176     Vars.push_back(Record.readSubExpr());
12177   C->setSourceExprs(Vars);
12178   Vars.clear();
12179   for (unsigned i = 0; i != NumVars; ++i)
12180     Vars.push_back(Record.readSubExpr());
12181   C->setDestinationExprs(Vars);
12182   Vars.clear();
12183   for (unsigned i = 0; i != NumVars; ++i)
12184     Vars.push_back(Record.readSubExpr());
12185   C->setAssignmentOps(Vars);
12186 }
12187 
12188 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12189   C->setLParenLoc(Record.readSourceLocation());
12190   unsigned NumVars = C->varlist_size();
12191   SmallVector<Expr *, 16> Vars;
12192   Vars.reserve(NumVars);
12193   for (unsigned i = 0; i != NumVars; ++i)
12194     Vars.push_back(Record.readSubExpr());
12195   C->setVarRefs(Vars);
12196 }
12197 
12198 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12199   VisitOMPClauseWithPostUpdate(C);
12200   C->setLParenLoc(Record.readSourceLocation());
12201   C->setModifierLoc(Record.readSourceLocation());
12202   C->setColonLoc(Record.readSourceLocation());
12203   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12204   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12205   C->setQualifierLoc(NNSL);
12206   C->setNameInfo(DNI);
12207 
12208   unsigned NumVars = C->varlist_size();
12209   SmallVector<Expr *, 16> Vars;
12210   Vars.reserve(NumVars);
12211   for (unsigned i = 0; i != NumVars; ++i)
12212     Vars.push_back(Record.readSubExpr());
12213   C->setVarRefs(Vars);
12214   Vars.clear();
12215   for (unsigned i = 0; i != NumVars; ++i)
12216     Vars.push_back(Record.readSubExpr());
12217   C->setPrivates(Vars);
12218   Vars.clear();
12219   for (unsigned i = 0; i != NumVars; ++i)
12220     Vars.push_back(Record.readSubExpr());
12221   C->setLHSExprs(Vars);
12222   Vars.clear();
12223   for (unsigned i = 0; i != NumVars; ++i)
12224     Vars.push_back(Record.readSubExpr());
12225   C->setRHSExprs(Vars);
12226   Vars.clear();
12227   for (unsigned i = 0; i != NumVars; ++i)
12228     Vars.push_back(Record.readSubExpr());
12229   C->setReductionOps(Vars);
12230   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12231     Vars.clear();
12232     for (unsigned i = 0; i != NumVars; ++i)
12233       Vars.push_back(Record.readSubExpr());
12234     C->setInscanCopyOps(Vars);
12235     Vars.clear();
12236     for (unsigned i = 0; i != NumVars; ++i)
12237       Vars.push_back(Record.readSubExpr());
12238     C->setInscanCopyArrayTemps(Vars);
12239     Vars.clear();
12240     for (unsigned i = 0; i != NumVars; ++i)
12241       Vars.push_back(Record.readSubExpr());
12242     C->setInscanCopyArrayElems(Vars);
12243   }
12244 }
12245 
12246 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12247   VisitOMPClauseWithPostUpdate(C);
12248   C->setLParenLoc(Record.readSourceLocation());
12249   C->setColonLoc(Record.readSourceLocation());
12250   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12251   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12252   C->setQualifierLoc(NNSL);
12253   C->setNameInfo(DNI);
12254 
12255   unsigned NumVars = C->varlist_size();
12256   SmallVector<Expr *, 16> Vars;
12257   Vars.reserve(NumVars);
12258   for (unsigned I = 0; I != NumVars; ++I)
12259     Vars.push_back(Record.readSubExpr());
12260   C->setVarRefs(Vars);
12261   Vars.clear();
12262   for (unsigned I = 0; I != NumVars; ++I)
12263     Vars.push_back(Record.readSubExpr());
12264   C->setPrivates(Vars);
12265   Vars.clear();
12266   for (unsigned I = 0; I != NumVars; ++I)
12267     Vars.push_back(Record.readSubExpr());
12268   C->setLHSExprs(Vars);
12269   Vars.clear();
12270   for (unsigned I = 0; I != NumVars; ++I)
12271     Vars.push_back(Record.readSubExpr());
12272   C->setRHSExprs(Vars);
12273   Vars.clear();
12274   for (unsigned I = 0; I != NumVars; ++I)
12275     Vars.push_back(Record.readSubExpr());
12276   C->setReductionOps(Vars);
12277 }
12278 
12279 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12280   VisitOMPClauseWithPostUpdate(C);
12281   C->setLParenLoc(Record.readSourceLocation());
12282   C->setColonLoc(Record.readSourceLocation());
12283   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12284   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12285   C->setQualifierLoc(NNSL);
12286   C->setNameInfo(DNI);
12287 
12288   unsigned NumVars = C->varlist_size();
12289   SmallVector<Expr *, 16> Vars;
12290   Vars.reserve(NumVars);
12291   for (unsigned I = 0; I != NumVars; ++I)
12292     Vars.push_back(Record.readSubExpr());
12293   C->setVarRefs(Vars);
12294   Vars.clear();
12295   for (unsigned I = 0; I != NumVars; ++I)
12296     Vars.push_back(Record.readSubExpr());
12297   C->setPrivates(Vars);
12298   Vars.clear();
12299   for (unsigned I = 0; I != NumVars; ++I)
12300     Vars.push_back(Record.readSubExpr());
12301   C->setLHSExprs(Vars);
12302   Vars.clear();
12303   for (unsigned I = 0; I != NumVars; ++I)
12304     Vars.push_back(Record.readSubExpr());
12305   C->setRHSExprs(Vars);
12306   Vars.clear();
12307   for (unsigned I = 0; I != NumVars; ++I)
12308     Vars.push_back(Record.readSubExpr());
12309   C->setReductionOps(Vars);
12310   Vars.clear();
12311   for (unsigned I = 0; I != NumVars; ++I)
12312     Vars.push_back(Record.readSubExpr());
12313   C->setTaskgroupDescriptors(Vars);
12314 }
12315 
12316 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12317   VisitOMPClauseWithPostUpdate(C);
12318   C->setLParenLoc(Record.readSourceLocation());
12319   C->setColonLoc(Record.readSourceLocation());
12320   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12321   C->setModifierLoc(Record.readSourceLocation());
12322   unsigned NumVars = C->varlist_size();
12323   SmallVector<Expr *, 16> Vars;
12324   Vars.reserve(NumVars);
12325   for (unsigned i = 0; i != NumVars; ++i)
12326     Vars.push_back(Record.readSubExpr());
12327   C->setVarRefs(Vars);
12328   Vars.clear();
12329   for (unsigned i = 0; i != NumVars; ++i)
12330     Vars.push_back(Record.readSubExpr());
12331   C->setPrivates(Vars);
12332   Vars.clear();
12333   for (unsigned i = 0; i != NumVars; ++i)
12334     Vars.push_back(Record.readSubExpr());
12335   C->setInits(Vars);
12336   Vars.clear();
12337   for (unsigned i = 0; i != NumVars; ++i)
12338     Vars.push_back(Record.readSubExpr());
12339   C->setUpdates(Vars);
12340   Vars.clear();
12341   for (unsigned i = 0; i != NumVars; ++i)
12342     Vars.push_back(Record.readSubExpr());
12343   C->setFinals(Vars);
12344   C->setStep(Record.readSubExpr());
12345   C->setCalcStep(Record.readSubExpr());
12346   Vars.clear();
12347   for (unsigned I = 0; I != NumVars + 1; ++I)
12348     Vars.push_back(Record.readSubExpr());
12349   C->setUsedExprs(Vars);
12350 }
12351 
12352 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12353   C->setLParenLoc(Record.readSourceLocation());
12354   C->setColonLoc(Record.readSourceLocation());
12355   unsigned NumVars = C->varlist_size();
12356   SmallVector<Expr *, 16> Vars;
12357   Vars.reserve(NumVars);
12358   for (unsigned i = 0; i != NumVars; ++i)
12359     Vars.push_back(Record.readSubExpr());
12360   C->setVarRefs(Vars);
12361   C->setAlignment(Record.readSubExpr());
12362 }
12363 
12364 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12365   C->setLParenLoc(Record.readSourceLocation());
12366   unsigned NumVars = C->varlist_size();
12367   SmallVector<Expr *, 16> Exprs;
12368   Exprs.reserve(NumVars);
12369   for (unsigned i = 0; i != NumVars; ++i)
12370     Exprs.push_back(Record.readSubExpr());
12371   C->setVarRefs(Exprs);
12372   Exprs.clear();
12373   for (unsigned i = 0; i != NumVars; ++i)
12374     Exprs.push_back(Record.readSubExpr());
12375   C->setSourceExprs(Exprs);
12376   Exprs.clear();
12377   for (unsigned i = 0; i != NumVars; ++i)
12378     Exprs.push_back(Record.readSubExpr());
12379   C->setDestinationExprs(Exprs);
12380   Exprs.clear();
12381   for (unsigned i = 0; i != NumVars; ++i)
12382     Exprs.push_back(Record.readSubExpr());
12383   C->setAssignmentOps(Exprs);
12384 }
12385 
12386 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12387   C->setLParenLoc(Record.readSourceLocation());
12388   unsigned NumVars = C->varlist_size();
12389   SmallVector<Expr *, 16> Exprs;
12390   Exprs.reserve(NumVars);
12391   for (unsigned i = 0; i != NumVars; ++i)
12392     Exprs.push_back(Record.readSubExpr());
12393   C->setVarRefs(Exprs);
12394   Exprs.clear();
12395   for (unsigned i = 0; i != NumVars; ++i)
12396     Exprs.push_back(Record.readSubExpr());
12397   C->setSourceExprs(Exprs);
12398   Exprs.clear();
12399   for (unsigned i = 0; i != NumVars; ++i)
12400     Exprs.push_back(Record.readSubExpr());
12401   C->setDestinationExprs(Exprs);
12402   Exprs.clear();
12403   for (unsigned i = 0; i != NumVars; ++i)
12404     Exprs.push_back(Record.readSubExpr());
12405   C->setAssignmentOps(Exprs);
12406 }
12407 
12408 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12409   C->setLParenLoc(Record.readSourceLocation());
12410   unsigned NumVars = C->varlist_size();
12411   SmallVector<Expr *, 16> Vars;
12412   Vars.reserve(NumVars);
12413   for (unsigned i = 0; i != NumVars; ++i)
12414     Vars.push_back(Record.readSubExpr());
12415   C->setVarRefs(Vars);
12416 }
12417 
12418 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12419   C->setDepobj(Record.readSubExpr());
12420   C->setLParenLoc(Record.readSourceLocation());
12421 }
12422 
12423 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12424   C->setLParenLoc(Record.readSourceLocation());
12425   C->setModifier(Record.readSubExpr());
12426   C->setDependencyKind(
12427       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12428   C->setDependencyLoc(Record.readSourceLocation());
12429   C->setColonLoc(Record.readSourceLocation());
12430   unsigned NumVars = C->varlist_size();
12431   SmallVector<Expr *, 16> Vars;
12432   Vars.reserve(NumVars);
12433   for (unsigned I = 0; I != NumVars; ++I)
12434     Vars.push_back(Record.readSubExpr());
12435   C->setVarRefs(Vars);
12436   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12437     C->setLoopData(I, Record.readSubExpr());
12438 }
12439 
12440 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12441   VisitOMPClauseWithPreInit(C);
12442   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12443   C->setDevice(Record.readSubExpr());
12444   C->setModifierLoc(Record.readSourceLocation());
12445   C->setLParenLoc(Record.readSourceLocation());
12446 }
12447 
12448 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12449   C->setLParenLoc(Record.readSourceLocation());
12450   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12451     C->setMapTypeModifier(
12452         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12453     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12454   }
12455   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12456   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12457   C->setMapType(
12458      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12459   C->setMapLoc(Record.readSourceLocation());
12460   C->setColonLoc(Record.readSourceLocation());
12461   auto NumVars = C->varlist_size();
12462   auto UniqueDecls = C->getUniqueDeclarationsNum();
12463   auto TotalLists = C->getTotalComponentListNum();
12464   auto TotalComponents = C->getTotalComponentsNum();
12465 
12466   SmallVector<Expr *, 16> Vars;
12467   Vars.reserve(NumVars);
12468   for (unsigned i = 0; i != NumVars; ++i)
12469     Vars.push_back(Record.readExpr());
12470   C->setVarRefs(Vars);
12471 
12472   SmallVector<Expr *, 16> UDMappers;
12473   UDMappers.reserve(NumVars);
12474   for (unsigned I = 0; I < NumVars; ++I)
12475     UDMappers.push_back(Record.readExpr());
12476   C->setUDMapperRefs(UDMappers);
12477 
12478   SmallVector<ValueDecl *, 16> Decls;
12479   Decls.reserve(UniqueDecls);
12480   for (unsigned i = 0; i < UniqueDecls; ++i)
12481     Decls.push_back(Record.readDeclAs<ValueDecl>());
12482   C->setUniqueDecls(Decls);
12483 
12484   SmallVector<unsigned, 16> ListsPerDecl;
12485   ListsPerDecl.reserve(UniqueDecls);
12486   for (unsigned i = 0; i < UniqueDecls; ++i)
12487     ListsPerDecl.push_back(Record.readInt());
12488   C->setDeclNumLists(ListsPerDecl);
12489 
12490   SmallVector<unsigned, 32> ListSizes;
12491   ListSizes.reserve(TotalLists);
12492   for (unsigned i = 0; i < TotalLists; ++i)
12493     ListSizes.push_back(Record.readInt());
12494   C->setComponentListSizes(ListSizes);
12495 
12496   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12497   Components.reserve(TotalComponents);
12498   for (unsigned i = 0; i < TotalComponents; ++i) {
12499     Expr *AssociatedExprPr = Record.readExpr();
12500     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12501     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12502                             /*IsNonContiguous=*/false);
12503   }
12504   C->setComponents(Components, ListSizes);
12505 }
12506 
12507 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12508   C->setLParenLoc(Record.readSourceLocation());
12509   C->setColonLoc(Record.readSourceLocation());
12510   C->setAllocator(Record.readSubExpr());
12511   unsigned NumVars = C->varlist_size();
12512   SmallVector<Expr *, 16> Vars;
12513   Vars.reserve(NumVars);
12514   for (unsigned i = 0; i != NumVars; ++i)
12515     Vars.push_back(Record.readSubExpr());
12516   C->setVarRefs(Vars);
12517 }
12518 
12519 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12520   VisitOMPClauseWithPreInit(C);
12521   C->setNumTeams(Record.readSubExpr());
12522   C->setLParenLoc(Record.readSourceLocation());
12523 }
12524 
12525 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12526   VisitOMPClauseWithPreInit(C);
12527   C->setThreadLimit(Record.readSubExpr());
12528   C->setLParenLoc(Record.readSourceLocation());
12529 }
12530 
12531 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12532   VisitOMPClauseWithPreInit(C);
12533   C->setPriority(Record.readSubExpr());
12534   C->setLParenLoc(Record.readSourceLocation());
12535 }
12536 
12537 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12538   VisitOMPClauseWithPreInit(C);
12539   C->setGrainsize(Record.readSubExpr());
12540   C->setLParenLoc(Record.readSourceLocation());
12541 }
12542 
12543 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12544   VisitOMPClauseWithPreInit(C);
12545   C->setNumTasks(Record.readSubExpr());
12546   C->setLParenLoc(Record.readSourceLocation());
12547 }
12548 
12549 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12550   C->setHint(Record.readSubExpr());
12551   C->setLParenLoc(Record.readSourceLocation());
12552 }
12553 
12554 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12555   VisitOMPClauseWithPreInit(C);
12556   C->setDistScheduleKind(
12557       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12558   C->setChunkSize(Record.readSubExpr());
12559   C->setLParenLoc(Record.readSourceLocation());
12560   C->setDistScheduleKindLoc(Record.readSourceLocation());
12561   C->setCommaLoc(Record.readSourceLocation());
12562 }
12563 
12564 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12565   C->setDefaultmapKind(
12566        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12567   C->setDefaultmapModifier(
12568       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12569   C->setLParenLoc(Record.readSourceLocation());
12570   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12571   C->setDefaultmapKindLoc(Record.readSourceLocation());
12572 }
12573 
12574 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12575   C->setLParenLoc(Record.readSourceLocation());
12576   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12577     C->setMotionModifier(
12578         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12579     C->setMotionModifierLoc(I, Record.readSourceLocation());
12580   }
12581   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12582   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12583   C->setColonLoc(Record.readSourceLocation());
12584   auto NumVars = C->varlist_size();
12585   auto UniqueDecls = C->getUniqueDeclarationsNum();
12586   auto TotalLists = C->getTotalComponentListNum();
12587   auto TotalComponents = C->getTotalComponentsNum();
12588 
12589   SmallVector<Expr *, 16> Vars;
12590   Vars.reserve(NumVars);
12591   for (unsigned i = 0; i != NumVars; ++i)
12592     Vars.push_back(Record.readSubExpr());
12593   C->setVarRefs(Vars);
12594 
12595   SmallVector<Expr *, 16> UDMappers;
12596   UDMappers.reserve(NumVars);
12597   for (unsigned I = 0; I < NumVars; ++I)
12598     UDMappers.push_back(Record.readSubExpr());
12599   C->setUDMapperRefs(UDMappers);
12600 
12601   SmallVector<ValueDecl *, 16> Decls;
12602   Decls.reserve(UniqueDecls);
12603   for (unsigned i = 0; i < UniqueDecls; ++i)
12604     Decls.push_back(Record.readDeclAs<ValueDecl>());
12605   C->setUniqueDecls(Decls);
12606 
12607   SmallVector<unsigned, 16> ListsPerDecl;
12608   ListsPerDecl.reserve(UniqueDecls);
12609   for (unsigned i = 0; i < UniqueDecls; ++i)
12610     ListsPerDecl.push_back(Record.readInt());
12611   C->setDeclNumLists(ListsPerDecl);
12612 
12613   SmallVector<unsigned, 32> ListSizes;
12614   ListSizes.reserve(TotalLists);
12615   for (unsigned i = 0; i < TotalLists; ++i)
12616     ListSizes.push_back(Record.readInt());
12617   C->setComponentListSizes(ListSizes);
12618 
12619   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12620   Components.reserve(TotalComponents);
12621   for (unsigned i = 0; i < TotalComponents; ++i) {
12622     Expr *AssociatedExprPr = Record.readSubExpr();
12623     bool IsNonContiguous = Record.readBool();
12624     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12625     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12626   }
12627   C->setComponents(Components, ListSizes);
12628 }
12629 
12630 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12631   C->setLParenLoc(Record.readSourceLocation());
12632   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12633     C->setMotionModifier(
12634         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12635     C->setMotionModifierLoc(I, Record.readSourceLocation());
12636   }
12637   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12638   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12639   C->setColonLoc(Record.readSourceLocation());
12640   auto NumVars = C->varlist_size();
12641   auto UniqueDecls = C->getUniqueDeclarationsNum();
12642   auto TotalLists = C->getTotalComponentListNum();
12643   auto TotalComponents = C->getTotalComponentsNum();
12644 
12645   SmallVector<Expr *, 16> Vars;
12646   Vars.reserve(NumVars);
12647   for (unsigned i = 0; i != NumVars; ++i)
12648     Vars.push_back(Record.readSubExpr());
12649   C->setVarRefs(Vars);
12650 
12651   SmallVector<Expr *, 16> UDMappers;
12652   UDMappers.reserve(NumVars);
12653   for (unsigned I = 0; I < NumVars; ++I)
12654     UDMappers.push_back(Record.readSubExpr());
12655   C->setUDMapperRefs(UDMappers);
12656 
12657   SmallVector<ValueDecl *, 16> Decls;
12658   Decls.reserve(UniqueDecls);
12659   for (unsigned i = 0; i < UniqueDecls; ++i)
12660     Decls.push_back(Record.readDeclAs<ValueDecl>());
12661   C->setUniqueDecls(Decls);
12662 
12663   SmallVector<unsigned, 16> ListsPerDecl;
12664   ListsPerDecl.reserve(UniqueDecls);
12665   for (unsigned i = 0; i < UniqueDecls; ++i)
12666     ListsPerDecl.push_back(Record.readInt());
12667   C->setDeclNumLists(ListsPerDecl);
12668 
12669   SmallVector<unsigned, 32> ListSizes;
12670   ListSizes.reserve(TotalLists);
12671   for (unsigned i = 0; i < TotalLists; ++i)
12672     ListSizes.push_back(Record.readInt());
12673   C->setComponentListSizes(ListSizes);
12674 
12675   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12676   Components.reserve(TotalComponents);
12677   for (unsigned i = 0; i < TotalComponents; ++i) {
12678     Expr *AssociatedExprPr = Record.readSubExpr();
12679     bool IsNonContiguous = Record.readBool();
12680     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12681     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12682   }
12683   C->setComponents(Components, ListSizes);
12684 }
12685 
12686 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12687   C->setLParenLoc(Record.readSourceLocation());
12688   auto NumVars = C->varlist_size();
12689   auto UniqueDecls = C->getUniqueDeclarationsNum();
12690   auto TotalLists = C->getTotalComponentListNum();
12691   auto TotalComponents = C->getTotalComponentsNum();
12692 
12693   SmallVector<Expr *, 16> Vars;
12694   Vars.reserve(NumVars);
12695   for (unsigned i = 0; i != NumVars; ++i)
12696     Vars.push_back(Record.readSubExpr());
12697   C->setVarRefs(Vars);
12698   Vars.clear();
12699   for (unsigned i = 0; i != NumVars; ++i)
12700     Vars.push_back(Record.readSubExpr());
12701   C->setPrivateCopies(Vars);
12702   Vars.clear();
12703   for (unsigned i = 0; i != NumVars; ++i)
12704     Vars.push_back(Record.readSubExpr());
12705   C->setInits(Vars);
12706 
12707   SmallVector<ValueDecl *, 16> Decls;
12708   Decls.reserve(UniqueDecls);
12709   for (unsigned i = 0; i < UniqueDecls; ++i)
12710     Decls.push_back(Record.readDeclAs<ValueDecl>());
12711   C->setUniqueDecls(Decls);
12712 
12713   SmallVector<unsigned, 16> ListsPerDecl;
12714   ListsPerDecl.reserve(UniqueDecls);
12715   for (unsigned i = 0; i < UniqueDecls; ++i)
12716     ListsPerDecl.push_back(Record.readInt());
12717   C->setDeclNumLists(ListsPerDecl);
12718 
12719   SmallVector<unsigned, 32> ListSizes;
12720   ListSizes.reserve(TotalLists);
12721   for (unsigned i = 0; i < TotalLists; ++i)
12722     ListSizes.push_back(Record.readInt());
12723   C->setComponentListSizes(ListSizes);
12724 
12725   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12726   Components.reserve(TotalComponents);
12727   for (unsigned i = 0; i < TotalComponents; ++i) {
12728     auto *AssociatedExprPr = Record.readSubExpr();
12729     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12730     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12731                             /*IsNonContiguous=*/false);
12732   }
12733   C->setComponents(Components, ListSizes);
12734 }
12735 
12736 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12737   C->setLParenLoc(Record.readSourceLocation());
12738   auto NumVars = C->varlist_size();
12739   auto UniqueDecls = C->getUniqueDeclarationsNum();
12740   auto TotalLists = C->getTotalComponentListNum();
12741   auto TotalComponents = C->getTotalComponentsNum();
12742 
12743   SmallVector<Expr *, 16> Vars;
12744   Vars.reserve(NumVars);
12745   for (unsigned i = 0; i != NumVars; ++i)
12746     Vars.push_back(Record.readSubExpr());
12747   C->setVarRefs(Vars);
12748 
12749   SmallVector<ValueDecl *, 16> Decls;
12750   Decls.reserve(UniqueDecls);
12751   for (unsigned i = 0; i < UniqueDecls; ++i)
12752     Decls.push_back(Record.readDeclAs<ValueDecl>());
12753   C->setUniqueDecls(Decls);
12754 
12755   SmallVector<unsigned, 16> ListsPerDecl;
12756   ListsPerDecl.reserve(UniqueDecls);
12757   for (unsigned i = 0; i < UniqueDecls; ++i)
12758     ListsPerDecl.push_back(Record.readInt());
12759   C->setDeclNumLists(ListsPerDecl);
12760 
12761   SmallVector<unsigned, 32> ListSizes;
12762   ListSizes.reserve(TotalLists);
12763   for (unsigned i = 0; i < TotalLists; ++i)
12764     ListSizes.push_back(Record.readInt());
12765   C->setComponentListSizes(ListSizes);
12766 
12767   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12768   Components.reserve(TotalComponents);
12769   for (unsigned i = 0; i < TotalComponents; ++i) {
12770     Expr *AssociatedExpr = Record.readSubExpr();
12771     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12772     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12773                             /*IsNonContiguous*/ false);
12774   }
12775   C->setComponents(Components, ListSizes);
12776 }
12777 
12778 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12779   C->setLParenLoc(Record.readSourceLocation());
12780   auto NumVars = C->varlist_size();
12781   auto UniqueDecls = C->getUniqueDeclarationsNum();
12782   auto TotalLists = C->getTotalComponentListNum();
12783   auto TotalComponents = C->getTotalComponentsNum();
12784 
12785   SmallVector<Expr *, 16> Vars;
12786   Vars.reserve(NumVars);
12787   for (unsigned i = 0; i != NumVars; ++i)
12788     Vars.push_back(Record.readSubExpr());
12789   C->setVarRefs(Vars);
12790   Vars.clear();
12791 
12792   SmallVector<ValueDecl *, 16> Decls;
12793   Decls.reserve(UniqueDecls);
12794   for (unsigned i = 0; i < UniqueDecls; ++i)
12795     Decls.push_back(Record.readDeclAs<ValueDecl>());
12796   C->setUniqueDecls(Decls);
12797 
12798   SmallVector<unsigned, 16> ListsPerDecl;
12799   ListsPerDecl.reserve(UniqueDecls);
12800   for (unsigned i = 0; i < UniqueDecls; ++i)
12801     ListsPerDecl.push_back(Record.readInt());
12802   C->setDeclNumLists(ListsPerDecl);
12803 
12804   SmallVector<unsigned, 32> ListSizes;
12805   ListSizes.reserve(TotalLists);
12806   for (unsigned i = 0; i < TotalLists; ++i)
12807     ListSizes.push_back(Record.readInt());
12808   C->setComponentListSizes(ListSizes);
12809 
12810   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12811   Components.reserve(TotalComponents);
12812   for (unsigned i = 0; i < TotalComponents; ++i) {
12813     Expr *AssociatedExpr = Record.readSubExpr();
12814     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12815     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12816                             /*IsNonContiguous=*/false);
12817   }
12818   C->setComponents(Components, ListSizes);
12819 }
12820 
12821 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12822   C->setLParenLoc(Record.readSourceLocation());
12823   unsigned NumVars = C->varlist_size();
12824   SmallVector<Expr *, 16> Vars;
12825   Vars.reserve(NumVars);
12826   for (unsigned i = 0; i != NumVars; ++i)
12827     Vars.push_back(Record.readSubExpr());
12828   C->setVarRefs(Vars);
12829   Vars.clear();
12830   Vars.reserve(NumVars);
12831   for (unsigned i = 0; i != NumVars; ++i)
12832     Vars.push_back(Record.readSubExpr());
12833   C->setPrivateRefs(Vars);
12834 }
12835 
12836 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12837   C->setLParenLoc(Record.readSourceLocation());
12838   unsigned NumVars = C->varlist_size();
12839   SmallVector<Expr *, 16> Vars;
12840   Vars.reserve(NumVars);
12841   for (unsigned i = 0; i != NumVars; ++i)
12842     Vars.push_back(Record.readSubExpr());
12843   C->setVarRefs(Vars);
12844 }
12845 
12846 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12847   C->setLParenLoc(Record.readSourceLocation());
12848   unsigned NumVars = C->varlist_size();
12849   SmallVector<Expr *, 16> Vars;
12850   Vars.reserve(NumVars);
12851   for (unsigned i = 0; i != NumVars; ++i)
12852     Vars.push_back(Record.readSubExpr());
12853   C->setVarRefs(Vars);
12854 }
12855 
12856 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12857   C->setLParenLoc(Record.readSourceLocation());
12858   unsigned NumOfAllocators = C->getNumberOfAllocators();
12859   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12860   Data.reserve(NumOfAllocators);
12861   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12862     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12863     D.Allocator = Record.readSubExpr();
12864     D.AllocatorTraits = Record.readSubExpr();
12865     D.LParenLoc = Record.readSourceLocation();
12866     D.RParenLoc = Record.readSourceLocation();
12867   }
12868   C->setAllocatorsData(Data);
12869 }
12870 
12871 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12872   C->setLParenLoc(Record.readSourceLocation());
12873   C->setModifier(Record.readSubExpr());
12874   C->setColonLoc(Record.readSourceLocation());
12875   unsigned NumOfLocators = C->varlist_size();
12876   SmallVector<Expr *, 4> Locators;
12877   Locators.reserve(NumOfLocators);
12878   for (unsigned I = 0; I != NumOfLocators; ++I)
12879     Locators.push_back(Record.readSubExpr());
12880   C->setVarRefs(Locators);
12881 }
12882 
12883 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12884   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12885   C->setLParenLoc(Record.readSourceLocation());
12886   C->setKindKwLoc(Record.readSourceLocation());
12887 }
12888 
12889 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12890   VisitOMPClauseWithPreInit(C);
12891   C->setThreadID(Record.readSubExpr());
12892   C->setLParenLoc(Record.readSourceLocation());
12893 }
12894 
12895 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12896   C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12897   C->setLParenLoc(Record.readSourceLocation());
12898   C->setBindKindLoc(Record.readSourceLocation());
12899 }
12900 
12901 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12902   C->setAlignment(Record.readExpr());
12903   C->setLParenLoc(Record.readSourceLocation());
12904 }
12905 
12906 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12907   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12908   TI.Sets.resize(readUInt32());
12909   for (auto &Set : TI.Sets) {
12910     Set.Kind = readEnum<llvm::omp::TraitSet>();
12911     Set.Selectors.resize(readUInt32());
12912     for (auto &Selector : Set.Selectors) {
12913       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12914       Selector.ScoreOrCondition = nullptr;
12915       if (readBool())
12916         Selector.ScoreOrCondition = readExprRef();
12917       Selector.Properties.resize(readUInt32());
12918       for (auto &Property : Selector.Properties)
12919         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12920     }
12921   }
12922   return &TI;
12923 }
12924 
12925 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12926   if (!Data)
12927     return;
12928   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12929     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12930     skipInts(3);
12931   }
12932   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12933   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12934     Clauses[I] = readOMPClause();
12935   Data->setClauses(Clauses);
12936   if (Data->hasAssociatedStmt())
12937     Data->setAssociatedStmt(readStmt());
12938   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12939     Data->getChildren()[I] = readStmt();
12940 }
12941