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