1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Basic/OpenMPKinds.h"
14 #include "clang/Serialization/ASTRecordReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/AbstractTypeReader.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/AST/ASTUnresolvedSet.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/OpenMPClause.h"
35 #include "clang/AST/ODRHash.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/ExceptionSpecificationType.h"
47 #include "clang/Basic/FileManager.h"
48 #include "clang/Basic/FileSystemOptions.h"
49 #include "clang/Basic/IdentifierTable.h"
50 #include "clang/Basic/LLVM.h"
51 #include "clang/Basic/LangOptions.h"
52 #include "clang/Basic/Module.h"
53 #include "clang/Basic/ObjCRuntime.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/InMemoryModuleCache.h"
82 #include "clang/Serialization/ModuleFile.h"
83 #include "clang/Serialization/ModuleFileExtension.h"
84 #include "clang/Serialization/ModuleManager.h"
85 #include "clang/Serialization/PCHContainerOperations.h"
86 #include "clang/Serialization/SerializationDiagnostic.h"
87 #include "llvm/ADT/APFloat.h"
88 #include "llvm/ADT/APInt.h"
89 #include "llvm/ADT/APSInt.h"
90 #include "llvm/ADT/ArrayRef.h"
91 #include "llvm/ADT/DenseMap.h"
92 #include "llvm/ADT/FloatingPointMode.h"
93 #include "llvm/ADT/FoldingSet.h"
94 #include "llvm/ADT/Hashing.h"
95 #include "llvm/ADT/IntrusiveRefCntPtr.h"
96 #include "llvm/ADT/None.h"
97 #include "llvm/ADT/Optional.h"
98 #include "llvm/ADT/STLExtras.h"
99 #include "llvm/ADT/ScopeExit.h"
100 #include "llvm/ADT/SmallPtrSet.h"
101 #include "llvm/ADT/SmallString.h"
102 #include "llvm/ADT/SmallVector.h"
103 #include "llvm/ADT/StringExtras.h"
104 #include "llvm/ADT/StringMap.h"
105 #include "llvm/ADT/StringRef.h"
106 #include "llvm/ADT/Triple.h"
107 #include "llvm/ADT/iterator_range.h"
108 #include "llvm/Bitstream/BitstreamReader.h"
109 #include "llvm/Support/Casting.h"
110 #include "llvm/Support/Compiler.h"
111 #include "llvm/Support/Compression.h"
112 #include "llvm/Support/DJB.h"
113 #include "llvm/Support/Endian.h"
114 #include "llvm/Support/Error.h"
115 #include "llvm/Support/ErrorHandling.h"
116 #include "llvm/Support/FileSystem.h"
117 #include "llvm/Support/LEB128.h"
118 #include "llvm/Support/MemoryBuffer.h"
119 #include "llvm/Support/Path.h"
120 #include "llvm/Support/SaveAndRestore.h"
121 #include "llvm/Support/Timer.h"
122 #include "llvm/Support/VersionTuple.h"
123 #include "llvm/Support/raw_ostream.h"
124 #include <algorithm>
125 #include <cassert>
126 #include <cstddef>
127 #include <cstdint>
128 #include <cstdio>
129 #include <ctime>
130 #include <iterator>
131 #include <limits>
132 #include <map>
133 #include <memory>
134 #include <string>
135 #include <system_error>
136 #include <tuple>
137 #include <utility>
138 #include <vector>
139 
140 using namespace clang;
141 using namespace clang::serialization;
142 using namespace clang::serialization::reader;
143 using llvm::BitstreamCursor;
144 using llvm::RoundingMode;
145 
146 //===----------------------------------------------------------------------===//
147 // ChainedASTReaderListener implementation
148 //===----------------------------------------------------------------------===//
149 
150 bool
151 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
152   return First->ReadFullVersionInformation(FullVersion) ||
153          Second->ReadFullVersionInformation(FullVersion);
154 }
155 
156 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
157   First->ReadModuleName(ModuleName);
158   Second->ReadModuleName(ModuleName);
159 }
160 
161 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
162   First->ReadModuleMapFile(ModuleMapPath);
163   Second->ReadModuleMapFile(ModuleMapPath);
164 }
165 
166 bool
167 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
168                                               bool Complain,
169                                               bool AllowCompatibleDifferences) {
170   return First->ReadLanguageOptions(LangOpts, Complain,
171                                     AllowCompatibleDifferences) ||
172          Second->ReadLanguageOptions(LangOpts, Complain,
173                                      AllowCompatibleDifferences);
174 }
175 
176 bool ChainedASTReaderListener::ReadTargetOptions(
177     const TargetOptions &TargetOpts, bool Complain,
178     bool AllowCompatibleDifferences) {
179   return First->ReadTargetOptions(TargetOpts, Complain,
180                                   AllowCompatibleDifferences) ||
181          Second->ReadTargetOptions(TargetOpts, Complain,
182                                    AllowCompatibleDifferences);
183 }
184 
185 bool ChainedASTReaderListener::ReadDiagnosticOptions(
186     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
187   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
188          Second->ReadDiagnosticOptions(DiagOpts, Complain);
189 }
190 
191 bool
192 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
193                                                 bool Complain) {
194   return First->ReadFileSystemOptions(FSOpts, Complain) ||
195          Second->ReadFileSystemOptions(FSOpts, Complain);
196 }
197 
198 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
199     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
200     bool Complain) {
201   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
202                                         Complain) ||
203          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
204                                          Complain);
205 }
206 
207 bool ChainedASTReaderListener::ReadPreprocessorOptions(
208     const PreprocessorOptions &PPOpts, bool Complain,
209     std::string &SuggestedPredefines) {
210   return First->ReadPreprocessorOptions(PPOpts, Complain,
211                                         SuggestedPredefines) ||
212          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
213 }
214 
215 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
216                                            unsigned Value) {
217   First->ReadCounter(M, Value);
218   Second->ReadCounter(M, Value);
219 }
220 
221 bool ChainedASTReaderListener::needsInputFileVisitation() {
222   return First->needsInputFileVisitation() ||
223          Second->needsInputFileVisitation();
224 }
225 
226 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
227   return First->needsSystemInputFileVisitation() ||
228   Second->needsSystemInputFileVisitation();
229 }
230 
231 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
232                                                ModuleKind Kind) {
233   First->visitModuleFile(Filename, Kind);
234   Second->visitModuleFile(Filename, Kind);
235 }
236 
237 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
238                                               bool isSystem,
239                                               bool isOverridden,
240                                               bool isExplicitModule) {
241   bool Continue = false;
242   if (First->needsInputFileVisitation() &&
243       (!isSystem || First->needsSystemInputFileVisitation()))
244     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
245                                       isExplicitModule);
246   if (Second->needsInputFileVisitation() &&
247       (!isSystem || Second->needsSystemInputFileVisitation()))
248     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
249                                        isExplicitModule);
250   return Continue;
251 }
252 
253 void ChainedASTReaderListener::readModuleFileExtension(
254        const ModuleFileExtensionMetadata &Metadata) {
255   First->readModuleFileExtension(Metadata);
256   Second->readModuleFileExtension(Metadata);
257 }
258 
259 //===----------------------------------------------------------------------===//
260 // PCH validator implementation
261 //===----------------------------------------------------------------------===//
262 
263 ASTReaderListener::~ASTReaderListener() = default;
264 
265 /// Compare the given set of language options against an existing set of
266 /// language options.
267 ///
268 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
269 /// \param AllowCompatibleDifferences If true, differences between compatible
270 ///        language options will be permitted.
271 ///
272 /// \returns true if the languagae options mis-match, false otherwise.
273 static bool checkLanguageOptions(const LangOptions &LangOpts,
274                                  const LangOptions &ExistingLangOpts,
275                                  DiagnosticsEngine *Diags,
276                                  bool AllowCompatibleDifferences = true) {
277 #define LANGOPT(Name, Bits, Default, Description)                 \
278   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
279     if (Diags)                                                    \
280       Diags->Report(diag::err_pch_langopt_mismatch)               \
281         << Description << LangOpts.Name << ExistingLangOpts.Name; \
282     return true;                                                  \
283   }
284 
285 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
286   if (ExistingLangOpts.Name != LangOpts.Name) {           \
287     if (Diags)                                            \
288       Diags->Report(diag::err_pch_langopt_value_mismatch) \
289         << Description;                                   \
290     return true;                                          \
291   }
292 
293 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
294   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
295     if (Diags)                                                 \
296       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
297         << Description;                                        \
298     return true;                                               \
299   }
300 
301 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
302   if (!AllowCompatibleDifferences)                            \
303     LANGOPT(Name, Bits, Default, Description)
304 
305 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
306   if (!AllowCompatibleDifferences)                                 \
307     ENUM_LANGOPT(Name, Bits, Default, Description)
308 
309 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
310   if (!AllowCompatibleDifferences)                                 \
311     VALUE_LANGOPT(Name, Bits, Default, Description)
312 
313 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
314 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
315 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
316 #include "clang/Basic/LangOptions.def"
317 
318   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
321     return true;
322   }
323 
324   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
325     if (Diags)
326       Diags->Report(diag::err_pch_langopt_value_mismatch)
327       << "target Objective-C runtime";
328     return true;
329   }
330 
331   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
332       LangOpts.CommentOpts.BlockCommandNames) {
333     if (Diags)
334       Diags->Report(diag::err_pch_langopt_value_mismatch)
335         << "block command names";
336     return true;
337   }
338 
339   // Sanitizer feature mismatches are treated as compatible differences. If
340   // compatible differences aren't allowed, we still only want to check for
341   // mismatches of non-modular sanitizers (the only ones which can affect AST
342   // generation).
343   if (!AllowCompatibleDifferences) {
344     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
345     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
346     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
347     ExistingSanitizers.clear(ModularSanitizers);
348     ImportedSanitizers.clear(ModularSanitizers);
349     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
350       const std::string Flag = "-fsanitize=";
351       if (Diags) {
352 #define SANITIZER(NAME, ID)                                                    \
353   {                                                                            \
354     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
355     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
356     if (InExistingModule != InImportedModule)                                  \
357       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
358           << InExistingModule << (Flag + NAME);                                \
359   }
360 #include "clang/Basic/Sanitizers.def"
361       }
362       return true;
363     }
364   }
365 
366   return false;
367 }
368 
369 /// Compare the given set of target options against an existing set of
370 /// target options.
371 ///
372 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
373 ///
374 /// \returns true if the target options mis-match, false otherwise.
375 static bool checkTargetOptions(const TargetOptions &TargetOpts,
376                                const TargetOptions &ExistingTargetOpts,
377                                DiagnosticsEngine *Diags,
378                                bool AllowCompatibleDifferences = true) {
379 #define CHECK_TARGET_OPT(Field, Name)                             \
380   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
381     if (Diags)                                                    \
382       Diags->Report(diag::err_pch_targetopt_mismatch)             \
383         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
384     return true;                                                  \
385   }
386 
387   // The triple and ABI must match exactly.
388   CHECK_TARGET_OPT(Triple, "target");
389   CHECK_TARGET_OPT(ABI, "target ABI");
390 
391   // We can tolerate different CPUs in many cases, notably when one CPU
392   // supports a strict superset of another. When allowing compatible
393   // differences skip this check.
394   if (!AllowCompatibleDifferences) {
395     CHECK_TARGET_OPT(CPU, "target CPU");
396     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
397   }
398 
399 #undef CHECK_TARGET_OPT
400 
401   // Compare feature sets.
402   SmallVector<StringRef, 4> ExistingFeatures(
403                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
404                                              ExistingTargetOpts.FeaturesAsWritten.end());
405   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
406                                          TargetOpts.FeaturesAsWritten.end());
407   llvm::sort(ExistingFeatures);
408   llvm::sort(ReadFeatures);
409 
410   // We compute the set difference in both directions explicitly so that we can
411   // diagnose the differences differently.
412   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
413   std::set_difference(
414       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
415       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
416   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
417                       ExistingFeatures.begin(), ExistingFeatures.end(),
418                       std::back_inserter(UnmatchedReadFeatures));
419 
420   // If we are allowing compatible differences and the read feature set is
421   // a strict subset of the existing feature set, there is nothing to diagnose.
422   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
423     return false;
424 
425   if (Diags) {
426     for (StringRef Feature : UnmatchedReadFeatures)
427       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428           << /* is-existing-feature */ false << Feature;
429     for (StringRef Feature : UnmatchedExistingFeatures)
430       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
431           << /* is-existing-feature */ true << Feature;
432   }
433 
434   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
435 }
436 
437 bool
438 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
439                                   bool Complain,
440                                   bool AllowCompatibleDifferences) {
441   const LangOptions &ExistingLangOpts = PP.getLangOpts();
442   return checkLanguageOptions(LangOpts, ExistingLangOpts,
443                               Complain ? &Reader.Diags : nullptr,
444                               AllowCompatibleDifferences);
445 }
446 
447 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
448                                      bool Complain,
449                                      bool AllowCompatibleDifferences) {
450   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
451   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
452                             Complain ? &Reader.Diags : nullptr,
453                             AllowCompatibleDifferences);
454 }
455 
456 namespace {
457 
458 using MacroDefinitionsMap =
459     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
460 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
461 
462 } // namespace
463 
464 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
465                                          DiagnosticsEngine &Diags,
466                                          bool Complain) {
467   using Level = DiagnosticsEngine::Level;
468 
469   // Check current mappings for new -Werror mappings, and the stored mappings
470   // for cases that were explicitly mapped to *not* be errors that are now
471   // errors because of options like -Werror.
472   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
473 
474   for (DiagnosticsEngine *MappingSource : MappingSources) {
475     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
476       diag::kind DiagID = DiagIDMappingPair.first;
477       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
478       if (CurLevel < DiagnosticsEngine::Error)
479         continue; // not significant
480       Level StoredLevel =
481           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
482       if (StoredLevel < DiagnosticsEngine::Error) {
483         if (Complain)
484           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
485               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
486         return true;
487       }
488     }
489   }
490 
491   return false;
492 }
493 
494 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
495   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
496   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
497     return true;
498   return Ext >= diag::Severity::Error;
499 }
500 
501 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
502                                     DiagnosticsEngine &Diags,
503                                     bool IsSystem, bool Complain) {
504   // Top-level options
505   if (IsSystem) {
506     if (Diags.getSuppressSystemWarnings())
507       return false;
508     // If -Wsystem-headers was not enabled before, be conservative
509     if (StoredDiags.getSuppressSystemWarnings()) {
510       if (Complain)
511         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
512       return true;
513     }
514   }
515 
516   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
517     if (Complain)
518       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
519     return true;
520   }
521 
522   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
523       !StoredDiags.getEnableAllWarnings()) {
524     if (Complain)
525       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
526     return true;
527   }
528 
529   if (isExtHandlingFromDiagsError(Diags) &&
530       !isExtHandlingFromDiagsError(StoredDiags)) {
531     if (Complain)
532       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
533     return true;
534   }
535 
536   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
537 }
538 
539 /// Return the top import module if it is implicit, nullptr otherwise.
540 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
541                                           Preprocessor &PP) {
542   // If the original import came from a file explicitly generated by the user,
543   // don't check the diagnostic mappings.
544   // FIXME: currently this is approximated by checking whether this is not a
545   // module import of an implicitly-loaded module file.
546   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
547   // the transitive closure of its imports, since unrelated modules cannot be
548   // imported until after this module finishes validation.
549   ModuleFile *TopImport = &*ModuleMgr.rbegin();
550   while (!TopImport->ImportedBy.empty())
551     TopImport = TopImport->ImportedBy[0];
552   if (TopImport->Kind != MK_ImplicitModule)
553     return nullptr;
554 
555   StringRef ModuleName = TopImport->ModuleName;
556   assert(!ModuleName.empty() && "diagnostic options read before module name");
557 
558   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
559   assert(M && "missing module");
560   return M;
561 }
562 
563 bool PCHValidator::ReadDiagnosticOptions(
564     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
565   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
566   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
567   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
568       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
569   // This should never fail, because we would have processed these options
570   // before writing them to an ASTFile.
571   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
572 
573   ModuleManager &ModuleMgr = Reader.getModuleManager();
574   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
575 
576   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
577   if (!TopM)
578     return false;
579 
580   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
581   // contains the union of their flags.
582   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
583                                  Complain);
584 }
585 
586 /// Collect the macro definitions provided by the given preprocessor
587 /// options.
588 static void
589 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
590                         MacroDefinitionsMap &Macros,
591                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
592   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
593     StringRef Macro = PPOpts.Macros[I].first;
594     bool IsUndef = PPOpts.Macros[I].second;
595 
596     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
597     StringRef MacroName = MacroPair.first;
598     StringRef MacroBody = MacroPair.second;
599 
600     // For an #undef'd macro, we only care about the name.
601     if (IsUndef) {
602       if (MacroNames && !Macros.count(MacroName))
603         MacroNames->push_back(MacroName);
604 
605       Macros[MacroName] = std::make_pair("", true);
606       continue;
607     }
608 
609     // For a #define'd macro, figure out the actual definition.
610     if (MacroName.size() == Macro.size())
611       MacroBody = "1";
612     else {
613       // Note: GCC drops anything following an end-of-line character.
614       StringRef::size_type End = MacroBody.find_first_of("\n\r");
615       MacroBody = MacroBody.substr(0, End);
616     }
617 
618     if (MacroNames && !Macros.count(MacroName))
619       MacroNames->push_back(MacroName);
620     Macros[MacroName] = std::make_pair(MacroBody, false);
621   }
622 }
623 
624 /// Check the preprocessor options deserialized from the control block
625 /// against the preprocessor options in an existing preprocessor.
626 ///
627 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
628 /// \param Validate If true, validate preprocessor options. If false, allow
629 ///        macros defined by \p ExistingPPOpts to override those defined by
630 ///        \p PPOpts in SuggestedPredefines.
631 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
632                                      const PreprocessorOptions &ExistingPPOpts,
633                                      DiagnosticsEngine *Diags,
634                                      FileManager &FileMgr,
635                                      std::string &SuggestedPredefines,
636                                      const LangOptions &LangOpts,
637                                      bool Validate = true) {
638   // Check macro definitions.
639   MacroDefinitionsMap ASTFileMacros;
640   collectMacroDefinitions(PPOpts, ASTFileMacros);
641   MacroDefinitionsMap ExistingMacros;
642   SmallVector<StringRef, 4> ExistingMacroNames;
643   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
644 
645   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
646     // Dig out the macro definition in the existing preprocessor options.
647     StringRef MacroName = ExistingMacroNames[I];
648     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
649 
650     // Check whether we know anything about this macro name or not.
651     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
652         ASTFileMacros.find(MacroName);
653     if (!Validate || Known == ASTFileMacros.end()) {
654       // FIXME: Check whether this identifier was referenced anywhere in the
655       // AST file. If so, we should reject the AST file. Unfortunately, this
656       // information isn't in the control block. What shall we do about it?
657 
658       if (Existing.second) {
659         SuggestedPredefines += "#undef ";
660         SuggestedPredefines += MacroName.str();
661         SuggestedPredefines += '\n';
662       } else {
663         SuggestedPredefines += "#define ";
664         SuggestedPredefines += MacroName.str();
665         SuggestedPredefines += ' ';
666         SuggestedPredefines += Existing.first.str();
667         SuggestedPredefines += '\n';
668       }
669       continue;
670     }
671 
672     // If the macro was defined in one but undef'd in the other, we have a
673     // conflict.
674     if (Existing.second != Known->second.second) {
675       if (Diags) {
676         Diags->Report(diag::err_pch_macro_def_undef)
677           << MacroName << Known->second.second;
678       }
679       return true;
680     }
681 
682     // If the macro was #undef'd in both, or if the macro bodies are identical,
683     // it's fine.
684     if (Existing.second || Existing.first == Known->second.first)
685       continue;
686 
687     // The macro bodies differ; complain.
688     if (Diags) {
689       Diags->Report(diag::err_pch_macro_def_conflict)
690         << MacroName << Known->second.first << Existing.first;
691     }
692     return true;
693   }
694 
695   // Check whether we're using predefines.
696   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
697     if (Diags) {
698       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
699     }
700     return true;
701   }
702 
703   // Detailed record is important since it is used for the module cache hash.
704   if (LangOpts.Modules &&
705       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
706     if (Diags) {
707       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
708     }
709     return true;
710   }
711 
712   // Compute the #include and #include_macros lines we need.
713   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
714     StringRef File = ExistingPPOpts.Includes[I];
715 
716     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
717         !ExistingPPOpts.PCHThroughHeader.empty()) {
718       // In case the through header is an include, we must add all the includes
719       // to the predefines so the start point can be determined.
720       SuggestedPredefines += "#include \"";
721       SuggestedPredefines += File;
722       SuggestedPredefines += "\"\n";
723       continue;
724     }
725 
726     if (File == ExistingPPOpts.ImplicitPCHInclude)
727       continue;
728 
729     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
730           != PPOpts.Includes.end())
731       continue;
732 
733     SuggestedPredefines += "#include \"";
734     SuggestedPredefines += File;
735     SuggestedPredefines += "\"\n";
736   }
737 
738   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
739     StringRef File = ExistingPPOpts.MacroIncludes[I];
740     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
741                   File)
742         != PPOpts.MacroIncludes.end())
743       continue;
744 
745     SuggestedPredefines += "#__include_macros \"";
746     SuggestedPredefines += File;
747     SuggestedPredefines += "\"\n##\n";
748   }
749 
750   return false;
751 }
752 
753 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
754                                            bool Complain,
755                                            std::string &SuggestedPredefines) {
756   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
757 
758   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
759                                   Complain? &Reader.Diags : nullptr,
760                                   PP.getFileManager(),
761                                   SuggestedPredefines,
762                                   PP.getLangOpts());
763 }
764 
765 bool SimpleASTReaderListener::ReadPreprocessorOptions(
766                                   const PreprocessorOptions &PPOpts,
767                                   bool Complain,
768                                   std::string &SuggestedPredefines) {
769   return checkPreprocessorOptions(PPOpts,
770                                   PP.getPreprocessorOpts(),
771                                   nullptr,
772                                   PP.getFileManager(),
773                                   SuggestedPredefines,
774                                   PP.getLangOpts(),
775                                   false);
776 }
777 
778 /// Check the header search options deserialized from the control block
779 /// against the header search options in an existing preprocessor.
780 ///
781 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
782 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
783                                      StringRef SpecificModuleCachePath,
784                                      StringRef ExistingModuleCachePath,
785                                      DiagnosticsEngine *Diags,
786                                      const LangOptions &LangOpts,
787                                      const PreprocessorOptions &PPOpts) {
788   if (LangOpts.Modules) {
789     if (SpecificModuleCachePath != ExistingModuleCachePath &&
790         !PPOpts.AllowPCHWithDifferentModulesCachePath) {
791       if (Diags)
792         Diags->Report(diag::err_pch_modulecache_mismatch)
793           << SpecificModuleCachePath << ExistingModuleCachePath;
794       return true;
795     }
796   }
797 
798   return false;
799 }
800 
801 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
802                                            StringRef SpecificModuleCachePath,
803                                            bool Complain) {
804   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
805                                   PP.getHeaderSearchInfo().getModuleCachePath(),
806                                   Complain ? &Reader.Diags : nullptr,
807                                   PP.getLangOpts(), PP.getPreprocessorOpts());
808 }
809 
810 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
811   PP.setCounterValue(Value);
812 }
813 
814 //===----------------------------------------------------------------------===//
815 // AST reader implementation
816 //===----------------------------------------------------------------------===//
817 
818 static uint64_t readULEB(const unsigned char *&P) {
819   unsigned Length = 0;
820   const char *Error = nullptr;
821 
822   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
823   if (Error)
824     llvm::report_fatal_error(Error);
825   P += Length;
826   return Val;
827 }
828 
829 /// Read ULEB-encoded key length and data length.
830 static std::pair<unsigned, unsigned>
831 readULEBKeyDataLength(const unsigned char *&P) {
832   unsigned KeyLen = readULEB(P);
833   if ((unsigned)KeyLen != KeyLen)
834     llvm::report_fatal_error("key too large");
835 
836   unsigned DataLen = readULEB(P);
837   if ((unsigned)DataLen != DataLen)
838     llvm::report_fatal_error("data too large");
839 
840   return std::make_pair(KeyLen, DataLen);
841 }
842 
843 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
844                                            bool TakeOwnership) {
845   DeserializationListener = Listener;
846   OwnsDeserializationListener = TakeOwnership;
847 }
848 
849 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
850   return serialization::ComputeHash(Sel);
851 }
852 
853 std::pair<unsigned, unsigned>
854 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
855   return readULEBKeyDataLength(d);
856 }
857 
858 ASTSelectorLookupTrait::internal_key_type
859 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
860   using namespace llvm::support;
861 
862   SelectorTable &SelTable = Reader.getContext().Selectors;
863   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
864   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
865       F, endian::readNext<uint32_t, little, unaligned>(d));
866   if (N == 0)
867     return SelTable.getNullarySelector(FirstII);
868   else if (N == 1)
869     return SelTable.getUnarySelector(FirstII);
870 
871   SmallVector<IdentifierInfo *, 16> Args;
872   Args.push_back(FirstII);
873   for (unsigned I = 1; I != N; ++I)
874     Args.push_back(Reader.getLocalIdentifier(
875         F, endian::readNext<uint32_t, little, unaligned>(d)));
876 
877   return SelTable.getSelector(N, Args.data());
878 }
879 
880 ASTSelectorLookupTrait::data_type
881 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
882                                  unsigned DataLen) {
883   using namespace llvm::support;
884 
885   data_type Result;
886 
887   Result.ID = Reader.getGlobalSelectorID(
888       F, endian::readNext<uint32_t, little, unaligned>(d));
889   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
890   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
891   Result.InstanceBits = FullInstanceBits & 0x3;
892   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
893   Result.FactoryBits = FullFactoryBits & 0x3;
894   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
895   unsigned NumInstanceMethods = FullInstanceBits >> 3;
896   unsigned NumFactoryMethods = FullFactoryBits >> 3;
897 
898   // Load instance methods
899   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
900     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
901             F, endian::readNext<uint32_t, little, unaligned>(d)))
902       Result.Instance.push_back(Method);
903   }
904 
905   // Load factory methods
906   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
907     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
908             F, endian::readNext<uint32_t, little, unaligned>(d)))
909       Result.Factory.push_back(Method);
910   }
911 
912   return Result;
913 }
914 
915 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
916   return llvm::djbHash(a);
917 }
918 
919 std::pair<unsigned, unsigned>
920 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
921   return readULEBKeyDataLength(d);
922 }
923 
924 ASTIdentifierLookupTraitBase::internal_key_type
925 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
926   assert(n >= 2 && d[n-1] == '\0');
927   return StringRef((const char*) d, n-1);
928 }
929 
930 /// Whether the given identifier is "interesting".
931 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
932                                     bool IsModule) {
933   return II.hadMacroDefinition() || II.isPoisoned() ||
934          (!IsModule && II.getObjCOrBuiltinID()) ||
935          II.hasRevertedTokenIDToIdentifier() ||
936          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
937           II.getFETokenInfo());
938 }
939 
940 static bool readBit(unsigned &Bits) {
941   bool Value = Bits & 0x1;
942   Bits >>= 1;
943   return Value;
944 }
945 
946 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
947   using namespace llvm::support;
948 
949   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
950   return Reader.getGlobalIdentifierID(F, RawID >> 1);
951 }
952 
953 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
954   if (!II.isFromAST()) {
955     II.setIsFromAST();
956     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
957     if (isInterestingIdentifier(Reader, II, IsModule))
958       II.setChangedSinceDeserialization();
959   }
960 }
961 
962 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
963                                                    const unsigned char* d,
964                                                    unsigned DataLen) {
965   using namespace llvm::support;
966 
967   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
968   bool IsInteresting = RawID & 0x01;
969 
970   // Wipe out the "is interesting" bit.
971   RawID = RawID >> 1;
972 
973   // Build the IdentifierInfo and link the identifier ID with it.
974   IdentifierInfo *II = KnownII;
975   if (!II) {
976     II = &Reader.getIdentifierTable().getOwn(k);
977     KnownII = II;
978   }
979   markIdentifierFromAST(Reader, *II);
980   Reader.markIdentifierUpToDate(II);
981 
982   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
983   if (!IsInteresting) {
984     // For uninteresting identifiers, there's nothing else to do. Just notify
985     // the reader that we've finished loading this identifier.
986     Reader.SetIdentifierInfo(ID, II);
987     return II;
988   }
989 
990   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
991   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
992   bool CPlusPlusOperatorKeyword = readBit(Bits);
993   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
994   bool Poisoned = readBit(Bits);
995   bool ExtensionToken = readBit(Bits);
996   bool HadMacroDefinition = readBit(Bits);
997 
998   assert(Bits == 0 && "Extra bits in the identifier?");
999   DataLen -= 8;
1000 
1001   // Set or check the various bits in the IdentifierInfo structure.
1002   // Token IDs are read-only.
1003   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1004     II->revertTokenIDToIdentifier();
1005   if (!F.isModule())
1006     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1007   assert(II->isExtensionToken() == ExtensionToken &&
1008          "Incorrect extension token flag");
1009   (void)ExtensionToken;
1010   if (Poisoned)
1011     II->setIsPoisoned(true);
1012   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1013          "Incorrect C++ operator keyword flag");
1014   (void)CPlusPlusOperatorKeyword;
1015 
1016   // If this identifier is a macro, deserialize the macro
1017   // definition.
1018   if (HadMacroDefinition) {
1019     uint32_t MacroDirectivesOffset =
1020         endian::readNext<uint32_t, little, unaligned>(d);
1021     DataLen -= 4;
1022 
1023     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1024   }
1025 
1026   Reader.SetIdentifierInfo(ID, II);
1027 
1028   // Read all of the declarations visible at global scope with this
1029   // name.
1030   if (DataLen > 0) {
1031     SmallVector<uint32_t, 4> DeclIDs;
1032     for (; DataLen > 0; DataLen -= 4)
1033       DeclIDs.push_back(Reader.getGlobalDeclID(
1034           F, endian::readNext<uint32_t, little, unaligned>(d)));
1035     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1036   }
1037 
1038   return II;
1039 }
1040 
1041 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1042     : Kind(Name.getNameKind()) {
1043   switch (Kind) {
1044   case DeclarationName::Identifier:
1045     Data = (uint64_t)Name.getAsIdentifierInfo();
1046     break;
1047   case DeclarationName::ObjCZeroArgSelector:
1048   case DeclarationName::ObjCOneArgSelector:
1049   case DeclarationName::ObjCMultiArgSelector:
1050     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1051     break;
1052   case DeclarationName::CXXOperatorName:
1053     Data = Name.getCXXOverloadedOperator();
1054     break;
1055   case DeclarationName::CXXLiteralOperatorName:
1056     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1057     break;
1058   case DeclarationName::CXXDeductionGuideName:
1059     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1060                ->getDeclName().getAsIdentifierInfo();
1061     break;
1062   case DeclarationName::CXXConstructorName:
1063   case DeclarationName::CXXDestructorName:
1064   case DeclarationName::CXXConversionFunctionName:
1065   case DeclarationName::CXXUsingDirective:
1066     Data = 0;
1067     break;
1068   }
1069 }
1070 
1071 unsigned DeclarationNameKey::getHash() const {
1072   llvm::FoldingSetNodeID ID;
1073   ID.AddInteger(Kind);
1074 
1075   switch (Kind) {
1076   case DeclarationName::Identifier:
1077   case DeclarationName::CXXLiteralOperatorName:
1078   case DeclarationName::CXXDeductionGuideName:
1079     ID.AddString(((IdentifierInfo*)Data)->getName());
1080     break;
1081   case DeclarationName::ObjCZeroArgSelector:
1082   case DeclarationName::ObjCOneArgSelector:
1083   case DeclarationName::ObjCMultiArgSelector:
1084     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1085     break;
1086   case DeclarationName::CXXOperatorName:
1087     ID.AddInteger((OverloadedOperatorKind)Data);
1088     break;
1089   case DeclarationName::CXXConstructorName:
1090   case DeclarationName::CXXDestructorName:
1091   case DeclarationName::CXXConversionFunctionName:
1092   case DeclarationName::CXXUsingDirective:
1093     break;
1094   }
1095 
1096   return ID.ComputeHash();
1097 }
1098 
1099 ModuleFile *
1100 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1101   using namespace llvm::support;
1102 
1103   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1104   return Reader.getLocalModuleFile(F, ModuleFileID);
1105 }
1106 
1107 std::pair<unsigned, unsigned>
1108 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1109   return readULEBKeyDataLength(d);
1110 }
1111 
1112 ASTDeclContextNameLookupTrait::internal_key_type
1113 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1114   using namespace llvm::support;
1115 
1116   auto Kind = (DeclarationName::NameKind)*d++;
1117   uint64_t Data;
1118   switch (Kind) {
1119   case DeclarationName::Identifier:
1120   case DeclarationName::CXXLiteralOperatorName:
1121   case DeclarationName::CXXDeductionGuideName:
1122     Data = (uint64_t)Reader.getLocalIdentifier(
1123         F, endian::readNext<uint32_t, little, unaligned>(d));
1124     break;
1125   case DeclarationName::ObjCZeroArgSelector:
1126   case DeclarationName::ObjCOneArgSelector:
1127   case DeclarationName::ObjCMultiArgSelector:
1128     Data =
1129         (uint64_t)Reader.getLocalSelector(
1130                              F, endian::readNext<uint32_t, little, unaligned>(
1131                                     d)).getAsOpaquePtr();
1132     break;
1133   case DeclarationName::CXXOperatorName:
1134     Data = *d++; // OverloadedOperatorKind
1135     break;
1136   case DeclarationName::CXXConstructorName:
1137   case DeclarationName::CXXDestructorName:
1138   case DeclarationName::CXXConversionFunctionName:
1139   case DeclarationName::CXXUsingDirective:
1140     Data = 0;
1141     break;
1142   }
1143 
1144   return DeclarationNameKey(Kind, Data);
1145 }
1146 
1147 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1148                                                  const unsigned char *d,
1149                                                  unsigned DataLen,
1150                                                  data_type_builder &Val) {
1151   using namespace llvm::support;
1152 
1153   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1154     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1155     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1156   }
1157 }
1158 
1159 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1160                                               BitstreamCursor &Cursor,
1161                                               uint64_t Offset,
1162                                               DeclContext *DC) {
1163   assert(Offset != 0);
1164 
1165   SavedStreamPosition SavedPosition(Cursor);
1166   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1167     Error(std::move(Err));
1168     return true;
1169   }
1170 
1171   RecordData Record;
1172   StringRef Blob;
1173   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1174   if (!MaybeCode) {
1175     Error(MaybeCode.takeError());
1176     return true;
1177   }
1178   unsigned Code = MaybeCode.get();
1179 
1180   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1181   if (!MaybeRecCode) {
1182     Error(MaybeRecCode.takeError());
1183     return true;
1184   }
1185   unsigned RecCode = MaybeRecCode.get();
1186   if (RecCode != DECL_CONTEXT_LEXICAL) {
1187     Error("Expected lexical block");
1188     return true;
1189   }
1190 
1191   assert(!isa<TranslationUnitDecl>(DC) &&
1192          "expected a TU_UPDATE_LEXICAL record for TU");
1193   // If we are handling a C++ class template instantiation, we can see multiple
1194   // lexical updates for the same record. It's important that we select only one
1195   // of them, so that field numbering works properly. Just pick the first one we
1196   // see.
1197   auto &Lex = LexicalDecls[DC];
1198   if (!Lex.first) {
1199     Lex = std::make_pair(
1200         &M, llvm::makeArrayRef(
1201                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1202                     Blob.data()),
1203                 Blob.size() / 4));
1204   }
1205   DC->setHasExternalLexicalStorage(true);
1206   return false;
1207 }
1208 
1209 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1210                                               BitstreamCursor &Cursor,
1211                                               uint64_t Offset,
1212                                               DeclID ID) {
1213   assert(Offset != 0);
1214 
1215   SavedStreamPosition SavedPosition(Cursor);
1216   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1217     Error(std::move(Err));
1218     return true;
1219   }
1220 
1221   RecordData Record;
1222   StringRef Blob;
1223   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1224   if (!MaybeCode) {
1225     Error(MaybeCode.takeError());
1226     return true;
1227   }
1228   unsigned Code = MaybeCode.get();
1229 
1230   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1231   if (!MaybeRecCode) {
1232     Error(MaybeRecCode.takeError());
1233     return true;
1234   }
1235   unsigned RecCode = MaybeRecCode.get();
1236   if (RecCode != DECL_CONTEXT_VISIBLE) {
1237     Error("Expected visible lookup table block");
1238     return true;
1239   }
1240 
1241   // We can't safely determine the primary context yet, so delay attaching the
1242   // lookup table until we're done with recursive deserialization.
1243   auto *Data = (const unsigned char*)Blob.data();
1244   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1245   return false;
1246 }
1247 
1248 void ASTReader::Error(StringRef Msg) const {
1249   Error(diag::err_fe_pch_malformed, Msg);
1250   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1251       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1252     Diag(diag::note_module_cache_path)
1253       << PP.getHeaderSearchInfo().getModuleCachePath();
1254   }
1255 }
1256 
1257 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1258                       StringRef Arg3) const {
1259   if (Diags.isDiagnosticInFlight())
1260     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1261   else
1262     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1263 }
1264 
1265 void ASTReader::Error(llvm::Error &&Err) const {
1266   Error(toString(std::move(Err)));
1267 }
1268 
1269 //===----------------------------------------------------------------------===//
1270 // Source Manager Deserialization
1271 //===----------------------------------------------------------------------===//
1272 
1273 /// Read the line table in the source manager block.
1274 /// \returns true if there was an error.
1275 bool ASTReader::ParseLineTable(ModuleFile &F,
1276                                const RecordData &Record) {
1277   unsigned Idx = 0;
1278   LineTableInfo &LineTable = SourceMgr.getLineTable();
1279 
1280   // Parse the file names
1281   std::map<int, int> FileIDs;
1282   FileIDs[-1] = -1; // For unspecified filenames.
1283   for (unsigned I = 0; Record[Idx]; ++I) {
1284     // Extract the file name
1285     auto Filename = ReadPath(F, Record, Idx);
1286     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1287   }
1288   ++Idx;
1289 
1290   // Parse the line entries
1291   std::vector<LineEntry> Entries;
1292   while (Idx < Record.size()) {
1293     int FID = Record[Idx++];
1294     assert(FID >= 0 && "Serialized line entries for non-local file.");
1295     // Remap FileID from 1-based old view.
1296     FID += F.SLocEntryBaseID - 1;
1297 
1298     // Extract the line entries
1299     unsigned NumEntries = Record[Idx++];
1300     assert(NumEntries && "no line entries for file ID");
1301     Entries.clear();
1302     Entries.reserve(NumEntries);
1303     for (unsigned I = 0; I != NumEntries; ++I) {
1304       unsigned FileOffset = Record[Idx++];
1305       unsigned LineNo = Record[Idx++];
1306       int FilenameID = FileIDs[Record[Idx++]];
1307       SrcMgr::CharacteristicKind FileKind
1308         = (SrcMgr::CharacteristicKind)Record[Idx++];
1309       unsigned IncludeOffset = Record[Idx++];
1310       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1311                                        FileKind, IncludeOffset));
1312     }
1313     LineTable.AddEntry(FileID::get(FID), Entries);
1314   }
1315 
1316   return false;
1317 }
1318 
1319 /// Read a source manager block
1320 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1321   using namespace SrcMgr;
1322 
1323   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1324 
1325   // Set the source-location entry cursor to the current position in
1326   // the stream. This cursor will be used to read the contents of the
1327   // source manager block initially, and then lazily read
1328   // source-location entries as needed.
1329   SLocEntryCursor = F.Stream;
1330 
1331   // The stream itself is going to skip over the source manager block.
1332   if (llvm::Error Err = F.Stream.SkipBlock()) {
1333     Error(std::move(Err));
1334     return true;
1335   }
1336 
1337   // Enter the source manager block.
1338   if (llvm::Error Err =
1339           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1340     Error(std::move(Err));
1341     return true;
1342   }
1343   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1344 
1345   RecordData Record;
1346   while (true) {
1347     Expected<llvm::BitstreamEntry> MaybeE =
1348         SLocEntryCursor.advanceSkippingSubblocks();
1349     if (!MaybeE) {
1350       Error(MaybeE.takeError());
1351       return true;
1352     }
1353     llvm::BitstreamEntry E = MaybeE.get();
1354 
1355     switch (E.Kind) {
1356     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1357     case llvm::BitstreamEntry::Error:
1358       Error("malformed block record in AST file");
1359       return true;
1360     case llvm::BitstreamEntry::EndBlock:
1361       return false;
1362     case llvm::BitstreamEntry::Record:
1363       // The interesting case.
1364       break;
1365     }
1366 
1367     // Read a record.
1368     Record.clear();
1369     StringRef Blob;
1370     Expected<unsigned> MaybeRecord =
1371         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1372     if (!MaybeRecord) {
1373       Error(MaybeRecord.takeError());
1374       return true;
1375     }
1376     switch (MaybeRecord.get()) {
1377     default:  // Default behavior: ignore.
1378       break;
1379 
1380     case SM_SLOC_FILE_ENTRY:
1381     case SM_SLOC_BUFFER_ENTRY:
1382     case SM_SLOC_EXPANSION_ENTRY:
1383       // Once we hit one of the source location entries, we're done.
1384       return false;
1385     }
1386   }
1387 }
1388 
1389 /// If a header file is not found at the path that we expect it to be
1390 /// and the PCH file was moved from its original location, try to resolve the
1391 /// file by assuming that header+PCH were moved together and the header is in
1392 /// the same place relative to the PCH.
1393 static std::string
1394 resolveFileRelativeToOriginalDir(const std::string &Filename,
1395                                  const std::string &OriginalDir,
1396                                  const std::string &CurrDir) {
1397   assert(OriginalDir != CurrDir &&
1398          "No point trying to resolve the file if the PCH dir didn't change");
1399 
1400   using namespace llvm::sys;
1401 
1402   SmallString<128> filePath(Filename);
1403   fs::make_absolute(filePath);
1404   assert(path::is_absolute(OriginalDir));
1405   SmallString<128> currPCHPath(CurrDir);
1406 
1407   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1408                        fileDirE = path::end(path::parent_path(filePath));
1409   path::const_iterator origDirI = path::begin(OriginalDir),
1410                        origDirE = path::end(OriginalDir);
1411   // Skip the common path components from filePath and OriginalDir.
1412   while (fileDirI != fileDirE && origDirI != origDirE &&
1413          *fileDirI == *origDirI) {
1414     ++fileDirI;
1415     ++origDirI;
1416   }
1417   for (; origDirI != origDirE; ++origDirI)
1418     path::append(currPCHPath, "..");
1419   path::append(currPCHPath, fileDirI, fileDirE);
1420   path::append(currPCHPath, path::filename(Filename));
1421   return std::string(currPCHPath.str());
1422 }
1423 
1424 bool ASTReader::ReadSLocEntry(int ID) {
1425   if (ID == 0)
1426     return false;
1427 
1428   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1429     Error("source location entry ID out-of-range for AST file");
1430     return true;
1431   }
1432 
1433   // Local helper to read the (possibly-compressed) buffer data following the
1434   // entry record.
1435   auto ReadBuffer = [this](
1436       BitstreamCursor &SLocEntryCursor,
1437       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1438     RecordData Record;
1439     StringRef Blob;
1440     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1441     if (!MaybeCode) {
1442       Error(MaybeCode.takeError());
1443       return nullptr;
1444     }
1445     unsigned Code = MaybeCode.get();
1446 
1447     Expected<unsigned> MaybeRecCode =
1448         SLocEntryCursor.readRecord(Code, Record, &Blob);
1449     if (!MaybeRecCode) {
1450       Error(MaybeRecCode.takeError());
1451       return nullptr;
1452     }
1453     unsigned RecCode = MaybeRecCode.get();
1454 
1455     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1456       if (!llvm::zlib::isAvailable()) {
1457         Error("zlib is not available");
1458         return nullptr;
1459       }
1460       SmallString<0> Uncompressed;
1461       if (llvm::Error E =
1462               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1463         Error("could not decompress embedded file contents: " +
1464               llvm::toString(std::move(E)));
1465         return nullptr;
1466       }
1467       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1468     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1469       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1470     } else {
1471       Error("AST record has invalid code");
1472       return nullptr;
1473     }
1474   };
1475 
1476   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1477   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1478           F->SLocEntryOffsetsBase +
1479           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1480     Error(std::move(Err));
1481     return true;
1482   }
1483 
1484   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1485   unsigned BaseOffset = F->SLocEntryBaseOffset;
1486 
1487   ++NumSLocEntriesRead;
1488   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1489   if (!MaybeEntry) {
1490     Error(MaybeEntry.takeError());
1491     return true;
1492   }
1493   llvm::BitstreamEntry Entry = MaybeEntry.get();
1494 
1495   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1496     Error("incorrectly-formatted source location entry in AST file");
1497     return true;
1498   }
1499 
1500   RecordData Record;
1501   StringRef Blob;
1502   Expected<unsigned> MaybeSLOC =
1503       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1504   if (!MaybeSLOC) {
1505     Error(MaybeSLOC.takeError());
1506     return true;
1507   }
1508   switch (MaybeSLOC.get()) {
1509   default:
1510     Error("incorrectly-formatted source location entry in AST file");
1511     return true;
1512 
1513   case SM_SLOC_FILE_ENTRY: {
1514     // We will detect whether a file changed and return 'Failure' for it, but
1515     // we will also try to fail gracefully by setting up the SLocEntry.
1516     unsigned InputID = Record[4];
1517     InputFile IF = getInputFile(*F, InputID);
1518     Optional<FileEntryRef> File = IF.getFile();
1519     bool OverriddenBuffer = IF.isOverridden();
1520 
1521     // Note that we only check if a File was returned. If it was out-of-date
1522     // we have complained but we will continue creating a FileID to recover
1523     // gracefully.
1524     if (!File)
1525       return true;
1526 
1527     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1528     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1529       // This is the module's main file.
1530       IncludeLoc = getImportLocation(F);
1531     }
1532     SrcMgr::CharacteristicKind
1533       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1534     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1535                                         BaseOffset + Record[0]);
1536     SrcMgr::FileInfo &FileInfo =
1537           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1538     FileInfo.NumCreatedFIDs = Record[5];
1539     if (Record[3])
1540       FileInfo.setHasLineDirectives();
1541 
1542     unsigned NumFileDecls = Record[7];
1543     if (NumFileDecls && ContextObj) {
1544       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1545       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1546       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1547                                                              NumFileDecls));
1548     }
1549 
1550     const SrcMgr::ContentCache &ContentCache =
1551         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1552     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1553         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1554         !ContentCache.getBufferIfLoaded()) {
1555       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1556       if (!Buffer)
1557         return true;
1558       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1559     }
1560 
1561     break;
1562   }
1563 
1564   case SM_SLOC_BUFFER_ENTRY: {
1565     const char *Name = Blob.data();
1566     unsigned Offset = Record[0];
1567     SrcMgr::CharacteristicKind
1568       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1569     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1570     if (IncludeLoc.isInvalid() && F->isModule()) {
1571       IncludeLoc = getImportLocation(F);
1572     }
1573 
1574     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1575     if (!Buffer)
1576       return true;
1577     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1578                            BaseOffset + Offset, IncludeLoc);
1579     break;
1580   }
1581 
1582   case SM_SLOC_EXPANSION_ENTRY: {
1583     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1584     SourceMgr.createExpansionLoc(SpellingLoc,
1585                                      ReadSourceLocation(*F, Record[2]),
1586                                      ReadSourceLocation(*F, Record[3]),
1587                                      Record[5],
1588                                      Record[4],
1589                                      ID,
1590                                      BaseOffset + Record[0]);
1591     break;
1592   }
1593   }
1594 
1595   return false;
1596 }
1597 
1598 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1599   if (ID == 0)
1600     return std::make_pair(SourceLocation(), "");
1601 
1602   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1603     Error("source location entry ID out-of-range for AST file");
1604     return std::make_pair(SourceLocation(), "");
1605   }
1606 
1607   // Find which module file this entry lands in.
1608   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1609   if (!M->isModule())
1610     return std::make_pair(SourceLocation(), "");
1611 
1612   // FIXME: Can we map this down to a particular submodule? That would be
1613   // ideal.
1614   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1615 }
1616 
1617 /// Find the location where the module F is imported.
1618 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1619   if (F->ImportLoc.isValid())
1620     return F->ImportLoc;
1621 
1622   // Otherwise we have a PCH. It's considered to be "imported" at the first
1623   // location of its includer.
1624   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1625     // Main file is the importer.
1626     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1627     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1628   }
1629   return F->ImportedBy[0]->FirstLoc;
1630 }
1631 
1632 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1633 /// the abbreviations that are at the top of the block and then leave the cursor
1634 /// pointing into the block.
1635 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1636                                  uint64_t *StartOfBlockOffset) {
1637   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1638     // FIXME this drops errors on the floor.
1639     consumeError(std::move(Err));
1640     return true;
1641   }
1642 
1643   if (StartOfBlockOffset)
1644     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1645 
1646   while (true) {
1647     uint64_t Offset = Cursor.GetCurrentBitNo();
1648     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1649     if (!MaybeCode) {
1650       // FIXME this drops errors on the floor.
1651       consumeError(MaybeCode.takeError());
1652       return true;
1653     }
1654     unsigned Code = MaybeCode.get();
1655 
1656     // We expect all abbrevs to be at the start of the block.
1657     if (Code != llvm::bitc::DEFINE_ABBREV) {
1658       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1659         // FIXME this drops errors on the floor.
1660         consumeError(std::move(Err));
1661         return true;
1662       }
1663       return false;
1664     }
1665     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1666       // FIXME this drops errors on the floor.
1667       consumeError(std::move(Err));
1668       return true;
1669     }
1670   }
1671 }
1672 
1673 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1674                            unsigned &Idx) {
1675   Token Tok;
1676   Tok.startToken();
1677   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1678   Tok.setLength(Record[Idx++]);
1679   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1680     Tok.setIdentifierInfo(II);
1681   Tok.setKind((tok::TokenKind)Record[Idx++]);
1682   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1683   return Tok;
1684 }
1685 
1686 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1687   BitstreamCursor &Stream = F.MacroCursor;
1688 
1689   // Keep track of where we are in the stream, then jump back there
1690   // after reading this macro.
1691   SavedStreamPosition SavedPosition(Stream);
1692 
1693   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1694     // FIXME this drops errors on the floor.
1695     consumeError(std::move(Err));
1696     return nullptr;
1697   }
1698   RecordData Record;
1699   SmallVector<IdentifierInfo*, 16> MacroParams;
1700   MacroInfo *Macro = nullptr;
1701 
1702   while (true) {
1703     // Advance to the next record, but if we get to the end of the block, don't
1704     // pop it (removing all the abbreviations from the cursor) since we want to
1705     // be able to reseek within the block and read entries.
1706     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1707     Expected<llvm::BitstreamEntry> MaybeEntry =
1708         Stream.advanceSkippingSubblocks(Flags);
1709     if (!MaybeEntry) {
1710       Error(MaybeEntry.takeError());
1711       return Macro;
1712     }
1713     llvm::BitstreamEntry Entry = MaybeEntry.get();
1714 
1715     switch (Entry.Kind) {
1716     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1717     case llvm::BitstreamEntry::Error:
1718       Error("malformed block record in AST file");
1719       return Macro;
1720     case llvm::BitstreamEntry::EndBlock:
1721       return Macro;
1722     case llvm::BitstreamEntry::Record:
1723       // The interesting case.
1724       break;
1725     }
1726 
1727     // Read a record.
1728     Record.clear();
1729     PreprocessorRecordTypes RecType;
1730     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1731       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1732     else {
1733       Error(MaybeRecType.takeError());
1734       return Macro;
1735     }
1736     switch (RecType) {
1737     case PP_MODULE_MACRO:
1738     case PP_MACRO_DIRECTIVE_HISTORY:
1739       return Macro;
1740 
1741     case PP_MACRO_OBJECT_LIKE:
1742     case PP_MACRO_FUNCTION_LIKE: {
1743       // If we already have a macro, that means that we've hit the end
1744       // of the definition of the macro we were looking for. We're
1745       // done.
1746       if (Macro)
1747         return Macro;
1748 
1749       unsigned NextIndex = 1; // Skip identifier ID.
1750       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1751       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1752       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1753       MI->setIsUsed(Record[NextIndex++]);
1754       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1755 
1756       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1757         // Decode function-like macro info.
1758         bool isC99VarArgs = Record[NextIndex++];
1759         bool isGNUVarArgs = Record[NextIndex++];
1760         bool hasCommaPasting = Record[NextIndex++];
1761         MacroParams.clear();
1762         unsigned NumArgs = Record[NextIndex++];
1763         for (unsigned i = 0; i != NumArgs; ++i)
1764           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1765 
1766         // Install function-like macro info.
1767         MI->setIsFunctionLike();
1768         if (isC99VarArgs) MI->setIsC99Varargs();
1769         if (isGNUVarArgs) MI->setIsGNUVarargs();
1770         if (hasCommaPasting) MI->setHasCommaPasting();
1771         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1772       }
1773 
1774       // Remember that we saw this macro last so that we add the tokens that
1775       // form its body to it.
1776       Macro = MI;
1777 
1778       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1779           Record[NextIndex]) {
1780         // We have a macro definition. Register the association
1781         PreprocessedEntityID
1782             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1783         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1784         PreprocessingRecord::PPEntityID PPID =
1785             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1786         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1787             PPRec.getPreprocessedEntity(PPID));
1788         if (PPDef)
1789           PPRec.RegisterMacroDefinition(Macro, PPDef);
1790       }
1791 
1792       ++NumMacrosRead;
1793       break;
1794     }
1795 
1796     case PP_TOKEN: {
1797       // If we see a TOKEN before a PP_MACRO_*, then the file is
1798       // erroneous, just pretend we didn't see this.
1799       if (!Macro) break;
1800 
1801       unsigned Idx = 0;
1802       Token Tok = ReadToken(F, Record, Idx);
1803       Macro->AddTokenToBody(Tok);
1804       break;
1805     }
1806     }
1807   }
1808 }
1809 
1810 PreprocessedEntityID
1811 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1812                                          unsigned LocalID) const {
1813   if (!M.ModuleOffsetMap.empty())
1814     ReadModuleOffsetMap(M);
1815 
1816   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1817     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1818   assert(I != M.PreprocessedEntityRemap.end()
1819          && "Invalid index into preprocessed entity index remap");
1820 
1821   return LocalID + I->second;
1822 }
1823 
1824 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1825   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1826 }
1827 
1828 HeaderFileInfoTrait::internal_key_type
1829 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1830   internal_key_type ikey = {FE->getSize(),
1831                             M.HasTimestamps ? FE->getModificationTime() : 0,
1832                             FE->getName(), /*Imported*/ false};
1833   return ikey;
1834 }
1835 
1836 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1837   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1838     return false;
1839 
1840   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1841     return true;
1842 
1843   // Determine whether the actual files are equivalent.
1844   FileManager &FileMgr = Reader.getFileManager();
1845   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1846     if (!Key.Imported) {
1847       if (auto File = FileMgr.getFile(Key.Filename))
1848         return *File;
1849       return nullptr;
1850     }
1851 
1852     std::string Resolved = std::string(Key.Filename);
1853     Reader.ResolveImportedPath(M, Resolved);
1854     if (auto File = FileMgr.getFile(Resolved))
1855       return *File;
1856     return nullptr;
1857   };
1858 
1859   const FileEntry *FEA = GetFile(a);
1860   const FileEntry *FEB = GetFile(b);
1861   return FEA && FEA == FEB;
1862 }
1863 
1864 std::pair<unsigned, unsigned>
1865 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1866   return readULEBKeyDataLength(d);
1867 }
1868 
1869 HeaderFileInfoTrait::internal_key_type
1870 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1871   using namespace llvm::support;
1872 
1873   internal_key_type ikey;
1874   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1875   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1876   ikey.Filename = (const char *)d;
1877   ikey.Imported = true;
1878   return ikey;
1879 }
1880 
1881 HeaderFileInfoTrait::data_type
1882 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1883                               unsigned DataLen) {
1884   using namespace llvm::support;
1885 
1886   const unsigned char *End = d + DataLen;
1887   HeaderFileInfo HFI;
1888   unsigned Flags = *d++;
1889   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1890   HFI.isImport |= (Flags >> 5) & 0x01;
1891   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1892   HFI.DirInfo = (Flags >> 1) & 0x07;
1893   HFI.IndexHeaderMapHeader = Flags & 0x01;
1894   // FIXME: Find a better way to handle this. Maybe just store a
1895   // "has been included" flag?
1896   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1897                              HFI.NumIncludes);
1898   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1899       M, endian::readNext<uint32_t, little, unaligned>(d));
1900   if (unsigned FrameworkOffset =
1901           endian::readNext<uint32_t, little, unaligned>(d)) {
1902     // The framework offset is 1 greater than the actual offset,
1903     // since 0 is used as an indicator for "no framework name".
1904     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1905     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1906   }
1907 
1908   assert((End - d) % 4 == 0 &&
1909          "Wrong data length in HeaderFileInfo deserialization");
1910   while (d != End) {
1911     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1912     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1913     LocalSMID >>= 2;
1914 
1915     // This header is part of a module. Associate it with the module to enable
1916     // implicit module import.
1917     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1918     Module *Mod = Reader.getSubmodule(GlobalSMID);
1919     FileManager &FileMgr = Reader.getFileManager();
1920     ModuleMap &ModMap =
1921         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1922 
1923     std::string Filename = std::string(key.Filename);
1924     if (key.Imported)
1925       Reader.ResolveImportedPath(M, Filename);
1926     // FIXME: NameAsWritten
1927     Module::Header H = {std::string(key.Filename), "",
1928                         *FileMgr.getFile(Filename)};
1929     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1930     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1931   }
1932 
1933   // This HeaderFileInfo was externally loaded.
1934   HFI.External = true;
1935   HFI.IsValid = true;
1936   return HFI;
1937 }
1938 
1939 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1940                                 uint32_t MacroDirectivesOffset) {
1941   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1942   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1943 }
1944 
1945 void ASTReader::ReadDefinedMacros() {
1946   // Note that we are loading defined macros.
1947   Deserializing Macros(this);
1948 
1949   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1950     BitstreamCursor &MacroCursor = I.MacroCursor;
1951 
1952     // If there was no preprocessor block, skip this file.
1953     if (MacroCursor.getBitcodeBytes().empty())
1954       continue;
1955 
1956     BitstreamCursor Cursor = MacroCursor;
1957     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1958       Error(std::move(Err));
1959       return;
1960     }
1961 
1962     RecordData Record;
1963     while (true) {
1964       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1965       if (!MaybeE) {
1966         Error(MaybeE.takeError());
1967         return;
1968       }
1969       llvm::BitstreamEntry E = MaybeE.get();
1970 
1971       switch (E.Kind) {
1972       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1973       case llvm::BitstreamEntry::Error:
1974         Error("malformed block record in AST file");
1975         return;
1976       case llvm::BitstreamEntry::EndBlock:
1977         goto NextCursor;
1978 
1979       case llvm::BitstreamEntry::Record: {
1980         Record.clear();
1981         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1982         if (!MaybeRecord) {
1983           Error(MaybeRecord.takeError());
1984           return;
1985         }
1986         switch (MaybeRecord.get()) {
1987         default:  // Default behavior: ignore.
1988           break;
1989 
1990         case PP_MACRO_OBJECT_LIKE:
1991         case PP_MACRO_FUNCTION_LIKE: {
1992           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1993           if (II->isOutOfDate())
1994             updateOutOfDateIdentifier(*II);
1995           break;
1996         }
1997 
1998         case PP_TOKEN:
1999           // Ignore tokens.
2000           break;
2001         }
2002         break;
2003       }
2004       }
2005     }
2006     NextCursor:  ;
2007   }
2008 }
2009 
2010 namespace {
2011 
2012   /// Visitor class used to look up identifirs in an AST file.
2013   class IdentifierLookupVisitor {
2014     StringRef Name;
2015     unsigned NameHash;
2016     unsigned PriorGeneration;
2017     unsigned &NumIdentifierLookups;
2018     unsigned &NumIdentifierLookupHits;
2019     IdentifierInfo *Found = nullptr;
2020 
2021   public:
2022     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2023                             unsigned &NumIdentifierLookups,
2024                             unsigned &NumIdentifierLookupHits)
2025       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2026         PriorGeneration(PriorGeneration),
2027         NumIdentifierLookups(NumIdentifierLookups),
2028         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2029 
2030     bool operator()(ModuleFile &M) {
2031       // If we've already searched this module file, skip it now.
2032       if (M.Generation <= PriorGeneration)
2033         return true;
2034 
2035       ASTIdentifierLookupTable *IdTable
2036         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2037       if (!IdTable)
2038         return false;
2039 
2040       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2041                                      Found);
2042       ++NumIdentifierLookups;
2043       ASTIdentifierLookupTable::iterator Pos =
2044           IdTable->find_hashed(Name, NameHash, &Trait);
2045       if (Pos == IdTable->end())
2046         return false;
2047 
2048       // Dereferencing the iterator has the effect of building the
2049       // IdentifierInfo node and populating it with the various
2050       // declarations it needs.
2051       ++NumIdentifierLookupHits;
2052       Found = *Pos;
2053       return true;
2054     }
2055 
2056     // Retrieve the identifier info found within the module
2057     // files.
2058     IdentifierInfo *getIdentifierInfo() const { return Found; }
2059   };
2060 
2061 } // namespace
2062 
2063 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2064   // Note that we are loading an identifier.
2065   Deserializing AnIdentifier(this);
2066 
2067   unsigned PriorGeneration = 0;
2068   if (getContext().getLangOpts().Modules)
2069     PriorGeneration = IdentifierGeneration[&II];
2070 
2071   // If there is a global index, look there first to determine which modules
2072   // provably do not have any results for this identifier.
2073   GlobalModuleIndex::HitSet Hits;
2074   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2075   if (!loadGlobalIndex()) {
2076     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2077       HitsPtr = &Hits;
2078     }
2079   }
2080 
2081   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2082                                   NumIdentifierLookups,
2083                                   NumIdentifierLookupHits);
2084   ModuleMgr.visit(Visitor, HitsPtr);
2085   markIdentifierUpToDate(&II);
2086 }
2087 
2088 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2089   if (!II)
2090     return;
2091 
2092   II->setOutOfDate(false);
2093 
2094   // Update the generation for this identifier.
2095   if (getContext().getLangOpts().Modules)
2096     IdentifierGeneration[II] = getGeneration();
2097 }
2098 
2099 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2100                                     const PendingMacroInfo &PMInfo) {
2101   ModuleFile &M = *PMInfo.M;
2102 
2103   BitstreamCursor &Cursor = M.MacroCursor;
2104   SavedStreamPosition SavedPosition(Cursor);
2105   if (llvm::Error Err =
2106           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2107     Error(std::move(Err));
2108     return;
2109   }
2110 
2111   struct ModuleMacroRecord {
2112     SubmoduleID SubModID;
2113     MacroInfo *MI;
2114     SmallVector<SubmoduleID, 8> Overrides;
2115   };
2116   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2117 
2118   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2119   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2120   // macro histroy.
2121   RecordData Record;
2122   while (true) {
2123     Expected<llvm::BitstreamEntry> MaybeEntry =
2124         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2125     if (!MaybeEntry) {
2126       Error(MaybeEntry.takeError());
2127       return;
2128     }
2129     llvm::BitstreamEntry Entry = MaybeEntry.get();
2130 
2131     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2132       Error("malformed block record in AST file");
2133       return;
2134     }
2135 
2136     Record.clear();
2137     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2138     if (!MaybePP) {
2139       Error(MaybePP.takeError());
2140       return;
2141     }
2142     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2143     case PP_MACRO_DIRECTIVE_HISTORY:
2144       break;
2145 
2146     case PP_MODULE_MACRO: {
2147       ModuleMacros.push_back(ModuleMacroRecord());
2148       auto &Info = ModuleMacros.back();
2149       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2150       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2151       for (int I = 2, N = Record.size(); I != N; ++I)
2152         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2153       continue;
2154     }
2155 
2156     default:
2157       Error("malformed block record in AST file");
2158       return;
2159     }
2160 
2161     // We found the macro directive history; that's the last record
2162     // for this macro.
2163     break;
2164   }
2165 
2166   // Module macros are listed in reverse dependency order.
2167   {
2168     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2169     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2170     for (auto &MMR : ModuleMacros) {
2171       Overrides.clear();
2172       for (unsigned ModID : MMR.Overrides) {
2173         Module *Mod = getSubmodule(ModID);
2174         auto *Macro = PP.getModuleMacro(Mod, II);
2175         assert(Macro && "missing definition for overridden macro");
2176         Overrides.push_back(Macro);
2177       }
2178 
2179       bool Inserted = false;
2180       Module *Owner = getSubmodule(MMR.SubModID);
2181       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2182     }
2183   }
2184 
2185   // Don't read the directive history for a module; we don't have anywhere
2186   // to put it.
2187   if (M.isModule())
2188     return;
2189 
2190   // Deserialize the macro directives history in reverse source-order.
2191   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2192   unsigned Idx = 0, N = Record.size();
2193   while (Idx < N) {
2194     MacroDirective *MD = nullptr;
2195     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2196     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2197     switch (K) {
2198     case MacroDirective::MD_Define: {
2199       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2200       MD = PP.AllocateDefMacroDirective(MI, Loc);
2201       break;
2202     }
2203     case MacroDirective::MD_Undefine:
2204       MD = PP.AllocateUndefMacroDirective(Loc);
2205       break;
2206     case MacroDirective::MD_Visibility:
2207       bool isPublic = Record[Idx++];
2208       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2209       break;
2210     }
2211 
2212     if (!Latest)
2213       Latest = MD;
2214     if (Earliest)
2215       Earliest->setPrevious(MD);
2216     Earliest = MD;
2217   }
2218 
2219   if (Latest)
2220     PP.setLoadedMacroDirective(II, Earliest, Latest);
2221 }
2222 
2223 bool ASTReader::shouldDisableValidationForFile(
2224     const serialization::ModuleFile &M) const {
2225   if (DisableValidationKind == DisableValidationForModuleKind::None)
2226     return false;
2227 
2228   // If a PCH is loaded and validation is disabled for PCH then disable
2229   // validation for the PCH and the modules it loads.
2230   ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2231 
2232   switch (K) {
2233   case MK_MainFile:
2234   case MK_Preamble:
2235   case MK_PCH:
2236     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2237   case MK_ImplicitModule:
2238   case MK_ExplicitModule:
2239   case MK_PrebuiltModule:
2240     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2241   }
2242 
2243   return false;
2244 }
2245 
2246 ASTReader::InputFileInfo
2247 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2248   // Go find this input file.
2249   BitstreamCursor &Cursor = F.InputFilesCursor;
2250   SavedStreamPosition SavedPosition(Cursor);
2251   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2252     // FIXME this drops errors on the floor.
2253     consumeError(std::move(Err));
2254   }
2255 
2256   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2257   if (!MaybeCode) {
2258     // FIXME this drops errors on the floor.
2259     consumeError(MaybeCode.takeError());
2260   }
2261   unsigned Code = MaybeCode.get();
2262   RecordData Record;
2263   StringRef Blob;
2264 
2265   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2266     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2267            "invalid record type for input file");
2268   else {
2269     // FIXME this drops errors on the floor.
2270     consumeError(Maybe.takeError());
2271   }
2272 
2273   assert(Record[0] == ID && "Bogus stored ID or offset");
2274   InputFileInfo R;
2275   R.StoredSize = static_cast<off_t>(Record[1]);
2276   R.StoredTime = static_cast<time_t>(Record[2]);
2277   R.Overridden = static_cast<bool>(Record[3]);
2278   R.Transient = static_cast<bool>(Record[4]);
2279   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2280   R.Filename = std::string(Blob);
2281   ResolveImportedPath(F, R.Filename);
2282 
2283   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2284   if (!MaybeEntry) // FIXME this drops errors on the floor.
2285     consumeError(MaybeEntry.takeError());
2286   llvm::BitstreamEntry Entry = MaybeEntry.get();
2287   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2288          "expected record type for input file hash");
2289 
2290   Record.clear();
2291   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2292     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2293            "invalid record type for input file hash");
2294   else {
2295     // FIXME this drops errors on the floor.
2296     consumeError(Maybe.takeError());
2297   }
2298   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2299                   static_cast<uint64_t>(Record[0]);
2300   return R;
2301 }
2302 
2303 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2304 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2305   // If this ID is bogus, just return an empty input file.
2306   if (ID == 0 || ID > F.InputFilesLoaded.size())
2307     return InputFile();
2308 
2309   // If we've already loaded this input file, return it.
2310   if (F.InputFilesLoaded[ID-1].getFile())
2311     return F.InputFilesLoaded[ID-1];
2312 
2313   if (F.InputFilesLoaded[ID-1].isNotFound())
2314     return InputFile();
2315 
2316   // Go find this input file.
2317   BitstreamCursor &Cursor = F.InputFilesCursor;
2318   SavedStreamPosition SavedPosition(Cursor);
2319   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2320     // FIXME this drops errors on the floor.
2321     consumeError(std::move(Err));
2322   }
2323 
2324   InputFileInfo FI = readInputFileInfo(F, ID);
2325   off_t StoredSize = FI.StoredSize;
2326   time_t StoredTime = FI.StoredTime;
2327   bool Overridden = FI.Overridden;
2328   bool Transient = FI.Transient;
2329   StringRef Filename = FI.Filename;
2330   uint64_t StoredContentHash = FI.ContentHash;
2331 
2332   OptionalFileEntryRefDegradesToFileEntryPtr File =
2333       expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2334 
2335   // If we didn't find the file, resolve it relative to the
2336   // original directory from which this AST file was created.
2337   if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2338       F.OriginalDir != F.BaseDirectory) {
2339     std::string Resolved = resolveFileRelativeToOriginalDir(
2340         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2341     if (!Resolved.empty())
2342       File = expectedToOptional(FileMgr.getFileRef(Resolved));
2343   }
2344 
2345   // For an overridden file, create a virtual file with the stored
2346   // size/timestamp.
2347   if ((Overridden || Transient) && !File)
2348     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2349 
2350   if (!File) {
2351     if (Complain) {
2352       std::string ErrorStr = "could not find file '";
2353       ErrorStr += Filename;
2354       ErrorStr += "' referenced by AST file '";
2355       ErrorStr += F.FileName;
2356       ErrorStr += "'";
2357       Error(ErrorStr);
2358     }
2359     // Record that we didn't find the file.
2360     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2361     return InputFile();
2362   }
2363 
2364   // Check if there was a request to override the contents of the file
2365   // that was part of the precompiled header. Overriding such a file
2366   // can lead to problems when lexing using the source locations from the
2367   // PCH.
2368   SourceManager &SM = getSourceManager();
2369   // FIXME: Reject if the overrides are different.
2370   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2371     if (Complain)
2372       Error(diag::err_fe_pch_file_overridden, Filename);
2373 
2374     // After emitting the diagnostic, bypass the overriding file to recover
2375     // (this creates a separate FileEntry).
2376     File = SM.bypassFileContentsOverride(*File);
2377     if (!File) {
2378       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2379       return InputFile();
2380     }
2381   }
2382 
2383   enum ModificationType {
2384     Size,
2385     ModTime,
2386     Content,
2387     None,
2388   };
2389   auto HasInputFileChanged = [&]() {
2390     if (StoredSize != File->getSize())
2391       return ModificationType::Size;
2392     if (!shouldDisableValidationForFile(F) && StoredTime &&
2393         StoredTime != File->getModificationTime()) {
2394       // In case the modification time changes but not the content,
2395       // accept the cached file as legit.
2396       if (ValidateASTInputFilesContent &&
2397           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2398         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2399         if (!MemBuffOrError) {
2400           if (!Complain)
2401             return ModificationType::ModTime;
2402           std::string ErrorStr = "could not get buffer for file '";
2403           ErrorStr += File->getName();
2404           ErrorStr += "'";
2405           Error(ErrorStr);
2406           return ModificationType::ModTime;
2407         }
2408 
2409         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2410         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2411           return ModificationType::None;
2412         return ModificationType::Content;
2413       }
2414       return ModificationType::ModTime;
2415     }
2416     return ModificationType::None;
2417   };
2418 
2419   bool IsOutOfDate = false;
2420   auto FileChange = HasInputFileChanged();
2421   // For an overridden file, there is nothing to validate.
2422   if (!Overridden && FileChange != ModificationType::None) {
2423     if (Complain && !Diags.isDiagnosticInFlight()) {
2424       // Build a list of the PCH imports that got us here (in reverse).
2425       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2426       while (!ImportStack.back()->ImportedBy.empty())
2427         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2428 
2429       // The top-level PCH is stale.
2430       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2431       Diag(diag::err_fe_ast_file_modified)
2432           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2433           << TopLevelPCHName << FileChange;
2434 
2435       // Print the import stack.
2436       if (ImportStack.size() > 1) {
2437         Diag(diag::note_pch_required_by)
2438           << Filename << ImportStack[0]->FileName;
2439         for (unsigned I = 1; I < ImportStack.size(); ++I)
2440           Diag(diag::note_pch_required_by)
2441             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2442       }
2443 
2444       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2445     }
2446 
2447     IsOutOfDate = true;
2448   }
2449   // FIXME: If the file is overridden and we've already opened it,
2450   // issue an error (or split it into a separate FileEntry).
2451 
2452   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2453 
2454   // Note that we've loaded this input file.
2455   F.InputFilesLoaded[ID-1] = IF;
2456   return IF;
2457 }
2458 
2459 /// If we are loading a relocatable PCH or module file, and the filename
2460 /// is not an absolute path, add the system or module root to the beginning of
2461 /// the file name.
2462 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2463   // Resolve relative to the base directory, if we have one.
2464   if (!M.BaseDirectory.empty())
2465     return ResolveImportedPath(Filename, M.BaseDirectory);
2466 }
2467 
2468 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2469   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2470     return;
2471 
2472   SmallString<128> Buffer;
2473   llvm::sys::path::append(Buffer, Prefix, Filename);
2474   Filename.assign(Buffer.begin(), Buffer.end());
2475 }
2476 
2477 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2478   switch (ARR) {
2479   case ASTReader::Failure: return true;
2480   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2481   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2482   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2483   case ASTReader::ConfigurationMismatch:
2484     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2485   case ASTReader::HadErrors: return true;
2486   case ASTReader::Success: return false;
2487   }
2488 
2489   llvm_unreachable("unknown ASTReadResult");
2490 }
2491 
2492 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2493     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2494     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2495     std::string &SuggestedPredefines) {
2496   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2497     // FIXME this drops errors on the floor.
2498     consumeError(std::move(Err));
2499     return Failure;
2500   }
2501 
2502   // Read all of the records in the options block.
2503   RecordData Record;
2504   ASTReadResult Result = Success;
2505   while (true) {
2506     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2507     if (!MaybeEntry) {
2508       // FIXME this drops errors on the floor.
2509       consumeError(MaybeEntry.takeError());
2510       return Failure;
2511     }
2512     llvm::BitstreamEntry Entry = MaybeEntry.get();
2513 
2514     switch (Entry.Kind) {
2515     case llvm::BitstreamEntry::Error:
2516     case llvm::BitstreamEntry::SubBlock:
2517       return Failure;
2518 
2519     case llvm::BitstreamEntry::EndBlock:
2520       return Result;
2521 
2522     case llvm::BitstreamEntry::Record:
2523       // The interesting case.
2524       break;
2525     }
2526 
2527     // Read and process a record.
2528     Record.clear();
2529     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2530     if (!MaybeRecordType) {
2531       // FIXME this drops errors on the floor.
2532       consumeError(MaybeRecordType.takeError());
2533       return Failure;
2534     }
2535     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2536     case LANGUAGE_OPTIONS: {
2537       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2538       if (ParseLanguageOptions(Record, Complain, Listener,
2539                                AllowCompatibleConfigurationMismatch))
2540         Result = ConfigurationMismatch;
2541       break;
2542     }
2543 
2544     case TARGET_OPTIONS: {
2545       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2546       if (ParseTargetOptions(Record, Complain, Listener,
2547                              AllowCompatibleConfigurationMismatch))
2548         Result = ConfigurationMismatch;
2549       break;
2550     }
2551 
2552     case FILE_SYSTEM_OPTIONS: {
2553       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2554       if (!AllowCompatibleConfigurationMismatch &&
2555           ParseFileSystemOptions(Record, Complain, Listener))
2556         Result = ConfigurationMismatch;
2557       break;
2558     }
2559 
2560     case HEADER_SEARCH_OPTIONS: {
2561       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2562       if (!AllowCompatibleConfigurationMismatch &&
2563           ParseHeaderSearchOptions(Record, Complain, Listener))
2564         Result = ConfigurationMismatch;
2565       break;
2566     }
2567 
2568     case PREPROCESSOR_OPTIONS:
2569       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2570       if (!AllowCompatibleConfigurationMismatch &&
2571           ParsePreprocessorOptions(Record, Complain, Listener,
2572                                    SuggestedPredefines))
2573         Result = ConfigurationMismatch;
2574       break;
2575     }
2576   }
2577 }
2578 
2579 ASTReader::ASTReadResult
2580 ASTReader::ReadControlBlock(ModuleFile &F,
2581                             SmallVectorImpl<ImportedModule> &Loaded,
2582                             const ModuleFile *ImportedBy,
2583                             unsigned ClientLoadCapabilities) {
2584   BitstreamCursor &Stream = F.Stream;
2585 
2586   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2587     Error(std::move(Err));
2588     return Failure;
2589   }
2590 
2591   // Lambda to read the unhashed control block the first time it's called.
2592   //
2593   // For PCM files, the unhashed control block cannot be read until after the
2594   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2595   // need to look ahead before reading the IMPORTS record.  For consistency,
2596   // this block is always read somehow (see BitstreamEntry::EndBlock).
2597   bool HasReadUnhashedControlBlock = false;
2598   auto readUnhashedControlBlockOnce = [&]() {
2599     if (!HasReadUnhashedControlBlock) {
2600       HasReadUnhashedControlBlock = true;
2601       if (ASTReadResult Result =
2602               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2603         return Result;
2604     }
2605     return Success;
2606   };
2607 
2608   bool DisableValidation = shouldDisableValidationForFile(F);
2609 
2610   // Read all of the records and blocks in the control block.
2611   RecordData Record;
2612   unsigned NumInputs = 0;
2613   unsigned NumUserInputs = 0;
2614   StringRef BaseDirectoryAsWritten;
2615   while (true) {
2616     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2617     if (!MaybeEntry) {
2618       Error(MaybeEntry.takeError());
2619       return Failure;
2620     }
2621     llvm::BitstreamEntry Entry = MaybeEntry.get();
2622 
2623     switch (Entry.Kind) {
2624     case llvm::BitstreamEntry::Error:
2625       Error("malformed block record in AST file");
2626       return Failure;
2627     case llvm::BitstreamEntry::EndBlock: {
2628       // Validate the module before returning.  This call catches an AST with
2629       // no module name and no imports.
2630       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2631         return Result;
2632 
2633       // Validate input files.
2634       const HeaderSearchOptions &HSOpts =
2635           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2636 
2637       // All user input files reside at the index range [0, NumUserInputs), and
2638       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2639       // loaded module files, ignore missing inputs.
2640       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2641           F.Kind != MK_PrebuiltModule) {
2642         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2643 
2644         // If we are reading a module, we will create a verification timestamp,
2645         // so we verify all input files.  Otherwise, verify only user input
2646         // files.
2647 
2648         unsigned N = NumUserInputs;
2649         if (ValidateSystemInputs ||
2650             (HSOpts.ModulesValidateOncePerBuildSession &&
2651              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2652              F.Kind == MK_ImplicitModule))
2653           N = NumInputs;
2654 
2655         for (unsigned I = 0; I < N; ++I) {
2656           InputFile IF = getInputFile(F, I+1, Complain);
2657           if (!IF.getFile() || IF.isOutOfDate())
2658             return OutOfDate;
2659         }
2660       }
2661 
2662       if (Listener)
2663         Listener->visitModuleFile(F.FileName, F.Kind);
2664 
2665       if (Listener && Listener->needsInputFileVisitation()) {
2666         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2667                                                                 : NumUserInputs;
2668         for (unsigned I = 0; I < N; ++I) {
2669           bool IsSystem = I >= NumUserInputs;
2670           InputFileInfo FI = readInputFileInfo(F, I+1);
2671           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2672                                    F.Kind == MK_ExplicitModule ||
2673                                    F.Kind == MK_PrebuiltModule);
2674         }
2675       }
2676 
2677       return Success;
2678     }
2679 
2680     case llvm::BitstreamEntry::SubBlock:
2681       switch (Entry.ID) {
2682       case INPUT_FILES_BLOCK_ID:
2683         F.InputFilesCursor = Stream;
2684         if (llvm::Error Err = Stream.SkipBlock()) {
2685           Error(std::move(Err));
2686           return Failure;
2687         }
2688         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2689           Error("malformed block record in AST file");
2690           return Failure;
2691         }
2692         continue;
2693 
2694       case OPTIONS_BLOCK_ID:
2695         // If we're reading the first module for this group, check its options
2696         // are compatible with ours. For modules it imports, no further checking
2697         // is required, because we checked them when we built it.
2698         if (Listener && !ImportedBy) {
2699           // Should we allow the configuration of the module file to differ from
2700           // the configuration of the current translation unit in a compatible
2701           // way?
2702           //
2703           // FIXME: Allow this for files explicitly specified with -include-pch.
2704           bool AllowCompatibleConfigurationMismatch =
2705               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2706 
2707           ASTReadResult Result =
2708               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2709                                AllowCompatibleConfigurationMismatch, *Listener,
2710                                SuggestedPredefines);
2711           if (Result == Failure) {
2712             Error("malformed block record in AST file");
2713             return Result;
2714           }
2715 
2716           if (DisableValidation ||
2717               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2718             Result = Success;
2719 
2720           // If we can't load the module, exit early since we likely
2721           // will rebuild the module anyway. The stream may be in the
2722           // middle of a block.
2723           if (Result != Success)
2724             return Result;
2725         } else if (llvm::Error Err = Stream.SkipBlock()) {
2726           Error(std::move(Err));
2727           return Failure;
2728         }
2729         continue;
2730 
2731       default:
2732         if (llvm::Error Err = Stream.SkipBlock()) {
2733           Error(std::move(Err));
2734           return Failure;
2735         }
2736         continue;
2737       }
2738 
2739     case llvm::BitstreamEntry::Record:
2740       // The interesting case.
2741       break;
2742     }
2743 
2744     // Read and process a record.
2745     Record.clear();
2746     StringRef Blob;
2747     Expected<unsigned> MaybeRecordType =
2748         Stream.readRecord(Entry.ID, Record, &Blob);
2749     if (!MaybeRecordType) {
2750       Error(MaybeRecordType.takeError());
2751       return Failure;
2752     }
2753     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2754     case METADATA: {
2755       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2756         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2757           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2758                                         : diag::err_pch_version_too_new);
2759         return VersionMismatch;
2760       }
2761 
2762       bool hasErrors = Record[6];
2763       if (hasErrors && !DisableValidation) {
2764         // If requested by the caller and the module hasn't already been read
2765         // or compiled, mark modules on error as out-of-date.
2766         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2767             !ModuleMgr.getModuleCache().isPCMFinal(F.FileName))
2768           return OutOfDate;
2769 
2770         if (!AllowASTWithCompilerErrors) {
2771           Diag(diag::err_pch_with_compiler_errors);
2772           return HadErrors;
2773         }
2774       }
2775       if (hasErrors) {
2776         Diags.ErrorOccurred = true;
2777         Diags.UncompilableErrorOccurred = true;
2778         Diags.UnrecoverableErrorOccurred = true;
2779       }
2780 
2781       F.RelocatablePCH = Record[4];
2782       // Relative paths in a relocatable PCH are relative to our sysroot.
2783       if (F.RelocatablePCH)
2784         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2785 
2786       F.HasTimestamps = Record[5];
2787 
2788       const std::string &CurBranch = getClangFullRepositoryVersion();
2789       StringRef ASTBranch = Blob;
2790       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2791         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2792           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2793         return VersionMismatch;
2794       }
2795       break;
2796     }
2797 
2798     case IMPORTS: {
2799       // Validate the AST before processing any imports (otherwise, untangling
2800       // them can be error-prone and expensive).  A module will have a name and
2801       // will already have been validated, but this catches the PCH case.
2802       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2803         return Result;
2804 
2805       // Load each of the imported PCH files.
2806       unsigned Idx = 0, N = Record.size();
2807       while (Idx < N) {
2808         // Read information about the AST file.
2809         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2810         // The import location will be the local one for now; we will adjust
2811         // all import locations of module imports after the global source
2812         // location info are setup, in ReadAST.
2813         SourceLocation ImportLoc =
2814             ReadUntranslatedSourceLocation(Record[Idx++]);
2815         off_t StoredSize = (off_t)Record[Idx++];
2816         time_t StoredModTime = (time_t)Record[Idx++];
2817         auto FirstSignatureByte = Record.begin() + Idx;
2818         ASTFileSignature StoredSignature = ASTFileSignature::create(
2819             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2820         Idx += ASTFileSignature::size;
2821 
2822         std::string ImportedName = ReadString(Record, Idx);
2823         std::string ImportedFile;
2824 
2825         // For prebuilt and explicit modules first consult the file map for
2826         // an override. Note that here we don't search prebuilt module
2827         // directories, only the explicit name to file mappings. Also, we will
2828         // still verify the size/signature making sure it is essentially the
2829         // same file but perhaps in a different location.
2830         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2831           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2832             ImportedName, /*FileMapOnly*/ true);
2833 
2834         if (ImportedFile.empty())
2835           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2836           // ModuleCache as when writing.
2837           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2838         else
2839           SkipPath(Record, Idx);
2840 
2841         // If our client can't cope with us being out of date, we can't cope with
2842         // our dependency being missing.
2843         unsigned Capabilities = ClientLoadCapabilities;
2844         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2845           Capabilities &= ~ARR_Missing;
2846 
2847         // Load the AST file.
2848         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2849                                   Loaded, StoredSize, StoredModTime,
2850                                   StoredSignature, Capabilities);
2851 
2852         // If we diagnosed a problem, produce a backtrace.
2853         if (isDiagnosedResult(Result, Capabilities))
2854           Diag(diag::note_module_file_imported_by)
2855               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2856 
2857         switch (Result) {
2858         case Failure: return Failure;
2859           // If we have to ignore the dependency, we'll have to ignore this too.
2860         case Missing:
2861         case OutOfDate: return OutOfDate;
2862         case VersionMismatch: return VersionMismatch;
2863         case ConfigurationMismatch: return ConfigurationMismatch;
2864         case HadErrors: return HadErrors;
2865         case Success: break;
2866         }
2867       }
2868       break;
2869     }
2870 
2871     case ORIGINAL_FILE:
2872       F.OriginalSourceFileID = FileID::get(Record[0]);
2873       F.ActualOriginalSourceFileName = std::string(Blob);
2874       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2875       ResolveImportedPath(F, F.OriginalSourceFileName);
2876       break;
2877 
2878     case ORIGINAL_FILE_ID:
2879       F.OriginalSourceFileID = FileID::get(Record[0]);
2880       break;
2881 
2882     case ORIGINAL_PCH_DIR:
2883       F.OriginalDir = std::string(Blob);
2884       break;
2885 
2886     case MODULE_NAME:
2887       F.ModuleName = std::string(Blob);
2888       Diag(diag::remark_module_import)
2889           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2890           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2891       if (Listener)
2892         Listener->ReadModuleName(F.ModuleName);
2893 
2894       // Validate the AST as soon as we have a name so we can exit early on
2895       // failure.
2896       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2897         return Result;
2898 
2899       break;
2900 
2901     case MODULE_DIRECTORY: {
2902       // Save the BaseDirectory as written in the PCM for computing the module
2903       // filename for the ModuleCache.
2904       BaseDirectoryAsWritten = Blob;
2905       assert(!F.ModuleName.empty() &&
2906              "MODULE_DIRECTORY found before MODULE_NAME");
2907       // If we've already loaded a module map file covering this module, we may
2908       // have a better path for it (relative to the current build).
2909       Module *M = PP.getHeaderSearchInfo().lookupModule(
2910           F.ModuleName, /*AllowSearch*/ true,
2911           /*AllowExtraModuleMapSearch*/ true);
2912       if (M && M->Directory) {
2913         // If we're implicitly loading a module, the base directory can't
2914         // change between the build and use.
2915         // Don't emit module relocation error if we have -fno-validate-pch
2916         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2917                   DisableValidationForModuleKind::Module) &&
2918             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2919           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2920           if (!BuildDir || *BuildDir != M->Directory) {
2921             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2922               Diag(diag::err_imported_module_relocated)
2923                   << F.ModuleName << Blob << M->Directory->getName();
2924             return OutOfDate;
2925           }
2926         }
2927         F.BaseDirectory = std::string(M->Directory->getName());
2928       } else {
2929         F.BaseDirectory = std::string(Blob);
2930       }
2931       break;
2932     }
2933 
2934     case MODULE_MAP_FILE:
2935       if (ASTReadResult Result =
2936               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2937         return Result;
2938       break;
2939 
2940     case INPUT_FILE_OFFSETS:
2941       NumInputs = Record[0];
2942       NumUserInputs = Record[1];
2943       F.InputFileOffsets =
2944           (const llvm::support::unaligned_uint64_t *)Blob.data();
2945       F.InputFilesLoaded.resize(NumInputs);
2946       F.NumUserInputFiles = NumUserInputs;
2947       break;
2948     }
2949   }
2950 }
2951 
2952 ASTReader::ASTReadResult
2953 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2954   BitstreamCursor &Stream = F.Stream;
2955 
2956   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2957     Error(std::move(Err));
2958     return Failure;
2959   }
2960   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2961 
2962   // Read all of the records and blocks for the AST file.
2963   RecordData Record;
2964   while (true) {
2965     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2966     if (!MaybeEntry) {
2967       Error(MaybeEntry.takeError());
2968       return Failure;
2969     }
2970     llvm::BitstreamEntry Entry = MaybeEntry.get();
2971 
2972     switch (Entry.Kind) {
2973     case llvm::BitstreamEntry::Error:
2974       Error("error at end of module block in AST file");
2975       return Failure;
2976     case llvm::BitstreamEntry::EndBlock:
2977       // Outside of C++, we do not store a lookup map for the translation unit.
2978       // Instead, mark it as needing a lookup map to be built if this module
2979       // contains any declarations lexically within it (which it always does!).
2980       // This usually has no cost, since we very rarely need the lookup map for
2981       // the translation unit outside C++.
2982       if (ASTContext *Ctx = ContextObj) {
2983         DeclContext *DC = Ctx->getTranslationUnitDecl();
2984         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2985           DC->setMustBuildLookupTable();
2986       }
2987 
2988       return Success;
2989     case llvm::BitstreamEntry::SubBlock:
2990       switch (Entry.ID) {
2991       case DECLTYPES_BLOCK_ID:
2992         // We lazily load the decls block, but we want to set up the
2993         // DeclsCursor cursor to point into it.  Clone our current bitcode
2994         // cursor to it, enter the block and read the abbrevs in that block.
2995         // With the main cursor, we just skip over it.
2996         F.DeclsCursor = Stream;
2997         if (llvm::Error Err = Stream.SkipBlock()) {
2998           Error(std::move(Err));
2999           return Failure;
3000         }
3001         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
3002                              &F.DeclsBlockStartOffset)) {
3003           Error("malformed block record in AST file");
3004           return Failure;
3005         }
3006         break;
3007 
3008       case PREPROCESSOR_BLOCK_ID:
3009         F.MacroCursor = Stream;
3010         if (!PP.getExternalSource())
3011           PP.setExternalSource(this);
3012 
3013         if (llvm::Error Err = Stream.SkipBlock()) {
3014           Error(std::move(Err));
3015           return Failure;
3016         }
3017         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
3018           Error("malformed block record in AST file");
3019           return Failure;
3020         }
3021         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3022         break;
3023 
3024       case PREPROCESSOR_DETAIL_BLOCK_ID:
3025         F.PreprocessorDetailCursor = Stream;
3026 
3027         if (llvm::Error Err = Stream.SkipBlock()) {
3028           Error(std::move(Err));
3029           return Failure;
3030         }
3031         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3032                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3033           Error("malformed preprocessor detail record in AST file");
3034           return Failure;
3035         }
3036         F.PreprocessorDetailStartOffset
3037         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3038 
3039         if (!PP.getPreprocessingRecord())
3040           PP.createPreprocessingRecord();
3041         if (!PP.getPreprocessingRecord()->getExternalSource())
3042           PP.getPreprocessingRecord()->SetExternalSource(*this);
3043         break;
3044 
3045       case SOURCE_MANAGER_BLOCK_ID:
3046         if (ReadSourceManagerBlock(F))
3047           return Failure;
3048         break;
3049 
3050       case SUBMODULE_BLOCK_ID:
3051         if (ASTReadResult Result =
3052                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3053           return Result;
3054         break;
3055 
3056       case COMMENTS_BLOCK_ID: {
3057         BitstreamCursor C = Stream;
3058 
3059         if (llvm::Error Err = Stream.SkipBlock()) {
3060           Error(std::move(Err));
3061           return Failure;
3062         }
3063         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3064           Error("malformed comments block in AST file");
3065           return Failure;
3066         }
3067         CommentsCursors.push_back(std::make_pair(C, &F));
3068         break;
3069       }
3070 
3071       default:
3072         if (llvm::Error Err = Stream.SkipBlock()) {
3073           Error(std::move(Err));
3074           return Failure;
3075         }
3076         break;
3077       }
3078       continue;
3079 
3080     case llvm::BitstreamEntry::Record:
3081       // The interesting case.
3082       break;
3083     }
3084 
3085     // Read and process a record.
3086     Record.clear();
3087     StringRef Blob;
3088     Expected<unsigned> MaybeRecordType =
3089         Stream.readRecord(Entry.ID, Record, &Blob);
3090     if (!MaybeRecordType) {
3091       Error(MaybeRecordType.takeError());
3092       return Failure;
3093     }
3094     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3095 
3096     // If we're not loading an AST context, we don't care about most records.
3097     if (!ContextObj) {
3098       switch (RecordType) {
3099       case IDENTIFIER_TABLE:
3100       case IDENTIFIER_OFFSET:
3101       case INTERESTING_IDENTIFIERS:
3102       case STATISTICS:
3103       case PP_CONDITIONAL_STACK:
3104       case PP_COUNTER_VALUE:
3105       case SOURCE_LOCATION_OFFSETS:
3106       case MODULE_OFFSET_MAP:
3107       case SOURCE_MANAGER_LINE_TABLE:
3108       case SOURCE_LOCATION_PRELOADS:
3109       case PPD_ENTITIES_OFFSETS:
3110       case HEADER_SEARCH_TABLE:
3111       case IMPORTED_MODULES:
3112       case MACRO_OFFSET:
3113         break;
3114       default:
3115         continue;
3116       }
3117     }
3118 
3119     switch (RecordType) {
3120     default:  // Default behavior: ignore.
3121       break;
3122 
3123     case TYPE_OFFSET: {
3124       if (F.LocalNumTypes != 0) {
3125         Error("duplicate TYPE_OFFSET record in AST file");
3126         return Failure;
3127       }
3128       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3129       F.LocalNumTypes = Record[0];
3130       unsigned LocalBaseTypeIndex = Record[1];
3131       F.BaseTypeIndex = getTotalNumTypes();
3132 
3133       if (F.LocalNumTypes > 0) {
3134         // Introduce the global -> local mapping for types within this module.
3135         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3136 
3137         // Introduce the local -> global mapping for types within this module.
3138         F.TypeRemap.insertOrReplace(
3139           std::make_pair(LocalBaseTypeIndex,
3140                          F.BaseTypeIndex - LocalBaseTypeIndex));
3141 
3142         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3143       }
3144       break;
3145     }
3146 
3147     case DECL_OFFSET: {
3148       if (F.LocalNumDecls != 0) {
3149         Error("duplicate DECL_OFFSET record in AST file");
3150         return Failure;
3151       }
3152       F.DeclOffsets = (const DeclOffset *)Blob.data();
3153       F.LocalNumDecls = Record[0];
3154       unsigned LocalBaseDeclID = Record[1];
3155       F.BaseDeclID = getTotalNumDecls();
3156 
3157       if (F.LocalNumDecls > 0) {
3158         // Introduce the global -> local mapping for declarations within this
3159         // module.
3160         GlobalDeclMap.insert(
3161           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3162 
3163         // Introduce the local -> global mapping for declarations within this
3164         // module.
3165         F.DeclRemap.insertOrReplace(
3166           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3167 
3168         // Introduce the global -> local mapping for declarations within this
3169         // module.
3170         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3171 
3172         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3173       }
3174       break;
3175     }
3176 
3177     case TU_UPDATE_LEXICAL: {
3178       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3179       LexicalContents Contents(
3180           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3181               Blob.data()),
3182           static_cast<unsigned int>(Blob.size() / 4));
3183       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3184       TU->setHasExternalLexicalStorage(true);
3185       break;
3186     }
3187 
3188     case UPDATE_VISIBLE: {
3189       unsigned Idx = 0;
3190       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3191       auto *Data = (const unsigned char*)Blob.data();
3192       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3193       // If we've already loaded the decl, perform the updates when we finish
3194       // loading this block.
3195       if (Decl *D = GetExistingDecl(ID))
3196         PendingUpdateRecords.push_back(
3197             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3198       break;
3199     }
3200 
3201     case IDENTIFIER_TABLE:
3202       F.IdentifierTableData =
3203           reinterpret_cast<const unsigned char *>(Blob.data());
3204       if (Record[0]) {
3205         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3206             F.IdentifierTableData + Record[0],
3207             F.IdentifierTableData + sizeof(uint32_t),
3208             F.IdentifierTableData,
3209             ASTIdentifierLookupTrait(*this, F));
3210 
3211         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3212       }
3213       break;
3214 
3215     case IDENTIFIER_OFFSET: {
3216       if (F.LocalNumIdentifiers != 0) {
3217         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3218         return Failure;
3219       }
3220       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3221       F.LocalNumIdentifiers = Record[0];
3222       unsigned LocalBaseIdentifierID = Record[1];
3223       F.BaseIdentifierID = getTotalNumIdentifiers();
3224 
3225       if (F.LocalNumIdentifiers > 0) {
3226         // Introduce the global -> local mapping for identifiers within this
3227         // module.
3228         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3229                                                   &F));
3230 
3231         // Introduce the local -> global mapping for identifiers within this
3232         // module.
3233         F.IdentifierRemap.insertOrReplace(
3234           std::make_pair(LocalBaseIdentifierID,
3235                          F.BaseIdentifierID - LocalBaseIdentifierID));
3236 
3237         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3238                                  + F.LocalNumIdentifiers);
3239       }
3240       break;
3241     }
3242 
3243     case INTERESTING_IDENTIFIERS:
3244       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3245       break;
3246 
3247     case EAGERLY_DESERIALIZED_DECLS:
3248       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3249       // about "interesting" decls (for instance, if we're building a module).
3250       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3251         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3252       break;
3253 
3254     case MODULAR_CODEGEN_DECLS:
3255       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3256       // them (ie: if we're not codegenerating this module).
3257       if (F.Kind == MK_MainFile ||
3258           getContext().getLangOpts().BuildingPCHWithObjectFile)
3259         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3260           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3261       break;
3262 
3263     case SPECIAL_TYPES:
3264       if (SpecialTypes.empty()) {
3265         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3266           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3267         break;
3268       }
3269 
3270       if (SpecialTypes.size() != Record.size()) {
3271         Error("invalid special-types record");
3272         return Failure;
3273       }
3274 
3275       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3276         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3277         if (!SpecialTypes[I])
3278           SpecialTypes[I] = ID;
3279         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3280         // merge step?
3281       }
3282       break;
3283 
3284     case STATISTICS:
3285       TotalNumStatements += Record[0];
3286       TotalNumMacros += Record[1];
3287       TotalLexicalDeclContexts += Record[2];
3288       TotalVisibleDeclContexts += Record[3];
3289       break;
3290 
3291     case UNUSED_FILESCOPED_DECLS:
3292       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3293         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3294       break;
3295 
3296     case DELEGATING_CTORS:
3297       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3298         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3299       break;
3300 
3301     case WEAK_UNDECLARED_IDENTIFIERS:
3302       if (Record.size() % 4 != 0) {
3303         Error("invalid weak identifiers record");
3304         return Failure;
3305       }
3306 
3307       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3308       // files. This isn't the way to do it :)
3309       WeakUndeclaredIdentifiers.clear();
3310 
3311       // Translate the weak, undeclared identifiers into global IDs.
3312       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3313         WeakUndeclaredIdentifiers.push_back(
3314           getGlobalIdentifierID(F, Record[I++]));
3315         WeakUndeclaredIdentifiers.push_back(
3316           getGlobalIdentifierID(F, Record[I++]));
3317         WeakUndeclaredIdentifiers.push_back(
3318           ReadSourceLocation(F, Record, I).getRawEncoding());
3319         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3320       }
3321       break;
3322 
3323     case SELECTOR_OFFSETS: {
3324       F.SelectorOffsets = (const uint32_t *)Blob.data();
3325       F.LocalNumSelectors = Record[0];
3326       unsigned LocalBaseSelectorID = Record[1];
3327       F.BaseSelectorID = getTotalNumSelectors();
3328 
3329       if (F.LocalNumSelectors > 0) {
3330         // Introduce the global -> local mapping for selectors within this
3331         // module.
3332         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3333 
3334         // Introduce the local -> global mapping for selectors within this
3335         // module.
3336         F.SelectorRemap.insertOrReplace(
3337           std::make_pair(LocalBaseSelectorID,
3338                          F.BaseSelectorID - LocalBaseSelectorID));
3339 
3340         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3341       }
3342       break;
3343     }
3344 
3345     case METHOD_POOL:
3346       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3347       if (Record[0])
3348         F.SelectorLookupTable
3349           = ASTSelectorLookupTable::Create(
3350                         F.SelectorLookupTableData + Record[0],
3351                         F.SelectorLookupTableData,
3352                         ASTSelectorLookupTrait(*this, F));
3353       TotalNumMethodPoolEntries += Record[1];
3354       break;
3355 
3356     case REFERENCED_SELECTOR_POOL:
3357       if (!Record.empty()) {
3358         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3359           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3360                                                                 Record[Idx++]));
3361           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3362                                               getRawEncoding());
3363         }
3364       }
3365       break;
3366 
3367     case PP_CONDITIONAL_STACK:
3368       if (!Record.empty()) {
3369         unsigned Idx = 0, End = Record.size() - 1;
3370         bool ReachedEOFWhileSkipping = Record[Idx++];
3371         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3372         if (ReachedEOFWhileSkipping) {
3373           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3374           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3375           bool FoundNonSkipPortion = Record[Idx++];
3376           bool FoundElse = Record[Idx++];
3377           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3378           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3379                            FoundElse, ElseLoc);
3380         }
3381         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3382         while (Idx < End) {
3383           auto Loc = ReadSourceLocation(F, Record, Idx);
3384           bool WasSkipping = Record[Idx++];
3385           bool FoundNonSkip = Record[Idx++];
3386           bool FoundElse = Record[Idx++];
3387           ConditionalStack.push_back(
3388               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3389         }
3390         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3391       }
3392       break;
3393 
3394     case PP_COUNTER_VALUE:
3395       if (!Record.empty() && Listener)
3396         Listener->ReadCounter(F, Record[0]);
3397       break;
3398 
3399     case FILE_SORTED_DECLS:
3400       F.FileSortedDecls = (const DeclID *)Blob.data();
3401       F.NumFileSortedDecls = Record[0];
3402       break;
3403 
3404     case SOURCE_LOCATION_OFFSETS: {
3405       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3406       F.LocalNumSLocEntries = Record[0];
3407       unsigned SLocSpaceSize = Record[1];
3408       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3409       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3410           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3411                                               SLocSpaceSize);
3412       if (!F.SLocEntryBaseID) {
3413         Error("ran out of source locations");
3414         break;
3415       }
3416       // Make our entry in the range map. BaseID is negative and growing, so
3417       // we invert it. Because we invert it, though, we need the other end of
3418       // the range.
3419       unsigned RangeStart =
3420           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3421       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3422       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3423 
3424       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3425       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3426       GlobalSLocOffsetMap.insert(
3427           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3428                            - SLocSpaceSize,&F));
3429 
3430       // Initialize the remapping table.
3431       // Invalid stays invalid.
3432       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3433       // This module. Base was 2 when being compiled.
3434       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3435                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3436 
3437       TotalNumSLocEntries += F.LocalNumSLocEntries;
3438       break;
3439     }
3440 
3441     case MODULE_OFFSET_MAP:
3442       F.ModuleOffsetMap = Blob;
3443       break;
3444 
3445     case SOURCE_MANAGER_LINE_TABLE:
3446       if (ParseLineTable(F, Record)) {
3447         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3448         return Failure;
3449       }
3450       break;
3451 
3452     case SOURCE_LOCATION_PRELOADS: {
3453       // Need to transform from the local view (1-based IDs) to the global view,
3454       // which is based off F.SLocEntryBaseID.
3455       if (!F.PreloadSLocEntries.empty()) {
3456         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3457         return Failure;
3458       }
3459 
3460       F.PreloadSLocEntries.swap(Record);
3461       break;
3462     }
3463 
3464     case EXT_VECTOR_DECLS:
3465       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3466         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3467       break;
3468 
3469     case VTABLE_USES:
3470       if (Record.size() % 3 != 0) {
3471         Error("Invalid VTABLE_USES record");
3472         return Failure;
3473       }
3474 
3475       // Later tables overwrite earlier ones.
3476       // FIXME: Modules will have some trouble with this. This is clearly not
3477       // the right way to do this.
3478       VTableUses.clear();
3479 
3480       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3481         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3482         VTableUses.push_back(
3483           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3484         VTableUses.push_back(Record[Idx++]);
3485       }
3486       break;
3487 
3488     case PENDING_IMPLICIT_INSTANTIATIONS:
3489       if (PendingInstantiations.size() % 2 != 0) {
3490         Error("Invalid existing PendingInstantiations");
3491         return Failure;
3492       }
3493 
3494       if (Record.size() % 2 != 0) {
3495         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3496         return Failure;
3497       }
3498 
3499       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3500         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3501         PendingInstantiations.push_back(
3502           ReadSourceLocation(F, Record, I).getRawEncoding());
3503       }
3504       break;
3505 
3506     case SEMA_DECL_REFS:
3507       if (Record.size() != 3) {
3508         Error("Invalid SEMA_DECL_REFS block");
3509         return Failure;
3510       }
3511       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3512         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3513       break;
3514 
3515     case PPD_ENTITIES_OFFSETS: {
3516       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3517       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3518       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3519 
3520       unsigned LocalBasePreprocessedEntityID = Record[0];
3521 
3522       unsigned StartingID;
3523       if (!PP.getPreprocessingRecord())
3524         PP.createPreprocessingRecord();
3525       if (!PP.getPreprocessingRecord()->getExternalSource())
3526         PP.getPreprocessingRecord()->SetExternalSource(*this);
3527       StartingID
3528         = PP.getPreprocessingRecord()
3529             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3530       F.BasePreprocessedEntityID = StartingID;
3531 
3532       if (F.NumPreprocessedEntities > 0) {
3533         // Introduce the global -> local mapping for preprocessed entities in
3534         // this module.
3535         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3536 
3537         // Introduce the local -> global mapping for preprocessed entities in
3538         // this module.
3539         F.PreprocessedEntityRemap.insertOrReplace(
3540           std::make_pair(LocalBasePreprocessedEntityID,
3541             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3542       }
3543 
3544       break;
3545     }
3546 
3547     case PPD_SKIPPED_RANGES: {
3548       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3549       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3550       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3551 
3552       if (!PP.getPreprocessingRecord())
3553         PP.createPreprocessingRecord();
3554       if (!PP.getPreprocessingRecord()->getExternalSource())
3555         PP.getPreprocessingRecord()->SetExternalSource(*this);
3556       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3557           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3558 
3559       if (F.NumPreprocessedSkippedRanges > 0)
3560         GlobalSkippedRangeMap.insert(
3561             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3562       break;
3563     }
3564 
3565     case DECL_UPDATE_OFFSETS:
3566       if (Record.size() % 2 != 0) {
3567         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3568         return Failure;
3569       }
3570       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3571         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3572         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3573 
3574         // If we've already loaded the decl, perform the updates when we finish
3575         // loading this block.
3576         if (Decl *D = GetExistingDecl(ID))
3577           PendingUpdateRecords.push_back(
3578               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3579       }
3580       break;
3581 
3582     case OBJC_CATEGORIES_MAP:
3583       if (F.LocalNumObjCCategoriesInMap != 0) {
3584         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3585         return Failure;
3586       }
3587 
3588       F.LocalNumObjCCategoriesInMap = Record[0];
3589       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3590       break;
3591 
3592     case OBJC_CATEGORIES:
3593       F.ObjCCategories.swap(Record);
3594       break;
3595 
3596     case CUDA_SPECIAL_DECL_REFS:
3597       // Later tables overwrite earlier ones.
3598       // FIXME: Modules will have trouble with this.
3599       CUDASpecialDeclRefs.clear();
3600       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3601         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3602       break;
3603 
3604     case HEADER_SEARCH_TABLE:
3605       F.HeaderFileInfoTableData = Blob.data();
3606       F.LocalNumHeaderFileInfos = Record[1];
3607       if (Record[0]) {
3608         F.HeaderFileInfoTable
3609           = HeaderFileInfoLookupTable::Create(
3610                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3611                    (const unsigned char *)F.HeaderFileInfoTableData,
3612                    HeaderFileInfoTrait(*this, F,
3613                                        &PP.getHeaderSearchInfo(),
3614                                        Blob.data() + Record[2]));
3615 
3616         PP.getHeaderSearchInfo().SetExternalSource(this);
3617         if (!PP.getHeaderSearchInfo().getExternalLookup())
3618           PP.getHeaderSearchInfo().SetExternalLookup(this);
3619       }
3620       break;
3621 
3622     case FP_PRAGMA_OPTIONS:
3623       // Later tables overwrite earlier ones.
3624       FPPragmaOptions.swap(Record);
3625       break;
3626 
3627     case OPENCL_EXTENSIONS:
3628       for (unsigned I = 0, E = Record.size(); I != E; ) {
3629         auto Name = ReadString(Record, I);
3630         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3631         OptInfo.Supported = Record[I++] != 0;
3632         OptInfo.Enabled = Record[I++] != 0;
3633         OptInfo.WithPragma = Record[I++] != 0;
3634         OptInfo.Avail = Record[I++];
3635         OptInfo.Core = Record[I++];
3636         OptInfo.Opt = Record[I++];
3637       }
3638       break;
3639 
3640     case TENTATIVE_DEFINITIONS:
3641       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3642         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3643       break;
3644 
3645     case KNOWN_NAMESPACES:
3646       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3647         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3648       break;
3649 
3650     case UNDEFINED_BUT_USED:
3651       if (UndefinedButUsed.size() % 2 != 0) {
3652         Error("Invalid existing UndefinedButUsed");
3653         return Failure;
3654       }
3655 
3656       if (Record.size() % 2 != 0) {
3657         Error("invalid undefined-but-used record");
3658         return Failure;
3659       }
3660       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3661         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3662         UndefinedButUsed.push_back(
3663             ReadSourceLocation(F, Record, I).getRawEncoding());
3664       }
3665       break;
3666 
3667     case DELETE_EXPRS_TO_ANALYZE:
3668       for (unsigned I = 0, N = Record.size(); I != N;) {
3669         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3670         const uint64_t Count = Record[I++];
3671         DelayedDeleteExprs.push_back(Count);
3672         for (uint64_t C = 0; C < Count; ++C) {
3673           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3674           bool IsArrayForm = Record[I++] == 1;
3675           DelayedDeleteExprs.push_back(IsArrayForm);
3676         }
3677       }
3678       break;
3679 
3680     case IMPORTED_MODULES:
3681       if (!F.isModule()) {
3682         // If we aren't loading a module (which has its own exports), make
3683         // all of the imported modules visible.
3684         // FIXME: Deal with macros-only imports.
3685         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3686           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3687           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3688           if (GlobalID) {
3689             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3690             if (DeserializationListener)
3691               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3692           }
3693         }
3694       }
3695       break;
3696 
3697     case MACRO_OFFSET: {
3698       if (F.LocalNumMacros != 0) {
3699         Error("duplicate MACRO_OFFSET record in AST file");
3700         return Failure;
3701       }
3702       F.MacroOffsets = (const uint32_t *)Blob.data();
3703       F.LocalNumMacros = Record[0];
3704       unsigned LocalBaseMacroID = Record[1];
3705       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3706       F.BaseMacroID = getTotalNumMacros();
3707 
3708       if (F.LocalNumMacros > 0) {
3709         // Introduce the global -> local mapping for macros within this module.
3710         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3711 
3712         // Introduce the local -> global mapping for macros within this module.
3713         F.MacroRemap.insertOrReplace(
3714           std::make_pair(LocalBaseMacroID,
3715                          F.BaseMacroID - LocalBaseMacroID));
3716 
3717         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3718       }
3719       break;
3720     }
3721 
3722     case LATE_PARSED_TEMPLATE:
3723       LateParsedTemplates.emplace_back(
3724           std::piecewise_construct, std::forward_as_tuple(&F),
3725           std::forward_as_tuple(Record.begin(), Record.end()));
3726       break;
3727 
3728     case OPTIMIZE_PRAGMA_OPTIONS:
3729       if (Record.size() != 1) {
3730         Error("invalid pragma optimize record");
3731         return Failure;
3732       }
3733       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3734       break;
3735 
3736     case MSSTRUCT_PRAGMA_OPTIONS:
3737       if (Record.size() != 1) {
3738         Error("invalid pragma ms_struct record");
3739         return Failure;
3740       }
3741       PragmaMSStructState = Record[0];
3742       break;
3743 
3744     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3745       if (Record.size() != 2) {
3746         Error("invalid pragma ms_struct record");
3747         return Failure;
3748       }
3749       PragmaMSPointersToMembersState = Record[0];
3750       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3751       break;
3752 
3753     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3754       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3755         UnusedLocalTypedefNameCandidates.push_back(
3756             getGlobalDeclID(F, Record[I]));
3757       break;
3758 
3759     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3760       if (Record.size() != 1) {
3761         Error("invalid cuda pragma options record");
3762         return Failure;
3763       }
3764       ForceCUDAHostDeviceDepth = Record[0];
3765       break;
3766 
3767     case ALIGN_PACK_PRAGMA_OPTIONS: {
3768       if (Record.size() < 3) {
3769         Error("invalid pragma pack record");
3770         return Failure;
3771       }
3772       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3773       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3774       unsigned NumStackEntries = Record[2];
3775       unsigned Idx = 3;
3776       // Reset the stack when importing a new module.
3777       PragmaAlignPackStack.clear();
3778       for (unsigned I = 0; I < NumStackEntries; ++I) {
3779         PragmaAlignPackStackEntry Entry;
3780         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3781         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3782         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3783         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3784         Entry.SlotLabel = PragmaAlignPackStrings.back();
3785         PragmaAlignPackStack.push_back(Entry);
3786       }
3787       break;
3788     }
3789 
3790     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3791       if (Record.size() < 3) {
3792         Error("invalid pragma pack record");
3793         return Failure;
3794       }
3795       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3796       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3797       unsigned NumStackEntries = Record[2];
3798       unsigned Idx = 3;
3799       // Reset the stack when importing a new module.
3800       FpPragmaStack.clear();
3801       for (unsigned I = 0; I < NumStackEntries; ++I) {
3802         FpPragmaStackEntry Entry;
3803         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3804         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3805         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3806         FpPragmaStrings.push_back(ReadString(Record, Idx));
3807         Entry.SlotLabel = FpPragmaStrings.back();
3808         FpPragmaStack.push_back(Entry);
3809       }
3810       break;
3811     }
3812 
3813     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3814       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3815         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3816       break;
3817     }
3818   }
3819 }
3820 
3821 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3822   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3823 
3824   // Additional remapping information.
3825   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3826   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3827   F.ModuleOffsetMap = StringRef();
3828 
3829   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3830   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3831     F.SLocRemap.insert(std::make_pair(0U, 0));
3832     F.SLocRemap.insert(std::make_pair(2U, 1));
3833   }
3834 
3835   // Continuous range maps we may be updating in our module.
3836   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3837   RemapBuilder SLocRemap(F.SLocRemap);
3838   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3839   RemapBuilder MacroRemap(F.MacroRemap);
3840   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3841   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3842   RemapBuilder SelectorRemap(F.SelectorRemap);
3843   RemapBuilder DeclRemap(F.DeclRemap);
3844   RemapBuilder TypeRemap(F.TypeRemap);
3845 
3846   while (Data < DataEnd) {
3847     // FIXME: Looking up dependency modules by filename is horrible. Let's
3848     // start fixing this with prebuilt, explicit and implicit modules and see
3849     // how it goes...
3850     using namespace llvm::support;
3851     ModuleKind Kind = static_cast<ModuleKind>(
3852       endian::readNext<uint8_t, little, unaligned>(Data));
3853     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3854     StringRef Name = StringRef((const char*)Data, Len);
3855     Data += Len;
3856     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3857                               Kind == MK_ImplicitModule
3858                           ? ModuleMgr.lookupByModuleName(Name)
3859                           : ModuleMgr.lookupByFileName(Name));
3860     if (!OM) {
3861       std::string Msg =
3862           "SourceLocation remap refers to unknown module, cannot find ";
3863       Msg.append(std::string(Name));
3864       Error(Msg);
3865       return;
3866     }
3867 
3868     uint32_t SLocOffset =
3869         endian::readNext<uint32_t, little, unaligned>(Data);
3870     uint32_t IdentifierIDOffset =
3871         endian::readNext<uint32_t, little, unaligned>(Data);
3872     uint32_t MacroIDOffset =
3873         endian::readNext<uint32_t, little, unaligned>(Data);
3874     uint32_t PreprocessedEntityIDOffset =
3875         endian::readNext<uint32_t, little, unaligned>(Data);
3876     uint32_t SubmoduleIDOffset =
3877         endian::readNext<uint32_t, little, unaligned>(Data);
3878     uint32_t SelectorIDOffset =
3879         endian::readNext<uint32_t, little, unaligned>(Data);
3880     uint32_t DeclIDOffset =
3881         endian::readNext<uint32_t, little, unaligned>(Data);
3882     uint32_t TypeIndexOffset =
3883         endian::readNext<uint32_t, little, unaligned>(Data);
3884 
3885     uint32_t None = std::numeric_limits<uint32_t>::max();
3886 
3887     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3888                          RemapBuilder &Remap) {
3889       if (Offset != None)
3890         Remap.insert(std::make_pair(Offset,
3891                                     static_cast<int>(BaseOffset - Offset)));
3892     };
3893     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3894     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3895     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3896     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3897               PreprocessedEntityRemap);
3898     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3899     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3900     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3901     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3902 
3903     // Global -> local mappings.
3904     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3905   }
3906 }
3907 
3908 ASTReader::ASTReadResult
3909 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3910                                   const ModuleFile *ImportedBy,
3911                                   unsigned ClientLoadCapabilities) {
3912   unsigned Idx = 0;
3913   F.ModuleMapPath = ReadPath(F, Record, Idx);
3914 
3915   // Try to resolve ModuleName in the current header search context and
3916   // verify that it is found in the same module map file as we saved. If the
3917   // top-level AST file is a main file, skip this check because there is no
3918   // usable header search context.
3919   assert(!F.ModuleName.empty() &&
3920          "MODULE_NAME should come before MODULE_MAP_FILE");
3921   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3922     // An implicitly-loaded module file should have its module listed in some
3923     // module map file that we've already loaded.
3924     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3925     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3926     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3927     // Don't emit module relocation error if we have -fno-validate-pch
3928     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3929               DisableValidationForModuleKind::Module) &&
3930         !ModMap) {
3931       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3932         if (auto ASTFE = M ? M->getASTFile() : None) {
3933           // This module was defined by an imported (explicit) module.
3934           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3935                                                << ASTFE->getName();
3936         } else {
3937           // This module was built with a different module map.
3938           Diag(diag::err_imported_module_not_found)
3939               << F.ModuleName << F.FileName
3940               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3941               << !ImportedBy;
3942           // In case it was imported by a PCH, there's a chance the user is
3943           // just missing to include the search path to the directory containing
3944           // the modulemap.
3945           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3946             Diag(diag::note_imported_by_pch_module_not_found)
3947                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3948         }
3949       }
3950       return OutOfDate;
3951     }
3952 
3953     assert(M && M->Name == F.ModuleName && "found module with different name");
3954 
3955     // Check the primary module map file.
3956     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3957     if (!StoredModMap || *StoredModMap != ModMap) {
3958       assert(ModMap && "found module is missing module map file");
3959       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3960              "top-level import should be verified");
3961       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3962       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3963         Diag(diag::err_imported_module_modmap_changed)
3964             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3965             << ModMap->getName() << F.ModuleMapPath << NotImported;
3966       return OutOfDate;
3967     }
3968 
3969     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3970     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3971       // FIXME: we should use input files rather than storing names.
3972       std::string Filename = ReadPath(F, Record, Idx);
3973       auto F = FileMgr.getFile(Filename, false, false);
3974       if (!F) {
3975         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3976           Error("could not find file '" + Filename +"' referenced by AST file");
3977         return OutOfDate;
3978       }
3979       AdditionalStoredMaps.insert(*F);
3980     }
3981 
3982     // Check any additional module map files (e.g. module.private.modulemap)
3983     // that are not in the pcm.
3984     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3985       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3986         // Remove files that match
3987         // Note: SmallPtrSet::erase is really remove
3988         if (!AdditionalStoredMaps.erase(ModMap)) {
3989           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3990             Diag(diag::err_module_different_modmap)
3991               << F.ModuleName << /*new*/0 << ModMap->getName();
3992           return OutOfDate;
3993         }
3994       }
3995     }
3996 
3997     // Check any additional module map files that are in the pcm, but not
3998     // found in header search. Cases that match are already removed.
3999     for (const FileEntry *ModMap : AdditionalStoredMaps) {
4000       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4001         Diag(diag::err_module_different_modmap)
4002           << F.ModuleName << /*not new*/1 << ModMap->getName();
4003       return OutOfDate;
4004     }
4005   }
4006 
4007   if (Listener)
4008     Listener->ReadModuleMapFile(F.ModuleMapPath);
4009   return Success;
4010 }
4011 
4012 /// Move the given method to the back of the global list of methods.
4013 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4014   // Find the entry for this selector in the method pool.
4015   Sema::GlobalMethodPool::iterator Known
4016     = S.MethodPool.find(Method->getSelector());
4017   if (Known == S.MethodPool.end())
4018     return;
4019 
4020   // Retrieve the appropriate method list.
4021   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4022                                                     : Known->second.second;
4023   bool Found = false;
4024   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4025     if (!Found) {
4026       if (List->getMethod() == Method) {
4027         Found = true;
4028       } else {
4029         // Keep searching.
4030         continue;
4031       }
4032     }
4033 
4034     if (List->getNext())
4035       List->setMethod(List->getNext()->getMethod());
4036     else
4037       List->setMethod(Method);
4038   }
4039 }
4040 
4041 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4042   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4043   for (Decl *D : Names) {
4044     bool wasHidden = !D->isUnconditionallyVisible();
4045     D->setVisibleDespiteOwningModule();
4046 
4047     if (wasHidden && SemaObj) {
4048       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4049         moveMethodToBackOfGlobalList(*SemaObj, Method);
4050       }
4051     }
4052   }
4053 }
4054 
4055 void ASTReader::makeModuleVisible(Module *Mod,
4056                                   Module::NameVisibilityKind NameVisibility,
4057                                   SourceLocation ImportLoc) {
4058   llvm::SmallPtrSet<Module *, 4> Visited;
4059   SmallVector<Module *, 4> Stack;
4060   Stack.push_back(Mod);
4061   while (!Stack.empty()) {
4062     Mod = Stack.pop_back_val();
4063 
4064     if (NameVisibility <= Mod->NameVisibility) {
4065       // This module already has this level of visibility (or greater), so
4066       // there is nothing more to do.
4067       continue;
4068     }
4069 
4070     if (Mod->isUnimportable()) {
4071       // Modules that aren't importable cannot be made visible.
4072       continue;
4073     }
4074 
4075     // Update the module's name visibility.
4076     Mod->NameVisibility = NameVisibility;
4077 
4078     // If we've already deserialized any names from this module,
4079     // mark them as visible.
4080     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4081     if (Hidden != HiddenNamesMap.end()) {
4082       auto HiddenNames = std::move(*Hidden);
4083       HiddenNamesMap.erase(Hidden);
4084       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4085       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4086              "making names visible added hidden names");
4087     }
4088 
4089     // Push any exported modules onto the stack to be marked as visible.
4090     SmallVector<Module *, 16> Exports;
4091     Mod->getExportedModules(Exports);
4092     for (SmallVectorImpl<Module *>::iterator
4093            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4094       Module *Exported = *I;
4095       if (Visited.insert(Exported).second)
4096         Stack.push_back(Exported);
4097     }
4098   }
4099 }
4100 
4101 /// We've merged the definition \p MergedDef into the existing definition
4102 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4103 /// visible.
4104 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4105                                           NamedDecl *MergedDef) {
4106   if (!Def->isUnconditionallyVisible()) {
4107     // If MergedDef is visible or becomes visible, make the definition visible.
4108     if (MergedDef->isUnconditionallyVisible())
4109       Def->setVisibleDespiteOwningModule();
4110     else {
4111       getContext().mergeDefinitionIntoModule(
4112           Def, MergedDef->getImportedOwningModule(),
4113           /*NotifyListeners*/ false);
4114       PendingMergedDefinitionsToDeduplicate.insert(Def);
4115     }
4116   }
4117 }
4118 
4119 bool ASTReader::loadGlobalIndex() {
4120   if (GlobalIndex)
4121     return false;
4122 
4123   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4124       !PP.getLangOpts().Modules)
4125     return true;
4126 
4127   // Try to load the global index.
4128   TriedLoadingGlobalIndex = true;
4129   StringRef ModuleCachePath
4130     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4131   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4132       GlobalModuleIndex::readIndex(ModuleCachePath);
4133   if (llvm::Error Err = std::move(Result.second)) {
4134     assert(!Result.first);
4135     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4136     return true;
4137   }
4138 
4139   GlobalIndex.reset(Result.first);
4140   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4141   return false;
4142 }
4143 
4144 bool ASTReader::isGlobalIndexUnavailable() const {
4145   return PP.getLangOpts().Modules && UseGlobalIndex &&
4146          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4147 }
4148 
4149 static void updateModuleTimestamp(ModuleFile &MF) {
4150   // Overwrite the timestamp file contents so that file's mtime changes.
4151   std::string TimestampFilename = MF.getTimestampFilename();
4152   std::error_code EC;
4153   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4154                           llvm::sys::fs::OF_TextWithCRLF);
4155   if (EC)
4156     return;
4157   OS << "Timestamp file\n";
4158   OS.close();
4159   OS.clear_error(); // Avoid triggering a fatal error.
4160 }
4161 
4162 /// Given a cursor at the start of an AST file, scan ahead and drop the
4163 /// cursor into the start of the given block ID, returning false on success and
4164 /// true on failure.
4165 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4166   while (true) {
4167     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4168     if (!MaybeEntry) {
4169       // FIXME this drops errors on the floor.
4170       consumeError(MaybeEntry.takeError());
4171       return true;
4172     }
4173     llvm::BitstreamEntry Entry = MaybeEntry.get();
4174 
4175     switch (Entry.Kind) {
4176     case llvm::BitstreamEntry::Error:
4177     case llvm::BitstreamEntry::EndBlock:
4178       return true;
4179 
4180     case llvm::BitstreamEntry::Record:
4181       // Ignore top-level records.
4182       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4183         break;
4184       else {
4185         // FIXME this drops errors on the floor.
4186         consumeError(Skipped.takeError());
4187         return true;
4188       }
4189 
4190     case llvm::BitstreamEntry::SubBlock:
4191       if (Entry.ID == BlockID) {
4192         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4193           // FIXME this drops the error on the floor.
4194           consumeError(std::move(Err));
4195           return true;
4196         }
4197         // Found it!
4198         return false;
4199       }
4200 
4201       if (llvm::Error Err = Cursor.SkipBlock()) {
4202         // FIXME this drops the error on the floor.
4203         consumeError(std::move(Err));
4204         return true;
4205       }
4206     }
4207   }
4208 }
4209 
4210 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4211                                             ModuleKind Type,
4212                                             SourceLocation ImportLoc,
4213                                             unsigned ClientLoadCapabilities,
4214                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4215   llvm::SaveAndRestore<SourceLocation>
4216     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4217   llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4218       CurrentDeserializingModuleKind, Type);
4219 
4220   // Defer any pending actions until we get to the end of reading the AST file.
4221   Deserializing AnASTFile(this);
4222 
4223   // Bump the generation number.
4224   unsigned PreviousGeneration = 0;
4225   if (ContextObj)
4226     PreviousGeneration = incrementGeneration(*ContextObj);
4227 
4228   unsigned NumModules = ModuleMgr.size();
4229   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4230     assert(ReadResult && "expected to return error");
4231     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4232                             PP.getLangOpts().Modules
4233                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4234                                 : nullptr);
4235 
4236     // If we find that any modules are unusable, the global index is going
4237     // to be out-of-date. Just remove it.
4238     GlobalIndex.reset();
4239     ModuleMgr.setGlobalIndex(nullptr);
4240     return ReadResult;
4241   };
4242 
4243   SmallVector<ImportedModule, 4> Loaded;
4244   switch (ASTReadResult ReadResult =
4245               ReadASTCore(FileName, Type, ImportLoc,
4246                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4247                           ASTFileSignature(), ClientLoadCapabilities)) {
4248   case Failure:
4249   case Missing:
4250   case OutOfDate:
4251   case VersionMismatch:
4252   case ConfigurationMismatch:
4253   case HadErrors:
4254     return removeModulesAndReturn(ReadResult);
4255   case Success:
4256     break;
4257   }
4258 
4259   // Here comes stuff that we only do once the entire chain is loaded.
4260 
4261   // Load the AST blocks of all of the modules that we loaded.  We can still
4262   // hit errors parsing the ASTs at this point.
4263   for (ImportedModule &M : Loaded) {
4264     ModuleFile &F = *M.Mod;
4265 
4266     // Read the AST block.
4267     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4268       return removeModulesAndReturn(Result);
4269 
4270     // The AST block should always have a definition for the main module.
4271     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4272       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4273       return removeModulesAndReturn(Failure);
4274     }
4275 
4276     // Read the extension blocks.
4277     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4278       if (ASTReadResult Result = ReadExtensionBlock(F))
4279         return removeModulesAndReturn(Result);
4280     }
4281 
4282     // Once read, set the ModuleFile bit base offset and update the size in
4283     // bits of all files we've seen.
4284     F.GlobalBitOffset = TotalModulesSizeInBits;
4285     TotalModulesSizeInBits += F.SizeInBits;
4286     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4287   }
4288 
4289   // Preload source locations and interesting indentifiers.
4290   for (ImportedModule &M : Loaded) {
4291     ModuleFile &F = *M.Mod;
4292 
4293     // Preload SLocEntries.
4294     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4295       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4296       // Load it through the SourceManager and don't call ReadSLocEntry()
4297       // directly because the entry may have already been loaded in which case
4298       // calling ReadSLocEntry() directly would trigger an assertion in
4299       // SourceManager.
4300       SourceMgr.getLoadedSLocEntryByID(Index);
4301     }
4302 
4303     // Map the original source file ID into the ID space of the current
4304     // compilation.
4305     if (F.OriginalSourceFileID.isValid()) {
4306       F.OriginalSourceFileID = FileID::get(
4307           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4308     }
4309 
4310     // Preload all the pending interesting identifiers by marking them out of
4311     // date.
4312     for (auto Offset : F.PreloadIdentifierOffsets) {
4313       const unsigned char *Data = F.IdentifierTableData + Offset;
4314 
4315       ASTIdentifierLookupTrait Trait(*this, F);
4316       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4317       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4318       auto &II = PP.getIdentifierTable().getOwn(Key);
4319       II.setOutOfDate(true);
4320 
4321       // Mark this identifier as being from an AST file so that we can track
4322       // whether we need to serialize it.
4323       markIdentifierFromAST(*this, II);
4324 
4325       // Associate the ID with the identifier so that the writer can reuse it.
4326       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4327       SetIdentifierInfo(ID, &II);
4328     }
4329   }
4330 
4331   // Setup the import locations and notify the module manager that we've
4332   // committed to these module files.
4333   for (ImportedModule &M : Loaded) {
4334     ModuleFile &F = *M.Mod;
4335 
4336     ModuleMgr.moduleFileAccepted(&F);
4337 
4338     // Set the import location.
4339     F.DirectImportLoc = ImportLoc;
4340     // FIXME: We assume that locations from PCH / preamble do not need
4341     // any translation.
4342     if (!M.ImportedBy)
4343       F.ImportLoc = M.ImportLoc;
4344     else
4345       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4346   }
4347 
4348   if (!PP.getLangOpts().CPlusPlus ||
4349       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4350        Type != MK_PrebuiltModule)) {
4351     // Mark all of the identifiers in the identifier table as being out of date,
4352     // so that various accessors know to check the loaded modules when the
4353     // identifier is used.
4354     //
4355     // For C++ modules, we don't need information on many identifiers (just
4356     // those that provide macros or are poisoned), so we mark all of
4357     // the interesting ones via PreloadIdentifierOffsets.
4358     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4359                                 IdEnd = PP.getIdentifierTable().end();
4360          Id != IdEnd; ++Id)
4361       Id->second->setOutOfDate(true);
4362   }
4363   // Mark selectors as out of date.
4364   for (auto Sel : SelectorGeneration)
4365     SelectorOutOfDate[Sel.first] = true;
4366 
4367   // Resolve any unresolved module exports.
4368   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4369     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4370     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4371     Module *ResolvedMod = getSubmodule(GlobalID);
4372 
4373     switch (Unresolved.Kind) {
4374     case UnresolvedModuleRef::Conflict:
4375       if (ResolvedMod) {
4376         Module::Conflict Conflict;
4377         Conflict.Other = ResolvedMod;
4378         Conflict.Message = Unresolved.String.str();
4379         Unresolved.Mod->Conflicts.push_back(Conflict);
4380       }
4381       continue;
4382 
4383     case UnresolvedModuleRef::Import:
4384       if (ResolvedMod)
4385         Unresolved.Mod->Imports.insert(ResolvedMod);
4386       continue;
4387 
4388     case UnresolvedModuleRef::Export:
4389       if (ResolvedMod || Unresolved.IsWildcard)
4390         Unresolved.Mod->Exports.push_back(
4391           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4392       continue;
4393     }
4394   }
4395   UnresolvedModuleRefs.clear();
4396 
4397   if (Imported)
4398     Imported->append(ImportedModules.begin(),
4399                      ImportedModules.end());
4400 
4401   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4402   // Might be unnecessary as use declarations are only used to build the
4403   // module itself.
4404 
4405   if (ContextObj)
4406     InitializeContext();
4407 
4408   if (SemaObj)
4409     UpdateSema();
4410 
4411   if (DeserializationListener)
4412     DeserializationListener->ReaderInitialized(this);
4413 
4414   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4415   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4416     // If this AST file is a precompiled preamble, then set the
4417     // preamble file ID of the source manager to the file source file
4418     // from which the preamble was built.
4419     if (Type == MK_Preamble) {
4420       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4421     } else if (Type == MK_MainFile) {
4422       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4423     }
4424   }
4425 
4426   // For any Objective-C class definitions we have already loaded, make sure
4427   // that we load any additional categories.
4428   if (ContextObj) {
4429     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4430       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4431                          ObjCClassesLoaded[I],
4432                          PreviousGeneration);
4433     }
4434   }
4435 
4436   if (PP.getHeaderSearchInfo()
4437           .getHeaderSearchOpts()
4438           .ModulesValidateOncePerBuildSession) {
4439     // Now we are certain that the module and all modules it depends on are
4440     // up to date.  Create or update timestamp files for modules that are
4441     // located in the module cache (not for PCH files that could be anywhere
4442     // in the filesystem).
4443     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4444       ImportedModule &M = Loaded[I];
4445       if (M.Mod->Kind == MK_ImplicitModule) {
4446         updateModuleTimestamp(*M.Mod);
4447       }
4448     }
4449   }
4450 
4451   return Success;
4452 }
4453 
4454 static ASTFileSignature readASTFileSignature(StringRef PCH);
4455 
4456 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4457 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4458   // FIXME checking magic headers is done in other places such as
4459   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4460   // always done the same. Unify it all with a helper.
4461   if (!Stream.canSkipToPos(4))
4462     return llvm::createStringError(std::errc::illegal_byte_sequence,
4463                                    "file too small to contain AST file magic");
4464   for (unsigned C : {'C', 'P', 'C', 'H'})
4465     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4466       if (Res.get() != C)
4467         return llvm::createStringError(
4468             std::errc::illegal_byte_sequence,
4469             "file doesn't start with AST file magic");
4470     } else
4471       return Res.takeError();
4472   return llvm::Error::success();
4473 }
4474 
4475 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4476   switch (Kind) {
4477   case MK_PCH:
4478     return 0; // PCH
4479   case MK_ImplicitModule:
4480   case MK_ExplicitModule:
4481   case MK_PrebuiltModule:
4482     return 1; // module
4483   case MK_MainFile:
4484   case MK_Preamble:
4485     return 2; // main source file
4486   }
4487   llvm_unreachable("unknown module kind");
4488 }
4489 
4490 ASTReader::ASTReadResult
4491 ASTReader::ReadASTCore(StringRef FileName,
4492                        ModuleKind Type,
4493                        SourceLocation ImportLoc,
4494                        ModuleFile *ImportedBy,
4495                        SmallVectorImpl<ImportedModule> &Loaded,
4496                        off_t ExpectedSize, time_t ExpectedModTime,
4497                        ASTFileSignature ExpectedSignature,
4498                        unsigned ClientLoadCapabilities) {
4499   ModuleFile *M;
4500   std::string ErrorStr;
4501   ModuleManager::AddModuleResult AddResult
4502     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4503                           getGeneration(), ExpectedSize, ExpectedModTime,
4504                           ExpectedSignature, readASTFileSignature,
4505                           M, ErrorStr);
4506 
4507   switch (AddResult) {
4508   case ModuleManager::AlreadyLoaded:
4509     Diag(diag::remark_module_import)
4510         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4511         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4512     return Success;
4513 
4514   case ModuleManager::NewlyLoaded:
4515     // Load module file below.
4516     break;
4517 
4518   case ModuleManager::Missing:
4519     // The module file was missing; if the client can handle that, return
4520     // it.
4521     if (ClientLoadCapabilities & ARR_Missing)
4522       return Missing;
4523 
4524     // Otherwise, return an error.
4525     Diag(diag::err_ast_file_not_found)
4526         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4527         << ErrorStr;
4528     return Failure;
4529 
4530   case ModuleManager::OutOfDate:
4531     // We couldn't load the module file because it is out-of-date. If the
4532     // client can handle out-of-date, return it.
4533     if (ClientLoadCapabilities & ARR_OutOfDate)
4534       return OutOfDate;
4535 
4536     // Otherwise, return an error.
4537     Diag(diag::err_ast_file_out_of_date)
4538         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4539         << ErrorStr;
4540     return Failure;
4541   }
4542 
4543   assert(M && "Missing module file");
4544 
4545   bool ShouldFinalizePCM = false;
4546   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4547     auto &MC = getModuleManager().getModuleCache();
4548     if (ShouldFinalizePCM)
4549       MC.finalizePCM(FileName);
4550     else
4551       MC.tryToDropPCM(FileName);
4552   });
4553   ModuleFile &F = *M;
4554   BitstreamCursor &Stream = F.Stream;
4555   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4556   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4557 
4558   // Sniff for the signature.
4559   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4560     Diag(diag::err_ast_file_invalid)
4561         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4562     return Failure;
4563   }
4564 
4565   // This is used for compatibility with older PCH formats.
4566   bool HaveReadControlBlock = false;
4567   while (true) {
4568     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4569     if (!MaybeEntry) {
4570       Error(MaybeEntry.takeError());
4571       return Failure;
4572     }
4573     llvm::BitstreamEntry Entry = MaybeEntry.get();
4574 
4575     switch (Entry.Kind) {
4576     case llvm::BitstreamEntry::Error:
4577     case llvm::BitstreamEntry::Record:
4578     case llvm::BitstreamEntry::EndBlock:
4579       Error("invalid record at top-level of AST file");
4580       return Failure;
4581 
4582     case llvm::BitstreamEntry::SubBlock:
4583       break;
4584     }
4585 
4586     switch (Entry.ID) {
4587     case CONTROL_BLOCK_ID:
4588       HaveReadControlBlock = true;
4589       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4590       case Success:
4591         // Check that we didn't try to load a non-module AST file as a module.
4592         //
4593         // FIXME: Should we also perform the converse check? Loading a module as
4594         // a PCH file sort of works, but it's a bit wonky.
4595         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4596              Type == MK_PrebuiltModule) &&
4597             F.ModuleName.empty()) {
4598           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4599           if (Result != OutOfDate ||
4600               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4601             Diag(diag::err_module_file_not_module) << FileName;
4602           return Result;
4603         }
4604         break;
4605 
4606       case Failure: return Failure;
4607       case Missing: return Missing;
4608       case OutOfDate: return OutOfDate;
4609       case VersionMismatch: return VersionMismatch;
4610       case ConfigurationMismatch: return ConfigurationMismatch;
4611       case HadErrors: return HadErrors;
4612       }
4613       break;
4614 
4615     case AST_BLOCK_ID:
4616       if (!HaveReadControlBlock) {
4617         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4618           Diag(diag::err_pch_version_too_old);
4619         return VersionMismatch;
4620       }
4621 
4622       // Record that we've loaded this module.
4623       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4624       ShouldFinalizePCM = true;
4625       return Success;
4626 
4627     case UNHASHED_CONTROL_BLOCK_ID:
4628       // This block is handled using look-ahead during ReadControlBlock.  We
4629       // shouldn't get here!
4630       Error("malformed block record in AST file");
4631       return Failure;
4632 
4633     default:
4634       if (llvm::Error Err = Stream.SkipBlock()) {
4635         Error(std::move(Err));
4636         return Failure;
4637       }
4638       break;
4639     }
4640   }
4641 
4642   llvm_unreachable("unexpected break; expected return");
4643 }
4644 
4645 ASTReader::ASTReadResult
4646 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4647                                     unsigned ClientLoadCapabilities) {
4648   const HeaderSearchOptions &HSOpts =
4649       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4650   bool AllowCompatibleConfigurationMismatch =
4651       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4652   bool DisableValidation = shouldDisableValidationForFile(F);
4653 
4654   ASTReadResult Result = readUnhashedControlBlockImpl(
4655       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4656       Listener.get(),
4657       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4658 
4659   // If F was directly imported by another module, it's implicitly validated by
4660   // the importing module.
4661   if (DisableValidation || WasImportedBy ||
4662       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4663     return Success;
4664 
4665   if (Result == Failure) {
4666     Error("malformed block record in AST file");
4667     return Failure;
4668   }
4669 
4670   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4671     // If this module has already been finalized in the ModuleCache, we're stuck
4672     // with it; we can only load a single version of each module.
4673     //
4674     // This can happen when a module is imported in two contexts: in one, as a
4675     // user module; in another, as a system module (due to an import from
4676     // another module marked with the [system] flag).  It usually indicates a
4677     // bug in the module map: this module should also be marked with [system].
4678     //
4679     // If -Wno-system-headers (the default), and the first import is as a
4680     // system module, then validation will fail during the as-user import,
4681     // since -Werror flags won't have been validated.  However, it's reasonable
4682     // to treat this consistently as a system module.
4683     //
4684     // If -Wsystem-headers, the PCM on disk was built with
4685     // -Wno-system-headers, and the first import is as a user module, then
4686     // validation will fail during the as-system import since the PCM on disk
4687     // doesn't guarantee that -Werror was respected.  However, the -Werror
4688     // flags were checked during the initial as-user import.
4689     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4690       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4691       return Success;
4692     }
4693   }
4694 
4695   return Result;
4696 }
4697 
4698 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4699     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4700     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4701     bool ValidateDiagnosticOptions) {
4702   // Initialize a stream.
4703   BitstreamCursor Stream(StreamData);
4704 
4705   // Sniff for the signature.
4706   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4707     // FIXME this drops the error on the floor.
4708     consumeError(std::move(Err));
4709     return Failure;
4710   }
4711 
4712   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4713   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4714     return Failure;
4715 
4716   // Read all of the records in the options block.
4717   RecordData Record;
4718   ASTReadResult Result = Success;
4719   while (true) {
4720     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4721     if (!MaybeEntry) {
4722       // FIXME this drops the error on the floor.
4723       consumeError(MaybeEntry.takeError());
4724       return Failure;
4725     }
4726     llvm::BitstreamEntry Entry = MaybeEntry.get();
4727 
4728     switch (Entry.Kind) {
4729     case llvm::BitstreamEntry::Error:
4730     case llvm::BitstreamEntry::SubBlock:
4731       return Failure;
4732 
4733     case llvm::BitstreamEntry::EndBlock:
4734       return Result;
4735 
4736     case llvm::BitstreamEntry::Record:
4737       // The interesting case.
4738       break;
4739     }
4740 
4741     // Read and process a record.
4742     Record.clear();
4743     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4744     if (!MaybeRecordType) {
4745       // FIXME this drops the error.
4746       return Failure;
4747     }
4748     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4749     case SIGNATURE:
4750       if (F)
4751         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4752       break;
4753     case AST_BLOCK_HASH:
4754       if (F)
4755         F->ASTBlockHash =
4756             ASTFileSignature::create(Record.begin(), Record.end());
4757       break;
4758     case DIAGNOSTIC_OPTIONS: {
4759       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4760       if (Listener && ValidateDiagnosticOptions &&
4761           !AllowCompatibleConfigurationMismatch &&
4762           ParseDiagnosticOptions(Record, Complain, *Listener))
4763         Result = OutOfDate; // Don't return early.  Read the signature.
4764       break;
4765     }
4766     case DIAG_PRAGMA_MAPPINGS:
4767       if (!F)
4768         break;
4769       if (F->PragmaDiagMappings.empty())
4770         F->PragmaDiagMappings.swap(Record);
4771       else
4772         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4773                                      Record.begin(), Record.end());
4774       break;
4775     }
4776   }
4777 }
4778 
4779 /// Parse a record and blob containing module file extension metadata.
4780 static bool parseModuleFileExtensionMetadata(
4781               const SmallVectorImpl<uint64_t> &Record,
4782               StringRef Blob,
4783               ModuleFileExtensionMetadata &Metadata) {
4784   if (Record.size() < 4) return true;
4785 
4786   Metadata.MajorVersion = Record[0];
4787   Metadata.MinorVersion = Record[1];
4788 
4789   unsigned BlockNameLen = Record[2];
4790   unsigned UserInfoLen = Record[3];
4791 
4792   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4793 
4794   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4795   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4796                                   Blob.data() + BlockNameLen + UserInfoLen);
4797   return false;
4798 }
4799 
4800 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4801   BitstreamCursor &Stream = F.Stream;
4802 
4803   RecordData Record;
4804   while (true) {
4805     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4806     if (!MaybeEntry) {
4807       Error(MaybeEntry.takeError());
4808       return Failure;
4809     }
4810     llvm::BitstreamEntry Entry = MaybeEntry.get();
4811 
4812     switch (Entry.Kind) {
4813     case llvm::BitstreamEntry::SubBlock:
4814       if (llvm::Error Err = Stream.SkipBlock()) {
4815         Error(std::move(Err));
4816         return Failure;
4817       }
4818       continue;
4819 
4820     case llvm::BitstreamEntry::EndBlock:
4821       return Success;
4822 
4823     case llvm::BitstreamEntry::Error:
4824       return HadErrors;
4825 
4826     case llvm::BitstreamEntry::Record:
4827       break;
4828     }
4829 
4830     Record.clear();
4831     StringRef Blob;
4832     Expected<unsigned> MaybeRecCode =
4833         Stream.readRecord(Entry.ID, Record, &Blob);
4834     if (!MaybeRecCode) {
4835       Error(MaybeRecCode.takeError());
4836       return Failure;
4837     }
4838     switch (MaybeRecCode.get()) {
4839     case EXTENSION_METADATA: {
4840       ModuleFileExtensionMetadata Metadata;
4841       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4842         Error("malformed EXTENSION_METADATA in AST file");
4843         return Failure;
4844       }
4845 
4846       // Find a module file extension with this block name.
4847       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4848       if (Known == ModuleFileExtensions.end()) break;
4849 
4850       // Form a reader.
4851       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4852                                                              F, Stream)) {
4853         F.ExtensionReaders.push_back(std::move(Reader));
4854       }
4855 
4856       break;
4857     }
4858     }
4859   }
4860 
4861   return Success;
4862 }
4863 
4864 void ASTReader::InitializeContext() {
4865   assert(ContextObj && "no context to initialize");
4866   ASTContext &Context = *ContextObj;
4867 
4868   // If there's a listener, notify them that we "read" the translation unit.
4869   if (DeserializationListener)
4870     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4871                                       Context.getTranslationUnitDecl());
4872 
4873   // FIXME: Find a better way to deal with collisions between these
4874   // built-in types. Right now, we just ignore the problem.
4875 
4876   // Load the special types.
4877   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4878     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4879       if (!Context.CFConstantStringTypeDecl)
4880         Context.setCFConstantStringType(GetType(String));
4881     }
4882 
4883     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4884       QualType FileType = GetType(File);
4885       if (FileType.isNull()) {
4886         Error("FILE type is NULL");
4887         return;
4888       }
4889 
4890       if (!Context.FILEDecl) {
4891         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4892           Context.setFILEDecl(Typedef->getDecl());
4893         else {
4894           const TagType *Tag = FileType->getAs<TagType>();
4895           if (!Tag) {
4896             Error("Invalid FILE type in AST file");
4897             return;
4898           }
4899           Context.setFILEDecl(Tag->getDecl());
4900         }
4901       }
4902     }
4903 
4904     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4905       QualType Jmp_bufType = GetType(Jmp_buf);
4906       if (Jmp_bufType.isNull()) {
4907         Error("jmp_buf type is NULL");
4908         return;
4909       }
4910 
4911       if (!Context.jmp_bufDecl) {
4912         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4913           Context.setjmp_bufDecl(Typedef->getDecl());
4914         else {
4915           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4916           if (!Tag) {
4917             Error("Invalid jmp_buf type in AST file");
4918             return;
4919           }
4920           Context.setjmp_bufDecl(Tag->getDecl());
4921         }
4922       }
4923     }
4924 
4925     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4926       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4927       if (Sigjmp_bufType.isNull()) {
4928         Error("sigjmp_buf type is NULL");
4929         return;
4930       }
4931 
4932       if (!Context.sigjmp_bufDecl) {
4933         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4934           Context.setsigjmp_bufDecl(Typedef->getDecl());
4935         else {
4936           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4937           assert(Tag && "Invalid sigjmp_buf type in AST file");
4938           Context.setsigjmp_bufDecl(Tag->getDecl());
4939         }
4940       }
4941     }
4942 
4943     if (unsigned ObjCIdRedef
4944           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4945       if (Context.ObjCIdRedefinitionType.isNull())
4946         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4947     }
4948 
4949     if (unsigned ObjCClassRedef
4950           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4951       if (Context.ObjCClassRedefinitionType.isNull())
4952         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4953     }
4954 
4955     if (unsigned ObjCSelRedef
4956           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4957       if (Context.ObjCSelRedefinitionType.isNull())
4958         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4959     }
4960 
4961     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4962       QualType Ucontext_tType = GetType(Ucontext_t);
4963       if (Ucontext_tType.isNull()) {
4964         Error("ucontext_t type is NULL");
4965         return;
4966       }
4967 
4968       if (!Context.ucontext_tDecl) {
4969         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4970           Context.setucontext_tDecl(Typedef->getDecl());
4971         else {
4972           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4973           assert(Tag && "Invalid ucontext_t type in AST file");
4974           Context.setucontext_tDecl(Tag->getDecl());
4975         }
4976       }
4977     }
4978   }
4979 
4980   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4981 
4982   // If there were any CUDA special declarations, deserialize them.
4983   if (!CUDASpecialDeclRefs.empty()) {
4984     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4985     Context.setcudaConfigureCallDecl(
4986                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4987   }
4988 
4989   // Re-export any modules that were imported by a non-module AST file.
4990   // FIXME: This does not make macro-only imports visible again.
4991   for (auto &Import : ImportedModules) {
4992     if (Module *Imported = getSubmodule(Import.ID)) {
4993       makeModuleVisible(Imported, Module::AllVisible,
4994                         /*ImportLoc=*/Import.ImportLoc);
4995       if (Import.ImportLoc.isValid())
4996         PP.makeModuleVisible(Imported, Import.ImportLoc);
4997       // This updates visibility for Preprocessor only. For Sema, which can be
4998       // nullptr here, we do the same later, in UpdateSema().
4999     }
5000   }
5001 }
5002 
5003 void ASTReader::finalizeForWriting() {
5004   // Nothing to do for now.
5005 }
5006 
5007 /// Reads and return the signature record from \p PCH's control block, or
5008 /// else returns 0.
5009 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5010   BitstreamCursor Stream(PCH);
5011   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5012     // FIXME this drops the error on the floor.
5013     consumeError(std::move(Err));
5014     return ASTFileSignature();
5015   }
5016 
5017   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5018   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5019     return ASTFileSignature();
5020 
5021   // Scan for SIGNATURE inside the diagnostic options block.
5022   ASTReader::RecordData Record;
5023   while (true) {
5024     Expected<llvm::BitstreamEntry> MaybeEntry =
5025         Stream.advanceSkippingSubblocks();
5026     if (!MaybeEntry) {
5027       // FIXME this drops the error on the floor.
5028       consumeError(MaybeEntry.takeError());
5029       return ASTFileSignature();
5030     }
5031     llvm::BitstreamEntry Entry = MaybeEntry.get();
5032 
5033     if (Entry.Kind != llvm::BitstreamEntry::Record)
5034       return ASTFileSignature();
5035 
5036     Record.clear();
5037     StringRef Blob;
5038     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5039     if (!MaybeRecord) {
5040       // FIXME this drops the error on the floor.
5041       consumeError(MaybeRecord.takeError());
5042       return ASTFileSignature();
5043     }
5044     if (SIGNATURE == MaybeRecord.get())
5045       return ASTFileSignature::create(Record.begin(),
5046                                       Record.begin() + ASTFileSignature::size);
5047   }
5048 }
5049 
5050 /// Retrieve the name of the original source file name
5051 /// directly from the AST file, without actually loading the AST
5052 /// file.
5053 std::string ASTReader::getOriginalSourceFile(
5054     const std::string &ASTFileName, FileManager &FileMgr,
5055     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5056   // Open the AST file.
5057   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5058   if (!Buffer) {
5059     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5060         << ASTFileName << Buffer.getError().message();
5061     return std::string();
5062   }
5063 
5064   // Initialize the stream
5065   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5066 
5067   // Sniff for the signature.
5068   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5069     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5070     return std::string();
5071   }
5072 
5073   // Scan for the CONTROL_BLOCK_ID block.
5074   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5075     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5076     return std::string();
5077   }
5078 
5079   // Scan for ORIGINAL_FILE inside the control block.
5080   RecordData Record;
5081   while (true) {
5082     Expected<llvm::BitstreamEntry> MaybeEntry =
5083         Stream.advanceSkippingSubblocks();
5084     if (!MaybeEntry) {
5085       // FIXME this drops errors on the floor.
5086       consumeError(MaybeEntry.takeError());
5087       return std::string();
5088     }
5089     llvm::BitstreamEntry Entry = MaybeEntry.get();
5090 
5091     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5092       return std::string();
5093 
5094     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5095       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5096       return std::string();
5097     }
5098 
5099     Record.clear();
5100     StringRef Blob;
5101     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5102     if (!MaybeRecord) {
5103       // FIXME this drops the errors on the floor.
5104       consumeError(MaybeRecord.takeError());
5105       return std::string();
5106     }
5107     if (ORIGINAL_FILE == MaybeRecord.get())
5108       return Blob.str();
5109   }
5110 }
5111 
5112 namespace {
5113 
5114   class SimplePCHValidator : public ASTReaderListener {
5115     const LangOptions &ExistingLangOpts;
5116     const TargetOptions &ExistingTargetOpts;
5117     const PreprocessorOptions &ExistingPPOpts;
5118     std::string ExistingModuleCachePath;
5119     FileManager &FileMgr;
5120 
5121   public:
5122     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5123                        const TargetOptions &ExistingTargetOpts,
5124                        const PreprocessorOptions &ExistingPPOpts,
5125                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5126         : ExistingLangOpts(ExistingLangOpts),
5127           ExistingTargetOpts(ExistingTargetOpts),
5128           ExistingPPOpts(ExistingPPOpts),
5129           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5130 
5131     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5132                              bool AllowCompatibleDifferences) override {
5133       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5134                                   AllowCompatibleDifferences);
5135     }
5136 
5137     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5138                            bool AllowCompatibleDifferences) override {
5139       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5140                                 AllowCompatibleDifferences);
5141     }
5142 
5143     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5144                                  StringRef SpecificModuleCachePath,
5145                                  bool Complain) override {
5146       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5147                                       ExistingModuleCachePath, nullptr,
5148                                       ExistingLangOpts, ExistingPPOpts);
5149     }
5150 
5151     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5152                                  bool Complain,
5153                                  std::string &SuggestedPredefines) override {
5154       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5155                                       SuggestedPredefines, ExistingLangOpts);
5156     }
5157   };
5158 
5159 } // namespace
5160 
5161 bool ASTReader::readASTFileControlBlock(
5162     StringRef Filename, FileManager &FileMgr,
5163     const PCHContainerReader &PCHContainerRdr,
5164     bool FindModuleFileExtensions,
5165     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5166   // Open the AST file.
5167   // FIXME: This allows use of the VFS; we do not allow use of the
5168   // VFS when actually loading a module.
5169   auto Buffer = FileMgr.getBufferForFile(Filename);
5170   if (!Buffer) {
5171     return true;
5172   }
5173 
5174   // Initialize the stream
5175   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5176   BitstreamCursor Stream(Bytes);
5177 
5178   // Sniff for the signature.
5179   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5180     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5181     return true;
5182   }
5183 
5184   // Scan for the CONTROL_BLOCK_ID block.
5185   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5186     return true;
5187 
5188   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5189   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5190   bool NeedsImports = Listener.needsImportVisitation();
5191   BitstreamCursor InputFilesCursor;
5192 
5193   RecordData Record;
5194   std::string ModuleDir;
5195   bool DoneWithControlBlock = false;
5196   while (!DoneWithControlBlock) {
5197     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5198     if (!MaybeEntry) {
5199       // FIXME this drops the error on the floor.
5200       consumeError(MaybeEntry.takeError());
5201       return true;
5202     }
5203     llvm::BitstreamEntry Entry = MaybeEntry.get();
5204 
5205     switch (Entry.Kind) {
5206     case llvm::BitstreamEntry::SubBlock: {
5207       switch (Entry.ID) {
5208       case OPTIONS_BLOCK_ID: {
5209         std::string IgnoredSuggestedPredefines;
5210         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5211                              /*AllowCompatibleConfigurationMismatch*/ false,
5212                              Listener, IgnoredSuggestedPredefines) != Success)
5213           return true;
5214         break;
5215       }
5216 
5217       case INPUT_FILES_BLOCK_ID:
5218         InputFilesCursor = Stream;
5219         if (llvm::Error Err = Stream.SkipBlock()) {
5220           // FIXME this drops the error on the floor.
5221           consumeError(std::move(Err));
5222           return true;
5223         }
5224         if (NeedsInputFiles &&
5225             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5226           return true;
5227         break;
5228 
5229       default:
5230         if (llvm::Error Err = Stream.SkipBlock()) {
5231           // FIXME this drops the error on the floor.
5232           consumeError(std::move(Err));
5233           return true;
5234         }
5235         break;
5236       }
5237 
5238       continue;
5239     }
5240 
5241     case llvm::BitstreamEntry::EndBlock:
5242       DoneWithControlBlock = true;
5243       break;
5244 
5245     case llvm::BitstreamEntry::Error:
5246       return true;
5247 
5248     case llvm::BitstreamEntry::Record:
5249       break;
5250     }
5251 
5252     if (DoneWithControlBlock) break;
5253 
5254     Record.clear();
5255     StringRef Blob;
5256     Expected<unsigned> MaybeRecCode =
5257         Stream.readRecord(Entry.ID, Record, &Blob);
5258     if (!MaybeRecCode) {
5259       // FIXME this drops the error.
5260       return Failure;
5261     }
5262     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5263     case METADATA:
5264       if (Record[0] != VERSION_MAJOR)
5265         return true;
5266       if (Listener.ReadFullVersionInformation(Blob))
5267         return true;
5268       break;
5269     case MODULE_NAME:
5270       Listener.ReadModuleName(Blob);
5271       break;
5272     case MODULE_DIRECTORY:
5273       ModuleDir = std::string(Blob);
5274       break;
5275     case MODULE_MAP_FILE: {
5276       unsigned Idx = 0;
5277       auto Path = ReadString(Record, Idx);
5278       ResolveImportedPath(Path, ModuleDir);
5279       Listener.ReadModuleMapFile(Path);
5280       break;
5281     }
5282     case INPUT_FILE_OFFSETS: {
5283       if (!NeedsInputFiles)
5284         break;
5285 
5286       unsigned NumInputFiles = Record[0];
5287       unsigned NumUserFiles = Record[1];
5288       const llvm::support::unaligned_uint64_t *InputFileOffs =
5289           (const llvm::support::unaligned_uint64_t *)Blob.data();
5290       for (unsigned I = 0; I != NumInputFiles; ++I) {
5291         // Go find this input file.
5292         bool isSystemFile = I >= NumUserFiles;
5293 
5294         if (isSystemFile && !NeedsSystemInputFiles)
5295           break; // the rest are system input files
5296 
5297         BitstreamCursor &Cursor = InputFilesCursor;
5298         SavedStreamPosition SavedPosition(Cursor);
5299         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5300           // FIXME this drops errors on the floor.
5301           consumeError(std::move(Err));
5302         }
5303 
5304         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5305         if (!MaybeCode) {
5306           // FIXME this drops errors on the floor.
5307           consumeError(MaybeCode.takeError());
5308         }
5309         unsigned Code = MaybeCode.get();
5310 
5311         RecordData Record;
5312         StringRef Blob;
5313         bool shouldContinue = false;
5314         Expected<unsigned> MaybeRecordType =
5315             Cursor.readRecord(Code, Record, &Blob);
5316         if (!MaybeRecordType) {
5317           // FIXME this drops errors on the floor.
5318           consumeError(MaybeRecordType.takeError());
5319         }
5320         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5321         case INPUT_FILE_HASH:
5322           break;
5323         case INPUT_FILE:
5324           bool Overridden = static_cast<bool>(Record[3]);
5325           std::string Filename = std::string(Blob);
5326           ResolveImportedPath(Filename, ModuleDir);
5327           shouldContinue = Listener.visitInputFile(
5328               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5329           break;
5330         }
5331         if (!shouldContinue)
5332           break;
5333       }
5334       break;
5335     }
5336 
5337     case IMPORTS: {
5338       if (!NeedsImports)
5339         break;
5340 
5341       unsigned Idx = 0, N = Record.size();
5342       while (Idx < N) {
5343         // Read information about the AST file.
5344         Idx +=
5345             1 + 1 + 1 + 1 +
5346             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5347         std::string ModuleName = ReadString(Record, Idx);
5348         std::string Filename = ReadString(Record, Idx);
5349         ResolveImportedPath(Filename, ModuleDir);
5350         Listener.visitImport(ModuleName, Filename);
5351       }
5352       break;
5353     }
5354 
5355     default:
5356       // No other validation to perform.
5357       break;
5358     }
5359   }
5360 
5361   // Look for module file extension blocks, if requested.
5362   if (FindModuleFileExtensions) {
5363     BitstreamCursor SavedStream = Stream;
5364     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5365       bool DoneWithExtensionBlock = false;
5366       while (!DoneWithExtensionBlock) {
5367         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5368         if (!MaybeEntry) {
5369           // FIXME this drops the error.
5370           return true;
5371         }
5372         llvm::BitstreamEntry Entry = MaybeEntry.get();
5373 
5374         switch (Entry.Kind) {
5375         case llvm::BitstreamEntry::SubBlock:
5376           if (llvm::Error Err = Stream.SkipBlock()) {
5377             // FIXME this drops the error on the floor.
5378             consumeError(std::move(Err));
5379             return true;
5380           }
5381           continue;
5382 
5383         case llvm::BitstreamEntry::EndBlock:
5384           DoneWithExtensionBlock = true;
5385           continue;
5386 
5387         case llvm::BitstreamEntry::Error:
5388           return true;
5389 
5390         case llvm::BitstreamEntry::Record:
5391           break;
5392         }
5393 
5394        Record.clear();
5395        StringRef Blob;
5396        Expected<unsigned> MaybeRecCode =
5397            Stream.readRecord(Entry.ID, Record, &Blob);
5398        if (!MaybeRecCode) {
5399          // FIXME this drops the error.
5400          return true;
5401        }
5402        switch (MaybeRecCode.get()) {
5403        case EXTENSION_METADATA: {
5404          ModuleFileExtensionMetadata Metadata;
5405          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5406            return true;
5407 
5408          Listener.readModuleFileExtension(Metadata);
5409          break;
5410        }
5411        }
5412       }
5413     }
5414     Stream = SavedStream;
5415   }
5416 
5417   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5418   if (readUnhashedControlBlockImpl(
5419           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5420           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5421           ValidateDiagnosticOptions) != Success)
5422     return true;
5423 
5424   return false;
5425 }
5426 
5427 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5428                                     const PCHContainerReader &PCHContainerRdr,
5429                                     const LangOptions &LangOpts,
5430                                     const TargetOptions &TargetOpts,
5431                                     const PreprocessorOptions &PPOpts,
5432                                     StringRef ExistingModuleCachePath) {
5433   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5434                                ExistingModuleCachePath, FileMgr);
5435   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5436                                   /*FindModuleFileExtensions=*/false,
5437                                   validator,
5438                                   /*ValidateDiagnosticOptions=*/true);
5439 }
5440 
5441 ASTReader::ASTReadResult
5442 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5443   // Enter the submodule block.
5444   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5445     Error(std::move(Err));
5446     return Failure;
5447   }
5448 
5449   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5450   bool First = true;
5451   Module *CurrentModule = nullptr;
5452   RecordData Record;
5453   while (true) {
5454     Expected<llvm::BitstreamEntry> MaybeEntry =
5455         F.Stream.advanceSkippingSubblocks();
5456     if (!MaybeEntry) {
5457       Error(MaybeEntry.takeError());
5458       return Failure;
5459     }
5460     llvm::BitstreamEntry Entry = MaybeEntry.get();
5461 
5462     switch (Entry.Kind) {
5463     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5464     case llvm::BitstreamEntry::Error:
5465       Error("malformed block record in AST file");
5466       return Failure;
5467     case llvm::BitstreamEntry::EndBlock:
5468       return Success;
5469     case llvm::BitstreamEntry::Record:
5470       // The interesting case.
5471       break;
5472     }
5473 
5474     // Read a record.
5475     StringRef Blob;
5476     Record.clear();
5477     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5478     if (!MaybeKind) {
5479       Error(MaybeKind.takeError());
5480       return Failure;
5481     }
5482     unsigned Kind = MaybeKind.get();
5483 
5484     if ((Kind == SUBMODULE_METADATA) != First) {
5485       Error("submodule metadata record should be at beginning of block");
5486       return Failure;
5487     }
5488     First = false;
5489 
5490     // Submodule information is only valid if we have a current module.
5491     // FIXME: Should we error on these cases?
5492     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5493         Kind != SUBMODULE_DEFINITION)
5494       continue;
5495 
5496     switch (Kind) {
5497     default:  // Default behavior: ignore.
5498       break;
5499 
5500     case SUBMODULE_DEFINITION: {
5501       if (Record.size() < 12) {
5502         Error("malformed module definition");
5503         return Failure;
5504       }
5505 
5506       StringRef Name = Blob;
5507       unsigned Idx = 0;
5508       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5509       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5510       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5511       bool IsFramework = Record[Idx++];
5512       bool IsExplicit = Record[Idx++];
5513       bool IsSystem = Record[Idx++];
5514       bool IsExternC = Record[Idx++];
5515       bool InferSubmodules = Record[Idx++];
5516       bool InferExplicitSubmodules = Record[Idx++];
5517       bool InferExportWildcard = Record[Idx++];
5518       bool ConfigMacrosExhaustive = Record[Idx++];
5519       bool ModuleMapIsPrivate = Record[Idx++];
5520 
5521       Module *ParentModule = nullptr;
5522       if (Parent)
5523         ParentModule = getSubmodule(Parent);
5524 
5525       // Retrieve this (sub)module from the module map, creating it if
5526       // necessary.
5527       CurrentModule =
5528           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5529               .first;
5530 
5531       // FIXME: set the definition loc for CurrentModule, or call
5532       // ModMap.setInferredModuleAllowedBy()
5533 
5534       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5535       if (GlobalIndex >= SubmodulesLoaded.size() ||
5536           SubmodulesLoaded[GlobalIndex]) {
5537         Error("too many submodules");
5538         return Failure;
5539       }
5540 
5541       if (!ParentModule) {
5542         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5543           // Don't emit module relocation error if we have -fno-validate-pch
5544           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5545                     DisableValidationForModuleKind::Module) &&
5546               CurFile != F.File) {
5547             Error(diag::err_module_file_conflict,
5548                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5549                   F.File->getName());
5550             return Failure;
5551           }
5552         }
5553 
5554         F.DidReadTopLevelSubmodule = true;
5555         CurrentModule->setASTFile(F.File);
5556         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5557       }
5558 
5559       CurrentModule->Kind = Kind;
5560       CurrentModule->Signature = F.Signature;
5561       CurrentModule->IsFromModuleFile = true;
5562       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5563       CurrentModule->IsExternC = IsExternC;
5564       CurrentModule->InferSubmodules = InferSubmodules;
5565       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5566       CurrentModule->InferExportWildcard = InferExportWildcard;
5567       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5568       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5569       if (DeserializationListener)
5570         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5571 
5572       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5573 
5574       // Clear out data that will be replaced by what is in the module file.
5575       CurrentModule->LinkLibraries.clear();
5576       CurrentModule->ConfigMacros.clear();
5577       CurrentModule->UnresolvedConflicts.clear();
5578       CurrentModule->Conflicts.clear();
5579 
5580       // The module is available unless it's missing a requirement; relevant
5581       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5582       // Missing headers that were present when the module was built do not
5583       // make it unavailable -- if we got this far, this must be an explicitly
5584       // imported module file.
5585       CurrentModule->Requirements.clear();
5586       CurrentModule->MissingHeaders.clear();
5587       CurrentModule->IsUnimportable =
5588           ParentModule && ParentModule->IsUnimportable;
5589       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5590       break;
5591     }
5592 
5593     case SUBMODULE_UMBRELLA_HEADER: {
5594       std::string Filename = std::string(Blob);
5595       ResolveImportedPath(F, Filename);
5596       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5597         if (!CurrentModule->getUmbrellaHeader())
5598           // FIXME: NameAsWritten
5599           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5600         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5601           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5602             Error("mismatched umbrella headers in submodule");
5603           return OutOfDate;
5604         }
5605       }
5606       break;
5607     }
5608 
5609     case SUBMODULE_HEADER:
5610     case SUBMODULE_EXCLUDED_HEADER:
5611     case SUBMODULE_PRIVATE_HEADER:
5612       // We lazily associate headers with their modules via the HeaderInfo table.
5613       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5614       // of complete filenames or remove it entirely.
5615       break;
5616 
5617     case SUBMODULE_TEXTUAL_HEADER:
5618     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5619       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5620       // them here.
5621       break;
5622 
5623     case SUBMODULE_TOPHEADER:
5624       CurrentModule->addTopHeaderFilename(Blob);
5625       break;
5626 
5627     case SUBMODULE_UMBRELLA_DIR: {
5628       std::string Dirname = std::string(Blob);
5629       ResolveImportedPath(F, Dirname);
5630       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5631         if (!CurrentModule->getUmbrellaDir())
5632           // FIXME: NameAsWritten
5633           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5634         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5635           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5636             Error("mismatched umbrella directories in submodule");
5637           return OutOfDate;
5638         }
5639       }
5640       break;
5641     }
5642 
5643     case SUBMODULE_METADATA: {
5644       F.BaseSubmoduleID = getTotalNumSubmodules();
5645       F.LocalNumSubmodules = Record[0];
5646       unsigned LocalBaseSubmoduleID = Record[1];
5647       if (F.LocalNumSubmodules > 0) {
5648         // Introduce the global -> local mapping for submodules within this
5649         // module.
5650         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5651 
5652         // Introduce the local -> global mapping for submodules within this
5653         // module.
5654         F.SubmoduleRemap.insertOrReplace(
5655           std::make_pair(LocalBaseSubmoduleID,
5656                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5657 
5658         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5659       }
5660       break;
5661     }
5662 
5663     case SUBMODULE_IMPORTS:
5664       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5665         UnresolvedModuleRef Unresolved;
5666         Unresolved.File = &F;
5667         Unresolved.Mod = CurrentModule;
5668         Unresolved.ID = Record[Idx];
5669         Unresolved.Kind = UnresolvedModuleRef::Import;
5670         Unresolved.IsWildcard = false;
5671         UnresolvedModuleRefs.push_back(Unresolved);
5672       }
5673       break;
5674 
5675     case SUBMODULE_EXPORTS:
5676       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5677         UnresolvedModuleRef Unresolved;
5678         Unresolved.File = &F;
5679         Unresolved.Mod = CurrentModule;
5680         Unresolved.ID = Record[Idx];
5681         Unresolved.Kind = UnresolvedModuleRef::Export;
5682         Unresolved.IsWildcard = Record[Idx + 1];
5683         UnresolvedModuleRefs.push_back(Unresolved);
5684       }
5685 
5686       // Once we've loaded the set of exports, there's no reason to keep
5687       // the parsed, unresolved exports around.
5688       CurrentModule->UnresolvedExports.clear();
5689       break;
5690 
5691     case SUBMODULE_REQUIRES:
5692       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5693                                     PP.getTargetInfo());
5694       break;
5695 
5696     case SUBMODULE_LINK_LIBRARY:
5697       ModMap.resolveLinkAsDependencies(CurrentModule);
5698       CurrentModule->LinkLibraries.push_back(
5699           Module::LinkLibrary(std::string(Blob), Record[0]));
5700       break;
5701 
5702     case SUBMODULE_CONFIG_MACRO:
5703       CurrentModule->ConfigMacros.push_back(Blob.str());
5704       break;
5705 
5706     case SUBMODULE_CONFLICT: {
5707       UnresolvedModuleRef Unresolved;
5708       Unresolved.File = &F;
5709       Unresolved.Mod = CurrentModule;
5710       Unresolved.ID = Record[0];
5711       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5712       Unresolved.IsWildcard = false;
5713       Unresolved.String = Blob;
5714       UnresolvedModuleRefs.push_back(Unresolved);
5715       break;
5716     }
5717 
5718     case SUBMODULE_INITIALIZERS: {
5719       if (!ContextObj)
5720         break;
5721       SmallVector<uint32_t, 16> Inits;
5722       for (auto &ID : Record)
5723         Inits.push_back(getGlobalDeclID(F, ID));
5724       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5725       break;
5726     }
5727 
5728     case SUBMODULE_EXPORT_AS:
5729       CurrentModule->ExportAsModule = Blob.str();
5730       ModMap.addLinkAsDependency(CurrentModule);
5731       break;
5732     }
5733   }
5734 }
5735 
5736 /// Parse the record that corresponds to a LangOptions data
5737 /// structure.
5738 ///
5739 /// This routine parses the language options from the AST file and then gives
5740 /// them to the AST listener if one is set.
5741 ///
5742 /// \returns true if the listener deems the file unacceptable, false otherwise.
5743 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5744                                      bool Complain,
5745                                      ASTReaderListener &Listener,
5746                                      bool AllowCompatibleDifferences) {
5747   LangOptions LangOpts;
5748   unsigned Idx = 0;
5749 #define LANGOPT(Name, Bits, Default, Description) \
5750   LangOpts.Name = Record[Idx++];
5751 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5752   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5753 #include "clang/Basic/LangOptions.def"
5754 #define SANITIZER(NAME, ID)                                                    \
5755   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5756 #include "clang/Basic/Sanitizers.def"
5757 
5758   for (unsigned N = Record[Idx++]; N; --N)
5759     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5760 
5761   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5762   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5763   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5764 
5765   LangOpts.CurrentModule = ReadString(Record, Idx);
5766 
5767   // Comment options.
5768   for (unsigned N = Record[Idx++]; N; --N) {
5769     LangOpts.CommentOpts.BlockCommandNames.push_back(
5770       ReadString(Record, Idx));
5771   }
5772   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5773 
5774   // OpenMP offloading options.
5775   for (unsigned N = Record[Idx++]; N; --N) {
5776     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5777   }
5778 
5779   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5780 
5781   return Listener.ReadLanguageOptions(LangOpts, Complain,
5782                                       AllowCompatibleDifferences);
5783 }
5784 
5785 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5786                                    ASTReaderListener &Listener,
5787                                    bool AllowCompatibleDifferences) {
5788   unsigned Idx = 0;
5789   TargetOptions TargetOpts;
5790   TargetOpts.Triple = ReadString(Record, Idx);
5791   TargetOpts.CPU = ReadString(Record, Idx);
5792   TargetOpts.TuneCPU = ReadString(Record, Idx);
5793   TargetOpts.ABI = ReadString(Record, Idx);
5794   for (unsigned N = Record[Idx++]; N; --N) {
5795     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5796   }
5797   for (unsigned N = Record[Idx++]; N; --N) {
5798     TargetOpts.Features.push_back(ReadString(Record, Idx));
5799   }
5800 
5801   return Listener.ReadTargetOptions(TargetOpts, Complain,
5802                                     AllowCompatibleDifferences);
5803 }
5804 
5805 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5806                                        ASTReaderListener &Listener) {
5807   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5808   unsigned Idx = 0;
5809 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5810 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5811   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5812 #include "clang/Basic/DiagnosticOptions.def"
5813 
5814   for (unsigned N = Record[Idx++]; N; --N)
5815     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5816   for (unsigned N = Record[Idx++]; N; --N)
5817     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5818 
5819   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5820 }
5821 
5822 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5823                                        ASTReaderListener &Listener) {
5824   FileSystemOptions FSOpts;
5825   unsigned Idx = 0;
5826   FSOpts.WorkingDir = ReadString(Record, Idx);
5827   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5828 }
5829 
5830 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5831                                          bool Complain,
5832                                          ASTReaderListener &Listener) {
5833   HeaderSearchOptions HSOpts;
5834   unsigned Idx = 0;
5835   HSOpts.Sysroot = ReadString(Record, Idx);
5836 
5837   // Include entries.
5838   for (unsigned N = Record[Idx++]; N; --N) {
5839     std::string Path = ReadString(Record, Idx);
5840     frontend::IncludeDirGroup Group
5841       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5842     bool IsFramework = Record[Idx++];
5843     bool IgnoreSysRoot = Record[Idx++];
5844     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5845                                     IgnoreSysRoot);
5846   }
5847 
5848   // System header prefixes.
5849   for (unsigned N = Record[Idx++]; N; --N) {
5850     std::string Prefix = ReadString(Record, Idx);
5851     bool IsSystemHeader = Record[Idx++];
5852     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5853   }
5854 
5855   HSOpts.ResourceDir = ReadString(Record, Idx);
5856   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5857   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5858   HSOpts.DisableModuleHash = Record[Idx++];
5859   HSOpts.ImplicitModuleMaps = Record[Idx++];
5860   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5861   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5862   HSOpts.UseBuiltinIncludes = Record[Idx++];
5863   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5864   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5865   HSOpts.UseLibcxx = Record[Idx++];
5866   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5867 
5868   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5869                                           Complain);
5870 }
5871 
5872 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5873                                          bool Complain,
5874                                          ASTReaderListener &Listener,
5875                                          std::string &SuggestedPredefines) {
5876   PreprocessorOptions PPOpts;
5877   unsigned Idx = 0;
5878 
5879   // Macro definitions/undefs
5880   for (unsigned N = Record[Idx++]; N; --N) {
5881     std::string Macro = ReadString(Record, Idx);
5882     bool IsUndef = Record[Idx++];
5883     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5884   }
5885 
5886   // Includes
5887   for (unsigned N = Record[Idx++]; N; --N) {
5888     PPOpts.Includes.push_back(ReadString(Record, Idx));
5889   }
5890 
5891   // Macro Includes
5892   for (unsigned N = Record[Idx++]; N; --N) {
5893     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5894   }
5895 
5896   PPOpts.UsePredefines = Record[Idx++];
5897   PPOpts.DetailedRecord = Record[Idx++];
5898   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5899   PPOpts.ObjCXXARCStandardLibrary =
5900     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5901   SuggestedPredefines.clear();
5902   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5903                                           SuggestedPredefines);
5904 }
5905 
5906 std::pair<ModuleFile *, unsigned>
5907 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5908   GlobalPreprocessedEntityMapType::iterator
5909   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5910   assert(I != GlobalPreprocessedEntityMap.end() &&
5911          "Corrupted global preprocessed entity map");
5912   ModuleFile *M = I->second;
5913   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5914   return std::make_pair(M, LocalIndex);
5915 }
5916 
5917 llvm::iterator_range<PreprocessingRecord::iterator>
5918 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5919   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5920     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5921                                              Mod.NumPreprocessedEntities);
5922 
5923   return llvm::make_range(PreprocessingRecord::iterator(),
5924                           PreprocessingRecord::iterator());
5925 }
5926 
5927 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5928 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5929   return llvm::make_range(
5930       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5931       ModuleDeclIterator(this, &Mod,
5932                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5933 }
5934 
5935 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5936   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5937   assert(I != GlobalSkippedRangeMap.end() &&
5938     "Corrupted global skipped range map");
5939   ModuleFile *M = I->second;
5940   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5941   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5942   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5943   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5944                     TranslateSourceLocation(*M, RawRange.getEnd()));
5945   assert(Range.isValid());
5946   return Range;
5947 }
5948 
5949 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5950   PreprocessedEntityID PPID = Index+1;
5951   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5952   ModuleFile &M = *PPInfo.first;
5953   unsigned LocalIndex = PPInfo.second;
5954   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5955 
5956   if (!PP.getPreprocessingRecord()) {
5957     Error("no preprocessing record");
5958     return nullptr;
5959   }
5960 
5961   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5962   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5963           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5964     Error(std::move(Err));
5965     return nullptr;
5966   }
5967 
5968   Expected<llvm::BitstreamEntry> MaybeEntry =
5969       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5970   if (!MaybeEntry) {
5971     Error(MaybeEntry.takeError());
5972     return nullptr;
5973   }
5974   llvm::BitstreamEntry Entry = MaybeEntry.get();
5975 
5976   if (Entry.Kind != llvm::BitstreamEntry::Record)
5977     return nullptr;
5978 
5979   // Read the record.
5980   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5981                     TranslateSourceLocation(M, PPOffs.getEnd()));
5982   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5983   StringRef Blob;
5984   RecordData Record;
5985   Expected<unsigned> MaybeRecType =
5986       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5987   if (!MaybeRecType) {
5988     Error(MaybeRecType.takeError());
5989     return nullptr;
5990   }
5991   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5992   case PPD_MACRO_EXPANSION: {
5993     bool isBuiltin = Record[0];
5994     IdentifierInfo *Name = nullptr;
5995     MacroDefinitionRecord *Def = nullptr;
5996     if (isBuiltin)
5997       Name = getLocalIdentifier(M, Record[1]);
5998     else {
5999       PreprocessedEntityID GlobalID =
6000           getGlobalPreprocessedEntityID(M, Record[1]);
6001       Def = cast<MacroDefinitionRecord>(
6002           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
6003     }
6004 
6005     MacroExpansion *ME;
6006     if (isBuiltin)
6007       ME = new (PPRec) MacroExpansion(Name, Range);
6008     else
6009       ME = new (PPRec) MacroExpansion(Def, Range);
6010 
6011     return ME;
6012   }
6013 
6014   case PPD_MACRO_DEFINITION: {
6015     // Decode the identifier info and then check again; if the macro is
6016     // still defined and associated with the identifier,
6017     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6018     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6019 
6020     if (DeserializationListener)
6021       DeserializationListener->MacroDefinitionRead(PPID, MD);
6022 
6023     return MD;
6024   }
6025 
6026   case PPD_INCLUSION_DIRECTIVE: {
6027     const char *FullFileNameStart = Blob.data() + Record[0];
6028     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6029     const FileEntry *File = nullptr;
6030     if (!FullFileName.empty())
6031       if (auto FE = PP.getFileManager().getFile(FullFileName))
6032         File = *FE;
6033 
6034     // FIXME: Stable encoding
6035     InclusionDirective::InclusionKind Kind
6036       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6037     InclusionDirective *ID
6038       = new (PPRec) InclusionDirective(PPRec, Kind,
6039                                        StringRef(Blob.data(), Record[0]),
6040                                        Record[1], Record[3],
6041                                        File,
6042                                        Range);
6043     return ID;
6044   }
6045   }
6046 
6047   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6048 }
6049 
6050 /// Find the next module that contains entities and return the ID
6051 /// of the first entry.
6052 ///
6053 /// \param SLocMapI points at a chunk of a module that contains no
6054 /// preprocessed entities or the entities it contains are not the ones we are
6055 /// looking for.
6056 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6057                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6058   ++SLocMapI;
6059   for (GlobalSLocOffsetMapType::const_iterator
6060          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6061     ModuleFile &M = *SLocMapI->second;
6062     if (M.NumPreprocessedEntities)
6063       return M.BasePreprocessedEntityID;
6064   }
6065 
6066   return getTotalNumPreprocessedEntities();
6067 }
6068 
6069 namespace {
6070 
6071 struct PPEntityComp {
6072   const ASTReader &Reader;
6073   ModuleFile &M;
6074 
6075   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6076 
6077   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6078     SourceLocation LHS = getLoc(L);
6079     SourceLocation RHS = getLoc(R);
6080     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6081   }
6082 
6083   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6084     SourceLocation LHS = getLoc(L);
6085     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6086   }
6087 
6088   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6089     SourceLocation RHS = getLoc(R);
6090     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6091   }
6092 
6093   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6094     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6095   }
6096 };
6097 
6098 } // namespace
6099 
6100 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6101                                                        bool EndsAfter) const {
6102   if (SourceMgr.isLocalSourceLocation(Loc))
6103     return getTotalNumPreprocessedEntities();
6104 
6105   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6106       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6107   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6108          "Corrupted global sloc offset map");
6109 
6110   if (SLocMapI->second->NumPreprocessedEntities == 0)
6111     return findNextPreprocessedEntity(SLocMapI);
6112 
6113   ModuleFile &M = *SLocMapI->second;
6114 
6115   using pp_iterator = const PPEntityOffset *;
6116 
6117   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6118   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6119 
6120   size_t Count = M.NumPreprocessedEntities;
6121   size_t Half;
6122   pp_iterator First = pp_begin;
6123   pp_iterator PPI;
6124 
6125   if (EndsAfter) {
6126     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6127                            PPEntityComp(*this, M));
6128   } else {
6129     // Do a binary search manually instead of using std::lower_bound because
6130     // The end locations of entities may be unordered (when a macro expansion
6131     // is inside another macro argument), but for this case it is not important
6132     // whether we get the first macro expansion or its containing macro.
6133     while (Count > 0) {
6134       Half = Count / 2;
6135       PPI = First;
6136       std::advance(PPI, Half);
6137       if (SourceMgr.isBeforeInTranslationUnit(
6138               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6139         First = PPI;
6140         ++First;
6141         Count = Count - Half - 1;
6142       } else
6143         Count = Half;
6144     }
6145   }
6146 
6147   if (PPI == pp_end)
6148     return findNextPreprocessedEntity(SLocMapI);
6149 
6150   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6151 }
6152 
6153 /// Returns a pair of [Begin, End) indices of preallocated
6154 /// preprocessed entities that \arg Range encompasses.
6155 std::pair<unsigned, unsigned>
6156     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6157   if (Range.isInvalid())
6158     return std::make_pair(0,0);
6159   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6160 
6161   PreprocessedEntityID BeginID =
6162       findPreprocessedEntity(Range.getBegin(), false);
6163   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6164   return std::make_pair(BeginID, EndID);
6165 }
6166 
6167 /// Optionally returns true or false if the preallocated preprocessed
6168 /// entity with index \arg Index came from file \arg FID.
6169 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6170                                                              FileID FID) {
6171   if (FID.isInvalid())
6172     return false;
6173 
6174   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6175   ModuleFile &M = *PPInfo.first;
6176   unsigned LocalIndex = PPInfo.second;
6177   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6178 
6179   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6180   if (Loc.isInvalid())
6181     return false;
6182 
6183   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6184     return true;
6185   else
6186     return false;
6187 }
6188 
6189 namespace {
6190 
6191   /// Visitor used to search for information about a header file.
6192   class HeaderFileInfoVisitor {
6193     const FileEntry *FE;
6194     Optional<HeaderFileInfo> HFI;
6195 
6196   public:
6197     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6198 
6199     bool operator()(ModuleFile &M) {
6200       HeaderFileInfoLookupTable *Table
6201         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6202       if (!Table)
6203         return false;
6204 
6205       // Look in the on-disk hash table for an entry for this file name.
6206       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6207       if (Pos == Table->end())
6208         return false;
6209 
6210       HFI = *Pos;
6211       return true;
6212     }
6213 
6214     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6215   };
6216 
6217 } // namespace
6218 
6219 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6220   HeaderFileInfoVisitor Visitor(FE);
6221   ModuleMgr.visit(Visitor);
6222   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6223     return *HFI;
6224 
6225   return HeaderFileInfo();
6226 }
6227 
6228 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6229   using DiagState = DiagnosticsEngine::DiagState;
6230   SmallVector<DiagState *, 32> DiagStates;
6231 
6232   for (ModuleFile &F : ModuleMgr) {
6233     unsigned Idx = 0;
6234     auto &Record = F.PragmaDiagMappings;
6235     if (Record.empty())
6236       continue;
6237 
6238     DiagStates.clear();
6239 
6240     auto ReadDiagState =
6241         [&](const DiagState &BasedOn, SourceLocation Loc,
6242             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6243       unsigned BackrefID = Record[Idx++];
6244       if (BackrefID != 0)
6245         return DiagStates[BackrefID - 1];
6246 
6247       // A new DiagState was created here.
6248       Diag.DiagStates.push_back(BasedOn);
6249       DiagState *NewState = &Diag.DiagStates.back();
6250       DiagStates.push_back(NewState);
6251       unsigned Size = Record[Idx++];
6252       assert(Idx + Size * 2 <= Record.size() &&
6253              "Invalid data, not enough diag/map pairs");
6254       while (Size--) {
6255         unsigned DiagID = Record[Idx++];
6256         DiagnosticMapping NewMapping =
6257             DiagnosticMapping::deserialize(Record[Idx++]);
6258         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6259           continue;
6260 
6261         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6262 
6263         // If this mapping was specified as a warning but the severity was
6264         // upgraded due to diagnostic settings, simulate the current diagnostic
6265         // settings (and use a warning).
6266         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6267           NewMapping.setSeverity(diag::Severity::Warning);
6268           NewMapping.setUpgradedFromWarning(false);
6269         }
6270 
6271         Mapping = NewMapping;
6272       }
6273       return NewState;
6274     };
6275 
6276     // Read the first state.
6277     DiagState *FirstState;
6278     if (F.Kind == MK_ImplicitModule) {
6279       // Implicitly-built modules are reused with different diagnostic
6280       // settings.  Use the initial diagnostic state from Diag to simulate this
6281       // compilation's diagnostic settings.
6282       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6283       DiagStates.push_back(FirstState);
6284 
6285       // Skip the initial diagnostic state from the serialized module.
6286       assert(Record[1] == 0 &&
6287              "Invalid data, unexpected backref in initial state");
6288       Idx = 3 + Record[2] * 2;
6289       assert(Idx < Record.size() &&
6290              "Invalid data, not enough state change pairs in initial state");
6291     } else if (F.isModule()) {
6292       // For an explicit module, preserve the flags from the module build
6293       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6294       // -Wblah flags.
6295       unsigned Flags = Record[Idx++];
6296       DiagState Initial;
6297       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6298       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6299       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6300       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6301       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6302       Initial.ExtBehavior = (diag::Severity)Flags;
6303       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6304 
6305       assert(F.OriginalSourceFileID.isValid());
6306 
6307       // Set up the root buffer of the module to start with the initial
6308       // diagnostic state of the module itself, to cover files that contain no
6309       // explicit transitions (for which we did not serialize anything).
6310       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6311           .StateTransitions.push_back({FirstState, 0});
6312     } else {
6313       // For prefix ASTs, start with whatever the user configured on the
6314       // command line.
6315       Idx++; // Skip flags.
6316       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6317                                  SourceLocation(), false);
6318     }
6319 
6320     // Read the state transitions.
6321     unsigned NumLocations = Record[Idx++];
6322     while (NumLocations--) {
6323       assert(Idx < Record.size() &&
6324              "Invalid data, missing pragma diagnostic states");
6325       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6326       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6327       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6328       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6329       unsigned Transitions = Record[Idx++];
6330 
6331       // Note that we don't need to set up Parent/ParentOffset here, because
6332       // we won't be changing the diagnostic state within imported FileIDs
6333       // (other than perhaps appending to the main source file, which has no
6334       // parent).
6335       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6336       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6337       for (unsigned I = 0; I != Transitions; ++I) {
6338         unsigned Offset = Record[Idx++];
6339         auto *State =
6340             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6341         F.StateTransitions.push_back({State, Offset});
6342       }
6343     }
6344 
6345     // Read the final state.
6346     assert(Idx < Record.size() &&
6347            "Invalid data, missing final pragma diagnostic state");
6348     SourceLocation CurStateLoc =
6349         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6350     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6351 
6352     if (!F.isModule()) {
6353       Diag.DiagStatesByLoc.CurDiagState = CurState;
6354       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6355 
6356       // Preserve the property that the imaginary root file describes the
6357       // current state.
6358       FileID NullFile;
6359       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6360       if (T.empty())
6361         T.push_back({CurState, 0});
6362       else
6363         T[0].State = CurState;
6364     }
6365 
6366     // Don't try to read these mappings again.
6367     Record.clear();
6368   }
6369 }
6370 
6371 /// Get the correct cursor and offset for loading a type.
6372 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6373   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6374   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6375   ModuleFile *M = I->second;
6376   return RecordLocation(
6377       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6378              M->DeclsBlockStartOffset);
6379 }
6380 
6381 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6382   switch (code) {
6383 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6384   case TYPE_##CODE_ID: return Type::CLASS_ID;
6385 #include "clang/Serialization/TypeBitCodes.def"
6386   default: return llvm::None;
6387   }
6388 }
6389 
6390 /// Read and return the type with the given index..
6391 ///
6392 /// The index is the type ID, shifted and minus the number of predefs. This
6393 /// routine actually reads the record corresponding to the type at the given
6394 /// location. It is a helper routine for GetType, which deals with reading type
6395 /// IDs.
6396 QualType ASTReader::readTypeRecord(unsigned Index) {
6397   assert(ContextObj && "reading type with no AST context");
6398   ASTContext &Context = *ContextObj;
6399   RecordLocation Loc = TypeCursorForIndex(Index);
6400   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6401 
6402   // Keep track of where we are in the stream, then jump back there
6403   // after reading this type.
6404   SavedStreamPosition SavedPosition(DeclsCursor);
6405 
6406   ReadingKindTracker ReadingKind(Read_Type, *this);
6407 
6408   // Note that we are loading a type record.
6409   Deserializing AType(this);
6410 
6411   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6412     Error(std::move(Err));
6413     return QualType();
6414   }
6415   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6416   if (!RawCode) {
6417     Error(RawCode.takeError());
6418     return QualType();
6419   }
6420 
6421   ASTRecordReader Record(*this, *Loc.F);
6422   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6423   if (!Code) {
6424     Error(Code.takeError());
6425     return QualType();
6426   }
6427   if (Code.get() == TYPE_EXT_QUAL) {
6428     QualType baseType = Record.readQualType();
6429     Qualifiers quals = Record.readQualifiers();
6430     return Context.getQualifiedType(baseType, quals);
6431   }
6432 
6433   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6434   if (!maybeClass) {
6435     Error("Unexpected code for type");
6436     return QualType();
6437   }
6438 
6439   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6440   return TypeReader.read(*maybeClass);
6441 }
6442 
6443 namespace clang {
6444 
6445 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6446   ASTRecordReader &Reader;
6447 
6448   SourceLocation readSourceLocation() {
6449     return Reader.readSourceLocation();
6450   }
6451 
6452   TypeSourceInfo *GetTypeSourceInfo() {
6453     return Reader.readTypeSourceInfo();
6454   }
6455 
6456   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6457     return Reader.readNestedNameSpecifierLoc();
6458   }
6459 
6460   Attr *ReadAttr() {
6461     return Reader.readAttr();
6462   }
6463 
6464 public:
6465   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6466 
6467   // We want compile-time assurance that we've enumerated all of
6468   // these, so unfortunately we have to declare them first, then
6469   // define them out-of-line.
6470 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6471 #define TYPELOC(CLASS, PARENT) \
6472   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6473 #include "clang/AST/TypeLocNodes.def"
6474 
6475   void VisitFunctionTypeLoc(FunctionTypeLoc);
6476   void VisitArrayTypeLoc(ArrayTypeLoc);
6477 };
6478 
6479 } // namespace clang
6480 
6481 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6482   // nothing to do
6483 }
6484 
6485 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6486   TL.setBuiltinLoc(readSourceLocation());
6487   if (TL.needsExtraLocalData()) {
6488     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6489     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6490     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6491     TL.setModeAttr(Reader.readInt());
6492   }
6493 }
6494 
6495 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6496   TL.setNameLoc(readSourceLocation());
6497 }
6498 
6499 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6500   TL.setStarLoc(readSourceLocation());
6501 }
6502 
6503 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6504   // nothing to do
6505 }
6506 
6507 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6508   // nothing to do
6509 }
6510 
6511 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6512   TL.setExpansionLoc(readSourceLocation());
6513 }
6514 
6515 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6516   TL.setCaretLoc(readSourceLocation());
6517 }
6518 
6519 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6520   TL.setAmpLoc(readSourceLocation());
6521 }
6522 
6523 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6524   TL.setAmpAmpLoc(readSourceLocation());
6525 }
6526 
6527 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6528   TL.setStarLoc(readSourceLocation());
6529   TL.setClassTInfo(GetTypeSourceInfo());
6530 }
6531 
6532 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6533   TL.setLBracketLoc(readSourceLocation());
6534   TL.setRBracketLoc(readSourceLocation());
6535   if (Reader.readBool())
6536     TL.setSizeExpr(Reader.readExpr());
6537   else
6538     TL.setSizeExpr(nullptr);
6539 }
6540 
6541 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6542   VisitArrayTypeLoc(TL);
6543 }
6544 
6545 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6546   VisitArrayTypeLoc(TL);
6547 }
6548 
6549 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6550   VisitArrayTypeLoc(TL);
6551 }
6552 
6553 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6554                                             DependentSizedArrayTypeLoc TL) {
6555   VisitArrayTypeLoc(TL);
6556 }
6557 
6558 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6559     DependentAddressSpaceTypeLoc TL) {
6560 
6561     TL.setAttrNameLoc(readSourceLocation());
6562     TL.setAttrOperandParensRange(Reader.readSourceRange());
6563     TL.setAttrExprOperand(Reader.readExpr());
6564 }
6565 
6566 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6567                                         DependentSizedExtVectorTypeLoc TL) {
6568   TL.setNameLoc(readSourceLocation());
6569 }
6570 
6571 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6572   TL.setNameLoc(readSourceLocation());
6573 }
6574 
6575 void TypeLocReader::VisitDependentVectorTypeLoc(
6576     DependentVectorTypeLoc TL) {
6577   TL.setNameLoc(readSourceLocation());
6578 }
6579 
6580 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6581   TL.setNameLoc(readSourceLocation());
6582 }
6583 
6584 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6585   TL.setAttrNameLoc(readSourceLocation());
6586   TL.setAttrOperandParensRange(Reader.readSourceRange());
6587   TL.setAttrRowOperand(Reader.readExpr());
6588   TL.setAttrColumnOperand(Reader.readExpr());
6589 }
6590 
6591 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6592     DependentSizedMatrixTypeLoc TL) {
6593   TL.setAttrNameLoc(readSourceLocation());
6594   TL.setAttrOperandParensRange(Reader.readSourceRange());
6595   TL.setAttrRowOperand(Reader.readExpr());
6596   TL.setAttrColumnOperand(Reader.readExpr());
6597 }
6598 
6599 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6600   TL.setLocalRangeBegin(readSourceLocation());
6601   TL.setLParenLoc(readSourceLocation());
6602   TL.setRParenLoc(readSourceLocation());
6603   TL.setExceptionSpecRange(Reader.readSourceRange());
6604   TL.setLocalRangeEnd(readSourceLocation());
6605   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6606     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6607   }
6608 }
6609 
6610 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6611   VisitFunctionTypeLoc(TL);
6612 }
6613 
6614 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6615   VisitFunctionTypeLoc(TL);
6616 }
6617 
6618 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6619   TL.setNameLoc(readSourceLocation());
6620 }
6621 
6622 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6623   TL.setNameLoc(readSourceLocation());
6624 }
6625 
6626 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6627   TL.setTypeofLoc(readSourceLocation());
6628   TL.setLParenLoc(readSourceLocation());
6629   TL.setRParenLoc(readSourceLocation());
6630 }
6631 
6632 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6633   TL.setTypeofLoc(readSourceLocation());
6634   TL.setLParenLoc(readSourceLocation());
6635   TL.setRParenLoc(readSourceLocation());
6636   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6637 }
6638 
6639 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6640   TL.setNameLoc(readSourceLocation());
6641 }
6642 
6643 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6644   TL.setKWLoc(readSourceLocation());
6645   TL.setLParenLoc(readSourceLocation());
6646   TL.setRParenLoc(readSourceLocation());
6647   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6648 }
6649 
6650 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6651   TL.setNameLoc(readSourceLocation());
6652   if (Reader.readBool()) {
6653     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6654     TL.setTemplateKWLoc(readSourceLocation());
6655     TL.setConceptNameLoc(readSourceLocation());
6656     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6657     TL.setLAngleLoc(readSourceLocation());
6658     TL.setRAngleLoc(readSourceLocation());
6659     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6660       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6661                               TL.getTypePtr()->getArg(i).getKind()));
6662   }
6663 }
6664 
6665 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6666     DeducedTemplateSpecializationTypeLoc TL) {
6667   TL.setTemplateNameLoc(readSourceLocation());
6668 }
6669 
6670 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6671   TL.setNameLoc(readSourceLocation());
6672 }
6673 
6674 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6675   TL.setNameLoc(readSourceLocation());
6676 }
6677 
6678 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6679   TL.setAttr(ReadAttr());
6680 }
6681 
6682 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6683   TL.setNameLoc(readSourceLocation());
6684 }
6685 
6686 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6687                                             SubstTemplateTypeParmTypeLoc TL) {
6688   TL.setNameLoc(readSourceLocation());
6689 }
6690 
6691 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6692                                           SubstTemplateTypeParmPackTypeLoc TL) {
6693   TL.setNameLoc(readSourceLocation());
6694 }
6695 
6696 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6697                                            TemplateSpecializationTypeLoc TL) {
6698   TL.setTemplateKeywordLoc(readSourceLocation());
6699   TL.setTemplateNameLoc(readSourceLocation());
6700   TL.setLAngleLoc(readSourceLocation());
6701   TL.setRAngleLoc(readSourceLocation());
6702   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6703     TL.setArgLocInfo(
6704         i,
6705         Reader.readTemplateArgumentLocInfo(
6706           TL.getTypePtr()->getArg(i).getKind()));
6707 }
6708 
6709 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6710   TL.setLParenLoc(readSourceLocation());
6711   TL.setRParenLoc(readSourceLocation());
6712 }
6713 
6714 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6715   TL.setElaboratedKeywordLoc(readSourceLocation());
6716   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6717 }
6718 
6719 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6720   TL.setNameLoc(readSourceLocation());
6721 }
6722 
6723 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6724   TL.setElaboratedKeywordLoc(readSourceLocation());
6725   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6726   TL.setNameLoc(readSourceLocation());
6727 }
6728 
6729 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6730        DependentTemplateSpecializationTypeLoc TL) {
6731   TL.setElaboratedKeywordLoc(readSourceLocation());
6732   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6733   TL.setTemplateKeywordLoc(readSourceLocation());
6734   TL.setTemplateNameLoc(readSourceLocation());
6735   TL.setLAngleLoc(readSourceLocation());
6736   TL.setRAngleLoc(readSourceLocation());
6737   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6738     TL.setArgLocInfo(
6739         I,
6740         Reader.readTemplateArgumentLocInfo(
6741             TL.getTypePtr()->getArg(I).getKind()));
6742 }
6743 
6744 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6745   TL.setEllipsisLoc(readSourceLocation());
6746 }
6747 
6748 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6749   TL.setNameLoc(readSourceLocation());
6750 }
6751 
6752 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6753   if (TL.getNumProtocols()) {
6754     TL.setProtocolLAngleLoc(readSourceLocation());
6755     TL.setProtocolRAngleLoc(readSourceLocation());
6756   }
6757   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6758     TL.setProtocolLoc(i, readSourceLocation());
6759 }
6760 
6761 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6762   TL.setHasBaseTypeAsWritten(Reader.readBool());
6763   TL.setTypeArgsLAngleLoc(readSourceLocation());
6764   TL.setTypeArgsRAngleLoc(readSourceLocation());
6765   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6766     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6767   TL.setProtocolLAngleLoc(readSourceLocation());
6768   TL.setProtocolRAngleLoc(readSourceLocation());
6769   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6770     TL.setProtocolLoc(i, readSourceLocation());
6771 }
6772 
6773 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6774   TL.setStarLoc(readSourceLocation());
6775 }
6776 
6777 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6778   TL.setKWLoc(readSourceLocation());
6779   TL.setLParenLoc(readSourceLocation());
6780   TL.setRParenLoc(readSourceLocation());
6781 }
6782 
6783 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6784   TL.setKWLoc(readSourceLocation());
6785 }
6786 
6787 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6788   TL.setNameLoc(readSourceLocation());
6789 }
6790 void TypeLocReader::VisitDependentExtIntTypeLoc(
6791     clang::DependentExtIntTypeLoc TL) {
6792   TL.setNameLoc(readSourceLocation());
6793 }
6794 
6795 
6796 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6797   TypeLocReader TLR(*this);
6798   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6799     TLR.Visit(TL);
6800 }
6801 
6802 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6803   QualType InfoTy = readType();
6804   if (InfoTy.isNull())
6805     return nullptr;
6806 
6807   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6808   readTypeLoc(TInfo->getTypeLoc());
6809   return TInfo;
6810 }
6811 
6812 QualType ASTReader::GetType(TypeID ID) {
6813   assert(ContextObj && "reading type with no AST context");
6814   ASTContext &Context = *ContextObj;
6815 
6816   unsigned FastQuals = ID & Qualifiers::FastMask;
6817   unsigned Index = ID >> Qualifiers::FastWidth;
6818 
6819   if (Index < NUM_PREDEF_TYPE_IDS) {
6820     QualType T;
6821     switch ((PredefinedTypeIDs)Index) {
6822     case PREDEF_TYPE_NULL_ID:
6823       return QualType();
6824     case PREDEF_TYPE_VOID_ID:
6825       T = Context.VoidTy;
6826       break;
6827     case PREDEF_TYPE_BOOL_ID:
6828       T = Context.BoolTy;
6829       break;
6830     case PREDEF_TYPE_CHAR_U_ID:
6831     case PREDEF_TYPE_CHAR_S_ID:
6832       // FIXME: Check that the signedness of CharTy is correct!
6833       T = Context.CharTy;
6834       break;
6835     case PREDEF_TYPE_UCHAR_ID:
6836       T = Context.UnsignedCharTy;
6837       break;
6838     case PREDEF_TYPE_USHORT_ID:
6839       T = Context.UnsignedShortTy;
6840       break;
6841     case PREDEF_TYPE_UINT_ID:
6842       T = Context.UnsignedIntTy;
6843       break;
6844     case PREDEF_TYPE_ULONG_ID:
6845       T = Context.UnsignedLongTy;
6846       break;
6847     case PREDEF_TYPE_ULONGLONG_ID:
6848       T = Context.UnsignedLongLongTy;
6849       break;
6850     case PREDEF_TYPE_UINT128_ID:
6851       T = Context.UnsignedInt128Ty;
6852       break;
6853     case PREDEF_TYPE_SCHAR_ID:
6854       T = Context.SignedCharTy;
6855       break;
6856     case PREDEF_TYPE_WCHAR_ID:
6857       T = Context.WCharTy;
6858       break;
6859     case PREDEF_TYPE_SHORT_ID:
6860       T = Context.ShortTy;
6861       break;
6862     case PREDEF_TYPE_INT_ID:
6863       T = Context.IntTy;
6864       break;
6865     case PREDEF_TYPE_LONG_ID:
6866       T = Context.LongTy;
6867       break;
6868     case PREDEF_TYPE_LONGLONG_ID:
6869       T = Context.LongLongTy;
6870       break;
6871     case PREDEF_TYPE_INT128_ID:
6872       T = Context.Int128Ty;
6873       break;
6874     case PREDEF_TYPE_BFLOAT16_ID:
6875       T = Context.BFloat16Ty;
6876       break;
6877     case PREDEF_TYPE_HALF_ID:
6878       T = Context.HalfTy;
6879       break;
6880     case PREDEF_TYPE_FLOAT_ID:
6881       T = Context.FloatTy;
6882       break;
6883     case PREDEF_TYPE_DOUBLE_ID:
6884       T = Context.DoubleTy;
6885       break;
6886     case PREDEF_TYPE_LONGDOUBLE_ID:
6887       T = Context.LongDoubleTy;
6888       break;
6889     case PREDEF_TYPE_SHORT_ACCUM_ID:
6890       T = Context.ShortAccumTy;
6891       break;
6892     case PREDEF_TYPE_ACCUM_ID:
6893       T = Context.AccumTy;
6894       break;
6895     case PREDEF_TYPE_LONG_ACCUM_ID:
6896       T = Context.LongAccumTy;
6897       break;
6898     case PREDEF_TYPE_USHORT_ACCUM_ID:
6899       T = Context.UnsignedShortAccumTy;
6900       break;
6901     case PREDEF_TYPE_UACCUM_ID:
6902       T = Context.UnsignedAccumTy;
6903       break;
6904     case PREDEF_TYPE_ULONG_ACCUM_ID:
6905       T = Context.UnsignedLongAccumTy;
6906       break;
6907     case PREDEF_TYPE_SHORT_FRACT_ID:
6908       T = Context.ShortFractTy;
6909       break;
6910     case PREDEF_TYPE_FRACT_ID:
6911       T = Context.FractTy;
6912       break;
6913     case PREDEF_TYPE_LONG_FRACT_ID:
6914       T = Context.LongFractTy;
6915       break;
6916     case PREDEF_TYPE_USHORT_FRACT_ID:
6917       T = Context.UnsignedShortFractTy;
6918       break;
6919     case PREDEF_TYPE_UFRACT_ID:
6920       T = Context.UnsignedFractTy;
6921       break;
6922     case PREDEF_TYPE_ULONG_FRACT_ID:
6923       T = Context.UnsignedLongFractTy;
6924       break;
6925     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6926       T = Context.SatShortAccumTy;
6927       break;
6928     case PREDEF_TYPE_SAT_ACCUM_ID:
6929       T = Context.SatAccumTy;
6930       break;
6931     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6932       T = Context.SatLongAccumTy;
6933       break;
6934     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6935       T = Context.SatUnsignedShortAccumTy;
6936       break;
6937     case PREDEF_TYPE_SAT_UACCUM_ID:
6938       T = Context.SatUnsignedAccumTy;
6939       break;
6940     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6941       T = Context.SatUnsignedLongAccumTy;
6942       break;
6943     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6944       T = Context.SatShortFractTy;
6945       break;
6946     case PREDEF_TYPE_SAT_FRACT_ID:
6947       T = Context.SatFractTy;
6948       break;
6949     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6950       T = Context.SatLongFractTy;
6951       break;
6952     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6953       T = Context.SatUnsignedShortFractTy;
6954       break;
6955     case PREDEF_TYPE_SAT_UFRACT_ID:
6956       T = Context.SatUnsignedFractTy;
6957       break;
6958     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6959       T = Context.SatUnsignedLongFractTy;
6960       break;
6961     case PREDEF_TYPE_FLOAT16_ID:
6962       T = Context.Float16Ty;
6963       break;
6964     case PREDEF_TYPE_FLOAT128_ID:
6965       T = Context.Float128Ty;
6966       break;
6967     case PREDEF_TYPE_OVERLOAD_ID:
6968       T = Context.OverloadTy;
6969       break;
6970     case PREDEF_TYPE_BOUND_MEMBER:
6971       T = Context.BoundMemberTy;
6972       break;
6973     case PREDEF_TYPE_PSEUDO_OBJECT:
6974       T = Context.PseudoObjectTy;
6975       break;
6976     case PREDEF_TYPE_DEPENDENT_ID:
6977       T = Context.DependentTy;
6978       break;
6979     case PREDEF_TYPE_UNKNOWN_ANY:
6980       T = Context.UnknownAnyTy;
6981       break;
6982     case PREDEF_TYPE_NULLPTR_ID:
6983       T = Context.NullPtrTy;
6984       break;
6985     case PREDEF_TYPE_CHAR8_ID:
6986       T = Context.Char8Ty;
6987       break;
6988     case PREDEF_TYPE_CHAR16_ID:
6989       T = Context.Char16Ty;
6990       break;
6991     case PREDEF_TYPE_CHAR32_ID:
6992       T = Context.Char32Ty;
6993       break;
6994     case PREDEF_TYPE_OBJC_ID:
6995       T = Context.ObjCBuiltinIdTy;
6996       break;
6997     case PREDEF_TYPE_OBJC_CLASS:
6998       T = Context.ObjCBuiltinClassTy;
6999       break;
7000     case PREDEF_TYPE_OBJC_SEL:
7001       T = Context.ObjCBuiltinSelTy;
7002       break;
7003 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7004     case PREDEF_TYPE_##Id##_ID: \
7005       T = Context.SingletonId; \
7006       break;
7007 #include "clang/Basic/OpenCLImageTypes.def"
7008 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7009     case PREDEF_TYPE_##Id##_ID: \
7010       T = Context.Id##Ty; \
7011       break;
7012 #include "clang/Basic/OpenCLExtensionTypes.def"
7013     case PREDEF_TYPE_SAMPLER_ID:
7014       T = Context.OCLSamplerTy;
7015       break;
7016     case PREDEF_TYPE_EVENT_ID:
7017       T = Context.OCLEventTy;
7018       break;
7019     case PREDEF_TYPE_CLK_EVENT_ID:
7020       T = Context.OCLClkEventTy;
7021       break;
7022     case PREDEF_TYPE_QUEUE_ID:
7023       T = Context.OCLQueueTy;
7024       break;
7025     case PREDEF_TYPE_RESERVE_ID_ID:
7026       T = Context.OCLReserveIDTy;
7027       break;
7028     case PREDEF_TYPE_AUTO_DEDUCT:
7029       T = Context.getAutoDeductType();
7030       break;
7031     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7032       T = Context.getAutoRRefDeductType();
7033       break;
7034     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7035       T = Context.ARCUnbridgedCastTy;
7036       break;
7037     case PREDEF_TYPE_BUILTIN_FN:
7038       T = Context.BuiltinFnTy;
7039       break;
7040     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7041       T = Context.IncompleteMatrixIdxTy;
7042       break;
7043     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7044       T = Context.OMPArraySectionTy;
7045       break;
7046     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7047       T = Context.OMPArraySectionTy;
7048       break;
7049     case PREDEF_TYPE_OMP_ITERATOR:
7050       T = Context.OMPIteratorTy;
7051       break;
7052 #define SVE_TYPE(Name, Id, SingletonId) \
7053     case PREDEF_TYPE_##Id##_ID: \
7054       T = Context.SingletonId; \
7055       break;
7056 #include "clang/Basic/AArch64SVEACLETypes.def"
7057 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7058     case PREDEF_TYPE_##Id##_ID: \
7059       T = Context.Id##Ty; \
7060       break;
7061 #include "clang/Basic/PPCTypes.def"
7062 #define RVV_TYPE(Name, Id, SingletonId) \
7063     case PREDEF_TYPE_##Id##_ID: \
7064       T = Context.SingletonId; \
7065       break;
7066 #include "clang/Basic/RISCVVTypes.def"
7067     }
7068 
7069     assert(!T.isNull() && "Unknown predefined type");
7070     return T.withFastQualifiers(FastQuals);
7071   }
7072 
7073   Index -= NUM_PREDEF_TYPE_IDS;
7074   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7075   if (TypesLoaded[Index].isNull()) {
7076     TypesLoaded[Index] = readTypeRecord(Index);
7077     if (TypesLoaded[Index].isNull())
7078       return QualType();
7079 
7080     TypesLoaded[Index]->setFromAST();
7081     if (DeserializationListener)
7082       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7083                                         TypesLoaded[Index]);
7084   }
7085 
7086   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7087 }
7088 
7089 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7090   return GetType(getGlobalTypeID(F, LocalID));
7091 }
7092 
7093 serialization::TypeID
7094 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7095   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7096   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7097 
7098   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7099     return LocalID;
7100 
7101   if (!F.ModuleOffsetMap.empty())
7102     ReadModuleOffsetMap(F);
7103 
7104   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7105     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7106   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7107 
7108   unsigned GlobalIndex = LocalIndex + I->second;
7109   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7110 }
7111 
7112 TemplateArgumentLocInfo
7113 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7114   switch (Kind) {
7115   case TemplateArgument::Expression:
7116     return readExpr();
7117   case TemplateArgument::Type:
7118     return readTypeSourceInfo();
7119   case TemplateArgument::Template: {
7120     NestedNameSpecifierLoc QualifierLoc =
7121       readNestedNameSpecifierLoc();
7122     SourceLocation TemplateNameLoc = readSourceLocation();
7123     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7124                                    TemplateNameLoc, SourceLocation());
7125   }
7126   case TemplateArgument::TemplateExpansion: {
7127     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7128     SourceLocation TemplateNameLoc = readSourceLocation();
7129     SourceLocation EllipsisLoc = readSourceLocation();
7130     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7131                                    TemplateNameLoc, EllipsisLoc);
7132   }
7133   case TemplateArgument::Null:
7134   case TemplateArgument::Integral:
7135   case TemplateArgument::Declaration:
7136   case TemplateArgument::NullPtr:
7137   case TemplateArgument::Pack:
7138     // FIXME: Is this right?
7139     return TemplateArgumentLocInfo();
7140   }
7141   llvm_unreachable("unexpected template argument loc");
7142 }
7143 
7144 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7145   TemplateArgument Arg = readTemplateArgument();
7146 
7147   if (Arg.getKind() == TemplateArgument::Expression) {
7148     if (readBool()) // bool InfoHasSameExpr.
7149       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7150   }
7151   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7152 }
7153 
7154 const ASTTemplateArgumentListInfo *
7155 ASTRecordReader::readASTTemplateArgumentListInfo() {
7156   SourceLocation LAngleLoc = readSourceLocation();
7157   SourceLocation RAngleLoc = readSourceLocation();
7158   unsigned NumArgsAsWritten = readInt();
7159   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7160   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7161     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7162   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7163 }
7164 
7165 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7166   return GetDecl(ID);
7167 }
7168 
7169 void ASTReader::CompleteRedeclChain(const Decl *D) {
7170   if (NumCurrentElementsDeserializing) {
7171     // We arrange to not care about the complete redeclaration chain while we're
7172     // deserializing. Just remember that the AST has marked this one as complete
7173     // but that it's not actually complete yet, so we know we still need to
7174     // complete it later.
7175     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7176     return;
7177   }
7178 
7179   if (!D->getDeclContext()) {
7180     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7181     return;
7182   }
7183 
7184   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7185 
7186   // If this is a named declaration, complete it by looking it up
7187   // within its context.
7188   //
7189   // FIXME: Merging a function definition should merge
7190   // all mergeable entities within it.
7191   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7192       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7193     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7194       if (!getContext().getLangOpts().CPlusPlus &&
7195           isa<TranslationUnitDecl>(DC)) {
7196         // Outside of C++, we don't have a lookup table for the TU, so update
7197         // the identifier instead. (For C++ modules, we don't store decls
7198         // in the serialized identifier table, so we do the lookup in the TU.)
7199         auto *II = Name.getAsIdentifierInfo();
7200         assert(II && "non-identifier name in C?");
7201         if (II->isOutOfDate())
7202           updateOutOfDateIdentifier(*II);
7203       } else
7204         DC->lookup(Name);
7205     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7206       // Find all declarations of this kind from the relevant context.
7207       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7208         auto *DC = cast<DeclContext>(DCDecl);
7209         SmallVector<Decl*, 8> Decls;
7210         FindExternalLexicalDecls(
7211             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7212       }
7213     }
7214   }
7215 
7216   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7217     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7218   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7219     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7220   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7221     if (auto *Template = FD->getPrimaryTemplate())
7222       Template->LoadLazySpecializations();
7223   }
7224 }
7225 
7226 CXXCtorInitializer **
7227 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7228   RecordLocation Loc = getLocalBitOffset(Offset);
7229   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7230   SavedStreamPosition SavedPosition(Cursor);
7231   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7232     Error(std::move(Err));
7233     return nullptr;
7234   }
7235   ReadingKindTracker ReadingKind(Read_Decl, *this);
7236 
7237   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7238   if (!MaybeCode) {
7239     Error(MaybeCode.takeError());
7240     return nullptr;
7241   }
7242   unsigned Code = MaybeCode.get();
7243 
7244   ASTRecordReader Record(*this, *Loc.F);
7245   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7246   if (!MaybeRecCode) {
7247     Error(MaybeRecCode.takeError());
7248     return nullptr;
7249   }
7250   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7251     Error("malformed AST file: missing C++ ctor initializers");
7252     return nullptr;
7253   }
7254 
7255   return Record.readCXXCtorInitializers();
7256 }
7257 
7258 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7259   assert(ContextObj && "reading base specifiers with no AST context");
7260   ASTContext &Context = *ContextObj;
7261 
7262   RecordLocation Loc = getLocalBitOffset(Offset);
7263   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7264   SavedStreamPosition SavedPosition(Cursor);
7265   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7266     Error(std::move(Err));
7267     return nullptr;
7268   }
7269   ReadingKindTracker ReadingKind(Read_Decl, *this);
7270 
7271   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7272   if (!MaybeCode) {
7273     Error(MaybeCode.takeError());
7274     return nullptr;
7275   }
7276   unsigned Code = MaybeCode.get();
7277 
7278   ASTRecordReader Record(*this, *Loc.F);
7279   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7280   if (!MaybeRecCode) {
7281     Error(MaybeCode.takeError());
7282     return nullptr;
7283   }
7284   unsigned RecCode = MaybeRecCode.get();
7285 
7286   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7287     Error("malformed AST file: missing C++ base specifiers");
7288     return nullptr;
7289   }
7290 
7291   unsigned NumBases = Record.readInt();
7292   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7293   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7294   for (unsigned I = 0; I != NumBases; ++I)
7295     Bases[I] = Record.readCXXBaseSpecifier();
7296   return Bases;
7297 }
7298 
7299 serialization::DeclID
7300 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7301   if (LocalID < NUM_PREDEF_DECL_IDS)
7302     return LocalID;
7303 
7304   if (!F.ModuleOffsetMap.empty())
7305     ReadModuleOffsetMap(F);
7306 
7307   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7308     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7309   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7310 
7311   return LocalID + I->second;
7312 }
7313 
7314 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7315                                    ModuleFile &M) const {
7316   // Predefined decls aren't from any module.
7317   if (ID < NUM_PREDEF_DECL_IDS)
7318     return false;
7319 
7320   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7321          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7322 }
7323 
7324 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7325   if (!D->isFromASTFile())
7326     return nullptr;
7327   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7328   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7329   return I->second;
7330 }
7331 
7332 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7333   if (ID < NUM_PREDEF_DECL_IDS)
7334     return SourceLocation();
7335 
7336   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7337 
7338   if (Index > DeclsLoaded.size()) {
7339     Error("declaration ID out-of-range for AST file");
7340     return SourceLocation();
7341   }
7342 
7343   if (Decl *D = DeclsLoaded[Index])
7344     return D->getLocation();
7345 
7346   SourceLocation Loc;
7347   DeclCursorForID(ID, Loc);
7348   return Loc;
7349 }
7350 
7351 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7352   switch (ID) {
7353   case PREDEF_DECL_NULL_ID:
7354     return nullptr;
7355 
7356   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7357     return Context.getTranslationUnitDecl();
7358 
7359   case PREDEF_DECL_OBJC_ID_ID:
7360     return Context.getObjCIdDecl();
7361 
7362   case PREDEF_DECL_OBJC_SEL_ID:
7363     return Context.getObjCSelDecl();
7364 
7365   case PREDEF_DECL_OBJC_CLASS_ID:
7366     return Context.getObjCClassDecl();
7367 
7368   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7369     return Context.getObjCProtocolDecl();
7370 
7371   case PREDEF_DECL_INT_128_ID:
7372     return Context.getInt128Decl();
7373 
7374   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7375     return Context.getUInt128Decl();
7376 
7377   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7378     return Context.getObjCInstanceTypeDecl();
7379 
7380   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7381     return Context.getBuiltinVaListDecl();
7382 
7383   case PREDEF_DECL_VA_LIST_TAG:
7384     return Context.getVaListTagDecl();
7385 
7386   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7387     return Context.getBuiltinMSVaListDecl();
7388 
7389   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7390     return Context.getMSGuidTagDecl();
7391 
7392   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7393     return Context.getExternCContextDecl();
7394 
7395   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7396     return Context.getMakeIntegerSeqDecl();
7397 
7398   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7399     return Context.getCFConstantStringDecl();
7400 
7401   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7402     return Context.getCFConstantStringTagDecl();
7403 
7404   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7405     return Context.getTypePackElementDecl();
7406   }
7407   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7408 }
7409 
7410 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7411   assert(ContextObj && "reading decl with no AST context");
7412   if (ID < NUM_PREDEF_DECL_IDS) {
7413     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7414     if (D) {
7415       // Track that we have merged the declaration with ID \p ID into the
7416       // pre-existing predefined declaration \p D.
7417       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7418       if (Merged.empty())
7419         Merged.push_back(ID);
7420     }
7421     return D;
7422   }
7423 
7424   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7425 
7426   if (Index >= DeclsLoaded.size()) {
7427     assert(0 && "declaration ID out-of-range for AST file");
7428     Error("declaration ID out-of-range for AST file");
7429     return nullptr;
7430   }
7431 
7432   return DeclsLoaded[Index];
7433 }
7434 
7435 Decl *ASTReader::GetDecl(DeclID ID) {
7436   if (ID < NUM_PREDEF_DECL_IDS)
7437     return GetExistingDecl(ID);
7438 
7439   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7440 
7441   if (Index >= DeclsLoaded.size()) {
7442     assert(0 && "declaration ID out-of-range for AST file");
7443     Error("declaration ID out-of-range for AST file");
7444     return nullptr;
7445   }
7446 
7447   if (!DeclsLoaded[Index]) {
7448     ReadDeclRecord(ID);
7449     if (DeserializationListener)
7450       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7451   }
7452 
7453   return DeclsLoaded[Index];
7454 }
7455 
7456 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7457                                                   DeclID GlobalID) {
7458   if (GlobalID < NUM_PREDEF_DECL_IDS)
7459     return GlobalID;
7460 
7461   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7462   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7463   ModuleFile *Owner = I->second;
7464 
7465   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7466     = M.GlobalToLocalDeclIDs.find(Owner);
7467   if (Pos == M.GlobalToLocalDeclIDs.end())
7468     return 0;
7469 
7470   return GlobalID - Owner->BaseDeclID + Pos->second;
7471 }
7472 
7473 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7474                                             const RecordData &Record,
7475                                             unsigned &Idx) {
7476   if (Idx >= Record.size()) {
7477     Error("Corrupted AST file");
7478     return 0;
7479   }
7480 
7481   return getGlobalDeclID(F, Record[Idx++]);
7482 }
7483 
7484 /// Resolve the offset of a statement into a statement.
7485 ///
7486 /// This operation will read a new statement from the external
7487 /// source each time it is called, and is meant to be used via a
7488 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7489 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7490   // Switch case IDs are per Decl.
7491   ClearSwitchCaseIDs();
7492 
7493   // Offset here is a global offset across the entire chain.
7494   RecordLocation Loc = getLocalBitOffset(Offset);
7495   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7496     Error(std::move(Err));
7497     return nullptr;
7498   }
7499   assert(NumCurrentElementsDeserializing == 0 &&
7500          "should not be called while already deserializing");
7501   Deserializing D(this);
7502   return ReadStmtFromStream(*Loc.F);
7503 }
7504 
7505 void ASTReader::FindExternalLexicalDecls(
7506     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7507     SmallVectorImpl<Decl *> &Decls) {
7508   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7509 
7510   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7511     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7512     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7513       auto K = (Decl::Kind)+LexicalDecls[I];
7514       if (!IsKindWeWant(K))
7515         continue;
7516 
7517       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7518 
7519       // Don't add predefined declarations to the lexical context more
7520       // than once.
7521       if (ID < NUM_PREDEF_DECL_IDS) {
7522         if (PredefsVisited[ID])
7523           continue;
7524 
7525         PredefsVisited[ID] = true;
7526       }
7527 
7528       if (Decl *D = GetLocalDecl(*M, ID)) {
7529         assert(D->getKind() == K && "wrong kind for lexical decl");
7530         if (!DC->isDeclInLexicalTraversal(D))
7531           Decls.push_back(D);
7532       }
7533     }
7534   };
7535 
7536   if (isa<TranslationUnitDecl>(DC)) {
7537     for (auto Lexical : TULexicalDecls)
7538       Visit(Lexical.first, Lexical.second);
7539   } else {
7540     auto I = LexicalDecls.find(DC);
7541     if (I != LexicalDecls.end())
7542       Visit(I->second.first, I->second.second);
7543   }
7544 
7545   ++NumLexicalDeclContextsRead;
7546 }
7547 
7548 namespace {
7549 
7550 class DeclIDComp {
7551   ASTReader &Reader;
7552   ModuleFile &Mod;
7553 
7554 public:
7555   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7556 
7557   bool operator()(LocalDeclID L, LocalDeclID R) const {
7558     SourceLocation LHS = getLocation(L);
7559     SourceLocation RHS = getLocation(R);
7560     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7561   }
7562 
7563   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7564     SourceLocation RHS = getLocation(R);
7565     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7566   }
7567 
7568   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7569     SourceLocation LHS = getLocation(L);
7570     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7571   }
7572 
7573   SourceLocation getLocation(LocalDeclID ID) const {
7574     return Reader.getSourceManager().getFileLoc(
7575             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7576   }
7577 };
7578 
7579 } // namespace
7580 
7581 void ASTReader::FindFileRegionDecls(FileID File,
7582                                     unsigned Offset, unsigned Length,
7583                                     SmallVectorImpl<Decl *> &Decls) {
7584   SourceManager &SM = getSourceManager();
7585 
7586   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7587   if (I == FileDeclIDs.end())
7588     return;
7589 
7590   FileDeclsInfo &DInfo = I->second;
7591   if (DInfo.Decls.empty())
7592     return;
7593 
7594   SourceLocation
7595     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7596   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7597 
7598   DeclIDComp DIDComp(*this, *DInfo.Mod);
7599   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7600       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7601   if (BeginIt != DInfo.Decls.begin())
7602     --BeginIt;
7603 
7604   // If we are pointing at a top-level decl inside an objc container, we need
7605   // to backtrack until we find it otherwise we will fail to report that the
7606   // region overlaps with an objc container.
7607   while (BeginIt != DInfo.Decls.begin() &&
7608          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7609              ->isTopLevelDeclInObjCContainer())
7610     --BeginIt;
7611 
7612   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7613       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7614   if (EndIt != DInfo.Decls.end())
7615     ++EndIt;
7616 
7617   for (ArrayRef<serialization::LocalDeclID>::iterator
7618          DIt = BeginIt; DIt != EndIt; ++DIt)
7619     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7620 }
7621 
7622 bool
7623 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7624                                           DeclarationName Name) {
7625   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7626          "DeclContext has no visible decls in storage");
7627   if (!Name)
7628     return false;
7629 
7630   auto It = Lookups.find(DC);
7631   if (It == Lookups.end())
7632     return false;
7633 
7634   Deserializing LookupResults(this);
7635 
7636   // Load the list of declarations.
7637   SmallVector<NamedDecl *, 64> Decls;
7638   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7639   for (DeclID ID : It->second.Table.find(Name)) {
7640     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7641     if (ND->getDeclName() == Name && Found.insert(ND).second)
7642       Decls.push_back(ND);
7643   }
7644 
7645   ++NumVisibleDeclContextsRead;
7646   SetExternalVisibleDeclsForName(DC, Name, Decls);
7647   return !Decls.empty();
7648 }
7649 
7650 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7651   if (!DC->hasExternalVisibleStorage())
7652     return;
7653 
7654   auto It = Lookups.find(DC);
7655   assert(It != Lookups.end() &&
7656          "have external visible storage but no lookup tables");
7657 
7658   DeclsMap Decls;
7659 
7660   for (DeclID ID : It->second.Table.findAll()) {
7661     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7662     Decls[ND->getDeclName()].push_back(ND);
7663   }
7664 
7665   ++NumVisibleDeclContextsRead;
7666 
7667   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7668     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7669   }
7670   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7671 }
7672 
7673 const serialization::reader::DeclContextLookupTable *
7674 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7675   auto I = Lookups.find(Primary);
7676   return I == Lookups.end() ? nullptr : &I->second;
7677 }
7678 
7679 /// Under non-PCH compilation the consumer receives the objc methods
7680 /// before receiving the implementation, and codegen depends on this.
7681 /// We simulate this by deserializing and passing to consumer the methods of the
7682 /// implementation before passing the deserialized implementation decl.
7683 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7684                                        ASTConsumer *Consumer) {
7685   assert(ImplD && Consumer);
7686 
7687   for (auto *I : ImplD->methods())
7688     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7689 
7690   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7691 }
7692 
7693 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7694   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7695     PassObjCImplDeclToConsumer(ImplD, Consumer);
7696   else
7697     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7698 }
7699 
7700 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7701   this->Consumer = Consumer;
7702 
7703   if (Consumer)
7704     PassInterestingDeclsToConsumer();
7705 
7706   if (DeserializationListener)
7707     DeserializationListener->ReaderInitialized(this);
7708 }
7709 
7710 void ASTReader::PrintStats() {
7711   std::fprintf(stderr, "*** AST File Statistics:\n");
7712 
7713   unsigned NumTypesLoaded
7714     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7715                                       QualType());
7716   unsigned NumDeclsLoaded
7717     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7718                                       (Decl *)nullptr);
7719   unsigned NumIdentifiersLoaded
7720     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7721                                             IdentifiersLoaded.end(),
7722                                             (IdentifierInfo *)nullptr);
7723   unsigned NumMacrosLoaded
7724     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7725                                        MacrosLoaded.end(),
7726                                        (MacroInfo *)nullptr);
7727   unsigned NumSelectorsLoaded
7728     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7729                                           SelectorsLoaded.end(),
7730                                           Selector());
7731 
7732   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7733     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7734                  NumSLocEntriesRead, TotalNumSLocEntries,
7735                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7736   if (!TypesLoaded.empty())
7737     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7738                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7739                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7740   if (!DeclsLoaded.empty())
7741     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7742                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7743                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7744   if (!IdentifiersLoaded.empty())
7745     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7746                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7747                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7748   if (!MacrosLoaded.empty())
7749     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7750                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7751                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7752   if (!SelectorsLoaded.empty())
7753     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7754                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7755                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7756   if (TotalNumStatements)
7757     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7758                  NumStatementsRead, TotalNumStatements,
7759                  ((float)NumStatementsRead/TotalNumStatements * 100));
7760   if (TotalNumMacros)
7761     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7762                  NumMacrosRead, TotalNumMacros,
7763                  ((float)NumMacrosRead/TotalNumMacros * 100));
7764   if (TotalLexicalDeclContexts)
7765     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7766                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7767                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7768                   * 100));
7769   if (TotalVisibleDeclContexts)
7770     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7771                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7772                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7773                   * 100));
7774   if (TotalNumMethodPoolEntries)
7775     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7776                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7777                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7778                   * 100));
7779   if (NumMethodPoolLookups)
7780     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7781                  NumMethodPoolHits, NumMethodPoolLookups,
7782                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7783   if (NumMethodPoolTableLookups)
7784     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7785                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7786                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7787                   * 100.0));
7788   if (NumIdentifierLookupHits)
7789     std::fprintf(stderr,
7790                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7791                  NumIdentifierLookupHits, NumIdentifierLookups,
7792                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7793 
7794   if (GlobalIndex) {
7795     std::fprintf(stderr, "\n");
7796     GlobalIndex->printStats();
7797   }
7798 
7799   std::fprintf(stderr, "\n");
7800   dump();
7801   std::fprintf(stderr, "\n");
7802 }
7803 
7804 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7805 LLVM_DUMP_METHOD static void
7806 dumpModuleIDMap(StringRef Name,
7807                 const ContinuousRangeMap<Key, ModuleFile *,
7808                                          InitialCapacity> &Map) {
7809   if (Map.begin() == Map.end())
7810     return;
7811 
7812   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7813 
7814   llvm::errs() << Name << ":\n";
7815   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7816        I != IEnd; ++I) {
7817     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7818       << "\n";
7819   }
7820 }
7821 
7822 LLVM_DUMP_METHOD void ASTReader::dump() {
7823   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7824   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7825   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7826   dumpModuleIDMap("Global type map", GlobalTypeMap);
7827   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7828   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7829   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7830   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7831   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7832   dumpModuleIDMap("Global preprocessed entity map",
7833                   GlobalPreprocessedEntityMap);
7834 
7835   llvm::errs() << "\n*** PCH/Modules Loaded:";
7836   for (ModuleFile &M : ModuleMgr)
7837     M.dump();
7838 }
7839 
7840 /// Return the amount of memory used by memory buffers, breaking down
7841 /// by heap-backed versus mmap'ed memory.
7842 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7843   for (ModuleFile &I : ModuleMgr) {
7844     if (llvm::MemoryBuffer *buf = I.Buffer) {
7845       size_t bytes = buf->getBufferSize();
7846       switch (buf->getBufferKind()) {
7847         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7848           sizes.malloc_bytes += bytes;
7849           break;
7850         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7851           sizes.mmap_bytes += bytes;
7852           break;
7853       }
7854     }
7855   }
7856 }
7857 
7858 void ASTReader::InitializeSema(Sema &S) {
7859   SemaObj = &S;
7860   S.addExternalSource(this);
7861 
7862   // Makes sure any declarations that were deserialized "too early"
7863   // still get added to the identifier's declaration chains.
7864   for (uint64_t ID : PreloadedDeclIDs) {
7865     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7866     pushExternalDeclIntoScope(D, D->getDeclName());
7867   }
7868   PreloadedDeclIDs.clear();
7869 
7870   // FIXME: What happens if these are changed by a module import?
7871   if (!FPPragmaOptions.empty()) {
7872     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7873     FPOptionsOverride NewOverrides =
7874         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7875     SemaObj->CurFPFeatures =
7876         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7877   }
7878 
7879   SemaObj->OpenCLFeatures = OpenCLExtensions;
7880 
7881   UpdateSema();
7882 }
7883 
7884 void ASTReader::UpdateSema() {
7885   assert(SemaObj && "no Sema to update");
7886 
7887   // Load the offsets of the declarations that Sema references.
7888   // They will be lazily deserialized when needed.
7889   if (!SemaDeclRefs.empty()) {
7890     assert(SemaDeclRefs.size() % 3 == 0);
7891     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7892       if (!SemaObj->StdNamespace)
7893         SemaObj->StdNamespace = SemaDeclRefs[I];
7894       if (!SemaObj->StdBadAlloc)
7895         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7896       if (!SemaObj->StdAlignValT)
7897         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7898     }
7899     SemaDeclRefs.clear();
7900   }
7901 
7902   // Update the state of pragmas. Use the same API as if we had encountered the
7903   // pragma in the source.
7904   if(OptimizeOffPragmaLocation.isValid())
7905     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7906   if (PragmaMSStructState != -1)
7907     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7908   if (PointersToMembersPragmaLocation.isValid()) {
7909     SemaObj->ActOnPragmaMSPointersToMembers(
7910         (LangOptions::PragmaMSPointersToMembersKind)
7911             PragmaMSPointersToMembersState,
7912         PointersToMembersPragmaLocation);
7913   }
7914   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7915 
7916   if (PragmaAlignPackCurrentValue) {
7917     // The bottom of the stack might have a default value. It must be adjusted
7918     // to the current value to ensure that the packing state is preserved after
7919     // popping entries that were included/imported from a PCH/module.
7920     bool DropFirst = false;
7921     if (!PragmaAlignPackStack.empty() &&
7922         PragmaAlignPackStack.front().Location.isInvalid()) {
7923       assert(PragmaAlignPackStack.front().Value ==
7924                  SemaObj->AlignPackStack.DefaultValue &&
7925              "Expected a default alignment value");
7926       SemaObj->AlignPackStack.Stack.emplace_back(
7927           PragmaAlignPackStack.front().SlotLabel,
7928           SemaObj->AlignPackStack.CurrentValue,
7929           SemaObj->AlignPackStack.CurrentPragmaLocation,
7930           PragmaAlignPackStack.front().PushLocation);
7931       DropFirst = true;
7932     }
7933     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7934                                  .drop_front(DropFirst ? 1 : 0)) {
7935       SemaObj->AlignPackStack.Stack.emplace_back(
7936           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7937     }
7938     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7939       assert(*PragmaAlignPackCurrentValue ==
7940                  SemaObj->AlignPackStack.DefaultValue &&
7941              "Expected a default align and pack value");
7942       // Keep the current values.
7943     } else {
7944       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7945       SemaObj->AlignPackStack.CurrentPragmaLocation =
7946           PragmaAlignPackCurrentLocation;
7947     }
7948   }
7949   if (FpPragmaCurrentValue) {
7950     // The bottom of the stack might have a default value. It must be adjusted
7951     // to the current value to ensure that fp-pragma state is preserved after
7952     // popping entries that were included/imported from a PCH/module.
7953     bool DropFirst = false;
7954     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7955       assert(FpPragmaStack.front().Value ==
7956                  SemaObj->FpPragmaStack.DefaultValue &&
7957              "Expected a default pragma float_control value");
7958       SemaObj->FpPragmaStack.Stack.emplace_back(
7959           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7960           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7961           FpPragmaStack.front().PushLocation);
7962       DropFirst = true;
7963     }
7964     for (const auto &Entry :
7965          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7966       SemaObj->FpPragmaStack.Stack.emplace_back(
7967           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7968     if (FpPragmaCurrentLocation.isInvalid()) {
7969       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7970              "Expected a default pragma float_control value");
7971       // Keep the current values.
7972     } else {
7973       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7974       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7975     }
7976   }
7977 
7978   // For non-modular AST files, restore visiblity of modules.
7979   for (auto &Import : ImportedModules) {
7980     if (Import.ImportLoc.isInvalid())
7981       continue;
7982     if (Module *Imported = getSubmodule(Import.ID)) {
7983       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7984     }
7985   }
7986 }
7987 
7988 IdentifierInfo *ASTReader::get(StringRef Name) {
7989   // Note that we are loading an identifier.
7990   Deserializing AnIdentifier(this);
7991 
7992   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7993                                   NumIdentifierLookups,
7994                                   NumIdentifierLookupHits);
7995 
7996   // We don't need to do identifier table lookups in C++ modules (we preload
7997   // all interesting declarations, and don't need to use the scope for name
7998   // lookups). Perform the lookup in PCH files, though, since we don't build
7999   // a complete initial identifier table if we're carrying on from a PCH.
8000   if (PP.getLangOpts().CPlusPlus) {
8001     for (auto F : ModuleMgr.pch_modules())
8002       if (Visitor(*F))
8003         break;
8004   } else {
8005     // If there is a global index, look there first to determine which modules
8006     // provably do not have any results for this identifier.
8007     GlobalModuleIndex::HitSet Hits;
8008     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
8009     if (!loadGlobalIndex()) {
8010       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8011         HitsPtr = &Hits;
8012       }
8013     }
8014 
8015     ModuleMgr.visit(Visitor, HitsPtr);
8016   }
8017 
8018   IdentifierInfo *II = Visitor.getIdentifierInfo();
8019   markIdentifierUpToDate(II);
8020   return II;
8021 }
8022 
8023 namespace clang {
8024 
8025   /// An identifier-lookup iterator that enumerates all of the
8026   /// identifiers stored within a set of AST files.
8027   class ASTIdentifierIterator : public IdentifierIterator {
8028     /// The AST reader whose identifiers are being enumerated.
8029     const ASTReader &Reader;
8030 
8031     /// The current index into the chain of AST files stored in
8032     /// the AST reader.
8033     unsigned Index;
8034 
8035     /// The current position within the identifier lookup table
8036     /// of the current AST file.
8037     ASTIdentifierLookupTable::key_iterator Current;
8038 
8039     /// The end position within the identifier lookup table of
8040     /// the current AST file.
8041     ASTIdentifierLookupTable::key_iterator End;
8042 
8043     /// Whether to skip any modules in the ASTReader.
8044     bool SkipModules;
8045 
8046   public:
8047     explicit ASTIdentifierIterator(const ASTReader &Reader,
8048                                    bool SkipModules = false);
8049 
8050     StringRef Next() override;
8051   };
8052 
8053 } // namespace clang
8054 
8055 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8056                                              bool SkipModules)
8057     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8058 }
8059 
8060 StringRef ASTIdentifierIterator::Next() {
8061   while (Current == End) {
8062     // If we have exhausted all of our AST files, we're done.
8063     if (Index == 0)
8064       return StringRef();
8065 
8066     --Index;
8067     ModuleFile &F = Reader.ModuleMgr[Index];
8068     if (SkipModules && F.isModule())
8069       continue;
8070 
8071     ASTIdentifierLookupTable *IdTable =
8072         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8073     Current = IdTable->key_begin();
8074     End = IdTable->key_end();
8075   }
8076 
8077   // We have any identifiers remaining in the current AST file; return
8078   // the next one.
8079   StringRef Result = *Current;
8080   ++Current;
8081   return Result;
8082 }
8083 
8084 namespace {
8085 
8086 /// A utility for appending two IdentifierIterators.
8087 class ChainedIdentifierIterator : public IdentifierIterator {
8088   std::unique_ptr<IdentifierIterator> Current;
8089   std::unique_ptr<IdentifierIterator> Queued;
8090 
8091 public:
8092   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8093                             std::unique_ptr<IdentifierIterator> Second)
8094       : Current(std::move(First)), Queued(std::move(Second)) {}
8095 
8096   StringRef Next() override {
8097     if (!Current)
8098       return StringRef();
8099 
8100     StringRef result = Current->Next();
8101     if (!result.empty())
8102       return result;
8103 
8104     // Try the queued iterator, which may itself be empty.
8105     Current.reset();
8106     std::swap(Current, Queued);
8107     return Next();
8108   }
8109 };
8110 
8111 } // namespace
8112 
8113 IdentifierIterator *ASTReader::getIdentifiers() {
8114   if (!loadGlobalIndex()) {
8115     std::unique_ptr<IdentifierIterator> ReaderIter(
8116         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8117     std::unique_ptr<IdentifierIterator> ModulesIter(
8118         GlobalIndex->createIdentifierIterator());
8119     return new ChainedIdentifierIterator(std::move(ReaderIter),
8120                                          std::move(ModulesIter));
8121   }
8122 
8123   return new ASTIdentifierIterator(*this);
8124 }
8125 
8126 namespace clang {
8127 namespace serialization {
8128 
8129   class ReadMethodPoolVisitor {
8130     ASTReader &Reader;
8131     Selector Sel;
8132     unsigned PriorGeneration;
8133     unsigned InstanceBits = 0;
8134     unsigned FactoryBits = 0;
8135     bool InstanceHasMoreThanOneDecl = false;
8136     bool FactoryHasMoreThanOneDecl = false;
8137     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8138     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8139 
8140   public:
8141     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8142                           unsigned PriorGeneration)
8143         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8144 
8145     bool operator()(ModuleFile &M) {
8146       if (!M.SelectorLookupTable)
8147         return false;
8148 
8149       // If we've already searched this module file, skip it now.
8150       if (M.Generation <= PriorGeneration)
8151         return true;
8152 
8153       ++Reader.NumMethodPoolTableLookups;
8154       ASTSelectorLookupTable *PoolTable
8155         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8156       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8157       if (Pos == PoolTable->end())
8158         return false;
8159 
8160       ++Reader.NumMethodPoolTableHits;
8161       ++Reader.NumSelectorsRead;
8162       // FIXME: Not quite happy with the statistics here. We probably should
8163       // disable this tracking when called via LoadSelector.
8164       // Also, should entries without methods count as misses?
8165       ++Reader.NumMethodPoolEntriesRead;
8166       ASTSelectorLookupTrait::data_type Data = *Pos;
8167       if (Reader.DeserializationListener)
8168         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8169 
8170       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8171       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8172       InstanceBits = Data.InstanceBits;
8173       FactoryBits = Data.FactoryBits;
8174       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8175       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8176       return true;
8177     }
8178 
8179     /// Retrieve the instance methods found by this visitor.
8180     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8181       return InstanceMethods;
8182     }
8183 
8184     /// Retrieve the instance methods found by this visitor.
8185     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8186       return FactoryMethods;
8187     }
8188 
8189     unsigned getInstanceBits() const { return InstanceBits; }
8190     unsigned getFactoryBits() const { return FactoryBits; }
8191 
8192     bool instanceHasMoreThanOneDecl() const {
8193       return InstanceHasMoreThanOneDecl;
8194     }
8195 
8196     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8197   };
8198 
8199 } // namespace serialization
8200 } // namespace clang
8201 
8202 /// Add the given set of methods to the method list.
8203 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8204                              ObjCMethodList &List) {
8205   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8206     S.addMethodToGlobalList(&List, Methods[I]);
8207   }
8208 }
8209 
8210 void ASTReader::ReadMethodPool(Selector Sel) {
8211   // Get the selector generation and update it to the current generation.
8212   unsigned &Generation = SelectorGeneration[Sel];
8213   unsigned PriorGeneration = Generation;
8214   Generation = getGeneration();
8215   SelectorOutOfDate[Sel] = false;
8216 
8217   // Search for methods defined with this selector.
8218   ++NumMethodPoolLookups;
8219   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8220   ModuleMgr.visit(Visitor);
8221 
8222   if (Visitor.getInstanceMethods().empty() &&
8223       Visitor.getFactoryMethods().empty())
8224     return;
8225 
8226   ++NumMethodPoolHits;
8227 
8228   if (!getSema())
8229     return;
8230 
8231   Sema &S = *getSema();
8232   Sema::GlobalMethodPool::iterator Pos
8233     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8234 
8235   Pos->second.first.setBits(Visitor.getInstanceBits());
8236   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8237   Pos->second.second.setBits(Visitor.getFactoryBits());
8238   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8239 
8240   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8241   // when building a module we keep every method individually and may need to
8242   // update hasMoreThanOneDecl as we add the methods.
8243   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8244   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8245 }
8246 
8247 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8248   if (SelectorOutOfDate[Sel])
8249     ReadMethodPool(Sel);
8250 }
8251 
8252 void ASTReader::ReadKnownNamespaces(
8253                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8254   Namespaces.clear();
8255 
8256   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8257     if (NamespaceDecl *Namespace
8258                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8259       Namespaces.push_back(Namespace);
8260   }
8261 }
8262 
8263 void ASTReader::ReadUndefinedButUsed(
8264     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8265   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8266     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8267     SourceLocation Loc =
8268         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8269     Undefined.insert(std::make_pair(D, Loc));
8270   }
8271 }
8272 
8273 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8274     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8275                                                      Exprs) {
8276   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8277     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8278     uint64_t Count = DelayedDeleteExprs[Idx++];
8279     for (uint64_t C = 0; C < Count; ++C) {
8280       SourceLocation DeleteLoc =
8281           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8282       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8283       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8284     }
8285   }
8286 }
8287 
8288 void ASTReader::ReadTentativeDefinitions(
8289                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8290   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8291     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8292     if (Var)
8293       TentativeDefs.push_back(Var);
8294   }
8295   TentativeDefinitions.clear();
8296 }
8297 
8298 void ASTReader::ReadUnusedFileScopedDecls(
8299                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8300   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8301     DeclaratorDecl *D
8302       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8303     if (D)
8304       Decls.push_back(D);
8305   }
8306   UnusedFileScopedDecls.clear();
8307 }
8308 
8309 void ASTReader::ReadDelegatingConstructors(
8310                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8311   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8312     CXXConstructorDecl *D
8313       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8314     if (D)
8315       Decls.push_back(D);
8316   }
8317   DelegatingCtorDecls.clear();
8318 }
8319 
8320 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8321   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8322     TypedefNameDecl *D
8323       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8324     if (D)
8325       Decls.push_back(D);
8326   }
8327   ExtVectorDecls.clear();
8328 }
8329 
8330 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8331     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8332   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8333        ++I) {
8334     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8335         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8336     if (D)
8337       Decls.insert(D);
8338   }
8339   UnusedLocalTypedefNameCandidates.clear();
8340 }
8341 
8342 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8343     llvm::SmallSetVector<Decl *, 4> &Decls) {
8344   for (auto I : DeclsToCheckForDeferredDiags) {
8345     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8346     if (D)
8347       Decls.insert(D);
8348   }
8349   DeclsToCheckForDeferredDiags.clear();
8350 }
8351 
8352 void ASTReader::ReadReferencedSelectors(
8353        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8354   if (ReferencedSelectorsData.empty())
8355     return;
8356 
8357   // If there are @selector references added them to its pool. This is for
8358   // implementation of -Wselector.
8359   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8360   unsigned I = 0;
8361   while (I < DataSize) {
8362     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8363     SourceLocation SelLoc
8364       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8365     Sels.push_back(std::make_pair(Sel, SelLoc));
8366   }
8367   ReferencedSelectorsData.clear();
8368 }
8369 
8370 void ASTReader::ReadWeakUndeclaredIdentifiers(
8371        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8372   if (WeakUndeclaredIdentifiers.empty())
8373     return;
8374 
8375   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8376     IdentifierInfo *WeakId
8377       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8378     IdentifierInfo *AliasId
8379       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8380     SourceLocation Loc
8381       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8382     bool Used = WeakUndeclaredIdentifiers[I++];
8383     WeakInfo WI(AliasId, Loc);
8384     WI.setUsed(Used);
8385     WeakIDs.push_back(std::make_pair(WeakId, WI));
8386   }
8387   WeakUndeclaredIdentifiers.clear();
8388 }
8389 
8390 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8391   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8392     ExternalVTableUse VT;
8393     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8394     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8395     VT.DefinitionRequired = VTableUses[Idx++];
8396     VTables.push_back(VT);
8397   }
8398 
8399   VTableUses.clear();
8400 }
8401 
8402 void ASTReader::ReadPendingInstantiations(
8403        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8404   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8405     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8406     SourceLocation Loc
8407       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8408 
8409     Pending.push_back(std::make_pair(D, Loc));
8410   }
8411   PendingInstantiations.clear();
8412 }
8413 
8414 void ASTReader::ReadLateParsedTemplates(
8415     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8416         &LPTMap) {
8417   for (auto &LPT : LateParsedTemplates) {
8418     ModuleFile *FMod = LPT.first;
8419     RecordDataImpl &LateParsed = LPT.second;
8420     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8421          /* In loop */) {
8422       FunctionDecl *FD =
8423           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8424 
8425       auto LT = std::make_unique<LateParsedTemplate>();
8426       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8427 
8428       ModuleFile *F = getOwningModuleFile(LT->D);
8429       assert(F && "No module");
8430 
8431       unsigned TokN = LateParsed[Idx++];
8432       LT->Toks.reserve(TokN);
8433       for (unsigned T = 0; T < TokN; ++T)
8434         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8435 
8436       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8437     }
8438   }
8439 }
8440 
8441 void ASTReader::LoadSelector(Selector Sel) {
8442   // It would be complicated to avoid reading the methods anyway. So don't.
8443   ReadMethodPool(Sel);
8444 }
8445 
8446 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8447   assert(ID && "Non-zero identifier ID required");
8448   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8449   IdentifiersLoaded[ID - 1] = II;
8450   if (DeserializationListener)
8451     DeserializationListener->IdentifierRead(ID, II);
8452 }
8453 
8454 /// Set the globally-visible declarations associated with the given
8455 /// identifier.
8456 ///
8457 /// If the AST reader is currently in a state where the given declaration IDs
8458 /// cannot safely be resolved, they are queued until it is safe to resolve
8459 /// them.
8460 ///
8461 /// \param II an IdentifierInfo that refers to one or more globally-visible
8462 /// declarations.
8463 ///
8464 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8465 /// visible at global scope.
8466 ///
8467 /// \param Decls if non-null, this vector will be populated with the set of
8468 /// deserialized declarations. These declarations will not be pushed into
8469 /// scope.
8470 void
8471 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8472                               const SmallVectorImpl<uint32_t> &DeclIDs,
8473                                    SmallVectorImpl<Decl *> *Decls) {
8474   if (NumCurrentElementsDeserializing && !Decls) {
8475     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8476     return;
8477   }
8478 
8479   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8480     if (!SemaObj) {
8481       // Queue this declaration so that it will be added to the
8482       // translation unit scope and identifier's declaration chain
8483       // once a Sema object is known.
8484       PreloadedDeclIDs.push_back(DeclIDs[I]);
8485       continue;
8486     }
8487 
8488     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8489 
8490     // If we're simply supposed to record the declarations, do so now.
8491     if (Decls) {
8492       Decls->push_back(D);
8493       continue;
8494     }
8495 
8496     // Introduce this declaration into the translation-unit scope
8497     // and add it to the declaration chain for this identifier, so
8498     // that (unqualified) name lookup will find it.
8499     pushExternalDeclIntoScope(D, II);
8500   }
8501 }
8502 
8503 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8504   if (ID == 0)
8505     return nullptr;
8506 
8507   if (IdentifiersLoaded.empty()) {
8508     Error("no identifier table in AST file");
8509     return nullptr;
8510   }
8511 
8512   ID -= 1;
8513   if (!IdentifiersLoaded[ID]) {
8514     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8515     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8516     ModuleFile *M = I->second;
8517     unsigned Index = ID - M->BaseIdentifierID;
8518     const unsigned char *Data =
8519         M->IdentifierTableData + M->IdentifierOffsets[Index];
8520 
8521     ASTIdentifierLookupTrait Trait(*this, *M);
8522     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8523     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8524     auto &II = PP.getIdentifierTable().get(Key);
8525     IdentifiersLoaded[ID] = &II;
8526     markIdentifierFromAST(*this,  II);
8527     if (DeserializationListener)
8528       DeserializationListener->IdentifierRead(ID + 1, &II);
8529   }
8530 
8531   return IdentifiersLoaded[ID];
8532 }
8533 
8534 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8535   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8536 }
8537 
8538 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8539   if (LocalID < NUM_PREDEF_IDENT_IDS)
8540     return LocalID;
8541 
8542   if (!M.ModuleOffsetMap.empty())
8543     ReadModuleOffsetMap(M);
8544 
8545   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8546     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8547   assert(I != M.IdentifierRemap.end()
8548          && "Invalid index into identifier index remap");
8549 
8550   return LocalID + I->second;
8551 }
8552 
8553 MacroInfo *ASTReader::getMacro(MacroID ID) {
8554   if (ID == 0)
8555     return nullptr;
8556 
8557   if (MacrosLoaded.empty()) {
8558     Error("no macro table in AST file");
8559     return nullptr;
8560   }
8561 
8562   ID -= NUM_PREDEF_MACRO_IDS;
8563   if (!MacrosLoaded[ID]) {
8564     GlobalMacroMapType::iterator I
8565       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8566     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8567     ModuleFile *M = I->second;
8568     unsigned Index = ID - M->BaseMacroID;
8569     MacrosLoaded[ID] =
8570         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8571 
8572     if (DeserializationListener)
8573       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8574                                          MacrosLoaded[ID]);
8575   }
8576 
8577   return MacrosLoaded[ID];
8578 }
8579 
8580 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8581   if (LocalID < NUM_PREDEF_MACRO_IDS)
8582     return LocalID;
8583 
8584   if (!M.ModuleOffsetMap.empty())
8585     ReadModuleOffsetMap(M);
8586 
8587   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8588     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8589   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8590 
8591   return LocalID + I->second;
8592 }
8593 
8594 serialization::SubmoduleID
8595 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8596   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8597     return LocalID;
8598 
8599   if (!M.ModuleOffsetMap.empty())
8600     ReadModuleOffsetMap(M);
8601 
8602   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8603     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8604   assert(I != M.SubmoduleRemap.end()
8605          && "Invalid index into submodule index remap");
8606 
8607   return LocalID + I->second;
8608 }
8609 
8610 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8611   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8612     assert(GlobalID == 0 && "Unhandled global submodule ID");
8613     return nullptr;
8614   }
8615 
8616   if (GlobalID > SubmodulesLoaded.size()) {
8617     Error("submodule ID out of range in AST file");
8618     return nullptr;
8619   }
8620 
8621   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8622 }
8623 
8624 Module *ASTReader::getModule(unsigned ID) {
8625   return getSubmodule(ID);
8626 }
8627 
8628 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8629   if (ID & 1) {
8630     // It's a module, look it up by submodule ID.
8631     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8632     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8633   } else {
8634     // It's a prefix (preamble, PCH, ...). Look it up by index.
8635     unsigned IndexFromEnd = ID >> 1;
8636     assert(IndexFromEnd && "got reference to unknown module file");
8637     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8638   }
8639 }
8640 
8641 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8642   if (!F)
8643     return 1;
8644 
8645   // For a file representing a module, use the submodule ID of the top-level
8646   // module as the file ID. For any other kind of file, the number of such
8647   // files loaded beforehand will be the same on reload.
8648   // FIXME: Is this true even if we have an explicit module file and a PCH?
8649   if (F->isModule())
8650     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8651 
8652   auto PCHModules = getModuleManager().pch_modules();
8653   auto I = llvm::find(PCHModules, F);
8654   assert(I != PCHModules.end() && "emitting reference to unknown file");
8655   return (I - PCHModules.end()) << 1;
8656 }
8657 
8658 llvm::Optional<ASTSourceDescriptor>
8659 ASTReader::getSourceDescriptor(unsigned ID) {
8660   if (Module *M = getSubmodule(ID))
8661     return ASTSourceDescriptor(*M);
8662 
8663   // If there is only a single PCH, return it instead.
8664   // Chained PCH are not supported.
8665   const auto &PCHChain = ModuleMgr.pch_modules();
8666   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8667     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8668     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8669     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8670     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8671                                MF.Signature);
8672   }
8673   return None;
8674 }
8675 
8676 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8677   auto I = DefinitionSource.find(FD);
8678   if (I == DefinitionSource.end())
8679     return EK_ReplyHazy;
8680   return I->second ? EK_Never : EK_Always;
8681 }
8682 
8683 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8684   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8685 }
8686 
8687 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8688   if (ID == 0)
8689     return Selector();
8690 
8691   if (ID > SelectorsLoaded.size()) {
8692     Error("selector ID out of range in AST file");
8693     return Selector();
8694   }
8695 
8696   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8697     // Load this selector from the selector table.
8698     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8699     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8700     ModuleFile &M = *I->second;
8701     ASTSelectorLookupTrait Trait(*this, M);
8702     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8703     SelectorsLoaded[ID - 1] =
8704       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8705     if (DeserializationListener)
8706       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8707   }
8708 
8709   return SelectorsLoaded[ID - 1];
8710 }
8711 
8712 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8713   return DecodeSelector(ID);
8714 }
8715 
8716 uint32_t ASTReader::GetNumExternalSelectors() {
8717   // ID 0 (the null selector) is considered an external selector.
8718   return getTotalNumSelectors() + 1;
8719 }
8720 
8721 serialization::SelectorID
8722 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8723   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8724     return LocalID;
8725 
8726   if (!M.ModuleOffsetMap.empty())
8727     ReadModuleOffsetMap(M);
8728 
8729   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8730     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8731   assert(I != M.SelectorRemap.end()
8732          && "Invalid index into selector index remap");
8733 
8734   return LocalID + I->second;
8735 }
8736 
8737 DeclarationNameLoc
8738 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8739   switch (Name.getNameKind()) {
8740   case DeclarationName::CXXConstructorName:
8741   case DeclarationName::CXXDestructorName:
8742   case DeclarationName::CXXConversionFunctionName:
8743     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8744 
8745   case DeclarationName::CXXOperatorName:
8746     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8747 
8748   case DeclarationName::CXXLiteralOperatorName:
8749     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8750         readSourceLocation());
8751 
8752   case DeclarationName::Identifier:
8753   case DeclarationName::ObjCZeroArgSelector:
8754   case DeclarationName::ObjCOneArgSelector:
8755   case DeclarationName::ObjCMultiArgSelector:
8756   case DeclarationName::CXXUsingDirective:
8757   case DeclarationName::CXXDeductionGuideName:
8758     break;
8759   }
8760   return DeclarationNameLoc();
8761 }
8762 
8763 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8764   DeclarationNameInfo NameInfo;
8765   NameInfo.setName(readDeclarationName());
8766   NameInfo.setLoc(readSourceLocation());
8767   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8768   return NameInfo;
8769 }
8770 
8771 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8772   Info.QualifierLoc = readNestedNameSpecifierLoc();
8773   unsigned NumTPLists = readInt();
8774   Info.NumTemplParamLists = NumTPLists;
8775   if (NumTPLists) {
8776     Info.TemplParamLists =
8777         new (getContext()) TemplateParameterList *[NumTPLists];
8778     for (unsigned i = 0; i != NumTPLists; ++i)
8779       Info.TemplParamLists[i] = readTemplateParameterList();
8780   }
8781 }
8782 
8783 TemplateParameterList *
8784 ASTRecordReader::readTemplateParameterList() {
8785   SourceLocation TemplateLoc = readSourceLocation();
8786   SourceLocation LAngleLoc = readSourceLocation();
8787   SourceLocation RAngleLoc = readSourceLocation();
8788 
8789   unsigned NumParams = readInt();
8790   SmallVector<NamedDecl *, 16> Params;
8791   Params.reserve(NumParams);
8792   while (NumParams--)
8793     Params.push_back(readDeclAs<NamedDecl>());
8794 
8795   bool HasRequiresClause = readBool();
8796   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8797 
8798   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8799       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8800   return TemplateParams;
8801 }
8802 
8803 void ASTRecordReader::readTemplateArgumentList(
8804                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8805                         bool Canonicalize) {
8806   unsigned NumTemplateArgs = readInt();
8807   TemplArgs.reserve(NumTemplateArgs);
8808   while (NumTemplateArgs--)
8809     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8810 }
8811 
8812 /// Read a UnresolvedSet structure.
8813 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8814   unsigned NumDecls = readInt();
8815   Set.reserve(getContext(), NumDecls);
8816   while (NumDecls--) {
8817     DeclID ID = readDeclID();
8818     AccessSpecifier AS = (AccessSpecifier) readInt();
8819     Set.addLazyDecl(getContext(), ID, AS);
8820   }
8821 }
8822 
8823 CXXBaseSpecifier
8824 ASTRecordReader::readCXXBaseSpecifier() {
8825   bool isVirtual = readBool();
8826   bool isBaseOfClass = readBool();
8827   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8828   bool inheritConstructors = readBool();
8829   TypeSourceInfo *TInfo = readTypeSourceInfo();
8830   SourceRange Range = readSourceRange();
8831   SourceLocation EllipsisLoc = readSourceLocation();
8832   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8833                           EllipsisLoc);
8834   Result.setInheritConstructors(inheritConstructors);
8835   return Result;
8836 }
8837 
8838 CXXCtorInitializer **
8839 ASTRecordReader::readCXXCtorInitializers() {
8840   ASTContext &Context = getContext();
8841   unsigned NumInitializers = readInt();
8842   assert(NumInitializers && "wrote ctor initializers but have no inits");
8843   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8844   for (unsigned i = 0; i != NumInitializers; ++i) {
8845     TypeSourceInfo *TInfo = nullptr;
8846     bool IsBaseVirtual = false;
8847     FieldDecl *Member = nullptr;
8848     IndirectFieldDecl *IndirectMember = nullptr;
8849 
8850     CtorInitializerType Type = (CtorInitializerType) readInt();
8851     switch (Type) {
8852     case CTOR_INITIALIZER_BASE:
8853       TInfo = readTypeSourceInfo();
8854       IsBaseVirtual = readBool();
8855       break;
8856 
8857     case CTOR_INITIALIZER_DELEGATING:
8858       TInfo = readTypeSourceInfo();
8859       break;
8860 
8861      case CTOR_INITIALIZER_MEMBER:
8862       Member = readDeclAs<FieldDecl>();
8863       break;
8864 
8865      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8866       IndirectMember = readDeclAs<IndirectFieldDecl>();
8867       break;
8868     }
8869 
8870     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8871     Expr *Init = readExpr();
8872     SourceLocation LParenLoc = readSourceLocation();
8873     SourceLocation RParenLoc = readSourceLocation();
8874 
8875     CXXCtorInitializer *BOMInit;
8876     if (Type == CTOR_INITIALIZER_BASE)
8877       BOMInit = new (Context)
8878           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8879                              RParenLoc, MemberOrEllipsisLoc);
8880     else if (Type == CTOR_INITIALIZER_DELEGATING)
8881       BOMInit = new (Context)
8882           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8883     else if (Member)
8884       BOMInit = new (Context)
8885           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8886                              Init, RParenLoc);
8887     else
8888       BOMInit = new (Context)
8889           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8890                              LParenLoc, Init, RParenLoc);
8891 
8892     if (/*IsWritten*/readBool()) {
8893       unsigned SourceOrder = readInt();
8894       BOMInit->setSourceOrder(SourceOrder);
8895     }
8896 
8897     CtorInitializers[i] = BOMInit;
8898   }
8899 
8900   return CtorInitializers;
8901 }
8902 
8903 NestedNameSpecifierLoc
8904 ASTRecordReader::readNestedNameSpecifierLoc() {
8905   ASTContext &Context = getContext();
8906   unsigned N = readInt();
8907   NestedNameSpecifierLocBuilder Builder;
8908   for (unsigned I = 0; I != N; ++I) {
8909     auto Kind = readNestedNameSpecifierKind();
8910     switch (Kind) {
8911     case NestedNameSpecifier::Identifier: {
8912       IdentifierInfo *II = readIdentifier();
8913       SourceRange Range = readSourceRange();
8914       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8915       break;
8916     }
8917 
8918     case NestedNameSpecifier::Namespace: {
8919       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8920       SourceRange Range = readSourceRange();
8921       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8922       break;
8923     }
8924 
8925     case NestedNameSpecifier::NamespaceAlias: {
8926       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8927       SourceRange Range = readSourceRange();
8928       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8929       break;
8930     }
8931 
8932     case NestedNameSpecifier::TypeSpec:
8933     case NestedNameSpecifier::TypeSpecWithTemplate: {
8934       bool Template = readBool();
8935       TypeSourceInfo *T = readTypeSourceInfo();
8936       if (!T)
8937         return NestedNameSpecifierLoc();
8938       SourceLocation ColonColonLoc = readSourceLocation();
8939 
8940       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8941       Builder.Extend(Context,
8942                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8943                      T->getTypeLoc(), ColonColonLoc);
8944       break;
8945     }
8946 
8947     case NestedNameSpecifier::Global: {
8948       SourceLocation ColonColonLoc = readSourceLocation();
8949       Builder.MakeGlobal(Context, ColonColonLoc);
8950       break;
8951     }
8952 
8953     case NestedNameSpecifier::Super: {
8954       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8955       SourceRange Range = readSourceRange();
8956       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8957       break;
8958     }
8959     }
8960   }
8961 
8962   return Builder.getWithLocInContext(Context);
8963 }
8964 
8965 SourceRange
8966 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8967                            unsigned &Idx) {
8968   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8969   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8970   return SourceRange(beg, end);
8971 }
8972 
8973 /// Read a floating-point value
8974 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8975   return llvm::APFloat(Sem, readAPInt());
8976 }
8977 
8978 // Read a string
8979 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8980   unsigned Len = Record[Idx++];
8981   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8982   Idx += Len;
8983   return Result;
8984 }
8985 
8986 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8987                                 unsigned &Idx) {
8988   std::string Filename = ReadString(Record, Idx);
8989   ResolveImportedPath(F, Filename);
8990   return Filename;
8991 }
8992 
8993 std::string ASTReader::ReadPath(StringRef BaseDirectory,
8994                                 const RecordData &Record, unsigned &Idx) {
8995   std::string Filename = ReadString(Record, Idx);
8996   if (!BaseDirectory.empty())
8997     ResolveImportedPath(Filename, BaseDirectory);
8998   return Filename;
8999 }
9000 
9001 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9002                                          unsigned &Idx) {
9003   unsigned Major = Record[Idx++];
9004   unsigned Minor = Record[Idx++];
9005   unsigned Subminor = Record[Idx++];
9006   if (Minor == 0)
9007     return VersionTuple(Major);
9008   if (Subminor == 0)
9009     return VersionTuple(Major, Minor - 1);
9010   return VersionTuple(Major, Minor - 1, Subminor - 1);
9011 }
9012 
9013 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9014                                           const RecordData &Record,
9015                                           unsigned &Idx) {
9016   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9017   return CXXTemporary::Create(getContext(), Decl);
9018 }
9019 
9020 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9021   return Diag(CurrentImportLoc, DiagID);
9022 }
9023 
9024 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9025   return Diags.Report(Loc, DiagID);
9026 }
9027 
9028 /// Retrieve the identifier table associated with the
9029 /// preprocessor.
9030 IdentifierTable &ASTReader::getIdentifierTable() {
9031   return PP.getIdentifierTable();
9032 }
9033 
9034 /// Record that the given ID maps to the given switch-case
9035 /// statement.
9036 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9037   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9038          "Already have a SwitchCase with this ID");
9039   (*CurrSwitchCaseStmts)[ID] = SC;
9040 }
9041 
9042 /// Retrieve the switch-case statement with the given ID.
9043 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9044   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9045   return (*CurrSwitchCaseStmts)[ID];
9046 }
9047 
9048 void ASTReader::ClearSwitchCaseIDs() {
9049   CurrSwitchCaseStmts->clear();
9050 }
9051 
9052 void ASTReader::ReadComments() {
9053   ASTContext &Context = getContext();
9054   std::vector<RawComment *> Comments;
9055   for (SmallVectorImpl<std::pair<BitstreamCursor,
9056                                  serialization::ModuleFile *>>::iterator
9057        I = CommentsCursors.begin(),
9058        E = CommentsCursors.end();
9059        I != E; ++I) {
9060     Comments.clear();
9061     BitstreamCursor &Cursor = I->first;
9062     serialization::ModuleFile &F = *I->second;
9063     SavedStreamPosition SavedPosition(Cursor);
9064 
9065     RecordData Record;
9066     while (true) {
9067       Expected<llvm::BitstreamEntry> MaybeEntry =
9068           Cursor.advanceSkippingSubblocks(
9069               BitstreamCursor::AF_DontPopBlockAtEnd);
9070       if (!MaybeEntry) {
9071         Error(MaybeEntry.takeError());
9072         return;
9073       }
9074       llvm::BitstreamEntry Entry = MaybeEntry.get();
9075 
9076       switch (Entry.Kind) {
9077       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9078       case llvm::BitstreamEntry::Error:
9079         Error("malformed block record in AST file");
9080         return;
9081       case llvm::BitstreamEntry::EndBlock:
9082         goto NextCursor;
9083       case llvm::BitstreamEntry::Record:
9084         // The interesting case.
9085         break;
9086       }
9087 
9088       // Read a record.
9089       Record.clear();
9090       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9091       if (!MaybeComment) {
9092         Error(MaybeComment.takeError());
9093         return;
9094       }
9095       switch ((CommentRecordTypes)MaybeComment.get()) {
9096       case COMMENTS_RAW_COMMENT: {
9097         unsigned Idx = 0;
9098         SourceRange SR = ReadSourceRange(F, Record, Idx);
9099         RawComment::CommentKind Kind =
9100             (RawComment::CommentKind) Record[Idx++];
9101         bool IsTrailingComment = Record[Idx++];
9102         bool IsAlmostTrailingComment = Record[Idx++];
9103         Comments.push_back(new (Context) RawComment(
9104             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9105         break;
9106       }
9107       }
9108     }
9109   NextCursor:
9110     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9111         FileToOffsetToComment;
9112     for (RawComment *C : Comments) {
9113       SourceLocation CommentLoc = C->getBeginLoc();
9114       if (CommentLoc.isValid()) {
9115         std::pair<FileID, unsigned> Loc =
9116             SourceMgr.getDecomposedLoc(CommentLoc);
9117         if (Loc.first.isValid())
9118           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9119       }
9120     }
9121   }
9122 }
9123 
9124 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9125                                 bool IncludeSystem, bool Complain,
9126                     llvm::function_ref<void(const serialization::InputFile &IF,
9127                                             bool isSystem)> Visitor) {
9128   unsigned NumUserInputs = MF.NumUserInputFiles;
9129   unsigned NumInputs = MF.InputFilesLoaded.size();
9130   assert(NumUserInputs <= NumInputs);
9131   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9132   for (unsigned I = 0; I < N; ++I) {
9133     bool IsSystem = I >= NumUserInputs;
9134     InputFile IF = getInputFile(MF, I+1, Complain);
9135     Visitor(IF, IsSystem);
9136   }
9137 }
9138 
9139 void ASTReader::visitTopLevelModuleMaps(
9140     serialization::ModuleFile &MF,
9141     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9142   unsigned NumInputs = MF.InputFilesLoaded.size();
9143   for (unsigned I = 0; I < NumInputs; ++I) {
9144     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9145     if (IFI.TopLevelModuleMap)
9146       // FIXME: This unnecessarily re-reads the InputFileInfo.
9147       if (auto FE = getInputFile(MF, I + 1).getFile())
9148         Visitor(FE);
9149   }
9150 }
9151 
9152 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9153   // If we know the owning module, use it.
9154   if (Module *M = D->getImportedOwningModule())
9155     return M->getFullModuleName();
9156 
9157   // Otherwise, use the name of the top-level module the decl is within.
9158   if (ModuleFile *M = getOwningModuleFile(D))
9159     return M->ModuleName;
9160 
9161   // Not from a module.
9162   return {};
9163 }
9164 
9165 void ASTReader::finishPendingActions() {
9166   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9167          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9168          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9169          !PendingUpdateRecords.empty()) {
9170     // If any identifiers with corresponding top-level declarations have
9171     // been loaded, load those declarations now.
9172     using TopLevelDeclsMap =
9173         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9174     TopLevelDeclsMap TopLevelDecls;
9175 
9176     while (!PendingIdentifierInfos.empty()) {
9177       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9178       SmallVector<uint32_t, 4> DeclIDs =
9179           std::move(PendingIdentifierInfos.back().second);
9180       PendingIdentifierInfos.pop_back();
9181 
9182       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9183     }
9184 
9185     // Load each function type that we deferred loading because it was a
9186     // deduced type that might refer to a local type declared within itself.
9187     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9188       auto *FD = PendingFunctionTypes[I].first;
9189       FD->setType(GetType(PendingFunctionTypes[I].second));
9190 
9191       // If we gave a function a deduced return type, remember that we need to
9192       // propagate that along the redeclaration chain.
9193       auto *DT = FD->getReturnType()->getContainedDeducedType();
9194       if (DT && DT->isDeduced())
9195         PendingDeducedTypeUpdates.insert(
9196             {FD->getCanonicalDecl(), FD->getReturnType()});
9197     }
9198     PendingFunctionTypes.clear();
9199 
9200     // For each decl chain that we wanted to complete while deserializing, mark
9201     // it as "still needs to be completed".
9202     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9203       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9204     }
9205     PendingIncompleteDeclChains.clear();
9206 
9207     // Load pending declaration chains.
9208     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9209       loadPendingDeclChain(PendingDeclChains[I].first,
9210                            PendingDeclChains[I].second);
9211     PendingDeclChains.clear();
9212 
9213     // Make the most recent of the top-level declarations visible.
9214     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9215            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9216       IdentifierInfo *II = TLD->first;
9217       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9218         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9219       }
9220     }
9221 
9222     // Load any pending macro definitions.
9223     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9224       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9225       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9226       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9227       // Initialize the macro history from chained-PCHs ahead of module imports.
9228       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9229            ++IDIdx) {
9230         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9231         if (!Info.M->isModule())
9232           resolvePendingMacro(II, Info);
9233       }
9234       // Handle module imports.
9235       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9236            ++IDIdx) {
9237         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9238         if (Info.M->isModule())
9239           resolvePendingMacro(II, Info);
9240       }
9241     }
9242     PendingMacroIDs.clear();
9243 
9244     // Wire up the DeclContexts for Decls that we delayed setting until
9245     // recursive loading is completed.
9246     while (!PendingDeclContextInfos.empty()) {
9247       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9248       PendingDeclContextInfos.pop_front();
9249       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9250       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9251       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9252     }
9253 
9254     // Perform any pending declaration updates.
9255     while (!PendingUpdateRecords.empty()) {
9256       auto Update = PendingUpdateRecords.pop_back_val();
9257       ReadingKindTracker ReadingKind(Read_Decl, *this);
9258       loadDeclUpdateRecords(Update);
9259     }
9260   }
9261 
9262   // At this point, all update records for loaded decls are in place, so any
9263   // fake class definitions should have become real.
9264   assert(PendingFakeDefinitionData.empty() &&
9265          "faked up a class definition but never saw the real one");
9266 
9267   // If we deserialized any C++ or Objective-C class definitions, any
9268   // Objective-C protocol definitions, or any redeclarable templates, make sure
9269   // that all redeclarations point to the definitions. Note that this can only
9270   // happen now, after the redeclaration chains have been fully wired.
9271   for (Decl *D : PendingDefinitions) {
9272     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9273       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9274         // Make sure that the TagType points at the definition.
9275         const_cast<TagType*>(TagT)->decl = TD;
9276       }
9277 
9278       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9279         for (auto *R = getMostRecentExistingDecl(RD); R;
9280              R = R->getPreviousDecl()) {
9281           assert((R == D) ==
9282                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9283                  "declaration thinks it's the definition but it isn't");
9284           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9285         }
9286       }
9287 
9288       continue;
9289     }
9290 
9291     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9292       // Make sure that the ObjCInterfaceType points at the definition.
9293       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9294         ->Decl = ID;
9295 
9296       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9297         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9298 
9299       continue;
9300     }
9301 
9302     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9303       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9304         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9305 
9306       continue;
9307     }
9308 
9309     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9310     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9311       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9312   }
9313   PendingDefinitions.clear();
9314 
9315   // Load the bodies of any functions or methods we've encountered. We do
9316   // this now (delayed) so that we can be sure that the declaration chains
9317   // have been fully wired up (hasBody relies on this).
9318   // FIXME: We shouldn't require complete redeclaration chains here.
9319   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9320                                PBEnd = PendingBodies.end();
9321        PB != PBEnd; ++PB) {
9322     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9323       // For a function defined inline within a class template, force the
9324       // canonical definition to be the one inside the canonical definition of
9325       // the template. This ensures that we instantiate from a correct view
9326       // of the template.
9327       //
9328       // Sadly we can't do this more generally: we can't be sure that all
9329       // copies of an arbitrary class definition will have the same members
9330       // defined (eg, some member functions may not be instantiated, and some
9331       // special members may or may not have been implicitly defined).
9332       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9333         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9334           continue;
9335 
9336       // FIXME: Check for =delete/=default?
9337       // FIXME: Complain about ODR violations here?
9338       const FunctionDecl *Defn = nullptr;
9339       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9340         FD->setLazyBody(PB->second);
9341       } else {
9342         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9343         mergeDefinitionVisibility(NonConstDefn, FD);
9344 
9345         if (!FD->isLateTemplateParsed() &&
9346             !NonConstDefn->isLateTemplateParsed() &&
9347             FD->getODRHash() != NonConstDefn->getODRHash()) {
9348           if (!isa<CXXMethodDecl>(FD)) {
9349             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9350           } else if (FD->getLexicalParent()->isFileContext() &&
9351                      NonConstDefn->getLexicalParent()->isFileContext()) {
9352             // Only diagnose out-of-line method definitions.  If they are
9353             // in class definitions, then an error will be generated when
9354             // processing the class bodies.
9355             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9356           }
9357         }
9358       }
9359       continue;
9360     }
9361 
9362     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9363     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9364       MD->setLazyBody(PB->second);
9365   }
9366   PendingBodies.clear();
9367 
9368   // Do some cleanup.
9369   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9370     getContext().deduplicateMergedDefinitonsFor(ND);
9371   PendingMergedDefinitionsToDeduplicate.clear();
9372 }
9373 
9374 void ASTReader::diagnoseOdrViolations() {
9375   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9376       PendingFunctionOdrMergeFailures.empty() &&
9377       PendingEnumOdrMergeFailures.empty())
9378     return;
9379 
9380   // Trigger the import of the full definition of each class that had any
9381   // odr-merging problems, so we can produce better diagnostics for them.
9382   // These updates may in turn find and diagnose some ODR failures, so take
9383   // ownership of the set first.
9384   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9385   PendingOdrMergeFailures.clear();
9386   for (auto &Merge : OdrMergeFailures) {
9387     Merge.first->buildLookup();
9388     Merge.first->decls_begin();
9389     Merge.first->bases_begin();
9390     Merge.first->vbases_begin();
9391     for (auto &RecordPair : Merge.second) {
9392       auto *RD = RecordPair.first;
9393       RD->decls_begin();
9394       RD->bases_begin();
9395       RD->vbases_begin();
9396     }
9397   }
9398 
9399   // Trigger the import of functions.
9400   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9401   PendingFunctionOdrMergeFailures.clear();
9402   for (auto &Merge : FunctionOdrMergeFailures) {
9403     Merge.first->buildLookup();
9404     Merge.first->decls_begin();
9405     Merge.first->getBody();
9406     for (auto &FD : Merge.second) {
9407       FD->buildLookup();
9408       FD->decls_begin();
9409       FD->getBody();
9410     }
9411   }
9412 
9413   // Trigger the import of enums.
9414   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9415   PendingEnumOdrMergeFailures.clear();
9416   for (auto &Merge : EnumOdrMergeFailures) {
9417     Merge.first->decls_begin();
9418     for (auto &Enum : Merge.second) {
9419       Enum->decls_begin();
9420     }
9421   }
9422 
9423   // For each declaration from a merged context, check that the canonical
9424   // definition of that context also contains a declaration of the same
9425   // entity.
9426   //
9427   // Caution: this loop does things that might invalidate iterators into
9428   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9429   while (!PendingOdrMergeChecks.empty()) {
9430     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9431 
9432     // FIXME: Skip over implicit declarations for now. This matters for things
9433     // like implicitly-declared special member functions. This isn't entirely
9434     // correct; we can end up with multiple unmerged declarations of the same
9435     // implicit entity.
9436     if (D->isImplicit())
9437       continue;
9438 
9439     DeclContext *CanonDef = D->getDeclContext();
9440 
9441     bool Found = false;
9442     const Decl *DCanon = D->getCanonicalDecl();
9443 
9444     for (auto RI : D->redecls()) {
9445       if (RI->getLexicalDeclContext() == CanonDef) {
9446         Found = true;
9447         break;
9448       }
9449     }
9450     if (Found)
9451       continue;
9452 
9453     // Quick check failed, time to do the slow thing. Note, we can't just
9454     // look up the name of D in CanonDef here, because the member that is
9455     // in CanonDef might not be found by name lookup (it might have been
9456     // replaced by a more recent declaration in the lookup table), and we
9457     // can't necessarily find it in the redeclaration chain because it might
9458     // be merely mergeable, not redeclarable.
9459     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9460     for (auto *CanonMember : CanonDef->decls()) {
9461       if (CanonMember->getCanonicalDecl() == DCanon) {
9462         // This can happen if the declaration is merely mergeable and not
9463         // actually redeclarable (we looked for redeclarations earlier).
9464         //
9465         // FIXME: We should be able to detect this more efficiently, without
9466         // pulling in all of the members of CanonDef.
9467         Found = true;
9468         break;
9469       }
9470       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9471         if (ND->getDeclName() == D->getDeclName())
9472           Candidates.push_back(ND);
9473     }
9474 
9475     if (!Found) {
9476       // The AST doesn't like TagDecls becoming invalid after they've been
9477       // completed. We only really need to mark FieldDecls as invalid here.
9478       if (!isa<TagDecl>(D))
9479         D->setInvalidDecl();
9480 
9481       // Ensure we don't accidentally recursively enter deserialization while
9482       // we're producing our diagnostic.
9483       Deserializing RecursionGuard(this);
9484 
9485       std::string CanonDefModule =
9486           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9487       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9488         << D << getOwningModuleNameForDiagnostic(D)
9489         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9490 
9491       if (Candidates.empty())
9492         Diag(cast<Decl>(CanonDef)->getLocation(),
9493              diag::note_module_odr_violation_no_possible_decls) << D;
9494       else {
9495         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9496           Diag(Candidates[I]->getLocation(),
9497                diag::note_module_odr_violation_possible_decl)
9498             << Candidates[I];
9499       }
9500 
9501       DiagnosedOdrMergeFailures.insert(CanonDef);
9502     }
9503   }
9504 
9505   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9506       EnumOdrMergeFailures.empty())
9507     return;
9508 
9509   // Ensure we don't accidentally recursively enter deserialization while
9510   // we're producing our diagnostics.
9511   Deserializing RecursionGuard(this);
9512 
9513   // Common code for hashing helpers.
9514   ODRHash Hash;
9515   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9516     Hash.clear();
9517     Hash.AddQualType(Ty);
9518     return Hash.CalculateHash();
9519   };
9520 
9521   auto ComputeODRHash = [&Hash](const Stmt *S) {
9522     assert(S);
9523     Hash.clear();
9524     Hash.AddStmt(S);
9525     return Hash.CalculateHash();
9526   };
9527 
9528   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9529     assert(D);
9530     Hash.clear();
9531     Hash.AddSubDecl(D);
9532     return Hash.CalculateHash();
9533   };
9534 
9535   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9536     Hash.clear();
9537     Hash.AddTemplateArgument(TA);
9538     return Hash.CalculateHash();
9539   };
9540 
9541   auto ComputeTemplateParameterListODRHash =
9542       [&Hash](const TemplateParameterList *TPL) {
9543         assert(TPL);
9544         Hash.clear();
9545         Hash.AddTemplateParameterList(TPL);
9546         return Hash.CalculateHash();
9547       };
9548 
9549   // Used with err_module_odr_violation_mismatch_decl and
9550   // note_module_odr_violation_mismatch_decl
9551   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9552   enum ODRMismatchDecl {
9553     EndOfClass,
9554     PublicSpecifer,
9555     PrivateSpecifer,
9556     ProtectedSpecifer,
9557     StaticAssert,
9558     Field,
9559     CXXMethod,
9560     TypeAlias,
9561     TypeDef,
9562     Var,
9563     Friend,
9564     FunctionTemplate,
9565     Other
9566   };
9567 
9568   // Used with err_module_odr_violation_mismatch_decl_diff and
9569   // note_module_odr_violation_mismatch_decl_diff
9570   enum ODRMismatchDeclDifference {
9571     StaticAssertCondition,
9572     StaticAssertMessage,
9573     StaticAssertOnlyMessage,
9574     FieldName,
9575     FieldTypeName,
9576     FieldSingleBitField,
9577     FieldDifferentWidthBitField,
9578     FieldSingleMutable,
9579     FieldSingleInitializer,
9580     FieldDifferentInitializers,
9581     MethodName,
9582     MethodDeleted,
9583     MethodDefaulted,
9584     MethodVirtual,
9585     MethodStatic,
9586     MethodVolatile,
9587     MethodConst,
9588     MethodInline,
9589     MethodNumberParameters,
9590     MethodParameterType,
9591     MethodParameterName,
9592     MethodParameterSingleDefaultArgument,
9593     MethodParameterDifferentDefaultArgument,
9594     MethodNoTemplateArguments,
9595     MethodDifferentNumberTemplateArguments,
9596     MethodDifferentTemplateArgument,
9597     MethodSingleBody,
9598     MethodDifferentBody,
9599     TypedefName,
9600     TypedefType,
9601     VarName,
9602     VarType,
9603     VarSingleInitializer,
9604     VarDifferentInitializer,
9605     VarConstexpr,
9606     FriendTypeFunction,
9607     FriendType,
9608     FriendFunction,
9609     FunctionTemplateDifferentNumberParameters,
9610     FunctionTemplateParameterDifferentKind,
9611     FunctionTemplateParameterName,
9612     FunctionTemplateParameterSingleDefaultArgument,
9613     FunctionTemplateParameterDifferentDefaultArgument,
9614     FunctionTemplateParameterDifferentType,
9615     FunctionTemplatePackParameter,
9616   };
9617 
9618   // These lambdas have the common portions of the ODR diagnostics.  This
9619   // has the same return as Diag(), so addition parameters can be passed
9620   // in with operator<<
9621   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9622                                  SourceLocation Loc, SourceRange Range,
9623                                  ODRMismatchDeclDifference DiffType) {
9624     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9625            << FirstRecord << FirstModule.empty() << FirstModule << Range
9626            << DiffType;
9627   };
9628   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9629                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9630     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9631            << SecondModule << Range << DiffType;
9632   };
9633 
9634   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9635                        &ComputeQualTypeODRHash, &ComputeODRHash](
9636                           NamedDecl *FirstRecord, StringRef FirstModule,
9637                           StringRef SecondModule, FieldDecl *FirstField,
9638                           FieldDecl *SecondField) {
9639     IdentifierInfo *FirstII = FirstField->getIdentifier();
9640     IdentifierInfo *SecondII = SecondField->getIdentifier();
9641     if (FirstII->getName() != SecondII->getName()) {
9642       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9643                        FirstField->getSourceRange(), FieldName)
9644           << FirstII;
9645       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9646                       SecondField->getSourceRange(), FieldName)
9647           << SecondII;
9648 
9649       return true;
9650     }
9651 
9652     assert(getContext().hasSameType(FirstField->getType(),
9653                                     SecondField->getType()));
9654 
9655     QualType FirstType = FirstField->getType();
9656     QualType SecondType = SecondField->getType();
9657     if (ComputeQualTypeODRHash(FirstType) !=
9658         ComputeQualTypeODRHash(SecondType)) {
9659       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9660                        FirstField->getSourceRange(), FieldTypeName)
9661           << FirstII << FirstType;
9662       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9663                       SecondField->getSourceRange(), FieldTypeName)
9664           << SecondII << SecondType;
9665 
9666       return true;
9667     }
9668 
9669     const bool IsFirstBitField = FirstField->isBitField();
9670     const bool IsSecondBitField = SecondField->isBitField();
9671     if (IsFirstBitField != IsSecondBitField) {
9672       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9673                        FirstField->getSourceRange(), FieldSingleBitField)
9674           << FirstII << IsFirstBitField;
9675       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9676                       SecondField->getSourceRange(), FieldSingleBitField)
9677           << SecondII << IsSecondBitField;
9678       return true;
9679     }
9680 
9681     if (IsFirstBitField && IsSecondBitField) {
9682       unsigned FirstBitWidthHash =
9683           ComputeODRHash(FirstField->getBitWidth());
9684       unsigned SecondBitWidthHash =
9685           ComputeODRHash(SecondField->getBitWidth());
9686       if (FirstBitWidthHash != SecondBitWidthHash) {
9687         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9688                          FirstField->getSourceRange(),
9689                          FieldDifferentWidthBitField)
9690             << FirstII << FirstField->getBitWidth()->getSourceRange();
9691         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9692                         SecondField->getSourceRange(),
9693                         FieldDifferentWidthBitField)
9694             << SecondII << SecondField->getBitWidth()->getSourceRange();
9695         return true;
9696       }
9697     }
9698 
9699     if (!PP.getLangOpts().CPlusPlus)
9700       return false;
9701 
9702     const bool IsFirstMutable = FirstField->isMutable();
9703     const bool IsSecondMutable = SecondField->isMutable();
9704     if (IsFirstMutable != IsSecondMutable) {
9705       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9706                        FirstField->getSourceRange(), FieldSingleMutable)
9707           << FirstII << IsFirstMutable;
9708       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9709                       SecondField->getSourceRange(), FieldSingleMutable)
9710           << SecondII << IsSecondMutable;
9711       return true;
9712     }
9713 
9714     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9715     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9716     if ((!FirstInitializer && SecondInitializer) ||
9717         (FirstInitializer && !SecondInitializer)) {
9718       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9719                        FirstField->getSourceRange(), FieldSingleInitializer)
9720           << FirstII << (FirstInitializer != nullptr);
9721       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9722                       SecondField->getSourceRange(), FieldSingleInitializer)
9723           << SecondII << (SecondInitializer != nullptr);
9724       return true;
9725     }
9726 
9727     if (FirstInitializer && SecondInitializer) {
9728       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9729       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9730       if (FirstInitHash != SecondInitHash) {
9731         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9732                          FirstField->getSourceRange(),
9733                          FieldDifferentInitializers)
9734             << FirstII << FirstInitializer->getSourceRange();
9735         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9736                         SecondField->getSourceRange(),
9737                         FieldDifferentInitializers)
9738             << SecondII << SecondInitializer->getSourceRange();
9739         return true;
9740       }
9741     }
9742 
9743     return false;
9744   };
9745 
9746   auto ODRDiagTypeDefOrAlias =
9747       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9748           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9749           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9750           bool IsTypeAlias) {
9751         auto FirstName = FirstTD->getDeclName();
9752         auto SecondName = SecondTD->getDeclName();
9753         if (FirstName != SecondName) {
9754           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9755                            FirstTD->getSourceRange(), TypedefName)
9756               << IsTypeAlias << FirstName;
9757           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9758                           SecondTD->getSourceRange(), TypedefName)
9759               << IsTypeAlias << SecondName;
9760           return true;
9761         }
9762 
9763         QualType FirstType = FirstTD->getUnderlyingType();
9764         QualType SecondType = SecondTD->getUnderlyingType();
9765         if (ComputeQualTypeODRHash(FirstType) !=
9766             ComputeQualTypeODRHash(SecondType)) {
9767           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9768                            FirstTD->getSourceRange(), TypedefType)
9769               << IsTypeAlias << FirstName << FirstType;
9770           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9771                           SecondTD->getSourceRange(), TypedefType)
9772               << IsTypeAlias << SecondName << SecondType;
9773           return true;
9774         }
9775 
9776         return false;
9777   };
9778 
9779   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9780                      &ComputeQualTypeODRHash, &ComputeODRHash,
9781                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9782                            StringRef SecondModule, VarDecl *FirstVD,
9783                            VarDecl *SecondVD) {
9784     auto FirstName = FirstVD->getDeclName();
9785     auto SecondName = SecondVD->getDeclName();
9786     if (FirstName != SecondName) {
9787       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9788                        FirstVD->getSourceRange(), VarName)
9789           << FirstName;
9790       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9791                       SecondVD->getSourceRange(), VarName)
9792           << SecondName;
9793       return true;
9794     }
9795 
9796     QualType FirstType = FirstVD->getType();
9797     QualType SecondType = SecondVD->getType();
9798     if (ComputeQualTypeODRHash(FirstType) !=
9799         ComputeQualTypeODRHash(SecondType)) {
9800       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9801                        FirstVD->getSourceRange(), VarType)
9802           << FirstName << FirstType;
9803       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9804                       SecondVD->getSourceRange(), VarType)
9805           << SecondName << SecondType;
9806       return true;
9807     }
9808 
9809     if (!PP.getLangOpts().CPlusPlus)
9810       return false;
9811 
9812     const Expr *FirstInit = FirstVD->getInit();
9813     const Expr *SecondInit = SecondVD->getInit();
9814     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9815       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9816                        FirstVD->getSourceRange(), VarSingleInitializer)
9817           << FirstName << (FirstInit == nullptr)
9818           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9819       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9820                       SecondVD->getSourceRange(), VarSingleInitializer)
9821           << SecondName << (SecondInit == nullptr)
9822           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9823       return true;
9824     }
9825 
9826     if (FirstInit && SecondInit &&
9827         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9828       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9829                        FirstVD->getSourceRange(), VarDifferentInitializer)
9830           << FirstName << FirstInit->getSourceRange();
9831       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9832                       SecondVD->getSourceRange(), VarDifferentInitializer)
9833           << SecondName << SecondInit->getSourceRange();
9834       return true;
9835     }
9836 
9837     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9838     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9839     if (FirstIsConstexpr != SecondIsConstexpr) {
9840       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9841                        FirstVD->getSourceRange(), VarConstexpr)
9842           << FirstName << FirstIsConstexpr;
9843       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9844                       SecondVD->getSourceRange(), VarConstexpr)
9845           << SecondName << SecondIsConstexpr;
9846       return true;
9847     }
9848     return false;
9849   };
9850 
9851   auto DifferenceSelector = [](Decl *D) {
9852     assert(D && "valid Decl required");
9853     switch (D->getKind()) {
9854     default:
9855       return Other;
9856     case Decl::AccessSpec:
9857       switch (D->getAccess()) {
9858       case AS_public:
9859         return PublicSpecifer;
9860       case AS_private:
9861         return PrivateSpecifer;
9862       case AS_protected:
9863         return ProtectedSpecifer;
9864       case AS_none:
9865         break;
9866       }
9867       llvm_unreachable("Invalid access specifier");
9868     case Decl::StaticAssert:
9869       return StaticAssert;
9870     case Decl::Field:
9871       return Field;
9872     case Decl::CXXMethod:
9873     case Decl::CXXConstructor:
9874     case Decl::CXXDestructor:
9875       return CXXMethod;
9876     case Decl::TypeAlias:
9877       return TypeAlias;
9878     case Decl::Typedef:
9879       return TypeDef;
9880     case Decl::Var:
9881       return Var;
9882     case Decl::Friend:
9883       return Friend;
9884     case Decl::FunctionTemplate:
9885       return FunctionTemplate;
9886     }
9887   };
9888 
9889   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9890   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9891                                                  RecordDecl *Record,
9892                                                  const DeclContext *DC) {
9893     for (auto *D : Record->decls()) {
9894       if (!ODRHash::isDeclToBeProcessed(D, DC))
9895         continue;
9896       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9897     }
9898   };
9899 
9900   struct DiffResult {
9901     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9902     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9903   };
9904 
9905   // If there is a diagnoseable difference, FirstDiffType and
9906   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9907   // filled in if not EndOfClass.
9908   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9909                                              DeclHashes &SecondHashes) {
9910     DiffResult DR;
9911     auto FirstIt = FirstHashes.begin();
9912     auto SecondIt = SecondHashes.begin();
9913     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9914       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9915           FirstIt->second == SecondIt->second) {
9916         ++FirstIt;
9917         ++SecondIt;
9918         continue;
9919       }
9920 
9921       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9922       DR.SecondDecl =
9923           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9924 
9925       DR.FirstDiffType =
9926           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9927       DR.SecondDiffType =
9928           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9929       return DR;
9930     }
9931     return DR;
9932   };
9933 
9934   // Use this to diagnose that an unexpected Decl was encountered
9935   // or no difference was detected. This causes a generic error
9936   // message to be emitted.
9937   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9938                                       StringRef FirstModule,
9939                                       NamedDecl *SecondRecord,
9940                                       StringRef SecondModule) {
9941     Diag(FirstRecord->getLocation(),
9942          diag::err_module_odr_violation_different_definitions)
9943         << FirstRecord << FirstModule.empty() << FirstModule;
9944 
9945     if (DR.FirstDecl) {
9946       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9947           << FirstRecord << DR.FirstDecl->getSourceRange();
9948     }
9949 
9950     Diag(SecondRecord->getLocation(),
9951          diag::note_module_odr_violation_different_definitions)
9952         << SecondModule;
9953 
9954     if (DR.SecondDecl) {
9955       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9956           << DR.SecondDecl->getSourceRange();
9957     }
9958   };
9959 
9960   auto DiagnoseODRMismatch =
9961       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9962              NamedDecl *SecondRecord, StringRef SecondModule) {
9963         SourceLocation FirstLoc;
9964         SourceRange FirstRange;
9965         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9966         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9967           FirstLoc = FirstTag->getBraceRange().getEnd();
9968         } else {
9969           FirstLoc = DR.FirstDecl->getLocation();
9970           FirstRange = DR.FirstDecl->getSourceRange();
9971         }
9972         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9973             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9974             << DR.FirstDiffType;
9975 
9976         SourceLocation SecondLoc;
9977         SourceRange SecondRange;
9978         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9979         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9980           SecondLoc = SecondTag->getBraceRange().getEnd();
9981         } else {
9982           SecondLoc = DR.SecondDecl->getLocation();
9983           SecondRange = DR.SecondDecl->getSourceRange();
9984         }
9985         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9986             << SecondModule << SecondRange << DR.SecondDiffType;
9987       };
9988 
9989   // Issue any pending ODR-failure diagnostics.
9990   for (auto &Merge : OdrMergeFailures) {
9991     // If we've already pointed out a specific problem with this class, don't
9992     // bother issuing a general "something's different" diagnostic.
9993     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9994       continue;
9995 
9996     bool Diagnosed = false;
9997     CXXRecordDecl *FirstRecord = Merge.first;
9998     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9999     for (auto &RecordPair : Merge.second) {
10000       CXXRecordDecl *SecondRecord = RecordPair.first;
10001       // Multiple different declarations got merged together; tell the user
10002       // where they came from.
10003       if (FirstRecord == SecondRecord)
10004         continue;
10005 
10006       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10007 
10008       auto *FirstDD = FirstRecord->DefinitionData;
10009       auto *SecondDD = RecordPair.second;
10010 
10011       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10012 
10013       // Diagnostics from DefinitionData are emitted here.
10014       if (FirstDD != SecondDD) {
10015         enum ODRDefinitionDataDifference {
10016           NumBases,
10017           NumVBases,
10018           BaseType,
10019           BaseVirtual,
10020           BaseAccess,
10021         };
10022         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10023                                  this](SourceLocation Loc, SourceRange Range,
10024                                        ODRDefinitionDataDifference DiffType) {
10025           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10026                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10027                  << DiffType;
10028         };
10029         auto ODRDiagBaseNote = [&SecondModule,
10030                                 this](SourceLocation Loc, SourceRange Range,
10031                                       ODRDefinitionDataDifference DiffType) {
10032           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10033                  << SecondModule << Range << DiffType;
10034         };
10035 
10036         unsigned FirstNumBases = FirstDD->NumBases;
10037         unsigned FirstNumVBases = FirstDD->NumVBases;
10038         unsigned SecondNumBases = SecondDD->NumBases;
10039         unsigned SecondNumVBases = SecondDD->NumVBases;
10040 
10041         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10042           unsigned NumBases = DD->NumBases;
10043           if (NumBases == 0) return SourceRange();
10044           auto bases = DD->bases();
10045           return SourceRange(bases[0].getBeginLoc(),
10046                              bases[NumBases - 1].getEndLoc());
10047         };
10048 
10049         if (FirstNumBases != SecondNumBases) {
10050           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10051                            NumBases)
10052               << FirstNumBases;
10053           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10054                           NumBases)
10055               << SecondNumBases;
10056           Diagnosed = true;
10057           break;
10058         }
10059 
10060         if (FirstNumVBases != SecondNumVBases) {
10061           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10062                            NumVBases)
10063               << FirstNumVBases;
10064           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10065                           NumVBases)
10066               << SecondNumVBases;
10067           Diagnosed = true;
10068           break;
10069         }
10070 
10071         auto FirstBases = FirstDD->bases();
10072         auto SecondBases = SecondDD->bases();
10073         unsigned i = 0;
10074         for (i = 0; i < FirstNumBases; ++i) {
10075           auto FirstBase = FirstBases[i];
10076           auto SecondBase = SecondBases[i];
10077           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10078               ComputeQualTypeODRHash(SecondBase.getType())) {
10079             ODRDiagBaseError(FirstRecord->getLocation(),
10080                              FirstBase.getSourceRange(), BaseType)
10081                 << (i + 1) << FirstBase.getType();
10082             ODRDiagBaseNote(SecondRecord->getLocation(),
10083                             SecondBase.getSourceRange(), BaseType)
10084                 << (i + 1) << SecondBase.getType();
10085             break;
10086           }
10087 
10088           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10089             ODRDiagBaseError(FirstRecord->getLocation(),
10090                              FirstBase.getSourceRange(), BaseVirtual)
10091                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10092             ODRDiagBaseNote(SecondRecord->getLocation(),
10093                             SecondBase.getSourceRange(), BaseVirtual)
10094                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10095             break;
10096           }
10097 
10098           if (FirstBase.getAccessSpecifierAsWritten() !=
10099               SecondBase.getAccessSpecifierAsWritten()) {
10100             ODRDiagBaseError(FirstRecord->getLocation(),
10101                              FirstBase.getSourceRange(), BaseAccess)
10102                 << (i + 1) << FirstBase.getType()
10103                 << (int)FirstBase.getAccessSpecifierAsWritten();
10104             ODRDiagBaseNote(SecondRecord->getLocation(),
10105                             SecondBase.getSourceRange(), BaseAccess)
10106                 << (i + 1) << SecondBase.getType()
10107                 << (int)SecondBase.getAccessSpecifierAsWritten();
10108             break;
10109           }
10110         }
10111 
10112         if (i != FirstNumBases) {
10113           Diagnosed = true;
10114           break;
10115         }
10116       }
10117 
10118       const ClassTemplateDecl *FirstTemplate =
10119           FirstRecord->getDescribedClassTemplate();
10120       const ClassTemplateDecl *SecondTemplate =
10121           SecondRecord->getDescribedClassTemplate();
10122 
10123       assert(!FirstTemplate == !SecondTemplate &&
10124              "Both pointers should be null or non-null");
10125 
10126       enum ODRTemplateDifference {
10127         ParamEmptyName,
10128         ParamName,
10129         ParamSingleDefaultArgument,
10130         ParamDifferentDefaultArgument,
10131       };
10132 
10133       if (FirstTemplate && SecondTemplate) {
10134         DeclHashes FirstTemplateHashes;
10135         DeclHashes SecondTemplateHashes;
10136 
10137         auto PopulateTemplateParameterHashs =
10138             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10139                                      const ClassTemplateDecl *TD) {
10140               for (auto *D : TD->getTemplateParameters()->asArray()) {
10141                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10142               }
10143             };
10144 
10145         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10146         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10147 
10148         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10149                "Number of template parameters should be equal.");
10150 
10151         auto FirstIt = FirstTemplateHashes.begin();
10152         auto FirstEnd = FirstTemplateHashes.end();
10153         auto SecondIt = SecondTemplateHashes.begin();
10154         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10155           if (FirstIt->second == SecondIt->second)
10156             continue;
10157 
10158           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10159                                           SourceLocation Loc, SourceRange Range,
10160                                           ODRTemplateDifference DiffType) {
10161             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10162                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10163                    << DiffType;
10164           };
10165           auto ODRDiagTemplateNote = [&SecondModule, this](
10166                                          SourceLocation Loc, SourceRange Range,
10167                                          ODRTemplateDifference DiffType) {
10168             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10169                    << SecondModule << Range << DiffType;
10170           };
10171 
10172           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10173           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10174 
10175           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10176                  "Parameter Decl's should be the same kind.");
10177 
10178           DeclarationName FirstName = FirstDecl->getDeclName();
10179           DeclarationName SecondName = SecondDecl->getDeclName();
10180 
10181           if (FirstName != SecondName) {
10182             const bool FirstNameEmpty =
10183                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10184             const bool SecondNameEmpty =
10185                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10186             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10187                    "Both template parameters cannot be unnamed.");
10188             ODRDiagTemplateError(FirstDecl->getLocation(),
10189                                  FirstDecl->getSourceRange(),
10190                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10191                 << FirstName;
10192             ODRDiagTemplateNote(SecondDecl->getLocation(),
10193                                 SecondDecl->getSourceRange(),
10194                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10195                 << SecondName;
10196             break;
10197           }
10198 
10199           switch (FirstDecl->getKind()) {
10200           default:
10201             llvm_unreachable("Invalid template parameter type.");
10202           case Decl::TemplateTypeParm: {
10203             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10204             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10205             const bool HasFirstDefaultArgument =
10206                 FirstParam->hasDefaultArgument() &&
10207                 !FirstParam->defaultArgumentWasInherited();
10208             const bool HasSecondDefaultArgument =
10209                 SecondParam->hasDefaultArgument() &&
10210                 !SecondParam->defaultArgumentWasInherited();
10211 
10212             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10213               ODRDiagTemplateError(FirstDecl->getLocation(),
10214                                    FirstDecl->getSourceRange(),
10215                                    ParamSingleDefaultArgument)
10216                   << HasFirstDefaultArgument;
10217               ODRDiagTemplateNote(SecondDecl->getLocation(),
10218                                   SecondDecl->getSourceRange(),
10219                                   ParamSingleDefaultArgument)
10220                   << HasSecondDefaultArgument;
10221               break;
10222             }
10223 
10224             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10225                    "Expecting default arguments.");
10226 
10227             ODRDiagTemplateError(FirstDecl->getLocation(),
10228                                  FirstDecl->getSourceRange(),
10229                                  ParamDifferentDefaultArgument);
10230             ODRDiagTemplateNote(SecondDecl->getLocation(),
10231                                 SecondDecl->getSourceRange(),
10232                                 ParamDifferentDefaultArgument);
10233 
10234             break;
10235           }
10236           case Decl::NonTypeTemplateParm: {
10237             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10238             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10239             const bool HasFirstDefaultArgument =
10240                 FirstParam->hasDefaultArgument() &&
10241                 !FirstParam->defaultArgumentWasInherited();
10242             const bool HasSecondDefaultArgument =
10243                 SecondParam->hasDefaultArgument() &&
10244                 !SecondParam->defaultArgumentWasInherited();
10245 
10246             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10247               ODRDiagTemplateError(FirstDecl->getLocation(),
10248                                    FirstDecl->getSourceRange(),
10249                                    ParamSingleDefaultArgument)
10250                   << HasFirstDefaultArgument;
10251               ODRDiagTemplateNote(SecondDecl->getLocation(),
10252                                   SecondDecl->getSourceRange(),
10253                                   ParamSingleDefaultArgument)
10254                   << HasSecondDefaultArgument;
10255               break;
10256             }
10257 
10258             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10259                    "Expecting default arguments.");
10260 
10261             ODRDiagTemplateError(FirstDecl->getLocation(),
10262                                  FirstDecl->getSourceRange(),
10263                                  ParamDifferentDefaultArgument);
10264             ODRDiagTemplateNote(SecondDecl->getLocation(),
10265                                 SecondDecl->getSourceRange(),
10266                                 ParamDifferentDefaultArgument);
10267 
10268             break;
10269           }
10270           case Decl::TemplateTemplateParm: {
10271             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10272             const auto *SecondParam =
10273                 cast<TemplateTemplateParmDecl>(SecondDecl);
10274             const bool HasFirstDefaultArgument =
10275                 FirstParam->hasDefaultArgument() &&
10276                 !FirstParam->defaultArgumentWasInherited();
10277             const bool HasSecondDefaultArgument =
10278                 SecondParam->hasDefaultArgument() &&
10279                 !SecondParam->defaultArgumentWasInherited();
10280 
10281             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10282               ODRDiagTemplateError(FirstDecl->getLocation(),
10283                                    FirstDecl->getSourceRange(),
10284                                    ParamSingleDefaultArgument)
10285                   << HasFirstDefaultArgument;
10286               ODRDiagTemplateNote(SecondDecl->getLocation(),
10287                                   SecondDecl->getSourceRange(),
10288                                   ParamSingleDefaultArgument)
10289                   << HasSecondDefaultArgument;
10290               break;
10291             }
10292 
10293             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10294                    "Expecting default arguments.");
10295 
10296             ODRDiagTemplateError(FirstDecl->getLocation(),
10297                                  FirstDecl->getSourceRange(),
10298                                  ParamDifferentDefaultArgument);
10299             ODRDiagTemplateNote(SecondDecl->getLocation(),
10300                                 SecondDecl->getSourceRange(),
10301                                 ParamDifferentDefaultArgument);
10302 
10303             break;
10304           }
10305           }
10306 
10307           break;
10308         }
10309 
10310         if (FirstIt != FirstEnd) {
10311           Diagnosed = true;
10312           break;
10313         }
10314       }
10315 
10316       DeclHashes FirstHashes;
10317       DeclHashes SecondHashes;
10318       const DeclContext *DC = FirstRecord;
10319       PopulateHashes(FirstHashes, FirstRecord, DC);
10320       PopulateHashes(SecondHashes, SecondRecord, DC);
10321 
10322       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10323       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10324       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10325       Decl *FirstDecl = DR.FirstDecl;
10326       Decl *SecondDecl = DR.SecondDecl;
10327 
10328       if (FirstDiffType == Other || SecondDiffType == Other) {
10329         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10330                               SecondModule);
10331         Diagnosed = true;
10332         break;
10333       }
10334 
10335       if (FirstDiffType != SecondDiffType) {
10336         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10337                             SecondModule);
10338         Diagnosed = true;
10339         break;
10340       }
10341 
10342       assert(FirstDiffType == SecondDiffType);
10343 
10344       switch (FirstDiffType) {
10345       case Other:
10346       case EndOfClass:
10347       case PublicSpecifer:
10348       case PrivateSpecifer:
10349       case ProtectedSpecifer:
10350         llvm_unreachable("Invalid diff type");
10351 
10352       case StaticAssert: {
10353         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10354         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10355 
10356         Expr *FirstExpr = FirstSA->getAssertExpr();
10357         Expr *SecondExpr = SecondSA->getAssertExpr();
10358         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10359         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10360         if (FirstODRHash != SecondODRHash) {
10361           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10362                            FirstExpr->getSourceRange(), StaticAssertCondition);
10363           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10364                           SecondExpr->getSourceRange(), StaticAssertCondition);
10365           Diagnosed = true;
10366           break;
10367         }
10368 
10369         StringLiteral *FirstStr = FirstSA->getMessage();
10370         StringLiteral *SecondStr = SecondSA->getMessage();
10371         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10372         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10373           SourceLocation FirstLoc, SecondLoc;
10374           SourceRange FirstRange, SecondRange;
10375           if (FirstStr) {
10376             FirstLoc = FirstStr->getBeginLoc();
10377             FirstRange = FirstStr->getSourceRange();
10378           } else {
10379             FirstLoc = FirstSA->getBeginLoc();
10380             FirstRange = FirstSA->getSourceRange();
10381           }
10382           if (SecondStr) {
10383             SecondLoc = SecondStr->getBeginLoc();
10384             SecondRange = SecondStr->getSourceRange();
10385           } else {
10386             SecondLoc = SecondSA->getBeginLoc();
10387             SecondRange = SecondSA->getSourceRange();
10388           }
10389           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10390                            StaticAssertOnlyMessage)
10391               << (FirstStr == nullptr);
10392           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10393                           StaticAssertOnlyMessage)
10394               << (SecondStr == nullptr);
10395           Diagnosed = true;
10396           break;
10397         }
10398 
10399         if (FirstStr && SecondStr &&
10400             FirstStr->getString() != SecondStr->getString()) {
10401           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10402                            FirstStr->getSourceRange(), StaticAssertMessage);
10403           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10404                           SecondStr->getSourceRange(), StaticAssertMessage);
10405           Diagnosed = true;
10406           break;
10407         }
10408         break;
10409       }
10410       case Field: {
10411         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10412                                  cast<FieldDecl>(FirstDecl),
10413                                  cast<FieldDecl>(SecondDecl));
10414         break;
10415       }
10416       case CXXMethod: {
10417         enum {
10418           DiagMethod,
10419           DiagConstructor,
10420           DiagDestructor,
10421         } FirstMethodType,
10422             SecondMethodType;
10423         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10424           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10425           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10426           return DiagMethod;
10427         };
10428         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10429         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10430         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10431         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10432         auto FirstName = FirstMethod->getDeclName();
10433         auto SecondName = SecondMethod->getDeclName();
10434         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10435           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10436                            FirstMethod->getSourceRange(), MethodName)
10437               << FirstMethodType << FirstName;
10438           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10439                           SecondMethod->getSourceRange(), MethodName)
10440               << SecondMethodType << SecondName;
10441 
10442           Diagnosed = true;
10443           break;
10444         }
10445 
10446         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10447         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10448         if (FirstDeleted != SecondDeleted) {
10449           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10450                            FirstMethod->getSourceRange(), MethodDeleted)
10451               << FirstMethodType << FirstName << FirstDeleted;
10452 
10453           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10454                           SecondMethod->getSourceRange(), MethodDeleted)
10455               << SecondMethodType << SecondName << SecondDeleted;
10456           Diagnosed = true;
10457           break;
10458         }
10459 
10460         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10461         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10462         if (FirstDefaulted != SecondDefaulted) {
10463           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10464                            FirstMethod->getSourceRange(), MethodDefaulted)
10465               << FirstMethodType << FirstName << FirstDefaulted;
10466 
10467           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10468                           SecondMethod->getSourceRange(), MethodDefaulted)
10469               << SecondMethodType << SecondName << SecondDefaulted;
10470           Diagnosed = true;
10471           break;
10472         }
10473 
10474         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10475         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10476         const bool FirstPure = FirstMethod->isPure();
10477         const bool SecondPure = SecondMethod->isPure();
10478         if ((FirstVirtual || SecondVirtual) &&
10479             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10480           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10481                            FirstMethod->getSourceRange(), MethodVirtual)
10482               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10483           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10484                           SecondMethod->getSourceRange(), MethodVirtual)
10485               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10486           Diagnosed = true;
10487           break;
10488         }
10489 
10490         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10491         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10492         // class needs to be checked instead.
10493         const auto FirstStorage = FirstMethod->getStorageClass();
10494         const auto SecondStorage = SecondMethod->getStorageClass();
10495         const bool FirstStatic = FirstStorage == SC_Static;
10496         const bool SecondStatic = SecondStorage == SC_Static;
10497         if (FirstStatic != SecondStatic) {
10498           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10499                            FirstMethod->getSourceRange(), MethodStatic)
10500               << FirstMethodType << FirstName << FirstStatic;
10501           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10502                           SecondMethod->getSourceRange(), MethodStatic)
10503               << SecondMethodType << SecondName << SecondStatic;
10504           Diagnosed = true;
10505           break;
10506         }
10507 
10508         const bool FirstVolatile = FirstMethod->isVolatile();
10509         const bool SecondVolatile = SecondMethod->isVolatile();
10510         if (FirstVolatile != SecondVolatile) {
10511           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10512                            FirstMethod->getSourceRange(), MethodVolatile)
10513               << FirstMethodType << FirstName << FirstVolatile;
10514           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10515                           SecondMethod->getSourceRange(), MethodVolatile)
10516               << SecondMethodType << SecondName << SecondVolatile;
10517           Diagnosed = true;
10518           break;
10519         }
10520 
10521         const bool FirstConst = FirstMethod->isConst();
10522         const bool SecondConst = SecondMethod->isConst();
10523         if (FirstConst != SecondConst) {
10524           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10525                            FirstMethod->getSourceRange(), MethodConst)
10526               << FirstMethodType << FirstName << FirstConst;
10527           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10528                           SecondMethod->getSourceRange(), MethodConst)
10529               << SecondMethodType << SecondName << SecondConst;
10530           Diagnosed = true;
10531           break;
10532         }
10533 
10534         const bool FirstInline = FirstMethod->isInlineSpecified();
10535         const bool SecondInline = SecondMethod->isInlineSpecified();
10536         if (FirstInline != SecondInline) {
10537           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10538                            FirstMethod->getSourceRange(), MethodInline)
10539               << FirstMethodType << FirstName << FirstInline;
10540           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10541                           SecondMethod->getSourceRange(), MethodInline)
10542               << SecondMethodType << SecondName << SecondInline;
10543           Diagnosed = true;
10544           break;
10545         }
10546 
10547         const unsigned FirstNumParameters = FirstMethod->param_size();
10548         const unsigned SecondNumParameters = SecondMethod->param_size();
10549         if (FirstNumParameters != SecondNumParameters) {
10550           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10551                            FirstMethod->getSourceRange(),
10552                            MethodNumberParameters)
10553               << FirstMethodType << FirstName << FirstNumParameters;
10554           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10555                           SecondMethod->getSourceRange(),
10556                           MethodNumberParameters)
10557               << SecondMethodType << SecondName << SecondNumParameters;
10558           Diagnosed = true;
10559           break;
10560         }
10561 
10562         // Need this status boolean to know when break out of the switch.
10563         bool ParameterMismatch = false;
10564         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10565           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10566           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10567 
10568           QualType FirstParamType = FirstParam->getType();
10569           QualType SecondParamType = SecondParam->getType();
10570           if (FirstParamType != SecondParamType &&
10571               ComputeQualTypeODRHash(FirstParamType) !=
10572                   ComputeQualTypeODRHash(SecondParamType)) {
10573             if (const DecayedType *ParamDecayedType =
10574                     FirstParamType->getAs<DecayedType>()) {
10575               ODRDiagDeclError(
10576                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10577                   FirstMethod->getSourceRange(), MethodParameterType)
10578                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10579                   << true << ParamDecayedType->getOriginalType();
10580             } else {
10581               ODRDiagDeclError(
10582                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10583                   FirstMethod->getSourceRange(), MethodParameterType)
10584                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10585                   << false;
10586             }
10587 
10588             if (const DecayedType *ParamDecayedType =
10589                     SecondParamType->getAs<DecayedType>()) {
10590               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10591                               SecondMethod->getSourceRange(),
10592                               MethodParameterType)
10593                   << SecondMethodType << SecondName << (I + 1)
10594                   << SecondParamType << true
10595                   << ParamDecayedType->getOriginalType();
10596             } else {
10597               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10598                               SecondMethod->getSourceRange(),
10599                               MethodParameterType)
10600                   << SecondMethodType << SecondName << (I + 1)
10601                   << SecondParamType << false;
10602             }
10603             ParameterMismatch = true;
10604             break;
10605           }
10606 
10607           DeclarationName FirstParamName = FirstParam->getDeclName();
10608           DeclarationName SecondParamName = SecondParam->getDeclName();
10609           if (FirstParamName != SecondParamName) {
10610             ODRDiagDeclError(FirstRecord, FirstModule,
10611                              FirstMethod->getLocation(),
10612                              FirstMethod->getSourceRange(), MethodParameterName)
10613                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10614             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10615                             SecondMethod->getSourceRange(), MethodParameterName)
10616                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10617             ParameterMismatch = true;
10618             break;
10619           }
10620 
10621           const Expr *FirstInit = FirstParam->getInit();
10622           const Expr *SecondInit = SecondParam->getInit();
10623           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10624             ODRDiagDeclError(FirstRecord, FirstModule,
10625                              FirstMethod->getLocation(),
10626                              FirstMethod->getSourceRange(),
10627                              MethodParameterSingleDefaultArgument)
10628                 << FirstMethodType << FirstName << (I + 1)
10629                 << (FirstInit == nullptr)
10630                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10631             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10632                             SecondMethod->getSourceRange(),
10633                             MethodParameterSingleDefaultArgument)
10634                 << SecondMethodType << SecondName << (I + 1)
10635                 << (SecondInit == nullptr)
10636                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10637             ParameterMismatch = true;
10638             break;
10639           }
10640 
10641           if (FirstInit && SecondInit &&
10642               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10643             ODRDiagDeclError(FirstRecord, FirstModule,
10644                              FirstMethod->getLocation(),
10645                              FirstMethod->getSourceRange(),
10646                              MethodParameterDifferentDefaultArgument)
10647                 << FirstMethodType << FirstName << (I + 1)
10648                 << FirstInit->getSourceRange();
10649             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10650                             SecondMethod->getSourceRange(),
10651                             MethodParameterDifferentDefaultArgument)
10652                 << SecondMethodType << SecondName << (I + 1)
10653                 << SecondInit->getSourceRange();
10654             ParameterMismatch = true;
10655             break;
10656 
10657           }
10658         }
10659 
10660         if (ParameterMismatch) {
10661           Diagnosed = true;
10662           break;
10663         }
10664 
10665         const auto *FirstTemplateArgs =
10666             FirstMethod->getTemplateSpecializationArgs();
10667         const auto *SecondTemplateArgs =
10668             SecondMethod->getTemplateSpecializationArgs();
10669 
10670         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10671             (!FirstTemplateArgs && SecondTemplateArgs)) {
10672           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10673                            FirstMethod->getSourceRange(),
10674                            MethodNoTemplateArguments)
10675               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10676           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10677                           SecondMethod->getSourceRange(),
10678                           MethodNoTemplateArguments)
10679               << SecondMethodType << SecondName
10680               << (SecondTemplateArgs != nullptr);
10681 
10682           Diagnosed = true;
10683           break;
10684         }
10685 
10686         if (FirstTemplateArgs && SecondTemplateArgs) {
10687           // Remove pack expansions from argument list.
10688           auto ExpandTemplateArgumentList =
10689               [](const TemplateArgumentList *TAL) {
10690                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10691                 for (const TemplateArgument &TA : TAL->asArray()) {
10692                   if (TA.getKind() != TemplateArgument::Pack) {
10693                     ExpandedList.push_back(&TA);
10694                     continue;
10695                   }
10696                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10697                     ExpandedList.push_back(&PackTA);
10698                   }
10699                 }
10700                 return ExpandedList;
10701               };
10702           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10703               ExpandTemplateArgumentList(FirstTemplateArgs);
10704           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10705               ExpandTemplateArgumentList(SecondTemplateArgs);
10706 
10707           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10708             ODRDiagDeclError(FirstRecord, FirstModule,
10709                              FirstMethod->getLocation(),
10710                              FirstMethod->getSourceRange(),
10711                              MethodDifferentNumberTemplateArguments)
10712                 << FirstMethodType << FirstName
10713                 << (unsigned)FirstExpandedList.size();
10714             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10715                             SecondMethod->getSourceRange(),
10716                             MethodDifferentNumberTemplateArguments)
10717                 << SecondMethodType << SecondName
10718                 << (unsigned)SecondExpandedList.size();
10719 
10720             Diagnosed = true;
10721             break;
10722           }
10723 
10724           bool TemplateArgumentMismatch = false;
10725           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10726             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10727                                    &SecondTA = *SecondExpandedList[i];
10728             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10729                 ComputeTemplateArgumentODRHash(SecondTA)) {
10730               continue;
10731             }
10732 
10733             ODRDiagDeclError(
10734                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10735                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10736                 << FirstMethodType << FirstName << FirstTA << i + 1;
10737             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10738                             SecondMethod->getSourceRange(),
10739                             MethodDifferentTemplateArgument)
10740                 << SecondMethodType << SecondName << SecondTA << i + 1;
10741 
10742             TemplateArgumentMismatch = true;
10743             break;
10744           }
10745 
10746           if (TemplateArgumentMismatch) {
10747             Diagnosed = true;
10748             break;
10749           }
10750         }
10751 
10752         // Compute the hash of the method as if it has no body.
10753         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10754           Hash.clear();
10755           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10756           return Hash.CalculateHash();
10757         };
10758 
10759         // Compare the hash generated to the hash stored.  A difference means
10760         // that a body was present in the original source.  Due to merging,
10761         // the stardard way of detecting a body will not work.
10762         const bool HasFirstBody =
10763             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10764         const bool HasSecondBody =
10765             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10766 
10767         if (HasFirstBody != HasSecondBody) {
10768           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10769                            FirstMethod->getSourceRange(), MethodSingleBody)
10770               << FirstMethodType << FirstName << HasFirstBody;
10771           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10772                           SecondMethod->getSourceRange(), MethodSingleBody)
10773               << SecondMethodType << SecondName << HasSecondBody;
10774           Diagnosed = true;
10775           break;
10776         }
10777 
10778         if (HasFirstBody && HasSecondBody) {
10779           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10780                            FirstMethod->getSourceRange(), MethodDifferentBody)
10781               << FirstMethodType << FirstName;
10782           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10783                           SecondMethod->getSourceRange(), MethodDifferentBody)
10784               << SecondMethodType << SecondName;
10785           Diagnosed = true;
10786           break;
10787         }
10788 
10789         break;
10790       }
10791       case TypeAlias:
10792       case TypeDef: {
10793         Diagnosed = ODRDiagTypeDefOrAlias(
10794             FirstRecord, FirstModule, SecondModule,
10795             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10796             FirstDiffType == TypeAlias);
10797         break;
10798       }
10799       case Var: {
10800         Diagnosed =
10801             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10802                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10803         break;
10804       }
10805       case Friend: {
10806         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10807         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10808 
10809         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10810         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10811 
10812         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10813         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10814 
10815         if (FirstND && SecondND) {
10816           ODRDiagDeclError(FirstRecord, FirstModule,
10817                            FirstFriend->getFriendLoc(),
10818                            FirstFriend->getSourceRange(), FriendFunction)
10819               << FirstND;
10820           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10821                           SecondFriend->getSourceRange(), FriendFunction)
10822               << SecondND;
10823 
10824           Diagnosed = true;
10825           break;
10826         }
10827 
10828         if (FirstTSI && SecondTSI) {
10829           QualType FirstFriendType = FirstTSI->getType();
10830           QualType SecondFriendType = SecondTSI->getType();
10831           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10832                  ComputeQualTypeODRHash(SecondFriendType));
10833           ODRDiagDeclError(FirstRecord, FirstModule,
10834                            FirstFriend->getFriendLoc(),
10835                            FirstFriend->getSourceRange(), FriendType)
10836               << FirstFriendType;
10837           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10838                           SecondFriend->getSourceRange(), FriendType)
10839               << SecondFriendType;
10840           Diagnosed = true;
10841           break;
10842         }
10843 
10844         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10845                          FirstFriend->getSourceRange(), FriendTypeFunction)
10846             << (FirstTSI == nullptr);
10847         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10848                         SecondFriend->getSourceRange(), FriendTypeFunction)
10849             << (SecondTSI == nullptr);
10850 
10851         Diagnosed = true;
10852         break;
10853       }
10854       case FunctionTemplate: {
10855         FunctionTemplateDecl *FirstTemplate =
10856             cast<FunctionTemplateDecl>(FirstDecl);
10857         FunctionTemplateDecl *SecondTemplate =
10858             cast<FunctionTemplateDecl>(SecondDecl);
10859 
10860         TemplateParameterList *FirstTPL =
10861             FirstTemplate->getTemplateParameters();
10862         TemplateParameterList *SecondTPL =
10863             SecondTemplate->getTemplateParameters();
10864 
10865         if (FirstTPL->size() != SecondTPL->size()) {
10866           ODRDiagDeclError(FirstRecord, FirstModule,
10867                            FirstTemplate->getLocation(),
10868                            FirstTemplate->getSourceRange(),
10869                            FunctionTemplateDifferentNumberParameters)
10870               << FirstTemplate << FirstTPL->size();
10871           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10872                           SecondTemplate->getSourceRange(),
10873                           FunctionTemplateDifferentNumberParameters)
10874               << SecondTemplate << SecondTPL->size();
10875 
10876           Diagnosed = true;
10877           break;
10878         }
10879 
10880         bool ParameterMismatch = false;
10881         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10882           NamedDecl *FirstParam = FirstTPL->getParam(i);
10883           NamedDecl *SecondParam = SecondTPL->getParam(i);
10884 
10885           if (FirstParam->getKind() != SecondParam->getKind()) {
10886             enum {
10887               TemplateTypeParameter,
10888               NonTypeTemplateParameter,
10889               TemplateTemplateParameter,
10890             };
10891             auto GetParamType = [](NamedDecl *D) {
10892               switch (D->getKind()) {
10893                 default:
10894                   llvm_unreachable("Unexpected template parameter type");
10895                 case Decl::TemplateTypeParm:
10896                   return TemplateTypeParameter;
10897                 case Decl::NonTypeTemplateParm:
10898                   return NonTypeTemplateParameter;
10899                 case Decl::TemplateTemplateParm:
10900                   return TemplateTemplateParameter;
10901               }
10902             };
10903 
10904             ODRDiagDeclError(FirstRecord, FirstModule,
10905                              FirstTemplate->getLocation(),
10906                              FirstTemplate->getSourceRange(),
10907                              FunctionTemplateParameterDifferentKind)
10908                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10909             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10910                             SecondTemplate->getSourceRange(),
10911                             FunctionTemplateParameterDifferentKind)
10912                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10913 
10914             ParameterMismatch = true;
10915             break;
10916           }
10917 
10918           if (FirstParam->getName() != SecondParam->getName()) {
10919             ODRDiagDeclError(
10920                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10921                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10922                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10923                 << FirstParam;
10924             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10925                             SecondTemplate->getSourceRange(),
10926                             FunctionTemplateParameterName)
10927                 << SecondTemplate << (i + 1)
10928                 << (bool)SecondParam->getIdentifier() << SecondParam;
10929             ParameterMismatch = true;
10930             break;
10931           }
10932 
10933           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10934               isa<TemplateTypeParmDecl>(SecondParam)) {
10935             TemplateTypeParmDecl *FirstTTPD =
10936                 cast<TemplateTypeParmDecl>(FirstParam);
10937             TemplateTypeParmDecl *SecondTTPD =
10938                 cast<TemplateTypeParmDecl>(SecondParam);
10939             bool HasFirstDefaultArgument =
10940                 FirstTTPD->hasDefaultArgument() &&
10941                 !FirstTTPD->defaultArgumentWasInherited();
10942             bool HasSecondDefaultArgument =
10943                 SecondTTPD->hasDefaultArgument() &&
10944                 !SecondTTPD->defaultArgumentWasInherited();
10945             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10946               ODRDiagDeclError(FirstRecord, FirstModule,
10947                                FirstTemplate->getLocation(),
10948                                FirstTemplate->getSourceRange(),
10949                                FunctionTemplateParameterSingleDefaultArgument)
10950                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10951               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10952                               SecondTemplate->getSourceRange(),
10953                               FunctionTemplateParameterSingleDefaultArgument)
10954                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10955               ParameterMismatch = true;
10956               break;
10957             }
10958 
10959             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10960               QualType FirstType = FirstTTPD->getDefaultArgument();
10961               QualType SecondType = SecondTTPD->getDefaultArgument();
10962               if (ComputeQualTypeODRHash(FirstType) !=
10963                   ComputeQualTypeODRHash(SecondType)) {
10964                 ODRDiagDeclError(
10965                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10966                     FirstTemplate->getSourceRange(),
10967                     FunctionTemplateParameterDifferentDefaultArgument)
10968                     << FirstTemplate << (i + 1) << FirstType;
10969                 ODRDiagDeclNote(
10970                     SecondModule, SecondTemplate->getLocation(),
10971                     SecondTemplate->getSourceRange(),
10972                     FunctionTemplateParameterDifferentDefaultArgument)
10973                     << SecondTemplate << (i + 1) << SecondType;
10974                 ParameterMismatch = true;
10975                 break;
10976               }
10977             }
10978 
10979             if (FirstTTPD->isParameterPack() !=
10980                 SecondTTPD->isParameterPack()) {
10981               ODRDiagDeclError(FirstRecord, FirstModule,
10982                                FirstTemplate->getLocation(),
10983                                FirstTemplate->getSourceRange(),
10984                                FunctionTemplatePackParameter)
10985                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10986               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10987                               SecondTemplate->getSourceRange(),
10988                               FunctionTemplatePackParameter)
10989                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10990               ParameterMismatch = true;
10991               break;
10992             }
10993           }
10994 
10995           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10996               isa<TemplateTemplateParmDecl>(SecondParam)) {
10997             TemplateTemplateParmDecl *FirstTTPD =
10998                 cast<TemplateTemplateParmDecl>(FirstParam);
10999             TemplateTemplateParmDecl *SecondTTPD =
11000                 cast<TemplateTemplateParmDecl>(SecondParam);
11001 
11002             TemplateParameterList *FirstTPL =
11003                 FirstTTPD->getTemplateParameters();
11004             TemplateParameterList *SecondTPL =
11005                 SecondTTPD->getTemplateParameters();
11006 
11007             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11008                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11009               ODRDiagDeclError(FirstRecord, FirstModule,
11010                                FirstTemplate->getLocation(),
11011                                FirstTemplate->getSourceRange(),
11012                                FunctionTemplateParameterDifferentType)
11013                   << FirstTemplate << (i + 1);
11014               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11015                               SecondTemplate->getSourceRange(),
11016                               FunctionTemplateParameterDifferentType)
11017                   << SecondTemplate << (i + 1);
11018               ParameterMismatch = true;
11019               break;
11020             }
11021 
11022             bool HasFirstDefaultArgument =
11023                 FirstTTPD->hasDefaultArgument() &&
11024                 !FirstTTPD->defaultArgumentWasInherited();
11025             bool HasSecondDefaultArgument =
11026                 SecondTTPD->hasDefaultArgument() &&
11027                 !SecondTTPD->defaultArgumentWasInherited();
11028             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11029               ODRDiagDeclError(FirstRecord, FirstModule,
11030                                FirstTemplate->getLocation(),
11031                                FirstTemplate->getSourceRange(),
11032                                FunctionTemplateParameterSingleDefaultArgument)
11033                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11034               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11035                               SecondTemplate->getSourceRange(),
11036                               FunctionTemplateParameterSingleDefaultArgument)
11037                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11038               ParameterMismatch = true;
11039               break;
11040             }
11041 
11042             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11043               TemplateArgument FirstTA =
11044                   FirstTTPD->getDefaultArgument().getArgument();
11045               TemplateArgument SecondTA =
11046                   SecondTTPD->getDefaultArgument().getArgument();
11047               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11048                   ComputeTemplateArgumentODRHash(SecondTA)) {
11049                 ODRDiagDeclError(
11050                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11051                     FirstTemplate->getSourceRange(),
11052                     FunctionTemplateParameterDifferentDefaultArgument)
11053                     << FirstTemplate << (i + 1) << FirstTA;
11054                 ODRDiagDeclNote(
11055                     SecondModule, SecondTemplate->getLocation(),
11056                     SecondTemplate->getSourceRange(),
11057                     FunctionTemplateParameterDifferentDefaultArgument)
11058                     << SecondTemplate << (i + 1) << SecondTA;
11059                 ParameterMismatch = true;
11060                 break;
11061               }
11062             }
11063 
11064             if (FirstTTPD->isParameterPack() !=
11065                 SecondTTPD->isParameterPack()) {
11066               ODRDiagDeclError(FirstRecord, FirstModule,
11067                                FirstTemplate->getLocation(),
11068                                FirstTemplate->getSourceRange(),
11069                                FunctionTemplatePackParameter)
11070                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11071               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11072                               SecondTemplate->getSourceRange(),
11073                               FunctionTemplatePackParameter)
11074                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11075               ParameterMismatch = true;
11076               break;
11077             }
11078           }
11079 
11080           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11081               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11082             NonTypeTemplateParmDecl *FirstNTTPD =
11083                 cast<NonTypeTemplateParmDecl>(FirstParam);
11084             NonTypeTemplateParmDecl *SecondNTTPD =
11085                 cast<NonTypeTemplateParmDecl>(SecondParam);
11086 
11087             QualType FirstType = FirstNTTPD->getType();
11088             QualType SecondType = SecondNTTPD->getType();
11089             if (ComputeQualTypeODRHash(FirstType) !=
11090                 ComputeQualTypeODRHash(SecondType)) {
11091               ODRDiagDeclError(FirstRecord, FirstModule,
11092                                FirstTemplate->getLocation(),
11093                                FirstTemplate->getSourceRange(),
11094                                FunctionTemplateParameterDifferentType)
11095                   << FirstTemplate << (i + 1);
11096               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11097                               SecondTemplate->getSourceRange(),
11098                               FunctionTemplateParameterDifferentType)
11099                   << SecondTemplate << (i + 1);
11100               ParameterMismatch = true;
11101               break;
11102             }
11103 
11104             bool HasFirstDefaultArgument =
11105                 FirstNTTPD->hasDefaultArgument() &&
11106                 !FirstNTTPD->defaultArgumentWasInherited();
11107             bool HasSecondDefaultArgument =
11108                 SecondNTTPD->hasDefaultArgument() &&
11109                 !SecondNTTPD->defaultArgumentWasInherited();
11110             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11111               ODRDiagDeclError(FirstRecord, FirstModule,
11112                                FirstTemplate->getLocation(),
11113                                FirstTemplate->getSourceRange(),
11114                                FunctionTemplateParameterSingleDefaultArgument)
11115                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11116               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11117                               SecondTemplate->getSourceRange(),
11118                               FunctionTemplateParameterSingleDefaultArgument)
11119                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11120               ParameterMismatch = true;
11121               break;
11122             }
11123 
11124             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11125               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11126               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11127               if (ComputeODRHash(FirstDefaultArgument) !=
11128                   ComputeODRHash(SecondDefaultArgument)) {
11129                 ODRDiagDeclError(
11130                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11131                     FirstTemplate->getSourceRange(),
11132                     FunctionTemplateParameterDifferentDefaultArgument)
11133                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11134                 ODRDiagDeclNote(
11135                     SecondModule, SecondTemplate->getLocation(),
11136                     SecondTemplate->getSourceRange(),
11137                     FunctionTemplateParameterDifferentDefaultArgument)
11138                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11139                 ParameterMismatch = true;
11140                 break;
11141               }
11142             }
11143 
11144             if (FirstNTTPD->isParameterPack() !=
11145                 SecondNTTPD->isParameterPack()) {
11146               ODRDiagDeclError(FirstRecord, FirstModule,
11147                                FirstTemplate->getLocation(),
11148                                FirstTemplate->getSourceRange(),
11149                                FunctionTemplatePackParameter)
11150                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11151               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11152                               SecondTemplate->getSourceRange(),
11153                               FunctionTemplatePackParameter)
11154                   << SecondTemplate << (i + 1)
11155                   << SecondNTTPD->isParameterPack();
11156               ParameterMismatch = true;
11157               break;
11158             }
11159           }
11160         }
11161 
11162         if (ParameterMismatch) {
11163           Diagnosed = true;
11164           break;
11165         }
11166 
11167         break;
11168       }
11169       }
11170 
11171       if (Diagnosed)
11172         continue;
11173 
11174       Diag(FirstDecl->getLocation(),
11175            diag::err_module_odr_violation_mismatch_decl_unknown)
11176           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11177           << FirstDecl->getSourceRange();
11178       Diag(SecondDecl->getLocation(),
11179            diag::note_module_odr_violation_mismatch_decl_unknown)
11180           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11181       Diagnosed = true;
11182     }
11183 
11184     if (!Diagnosed) {
11185       // All definitions are updates to the same declaration. This happens if a
11186       // module instantiates the declaration of a class template specialization
11187       // and two or more other modules instantiate its definition.
11188       //
11189       // FIXME: Indicate which modules had instantiations of this definition.
11190       // FIXME: How can this even happen?
11191       Diag(Merge.first->getLocation(),
11192            diag::err_module_odr_violation_different_instantiations)
11193         << Merge.first;
11194     }
11195   }
11196 
11197   // Issue ODR failures diagnostics for functions.
11198   for (auto &Merge : FunctionOdrMergeFailures) {
11199     enum ODRFunctionDifference {
11200       ReturnType,
11201       ParameterName,
11202       ParameterType,
11203       ParameterSingleDefaultArgument,
11204       ParameterDifferentDefaultArgument,
11205       FunctionBody,
11206     };
11207 
11208     FunctionDecl *FirstFunction = Merge.first;
11209     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11210 
11211     bool Diagnosed = false;
11212     for (auto &SecondFunction : Merge.second) {
11213 
11214       if (FirstFunction == SecondFunction)
11215         continue;
11216 
11217       std::string SecondModule =
11218           getOwningModuleNameForDiagnostic(SecondFunction);
11219 
11220       auto ODRDiagError = [FirstFunction, &FirstModule,
11221                            this](SourceLocation Loc, SourceRange Range,
11222                                  ODRFunctionDifference DiffType) {
11223         return Diag(Loc, diag::err_module_odr_violation_function)
11224                << FirstFunction << FirstModule.empty() << FirstModule << Range
11225                << DiffType;
11226       };
11227       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11228                                                SourceRange Range,
11229                                                ODRFunctionDifference DiffType) {
11230         return Diag(Loc, diag::note_module_odr_violation_function)
11231                << SecondModule << Range << DiffType;
11232       };
11233 
11234       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11235           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11236         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11237                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11238             << FirstFunction->getReturnType();
11239         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11240                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11241             << SecondFunction->getReturnType();
11242         Diagnosed = true;
11243         break;
11244       }
11245 
11246       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11247              "Merged functions with different number of parameters");
11248 
11249       auto ParamSize = FirstFunction->param_size();
11250       bool ParameterMismatch = false;
11251       for (unsigned I = 0; I < ParamSize; ++I) {
11252         auto *FirstParam = FirstFunction->getParamDecl(I);
11253         auto *SecondParam = SecondFunction->getParamDecl(I);
11254 
11255         assert(getContext().hasSameType(FirstParam->getType(),
11256                                       SecondParam->getType()) &&
11257                "Merged function has different parameter types.");
11258 
11259         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11260           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11261                        ParameterName)
11262               << I + 1 << FirstParam->getDeclName();
11263           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11264                       ParameterName)
11265               << I + 1 << SecondParam->getDeclName();
11266           ParameterMismatch = true;
11267           break;
11268         };
11269 
11270         QualType FirstParamType = FirstParam->getType();
11271         QualType SecondParamType = SecondParam->getType();
11272         if (FirstParamType != SecondParamType &&
11273             ComputeQualTypeODRHash(FirstParamType) !=
11274                 ComputeQualTypeODRHash(SecondParamType)) {
11275           if (const DecayedType *ParamDecayedType =
11276                   FirstParamType->getAs<DecayedType>()) {
11277             ODRDiagError(FirstParam->getLocation(),
11278                          FirstParam->getSourceRange(), ParameterType)
11279                 << (I + 1) << FirstParamType << true
11280                 << ParamDecayedType->getOriginalType();
11281           } else {
11282             ODRDiagError(FirstParam->getLocation(),
11283                          FirstParam->getSourceRange(), ParameterType)
11284                 << (I + 1) << FirstParamType << false;
11285           }
11286 
11287           if (const DecayedType *ParamDecayedType =
11288                   SecondParamType->getAs<DecayedType>()) {
11289             ODRDiagNote(SecondParam->getLocation(),
11290                         SecondParam->getSourceRange(), ParameterType)
11291                 << (I + 1) << SecondParamType << true
11292                 << ParamDecayedType->getOriginalType();
11293           } else {
11294             ODRDiagNote(SecondParam->getLocation(),
11295                         SecondParam->getSourceRange(), ParameterType)
11296                 << (I + 1) << SecondParamType << false;
11297           }
11298           ParameterMismatch = true;
11299           break;
11300         }
11301 
11302         const Expr *FirstInit = FirstParam->getInit();
11303         const Expr *SecondInit = SecondParam->getInit();
11304         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11305           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11306                        ParameterSingleDefaultArgument)
11307               << (I + 1) << (FirstInit == nullptr)
11308               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11309           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11310                       ParameterSingleDefaultArgument)
11311               << (I + 1) << (SecondInit == nullptr)
11312               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11313           ParameterMismatch = true;
11314           break;
11315         }
11316 
11317         if (FirstInit && SecondInit &&
11318             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11319           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11320                        ParameterDifferentDefaultArgument)
11321               << (I + 1) << FirstInit->getSourceRange();
11322           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11323                       ParameterDifferentDefaultArgument)
11324               << (I + 1) << SecondInit->getSourceRange();
11325           ParameterMismatch = true;
11326           break;
11327         }
11328 
11329         assert(ComputeSubDeclODRHash(FirstParam) ==
11330                    ComputeSubDeclODRHash(SecondParam) &&
11331                "Undiagnosed parameter difference.");
11332       }
11333 
11334       if (ParameterMismatch) {
11335         Diagnosed = true;
11336         break;
11337       }
11338 
11339       // If no error has been generated before now, assume the problem is in
11340       // the body and generate a message.
11341       ODRDiagError(FirstFunction->getLocation(),
11342                    FirstFunction->getSourceRange(), FunctionBody);
11343       ODRDiagNote(SecondFunction->getLocation(),
11344                   SecondFunction->getSourceRange(), FunctionBody);
11345       Diagnosed = true;
11346       break;
11347     }
11348     (void)Diagnosed;
11349     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11350   }
11351 
11352   // Issue ODR failures diagnostics for enums.
11353   for (auto &Merge : EnumOdrMergeFailures) {
11354     enum ODREnumDifference {
11355       SingleScopedEnum,
11356       EnumTagKeywordMismatch,
11357       SingleSpecifiedType,
11358       DifferentSpecifiedTypes,
11359       DifferentNumberEnumConstants,
11360       EnumConstantName,
11361       EnumConstantSingleInitilizer,
11362       EnumConstantDifferentInitilizer,
11363     };
11364 
11365     // If we've already pointed out a specific problem with this enum, don't
11366     // bother issuing a general "something's different" diagnostic.
11367     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11368       continue;
11369 
11370     EnumDecl *FirstEnum = Merge.first;
11371     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11372 
11373     using DeclHashes =
11374         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11375     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11376                               DeclHashes &Hashes, EnumDecl *Enum) {
11377       for (auto *D : Enum->decls()) {
11378         // Due to decl merging, the first EnumDecl is the parent of
11379         // Decls in both records.
11380         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11381           continue;
11382         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11383         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11384                             ComputeSubDeclODRHash(D));
11385       }
11386     };
11387     DeclHashes FirstHashes;
11388     PopulateHashes(FirstHashes, FirstEnum);
11389     bool Diagnosed = false;
11390     for (auto &SecondEnum : Merge.second) {
11391 
11392       if (FirstEnum == SecondEnum)
11393         continue;
11394 
11395       std::string SecondModule =
11396           getOwningModuleNameForDiagnostic(SecondEnum);
11397 
11398       auto ODRDiagError = [FirstEnum, &FirstModule,
11399                            this](SourceLocation Loc, SourceRange Range,
11400                                  ODREnumDifference DiffType) {
11401         return Diag(Loc, diag::err_module_odr_violation_enum)
11402                << FirstEnum << FirstModule.empty() << FirstModule << Range
11403                << DiffType;
11404       };
11405       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11406                                                SourceRange Range,
11407                                                ODREnumDifference DiffType) {
11408         return Diag(Loc, diag::note_module_odr_violation_enum)
11409                << SecondModule << Range << DiffType;
11410       };
11411 
11412       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11413         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11414                      SingleScopedEnum)
11415             << FirstEnum->isScoped();
11416         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11417                     SingleScopedEnum)
11418             << SecondEnum->isScoped();
11419         Diagnosed = true;
11420         continue;
11421       }
11422 
11423       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11424         if (FirstEnum->isScopedUsingClassTag() !=
11425             SecondEnum->isScopedUsingClassTag()) {
11426           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11427                        EnumTagKeywordMismatch)
11428               << FirstEnum->isScopedUsingClassTag();
11429           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11430                       EnumTagKeywordMismatch)
11431               << SecondEnum->isScopedUsingClassTag();
11432           Diagnosed = true;
11433           continue;
11434         }
11435       }
11436 
11437       QualType FirstUnderlyingType =
11438           FirstEnum->getIntegerTypeSourceInfo()
11439               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11440               : QualType();
11441       QualType SecondUnderlyingType =
11442           SecondEnum->getIntegerTypeSourceInfo()
11443               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11444               : QualType();
11445       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11446           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11447                        SingleSpecifiedType)
11448               << !FirstUnderlyingType.isNull();
11449           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11450                       SingleSpecifiedType)
11451               << !SecondUnderlyingType.isNull();
11452           Diagnosed = true;
11453           continue;
11454       }
11455 
11456       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11457         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11458             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11459           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11460                        DifferentSpecifiedTypes)
11461               << FirstUnderlyingType;
11462           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11463                       DifferentSpecifiedTypes)
11464               << SecondUnderlyingType;
11465           Diagnosed = true;
11466           continue;
11467         }
11468       }
11469 
11470       DeclHashes SecondHashes;
11471       PopulateHashes(SecondHashes, SecondEnum);
11472 
11473       if (FirstHashes.size() != SecondHashes.size()) {
11474         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11475                      DifferentNumberEnumConstants)
11476             << (int)FirstHashes.size();
11477         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11478                     DifferentNumberEnumConstants)
11479             << (int)SecondHashes.size();
11480         Diagnosed = true;
11481         continue;
11482       }
11483 
11484       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11485         if (FirstHashes[I].second == SecondHashes[I].second)
11486           continue;
11487         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11488         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11489 
11490         if (FirstEnumConstant->getDeclName() !=
11491             SecondEnumConstant->getDeclName()) {
11492 
11493           ODRDiagError(FirstEnumConstant->getLocation(),
11494                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11495               << I + 1 << FirstEnumConstant;
11496           ODRDiagNote(SecondEnumConstant->getLocation(),
11497                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11498               << I + 1 << SecondEnumConstant;
11499           Diagnosed = true;
11500           break;
11501         }
11502 
11503         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11504         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11505         if (!FirstInit && !SecondInit)
11506           continue;
11507 
11508         if (!FirstInit || !SecondInit) {
11509           ODRDiagError(FirstEnumConstant->getLocation(),
11510                        FirstEnumConstant->getSourceRange(),
11511                        EnumConstantSingleInitilizer)
11512               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11513           ODRDiagNote(SecondEnumConstant->getLocation(),
11514                       SecondEnumConstant->getSourceRange(),
11515                       EnumConstantSingleInitilizer)
11516               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11517           Diagnosed = true;
11518           break;
11519         }
11520 
11521         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11522           ODRDiagError(FirstEnumConstant->getLocation(),
11523                        FirstEnumConstant->getSourceRange(),
11524                        EnumConstantDifferentInitilizer)
11525               << I + 1 << FirstEnumConstant;
11526           ODRDiagNote(SecondEnumConstant->getLocation(),
11527                       SecondEnumConstant->getSourceRange(),
11528                       EnumConstantDifferentInitilizer)
11529               << I + 1 << SecondEnumConstant;
11530           Diagnosed = true;
11531           break;
11532         }
11533       }
11534     }
11535 
11536     (void)Diagnosed;
11537     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11538   }
11539 }
11540 
11541 void ASTReader::StartedDeserializing() {
11542   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11543     ReadTimer->startTimer();
11544 }
11545 
11546 void ASTReader::FinishedDeserializing() {
11547   assert(NumCurrentElementsDeserializing &&
11548          "FinishedDeserializing not paired with StartedDeserializing");
11549   if (NumCurrentElementsDeserializing == 1) {
11550     // We decrease NumCurrentElementsDeserializing only after pending actions
11551     // are finished, to avoid recursively re-calling finishPendingActions().
11552     finishPendingActions();
11553   }
11554   --NumCurrentElementsDeserializing;
11555 
11556   if (NumCurrentElementsDeserializing == 0) {
11557     // Propagate exception specification and deduced type updates along
11558     // redeclaration chains.
11559     //
11560     // We do this now rather than in finishPendingActions because we want to
11561     // be able to walk the complete redeclaration chains of the updated decls.
11562     while (!PendingExceptionSpecUpdates.empty() ||
11563            !PendingDeducedTypeUpdates.empty()) {
11564       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11565       PendingExceptionSpecUpdates.clear();
11566       for (auto Update : ESUpdates) {
11567         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11568         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11569         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11570         if (auto *Listener = getContext().getASTMutationListener())
11571           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11572         for (auto *Redecl : Update.second->redecls())
11573           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11574       }
11575 
11576       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11577       PendingDeducedTypeUpdates.clear();
11578       for (auto Update : DTUpdates) {
11579         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11580         // FIXME: If the return type is already deduced, check that it matches.
11581         getContext().adjustDeducedFunctionResultType(Update.first,
11582                                                      Update.second);
11583       }
11584     }
11585 
11586     if (ReadTimer)
11587       ReadTimer->stopTimer();
11588 
11589     diagnoseOdrViolations();
11590 
11591     // We are not in recursive loading, so it's safe to pass the "interesting"
11592     // decls to the consumer.
11593     if (Consumer)
11594       PassInterestingDeclsToConsumer();
11595   }
11596 }
11597 
11598 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11599   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11600     // Remove any fake results before adding any real ones.
11601     auto It = PendingFakeLookupResults.find(II);
11602     if (It != PendingFakeLookupResults.end()) {
11603       for (auto *ND : It->second)
11604         SemaObj->IdResolver.RemoveDecl(ND);
11605       // FIXME: this works around module+PCH performance issue.
11606       // Rather than erase the result from the map, which is O(n), just clear
11607       // the vector of NamedDecls.
11608       It->second.clear();
11609     }
11610   }
11611 
11612   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11613     SemaObj->TUScope->AddDecl(D);
11614   } else if (SemaObj->TUScope) {
11615     // Adding the decl to IdResolver may have failed because it was already in
11616     // (even though it was not added in scope). If it is already in, make sure
11617     // it gets in the scope as well.
11618     if (std::find(SemaObj->IdResolver.begin(Name),
11619                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11620       SemaObj->TUScope->AddDecl(D);
11621   }
11622 }
11623 
11624 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11625                      ASTContext *Context,
11626                      const PCHContainerReader &PCHContainerRdr,
11627                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11628                      StringRef isysroot,
11629                      DisableValidationForModuleKind DisableValidationKind,
11630                      bool AllowASTWithCompilerErrors,
11631                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11632                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11633                      std::unique_ptr<llvm::Timer> ReadTimer)
11634     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11635                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11636                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11637       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11638       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11639       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11640                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11641       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11642       DisableValidationKind(DisableValidationKind),
11643       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11644       AllowConfigurationMismatch(AllowConfigurationMismatch),
11645       ValidateSystemInputs(ValidateSystemInputs),
11646       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11647       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11648   SourceMgr.setExternalSLocEntrySource(this);
11649 
11650   for (const auto &Ext : Extensions) {
11651     auto BlockName = Ext->getExtensionMetadata().BlockName;
11652     auto Known = ModuleFileExtensions.find(BlockName);
11653     if (Known != ModuleFileExtensions.end()) {
11654       Diags.Report(diag::warn_duplicate_module_file_extension)
11655         << BlockName;
11656       continue;
11657     }
11658 
11659     ModuleFileExtensions.insert({BlockName, Ext});
11660   }
11661 }
11662 
11663 ASTReader::~ASTReader() {
11664   if (OwnsDeserializationListener)
11665     delete DeserializationListener;
11666 }
11667 
11668 IdentifierResolver &ASTReader::getIdResolver() {
11669   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11670 }
11671 
11672 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11673                                                unsigned AbbrevID) {
11674   Idx = 0;
11675   Record.clear();
11676   return Cursor.readRecord(AbbrevID, Record);
11677 }
11678 //===----------------------------------------------------------------------===//
11679 //// OMPClauseReader implementation
11680 ////===----------------------------------------------------------------------===//
11681 
11682 // This has to be in namespace clang because it's friended by all
11683 // of the OMP clauses.
11684 namespace clang {
11685 
11686 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11687   ASTRecordReader &Record;
11688   ASTContext &Context;
11689 
11690 public:
11691   OMPClauseReader(ASTRecordReader &Record)
11692       : Record(Record), Context(Record.getContext()) {}
11693 #define GEN_CLANG_CLAUSE_CLASS
11694 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11695 #include "llvm/Frontend/OpenMP/OMP.inc"
11696   OMPClause *readClause();
11697   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11698   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11699 };
11700 
11701 } // end namespace clang
11702 
11703 OMPClause *ASTRecordReader::readOMPClause() {
11704   return OMPClauseReader(*this).readClause();
11705 }
11706 
11707 OMPClause *OMPClauseReader::readClause() {
11708   OMPClause *C = nullptr;
11709   switch (llvm::omp::Clause(Record.readInt())) {
11710   case llvm::omp::OMPC_if:
11711     C = new (Context) OMPIfClause();
11712     break;
11713   case llvm::omp::OMPC_final:
11714     C = new (Context) OMPFinalClause();
11715     break;
11716   case llvm::omp::OMPC_num_threads:
11717     C = new (Context) OMPNumThreadsClause();
11718     break;
11719   case llvm::omp::OMPC_safelen:
11720     C = new (Context) OMPSafelenClause();
11721     break;
11722   case llvm::omp::OMPC_simdlen:
11723     C = new (Context) OMPSimdlenClause();
11724     break;
11725   case llvm::omp::OMPC_sizes: {
11726     unsigned NumSizes = Record.readInt();
11727     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11728     break;
11729   }
11730   case llvm::omp::OMPC_full:
11731     C = OMPFullClause::CreateEmpty(Context);
11732     break;
11733   case llvm::omp::OMPC_partial:
11734     C = OMPPartialClause::CreateEmpty(Context);
11735     break;
11736   case llvm::omp::OMPC_allocator:
11737     C = new (Context) OMPAllocatorClause();
11738     break;
11739   case llvm::omp::OMPC_collapse:
11740     C = new (Context) OMPCollapseClause();
11741     break;
11742   case llvm::omp::OMPC_default:
11743     C = new (Context) OMPDefaultClause();
11744     break;
11745   case llvm::omp::OMPC_proc_bind:
11746     C = new (Context) OMPProcBindClause();
11747     break;
11748   case llvm::omp::OMPC_schedule:
11749     C = new (Context) OMPScheduleClause();
11750     break;
11751   case llvm::omp::OMPC_ordered:
11752     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11753     break;
11754   case llvm::omp::OMPC_nowait:
11755     C = new (Context) OMPNowaitClause();
11756     break;
11757   case llvm::omp::OMPC_untied:
11758     C = new (Context) OMPUntiedClause();
11759     break;
11760   case llvm::omp::OMPC_mergeable:
11761     C = new (Context) OMPMergeableClause();
11762     break;
11763   case llvm::omp::OMPC_read:
11764     C = new (Context) OMPReadClause();
11765     break;
11766   case llvm::omp::OMPC_write:
11767     C = new (Context) OMPWriteClause();
11768     break;
11769   case llvm::omp::OMPC_update:
11770     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11771     break;
11772   case llvm::omp::OMPC_capture:
11773     C = new (Context) OMPCaptureClause();
11774     break;
11775   case llvm::omp::OMPC_seq_cst:
11776     C = new (Context) OMPSeqCstClause();
11777     break;
11778   case llvm::omp::OMPC_acq_rel:
11779     C = new (Context) OMPAcqRelClause();
11780     break;
11781   case llvm::omp::OMPC_acquire:
11782     C = new (Context) OMPAcquireClause();
11783     break;
11784   case llvm::omp::OMPC_release:
11785     C = new (Context) OMPReleaseClause();
11786     break;
11787   case llvm::omp::OMPC_relaxed:
11788     C = new (Context) OMPRelaxedClause();
11789     break;
11790   case llvm::omp::OMPC_threads:
11791     C = new (Context) OMPThreadsClause();
11792     break;
11793   case llvm::omp::OMPC_simd:
11794     C = new (Context) OMPSIMDClause();
11795     break;
11796   case llvm::omp::OMPC_nogroup:
11797     C = new (Context) OMPNogroupClause();
11798     break;
11799   case llvm::omp::OMPC_unified_address:
11800     C = new (Context) OMPUnifiedAddressClause();
11801     break;
11802   case llvm::omp::OMPC_unified_shared_memory:
11803     C = new (Context) OMPUnifiedSharedMemoryClause();
11804     break;
11805   case llvm::omp::OMPC_reverse_offload:
11806     C = new (Context) OMPReverseOffloadClause();
11807     break;
11808   case llvm::omp::OMPC_dynamic_allocators:
11809     C = new (Context) OMPDynamicAllocatorsClause();
11810     break;
11811   case llvm::omp::OMPC_atomic_default_mem_order:
11812     C = new (Context) OMPAtomicDefaultMemOrderClause();
11813     break;
11814  case llvm::omp::OMPC_private:
11815     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11816     break;
11817   case llvm::omp::OMPC_firstprivate:
11818     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11819     break;
11820   case llvm::omp::OMPC_lastprivate:
11821     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11822     break;
11823   case llvm::omp::OMPC_shared:
11824     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11825     break;
11826   case llvm::omp::OMPC_reduction: {
11827     unsigned N = Record.readInt();
11828     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11829     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11830     break;
11831   }
11832   case llvm::omp::OMPC_task_reduction:
11833     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11834     break;
11835   case llvm::omp::OMPC_in_reduction:
11836     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11837     break;
11838   case llvm::omp::OMPC_linear:
11839     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11840     break;
11841   case llvm::omp::OMPC_aligned:
11842     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11843     break;
11844   case llvm::omp::OMPC_copyin:
11845     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11846     break;
11847   case llvm::omp::OMPC_copyprivate:
11848     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11849     break;
11850   case llvm::omp::OMPC_flush:
11851     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11852     break;
11853   case llvm::omp::OMPC_depobj:
11854     C = OMPDepobjClause::CreateEmpty(Context);
11855     break;
11856   case llvm::omp::OMPC_depend: {
11857     unsigned NumVars = Record.readInt();
11858     unsigned NumLoops = Record.readInt();
11859     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11860     break;
11861   }
11862   case llvm::omp::OMPC_device:
11863     C = new (Context) OMPDeviceClause();
11864     break;
11865   case llvm::omp::OMPC_map: {
11866     OMPMappableExprListSizeTy Sizes;
11867     Sizes.NumVars = Record.readInt();
11868     Sizes.NumUniqueDeclarations = Record.readInt();
11869     Sizes.NumComponentLists = Record.readInt();
11870     Sizes.NumComponents = Record.readInt();
11871     C = OMPMapClause::CreateEmpty(Context, Sizes);
11872     break;
11873   }
11874   case llvm::omp::OMPC_num_teams:
11875     C = new (Context) OMPNumTeamsClause();
11876     break;
11877   case llvm::omp::OMPC_thread_limit:
11878     C = new (Context) OMPThreadLimitClause();
11879     break;
11880   case llvm::omp::OMPC_priority:
11881     C = new (Context) OMPPriorityClause();
11882     break;
11883   case llvm::omp::OMPC_grainsize:
11884     C = new (Context) OMPGrainsizeClause();
11885     break;
11886   case llvm::omp::OMPC_num_tasks:
11887     C = new (Context) OMPNumTasksClause();
11888     break;
11889   case llvm::omp::OMPC_hint:
11890     C = new (Context) OMPHintClause();
11891     break;
11892   case llvm::omp::OMPC_dist_schedule:
11893     C = new (Context) OMPDistScheduleClause();
11894     break;
11895   case llvm::omp::OMPC_defaultmap:
11896     C = new (Context) OMPDefaultmapClause();
11897     break;
11898   case llvm::omp::OMPC_to: {
11899     OMPMappableExprListSizeTy Sizes;
11900     Sizes.NumVars = Record.readInt();
11901     Sizes.NumUniqueDeclarations = Record.readInt();
11902     Sizes.NumComponentLists = Record.readInt();
11903     Sizes.NumComponents = Record.readInt();
11904     C = OMPToClause::CreateEmpty(Context, Sizes);
11905     break;
11906   }
11907   case llvm::omp::OMPC_from: {
11908     OMPMappableExprListSizeTy Sizes;
11909     Sizes.NumVars = Record.readInt();
11910     Sizes.NumUniqueDeclarations = Record.readInt();
11911     Sizes.NumComponentLists = Record.readInt();
11912     Sizes.NumComponents = Record.readInt();
11913     C = OMPFromClause::CreateEmpty(Context, Sizes);
11914     break;
11915   }
11916   case llvm::omp::OMPC_use_device_ptr: {
11917     OMPMappableExprListSizeTy Sizes;
11918     Sizes.NumVars = Record.readInt();
11919     Sizes.NumUniqueDeclarations = Record.readInt();
11920     Sizes.NumComponentLists = Record.readInt();
11921     Sizes.NumComponents = Record.readInt();
11922     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11923     break;
11924   }
11925   case llvm::omp::OMPC_use_device_addr: {
11926     OMPMappableExprListSizeTy Sizes;
11927     Sizes.NumVars = Record.readInt();
11928     Sizes.NumUniqueDeclarations = Record.readInt();
11929     Sizes.NumComponentLists = Record.readInt();
11930     Sizes.NumComponents = Record.readInt();
11931     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11932     break;
11933   }
11934   case llvm::omp::OMPC_is_device_ptr: {
11935     OMPMappableExprListSizeTy Sizes;
11936     Sizes.NumVars = Record.readInt();
11937     Sizes.NumUniqueDeclarations = Record.readInt();
11938     Sizes.NumComponentLists = Record.readInt();
11939     Sizes.NumComponents = Record.readInt();
11940     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11941     break;
11942   }
11943   case llvm::omp::OMPC_allocate:
11944     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11945     break;
11946   case llvm::omp::OMPC_nontemporal:
11947     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11948     break;
11949   case llvm::omp::OMPC_inclusive:
11950     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11951     break;
11952   case llvm::omp::OMPC_exclusive:
11953     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11954     break;
11955   case llvm::omp::OMPC_order:
11956     C = new (Context) OMPOrderClause();
11957     break;
11958   case llvm::omp::OMPC_init:
11959     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11960     break;
11961   case llvm::omp::OMPC_use:
11962     C = new (Context) OMPUseClause();
11963     break;
11964   case llvm::omp::OMPC_destroy:
11965     C = new (Context) OMPDestroyClause();
11966     break;
11967   case llvm::omp::OMPC_novariants:
11968     C = new (Context) OMPNovariantsClause();
11969     break;
11970   case llvm::omp::OMPC_nocontext:
11971     C = new (Context) OMPNocontextClause();
11972     break;
11973   case llvm::omp::OMPC_detach:
11974     C = new (Context) OMPDetachClause();
11975     break;
11976   case llvm::omp::OMPC_uses_allocators:
11977     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11978     break;
11979   case llvm::omp::OMPC_affinity:
11980     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11981     break;
11982   case llvm::omp::OMPC_filter:
11983     C = new (Context) OMPFilterClause();
11984     break;
11985 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11986   case llvm::omp::Enum:                                                        \
11987     break;
11988 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11989   default:
11990     break;
11991   }
11992   assert(C && "Unknown OMPClause type");
11993 
11994   Visit(C);
11995   C->setLocStart(Record.readSourceLocation());
11996   C->setLocEnd(Record.readSourceLocation());
11997 
11998   return C;
11999 }
12000 
12001 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12002   C->setPreInitStmt(Record.readSubStmt(),
12003                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12004 }
12005 
12006 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12007   VisitOMPClauseWithPreInit(C);
12008   C->setPostUpdateExpr(Record.readSubExpr());
12009 }
12010 
12011 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12012   VisitOMPClauseWithPreInit(C);
12013   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12014   C->setNameModifierLoc(Record.readSourceLocation());
12015   C->setColonLoc(Record.readSourceLocation());
12016   C->setCondition(Record.readSubExpr());
12017   C->setLParenLoc(Record.readSourceLocation());
12018 }
12019 
12020 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12021   VisitOMPClauseWithPreInit(C);
12022   C->setCondition(Record.readSubExpr());
12023   C->setLParenLoc(Record.readSourceLocation());
12024 }
12025 
12026 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12027   VisitOMPClauseWithPreInit(C);
12028   C->setNumThreads(Record.readSubExpr());
12029   C->setLParenLoc(Record.readSourceLocation());
12030 }
12031 
12032 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12033   C->setSafelen(Record.readSubExpr());
12034   C->setLParenLoc(Record.readSourceLocation());
12035 }
12036 
12037 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12038   C->setSimdlen(Record.readSubExpr());
12039   C->setLParenLoc(Record.readSourceLocation());
12040 }
12041 
12042 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12043   for (Expr *&E : C->getSizesRefs())
12044     E = Record.readSubExpr();
12045   C->setLParenLoc(Record.readSourceLocation());
12046 }
12047 
12048 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12049 
12050 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12051   C->setFactor(Record.readSubExpr());
12052   C->setLParenLoc(Record.readSourceLocation());
12053 }
12054 
12055 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12056   C->setAllocator(Record.readExpr());
12057   C->setLParenLoc(Record.readSourceLocation());
12058 }
12059 
12060 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12061   C->setNumForLoops(Record.readSubExpr());
12062   C->setLParenLoc(Record.readSourceLocation());
12063 }
12064 
12065 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12066   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12067   C->setLParenLoc(Record.readSourceLocation());
12068   C->setDefaultKindKwLoc(Record.readSourceLocation());
12069 }
12070 
12071 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12072   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12073   C->setLParenLoc(Record.readSourceLocation());
12074   C->setProcBindKindKwLoc(Record.readSourceLocation());
12075 }
12076 
12077 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12078   VisitOMPClauseWithPreInit(C);
12079   C->setScheduleKind(
12080        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12081   C->setFirstScheduleModifier(
12082       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12083   C->setSecondScheduleModifier(
12084       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12085   C->setChunkSize(Record.readSubExpr());
12086   C->setLParenLoc(Record.readSourceLocation());
12087   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12088   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12089   C->setScheduleKindLoc(Record.readSourceLocation());
12090   C->setCommaLoc(Record.readSourceLocation());
12091 }
12092 
12093 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12094   C->setNumForLoops(Record.readSubExpr());
12095   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12096     C->setLoopNumIterations(I, Record.readSubExpr());
12097   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12098     C->setLoopCounter(I, Record.readSubExpr());
12099   C->setLParenLoc(Record.readSourceLocation());
12100 }
12101 
12102 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12103   C->setEventHandler(Record.readSubExpr());
12104   C->setLParenLoc(Record.readSourceLocation());
12105 }
12106 
12107 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12108 
12109 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12110 
12111 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12112 
12113 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12114 
12115 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12116 
12117 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12118   if (C->isExtended()) {
12119     C->setLParenLoc(Record.readSourceLocation());
12120     C->setArgumentLoc(Record.readSourceLocation());
12121     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12122   }
12123 }
12124 
12125 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12126 
12127 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12128 
12129 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12130 
12131 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12132 
12133 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12134 
12135 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12136 
12137 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12138 
12139 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12140 
12141 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12142 
12143 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12144   unsigned NumVars = C->varlist_size();
12145   SmallVector<Expr *, 16> Vars;
12146   Vars.reserve(NumVars);
12147   for (unsigned I = 0; I != NumVars; ++I)
12148     Vars.push_back(Record.readSubExpr());
12149   C->setVarRefs(Vars);
12150   C->setIsTarget(Record.readBool());
12151   C->setIsTargetSync(Record.readBool());
12152   C->setLParenLoc(Record.readSourceLocation());
12153   C->setVarLoc(Record.readSourceLocation());
12154 }
12155 
12156 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12157   C->setInteropVar(Record.readSubExpr());
12158   C->setLParenLoc(Record.readSourceLocation());
12159   C->setVarLoc(Record.readSourceLocation());
12160 }
12161 
12162 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12163   C->setInteropVar(Record.readSubExpr());
12164   C->setLParenLoc(Record.readSourceLocation());
12165   C->setVarLoc(Record.readSourceLocation());
12166 }
12167 
12168 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12169   VisitOMPClauseWithPreInit(C);
12170   C->setCondition(Record.readSubExpr());
12171   C->setLParenLoc(Record.readSourceLocation());
12172 }
12173 
12174 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12175   VisitOMPClauseWithPreInit(C);
12176   C->setCondition(Record.readSubExpr());
12177   C->setLParenLoc(Record.readSourceLocation());
12178 }
12179 
12180 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12181 
12182 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12183     OMPUnifiedSharedMemoryClause *) {}
12184 
12185 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12186 
12187 void
12188 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12189 }
12190 
12191 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12192     OMPAtomicDefaultMemOrderClause *C) {
12193   C->setAtomicDefaultMemOrderKind(
12194       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12195   C->setLParenLoc(Record.readSourceLocation());
12196   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12197 }
12198 
12199 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12200   C->setLParenLoc(Record.readSourceLocation());
12201   unsigned NumVars = C->varlist_size();
12202   SmallVector<Expr *, 16> Vars;
12203   Vars.reserve(NumVars);
12204   for (unsigned i = 0; i != NumVars; ++i)
12205     Vars.push_back(Record.readSubExpr());
12206   C->setVarRefs(Vars);
12207   Vars.clear();
12208   for (unsigned i = 0; i != NumVars; ++i)
12209     Vars.push_back(Record.readSubExpr());
12210   C->setPrivateCopies(Vars);
12211 }
12212 
12213 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12214   VisitOMPClauseWithPreInit(C);
12215   C->setLParenLoc(Record.readSourceLocation());
12216   unsigned NumVars = C->varlist_size();
12217   SmallVector<Expr *, 16> Vars;
12218   Vars.reserve(NumVars);
12219   for (unsigned i = 0; i != NumVars; ++i)
12220     Vars.push_back(Record.readSubExpr());
12221   C->setVarRefs(Vars);
12222   Vars.clear();
12223   for (unsigned i = 0; i != NumVars; ++i)
12224     Vars.push_back(Record.readSubExpr());
12225   C->setPrivateCopies(Vars);
12226   Vars.clear();
12227   for (unsigned i = 0; i != NumVars; ++i)
12228     Vars.push_back(Record.readSubExpr());
12229   C->setInits(Vars);
12230 }
12231 
12232 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12233   VisitOMPClauseWithPostUpdate(C);
12234   C->setLParenLoc(Record.readSourceLocation());
12235   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12236   C->setKindLoc(Record.readSourceLocation());
12237   C->setColonLoc(Record.readSourceLocation());
12238   unsigned NumVars = C->varlist_size();
12239   SmallVector<Expr *, 16> Vars;
12240   Vars.reserve(NumVars);
12241   for (unsigned i = 0; i != NumVars; ++i)
12242     Vars.push_back(Record.readSubExpr());
12243   C->setVarRefs(Vars);
12244   Vars.clear();
12245   for (unsigned i = 0; i != NumVars; ++i)
12246     Vars.push_back(Record.readSubExpr());
12247   C->setPrivateCopies(Vars);
12248   Vars.clear();
12249   for (unsigned i = 0; i != NumVars; ++i)
12250     Vars.push_back(Record.readSubExpr());
12251   C->setSourceExprs(Vars);
12252   Vars.clear();
12253   for (unsigned i = 0; i != NumVars; ++i)
12254     Vars.push_back(Record.readSubExpr());
12255   C->setDestinationExprs(Vars);
12256   Vars.clear();
12257   for (unsigned i = 0; i != NumVars; ++i)
12258     Vars.push_back(Record.readSubExpr());
12259   C->setAssignmentOps(Vars);
12260 }
12261 
12262 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12263   C->setLParenLoc(Record.readSourceLocation());
12264   unsigned NumVars = C->varlist_size();
12265   SmallVector<Expr *, 16> Vars;
12266   Vars.reserve(NumVars);
12267   for (unsigned i = 0; i != NumVars; ++i)
12268     Vars.push_back(Record.readSubExpr());
12269   C->setVarRefs(Vars);
12270 }
12271 
12272 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12273   VisitOMPClauseWithPostUpdate(C);
12274   C->setLParenLoc(Record.readSourceLocation());
12275   C->setModifierLoc(Record.readSourceLocation());
12276   C->setColonLoc(Record.readSourceLocation());
12277   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12278   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12279   C->setQualifierLoc(NNSL);
12280   C->setNameInfo(DNI);
12281 
12282   unsigned NumVars = C->varlist_size();
12283   SmallVector<Expr *, 16> Vars;
12284   Vars.reserve(NumVars);
12285   for (unsigned i = 0; i != NumVars; ++i)
12286     Vars.push_back(Record.readSubExpr());
12287   C->setVarRefs(Vars);
12288   Vars.clear();
12289   for (unsigned i = 0; i != NumVars; ++i)
12290     Vars.push_back(Record.readSubExpr());
12291   C->setPrivates(Vars);
12292   Vars.clear();
12293   for (unsigned i = 0; i != NumVars; ++i)
12294     Vars.push_back(Record.readSubExpr());
12295   C->setLHSExprs(Vars);
12296   Vars.clear();
12297   for (unsigned i = 0; i != NumVars; ++i)
12298     Vars.push_back(Record.readSubExpr());
12299   C->setRHSExprs(Vars);
12300   Vars.clear();
12301   for (unsigned i = 0; i != NumVars; ++i)
12302     Vars.push_back(Record.readSubExpr());
12303   C->setReductionOps(Vars);
12304   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12305     Vars.clear();
12306     for (unsigned i = 0; i != NumVars; ++i)
12307       Vars.push_back(Record.readSubExpr());
12308     C->setInscanCopyOps(Vars);
12309     Vars.clear();
12310     for (unsigned i = 0; i != NumVars; ++i)
12311       Vars.push_back(Record.readSubExpr());
12312     C->setInscanCopyArrayTemps(Vars);
12313     Vars.clear();
12314     for (unsigned i = 0; i != NumVars; ++i)
12315       Vars.push_back(Record.readSubExpr());
12316     C->setInscanCopyArrayElems(Vars);
12317   }
12318 }
12319 
12320 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12321   VisitOMPClauseWithPostUpdate(C);
12322   C->setLParenLoc(Record.readSourceLocation());
12323   C->setColonLoc(Record.readSourceLocation());
12324   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12325   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12326   C->setQualifierLoc(NNSL);
12327   C->setNameInfo(DNI);
12328 
12329   unsigned NumVars = C->varlist_size();
12330   SmallVector<Expr *, 16> Vars;
12331   Vars.reserve(NumVars);
12332   for (unsigned I = 0; I != NumVars; ++I)
12333     Vars.push_back(Record.readSubExpr());
12334   C->setVarRefs(Vars);
12335   Vars.clear();
12336   for (unsigned I = 0; I != NumVars; ++I)
12337     Vars.push_back(Record.readSubExpr());
12338   C->setPrivates(Vars);
12339   Vars.clear();
12340   for (unsigned I = 0; I != NumVars; ++I)
12341     Vars.push_back(Record.readSubExpr());
12342   C->setLHSExprs(Vars);
12343   Vars.clear();
12344   for (unsigned I = 0; I != NumVars; ++I)
12345     Vars.push_back(Record.readSubExpr());
12346   C->setRHSExprs(Vars);
12347   Vars.clear();
12348   for (unsigned I = 0; I != NumVars; ++I)
12349     Vars.push_back(Record.readSubExpr());
12350   C->setReductionOps(Vars);
12351 }
12352 
12353 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12354   VisitOMPClauseWithPostUpdate(C);
12355   C->setLParenLoc(Record.readSourceLocation());
12356   C->setColonLoc(Record.readSourceLocation());
12357   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12358   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12359   C->setQualifierLoc(NNSL);
12360   C->setNameInfo(DNI);
12361 
12362   unsigned NumVars = C->varlist_size();
12363   SmallVector<Expr *, 16> Vars;
12364   Vars.reserve(NumVars);
12365   for (unsigned I = 0; I != NumVars; ++I)
12366     Vars.push_back(Record.readSubExpr());
12367   C->setVarRefs(Vars);
12368   Vars.clear();
12369   for (unsigned I = 0; I != NumVars; ++I)
12370     Vars.push_back(Record.readSubExpr());
12371   C->setPrivates(Vars);
12372   Vars.clear();
12373   for (unsigned I = 0; I != NumVars; ++I)
12374     Vars.push_back(Record.readSubExpr());
12375   C->setLHSExprs(Vars);
12376   Vars.clear();
12377   for (unsigned I = 0; I != NumVars; ++I)
12378     Vars.push_back(Record.readSubExpr());
12379   C->setRHSExprs(Vars);
12380   Vars.clear();
12381   for (unsigned I = 0; I != NumVars; ++I)
12382     Vars.push_back(Record.readSubExpr());
12383   C->setReductionOps(Vars);
12384   Vars.clear();
12385   for (unsigned I = 0; I != NumVars; ++I)
12386     Vars.push_back(Record.readSubExpr());
12387   C->setTaskgroupDescriptors(Vars);
12388 }
12389 
12390 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12391   VisitOMPClauseWithPostUpdate(C);
12392   C->setLParenLoc(Record.readSourceLocation());
12393   C->setColonLoc(Record.readSourceLocation());
12394   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12395   C->setModifierLoc(Record.readSourceLocation());
12396   unsigned NumVars = C->varlist_size();
12397   SmallVector<Expr *, 16> Vars;
12398   Vars.reserve(NumVars);
12399   for (unsigned i = 0; i != NumVars; ++i)
12400     Vars.push_back(Record.readSubExpr());
12401   C->setVarRefs(Vars);
12402   Vars.clear();
12403   for (unsigned i = 0; i != NumVars; ++i)
12404     Vars.push_back(Record.readSubExpr());
12405   C->setPrivates(Vars);
12406   Vars.clear();
12407   for (unsigned i = 0; i != NumVars; ++i)
12408     Vars.push_back(Record.readSubExpr());
12409   C->setInits(Vars);
12410   Vars.clear();
12411   for (unsigned i = 0; i != NumVars; ++i)
12412     Vars.push_back(Record.readSubExpr());
12413   C->setUpdates(Vars);
12414   Vars.clear();
12415   for (unsigned i = 0; i != NumVars; ++i)
12416     Vars.push_back(Record.readSubExpr());
12417   C->setFinals(Vars);
12418   C->setStep(Record.readSubExpr());
12419   C->setCalcStep(Record.readSubExpr());
12420   Vars.clear();
12421   for (unsigned I = 0; I != NumVars + 1; ++I)
12422     Vars.push_back(Record.readSubExpr());
12423   C->setUsedExprs(Vars);
12424 }
12425 
12426 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12427   C->setLParenLoc(Record.readSourceLocation());
12428   C->setColonLoc(Record.readSourceLocation());
12429   unsigned NumVars = C->varlist_size();
12430   SmallVector<Expr *, 16> Vars;
12431   Vars.reserve(NumVars);
12432   for (unsigned i = 0; i != NumVars; ++i)
12433     Vars.push_back(Record.readSubExpr());
12434   C->setVarRefs(Vars);
12435   C->setAlignment(Record.readSubExpr());
12436 }
12437 
12438 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12439   C->setLParenLoc(Record.readSourceLocation());
12440   unsigned NumVars = C->varlist_size();
12441   SmallVector<Expr *, 16> Exprs;
12442   Exprs.reserve(NumVars);
12443   for (unsigned i = 0; i != NumVars; ++i)
12444     Exprs.push_back(Record.readSubExpr());
12445   C->setVarRefs(Exprs);
12446   Exprs.clear();
12447   for (unsigned i = 0; i != NumVars; ++i)
12448     Exprs.push_back(Record.readSubExpr());
12449   C->setSourceExprs(Exprs);
12450   Exprs.clear();
12451   for (unsigned i = 0; i != NumVars; ++i)
12452     Exprs.push_back(Record.readSubExpr());
12453   C->setDestinationExprs(Exprs);
12454   Exprs.clear();
12455   for (unsigned i = 0; i != NumVars; ++i)
12456     Exprs.push_back(Record.readSubExpr());
12457   C->setAssignmentOps(Exprs);
12458 }
12459 
12460 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12461   C->setLParenLoc(Record.readSourceLocation());
12462   unsigned NumVars = C->varlist_size();
12463   SmallVector<Expr *, 16> Exprs;
12464   Exprs.reserve(NumVars);
12465   for (unsigned i = 0; i != NumVars; ++i)
12466     Exprs.push_back(Record.readSubExpr());
12467   C->setVarRefs(Exprs);
12468   Exprs.clear();
12469   for (unsigned i = 0; i != NumVars; ++i)
12470     Exprs.push_back(Record.readSubExpr());
12471   C->setSourceExprs(Exprs);
12472   Exprs.clear();
12473   for (unsigned i = 0; i != NumVars; ++i)
12474     Exprs.push_back(Record.readSubExpr());
12475   C->setDestinationExprs(Exprs);
12476   Exprs.clear();
12477   for (unsigned i = 0; i != NumVars; ++i)
12478     Exprs.push_back(Record.readSubExpr());
12479   C->setAssignmentOps(Exprs);
12480 }
12481 
12482 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12483   C->setLParenLoc(Record.readSourceLocation());
12484   unsigned NumVars = C->varlist_size();
12485   SmallVector<Expr *, 16> Vars;
12486   Vars.reserve(NumVars);
12487   for (unsigned i = 0; i != NumVars; ++i)
12488     Vars.push_back(Record.readSubExpr());
12489   C->setVarRefs(Vars);
12490 }
12491 
12492 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12493   C->setDepobj(Record.readSubExpr());
12494   C->setLParenLoc(Record.readSourceLocation());
12495 }
12496 
12497 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12498   C->setLParenLoc(Record.readSourceLocation());
12499   C->setModifier(Record.readSubExpr());
12500   C->setDependencyKind(
12501       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12502   C->setDependencyLoc(Record.readSourceLocation());
12503   C->setColonLoc(Record.readSourceLocation());
12504   unsigned NumVars = C->varlist_size();
12505   SmallVector<Expr *, 16> Vars;
12506   Vars.reserve(NumVars);
12507   for (unsigned I = 0; I != NumVars; ++I)
12508     Vars.push_back(Record.readSubExpr());
12509   C->setVarRefs(Vars);
12510   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12511     C->setLoopData(I, Record.readSubExpr());
12512 }
12513 
12514 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12515   VisitOMPClauseWithPreInit(C);
12516   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12517   C->setDevice(Record.readSubExpr());
12518   C->setModifierLoc(Record.readSourceLocation());
12519   C->setLParenLoc(Record.readSourceLocation());
12520 }
12521 
12522 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12523   C->setLParenLoc(Record.readSourceLocation());
12524   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12525     C->setMapTypeModifier(
12526         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12527     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12528   }
12529   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12530   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12531   C->setMapType(
12532      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12533   C->setMapLoc(Record.readSourceLocation());
12534   C->setColonLoc(Record.readSourceLocation());
12535   auto NumVars = C->varlist_size();
12536   auto UniqueDecls = C->getUniqueDeclarationsNum();
12537   auto TotalLists = C->getTotalComponentListNum();
12538   auto TotalComponents = C->getTotalComponentsNum();
12539 
12540   SmallVector<Expr *, 16> Vars;
12541   Vars.reserve(NumVars);
12542   for (unsigned i = 0; i != NumVars; ++i)
12543     Vars.push_back(Record.readExpr());
12544   C->setVarRefs(Vars);
12545 
12546   SmallVector<Expr *, 16> UDMappers;
12547   UDMappers.reserve(NumVars);
12548   for (unsigned I = 0; I < NumVars; ++I)
12549     UDMappers.push_back(Record.readExpr());
12550   C->setUDMapperRefs(UDMappers);
12551 
12552   SmallVector<ValueDecl *, 16> Decls;
12553   Decls.reserve(UniqueDecls);
12554   for (unsigned i = 0; i < UniqueDecls; ++i)
12555     Decls.push_back(Record.readDeclAs<ValueDecl>());
12556   C->setUniqueDecls(Decls);
12557 
12558   SmallVector<unsigned, 16> ListsPerDecl;
12559   ListsPerDecl.reserve(UniqueDecls);
12560   for (unsigned i = 0; i < UniqueDecls; ++i)
12561     ListsPerDecl.push_back(Record.readInt());
12562   C->setDeclNumLists(ListsPerDecl);
12563 
12564   SmallVector<unsigned, 32> ListSizes;
12565   ListSizes.reserve(TotalLists);
12566   for (unsigned i = 0; i < TotalLists; ++i)
12567     ListSizes.push_back(Record.readInt());
12568   C->setComponentListSizes(ListSizes);
12569 
12570   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12571   Components.reserve(TotalComponents);
12572   for (unsigned i = 0; i < TotalComponents; ++i) {
12573     Expr *AssociatedExprPr = Record.readExpr();
12574     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12575     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12576                             /*IsNonContiguous=*/false);
12577   }
12578   C->setComponents(Components, ListSizes);
12579 }
12580 
12581 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12582   C->setLParenLoc(Record.readSourceLocation());
12583   C->setColonLoc(Record.readSourceLocation());
12584   C->setAllocator(Record.readSubExpr());
12585   unsigned NumVars = C->varlist_size();
12586   SmallVector<Expr *, 16> Vars;
12587   Vars.reserve(NumVars);
12588   for (unsigned i = 0; i != NumVars; ++i)
12589     Vars.push_back(Record.readSubExpr());
12590   C->setVarRefs(Vars);
12591 }
12592 
12593 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12594   VisitOMPClauseWithPreInit(C);
12595   C->setNumTeams(Record.readSubExpr());
12596   C->setLParenLoc(Record.readSourceLocation());
12597 }
12598 
12599 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12600   VisitOMPClauseWithPreInit(C);
12601   C->setThreadLimit(Record.readSubExpr());
12602   C->setLParenLoc(Record.readSourceLocation());
12603 }
12604 
12605 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12606   VisitOMPClauseWithPreInit(C);
12607   C->setPriority(Record.readSubExpr());
12608   C->setLParenLoc(Record.readSourceLocation());
12609 }
12610 
12611 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12612   VisitOMPClauseWithPreInit(C);
12613   C->setGrainsize(Record.readSubExpr());
12614   C->setLParenLoc(Record.readSourceLocation());
12615 }
12616 
12617 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12618   VisitOMPClauseWithPreInit(C);
12619   C->setNumTasks(Record.readSubExpr());
12620   C->setLParenLoc(Record.readSourceLocation());
12621 }
12622 
12623 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12624   C->setHint(Record.readSubExpr());
12625   C->setLParenLoc(Record.readSourceLocation());
12626 }
12627 
12628 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12629   VisitOMPClauseWithPreInit(C);
12630   C->setDistScheduleKind(
12631       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12632   C->setChunkSize(Record.readSubExpr());
12633   C->setLParenLoc(Record.readSourceLocation());
12634   C->setDistScheduleKindLoc(Record.readSourceLocation());
12635   C->setCommaLoc(Record.readSourceLocation());
12636 }
12637 
12638 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12639   C->setDefaultmapKind(
12640        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12641   C->setDefaultmapModifier(
12642       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12643   C->setLParenLoc(Record.readSourceLocation());
12644   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12645   C->setDefaultmapKindLoc(Record.readSourceLocation());
12646 }
12647 
12648 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12649   C->setLParenLoc(Record.readSourceLocation());
12650   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12651     C->setMotionModifier(
12652         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12653     C->setMotionModifierLoc(I, Record.readSourceLocation());
12654   }
12655   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12656   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12657   C->setColonLoc(Record.readSourceLocation());
12658   auto NumVars = C->varlist_size();
12659   auto UniqueDecls = C->getUniqueDeclarationsNum();
12660   auto TotalLists = C->getTotalComponentListNum();
12661   auto TotalComponents = C->getTotalComponentsNum();
12662 
12663   SmallVector<Expr *, 16> Vars;
12664   Vars.reserve(NumVars);
12665   for (unsigned i = 0; i != NumVars; ++i)
12666     Vars.push_back(Record.readSubExpr());
12667   C->setVarRefs(Vars);
12668 
12669   SmallVector<Expr *, 16> UDMappers;
12670   UDMappers.reserve(NumVars);
12671   for (unsigned I = 0; I < NumVars; ++I)
12672     UDMappers.push_back(Record.readSubExpr());
12673   C->setUDMapperRefs(UDMappers);
12674 
12675   SmallVector<ValueDecl *, 16> Decls;
12676   Decls.reserve(UniqueDecls);
12677   for (unsigned i = 0; i < UniqueDecls; ++i)
12678     Decls.push_back(Record.readDeclAs<ValueDecl>());
12679   C->setUniqueDecls(Decls);
12680 
12681   SmallVector<unsigned, 16> ListsPerDecl;
12682   ListsPerDecl.reserve(UniqueDecls);
12683   for (unsigned i = 0; i < UniqueDecls; ++i)
12684     ListsPerDecl.push_back(Record.readInt());
12685   C->setDeclNumLists(ListsPerDecl);
12686 
12687   SmallVector<unsigned, 32> ListSizes;
12688   ListSizes.reserve(TotalLists);
12689   for (unsigned i = 0; i < TotalLists; ++i)
12690     ListSizes.push_back(Record.readInt());
12691   C->setComponentListSizes(ListSizes);
12692 
12693   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12694   Components.reserve(TotalComponents);
12695   for (unsigned i = 0; i < TotalComponents; ++i) {
12696     Expr *AssociatedExprPr = Record.readSubExpr();
12697     bool IsNonContiguous = Record.readBool();
12698     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12699     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12700   }
12701   C->setComponents(Components, ListSizes);
12702 }
12703 
12704 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12705   C->setLParenLoc(Record.readSourceLocation());
12706   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12707     C->setMotionModifier(
12708         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12709     C->setMotionModifierLoc(I, Record.readSourceLocation());
12710   }
12711   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12712   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12713   C->setColonLoc(Record.readSourceLocation());
12714   auto NumVars = C->varlist_size();
12715   auto UniqueDecls = C->getUniqueDeclarationsNum();
12716   auto TotalLists = C->getTotalComponentListNum();
12717   auto TotalComponents = C->getTotalComponentsNum();
12718 
12719   SmallVector<Expr *, 16> Vars;
12720   Vars.reserve(NumVars);
12721   for (unsigned i = 0; i != NumVars; ++i)
12722     Vars.push_back(Record.readSubExpr());
12723   C->setVarRefs(Vars);
12724 
12725   SmallVector<Expr *, 16> UDMappers;
12726   UDMappers.reserve(NumVars);
12727   for (unsigned I = 0; I < NumVars; ++I)
12728     UDMappers.push_back(Record.readSubExpr());
12729   C->setUDMapperRefs(UDMappers);
12730 
12731   SmallVector<ValueDecl *, 16> Decls;
12732   Decls.reserve(UniqueDecls);
12733   for (unsigned i = 0; i < UniqueDecls; ++i)
12734     Decls.push_back(Record.readDeclAs<ValueDecl>());
12735   C->setUniqueDecls(Decls);
12736 
12737   SmallVector<unsigned, 16> ListsPerDecl;
12738   ListsPerDecl.reserve(UniqueDecls);
12739   for (unsigned i = 0; i < UniqueDecls; ++i)
12740     ListsPerDecl.push_back(Record.readInt());
12741   C->setDeclNumLists(ListsPerDecl);
12742 
12743   SmallVector<unsigned, 32> ListSizes;
12744   ListSizes.reserve(TotalLists);
12745   for (unsigned i = 0; i < TotalLists; ++i)
12746     ListSizes.push_back(Record.readInt());
12747   C->setComponentListSizes(ListSizes);
12748 
12749   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12750   Components.reserve(TotalComponents);
12751   for (unsigned i = 0; i < TotalComponents; ++i) {
12752     Expr *AssociatedExprPr = Record.readSubExpr();
12753     bool IsNonContiguous = Record.readBool();
12754     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12755     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12756   }
12757   C->setComponents(Components, ListSizes);
12758 }
12759 
12760 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12761   C->setLParenLoc(Record.readSourceLocation());
12762   auto NumVars = C->varlist_size();
12763   auto UniqueDecls = C->getUniqueDeclarationsNum();
12764   auto TotalLists = C->getTotalComponentListNum();
12765   auto TotalComponents = C->getTotalComponentsNum();
12766 
12767   SmallVector<Expr *, 16> Vars;
12768   Vars.reserve(NumVars);
12769   for (unsigned i = 0; i != NumVars; ++i)
12770     Vars.push_back(Record.readSubExpr());
12771   C->setVarRefs(Vars);
12772   Vars.clear();
12773   for (unsigned i = 0; i != NumVars; ++i)
12774     Vars.push_back(Record.readSubExpr());
12775   C->setPrivateCopies(Vars);
12776   Vars.clear();
12777   for (unsigned i = 0; i != NumVars; ++i)
12778     Vars.push_back(Record.readSubExpr());
12779   C->setInits(Vars);
12780 
12781   SmallVector<ValueDecl *, 16> Decls;
12782   Decls.reserve(UniqueDecls);
12783   for (unsigned i = 0; i < UniqueDecls; ++i)
12784     Decls.push_back(Record.readDeclAs<ValueDecl>());
12785   C->setUniqueDecls(Decls);
12786 
12787   SmallVector<unsigned, 16> ListsPerDecl;
12788   ListsPerDecl.reserve(UniqueDecls);
12789   for (unsigned i = 0; i < UniqueDecls; ++i)
12790     ListsPerDecl.push_back(Record.readInt());
12791   C->setDeclNumLists(ListsPerDecl);
12792 
12793   SmallVector<unsigned, 32> ListSizes;
12794   ListSizes.reserve(TotalLists);
12795   for (unsigned i = 0; i < TotalLists; ++i)
12796     ListSizes.push_back(Record.readInt());
12797   C->setComponentListSizes(ListSizes);
12798 
12799   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12800   Components.reserve(TotalComponents);
12801   for (unsigned i = 0; i < TotalComponents; ++i) {
12802     auto *AssociatedExprPr = Record.readSubExpr();
12803     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12804     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12805                             /*IsNonContiguous=*/false);
12806   }
12807   C->setComponents(Components, ListSizes);
12808 }
12809 
12810 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12811   C->setLParenLoc(Record.readSourceLocation());
12812   auto NumVars = C->varlist_size();
12813   auto UniqueDecls = C->getUniqueDeclarationsNum();
12814   auto TotalLists = C->getTotalComponentListNum();
12815   auto TotalComponents = C->getTotalComponentsNum();
12816 
12817   SmallVector<Expr *, 16> Vars;
12818   Vars.reserve(NumVars);
12819   for (unsigned i = 0; i != NumVars; ++i)
12820     Vars.push_back(Record.readSubExpr());
12821   C->setVarRefs(Vars);
12822 
12823   SmallVector<ValueDecl *, 16> Decls;
12824   Decls.reserve(UniqueDecls);
12825   for (unsigned i = 0; i < UniqueDecls; ++i)
12826     Decls.push_back(Record.readDeclAs<ValueDecl>());
12827   C->setUniqueDecls(Decls);
12828 
12829   SmallVector<unsigned, 16> ListsPerDecl;
12830   ListsPerDecl.reserve(UniqueDecls);
12831   for (unsigned i = 0; i < UniqueDecls; ++i)
12832     ListsPerDecl.push_back(Record.readInt());
12833   C->setDeclNumLists(ListsPerDecl);
12834 
12835   SmallVector<unsigned, 32> ListSizes;
12836   ListSizes.reserve(TotalLists);
12837   for (unsigned i = 0; i < TotalLists; ++i)
12838     ListSizes.push_back(Record.readInt());
12839   C->setComponentListSizes(ListSizes);
12840 
12841   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12842   Components.reserve(TotalComponents);
12843   for (unsigned i = 0; i < TotalComponents; ++i) {
12844     Expr *AssociatedExpr = Record.readSubExpr();
12845     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12846     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12847                             /*IsNonContiguous*/ false);
12848   }
12849   C->setComponents(Components, ListSizes);
12850 }
12851 
12852 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12853   C->setLParenLoc(Record.readSourceLocation());
12854   auto NumVars = C->varlist_size();
12855   auto UniqueDecls = C->getUniqueDeclarationsNum();
12856   auto TotalLists = C->getTotalComponentListNum();
12857   auto TotalComponents = C->getTotalComponentsNum();
12858 
12859   SmallVector<Expr *, 16> Vars;
12860   Vars.reserve(NumVars);
12861   for (unsigned i = 0; i != NumVars; ++i)
12862     Vars.push_back(Record.readSubExpr());
12863   C->setVarRefs(Vars);
12864   Vars.clear();
12865 
12866   SmallVector<ValueDecl *, 16> Decls;
12867   Decls.reserve(UniqueDecls);
12868   for (unsigned i = 0; i < UniqueDecls; ++i)
12869     Decls.push_back(Record.readDeclAs<ValueDecl>());
12870   C->setUniqueDecls(Decls);
12871 
12872   SmallVector<unsigned, 16> ListsPerDecl;
12873   ListsPerDecl.reserve(UniqueDecls);
12874   for (unsigned i = 0; i < UniqueDecls; ++i)
12875     ListsPerDecl.push_back(Record.readInt());
12876   C->setDeclNumLists(ListsPerDecl);
12877 
12878   SmallVector<unsigned, 32> ListSizes;
12879   ListSizes.reserve(TotalLists);
12880   for (unsigned i = 0; i < TotalLists; ++i)
12881     ListSizes.push_back(Record.readInt());
12882   C->setComponentListSizes(ListSizes);
12883 
12884   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12885   Components.reserve(TotalComponents);
12886   for (unsigned i = 0; i < TotalComponents; ++i) {
12887     Expr *AssociatedExpr = Record.readSubExpr();
12888     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12889     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12890                             /*IsNonContiguous=*/false);
12891   }
12892   C->setComponents(Components, ListSizes);
12893 }
12894 
12895 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12896   C->setLParenLoc(Record.readSourceLocation());
12897   unsigned NumVars = C->varlist_size();
12898   SmallVector<Expr *, 16> Vars;
12899   Vars.reserve(NumVars);
12900   for (unsigned i = 0; i != NumVars; ++i)
12901     Vars.push_back(Record.readSubExpr());
12902   C->setVarRefs(Vars);
12903   Vars.clear();
12904   Vars.reserve(NumVars);
12905   for (unsigned i = 0; i != NumVars; ++i)
12906     Vars.push_back(Record.readSubExpr());
12907   C->setPrivateRefs(Vars);
12908 }
12909 
12910 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12911   C->setLParenLoc(Record.readSourceLocation());
12912   unsigned NumVars = C->varlist_size();
12913   SmallVector<Expr *, 16> Vars;
12914   Vars.reserve(NumVars);
12915   for (unsigned i = 0; i != NumVars; ++i)
12916     Vars.push_back(Record.readSubExpr());
12917   C->setVarRefs(Vars);
12918 }
12919 
12920 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12921   C->setLParenLoc(Record.readSourceLocation());
12922   unsigned NumVars = C->varlist_size();
12923   SmallVector<Expr *, 16> Vars;
12924   Vars.reserve(NumVars);
12925   for (unsigned i = 0; i != NumVars; ++i)
12926     Vars.push_back(Record.readSubExpr());
12927   C->setVarRefs(Vars);
12928 }
12929 
12930 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12931   C->setLParenLoc(Record.readSourceLocation());
12932   unsigned NumOfAllocators = C->getNumberOfAllocators();
12933   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12934   Data.reserve(NumOfAllocators);
12935   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12936     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12937     D.Allocator = Record.readSubExpr();
12938     D.AllocatorTraits = Record.readSubExpr();
12939     D.LParenLoc = Record.readSourceLocation();
12940     D.RParenLoc = Record.readSourceLocation();
12941   }
12942   C->setAllocatorsData(Data);
12943 }
12944 
12945 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12946   C->setLParenLoc(Record.readSourceLocation());
12947   C->setModifier(Record.readSubExpr());
12948   C->setColonLoc(Record.readSourceLocation());
12949   unsigned NumOfLocators = C->varlist_size();
12950   SmallVector<Expr *, 4> Locators;
12951   Locators.reserve(NumOfLocators);
12952   for (unsigned I = 0; I != NumOfLocators; ++I)
12953     Locators.push_back(Record.readSubExpr());
12954   C->setVarRefs(Locators);
12955 }
12956 
12957 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12958   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12959   C->setLParenLoc(Record.readSourceLocation());
12960   C->setKindKwLoc(Record.readSourceLocation());
12961 }
12962 
12963 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12964   VisitOMPClauseWithPreInit(C);
12965   C->setThreadID(Record.readSubExpr());
12966   C->setLParenLoc(Record.readSourceLocation());
12967 }
12968 
12969 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12970   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12971   TI.Sets.resize(readUInt32());
12972   for (auto &Set : TI.Sets) {
12973     Set.Kind = readEnum<llvm::omp::TraitSet>();
12974     Set.Selectors.resize(readUInt32());
12975     for (auto &Selector : Set.Selectors) {
12976       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12977       Selector.ScoreOrCondition = nullptr;
12978       if (readBool())
12979         Selector.ScoreOrCondition = readExprRef();
12980       Selector.Properties.resize(readUInt32());
12981       for (auto &Property : Selector.Properties)
12982         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12983     }
12984   }
12985   return &TI;
12986 }
12987 
12988 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12989   if (!Data)
12990     return;
12991   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12992     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12993     skipInts(3);
12994   }
12995   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12996   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12997     Clauses[I] = readOMPClause();
12998   Data->setClauses(Clauses);
12999   if (Data->hasAssociatedStmt())
13000     Data->setAssociatedStmt(readStmt());
13001   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13002     Data->getChildren()[I] = readStmt();
13003 }
13004