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   SourceLocation::UIntTy 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   struct Change {
2384     enum ModificationKind {
2385       Size,
2386       ModTime,
2387       Content,
2388       None,
2389     } Kind;
2390     llvm::Optional<int64_t> Old = llvm::None;
2391     llvm::Optional<int64_t> New = llvm::None;
2392   };
2393   auto HasInputFileChanged = [&]() {
2394     if (StoredSize != File->getSize())
2395       return Change{Change::Size, StoredSize, File->getSize()};
2396     if (!shouldDisableValidationForFile(F) && StoredTime &&
2397         StoredTime != File->getModificationTime()) {
2398       Change MTimeChange = {Change::ModTime, StoredTime,
2399                             File->getModificationTime()};
2400 
2401       // In case the modification time changes but not the content,
2402       // accept the cached file as legit.
2403       if (ValidateASTInputFilesContent &&
2404           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2405         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2406         if (!MemBuffOrError) {
2407           if (!Complain)
2408             return MTimeChange;
2409           std::string ErrorStr = "could not get buffer for file '";
2410           ErrorStr += File->getName();
2411           ErrorStr += "'";
2412           Error(ErrorStr);
2413           return MTimeChange;
2414         }
2415 
2416         // FIXME: hash_value is not guaranteed to be stable!
2417         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2418         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2419           return Change{Change::None};
2420 
2421         return Change{Change::Content};
2422       }
2423       return MTimeChange;
2424     }
2425     return Change{Change::None};
2426   };
2427 
2428   bool IsOutOfDate = false;
2429   auto FileChange = HasInputFileChanged();
2430   // For an overridden file, there is nothing to validate.
2431   if (!Overridden && FileChange.Kind != Change::None) {
2432     if (Complain && !Diags.isDiagnosticInFlight()) {
2433       // Build a list of the PCH imports that got us here (in reverse).
2434       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2435       while (!ImportStack.back()->ImportedBy.empty())
2436         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2437 
2438       // The top-level PCH is stale.
2439       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2440       Diag(diag::err_fe_ast_file_modified)
2441           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2442           << TopLevelPCHName << FileChange.Kind
2443           << (FileChange.Old && FileChange.New)
2444           << llvm::itostr(FileChange.Old.getValueOr(0))
2445           << llvm::itostr(FileChange.New.getValueOr(0));
2446 
2447       // Print the import stack.
2448       if (ImportStack.size() > 1) {
2449         Diag(diag::note_pch_required_by)
2450           << Filename << ImportStack[0]->FileName;
2451         for (unsigned I = 1; I < ImportStack.size(); ++I)
2452           Diag(diag::note_pch_required_by)
2453             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2454       }
2455 
2456       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2457     }
2458 
2459     IsOutOfDate = true;
2460   }
2461   // FIXME: If the file is overridden and we've already opened it,
2462   // issue an error (or split it into a separate FileEntry).
2463 
2464   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2465 
2466   // Note that we've loaded this input file.
2467   F.InputFilesLoaded[ID-1] = IF;
2468   return IF;
2469 }
2470 
2471 /// If we are loading a relocatable PCH or module file, and the filename
2472 /// is not an absolute path, add the system or module root to the beginning of
2473 /// the file name.
2474 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2475   // Resolve relative to the base directory, if we have one.
2476   if (!M.BaseDirectory.empty())
2477     return ResolveImportedPath(Filename, M.BaseDirectory);
2478 }
2479 
2480 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2481   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2482     return;
2483 
2484   SmallString<128> Buffer;
2485   llvm::sys::path::append(Buffer, Prefix, Filename);
2486   Filename.assign(Buffer.begin(), Buffer.end());
2487 }
2488 
2489 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2490   switch (ARR) {
2491   case ASTReader::Failure: return true;
2492   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2493   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2494   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2495   case ASTReader::ConfigurationMismatch:
2496     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2497   case ASTReader::HadErrors: return true;
2498   case ASTReader::Success: return false;
2499   }
2500 
2501   llvm_unreachable("unknown ASTReadResult");
2502 }
2503 
2504 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2505     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2506     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2507     std::string &SuggestedPredefines) {
2508   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2509     // FIXME this drops errors on the floor.
2510     consumeError(std::move(Err));
2511     return Failure;
2512   }
2513 
2514   // Read all of the records in the options block.
2515   RecordData Record;
2516   ASTReadResult Result = Success;
2517   while (true) {
2518     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2519     if (!MaybeEntry) {
2520       // FIXME this drops errors on the floor.
2521       consumeError(MaybeEntry.takeError());
2522       return Failure;
2523     }
2524     llvm::BitstreamEntry Entry = MaybeEntry.get();
2525 
2526     switch (Entry.Kind) {
2527     case llvm::BitstreamEntry::Error:
2528     case llvm::BitstreamEntry::SubBlock:
2529       return Failure;
2530 
2531     case llvm::BitstreamEntry::EndBlock:
2532       return Result;
2533 
2534     case llvm::BitstreamEntry::Record:
2535       // The interesting case.
2536       break;
2537     }
2538 
2539     // Read and process a record.
2540     Record.clear();
2541     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2542     if (!MaybeRecordType) {
2543       // FIXME this drops errors on the floor.
2544       consumeError(MaybeRecordType.takeError());
2545       return Failure;
2546     }
2547     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2548     case LANGUAGE_OPTIONS: {
2549       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2550       if (ParseLanguageOptions(Record, Complain, Listener,
2551                                AllowCompatibleConfigurationMismatch))
2552         Result = ConfigurationMismatch;
2553       break;
2554     }
2555 
2556     case TARGET_OPTIONS: {
2557       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2558       if (ParseTargetOptions(Record, Complain, Listener,
2559                              AllowCompatibleConfigurationMismatch))
2560         Result = ConfigurationMismatch;
2561       break;
2562     }
2563 
2564     case FILE_SYSTEM_OPTIONS: {
2565       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2566       if (!AllowCompatibleConfigurationMismatch &&
2567           ParseFileSystemOptions(Record, Complain, Listener))
2568         Result = ConfigurationMismatch;
2569       break;
2570     }
2571 
2572     case HEADER_SEARCH_OPTIONS: {
2573       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2574       if (!AllowCompatibleConfigurationMismatch &&
2575           ParseHeaderSearchOptions(Record, Complain, Listener))
2576         Result = ConfigurationMismatch;
2577       break;
2578     }
2579 
2580     case PREPROCESSOR_OPTIONS:
2581       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2582       if (!AllowCompatibleConfigurationMismatch &&
2583           ParsePreprocessorOptions(Record, Complain, Listener,
2584                                    SuggestedPredefines))
2585         Result = ConfigurationMismatch;
2586       break;
2587     }
2588   }
2589 }
2590 
2591 ASTReader::ASTReadResult
2592 ASTReader::ReadControlBlock(ModuleFile &F,
2593                             SmallVectorImpl<ImportedModule> &Loaded,
2594                             const ModuleFile *ImportedBy,
2595                             unsigned ClientLoadCapabilities) {
2596   BitstreamCursor &Stream = F.Stream;
2597 
2598   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2599     Error(std::move(Err));
2600     return Failure;
2601   }
2602 
2603   // Lambda to read the unhashed control block the first time it's called.
2604   //
2605   // For PCM files, the unhashed control block cannot be read until after the
2606   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2607   // need to look ahead before reading the IMPORTS record.  For consistency,
2608   // this block is always read somehow (see BitstreamEntry::EndBlock).
2609   bool HasReadUnhashedControlBlock = false;
2610   auto readUnhashedControlBlockOnce = [&]() {
2611     if (!HasReadUnhashedControlBlock) {
2612       HasReadUnhashedControlBlock = true;
2613       if (ASTReadResult Result =
2614               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2615         return Result;
2616     }
2617     return Success;
2618   };
2619 
2620   bool DisableValidation = shouldDisableValidationForFile(F);
2621 
2622   // Read all of the records and blocks in the control block.
2623   RecordData Record;
2624   unsigned NumInputs = 0;
2625   unsigned NumUserInputs = 0;
2626   StringRef BaseDirectoryAsWritten;
2627   while (true) {
2628     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2629     if (!MaybeEntry) {
2630       Error(MaybeEntry.takeError());
2631       return Failure;
2632     }
2633     llvm::BitstreamEntry Entry = MaybeEntry.get();
2634 
2635     switch (Entry.Kind) {
2636     case llvm::BitstreamEntry::Error:
2637       Error("malformed block record in AST file");
2638       return Failure;
2639     case llvm::BitstreamEntry::EndBlock: {
2640       // Validate the module before returning.  This call catches an AST with
2641       // no module name and no imports.
2642       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2643         return Result;
2644 
2645       // Validate input files.
2646       const HeaderSearchOptions &HSOpts =
2647           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2648 
2649       // All user input files reside at the index range [0, NumUserInputs), and
2650       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2651       // loaded module files, ignore missing inputs.
2652       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2653           F.Kind != MK_PrebuiltModule) {
2654         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2655 
2656         // If we are reading a module, we will create a verification timestamp,
2657         // so we verify all input files.  Otherwise, verify only user input
2658         // files.
2659 
2660         unsigned N = NumUserInputs;
2661         if (ValidateSystemInputs ||
2662             (HSOpts.ModulesValidateOncePerBuildSession &&
2663              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2664              F.Kind == MK_ImplicitModule))
2665           N = NumInputs;
2666 
2667         for (unsigned I = 0; I < N; ++I) {
2668           InputFile IF = getInputFile(F, I+1, Complain);
2669           if (!IF.getFile() || IF.isOutOfDate())
2670             return OutOfDate;
2671         }
2672       }
2673 
2674       if (Listener)
2675         Listener->visitModuleFile(F.FileName, F.Kind);
2676 
2677       if (Listener && Listener->needsInputFileVisitation()) {
2678         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2679                                                                 : NumUserInputs;
2680         for (unsigned I = 0; I < N; ++I) {
2681           bool IsSystem = I >= NumUserInputs;
2682           InputFileInfo FI = readInputFileInfo(F, I+1);
2683           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2684                                    F.Kind == MK_ExplicitModule ||
2685                                    F.Kind == MK_PrebuiltModule);
2686         }
2687       }
2688 
2689       return Success;
2690     }
2691 
2692     case llvm::BitstreamEntry::SubBlock:
2693       switch (Entry.ID) {
2694       case INPUT_FILES_BLOCK_ID:
2695         F.InputFilesCursor = Stream;
2696         if (llvm::Error Err = Stream.SkipBlock()) {
2697           Error(std::move(Err));
2698           return Failure;
2699         }
2700         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2701           Error("malformed block record in AST file");
2702           return Failure;
2703         }
2704         continue;
2705 
2706       case OPTIONS_BLOCK_ID:
2707         // If we're reading the first module for this group, check its options
2708         // are compatible with ours. For modules it imports, no further checking
2709         // is required, because we checked them when we built it.
2710         if (Listener && !ImportedBy) {
2711           // Should we allow the configuration of the module file to differ from
2712           // the configuration of the current translation unit in a compatible
2713           // way?
2714           //
2715           // FIXME: Allow this for files explicitly specified with -include-pch.
2716           bool AllowCompatibleConfigurationMismatch =
2717               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2718 
2719           ASTReadResult Result =
2720               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2721                                AllowCompatibleConfigurationMismatch, *Listener,
2722                                SuggestedPredefines);
2723           if (Result == Failure) {
2724             Error("malformed block record in AST file");
2725             return Result;
2726           }
2727 
2728           if (DisableValidation ||
2729               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2730             Result = Success;
2731 
2732           // If we can't load the module, exit early since we likely
2733           // will rebuild the module anyway. The stream may be in the
2734           // middle of a block.
2735           if (Result != Success)
2736             return Result;
2737         } else if (llvm::Error Err = Stream.SkipBlock()) {
2738           Error(std::move(Err));
2739           return Failure;
2740         }
2741         continue;
2742 
2743       default:
2744         if (llvm::Error Err = Stream.SkipBlock()) {
2745           Error(std::move(Err));
2746           return Failure;
2747         }
2748         continue;
2749       }
2750 
2751     case llvm::BitstreamEntry::Record:
2752       // The interesting case.
2753       break;
2754     }
2755 
2756     // Read and process a record.
2757     Record.clear();
2758     StringRef Blob;
2759     Expected<unsigned> MaybeRecordType =
2760         Stream.readRecord(Entry.ID, Record, &Blob);
2761     if (!MaybeRecordType) {
2762       Error(MaybeRecordType.takeError());
2763       return Failure;
2764     }
2765     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2766     case METADATA: {
2767       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2768         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2769           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2770                                         : diag::err_pch_version_too_new);
2771         return VersionMismatch;
2772       }
2773 
2774       bool hasErrors = Record[6];
2775       if (hasErrors && !DisableValidation) {
2776         // If requested by the caller and the module hasn't already been read
2777         // or compiled, mark modules on error as out-of-date.
2778         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2779             canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2780           return OutOfDate;
2781 
2782         if (!AllowASTWithCompilerErrors) {
2783           Diag(diag::err_pch_with_compiler_errors);
2784           return HadErrors;
2785         }
2786       }
2787       if (hasErrors) {
2788         Diags.ErrorOccurred = true;
2789         Diags.UncompilableErrorOccurred = true;
2790         Diags.UnrecoverableErrorOccurred = true;
2791       }
2792 
2793       F.RelocatablePCH = Record[4];
2794       // Relative paths in a relocatable PCH are relative to our sysroot.
2795       if (F.RelocatablePCH)
2796         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2797 
2798       F.HasTimestamps = Record[5];
2799 
2800       const std::string &CurBranch = getClangFullRepositoryVersion();
2801       StringRef ASTBranch = Blob;
2802       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2803         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2804           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2805         return VersionMismatch;
2806       }
2807       break;
2808     }
2809 
2810     case IMPORTS: {
2811       // Validate the AST before processing any imports (otherwise, untangling
2812       // them can be error-prone and expensive).  A module will have a name and
2813       // will already have been validated, but this catches the PCH case.
2814       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2815         return Result;
2816 
2817       // Load each of the imported PCH files.
2818       unsigned Idx = 0, N = Record.size();
2819       while (Idx < N) {
2820         // Read information about the AST file.
2821         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2822         // The import location will be the local one for now; we will adjust
2823         // all import locations of module imports after the global source
2824         // location info are setup, in ReadAST.
2825         SourceLocation ImportLoc =
2826             ReadUntranslatedSourceLocation(Record[Idx++]);
2827         off_t StoredSize = (off_t)Record[Idx++];
2828         time_t StoredModTime = (time_t)Record[Idx++];
2829         auto FirstSignatureByte = Record.begin() + Idx;
2830         ASTFileSignature StoredSignature = ASTFileSignature::create(
2831             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2832         Idx += ASTFileSignature::size;
2833 
2834         std::string ImportedName = ReadString(Record, Idx);
2835         std::string ImportedFile;
2836 
2837         // For prebuilt and explicit modules first consult the file map for
2838         // an override. Note that here we don't search prebuilt module
2839         // directories, only the explicit name to file mappings. Also, we will
2840         // still verify the size/signature making sure it is essentially the
2841         // same file but perhaps in a different location.
2842         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2843           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2844             ImportedName, /*FileMapOnly*/ true);
2845 
2846         if (ImportedFile.empty())
2847           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2848           // ModuleCache as when writing.
2849           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2850         else
2851           SkipPath(Record, Idx);
2852 
2853         // If our client can't cope with us being out of date, we can't cope with
2854         // our dependency being missing.
2855         unsigned Capabilities = ClientLoadCapabilities;
2856         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2857           Capabilities &= ~ARR_Missing;
2858 
2859         // Load the AST file.
2860         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2861                                   Loaded, StoredSize, StoredModTime,
2862                                   StoredSignature, Capabilities);
2863 
2864         // If we diagnosed a problem, produce a backtrace.
2865         bool recompilingFinalized =
2866             Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
2867             getModuleManager().getModuleCache().isPCMFinal(F.FileName);
2868         if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
2869           Diag(diag::note_module_file_imported_by)
2870               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2871         if (recompilingFinalized)
2872           Diag(diag::note_module_file_conflict);
2873 
2874         switch (Result) {
2875         case Failure: return Failure;
2876           // If we have to ignore the dependency, we'll have to ignore this too.
2877         case Missing:
2878         case OutOfDate: return OutOfDate;
2879         case VersionMismatch: return VersionMismatch;
2880         case ConfigurationMismatch: return ConfigurationMismatch;
2881         case HadErrors: return HadErrors;
2882         case Success: break;
2883         }
2884       }
2885       break;
2886     }
2887 
2888     case ORIGINAL_FILE:
2889       F.OriginalSourceFileID = FileID::get(Record[0]);
2890       F.ActualOriginalSourceFileName = std::string(Blob);
2891       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2892       ResolveImportedPath(F, F.OriginalSourceFileName);
2893       break;
2894 
2895     case ORIGINAL_FILE_ID:
2896       F.OriginalSourceFileID = FileID::get(Record[0]);
2897       break;
2898 
2899     case ORIGINAL_PCH_DIR:
2900       F.OriginalDir = std::string(Blob);
2901       break;
2902 
2903     case MODULE_NAME:
2904       F.ModuleName = std::string(Blob);
2905       Diag(diag::remark_module_import)
2906           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2907           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2908       if (Listener)
2909         Listener->ReadModuleName(F.ModuleName);
2910 
2911       // Validate the AST as soon as we have a name so we can exit early on
2912       // failure.
2913       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2914         return Result;
2915 
2916       break;
2917 
2918     case MODULE_DIRECTORY: {
2919       // Save the BaseDirectory as written in the PCM for computing the module
2920       // filename for the ModuleCache.
2921       BaseDirectoryAsWritten = Blob;
2922       assert(!F.ModuleName.empty() &&
2923              "MODULE_DIRECTORY found before MODULE_NAME");
2924       // If we've already loaded a module map file covering this module, we may
2925       // have a better path for it (relative to the current build).
2926       Module *M = PP.getHeaderSearchInfo().lookupModule(
2927           F.ModuleName, /*AllowSearch*/ true,
2928           /*AllowExtraModuleMapSearch*/ true);
2929       if (M && M->Directory) {
2930         // If we're implicitly loading a module, the base directory can't
2931         // change between the build and use.
2932         // Don't emit module relocation error if we have -fno-validate-pch
2933         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2934                   DisableValidationForModuleKind::Module) &&
2935             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2936           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2937           if (!BuildDir || *BuildDir != M->Directory) {
2938             if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2939               Diag(diag::err_imported_module_relocated)
2940                   << F.ModuleName << Blob << M->Directory->getName();
2941             return OutOfDate;
2942           }
2943         }
2944         F.BaseDirectory = std::string(M->Directory->getName());
2945       } else {
2946         F.BaseDirectory = std::string(Blob);
2947       }
2948       break;
2949     }
2950 
2951     case MODULE_MAP_FILE:
2952       if (ASTReadResult Result =
2953               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2954         return Result;
2955       break;
2956 
2957     case INPUT_FILE_OFFSETS:
2958       NumInputs = Record[0];
2959       NumUserInputs = Record[1];
2960       F.InputFileOffsets =
2961           (const llvm::support::unaligned_uint64_t *)Blob.data();
2962       F.InputFilesLoaded.resize(NumInputs);
2963       F.NumUserInputFiles = NumUserInputs;
2964       break;
2965     }
2966   }
2967 }
2968 
2969 ASTReader::ASTReadResult
2970 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2971   BitstreamCursor &Stream = F.Stream;
2972 
2973   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2974     Error(std::move(Err));
2975     return Failure;
2976   }
2977   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2978 
2979   // Read all of the records and blocks for the AST file.
2980   RecordData Record;
2981   while (true) {
2982     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2983     if (!MaybeEntry) {
2984       Error(MaybeEntry.takeError());
2985       return Failure;
2986     }
2987     llvm::BitstreamEntry Entry = MaybeEntry.get();
2988 
2989     switch (Entry.Kind) {
2990     case llvm::BitstreamEntry::Error:
2991       Error("error at end of module block in AST file");
2992       return Failure;
2993     case llvm::BitstreamEntry::EndBlock:
2994       // Outside of C++, we do not store a lookup map for the translation unit.
2995       // Instead, mark it as needing a lookup map to be built if this module
2996       // contains any declarations lexically within it (which it always does!).
2997       // This usually has no cost, since we very rarely need the lookup map for
2998       // the translation unit outside C++.
2999       if (ASTContext *Ctx = ContextObj) {
3000         DeclContext *DC = Ctx->getTranslationUnitDecl();
3001         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
3002           DC->setMustBuildLookupTable();
3003       }
3004 
3005       return Success;
3006     case llvm::BitstreamEntry::SubBlock:
3007       switch (Entry.ID) {
3008       case DECLTYPES_BLOCK_ID:
3009         // We lazily load the decls block, but we want to set up the
3010         // DeclsCursor cursor to point into it.  Clone our current bitcode
3011         // cursor to it, enter the block and read the abbrevs in that block.
3012         // With the main cursor, we just skip over it.
3013         F.DeclsCursor = Stream;
3014         if (llvm::Error Err = Stream.SkipBlock()) {
3015           Error(std::move(Err));
3016           return Failure;
3017         }
3018         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
3019                              &F.DeclsBlockStartOffset)) {
3020           Error("malformed block record in AST file");
3021           return Failure;
3022         }
3023         break;
3024 
3025       case PREPROCESSOR_BLOCK_ID:
3026         F.MacroCursor = Stream;
3027         if (!PP.getExternalSource())
3028           PP.setExternalSource(this);
3029 
3030         if (llvm::Error Err = Stream.SkipBlock()) {
3031           Error(std::move(Err));
3032           return Failure;
3033         }
3034         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
3035           Error("malformed block record in AST file");
3036           return Failure;
3037         }
3038         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3039         break;
3040 
3041       case PREPROCESSOR_DETAIL_BLOCK_ID:
3042         F.PreprocessorDetailCursor = Stream;
3043 
3044         if (llvm::Error Err = Stream.SkipBlock()) {
3045           Error(std::move(Err));
3046           return Failure;
3047         }
3048         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3049                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3050           Error("malformed preprocessor detail record in AST file");
3051           return Failure;
3052         }
3053         F.PreprocessorDetailStartOffset
3054         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3055 
3056         if (!PP.getPreprocessingRecord())
3057           PP.createPreprocessingRecord();
3058         if (!PP.getPreprocessingRecord()->getExternalSource())
3059           PP.getPreprocessingRecord()->SetExternalSource(*this);
3060         break;
3061 
3062       case SOURCE_MANAGER_BLOCK_ID:
3063         if (ReadSourceManagerBlock(F))
3064           return Failure;
3065         break;
3066 
3067       case SUBMODULE_BLOCK_ID:
3068         if (ASTReadResult Result =
3069                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3070           return Result;
3071         break;
3072 
3073       case COMMENTS_BLOCK_ID: {
3074         BitstreamCursor C = Stream;
3075 
3076         if (llvm::Error Err = Stream.SkipBlock()) {
3077           Error(std::move(Err));
3078           return Failure;
3079         }
3080         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3081           Error("malformed comments block in AST file");
3082           return Failure;
3083         }
3084         CommentsCursors.push_back(std::make_pair(C, &F));
3085         break;
3086       }
3087 
3088       default:
3089         if (llvm::Error Err = Stream.SkipBlock()) {
3090           Error(std::move(Err));
3091           return Failure;
3092         }
3093         break;
3094       }
3095       continue;
3096 
3097     case llvm::BitstreamEntry::Record:
3098       // The interesting case.
3099       break;
3100     }
3101 
3102     // Read and process a record.
3103     Record.clear();
3104     StringRef Blob;
3105     Expected<unsigned> MaybeRecordType =
3106         Stream.readRecord(Entry.ID, Record, &Blob);
3107     if (!MaybeRecordType) {
3108       Error(MaybeRecordType.takeError());
3109       return Failure;
3110     }
3111     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3112 
3113     // If we're not loading an AST context, we don't care about most records.
3114     if (!ContextObj) {
3115       switch (RecordType) {
3116       case IDENTIFIER_TABLE:
3117       case IDENTIFIER_OFFSET:
3118       case INTERESTING_IDENTIFIERS:
3119       case STATISTICS:
3120       case PP_CONDITIONAL_STACK:
3121       case PP_COUNTER_VALUE:
3122       case SOURCE_LOCATION_OFFSETS:
3123       case MODULE_OFFSET_MAP:
3124       case SOURCE_MANAGER_LINE_TABLE:
3125       case SOURCE_LOCATION_PRELOADS:
3126       case PPD_ENTITIES_OFFSETS:
3127       case HEADER_SEARCH_TABLE:
3128       case IMPORTED_MODULES:
3129       case MACRO_OFFSET:
3130         break;
3131       default:
3132         continue;
3133       }
3134     }
3135 
3136     switch (RecordType) {
3137     default:  // Default behavior: ignore.
3138       break;
3139 
3140     case TYPE_OFFSET: {
3141       if (F.LocalNumTypes != 0) {
3142         Error("duplicate TYPE_OFFSET record in AST file");
3143         return Failure;
3144       }
3145       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3146       F.LocalNumTypes = Record[0];
3147       unsigned LocalBaseTypeIndex = Record[1];
3148       F.BaseTypeIndex = getTotalNumTypes();
3149 
3150       if (F.LocalNumTypes > 0) {
3151         // Introduce the global -> local mapping for types within this module.
3152         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3153 
3154         // Introduce the local -> global mapping for types within this module.
3155         F.TypeRemap.insertOrReplace(
3156           std::make_pair(LocalBaseTypeIndex,
3157                          F.BaseTypeIndex - LocalBaseTypeIndex));
3158 
3159         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3160       }
3161       break;
3162     }
3163 
3164     case DECL_OFFSET: {
3165       if (F.LocalNumDecls != 0) {
3166         Error("duplicate DECL_OFFSET record in AST file");
3167         return Failure;
3168       }
3169       F.DeclOffsets = (const DeclOffset *)Blob.data();
3170       F.LocalNumDecls = Record[0];
3171       unsigned LocalBaseDeclID = Record[1];
3172       F.BaseDeclID = getTotalNumDecls();
3173 
3174       if (F.LocalNumDecls > 0) {
3175         // Introduce the global -> local mapping for declarations within this
3176         // module.
3177         GlobalDeclMap.insert(
3178           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3179 
3180         // Introduce the local -> global mapping for declarations within this
3181         // module.
3182         F.DeclRemap.insertOrReplace(
3183           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3184 
3185         // Introduce the global -> local mapping for declarations within this
3186         // module.
3187         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3188 
3189         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3190       }
3191       break;
3192     }
3193 
3194     case TU_UPDATE_LEXICAL: {
3195       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3196       LexicalContents Contents(
3197           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3198               Blob.data()),
3199           static_cast<unsigned int>(Blob.size() / 4));
3200       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3201       TU->setHasExternalLexicalStorage(true);
3202       break;
3203     }
3204 
3205     case UPDATE_VISIBLE: {
3206       unsigned Idx = 0;
3207       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3208       auto *Data = (const unsigned char*)Blob.data();
3209       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3210       // If we've already loaded the decl, perform the updates when we finish
3211       // loading this block.
3212       if (Decl *D = GetExistingDecl(ID))
3213         PendingUpdateRecords.push_back(
3214             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3215       break;
3216     }
3217 
3218     case IDENTIFIER_TABLE:
3219       F.IdentifierTableData =
3220           reinterpret_cast<const unsigned char *>(Blob.data());
3221       if (Record[0]) {
3222         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3223             F.IdentifierTableData + Record[0],
3224             F.IdentifierTableData + sizeof(uint32_t),
3225             F.IdentifierTableData,
3226             ASTIdentifierLookupTrait(*this, F));
3227 
3228         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3229       }
3230       break;
3231 
3232     case IDENTIFIER_OFFSET: {
3233       if (F.LocalNumIdentifiers != 0) {
3234         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3235         return Failure;
3236       }
3237       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3238       F.LocalNumIdentifiers = Record[0];
3239       unsigned LocalBaseIdentifierID = Record[1];
3240       F.BaseIdentifierID = getTotalNumIdentifiers();
3241 
3242       if (F.LocalNumIdentifiers > 0) {
3243         // Introduce the global -> local mapping for identifiers within this
3244         // module.
3245         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3246                                                   &F));
3247 
3248         // Introduce the local -> global mapping for identifiers within this
3249         // module.
3250         F.IdentifierRemap.insertOrReplace(
3251           std::make_pair(LocalBaseIdentifierID,
3252                          F.BaseIdentifierID - LocalBaseIdentifierID));
3253 
3254         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3255                                  + F.LocalNumIdentifiers);
3256       }
3257       break;
3258     }
3259 
3260     case INTERESTING_IDENTIFIERS:
3261       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3262       break;
3263 
3264     case EAGERLY_DESERIALIZED_DECLS:
3265       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3266       // about "interesting" decls (for instance, if we're building a module).
3267       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3268         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3269       break;
3270 
3271     case MODULAR_CODEGEN_DECLS:
3272       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3273       // them (ie: if we're not codegenerating this module).
3274       if (F.Kind == MK_MainFile ||
3275           getContext().getLangOpts().BuildingPCHWithObjectFile)
3276         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3277           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3278       break;
3279 
3280     case SPECIAL_TYPES:
3281       if (SpecialTypes.empty()) {
3282         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3283           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3284         break;
3285       }
3286 
3287       if (SpecialTypes.size() != Record.size()) {
3288         Error("invalid special-types record");
3289         return Failure;
3290       }
3291 
3292       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3293         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3294         if (!SpecialTypes[I])
3295           SpecialTypes[I] = ID;
3296         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3297         // merge step?
3298       }
3299       break;
3300 
3301     case STATISTICS:
3302       TotalNumStatements += Record[0];
3303       TotalNumMacros += Record[1];
3304       TotalLexicalDeclContexts += Record[2];
3305       TotalVisibleDeclContexts += Record[3];
3306       break;
3307 
3308     case UNUSED_FILESCOPED_DECLS:
3309       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3310         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3311       break;
3312 
3313     case DELEGATING_CTORS:
3314       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3315         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3316       break;
3317 
3318     case WEAK_UNDECLARED_IDENTIFIERS:
3319       if (Record.size() % 4 != 0) {
3320         Error("invalid weak identifiers record");
3321         return Failure;
3322       }
3323 
3324       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3325       // files. This isn't the way to do it :)
3326       WeakUndeclaredIdentifiers.clear();
3327 
3328       // Translate the weak, undeclared identifiers into global IDs.
3329       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3330         WeakUndeclaredIdentifiers.push_back(
3331           getGlobalIdentifierID(F, Record[I++]));
3332         WeakUndeclaredIdentifiers.push_back(
3333           getGlobalIdentifierID(F, Record[I++]));
3334         WeakUndeclaredIdentifiers.push_back(
3335           ReadSourceLocation(F, Record, I).getRawEncoding());
3336         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3337       }
3338       break;
3339 
3340     case SELECTOR_OFFSETS: {
3341       F.SelectorOffsets = (const uint32_t *)Blob.data();
3342       F.LocalNumSelectors = Record[0];
3343       unsigned LocalBaseSelectorID = Record[1];
3344       F.BaseSelectorID = getTotalNumSelectors();
3345 
3346       if (F.LocalNumSelectors > 0) {
3347         // Introduce the global -> local mapping for selectors within this
3348         // module.
3349         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3350 
3351         // Introduce the local -> global mapping for selectors within this
3352         // module.
3353         F.SelectorRemap.insertOrReplace(
3354           std::make_pair(LocalBaseSelectorID,
3355                          F.BaseSelectorID - LocalBaseSelectorID));
3356 
3357         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3358       }
3359       break;
3360     }
3361 
3362     case METHOD_POOL:
3363       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3364       if (Record[0])
3365         F.SelectorLookupTable
3366           = ASTSelectorLookupTable::Create(
3367                         F.SelectorLookupTableData + Record[0],
3368                         F.SelectorLookupTableData,
3369                         ASTSelectorLookupTrait(*this, F));
3370       TotalNumMethodPoolEntries += Record[1];
3371       break;
3372 
3373     case REFERENCED_SELECTOR_POOL:
3374       if (!Record.empty()) {
3375         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3376           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3377                                                                 Record[Idx++]));
3378           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3379                                               getRawEncoding());
3380         }
3381       }
3382       break;
3383 
3384     case PP_CONDITIONAL_STACK:
3385       if (!Record.empty()) {
3386         unsigned Idx = 0, End = Record.size() - 1;
3387         bool ReachedEOFWhileSkipping = Record[Idx++];
3388         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3389         if (ReachedEOFWhileSkipping) {
3390           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3391           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3392           bool FoundNonSkipPortion = Record[Idx++];
3393           bool FoundElse = Record[Idx++];
3394           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3395           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3396                            FoundElse, ElseLoc);
3397         }
3398         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3399         while (Idx < End) {
3400           auto Loc = ReadSourceLocation(F, Record, Idx);
3401           bool WasSkipping = Record[Idx++];
3402           bool FoundNonSkip = Record[Idx++];
3403           bool FoundElse = Record[Idx++];
3404           ConditionalStack.push_back(
3405               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3406         }
3407         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3408       }
3409       break;
3410 
3411     case PP_COUNTER_VALUE:
3412       if (!Record.empty() && Listener)
3413         Listener->ReadCounter(F, Record[0]);
3414       break;
3415 
3416     case FILE_SORTED_DECLS:
3417       F.FileSortedDecls = (const DeclID *)Blob.data();
3418       F.NumFileSortedDecls = Record[0];
3419       break;
3420 
3421     case SOURCE_LOCATION_OFFSETS: {
3422       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3423       F.LocalNumSLocEntries = Record[0];
3424       SourceLocation::UIntTy SLocSpaceSize = Record[1];
3425       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3426       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3427           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3428                                               SLocSpaceSize);
3429       if (!F.SLocEntryBaseID) {
3430         Error("ran out of source locations");
3431         break;
3432       }
3433       // Make our entry in the range map. BaseID is negative and growing, so
3434       // we invert it. Because we invert it, though, we need the other end of
3435       // the range.
3436       unsigned RangeStart =
3437           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3438       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3439       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3440 
3441       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3442       assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3443       GlobalSLocOffsetMap.insert(
3444           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3445                            - SLocSpaceSize,&F));
3446 
3447       // Initialize the remapping table.
3448       // Invalid stays invalid.
3449       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3450       // This module. Base was 2 when being compiled.
3451       F.SLocRemap.insertOrReplace(std::make_pair(
3452           2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3453 
3454       TotalNumSLocEntries += F.LocalNumSLocEntries;
3455       break;
3456     }
3457 
3458     case MODULE_OFFSET_MAP:
3459       F.ModuleOffsetMap = Blob;
3460       break;
3461 
3462     case SOURCE_MANAGER_LINE_TABLE:
3463       if (ParseLineTable(F, Record)) {
3464         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3465         return Failure;
3466       }
3467       break;
3468 
3469     case SOURCE_LOCATION_PRELOADS: {
3470       // Need to transform from the local view (1-based IDs) to the global view,
3471       // which is based off F.SLocEntryBaseID.
3472       if (!F.PreloadSLocEntries.empty()) {
3473         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3474         return Failure;
3475       }
3476 
3477       F.PreloadSLocEntries.swap(Record);
3478       break;
3479     }
3480 
3481     case EXT_VECTOR_DECLS:
3482       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3483         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3484       break;
3485 
3486     case VTABLE_USES:
3487       if (Record.size() % 3 != 0) {
3488         Error("Invalid VTABLE_USES record");
3489         return Failure;
3490       }
3491 
3492       // Later tables overwrite earlier ones.
3493       // FIXME: Modules will have some trouble with this. This is clearly not
3494       // the right way to do this.
3495       VTableUses.clear();
3496 
3497       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3498         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3499         VTableUses.push_back(
3500           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3501         VTableUses.push_back(Record[Idx++]);
3502       }
3503       break;
3504 
3505     case PENDING_IMPLICIT_INSTANTIATIONS:
3506       if (PendingInstantiations.size() % 2 != 0) {
3507         Error("Invalid existing PendingInstantiations");
3508         return Failure;
3509       }
3510 
3511       if (Record.size() % 2 != 0) {
3512         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3513         return Failure;
3514       }
3515 
3516       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3517         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3518         PendingInstantiations.push_back(
3519           ReadSourceLocation(F, Record, I).getRawEncoding());
3520       }
3521       break;
3522 
3523     case SEMA_DECL_REFS:
3524       if (Record.size() != 3) {
3525         Error("Invalid SEMA_DECL_REFS block");
3526         return Failure;
3527       }
3528       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3529         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3530       break;
3531 
3532     case PPD_ENTITIES_OFFSETS: {
3533       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3534       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3535       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3536 
3537       unsigned LocalBasePreprocessedEntityID = Record[0];
3538 
3539       unsigned StartingID;
3540       if (!PP.getPreprocessingRecord())
3541         PP.createPreprocessingRecord();
3542       if (!PP.getPreprocessingRecord()->getExternalSource())
3543         PP.getPreprocessingRecord()->SetExternalSource(*this);
3544       StartingID
3545         = PP.getPreprocessingRecord()
3546             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3547       F.BasePreprocessedEntityID = StartingID;
3548 
3549       if (F.NumPreprocessedEntities > 0) {
3550         // Introduce the global -> local mapping for preprocessed entities in
3551         // this module.
3552         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3553 
3554         // Introduce the local -> global mapping for preprocessed entities in
3555         // this module.
3556         F.PreprocessedEntityRemap.insertOrReplace(
3557           std::make_pair(LocalBasePreprocessedEntityID,
3558             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3559       }
3560 
3561       break;
3562     }
3563 
3564     case PPD_SKIPPED_RANGES: {
3565       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3566       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3567       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3568 
3569       if (!PP.getPreprocessingRecord())
3570         PP.createPreprocessingRecord();
3571       if (!PP.getPreprocessingRecord()->getExternalSource())
3572         PP.getPreprocessingRecord()->SetExternalSource(*this);
3573       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3574           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3575 
3576       if (F.NumPreprocessedSkippedRanges > 0)
3577         GlobalSkippedRangeMap.insert(
3578             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3579       break;
3580     }
3581 
3582     case DECL_UPDATE_OFFSETS:
3583       if (Record.size() % 2 != 0) {
3584         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3585         return Failure;
3586       }
3587       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3588         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3589         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3590 
3591         // If we've already loaded the decl, perform the updates when we finish
3592         // loading this block.
3593         if (Decl *D = GetExistingDecl(ID))
3594           PendingUpdateRecords.push_back(
3595               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3596       }
3597       break;
3598 
3599     case OBJC_CATEGORIES_MAP:
3600       if (F.LocalNumObjCCategoriesInMap != 0) {
3601         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3602         return Failure;
3603       }
3604 
3605       F.LocalNumObjCCategoriesInMap = Record[0];
3606       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3607       break;
3608 
3609     case OBJC_CATEGORIES:
3610       F.ObjCCategories.swap(Record);
3611       break;
3612 
3613     case CUDA_SPECIAL_DECL_REFS:
3614       // Later tables overwrite earlier ones.
3615       // FIXME: Modules will have trouble with this.
3616       CUDASpecialDeclRefs.clear();
3617       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3618         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3619       break;
3620 
3621     case HEADER_SEARCH_TABLE:
3622       F.HeaderFileInfoTableData = Blob.data();
3623       F.LocalNumHeaderFileInfos = Record[1];
3624       if (Record[0]) {
3625         F.HeaderFileInfoTable
3626           = HeaderFileInfoLookupTable::Create(
3627                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3628                    (const unsigned char *)F.HeaderFileInfoTableData,
3629                    HeaderFileInfoTrait(*this, F,
3630                                        &PP.getHeaderSearchInfo(),
3631                                        Blob.data() + Record[2]));
3632 
3633         PP.getHeaderSearchInfo().SetExternalSource(this);
3634         if (!PP.getHeaderSearchInfo().getExternalLookup())
3635           PP.getHeaderSearchInfo().SetExternalLookup(this);
3636       }
3637       break;
3638 
3639     case FP_PRAGMA_OPTIONS:
3640       // Later tables overwrite earlier ones.
3641       FPPragmaOptions.swap(Record);
3642       break;
3643 
3644     case OPENCL_EXTENSIONS:
3645       for (unsigned I = 0, E = Record.size(); I != E; ) {
3646         auto Name = ReadString(Record, I);
3647         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3648         OptInfo.Supported = Record[I++] != 0;
3649         OptInfo.Enabled = Record[I++] != 0;
3650         OptInfo.WithPragma = Record[I++] != 0;
3651         OptInfo.Avail = Record[I++];
3652         OptInfo.Core = Record[I++];
3653         OptInfo.Opt = Record[I++];
3654       }
3655       break;
3656 
3657     case TENTATIVE_DEFINITIONS:
3658       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3659         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3660       break;
3661 
3662     case KNOWN_NAMESPACES:
3663       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3664         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3665       break;
3666 
3667     case UNDEFINED_BUT_USED:
3668       if (UndefinedButUsed.size() % 2 != 0) {
3669         Error("Invalid existing UndefinedButUsed");
3670         return Failure;
3671       }
3672 
3673       if (Record.size() % 2 != 0) {
3674         Error("invalid undefined-but-used record");
3675         return Failure;
3676       }
3677       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3678         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3679         UndefinedButUsed.push_back(
3680             ReadSourceLocation(F, Record, I).getRawEncoding());
3681       }
3682       break;
3683 
3684     case DELETE_EXPRS_TO_ANALYZE:
3685       for (unsigned I = 0, N = Record.size(); I != N;) {
3686         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3687         const uint64_t Count = Record[I++];
3688         DelayedDeleteExprs.push_back(Count);
3689         for (uint64_t C = 0; C < Count; ++C) {
3690           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3691           bool IsArrayForm = Record[I++] == 1;
3692           DelayedDeleteExprs.push_back(IsArrayForm);
3693         }
3694       }
3695       break;
3696 
3697     case IMPORTED_MODULES:
3698       if (!F.isModule()) {
3699         // If we aren't loading a module (which has its own exports), make
3700         // all of the imported modules visible.
3701         // FIXME: Deal with macros-only imports.
3702         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3703           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3704           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3705           if (GlobalID) {
3706             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3707             if (DeserializationListener)
3708               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3709           }
3710         }
3711       }
3712       break;
3713 
3714     case MACRO_OFFSET: {
3715       if (F.LocalNumMacros != 0) {
3716         Error("duplicate MACRO_OFFSET record in AST file");
3717         return Failure;
3718       }
3719       F.MacroOffsets = (const uint32_t *)Blob.data();
3720       F.LocalNumMacros = Record[0];
3721       unsigned LocalBaseMacroID = Record[1];
3722       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3723       F.BaseMacroID = getTotalNumMacros();
3724 
3725       if (F.LocalNumMacros > 0) {
3726         // Introduce the global -> local mapping for macros within this module.
3727         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3728 
3729         // Introduce the local -> global mapping for macros within this module.
3730         F.MacroRemap.insertOrReplace(
3731           std::make_pair(LocalBaseMacroID,
3732                          F.BaseMacroID - LocalBaseMacroID));
3733 
3734         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3735       }
3736       break;
3737     }
3738 
3739     case LATE_PARSED_TEMPLATE:
3740       LateParsedTemplates.emplace_back(
3741           std::piecewise_construct, std::forward_as_tuple(&F),
3742           std::forward_as_tuple(Record.begin(), Record.end()));
3743       break;
3744 
3745     case OPTIMIZE_PRAGMA_OPTIONS:
3746       if (Record.size() != 1) {
3747         Error("invalid pragma optimize record");
3748         return Failure;
3749       }
3750       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3751       break;
3752 
3753     case MSSTRUCT_PRAGMA_OPTIONS:
3754       if (Record.size() != 1) {
3755         Error("invalid pragma ms_struct record");
3756         return Failure;
3757       }
3758       PragmaMSStructState = Record[0];
3759       break;
3760 
3761     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3762       if (Record.size() != 2) {
3763         Error("invalid pragma ms_struct record");
3764         return Failure;
3765       }
3766       PragmaMSPointersToMembersState = Record[0];
3767       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3768       break;
3769 
3770     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3771       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3772         UnusedLocalTypedefNameCandidates.push_back(
3773             getGlobalDeclID(F, Record[I]));
3774       break;
3775 
3776     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3777       if (Record.size() != 1) {
3778         Error("invalid cuda pragma options record");
3779         return Failure;
3780       }
3781       ForceCUDAHostDeviceDepth = Record[0];
3782       break;
3783 
3784     case ALIGN_PACK_PRAGMA_OPTIONS: {
3785       if (Record.size() < 3) {
3786         Error("invalid pragma pack record");
3787         return Failure;
3788       }
3789       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3790       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3791       unsigned NumStackEntries = Record[2];
3792       unsigned Idx = 3;
3793       // Reset the stack when importing a new module.
3794       PragmaAlignPackStack.clear();
3795       for (unsigned I = 0; I < NumStackEntries; ++I) {
3796         PragmaAlignPackStackEntry Entry;
3797         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3798         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3799         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3800         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3801         Entry.SlotLabel = PragmaAlignPackStrings.back();
3802         PragmaAlignPackStack.push_back(Entry);
3803       }
3804       break;
3805     }
3806 
3807     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3808       if (Record.size() < 3) {
3809         Error("invalid pragma pack record");
3810         return Failure;
3811       }
3812       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3813       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3814       unsigned NumStackEntries = Record[2];
3815       unsigned Idx = 3;
3816       // Reset the stack when importing a new module.
3817       FpPragmaStack.clear();
3818       for (unsigned I = 0; I < NumStackEntries; ++I) {
3819         FpPragmaStackEntry Entry;
3820         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3821         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3822         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3823         FpPragmaStrings.push_back(ReadString(Record, Idx));
3824         Entry.SlotLabel = FpPragmaStrings.back();
3825         FpPragmaStack.push_back(Entry);
3826       }
3827       break;
3828     }
3829 
3830     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3831       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3832         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3833       break;
3834     }
3835   }
3836 }
3837 
3838 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3839   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3840 
3841   // Additional remapping information.
3842   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3843   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3844   F.ModuleOffsetMap = StringRef();
3845 
3846   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3847   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3848     F.SLocRemap.insert(std::make_pair(0U, 0));
3849     F.SLocRemap.insert(std::make_pair(2U, 1));
3850   }
3851 
3852   // Continuous range maps we may be updating in our module.
3853   using SLocRemapBuilder =
3854       ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
3855                          2>::Builder;
3856   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3857   SLocRemapBuilder SLocRemap(F.SLocRemap);
3858   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3859   RemapBuilder MacroRemap(F.MacroRemap);
3860   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3861   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3862   RemapBuilder SelectorRemap(F.SelectorRemap);
3863   RemapBuilder DeclRemap(F.DeclRemap);
3864   RemapBuilder TypeRemap(F.TypeRemap);
3865 
3866   while (Data < DataEnd) {
3867     // FIXME: Looking up dependency modules by filename is horrible. Let's
3868     // start fixing this with prebuilt, explicit and implicit modules and see
3869     // how it goes...
3870     using namespace llvm::support;
3871     ModuleKind Kind = static_cast<ModuleKind>(
3872       endian::readNext<uint8_t, little, unaligned>(Data));
3873     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3874     StringRef Name = StringRef((const char*)Data, Len);
3875     Data += Len;
3876     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3877                               Kind == MK_ImplicitModule
3878                           ? ModuleMgr.lookupByModuleName(Name)
3879                           : ModuleMgr.lookupByFileName(Name));
3880     if (!OM) {
3881       std::string Msg =
3882           "SourceLocation remap refers to unknown module, cannot find ";
3883       Msg.append(std::string(Name));
3884       Error(Msg);
3885       return;
3886     }
3887 
3888     SourceLocation::UIntTy SLocOffset =
3889         endian::readNext<uint32_t, little, unaligned>(Data);
3890     uint32_t IdentifierIDOffset =
3891         endian::readNext<uint32_t, little, unaligned>(Data);
3892     uint32_t MacroIDOffset =
3893         endian::readNext<uint32_t, little, unaligned>(Data);
3894     uint32_t PreprocessedEntityIDOffset =
3895         endian::readNext<uint32_t, little, unaligned>(Data);
3896     uint32_t SubmoduleIDOffset =
3897         endian::readNext<uint32_t, little, unaligned>(Data);
3898     uint32_t SelectorIDOffset =
3899         endian::readNext<uint32_t, little, unaligned>(Data);
3900     uint32_t DeclIDOffset =
3901         endian::readNext<uint32_t, little, unaligned>(Data);
3902     uint32_t TypeIndexOffset =
3903         endian::readNext<uint32_t, little, unaligned>(Data);
3904 
3905     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3906                          RemapBuilder &Remap) {
3907       constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
3908       if (Offset != None)
3909         Remap.insert(std::make_pair(Offset,
3910                                     static_cast<int>(BaseOffset - Offset)));
3911     };
3912 
3913     constexpr SourceLocation::UIntTy SLocNone =
3914         std::numeric_limits<SourceLocation::UIntTy>::max();
3915     if (SLocOffset != SLocNone)
3916       SLocRemap.insert(std::make_pair(
3917           SLocOffset, static_cast<SourceLocation::IntTy>(
3918                           OM->SLocEntryBaseOffset - SLocOffset)));
3919 
3920     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3921     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3922     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3923               PreprocessedEntityRemap);
3924     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3925     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3926     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3927     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3928 
3929     // Global -> local mappings.
3930     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3931   }
3932 }
3933 
3934 ASTReader::ASTReadResult
3935 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3936                                   const ModuleFile *ImportedBy,
3937                                   unsigned ClientLoadCapabilities) {
3938   unsigned Idx = 0;
3939   F.ModuleMapPath = ReadPath(F, Record, Idx);
3940 
3941   // Try to resolve ModuleName in the current header search context and
3942   // verify that it is found in the same module map file as we saved. If the
3943   // top-level AST file is a main file, skip this check because there is no
3944   // usable header search context.
3945   assert(!F.ModuleName.empty() &&
3946          "MODULE_NAME should come before MODULE_MAP_FILE");
3947   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3948     // An implicitly-loaded module file should have its module listed in some
3949     // module map file that we've already loaded.
3950     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3951     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3952     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3953     // Don't emit module relocation error if we have -fno-validate-pch
3954     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3955               DisableValidationForModuleKind::Module) &&
3956         !ModMap) {
3957       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
3958         if (auto ASTFE = M ? M->getASTFile() : None) {
3959           // This module was defined by an imported (explicit) module.
3960           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3961                                                << ASTFE->getName();
3962         } else {
3963           // This module was built with a different module map.
3964           Diag(diag::err_imported_module_not_found)
3965               << F.ModuleName << F.FileName
3966               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3967               << !ImportedBy;
3968           // In case it was imported by a PCH, there's a chance the user is
3969           // just missing to include the search path to the directory containing
3970           // the modulemap.
3971           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3972             Diag(diag::note_imported_by_pch_module_not_found)
3973                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3974         }
3975       }
3976       return OutOfDate;
3977     }
3978 
3979     assert(M && M->Name == F.ModuleName && "found module with different name");
3980 
3981     // Check the primary module map file.
3982     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3983     if (!StoredModMap || *StoredModMap != ModMap) {
3984       assert(ModMap && "found module is missing module map file");
3985       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3986              "top-level import should be verified");
3987       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3988       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3989         Diag(diag::err_imported_module_modmap_changed)
3990             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3991             << ModMap->getName() << F.ModuleMapPath << NotImported;
3992       return OutOfDate;
3993     }
3994 
3995     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3996     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3997       // FIXME: we should use input files rather than storing names.
3998       std::string Filename = ReadPath(F, Record, Idx);
3999       auto SF = FileMgr.getFile(Filename, false, false);
4000       if (!SF) {
4001         if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4002           Error("could not find file '" + Filename +"' referenced by AST file");
4003         return OutOfDate;
4004       }
4005       AdditionalStoredMaps.insert(*SF);
4006     }
4007 
4008     // Check any additional module map files (e.g. module.private.modulemap)
4009     // that are not in the pcm.
4010     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
4011       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
4012         // Remove files that match
4013         // Note: SmallPtrSet::erase is really remove
4014         if (!AdditionalStoredMaps.erase(ModMap)) {
4015           if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4016             Diag(diag::err_module_different_modmap)
4017               << F.ModuleName << /*new*/0 << ModMap->getName();
4018           return OutOfDate;
4019         }
4020       }
4021     }
4022 
4023     // Check any additional module map files that are in the pcm, but not
4024     // found in header search. Cases that match are already removed.
4025     for (const FileEntry *ModMap : AdditionalStoredMaps) {
4026       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
4027         Diag(diag::err_module_different_modmap)
4028           << F.ModuleName << /*not new*/1 << ModMap->getName();
4029       return OutOfDate;
4030     }
4031   }
4032 
4033   if (Listener)
4034     Listener->ReadModuleMapFile(F.ModuleMapPath);
4035   return Success;
4036 }
4037 
4038 /// Move the given method to the back of the global list of methods.
4039 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4040   // Find the entry for this selector in the method pool.
4041   Sema::GlobalMethodPool::iterator Known
4042     = S.MethodPool.find(Method->getSelector());
4043   if (Known == S.MethodPool.end())
4044     return;
4045 
4046   // Retrieve the appropriate method list.
4047   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4048                                                     : Known->second.second;
4049   bool Found = false;
4050   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4051     if (!Found) {
4052       if (List->getMethod() == Method) {
4053         Found = true;
4054       } else {
4055         // Keep searching.
4056         continue;
4057       }
4058     }
4059 
4060     if (List->getNext())
4061       List->setMethod(List->getNext()->getMethod());
4062     else
4063       List->setMethod(Method);
4064   }
4065 }
4066 
4067 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4068   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4069   for (Decl *D : Names) {
4070     bool wasHidden = !D->isUnconditionallyVisible();
4071     D->setVisibleDespiteOwningModule();
4072 
4073     if (wasHidden && SemaObj) {
4074       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4075         moveMethodToBackOfGlobalList(*SemaObj, Method);
4076       }
4077     }
4078   }
4079 }
4080 
4081 void ASTReader::makeModuleVisible(Module *Mod,
4082                                   Module::NameVisibilityKind NameVisibility,
4083                                   SourceLocation ImportLoc) {
4084   llvm::SmallPtrSet<Module *, 4> Visited;
4085   SmallVector<Module *, 4> Stack;
4086   Stack.push_back(Mod);
4087   while (!Stack.empty()) {
4088     Mod = Stack.pop_back_val();
4089 
4090     if (NameVisibility <= Mod->NameVisibility) {
4091       // This module already has this level of visibility (or greater), so
4092       // there is nothing more to do.
4093       continue;
4094     }
4095 
4096     if (Mod->isUnimportable()) {
4097       // Modules that aren't importable cannot be made visible.
4098       continue;
4099     }
4100 
4101     // Update the module's name visibility.
4102     Mod->NameVisibility = NameVisibility;
4103 
4104     // If we've already deserialized any names from this module,
4105     // mark them as visible.
4106     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4107     if (Hidden != HiddenNamesMap.end()) {
4108       auto HiddenNames = std::move(*Hidden);
4109       HiddenNamesMap.erase(Hidden);
4110       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4111       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4112              "making names visible added hidden names");
4113     }
4114 
4115     // Push any exported modules onto the stack to be marked as visible.
4116     SmallVector<Module *, 16> Exports;
4117     Mod->getExportedModules(Exports);
4118     for (SmallVectorImpl<Module *>::iterator
4119            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4120       Module *Exported = *I;
4121       if (Visited.insert(Exported).second)
4122         Stack.push_back(Exported);
4123     }
4124   }
4125 }
4126 
4127 /// We've merged the definition \p MergedDef into the existing definition
4128 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4129 /// visible.
4130 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4131                                           NamedDecl *MergedDef) {
4132   if (!Def->isUnconditionallyVisible()) {
4133     // If MergedDef is visible or becomes visible, make the definition visible.
4134     if (MergedDef->isUnconditionallyVisible())
4135       Def->setVisibleDespiteOwningModule();
4136     else {
4137       getContext().mergeDefinitionIntoModule(
4138           Def, MergedDef->getImportedOwningModule(),
4139           /*NotifyListeners*/ false);
4140       PendingMergedDefinitionsToDeduplicate.insert(Def);
4141     }
4142   }
4143 }
4144 
4145 bool ASTReader::loadGlobalIndex() {
4146   if (GlobalIndex)
4147     return false;
4148 
4149   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4150       !PP.getLangOpts().Modules)
4151     return true;
4152 
4153   // Try to load the global index.
4154   TriedLoadingGlobalIndex = true;
4155   StringRef ModuleCachePath
4156     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4157   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4158       GlobalModuleIndex::readIndex(ModuleCachePath);
4159   if (llvm::Error Err = std::move(Result.second)) {
4160     assert(!Result.first);
4161     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4162     return true;
4163   }
4164 
4165   GlobalIndex.reset(Result.first);
4166   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4167   return false;
4168 }
4169 
4170 bool ASTReader::isGlobalIndexUnavailable() const {
4171   return PP.getLangOpts().Modules && UseGlobalIndex &&
4172          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4173 }
4174 
4175 static void updateModuleTimestamp(ModuleFile &MF) {
4176   // Overwrite the timestamp file contents so that file's mtime changes.
4177   std::string TimestampFilename = MF.getTimestampFilename();
4178   std::error_code EC;
4179   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4180                           llvm::sys::fs::OF_TextWithCRLF);
4181   if (EC)
4182     return;
4183   OS << "Timestamp file\n";
4184   OS.close();
4185   OS.clear_error(); // Avoid triggering a fatal error.
4186 }
4187 
4188 /// Given a cursor at the start of an AST file, scan ahead and drop the
4189 /// cursor into the start of the given block ID, returning false on success and
4190 /// true on failure.
4191 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4192   while (true) {
4193     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4194     if (!MaybeEntry) {
4195       // FIXME this drops errors on the floor.
4196       consumeError(MaybeEntry.takeError());
4197       return true;
4198     }
4199     llvm::BitstreamEntry Entry = MaybeEntry.get();
4200 
4201     switch (Entry.Kind) {
4202     case llvm::BitstreamEntry::Error:
4203     case llvm::BitstreamEntry::EndBlock:
4204       return true;
4205 
4206     case llvm::BitstreamEntry::Record:
4207       // Ignore top-level records.
4208       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4209         break;
4210       else {
4211         // FIXME this drops errors on the floor.
4212         consumeError(Skipped.takeError());
4213         return true;
4214       }
4215 
4216     case llvm::BitstreamEntry::SubBlock:
4217       if (Entry.ID == BlockID) {
4218         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4219           // FIXME this drops the error on the floor.
4220           consumeError(std::move(Err));
4221           return true;
4222         }
4223         // Found it!
4224         return false;
4225       }
4226 
4227       if (llvm::Error Err = Cursor.SkipBlock()) {
4228         // FIXME this drops the error on the floor.
4229         consumeError(std::move(Err));
4230         return true;
4231       }
4232     }
4233   }
4234 }
4235 
4236 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4237                                             ModuleKind Type,
4238                                             SourceLocation ImportLoc,
4239                                             unsigned ClientLoadCapabilities,
4240                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4241   llvm::SaveAndRestore<SourceLocation>
4242     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4243   llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4244       CurrentDeserializingModuleKind, Type);
4245 
4246   // Defer any pending actions until we get to the end of reading the AST file.
4247   Deserializing AnASTFile(this);
4248 
4249   // Bump the generation number.
4250   unsigned PreviousGeneration = 0;
4251   if (ContextObj)
4252     PreviousGeneration = incrementGeneration(*ContextObj);
4253 
4254   unsigned NumModules = ModuleMgr.size();
4255   SmallVector<ImportedModule, 4> Loaded;
4256   if (ASTReadResult ReadResult =
4257           ReadASTCore(FileName, Type, ImportLoc,
4258                       /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4259                       ClientLoadCapabilities)) {
4260     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4261                             PP.getLangOpts().Modules
4262                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4263                                 : nullptr);
4264 
4265     // If we find that any modules are unusable, the global index is going
4266     // to be out-of-date. Just remove it.
4267     GlobalIndex.reset();
4268     ModuleMgr.setGlobalIndex(nullptr);
4269     return ReadResult;
4270   }
4271 
4272   // Here comes stuff that we only do once the entire chain is loaded.
4273 
4274   // Load the AST blocks of all of the modules that we loaded.  We can still
4275   // hit errors parsing the ASTs at this point.
4276   for (ImportedModule &M : Loaded) {
4277     ModuleFile &F = *M.Mod;
4278 
4279     // Read the AST block.
4280     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4281       return Failure;
4282 
4283     // The AST block should always have a definition for the main module.
4284     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4285       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4286       return Failure;
4287     }
4288 
4289     // Read the extension blocks.
4290     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4291       if (ASTReadResult Result = ReadExtensionBlock(F))
4292         return Failure;
4293     }
4294 
4295     // Once read, set the ModuleFile bit base offset and update the size in
4296     // bits of all files we've seen.
4297     F.GlobalBitOffset = TotalModulesSizeInBits;
4298     TotalModulesSizeInBits += F.SizeInBits;
4299     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4300   }
4301 
4302   // Preload source locations and interesting indentifiers.
4303   for (ImportedModule &M : Loaded) {
4304     ModuleFile &F = *M.Mod;
4305 
4306     // Preload SLocEntries.
4307     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4308       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4309       // Load it through the SourceManager and don't call ReadSLocEntry()
4310       // directly because the entry may have already been loaded in which case
4311       // calling ReadSLocEntry() directly would trigger an assertion in
4312       // SourceManager.
4313       SourceMgr.getLoadedSLocEntryByID(Index);
4314     }
4315 
4316     // Map the original source file ID into the ID space of the current
4317     // compilation.
4318     if (F.OriginalSourceFileID.isValid()) {
4319       F.OriginalSourceFileID = FileID::get(
4320           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4321     }
4322 
4323     // Preload all the pending interesting identifiers by marking them out of
4324     // date.
4325     for (auto Offset : F.PreloadIdentifierOffsets) {
4326       const unsigned char *Data = F.IdentifierTableData + Offset;
4327 
4328       ASTIdentifierLookupTrait Trait(*this, F);
4329       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4330       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4331       auto &II = PP.getIdentifierTable().getOwn(Key);
4332       II.setOutOfDate(true);
4333 
4334       // Mark this identifier as being from an AST file so that we can track
4335       // whether we need to serialize it.
4336       markIdentifierFromAST(*this, II);
4337 
4338       // Associate the ID with the identifier so that the writer can reuse it.
4339       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4340       SetIdentifierInfo(ID, &II);
4341     }
4342   }
4343 
4344   // Setup the import locations and notify the module manager that we've
4345   // committed to these module files.
4346   for (ImportedModule &M : Loaded) {
4347     ModuleFile &F = *M.Mod;
4348 
4349     ModuleMgr.moduleFileAccepted(&F);
4350 
4351     // Set the import location.
4352     F.DirectImportLoc = ImportLoc;
4353     // FIXME: We assume that locations from PCH / preamble do not need
4354     // any translation.
4355     if (!M.ImportedBy)
4356       F.ImportLoc = M.ImportLoc;
4357     else
4358       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4359   }
4360 
4361   if (!PP.getLangOpts().CPlusPlus ||
4362       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4363        Type != MK_PrebuiltModule)) {
4364     // Mark all of the identifiers in the identifier table as being out of date,
4365     // so that various accessors know to check the loaded modules when the
4366     // identifier is used.
4367     //
4368     // For C++ modules, we don't need information on many identifiers (just
4369     // those that provide macros or are poisoned), so we mark all of
4370     // the interesting ones via PreloadIdentifierOffsets.
4371     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4372                                 IdEnd = PP.getIdentifierTable().end();
4373          Id != IdEnd; ++Id)
4374       Id->second->setOutOfDate(true);
4375   }
4376   // Mark selectors as out of date.
4377   for (auto Sel : SelectorGeneration)
4378     SelectorOutOfDate[Sel.first] = true;
4379 
4380   // Resolve any unresolved module exports.
4381   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4382     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4383     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4384     Module *ResolvedMod = getSubmodule(GlobalID);
4385 
4386     switch (Unresolved.Kind) {
4387     case UnresolvedModuleRef::Conflict:
4388       if (ResolvedMod) {
4389         Module::Conflict Conflict;
4390         Conflict.Other = ResolvedMod;
4391         Conflict.Message = Unresolved.String.str();
4392         Unresolved.Mod->Conflicts.push_back(Conflict);
4393       }
4394       continue;
4395 
4396     case UnresolvedModuleRef::Import:
4397       if (ResolvedMod)
4398         Unresolved.Mod->Imports.insert(ResolvedMod);
4399       continue;
4400 
4401     case UnresolvedModuleRef::Export:
4402       if (ResolvedMod || Unresolved.IsWildcard)
4403         Unresolved.Mod->Exports.push_back(
4404           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4405       continue;
4406     }
4407   }
4408   UnresolvedModuleRefs.clear();
4409 
4410   if (Imported)
4411     Imported->append(ImportedModules.begin(),
4412                      ImportedModules.end());
4413 
4414   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4415   // Might be unnecessary as use declarations are only used to build the
4416   // module itself.
4417 
4418   if (ContextObj)
4419     InitializeContext();
4420 
4421   if (SemaObj)
4422     UpdateSema();
4423 
4424   if (DeserializationListener)
4425     DeserializationListener->ReaderInitialized(this);
4426 
4427   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4428   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4429     // If this AST file is a precompiled preamble, then set the
4430     // preamble file ID of the source manager to the file source file
4431     // from which the preamble was built.
4432     if (Type == MK_Preamble) {
4433       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4434     } else if (Type == MK_MainFile) {
4435       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4436     }
4437   }
4438 
4439   // For any Objective-C class definitions we have already loaded, make sure
4440   // that we load any additional categories.
4441   if (ContextObj) {
4442     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4443       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4444                          ObjCClassesLoaded[I],
4445                          PreviousGeneration);
4446     }
4447   }
4448 
4449   if (PP.getHeaderSearchInfo()
4450           .getHeaderSearchOpts()
4451           .ModulesValidateOncePerBuildSession) {
4452     // Now we are certain that the module and all modules it depends on are
4453     // up to date.  Create or update timestamp files for modules that are
4454     // located in the module cache (not for PCH files that could be anywhere
4455     // in the filesystem).
4456     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4457       ImportedModule &M = Loaded[I];
4458       if (M.Mod->Kind == MK_ImplicitModule) {
4459         updateModuleTimestamp(*M.Mod);
4460       }
4461     }
4462   }
4463 
4464   return Success;
4465 }
4466 
4467 static ASTFileSignature readASTFileSignature(StringRef PCH);
4468 
4469 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4470 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4471   // FIXME checking magic headers is done in other places such as
4472   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4473   // always done the same. Unify it all with a helper.
4474   if (!Stream.canSkipToPos(4))
4475     return llvm::createStringError(std::errc::illegal_byte_sequence,
4476                                    "file too small to contain AST file magic");
4477   for (unsigned C : {'C', 'P', 'C', 'H'})
4478     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4479       if (Res.get() != C)
4480         return llvm::createStringError(
4481             std::errc::illegal_byte_sequence,
4482             "file doesn't start with AST file magic");
4483     } else
4484       return Res.takeError();
4485   return llvm::Error::success();
4486 }
4487 
4488 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4489   switch (Kind) {
4490   case MK_PCH:
4491     return 0; // PCH
4492   case MK_ImplicitModule:
4493   case MK_ExplicitModule:
4494   case MK_PrebuiltModule:
4495     return 1; // module
4496   case MK_MainFile:
4497   case MK_Preamble:
4498     return 2; // main source file
4499   }
4500   llvm_unreachable("unknown module kind");
4501 }
4502 
4503 ASTReader::ASTReadResult
4504 ASTReader::ReadASTCore(StringRef FileName,
4505                        ModuleKind Type,
4506                        SourceLocation ImportLoc,
4507                        ModuleFile *ImportedBy,
4508                        SmallVectorImpl<ImportedModule> &Loaded,
4509                        off_t ExpectedSize, time_t ExpectedModTime,
4510                        ASTFileSignature ExpectedSignature,
4511                        unsigned ClientLoadCapabilities) {
4512   ModuleFile *M;
4513   std::string ErrorStr;
4514   ModuleManager::AddModuleResult AddResult
4515     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4516                           getGeneration(), ExpectedSize, ExpectedModTime,
4517                           ExpectedSignature, readASTFileSignature,
4518                           M, ErrorStr);
4519 
4520   switch (AddResult) {
4521   case ModuleManager::AlreadyLoaded:
4522     Diag(diag::remark_module_import)
4523         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4524         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4525     return Success;
4526 
4527   case ModuleManager::NewlyLoaded:
4528     // Load module file below.
4529     break;
4530 
4531   case ModuleManager::Missing:
4532     // The module file was missing; if the client can handle that, return
4533     // it.
4534     if (ClientLoadCapabilities & ARR_Missing)
4535       return Missing;
4536 
4537     // Otherwise, return an error.
4538     Diag(diag::err_ast_file_not_found)
4539         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4540         << ErrorStr;
4541     return Failure;
4542 
4543   case ModuleManager::OutOfDate:
4544     // We couldn't load the module file because it is out-of-date. If the
4545     // client can handle out-of-date, return it.
4546     if (ClientLoadCapabilities & ARR_OutOfDate)
4547       return OutOfDate;
4548 
4549     // Otherwise, return an error.
4550     Diag(diag::err_ast_file_out_of_date)
4551         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4552         << ErrorStr;
4553     return Failure;
4554   }
4555 
4556   assert(M && "Missing module file");
4557 
4558   bool ShouldFinalizePCM = false;
4559   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4560     auto &MC = getModuleManager().getModuleCache();
4561     if (ShouldFinalizePCM)
4562       MC.finalizePCM(FileName);
4563     else
4564       MC.tryToDropPCM(FileName);
4565   });
4566   ModuleFile &F = *M;
4567   BitstreamCursor &Stream = F.Stream;
4568   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4569   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4570 
4571   // Sniff for the signature.
4572   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4573     Diag(diag::err_ast_file_invalid)
4574         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4575     return Failure;
4576   }
4577 
4578   // This is used for compatibility with older PCH formats.
4579   bool HaveReadControlBlock = false;
4580   while (true) {
4581     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4582     if (!MaybeEntry) {
4583       Error(MaybeEntry.takeError());
4584       return Failure;
4585     }
4586     llvm::BitstreamEntry Entry = MaybeEntry.get();
4587 
4588     switch (Entry.Kind) {
4589     case llvm::BitstreamEntry::Error:
4590     case llvm::BitstreamEntry::Record:
4591     case llvm::BitstreamEntry::EndBlock:
4592       Error("invalid record at top-level of AST file");
4593       return Failure;
4594 
4595     case llvm::BitstreamEntry::SubBlock:
4596       break;
4597     }
4598 
4599     switch (Entry.ID) {
4600     case CONTROL_BLOCK_ID:
4601       HaveReadControlBlock = true;
4602       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4603       case Success:
4604         // Check that we didn't try to load a non-module AST file as a module.
4605         //
4606         // FIXME: Should we also perform the converse check? Loading a module as
4607         // a PCH file sort of works, but it's a bit wonky.
4608         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4609              Type == MK_PrebuiltModule) &&
4610             F.ModuleName.empty()) {
4611           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4612           if (Result != OutOfDate ||
4613               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4614             Diag(diag::err_module_file_not_module) << FileName;
4615           return Result;
4616         }
4617         break;
4618 
4619       case Failure: return Failure;
4620       case Missing: return Missing;
4621       case OutOfDate: return OutOfDate;
4622       case VersionMismatch: return VersionMismatch;
4623       case ConfigurationMismatch: return ConfigurationMismatch;
4624       case HadErrors: return HadErrors;
4625       }
4626       break;
4627 
4628     case AST_BLOCK_ID:
4629       if (!HaveReadControlBlock) {
4630         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4631           Diag(diag::err_pch_version_too_old);
4632         return VersionMismatch;
4633       }
4634 
4635       // Record that we've loaded this module.
4636       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4637       ShouldFinalizePCM = true;
4638       return Success;
4639 
4640     case UNHASHED_CONTROL_BLOCK_ID:
4641       // This block is handled using look-ahead during ReadControlBlock.  We
4642       // shouldn't get here!
4643       Error("malformed block record in AST file");
4644       return Failure;
4645 
4646     default:
4647       if (llvm::Error Err = Stream.SkipBlock()) {
4648         Error(std::move(Err));
4649         return Failure;
4650       }
4651       break;
4652     }
4653   }
4654 
4655   llvm_unreachable("unexpected break; expected return");
4656 }
4657 
4658 ASTReader::ASTReadResult
4659 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4660                                     unsigned ClientLoadCapabilities) {
4661   const HeaderSearchOptions &HSOpts =
4662       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4663   bool AllowCompatibleConfigurationMismatch =
4664       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4665   bool DisableValidation = shouldDisableValidationForFile(F);
4666 
4667   ASTReadResult Result = readUnhashedControlBlockImpl(
4668       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4669       Listener.get(),
4670       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4671 
4672   // If F was directly imported by another module, it's implicitly validated by
4673   // the importing module.
4674   if (DisableValidation || WasImportedBy ||
4675       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4676     return Success;
4677 
4678   if (Result == Failure) {
4679     Error("malformed block record in AST file");
4680     return Failure;
4681   }
4682 
4683   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4684     // If this module has already been finalized in the ModuleCache, we're stuck
4685     // with it; we can only load a single version of each module.
4686     //
4687     // This can happen when a module is imported in two contexts: in one, as a
4688     // user module; in another, as a system module (due to an import from
4689     // another module marked with the [system] flag).  It usually indicates a
4690     // bug in the module map: this module should also be marked with [system].
4691     //
4692     // If -Wno-system-headers (the default), and the first import is as a
4693     // system module, then validation will fail during the as-user import,
4694     // since -Werror flags won't have been validated.  However, it's reasonable
4695     // to treat this consistently as a system module.
4696     //
4697     // If -Wsystem-headers, the PCM on disk was built with
4698     // -Wno-system-headers, and the first import is as a user module, then
4699     // validation will fail during the as-system import since the PCM on disk
4700     // doesn't guarantee that -Werror was respected.  However, the -Werror
4701     // flags were checked during the initial as-user import.
4702     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4703       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4704       return Success;
4705     }
4706   }
4707 
4708   return Result;
4709 }
4710 
4711 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4712     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4713     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4714     bool ValidateDiagnosticOptions) {
4715   // Initialize a stream.
4716   BitstreamCursor Stream(StreamData);
4717 
4718   // Sniff for the signature.
4719   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4720     // FIXME this drops the error on the floor.
4721     consumeError(std::move(Err));
4722     return Failure;
4723   }
4724 
4725   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4726   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4727     return Failure;
4728 
4729   // Read all of the records in the options block.
4730   RecordData Record;
4731   ASTReadResult Result = Success;
4732   while (true) {
4733     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4734     if (!MaybeEntry) {
4735       // FIXME this drops the error on the floor.
4736       consumeError(MaybeEntry.takeError());
4737       return Failure;
4738     }
4739     llvm::BitstreamEntry Entry = MaybeEntry.get();
4740 
4741     switch (Entry.Kind) {
4742     case llvm::BitstreamEntry::Error:
4743     case llvm::BitstreamEntry::SubBlock:
4744       return Failure;
4745 
4746     case llvm::BitstreamEntry::EndBlock:
4747       return Result;
4748 
4749     case llvm::BitstreamEntry::Record:
4750       // The interesting case.
4751       break;
4752     }
4753 
4754     // Read and process a record.
4755     Record.clear();
4756     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4757     if (!MaybeRecordType) {
4758       // FIXME this drops the error.
4759       return Failure;
4760     }
4761     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4762     case SIGNATURE:
4763       if (F)
4764         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4765       break;
4766     case AST_BLOCK_HASH:
4767       if (F)
4768         F->ASTBlockHash =
4769             ASTFileSignature::create(Record.begin(), Record.end());
4770       break;
4771     case DIAGNOSTIC_OPTIONS: {
4772       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4773       if (Listener && ValidateDiagnosticOptions &&
4774           !AllowCompatibleConfigurationMismatch &&
4775           ParseDiagnosticOptions(Record, Complain, *Listener))
4776         Result = OutOfDate; // Don't return early.  Read the signature.
4777       break;
4778     }
4779     case DIAG_PRAGMA_MAPPINGS:
4780       if (!F)
4781         break;
4782       if (F->PragmaDiagMappings.empty())
4783         F->PragmaDiagMappings.swap(Record);
4784       else
4785         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4786                                      Record.begin(), Record.end());
4787       break;
4788     }
4789   }
4790 }
4791 
4792 /// Parse a record and blob containing module file extension metadata.
4793 static bool parseModuleFileExtensionMetadata(
4794               const SmallVectorImpl<uint64_t> &Record,
4795               StringRef Blob,
4796               ModuleFileExtensionMetadata &Metadata) {
4797   if (Record.size() < 4) return true;
4798 
4799   Metadata.MajorVersion = Record[0];
4800   Metadata.MinorVersion = Record[1];
4801 
4802   unsigned BlockNameLen = Record[2];
4803   unsigned UserInfoLen = Record[3];
4804 
4805   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4806 
4807   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4808   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4809                                   Blob.data() + BlockNameLen + UserInfoLen);
4810   return false;
4811 }
4812 
4813 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4814   BitstreamCursor &Stream = F.Stream;
4815 
4816   RecordData Record;
4817   while (true) {
4818     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4819     if (!MaybeEntry) {
4820       Error(MaybeEntry.takeError());
4821       return Failure;
4822     }
4823     llvm::BitstreamEntry Entry = MaybeEntry.get();
4824 
4825     switch (Entry.Kind) {
4826     case llvm::BitstreamEntry::SubBlock:
4827       if (llvm::Error Err = Stream.SkipBlock()) {
4828         Error(std::move(Err));
4829         return Failure;
4830       }
4831       continue;
4832 
4833     case llvm::BitstreamEntry::EndBlock:
4834       return Success;
4835 
4836     case llvm::BitstreamEntry::Error:
4837       return HadErrors;
4838 
4839     case llvm::BitstreamEntry::Record:
4840       break;
4841     }
4842 
4843     Record.clear();
4844     StringRef Blob;
4845     Expected<unsigned> MaybeRecCode =
4846         Stream.readRecord(Entry.ID, Record, &Blob);
4847     if (!MaybeRecCode) {
4848       Error(MaybeRecCode.takeError());
4849       return Failure;
4850     }
4851     switch (MaybeRecCode.get()) {
4852     case EXTENSION_METADATA: {
4853       ModuleFileExtensionMetadata Metadata;
4854       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4855         Error("malformed EXTENSION_METADATA in AST file");
4856         return Failure;
4857       }
4858 
4859       // Find a module file extension with this block name.
4860       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4861       if (Known == ModuleFileExtensions.end()) break;
4862 
4863       // Form a reader.
4864       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4865                                                              F, Stream)) {
4866         F.ExtensionReaders.push_back(std::move(Reader));
4867       }
4868 
4869       break;
4870     }
4871     }
4872   }
4873 
4874   return Success;
4875 }
4876 
4877 void ASTReader::InitializeContext() {
4878   assert(ContextObj && "no context to initialize");
4879   ASTContext &Context = *ContextObj;
4880 
4881   // If there's a listener, notify them that we "read" the translation unit.
4882   if (DeserializationListener)
4883     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4884                                       Context.getTranslationUnitDecl());
4885 
4886   // FIXME: Find a better way to deal with collisions between these
4887   // built-in types. Right now, we just ignore the problem.
4888 
4889   // Load the special types.
4890   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4891     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4892       if (!Context.CFConstantStringTypeDecl)
4893         Context.setCFConstantStringType(GetType(String));
4894     }
4895 
4896     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4897       QualType FileType = GetType(File);
4898       if (FileType.isNull()) {
4899         Error("FILE type is NULL");
4900         return;
4901       }
4902 
4903       if (!Context.FILEDecl) {
4904         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4905           Context.setFILEDecl(Typedef->getDecl());
4906         else {
4907           const TagType *Tag = FileType->getAs<TagType>();
4908           if (!Tag) {
4909             Error("Invalid FILE type in AST file");
4910             return;
4911           }
4912           Context.setFILEDecl(Tag->getDecl());
4913         }
4914       }
4915     }
4916 
4917     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4918       QualType Jmp_bufType = GetType(Jmp_buf);
4919       if (Jmp_bufType.isNull()) {
4920         Error("jmp_buf type is NULL");
4921         return;
4922       }
4923 
4924       if (!Context.jmp_bufDecl) {
4925         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4926           Context.setjmp_bufDecl(Typedef->getDecl());
4927         else {
4928           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4929           if (!Tag) {
4930             Error("Invalid jmp_buf type in AST file");
4931             return;
4932           }
4933           Context.setjmp_bufDecl(Tag->getDecl());
4934         }
4935       }
4936     }
4937 
4938     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4939       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4940       if (Sigjmp_bufType.isNull()) {
4941         Error("sigjmp_buf type is NULL");
4942         return;
4943       }
4944 
4945       if (!Context.sigjmp_bufDecl) {
4946         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4947           Context.setsigjmp_bufDecl(Typedef->getDecl());
4948         else {
4949           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4950           assert(Tag && "Invalid sigjmp_buf type in AST file");
4951           Context.setsigjmp_bufDecl(Tag->getDecl());
4952         }
4953       }
4954     }
4955 
4956     if (unsigned ObjCIdRedef
4957           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4958       if (Context.ObjCIdRedefinitionType.isNull())
4959         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4960     }
4961 
4962     if (unsigned ObjCClassRedef
4963           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4964       if (Context.ObjCClassRedefinitionType.isNull())
4965         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4966     }
4967 
4968     if (unsigned ObjCSelRedef
4969           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4970       if (Context.ObjCSelRedefinitionType.isNull())
4971         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4972     }
4973 
4974     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4975       QualType Ucontext_tType = GetType(Ucontext_t);
4976       if (Ucontext_tType.isNull()) {
4977         Error("ucontext_t type is NULL");
4978         return;
4979       }
4980 
4981       if (!Context.ucontext_tDecl) {
4982         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4983           Context.setucontext_tDecl(Typedef->getDecl());
4984         else {
4985           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4986           assert(Tag && "Invalid ucontext_t type in AST file");
4987           Context.setucontext_tDecl(Tag->getDecl());
4988         }
4989       }
4990     }
4991   }
4992 
4993   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4994 
4995   // If there were any CUDA special declarations, deserialize them.
4996   if (!CUDASpecialDeclRefs.empty()) {
4997     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4998     Context.setcudaConfigureCallDecl(
4999                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
5000   }
5001 
5002   // Re-export any modules that were imported by a non-module AST file.
5003   // FIXME: This does not make macro-only imports visible again.
5004   for (auto &Import : ImportedModules) {
5005     if (Module *Imported = getSubmodule(Import.ID)) {
5006       makeModuleVisible(Imported, Module::AllVisible,
5007                         /*ImportLoc=*/Import.ImportLoc);
5008       if (Import.ImportLoc.isValid())
5009         PP.makeModuleVisible(Imported, Import.ImportLoc);
5010       // This updates visibility for Preprocessor only. For Sema, which can be
5011       // nullptr here, we do the same later, in UpdateSema().
5012     }
5013   }
5014 }
5015 
5016 void ASTReader::finalizeForWriting() {
5017   // Nothing to do for now.
5018 }
5019 
5020 /// Reads and return the signature record from \p PCH's control block, or
5021 /// else returns 0.
5022 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5023   BitstreamCursor Stream(PCH);
5024   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5025     // FIXME this drops the error on the floor.
5026     consumeError(std::move(Err));
5027     return ASTFileSignature();
5028   }
5029 
5030   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5031   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5032     return ASTFileSignature();
5033 
5034   // Scan for SIGNATURE inside the diagnostic options block.
5035   ASTReader::RecordData Record;
5036   while (true) {
5037     Expected<llvm::BitstreamEntry> MaybeEntry =
5038         Stream.advanceSkippingSubblocks();
5039     if (!MaybeEntry) {
5040       // FIXME this drops the error on the floor.
5041       consumeError(MaybeEntry.takeError());
5042       return ASTFileSignature();
5043     }
5044     llvm::BitstreamEntry Entry = MaybeEntry.get();
5045 
5046     if (Entry.Kind != llvm::BitstreamEntry::Record)
5047       return ASTFileSignature();
5048 
5049     Record.clear();
5050     StringRef Blob;
5051     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5052     if (!MaybeRecord) {
5053       // FIXME this drops the error on the floor.
5054       consumeError(MaybeRecord.takeError());
5055       return ASTFileSignature();
5056     }
5057     if (SIGNATURE == MaybeRecord.get())
5058       return ASTFileSignature::create(Record.begin(),
5059                                       Record.begin() + ASTFileSignature::size);
5060   }
5061 }
5062 
5063 /// Retrieve the name of the original source file name
5064 /// directly from the AST file, without actually loading the AST
5065 /// file.
5066 std::string ASTReader::getOriginalSourceFile(
5067     const std::string &ASTFileName, FileManager &FileMgr,
5068     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5069   // Open the AST file.
5070   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5071   if (!Buffer) {
5072     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5073         << ASTFileName << Buffer.getError().message();
5074     return std::string();
5075   }
5076 
5077   // Initialize the stream
5078   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5079 
5080   // Sniff for the signature.
5081   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5082     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5083     return std::string();
5084   }
5085 
5086   // Scan for the CONTROL_BLOCK_ID block.
5087   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5088     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5089     return std::string();
5090   }
5091 
5092   // Scan for ORIGINAL_FILE inside the control block.
5093   RecordData Record;
5094   while (true) {
5095     Expected<llvm::BitstreamEntry> MaybeEntry =
5096         Stream.advanceSkippingSubblocks();
5097     if (!MaybeEntry) {
5098       // FIXME this drops errors on the floor.
5099       consumeError(MaybeEntry.takeError());
5100       return std::string();
5101     }
5102     llvm::BitstreamEntry Entry = MaybeEntry.get();
5103 
5104     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5105       return std::string();
5106 
5107     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5108       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5109       return std::string();
5110     }
5111 
5112     Record.clear();
5113     StringRef Blob;
5114     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5115     if (!MaybeRecord) {
5116       // FIXME this drops the errors on the floor.
5117       consumeError(MaybeRecord.takeError());
5118       return std::string();
5119     }
5120     if (ORIGINAL_FILE == MaybeRecord.get())
5121       return Blob.str();
5122   }
5123 }
5124 
5125 namespace {
5126 
5127   class SimplePCHValidator : public ASTReaderListener {
5128     const LangOptions &ExistingLangOpts;
5129     const TargetOptions &ExistingTargetOpts;
5130     const PreprocessorOptions &ExistingPPOpts;
5131     std::string ExistingModuleCachePath;
5132     FileManager &FileMgr;
5133 
5134   public:
5135     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5136                        const TargetOptions &ExistingTargetOpts,
5137                        const PreprocessorOptions &ExistingPPOpts,
5138                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5139         : ExistingLangOpts(ExistingLangOpts),
5140           ExistingTargetOpts(ExistingTargetOpts),
5141           ExistingPPOpts(ExistingPPOpts),
5142           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5143 
5144     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5145                              bool AllowCompatibleDifferences) override {
5146       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5147                                   AllowCompatibleDifferences);
5148     }
5149 
5150     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5151                            bool AllowCompatibleDifferences) override {
5152       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5153                                 AllowCompatibleDifferences);
5154     }
5155 
5156     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5157                                  StringRef SpecificModuleCachePath,
5158                                  bool Complain) override {
5159       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5160                                       ExistingModuleCachePath, nullptr,
5161                                       ExistingLangOpts, ExistingPPOpts);
5162     }
5163 
5164     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5165                                  bool Complain,
5166                                  std::string &SuggestedPredefines) override {
5167       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5168                                       SuggestedPredefines, ExistingLangOpts);
5169     }
5170   };
5171 
5172 } // namespace
5173 
5174 bool ASTReader::readASTFileControlBlock(
5175     StringRef Filename, FileManager &FileMgr,
5176     const PCHContainerReader &PCHContainerRdr,
5177     bool FindModuleFileExtensions,
5178     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5179   // Open the AST file.
5180   // FIXME: This allows use of the VFS; we do not allow use of the
5181   // VFS when actually loading a module.
5182   auto Buffer = FileMgr.getBufferForFile(Filename);
5183   if (!Buffer) {
5184     return true;
5185   }
5186 
5187   // Initialize the stream
5188   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5189   BitstreamCursor Stream(Bytes);
5190 
5191   // Sniff for the signature.
5192   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5193     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5194     return true;
5195   }
5196 
5197   // Scan for the CONTROL_BLOCK_ID block.
5198   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5199     return true;
5200 
5201   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5202   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5203   bool NeedsImports = Listener.needsImportVisitation();
5204   BitstreamCursor InputFilesCursor;
5205 
5206   RecordData Record;
5207   std::string ModuleDir;
5208   bool DoneWithControlBlock = false;
5209   while (!DoneWithControlBlock) {
5210     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5211     if (!MaybeEntry) {
5212       // FIXME this drops the error on the floor.
5213       consumeError(MaybeEntry.takeError());
5214       return true;
5215     }
5216     llvm::BitstreamEntry Entry = MaybeEntry.get();
5217 
5218     switch (Entry.Kind) {
5219     case llvm::BitstreamEntry::SubBlock: {
5220       switch (Entry.ID) {
5221       case OPTIONS_BLOCK_ID: {
5222         std::string IgnoredSuggestedPredefines;
5223         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5224                              /*AllowCompatibleConfigurationMismatch*/ false,
5225                              Listener, IgnoredSuggestedPredefines) != Success)
5226           return true;
5227         break;
5228       }
5229 
5230       case INPUT_FILES_BLOCK_ID:
5231         InputFilesCursor = Stream;
5232         if (llvm::Error Err = Stream.SkipBlock()) {
5233           // FIXME this drops the error on the floor.
5234           consumeError(std::move(Err));
5235           return true;
5236         }
5237         if (NeedsInputFiles &&
5238             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5239           return true;
5240         break;
5241 
5242       default:
5243         if (llvm::Error Err = Stream.SkipBlock()) {
5244           // FIXME this drops the error on the floor.
5245           consumeError(std::move(Err));
5246           return true;
5247         }
5248         break;
5249       }
5250 
5251       continue;
5252     }
5253 
5254     case llvm::BitstreamEntry::EndBlock:
5255       DoneWithControlBlock = true;
5256       break;
5257 
5258     case llvm::BitstreamEntry::Error:
5259       return true;
5260 
5261     case llvm::BitstreamEntry::Record:
5262       break;
5263     }
5264 
5265     if (DoneWithControlBlock) break;
5266 
5267     Record.clear();
5268     StringRef Blob;
5269     Expected<unsigned> MaybeRecCode =
5270         Stream.readRecord(Entry.ID, Record, &Blob);
5271     if (!MaybeRecCode) {
5272       // FIXME this drops the error.
5273       return Failure;
5274     }
5275     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5276     case METADATA:
5277       if (Record[0] != VERSION_MAJOR)
5278         return true;
5279       if (Listener.ReadFullVersionInformation(Blob))
5280         return true;
5281       break;
5282     case MODULE_NAME:
5283       Listener.ReadModuleName(Blob);
5284       break;
5285     case MODULE_DIRECTORY:
5286       ModuleDir = std::string(Blob);
5287       break;
5288     case MODULE_MAP_FILE: {
5289       unsigned Idx = 0;
5290       auto Path = ReadString(Record, Idx);
5291       ResolveImportedPath(Path, ModuleDir);
5292       Listener.ReadModuleMapFile(Path);
5293       break;
5294     }
5295     case INPUT_FILE_OFFSETS: {
5296       if (!NeedsInputFiles)
5297         break;
5298 
5299       unsigned NumInputFiles = Record[0];
5300       unsigned NumUserFiles = Record[1];
5301       const llvm::support::unaligned_uint64_t *InputFileOffs =
5302           (const llvm::support::unaligned_uint64_t *)Blob.data();
5303       for (unsigned I = 0; I != NumInputFiles; ++I) {
5304         // Go find this input file.
5305         bool isSystemFile = I >= NumUserFiles;
5306 
5307         if (isSystemFile && !NeedsSystemInputFiles)
5308           break; // the rest are system input files
5309 
5310         BitstreamCursor &Cursor = InputFilesCursor;
5311         SavedStreamPosition SavedPosition(Cursor);
5312         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5313           // FIXME this drops errors on the floor.
5314           consumeError(std::move(Err));
5315         }
5316 
5317         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5318         if (!MaybeCode) {
5319           // FIXME this drops errors on the floor.
5320           consumeError(MaybeCode.takeError());
5321         }
5322         unsigned Code = MaybeCode.get();
5323 
5324         RecordData Record;
5325         StringRef Blob;
5326         bool shouldContinue = false;
5327         Expected<unsigned> MaybeRecordType =
5328             Cursor.readRecord(Code, Record, &Blob);
5329         if (!MaybeRecordType) {
5330           // FIXME this drops errors on the floor.
5331           consumeError(MaybeRecordType.takeError());
5332         }
5333         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5334         case INPUT_FILE_HASH:
5335           break;
5336         case INPUT_FILE:
5337           bool Overridden = static_cast<bool>(Record[3]);
5338           std::string Filename = std::string(Blob);
5339           ResolveImportedPath(Filename, ModuleDir);
5340           shouldContinue = Listener.visitInputFile(
5341               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5342           break;
5343         }
5344         if (!shouldContinue)
5345           break;
5346       }
5347       break;
5348     }
5349 
5350     case IMPORTS: {
5351       if (!NeedsImports)
5352         break;
5353 
5354       unsigned Idx = 0, N = Record.size();
5355       while (Idx < N) {
5356         // Read information about the AST file.
5357         Idx +=
5358             1 + 1 + 1 + 1 +
5359             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5360         std::string ModuleName = ReadString(Record, Idx);
5361         std::string Filename = ReadString(Record, Idx);
5362         ResolveImportedPath(Filename, ModuleDir);
5363         Listener.visitImport(ModuleName, Filename);
5364       }
5365       break;
5366     }
5367 
5368     default:
5369       // No other validation to perform.
5370       break;
5371     }
5372   }
5373 
5374   // Look for module file extension blocks, if requested.
5375   if (FindModuleFileExtensions) {
5376     BitstreamCursor SavedStream = Stream;
5377     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5378       bool DoneWithExtensionBlock = false;
5379       while (!DoneWithExtensionBlock) {
5380         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5381         if (!MaybeEntry) {
5382           // FIXME this drops the error.
5383           return true;
5384         }
5385         llvm::BitstreamEntry Entry = MaybeEntry.get();
5386 
5387         switch (Entry.Kind) {
5388         case llvm::BitstreamEntry::SubBlock:
5389           if (llvm::Error Err = Stream.SkipBlock()) {
5390             // FIXME this drops the error on the floor.
5391             consumeError(std::move(Err));
5392             return true;
5393           }
5394           continue;
5395 
5396         case llvm::BitstreamEntry::EndBlock:
5397           DoneWithExtensionBlock = true;
5398           continue;
5399 
5400         case llvm::BitstreamEntry::Error:
5401           return true;
5402 
5403         case llvm::BitstreamEntry::Record:
5404           break;
5405         }
5406 
5407        Record.clear();
5408        StringRef Blob;
5409        Expected<unsigned> MaybeRecCode =
5410            Stream.readRecord(Entry.ID, Record, &Blob);
5411        if (!MaybeRecCode) {
5412          // FIXME this drops the error.
5413          return true;
5414        }
5415        switch (MaybeRecCode.get()) {
5416        case EXTENSION_METADATA: {
5417          ModuleFileExtensionMetadata Metadata;
5418          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5419            return true;
5420 
5421          Listener.readModuleFileExtension(Metadata);
5422          break;
5423        }
5424        }
5425       }
5426     }
5427     Stream = SavedStream;
5428   }
5429 
5430   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5431   if (readUnhashedControlBlockImpl(
5432           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5433           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5434           ValidateDiagnosticOptions) != Success)
5435     return true;
5436 
5437   return false;
5438 }
5439 
5440 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5441                                     const PCHContainerReader &PCHContainerRdr,
5442                                     const LangOptions &LangOpts,
5443                                     const TargetOptions &TargetOpts,
5444                                     const PreprocessorOptions &PPOpts,
5445                                     StringRef ExistingModuleCachePath) {
5446   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5447                                ExistingModuleCachePath, FileMgr);
5448   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5449                                   /*FindModuleFileExtensions=*/false,
5450                                   validator,
5451                                   /*ValidateDiagnosticOptions=*/true);
5452 }
5453 
5454 ASTReader::ASTReadResult
5455 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5456   // Enter the submodule block.
5457   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5458     Error(std::move(Err));
5459     return Failure;
5460   }
5461 
5462   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5463   bool First = true;
5464   Module *CurrentModule = nullptr;
5465   RecordData Record;
5466   while (true) {
5467     Expected<llvm::BitstreamEntry> MaybeEntry =
5468         F.Stream.advanceSkippingSubblocks();
5469     if (!MaybeEntry) {
5470       Error(MaybeEntry.takeError());
5471       return Failure;
5472     }
5473     llvm::BitstreamEntry Entry = MaybeEntry.get();
5474 
5475     switch (Entry.Kind) {
5476     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5477     case llvm::BitstreamEntry::Error:
5478       Error("malformed block record in AST file");
5479       return Failure;
5480     case llvm::BitstreamEntry::EndBlock:
5481       return Success;
5482     case llvm::BitstreamEntry::Record:
5483       // The interesting case.
5484       break;
5485     }
5486 
5487     // Read a record.
5488     StringRef Blob;
5489     Record.clear();
5490     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5491     if (!MaybeKind) {
5492       Error(MaybeKind.takeError());
5493       return Failure;
5494     }
5495     unsigned Kind = MaybeKind.get();
5496 
5497     if ((Kind == SUBMODULE_METADATA) != First) {
5498       Error("submodule metadata record should be at beginning of block");
5499       return Failure;
5500     }
5501     First = false;
5502 
5503     // Submodule information is only valid if we have a current module.
5504     // FIXME: Should we error on these cases?
5505     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5506         Kind != SUBMODULE_DEFINITION)
5507       continue;
5508 
5509     switch (Kind) {
5510     default:  // Default behavior: ignore.
5511       break;
5512 
5513     case SUBMODULE_DEFINITION: {
5514       if (Record.size() < 12) {
5515         Error("malformed module definition");
5516         return Failure;
5517       }
5518 
5519       StringRef Name = Blob;
5520       unsigned Idx = 0;
5521       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5522       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5523       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5524       bool IsFramework = Record[Idx++];
5525       bool IsExplicit = Record[Idx++];
5526       bool IsSystem = Record[Idx++];
5527       bool IsExternC = Record[Idx++];
5528       bool InferSubmodules = Record[Idx++];
5529       bool InferExplicitSubmodules = Record[Idx++];
5530       bool InferExportWildcard = Record[Idx++];
5531       bool ConfigMacrosExhaustive = Record[Idx++];
5532       bool ModuleMapIsPrivate = Record[Idx++];
5533 
5534       Module *ParentModule = nullptr;
5535       if (Parent)
5536         ParentModule = getSubmodule(Parent);
5537 
5538       // Retrieve this (sub)module from the module map, creating it if
5539       // necessary.
5540       CurrentModule =
5541           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5542               .first;
5543 
5544       // FIXME: set the definition loc for CurrentModule, or call
5545       // ModMap.setInferredModuleAllowedBy()
5546 
5547       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5548       if (GlobalIndex >= SubmodulesLoaded.size() ||
5549           SubmodulesLoaded[GlobalIndex]) {
5550         Error("too many submodules");
5551         return Failure;
5552       }
5553 
5554       if (!ParentModule) {
5555         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5556           // Don't emit module relocation error if we have -fno-validate-pch
5557           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5558                     DisableValidationForModuleKind::Module) &&
5559               CurFile != F.File) {
5560             Error(diag::err_module_file_conflict,
5561                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5562                   F.File->getName());
5563             return Failure;
5564           }
5565         }
5566 
5567         F.DidReadTopLevelSubmodule = true;
5568         CurrentModule->setASTFile(F.File);
5569         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5570       }
5571 
5572       CurrentModule->Kind = Kind;
5573       CurrentModule->Signature = F.Signature;
5574       CurrentModule->IsFromModuleFile = true;
5575       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5576       CurrentModule->IsExternC = IsExternC;
5577       CurrentModule->InferSubmodules = InferSubmodules;
5578       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5579       CurrentModule->InferExportWildcard = InferExportWildcard;
5580       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5581       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5582       if (DeserializationListener)
5583         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5584 
5585       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5586 
5587       // Clear out data that will be replaced by what is in the module file.
5588       CurrentModule->LinkLibraries.clear();
5589       CurrentModule->ConfigMacros.clear();
5590       CurrentModule->UnresolvedConflicts.clear();
5591       CurrentModule->Conflicts.clear();
5592 
5593       // The module is available unless it's missing a requirement; relevant
5594       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5595       // Missing headers that were present when the module was built do not
5596       // make it unavailable -- if we got this far, this must be an explicitly
5597       // imported module file.
5598       CurrentModule->Requirements.clear();
5599       CurrentModule->MissingHeaders.clear();
5600       CurrentModule->IsUnimportable =
5601           ParentModule && ParentModule->IsUnimportable;
5602       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5603       break;
5604     }
5605 
5606     case SUBMODULE_UMBRELLA_HEADER: {
5607       // FIXME: This doesn't work for framework modules as `Filename` is the
5608       //        name as written in the module file and does not include
5609       //        `Headers/`, so this path will never exist.
5610       std::string Filename = std::string(Blob);
5611       ResolveImportedPath(F, Filename);
5612       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5613         if (!CurrentModule->getUmbrellaHeader()) {
5614           // FIXME: NameAsWritten
5615           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5616         }
5617         // Note that it's too late at this point to return out of date if the
5618         // name from the PCM doesn't match up with the one in the module map,
5619         // but also quite unlikely since we will have already checked the
5620         // modification time and size of the module map file itself.
5621       }
5622       break;
5623     }
5624 
5625     case SUBMODULE_HEADER:
5626     case SUBMODULE_EXCLUDED_HEADER:
5627     case SUBMODULE_PRIVATE_HEADER:
5628       // We lazily associate headers with their modules via the HeaderInfo table.
5629       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5630       // of complete filenames or remove it entirely.
5631       break;
5632 
5633     case SUBMODULE_TEXTUAL_HEADER:
5634     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5635       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5636       // them here.
5637       break;
5638 
5639     case SUBMODULE_TOPHEADER:
5640       CurrentModule->addTopHeaderFilename(Blob);
5641       break;
5642 
5643     case SUBMODULE_UMBRELLA_DIR: {
5644       // See comments in SUBMODULE_UMBRELLA_HEADER
5645       std::string Dirname = std::string(Blob);
5646       ResolveImportedPath(F, Dirname);
5647       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5648         if (!CurrentModule->getUmbrellaDir()) {
5649           // FIXME: NameAsWritten
5650           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5651         }
5652       }
5653       break;
5654     }
5655 
5656     case SUBMODULE_METADATA: {
5657       F.BaseSubmoduleID = getTotalNumSubmodules();
5658       F.LocalNumSubmodules = Record[0];
5659       unsigned LocalBaseSubmoduleID = Record[1];
5660       if (F.LocalNumSubmodules > 0) {
5661         // Introduce the global -> local mapping for submodules within this
5662         // module.
5663         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5664 
5665         // Introduce the local -> global mapping for submodules within this
5666         // module.
5667         F.SubmoduleRemap.insertOrReplace(
5668           std::make_pair(LocalBaseSubmoduleID,
5669                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5670 
5671         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5672       }
5673       break;
5674     }
5675 
5676     case SUBMODULE_IMPORTS:
5677       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5678         UnresolvedModuleRef Unresolved;
5679         Unresolved.File = &F;
5680         Unresolved.Mod = CurrentModule;
5681         Unresolved.ID = Record[Idx];
5682         Unresolved.Kind = UnresolvedModuleRef::Import;
5683         Unresolved.IsWildcard = false;
5684         UnresolvedModuleRefs.push_back(Unresolved);
5685       }
5686       break;
5687 
5688     case SUBMODULE_EXPORTS:
5689       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5690         UnresolvedModuleRef Unresolved;
5691         Unresolved.File = &F;
5692         Unresolved.Mod = CurrentModule;
5693         Unresolved.ID = Record[Idx];
5694         Unresolved.Kind = UnresolvedModuleRef::Export;
5695         Unresolved.IsWildcard = Record[Idx + 1];
5696         UnresolvedModuleRefs.push_back(Unresolved);
5697       }
5698 
5699       // Once we've loaded the set of exports, there's no reason to keep
5700       // the parsed, unresolved exports around.
5701       CurrentModule->UnresolvedExports.clear();
5702       break;
5703 
5704     case SUBMODULE_REQUIRES:
5705       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5706                                     PP.getTargetInfo());
5707       break;
5708 
5709     case SUBMODULE_LINK_LIBRARY:
5710       ModMap.resolveLinkAsDependencies(CurrentModule);
5711       CurrentModule->LinkLibraries.push_back(
5712           Module::LinkLibrary(std::string(Blob), Record[0]));
5713       break;
5714 
5715     case SUBMODULE_CONFIG_MACRO:
5716       CurrentModule->ConfigMacros.push_back(Blob.str());
5717       break;
5718 
5719     case SUBMODULE_CONFLICT: {
5720       UnresolvedModuleRef Unresolved;
5721       Unresolved.File = &F;
5722       Unresolved.Mod = CurrentModule;
5723       Unresolved.ID = Record[0];
5724       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5725       Unresolved.IsWildcard = false;
5726       Unresolved.String = Blob;
5727       UnresolvedModuleRefs.push_back(Unresolved);
5728       break;
5729     }
5730 
5731     case SUBMODULE_INITIALIZERS: {
5732       if (!ContextObj)
5733         break;
5734       SmallVector<uint32_t, 16> Inits;
5735       for (auto &ID : Record)
5736         Inits.push_back(getGlobalDeclID(F, ID));
5737       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5738       break;
5739     }
5740 
5741     case SUBMODULE_EXPORT_AS:
5742       CurrentModule->ExportAsModule = Blob.str();
5743       ModMap.addLinkAsDependency(CurrentModule);
5744       break;
5745     }
5746   }
5747 }
5748 
5749 /// Parse the record that corresponds to a LangOptions data
5750 /// structure.
5751 ///
5752 /// This routine parses the language options from the AST file and then gives
5753 /// them to the AST listener if one is set.
5754 ///
5755 /// \returns true if the listener deems the file unacceptable, false otherwise.
5756 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5757                                      bool Complain,
5758                                      ASTReaderListener &Listener,
5759                                      bool AllowCompatibleDifferences) {
5760   LangOptions LangOpts;
5761   unsigned Idx = 0;
5762 #define LANGOPT(Name, Bits, Default, Description) \
5763   LangOpts.Name = Record[Idx++];
5764 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5765   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5766 #include "clang/Basic/LangOptions.def"
5767 #define SANITIZER(NAME, ID)                                                    \
5768   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5769 #include "clang/Basic/Sanitizers.def"
5770 
5771   for (unsigned N = Record[Idx++]; N; --N)
5772     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5773 
5774   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5775   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5776   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5777 
5778   LangOpts.CurrentModule = ReadString(Record, Idx);
5779 
5780   // Comment options.
5781   for (unsigned N = Record[Idx++]; N; --N) {
5782     LangOpts.CommentOpts.BlockCommandNames.push_back(
5783       ReadString(Record, Idx));
5784   }
5785   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5786 
5787   // OpenMP offloading options.
5788   for (unsigned N = Record[Idx++]; N; --N) {
5789     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5790   }
5791 
5792   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5793 
5794   return Listener.ReadLanguageOptions(LangOpts, Complain,
5795                                       AllowCompatibleDifferences);
5796 }
5797 
5798 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5799                                    ASTReaderListener &Listener,
5800                                    bool AllowCompatibleDifferences) {
5801   unsigned Idx = 0;
5802   TargetOptions TargetOpts;
5803   TargetOpts.Triple = ReadString(Record, Idx);
5804   TargetOpts.CPU = ReadString(Record, Idx);
5805   TargetOpts.TuneCPU = ReadString(Record, Idx);
5806   TargetOpts.ABI = ReadString(Record, Idx);
5807   for (unsigned N = Record[Idx++]; N; --N) {
5808     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5809   }
5810   for (unsigned N = Record[Idx++]; N; --N) {
5811     TargetOpts.Features.push_back(ReadString(Record, Idx));
5812   }
5813 
5814   return Listener.ReadTargetOptions(TargetOpts, Complain,
5815                                     AllowCompatibleDifferences);
5816 }
5817 
5818 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5819                                        ASTReaderListener &Listener) {
5820   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5821   unsigned Idx = 0;
5822 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5823 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5824   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5825 #include "clang/Basic/DiagnosticOptions.def"
5826 
5827   for (unsigned N = Record[Idx++]; N; --N)
5828     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5829   for (unsigned N = Record[Idx++]; N; --N)
5830     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5831 
5832   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5833 }
5834 
5835 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5836                                        ASTReaderListener &Listener) {
5837   FileSystemOptions FSOpts;
5838   unsigned Idx = 0;
5839   FSOpts.WorkingDir = ReadString(Record, Idx);
5840   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5841 }
5842 
5843 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5844                                          bool Complain,
5845                                          ASTReaderListener &Listener) {
5846   HeaderSearchOptions HSOpts;
5847   unsigned Idx = 0;
5848   HSOpts.Sysroot = ReadString(Record, Idx);
5849 
5850   // Include entries.
5851   for (unsigned N = Record[Idx++]; N; --N) {
5852     std::string Path = ReadString(Record, Idx);
5853     frontend::IncludeDirGroup Group
5854       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5855     bool IsFramework = Record[Idx++];
5856     bool IgnoreSysRoot = Record[Idx++];
5857     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5858                                     IgnoreSysRoot);
5859   }
5860 
5861   // System header prefixes.
5862   for (unsigned N = Record[Idx++]; N; --N) {
5863     std::string Prefix = ReadString(Record, Idx);
5864     bool IsSystemHeader = Record[Idx++];
5865     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5866   }
5867 
5868   HSOpts.ResourceDir = ReadString(Record, Idx);
5869   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5870   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5871   HSOpts.DisableModuleHash = Record[Idx++];
5872   HSOpts.ImplicitModuleMaps = Record[Idx++];
5873   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5874   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5875   HSOpts.UseBuiltinIncludes = Record[Idx++];
5876   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5877   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5878   HSOpts.UseLibcxx = Record[Idx++];
5879   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5880 
5881   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5882                                           Complain);
5883 }
5884 
5885 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5886                                          bool Complain,
5887                                          ASTReaderListener &Listener,
5888                                          std::string &SuggestedPredefines) {
5889   PreprocessorOptions PPOpts;
5890   unsigned Idx = 0;
5891 
5892   // Macro definitions/undefs
5893   for (unsigned N = Record[Idx++]; N; --N) {
5894     std::string Macro = ReadString(Record, Idx);
5895     bool IsUndef = Record[Idx++];
5896     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5897   }
5898 
5899   // Includes
5900   for (unsigned N = Record[Idx++]; N; --N) {
5901     PPOpts.Includes.push_back(ReadString(Record, Idx));
5902   }
5903 
5904   // Macro Includes
5905   for (unsigned N = Record[Idx++]; N; --N) {
5906     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5907   }
5908 
5909   PPOpts.UsePredefines = Record[Idx++];
5910   PPOpts.DetailedRecord = Record[Idx++];
5911   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5912   PPOpts.ObjCXXARCStandardLibrary =
5913     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5914   SuggestedPredefines.clear();
5915   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5916                                           SuggestedPredefines);
5917 }
5918 
5919 std::pair<ModuleFile *, unsigned>
5920 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5921   GlobalPreprocessedEntityMapType::iterator
5922   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5923   assert(I != GlobalPreprocessedEntityMap.end() &&
5924          "Corrupted global preprocessed entity map");
5925   ModuleFile *M = I->second;
5926   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5927   return std::make_pair(M, LocalIndex);
5928 }
5929 
5930 llvm::iterator_range<PreprocessingRecord::iterator>
5931 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5932   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5933     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5934                                              Mod.NumPreprocessedEntities);
5935 
5936   return llvm::make_range(PreprocessingRecord::iterator(),
5937                           PreprocessingRecord::iterator());
5938 }
5939 
5940 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
5941                                         unsigned int ClientLoadCapabilities) {
5942   return ClientLoadCapabilities & ARR_OutOfDate &&
5943          !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
5944 }
5945 
5946 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5947 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5948   return llvm::make_range(
5949       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5950       ModuleDeclIterator(this, &Mod,
5951                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5952 }
5953 
5954 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5955   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5956   assert(I != GlobalSkippedRangeMap.end() &&
5957     "Corrupted global skipped range map");
5958   ModuleFile *M = I->second;
5959   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5960   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5961   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5962   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5963                     TranslateSourceLocation(*M, RawRange.getEnd()));
5964   assert(Range.isValid());
5965   return Range;
5966 }
5967 
5968 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5969   PreprocessedEntityID PPID = Index+1;
5970   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5971   ModuleFile &M = *PPInfo.first;
5972   unsigned LocalIndex = PPInfo.second;
5973   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5974 
5975   if (!PP.getPreprocessingRecord()) {
5976     Error("no preprocessing record");
5977     return nullptr;
5978   }
5979 
5980   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5981   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5982           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5983     Error(std::move(Err));
5984     return nullptr;
5985   }
5986 
5987   Expected<llvm::BitstreamEntry> MaybeEntry =
5988       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5989   if (!MaybeEntry) {
5990     Error(MaybeEntry.takeError());
5991     return nullptr;
5992   }
5993   llvm::BitstreamEntry Entry = MaybeEntry.get();
5994 
5995   if (Entry.Kind != llvm::BitstreamEntry::Record)
5996     return nullptr;
5997 
5998   // Read the record.
5999   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
6000                     TranslateSourceLocation(M, PPOffs.getEnd()));
6001   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
6002   StringRef Blob;
6003   RecordData Record;
6004   Expected<unsigned> MaybeRecType =
6005       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
6006   if (!MaybeRecType) {
6007     Error(MaybeRecType.takeError());
6008     return nullptr;
6009   }
6010   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
6011   case PPD_MACRO_EXPANSION: {
6012     bool isBuiltin = Record[0];
6013     IdentifierInfo *Name = nullptr;
6014     MacroDefinitionRecord *Def = nullptr;
6015     if (isBuiltin)
6016       Name = getLocalIdentifier(M, Record[1]);
6017     else {
6018       PreprocessedEntityID GlobalID =
6019           getGlobalPreprocessedEntityID(M, Record[1]);
6020       Def = cast<MacroDefinitionRecord>(
6021           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6022     }
6023 
6024     MacroExpansion *ME;
6025     if (isBuiltin)
6026       ME = new (PPRec) MacroExpansion(Name, Range);
6027     else
6028       ME = new (PPRec) MacroExpansion(Def, Range);
6029 
6030     return ME;
6031   }
6032 
6033   case PPD_MACRO_DEFINITION: {
6034     // Decode the identifier info and then check again; if the macro is
6035     // still defined and associated with the identifier,
6036     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6037     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6038 
6039     if (DeserializationListener)
6040       DeserializationListener->MacroDefinitionRead(PPID, MD);
6041 
6042     return MD;
6043   }
6044 
6045   case PPD_INCLUSION_DIRECTIVE: {
6046     const char *FullFileNameStart = Blob.data() + Record[0];
6047     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6048     const FileEntry *File = nullptr;
6049     if (!FullFileName.empty())
6050       if (auto FE = PP.getFileManager().getFile(FullFileName))
6051         File = *FE;
6052 
6053     // FIXME: Stable encoding
6054     InclusionDirective::InclusionKind Kind
6055       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6056     InclusionDirective *ID
6057       = new (PPRec) InclusionDirective(PPRec, Kind,
6058                                        StringRef(Blob.data(), Record[0]),
6059                                        Record[1], Record[3],
6060                                        File,
6061                                        Range);
6062     return ID;
6063   }
6064   }
6065 
6066   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6067 }
6068 
6069 /// Find the next module that contains entities and return the ID
6070 /// of the first entry.
6071 ///
6072 /// \param SLocMapI points at a chunk of a module that contains no
6073 /// preprocessed entities or the entities it contains are not the ones we are
6074 /// looking for.
6075 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6076                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6077   ++SLocMapI;
6078   for (GlobalSLocOffsetMapType::const_iterator
6079          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6080     ModuleFile &M = *SLocMapI->second;
6081     if (M.NumPreprocessedEntities)
6082       return M.BasePreprocessedEntityID;
6083   }
6084 
6085   return getTotalNumPreprocessedEntities();
6086 }
6087 
6088 namespace {
6089 
6090 struct PPEntityComp {
6091   const ASTReader &Reader;
6092   ModuleFile &M;
6093 
6094   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6095 
6096   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6097     SourceLocation LHS = getLoc(L);
6098     SourceLocation RHS = getLoc(R);
6099     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6100   }
6101 
6102   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6103     SourceLocation LHS = getLoc(L);
6104     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6105   }
6106 
6107   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6108     SourceLocation RHS = getLoc(R);
6109     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6110   }
6111 
6112   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6113     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6114   }
6115 };
6116 
6117 } // namespace
6118 
6119 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6120                                                        bool EndsAfter) const {
6121   if (SourceMgr.isLocalSourceLocation(Loc))
6122     return getTotalNumPreprocessedEntities();
6123 
6124   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6125       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6126   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6127          "Corrupted global sloc offset map");
6128 
6129   if (SLocMapI->second->NumPreprocessedEntities == 0)
6130     return findNextPreprocessedEntity(SLocMapI);
6131 
6132   ModuleFile &M = *SLocMapI->second;
6133 
6134   using pp_iterator = const PPEntityOffset *;
6135 
6136   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6137   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6138 
6139   size_t Count = M.NumPreprocessedEntities;
6140   size_t Half;
6141   pp_iterator First = pp_begin;
6142   pp_iterator PPI;
6143 
6144   if (EndsAfter) {
6145     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6146                            PPEntityComp(*this, M));
6147   } else {
6148     // Do a binary search manually instead of using std::lower_bound because
6149     // The end locations of entities may be unordered (when a macro expansion
6150     // is inside another macro argument), but for this case it is not important
6151     // whether we get the first macro expansion or its containing macro.
6152     while (Count > 0) {
6153       Half = Count / 2;
6154       PPI = First;
6155       std::advance(PPI, Half);
6156       if (SourceMgr.isBeforeInTranslationUnit(
6157               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6158         First = PPI;
6159         ++First;
6160         Count = Count - Half - 1;
6161       } else
6162         Count = Half;
6163     }
6164   }
6165 
6166   if (PPI == pp_end)
6167     return findNextPreprocessedEntity(SLocMapI);
6168 
6169   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6170 }
6171 
6172 /// Returns a pair of [Begin, End) indices of preallocated
6173 /// preprocessed entities that \arg Range encompasses.
6174 std::pair<unsigned, unsigned>
6175     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6176   if (Range.isInvalid())
6177     return std::make_pair(0,0);
6178   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6179 
6180   PreprocessedEntityID BeginID =
6181       findPreprocessedEntity(Range.getBegin(), false);
6182   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6183   return std::make_pair(BeginID, EndID);
6184 }
6185 
6186 /// Optionally returns true or false if the preallocated preprocessed
6187 /// entity with index \arg Index came from file \arg FID.
6188 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6189                                                              FileID FID) {
6190   if (FID.isInvalid())
6191     return false;
6192 
6193   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6194   ModuleFile &M = *PPInfo.first;
6195   unsigned LocalIndex = PPInfo.second;
6196   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6197 
6198   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6199   if (Loc.isInvalid())
6200     return false;
6201 
6202   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6203     return true;
6204   else
6205     return false;
6206 }
6207 
6208 namespace {
6209 
6210   /// Visitor used to search for information about a header file.
6211   class HeaderFileInfoVisitor {
6212     const FileEntry *FE;
6213     Optional<HeaderFileInfo> HFI;
6214 
6215   public:
6216     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6217 
6218     bool operator()(ModuleFile &M) {
6219       HeaderFileInfoLookupTable *Table
6220         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6221       if (!Table)
6222         return false;
6223 
6224       // Look in the on-disk hash table for an entry for this file name.
6225       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6226       if (Pos == Table->end())
6227         return false;
6228 
6229       HFI = *Pos;
6230       return true;
6231     }
6232 
6233     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6234   };
6235 
6236 } // namespace
6237 
6238 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6239   HeaderFileInfoVisitor Visitor(FE);
6240   ModuleMgr.visit(Visitor);
6241   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6242     return *HFI;
6243 
6244   return HeaderFileInfo();
6245 }
6246 
6247 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6248   using DiagState = DiagnosticsEngine::DiagState;
6249   SmallVector<DiagState *, 32> DiagStates;
6250 
6251   for (ModuleFile &F : ModuleMgr) {
6252     unsigned Idx = 0;
6253     auto &Record = F.PragmaDiagMappings;
6254     if (Record.empty())
6255       continue;
6256 
6257     DiagStates.clear();
6258 
6259     auto ReadDiagState =
6260         [&](const DiagState &BasedOn, SourceLocation Loc,
6261             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6262       unsigned BackrefID = Record[Idx++];
6263       if (BackrefID != 0)
6264         return DiagStates[BackrefID - 1];
6265 
6266       // A new DiagState was created here.
6267       Diag.DiagStates.push_back(BasedOn);
6268       DiagState *NewState = &Diag.DiagStates.back();
6269       DiagStates.push_back(NewState);
6270       unsigned Size = Record[Idx++];
6271       assert(Idx + Size * 2 <= Record.size() &&
6272              "Invalid data, not enough diag/map pairs");
6273       while (Size--) {
6274         unsigned DiagID = Record[Idx++];
6275         DiagnosticMapping NewMapping =
6276             DiagnosticMapping::deserialize(Record[Idx++]);
6277         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6278           continue;
6279 
6280         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6281 
6282         // If this mapping was specified as a warning but the severity was
6283         // upgraded due to diagnostic settings, simulate the current diagnostic
6284         // settings (and use a warning).
6285         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6286           NewMapping.setSeverity(diag::Severity::Warning);
6287           NewMapping.setUpgradedFromWarning(false);
6288         }
6289 
6290         Mapping = NewMapping;
6291       }
6292       return NewState;
6293     };
6294 
6295     // Read the first state.
6296     DiagState *FirstState;
6297     if (F.Kind == MK_ImplicitModule) {
6298       // Implicitly-built modules are reused with different diagnostic
6299       // settings.  Use the initial diagnostic state from Diag to simulate this
6300       // compilation's diagnostic settings.
6301       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6302       DiagStates.push_back(FirstState);
6303 
6304       // Skip the initial diagnostic state from the serialized module.
6305       assert(Record[1] == 0 &&
6306              "Invalid data, unexpected backref in initial state");
6307       Idx = 3 + Record[2] * 2;
6308       assert(Idx < Record.size() &&
6309              "Invalid data, not enough state change pairs in initial state");
6310     } else if (F.isModule()) {
6311       // For an explicit module, preserve the flags from the module build
6312       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6313       // -Wblah flags.
6314       unsigned Flags = Record[Idx++];
6315       DiagState Initial;
6316       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6317       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6318       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6319       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6320       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6321       Initial.ExtBehavior = (diag::Severity)Flags;
6322       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6323 
6324       assert(F.OriginalSourceFileID.isValid());
6325 
6326       // Set up the root buffer of the module to start with the initial
6327       // diagnostic state of the module itself, to cover files that contain no
6328       // explicit transitions (for which we did not serialize anything).
6329       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6330           .StateTransitions.push_back({FirstState, 0});
6331     } else {
6332       // For prefix ASTs, start with whatever the user configured on the
6333       // command line.
6334       Idx++; // Skip flags.
6335       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6336                                  SourceLocation(), false);
6337     }
6338 
6339     // Read the state transitions.
6340     unsigned NumLocations = Record[Idx++];
6341     while (NumLocations--) {
6342       assert(Idx < Record.size() &&
6343              "Invalid data, missing pragma diagnostic states");
6344       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6345       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6346       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6347       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6348       unsigned Transitions = Record[Idx++];
6349 
6350       // Note that we don't need to set up Parent/ParentOffset here, because
6351       // we won't be changing the diagnostic state within imported FileIDs
6352       // (other than perhaps appending to the main source file, which has no
6353       // parent).
6354       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6355       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6356       for (unsigned I = 0; I != Transitions; ++I) {
6357         unsigned Offset = Record[Idx++];
6358         auto *State =
6359             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6360         F.StateTransitions.push_back({State, Offset});
6361       }
6362     }
6363 
6364     // Read the final state.
6365     assert(Idx < Record.size() &&
6366            "Invalid data, missing final pragma diagnostic state");
6367     SourceLocation CurStateLoc =
6368         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6369     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6370 
6371     if (!F.isModule()) {
6372       Diag.DiagStatesByLoc.CurDiagState = CurState;
6373       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6374 
6375       // Preserve the property that the imaginary root file describes the
6376       // current state.
6377       FileID NullFile;
6378       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6379       if (T.empty())
6380         T.push_back({CurState, 0});
6381       else
6382         T[0].State = CurState;
6383     }
6384 
6385     // Don't try to read these mappings again.
6386     Record.clear();
6387   }
6388 }
6389 
6390 /// Get the correct cursor and offset for loading a type.
6391 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6392   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6393   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6394   ModuleFile *M = I->second;
6395   return RecordLocation(
6396       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6397              M->DeclsBlockStartOffset);
6398 }
6399 
6400 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6401   switch (code) {
6402 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6403   case TYPE_##CODE_ID: return Type::CLASS_ID;
6404 #include "clang/Serialization/TypeBitCodes.def"
6405   default: return llvm::None;
6406   }
6407 }
6408 
6409 /// Read and return the type with the given index..
6410 ///
6411 /// The index is the type ID, shifted and minus the number of predefs. This
6412 /// routine actually reads the record corresponding to the type at the given
6413 /// location. It is a helper routine for GetType, which deals with reading type
6414 /// IDs.
6415 QualType ASTReader::readTypeRecord(unsigned Index) {
6416   assert(ContextObj && "reading type with no AST context");
6417   ASTContext &Context = *ContextObj;
6418   RecordLocation Loc = TypeCursorForIndex(Index);
6419   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6420 
6421   // Keep track of where we are in the stream, then jump back there
6422   // after reading this type.
6423   SavedStreamPosition SavedPosition(DeclsCursor);
6424 
6425   ReadingKindTracker ReadingKind(Read_Type, *this);
6426 
6427   // Note that we are loading a type record.
6428   Deserializing AType(this);
6429 
6430   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6431     Error(std::move(Err));
6432     return QualType();
6433   }
6434   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6435   if (!RawCode) {
6436     Error(RawCode.takeError());
6437     return QualType();
6438   }
6439 
6440   ASTRecordReader Record(*this, *Loc.F);
6441   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6442   if (!Code) {
6443     Error(Code.takeError());
6444     return QualType();
6445   }
6446   if (Code.get() == TYPE_EXT_QUAL) {
6447     QualType baseType = Record.readQualType();
6448     Qualifiers quals = Record.readQualifiers();
6449     return Context.getQualifiedType(baseType, quals);
6450   }
6451 
6452   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6453   if (!maybeClass) {
6454     Error("Unexpected code for type");
6455     return QualType();
6456   }
6457 
6458   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6459   return TypeReader.read(*maybeClass);
6460 }
6461 
6462 namespace clang {
6463 
6464 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6465   ASTRecordReader &Reader;
6466 
6467   SourceLocation readSourceLocation() {
6468     return Reader.readSourceLocation();
6469   }
6470 
6471   TypeSourceInfo *GetTypeSourceInfo() {
6472     return Reader.readTypeSourceInfo();
6473   }
6474 
6475   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6476     return Reader.readNestedNameSpecifierLoc();
6477   }
6478 
6479   Attr *ReadAttr() {
6480     return Reader.readAttr();
6481   }
6482 
6483 public:
6484   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6485 
6486   // We want compile-time assurance that we've enumerated all of
6487   // these, so unfortunately we have to declare them first, then
6488   // define them out-of-line.
6489 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6490 #define TYPELOC(CLASS, PARENT) \
6491   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6492 #include "clang/AST/TypeLocNodes.def"
6493 
6494   void VisitFunctionTypeLoc(FunctionTypeLoc);
6495   void VisitArrayTypeLoc(ArrayTypeLoc);
6496 };
6497 
6498 } // namespace clang
6499 
6500 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6501   // nothing to do
6502 }
6503 
6504 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6505   TL.setBuiltinLoc(readSourceLocation());
6506   if (TL.needsExtraLocalData()) {
6507     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6508     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6509     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6510     TL.setModeAttr(Reader.readInt());
6511   }
6512 }
6513 
6514 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6515   TL.setNameLoc(readSourceLocation());
6516 }
6517 
6518 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6519   TL.setStarLoc(readSourceLocation());
6520 }
6521 
6522 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6523   // nothing to do
6524 }
6525 
6526 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6527   // nothing to do
6528 }
6529 
6530 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6531   TL.setExpansionLoc(readSourceLocation());
6532 }
6533 
6534 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6535   TL.setCaretLoc(readSourceLocation());
6536 }
6537 
6538 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6539   TL.setAmpLoc(readSourceLocation());
6540 }
6541 
6542 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6543   TL.setAmpAmpLoc(readSourceLocation());
6544 }
6545 
6546 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6547   TL.setStarLoc(readSourceLocation());
6548   TL.setClassTInfo(GetTypeSourceInfo());
6549 }
6550 
6551 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6552   TL.setLBracketLoc(readSourceLocation());
6553   TL.setRBracketLoc(readSourceLocation());
6554   if (Reader.readBool())
6555     TL.setSizeExpr(Reader.readExpr());
6556   else
6557     TL.setSizeExpr(nullptr);
6558 }
6559 
6560 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6561   VisitArrayTypeLoc(TL);
6562 }
6563 
6564 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6565   VisitArrayTypeLoc(TL);
6566 }
6567 
6568 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6569   VisitArrayTypeLoc(TL);
6570 }
6571 
6572 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6573                                             DependentSizedArrayTypeLoc TL) {
6574   VisitArrayTypeLoc(TL);
6575 }
6576 
6577 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6578     DependentAddressSpaceTypeLoc TL) {
6579 
6580     TL.setAttrNameLoc(readSourceLocation());
6581     TL.setAttrOperandParensRange(Reader.readSourceRange());
6582     TL.setAttrExprOperand(Reader.readExpr());
6583 }
6584 
6585 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6586                                         DependentSizedExtVectorTypeLoc TL) {
6587   TL.setNameLoc(readSourceLocation());
6588 }
6589 
6590 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6591   TL.setNameLoc(readSourceLocation());
6592 }
6593 
6594 void TypeLocReader::VisitDependentVectorTypeLoc(
6595     DependentVectorTypeLoc TL) {
6596   TL.setNameLoc(readSourceLocation());
6597 }
6598 
6599 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6600   TL.setNameLoc(readSourceLocation());
6601 }
6602 
6603 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6604   TL.setAttrNameLoc(readSourceLocation());
6605   TL.setAttrOperandParensRange(Reader.readSourceRange());
6606   TL.setAttrRowOperand(Reader.readExpr());
6607   TL.setAttrColumnOperand(Reader.readExpr());
6608 }
6609 
6610 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6611     DependentSizedMatrixTypeLoc TL) {
6612   TL.setAttrNameLoc(readSourceLocation());
6613   TL.setAttrOperandParensRange(Reader.readSourceRange());
6614   TL.setAttrRowOperand(Reader.readExpr());
6615   TL.setAttrColumnOperand(Reader.readExpr());
6616 }
6617 
6618 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6619   TL.setLocalRangeBegin(readSourceLocation());
6620   TL.setLParenLoc(readSourceLocation());
6621   TL.setRParenLoc(readSourceLocation());
6622   TL.setExceptionSpecRange(Reader.readSourceRange());
6623   TL.setLocalRangeEnd(readSourceLocation());
6624   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6625     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6626   }
6627 }
6628 
6629 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6630   VisitFunctionTypeLoc(TL);
6631 }
6632 
6633 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6634   VisitFunctionTypeLoc(TL);
6635 }
6636 
6637 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6638   TL.setNameLoc(readSourceLocation());
6639 }
6640 
6641 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6642   TL.setNameLoc(readSourceLocation());
6643 }
6644 
6645 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6646   TL.setTypeofLoc(readSourceLocation());
6647   TL.setLParenLoc(readSourceLocation());
6648   TL.setRParenLoc(readSourceLocation());
6649 }
6650 
6651 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6652   TL.setTypeofLoc(readSourceLocation());
6653   TL.setLParenLoc(readSourceLocation());
6654   TL.setRParenLoc(readSourceLocation());
6655   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6656 }
6657 
6658 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6659   TL.setNameLoc(readSourceLocation());
6660 }
6661 
6662 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6663   TL.setKWLoc(readSourceLocation());
6664   TL.setLParenLoc(readSourceLocation());
6665   TL.setRParenLoc(readSourceLocation());
6666   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6667 }
6668 
6669 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6670   TL.setNameLoc(readSourceLocation());
6671   if (Reader.readBool()) {
6672     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6673     TL.setTemplateKWLoc(readSourceLocation());
6674     TL.setConceptNameLoc(readSourceLocation());
6675     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6676     TL.setLAngleLoc(readSourceLocation());
6677     TL.setRAngleLoc(readSourceLocation());
6678     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6679       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6680                               TL.getTypePtr()->getArg(i).getKind()));
6681   }
6682 }
6683 
6684 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6685     DeducedTemplateSpecializationTypeLoc TL) {
6686   TL.setTemplateNameLoc(readSourceLocation());
6687 }
6688 
6689 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6690   TL.setNameLoc(readSourceLocation());
6691 }
6692 
6693 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6694   TL.setNameLoc(readSourceLocation());
6695 }
6696 
6697 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6698   TL.setAttr(ReadAttr());
6699 }
6700 
6701 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6702   TL.setNameLoc(readSourceLocation());
6703 }
6704 
6705 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6706                                             SubstTemplateTypeParmTypeLoc TL) {
6707   TL.setNameLoc(readSourceLocation());
6708 }
6709 
6710 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6711                                           SubstTemplateTypeParmPackTypeLoc TL) {
6712   TL.setNameLoc(readSourceLocation());
6713 }
6714 
6715 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6716                                            TemplateSpecializationTypeLoc TL) {
6717   TL.setTemplateKeywordLoc(readSourceLocation());
6718   TL.setTemplateNameLoc(readSourceLocation());
6719   TL.setLAngleLoc(readSourceLocation());
6720   TL.setRAngleLoc(readSourceLocation());
6721   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6722     TL.setArgLocInfo(
6723         i,
6724         Reader.readTemplateArgumentLocInfo(
6725           TL.getTypePtr()->getArg(i).getKind()));
6726 }
6727 
6728 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6729   TL.setLParenLoc(readSourceLocation());
6730   TL.setRParenLoc(readSourceLocation());
6731 }
6732 
6733 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6734   TL.setElaboratedKeywordLoc(readSourceLocation());
6735   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6736 }
6737 
6738 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6739   TL.setNameLoc(readSourceLocation());
6740 }
6741 
6742 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6743   TL.setElaboratedKeywordLoc(readSourceLocation());
6744   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6745   TL.setNameLoc(readSourceLocation());
6746 }
6747 
6748 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6749        DependentTemplateSpecializationTypeLoc TL) {
6750   TL.setElaboratedKeywordLoc(readSourceLocation());
6751   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6752   TL.setTemplateKeywordLoc(readSourceLocation());
6753   TL.setTemplateNameLoc(readSourceLocation());
6754   TL.setLAngleLoc(readSourceLocation());
6755   TL.setRAngleLoc(readSourceLocation());
6756   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6757     TL.setArgLocInfo(
6758         I,
6759         Reader.readTemplateArgumentLocInfo(
6760             TL.getTypePtr()->getArg(I).getKind()));
6761 }
6762 
6763 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6764   TL.setEllipsisLoc(readSourceLocation());
6765 }
6766 
6767 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6768   TL.setNameLoc(readSourceLocation());
6769 }
6770 
6771 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6772   if (TL.getNumProtocols()) {
6773     TL.setProtocolLAngleLoc(readSourceLocation());
6774     TL.setProtocolRAngleLoc(readSourceLocation());
6775   }
6776   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6777     TL.setProtocolLoc(i, readSourceLocation());
6778 }
6779 
6780 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6781   TL.setHasBaseTypeAsWritten(Reader.readBool());
6782   TL.setTypeArgsLAngleLoc(readSourceLocation());
6783   TL.setTypeArgsRAngleLoc(readSourceLocation());
6784   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6785     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6786   TL.setProtocolLAngleLoc(readSourceLocation());
6787   TL.setProtocolRAngleLoc(readSourceLocation());
6788   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6789     TL.setProtocolLoc(i, readSourceLocation());
6790 }
6791 
6792 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6793   TL.setStarLoc(readSourceLocation());
6794 }
6795 
6796 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6797   TL.setKWLoc(readSourceLocation());
6798   TL.setLParenLoc(readSourceLocation());
6799   TL.setRParenLoc(readSourceLocation());
6800 }
6801 
6802 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6803   TL.setKWLoc(readSourceLocation());
6804 }
6805 
6806 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6807   TL.setNameLoc(readSourceLocation());
6808 }
6809 void TypeLocReader::VisitDependentExtIntTypeLoc(
6810     clang::DependentExtIntTypeLoc TL) {
6811   TL.setNameLoc(readSourceLocation());
6812 }
6813 
6814 
6815 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6816   TypeLocReader TLR(*this);
6817   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6818     TLR.Visit(TL);
6819 }
6820 
6821 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6822   QualType InfoTy = readType();
6823   if (InfoTy.isNull())
6824     return nullptr;
6825 
6826   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6827   readTypeLoc(TInfo->getTypeLoc());
6828   return TInfo;
6829 }
6830 
6831 QualType ASTReader::GetType(TypeID ID) {
6832   assert(ContextObj && "reading type with no AST context");
6833   ASTContext &Context = *ContextObj;
6834 
6835   unsigned FastQuals = ID & Qualifiers::FastMask;
6836   unsigned Index = ID >> Qualifiers::FastWidth;
6837 
6838   if (Index < NUM_PREDEF_TYPE_IDS) {
6839     QualType T;
6840     switch ((PredefinedTypeIDs)Index) {
6841     case PREDEF_TYPE_NULL_ID:
6842       return QualType();
6843     case PREDEF_TYPE_VOID_ID:
6844       T = Context.VoidTy;
6845       break;
6846     case PREDEF_TYPE_BOOL_ID:
6847       T = Context.BoolTy;
6848       break;
6849     case PREDEF_TYPE_CHAR_U_ID:
6850     case PREDEF_TYPE_CHAR_S_ID:
6851       // FIXME: Check that the signedness of CharTy is correct!
6852       T = Context.CharTy;
6853       break;
6854     case PREDEF_TYPE_UCHAR_ID:
6855       T = Context.UnsignedCharTy;
6856       break;
6857     case PREDEF_TYPE_USHORT_ID:
6858       T = Context.UnsignedShortTy;
6859       break;
6860     case PREDEF_TYPE_UINT_ID:
6861       T = Context.UnsignedIntTy;
6862       break;
6863     case PREDEF_TYPE_ULONG_ID:
6864       T = Context.UnsignedLongTy;
6865       break;
6866     case PREDEF_TYPE_ULONGLONG_ID:
6867       T = Context.UnsignedLongLongTy;
6868       break;
6869     case PREDEF_TYPE_UINT128_ID:
6870       T = Context.UnsignedInt128Ty;
6871       break;
6872     case PREDEF_TYPE_SCHAR_ID:
6873       T = Context.SignedCharTy;
6874       break;
6875     case PREDEF_TYPE_WCHAR_ID:
6876       T = Context.WCharTy;
6877       break;
6878     case PREDEF_TYPE_SHORT_ID:
6879       T = Context.ShortTy;
6880       break;
6881     case PREDEF_TYPE_INT_ID:
6882       T = Context.IntTy;
6883       break;
6884     case PREDEF_TYPE_LONG_ID:
6885       T = Context.LongTy;
6886       break;
6887     case PREDEF_TYPE_LONGLONG_ID:
6888       T = Context.LongLongTy;
6889       break;
6890     case PREDEF_TYPE_INT128_ID:
6891       T = Context.Int128Ty;
6892       break;
6893     case PREDEF_TYPE_BFLOAT16_ID:
6894       T = Context.BFloat16Ty;
6895       break;
6896     case PREDEF_TYPE_HALF_ID:
6897       T = Context.HalfTy;
6898       break;
6899     case PREDEF_TYPE_FLOAT_ID:
6900       T = Context.FloatTy;
6901       break;
6902     case PREDEF_TYPE_DOUBLE_ID:
6903       T = Context.DoubleTy;
6904       break;
6905     case PREDEF_TYPE_LONGDOUBLE_ID:
6906       T = Context.LongDoubleTy;
6907       break;
6908     case PREDEF_TYPE_SHORT_ACCUM_ID:
6909       T = Context.ShortAccumTy;
6910       break;
6911     case PREDEF_TYPE_ACCUM_ID:
6912       T = Context.AccumTy;
6913       break;
6914     case PREDEF_TYPE_LONG_ACCUM_ID:
6915       T = Context.LongAccumTy;
6916       break;
6917     case PREDEF_TYPE_USHORT_ACCUM_ID:
6918       T = Context.UnsignedShortAccumTy;
6919       break;
6920     case PREDEF_TYPE_UACCUM_ID:
6921       T = Context.UnsignedAccumTy;
6922       break;
6923     case PREDEF_TYPE_ULONG_ACCUM_ID:
6924       T = Context.UnsignedLongAccumTy;
6925       break;
6926     case PREDEF_TYPE_SHORT_FRACT_ID:
6927       T = Context.ShortFractTy;
6928       break;
6929     case PREDEF_TYPE_FRACT_ID:
6930       T = Context.FractTy;
6931       break;
6932     case PREDEF_TYPE_LONG_FRACT_ID:
6933       T = Context.LongFractTy;
6934       break;
6935     case PREDEF_TYPE_USHORT_FRACT_ID:
6936       T = Context.UnsignedShortFractTy;
6937       break;
6938     case PREDEF_TYPE_UFRACT_ID:
6939       T = Context.UnsignedFractTy;
6940       break;
6941     case PREDEF_TYPE_ULONG_FRACT_ID:
6942       T = Context.UnsignedLongFractTy;
6943       break;
6944     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6945       T = Context.SatShortAccumTy;
6946       break;
6947     case PREDEF_TYPE_SAT_ACCUM_ID:
6948       T = Context.SatAccumTy;
6949       break;
6950     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6951       T = Context.SatLongAccumTy;
6952       break;
6953     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6954       T = Context.SatUnsignedShortAccumTy;
6955       break;
6956     case PREDEF_TYPE_SAT_UACCUM_ID:
6957       T = Context.SatUnsignedAccumTy;
6958       break;
6959     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6960       T = Context.SatUnsignedLongAccumTy;
6961       break;
6962     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6963       T = Context.SatShortFractTy;
6964       break;
6965     case PREDEF_TYPE_SAT_FRACT_ID:
6966       T = Context.SatFractTy;
6967       break;
6968     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6969       T = Context.SatLongFractTy;
6970       break;
6971     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6972       T = Context.SatUnsignedShortFractTy;
6973       break;
6974     case PREDEF_TYPE_SAT_UFRACT_ID:
6975       T = Context.SatUnsignedFractTy;
6976       break;
6977     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6978       T = Context.SatUnsignedLongFractTy;
6979       break;
6980     case PREDEF_TYPE_FLOAT16_ID:
6981       T = Context.Float16Ty;
6982       break;
6983     case PREDEF_TYPE_FLOAT128_ID:
6984       T = Context.Float128Ty;
6985       break;
6986     case PREDEF_TYPE_OVERLOAD_ID:
6987       T = Context.OverloadTy;
6988       break;
6989     case PREDEF_TYPE_BOUND_MEMBER:
6990       T = Context.BoundMemberTy;
6991       break;
6992     case PREDEF_TYPE_PSEUDO_OBJECT:
6993       T = Context.PseudoObjectTy;
6994       break;
6995     case PREDEF_TYPE_DEPENDENT_ID:
6996       T = Context.DependentTy;
6997       break;
6998     case PREDEF_TYPE_UNKNOWN_ANY:
6999       T = Context.UnknownAnyTy;
7000       break;
7001     case PREDEF_TYPE_NULLPTR_ID:
7002       T = Context.NullPtrTy;
7003       break;
7004     case PREDEF_TYPE_CHAR8_ID:
7005       T = Context.Char8Ty;
7006       break;
7007     case PREDEF_TYPE_CHAR16_ID:
7008       T = Context.Char16Ty;
7009       break;
7010     case PREDEF_TYPE_CHAR32_ID:
7011       T = Context.Char32Ty;
7012       break;
7013     case PREDEF_TYPE_OBJC_ID:
7014       T = Context.ObjCBuiltinIdTy;
7015       break;
7016     case PREDEF_TYPE_OBJC_CLASS:
7017       T = Context.ObjCBuiltinClassTy;
7018       break;
7019     case PREDEF_TYPE_OBJC_SEL:
7020       T = Context.ObjCBuiltinSelTy;
7021       break;
7022 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7023     case PREDEF_TYPE_##Id##_ID: \
7024       T = Context.SingletonId; \
7025       break;
7026 #include "clang/Basic/OpenCLImageTypes.def"
7027 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7028     case PREDEF_TYPE_##Id##_ID: \
7029       T = Context.Id##Ty; \
7030       break;
7031 #include "clang/Basic/OpenCLExtensionTypes.def"
7032     case PREDEF_TYPE_SAMPLER_ID:
7033       T = Context.OCLSamplerTy;
7034       break;
7035     case PREDEF_TYPE_EVENT_ID:
7036       T = Context.OCLEventTy;
7037       break;
7038     case PREDEF_TYPE_CLK_EVENT_ID:
7039       T = Context.OCLClkEventTy;
7040       break;
7041     case PREDEF_TYPE_QUEUE_ID:
7042       T = Context.OCLQueueTy;
7043       break;
7044     case PREDEF_TYPE_RESERVE_ID_ID:
7045       T = Context.OCLReserveIDTy;
7046       break;
7047     case PREDEF_TYPE_AUTO_DEDUCT:
7048       T = Context.getAutoDeductType();
7049       break;
7050     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7051       T = Context.getAutoRRefDeductType();
7052       break;
7053     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7054       T = Context.ARCUnbridgedCastTy;
7055       break;
7056     case PREDEF_TYPE_BUILTIN_FN:
7057       T = Context.BuiltinFnTy;
7058       break;
7059     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7060       T = Context.IncompleteMatrixIdxTy;
7061       break;
7062     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7063       T = Context.OMPArraySectionTy;
7064       break;
7065     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7066       T = Context.OMPArraySectionTy;
7067       break;
7068     case PREDEF_TYPE_OMP_ITERATOR:
7069       T = Context.OMPIteratorTy;
7070       break;
7071 #define SVE_TYPE(Name, Id, SingletonId) \
7072     case PREDEF_TYPE_##Id##_ID: \
7073       T = Context.SingletonId; \
7074       break;
7075 #include "clang/Basic/AArch64SVEACLETypes.def"
7076 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7077     case PREDEF_TYPE_##Id##_ID: \
7078       T = Context.Id##Ty; \
7079       break;
7080 #include "clang/Basic/PPCTypes.def"
7081 #define RVV_TYPE(Name, Id, SingletonId) \
7082     case PREDEF_TYPE_##Id##_ID: \
7083       T = Context.SingletonId; \
7084       break;
7085 #include "clang/Basic/RISCVVTypes.def"
7086     }
7087 
7088     assert(!T.isNull() && "Unknown predefined type");
7089     return T.withFastQualifiers(FastQuals);
7090   }
7091 
7092   Index -= NUM_PREDEF_TYPE_IDS;
7093   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7094   if (TypesLoaded[Index].isNull()) {
7095     TypesLoaded[Index] = readTypeRecord(Index);
7096     if (TypesLoaded[Index].isNull())
7097       return QualType();
7098 
7099     TypesLoaded[Index]->setFromAST();
7100     if (DeserializationListener)
7101       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7102                                         TypesLoaded[Index]);
7103   }
7104 
7105   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7106 }
7107 
7108 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7109   return GetType(getGlobalTypeID(F, LocalID));
7110 }
7111 
7112 serialization::TypeID
7113 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7114   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7115   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7116 
7117   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7118     return LocalID;
7119 
7120   if (!F.ModuleOffsetMap.empty())
7121     ReadModuleOffsetMap(F);
7122 
7123   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7124     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7125   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7126 
7127   unsigned GlobalIndex = LocalIndex + I->second;
7128   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7129 }
7130 
7131 TemplateArgumentLocInfo
7132 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7133   switch (Kind) {
7134   case TemplateArgument::Expression:
7135     return readExpr();
7136   case TemplateArgument::Type:
7137     return readTypeSourceInfo();
7138   case TemplateArgument::Template: {
7139     NestedNameSpecifierLoc QualifierLoc =
7140       readNestedNameSpecifierLoc();
7141     SourceLocation TemplateNameLoc = readSourceLocation();
7142     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7143                                    TemplateNameLoc, SourceLocation());
7144   }
7145   case TemplateArgument::TemplateExpansion: {
7146     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7147     SourceLocation TemplateNameLoc = readSourceLocation();
7148     SourceLocation EllipsisLoc = readSourceLocation();
7149     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7150                                    TemplateNameLoc, EllipsisLoc);
7151   }
7152   case TemplateArgument::Null:
7153   case TemplateArgument::Integral:
7154   case TemplateArgument::Declaration:
7155   case TemplateArgument::NullPtr:
7156   case TemplateArgument::Pack:
7157     // FIXME: Is this right?
7158     return TemplateArgumentLocInfo();
7159   }
7160   llvm_unreachable("unexpected template argument loc");
7161 }
7162 
7163 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7164   TemplateArgument Arg = readTemplateArgument();
7165 
7166   if (Arg.getKind() == TemplateArgument::Expression) {
7167     if (readBool()) // bool InfoHasSameExpr.
7168       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7169   }
7170   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7171 }
7172 
7173 const ASTTemplateArgumentListInfo *
7174 ASTRecordReader::readASTTemplateArgumentListInfo() {
7175   SourceLocation LAngleLoc = readSourceLocation();
7176   SourceLocation RAngleLoc = readSourceLocation();
7177   unsigned NumArgsAsWritten = readInt();
7178   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7179   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7180     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7181   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7182 }
7183 
7184 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7185   return GetDecl(ID);
7186 }
7187 
7188 void ASTReader::CompleteRedeclChain(const Decl *D) {
7189   if (NumCurrentElementsDeserializing) {
7190     // We arrange to not care about the complete redeclaration chain while we're
7191     // deserializing. Just remember that the AST has marked this one as complete
7192     // but that it's not actually complete yet, so we know we still need to
7193     // complete it later.
7194     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7195     return;
7196   }
7197 
7198   if (!D->getDeclContext()) {
7199     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7200     return;
7201   }
7202 
7203   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7204 
7205   // If this is a named declaration, complete it by looking it up
7206   // within its context.
7207   //
7208   // FIXME: Merging a function definition should merge
7209   // all mergeable entities within it.
7210   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7211       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7212     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7213       if (!getContext().getLangOpts().CPlusPlus &&
7214           isa<TranslationUnitDecl>(DC)) {
7215         // Outside of C++, we don't have a lookup table for the TU, so update
7216         // the identifier instead. (For C++ modules, we don't store decls
7217         // in the serialized identifier table, so we do the lookup in the TU.)
7218         auto *II = Name.getAsIdentifierInfo();
7219         assert(II && "non-identifier name in C?");
7220         if (II->isOutOfDate())
7221           updateOutOfDateIdentifier(*II);
7222       } else
7223         DC->lookup(Name);
7224     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7225       // Find all declarations of this kind from the relevant context.
7226       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7227         auto *DC = cast<DeclContext>(DCDecl);
7228         SmallVector<Decl*, 8> Decls;
7229         FindExternalLexicalDecls(
7230             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7231       }
7232     }
7233   }
7234 
7235   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7236     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7237   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7238     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7239   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7240     if (auto *Template = FD->getPrimaryTemplate())
7241       Template->LoadLazySpecializations();
7242   }
7243 }
7244 
7245 CXXCtorInitializer **
7246 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7247   RecordLocation Loc = getLocalBitOffset(Offset);
7248   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7249   SavedStreamPosition SavedPosition(Cursor);
7250   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7251     Error(std::move(Err));
7252     return nullptr;
7253   }
7254   ReadingKindTracker ReadingKind(Read_Decl, *this);
7255 
7256   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7257   if (!MaybeCode) {
7258     Error(MaybeCode.takeError());
7259     return nullptr;
7260   }
7261   unsigned Code = MaybeCode.get();
7262 
7263   ASTRecordReader Record(*this, *Loc.F);
7264   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7265   if (!MaybeRecCode) {
7266     Error(MaybeRecCode.takeError());
7267     return nullptr;
7268   }
7269   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7270     Error("malformed AST file: missing C++ ctor initializers");
7271     return nullptr;
7272   }
7273 
7274   return Record.readCXXCtorInitializers();
7275 }
7276 
7277 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7278   assert(ContextObj && "reading base specifiers with no AST context");
7279   ASTContext &Context = *ContextObj;
7280 
7281   RecordLocation Loc = getLocalBitOffset(Offset);
7282   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7283   SavedStreamPosition SavedPosition(Cursor);
7284   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7285     Error(std::move(Err));
7286     return nullptr;
7287   }
7288   ReadingKindTracker ReadingKind(Read_Decl, *this);
7289 
7290   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7291   if (!MaybeCode) {
7292     Error(MaybeCode.takeError());
7293     return nullptr;
7294   }
7295   unsigned Code = MaybeCode.get();
7296 
7297   ASTRecordReader Record(*this, *Loc.F);
7298   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7299   if (!MaybeRecCode) {
7300     Error(MaybeCode.takeError());
7301     return nullptr;
7302   }
7303   unsigned RecCode = MaybeRecCode.get();
7304 
7305   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7306     Error("malformed AST file: missing C++ base specifiers");
7307     return nullptr;
7308   }
7309 
7310   unsigned NumBases = Record.readInt();
7311   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7312   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7313   for (unsigned I = 0; I != NumBases; ++I)
7314     Bases[I] = Record.readCXXBaseSpecifier();
7315   return Bases;
7316 }
7317 
7318 serialization::DeclID
7319 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7320   if (LocalID < NUM_PREDEF_DECL_IDS)
7321     return LocalID;
7322 
7323   if (!F.ModuleOffsetMap.empty())
7324     ReadModuleOffsetMap(F);
7325 
7326   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7327     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7328   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7329 
7330   return LocalID + I->second;
7331 }
7332 
7333 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7334                                    ModuleFile &M) const {
7335   // Predefined decls aren't from any module.
7336   if (ID < NUM_PREDEF_DECL_IDS)
7337     return false;
7338 
7339   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7340          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7341 }
7342 
7343 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7344   if (!D->isFromASTFile())
7345     return nullptr;
7346   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7347   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7348   return I->second;
7349 }
7350 
7351 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7352   if (ID < NUM_PREDEF_DECL_IDS)
7353     return SourceLocation();
7354 
7355   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7356 
7357   if (Index > DeclsLoaded.size()) {
7358     Error("declaration ID out-of-range for AST file");
7359     return SourceLocation();
7360   }
7361 
7362   if (Decl *D = DeclsLoaded[Index])
7363     return D->getLocation();
7364 
7365   SourceLocation Loc;
7366   DeclCursorForID(ID, Loc);
7367   return Loc;
7368 }
7369 
7370 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7371   switch (ID) {
7372   case PREDEF_DECL_NULL_ID:
7373     return nullptr;
7374 
7375   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7376     return Context.getTranslationUnitDecl();
7377 
7378   case PREDEF_DECL_OBJC_ID_ID:
7379     return Context.getObjCIdDecl();
7380 
7381   case PREDEF_DECL_OBJC_SEL_ID:
7382     return Context.getObjCSelDecl();
7383 
7384   case PREDEF_DECL_OBJC_CLASS_ID:
7385     return Context.getObjCClassDecl();
7386 
7387   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7388     return Context.getObjCProtocolDecl();
7389 
7390   case PREDEF_DECL_INT_128_ID:
7391     return Context.getInt128Decl();
7392 
7393   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7394     return Context.getUInt128Decl();
7395 
7396   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7397     return Context.getObjCInstanceTypeDecl();
7398 
7399   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7400     return Context.getBuiltinVaListDecl();
7401 
7402   case PREDEF_DECL_VA_LIST_TAG:
7403     return Context.getVaListTagDecl();
7404 
7405   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7406     return Context.getBuiltinMSVaListDecl();
7407 
7408   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7409     return Context.getMSGuidTagDecl();
7410 
7411   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7412     return Context.getExternCContextDecl();
7413 
7414   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7415     return Context.getMakeIntegerSeqDecl();
7416 
7417   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7418     return Context.getCFConstantStringDecl();
7419 
7420   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7421     return Context.getCFConstantStringTagDecl();
7422 
7423   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7424     return Context.getTypePackElementDecl();
7425   }
7426   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7427 }
7428 
7429 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7430   assert(ContextObj && "reading decl with no AST context");
7431   if (ID < NUM_PREDEF_DECL_IDS) {
7432     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7433     if (D) {
7434       // Track that we have merged the declaration with ID \p ID into the
7435       // pre-existing predefined declaration \p D.
7436       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7437       if (Merged.empty())
7438         Merged.push_back(ID);
7439     }
7440     return D;
7441   }
7442 
7443   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7444 
7445   if (Index >= DeclsLoaded.size()) {
7446     assert(0 && "declaration ID out-of-range for AST file");
7447     Error("declaration ID out-of-range for AST file");
7448     return nullptr;
7449   }
7450 
7451   return DeclsLoaded[Index];
7452 }
7453 
7454 Decl *ASTReader::GetDecl(DeclID ID) {
7455   if (ID < NUM_PREDEF_DECL_IDS)
7456     return GetExistingDecl(ID);
7457 
7458   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7459 
7460   if (Index >= DeclsLoaded.size()) {
7461     assert(0 && "declaration ID out-of-range for AST file");
7462     Error("declaration ID out-of-range for AST file");
7463     return nullptr;
7464   }
7465 
7466   if (!DeclsLoaded[Index]) {
7467     ReadDeclRecord(ID);
7468     if (DeserializationListener)
7469       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7470   }
7471 
7472   return DeclsLoaded[Index];
7473 }
7474 
7475 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7476                                                   DeclID GlobalID) {
7477   if (GlobalID < NUM_PREDEF_DECL_IDS)
7478     return GlobalID;
7479 
7480   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7481   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7482   ModuleFile *Owner = I->second;
7483 
7484   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7485     = M.GlobalToLocalDeclIDs.find(Owner);
7486   if (Pos == M.GlobalToLocalDeclIDs.end())
7487     return 0;
7488 
7489   return GlobalID - Owner->BaseDeclID + Pos->second;
7490 }
7491 
7492 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7493                                             const RecordData &Record,
7494                                             unsigned &Idx) {
7495   if (Idx >= Record.size()) {
7496     Error("Corrupted AST file");
7497     return 0;
7498   }
7499 
7500   return getGlobalDeclID(F, Record[Idx++]);
7501 }
7502 
7503 /// Resolve the offset of a statement into a statement.
7504 ///
7505 /// This operation will read a new statement from the external
7506 /// source each time it is called, and is meant to be used via a
7507 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7508 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7509   // Switch case IDs are per Decl.
7510   ClearSwitchCaseIDs();
7511 
7512   // Offset here is a global offset across the entire chain.
7513   RecordLocation Loc = getLocalBitOffset(Offset);
7514   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7515     Error(std::move(Err));
7516     return nullptr;
7517   }
7518   assert(NumCurrentElementsDeserializing == 0 &&
7519          "should not be called while already deserializing");
7520   Deserializing D(this);
7521   return ReadStmtFromStream(*Loc.F);
7522 }
7523 
7524 void ASTReader::FindExternalLexicalDecls(
7525     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7526     SmallVectorImpl<Decl *> &Decls) {
7527   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7528 
7529   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7530     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7531     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7532       auto K = (Decl::Kind)+LexicalDecls[I];
7533       if (!IsKindWeWant(K))
7534         continue;
7535 
7536       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7537 
7538       // Don't add predefined declarations to the lexical context more
7539       // than once.
7540       if (ID < NUM_PREDEF_DECL_IDS) {
7541         if (PredefsVisited[ID])
7542           continue;
7543 
7544         PredefsVisited[ID] = true;
7545       }
7546 
7547       if (Decl *D = GetLocalDecl(*M, ID)) {
7548         assert(D->getKind() == K && "wrong kind for lexical decl");
7549         if (!DC->isDeclInLexicalTraversal(D))
7550           Decls.push_back(D);
7551       }
7552     }
7553   };
7554 
7555   if (isa<TranslationUnitDecl>(DC)) {
7556     for (auto Lexical : TULexicalDecls)
7557       Visit(Lexical.first, Lexical.second);
7558   } else {
7559     auto I = LexicalDecls.find(DC);
7560     if (I != LexicalDecls.end())
7561       Visit(I->second.first, I->second.second);
7562   }
7563 
7564   ++NumLexicalDeclContextsRead;
7565 }
7566 
7567 namespace {
7568 
7569 class DeclIDComp {
7570   ASTReader &Reader;
7571   ModuleFile &Mod;
7572 
7573 public:
7574   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7575 
7576   bool operator()(LocalDeclID L, LocalDeclID R) const {
7577     SourceLocation LHS = getLocation(L);
7578     SourceLocation RHS = getLocation(R);
7579     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7580   }
7581 
7582   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7583     SourceLocation RHS = getLocation(R);
7584     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7585   }
7586 
7587   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7588     SourceLocation LHS = getLocation(L);
7589     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7590   }
7591 
7592   SourceLocation getLocation(LocalDeclID ID) const {
7593     return Reader.getSourceManager().getFileLoc(
7594             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7595   }
7596 };
7597 
7598 } // namespace
7599 
7600 void ASTReader::FindFileRegionDecls(FileID File,
7601                                     unsigned Offset, unsigned Length,
7602                                     SmallVectorImpl<Decl *> &Decls) {
7603   SourceManager &SM = getSourceManager();
7604 
7605   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7606   if (I == FileDeclIDs.end())
7607     return;
7608 
7609   FileDeclsInfo &DInfo = I->second;
7610   if (DInfo.Decls.empty())
7611     return;
7612 
7613   SourceLocation
7614     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7615   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7616 
7617   DeclIDComp DIDComp(*this, *DInfo.Mod);
7618   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7619       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7620   if (BeginIt != DInfo.Decls.begin())
7621     --BeginIt;
7622 
7623   // If we are pointing at a top-level decl inside an objc container, we need
7624   // to backtrack until we find it otherwise we will fail to report that the
7625   // region overlaps with an objc container.
7626   while (BeginIt != DInfo.Decls.begin() &&
7627          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7628              ->isTopLevelDeclInObjCContainer())
7629     --BeginIt;
7630 
7631   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7632       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7633   if (EndIt != DInfo.Decls.end())
7634     ++EndIt;
7635 
7636   for (ArrayRef<serialization::LocalDeclID>::iterator
7637          DIt = BeginIt; DIt != EndIt; ++DIt)
7638     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7639 }
7640 
7641 bool
7642 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7643                                           DeclarationName Name) {
7644   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7645          "DeclContext has no visible decls in storage");
7646   if (!Name)
7647     return false;
7648 
7649   auto It = Lookups.find(DC);
7650   if (It == Lookups.end())
7651     return false;
7652 
7653   Deserializing LookupResults(this);
7654 
7655   // Load the list of declarations.
7656   SmallVector<NamedDecl *, 64> Decls;
7657   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7658   for (DeclID ID : It->second.Table.find(Name)) {
7659     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7660     if (ND->getDeclName() == Name && Found.insert(ND).second)
7661       Decls.push_back(ND);
7662   }
7663 
7664   ++NumVisibleDeclContextsRead;
7665   SetExternalVisibleDeclsForName(DC, Name, Decls);
7666   return !Decls.empty();
7667 }
7668 
7669 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7670   if (!DC->hasExternalVisibleStorage())
7671     return;
7672 
7673   auto It = Lookups.find(DC);
7674   assert(It != Lookups.end() &&
7675          "have external visible storage but no lookup tables");
7676 
7677   DeclsMap Decls;
7678 
7679   for (DeclID ID : It->second.Table.findAll()) {
7680     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7681     Decls[ND->getDeclName()].push_back(ND);
7682   }
7683 
7684   ++NumVisibleDeclContextsRead;
7685 
7686   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7687     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7688   }
7689   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7690 }
7691 
7692 const serialization::reader::DeclContextLookupTable *
7693 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7694   auto I = Lookups.find(Primary);
7695   return I == Lookups.end() ? nullptr : &I->second;
7696 }
7697 
7698 /// Under non-PCH compilation the consumer receives the objc methods
7699 /// before receiving the implementation, and codegen depends on this.
7700 /// We simulate this by deserializing and passing to consumer the methods of the
7701 /// implementation before passing the deserialized implementation decl.
7702 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7703                                        ASTConsumer *Consumer) {
7704   assert(ImplD && Consumer);
7705 
7706   for (auto *I : ImplD->methods())
7707     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7708 
7709   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7710 }
7711 
7712 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7713   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7714     PassObjCImplDeclToConsumer(ImplD, Consumer);
7715   else
7716     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7717 }
7718 
7719 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7720   this->Consumer = Consumer;
7721 
7722   if (Consumer)
7723     PassInterestingDeclsToConsumer();
7724 
7725   if (DeserializationListener)
7726     DeserializationListener->ReaderInitialized(this);
7727 }
7728 
7729 void ASTReader::PrintStats() {
7730   std::fprintf(stderr, "*** AST File Statistics:\n");
7731 
7732   unsigned NumTypesLoaded
7733     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7734                                       QualType());
7735   unsigned NumDeclsLoaded
7736     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7737                                       (Decl *)nullptr);
7738   unsigned NumIdentifiersLoaded
7739     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7740                                             IdentifiersLoaded.end(),
7741                                             (IdentifierInfo *)nullptr);
7742   unsigned NumMacrosLoaded
7743     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7744                                        MacrosLoaded.end(),
7745                                        (MacroInfo *)nullptr);
7746   unsigned NumSelectorsLoaded
7747     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7748                                           SelectorsLoaded.end(),
7749                                           Selector());
7750 
7751   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7752     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7753                  NumSLocEntriesRead, TotalNumSLocEntries,
7754                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7755   if (!TypesLoaded.empty())
7756     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7757                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7758                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7759   if (!DeclsLoaded.empty())
7760     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7761                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7762                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7763   if (!IdentifiersLoaded.empty())
7764     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7765                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7766                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7767   if (!MacrosLoaded.empty())
7768     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7769                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7770                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7771   if (!SelectorsLoaded.empty())
7772     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7773                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7774                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7775   if (TotalNumStatements)
7776     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7777                  NumStatementsRead, TotalNumStatements,
7778                  ((float)NumStatementsRead/TotalNumStatements * 100));
7779   if (TotalNumMacros)
7780     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7781                  NumMacrosRead, TotalNumMacros,
7782                  ((float)NumMacrosRead/TotalNumMacros * 100));
7783   if (TotalLexicalDeclContexts)
7784     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7785                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7786                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7787                   * 100));
7788   if (TotalVisibleDeclContexts)
7789     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7790                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7791                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7792                   * 100));
7793   if (TotalNumMethodPoolEntries)
7794     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7795                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7796                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7797                   * 100));
7798   if (NumMethodPoolLookups)
7799     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7800                  NumMethodPoolHits, NumMethodPoolLookups,
7801                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7802   if (NumMethodPoolTableLookups)
7803     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7804                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7805                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7806                   * 100.0));
7807   if (NumIdentifierLookupHits)
7808     std::fprintf(stderr,
7809                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7810                  NumIdentifierLookupHits, NumIdentifierLookups,
7811                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7812 
7813   if (GlobalIndex) {
7814     std::fprintf(stderr, "\n");
7815     GlobalIndex->printStats();
7816   }
7817 
7818   std::fprintf(stderr, "\n");
7819   dump();
7820   std::fprintf(stderr, "\n");
7821 }
7822 
7823 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7824 LLVM_DUMP_METHOD static void
7825 dumpModuleIDMap(StringRef Name,
7826                 const ContinuousRangeMap<Key, ModuleFile *,
7827                                          InitialCapacity> &Map) {
7828   if (Map.begin() == Map.end())
7829     return;
7830 
7831   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7832 
7833   llvm::errs() << Name << ":\n";
7834   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7835        I != IEnd; ++I) {
7836     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7837       << "\n";
7838   }
7839 }
7840 
7841 LLVM_DUMP_METHOD void ASTReader::dump() {
7842   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7843   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7844   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7845   dumpModuleIDMap("Global type map", GlobalTypeMap);
7846   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7847   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7848   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7849   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7850   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7851   dumpModuleIDMap("Global preprocessed entity map",
7852                   GlobalPreprocessedEntityMap);
7853 
7854   llvm::errs() << "\n*** PCH/Modules Loaded:";
7855   for (ModuleFile &M : ModuleMgr)
7856     M.dump();
7857 }
7858 
7859 /// Return the amount of memory used by memory buffers, breaking down
7860 /// by heap-backed versus mmap'ed memory.
7861 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7862   for (ModuleFile &I : ModuleMgr) {
7863     if (llvm::MemoryBuffer *buf = I.Buffer) {
7864       size_t bytes = buf->getBufferSize();
7865       switch (buf->getBufferKind()) {
7866         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7867           sizes.malloc_bytes += bytes;
7868           break;
7869         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7870           sizes.mmap_bytes += bytes;
7871           break;
7872       }
7873     }
7874   }
7875 }
7876 
7877 void ASTReader::InitializeSema(Sema &S) {
7878   SemaObj = &S;
7879   S.addExternalSource(this);
7880 
7881   // Makes sure any declarations that were deserialized "too early"
7882   // still get added to the identifier's declaration chains.
7883   for (uint64_t ID : PreloadedDeclIDs) {
7884     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7885     pushExternalDeclIntoScope(D, D->getDeclName());
7886   }
7887   PreloadedDeclIDs.clear();
7888 
7889   // FIXME: What happens if these are changed by a module import?
7890   if (!FPPragmaOptions.empty()) {
7891     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7892     FPOptionsOverride NewOverrides =
7893         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7894     SemaObj->CurFPFeatures =
7895         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7896   }
7897 
7898   SemaObj->OpenCLFeatures = OpenCLExtensions;
7899 
7900   UpdateSema();
7901 }
7902 
7903 void ASTReader::UpdateSema() {
7904   assert(SemaObj && "no Sema to update");
7905 
7906   // Load the offsets of the declarations that Sema references.
7907   // They will be lazily deserialized when needed.
7908   if (!SemaDeclRefs.empty()) {
7909     assert(SemaDeclRefs.size() % 3 == 0);
7910     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7911       if (!SemaObj->StdNamespace)
7912         SemaObj->StdNamespace = SemaDeclRefs[I];
7913       if (!SemaObj->StdBadAlloc)
7914         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7915       if (!SemaObj->StdAlignValT)
7916         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7917     }
7918     SemaDeclRefs.clear();
7919   }
7920 
7921   // Update the state of pragmas. Use the same API as if we had encountered the
7922   // pragma in the source.
7923   if(OptimizeOffPragmaLocation.isValid())
7924     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7925   if (PragmaMSStructState != -1)
7926     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7927   if (PointersToMembersPragmaLocation.isValid()) {
7928     SemaObj->ActOnPragmaMSPointersToMembers(
7929         (LangOptions::PragmaMSPointersToMembersKind)
7930             PragmaMSPointersToMembersState,
7931         PointersToMembersPragmaLocation);
7932   }
7933   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7934 
7935   if (PragmaAlignPackCurrentValue) {
7936     // The bottom of the stack might have a default value. It must be adjusted
7937     // to the current value to ensure that the packing state is preserved after
7938     // popping entries that were included/imported from a PCH/module.
7939     bool DropFirst = false;
7940     if (!PragmaAlignPackStack.empty() &&
7941         PragmaAlignPackStack.front().Location.isInvalid()) {
7942       assert(PragmaAlignPackStack.front().Value ==
7943                  SemaObj->AlignPackStack.DefaultValue &&
7944              "Expected a default alignment value");
7945       SemaObj->AlignPackStack.Stack.emplace_back(
7946           PragmaAlignPackStack.front().SlotLabel,
7947           SemaObj->AlignPackStack.CurrentValue,
7948           SemaObj->AlignPackStack.CurrentPragmaLocation,
7949           PragmaAlignPackStack.front().PushLocation);
7950       DropFirst = true;
7951     }
7952     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7953                                  .drop_front(DropFirst ? 1 : 0)) {
7954       SemaObj->AlignPackStack.Stack.emplace_back(
7955           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7956     }
7957     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7958       assert(*PragmaAlignPackCurrentValue ==
7959                  SemaObj->AlignPackStack.DefaultValue &&
7960              "Expected a default align and pack value");
7961       // Keep the current values.
7962     } else {
7963       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7964       SemaObj->AlignPackStack.CurrentPragmaLocation =
7965           PragmaAlignPackCurrentLocation;
7966     }
7967   }
7968   if (FpPragmaCurrentValue) {
7969     // The bottom of the stack might have a default value. It must be adjusted
7970     // to the current value to ensure that fp-pragma state is preserved after
7971     // popping entries that were included/imported from a PCH/module.
7972     bool DropFirst = false;
7973     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7974       assert(FpPragmaStack.front().Value ==
7975                  SemaObj->FpPragmaStack.DefaultValue &&
7976              "Expected a default pragma float_control value");
7977       SemaObj->FpPragmaStack.Stack.emplace_back(
7978           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7979           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7980           FpPragmaStack.front().PushLocation);
7981       DropFirst = true;
7982     }
7983     for (const auto &Entry :
7984          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7985       SemaObj->FpPragmaStack.Stack.emplace_back(
7986           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7987     if (FpPragmaCurrentLocation.isInvalid()) {
7988       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7989              "Expected a default pragma float_control value");
7990       // Keep the current values.
7991     } else {
7992       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7993       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7994     }
7995   }
7996 
7997   // For non-modular AST files, restore visiblity of modules.
7998   for (auto &Import : ImportedModules) {
7999     if (Import.ImportLoc.isInvalid())
8000       continue;
8001     if (Module *Imported = getSubmodule(Import.ID)) {
8002       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
8003     }
8004   }
8005 }
8006 
8007 IdentifierInfo *ASTReader::get(StringRef Name) {
8008   // Note that we are loading an identifier.
8009   Deserializing AnIdentifier(this);
8010 
8011   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
8012                                   NumIdentifierLookups,
8013                                   NumIdentifierLookupHits);
8014 
8015   // We don't need to do identifier table lookups in C++ modules (we preload
8016   // all interesting declarations, and don't need to use the scope for name
8017   // lookups). Perform the lookup in PCH files, though, since we don't build
8018   // a complete initial identifier table if we're carrying on from a PCH.
8019   if (PP.getLangOpts().CPlusPlus) {
8020     for (auto F : ModuleMgr.pch_modules())
8021       if (Visitor(*F))
8022         break;
8023   } else {
8024     // If there is a global index, look there first to determine which modules
8025     // provably do not have any results for this identifier.
8026     GlobalModuleIndex::HitSet Hits;
8027     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8028     if (!loadGlobalIndex()) {
8029       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8030         HitsPtr = &Hits;
8031       }
8032     }
8033 
8034     ModuleMgr.visit(Visitor, HitsPtr);
8035   }
8036 
8037   IdentifierInfo *II = Visitor.getIdentifierInfo();
8038   markIdentifierUpToDate(II);
8039   return II;
8040 }
8041 
8042 namespace clang {
8043 
8044   /// An identifier-lookup iterator that enumerates all of the
8045   /// identifiers stored within a set of AST files.
8046   class ASTIdentifierIterator : public IdentifierIterator {
8047     /// The AST reader whose identifiers are being enumerated.
8048     const ASTReader &Reader;
8049 
8050     /// The current index into the chain of AST files stored in
8051     /// the AST reader.
8052     unsigned Index;
8053 
8054     /// The current position within the identifier lookup table
8055     /// of the current AST file.
8056     ASTIdentifierLookupTable::key_iterator Current;
8057 
8058     /// The end position within the identifier lookup table of
8059     /// the current AST file.
8060     ASTIdentifierLookupTable::key_iterator End;
8061 
8062     /// Whether to skip any modules in the ASTReader.
8063     bool SkipModules;
8064 
8065   public:
8066     explicit ASTIdentifierIterator(const ASTReader &Reader,
8067                                    bool SkipModules = false);
8068 
8069     StringRef Next() override;
8070   };
8071 
8072 } // namespace clang
8073 
8074 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8075                                              bool SkipModules)
8076     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8077 }
8078 
8079 StringRef ASTIdentifierIterator::Next() {
8080   while (Current == End) {
8081     // If we have exhausted all of our AST files, we're done.
8082     if (Index == 0)
8083       return StringRef();
8084 
8085     --Index;
8086     ModuleFile &F = Reader.ModuleMgr[Index];
8087     if (SkipModules && F.isModule())
8088       continue;
8089 
8090     ASTIdentifierLookupTable *IdTable =
8091         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8092     Current = IdTable->key_begin();
8093     End = IdTable->key_end();
8094   }
8095 
8096   // We have any identifiers remaining in the current AST file; return
8097   // the next one.
8098   StringRef Result = *Current;
8099   ++Current;
8100   return Result;
8101 }
8102 
8103 namespace {
8104 
8105 /// A utility for appending two IdentifierIterators.
8106 class ChainedIdentifierIterator : public IdentifierIterator {
8107   std::unique_ptr<IdentifierIterator> Current;
8108   std::unique_ptr<IdentifierIterator> Queued;
8109 
8110 public:
8111   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8112                             std::unique_ptr<IdentifierIterator> Second)
8113       : Current(std::move(First)), Queued(std::move(Second)) {}
8114 
8115   StringRef Next() override {
8116     if (!Current)
8117       return StringRef();
8118 
8119     StringRef result = Current->Next();
8120     if (!result.empty())
8121       return result;
8122 
8123     // Try the queued iterator, which may itself be empty.
8124     Current.reset();
8125     std::swap(Current, Queued);
8126     return Next();
8127   }
8128 };
8129 
8130 } // namespace
8131 
8132 IdentifierIterator *ASTReader::getIdentifiers() {
8133   if (!loadGlobalIndex()) {
8134     std::unique_ptr<IdentifierIterator> ReaderIter(
8135         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8136     std::unique_ptr<IdentifierIterator> ModulesIter(
8137         GlobalIndex->createIdentifierIterator());
8138     return new ChainedIdentifierIterator(std::move(ReaderIter),
8139                                          std::move(ModulesIter));
8140   }
8141 
8142   return new ASTIdentifierIterator(*this);
8143 }
8144 
8145 namespace clang {
8146 namespace serialization {
8147 
8148   class ReadMethodPoolVisitor {
8149     ASTReader &Reader;
8150     Selector Sel;
8151     unsigned PriorGeneration;
8152     unsigned InstanceBits = 0;
8153     unsigned FactoryBits = 0;
8154     bool InstanceHasMoreThanOneDecl = false;
8155     bool FactoryHasMoreThanOneDecl = false;
8156     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8157     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8158 
8159   public:
8160     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8161                           unsigned PriorGeneration)
8162         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8163 
8164     bool operator()(ModuleFile &M) {
8165       if (!M.SelectorLookupTable)
8166         return false;
8167 
8168       // If we've already searched this module file, skip it now.
8169       if (M.Generation <= PriorGeneration)
8170         return true;
8171 
8172       ++Reader.NumMethodPoolTableLookups;
8173       ASTSelectorLookupTable *PoolTable
8174         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8175       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8176       if (Pos == PoolTable->end())
8177         return false;
8178 
8179       ++Reader.NumMethodPoolTableHits;
8180       ++Reader.NumSelectorsRead;
8181       // FIXME: Not quite happy with the statistics here. We probably should
8182       // disable this tracking when called via LoadSelector.
8183       // Also, should entries without methods count as misses?
8184       ++Reader.NumMethodPoolEntriesRead;
8185       ASTSelectorLookupTrait::data_type Data = *Pos;
8186       if (Reader.DeserializationListener)
8187         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8188 
8189       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8190       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8191       InstanceBits = Data.InstanceBits;
8192       FactoryBits = Data.FactoryBits;
8193       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8194       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8195       return true;
8196     }
8197 
8198     /// Retrieve the instance methods found by this visitor.
8199     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8200       return InstanceMethods;
8201     }
8202 
8203     /// Retrieve the instance methods found by this visitor.
8204     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8205       return FactoryMethods;
8206     }
8207 
8208     unsigned getInstanceBits() const { return InstanceBits; }
8209     unsigned getFactoryBits() const { return FactoryBits; }
8210 
8211     bool instanceHasMoreThanOneDecl() const {
8212       return InstanceHasMoreThanOneDecl;
8213     }
8214 
8215     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8216   };
8217 
8218 } // namespace serialization
8219 } // namespace clang
8220 
8221 /// Add the given set of methods to the method list.
8222 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8223                              ObjCMethodList &List) {
8224   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8225     S.addMethodToGlobalList(&List, Methods[I]);
8226   }
8227 }
8228 
8229 void ASTReader::ReadMethodPool(Selector Sel) {
8230   // Get the selector generation and update it to the current generation.
8231   unsigned &Generation = SelectorGeneration[Sel];
8232   unsigned PriorGeneration = Generation;
8233   Generation = getGeneration();
8234   SelectorOutOfDate[Sel] = false;
8235 
8236   // Search for methods defined with this selector.
8237   ++NumMethodPoolLookups;
8238   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8239   ModuleMgr.visit(Visitor);
8240 
8241   if (Visitor.getInstanceMethods().empty() &&
8242       Visitor.getFactoryMethods().empty())
8243     return;
8244 
8245   ++NumMethodPoolHits;
8246 
8247   if (!getSema())
8248     return;
8249 
8250   Sema &S = *getSema();
8251   Sema::GlobalMethodPool::iterator Pos
8252     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8253 
8254   Pos->second.first.setBits(Visitor.getInstanceBits());
8255   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8256   Pos->second.second.setBits(Visitor.getFactoryBits());
8257   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8258 
8259   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8260   // when building a module we keep every method individually and may need to
8261   // update hasMoreThanOneDecl as we add the methods.
8262   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8263   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8264 }
8265 
8266 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8267   if (SelectorOutOfDate[Sel])
8268     ReadMethodPool(Sel);
8269 }
8270 
8271 void ASTReader::ReadKnownNamespaces(
8272                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8273   Namespaces.clear();
8274 
8275   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8276     if (NamespaceDecl *Namespace
8277                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8278       Namespaces.push_back(Namespace);
8279   }
8280 }
8281 
8282 void ASTReader::ReadUndefinedButUsed(
8283     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8284   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8285     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8286     SourceLocation Loc =
8287         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8288     Undefined.insert(std::make_pair(D, Loc));
8289   }
8290 }
8291 
8292 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8293     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8294                                                      Exprs) {
8295   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8296     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8297     uint64_t Count = DelayedDeleteExprs[Idx++];
8298     for (uint64_t C = 0; C < Count; ++C) {
8299       SourceLocation DeleteLoc =
8300           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8301       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8302       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8303     }
8304   }
8305 }
8306 
8307 void ASTReader::ReadTentativeDefinitions(
8308                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8309   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8310     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8311     if (Var)
8312       TentativeDefs.push_back(Var);
8313   }
8314   TentativeDefinitions.clear();
8315 }
8316 
8317 void ASTReader::ReadUnusedFileScopedDecls(
8318                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8319   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8320     DeclaratorDecl *D
8321       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8322     if (D)
8323       Decls.push_back(D);
8324   }
8325   UnusedFileScopedDecls.clear();
8326 }
8327 
8328 void ASTReader::ReadDelegatingConstructors(
8329                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8330   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8331     CXXConstructorDecl *D
8332       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8333     if (D)
8334       Decls.push_back(D);
8335   }
8336   DelegatingCtorDecls.clear();
8337 }
8338 
8339 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8340   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8341     TypedefNameDecl *D
8342       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8343     if (D)
8344       Decls.push_back(D);
8345   }
8346   ExtVectorDecls.clear();
8347 }
8348 
8349 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8350     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8351   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8352        ++I) {
8353     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8354         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8355     if (D)
8356       Decls.insert(D);
8357   }
8358   UnusedLocalTypedefNameCandidates.clear();
8359 }
8360 
8361 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8362     llvm::SmallSetVector<Decl *, 4> &Decls) {
8363   for (auto I : DeclsToCheckForDeferredDiags) {
8364     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8365     if (D)
8366       Decls.insert(D);
8367   }
8368   DeclsToCheckForDeferredDiags.clear();
8369 }
8370 
8371 void ASTReader::ReadReferencedSelectors(
8372        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8373   if (ReferencedSelectorsData.empty())
8374     return;
8375 
8376   // If there are @selector references added them to its pool. This is for
8377   // implementation of -Wselector.
8378   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8379   unsigned I = 0;
8380   while (I < DataSize) {
8381     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8382     SourceLocation SelLoc
8383       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8384     Sels.push_back(std::make_pair(Sel, SelLoc));
8385   }
8386   ReferencedSelectorsData.clear();
8387 }
8388 
8389 void ASTReader::ReadWeakUndeclaredIdentifiers(
8390        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8391   if (WeakUndeclaredIdentifiers.empty())
8392     return;
8393 
8394   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8395     IdentifierInfo *WeakId
8396       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8397     IdentifierInfo *AliasId
8398       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8399     SourceLocation Loc
8400       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8401     bool Used = WeakUndeclaredIdentifiers[I++];
8402     WeakInfo WI(AliasId, Loc);
8403     WI.setUsed(Used);
8404     WeakIDs.push_back(std::make_pair(WeakId, WI));
8405   }
8406   WeakUndeclaredIdentifiers.clear();
8407 }
8408 
8409 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8410   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8411     ExternalVTableUse VT;
8412     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8413     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8414     VT.DefinitionRequired = VTableUses[Idx++];
8415     VTables.push_back(VT);
8416   }
8417 
8418   VTableUses.clear();
8419 }
8420 
8421 void ASTReader::ReadPendingInstantiations(
8422        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8423   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8424     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8425     SourceLocation Loc
8426       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8427 
8428     Pending.push_back(std::make_pair(D, Loc));
8429   }
8430   PendingInstantiations.clear();
8431 }
8432 
8433 void ASTReader::ReadLateParsedTemplates(
8434     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8435         &LPTMap) {
8436   for (auto &LPT : LateParsedTemplates) {
8437     ModuleFile *FMod = LPT.first;
8438     RecordDataImpl &LateParsed = LPT.second;
8439     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8440          /* In loop */) {
8441       FunctionDecl *FD =
8442           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8443 
8444       auto LT = std::make_unique<LateParsedTemplate>();
8445       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8446 
8447       ModuleFile *F = getOwningModuleFile(LT->D);
8448       assert(F && "No module");
8449 
8450       unsigned TokN = LateParsed[Idx++];
8451       LT->Toks.reserve(TokN);
8452       for (unsigned T = 0; T < TokN; ++T)
8453         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8454 
8455       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8456     }
8457   }
8458 }
8459 
8460 void ASTReader::LoadSelector(Selector Sel) {
8461   // It would be complicated to avoid reading the methods anyway. So don't.
8462   ReadMethodPool(Sel);
8463 }
8464 
8465 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8466   assert(ID && "Non-zero identifier ID required");
8467   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8468   IdentifiersLoaded[ID - 1] = II;
8469   if (DeserializationListener)
8470     DeserializationListener->IdentifierRead(ID, II);
8471 }
8472 
8473 /// Set the globally-visible declarations associated with the given
8474 /// identifier.
8475 ///
8476 /// If the AST reader is currently in a state where the given declaration IDs
8477 /// cannot safely be resolved, they are queued until it is safe to resolve
8478 /// them.
8479 ///
8480 /// \param II an IdentifierInfo that refers to one or more globally-visible
8481 /// declarations.
8482 ///
8483 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8484 /// visible at global scope.
8485 ///
8486 /// \param Decls if non-null, this vector will be populated with the set of
8487 /// deserialized declarations. These declarations will not be pushed into
8488 /// scope.
8489 void
8490 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8491                               const SmallVectorImpl<uint32_t> &DeclIDs,
8492                                    SmallVectorImpl<Decl *> *Decls) {
8493   if (NumCurrentElementsDeserializing && !Decls) {
8494     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8495     return;
8496   }
8497 
8498   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8499     if (!SemaObj) {
8500       // Queue this declaration so that it will be added to the
8501       // translation unit scope and identifier's declaration chain
8502       // once a Sema object is known.
8503       PreloadedDeclIDs.push_back(DeclIDs[I]);
8504       continue;
8505     }
8506 
8507     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8508 
8509     // If we're simply supposed to record the declarations, do so now.
8510     if (Decls) {
8511       Decls->push_back(D);
8512       continue;
8513     }
8514 
8515     // Introduce this declaration into the translation-unit scope
8516     // and add it to the declaration chain for this identifier, so
8517     // that (unqualified) name lookup will find it.
8518     pushExternalDeclIntoScope(D, II);
8519   }
8520 }
8521 
8522 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8523   if (ID == 0)
8524     return nullptr;
8525 
8526   if (IdentifiersLoaded.empty()) {
8527     Error("no identifier table in AST file");
8528     return nullptr;
8529   }
8530 
8531   ID -= 1;
8532   if (!IdentifiersLoaded[ID]) {
8533     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8534     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8535     ModuleFile *M = I->second;
8536     unsigned Index = ID - M->BaseIdentifierID;
8537     const unsigned char *Data =
8538         M->IdentifierTableData + M->IdentifierOffsets[Index];
8539 
8540     ASTIdentifierLookupTrait Trait(*this, *M);
8541     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8542     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8543     auto &II = PP.getIdentifierTable().get(Key);
8544     IdentifiersLoaded[ID] = &II;
8545     markIdentifierFromAST(*this,  II);
8546     if (DeserializationListener)
8547       DeserializationListener->IdentifierRead(ID + 1, &II);
8548   }
8549 
8550   return IdentifiersLoaded[ID];
8551 }
8552 
8553 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8554   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8555 }
8556 
8557 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8558   if (LocalID < NUM_PREDEF_IDENT_IDS)
8559     return LocalID;
8560 
8561   if (!M.ModuleOffsetMap.empty())
8562     ReadModuleOffsetMap(M);
8563 
8564   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8565     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8566   assert(I != M.IdentifierRemap.end()
8567          && "Invalid index into identifier index remap");
8568 
8569   return LocalID + I->second;
8570 }
8571 
8572 MacroInfo *ASTReader::getMacro(MacroID ID) {
8573   if (ID == 0)
8574     return nullptr;
8575 
8576   if (MacrosLoaded.empty()) {
8577     Error("no macro table in AST file");
8578     return nullptr;
8579   }
8580 
8581   ID -= NUM_PREDEF_MACRO_IDS;
8582   if (!MacrosLoaded[ID]) {
8583     GlobalMacroMapType::iterator I
8584       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8585     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8586     ModuleFile *M = I->second;
8587     unsigned Index = ID - M->BaseMacroID;
8588     MacrosLoaded[ID] =
8589         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8590 
8591     if (DeserializationListener)
8592       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8593                                          MacrosLoaded[ID]);
8594   }
8595 
8596   return MacrosLoaded[ID];
8597 }
8598 
8599 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8600   if (LocalID < NUM_PREDEF_MACRO_IDS)
8601     return LocalID;
8602 
8603   if (!M.ModuleOffsetMap.empty())
8604     ReadModuleOffsetMap(M);
8605 
8606   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8607     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8608   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8609 
8610   return LocalID + I->second;
8611 }
8612 
8613 serialization::SubmoduleID
8614 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8615   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8616     return LocalID;
8617 
8618   if (!M.ModuleOffsetMap.empty())
8619     ReadModuleOffsetMap(M);
8620 
8621   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8622     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8623   assert(I != M.SubmoduleRemap.end()
8624          && "Invalid index into submodule index remap");
8625 
8626   return LocalID + I->second;
8627 }
8628 
8629 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8630   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8631     assert(GlobalID == 0 && "Unhandled global submodule ID");
8632     return nullptr;
8633   }
8634 
8635   if (GlobalID > SubmodulesLoaded.size()) {
8636     Error("submodule ID out of range in AST file");
8637     return nullptr;
8638   }
8639 
8640   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8641 }
8642 
8643 Module *ASTReader::getModule(unsigned ID) {
8644   return getSubmodule(ID);
8645 }
8646 
8647 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8648   if (ID & 1) {
8649     // It's a module, look it up by submodule ID.
8650     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8651     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8652   } else {
8653     // It's a prefix (preamble, PCH, ...). Look it up by index.
8654     unsigned IndexFromEnd = ID >> 1;
8655     assert(IndexFromEnd && "got reference to unknown module file");
8656     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8657   }
8658 }
8659 
8660 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8661   if (!F)
8662     return 1;
8663 
8664   // For a file representing a module, use the submodule ID of the top-level
8665   // module as the file ID. For any other kind of file, the number of such
8666   // files loaded beforehand will be the same on reload.
8667   // FIXME: Is this true even if we have an explicit module file and a PCH?
8668   if (F->isModule())
8669     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8670 
8671   auto PCHModules = getModuleManager().pch_modules();
8672   auto I = llvm::find(PCHModules, F);
8673   assert(I != PCHModules.end() && "emitting reference to unknown file");
8674   return (I - PCHModules.end()) << 1;
8675 }
8676 
8677 llvm::Optional<ASTSourceDescriptor>
8678 ASTReader::getSourceDescriptor(unsigned ID) {
8679   if (Module *M = getSubmodule(ID))
8680     return ASTSourceDescriptor(*M);
8681 
8682   // If there is only a single PCH, return it instead.
8683   // Chained PCH are not supported.
8684   const auto &PCHChain = ModuleMgr.pch_modules();
8685   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8686     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8687     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8688     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8689     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8690                                MF.Signature);
8691   }
8692   return None;
8693 }
8694 
8695 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8696   auto I = DefinitionSource.find(FD);
8697   if (I == DefinitionSource.end())
8698     return EK_ReplyHazy;
8699   return I->second ? EK_Never : EK_Always;
8700 }
8701 
8702 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8703   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8704 }
8705 
8706 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8707   if (ID == 0)
8708     return Selector();
8709 
8710   if (ID > SelectorsLoaded.size()) {
8711     Error("selector ID out of range in AST file");
8712     return Selector();
8713   }
8714 
8715   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8716     // Load this selector from the selector table.
8717     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8718     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8719     ModuleFile &M = *I->second;
8720     ASTSelectorLookupTrait Trait(*this, M);
8721     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8722     SelectorsLoaded[ID - 1] =
8723       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8724     if (DeserializationListener)
8725       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8726   }
8727 
8728   return SelectorsLoaded[ID - 1];
8729 }
8730 
8731 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8732   return DecodeSelector(ID);
8733 }
8734 
8735 uint32_t ASTReader::GetNumExternalSelectors() {
8736   // ID 0 (the null selector) is considered an external selector.
8737   return getTotalNumSelectors() + 1;
8738 }
8739 
8740 serialization::SelectorID
8741 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8742   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8743     return LocalID;
8744 
8745   if (!M.ModuleOffsetMap.empty())
8746     ReadModuleOffsetMap(M);
8747 
8748   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8749     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8750   assert(I != M.SelectorRemap.end()
8751          && "Invalid index into selector index remap");
8752 
8753   return LocalID + I->second;
8754 }
8755 
8756 DeclarationNameLoc
8757 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8758   switch (Name.getNameKind()) {
8759   case DeclarationName::CXXConstructorName:
8760   case DeclarationName::CXXDestructorName:
8761   case DeclarationName::CXXConversionFunctionName:
8762     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8763 
8764   case DeclarationName::CXXOperatorName:
8765     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8766 
8767   case DeclarationName::CXXLiteralOperatorName:
8768     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8769         readSourceLocation());
8770 
8771   case DeclarationName::Identifier:
8772   case DeclarationName::ObjCZeroArgSelector:
8773   case DeclarationName::ObjCOneArgSelector:
8774   case DeclarationName::ObjCMultiArgSelector:
8775   case DeclarationName::CXXUsingDirective:
8776   case DeclarationName::CXXDeductionGuideName:
8777     break;
8778   }
8779   return DeclarationNameLoc();
8780 }
8781 
8782 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8783   DeclarationNameInfo NameInfo;
8784   NameInfo.setName(readDeclarationName());
8785   NameInfo.setLoc(readSourceLocation());
8786   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8787   return NameInfo;
8788 }
8789 
8790 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8791   Info.QualifierLoc = readNestedNameSpecifierLoc();
8792   unsigned NumTPLists = readInt();
8793   Info.NumTemplParamLists = NumTPLists;
8794   if (NumTPLists) {
8795     Info.TemplParamLists =
8796         new (getContext()) TemplateParameterList *[NumTPLists];
8797     for (unsigned i = 0; i != NumTPLists; ++i)
8798       Info.TemplParamLists[i] = readTemplateParameterList();
8799   }
8800 }
8801 
8802 TemplateParameterList *
8803 ASTRecordReader::readTemplateParameterList() {
8804   SourceLocation TemplateLoc = readSourceLocation();
8805   SourceLocation LAngleLoc = readSourceLocation();
8806   SourceLocation RAngleLoc = readSourceLocation();
8807 
8808   unsigned NumParams = readInt();
8809   SmallVector<NamedDecl *, 16> Params;
8810   Params.reserve(NumParams);
8811   while (NumParams--)
8812     Params.push_back(readDeclAs<NamedDecl>());
8813 
8814   bool HasRequiresClause = readBool();
8815   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8816 
8817   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8818       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8819   return TemplateParams;
8820 }
8821 
8822 void ASTRecordReader::readTemplateArgumentList(
8823                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8824                         bool Canonicalize) {
8825   unsigned NumTemplateArgs = readInt();
8826   TemplArgs.reserve(NumTemplateArgs);
8827   while (NumTemplateArgs--)
8828     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8829 }
8830 
8831 /// Read a UnresolvedSet structure.
8832 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8833   unsigned NumDecls = readInt();
8834   Set.reserve(getContext(), NumDecls);
8835   while (NumDecls--) {
8836     DeclID ID = readDeclID();
8837     AccessSpecifier AS = (AccessSpecifier) readInt();
8838     Set.addLazyDecl(getContext(), ID, AS);
8839   }
8840 }
8841 
8842 CXXBaseSpecifier
8843 ASTRecordReader::readCXXBaseSpecifier() {
8844   bool isVirtual = readBool();
8845   bool isBaseOfClass = readBool();
8846   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8847   bool inheritConstructors = readBool();
8848   TypeSourceInfo *TInfo = readTypeSourceInfo();
8849   SourceRange Range = readSourceRange();
8850   SourceLocation EllipsisLoc = readSourceLocation();
8851   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8852                           EllipsisLoc);
8853   Result.setInheritConstructors(inheritConstructors);
8854   return Result;
8855 }
8856 
8857 CXXCtorInitializer **
8858 ASTRecordReader::readCXXCtorInitializers() {
8859   ASTContext &Context = getContext();
8860   unsigned NumInitializers = readInt();
8861   assert(NumInitializers && "wrote ctor initializers but have no inits");
8862   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8863   for (unsigned i = 0; i != NumInitializers; ++i) {
8864     TypeSourceInfo *TInfo = nullptr;
8865     bool IsBaseVirtual = false;
8866     FieldDecl *Member = nullptr;
8867     IndirectFieldDecl *IndirectMember = nullptr;
8868 
8869     CtorInitializerType Type = (CtorInitializerType) readInt();
8870     switch (Type) {
8871     case CTOR_INITIALIZER_BASE:
8872       TInfo = readTypeSourceInfo();
8873       IsBaseVirtual = readBool();
8874       break;
8875 
8876     case CTOR_INITIALIZER_DELEGATING:
8877       TInfo = readTypeSourceInfo();
8878       break;
8879 
8880      case CTOR_INITIALIZER_MEMBER:
8881       Member = readDeclAs<FieldDecl>();
8882       break;
8883 
8884      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8885       IndirectMember = readDeclAs<IndirectFieldDecl>();
8886       break;
8887     }
8888 
8889     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8890     Expr *Init = readExpr();
8891     SourceLocation LParenLoc = readSourceLocation();
8892     SourceLocation RParenLoc = readSourceLocation();
8893 
8894     CXXCtorInitializer *BOMInit;
8895     if (Type == CTOR_INITIALIZER_BASE)
8896       BOMInit = new (Context)
8897           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8898                              RParenLoc, MemberOrEllipsisLoc);
8899     else if (Type == CTOR_INITIALIZER_DELEGATING)
8900       BOMInit = new (Context)
8901           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8902     else if (Member)
8903       BOMInit = new (Context)
8904           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8905                              Init, RParenLoc);
8906     else
8907       BOMInit = new (Context)
8908           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8909                              LParenLoc, Init, RParenLoc);
8910 
8911     if (/*IsWritten*/readBool()) {
8912       unsigned SourceOrder = readInt();
8913       BOMInit->setSourceOrder(SourceOrder);
8914     }
8915 
8916     CtorInitializers[i] = BOMInit;
8917   }
8918 
8919   return CtorInitializers;
8920 }
8921 
8922 NestedNameSpecifierLoc
8923 ASTRecordReader::readNestedNameSpecifierLoc() {
8924   ASTContext &Context = getContext();
8925   unsigned N = readInt();
8926   NestedNameSpecifierLocBuilder Builder;
8927   for (unsigned I = 0; I != N; ++I) {
8928     auto Kind = readNestedNameSpecifierKind();
8929     switch (Kind) {
8930     case NestedNameSpecifier::Identifier: {
8931       IdentifierInfo *II = readIdentifier();
8932       SourceRange Range = readSourceRange();
8933       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8934       break;
8935     }
8936 
8937     case NestedNameSpecifier::Namespace: {
8938       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8939       SourceRange Range = readSourceRange();
8940       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8941       break;
8942     }
8943 
8944     case NestedNameSpecifier::NamespaceAlias: {
8945       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8946       SourceRange Range = readSourceRange();
8947       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8948       break;
8949     }
8950 
8951     case NestedNameSpecifier::TypeSpec:
8952     case NestedNameSpecifier::TypeSpecWithTemplate: {
8953       bool Template = readBool();
8954       TypeSourceInfo *T = readTypeSourceInfo();
8955       if (!T)
8956         return NestedNameSpecifierLoc();
8957       SourceLocation ColonColonLoc = readSourceLocation();
8958 
8959       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8960       Builder.Extend(Context,
8961                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8962                      T->getTypeLoc(), ColonColonLoc);
8963       break;
8964     }
8965 
8966     case NestedNameSpecifier::Global: {
8967       SourceLocation ColonColonLoc = readSourceLocation();
8968       Builder.MakeGlobal(Context, ColonColonLoc);
8969       break;
8970     }
8971 
8972     case NestedNameSpecifier::Super: {
8973       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8974       SourceRange Range = readSourceRange();
8975       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8976       break;
8977     }
8978     }
8979   }
8980 
8981   return Builder.getWithLocInContext(Context);
8982 }
8983 
8984 SourceRange
8985 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8986                            unsigned &Idx) {
8987   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8988   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8989   return SourceRange(beg, end);
8990 }
8991 
8992 /// Read a floating-point value
8993 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8994   return llvm::APFloat(Sem, readAPInt());
8995 }
8996 
8997 // Read a string
8998 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8999   unsigned Len = Record[Idx++];
9000   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9001   Idx += Len;
9002   return Result;
9003 }
9004 
9005 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9006                                 unsigned &Idx) {
9007   std::string Filename = ReadString(Record, Idx);
9008   ResolveImportedPath(F, Filename);
9009   return Filename;
9010 }
9011 
9012 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9013                                 const RecordData &Record, unsigned &Idx) {
9014   std::string Filename = ReadString(Record, Idx);
9015   if (!BaseDirectory.empty())
9016     ResolveImportedPath(Filename, BaseDirectory);
9017   return Filename;
9018 }
9019 
9020 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9021                                          unsigned &Idx) {
9022   unsigned Major = Record[Idx++];
9023   unsigned Minor = Record[Idx++];
9024   unsigned Subminor = Record[Idx++];
9025   if (Minor == 0)
9026     return VersionTuple(Major);
9027   if (Subminor == 0)
9028     return VersionTuple(Major, Minor - 1);
9029   return VersionTuple(Major, Minor - 1, Subminor - 1);
9030 }
9031 
9032 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9033                                           const RecordData &Record,
9034                                           unsigned &Idx) {
9035   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9036   return CXXTemporary::Create(getContext(), Decl);
9037 }
9038 
9039 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9040   return Diag(CurrentImportLoc, DiagID);
9041 }
9042 
9043 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9044   return Diags.Report(Loc, DiagID);
9045 }
9046 
9047 /// Retrieve the identifier table associated with the
9048 /// preprocessor.
9049 IdentifierTable &ASTReader::getIdentifierTable() {
9050   return PP.getIdentifierTable();
9051 }
9052 
9053 /// Record that the given ID maps to the given switch-case
9054 /// statement.
9055 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9056   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9057          "Already have a SwitchCase with this ID");
9058   (*CurrSwitchCaseStmts)[ID] = SC;
9059 }
9060 
9061 /// Retrieve the switch-case statement with the given ID.
9062 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9063   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9064   return (*CurrSwitchCaseStmts)[ID];
9065 }
9066 
9067 void ASTReader::ClearSwitchCaseIDs() {
9068   CurrSwitchCaseStmts->clear();
9069 }
9070 
9071 void ASTReader::ReadComments() {
9072   ASTContext &Context = getContext();
9073   std::vector<RawComment *> Comments;
9074   for (SmallVectorImpl<std::pair<BitstreamCursor,
9075                                  serialization::ModuleFile *>>::iterator
9076        I = CommentsCursors.begin(),
9077        E = CommentsCursors.end();
9078        I != E; ++I) {
9079     Comments.clear();
9080     BitstreamCursor &Cursor = I->first;
9081     serialization::ModuleFile &F = *I->second;
9082     SavedStreamPosition SavedPosition(Cursor);
9083 
9084     RecordData Record;
9085     while (true) {
9086       Expected<llvm::BitstreamEntry> MaybeEntry =
9087           Cursor.advanceSkippingSubblocks(
9088               BitstreamCursor::AF_DontPopBlockAtEnd);
9089       if (!MaybeEntry) {
9090         Error(MaybeEntry.takeError());
9091         return;
9092       }
9093       llvm::BitstreamEntry Entry = MaybeEntry.get();
9094 
9095       switch (Entry.Kind) {
9096       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9097       case llvm::BitstreamEntry::Error:
9098         Error("malformed block record in AST file");
9099         return;
9100       case llvm::BitstreamEntry::EndBlock:
9101         goto NextCursor;
9102       case llvm::BitstreamEntry::Record:
9103         // The interesting case.
9104         break;
9105       }
9106 
9107       // Read a record.
9108       Record.clear();
9109       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9110       if (!MaybeComment) {
9111         Error(MaybeComment.takeError());
9112         return;
9113       }
9114       switch ((CommentRecordTypes)MaybeComment.get()) {
9115       case COMMENTS_RAW_COMMENT: {
9116         unsigned Idx = 0;
9117         SourceRange SR = ReadSourceRange(F, Record, Idx);
9118         RawComment::CommentKind Kind =
9119             (RawComment::CommentKind) Record[Idx++];
9120         bool IsTrailingComment = Record[Idx++];
9121         bool IsAlmostTrailingComment = Record[Idx++];
9122         Comments.push_back(new (Context) RawComment(
9123             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9124         break;
9125       }
9126       }
9127     }
9128   NextCursor:
9129     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9130         FileToOffsetToComment;
9131     for (RawComment *C : Comments) {
9132       SourceLocation CommentLoc = C->getBeginLoc();
9133       if (CommentLoc.isValid()) {
9134         std::pair<FileID, unsigned> Loc =
9135             SourceMgr.getDecomposedLoc(CommentLoc);
9136         if (Loc.first.isValid())
9137           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9138       }
9139     }
9140   }
9141 }
9142 
9143 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9144                                 bool IncludeSystem, bool Complain,
9145                     llvm::function_ref<void(const serialization::InputFile &IF,
9146                                             bool isSystem)> Visitor) {
9147   unsigned NumUserInputs = MF.NumUserInputFiles;
9148   unsigned NumInputs = MF.InputFilesLoaded.size();
9149   assert(NumUserInputs <= NumInputs);
9150   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9151   for (unsigned I = 0; I < N; ++I) {
9152     bool IsSystem = I >= NumUserInputs;
9153     InputFile IF = getInputFile(MF, I+1, Complain);
9154     Visitor(IF, IsSystem);
9155   }
9156 }
9157 
9158 void ASTReader::visitTopLevelModuleMaps(
9159     serialization::ModuleFile &MF,
9160     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9161   unsigned NumInputs = MF.InputFilesLoaded.size();
9162   for (unsigned I = 0; I < NumInputs; ++I) {
9163     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9164     if (IFI.TopLevelModuleMap)
9165       // FIXME: This unnecessarily re-reads the InputFileInfo.
9166       if (auto FE = getInputFile(MF, I + 1).getFile())
9167         Visitor(FE);
9168   }
9169 }
9170 
9171 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9172   // If we know the owning module, use it.
9173   if (Module *M = D->getImportedOwningModule())
9174     return M->getFullModuleName();
9175 
9176   // Otherwise, use the name of the top-level module the decl is within.
9177   if (ModuleFile *M = getOwningModuleFile(D))
9178     return M->ModuleName;
9179 
9180   // Not from a module.
9181   return {};
9182 }
9183 
9184 void ASTReader::finishPendingActions() {
9185   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9186          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9187          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9188          !PendingUpdateRecords.empty()) {
9189     // If any identifiers with corresponding top-level declarations have
9190     // been loaded, load those declarations now.
9191     using TopLevelDeclsMap =
9192         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9193     TopLevelDeclsMap TopLevelDecls;
9194 
9195     while (!PendingIdentifierInfos.empty()) {
9196       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9197       SmallVector<uint32_t, 4> DeclIDs =
9198           std::move(PendingIdentifierInfos.back().second);
9199       PendingIdentifierInfos.pop_back();
9200 
9201       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9202     }
9203 
9204     // Load each function type that we deferred loading because it was a
9205     // deduced type that might refer to a local type declared within itself.
9206     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9207       auto *FD = PendingFunctionTypes[I].first;
9208       FD->setType(GetType(PendingFunctionTypes[I].second));
9209 
9210       // If we gave a function a deduced return type, remember that we need to
9211       // propagate that along the redeclaration chain.
9212       auto *DT = FD->getReturnType()->getContainedDeducedType();
9213       if (DT && DT->isDeduced())
9214         PendingDeducedTypeUpdates.insert(
9215             {FD->getCanonicalDecl(), FD->getReturnType()});
9216     }
9217     PendingFunctionTypes.clear();
9218 
9219     // For each decl chain that we wanted to complete while deserializing, mark
9220     // it as "still needs to be completed".
9221     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9222       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9223     }
9224     PendingIncompleteDeclChains.clear();
9225 
9226     // Load pending declaration chains.
9227     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9228       loadPendingDeclChain(PendingDeclChains[I].first,
9229                            PendingDeclChains[I].second);
9230     PendingDeclChains.clear();
9231 
9232     // Make the most recent of the top-level declarations visible.
9233     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9234            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9235       IdentifierInfo *II = TLD->first;
9236       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9237         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9238       }
9239     }
9240 
9241     // Load any pending macro definitions.
9242     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9243       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9244       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9245       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9246       // Initialize the macro history from chained-PCHs ahead of module imports.
9247       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9248            ++IDIdx) {
9249         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9250         if (!Info.M->isModule())
9251           resolvePendingMacro(II, Info);
9252       }
9253       // Handle module imports.
9254       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9255            ++IDIdx) {
9256         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9257         if (Info.M->isModule())
9258           resolvePendingMacro(II, Info);
9259       }
9260     }
9261     PendingMacroIDs.clear();
9262 
9263     // Wire up the DeclContexts for Decls that we delayed setting until
9264     // recursive loading is completed.
9265     while (!PendingDeclContextInfos.empty()) {
9266       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9267       PendingDeclContextInfos.pop_front();
9268       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9269       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9270       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9271     }
9272 
9273     // Perform any pending declaration updates.
9274     while (!PendingUpdateRecords.empty()) {
9275       auto Update = PendingUpdateRecords.pop_back_val();
9276       ReadingKindTracker ReadingKind(Read_Decl, *this);
9277       loadDeclUpdateRecords(Update);
9278     }
9279   }
9280 
9281   // At this point, all update records for loaded decls are in place, so any
9282   // fake class definitions should have become real.
9283   assert(PendingFakeDefinitionData.empty() &&
9284          "faked up a class definition but never saw the real one");
9285 
9286   // If we deserialized any C++ or Objective-C class definitions, any
9287   // Objective-C protocol definitions, or any redeclarable templates, make sure
9288   // that all redeclarations point to the definitions. Note that this can only
9289   // happen now, after the redeclaration chains have been fully wired.
9290   for (Decl *D : PendingDefinitions) {
9291     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9292       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9293         // Make sure that the TagType points at the definition.
9294         const_cast<TagType*>(TagT)->decl = TD;
9295       }
9296 
9297       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9298         for (auto *R = getMostRecentExistingDecl(RD); R;
9299              R = R->getPreviousDecl()) {
9300           assert((R == D) ==
9301                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9302                  "declaration thinks it's the definition but it isn't");
9303           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9304         }
9305       }
9306 
9307       continue;
9308     }
9309 
9310     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9311       // Make sure that the ObjCInterfaceType points at the definition.
9312       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9313         ->Decl = ID;
9314 
9315       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9316         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9317 
9318       continue;
9319     }
9320 
9321     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9322       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9323         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9324 
9325       continue;
9326     }
9327 
9328     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9329     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9330       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9331   }
9332   PendingDefinitions.clear();
9333 
9334   // Load the bodies of any functions or methods we've encountered. We do
9335   // this now (delayed) so that we can be sure that the declaration chains
9336   // have been fully wired up (hasBody relies on this).
9337   // FIXME: We shouldn't require complete redeclaration chains here.
9338   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9339                                PBEnd = PendingBodies.end();
9340        PB != PBEnd; ++PB) {
9341     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9342       // For a function defined inline within a class template, force the
9343       // canonical definition to be the one inside the canonical definition of
9344       // the template. This ensures that we instantiate from a correct view
9345       // of the template.
9346       //
9347       // Sadly we can't do this more generally: we can't be sure that all
9348       // copies of an arbitrary class definition will have the same members
9349       // defined (eg, some member functions may not be instantiated, and some
9350       // special members may or may not have been implicitly defined).
9351       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9352         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9353           continue;
9354 
9355       // FIXME: Check for =delete/=default?
9356       // FIXME: Complain about ODR violations here?
9357       const FunctionDecl *Defn = nullptr;
9358       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9359         FD->setLazyBody(PB->second);
9360       } else {
9361         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9362         mergeDefinitionVisibility(NonConstDefn, FD);
9363 
9364         if (!FD->isLateTemplateParsed() &&
9365             !NonConstDefn->isLateTemplateParsed() &&
9366             FD->getODRHash() != NonConstDefn->getODRHash()) {
9367           if (!isa<CXXMethodDecl>(FD)) {
9368             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9369           } else if (FD->getLexicalParent()->isFileContext() &&
9370                      NonConstDefn->getLexicalParent()->isFileContext()) {
9371             // Only diagnose out-of-line method definitions.  If they are
9372             // in class definitions, then an error will be generated when
9373             // processing the class bodies.
9374             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9375           }
9376         }
9377       }
9378       continue;
9379     }
9380 
9381     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9382     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9383       MD->setLazyBody(PB->second);
9384   }
9385   PendingBodies.clear();
9386 
9387   // Do some cleanup.
9388   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9389     getContext().deduplicateMergedDefinitonsFor(ND);
9390   PendingMergedDefinitionsToDeduplicate.clear();
9391 }
9392 
9393 void ASTReader::diagnoseOdrViolations() {
9394   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9395       PendingFunctionOdrMergeFailures.empty() &&
9396       PendingEnumOdrMergeFailures.empty())
9397     return;
9398 
9399   // Trigger the import of the full definition of each class that had any
9400   // odr-merging problems, so we can produce better diagnostics for them.
9401   // These updates may in turn find and diagnose some ODR failures, so take
9402   // ownership of the set first.
9403   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9404   PendingOdrMergeFailures.clear();
9405   for (auto &Merge : OdrMergeFailures) {
9406     Merge.first->buildLookup();
9407     Merge.first->decls_begin();
9408     Merge.first->bases_begin();
9409     Merge.first->vbases_begin();
9410     for (auto &RecordPair : Merge.second) {
9411       auto *RD = RecordPair.first;
9412       RD->decls_begin();
9413       RD->bases_begin();
9414       RD->vbases_begin();
9415     }
9416   }
9417 
9418   // Trigger the import of functions.
9419   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9420   PendingFunctionOdrMergeFailures.clear();
9421   for (auto &Merge : FunctionOdrMergeFailures) {
9422     Merge.first->buildLookup();
9423     Merge.first->decls_begin();
9424     Merge.first->getBody();
9425     for (auto &FD : Merge.second) {
9426       FD->buildLookup();
9427       FD->decls_begin();
9428       FD->getBody();
9429     }
9430   }
9431 
9432   // Trigger the import of enums.
9433   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9434   PendingEnumOdrMergeFailures.clear();
9435   for (auto &Merge : EnumOdrMergeFailures) {
9436     Merge.first->decls_begin();
9437     for (auto &Enum : Merge.second) {
9438       Enum->decls_begin();
9439     }
9440   }
9441 
9442   // For each declaration from a merged context, check that the canonical
9443   // definition of that context also contains a declaration of the same
9444   // entity.
9445   //
9446   // Caution: this loop does things that might invalidate iterators into
9447   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9448   while (!PendingOdrMergeChecks.empty()) {
9449     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9450 
9451     // FIXME: Skip over implicit declarations for now. This matters for things
9452     // like implicitly-declared special member functions. This isn't entirely
9453     // correct; we can end up with multiple unmerged declarations of the same
9454     // implicit entity.
9455     if (D->isImplicit())
9456       continue;
9457 
9458     DeclContext *CanonDef = D->getDeclContext();
9459 
9460     bool Found = false;
9461     const Decl *DCanon = D->getCanonicalDecl();
9462 
9463     for (auto RI : D->redecls()) {
9464       if (RI->getLexicalDeclContext() == CanonDef) {
9465         Found = true;
9466         break;
9467       }
9468     }
9469     if (Found)
9470       continue;
9471 
9472     // Quick check failed, time to do the slow thing. Note, we can't just
9473     // look up the name of D in CanonDef here, because the member that is
9474     // in CanonDef might not be found by name lookup (it might have been
9475     // replaced by a more recent declaration in the lookup table), and we
9476     // can't necessarily find it in the redeclaration chain because it might
9477     // be merely mergeable, not redeclarable.
9478     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9479     for (auto *CanonMember : CanonDef->decls()) {
9480       if (CanonMember->getCanonicalDecl() == DCanon) {
9481         // This can happen if the declaration is merely mergeable and not
9482         // actually redeclarable (we looked for redeclarations earlier).
9483         //
9484         // FIXME: We should be able to detect this more efficiently, without
9485         // pulling in all of the members of CanonDef.
9486         Found = true;
9487         break;
9488       }
9489       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9490         if (ND->getDeclName() == D->getDeclName())
9491           Candidates.push_back(ND);
9492     }
9493 
9494     if (!Found) {
9495       // The AST doesn't like TagDecls becoming invalid after they've been
9496       // completed. We only really need to mark FieldDecls as invalid here.
9497       if (!isa<TagDecl>(D))
9498         D->setInvalidDecl();
9499 
9500       // Ensure we don't accidentally recursively enter deserialization while
9501       // we're producing our diagnostic.
9502       Deserializing RecursionGuard(this);
9503 
9504       std::string CanonDefModule =
9505           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9506       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9507         << D << getOwningModuleNameForDiagnostic(D)
9508         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9509 
9510       if (Candidates.empty())
9511         Diag(cast<Decl>(CanonDef)->getLocation(),
9512              diag::note_module_odr_violation_no_possible_decls) << D;
9513       else {
9514         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9515           Diag(Candidates[I]->getLocation(),
9516                diag::note_module_odr_violation_possible_decl)
9517             << Candidates[I];
9518       }
9519 
9520       DiagnosedOdrMergeFailures.insert(CanonDef);
9521     }
9522   }
9523 
9524   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9525       EnumOdrMergeFailures.empty())
9526     return;
9527 
9528   // Ensure we don't accidentally recursively enter deserialization while
9529   // we're producing our diagnostics.
9530   Deserializing RecursionGuard(this);
9531 
9532   // Common code for hashing helpers.
9533   ODRHash Hash;
9534   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9535     Hash.clear();
9536     Hash.AddQualType(Ty);
9537     return Hash.CalculateHash();
9538   };
9539 
9540   auto ComputeODRHash = [&Hash](const Stmt *S) {
9541     assert(S);
9542     Hash.clear();
9543     Hash.AddStmt(S);
9544     return Hash.CalculateHash();
9545   };
9546 
9547   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9548     assert(D);
9549     Hash.clear();
9550     Hash.AddSubDecl(D);
9551     return Hash.CalculateHash();
9552   };
9553 
9554   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9555     Hash.clear();
9556     Hash.AddTemplateArgument(TA);
9557     return Hash.CalculateHash();
9558   };
9559 
9560   auto ComputeTemplateParameterListODRHash =
9561       [&Hash](const TemplateParameterList *TPL) {
9562         assert(TPL);
9563         Hash.clear();
9564         Hash.AddTemplateParameterList(TPL);
9565         return Hash.CalculateHash();
9566       };
9567 
9568   // Used with err_module_odr_violation_mismatch_decl and
9569   // note_module_odr_violation_mismatch_decl
9570   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9571   enum ODRMismatchDecl {
9572     EndOfClass,
9573     PublicSpecifer,
9574     PrivateSpecifer,
9575     ProtectedSpecifer,
9576     StaticAssert,
9577     Field,
9578     CXXMethod,
9579     TypeAlias,
9580     TypeDef,
9581     Var,
9582     Friend,
9583     FunctionTemplate,
9584     Other
9585   };
9586 
9587   // Used with err_module_odr_violation_mismatch_decl_diff and
9588   // note_module_odr_violation_mismatch_decl_diff
9589   enum ODRMismatchDeclDifference {
9590     StaticAssertCondition,
9591     StaticAssertMessage,
9592     StaticAssertOnlyMessage,
9593     FieldName,
9594     FieldTypeName,
9595     FieldSingleBitField,
9596     FieldDifferentWidthBitField,
9597     FieldSingleMutable,
9598     FieldSingleInitializer,
9599     FieldDifferentInitializers,
9600     MethodName,
9601     MethodDeleted,
9602     MethodDefaulted,
9603     MethodVirtual,
9604     MethodStatic,
9605     MethodVolatile,
9606     MethodConst,
9607     MethodInline,
9608     MethodNumberParameters,
9609     MethodParameterType,
9610     MethodParameterName,
9611     MethodParameterSingleDefaultArgument,
9612     MethodParameterDifferentDefaultArgument,
9613     MethodNoTemplateArguments,
9614     MethodDifferentNumberTemplateArguments,
9615     MethodDifferentTemplateArgument,
9616     MethodSingleBody,
9617     MethodDifferentBody,
9618     TypedefName,
9619     TypedefType,
9620     VarName,
9621     VarType,
9622     VarSingleInitializer,
9623     VarDifferentInitializer,
9624     VarConstexpr,
9625     FriendTypeFunction,
9626     FriendType,
9627     FriendFunction,
9628     FunctionTemplateDifferentNumberParameters,
9629     FunctionTemplateParameterDifferentKind,
9630     FunctionTemplateParameterName,
9631     FunctionTemplateParameterSingleDefaultArgument,
9632     FunctionTemplateParameterDifferentDefaultArgument,
9633     FunctionTemplateParameterDifferentType,
9634     FunctionTemplatePackParameter,
9635   };
9636 
9637   // These lambdas have the common portions of the ODR diagnostics.  This
9638   // has the same return as Diag(), so addition parameters can be passed
9639   // in with operator<<
9640   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9641                                  SourceLocation Loc, SourceRange Range,
9642                                  ODRMismatchDeclDifference DiffType) {
9643     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9644            << FirstRecord << FirstModule.empty() << FirstModule << Range
9645            << DiffType;
9646   };
9647   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9648                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9649     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9650            << SecondModule << Range << DiffType;
9651   };
9652 
9653   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9654                        &ComputeQualTypeODRHash, &ComputeODRHash](
9655                           NamedDecl *FirstRecord, StringRef FirstModule,
9656                           StringRef SecondModule, FieldDecl *FirstField,
9657                           FieldDecl *SecondField) {
9658     IdentifierInfo *FirstII = FirstField->getIdentifier();
9659     IdentifierInfo *SecondII = SecondField->getIdentifier();
9660     if (FirstII->getName() != SecondII->getName()) {
9661       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9662                        FirstField->getSourceRange(), FieldName)
9663           << FirstII;
9664       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9665                       SecondField->getSourceRange(), FieldName)
9666           << SecondII;
9667 
9668       return true;
9669     }
9670 
9671     assert(getContext().hasSameType(FirstField->getType(),
9672                                     SecondField->getType()));
9673 
9674     QualType FirstType = FirstField->getType();
9675     QualType SecondType = SecondField->getType();
9676     if (ComputeQualTypeODRHash(FirstType) !=
9677         ComputeQualTypeODRHash(SecondType)) {
9678       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9679                        FirstField->getSourceRange(), FieldTypeName)
9680           << FirstII << FirstType;
9681       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9682                       SecondField->getSourceRange(), FieldTypeName)
9683           << SecondII << SecondType;
9684 
9685       return true;
9686     }
9687 
9688     const bool IsFirstBitField = FirstField->isBitField();
9689     const bool IsSecondBitField = SecondField->isBitField();
9690     if (IsFirstBitField != IsSecondBitField) {
9691       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9692                        FirstField->getSourceRange(), FieldSingleBitField)
9693           << FirstII << IsFirstBitField;
9694       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9695                       SecondField->getSourceRange(), FieldSingleBitField)
9696           << SecondII << IsSecondBitField;
9697       return true;
9698     }
9699 
9700     if (IsFirstBitField && IsSecondBitField) {
9701       unsigned FirstBitWidthHash =
9702           ComputeODRHash(FirstField->getBitWidth());
9703       unsigned SecondBitWidthHash =
9704           ComputeODRHash(SecondField->getBitWidth());
9705       if (FirstBitWidthHash != SecondBitWidthHash) {
9706         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9707                          FirstField->getSourceRange(),
9708                          FieldDifferentWidthBitField)
9709             << FirstII << FirstField->getBitWidth()->getSourceRange();
9710         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9711                         SecondField->getSourceRange(),
9712                         FieldDifferentWidthBitField)
9713             << SecondII << SecondField->getBitWidth()->getSourceRange();
9714         return true;
9715       }
9716     }
9717 
9718     if (!PP.getLangOpts().CPlusPlus)
9719       return false;
9720 
9721     const bool IsFirstMutable = FirstField->isMutable();
9722     const bool IsSecondMutable = SecondField->isMutable();
9723     if (IsFirstMutable != IsSecondMutable) {
9724       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9725                        FirstField->getSourceRange(), FieldSingleMutable)
9726           << FirstII << IsFirstMutable;
9727       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9728                       SecondField->getSourceRange(), FieldSingleMutable)
9729           << SecondII << IsSecondMutable;
9730       return true;
9731     }
9732 
9733     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9734     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9735     if ((!FirstInitializer && SecondInitializer) ||
9736         (FirstInitializer && !SecondInitializer)) {
9737       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9738                        FirstField->getSourceRange(), FieldSingleInitializer)
9739           << FirstII << (FirstInitializer != nullptr);
9740       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9741                       SecondField->getSourceRange(), FieldSingleInitializer)
9742           << SecondII << (SecondInitializer != nullptr);
9743       return true;
9744     }
9745 
9746     if (FirstInitializer && SecondInitializer) {
9747       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9748       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9749       if (FirstInitHash != SecondInitHash) {
9750         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9751                          FirstField->getSourceRange(),
9752                          FieldDifferentInitializers)
9753             << FirstII << FirstInitializer->getSourceRange();
9754         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9755                         SecondField->getSourceRange(),
9756                         FieldDifferentInitializers)
9757             << SecondII << SecondInitializer->getSourceRange();
9758         return true;
9759       }
9760     }
9761 
9762     return false;
9763   };
9764 
9765   auto ODRDiagTypeDefOrAlias =
9766       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9767           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9768           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9769           bool IsTypeAlias) {
9770         auto FirstName = FirstTD->getDeclName();
9771         auto SecondName = SecondTD->getDeclName();
9772         if (FirstName != SecondName) {
9773           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9774                            FirstTD->getSourceRange(), TypedefName)
9775               << IsTypeAlias << FirstName;
9776           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9777                           SecondTD->getSourceRange(), TypedefName)
9778               << IsTypeAlias << SecondName;
9779           return true;
9780         }
9781 
9782         QualType FirstType = FirstTD->getUnderlyingType();
9783         QualType SecondType = SecondTD->getUnderlyingType();
9784         if (ComputeQualTypeODRHash(FirstType) !=
9785             ComputeQualTypeODRHash(SecondType)) {
9786           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9787                            FirstTD->getSourceRange(), TypedefType)
9788               << IsTypeAlias << FirstName << FirstType;
9789           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9790                           SecondTD->getSourceRange(), TypedefType)
9791               << IsTypeAlias << SecondName << SecondType;
9792           return true;
9793         }
9794 
9795         return false;
9796   };
9797 
9798   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9799                      &ComputeQualTypeODRHash, &ComputeODRHash,
9800                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9801                            StringRef SecondModule, VarDecl *FirstVD,
9802                            VarDecl *SecondVD) {
9803     auto FirstName = FirstVD->getDeclName();
9804     auto SecondName = SecondVD->getDeclName();
9805     if (FirstName != SecondName) {
9806       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9807                        FirstVD->getSourceRange(), VarName)
9808           << FirstName;
9809       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9810                       SecondVD->getSourceRange(), VarName)
9811           << SecondName;
9812       return true;
9813     }
9814 
9815     QualType FirstType = FirstVD->getType();
9816     QualType SecondType = SecondVD->getType();
9817     if (ComputeQualTypeODRHash(FirstType) !=
9818         ComputeQualTypeODRHash(SecondType)) {
9819       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9820                        FirstVD->getSourceRange(), VarType)
9821           << FirstName << FirstType;
9822       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9823                       SecondVD->getSourceRange(), VarType)
9824           << SecondName << SecondType;
9825       return true;
9826     }
9827 
9828     if (!PP.getLangOpts().CPlusPlus)
9829       return false;
9830 
9831     const Expr *FirstInit = FirstVD->getInit();
9832     const Expr *SecondInit = SecondVD->getInit();
9833     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9834       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9835                        FirstVD->getSourceRange(), VarSingleInitializer)
9836           << FirstName << (FirstInit == nullptr)
9837           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9838       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9839                       SecondVD->getSourceRange(), VarSingleInitializer)
9840           << SecondName << (SecondInit == nullptr)
9841           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9842       return true;
9843     }
9844 
9845     if (FirstInit && SecondInit &&
9846         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9847       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9848                        FirstVD->getSourceRange(), VarDifferentInitializer)
9849           << FirstName << FirstInit->getSourceRange();
9850       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9851                       SecondVD->getSourceRange(), VarDifferentInitializer)
9852           << SecondName << SecondInit->getSourceRange();
9853       return true;
9854     }
9855 
9856     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9857     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9858     if (FirstIsConstexpr != SecondIsConstexpr) {
9859       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9860                        FirstVD->getSourceRange(), VarConstexpr)
9861           << FirstName << FirstIsConstexpr;
9862       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9863                       SecondVD->getSourceRange(), VarConstexpr)
9864           << SecondName << SecondIsConstexpr;
9865       return true;
9866     }
9867     return false;
9868   };
9869 
9870   auto DifferenceSelector = [](Decl *D) {
9871     assert(D && "valid Decl required");
9872     switch (D->getKind()) {
9873     default:
9874       return Other;
9875     case Decl::AccessSpec:
9876       switch (D->getAccess()) {
9877       case AS_public:
9878         return PublicSpecifer;
9879       case AS_private:
9880         return PrivateSpecifer;
9881       case AS_protected:
9882         return ProtectedSpecifer;
9883       case AS_none:
9884         break;
9885       }
9886       llvm_unreachable("Invalid access specifier");
9887     case Decl::StaticAssert:
9888       return StaticAssert;
9889     case Decl::Field:
9890       return Field;
9891     case Decl::CXXMethod:
9892     case Decl::CXXConstructor:
9893     case Decl::CXXDestructor:
9894       return CXXMethod;
9895     case Decl::TypeAlias:
9896       return TypeAlias;
9897     case Decl::Typedef:
9898       return TypeDef;
9899     case Decl::Var:
9900       return Var;
9901     case Decl::Friend:
9902       return Friend;
9903     case Decl::FunctionTemplate:
9904       return FunctionTemplate;
9905     }
9906   };
9907 
9908   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9909   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9910                                                  RecordDecl *Record,
9911                                                  const DeclContext *DC) {
9912     for (auto *D : Record->decls()) {
9913       if (!ODRHash::isDeclToBeProcessed(D, DC))
9914         continue;
9915       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9916     }
9917   };
9918 
9919   struct DiffResult {
9920     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9921     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9922   };
9923 
9924   // If there is a diagnoseable difference, FirstDiffType and
9925   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9926   // filled in if not EndOfClass.
9927   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9928                                              DeclHashes &SecondHashes) {
9929     DiffResult DR;
9930     auto FirstIt = FirstHashes.begin();
9931     auto SecondIt = SecondHashes.begin();
9932     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9933       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9934           FirstIt->second == SecondIt->second) {
9935         ++FirstIt;
9936         ++SecondIt;
9937         continue;
9938       }
9939 
9940       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9941       DR.SecondDecl =
9942           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9943 
9944       DR.FirstDiffType =
9945           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9946       DR.SecondDiffType =
9947           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9948       return DR;
9949     }
9950     return DR;
9951   };
9952 
9953   // Use this to diagnose that an unexpected Decl was encountered
9954   // or no difference was detected. This causes a generic error
9955   // message to be emitted.
9956   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9957                                       StringRef FirstModule,
9958                                       NamedDecl *SecondRecord,
9959                                       StringRef SecondModule) {
9960     Diag(FirstRecord->getLocation(),
9961          diag::err_module_odr_violation_different_definitions)
9962         << FirstRecord << FirstModule.empty() << FirstModule;
9963 
9964     if (DR.FirstDecl) {
9965       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9966           << FirstRecord << DR.FirstDecl->getSourceRange();
9967     }
9968 
9969     Diag(SecondRecord->getLocation(),
9970          diag::note_module_odr_violation_different_definitions)
9971         << SecondModule;
9972 
9973     if (DR.SecondDecl) {
9974       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9975           << DR.SecondDecl->getSourceRange();
9976     }
9977   };
9978 
9979   auto DiagnoseODRMismatch =
9980       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9981              NamedDecl *SecondRecord, StringRef SecondModule) {
9982         SourceLocation FirstLoc;
9983         SourceRange FirstRange;
9984         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9985         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9986           FirstLoc = FirstTag->getBraceRange().getEnd();
9987         } else {
9988           FirstLoc = DR.FirstDecl->getLocation();
9989           FirstRange = DR.FirstDecl->getSourceRange();
9990         }
9991         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9992             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9993             << DR.FirstDiffType;
9994 
9995         SourceLocation SecondLoc;
9996         SourceRange SecondRange;
9997         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9998         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9999           SecondLoc = SecondTag->getBraceRange().getEnd();
10000         } else {
10001           SecondLoc = DR.SecondDecl->getLocation();
10002           SecondRange = DR.SecondDecl->getSourceRange();
10003         }
10004         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10005             << SecondModule << SecondRange << DR.SecondDiffType;
10006       };
10007 
10008   // Issue any pending ODR-failure diagnostics.
10009   for (auto &Merge : OdrMergeFailures) {
10010     // If we've already pointed out a specific problem with this class, don't
10011     // bother issuing a general "something's different" diagnostic.
10012     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10013       continue;
10014 
10015     bool Diagnosed = false;
10016     CXXRecordDecl *FirstRecord = Merge.first;
10017     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10018     for (auto &RecordPair : Merge.second) {
10019       CXXRecordDecl *SecondRecord = RecordPair.first;
10020       // Multiple different declarations got merged together; tell the user
10021       // where they came from.
10022       if (FirstRecord == SecondRecord)
10023         continue;
10024 
10025       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10026 
10027       auto *FirstDD = FirstRecord->DefinitionData;
10028       auto *SecondDD = RecordPair.second;
10029 
10030       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10031 
10032       // Diagnostics from DefinitionData are emitted here.
10033       if (FirstDD != SecondDD) {
10034         enum ODRDefinitionDataDifference {
10035           NumBases,
10036           NumVBases,
10037           BaseType,
10038           BaseVirtual,
10039           BaseAccess,
10040         };
10041         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10042                                  this](SourceLocation Loc, SourceRange Range,
10043                                        ODRDefinitionDataDifference DiffType) {
10044           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10045                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10046                  << DiffType;
10047         };
10048         auto ODRDiagBaseNote = [&SecondModule,
10049                                 this](SourceLocation Loc, SourceRange Range,
10050                                       ODRDefinitionDataDifference DiffType) {
10051           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10052                  << SecondModule << Range << DiffType;
10053         };
10054 
10055         unsigned FirstNumBases = FirstDD->NumBases;
10056         unsigned FirstNumVBases = FirstDD->NumVBases;
10057         unsigned SecondNumBases = SecondDD->NumBases;
10058         unsigned SecondNumVBases = SecondDD->NumVBases;
10059 
10060         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10061           unsigned NumBases = DD->NumBases;
10062           if (NumBases == 0) return SourceRange();
10063           auto bases = DD->bases();
10064           return SourceRange(bases[0].getBeginLoc(),
10065                              bases[NumBases - 1].getEndLoc());
10066         };
10067 
10068         if (FirstNumBases != SecondNumBases) {
10069           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10070                            NumBases)
10071               << FirstNumBases;
10072           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10073                           NumBases)
10074               << SecondNumBases;
10075           Diagnosed = true;
10076           break;
10077         }
10078 
10079         if (FirstNumVBases != SecondNumVBases) {
10080           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10081                            NumVBases)
10082               << FirstNumVBases;
10083           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10084                           NumVBases)
10085               << SecondNumVBases;
10086           Diagnosed = true;
10087           break;
10088         }
10089 
10090         auto FirstBases = FirstDD->bases();
10091         auto SecondBases = SecondDD->bases();
10092         unsigned i = 0;
10093         for (i = 0; i < FirstNumBases; ++i) {
10094           auto FirstBase = FirstBases[i];
10095           auto SecondBase = SecondBases[i];
10096           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10097               ComputeQualTypeODRHash(SecondBase.getType())) {
10098             ODRDiagBaseError(FirstRecord->getLocation(),
10099                              FirstBase.getSourceRange(), BaseType)
10100                 << (i + 1) << FirstBase.getType();
10101             ODRDiagBaseNote(SecondRecord->getLocation(),
10102                             SecondBase.getSourceRange(), BaseType)
10103                 << (i + 1) << SecondBase.getType();
10104             break;
10105           }
10106 
10107           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10108             ODRDiagBaseError(FirstRecord->getLocation(),
10109                              FirstBase.getSourceRange(), BaseVirtual)
10110                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10111             ODRDiagBaseNote(SecondRecord->getLocation(),
10112                             SecondBase.getSourceRange(), BaseVirtual)
10113                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10114             break;
10115           }
10116 
10117           if (FirstBase.getAccessSpecifierAsWritten() !=
10118               SecondBase.getAccessSpecifierAsWritten()) {
10119             ODRDiagBaseError(FirstRecord->getLocation(),
10120                              FirstBase.getSourceRange(), BaseAccess)
10121                 << (i + 1) << FirstBase.getType()
10122                 << (int)FirstBase.getAccessSpecifierAsWritten();
10123             ODRDiagBaseNote(SecondRecord->getLocation(),
10124                             SecondBase.getSourceRange(), BaseAccess)
10125                 << (i + 1) << SecondBase.getType()
10126                 << (int)SecondBase.getAccessSpecifierAsWritten();
10127             break;
10128           }
10129         }
10130 
10131         if (i != FirstNumBases) {
10132           Diagnosed = true;
10133           break;
10134         }
10135       }
10136 
10137       const ClassTemplateDecl *FirstTemplate =
10138           FirstRecord->getDescribedClassTemplate();
10139       const ClassTemplateDecl *SecondTemplate =
10140           SecondRecord->getDescribedClassTemplate();
10141 
10142       assert(!FirstTemplate == !SecondTemplate &&
10143              "Both pointers should be null or non-null");
10144 
10145       enum ODRTemplateDifference {
10146         ParamEmptyName,
10147         ParamName,
10148         ParamSingleDefaultArgument,
10149         ParamDifferentDefaultArgument,
10150       };
10151 
10152       if (FirstTemplate && SecondTemplate) {
10153         DeclHashes FirstTemplateHashes;
10154         DeclHashes SecondTemplateHashes;
10155 
10156         auto PopulateTemplateParameterHashs =
10157             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10158                                      const ClassTemplateDecl *TD) {
10159               for (auto *D : TD->getTemplateParameters()->asArray()) {
10160                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10161               }
10162             };
10163 
10164         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10165         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10166 
10167         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10168                "Number of template parameters should be equal.");
10169 
10170         auto FirstIt = FirstTemplateHashes.begin();
10171         auto FirstEnd = FirstTemplateHashes.end();
10172         auto SecondIt = SecondTemplateHashes.begin();
10173         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10174           if (FirstIt->second == SecondIt->second)
10175             continue;
10176 
10177           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10178                                           SourceLocation Loc, SourceRange Range,
10179                                           ODRTemplateDifference DiffType) {
10180             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10181                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10182                    << DiffType;
10183           };
10184           auto ODRDiagTemplateNote = [&SecondModule, this](
10185                                          SourceLocation Loc, SourceRange Range,
10186                                          ODRTemplateDifference DiffType) {
10187             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10188                    << SecondModule << Range << DiffType;
10189           };
10190 
10191           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10192           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10193 
10194           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10195                  "Parameter Decl's should be the same kind.");
10196 
10197           DeclarationName FirstName = FirstDecl->getDeclName();
10198           DeclarationName SecondName = SecondDecl->getDeclName();
10199 
10200           if (FirstName != SecondName) {
10201             const bool FirstNameEmpty =
10202                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10203             const bool SecondNameEmpty =
10204                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10205             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10206                    "Both template parameters cannot be unnamed.");
10207             ODRDiagTemplateError(FirstDecl->getLocation(),
10208                                  FirstDecl->getSourceRange(),
10209                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10210                 << FirstName;
10211             ODRDiagTemplateNote(SecondDecl->getLocation(),
10212                                 SecondDecl->getSourceRange(),
10213                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10214                 << SecondName;
10215             break;
10216           }
10217 
10218           switch (FirstDecl->getKind()) {
10219           default:
10220             llvm_unreachable("Invalid template parameter type.");
10221           case Decl::TemplateTypeParm: {
10222             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10223             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10224             const bool HasFirstDefaultArgument =
10225                 FirstParam->hasDefaultArgument() &&
10226                 !FirstParam->defaultArgumentWasInherited();
10227             const bool HasSecondDefaultArgument =
10228                 SecondParam->hasDefaultArgument() &&
10229                 !SecondParam->defaultArgumentWasInherited();
10230 
10231             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10232               ODRDiagTemplateError(FirstDecl->getLocation(),
10233                                    FirstDecl->getSourceRange(),
10234                                    ParamSingleDefaultArgument)
10235                   << HasFirstDefaultArgument;
10236               ODRDiagTemplateNote(SecondDecl->getLocation(),
10237                                   SecondDecl->getSourceRange(),
10238                                   ParamSingleDefaultArgument)
10239                   << HasSecondDefaultArgument;
10240               break;
10241             }
10242 
10243             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10244                    "Expecting default arguments.");
10245 
10246             ODRDiagTemplateError(FirstDecl->getLocation(),
10247                                  FirstDecl->getSourceRange(),
10248                                  ParamDifferentDefaultArgument);
10249             ODRDiagTemplateNote(SecondDecl->getLocation(),
10250                                 SecondDecl->getSourceRange(),
10251                                 ParamDifferentDefaultArgument);
10252 
10253             break;
10254           }
10255           case Decl::NonTypeTemplateParm: {
10256             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10257             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10258             const bool HasFirstDefaultArgument =
10259                 FirstParam->hasDefaultArgument() &&
10260                 !FirstParam->defaultArgumentWasInherited();
10261             const bool HasSecondDefaultArgument =
10262                 SecondParam->hasDefaultArgument() &&
10263                 !SecondParam->defaultArgumentWasInherited();
10264 
10265             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10266               ODRDiagTemplateError(FirstDecl->getLocation(),
10267                                    FirstDecl->getSourceRange(),
10268                                    ParamSingleDefaultArgument)
10269                   << HasFirstDefaultArgument;
10270               ODRDiagTemplateNote(SecondDecl->getLocation(),
10271                                   SecondDecl->getSourceRange(),
10272                                   ParamSingleDefaultArgument)
10273                   << HasSecondDefaultArgument;
10274               break;
10275             }
10276 
10277             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10278                    "Expecting default arguments.");
10279 
10280             ODRDiagTemplateError(FirstDecl->getLocation(),
10281                                  FirstDecl->getSourceRange(),
10282                                  ParamDifferentDefaultArgument);
10283             ODRDiagTemplateNote(SecondDecl->getLocation(),
10284                                 SecondDecl->getSourceRange(),
10285                                 ParamDifferentDefaultArgument);
10286 
10287             break;
10288           }
10289           case Decl::TemplateTemplateParm: {
10290             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10291             const auto *SecondParam =
10292                 cast<TemplateTemplateParmDecl>(SecondDecl);
10293             const bool HasFirstDefaultArgument =
10294                 FirstParam->hasDefaultArgument() &&
10295                 !FirstParam->defaultArgumentWasInherited();
10296             const bool HasSecondDefaultArgument =
10297                 SecondParam->hasDefaultArgument() &&
10298                 !SecondParam->defaultArgumentWasInherited();
10299 
10300             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10301               ODRDiagTemplateError(FirstDecl->getLocation(),
10302                                    FirstDecl->getSourceRange(),
10303                                    ParamSingleDefaultArgument)
10304                   << HasFirstDefaultArgument;
10305               ODRDiagTemplateNote(SecondDecl->getLocation(),
10306                                   SecondDecl->getSourceRange(),
10307                                   ParamSingleDefaultArgument)
10308                   << HasSecondDefaultArgument;
10309               break;
10310             }
10311 
10312             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10313                    "Expecting default arguments.");
10314 
10315             ODRDiagTemplateError(FirstDecl->getLocation(),
10316                                  FirstDecl->getSourceRange(),
10317                                  ParamDifferentDefaultArgument);
10318             ODRDiagTemplateNote(SecondDecl->getLocation(),
10319                                 SecondDecl->getSourceRange(),
10320                                 ParamDifferentDefaultArgument);
10321 
10322             break;
10323           }
10324           }
10325 
10326           break;
10327         }
10328 
10329         if (FirstIt != FirstEnd) {
10330           Diagnosed = true;
10331           break;
10332         }
10333       }
10334 
10335       DeclHashes FirstHashes;
10336       DeclHashes SecondHashes;
10337       const DeclContext *DC = FirstRecord;
10338       PopulateHashes(FirstHashes, FirstRecord, DC);
10339       PopulateHashes(SecondHashes, SecondRecord, DC);
10340 
10341       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10342       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10343       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10344       Decl *FirstDecl = DR.FirstDecl;
10345       Decl *SecondDecl = DR.SecondDecl;
10346 
10347       if (FirstDiffType == Other || SecondDiffType == Other) {
10348         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10349                               SecondModule);
10350         Diagnosed = true;
10351         break;
10352       }
10353 
10354       if (FirstDiffType != SecondDiffType) {
10355         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10356                             SecondModule);
10357         Diagnosed = true;
10358         break;
10359       }
10360 
10361       assert(FirstDiffType == SecondDiffType);
10362 
10363       switch (FirstDiffType) {
10364       case Other:
10365       case EndOfClass:
10366       case PublicSpecifer:
10367       case PrivateSpecifer:
10368       case ProtectedSpecifer:
10369         llvm_unreachable("Invalid diff type");
10370 
10371       case StaticAssert: {
10372         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10373         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10374 
10375         Expr *FirstExpr = FirstSA->getAssertExpr();
10376         Expr *SecondExpr = SecondSA->getAssertExpr();
10377         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10378         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10379         if (FirstODRHash != SecondODRHash) {
10380           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10381                            FirstExpr->getSourceRange(), StaticAssertCondition);
10382           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10383                           SecondExpr->getSourceRange(), StaticAssertCondition);
10384           Diagnosed = true;
10385           break;
10386         }
10387 
10388         StringLiteral *FirstStr = FirstSA->getMessage();
10389         StringLiteral *SecondStr = SecondSA->getMessage();
10390         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10391         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10392           SourceLocation FirstLoc, SecondLoc;
10393           SourceRange FirstRange, SecondRange;
10394           if (FirstStr) {
10395             FirstLoc = FirstStr->getBeginLoc();
10396             FirstRange = FirstStr->getSourceRange();
10397           } else {
10398             FirstLoc = FirstSA->getBeginLoc();
10399             FirstRange = FirstSA->getSourceRange();
10400           }
10401           if (SecondStr) {
10402             SecondLoc = SecondStr->getBeginLoc();
10403             SecondRange = SecondStr->getSourceRange();
10404           } else {
10405             SecondLoc = SecondSA->getBeginLoc();
10406             SecondRange = SecondSA->getSourceRange();
10407           }
10408           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10409                            StaticAssertOnlyMessage)
10410               << (FirstStr == nullptr);
10411           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10412                           StaticAssertOnlyMessage)
10413               << (SecondStr == nullptr);
10414           Diagnosed = true;
10415           break;
10416         }
10417 
10418         if (FirstStr && SecondStr &&
10419             FirstStr->getString() != SecondStr->getString()) {
10420           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10421                            FirstStr->getSourceRange(), StaticAssertMessage);
10422           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10423                           SecondStr->getSourceRange(), StaticAssertMessage);
10424           Diagnosed = true;
10425           break;
10426         }
10427         break;
10428       }
10429       case Field: {
10430         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10431                                  cast<FieldDecl>(FirstDecl),
10432                                  cast<FieldDecl>(SecondDecl));
10433         break;
10434       }
10435       case CXXMethod: {
10436         enum {
10437           DiagMethod,
10438           DiagConstructor,
10439           DiagDestructor,
10440         } FirstMethodType,
10441             SecondMethodType;
10442         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10443           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10444           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10445           return DiagMethod;
10446         };
10447         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10448         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10449         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10450         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10451         auto FirstName = FirstMethod->getDeclName();
10452         auto SecondName = SecondMethod->getDeclName();
10453         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10454           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10455                            FirstMethod->getSourceRange(), MethodName)
10456               << FirstMethodType << FirstName;
10457           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10458                           SecondMethod->getSourceRange(), MethodName)
10459               << SecondMethodType << SecondName;
10460 
10461           Diagnosed = true;
10462           break;
10463         }
10464 
10465         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10466         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10467         if (FirstDeleted != SecondDeleted) {
10468           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10469                            FirstMethod->getSourceRange(), MethodDeleted)
10470               << FirstMethodType << FirstName << FirstDeleted;
10471 
10472           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10473                           SecondMethod->getSourceRange(), MethodDeleted)
10474               << SecondMethodType << SecondName << SecondDeleted;
10475           Diagnosed = true;
10476           break;
10477         }
10478 
10479         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10480         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10481         if (FirstDefaulted != SecondDefaulted) {
10482           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10483                            FirstMethod->getSourceRange(), MethodDefaulted)
10484               << FirstMethodType << FirstName << FirstDefaulted;
10485 
10486           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10487                           SecondMethod->getSourceRange(), MethodDefaulted)
10488               << SecondMethodType << SecondName << SecondDefaulted;
10489           Diagnosed = true;
10490           break;
10491         }
10492 
10493         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10494         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10495         const bool FirstPure = FirstMethod->isPure();
10496         const bool SecondPure = SecondMethod->isPure();
10497         if ((FirstVirtual || SecondVirtual) &&
10498             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10499           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10500                            FirstMethod->getSourceRange(), MethodVirtual)
10501               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10502           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10503                           SecondMethod->getSourceRange(), MethodVirtual)
10504               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10505           Diagnosed = true;
10506           break;
10507         }
10508 
10509         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10510         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10511         // class needs to be checked instead.
10512         const auto FirstStorage = FirstMethod->getStorageClass();
10513         const auto SecondStorage = SecondMethod->getStorageClass();
10514         const bool FirstStatic = FirstStorage == SC_Static;
10515         const bool SecondStatic = SecondStorage == SC_Static;
10516         if (FirstStatic != SecondStatic) {
10517           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10518                            FirstMethod->getSourceRange(), MethodStatic)
10519               << FirstMethodType << FirstName << FirstStatic;
10520           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10521                           SecondMethod->getSourceRange(), MethodStatic)
10522               << SecondMethodType << SecondName << SecondStatic;
10523           Diagnosed = true;
10524           break;
10525         }
10526 
10527         const bool FirstVolatile = FirstMethod->isVolatile();
10528         const bool SecondVolatile = SecondMethod->isVolatile();
10529         if (FirstVolatile != SecondVolatile) {
10530           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10531                            FirstMethod->getSourceRange(), MethodVolatile)
10532               << FirstMethodType << FirstName << FirstVolatile;
10533           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10534                           SecondMethod->getSourceRange(), MethodVolatile)
10535               << SecondMethodType << SecondName << SecondVolatile;
10536           Diagnosed = true;
10537           break;
10538         }
10539 
10540         const bool FirstConst = FirstMethod->isConst();
10541         const bool SecondConst = SecondMethod->isConst();
10542         if (FirstConst != SecondConst) {
10543           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10544                            FirstMethod->getSourceRange(), MethodConst)
10545               << FirstMethodType << FirstName << FirstConst;
10546           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10547                           SecondMethod->getSourceRange(), MethodConst)
10548               << SecondMethodType << SecondName << SecondConst;
10549           Diagnosed = true;
10550           break;
10551         }
10552 
10553         const bool FirstInline = FirstMethod->isInlineSpecified();
10554         const bool SecondInline = SecondMethod->isInlineSpecified();
10555         if (FirstInline != SecondInline) {
10556           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10557                            FirstMethod->getSourceRange(), MethodInline)
10558               << FirstMethodType << FirstName << FirstInline;
10559           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10560                           SecondMethod->getSourceRange(), MethodInline)
10561               << SecondMethodType << SecondName << SecondInline;
10562           Diagnosed = true;
10563           break;
10564         }
10565 
10566         const unsigned FirstNumParameters = FirstMethod->param_size();
10567         const unsigned SecondNumParameters = SecondMethod->param_size();
10568         if (FirstNumParameters != SecondNumParameters) {
10569           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10570                            FirstMethod->getSourceRange(),
10571                            MethodNumberParameters)
10572               << FirstMethodType << FirstName << FirstNumParameters;
10573           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10574                           SecondMethod->getSourceRange(),
10575                           MethodNumberParameters)
10576               << SecondMethodType << SecondName << SecondNumParameters;
10577           Diagnosed = true;
10578           break;
10579         }
10580 
10581         // Need this status boolean to know when break out of the switch.
10582         bool ParameterMismatch = false;
10583         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10584           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10585           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10586 
10587           QualType FirstParamType = FirstParam->getType();
10588           QualType SecondParamType = SecondParam->getType();
10589           if (FirstParamType != SecondParamType &&
10590               ComputeQualTypeODRHash(FirstParamType) !=
10591                   ComputeQualTypeODRHash(SecondParamType)) {
10592             if (const DecayedType *ParamDecayedType =
10593                     FirstParamType->getAs<DecayedType>()) {
10594               ODRDiagDeclError(
10595                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10596                   FirstMethod->getSourceRange(), MethodParameterType)
10597                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10598                   << true << ParamDecayedType->getOriginalType();
10599             } else {
10600               ODRDiagDeclError(
10601                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10602                   FirstMethod->getSourceRange(), MethodParameterType)
10603                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10604                   << false;
10605             }
10606 
10607             if (const DecayedType *ParamDecayedType =
10608                     SecondParamType->getAs<DecayedType>()) {
10609               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10610                               SecondMethod->getSourceRange(),
10611                               MethodParameterType)
10612                   << SecondMethodType << SecondName << (I + 1)
10613                   << SecondParamType << true
10614                   << ParamDecayedType->getOriginalType();
10615             } else {
10616               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10617                               SecondMethod->getSourceRange(),
10618                               MethodParameterType)
10619                   << SecondMethodType << SecondName << (I + 1)
10620                   << SecondParamType << false;
10621             }
10622             ParameterMismatch = true;
10623             break;
10624           }
10625 
10626           DeclarationName FirstParamName = FirstParam->getDeclName();
10627           DeclarationName SecondParamName = SecondParam->getDeclName();
10628           if (FirstParamName != SecondParamName) {
10629             ODRDiagDeclError(FirstRecord, FirstModule,
10630                              FirstMethod->getLocation(),
10631                              FirstMethod->getSourceRange(), MethodParameterName)
10632                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10633             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10634                             SecondMethod->getSourceRange(), MethodParameterName)
10635                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10636             ParameterMismatch = true;
10637             break;
10638           }
10639 
10640           const Expr *FirstInit = FirstParam->getInit();
10641           const Expr *SecondInit = SecondParam->getInit();
10642           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10643             ODRDiagDeclError(FirstRecord, FirstModule,
10644                              FirstMethod->getLocation(),
10645                              FirstMethod->getSourceRange(),
10646                              MethodParameterSingleDefaultArgument)
10647                 << FirstMethodType << FirstName << (I + 1)
10648                 << (FirstInit == nullptr)
10649                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10650             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10651                             SecondMethod->getSourceRange(),
10652                             MethodParameterSingleDefaultArgument)
10653                 << SecondMethodType << SecondName << (I + 1)
10654                 << (SecondInit == nullptr)
10655                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10656             ParameterMismatch = true;
10657             break;
10658           }
10659 
10660           if (FirstInit && SecondInit &&
10661               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10662             ODRDiagDeclError(FirstRecord, FirstModule,
10663                              FirstMethod->getLocation(),
10664                              FirstMethod->getSourceRange(),
10665                              MethodParameterDifferentDefaultArgument)
10666                 << FirstMethodType << FirstName << (I + 1)
10667                 << FirstInit->getSourceRange();
10668             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10669                             SecondMethod->getSourceRange(),
10670                             MethodParameterDifferentDefaultArgument)
10671                 << SecondMethodType << SecondName << (I + 1)
10672                 << SecondInit->getSourceRange();
10673             ParameterMismatch = true;
10674             break;
10675 
10676           }
10677         }
10678 
10679         if (ParameterMismatch) {
10680           Diagnosed = true;
10681           break;
10682         }
10683 
10684         const auto *FirstTemplateArgs =
10685             FirstMethod->getTemplateSpecializationArgs();
10686         const auto *SecondTemplateArgs =
10687             SecondMethod->getTemplateSpecializationArgs();
10688 
10689         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10690             (!FirstTemplateArgs && SecondTemplateArgs)) {
10691           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10692                            FirstMethod->getSourceRange(),
10693                            MethodNoTemplateArguments)
10694               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10695           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10696                           SecondMethod->getSourceRange(),
10697                           MethodNoTemplateArguments)
10698               << SecondMethodType << SecondName
10699               << (SecondTemplateArgs != nullptr);
10700 
10701           Diagnosed = true;
10702           break;
10703         }
10704 
10705         if (FirstTemplateArgs && SecondTemplateArgs) {
10706           // Remove pack expansions from argument list.
10707           auto ExpandTemplateArgumentList =
10708               [](const TemplateArgumentList *TAL) {
10709                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10710                 for (const TemplateArgument &TA : TAL->asArray()) {
10711                   if (TA.getKind() != TemplateArgument::Pack) {
10712                     ExpandedList.push_back(&TA);
10713                     continue;
10714                   }
10715                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10716                     ExpandedList.push_back(&PackTA);
10717                   }
10718                 }
10719                 return ExpandedList;
10720               };
10721           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10722               ExpandTemplateArgumentList(FirstTemplateArgs);
10723           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10724               ExpandTemplateArgumentList(SecondTemplateArgs);
10725 
10726           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10727             ODRDiagDeclError(FirstRecord, FirstModule,
10728                              FirstMethod->getLocation(),
10729                              FirstMethod->getSourceRange(),
10730                              MethodDifferentNumberTemplateArguments)
10731                 << FirstMethodType << FirstName
10732                 << (unsigned)FirstExpandedList.size();
10733             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10734                             SecondMethod->getSourceRange(),
10735                             MethodDifferentNumberTemplateArguments)
10736                 << SecondMethodType << SecondName
10737                 << (unsigned)SecondExpandedList.size();
10738 
10739             Diagnosed = true;
10740             break;
10741           }
10742 
10743           bool TemplateArgumentMismatch = false;
10744           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10745             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10746                                    &SecondTA = *SecondExpandedList[i];
10747             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10748                 ComputeTemplateArgumentODRHash(SecondTA)) {
10749               continue;
10750             }
10751 
10752             ODRDiagDeclError(
10753                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10754                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10755                 << FirstMethodType << FirstName << FirstTA << i + 1;
10756             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10757                             SecondMethod->getSourceRange(),
10758                             MethodDifferentTemplateArgument)
10759                 << SecondMethodType << SecondName << SecondTA << i + 1;
10760 
10761             TemplateArgumentMismatch = true;
10762             break;
10763           }
10764 
10765           if (TemplateArgumentMismatch) {
10766             Diagnosed = true;
10767             break;
10768           }
10769         }
10770 
10771         // Compute the hash of the method as if it has no body.
10772         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10773           Hash.clear();
10774           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10775           return Hash.CalculateHash();
10776         };
10777 
10778         // Compare the hash generated to the hash stored.  A difference means
10779         // that a body was present in the original source.  Due to merging,
10780         // the stardard way of detecting a body will not work.
10781         const bool HasFirstBody =
10782             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10783         const bool HasSecondBody =
10784             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10785 
10786         if (HasFirstBody != HasSecondBody) {
10787           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10788                            FirstMethod->getSourceRange(), MethodSingleBody)
10789               << FirstMethodType << FirstName << HasFirstBody;
10790           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10791                           SecondMethod->getSourceRange(), MethodSingleBody)
10792               << SecondMethodType << SecondName << HasSecondBody;
10793           Diagnosed = true;
10794           break;
10795         }
10796 
10797         if (HasFirstBody && HasSecondBody) {
10798           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10799                            FirstMethod->getSourceRange(), MethodDifferentBody)
10800               << FirstMethodType << FirstName;
10801           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10802                           SecondMethod->getSourceRange(), MethodDifferentBody)
10803               << SecondMethodType << SecondName;
10804           Diagnosed = true;
10805           break;
10806         }
10807 
10808         break;
10809       }
10810       case TypeAlias:
10811       case TypeDef: {
10812         Diagnosed = ODRDiagTypeDefOrAlias(
10813             FirstRecord, FirstModule, SecondModule,
10814             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10815             FirstDiffType == TypeAlias);
10816         break;
10817       }
10818       case Var: {
10819         Diagnosed =
10820             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10821                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10822         break;
10823       }
10824       case Friend: {
10825         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10826         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10827 
10828         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10829         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10830 
10831         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10832         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10833 
10834         if (FirstND && SecondND) {
10835           ODRDiagDeclError(FirstRecord, FirstModule,
10836                            FirstFriend->getFriendLoc(),
10837                            FirstFriend->getSourceRange(), FriendFunction)
10838               << FirstND;
10839           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10840                           SecondFriend->getSourceRange(), FriendFunction)
10841               << SecondND;
10842 
10843           Diagnosed = true;
10844           break;
10845         }
10846 
10847         if (FirstTSI && SecondTSI) {
10848           QualType FirstFriendType = FirstTSI->getType();
10849           QualType SecondFriendType = SecondTSI->getType();
10850           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10851                  ComputeQualTypeODRHash(SecondFriendType));
10852           ODRDiagDeclError(FirstRecord, FirstModule,
10853                            FirstFriend->getFriendLoc(),
10854                            FirstFriend->getSourceRange(), FriendType)
10855               << FirstFriendType;
10856           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10857                           SecondFriend->getSourceRange(), FriendType)
10858               << SecondFriendType;
10859           Diagnosed = true;
10860           break;
10861         }
10862 
10863         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10864                          FirstFriend->getSourceRange(), FriendTypeFunction)
10865             << (FirstTSI == nullptr);
10866         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10867                         SecondFriend->getSourceRange(), FriendTypeFunction)
10868             << (SecondTSI == nullptr);
10869 
10870         Diagnosed = true;
10871         break;
10872       }
10873       case FunctionTemplate: {
10874         FunctionTemplateDecl *FirstTemplate =
10875             cast<FunctionTemplateDecl>(FirstDecl);
10876         FunctionTemplateDecl *SecondTemplate =
10877             cast<FunctionTemplateDecl>(SecondDecl);
10878 
10879         TemplateParameterList *FirstTPL =
10880             FirstTemplate->getTemplateParameters();
10881         TemplateParameterList *SecondTPL =
10882             SecondTemplate->getTemplateParameters();
10883 
10884         if (FirstTPL->size() != SecondTPL->size()) {
10885           ODRDiagDeclError(FirstRecord, FirstModule,
10886                            FirstTemplate->getLocation(),
10887                            FirstTemplate->getSourceRange(),
10888                            FunctionTemplateDifferentNumberParameters)
10889               << FirstTemplate << FirstTPL->size();
10890           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10891                           SecondTemplate->getSourceRange(),
10892                           FunctionTemplateDifferentNumberParameters)
10893               << SecondTemplate << SecondTPL->size();
10894 
10895           Diagnosed = true;
10896           break;
10897         }
10898 
10899         bool ParameterMismatch = false;
10900         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10901           NamedDecl *FirstParam = FirstTPL->getParam(i);
10902           NamedDecl *SecondParam = SecondTPL->getParam(i);
10903 
10904           if (FirstParam->getKind() != SecondParam->getKind()) {
10905             enum {
10906               TemplateTypeParameter,
10907               NonTypeTemplateParameter,
10908               TemplateTemplateParameter,
10909             };
10910             auto GetParamType = [](NamedDecl *D) {
10911               switch (D->getKind()) {
10912                 default:
10913                   llvm_unreachable("Unexpected template parameter type");
10914                 case Decl::TemplateTypeParm:
10915                   return TemplateTypeParameter;
10916                 case Decl::NonTypeTemplateParm:
10917                   return NonTypeTemplateParameter;
10918                 case Decl::TemplateTemplateParm:
10919                   return TemplateTemplateParameter;
10920               }
10921             };
10922 
10923             ODRDiagDeclError(FirstRecord, FirstModule,
10924                              FirstTemplate->getLocation(),
10925                              FirstTemplate->getSourceRange(),
10926                              FunctionTemplateParameterDifferentKind)
10927                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10928             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10929                             SecondTemplate->getSourceRange(),
10930                             FunctionTemplateParameterDifferentKind)
10931                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10932 
10933             ParameterMismatch = true;
10934             break;
10935           }
10936 
10937           if (FirstParam->getName() != SecondParam->getName()) {
10938             ODRDiagDeclError(
10939                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10940                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10941                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10942                 << FirstParam;
10943             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10944                             SecondTemplate->getSourceRange(),
10945                             FunctionTemplateParameterName)
10946                 << SecondTemplate << (i + 1)
10947                 << (bool)SecondParam->getIdentifier() << SecondParam;
10948             ParameterMismatch = true;
10949             break;
10950           }
10951 
10952           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10953               isa<TemplateTypeParmDecl>(SecondParam)) {
10954             TemplateTypeParmDecl *FirstTTPD =
10955                 cast<TemplateTypeParmDecl>(FirstParam);
10956             TemplateTypeParmDecl *SecondTTPD =
10957                 cast<TemplateTypeParmDecl>(SecondParam);
10958             bool HasFirstDefaultArgument =
10959                 FirstTTPD->hasDefaultArgument() &&
10960                 !FirstTTPD->defaultArgumentWasInherited();
10961             bool HasSecondDefaultArgument =
10962                 SecondTTPD->hasDefaultArgument() &&
10963                 !SecondTTPD->defaultArgumentWasInherited();
10964             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10965               ODRDiagDeclError(FirstRecord, FirstModule,
10966                                FirstTemplate->getLocation(),
10967                                FirstTemplate->getSourceRange(),
10968                                FunctionTemplateParameterSingleDefaultArgument)
10969                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10970               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10971                               SecondTemplate->getSourceRange(),
10972                               FunctionTemplateParameterSingleDefaultArgument)
10973                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10974               ParameterMismatch = true;
10975               break;
10976             }
10977 
10978             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10979               QualType FirstType = FirstTTPD->getDefaultArgument();
10980               QualType SecondType = SecondTTPD->getDefaultArgument();
10981               if (ComputeQualTypeODRHash(FirstType) !=
10982                   ComputeQualTypeODRHash(SecondType)) {
10983                 ODRDiagDeclError(
10984                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10985                     FirstTemplate->getSourceRange(),
10986                     FunctionTemplateParameterDifferentDefaultArgument)
10987                     << FirstTemplate << (i + 1) << FirstType;
10988                 ODRDiagDeclNote(
10989                     SecondModule, SecondTemplate->getLocation(),
10990                     SecondTemplate->getSourceRange(),
10991                     FunctionTemplateParameterDifferentDefaultArgument)
10992                     << SecondTemplate << (i + 1) << SecondType;
10993                 ParameterMismatch = true;
10994                 break;
10995               }
10996             }
10997 
10998             if (FirstTTPD->isParameterPack() !=
10999                 SecondTTPD->isParameterPack()) {
11000               ODRDiagDeclError(FirstRecord, FirstModule,
11001                                FirstTemplate->getLocation(),
11002                                FirstTemplate->getSourceRange(),
11003                                FunctionTemplatePackParameter)
11004                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11005               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11006                               SecondTemplate->getSourceRange(),
11007                               FunctionTemplatePackParameter)
11008                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11009               ParameterMismatch = true;
11010               break;
11011             }
11012           }
11013 
11014           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11015               isa<TemplateTemplateParmDecl>(SecondParam)) {
11016             TemplateTemplateParmDecl *FirstTTPD =
11017                 cast<TemplateTemplateParmDecl>(FirstParam);
11018             TemplateTemplateParmDecl *SecondTTPD =
11019                 cast<TemplateTemplateParmDecl>(SecondParam);
11020 
11021             TemplateParameterList *FirstTPL =
11022                 FirstTTPD->getTemplateParameters();
11023             TemplateParameterList *SecondTPL =
11024                 SecondTTPD->getTemplateParameters();
11025 
11026             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11027                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11028               ODRDiagDeclError(FirstRecord, FirstModule,
11029                                FirstTemplate->getLocation(),
11030                                FirstTemplate->getSourceRange(),
11031                                FunctionTemplateParameterDifferentType)
11032                   << FirstTemplate << (i + 1);
11033               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11034                               SecondTemplate->getSourceRange(),
11035                               FunctionTemplateParameterDifferentType)
11036                   << SecondTemplate << (i + 1);
11037               ParameterMismatch = true;
11038               break;
11039             }
11040 
11041             bool HasFirstDefaultArgument =
11042                 FirstTTPD->hasDefaultArgument() &&
11043                 !FirstTTPD->defaultArgumentWasInherited();
11044             bool HasSecondDefaultArgument =
11045                 SecondTTPD->hasDefaultArgument() &&
11046                 !SecondTTPD->defaultArgumentWasInherited();
11047             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11048               ODRDiagDeclError(FirstRecord, FirstModule,
11049                                FirstTemplate->getLocation(),
11050                                FirstTemplate->getSourceRange(),
11051                                FunctionTemplateParameterSingleDefaultArgument)
11052                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11053               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11054                               SecondTemplate->getSourceRange(),
11055                               FunctionTemplateParameterSingleDefaultArgument)
11056                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11057               ParameterMismatch = true;
11058               break;
11059             }
11060 
11061             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11062               TemplateArgument FirstTA =
11063                   FirstTTPD->getDefaultArgument().getArgument();
11064               TemplateArgument SecondTA =
11065                   SecondTTPD->getDefaultArgument().getArgument();
11066               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11067                   ComputeTemplateArgumentODRHash(SecondTA)) {
11068                 ODRDiagDeclError(
11069                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11070                     FirstTemplate->getSourceRange(),
11071                     FunctionTemplateParameterDifferentDefaultArgument)
11072                     << FirstTemplate << (i + 1) << FirstTA;
11073                 ODRDiagDeclNote(
11074                     SecondModule, SecondTemplate->getLocation(),
11075                     SecondTemplate->getSourceRange(),
11076                     FunctionTemplateParameterDifferentDefaultArgument)
11077                     << SecondTemplate << (i + 1) << SecondTA;
11078                 ParameterMismatch = true;
11079                 break;
11080               }
11081             }
11082 
11083             if (FirstTTPD->isParameterPack() !=
11084                 SecondTTPD->isParameterPack()) {
11085               ODRDiagDeclError(FirstRecord, FirstModule,
11086                                FirstTemplate->getLocation(),
11087                                FirstTemplate->getSourceRange(),
11088                                FunctionTemplatePackParameter)
11089                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11090               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11091                               SecondTemplate->getSourceRange(),
11092                               FunctionTemplatePackParameter)
11093                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11094               ParameterMismatch = true;
11095               break;
11096             }
11097           }
11098 
11099           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11100               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11101             NonTypeTemplateParmDecl *FirstNTTPD =
11102                 cast<NonTypeTemplateParmDecl>(FirstParam);
11103             NonTypeTemplateParmDecl *SecondNTTPD =
11104                 cast<NonTypeTemplateParmDecl>(SecondParam);
11105 
11106             QualType FirstType = FirstNTTPD->getType();
11107             QualType SecondType = SecondNTTPD->getType();
11108             if (ComputeQualTypeODRHash(FirstType) !=
11109                 ComputeQualTypeODRHash(SecondType)) {
11110               ODRDiagDeclError(FirstRecord, FirstModule,
11111                                FirstTemplate->getLocation(),
11112                                FirstTemplate->getSourceRange(),
11113                                FunctionTemplateParameterDifferentType)
11114                   << FirstTemplate << (i + 1);
11115               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11116                               SecondTemplate->getSourceRange(),
11117                               FunctionTemplateParameterDifferentType)
11118                   << SecondTemplate << (i + 1);
11119               ParameterMismatch = true;
11120               break;
11121             }
11122 
11123             bool HasFirstDefaultArgument =
11124                 FirstNTTPD->hasDefaultArgument() &&
11125                 !FirstNTTPD->defaultArgumentWasInherited();
11126             bool HasSecondDefaultArgument =
11127                 SecondNTTPD->hasDefaultArgument() &&
11128                 !SecondNTTPD->defaultArgumentWasInherited();
11129             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11130               ODRDiagDeclError(FirstRecord, FirstModule,
11131                                FirstTemplate->getLocation(),
11132                                FirstTemplate->getSourceRange(),
11133                                FunctionTemplateParameterSingleDefaultArgument)
11134                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11135               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11136                               SecondTemplate->getSourceRange(),
11137                               FunctionTemplateParameterSingleDefaultArgument)
11138                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11139               ParameterMismatch = true;
11140               break;
11141             }
11142 
11143             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11144               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11145               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11146               if (ComputeODRHash(FirstDefaultArgument) !=
11147                   ComputeODRHash(SecondDefaultArgument)) {
11148                 ODRDiagDeclError(
11149                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11150                     FirstTemplate->getSourceRange(),
11151                     FunctionTemplateParameterDifferentDefaultArgument)
11152                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11153                 ODRDiagDeclNote(
11154                     SecondModule, SecondTemplate->getLocation(),
11155                     SecondTemplate->getSourceRange(),
11156                     FunctionTemplateParameterDifferentDefaultArgument)
11157                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11158                 ParameterMismatch = true;
11159                 break;
11160               }
11161             }
11162 
11163             if (FirstNTTPD->isParameterPack() !=
11164                 SecondNTTPD->isParameterPack()) {
11165               ODRDiagDeclError(FirstRecord, FirstModule,
11166                                FirstTemplate->getLocation(),
11167                                FirstTemplate->getSourceRange(),
11168                                FunctionTemplatePackParameter)
11169                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11170               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11171                               SecondTemplate->getSourceRange(),
11172                               FunctionTemplatePackParameter)
11173                   << SecondTemplate << (i + 1)
11174                   << SecondNTTPD->isParameterPack();
11175               ParameterMismatch = true;
11176               break;
11177             }
11178           }
11179         }
11180 
11181         if (ParameterMismatch) {
11182           Diagnosed = true;
11183           break;
11184         }
11185 
11186         break;
11187       }
11188       }
11189 
11190       if (Diagnosed)
11191         continue;
11192 
11193       Diag(FirstDecl->getLocation(),
11194            diag::err_module_odr_violation_mismatch_decl_unknown)
11195           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11196           << FirstDecl->getSourceRange();
11197       Diag(SecondDecl->getLocation(),
11198            diag::note_module_odr_violation_mismatch_decl_unknown)
11199           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11200       Diagnosed = true;
11201     }
11202 
11203     if (!Diagnosed) {
11204       // All definitions are updates to the same declaration. This happens if a
11205       // module instantiates the declaration of a class template specialization
11206       // and two or more other modules instantiate its definition.
11207       //
11208       // FIXME: Indicate which modules had instantiations of this definition.
11209       // FIXME: How can this even happen?
11210       Diag(Merge.first->getLocation(),
11211            diag::err_module_odr_violation_different_instantiations)
11212         << Merge.first;
11213     }
11214   }
11215 
11216   // Issue ODR failures diagnostics for functions.
11217   for (auto &Merge : FunctionOdrMergeFailures) {
11218     enum ODRFunctionDifference {
11219       ReturnType,
11220       ParameterName,
11221       ParameterType,
11222       ParameterSingleDefaultArgument,
11223       ParameterDifferentDefaultArgument,
11224       FunctionBody,
11225     };
11226 
11227     FunctionDecl *FirstFunction = Merge.first;
11228     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11229 
11230     bool Diagnosed = false;
11231     for (auto &SecondFunction : Merge.second) {
11232 
11233       if (FirstFunction == SecondFunction)
11234         continue;
11235 
11236       std::string SecondModule =
11237           getOwningModuleNameForDiagnostic(SecondFunction);
11238 
11239       auto ODRDiagError = [FirstFunction, &FirstModule,
11240                            this](SourceLocation Loc, SourceRange Range,
11241                                  ODRFunctionDifference DiffType) {
11242         return Diag(Loc, diag::err_module_odr_violation_function)
11243                << FirstFunction << FirstModule.empty() << FirstModule << Range
11244                << DiffType;
11245       };
11246       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11247                                                SourceRange Range,
11248                                                ODRFunctionDifference DiffType) {
11249         return Diag(Loc, diag::note_module_odr_violation_function)
11250                << SecondModule << Range << DiffType;
11251       };
11252 
11253       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11254           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11255         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11256                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11257             << FirstFunction->getReturnType();
11258         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11259                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11260             << SecondFunction->getReturnType();
11261         Diagnosed = true;
11262         break;
11263       }
11264 
11265       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11266              "Merged functions with different number of parameters");
11267 
11268       auto ParamSize = FirstFunction->param_size();
11269       bool ParameterMismatch = false;
11270       for (unsigned I = 0; I < ParamSize; ++I) {
11271         auto *FirstParam = FirstFunction->getParamDecl(I);
11272         auto *SecondParam = SecondFunction->getParamDecl(I);
11273 
11274         assert(getContext().hasSameType(FirstParam->getType(),
11275                                       SecondParam->getType()) &&
11276                "Merged function has different parameter types.");
11277 
11278         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11279           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11280                        ParameterName)
11281               << I + 1 << FirstParam->getDeclName();
11282           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11283                       ParameterName)
11284               << I + 1 << SecondParam->getDeclName();
11285           ParameterMismatch = true;
11286           break;
11287         };
11288 
11289         QualType FirstParamType = FirstParam->getType();
11290         QualType SecondParamType = SecondParam->getType();
11291         if (FirstParamType != SecondParamType &&
11292             ComputeQualTypeODRHash(FirstParamType) !=
11293                 ComputeQualTypeODRHash(SecondParamType)) {
11294           if (const DecayedType *ParamDecayedType =
11295                   FirstParamType->getAs<DecayedType>()) {
11296             ODRDiagError(FirstParam->getLocation(),
11297                          FirstParam->getSourceRange(), ParameterType)
11298                 << (I + 1) << FirstParamType << true
11299                 << ParamDecayedType->getOriginalType();
11300           } else {
11301             ODRDiagError(FirstParam->getLocation(),
11302                          FirstParam->getSourceRange(), ParameterType)
11303                 << (I + 1) << FirstParamType << false;
11304           }
11305 
11306           if (const DecayedType *ParamDecayedType =
11307                   SecondParamType->getAs<DecayedType>()) {
11308             ODRDiagNote(SecondParam->getLocation(),
11309                         SecondParam->getSourceRange(), ParameterType)
11310                 << (I + 1) << SecondParamType << true
11311                 << ParamDecayedType->getOriginalType();
11312           } else {
11313             ODRDiagNote(SecondParam->getLocation(),
11314                         SecondParam->getSourceRange(), ParameterType)
11315                 << (I + 1) << SecondParamType << false;
11316           }
11317           ParameterMismatch = true;
11318           break;
11319         }
11320 
11321         const Expr *FirstInit = FirstParam->getInit();
11322         const Expr *SecondInit = SecondParam->getInit();
11323         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11324           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11325                        ParameterSingleDefaultArgument)
11326               << (I + 1) << (FirstInit == nullptr)
11327               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11328           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11329                       ParameterSingleDefaultArgument)
11330               << (I + 1) << (SecondInit == nullptr)
11331               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11332           ParameterMismatch = true;
11333           break;
11334         }
11335 
11336         if (FirstInit && SecondInit &&
11337             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11338           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11339                        ParameterDifferentDefaultArgument)
11340               << (I + 1) << FirstInit->getSourceRange();
11341           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11342                       ParameterDifferentDefaultArgument)
11343               << (I + 1) << SecondInit->getSourceRange();
11344           ParameterMismatch = true;
11345           break;
11346         }
11347 
11348         assert(ComputeSubDeclODRHash(FirstParam) ==
11349                    ComputeSubDeclODRHash(SecondParam) &&
11350                "Undiagnosed parameter difference.");
11351       }
11352 
11353       if (ParameterMismatch) {
11354         Diagnosed = true;
11355         break;
11356       }
11357 
11358       // If no error has been generated before now, assume the problem is in
11359       // the body and generate a message.
11360       ODRDiagError(FirstFunction->getLocation(),
11361                    FirstFunction->getSourceRange(), FunctionBody);
11362       ODRDiagNote(SecondFunction->getLocation(),
11363                   SecondFunction->getSourceRange(), FunctionBody);
11364       Diagnosed = true;
11365       break;
11366     }
11367     (void)Diagnosed;
11368     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11369   }
11370 
11371   // Issue ODR failures diagnostics for enums.
11372   for (auto &Merge : EnumOdrMergeFailures) {
11373     enum ODREnumDifference {
11374       SingleScopedEnum,
11375       EnumTagKeywordMismatch,
11376       SingleSpecifiedType,
11377       DifferentSpecifiedTypes,
11378       DifferentNumberEnumConstants,
11379       EnumConstantName,
11380       EnumConstantSingleInitilizer,
11381       EnumConstantDifferentInitilizer,
11382     };
11383 
11384     // If we've already pointed out a specific problem with this enum, don't
11385     // bother issuing a general "something's different" diagnostic.
11386     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11387       continue;
11388 
11389     EnumDecl *FirstEnum = Merge.first;
11390     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11391 
11392     using DeclHashes =
11393         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11394     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11395                               DeclHashes &Hashes, EnumDecl *Enum) {
11396       for (auto *D : Enum->decls()) {
11397         // Due to decl merging, the first EnumDecl is the parent of
11398         // Decls in both records.
11399         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11400           continue;
11401         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11402         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11403                             ComputeSubDeclODRHash(D));
11404       }
11405     };
11406     DeclHashes FirstHashes;
11407     PopulateHashes(FirstHashes, FirstEnum);
11408     bool Diagnosed = false;
11409     for (auto &SecondEnum : Merge.second) {
11410 
11411       if (FirstEnum == SecondEnum)
11412         continue;
11413 
11414       std::string SecondModule =
11415           getOwningModuleNameForDiagnostic(SecondEnum);
11416 
11417       auto ODRDiagError = [FirstEnum, &FirstModule,
11418                            this](SourceLocation Loc, SourceRange Range,
11419                                  ODREnumDifference DiffType) {
11420         return Diag(Loc, diag::err_module_odr_violation_enum)
11421                << FirstEnum << FirstModule.empty() << FirstModule << Range
11422                << DiffType;
11423       };
11424       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11425                                                SourceRange Range,
11426                                                ODREnumDifference DiffType) {
11427         return Diag(Loc, diag::note_module_odr_violation_enum)
11428                << SecondModule << Range << DiffType;
11429       };
11430 
11431       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11432         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11433                      SingleScopedEnum)
11434             << FirstEnum->isScoped();
11435         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11436                     SingleScopedEnum)
11437             << SecondEnum->isScoped();
11438         Diagnosed = true;
11439         continue;
11440       }
11441 
11442       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11443         if (FirstEnum->isScopedUsingClassTag() !=
11444             SecondEnum->isScopedUsingClassTag()) {
11445           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11446                        EnumTagKeywordMismatch)
11447               << FirstEnum->isScopedUsingClassTag();
11448           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11449                       EnumTagKeywordMismatch)
11450               << SecondEnum->isScopedUsingClassTag();
11451           Diagnosed = true;
11452           continue;
11453         }
11454       }
11455 
11456       QualType FirstUnderlyingType =
11457           FirstEnum->getIntegerTypeSourceInfo()
11458               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11459               : QualType();
11460       QualType SecondUnderlyingType =
11461           SecondEnum->getIntegerTypeSourceInfo()
11462               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11463               : QualType();
11464       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11465           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11466                        SingleSpecifiedType)
11467               << !FirstUnderlyingType.isNull();
11468           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11469                       SingleSpecifiedType)
11470               << !SecondUnderlyingType.isNull();
11471           Diagnosed = true;
11472           continue;
11473       }
11474 
11475       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11476         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11477             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11478           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11479                        DifferentSpecifiedTypes)
11480               << FirstUnderlyingType;
11481           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11482                       DifferentSpecifiedTypes)
11483               << SecondUnderlyingType;
11484           Diagnosed = true;
11485           continue;
11486         }
11487       }
11488 
11489       DeclHashes SecondHashes;
11490       PopulateHashes(SecondHashes, SecondEnum);
11491 
11492       if (FirstHashes.size() != SecondHashes.size()) {
11493         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11494                      DifferentNumberEnumConstants)
11495             << (int)FirstHashes.size();
11496         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11497                     DifferentNumberEnumConstants)
11498             << (int)SecondHashes.size();
11499         Diagnosed = true;
11500         continue;
11501       }
11502 
11503       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11504         if (FirstHashes[I].second == SecondHashes[I].second)
11505           continue;
11506         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11507         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11508 
11509         if (FirstEnumConstant->getDeclName() !=
11510             SecondEnumConstant->getDeclName()) {
11511 
11512           ODRDiagError(FirstEnumConstant->getLocation(),
11513                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11514               << I + 1 << FirstEnumConstant;
11515           ODRDiagNote(SecondEnumConstant->getLocation(),
11516                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11517               << I + 1 << SecondEnumConstant;
11518           Diagnosed = true;
11519           break;
11520         }
11521 
11522         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11523         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11524         if (!FirstInit && !SecondInit)
11525           continue;
11526 
11527         if (!FirstInit || !SecondInit) {
11528           ODRDiagError(FirstEnumConstant->getLocation(),
11529                        FirstEnumConstant->getSourceRange(),
11530                        EnumConstantSingleInitilizer)
11531               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11532           ODRDiagNote(SecondEnumConstant->getLocation(),
11533                       SecondEnumConstant->getSourceRange(),
11534                       EnumConstantSingleInitilizer)
11535               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11536           Diagnosed = true;
11537           break;
11538         }
11539 
11540         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11541           ODRDiagError(FirstEnumConstant->getLocation(),
11542                        FirstEnumConstant->getSourceRange(),
11543                        EnumConstantDifferentInitilizer)
11544               << I + 1 << FirstEnumConstant;
11545           ODRDiagNote(SecondEnumConstant->getLocation(),
11546                       SecondEnumConstant->getSourceRange(),
11547                       EnumConstantDifferentInitilizer)
11548               << I + 1 << SecondEnumConstant;
11549           Diagnosed = true;
11550           break;
11551         }
11552       }
11553     }
11554 
11555     (void)Diagnosed;
11556     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11557   }
11558 }
11559 
11560 void ASTReader::StartedDeserializing() {
11561   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11562     ReadTimer->startTimer();
11563 }
11564 
11565 void ASTReader::FinishedDeserializing() {
11566   assert(NumCurrentElementsDeserializing &&
11567          "FinishedDeserializing not paired with StartedDeserializing");
11568   if (NumCurrentElementsDeserializing == 1) {
11569     // We decrease NumCurrentElementsDeserializing only after pending actions
11570     // are finished, to avoid recursively re-calling finishPendingActions().
11571     finishPendingActions();
11572   }
11573   --NumCurrentElementsDeserializing;
11574 
11575   if (NumCurrentElementsDeserializing == 0) {
11576     // Propagate exception specification and deduced type updates along
11577     // redeclaration chains.
11578     //
11579     // We do this now rather than in finishPendingActions because we want to
11580     // be able to walk the complete redeclaration chains of the updated decls.
11581     while (!PendingExceptionSpecUpdates.empty() ||
11582            !PendingDeducedTypeUpdates.empty()) {
11583       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11584       PendingExceptionSpecUpdates.clear();
11585       for (auto Update : ESUpdates) {
11586         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11587         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11588         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11589         if (auto *Listener = getContext().getASTMutationListener())
11590           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11591         for (auto *Redecl : Update.second->redecls())
11592           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11593       }
11594 
11595       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11596       PendingDeducedTypeUpdates.clear();
11597       for (auto Update : DTUpdates) {
11598         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11599         // FIXME: If the return type is already deduced, check that it matches.
11600         getContext().adjustDeducedFunctionResultType(Update.first,
11601                                                      Update.second);
11602       }
11603     }
11604 
11605     if (ReadTimer)
11606       ReadTimer->stopTimer();
11607 
11608     diagnoseOdrViolations();
11609 
11610     // We are not in recursive loading, so it's safe to pass the "interesting"
11611     // decls to the consumer.
11612     if (Consumer)
11613       PassInterestingDeclsToConsumer();
11614   }
11615 }
11616 
11617 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11618   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11619     // Remove any fake results before adding any real ones.
11620     auto It = PendingFakeLookupResults.find(II);
11621     if (It != PendingFakeLookupResults.end()) {
11622       for (auto *ND : It->second)
11623         SemaObj->IdResolver.RemoveDecl(ND);
11624       // FIXME: this works around module+PCH performance issue.
11625       // Rather than erase the result from the map, which is O(n), just clear
11626       // the vector of NamedDecls.
11627       It->second.clear();
11628     }
11629   }
11630 
11631   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11632     SemaObj->TUScope->AddDecl(D);
11633   } else if (SemaObj->TUScope) {
11634     // Adding the decl to IdResolver may have failed because it was already in
11635     // (even though it was not added in scope). If it is already in, make sure
11636     // it gets in the scope as well.
11637     if (std::find(SemaObj->IdResolver.begin(Name),
11638                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11639       SemaObj->TUScope->AddDecl(D);
11640   }
11641 }
11642 
11643 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11644                      ASTContext *Context,
11645                      const PCHContainerReader &PCHContainerRdr,
11646                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11647                      StringRef isysroot,
11648                      DisableValidationForModuleKind DisableValidationKind,
11649                      bool AllowASTWithCompilerErrors,
11650                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11651                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11652                      std::unique_ptr<llvm::Timer> ReadTimer)
11653     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11654                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11655                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11656       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11657       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11658       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11659                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11660       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11661       DisableValidationKind(DisableValidationKind),
11662       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11663       AllowConfigurationMismatch(AllowConfigurationMismatch),
11664       ValidateSystemInputs(ValidateSystemInputs),
11665       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11666       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11667   SourceMgr.setExternalSLocEntrySource(this);
11668 
11669   for (const auto &Ext : Extensions) {
11670     auto BlockName = Ext->getExtensionMetadata().BlockName;
11671     auto Known = ModuleFileExtensions.find(BlockName);
11672     if (Known != ModuleFileExtensions.end()) {
11673       Diags.Report(diag::warn_duplicate_module_file_extension)
11674         << BlockName;
11675       continue;
11676     }
11677 
11678     ModuleFileExtensions.insert({BlockName, Ext});
11679   }
11680 }
11681 
11682 ASTReader::~ASTReader() {
11683   if (OwnsDeserializationListener)
11684     delete DeserializationListener;
11685 }
11686 
11687 IdentifierResolver &ASTReader::getIdResolver() {
11688   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11689 }
11690 
11691 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11692                                                unsigned AbbrevID) {
11693   Idx = 0;
11694   Record.clear();
11695   return Cursor.readRecord(AbbrevID, Record);
11696 }
11697 //===----------------------------------------------------------------------===//
11698 //// OMPClauseReader implementation
11699 ////===----------------------------------------------------------------------===//
11700 
11701 // This has to be in namespace clang because it's friended by all
11702 // of the OMP clauses.
11703 namespace clang {
11704 
11705 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11706   ASTRecordReader &Record;
11707   ASTContext &Context;
11708 
11709 public:
11710   OMPClauseReader(ASTRecordReader &Record)
11711       : Record(Record), Context(Record.getContext()) {}
11712 #define GEN_CLANG_CLAUSE_CLASS
11713 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11714 #include "llvm/Frontend/OpenMP/OMP.inc"
11715   OMPClause *readClause();
11716   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11717   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11718 };
11719 
11720 } // end namespace clang
11721 
11722 OMPClause *ASTRecordReader::readOMPClause() {
11723   return OMPClauseReader(*this).readClause();
11724 }
11725 
11726 OMPClause *OMPClauseReader::readClause() {
11727   OMPClause *C = nullptr;
11728   switch (llvm::omp::Clause(Record.readInt())) {
11729   case llvm::omp::OMPC_if:
11730     C = new (Context) OMPIfClause();
11731     break;
11732   case llvm::omp::OMPC_final:
11733     C = new (Context) OMPFinalClause();
11734     break;
11735   case llvm::omp::OMPC_num_threads:
11736     C = new (Context) OMPNumThreadsClause();
11737     break;
11738   case llvm::omp::OMPC_safelen:
11739     C = new (Context) OMPSafelenClause();
11740     break;
11741   case llvm::omp::OMPC_simdlen:
11742     C = new (Context) OMPSimdlenClause();
11743     break;
11744   case llvm::omp::OMPC_sizes: {
11745     unsigned NumSizes = Record.readInt();
11746     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11747     break;
11748   }
11749   case llvm::omp::OMPC_full:
11750     C = OMPFullClause::CreateEmpty(Context);
11751     break;
11752   case llvm::omp::OMPC_partial:
11753     C = OMPPartialClause::CreateEmpty(Context);
11754     break;
11755   case llvm::omp::OMPC_allocator:
11756     C = new (Context) OMPAllocatorClause();
11757     break;
11758   case llvm::omp::OMPC_collapse:
11759     C = new (Context) OMPCollapseClause();
11760     break;
11761   case llvm::omp::OMPC_default:
11762     C = new (Context) OMPDefaultClause();
11763     break;
11764   case llvm::omp::OMPC_proc_bind:
11765     C = new (Context) OMPProcBindClause();
11766     break;
11767   case llvm::omp::OMPC_schedule:
11768     C = new (Context) OMPScheduleClause();
11769     break;
11770   case llvm::omp::OMPC_ordered:
11771     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11772     break;
11773   case llvm::omp::OMPC_nowait:
11774     C = new (Context) OMPNowaitClause();
11775     break;
11776   case llvm::omp::OMPC_untied:
11777     C = new (Context) OMPUntiedClause();
11778     break;
11779   case llvm::omp::OMPC_mergeable:
11780     C = new (Context) OMPMergeableClause();
11781     break;
11782   case llvm::omp::OMPC_read:
11783     C = new (Context) OMPReadClause();
11784     break;
11785   case llvm::omp::OMPC_write:
11786     C = new (Context) OMPWriteClause();
11787     break;
11788   case llvm::omp::OMPC_update:
11789     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11790     break;
11791   case llvm::omp::OMPC_capture:
11792     C = new (Context) OMPCaptureClause();
11793     break;
11794   case llvm::omp::OMPC_seq_cst:
11795     C = new (Context) OMPSeqCstClause();
11796     break;
11797   case llvm::omp::OMPC_acq_rel:
11798     C = new (Context) OMPAcqRelClause();
11799     break;
11800   case llvm::omp::OMPC_acquire:
11801     C = new (Context) OMPAcquireClause();
11802     break;
11803   case llvm::omp::OMPC_release:
11804     C = new (Context) OMPReleaseClause();
11805     break;
11806   case llvm::omp::OMPC_relaxed:
11807     C = new (Context) OMPRelaxedClause();
11808     break;
11809   case llvm::omp::OMPC_threads:
11810     C = new (Context) OMPThreadsClause();
11811     break;
11812   case llvm::omp::OMPC_simd:
11813     C = new (Context) OMPSIMDClause();
11814     break;
11815   case llvm::omp::OMPC_nogroup:
11816     C = new (Context) OMPNogroupClause();
11817     break;
11818   case llvm::omp::OMPC_unified_address:
11819     C = new (Context) OMPUnifiedAddressClause();
11820     break;
11821   case llvm::omp::OMPC_unified_shared_memory:
11822     C = new (Context) OMPUnifiedSharedMemoryClause();
11823     break;
11824   case llvm::omp::OMPC_reverse_offload:
11825     C = new (Context) OMPReverseOffloadClause();
11826     break;
11827   case llvm::omp::OMPC_dynamic_allocators:
11828     C = new (Context) OMPDynamicAllocatorsClause();
11829     break;
11830   case llvm::omp::OMPC_atomic_default_mem_order:
11831     C = new (Context) OMPAtomicDefaultMemOrderClause();
11832     break;
11833  case llvm::omp::OMPC_private:
11834     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11835     break;
11836   case llvm::omp::OMPC_firstprivate:
11837     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11838     break;
11839   case llvm::omp::OMPC_lastprivate:
11840     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11841     break;
11842   case llvm::omp::OMPC_shared:
11843     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11844     break;
11845   case llvm::omp::OMPC_reduction: {
11846     unsigned N = Record.readInt();
11847     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11848     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11849     break;
11850   }
11851   case llvm::omp::OMPC_task_reduction:
11852     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11853     break;
11854   case llvm::omp::OMPC_in_reduction:
11855     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11856     break;
11857   case llvm::omp::OMPC_linear:
11858     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11859     break;
11860   case llvm::omp::OMPC_aligned:
11861     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11862     break;
11863   case llvm::omp::OMPC_copyin:
11864     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11865     break;
11866   case llvm::omp::OMPC_copyprivate:
11867     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11868     break;
11869   case llvm::omp::OMPC_flush:
11870     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11871     break;
11872   case llvm::omp::OMPC_depobj:
11873     C = OMPDepobjClause::CreateEmpty(Context);
11874     break;
11875   case llvm::omp::OMPC_depend: {
11876     unsigned NumVars = Record.readInt();
11877     unsigned NumLoops = Record.readInt();
11878     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11879     break;
11880   }
11881   case llvm::omp::OMPC_device:
11882     C = new (Context) OMPDeviceClause();
11883     break;
11884   case llvm::omp::OMPC_map: {
11885     OMPMappableExprListSizeTy Sizes;
11886     Sizes.NumVars = Record.readInt();
11887     Sizes.NumUniqueDeclarations = Record.readInt();
11888     Sizes.NumComponentLists = Record.readInt();
11889     Sizes.NumComponents = Record.readInt();
11890     C = OMPMapClause::CreateEmpty(Context, Sizes);
11891     break;
11892   }
11893   case llvm::omp::OMPC_num_teams:
11894     C = new (Context) OMPNumTeamsClause();
11895     break;
11896   case llvm::omp::OMPC_thread_limit:
11897     C = new (Context) OMPThreadLimitClause();
11898     break;
11899   case llvm::omp::OMPC_priority:
11900     C = new (Context) OMPPriorityClause();
11901     break;
11902   case llvm::omp::OMPC_grainsize:
11903     C = new (Context) OMPGrainsizeClause();
11904     break;
11905   case llvm::omp::OMPC_num_tasks:
11906     C = new (Context) OMPNumTasksClause();
11907     break;
11908   case llvm::omp::OMPC_hint:
11909     C = new (Context) OMPHintClause();
11910     break;
11911   case llvm::omp::OMPC_dist_schedule:
11912     C = new (Context) OMPDistScheduleClause();
11913     break;
11914   case llvm::omp::OMPC_defaultmap:
11915     C = new (Context) OMPDefaultmapClause();
11916     break;
11917   case llvm::omp::OMPC_to: {
11918     OMPMappableExprListSizeTy Sizes;
11919     Sizes.NumVars = Record.readInt();
11920     Sizes.NumUniqueDeclarations = Record.readInt();
11921     Sizes.NumComponentLists = Record.readInt();
11922     Sizes.NumComponents = Record.readInt();
11923     C = OMPToClause::CreateEmpty(Context, Sizes);
11924     break;
11925   }
11926   case llvm::omp::OMPC_from: {
11927     OMPMappableExprListSizeTy Sizes;
11928     Sizes.NumVars = Record.readInt();
11929     Sizes.NumUniqueDeclarations = Record.readInt();
11930     Sizes.NumComponentLists = Record.readInt();
11931     Sizes.NumComponents = Record.readInt();
11932     C = OMPFromClause::CreateEmpty(Context, Sizes);
11933     break;
11934   }
11935   case llvm::omp::OMPC_use_device_ptr: {
11936     OMPMappableExprListSizeTy Sizes;
11937     Sizes.NumVars = Record.readInt();
11938     Sizes.NumUniqueDeclarations = Record.readInt();
11939     Sizes.NumComponentLists = Record.readInt();
11940     Sizes.NumComponents = Record.readInt();
11941     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11942     break;
11943   }
11944   case llvm::omp::OMPC_use_device_addr: {
11945     OMPMappableExprListSizeTy Sizes;
11946     Sizes.NumVars = Record.readInt();
11947     Sizes.NumUniqueDeclarations = Record.readInt();
11948     Sizes.NumComponentLists = Record.readInt();
11949     Sizes.NumComponents = Record.readInt();
11950     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11951     break;
11952   }
11953   case llvm::omp::OMPC_is_device_ptr: {
11954     OMPMappableExprListSizeTy Sizes;
11955     Sizes.NumVars = Record.readInt();
11956     Sizes.NumUniqueDeclarations = Record.readInt();
11957     Sizes.NumComponentLists = Record.readInt();
11958     Sizes.NumComponents = Record.readInt();
11959     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11960     break;
11961   }
11962   case llvm::omp::OMPC_allocate:
11963     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11964     break;
11965   case llvm::omp::OMPC_nontemporal:
11966     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11967     break;
11968   case llvm::omp::OMPC_inclusive:
11969     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11970     break;
11971   case llvm::omp::OMPC_exclusive:
11972     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11973     break;
11974   case llvm::omp::OMPC_order:
11975     C = new (Context) OMPOrderClause();
11976     break;
11977   case llvm::omp::OMPC_init:
11978     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11979     break;
11980   case llvm::omp::OMPC_use:
11981     C = new (Context) OMPUseClause();
11982     break;
11983   case llvm::omp::OMPC_destroy:
11984     C = new (Context) OMPDestroyClause();
11985     break;
11986   case llvm::omp::OMPC_novariants:
11987     C = new (Context) OMPNovariantsClause();
11988     break;
11989   case llvm::omp::OMPC_nocontext:
11990     C = new (Context) OMPNocontextClause();
11991     break;
11992   case llvm::omp::OMPC_detach:
11993     C = new (Context) OMPDetachClause();
11994     break;
11995   case llvm::omp::OMPC_uses_allocators:
11996     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11997     break;
11998   case llvm::omp::OMPC_affinity:
11999     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
12000     break;
12001   case llvm::omp::OMPC_filter:
12002     C = new (Context) OMPFilterClause();
12003     break;
12004 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
12005   case llvm::omp::Enum:                                                        \
12006     break;
12007 #include "llvm/Frontend/OpenMP/OMPKinds.def"
12008   default:
12009     break;
12010   }
12011   assert(C && "Unknown OMPClause type");
12012 
12013   Visit(C);
12014   C->setLocStart(Record.readSourceLocation());
12015   C->setLocEnd(Record.readSourceLocation());
12016 
12017   return C;
12018 }
12019 
12020 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12021   C->setPreInitStmt(Record.readSubStmt(),
12022                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12023 }
12024 
12025 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12026   VisitOMPClauseWithPreInit(C);
12027   C->setPostUpdateExpr(Record.readSubExpr());
12028 }
12029 
12030 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12031   VisitOMPClauseWithPreInit(C);
12032   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12033   C->setNameModifierLoc(Record.readSourceLocation());
12034   C->setColonLoc(Record.readSourceLocation());
12035   C->setCondition(Record.readSubExpr());
12036   C->setLParenLoc(Record.readSourceLocation());
12037 }
12038 
12039 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12040   VisitOMPClauseWithPreInit(C);
12041   C->setCondition(Record.readSubExpr());
12042   C->setLParenLoc(Record.readSourceLocation());
12043 }
12044 
12045 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12046   VisitOMPClauseWithPreInit(C);
12047   C->setNumThreads(Record.readSubExpr());
12048   C->setLParenLoc(Record.readSourceLocation());
12049 }
12050 
12051 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12052   C->setSafelen(Record.readSubExpr());
12053   C->setLParenLoc(Record.readSourceLocation());
12054 }
12055 
12056 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12057   C->setSimdlen(Record.readSubExpr());
12058   C->setLParenLoc(Record.readSourceLocation());
12059 }
12060 
12061 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12062   for (Expr *&E : C->getSizesRefs())
12063     E = Record.readSubExpr();
12064   C->setLParenLoc(Record.readSourceLocation());
12065 }
12066 
12067 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12068 
12069 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12070   C->setFactor(Record.readSubExpr());
12071   C->setLParenLoc(Record.readSourceLocation());
12072 }
12073 
12074 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12075   C->setAllocator(Record.readExpr());
12076   C->setLParenLoc(Record.readSourceLocation());
12077 }
12078 
12079 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12080   C->setNumForLoops(Record.readSubExpr());
12081   C->setLParenLoc(Record.readSourceLocation());
12082 }
12083 
12084 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12085   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12086   C->setLParenLoc(Record.readSourceLocation());
12087   C->setDefaultKindKwLoc(Record.readSourceLocation());
12088 }
12089 
12090 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12091   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12092   C->setLParenLoc(Record.readSourceLocation());
12093   C->setProcBindKindKwLoc(Record.readSourceLocation());
12094 }
12095 
12096 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12097   VisitOMPClauseWithPreInit(C);
12098   C->setScheduleKind(
12099        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12100   C->setFirstScheduleModifier(
12101       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12102   C->setSecondScheduleModifier(
12103       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12104   C->setChunkSize(Record.readSubExpr());
12105   C->setLParenLoc(Record.readSourceLocation());
12106   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12107   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12108   C->setScheduleKindLoc(Record.readSourceLocation());
12109   C->setCommaLoc(Record.readSourceLocation());
12110 }
12111 
12112 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12113   C->setNumForLoops(Record.readSubExpr());
12114   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12115     C->setLoopNumIterations(I, Record.readSubExpr());
12116   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12117     C->setLoopCounter(I, Record.readSubExpr());
12118   C->setLParenLoc(Record.readSourceLocation());
12119 }
12120 
12121 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12122   C->setEventHandler(Record.readSubExpr());
12123   C->setLParenLoc(Record.readSourceLocation());
12124 }
12125 
12126 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12127 
12128 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12129 
12130 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12131 
12132 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12133 
12134 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12135 
12136 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12137   if (C->isExtended()) {
12138     C->setLParenLoc(Record.readSourceLocation());
12139     C->setArgumentLoc(Record.readSourceLocation());
12140     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12141   }
12142 }
12143 
12144 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12145 
12146 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12147 
12148 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12149 
12150 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12151 
12152 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12153 
12154 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12155 
12156 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12157 
12158 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12159 
12160 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12161 
12162 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12163   unsigned NumVars = C->varlist_size();
12164   SmallVector<Expr *, 16> Vars;
12165   Vars.reserve(NumVars);
12166   for (unsigned I = 0; I != NumVars; ++I)
12167     Vars.push_back(Record.readSubExpr());
12168   C->setVarRefs(Vars);
12169   C->setIsTarget(Record.readBool());
12170   C->setIsTargetSync(Record.readBool());
12171   C->setLParenLoc(Record.readSourceLocation());
12172   C->setVarLoc(Record.readSourceLocation());
12173 }
12174 
12175 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12176   C->setInteropVar(Record.readSubExpr());
12177   C->setLParenLoc(Record.readSourceLocation());
12178   C->setVarLoc(Record.readSourceLocation());
12179 }
12180 
12181 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12182   C->setInteropVar(Record.readSubExpr());
12183   C->setLParenLoc(Record.readSourceLocation());
12184   C->setVarLoc(Record.readSourceLocation());
12185 }
12186 
12187 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12188   VisitOMPClauseWithPreInit(C);
12189   C->setCondition(Record.readSubExpr());
12190   C->setLParenLoc(Record.readSourceLocation());
12191 }
12192 
12193 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12194   VisitOMPClauseWithPreInit(C);
12195   C->setCondition(Record.readSubExpr());
12196   C->setLParenLoc(Record.readSourceLocation());
12197 }
12198 
12199 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12200 
12201 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12202     OMPUnifiedSharedMemoryClause *) {}
12203 
12204 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12205 
12206 void
12207 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12208 }
12209 
12210 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12211     OMPAtomicDefaultMemOrderClause *C) {
12212   C->setAtomicDefaultMemOrderKind(
12213       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12214   C->setLParenLoc(Record.readSourceLocation());
12215   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12216 }
12217 
12218 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12219   C->setLParenLoc(Record.readSourceLocation());
12220   unsigned NumVars = C->varlist_size();
12221   SmallVector<Expr *, 16> Vars;
12222   Vars.reserve(NumVars);
12223   for (unsigned i = 0; i != NumVars; ++i)
12224     Vars.push_back(Record.readSubExpr());
12225   C->setVarRefs(Vars);
12226   Vars.clear();
12227   for (unsigned i = 0; i != NumVars; ++i)
12228     Vars.push_back(Record.readSubExpr());
12229   C->setPrivateCopies(Vars);
12230 }
12231 
12232 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12233   VisitOMPClauseWithPreInit(C);
12234   C->setLParenLoc(Record.readSourceLocation());
12235   unsigned NumVars = C->varlist_size();
12236   SmallVector<Expr *, 16> Vars;
12237   Vars.reserve(NumVars);
12238   for (unsigned i = 0; i != NumVars; ++i)
12239     Vars.push_back(Record.readSubExpr());
12240   C->setVarRefs(Vars);
12241   Vars.clear();
12242   for (unsigned i = 0; i != NumVars; ++i)
12243     Vars.push_back(Record.readSubExpr());
12244   C->setPrivateCopies(Vars);
12245   Vars.clear();
12246   for (unsigned i = 0; i != NumVars; ++i)
12247     Vars.push_back(Record.readSubExpr());
12248   C->setInits(Vars);
12249 }
12250 
12251 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12252   VisitOMPClauseWithPostUpdate(C);
12253   C->setLParenLoc(Record.readSourceLocation());
12254   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12255   C->setKindLoc(Record.readSourceLocation());
12256   C->setColonLoc(Record.readSourceLocation());
12257   unsigned NumVars = C->varlist_size();
12258   SmallVector<Expr *, 16> Vars;
12259   Vars.reserve(NumVars);
12260   for (unsigned i = 0; i != NumVars; ++i)
12261     Vars.push_back(Record.readSubExpr());
12262   C->setVarRefs(Vars);
12263   Vars.clear();
12264   for (unsigned i = 0; i != NumVars; ++i)
12265     Vars.push_back(Record.readSubExpr());
12266   C->setPrivateCopies(Vars);
12267   Vars.clear();
12268   for (unsigned i = 0; i != NumVars; ++i)
12269     Vars.push_back(Record.readSubExpr());
12270   C->setSourceExprs(Vars);
12271   Vars.clear();
12272   for (unsigned i = 0; i != NumVars; ++i)
12273     Vars.push_back(Record.readSubExpr());
12274   C->setDestinationExprs(Vars);
12275   Vars.clear();
12276   for (unsigned i = 0; i != NumVars; ++i)
12277     Vars.push_back(Record.readSubExpr());
12278   C->setAssignmentOps(Vars);
12279 }
12280 
12281 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12282   C->setLParenLoc(Record.readSourceLocation());
12283   unsigned NumVars = C->varlist_size();
12284   SmallVector<Expr *, 16> Vars;
12285   Vars.reserve(NumVars);
12286   for (unsigned i = 0; i != NumVars; ++i)
12287     Vars.push_back(Record.readSubExpr());
12288   C->setVarRefs(Vars);
12289 }
12290 
12291 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12292   VisitOMPClauseWithPostUpdate(C);
12293   C->setLParenLoc(Record.readSourceLocation());
12294   C->setModifierLoc(Record.readSourceLocation());
12295   C->setColonLoc(Record.readSourceLocation());
12296   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12297   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12298   C->setQualifierLoc(NNSL);
12299   C->setNameInfo(DNI);
12300 
12301   unsigned NumVars = C->varlist_size();
12302   SmallVector<Expr *, 16> Vars;
12303   Vars.reserve(NumVars);
12304   for (unsigned i = 0; i != NumVars; ++i)
12305     Vars.push_back(Record.readSubExpr());
12306   C->setVarRefs(Vars);
12307   Vars.clear();
12308   for (unsigned i = 0; i != NumVars; ++i)
12309     Vars.push_back(Record.readSubExpr());
12310   C->setPrivates(Vars);
12311   Vars.clear();
12312   for (unsigned i = 0; i != NumVars; ++i)
12313     Vars.push_back(Record.readSubExpr());
12314   C->setLHSExprs(Vars);
12315   Vars.clear();
12316   for (unsigned i = 0; i != NumVars; ++i)
12317     Vars.push_back(Record.readSubExpr());
12318   C->setRHSExprs(Vars);
12319   Vars.clear();
12320   for (unsigned i = 0; i != NumVars; ++i)
12321     Vars.push_back(Record.readSubExpr());
12322   C->setReductionOps(Vars);
12323   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12324     Vars.clear();
12325     for (unsigned i = 0; i != NumVars; ++i)
12326       Vars.push_back(Record.readSubExpr());
12327     C->setInscanCopyOps(Vars);
12328     Vars.clear();
12329     for (unsigned i = 0; i != NumVars; ++i)
12330       Vars.push_back(Record.readSubExpr());
12331     C->setInscanCopyArrayTemps(Vars);
12332     Vars.clear();
12333     for (unsigned i = 0; i != NumVars; ++i)
12334       Vars.push_back(Record.readSubExpr());
12335     C->setInscanCopyArrayElems(Vars);
12336   }
12337 }
12338 
12339 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12340   VisitOMPClauseWithPostUpdate(C);
12341   C->setLParenLoc(Record.readSourceLocation());
12342   C->setColonLoc(Record.readSourceLocation());
12343   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12344   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12345   C->setQualifierLoc(NNSL);
12346   C->setNameInfo(DNI);
12347 
12348   unsigned NumVars = C->varlist_size();
12349   SmallVector<Expr *, 16> Vars;
12350   Vars.reserve(NumVars);
12351   for (unsigned I = 0; I != NumVars; ++I)
12352     Vars.push_back(Record.readSubExpr());
12353   C->setVarRefs(Vars);
12354   Vars.clear();
12355   for (unsigned I = 0; I != NumVars; ++I)
12356     Vars.push_back(Record.readSubExpr());
12357   C->setPrivates(Vars);
12358   Vars.clear();
12359   for (unsigned I = 0; I != NumVars; ++I)
12360     Vars.push_back(Record.readSubExpr());
12361   C->setLHSExprs(Vars);
12362   Vars.clear();
12363   for (unsigned I = 0; I != NumVars; ++I)
12364     Vars.push_back(Record.readSubExpr());
12365   C->setRHSExprs(Vars);
12366   Vars.clear();
12367   for (unsigned I = 0; I != NumVars; ++I)
12368     Vars.push_back(Record.readSubExpr());
12369   C->setReductionOps(Vars);
12370 }
12371 
12372 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12373   VisitOMPClauseWithPostUpdate(C);
12374   C->setLParenLoc(Record.readSourceLocation());
12375   C->setColonLoc(Record.readSourceLocation());
12376   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12377   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12378   C->setQualifierLoc(NNSL);
12379   C->setNameInfo(DNI);
12380 
12381   unsigned NumVars = C->varlist_size();
12382   SmallVector<Expr *, 16> Vars;
12383   Vars.reserve(NumVars);
12384   for (unsigned I = 0; I != NumVars; ++I)
12385     Vars.push_back(Record.readSubExpr());
12386   C->setVarRefs(Vars);
12387   Vars.clear();
12388   for (unsigned I = 0; I != NumVars; ++I)
12389     Vars.push_back(Record.readSubExpr());
12390   C->setPrivates(Vars);
12391   Vars.clear();
12392   for (unsigned I = 0; I != NumVars; ++I)
12393     Vars.push_back(Record.readSubExpr());
12394   C->setLHSExprs(Vars);
12395   Vars.clear();
12396   for (unsigned I = 0; I != NumVars; ++I)
12397     Vars.push_back(Record.readSubExpr());
12398   C->setRHSExprs(Vars);
12399   Vars.clear();
12400   for (unsigned I = 0; I != NumVars; ++I)
12401     Vars.push_back(Record.readSubExpr());
12402   C->setReductionOps(Vars);
12403   Vars.clear();
12404   for (unsigned I = 0; I != NumVars; ++I)
12405     Vars.push_back(Record.readSubExpr());
12406   C->setTaskgroupDescriptors(Vars);
12407 }
12408 
12409 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12410   VisitOMPClauseWithPostUpdate(C);
12411   C->setLParenLoc(Record.readSourceLocation());
12412   C->setColonLoc(Record.readSourceLocation());
12413   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12414   C->setModifierLoc(Record.readSourceLocation());
12415   unsigned NumVars = C->varlist_size();
12416   SmallVector<Expr *, 16> Vars;
12417   Vars.reserve(NumVars);
12418   for (unsigned i = 0; i != NumVars; ++i)
12419     Vars.push_back(Record.readSubExpr());
12420   C->setVarRefs(Vars);
12421   Vars.clear();
12422   for (unsigned i = 0; i != NumVars; ++i)
12423     Vars.push_back(Record.readSubExpr());
12424   C->setPrivates(Vars);
12425   Vars.clear();
12426   for (unsigned i = 0; i != NumVars; ++i)
12427     Vars.push_back(Record.readSubExpr());
12428   C->setInits(Vars);
12429   Vars.clear();
12430   for (unsigned i = 0; i != NumVars; ++i)
12431     Vars.push_back(Record.readSubExpr());
12432   C->setUpdates(Vars);
12433   Vars.clear();
12434   for (unsigned i = 0; i != NumVars; ++i)
12435     Vars.push_back(Record.readSubExpr());
12436   C->setFinals(Vars);
12437   C->setStep(Record.readSubExpr());
12438   C->setCalcStep(Record.readSubExpr());
12439   Vars.clear();
12440   for (unsigned I = 0; I != NumVars + 1; ++I)
12441     Vars.push_back(Record.readSubExpr());
12442   C->setUsedExprs(Vars);
12443 }
12444 
12445 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12446   C->setLParenLoc(Record.readSourceLocation());
12447   C->setColonLoc(Record.readSourceLocation());
12448   unsigned NumVars = C->varlist_size();
12449   SmallVector<Expr *, 16> Vars;
12450   Vars.reserve(NumVars);
12451   for (unsigned i = 0; i != NumVars; ++i)
12452     Vars.push_back(Record.readSubExpr());
12453   C->setVarRefs(Vars);
12454   C->setAlignment(Record.readSubExpr());
12455 }
12456 
12457 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12458   C->setLParenLoc(Record.readSourceLocation());
12459   unsigned NumVars = C->varlist_size();
12460   SmallVector<Expr *, 16> Exprs;
12461   Exprs.reserve(NumVars);
12462   for (unsigned i = 0; i != NumVars; ++i)
12463     Exprs.push_back(Record.readSubExpr());
12464   C->setVarRefs(Exprs);
12465   Exprs.clear();
12466   for (unsigned i = 0; i != NumVars; ++i)
12467     Exprs.push_back(Record.readSubExpr());
12468   C->setSourceExprs(Exprs);
12469   Exprs.clear();
12470   for (unsigned i = 0; i != NumVars; ++i)
12471     Exprs.push_back(Record.readSubExpr());
12472   C->setDestinationExprs(Exprs);
12473   Exprs.clear();
12474   for (unsigned i = 0; i != NumVars; ++i)
12475     Exprs.push_back(Record.readSubExpr());
12476   C->setAssignmentOps(Exprs);
12477 }
12478 
12479 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12480   C->setLParenLoc(Record.readSourceLocation());
12481   unsigned NumVars = C->varlist_size();
12482   SmallVector<Expr *, 16> Exprs;
12483   Exprs.reserve(NumVars);
12484   for (unsigned i = 0; i != NumVars; ++i)
12485     Exprs.push_back(Record.readSubExpr());
12486   C->setVarRefs(Exprs);
12487   Exprs.clear();
12488   for (unsigned i = 0; i != NumVars; ++i)
12489     Exprs.push_back(Record.readSubExpr());
12490   C->setSourceExprs(Exprs);
12491   Exprs.clear();
12492   for (unsigned i = 0; i != NumVars; ++i)
12493     Exprs.push_back(Record.readSubExpr());
12494   C->setDestinationExprs(Exprs);
12495   Exprs.clear();
12496   for (unsigned i = 0; i != NumVars; ++i)
12497     Exprs.push_back(Record.readSubExpr());
12498   C->setAssignmentOps(Exprs);
12499 }
12500 
12501 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12502   C->setLParenLoc(Record.readSourceLocation());
12503   unsigned NumVars = C->varlist_size();
12504   SmallVector<Expr *, 16> Vars;
12505   Vars.reserve(NumVars);
12506   for (unsigned i = 0; i != NumVars; ++i)
12507     Vars.push_back(Record.readSubExpr());
12508   C->setVarRefs(Vars);
12509 }
12510 
12511 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12512   C->setDepobj(Record.readSubExpr());
12513   C->setLParenLoc(Record.readSourceLocation());
12514 }
12515 
12516 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12517   C->setLParenLoc(Record.readSourceLocation());
12518   C->setModifier(Record.readSubExpr());
12519   C->setDependencyKind(
12520       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12521   C->setDependencyLoc(Record.readSourceLocation());
12522   C->setColonLoc(Record.readSourceLocation());
12523   unsigned NumVars = C->varlist_size();
12524   SmallVector<Expr *, 16> Vars;
12525   Vars.reserve(NumVars);
12526   for (unsigned I = 0; I != NumVars; ++I)
12527     Vars.push_back(Record.readSubExpr());
12528   C->setVarRefs(Vars);
12529   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12530     C->setLoopData(I, Record.readSubExpr());
12531 }
12532 
12533 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12534   VisitOMPClauseWithPreInit(C);
12535   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12536   C->setDevice(Record.readSubExpr());
12537   C->setModifierLoc(Record.readSourceLocation());
12538   C->setLParenLoc(Record.readSourceLocation());
12539 }
12540 
12541 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12542   C->setLParenLoc(Record.readSourceLocation());
12543   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12544     C->setMapTypeModifier(
12545         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12546     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12547   }
12548   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12549   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12550   C->setMapType(
12551      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12552   C->setMapLoc(Record.readSourceLocation());
12553   C->setColonLoc(Record.readSourceLocation());
12554   auto NumVars = C->varlist_size();
12555   auto UniqueDecls = C->getUniqueDeclarationsNum();
12556   auto TotalLists = C->getTotalComponentListNum();
12557   auto TotalComponents = C->getTotalComponentsNum();
12558 
12559   SmallVector<Expr *, 16> Vars;
12560   Vars.reserve(NumVars);
12561   for (unsigned i = 0; i != NumVars; ++i)
12562     Vars.push_back(Record.readExpr());
12563   C->setVarRefs(Vars);
12564 
12565   SmallVector<Expr *, 16> UDMappers;
12566   UDMappers.reserve(NumVars);
12567   for (unsigned I = 0; I < NumVars; ++I)
12568     UDMappers.push_back(Record.readExpr());
12569   C->setUDMapperRefs(UDMappers);
12570 
12571   SmallVector<ValueDecl *, 16> Decls;
12572   Decls.reserve(UniqueDecls);
12573   for (unsigned i = 0; i < UniqueDecls; ++i)
12574     Decls.push_back(Record.readDeclAs<ValueDecl>());
12575   C->setUniqueDecls(Decls);
12576 
12577   SmallVector<unsigned, 16> ListsPerDecl;
12578   ListsPerDecl.reserve(UniqueDecls);
12579   for (unsigned i = 0; i < UniqueDecls; ++i)
12580     ListsPerDecl.push_back(Record.readInt());
12581   C->setDeclNumLists(ListsPerDecl);
12582 
12583   SmallVector<unsigned, 32> ListSizes;
12584   ListSizes.reserve(TotalLists);
12585   for (unsigned i = 0; i < TotalLists; ++i)
12586     ListSizes.push_back(Record.readInt());
12587   C->setComponentListSizes(ListSizes);
12588 
12589   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12590   Components.reserve(TotalComponents);
12591   for (unsigned i = 0; i < TotalComponents; ++i) {
12592     Expr *AssociatedExprPr = Record.readExpr();
12593     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12594     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12595                             /*IsNonContiguous=*/false);
12596   }
12597   C->setComponents(Components, ListSizes);
12598 }
12599 
12600 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12601   C->setLParenLoc(Record.readSourceLocation());
12602   C->setColonLoc(Record.readSourceLocation());
12603   C->setAllocator(Record.readSubExpr());
12604   unsigned NumVars = C->varlist_size();
12605   SmallVector<Expr *, 16> Vars;
12606   Vars.reserve(NumVars);
12607   for (unsigned i = 0; i != NumVars; ++i)
12608     Vars.push_back(Record.readSubExpr());
12609   C->setVarRefs(Vars);
12610 }
12611 
12612 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12613   VisitOMPClauseWithPreInit(C);
12614   C->setNumTeams(Record.readSubExpr());
12615   C->setLParenLoc(Record.readSourceLocation());
12616 }
12617 
12618 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12619   VisitOMPClauseWithPreInit(C);
12620   C->setThreadLimit(Record.readSubExpr());
12621   C->setLParenLoc(Record.readSourceLocation());
12622 }
12623 
12624 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12625   VisitOMPClauseWithPreInit(C);
12626   C->setPriority(Record.readSubExpr());
12627   C->setLParenLoc(Record.readSourceLocation());
12628 }
12629 
12630 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12631   VisitOMPClauseWithPreInit(C);
12632   C->setGrainsize(Record.readSubExpr());
12633   C->setLParenLoc(Record.readSourceLocation());
12634 }
12635 
12636 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12637   VisitOMPClauseWithPreInit(C);
12638   C->setNumTasks(Record.readSubExpr());
12639   C->setLParenLoc(Record.readSourceLocation());
12640 }
12641 
12642 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12643   C->setHint(Record.readSubExpr());
12644   C->setLParenLoc(Record.readSourceLocation());
12645 }
12646 
12647 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12648   VisitOMPClauseWithPreInit(C);
12649   C->setDistScheduleKind(
12650       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12651   C->setChunkSize(Record.readSubExpr());
12652   C->setLParenLoc(Record.readSourceLocation());
12653   C->setDistScheduleKindLoc(Record.readSourceLocation());
12654   C->setCommaLoc(Record.readSourceLocation());
12655 }
12656 
12657 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12658   C->setDefaultmapKind(
12659        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12660   C->setDefaultmapModifier(
12661       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12662   C->setLParenLoc(Record.readSourceLocation());
12663   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12664   C->setDefaultmapKindLoc(Record.readSourceLocation());
12665 }
12666 
12667 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12668   C->setLParenLoc(Record.readSourceLocation());
12669   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12670     C->setMotionModifier(
12671         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12672     C->setMotionModifierLoc(I, Record.readSourceLocation());
12673   }
12674   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12675   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12676   C->setColonLoc(Record.readSourceLocation());
12677   auto NumVars = C->varlist_size();
12678   auto UniqueDecls = C->getUniqueDeclarationsNum();
12679   auto TotalLists = C->getTotalComponentListNum();
12680   auto TotalComponents = C->getTotalComponentsNum();
12681 
12682   SmallVector<Expr *, 16> Vars;
12683   Vars.reserve(NumVars);
12684   for (unsigned i = 0; i != NumVars; ++i)
12685     Vars.push_back(Record.readSubExpr());
12686   C->setVarRefs(Vars);
12687 
12688   SmallVector<Expr *, 16> UDMappers;
12689   UDMappers.reserve(NumVars);
12690   for (unsigned I = 0; I < NumVars; ++I)
12691     UDMappers.push_back(Record.readSubExpr());
12692   C->setUDMapperRefs(UDMappers);
12693 
12694   SmallVector<ValueDecl *, 16> Decls;
12695   Decls.reserve(UniqueDecls);
12696   for (unsigned i = 0; i < UniqueDecls; ++i)
12697     Decls.push_back(Record.readDeclAs<ValueDecl>());
12698   C->setUniqueDecls(Decls);
12699 
12700   SmallVector<unsigned, 16> ListsPerDecl;
12701   ListsPerDecl.reserve(UniqueDecls);
12702   for (unsigned i = 0; i < UniqueDecls; ++i)
12703     ListsPerDecl.push_back(Record.readInt());
12704   C->setDeclNumLists(ListsPerDecl);
12705 
12706   SmallVector<unsigned, 32> ListSizes;
12707   ListSizes.reserve(TotalLists);
12708   for (unsigned i = 0; i < TotalLists; ++i)
12709     ListSizes.push_back(Record.readInt());
12710   C->setComponentListSizes(ListSizes);
12711 
12712   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12713   Components.reserve(TotalComponents);
12714   for (unsigned i = 0; i < TotalComponents; ++i) {
12715     Expr *AssociatedExprPr = Record.readSubExpr();
12716     bool IsNonContiguous = Record.readBool();
12717     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12718     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12719   }
12720   C->setComponents(Components, ListSizes);
12721 }
12722 
12723 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12724   C->setLParenLoc(Record.readSourceLocation());
12725   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12726     C->setMotionModifier(
12727         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12728     C->setMotionModifierLoc(I, Record.readSourceLocation());
12729   }
12730   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12731   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12732   C->setColonLoc(Record.readSourceLocation());
12733   auto NumVars = C->varlist_size();
12734   auto UniqueDecls = C->getUniqueDeclarationsNum();
12735   auto TotalLists = C->getTotalComponentListNum();
12736   auto TotalComponents = C->getTotalComponentsNum();
12737 
12738   SmallVector<Expr *, 16> Vars;
12739   Vars.reserve(NumVars);
12740   for (unsigned i = 0; i != NumVars; ++i)
12741     Vars.push_back(Record.readSubExpr());
12742   C->setVarRefs(Vars);
12743 
12744   SmallVector<Expr *, 16> UDMappers;
12745   UDMappers.reserve(NumVars);
12746   for (unsigned I = 0; I < NumVars; ++I)
12747     UDMappers.push_back(Record.readSubExpr());
12748   C->setUDMapperRefs(UDMappers);
12749 
12750   SmallVector<ValueDecl *, 16> Decls;
12751   Decls.reserve(UniqueDecls);
12752   for (unsigned i = 0; i < UniqueDecls; ++i)
12753     Decls.push_back(Record.readDeclAs<ValueDecl>());
12754   C->setUniqueDecls(Decls);
12755 
12756   SmallVector<unsigned, 16> ListsPerDecl;
12757   ListsPerDecl.reserve(UniqueDecls);
12758   for (unsigned i = 0; i < UniqueDecls; ++i)
12759     ListsPerDecl.push_back(Record.readInt());
12760   C->setDeclNumLists(ListsPerDecl);
12761 
12762   SmallVector<unsigned, 32> ListSizes;
12763   ListSizes.reserve(TotalLists);
12764   for (unsigned i = 0; i < TotalLists; ++i)
12765     ListSizes.push_back(Record.readInt());
12766   C->setComponentListSizes(ListSizes);
12767 
12768   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12769   Components.reserve(TotalComponents);
12770   for (unsigned i = 0; i < TotalComponents; ++i) {
12771     Expr *AssociatedExprPr = Record.readSubExpr();
12772     bool IsNonContiguous = Record.readBool();
12773     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12774     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12775   }
12776   C->setComponents(Components, ListSizes);
12777 }
12778 
12779 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12780   C->setLParenLoc(Record.readSourceLocation());
12781   auto NumVars = C->varlist_size();
12782   auto UniqueDecls = C->getUniqueDeclarationsNum();
12783   auto TotalLists = C->getTotalComponentListNum();
12784   auto TotalComponents = C->getTotalComponentsNum();
12785 
12786   SmallVector<Expr *, 16> Vars;
12787   Vars.reserve(NumVars);
12788   for (unsigned i = 0; i != NumVars; ++i)
12789     Vars.push_back(Record.readSubExpr());
12790   C->setVarRefs(Vars);
12791   Vars.clear();
12792   for (unsigned i = 0; i != NumVars; ++i)
12793     Vars.push_back(Record.readSubExpr());
12794   C->setPrivateCopies(Vars);
12795   Vars.clear();
12796   for (unsigned i = 0; i != NumVars; ++i)
12797     Vars.push_back(Record.readSubExpr());
12798   C->setInits(Vars);
12799 
12800   SmallVector<ValueDecl *, 16> Decls;
12801   Decls.reserve(UniqueDecls);
12802   for (unsigned i = 0; i < UniqueDecls; ++i)
12803     Decls.push_back(Record.readDeclAs<ValueDecl>());
12804   C->setUniqueDecls(Decls);
12805 
12806   SmallVector<unsigned, 16> ListsPerDecl;
12807   ListsPerDecl.reserve(UniqueDecls);
12808   for (unsigned i = 0; i < UniqueDecls; ++i)
12809     ListsPerDecl.push_back(Record.readInt());
12810   C->setDeclNumLists(ListsPerDecl);
12811 
12812   SmallVector<unsigned, 32> ListSizes;
12813   ListSizes.reserve(TotalLists);
12814   for (unsigned i = 0; i < TotalLists; ++i)
12815     ListSizes.push_back(Record.readInt());
12816   C->setComponentListSizes(ListSizes);
12817 
12818   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12819   Components.reserve(TotalComponents);
12820   for (unsigned i = 0; i < TotalComponents; ++i) {
12821     auto *AssociatedExprPr = Record.readSubExpr();
12822     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12823     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12824                             /*IsNonContiguous=*/false);
12825   }
12826   C->setComponents(Components, ListSizes);
12827 }
12828 
12829 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12830   C->setLParenLoc(Record.readSourceLocation());
12831   auto NumVars = C->varlist_size();
12832   auto UniqueDecls = C->getUniqueDeclarationsNum();
12833   auto TotalLists = C->getTotalComponentListNum();
12834   auto TotalComponents = C->getTotalComponentsNum();
12835 
12836   SmallVector<Expr *, 16> Vars;
12837   Vars.reserve(NumVars);
12838   for (unsigned i = 0; i != NumVars; ++i)
12839     Vars.push_back(Record.readSubExpr());
12840   C->setVarRefs(Vars);
12841 
12842   SmallVector<ValueDecl *, 16> Decls;
12843   Decls.reserve(UniqueDecls);
12844   for (unsigned i = 0; i < UniqueDecls; ++i)
12845     Decls.push_back(Record.readDeclAs<ValueDecl>());
12846   C->setUniqueDecls(Decls);
12847 
12848   SmallVector<unsigned, 16> ListsPerDecl;
12849   ListsPerDecl.reserve(UniqueDecls);
12850   for (unsigned i = 0; i < UniqueDecls; ++i)
12851     ListsPerDecl.push_back(Record.readInt());
12852   C->setDeclNumLists(ListsPerDecl);
12853 
12854   SmallVector<unsigned, 32> ListSizes;
12855   ListSizes.reserve(TotalLists);
12856   for (unsigned i = 0; i < TotalLists; ++i)
12857     ListSizes.push_back(Record.readInt());
12858   C->setComponentListSizes(ListSizes);
12859 
12860   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12861   Components.reserve(TotalComponents);
12862   for (unsigned i = 0; i < TotalComponents; ++i) {
12863     Expr *AssociatedExpr = Record.readSubExpr();
12864     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12865     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12866                             /*IsNonContiguous*/ false);
12867   }
12868   C->setComponents(Components, ListSizes);
12869 }
12870 
12871 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12872   C->setLParenLoc(Record.readSourceLocation());
12873   auto NumVars = C->varlist_size();
12874   auto UniqueDecls = C->getUniqueDeclarationsNum();
12875   auto TotalLists = C->getTotalComponentListNum();
12876   auto TotalComponents = C->getTotalComponentsNum();
12877 
12878   SmallVector<Expr *, 16> Vars;
12879   Vars.reserve(NumVars);
12880   for (unsigned i = 0; i != NumVars; ++i)
12881     Vars.push_back(Record.readSubExpr());
12882   C->setVarRefs(Vars);
12883   Vars.clear();
12884 
12885   SmallVector<ValueDecl *, 16> Decls;
12886   Decls.reserve(UniqueDecls);
12887   for (unsigned i = 0; i < UniqueDecls; ++i)
12888     Decls.push_back(Record.readDeclAs<ValueDecl>());
12889   C->setUniqueDecls(Decls);
12890 
12891   SmallVector<unsigned, 16> ListsPerDecl;
12892   ListsPerDecl.reserve(UniqueDecls);
12893   for (unsigned i = 0; i < UniqueDecls; ++i)
12894     ListsPerDecl.push_back(Record.readInt());
12895   C->setDeclNumLists(ListsPerDecl);
12896 
12897   SmallVector<unsigned, 32> ListSizes;
12898   ListSizes.reserve(TotalLists);
12899   for (unsigned i = 0; i < TotalLists; ++i)
12900     ListSizes.push_back(Record.readInt());
12901   C->setComponentListSizes(ListSizes);
12902 
12903   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12904   Components.reserve(TotalComponents);
12905   for (unsigned i = 0; i < TotalComponents; ++i) {
12906     Expr *AssociatedExpr = Record.readSubExpr();
12907     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12908     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12909                             /*IsNonContiguous=*/false);
12910   }
12911   C->setComponents(Components, ListSizes);
12912 }
12913 
12914 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12915   C->setLParenLoc(Record.readSourceLocation());
12916   unsigned NumVars = C->varlist_size();
12917   SmallVector<Expr *, 16> Vars;
12918   Vars.reserve(NumVars);
12919   for (unsigned i = 0; i != NumVars; ++i)
12920     Vars.push_back(Record.readSubExpr());
12921   C->setVarRefs(Vars);
12922   Vars.clear();
12923   Vars.reserve(NumVars);
12924   for (unsigned i = 0; i != NumVars; ++i)
12925     Vars.push_back(Record.readSubExpr());
12926   C->setPrivateRefs(Vars);
12927 }
12928 
12929 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12930   C->setLParenLoc(Record.readSourceLocation());
12931   unsigned NumVars = C->varlist_size();
12932   SmallVector<Expr *, 16> Vars;
12933   Vars.reserve(NumVars);
12934   for (unsigned i = 0; i != NumVars; ++i)
12935     Vars.push_back(Record.readSubExpr());
12936   C->setVarRefs(Vars);
12937 }
12938 
12939 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12940   C->setLParenLoc(Record.readSourceLocation());
12941   unsigned NumVars = C->varlist_size();
12942   SmallVector<Expr *, 16> Vars;
12943   Vars.reserve(NumVars);
12944   for (unsigned i = 0; i != NumVars; ++i)
12945     Vars.push_back(Record.readSubExpr());
12946   C->setVarRefs(Vars);
12947 }
12948 
12949 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12950   C->setLParenLoc(Record.readSourceLocation());
12951   unsigned NumOfAllocators = C->getNumberOfAllocators();
12952   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12953   Data.reserve(NumOfAllocators);
12954   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12955     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12956     D.Allocator = Record.readSubExpr();
12957     D.AllocatorTraits = Record.readSubExpr();
12958     D.LParenLoc = Record.readSourceLocation();
12959     D.RParenLoc = Record.readSourceLocation();
12960   }
12961   C->setAllocatorsData(Data);
12962 }
12963 
12964 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12965   C->setLParenLoc(Record.readSourceLocation());
12966   C->setModifier(Record.readSubExpr());
12967   C->setColonLoc(Record.readSourceLocation());
12968   unsigned NumOfLocators = C->varlist_size();
12969   SmallVector<Expr *, 4> Locators;
12970   Locators.reserve(NumOfLocators);
12971   for (unsigned I = 0; I != NumOfLocators; ++I)
12972     Locators.push_back(Record.readSubExpr());
12973   C->setVarRefs(Locators);
12974 }
12975 
12976 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12977   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12978   C->setLParenLoc(Record.readSourceLocation());
12979   C->setKindKwLoc(Record.readSourceLocation());
12980 }
12981 
12982 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12983   VisitOMPClauseWithPreInit(C);
12984   C->setThreadID(Record.readSubExpr());
12985   C->setLParenLoc(Record.readSourceLocation());
12986 }
12987 
12988 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12989   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12990   TI.Sets.resize(readUInt32());
12991   for (auto &Set : TI.Sets) {
12992     Set.Kind = readEnum<llvm::omp::TraitSet>();
12993     Set.Selectors.resize(readUInt32());
12994     for (auto &Selector : Set.Selectors) {
12995       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12996       Selector.ScoreOrCondition = nullptr;
12997       if (readBool())
12998         Selector.ScoreOrCondition = readExprRef();
12999       Selector.Properties.resize(readUInt32());
13000       for (auto &Property : Selector.Properties)
13001         Property.Kind = readEnum<llvm::omp::TraitProperty>();
13002     }
13003   }
13004   return &TI;
13005 }
13006 
13007 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13008   if (!Data)
13009     return;
13010   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13011     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13012     skipInts(3);
13013   }
13014   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13015   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13016     Clauses[I] = readOMPClause();
13017   Data->setClauses(Clauses);
13018   if (Data->hasAssociatedStmt())
13019     Data->setAssociatedStmt(readStmt());
13020   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13021     Data->getChildren()[I] = readStmt();
13022 }
13023