1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Basic/OpenMPKinds.h"
14 #include "clang/Serialization/ASTRecordReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/AbstractTypeReader.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/AST/ASTUnresolvedSet.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/OpenMPClause.h"
35 #include "clang/AST/ODRHash.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/ExceptionSpecificationType.h"
47 #include "clang/Basic/FileManager.h"
48 #include "clang/Basic/FileSystemOptions.h"
49 #include "clang/Basic/IdentifierTable.h"
50 #include "clang/Basic/LLVM.h"
51 #include "clang/Basic/LangOptions.h"
52 #include "clang/Basic/Module.h"
53 #include "clang/Basic/ObjCRuntime.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/InMemoryModuleCache.h"
82 #include "clang/Serialization/ModuleFile.h"
83 #include "clang/Serialization/ModuleFileExtension.h"
84 #include "clang/Serialization/ModuleManager.h"
85 #include "clang/Serialization/PCHContainerOperations.h"
86 #include "clang/Serialization/SerializationDiagnostic.h"
87 #include "llvm/ADT/APFloat.h"
88 #include "llvm/ADT/APInt.h"
89 #include "llvm/ADT/APSInt.h"
90 #include "llvm/ADT/ArrayRef.h"
91 #include "llvm/ADT/DenseMap.h"
92 #include "llvm/ADT/FloatingPointMode.h"
93 #include "llvm/ADT/FoldingSet.h"
94 #include "llvm/ADT/Hashing.h"
95 #include "llvm/ADT/IntrusiveRefCntPtr.h"
96 #include "llvm/ADT/None.h"
97 #include "llvm/ADT/Optional.h"
98 #include "llvm/ADT/STLExtras.h"
99 #include "llvm/ADT/ScopeExit.h"
100 #include "llvm/ADT/SmallPtrSet.h"
101 #include "llvm/ADT/SmallString.h"
102 #include "llvm/ADT/SmallVector.h"
103 #include "llvm/ADT/StringExtras.h"
104 #include "llvm/ADT/StringMap.h"
105 #include "llvm/ADT/StringRef.h"
106 #include "llvm/ADT/Triple.h"
107 #include "llvm/ADT/iterator_range.h"
108 #include "llvm/Bitstream/BitstreamReader.h"
109 #include "llvm/Support/Casting.h"
110 #include "llvm/Support/Compiler.h"
111 #include "llvm/Support/Compression.h"
112 #include "llvm/Support/DJB.h"
113 #include "llvm/Support/Endian.h"
114 #include "llvm/Support/Error.h"
115 #include "llvm/Support/ErrorHandling.h"
116 #include "llvm/Support/FileSystem.h"
117 #include "llvm/Support/LEB128.h"
118 #include "llvm/Support/MemoryBuffer.h"
119 #include "llvm/Support/Path.h"
120 #include "llvm/Support/SaveAndRestore.h"
121 #include "llvm/Support/Timer.h"
122 #include "llvm/Support/VersionTuple.h"
123 #include "llvm/Support/raw_ostream.h"
124 #include <algorithm>
125 #include <cassert>
126 #include <cstddef>
127 #include <cstdint>
128 #include <cstdio>
129 #include <ctime>
130 #include <iterator>
131 #include <limits>
132 #include <map>
133 #include <memory>
134 #include <string>
135 #include <system_error>
136 #include <tuple>
137 #include <utility>
138 #include <vector>
139 
140 using namespace clang;
141 using namespace clang::serialization;
142 using namespace clang::serialization::reader;
143 using llvm::BitstreamCursor;
144 using llvm::RoundingMode;
145 
146 //===----------------------------------------------------------------------===//
147 // ChainedASTReaderListener implementation
148 //===----------------------------------------------------------------------===//
149 
150 bool
151 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
152   return First->ReadFullVersionInformation(FullVersion) ||
153          Second->ReadFullVersionInformation(FullVersion);
154 }
155 
156 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
157   First->ReadModuleName(ModuleName);
158   Second->ReadModuleName(ModuleName);
159 }
160 
161 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
162   First->ReadModuleMapFile(ModuleMapPath);
163   Second->ReadModuleMapFile(ModuleMapPath);
164 }
165 
166 bool
167 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
168                                               bool Complain,
169                                               bool AllowCompatibleDifferences) {
170   return First->ReadLanguageOptions(LangOpts, Complain,
171                                     AllowCompatibleDifferences) ||
172          Second->ReadLanguageOptions(LangOpts, Complain,
173                                      AllowCompatibleDifferences);
174 }
175 
176 bool ChainedASTReaderListener::ReadTargetOptions(
177     const TargetOptions &TargetOpts, bool Complain,
178     bool AllowCompatibleDifferences) {
179   return First->ReadTargetOptions(TargetOpts, Complain,
180                                   AllowCompatibleDifferences) ||
181          Second->ReadTargetOptions(TargetOpts, Complain,
182                                    AllowCompatibleDifferences);
183 }
184 
185 bool ChainedASTReaderListener::ReadDiagnosticOptions(
186     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
187   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
188          Second->ReadDiagnosticOptions(DiagOpts, Complain);
189 }
190 
191 bool
192 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
193                                                 bool Complain) {
194   return First->ReadFileSystemOptions(FSOpts, Complain) ||
195          Second->ReadFileSystemOptions(FSOpts, Complain);
196 }
197 
198 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
199     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
200     bool Complain) {
201   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
202                                         Complain) ||
203          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
204                                          Complain);
205 }
206 
207 bool ChainedASTReaderListener::ReadPreprocessorOptions(
208     const PreprocessorOptions &PPOpts, bool Complain,
209     std::string &SuggestedPredefines) {
210   return First->ReadPreprocessorOptions(PPOpts, Complain,
211                                         SuggestedPredefines) ||
212          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
213 }
214 
215 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
216                                            unsigned Value) {
217   First->ReadCounter(M, Value);
218   Second->ReadCounter(M, Value);
219 }
220 
221 bool ChainedASTReaderListener::needsInputFileVisitation() {
222   return First->needsInputFileVisitation() ||
223          Second->needsInputFileVisitation();
224 }
225 
226 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
227   return First->needsSystemInputFileVisitation() ||
228   Second->needsSystemInputFileVisitation();
229 }
230 
231 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
232                                                ModuleKind Kind) {
233   First->visitModuleFile(Filename, Kind);
234   Second->visitModuleFile(Filename, Kind);
235 }
236 
237 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
238                                               bool isSystem,
239                                               bool isOverridden,
240                                               bool isExplicitModule) {
241   bool Continue = false;
242   if (First->needsInputFileVisitation() &&
243       (!isSystem || First->needsSystemInputFileVisitation()))
244     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
245                                       isExplicitModule);
246   if (Second->needsInputFileVisitation() &&
247       (!isSystem || Second->needsSystemInputFileVisitation()))
248     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
249                                        isExplicitModule);
250   return Continue;
251 }
252 
253 void ChainedASTReaderListener::readModuleFileExtension(
254        const ModuleFileExtensionMetadata &Metadata) {
255   First->readModuleFileExtension(Metadata);
256   Second->readModuleFileExtension(Metadata);
257 }
258 
259 //===----------------------------------------------------------------------===//
260 // PCH validator implementation
261 //===----------------------------------------------------------------------===//
262 
263 ASTReaderListener::~ASTReaderListener() = default;
264 
265 /// Compare the given set of language options against an existing set of
266 /// language options.
267 ///
268 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
269 /// \param AllowCompatibleDifferences If true, differences between compatible
270 ///        language options will be permitted.
271 ///
272 /// \returns true if the languagae options mis-match, false otherwise.
273 static bool checkLanguageOptions(const LangOptions &LangOpts,
274                                  const LangOptions &ExistingLangOpts,
275                                  DiagnosticsEngine *Diags,
276                                  bool AllowCompatibleDifferences = true) {
277 #define LANGOPT(Name, Bits, Default, Description)                 \
278   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
279     if (Diags)                                                    \
280       Diags->Report(diag::err_pch_langopt_mismatch)               \
281         << Description << LangOpts.Name << ExistingLangOpts.Name; \
282     return true;                                                  \
283   }
284 
285 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
286   if (ExistingLangOpts.Name != LangOpts.Name) {           \
287     if (Diags)                                            \
288       Diags->Report(diag::err_pch_langopt_value_mismatch) \
289         << Description;                                   \
290     return true;                                          \
291   }
292 
293 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
294   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
295     if (Diags)                                                 \
296       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
297         << Description;                                        \
298     return true;                                               \
299   }
300 
301 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
302   if (!AllowCompatibleDifferences)                            \
303     LANGOPT(Name, Bits, Default, Description)
304 
305 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
306   if (!AllowCompatibleDifferences)                                 \
307     ENUM_LANGOPT(Name, Bits, Default, Description)
308 
309 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
310   if (!AllowCompatibleDifferences)                                 \
311     VALUE_LANGOPT(Name, Bits, Default, Description)
312 
313 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
314 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
315 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
316 #include "clang/Basic/LangOptions.def"
317 
318   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
321     return true;
322   }
323 
324   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
325     if (Diags)
326       Diags->Report(diag::err_pch_langopt_value_mismatch)
327       << "target Objective-C runtime";
328     return true;
329   }
330 
331   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
332       LangOpts.CommentOpts.BlockCommandNames) {
333     if (Diags)
334       Diags->Report(diag::err_pch_langopt_value_mismatch)
335         << "block command names";
336     return true;
337   }
338 
339   // Sanitizer feature mismatches are treated as compatible differences. If
340   // compatible differences aren't allowed, we still only want to check for
341   // mismatches of non-modular sanitizers (the only ones which can affect AST
342   // generation).
343   if (!AllowCompatibleDifferences) {
344     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
345     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
346     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
347     ExistingSanitizers.clear(ModularSanitizers);
348     ImportedSanitizers.clear(ModularSanitizers);
349     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
350       const std::string Flag = "-fsanitize=";
351       if (Diags) {
352 #define SANITIZER(NAME, ID)                                                    \
353   {                                                                            \
354     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
355     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
356     if (InExistingModule != InImportedModule)                                  \
357       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
358           << InExistingModule << (Flag + NAME);                                \
359   }
360 #include "clang/Basic/Sanitizers.def"
361       }
362       return true;
363     }
364   }
365 
366   return false;
367 }
368 
369 /// Compare the given set of target options against an existing set of
370 /// target options.
371 ///
372 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
373 ///
374 /// \returns true if the target options mis-match, false otherwise.
375 static bool checkTargetOptions(const TargetOptions &TargetOpts,
376                                const TargetOptions &ExistingTargetOpts,
377                                DiagnosticsEngine *Diags,
378                                bool AllowCompatibleDifferences = true) {
379 #define CHECK_TARGET_OPT(Field, Name)                             \
380   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
381     if (Diags)                                                    \
382       Diags->Report(diag::err_pch_targetopt_mismatch)             \
383         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
384     return true;                                                  \
385   }
386 
387   // The triple and ABI must match exactly.
388   CHECK_TARGET_OPT(Triple, "target");
389   CHECK_TARGET_OPT(ABI, "target ABI");
390 
391   // We can tolerate different CPUs in many cases, notably when one CPU
392   // supports a strict superset of another. When allowing compatible
393   // differences skip this check.
394   if (!AllowCompatibleDifferences) {
395     CHECK_TARGET_OPT(CPU, "target CPU");
396     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
397   }
398 
399 #undef CHECK_TARGET_OPT
400 
401   // Compare feature sets.
402   SmallVector<StringRef, 4> ExistingFeatures(
403                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
404                                              ExistingTargetOpts.FeaturesAsWritten.end());
405   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
406                                          TargetOpts.FeaturesAsWritten.end());
407   llvm::sort(ExistingFeatures);
408   llvm::sort(ReadFeatures);
409 
410   // We compute the set difference in both directions explicitly so that we can
411   // diagnose the differences differently.
412   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
413   std::set_difference(
414       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
415       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
416   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
417                       ExistingFeatures.begin(), ExistingFeatures.end(),
418                       std::back_inserter(UnmatchedReadFeatures));
419 
420   // If we are allowing compatible differences and the read feature set is
421   // a strict subset of the existing feature set, there is nothing to diagnose.
422   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
423     return false;
424 
425   if (Diags) {
426     for (StringRef Feature : UnmatchedReadFeatures)
427       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428           << /* is-existing-feature */ false << Feature;
429     for (StringRef Feature : UnmatchedExistingFeatures)
430       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
431           << /* is-existing-feature */ true << Feature;
432   }
433 
434   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
435 }
436 
437 bool
438 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
439                                   bool Complain,
440                                   bool AllowCompatibleDifferences) {
441   const LangOptions &ExistingLangOpts = PP.getLangOpts();
442   return checkLanguageOptions(LangOpts, ExistingLangOpts,
443                               Complain ? &Reader.Diags : nullptr,
444                               AllowCompatibleDifferences);
445 }
446 
447 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
448                                      bool Complain,
449                                      bool AllowCompatibleDifferences) {
450   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
451   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
452                             Complain ? &Reader.Diags : nullptr,
453                             AllowCompatibleDifferences);
454 }
455 
456 namespace {
457 
458 using MacroDefinitionsMap =
459     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
460 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
461 
462 } // namespace
463 
464 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
465                                          DiagnosticsEngine &Diags,
466                                          bool Complain) {
467   using Level = DiagnosticsEngine::Level;
468 
469   // Check current mappings for new -Werror mappings, and the stored mappings
470   // for cases that were explicitly mapped to *not* be errors that are now
471   // errors because of options like -Werror.
472   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
473 
474   for (DiagnosticsEngine *MappingSource : MappingSources) {
475     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
476       diag::kind DiagID = DiagIDMappingPair.first;
477       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
478       if (CurLevel < DiagnosticsEngine::Error)
479         continue; // not significant
480       Level StoredLevel =
481           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
482       if (StoredLevel < DiagnosticsEngine::Error) {
483         if (Complain)
484           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
485               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
486         return true;
487       }
488     }
489   }
490 
491   return false;
492 }
493 
494 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
495   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
496   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
497     return true;
498   return Ext >= diag::Severity::Error;
499 }
500 
501 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
502                                     DiagnosticsEngine &Diags,
503                                     bool IsSystem, bool Complain) {
504   // Top-level options
505   if (IsSystem) {
506     if (Diags.getSuppressSystemWarnings())
507       return false;
508     // If -Wsystem-headers was not enabled before, be conservative
509     if (StoredDiags.getSuppressSystemWarnings()) {
510       if (Complain)
511         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
512       return true;
513     }
514   }
515 
516   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
517     if (Complain)
518       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
519     return true;
520   }
521 
522   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
523       !StoredDiags.getEnableAllWarnings()) {
524     if (Complain)
525       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
526     return true;
527   }
528 
529   if (isExtHandlingFromDiagsError(Diags) &&
530       !isExtHandlingFromDiagsError(StoredDiags)) {
531     if (Complain)
532       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
533     return true;
534   }
535 
536   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
537 }
538 
539 /// Return the top import module if it is implicit, nullptr otherwise.
540 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
541                                           Preprocessor &PP) {
542   // If the original import came from a file explicitly generated by the user,
543   // don't check the diagnostic mappings.
544   // FIXME: currently this is approximated by checking whether this is not a
545   // module import of an implicitly-loaded module file.
546   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
547   // the transitive closure of its imports, since unrelated modules cannot be
548   // imported until after this module finishes validation.
549   ModuleFile *TopImport = &*ModuleMgr.rbegin();
550   while (!TopImport->ImportedBy.empty())
551     TopImport = TopImport->ImportedBy[0];
552   if (TopImport->Kind != MK_ImplicitModule)
553     return nullptr;
554 
555   StringRef ModuleName = TopImport->ModuleName;
556   assert(!ModuleName.empty() && "diagnostic options read before module name");
557 
558   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
559   assert(M && "missing module");
560   return M;
561 }
562 
563 bool PCHValidator::ReadDiagnosticOptions(
564     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
565   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
566   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
567   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
568       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
569   // This should never fail, because we would have processed these options
570   // before writing them to an ASTFile.
571   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
572 
573   ModuleManager &ModuleMgr = Reader.getModuleManager();
574   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
575 
576   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
577   if (!TopM)
578     return false;
579 
580   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
581   // contains the union of their flags.
582   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
583                                  Complain);
584 }
585 
586 /// Collect the macro definitions provided by the given preprocessor
587 /// options.
588 static void
589 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
590                         MacroDefinitionsMap &Macros,
591                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
592   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
593     StringRef Macro = PPOpts.Macros[I].first;
594     bool IsUndef = PPOpts.Macros[I].second;
595 
596     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
597     StringRef MacroName = MacroPair.first;
598     StringRef MacroBody = MacroPair.second;
599 
600     // For an #undef'd macro, we only care about the name.
601     if (IsUndef) {
602       if (MacroNames && !Macros.count(MacroName))
603         MacroNames->push_back(MacroName);
604 
605       Macros[MacroName] = std::make_pair("", true);
606       continue;
607     }
608 
609     // For a #define'd macro, figure out the actual definition.
610     if (MacroName.size() == Macro.size())
611       MacroBody = "1";
612     else {
613       // Note: GCC drops anything following an end-of-line character.
614       StringRef::size_type End = MacroBody.find_first_of("\n\r");
615       MacroBody = MacroBody.substr(0, End);
616     }
617 
618     if (MacroNames && !Macros.count(MacroName))
619       MacroNames->push_back(MacroName);
620     Macros[MacroName] = std::make_pair(MacroBody, false);
621   }
622 }
623 
624 /// Check the preprocessor options deserialized from the control block
625 /// against the preprocessor options in an existing preprocessor.
626 ///
627 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
628 /// \param Validate If true, validate preprocessor options. If false, allow
629 ///        macros defined by \p ExistingPPOpts to override those defined by
630 ///        \p PPOpts in SuggestedPredefines.
631 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
632                                      const PreprocessorOptions &ExistingPPOpts,
633                                      DiagnosticsEngine *Diags,
634                                      FileManager &FileMgr,
635                                      std::string &SuggestedPredefines,
636                                      const LangOptions &LangOpts,
637                                      bool Validate = true) {
638   // Check macro definitions.
639   MacroDefinitionsMap ASTFileMacros;
640   collectMacroDefinitions(PPOpts, ASTFileMacros);
641   MacroDefinitionsMap ExistingMacros;
642   SmallVector<StringRef, 4> ExistingMacroNames;
643   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
644 
645   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
646     // Dig out the macro definition in the existing preprocessor options.
647     StringRef MacroName = ExistingMacroNames[I];
648     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
649 
650     // Check whether we know anything about this macro name or not.
651     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
652         ASTFileMacros.find(MacroName);
653     if (!Validate || Known == ASTFileMacros.end()) {
654       // FIXME: Check whether this identifier was referenced anywhere in the
655       // AST file. If so, we should reject the AST file. Unfortunately, this
656       // information isn't in the control block. What shall we do about it?
657 
658       if (Existing.second) {
659         SuggestedPredefines += "#undef ";
660         SuggestedPredefines += MacroName.str();
661         SuggestedPredefines += '\n';
662       } else {
663         SuggestedPredefines += "#define ";
664         SuggestedPredefines += MacroName.str();
665         SuggestedPredefines += ' ';
666         SuggestedPredefines += Existing.first.str();
667         SuggestedPredefines += '\n';
668       }
669       continue;
670     }
671 
672     // If the macro was defined in one but undef'd in the other, we have a
673     // conflict.
674     if (Existing.second != Known->second.second) {
675       if (Diags) {
676         Diags->Report(diag::err_pch_macro_def_undef)
677           << MacroName << Known->second.second;
678       }
679       return true;
680     }
681 
682     // If the macro was #undef'd in both, or if the macro bodies are identical,
683     // it's fine.
684     if (Existing.second || Existing.first == Known->second.first)
685       continue;
686 
687     // The macro bodies differ; complain.
688     if (Diags) {
689       Diags->Report(diag::err_pch_macro_def_conflict)
690         << MacroName << Known->second.first << Existing.first;
691     }
692     return true;
693   }
694 
695   // Check whether we're using predefines.
696   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
697     if (Diags) {
698       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
699     }
700     return true;
701   }
702 
703   // Detailed record is important since it is used for the module cache hash.
704   if (LangOpts.Modules &&
705       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
706     if (Diags) {
707       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
708     }
709     return true;
710   }
711 
712   // Compute the #include and #include_macros lines we need.
713   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
714     StringRef File = ExistingPPOpts.Includes[I];
715 
716     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
717         !ExistingPPOpts.PCHThroughHeader.empty()) {
718       // In case the through header is an include, we must add all the includes
719       // to the predefines so the start point can be determined.
720       SuggestedPredefines += "#include \"";
721       SuggestedPredefines += File;
722       SuggestedPredefines += "\"\n";
723       continue;
724     }
725 
726     if (File == ExistingPPOpts.ImplicitPCHInclude)
727       continue;
728 
729     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
730           != PPOpts.Includes.end())
731       continue;
732 
733     SuggestedPredefines += "#include \"";
734     SuggestedPredefines += File;
735     SuggestedPredefines += "\"\n";
736   }
737 
738   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
739     StringRef File = ExistingPPOpts.MacroIncludes[I];
740     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
741                   File)
742         != PPOpts.MacroIncludes.end())
743       continue;
744 
745     SuggestedPredefines += "#__include_macros \"";
746     SuggestedPredefines += File;
747     SuggestedPredefines += "\"\n##\n";
748   }
749 
750   return false;
751 }
752 
753 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
754                                            bool Complain,
755                                            std::string &SuggestedPredefines) {
756   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
757 
758   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
759                                   Complain? &Reader.Diags : nullptr,
760                                   PP.getFileManager(),
761                                   SuggestedPredefines,
762                                   PP.getLangOpts());
763 }
764 
765 bool SimpleASTReaderListener::ReadPreprocessorOptions(
766                                   const PreprocessorOptions &PPOpts,
767                                   bool Complain,
768                                   std::string &SuggestedPredefines) {
769   return checkPreprocessorOptions(PPOpts,
770                                   PP.getPreprocessorOpts(),
771                                   nullptr,
772                                   PP.getFileManager(),
773                                   SuggestedPredefines,
774                                   PP.getLangOpts(),
775                                   false);
776 }
777 
778 /// Check the header search options deserialized from the control block
779 /// against the header search options in an existing preprocessor.
780 ///
781 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
782 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
783                                      StringRef SpecificModuleCachePath,
784                                      StringRef ExistingModuleCachePath,
785                                      DiagnosticsEngine *Diags,
786                                      const LangOptions &LangOpts,
787                                      const PreprocessorOptions &PPOpts) {
788   if (LangOpts.Modules) {
789     if (SpecificModuleCachePath != ExistingModuleCachePath &&
790         !PPOpts.AllowPCHWithDifferentModulesCachePath) {
791       if (Diags)
792         Diags->Report(diag::err_pch_modulecache_mismatch)
793           << SpecificModuleCachePath << ExistingModuleCachePath;
794       return true;
795     }
796   }
797 
798   return false;
799 }
800 
801 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
802                                            StringRef SpecificModuleCachePath,
803                                            bool Complain) {
804   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
805                                   PP.getHeaderSearchInfo().getModuleCachePath(),
806                                   Complain ? &Reader.Diags : nullptr,
807                                   PP.getLangOpts(), PP.getPreprocessorOpts());
808 }
809 
810 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
811   PP.setCounterValue(Value);
812 }
813 
814 //===----------------------------------------------------------------------===//
815 // AST reader implementation
816 //===----------------------------------------------------------------------===//
817 
818 static uint64_t readULEB(const unsigned char *&P) {
819   unsigned Length = 0;
820   const char *Error = nullptr;
821 
822   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
823   if (Error)
824     llvm::report_fatal_error(Error);
825   P += Length;
826   return Val;
827 }
828 
829 /// Read ULEB-encoded key length and data length.
830 static std::pair<unsigned, unsigned>
831 readULEBKeyDataLength(const unsigned char *&P) {
832   unsigned KeyLen = readULEB(P);
833   if ((unsigned)KeyLen != KeyLen)
834     llvm::report_fatal_error("key too large");
835 
836   unsigned DataLen = readULEB(P);
837   if ((unsigned)DataLen != DataLen)
838     llvm::report_fatal_error("data too large");
839 
840   return std::make_pair(KeyLen, DataLen);
841 }
842 
843 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
844                                            bool TakeOwnership) {
845   DeserializationListener = Listener;
846   OwnsDeserializationListener = TakeOwnership;
847 }
848 
849 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
850   return serialization::ComputeHash(Sel);
851 }
852 
853 std::pair<unsigned, unsigned>
854 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
855   return readULEBKeyDataLength(d);
856 }
857 
858 ASTSelectorLookupTrait::internal_key_type
859 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
860   using namespace llvm::support;
861 
862   SelectorTable &SelTable = Reader.getContext().Selectors;
863   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
864   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
865       F, endian::readNext<uint32_t, little, unaligned>(d));
866   if (N == 0)
867     return SelTable.getNullarySelector(FirstII);
868   else if (N == 1)
869     return SelTable.getUnarySelector(FirstII);
870 
871   SmallVector<IdentifierInfo *, 16> Args;
872   Args.push_back(FirstII);
873   for (unsigned I = 1; I != N; ++I)
874     Args.push_back(Reader.getLocalIdentifier(
875         F, endian::readNext<uint32_t, little, unaligned>(d)));
876 
877   return SelTable.getSelector(N, Args.data());
878 }
879 
880 ASTSelectorLookupTrait::data_type
881 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
882                                  unsigned DataLen) {
883   using namespace llvm::support;
884 
885   data_type Result;
886 
887   Result.ID = Reader.getGlobalSelectorID(
888       F, endian::readNext<uint32_t, little, unaligned>(d));
889   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
890   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
891   Result.InstanceBits = FullInstanceBits & 0x3;
892   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
893   Result.FactoryBits = FullFactoryBits & 0x3;
894   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
895   unsigned NumInstanceMethods = FullInstanceBits >> 3;
896   unsigned NumFactoryMethods = FullFactoryBits >> 3;
897 
898   // Load instance methods
899   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
900     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
901             F, endian::readNext<uint32_t, little, unaligned>(d)))
902       Result.Instance.push_back(Method);
903   }
904 
905   // Load factory methods
906   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
907     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
908             F, endian::readNext<uint32_t, little, unaligned>(d)))
909       Result.Factory.push_back(Method);
910   }
911 
912   return Result;
913 }
914 
915 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
916   return llvm::djbHash(a);
917 }
918 
919 std::pair<unsigned, unsigned>
920 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
921   return readULEBKeyDataLength(d);
922 }
923 
924 ASTIdentifierLookupTraitBase::internal_key_type
925 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
926   assert(n >= 2 && d[n-1] == '\0');
927   return StringRef((const char*) d, n-1);
928 }
929 
930 /// Whether the given identifier is "interesting".
931 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
932                                     bool IsModule) {
933   return II.hadMacroDefinition() || II.isPoisoned() ||
934          (!IsModule && II.getObjCOrBuiltinID()) ||
935          II.hasRevertedTokenIDToIdentifier() ||
936          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
937           II.getFETokenInfo());
938 }
939 
940 static bool readBit(unsigned &Bits) {
941   bool Value = Bits & 0x1;
942   Bits >>= 1;
943   return Value;
944 }
945 
946 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
947   using namespace llvm::support;
948 
949   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
950   return Reader.getGlobalIdentifierID(F, RawID >> 1);
951 }
952 
953 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
954   if (!II.isFromAST()) {
955     II.setIsFromAST();
956     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
957     if (isInterestingIdentifier(Reader, II, IsModule))
958       II.setChangedSinceDeserialization();
959   }
960 }
961 
962 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
963                                                    const unsigned char* d,
964                                                    unsigned DataLen) {
965   using namespace llvm::support;
966 
967   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
968   bool IsInteresting = RawID & 0x01;
969 
970   // Wipe out the "is interesting" bit.
971   RawID = RawID >> 1;
972 
973   // Build the IdentifierInfo and link the identifier ID with it.
974   IdentifierInfo *II = KnownII;
975   if (!II) {
976     II = &Reader.getIdentifierTable().getOwn(k);
977     KnownII = II;
978   }
979   markIdentifierFromAST(Reader, *II);
980   Reader.markIdentifierUpToDate(II);
981 
982   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
983   if (!IsInteresting) {
984     // For uninteresting identifiers, there's nothing else to do. Just notify
985     // the reader that we've finished loading this identifier.
986     Reader.SetIdentifierInfo(ID, II);
987     return II;
988   }
989 
990   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
991   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
992   bool CPlusPlusOperatorKeyword = readBit(Bits);
993   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
994   bool Poisoned = readBit(Bits);
995   bool ExtensionToken = readBit(Bits);
996   bool HadMacroDefinition = readBit(Bits);
997 
998   assert(Bits == 0 && "Extra bits in the identifier?");
999   DataLen -= 8;
1000 
1001   // Set or check the various bits in the IdentifierInfo structure.
1002   // Token IDs are read-only.
1003   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1004     II->revertTokenIDToIdentifier();
1005   if (!F.isModule())
1006     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1007   assert(II->isExtensionToken() == ExtensionToken &&
1008          "Incorrect extension token flag");
1009   (void)ExtensionToken;
1010   if (Poisoned)
1011     II->setIsPoisoned(true);
1012   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1013          "Incorrect C++ operator keyword flag");
1014   (void)CPlusPlusOperatorKeyword;
1015 
1016   // If this identifier is a macro, deserialize the macro
1017   // definition.
1018   if (HadMacroDefinition) {
1019     uint32_t MacroDirectivesOffset =
1020         endian::readNext<uint32_t, little, unaligned>(d);
1021     DataLen -= 4;
1022 
1023     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1024   }
1025 
1026   Reader.SetIdentifierInfo(ID, II);
1027 
1028   // Read all of the declarations visible at global scope with this
1029   // name.
1030   if (DataLen > 0) {
1031     SmallVector<uint32_t, 4> DeclIDs;
1032     for (; DataLen > 0; DataLen -= 4)
1033       DeclIDs.push_back(Reader.getGlobalDeclID(
1034           F, endian::readNext<uint32_t, little, unaligned>(d)));
1035     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1036   }
1037 
1038   return II;
1039 }
1040 
1041 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1042     : Kind(Name.getNameKind()) {
1043   switch (Kind) {
1044   case DeclarationName::Identifier:
1045     Data = (uint64_t)Name.getAsIdentifierInfo();
1046     break;
1047   case DeclarationName::ObjCZeroArgSelector:
1048   case DeclarationName::ObjCOneArgSelector:
1049   case DeclarationName::ObjCMultiArgSelector:
1050     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1051     break;
1052   case DeclarationName::CXXOperatorName:
1053     Data = Name.getCXXOverloadedOperator();
1054     break;
1055   case DeclarationName::CXXLiteralOperatorName:
1056     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1057     break;
1058   case DeclarationName::CXXDeductionGuideName:
1059     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1060                ->getDeclName().getAsIdentifierInfo();
1061     break;
1062   case DeclarationName::CXXConstructorName:
1063   case DeclarationName::CXXDestructorName:
1064   case DeclarationName::CXXConversionFunctionName:
1065   case DeclarationName::CXXUsingDirective:
1066     Data = 0;
1067     break;
1068   }
1069 }
1070 
1071 unsigned DeclarationNameKey::getHash() const {
1072   llvm::FoldingSetNodeID ID;
1073   ID.AddInteger(Kind);
1074 
1075   switch (Kind) {
1076   case DeclarationName::Identifier:
1077   case DeclarationName::CXXLiteralOperatorName:
1078   case DeclarationName::CXXDeductionGuideName:
1079     ID.AddString(((IdentifierInfo*)Data)->getName());
1080     break;
1081   case DeclarationName::ObjCZeroArgSelector:
1082   case DeclarationName::ObjCOneArgSelector:
1083   case DeclarationName::ObjCMultiArgSelector:
1084     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1085     break;
1086   case DeclarationName::CXXOperatorName:
1087     ID.AddInteger((OverloadedOperatorKind)Data);
1088     break;
1089   case DeclarationName::CXXConstructorName:
1090   case DeclarationName::CXXDestructorName:
1091   case DeclarationName::CXXConversionFunctionName:
1092   case DeclarationName::CXXUsingDirective:
1093     break;
1094   }
1095 
1096   return ID.ComputeHash();
1097 }
1098 
1099 ModuleFile *
1100 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1101   using namespace llvm::support;
1102 
1103   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1104   return Reader.getLocalModuleFile(F, ModuleFileID);
1105 }
1106 
1107 std::pair<unsigned, unsigned>
1108 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1109   return readULEBKeyDataLength(d);
1110 }
1111 
1112 ASTDeclContextNameLookupTrait::internal_key_type
1113 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1114   using namespace llvm::support;
1115 
1116   auto Kind = (DeclarationName::NameKind)*d++;
1117   uint64_t Data;
1118   switch (Kind) {
1119   case DeclarationName::Identifier:
1120   case DeclarationName::CXXLiteralOperatorName:
1121   case DeclarationName::CXXDeductionGuideName:
1122     Data = (uint64_t)Reader.getLocalIdentifier(
1123         F, endian::readNext<uint32_t, little, unaligned>(d));
1124     break;
1125   case DeclarationName::ObjCZeroArgSelector:
1126   case DeclarationName::ObjCOneArgSelector:
1127   case DeclarationName::ObjCMultiArgSelector:
1128     Data =
1129         (uint64_t)Reader.getLocalSelector(
1130                              F, endian::readNext<uint32_t, little, unaligned>(
1131                                     d)).getAsOpaquePtr();
1132     break;
1133   case DeclarationName::CXXOperatorName:
1134     Data = *d++; // OverloadedOperatorKind
1135     break;
1136   case DeclarationName::CXXConstructorName:
1137   case DeclarationName::CXXDestructorName:
1138   case DeclarationName::CXXConversionFunctionName:
1139   case DeclarationName::CXXUsingDirective:
1140     Data = 0;
1141     break;
1142   }
1143 
1144   return DeclarationNameKey(Kind, Data);
1145 }
1146 
1147 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1148                                                  const unsigned char *d,
1149                                                  unsigned DataLen,
1150                                                  data_type_builder &Val) {
1151   using namespace llvm::support;
1152 
1153   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1154     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1155     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1156   }
1157 }
1158 
1159 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1160                                               BitstreamCursor &Cursor,
1161                                               uint64_t Offset,
1162                                               DeclContext *DC) {
1163   assert(Offset != 0);
1164 
1165   SavedStreamPosition SavedPosition(Cursor);
1166   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1167     Error(std::move(Err));
1168     return true;
1169   }
1170 
1171   RecordData Record;
1172   StringRef Blob;
1173   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1174   if (!MaybeCode) {
1175     Error(MaybeCode.takeError());
1176     return true;
1177   }
1178   unsigned Code = MaybeCode.get();
1179 
1180   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1181   if (!MaybeRecCode) {
1182     Error(MaybeRecCode.takeError());
1183     return true;
1184   }
1185   unsigned RecCode = MaybeRecCode.get();
1186   if (RecCode != DECL_CONTEXT_LEXICAL) {
1187     Error("Expected lexical block");
1188     return true;
1189   }
1190 
1191   assert(!isa<TranslationUnitDecl>(DC) &&
1192          "expected a TU_UPDATE_LEXICAL record for TU");
1193   // If we are handling a C++ class template instantiation, we can see multiple
1194   // lexical updates for the same record. It's important that we select only one
1195   // of them, so that field numbering works properly. Just pick the first one we
1196   // see.
1197   auto &Lex = LexicalDecls[DC];
1198   if (!Lex.first) {
1199     Lex = std::make_pair(
1200         &M, llvm::makeArrayRef(
1201                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1202                     Blob.data()),
1203                 Blob.size() / 4));
1204   }
1205   DC->setHasExternalLexicalStorage(true);
1206   return false;
1207 }
1208 
1209 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1210                                               BitstreamCursor &Cursor,
1211                                               uint64_t Offset,
1212                                               DeclID ID) {
1213   assert(Offset != 0);
1214 
1215   SavedStreamPosition SavedPosition(Cursor);
1216   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1217     Error(std::move(Err));
1218     return true;
1219   }
1220 
1221   RecordData Record;
1222   StringRef Blob;
1223   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1224   if (!MaybeCode) {
1225     Error(MaybeCode.takeError());
1226     return true;
1227   }
1228   unsigned Code = MaybeCode.get();
1229 
1230   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1231   if (!MaybeRecCode) {
1232     Error(MaybeRecCode.takeError());
1233     return true;
1234   }
1235   unsigned RecCode = MaybeRecCode.get();
1236   if (RecCode != DECL_CONTEXT_VISIBLE) {
1237     Error("Expected visible lookup table block");
1238     return true;
1239   }
1240 
1241   // We can't safely determine the primary context yet, so delay attaching the
1242   // lookup table until we're done with recursive deserialization.
1243   auto *Data = (const unsigned char*)Blob.data();
1244   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1245   return false;
1246 }
1247 
1248 void ASTReader::Error(StringRef Msg) const {
1249   Error(diag::err_fe_pch_malformed, Msg);
1250   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1251       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1252     Diag(diag::note_module_cache_path)
1253       << PP.getHeaderSearchInfo().getModuleCachePath();
1254   }
1255 }
1256 
1257 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1258                       StringRef Arg3) const {
1259   if (Diags.isDiagnosticInFlight())
1260     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1261   else
1262     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1263 }
1264 
1265 void ASTReader::Error(llvm::Error &&Err) const {
1266   Error(toString(std::move(Err)));
1267 }
1268 
1269 //===----------------------------------------------------------------------===//
1270 // Source Manager Deserialization
1271 //===----------------------------------------------------------------------===//
1272 
1273 /// Read the line table in the source manager block.
1274 /// \returns true if there was an error.
1275 bool ASTReader::ParseLineTable(ModuleFile &F,
1276                                const RecordData &Record) {
1277   unsigned Idx = 0;
1278   LineTableInfo &LineTable = SourceMgr.getLineTable();
1279 
1280   // Parse the file names
1281   std::map<int, int> FileIDs;
1282   FileIDs[-1] = -1; // For unspecified filenames.
1283   for (unsigned I = 0; Record[Idx]; ++I) {
1284     // Extract the file name
1285     auto Filename = ReadPath(F, Record, Idx);
1286     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1287   }
1288   ++Idx;
1289 
1290   // Parse the line entries
1291   std::vector<LineEntry> Entries;
1292   while (Idx < Record.size()) {
1293     int FID = Record[Idx++];
1294     assert(FID >= 0 && "Serialized line entries for non-local file.");
1295     // Remap FileID from 1-based old view.
1296     FID += F.SLocEntryBaseID - 1;
1297 
1298     // Extract the line entries
1299     unsigned NumEntries = Record[Idx++];
1300     assert(NumEntries && "no line entries for file ID");
1301     Entries.clear();
1302     Entries.reserve(NumEntries);
1303     for (unsigned I = 0; I != NumEntries; ++I) {
1304       unsigned FileOffset = Record[Idx++];
1305       unsigned LineNo = Record[Idx++];
1306       int FilenameID = FileIDs[Record[Idx++]];
1307       SrcMgr::CharacteristicKind FileKind
1308         = (SrcMgr::CharacteristicKind)Record[Idx++];
1309       unsigned IncludeOffset = Record[Idx++];
1310       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1311                                        FileKind, IncludeOffset));
1312     }
1313     LineTable.AddEntry(FileID::get(FID), Entries);
1314   }
1315 
1316   return false;
1317 }
1318 
1319 /// Read a source manager block
1320 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1321   using namespace SrcMgr;
1322 
1323   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1324 
1325   // Set the source-location entry cursor to the current position in
1326   // the stream. This cursor will be used to read the contents of the
1327   // source manager block initially, and then lazily read
1328   // source-location entries as needed.
1329   SLocEntryCursor = F.Stream;
1330 
1331   // The stream itself is going to skip over the source manager block.
1332   if (llvm::Error Err = F.Stream.SkipBlock()) {
1333     Error(std::move(Err));
1334     return true;
1335   }
1336 
1337   // Enter the source manager block.
1338   if (llvm::Error Err =
1339           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1340     Error(std::move(Err));
1341     return true;
1342   }
1343   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1344 
1345   RecordData Record;
1346   while (true) {
1347     Expected<llvm::BitstreamEntry> MaybeE =
1348         SLocEntryCursor.advanceSkippingSubblocks();
1349     if (!MaybeE) {
1350       Error(MaybeE.takeError());
1351       return true;
1352     }
1353     llvm::BitstreamEntry E = MaybeE.get();
1354 
1355     switch (E.Kind) {
1356     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1357     case llvm::BitstreamEntry::Error:
1358       Error("malformed block record in AST file");
1359       return true;
1360     case llvm::BitstreamEntry::EndBlock:
1361       return false;
1362     case llvm::BitstreamEntry::Record:
1363       // The interesting case.
1364       break;
1365     }
1366 
1367     // Read a record.
1368     Record.clear();
1369     StringRef Blob;
1370     Expected<unsigned> MaybeRecord =
1371         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1372     if (!MaybeRecord) {
1373       Error(MaybeRecord.takeError());
1374       return true;
1375     }
1376     switch (MaybeRecord.get()) {
1377     default:  // Default behavior: ignore.
1378       break;
1379 
1380     case SM_SLOC_FILE_ENTRY:
1381     case SM_SLOC_BUFFER_ENTRY:
1382     case SM_SLOC_EXPANSION_ENTRY:
1383       // Once we hit one of the source location entries, we're done.
1384       return false;
1385     }
1386   }
1387 }
1388 
1389 /// If a header file is not found at the path that we expect it to be
1390 /// and the PCH file was moved from its original location, try to resolve the
1391 /// file by assuming that header+PCH were moved together and the header is in
1392 /// the same place relative to the PCH.
1393 static std::string
1394 resolveFileRelativeToOriginalDir(const std::string &Filename,
1395                                  const std::string &OriginalDir,
1396                                  const std::string &CurrDir) {
1397   assert(OriginalDir != CurrDir &&
1398          "No point trying to resolve the file if the PCH dir didn't change");
1399 
1400   using namespace llvm::sys;
1401 
1402   SmallString<128> filePath(Filename);
1403   fs::make_absolute(filePath);
1404   assert(path::is_absolute(OriginalDir));
1405   SmallString<128> currPCHPath(CurrDir);
1406 
1407   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1408                        fileDirE = path::end(path::parent_path(filePath));
1409   path::const_iterator origDirI = path::begin(OriginalDir),
1410                        origDirE = path::end(OriginalDir);
1411   // Skip the common path components from filePath and OriginalDir.
1412   while (fileDirI != fileDirE && origDirI != origDirE &&
1413          *fileDirI == *origDirI) {
1414     ++fileDirI;
1415     ++origDirI;
1416   }
1417   for (; origDirI != origDirE; ++origDirI)
1418     path::append(currPCHPath, "..");
1419   path::append(currPCHPath, fileDirI, fileDirE);
1420   path::append(currPCHPath, path::filename(Filename));
1421   return std::string(currPCHPath.str());
1422 }
1423 
1424 bool ASTReader::ReadSLocEntry(int ID) {
1425   if (ID == 0)
1426     return false;
1427 
1428   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1429     Error("source location entry ID out-of-range for AST file");
1430     return true;
1431   }
1432 
1433   // Local helper to read the (possibly-compressed) buffer data following the
1434   // entry record.
1435   auto ReadBuffer = [this](
1436       BitstreamCursor &SLocEntryCursor,
1437       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1438     RecordData Record;
1439     StringRef Blob;
1440     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1441     if (!MaybeCode) {
1442       Error(MaybeCode.takeError());
1443       return nullptr;
1444     }
1445     unsigned Code = MaybeCode.get();
1446 
1447     Expected<unsigned> MaybeRecCode =
1448         SLocEntryCursor.readRecord(Code, Record, &Blob);
1449     if (!MaybeRecCode) {
1450       Error(MaybeRecCode.takeError());
1451       return nullptr;
1452     }
1453     unsigned RecCode = MaybeRecCode.get();
1454 
1455     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1456       if (!llvm::zlib::isAvailable()) {
1457         Error("zlib is not available");
1458         return nullptr;
1459       }
1460       SmallString<0> Uncompressed;
1461       if (llvm::Error E =
1462               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1463         Error("could not decompress embedded file contents: " +
1464               llvm::toString(std::move(E)));
1465         return nullptr;
1466       }
1467       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1468     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1469       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1470     } else {
1471       Error("AST record has invalid code");
1472       return nullptr;
1473     }
1474   };
1475 
1476   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1477   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1478           F->SLocEntryOffsetsBase +
1479           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1480     Error(std::move(Err));
1481     return true;
1482   }
1483 
1484   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1485   unsigned BaseOffset = F->SLocEntryBaseOffset;
1486 
1487   ++NumSLocEntriesRead;
1488   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1489   if (!MaybeEntry) {
1490     Error(MaybeEntry.takeError());
1491     return true;
1492   }
1493   llvm::BitstreamEntry Entry = MaybeEntry.get();
1494 
1495   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1496     Error("incorrectly-formatted source location entry in AST file");
1497     return true;
1498   }
1499 
1500   RecordData Record;
1501   StringRef Blob;
1502   Expected<unsigned> MaybeSLOC =
1503       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1504   if (!MaybeSLOC) {
1505     Error(MaybeSLOC.takeError());
1506     return true;
1507   }
1508   switch (MaybeSLOC.get()) {
1509   default:
1510     Error("incorrectly-formatted source location entry in AST file");
1511     return true;
1512 
1513   case SM_SLOC_FILE_ENTRY: {
1514     // We will detect whether a file changed and return 'Failure' for it, but
1515     // we will also try to fail gracefully by setting up the SLocEntry.
1516     unsigned InputID = Record[4];
1517     InputFile IF = getInputFile(*F, InputID);
1518     Optional<FileEntryRef> File = IF.getFile();
1519     bool OverriddenBuffer = IF.isOverridden();
1520 
1521     // Note that we only check if a File was returned. If it was out-of-date
1522     // we have complained but we will continue creating a FileID to recover
1523     // gracefully.
1524     if (!File)
1525       return true;
1526 
1527     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1528     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1529       // This is the module's main file.
1530       IncludeLoc = getImportLocation(F);
1531     }
1532     SrcMgr::CharacteristicKind
1533       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1534     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1535                                         BaseOffset + Record[0]);
1536     SrcMgr::FileInfo &FileInfo =
1537           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1538     FileInfo.NumCreatedFIDs = Record[5];
1539     if (Record[3])
1540       FileInfo.setHasLineDirectives();
1541 
1542     unsigned NumFileDecls = Record[7];
1543     if (NumFileDecls && ContextObj) {
1544       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1545       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1546       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1547                                                              NumFileDecls));
1548     }
1549 
1550     const SrcMgr::ContentCache &ContentCache =
1551         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1552     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1553         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1554         !ContentCache.getBufferIfLoaded()) {
1555       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1556       if (!Buffer)
1557         return true;
1558       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1559     }
1560 
1561     break;
1562   }
1563 
1564   case SM_SLOC_BUFFER_ENTRY: {
1565     const char *Name = Blob.data();
1566     unsigned Offset = Record[0];
1567     SrcMgr::CharacteristicKind
1568       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1569     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1570     if (IncludeLoc.isInvalid() && F->isModule()) {
1571       IncludeLoc = getImportLocation(F);
1572     }
1573 
1574     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1575     if (!Buffer)
1576       return true;
1577     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1578                            BaseOffset + Offset, IncludeLoc);
1579     break;
1580   }
1581 
1582   case SM_SLOC_EXPANSION_ENTRY: {
1583     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1584     SourceMgr.createExpansionLoc(SpellingLoc,
1585                                      ReadSourceLocation(*F, Record[2]),
1586                                      ReadSourceLocation(*F, Record[3]),
1587                                      Record[5],
1588                                      Record[4],
1589                                      ID,
1590                                      BaseOffset + Record[0]);
1591     break;
1592   }
1593   }
1594 
1595   return false;
1596 }
1597 
1598 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1599   if (ID == 0)
1600     return std::make_pair(SourceLocation(), "");
1601 
1602   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1603     Error("source location entry ID out-of-range for AST file");
1604     return std::make_pair(SourceLocation(), "");
1605   }
1606 
1607   // Find which module file this entry lands in.
1608   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1609   if (!M->isModule())
1610     return std::make_pair(SourceLocation(), "");
1611 
1612   // FIXME: Can we map this down to a particular submodule? That would be
1613   // ideal.
1614   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1615 }
1616 
1617 /// Find the location where the module F is imported.
1618 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1619   if (F->ImportLoc.isValid())
1620     return F->ImportLoc;
1621 
1622   // Otherwise we have a PCH. It's considered to be "imported" at the first
1623   // location of its includer.
1624   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1625     // Main file is the importer.
1626     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1627     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1628   }
1629   return F->ImportedBy[0]->FirstLoc;
1630 }
1631 
1632 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1633 /// the abbreviations that are at the top of the block and then leave the cursor
1634 /// pointing into the block.
1635 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1636                                  uint64_t *StartOfBlockOffset) {
1637   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1638     // FIXME this drops errors on the floor.
1639     consumeError(std::move(Err));
1640     return true;
1641   }
1642 
1643   if (StartOfBlockOffset)
1644     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1645 
1646   while (true) {
1647     uint64_t Offset = Cursor.GetCurrentBitNo();
1648     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1649     if (!MaybeCode) {
1650       // FIXME this drops errors on the floor.
1651       consumeError(MaybeCode.takeError());
1652       return true;
1653     }
1654     unsigned Code = MaybeCode.get();
1655 
1656     // We expect all abbrevs to be at the start of the block.
1657     if (Code != llvm::bitc::DEFINE_ABBREV) {
1658       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1659         // FIXME this drops errors on the floor.
1660         consumeError(std::move(Err));
1661         return true;
1662       }
1663       return false;
1664     }
1665     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1666       // FIXME this drops errors on the floor.
1667       consumeError(std::move(Err));
1668       return true;
1669     }
1670   }
1671 }
1672 
1673 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1674                            unsigned &Idx) {
1675   Token Tok;
1676   Tok.startToken();
1677   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1678   Tok.setLength(Record[Idx++]);
1679   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1680     Tok.setIdentifierInfo(II);
1681   Tok.setKind((tok::TokenKind)Record[Idx++]);
1682   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1683   return Tok;
1684 }
1685 
1686 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1687   BitstreamCursor &Stream = F.MacroCursor;
1688 
1689   // Keep track of where we are in the stream, then jump back there
1690   // after reading this macro.
1691   SavedStreamPosition SavedPosition(Stream);
1692 
1693   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1694     // FIXME this drops errors on the floor.
1695     consumeError(std::move(Err));
1696     return nullptr;
1697   }
1698   RecordData Record;
1699   SmallVector<IdentifierInfo*, 16> MacroParams;
1700   MacroInfo *Macro = nullptr;
1701 
1702   while (true) {
1703     // Advance to the next record, but if we get to the end of the block, don't
1704     // pop it (removing all the abbreviations from the cursor) since we want to
1705     // be able to reseek within the block and read entries.
1706     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1707     Expected<llvm::BitstreamEntry> MaybeEntry =
1708         Stream.advanceSkippingSubblocks(Flags);
1709     if (!MaybeEntry) {
1710       Error(MaybeEntry.takeError());
1711       return Macro;
1712     }
1713     llvm::BitstreamEntry Entry = MaybeEntry.get();
1714 
1715     switch (Entry.Kind) {
1716     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1717     case llvm::BitstreamEntry::Error:
1718       Error("malformed block record in AST file");
1719       return Macro;
1720     case llvm::BitstreamEntry::EndBlock:
1721       return Macro;
1722     case llvm::BitstreamEntry::Record:
1723       // The interesting case.
1724       break;
1725     }
1726 
1727     // Read a record.
1728     Record.clear();
1729     PreprocessorRecordTypes RecType;
1730     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1731       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1732     else {
1733       Error(MaybeRecType.takeError());
1734       return Macro;
1735     }
1736     switch (RecType) {
1737     case PP_MODULE_MACRO:
1738     case PP_MACRO_DIRECTIVE_HISTORY:
1739       return Macro;
1740 
1741     case PP_MACRO_OBJECT_LIKE:
1742     case PP_MACRO_FUNCTION_LIKE: {
1743       // If we already have a macro, that means that we've hit the end
1744       // of the definition of the macro we were looking for. We're
1745       // done.
1746       if (Macro)
1747         return Macro;
1748 
1749       unsigned NextIndex = 1; // Skip identifier ID.
1750       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1751       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1752       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1753       MI->setIsUsed(Record[NextIndex++]);
1754       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1755 
1756       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1757         // Decode function-like macro info.
1758         bool isC99VarArgs = Record[NextIndex++];
1759         bool isGNUVarArgs = Record[NextIndex++];
1760         bool hasCommaPasting = Record[NextIndex++];
1761         MacroParams.clear();
1762         unsigned NumArgs = Record[NextIndex++];
1763         for (unsigned i = 0; i != NumArgs; ++i)
1764           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1765 
1766         // Install function-like macro info.
1767         MI->setIsFunctionLike();
1768         if (isC99VarArgs) MI->setIsC99Varargs();
1769         if (isGNUVarArgs) MI->setIsGNUVarargs();
1770         if (hasCommaPasting) MI->setHasCommaPasting();
1771         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1772       }
1773 
1774       // Remember that we saw this macro last so that we add the tokens that
1775       // form its body to it.
1776       Macro = MI;
1777 
1778       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1779           Record[NextIndex]) {
1780         // We have a macro definition. Register the association
1781         PreprocessedEntityID
1782             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1783         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1784         PreprocessingRecord::PPEntityID PPID =
1785             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1786         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1787             PPRec.getPreprocessedEntity(PPID));
1788         if (PPDef)
1789           PPRec.RegisterMacroDefinition(Macro, PPDef);
1790       }
1791 
1792       ++NumMacrosRead;
1793       break;
1794     }
1795 
1796     case PP_TOKEN: {
1797       // If we see a TOKEN before a PP_MACRO_*, then the file is
1798       // erroneous, just pretend we didn't see this.
1799       if (!Macro) break;
1800 
1801       unsigned Idx = 0;
1802       Token Tok = ReadToken(F, Record, Idx);
1803       Macro->AddTokenToBody(Tok);
1804       break;
1805     }
1806     }
1807   }
1808 }
1809 
1810 PreprocessedEntityID
1811 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1812                                          unsigned LocalID) const {
1813   if (!M.ModuleOffsetMap.empty())
1814     ReadModuleOffsetMap(M);
1815 
1816   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1817     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1818   assert(I != M.PreprocessedEntityRemap.end()
1819          && "Invalid index into preprocessed entity index remap");
1820 
1821   return LocalID + I->second;
1822 }
1823 
1824 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1825   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1826 }
1827 
1828 HeaderFileInfoTrait::internal_key_type
1829 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1830   internal_key_type ikey = {FE->getSize(),
1831                             M.HasTimestamps ? FE->getModificationTime() : 0,
1832                             FE->getName(), /*Imported*/ false};
1833   return ikey;
1834 }
1835 
1836 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1837   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1838     return false;
1839 
1840   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1841     return true;
1842 
1843   // Determine whether the actual files are equivalent.
1844   FileManager &FileMgr = Reader.getFileManager();
1845   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1846     if (!Key.Imported) {
1847       if (auto File = FileMgr.getFile(Key.Filename))
1848         return *File;
1849       return nullptr;
1850     }
1851 
1852     std::string Resolved = std::string(Key.Filename);
1853     Reader.ResolveImportedPath(M, Resolved);
1854     if (auto File = FileMgr.getFile(Resolved))
1855       return *File;
1856     return nullptr;
1857   };
1858 
1859   const FileEntry *FEA = GetFile(a);
1860   const FileEntry *FEB = GetFile(b);
1861   return FEA && FEA == FEB;
1862 }
1863 
1864 std::pair<unsigned, unsigned>
1865 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1866   return readULEBKeyDataLength(d);
1867 }
1868 
1869 HeaderFileInfoTrait::internal_key_type
1870 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1871   using namespace llvm::support;
1872 
1873   internal_key_type ikey;
1874   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1875   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1876   ikey.Filename = (const char *)d;
1877   ikey.Imported = true;
1878   return ikey;
1879 }
1880 
1881 HeaderFileInfoTrait::data_type
1882 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1883                               unsigned DataLen) {
1884   using namespace llvm::support;
1885 
1886   const unsigned char *End = d + DataLen;
1887   HeaderFileInfo HFI;
1888   unsigned Flags = *d++;
1889   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1890   HFI.isImport |= (Flags >> 5) & 0x01;
1891   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1892   HFI.DirInfo = (Flags >> 1) & 0x07;
1893   HFI.IndexHeaderMapHeader = Flags & 0x01;
1894   // FIXME: Find a better way to handle this. Maybe just store a
1895   // "has been included" flag?
1896   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1897                              HFI.NumIncludes);
1898   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1899       M, endian::readNext<uint32_t, little, unaligned>(d));
1900   if (unsigned FrameworkOffset =
1901           endian::readNext<uint32_t, little, unaligned>(d)) {
1902     // The framework offset is 1 greater than the actual offset,
1903     // since 0 is used as an indicator for "no framework name".
1904     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1905     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1906   }
1907 
1908   assert((End - d) % 4 == 0 &&
1909          "Wrong data length in HeaderFileInfo deserialization");
1910   while (d != End) {
1911     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1912     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1913     LocalSMID >>= 2;
1914 
1915     // This header is part of a module. Associate it with the module to enable
1916     // implicit module import.
1917     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1918     Module *Mod = Reader.getSubmodule(GlobalSMID);
1919     FileManager &FileMgr = Reader.getFileManager();
1920     ModuleMap &ModMap =
1921         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1922 
1923     std::string Filename = std::string(key.Filename);
1924     if (key.Imported)
1925       Reader.ResolveImportedPath(M, Filename);
1926     // FIXME: NameAsWritten
1927     Module::Header H = {std::string(key.Filename), "",
1928                         *FileMgr.getFile(Filename)};
1929     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1930     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1931   }
1932 
1933   // This HeaderFileInfo was externally loaded.
1934   HFI.External = true;
1935   HFI.IsValid = true;
1936   return HFI;
1937 }
1938 
1939 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1940                                 uint32_t MacroDirectivesOffset) {
1941   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1942   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1943 }
1944 
1945 void ASTReader::ReadDefinedMacros() {
1946   // Note that we are loading defined macros.
1947   Deserializing Macros(this);
1948 
1949   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1950     BitstreamCursor &MacroCursor = I.MacroCursor;
1951 
1952     // If there was no preprocessor block, skip this file.
1953     if (MacroCursor.getBitcodeBytes().empty())
1954       continue;
1955 
1956     BitstreamCursor Cursor = MacroCursor;
1957     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1958       Error(std::move(Err));
1959       return;
1960     }
1961 
1962     RecordData Record;
1963     while (true) {
1964       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1965       if (!MaybeE) {
1966         Error(MaybeE.takeError());
1967         return;
1968       }
1969       llvm::BitstreamEntry E = MaybeE.get();
1970 
1971       switch (E.Kind) {
1972       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1973       case llvm::BitstreamEntry::Error:
1974         Error("malformed block record in AST file");
1975         return;
1976       case llvm::BitstreamEntry::EndBlock:
1977         goto NextCursor;
1978 
1979       case llvm::BitstreamEntry::Record: {
1980         Record.clear();
1981         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1982         if (!MaybeRecord) {
1983           Error(MaybeRecord.takeError());
1984           return;
1985         }
1986         switch (MaybeRecord.get()) {
1987         default:  // Default behavior: ignore.
1988           break;
1989 
1990         case PP_MACRO_OBJECT_LIKE:
1991         case PP_MACRO_FUNCTION_LIKE: {
1992           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1993           if (II->isOutOfDate())
1994             updateOutOfDateIdentifier(*II);
1995           break;
1996         }
1997 
1998         case PP_TOKEN:
1999           // Ignore tokens.
2000           break;
2001         }
2002         break;
2003       }
2004       }
2005     }
2006     NextCursor:  ;
2007   }
2008 }
2009 
2010 namespace {
2011 
2012   /// Visitor class used to look up identifirs in an AST file.
2013   class IdentifierLookupVisitor {
2014     StringRef Name;
2015     unsigned NameHash;
2016     unsigned PriorGeneration;
2017     unsigned &NumIdentifierLookups;
2018     unsigned &NumIdentifierLookupHits;
2019     IdentifierInfo *Found = nullptr;
2020 
2021   public:
2022     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2023                             unsigned &NumIdentifierLookups,
2024                             unsigned &NumIdentifierLookupHits)
2025       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2026         PriorGeneration(PriorGeneration),
2027         NumIdentifierLookups(NumIdentifierLookups),
2028         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2029 
2030     bool operator()(ModuleFile &M) {
2031       // If we've already searched this module file, skip it now.
2032       if (M.Generation <= PriorGeneration)
2033         return true;
2034 
2035       ASTIdentifierLookupTable *IdTable
2036         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2037       if (!IdTable)
2038         return false;
2039 
2040       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2041                                      Found);
2042       ++NumIdentifierLookups;
2043       ASTIdentifierLookupTable::iterator Pos =
2044           IdTable->find_hashed(Name, NameHash, &Trait);
2045       if (Pos == IdTable->end())
2046         return false;
2047 
2048       // Dereferencing the iterator has the effect of building the
2049       // IdentifierInfo node and populating it with the various
2050       // declarations it needs.
2051       ++NumIdentifierLookupHits;
2052       Found = *Pos;
2053       return true;
2054     }
2055 
2056     // Retrieve the identifier info found within the module
2057     // files.
2058     IdentifierInfo *getIdentifierInfo() const { return Found; }
2059   };
2060 
2061 } // namespace
2062 
2063 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2064   // Note that we are loading an identifier.
2065   Deserializing AnIdentifier(this);
2066 
2067   unsigned PriorGeneration = 0;
2068   if (getContext().getLangOpts().Modules)
2069     PriorGeneration = IdentifierGeneration[&II];
2070 
2071   // If there is a global index, look there first to determine which modules
2072   // provably do not have any results for this identifier.
2073   GlobalModuleIndex::HitSet Hits;
2074   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2075   if (!loadGlobalIndex()) {
2076     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2077       HitsPtr = &Hits;
2078     }
2079   }
2080 
2081   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2082                                   NumIdentifierLookups,
2083                                   NumIdentifierLookupHits);
2084   ModuleMgr.visit(Visitor, HitsPtr);
2085   markIdentifierUpToDate(&II);
2086 }
2087 
2088 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2089   if (!II)
2090     return;
2091 
2092   II->setOutOfDate(false);
2093 
2094   // Update the generation for this identifier.
2095   if (getContext().getLangOpts().Modules)
2096     IdentifierGeneration[II] = getGeneration();
2097 }
2098 
2099 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2100                                     const PendingMacroInfo &PMInfo) {
2101   ModuleFile &M = *PMInfo.M;
2102 
2103   BitstreamCursor &Cursor = M.MacroCursor;
2104   SavedStreamPosition SavedPosition(Cursor);
2105   if (llvm::Error Err =
2106           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2107     Error(std::move(Err));
2108     return;
2109   }
2110 
2111   struct ModuleMacroRecord {
2112     SubmoduleID SubModID;
2113     MacroInfo *MI;
2114     SmallVector<SubmoduleID, 8> Overrides;
2115   };
2116   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2117 
2118   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2119   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2120   // macro histroy.
2121   RecordData Record;
2122   while (true) {
2123     Expected<llvm::BitstreamEntry> MaybeEntry =
2124         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2125     if (!MaybeEntry) {
2126       Error(MaybeEntry.takeError());
2127       return;
2128     }
2129     llvm::BitstreamEntry Entry = MaybeEntry.get();
2130 
2131     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2132       Error("malformed block record in AST file");
2133       return;
2134     }
2135 
2136     Record.clear();
2137     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2138     if (!MaybePP) {
2139       Error(MaybePP.takeError());
2140       return;
2141     }
2142     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2143     case PP_MACRO_DIRECTIVE_HISTORY:
2144       break;
2145 
2146     case PP_MODULE_MACRO: {
2147       ModuleMacros.push_back(ModuleMacroRecord());
2148       auto &Info = ModuleMacros.back();
2149       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2150       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2151       for (int I = 2, N = Record.size(); I != N; ++I)
2152         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2153       continue;
2154     }
2155 
2156     default:
2157       Error("malformed block record in AST file");
2158       return;
2159     }
2160 
2161     // We found the macro directive history; that's the last record
2162     // for this macro.
2163     break;
2164   }
2165 
2166   // Module macros are listed in reverse dependency order.
2167   {
2168     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2169     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2170     for (auto &MMR : ModuleMacros) {
2171       Overrides.clear();
2172       for (unsigned ModID : MMR.Overrides) {
2173         Module *Mod = getSubmodule(ModID);
2174         auto *Macro = PP.getModuleMacro(Mod, II);
2175         assert(Macro && "missing definition for overridden macro");
2176         Overrides.push_back(Macro);
2177       }
2178 
2179       bool Inserted = false;
2180       Module *Owner = getSubmodule(MMR.SubModID);
2181       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2182     }
2183   }
2184 
2185   // Don't read the directive history for a module; we don't have anywhere
2186   // to put it.
2187   if (M.isModule())
2188     return;
2189 
2190   // Deserialize the macro directives history in reverse source-order.
2191   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2192   unsigned Idx = 0, N = Record.size();
2193   while (Idx < N) {
2194     MacroDirective *MD = nullptr;
2195     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2196     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2197     switch (K) {
2198     case MacroDirective::MD_Define: {
2199       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2200       MD = PP.AllocateDefMacroDirective(MI, Loc);
2201       break;
2202     }
2203     case MacroDirective::MD_Undefine:
2204       MD = PP.AllocateUndefMacroDirective(Loc);
2205       break;
2206     case MacroDirective::MD_Visibility:
2207       bool isPublic = Record[Idx++];
2208       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2209       break;
2210     }
2211 
2212     if (!Latest)
2213       Latest = MD;
2214     if (Earliest)
2215       Earliest->setPrevious(MD);
2216     Earliest = MD;
2217   }
2218 
2219   if (Latest)
2220     PP.setLoadedMacroDirective(II, Earliest, Latest);
2221 }
2222 
2223 bool ASTReader::shouldDisableValidationForFile(
2224     const serialization::ModuleFile &M) const {
2225   if (DisableValidationKind == DisableValidationForModuleKind::None)
2226     return false;
2227 
2228   // If a PCH is loaded and validation is disabled for PCH then disable
2229   // validation for the PCH and the modules it loads.
2230   ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2231 
2232   switch (K) {
2233   case MK_MainFile:
2234   case MK_Preamble:
2235   case MK_PCH:
2236     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2237   case MK_ImplicitModule:
2238   case MK_ExplicitModule:
2239   case MK_PrebuiltModule:
2240     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2241   }
2242 
2243   return false;
2244 }
2245 
2246 ASTReader::InputFileInfo
2247 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2248   // Go find this input file.
2249   BitstreamCursor &Cursor = F.InputFilesCursor;
2250   SavedStreamPosition SavedPosition(Cursor);
2251   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2252     // FIXME this drops errors on the floor.
2253     consumeError(std::move(Err));
2254   }
2255 
2256   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2257   if (!MaybeCode) {
2258     // FIXME this drops errors on the floor.
2259     consumeError(MaybeCode.takeError());
2260   }
2261   unsigned Code = MaybeCode.get();
2262   RecordData Record;
2263   StringRef Blob;
2264 
2265   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2266     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2267            "invalid record type for input file");
2268   else {
2269     // FIXME this drops errors on the floor.
2270     consumeError(Maybe.takeError());
2271   }
2272 
2273   assert(Record[0] == ID && "Bogus stored ID or offset");
2274   InputFileInfo R;
2275   R.StoredSize = static_cast<off_t>(Record[1]);
2276   R.StoredTime = static_cast<time_t>(Record[2]);
2277   R.Overridden = static_cast<bool>(Record[3]);
2278   R.Transient = static_cast<bool>(Record[4]);
2279   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2280   R.Filename = std::string(Blob);
2281   ResolveImportedPath(F, R.Filename);
2282 
2283   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2284   if (!MaybeEntry) // FIXME this drops errors on the floor.
2285     consumeError(MaybeEntry.takeError());
2286   llvm::BitstreamEntry Entry = MaybeEntry.get();
2287   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2288          "expected record type for input file hash");
2289 
2290   Record.clear();
2291   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2292     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2293            "invalid record type for input file hash");
2294   else {
2295     // FIXME this drops errors on the floor.
2296     consumeError(Maybe.takeError());
2297   }
2298   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2299                   static_cast<uint64_t>(Record[0]);
2300   return R;
2301 }
2302 
2303 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2304 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2305   // If this ID is bogus, just return an empty input file.
2306   if (ID == 0 || ID > F.InputFilesLoaded.size())
2307     return InputFile();
2308 
2309   // If we've already loaded this input file, return it.
2310   if (F.InputFilesLoaded[ID-1].getFile())
2311     return F.InputFilesLoaded[ID-1];
2312 
2313   if (F.InputFilesLoaded[ID-1].isNotFound())
2314     return InputFile();
2315 
2316   // Go find this input file.
2317   BitstreamCursor &Cursor = F.InputFilesCursor;
2318   SavedStreamPosition SavedPosition(Cursor);
2319   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2320     // FIXME this drops errors on the floor.
2321     consumeError(std::move(Err));
2322   }
2323 
2324   InputFileInfo FI = readInputFileInfo(F, ID);
2325   off_t StoredSize = FI.StoredSize;
2326   time_t StoredTime = FI.StoredTime;
2327   bool Overridden = FI.Overridden;
2328   bool Transient = FI.Transient;
2329   StringRef Filename = FI.Filename;
2330   uint64_t StoredContentHash = FI.ContentHash;
2331 
2332   OptionalFileEntryRefDegradesToFileEntryPtr File =
2333       expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2334 
2335   // If we didn't find the file, resolve it relative to the
2336   // original directory from which this AST file was created.
2337   if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2338       F.OriginalDir != F.BaseDirectory) {
2339     std::string Resolved = resolveFileRelativeToOriginalDir(
2340         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2341     if (!Resolved.empty())
2342       File = expectedToOptional(FileMgr.getFileRef(Resolved));
2343   }
2344 
2345   // For an overridden file, create a virtual file with the stored
2346   // size/timestamp.
2347   if ((Overridden || Transient) && !File)
2348     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2349 
2350   if (!File) {
2351     if (Complain) {
2352       std::string ErrorStr = "could not find file '";
2353       ErrorStr += Filename;
2354       ErrorStr += "' referenced by AST file '";
2355       ErrorStr += F.FileName;
2356       ErrorStr += "'";
2357       Error(ErrorStr);
2358     }
2359     // Record that we didn't find the file.
2360     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2361     return InputFile();
2362   }
2363 
2364   // Check if there was a request to override the contents of the file
2365   // that was part of the precompiled header. Overriding such a file
2366   // can lead to problems when lexing using the source locations from the
2367   // PCH.
2368   SourceManager &SM = getSourceManager();
2369   // FIXME: Reject if the overrides are different.
2370   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2371     if (Complain)
2372       Error(diag::err_fe_pch_file_overridden, Filename);
2373 
2374     // After emitting the diagnostic, bypass the overriding file to recover
2375     // (this creates a separate FileEntry).
2376     File = SM.bypassFileContentsOverride(*File);
2377     if (!File) {
2378       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2379       return InputFile();
2380     }
2381   }
2382 
2383   enum ModificationType {
2384     Size,
2385     ModTime,
2386     Content,
2387     None,
2388   };
2389   auto HasInputFileChanged = [&]() {
2390     if (StoredSize != File->getSize())
2391       return ModificationType::Size;
2392     if (!shouldDisableValidationForFile(F) && StoredTime &&
2393         StoredTime != File->getModificationTime()) {
2394       // In case the modification time changes but not the content,
2395       // accept the cached file as legit.
2396       if (ValidateASTInputFilesContent &&
2397           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2398         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2399         if (!MemBuffOrError) {
2400           if (!Complain)
2401             return ModificationType::ModTime;
2402           std::string ErrorStr = "could not get buffer for file '";
2403           ErrorStr += File->getName();
2404           ErrorStr += "'";
2405           Error(ErrorStr);
2406           return ModificationType::ModTime;
2407         }
2408 
2409         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2410         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2411           return ModificationType::None;
2412         return ModificationType::Content;
2413       }
2414       return ModificationType::ModTime;
2415     }
2416     return ModificationType::None;
2417   };
2418 
2419   bool IsOutOfDate = false;
2420   auto FileChange = HasInputFileChanged();
2421   // For an overridden file, there is nothing to validate.
2422   if (!Overridden && FileChange != ModificationType::None) {
2423     if (Complain && !Diags.isDiagnosticInFlight()) {
2424       // Build a list of the PCH imports that got us here (in reverse).
2425       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2426       while (!ImportStack.back()->ImportedBy.empty())
2427         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2428 
2429       // The top-level PCH is stale.
2430       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2431       Diag(diag::err_fe_ast_file_modified)
2432           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2433           << TopLevelPCHName << FileChange;
2434 
2435       // Print the import stack.
2436       if (ImportStack.size() > 1) {
2437         Diag(diag::note_pch_required_by)
2438           << Filename << ImportStack[0]->FileName;
2439         for (unsigned I = 1; I < ImportStack.size(); ++I)
2440           Diag(diag::note_pch_required_by)
2441             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2442       }
2443 
2444       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2445     }
2446 
2447     IsOutOfDate = true;
2448   }
2449   // FIXME: If the file is overridden and we've already opened it,
2450   // issue an error (or split it into a separate FileEntry).
2451 
2452   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2453 
2454   // Note that we've loaded this input file.
2455   F.InputFilesLoaded[ID-1] = IF;
2456   return IF;
2457 }
2458 
2459 /// If we are loading a relocatable PCH or module file, and the filename
2460 /// is not an absolute path, add the system or module root to the beginning of
2461 /// the file name.
2462 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2463   // Resolve relative to the base directory, if we have one.
2464   if (!M.BaseDirectory.empty())
2465     return ResolveImportedPath(Filename, M.BaseDirectory);
2466 }
2467 
2468 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2469   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2470     return;
2471 
2472   SmallString<128> Buffer;
2473   llvm::sys::path::append(Buffer, Prefix, Filename);
2474   Filename.assign(Buffer.begin(), Buffer.end());
2475 }
2476 
2477 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2478   switch (ARR) {
2479   case ASTReader::Failure: return true;
2480   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2481   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2482   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2483   case ASTReader::ConfigurationMismatch:
2484     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2485   case ASTReader::HadErrors: return true;
2486   case ASTReader::Success: return false;
2487   }
2488 
2489   llvm_unreachable("unknown ASTReadResult");
2490 }
2491 
2492 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2493     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2494     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2495     std::string &SuggestedPredefines) {
2496   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2497     // FIXME this drops errors on the floor.
2498     consumeError(std::move(Err));
2499     return Failure;
2500   }
2501 
2502   // Read all of the records in the options block.
2503   RecordData Record;
2504   ASTReadResult Result = Success;
2505   while (true) {
2506     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2507     if (!MaybeEntry) {
2508       // FIXME this drops errors on the floor.
2509       consumeError(MaybeEntry.takeError());
2510       return Failure;
2511     }
2512     llvm::BitstreamEntry Entry = MaybeEntry.get();
2513 
2514     switch (Entry.Kind) {
2515     case llvm::BitstreamEntry::Error:
2516     case llvm::BitstreamEntry::SubBlock:
2517       return Failure;
2518 
2519     case llvm::BitstreamEntry::EndBlock:
2520       return Result;
2521 
2522     case llvm::BitstreamEntry::Record:
2523       // The interesting case.
2524       break;
2525     }
2526 
2527     // Read and process a record.
2528     Record.clear();
2529     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2530     if (!MaybeRecordType) {
2531       // FIXME this drops errors on the floor.
2532       consumeError(MaybeRecordType.takeError());
2533       return Failure;
2534     }
2535     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2536     case LANGUAGE_OPTIONS: {
2537       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2538       if (ParseLanguageOptions(Record, Complain, Listener,
2539                                AllowCompatibleConfigurationMismatch))
2540         Result = ConfigurationMismatch;
2541       break;
2542     }
2543 
2544     case TARGET_OPTIONS: {
2545       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2546       if (ParseTargetOptions(Record, Complain, Listener,
2547                              AllowCompatibleConfigurationMismatch))
2548         Result = ConfigurationMismatch;
2549       break;
2550     }
2551 
2552     case FILE_SYSTEM_OPTIONS: {
2553       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2554       if (!AllowCompatibleConfigurationMismatch &&
2555           ParseFileSystemOptions(Record, Complain, Listener))
2556         Result = ConfigurationMismatch;
2557       break;
2558     }
2559 
2560     case HEADER_SEARCH_OPTIONS: {
2561       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2562       if (!AllowCompatibleConfigurationMismatch &&
2563           ParseHeaderSearchOptions(Record, Complain, Listener))
2564         Result = ConfigurationMismatch;
2565       break;
2566     }
2567 
2568     case PREPROCESSOR_OPTIONS:
2569       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2570       if (!AllowCompatibleConfigurationMismatch &&
2571           ParsePreprocessorOptions(Record, Complain, Listener,
2572                                    SuggestedPredefines))
2573         Result = ConfigurationMismatch;
2574       break;
2575     }
2576   }
2577 }
2578 
2579 ASTReader::ASTReadResult
2580 ASTReader::ReadControlBlock(ModuleFile &F,
2581                             SmallVectorImpl<ImportedModule> &Loaded,
2582                             const ModuleFile *ImportedBy,
2583                             unsigned ClientLoadCapabilities) {
2584   BitstreamCursor &Stream = F.Stream;
2585 
2586   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2587     Error(std::move(Err));
2588     return Failure;
2589   }
2590 
2591   // Lambda to read the unhashed control block the first time it's called.
2592   //
2593   // For PCM files, the unhashed control block cannot be read until after the
2594   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2595   // need to look ahead before reading the IMPORTS record.  For consistency,
2596   // this block is always read somehow (see BitstreamEntry::EndBlock).
2597   bool HasReadUnhashedControlBlock = false;
2598   auto readUnhashedControlBlockOnce = [&]() {
2599     if (!HasReadUnhashedControlBlock) {
2600       HasReadUnhashedControlBlock = true;
2601       if (ASTReadResult Result =
2602               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2603         return Result;
2604     }
2605     return Success;
2606   };
2607 
2608   bool DisableValidation = shouldDisableValidationForFile(F);
2609 
2610   // Read all of the records and blocks in the control block.
2611   RecordData Record;
2612   unsigned NumInputs = 0;
2613   unsigned NumUserInputs = 0;
2614   StringRef BaseDirectoryAsWritten;
2615   while (true) {
2616     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2617     if (!MaybeEntry) {
2618       Error(MaybeEntry.takeError());
2619       return Failure;
2620     }
2621     llvm::BitstreamEntry Entry = MaybeEntry.get();
2622 
2623     switch (Entry.Kind) {
2624     case llvm::BitstreamEntry::Error:
2625       Error("malformed block record in AST file");
2626       return Failure;
2627     case llvm::BitstreamEntry::EndBlock: {
2628       // Validate the module before returning.  This call catches an AST with
2629       // no module name and no imports.
2630       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2631         return Result;
2632 
2633       // Validate input files.
2634       const HeaderSearchOptions &HSOpts =
2635           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2636 
2637       // All user input files reside at the index range [0, NumUserInputs), and
2638       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2639       // loaded module files, ignore missing inputs.
2640       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2641           F.Kind != MK_PrebuiltModule) {
2642         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2643 
2644         // If we are reading a module, we will create a verification timestamp,
2645         // so we verify all input files.  Otherwise, verify only user input
2646         // files.
2647 
2648         unsigned N = NumUserInputs;
2649         if (ValidateSystemInputs ||
2650             (HSOpts.ModulesValidateOncePerBuildSession &&
2651              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2652              F.Kind == MK_ImplicitModule))
2653           N = NumInputs;
2654 
2655         for (unsigned I = 0; I < N; ++I) {
2656           InputFile IF = getInputFile(F, I+1, Complain);
2657           if (!IF.getFile() || IF.isOutOfDate())
2658             return OutOfDate;
2659         }
2660       }
2661 
2662       if (Listener)
2663         Listener->visitModuleFile(F.FileName, F.Kind);
2664 
2665       if (Listener && Listener->needsInputFileVisitation()) {
2666         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2667                                                                 : NumUserInputs;
2668         for (unsigned I = 0; I < N; ++I) {
2669           bool IsSystem = I >= NumUserInputs;
2670           InputFileInfo FI = readInputFileInfo(F, I+1);
2671           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2672                                    F.Kind == MK_ExplicitModule ||
2673                                    F.Kind == MK_PrebuiltModule);
2674         }
2675       }
2676 
2677       return Success;
2678     }
2679 
2680     case llvm::BitstreamEntry::SubBlock:
2681       switch (Entry.ID) {
2682       case INPUT_FILES_BLOCK_ID:
2683         F.InputFilesCursor = Stream;
2684         if (llvm::Error Err = Stream.SkipBlock()) {
2685           Error(std::move(Err));
2686           return Failure;
2687         }
2688         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2689           Error("malformed block record in AST file");
2690           return Failure;
2691         }
2692         continue;
2693 
2694       case OPTIONS_BLOCK_ID:
2695         // If we're reading the first module for this group, check its options
2696         // are compatible with ours. For modules it imports, no further checking
2697         // is required, because we checked them when we built it.
2698         if (Listener && !ImportedBy) {
2699           // Should we allow the configuration of the module file to differ from
2700           // the configuration of the current translation unit in a compatible
2701           // way?
2702           //
2703           // FIXME: Allow this for files explicitly specified with -include-pch.
2704           bool AllowCompatibleConfigurationMismatch =
2705               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2706 
2707           ASTReadResult Result =
2708               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2709                                AllowCompatibleConfigurationMismatch, *Listener,
2710                                SuggestedPredefines);
2711           if (Result == Failure) {
2712             Error("malformed block record in AST file");
2713             return Result;
2714           }
2715 
2716           if (DisableValidation ||
2717               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2718             Result = Success;
2719 
2720           // If we can't load the module, exit early since we likely
2721           // will rebuild the module anyway. The stream may be in the
2722           // middle of a block.
2723           if (Result != Success)
2724             return Result;
2725         } else if (llvm::Error Err = Stream.SkipBlock()) {
2726           Error(std::move(Err));
2727           return Failure;
2728         }
2729         continue;
2730 
2731       default:
2732         if (llvm::Error Err = Stream.SkipBlock()) {
2733           Error(std::move(Err));
2734           return Failure;
2735         }
2736         continue;
2737       }
2738 
2739     case llvm::BitstreamEntry::Record:
2740       // The interesting case.
2741       break;
2742     }
2743 
2744     // Read and process a record.
2745     Record.clear();
2746     StringRef Blob;
2747     Expected<unsigned> MaybeRecordType =
2748         Stream.readRecord(Entry.ID, Record, &Blob);
2749     if (!MaybeRecordType) {
2750       Error(MaybeRecordType.takeError());
2751       return Failure;
2752     }
2753     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2754     case METADATA: {
2755       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2756         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2757           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2758                                         : diag::err_pch_version_too_new);
2759         return VersionMismatch;
2760       }
2761 
2762       bool hasErrors = Record[6];
2763       if (hasErrors && !DisableValidation) {
2764         // If requested by the caller and the module hasn't already been read
2765         // or compiled, mark modules on error as out-of-date.
2766         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2767             !ModuleMgr.getModuleCache().isPCMFinal(F.FileName))
2768           return OutOfDate;
2769 
2770         if (!AllowASTWithCompilerErrors) {
2771           Diag(diag::err_pch_with_compiler_errors);
2772           return HadErrors;
2773         }
2774       }
2775       if (hasErrors) {
2776         Diags.ErrorOccurred = true;
2777         Diags.UncompilableErrorOccurred = true;
2778         Diags.UnrecoverableErrorOccurred = true;
2779       }
2780 
2781       F.RelocatablePCH = Record[4];
2782       // Relative paths in a relocatable PCH are relative to our sysroot.
2783       if (F.RelocatablePCH)
2784         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2785 
2786       F.HasTimestamps = Record[5];
2787 
2788       const std::string &CurBranch = getClangFullRepositoryVersion();
2789       StringRef ASTBranch = Blob;
2790       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2791         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2792           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2793         return VersionMismatch;
2794       }
2795       break;
2796     }
2797 
2798     case IMPORTS: {
2799       // Validate the AST before processing any imports (otherwise, untangling
2800       // them can be error-prone and expensive).  A module will have a name and
2801       // will already have been validated, but this catches the PCH case.
2802       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2803         return Result;
2804 
2805       // Load each of the imported PCH files.
2806       unsigned Idx = 0, N = Record.size();
2807       while (Idx < N) {
2808         // Read information about the AST file.
2809         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2810         // The import location will be the local one for now; we will adjust
2811         // all import locations of module imports after the global source
2812         // location info are setup, in ReadAST.
2813         SourceLocation ImportLoc =
2814             ReadUntranslatedSourceLocation(Record[Idx++]);
2815         off_t StoredSize = (off_t)Record[Idx++];
2816         time_t StoredModTime = (time_t)Record[Idx++];
2817         auto FirstSignatureByte = Record.begin() + Idx;
2818         ASTFileSignature StoredSignature = ASTFileSignature::create(
2819             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2820         Idx += ASTFileSignature::size;
2821 
2822         std::string ImportedName = ReadString(Record, Idx);
2823         std::string ImportedFile;
2824 
2825         // For prebuilt and explicit modules first consult the file map for
2826         // an override. Note that here we don't search prebuilt module
2827         // directories, only the explicit name to file mappings. Also, we will
2828         // still verify the size/signature making sure it is essentially the
2829         // same file but perhaps in a different location.
2830         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2831           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2832             ImportedName, /*FileMapOnly*/ true);
2833 
2834         if (ImportedFile.empty())
2835           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2836           // ModuleCache as when writing.
2837           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2838         else
2839           SkipPath(Record, Idx);
2840 
2841         // If our client can't cope with us being out of date, we can't cope with
2842         // our dependency being missing.
2843         unsigned Capabilities = ClientLoadCapabilities;
2844         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2845           Capabilities &= ~ARR_Missing;
2846 
2847         // Load the AST file.
2848         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2849                                   Loaded, StoredSize, StoredModTime,
2850                                   StoredSignature, Capabilities);
2851 
2852         // If we diagnosed a problem, produce a backtrace.
2853         if (isDiagnosedResult(Result, Capabilities))
2854           Diag(diag::note_module_file_imported_by)
2855               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2856 
2857         switch (Result) {
2858         case Failure: return Failure;
2859           // If we have to ignore the dependency, we'll have to ignore this too.
2860         case Missing:
2861         case OutOfDate: return OutOfDate;
2862         case VersionMismatch: return VersionMismatch;
2863         case ConfigurationMismatch: return ConfigurationMismatch;
2864         case HadErrors: return HadErrors;
2865         case Success: break;
2866         }
2867       }
2868       break;
2869     }
2870 
2871     case ORIGINAL_FILE:
2872       F.OriginalSourceFileID = FileID::get(Record[0]);
2873       F.ActualOriginalSourceFileName = std::string(Blob);
2874       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2875       ResolveImportedPath(F, F.OriginalSourceFileName);
2876       break;
2877 
2878     case ORIGINAL_FILE_ID:
2879       F.OriginalSourceFileID = FileID::get(Record[0]);
2880       break;
2881 
2882     case ORIGINAL_PCH_DIR:
2883       F.OriginalDir = std::string(Blob);
2884       break;
2885 
2886     case MODULE_NAME:
2887       F.ModuleName = std::string(Blob);
2888       Diag(diag::remark_module_import)
2889           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2890           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2891       if (Listener)
2892         Listener->ReadModuleName(F.ModuleName);
2893 
2894       // Validate the AST as soon as we have a name so we can exit early on
2895       // failure.
2896       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2897         return Result;
2898 
2899       break;
2900 
2901     case MODULE_DIRECTORY: {
2902       // Save the BaseDirectory as written in the PCM for computing the module
2903       // filename for the ModuleCache.
2904       BaseDirectoryAsWritten = Blob;
2905       assert(!F.ModuleName.empty() &&
2906              "MODULE_DIRECTORY found before MODULE_NAME");
2907       // If we've already loaded a module map file covering this module, we may
2908       // have a better path for it (relative to the current build).
2909       Module *M = PP.getHeaderSearchInfo().lookupModule(
2910           F.ModuleName, /*AllowSearch*/ true,
2911           /*AllowExtraModuleMapSearch*/ true);
2912       if (M && M->Directory) {
2913         // If we're implicitly loading a module, the base directory can't
2914         // change between the build and use.
2915         // Don't emit module relocation error if we have -fno-validate-pch
2916         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2917                   DisableValidationForModuleKind::Module) &&
2918             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2919           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2920           if (!BuildDir || *BuildDir != M->Directory) {
2921             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2922               Diag(diag::err_imported_module_relocated)
2923                   << F.ModuleName << Blob << M->Directory->getName();
2924             return OutOfDate;
2925           }
2926         }
2927         F.BaseDirectory = std::string(M->Directory->getName());
2928       } else {
2929         F.BaseDirectory = std::string(Blob);
2930       }
2931       break;
2932     }
2933 
2934     case MODULE_MAP_FILE:
2935       if (ASTReadResult Result =
2936               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2937         return Result;
2938       break;
2939 
2940     case INPUT_FILE_OFFSETS:
2941       NumInputs = Record[0];
2942       NumUserInputs = Record[1];
2943       F.InputFileOffsets =
2944           (const llvm::support::unaligned_uint64_t *)Blob.data();
2945       F.InputFilesLoaded.resize(NumInputs);
2946       F.NumUserInputFiles = NumUserInputs;
2947       break;
2948     }
2949   }
2950 }
2951 
2952 ASTReader::ASTReadResult
2953 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2954   BitstreamCursor &Stream = F.Stream;
2955 
2956   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2957     Error(std::move(Err));
2958     return Failure;
2959   }
2960   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2961 
2962   // Read all of the records and blocks for the AST file.
2963   RecordData Record;
2964   while (true) {
2965     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2966     if (!MaybeEntry) {
2967       Error(MaybeEntry.takeError());
2968       return Failure;
2969     }
2970     llvm::BitstreamEntry Entry = MaybeEntry.get();
2971 
2972     switch (Entry.Kind) {
2973     case llvm::BitstreamEntry::Error:
2974       Error("error at end of module block in AST file");
2975       return Failure;
2976     case llvm::BitstreamEntry::EndBlock:
2977       // Outside of C++, we do not store a lookup map for the translation unit.
2978       // Instead, mark it as needing a lookup map to be built if this module
2979       // contains any declarations lexically within it (which it always does!).
2980       // This usually has no cost, since we very rarely need the lookup map for
2981       // the translation unit outside C++.
2982       if (ASTContext *Ctx = ContextObj) {
2983         DeclContext *DC = Ctx->getTranslationUnitDecl();
2984         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2985           DC->setMustBuildLookupTable();
2986       }
2987 
2988       return Success;
2989     case llvm::BitstreamEntry::SubBlock:
2990       switch (Entry.ID) {
2991       case DECLTYPES_BLOCK_ID:
2992         // We lazily load the decls block, but we want to set up the
2993         // DeclsCursor cursor to point into it.  Clone our current bitcode
2994         // cursor to it, enter the block and read the abbrevs in that block.
2995         // With the main cursor, we just skip over it.
2996         F.DeclsCursor = Stream;
2997         if (llvm::Error Err = Stream.SkipBlock()) {
2998           Error(std::move(Err));
2999           return Failure;
3000         }
3001         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
3002                              &F.DeclsBlockStartOffset)) {
3003           Error("malformed block record in AST file");
3004           return Failure;
3005         }
3006         break;
3007 
3008       case PREPROCESSOR_BLOCK_ID:
3009         F.MacroCursor = Stream;
3010         if (!PP.getExternalSource())
3011           PP.setExternalSource(this);
3012 
3013         if (llvm::Error Err = Stream.SkipBlock()) {
3014           Error(std::move(Err));
3015           return Failure;
3016         }
3017         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
3018           Error("malformed block record in AST file");
3019           return Failure;
3020         }
3021         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3022         break;
3023 
3024       case PREPROCESSOR_DETAIL_BLOCK_ID:
3025         F.PreprocessorDetailCursor = Stream;
3026 
3027         if (llvm::Error Err = Stream.SkipBlock()) {
3028           Error(std::move(Err));
3029           return Failure;
3030         }
3031         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3032                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3033           Error("malformed preprocessor detail record in AST file");
3034           return Failure;
3035         }
3036         F.PreprocessorDetailStartOffset
3037         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3038 
3039         if (!PP.getPreprocessingRecord())
3040           PP.createPreprocessingRecord();
3041         if (!PP.getPreprocessingRecord()->getExternalSource())
3042           PP.getPreprocessingRecord()->SetExternalSource(*this);
3043         break;
3044 
3045       case SOURCE_MANAGER_BLOCK_ID:
3046         if (ReadSourceManagerBlock(F))
3047           return Failure;
3048         break;
3049 
3050       case SUBMODULE_BLOCK_ID:
3051         if (ASTReadResult Result =
3052                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3053           return Result;
3054         break;
3055 
3056       case COMMENTS_BLOCK_ID: {
3057         BitstreamCursor C = Stream;
3058 
3059         if (llvm::Error Err = Stream.SkipBlock()) {
3060           Error(std::move(Err));
3061           return Failure;
3062         }
3063         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3064           Error("malformed comments block in AST file");
3065           return Failure;
3066         }
3067         CommentsCursors.push_back(std::make_pair(C, &F));
3068         break;
3069       }
3070 
3071       default:
3072         if (llvm::Error Err = Stream.SkipBlock()) {
3073           Error(std::move(Err));
3074           return Failure;
3075         }
3076         break;
3077       }
3078       continue;
3079 
3080     case llvm::BitstreamEntry::Record:
3081       // The interesting case.
3082       break;
3083     }
3084 
3085     // Read and process a record.
3086     Record.clear();
3087     StringRef Blob;
3088     Expected<unsigned> MaybeRecordType =
3089         Stream.readRecord(Entry.ID, Record, &Blob);
3090     if (!MaybeRecordType) {
3091       Error(MaybeRecordType.takeError());
3092       return Failure;
3093     }
3094     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3095 
3096     // If we're not loading an AST context, we don't care about most records.
3097     if (!ContextObj) {
3098       switch (RecordType) {
3099       case IDENTIFIER_TABLE:
3100       case IDENTIFIER_OFFSET:
3101       case INTERESTING_IDENTIFIERS:
3102       case STATISTICS:
3103       case PP_CONDITIONAL_STACK:
3104       case PP_COUNTER_VALUE:
3105       case SOURCE_LOCATION_OFFSETS:
3106       case MODULE_OFFSET_MAP:
3107       case SOURCE_MANAGER_LINE_TABLE:
3108       case SOURCE_LOCATION_PRELOADS:
3109       case PPD_ENTITIES_OFFSETS:
3110       case HEADER_SEARCH_TABLE:
3111       case IMPORTED_MODULES:
3112       case MACRO_OFFSET:
3113         break;
3114       default:
3115         continue;
3116       }
3117     }
3118 
3119     switch (RecordType) {
3120     default:  // Default behavior: ignore.
3121       break;
3122 
3123     case TYPE_OFFSET: {
3124       if (F.LocalNumTypes != 0) {
3125         Error("duplicate TYPE_OFFSET record in AST file");
3126         return Failure;
3127       }
3128       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3129       F.LocalNumTypes = Record[0];
3130       unsigned LocalBaseTypeIndex = Record[1];
3131       F.BaseTypeIndex = getTotalNumTypes();
3132 
3133       if (F.LocalNumTypes > 0) {
3134         // Introduce the global -> local mapping for types within this module.
3135         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3136 
3137         // Introduce the local -> global mapping for types within this module.
3138         F.TypeRemap.insertOrReplace(
3139           std::make_pair(LocalBaseTypeIndex,
3140                          F.BaseTypeIndex - LocalBaseTypeIndex));
3141 
3142         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3143       }
3144       break;
3145     }
3146 
3147     case DECL_OFFSET: {
3148       if (F.LocalNumDecls != 0) {
3149         Error("duplicate DECL_OFFSET record in AST file");
3150         return Failure;
3151       }
3152       F.DeclOffsets = (const DeclOffset *)Blob.data();
3153       F.LocalNumDecls = Record[0];
3154       unsigned LocalBaseDeclID = Record[1];
3155       F.BaseDeclID = getTotalNumDecls();
3156 
3157       if (F.LocalNumDecls > 0) {
3158         // Introduce the global -> local mapping for declarations within this
3159         // module.
3160         GlobalDeclMap.insert(
3161           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3162 
3163         // Introduce the local -> global mapping for declarations within this
3164         // module.
3165         F.DeclRemap.insertOrReplace(
3166           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3167 
3168         // Introduce the global -> local mapping for declarations within this
3169         // module.
3170         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3171 
3172         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3173       }
3174       break;
3175     }
3176 
3177     case TU_UPDATE_LEXICAL: {
3178       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3179       LexicalContents Contents(
3180           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3181               Blob.data()),
3182           static_cast<unsigned int>(Blob.size() / 4));
3183       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3184       TU->setHasExternalLexicalStorage(true);
3185       break;
3186     }
3187 
3188     case UPDATE_VISIBLE: {
3189       unsigned Idx = 0;
3190       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3191       auto *Data = (const unsigned char*)Blob.data();
3192       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3193       // If we've already loaded the decl, perform the updates when we finish
3194       // loading this block.
3195       if (Decl *D = GetExistingDecl(ID))
3196         PendingUpdateRecords.push_back(
3197             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3198       break;
3199     }
3200 
3201     case IDENTIFIER_TABLE:
3202       F.IdentifierTableData =
3203           reinterpret_cast<const unsigned char *>(Blob.data());
3204       if (Record[0]) {
3205         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3206             F.IdentifierTableData + Record[0],
3207             F.IdentifierTableData + sizeof(uint32_t),
3208             F.IdentifierTableData,
3209             ASTIdentifierLookupTrait(*this, F));
3210 
3211         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3212       }
3213       break;
3214 
3215     case IDENTIFIER_OFFSET: {
3216       if (F.LocalNumIdentifiers != 0) {
3217         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3218         return Failure;
3219       }
3220       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3221       F.LocalNumIdentifiers = Record[0];
3222       unsigned LocalBaseIdentifierID = Record[1];
3223       F.BaseIdentifierID = getTotalNumIdentifiers();
3224 
3225       if (F.LocalNumIdentifiers > 0) {
3226         // Introduce the global -> local mapping for identifiers within this
3227         // module.
3228         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3229                                                   &F));
3230 
3231         // Introduce the local -> global mapping for identifiers within this
3232         // module.
3233         F.IdentifierRemap.insertOrReplace(
3234           std::make_pair(LocalBaseIdentifierID,
3235                          F.BaseIdentifierID - LocalBaseIdentifierID));
3236 
3237         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3238                                  + F.LocalNumIdentifiers);
3239       }
3240       break;
3241     }
3242 
3243     case INTERESTING_IDENTIFIERS:
3244       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3245       break;
3246 
3247     case EAGERLY_DESERIALIZED_DECLS:
3248       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3249       // about "interesting" decls (for instance, if we're building a module).
3250       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3251         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3252       break;
3253 
3254     case MODULAR_CODEGEN_DECLS:
3255       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3256       // them (ie: if we're not codegenerating this module).
3257       if (F.Kind == MK_MainFile ||
3258           getContext().getLangOpts().BuildingPCHWithObjectFile)
3259         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3260           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3261       break;
3262 
3263     case SPECIAL_TYPES:
3264       if (SpecialTypes.empty()) {
3265         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3266           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3267         break;
3268       }
3269 
3270       if (SpecialTypes.size() != Record.size()) {
3271         Error("invalid special-types record");
3272         return Failure;
3273       }
3274 
3275       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3276         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3277         if (!SpecialTypes[I])
3278           SpecialTypes[I] = ID;
3279         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3280         // merge step?
3281       }
3282       break;
3283 
3284     case STATISTICS:
3285       TotalNumStatements += Record[0];
3286       TotalNumMacros += Record[1];
3287       TotalLexicalDeclContexts += Record[2];
3288       TotalVisibleDeclContexts += Record[3];
3289       break;
3290 
3291     case UNUSED_FILESCOPED_DECLS:
3292       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3293         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3294       break;
3295 
3296     case DELEGATING_CTORS:
3297       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3298         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3299       break;
3300 
3301     case WEAK_UNDECLARED_IDENTIFIERS:
3302       if (Record.size() % 4 != 0) {
3303         Error("invalid weak identifiers record");
3304         return Failure;
3305       }
3306 
3307       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3308       // files. This isn't the way to do it :)
3309       WeakUndeclaredIdentifiers.clear();
3310 
3311       // Translate the weak, undeclared identifiers into global IDs.
3312       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3313         WeakUndeclaredIdentifiers.push_back(
3314           getGlobalIdentifierID(F, Record[I++]));
3315         WeakUndeclaredIdentifiers.push_back(
3316           getGlobalIdentifierID(F, Record[I++]));
3317         WeakUndeclaredIdentifiers.push_back(
3318           ReadSourceLocation(F, Record, I).getRawEncoding());
3319         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3320       }
3321       break;
3322 
3323     case SELECTOR_OFFSETS: {
3324       F.SelectorOffsets = (const uint32_t *)Blob.data();
3325       F.LocalNumSelectors = Record[0];
3326       unsigned LocalBaseSelectorID = Record[1];
3327       F.BaseSelectorID = getTotalNumSelectors();
3328 
3329       if (F.LocalNumSelectors > 0) {
3330         // Introduce the global -> local mapping for selectors within this
3331         // module.
3332         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3333 
3334         // Introduce the local -> global mapping for selectors within this
3335         // module.
3336         F.SelectorRemap.insertOrReplace(
3337           std::make_pair(LocalBaseSelectorID,
3338                          F.BaseSelectorID - LocalBaseSelectorID));
3339 
3340         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3341       }
3342       break;
3343     }
3344 
3345     case METHOD_POOL:
3346       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3347       if (Record[0])
3348         F.SelectorLookupTable
3349           = ASTSelectorLookupTable::Create(
3350                         F.SelectorLookupTableData + Record[0],
3351                         F.SelectorLookupTableData,
3352                         ASTSelectorLookupTrait(*this, F));
3353       TotalNumMethodPoolEntries += Record[1];
3354       break;
3355 
3356     case REFERENCED_SELECTOR_POOL:
3357       if (!Record.empty()) {
3358         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3359           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3360                                                                 Record[Idx++]));
3361           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3362                                               getRawEncoding());
3363         }
3364       }
3365       break;
3366 
3367     case PP_CONDITIONAL_STACK:
3368       if (!Record.empty()) {
3369         unsigned Idx = 0, End = Record.size() - 1;
3370         bool ReachedEOFWhileSkipping = Record[Idx++];
3371         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3372         if (ReachedEOFWhileSkipping) {
3373           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3374           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3375           bool FoundNonSkipPortion = Record[Idx++];
3376           bool FoundElse = Record[Idx++];
3377           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3378           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3379                            FoundElse, ElseLoc);
3380         }
3381         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3382         while (Idx < End) {
3383           auto Loc = ReadSourceLocation(F, Record, Idx);
3384           bool WasSkipping = Record[Idx++];
3385           bool FoundNonSkip = Record[Idx++];
3386           bool FoundElse = Record[Idx++];
3387           ConditionalStack.push_back(
3388               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3389         }
3390         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3391       }
3392       break;
3393 
3394     case PP_COUNTER_VALUE:
3395       if (!Record.empty() && Listener)
3396         Listener->ReadCounter(F, Record[0]);
3397       break;
3398 
3399     case FILE_SORTED_DECLS:
3400       F.FileSortedDecls = (const DeclID *)Blob.data();
3401       F.NumFileSortedDecls = Record[0];
3402       break;
3403 
3404     case SOURCE_LOCATION_OFFSETS: {
3405       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3406       F.LocalNumSLocEntries = Record[0];
3407       unsigned SLocSpaceSize = Record[1];
3408       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3409       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3410           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3411                                               SLocSpaceSize);
3412       if (!F.SLocEntryBaseID) {
3413         Error("ran out of source locations");
3414         break;
3415       }
3416       // Make our entry in the range map. BaseID is negative and growing, so
3417       // we invert it. Because we invert it, though, we need the other end of
3418       // the range.
3419       unsigned RangeStart =
3420           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3421       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3422       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3423 
3424       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3425       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3426       GlobalSLocOffsetMap.insert(
3427           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3428                            - SLocSpaceSize,&F));
3429 
3430       // Initialize the remapping table.
3431       // Invalid stays invalid.
3432       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3433       // This module. Base was 2 when being compiled.
3434       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3435                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3436 
3437       TotalNumSLocEntries += F.LocalNumSLocEntries;
3438       break;
3439     }
3440 
3441     case MODULE_OFFSET_MAP:
3442       F.ModuleOffsetMap = Blob;
3443       break;
3444 
3445     case SOURCE_MANAGER_LINE_TABLE:
3446       if (ParseLineTable(F, Record)) {
3447         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3448         return Failure;
3449       }
3450       break;
3451 
3452     case SOURCE_LOCATION_PRELOADS: {
3453       // Need to transform from the local view (1-based IDs) to the global view,
3454       // which is based off F.SLocEntryBaseID.
3455       if (!F.PreloadSLocEntries.empty()) {
3456         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3457         return Failure;
3458       }
3459 
3460       F.PreloadSLocEntries.swap(Record);
3461       break;
3462     }
3463 
3464     case EXT_VECTOR_DECLS:
3465       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3466         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3467       break;
3468 
3469     case VTABLE_USES:
3470       if (Record.size() % 3 != 0) {
3471         Error("Invalid VTABLE_USES record");
3472         return Failure;
3473       }
3474 
3475       // Later tables overwrite earlier ones.
3476       // FIXME: Modules will have some trouble with this. This is clearly not
3477       // the right way to do this.
3478       VTableUses.clear();
3479 
3480       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3481         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3482         VTableUses.push_back(
3483           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3484         VTableUses.push_back(Record[Idx++]);
3485       }
3486       break;
3487 
3488     case PENDING_IMPLICIT_INSTANTIATIONS:
3489       if (PendingInstantiations.size() % 2 != 0) {
3490         Error("Invalid existing PendingInstantiations");
3491         return Failure;
3492       }
3493 
3494       if (Record.size() % 2 != 0) {
3495         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3496         return Failure;
3497       }
3498 
3499       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3500         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3501         PendingInstantiations.push_back(
3502           ReadSourceLocation(F, Record, I).getRawEncoding());
3503       }
3504       break;
3505 
3506     case SEMA_DECL_REFS:
3507       if (Record.size() != 3) {
3508         Error("Invalid SEMA_DECL_REFS block");
3509         return Failure;
3510       }
3511       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3512         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3513       break;
3514 
3515     case PPD_ENTITIES_OFFSETS: {
3516       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3517       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3518       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3519 
3520       unsigned LocalBasePreprocessedEntityID = Record[0];
3521 
3522       unsigned StartingID;
3523       if (!PP.getPreprocessingRecord())
3524         PP.createPreprocessingRecord();
3525       if (!PP.getPreprocessingRecord()->getExternalSource())
3526         PP.getPreprocessingRecord()->SetExternalSource(*this);
3527       StartingID
3528         = PP.getPreprocessingRecord()
3529             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3530       F.BasePreprocessedEntityID = StartingID;
3531 
3532       if (F.NumPreprocessedEntities > 0) {
3533         // Introduce the global -> local mapping for preprocessed entities in
3534         // this module.
3535         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3536 
3537         // Introduce the local -> global mapping for preprocessed entities in
3538         // this module.
3539         F.PreprocessedEntityRemap.insertOrReplace(
3540           std::make_pair(LocalBasePreprocessedEntityID,
3541             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3542       }
3543 
3544       break;
3545     }
3546 
3547     case PPD_SKIPPED_RANGES: {
3548       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3549       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3550       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3551 
3552       if (!PP.getPreprocessingRecord())
3553         PP.createPreprocessingRecord();
3554       if (!PP.getPreprocessingRecord()->getExternalSource())
3555         PP.getPreprocessingRecord()->SetExternalSource(*this);
3556       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3557           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3558 
3559       if (F.NumPreprocessedSkippedRanges > 0)
3560         GlobalSkippedRangeMap.insert(
3561             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3562       break;
3563     }
3564 
3565     case DECL_UPDATE_OFFSETS:
3566       if (Record.size() % 2 != 0) {
3567         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3568         return Failure;
3569       }
3570       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3571         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3572         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3573 
3574         // If we've already loaded the decl, perform the updates when we finish
3575         // loading this block.
3576         if (Decl *D = GetExistingDecl(ID))
3577           PendingUpdateRecords.push_back(
3578               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3579       }
3580       break;
3581 
3582     case OBJC_CATEGORIES_MAP:
3583       if (F.LocalNumObjCCategoriesInMap != 0) {
3584         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3585         return Failure;
3586       }
3587 
3588       F.LocalNumObjCCategoriesInMap = Record[0];
3589       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3590       break;
3591 
3592     case OBJC_CATEGORIES:
3593       F.ObjCCategories.swap(Record);
3594       break;
3595 
3596     case CUDA_SPECIAL_DECL_REFS:
3597       // Later tables overwrite earlier ones.
3598       // FIXME: Modules will have trouble with this.
3599       CUDASpecialDeclRefs.clear();
3600       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3601         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3602       break;
3603 
3604     case HEADER_SEARCH_TABLE:
3605       F.HeaderFileInfoTableData = Blob.data();
3606       F.LocalNumHeaderFileInfos = Record[1];
3607       if (Record[0]) {
3608         F.HeaderFileInfoTable
3609           = HeaderFileInfoLookupTable::Create(
3610                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3611                    (const unsigned char *)F.HeaderFileInfoTableData,
3612                    HeaderFileInfoTrait(*this, F,
3613                                        &PP.getHeaderSearchInfo(),
3614                                        Blob.data() + Record[2]));
3615 
3616         PP.getHeaderSearchInfo().SetExternalSource(this);
3617         if (!PP.getHeaderSearchInfo().getExternalLookup())
3618           PP.getHeaderSearchInfo().SetExternalLookup(this);
3619       }
3620       break;
3621 
3622     case FP_PRAGMA_OPTIONS:
3623       // Later tables overwrite earlier ones.
3624       FPPragmaOptions.swap(Record);
3625       break;
3626 
3627     case OPENCL_EXTENSIONS:
3628       for (unsigned I = 0, E = Record.size(); I != E; ) {
3629         auto Name = ReadString(Record, I);
3630         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3631         OptInfo.Supported = Record[I++] != 0;
3632         OptInfo.Enabled = Record[I++] != 0;
3633         OptInfo.WithPragma = Record[I++] != 0;
3634         OptInfo.Avail = Record[I++];
3635         OptInfo.Core = Record[I++];
3636         OptInfo.Opt = Record[I++];
3637       }
3638       break;
3639 
3640     case TENTATIVE_DEFINITIONS:
3641       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3642         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3643       break;
3644 
3645     case KNOWN_NAMESPACES:
3646       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3647         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3648       break;
3649 
3650     case UNDEFINED_BUT_USED:
3651       if (UndefinedButUsed.size() % 2 != 0) {
3652         Error("Invalid existing UndefinedButUsed");
3653         return Failure;
3654       }
3655 
3656       if (Record.size() % 2 != 0) {
3657         Error("invalid undefined-but-used record");
3658         return Failure;
3659       }
3660       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3661         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3662         UndefinedButUsed.push_back(
3663             ReadSourceLocation(F, Record, I).getRawEncoding());
3664       }
3665       break;
3666 
3667     case DELETE_EXPRS_TO_ANALYZE:
3668       for (unsigned I = 0, N = Record.size(); I != N;) {
3669         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3670         const uint64_t Count = Record[I++];
3671         DelayedDeleteExprs.push_back(Count);
3672         for (uint64_t C = 0; C < Count; ++C) {
3673           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3674           bool IsArrayForm = Record[I++] == 1;
3675           DelayedDeleteExprs.push_back(IsArrayForm);
3676         }
3677       }
3678       break;
3679 
3680     case IMPORTED_MODULES:
3681       if (!F.isModule()) {
3682         // If we aren't loading a module (which has its own exports), make
3683         // all of the imported modules visible.
3684         // FIXME: Deal with macros-only imports.
3685         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3686           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3687           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3688           if (GlobalID) {
3689             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3690             if (DeserializationListener)
3691               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3692           }
3693         }
3694       }
3695       break;
3696 
3697     case MACRO_OFFSET: {
3698       if (F.LocalNumMacros != 0) {
3699         Error("duplicate MACRO_OFFSET record in AST file");
3700         return Failure;
3701       }
3702       F.MacroOffsets = (const uint32_t *)Blob.data();
3703       F.LocalNumMacros = Record[0];
3704       unsigned LocalBaseMacroID = Record[1];
3705       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3706       F.BaseMacroID = getTotalNumMacros();
3707 
3708       if (F.LocalNumMacros > 0) {
3709         // Introduce the global -> local mapping for macros within this module.
3710         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3711 
3712         // Introduce the local -> global mapping for macros within this module.
3713         F.MacroRemap.insertOrReplace(
3714           std::make_pair(LocalBaseMacroID,
3715                          F.BaseMacroID - LocalBaseMacroID));
3716 
3717         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3718       }
3719       break;
3720     }
3721 
3722     case LATE_PARSED_TEMPLATE:
3723       LateParsedTemplates.emplace_back(
3724           std::piecewise_construct, std::forward_as_tuple(&F),
3725           std::forward_as_tuple(Record.begin(), Record.end()));
3726       break;
3727 
3728     case OPTIMIZE_PRAGMA_OPTIONS:
3729       if (Record.size() != 1) {
3730         Error("invalid pragma optimize record");
3731         return Failure;
3732       }
3733       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3734       break;
3735 
3736     case MSSTRUCT_PRAGMA_OPTIONS:
3737       if (Record.size() != 1) {
3738         Error("invalid pragma ms_struct record");
3739         return Failure;
3740       }
3741       PragmaMSStructState = Record[0];
3742       break;
3743 
3744     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3745       if (Record.size() != 2) {
3746         Error("invalid pragma ms_struct record");
3747         return Failure;
3748       }
3749       PragmaMSPointersToMembersState = Record[0];
3750       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3751       break;
3752 
3753     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3754       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3755         UnusedLocalTypedefNameCandidates.push_back(
3756             getGlobalDeclID(F, Record[I]));
3757       break;
3758 
3759     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3760       if (Record.size() != 1) {
3761         Error("invalid cuda pragma options record");
3762         return Failure;
3763       }
3764       ForceCUDAHostDeviceDepth = Record[0];
3765       break;
3766 
3767     case ALIGN_PACK_PRAGMA_OPTIONS: {
3768       if (Record.size() < 3) {
3769         Error("invalid pragma pack record");
3770         return Failure;
3771       }
3772       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3773       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3774       unsigned NumStackEntries = Record[2];
3775       unsigned Idx = 3;
3776       // Reset the stack when importing a new module.
3777       PragmaAlignPackStack.clear();
3778       for (unsigned I = 0; I < NumStackEntries; ++I) {
3779         PragmaAlignPackStackEntry Entry;
3780         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3781         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3782         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3783         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3784         Entry.SlotLabel = PragmaAlignPackStrings.back();
3785         PragmaAlignPackStack.push_back(Entry);
3786       }
3787       break;
3788     }
3789 
3790     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3791       if (Record.size() < 3) {
3792         Error("invalid pragma pack record");
3793         return Failure;
3794       }
3795       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3796       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3797       unsigned NumStackEntries = Record[2];
3798       unsigned Idx = 3;
3799       // Reset the stack when importing a new module.
3800       FpPragmaStack.clear();
3801       for (unsigned I = 0; I < NumStackEntries; ++I) {
3802         FpPragmaStackEntry Entry;
3803         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3804         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3805         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3806         FpPragmaStrings.push_back(ReadString(Record, Idx));
3807         Entry.SlotLabel = FpPragmaStrings.back();
3808         FpPragmaStack.push_back(Entry);
3809       }
3810       break;
3811     }
3812 
3813     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3814       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3815         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3816       break;
3817     }
3818   }
3819 }
3820 
3821 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3822   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3823 
3824   // Additional remapping information.
3825   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3826   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3827   F.ModuleOffsetMap = StringRef();
3828 
3829   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3830   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3831     F.SLocRemap.insert(std::make_pair(0U, 0));
3832     F.SLocRemap.insert(std::make_pair(2U, 1));
3833   }
3834 
3835   // Continuous range maps we may be updating in our module.
3836   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3837   RemapBuilder SLocRemap(F.SLocRemap);
3838   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3839   RemapBuilder MacroRemap(F.MacroRemap);
3840   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3841   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3842   RemapBuilder SelectorRemap(F.SelectorRemap);
3843   RemapBuilder DeclRemap(F.DeclRemap);
3844   RemapBuilder TypeRemap(F.TypeRemap);
3845 
3846   while (Data < DataEnd) {
3847     // FIXME: Looking up dependency modules by filename is horrible. Let's
3848     // start fixing this with prebuilt, explicit and implicit modules and see
3849     // how it goes...
3850     using namespace llvm::support;
3851     ModuleKind Kind = static_cast<ModuleKind>(
3852       endian::readNext<uint8_t, little, unaligned>(Data));
3853     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3854     StringRef Name = StringRef((const char*)Data, Len);
3855     Data += Len;
3856     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3857                               Kind == MK_ImplicitModule
3858                           ? ModuleMgr.lookupByModuleName(Name)
3859                           : ModuleMgr.lookupByFileName(Name));
3860     if (!OM) {
3861       std::string Msg =
3862           "SourceLocation remap refers to unknown module, cannot find ";
3863       Msg.append(std::string(Name));
3864       Error(Msg);
3865       return;
3866     }
3867 
3868     uint32_t SLocOffset =
3869         endian::readNext<uint32_t, little, unaligned>(Data);
3870     uint32_t IdentifierIDOffset =
3871         endian::readNext<uint32_t, little, unaligned>(Data);
3872     uint32_t MacroIDOffset =
3873         endian::readNext<uint32_t, little, unaligned>(Data);
3874     uint32_t PreprocessedEntityIDOffset =
3875         endian::readNext<uint32_t, little, unaligned>(Data);
3876     uint32_t SubmoduleIDOffset =
3877         endian::readNext<uint32_t, little, unaligned>(Data);
3878     uint32_t SelectorIDOffset =
3879         endian::readNext<uint32_t, little, unaligned>(Data);
3880     uint32_t DeclIDOffset =
3881         endian::readNext<uint32_t, little, unaligned>(Data);
3882     uint32_t TypeIndexOffset =
3883         endian::readNext<uint32_t, little, unaligned>(Data);
3884 
3885     uint32_t None = std::numeric_limits<uint32_t>::max();
3886 
3887     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3888                          RemapBuilder &Remap) {
3889       if (Offset != None)
3890         Remap.insert(std::make_pair(Offset,
3891                                     static_cast<int>(BaseOffset - Offset)));
3892     };
3893     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3894     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3895     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3896     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3897               PreprocessedEntityRemap);
3898     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3899     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3900     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3901     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3902 
3903     // Global -> local mappings.
3904     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3905   }
3906 }
3907 
3908 ASTReader::ASTReadResult
3909 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3910                                   const ModuleFile *ImportedBy,
3911                                   unsigned ClientLoadCapabilities) {
3912   unsigned Idx = 0;
3913   F.ModuleMapPath = ReadPath(F, Record, Idx);
3914 
3915   // Try to resolve ModuleName in the current header search context and
3916   // verify that it is found in the same module map file as we saved. If the
3917   // top-level AST file is a main file, skip this check because there is no
3918   // usable header search context.
3919   assert(!F.ModuleName.empty() &&
3920          "MODULE_NAME should come before MODULE_MAP_FILE");
3921   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3922     // An implicitly-loaded module file should have its module listed in some
3923     // module map file that we've already loaded.
3924     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3925     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3926     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3927     // Don't emit module relocation error if we have -fno-validate-pch
3928     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3929               DisableValidationForModuleKind::Module) &&
3930         !ModMap) {
3931       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3932         if (auto ASTFE = M ? M->getASTFile() : None) {
3933           // This module was defined by an imported (explicit) module.
3934           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3935                                                << ASTFE->getName();
3936         } else {
3937           // This module was built with a different module map.
3938           Diag(diag::err_imported_module_not_found)
3939               << F.ModuleName << F.FileName
3940               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3941               << !ImportedBy;
3942           // In case it was imported by a PCH, there's a chance the user is
3943           // just missing to include the search path to the directory containing
3944           // the modulemap.
3945           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3946             Diag(diag::note_imported_by_pch_module_not_found)
3947                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3948         }
3949       }
3950       return OutOfDate;
3951     }
3952 
3953     assert(M && M->Name == F.ModuleName && "found module with different name");
3954 
3955     // Check the primary module map file.
3956     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3957     if (!StoredModMap || *StoredModMap != ModMap) {
3958       assert(ModMap && "found module is missing module map file");
3959       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3960              "top-level import should be verified");
3961       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3962       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3963         Diag(diag::err_imported_module_modmap_changed)
3964             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3965             << ModMap->getName() << F.ModuleMapPath << NotImported;
3966       return OutOfDate;
3967     }
3968 
3969     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3970     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3971       // FIXME: we should use input files rather than storing names.
3972       std::string Filename = ReadPath(F, Record, Idx);
3973       auto F = FileMgr.getFile(Filename, false, false);
3974       if (!F) {
3975         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3976           Error("could not find file '" + Filename +"' referenced by AST file");
3977         return OutOfDate;
3978       }
3979       AdditionalStoredMaps.insert(*F);
3980     }
3981 
3982     // Check any additional module map files (e.g. module.private.modulemap)
3983     // that are not in the pcm.
3984     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3985       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3986         // Remove files that match
3987         // Note: SmallPtrSet::erase is really remove
3988         if (!AdditionalStoredMaps.erase(ModMap)) {
3989           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3990             Diag(diag::err_module_different_modmap)
3991               << F.ModuleName << /*new*/0 << ModMap->getName();
3992           return OutOfDate;
3993         }
3994       }
3995     }
3996 
3997     // Check any additional module map files that are in the pcm, but not
3998     // found in header search. Cases that match are already removed.
3999     for (const FileEntry *ModMap : AdditionalStoredMaps) {
4000       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4001         Diag(diag::err_module_different_modmap)
4002           << F.ModuleName << /*not new*/1 << ModMap->getName();
4003       return OutOfDate;
4004     }
4005   }
4006 
4007   if (Listener)
4008     Listener->ReadModuleMapFile(F.ModuleMapPath);
4009   return Success;
4010 }
4011 
4012 /// Move the given method to the back of the global list of methods.
4013 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4014   // Find the entry for this selector in the method pool.
4015   Sema::GlobalMethodPool::iterator Known
4016     = S.MethodPool.find(Method->getSelector());
4017   if (Known == S.MethodPool.end())
4018     return;
4019 
4020   // Retrieve the appropriate method list.
4021   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4022                                                     : Known->second.second;
4023   bool Found = false;
4024   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4025     if (!Found) {
4026       if (List->getMethod() == Method) {
4027         Found = true;
4028       } else {
4029         // Keep searching.
4030         continue;
4031       }
4032     }
4033 
4034     if (List->getNext())
4035       List->setMethod(List->getNext()->getMethod());
4036     else
4037       List->setMethod(Method);
4038   }
4039 }
4040 
4041 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4042   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4043   for (Decl *D : Names) {
4044     bool wasHidden = !D->isUnconditionallyVisible();
4045     D->setVisibleDespiteOwningModule();
4046 
4047     if (wasHidden && SemaObj) {
4048       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4049         moveMethodToBackOfGlobalList(*SemaObj, Method);
4050       }
4051     }
4052   }
4053 }
4054 
4055 void ASTReader::makeModuleVisible(Module *Mod,
4056                                   Module::NameVisibilityKind NameVisibility,
4057                                   SourceLocation ImportLoc) {
4058   llvm::SmallPtrSet<Module *, 4> Visited;
4059   SmallVector<Module *, 4> Stack;
4060   Stack.push_back(Mod);
4061   while (!Stack.empty()) {
4062     Mod = Stack.pop_back_val();
4063 
4064     if (NameVisibility <= Mod->NameVisibility) {
4065       // This module already has this level of visibility (or greater), so
4066       // there is nothing more to do.
4067       continue;
4068     }
4069 
4070     if (Mod->isUnimportable()) {
4071       // Modules that aren't importable cannot be made visible.
4072       continue;
4073     }
4074 
4075     // Update the module's name visibility.
4076     Mod->NameVisibility = NameVisibility;
4077 
4078     // If we've already deserialized any names from this module,
4079     // mark them as visible.
4080     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4081     if (Hidden != HiddenNamesMap.end()) {
4082       auto HiddenNames = std::move(*Hidden);
4083       HiddenNamesMap.erase(Hidden);
4084       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4085       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4086              "making names visible added hidden names");
4087     }
4088 
4089     // Push any exported modules onto the stack to be marked as visible.
4090     SmallVector<Module *, 16> Exports;
4091     Mod->getExportedModules(Exports);
4092     for (SmallVectorImpl<Module *>::iterator
4093            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4094       Module *Exported = *I;
4095       if (Visited.insert(Exported).second)
4096         Stack.push_back(Exported);
4097     }
4098   }
4099 }
4100 
4101 /// We've merged the definition \p MergedDef into the existing definition
4102 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4103 /// visible.
4104 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4105                                           NamedDecl *MergedDef) {
4106   if (!Def->isUnconditionallyVisible()) {
4107     // If MergedDef is visible or becomes visible, make the definition visible.
4108     if (MergedDef->isUnconditionallyVisible())
4109       Def->setVisibleDespiteOwningModule();
4110     else {
4111       getContext().mergeDefinitionIntoModule(
4112           Def, MergedDef->getImportedOwningModule(),
4113           /*NotifyListeners*/ false);
4114       PendingMergedDefinitionsToDeduplicate.insert(Def);
4115     }
4116   }
4117 }
4118 
4119 bool ASTReader::loadGlobalIndex() {
4120   if (GlobalIndex)
4121     return false;
4122 
4123   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4124       !PP.getLangOpts().Modules)
4125     return true;
4126 
4127   // Try to load the global index.
4128   TriedLoadingGlobalIndex = true;
4129   StringRef ModuleCachePath
4130     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4131   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4132       GlobalModuleIndex::readIndex(ModuleCachePath);
4133   if (llvm::Error Err = std::move(Result.second)) {
4134     assert(!Result.first);
4135     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4136     return true;
4137   }
4138 
4139   GlobalIndex.reset(Result.first);
4140   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4141   return false;
4142 }
4143 
4144 bool ASTReader::isGlobalIndexUnavailable() const {
4145   return PP.getLangOpts().Modules && UseGlobalIndex &&
4146          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4147 }
4148 
4149 static void updateModuleTimestamp(ModuleFile &MF) {
4150   // Overwrite the timestamp file contents so that file's mtime changes.
4151   std::string TimestampFilename = MF.getTimestampFilename();
4152   std::error_code EC;
4153   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4154                           llvm::sys::fs::OF_TextWithCRLF);
4155   if (EC)
4156     return;
4157   OS << "Timestamp file\n";
4158   OS.close();
4159   OS.clear_error(); // Avoid triggering a fatal error.
4160 }
4161 
4162 /// Given a cursor at the start of an AST file, scan ahead and drop the
4163 /// cursor into the start of the given block ID, returning false on success and
4164 /// true on failure.
4165 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4166   while (true) {
4167     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4168     if (!MaybeEntry) {
4169       // FIXME this drops errors on the floor.
4170       consumeError(MaybeEntry.takeError());
4171       return true;
4172     }
4173     llvm::BitstreamEntry Entry = MaybeEntry.get();
4174 
4175     switch (Entry.Kind) {
4176     case llvm::BitstreamEntry::Error:
4177     case llvm::BitstreamEntry::EndBlock:
4178       return true;
4179 
4180     case llvm::BitstreamEntry::Record:
4181       // Ignore top-level records.
4182       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4183         break;
4184       else {
4185         // FIXME this drops errors on the floor.
4186         consumeError(Skipped.takeError());
4187         return true;
4188       }
4189 
4190     case llvm::BitstreamEntry::SubBlock:
4191       if (Entry.ID == BlockID) {
4192         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4193           // FIXME this drops the error on the floor.
4194           consumeError(std::move(Err));
4195           return true;
4196         }
4197         // Found it!
4198         return false;
4199       }
4200 
4201       if (llvm::Error Err = Cursor.SkipBlock()) {
4202         // FIXME this drops the error on the floor.
4203         consumeError(std::move(Err));
4204         return true;
4205       }
4206     }
4207   }
4208 }
4209 
4210 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4211                                             ModuleKind Type,
4212                                             SourceLocation ImportLoc,
4213                                             unsigned ClientLoadCapabilities,
4214                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4215   llvm::SaveAndRestore<SourceLocation>
4216     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4217   llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4218       CurrentDeserializingModuleKind, Type);
4219 
4220   // Defer any pending actions until we get to the end of reading the AST file.
4221   Deserializing AnASTFile(this);
4222 
4223   // Bump the generation number.
4224   unsigned PreviousGeneration = 0;
4225   if (ContextObj)
4226     PreviousGeneration = incrementGeneration(*ContextObj);
4227 
4228   unsigned NumModules = ModuleMgr.size();
4229   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4230     assert(ReadResult && "expected to return error");
4231     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4232                             PP.getLangOpts().Modules
4233                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4234                                 : nullptr);
4235 
4236     // If we find that any modules are unusable, the global index is going
4237     // to be out-of-date. Just remove it.
4238     GlobalIndex.reset();
4239     ModuleMgr.setGlobalIndex(nullptr);
4240     return ReadResult;
4241   };
4242 
4243   SmallVector<ImportedModule, 4> Loaded;
4244   switch (ASTReadResult ReadResult =
4245               ReadASTCore(FileName, Type, ImportLoc,
4246                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4247                           ASTFileSignature(), ClientLoadCapabilities)) {
4248   case Failure:
4249   case Missing:
4250   case OutOfDate:
4251   case VersionMismatch:
4252   case ConfigurationMismatch:
4253   case HadErrors:
4254     return removeModulesAndReturn(ReadResult);
4255   case Success:
4256     break;
4257   }
4258 
4259   // Here comes stuff that we only do once the entire chain is loaded.
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 (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4268       return removeModulesAndReturn(Result);
4269 
4270     // The AST block should always have a definition for the main module.
4271     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4272       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4273       return removeModulesAndReturn(Failure);
4274     }
4275 
4276     // Read the extension blocks.
4277     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4278       if (ASTReadResult Result = ReadExtensionBlock(F))
4279         return removeModulesAndReturn(Result);
4280     }
4281 
4282     // Once read, set the ModuleFile bit base offset and update the size in
4283     // bits of all files we've seen.
4284     F.GlobalBitOffset = TotalModulesSizeInBits;
4285     TotalModulesSizeInBits += F.SizeInBits;
4286     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4287   }
4288 
4289   // Preload source locations and interesting indentifiers.
4290   for (ImportedModule &M : Loaded) {
4291     ModuleFile &F = *M.Mod;
4292 
4293     // Preload SLocEntries.
4294     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4295       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4296       // Load it through the SourceManager and don't call ReadSLocEntry()
4297       // directly because the entry may have already been loaded in which case
4298       // calling ReadSLocEntry() directly would trigger an assertion in
4299       // SourceManager.
4300       SourceMgr.getLoadedSLocEntryByID(Index);
4301     }
4302 
4303     // Map the original source file ID into the ID space of the current
4304     // compilation.
4305     if (F.OriginalSourceFileID.isValid()) {
4306       F.OriginalSourceFileID = FileID::get(
4307           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4308     }
4309 
4310     // Preload all the pending interesting identifiers by marking them out of
4311     // date.
4312     for (auto Offset : F.PreloadIdentifierOffsets) {
4313       const unsigned char *Data = F.IdentifierTableData + Offset;
4314 
4315       ASTIdentifierLookupTrait Trait(*this, F);
4316       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4317       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4318       auto &II = PP.getIdentifierTable().getOwn(Key);
4319       II.setOutOfDate(true);
4320 
4321       // Mark this identifier as being from an AST file so that we can track
4322       // whether we need to serialize it.
4323       markIdentifierFromAST(*this, II);
4324 
4325       // Associate the ID with the identifier so that the writer can reuse it.
4326       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4327       SetIdentifierInfo(ID, &II);
4328     }
4329   }
4330 
4331   // Setup the import locations and notify the module manager that we've
4332   // committed to these module files.
4333   for (ImportedModule &M : Loaded) {
4334     ModuleFile &F = *M.Mod;
4335 
4336     ModuleMgr.moduleFileAccepted(&F);
4337 
4338     // Set the import location.
4339     F.DirectImportLoc = ImportLoc;
4340     // FIXME: We assume that locations from PCH / preamble do not need
4341     // any translation.
4342     if (!M.ImportedBy)
4343       F.ImportLoc = M.ImportLoc;
4344     else
4345       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4346   }
4347 
4348   if (!PP.getLangOpts().CPlusPlus ||
4349       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4350        Type != MK_PrebuiltModule)) {
4351     // Mark all of the identifiers in the identifier table as being out of date,
4352     // so that various accessors know to check the loaded modules when the
4353     // identifier is used.
4354     //
4355     // For C++ modules, we don't need information on many identifiers (just
4356     // those that provide macros or are poisoned), so we mark all of
4357     // the interesting ones via PreloadIdentifierOffsets.
4358     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4359                                 IdEnd = PP.getIdentifierTable().end();
4360          Id != IdEnd; ++Id)
4361       Id->second->setOutOfDate(true);
4362   }
4363   // Mark selectors as out of date.
4364   for (auto Sel : SelectorGeneration)
4365     SelectorOutOfDate[Sel.first] = true;
4366 
4367   // Resolve any unresolved module exports.
4368   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4369     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4370     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4371     Module *ResolvedMod = getSubmodule(GlobalID);
4372 
4373     switch (Unresolved.Kind) {
4374     case UnresolvedModuleRef::Conflict:
4375       if (ResolvedMod) {
4376         Module::Conflict Conflict;
4377         Conflict.Other = ResolvedMod;
4378         Conflict.Message = Unresolved.String.str();
4379         Unresolved.Mod->Conflicts.push_back(Conflict);
4380       }
4381       continue;
4382 
4383     case UnresolvedModuleRef::Import:
4384       if (ResolvedMod)
4385         Unresolved.Mod->Imports.insert(ResolvedMod);
4386       continue;
4387 
4388     case UnresolvedModuleRef::Export:
4389       if (ResolvedMod || Unresolved.IsWildcard)
4390         Unresolved.Mod->Exports.push_back(
4391           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4392       continue;
4393     }
4394   }
4395   UnresolvedModuleRefs.clear();
4396 
4397   if (Imported)
4398     Imported->append(ImportedModules.begin(),
4399                      ImportedModules.end());
4400 
4401   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4402   // Might be unnecessary as use declarations are only used to build the
4403   // module itself.
4404 
4405   if (ContextObj)
4406     InitializeContext();
4407 
4408   if (SemaObj)
4409     UpdateSema();
4410 
4411   if (DeserializationListener)
4412     DeserializationListener->ReaderInitialized(this);
4413 
4414   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4415   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4416     // If this AST file is a precompiled preamble, then set the
4417     // preamble file ID of the source manager to the file source file
4418     // from which the preamble was built.
4419     if (Type == MK_Preamble) {
4420       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4421     } else if (Type == MK_MainFile) {
4422       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4423     }
4424   }
4425 
4426   // For any Objective-C class definitions we have already loaded, make sure
4427   // that we load any additional categories.
4428   if (ContextObj) {
4429     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4430       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4431                          ObjCClassesLoaded[I],
4432                          PreviousGeneration);
4433     }
4434   }
4435 
4436   if (PP.getHeaderSearchInfo()
4437           .getHeaderSearchOpts()
4438           .ModulesValidateOncePerBuildSession) {
4439     // Now we are certain that the module and all modules it depends on are
4440     // up to date.  Create or update timestamp files for modules that are
4441     // located in the module cache (not for PCH files that could be anywhere
4442     // in the filesystem).
4443     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4444       ImportedModule &M = Loaded[I];
4445       if (M.Mod->Kind == MK_ImplicitModule) {
4446         updateModuleTimestamp(*M.Mod);
4447       }
4448     }
4449   }
4450 
4451   return Success;
4452 }
4453 
4454 static ASTFileSignature readASTFileSignature(StringRef PCH);
4455 
4456 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4457 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4458   // FIXME checking magic headers is done in other places such as
4459   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4460   // always done the same. Unify it all with a helper.
4461   if (!Stream.canSkipToPos(4))
4462     return llvm::createStringError(std::errc::illegal_byte_sequence,
4463                                    "file too small to contain AST file magic");
4464   for (unsigned C : {'C', 'P', 'C', 'H'})
4465     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4466       if (Res.get() != C)
4467         return llvm::createStringError(
4468             std::errc::illegal_byte_sequence,
4469             "file doesn't start with AST file magic");
4470     } else
4471       return Res.takeError();
4472   return llvm::Error::success();
4473 }
4474 
4475 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4476   switch (Kind) {
4477   case MK_PCH:
4478     return 0; // PCH
4479   case MK_ImplicitModule:
4480   case MK_ExplicitModule:
4481   case MK_PrebuiltModule:
4482     return 1; // module
4483   case MK_MainFile:
4484   case MK_Preamble:
4485     return 2; // main source file
4486   }
4487   llvm_unreachable("unknown module kind");
4488 }
4489 
4490 ASTReader::ASTReadResult
4491 ASTReader::ReadASTCore(StringRef FileName,
4492                        ModuleKind Type,
4493                        SourceLocation ImportLoc,
4494                        ModuleFile *ImportedBy,
4495                        SmallVectorImpl<ImportedModule> &Loaded,
4496                        off_t ExpectedSize, time_t ExpectedModTime,
4497                        ASTFileSignature ExpectedSignature,
4498                        unsigned ClientLoadCapabilities) {
4499   ModuleFile *M;
4500   std::string ErrorStr;
4501   ModuleManager::AddModuleResult AddResult
4502     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4503                           getGeneration(), ExpectedSize, ExpectedModTime,
4504                           ExpectedSignature, readASTFileSignature,
4505                           M, ErrorStr);
4506 
4507   switch (AddResult) {
4508   case ModuleManager::AlreadyLoaded:
4509     Diag(diag::remark_module_import)
4510         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4511         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4512     return Success;
4513 
4514   case ModuleManager::NewlyLoaded:
4515     // Load module file below.
4516     break;
4517 
4518   case ModuleManager::Missing:
4519     // The module file was missing; if the client can handle that, return
4520     // it.
4521     if (ClientLoadCapabilities & ARR_Missing)
4522       return Missing;
4523 
4524     // Otherwise, return an error.
4525     Diag(diag::err_ast_file_not_found)
4526         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4527         << ErrorStr;
4528     return Failure;
4529 
4530   case ModuleManager::OutOfDate:
4531     // We couldn't load the module file because it is out-of-date. If the
4532     // client can handle out-of-date, return it.
4533     if (ClientLoadCapabilities & ARR_OutOfDate)
4534       return OutOfDate;
4535 
4536     // Otherwise, return an error.
4537     Diag(diag::err_ast_file_out_of_date)
4538         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4539         << ErrorStr;
4540     return Failure;
4541   }
4542 
4543   assert(M && "Missing module file");
4544 
4545   bool ShouldFinalizePCM = false;
4546   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4547     auto &MC = getModuleManager().getModuleCache();
4548     if (ShouldFinalizePCM)
4549       MC.finalizePCM(FileName);
4550     else
4551       MC.tryToDropPCM(FileName);
4552   });
4553   ModuleFile &F = *M;
4554   BitstreamCursor &Stream = F.Stream;
4555   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4556   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4557 
4558   // Sniff for the signature.
4559   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4560     Diag(diag::err_ast_file_invalid)
4561         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4562     return Failure;
4563   }
4564 
4565   // This is used for compatibility with older PCH formats.
4566   bool HaveReadControlBlock = false;
4567   while (true) {
4568     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4569     if (!MaybeEntry) {
4570       Error(MaybeEntry.takeError());
4571       return Failure;
4572     }
4573     llvm::BitstreamEntry Entry = MaybeEntry.get();
4574 
4575     switch (Entry.Kind) {
4576     case llvm::BitstreamEntry::Error:
4577     case llvm::BitstreamEntry::Record:
4578     case llvm::BitstreamEntry::EndBlock:
4579       Error("invalid record at top-level of AST file");
4580       return Failure;
4581 
4582     case llvm::BitstreamEntry::SubBlock:
4583       break;
4584     }
4585 
4586     switch (Entry.ID) {
4587     case CONTROL_BLOCK_ID:
4588       HaveReadControlBlock = true;
4589       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4590       case Success:
4591         // Check that we didn't try to load a non-module AST file as a module.
4592         //
4593         // FIXME: Should we also perform the converse check? Loading a module as
4594         // a PCH file sort of works, but it's a bit wonky.
4595         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4596              Type == MK_PrebuiltModule) &&
4597             F.ModuleName.empty()) {
4598           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4599           if (Result != OutOfDate ||
4600               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4601             Diag(diag::err_module_file_not_module) << FileName;
4602           return Result;
4603         }
4604         break;
4605 
4606       case Failure: return Failure;
4607       case Missing: return Missing;
4608       case OutOfDate: return OutOfDate;
4609       case VersionMismatch: return VersionMismatch;
4610       case ConfigurationMismatch: return ConfigurationMismatch;
4611       case HadErrors: return HadErrors;
4612       }
4613       break;
4614 
4615     case AST_BLOCK_ID:
4616       if (!HaveReadControlBlock) {
4617         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4618           Diag(diag::err_pch_version_too_old);
4619         return VersionMismatch;
4620       }
4621 
4622       // Record that we've loaded this module.
4623       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4624       ShouldFinalizePCM = true;
4625       return Success;
4626 
4627     case UNHASHED_CONTROL_BLOCK_ID:
4628       // This block is handled using look-ahead during ReadControlBlock.  We
4629       // shouldn't get here!
4630       Error("malformed block record in AST file");
4631       return Failure;
4632 
4633     default:
4634       if (llvm::Error Err = Stream.SkipBlock()) {
4635         Error(std::move(Err));
4636         return Failure;
4637       }
4638       break;
4639     }
4640   }
4641 
4642   llvm_unreachable("unexpected break; expected return");
4643 }
4644 
4645 ASTReader::ASTReadResult
4646 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4647                                     unsigned ClientLoadCapabilities) {
4648   const HeaderSearchOptions &HSOpts =
4649       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4650   bool AllowCompatibleConfigurationMismatch =
4651       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4652   bool DisableValidation = shouldDisableValidationForFile(F);
4653 
4654   ASTReadResult Result = readUnhashedControlBlockImpl(
4655       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4656       Listener.get(),
4657       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4658 
4659   // If F was directly imported by another module, it's implicitly validated by
4660   // the importing module.
4661   if (DisableValidation || WasImportedBy ||
4662       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4663     return Success;
4664 
4665   if (Result == Failure) {
4666     Error("malformed block record in AST file");
4667     return Failure;
4668   }
4669 
4670   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4671     // If this module has already been finalized in the ModuleCache, we're stuck
4672     // with it; we can only load a single version of each module.
4673     //
4674     // This can happen when a module is imported in two contexts: in one, as a
4675     // user module; in another, as a system module (due to an import from
4676     // another module marked with the [system] flag).  It usually indicates a
4677     // bug in the module map: this module should also be marked with [system].
4678     //
4679     // If -Wno-system-headers (the default), and the first import is as a
4680     // system module, then validation will fail during the as-user import,
4681     // since -Werror flags won't have been validated.  However, it's reasonable
4682     // to treat this consistently as a system module.
4683     //
4684     // If -Wsystem-headers, the PCM on disk was built with
4685     // -Wno-system-headers, and the first import is as a user module, then
4686     // validation will fail during the as-system import since the PCM on disk
4687     // doesn't guarantee that -Werror was respected.  However, the -Werror
4688     // flags were checked during the initial as-user import.
4689     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4690       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4691       return Success;
4692     }
4693   }
4694 
4695   return Result;
4696 }
4697 
4698 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4699     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4700     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4701     bool ValidateDiagnosticOptions) {
4702   // Initialize a stream.
4703   BitstreamCursor Stream(StreamData);
4704 
4705   // Sniff for the signature.
4706   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4707     // FIXME this drops the error on the floor.
4708     consumeError(std::move(Err));
4709     return Failure;
4710   }
4711 
4712   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4713   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4714     return Failure;
4715 
4716   // Read all of the records in the options block.
4717   RecordData Record;
4718   ASTReadResult Result = Success;
4719   while (true) {
4720     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4721     if (!MaybeEntry) {
4722       // FIXME this drops the error on the floor.
4723       consumeError(MaybeEntry.takeError());
4724       return Failure;
4725     }
4726     llvm::BitstreamEntry Entry = MaybeEntry.get();
4727 
4728     switch (Entry.Kind) {
4729     case llvm::BitstreamEntry::Error:
4730     case llvm::BitstreamEntry::SubBlock:
4731       return Failure;
4732 
4733     case llvm::BitstreamEntry::EndBlock:
4734       return Result;
4735 
4736     case llvm::BitstreamEntry::Record:
4737       // The interesting case.
4738       break;
4739     }
4740 
4741     // Read and process a record.
4742     Record.clear();
4743     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4744     if (!MaybeRecordType) {
4745       // FIXME this drops the error.
4746       return Failure;
4747     }
4748     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4749     case SIGNATURE:
4750       if (F)
4751         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4752       break;
4753     case AST_BLOCK_HASH:
4754       if (F)
4755         F->ASTBlockHash =
4756             ASTFileSignature::create(Record.begin(), Record.end());
4757       break;
4758     case DIAGNOSTIC_OPTIONS: {
4759       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4760       if (Listener && ValidateDiagnosticOptions &&
4761           !AllowCompatibleConfigurationMismatch &&
4762           ParseDiagnosticOptions(Record, Complain, *Listener))
4763         Result = OutOfDate; // Don't return early.  Read the signature.
4764       break;
4765     }
4766     case DIAG_PRAGMA_MAPPINGS:
4767       if (!F)
4768         break;
4769       if (F->PragmaDiagMappings.empty())
4770         F->PragmaDiagMappings.swap(Record);
4771       else
4772         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4773                                      Record.begin(), Record.end());
4774       break;
4775     }
4776   }
4777 }
4778 
4779 /// Parse a record and blob containing module file extension metadata.
4780 static bool parseModuleFileExtensionMetadata(
4781               const SmallVectorImpl<uint64_t> &Record,
4782               StringRef Blob,
4783               ModuleFileExtensionMetadata &Metadata) {
4784   if (Record.size() < 4) return true;
4785 
4786   Metadata.MajorVersion = Record[0];
4787   Metadata.MinorVersion = Record[1];
4788 
4789   unsigned BlockNameLen = Record[2];
4790   unsigned UserInfoLen = Record[3];
4791 
4792   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4793 
4794   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4795   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4796                                   Blob.data() + BlockNameLen + UserInfoLen);
4797   return false;
4798 }
4799 
4800 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4801   BitstreamCursor &Stream = F.Stream;
4802 
4803   RecordData Record;
4804   while (true) {
4805     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4806     if (!MaybeEntry) {
4807       Error(MaybeEntry.takeError());
4808       return Failure;
4809     }
4810     llvm::BitstreamEntry Entry = MaybeEntry.get();
4811 
4812     switch (Entry.Kind) {
4813     case llvm::BitstreamEntry::SubBlock:
4814       if (llvm::Error Err = Stream.SkipBlock()) {
4815         Error(std::move(Err));
4816         return Failure;
4817       }
4818       continue;
4819 
4820     case llvm::BitstreamEntry::EndBlock:
4821       return Success;
4822 
4823     case llvm::BitstreamEntry::Error:
4824       return HadErrors;
4825 
4826     case llvm::BitstreamEntry::Record:
4827       break;
4828     }
4829 
4830     Record.clear();
4831     StringRef Blob;
4832     Expected<unsigned> MaybeRecCode =
4833         Stream.readRecord(Entry.ID, Record, &Blob);
4834     if (!MaybeRecCode) {
4835       Error(MaybeRecCode.takeError());
4836       return Failure;
4837     }
4838     switch (MaybeRecCode.get()) {
4839     case EXTENSION_METADATA: {
4840       ModuleFileExtensionMetadata Metadata;
4841       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4842         Error("malformed EXTENSION_METADATA in AST file");
4843         return Failure;
4844       }
4845 
4846       // Find a module file extension with this block name.
4847       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4848       if (Known == ModuleFileExtensions.end()) break;
4849 
4850       // Form a reader.
4851       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4852                                                              F, Stream)) {
4853         F.ExtensionReaders.push_back(std::move(Reader));
4854       }
4855 
4856       break;
4857     }
4858     }
4859   }
4860 
4861   return Success;
4862 }
4863 
4864 void ASTReader::InitializeContext() {
4865   assert(ContextObj && "no context to initialize");
4866   ASTContext &Context = *ContextObj;
4867 
4868   // If there's a listener, notify them that we "read" the translation unit.
4869   if (DeserializationListener)
4870     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4871                                       Context.getTranslationUnitDecl());
4872 
4873   // FIXME: Find a better way to deal with collisions between these
4874   // built-in types. Right now, we just ignore the problem.
4875 
4876   // Load the special types.
4877   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4878     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4879       if (!Context.CFConstantStringTypeDecl)
4880         Context.setCFConstantStringType(GetType(String));
4881     }
4882 
4883     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4884       QualType FileType = GetType(File);
4885       if (FileType.isNull()) {
4886         Error("FILE type is NULL");
4887         return;
4888       }
4889 
4890       if (!Context.FILEDecl) {
4891         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4892           Context.setFILEDecl(Typedef->getDecl());
4893         else {
4894           const TagType *Tag = FileType->getAs<TagType>();
4895           if (!Tag) {
4896             Error("Invalid FILE type in AST file");
4897             return;
4898           }
4899           Context.setFILEDecl(Tag->getDecl());
4900         }
4901       }
4902     }
4903 
4904     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4905       QualType Jmp_bufType = GetType(Jmp_buf);
4906       if (Jmp_bufType.isNull()) {
4907         Error("jmp_buf type is NULL");
4908         return;
4909       }
4910 
4911       if (!Context.jmp_bufDecl) {
4912         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4913           Context.setjmp_bufDecl(Typedef->getDecl());
4914         else {
4915           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4916           if (!Tag) {
4917             Error("Invalid jmp_buf type in AST file");
4918             return;
4919           }
4920           Context.setjmp_bufDecl(Tag->getDecl());
4921         }
4922       }
4923     }
4924 
4925     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4926       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4927       if (Sigjmp_bufType.isNull()) {
4928         Error("sigjmp_buf type is NULL");
4929         return;
4930       }
4931 
4932       if (!Context.sigjmp_bufDecl) {
4933         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4934           Context.setsigjmp_bufDecl(Typedef->getDecl());
4935         else {
4936           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4937           assert(Tag && "Invalid sigjmp_buf type in AST file");
4938           Context.setsigjmp_bufDecl(Tag->getDecl());
4939         }
4940       }
4941     }
4942 
4943     if (unsigned ObjCIdRedef
4944           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4945       if (Context.ObjCIdRedefinitionType.isNull())
4946         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4947     }
4948 
4949     if (unsigned ObjCClassRedef
4950           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4951       if (Context.ObjCClassRedefinitionType.isNull())
4952         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4953     }
4954 
4955     if (unsigned ObjCSelRedef
4956           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4957       if (Context.ObjCSelRedefinitionType.isNull())
4958         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4959     }
4960 
4961     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4962       QualType Ucontext_tType = GetType(Ucontext_t);
4963       if (Ucontext_tType.isNull()) {
4964         Error("ucontext_t type is NULL");
4965         return;
4966       }
4967 
4968       if (!Context.ucontext_tDecl) {
4969         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4970           Context.setucontext_tDecl(Typedef->getDecl());
4971         else {
4972           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4973           assert(Tag && "Invalid ucontext_t type in AST file");
4974           Context.setucontext_tDecl(Tag->getDecl());
4975         }
4976       }
4977     }
4978   }
4979 
4980   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4981 
4982   // If there were any CUDA special declarations, deserialize them.
4983   if (!CUDASpecialDeclRefs.empty()) {
4984     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4985     Context.setcudaConfigureCallDecl(
4986                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4987   }
4988 
4989   // Re-export any modules that were imported by a non-module AST file.
4990   // FIXME: This does not make macro-only imports visible again.
4991   for (auto &Import : ImportedModules) {
4992     if (Module *Imported = getSubmodule(Import.ID)) {
4993       makeModuleVisible(Imported, Module::AllVisible,
4994                         /*ImportLoc=*/Import.ImportLoc);
4995       if (Import.ImportLoc.isValid())
4996         PP.makeModuleVisible(Imported, Import.ImportLoc);
4997       // This updates visibility for Preprocessor only. For Sema, which can be
4998       // nullptr here, we do the same later, in UpdateSema().
4999     }
5000   }
5001 }
5002 
5003 void ASTReader::finalizeForWriting() {
5004   // Nothing to do for now.
5005 }
5006 
5007 /// Reads and return the signature record from \p PCH's control block, or
5008 /// else returns 0.
5009 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5010   BitstreamCursor Stream(PCH);
5011   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5012     // FIXME this drops the error on the floor.
5013     consumeError(std::move(Err));
5014     return ASTFileSignature();
5015   }
5016 
5017   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5018   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5019     return ASTFileSignature();
5020 
5021   // Scan for SIGNATURE inside the diagnostic options block.
5022   ASTReader::RecordData Record;
5023   while (true) {
5024     Expected<llvm::BitstreamEntry> MaybeEntry =
5025         Stream.advanceSkippingSubblocks();
5026     if (!MaybeEntry) {
5027       // FIXME this drops the error on the floor.
5028       consumeError(MaybeEntry.takeError());
5029       return ASTFileSignature();
5030     }
5031     llvm::BitstreamEntry Entry = MaybeEntry.get();
5032 
5033     if (Entry.Kind != llvm::BitstreamEntry::Record)
5034       return ASTFileSignature();
5035 
5036     Record.clear();
5037     StringRef Blob;
5038     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5039     if (!MaybeRecord) {
5040       // FIXME this drops the error on the floor.
5041       consumeError(MaybeRecord.takeError());
5042       return ASTFileSignature();
5043     }
5044     if (SIGNATURE == MaybeRecord.get())
5045       return ASTFileSignature::create(Record.begin(),
5046                                       Record.begin() + ASTFileSignature::size);
5047   }
5048 }
5049 
5050 /// Retrieve the name of the original source file name
5051 /// directly from the AST file, without actually loading the AST
5052 /// file.
5053 std::string ASTReader::getOriginalSourceFile(
5054     const std::string &ASTFileName, FileManager &FileMgr,
5055     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5056   // Open the AST file.
5057   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5058   if (!Buffer) {
5059     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5060         << ASTFileName << Buffer.getError().message();
5061     return std::string();
5062   }
5063 
5064   // Initialize the stream
5065   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5066 
5067   // Sniff for the signature.
5068   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5069     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5070     return std::string();
5071   }
5072 
5073   // Scan for the CONTROL_BLOCK_ID block.
5074   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5075     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5076     return std::string();
5077   }
5078 
5079   // Scan for ORIGINAL_FILE inside the control block.
5080   RecordData Record;
5081   while (true) {
5082     Expected<llvm::BitstreamEntry> MaybeEntry =
5083         Stream.advanceSkippingSubblocks();
5084     if (!MaybeEntry) {
5085       // FIXME this drops errors on the floor.
5086       consumeError(MaybeEntry.takeError());
5087       return std::string();
5088     }
5089     llvm::BitstreamEntry Entry = MaybeEntry.get();
5090 
5091     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5092       return std::string();
5093 
5094     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5095       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5096       return std::string();
5097     }
5098 
5099     Record.clear();
5100     StringRef Blob;
5101     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5102     if (!MaybeRecord) {
5103       // FIXME this drops the errors on the floor.
5104       consumeError(MaybeRecord.takeError());
5105       return std::string();
5106     }
5107     if (ORIGINAL_FILE == MaybeRecord.get())
5108       return Blob.str();
5109   }
5110 }
5111 
5112 namespace {
5113 
5114   class SimplePCHValidator : public ASTReaderListener {
5115     const LangOptions &ExistingLangOpts;
5116     const TargetOptions &ExistingTargetOpts;
5117     const PreprocessorOptions &ExistingPPOpts;
5118     std::string ExistingModuleCachePath;
5119     FileManager &FileMgr;
5120 
5121   public:
5122     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5123                        const TargetOptions &ExistingTargetOpts,
5124                        const PreprocessorOptions &ExistingPPOpts,
5125                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5126         : ExistingLangOpts(ExistingLangOpts),
5127           ExistingTargetOpts(ExistingTargetOpts),
5128           ExistingPPOpts(ExistingPPOpts),
5129           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5130 
5131     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5132                              bool AllowCompatibleDifferences) override {
5133       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5134                                   AllowCompatibleDifferences);
5135     }
5136 
5137     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5138                            bool AllowCompatibleDifferences) override {
5139       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5140                                 AllowCompatibleDifferences);
5141     }
5142 
5143     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5144                                  StringRef SpecificModuleCachePath,
5145                                  bool Complain) override {
5146       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5147                                       ExistingModuleCachePath, nullptr,
5148                                       ExistingLangOpts, ExistingPPOpts);
5149     }
5150 
5151     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5152                                  bool Complain,
5153                                  std::string &SuggestedPredefines) override {
5154       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5155                                       SuggestedPredefines, ExistingLangOpts);
5156     }
5157   };
5158 
5159 } // namespace
5160 
5161 bool ASTReader::readASTFileControlBlock(
5162     StringRef Filename, FileManager &FileMgr,
5163     const PCHContainerReader &PCHContainerRdr,
5164     bool FindModuleFileExtensions,
5165     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5166   // Open the AST file.
5167   // FIXME: This allows use of the VFS; we do not allow use of the
5168   // VFS when actually loading a module.
5169   auto Buffer = FileMgr.getBufferForFile(Filename);
5170   if (!Buffer) {
5171     return true;
5172   }
5173 
5174   // Initialize the stream
5175   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5176   BitstreamCursor Stream(Bytes);
5177 
5178   // Sniff for the signature.
5179   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5180     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5181     return true;
5182   }
5183 
5184   // Scan for the CONTROL_BLOCK_ID block.
5185   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5186     return true;
5187 
5188   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5189   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5190   bool NeedsImports = Listener.needsImportVisitation();
5191   BitstreamCursor InputFilesCursor;
5192 
5193   RecordData Record;
5194   std::string ModuleDir;
5195   bool DoneWithControlBlock = false;
5196   while (!DoneWithControlBlock) {
5197     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5198     if (!MaybeEntry) {
5199       // FIXME this drops the error on the floor.
5200       consumeError(MaybeEntry.takeError());
5201       return true;
5202     }
5203     llvm::BitstreamEntry Entry = MaybeEntry.get();
5204 
5205     switch (Entry.Kind) {
5206     case llvm::BitstreamEntry::SubBlock: {
5207       switch (Entry.ID) {
5208       case OPTIONS_BLOCK_ID: {
5209         std::string IgnoredSuggestedPredefines;
5210         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5211                              /*AllowCompatibleConfigurationMismatch*/ false,
5212                              Listener, IgnoredSuggestedPredefines) != Success)
5213           return true;
5214         break;
5215       }
5216 
5217       case INPUT_FILES_BLOCK_ID:
5218         InputFilesCursor = Stream;
5219         if (llvm::Error Err = Stream.SkipBlock()) {
5220           // FIXME this drops the error on the floor.
5221           consumeError(std::move(Err));
5222           return true;
5223         }
5224         if (NeedsInputFiles &&
5225             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5226           return true;
5227         break;
5228 
5229       default:
5230         if (llvm::Error Err = Stream.SkipBlock()) {
5231           // FIXME this drops the error on the floor.
5232           consumeError(std::move(Err));
5233           return true;
5234         }
5235         break;
5236       }
5237 
5238       continue;
5239     }
5240 
5241     case llvm::BitstreamEntry::EndBlock:
5242       DoneWithControlBlock = true;
5243       break;
5244 
5245     case llvm::BitstreamEntry::Error:
5246       return true;
5247 
5248     case llvm::BitstreamEntry::Record:
5249       break;
5250     }
5251 
5252     if (DoneWithControlBlock) break;
5253 
5254     Record.clear();
5255     StringRef Blob;
5256     Expected<unsigned> MaybeRecCode =
5257         Stream.readRecord(Entry.ID, Record, &Blob);
5258     if (!MaybeRecCode) {
5259       // FIXME this drops the error.
5260       return Failure;
5261     }
5262     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5263     case METADATA:
5264       if (Record[0] != VERSION_MAJOR)
5265         return true;
5266       if (Listener.ReadFullVersionInformation(Blob))
5267         return true;
5268       break;
5269     case MODULE_NAME:
5270       Listener.ReadModuleName(Blob);
5271       break;
5272     case MODULE_DIRECTORY:
5273       ModuleDir = std::string(Blob);
5274       break;
5275     case MODULE_MAP_FILE: {
5276       unsigned Idx = 0;
5277       auto Path = ReadString(Record, Idx);
5278       ResolveImportedPath(Path, ModuleDir);
5279       Listener.ReadModuleMapFile(Path);
5280       break;
5281     }
5282     case INPUT_FILE_OFFSETS: {
5283       if (!NeedsInputFiles)
5284         break;
5285 
5286       unsigned NumInputFiles = Record[0];
5287       unsigned NumUserFiles = Record[1];
5288       const llvm::support::unaligned_uint64_t *InputFileOffs =
5289           (const llvm::support::unaligned_uint64_t *)Blob.data();
5290       for (unsigned I = 0; I != NumInputFiles; ++I) {
5291         // Go find this input file.
5292         bool isSystemFile = I >= NumUserFiles;
5293 
5294         if (isSystemFile && !NeedsSystemInputFiles)
5295           break; // the rest are system input files
5296 
5297         BitstreamCursor &Cursor = InputFilesCursor;
5298         SavedStreamPosition SavedPosition(Cursor);
5299         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5300           // FIXME this drops errors on the floor.
5301           consumeError(std::move(Err));
5302         }
5303 
5304         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5305         if (!MaybeCode) {
5306           // FIXME this drops errors on the floor.
5307           consumeError(MaybeCode.takeError());
5308         }
5309         unsigned Code = MaybeCode.get();
5310 
5311         RecordData Record;
5312         StringRef Blob;
5313         bool shouldContinue = false;
5314         Expected<unsigned> MaybeRecordType =
5315             Cursor.readRecord(Code, Record, &Blob);
5316         if (!MaybeRecordType) {
5317           // FIXME this drops errors on the floor.
5318           consumeError(MaybeRecordType.takeError());
5319         }
5320         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5321         case INPUT_FILE_HASH:
5322           break;
5323         case INPUT_FILE:
5324           bool Overridden = static_cast<bool>(Record[3]);
5325           std::string Filename = std::string(Blob);
5326           ResolveImportedPath(Filename, ModuleDir);
5327           shouldContinue = Listener.visitInputFile(
5328               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5329           break;
5330         }
5331         if (!shouldContinue)
5332           break;
5333       }
5334       break;
5335     }
5336 
5337     case IMPORTS: {
5338       if (!NeedsImports)
5339         break;
5340 
5341       unsigned Idx = 0, N = Record.size();
5342       while (Idx < N) {
5343         // Read information about the AST file.
5344         Idx +=
5345             1 + 1 + 1 + 1 +
5346             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5347         std::string ModuleName = ReadString(Record, Idx);
5348         std::string Filename = ReadString(Record, Idx);
5349         ResolveImportedPath(Filename, ModuleDir);
5350         Listener.visitImport(ModuleName, Filename);
5351       }
5352       break;
5353     }
5354 
5355     default:
5356       // No other validation to perform.
5357       break;
5358     }
5359   }
5360 
5361   // Look for module file extension blocks, if requested.
5362   if (FindModuleFileExtensions) {
5363     BitstreamCursor SavedStream = Stream;
5364     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5365       bool DoneWithExtensionBlock = false;
5366       while (!DoneWithExtensionBlock) {
5367         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5368         if (!MaybeEntry) {
5369           // FIXME this drops the error.
5370           return true;
5371         }
5372         llvm::BitstreamEntry Entry = MaybeEntry.get();
5373 
5374         switch (Entry.Kind) {
5375         case llvm::BitstreamEntry::SubBlock:
5376           if (llvm::Error Err = Stream.SkipBlock()) {
5377             // FIXME this drops the error on the floor.
5378             consumeError(std::move(Err));
5379             return true;
5380           }
5381           continue;
5382 
5383         case llvm::BitstreamEntry::EndBlock:
5384           DoneWithExtensionBlock = true;
5385           continue;
5386 
5387         case llvm::BitstreamEntry::Error:
5388           return true;
5389 
5390         case llvm::BitstreamEntry::Record:
5391           break;
5392         }
5393 
5394        Record.clear();
5395        StringRef Blob;
5396        Expected<unsigned> MaybeRecCode =
5397            Stream.readRecord(Entry.ID, Record, &Blob);
5398        if (!MaybeRecCode) {
5399          // FIXME this drops the error.
5400          return true;
5401        }
5402        switch (MaybeRecCode.get()) {
5403        case EXTENSION_METADATA: {
5404          ModuleFileExtensionMetadata Metadata;
5405          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5406            return true;
5407 
5408          Listener.readModuleFileExtension(Metadata);
5409          break;
5410        }
5411        }
5412       }
5413     }
5414     Stream = SavedStream;
5415   }
5416 
5417   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5418   if (readUnhashedControlBlockImpl(
5419           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5420           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5421           ValidateDiagnosticOptions) != Success)
5422     return true;
5423 
5424   return false;
5425 }
5426 
5427 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5428                                     const PCHContainerReader &PCHContainerRdr,
5429                                     const LangOptions &LangOpts,
5430                                     const TargetOptions &TargetOpts,
5431                                     const PreprocessorOptions &PPOpts,
5432                                     StringRef ExistingModuleCachePath) {
5433   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5434                                ExistingModuleCachePath, FileMgr);
5435   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5436                                   /*FindModuleFileExtensions=*/false,
5437                                   validator,
5438                                   /*ValidateDiagnosticOptions=*/true);
5439 }
5440 
5441 ASTReader::ASTReadResult
5442 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5443   // Enter the submodule block.
5444   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5445     Error(std::move(Err));
5446     return Failure;
5447   }
5448 
5449   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5450   bool First = true;
5451   Module *CurrentModule = nullptr;
5452   RecordData Record;
5453   while (true) {
5454     Expected<llvm::BitstreamEntry> MaybeEntry =
5455         F.Stream.advanceSkippingSubblocks();
5456     if (!MaybeEntry) {
5457       Error(MaybeEntry.takeError());
5458       return Failure;
5459     }
5460     llvm::BitstreamEntry Entry = MaybeEntry.get();
5461 
5462     switch (Entry.Kind) {
5463     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5464     case llvm::BitstreamEntry::Error:
5465       Error("malformed block record in AST file");
5466       return Failure;
5467     case llvm::BitstreamEntry::EndBlock:
5468       return Success;
5469     case llvm::BitstreamEntry::Record:
5470       // The interesting case.
5471       break;
5472     }
5473 
5474     // Read a record.
5475     StringRef Blob;
5476     Record.clear();
5477     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5478     if (!MaybeKind) {
5479       Error(MaybeKind.takeError());
5480       return Failure;
5481     }
5482     unsigned Kind = MaybeKind.get();
5483 
5484     if ((Kind == SUBMODULE_METADATA) != First) {
5485       Error("submodule metadata record should be at beginning of block");
5486       return Failure;
5487     }
5488     First = false;
5489 
5490     // Submodule information is only valid if we have a current module.
5491     // FIXME: Should we error on these cases?
5492     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5493         Kind != SUBMODULE_DEFINITION)
5494       continue;
5495 
5496     switch (Kind) {
5497     default:  // Default behavior: ignore.
5498       break;
5499 
5500     case SUBMODULE_DEFINITION: {
5501       if (Record.size() < 12) {
5502         Error("malformed module definition");
5503         return Failure;
5504       }
5505 
5506       StringRef Name = Blob;
5507       unsigned Idx = 0;
5508       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5509       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5510       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5511       bool IsFramework = Record[Idx++];
5512       bool IsExplicit = Record[Idx++];
5513       bool IsSystem = Record[Idx++];
5514       bool IsExternC = Record[Idx++];
5515       bool InferSubmodules = Record[Idx++];
5516       bool InferExplicitSubmodules = Record[Idx++];
5517       bool InferExportWildcard = Record[Idx++];
5518       bool ConfigMacrosExhaustive = Record[Idx++];
5519       bool ModuleMapIsPrivate = Record[Idx++];
5520 
5521       Module *ParentModule = nullptr;
5522       if (Parent)
5523         ParentModule = getSubmodule(Parent);
5524 
5525       // Retrieve this (sub)module from the module map, creating it if
5526       // necessary.
5527       CurrentModule =
5528           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5529               .first;
5530 
5531       // FIXME: set the definition loc for CurrentModule, or call
5532       // ModMap.setInferredModuleAllowedBy()
5533 
5534       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5535       if (GlobalIndex >= SubmodulesLoaded.size() ||
5536           SubmodulesLoaded[GlobalIndex]) {
5537         Error("too many submodules");
5538         return Failure;
5539       }
5540 
5541       if (!ParentModule) {
5542         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5543           // Don't emit module relocation error if we have -fno-validate-pch
5544           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5545                     DisableValidationForModuleKind::Module) &&
5546               CurFile != F.File) {
5547             Error(diag::err_module_file_conflict,
5548                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5549                   F.File->getName());
5550             return Failure;
5551           }
5552         }
5553 
5554         F.DidReadTopLevelSubmodule = true;
5555         CurrentModule->setASTFile(F.File);
5556         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5557       }
5558 
5559       CurrentModule->Kind = Kind;
5560       CurrentModule->Signature = F.Signature;
5561       CurrentModule->IsFromModuleFile = true;
5562       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5563       CurrentModule->IsExternC = IsExternC;
5564       CurrentModule->InferSubmodules = InferSubmodules;
5565       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5566       CurrentModule->InferExportWildcard = InferExportWildcard;
5567       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5568       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5569       if (DeserializationListener)
5570         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5571 
5572       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5573 
5574       // Clear out data that will be replaced by what is in the module file.
5575       CurrentModule->LinkLibraries.clear();
5576       CurrentModule->ConfigMacros.clear();
5577       CurrentModule->UnresolvedConflicts.clear();
5578       CurrentModule->Conflicts.clear();
5579 
5580       // The module is available unless it's missing a requirement; relevant
5581       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5582       // Missing headers that were present when the module was built do not
5583       // make it unavailable -- if we got this far, this must be an explicitly
5584       // imported module file.
5585       CurrentModule->Requirements.clear();
5586       CurrentModule->MissingHeaders.clear();
5587       CurrentModule->IsUnimportable =
5588           ParentModule && ParentModule->IsUnimportable;
5589       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5590       break;
5591     }
5592 
5593     case SUBMODULE_UMBRELLA_HEADER: {
5594       std::string Filename = std::string(Blob);
5595       ResolveImportedPath(F, Filename);
5596       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5597         if (!CurrentModule->getUmbrellaHeader())
5598           // FIXME: NameAsWritten
5599           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5600         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5601           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5602             Error("mismatched umbrella headers in submodule");
5603           return OutOfDate;
5604         }
5605       }
5606       break;
5607     }
5608 
5609     case SUBMODULE_HEADER:
5610     case SUBMODULE_EXCLUDED_HEADER:
5611     case SUBMODULE_PRIVATE_HEADER:
5612       // We lazily associate headers with their modules via the HeaderInfo table.
5613       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5614       // of complete filenames or remove it entirely.
5615       break;
5616 
5617     case SUBMODULE_TEXTUAL_HEADER:
5618     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5619       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5620       // them here.
5621       break;
5622 
5623     case SUBMODULE_TOPHEADER:
5624       CurrentModule->addTopHeaderFilename(Blob);
5625       break;
5626 
5627     case SUBMODULE_UMBRELLA_DIR: {
5628       std::string Dirname = std::string(Blob);
5629       ResolveImportedPath(F, Dirname);
5630       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5631         if (!CurrentModule->getUmbrellaDir())
5632           // FIXME: NameAsWritten
5633           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5634         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5635           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5636             Error("mismatched umbrella directories in submodule");
5637           return OutOfDate;
5638         }
5639       }
5640       break;
5641     }
5642 
5643     case SUBMODULE_METADATA: {
5644       F.BaseSubmoduleID = getTotalNumSubmodules();
5645       F.LocalNumSubmodules = Record[0];
5646       unsigned LocalBaseSubmoduleID = Record[1];
5647       if (F.LocalNumSubmodules > 0) {
5648         // Introduce the global -> local mapping for submodules within this
5649         // module.
5650         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5651 
5652         // Introduce the local -> global mapping for submodules within this
5653         // module.
5654         F.SubmoduleRemap.insertOrReplace(
5655           std::make_pair(LocalBaseSubmoduleID,
5656                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5657 
5658         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5659       }
5660       break;
5661     }
5662 
5663     case SUBMODULE_IMPORTS:
5664       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5665         UnresolvedModuleRef Unresolved;
5666         Unresolved.File = &F;
5667         Unresolved.Mod = CurrentModule;
5668         Unresolved.ID = Record[Idx];
5669         Unresolved.Kind = UnresolvedModuleRef::Import;
5670         Unresolved.IsWildcard = false;
5671         UnresolvedModuleRefs.push_back(Unresolved);
5672       }
5673       break;
5674 
5675     case SUBMODULE_EXPORTS:
5676       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5677         UnresolvedModuleRef Unresolved;
5678         Unresolved.File = &F;
5679         Unresolved.Mod = CurrentModule;
5680         Unresolved.ID = Record[Idx];
5681         Unresolved.Kind = UnresolvedModuleRef::Export;
5682         Unresolved.IsWildcard = Record[Idx + 1];
5683         UnresolvedModuleRefs.push_back(Unresolved);
5684       }
5685 
5686       // Once we've loaded the set of exports, there's no reason to keep
5687       // the parsed, unresolved exports around.
5688       CurrentModule->UnresolvedExports.clear();
5689       break;
5690 
5691     case SUBMODULE_REQUIRES:
5692       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5693                                     PP.getTargetInfo());
5694       break;
5695 
5696     case SUBMODULE_LINK_LIBRARY:
5697       ModMap.resolveLinkAsDependencies(CurrentModule);
5698       CurrentModule->LinkLibraries.push_back(
5699           Module::LinkLibrary(std::string(Blob), Record[0]));
5700       break;
5701 
5702     case SUBMODULE_CONFIG_MACRO:
5703       CurrentModule->ConfigMacros.push_back(Blob.str());
5704       break;
5705 
5706     case SUBMODULE_CONFLICT: {
5707       UnresolvedModuleRef Unresolved;
5708       Unresolved.File = &F;
5709       Unresolved.Mod = CurrentModule;
5710       Unresolved.ID = Record[0];
5711       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5712       Unresolved.IsWildcard = false;
5713       Unresolved.String = Blob;
5714       UnresolvedModuleRefs.push_back(Unresolved);
5715       break;
5716     }
5717 
5718     case SUBMODULE_INITIALIZERS: {
5719       if (!ContextObj)
5720         break;
5721       SmallVector<uint32_t, 16> Inits;
5722       for (auto &ID : Record)
5723         Inits.push_back(getGlobalDeclID(F, ID));
5724       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5725       break;
5726     }
5727 
5728     case SUBMODULE_EXPORT_AS:
5729       CurrentModule->ExportAsModule = Blob.str();
5730       ModMap.addLinkAsDependency(CurrentModule);
5731       break;
5732     }
5733   }
5734 }
5735 
5736 /// Parse the record that corresponds to a LangOptions data
5737 /// structure.
5738 ///
5739 /// This routine parses the language options from the AST file and then gives
5740 /// them to the AST listener if one is set.
5741 ///
5742 /// \returns true if the listener deems the file unacceptable, false otherwise.
5743 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5744                                      bool Complain,
5745                                      ASTReaderListener &Listener,
5746                                      bool AllowCompatibleDifferences) {
5747   LangOptions LangOpts;
5748   unsigned Idx = 0;
5749 #define LANGOPT(Name, Bits, Default, Description) \
5750   LangOpts.Name = Record[Idx++];
5751 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5752   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5753 #include "clang/Basic/LangOptions.def"
5754 #define SANITIZER(NAME, ID)                                                    \
5755   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5756 #include "clang/Basic/Sanitizers.def"
5757 
5758   for (unsigned N = Record[Idx++]; N; --N)
5759     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5760 
5761   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5762   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5763   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5764 
5765   LangOpts.CurrentModule = ReadString(Record, Idx);
5766 
5767   // Comment options.
5768   for (unsigned N = Record[Idx++]; N; --N) {
5769     LangOpts.CommentOpts.BlockCommandNames.push_back(
5770       ReadString(Record, Idx));
5771   }
5772   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5773 
5774   // OpenMP offloading options.
5775   for (unsigned N = Record[Idx++]; N; --N) {
5776     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5777   }
5778 
5779   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5780 
5781   return Listener.ReadLanguageOptions(LangOpts, Complain,
5782                                       AllowCompatibleDifferences);
5783 }
5784 
5785 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5786                                    ASTReaderListener &Listener,
5787                                    bool AllowCompatibleDifferences) {
5788   unsigned Idx = 0;
5789   TargetOptions TargetOpts;
5790   TargetOpts.Triple = ReadString(Record, Idx);
5791   TargetOpts.CPU = ReadString(Record, Idx);
5792   TargetOpts.TuneCPU = ReadString(Record, Idx);
5793   TargetOpts.ABI = ReadString(Record, Idx);
5794   for (unsigned N = Record[Idx++]; N; --N) {
5795     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5796   }
5797   for (unsigned N = Record[Idx++]; N; --N) {
5798     TargetOpts.Features.push_back(ReadString(Record, Idx));
5799   }
5800 
5801   return Listener.ReadTargetOptions(TargetOpts, Complain,
5802                                     AllowCompatibleDifferences);
5803 }
5804 
5805 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5806                                        ASTReaderListener &Listener) {
5807   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5808   unsigned Idx = 0;
5809 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5810 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5811   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5812 #include "clang/Basic/DiagnosticOptions.def"
5813 
5814   for (unsigned N = Record[Idx++]; N; --N)
5815     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5816   for (unsigned N = Record[Idx++]; N; --N)
5817     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5818 
5819   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5820 }
5821 
5822 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5823                                        ASTReaderListener &Listener) {
5824   FileSystemOptions FSOpts;
5825   unsigned Idx = 0;
5826   FSOpts.WorkingDir = ReadString(Record, Idx);
5827   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5828 }
5829 
5830 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5831                                          bool Complain,
5832                                          ASTReaderListener &Listener) {
5833   HeaderSearchOptions HSOpts;
5834   unsigned Idx = 0;
5835   HSOpts.Sysroot = ReadString(Record, Idx);
5836 
5837   // Include entries.
5838   for (unsigned N = Record[Idx++]; N; --N) {
5839     std::string Path = ReadString(Record, Idx);
5840     frontend::IncludeDirGroup Group
5841       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5842     bool IsFramework = Record[Idx++];
5843     bool IgnoreSysRoot = Record[Idx++];
5844     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5845                                     IgnoreSysRoot);
5846   }
5847 
5848   // System header prefixes.
5849   for (unsigned N = Record[Idx++]; N; --N) {
5850     std::string Prefix = ReadString(Record, Idx);
5851     bool IsSystemHeader = Record[Idx++];
5852     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5853   }
5854 
5855   HSOpts.ResourceDir = ReadString(Record, Idx);
5856   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5857   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5858   HSOpts.DisableModuleHash = Record[Idx++];
5859   HSOpts.ImplicitModuleMaps = Record[Idx++];
5860   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5861   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5862   HSOpts.UseBuiltinIncludes = Record[Idx++];
5863   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5864   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5865   HSOpts.UseLibcxx = Record[Idx++];
5866   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5867 
5868   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5869                                           Complain);
5870 }
5871 
5872 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5873                                          bool Complain,
5874                                          ASTReaderListener &Listener,
5875                                          std::string &SuggestedPredefines) {
5876   PreprocessorOptions PPOpts;
5877   unsigned Idx = 0;
5878 
5879   // Macro definitions/undefs
5880   for (unsigned N = Record[Idx++]; N; --N) {
5881     std::string Macro = ReadString(Record, Idx);
5882     bool IsUndef = Record[Idx++];
5883     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5884   }
5885 
5886   // Includes
5887   for (unsigned N = Record[Idx++]; N; --N) {
5888     PPOpts.Includes.push_back(ReadString(Record, Idx));
5889   }
5890 
5891   // Macro Includes
5892   for (unsigned N = Record[Idx++]; N; --N) {
5893     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5894   }
5895 
5896   PPOpts.UsePredefines = Record[Idx++];
5897   PPOpts.DetailedRecord = Record[Idx++];
5898   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5899   PPOpts.ObjCXXARCStandardLibrary =
5900     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5901   SuggestedPredefines.clear();
5902   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5903                                           SuggestedPredefines);
5904 }
5905 
5906 std::pair<ModuleFile *, unsigned>
5907 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5908   GlobalPreprocessedEntityMapType::iterator
5909   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5910   assert(I != GlobalPreprocessedEntityMap.end() &&
5911          "Corrupted global preprocessed entity map");
5912   ModuleFile *M = I->second;
5913   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5914   return std::make_pair(M, LocalIndex);
5915 }
5916 
5917 llvm::iterator_range<PreprocessingRecord::iterator>
5918 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5919   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5920     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5921                                              Mod.NumPreprocessedEntities);
5922 
5923   return llvm::make_range(PreprocessingRecord::iterator(),
5924                           PreprocessingRecord::iterator());
5925 }
5926 
5927 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5928 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5929   return llvm::make_range(
5930       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5931       ModuleDeclIterator(this, &Mod,
5932                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5933 }
5934 
5935 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5936   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5937   assert(I != GlobalSkippedRangeMap.end() &&
5938     "Corrupted global skipped range map");
5939   ModuleFile *M = I->second;
5940   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5941   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5942   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5943   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5944                     TranslateSourceLocation(*M, RawRange.getEnd()));
5945   assert(Range.isValid());
5946   return Range;
5947 }
5948 
5949 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5950   PreprocessedEntityID PPID = Index+1;
5951   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5952   ModuleFile &M = *PPInfo.first;
5953   unsigned LocalIndex = PPInfo.second;
5954   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5955 
5956   if (!PP.getPreprocessingRecord()) {
5957     Error("no preprocessing record");
5958     return nullptr;
5959   }
5960 
5961   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5962   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5963           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5964     Error(std::move(Err));
5965     return nullptr;
5966   }
5967 
5968   Expected<llvm::BitstreamEntry> MaybeEntry =
5969       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5970   if (!MaybeEntry) {
5971     Error(MaybeEntry.takeError());
5972     return nullptr;
5973   }
5974   llvm::BitstreamEntry Entry = MaybeEntry.get();
5975 
5976   if (Entry.Kind != llvm::BitstreamEntry::Record)
5977     return nullptr;
5978 
5979   // Read the record.
5980   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5981                     TranslateSourceLocation(M, PPOffs.getEnd()));
5982   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5983   StringRef Blob;
5984   RecordData Record;
5985   Expected<unsigned> MaybeRecType =
5986       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5987   if (!MaybeRecType) {
5988     Error(MaybeRecType.takeError());
5989     return nullptr;
5990   }
5991   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5992   case PPD_MACRO_EXPANSION: {
5993     bool isBuiltin = Record[0];
5994     IdentifierInfo *Name = nullptr;
5995     MacroDefinitionRecord *Def = nullptr;
5996     if (isBuiltin)
5997       Name = getLocalIdentifier(M, Record[1]);
5998     else {
5999       PreprocessedEntityID GlobalID =
6000           getGlobalPreprocessedEntityID(M, Record[1]);
6001       Def = cast<MacroDefinitionRecord>(
6002           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6003     }
6004 
6005     MacroExpansion *ME;
6006     if (isBuiltin)
6007       ME = new (PPRec) MacroExpansion(Name, Range);
6008     else
6009       ME = new (PPRec) MacroExpansion(Def, Range);
6010 
6011     return ME;
6012   }
6013 
6014   case PPD_MACRO_DEFINITION: {
6015     // Decode the identifier info and then check again; if the macro is
6016     // still defined and associated with the identifier,
6017     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6018     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6019 
6020     if (DeserializationListener)
6021       DeserializationListener->MacroDefinitionRead(PPID, MD);
6022 
6023     return MD;
6024   }
6025 
6026   case PPD_INCLUSION_DIRECTIVE: {
6027     const char *FullFileNameStart = Blob.data() + Record[0];
6028     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6029     const FileEntry *File = nullptr;
6030     if (!FullFileName.empty())
6031       if (auto FE = PP.getFileManager().getFile(FullFileName))
6032         File = *FE;
6033 
6034     // FIXME: Stable encoding
6035     InclusionDirective::InclusionKind Kind
6036       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6037     InclusionDirective *ID
6038       = new (PPRec) InclusionDirective(PPRec, Kind,
6039                                        StringRef(Blob.data(), Record[0]),
6040                                        Record[1], Record[3],
6041                                        File,
6042                                        Range);
6043     return ID;
6044   }
6045   }
6046 
6047   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6048 }
6049 
6050 /// Find the next module that contains entities and return the ID
6051 /// of the first entry.
6052 ///
6053 /// \param SLocMapI points at a chunk of a module that contains no
6054 /// preprocessed entities or the entities it contains are not the ones we are
6055 /// looking for.
6056 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6057                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6058   ++SLocMapI;
6059   for (GlobalSLocOffsetMapType::const_iterator
6060          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6061     ModuleFile &M = *SLocMapI->second;
6062     if (M.NumPreprocessedEntities)
6063       return M.BasePreprocessedEntityID;
6064   }
6065 
6066   return getTotalNumPreprocessedEntities();
6067 }
6068 
6069 namespace {
6070 
6071 struct PPEntityComp {
6072   const ASTReader &Reader;
6073   ModuleFile &M;
6074 
6075   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6076 
6077   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6078     SourceLocation LHS = getLoc(L);
6079     SourceLocation RHS = getLoc(R);
6080     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6081   }
6082 
6083   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6084     SourceLocation LHS = getLoc(L);
6085     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6086   }
6087 
6088   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6089     SourceLocation RHS = getLoc(R);
6090     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6091   }
6092 
6093   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6094     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6095   }
6096 };
6097 
6098 } // namespace
6099 
6100 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6101                                                        bool EndsAfter) const {
6102   if (SourceMgr.isLocalSourceLocation(Loc))
6103     return getTotalNumPreprocessedEntities();
6104 
6105   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6106       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6107   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6108          "Corrupted global sloc offset map");
6109 
6110   if (SLocMapI->second->NumPreprocessedEntities == 0)
6111     return findNextPreprocessedEntity(SLocMapI);
6112 
6113   ModuleFile &M = *SLocMapI->second;
6114 
6115   using pp_iterator = const PPEntityOffset *;
6116 
6117   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6118   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6119 
6120   size_t Count = M.NumPreprocessedEntities;
6121   size_t Half;
6122   pp_iterator First = pp_begin;
6123   pp_iterator PPI;
6124 
6125   if (EndsAfter) {
6126     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6127                            PPEntityComp(*this, M));
6128   } else {
6129     // Do a binary search manually instead of using std::lower_bound because
6130     // The end locations of entities may be unordered (when a macro expansion
6131     // is inside another macro argument), but for this case it is not important
6132     // whether we get the first macro expansion or its containing macro.
6133     while (Count > 0) {
6134       Half = Count / 2;
6135       PPI = First;
6136       std::advance(PPI, Half);
6137       if (SourceMgr.isBeforeInTranslationUnit(
6138               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6139         First = PPI;
6140         ++First;
6141         Count = Count - Half - 1;
6142       } else
6143         Count = Half;
6144     }
6145   }
6146 
6147   if (PPI == pp_end)
6148     return findNextPreprocessedEntity(SLocMapI);
6149 
6150   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6151 }
6152 
6153 /// Returns a pair of [Begin, End) indices of preallocated
6154 /// preprocessed entities that \arg Range encompasses.
6155 std::pair<unsigned, unsigned>
6156     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6157   if (Range.isInvalid())
6158     return std::make_pair(0,0);
6159   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6160 
6161   PreprocessedEntityID BeginID =
6162       findPreprocessedEntity(Range.getBegin(), false);
6163   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6164   return std::make_pair(BeginID, EndID);
6165 }
6166 
6167 /// Optionally returns true or false if the preallocated preprocessed
6168 /// entity with index \arg Index came from file \arg FID.
6169 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6170                                                              FileID FID) {
6171   if (FID.isInvalid())
6172     return false;
6173 
6174   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6175   ModuleFile &M = *PPInfo.first;
6176   unsigned LocalIndex = PPInfo.second;
6177   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6178 
6179   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6180   if (Loc.isInvalid())
6181     return false;
6182 
6183   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6184     return true;
6185   else
6186     return false;
6187 }
6188 
6189 namespace {
6190 
6191   /// Visitor used to search for information about a header file.
6192   class HeaderFileInfoVisitor {
6193     const FileEntry *FE;
6194     Optional<HeaderFileInfo> HFI;
6195 
6196   public:
6197     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6198 
6199     bool operator()(ModuleFile &M) {
6200       HeaderFileInfoLookupTable *Table
6201         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6202       if (!Table)
6203         return false;
6204 
6205       // Look in the on-disk hash table for an entry for this file name.
6206       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6207       if (Pos == Table->end())
6208         return false;
6209 
6210       HFI = *Pos;
6211       return true;
6212     }
6213 
6214     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6215   };
6216 
6217 } // namespace
6218 
6219 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6220   HeaderFileInfoVisitor Visitor(FE);
6221   ModuleMgr.visit(Visitor);
6222   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6223     return *HFI;
6224 
6225   return HeaderFileInfo();
6226 }
6227 
6228 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6229   using DiagState = DiagnosticsEngine::DiagState;
6230   SmallVector<DiagState *, 32> DiagStates;
6231 
6232   for (ModuleFile &F : ModuleMgr) {
6233     unsigned Idx = 0;
6234     auto &Record = F.PragmaDiagMappings;
6235     if (Record.empty())
6236       continue;
6237 
6238     DiagStates.clear();
6239 
6240     auto ReadDiagState =
6241         [&](const DiagState &BasedOn, SourceLocation Loc,
6242             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6243       unsigned BackrefID = Record[Idx++];
6244       if (BackrefID != 0)
6245         return DiagStates[BackrefID - 1];
6246 
6247       // A new DiagState was created here.
6248       Diag.DiagStates.push_back(BasedOn);
6249       DiagState *NewState = &Diag.DiagStates.back();
6250       DiagStates.push_back(NewState);
6251       unsigned Size = Record[Idx++];
6252       assert(Idx + Size * 2 <= Record.size() &&
6253              "Invalid data, not enough diag/map pairs");
6254       while (Size--) {
6255         unsigned DiagID = Record[Idx++];
6256         DiagnosticMapping NewMapping =
6257             DiagnosticMapping::deserialize(Record[Idx++]);
6258         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6259           continue;
6260 
6261         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6262 
6263         // If this mapping was specified as a warning but the severity was
6264         // upgraded due to diagnostic settings, simulate the current diagnostic
6265         // settings (and use a warning).
6266         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6267           NewMapping.setSeverity(diag::Severity::Warning);
6268           NewMapping.setUpgradedFromWarning(false);
6269         }
6270 
6271         Mapping = NewMapping;
6272       }
6273       return NewState;
6274     };
6275 
6276     // Read the first state.
6277     DiagState *FirstState;
6278     if (F.Kind == MK_ImplicitModule) {
6279       // Implicitly-built modules are reused with different diagnostic
6280       // settings.  Use the initial diagnostic state from Diag to simulate this
6281       // compilation's diagnostic settings.
6282       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6283       DiagStates.push_back(FirstState);
6284 
6285       // Skip the initial diagnostic state from the serialized module.
6286       assert(Record[1] == 0 &&
6287              "Invalid data, unexpected backref in initial state");
6288       Idx = 3 + Record[2] * 2;
6289       assert(Idx < Record.size() &&
6290              "Invalid data, not enough state change pairs in initial state");
6291     } else if (F.isModule()) {
6292       // For an explicit module, preserve the flags from the module build
6293       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6294       // -Wblah flags.
6295       unsigned Flags = Record[Idx++];
6296       DiagState Initial;
6297       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6298       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6299       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6300       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6301       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6302       Initial.ExtBehavior = (diag::Severity)Flags;
6303       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6304 
6305       assert(F.OriginalSourceFileID.isValid());
6306 
6307       // Set up the root buffer of the module to start with the initial
6308       // diagnostic state of the module itself, to cover files that contain no
6309       // explicit transitions (for which we did not serialize anything).
6310       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6311           .StateTransitions.push_back({FirstState, 0});
6312     } else {
6313       // For prefix ASTs, start with whatever the user configured on the
6314       // command line.
6315       Idx++; // Skip flags.
6316       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6317                                  SourceLocation(), false);
6318     }
6319 
6320     // Read the state transitions.
6321     unsigned NumLocations = Record[Idx++];
6322     while (NumLocations--) {
6323       assert(Idx < Record.size() &&
6324              "Invalid data, missing pragma diagnostic states");
6325       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6326       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6327       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6328       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6329       unsigned Transitions = Record[Idx++];
6330 
6331       // Note that we don't need to set up Parent/ParentOffset here, because
6332       // we won't be changing the diagnostic state within imported FileIDs
6333       // (other than perhaps appending to the main source file, which has no
6334       // parent).
6335       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6336       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6337       for (unsigned I = 0; I != Transitions; ++I) {
6338         unsigned Offset = Record[Idx++];
6339         auto *State =
6340             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6341         F.StateTransitions.push_back({State, Offset});
6342       }
6343     }
6344 
6345     // Read the final state.
6346     assert(Idx < Record.size() &&
6347            "Invalid data, missing final pragma diagnostic state");
6348     SourceLocation CurStateLoc =
6349         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6350     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6351 
6352     if (!F.isModule()) {
6353       Diag.DiagStatesByLoc.CurDiagState = CurState;
6354       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6355 
6356       // Preserve the property that the imaginary root file describes the
6357       // current state.
6358       FileID NullFile;
6359       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6360       if (T.empty())
6361         T.push_back({CurState, 0});
6362       else
6363         T[0].State = CurState;
6364     }
6365 
6366     // Don't try to read these mappings again.
6367     Record.clear();
6368   }
6369 }
6370 
6371 /// Get the correct cursor and offset for loading a type.
6372 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6373   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6374   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6375   ModuleFile *M = I->second;
6376   return RecordLocation(
6377       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6378              M->DeclsBlockStartOffset);
6379 }
6380 
6381 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6382   switch (code) {
6383 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6384   case TYPE_##CODE_ID: return Type::CLASS_ID;
6385 #include "clang/Serialization/TypeBitCodes.def"
6386   default: return llvm::None;
6387   }
6388 }
6389 
6390 /// Read and return the type with the given index..
6391 ///
6392 /// The index is the type ID, shifted and minus the number of predefs. This
6393 /// routine actually reads the record corresponding to the type at the given
6394 /// location. It is a helper routine for GetType, which deals with reading type
6395 /// IDs.
6396 QualType ASTReader::readTypeRecord(unsigned Index) {
6397   assert(ContextObj && "reading type with no AST context");
6398   ASTContext &Context = *ContextObj;
6399   RecordLocation Loc = TypeCursorForIndex(Index);
6400   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6401 
6402   // Keep track of where we are in the stream, then jump back there
6403   // after reading this type.
6404   SavedStreamPosition SavedPosition(DeclsCursor);
6405 
6406   ReadingKindTracker ReadingKind(Read_Type, *this);
6407 
6408   // Note that we are loading a type record.
6409   Deserializing AType(this);
6410 
6411   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6412     Error(std::move(Err));
6413     return QualType();
6414   }
6415   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6416   if (!RawCode) {
6417     Error(RawCode.takeError());
6418     return QualType();
6419   }
6420 
6421   ASTRecordReader Record(*this, *Loc.F);
6422   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6423   if (!Code) {
6424     Error(Code.takeError());
6425     return QualType();
6426   }
6427   if (Code.get() == TYPE_EXT_QUAL) {
6428     QualType baseType = Record.readQualType();
6429     Qualifiers quals = Record.readQualifiers();
6430     return Context.getQualifiedType(baseType, quals);
6431   }
6432 
6433   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6434   if (!maybeClass) {
6435     Error("Unexpected code for type");
6436     return QualType();
6437   }
6438 
6439   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6440   return TypeReader.read(*maybeClass);
6441 }
6442 
6443 namespace clang {
6444 
6445 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6446   ASTRecordReader &Reader;
6447 
6448   SourceLocation readSourceLocation() {
6449     return Reader.readSourceLocation();
6450   }
6451 
6452   TypeSourceInfo *GetTypeSourceInfo() {
6453     return Reader.readTypeSourceInfo();
6454   }
6455 
6456   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6457     return Reader.readNestedNameSpecifierLoc();
6458   }
6459 
6460   Attr *ReadAttr() {
6461     return Reader.readAttr();
6462   }
6463 
6464 public:
6465   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6466 
6467   // We want compile-time assurance that we've enumerated all of
6468   // these, so unfortunately we have to declare them first, then
6469   // define them out-of-line.
6470 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6471 #define TYPELOC(CLASS, PARENT) \
6472   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6473 #include "clang/AST/TypeLocNodes.def"
6474 
6475   void VisitFunctionTypeLoc(FunctionTypeLoc);
6476   void VisitArrayTypeLoc(ArrayTypeLoc);
6477 };
6478 
6479 } // namespace clang
6480 
6481 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6482   // nothing to do
6483 }
6484 
6485 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6486   TL.setBuiltinLoc(readSourceLocation());
6487   if (TL.needsExtraLocalData()) {
6488     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6489     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6490     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6491     TL.setModeAttr(Reader.readInt());
6492   }
6493 }
6494 
6495 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6496   TL.setNameLoc(readSourceLocation());
6497 }
6498 
6499 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6500   TL.setStarLoc(readSourceLocation());
6501 }
6502 
6503 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6504   // nothing to do
6505 }
6506 
6507 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6508   // nothing to do
6509 }
6510 
6511 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6512   TL.setExpansionLoc(readSourceLocation());
6513 }
6514 
6515 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6516   TL.setCaretLoc(readSourceLocation());
6517 }
6518 
6519 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6520   TL.setAmpLoc(readSourceLocation());
6521 }
6522 
6523 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6524   TL.setAmpAmpLoc(readSourceLocation());
6525 }
6526 
6527 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6528   TL.setStarLoc(readSourceLocation());
6529   TL.setClassTInfo(GetTypeSourceInfo());
6530 }
6531 
6532 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6533   TL.setLBracketLoc(readSourceLocation());
6534   TL.setRBracketLoc(readSourceLocation());
6535   if (Reader.readBool())
6536     TL.setSizeExpr(Reader.readExpr());
6537   else
6538     TL.setSizeExpr(nullptr);
6539 }
6540 
6541 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6542   VisitArrayTypeLoc(TL);
6543 }
6544 
6545 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6546   VisitArrayTypeLoc(TL);
6547 }
6548 
6549 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6550   VisitArrayTypeLoc(TL);
6551 }
6552 
6553 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6554                                             DependentSizedArrayTypeLoc TL) {
6555   VisitArrayTypeLoc(TL);
6556 }
6557 
6558 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6559     DependentAddressSpaceTypeLoc TL) {
6560 
6561     TL.setAttrNameLoc(readSourceLocation());
6562     TL.setAttrOperandParensRange(Reader.readSourceRange());
6563     TL.setAttrExprOperand(Reader.readExpr());
6564 }
6565 
6566 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6567                                         DependentSizedExtVectorTypeLoc TL) {
6568   TL.setNameLoc(readSourceLocation());
6569 }
6570 
6571 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6572   TL.setNameLoc(readSourceLocation());
6573 }
6574 
6575 void TypeLocReader::VisitDependentVectorTypeLoc(
6576     DependentVectorTypeLoc TL) {
6577   TL.setNameLoc(readSourceLocation());
6578 }
6579 
6580 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6581   TL.setNameLoc(readSourceLocation());
6582 }
6583 
6584 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6585   TL.setAttrNameLoc(readSourceLocation());
6586   TL.setAttrOperandParensRange(Reader.readSourceRange());
6587   TL.setAttrRowOperand(Reader.readExpr());
6588   TL.setAttrColumnOperand(Reader.readExpr());
6589 }
6590 
6591 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6592     DependentSizedMatrixTypeLoc TL) {
6593   TL.setAttrNameLoc(readSourceLocation());
6594   TL.setAttrOperandParensRange(Reader.readSourceRange());
6595   TL.setAttrRowOperand(Reader.readExpr());
6596   TL.setAttrColumnOperand(Reader.readExpr());
6597 }
6598 
6599 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6600   TL.setLocalRangeBegin(readSourceLocation());
6601   TL.setLParenLoc(readSourceLocation());
6602   TL.setRParenLoc(readSourceLocation());
6603   TL.setExceptionSpecRange(Reader.readSourceRange());
6604   TL.setLocalRangeEnd(readSourceLocation());
6605   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6606     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6607   }
6608 }
6609 
6610 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6611   VisitFunctionTypeLoc(TL);
6612 }
6613 
6614 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6615   VisitFunctionTypeLoc(TL);
6616 }
6617 
6618 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6619   TL.setNameLoc(readSourceLocation());
6620 }
6621 
6622 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6623   TL.setNameLoc(readSourceLocation());
6624 }
6625 
6626 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6627   TL.setTypeofLoc(readSourceLocation());
6628   TL.setLParenLoc(readSourceLocation());
6629   TL.setRParenLoc(readSourceLocation());
6630 }
6631 
6632 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6633   TL.setTypeofLoc(readSourceLocation());
6634   TL.setLParenLoc(readSourceLocation());
6635   TL.setRParenLoc(readSourceLocation());
6636   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6637 }
6638 
6639 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6640   TL.setNameLoc(readSourceLocation());
6641 }
6642 
6643 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6644   TL.setKWLoc(readSourceLocation());
6645   TL.setLParenLoc(readSourceLocation());
6646   TL.setRParenLoc(readSourceLocation());
6647   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6648 }
6649 
6650 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6651   TL.setNameLoc(readSourceLocation());
6652   if (Reader.readBool()) {
6653     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6654     TL.setTemplateKWLoc(readSourceLocation());
6655     TL.setConceptNameLoc(readSourceLocation());
6656     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6657     TL.setLAngleLoc(readSourceLocation());
6658     TL.setRAngleLoc(readSourceLocation());
6659     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6660       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6661                               TL.getTypePtr()->getArg(i).getKind()));
6662   }
6663 }
6664 
6665 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6666     DeducedTemplateSpecializationTypeLoc TL) {
6667   TL.setTemplateNameLoc(readSourceLocation());
6668 }
6669 
6670 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6671   TL.setNameLoc(readSourceLocation());
6672 }
6673 
6674 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6675   TL.setNameLoc(readSourceLocation());
6676 }
6677 
6678 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6679   TL.setAttr(ReadAttr());
6680 }
6681 
6682 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6683   TL.setNameLoc(readSourceLocation());
6684 }
6685 
6686 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6687                                             SubstTemplateTypeParmTypeLoc TL) {
6688   TL.setNameLoc(readSourceLocation());
6689 }
6690 
6691 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6692                                           SubstTemplateTypeParmPackTypeLoc TL) {
6693   TL.setNameLoc(readSourceLocation());
6694 }
6695 
6696 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6697                                            TemplateSpecializationTypeLoc TL) {
6698   TL.setTemplateKeywordLoc(readSourceLocation());
6699   TL.setTemplateNameLoc(readSourceLocation());
6700   TL.setLAngleLoc(readSourceLocation());
6701   TL.setRAngleLoc(readSourceLocation());
6702   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6703     TL.setArgLocInfo(
6704         i,
6705         Reader.readTemplateArgumentLocInfo(
6706           TL.getTypePtr()->getArg(i).getKind()));
6707 }
6708 
6709 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6710   TL.setLParenLoc(readSourceLocation());
6711   TL.setRParenLoc(readSourceLocation());
6712 }
6713 
6714 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6715   TL.setElaboratedKeywordLoc(readSourceLocation());
6716   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6717 }
6718 
6719 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6720   TL.setNameLoc(readSourceLocation());
6721 }
6722 
6723 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6724   TL.setElaboratedKeywordLoc(readSourceLocation());
6725   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6726   TL.setNameLoc(readSourceLocation());
6727 }
6728 
6729 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6730        DependentTemplateSpecializationTypeLoc TL) {
6731   TL.setElaboratedKeywordLoc(readSourceLocation());
6732   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6733   TL.setTemplateKeywordLoc(readSourceLocation());
6734   TL.setTemplateNameLoc(readSourceLocation());
6735   TL.setLAngleLoc(readSourceLocation());
6736   TL.setRAngleLoc(readSourceLocation());
6737   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6738     TL.setArgLocInfo(
6739         I,
6740         Reader.readTemplateArgumentLocInfo(
6741             TL.getTypePtr()->getArg(I).getKind()));
6742 }
6743 
6744 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6745   TL.setEllipsisLoc(readSourceLocation());
6746 }
6747 
6748 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6749   TL.setNameLoc(readSourceLocation());
6750 }
6751 
6752 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6753   if (TL.getNumProtocols()) {
6754     TL.setProtocolLAngleLoc(readSourceLocation());
6755     TL.setProtocolRAngleLoc(readSourceLocation());
6756   }
6757   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6758     TL.setProtocolLoc(i, readSourceLocation());
6759 }
6760 
6761 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6762   TL.setHasBaseTypeAsWritten(Reader.readBool());
6763   TL.setTypeArgsLAngleLoc(readSourceLocation());
6764   TL.setTypeArgsRAngleLoc(readSourceLocation());
6765   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6766     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6767   TL.setProtocolLAngleLoc(readSourceLocation());
6768   TL.setProtocolRAngleLoc(readSourceLocation());
6769   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6770     TL.setProtocolLoc(i, readSourceLocation());
6771 }
6772 
6773 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6774   TL.setStarLoc(readSourceLocation());
6775 }
6776 
6777 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6778   TL.setKWLoc(readSourceLocation());
6779   TL.setLParenLoc(readSourceLocation());
6780   TL.setRParenLoc(readSourceLocation());
6781 }
6782 
6783 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6784   TL.setKWLoc(readSourceLocation());
6785 }
6786 
6787 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6788   TL.setNameLoc(readSourceLocation());
6789 }
6790 void TypeLocReader::VisitDependentExtIntTypeLoc(
6791     clang::DependentExtIntTypeLoc TL) {
6792   TL.setNameLoc(readSourceLocation());
6793 }
6794 
6795 
6796 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6797   TypeLocReader TLR(*this);
6798   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6799     TLR.Visit(TL);
6800 }
6801 
6802 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6803   QualType InfoTy = readType();
6804   if (InfoTy.isNull())
6805     return nullptr;
6806 
6807   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6808   readTypeLoc(TInfo->getTypeLoc());
6809   return TInfo;
6810 }
6811 
6812 QualType ASTReader::GetType(TypeID ID) {
6813   assert(ContextObj && "reading type with no AST context");
6814   ASTContext &Context = *ContextObj;
6815 
6816   unsigned FastQuals = ID & Qualifiers::FastMask;
6817   unsigned Index = ID >> Qualifiers::FastWidth;
6818 
6819   if (Index < NUM_PREDEF_TYPE_IDS) {
6820     QualType T;
6821     switch ((PredefinedTypeIDs)Index) {
6822     case PREDEF_TYPE_NULL_ID:
6823       return QualType();
6824     case PREDEF_TYPE_VOID_ID:
6825       T = Context.VoidTy;
6826       break;
6827     case PREDEF_TYPE_BOOL_ID:
6828       T = Context.BoolTy;
6829       break;
6830     case PREDEF_TYPE_CHAR_U_ID:
6831     case PREDEF_TYPE_CHAR_S_ID:
6832       // FIXME: Check that the signedness of CharTy is correct!
6833       T = Context.CharTy;
6834       break;
6835     case PREDEF_TYPE_UCHAR_ID:
6836       T = Context.UnsignedCharTy;
6837       break;
6838     case PREDEF_TYPE_USHORT_ID:
6839       T = Context.UnsignedShortTy;
6840       break;
6841     case PREDEF_TYPE_UINT_ID:
6842       T = Context.UnsignedIntTy;
6843       break;
6844     case PREDEF_TYPE_ULONG_ID:
6845       T = Context.UnsignedLongTy;
6846       break;
6847     case PREDEF_TYPE_ULONGLONG_ID:
6848       T = Context.UnsignedLongLongTy;
6849       break;
6850     case PREDEF_TYPE_UINT128_ID:
6851       T = Context.UnsignedInt128Ty;
6852       break;
6853     case PREDEF_TYPE_SCHAR_ID:
6854       T = Context.SignedCharTy;
6855       break;
6856     case PREDEF_TYPE_WCHAR_ID:
6857       T = Context.WCharTy;
6858       break;
6859     case PREDEF_TYPE_SHORT_ID:
6860       T = Context.ShortTy;
6861       break;
6862     case PREDEF_TYPE_INT_ID:
6863       T = Context.IntTy;
6864       break;
6865     case PREDEF_TYPE_LONG_ID:
6866       T = Context.LongTy;
6867       break;
6868     case PREDEF_TYPE_LONGLONG_ID:
6869       T = Context.LongLongTy;
6870       break;
6871     case PREDEF_TYPE_INT128_ID:
6872       T = Context.Int128Ty;
6873       break;
6874     case PREDEF_TYPE_BFLOAT16_ID:
6875       T = Context.BFloat16Ty;
6876       break;
6877     case PREDEF_TYPE_HALF_ID:
6878       T = Context.HalfTy;
6879       break;
6880     case PREDEF_TYPE_FLOAT_ID:
6881       T = Context.FloatTy;
6882       break;
6883     case PREDEF_TYPE_DOUBLE_ID:
6884       T = Context.DoubleTy;
6885       break;
6886     case PREDEF_TYPE_LONGDOUBLE_ID:
6887       T = Context.LongDoubleTy;
6888       break;
6889     case PREDEF_TYPE_SHORT_ACCUM_ID:
6890       T = Context.ShortAccumTy;
6891       break;
6892     case PREDEF_TYPE_ACCUM_ID:
6893       T = Context.AccumTy;
6894       break;
6895     case PREDEF_TYPE_LONG_ACCUM_ID:
6896       T = Context.LongAccumTy;
6897       break;
6898     case PREDEF_TYPE_USHORT_ACCUM_ID:
6899       T = Context.UnsignedShortAccumTy;
6900       break;
6901     case PREDEF_TYPE_UACCUM_ID:
6902       T = Context.UnsignedAccumTy;
6903       break;
6904     case PREDEF_TYPE_ULONG_ACCUM_ID:
6905       T = Context.UnsignedLongAccumTy;
6906       break;
6907     case PREDEF_TYPE_SHORT_FRACT_ID:
6908       T = Context.ShortFractTy;
6909       break;
6910     case PREDEF_TYPE_FRACT_ID:
6911       T = Context.FractTy;
6912       break;
6913     case PREDEF_TYPE_LONG_FRACT_ID:
6914       T = Context.LongFractTy;
6915       break;
6916     case PREDEF_TYPE_USHORT_FRACT_ID:
6917       T = Context.UnsignedShortFractTy;
6918       break;
6919     case PREDEF_TYPE_UFRACT_ID:
6920       T = Context.UnsignedFractTy;
6921       break;
6922     case PREDEF_TYPE_ULONG_FRACT_ID:
6923       T = Context.UnsignedLongFractTy;
6924       break;
6925     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6926       T = Context.SatShortAccumTy;
6927       break;
6928     case PREDEF_TYPE_SAT_ACCUM_ID:
6929       T = Context.SatAccumTy;
6930       break;
6931     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6932       T = Context.SatLongAccumTy;
6933       break;
6934     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6935       T = Context.SatUnsignedShortAccumTy;
6936       break;
6937     case PREDEF_TYPE_SAT_UACCUM_ID:
6938       T = Context.SatUnsignedAccumTy;
6939       break;
6940     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6941       T = Context.SatUnsignedLongAccumTy;
6942       break;
6943     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6944       T = Context.SatShortFractTy;
6945       break;
6946     case PREDEF_TYPE_SAT_FRACT_ID:
6947       T = Context.SatFractTy;
6948       break;
6949     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6950       T = Context.SatLongFractTy;
6951       break;
6952     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6953       T = Context.SatUnsignedShortFractTy;
6954       break;
6955     case PREDEF_TYPE_SAT_UFRACT_ID:
6956       T = Context.SatUnsignedFractTy;
6957       break;
6958     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6959       T = Context.SatUnsignedLongFractTy;
6960       break;
6961     case PREDEF_TYPE_FLOAT16_ID:
6962       T = Context.Float16Ty;
6963       break;
6964     case PREDEF_TYPE_FLOAT128_ID:
6965       T = Context.Float128Ty;
6966       break;
6967     case PREDEF_TYPE_OVERLOAD_ID:
6968       T = Context.OverloadTy;
6969       break;
6970     case PREDEF_TYPE_BOUND_MEMBER:
6971       T = Context.BoundMemberTy;
6972       break;
6973     case PREDEF_TYPE_PSEUDO_OBJECT:
6974       T = Context.PseudoObjectTy;
6975       break;
6976     case PREDEF_TYPE_DEPENDENT_ID:
6977       T = Context.DependentTy;
6978       break;
6979     case PREDEF_TYPE_UNKNOWN_ANY:
6980       T = Context.UnknownAnyTy;
6981       break;
6982     case PREDEF_TYPE_NULLPTR_ID:
6983       T = Context.NullPtrTy;
6984       break;
6985     case PREDEF_TYPE_CHAR8_ID:
6986       T = Context.Char8Ty;
6987       break;
6988     case PREDEF_TYPE_CHAR16_ID:
6989       T = Context.Char16Ty;
6990       break;
6991     case PREDEF_TYPE_CHAR32_ID:
6992       T = Context.Char32Ty;
6993       break;
6994     case PREDEF_TYPE_OBJC_ID:
6995       T = Context.ObjCBuiltinIdTy;
6996       break;
6997     case PREDEF_TYPE_OBJC_CLASS:
6998       T = Context.ObjCBuiltinClassTy;
6999       break;
7000     case PREDEF_TYPE_OBJC_SEL:
7001       T = Context.ObjCBuiltinSelTy;
7002       break;
7003 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7004     case PREDEF_TYPE_##Id##_ID: \
7005       T = Context.SingletonId; \
7006       break;
7007 #include "clang/Basic/OpenCLImageTypes.def"
7008 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7009     case PREDEF_TYPE_##Id##_ID: \
7010       T = Context.Id##Ty; \
7011       break;
7012 #include "clang/Basic/OpenCLExtensionTypes.def"
7013     case PREDEF_TYPE_SAMPLER_ID:
7014       T = Context.OCLSamplerTy;
7015       break;
7016     case PREDEF_TYPE_EVENT_ID:
7017       T = Context.OCLEventTy;
7018       break;
7019     case PREDEF_TYPE_CLK_EVENT_ID:
7020       T = Context.OCLClkEventTy;
7021       break;
7022     case PREDEF_TYPE_QUEUE_ID:
7023       T = Context.OCLQueueTy;
7024       break;
7025     case PREDEF_TYPE_RESERVE_ID_ID:
7026       T = Context.OCLReserveIDTy;
7027       break;
7028     case PREDEF_TYPE_AUTO_DEDUCT:
7029       T = Context.getAutoDeductType();
7030       break;
7031     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7032       T = Context.getAutoRRefDeductType();
7033       break;
7034     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7035       T = Context.ARCUnbridgedCastTy;
7036       break;
7037     case PREDEF_TYPE_BUILTIN_FN:
7038       T = Context.BuiltinFnTy;
7039       break;
7040     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7041       T = Context.IncompleteMatrixIdxTy;
7042       break;
7043     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7044       T = Context.OMPArraySectionTy;
7045       break;
7046     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7047       T = Context.OMPArraySectionTy;
7048       break;
7049     case PREDEF_TYPE_OMP_ITERATOR:
7050       T = Context.OMPIteratorTy;
7051       break;
7052 #define SVE_TYPE(Name, Id, SingletonId) \
7053     case PREDEF_TYPE_##Id##_ID: \
7054       T = Context.SingletonId; \
7055       break;
7056 #include "clang/Basic/AArch64SVEACLETypes.def"
7057 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7058     case PREDEF_TYPE_##Id##_ID: \
7059       T = Context.Id##Ty; \
7060       break;
7061 #include "clang/Basic/PPCTypes.def"
7062 #define RVV_TYPE(Name, Id, SingletonId) \
7063     case PREDEF_TYPE_##Id##_ID: \
7064       T = Context.SingletonId; \
7065       break;
7066 #include "clang/Basic/RISCVVTypes.def"
7067     }
7068 
7069     assert(!T.isNull() && "Unknown predefined type");
7070     return T.withFastQualifiers(FastQuals);
7071   }
7072 
7073   Index -= NUM_PREDEF_TYPE_IDS;
7074   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7075   if (TypesLoaded[Index].isNull()) {
7076     TypesLoaded[Index] = readTypeRecord(Index);
7077     if (TypesLoaded[Index].isNull())
7078       return QualType();
7079 
7080     TypesLoaded[Index]->setFromAST();
7081     if (DeserializationListener)
7082       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7083                                         TypesLoaded[Index]);
7084   }
7085 
7086   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7087 }
7088 
7089 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7090   return GetType(getGlobalTypeID(F, LocalID));
7091 }
7092 
7093 serialization::TypeID
7094 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7095   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7096   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7097 
7098   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7099     return LocalID;
7100 
7101   if (!F.ModuleOffsetMap.empty())
7102     ReadModuleOffsetMap(F);
7103 
7104   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7105     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7106   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7107 
7108   unsigned GlobalIndex = LocalIndex + I->second;
7109   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7110 }
7111 
7112 TemplateArgumentLocInfo
7113 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7114   switch (Kind) {
7115   case TemplateArgument::Expression:
7116     return readExpr();
7117   case TemplateArgument::Type:
7118     return readTypeSourceInfo();
7119   case TemplateArgument::Template: {
7120     NestedNameSpecifierLoc QualifierLoc =
7121       readNestedNameSpecifierLoc();
7122     SourceLocation TemplateNameLoc = readSourceLocation();
7123     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7124                                    TemplateNameLoc, SourceLocation());
7125   }
7126   case TemplateArgument::TemplateExpansion: {
7127     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7128     SourceLocation TemplateNameLoc = readSourceLocation();
7129     SourceLocation EllipsisLoc = readSourceLocation();
7130     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7131                                    TemplateNameLoc, EllipsisLoc);
7132   }
7133   case TemplateArgument::Null:
7134   case TemplateArgument::Integral:
7135   case TemplateArgument::Declaration:
7136   case TemplateArgument::NullPtr:
7137   case TemplateArgument::Pack:
7138     // FIXME: Is this right?
7139     return TemplateArgumentLocInfo();
7140   }
7141   llvm_unreachable("unexpected template argument loc");
7142 }
7143 
7144 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7145   TemplateArgument Arg = readTemplateArgument();
7146 
7147   if (Arg.getKind() == TemplateArgument::Expression) {
7148     if (readBool()) // bool InfoHasSameExpr.
7149       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7150   }
7151   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7152 }
7153 
7154 const ASTTemplateArgumentListInfo *
7155 ASTRecordReader::readASTTemplateArgumentListInfo() {
7156   SourceLocation LAngleLoc = readSourceLocation();
7157   SourceLocation RAngleLoc = readSourceLocation();
7158   unsigned NumArgsAsWritten = readInt();
7159   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7160   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7161     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7162   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7163 }
7164 
7165 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7166   return GetDecl(ID);
7167 }
7168 
7169 void ASTReader::CompleteRedeclChain(const Decl *D) {
7170   if (NumCurrentElementsDeserializing) {
7171     // We arrange to not care about the complete redeclaration chain while we're
7172     // deserializing. Just remember that the AST has marked this one as complete
7173     // but that it's not actually complete yet, so we know we still need to
7174     // complete it later.
7175     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7176     return;
7177   }
7178 
7179   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7180 
7181   // If this is a named declaration, complete it by looking it up
7182   // within its context.
7183   //
7184   // FIXME: Merging a function definition should merge
7185   // all mergeable entities within it.
7186   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7187       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7188     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7189       if (!getContext().getLangOpts().CPlusPlus &&
7190           isa<TranslationUnitDecl>(DC)) {
7191         // Outside of C++, we don't have a lookup table for the TU, so update
7192         // the identifier instead. (For C++ modules, we don't store decls
7193         // in the serialized identifier table, so we do the lookup in the TU.)
7194         auto *II = Name.getAsIdentifierInfo();
7195         assert(II && "non-identifier name in C?");
7196         if (II->isOutOfDate())
7197           updateOutOfDateIdentifier(*II);
7198       } else
7199         DC->lookup(Name);
7200     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7201       // Find all declarations of this kind from the relevant context.
7202       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7203         auto *DC = cast<DeclContext>(DCDecl);
7204         SmallVector<Decl*, 8> Decls;
7205         FindExternalLexicalDecls(
7206             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7207       }
7208     }
7209   }
7210 
7211   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7212     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7213   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7214     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7215   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7216     if (auto *Template = FD->getPrimaryTemplate())
7217       Template->LoadLazySpecializations();
7218   }
7219 }
7220 
7221 CXXCtorInitializer **
7222 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7223   RecordLocation Loc = getLocalBitOffset(Offset);
7224   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7225   SavedStreamPosition SavedPosition(Cursor);
7226   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7227     Error(std::move(Err));
7228     return nullptr;
7229   }
7230   ReadingKindTracker ReadingKind(Read_Decl, *this);
7231 
7232   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7233   if (!MaybeCode) {
7234     Error(MaybeCode.takeError());
7235     return nullptr;
7236   }
7237   unsigned Code = MaybeCode.get();
7238 
7239   ASTRecordReader Record(*this, *Loc.F);
7240   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7241   if (!MaybeRecCode) {
7242     Error(MaybeRecCode.takeError());
7243     return nullptr;
7244   }
7245   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7246     Error("malformed AST file: missing C++ ctor initializers");
7247     return nullptr;
7248   }
7249 
7250   return Record.readCXXCtorInitializers();
7251 }
7252 
7253 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7254   assert(ContextObj && "reading base specifiers with no AST context");
7255   ASTContext &Context = *ContextObj;
7256 
7257   RecordLocation Loc = getLocalBitOffset(Offset);
7258   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7259   SavedStreamPosition SavedPosition(Cursor);
7260   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7261     Error(std::move(Err));
7262     return nullptr;
7263   }
7264   ReadingKindTracker ReadingKind(Read_Decl, *this);
7265 
7266   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7267   if (!MaybeCode) {
7268     Error(MaybeCode.takeError());
7269     return nullptr;
7270   }
7271   unsigned Code = MaybeCode.get();
7272 
7273   ASTRecordReader Record(*this, *Loc.F);
7274   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7275   if (!MaybeRecCode) {
7276     Error(MaybeCode.takeError());
7277     return nullptr;
7278   }
7279   unsigned RecCode = MaybeRecCode.get();
7280 
7281   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7282     Error("malformed AST file: missing C++ base specifiers");
7283     return nullptr;
7284   }
7285 
7286   unsigned NumBases = Record.readInt();
7287   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7288   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7289   for (unsigned I = 0; I != NumBases; ++I)
7290     Bases[I] = Record.readCXXBaseSpecifier();
7291   return Bases;
7292 }
7293 
7294 serialization::DeclID
7295 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7296   if (LocalID < NUM_PREDEF_DECL_IDS)
7297     return LocalID;
7298 
7299   if (!F.ModuleOffsetMap.empty())
7300     ReadModuleOffsetMap(F);
7301 
7302   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7303     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7304   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7305 
7306   return LocalID + I->second;
7307 }
7308 
7309 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7310                                    ModuleFile &M) const {
7311   // Predefined decls aren't from any module.
7312   if (ID < NUM_PREDEF_DECL_IDS)
7313     return false;
7314 
7315   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7316          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7317 }
7318 
7319 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7320   if (!D->isFromASTFile())
7321     return nullptr;
7322   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7323   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7324   return I->second;
7325 }
7326 
7327 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7328   if (ID < NUM_PREDEF_DECL_IDS)
7329     return SourceLocation();
7330 
7331   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7332 
7333   if (Index > DeclsLoaded.size()) {
7334     Error("declaration ID out-of-range for AST file");
7335     return SourceLocation();
7336   }
7337 
7338   if (Decl *D = DeclsLoaded[Index])
7339     return D->getLocation();
7340 
7341   SourceLocation Loc;
7342   DeclCursorForID(ID, Loc);
7343   return Loc;
7344 }
7345 
7346 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7347   switch (ID) {
7348   case PREDEF_DECL_NULL_ID:
7349     return nullptr;
7350 
7351   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7352     return Context.getTranslationUnitDecl();
7353 
7354   case PREDEF_DECL_OBJC_ID_ID:
7355     return Context.getObjCIdDecl();
7356 
7357   case PREDEF_DECL_OBJC_SEL_ID:
7358     return Context.getObjCSelDecl();
7359 
7360   case PREDEF_DECL_OBJC_CLASS_ID:
7361     return Context.getObjCClassDecl();
7362 
7363   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7364     return Context.getObjCProtocolDecl();
7365 
7366   case PREDEF_DECL_INT_128_ID:
7367     return Context.getInt128Decl();
7368 
7369   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7370     return Context.getUInt128Decl();
7371 
7372   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7373     return Context.getObjCInstanceTypeDecl();
7374 
7375   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7376     return Context.getBuiltinVaListDecl();
7377 
7378   case PREDEF_DECL_VA_LIST_TAG:
7379     return Context.getVaListTagDecl();
7380 
7381   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7382     return Context.getBuiltinMSVaListDecl();
7383 
7384   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7385     return Context.getMSGuidTagDecl();
7386 
7387   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7388     return Context.getExternCContextDecl();
7389 
7390   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7391     return Context.getMakeIntegerSeqDecl();
7392 
7393   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7394     return Context.getCFConstantStringDecl();
7395 
7396   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7397     return Context.getCFConstantStringTagDecl();
7398 
7399   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7400     return Context.getTypePackElementDecl();
7401   }
7402   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7403 }
7404 
7405 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7406   assert(ContextObj && "reading decl with no AST context");
7407   if (ID < NUM_PREDEF_DECL_IDS) {
7408     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7409     if (D) {
7410       // Track that we have merged the declaration with ID \p ID into the
7411       // pre-existing predefined declaration \p D.
7412       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7413       if (Merged.empty())
7414         Merged.push_back(ID);
7415     }
7416     return D;
7417   }
7418 
7419   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7420 
7421   if (Index >= DeclsLoaded.size()) {
7422     assert(0 && "declaration ID out-of-range for AST file");
7423     Error("declaration ID out-of-range for AST file");
7424     return nullptr;
7425   }
7426 
7427   return DeclsLoaded[Index];
7428 }
7429 
7430 Decl *ASTReader::GetDecl(DeclID ID) {
7431   if (ID < NUM_PREDEF_DECL_IDS)
7432     return GetExistingDecl(ID);
7433 
7434   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7435 
7436   if (Index >= DeclsLoaded.size()) {
7437     assert(0 && "declaration ID out-of-range for AST file");
7438     Error("declaration ID out-of-range for AST file");
7439     return nullptr;
7440   }
7441 
7442   if (!DeclsLoaded[Index]) {
7443     ReadDeclRecord(ID);
7444     if (DeserializationListener)
7445       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7446   }
7447 
7448   return DeclsLoaded[Index];
7449 }
7450 
7451 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7452                                                   DeclID GlobalID) {
7453   if (GlobalID < NUM_PREDEF_DECL_IDS)
7454     return GlobalID;
7455 
7456   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7457   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7458   ModuleFile *Owner = I->second;
7459 
7460   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7461     = M.GlobalToLocalDeclIDs.find(Owner);
7462   if (Pos == M.GlobalToLocalDeclIDs.end())
7463     return 0;
7464 
7465   return GlobalID - Owner->BaseDeclID + Pos->second;
7466 }
7467 
7468 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7469                                             const RecordData &Record,
7470                                             unsigned &Idx) {
7471   if (Idx >= Record.size()) {
7472     Error("Corrupted AST file");
7473     return 0;
7474   }
7475 
7476   return getGlobalDeclID(F, Record[Idx++]);
7477 }
7478 
7479 /// Resolve the offset of a statement into a statement.
7480 ///
7481 /// This operation will read a new statement from the external
7482 /// source each time it is called, and is meant to be used via a
7483 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7484 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7485   // Switch case IDs are per Decl.
7486   ClearSwitchCaseIDs();
7487 
7488   // Offset here is a global offset across the entire chain.
7489   RecordLocation Loc = getLocalBitOffset(Offset);
7490   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7491     Error(std::move(Err));
7492     return nullptr;
7493   }
7494   assert(NumCurrentElementsDeserializing == 0 &&
7495          "should not be called while already deserializing");
7496   Deserializing D(this);
7497   return ReadStmtFromStream(*Loc.F);
7498 }
7499 
7500 void ASTReader::FindExternalLexicalDecls(
7501     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7502     SmallVectorImpl<Decl *> &Decls) {
7503   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7504 
7505   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7506     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7507     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7508       auto K = (Decl::Kind)+LexicalDecls[I];
7509       if (!IsKindWeWant(K))
7510         continue;
7511 
7512       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7513 
7514       // Don't add predefined declarations to the lexical context more
7515       // than once.
7516       if (ID < NUM_PREDEF_DECL_IDS) {
7517         if (PredefsVisited[ID])
7518           continue;
7519 
7520         PredefsVisited[ID] = true;
7521       }
7522 
7523       if (Decl *D = GetLocalDecl(*M, ID)) {
7524         assert(D->getKind() == K && "wrong kind for lexical decl");
7525         if (!DC->isDeclInLexicalTraversal(D))
7526           Decls.push_back(D);
7527       }
7528     }
7529   };
7530 
7531   if (isa<TranslationUnitDecl>(DC)) {
7532     for (auto Lexical : TULexicalDecls)
7533       Visit(Lexical.first, Lexical.second);
7534   } else {
7535     auto I = LexicalDecls.find(DC);
7536     if (I != LexicalDecls.end())
7537       Visit(I->second.first, I->second.second);
7538   }
7539 
7540   ++NumLexicalDeclContextsRead;
7541 }
7542 
7543 namespace {
7544 
7545 class DeclIDComp {
7546   ASTReader &Reader;
7547   ModuleFile &Mod;
7548 
7549 public:
7550   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7551 
7552   bool operator()(LocalDeclID L, LocalDeclID R) const {
7553     SourceLocation LHS = getLocation(L);
7554     SourceLocation RHS = getLocation(R);
7555     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7556   }
7557 
7558   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7559     SourceLocation RHS = getLocation(R);
7560     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7561   }
7562 
7563   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7564     SourceLocation LHS = getLocation(L);
7565     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7566   }
7567 
7568   SourceLocation getLocation(LocalDeclID ID) const {
7569     return Reader.getSourceManager().getFileLoc(
7570             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7571   }
7572 };
7573 
7574 } // namespace
7575 
7576 void ASTReader::FindFileRegionDecls(FileID File,
7577                                     unsigned Offset, unsigned Length,
7578                                     SmallVectorImpl<Decl *> &Decls) {
7579   SourceManager &SM = getSourceManager();
7580 
7581   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7582   if (I == FileDeclIDs.end())
7583     return;
7584 
7585   FileDeclsInfo &DInfo = I->second;
7586   if (DInfo.Decls.empty())
7587     return;
7588 
7589   SourceLocation
7590     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7591   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7592 
7593   DeclIDComp DIDComp(*this, *DInfo.Mod);
7594   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7595       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7596   if (BeginIt != DInfo.Decls.begin())
7597     --BeginIt;
7598 
7599   // If we are pointing at a top-level decl inside an objc container, we need
7600   // to backtrack until we find it otherwise we will fail to report that the
7601   // region overlaps with an objc container.
7602   while (BeginIt != DInfo.Decls.begin() &&
7603          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7604              ->isTopLevelDeclInObjCContainer())
7605     --BeginIt;
7606 
7607   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7608       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7609   if (EndIt != DInfo.Decls.end())
7610     ++EndIt;
7611 
7612   for (ArrayRef<serialization::LocalDeclID>::iterator
7613          DIt = BeginIt; DIt != EndIt; ++DIt)
7614     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7615 }
7616 
7617 bool
7618 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7619                                           DeclarationName Name) {
7620   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7621          "DeclContext has no visible decls in storage");
7622   if (!Name)
7623     return false;
7624 
7625   auto It = Lookups.find(DC);
7626   if (It == Lookups.end())
7627     return false;
7628 
7629   Deserializing LookupResults(this);
7630 
7631   // Load the list of declarations.
7632   SmallVector<NamedDecl *, 64> Decls;
7633   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7634   for (DeclID ID : It->second.Table.find(Name)) {
7635     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7636     if (ND->getDeclName() == Name && Found.insert(ND).second)
7637       Decls.push_back(ND);
7638   }
7639 
7640   ++NumVisibleDeclContextsRead;
7641   SetExternalVisibleDeclsForName(DC, Name, Decls);
7642   return !Decls.empty();
7643 }
7644 
7645 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7646   if (!DC->hasExternalVisibleStorage())
7647     return;
7648 
7649   auto It = Lookups.find(DC);
7650   assert(It != Lookups.end() &&
7651          "have external visible storage but no lookup tables");
7652 
7653   DeclsMap Decls;
7654 
7655   for (DeclID ID : It->second.Table.findAll()) {
7656     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7657     Decls[ND->getDeclName()].push_back(ND);
7658   }
7659 
7660   ++NumVisibleDeclContextsRead;
7661 
7662   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7663     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7664   }
7665   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7666 }
7667 
7668 const serialization::reader::DeclContextLookupTable *
7669 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7670   auto I = Lookups.find(Primary);
7671   return I == Lookups.end() ? nullptr : &I->second;
7672 }
7673 
7674 /// Under non-PCH compilation the consumer receives the objc methods
7675 /// before receiving the implementation, and codegen depends on this.
7676 /// We simulate this by deserializing and passing to consumer the methods of the
7677 /// implementation before passing the deserialized implementation decl.
7678 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7679                                        ASTConsumer *Consumer) {
7680   assert(ImplD && Consumer);
7681 
7682   for (auto *I : ImplD->methods())
7683     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7684 
7685   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7686 }
7687 
7688 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7689   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7690     PassObjCImplDeclToConsumer(ImplD, Consumer);
7691   else
7692     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7693 }
7694 
7695 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7696   this->Consumer = Consumer;
7697 
7698   if (Consumer)
7699     PassInterestingDeclsToConsumer();
7700 
7701   if (DeserializationListener)
7702     DeserializationListener->ReaderInitialized(this);
7703 }
7704 
7705 void ASTReader::PrintStats() {
7706   std::fprintf(stderr, "*** AST File Statistics:\n");
7707 
7708   unsigned NumTypesLoaded
7709     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7710                                       QualType());
7711   unsigned NumDeclsLoaded
7712     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7713                                       (Decl *)nullptr);
7714   unsigned NumIdentifiersLoaded
7715     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7716                                             IdentifiersLoaded.end(),
7717                                             (IdentifierInfo *)nullptr);
7718   unsigned NumMacrosLoaded
7719     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7720                                        MacrosLoaded.end(),
7721                                        (MacroInfo *)nullptr);
7722   unsigned NumSelectorsLoaded
7723     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7724                                           SelectorsLoaded.end(),
7725                                           Selector());
7726 
7727   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7728     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7729                  NumSLocEntriesRead, TotalNumSLocEntries,
7730                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7731   if (!TypesLoaded.empty())
7732     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7733                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7734                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7735   if (!DeclsLoaded.empty())
7736     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7737                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7738                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7739   if (!IdentifiersLoaded.empty())
7740     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7741                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7742                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7743   if (!MacrosLoaded.empty())
7744     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7745                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7746                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7747   if (!SelectorsLoaded.empty())
7748     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7749                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7750                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7751   if (TotalNumStatements)
7752     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7753                  NumStatementsRead, TotalNumStatements,
7754                  ((float)NumStatementsRead/TotalNumStatements * 100));
7755   if (TotalNumMacros)
7756     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7757                  NumMacrosRead, TotalNumMacros,
7758                  ((float)NumMacrosRead/TotalNumMacros * 100));
7759   if (TotalLexicalDeclContexts)
7760     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7761                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7762                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7763                   * 100));
7764   if (TotalVisibleDeclContexts)
7765     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7766                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7767                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7768                   * 100));
7769   if (TotalNumMethodPoolEntries)
7770     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7771                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7772                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7773                   * 100));
7774   if (NumMethodPoolLookups)
7775     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7776                  NumMethodPoolHits, NumMethodPoolLookups,
7777                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7778   if (NumMethodPoolTableLookups)
7779     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7780                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7781                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7782                   * 100.0));
7783   if (NumIdentifierLookupHits)
7784     std::fprintf(stderr,
7785                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7786                  NumIdentifierLookupHits, NumIdentifierLookups,
7787                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7788 
7789   if (GlobalIndex) {
7790     std::fprintf(stderr, "\n");
7791     GlobalIndex->printStats();
7792   }
7793 
7794   std::fprintf(stderr, "\n");
7795   dump();
7796   std::fprintf(stderr, "\n");
7797 }
7798 
7799 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7800 LLVM_DUMP_METHOD static void
7801 dumpModuleIDMap(StringRef Name,
7802                 const ContinuousRangeMap<Key, ModuleFile *,
7803                                          InitialCapacity> &Map) {
7804   if (Map.begin() == Map.end())
7805     return;
7806 
7807   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7808 
7809   llvm::errs() << Name << ":\n";
7810   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7811        I != IEnd; ++I) {
7812     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7813       << "\n";
7814   }
7815 }
7816 
7817 LLVM_DUMP_METHOD void ASTReader::dump() {
7818   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7819   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7820   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7821   dumpModuleIDMap("Global type map", GlobalTypeMap);
7822   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7823   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7824   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7825   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7826   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7827   dumpModuleIDMap("Global preprocessed entity map",
7828                   GlobalPreprocessedEntityMap);
7829 
7830   llvm::errs() << "\n*** PCH/Modules Loaded:";
7831   for (ModuleFile &M : ModuleMgr)
7832     M.dump();
7833 }
7834 
7835 /// Return the amount of memory used by memory buffers, breaking down
7836 /// by heap-backed versus mmap'ed memory.
7837 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7838   for (ModuleFile &I : ModuleMgr) {
7839     if (llvm::MemoryBuffer *buf = I.Buffer) {
7840       size_t bytes = buf->getBufferSize();
7841       switch (buf->getBufferKind()) {
7842         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7843           sizes.malloc_bytes += bytes;
7844           break;
7845         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7846           sizes.mmap_bytes += bytes;
7847           break;
7848       }
7849     }
7850   }
7851 }
7852 
7853 void ASTReader::InitializeSema(Sema &S) {
7854   SemaObj = &S;
7855   S.addExternalSource(this);
7856 
7857   // Makes sure any declarations that were deserialized "too early"
7858   // still get added to the identifier's declaration chains.
7859   for (uint64_t ID : PreloadedDeclIDs) {
7860     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7861     pushExternalDeclIntoScope(D, D->getDeclName());
7862   }
7863   PreloadedDeclIDs.clear();
7864 
7865   // FIXME: What happens if these are changed by a module import?
7866   if (!FPPragmaOptions.empty()) {
7867     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7868     FPOptionsOverride NewOverrides =
7869         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7870     SemaObj->CurFPFeatures =
7871         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7872   }
7873 
7874   SemaObj->OpenCLFeatures = OpenCLExtensions;
7875 
7876   UpdateSema();
7877 }
7878 
7879 void ASTReader::UpdateSema() {
7880   assert(SemaObj && "no Sema to update");
7881 
7882   // Load the offsets of the declarations that Sema references.
7883   // They will be lazily deserialized when needed.
7884   if (!SemaDeclRefs.empty()) {
7885     assert(SemaDeclRefs.size() % 3 == 0);
7886     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7887       if (!SemaObj->StdNamespace)
7888         SemaObj->StdNamespace = SemaDeclRefs[I];
7889       if (!SemaObj->StdBadAlloc)
7890         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7891       if (!SemaObj->StdAlignValT)
7892         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7893     }
7894     SemaDeclRefs.clear();
7895   }
7896 
7897   // Update the state of pragmas. Use the same API as if we had encountered the
7898   // pragma in the source.
7899   if(OptimizeOffPragmaLocation.isValid())
7900     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7901   if (PragmaMSStructState != -1)
7902     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7903   if (PointersToMembersPragmaLocation.isValid()) {
7904     SemaObj->ActOnPragmaMSPointersToMembers(
7905         (LangOptions::PragmaMSPointersToMembersKind)
7906             PragmaMSPointersToMembersState,
7907         PointersToMembersPragmaLocation);
7908   }
7909   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7910 
7911   if (PragmaAlignPackCurrentValue) {
7912     // The bottom of the stack might have a default value. It must be adjusted
7913     // to the current value to ensure that the packing state is preserved after
7914     // popping entries that were included/imported from a PCH/module.
7915     bool DropFirst = false;
7916     if (!PragmaAlignPackStack.empty() &&
7917         PragmaAlignPackStack.front().Location.isInvalid()) {
7918       assert(PragmaAlignPackStack.front().Value ==
7919                  SemaObj->AlignPackStack.DefaultValue &&
7920              "Expected a default alignment value");
7921       SemaObj->AlignPackStack.Stack.emplace_back(
7922           PragmaAlignPackStack.front().SlotLabel,
7923           SemaObj->AlignPackStack.CurrentValue,
7924           SemaObj->AlignPackStack.CurrentPragmaLocation,
7925           PragmaAlignPackStack.front().PushLocation);
7926       DropFirst = true;
7927     }
7928     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7929                                  .drop_front(DropFirst ? 1 : 0)) {
7930       SemaObj->AlignPackStack.Stack.emplace_back(
7931           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7932     }
7933     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7934       assert(*PragmaAlignPackCurrentValue ==
7935                  SemaObj->AlignPackStack.DefaultValue &&
7936              "Expected a default align and pack value");
7937       // Keep the current values.
7938     } else {
7939       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7940       SemaObj->AlignPackStack.CurrentPragmaLocation =
7941           PragmaAlignPackCurrentLocation;
7942     }
7943   }
7944   if (FpPragmaCurrentValue) {
7945     // The bottom of the stack might have a default value. It must be adjusted
7946     // to the current value to ensure that fp-pragma state is preserved after
7947     // popping entries that were included/imported from a PCH/module.
7948     bool DropFirst = false;
7949     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7950       assert(FpPragmaStack.front().Value ==
7951                  SemaObj->FpPragmaStack.DefaultValue &&
7952              "Expected a default pragma float_control value");
7953       SemaObj->FpPragmaStack.Stack.emplace_back(
7954           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7955           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7956           FpPragmaStack.front().PushLocation);
7957       DropFirst = true;
7958     }
7959     for (const auto &Entry :
7960          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7961       SemaObj->FpPragmaStack.Stack.emplace_back(
7962           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7963     if (FpPragmaCurrentLocation.isInvalid()) {
7964       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7965              "Expected a default pragma float_control value");
7966       // Keep the current values.
7967     } else {
7968       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7969       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7970     }
7971   }
7972 
7973   // For non-modular AST files, restore visiblity of modules.
7974   for (auto &Import : ImportedModules) {
7975     if (Import.ImportLoc.isInvalid())
7976       continue;
7977     if (Module *Imported = getSubmodule(Import.ID)) {
7978       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7979     }
7980   }
7981 }
7982 
7983 IdentifierInfo *ASTReader::get(StringRef Name) {
7984   // Note that we are loading an identifier.
7985   Deserializing AnIdentifier(this);
7986 
7987   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7988                                   NumIdentifierLookups,
7989                                   NumIdentifierLookupHits);
7990 
7991   // We don't need to do identifier table lookups in C++ modules (we preload
7992   // all interesting declarations, and don't need to use the scope for name
7993   // lookups). Perform the lookup in PCH files, though, since we don't build
7994   // a complete initial identifier table if we're carrying on from a PCH.
7995   if (PP.getLangOpts().CPlusPlus) {
7996     for (auto F : ModuleMgr.pch_modules())
7997       if (Visitor(*F))
7998         break;
7999   } else {
8000     // If there is a global index, look there first to determine which modules
8001     // provably do not have any results for this identifier.
8002     GlobalModuleIndex::HitSet Hits;
8003     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8004     if (!loadGlobalIndex()) {
8005       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8006         HitsPtr = &Hits;
8007       }
8008     }
8009 
8010     ModuleMgr.visit(Visitor, HitsPtr);
8011   }
8012 
8013   IdentifierInfo *II = Visitor.getIdentifierInfo();
8014   markIdentifierUpToDate(II);
8015   return II;
8016 }
8017 
8018 namespace clang {
8019 
8020   /// An identifier-lookup iterator that enumerates all of the
8021   /// identifiers stored within a set of AST files.
8022   class ASTIdentifierIterator : public IdentifierIterator {
8023     /// The AST reader whose identifiers are being enumerated.
8024     const ASTReader &Reader;
8025 
8026     /// The current index into the chain of AST files stored in
8027     /// the AST reader.
8028     unsigned Index;
8029 
8030     /// The current position within the identifier lookup table
8031     /// of the current AST file.
8032     ASTIdentifierLookupTable::key_iterator Current;
8033 
8034     /// The end position within the identifier lookup table of
8035     /// the current AST file.
8036     ASTIdentifierLookupTable::key_iterator End;
8037 
8038     /// Whether to skip any modules in the ASTReader.
8039     bool SkipModules;
8040 
8041   public:
8042     explicit ASTIdentifierIterator(const ASTReader &Reader,
8043                                    bool SkipModules = false);
8044 
8045     StringRef Next() override;
8046   };
8047 
8048 } // namespace clang
8049 
8050 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8051                                              bool SkipModules)
8052     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8053 }
8054 
8055 StringRef ASTIdentifierIterator::Next() {
8056   while (Current == End) {
8057     // If we have exhausted all of our AST files, we're done.
8058     if (Index == 0)
8059       return StringRef();
8060 
8061     --Index;
8062     ModuleFile &F = Reader.ModuleMgr[Index];
8063     if (SkipModules && F.isModule())
8064       continue;
8065 
8066     ASTIdentifierLookupTable *IdTable =
8067         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8068     Current = IdTable->key_begin();
8069     End = IdTable->key_end();
8070   }
8071 
8072   // We have any identifiers remaining in the current AST file; return
8073   // the next one.
8074   StringRef Result = *Current;
8075   ++Current;
8076   return Result;
8077 }
8078 
8079 namespace {
8080 
8081 /// A utility for appending two IdentifierIterators.
8082 class ChainedIdentifierIterator : public IdentifierIterator {
8083   std::unique_ptr<IdentifierIterator> Current;
8084   std::unique_ptr<IdentifierIterator> Queued;
8085 
8086 public:
8087   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8088                             std::unique_ptr<IdentifierIterator> Second)
8089       : Current(std::move(First)), Queued(std::move(Second)) {}
8090 
8091   StringRef Next() override {
8092     if (!Current)
8093       return StringRef();
8094 
8095     StringRef result = Current->Next();
8096     if (!result.empty())
8097       return result;
8098 
8099     // Try the queued iterator, which may itself be empty.
8100     Current.reset();
8101     std::swap(Current, Queued);
8102     return Next();
8103   }
8104 };
8105 
8106 } // namespace
8107 
8108 IdentifierIterator *ASTReader::getIdentifiers() {
8109   if (!loadGlobalIndex()) {
8110     std::unique_ptr<IdentifierIterator> ReaderIter(
8111         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8112     std::unique_ptr<IdentifierIterator> ModulesIter(
8113         GlobalIndex->createIdentifierIterator());
8114     return new ChainedIdentifierIterator(std::move(ReaderIter),
8115                                          std::move(ModulesIter));
8116   }
8117 
8118   return new ASTIdentifierIterator(*this);
8119 }
8120 
8121 namespace clang {
8122 namespace serialization {
8123 
8124   class ReadMethodPoolVisitor {
8125     ASTReader &Reader;
8126     Selector Sel;
8127     unsigned PriorGeneration;
8128     unsigned InstanceBits = 0;
8129     unsigned FactoryBits = 0;
8130     bool InstanceHasMoreThanOneDecl = false;
8131     bool FactoryHasMoreThanOneDecl = false;
8132     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8133     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8134 
8135   public:
8136     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8137                           unsigned PriorGeneration)
8138         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8139 
8140     bool operator()(ModuleFile &M) {
8141       if (!M.SelectorLookupTable)
8142         return false;
8143 
8144       // If we've already searched this module file, skip it now.
8145       if (M.Generation <= PriorGeneration)
8146         return true;
8147 
8148       ++Reader.NumMethodPoolTableLookups;
8149       ASTSelectorLookupTable *PoolTable
8150         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8151       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8152       if (Pos == PoolTable->end())
8153         return false;
8154 
8155       ++Reader.NumMethodPoolTableHits;
8156       ++Reader.NumSelectorsRead;
8157       // FIXME: Not quite happy with the statistics here. We probably should
8158       // disable this tracking when called via LoadSelector.
8159       // Also, should entries without methods count as misses?
8160       ++Reader.NumMethodPoolEntriesRead;
8161       ASTSelectorLookupTrait::data_type Data = *Pos;
8162       if (Reader.DeserializationListener)
8163         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8164 
8165       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8166       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8167       InstanceBits = Data.InstanceBits;
8168       FactoryBits = Data.FactoryBits;
8169       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8170       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8171       return true;
8172     }
8173 
8174     /// Retrieve the instance methods found by this visitor.
8175     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8176       return InstanceMethods;
8177     }
8178 
8179     /// Retrieve the instance methods found by this visitor.
8180     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8181       return FactoryMethods;
8182     }
8183 
8184     unsigned getInstanceBits() const { return InstanceBits; }
8185     unsigned getFactoryBits() const { return FactoryBits; }
8186 
8187     bool instanceHasMoreThanOneDecl() const {
8188       return InstanceHasMoreThanOneDecl;
8189     }
8190 
8191     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8192   };
8193 
8194 } // namespace serialization
8195 } // namespace clang
8196 
8197 /// Add the given set of methods to the method list.
8198 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8199                              ObjCMethodList &List) {
8200   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8201     S.addMethodToGlobalList(&List, Methods[I]);
8202   }
8203 }
8204 
8205 void ASTReader::ReadMethodPool(Selector Sel) {
8206   // Get the selector generation and update it to the current generation.
8207   unsigned &Generation = SelectorGeneration[Sel];
8208   unsigned PriorGeneration = Generation;
8209   Generation = getGeneration();
8210   SelectorOutOfDate[Sel] = false;
8211 
8212   // Search for methods defined with this selector.
8213   ++NumMethodPoolLookups;
8214   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8215   ModuleMgr.visit(Visitor);
8216 
8217   if (Visitor.getInstanceMethods().empty() &&
8218       Visitor.getFactoryMethods().empty())
8219     return;
8220 
8221   ++NumMethodPoolHits;
8222 
8223   if (!getSema())
8224     return;
8225 
8226   Sema &S = *getSema();
8227   Sema::GlobalMethodPool::iterator Pos
8228     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8229 
8230   Pos->second.first.setBits(Visitor.getInstanceBits());
8231   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8232   Pos->second.second.setBits(Visitor.getFactoryBits());
8233   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8234 
8235   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8236   // when building a module we keep every method individually and may need to
8237   // update hasMoreThanOneDecl as we add the methods.
8238   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8239   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8240 }
8241 
8242 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8243   if (SelectorOutOfDate[Sel])
8244     ReadMethodPool(Sel);
8245 }
8246 
8247 void ASTReader::ReadKnownNamespaces(
8248                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8249   Namespaces.clear();
8250 
8251   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8252     if (NamespaceDecl *Namespace
8253                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8254       Namespaces.push_back(Namespace);
8255   }
8256 }
8257 
8258 void ASTReader::ReadUndefinedButUsed(
8259     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8260   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8261     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8262     SourceLocation Loc =
8263         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8264     Undefined.insert(std::make_pair(D, Loc));
8265   }
8266 }
8267 
8268 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8269     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8270                                                      Exprs) {
8271   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8272     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8273     uint64_t Count = DelayedDeleteExprs[Idx++];
8274     for (uint64_t C = 0; C < Count; ++C) {
8275       SourceLocation DeleteLoc =
8276           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8277       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8278       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8279     }
8280   }
8281 }
8282 
8283 void ASTReader::ReadTentativeDefinitions(
8284                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8285   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8286     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8287     if (Var)
8288       TentativeDefs.push_back(Var);
8289   }
8290   TentativeDefinitions.clear();
8291 }
8292 
8293 void ASTReader::ReadUnusedFileScopedDecls(
8294                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8295   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8296     DeclaratorDecl *D
8297       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8298     if (D)
8299       Decls.push_back(D);
8300   }
8301   UnusedFileScopedDecls.clear();
8302 }
8303 
8304 void ASTReader::ReadDelegatingConstructors(
8305                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8306   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8307     CXXConstructorDecl *D
8308       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8309     if (D)
8310       Decls.push_back(D);
8311   }
8312   DelegatingCtorDecls.clear();
8313 }
8314 
8315 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8316   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8317     TypedefNameDecl *D
8318       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8319     if (D)
8320       Decls.push_back(D);
8321   }
8322   ExtVectorDecls.clear();
8323 }
8324 
8325 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8326     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8327   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8328        ++I) {
8329     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8330         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8331     if (D)
8332       Decls.insert(D);
8333   }
8334   UnusedLocalTypedefNameCandidates.clear();
8335 }
8336 
8337 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8338     llvm::SmallSetVector<Decl *, 4> &Decls) {
8339   for (auto I : DeclsToCheckForDeferredDiags) {
8340     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8341     if (D)
8342       Decls.insert(D);
8343   }
8344   DeclsToCheckForDeferredDiags.clear();
8345 }
8346 
8347 void ASTReader::ReadReferencedSelectors(
8348        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8349   if (ReferencedSelectorsData.empty())
8350     return;
8351 
8352   // If there are @selector references added them to its pool. This is for
8353   // implementation of -Wselector.
8354   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8355   unsigned I = 0;
8356   while (I < DataSize) {
8357     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8358     SourceLocation SelLoc
8359       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8360     Sels.push_back(std::make_pair(Sel, SelLoc));
8361   }
8362   ReferencedSelectorsData.clear();
8363 }
8364 
8365 void ASTReader::ReadWeakUndeclaredIdentifiers(
8366        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8367   if (WeakUndeclaredIdentifiers.empty())
8368     return;
8369 
8370   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8371     IdentifierInfo *WeakId
8372       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8373     IdentifierInfo *AliasId
8374       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8375     SourceLocation Loc
8376       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8377     bool Used = WeakUndeclaredIdentifiers[I++];
8378     WeakInfo WI(AliasId, Loc);
8379     WI.setUsed(Used);
8380     WeakIDs.push_back(std::make_pair(WeakId, WI));
8381   }
8382   WeakUndeclaredIdentifiers.clear();
8383 }
8384 
8385 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8386   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8387     ExternalVTableUse VT;
8388     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8389     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8390     VT.DefinitionRequired = VTableUses[Idx++];
8391     VTables.push_back(VT);
8392   }
8393 
8394   VTableUses.clear();
8395 }
8396 
8397 void ASTReader::ReadPendingInstantiations(
8398        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8399   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8400     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8401     SourceLocation Loc
8402       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8403 
8404     Pending.push_back(std::make_pair(D, Loc));
8405   }
8406   PendingInstantiations.clear();
8407 }
8408 
8409 void ASTReader::ReadLateParsedTemplates(
8410     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8411         &LPTMap) {
8412   for (auto &LPT : LateParsedTemplates) {
8413     ModuleFile *FMod = LPT.first;
8414     RecordDataImpl &LateParsed = LPT.second;
8415     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8416          /* In loop */) {
8417       FunctionDecl *FD =
8418           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8419 
8420       auto LT = std::make_unique<LateParsedTemplate>();
8421       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8422 
8423       ModuleFile *F = getOwningModuleFile(LT->D);
8424       assert(F && "No module");
8425 
8426       unsigned TokN = LateParsed[Idx++];
8427       LT->Toks.reserve(TokN);
8428       for (unsigned T = 0; T < TokN; ++T)
8429         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8430 
8431       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8432     }
8433   }
8434 }
8435 
8436 void ASTReader::LoadSelector(Selector Sel) {
8437   // It would be complicated to avoid reading the methods anyway. So don't.
8438   ReadMethodPool(Sel);
8439 }
8440 
8441 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8442   assert(ID && "Non-zero identifier ID required");
8443   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8444   IdentifiersLoaded[ID - 1] = II;
8445   if (DeserializationListener)
8446     DeserializationListener->IdentifierRead(ID, II);
8447 }
8448 
8449 /// Set the globally-visible declarations associated with the given
8450 /// identifier.
8451 ///
8452 /// If the AST reader is currently in a state where the given declaration IDs
8453 /// cannot safely be resolved, they are queued until it is safe to resolve
8454 /// them.
8455 ///
8456 /// \param II an IdentifierInfo that refers to one or more globally-visible
8457 /// declarations.
8458 ///
8459 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8460 /// visible at global scope.
8461 ///
8462 /// \param Decls if non-null, this vector will be populated with the set of
8463 /// deserialized declarations. These declarations will not be pushed into
8464 /// scope.
8465 void
8466 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8467                               const SmallVectorImpl<uint32_t> &DeclIDs,
8468                                    SmallVectorImpl<Decl *> *Decls) {
8469   if (NumCurrentElementsDeserializing && !Decls) {
8470     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8471     return;
8472   }
8473 
8474   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8475     if (!SemaObj) {
8476       // Queue this declaration so that it will be added to the
8477       // translation unit scope and identifier's declaration chain
8478       // once a Sema object is known.
8479       PreloadedDeclIDs.push_back(DeclIDs[I]);
8480       continue;
8481     }
8482 
8483     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8484 
8485     // If we're simply supposed to record the declarations, do so now.
8486     if (Decls) {
8487       Decls->push_back(D);
8488       continue;
8489     }
8490 
8491     // Introduce this declaration into the translation-unit scope
8492     // and add it to the declaration chain for this identifier, so
8493     // that (unqualified) name lookup will find it.
8494     pushExternalDeclIntoScope(D, II);
8495   }
8496 }
8497 
8498 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8499   if (ID == 0)
8500     return nullptr;
8501 
8502   if (IdentifiersLoaded.empty()) {
8503     Error("no identifier table in AST file");
8504     return nullptr;
8505   }
8506 
8507   ID -= 1;
8508   if (!IdentifiersLoaded[ID]) {
8509     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8510     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8511     ModuleFile *M = I->second;
8512     unsigned Index = ID - M->BaseIdentifierID;
8513     const unsigned char *Data =
8514         M->IdentifierTableData + M->IdentifierOffsets[Index];
8515 
8516     ASTIdentifierLookupTrait Trait(*this, *M);
8517     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8518     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8519     auto &II = PP.getIdentifierTable().get(Key);
8520     IdentifiersLoaded[ID] = &II;
8521     markIdentifierFromAST(*this,  II);
8522     if (DeserializationListener)
8523       DeserializationListener->IdentifierRead(ID + 1, &II);
8524   }
8525 
8526   return IdentifiersLoaded[ID];
8527 }
8528 
8529 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8530   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8531 }
8532 
8533 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8534   if (LocalID < NUM_PREDEF_IDENT_IDS)
8535     return LocalID;
8536 
8537   if (!M.ModuleOffsetMap.empty())
8538     ReadModuleOffsetMap(M);
8539 
8540   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8541     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8542   assert(I != M.IdentifierRemap.end()
8543          && "Invalid index into identifier index remap");
8544 
8545   return LocalID + I->second;
8546 }
8547 
8548 MacroInfo *ASTReader::getMacro(MacroID ID) {
8549   if (ID == 0)
8550     return nullptr;
8551 
8552   if (MacrosLoaded.empty()) {
8553     Error("no macro table in AST file");
8554     return nullptr;
8555   }
8556 
8557   ID -= NUM_PREDEF_MACRO_IDS;
8558   if (!MacrosLoaded[ID]) {
8559     GlobalMacroMapType::iterator I
8560       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8561     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8562     ModuleFile *M = I->second;
8563     unsigned Index = ID - M->BaseMacroID;
8564     MacrosLoaded[ID] =
8565         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8566 
8567     if (DeserializationListener)
8568       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8569                                          MacrosLoaded[ID]);
8570   }
8571 
8572   return MacrosLoaded[ID];
8573 }
8574 
8575 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8576   if (LocalID < NUM_PREDEF_MACRO_IDS)
8577     return LocalID;
8578 
8579   if (!M.ModuleOffsetMap.empty())
8580     ReadModuleOffsetMap(M);
8581 
8582   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8583     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8584   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8585 
8586   return LocalID + I->second;
8587 }
8588 
8589 serialization::SubmoduleID
8590 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8591   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8592     return LocalID;
8593 
8594   if (!M.ModuleOffsetMap.empty())
8595     ReadModuleOffsetMap(M);
8596 
8597   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8598     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8599   assert(I != M.SubmoduleRemap.end()
8600          && "Invalid index into submodule index remap");
8601 
8602   return LocalID + I->second;
8603 }
8604 
8605 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8606   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8607     assert(GlobalID == 0 && "Unhandled global submodule ID");
8608     return nullptr;
8609   }
8610 
8611   if (GlobalID > SubmodulesLoaded.size()) {
8612     Error("submodule ID out of range in AST file");
8613     return nullptr;
8614   }
8615 
8616   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8617 }
8618 
8619 Module *ASTReader::getModule(unsigned ID) {
8620   return getSubmodule(ID);
8621 }
8622 
8623 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8624   if (ID & 1) {
8625     // It's a module, look it up by submodule ID.
8626     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8627     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8628   } else {
8629     // It's a prefix (preamble, PCH, ...). Look it up by index.
8630     unsigned IndexFromEnd = ID >> 1;
8631     assert(IndexFromEnd && "got reference to unknown module file");
8632     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8633   }
8634 }
8635 
8636 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8637   if (!F)
8638     return 1;
8639 
8640   // For a file representing a module, use the submodule ID of the top-level
8641   // module as the file ID. For any other kind of file, the number of such
8642   // files loaded beforehand will be the same on reload.
8643   // FIXME: Is this true even if we have an explicit module file and a PCH?
8644   if (F->isModule())
8645     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8646 
8647   auto PCHModules = getModuleManager().pch_modules();
8648   auto I = llvm::find(PCHModules, F);
8649   assert(I != PCHModules.end() && "emitting reference to unknown file");
8650   return (I - PCHModules.end()) << 1;
8651 }
8652 
8653 llvm::Optional<ASTSourceDescriptor>
8654 ASTReader::getSourceDescriptor(unsigned ID) {
8655   if (Module *M = getSubmodule(ID))
8656     return ASTSourceDescriptor(*M);
8657 
8658   // If there is only a single PCH, return it instead.
8659   // Chained PCH are not supported.
8660   const auto &PCHChain = ModuleMgr.pch_modules();
8661   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8662     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8663     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8664     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8665     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8666                                MF.Signature);
8667   }
8668   return None;
8669 }
8670 
8671 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8672   auto I = DefinitionSource.find(FD);
8673   if (I == DefinitionSource.end())
8674     return EK_ReplyHazy;
8675   return I->second ? EK_Never : EK_Always;
8676 }
8677 
8678 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8679   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8680 }
8681 
8682 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8683   if (ID == 0)
8684     return Selector();
8685 
8686   if (ID > SelectorsLoaded.size()) {
8687     Error("selector ID out of range in AST file");
8688     return Selector();
8689   }
8690 
8691   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8692     // Load this selector from the selector table.
8693     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8694     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8695     ModuleFile &M = *I->second;
8696     ASTSelectorLookupTrait Trait(*this, M);
8697     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8698     SelectorsLoaded[ID - 1] =
8699       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8700     if (DeserializationListener)
8701       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8702   }
8703 
8704   return SelectorsLoaded[ID - 1];
8705 }
8706 
8707 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8708   return DecodeSelector(ID);
8709 }
8710 
8711 uint32_t ASTReader::GetNumExternalSelectors() {
8712   // ID 0 (the null selector) is considered an external selector.
8713   return getTotalNumSelectors() + 1;
8714 }
8715 
8716 serialization::SelectorID
8717 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8718   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8719     return LocalID;
8720 
8721   if (!M.ModuleOffsetMap.empty())
8722     ReadModuleOffsetMap(M);
8723 
8724   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8725     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8726   assert(I != M.SelectorRemap.end()
8727          && "Invalid index into selector index remap");
8728 
8729   return LocalID + I->second;
8730 }
8731 
8732 DeclarationNameLoc
8733 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8734   switch (Name.getNameKind()) {
8735   case DeclarationName::CXXConstructorName:
8736   case DeclarationName::CXXDestructorName:
8737   case DeclarationName::CXXConversionFunctionName:
8738     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8739 
8740   case DeclarationName::CXXOperatorName:
8741     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8742 
8743   case DeclarationName::CXXLiteralOperatorName:
8744     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8745         readSourceLocation());
8746 
8747   case DeclarationName::Identifier:
8748   case DeclarationName::ObjCZeroArgSelector:
8749   case DeclarationName::ObjCOneArgSelector:
8750   case DeclarationName::ObjCMultiArgSelector:
8751   case DeclarationName::CXXUsingDirective:
8752   case DeclarationName::CXXDeductionGuideName:
8753     break;
8754   }
8755   return DeclarationNameLoc();
8756 }
8757 
8758 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8759   DeclarationNameInfo NameInfo;
8760   NameInfo.setName(readDeclarationName());
8761   NameInfo.setLoc(readSourceLocation());
8762   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8763   return NameInfo;
8764 }
8765 
8766 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8767   Info.QualifierLoc = readNestedNameSpecifierLoc();
8768   unsigned NumTPLists = readInt();
8769   Info.NumTemplParamLists = NumTPLists;
8770   if (NumTPLists) {
8771     Info.TemplParamLists =
8772         new (getContext()) TemplateParameterList *[NumTPLists];
8773     for (unsigned i = 0; i != NumTPLists; ++i)
8774       Info.TemplParamLists[i] = readTemplateParameterList();
8775   }
8776 }
8777 
8778 TemplateParameterList *
8779 ASTRecordReader::readTemplateParameterList() {
8780   SourceLocation TemplateLoc = readSourceLocation();
8781   SourceLocation LAngleLoc = readSourceLocation();
8782   SourceLocation RAngleLoc = readSourceLocation();
8783 
8784   unsigned NumParams = readInt();
8785   SmallVector<NamedDecl *, 16> Params;
8786   Params.reserve(NumParams);
8787   while (NumParams--)
8788     Params.push_back(readDeclAs<NamedDecl>());
8789 
8790   bool HasRequiresClause = readBool();
8791   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8792 
8793   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8794       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8795   return TemplateParams;
8796 }
8797 
8798 void ASTRecordReader::readTemplateArgumentList(
8799                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8800                         bool Canonicalize) {
8801   unsigned NumTemplateArgs = readInt();
8802   TemplArgs.reserve(NumTemplateArgs);
8803   while (NumTemplateArgs--)
8804     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8805 }
8806 
8807 /// Read a UnresolvedSet structure.
8808 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8809   unsigned NumDecls = readInt();
8810   Set.reserve(getContext(), NumDecls);
8811   while (NumDecls--) {
8812     DeclID ID = readDeclID();
8813     AccessSpecifier AS = (AccessSpecifier) readInt();
8814     Set.addLazyDecl(getContext(), ID, AS);
8815   }
8816 }
8817 
8818 CXXBaseSpecifier
8819 ASTRecordReader::readCXXBaseSpecifier() {
8820   bool isVirtual = readBool();
8821   bool isBaseOfClass = readBool();
8822   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8823   bool inheritConstructors = readBool();
8824   TypeSourceInfo *TInfo = readTypeSourceInfo();
8825   SourceRange Range = readSourceRange();
8826   SourceLocation EllipsisLoc = readSourceLocation();
8827   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8828                           EllipsisLoc);
8829   Result.setInheritConstructors(inheritConstructors);
8830   return Result;
8831 }
8832 
8833 CXXCtorInitializer **
8834 ASTRecordReader::readCXXCtorInitializers() {
8835   ASTContext &Context = getContext();
8836   unsigned NumInitializers = readInt();
8837   assert(NumInitializers && "wrote ctor initializers but have no inits");
8838   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8839   for (unsigned i = 0; i != NumInitializers; ++i) {
8840     TypeSourceInfo *TInfo = nullptr;
8841     bool IsBaseVirtual = false;
8842     FieldDecl *Member = nullptr;
8843     IndirectFieldDecl *IndirectMember = nullptr;
8844 
8845     CtorInitializerType Type = (CtorInitializerType) readInt();
8846     switch (Type) {
8847     case CTOR_INITIALIZER_BASE:
8848       TInfo = readTypeSourceInfo();
8849       IsBaseVirtual = readBool();
8850       break;
8851 
8852     case CTOR_INITIALIZER_DELEGATING:
8853       TInfo = readTypeSourceInfo();
8854       break;
8855 
8856      case CTOR_INITIALIZER_MEMBER:
8857       Member = readDeclAs<FieldDecl>();
8858       break;
8859 
8860      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8861       IndirectMember = readDeclAs<IndirectFieldDecl>();
8862       break;
8863     }
8864 
8865     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8866     Expr *Init = readExpr();
8867     SourceLocation LParenLoc = readSourceLocation();
8868     SourceLocation RParenLoc = readSourceLocation();
8869 
8870     CXXCtorInitializer *BOMInit;
8871     if (Type == CTOR_INITIALIZER_BASE)
8872       BOMInit = new (Context)
8873           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8874                              RParenLoc, MemberOrEllipsisLoc);
8875     else if (Type == CTOR_INITIALIZER_DELEGATING)
8876       BOMInit = new (Context)
8877           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8878     else if (Member)
8879       BOMInit = new (Context)
8880           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8881                              Init, RParenLoc);
8882     else
8883       BOMInit = new (Context)
8884           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8885                              LParenLoc, Init, RParenLoc);
8886 
8887     if (/*IsWritten*/readBool()) {
8888       unsigned SourceOrder = readInt();
8889       BOMInit->setSourceOrder(SourceOrder);
8890     }
8891 
8892     CtorInitializers[i] = BOMInit;
8893   }
8894 
8895   return CtorInitializers;
8896 }
8897 
8898 NestedNameSpecifierLoc
8899 ASTRecordReader::readNestedNameSpecifierLoc() {
8900   ASTContext &Context = getContext();
8901   unsigned N = readInt();
8902   NestedNameSpecifierLocBuilder Builder;
8903   for (unsigned I = 0; I != N; ++I) {
8904     auto Kind = readNestedNameSpecifierKind();
8905     switch (Kind) {
8906     case NestedNameSpecifier::Identifier: {
8907       IdentifierInfo *II = readIdentifier();
8908       SourceRange Range = readSourceRange();
8909       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8910       break;
8911     }
8912 
8913     case NestedNameSpecifier::Namespace: {
8914       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8915       SourceRange Range = readSourceRange();
8916       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8917       break;
8918     }
8919 
8920     case NestedNameSpecifier::NamespaceAlias: {
8921       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8922       SourceRange Range = readSourceRange();
8923       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8924       break;
8925     }
8926 
8927     case NestedNameSpecifier::TypeSpec:
8928     case NestedNameSpecifier::TypeSpecWithTemplate: {
8929       bool Template = readBool();
8930       TypeSourceInfo *T = readTypeSourceInfo();
8931       if (!T)
8932         return NestedNameSpecifierLoc();
8933       SourceLocation ColonColonLoc = readSourceLocation();
8934 
8935       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8936       Builder.Extend(Context,
8937                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8938                      T->getTypeLoc(), ColonColonLoc);
8939       break;
8940     }
8941 
8942     case NestedNameSpecifier::Global: {
8943       SourceLocation ColonColonLoc = readSourceLocation();
8944       Builder.MakeGlobal(Context, ColonColonLoc);
8945       break;
8946     }
8947 
8948     case NestedNameSpecifier::Super: {
8949       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8950       SourceRange Range = readSourceRange();
8951       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8952       break;
8953     }
8954     }
8955   }
8956 
8957   return Builder.getWithLocInContext(Context);
8958 }
8959 
8960 SourceRange
8961 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8962                            unsigned &Idx) {
8963   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8964   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8965   return SourceRange(beg, end);
8966 }
8967 
8968 /// Read a floating-point value
8969 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8970   return llvm::APFloat(Sem, readAPInt());
8971 }
8972 
8973 // Read a string
8974 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8975   unsigned Len = Record[Idx++];
8976   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8977   Idx += Len;
8978   return Result;
8979 }
8980 
8981 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8982                                 unsigned &Idx) {
8983   std::string Filename = ReadString(Record, Idx);
8984   ResolveImportedPath(F, Filename);
8985   return Filename;
8986 }
8987 
8988 std::string ASTReader::ReadPath(StringRef BaseDirectory,
8989                                 const RecordData &Record, unsigned &Idx) {
8990   std::string Filename = ReadString(Record, Idx);
8991   if (!BaseDirectory.empty())
8992     ResolveImportedPath(Filename, BaseDirectory);
8993   return Filename;
8994 }
8995 
8996 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8997                                          unsigned &Idx) {
8998   unsigned Major = Record[Idx++];
8999   unsigned Minor = Record[Idx++];
9000   unsigned Subminor = Record[Idx++];
9001   if (Minor == 0)
9002     return VersionTuple(Major);
9003   if (Subminor == 0)
9004     return VersionTuple(Major, Minor - 1);
9005   return VersionTuple(Major, Minor - 1, Subminor - 1);
9006 }
9007 
9008 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9009                                           const RecordData &Record,
9010                                           unsigned &Idx) {
9011   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9012   return CXXTemporary::Create(getContext(), Decl);
9013 }
9014 
9015 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9016   return Diag(CurrentImportLoc, DiagID);
9017 }
9018 
9019 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9020   return Diags.Report(Loc, DiagID);
9021 }
9022 
9023 /// Retrieve the identifier table associated with the
9024 /// preprocessor.
9025 IdentifierTable &ASTReader::getIdentifierTable() {
9026   return PP.getIdentifierTable();
9027 }
9028 
9029 /// Record that the given ID maps to the given switch-case
9030 /// statement.
9031 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9032   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9033          "Already have a SwitchCase with this ID");
9034   (*CurrSwitchCaseStmts)[ID] = SC;
9035 }
9036 
9037 /// Retrieve the switch-case statement with the given ID.
9038 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9039   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9040   return (*CurrSwitchCaseStmts)[ID];
9041 }
9042 
9043 void ASTReader::ClearSwitchCaseIDs() {
9044   CurrSwitchCaseStmts->clear();
9045 }
9046 
9047 void ASTReader::ReadComments() {
9048   ASTContext &Context = getContext();
9049   std::vector<RawComment *> Comments;
9050   for (SmallVectorImpl<std::pair<BitstreamCursor,
9051                                  serialization::ModuleFile *>>::iterator
9052        I = CommentsCursors.begin(),
9053        E = CommentsCursors.end();
9054        I != E; ++I) {
9055     Comments.clear();
9056     BitstreamCursor &Cursor = I->first;
9057     serialization::ModuleFile &F = *I->second;
9058     SavedStreamPosition SavedPosition(Cursor);
9059 
9060     RecordData Record;
9061     while (true) {
9062       Expected<llvm::BitstreamEntry> MaybeEntry =
9063           Cursor.advanceSkippingSubblocks(
9064               BitstreamCursor::AF_DontPopBlockAtEnd);
9065       if (!MaybeEntry) {
9066         Error(MaybeEntry.takeError());
9067         return;
9068       }
9069       llvm::BitstreamEntry Entry = MaybeEntry.get();
9070 
9071       switch (Entry.Kind) {
9072       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9073       case llvm::BitstreamEntry::Error:
9074         Error("malformed block record in AST file");
9075         return;
9076       case llvm::BitstreamEntry::EndBlock:
9077         goto NextCursor;
9078       case llvm::BitstreamEntry::Record:
9079         // The interesting case.
9080         break;
9081       }
9082 
9083       // Read a record.
9084       Record.clear();
9085       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9086       if (!MaybeComment) {
9087         Error(MaybeComment.takeError());
9088         return;
9089       }
9090       switch ((CommentRecordTypes)MaybeComment.get()) {
9091       case COMMENTS_RAW_COMMENT: {
9092         unsigned Idx = 0;
9093         SourceRange SR = ReadSourceRange(F, Record, Idx);
9094         RawComment::CommentKind Kind =
9095             (RawComment::CommentKind) Record[Idx++];
9096         bool IsTrailingComment = Record[Idx++];
9097         bool IsAlmostTrailingComment = Record[Idx++];
9098         Comments.push_back(new (Context) RawComment(
9099             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9100         break;
9101       }
9102       }
9103     }
9104   NextCursor:
9105     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9106         FileToOffsetToComment;
9107     for (RawComment *C : Comments) {
9108       SourceLocation CommentLoc = C->getBeginLoc();
9109       if (CommentLoc.isValid()) {
9110         std::pair<FileID, unsigned> Loc =
9111             SourceMgr.getDecomposedLoc(CommentLoc);
9112         if (Loc.first.isValid())
9113           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9114       }
9115     }
9116   }
9117 }
9118 
9119 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9120                                 bool IncludeSystem, bool Complain,
9121                     llvm::function_ref<void(const serialization::InputFile &IF,
9122                                             bool isSystem)> Visitor) {
9123   unsigned NumUserInputs = MF.NumUserInputFiles;
9124   unsigned NumInputs = MF.InputFilesLoaded.size();
9125   assert(NumUserInputs <= NumInputs);
9126   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9127   for (unsigned I = 0; I < N; ++I) {
9128     bool IsSystem = I >= NumUserInputs;
9129     InputFile IF = getInputFile(MF, I+1, Complain);
9130     Visitor(IF, IsSystem);
9131   }
9132 }
9133 
9134 void ASTReader::visitTopLevelModuleMaps(
9135     serialization::ModuleFile &MF,
9136     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9137   unsigned NumInputs = MF.InputFilesLoaded.size();
9138   for (unsigned I = 0; I < NumInputs; ++I) {
9139     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9140     if (IFI.TopLevelModuleMap)
9141       // FIXME: This unnecessarily re-reads the InputFileInfo.
9142       if (auto FE = getInputFile(MF, I + 1).getFile())
9143         Visitor(FE);
9144   }
9145 }
9146 
9147 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9148   // If we know the owning module, use it.
9149   if (Module *M = D->getImportedOwningModule())
9150     return M->getFullModuleName();
9151 
9152   // Otherwise, use the name of the top-level module the decl is within.
9153   if (ModuleFile *M = getOwningModuleFile(D))
9154     return M->ModuleName;
9155 
9156   // Not from a module.
9157   return {};
9158 }
9159 
9160 void ASTReader::finishPendingActions() {
9161   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9162          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9163          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9164          !PendingUpdateRecords.empty()) {
9165     // If any identifiers with corresponding top-level declarations have
9166     // been loaded, load those declarations now.
9167     using TopLevelDeclsMap =
9168         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9169     TopLevelDeclsMap TopLevelDecls;
9170 
9171     while (!PendingIdentifierInfos.empty()) {
9172       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9173       SmallVector<uint32_t, 4> DeclIDs =
9174           std::move(PendingIdentifierInfos.back().second);
9175       PendingIdentifierInfos.pop_back();
9176 
9177       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9178     }
9179 
9180     // Load each function type that we deferred loading because it was a
9181     // deduced type that might refer to a local type declared within itself.
9182     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9183       auto *FD = PendingFunctionTypes[I].first;
9184       FD->setType(GetType(PendingFunctionTypes[I].second));
9185 
9186       // If we gave a function a deduced return type, remember that we need to
9187       // propagate that along the redeclaration chain.
9188       auto *DT = FD->getReturnType()->getContainedDeducedType();
9189       if (DT && DT->isDeduced())
9190         PendingDeducedTypeUpdates.insert(
9191             {FD->getCanonicalDecl(), FD->getReturnType()});
9192     }
9193     PendingFunctionTypes.clear();
9194 
9195     // For each decl chain that we wanted to complete while deserializing, mark
9196     // it as "still needs to be completed".
9197     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9198       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9199     }
9200     PendingIncompleteDeclChains.clear();
9201 
9202     // Load pending declaration chains.
9203     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9204       loadPendingDeclChain(PendingDeclChains[I].first,
9205                            PendingDeclChains[I].second);
9206     PendingDeclChains.clear();
9207 
9208     // Make the most recent of the top-level declarations visible.
9209     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9210            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9211       IdentifierInfo *II = TLD->first;
9212       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9213         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9214       }
9215     }
9216 
9217     // Load any pending macro definitions.
9218     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9219       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9220       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9221       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9222       // Initialize the macro history from chained-PCHs ahead of module imports.
9223       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9224            ++IDIdx) {
9225         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9226         if (!Info.M->isModule())
9227           resolvePendingMacro(II, Info);
9228       }
9229       // Handle module imports.
9230       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9231            ++IDIdx) {
9232         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9233         if (Info.M->isModule())
9234           resolvePendingMacro(II, Info);
9235       }
9236     }
9237     PendingMacroIDs.clear();
9238 
9239     // Wire up the DeclContexts for Decls that we delayed setting until
9240     // recursive loading is completed.
9241     while (!PendingDeclContextInfos.empty()) {
9242       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9243       PendingDeclContextInfos.pop_front();
9244       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9245       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9246       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9247     }
9248 
9249     // Perform any pending declaration updates.
9250     while (!PendingUpdateRecords.empty()) {
9251       auto Update = PendingUpdateRecords.pop_back_val();
9252       ReadingKindTracker ReadingKind(Read_Decl, *this);
9253       loadDeclUpdateRecords(Update);
9254     }
9255   }
9256 
9257   // At this point, all update records for loaded decls are in place, so any
9258   // fake class definitions should have become real.
9259   assert(PendingFakeDefinitionData.empty() &&
9260          "faked up a class definition but never saw the real one");
9261 
9262   // If we deserialized any C++ or Objective-C class definitions, any
9263   // Objective-C protocol definitions, or any redeclarable templates, make sure
9264   // that all redeclarations point to the definitions. Note that this can only
9265   // happen now, after the redeclaration chains have been fully wired.
9266   for (Decl *D : PendingDefinitions) {
9267     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9268       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9269         // Make sure that the TagType points at the definition.
9270         const_cast<TagType*>(TagT)->decl = TD;
9271       }
9272 
9273       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9274         for (auto *R = getMostRecentExistingDecl(RD); R;
9275              R = R->getPreviousDecl()) {
9276           assert((R == D) ==
9277                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9278                  "declaration thinks it's the definition but it isn't");
9279           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9280         }
9281       }
9282 
9283       continue;
9284     }
9285 
9286     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9287       // Make sure that the ObjCInterfaceType points at the definition.
9288       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9289         ->Decl = ID;
9290 
9291       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9292         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9293 
9294       continue;
9295     }
9296 
9297     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9298       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9299         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9300 
9301       continue;
9302     }
9303 
9304     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9305     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9306       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9307   }
9308   PendingDefinitions.clear();
9309 
9310   // Load the bodies of any functions or methods we've encountered. We do
9311   // this now (delayed) so that we can be sure that the declaration chains
9312   // have been fully wired up (hasBody relies on this).
9313   // FIXME: We shouldn't require complete redeclaration chains here.
9314   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9315                                PBEnd = PendingBodies.end();
9316        PB != PBEnd; ++PB) {
9317     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9318       // For a function defined inline within a class template, force the
9319       // canonical definition to be the one inside the canonical definition of
9320       // the template. This ensures that we instantiate from a correct view
9321       // of the template.
9322       //
9323       // Sadly we can't do this more generally: we can't be sure that all
9324       // copies of an arbitrary class definition will have the same members
9325       // defined (eg, some member functions may not be instantiated, and some
9326       // special members may or may not have been implicitly defined).
9327       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9328         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9329           continue;
9330 
9331       // FIXME: Check for =delete/=default?
9332       // FIXME: Complain about ODR violations here?
9333       const FunctionDecl *Defn = nullptr;
9334       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9335         FD->setLazyBody(PB->second);
9336       } else {
9337         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9338         mergeDefinitionVisibility(NonConstDefn, FD);
9339 
9340         if (!FD->isLateTemplateParsed() &&
9341             !NonConstDefn->isLateTemplateParsed() &&
9342             FD->getODRHash() != NonConstDefn->getODRHash()) {
9343           if (!isa<CXXMethodDecl>(FD)) {
9344             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9345           } else if (FD->getLexicalParent()->isFileContext() &&
9346                      NonConstDefn->getLexicalParent()->isFileContext()) {
9347             // Only diagnose out-of-line method definitions.  If they are
9348             // in class definitions, then an error will be generated when
9349             // processing the class bodies.
9350             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9351           }
9352         }
9353       }
9354       continue;
9355     }
9356 
9357     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9358     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9359       MD->setLazyBody(PB->second);
9360   }
9361   PendingBodies.clear();
9362 
9363   // Do some cleanup.
9364   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9365     getContext().deduplicateMergedDefinitonsFor(ND);
9366   PendingMergedDefinitionsToDeduplicate.clear();
9367 }
9368 
9369 void ASTReader::diagnoseOdrViolations() {
9370   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9371       PendingFunctionOdrMergeFailures.empty() &&
9372       PendingEnumOdrMergeFailures.empty())
9373     return;
9374 
9375   // Trigger the import of the full definition of each class that had any
9376   // odr-merging problems, so we can produce better diagnostics for them.
9377   // These updates may in turn find and diagnose some ODR failures, so take
9378   // ownership of the set first.
9379   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9380   PendingOdrMergeFailures.clear();
9381   for (auto &Merge : OdrMergeFailures) {
9382     Merge.first->buildLookup();
9383     Merge.first->decls_begin();
9384     Merge.first->bases_begin();
9385     Merge.first->vbases_begin();
9386     for (auto &RecordPair : Merge.second) {
9387       auto *RD = RecordPair.first;
9388       RD->decls_begin();
9389       RD->bases_begin();
9390       RD->vbases_begin();
9391     }
9392   }
9393 
9394   // Trigger the import of functions.
9395   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9396   PendingFunctionOdrMergeFailures.clear();
9397   for (auto &Merge : FunctionOdrMergeFailures) {
9398     Merge.first->buildLookup();
9399     Merge.first->decls_begin();
9400     Merge.first->getBody();
9401     for (auto &FD : Merge.second) {
9402       FD->buildLookup();
9403       FD->decls_begin();
9404       FD->getBody();
9405     }
9406   }
9407 
9408   // Trigger the import of enums.
9409   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9410   PendingEnumOdrMergeFailures.clear();
9411   for (auto &Merge : EnumOdrMergeFailures) {
9412     Merge.first->decls_begin();
9413     for (auto &Enum : Merge.second) {
9414       Enum->decls_begin();
9415     }
9416   }
9417 
9418   // For each declaration from a merged context, check that the canonical
9419   // definition of that context also contains a declaration of the same
9420   // entity.
9421   //
9422   // Caution: this loop does things that might invalidate iterators into
9423   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9424   while (!PendingOdrMergeChecks.empty()) {
9425     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9426 
9427     // FIXME: Skip over implicit declarations for now. This matters for things
9428     // like implicitly-declared special member functions. This isn't entirely
9429     // correct; we can end up with multiple unmerged declarations of the same
9430     // implicit entity.
9431     if (D->isImplicit())
9432       continue;
9433 
9434     DeclContext *CanonDef = D->getDeclContext();
9435 
9436     bool Found = false;
9437     const Decl *DCanon = D->getCanonicalDecl();
9438 
9439     for (auto RI : D->redecls()) {
9440       if (RI->getLexicalDeclContext() == CanonDef) {
9441         Found = true;
9442         break;
9443       }
9444     }
9445     if (Found)
9446       continue;
9447 
9448     // Quick check failed, time to do the slow thing. Note, we can't just
9449     // look up the name of D in CanonDef here, because the member that is
9450     // in CanonDef might not be found by name lookup (it might have been
9451     // replaced by a more recent declaration in the lookup table), and we
9452     // can't necessarily find it in the redeclaration chain because it might
9453     // be merely mergeable, not redeclarable.
9454     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9455     for (auto *CanonMember : CanonDef->decls()) {
9456       if (CanonMember->getCanonicalDecl() == DCanon) {
9457         // This can happen if the declaration is merely mergeable and not
9458         // actually redeclarable (we looked for redeclarations earlier).
9459         //
9460         // FIXME: We should be able to detect this more efficiently, without
9461         // pulling in all of the members of CanonDef.
9462         Found = true;
9463         break;
9464       }
9465       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9466         if (ND->getDeclName() == D->getDeclName())
9467           Candidates.push_back(ND);
9468     }
9469 
9470     if (!Found) {
9471       // The AST doesn't like TagDecls becoming invalid after they've been
9472       // completed. We only really need to mark FieldDecls as invalid here.
9473       if (!isa<TagDecl>(D))
9474         D->setInvalidDecl();
9475 
9476       // Ensure we don't accidentally recursively enter deserialization while
9477       // we're producing our diagnostic.
9478       Deserializing RecursionGuard(this);
9479 
9480       std::string CanonDefModule =
9481           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9482       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9483         << D << getOwningModuleNameForDiagnostic(D)
9484         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9485 
9486       if (Candidates.empty())
9487         Diag(cast<Decl>(CanonDef)->getLocation(),
9488              diag::note_module_odr_violation_no_possible_decls) << D;
9489       else {
9490         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9491           Diag(Candidates[I]->getLocation(),
9492                diag::note_module_odr_violation_possible_decl)
9493             << Candidates[I];
9494       }
9495 
9496       DiagnosedOdrMergeFailures.insert(CanonDef);
9497     }
9498   }
9499 
9500   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9501       EnumOdrMergeFailures.empty())
9502     return;
9503 
9504   // Ensure we don't accidentally recursively enter deserialization while
9505   // we're producing our diagnostics.
9506   Deserializing RecursionGuard(this);
9507 
9508   // Common code for hashing helpers.
9509   ODRHash Hash;
9510   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9511     Hash.clear();
9512     Hash.AddQualType(Ty);
9513     return Hash.CalculateHash();
9514   };
9515 
9516   auto ComputeODRHash = [&Hash](const Stmt *S) {
9517     assert(S);
9518     Hash.clear();
9519     Hash.AddStmt(S);
9520     return Hash.CalculateHash();
9521   };
9522 
9523   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9524     assert(D);
9525     Hash.clear();
9526     Hash.AddSubDecl(D);
9527     return Hash.CalculateHash();
9528   };
9529 
9530   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9531     Hash.clear();
9532     Hash.AddTemplateArgument(TA);
9533     return Hash.CalculateHash();
9534   };
9535 
9536   auto ComputeTemplateParameterListODRHash =
9537       [&Hash](const TemplateParameterList *TPL) {
9538         assert(TPL);
9539         Hash.clear();
9540         Hash.AddTemplateParameterList(TPL);
9541         return Hash.CalculateHash();
9542       };
9543 
9544   // Used with err_module_odr_violation_mismatch_decl and
9545   // note_module_odr_violation_mismatch_decl
9546   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9547   enum ODRMismatchDecl {
9548     EndOfClass,
9549     PublicSpecifer,
9550     PrivateSpecifer,
9551     ProtectedSpecifer,
9552     StaticAssert,
9553     Field,
9554     CXXMethod,
9555     TypeAlias,
9556     TypeDef,
9557     Var,
9558     Friend,
9559     FunctionTemplate,
9560     Other
9561   };
9562 
9563   // Used with err_module_odr_violation_mismatch_decl_diff and
9564   // note_module_odr_violation_mismatch_decl_diff
9565   enum ODRMismatchDeclDifference {
9566     StaticAssertCondition,
9567     StaticAssertMessage,
9568     StaticAssertOnlyMessage,
9569     FieldName,
9570     FieldTypeName,
9571     FieldSingleBitField,
9572     FieldDifferentWidthBitField,
9573     FieldSingleMutable,
9574     FieldSingleInitializer,
9575     FieldDifferentInitializers,
9576     MethodName,
9577     MethodDeleted,
9578     MethodDefaulted,
9579     MethodVirtual,
9580     MethodStatic,
9581     MethodVolatile,
9582     MethodConst,
9583     MethodInline,
9584     MethodNumberParameters,
9585     MethodParameterType,
9586     MethodParameterName,
9587     MethodParameterSingleDefaultArgument,
9588     MethodParameterDifferentDefaultArgument,
9589     MethodNoTemplateArguments,
9590     MethodDifferentNumberTemplateArguments,
9591     MethodDifferentTemplateArgument,
9592     MethodSingleBody,
9593     MethodDifferentBody,
9594     TypedefName,
9595     TypedefType,
9596     VarName,
9597     VarType,
9598     VarSingleInitializer,
9599     VarDifferentInitializer,
9600     VarConstexpr,
9601     FriendTypeFunction,
9602     FriendType,
9603     FriendFunction,
9604     FunctionTemplateDifferentNumberParameters,
9605     FunctionTemplateParameterDifferentKind,
9606     FunctionTemplateParameterName,
9607     FunctionTemplateParameterSingleDefaultArgument,
9608     FunctionTemplateParameterDifferentDefaultArgument,
9609     FunctionTemplateParameterDifferentType,
9610     FunctionTemplatePackParameter,
9611   };
9612 
9613   // These lambdas have the common portions of the ODR diagnostics.  This
9614   // has the same return as Diag(), so addition parameters can be passed
9615   // in with operator<<
9616   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9617                                  SourceLocation Loc, SourceRange Range,
9618                                  ODRMismatchDeclDifference DiffType) {
9619     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9620            << FirstRecord << FirstModule.empty() << FirstModule << Range
9621            << DiffType;
9622   };
9623   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9624                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9625     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9626            << SecondModule << Range << DiffType;
9627   };
9628 
9629   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9630                        &ComputeQualTypeODRHash, &ComputeODRHash](
9631                           NamedDecl *FirstRecord, StringRef FirstModule,
9632                           StringRef SecondModule, FieldDecl *FirstField,
9633                           FieldDecl *SecondField) {
9634     IdentifierInfo *FirstII = FirstField->getIdentifier();
9635     IdentifierInfo *SecondII = SecondField->getIdentifier();
9636     if (FirstII->getName() != SecondII->getName()) {
9637       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9638                        FirstField->getSourceRange(), FieldName)
9639           << FirstII;
9640       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9641                       SecondField->getSourceRange(), FieldName)
9642           << SecondII;
9643 
9644       return true;
9645     }
9646 
9647     assert(getContext().hasSameType(FirstField->getType(),
9648                                     SecondField->getType()));
9649 
9650     QualType FirstType = FirstField->getType();
9651     QualType SecondType = SecondField->getType();
9652     if (ComputeQualTypeODRHash(FirstType) !=
9653         ComputeQualTypeODRHash(SecondType)) {
9654       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9655                        FirstField->getSourceRange(), FieldTypeName)
9656           << FirstII << FirstType;
9657       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9658                       SecondField->getSourceRange(), FieldTypeName)
9659           << SecondII << SecondType;
9660 
9661       return true;
9662     }
9663 
9664     const bool IsFirstBitField = FirstField->isBitField();
9665     const bool IsSecondBitField = SecondField->isBitField();
9666     if (IsFirstBitField != IsSecondBitField) {
9667       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9668                        FirstField->getSourceRange(), FieldSingleBitField)
9669           << FirstII << IsFirstBitField;
9670       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9671                       SecondField->getSourceRange(), FieldSingleBitField)
9672           << SecondII << IsSecondBitField;
9673       return true;
9674     }
9675 
9676     if (IsFirstBitField && IsSecondBitField) {
9677       unsigned FirstBitWidthHash =
9678           ComputeODRHash(FirstField->getBitWidth());
9679       unsigned SecondBitWidthHash =
9680           ComputeODRHash(SecondField->getBitWidth());
9681       if (FirstBitWidthHash != SecondBitWidthHash) {
9682         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9683                          FirstField->getSourceRange(),
9684                          FieldDifferentWidthBitField)
9685             << FirstII << FirstField->getBitWidth()->getSourceRange();
9686         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9687                         SecondField->getSourceRange(),
9688                         FieldDifferentWidthBitField)
9689             << SecondII << SecondField->getBitWidth()->getSourceRange();
9690         return true;
9691       }
9692     }
9693 
9694     if (!PP.getLangOpts().CPlusPlus)
9695       return false;
9696 
9697     const bool IsFirstMutable = FirstField->isMutable();
9698     const bool IsSecondMutable = SecondField->isMutable();
9699     if (IsFirstMutable != IsSecondMutable) {
9700       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9701                        FirstField->getSourceRange(), FieldSingleMutable)
9702           << FirstII << IsFirstMutable;
9703       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9704                       SecondField->getSourceRange(), FieldSingleMutable)
9705           << SecondII << IsSecondMutable;
9706       return true;
9707     }
9708 
9709     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9710     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9711     if ((!FirstInitializer && SecondInitializer) ||
9712         (FirstInitializer && !SecondInitializer)) {
9713       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9714                        FirstField->getSourceRange(), FieldSingleInitializer)
9715           << FirstII << (FirstInitializer != nullptr);
9716       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9717                       SecondField->getSourceRange(), FieldSingleInitializer)
9718           << SecondII << (SecondInitializer != nullptr);
9719       return true;
9720     }
9721 
9722     if (FirstInitializer && SecondInitializer) {
9723       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9724       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9725       if (FirstInitHash != SecondInitHash) {
9726         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9727                          FirstField->getSourceRange(),
9728                          FieldDifferentInitializers)
9729             << FirstII << FirstInitializer->getSourceRange();
9730         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9731                         SecondField->getSourceRange(),
9732                         FieldDifferentInitializers)
9733             << SecondII << SecondInitializer->getSourceRange();
9734         return true;
9735       }
9736     }
9737 
9738     return false;
9739   };
9740 
9741   auto ODRDiagTypeDefOrAlias =
9742       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9743           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9744           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9745           bool IsTypeAlias) {
9746         auto FirstName = FirstTD->getDeclName();
9747         auto SecondName = SecondTD->getDeclName();
9748         if (FirstName != SecondName) {
9749           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9750                            FirstTD->getSourceRange(), TypedefName)
9751               << IsTypeAlias << FirstName;
9752           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9753                           SecondTD->getSourceRange(), TypedefName)
9754               << IsTypeAlias << SecondName;
9755           return true;
9756         }
9757 
9758         QualType FirstType = FirstTD->getUnderlyingType();
9759         QualType SecondType = SecondTD->getUnderlyingType();
9760         if (ComputeQualTypeODRHash(FirstType) !=
9761             ComputeQualTypeODRHash(SecondType)) {
9762           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9763                            FirstTD->getSourceRange(), TypedefType)
9764               << IsTypeAlias << FirstName << FirstType;
9765           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9766                           SecondTD->getSourceRange(), TypedefType)
9767               << IsTypeAlias << SecondName << SecondType;
9768           return true;
9769         }
9770 
9771         return false;
9772   };
9773 
9774   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9775                      &ComputeQualTypeODRHash, &ComputeODRHash,
9776                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9777                            StringRef SecondModule, VarDecl *FirstVD,
9778                            VarDecl *SecondVD) {
9779     auto FirstName = FirstVD->getDeclName();
9780     auto SecondName = SecondVD->getDeclName();
9781     if (FirstName != SecondName) {
9782       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9783                        FirstVD->getSourceRange(), VarName)
9784           << FirstName;
9785       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9786                       SecondVD->getSourceRange(), VarName)
9787           << SecondName;
9788       return true;
9789     }
9790 
9791     QualType FirstType = FirstVD->getType();
9792     QualType SecondType = SecondVD->getType();
9793     if (ComputeQualTypeODRHash(FirstType) !=
9794         ComputeQualTypeODRHash(SecondType)) {
9795       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9796                        FirstVD->getSourceRange(), VarType)
9797           << FirstName << FirstType;
9798       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9799                       SecondVD->getSourceRange(), VarType)
9800           << SecondName << SecondType;
9801       return true;
9802     }
9803 
9804     if (!PP.getLangOpts().CPlusPlus)
9805       return false;
9806 
9807     const Expr *FirstInit = FirstVD->getInit();
9808     const Expr *SecondInit = SecondVD->getInit();
9809     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9810       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9811                        FirstVD->getSourceRange(), VarSingleInitializer)
9812           << FirstName << (FirstInit == nullptr)
9813           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9814       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9815                       SecondVD->getSourceRange(), VarSingleInitializer)
9816           << SecondName << (SecondInit == nullptr)
9817           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9818       return true;
9819     }
9820 
9821     if (FirstInit && SecondInit &&
9822         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9823       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9824                        FirstVD->getSourceRange(), VarDifferentInitializer)
9825           << FirstName << FirstInit->getSourceRange();
9826       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9827                       SecondVD->getSourceRange(), VarDifferentInitializer)
9828           << SecondName << SecondInit->getSourceRange();
9829       return true;
9830     }
9831 
9832     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9833     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9834     if (FirstIsConstexpr != SecondIsConstexpr) {
9835       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9836                        FirstVD->getSourceRange(), VarConstexpr)
9837           << FirstName << FirstIsConstexpr;
9838       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9839                       SecondVD->getSourceRange(), VarConstexpr)
9840           << SecondName << SecondIsConstexpr;
9841       return true;
9842     }
9843     return false;
9844   };
9845 
9846   auto DifferenceSelector = [](Decl *D) {
9847     assert(D && "valid Decl required");
9848     switch (D->getKind()) {
9849     default:
9850       return Other;
9851     case Decl::AccessSpec:
9852       switch (D->getAccess()) {
9853       case AS_public:
9854         return PublicSpecifer;
9855       case AS_private:
9856         return PrivateSpecifer;
9857       case AS_protected:
9858         return ProtectedSpecifer;
9859       case AS_none:
9860         break;
9861       }
9862       llvm_unreachable("Invalid access specifier");
9863     case Decl::StaticAssert:
9864       return StaticAssert;
9865     case Decl::Field:
9866       return Field;
9867     case Decl::CXXMethod:
9868     case Decl::CXXConstructor:
9869     case Decl::CXXDestructor:
9870       return CXXMethod;
9871     case Decl::TypeAlias:
9872       return TypeAlias;
9873     case Decl::Typedef:
9874       return TypeDef;
9875     case Decl::Var:
9876       return Var;
9877     case Decl::Friend:
9878       return Friend;
9879     case Decl::FunctionTemplate:
9880       return FunctionTemplate;
9881     }
9882   };
9883 
9884   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9885   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9886                                                  RecordDecl *Record,
9887                                                  const DeclContext *DC) {
9888     for (auto *D : Record->decls()) {
9889       if (!ODRHash::isDeclToBeProcessed(D, DC))
9890         continue;
9891       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9892     }
9893   };
9894 
9895   struct DiffResult {
9896     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9897     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9898   };
9899 
9900   // If there is a diagnoseable difference, FirstDiffType and
9901   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9902   // filled in if not EndOfClass.
9903   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9904                                              DeclHashes &SecondHashes) {
9905     DiffResult DR;
9906     auto FirstIt = FirstHashes.begin();
9907     auto SecondIt = SecondHashes.begin();
9908     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9909       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9910           FirstIt->second == SecondIt->second) {
9911         ++FirstIt;
9912         ++SecondIt;
9913         continue;
9914       }
9915 
9916       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9917       DR.SecondDecl =
9918           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9919 
9920       DR.FirstDiffType =
9921           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9922       DR.SecondDiffType =
9923           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9924       return DR;
9925     }
9926     return DR;
9927   };
9928 
9929   // Use this to diagnose that an unexpected Decl was encountered
9930   // or no difference was detected. This causes a generic error
9931   // message to be emitted.
9932   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9933                                       StringRef FirstModule,
9934                                       NamedDecl *SecondRecord,
9935                                       StringRef SecondModule) {
9936     Diag(FirstRecord->getLocation(),
9937          diag::err_module_odr_violation_different_definitions)
9938         << FirstRecord << FirstModule.empty() << FirstModule;
9939 
9940     if (DR.FirstDecl) {
9941       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9942           << FirstRecord << DR.FirstDecl->getSourceRange();
9943     }
9944 
9945     Diag(SecondRecord->getLocation(),
9946          diag::note_module_odr_violation_different_definitions)
9947         << SecondModule;
9948 
9949     if (DR.SecondDecl) {
9950       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9951           << DR.SecondDecl->getSourceRange();
9952     }
9953   };
9954 
9955   auto DiagnoseODRMismatch =
9956       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9957              NamedDecl *SecondRecord, StringRef SecondModule) {
9958         SourceLocation FirstLoc;
9959         SourceRange FirstRange;
9960         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9961         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9962           FirstLoc = FirstTag->getBraceRange().getEnd();
9963         } else {
9964           FirstLoc = DR.FirstDecl->getLocation();
9965           FirstRange = DR.FirstDecl->getSourceRange();
9966         }
9967         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9968             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9969             << DR.FirstDiffType;
9970 
9971         SourceLocation SecondLoc;
9972         SourceRange SecondRange;
9973         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9974         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9975           SecondLoc = SecondTag->getBraceRange().getEnd();
9976         } else {
9977           SecondLoc = DR.SecondDecl->getLocation();
9978           SecondRange = DR.SecondDecl->getSourceRange();
9979         }
9980         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9981             << SecondModule << SecondRange << DR.SecondDiffType;
9982       };
9983 
9984   // Issue any pending ODR-failure diagnostics.
9985   for (auto &Merge : OdrMergeFailures) {
9986     // If we've already pointed out a specific problem with this class, don't
9987     // bother issuing a general "something's different" diagnostic.
9988     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9989       continue;
9990 
9991     bool Diagnosed = false;
9992     CXXRecordDecl *FirstRecord = Merge.first;
9993     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9994     for (auto &RecordPair : Merge.second) {
9995       CXXRecordDecl *SecondRecord = RecordPair.first;
9996       // Multiple different declarations got merged together; tell the user
9997       // where they came from.
9998       if (FirstRecord == SecondRecord)
9999         continue;
10000 
10001       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10002 
10003       auto *FirstDD = FirstRecord->DefinitionData;
10004       auto *SecondDD = RecordPair.second;
10005 
10006       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10007 
10008       // Diagnostics from DefinitionData are emitted here.
10009       if (FirstDD != SecondDD) {
10010         enum ODRDefinitionDataDifference {
10011           NumBases,
10012           NumVBases,
10013           BaseType,
10014           BaseVirtual,
10015           BaseAccess,
10016         };
10017         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10018                                  this](SourceLocation Loc, SourceRange Range,
10019                                        ODRDefinitionDataDifference DiffType) {
10020           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10021                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10022                  << DiffType;
10023         };
10024         auto ODRDiagBaseNote = [&SecondModule,
10025                                 this](SourceLocation Loc, SourceRange Range,
10026                                       ODRDefinitionDataDifference DiffType) {
10027           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10028                  << SecondModule << Range << DiffType;
10029         };
10030 
10031         unsigned FirstNumBases = FirstDD->NumBases;
10032         unsigned FirstNumVBases = FirstDD->NumVBases;
10033         unsigned SecondNumBases = SecondDD->NumBases;
10034         unsigned SecondNumVBases = SecondDD->NumVBases;
10035 
10036         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10037           unsigned NumBases = DD->NumBases;
10038           if (NumBases == 0) return SourceRange();
10039           auto bases = DD->bases();
10040           return SourceRange(bases[0].getBeginLoc(),
10041                              bases[NumBases - 1].getEndLoc());
10042         };
10043 
10044         if (FirstNumBases != SecondNumBases) {
10045           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10046                            NumBases)
10047               << FirstNumBases;
10048           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10049                           NumBases)
10050               << SecondNumBases;
10051           Diagnosed = true;
10052           break;
10053         }
10054 
10055         if (FirstNumVBases != SecondNumVBases) {
10056           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10057                            NumVBases)
10058               << FirstNumVBases;
10059           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10060                           NumVBases)
10061               << SecondNumVBases;
10062           Diagnosed = true;
10063           break;
10064         }
10065 
10066         auto FirstBases = FirstDD->bases();
10067         auto SecondBases = SecondDD->bases();
10068         unsigned i = 0;
10069         for (i = 0; i < FirstNumBases; ++i) {
10070           auto FirstBase = FirstBases[i];
10071           auto SecondBase = SecondBases[i];
10072           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10073               ComputeQualTypeODRHash(SecondBase.getType())) {
10074             ODRDiagBaseError(FirstRecord->getLocation(),
10075                              FirstBase.getSourceRange(), BaseType)
10076                 << (i + 1) << FirstBase.getType();
10077             ODRDiagBaseNote(SecondRecord->getLocation(),
10078                             SecondBase.getSourceRange(), BaseType)
10079                 << (i + 1) << SecondBase.getType();
10080             break;
10081           }
10082 
10083           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10084             ODRDiagBaseError(FirstRecord->getLocation(),
10085                              FirstBase.getSourceRange(), BaseVirtual)
10086                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10087             ODRDiagBaseNote(SecondRecord->getLocation(),
10088                             SecondBase.getSourceRange(), BaseVirtual)
10089                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10090             break;
10091           }
10092 
10093           if (FirstBase.getAccessSpecifierAsWritten() !=
10094               SecondBase.getAccessSpecifierAsWritten()) {
10095             ODRDiagBaseError(FirstRecord->getLocation(),
10096                              FirstBase.getSourceRange(), BaseAccess)
10097                 << (i + 1) << FirstBase.getType()
10098                 << (int)FirstBase.getAccessSpecifierAsWritten();
10099             ODRDiagBaseNote(SecondRecord->getLocation(),
10100                             SecondBase.getSourceRange(), BaseAccess)
10101                 << (i + 1) << SecondBase.getType()
10102                 << (int)SecondBase.getAccessSpecifierAsWritten();
10103             break;
10104           }
10105         }
10106 
10107         if (i != FirstNumBases) {
10108           Diagnosed = true;
10109           break;
10110         }
10111       }
10112 
10113       const ClassTemplateDecl *FirstTemplate =
10114           FirstRecord->getDescribedClassTemplate();
10115       const ClassTemplateDecl *SecondTemplate =
10116           SecondRecord->getDescribedClassTemplate();
10117 
10118       assert(!FirstTemplate == !SecondTemplate &&
10119              "Both pointers should be null or non-null");
10120 
10121       enum ODRTemplateDifference {
10122         ParamEmptyName,
10123         ParamName,
10124         ParamSingleDefaultArgument,
10125         ParamDifferentDefaultArgument,
10126       };
10127 
10128       if (FirstTemplate && SecondTemplate) {
10129         DeclHashes FirstTemplateHashes;
10130         DeclHashes SecondTemplateHashes;
10131 
10132         auto PopulateTemplateParameterHashs =
10133             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10134                                      const ClassTemplateDecl *TD) {
10135               for (auto *D : TD->getTemplateParameters()->asArray()) {
10136                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10137               }
10138             };
10139 
10140         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10141         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10142 
10143         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10144                "Number of template parameters should be equal.");
10145 
10146         auto FirstIt = FirstTemplateHashes.begin();
10147         auto FirstEnd = FirstTemplateHashes.end();
10148         auto SecondIt = SecondTemplateHashes.begin();
10149         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10150           if (FirstIt->second == SecondIt->second)
10151             continue;
10152 
10153           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10154                                           SourceLocation Loc, SourceRange Range,
10155                                           ODRTemplateDifference DiffType) {
10156             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10157                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10158                    << DiffType;
10159           };
10160           auto ODRDiagTemplateNote = [&SecondModule, this](
10161                                          SourceLocation Loc, SourceRange Range,
10162                                          ODRTemplateDifference DiffType) {
10163             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10164                    << SecondModule << Range << DiffType;
10165           };
10166 
10167           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10168           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10169 
10170           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10171                  "Parameter Decl's should be the same kind.");
10172 
10173           DeclarationName FirstName = FirstDecl->getDeclName();
10174           DeclarationName SecondName = SecondDecl->getDeclName();
10175 
10176           if (FirstName != SecondName) {
10177             const bool FirstNameEmpty =
10178                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10179             const bool SecondNameEmpty =
10180                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10181             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10182                    "Both template parameters cannot be unnamed.");
10183             ODRDiagTemplateError(FirstDecl->getLocation(),
10184                                  FirstDecl->getSourceRange(),
10185                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10186                 << FirstName;
10187             ODRDiagTemplateNote(SecondDecl->getLocation(),
10188                                 SecondDecl->getSourceRange(),
10189                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10190                 << SecondName;
10191             break;
10192           }
10193 
10194           switch (FirstDecl->getKind()) {
10195           default:
10196             llvm_unreachable("Invalid template parameter type.");
10197           case Decl::TemplateTypeParm: {
10198             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10199             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10200             const bool HasFirstDefaultArgument =
10201                 FirstParam->hasDefaultArgument() &&
10202                 !FirstParam->defaultArgumentWasInherited();
10203             const bool HasSecondDefaultArgument =
10204                 SecondParam->hasDefaultArgument() &&
10205                 !SecondParam->defaultArgumentWasInherited();
10206 
10207             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10208               ODRDiagTemplateError(FirstDecl->getLocation(),
10209                                    FirstDecl->getSourceRange(),
10210                                    ParamSingleDefaultArgument)
10211                   << HasFirstDefaultArgument;
10212               ODRDiagTemplateNote(SecondDecl->getLocation(),
10213                                   SecondDecl->getSourceRange(),
10214                                   ParamSingleDefaultArgument)
10215                   << HasSecondDefaultArgument;
10216               break;
10217             }
10218 
10219             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10220                    "Expecting default arguments.");
10221 
10222             ODRDiagTemplateError(FirstDecl->getLocation(),
10223                                  FirstDecl->getSourceRange(),
10224                                  ParamDifferentDefaultArgument);
10225             ODRDiagTemplateNote(SecondDecl->getLocation(),
10226                                 SecondDecl->getSourceRange(),
10227                                 ParamDifferentDefaultArgument);
10228 
10229             break;
10230           }
10231           case Decl::NonTypeTemplateParm: {
10232             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10233             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10234             const bool HasFirstDefaultArgument =
10235                 FirstParam->hasDefaultArgument() &&
10236                 !FirstParam->defaultArgumentWasInherited();
10237             const bool HasSecondDefaultArgument =
10238                 SecondParam->hasDefaultArgument() &&
10239                 !SecondParam->defaultArgumentWasInherited();
10240 
10241             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10242               ODRDiagTemplateError(FirstDecl->getLocation(),
10243                                    FirstDecl->getSourceRange(),
10244                                    ParamSingleDefaultArgument)
10245                   << HasFirstDefaultArgument;
10246               ODRDiagTemplateNote(SecondDecl->getLocation(),
10247                                   SecondDecl->getSourceRange(),
10248                                   ParamSingleDefaultArgument)
10249                   << HasSecondDefaultArgument;
10250               break;
10251             }
10252 
10253             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10254                    "Expecting default arguments.");
10255 
10256             ODRDiagTemplateError(FirstDecl->getLocation(),
10257                                  FirstDecl->getSourceRange(),
10258                                  ParamDifferentDefaultArgument);
10259             ODRDiagTemplateNote(SecondDecl->getLocation(),
10260                                 SecondDecl->getSourceRange(),
10261                                 ParamDifferentDefaultArgument);
10262 
10263             break;
10264           }
10265           case Decl::TemplateTemplateParm: {
10266             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10267             const auto *SecondParam =
10268                 cast<TemplateTemplateParmDecl>(SecondDecl);
10269             const bool HasFirstDefaultArgument =
10270                 FirstParam->hasDefaultArgument() &&
10271                 !FirstParam->defaultArgumentWasInherited();
10272             const bool HasSecondDefaultArgument =
10273                 SecondParam->hasDefaultArgument() &&
10274                 !SecondParam->defaultArgumentWasInherited();
10275 
10276             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10277               ODRDiagTemplateError(FirstDecl->getLocation(),
10278                                    FirstDecl->getSourceRange(),
10279                                    ParamSingleDefaultArgument)
10280                   << HasFirstDefaultArgument;
10281               ODRDiagTemplateNote(SecondDecl->getLocation(),
10282                                   SecondDecl->getSourceRange(),
10283                                   ParamSingleDefaultArgument)
10284                   << HasSecondDefaultArgument;
10285               break;
10286             }
10287 
10288             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10289                    "Expecting default arguments.");
10290 
10291             ODRDiagTemplateError(FirstDecl->getLocation(),
10292                                  FirstDecl->getSourceRange(),
10293                                  ParamDifferentDefaultArgument);
10294             ODRDiagTemplateNote(SecondDecl->getLocation(),
10295                                 SecondDecl->getSourceRange(),
10296                                 ParamDifferentDefaultArgument);
10297 
10298             break;
10299           }
10300           }
10301 
10302           break;
10303         }
10304 
10305         if (FirstIt != FirstEnd) {
10306           Diagnosed = true;
10307           break;
10308         }
10309       }
10310 
10311       DeclHashes FirstHashes;
10312       DeclHashes SecondHashes;
10313       const DeclContext *DC = FirstRecord;
10314       PopulateHashes(FirstHashes, FirstRecord, DC);
10315       PopulateHashes(SecondHashes, SecondRecord, DC);
10316 
10317       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10318       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10319       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10320       Decl *FirstDecl = DR.FirstDecl;
10321       Decl *SecondDecl = DR.SecondDecl;
10322 
10323       if (FirstDiffType == Other || SecondDiffType == Other) {
10324         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10325                               SecondModule);
10326         Diagnosed = true;
10327         break;
10328       }
10329 
10330       if (FirstDiffType != SecondDiffType) {
10331         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10332                             SecondModule);
10333         Diagnosed = true;
10334         break;
10335       }
10336 
10337       assert(FirstDiffType == SecondDiffType);
10338 
10339       switch (FirstDiffType) {
10340       case Other:
10341       case EndOfClass:
10342       case PublicSpecifer:
10343       case PrivateSpecifer:
10344       case ProtectedSpecifer:
10345         llvm_unreachable("Invalid diff type");
10346 
10347       case StaticAssert: {
10348         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10349         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10350 
10351         Expr *FirstExpr = FirstSA->getAssertExpr();
10352         Expr *SecondExpr = SecondSA->getAssertExpr();
10353         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10354         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10355         if (FirstODRHash != SecondODRHash) {
10356           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10357                            FirstExpr->getSourceRange(), StaticAssertCondition);
10358           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10359                           SecondExpr->getSourceRange(), StaticAssertCondition);
10360           Diagnosed = true;
10361           break;
10362         }
10363 
10364         StringLiteral *FirstStr = FirstSA->getMessage();
10365         StringLiteral *SecondStr = SecondSA->getMessage();
10366         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10367         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10368           SourceLocation FirstLoc, SecondLoc;
10369           SourceRange FirstRange, SecondRange;
10370           if (FirstStr) {
10371             FirstLoc = FirstStr->getBeginLoc();
10372             FirstRange = FirstStr->getSourceRange();
10373           } else {
10374             FirstLoc = FirstSA->getBeginLoc();
10375             FirstRange = FirstSA->getSourceRange();
10376           }
10377           if (SecondStr) {
10378             SecondLoc = SecondStr->getBeginLoc();
10379             SecondRange = SecondStr->getSourceRange();
10380           } else {
10381             SecondLoc = SecondSA->getBeginLoc();
10382             SecondRange = SecondSA->getSourceRange();
10383           }
10384           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10385                            StaticAssertOnlyMessage)
10386               << (FirstStr == nullptr);
10387           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10388                           StaticAssertOnlyMessage)
10389               << (SecondStr == nullptr);
10390           Diagnosed = true;
10391           break;
10392         }
10393 
10394         if (FirstStr && SecondStr &&
10395             FirstStr->getString() != SecondStr->getString()) {
10396           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10397                            FirstStr->getSourceRange(), StaticAssertMessage);
10398           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10399                           SecondStr->getSourceRange(), StaticAssertMessage);
10400           Diagnosed = true;
10401           break;
10402         }
10403         break;
10404       }
10405       case Field: {
10406         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10407                                  cast<FieldDecl>(FirstDecl),
10408                                  cast<FieldDecl>(SecondDecl));
10409         break;
10410       }
10411       case CXXMethod: {
10412         enum {
10413           DiagMethod,
10414           DiagConstructor,
10415           DiagDestructor,
10416         } FirstMethodType,
10417             SecondMethodType;
10418         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10419           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10420           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10421           return DiagMethod;
10422         };
10423         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10424         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10425         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10426         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10427         auto FirstName = FirstMethod->getDeclName();
10428         auto SecondName = SecondMethod->getDeclName();
10429         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10430           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10431                            FirstMethod->getSourceRange(), MethodName)
10432               << FirstMethodType << FirstName;
10433           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10434                           SecondMethod->getSourceRange(), MethodName)
10435               << SecondMethodType << SecondName;
10436 
10437           Diagnosed = true;
10438           break;
10439         }
10440 
10441         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10442         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10443         if (FirstDeleted != SecondDeleted) {
10444           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10445                            FirstMethod->getSourceRange(), MethodDeleted)
10446               << FirstMethodType << FirstName << FirstDeleted;
10447 
10448           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10449                           SecondMethod->getSourceRange(), MethodDeleted)
10450               << SecondMethodType << SecondName << SecondDeleted;
10451           Diagnosed = true;
10452           break;
10453         }
10454 
10455         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10456         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10457         if (FirstDefaulted != SecondDefaulted) {
10458           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10459                            FirstMethod->getSourceRange(), MethodDefaulted)
10460               << FirstMethodType << FirstName << FirstDefaulted;
10461 
10462           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10463                           SecondMethod->getSourceRange(), MethodDefaulted)
10464               << SecondMethodType << SecondName << SecondDefaulted;
10465           Diagnosed = true;
10466           break;
10467         }
10468 
10469         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10470         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10471         const bool FirstPure = FirstMethod->isPure();
10472         const bool SecondPure = SecondMethod->isPure();
10473         if ((FirstVirtual || SecondVirtual) &&
10474             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10475           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10476                            FirstMethod->getSourceRange(), MethodVirtual)
10477               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10478           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10479                           SecondMethod->getSourceRange(), MethodVirtual)
10480               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10481           Diagnosed = true;
10482           break;
10483         }
10484 
10485         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10486         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10487         // class needs to be checked instead.
10488         const auto FirstStorage = FirstMethod->getStorageClass();
10489         const auto SecondStorage = SecondMethod->getStorageClass();
10490         const bool FirstStatic = FirstStorage == SC_Static;
10491         const bool SecondStatic = SecondStorage == SC_Static;
10492         if (FirstStatic != SecondStatic) {
10493           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10494                            FirstMethod->getSourceRange(), MethodStatic)
10495               << FirstMethodType << FirstName << FirstStatic;
10496           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10497                           SecondMethod->getSourceRange(), MethodStatic)
10498               << SecondMethodType << SecondName << SecondStatic;
10499           Diagnosed = true;
10500           break;
10501         }
10502 
10503         const bool FirstVolatile = FirstMethod->isVolatile();
10504         const bool SecondVolatile = SecondMethod->isVolatile();
10505         if (FirstVolatile != SecondVolatile) {
10506           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10507                            FirstMethod->getSourceRange(), MethodVolatile)
10508               << FirstMethodType << FirstName << FirstVolatile;
10509           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10510                           SecondMethod->getSourceRange(), MethodVolatile)
10511               << SecondMethodType << SecondName << SecondVolatile;
10512           Diagnosed = true;
10513           break;
10514         }
10515 
10516         const bool FirstConst = FirstMethod->isConst();
10517         const bool SecondConst = SecondMethod->isConst();
10518         if (FirstConst != SecondConst) {
10519           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10520                            FirstMethod->getSourceRange(), MethodConst)
10521               << FirstMethodType << FirstName << FirstConst;
10522           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10523                           SecondMethod->getSourceRange(), MethodConst)
10524               << SecondMethodType << SecondName << SecondConst;
10525           Diagnosed = true;
10526           break;
10527         }
10528 
10529         const bool FirstInline = FirstMethod->isInlineSpecified();
10530         const bool SecondInline = SecondMethod->isInlineSpecified();
10531         if (FirstInline != SecondInline) {
10532           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10533                            FirstMethod->getSourceRange(), MethodInline)
10534               << FirstMethodType << FirstName << FirstInline;
10535           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10536                           SecondMethod->getSourceRange(), MethodInline)
10537               << SecondMethodType << SecondName << SecondInline;
10538           Diagnosed = true;
10539           break;
10540         }
10541 
10542         const unsigned FirstNumParameters = FirstMethod->param_size();
10543         const unsigned SecondNumParameters = SecondMethod->param_size();
10544         if (FirstNumParameters != SecondNumParameters) {
10545           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10546                            FirstMethod->getSourceRange(),
10547                            MethodNumberParameters)
10548               << FirstMethodType << FirstName << FirstNumParameters;
10549           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10550                           SecondMethod->getSourceRange(),
10551                           MethodNumberParameters)
10552               << SecondMethodType << SecondName << SecondNumParameters;
10553           Diagnosed = true;
10554           break;
10555         }
10556 
10557         // Need this status boolean to know when break out of the switch.
10558         bool ParameterMismatch = false;
10559         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10560           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10561           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10562 
10563           QualType FirstParamType = FirstParam->getType();
10564           QualType SecondParamType = SecondParam->getType();
10565           if (FirstParamType != SecondParamType &&
10566               ComputeQualTypeODRHash(FirstParamType) !=
10567                   ComputeQualTypeODRHash(SecondParamType)) {
10568             if (const DecayedType *ParamDecayedType =
10569                     FirstParamType->getAs<DecayedType>()) {
10570               ODRDiagDeclError(
10571                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10572                   FirstMethod->getSourceRange(), MethodParameterType)
10573                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10574                   << true << ParamDecayedType->getOriginalType();
10575             } else {
10576               ODRDiagDeclError(
10577                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10578                   FirstMethod->getSourceRange(), MethodParameterType)
10579                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10580                   << false;
10581             }
10582 
10583             if (const DecayedType *ParamDecayedType =
10584                     SecondParamType->getAs<DecayedType>()) {
10585               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10586                               SecondMethod->getSourceRange(),
10587                               MethodParameterType)
10588                   << SecondMethodType << SecondName << (I + 1)
10589                   << SecondParamType << true
10590                   << ParamDecayedType->getOriginalType();
10591             } else {
10592               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10593                               SecondMethod->getSourceRange(),
10594                               MethodParameterType)
10595                   << SecondMethodType << SecondName << (I + 1)
10596                   << SecondParamType << false;
10597             }
10598             ParameterMismatch = true;
10599             break;
10600           }
10601 
10602           DeclarationName FirstParamName = FirstParam->getDeclName();
10603           DeclarationName SecondParamName = SecondParam->getDeclName();
10604           if (FirstParamName != SecondParamName) {
10605             ODRDiagDeclError(FirstRecord, FirstModule,
10606                              FirstMethod->getLocation(),
10607                              FirstMethod->getSourceRange(), MethodParameterName)
10608                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10609             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10610                             SecondMethod->getSourceRange(), MethodParameterName)
10611                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10612             ParameterMismatch = true;
10613             break;
10614           }
10615 
10616           const Expr *FirstInit = FirstParam->getInit();
10617           const Expr *SecondInit = SecondParam->getInit();
10618           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10619             ODRDiagDeclError(FirstRecord, FirstModule,
10620                              FirstMethod->getLocation(),
10621                              FirstMethod->getSourceRange(),
10622                              MethodParameterSingleDefaultArgument)
10623                 << FirstMethodType << FirstName << (I + 1)
10624                 << (FirstInit == nullptr)
10625                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10626             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10627                             SecondMethod->getSourceRange(),
10628                             MethodParameterSingleDefaultArgument)
10629                 << SecondMethodType << SecondName << (I + 1)
10630                 << (SecondInit == nullptr)
10631                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10632             ParameterMismatch = true;
10633             break;
10634           }
10635 
10636           if (FirstInit && SecondInit &&
10637               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10638             ODRDiagDeclError(FirstRecord, FirstModule,
10639                              FirstMethod->getLocation(),
10640                              FirstMethod->getSourceRange(),
10641                              MethodParameterDifferentDefaultArgument)
10642                 << FirstMethodType << FirstName << (I + 1)
10643                 << FirstInit->getSourceRange();
10644             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10645                             SecondMethod->getSourceRange(),
10646                             MethodParameterDifferentDefaultArgument)
10647                 << SecondMethodType << SecondName << (I + 1)
10648                 << SecondInit->getSourceRange();
10649             ParameterMismatch = true;
10650             break;
10651 
10652           }
10653         }
10654 
10655         if (ParameterMismatch) {
10656           Diagnosed = true;
10657           break;
10658         }
10659 
10660         const auto *FirstTemplateArgs =
10661             FirstMethod->getTemplateSpecializationArgs();
10662         const auto *SecondTemplateArgs =
10663             SecondMethod->getTemplateSpecializationArgs();
10664 
10665         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10666             (!FirstTemplateArgs && SecondTemplateArgs)) {
10667           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10668                            FirstMethod->getSourceRange(),
10669                            MethodNoTemplateArguments)
10670               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10671           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10672                           SecondMethod->getSourceRange(),
10673                           MethodNoTemplateArguments)
10674               << SecondMethodType << SecondName
10675               << (SecondTemplateArgs != nullptr);
10676 
10677           Diagnosed = true;
10678           break;
10679         }
10680 
10681         if (FirstTemplateArgs && SecondTemplateArgs) {
10682           // Remove pack expansions from argument list.
10683           auto ExpandTemplateArgumentList =
10684               [](const TemplateArgumentList *TAL) {
10685                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10686                 for (const TemplateArgument &TA : TAL->asArray()) {
10687                   if (TA.getKind() != TemplateArgument::Pack) {
10688                     ExpandedList.push_back(&TA);
10689                     continue;
10690                   }
10691                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10692                     ExpandedList.push_back(&PackTA);
10693                   }
10694                 }
10695                 return ExpandedList;
10696               };
10697           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10698               ExpandTemplateArgumentList(FirstTemplateArgs);
10699           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10700               ExpandTemplateArgumentList(SecondTemplateArgs);
10701 
10702           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10703             ODRDiagDeclError(FirstRecord, FirstModule,
10704                              FirstMethod->getLocation(),
10705                              FirstMethod->getSourceRange(),
10706                              MethodDifferentNumberTemplateArguments)
10707                 << FirstMethodType << FirstName
10708                 << (unsigned)FirstExpandedList.size();
10709             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10710                             SecondMethod->getSourceRange(),
10711                             MethodDifferentNumberTemplateArguments)
10712                 << SecondMethodType << SecondName
10713                 << (unsigned)SecondExpandedList.size();
10714 
10715             Diagnosed = true;
10716             break;
10717           }
10718 
10719           bool TemplateArgumentMismatch = false;
10720           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10721             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10722                                    &SecondTA = *SecondExpandedList[i];
10723             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10724                 ComputeTemplateArgumentODRHash(SecondTA)) {
10725               continue;
10726             }
10727 
10728             ODRDiagDeclError(
10729                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10730                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10731                 << FirstMethodType << FirstName << FirstTA << i + 1;
10732             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10733                             SecondMethod->getSourceRange(),
10734                             MethodDifferentTemplateArgument)
10735                 << SecondMethodType << SecondName << SecondTA << i + 1;
10736 
10737             TemplateArgumentMismatch = true;
10738             break;
10739           }
10740 
10741           if (TemplateArgumentMismatch) {
10742             Diagnosed = true;
10743             break;
10744           }
10745         }
10746 
10747         // Compute the hash of the method as if it has no body.
10748         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10749           Hash.clear();
10750           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10751           return Hash.CalculateHash();
10752         };
10753 
10754         // Compare the hash generated to the hash stored.  A difference means
10755         // that a body was present in the original source.  Due to merging,
10756         // the stardard way of detecting a body will not work.
10757         const bool HasFirstBody =
10758             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10759         const bool HasSecondBody =
10760             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10761 
10762         if (HasFirstBody != HasSecondBody) {
10763           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10764                            FirstMethod->getSourceRange(), MethodSingleBody)
10765               << FirstMethodType << FirstName << HasFirstBody;
10766           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10767                           SecondMethod->getSourceRange(), MethodSingleBody)
10768               << SecondMethodType << SecondName << HasSecondBody;
10769           Diagnosed = true;
10770           break;
10771         }
10772 
10773         if (HasFirstBody && HasSecondBody) {
10774           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10775                            FirstMethod->getSourceRange(), MethodDifferentBody)
10776               << FirstMethodType << FirstName;
10777           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10778                           SecondMethod->getSourceRange(), MethodDifferentBody)
10779               << SecondMethodType << SecondName;
10780           Diagnosed = true;
10781           break;
10782         }
10783 
10784         break;
10785       }
10786       case TypeAlias:
10787       case TypeDef: {
10788         Diagnosed = ODRDiagTypeDefOrAlias(
10789             FirstRecord, FirstModule, SecondModule,
10790             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10791             FirstDiffType == TypeAlias);
10792         break;
10793       }
10794       case Var: {
10795         Diagnosed =
10796             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10797                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10798         break;
10799       }
10800       case Friend: {
10801         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10802         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10803 
10804         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10805         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10806 
10807         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10808         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10809 
10810         if (FirstND && SecondND) {
10811           ODRDiagDeclError(FirstRecord, FirstModule,
10812                            FirstFriend->getFriendLoc(),
10813                            FirstFriend->getSourceRange(), FriendFunction)
10814               << FirstND;
10815           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10816                           SecondFriend->getSourceRange(), FriendFunction)
10817               << SecondND;
10818 
10819           Diagnosed = true;
10820           break;
10821         }
10822 
10823         if (FirstTSI && SecondTSI) {
10824           QualType FirstFriendType = FirstTSI->getType();
10825           QualType SecondFriendType = SecondTSI->getType();
10826           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10827                  ComputeQualTypeODRHash(SecondFriendType));
10828           ODRDiagDeclError(FirstRecord, FirstModule,
10829                            FirstFriend->getFriendLoc(),
10830                            FirstFriend->getSourceRange(), FriendType)
10831               << FirstFriendType;
10832           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10833                           SecondFriend->getSourceRange(), FriendType)
10834               << SecondFriendType;
10835           Diagnosed = true;
10836           break;
10837         }
10838 
10839         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10840                          FirstFriend->getSourceRange(), FriendTypeFunction)
10841             << (FirstTSI == nullptr);
10842         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10843                         SecondFriend->getSourceRange(), FriendTypeFunction)
10844             << (SecondTSI == nullptr);
10845 
10846         Diagnosed = true;
10847         break;
10848       }
10849       case FunctionTemplate: {
10850         FunctionTemplateDecl *FirstTemplate =
10851             cast<FunctionTemplateDecl>(FirstDecl);
10852         FunctionTemplateDecl *SecondTemplate =
10853             cast<FunctionTemplateDecl>(SecondDecl);
10854 
10855         TemplateParameterList *FirstTPL =
10856             FirstTemplate->getTemplateParameters();
10857         TemplateParameterList *SecondTPL =
10858             SecondTemplate->getTemplateParameters();
10859 
10860         if (FirstTPL->size() != SecondTPL->size()) {
10861           ODRDiagDeclError(FirstRecord, FirstModule,
10862                            FirstTemplate->getLocation(),
10863                            FirstTemplate->getSourceRange(),
10864                            FunctionTemplateDifferentNumberParameters)
10865               << FirstTemplate << FirstTPL->size();
10866           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10867                           SecondTemplate->getSourceRange(),
10868                           FunctionTemplateDifferentNumberParameters)
10869               << SecondTemplate << SecondTPL->size();
10870 
10871           Diagnosed = true;
10872           break;
10873         }
10874 
10875         bool ParameterMismatch = false;
10876         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10877           NamedDecl *FirstParam = FirstTPL->getParam(i);
10878           NamedDecl *SecondParam = SecondTPL->getParam(i);
10879 
10880           if (FirstParam->getKind() != SecondParam->getKind()) {
10881             enum {
10882               TemplateTypeParameter,
10883               NonTypeTemplateParameter,
10884               TemplateTemplateParameter,
10885             };
10886             auto GetParamType = [](NamedDecl *D) {
10887               switch (D->getKind()) {
10888                 default:
10889                   llvm_unreachable("Unexpected template parameter type");
10890                 case Decl::TemplateTypeParm:
10891                   return TemplateTypeParameter;
10892                 case Decl::NonTypeTemplateParm:
10893                   return NonTypeTemplateParameter;
10894                 case Decl::TemplateTemplateParm:
10895                   return TemplateTemplateParameter;
10896               }
10897             };
10898 
10899             ODRDiagDeclError(FirstRecord, FirstModule,
10900                              FirstTemplate->getLocation(),
10901                              FirstTemplate->getSourceRange(),
10902                              FunctionTemplateParameterDifferentKind)
10903                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10904             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10905                             SecondTemplate->getSourceRange(),
10906                             FunctionTemplateParameterDifferentKind)
10907                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10908 
10909             ParameterMismatch = true;
10910             break;
10911           }
10912 
10913           if (FirstParam->getName() != SecondParam->getName()) {
10914             ODRDiagDeclError(
10915                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10916                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10917                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10918                 << FirstParam;
10919             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10920                             SecondTemplate->getSourceRange(),
10921                             FunctionTemplateParameterName)
10922                 << SecondTemplate << (i + 1)
10923                 << (bool)SecondParam->getIdentifier() << SecondParam;
10924             ParameterMismatch = true;
10925             break;
10926           }
10927 
10928           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10929               isa<TemplateTypeParmDecl>(SecondParam)) {
10930             TemplateTypeParmDecl *FirstTTPD =
10931                 cast<TemplateTypeParmDecl>(FirstParam);
10932             TemplateTypeParmDecl *SecondTTPD =
10933                 cast<TemplateTypeParmDecl>(SecondParam);
10934             bool HasFirstDefaultArgument =
10935                 FirstTTPD->hasDefaultArgument() &&
10936                 !FirstTTPD->defaultArgumentWasInherited();
10937             bool HasSecondDefaultArgument =
10938                 SecondTTPD->hasDefaultArgument() &&
10939                 !SecondTTPD->defaultArgumentWasInherited();
10940             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10941               ODRDiagDeclError(FirstRecord, FirstModule,
10942                                FirstTemplate->getLocation(),
10943                                FirstTemplate->getSourceRange(),
10944                                FunctionTemplateParameterSingleDefaultArgument)
10945                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10946               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10947                               SecondTemplate->getSourceRange(),
10948                               FunctionTemplateParameterSingleDefaultArgument)
10949                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10950               ParameterMismatch = true;
10951               break;
10952             }
10953 
10954             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10955               QualType FirstType = FirstTTPD->getDefaultArgument();
10956               QualType SecondType = SecondTTPD->getDefaultArgument();
10957               if (ComputeQualTypeODRHash(FirstType) !=
10958                   ComputeQualTypeODRHash(SecondType)) {
10959                 ODRDiagDeclError(
10960                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10961                     FirstTemplate->getSourceRange(),
10962                     FunctionTemplateParameterDifferentDefaultArgument)
10963                     << FirstTemplate << (i + 1) << FirstType;
10964                 ODRDiagDeclNote(
10965                     SecondModule, SecondTemplate->getLocation(),
10966                     SecondTemplate->getSourceRange(),
10967                     FunctionTemplateParameterDifferentDefaultArgument)
10968                     << SecondTemplate << (i + 1) << SecondType;
10969                 ParameterMismatch = true;
10970                 break;
10971               }
10972             }
10973 
10974             if (FirstTTPD->isParameterPack() !=
10975                 SecondTTPD->isParameterPack()) {
10976               ODRDiagDeclError(FirstRecord, FirstModule,
10977                                FirstTemplate->getLocation(),
10978                                FirstTemplate->getSourceRange(),
10979                                FunctionTemplatePackParameter)
10980                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10981               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10982                               SecondTemplate->getSourceRange(),
10983                               FunctionTemplatePackParameter)
10984                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10985               ParameterMismatch = true;
10986               break;
10987             }
10988           }
10989 
10990           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10991               isa<TemplateTemplateParmDecl>(SecondParam)) {
10992             TemplateTemplateParmDecl *FirstTTPD =
10993                 cast<TemplateTemplateParmDecl>(FirstParam);
10994             TemplateTemplateParmDecl *SecondTTPD =
10995                 cast<TemplateTemplateParmDecl>(SecondParam);
10996 
10997             TemplateParameterList *FirstTPL =
10998                 FirstTTPD->getTemplateParameters();
10999             TemplateParameterList *SecondTPL =
11000                 SecondTTPD->getTemplateParameters();
11001 
11002             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11003                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11004               ODRDiagDeclError(FirstRecord, FirstModule,
11005                                FirstTemplate->getLocation(),
11006                                FirstTemplate->getSourceRange(),
11007                                FunctionTemplateParameterDifferentType)
11008                   << FirstTemplate << (i + 1);
11009               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11010                               SecondTemplate->getSourceRange(),
11011                               FunctionTemplateParameterDifferentType)
11012                   << SecondTemplate << (i + 1);
11013               ParameterMismatch = true;
11014               break;
11015             }
11016 
11017             bool HasFirstDefaultArgument =
11018                 FirstTTPD->hasDefaultArgument() &&
11019                 !FirstTTPD->defaultArgumentWasInherited();
11020             bool HasSecondDefaultArgument =
11021                 SecondTTPD->hasDefaultArgument() &&
11022                 !SecondTTPD->defaultArgumentWasInherited();
11023             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11024               ODRDiagDeclError(FirstRecord, FirstModule,
11025                                FirstTemplate->getLocation(),
11026                                FirstTemplate->getSourceRange(),
11027                                FunctionTemplateParameterSingleDefaultArgument)
11028                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11029               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11030                               SecondTemplate->getSourceRange(),
11031                               FunctionTemplateParameterSingleDefaultArgument)
11032                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11033               ParameterMismatch = true;
11034               break;
11035             }
11036 
11037             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11038               TemplateArgument FirstTA =
11039                   FirstTTPD->getDefaultArgument().getArgument();
11040               TemplateArgument SecondTA =
11041                   SecondTTPD->getDefaultArgument().getArgument();
11042               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11043                   ComputeTemplateArgumentODRHash(SecondTA)) {
11044                 ODRDiagDeclError(
11045                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11046                     FirstTemplate->getSourceRange(),
11047                     FunctionTemplateParameterDifferentDefaultArgument)
11048                     << FirstTemplate << (i + 1) << FirstTA;
11049                 ODRDiagDeclNote(
11050                     SecondModule, SecondTemplate->getLocation(),
11051                     SecondTemplate->getSourceRange(),
11052                     FunctionTemplateParameterDifferentDefaultArgument)
11053                     << SecondTemplate << (i + 1) << SecondTA;
11054                 ParameterMismatch = true;
11055                 break;
11056               }
11057             }
11058 
11059             if (FirstTTPD->isParameterPack() !=
11060                 SecondTTPD->isParameterPack()) {
11061               ODRDiagDeclError(FirstRecord, FirstModule,
11062                                FirstTemplate->getLocation(),
11063                                FirstTemplate->getSourceRange(),
11064                                FunctionTemplatePackParameter)
11065                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11066               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11067                               SecondTemplate->getSourceRange(),
11068                               FunctionTemplatePackParameter)
11069                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11070               ParameterMismatch = true;
11071               break;
11072             }
11073           }
11074 
11075           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11076               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11077             NonTypeTemplateParmDecl *FirstNTTPD =
11078                 cast<NonTypeTemplateParmDecl>(FirstParam);
11079             NonTypeTemplateParmDecl *SecondNTTPD =
11080                 cast<NonTypeTemplateParmDecl>(SecondParam);
11081 
11082             QualType FirstType = FirstNTTPD->getType();
11083             QualType SecondType = SecondNTTPD->getType();
11084             if (ComputeQualTypeODRHash(FirstType) !=
11085                 ComputeQualTypeODRHash(SecondType)) {
11086               ODRDiagDeclError(FirstRecord, FirstModule,
11087                                FirstTemplate->getLocation(),
11088                                FirstTemplate->getSourceRange(),
11089                                FunctionTemplateParameterDifferentType)
11090                   << FirstTemplate << (i + 1);
11091               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11092                               SecondTemplate->getSourceRange(),
11093                               FunctionTemplateParameterDifferentType)
11094                   << SecondTemplate << (i + 1);
11095               ParameterMismatch = true;
11096               break;
11097             }
11098 
11099             bool HasFirstDefaultArgument =
11100                 FirstNTTPD->hasDefaultArgument() &&
11101                 !FirstNTTPD->defaultArgumentWasInherited();
11102             bool HasSecondDefaultArgument =
11103                 SecondNTTPD->hasDefaultArgument() &&
11104                 !SecondNTTPD->defaultArgumentWasInherited();
11105             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11106               ODRDiagDeclError(FirstRecord, FirstModule,
11107                                FirstTemplate->getLocation(),
11108                                FirstTemplate->getSourceRange(),
11109                                FunctionTemplateParameterSingleDefaultArgument)
11110                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11111               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11112                               SecondTemplate->getSourceRange(),
11113                               FunctionTemplateParameterSingleDefaultArgument)
11114                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11115               ParameterMismatch = true;
11116               break;
11117             }
11118 
11119             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11120               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11121               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11122               if (ComputeODRHash(FirstDefaultArgument) !=
11123                   ComputeODRHash(SecondDefaultArgument)) {
11124                 ODRDiagDeclError(
11125                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11126                     FirstTemplate->getSourceRange(),
11127                     FunctionTemplateParameterDifferentDefaultArgument)
11128                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11129                 ODRDiagDeclNote(
11130                     SecondModule, SecondTemplate->getLocation(),
11131                     SecondTemplate->getSourceRange(),
11132                     FunctionTemplateParameterDifferentDefaultArgument)
11133                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11134                 ParameterMismatch = true;
11135                 break;
11136               }
11137             }
11138 
11139             if (FirstNTTPD->isParameterPack() !=
11140                 SecondNTTPD->isParameterPack()) {
11141               ODRDiagDeclError(FirstRecord, FirstModule,
11142                                FirstTemplate->getLocation(),
11143                                FirstTemplate->getSourceRange(),
11144                                FunctionTemplatePackParameter)
11145                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11146               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11147                               SecondTemplate->getSourceRange(),
11148                               FunctionTemplatePackParameter)
11149                   << SecondTemplate << (i + 1)
11150                   << SecondNTTPD->isParameterPack();
11151               ParameterMismatch = true;
11152               break;
11153             }
11154           }
11155         }
11156 
11157         if (ParameterMismatch) {
11158           Diagnosed = true;
11159           break;
11160         }
11161 
11162         break;
11163       }
11164       }
11165 
11166       if (Diagnosed)
11167         continue;
11168 
11169       Diag(FirstDecl->getLocation(),
11170            diag::err_module_odr_violation_mismatch_decl_unknown)
11171           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11172           << FirstDecl->getSourceRange();
11173       Diag(SecondDecl->getLocation(),
11174            diag::note_module_odr_violation_mismatch_decl_unknown)
11175           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11176       Diagnosed = true;
11177     }
11178 
11179     if (!Diagnosed) {
11180       // All definitions are updates to the same declaration. This happens if a
11181       // module instantiates the declaration of a class template specialization
11182       // and two or more other modules instantiate its definition.
11183       //
11184       // FIXME: Indicate which modules had instantiations of this definition.
11185       // FIXME: How can this even happen?
11186       Diag(Merge.first->getLocation(),
11187            diag::err_module_odr_violation_different_instantiations)
11188         << Merge.first;
11189     }
11190   }
11191 
11192   // Issue ODR failures diagnostics for functions.
11193   for (auto &Merge : FunctionOdrMergeFailures) {
11194     enum ODRFunctionDifference {
11195       ReturnType,
11196       ParameterName,
11197       ParameterType,
11198       ParameterSingleDefaultArgument,
11199       ParameterDifferentDefaultArgument,
11200       FunctionBody,
11201     };
11202 
11203     FunctionDecl *FirstFunction = Merge.first;
11204     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11205 
11206     bool Diagnosed = false;
11207     for (auto &SecondFunction : Merge.second) {
11208 
11209       if (FirstFunction == SecondFunction)
11210         continue;
11211 
11212       std::string SecondModule =
11213           getOwningModuleNameForDiagnostic(SecondFunction);
11214 
11215       auto ODRDiagError = [FirstFunction, &FirstModule,
11216                            this](SourceLocation Loc, SourceRange Range,
11217                                  ODRFunctionDifference DiffType) {
11218         return Diag(Loc, diag::err_module_odr_violation_function)
11219                << FirstFunction << FirstModule.empty() << FirstModule << Range
11220                << DiffType;
11221       };
11222       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11223                                                SourceRange Range,
11224                                                ODRFunctionDifference DiffType) {
11225         return Diag(Loc, diag::note_module_odr_violation_function)
11226                << SecondModule << Range << DiffType;
11227       };
11228 
11229       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11230           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11231         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11232                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11233             << FirstFunction->getReturnType();
11234         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11235                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11236             << SecondFunction->getReturnType();
11237         Diagnosed = true;
11238         break;
11239       }
11240 
11241       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11242              "Merged functions with different number of parameters");
11243 
11244       auto ParamSize = FirstFunction->param_size();
11245       bool ParameterMismatch = false;
11246       for (unsigned I = 0; I < ParamSize; ++I) {
11247         auto *FirstParam = FirstFunction->getParamDecl(I);
11248         auto *SecondParam = SecondFunction->getParamDecl(I);
11249 
11250         assert(getContext().hasSameType(FirstParam->getType(),
11251                                       SecondParam->getType()) &&
11252                "Merged function has different parameter types.");
11253 
11254         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11255           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11256                        ParameterName)
11257               << I + 1 << FirstParam->getDeclName();
11258           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11259                       ParameterName)
11260               << I + 1 << SecondParam->getDeclName();
11261           ParameterMismatch = true;
11262           break;
11263         };
11264 
11265         QualType FirstParamType = FirstParam->getType();
11266         QualType SecondParamType = SecondParam->getType();
11267         if (FirstParamType != SecondParamType &&
11268             ComputeQualTypeODRHash(FirstParamType) !=
11269                 ComputeQualTypeODRHash(SecondParamType)) {
11270           if (const DecayedType *ParamDecayedType =
11271                   FirstParamType->getAs<DecayedType>()) {
11272             ODRDiagError(FirstParam->getLocation(),
11273                          FirstParam->getSourceRange(), ParameterType)
11274                 << (I + 1) << FirstParamType << true
11275                 << ParamDecayedType->getOriginalType();
11276           } else {
11277             ODRDiagError(FirstParam->getLocation(),
11278                          FirstParam->getSourceRange(), ParameterType)
11279                 << (I + 1) << FirstParamType << false;
11280           }
11281 
11282           if (const DecayedType *ParamDecayedType =
11283                   SecondParamType->getAs<DecayedType>()) {
11284             ODRDiagNote(SecondParam->getLocation(),
11285                         SecondParam->getSourceRange(), ParameterType)
11286                 << (I + 1) << SecondParamType << true
11287                 << ParamDecayedType->getOriginalType();
11288           } else {
11289             ODRDiagNote(SecondParam->getLocation(),
11290                         SecondParam->getSourceRange(), ParameterType)
11291                 << (I + 1) << SecondParamType << false;
11292           }
11293           ParameterMismatch = true;
11294           break;
11295         }
11296 
11297         const Expr *FirstInit = FirstParam->getInit();
11298         const Expr *SecondInit = SecondParam->getInit();
11299         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11300           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11301                        ParameterSingleDefaultArgument)
11302               << (I + 1) << (FirstInit == nullptr)
11303               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11304           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11305                       ParameterSingleDefaultArgument)
11306               << (I + 1) << (SecondInit == nullptr)
11307               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11308           ParameterMismatch = true;
11309           break;
11310         }
11311 
11312         if (FirstInit && SecondInit &&
11313             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11314           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11315                        ParameterDifferentDefaultArgument)
11316               << (I + 1) << FirstInit->getSourceRange();
11317           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11318                       ParameterDifferentDefaultArgument)
11319               << (I + 1) << SecondInit->getSourceRange();
11320           ParameterMismatch = true;
11321           break;
11322         }
11323 
11324         assert(ComputeSubDeclODRHash(FirstParam) ==
11325                    ComputeSubDeclODRHash(SecondParam) &&
11326                "Undiagnosed parameter difference.");
11327       }
11328 
11329       if (ParameterMismatch) {
11330         Diagnosed = true;
11331         break;
11332       }
11333 
11334       // If no error has been generated before now, assume the problem is in
11335       // the body and generate a message.
11336       ODRDiagError(FirstFunction->getLocation(),
11337                    FirstFunction->getSourceRange(), FunctionBody);
11338       ODRDiagNote(SecondFunction->getLocation(),
11339                   SecondFunction->getSourceRange(), FunctionBody);
11340       Diagnosed = true;
11341       break;
11342     }
11343     (void)Diagnosed;
11344     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11345   }
11346 
11347   // Issue ODR failures diagnostics for enums.
11348   for (auto &Merge : EnumOdrMergeFailures) {
11349     enum ODREnumDifference {
11350       SingleScopedEnum,
11351       EnumTagKeywordMismatch,
11352       SingleSpecifiedType,
11353       DifferentSpecifiedTypes,
11354       DifferentNumberEnumConstants,
11355       EnumConstantName,
11356       EnumConstantSingleInitilizer,
11357       EnumConstantDifferentInitilizer,
11358     };
11359 
11360     // If we've already pointed out a specific problem with this enum, don't
11361     // bother issuing a general "something's different" diagnostic.
11362     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11363       continue;
11364 
11365     EnumDecl *FirstEnum = Merge.first;
11366     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11367 
11368     using DeclHashes =
11369         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11370     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11371                               DeclHashes &Hashes, EnumDecl *Enum) {
11372       for (auto *D : Enum->decls()) {
11373         // Due to decl merging, the first EnumDecl is the parent of
11374         // Decls in both records.
11375         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11376           continue;
11377         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11378         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11379                             ComputeSubDeclODRHash(D));
11380       }
11381     };
11382     DeclHashes FirstHashes;
11383     PopulateHashes(FirstHashes, FirstEnum);
11384     bool Diagnosed = false;
11385     for (auto &SecondEnum : Merge.second) {
11386 
11387       if (FirstEnum == SecondEnum)
11388         continue;
11389 
11390       std::string SecondModule =
11391           getOwningModuleNameForDiagnostic(SecondEnum);
11392 
11393       auto ODRDiagError = [FirstEnum, &FirstModule,
11394                            this](SourceLocation Loc, SourceRange Range,
11395                                  ODREnumDifference DiffType) {
11396         return Diag(Loc, diag::err_module_odr_violation_enum)
11397                << FirstEnum << FirstModule.empty() << FirstModule << Range
11398                << DiffType;
11399       };
11400       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11401                                                SourceRange Range,
11402                                                ODREnumDifference DiffType) {
11403         return Diag(Loc, diag::note_module_odr_violation_enum)
11404                << SecondModule << Range << DiffType;
11405       };
11406 
11407       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11408         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11409                      SingleScopedEnum)
11410             << FirstEnum->isScoped();
11411         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11412                     SingleScopedEnum)
11413             << SecondEnum->isScoped();
11414         Diagnosed = true;
11415         continue;
11416       }
11417 
11418       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11419         if (FirstEnum->isScopedUsingClassTag() !=
11420             SecondEnum->isScopedUsingClassTag()) {
11421           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11422                        EnumTagKeywordMismatch)
11423               << FirstEnum->isScopedUsingClassTag();
11424           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11425                       EnumTagKeywordMismatch)
11426               << SecondEnum->isScopedUsingClassTag();
11427           Diagnosed = true;
11428           continue;
11429         }
11430       }
11431 
11432       QualType FirstUnderlyingType =
11433           FirstEnum->getIntegerTypeSourceInfo()
11434               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11435               : QualType();
11436       QualType SecondUnderlyingType =
11437           SecondEnum->getIntegerTypeSourceInfo()
11438               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11439               : QualType();
11440       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11441           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11442                        SingleSpecifiedType)
11443               << !FirstUnderlyingType.isNull();
11444           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11445                       SingleSpecifiedType)
11446               << !SecondUnderlyingType.isNull();
11447           Diagnosed = true;
11448           continue;
11449       }
11450 
11451       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11452         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11453             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11454           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11455                        DifferentSpecifiedTypes)
11456               << FirstUnderlyingType;
11457           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11458                       DifferentSpecifiedTypes)
11459               << SecondUnderlyingType;
11460           Diagnosed = true;
11461           continue;
11462         }
11463       }
11464 
11465       DeclHashes SecondHashes;
11466       PopulateHashes(SecondHashes, SecondEnum);
11467 
11468       if (FirstHashes.size() != SecondHashes.size()) {
11469         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11470                      DifferentNumberEnumConstants)
11471             << (int)FirstHashes.size();
11472         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11473                     DifferentNumberEnumConstants)
11474             << (int)SecondHashes.size();
11475         Diagnosed = true;
11476         continue;
11477       }
11478 
11479       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11480         if (FirstHashes[I].second == SecondHashes[I].second)
11481           continue;
11482         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11483         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11484 
11485         if (FirstEnumConstant->getDeclName() !=
11486             SecondEnumConstant->getDeclName()) {
11487 
11488           ODRDiagError(FirstEnumConstant->getLocation(),
11489                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11490               << I + 1 << FirstEnumConstant;
11491           ODRDiagNote(SecondEnumConstant->getLocation(),
11492                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11493               << I + 1 << SecondEnumConstant;
11494           Diagnosed = true;
11495           break;
11496         }
11497 
11498         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11499         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11500         if (!FirstInit && !SecondInit)
11501           continue;
11502 
11503         if (!FirstInit || !SecondInit) {
11504           ODRDiagError(FirstEnumConstant->getLocation(),
11505                        FirstEnumConstant->getSourceRange(),
11506                        EnumConstantSingleInitilizer)
11507               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11508           ODRDiagNote(SecondEnumConstant->getLocation(),
11509                       SecondEnumConstant->getSourceRange(),
11510                       EnumConstantSingleInitilizer)
11511               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11512           Diagnosed = true;
11513           break;
11514         }
11515 
11516         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11517           ODRDiagError(FirstEnumConstant->getLocation(),
11518                        FirstEnumConstant->getSourceRange(),
11519                        EnumConstantDifferentInitilizer)
11520               << I + 1 << FirstEnumConstant;
11521           ODRDiagNote(SecondEnumConstant->getLocation(),
11522                       SecondEnumConstant->getSourceRange(),
11523                       EnumConstantDifferentInitilizer)
11524               << I + 1 << SecondEnumConstant;
11525           Diagnosed = true;
11526           break;
11527         }
11528       }
11529     }
11530 
11531     (void)Diagnosed;
11532     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11533   }
11534 }
11535 
11536 void ASTReader::StartedDeserializing() {
11537   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11538     ReadTimer->startTimer();
11539 }
11540 
11541 void ASTReader::FinishedDeserializing() {
11542   assert(NumCurrentElementsDeserializing &&
11543          "FinishedDeserializing not paired with StartedDeserializing");
11544   if (NumCurrentElementsDeserializing == 1) {
11545     // We decrease NumCurrentElementsDeserializing only after pending actions
11546     // are finished, to avoid recursively re-calling finishPendingActions().
11547     finishPendingActions();
11548   }
11549   --NumCurrentElementsDeserializing;
11550 
11551   if (NumCurrentElementsDeserializing == 0) {
11552     // Propagate exception specification and deduced type updates along
11553     // redeclaration chains.
11554     //
11555     // We do this now rather than in finishPendingActions because we want to
11556     // be able to walk the complete redeclaration chains of the updated decls.
11557     while (!PendingExceptionSpecUpdates.empty() ||
11558            !PendingDeducedTypeUpdates.empty()) {
11559       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11560       PendingExceptionSpecUpdates.clear();
11561       for (auto Update : ESUpdates) {
11562         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11563         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11564         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11565         if (auto *Listener = getContext().getASTMutationListener())
11566           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11567         for (auto *Redecl : Update.second->redecls())
11568           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11569       }
11570 
11571       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11572       PendingDeducedTypeUpdates.clear();
11573       for (auto Update : DTUpdates) {
11574         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11575         // FIXME: If the return type is already deduced, check that it matches.
11576         getContext().adjustDeducedFunctionResultType(Update.first,
11577                                                      Update.second);
11578       }
11579     }
11580 
11581     if (ReadTimer)
11582       ReadTimer->stopTimer();
11583 
11584     diagnoseOdrViolations();
11585 
11586     // We are not in recursive loading, so it's safe to pass the "interesting"
11587     // decls to the consumer.
11588     if (Consumer)
11589       PassInterestingDeclsToConsumer();
11590   }
11591 }
11592 
11593 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11594   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11595     // Remove any fake results before adding any real ones.
11596     auto It = PendingFakeLookupResults.find(II);
11597     if (It != PendingFakeLookupResults.end()) {
11598       for (auto *ND : It->second)
11599         SemaObj->IdResolver.RemoveDecl(ND);
11600       // FIXME: this works around module+PCH performance issue.
11601       // Rather than erase the result from the map, which is O(n), just clear
11602       // the vector of NamedDecls.
11603       It->second.clear();
11604     }
11605   }
11606 
11607   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11608     SemaObj->TUScope->AddDecl(D);
11609   } else if (SemaObj->TUScope) {
11610     // Adding the decl to IdResolver may have failed because it was already in
11611     // (even though it was not added in scope). If it is already in, make sure
11612     // it gets in the scope as well.
11613     if (std::find(SemaObj->IdResolver.begin(Name),
11614                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11615       SemaObj->TUScope->AddDecl(D);
11616   }
11617 }
11618 
11619 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11620                      ASTContext *Context,
11621                      const PCHContainerReader &PCHContainerRdr,
11622                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11623                      StringRef isysroot,
11624                      DisableValidationForModuleKind DisableValidationKind,
11625                      bool AllowASTWithCompilerErrors,
11626                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11627                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11628                      std::unique_ptr<llvm::Timer> ReadTimer)
11629     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11630                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11631                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11632       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11633       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11634       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11635                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11636       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11637       DisableValidationKind(DisableValidationKind),
11638       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11639       AllowConfigurationMismatch(AllowConfigurationMismatch),
11640       ValidateSystemInputs(ValidateSystemInputs),
11641       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11642       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11643   SourceMgr.setExternalSLocEntrySource(this);
11644 
11645   for (const auto &Ext : Extensions) {
11646     auto BlockName = Ext->getExtensionMetadata().BlockName;
11647     auto Known = ModuleFileExtensions.find(BlockName);
11648     if (Known != ModuleFileExtensions.end()) {
11649       Diags.Report(diag::warn_duplicate_module_file_extension)
11650         << BlockName;
11651       continue;
11652     }
11653 
11654     ModuleFileExtensions.insert({BlockName, Ext});
11655   }
11656 }
11657 
11658 ASTReader::~ASTReader() {
11659   if (OwnsDeserializationListener)
11660     delete DeserializationListener;
11661 }
11662 
11663 IdentifierResolver &ASTReader::getIdResolver() {
11664   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11665 }
11666 
11667 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11668                                                unsigned AbbrevID) {
11669   Idx = 0;
11670   Record.clear();
11671   return Cursor.readRecord(AbbrevID, Record);
11672 }
11673 //===----------------------------------------------------------------------===//
11674 //// OMPClauseReader implementation
11675 ////===----------------------------------------------------------------------===//
11676 
11677 // This has to be in namespace clang because it's friended by all
11678 // of the OMP clauses.
11679 namespace clang {
11680 
11681 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11682   ASTRecordReader &Record;
11683   ASTContext &Context;
11684 
11685 public:
11686   OMPClauseReader(ASTRecordReader &Record)
11687       : Record(Record), Context(Record.getContext()) {}
11688 #define GEN_CLANG_CLAUSE_CLASS
11689 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11690 #include "llvm/Frontend/OpenMP/OMP.inc"
11691   OMPClause *readClause();
11692   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11693   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11694 };
11695 
11696 } // end namespace clang
11697 
11698 OMPClause *ASTRecordReader::readOMPClause() {
11699   return OMPClauseReader(*this).readClause();
11700 }
11701 
11702 OMPClause *OMPClauseReader::readClause() {
11703   OMPClause *C = nullptr;
11704   switch (llvm::omp::Clause(Record.readInt())) {
11705   case llvm::omp::OMPC_if:
11706     C = new (Context) OMPIfClause();
11707     break;
11708   case llvm::omp::OMPC_final:
11709     C = new (Context) OMPFinalClause();
11710     break;
11711   case llvm::omp::OMPC_num_threads:
11712     C = new (Context) OMPNumThreadsClause();
11713     break;
11714   case llvm::omp::OMPC_safelen:
11715     C = new (Context) OMPSafelenClause();
11716     break;
11717   case llvm::omp::OMPC_simdlen:
11718     C = new (Context) OMPSimdlenClause();
11719     break;
11720   case llvm::omp::OMPC_sizes: {
11721     unsigned NumSizes = Record.readInt();
11722     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11723     break;
11724   }
11725   case llvm::omp::OMPC_full:
11726     C = OMPFullClause::CreateEmpty(Context);
11727     break;
11728   case llvm::omp::OMPC_partial:
11729     C = OMPPartialClause::CreateEmpty(Context);
11730     break;
11731   case llvm::omp::OMPC_allocator:
11732     C = new (Context) OMPAllocatorClause();
11733     break;
11734   case llvm::omp::OMPC_collapse:
11735     C = new (Context) OMPCollapseClause();
11736     break;
11737   case llvm::omp::OMPC_default:
11738     C = new (Context) OMPDefaultClause();
11739     break;
11740   case llvm::omp::OMPC_proc_bind:
11741     C = new (Context) OMPProcBindClause();
11742     break;
11743   case llvm::omp::OMPC_schedule:
11744     C = new (Context) OMPScheduleClause();
11745     break;
11746   case llvm::omp::OMPC_ordered:
11747     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11748     break;
11749   case llvm::omp::OMPC_nowait:
11750     C = new (Context) OMPNowaitClause();
11751     break;
11752   case llvm::omp::OMPC_untied:
11753     C = new (Context) OMPUntiedClause();
11754     break;
11755   case llvm::omp::OMPC_mergeable:
11756     C = new (Context) OMPMergeableClause();
11757     break;
11758   case llvm::omp::OMPC_read:
11759     C = new (Context) OMPReadClause();
11760     break;
11761   case llvm::omp::OMPC_write:
11762     C = new (Context) OMPWriteClause();
11763     break;
11764   case llvm::omp::OMPC_update:
11765     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11766     break;
11767   case llvm::omp::OMPC_capture:
11768     C = new (Context) OMPCaptureClause();
11769     break;
11770   case llvm::omp::OMPC_seq_cst:
11771     C = new (Context) OMPSeqCstClause();
11772     break;
11773   case llvm::omp::OMPC_acq_rel:
11774     C = new (Context) OMPAcqRelClause();
11775     break;
11776   case llvm::omp::OMPC_acquire:
11777     C = new (Context) OMPAcquireClause();
11778     break;
11779   case llvm::omp::OMPC_release:
11780     C = new (Context) OMPReleaseClause();
11781     break;
11782   case llvm::omp::OMPC_relaxed:
11783     C = new (Context) OMPRelaxedClause();
11784     break;
11785   case llvm::omp::OMPC_threads:
11786     C = new (Context) OMPThreadsClause();
11787     break;
11788   case llvm::omp::OMPC_simd:
11789     C = new (Context) OMPSIMDClause();
11790     break;
11791   case llvm::omp::OMPC_nogroup:
11792     C = new (Context) OMPNogroupClause();
11793     break;
11794   case llvm::omp::OMPC_unified_address:
11795     C = new (Context) OMPUnifiedAddressClause();
11796     break;
11797   case llvm::omp::OMPC_unified_shared_memory:
11798     C = new (Context) OMPUnifiedSharedMemoryClause();
11799     break;
11800   case llvm::omp::OMPC_reverse_offload:
11801     C = new (Context) OMPReverseOffloadClause();
11802     break;
11803   case llvm::omp::OMPC_dynamic_allocators:
11804     C = new (Context) OMPDynamicAllocatorsClause();
11805     break;
11806   case llvm::omp::OMPC_atomic_default_mem_order:
11807     C = new (Context) OMPAtomicDefaultMemOrderClause();
11808     break;
11809  case llvm::omp::OMPC_private:
11810     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11811     break;
11812   case llvm::omp::OMPC_firstprivate:
11813     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11814     break;
11815   case llvm::omp::OMPC_lastprivate:
11816     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11817     break;
11818   case llvm::omp::OMPC_shared:
11819     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11820     break;
11821   case llvm::omp::OMPC_reduction: {
11822     unsigned N = Record.readInt();
11823     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11824     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11825     break;
11826   }
11827   case llvm::omp::OMPC_task_reduction:
11828     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11829     break;
11830   case llvm::omp::OMPC_in_reduction:
11831     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11832     break;
11833   case llvm::omp::OMPC_linear:
11834     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11835     break;
11836   case llvm::omp::OMPC_aligned:
11837     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11838     break;
11839   case llvm::omp::OMPC_copyin:
11840     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11841     break;
11842   case llvm::omp::OMPC_copyprivate:
11843     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11844     break;
11845   case llvm::omp::OMPC_flush:
11846     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11847     break;
11848   case llvm::omp::OMPC_depobj:
11849     C = OMPDepobjClause::CreateEmpty(Context);
11850     break;
11851   case llvm::omp::OMPC_depend: {
11852     unsigned NumVars = Record.readInt();
11853     unsigned NumLoops = Record.readInt();
11854     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11855     break;
11856   }
11857   case llvm::omp::OMPC_device:
11858     C = new (Context) OMPDeviceClause();
11859     break;
11860   case llvm::omp::OMPC_map: {
11861     OMPMappableExprListSizeTy Sizes;
11862     Sizes.NumVars = Record.readInt();
11863     Sizes.NumUniqueDeclarations = Record.readInt();
11864     Sizes.NumComponentLists = Record.readInt();
11865     Sizes.NumComponents = Record.readInt();
11866     C = OMPMapClause::CreateEmpty(Context, Sizes);
11867     break;
11868   }
11869   case llvm::omp::OMPC_num_teams:
11870     C = new (Context) OMPNumTeamsClause();
11871     break;
11872   case llvm::omp::OMPC_thread_limit:
11873     C = new (Context) OMPThreadLimitClause();
11874     break;
11875   case llvm::omp::OMPC_priority:
11876     C = new (Context) OMPPriorityClause();
11877     break;
11878   case llvm::omp::OMPC_grainsize:
11879     C = new (Context) OMPGrainsizeClause();
11880     break;
11881   case llvm::omp::OMPC_num_tasks:
11882     C = new (Context) OMPNumTasksClause();
11883     break;
11884   case llvm::omp::OMPC_hint:
11885     C = new (Context) OMPHintClause();
11886     break;
11887   case llvm::omp::OMPC_dist_schedule:
11888     C = new (Context) OMPDistScheduleClause();
11889     break;
11890   case llvm::omp::OMPC_defaultmap:
11891     C = new (Context) OMPDefaultmapClause();
11892     break;
11893   case llvm::omp::OMPC_to: {
11894     OMPMappableExprListSizeTy Sizes;
11895     Sizes.NumVars = Record.readInt();
11896     Sizes.NumUniqueDeclarations = Record.readInt();
11897     Sizes.NumComponentLists = Record.readInt();
11898     Sizes.NumComponents = Record.readInt();
11899     C = OMPToClause::CreateEmpty(Context, Sizes);
11900     break;
11901   }
11902   case llvm::omp::OMPC_from: {
11903     OMPMappableExprListSizeTy Sizes;
11904     Sizes.NumVars = Record.readInt();
11905     Sizes.NumUniqueDeclarations = Record.readInt();
11906     Sizes.NumComponentLists = Record.readInt();
11907     Sizes.NumComponents = Record.readInt();
11908     C = OMPFromClause::CreateEmpty(Context, Sizes);
11909     break;
11910   }
11911   case llvm::omp::OMPC_use_device_ptr: {
11912     OMPMappableExprListSizeTy Sizes;
11913     Sizes.NumVars = Record.readInt();
11914     Sizes.NumUniqueDeclarations = Record.readInt();
11915     Sizes.NumComponentLists = Record.readInt();
11916     Sizes.NumComponents = Record.readInt();
11917     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11918     break;
11919   }
11920   case llvm::omp::OMPC_use_device_addr: {
11921     OMPMappableExprListSizeTy Sizes;
11922     Sizes.NumVars = Record.readInt();
11923     Sizes.NumUniqueDeclarations = Record.readInt();
11924     Sizes.NumComponentLists = Record.readInt();
11925     Sizes.NumComponents = Record.readInt();
11926     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11927     break;
11928   }
11929   case llvm::omp::OMPC_is_device_ptr: {
11930     OMPMappableExprListSizeTy Sizes;
11931     Sizes.NumVars = Record.readInt();
11932     Sizes.NumUniqueDeclarations = Record.readInt();
11933     Sizes.NumComponentLists = Record.readInt();
11934     Sizes.NumComponents = Record.readInt();
11935     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11936     break;
11937   }
11938   case llvm::omp::OMPC_allocate:
11939     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11940     break;
11941   case llvm::omp::OMPC_nontemporal:
11942     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11943     break;
11944   case llvm::omp::OMPC_inclusive:
11945     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11946     break;
11947   case llvm::omp::OMPC_exclusive:
11948     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11949     break;
11950   case llvm::omp::OMPC_order:
11951     C = new (Context) OMPOrderClause();
11952     break;
11953   case llvm::omp::OMPC_init:
11954     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11955     break;
11956   case llvm::omp::OMPC_use:
11957     C = new (Context) OMPUseClause();
11958     break;
11959   case llvm::omp::OMPC_destroy:
11960     C = new (Context) OMPDestroyClause();
11961     break;
11962   case llvm::omp::OMPC_novariants:
11963     C = new (Context) OMPNovariantsClause();
11964     break;
11965   case llvm::omp::OMPC_nocontext:
11966     C = new (Context) OMPNocontextClause();
11967     break;
11968   case llvm::omp::OMPC_detach:
11969     C = new (Context) OMPDetachClause();
11970     break;
11971   case llvm::omp::OMPC_uses_allocators:
11972     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11973     break;
11974   case llvm::omp::OMPC_affinity:
11975     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11976     break;
11977   case llvm::omp::OMPC_filter:
11978     C = new (Context) OMPFilterClause();
11979     break;
11980 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11981   case llvm::omp::Enum:                                                        \
11982     break;
11983 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11984   default:
11985     break;
11986   }
11987   assert(C && "Unknown OMPClause type");
11988 
11989   Visit(C);
11990   C->setLocStart(Record.readSourceLocation());
11991   C->setLocEnd(Record.readSourceLocation());
11992 
11993   return C;
11994 }
11995 
11996 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11997   C->setPreInitStmt(Record.readSubStmt(),
11998                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11999 }
12000 
12001 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12002   VisitOMPClauseWithPreInit(C);
12003   C->setPostUpdateExpr(Record.readSubExpr());
12004 }
12005 
12006 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12007   VisitOMPClauseWithPreInit(C);
12008   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12009   C->setNameModifierLoc(Record.readSourceLocation());
12010   C->setColonLoc(Record.readSourceLocation());
12011   C->setCondition(Record.readSubExpr());
12012   C->setLParenLoc(Record.readSourceLocation());
12013 }
12014 
12015 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12016   VisitOMPClauseWithPreInit(C);
12017   C->setCondition(Record.readSubExpr());
12018   C->setLParenLoc(Record.readSourceLocation());
12019 }
12020 
12021 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12022   VisitOMPClauseWithPreInit(C);
12023   C->setNumThreads(Record.readSubExpr());
12024   C->setLParenLoc(Record.readSourceLocation());
12025 }
12026 
12027 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12028   C->setSafelen(Record.readSubExpr());
12029   C->setLParenLoc(Record.readSourceLocation());
12030 }
12031 
12032 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12033   C->setSimdlen(Record.readSubExpr());
12034   C->setLParenLoc(Record.readSourceLocation());
12035 }
12036 
12037 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12038   for (Expr *&E : C->getSizesRefs())
12039     E = Record.readSubExpr();
12040   C->setLParenLoc(Record.readSourceLocation());
12041 }
12042 
12043 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12044 
12045 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12046   C->setFactor(Record.readSubExpr());
12047   C->setLParenLoc(Record.readSourceLocation());
12048 }
12049 
12050 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12051   C->setAllocator(Record.readExpr());
12052   C->setLParenLoc(Record.readSourceLocation());
12053 }
12054 
12055 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12056   C->setNumForLoops(Record.readSubExpr());
12057   C->setLParenLoc(Record.readSourceLocation());
12058 }
12059 
12060 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12061   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12062   C->setLParenLoc(Record.readSourceLocation());
12063   C->setDefaultKindKwLoc(Record.readSourceLocation());
12064 }
12065 
12066 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12067   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12068   C->setLParenLoc(Record.readSourceLocation());
12069   C->setProcBindKindKwLoc(Record.readSourceLocation());
12070 }
12071 
12072 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12073   VisitOMPClauseWithPreInit(C);
12074   C->setScheduleKind(
12075        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12076   C->setFirstScheduleModifier(
12077       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12078   C->setSecondScheduleModifier(
12079       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12080   C->setChunkSize(Record.readSubExpr());
12081   C->setLParenLoc(Record.readSourceLocation());
12082   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12083   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12084   C->setScheduleKindLoc(Record.readSourceLocation());
12085   C->setCommaLoc(Record.readSourceLocation());
12086 }
12087 
12088 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12089   C->setNumForLoops(Record.readSubExpr());
12090   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12091     C->setLoopNumIterations(I, Record.readSubExpr());
12092   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12093     C->setLoopCounter(I, Record.readSubExpr());
12094   C->setLParenLoc(Record.readSourceLocation());
12095 }
12096 
12097 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12098   C->setEventHandler(Record.readSubExpr());
12099   C->setLParenLoc(Record.readSourceLocation());
12100 }
12101 
12102 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12103 
12104 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12105 
12106 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12107 
12108 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12109 
12110 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12111 
12112 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12113   if (C->isExtended()) {
12114     C->setLParenLoc(Record.readSourceLocation());
12115     C->setArgumentLoc(Record.readSourceLocation());
12116     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12117   }
12118 }
12119 
12120 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12121 
12122 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12123 
12124 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12125 
12126 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12127 
12128 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12129 
12130 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12131 
12132 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12133 
12134 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12135 
12136 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12137 
12138 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12139   unsigned NumVars = C->varlist_size();
12140   SmallVector<Expr *, 16> Vars;
12141   Vars.reserve(NumVars);
12142   for (unsigned I = 0; I != NumVars; ++I)
12143     Vars.push_back(Record.readSubExpr());
12144   C->setVarRefs(Vars);
12145   C->setIsTarget(Record.readBool());
12146   C->setIsTargetSync(Record.readBool());
12147   C->setLParenLoc(Record.readSourceLocation());
12148   C->setVarLoc(Record.readSourceLocation());
12149 }
12150 
12151 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12152   C->setInteropVar(Record.readSubExpr());
12153   C->setLParenLoc(Record.readSourceLocation());
12154   C->setVarLoc(Record.readSourceLocation());
12155 }
12156 
12157 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12158   C->setInteropVar(Record.readSubExpr());
12159   C->setLParenLoc(Record.readSourceLocation());
12160   C->setVarLoc(Record.readSourceLocation());
12161 }
12162 
12163 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12164   VisitOMPClauseWithPreInit(C);
12165   C->setCondition(Record.readSubExpr());
12166   C->setLParenLoc(Record.readSourceLocation());
12167 }
12168 
12169 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12170   VisitOMPClauseWithPreInit(C);
12171   C->setCondition(Record.readSubExpr());
12172   C->setLParenLoc(Record.readSourceLocation());
12173 }
12174 
12175 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12176 
12177 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12178     OMPUnifiedSharedMemoryClause *) {}
12179 
12180 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12181 
12182 void
12183 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12184 }
12185 
12186 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12187     OMPAtomicDefaultMemOrderClause *C) {
12188   C->setAtomicDefaultMemOrderKind(
12189       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12190   C->setLParenLoc(Record.readSourceLocation());
12191   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12192 }
12193 
12194 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12195   C->setLParenLoc(Record.readSourceLocation());
12196   unsigned NumVars = C->varlist_size();
12197   SmallVector<Expr *, 16> Vars;
12198   Vars.reserve(NumVars);
12199   for (unsigned i = 0; i != NumVars; ++i)
12200     Vars.push_back(Record.readSubExpr());
12201   C->setVarRefs(Vars);
12202   Vars.clear();
12203   for (unsigned i = 0; i != NumVars; ++i)
12204     Vars.push_back(Record.readSubExpr());
12205   C->setPrivateCopies(Vars);
12206 }
12207 
12208 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12209   VisitOMPClauseWithPreInit(C);
12210   C->setLParenLoc(Record.readSourceLocation());
12211   unsigned NumVars = C->varlist_size();
12212   SmallVector<Expr *, 16> Vars;
12213   Vars.reserve(NumVars);
12214   for (unsigned i = 0; i != NumVars; ++i)
12215     Vars.push_back(Record.readSubExpr());
12216   C->setVarRefs(Vars);
12217   Vars.clear();
12218   for (unsigned i = 0; i != NumVars; ++i)
12219     Vars.push_back(Record.readSubExpr());
12220   C->setPrivateCopies(Vars);
12221   Vars.clear();
12222   for (unsigned i = 0; i != NumVars; ++i)
12223     Vars.push_back(Record.readSubExpr());
12224   C->setInits(Vars);
12225 }
12226 
12227 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12228   VisitOMPClauseWithPostUpdate(C);
12229   C->setLParenLoc(Record.readSourceLocation());
12230   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12231   C->setKindLoc(Record.readSourceLocation());
12232   C->setColonLoc(Record.readSourceLocation());
12233   unsigned NumVars = C->varlist_size();
12234   SmallVector<Expr *, 16> Vars;
12235   Vars.reserve(NumVars);
12236   for (unsigned i = 0; i != NumVars; ++i)
12237     Vars.push_back(Record.readSubExpr());
12238   C->setVarRefs(Vars);
12239   Vars.clear();
12240   for (unsigned i = 0; i != NumVars; ++i)
12241     Vars.push_back(Record.readSubExpr());
12242   C->setPrivateCopies(Vars);
12243   Vars.clear();
12244   for (unsigned i = 0; i != NumVars; ++i)
12245     Vars.push_back(Record.readSubExpr());
12246   C->setSourceExprs(Vars);
12247   Vars.clear();
12248   for (unsigned i = 0; i != NumVars; ++i)
12249     Vars.push_back(Record.readSubExpr());
12250   C->setDestinationExprs(Vars);
12251   Vars.clear();
12252   for (unsigned i = 0; i != NumVars; ++i)
12253     Vars.push_back(Record.readSubExpr());
12254   C->setAssignmentOps(Vars);
12255 }
12256 
12257 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12258   C->setLParenLoc(Record.readSourceLocation());
12259   unsigned NumVars = C->varlist_size();
12260   SmallVector<Expr *, 16> Vars;
12261   Vars.reserve(NumVars);
12262   for (unsigned i = 0; i != NumVars; ++i)
12263     Vars.push_back(Record.readSubExpr());
12264   C->setVarRefs(Vars);
12265 }
12266 
12267 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12268   VisitOMPClauseWithPostUpdate(C);
12269   C->setLParenLoc(Record.readSourceLocation());
12270   C->setModifierLoc(Record.readSourceLocation());
12271   C->setColonLoc(Record.readSourceLocation());
12272   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12273   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12274   C->setQualifierLoc(NNSL);
12275   C->setNameInfo(DNI);
12276 
12277   unsigned NumVars = C->varlist_size();
12278   SmallVector<Expr *, 16> Vars;
12279   Vars.reserve(NumVars);
12280   for (unsigned i = 0; i != NumVars; ++i)
12281     Vars.push_back(Record.readSubExpr());
12282   C->setVarRefs(Vars);
12283   Vars.clear();
12284   for (unsigned i = 0; i != NumVars; ++i)
12285     Vars.push_back(Record.readSubExpr());
12286   C->setPrivates(Vars);
12287   Vars.clear();
12288   for (unsigned i = 0; i != NumVars; ++i)
12289     Vars.push_back(Record.readSubExpr());
12290   C->setLHSExprs(Vars);
12291   Vars.clear();
12292   for (unsigned i = 0; i != NumVars; ++i)
12293     Vars.push_back(Record.readSubExpr());
12294   C->setRHSExprs(Vars);
12295   Vars.clear();
12296   for (unsigned i = 0; i != NumVars; ++i)
12297     Vars.push_back(Record.readSubExpr());
12298   C->setReductionOps(Vars);
12299   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12300     Vars.clear();
12301     for (unsigned i = 0; i != NumVars; ++i)
12302       Vars.push_back(Record.readSubExpr());
12303     C->setInscanCopyOps(Vars);
12304     Vars.clear();
12305     for (unsigned i = 0; i != NumVars; ++i)
12306       Vars.push_back(Record.readSubExpr());
12307     C->setInscanCopyArrayTemps(Vars);
12308     Vars.clear();
12309     for (unsigned i = 0; i != NumVars; ++i)
12310       Vars.push_back(Record.readSubExpr());
12311     C->setInscanCopyArrayElems(Vars);
12312   }
12313 }
12314 
12315 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12316   VisitOMPClauseWithPostUpdate(C);
12317   C->setLParenLoc(Record.readSourceLocation());
12318   C->setColonLoc(Record.readSourceLocation());
12319   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12320   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12321   C->setQualifierLoc(NNSL);
12322   C->setNameInfo(DNI);
12323 
12324   unsigned NumVars = C->varlist_size();
12325   SmallVector<Expr *, 16> Vars;
12326   Vars.reserve(NumVars);
12327   for (unsigned I = 0; I != NumVars; ++I)
12328     Vars.push_back(Record.readSubExpr());
12329   C->setVarRefs(Vars);
12330   Vars.clear();
12331   for (unsigned I = 0; I != NumVars; ++I)
12332     Vars.push_back(Record.readSubExpr());
12333   C->setPrivates(Vars);
12334   Vars.clear();
12335   for (unsigned I = 0; I != NumVars; ++I)
12336     Vars.push_back(Record.readSubExpr());
12337   C->setLHSExprs(Vars);
12338   Vars.clear();
12339   for (unsigned I = 0; I != NumVars; ++I)
12340     Vars.push_back(Record.readSubExpr());
12341   C->setRHSExprs(Vars);
12342   Vars.clear();
12343   for (unsigned I = 0; I != NumVars; ++I)
12344     Vars.push_back(Record.readSubExpr());
12345   C->setReductionOps(Vars);
12346 }
12347 
12348 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12349   VisitOMPClauseWithPostUpdate(C);
12350   C->setLParenLoc(Record.readSourceLocation());
12351   C->setColonLoc(Record.readSourceLocation());
12352   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12353   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12354   C->setQualifierLoc(NNSL);
12355   C->setNameInfo(DNI);
12356 
12357   unsigned NumVars = C->varlist_size();
12358   SmallVector<Expr *, 16> Vars;
12359   Vars.reserve(NumVars);
12360   for (unsigned I = 0; I != NumVars; ++I)
12361     Vars.push_back(Record.readSubExpr());
12362   C->setVarRefs(Vars);
12363   Vars.clear();
12364   for (unsigned I = 0; I != NumVars; ++I)
12365     Vars.push_back(Record.readSubExpr());
12366   C->setPrivates(Vars);
12367   Vars.clear();
12368   for (unsigned I = 0; I != NumVars; ++I)
12369     Vars.push_back(Record.readSubExpr());
12370   C->setLHSExprs(Vars);
12371   Vars.clear();
12372   for (unsigned I = 0; I != NumVars; ++I)
12373     Vars.push_back(Record.readSubExpr());
12374   C->setRHSExprs(Vars);
12375   Vars.clear();
12376   for (unsigned I = 0; I != NumVars; ++I)
12377     Vars.push_back(Record.readSubExpr());
12378   C->setReductionOps(Vars);
12379   Vars.clear();
12380   for (unsigned I = 0; I != NumVars; ++I)
12381     Vars.push_back(Record.readSubExpr());
12382   C->setTaskgroupDescriptors(Vars);
12383 }
12384 
12385 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12386   VisitOMPClauseWithPostUpdate(C);
12387   C->setLParenLoc(Record.readSourceLocation());
12388   C->setColonLoc(Record.readSourceLocation());
12389   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12390   C->setModifierLoc(Record.readSourceLocation());
12391   unsigned NumVars = C->varlist_size();
12392   SmallVector<Expr *, 16> Vars;
12393   Vars.reserve(NumVars);
12394   for (unsigned i = 0; i != NumVars; ++i)
12395     Vars.push_back(Record.readSubExpr());
12396   C->setVarRefs(Vars);
12397   Vars.clear();
12398   for (unsigned i = 0; i != NumVars; ++i)
12399     Vars.push_back(Record.readSubExpr());
12400   C->setPrivates(Vars);
12401   Vars.clear();
12402   for (unsigned i = 0; i != NumVars; ++i)
12403     Vars.push_back(Record.readSubExpr());
12404   C->setInits(Vars);
12405   Vars.clear();
12406   for (unsigned i = 0; i != NumVars; ++i)
12407     Vars.push_back(Record.readSubExpr());
12408   C->setUpdates(Vars);
12409   Vars.clear();
12410   for (unsigned i = 0; i != NumVars; ++i)
12411     Vars.push_back(Record.readSubExpr());
12412   C->setFinals(Vars);
12413   C->setStep(Record.readSubExpr());
12414   C->setCalcStep(Record.readSubExpr());
12415   Vars.clear();
12416   for (unsigned I = 0; I != NumVars + 1; ++I)
12417     Vars.push_back(Record.readSubExpr());
12418   C->setUsedExprs(Vars);
12419 }
12420 
12421 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12422   C->setLParenLoc(Record.readSourceLocation());
12423   C->setColonLoc(Record.readSourceLocation());
12424   unsigned NumVars = C->varlist_size();
12425   SmallVector<Expr *, 16> Vars;
12426   Vars.reserve(NumVars);
12427   for (unsigned i = 0; i != NumVars; ++i)
12428     Vars.push_back(Record.readSubExpr());
12429   C->setVarRefs(Vars);
12430   C->setAlignment(Record.readSubExpr());
12431 }
12432 
12433 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12434   C->setLParenLoc(Record.readSourceLocation());
12435   unsigned NumVars = C->varlist_size();
12436   SmallVector<Expr *, 16> Exprs;
12437   Exprs.reserve(NumVars);
12438   for (unsigned i = 0; i != NumVars; ++i)
12439     Exprs.push_back(Record.readSubExpr());
12440   C->setVarRefs(Exprs);
12441   Exprs.clear();
12442   for (unsigned i = 0; i != NumVars; ++i)
12443     Exprs.push_back(Record.readSubExpr());
12444   C->setSourceExprs(Exprs);
12445   Exprs.clear();
12446   for (unsigned i = 0; i != NumVars; ++i)
12447     Exprs.push_back(Record.readSubExpr());
12448   C->setDestinationExprs(Exprs);
12449   Exprs.clear();
12450   for (unsigned i = 0; i != NumVars; ++i)
12451     Exprs.push_back(Record.readSubExpr());
12452   C->setAssignmentOps(Exprs);
12453 }
12454 
12455 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12456   C->setLParenLoc(Record.readSourceLocation());
12457   unsigned NumVars = C->varlist_size();
12458   SmallVector<Expr *, 16> Exprs;
12459   Exprs.reserve(NumVars);
12460   for (unsigned i = 0; i != NumVars; ++i)
12461     Exprs.push_back(Record.readSubExpr());
12462   C->setVarRefs(Exprs);
12463   Exprs.clear();
12464   for (unsigned i = 0; i != NumVars; ++i)
12465     Exprs.push_back(Record.readSubExpr());
12466   C->setSourceExprs(Exprs);
12467   Exprs.clear();
12468   for (unsigned i = 0; i != NumVars; ++i)
12469     Exprs.push_back(Record.readSubExpr());
12470   C->setDestinationExprs(Exprs);
12471   Exprs.clear();
12472   for (unsigned i = 0; i != NumVars; ++i)
12473     Exprs.push_back(Record.readSubExpr());
12474   C->setAssignmentOps(Exprs);
12475 }
12476 
12477 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12478   C->setLParenLoc(Record.readSourceLocation());
12479   unsigned NumVars = C->varlist_size();
12480   SmallVector<Expr *, 16> Vars;
12481   Vars.reserve(NumVars);
12482   for (unsigned i = 0; i != NumVars; ++i)
12483     Vars.push_back(Record.readSubExpr());
12484   C->setVarRefs(Vars);
12485 }
12486 
12487 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12488   C->setDepobj(Record.readSubExpr());
12489   C->setLParenLoc(Record.readSourceLocation());
12490 }
12491 
12492 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12493   C->setLParenLoc(Record.readSourceLocation());
12494   C->setModifier(Record.readSubExpr());
12495   C->setDependencyKind(
12496       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12497   C->setDependencyLoc(Record.readSourceLocation());
12498   C->setColonLoc(Record.readSourceLocation());
12499   unsigned NumVars = C->varlist_size();
12500   SmallVector<Expr *, 16> Vars;
12501   Vars.reserve(NumVars);
12502   for (unsigned I = 0; I != NumVars; ++I)
12503     Vars.push_back(Record.readSubExpr());
12504   C->setVarRefs(Vars);
12505   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12506     C->setLoopData(I, Record.readSubExpr());
12507 }
12508 
12509 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12510   VisitOMPClauseWithPreInit(C);
12511   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12512   C->setDevice(Record.readSubExpr());
12513   C->setModifierLoc(Record.readSourceLocation());
12514   C->setLParenLoc(Record.readSourceLocation());
12515 }
12516 
12517 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12518   C->setLParenLoc(Record.readSourceLocation());
12519   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12520     C->setMapTypeModifier(
12521         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12522     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12523   }
12524   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12525   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12526   C->setMapType(
12527      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12528   C->setMapLoc(Record.readSourceLocation());
12529   C->setColonLoc(Record.readSourceLocation());
12530   auto NumVars = C->varlist_size();
12531   auto UniqueDecls = C->getUniqueDeclarationsNum();
12532   auto TotalLists = C->getTotalComponentListNum();
12533   auto TotalComponents = C->getTotalComponentsNum();
12534 
12535   SmallVector<Expr *, 16> Vars;
12536   Vars.reserve(NumVars);
12537   for (unsigned i = 0; i != NumVars; ++i)
12538     Vars.push_back(Record.readExpr());
12539   C->setVarRefs(Vars);
12540 
12541   SmallVector<Expr *, 16> UDMappers;
12542   UDMappers.reserve(NumVars);
12543   for (unsigned I = 0; I < NumVars; ++I)
12544     UDMappers.push_back(Record.readExpr());
12545   C->setUDMapperRefs(UDMappers);
12546 
12547   SmallVector<ValueDecl *, 16> Decls;
12548   Decls.reserve(UniqueDecls);
12549   for (unsigned i = 0; i < UniqueDecls; ++i)
12550     Decls.push_back(Record.readDeclAs<ValueDecl>());
12551   C->setUniqueDecls(Decls);
12552 
12553   SmallVector<unsigned, 16> ListsPerDecl;
12554   ListsPerDecl.reserve(UniqueDecls);
12555   for (unsigned i = 0; i < UniqueDecls; ++i)
12556     ListsPerDecl.push_back(Record.readInt());
12557   C->setDeclNumLists(ListsPerDecl);
12558 
12559   SmallVector<unsigned, 32> ListSizes;
12560   ListSizes.reserve(TotalLists);
12561   for (unsigned i = 0; i < TotalLists; ++i)
12562     ListSizes.push_back(Record.readInt());
12563   C->setComponentListSizes(ListSizes);
12564 
12565   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12566   Components.reserve(TotalComponents);
12567   for (unsigned i = 0; i < TotalComponents; ++i) {
12568     Expr *AssociatedExprPr = Record.readExpr();
12569     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12570     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12571                             /*IsNonContiguous=*/false);
12572   }
12573   C->setComponents(Components, ListSizes);
12574 }
12575 
12576 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12577   C->setLParenLoc(Record.readSourceLocation());
12578   C->setColonLoc(Record.readSourceLocation());
12579   C->setAllocator(Record.readSubExpr());
12580   unsigned NumVars = C->varlist_size();
12581   SmallVector<Expr *, 16> Vars;
12582   Vars.reserve(NumVars);
12583   for (unsigned i = 0; i != NumVars; ++i)
12584     Vars.push_back(Record.readSubExpr());
12585   C->setVarRefs(Vars);
12586 }
12587 
12588 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12589   VisitOMPClauseWithPreInit(C);
12590   C->setNumTeams(Record.readSubExpr());
12591   C->setLParenLoc(Record.readSourceLocation());
12592 }
12593 
12594 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12595   VisitOMPClauseWithPreInit(C);
12596   C->setThreadLimit(Record.readSubExpr());
12597   C->setLParenLoc(Record.readSourceLocation());
12598 }
12599 
12600 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12601   VisitOMPClauseWithPreInit(C);
12602   C->setPriority(Record.readSubExpr());
12603   C->setLParenLoc(Record.readSourceLocation());
12604 }
12605 
12606 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12607   VisitOMPClauseWithPreInit(C);
12608   C->setGrainsize(Record.readSubExpr());
12609   C->setLParenLoc(Record.readSourceLocation());
12610 }
12611 
12612 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12613   VisitOMPClauseWithPreInit(C);
12614   C->setNumTasks(Record.readSubExpr());
12615   C->setLParenLoc(Record.readSourceLocation());
12616 }
12617 
12618 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12619   C->setHint(Record.readSubExpr());
12620   C->setLParenLoc(Record.readSourceLocation());
12621 }
12622 
12623 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12624   VisitOMPClauseWithPreInit(C);
12625   C->setDistScheduleKind(
12626       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12627   C->setChunkSize(Record.readSubExpr());
12628   C->setLParenLoc(Record.readSourceLocation());
12629   C->setDistScheduleKindLoc(Record.readSourceLocation());
12630   C->setCommaLoc(Record.readSourceLocation());
12631 }
12632 
12633 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12634   C->setDefaultmapKind(
12635        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12636   C->setDefaultmapModifier(
12637       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12638   C->setLParenLoc(Record.readSourceLocation());
12639   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12640   C->setDefaultmapKindLoc(Record.readSourceLocation());
12641 }
12642 
12643 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12644   C->setLParenLoc(Record.readSourceLocation());
12645   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12646     C->setMotionModifier(
12647         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12648     C->setMotionModifierLoc(I, Record.readSourceLocation());
12649   }
12650   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12651   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12652   C->setColonLoc(Record.readSourceLocation());
12653   auto NumVars = C->varlist_size();
12654   auto UniqueDecls = C->getUniqueDeclarationsNum();
12655   auto TotalLists = C->getTotalComponentListNum();
12656   auto TotalComponents = C->getTotalComponentsNum();
12657 
12658   SmallVector<Expr *, 16> Vars;
12659   Vars.reserve(NumVars);
12660   for (unsigned i = 0; i != NumVars; ++i)
12661     Vars.push_back(Record.readSubExpr());
12662   C->setVarRefs(Vars);
12663 
12664   SmallVector<Expr *, 16> UDMappers;
12665   UDMappers.reserve(NumVars);
12666   for (unsigned I = 0; I < NumVars; ++I)
12667     UDMappers.push_back(Record.readSubExpr());
12668   C->setUDMapperRefs(UDMappers);
12669 
12670   SmallVector<ValueDecl *, 16> Decls;
12671   Decls.reserve(UniqueDecls);
12672   for (unsigned i = 0; i < UniqueDecls; ++i)
12673     Decls.push_back(Record.readDeclAs<ValueDecl>());
12674   C->setUniqueDecls(Decls);
12675 
12676   SmallVector<unsigned, 16> ListsPerDecl;
12677   ListsPerDecl.reserve(UniqueDecls);
12678   for (unsigned i = 0; i < UniqueDecls; ++i)
12679     ListsPerDecl.push_back(Record.readInt());
12680   C->setDeclNumLists(ListsPerDecl);
12681 
12682   SmallVector<unsigned, 32> ListSizes;
12683   ListSizes.reserve(TotalLists);
12684   for (unsigned i = 0; i < TotalLists; ++i)
12685     ListSizes.push_back(Record.readInt());
12686   C->setComponentListSizes(ListSizes);
12687 
12688   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12689   Components.reserve(TotalComponents);
12690   for (unsigned i = 0; i < TotalComponents; ++i) {
12691     Expr *AssociatedExprPr = Record.readSubExpr();
12692     bool IsNonContiguous = Record.readBool();
12693     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12694     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12695   }
12696   C->setComponents(Components, ListSizes);
12697 }
12698 
12699 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12700   C->setLParenLoc(Record.readSourceLocation());
12701   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12702     C->setMotionModifier(
12703         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12704     C->setMotionModifierLoc(I, Record.readSourceLocation());
12705   }
12706   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12707   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12708   C->setColonLoc(Record.readSourceLocation());
12709   auto NumVars = C->varlist_size();
12710   auto UniqueDecls = C->getUniqueDeclarationsNum();
12711   auto TotalLists = C->getTotalComponentListNum();
12712   auto TotalComponents = C->getTotalComponentsNum();
12713 
12714   SmallVector<Expr *, 16> Vars;
12715   Vars.reserve(NumVars);
12716   for (unsigned i = 0; i != NumVars; ++i)
12717     Vars.push_back(Record.readSubExpr());
12718   C->setVarRefs(Vars);
12719 
12720   SmallVector<Expr *, 16> UDMappers;
12721   UDMappers.reserve(NumVars);
12722   for (unsigned I = 0; I < NumVars; ++I)
12723     UDMappers.push_back(Record.readSubExpr());
12724   C->setUDMapperRefs(UDMappers);
12725 
12726   SmallVector<ValueDecl *, 16> Decls;
12727   Decls.reserve(UniqueDecls);
12728   for (unsigned i = 0; i < UniqueDecls; ++i)
12729     Decls.push_back(Record.readDeclAs<ValueDecl>());
12730   C->setUniqueDecls(Decls);
12731 
12732   SmallVector<unsigned, 16> ListsPerDecl;
12733   ListsPerDecl.reserve(UniqueDecls);
12734   for (unsigned i = 0; i < UniqueDecls; ++i)
12735     ListsPerDecl.push_back(Record.readInt());
12736   C->setDeclNumLists(ListsPerDecl);
12737 
12738   SmallVector<unsigned, 32> ListSizes;
12739   ListSizes.reserve(TotalLists);
12740   for (unsigned i = 0; i < TotalLists; ++i)
12741     ListSizes.push_back(Record.readInt());
12742   C->setComponentListSizes(ListSizes);
12743 
12744   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12745   Components.reserve(TotalComponents);
12746   for (unsigned i = 0; i < TotalComponents; ++i) {
12747     Expr *AssociatedExprPr = Record.readSubExpr();
12748     bool IsNonContiguous = Record.readBool();
12749     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12750     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12751   }
12752   C->setComponents(Components, ListSizes);
12753 }
12754 
12755 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12756   C->setLParenLoc(Record.readSourceLocation());
12757   auto NumVars = C->varlist_size();
12758   auto UniqueDecls = C->getUniqueDeclarationsNum();
12759   auto TotalLists = C->getTotalComponentListNum();
12760   auto TotalComponents = C->getTotalComponentsNum();
12761 
12762   SmallVector<Expr *, 16> Vars;
12763   Vars.reserve(NumVars);
12764   for (unsigned i = 0; i != NumVars; ++i)
12765     Vars.push_back(Record.readSubExpr());
12766   C->setVarRefs(Vars);
12767   Vars.clear();
12768   for (unsigned i = 0; i != NumVars; ++i)
12769     Vars.push_back(Record.readSubExpr());
12770   C->setPrivateCopies(Vars);
12771   Vars.clear();
12772   for (unsigned i = 0; i != NumVars; ++i)
12773     Vars.push_back(Record.readSubExpr());
12774   C->setInits(Vars);
12775 
12776   SmallVector<ValueDecl *, 16> Decls;
12777   Decls.reserve(UniqueDecls);
12778   for (unsigned i = 0; i < UniqueDecls; ++i)
12779     Decls.push_back(Record.readDeclAs<ValueDecl>());
12780   C->setUniqueDecls(Decls);
12781 
12782   SmallVector<unsigned, 16> ListsPerDecl;
12783   ListsPerDecl.reserve(UniqueDecls);
12784   for (unsigned i = 0; i < UniqueDecls; ++i)
12785     ListsPerDecl.push_back(Record.readInt());
12786   C->setDeclNumLists(ListsPerDecl);
12787 
12788   SmallVector<unsigned, 32> ListSizes;
12789   ListSizes.reserve(TotalLists);
12790   for (unsigned i = 0; i < TotalLists; ++i)
12791     ListSizes.push_back(Record.readInt());
12792   C->setComponentListSizes(ListSizes);
12793 
12794   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12795   Components.reserve(TotalComponents);
12796   for (unsigned i = 0; i < TotalComponents; ++i) {
12797     auto *AssociatedExprPr = Record.readSubExpr();
12798     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12799     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12800                             /*IsNonContiguous=*/false);
12801   }
12802   C->setComponents(Components, ListSizes);
12803 }
12804 
12805 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12806   C->setLParenLoc(Record.readSourceLocation());
12807   auto NumVars = C->varlist_size();
12808   auto UniqueDecls = C->getUniqueDeclarationsNum();
12809   auto TotalLists = C->getTotalComponentListNum();
12810   auto TotalComponents = C->getTotalComponentsNum();
12811 
12812   SmallVector<Expr *, 16> Vars;
12813   Vars.reserve(NumVars);
12814   for (unsigned i = 0; i != NumVars; ++i)
12815     Vars.push_back(Record.readSubExpr());
12816   C->setVarRefs(Vars);
12817 
12818   SmallVector<ValueDecl *, 16> Decls;
12819   Decls.reserve(UniqueDecls);
12820   for (unsigned i = 0; i < UniqueDecls; ++i)
12821     Decls.push_back(Record.readDeclAs<ValueDecl>());
12822   C->setUniqueDecls(Decls);
12823 
12824   SmallVector<unsigned, 16> ListsPerDecl;
12825   ListsPerDecl.reserve(UniqueDecls);
12826   for (unsigned i = 0; i < UniqueDecls; ++i)
12827     ListsPerDecl.push_back(Record.readInt());
12828   C->setDeclNumLists(ListsPerDecl);
12829 
12830   SmallVector<unsigned, 32> ListSizes;
12831   ListSizes.reserve(TotalLists);
12832   for (unsigned i = 0; i < TotalLists; ++i)
12833     ListSizes.push_back(Record.readInt());
12834   C->setComponentListSizes(ListSizes);
12835 
12836   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12837   Components.reserve(TotalComponents);
12838   for (unsigned i = 0; i < TotalComponents; ++i) {
12839     Expr *AssociatedExpr = Record.readSubExpr();
12840     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12841     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12842                             /*IsNonContiguous*/ false);
12843   }
12844   C->setComponents(Components, ListSizes);
12845 }
12846 
12847 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12848   C->setLParenLoc(Record.readSourceLocation());
12849   auto NumVars = C->varlist_size();
12850   auto UniqueDecls = C->getUniqueDeclarationsNum();
12851   auto TotalLists = C->getTotalComponentListNum();
12852   auto TotalComponents = C->getTotalComponentsNum();
12853 
12854   SmallVector<Expr *, 16> Vars;
12855   Vars.reserve(NumVars);
12856   for (unsigned i = 0; i != NumVars; ++i)
12857     Vars.push_back(Record.readSubExpr());
12858   C->setVarRefs(Vars);
12859   Vars.clear();
12860 
12861   SmallVector<ValueDecl *, 16> Decls;
12862   Decls.reserve(UniqueDecls);
12863   for (unsigned i = 0; i < UniqueDecls; ++i)
12864     Decls.push_back(Record.readDeclAs<ValueDecl>());
12865   C->setUniqueDecls(Decls);
12866 
12867   SmallVector<unsigned, 16> ListsPerDecl;
12868   ListsPerDecl.reserve(UniqueDecls);
12869   for (unsigned i = 0; i < UniqueDecls; ++i)
12870     ListsPerDecl.push_back(Record.readInt());
12871   C->setDeclNumLists(ListsPerDecl);
12872 
12873   SmallVector<unsigned, 32> ListSizes;
12874   ListSizes.reserve(TotalLists);
12875   for (unsigned i = 0; i < TotalLists; ++i)
12876     ListSizes.push_back(Record.readInt());
12877   C->setComponentListSizes(ListSizes);
12878 
12879   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12880   Components.reserve(TotalComponents);
12881   for (unsigned i = 0; i < TotalComponents; ++i) {
12882     Expr *AssociatedExpr = Record.readSubExpr();
12883     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12884     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12885                             /*IsNonContiguous=*/false);
12886   }
12887   C->setComponents(Components, ListSizes);
12888 }
12889 
12890 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12891   C->setLParenLoc(Record.readSourceLocation());
12892   unsigned NumVars = C->varlist_size();
12893   SmallVector<Expr *, 16> Vars;
12894   Vars.reserve(NumVars);
12895   for (unsigned i = 0; i != NumVars; ++i)
12896     Vars.push_back(Record.readSubExpr());
12897   C->setVarRefs(Vars);
12898   Vars.clear();
12899   Vars.reserve(NumVars);
12900   for (unsigned i = 0; i != NumVars; ++i)
12901     Vars.push_back(Record.readSubExpr());
12902   C->setPrivateRefs(Vars);
12903 }
12904 
12905 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12906   C->setLParenLoc(Record.readSourceLocation());
12907   unsigned NumVars = C->varlist_size();
12908   SmallVector<Expr *, 16> Vars;
12909   Vars.reserve(NumVars);
12910   for (unsigned i = 0; i != NumVars; ++i)
12911     Vars.push_back(Record.readSubExpr());
12912   C->setVarRefs(Vars);
12913 }
12914 
12915 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12916   C->setLParenLoc(Record.readSourceLocation());
12917   unsigned NumVars = C->varlist_size();
12918   SmallVector<Expr *, 16> Vars;
12919   Vars.reserve(NumVars);
12920   for (unsigned i = 0; i != NumVars; ++i)
12921     Vars.push_back(Record.readSubExpr());
12922   C->setVarRefs(Vars);
12923 }
12924 
12925 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12926   C->setLParenLoc(Record.readSourceLocation());
12927   unsigned NumOfAllocators = C->getNumberOfAllocators();
12928   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12929   Data.reserve(NumOfAllocators);
12930   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12931     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12932     D.Allocator = Record.readSubExpr();
12933     D.AllocatorTraits = Record.readSubExpr();
12934     D.LParenLoc = Record.readSourceLocation();
12935     D.RParenLoc = Record.readSourceLocation();
12936   }
12937   C->setAllocatorsData(Data);
12938 }
12939 
12940 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12941   C->setLParenLoc(Record.readSourceLocation());
12942   C->setModifier(Record.readSubExpr());
12943   C->setColonLoc(Record.readSourceLocation());
12944   unsigned NumOfLocators = C->varlist_size();
12945   SmallVector<Expr *, 4> Locators;
12946   Locators.reserve(NumOfLocators);
12947   for (unsigned I = 0; I != NumOfLocators; ++I)
12948     Locators.push_back(Record.readSubExpr());
12949   C->setVarRefs(Locators);
12950 }
12951 
12952 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12953   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12954   C->setLParenLoc(Record.readSourceLocation());
12955   C->setKindKwLoc(Record.readSourceLocation());
12956 }
12957 
12958 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12959   VisitOMPClauseWithPreInit(C);
12960   C->setThreadID(Record.readSubExpr());
12961   C->setLParenLoc(Record.readSourceLocation());
12962 }
12963 
12964 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12965   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12966   TI.Sets.resize(readUInt32());
12967   for (auto &Set : TI.Sets) {
12968     Set.Kind = readEnum<llvm::omp::TraitSet>();
12969     Set.Selectors.resize(readUInt32());
12970     for (auto &Selector : Set.Selectors) {
12971       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12972       Selector.ScoreOrCondition = nullptr;
12973       if (readBool())
12974         Selector.ScoreOrCondition = readExprRef();
12975       Selector.Properties.resize(readUInt32());
12976       for (auto &Property : Selector.Properties)
12977         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12978     }
12979   }
12980   return &TI;
12981 }
12982 
12983 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12984   if (!Data)
12985     return;
12986   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12987     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12988     skipInts(3);
12989   }
12990   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12991   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12992     Clauses[I] = readOMPClause();
12993   Data->setClauses(Clauses);
12994   if (Data->hasAssociatedStmt())
12995     Data->setAssociatedStmt(readStmt());
12996   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12997     Data->getChildren()[I] = readStmt();
12998 }
12999