1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ASTCommon.h"
14 #include "ASTReaderInternals.h"
15 #include "clang/AST/ASTConsumer.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/ASTUnresolvedSet.h"
19 #include "clang/AST/AbstractTypeReader.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclFriend.h"
24 #include "clang/AST/DeclGroup.h"
25 #include "clang/AST/DeclObjC.h"
26 #include "clang/AST/DeclTemplate.h"
27 #include "clang/AST/DeclarationName.h"
28 #include "clang/AST/Expr.h"
29 #include "clang/AST/ExprCXX.h"
30 #include "clang/AST/ExternalASTSource.h"
31 #include "clang/AST/NestedNameSpecifier.h"
32 #include "clang/AST/ODRHash.h"
33 #include "clang/AST/OpenMPClause.h"
34 #include "clang/AST/RawCommentList.h"
35 #include "clang/AST/TemplateBase.h"
36 #include "clang/AST/TemplateName.h"
37 #include "clang/AST/Type.h"
38 #include "clang/AST/TypeLoc.h"
39 #include "clang/AST/TypeLocVisitor.h"
40 #include "clang/AST/UnresolvedSet.h"
41 #include "clang/Basic/CommentOptions.h"
42 #include "clang/Basic/Diagnostic.h"
43 #include "clang/Basic/DiagnosticError.h"
44 #include "clang/Basic/DiagnosticOptions.h"
45 #include "clang/Basic/ExceptionSpecificationType.h"
46 #include "clang/Basic/FileManager.h"
47 #include "clang/Basic/FileSystemOptions.h"
48 #include "clang/Basic/IdentifierTable.h"
49 #include "clang/Basic/LLVM.h"
50 #include "clang/Basic/LangOptions.h"
51 #include "clang/Basic/Module.h"
52 #include "clang/Basic/ObjCRuntime.h"
53 #include "clang/Basic/OpenMPKinds.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ASTRecordReader.h"
80 #include "clang/Serialization/ContinuousRangeMap.h"
81 #include "clang/Serialization/GlobalModuleIndex.h"
82 #include "clang/Serialization/InMemoryModuleCache.h"
83 #include "clang/Serialization/ModuleFile.h"
84 #include "clang/Serialization/ModuleFileExtension.h"
85 #include "clang/Serialization/ModuleManager.h"
86 #include "clang/Serialization/PCHContainerOperations.h"
87 #include "clang/Serialization/SerializationDiagnostic.h"
88 #include "llvm/ADT/APFloat.h"
89 #include "llvm/ADT/APInt.h"
90 #include "llvm/ADT/APSInt.h"
91 #include "llvm/ADT/ArrayRef.h"
92 #include "llvm/ADT/DenseMap.h"
93 #include "llvm/ADT/FloatingPointMode.h"
94 #include "llvm/ADT/FoldingSet.h"
95 #include "llvm/ADT/Hashing.h"
96 #include "llvm/ADT/IntrusiveRefCntPtr.h"
97 #include "llvm/ADT/None.h"
98 #include "llvm/ADT/Optional.h"
99 #include "llvm/ADT/STLExtras.h"
100 #include "llvm/ADT/ScopeExit.h"
101 #include "llvm/ADT/SmallPtrSet.h"
102 #include "llvm/ADT/SmallString.h"
103 #include "llvm/ADT/SmallVector.h"
104 #include "llvm/ADT/StringExtras.h"
105 #include "llvm/ADT/StringMap.h"
106 #include "llvm/ADT/StringRef.h"
107 #include "llvm/ADT/Triple.h"
108 #include "llvm/ADT/iterator_range.h"
109 #include "llvm/Bitstream/BitstreamReader.h"
110 #include "llvm/Support/Casting.h"
111 #include "llvm/Support/Compiler.h"
112 #include "llvm/Support/Compression.h"
113 #include "llvm/Support/DJB.h"
114 #include "llvm/Support/Endian.h"
115 #include "llvm/Support/Error.h"
116 #include "llvm/Support/ErrorHandling.h"
117 #include "llvm/Support/FileSystem.h"
118 #include "llvm/Support/LEB128.h"
119 #include "llvm/Support/MemoryBuffer.h"
120 #include "llvm/Support/Path.h"
121 #include "llvm/Support/SaveAndRestore.h"
122 #include "llvm/Support/Timer.h"
123 #include "llvm/Support/VersionTuple.h"
124 #include "llvm/Support/raw_ostream.h"
125 #include <algorithm>
126 #include <cassert>
127 #include <cstddef>
128 #include <cstdint>
129 #include <cstdio>
130 #include <ctime>
131 #include <iterator>
132 #include <limits>
133 #include <map>
134 #include <memory>
135 #include <string>
136 #include <system_error>
137 #include <tuple>
138 #include <utility>
139 #include <vector>
140 
141 using namespace clang;
142 using namespace clang::serialization;
143 using namespace clang::serialization::reader;
144 using llvm::BitstreamCursor;
145 
146 //===----------------------------------------------------------------------===//
147 // ChainedASTReaderListener implementation
148 //===----------------------------------------------------------------------===//
149 
150 bool
151 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
152   return First->ReadFullVersionInformation(FullVersion) ||
153          Second->ReadFullVersionInformation(FullVersion);
154 }
155 
156 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
157   First->ReadModuleName(ModuleName);
158   Second->ReadModuleName(ModuleName);
159 }
160 
161 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
162   First->ReadModuleMapFile(ModuleMapPath);
163   Second->ReadModuleMapFile(ModuleMapPath);
164 }
165 
166 bool
167 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
168                                               bool Complain,
169                                               bool AllowCompatibleDifferences) {
170   return First->ReadLanguageOptions(LangOpts, Complain,
171                                     AllowCompatibleDifferences) ||
172          Second->ReadLanguageOptions(LangOpts, Complain,
173                                      AllowCompatibleDifferences);
174 }
175 
176 bool ChainedASTReaderListener::ReadTargetOptions(
177     const TargetOptions &TargetOpts, bool Complain,
178     bool AllowCompatibleDifferences) {
179   return First->ReadTargetOptions(TargetOpts, Complain,
180                                   AllowCompatibleDifferences) ||
181          Second->ReadTargetOptions(TargetOpts, Complain,
182                                    AllowCompatibleDifferences);
183 }
184 
185 bool ChainedASTReaderListener::ReadDiagnosticOptions(
186     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
187   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
188          Second->ReadDiagnosticOptions(DiagOpts, Complain);
189 }
190 
191 bool
192 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
193                                                 bool Complain) {
194   return First->ReadFileSystemOptions(FSOpts, Complain) ||
195          Second->ReadFileSystemOptions(FSOpts, Complain);
196 }
197 
198 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
199     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
200     bool Complain) {
201   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
202                                         Complain) ||
203          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
204                                          Complain);
205 }
206 
207 bool ChainedASTReaderListener::ReadPreprocessorOptions(
208     const PreprocessorOptions &PPOpts, bool Complain,
209     std::string &SuggestedPredefines) {
210   return First->ReadPreprocessorOptions(PPOpts, Complain,
211                                         SuggestedPredefines) ||
212          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
213 }
214 
215 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
216                                            unsigned Value) {
217   First->ReadCounter(M, Value);
218   Second->ReadCounter(M, Value);
219 }
220 
221 bool ChainedASTReaderListener::needsInputFileVisitation() {
222   return First->needsInputFileVisitation() ||
223          Second->needsInputFileVisitation();
224 }
225 
226 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
227   return First->needsSystemInputFileVisitation() ||
228   Second->needsSystemInputFileVisitation();
229 }
230 
231 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
232                                                ModuleKind Kind) {
233   First->visitModuleFile(Filename, Kind);
234   Second->visitModuleFile(Filename, Kind);
235 }
236 
237 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
238                                               bool isSystem,
239                                               bool isOverridden,
240                                               bool isExplicitModule) {
241   bool Continue = false;
242   if (First->needsInputFileVisitation() &&
243       (!isSystem || First->needsSystemInputFileVisitation()))
244     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
245                                       isExplicitModule);
246   if (Second->needsInputFileVisitation() &&
247       (!isSystem || Second->needsSystemInputFileVisitation()))
248     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
249                                        isExplicitModule);
250   return Continue;
251 }
252 
253 void ChainedASTReaderListener::readModuleFileExtension(
254        const ModuleFileExtensionMetadata &Metadata) {
255   First->readModuleFileExtension(Metadata);
256   Second->readModuleFileExtension(Metadata);
257 }
258 
259 //===----------------------------------------------------------------------===//
260 // PCH validator implementation
261 //===----------------------------------------------------------------------===//
262 
263 ASTReaderListener::~ASTReaderListener() = default;
264 
265 /// Compare the given set of language options against an existing set of
266 /// language options.
267 ///
268 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
269 /// \param AllowCompatibleDifferences If true, differences between compatible
270 ///        language options will be permitted.
271 ///
272 /// \returns true if the languagae options mis-match, false otherwise.
273 static bool checkLanguageOptions(const LangOptions &LangOpts,
274                                  const LangOptions &ExistingLangOpts,
275                                  DiagnosticsEngine *Diags,
276                                  bool AllowCompatibleDifferences = true) {
277 #define LANGOPT(Name, Bits, Default, Description)                 \
278   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
279     if (Diags)                                                    \
280       Diags->Report(diag::err_pch_langopt_mismatch)               \
281         << Description << LangOpts.Name << ExistingLangOpts.Name; \
282     return true;                                                  \
283   }
284 
285 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
286   if (ExistingLangOpts.Name != LangOpts.Name) {           \
287     if (Diags)                                            \
288       Diags->Report(diag::err_pch_langopt_value_mismatch) \
289         << Description;                                   \
290     return true;                                          \
291   }
292 
293 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
294   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
295     if (Diags)                                                 \
296       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
297         << Description;                                        \
298     return true;                                               \
299   }
300 
301 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
302   if (!AllowCompatibleDifferences)                            \
303     LANGOPT(Name, Bits, Default, Description)
304 
305 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
306   if (!AllowCompatibleDifferences)                                 \
307     ENUM_LANGOPT(Name, Bits, Default, Description)
308 
309 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
310   if (!AllowCompatibleDifferences)                                 \
311     VALUE_LANGOPT(Name, Bits, Default, Description)
312 
313 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
314 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
315 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
316 #include "clang/Basic/LangOptions.def"
317 
318   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
319     if (Diags)
320       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
321     return true;
322   }
323 
324   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
325     if (Diags)
326       Diags->Report(diag::err_pch_langopt_value_mismatch)
327       << "target Objective-C runtime";
328     return true;
329   }
330 
331   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
332       LangOpts.CommentOpts.BlockCommandNames) {
333     if (Diags)
334       Diags->Report(diag::err_pch_langopt_value_mismatch)
335         << "block command names";
336     return true;
337   }
338 
339   // Sanitizer feature mismatches are treated as compatible differences. If
340   // compatible differences aren't allowed, we still only want to check for
341   // mismatches of non-modular sanitizers (the only ones which can affect AST
342   // generation).
343   if (!AllowCompatibleDifferences) {
344     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
345     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
346     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
347     ExistingSanitizers.clear(ModularSanitizers);
348     ImportedSanitizers.clear(ModularSanitizers);
349     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
350       const std::string Flag = "-fsanitize=";
351       if (Diags) {
352 #define SANITIZER(NAME, ID)                                                    \
353   {                                                                            \
354     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
355     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
356     if (InExistingModule != InImportedModule)                                  \
357       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
358           << InExistingModule << (Flag + NAME);                                \
359   }
360 #include "clang/Basic/Sanitizers.def"
361       }
362       return true;
363     }
364   }
365 
366   return false;
367 }
368 
369 /// Compare the given set of target options against an existing set of
370 /// target options.
371 ///
372 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
373 ///
374 /// \returns true if the target options mis-match, false otherwise.
375 static bool checkTargetOptions(const TargetOptions &TargetOpts,
376                                const TargetOptions &ExistingTargetOpts,
377                                DiagnosticsEngine *Diags,
378                                bool AllowCompatibleDifferences = true) {
379 #define CHECK_TARGET_OPT(Field, Name)                             \
380   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
381     if (Diags)                                                    \
382       Diags->Report(diag::err_pch_targetopt_mismatch)             \
383         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
384     return true;                                                  \
385   }
386 
387   // The triple and ABI must match exactly.
388   CHECK_TARGET_OPT(Triple, "target");
389   CHECK_TARGET_OPT(ABI, "target ABI");
390 
391   // We can tolerate different CPUs in many cases, notably when one CPU
392   // supports a strict superset of another. When allowing compatible
393   // differences skip this check.
394   if (!AllowCompatibleDifferences) {
395     CHECK_TARGET_OPT(CPU, "target CPU");
396     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
397   }
398 
399 #undef CHECK_TARGET_OPT
400 
401   // Compare feature sets.
402   SmallVector<StringRef, 4> ExistingFeatures(
403                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
404                                              ExistingTargetOpts.FeaturesAsWritten.end());
405   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
406                                          TargetOpts.FeaturesAsWritten.end());
407   llvm::sort(ExistingFeatures);
408   llvm::sort(ReadFeatures);
409 
410   // We compute the set difference in both directions explicitly so that we can
411   // diagnose the differences differently.
412   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
413   std::set_difference(
414       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
415       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
416   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
417                       ExistingFeatures.begin(), ExistingFeatures.end(),
418                       std::back_inserter(UnmatchedReadFeatures));
419 
420   // If we are allowing compatible differences and the read feature set is
421   // a strict subset of the existing feature set, there is nothing to diagnose.
422   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
423     return false;
424 
425   if (Diags) {
426     for (StringRef Feature : UnmatchedReadFeatures)
427       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428           << /* is-existing-feature */ false << Feature;
429     for (StringRef Feature : UnmatchedExistingFeatures)
430       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
431           << /* is-existing-feature */ true << Feature;
432   }
433 
434   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
435 }
436 
437 bool
438 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
439                                   bool Complain,
440                                   bool AllowCompatibleDifferences) {
441   const LangOptions &ExistingLangOpts = PP.getLangOpts();
442   return checkLanguageOptions(LangOpts, ExistingLangOpts,
443                               Complain ? &Reader.Diags : nullptr,
444                               AllowCompatibleDifferences);
445 }
446 
447 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
448                                      bool Complain,
449                                      bool AllowCompatibleDifferences) {
450   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
451   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
452                             Complain ? &Reader.Diags : nullptr,
453                             AllowCompatibleDifferences);
454 }
455 
456 namespace {
457 
458 using MacroDefinitionsMap =
459     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
460 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
461 
462 } // namespace
463 
464 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
465                                          DiagnosticsEngine &Diags,
466                                          bool Complain) {
467   using Level = DiagnosticsEngine::Level;
468 
469   // Check current mappings for new -Werror mappings, and the stored mappings
470   // for cases that were explicitly mapped to *not* be errors that are now
471   // errors because of options like -Werror.
472   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
473 
474   for (DiagnosticsEngine *MappingSource : MappingSources) {
475     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
476       diag::kind DiagID = DiagIDMappingPair.first;
477       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
478       if (CurLevel < DiagnosticsEngine::Error)
479         continue; // not significant
480       Level StoredLevel =
481           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
482       if (StoredLevel < DiagnosticsEngine::Error) {
483         if (Complain)
484           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
485               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
486         return true;
487       }
488     }
489   }
490 
491   return false;
492 }
493 
494 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
495   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
496   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
497     return true;
498   return Ext >= diag::Severity::Error;
499 }
500 
501 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
502                                     DiagnosticsEngine &Diags,
503                                     bool IsSystem, bool Complain) {
504   // Top-level options
505   if (IsSystem) {
506     if (Diags.getSuppressSystemWarnings())
507       return false;
508     // If -Wsystem-headers was not enabled before, be conservative
509     if (StoredDiags.getSuppressSystemWarnings()) {
510       if (Complain)
511         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
512       return true;
513     }
514   }
515 
516   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
517     if (Complain)
518       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
519     return true;
520   }
521 
522   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
523       !StoredDiags.getEnableAllWarnings()) {
524     if (Complain)
525       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
526     return true;
527   }
528 
529   if (isExtHandlingFromDiagsError(Diags) &&
530       !isExtHandlingFromDiagsError(StoredDiags)) {
531     if (Complain)
532       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
533     return true;
534   }
535 
536   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
537 }
538 
539 /// Return the top import module if it is implicit, nullptr otherwise.
540 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
541                                           Preprocessor &PP) {
542   // If the original import came from a file explicitly generated by the user,
543   // don't check the diagnostic mappings.
544   // FIXME: currently this is approximated by checking whether this is not a
545   // module import of an implicitly-loaded module file.
546   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
547   // the transitive closure of its imports, since unrelated modules cannot be
548   // imported until after this module finishes validation.
549   ModuleFile *TopImport = &*ModuleMgr.rbegin();
550   while (!TopImport->ImportedBy.empty())
551     TopImport = TopImport->ImportedBy[0];
552   if (TopImport->Kind != MK_ImplicitModule)
553     return nullptr;
554 
555   StringRef ModuleName = TopImport->ModuleName;
556   assert(!ModuleName.empty() && "diagnostic options read before module name");
557 
558   Module *M =
559       PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
560   assert(M && "missing module");
561   return M;
562 }
563 
564 bool PCHValidator::ReadDiagnosticOptions(
565     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
566   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
567   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
568   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
569       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
570   // This should never fail, because we would have processed these options
571   // before writing them to an ASTFile.
572   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
573 
574   ModuleManager &ModuleMgr = Reader.getModuleManager();
575   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
576 
577   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
578   if (!TopM)
579     return false;
580 
581   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
582   // contains the union of their flags.
583   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
584                                  Complain);
585 }
586 
587 /// Collect the macro definitions provided by the given preprocessor
588 /// options.
589 static void
590 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
591                         MacroDefinitionsMap &Macros,
592                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
593   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
594     StringRef Macro = PPOpts.Macros[I].first;
595     bool IsUndef = PPOpts.Macros[I].second;
596 
597     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
598     StringRef MacroName = MacroPair.first;
599     StringRef MacroBody = MacroPair.second;
600 
601     // For an #undef'd macro, we only care about the name.
602     if (IsUndef) {
603       if (MacroNames && !Macros.count(MacroName))
604         MacroNames->push_back(MacroName);
605 
606       Macros[MacroName] = std::make_pair("", true);
607       continue;
608     }
609 
610     // For a #define'd macro, figure out the actual definition.
611     if (MacroName.size() == Macro.size())
612       MacroBody = "1";
613     else {
614       // Note: GCC drops anything following an end-of-line character.
615       StringRef::size_type End = MacroBody.find_first_of("\n\r");
616       MacroBody = MacroBody.substr(0, End);
617     }
618 
619     if (MacroNames && !Macros.count(MacroName))
620       MacroNames->push_back(MacroName);
621     Macros[MacroName] = std::make_pair(MacroBody, false);
622   }
623 }
624 
625 /// Check the preprocessor options deserialized from the control block
626 /// against the preprocessor options in an existing preprocessor.
627 ///
628 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
629 /// \param Validate If true, validate preprocessor options. If false, allow
630 ///        macros defined by \p ExistingPPOpts to override those defined by
631 ///        \p PPOpts in SuggestedPredefines.
632 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
633                                      const PreprocessorOptions &ExistingPPOpts,
634                                      DiagnosticsEngine *Diags,
635                                      FileManager &FileMgr,
636                                      std::string &SuggestedPredefines,
637                                      const LangOptions &LangOpts,
638                                      bool Validate = true) {
639   // Check macro definitions.
640   MacroDefinitionsMap ASTFileMacros;
641   collectMacroDefinitions(PPOpts, ASTFileMacros);
642   MacroDefinitionsMap ExistingMacros;
643   SmallVector<StringRef, 4> ExistingMacroNames;
644   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
645 
646   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
647     // Dig out the macro definition in the existing preprocessor options.
648     StringRef MacroName = ExistingMacroNames[I];
649     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
650 
651     // Check whether we know anything about this macro name or not.
652     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
653         ASTFileMacros.find(MacroName);
654     if (!Validate || Known == ASTFileMacros.end()) {
655       // FIXME: Check whether this identifier was referenced anywhere in the
656       // AST file. If so, we should reject the AST file. Unfortunately, this
657       // information isn't in the control block. What shall we do about it?
658 
659       if (Existing.second) {
660         SuggestedPredefines += "#undef ";
661         SuggestedPredefines += MacroName.str();
662         SuggestedPredefines += '\n';
663       } else {
664         SuggestedPredefines += "#define ";
665         SuggestedPredefines += MacroName.str();
666         SuggestedPredefines += ' ';
667         SuggestedPredefines += Existing.first.str();
668         SuggestedPredefines += '\n';
669       }
670       continue;
671     }
672 
673     // If the macro was defined in one but undef'd in the other, we have a
674     // conflict.
675     if (Existing.second != Known->second.second) {
676       if (Diags) {
677         Diags->Report(diag::err_pch_macro_def_undef)
678           << MacroName << Known->second.second;
679       }
680       return true;
681     }
682 
683     // If the macro was #undef'd in both, or if the macro bodies are identical,
684     // it's fine.
685     if (Existing.second || Existing.first == Known->second.first)
686       continue;
687 
688     // The macro bodies differ; complain.
689     if (Diags) {
690       Diags->Report(diag::err_pch_macro_def_conflict)
691         << MacroName << Known->second.first << Existing.first;
692     }
693     return true;
694   }
695 
696   // Check whether we're using predefines.
697   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
698     if (Diags) {
699       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
700     }
701     return true;
702   }
703 
704   // Detailed record is important since it is used for the module cache hash.
705   if (LangOpts.Modules &&
706       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
707     if (Diags) {
708       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
709     }
710     return true;
711   }
712 
713   // Compute the #include and #include_macros lines we need.
714   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
715     StringRef File = ExistingPPOpts.Includes[I];
716 
717     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
718         !ExistingPPOpts.PCHThroughHeader.empty()) {
719       // In case the through header is an include, we must add all the includes
720       // to the predefines so the start point can be determined.
721       SuggestedPredefines += "#include \"";
722       SuggestedPredefines += File;
723       SuggestedPredefines += "\"\n";
724       continue;
725     }
726 
727     if (File == ExistingPPOpts.ImplicitPCHInclude)
728       continue;
729 
730     if (llvm::is_contained(PPOpts.Includes, File))
731       continue;
732 
733     SuggestedPredefines += "#include \"";
734     SuggestedPredefines += File;
735     SuggestedPredefines += "\"\n";
736   }
737 
738   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
739     StringRef File = ExistingPPOpts.MacroIncludes[I];
740     if (llvm::is_contained(PPOpts.MacroIncludes, File))
741       continue;
742 
743     SuggestedPredefines += "#__include_macros \"";
744     SuggestedPredefines += File;
745     SuggestedPredefines += "\"\n##\n";
746   }
747 
748   return false;
749 }
750 
751 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
752                                            bool Complain,
753                                            std::string &SuggestedPredefines) {
754   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
755 
756   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
757                                   Complain? &Reader.Diags : nullptr,
758                                   PP.getFileManager(),
759                                   SuggestedPredefines,
760                                   PP.getLangOpts());
761 }
762 
763 bool SimpleASTReaderListener::ReadPreprocessorOptions(
764                                   const PreprocessorOptions &PPOpts,
765                                   bool Complain,
766                                   std::string &SuggestedPredefines) {
767   return checkPreprocessorOptions(PPOpts,
768                                   PP.getPreprocessorOpts(),
769                                   nullptr,
770                                   PP.getFileManager(),
771                                   SuggestedPredefines,
772                                   PP.getLangOpts(),
773                                   false);
774 }
775 
776 /// Check the header search options deserialized from the control block
777 /// against the header search options in an existing preprocessor.
778 ///
779 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
780 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
781                                      StringRef SpecificModuleCachePath,
782                                      StringRef ExistingModuleCachePath,
783                                      DiagnosticsEngine *Diags,
784                                      const LangOptions &LangOpts,
785                                      const PreprocessorOptions &PPOpts) {
786   if (LangOpts.Modules) {
787     if (SpecificModuleCachePath != ExistingModuleCachePath &&
788         !PPOpts.AllowPCHWithDifferentModulesCachePath) {
789       if (Diags)
790         Diags->Report(diag::err_pch_modulecache_mismatch)
791           << SpecificModuleCachePath << ExistingModuleCachePath;
792       return true;
793     }
794   }
795 
796   return false;
797 }
798 
799 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
800                                            StringRef SpecificModuleCachePath,
801                                            bool Complain) {
802   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
803                                   PP.getHeaderSearchInfo().getModuleCachePath(),
804                                   Complain ? &Reader.Diags : nullptr,
805                                   PP.getLangOpts(), PP.getPreprocessorOpts());
806 }
807 
808 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
809   PP.setCounterValue(Value);
810 }
811 
812 //===----------------------------------------------------------------------===//
813 // AST reader implementation
814 //===----------------------------------------------------------------------===//
815 
816 static uint64_t readULEB(const unsigned char *&P) {
817   unsigned Length = 0;
818   const char *Error = nullptr;
819 
820   uint64_t Val = llvm::decodeULEB128(P, &Length, nullptr, &Error);
821   if (Error)
822     llvm::report_fatal_error(Error);
823   P += Length;
824   return Val;
825 }
826 
827 /// Read ULEB-encoded key length and data length.
828 static std::pair<unsigned, unsigned>
829 readULEBKeyDataLength(const unsigned char *&P) {
830   unsigned KeyLen = readULEB(P);
831   if ((unsigned)KeyLen != KeyLen)
832     llvm::report_fatal_error("key too large");
833 
834   unsigned DataLen = readULEB(P);
835   if ((unsigned)DataLen != DataLen)
836     llvm::report_fatal_error("data too large");
837 
838   return std::make_pair(KeyLen, DataLen);
839 }
840 
841 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
842                                            bool TakeOwnership) {
843   DeserializationListener = Listener;
844   OwnsDeserializationListener = TakeOwnership;
845 }
846 
847 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
848   return serialization::ComputeHash(Sel);
849 }
850 
851 std::pair<unsigned, unsigned>
852 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
853   return readULEBKeyDataLength(d);
854 }
855 
856 ASTSelectorLookupTrait::internal_key_type
857 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
858   using namespace llvm::support;
859 
860   SelectorTable &SelTable = Reader.getContext().Selectors;
861   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
862   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
863       F, endian::readNext<uint32_t, little, unaligned>(d));
864   if (N == 0)
865     return SelTable.getNullarySelector(FirstII);
866   else if (N == 1)
867     return SelTable.getUnarySelector(FirstII);
868 
869   SmallVector<IdentifierInfo *, 16> Args;
870   Args.push_back(FirstII);
871   for (unsigned I = 1; I != N; ++I)
872     Args.push_back(Reader.getLocalIdentifier(
873         F, endian::readNext<uint32_t, little, unaligned>(d)));
874 
875   return SelTable.getSelector(N, Args.data());
876 }
877 
878 ASTSelectorLookupTrait::data_type
879 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
880                                  unsigned DataLen) {
881   using namespace llvm::support;
882 
883   data_type Result;
884 
885   Result.ID = Reader.getGlobalSelectorID(
886       F, endian::readNext<uint32_t, little, unaligned>(d));
887   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
888   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
889   Result.InstanceBits = FullInstanceBits & 0x3;
890   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
891   Result.FactoryBits = FullFactoryBits & 0x3;
892   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
893   unsigned NumInstanceMethods = FullInstanceBits >> 3;
894   unsigned NumFactoryMethods = FullFactoryBits >> 3;
895 
896   // Load instance methods
897   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
898     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
899             F, endian::readNext<uint32_t, little, unaligned>(d)))
900       Result.Instance.push_back(Method);
901   }
902 
903   // Load factory methods
904   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
905     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
906             F, endian::readNext<uint32_t, little, unaligned>(d)))
907       Result.Factory.push_back(Method);
908   }
909 
910   return Result;
911 }
912 
913 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
914   return llvm::djbHash(a);
915 }
916 
917 std::pair<unsigned, unsigned>
918 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
919   return readULEBKeyDataLength(d);
920 }
921 
922 ASTIdentifierLookupTraitBase::internal_key_type
923 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
924   assert(n >= 2 && d[n-1] == '\0');
925   return StringRef((const char*) d, n-1);
926 }
927 
928 /// Whether the given identifier is "interesting".
929 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
930                                     bool IsModule) {
931   return II.hadMacroDefinition() || II.isPoisoned() ||
932          (!IsModule && II.getObjCOrBuiltinID()) ||
933          II.hasRevertedTokenIDToIdentifier() ||
934          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
935           II.getFETokenInfo());
936 }
937 
938 static bool readBit(unsigned &Bits) {
939   bool Value = Bits & 0x1;
940   Bits >>= 1;
941   return Value;
942 }
943 
944 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
945   using namespace llvm::support;
946 
947   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
948   return Reader.getGlobalIdentifierID(F, RawID >> 1);
949 }
950 
951 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
952   if (!II.isFromAST()) {
953     II.setIsFromAST();
954     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
955     if (isInterestingIdentifier(Reader, II, IsModule))
956       II.setChangedSinceDeserialization();
957   }
958 }
959 
960 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
961                                                    const unsigned char* d,
962                                                    unsigned DataLen) {
963   using namespace llvm::support;
964 
965   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
966   bool IsInteresting = RawID & 0x01;
967 
968   // Wipe out the "is interesting" bit.
969   RawID = RawID >> 1;
970 
971   // Build the IdentifierInfo and link the identifier ID with it.
972   IdentifierInfo *II = KnownII;
973   if (!II) {
974     II = &Reader.getIdentifierTable().getOwn(k);
975     KnownII = II;
976   }
977   markIdentifierFromAST(Reader, *II);
978   Reader.markIdentifierUpToDate(II);
979 
980   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
981   if (!IsInteresting) {
982     // For uninteresting identifiers, there's nothing else to do. Just notify
983     // the reader that we've finished loading this identifier.
984     Reader.SetIdentifierInfo(ID, II);
985     return II;
986   }
987 
988   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
989   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
990   bool CPlusPlusOperatorKeyword = readBit(Bits);
991   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
992   bool Poisoned = readBit(Bits);
993   bool ExtensionToken = readBit(Bits);
994   bool HadMacroDefinition = readBit(Bits);
995 
996   assert(Bits == 0 && "Extra bits in the identifier?");
997   DataLen -= 8;
998 
999   // Set or check the various bits in the IdentifierInfo structure.
1000   // Token IDs are read-only.
1001   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
1002     II->revertTokenIDToIdentifier();
1003   if (!F.isModule())
1004     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
1005   assert(II->isExtensionToken() == ExtensionToken &&
1006          "Incorrect extension token flag");
1007   (void)ExtensionToken;
1008   if (Poisoned)
1009     II->setIsPoisoned(true);
1010   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1011          "Incorrect C++ operator keyword flag");
1012   (void)CPlusPlusOperatorKeyword;
1013 
1014   // If this identifier is a macro, deserialize the macro
1015   // definition.
1016   if (HadMacroDefinition) {
1017     uint32_t MacroDirectivesOffset =
1018         endian::readNext<uint32_t, little, unaligned>(d);
1019     DataLen -= 4;
1020 
1021     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1022   }
1023 
1024   Reader.SetIdentifierInfo(ID, II);
1025 
1026   // Read all of the declarations visible at global scope with this
1027   // name.
1028   if (DataLen > 0) {
1029     SmallVector<uint32_t, 4> DeclIDs;
1030     for (; DataLen > 0; DataLen -= 4)
1031       DeclIDs.push_back(Reader.getGlobalDeclID(
1032           F, endian::readNext<uint32_t, little, unaligned>(d)));
1033     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1034   }
1035 
1036   return II;
1037 }
1038 
1039 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1040     : Kind(Name.getNameKind()) {
1041   switch (Kind) {
1042   case DeclarationName::Identifier:
1043     Data = (uint64_t)Name.getAsIdentifierInfo();
1044     break;
1045   case DeclarationName::ObjCZeroArgSelector:
1046   case DeclarationName::ObjCOneArgSelector:
1047   case DeclarationName::ObjCMultiArgSelector:
1048     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1049     break;
1050   case DeclarationName::CXXOperatorName:
1051     Data = Name.getCXXOverloadedOperator();
1052     break;
1053   case DeclarationName::CXXLiteralOperatorName:
1054     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1055     break;
1056   case DeclarationName::CXXDeductionGuideName:
1057     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1058                ->getDeclName().getAsIdentifierInfo();
1059     break;
1060   case DeclarationName::CXXConstructorName:
1061   case DeclarationName::CXXDestructorName:
1062   case DeclarationName::CXXConversionFunctionName:
1063   case DeclarationName::CXXUsingDirective:
1064     Data = 0;
1065     break;
1066   }
1067 }
1068 
1069 unsigned DeclarationNameKey::getHash() const {
1070   llvm::FoldingSetNodeID ID;
1071   ID.AddInteger(Kind);
1072 
1073   switch (Kind) {
1074   case DeclarationName::Identifier:
1075   case DeclarationName::CXXLiteralOperatorName:
1076   case DeclarationName::CXXDeductionGuideName:
1077     ID.AddString(((IdentifierInfo*)Data)->getName());
1078     break;
1079   case DeclarationName::ObjCZeroArgSelector:
1080   case DeclarationName::ObjCOneArgSelector:
1081   case DeclarationName::ObjCMultiArgSelector:
1082     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1083     break;
1084   case DeclarationName::CXXOperatorName:
1085     ID.AddInteger((OverloadedOperatorKind)Data);
1086     break;
1087   case DeclarationName::CXXConstructorName:
1088   case DeclarationName::CXXDestructorName:
1089   case DeclarationName::CXXConversionFunctionName:
1090   case DeclarationName::CXXUsingDirective:
1091     break;
1092   }
1093 
1094   return ID.ComputeHash();
1095 }
1096 
1097 ModuleFile *
1098 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1099   using namespace llvm::support;
1100 
1101   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1102   return Reader.getLocalModuleFile(F, ModuleFileID);
1103 }
1104 
1105 std::pair<unsigned, unsigned>
1106 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1107   return readULEBKeyDataLength(d);
1108 }
1109 
1110 ASTDeclContextNameLookupTrait::internal_key_type
1111 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1112   using namespace llvm::support;
1113 
1114   auto Kind = (DeclarationName::NameKind)*d++;
1115   uint64_t Data;
1116   switch (Kind) {
1117   case DeclarationName::Identifier:
1118   case DeclarationName::CXXLiteralOperatorName:
1119   case DeclarationName::CXXDeductionGuideName:
1120     Data = (uint64_t)Reader.getLocalIdentifier(
1121         F, endian::readNext<uint32_t, little, unaligned>(d));
1122     break;
1123   case DeclarationName::ObjCZeroArgSelector:
1124   case DeclarationName::ObjCOneArgSelector:
1125   case DeclarationName::ObjCMultiArgSelector:
1126     Data =
1127         (uint64_t)Reader.getLocalSelector(
1128                              F, endian::readNext<uint32_t, little, unaligned>(
1129                                     d)).getAsOpaquePtr();
1130     break;
1131   case DeclarationName::CXXOperatorName:
1132     Data = *d++; // OverloadedOperatorKind
1133     break;
1134   case DeclarationName::CXXConstructorName:
1135   case DeclarationName::CXXDestructorName:
1136   case DeclarationName::CXXConversionFunctionName:
1137   case DeclarationName::CXXUsingDirective:
1138     Data = 0;
1139     break;
1140   }
1141 
1142   return DeclarationNameKey(Kind, Data);
1143 }
1144 
1145 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1146                                                  const unsigned char *d,
1147                                                  unsigned DataLen,
1148                                                  data_type_builder &Val) {
1149   using namespace llvm::support;
1150 
1151   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1152     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1153     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1154   }
1155 }
1156 
1157 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1158                                               BitstreamCursor &Cursor,
1159                                               uint64_t Offset,
1160                                               DeclContext *DC) {
1161   assert(Offset != 0);
1162 
1163   SavedStreamPosition SavedPosition(Cursor);
1164   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1165     Error(std::move(Err));
1166     return true;
1167   }
1168 
1169   RecordData Record;
1170   StringRef Blob;
1171   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1172   if (!MaybeCode) {
1173     Error(MaybeCode.takeError());
1174     return true;
1175   }
1176   unsigned Code = MaybeCode.get();
1177 
1178   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1179   if (!MaybeRecCode) {
1180     Error(MaybeRecCode.takeError());
1181     return true;
1182   }
1183   unsigned RecCode = MaybeRecCode.get();
1184   if (RecCode != DECL_CONTEXT_LEXICAL) {
1185     Error("Expected lexical block");
1186     return true;
1187   }
1188 
1189   assert(!isa<TranslationUnitDecl>(DC) &&
1190          "expected a TU_UPDATE_LEXICAL record for TU");
1191   // If we are handling a C++ class template instantiation, we can see multiple
1192   // lexical updates for the same record. It's important that we select only one
1193   // of them, so that field numbering works properly. Just pick the first one we
1194   // see.
1195   auto &Lex = LexicalDecls[DC];
1196   if (!Lex.first) {
1197     Lex = std::make_pair(
1198         &M, llvm::makeArrayRef(
1199                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1200                     Blob.data()),
1201                 Blob.size() / 4));
1202   }
1203   DC->setHasExternalLexicalStorage(true);
1204   return false;
1205 }
1206 
1207 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1208                                               BitstreamCursor &Cursor,
1209                                               uint64_t Offset,
1210                                               DeclID ID) {
1211   assert(Offset != 0);
1212 
1213   SavedStreamPosition SavedPosition(Cursor);
1214   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1215     Error(std::move(Err));
1216     return true;
1217   }
1218 
1219   RecordData Record;
1220   StringRef Blob;
1221   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1222   if (!MaybeCode) {
1223     Error(MaybeCode.takeError());
1224     return true;
1225   }
1226   unsigned Code = MaybeCode.get();
1227 
1228   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1229   if (!MaybeRecCode) {
1230     Error(MaybeRecCode.takeError());
1231     return true;
1232   }
1233   unsigned RecCode = MaybeRecCode.get();
1234   if (RecCode != DECL_CONTEXT_VISIBLE) {
1235     Error("Expected visible lookup table block");
1236     return true;
1237   }
1238 
1239   // We can't safely determine the primary context yet, so delay attaching the
1240   // lookup table until we're done with recursive deserialization.
1241   auto *Data = (const unsigned char*)Blob.data();
1242   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1243   return false;
1244 }
1245 
1246 void ASTReader::Error(StringRef Msg) const {
1247   Error(diag::err_fe_pch_malformed, Msg);
1248   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1249       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1250     Diag(diag::note_module_cache_path)
1251       << PP.getHeaderSearchInfo().getModuleCachePath();
1252   }
1253 }
1254 
1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1256                       StringRef Arg3) const {
1257   if (Diags.isDiagnosticInFlight())
1258     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1259   else
1260     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1261 }
1262 
1263 void ASTReader::Error(llvm::Error &&Err) const {
1264   llvm::Error RemainingErr =
1265       handleErrors(std::move(Err), [this](const DiagnosticError &E) {
1266         auto Diag = E.getDiagnostic().second;
1267 
1268         // Ideally we'd just emit it, but have to handle a possible in-flight
1269         // diagnostic. Note that the location is currently ignored as well.
1270         auto NumArgs = Diag.getStorage()->NumDiagArgs;
1271         assert(NumArgs <= 3 && "Can only have up to 3 arguments");
1272         StringRef Arg1, Arg2, Arg3;
1273         switch (NumArgs) {
1274         case 3:
1275           Arg3 = Diag.getStringArg(2);
1276           LLVM_FALLTHROUGH;
1277         case 2:
1278           Arg2 = Diag.getStringArg(1);
1279           LLVM_FALLTHROUGH;
1280         case 1:
1281           Arg1 = Diag.getStringArg(0);
1282         }
1283         Error(Diag.getDiagID(), Arg1, Arg2, Arg3);
1284       });
1285   if (RemainingErr)
1286     Error(toString(std::move(RemainingErr)));
1287 }
1288 
1289 //===----------------------------------------------------------------------===//
1290 // Source Manager Deserialization
1291 //===----------------------------------------------------------------------===//
1292 
1293 /// Read the line table in the source manager block.
1294 void ASTReader::ParseLineTable(ModuleFile &F, const RecordData &Record) {
1295   unsigned Idx = 0;
1296   LineTableInfo &LineTable = SourceMgr.getLineTable();
1297 
1298   // Parse the file names
1299   std::map<int, int> FileIDs;
1300   FileIDs[-1] = -1; // For unspecified filenames.
1301   for (unsigned I = 0; Record[Idx]; ++I) {
1302     // Extract the file name
1303     auto Filename = ReadPath(F, Record, Idx);
1304     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1305   }
1306   ++Idx;
1307 
1308   // Parse the line entries
1309   std::vector<LineEntry> Entries;
1310   while (Idx < Record.size()) {
1311     int FID = Record[Idx++];
1312     assert(FID >= 0 && "Serialized line entries for non-local file.");
1313     // Remap FileID from 1-based old view.
1314     FID += F.SLocEntryBaseID - 1;
1315 
1316     // Extract the line entries
1317     unsigned NumEntries = Record[Idx++];
1318     assert(NumEntries && "no line entries for file ID");
1319     Entries.clear();
1320     Entries.reserve(NumEntries);
1321     for (unsigned I = 0; I != NumEntries; ++I) {
1322       unsigned FileOffset = Record[Idx++];
1323       unsigned LineNo = Record[Idx++];
1324       int FilenameID = FileIDs[Record[Idx++]];
1325       SrcMgr::CharacteristicKind FileKind
1326         = (SrcMgr::CharacteristicKind)Record[Idx++];
1327       unsigned IncludeOffset = Record[Idx++];
1328       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1329                                        FileKind, IncludeOffset));
1330     }
1331     LineTable.AddEntry(FileID::get(FID), Entries);
1332   }
1333 }
1334 
1335 /// Read a source manager block
1336 llvm::Error ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1337   using namespace SrcMgr;
1338 
1339   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1340 
1341   // Set the source-location entry cursor to the current position in
1342   // the stream. This cursor will be used to read the contents of the
1343   // source manager block initially, and then lazily read
1344   // source-location entries as needed.
1345   SLocEntryCursor = F.Stream;
1346 
1347   // The stream itself is going to skip over the source manager block.
1348   if (llvm::Error Err = F.Stream.SkipBlock())
1349     return Err;
1350 
1351   // Enter the source manager block.
1352   if (llvm::Error Err = SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID))
1353     return Err;
1354   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1355 
1356   RecordData Record;
1357   while (true) {
1358     Expected<llvm::BitstreamEntry> MaybeE =
1359         SLocEntryCursor.advanceSkippingSubblocks();
1360     if (!MaybeE)
1361       return MaybeE.takeError();
1362     llvm::BitstreamEntry E = MaybeE.get();
1363 
1364     switch (E.Kind) {
1365     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1366     case llvm::BitstreamEntry::Error:
1367       return llvm::createStringError(std::errc::illegal_byte_sequence,
1368                                      "malformed block record in AST file");
1369     case llvm::BitstreamEntry::EndBlock:
1370       return llvm::Error::success();
1371     case llvm::BitstreamEntry::Record:
1372       // The interesting case.
1373       break;
1374     }
1375 
1376     // Read a record.
1377     Record.clear();
1378     StringRef Blob;
1379     Expected<unsigned> MaybeRecord =
1380         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1381     if (!MaybeRecord)
1382       return MaybeRecord.takeError();
1383     switch (MaybeRecord.get()) {
1384     default:  // Default behavior: ignore.
1385       break;
1386 
1387     case SM_SLOC_FILE_ENTRY:
1388     case SM_SLOC_BUFFER_ENTRY:
1389     case SM_SLOC_EXPANSION_ENTRY:
1390       // Once we hit one of the source location entries, we're done.
1391       return llvm::Error::success();
1392     }
1393   }
1394 }
1395 
1396 /// If a header file is not found at the path that we expect it to be
1397 /// and the PCH file was moved from its original location, try to resolve the
1398 /// file by assuming that header+PCH were moved together and the header is in
1399 /// the same place relative to the PCH.
1400 static std::string
1401 resolveFileRelativeToOriginalDir(const std::string &Filename,
1402                                  const std::string &OriginalDir,
1403                                  const std::string &CurrDir) {
1404   assert(OriginalDir != CurrDir &&
1405          "No point trying to resolve the file if the PCH dir didn't change");
1406 
1407   using namespace llvm::sys;
1408 
1409   SmallString<128> filePath(Filename);
1410   fs::make_absolute(filePath);
1411   assert(path::is_absolute(OriginalDir));
1412   SmallString<128> currPCHPath(CurrDir);
1413 
1414   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1415                        fileDirE = path::end(path::parent_path(filePath));
1416   path::const_iterator origDirI = path::begin(OriginalDir),
1417                        origDirE = path::end(OriginalDir);
1418   // Skip the common path components from filePath and OriginalDir.
1419   while (fileDirI != fileDirE && origDirI != origDirE &&
1420          *fileDirI == *origDirI) {
1421     ++fileDirI;
1422     ++origDirI;
1423   }
1424   for (; origDirI != origDirE; ++origDirI)
1425     path::append(currPCHPath, "..");
1426   path::append(currPCHPath, fileDirI, fileDirE);
1427   path::append(currPCHPath, path::filename(Filename));
1428   return std::string(currPCHPath.str());
1429 }
1430 
1431 bool ASTReader::ReadSLocEntry(int ID) {
1432   if (ID == 0)
1433     return false;
1434 
1435   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1436     Error("source location entry ID out-of-range for AST file");
1437     return true;
1438   }
1439 
1440   // Local helper to read the (possibly-compressed) buffer data following the
1441   // entry record.
1442   auto ReadBuffer = [this](
1443       BitstreamCursor &SLocEntryCursor,
1444       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1445     RecordData Record;
1446     StringRef Blob;
1447     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1448     if (!MaybeCode) {
1449       Error(MaybeCode.takeError());
1450       return nullptr;
1451     }
1452     unsigned Code = MaybeCode.get();
1453 
1454     Expected<unsigned> MaybeRecCode =
1455         SLocEntryCursor.readRecord(Code, Record, &Blob);
1456     if (!MaybeRecCode) {
1457       Error(MaybeRecCode.takeError());
1458       return nullptr;
1459     }
1460     unsigned RecCode = MaybeRecCode.get();
1461 
1462     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1463       if (!llvm::zlib::isAvailable()) {
1464         Error("zlib is not available");
1465         return nullptr;
1466       }
1467       SmallString<0> Uncompressed;
1468       if (llvm::Error E =
1469               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1470         Error("could not decompress embedded file contents: " +
1471               llvm::toString(std::move(E)));
1472         return nullptr;
1473       }
1474       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1475     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1476       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1477     } else {
1478       Error("AST record has invalid code");
1479       return nullptr;
1480     }
1481   };
1482 
1483   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1484   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1485           F->SLocEntryOffsetsBase +
1486           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1487     Error(std::move(Err));
1488     return true;
1489   }
1490 
1491   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1492   SourceLocation::UIntTy BaseOffset = F->SLocEntryBaseOffset;
1493 
1494   ++NumSLocEntriesRead;
1495   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1496   if (!MaybeEntry) {
1497     Error(MaybeEntry.takeError());
1498     return true;
1499   }
1500   llvm::BitstreamEntry Entry = MaybeEntry.get();
1501 
1502   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1503     Error("incorrectly-formatted source location entry in AST file");
1504     return true;
1505   }
1506 
1507   RecordData Record;
1508   StringRef Blob;
1509   Expected<unsigned> MaybeSLOC =
1510       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1511   if (!MaybeSLOC) {
1512     Error(MaybeSLOC.takeError());
1513     return true;
1514   }
1515   switch (MaybeSLOC.get()) {
1516   default:
1517     Error("incorrectly-formatted source location entry in AST file");
1518     return true;
1519 
1520   case SM_SLOC_FILE_ENTRY: {
1521     // We will detect whether a file changed and return 'Failure' for it, but
1522     // we will also try to fail gracefully by setting up the SLocEntry.
1523     unsigned InputID = Record[4];
1524     InputFile IF = getInputFile(*F, InputID);
1525     Optional<FileEntryRef> File = IF.getFile();
1526     bool OverriddenBuffer = IF.isOverridden();
1527 
1528     // Note that we only check if a File was returned. If it was out-of-date
1529     // we have complained but we will continue creating a FileID to recover
1530     // gracefully.
1531     if (!File)
1532       return true;
1533 
1534     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1535     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1536       // This is the module's main file.
1537       IncludeLoc = getImportLocation(F);
1538     }
1539     SrcMgr::CharacteristicKind
1540       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1541     FileID FID = SourceMgr.createFileID(*File, IncludeLoc, FileCharacter, ID,
1542                                         BaseOffset + Record[0]);
1543     SrcMgr::FileInfo &FileInfo =
1544           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1545     FileInfo.NumCreatedFIDs = Record[5];
1546     if (Record[3])
1547       FileInfo.setHasLineDirectives();
1548 
1549     unsigned NumFileDecls = Record[7];
1550     if (NumFileDecls && ContextObj) {
1551       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1552       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1553       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1554                                                              NumFileDecls));
1555     }
1556 
1557     const SrcMgr::ContentCache &ContentCache =
1558         SourceMgr.getOrCreateContentCache(*File, isSystem(FileCharacter));
1559     if (OverriddenBuffer && !ContentCache.BufferOverridden &&
1560         ContentCache.ContentsEntry == ContentCache.OrigEntry &&
1561         !ContentCache.getBufferIfLoaded()) {
1562       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1563       if (!Buffer)
1564         return true;
1565       SourceMgr.overrideFileContents(*File, std::move(Buffer));
1566     }
1567 
1568     break;
1569   }
1570 
1571   case SM_SLOC_BUFFER_ENTRY: {
1572     const char *Name = Blob.data();
1573     unsigned Offset = Record[0];
1574     SrcMgr::CharacteristicKind
1575       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1576     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1577     if (IncludeLoc.isInvalid() && F->isModule()) {
1578       IncludeLoc = getImportLocation(F);
1579     }
1580 
1581     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1582     if (!Buffer)
1583       return true;
1584     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1585                            BaseOffset + Offset, IncludeLoc);
1586     break;
1587   }
1588 
1589   case SM_SLOC_EXPANSION_ENTRY: {
1590     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1591     SourceMgr.createExpansionLoc(SpellingLoc,
1592                                      ReadSourceLocation(*F, Record[2]),
1593                                      ReadSourceLocation(*F, Record[3]),
1594                                      Record[5],
1595                                      Record[4],
1596                                      ID,
1597                                      BaseOffset + Record[0]);
1598     break;
1599   }
1600   }
1601 
1602   return false;
1603 }
1604 
1605 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1606   if (ID == 0)
1607     return std::make_pair(SourceLocation(), "");
1608 
1609   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1610     Error("source location entry ID out-of-range for AST file");
1611     return std::make_pair(SourceLocation(), "");
1612   }
1613 
1614   // Find which module file this entry lands in.
1615   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1616   if (!M->isModule())
1617     return std::make_pair(SourceLocation(), "");
1618 
1619   // FIXME: Can we map this down to a particular submodule? That would be
1620   // ideal.
1621   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1622 }
1623 
1624 /// Find the location where the module F is imported.
1625 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1626   if (F->ImportLoc.isValid())
1627     return F->ImportLoc;
1628 
1629   // Otherwise we have a PCH. It's considered to be "imported" at the first
1630   // location of its includer.
1631   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1632     // Main file is the importer.
1633     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1634     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1635   }
1636   return F->ImportedBy[0]->FirstLoc;
1637 }
1638 
1639 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1640 /// the abbreviations that are at the top of the block and then leave the cursor
1641 /// pointing into the block.
1642 llvm::Error ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor,
1643                                         unsigned BlockID,
1644                                         uint64_t *StartOfBlockOffset) {
1645   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID))
1646     return Err;
1647 
1648   if (StartOfBlockOffset)
1649     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1650 
1651   while (true) {
1652     uint64_t Offset = Cursor.GetCurrentBitNo();
1653     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1654     if (!MaybeCode)
1655       return MaybeCode.takeError();
1656     unsigned Code = MaybeCode.get();
1657 
1658     // We expect all abbrevs to be at the start of the block.
1659     if (Code != llvm::bitc::DEFINE_ABBREV) {
1660       if (llvm::Error Err = Cursor.JumpToBit(Offset))
1661         return Err;
1662       return llvm::Error::success();
1663     }
1664     if (llvm::Error Err = Cursor.ReadAbbrevRecord())
1665       return Err;
1666   }
1667 }
1668 
1669 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1670                            unsigned &Idx) {
1671   Token Tok;
1672   Tok.startToken();
1673   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1674   Tok.setLength(Record[Idx++]);
1675   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1676     Tok.setIdentifierInfo(II);
1677   Tok.setKind((tok::TokenKind)Record[Idx++]);
1678   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1679   return Tok;
1680 }
1681 
1682 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1683   BitstreamCursor &Stream = F.MacroCursor;
1684 
1685   // Keep track of where we are in the stream, then jump back there
1686   // after reading this macro.
1687   SavedStreamPosition SavedPosition(Stream);
1688 
1689   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1690     // FIXME this drops errors on the floor.
1691     consumeError(std::move(Err));
1692     return nullptr;
1693   }
1694   RecordData Record;
1695   SmallVector<IdentifierInfo*, 16> MacroParams;
1696   MacroInfo *Macro = nullptr;
1697 
1698   while (true) {
1699     // Advance to the next record, but if we get to the end of the block, don't
1700     // pop it (removing all the abbreviations from the cursor) since we want to
1701     // be able to reseek within the block and read entries.
1702     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1703     Expected<llvm::BitstreamEntry> MaybeEntry =
1704         Stream.advanceSkippingSubblocks(Flags);
1705     if (!MaybeEntry) {
1706       Error(MaybeEntry.takeError());
1707       return Macro;
1708     }
1709     llvm::BitstreamEntry Entry = MaybeEntry.get();
1710 
1711     switch (Entry.Kind) {
1712     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1713     case llvm::BitstreamEntry::Error:
1714       Error("malformed block record in AST file");
1715       return Macro;
1716     case llvm::BitstreamEntry::EndBlock:
1717       return Macro;
1718     case llvm::BitstreamEntry::Record:
1719       // The interesting case.
1720       break;
1721     }
1722 
1723     // Read a record.
1724     Record.clear();
1725     PreprocessorRecordTypes RecType;
1726     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1727       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1728     else {
1729       Error(MaybeRecType.takeError());
1730       return Macro;
1731     }
1732     switch (RecType) {
1733     case PP_MODULE_MACRO:
1734     case PP_MACRO_DIRECTIVE_HISTORY:
1735       return Macro;
1736 
1737     case PP_MACRO_OBJECT_LIKE:
1738     case PP_MACRO_FUNCTION_LIKE: {
1739       // If we already have a macro, that means that we've hit the end
1740       // of the definition of the macro we were looking for. We're
1741       // done.
1742       if (Macro)
1743         return Macro;
1744 
1745       unsigned NextIndex = 1; // Skip identifier ID.
1746       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1747       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1748       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1749       MI->setIsUsed(Record[NextIndex++]);
1750       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1751 
1752       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1753         // Decode function-like macro info.
1754         bool isC99VarArgs = Record[NextIndex++];
1755         bool isGNUVarArgs = Record[NextIndex++];
1756         bool hasCommaPasting = Record[NextIndex++];
1757         MacroParams.clear();
1758         unsigned NumArgs = Record[NextIndex++];
1759         for (unsigned i = 0; i != NumArgs; ++i)
1760           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1761 
1762         // Install function-like macro info.
1763         MI->setIsFunctionLike();
1764         if (isC99VarArgs) MI->setIsC99Varargs();
1765         if (isGNUVarArgs) MI->setIsGNUVarargs();
1766         if (hasCommaPasting) MI->setHasCommaPasting();
1767         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1768       }
1769 
1770       // Remember that we saw this macro last so that we add the tokens that
1771       // form its body to it.
1772       Macro = MI;
1773 
1774       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1775           Record[NextIndex]) {
1776         // We have a macro definition. Register the association
1777         PreprocessedEntityID
1778             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1779         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1780         PreprocessingRecord::PPEntityID PPID =
1781             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1782         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1783             PPRec.getPreprocessedEntity(PPID));
1784         if (PPDef)
1785           PPRec.RegisterMacroDefinition(Macro, PPDef);
1786       }
1787 
1788       ++NumMacrosRead;
1789       break;
1790     }
1791 
1792     case PP_TOKEN: {
1793       // If we see a TOKEN before a PP_MACRO_*, then the file is
1794       // erroneous, just pretend we didn't see this.
1795       if (!Macro) break;
1796 
1797       unsigned Idx = 0;
1798       Token Tok = ReadToken(F, Record, Idx);
1799       Macro->AddTokenToBody(Tok);
1800       break;
1801     }
1802     }
1803   }
1804 }
1805 
1806 PreprocessedEntityID
1807 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1808                                          unsigned LocalID) const {
1809   if (!M.ModuleOffsetMap.empty())
1810     ReadModuleOffsetMap(M);
1811 
1812   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1813     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1814   assert(I != M.PreprocessedEntityRemap.end()
1815          && "Invalid index into preprocessed entity index remap");
1816 
1817   return LocalID + I->second;
1818 }
1819 
1820 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1821   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1822 }
1823 
1824 HeaderFileInfoTrait::internal_key_type
1825 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1826   internal_key_type ikey = {FE->getSize(),
1827                             M.HasTimestamps ? FE->getModificationTime() : 0,
1828                             FE->getName(), /*Imported*/ false};
1829   return ikey;
1830 }
1831 
1832 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1833   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1834     return false;
1835 
1836   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1837     return true;
1838 
1839   // Determine whether the actual files are equivalent.
1840   FileManager &FileMgr = Reader.getFileManager();
1841   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1842     if (!Key.Imported) {
1843       if (auto File = FileMgr.getFile(Key.Filename))
1844         return *File;
1845       return nullptr;
1846     }
1847 
1848     std::string Resolved = std::string(Key.Filename);
1849     Reader.ResolveImportedPath(M, Resolved);
1850     if (auto File = FileMgr.getFile(Resolved))
1851       return *File;
1852     return nullptr;
1853   };
1854 
1855   const FileEntry *FEA = GetFile(a);
1856   const FileEntry *FEB = GetFile(b);
1857   return FEA && FEA == FEB;
1858 }
1859 
1860 std::pair<unsigned, unsigned>
1861 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1862   return readULEBKeyDataLength(d);
1863 }
1864 
1865 HeaderFileInfoTrait::internal_key_type
1866 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1867   using namespace llvm::support;
1868 
1869   internal_key_type ikey;
1870   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1871   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1872   ikey.Filename = (const char *)d;
1873   ikey.Imported = true;
1874   return ikey;
1875 }
1876 
1877 HeaderFileInfoTrait::data_type
1878 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1879                               unsigned DataLen) {
1880   using namespace llvm::support;
1881 
1882   const unsigned char *End = d + DataLen;
1883   HeaderFileInfo HFI;
1884   unsigned Flags = *d++;
1885   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1886   HFI.isImport |= (Flags >> 5) & 0x01;
1887   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1888   HFI.DirInfo = (Flags >> 1) & 0x07;
1889   HFI.IndexHeaderMapHeader = Flags & 0x01;
1890   // FIXME: Find a better way to handle this. Maybe just store a
1891   // "has been included" flag?
1892   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1893                              HFI.NumIncludes);
1894   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1895       M, endian::readNext<uint32_t, little, unaligned>(d));
1896   if (unsigned FrameworkOffset =
1897           endian::readNext<uint32_t, little, unaligned>(d)) {
1898     // The framework offset is 1 greater than the actual offset,
1899     // since 0 is used as an indicator for "no framework name".
1900     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1901     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1902   }
1903 
1904   assert((End - d) % 4 == 0 &&
1905          "Wrong data length in HeaderFileInfo deserialization");
1906   while (d != End) {
1907     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1908     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1909     LocalSMID >>= 2;
1910 
1911     // This header is part of a module. Associate it with the module to enable
1912     // implicit module import.
1913     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1914     Module *Mod = Reader.getSubmodule(GlobalSMID);
1915     FileManager &FileMgr = Reader.getFileManager();
1916     ModuleMap &ModMap =
1917         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1918 
1919     std::string Filename = std::string(key.Filename);
1920     if (key.Imported)
1921       Reader.ResolveImportedPath(M, Filename);
1922     // FIXME: NameAsWritten
1923     Module::Header H = {std::string(key.Filename), "",
1924                         *FileMgr.getFile(Filename)};
1925     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1926     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1927   }
1928 
1929   // This HeaderFileInfo was externally loaded.
1930   HFI.External = true;
1931   HFI.IsValid = true;
1932   return HFI;
1933 }
1934 
1935 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1936                                 uint32_t MacroDirectivesOffset) {
1937   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1938   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1939 }
1940 
1941 void ASTReader::ReadDefinedMacros() {
1942   // Note that we are loading defined macros.
1943   Deserializing Macros(this);
1944 
1945   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1946     BitstreamCursor &MacroCursor = I.MacroCursor;
1947 
1948     // If there was no preprocessor block, skip this file.
1949     if (MacroCursor.getBitcodeBytes().empty())
1950       continue;
1951 
1952     BitstreamCursor Cursor = MacroCursor;
1953     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1954       Error(std::move(Err));
1955       return;
1956     }
1957 
1958     RecordData Record;
1959     while (true) {
1960       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1961       if (!MaybeE) {
1962         Error(MaybeE.takeError());
1963         return;
1964       }
1965       llvm::BitstreamEntry E = MaybeE.get();
1966 
1967       switch (E.Kind) {
1968       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1969       case llvm::BitstreamEntry::Error:
1970         Error("malformed block record in AST file");
1971         return;
1972       case llvm::BitstreamEntry::EndBlock:
1973         goto NextCursor;
1974 
1975       case llvm::BitstreamEntry::Record: {
1976         Record.clear();
1977         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1978         if (!MaybeRecord) {
1979           Error(MaybeRecord.takeError());
1980           return;
1981         }
1982         switch (MaybeRecord.get()) {
1983         default:  // Default behavior: ignore.
1984           break;
1985 
1986         case PP_MACRO_OBJECT_LIKE:
1987         case PP_MACRO_FUNCTION_LIKE: {
1988           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1989           if (II->isOutOfDate())
1990             updateOutOfDateIdentifier(*II);
1991           break;
1992         }
1993 
1994         case PP_TOKEN:
1995           // Ignore tokens.
1996           break;
1997         }
1998         break;
1999       }
2000       }
2001     }
2002     NextCursor:  ;
2003   }
2004 }
2005 
2006 namespace {
2007 
2008   /// Visitor class used to look up identifirs in an AST file.
2009   class IdentifierLookupVisitor {
2010     StringRef Name;
2011     unsigned NameHash;
2012     unsigned PriorGeneration;
2013     unsigned &NumIdentifierLookups;
2014     unsigned &NumIdentifierLookupHits;
2015     IdentifierInfo *Found = nullptr;
2016 
2017   public:
2018     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2019                             unsigned &NumIdentifierLookups,
2020                             unsigned &NumIdentifierLookupHits)
2021       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2022         PriorGeneration(PriorGeneration),
2023         NumIdentifierLookups(NumIdentifierLookups),
2024         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2025 
2026     bool operator()(ModuleFile &M) {
2027       // If we've already searched this module file, skip it now.
2028       if (M.Generation <= PriorGeneration)
2029         return true;
2030 
2031       ASTIdentifierLookupTable *IdTable
2032         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2033       if (!IdTable)
2034         return false;
2035 
2036       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2037                                      Found);
2038       ++NumIdentifierLookups;
2039       ASTIdentifierLookupTable::iterator Pos =
2040           IdTable->find_hashed(Name, NameHash, &Trait);
2041       if (Pos == IdTable->end())
2042         return false;
2043 
2044       // Dereferencing the iterator has the effect of building the
2045       // IdentifierInfo node and populating it with the various
2046       // declarations it needs.
2047       ++NumIdentifierLookupHits;
2048       Found = *Pos;
2049       return true;
2050     }
2051 
2052     // Retrieve the identifier info found within the module
2053     // files.
2054     IdentifierInfo *getIdentifierInfo() const { return Found; }
2055   };
2056 
2057 } // namespace
2058 
2059 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2060   // Note that we are loading an identifier.
2061   Deserializing AnIdentifier(this);
2062 
2063   unsigned PriorGeneration = 0;
2064   if (getContext().getLangOpts().Modules)
2065     PriorGeneration = IdentifierGeneration[&II];
2066 
2067   // If there is a global index, look there first to determine which modules
2068   // provably do not have any results for this identifier.
2069   GlobalModuleIndex::HitSet Hits;
2070   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2071   if (!loadGlobalIndex()) {
2072     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2073       HitsPtr = &Hits;
2074     }
2075   }
2076 
2077   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2078                                   NumIdentifierLookups,
2079                                   NumIdentifierLookupHits);
2080   ModuleMgr.visit(Visitor, HitsPtr);
2081   markIdentifierUpToDate(&II);
2082 }
2083 
2084 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2085   if (!II)
2086     return;
2087 
2088   II->setOutOfDate(false);
2089 
2090   // Update the generation for this identifier.
2091   if (getContext().getLangOpts().Modules)
2092     IdentifierGeneration[II] = getGeneration();
2093 }
2094 
2095 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2096                                     const PendingMacroInfo &PMInfo) {
2097   ModuleFile &M = *PMInfo.M;
2098 
2099   BitstreamCursor &Cursor = M.MacroCursor;
2100   SavedStreamPosition SavedPosition(Cursor);
2101   if (llvm::Error Err =
2102           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2103     Error(std::move(Err));
2104     return;
2105   }
2106 
2107   struct ModuleMacroRecord {
2108     SubmoduleID SubModID;
2109     MacroInfo *MI;
2110     SmallVector<SubmoduleID, 8> Overrides;
2111   };
2112   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2113 
2114   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2115   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2116   // macro histroy.
2117   RecordData Record;
2118   while (true) {
2119     Expected<llvm::BitstreamEntry> MaybeEntry =
2120         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2121     if (!MaybeEntry) {
2122       Error(MaybeEntry.takeError());
2123       return;
2124     }
2125     llvm::BitstreamEntry Entry = MaybeEntry.get();
2126 
2127     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2128       Error("malformed block record in AST file");
2129       return;
2130     }
2131 
2132     Record.clear();
2133     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2134     if (!MaybePP) {
2135       Error(MaybePP.takeError());
2136       return;
2137     }
2138     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2139     case PP_MACRO_DIRECTIVE_HISTORY:
2140       break;
2141 
2142     case PP_MODULE_MACRO: {
2143       ModuleMacros.push_back(ModuleMacroRecord());
2144       auto &Info = ModuleMacros.back();
2145       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2146       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2147       for (int I = 2, N = Record.size(); I != N; ++I)
2148         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2149       continue;
2150     }
2151 
2152     default:
2153       Error("malformed block record in AST file");
2154       return;
2155     }
2156 
2157     // We found the macro directive history; that's the last record
2158     // for this macro.
2159     break;
2160   }
2161 
2162   // Module macros are listed in reverse dependency order.
2163   {
2164     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2165     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2166     for (auto &MMR : ModuleMacros) {
2167       Overrides.clear();
2168       for (unsigned ModID : MMR.Overrides) {
2169         Module *Mod = getSubmodule(ModID);
2170         auto *Macro = PP.getModuleMacro(Mod, II);
2171         assert(Macro && "missing definition for overridden macro");
2172         Overrides.push_back(Macro);
2173       }
2174 
2175       bool Inserted = false;
2176       Module *Owner = getSubmodule(MMR.SubModID);
2177       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2178     }
2179   }
2180 
2181   // Don't read the directive history for a module; we don't have anywhere
2182   // to put it.
2183   if (M.isModule())
2184     return;
2185 
2186   // Deserialize the macro directives history in reverse source-order.
2187   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2188   unsigned Idx = 0, N = Record.size();
2189   while (Idx < N) {
2190     MacroDirective *MD = nullptr;
2191     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2192     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2193     switch (K) {
2194     case MacroDirective::MD_Define: {
2195       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2196       MD = PP.AllocateDefMacroDirective(MI, Loc);
2197       break;
2198     }
2199     case MacroDirective::MD_Undefine:
2200       MD = PP.AllocateUndefMacroDirective(Loc);
2201       break;
2202     case MacroDirective::MD_Visibility:
2203       bool isPublic = Record[Idx++];
2204       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2205       break;
2206     }
2207 
2208     if (!Latest)
2209       Latest = MD;
2210     if (Earliest)
2211       Earliest->setPrevious(MD);
2212     Earliest = MD;
2213   }
2214 
2215   if (Latest)
2216     PP.setLoadedMacroDirective(II, Earliest, Latest);
2217 }
2218 
2219 bool ASTReader::shouldDisableValidationForFile(
2220     const serialization::ModuleFile &M) const {
2221   if (DisableValidationKind == DisableValidationForModuleKind::None)
2222     return false;
2223 
2224   // If a PCH is loaded and validation is disabled for PCH then disable
2225   // validation for the PCH and the modules it loads.
2226   ModuleKind K = CurrentDeserializingModuleKind.getValueOr(M.Kind);
2227 
2228   switch (K) {
2229   case MK_MainFile:
2230   case MK_Preamble:
2231   case MK_PCH:
2232     return bool(DisableValidationKind & DisableValidationForModuleKind::PCH);
2233   case MK_ImplicitModule:
2234   case MK_ExplicitModule:
2235   case MK_PrebuiltModule:
2236     return bool(DisableValidationKind & DisableValidationForModuleKind::Module);
2237   }
2238 
2239   return false;
2240 }
2241 
2242 ASTReader::InputFileInfo
2243 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2244   // Go find this input file.
2245   BitstreamCursor &Cursor = F.InputFilesCursor;
2246   SavedStreamPosition SavedPosition(Cursor);
2247   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2248     // FIXME this drops errors on the floor.
2249     consumeError(std::move(Err));
2250   }
2251 
2252   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2253   if (!MaybeCode) {
2254     // FIXME this drops errors on the floor.
2255     consumeError(MaybeCode.takeError());
2256   }
2257   unsigned Code = MaybeCode.get();
2258   RecordData Record;
2259   StringRef Blob;
2260 
2261   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2262     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2263            "invalid record type for input file");
2264   else {
2265     // FIXME this drops errors on the floor.
2266     consumeError(Maybe.takeError());
2267   }
2268 
2269   assert(Record[0] == ID && "Bogus stored ID or offset");
2270   InputFileInfo R;
2271   R.StoredSize = static_cast<off_t>(Record[1]);
2272   R.StoredTime = static_cast<time_t>(Record[2]);
2273   R.Overridden = static_cast<bool>(Record[3]);
2274   R.Transient = static_cast<bool>(Record[4]);
2275   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2276   R.Filename = std::string(Blob);
2277   ResolveImportedPath(F, R.Filename);
2278 
2279   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2280   if (!MaybeEntry) // FIXME this drops errors on the floor.
2281     consumeError(MaybeEntry.takeError());
2282   llvm::BitstreamEntry Entry = MaybeEntry.get();
2283   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2284          "expected record type for input file hash");
2285 
2286   Record.clear();
2287   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2288     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2289            "invalid record type for input file hash");
2290   else {
2291     // FIXME this drops errors on the floor.
2292     consumeError(Maybe.takeError());
2293   }
2294   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2295                   static_cast<uint64_t>(Record[0]);
2296   return R;
2297 }
2298 
2299 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2300 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2301   // If this ID is bogus, just return an empty input file.
2302   if (ID == 0 || ID > F.InputFilesLoaded.size())
2303     return InputFile();
2304 
2305   // If we've already loaded this input file, return it.
2306   if (F.InputFilesLoaded[ID-1].getFile())
2307     return F.InputFilesLoaded[ID-1];
2308 
2309   if (F.InputFilesLoaded[ID-1].isNotFound())
2310     return InputFile();
2311 
2312   // Go find this input file.
2313   BitstreamCursor &Cursor = F.InputFilesCursor;
2314   SavedStreamPosition SavedPosition(Cursor);
2315   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2316     // FIXME this drops errors on the floor.
2317     consumeError(std::move(Err));
2318   }
2319 
2320   InputFileInfo FI = readInputFileInfo(F, ID);
2321   off_t StoredSize = FI.StoredSize;
2322   time_t StoredTime = FI.StoredTime;
2323   bool Overridden = FI.Overridden;
2324   bool Transient = FI.Transient;
2325   StringRef Filename = FI.Filename;
2326   uint64_t StoredContentHash = FI.ContentHash;
2327 
2328   OptionalFileEntryRefDegradesToFileEntryPtr File =
2329       expectedToOptional(FileMgr.getFileRef(Filename, /*OpenFile=*/false));
2330 
2331   // If we didn't find the file, resolve it relative to the
2332   // original directory from which this AST file was created.
2333   if (!File && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2334       F.OriginalDir != F.BaseDirectory) {
2335     std::string Resolved = resolveFileRelativeToOriginalDir(
2336         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2337     if (!Resolved.empty())
2338       File = expectedToOptional(FileMgr.getFileRef(Resolved));
2339   }
2340 
2341   // For an overridden file, create a virtual file with the stored
2342   // size/timestamp.
2343   if ((Overridden || Transient) && !File)
2344     File = FileMgr.getVirtualFileRef(Filename, StoredSize, StoredTime);
2345 
2346   if (!File) {
2347     if (Complain) {
2348       std::string ErrorStr = "could not find file '";
2349       ErrorStr += Filename;
2350       ErrorStr += "' referenced by AST file '";
2351       ErrorStr += F.FileName;
2352       ErrorStr += "'";
2353       Error(ErrorStr);
2354     }
2355     // Record that we didn't find the file.
2356     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2357     return InputFile();
2358   }
2359 
2360   // Check if there was a request to override the contents of the file
2361   // that was part of the precompiled header. Overriding such a file
2362   // can lead to problems when lexing using the source locations from the
2363   // PCH.
2364   SourceManager &SM = getSourceManager();
2365   // FIXME: Reject if the overrides are different.
2366   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2367     if (Complain)
2368       Error(diag::err_fe_pch_file_overridden, Filename);
2369 
2370     // After emitting the diagnostic, bypass the overriding file to recover
2371     // (this creates a separate FileEntry).
2372     File = SM.bypassFileContentsOverride(*File);
2373     if (!File) {
2374       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2375       return InputFile();
2376     }
2377   }
2378 
2379   struct Change {
2380     enum ModificationKind {
2381       Size,
2382       ModTime,
2383       Content,
2384       None,
2385     } Kind;
2386     llvm::Optional<int64_t> Old = llvm::None;
2387     llvm::Optional<int64_t> New = llvm::None;
2388   };
2389   auto HasInputFileChanged = [&]() {
2390     if (StoredSize != File->getSize())
2391       return Change{Change::Size, StoredSize, File->getSize()};
2392     if (!shouldDisableValidationForFile(F) && StoredTime &&
2393         StoredTime != File->getModificationTime()) {
2394       Change MTimeChange = {Change::ModTime, StoredTime,
2395                             File->getModificationTime()};
2396 
2397       // In case the modification time changes but not the content,
2398       // accept the cached file as legit.
2399       if (ValidateASTInputFilesContent &&
2400           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2401         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2402         if (!MemBuffOrError) {
2403           if (!Complain)
2404             return MTimeChange;
2405           std::string ErrorStr = "could not get buffer for file '";
2406           ErrorStr += File->getName();
2407           ErrorStr += "'";
2408           Error(ErrorStr);
2409           return MTimeChange;
2410         }
2411 
2412         // FIXME: hash_value is not guaranteed to be stable!
2413         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2414         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2415           return Change{Change::None};
2416 
2417         return Change{Change::Content};
2418       }
2419       return MTimeChange;
2420     }
2421     return Change{Change::None};
2422   };
2423 
2424   bool IsOutOfDate = false;
2425   auto FileChange = HasInputFileChanged();
2426   // For an overridden file, there is nothing to validate.
2427   if (!Overridden && FileChange.Kind != Change::None) {
2428     if (Complain && !Diags.isDiagnosticInFlight()) {
2429       // Build a list of the PCH imports that got us here (in reverse).
2430       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2431       while (!ImportStack.back()->ImportedBy.empty())
2432         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2433 
2434       // The top-level PCH is stale.
2435       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2436       Diag(diag::err_fe_ast_file_modified)
2437           << Filename << moduleKindForDiagnostic(ImportStack.back()->Kind)
2438           << TopLevelPCHName << FileChange.Kind
2439           << (FileChange.Old && FileChange.New)
2440           << llvm::itostr(FileChange.Old.getValueOr(0))
2441           << llvm::itostr(FileChange.New.getValueOr(0));
2442 
2443       // Print the import stack.
2444       if (ImportStack.size() > 1) {
2445         Diag(diag::note_pch_required_by)
2446           << Filename << ImportStack[0]->FileName;
2447         for (unsigned I = 1; I < ImportStack.size(); ++I)
2448           Diag(diag::note_pch_required_by)
2449             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2450       }
2451 
2452       Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2453     }
2454 
2455     IsOutOfDate = true;
2456   }
2457   // FIXME: If the file is overridden and we've already opened it,
2458   // issue an error (or split it into a separate FileEntry).
2459 
2460   InputFile IF = InputFile(*File, Overridden || Transient, IsOutOfDate);
2461 
2462   // Note that we've loaded this input file.
2463   F.InputFilesLoaded[ID-1] = IF;
2464   return IF;
2465 }
2466 
2467 /// If we are loading a relocatable PCH or module file, and the filename
2468 /// is not an absolute path, add the system or module root to the beginning of
2469 /// the file name.
2470 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2471   // Resolve relative to the base directory, if we have one.
2472   if (!M.BaseDirectory.empty())
2473     return ResolveImportedPath(Filename, M.BaseDirectory);
2474 }
2475 
2476 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2477   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2478     return;
2479 
2480   SmallString<128> Buffer;
2481   llvm::sys::path::append(Buffer, Prefix, Filename);
2482   Filename.assign(Buffer.begin(), Buffer.end());
2483 }
2484 
2485 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2486   switch (ARR) {
2487   case ASTReader::Failure: return true;
2488   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2489   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2490   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2491   case ASTReader::ConfigurationMismatch:
2492     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2493   case ASTReader::HadErrors: return true;
2494   case ASTReader::Success: return false;
2495   }
2496 
2497   llvm_unreachable("unknown ASTReadResult");
2498 }
2499 
2500 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2501     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2502     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2503     std::string &SuggestedPredefines) {
2504   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2505     // FIXME this drops errors on the floor.
2506     consumeError(std::move(Err));
2507     return Failure;
2508   }
2509 
2510   // Read all of the records in the options block.
2511   RecordData Record;
2512   ASTReadResult Result = Success;
2513   while (true) {
2514     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2515     if (!MaybeEntry) {
2516       // FIXME this drops errors on the floor.
2517       consumeError(MaybeEntry.takeError());
2518       return Failure;
2519     }
2520     llvm::BitstreamEntry Entry = MaybeEntry.get();
2521 
2522     switch (Entry.Kind) {
2523     case llvm::BitstreamEntry::Error:
2524     case llvm::BitstreamEntry::SubBlock:
2525       return Failure;
2526 
2527     case llvm::BitstreamEntry::EndBlock:
2528       return Result;
2529 
2530     case llvm::BitstreamEntry::Record:
2531       // The interesting case.
2532       break;
2533     }
2534 
2535     // Read and process a record.
2536     Record.clear();
2537     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2538     if (!MaybeRecordType) {
2539       // FIXME this drops errors on the floor.
2540       consumeError(MaybeRecordType.takeError());
2541       return Failure;
2542     }
2543     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2544     case LANGUAGE_OPTIONS: {
2545       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2546       if (ParseLanguageOptions(Record, Complain, Listener,
2547                                AllowCompatibleConfigurationMismatch))
2548         Result = ConfigurationMismatch;
2549       break;
2550     }
2551 
2552     case TARGET_OPTIONS: {
2553       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2554       if (ParseTargetOptions(Record, Complain, Listener,
2555                              AllowCompatibleConfigurationMismatch))
2556         Result = ConfigurationMismatch;
2557       break;
2558     }
2559 
2560     case FILE_SYSTEM_OPTIONS: {
2561       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2562       if (!AllowCompatibleConfigurationMismatch &&
2563           ParseFileSystemOptions(Record, Complain, Listener))
2564         Result = ConfigurationMismatch;
2565       break;
2566     }
2567 
2568     case HEADER_SEARCH_OPTIONS: {
2569       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2570       if (!AllowCompatibleConfigurationMismatch &&
2571           ParseHeaderSearchOptions(Record, Complain, Listener))
2572         Result = ConfigurationMismatch;
2573       break;
2574     }
2575 
2576     case PREPROCESSOR_OPTIONS:
2577       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2578       if (!AllowCompatibleConfigurationMismatch &&
2579           ParsePreprocessorOptions(Record, Complain, Listener,
2580                                    SuggestedPredefines))
2581         Result = ConfigurationMismatch;
2582       break;
2583     }
2584   }
2585 }
2586 
2587 ASTReader::ASTReadResult
2588 ASTReader::ReadControlBlock(ModuleFile &F,
2589                             SmallVectorImpl<ImportedModule> &Loaded,
2590                             const ModuleFile *ImportedBy,
2591                             unsigned ClientLoadCapabilities) {
2592   BitstreamCursor &Stream = F.Stream;
2593 
2594   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2595     Error(std::move(Err));
2596     return Failure;
2597   }
2598 
2599   // Lambda to read the unhashed control block the first time it's called.
2600   //
2601   // For PCM files, the unhashed control block cannot be read until after the
2602   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2603   // need to look ahead before reading the IMPORTS record.  For consistency,
2604   // this block is always read somehow (see BitstreamEntry::EndBlock).
2605   bool HasReadUnhashedControlBlock = false;
2606   auto readUnhashedControlBlockOnce = [&]() {
2607     if (!HasReadUnhashedControlBlock) {
2608       HasReadUnhashedControlBlock = true;
2609       if (ASTReadResult Result =
2610               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2611         return Result;
2612     }
2613     return Success;
2614   };
2615 
2616   bool DisableValidation = shouldDisableValidationForFile(F);
2617 
2618   // Read all of the records and blocks in the control block.
2619   RecordData Record;
2620   unsigned NumInputs = 0;
2621   unsigned NumUserInputs = 0;
2622   StringRef BaseDirectoryAsWritten;
2623   while (true) {
2624     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2625     if (!MaybeEntry) {
2626       Error(MaybeEntry.takeError());
2627       return Failure;
2628     }
2629     llvm::BitstreamEntry Entry = MaybeEntry.get();
2630 
2631     switch (Entry.Kind) {
2632     case llvm::BitstreamEntry::Error:
2633       Error("malformed block record in AST file");
2634       return Failure;
2635     case llvm::BitstreamEntry::EndBlock: {
2636       // Validate the module before returning.  This call catches an AST with
2637       // no module name and no imports.
2638       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2639         return Result;
2640 
2641       // Validate input files.
2642       const HeaderSearchOptions &HSOpts =
2643           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2644 
2645       // All user input files reside at the index range [0, NumUserInputs), and
2646       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2647       // loaded module files, ignore missing inputs.
2648       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2649           F.Kind != MK_PrebuiltModule) {
2650         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2651 
2652         // If we are reading a module, we will create a verification timestamp,
2653         // so we verify all input files.  Otherwise, verify only user input
2654         // files.
2655 
2656         unsigned N = NumUserInputs;
2657         if (ValidateSystemInputs ||
2658             (HSOpts.ModulesValidateOncePerBuildSession &&
2659              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2660              F.Kind == MK_ImplicitModule))
2661           N = NumInputs;
2662 
2663         for (unsigned I = 0; I < N; ++I) {
2664           InputFile IF = getInputFile(F, I+1, Complain);
2665           if (!IF.getFile() || IF.isOutOfDate())
2666             return OutOfDate;
2667         }
2668       }
2669 
2670       if (Listener)
2671         Listener->visitModuleFile(F.FileName, F.Kind);
2672 
2673       if (Listener && Listener->needsInputFileVisitation()) {
2674         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2675                                                                 : NumUserInputs;
2676         for (unsigned I = 0; I < N; ++I) {
2677           bool IsSystem = I >= NumUserInputs;
2678           InputFileInfo FI = readInputFileInfo(F, I+1);
2679           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2680                                    F.Kind == MK_ExplicitModule ||
2681                                    F.Kind == MK_PrebuiltModule);
2682         }
2683       }
2684 
2685       return Success;
2686     }
2687 
2688     case llvm::BitstreamEntry::SubBlock:
2689       switch (Entry.ID) {
2690       case INPUT_FILES_BLOCK_ID:
2691         F.InputFilesCursor = Stream;
2692         if (llvm::Error Err = Stream.SkipBlock()) {
2693           Error(std::move(Err));
2694           return Failure;
2695         }
2696         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2697           Error("malformed block record in AST file");
2698           return Failure;
2699         }
2700         continue;
2701 
2702       case OPTIONS_BLOCK_ID:
2703         // If we're reading the first module for this group, check its options
2704         // are compatible with ours. For modules it imports, no further checking
2705         // is required, because we checked them when we built it.
2706         if (Listener && !ImportedBy) {
2707           // Should we allow the configuration of the module file to differ from
2708           // the configuration of the current translation unit in a compatible
2709           // way?
2710           //
2711           // FIXME: Allow this for files explicitly specified with -include-pch.
2712           bool AllowCompatibleConfigurationMismatch =
2713               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2714 
2715           ASTReadResult Result =
2716               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2717                                AllowCompatibleConfigurationMismatch, *Listener,
2718                                SuggestedPredefines);
2719           if (Result == Failure) {
2720             Error("malformed block record in AST file");
2721             return Result;
2722           }
2723 
2724           if (DisableValidation ||
2725               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2726             Result = Success;
2727 
2728           // If we can't load the module, exit early since we likely
2729           // will rebuild the module anyway. The stream may be in the
2730           // middle of a block.
2731           if (Result != Success)
2732             return Result;
2733         } else if (llvm::Error Err = Stream.SkipBlock()) {
2734           Error(std::move(Err));
2735           return Failure;
2736         }
2737         continue;
2738 
2739       default:
2740         if (llvm::Error Err = Stream.SkipBlock()) {
2741           Error(std::move(Err));
2742           return Failure;
2743         }
2744         continue;
2745       }
2746 
2747     case llvm::BitstreamEntry::Record:
2748       // The interesting case.
2749       break;
2750     }
2751 
2752     // Read and process a record.
2753     Record.clear();
2754     StringRef Blob;
2755     Expected<unsigned> MaybeRecordType =
2756         Stream.readRecord(Entry.ID, Record, &Blob);
2757     if (!MaybeRecordType) {
2758       Error(MaybeRecordType.takeError());
2759       return Failure;
2760     }
2761     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2762     case METADATA: {
2763       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2764         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2765           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2766                                         : diag::err_pch_version_too_new);
2767         return VersionMismatch;
2768       }
2769 
2770       bool hasErrors = Record[6];
2771       if (hasErrors && !DisableValidation) {
2772         // If requested by the caller and the module hasn't already been read
2773         // or compiled, mark modules on error as out-of-date.
2774         if ((ClientLoadCapabilities & ARR_TreatModuleWithErrorsAsOutOfDate) &&
2775             canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2776           return OutOfDate;
2777 
2778         if (!AllowASTWithCompilerErrors) {
2779           Diag(diag::err_pch_with_compiler_errors);
2780           return HadErrors;
2781         }
2782       }
2783       if (hasErrors) {
2784         Diags.ErrorOccurred = true;
2785         Diags.UncompilableErrorOccurred = true;
2786         Diags.UnrecoverableErrorOccurred = true;
2787       }
2788 
2789       F.RelocatablePCH = Record[4];
2790       // Relative paths in a relocatable PCH are relative to our sysroot.
2791       if (F.RelocatablePCH)
2792         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2793 
2794       F.HasTimestamps = Record[5];
2795 
2796       const std::string &CurBranch = getClangFullRepositoryVersion();
2797       StringRef ASTBranch = Blob;
2798       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2799         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2800           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2801         return VersionMismatch;
2802       }
2803       break;
2804     }
2805 
2806     case IMPORTS: {
2807       // Validate the AST before processing any imports (otherwise, untangling
2808       // them can be error-prone and expensive).  A module will have a name and
2809       // will already have been validated, but this catches the PCH case.
2810       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2811         return Result;
2812 
2813       // Load each of the imported PCH files.
2814       unsigned Idx = 0, N = Record.size();
2815       while (Idx < N) {
2816         // Read information about the AST file.
2817         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2818         // The import location will be the local one for now; we will adjust
2819         // all import locations of module imports after the global source
2820         // location info are setup, in ReadAST.
2821         SourceLocation ImportLoc =
2822             ReadUntranslatedSourceLocation(Record[Idx++]);
2823         off_t StoredSize = (off_t)Record[Idx++];
2824         time_t StoredModTime = (time_t)Record[Idx++];
2825         auto FirstSignatureByte = Record.begin() + Idx;
2826         ASTFileSignature StoredSignature = ASTFileSignature::create(
2827             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2828         Idx += ASTFileSignature::size;
2829 
2830         std::string ImportedName = ReadString(Record, Idx);
2831         std::string ImportedFile;
2832 
2833         // For prebuilt and explicit modules first consult the file map for
2834         // an override. Note that here we don't search prebuilt module
2835         // directories, only the explicit name to file mappings. Also, we will
2836         // still verify the size/signature making sure it is essentially the
2837         // same file but perhaps in a different location.
2838         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2839           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2840             ImportedName, /*FileMapOnly*/ true);
2841 
2842         if (ImportedFile.empty())
2843           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2844           // ModuleCache as when writing.
2845           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2846         else
2847           SkipPath(Record, Idx);
2848 
2849         // If our client can't cope with us being out of date, we can't cope with
2850         // our dependency being missing.
2851         unsigned Capabilities = ClientLoadCapabilities;
2852         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2853           Capabilities &= ~ARR_Missing;
2854 
2855         // Load the AST file.
2856         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2857                                   Loaded, StoredSize, StoredModTime,
2858                                   StoredSignature, Capabilities);
2859 
2860         // If we diagnosed a problem, produce a backtrace.
2861         bool recompilingFinalized =
2862             Result == OutOfDate && (Capabilities & ARR_OutOfDate) &&
2863             getModuleManager().getModuleCache().isPCMFinal(F.FileName);
2864         if (isDiagnosedResult(Result, Capabilities) || recompilingFinalized)
2865           Diag(diag::note_module_file_imported_by)
2866               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2867         if (recompilingFinalized)
2868           Diag(diag::note_module_file_conflict);
2869 
2870         switch (Result) {
2871         case Failure: return Failure;
2872           // If we have to ignore the dependency, we'll have to ignore this too.
2873         case Missing:
2874         case OutOfDate: return OutOfDate;
2875         case VersionMismatch: return VersionMismatch;
2876         case ConfigurationMismatch: return ConfigurationMismatch;
2877         case HadErrors: return HadErrors;
2878         case Success: break;
2879         }
2880       }
2881       break;
2882     }
2883 
2884     case ORIGINAL_FILE:
2885       F.OriginalSourceFileID = FileID::get(Record[0]);
2886       F.ActualOriginalSourceFileName = std::string(Blob);
2887       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2888       ResolveImportedPath(F, F.OriginalSourceFileName);
2889       break;
2890 
2891     case ORIGINAL_FILE_ID:
2892       F.OriginalSourceFileID = FileID::get(Record[0]);
2893       break;
2894 
2895     case ORIGINAL_PCH_DIR:
2896       F.OriginalDir = std::string(Blob);
2897       break;
2898 
2899     case MODULE_NAME:
2900       F.ModuleName = std::string(Blob);
2901       Diag(diag::remark_module_import)
2902           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2903           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2904       if (Listener)
2905         Listener->ReadModuleName(F.ModuleName);
2906 
2907       // Validate the AST as soon as we have a name so we can exit early on
2908       // failure.
2909       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2910         return Result;
2911 
2912       break;
2913 
2914     case MODULE_DIRECTORY: {
2915       // Save the BaseDirectory as written in the PCM for computing the module
2916       // filename for the ModuleCache.
2917       BaseDirectoryAsWritten = Blob;
2918       assert(!F.ModuleName.empty() &&
2919              "MODULE_DIRECTORY found before MODULE_NAME");
2920       // If we've already loaded a module map file covering this module, we may
2921       // have a better path for it (relative to the current build).
2922       Module *M = PP.getHeaderSearchInfo().lookupModule(
2923           F.ModuleName, SourceLocation(), /*AllowSearch*/ true,
2924           /*AllowExtraModuleMapSearch*/ true);
2925       if (M && M->Directory) {
2926         // If we're implicitly loading a module, the base directory can't
2927         // change between the build and use.
2928         // Don't emit module relocation error if we have -fno-validate-pch
2929         if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
2930                   DisableValidationForModuleKind::Module) &&
2931             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2932           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2933           if (!BuildDir || *BuildDir != M->Directory) {
2934             if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
2935               Diag(diag::err_imported_module_relocated)
2936                   << F.ModuleName << Blob << M->Directory->getName();
2937             return OutOfDate;
2938           }
2939         }
2940         F.BaseDirectory = std::string(M->Directory->getName());
2941       } else {
2942         F.BaseDirectory = std::string(Blob);
2943       }
2944       break;
2945     }
2946 
2947     case MODULE_MAP_FILE:
2948       if (ASTReadResult Result =
2949               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2950         return Result;
2951       break;
2952 
2953     case INPUT_FILE_OFFSETS:
2954       NumInputs = Record[0];
2955       NumUserInputs = Record[1];
2956       F.InputFileOffsets =
2957           (const llvm::support::unaligned_uint64_t *)Blob.data();
2958       F.InputFilesLoaded.resize(NumInputs);
2959       F.NumUserInputFiles = NumUserInputs;
2960       break;
2961     }
2962   }
2963 }
2964 
2965 llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
2966                                     unsigned ClientLoadCapabilities) {
2967   BitstreamCursor &Stream = F.Stream;
2968 
2969   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID))
2970     return Err;
2971   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2972 
2973   // Read all of the records and blocks for the AST file.
2974   RecordData Record;
2975   while (true) {
2976     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2977     if (!MaybeEntry)
2978       return MaybeEntry.takeError();
2979     llvm::BitstreamEntry Entry = MaybeEntry.get();
2980 
2981     switch (Entry.Kind) {
2982     case llvm::BitstreamEntry::Error:
2983       return llvm::createStringError(
2984           std::errc::illegal_byte_sequence,
2985           "error at end of module block in AST file");
2986     case llvm::BitstreamEntry::EndBlock:
2987       // Outside of C++, we do not store a lookup map for the translation unit.
2988       // Instead, mark it as needing a lookup map to be built if this module
2989       // contains any declarations lexically within it (which it always does!).
2990       // This usually has no cost, since we very rarely need the lookup map for
2991       // the translation unit outside C++.
2992       if (ASTContext *Ctx = ContextObj) {
2993         DeclContext *DC = Ctx->getTranslationUnitDecl();
2994         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2995           DC->setMustBuildLookupTable();
2996       }
2997 
2998       return llvm::Error::success();
2999     case llvm::BitstreamEntry::SubBlock:
3000       switch (Entry.ID) {
3001       case DECLTYPES_BLOCK_ID:
3002         // We lazily load the decls block, but we want to set up the
3003         // DeclsCursor cursor to point into it.  Clone our current bitcode
3004         // cursor to it, enter the block and read the abbrevs in that block.
3005         // With the main cursor, we just skip over it.
3006         F.DeclsCursor = Stream;
3007         if (llvm::Error Err = Stream.SkipBlock())
3008           return Err;
3009         if (llvm::Error Err = ReadBlockAbbrevs(
3010                 F.DeclsCursor, DECLTYPES_BLOCK_ID, &F.DeclsBlockStartOffset))
3011           return Err;
3012         break;
3013 
3014       case PREPROCESSOR_BLOCK_ID:
3015         F.MacroCursor = Stream;
3016         if (!PP.getExternalSource())
3017           PP.setExternalSource(this);
3018 
3019         if (llvm::Error Err = Stream.SkipBlock())
3020           return Err;
3021         if (llvm::Error Err =
3022                 ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID))
3023           return Err;
3024         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3025         break;
3026 
3027       case PREPROCESSOR_DETAIL_BLOCK_ID:
3028         F.PreprocessorDetailCursor = Stream;
3029 
3030         if (llvm::Error Err = Stream.SkipBlock()) {
3031           return Err;
3032         }
3033         if (llvm::Error Err = ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3034                                                PREPROCESSOR_DETAIL_BLOCK_ID))
3035           return Err;
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 (llvm::Error Err = ReadSourceManagerBlock(F))
3047           return Err;
3048         break;
3049 
3050       case SUBMODULE_BLOCK_ID:
3051         if (llvm::Error Err = ReadSubmoduleBlock(F, ClientLoadCapabilities))
3052           return Err;
3053         break;
3054 
3055       case COMMENTS_BLOCK_ID: {
3056         BitstreamCursor C = Stream;
3057 
3058         if (llvm::Error Err = Stream.SkipBlock())
3059           return Err;
3060         if (llvm::Error Err = ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID))
3061           return Err;
3062         CommentsCursors.push_back(std::make_pair(C, &F));
3063         break;
3064       }
3065 
3066       default:
3067         if (llvm::Error Err = Stream.SkipBlock())
3068           return Err;
3069         break;
3070       }
3071       continue;
3072 
3073     case llvm::BitstreamEntry::Record:
3074       // The interesting case.
3075       break;
3076     }
3077 
3078     // Read and process a record.
3079     Record.clear();
3080     StringRef Blob;
3081     Expected<unsigned> MaybeRecordType =
3082         Stream.readRecord(Entry.ID, Record, &Blob);
3083     if (!MaybeRecordType)
3084       return MaybeRecordType.takeError();
3085     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3086 
3087     // If we're not loading an AST context, we don't care about most records.
3088     if (!ContextObj) {
3089       switch (RecordType) {
3090       case IDENTIFIER_TABLE:
3091       case IDENTIFIER_OFFSET:
3092       case INTERESTING_IDENTIFIERS:
3093       case STATISTICS:
3094       case PP_CONDITIONAL_STACK:
3095       case PP_COUNTER_VALUE:
3096       case SOURCE_LOCATION_OFFSETS:
3097       case MODULE_OFFSET_MAP:
3098       case SOURCE_MANAGER_LINE_TABLE:
3099       case SOURCE_LOCATION_PRELOADS:
3100       case PPD_ENTITIES_OFFSETS:
3101       case HEADER_SEARCH_TABLE:
3102       case IMPORTED_MODULES:
3103       case MACRO_OFFSET:
3104         break;
3105       default:
3106         continue;
3107       }
3108     }
3109 
3110     switch (RecordType) {
3111     default:  // Default behavior: ignore.
3112       break;
3113 
3114     case TYPE_OFFSET: {
3115       if (F.LocalNumTypes != 0)
3116         return llvm::createStringError(
3117             std::errc::illegal_byte_sequence,
3118             "duplicate TYPE_OFFSET record in AST file");
3119       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3120       F.LocalNumTypes = Record[0];
3121       unsigned LocalBaseTypeIndex = Record[1];
3122       F.BaseTypeIndex = getTotalNumTypes();
3123 
3124       if (F.LocalNumTypes > 0) {
3125         // Introduce the global -> local mapping for types within this module.
3126         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3127 
3128         // Introduce the local -> global mapping for types within this module.
3129         F.TypeRemap.insertOrReplace(
3130           std::make_pair(LocalBaseTypeIndex,
3131                          F.BaseTypeIndex - LocalBaseTypeIndex));
3132 
3133         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3134       }
3135       break;
3136     }
3137 
3138     case DECL_OFFSET: {
3139       if (F.LocalNumDecls != 0)
3140         return llvm::createStringError(
3141             std::errc::illegal_byte_sequence,
3142             "duplicate DECL_OFFSET record in AST file");
3143       F.DeclOffsets = (const DeclOffset *)Blob.data();
3144       F.LocalNumDecls = Record[0];
3145       unsigned LocalBaseDeclID = Record[1];
3146       F.BaseDeclID = getTotalNumDecls();
3147 
3148       if (F.LocalNumDecls > 0) {
3149         // Introduce the global -> local mapping for declarations within this
3150         // module.
3151         GlobalDeclMap.insert(
3152           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3153 
3154         // Introduce the local -> global mapping for declarations within this
3155         // module.
3156         F.DeclRemap.insertOrReplace(
3157           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3158 
3159         // Introduce the global -> local mapping for declarations within this
3160         // module.
3161         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3162 
3163         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3164       }
3165       break;
3166     }
3167 
3168     case TU_UPDATE_LEXICAL: {
3169       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3170       LexicalContents Contents(
3171           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3172               Blob.data()),
3173           static_cast<unsigned int>(Blob.size() / 4));
3174       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3175       TU->setHasExternalLexicalStorage(true);
3176       break;
3177     }
3178 
3179     case UPDATE_VISIBLE: {
3180       unsigned Idx = 0;
3181       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3182       auto *Data = (const unsigned char*)Blob.data();
3183       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3184       // If we've already loaded the decl, perform the updates when we finish
3185       // loading this block.
3186       if (Decl *D = GetExistingDecl(ID))
3187         PendingUpdateRecords.push_back(
3188             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3189       break;
3190     }
3191 
3192     case IDENTIFIER_TABLE:
3193       F.IdentifierTableData =
3194           reinterpret_cast<const unsigned char *>(Blob.data());
3195       if (Record[0]) {
3196         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3197             F.IdentifierTableData + Record[0],
3198             F.IdentifierTableData + sizeof(uint32_t),
3199             F.IdentifierTableData,
3200             ASTIdentifierLookupTrait(*this, F));
3201 
3202         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3203       }
3204       break;
3205 
3206     case IDENTIFIER_OFFSET: {
3207       if (F.LocalNumIdentifiers != 0)
3208         return llvm::createStringError(
3209             std::errc::illegal_byte_sequence,
3210             "duplicate IDENTIFIER_OFFSET record in AST file");
3211       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3212       F.LocalNumIdentifiers = Record[0];
3213       unsigned LocalBaseIdentifierID = Record[1];
3214       F.BaseIdentifierID = getTotalNumIdentifiers();
3215 
3216       if (F.LocalNumIdentifiers > 0) {
3217         // Introduce the global -> local mapping for identifiers within this
3218         // module.
3219         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3220                                                   &F));
3221 
3222         // Introduce the local -> global mapping for identifiers within this
3223         // module.
3224         F.IdentifierRemap.insertOrReplace(
3225           std::make_pair(LocalBaseIdentifierID,
3226                          F.BaseIdentifierID - LocalBaseIdentifierID));
3227 
3228         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3229                                  + F.LocalNumIdentifiers);
3230       }
3231       break;
3232     }
3233 
3234     case INTERESTING_IDENTIFIERS:
3235       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3236       break;
3237 
3238     case EAGERLY_DESERIALIZED_DECLS:
3239       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3240       // about "interesting" decls (for instance, if we're building a module).
3241       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3242         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3243       break;
3244 
3245     case MODULAR_CODEGEN_DECLS:
3246       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3247       // them (ie: if we're not codegenerating this module).
3248       if (F.Kind == MK_MainFile ||
3249           getContext().getLangOpts().BuildingPCHWithObjectFile)
3250         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3251           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3252       break;
3253 
3254     case SPECIAL_TYPES:
3255       if (SpecialTypes.empty()) {
3256         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3257           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3258         break;
3259       }
3260 
3261       if (SpecialTypes.size() != Record.size())
3262         return llvm::createStringError(std::errc::illegal_byte_sequence,
3263                                        "invalid special-types record");
3264 
3265       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3266         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3267         if (!SpecialTypes[I])
3268           SpecialTypes[I] = ID;
3269         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3270         // merge step?
3271       }
3272       break;
3273 
3274     case STATISTICS:
3275       TotalNumStatements += Record[0];
3276       TotalNumMacros += Record[1];
3277       TotalLexicalDeclContexts += Record[2];
3278       TotalVisibleDeclContexts += Record[3];
3279       break;
3280 
3281     case UNUSED_FILESCOPED_DECLS:
3282       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3283         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3284       break;
3285 
3286     case DELEGATING_CTORS:
3287       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3288         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3289       break;
3290 
3291     case WEAK_UNDECLARED_IDENTIFIERS:
3292       if (Record.size() % 4 != 0)
3293         return llvm::createStringError(std::errc::illegal_byte_sequence,
3294                                        "invalid weak identifiers record");
3295 
3296       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3297       // files. This isn't the way to do it :)
3298       WeakUndeclaredIdentifiers.clear();
3299 
3300       // Translate the weak, undeclared identifiers into global IDs.
3301       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3302         WeakUndeclaredIdentifiers.push_back(
3303           getGlobalIdentifierID(F, Record[I++]));
3304         WeakUndeclaredIdentifiers.push_back(
3305           getGlobalIdentifierID(F, Record[I++]));
3306         WeakUndeclaredIdentifiers.push_back(
3307           ReadSourceLocation(F, Record, I).getRawEncoding());
3308         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3309       }
3310       break;
3311 
3312     case SELECTOR_OFFSETS: {
3313       F.SelectorOffsets = (const uint32_t *)Blob.data();
3314       F.LocalNumSelectors = Record[0];
3315       unsigned LocalBaseSelectorID = Record[1];
3316       F.BaseSelectorID = getTotalNumSelectors();
3317 
3318       if (F.LocalNumSelectors > 0) {
3319         // Introduce the global -> local mapping for selectors within this
3320         // module.
3321         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3322 
3323         // Introduce the local -> global mapping for selectors within this
3324         // module.
3325         F.SelectorRemap.insertOrReplace(
3326           std::make_pair(LocalBaseSelectorID,
3327                          F.BaseSelectorID - LocalBaseSelectorID));
3328 
3329         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3330       }
3331       break;
3332     }
3333 
3334     case METHOD_POOL:
3335       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3336       if (Record[0])
3337         F.SelectorLookupTable
3338           = ASTSelectorLookupTable::Create(
3339                         F.SelectorLookupTableData + Record[0],
3340                         F.SelectorLookupTableData,
3341                         ASTSelectorLookupTrait(*this, F));
3342       TotalNumMethodPoolEntries += Record[1];
3343       break;
3344 
3345     case REFERENCED_SELECTOR_POOL:
3346       if (!Record.empty()) {
3347         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3348           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3349                                                                 Record[Idx++]));
3350           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3351                                               getRawEncoding());
3352         }
3353       }
3354       break;
3355 
3356     case PP_CONDITIONAL_STACK:
3357       if (!Record.empty()) {
3358         unsigned Idx = 0, End = Record.size() - 1;
3359         bool ReachedEOFWhileSkipping = Record[Idx++];
3360         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3361         if (ReachedEOFWhileSkipping) {
3362           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3363           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3364           bool FoundNonSkipPortion = Record[Idx++];
3365           bool FoundElse = Record[Idx++];
3366           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3367           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3368                            FoundElse, ElseLoc);
3369         }
3370         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3371         while (Idx < End) {
3372           auto Loc = ReadSourceLocation(F, Record, Idx);
3373           bool WasSkipping = Record[Idx++];
3374           bool FoundNonSkip = Record[Idx++];
3375           bool FoundElse = Record[Idx++];
3376           ConditionalStack.push_back(
3377               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3378         }
3379         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3380       }
3381       break;
3382 
3383     case PP_COUNTER_VALUE:
3384       if (!Record.empty() && Listener)
3385         Listener->ReadCounter(F, Record[0]);
3386       break;
3387 
3388     case FILE_SORTED_DECLS:
3389       F.FileSortedDecls = (const DeclID *)Blob.data();
3390       F.NumFileSortedDecls = Record[0];
3391       break;
3392 
3393     case SOURCE_LOCATION_OFFSETS: {
3394       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3395       F.LocalNumSLocEntries = Record[0];
3396       SourceLocation::UIntTy SLocSpaceSize = Record[1];
3397       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3398       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3399           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3400                                               SLocSpaceSize);
3401       if (!F.SLocEntryBaseID)
3402         return llvm::createStringError(std::errc::invalid_argument,
3403                                        "ran out of source locations");
3404       // Make our entry in the range map. BaseID is negative and growing, so
3405       // we invert it. Because we invert it, though, we need the other end of
3406       // the range.
3407       unsigned RangeStart =
3408           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3409       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3410       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3411 
3412       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3413       assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0);
3414       GlobalSLocOffsetMap.insert(
3415           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3416                            - SLocSpaceSize,&F));
3417 
3418       // Initialize the remapping table.
3419       // Invalid stays invalid.
3420       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3421       // This module. Base was 2 when being compiled.
3422       F.SLocRemap.insertOrReplace(std::make_pair(
3423           2U, static_cast<SourceLocation::IntTy>(F.SLocEntryBaseOffset - 2)));
3424 
3425       TotalNumSLocEntries += F.LocalNumSLocEntries;
3426       break;
3427     }
3428 
3429     case MODULE_OFFSET_MAP:
3430       F.ModuleOffsetMap = Blob;
3431       break;
3432 
3433     case SOURCE_MANAGER_LINE_TABLE:
3434       ParseLineTable(F, Record);
3435       break;
3436 
3437     case SOURCE_LOCATION_PRELOADS: {
3438       // Need to transform from the local view (1-based IDs) to the global view,
3439       // which is based off F.SLocEntryBaseID.
3440       if (!F.PreloadSLocEntries.empty())
3441         return llvm::createStringError(
3442             std::errc::illegal_byte_sequence,
3443             "Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3444 
3445       F.PreloadSLocEntries.swap(Record);
3446       break;
3447     }
3448 
3449     case EXT_VECTOR_DECLS:
3450       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3451         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3452       break;
3453 
3454     case VTABLE_USES:
3455       if (Record.size() % 3 != 0)
3456         return llvm::createStringError(std::errc::illegal_byte_sequence,
3457                                        "Invalid VTABLE_USES record");
3458 
3459       // Later tables overwrite earlier ones.
3460       // FIXME: Modules will have some trouble with this. This is clearly not
3461       // the right way to do this.
3462       VTableUses.clear();
3463 
3464       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3465         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3466         VTableUses.push_back(
3467           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3468         VTableUses.push_back(Record[Idx++]);
3469       }
3470       break;
3471 
3472     case PENDING_IMPLICIT_INSTANTIATIONS:
3473       if (PendingInstantiations.size() % 2 != 0)
3474         return llvm::createStringError(
3475             std::errc::illegal_byte_sequence,
3476             "Invalid existing PendingInstantiations");
3477 
3478       if (Record.size() % 2 != 0)
3479         return llvm::createStringError(
3480             std::errc::illegal_byte_sequence,
3481             "Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3482 
3483       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3484         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3485         PendingInstantiations.push_back(
3486           ReadSourceLocation(F, Record, I).getRawEncoding());
3487       }
3488       break;
3489 
3490     case SEMA_DECL_REFS:
3491       if (Record.size() != 3)
3492         return llvm::createStringError(std::errc::illegal_byte_sequence,
3493                                        "Invalid SEMA_DECL_REFS block");
3494       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3495         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3496       break;
3497 
3498     case PPD_ENTITIES_OFFSETS: {
3499       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3500       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3501       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3502 
3503       unsigned LocalBasePreprocessedEntityID = Record[0];
3504 
3505       unsigned StartingID;
3506       if (!PP.getPreprocessingRecord())
3507         PP.createPreprocessingRecord();
3508       if (!PP.getPreprocessingRecord()->getExternalSource())
3509         PP.getPreprocessingRecord()->SetExternalSource(*this);
3510       StartingID
3511         = PP.getPreprocessingRecord()
3512             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3513       F.BasePreprocessedEntityID = StartingID;
3514 
3515       if (F.NumPreprocessedEntities > 0) {
3516         // Introduce the global -> local mapping for preprocessed entities in
3517         // this module.
3518         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3519 
3520         // Introduce the local -> global mapping for preprocessed entities in
3521         // this module.
3522         F.PreprocessedEntityRemap.insertOrReplace(
3523           std::make_pair(LocalBasePreprocessedEntityID,
3524             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3525       }
3526 
3527       break;
3528     }
3529 
3530     case PPD_SKIPPED_RANGES: {
3531       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3532       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3533       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3534 
3535       if (!PP.getPreprocessingRecord())
3536         PP.createPreprocessingRecord();
3537       if (!PP.getPreprocessingRecord()->getExternalSource())
3538         PP.getPreprocessingRecord()->SetExternalSource(*this);
3539       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3540           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3541 
3542       if (F.NumPreprocessedSkippedRanges > 0)
3543         GlobalSkippedRangeMap.insert(
3544             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3545       break;
3546     }
3547 
3548     case DECL_UPDATE_OFFSETS:
3549       if (Record.size() % 2 != 0)
3550         return llvm::createStringError(
3551             std::errc::illegal_byte_sequence,
3552             "invalid DECL_UPDATE_OFFSETS block in AST file");
3553       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3554         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3555         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3556 
3557         // If we've already loaded the decl, perform the updates when we finish
3558         // loading this block.
3559         if (Decl *D = GetExistingDecl(ID))
3560           PendingUpdateRecords.push_back(
3561               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3562       }
3563       break;
3564 
3565     case OBJC_CATEGORIES_MAP:
3566       if (F.LocalNumObjCCategoriesInMap != 0)
3567         return llvm::createStringError(
3568             std::errc::illegal_byte_sequence,
3569             "duplicate OBJC_CATEGORIES_MAP record in AST file");
3570 
3571       F.LocalNumObjCCategoriesInMap = Record[0];
3572       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3573       break;
3574 
3575     case OBJC_CATEGORIES:
3576       F.ObjCCategories.swap(Record);
3577       break;
3578 
3579     case CUDA_SPECIAL_DECL_REFS:
3580       // Later tables overwrite earlier ones.
3581       // FIXME: Modules will have trouble with this.
3582       CUDASpecialDeclRefs.clear();
3583       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3584         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3585       break;
3586 
3587     case HEADER_SEARCH_TABLE:
3588       F.HeaderFileInfoTableData = Blob.data();
3589       F.LocalNumHeaderFileInfos = Record[1];
3590       if (Record[0]) {
3591         F.HeaderFileInfoTable
3592           = HeaderFileInfoLookupTable::Create(
3593                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3594                    (const unsigned char *)F.HeaderFileInfoTableData,
3595                    HeaderFileInfoTrait(*this, F,
3596                                        &PP.getHeaderSearchInfo(),
3597                                        Blob.data() + Record[2]));
3598 
3599         PP.getHeaderSearchInfo().SetExternalSource(this);
3600         if (!PP.getHeaderSearchInfo().getExternalLookup())
3601           PP.getHeaderSearchInfo().SetExternalLookup(this);
3602       }
3603       break;
3604 
3605     case FP_PRAGMA_OPTIONS:
3606       // Later tables overwrite earlier ones.
3607       FPPragmaOptions.swap(Record);
3608       break;
3609 
3610     case OPENCL_EXTENSIONS:
3611       for (unsigned I = 0, E = Record.size(); I != E; ) {
3612         auto Name = ReadString(Record, I);
3613         auto &OptInfo = OpenCLExtensions.OptMap[Name];
3614         OptInfo.Supported = Record[I++] != 0;
3615         OptInfo.Enabled = Record[I++] != 0;
3616         OptInfo.WithPragma = Record[I++] != 0;
3617         OptInfo.Avail = Record[I++];
3618         OptInfo.Core = Record[I++];
3619         OptInfo.Opt = Record[I++];
3620       }
3621       break;
3622 
3623     case TENTATIVE_DEFINITIONS:
3624       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3625         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3626       break;
3627 
3628     case KNOWN_NAMESPACES:
3629       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3630         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3631       break;
3632 
3633     case UNDEFINED_BUT_USED:
3634       if (UndefinedButUsed.size() % 2 != 0)
3635         return llvm::createStringError(std::errc::illegal_byte_sequence,
3636                                        "Invalid existing UndefinedButUsed");
3637 
3638       if (Record.size() % 2 != 0)
3639         return llvm::createStringError(std::errc::illegal_byte_sequence,
3640                                        "invalid undefined-but-used record");
3641       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3642         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3643         UndefinedButUsed.push_back(
3644             ReadSourceLocation(F, Record, I).getRawEncoding());
3645       }
3646       break;
3647 
3648     case DELETE_EXPRS_TO_ANALYZE:
3649       for (unsigned I = 0, N = Record.size(); I != N;) {
3650         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3651         const uint64_t Count = Record[I++];
3652         DelayedDeleteExprs.push_back(Count);
3653         for (uint64_t C = 0; C < Count; ++C) {
3654           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3655           bool IsArrayForm = Record[I++] == 1;
3656           DelayedDeleteExprs.push_back(IsArrayForm);
3657         }
3658       }
3659       break;
3660 
3661     case IMPORTED_MODULES:
3662       if (!F.isModule()) {
3663         // If we aren't loading a module (which has its own exports), make
3664         // all of the imported modules visible.
3665         // FIXME: Deal with macros-only imports.
3666         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3667           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3668           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3669           if (GlobalID) {
3670             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3671             if (DeserializationListener)
3672               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3673           }
3674         }
3675       }
3676       break;
3677 
3678     case MACRO_OFFSET: {
3679       if (F.LocalNumMacros != 0)
3680         return llvm::createStringError(
3681             std::errc::illegal_byte_sequence,
3682             "duplicate MACRO_OFFSET record in AST file");
3683       F.MacroOffsets = (const uint32_t *)Blob.data();
3684       F.LocalNumMacros = Record[0];
3685       unsigned LocalBaseMacroID = Record[1];
3686       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3687       F.BaseMacroID = getTotalNumMacros();
3688 
3689       if (F.LocalNumMacros > 0) {
3690         // Introduce the global -> local mapping for macros within this module.
3691         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3692 
3693         // Introduce the local -> global mapping for macros within this module.
3694         F.MacroRemap.insertOrReplace(
3695           std::make_pair(LocalBaseMacroID,
3696                          F.BaseMacroID - LocalBaseMacroID));
3697 
3698         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3699       }
3700       break;
3701     }
3702 
3703     case LATE_PARSED_TEMPLATE:
3704       LateParsedTemplates.emplace_back(
3705           std::piecewise_construct, std::forward_as_tuple(&F),
3706           std::forward_as_tuple(Record.begin(), Record.end()));
3707       break;
3708 
3709     case OPTIMIZE_PRAGMA_OPTIONS:
3710       if (Record.size() != 1)
3711         return llvm::createStringError(std::errc::illegal_byte_sequence,
3712                                        "invalid pragma optimize record");
3713       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3714       break;
3715 
3716     case MSSTRUCT_PRAGMA_OPTIONS:
3717       if (Record.size() != 1)
3718         return llvm::createStringError(std::errc::illegal_byte_sequence,
3719                                        "invalid pragma ms_struct record");
3720       PragmaMSStructState = Record[0];
3721       break;
3722 
3723     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3724       if (Record.size() != 2)
3725         return llvm::createStringError(
3726             std::errc::illegal_byte_sequence,
3727             "invalid pragma pointers to members record");
3728       PragmaMSPointersToMembersState = Record[0];
3729       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3730       break;
3731 
3732     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3733       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3734         UnusedLocalTypedefNameCandidates.push_back(
3735             getGlobalDeclID(F, Record[I]));
3736       break;
3737 
3738     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3739       if (Record.size() != 1)
3740         return llvm::createStringError(std::errc::illegal_byte_sequence,
3741                                        "invalid cuda pragma options record");
3742       ForceCUDAHostDeviceDepth = Record[0];
3743       break;
3744 
3745     case ALIGN_PACK_PRAGMA_OPTIONS: {
3746       if (Record.size() < 3)
3747         return llvm::createStringError(std::errc::illegal_byte_sequence,
3748                                        "invalid pragma pack record");
3749       PragmaAlignPackCurrentValue = ReadAlignPackInfo(Record[0]);
3750       PragmaAlignPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3751       unsigned NumStackEntries = Record[2];
3752       unsigned Idx = 3;
3753       // Reset the stack when importing a new module.
3754       PragmaAlignPackStack.clear();
3755       for (unsigned I = 0; I < NumStackEntries; ++I) {
3756         PragmaAlignPackStackEntry Entry;
3757         Entry.Value = ReadAlignPackInfo(Record[Idx++]);
3758         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3759         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3760         PragmaAlignPackStrings.push_back(ReadString(Record, Idx));
3761         Entry.SlotLabel = PragmaAlignPackStrings.back();
3762         PragmaAlignPackStack.push_back(Entry);
3763       }
3764       break;
3765     }
3766 
3767     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3768       if (Record.size() < 3)
3769         return llvm::createStringError(std::errc::illegal_byte_sequence,
3770                                        "invalid pragma float control record");
3771       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3772       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3773       unsigned NumStackEntries = Record[2];
3774       unsigned Idx = 3;
3775       // Reset the stack when importing a new module.
3776       FpPragmaStack.clear();
3777       for (unsigned I = 0; I < NumStackEntries; ++I) {
3778         FpPragmaStackEntry Entry;
3779         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3780         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3781         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3782         FpPragmaStrings.push_back(ReadString(Record, Idx));
3783         Entry.SlotLabel = FpPragmaStrings.back();
3784         FpPragmaStack.push_back(Entry);
3785       }
3786       break;
3787     }
3788 
3789     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3790       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3791         DeclsToCheckForDeferredDiags.insert(getGlobalDeclID(F, Record[I]));
3792       break;
3793     }
3794   }
3795 }
3796 
3797 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3798   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3799 
3800   // Additional remapping information.
3801   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3802   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3803   F.ModuleOffsetMap = StringRef();
3804 
3805   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3806   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3807     F.SLocRemap.insert(std::make_pair(0U, 0));
3808     F.SLocRemap.insert(std::make_pair(2U, 1));
3809   }
3810 
3811   // Continuous range maps we may be updating in our module.
3812   using SLocRemapBuilder =
3813       ContinuousRangeMap<SourceLocation::UIntTy, SourceLocation::IntTy,
3814                          2>::Builder;
3815   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3816   SLocRemapBuilder SLocRemap(F.SLocRemap);
3817   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3818   RemapBuilder MacroRemap(F.MacroRemap);
3819   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3820   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3821   RemapBuilder SelectorRemap(F.SelectorRemap);
3822   RemapBuilder DeclRemap(F.DeclRemap);
3823   RemapBuilder TypeRemap(F.TypeRemap);
3824 
3825   while (Data < DataEnd) {
3826     // FIXME: Looking up dependency modules by filename is horrible. Let's
3827     // start fixing this with prebuilt, explicit and implicit modules and see
3828     // how it goes...
3829     using namespace llvm::support;
3830     ModuleKind Kind = static_cast<ModuleKind>(
3831       endian::readNext<uint8_t, little, unaligned>(Data));
3832     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3833     StringRef Name = StringRef((const char*)Data, Len);
3834     Data += Len;
3835     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3836                               Kind == MK_ImplicitModule
3837                           ? ModuleMgr.lookupByModuleName(Name)
3838                           : ModuleMgr.lookupByFileName(Name));
3839     if (!OM) {
3840       std::string Msg =
3841           "SourceLocation remap refers to unknown module, cannot find ";
3842       Msg.append(std::string(Name));
3843       Error(Msg);
3844       return;
3845     }
3846 
3847     SourceLocation::UIntTy SLocOffset =
3848         endian::readNext<uint32_t, little, unaligned>(Data);
3849     uint32_t IdentifierIDOffset =
3850         endian::readNext<uint32_t, little, unaligned>(Data);
3851     uint32_t MacroIDOffset =
3852         endian::readNext<uint32_t, little, unaligned>(Data);
3853     uint32_t PreprocessedEntityIDOffset =
3854         endian::readNext<uint32_t, little, unaligned>(Data);
3855     uint32_t SubmoduleIDOffset =
3856         endian::readNext<uint32_t, little, unaligned>(Data);
3857     uint32_t SelectorIDOffset =
3858         endian::readNext<uint32_t, little, unaligned>(Data);
3859     uint32_t DeclIDOffset =
3860         endian::readNext<uint32_t, little, unaligned>(Data);
3861     uint32_t TypeIndexOffset =
3862         endian::readNext<uint32_t, little, unaligned>(Data);
3863 
3864     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3865                          RemapBuilder &Remap) {
3866       constexpr uint32_t None = std::numeric_limits<uint32_t>::max();
3867       if (Offset != None)
3868         Remap.insert(std::make_pair(Offset,
3869                                     static_cast<int>(BaseOffset - Offset)));
3870     };
3871 
3872     constexpr SourceLocation::UIntTy SLocNone =
3873         std::numeric_limits<SourceLocation::UIntTy>::max();
3874     if (SLocOffset != SLocNone)
3875       SLocRemap.insert(std::make_pair(
3876           SLocOffset, static_cast<SourceLocation::IntTy>(
3877                           OM->SLocEntryBaseOffset - SLocOffset)));
3878 
3879     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3880     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3881     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3882               PreprocessedEntityRemap);
3883     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3884     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3885     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3886     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3887 
3888     // Global -> local mappings.
3889     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3890   }
3891 }
3892 
3893 ASTReader::ASTReadResult
3894 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3895                                   const ModuleFile *ImportedBy,
3896                                   unsigned ClientLoadCapabilities) {
3897   unsigned Idx = 0;
3898   F.ModuleMapPath = ReadPath(F, Record, Idx);
3899 
3900   // Try to resolve ModuleName in the current header search context and
3901   // verify that it is found in the same module map file as we saved. If the
3902   // top-level AST file is a main file, skip this check because there is no
3903   // usable header search context.
3904   assert(!F.ModuleName.empty() &&
3905          "MODULE_NAME should come before MODULE_MAP_FILE");
3906   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3907     // An implicitly-loaded module file should have its module listed in some
3908     // module map file that we've already loaded.
3909     Module *M =
3910         PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
3911     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3912     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3913     // Don't emit module relocation error if we have -fno-validate-pch
3914     if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
3915               DisableValidationForModuleKind::Module) &&
3916         !ModMap) {
3917       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities)) {
3918         if (auto ASTFE = M ? M->getASTFile() : None) {
3919           // This module was defined by an imported (explicit) module.
3920           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3921                                                << ASTFE->getName();
3922         } else {
3923           // This module was built with a different module map.
3924           Diag(diag::err_imported_module_not_found)
3925               << F.ModuleName << F.FileName
3926               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3927               << !ImportedBy;
3928           // In case it was imported by a PCH, there's a chance the user is
3929           // just missing to include the search path to the directory containing
3930           // the modulemap.
3931           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3932             Diag(diag::note_imported_by_pch_module_not_found)
3933                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3934         }
3935       }
3936       return OutOfDate;
3937     }
3938 
3939     assert(M && M->Name == F.ModuleName && "found module with different name");
3940 
3941     // Check the primary module map file.
3942     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3943     if (!StoredModMap || *StoredModMap != ModMap) {
3944       assert(ModMap && "found module is missing module map file");
3945       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3946              "top-level import should be verified");
3947       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3948       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3949         Diag(diag::err_imported_module_modmap_changed)
3950             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3951             << ModMap->getName() << F.ModuleMapPath << NotImported;
3952       return OutOfDate;
3953     }
3954 
3955     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3956     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3957       // FIXME: we should use input files rather than storing names.
3958       std::string Filename = ReadPath(F, Record, Idx);
3959       auto SF = FileMgr.getFile(Filename, false, false);
3960       if (!SF) {
3961         if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3962           Error("could not find file '" + Filename +"' referenced by AST file");
3963         return OutOfDate;
3964       }
3965       AdditionalStoredMaps.insert(*SF);
3966     }
3967 
3968     // Check any additional module map files (e.g. module.private.modulemap)
3969     // that are not in the pcm.
3970     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3971       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3972         // Remove files that match
3973         // Note: SmallPtrSet::erase is really remove
3974         if (!AdditionalStoredMaps.erase(ModMap)) {
3975           if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3976             Diag(diag::err_module_different_modmap)
3977               << F.ModuleName << /*new*/0 << ModMap->getName();
3978           return OutOfDate;
3979         }
3980       }
3981     }
3982 
3983     // Check any additional module map files that are in the pcm, but not
3984     // found in header search. Cases that match are already removed.
3985     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3986       if (!canRecoverFromOutOfDate(F.FileName, ClientLoadCapabilities))
3987         Diag(diag::err_module_different_modmap)
3988           << F.ModuleName << /*not new*/1 << ModMap->getName();
3989       return OutOfDate;
3990     }
3991   }
3992 
3993   if (Listener)
3994     Listener->ReadModuleMapFile(F.ModuleMapPath);
3995   return Success;
3996 }
3997 
3998 /// Move the given method to the back of the global list of methods.
3999 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4000   // Find the entry for this selector in the method pool.
4001   Sema::GlobalMethodPool::iterator Known
4002     = S.MethodPool.find(Method->getSelector());
4003   if (Known == S.MethodPool.end())
4004     return;
4005 
4006   // Retrieve the appropriate method list.
4007   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4008                                                     : Known->second.second;
4009   bool Found = false;
4010   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4011     if (!Found) {
4012       if (List->getMethod() == Method) {
4013         Found = true;
4014       } else {
4015         // Keep searching.
4016         continue;
4017       }
4018     }
4019 
4020     if (List->getNext())
4021       List->setMethod(List->getNext()->getMethod());
4022     else
4023       List->setMethod(Method);
4024   }
4025 }
4026 
4027 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4028   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4029   for (Decl *D : Names) {
4030     bool wasHidden = !D->isUnconditionallyVisible();
4031     D->setVisibleDespiteOwningModule();
4032 
4033     if (wasHidden && SemaObj) {
4034       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4035         moveMethodToBackOfGlobalList(*SemaObj, Method);
4036       }
4037     }
4038   }
4039 }
4040 
4041 void ASTReader::makeModuleVisible(Module *Mod,
4042                                   Module::NameVisibilityKind NameVisibility,
4043                                   SourceLocation ImportLoc) {
4044   llvm::SmallPtrSet<Module *, 4> Visited;
4045   SmallVector<Module *, 4> Stack;
4046   Stack.push_back(Mod);
4047   while (!Stack.empty()) {
4048     Mod = Stack.pop_back_val();
4049 
4050     if (NameVisibility <= Mod->NameVisibility) {
4051       // This module already has this level of visibility (or greater), so
4052       // there is nothing more to do.
4053       continue;
4054     }
4055 
4056     if (Mod->isUnimportable()) {
4057       // Modules that aren't importable cannot be made visible.
4058       continue;
4059     }
4060 
4061     // Update the module's name visibility.
4062     Mod->NameVisibility = NameVisibility;
4063 
4064     // If we've already deserialized any names from this module,
4065     // mark them as visible.
4066     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4067     if (Hidden != HiddenNamesMap.end()) {
4068       auto HiddenNames = std::move(*Hidden);
4069       HiddenNamesMap.erase(Hidden);
4070       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4071       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4072              "making names visible added hidden names");
4073     }
4074 
4075     // Push any exported modules onto the stack to be marked as visible.
4076     SmallVector<Module *, 16> Exports;
4077     Mod->getExportedModules(Exports);
4078     for (SmallVectorImpl<Module *>::iterator
4079            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4080       Module *Exported = *I;
4081       if (Visited.insert(Exported).second)
4082         Stack.push_back(Exported);
4083     }
4084   }
4085 }
4086 
4087 /// We've merged the definition \p MergedDef into the existing definition
4088 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4089 /// visible.
4090 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4091                                           NamedDecl *MergedDef) {
4092   if (!Def->isUnconditionallyVisible()) {
4093     // If MergedDef is visible or becomes visible, make the definition visible.
4094     if (MergedDef->isUnconditionallyVisible())
4095       Def->setVisibleDespiteOwningModule();
4096     else {
4097       getContext().mergeDefinitionIntoModule(
4098           Def, MergedDef->getImportedOwningModule(),
4099           /*NotifyListeners*/ false);
4100       PendingMergedDefinitionsToDeduplicate.insert(Def);
4101     }
4102   }
4103 }
4104 
4105 bool ASTReader::loadGlobalIndex() {
4106   if (GlobalIndex)
4107     return false;
4108 
4109   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4110       !PP.getLangOpts().Modules)
4111     return true;
4112 
4113   // Try to load the global index.
4114   TriedLoadingGlobalIndex = true;
4115   StringRef ModuleCachePath
4116     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4117   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4118       GlobalModuleIndex::readIndex(ModuleCachePath);
4119   if (llvm::Error Err = std::move(Result.second)) {
4120     assert(!Result.first);
4121     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4122     return true;
4123   }
4124 
4125   GlobalIndex.reset(Result.first);
4126   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4127   return false;
4128 }
4129 
4130 bool ASTReader::isGlobalIndexUnavailable() const {
4131   return PP.getLangOpts().Modules && UseGlobalIndex &&
4132          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4133 }
4134 
4135 static void updateModuleTimestamp(ModuleFile &MF) {
4136   // Overwrite the timestamp file contents so that file's mtime changes.
4137   std::string TimestampFilename = MF.getTimestampFilename();
4138   std::error_code EC;
4139   llvm::raw_fd_ostream OS(TimestampFilename, EC,
4140                           llvm::sys::fs::OF_TextWithCRLF);
4141   if (EC)
4142     return;
4143   OS << "Timestamp file\n";
4144   OS.close();
4145   OS.clear_error(); // Avoid triggering a fatal error.
4146 }
4147 
4148 /// Given a cursor at the start of an AST file, scan ahead and drop the
4149 /// cursor into the start of the given block ID, returning false on success and
4150 /// true on failure.
4151 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4152   while (true) {
4153     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4154     if (!MaybeEntry) {
4155       // FIXME this drops errors on the floor.
4156       consumeError(MaybeEntry.takeError());
4157       return true;
4158     }
4159     llvm::BitstreamEntry Entry = MaybeEntry.get();
4160 
4161     switch (Entry.Kind) {
4162     case llvm::BitstreamEntry::Error:
4163     case llvm::BitstreamEntry::EndBlock:
4164       return true;
4165 
4166     case llvm::BitstreamEntry::Record:
4167       // Ignore top-level records.
4168       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4169         break;
4170       else {
4171         // FIXME this drops errors on the floor.
4172         consumeError(Skipped.takeError());
4173         return true;
4174       }
4175 
4176     case llvm::BitstreamEntry::SubBlock:
4177       if (Entry.ID == BlockID) {
4178         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4179           // FIXME this drops the error on the floor.
4180           consumeError(std::move(Err));
4181           return true;
4182         }
4183         // Found it!
4184         return false;
4185       }
4186 
4187       if (llvm::Error Err = Cursor.SkipBlock()) {
4188         // FIXME this drops the error on the floor.
4189         consumeError(std::move(Err));
4190         return true;
4191       }
4192     }
4193   }
4194 }
4195 
4196 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4197                                             ModuleKind Type,
4198                                             SourceLocation ImportLoc,
4199                                             unsigned ClientLoadCapabilities,
4200                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4201   llvm::SaveAndRestore<SourceLocation>
4202     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4203   llvm::SaveAndRestore<Optional<ModuleKind>> SetCurModuleKindRAII(
4204       CurrentDeserializingModuleKind, Type);
4205 
4206   // Defer any pending actions until we get to the end of reading the AST file.
4207   Deserializing AnASTFile(this);
4208 
4209   // Bump the generation number.
4210   unsigned PreviousGeneration = 0;
4211   if (ContextObj)
4212     PreviousGeneration = incrementGeneration(*ContextObj);
4213 
4214   unsigned NumModules = ModuleMgr.size();
4215   SmallVector<ImportedModule, 4> Loaded;
4216   if (ASTReadResult ReadResult =
4217           ReadASTCore(FileName, Type, ImportLoc,
4218                       /*ImportedBy=*/nullptr, Loaded, 0, 0, ASTFileSignature(),
4219                       ClientLoadCapabilities)) {
4220     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4221                             PP.getLangOpts().Modules
4222                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4223                                 : nullptr);
4224 
4225     // If we find that any modules are unusable, the global index is going
4226     // to be out-of-date. Just remove it.
4227     GlobalIndex.reset();
4228     ModuleMgr.setGlobalIndex(nullptr);
4229     return ReadResult;
4230   }
4231 
4232   // Here comes stuff that we only do once the entire chain is loaded. Do *not*
4233   // remove modules from this point. Various fields are updated during reading
4234   // the AST block and removing the modules would result in dangling pointers.
4235   // They are generally only incidentally dereferenced, ie. a binary search
4236   // runs over `GlobalSLocEntryMap`, which could cause an invalid module to
4237   // be dereferenced but it wouldn't actually be used.
4238 
4239   // Load the AST blocks of all of the modules that we loaded. We can still
4240   // hit errors parsing the ASTs at this point.
4241   for (ImportedModule &M : Loaded) {
4242     ModuleFile &F = *M.Mod;
4243 
4244     // Read the AST block.
4245     if (llvm::Error Err = ReadASTBlock(F, ClientLoadCapabilities)) {
4246       Error(std::move(Err));
4247       return Failure;
4248     }
4249 
4250     // The AST block should always have a definition for the main module.
4251     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4252       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4253       return Failure;
4254     }
4255 
4256     // Read the extension blocks.
4257     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4258       if (llvm::Error Err = ReadExtensionBlock(F)) {
4259         Error(std::move(Err));
4260         return Failure;
4261       }
4262     }
4263 
4264     // Once read, set the ModuleFile bit base offset and update the size in
4265     // bits of all files we've seen.
4266     F.GlobalBitOffset = TotalModulesSizeInBits;
4267     TotalModulesSizeInBits += F.SizeInBits;
4268     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4269   }
4270 
4271   // Preload source locations and interesting indentifiers.
4272   for (ImportedModule &M : Loaded) {
4273     ModuleFile &F = *M.Mod;
4274 
4275     // Preload SLocEntries.
4276     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4277       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4278       // Load it through the SourceManager and don't call ReadSLocEntry()
4279       // directly because the entry may have already been loaded in which case
4280       // calling ReadSLocEntry() directly would trigger an assertion in
4281       // SourceManager.
4282       SourceMgr.getLoadedSLocEntryByID(Index);
4283     }
4284 
4285     // Map the original source file ID into the ID space of the current
4286     // compilation.
4287     if (F.OriginalSourceFileID.isValid()) {
4288       F.OriginalSourceFileID = FileID::get(
4289           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4290     }
4291 
4292     // Preload all the pending interesting identifiers by marking them out of
4293     // date.
4294     for (auto Offset : F.PreloadIdentifierOffsets) {
4295       const unsigned char *Data = F.IdentifierTableData + Offset;
4296 
4297       ASTIdentifierLookupTrait Trait(*this, F);
4298       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4299       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4300       auto &II = PP.getIdentifierTable().getOwn(Key);
4301       II.setOutOfDate(true);
4302 
4303       // Mark this identifier as being from an AST file so that we can track
4304       // whether we need to serialize it.
4305       markIdentifierFromAST(*this, II);
4306 
4307       // Associate the ID with the identifier so that the writer can reuse it.
4308       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4309       SetIdentifierInfo(ID, &II);
4310     }
4311   }
4312 
4313   // Setup the import locations and notify the module manager that we've
4314   // committed to these module files.
4315   for (ImportedModule &M : Loaded) {
4316     ModuleFile &F = *M.Mod;
4317 
4318     ModuleMgr.moduleFileAccepted(&F);
4319 
4320     // Set the import location.
4321     F.DirectImportLoc = ImportLoc;
4322     // FIXME: We assume that locations from PCH / preamble do not need
4323     // any translation.
4324     if (!M.ImportedBy)
4325       F.ImportLoc = M.ImportLoc;
4326     else
4327       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4328   }
4329 
4330   if (!PP.getLangOpts().CPlusPlus ||
4331       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4332        Type != MK_PrebuiltModule)) {
4333     // Mark all of the identifiers in the identifier table as being out of date,
4334     // so that various accessors know to check the loaded modules when the
4335     // identifier is used.
4336     //
4337     // For C++ modules, we don't need information on many identifiers (just
4338     // those that provide macros or are poisoned), so we mark all of
4339     // the interesting ones via PreloadIdentifierOffsets.
4340     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4341                                 IdEnd = PP.getIdentifierTable().end();
4342          Id != IdEnd; ++Id)
4343       Id->second->setOutOfDate(true);
4344   }
4345   // Mark selectors as out of date.
4346   for (auto Sel : SelectorGeneration)
4347     SelectorOutOfDate[Sel.first] = true;
4348 
4349   // Resolve any unresolved module exports.
4350   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4351     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4352     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4353     Module *ResolvedMod = getSubmodule(GlobalID);
4354 
4355     switch (Unresolved.Kind) {
4356     case UnresolvedModuleRef::Conflict:
4357       if (ResolvedMod) {
4358         Module::Conflict Conflict;
4359         Conflict.Other = ResolvedMod;
4360         Conflict.Message = Unresolved.String.str();
4361         Unresolved.Mod->Conflicts.push_back(Conflict);
4362       }
4363       continue;
4364 
4365     case UnresolvedModuleRef::Import:
4366       if (ResolvedMod)
4367         Unresolved.Mod->Imports.insert(ResolvedMod);
4368       continue;
4369 
4370     case UnresolvedModuleRef::Export:
4371       if (ResolvedMod || Unresolved.IsWildcard)
4372         Unresolved.Mod->Exports.push_back(
4373           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4374       continue;
4375     }
4376   }
4377   UnresolvedModuleRefs.clear();
4378 
4379   if (Imported)
4380     Imported->append(ImportedModules.begin(),
4381                      ImportedModules.end());
4382 
4383   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4384   // Might be unnecessary as use declarations are only used to build the
4385   // module itself.
4386 
4387   if (ContextObj)
4388     InitializeContext();
4389 
4390   if (SemaObj)
4391     UpdateSema();
4392 
4393   if (DeserializationListener)
4394     DeserializationListener->ReaderInitialized(this);
4395 
4396   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4397   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4398     // If this AST file is a precompiled preamble, then set the
4399     // preamble file ID of the source manager to the file source file
4400     // from which the preamble was built.
4401     if (Type == MK_Preamble) {
4402       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4403     } else if (Type == MK_MainFile) {
4404       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4405     }
4406   }
4407 
4408   // For any Objective-C class definitions we have already loaded, make sure
4409   // that we load any additional categories.
4410   if (ContextObj) {
4411     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4412       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4413                          ObjCClassesLoaded[I],
4414                          PreviousGeneration);
4415     }
4416   }
4417 
4418   if (PP.getHeaderSearchInfo()
4419           .getHeaderSearchOpts()
4420           .ModulesValidateOncePerBuildSession) {
4421     // Now we are certain that the module and all modules it depends on are
4422     // up to date.  Create or update timestamp files for modules that are
4423     // located in the module cache (not for PCH files that could be anywhere
4424     // in the filesystem).
4425     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4426       ImportedModule &M = Loaded[I];
4427       if (M.Mod->Kind == MK_ImplicitModule) {
4428         updateModuleTimestamp(*M.Mod);
4429       }
4430     }
4431   }
4432 
4433   return Success;
4434 }
4435 
4436 static ASTFileSignature readASTFileSignature(StringRef PCH);
4437 
4438 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4439 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4440   // FIXME checking magic headers is done in other places such as
4441   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4442   // always done the same. Unify it all with a helper.
4443   if (!Stream.canSkipToPos(4))
4444     return llvm::createStringError(std::errc::illegal_byte_sequence,
4445                                    "file too small to contain AST file magic");
4446   for (unsigned C : {'C', 'P', 'C', 'H'})
4447     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4448       if (Res.get() != C)
4449         return llvm::createStringError(
4450             std::errc::illegal_byte_sequence,
4451             "file doesn't start with AST file magic");
4452     } else
4453       return Res.takeError();
4454   return llvm::Error::success();
4455 }
4456 
4457 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4458   switch (Kind) {
4459   case MK_PCH:
4460     return 0; // PCH
4461   case MK_ImplicitModule:
4462   case MK_ExplicitModule:
4463   case MK_PrebuiltModule:
4464     return 1; // module
4465   case MK_MainFile:
4466   case MK_Preamble:
4467     return 2; // main source file
4468   }
4469   llvm_unreachable("unknown module kind");
4470 }
4471 
4472 ASTReader::ASTReadResult
4473 ASTReader::ReadASTCore(StringRef FileName,
4474                        ModuleKind Type,
4475                        SourceLocation ImportLoc,
4476                        ModuleFile *ImportedBy,
4477                        SmallVectorImpl<ImportedModule> &Loaded,
4478                        off_t ExpectedSize, time_t ExpectedModTime,
4479                        ASTFileSignature ExpectedSignature,
4480                        unsigned ClientLoadCapabilities) {
4481   ModuleFile *M;
4482   std::string ErrorStr;
4483   ModuleManager::AddModuleResult AddResult
4484     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4485                           getGeneration(), ExpectedSize, ExpectedModTime,
4486                           ExpectedSignature, readASTFileSignature,
4487                           M, ErrorStr);
4488 
4489   switch (AddResult) {
4490   case ModuleManager::AlreadyLoaded:
4491     Diag(diag::remark_module_import)
4492         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4493         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4494     return Success;
4495 
4496   case ModuleManager::NewlyLoaded:
4497     // Load module file below.
4498     break;
4499 
4500   case ModuleManager::Missing:
4501     // The module file was missing; if the client can handle that, return
4502     // it.
4503     if (ClientLoadCapabilities & ARR_Missing)
4504       return Missing;
4505 
4506     // Otherwise, return an error.
4507     Diag(diag::err_ast_file_not_found)
4508         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4509         << ErrorStr;
4510     return Failure;
4511 
4512   case ModuleManager::OutOfDate:
4513     // We couldn't load the module file because it is out-of-date. If the
4514     // client can handle out-of-date, return it.
4515     if (ClientLoadCapabilities & ARR_OutOfDate)
4516       return OutOfDate;
4517 
4518     // Otherwise, return an error.
4519     Diag(diag::err_ast_file_out_of_date)
4520         << moduleKindForDiagnostic(Type) << FileName << !ErrorStr.empty()
4521         << ErrorStr;
4522     return Failure;
4523   }
4524 
4525   assert(M && "Missing module file");
4526 
4527   bool ShouldFinalizePCM = false;
4528   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4529     auto &MC = getModuleManager().getModuleCache();
4530     if (ShouldFinalizePCM)
4531       MC.finalizePCM(FileName);
4532     else
4533       MC.tryToDropPCM(FileName);
4534   });
4535   ModuleFile &F = *M;
4536   BitstreamCursor &Stream = F.Stream;
4537   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4538   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4539 
4540   // Sniff for the signature.
4541   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4542     Diag(diag::err_ast_file_invalid)
4543         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4544     return Failure;
4545   }
4546 
4547   // This is used for compatibility with older PCH formats.
4548   bool HaveReadControlBlock = false;
4549   while (true) {
4550     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4551     if (!MaybeEntry) {
4552       Error(MaybeEntry.takeError());
4553       return Failure;
4554     }
4555     llvm::BitstreamEntry Entry = MaybeEntry.get();
4556 
4557     switch (Entry.Kind) {
4558     case llvm::BitstreamEntry::Error:
4559     case llvm::BitstreamEntry::Record:
4560     case llvm::BitstreamEntry::EndBlock:
4561       Error("invalid record at top-level of AST file");
4562       return Failure;
4563 
4564     case llvm::BitstreamEntry::SubBlock:
4565       break;
4566     }
4567 
4568     switch (Entry.ID) {
4569     case CONTROL_BLOCK_ID:
4570       HaveReadControlBlock = true;
4571       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4572       case Success:
4573         // Check that we didn't try to load a non-module AST file as a module.
4574         //
4575         // FIXME: Should we also perform the converse check? Loading a module as
4576         // a PCH file sort of works, but it's a bit wonky.
4577         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4578              Type == MK_PrebuiltModule) &&
4579             F.ModuleName.empty()) {
4580           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4581           if (Result != OutOfDate ||
4582               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4583             Diag(diag::err_module_file_not_module) << FileName;
4584           return Result;
4585         }
4586         break;
4587 
4588       case Failure: return Failure;
4589       case Missing: return Missing;
4590       case OutOfDate: return OutOfDate;
4591       case VersionMismatch: return VersionMismatch;
4592       case ConfigurationMismatch: return ConfigurationMismatch;
4593       case HadErrors: return HadErrors;
4594       }
4595       break;
4596 
4597     case AST_BLOCK_ID:
4598       if (!HaveReadControlBlock) {
4599         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4600           Diag(diag::err_pch_version_too_old);
4601         return VersionMismatch;
4602       }
4603 
4604       // Record that we've loaded this module.
4605       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4606       ShouldFinalizePCM = true;
4607       return Success;
4608 
4609     case UNHASHED_CONTROL_BLOCK_ID:
4610       // This block is handled using look-ahead during ReadControlBlock.  We
4611       // shouldn't get here!
4612       Error("malformed block record in AST file");
4613       return Failure;
4614 
4615     default:
4616       if (llvm::Error Err = Stream.SkipBlock()) {
4617         Error(std::move(Err));
4618         return Failure;
4619       }
4620       break;
4621     }
4622   }
4623 
4624   llvm_unreachable("unexpected break; expected return");
4625 }
4626 
4627 ASTReader::ASTReadResult
4628 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4629                                     unsigned ClientLoadCapabilities) {
4630   const HeaderSearchOptions &HSOpts =
4631       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4632   bool AllowCompatibleConfigurationMismatch =
4633       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4634   bool DisableValidation = shouldDisableValidationForFile(F);
4635 
4636   ASTReadResult Result = readUnhashedControlBlockImpl(
4637       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4638       Listener.get(),
4639       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4640 
4641   // If F was directly imported by another module, it's implicitly validated by
4642   // the importing module.
4643   if (DisableValidation || WasImportedBy ||
4644       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4645     return Success;
4646 
4647   if (Result == Failure) {
4648     Error("malformed block record in AST file");
4649     return Failure;
4650   }
4651 
4652   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4653     // If this module has already been finalized in the ModuleCache, we're stuck
4654     // with it; we can only load a single version of each module.
4655     //
4656     // This can happen when a module is imported in two contexts: in one, as a
4657     // user module; in another, as a system module (due to an import from
4658     // another module marked with the [system] flag).  It usually indicates a
4659     // bug in the module map: this module should also be marked with [system].
4660     //
4661     // If -Wno-system-headers (the default), and the first import is as a
4662     // system module, then validation will fail during the as-user import,
4663     // since -Werror flags won't have been validated.  However, it's reasonable
4664     // to treat this consistently as a system module.
4665     //
4666     // If -Wsystem-headers, the PCM on disk was built with
4667     // -Wno-system-headers, and the first import is as a user module, then
4668     // validation will fail during the as-system import since the PCM on disk
4669     // doesn't guarantee that -Werror was respected.  However, the -Werror
4670     // flags were checked during the initial as-user import.
4671     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4672       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4673       return Success;
4674     }
4675   }
4676 
4677   return Result;
4678 }
4679 
4680 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4681     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4682     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4683     bool ValidateDiagnosticOptions) {
4684   // Initialize a stream.
4685   BitstreamCursor Stream(StreamData);
4686 
4687   // Sniff for the signature.
4688   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4689     // FIXME this drops the error on the floor.
4690     consumeError(std::move(Err));
4691     return Failure;
4692   }
4693 
4694   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4695   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4696     return Failure;
4697 
4698   // Read all of the records in the options block.
4699   RecordData Record;
4700   ASTReadResult Result = Success;
4701   while (true) {
4702     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4703     if (!MaybeEntry) {
4704       // FIXME this drops the error on the floor.
4705       consumeError(MaybeEntry.takeError());
4706       return Failure;
4707     }
4708     llvm::BitstreamEntry Entry = MaybeEntry.get();
4709 
4710     switch (Entry.Kind) {
4711     case llvm::BitstreamEntry::Error:
4712     case llvm::BitstreamEntry::SubBlock:
4713       return Failure;
4714 
4715     case llvm::BitstreamEntry::EndBlock:
4716       return Result;
4717 
4718     case llvm::BitstreamEntry::Record:
4719       // The interesting case.
4720       break;
4721     }
4722 
4723     // Read and process a record.
4724     Record.clear();
4725     StringRef Blob;
4726     Expected<unsigned> MaybeRecordType =
4727         Stream.readRecord(Entry.ID, Record, &Blob);
4728     if (!MaybeRecordType) {
4729       // FIXME this drops the error.
4730       return Failure;
4731     }
4732     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4733     case SIGNATURE:
4734       if (F)
4735         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4736       break;
4737     case AST_BLOCK_HASH:
4738       if (F)
4739         F->ASTBlockHash =
4740             ASTFileSignature::create(Record.begin(), Record.end());
4741       break;
4742     case DIAGNOSTIC_OPTIONS: {
4743       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4744       if (Listener && ValidateDiagnosticOptions &&
4745           !AllowCompatibleConfigurationMismatch &&
4746           ParseDiagnosticOptions(Record, Complain, *Listener))
4747         Result = OutOfDate; // Don't return early.  Read the signature.
4748       break;
4749     }
4750     case DIAG_PRAGMA_MAPPINGS:
4751       if (!F)
4752         break;
4753       if (F->PragmaDiagMappings.empty())
4754         F->PragmaDiagMappings.swap(Record);
4755       else
4756         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4757                                      Record.begin(), Record.end());
4758       break;
4759     case HEADER_SEARCH_ENTRY_USAGE:
4760       if (!F)
4761         break;
4762       unsigned Count = Record[0];
4763       const char *Byte = Blob.data();
4764       F->SearchPathUsage = llvm::BitVector(Count, false);
4765       for (unsigned I = 0; I < Count; ++Byte)
4766         for (unsigned Bit = 0; Bit < 8 && I < Count; ++Bit, ++I)
4767           if (*Byte & (1 << Bit))
4768             F->SearchPathUsage[I] = true;
4769       break;
4770     }
4771   }
4772 }
4773 
4774 /// Parse a record and blob containing module file extension metadata.
4775 static bool parseModuleFileExtensionMetadata(
4776               const SmallVectorImpl<uint64_t> &Record,
4777               StringRef Blob,
4778               ModuleFileExtensionMetadata &Metadata) {
4779   if (Record.size() < 4) return true;
4780 
4781   Metadata.MajorVersion = Record[0];
4782   Metadata.MinorVersion = Record[1];
4783 
4784   unsigned BlockNameLen = Record[2];
4785   unsigned UserInfoLen = Record[3];
4786 
4787   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4788 
4789   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4790   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4791                                   Blob.data() + BlockNameLen + UserInfoLen);
4792   return false;
4793 }
4794 
4795 llvm::Error ASTReader::ReadExtensionBlock(ModuleFile &F) {
4796   BitstreamCursor &Stream = F.Stream;
4797 
4798   RecordData Record;
4799   while (true) {
4800     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4801     if (!MaybeEntry)
4802       return MaybeEntry.takeError();
4803     llvm::BitstreamEntry Entry = MaybeEntry.get();
4804 
4805     switch (Entry.Kind) {
4806     case llvm::BitstreamEntry::SubBlock:
4807       if (llvm::Error Err = Stream.SkipBlock())
4808         return Err;
4809       continue;
4810     case llvm::BitstreamEntry::EndBlock:
4811       return llvm::Error::success();
4812     case llvm::BitstreamEntry::Error:
4813       return llvm::createStringError(std::errc::illegal_byte_sequence,
4814                                      "malformed block record in AST file");
4815     case llvm::BitstreamEntry::Record:
4816       break;
4817     }
4818 
4819     Record.clear();
4820     StringRef Blob;
4821     Expected<unsigned> MaybeRecCode =
4822         Stream.readRecord(Entry.ID, Record, &Blob);
4823     if (!MaybeRecCode)
4824       return MaybeRecCode.takeError();
4825     switch (MaybeRecCode.get()) {
4826     case EXTENSION_METADATA: {
4827       ModuleFileExtensionMetadata Metadata;
4828       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
4829         return llvm::createStringError(
4830             std::errc::illegal_byte_sequence,
4831             "malformed EXTENSION_METADATA in AST file");
4832 
4833       // Find a module file extension with this block name.
4834       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4835       if (Known == ModuleFileExtensions.end()) break;
4836 
4837       // Form a reader.
4838       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4839                                                              F, Stream)) {
4840         F.ExtensionReaders.push_back(std::move(Reader));
4841       }
4842 
4843       break;
4844     }
4845     }
4846   }
4847 
4848   return llvm::Error::success();
4849 }
4850 
4851 void ASTReader::InitializeContext() {
4852   assert(ContextObj && "no context to initialize");
4853   ASTContext &Context = *ContextObj;
4854 
4855   // If there's a listener, notify them that we "read" the translation unit.
4856   if (DeserializationListener)
4857     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4858                                       Context.getTranslationUnitDecl());
4859 
4860   // FIXME: Find a better way to deal with collisions between these
4861   // built-in types. Right now, we just ignore the problem.
4862 
4863   // Load the special types.
4864   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4865     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4866       if (!Context.CFConstantStringTypeDecl)
4867         Context.setCFConstantStringType(GetType(String));
4868     }
4869 
4870     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4871       QualType FileType = GetType(File);
4872       if (FileType.isNull()) {
4873         Error("FILE type is NULL");
4874         return;
4875       }
4876 
4877       if (!Context.FILEDecl) {
4878         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4879           Context.setFILEDecl(Typedef->getDecl());
4880         else {
4881           const TagType *Tag = FileType->getAs<TagType>();
4882           if (!Tag) {
4883             Error("Invalid FILE type in AST file");
4884             return;
4885           }
4886           Context.setFILEDecl(Tag->getDecl());
4887         }
4888       }
4889     }
4890 
4891     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4892       QualType Jmp_bufType = GetType(Jmp_buf);
4893       if (Jmp_bufType.isNull()) {
4894         Error("jmp_buf type is NULL");
4895         return;
4896       }
4897 
4898       if (!Context.jmp_bufDecl) {
4899         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4900           Context.setjmp_bufDecl(Typedef->getDecl());
4901         else {
4902           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4903           if (!Tag) {
4904             Error("Invalid jmp_buf type in AST file");
4905             return;
4906           }
4907           Context.setjmp_bufDecl(Tag->getDecl());
4908         }
4909       }
4910     }
4911 
4912     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4913       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4914       if (Sigjmp_bufType.isNull()) {
4915         Error("sigjmp_buf type is NULL");
4916         return;
4917       }
4918 
4919       if (!Context.sigjmp_bufDecl) {
4920         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4921           Context.setsigjmp_bufDecl(Typedef->getDecl());
4922         else {
4923           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4924           assert(Tag && "Invalid sigjmp_buf type in AST file");
4925           Context.setsigjmp_bufDecl(Tag->getDecl());
4926         }
4927       }
4928     }
4929 
4930     if (unsigned ObjCIdRedef
4931           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4932       if (Context.ObjCIdRedefinitionType.isNull())
4933         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4934     }
4935 
4936     if (unsigned ObjCClassRedef
4937           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4938       if (Context.ObjCClassRedefinitionType.isNull())
4939         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4940     }
4941 
4942     if (unsigned ObjCSelRedef
4943           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4944       if (Context.ObjCSelRedefinitionType.isNull())
4945         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4946     }
4947 
4948     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4949       QualType Ucontext_tType = GetType(Ucontext_t);
4950       if (Ucontext_tType.isNull()) {
4951         Error("ucontext_t type is NULL");
4952         return;
4953       }
4954 
4955       if (!Context.ucontext_tDecl) {
4956         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4957           Context.setucontext_tDecl(Typedef->getDecl());
4958         else {
4959           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4960           assert(Tag && "Invalid ucontext_t type in AST file");
4961           Context.setucontext_tDecl(Tag->getDecl());
4962         }
4963       }
4964     }
4965   }
4966 
4967   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4968 
4969   // If there were any CUDA special declarations, deserialize them.
4970   if (!CUDASpecialDeclRefs.empty()) {
4971     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4972     Context.setcudaConfigureCallDecl(
4973                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4974   }
4975 
4976   // Re-export any modules that were imported by a non-module AST file.
4977   // FIXME: This does not make macro-only imports visible again.
4978   for (auto &Import : ImportedModules) {
4979     if (Module *Imported = getSubmodule(Import.ID)) {
4980       makeModuleVisible(Imported, Module::AllVisible,
4981                         /*ImportLoc=*/Import.ImportLoc);
4982       if (Import.ImportLoc.isValid())
4983         PP.makeModuleVisible(Imported, Import.ImportLoc);
4984       // This updates visibility for Preprocessor only. For Sema, which can be
4985       // nullptr here, we do the same later, in UpdateSema().
4986     }
4987   }
4988 }
4989 
4990 void ASTReader::finalizeForWriting() {
4991   // Nothing to do for now.
4992 }
4993 
4994 /// Reads and return the signature record from \p PCH's control block, or
4995 /// else returns 0.
4996 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4997   BitstreamCursor Stream(PCH);
4998   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4999     // FIXME this drops the error on the floor.
5000     consumeError(std::move(Err));
5001     return ASTFileSignature();
5002   }
5003 
5004   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5005   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5006     return ASTFileSignature();
5007 
5008   // Scan for SIGNATURE inside the diagnostic options block.
5009   ASTReader::RecordData Record;
5010   while (true) {
5011     Expected<llvm::BitstreamEntry> MaybeEntry =
5012         Stream.advanceSkippingSubblocks();
5013     if (!MaybeEntry) {
5014       // FIXME this drops the error on the floor.
5015       consumeError(MaybeEntry.takeError());
5016       return ASTFileSignature();
5017     }
5018     llvm::BitstreamEntry Entry = MaybeEntry.get();
5019 
5020     if (Entry.Kind != llvm::BitstreamEntry::Record)
5021       return ASTFileSignature();
5022 
5023     Record.clear();
5024     StringRef Blob;
5025     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5026     if (!MaybeRecord) {
5027       // FIXME this drops the error on the floor.
5028       consumeError(MaybeRecord.takeError());
5029       return ASTFileSignature();
5030     }
5031     if (SIGNATURE == MaybeRecord.get())
5032       return ASTFileSignature::create(Record.begin(),
5033                                       Record.begin() + ASTFileSignature::size);
5034   }
5035 }
5036 
5037 /// Retrieve the name of the original source file name
5038 /// directly from the AST file, without actually loading the AST
5039 /// file.
5040 std::string ASTReader::getOriginalSourceFile(
5041     const std::string &ASTFileName, FileManager &FileMgr,
5042     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5043   // Open the AST file.
5044   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5045   if (!Buffer) {
5046     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5047         << ASTFileName << Buffer.getError().message();
5048     return std::string();
5049   }
5050 
5051   // Initialize the stream
5052   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5053 
5054   // Sniff for the signature.
5055   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5056     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5057     return std::string();
5058   }
5059 
5060   // Scan for the CONTROL_BLOCK_ID block.
5061   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5062     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5063     return std::string();
5064   }
5065 
5066   // Scan for ORIGINAL_FILE inside the control block.
5067   RecordData Record;
5068   while (true) {
5069     Expected<llvm::BitstreamEntry> MaybeEntry =
5070         Stream.advanceSkippingSubblocks();
5071     if (!MaybeEntry) {
5072       // FIXME this drops errors on the floor.
5073       consumeError(MaybeEntry.takeError());
5074       return std::string();
5075     }
5076     llvm::BitstreamEntry Entry = MaybeEntry.get();
5077 
5078     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5079       return std::string();
5080 
5081     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5082       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5083       return std::string();
5084     }
5085 
5086     Record.clear();
5087     StringRef Blob;
5088     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5089     if (!MaybeRecord) {
5090       // FIXME this drops the errors on the floor.
5091       consumeError(MaybeRecord.takeError());
5092       return std::string();
5093     }
5094     if (ORIGINAL_FILE == MaybeRecord.get())
5095       return Blob.str();
5096   }
5097 }
5098 
5099 namespace {
5100 
5101   class SimplePCHValidator : public ASTReaderListener {
5102     const LangOptions &ExistingLangOpts;
5103     const TargetOptions &ExistingTargetOpts;
5104     const PreprocessorOptions &ExistingPPOpts;
5105     std::string ExistingModuleCachePath;
5106     FileManager &FileMgr;
5107 
5108   public:
5109     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5110                        const TargetOptions &ExistingTargetOpts,
5111                        const PreprocessorOptions &ExistingPPOpts,
5112                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5113         : ExistingLangOpts(ExistingLangOpts),
5114           ExistingTargetOpts(ExistingTargetOpts),
5115           ExistingPPOpts(ExistingPPOpts),
5116           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5117 
5118     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5119                              bool AllowCompatibleDifferences) override {
5120       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5121                                   AllowCompatibleDifferences);
5122     }
5123 
5124     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5125                            bool AllowCompatibleDifferences) override {
5126       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5127                                 AllowCompatibleDifferences);
5128     }
5129 
5130     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5131                                  StringRef SpecificModuleCachePath,
5132                                  bool Complain) override {
5133       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5134                                       ExistingModuleCachePath, nullptr,
5135                                       ExistingLangOpts, ExistingPPOpts);
5136     }
5137 
5138     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5139                                  bool Complain,
5140                                  std::string &SuggestedPredefines) override {
5141       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5142                                       SuggestedPredefines, ExistingLangOpts);
5143     }
5144   };
5145 
5146 } // namespace
5147 
5148 bool ASTReader::readASTFileControlBlock(
5149     StringRef Filename, FileManager &FileMgr,
5150     const PCHContainerReader &PCHContainerRdr,
5151     bool FindModuleFileExtensions,
5152     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5153   // Open the AST file.
5154   // FIXME: This allows use of the VFS; we do not allow use of the
5155   // VFS when actually loading a module.
5156   auto Buffer = FileMgr.getBufferForFile(Filename);
5157   if (!Buffer) {
5158     return true;
5159   }
5160 
5161   // Initialize the stream
5162   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5163   BitstreamCursor Stream(Bytes);
5164 
5165   // Sniff for the signature.
5166   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5167     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5168     return true;
5169   }
5170 
5171   // Scan for the CONTROL_BLOCK_ID block.
5172   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5173     return true;
5174 
5175   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5176   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5177   bool NeedsImports = Listener.needsImportVisitation();
5178   BitstreamCursor InputFilesCursor;
5179 
5180   RecordData Record;
5181   std::string ModuleDir;
5182   bool DoneWithControlBlock = false;
5183   while (!DoneWithControlBlock) {
5184     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5185     if (!MaybeEntry) {
5186       // FIXME this drops the error on the floor.
5187       consumeError(MaybeEntry.takeError());
5188       return true;
5189     }
5190     llvm::BitstreamEntry Entry = MaybeEntry.get();
5191 
5192     switch (Entry.Kind) {
5193     case llvm::BitstreamEntry::SubBlock: {
5194       switch (Entry.ID) {
5195       case OPTIONS_BLOCK_ID: {
5196         std::string IgnoredSuggestedPredefines;
5197         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5198                              /*AllowCompatibleConfigurationMismatch*/ false,
5199                              Listener, IgnoredSuggestedPredefines) != Success)
5200           return true;
5201         break;
5202       }
5203 
5204       case INPUT_FILES_BLOCK_ID:
5205         InputFilesCursor = Stream;
5206         if (llvm::Error Err = Stream.SkipBlock()) {
5207           // FIXME this drops the error on the floor.
5208           consumeError(std::move(Err));
5209           return true;
5210         }
5211         if (NeedsInputFiles &&
5212             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5213           return true;
5214         break;
5215 
5216       default:
5217         if (llvm::Error Err = Stream.SkipBlock()) {
5218           // FIXME this drops the error on the floor.
5219           consumeError(std::move(Err));
5220           return true;
5221         }
5222         break;
5223       }
5224 
5225       continue;
5226     }
5227 
5228     case llvm::BitstreamEntry::EndBlock:
5229       DoneWithControlBlock = true;
5230       break;
5231 
5232     case llvm::BitstreamEntry::Error:
5233       return true;
5234 
5235     case llvm::BitstreamEntry::Record:
5236       break;
5237     }
5238 
5239     if (DoneWithControlBlock) break;
5240 
5241     Record.clear();
5242     StringRef Blob;
5243     Expected<unsigned> MaybeRecCode =
5244         Stream.readRecord(Entry.ID, Record, &Blob);
5245     if (!MaybeRecCode) {
5246       // FIXME this drops the error.
5247       return Failure;
5248     }
5249     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5250     case METADATA:
5251       if (Record[0] != VERSION_MAJOR)
5252         return true;
5253       if (Listener.ReadFullVersionInformation(Blob))
5254         return true;
5255       break;
5256     case MODULE_NAME:
5257       Listener.ReadModuleName(Blob);
5258       break;
5259     case MODULE_DIRECTORY:
5260       ModuleDir = std::string(Blob);
5261       break;
5262     case MODULE_MAP_FILE: {
5263       unsigned Idx = 0;
5264       auto Path = ReadString(Record, Idx);
5265       ResolveImportedPath(Path, ModuleDir);
5266       Listener.ReadModuleMapFile(Path);
5267       break;
5268     }
5269     case INPUT_FILE_OFFSETS: {
5270       if (!NeedsInputFiles)
5271         break;
5272 
5273       unsigned NumInputFiles = Record[0];
5274       unsigned NumUserFiles = Record[1];
5275       const llvm::support::unaligned_uint64_t *InputFileOffs =
5276           (const llvm::support::unaligned_uint64_t *)Blob.data();
5277       for (unsigned I = 0; I != NumInputFiles; ++I) {
5278         // Go find this input file.
5279         bool isSystemFile = I >= NumUserFiles;
5280 
5281         if (isSystemFile && !NeedsSystemInputFiles)
5282           break; // the rest are system input files
5283 
5284         BitstreamCursor &Cursor = InputFilesCursor;
5285         SavedStreamPosition SavedPosition(Cursor);
5286         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5287           // FIXME this drops errors on the floor.
5288           consumeError(std::move(Err));
5289         }
5290 
5291         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5292         if (!MaybeCode) {
5293           // FIXME this drops errors on the floor.
5294           consumeError(MaybeCode.takeError());
5295         }
5296         unsigned Code = MaybeCode.get();
5297 
5298         RecordData Record;
5299         StringRef Blob;
5300         bool shouldContinue = false;
5301         Expected<unsigned> MaybeRecordType =
5302             Cursor.readRecord(Code, Record, &Blob);
5303         if (!MaybeRecordType) {
5304           // FIXME this drops errors on the floor.
5305           consumeError(MaybeRecordType.takeError());
5306         }
5307         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5308         case INPUT_FILE_HASH:
5309           break;
5310         case INPUT_FILE:
5311           bool Overridden = static_cast<bool>(Record[3]);
5312           std::string Filename = std::string(Blob);
5313           ResolveImportedPath(Filename, ModuleDir);
5314           shouldContinue = Listener.visitInputFile(
5315               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5316           break;
5317         }
5318         if (!shouldContinue)
5319           break;
5320       }
5321       break;
5322     }
5323 
5324     case IMPORTS: {
5325       if (!NeedsImports)
5326         break;
5327 
5328       unsigned Idx = 0, N = Record.size();
5329       while (Idx < N) {
5330         // Read information about the AST file.
5331         Idx +=
5332             1 + 1 + 1 + 1 +
5333             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5334         std::string ModuleName = ReadString(Record, Idx);
5335         std::string Filename = ReadString(Record, Idx);
5336         ResolveImportedPath(Filename, ModuleDir);
5337         Listener.visitImport(ModuleName, Filename);
5338       }
5339       break;
5340     }
5341 
5342     default:
5343       // No other validation to perform.
5344       break;
5345     }
5346   }
5347 
5348   // Look for module file extension blocks, if requested.
5349   if (FindModuleFileExtensions) {
5350     BitstreamCursor SavedStream = Stream;
5351     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5352       bool DoneWithExtensionBlock = false;
5353       while (!DoneWithExtensionBlock) {
5354         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5355         if (!MaybeEntry) {
5356           // FIXME this drops the error.
5357           return true;
5358         }
5359         llvm::BitstreamEntry Entry = MaybeEntry.get();
5360 
5361         switch (Entry.Kind) {
5362         case llvm::BitstreamEntry::SubBlock:
5363           if (llvm::Error Err = Stream.SkipBlock()) {
5364             // FIXME this drops the error on the floor.
5365             consumeError(std::move(Err));
5366             return true;
5367           }
5368           continue;
5369 
5370         case llvm::BitstreamEntry::EndBlock:
5371           DoneWithExtensionBlock = true;
5372           continue;
5373 
5374         case llvm::BitstreamEntry::Error:
5375           return true;
5376 
5377         case llvm::BitstreamEntry::Record:
5378           break;
5379         }
5380 
5381        Record.clear();
5382        StringRef Blob;
5383        Expected<unsigned> MaybeRecCode =
5384            Stream.readRecord(Entry.ID, Record, &Blob);
5385        if (!MaybeRecCode) {
5386          // FIXME this drops the error.
5387          return true;
5388        }
5389        switch (MaybeRecCode.get()) {
5390        case EXTENSION_METADATA: {
5391          ModuleFileExtensionMetadata Metadata;
5392          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5393            return true;
5394 
5395          Listener.readModuleFileExtension(Metadata);
5396          break;
5397        }
5398        }
5399       }
5400     }
5401     Stream = SavedStream;
5402   }
5403 
5404   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5405   if (readUnhashedControlBlockImpl(
5406           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5407           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5408           ValidateDiagnosticOptions) != Success)
5409     return true;
5410 
5411   return false;
5412 }
5413 
5414 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5415                                     const PCHContainerReader &PCHContainerRdr,
5416                                     const LangOptions &LangOpts,
5417                                     const TargetOptions &TargetOpts,
5418                                     const PreprocessorOptions &PPOpts,
5419                                     StringRef ExistingModuleCachePath) {
5420   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5421                                ExistingModuleCachePath, FileMgr);
5422   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5423                                   /*FindModuleFileExtensions=*/false,
5424                                   validator,
5425                                   /*ValidateDiagnosticOptions=*/true);
5426 }
5427 
5428 llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5429                                           unsigned ClientLoadCapabilities) {
5430   // Enter the submodule block.
5431   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID))
5432     return Err;
5433 
5434   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5435   bool First = true;
5436   Module *CurrentModule = nullptr;
5437   RecordData Record;
5438   while (true) {
5439     Expected<llvm::BitstreamEntry> MaybeEntry =
5440         F.Stream.advanceSkippingSubblocks();
5441     if (!MaybeEntry)
5442       return MaybeEntry.takeError();
5443     llvm::BitstreamEntry Entry = MaybeEntry.get();
5444 
5445     switch (Entry.Kind) {
5446     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5447     case llvm::BitstreamEntry::Error:
5448       return llvm::createStringError(std::errc::illegal_byte_sequence,
5449                                      "malformed block record in AST file");
5450     case llvm::BitstreamEntry::EndBlock:
5451       return llvm::Error::success();
5452     case llvm::BitstreamEntry::Record:
5453       // The interesting case.
5454       break;
5455     }
5456 
5457     // Read a record.
5458     StringRef Blob;
5459     Record.clear();
5460     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5461     if (!MaybeKind)
5462       return MaybeKind.takeError();
5463     unsigned Kind = MaybeKind.get();
5464 
5465     if ((Kind == SUBMODULE_METADATA) != First)
5466       return llvm::createStringError(
5467           std::errc::illegal_byte_sequence,
5468           "submodule metadata record should be at beginning of block");
5469     First = false;
5470 
5471     // Submodule information is only valid if we have a current module.
5472     // FIXME: Should we error on these cases?
5473     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5474         Kind != SUBMODULE_DEFINITION)
5475       continue;
5476 
5477     switch (Kind) {
5478     default:  // Default behavior: ignore.
5479       break;
5480 
5481     case SUBMODULE_DEFINITION: {
5482       if (Record.size() < 12)
5483         return llvm::createStringError(std::errc::illegal_byte_sequence,
5484                                        "malformed module definition");
5485 
5486       StringRef Name = Blob;
5487       unsigned Idx = 0;
5488       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5489       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5490       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5491       bool IsFramework = Record[Idx++];
5492       bool IsExplicit = Record[Idx++];
5493       bool IsSystem = Record[Idx++];
5494       bool IsExternC = Record[Idx++];
5495       bool InferSubmodules = Record[Idx++];
5496       bool InferExplicitSubmodules = Record[Idx++];
5497       bool InferExportWildcard = Record[Idx++];
5498       bool ConfigMacrosExhaustive = Record[Idx++];
5499       bool ModuleMapIsPrivate = Record[Idx++];
5500 
5501       Module *ParentModule = nullptr;
5502       if (Parent)
5503         ParentModule = getSubmodule(Parent);
5504 
5505       // Retrieve this (sub)module from the module map, creating it if
5506       // necessary.
5507       CurrentModule =
5508           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5509               .first;
5510 
5511       // FIXME: set the definition loc for CurrentModule, or call
5512       // ModMap.setInferredModuleAllowedBy()
5513 
5514       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5515       if (GlobalIndex >= SubmodulesLoaded.size() ||
5516           SubmodulesLoaded[GlobalIndex])
5517         return llvm::createStringError(std::errc::invalid_argument,
5518                                        "too many submodules");
5519 
5520       if (!ParentModule) {
5521         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5522           // Don't emit module relocation error if we have -fno-validate-pch
5523           if (!bool(PP.getPreprocessorOpts().DisablePCHOrModuleValidation &
5524                     DisableValidationForModuleKind::Module) &&
5525               CurFile != F.File) {
5526             auto ConflictError =
5527                 PartialDiagnostic(diag::err_module_file_conflict,
5528                                   ContextObj->DiagAllocator)
5529                 << CurrentModule->getTopLevelModuleName() << CurFile->getName()
5530                 << F.File->getName();
5531             return DiagnosticError::create(CurrentImportLoc, ConflictError);
5532           }
5533         }
5534 
5535         F.DidReadTopLevelSubmodule = true;
5536         CurrentModule->setASTFile(F.File);
5537         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5538       }
5539 
5540       CurrentModule->Kind = Kind;
5541       CurrentModule->Signature = F.Signature;
5542       CurrentModule->IsFromModuleFile = true;
5543       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5544       CurrentModule->IsExternC = IsExternC;
5545       CurrentModule->InferSubmodules = InferSubmodules;
5546       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5547       CurrentModule->InferExportWildcard = InferExportWildcard;
5548       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5549       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5550       if (DeserializationListener)
5551         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5552 
5553       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5554 
5555       // Clear out data that will be replaced by what is in the module file.
5556       CurrentModule->LinkLibraries.clear();
5557       CurrentModule->ConfigMacros.clear();
5558       CurrentModule->UnresolvedConflicts.clear();
5559       CurrentModule->Conflicts.clear();
5560 
5561       // The module is available unless it's missing a requirement; relevant
5562       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5563       // Missing headers that were present when the module was built do not
5564       // make it unavailable -- if we got this far, this must be an explicitly
5565       // imported module file.
5566       CurrentModule->Requirements.clear();
5567       CurrentModule->MissingHeaders.clear();
5568       CurrentModule->IsUnimportable =
5569           ParentModule && ParentModule->IsUnimportable;
5570       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5571       break;
5572     }
5573 
5574     case SUBMODULE_UMBRELLA_HEADER: {
5575       // FIXME: This doesn't work for framework modules as `Filename` is the
5576       //        name as written in the module file and does not include
5577       //        `Headers/`, so this path will never exist.
5578       std::string Filename = std::string(Blob);
5579       ResolveImportedPath(F, Filename);
5580       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5581         if (!CurrentModule->getUmbrellaHeader()) {
5582           // FIXME: NameAsWritten
5583           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
5584         }
5585         // Note that it's too late at this point to return out of date if the
5586         // name from the PCM doesn't match up with the one in the module map,
5587         // but also quite unlikely since we will have already checked the
5588         // modification time and size of the module map file itself.
5589       }
5590       break;
5591     }
5592 
5593     case SUBMODULE_HEADER:
5594     case SUBMODULE_EXCLUDED_HEADER:
5595     case SUBMODULE_PRIVATE_HEADER:
5596       // We lazily associate headers with their modules via the HeaderInfo table.
5597       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5598       // of complete filenames or remove it entirely.
5599       break;
5600 
5601     case SUBMODULE_TEXTUAL_HEADER:
5602     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5603       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5604       // them here.
5605       break;
5606 
5607     case SUBMODULE_TOPHEADER:
5608       CurrentModule->addTopHeaderFilename(Blob);
5609       break;
5610 
5611     case SUBMODULE_UMBRELLA_DIR: {
5612       // See comments in SUBMODULE_UMBRELLA_HEADER
5613       std::string Dirname = std::string(Blob);
5614       ResolveImportedPath(F, Dirname);
5615       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5616         if (!CurrentModule->getUmbrellaDir()) {
5617           // FIXME: NameAsWritten
5618           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
5619         }
5620       }
5621       break;
5622     }
5623 
5624     case SUBMODULE_METADATA: {
5625       F.BaseSubmoduleID = getTotalNumSubmodules();
5626       F.LocalNumSubmodules = Record[0];
5627       unsigned LocalBaseSubmoduleID = Record[1];
5628       if (F.LocalNumSubmodules > 0) {
5629         // Introduce the global -> local mapping for submodules within this
5630         // module.
5631         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5632 
5633         // Introduce the local -> global mapping for submodules within this
5634         // module.
5635         F.SubmoduleRemap.insertOrReplace(
5636           std::make_pair(LocalBaseSubmoduleID,
5637                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5638 
5639         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5640       }
5641       break;
5642     }
5643 
5644     case SUBMODULE_IMPORTS:
5645       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5646         UnresolvedModuleRef Unresolved;
5647         Unresolved.File = &F;
5648         Unresolved.Mod = CurrentModule;
5649         Unresolved.ID = Record[Idx];
5650         Unresolved.Kind = UnresolvedModuleRef::Import;
5651         Unresolved.IsWildcard = false;
5652         UnresolvedModuleRefs.push_back(Unresolved);
5653       }
5654       break;
5655 
5656     case SUBMODULE_EXPORTS:
5657       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5658         UnresolvedModuleRef Unresolved;
5659         Unresolved.File = &F;
5660         Unresolved.Mod = CurrentModule;
5661         Unresolved.ID = Record[Idx];
5662         Unresolved.Kind = UnresolvedModuleRef::Export;
5663         Unresolved.IsWildcard = Record[Idx + 1];
5664         UnresolvedModuleRefs.push_back(Unresolved);
5665       }
5666 
5667       // Once we've loaded the set of exports, there's no reason to keep
5668       // the parsed, unresolved exports around.
5669       CurrentModule->UnresolvedExports.clear();
5670       break;
5671 
5672     case SUBMODULE_REQUIRES:
5673       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5674                                     PP.getTargetInfo());
5675       break;
5676 
5677     case SUBMODULE_LINK_LIBRARY:
5678       ModMap.resolveLinkAsDependencies(CurrentModule);
5679       CurrentModule->LinkLibraries.push_back(
5680           Module::LinkLibrary(std::string(Blob), Record[0]));
5681       break;
5682 
5683     case SUBMODULE_CONFIG_MACRO:
5684       CurrentModule->ConfigMacros.push_back(Blob.str());
5685       break;
5686 
5687     case SUBMODULE_CONFLICT: {
5688       UnresolvedModuleRef Unresolved;
5689       Unresolved.File = &F;
5690       Unresolved.Mod = CurrentModule;
5691       Unresolved.ID = Record[0];
5692       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5693       Unresolved.IsWildcard = false;
5694       Unresolved.String = Blob;
5695       UnresolvedModuleRefs.push_back(Unresolved);
5696       break;
5697     }
5698 
5699     case SUBMODULE_INITIALIZERS: {
5700       if (!ContextObj)
5701         break;
5702       SmallVector<uint32_t, 16> Inits;
5703       for (auto &ID : Record)
5704         Inits.push_back(getGlobalDeclID(F, ID));
5705       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5706       break;
5707     }
5708 
5709     case SUBMODULE_EXPORT_AS:
5710       CurrentModule->ExportAsModule = Blob.str();
5711       ModMap.addLinkAsDependency(CurrentModule);
5712       break;
5713     }
5714   }
5715 }
5716 
5717 /// Parse the record that corresponds to a LangOptions data
5718 /// structure.
5719 ///
5720 /// This routine parses the language options from the AST file and then gives
5721 /// them to the AST listener if one is set.
5722 ///
5723 /// \returns true if the listener deems the file unacceptable, false otherwise.
5724 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5725                                      bool Complain,
5726                                      ASTReaderListener &Listener,
5727                                      bool AllowCompatibleDifferences) {
5728   LangOptions LangOpts;
5729   unsigned Idx = 0;
5730 #define LANGOPT(Name, Bits, Default, Description) \
5731   LangOpts.Name = Record[Idx++];
5732 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5733   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5734 #include "clang/Basic/LangOptions.def"
5735 #define SANITIZER(NAME, ID)                                                    \
5736   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5737 #include "clang/Basic/Sanitizers.def"
5738 
5739   for (unsigned N = Record[Idx++]; N; --N)
5740     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5741 
5742   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5743   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5744   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5745 
5746   LangOpts.CurrentModule = ReadString(Record, Idx);
5747 
5748   // Comment options.
5749   for (unsigned N = Record[Idx++]; N; --N) {
5750     LangOpts.CommentOpts.BlockCommandNames.push_back(
5751       ReadString(Record, Idx));
5752   }
5753   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5754 
5755   // OpenMP offloading options.
5756   for (unsigned N = Record[Idx++]; N; --N) {
5757     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5758   }
5759 
5760   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5761 
5762   return Listener.ReadLanguageOptions(LangOpts, Complain,
5763                                       AllowCompatibleDifferences);
5764 }
5765 
5766 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5767                                    ASTReaderListener &Listener,
5768                                    bool AllowCompatibleDifferences) {
5769   unsigned Idx = 0;
5770   TargetOptions TargetOpts;
5771   TargetOpts.Triple = ReadString(Record, Idx);
5772   TargetOpts.CPU = ReadString(Record, Idx);
5773   TargetOpts.TuneCPU = ReadString(Record, Idx);
5774   TargetOpts.ABI = ReadString(Record, Idx);
5775   for (unsigned N = Record[Idx++]; N; --N) {
5776     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5777   }
5778   for (unsigned N = Record[Idx++]; N; --N) {
5779     TargetOpts.Features.push_back(ReadString(Record, Idx));
5780   }
5781 
5782   return Listener.ReadTargetOptions(TargetOpts, Complain,
5783                                     AllowCompatibleDifferences);
5784 }
5785 
5786 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5787                                        ASTReaderListener &Listener) {
5788   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5789   unsigned Idx = 0;
5790 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5791 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5792   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5793 #include "clang/Basic/DiagnosticOptions.def"
5794 
5795   for (unsigned N = Record[Idx++]; N; --N)
5796     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5797   for (unsigned N = Record[Idx++]; N; --N)
5798     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5799 
5800   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5801 }
5802 
5803 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5804                                        ASTReaderListener &Listener) {
5805   FileSystemOptions FSOpts;
5806   unsigned Idx = 0;
5807   FSOpts.WorkingDir = ReadString(Record, Idx);
5808   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5809 }
5810 
5811 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5812                                          bool Complain,
5813                                          ASTReaderListener &Listener) {
5814   HeaderSearchOptions HSOpts;
5815   unsigned Idx = 0;
5816   HSOpts.Sysroot = ReadString(Record, Idx);
5817 
5818   // Include entries.
5819   for (unsigned N = Record[Idx++]; N; --N) {
5820     std::string Path = ReadString(Record, Idx);
5821     frontend::IncludeDirGroup Group
5822       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5823     bool IsFramework = Record[Idx++];
5824     bool IgnoreSysRoot = Record[Idx++];
5825     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5826                                     IgnoreSysRoot);
5827   }
5828 
5829   // System header prefixes.
5830   for (unsigned N = Record[Idx++]; N; --N) {
5831     std::string Prefix = ReadString(Record, Idx);
5832     bool IsSystemHeader = Record[Idx++];
5833     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5834   }
5835 
5836   HSOpts.ResourceDir = ReadString(Record, Idx);
5837   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5838   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5839   HSOpts.DisableModuleHash = Record[Idx++];
5840   HSOpts.ImplicitModuleMaps = Record[Idx++];
5841   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5842   HSOpts.EnablePrebuiltImplicitModules = Record[Idx++];
5843   HSOpts.UseBuiltinIncludes = Record[Idx++];
5844   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5845   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5846   HSOpts.UseLibcxx = Record[Idx++];
5847   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5848 
5849   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5850                                           Complain);
5851 }
5852 
5853 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5854                                          bool Complain,
5855                                          ASTReaderListener &Listener,
5856                                          std::string &SuggestedPredefines) {
5857   PreprocessorOptions PPOpts;
5858   unsigned Idx = 0;
5859 
5860   // Macro definitions/undefs
5861   for (unsigned N = Record[Idx++]; N; --N) {
5862     std::string Macro = ReadString(Record, Idx);
5863     bool IsUndef = Record[Idx++];
5864     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5865   }
5866 
5867   // Includes
5868   for (unsigned N = Record[Idx++]; N; --N) {
5869     PPOpts.Includes.push_back(ReadString(Record, Idx));
5870   }
5871 
5872   // Macro Includes
5873   for (unsigned N = Record[Idx++]; N; --N) {
5874     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5875   }
5876 
5877   PPOpts.UsePredefines = Record[Idx++];
5878   PPOpts.DetailedRecord = Record[Idx++];
5879   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5880   PPOpts.ObjCXXARCStandardLibrary =
5881     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5882   SuggestedPredefines.clear();
5883   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5884                                           SuggestedPredefines);
5885 }
5886 
5887 std::pair<ModuleFile *, unsigned>
5888 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5889   GlobalPreprocessedEntityMapType::iterator
5890   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5891   assert(I != GlobalPreprocessedEntityMap.end() &&
5892          "Corrupted global preprocessed entity map");
5893   ModuleFile *M = I->second;
5894   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5895   return std::make_pair(M, LocalIndex);
5896 }
5897 
5898 llvm::iterator_range<PreprocessingRecord::iterator>
5899 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5900   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5901     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5902                                              Mod.NumPreprocessedEntities);
5903 
5904   return llvm::make_range(PreprocessingRecord::iterator(),
5905                           PreprocessingRecord::iterator());
5906 }
5907 
5908 bool ASTReader::canRecoverFromOutOfDate(StringRef ModuleFileName,
5909                                         unsigned int ClientLoadCapabilities) {
5910   return ClientLoadCapabilities & ARR_OutOfDate &&
5911          !getModuleManager().getModuleCache().isPCMFinal(ModuleFileName);
5912 }
5913 
5914 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5915 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5916   return llvm::make_range(
5917       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5918       ModuleDeclIterator(this, &Mod,
5919                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5920 }
5921 
5922 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5923   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5924   assert(I != GlobalSkippedRangeMap.end() &&
5925     "Corrupted global skipped range map");
5926   ModuleFile *M = I->second;
5927   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5928   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5929   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5930   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5931                     TranslateSourceLocation(*M, RawRange.getEnd()));
5932   assert(Range.isValid());
5933   return Range;
5934 }
5935 
5936 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5937   PreprocessedEntityID PPID = Index+1;
5938   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5939   ModuleFile &M = *PPInfo.first;
5940   unsigned LocalIndex = PPInfo.second;
5941   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5942 
5943   if (!PP.getPreprocessingRecord()) {
5944     Error("no preprocessing record");
5945     return nullptr;
5946   }
5947 
5948   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5949   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5950           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5951     Error(std::move(Err));
5952     return nullptr;
5953   }
5954 
5955   Expected<llvm::BitstreamEntry> MaybeEntry =
5956       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5957   if (!MaybeEntry) {
5958     Error(MaybeEntry.takeError());
5959     return nullptr;
5960   }
5961   llvm::BitstreamEntry Entry = MaybeEntry.get();
5962 
5963   if (Entry.Kind != llvm::BitstreamEntry::Record)
5964     return nullptr;
5965 
5966   // Read the record.
5967   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5968                     TranslateSourceLocation(M, PPOffs.getEnd()));
5969   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5970   StringRef Blob;
5971   RecordData Record;
5972   Expected<unsigned> MaybeRecType =
5973       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5974   if (!MaybeRecType) {
5975     Error(MaybeRecType.takeError());
5976     return nullptr;
5977   }
5978   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5979   case PPD_MACRO_EXPANSION: {
5980     bool isBuiltin = Record[0];
5981     IdentifierInfo *Name = nullptr;
5982     MacroDefinitionRecord *Def = nullptr;
5983     if (isBuiltin)
5984       Name = getLocalIdentifier(M, Record[1]);
5985     else {
5986       PreprocessedEntityID GlobalID =
5987           getGlobalPreprocessedEntityID(M, Record[1]);
5988       Def = cast<MacroDefinitionRecord>(
5989           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5990     }
5991 
5992     MacroExpansion *ME;
5993     if (isBuiltin)
5994       ME = new (PPRec) MacroExpansion(Name, Range);
5995     else
5996       ME = new (PPRec) MacroExpansion(Def, Range);
5997 
5998     return ME;
5999   }
6000 
6001   case PPD_MACRO_DEFINITION: {
6002     // Decode the identifier info and then check again; if the macro is
6003     // still defined and associated with the identifier,
6004     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6005     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6006 
6007     if (DeserializationListener)
6008       DeserializationListener->MacroDefinitionRead(PPID, MD);
6009 
6010     return MD;
6011   }
6012 
6013   case PPD_INCLUSION_DIRECTIVE: {
6014     const char *FullFileNameStart = Blob.data() + Record[0];
6015     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6016     const FileEntry *File = nullptr;
6017     if (!FullFileName.empty())
6018       if (auto FE = PP.getFileManager().getFile(FullFileName))
6019         File = *FE;
6020 
6021     // FIXME: Stable encoding
6022     InclusionDirective::InclusionKind Kind
6023       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6024     InclusionDirective *ID
6025       = new (PPRec) InclusionDirective(PPRec, Kind,
6026                                        StringRef(Blob.data(), Record[0]),
6027                                        Record[1], Record[3],
6028                                        File,
6029                                        Range);
6030     return ID;
6031   }
6032   }
6033 
6034   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6035 }
6036 
6037 /// Find the next module that contains entities and return the ID
6038 /// of the first entry.
6039 ///
6040 /// \param SLocMapI points at a chunk of a module that contains no
6041 /// preprocessed entities or the entities it contains are not the ones we are
6042 /// looking for.
6043 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6044                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6045   ++SLocMapI;
6046   for (GlobalSLocOffsetMapType::const_iterator
6047          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6048     ModuleFile &M = *SLocMapI->second;
6049     if (M.NumPreprocessedEntities)
6050       return M.BasePreprocessedEntityID;
6051   }
6052 
6053   return getTotalNumPreprocessedEntities();
6054 }
6055 
6056 namespace {
6057 
6058 struct PPEntityComp {
6059   const ASTReader &Reader;
6060   ModuleFile &M;
6061 
6062   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6063 
6064   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6065     SourceLocation LHS = getLoc(L);
6066     SourceLocation RHS = getLoc(R);
6067     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6068   }
6069 
6070   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6071     SourceLocation LHS = getLoc(L);
6072     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6073   }
6074 
6075   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6076     SourceLocation RHS = getLoc(R);
6077     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6078   }
6079 
6080   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6081     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6082   }
6083 };
6084 
6085 } // namespace
6086 
6087 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6088                                                        bool EndsAfter) const {
6089   if (SourceMgr.isLocalSourceLocation(Loc))
6090     return getTotalNumPreprocessedEntities();
6091 
6092   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6093       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6094   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6095          "Corrupted global sloc offset map");
6096 
6097   if (SLocMapI->second->NumPreprocessedEntities == 0)
6098     return findNextPreprocessedEntity(SLocMapI);
6099 
6100   ModuleFile &M = *SLocMapI->second;
6101 
6102   using pp_iterator = const PPEntityOffset *;
6103 
6104   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6105   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6106 
6107   size_t Count = M.NumPreprocessedEntities;
6108   size_t Half;
6109   pp_iterator First = pp_begin;
6110   pp_iterator PPI;
6111 
6112   if (EndsAfter) {
6113     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6114                            PPEntityComp(*this, M));
6115   } else {
6116     // Do a binary search manually instead of using std::lower_bound because
6117     // The end locations of entities may be unordered (when a macro expansion
6118     // is inside another macro argument), but for this case it is not important
6119     // whether we get the first macro expansion or its containing macro.
6120     while (Count > 0) {
6121       Half = Count / 2;
6122       PPI = First;
6123       std::advance(PPI, Half);
6124       if (SourceMgr.isBeforeInTranslationUnit(
6125               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6126         First = PPI;
6127         ++First;
6128         Count = Count - Half - 1;
6129       } else
6130         Count = Half;
6131     }
6132   }
6133 
6134   if (PPI == pp_end)
6135     return findNextPreprocessedEntity(SLocMapI);
6136 
6137   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6138 }
6139 
6140 /// Returns a pair of [Begin, End) indices of preallocated
6141 /// preprocessed entities that \arg Range encompasses.
6142 std::pair<unsigned, unsigned>
6143     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6144   if (Range.isInvalid())
6145     return std::make_pair(0,0);
6146   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6147 
6148   PreprocessedEntityID BeginID =
6149       findPreprocessedEntity(Range.getBegin(), false);
6150   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6151   return std::make_pair(BeginID, EndID);
6152 }
6153 
6154 /// Optionally returns true or false if the preallocated preprocessed
6155 /// entity with index \arg Index came from file \arg FID.
6156 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6157                                                              FileID FID) {
6158   if (FID.isInvalid())
6159     return false;
6160 
6161   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6162   ModuleFile &M = *PPInfo.first;
6163   unsigned LocalIndex = PPInfo.second;
6164   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6165 
6166   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6167   if (Loc.isInvalid())
6168     return false;
6169 
6170   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6171     return true;
6172   else
6173     return false;
6174 }
6175 
6176 namespace {
6177 
6178   /// Visitor used to search for information about a header file.
6179   class HeaderFileInfoVisitor {
6180     const FileEntry *FE;
6181     Optional<HeaderFileInfo> HFI;
6182 
6183   public:
6184     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6185 
6186     bool operator()(ModuleFile &M) {
6187       HeaderFileInfoLookupTable *Table
6188         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6189       if (!Table)
6190         return false;
6191 
6192       // Look in the on-disk hash table for an entry for this file name.
6193       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6194       if (Pos == Table->end())
6195         return false;
6196 
6197       HFI = *Pos;
6198       return true;
6199     }
6200 
6201     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6202   };
6203 
6204 } // namespace
6205 
6206 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6207   HeaderFileInfoVisitor Visitor(FE);
6208   ModuleMgr.visit(Visitor);
6209   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6210     return *HFI;
6211 
6212   return HeaderFileInfo();
6213 }
6214 
6215 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6216   using DiagState = DiagnosticsEngine::DiagState;
6217   SmallVector<DiagState *, 32> DiagStates;
6218 
6219   for (ModuleFile &F : ModuleMgr) {
6220     unsigned Idx = 0;
6221     auto &Record = F.PragmaDiagMappings;
6222     if (Record.empty())
6223       continue;
6224 
6225     DiagStates.clear();
6226 
6227     auto ReadDiagState =
6228         [&](const DiagState &BasedOn, SourceLocation Loc,
6229             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6230       unsigned BackrefID = Record[Idx++];
6231       if (BackrefID != 0)
6232         return DiagStates[BackrefID - 1];
6233 
6234       // A new DiagState was created here.
6235       Diag.DiagStates.push_back(BasedOn);
6236       DiagState *NewState = &Diag.DiagStates.back();
6237       DiagStates.push_back(NewState);
6238       unsigned Size = Record[Idx++];
6239       assert(Idx + Size * 2 <= Record.size() &&
6240              "Invalid data, not enough diag/map pairs");
6241       while (Size--) {
6242         unsigned DiagID = Record[Idx++];
6243         DiagnosticMapping NewMapping =
6244             DiagnosticMapping::deserialize(Record[Idx++]);
6245         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6246           continue;
6247 
6248         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6249 
6250         // If this mapping was specified as a warning but the severity was
6251         // upgraded due to diagnostic settings, simulate the current diagnostic
6252         // settings (and use a warning).
6253         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6254           NewMapping.setSeverity(diag::Severity::Warning);
6255           NewMapping.setUpgradedFromWarning(false);
6256         }
6257 
6258         Mapping = NewMapping;
6259       }
6260       return NewState;
6261     };
6262 
6263     // Read the first state.
6264     DiagState *FirstState;
6265     if (F.Kind == MK_ImplicitModule) {
6266       // Implicitly-built modules are reused with different diagnostic
6267       // settings.  Use the initial diagnostic state from Diag to simulate this
6268       // compilation's diagnostic settings.
6269       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6270       DiagStates.push_back(FirstState);
6271 
6272       // Skip the initial diagnostic state from the serialized module.
6273       assert(Record[1] == 0 &&
6274              "Invalid data, unexpected backref in initial state");
6275       Idx = 3 + Record[2] * 2;
6276       assert(Idx < Record.size() &&
6277              "Invalid data, not enough state change pairs in initial state");
6278     } else if (F.isModule()) {
6279       // For an explicit module, preserve the flags from the module build
6280       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6281       // -Wblah flags.
6282       unsigned Flags = Record[Idx++];
6283       DiagState Initial;
6284       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6285       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6286       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6287       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6288       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6289       Initial.ExtBehavior = (diag::Severity)Flags;
6290       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6291 
6292       assert(F.OriginalSourceFileID.isValid());
6293 
6294       // Set up the root buffer of the module to start with the initial
6295       // diagnostic state of the module itself, to cover files that contain no
6296       // explicit transitions (for which we did not serialize anything).
6297       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6298           .StateTransitions.push_back({FirstState, 0});
6299     } else {
6300       // For prefix ASTs, start with whatever the user configured on the
6301       // command line.
6302       Idx++; // Skip flags.
6303       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6304                                  SourceLocation(), false);
6305     }
6306 
6307     // Read the state transitions.
6308     unsigned NumLocations = Record[Idx++];
6309     while (NumLocations--) {
6310       assert(Idx < Record.size() &&
6311              "Invalid data, missing pragma diagnostic states");
6312       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6313       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6314       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6315       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6316       unsigned Transitions = Record[Idx++];
6317 
6318       // Note that we don't need to set up Parent/ParentOffset here, because
6319       // we won't be changing the diagnostic state within imported FileIDs
6320       // (other than perhaps appending to the main source file, which has no
6321       // parent).
6322       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6323       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6324       for (unsigned I = 0; I != Transitions; ++I) {
6325         unsigned Offset = Record[Idx++];
6326         auto *State =
6327             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6328         F.StateTransitions.push_back({State, Offset});
6329       }
6330     }
6331 
6332     // Read the final state.
6333     assert(Idx < Record.size() &&
6334            "Invalid data, missing final pragma diagnostic state");
6335     SourceLocation CurStateLoc =
6336         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6337     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6338 
6339     if (!F.isModule()) {
6340       Diag.DiagStatesByLoc.CurDiagState = CurState;
6341       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6342 
6343       // Preserve the property that the imaginary root file describes the
6344       // current state.
6345       FileID NullFile;
6346       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6347       if (T.empty())
6348         T.push_back({CurState, 0});
6349       else
6350         T[0].State = CurState;
6351     }
6352 
6353     // Don't try to read these mappings again.
6354     Record.clear();
6355   }
6356 }
6357 
6358 /// Get the correct cursor and offset for loading a type.
6359 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6360   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6361   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6362   ModuleFile *M = I->second;
6363   return RecordLocation(
6364       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6365              M->DeclsBlockStartOffset);
6366 }
6367 
6368 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6369   switch (code) {
6370 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6371   case TYPE_##CODE_ID: return Type::CLASS_ID;
6372 #include "clang/Serialization/TypeBitCodes.def"
6373   default: return llvm::None;
6374   }
6375 }
6376 
6377 /// Read and return the type with the given index..
6378 ///
6379 /// The index is the type ID, shifted and minus the number of predefs. This
6380 /// routine actually reads the record corresponding to the type at the given
6381 /// location. It is a helper routine for GetType, which deals with reading type
6382 /// IDs.
6383 QualType ASTReader::readTypeRecord(unsigned Index) {
6384   assert(ContextObj && "reading type with no AST context");
6385   ASTContext &Context = *ContextObj;
6386   RecordLocation Loc = TypeCursorForIndex(Index);
6387   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6388 
6389   // Keep track of where we are in the stream, then jump back there
6390   // after reading this type.
6391   SavedStreamPosition SavedPosition(DeclsCursor);
6392 
6393   ReadingKindTracker ReadingKind(Read_Type, *this);
6394 
6395   // Note that we are loading a type record.
6396   Deserializing AType(this);
6397 
6398   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6399     Error(std::move(Err));
6400     return QualType();
6401   }
6402   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6403   if (!RawCode) {
6404     Error(RawCode.takeError());
6405     return QualType();
6406   }
6407 
6408   ASTRecordReader Record(*this, *Loc.F);
6409   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6410   if (!Code) {
6411     Error(Code.takeError());
6412     return QualType();
6413   }
6414   if (Code.get() == TYPE_EXT_QUAL) {
6415     QualType baseType = Record.readQualType();
6416     Qualifiers quals = Record.readQualifiers();
6417     return Context.getQualifiedType(baseType, quals);
6418   }
6419 
6420   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6421   if (!maybeClass) {
6422     Error("Unexpected code for type");
6423     return QualType();
6424   }
6425 
6426   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6427   return TypeReader.read(*maybeClass);
6428 }
6429 
6430 namespace clang {
6431 
6432 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6433   ASTRecordReader &Reader;
6434 
6435   SourceLocation readSourceLocation() {
6436     return Reader.readSourceLocation();
6437   }
6438 
6439   TypeSourceInfo *GetTypeSourceInfo() {
6440     return Reader.readTypeSourceInfo();
6441   }
6442 
6443   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6444     return Reader.readNestedNameSpecifierLoc();
6445   }
6446 
6447   Attr *ReadAttr() {
6448     return Reader.readAttr();
6449   }
6450 
6451 public:
6452   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6453 
6454   // We want compile-time assurance that we've enumerated all of
6455   // these, so unfortunately we have to declare them first, then
6456   // define them out-of-line.
6457 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6458 #define TYPELOC(CLASS, PARENT) \
6459   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6460 #include "clang/AST/TypeLocNodes.def"
6461 
6462   void VisitFunctionTypeLoc(FunctionTypeLoc);
6463   void VisitArrayTypeLoc(ArrayTypeLoc);
6464 };
6465 
6466 } // namespace clang
6467 
6468 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6469   // nothing to do
6470 }
6471 
6472 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6473   TL.setBuiltinLoc(readSourceLocation());
6474   if (TL.needsExtraLocalData()) {
6475     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6476     TL.setWrittenSignSpec(static_cast<TypeSpecifierSign>(Reader.readInt()));
6477     TL.setWrittenWidthSpec(static_cast<TypeSpecifierWidth>(Reader.readInt()));
6478     TL.setModeAttr(Reader.readInt());
6479   }
6480 }
6481 
6482 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6483   TL.setNameLoc(readSourceLocation());
6484 }
6485 
6486 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6487   TL.setStarLoc(readSourceLocation());
6488 }
6489 
6490 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6491   // nothing to do
6492 }
6493 
6494 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6495   // nothing to do
6496 }
6497 
6498 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6499   TL.setExpansionLoc(readSourceLocation());
6500 }
6501 
6502 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6503   TL.setCaretLoc(readSourceLocation());
6504 }
6505 
6506 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6507   TL.setAmpLoc(readSourceLocation());
6508 }
6509 
6510 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6511   TL.setAmpAmpLoc(readSourceLocation());
6512 }
6513 
6514 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6515   TL.setStarLoc(readSourceLocation());
6516   TL.setClassTInfo(GetTypeSourceInfo());
6517 }
6518 
6519 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6520   TL.setLBracketLoc(readSourceLocation());
6521   TL.setRBracketLoc(readSourceLocation());
6522   if (Reader.readBool())
6523     TL.setSizeExpr(Reader.readExpr());
6524   else
6525     TL.setSizeExpr(nullptr);
6526 }
6527 
6528 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6529   VisitArrayTypeLoc(TL);
6530 }
6531 
6532 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6533   VisitArrayTypeLoc(TL);
6534 }
6535 
6536 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6537   VisitArrayTypeLoc(TL);
6538 }
6539 
6540 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6541                                             DependentSizedArrayTypeLoc TL) {
6542   VisitArrayTypeLoc(TL);
6543 }
6544 
6545 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6546     DependentAddressSpaceTypeLoc TL) {
6547 
6548     TL.setAttrNameLoc(readSourceLocation());
6549     TL.setAttrOperandParensRange(Reader.readSourceRange());
6550     TL.setAttrExprOperand(Reader.readExpr());
6551 }
6552 
6553 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6554                                         DependentSizedExtVectorTypeLoc TL) {
6555   TL.setNameLoc(readSourceLocation());
6556 }
6557 
6558 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6559   TL.setNameLoc(readSourceLocation());
6560 }
6561 
6562 void TypeLocReader::VisitDependentVectorTypeLoc(
6563     DependentVectorTypeLoc TL) {
6564   TL.setNameLoc(readSourceLocation());
6565 }
6566 
6567 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6568   TL.setNameLoc(readSourceLocation());
6569 }
6570 
6571 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6572   TL.setAttrNameLoc(readSourceLocation());
6573   TL.setAttrOperandParensRange(Reader.readSourceRange());
6574   TL.setAttrRowOperand(Reader.readExpr());
6575   TL.setAttrColumnOperand(Reader.readExpr());
6576 }
6577 
6578 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6579     DependentSizedMatrixTypeLoc TL) {
6580   TL.setAttrNameLoc(readSourceLocation());
6581   TL.setAttrOperandParensRange(Reader.readSourceRange());
6582   TL.setAttrRowOperand(Reader.readExpr());
6583   TL.setAttrColumnOperand(Reader.readExpr());
6584 }
6585 
6586 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6587   TL.setLocalRangeBegin(readSourceLocation());
6588   TL.setLParenLoc(readSourceLocation());
6589   TL.setRParenLoc(readSourceLocation());
6590   TL.setExceptionSpecRange(Reader.readSourceRange());
6591   TL.setLocalRangeEnd(readSourceLocation());
6592   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6593     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6594   }
6595 }
6596 
6597 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6598   VisitFunctionTypeLoc(TL);
6599 }
6600 
6601 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6602   VisitFunctionTypeLoc(TL);
6603 }
6604 
6605 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6606   TL.setNameLoc(readSourceLocation());
6607 }
6608 
6609 void TypeLocReader::VisitUsingTypeLoc(UsingTypeLoc TL) {
6610   TL.setNameLoc(readSourceLocation());
6611 }
6612 
6613 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6614   TL.setNameLoc(readSourceLocation());
6615 }
6616 
6617 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6618   TL.setTypeofLoc(readSourceLocation());
6619   TL.setLParenLoc(readSourceLocation());
6620   TL.setRParenLoc(readSourceLocation());
6621 }
6622 
6623 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6624   TL.setTypeofLoc(readSourceLocation());
6625   TL.setLParenLoc(readSourceLocation());
6626   TL.setRParenLoc(readSourceLocation());
6627   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6628 }
6629 
6630 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6631   TL.setDecltypeLoc(readSourceLocation());
6632   TL.setRParenLoc(readSourceLocation());
6633 }
6634 
6635 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6636   TL.setKWLoc(readSourceLocation());
6637   TL.setLParenLoc(readSourceLocation());
6638   TL.setRParenLoc(readSourceLocation());
6639   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6640 }
6641 
6642 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6643   TL.setNameLoc(readSourceLocation());
6644   if (Reader.readBool()) {
6645     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6646     TL.setTemplateKWLoc(readSourceLocation());
6647     TL.setConceptNameLoc(readSourceLocation());
6648     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6649     TL.setLAngleLoc(readSourceLocation());
6650     TL.setRAngleLoc(readSourceLocation());
6651     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6652       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6653                               TL.getTypePtr()->getArg(i).getKind()));
6654   }
6655   if (Reader.readBool())
6656     TL.setRParenLoc(readSourceLocation());
6657 }
6658 
6659 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6660     DeducedTemplateSpecializationTypeLoc TL) {
6661   TL.setTemplateNameLoc(readSourceLocation());
6662 }
6663 
6664 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6665   TL.setNameLoc(readSourceLocation());
6666 }
6667 
6668 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6669   TL.setNameLoc(readSourceLocation());
6670 }
6671 
6672 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6673   TL.setAttr(ReadAttr());
6674 }
6675 
6676 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6677   TL.setNameLoc(readSourceLocation());
6678 }
6679 
6680 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6681                                             SubstTemplateTypeParmTypeLoc TL) {
6682   TL.setNameLoc(readSourceLocation());
6683 }
6684 
6685 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6686                                           SubstTemplateTypeParmPackTypeLoc TL) {
6687   TL.setNameLoc(readSourceLocation());
6688 }
6689 
6690 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6691                                            TemplateSpecializationTypeLoc TL) {
6692   TL.setTemplateKeywordLoc(readSourceLocation());
6693   TL.setTemplateNameLoc(readSourceLocation());
6694   TL.setLAngleLoc(readSourceLocation());
6695   TL.setRAngleLoc(readSourceLocation());
6696   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6697     TL.setArgLocInfo(
6698         i,
6699         Reader.readTemplateArgumentLocInfo(
6700           TL.getTypePtr()->getArg(i).getKind()));
6701 }
6702 
6703 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6704   TL.setLParenLoc(readSourceLocation());
6705   TL.setRParenLoc(readSourceLocation());
6706 }
6707 
6708 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6709   TL.setElaboratedKeywordLoc(readSourceLocation());
6710   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6711 }
6712 
6713 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6714   TL.setNameLoc(readSourceLocation());
6715 }
6716 
6717 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6718   TL.setElaboratedKeywordLoc(readSourceLocation());
6719   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6720   TL.setNameLoc(readSourceLocation());
6721 }
6722 
6723 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6724        DependentTemplateSpecializationTypeLoc TL) {
6725   TL.setElaboratedKeywordLoc(readSourceLocation());
6726   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6727   TL.setTemplateKeywordLoc(readSourceLocation());
6728   TL.setTemplateNameLoc(readSourceLocation());
6729   TL.setLAngleLoc(readSourceLocation());
6730   TL.setRAngleLoc(readSourceLocation());
6731   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6732     TL.setArgLocInfo(
6733         I,
6734         Reader.readTemplateArgumentLocInfo(
6735             TL.getTypePtr()->getArg(I).getKind()));
6736 }
6737 
6738 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6739   TL.setEllipsisLoc(readSourceLocation());
6740 }
6741 
6742 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6743   TL.setNameLoc(readSourceLocation());
6744 }
6745 
6746 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6747   if (TL.getNumProtocols()) {
6748     TL.setProtocolLAngleLoc(readSourceLocation());
6749     TL.setProtocolRAngleLoc(readSourceLocation());
6750   }
6751   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6752     TL.setProtocolLoc(i, readSourceLocation());
6753 }
6754 
6755 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6756   TL.setHasBaseTypeAsWritten(Reader.readBool());
6757   TL.setTypeArgsLAngleLoc(readSourceLocation());
6758   TL.setTypeArgsRAngleLoc(readSourceLocation());
6759   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6760     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6761   TL.setProtocolLAngleLoc(readSourceLocation());
6762   TL.setProtocolRAngleLoc(readSourceLocation());
6763   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6764     TL.setProtocolLoc(i, readSourceLocation());
6765 }
6766 
6767 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6768   TL.setStarLoc(readSourceLocation());
6769 }
6770 
6771 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6772   TL.setKWLoc(readSourceLocation());
6773   TL.setLParenLoc(readSourceLocation());
6774   TL.setRParenLoc(readSourceLocation());
6775 }
6776 
6777 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6778   TL.setKWLoc(readSourceLocation());
6779 }
6780 
6781 void TypeLocReader::VisitBitIntTypeLoc(clang::BitIntTypeLoc TL) {
6782   TL.setNameLoc(readSourceLocation());
6783 }
6784 void TypeLocReader::VisitDependentBitIntTypeLoc(
6785     clang::DependentBitIntTypeLoc TL) {
6786   TL.setNameLoc(readSourceLocation());
6787 }
6788 
6789 
6790 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6791   TypeLocReader TLR(*this);
6792   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6793     TLR.Visit(TL);
6794 }
6795 
6796 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6797   QualType InfoTy = readType();
6798   if (InfoTy.isNull())
6799     return nullptr;
6800 
6801   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6802   readTypeLoc(TInfo->getTypeLoc());
6803   return TInfo;
6804 }
6805 
6806 QualType ASTReader::GetType(TypeID ID) {
6807   assert(ContextObj && "reading type with no AST context");
6808   ASTContext &Context = *ContextObj;
6809 
6810   unsigned FastQuals = ID & Qualifiers::FastMask;
6811   unsigned Index = ID >> Qualifiers::FastWidth;
6812 
6813   if (Index < NUM_PREDEF_TYPE_IDS) {
6814     QualType T;
6815     switch ((PredefinedTypeIDs)Index) {
6816     case PREDEF_TYPE_NULL_ID:
6817       return QualType();
6818     case PREDEF_TYPE_VOID_ID:
6819       T = Context.VoidTy;
6820       break;
6821     case PREDEF_TYPE_BOOL_ID:
6822       T = Context.BoolTy;
6823       break;
6824     case PREDEF_TYPE_CHAR_U_ID:
6825     case PREDEF_TYPE_CHAR_S_ID:
6826       // FIXME: Check that the signedness of CharTy is correct!
6827       T = Context.CharTy;
6828       break;
6829     case PREDEF_TYPE_UCHAR_ID:
6830       T = Context.UnsignedCharTy;
6831       break;
6832     case PREDEF_TYPE_USHORT_ID:
6833       T = Context.UnsignedShortTy;
6834       break;
6835     case PREDEF_TYPE_UINT_ID:
6836       T = Context.UnsignedIntTy;
6837       break;
6838     case PREDEF_TYPE_ULONG_ID:
6839       T = Context.UnsignedLongTy;
6840       break;
6841     case PREDEF_TYPE_ULONGLONG_ID:
6842       T = Context.UnsignedLongLongTy;
6843       break;
6844     case PREDEF_TYPE_UINT128_ID:
6845       T = Context.UnsignedInt128Ty;
6846       break;
6847     case PREDEF_TYPE_SCHAR_ID:
6848       T = Context.SignedCharTy;
6849       break;
6850     case PREDEF_TYPE_WCHAR_ID:
6851       T = Context.WCharTy;
6852       break;
6853     case PREDEF_TYPE_SHORT_ID:
6854       T = Context.ShortTy;
6855       break;
6856     case PREDEF_TYPE_INT_ID:
6857       T = Context.IntTy;
6858       break;
6859     case PREDEF_TYPE_LONG_ID:
6860       T = Context.LongTy;
6861       break;
6862     case PREDEF_TYPE_LONGLONG_ID:
6863       T = Context.LongLongTy;
6864       break;
6865     case PREDEF_TYPE_INT128_ID:
6866       T = Context.Int128Ty;
6867       break;
6868     case PREDEF_TYPE_BFLOAT16_ID:
6869       T = Context.BFloat16Ty;
6870       break;
6871     case PREDEF_TYPE_HALF_ID:
6872       T = Context.HalfTy;
6873       break;
6874     case PREDEF_TYPE_FLOAT_ID:
6875       T = Context.FloatTy;
6876       break;
6877     case PREDEF_TYPE_DOUBLE_ID:
6878       T = Context.DoubleTy;
6879       break;
6880     case PREDEF_TYPE_LONGDOUBLE_ID:
6881       T = Context.LongDoubleTy;
6882       break;
6883     case PREDEF_TYPE_SHORT_ACCUM_ID:
6884       T = Context.ShortAccumTy;
6885       break;
6886     case PREDEF_TYPE_ACCUM_ID:
6887       T = Context.AccumTy;
6888       break;
6889     case PREDEF_TYPE_LONG_ACCUM_ID:
6890       T = Context.LongAccumTy;
6891       break;
6892     case PREDEF_TYPE_USHORT_ACCUM_ID:
6893       T = Context.UnsignedShortAccumTy;
6894       break;
6895     case PREDEF_TYPE_UACCUM_ID:
6896       T = Context.UnsignedAccumTy;
6897       break;
6898     case PREDEF_TYPE_ULONG_ACCUM_ID:
6899       T = Context.UnsignedLongAccumTy;
6900       break;
6901     case PREDEF_TYPE_SHORT_FRACT_ID:
6902       T = Context.ShortFractTy;
6903       break;
6904     case PREDEF_TYPE_FRACT_ID:
6905       T = Context.FractTy;
6906       break;
6907     case PREDEF_TYPE_LONG_FRACT_ID:
6908       T = Context.LongFractTy;
6909       break;
6910     case PREDEF_TYPE_USHORT_FRACT_ID:
6911       T = Context.UnsignedShortFractTy;
6912       break;
6913     case PREDEF_TYPE_UFRACT_ID:
6914       T = Context.UnsignedFractTy;
6915       break;
6916     case PREDEF_TYPE_ULONG_FRACT_ID:
6917       T = Context.UnsignedLongFractTy;
6918       break;
6919     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6920       T = Context.SatShortAccumTy;
6921       break;
6922     case PREDEF_TYPE_SAT_ACCUM_ID:
6923       T = Context.SatAccumTy;
6924       break;
6925     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6926       T = Context.SatLongAccumTy;
6927       break;
6928     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6929       T = Context.SatUnsignedShortAccumTy;
6930       break;
6931     case PREDEF_TYPE_SAT_UACCUM_ID:
6932       T = Context.SatUnsignedAccumTy;
6933       break;
6934     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6935       T = Context.SatUnsignedLongAccumTy;
6936       break;
6937     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6938       T = Context.SatShortFractTy;
6939       break;
6940     case PREDEF_TYPE_SAT_FRACT_ID:
6941       T = Context.SatFractTy;
6942       break;
6943     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6944       T = Context.SatLongFractTy;
6945       break;
6946     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6947       T = Context.SatUnsignedShortFractTy;
6948       break;
6949     case PREDEF_TYPE_SAT_UFRACT_ID:
6950       T = Context.SatUnsignedFractTy;
6951       break;
6952     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6953       T = Context.SatUnsignedLongFractTy;
6954       break;
6955     case PREDEF_TYPE_FLOAT16_ID:
6956       T = Context.Float16Ty;
6957       break;
6958     case PREDEF_TYPE_FLOAT128_ID:
6959       T = Context.Float128Ty;
6960       break;
6961     case PREDEF_TYPE_IBM128_ID:
6962       T = Context.Ibm128Ty;
6963       break;
6964     case PREDEF_TYPE_OVERLOAD_ID:
6965       T = Context.OverloadTy;
6966       break;
6967     case PREDEF_TYPE_BOUND_MEMBER:
6968       T = Context.BoundMemberTy;
6969       break;
6970     case PREDEF_TYPE_PSEUDO_OBJECT:
6971       T = Context.PseudoObjectTy;
6972       break;
6973     case PREDEF_TYPE_DEPENDENT_ID:
6974       T = Context.DependentTy;
6975       break;
6976     case PREDEF_TYPE_UNKNOWN_ANY:
6977       T = Context.UnknownAnyTy;
6978       break;
6979     case PREDEF_TYPE_NULLPTR_ID:
6980       T = Context.NullPtrTy;
6981       break;
6982     case PREDEF_TYPE_CHAR8_ID:
6983       T = Context.Char8Ty;
6984       break;
6985     case PREDEF_TYPE_CHAR16_ID:
6986       T = Context.Char16Ty;
6987       break;
6988     case PREDEF_TYPE_CHAR32_ID:
6989       T = Context.Char32Ty;
6990       break;
6991     case PREDEF_TYPE_OBJC_ID:
6992       T = Context.ObjCBuiltinIdTy;
6993       break;
6994     case PREDEF_TYPE_OBJC_CLASS:
6995       T = Context.ObjCBuiltinClassTy;
6996       break;
6997     case PREDEF_TYPE_OBJC_SEL:
6998       T = Context.ObjCBuiltinSelTy;
6999       break;
7000 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
7001     case PREDEF_TYPE_##Id##_ID: \
7002       T = Context.SingletonId; \
7003       break;
7004 #include "clang/Basic/OpenCLImageTypes.def"
7005 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7006     case PREDEF_TYPE_##Id##_ID: \
7007       T = Context.Id##Ty; \
7008       break;
7009 #include "clang/Basic/OpenCLExtensionTypes.def"
7010     case PREDEF_TYPE_SAMPLER_ID:
7011       T = Context.OCLSamplerTy;
7012       break;
7013     case PREDEF_TYPE_EVENT_ID:
7014       T = Context.OCLEventTy;
7015       break;
7016     case PREDEF_TYPE_CLK_EVENT_ID:
7017       T = Context.OCLClkEventTy;
7018       break;
7019     case PREDEF_TYPE_QUEUE_ID:
7020       T = Context.OCLQueueTy;
7021       break;
7022     case PREDEF_TYPE_RESERVE_ID_ID:
7023       T = Context.OCLReserveIDTy;
7024       break;
7025     case PREDEF_TYPE_AUTO_DEDUCT:
7026       T = Context.getAutoDeductType();
7027       break;
7028     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7029       T = Context.getAutoRRefDeductType();
7030       break;
7031     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7032       T = Context.ARCUnbridgedCastTy;
7033       break;
7034     case PREDEF_TYPE_BUILTIN_FN:
7035       T = Context.BuiltinFnTy;
7036       break;
7037     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7038       T = Context.IncompleteMatrixIdxTy;
7039       break;
7040     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7041       T = Context.OMPArraySectionTy;
7042       break;
7043     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7044       T = Context.OMPArraySectionTy;
7045       break;
7046     case PREDEF_TYPE_OMP_ITERATOR:
7047       T = Context.OMPIteratorTy;
7048       break;
7049 #define SVE_TYPE(Name, Id, SingletonId) \
7050     case PREDEF_TYPE_##Id##_ID: \
7051       T = Context.SingletonId; \
7052       break;
7053 #include "clang/Basic/AArch64SVEACLETypes.def"
7054 #define PPC_VECTOR_TYPE(Name, Id, Size) \
7055     case PREDEF_TYPE_##Id##_ID: \
7056       T = Context.Id##Ty; \
7057       break;
7058 #include "clang/Basic/PPCTypes.def"
7059 #define RVV_TYPE(Name, Id, SingletonId) \
7060     case PREDEF_TYPE_##Id##_ID: \
7061       T = Context.SingletonId; \
7062       break;
7063 #include "clang/Basic/RISCVVTypes.def"
7064     }
7065 
7066     assert(!T.isNull() && "Unknown predefined type");
7067     return T.withFastQualifiers(FastQuals);
7068   }
7069 
7070   Index -= NUM_PREDEF_TYPE_IDS;
7071   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7072   if (TypesLoaded[Index].isNull()) {
7073     TypesLoaded[Index] = readTypeRecord(Index);
7074     if (TypesLoaded[Index].isNull())
7075       return QualType();
7076 
7077     TypesLoaded[Index]->setFromAST();
7078     if (DeserializationListener)
7079       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7080                                         TypesLoaded[Index]);
7081   }
7082 
7083   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7084 }
7085 
7086 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7087   return GetType(getGlobalTypeID(F, LocalID));
7088 }
7089 
7090 serialization::TypeID
7091 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7092   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7093   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7094 
7095   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7096     return LocalID;
7097 
7098   if (!F.ModuleOffsetMap.empty())
7099     ReadModuleOffsetMap(F);
7100 
7101   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7102     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7103   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7104 
7105   unsigned GlobalIndex = LocalIndex + I->second;
7106   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7107 }
7108 
7109 TemplateArgumentLocInfo
7110 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7111   switch (Kind) {
7112   case TemplateArgument::Expression:
7113     return readExpr();
7114   case TemplateArgument::Type:
7115     return readTypeSourceInfo();
7116   case TemplateArgument::Template: {
7117     NestedNameSpecifierLoc QualifierLoc =
7118       readNestedNameSpecifierLoc();
7119     SourceLocation TemplateNameLoc = readSourceLocation();
7120     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7121                                    TemplateNameLoc, SourceLocation());
7122   }
7123   case TemplateArgument::TemplateExpansion: {
7124     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7125     SourceLocation TemplateNameLoc = readSourceLocation();
7126     SourceLocation EllipsisLoc = readSourceLocation();
7127     return TemplateArgumentLocInfo(getASTContext(), QualifierLoc,
7128                                    TemplateNameLoc, EllipsisLoc);
7129   }
7130   case TemplateArgument::Null:
7131   case TemplateArgument::Integral:
7132   case TemplateArgument::Declaration:
7133   case TemplateArgument::NullPtr:
7134   case TemplateArgument::Pack:
7135     // FIXME: Is this right?
7136     return TemplateArgumentLocInfo();
7137   }
7138   llvm_unreachable("unexpected template argument loc");
7139 }
7140 
7141 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7142   TemplateArgument Arg = readTemplateArgument();
7143 
7144   if (Arg.getKind() == TemplateArgument::Expression) {
7145     if (readBool()) // bool InfoHasSameExpr.
7146       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7147   }
7148   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7149 }
7150 
7151 const ASTTemplateArgumentListInfo *
7152 ASTRecordReader::readASTTemplateArgumentListInfo() {
7153   SourceLocation LAngleLoc = readSourceLocation();
7154   SourceLocation RAngleLoc = readSourceLocation();
7155   unsigned NumArgsAsWritten = readInt();
7156   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7157   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7158     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7159   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7160 }
7161 
7162 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7163   return GetDecl(ID);
7164 }
7165 
7166 void ASTReader::CompleteRedeclChain(const Decl *D) {
7167   if (NumCurrentElementsDeserializing) {
7168     // We arrange to not care about the complete redeclaration chain while we're
7169     // deserializing. Just remember that the AST has marked this one as complete
7170     // but that it's not actually complete yet, so we know we still need to
7171     // complete it later.
7172     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7173     return;
7174   }
7175 
7176   if (!D->getDeclContext()) {
7177     assert(isa<TranslationUnitDecl>(D) && "Not a TU?");
7178     return;
7179   }
7180 
7181   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7182 
7183   // If this is a named declaration, complete it by looking it up
7184   // within its context.
7185   //
7186   // FIXME: Merging a function definition should merge
7187   // all mergeable entities within it.
7188   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7189       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7190     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7191       if (!getContext().getLangOpts().CPlusPlus &&
7192           isa<TranslationUnitDecl>(DC)) {
7193         // Outside of C++, we don't have a lookup table for the TU, so update
7194         // the identifier instead. (For C++ modules, we don't store decls
7195         // in the serialized identifier table, so we do the lookup in the TU.)
7196         auto *II = Name.getAsIdentifierInfo();
7197         assert(II && "non-identifier name in C?");
7198         if (II->isOutOfDate())
7199           updateOutOfDateIdentifier(*II);
7200       } else
7201         DC->lookup(Name);
7202     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7203       // Find all declarations of this kind from the relevant context.
7204       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7205         auto *DC = cast<DeclContext>(DCDecl);
7206         SmallVector<Decl*, 8> Decls;
7207         FindExternalLexicalDecls(
7208             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7209       }
7210     }
7211   }
7212 
7213   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7214     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7215   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7216     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7217   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7218     if (auto *Template = FD->getPrimaryTemplate())
7219       Template->LoadLazySpecializations();
7220   }
7221 }
7222 
7223 CXXCtorInitializer **
7224 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7225   RecordLocation Loc = getLocalBitOffset(Offset);
7226   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7227   SavedStreamPosition SavedPosition(Cursor);
7228   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7229     Error(std::move(Err));
7230     return nullptr;
7231   }
7232   ReadingKindTracker ReadingKind(Read_Decl, *this);
7233 
7234   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7235   if (!MaybeCode) {
7236     Error(MaybeCode.takeError());
7237     return nullptr;
7238   }
7239   unsigned Code = MaybeCode.get();
7240 
7241   ASTRecordReader Record(*this, *Loc.F);
7242   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7243   if (!MaybeRecCode) {
7244     Error(MaybeRecCode.takeError());
7245     return nullptr;
7246   }
7247   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7248     Error("malformed AST file: missing C++ ctor initializers");
7249     return nullptr;
7250   }
7251 
7252   return Record.readCXXCtorInitializers();
7253 }
7254 
7255 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7256   assert(ContextObj && "reading base specifiers with no AST context");
7257   ASTContext &Context = *ContextObj;
7258 
7259   RecordLocation Loc = getLocalBitOffset(Offset);
7260   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7261   SavedStreamPosition SavedPosition(Cursor);
7262   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7263     Error(std::move(Err));
7264     return nullptr;
7265   }
7266   ReadingKindTracker ReadingKind(Read_Decl, *this);
7267 
7268   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7269   if (!MaybeCode) {
7270     Error(MaybeCode.takeError());
7271     return nullptr;
7272   }
7273   unsigned Code = MaybeCode.get();
7274 
7275   ASTRecordReader Record(*this, *Loc.F);
7276   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7277   if (!MaybeRecCode) {
7278     Error(MaybeCode.takeError());
7279     return nullptr;
7280   }
7281   unsigned RecCode = MaybeRecCode.get();
7282 
7283   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7284     Error("malformed AST file: missing C++ base specifiers");
7285     return nullptr;
7286   }
7287 
7288   unsigned NumBases = Record.readInt();
7289   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7290   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7291   for (unsigned I = 0; I != NumBases; ++I)
7292     Bases[I] = Record.readCXXBaseSpecifier();
7293   return Bases;
7294 }
7295 
7296 serialization::DeclID
7297 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7298   if (LocalID < NUM_PREDEF_DECL_IDS)
7299     return LocalID;
7300 
7301   if (!F.ModuleOffsetMap.empty())
7302     ReadModuleOffsetMap(F);
7303 
7304   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7305     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7306   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7307 
7308   return LocalID + I->second;
7309 }
7310 
7311 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7312                                    ModuleFile &M) const {
7313   // Predefined decls aren't from any module.
7314   if (ID < NUM_PREDEF_DECL_IDS)
7315     return false;
7316 
7317   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7318          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7319 }
7320 
7321 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7322   if (!D->isFromASTFile())
7323     return nullptr;
7324   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7325   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7326   return I->second;
7327 }
7328 
7329 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7330   if (ID < NUM_PREDEF_DECL_IDS)
7331     return SourceLocation();
7332 
7333   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7334 
7335   if (Index > DeclsLoaded.size()) {
7336     Error("declaration ID out-of-range for AST file");
7337     return SourceLocation();
7338   }
7339 
7340   if (Decl *D = DeclsLoaded[Index])
7341     return D->getLocation();
7342 
7343   SourceLocation Loc;
7344   DeclCursorForID(ID, Loc);
7345   return Loc;
7346 }
7347 
7348 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7349   switch (ID) {
7350   case PREDEF_DECL_NULL_ID:
7351     return nullptr;
7352 
7353   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7354     return Context.getTranslationUnitDecl();
7355 
7356   case PREDEF_DECL_OBJC_ID_ID:
7357     return Context.getObjCIdDecl();
7358 
7359   case PREDEF_DECL_OBJC_SEL_ID:
7360     return Context.getObjCSelDecl();
7361 
7362   case PREDEF_DECL_OBJC_CLASS_ID:
7363     return Context.getObjCClassDecl();
7364 
7365   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7366     return Context.getObjCProtocolDecl();
7367 
7368   case PREDEF_DECL_INT_128_ID:
7369     return Context.getInt128Decl();
7370 
7371   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7372     return Context.getUInt128Decl();
7373 
7374   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7375     return Context.getObjCInstanceTypeDecl();
7376 
7377   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7378     return Context.getBuiltinVaListDecl();
7379 
7380   case PREDEF_DECL_VA_LIST_TAG:
7381     return Context.getVaListTagDecl();
7382 
7383   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7384     return Context.getBuiltinMSVaListDecl();
7385 
7386   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7387     return Context.getMSGuidTagDecl();
7388 
7389   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7390     return Context.getExternCContextDecl();
7391 
7392   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7393     return Context.getMakeIntegerSeqDecl();
7394 
7395   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7396     return Context.getCFConstantStringDecl();
7397 
7398   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7399     return Context.getCFConstantStringTagDecl();
7400 
7401   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7402     return Context.getTypePackElementDecl();
7403   }
7404   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7405 }
7406 
7407 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7408   assert(ContextObj && "reading decl with no AST context");
7409   if (ID < NUM_PREDEF_DECL_IDS) {
7410     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7411     if (D) {
7412       // Track that we have merged the declaration with ID \p ID into the
7413       // pre-existing predefined declaration \p D.
7414       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7415       if (Merged.empty())
7416         Merged.push_back(ID);
7417     }
7418     return D;
7419   }
7420 
7421   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7422 
7423   if (Index >= DeclsLoaded.size()) {
7424     assert(0 && "declaration ID out-of-range for AST file");
7425     Error("declaration ID out-of-range for AST file");
7426     return nullptr;
7427   }
7428 
7429   return DeclsLoaded[Index];
7430 }
7431 
7432 Decl *ASTReader::GetDecl(DeclID ID) {
7433   if (ID < NUM_PREDEF_DECL_IDS)
7434     return GetExistingDecl(ID);
7435 
7436   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7437 
7438   if (Index >= DeclsLoaded.size()) {
7439     assert(0 && "declaration ID out-of-range for AST file");
7440     Error("declaration ID out-of-range for AST file");
7441     return nullptr;
7442   }
7443 
7444   if (!DeclsLoaded[Index]) {
7445     ReadDeclRecord(ID);
7446     if (DeserializationListener)
7447       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7448   }
7449 
7450   return DeclsLoaded[Index];
7451 }
7452 
7453 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7454                                                   DeclID GlobalID) {
7455   if (GlobalID < NUM_PREDEF_DECL_IDS)
7456     return GlobalID;
7457 
7458   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7459   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7460   ModuleFile *Owner = I->second;
7461 
7462   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7463     = M.GlobalToLocalDeclIDs.find(Owner);
7464   if (Pos == M.GlobalToLocalDeclIDs.end())
7465     return 0;
7466 
7467   return GlobalID - Owner->BaseDeclID + Pos->second;
7468 }
7469 
7470 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7471                                             const RecordData &Record,
7472                                             unsigned &Idx) {
7473   if (Idx >= Record.size()) {
7474     Error("Corrupted AST file");
7475     return 0;
7476   }
7477 
7478   return getGlobalDeclID(F, Record[Idx++]);
7479 }
7480 
7481 /// Resolve the offset of a statement into a statement.
7482 ///
7483 /// This operation will read a new statement from the external
7484 /// source each time it is called, and is meant to be used via a
7485 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7486 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7487   // Switch case IDs are per Decl.
7488   ClearSwitchCaseIDs();
7489 
7490   // Offset here is a global offset across the entire chain.
7491   RecordLocation Loc = getLocalBitOffset(Offset);
7492   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7493     Error(std::move(Err));
7494     return nullptr;
7495   }
7496   assert(NumCurrentElementsDeserializing == 0 &&
7497          "should not be called while already deserializing");
7498   Deserializing D(this);
7499   return ReadStmtFromStream(*Loc.F);
7500 }
7501 
7502 void ASTReader::FindExternalLexicalDecls(
7503     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7504     SmallVectorImpl<Decl *> &Decls) {
7505   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7506 
7507   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7508     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7509     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7510       auto K = (Decl::Kind)+LexicalDecls[I];
7511       if (!IsKindWeWant(K))
7512         continue;
7513 
7514       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7515 
7516       // Don't add predefined declarations to the lexical context more
7517       // than once.
7518       if (ID < NUM_PREDEF_DECL_IDS) {
7519         if (PredefsVisited[ID])
7520           continue;
7521 
7522         PredefsVisited[ID] = true;
7523       }
7524 
7525       if (Decl *D = GetLocalDecl(*M, ID)) {
7526         assert(D->getKind() == K && "wrong kind for lexical decl");
7527         if (!DC->isDeclInLexicalTraversal(D))
7528           Decls.push_back(D);
7529       }
7530     }
7531   };
7532 
7533   if (isa<TranslationUnitDecl>(DC)) {
7534     for (auto Lexical : TULexicalDecls)
7535       Visit(Lexical.first, Lexical.second);
7536   } else {
7537     auto I = LexicalDecls.find(DC);
7538     if (I != LexicalDecls.end())
7539       Visit(I->second.first, I->second.second);
7540   }
7541 
7542   ++NumLexicalDeclContextsRead;
7543 }
7544 
7545 namespace {
7546 
7547 class DeclIDComp {
7548   ASTReader &Reader;
7549   ModuleFile &Mod;
7550 
7551 public:
7552   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7553 
7554   bool operator()(LocalDeclID L, LocalDeclID R) const {
7555     SourceLocation LHS = getLocation(L);
7556     SourceLocation RHS = getLocation(R);
7557     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7558   }
7559 
7560   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7561     SourceLocation RHS = getLocation(R);
7562     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7563   }
7564 
7565   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7566     SourceLocation LHS = getLocation(L);
7567     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7568   }
7569 
7570   SourceLocation getLocation(LocalDeclID ID) const {
7571     return Reader.getSourceManager().getFileLoc(
7572             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7573   }
7574 };
7575 
7576 } // namespace
7577 
7578 void ASTReader::FindFileRegionDecls(FileID File,
7579                                     unsigned Offset, unsigned Length,
7580                                     SmallVectorImpl<Decl *> &Decls) {
7581   SourceManager &SM = getSourceManager();
7582 
7583   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7584   if (I == FileDeclIDs.end())
7585     return;
7586 
7587   FileDeclsInfo &DInfo = I->second;
7588   if (DInfo.Decls.empty())
7589     return;
7590 
7591   SourceLocation
7592     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7593   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7594 
7595   DeclIDComp DIDComp(*this, *DInfo.Mod);
7596   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7597       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7598   if (BeginIt != DInfo.Decls.begin())
7599     --BeginIt;
7600 
7601   // If we are pointing at a top-level decl inside an objc container, we need
7602   // to backtrack until we find it otherwise we will fail to report that the
7603   // region overlaps with an objc container.
7604   while (BeginIt != DInfo.Decls.begin() &&
7605          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7606              ->isTopLevelDeclInObjCContainer())
7607     --BeginIt;
7608 
7609   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7610       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7611   if (EndIt != DInfo.Decls.end())
7612     ++EndIt;
7613 
7614   for (ArrayRef<serialization::LocalDeclID>::iterator
7615          DIt = BeginIt; DIt != EndIt; ++DIt)
7616     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7617 }
7618 
7619 bool
7620 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7621                                           DeclarationName Name) {
7622   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7623          "DeclContext has no visible decls in storage");
7624   if (!Name)
7625     return false;
7626 
7627   auto It = Lookups.find(DC);
7628   if (It == Lookups.end())
7629     return false;
7630 
7631   Deserializing LookupResults(this);
7632 
7633   // Load the list of declarations.
7634   SmallVector<NamedDecl *, 64> Decls;
7635   llvm::SmallPtrSet<NamedDecl *, 8> Found;
7636   for (DeclID ID : It->second.Table.find(Name)) {
7637     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7638     if (ND->getDeclName() == Name && Found.insert(ND).second)
7639       Decls.push_back(ND);
7640   }
7641 
7642   ++NumVisibleDeclContextsRead;
7643   SetExternalVisibleDeclsForName(DC, Name, Decls);
7644   return !Decls.empty();
7645 }
7646 
7647 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7648   if (!DC->hasExternalVisibleStorage())
7649     return;
7650 
7651   auto It = Lookups.find(DC);
7652   assert(It != Lookups.end() &&
7653          "have external visible storage but no lookup tables");
7654 
7655   DeclsMap Decls;
7656 
7657   for (DeclID ID : It->second.Table.findAll()) {
7658     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7659     Decls[ND->getDeclName()].push_back(ND);
7660   }
7661 
7662   ++NumVisibleDeclContextsRead;
7663 
7664   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7665     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7666   }
7667   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7668 }
7669 
7670 const serialization::reader::DeclContextLookupTable *
7671 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7672   auto I = Lookups.find(Primary);
7673   return I == Lookups.end() ? nullptr : &I->second;
7674 }
7675 
7676 /// Under non-PCH compilation the consumer receives the objc methods
7677 /// before receiving the implementation, and codegen depends on this.
7678 /// We simulate this by deserializing and passing to consumer the methods of the
7679 /// implementation before passing the deserialized implementation decl.
7680 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7681                                        ASTConsumer *Consumer) {
7682   assert(ImplD && Consumer);
7683 
7684   for (auto *I : ImplD->methods())
7685     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7686 
7687   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7688 }
7689 
7690 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7691   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7692     PassObjCImplDeclToConsumer(ImplD, Consumer);
7693   else
7694     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7695 }
7696 
7697 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7698   this->Consumer = Consumer;
7699 
7700   if (Consumer)
7701     PassInterestingDeclsToConsumer();
7702 
7703   if (DeserializationListener)
7704     DeserializationListener->ReaderInitialized(this);
7705 }
7706 
7707 void ASTReader::PrintStats() {
7708   std::fprintf(stderr, "*** AST File Statistics:\n");
7709 
7710   unsigned NumTypesLoaded =
7711       TypesLoaded.size() - llvm::count(TypesLoaded, QualType());
7712   unsigned NumDeclsLoaded =
7713       DeclsLoaded.size() - llvm::count(DeclsLoaded, (Decl *)nullptr);
7714   unsigned NumIdentifiersLoaded =
7715       IdentifiersLoaded.size() -
7716       llvm::count(IdentifiersLoaded, (IdentifierInfo *)nullptr);
7717   unsigned NumMacrosLoaded =
7718       MacrosLoaded.size() - llvm::count(MacrosLoaded, (MacroInfo *)nullptr);
7719   unsigned NumSelectorsLoaded =
7720       SelectorsLoaded.size() - llvm::count(SelectorsLoaded, Selector());
7721 
7722   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7723     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7724                  NumSLocEntriesRead, TotalNumSLocEntries,
7725                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7726   if (!TypesLoaded.empty())
7727     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7728                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7729                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7730   if (!DeclsLoaded.empty())
7731     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7732                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7733                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7734   if (!IdentifiersLoaded.empty())
7735     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7736                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7737                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7738   if (!MacrosLoaded.empty())
7739     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7740                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7741                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7742   if (!SelectorsLoaded.empty())
7743     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7744                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7745                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7746   if (TotalNumStatements)
7747     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7748                  NumStatementsRead, TotalNumStatements,
7749                  ((float)NumStatementsRead/TotalNumStatements * 100));
7750   if (TotalNumMacros)
7751     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7752                  NumMacrosRead, TotalNumMacros,
7753                  ((float)NumMacrosRead/TotalNumMacros * 100));
7754   if (TotalLexicalDeclContexts)
7755     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7756                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7757                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7758                   * 100));
7759   if (TotalVisibleDeclContexts)
7760     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7761                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7762                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7763                   * 100));
7764   if (TotalNumMethodPoolEntries)
7765     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7766                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7767                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7768                   * 100));
7769   if (NumMethodPoolLookups)
7770     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7771                  NumMethodPoolHits, NumMethodPoolLookups,
7772                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7773   if (NumMethodPoolTableLookups)
7774     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7775                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7776                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7777                   * 100.0));
7778   if (NumIdentifierLookupHits)
7779     std::fprintf(stderr,
7780                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7781                  NumIdentifierLookupHits, NumIdentifierLookups,
7782                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7783 
7784   if (GlobalIndex) {
7785     std::fprintf(stderr, "\n");
7786     GlobalIndex->printStats();
7787   }
7788 
7789   std::fprintf(stderr, "\n");
7790   dump();
7791   std::fprintf(stderr, "\n");
7792 }
7793 
7794 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7795 LLVM_DUMP_METHOD static void
7796 dumpModuleIDMap(StringRef Name,
7797                 const ContinuousRangeMap<Key, ModuleFile *,
7798                                          InitialCapacity> &Map) {
7799   if (Map.begin() == Map.end())
7800     return;
7801 
7802   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7803 
7804   llvm::errs() << Name << ":\n";
7805   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7806        I != IEnd; ++I) {
7807     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7808       << "\n";
7809   }
7810 }
7811 
7812 LLVM_DUMP_METHOD void ASTReader::dump() {
7813   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7814   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7815   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7816   dumpModuleIDMap("Global type map", GlobalTypeMap);
7817   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7818   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7819   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7820   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7821   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7822   dumpModuleIDMap("Global preprocessed entity map",
7823                   GlobalPreprocessedEntityMap);
7824 
7825   llvm::errs() << "\n*** PCH/Modules Loaded:";
7826   for (ModuleFile &M : ModuleMgr)
7827     M.dump();
7828 }
7829 
7830 /// Return the amount of memory used by memory buffers, breaking down
7831 /// by heap-backed versus mmap'ed memory.
7832 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7833   for (ModuleFile &I : ModuleMgr) {
7834     if (llvm::MemoryBuffer *buf = I.Buffer) {
7835       size_t bytes = buf->getBufferSize();
7836       switch (buf->getBufferKind()) {
7837         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7838           sizes.malloc_bytes += bytes;
7839           break;
7840         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7841           sizes.mmap_bytes += bytes;
7842           break;
7843       }
7844     }
7845   }
7846 }
7847 
7848 void ASTReader::InitializeSema(Sema &S) {
7849   SemaObj = &S;
7850   S.addExternalSource(this);
7851 
7852   // Makes sure any declarations that were deserialized "too early"
7853   // still get added to the identifier's declaration chains.
7854   for (uint64_t ID : PreloadedDeclIDs) {
7855     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7856     pushExternalDeclIntoScope(D, D->getDeclName());
7857   }
7858   PreloadedDeclIDs.clear();
7859 
7860   // FIXME: What happens if these are changed by a module import?
7861   if (!FPPragmaOptions.empty()) {
7862     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7863     FPOptionsOverride NewOverrides =
7864         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7865     SemaObj->CurFPFeatures =
7866         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7867   }
7868 
7869   SemaObj->OpenCLFeatures = OpenCLExtensions;
7870 
7871   UpdateSema();
7872 }
7873 
7874 void ASTReader::UpdateSema() {
7875   assert(SemaObj && "no Sema to update");
7876 
7877   // Load the offsets of the declarations that Sema references.
7878   // They will be lazily deserialized when needed.
7879   if (!SemaDeclRefs.empty()) {
7880     assert(SemaDeclRefs.size() % 3 == 0);
7881     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7882       if (!SemaObj->StdNamespace)
7883         SemaObj->StdNamespace = SemaDeclRefs[I];
7884       if (!SemaObj->StdBadAlloc)
7885         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7886       if (!SemaObj->StdAlignValT)
7887         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7888     }
7889     SemaDeclRefs.clear();
7890   }
7891 
7892   // Update the state of pragmas. Use the same API as if we had encountered the
7893   // pragma in the source.
7894   if(OptimizeOffPragmaLocation.isValid())
7895     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7896   if (PragmaMSStructState != -1)
7897     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7898   if (PointersToMembersPragmaLocation.isValid()) {
7899     SemaObj->ActOnPragmaMSPointersToMembers(
7900         (LangOptions::PragmaMSPointersToMembersKind)
7901             PragmaMSPointersToMembersState,
7902         PointersToMembersPragmaLocation);
7903   }
7904   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7905 
7906   if (PragmaAlignPackCurrentValue) {
7907     // The bottom of the stack might have a default value. It must be adjusted
7908     // to the current value to ensure that the packing state is preserved after
7909     // popping entries that were included/imported from a PCH/module.
7910     bool DropFirst = false;
7911     if (!PragmaAlignPackStack.empty() &&
7912         PragmaAlignPackStack.front().Location.isInvalid()) {
7913       assert(PragmaAlignPackStack.front().Value ==
7914                  SemaObj->AlignPackStack.DefaultValue &&
7915              "Expected a default alignment value");
7916       SemaObj->AlignPackStack.Stack.emplace_back(
7917           PragmaAlignPackStack.front().SlotLabel,
7918           SemaObj->AlignPackStack.CurrentValue,
7919           SemaObj->AlignPackStack.CurrentPragmaLocation,
7920           PragmaAlignPackStack.front().PushLocation);
7921       DropFirst = true;
7922     }
7923     for (const auto &Entry : llvm::makeArrayRef(PragmaAlignPackStack)
7924                                  .drop_front(DropFirst ? 1 : 0)) {
7925       SemaObj->AlignPackStack.Stack.emplace_back(
7926           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7927     }
7928     if (PragmaAlignPackCurrentLocation.isInvalid()) {
7929       assert(*PragmaAlignPackCurrentValue ==
7930                  SemaObj->AlignPackStack.DefaultValue &&
7931              "Expected a default align and pack value");
7932       // Keep the current values.
7933     } else {
7934       SemaObj->AlignPackStack.CurrentValue = *PragmaAlignPackCurrentValue;
7935       SemaObj->AlignPackStack.CurrentPragmaLocation =
7936           PragmaAlignPackCurrentLocation;
7937     }
7938   }
7939   if (FpPragmaCurrentValue) {
7940     // The bottom of the stack might have a default value. It must be adjusted
7941     // to the current value to ensure that fp-pragma state is preserved after
7942     // popping entries that were included/imported from a PCH/module.
7943     bool DropFirst = false;
7944     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7945       assert(FpPragmaStack.front().Value ==
7946                  SemaObj->FpPragmaStack.DefaultValue &&
7947              "Expected a default pragma float_control value");
7948       SemaObj->FpPragmaStack.Stack.emplace_back(
7949           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7950           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7951           FpPragmaStack.front().PushLocation);
7952       DropFirst = true;
7953     }
7954     for (const auto &Entry :
7955          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7956       SemaObj->FpPragmaStack.Stack.emplace_back(
7957           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7958     if (FpPragmaCurrentLocation.isInvalid()) {
7959       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7960              "Expected a default pragma float_control value");
7961       // Keep the current values.
7962     } else {
7963       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7964       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7965     }
7966   }
7967 
7968   // For non-modular AST files, restore visiblity of modules.
7969   for (auto &Import : ImportedModules) {
7970     if (Import.ImportLoc.isInvalid())
7971       continue;
7972     if (Module *Imported = getSubmodule(Import.ID)) {
7973       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7974     }
7975   }
7976 }
7977 
7978 IdentifierInfo *ASTReader::get(StringRef Name) {
7979   // Note that we are loading an identifier.
7980   Deserializing AnIdentifier(this);
7981 
7982   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7983                                   NumIdentifierLookups,
7984                                   NumIdentifierLookupHits);
7985 
7986   // We don't need to do identifier table lookups in C++ modules (we preload
7987   // all interesting declarations, and don't need to use the scope for name
7988   // lookups). Perform the lookup in PCH files, though, since we don't build
7989   // a complete initial identifier table if we're carrying on from a PCH.
7990   if (PP.getLangOpts().CPlusPlus) {
7991     for (auto F : ModuleMgr.pch_modules())
7992       if (Visitor(*F))
7993         break;
7994   } else {
7995     // If there is a global index, look there first to determine which modules
7996     // provably do not have any results for this identifier.
7997     GlobalModuleIndex::HitSet Hits;
7998     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7999     if (!loadGlobalIndex()) {
8000       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
8001         HitsPtr = &Hits;
8002       }
8003     }
8004 
8005     ModuleMgr.visit(Visitor, HitsPtr);
8006   }
8007 
8008   IdentifierInfo *II = Visitor.getIdentifierInfo();
8009   markIdentifierUpToDate(II);
8010   return II;
8011 }
8012 
8013 namespace clang {
8014 
8015   /// An identifier-lookup iterator that enumerates all of the
8016   /// identifiers stored within a set of AST files.
8017   class ASTIdentifierIterator : public IdentifierIterator {
8018     /// The AST reader whose identifiers are being enumerated.
8019     const ASTReader &Reader;
8020 
8021     /// The current index into the chain of AST files stored in
8022     /// the AST reader.
8023     unsigned Index;
8024 
8025     /// The current position within the identifier lookup table
8026     /// of the current AST file.
8027     ASTIdentifierLookupTable::key_iterator Current;
8028 
8029     /// The end position within the identifier lookup table of
8030     /// the current AST file.
8031     ASTIdentifierLookupTable::key_iterator End;
8032 
8033     /// Whether to skip any modules in the ASTReader.
8034     bool SkipModules;
8035 
8036   public:
8037     explicit ASTIdentifierIterator(const ASTReader &Reader,
8038                                    bool SkipModules = false);
8039 
8040     StringRef Next() override;
8041   };
8042 
8043 } // namespace clang
8044 
8045 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8046                                              bool SkipModules)
8047     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8048 }
8049 
8050 StringRef ASTIdentifierIterator::Next() {
8051   while (Current == End) {
8052     // If we have exhausted all of our AST files, we're done.
8053     if (Index == 0)
8054       return StringRef();
8055 
8056     --Index;
8057     ModuleFile &F = Reader.ModuleMgr[Index];
8058     if (SkipModules && F.isModule())
8059       continue;
8060 
8061     ASTIdentifierLookupTable *IdTable =
8062         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8063     Current = IdTable->key_begin();
8064     End = IdTable->key_end();
8065   }
8066 
8067   // We have any identifiers remaining in the current AST file; return
8068   // the next one.
8069   StringRef Result = *Current;
8070   ++Current;
8071   return Result;
8072 }
8073 
8074 namespace {
8075 
8076 /// A utility for appending two IdentifierIterators.
8077 class ChainedIdentifierIterator : public IdentifierIterator {
8078   std::unique_ptr<IdentifierIterator> Current;
8079   std::unique_ptr<IdentifierIterator> Queued;
8080 
8081 public:
8082   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8083                             std::unique_ptr<IdentifierIterator> Second)
8084       : Current(std::move(First)), Queued(std::move(Second)) {}
8085 
8086   StringRef Next() override {
8087     if (!Current)
8088       return StringRef();
8089 
8090     StringRef result = Current->Next();
8091     if (!result.empty())
8092       return result;
8093 
8094     // Try the queued iterator, which may itself be empty.
8095     Current.reset();
8096     std::swap(Current, Queued);
8097     return Next();
8098   }
8099 };
8100 
8101 } // namespace
8102 
8103 IdentifierIterator *ASTReader::getIdentifiers() {
8104   if (!loadGlobalIndex()) {
8105     std::unique_ptr<IdentifierIterator> ReaderIter(
8106         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8107     std::unique_ptr<IdentifierIterator> ModulesIter(
8108         GlobalIndex->createIdentifierIterator());
8109     return new ChainedIdentifierIterator(std::move(ReaderIter),
8110                                          std::move(ModulesIter));
8111   }
8112 
8113   return new ASTIdentifierIterator(*this);
8114 }
8115 
8116 namespace clang {
8117 namespace serialization {
8118 
8119   class ReadMethodPoolVisitor {
8120     ASTReader &Reader;
8121     Selector Sel;
8122     unsigned PriorGeneration;
8123     unsigned InstanceBits = 0;
8124     unsigned FactoryBits = 0;
8125     bool InstanceHasMoreThanOneDecl = false;
8126     bool FactoryHasMoreThanOneDecl = false;
8127     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8128     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8129 
8130   public:
8131     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8132                           unsigned PriorGeneration)
8133         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8134 
8135     bool operator()(ModuleFile &M) {
8136       if (!M.SelectorLookupTable)
8137         return false;
8138 
8139       // If we've already searched this module file, skip it now.
8140       if (M.Generation <= PriorGeneration)
8141         return true;
8142 
8143       ++Reader.NumMethodPoolTableLookups;
8144       ASTSelectorLookupTable *PoolTable
8145         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8146       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8147       if (Pos == PoolTable->end())
8148         return false;
8149 
8150       ++Reader.NumMethodPoolTableHits;
8151       ++Reader.NumSelectorsRead;
8152       // FIXME: Not quite happy with the statistics here. We probably should
8153       // disable this tracking when called via LoadSelector.
8154       // Also, should entries without methods count as misses?
8155       ++Reader.NumMethodPoolEntriesRead;
8156       ASTSelectorLookupTrait::data_type Data = *Pos;
8157       if (Reader.DeserializationListener)
8158         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8159 
8160       // Append methods in the reverse order, so that later we can process them
8161       // in the order they appear in the source code by iterating through
8162       // the vector in the reverse order.
8163       InstanceMethods.append(Data.Instance.rbegin(), Data.Instance.rend());
8164       FactoryMethods.append(Data.Factory.rbegin(), Data.Factory.rend());
8165       InstanceBits = Data.InstanceBits;
8166       FactoryBits = Data.FactoryBits;
8167       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8168       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8169       return false;
8170     }
8171 
8172     /// Retrieve the instance methods found by this visitor.
8173     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8174       return InstanceMethods;
8175     }
8176 
8177     /// Retrieve the instance methods found by this visitor.
8178     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8179       return FactoryMethods;
8180     }
8181 
8182     unsigned getInstanceBits() const { return InstanceBits; }
8183     unsigned getFactoryBits() const { return FactoryBits; }
8184 
8185     bool instanceHasMoreThanOneDecl() const {
8186       return InstanceHasMoreThanOneDecl;
8187     }
8188 
8189     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8190   };
8191 
8192 } // namespace serialization
8193 } // namespace clang
8194 
8195 /// Add the given set of methods to the method list.
8196 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8197                              ObjCMethodList &List) {
8198   for (auto I = Methods.rbegin(), E = Methods.rend(); I != E; ++I)
8199     S.addMethodToGlobalList(&List, *I);
8200 }
8201 
8202 void ASTReader::ReadMethodPool(Selector Sel) {
8203   // Get the selector generation and update it to the current generation.
8204   unsigned &Generation = SelectorGeneration[Sel];
8205   unsigned PriorGeneration = Generation;
8206   Generation = getGeneration();
8207   SelectorOutOfDate[Sel] = false;
8208 
8209   // Search for methods defined with this selector.
8210   ++NumMethodPoolLookups;
8211   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8212   ModuleMgr.visit(Visitor);
8213 
8214   if (Visitor.getInstanceMethods().empty() &&
8215       Visitor.getFactoryMethods().empty())
8216     return;
8217 
8218   ++NumMethodPoolHits;
8219 
8220   if (!getSema())
8221     return;
8222 
8223   Sema &S = *getSema();
8224   Sema::GlobalMethodPool::iterator Pos =
8225       S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethodPool::Lists()))
8226           .first;
8227 
8228   Pos->second.first.setBits(Visitor.getInstanceBits());
8229   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8230   Pos->second.second.setBits(Visitor.getFactoryBits());
8231   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8232 
8233   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8234   // when building a module we keep every method individually and may need to
8235   // update hasMoreThanOneDecl as we add the methods.
8236   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8237   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8238 }
8239 
8240 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8241   if (SelectorOutOfDate[Sel])
8242     ReadMethodPool(Sel);
8243 }
8244 
8245 void ASTReader::ReadKnownNamespaces(
8246                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8247   Namespaces.clear();
8248 
8249   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8250     if (NamespaceDecl *Namespace
8251                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8252       Namespaces.push_back(Namespace);
8253   }
8254 }
8255 
8256 void ASTReader::ReadUndefinedButUsed(
8257     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8258   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8259     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8260     SourceLocation Loc =
8261         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8262     Undefined.insert(std::make_pair(D, Loc));
8263   }
8264 }
8265 
8266 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8267     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8268                                                      Exprs) {
8269   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8270     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8271     uint64_t Count = DelayedDeleteExprs[Idx++];
8272     for (uint64_t C = 0; C < Count; ++C) {
8273       SourceLocation DeleteLoc =
8274           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8275       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8276       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8277     }
8278   }
8279 }
8280 
8281 void ASTReader::ReadTentativeDefinitions(
8282                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8283   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8284     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8285     if (Var)
8286       TentativeDefs.push_back(Var);
8287   }
8288   TentativeDefinitions.clear();
8289 }
8290 
8291 void ASTReader::ReadUnusedFileScopedDecls(
8292                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8293   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8294     DeclaratorDecl *D
8295       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8296     if (D)
8297       Decls.push_back(D);
8298   }
8299   UnusedFileScopedDecls.clear();
8300 }
8301 
8302 void ASTReader::ReadDelegatingConstructors(
8303                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8304   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8305     CXXConstructorDecl *D
8306       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8307     if (D)
8308       Decls.push_back(D);
8309   }
8310   DelegatingCtorDecls.clear();
8311 }
8312 
8313 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8314   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8315     TypedefNameDecl *D
8316       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8317     if (D)
8318       Decls.push_back(D);
8319   }
8320   ExtVectorDecls.clear();
8321 }
8322 
8323 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8324     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8325   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8326        ++I) {
8327     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8328         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8329     if (D)
8330       Decls.insert(D);
8331   }
8332   UnusedLocalTypedefNameCandidates.clear();
8333 }
8334 
8335 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8336     llvm::SmallSetVector<Decl *, 4> &Decls) {
8337   for (auto I : DeclsToCheckForDeferredDiags) {
8338     auto *D = dyn_cast_or_null<Decl>(GetDecl(I));
8339     if (D)
8340       Decls.insert(D);
8341   }
8342   DeclsToCheckForDeferredDiags.clear();
8343 }
8344 
8345 void ASTReader::ReadReferencedSelectors(
8346        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8347   if (ReferencedSelectorsData.empty())
8348     return;
8349 
8350   // If there are @selector references added them to its pool. This is for
8351   // implementation of -Wselector.
8352   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8353   unsigned I = 0;
8354   while (I < DataSize) {
8355     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8356     SourceLocation SelLoc
8357       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8358     Sels.push_back(std::make_pair(Sel, SelLoc));
8359   }
8360   ReferencedSelectorsData.clear();
8361 }
8362 
8363 void ASTReader::ReadWeakUndeclaredIdentifiers(
8364        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8365   if (WeakUndeclaredIdentifiers.empty())
8366     return;
8367 
8368   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8369     IdentifierInfo *WeakId
8370       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8371     IdentifierInfo *AliasId
8372       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8373     SourceLocation Loc
8374       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8375     bool Used = WeakUndeclaredIdentifiers[I++];
8376     WeakInfo WI(AliasId, Loc);
8377     WI.setUsed(Used);
8378     WeakIDs.push_back(std::make_pair(WeakId, WI));
8379   }
8380   WeakUndeclaredIdentifiers.clear();
8381 }
8382 
8383 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8384   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8385     ExternalVTableUse VT;
8386     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8387     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8388     VT.DefinitionRequired = VTableUses[Idx++];
8389     VTables.push_back(VT);
8390   }
8391 
8392   VTableUses.clear();
8393 }
8394 
8395 void ASTReader::ReadPendingInstantiations(
8396        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8397   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8398     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8399     SourceLocation Loc
8400       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8401 
8402     Pending.push_back(std::make_pair(D, Loc));
8403   }
8404   PendingInstantiations.clear();
8405 }
8406 
8407 void ASTReader::ReadLateParsedTemplates(
8408     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8409         &LPTMap) {
8410   for (auto &LPT : LateParsedTemplates) {
8411     ModuleFile *FMod = LPT.first;
8412     RecordDataImpl &LateParsed = LPT.second;
8413     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8414          /* In loop */) {
8415       FunctionDecl *FD =
8416           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8417 
8418       auto LT = std::make_unique<LateParsedTemplate>();
8419       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8420 
8421       ModuleFile *F = getOwningModuleFile(LT->D);
8422       assert(F && "No module");
8423 
8424       unsigned TokN = LateParsed[Idx++];
8425       LT->Toks.reserve(TokN);
8426       for (unsigned T = 0; T < TokN; ++T)
8427         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8428 
8429       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8430     }
8431   }
8432 
8433   LateParsedTemplates.clear();
8434 }
8435 
8436 void ASTReader::LoadSelector(Selector Sel) {
8437   // It would be complicated to avoid reading the methods anyway. So don't.
8438   ReadMethodPool(Sel);
8439 }
8440 
8441 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8442   assert(ID && "Non-zero identifier ID required");
8443   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8444   IdentifiersLoaded[ID - 1] = II;
8445   if (DeserializationListener)
8446     DeserializationListener->IdentifierRead(ID, II);
8447 }
8448 
8449 /// Set the globally-visible declarations associated with the given
8450 /// identifier.
8451 ///
8452 /// If the AST reader is currently in a state where the given declaration IDs
8453 /// cannot safely be resolved, they are queued until it is safe to resolve
8454 /// them.
8455 ///
8456 /// \param II an IdentifierInfo that refers to one or more globally-visible
8457 /// declarations.
8458 ///
8459 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8460 /// visible at global scope.
8461 ///
8462 /// \param Decls if non-null, this vector will be populated with the set of
8463 /// deserialized declarations. These declarations will not be pushed into
8464 /// scope.
8465 void
8466 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8467                               const SmallVectorImpl<uint32_t> &DeclIDs,
8468                                    SmallVectorImpl<Decl *> *Decls) {
8469   if (NumCurrentElementsDeserializing && !Decls) {
8470     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8471     return;
8472   }
8473 
8474   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8475     if (!SemaObj) {
8476       // Queue this declaration so that it will be added to the
8477       // translation unit scope and identifier's declaration chain
8478       // once a Sema object is known.
8479       PreloadedDeclIDs.push_back(DeclIDs[I]);
8480       continue;
8481     }
8482 
8483     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8484 
8485     // If we're simply supposed to record the declarations, do so now.
8486     if (Decls) {
8487       Decls->push_back(D);
8488       continue;
8489     }
8490 
8491     // Introduce this declaration into the translation-unit scope
8492     // and add it to the declaration chain for this identifier, so
8493     // that (unqualified) name lookup will find it.
8494     pushExternalDeclIntoScope(D, II);
8495   }
8496 }
8497 
8498 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8499   if (ID == 0)
8500     return nullptr;
8501 
8502   if (IdentifiersLoaded.empty()) {
8503     Error("no identifier table in AST file");
8504     return nullptr;
8505   }
8506 
8507   ID -= 1;
8508   if (!IdentifiersLoaded[ID]) {
8509     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8510     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8511     ModuleFile *M = I->second;
8512     unsigned Index = ID - M->BaseIdentifierID;
8513     const unsigned char *Data =
8514         M->IdentifierTableData + M->IdentifierOffsets[Index];
8515 
8516     ASTIdentifierLookupTrait Trait(*this, *M);
8517     auto KeyDataLen = Trait.ReadKeyDataLength(Data);
8518     auto Key = Trait.ReadKey(Data, KeyDataLen.first);
8519     auto &II = PP.getIdentifierTable().get(Key);
8520     IdentifiersLoaded[ID] = &II;
8521     markIdentifierFromAST(*this,  II);
8522     if (DeserializationListener)
8523       DeserializationListener->IdentifierRead(ID + 1, &II);
8524   }
8525 
8526   return IdentifiersLoaded[ID];
8527 }
8528 
8529 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8530   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8531 }
8532 
8533 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8534   if (LocalID < NUM_PREDEF_IDENT_IDS)
8535     return LocalID;
8536 
8537   if (!M.ModuleOffsetMap.empty())
8538     ReadModuleOffsetMap(M);
8539 
8540   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8541     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8542   assert(I != M.IdentifierRemap.end()
8543          && "Invalid index into identifier index remap");
8544 
8545   return LocalID + I->second;
8546 }
8547 
8548 MacroInfo *ASTReader::getMacro(MacroID ID) {
8549   if (ID == 0)
8550     return nullptr;
8551 
8552   if (MacrosLoaded.empty()) {
8553     Error("no macro table in AST file");
8554     return nullptr;
8555   }
8556 
8557   ID -= NUM_PREDEF_MACRO_IDS;
8558   if (!MacrosLoaded[ID]) {
8559     GlobalMacroMapType::iterator I
8560       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8561     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8562     ModuleFile *M = I->second;
8563     unsigned Index = ID - M->BaseMacroID;
8564     MacrosLoaded[ID] =
8565         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8566 
8567     if (DeserializationListener)
8568       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8569                                          MacrosLoaded[ID]);
8570   }
8571 
8572   return MacrosLoaded[ID];
8573 }
8574 
8575 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8576   if (LocalID < NUM_PREDEF_MACRO_IDS)
8577     return LocalID;
8578 
8579   if (!M.ModuleOffsetMap.empty())
8580     ReadModuleOffsetMap(M);
8581 
8582   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8583     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8584   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8585 
8586   return LocalID + I->second;
8587 }
8588 
8589 serialization::SubmoduleID
8590 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8591   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8592     return LocalID;
8593 
8594   if (!M.ModuleOffsetMap.empty())
8595     ReadModuleOffsetMap(M);
8596 
8597   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8598     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8599   assert(I != M.SubmoduleRemap.end()
8600          && "Invalid index into submodule index remap");
8601 
8602   return LocalID + I->second;
8603 }
8604 
8605 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8606   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8607     assert(GlobalID == 0 && "Unhandled global submodule ID");
8608     return nullptr;
8609   }
8610 
8611   if (GlobalID > SubmodulesLoaded.size()) {
8612     Error("submodule ID out of range in AST file");
8613     return nullptr;
8614   }
8615 
8616   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8617 }
8618 
8619 Module *ASTReader::getModule(unsigned ID) {
8620   return getSubmodule(ID);
8621 }
8622 
8623 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8624   if (ID & 1) {
8625     // It's a module, look it up by submodule ID.
8626     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8627     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8628   } else {
8629     // It's a prefix (preamble, PCH, ...). Look it up by index.
8630     unsigned IndexFromEnd = ID >> 1;
8631     assert(IndexFromEnd && "got reference to unknown module file");
8632     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8633   }
8634 }
8635 
8636 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8637   if (!F)
8638     return 1;
8639 
8640   // For a file representing a module, use the submodule ID of the top-level
8641   // module as the file ID. For any other kind of file, the number of such
8642   // files loaded beforehand will be the same on reload.
8643   // FIXME: Is this true even if we have an explicit module file and a PCH?
8644   if (F->isModule())
8645     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8646 
8647   auto PCHModules = getModuleManager().pch_modules();
8648   auto I = llvm::find(PCHModules, F);
8649   assert(I != PCHModules.end() && "emitting reference to unknown file");
8650   return (I - PCHModules.end()) << 1;
8651 }
8652 
8653 llvm::Optional<ASTSourceDescriptor>
8654 ASTReader::getSourceDescriptor(unsigned ID) {
8655   if (Module *M = getSubmodule(ID))
8656     return ASTSourceDescriptor(*M);
8657 
8658   // If there is only a single PCH, return it instead.
8659   // Chained PCH are not supported.
8660   const auto &PCHChain = ModuleMgr.pch_modules();
8661   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8662     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8663     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8664     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8665     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8666                                MF.Signature);
8667   }
8668   return None;
8669 }
8670 
8671 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8672   auto I = DefinitionSource.find(FD);
8673   if (I == DefinitionSource.end())
8674     return EK_ReplyHazy;
8675   return I->second ? EK_Never : EK_Always;
8676 }
8677 
8678 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8679   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8680 }
8681 
8682 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8683   if (ID == 0)
8684     return Selector();
8685 
8686   if (ID > SelectorsLoaded.size()) {
8687     Error("selector ID out of range in AST file");
8688     return Selector();
8689   }
8690 
8691   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8692     // Load this selector from the selector table.
8693     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8694     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8695     ModuleFile &M = *I->second;
8696     ASTSelectorLookupTrait Trait(*this, M);
8697     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8698     SelectorsLoaded[ID - 1] =
8699       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8700     if (DeserializationListener)
8701       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8702   }
8703 
8704   return SelectorsLoaded[ID - 1];
8705 }
8706 
8707 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8708   return DecodeSelector(ID);
8709 }
8710 
8711 uint32_t ASTReader::GetNumExternalSelectors() {
8712   // ID 0 (the null selector) is considered an external selector.
8713   return getTotalNumSelectors() + 1;
8714 }
8715 
8716 serialization::SelectorID
8717 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8718   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8719     return LocalID;
8720 
8721   if (!M.ModuleOffsetMap.empty())
8722     ReadModuleOffsetMap(M);
8723 
8724   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8725     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8726   assert(I != M.SelectorRemap.end()
8727          && "Invalid index into selector index remap");
8728 
8729   return LocalID + I->second;
8730 }
8731 
8732 DeclarationNameLoc
8733 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8734   switch (Name.getNameKind()) {
8735   case DeclarationName::CXXConstructorName:
8736   case DeclarationName::CXXDestructorName:
8737   case DeclarationName::CXXConversionFunctionName:
8738     return DeclarationNameLoc::makeNamedTypeLoc(readTypeSourceInfo());
8739 
8740   case DeclarationName::CXXOperatorName:
8741     return DeclarationNameLoc::makeCXXOperatorNameLoc(readSourceRange());
8742 
8743   case DeclarationName::CXXLiteralOperatorName:
8744     return DeclarationNameLoc::makeCXXLiteralOperatorNameLoc(
8745         readSourceLocation());
8746 
8747   case DeclarationName::Identifier:
8748   case DeclarationName::ObjCZeroArgSelector:
8749   case DeclarationName::ObjCOneArgSelector:
8750   case DeclarationName::ObjCMultiArgSelector:
8751   case DeclarationName::CXXUsingDirective:
8752   case DeclarationName::CXXDeductionGuideName:
8753     break;
8754   }
8755   return DeclarationNameLoc();
8756 }
8757 
8758 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8759   DeclarationNameInfo NameInfo;
8760   NameInfo.setName(readDeclarationName());
8761   NameInfo.setLoc(readSourceLocation());
8762   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8763   return NameInfo;
8764 }
8765 
8766 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8767   Info.QualifierLoc = readNestedNameSpecifierLoc();
8768   unsigned NumTPLists = readInt();
8769   Info.NumTemplParamLists = NumTPLists;
8770   if (NumTPLists) {
8771     Info.TemplParamLists =
8772         new (getContext()) TemplateParameterList *[NumTPLists];
8773     for (unsigned i = 0; i != NumTPLists; ++i)
8774       Info.TemplParamLists[i] = readTemplateParameterList();
8775   }
8776 }
8777 
8778 TemplateParameterList *
8779 ASTRecordReader::readTemplateParameterList() {
8780   SourceLocation TemplateLoc = readSourceLocation();
8781   SourceLocation LAngleLoc = readSourceLocation();
8782   SourceLocation RAngleLoc = readSourceLocation();
8783 
8784   unsigned NumParams = readInt();
8785   SmallVector<NamedDecl *, 16> Params;
8786   Params.reserve(NumParams);
8787   while (NumParams--)
8788     Params.push_back(readDeclAs<NamedDecl>());
8789 
8790   bool HasRequiresClause = readBool();
8791   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8792 
8793   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8794       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8795   return TemplateParams;
8796 }
8797 
8798 void ASTRecordReader::readTemplateArgumentList(
8799                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8800                         bool Canonicalize) {
8801   unsigned NumTemplateArgs = readInt();
8802   TemplArgs.reserve(NumTemplateArgs);
8803   while (NumTemplateArgs--)
8804     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8805 }
8806 
8807 /// Read a UnresolvedSet structure.
8808 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8809   unsigned NumDecls = readInt();
8810   Set.reserve(getContext(), NumDecls);
8811   while (NumDecls--) {
8812     DeclID ID = readDeclID();
8813     AccessSpecifier AS = (AccessSpecifier) readInt();
8814     Set.addLazyDecl(getContext(), ID, AS);
8815   }
8816 }
8817 
8818 CXXBaseSpecifier
8819 ASTRecordReader::readCXXBaseSpecifier() {
8820   bool isVirtual = readBool();
8821   bool isBaseOfClass = readBool();
8822   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8823   bool inheritConstructors = readBool();
8824   TypeSourceInfo *TInfo = readTypeSourceInfo();
8825   SourceRange Range = readSourceRange();
8826   SourceLocation EllipsisLoc = readSourceLocation();
8827   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8828                           EllipsisLoc);
8829   Result.setInheritConstructors(inheritConstructors);
8830   return Result;
8831 }
8832 
8833 CXXCtorInitializer **
8834 ASTRecordReader::readCXXCtorInitializers() {
8835   ASTContext &Context = getContext();
8836   unsigned NumInitializers = readInt();
8837   assert(NumInitializers && "wrote ctor initializers but have no inits");
8838   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8839   for (unsigned i = 0; i != NumInitializers; ++i) {
8840     TypeSourceInfo *TInfo = nullptr;
8841     bool IsBaseVirtual = false;
8842     FieldDecl *Member = nullptr;
8843     IndirectFieldDecl *IndirectMember = nullptr;
8844 
8845     CtorInitializerType Type = (CtorInitializerType) readInt();
8846     switch (Type) {
8847     case CTOR_INITIALIZER_BASE:
8848       TInfo = readTypeSourceInfo();
8849       IsBaseVirtual = readBool();
8850       break;
8851 
8852     case CTOR_INITIALIZER_DELEGATING:
8853       TInfo = readTypeSourceInfo();
8854       break;
8855 
8856      case CTOR_INITIALIZER_MEMBER:
8857       Member = readDeclAs<FieldDecl>();
8858       break;
8859 
8860      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8861       IndirectMember = readDeclAs<IndirectFieldDecl>();
8862       break;
8863     }
8864 
8865     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8866     Expr *Init = readExpr();
8867     SourceLocation LParenLoc = readSourceLocation();
8868     SourceLocation RParenLoc = readSourceLocation();
8869 
8870     CXXCtorInitializer *BOMInit;
8871     if (Type == CTOR_INITIALIZER_BASE)
8872       BOMInit = new (Context)
8873           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8874                              RParenLoc, MemberOrEllipsisLoc);
8875     else if (Type == CTOR_INITIALIZER_DELEGATING)
8876       BOMInit = new (Context)
8877           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8878     else if (Member)
8879       BOMInit = new (Context)
8880           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8881                              Init, RParenLoc);
8882     else
8883       BOMInit = new (Context)
8884           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8885                              LParenLoc, Init, RParenLoc);
8886 
8887     if (/*IsWritten*/readBool()) {
8888       unsigned SourceOrder = readInt();
8889       BOMInit->setSourceOrder(SourceOrder);
8890     }
8891 
8892     CtorInitializers[i] = BOMInit;
8893   }
8894 
8895   return CtorInitializers;
8896 }
8897 
8898 NestedNameSpecifierLoc
8899 ASTRecordReader::readNestedNameSpecifierLoc() {
8900   ASTContext &Context = getContext();
8901   unsigned N = readInt();
8902   NestedNameSpecifierLocBuilder Builder;
8903   for (unsigned I = 0; I != N; ++I) {
8904     auto Kind = readNestedNameSpecifierKind();
8905     switch (Kind) {
8906     case NestedNameSpecifier::Identifier: {
8907       IdentifierInfo *II = readIdentifier();
8908       SourceRange Range = readSourceRange();
8909       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8910       break;
8911     }
8912 
8913     case NestedNameSpecifier::Namespace: {
8914       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8915       SourceRange Range = readSourceRange();
8916       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8917       break;
8918     }
8919 
8920     case NestedNameSpecifier::NamespaceAlias: {
8921       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8922       SourceRange Range = readSourceRange();
8923       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8924       break;
8925     }
8926 
8927     case NestedNameSpecifier::TypeSpec:
8928     case NestedNameSpecifier::TypeSpecWithTemplate: {
8929       bool Template = readBool();
8930       TypeSourceInfo *T = readTypeSourceInfo();
8931       if (!T)
8932         return NestedNameSpecifierLoc();
8933       SourceLocation ColonColonLoc = readSourceLocation();
8934 
8935       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8936       Builder.Extend(Context,
8937                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8938                      T->getTypeLoc(), ColonColonLoc);
8939       break;
8940     }
8941 
8942     case NestedNameSpecifier::Global: {
8943       SourceLocation ColonColonLoc = readSourceLocation();
8944       Builder.MakeGlobal(Context, ColonColonLoc);
8945       break;
8946     }
8947 
8948     case NestedNameSpecifier::Super: {
8949       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8950       SourceRange Range = readSourceRange();
8951       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8952       break;
8953     }
8954     }
8955   }
8956 
8957   return Builder.getWithLocInContext(Context);
8958 }
8959 
8960 SourceRange
8961 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8962                            unsigned &Idx) {
8963   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8964   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8965   return SourceRange(beg, end);
8966 }
8967 
8968 /// Read a floating-point value
8969 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8970   return llvm::APFloat(Sem, readAPInt());
8971 }
8972 
8973 // Read a string
8974 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8975   unsigned Len = Record[Idx++];
8976   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8977   Idx += Len;
8978   return Result;
8979 }
8980 
8981 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
8982                                 unsigned &Idx) {
8983   std::string Filename = ReadString(Record, Idx);
8984   ResolveImportedPath(F, Filename);
8985   return Filename;
8986 }
8987 
8988 std::string ASTReader::ReadPath(StringRef BaseDirectory,
8989                                 const RecordData &Record, unsigned &Idx) {
8990   std::string Filename = ReadString(Record, Idx);
8991   if (!BaseDirectory.empty())
8992     ResolveImportedPath(Filename, BaseDirectory);
8993   return Filename;
8994 }
8995 
8996 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
8997                                          unsigned &Idx) {
8998   unsigned Major = Record[Idx++];
8999   unsigned Minor = Record[Idx++];
9000   unsigned Subminor = Record[Idx++];
9001   if (Minor == 0)
9002     return VersionTuple(Major);
9003   if (Subminor == 0)
9004     return VersionTuple(Major, Minor - 1);
9005   return VersionTuple(Major, Minor - 1, Subminor - 1);
9006 }
9007 
9008 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9009                                           const RecordData &Record,
9010                                           unsigned &Idx) {
9011   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9012   return CXXTemporary::Create(getContext(), Decl);
9013 }
9014 
9015 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9016   return Diag(CurrentImportLoc, DiagID);
9017 }
9018 
9019 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9020   return Diags.Report(Loc, DiagID);
9021 }
9022 
9023 /// Retrieve the identifier table associated with the
9024 /// preprocessor.
9025 IdentifierTable &ASTReader::getIdentifierTable() {
9026   return PP.getIdentifierTable();
9027 }
9028 
9029 /// Record that the given ID maps to the given switch-case
9030 /// statement.
9031 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9032   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9033          "Already have a SwitchCase with this ID");
9034   (*CurrSwitchCaseStmts)[ID] = SC;
9035 }
9036 
9037 /// Retrieve the switch-case statement with the given ID.
9038 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9039   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9040   return (*CurrSwitchCaseStmts)[ID];
9041 }
9042 
9043 void ASTReader::ClearSwitchCaseIDs() {
9044   CurrSwitchCaseStmts->clear();
9045 }
9046 
9047 void ASTReader::ReadComments() {
9048   ASTContext &Context = getContext();
9049   std::vector<RawComment *> Comments;
9050   for (SmallVectorImpl<std::pair<BitstreamCursor,
9051                                  serialization::ModuleFile *>>::iterator
9052        I = CommentsCursors.begin(),
9053        E = CommentsCursors.end();
9054        I != E; ++I) {
9055     Comments.clear();
9056     BitstreamCursor &Cursor = I->first;
9057     serialization::ModuleFile &F = *I->second;
9058     SavedStreamPosition SavedPosition(Cursor);
9059 
9060     RecordData Record;
9061     while (true) {
9062       Expected<llvm::BitstreamEntry> MaybeEntry =
9063           Cursor.advanceSkippingSubblocks(
9064               BitstreamCursor::AF_DontPopBlockAtEnd);
9065       if (!MaybeEntry) {
9066         Error(MaybeEntry.takeError());
9067         return;
9068       }
9069       llvm::BitstreamEntry Entry = MaybeEntry.get();
9070 
9071       switch (Entry.Kind) {
9072       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9073       case llvm::BitstreamEntry::Error:
9074         Error("malformed block record in AST file");
9075         return;
9076       case llvm::BitstreamEntry::EndBlock:
9077         goto NextCursor;
9078       case llvm::BitstreamEntry::Record:
9079         // The interesting case.
9080         break;
9081       }
9082 
9083       // Read a record.
9084       Record.clear();
9085       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9086       if (!MaybeComment) {
9087         Error(MaybeComment.takeError());
9088         return;
9089       }
9090       switch ((CommentRecordTypes)MaybeComment.get()) {
9091       case COMMENTS_RAW_COMMENT: {
9092         unsigned Idx = 0;
9093         SourceRange SR = ReadSourceRange(F, Record, Idx);
9094         RawComment::CommentKind Kind =
9095             (RawComment::CommentKind) Record[Idx++];
9096         bool IsTrailingComment = Record[Idx++];
9097         bool IsAlmostTrailingComment = Record[Idx++];
9098         Comments.push_back(new (Context) RawComment(
9099             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9100         break;
9101       }
9102       }
9103     }
9104   NextCursor:
9105     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9106         FileToOffsetToComment;
9107     for (RawComment *C : Comments) {
9108       SourceLocation CommentLoc = C->getBeginLoc();
9109       if (CommentLoc.isValid()) {
9110         std::pair<FileID, unsigned> Loc =
9111             SourceMgr.getDecomposedLoc(CommentLoc);
9112         if (Loc.first.isValid())
9113           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9114       }
9115     }
9116   }
9117 }
9118 
9119 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9120                                 bool IncludeSystem, bool Complain,
9121                     llvm::function_ref<void(const serialization::InputFile &IF,
9122                                             bool isSystem)> Visitor) {
9123   unsigned NumUserInputs = MF.NumUserInputFiles;
9124   unsigned NumInputs = MF.InputFilesLoaded.size();
9125   assert(NumUserInputs <= NumInputs);
9126   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9127   for (unsigned I = 0; I < N; ++I) {
9128     bool IsSystem = I >= NumUserInputs;
9129     InputFile IF = getInputFile(MF, I+1, Complain);
9130     Visitor(IF, IsSystem);
9131   }
9132 }
9133 
9134 void ASTReader::visitTopLevelModuleMaps(
9135     serialization::ModuleFile &MF,
9136     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9137   unsigned NumInputs = MF.InputFilesLoaded.size();
9138   for (unsigned I = 0; I < NumInputs; ++I) {
9139     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9140     if (IFI.TopLevelModuleMap)
9141       // FIXME: This unnecessarily re-reads the InputFileInfo.
9142       if (auto FE = getInputFile(MF, I + 1).getFile())
9143         Visitor(FE);
9144   }
9145 }
9146 
9147 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9148   // If we know the owning module, use it.
9149   if (Module *M = D->getImportedOwningModule())
9150     return M->getFullModuleName();
9151 
9152   // Otherwise, use the name of the top-level module the decl is within.
9153   if (ModuleFile *M = getOwningModuleFile(D))
9154     return M->ModuleName;
9155 
9156   // Not from a module.
9157   return {};
9158 }
9159 
9160 void ASTReader::finishPendingActions() {
9161   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9162          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9163          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9164          !PendingUpdateRecords.empty()) {
9165     // If any identifiers with corresponding top-level declarations have
9166     // been loaded, load those declarations now.
9167     using TopLevelDeclsMap =
9168         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9169     TopLevelDeclsMap TopLevelDecls;
9170 
9171     while (!PendingIdentifierInfos.empty()) {
9172       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9173       SmallVector<uint32_t, 4> DeclIDs =
9174           std::move(PendingIdentifierInfos.back().second);
9175       PendingIdentifierInfos.pop_back();
9176 
9177       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9178     }
9179 
9180     // Load each function type that we deferred loading because it was a
9181     // deduced type that might refer to a local type declared within itself.
9182     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9183       auto *FD = PendingFunctionTypes[I].first;
9184       FD->setType(GetType(PendingFunctionTypes[I].second));
9185 
9186       // If we gave a function a deduced return type, remember that we need to
9187       // propagate that along the redeclaration chain.
9188       auto *DT = FD->getReturnType()->getContainedDeducedType();
9189       if (DT && DT->isDeduced())
9190         PendingDeducedTypeUpdates.insert(
9191             {FD->getCanonicalDecl(), FD->getReturnType()});
9192     }
9193     PendingFunctionTypes.clear();
9194 
9195     // For each decl chain that we wanted to complete while deserializing, mark
9196     // it as "still needs to be completed".
9197     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9198       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9199     }
9200     PendingIncompleteDeclChains.clear();
9201 
9202     // Load pending declaration chains.
9203     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9204       loadPendingDeclChain(PendingDeclChains[I].first,
9205                            PendingDeclChains[I].second);
9206     PendingDeclChains.clear();
9207 
9208     // Make the most recent of the top-level declarations visible.
9209     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9210            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9211       IdentifierInfo *II = TLD->first;
9212       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9213         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9214       }
9215     }
9216 
9217     // Load any pending macro definitions.
9218     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9219       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9220       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9221       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9222       // Initialize the macro history from chained-PCHs ahead of module imports.
9223       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9224            ++IDIdx) {
9225         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9226         if (!Info.M->isModule())
9227           resolvePendingMacro(II, Info);
9228       }
9229       // Handle module imports.
9230       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9231            ++IDIdx) {
9232         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9233         if (Info.M->isModule())
9234           resolvePendingMacro(II, Info);
9235       }
9236     }
9237     PendingMacroIDs.clear();
9238 
9239     // Wire up the DeclContexts for Decls that we delayed setting until
9240     // recursive loading is completed.
9241     while (!PendingDeclContextInfos.empty()) {
9242       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9243       PendingDeclContextInfos.pop_front();
9244       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9245       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9246       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9247     }
9248 
9249     // Perform any pending declaration updates.
9250     while (!PendingUpdateRecords.empty()) {
9251       auto Update = PendingUpdateRecords.pop_back_val();
9252       ReadingKindTracker ReadingKind(Read_Decl, *this);
9253       loadDeclUpdateRecords(Update);
9254     }
9255   }
9256 
9257   // At this point, all update records for loaded decls are in place, so any
9258   // fake class definitions should have become real.
9259   assert(PendingFakeDefinitionData.empty() &&
9260          "faked up a class definition but never saw the real one");
9261 
9262   // If we deserialized any C++ or Objective-C class definitions, any
9263   // Objective-C protocol definitions, or any redeclarable templates, make sure
9264   // that all redeclarations point to the definitions. Note that this can only
9265   // happen now, after the redeclaration chains have been fully wired.
9266   for (Decl *D : PendingDefinitions) {
9267     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9268       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9269         // Make sure that the TagType points at the definition.
9270         const_cast<TagType*>(TagT)->decl = TD;
9271       }
9272 
9273       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9274         for (auto *R = getMostRecentExistingDecl(RD); R;
9275              R = R->getPreviousDecl()) {
9276           assert((R == D) ==
9277                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9278                  "declaration thinks it's the definition but it isn't");
9279           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9280         }
9281       }
9282 
9283       continue;
9284     }
9285 
9286     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9287       // Make sure that the ObjCInterfaceType points at the definition.
9288       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9289         ->Decl = ID;
9290 
9291       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9292         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9293 
9294       continue;
9295     }
9296 
9297     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9298       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9299         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9300 
9301       continue;
9302     }
9303 
9304     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9305     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9306       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9307   }
9308   PendingDefinitions.clear();
9309 
9310   // Load the bodies of any functions or methods we've encountered. We do
9311   // this now (delayed) so that we can be sure that the declaration chains
9312   // have been fully wired up (hasBody relies on this).
9313   // FIXME: We shouldn't require complete redeclaration chains here.
9314   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9315                                PBEnd = PendingBodies.end();
9316        PB != PBEnd; ++PB) {
9317     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9318       // For a function defined inline within a class template, force the
9319       // canonical definition to be the one inside the canonical definition of
9320       // the template. This ensures that we instantiate from a correct view
9321       // of the template.
9322       //
9323       // Sadly we can't do this more generally: we can't be sure that all
9324       // copies of an arbitrary class definition will have the same members
9325       // defined (eg, some member functions may not be instantiated, and some
9326       // special members may or may not have been implicitly defined).
9327       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9328         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9329           continue;
9330 
9331       // FIXME: Check for =delete/=default?
9332       // FIXME: Complain about ODR violations here?
9333       const FunctionDecl *Defn = nullptr;
9334       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9335         FD->setLazyBody(PB->second);
9336       } else {
9337         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9338         mergeDefinitionVisibility(NonConstDefn, FD);
9339 
9340         if (!FD->isLateTemplateParsed() &&
9341             !NonConstDefn->isLateTemplateParsed() &&
9342             FD->getODRHash() != NonConstDefn->getODRHash()) {
9343           if (!isa<CXXMethodDecl>(FD)) {
9344             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9345           } else if (FD->getLexicalParent()->isFileContext() &&
9346                      NonConstDefn->getLexicalParent()->isFileContext()) {
9347             // Only diagnose out-of-line method definitions.  If they are
9348             // in class definitions, then an error will be generated when
9349             // processing the class bodies.
9350             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9351           }
9352         }
9353       }
9354       continue;
9355     }
9356 
9357     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9358     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9359       MD->setLazyBody(PB->second);
9360   }
9361   PendingBodies.clear();
9362 
9363   // Do some cleanup.
9364   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9365     getContext().deduplicateMergedDefinitonsFor(ND);
9366   PendingMergedDefinitionsToDeduplicate.clear();
9367 }
9368 
9369 void ASTReader::diagnoseOdrViolations() {
9370   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9371       PendingFunctionOdrMergeFailures.empty() &&
9372       PendingEnumOdrMergeFailures.empty())
9373     return;
9374 
9375   // Trigger the import of the full definition of each class that had any
9376   // odr-merging problems, so we can produce better diagnostics for them.
9377   // These updates may in turn find and diagnose some ODR failures, so take
9378   // ownership of the set first.
9379   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9380   PendingOdrMergeFailures.clear();
9381   for (auto &Merge : OdrMergeFailures) {
9382     Merge.first->buildLookup();
9383     Merge.first->decls_begin();
9384     Merge.first->bases_begin();
9385     Merge.first->vbases_begin();
9386     for (auto &RecordPair : Merge.second) {
9387       auto *RD = RecordPair.first;
9388       RD->decls_begin();
9389       RD->bases_begin();
9390       RD->vbases_begin();
9391     }
9392   }
9393 
9394   // Trigger the import of functions.
9395   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9396   PendingFunctionOdrMergeFailures.clear();
9397   for (auto &Merge : FunctionOdrMergeFailures) {
9398     Merge.first->buildLookup();
9399     Merge.first->decls_begin();
9400     Merge.first->getBody();
9401     for (auto &FD : Merge.second) {
9402       FD->buildLookup();
9403       FD->decls_begin();
9404       FD->getBody();
9405     }
9406   }
9407 
9408   // Trigger the import of enums.
9409   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9410   PendingEnumOdrMergeFailures.clear();
9411   for (auto &Merge : EnumOdrMergeFailures) {
9412     Merge.first->decls_begin();
9413     for (auto &Enum : Merge.second) {
9414       Enum->decls_begin();
9415     }
9416   }
9417 
9418   // For each declaration from a merged context, check that the canonical
9419   // definition of that context also contains a declaration of the same
9420   // entity.
9421   //
9422   // Caution: this loop does things that might invalidate iterators into
9423   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9424   while (!PendingOdrMergeChecks.empty()) {
9425     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9426 
9427     // FIXME: Skip over implicit declarations for now. This matters for things
9428     // like implicitly-declared special member functions. This isn't entirely
9429     // correct; we can end up with multiple unmerged declarations of the same
9430     // implicit entity.
9431     if (D->isImplicit())
9432       continue;
9433 
9434     DeclContext *CanonDef = D->getDeclContext();
9435 
9436     bool Found = false;
9437     const Decl *DCanon = D->getCanonicalDecl();
9438 
9439     for (auto RI : D->redecls()) {
9440       if (RI->getLexicalDeclContext() == CanonDef) {
9441         Found = true;
9442         break;
9443       }
9444     }
9445     if (Found)
9446       continue;
9447 
9448     // Quick check failed, time to do the slow thing. Note, we can't just
9449     // look up the name of D in CanonDef here, because the member that is
9450     // in CanonDef might not be found by name lookup (it might have been
9451     // replaced by a more recent declaration in the lookup table), and we
9452     // can't necessarily find it in the redeclaration chain because it might
9453     // be merely mergeable, not redeclarable.
9454     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9455     for (auto *CanonMember : CanonDef->decls()) {
9456       if (CanonMember->getCanonicalDecl() == DCanon) {
9457         // This can happen if the declaration is merely mergeable and not
9458         // actually redeclarable (we looked for redeclarations earlier).
9459         //
9460         // FIXME: We should be able to detect this more efficiently, without
9461         // pulling in all of the members of CanonDef.
9462         Found = true;
9463         break;
9464       }
9465       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9466         if (ND->getDeclName() == D->getDeclName())
9467           Candidates.push_back(ND);
9468     }
9469 
9470     if (!Found) {
9471       // The AST doesn't like TagDecls becoming invalid after they've been
9472       // completed. We only really need to mark FieldDecls as invalid here.
9473       if (!isa<TagDecl>(D))
9474         D->setInvalidDecl();
9475 
9476       // Ensure we don't accidentally recursively enter deserialization while
9477       // we're producing our diagnostic.
9478       Deserializing RecursionGuard(this);
9479 
9480       std::string CanonDefModule =
9481           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9482       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9483         << D << getOwningModuleNameForDiagnostic(D)
9484         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9485 
9486       if (Candidates.empty())
9487         Diag(cast<Decl>(CanonDef)->getLocation(),
9488              diag::note_module_odr_violation_no_possible_decls) << D;
9489       else {
9490         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9491           Diag(Candidates[I]->getLocation(),
9492                diag::note_module_odr_violation_possible_decl)
9493             << Candidates[I];
9494       }
9495 
9496       DiagnosedOdrMergeFailures.insert(CanonDef);
9497     }
9498   }
9499 
9500   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9501       EnumOdrMergeFailures.empty())
9502     return;
9503 
9504   // Ensure we don't accidentally recursively enter deserialization while
9505   // we're producing our diagnostics.
9506   Deserializing RecursionGuard(this);
9507 
9508   // Common code for hashing helpers.
9509   ODRHash Hash;
9510   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9511     Hash.clear();
9512     Hash.AddQualType(Ty);
9513     return Hash.CalculateHash();
9514   };
9515 
9516   auto ComputeODRHash = [&Hash](const Stmt *S) {
9517     assert(S);
9518     Hash.clear();
9519     Hash.AddStmt(S);
9520     return Hash.CalculateHash();
9521   };
9522 
9523   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9524     assert(D);
9525     Hash.clear();
9526     Hash.AddSubDecl(D);
9527     return Hash.CalculateHash();
9528   };
9529 
9530   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9531     Hash.clear();
9532     Hash.AddTemplateArgument(TA);
9533     return Hash.CalculateHash();
9534   };
9535 
9536   auto ComputeTemplateParameterListODRHash =
9537       [&Hash](const TemplateParameterList *TPL) {
9538         assert(TPL);
9539         Hash.clear();
9540         Hash.AddTemplateParameterList(TPL);
9541         return Hash.CalculateHash();
9542       };
9543 
9544   // Used with err_module_odr_violation_mismatch_decl and
9545   // note_module_odr_violation_mismatch_decl
9546   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9547   enum ODRMismatchDecl {
9548     EndOfClass,
9549     PublicSpecifer,
9550     PrivateSpecifer,
9551     ProtectedSpecifer,
9552     StaticAssert,
9553     Field,
9554     CXXMethod,
9555     TypeAlias,
9556     TypeDef,
9557     Var,
9558     Friend,
9559     FunctionTemplate,
9560     Other
9561   };
9562 
9563   // Used with err_module_odr_violation_mismatch_decl_diff and
9564   // note_module_odr_violation_mismatch_decl_diff
9565   enum ODRMismatchDeclDifference {
9566     StaticAssertCondition,
9567     StaticAssertMessage,
9568     StaticAssertOnlyMessage,
9569     FieldName,
9570     FieldTypeName,
9571     FieldSingleBitField,
9572     FieldDifferentWidthBitField,
9573     FieldSingleMutable,
9574     FieldSingleInitializer,
9575     FieldDifferentInitializers,
9576     MethodName,
9577     MethodDeleted,
9578     MethodDefaulted,
9579     MethodVirtual,
9580     MethodStatic,
9581     MethodVolatile,
9582     MethodConst,
9583     MethodInline,
9584     MethodNumberParameters,
9585     MethodParameterType,
9586     MethodParameterName,
9587     MethodParameterSingleDefaultArgument,
9588     MethodParameterDifferentDefaultArgument,
9589     MethodNoTemplateArguments,
9590     MethodDifferentNumberTemplateArguments,
9591     MethodDifferentTemplateArgument,
9592     MethodSingleBody,
9593     MethodDifferentBody,
9594     TypedefName,
9595     TypedefType,
9596     VarName,
9597     VarType,
9598     VarSingleInitializer,
9599     VarDifferentInitializer,
9600     VarConstexpr,
9601     FriendTypeFunction,
9602     FriendType,
9603     FriendFunction,
9604     FunctionTemplateDifferentNumberParameters,
9605     FunctionTemplateParameterDifferentKind,
9606     FunctionTemplateParameterName,
9607     FunctionTemplateParameterSingleDefaultArgument,
9608     FunctionTemplateParameterDifferentDefaultArgument,
9609     FunctionTemplateParameterDifferentType,
9610     FunctionTemplatePackParameter,
9611   };
9612 
9613   // These lambdas have the common portions of the ODR diagnostics.  This
9614   // has the same return as Diag(), so addition parameters can be passed
9615   // in with operator<<
9616   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9617                                  SourceLocation Loc, SourceRange Range,
9618                                  ODRMismatchDeclDifference DiffType) {
9619     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9620            << FirstRecord << FirstModule.empty() << FirstModule << Range
9621            << DiffType;
9622   };
9623   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9624                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9625     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9626            << SecondModule << Range << DiffType;
9627   };
9628 
9629   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9630                        &ComputeQualTypeODRHash, &ComputeODRHash](
9631                           NamedDecl *FirstRecord, StringRef FirstModule,
9632                           StringRef SecondModule, FieldDecl *FirstField,
9633                           FieldDecl *SecondField) {
9634     IdentifierInfo *FirstII = FirstField->getIdentifier();
9635     IdentifierInfo *SecondII = SecondField->getIdentifier();
9636     if (FirstII->getName() != SecondII->getName()) {
9637       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9638                        FirstField->getSourceRange(), FieldName)
9639           << FirstII;
9640       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9641                       SecondField->getSourceRange(), FieldName)
9642           << SecondII;
9643 
9644       return true;
9645     }
9646 
9647     assert(getContext().hasSameType(FirstField->getType(),
9648                                     SecondField->getType()));
9649 
9650     QualType FirstType = FirstField->getType();
9651     QualType SecondType = SecondField->getType();
9652     if (ComputeQualTypeODRHash(FirstType) !=
9653         ComputeQualTypeODRHash(SecondType)) {
9654       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9655                        FirstField->getSourceRange(), FieldTypeName)
9656           << FirstII << FirstType;
9657       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9658                       SecondField->getSourceRange(), FieldTypeName)
9659           << SecondII << SecondType;
9660 
9661       return true;
9662     }
9663 
9664     const bool IsFirstBitField = FirstField->isBitField();
9665     const bool IsSecondBitField = SecondField->isBitField();
9666     if (IsFirstBitField != IsSecondBitField) {
9667       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9668                        FirstField->getSourceRange(), FieldSingleBitField)
9669           << FirstII << IsFirstBitField;
9670       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9671                       SecondField->getSourceRange(), FieldSingleBitField)
9672           << SecondII << IsSecondBitField;
9673       return true;
9674     }
9675 
9676     if (IsFirstBitField && IsSecondBitField) {
9677       unsigned FirstBitWidthHash =
9678           ComputeODRHash(FirstField->getBitWidth());
9679       unsigned SecondBitWidthHash =
9680           ComputeODRHash(SecondField->getBitWidth());
9681       if (FirstBitWidthHash != SecondBitWidthHash) {
9682         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9683                          FirstField->getSourceRange(),
9684                          FieldDifferentWidthBitField)
9685             << FirstII << FirstField->getBitWidth()->getSourceRange();
9686         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9687                         SecondField->getSourceRange(),
9688                         FieldDifferentWidthBitField)
9689             << SecondII << SecondField->getBitWidth()->getSourceRange();
9690         return true;
9691       }
9692     }
9693 
9694     if (!PP.getLangOpts().CPlusPlus)
9695       return false;
9696 
9697     const bool IsFirstMutable = FirstField->isMutable();
9698     const bool IsSecondMutable = SecondField->isMutable();
9699     if (IsFirstMutable != IsSecondMutable) {
9700       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9701                        FirstField->getSourceRange(), FieldSingleMutable)
9702           << FirstII << IsFirstMutable;
9703       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9704                       SecondField->getSourceRange(), FieldSingleMutable)
9705           << SecondII << IsSecondMutable;
9706       return true;
9707     }
9708 
9709     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9710     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9711     if ((!FirstInitializer && SecondInitializer) ||
9712         (FirstInitializer && !SecondInitializer)) {
9713       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9714                        FirstField->getSourceRange(), FieldSingleInitializer)
9715           << FirstII << (FirstInitializer != nullptr);
9716       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9717                       SecondField->getSourceRange(), FieldSingleInitializer)
9718           << SecondII << (SecondInitializer != nullptr);
9719       return true;
9720     }
9721 
9722     if (FirstInitializer && SecondInitializer) {
9723       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9724       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9725       if (FirstInitHash != SecondInitHash) {
9726         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9727                          FirstField->getSourceRange(),
9728                          FieldDifferentInitializers)
9729             << FirstII << FirstInitializer->getSourceRange();
9730         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9731                         SecondField->getSourceRange(),
9732                         FieldDifferentInitializers)
9733             << SecondII << SecondInitializer->getSourceRange();
9734         return true;
9735       }
9736     }
9737 
9738     return false;
9739   };
9740 
9741   auto ODRDiagTypeDefOrAlias =
9742       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9743           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9744           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9745           bool IsTypeAlias) {
9746         auto FirstName = FirstTD->getDeclName();
9747         auto SecondName = SecondTD->getDeclName();
9748         if (FirstName != SecondName) {
9749           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9750                            FirstTD->getSourceRange(), TypedefName)
9751               << IsTypeAlias << FirstName;
9752           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9753                           SecondTD->getSourceRange(), TypedefName)
9754               << IsTypeAlias << SecondName;
9755           return true;
9756         }
9757 
9758         QualType FirstType = FirstTD->getUnderlyingType();
9759         QualType SecondType = SecondTD->getUnderlyingType();
9760         if (ComputeQualTypeODRHash(FirstType) !=
9761             ComputeQualTypeODRHash(SecondType)) {
9762           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9763                            FirstTD->getSourceRange(), TypedefType)
9764               << IsTypeAlias << FirstName << FirstType;
9765           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9766                           SecondTD->getSourceRange(), TypedefType)
9767               << IsTypeAlias << SecondName << SecondType;
9768           return true;
9769         }
9770 
9771         return false;
9772   };
9773 
9774   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9775                      &ComputeQualTypeODRHash, &ComputeODRHash,
9776                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9777                            StringRef SecondModule, VarDecl *FirstVD,
9778                            VarDecl *SecondVD) {
9779     auto FirstName = FirstVD->getDeclName();
9780     auto SecondName = SecondVD->getDeclName();
9781     if (FirstName != SecondName) {
9782       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9783                        FirstVD->getSourceRange(), VarName)
9784           << FirstName;
9785       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9786                       SecondVD->getSourceRange(), VarName)
9787           << SecondName;
9788       return true;
9789     }
9790 
9791     QualType FirstType = FirstVD->getType();
9792     QualType SecondType = SecondVD->getType();
9793     if (ComputeQualTypeODRHash(FirstType) !=
9794         ComputeQualTypeODRHash(SecondType)) {
9795       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9796                        FirstVD->getSourceRange(), VarType)
9797           << FirstName << FirstType;
9798       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9799                       SecondVD->getSourceRange(), VarType)
9800           << SecondName << SecondType;
9801       return true;
9802     }
9803 
9804     if (!PP.getLangOpts().CPlusPlus)
9805       return false;
9806 
9807     const Expr *FirstInit = FirstVD->getInit();
9808     const Expr *SecondInit = SecondVD->getInit();
9809     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9810       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9811                        FirstVD->getSourceRange(), VarSingleInitializer)
9812           << FirstName << (FirstInit == nullptr)
9813           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9814       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9815                       SecondVD->getSourceRange(), VarSingleInitializer)
9816           << SecondName << (SecondInit == nullptr)
9817           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9818       return true;
9819     }
9820 
9821     if (FirstInit && SecondInit &&
9822         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9823       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9824                        FirstVD->getSourceRange(), VarDifferentInitializer)
9825           << FirstName << FirstInit->getSourceRange();
9826       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9827                       SecondVD->getSourceRange(), VarDifferentInitializer)
9828           << SecondName << SecondInit->getSourceRange();
9829       return true;
9830     }
9831 
9832     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9833     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9834     if (FirstIsConstexpr != SecondIsConstexpr) {
9835       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9836                        FirstVD->getSourceRange(), VarConstexpr)
9837           << FirstName << FirstIsConstexpr;
9838       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9839                       SecondVD->getSourceRange(), VarConstexpr)
9840           << SecondName << SecondIsConstexpr;
9841       return true;
9842     }
9843     return false;
9844   };
9845 
9846   auto DifferenceSelector = [](Decl *D) {
9847     assert(D && "valid Decl required");
9848     switch (D->getKind()) {
9849     default:
9850       return Other;
9851     case Decl::AccessSpec:
9852       switch (D->getAccess()) {
9853       case AS_public:
9854         return PublicSpecifer;
9855       case AS_private:
9856         return PrivateSpecifer;
9857       case AS_protected:
9858         return ProtectedSpecifer;
9859       case AS_none:
9860         break;
9861       }
9862       llvm_unreachable("Invalid access specifier");
9863     case Decl::StaticAssert:
9864       return StaticAssert;
9865     case Decl::Field:
9866       return Field;
9867     case Decl::CXXMethod:
9868     case Decl::CXXConstructor:
9869     case Decl::CXXDestructor:
9870       return CXXMethod;
9871     case Decl::TypeAlias:
9872       return TypeAlias;
9873     case Decl::Typedef:
9874       return TypeDef;
9875     case Decl::Var:
9876       return Var;
9877     case Decl::Friend:
9878       return Friend;
9879     case Decl::FunctionTemplate:
9880       return FunctionTemplate;
9881     }
9882   };
9883 
9884   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9885   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9886                                                  RecordDecl *Record,
9887                                                  const DeclContext *DC) {
9888     for (auto *D : Record->decls()) {
9889       if (!ODRHash::isDeclToBeProcessed(D, DC))
9890         continue;
9891       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9892     }
9893   };
9894 
9895   struct DiffResult {
9896     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9897     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9898   };
9899 
9900   // If there is a diagnoseable difference, FirstDiffType and
9901   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9902   // filled in if not EndOfClass.
9903   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9904                                              DeclHashes &SecondHashes) {
9905     DiffResult DR;
9906     auto FirstIt = FirstHashes.begin();
9907     auto SecondIt = SecondHashes.begin();
9908     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9909       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9910           FirstIt->second == SecondIt->second) {
9911         ++FirstIt;
9912         ++SecondIt;
9913         continue;
9914       }
9915 
9916       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9917       DR.SecondDecl =
9918           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9919 
9920       DR.FirstDiffType =
9921           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9922       DR.SecondDiffType =
9923           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9924       return DR;
9925     }
9926     return DR;
9927   };
9928 
9929   // Use this to diagnose that an unexpected Decl was encountered
9930   // or no difference was detected. This causes a generic error
9931   // message to be emitted.
9932   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9933                                       StringRef FirstModule,
9934                                       NamedDecl *SecondRecord,
9935                                       StringRef SecondModule) {
9936     Diag(FirstRecord->getLocation(),
9937          diag::err_module_odr_violation_different_definitions)
9938         << FirstRecord << FirstModule.empty() << FirstModule;
9939 
9940     if (DR.FirstDecl) {
9941       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9942           << FirstRecord << DR.FirstDecl->getSourceRange();
9943     }
9944 
9945     Diag(SecondRecord->getLocation(),
9946          diag::note_module_odr_violation_different_definitions)
9947         << SecondModule;
9948 
9949     if (DR.SecondDecl) {
9950       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9951           << DR.SecondDecl->getSourceRange();
9952     }
9953   };
9954 
9955   auto DiagnoseODRMismatch =
9956       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9957              NamedDecl *SecondRecord, StringRef SecondModule) {
9958         SourceLocation FirstLoc;
9959         SourceRange FirstRange;
9960         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9961         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9962           FirstLoc = FirstTag->getBraceRange().getEnd();
9963         } else {
9964           FirstLoc = DR.FirstDecl->getLocation();
9965           FirstRange = DR.FirstDecl->getSourceRange();
9966         }
9967         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9968             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9969             << DR.FirstDiffType;
9970 
9971         SourceLocation SecondLoc;
9972         SourceRange SecondRange;
9973         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9974         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9975           SecondLoc = SecondTag->getBraceRange().getEnd();
9976         } else {
9977           SecondLoc = DR.SecondDecl->getLocation();
9978           SecondRange = DR.SecondDecl->getSourceRange();
9979         }
9980         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
9981             << SecondModule << SecondRange << DR.SecondDiffType;
9982       };
9983 
9984   // Issue any pending ODR-failure diagnostics.
9985   for (auto &Merge : OdrMergeFailures) {
9986     // If we've already pointed out a specific problem with this class, don't
9987     // bother issuing a general "something's different" diagnostic.
9988     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
9989       continue;
9990 
9991     bool Diagnosed = false;
9992     CXXRecordDecl *FirstRecord = Merge.first;
9993     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
9994     for (auto &RecordPair : Merge.second) {
9995       CXXRecordDecl *SecondRecord = RecordPair.first;
9996       // Multiple different declarations got merged together; tell the user
9997       // where they came from.
9998       if (FirstRecord == SecondRecord)
9999         continue;
10000 
10001       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10002 
10003       auto *FirstDD = FirstRecord->DefinitionData;
10004       auto *SecondDD = RecordPair.second;
10005 
10006       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10007 
10008       // Diagnostics from DefinitionData are emitted here.
10009       if (FirstDD != SecondDD) {
10010         enum ODRDefinitionDataDifference {
10011           NumBases,
10012           NumVBases,
10013           BaseType,
10014           BaseVirtual,
10015           BaseAccess,
10016         };
10017         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10018                                  this](SourceLocation Loc, SourceRange Range,
10019                                        ODRDefinitionDataDifference DiffType) {
10020           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10021                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10022                  << DiffType;
10023         };
10024         auto ODRDiagBaseNote = [&SecondModule,
10025                                 this](SourceLocation Loc, SourceRange Range,
10026                                       ODRDefinitionDataDifference DiffType) {
10027           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10028                  << SecondModule << Range << DiffType;
10029         };
10030 
10031         unsigned FirstNumBases = FirstDD->NumBases;
10032         unsigned FirstNumVBases = FirstDD->NumVBases;
10033         unsigned SecondNumBases = SecondDD->NumBases;
10034         unsigned SecondNumVBases = SecondDD->NumVBases;
10035 
10036         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10037           unsigned NumBases = DD->NumBases;
10038           if (NumBases == 0) return SourceRange();
10039           auto bases = DD->bases();
10040           return SourceRange(bases[0].getBeginLoc(),
10041                              bases[NumBases - 1].getEndLoc());
10042         };
10043 
10044         if (FirstNumBases != SecondNumBases) {
10045           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10046                            NumBases)
10047               << FirstNumBases;
10048           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10049                           NumBases)
10050               << SecondNumBases;
10051           Diagnosed = true;
10052           break;
10053         }
10054 
10055         if (FirstNumVBases != SecondNumVBases) {
10056           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10057                            NumVBases)
10058               << FirstNumVBases;
10059           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10060                           NumVBases)
10061               << SecondNumVBases;
10062           Diagnosed = true;
10063           break;
10064         }
10065 
10066         auto FirstBases = FirstDD->bases();
10067         auto SecondBases = SecondDD->bases();
10068         unsigned i = 0;
10069         for (i = 0; i < FirstNumBases; ++i) {
10070           auto FirstBase = FirstBases[i];
10071           auto SecondBase = SecondBases[i];
10072           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10073               ComputeQualTypeODRHash(SecondBase.getType())) {
10074             ODRDiagBaseError(FirstRecord->getLocation(),
10075                              FirstBase.getSourceRange(), BaseType)
10076                 << (i + 1) << FirstBase.getType();
10077             ODRDiagBaseNote(SecondRecord->getLocation(),
10078                             SecondBase.getSourceRange(), BaseType)
10079                 << (i + 1) << SecondBase.getType();
10080             break;
10081           }
10082 
10083           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10084             ODRDiagBaseError(FirstRecord->getLocation(),
10085                              FirstBase.getSourceRange(), BaseVirtual)
10086                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10087             ODRDiagBaseNote(SecondRecord->getLocation(),
10088                             SecondBase.getSourceRange(), BaseVirtual)
10089                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10090             break;
10091           }
10092 
10093           if (FirstBase.getAccessSpecifierAsWritten() !=
10094               SecondBase.getAccessSpecifierAsWritten()) {
10095             ODRDiagBaseError(FirstRecord->getLocation(),
10096                              FirstBase.getSourceRange(), BaseAccess)
10097                 << (i + 1) << FirstBase.getType()
10098                 << (int)FirstBase.getAccessSpecifierAsWritten();
10099             ODRDiagBaseNote(SecondRecord->getLocation(),
10100                             SecondBase.getSourceRange(), BaseAccess)
10101                 << (i + 1) << SecondBase.getType()
10102                 << (int)SecondBase.getAccessSpecifierAsWritten();
10103             break;
10104           }
10105         }
10106 
10107         if (i != FirstNumBases) {
10108           Diagnosed = true;
10109           break;
10110         }
10111       }
10112 
10113       const ClassTemplateDecl *FirstTemplate =
10114           FirstRecord->getDescribedClassTemplate();
10115       const ClassTemplateDecl *SecondTemplate =
10116           SecondRecord->getDescribedClassTemplate();
10117 
10118       assert(!FirstTemplate == !SecondTemplate &&
10119              "Both pointers should be null or non-null");
10120 
10121       enum ODRTemplateDifference {
10122         ParamEmptyName,
10123         ParamName,
10124         ParamSingleDefaultArgument,
10125         ParamDifferentDefaultArgument,
10126       };
10127 
10128       if (FirstTemplate && SecondTemplate) {
10129         DeclHashes FirstTemplateHashes;
10130         DeclHashes SecondTemplateHashes;
10131 
10132         auto PopulateTemplateParameterHashs =
10133             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10134                                      const ClassTemplateDecl *TD) {
10135               for (auto *D : TD->getTemplateParameters()->asArray()) {
10136                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10137               }
10138             };
10139 
10140         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10141         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10142 
10143         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10144                "Number of template parameters should be equal.");
10145 
10146         auto FirstIt = FirstTemplateHashes.begin();
10147         auto FirstEnd = FirstTemplateHashes.end();
10148         auto SecondIt = SecondTemplateHashes.begin();
10149         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10150           if (FirstIt->second == SecondIt->second)
10151             continue;
10152 
10153           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10154                                           SourceLocation Loc, SourceRange Range,
10155                                           ODRTemplateDifference DiffType) {
10156             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10157                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10158                    << DiffType;
10159           };
10160           auto ODRDiagTemplateNote = [&SecondModule, this](
10161                                          SourceLocation Loc, SourceRange Range,
10162                                          ODRTemplateDifference DiffType) {
10163             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10164                    << SecondModule << Range << DiffType;
10165           };
10166 
10167           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10168           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10169 
10170           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10171                  "Parameter Decl's should be the same kind.");
10172 
10173           DeclarationName FirstName = FirstDecl->getDeclName();
10174           DeclarationName SecondName = SecondDecl->getDeclName();
10175 
10176           if (FirstName != SecondName) {
10177             const bool FirstNameEmpty =
10178                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10179             const bool SecondNameEmpty =
10180                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10181             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10182                    "Both template parameters cannot be unnamed.");
10183             ODRDiagTemplateError(FirstDecl->getLocation(),
10184                                  FirstDecl->getSourceRange(),
10185                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10186                 << FirstName;
10187             ODRDiagTemplateNote(SecondDecl->getLocation(),
10188                                 SecondDecl->getSourceRange(),
10189                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10190                 << SecondName;
10191             break;
10192           }
10193 
10194           switch (FirstDecl->getKind()) {
10195           default:
10196             llvm_unreachable("Invalid template parameter type.");
10197           case Decl::TemplateTypeParm: {
10198             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10199             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10200             const bool HasFirstDefaultArgument =
10201                 FirstParam->hasDefaultArgument() &&
10202                 !FirstParam->defaultArgumentWasInherited();
10203             const bool HasSecondDefaultArgument =
10204                 SecondParam->hasDefaultArgument() &&
10205                 !SecondParam->defaultArgumentWasInherited();
10206 
10207             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10208               ODRDiagTemplateError(FirstDecl->getLocation(),
10209                                    FirstDecl->getSourceRange(),
10210                                    ParamSingleDefaultArgument)
10211                   << HasFirstDefaultArgument;
10212               ODRDiagTemplateNote(SecondDecl->getLocation(),
10213                                   SecondDecl->getSourceRange(),
10214                                   ParamSingleDefaultArgument)
10215                   << HasSecondDefaultArgument;
10216               break;
10217             }
10218 
10219             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10220                    "Expecting default arguments.");
10221 
10222             ODRDiagTemplateError(FirstDecl->getLocation(),
10223                                  FirstDecl->getSourceRange(),
10224                                  ParamDifferentDefaultArgument);
10225             ODRDiagTemplateNote(SecondDecl->getLocation(),
10226                                 SecondDecl->getSourceRange(),
10227                                 ParamDifferentDefaultArgument);
10228 
10229             break;
10230           }
10231           case Decl::NonTypeTemplateParm: {
10232             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10233             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10234             const bool HasFirstDefaultArgument =
10235                 FirstParam->hasDefaultArgument() &&
10236                 !FirstParam->defaultArgumentWasInherited();
10237             const bool HasSecondDefaultArgument =
10238                 SecondParam->hasDefaultArgument() &&
10239                 !SecondParam->defaultArgumentWasInherited();
10240 
10241             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10242               ODRDiagTemplateError(FirstDecl->getLocation(),
10243                                    FirstDecl->getSourceRange(),
10244                                    ParamSingleDefaultArgument)
10245                   << HasFirstDefaultArgument;
10246               ODRDiagTemplateNote(SecondDecl->getLocation(),
10247                                   SecondDecl->getSourceRange(),
10248                                   ParamSingleDefaultArgument)
10249                   << HasSecondDefaultArgument;
10250               break;
10251             }
10252 
10253             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10254                    "Expecting default arguments.");
10255 
10256             ODRDiagTemplateError(FirstDecl->getLocation(),
10257                                  FirstDecl->getSourceRange(),
10258                                  ParamDifferentDefaultArgument);
10259             ODRDiagTemplateNote(SecondDecl->getLocation(),
10260                                 SecondDecl->getSourceRange(),
10261                                 ParamDifferentDefaultArgument);
10262 
10263             break;
10264           }
10265           case Decl::TemplateTemplateParm: {
10266             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10267             const auto *SecondParam =
10268                 cast<TemplateTemplateParmDecl>(SecondDecl);
10269             const bool HasFirstDefaultArgument =
10270                 FirstParam->hasDefaultArgument() &&
10271                 !FirstParam->defaultArgumentWasInherited();
10272             const bool HasSecondDefaultArgument =
10273                 SecondParam->hasDefaultArgument() &&
10274                 !SecondParam->defaultArgumentWasInherited();
10275 
10276             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10277               ODRDiagTemplateError(FirstDecl->getLocation(),
10278                                    FirstDecl->getSourceRange(),
10279                                    ParamSingleDefaultArgument)
10280                   << HasFirstDefaultArgument;
10281               ODRDiagTemplateNote(SecondDecl->getLocation(),
10282                                   SecondDecl->getSourceRange(),
10283                                   ParamSingleDefaultArgument)
10284                   << HasSecondDefaultArgument;
10285               break;
10286             }
10287 
10288             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10289                    "Expecting default arguments.");
10290 
10291             ODRDiagTemplateError(FirstDecl->getLocation(),
10292                                  FirstDecl->getSourceRange(),
10293                                  ParamDifferentDefaultArgument);
10294             ODRDiagTemplateNote(SecondDecl->getLocation(),
10295                                 SecondDecl->getSourceRange(),
10296                                 ParamDifferentDefaultArgument);
10297 
10298             break;
10299           }
10300           }
10301 
10302           break;
10303         }
10304 
10305         if (FirstIt != FirstEnd) {
10306           Diagnosed = true;
10307           break;
10308         }
10309       }
10310 
10311       DeclHashes FirstHashes;
10312       DeclHashes SecondHashes;
10313       const DeclContext *DC = FirstRecord;
10314       PopulateHashes(FirstHashes, FirstRecord, DC);
10315       PopulateHashes(SecondHashes, SecondRecord, DC);
10316 
10317       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10318       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10319       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10320       Decl *FirstDecl = DR.FirstDecl;
10321       Decl *SecondDecl = DR.SecondDecl;
10322 
10323       if (FirstDiffType == Other || SecondDiffType == Other) {
10324         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10325                               SecondModule);
10326         Diagnosed = true;
10327         break;
10328       }
10329 
10330       if (FirstDiffType != SecondDiffType) {
10331         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10332                             SecondModule);
10333         Diagnosed = true;
10334         break;
10335       }
10336 
10337       assert(FirstDiffType == SecondDiffType);
10338 
10339       switch (FirstDiffType) {
10340       case Other:
10341       case EndOfClass:
10342       case PublicSpecifer:
10343       case PrivateSpecifer:
10344       case ProtectedSpecifer:
10345         llvm_unreachable("Invalid diff type");
10346 
10347       case StaticAssert: {
10348         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10349         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10350 
10351         Expr *FirstExpr = FirstSA->getAssertExpr();
10352         Expr *SecondExpr = SecondSA->getAssertExpr();
10353         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10354         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10355         if (FirstODRHash != SecondODRHash) {
10356           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10357                            FirstExpr->getSourceRange(), StaticAssertCondition);
10358           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10359                           SecondExpr->getSourceRange(), StaticAssertCondition);
10360           Diagnosed = true;
10361           break;
10362         }
10363 
10364         StringLiteral *FirstStr = FirstSA->getMessage();
10365         StringLiteral *SecondStr = SecondSA->getMessage();
10366         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10367         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10368           SourceLocation FirstLoc, SecondLoc;
10369           SourceRange FirstRange, SecondRange;
10370           if (FirstStr) {
10371             FirstLoc = FirstStr->getBeginLoc();
10372             FirstRange = FirstStr->getSourceRange();
10373           } else {
10374             FirstLoc = FirstSA->getBeginLoc();
10375             FirstRange = FirstSA->getSourceRange();
10376           }
10377           if (SecondStr) {
10378             SecondLoc = SecondStr->getBeginLoc();
10379             SecondRange = SecondStr->getSourceRange();
10380           } else {
10381             SecondLoc = SecondSA->getBeginLoc();
10382             SecondRange = SecondSA->getSourceRange();
10383           }
10384           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10385                            StaticAssertOnlyMessage)
10386               << (FirstStr == nullptr);
10387           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10388                           StaticAssertOnlyMessage)
10389               << (SecondStr == nullptr);
10390           Diagnosed = true;
10391           break;
10392         }
10393 
10394         if (FirstStr && SecondStr &&
10395             FirstStr->getString() != SecondStr->getString()) {
10396           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10397                            FirstStr->getSourceRange(), StaticAssertMessage);
10398           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10399                           SecondStr->getSourceRange(), StaticAssertMessage);
10400           Diagnosed = true;
10401           break;
10402         }
10403         break;
10404       }
10405       case Field: {
10406         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10407                                  cast<FieldDecl>(FirstDecl),
10408                                  cast<FieldDecl>(SecondDecl));
10409         break;
10410       }
10411       case CXXMethod: {
10412         enum {
10413           DiagMethod,
10414           DiagConstructor,
10415           DiagDestructor,
10416         } FirstMethodType,
10417             SecondMethodType;
10418         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10419           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10420           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10421           return DiagMethod;
10422         };
10423         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10424         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10425         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10426         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10427         auto FirstName = FirstMethod->getDeclName();
10428         auto SecondName = SecondMethod->getDeclName();
10429         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10430           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10431                            FirstMethod->getSourceRange(), MethodName)
10432               << FirstMethodType << FirstName;
10433           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10434                           SecondMethod->getSourceRange(), MethodName)
10435               << SecondMethodType << SecondName;
10436 
10437           Diagnosed = true;
10438           break;
10439         }
10440 
10441         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10442         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10443         if (FirstDeleted != SecondDeleted) {
10444           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10445                            FirstMethod->getSourceRange(), MethodDeleted)
10446               << FirstMethodType << FirstName << FirstDeleted;
10447 
10448           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10449                           SecondMethod->getSourceRange(), MethodDeleted)
10450               << SecondMethodType << SecondName << SecondDeleted;
10451           Diagnosed = true;
10452           break;
10453         }
10454 
10455         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10456         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10457         if (FirstDefaulted != SecondDefaulted) {
10458           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10459                            FirstMethod->getSourceRange(), MethodDefaulted)
10460               << FirstMethodType << FirstName << FirstDefaulted;
10461 
10462           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10463                           SecondMethod->getSourceRange(), MethodDefaulted)
10464               << SecondMethodType << SecondName << SecondDefaulted;
10465           Diagnosed = true;
10466           break;
10467         }
10468 
10469         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10470         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10471         const bool FirstPure = FirstMethod->isPure();
10472         const bool SecondPure = SecondMethod->isPure();
10473         if ((FirstVirtual || SecondVirtual) &&
10474             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10475           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10476                            FirstMethod->getSourceRange(), MethodVirtual)
10477               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10478           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10479                           SecondMethod->getSourceRange(), MethodVirtual)
10480               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10481           Diagnosed = true;
10482           break;
10483         }
10484 
10485         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10486         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10487         // class needs to be checked instead.
10488         const auto FirstStorage = FirstMethod->getStorageClass();
10489         const auto SecondStorage = SecondMethod->getStorageClass();
10490         const bool FirstStatic = FirstStorage == SC_Static;
10491         const bool SecondStatic = SecondStorage == SC_Static;
10492         if (FirstStatic != SecondStatic) {
10493           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10494                            FirstMethod->getSourceRange(), MethodStatic)
10495               << FirstMethodType << FirstName << FirstStatic;
10496           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10497                           SecondMethod->getSourceRange(), MethodStatic)
10498               << SecondMethodType << SecondName << SecondStatic;
10499           Diagnosed = true;
10500           break;
10501         }
10502 
10503         const bool FirstVolatile = FirstMethod->isVolatile();
10504         const bool SecondVolatile = SecondMethod->isVolatile();
10505         if (FirstVolatile != SecondVolatile) {
10506           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10507                            FirstMethod->getSourceRange(), MethodVolatile)
10508               << FirstMethodType << FirstName << FirstVolatile;
10509           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10510                           SecondMethod->getSourceRange(), MethodVolatile)
10511               << SecondMethodType << SecondName << SecondVolatile;
10512           Diagnosed = true;
10513           break;
10514         }
10515 
10516         const bool FirstConst = FirstMethod->isConst();
10517         const bool SecondConst = SecondMethod->isConst();
10518         if (FirstConst != SecondConst) {
10519           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10520                            FirstMethod->getSourceRange(), MethodConst)
10521               << FirstMethodType << FirstName << FirstConst;
10522           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10523                           SecondMethod->getSourceRange(), MethodConst)
10524               << SecondMethodType << SecondName << SecondConst;
10525           Diagnosed = true;
10526           break;
10527         }
10528 
10529         const bool FirstInline = FirstMethod->isInlineSpecified();
10530         const bool SecondInline = SecondMethod->isInlineSpecified();
10531         if (FirstInline != SecondInline) {
10532           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10533                            FirstMethod->getSourceRange(), MethodInline)
10534               << FirstMethodType << FirstName << FirstInline;
10535           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10536                           SecondMethod->getSourceRange(), MethodInline)
10537               << SecondMethodType << SecondName << SecondInline;
10538           Diagnosed = true;
10539           break;
10540         }
10541 
10542         const unsigned FirstNumParameters = FirstMethod->param_size();
10543         const unsigned SecondNumParameters = SecondMethod->param_size();
10544         if (FirstNumParameters != SecondNumParameters) {
10545           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10546                            FirstMethod->getSourceRange(),
10547                            MethodNumberParameters)
10548               << FirstMethodType << FirstName << FirstNumParameters;
10549           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10550                           SecondMethod->getSourceRange(),
10551                           MethodNumberParameters)
10552               << SecondMethodType << SecondName << SecondNumParameters;
10553           Diagnosed = true;
10554           break;
10555         }
10556 
10557         // Need this status boolean to know when break out of the switch.
10558         bool ParameterMismatch = false;
10559         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10560           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10561           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10562 
10563           QualType FirstParamType = FirstParam->getType();
10564           QualType SecondParamType = SecondParam->getType();
10565           if (FirstParamType != SecondParamType &&
10566               ComputeQualTypeODRHash(FirstParamType) !=
10567                   ComputeQualTypeODRHash(SecondParamType)) {
10568             if (const DecayedType *ParamDecayedType =
10569                     FirstParamType->getAs<DecayedType>()) {
10570               ODRDiagDeclError(
10571                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10572                   FirstMethod->getSourceRange(), MethodParameterType)
10573                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10574                   << true << ParamDecayedType->getOriginalType();
10575             } else {
10576               ODRDiagDeclError(
10577                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10578                   FirstMethod->getSourceRange(), MethodParameterType)
10579                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10580                   << false;
10581             }
10582 
10583             if (const DecayedType *ParamDecayedType =
10584                     SecondParamType->getAs<DecayedType>()) {
10585               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10586                               SecondMethod->getSourceRange(),
10587                               MethodParameterType)
10588                   << SecondMethodType << SecondName << (I + 1)
10589                   << SecondParamType << true
10590                   << ParamDecayedType->getOriginalType();
10591             } else {
10592               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10593                               SecondMethod->getSourceRange(),
10594                               MethodParameterType)
10595                   << SecondMethodType << SecondName << (I + 1)
10596                   << SecondParamType << false;
10597             }
10598             ParameterMismatch = true;
10599             break;
10600           }
10601 
10602           DeclarationName FirstParamName = FirstParam->getDeclName();
10603           DeclarationName SecondParamName = SecondParam->getDeclName();
10604           if (FirstParamName != SecondParamName) {
10605             ODRDiagDeclError(FirstRecord, FirstModule,
10606                              FirstMethod->getLocation(),
10607                              FirstMethod->getSourceRange(), MethodParameterName)
10608                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10609             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10610                             SecondMethod->getSourceRange(), MethodParameterName)
10611                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10612             ParameterMismatch = true;
10613             break;
10614           }
10615 
10616           const Expr *FirstInit = FirstParam->getInit();
10617           const Expr *SecondInit = SecondParam->getInit();
10618           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10619             ODRDiagDeclError(FirstRecord, FirstModule,
10620                              FirstMethod->getLocation(),
10621                              FirstMethod->getSourceRange(),
10622                              MethodParameterSingleDefaultArgument)
10623                 << FirstMethodType << FirstName << (I + 1)
10624                 << (FirstInit == nullptr)
10625                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10626             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10627                             SecondMethod->getSourceRange(),
10628                             MethodParameterSingleDefaultArgument)
10629                 << SecondMethodType << SecondName << (I + 1)
10630                 << (SecondInit == nullptr)
10631                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10632             ParameterMismatch = true;
10633             break;
10634           }
10635 
10636           if (FirstInit && SecondInit &&
10637               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10638             ODRDiagDeclError(FirstRecord, FirstModule,
10639                              FirstMethod->getLocation(),
10640                              FirstMethod->getSourceRange(),
10641                              MethodParameterDifferentDefaultArgument)
10642                 << FirstMethodType << FirstName << (I + 1)
10643                 << FirstInit->getSourceRange();
10644             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10645                             SecondMethod->getSourceRange(),
10646                             MethodParameterDifferentDefaultArgument)
10647                 << SecondMethodType << SecondName << (I + 1)
10648                 << SecondInit->getSourceRange();
10649             ParameterMismatch = true;
10650             break;
10651 
10652           }
10653         }
10654 
10655         if (ParameterMismatch) {
10656           Diagnosed = true;
10657           break;
10658         }
10659 
10660         const auto *FirstTemplateArgs =
10661             FirstMethod->getTemplateSpecializationArgs();
10662         const auto *SecondTemplateArgs =
10663             SecondMethod->getTemplateSpecializationArgs();
10664 
10665         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10666             (!FirstTemplateArgs && SecondTemplateArgs)) {
10667           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10668                            FirstMethod->getSourceRange(),
10669                            MethodNoTemplateArguments)
10670               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10671           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10672                           SecondMethod->getSourceRange(),
10673                           MethodNoTemplateArguments)
10674               << SecondMethodType << SecondName
10675               << (SecondTemplateArgs != nullptr);
10676 
10677           Diagnosed = true;
10678           break;
10679         }
10680 
10681         if (FirstTemplateArgs && SecondTemplateArgs) {
10682           // Remove pack expansions from argument list.
10683           auto ExpandTemplateArgumentList =
10684               [](const TemplateArgumentList *TAL) {
10685                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10686                 for (const TemplateArgument &TA : TAL->asArray()) {
10687                   if (TA.getKind() != TemplateArgument::Pack) {
10688                     ExpandedList.push_back(&TA);
10689                     continue;
10690                   }
10691                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10692                     ExpandedList.push_back(&PackTA);
10693                   }
10694                 }
10695                 return ExpandedList;
10696               };
10697           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10698               ExpandTemplateArgumentList(FirstTemplateArgs);
10699           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10700               ExpandTemplateArgumentList(SecondTemplateArgs);
10701 
10702           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10703             ODRDiagDeclError(FirstRecord, FirstModule,
10704                              FirstMethod->getLocation(),
10705                              FirstMethod->getSourceRange(),
10706                              MethodDifferentNumberTemplateArguments)
10707                 << FirstMethodType << FirstName
10708                 << (unsigned)FirstExpandedList.size();
10709             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10710                             SecondMethod->getSourceRange(),
10711                             MethodDifferentNumberTemplateArguments)
10712                 << SecondMethodType << SecondName
10713                 << (unsigned)SecondExpandedList.size();
10714 
10715             Diagnosed = true;
10716             break;
10717           }
10718 
10719           bool TemplateArgumentMismatch = false;
10720           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10721             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10722                                    &SecondTA = *SecondExpandedList[i];
10723             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10724                 ComputeTemplateArgumentODRHash(SecondTA)) {
10725               continue;
10726             }
10727 
10728             ODRDiagDeclError(
10729                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10730                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10731                 << FirstMethodType << FirstName << FirstTA << i + 1;
10732             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10733                             SecondMethod->getSourceRange(),
10734                             MethodDifferentTemplateArgument)
10735                 << SecondMethodType << SecondName << SecondTA << i + 1;
10736 
10737             TemplateArgumentMismatch = true;
10738             break;
10739           }
10740 
10741           if (TemplateArgumentMismatch) {
10742             Diagnosed = true;
10743             break;
10744           }
10745         }
10746 
10747         // Compute the hash of the method as if it has no body.
10748         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10749           Hash.clear();
10750           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10751           return Hash.CalculateHash();
10752         };
10753 
10754         // Compare the hash generated to the hash stored.  A difference means
10755         // that a body was present in the original source.  Due to merging,
10756         // the stardard way of detecting a body will not work.
10757         const bool HasFirstBody =
10758             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10759         const bool HasSecondBody =
10760             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10761 
10762         if (HasFirstBody != HasSecondBody) {
10763           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10764                            FirstMethod->getSourceRange(), MethodSingleBody)
10765               << FirstMethodType << FirstName << HasFirstBody;
10766           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10767                           SecondMethod->getSourceRange(), MethodSingleBody)
10768               << SecondMethodType << SecondName << HasSecondBody;
10769           Diagnosed = true;
10770           break;
10771         }
10772 
10773         if (HasFirstBody && HasSecondBody) {
10774           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10775                            FirstMethod->getSourceRange(), MethodDifferentBody)
10776               << FirstMethodType << FirstName;
10777           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10778                           SecondMethod->getSourceRange(), MethodDifferentBody)
10779               << SecondMethodType << SecondName;
10780           Diagnosed = true;
10781           break;
10782         }
10783 
10784         break;
10785       }
10786       case TypeAlias:
10787       case TypeDef: {
10788         Diagnosed = ODRDiagTypeDefOrAlias(
10789             FirstRecord, FirstModule, SecondModule,
10790             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10791             FirstDiffType == TypeAlias);
10792         break;
10793       }
10794       case Var: {
10795         Diagnosed =
10796             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10797                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10798         break;
10799       }
10800       case Friend: {
10801         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10802         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10803 
10804         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10805         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10806 
10807         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10808         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10809 
10810         if (FirstND && SecondND) {
10811           ODRDiagDeclError(FirstRecord, FirstModule,
10812                            FirstFriend->getFriendLoc(),
10813                            FirstFriend->getSourceRange(), FriendFunction)
10814               << FirstND;
10815           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10816                           SecondFriend->getSourceRange(), FriendFunction)
10817               << SecondND;
10818 
10819           Diagnosed = true;
10820           break;
10821         }
10822 
10823         if (FirstTSI && SecondTSI) {
10824           QualType FirstFriendType = FirstTSI->getType();
10825           QualType SecondFriendType = SecondTSI->getType();
10826           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10827                  ComputeQualTypeODRHash(SecondFriendType));
10828           ODRDiagDeclError(FirstRecord, FirstModule,
10829                            FirstFriend->getFriendLoc(),
10830                            FirstFriend->getSourceRange(), FriendType)
10831               << FirstFriendType;
10832           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10833                           SecondFriend->getSourceRange(), FriendType)
10834               << SecondFriendType;
10835           Diagnosed = true;
10836           break;
10837         }
10838 
10839         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10840                          FirstFriend->getSourceRange(), FriendTypeFunction)
10841             << (FirstTSI == nullptr);
10842         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10843                         SecondFriend->getSourceRange(), FriendTypeFunction)
10844             << (SecondTSI == nullptr);
10845 
10846         Diagnosed = true;
10847         break;
10848       }
10849       case FunctionTemplate: {
10850         FunctionTemplateDecl *FirstTemplate =
10851             cast<FunctionTemplateDecl>(FirstDecl);
10852         FunctionTemplateDecl *SecondTemplate =
10853             cast<FunctionTemplateDecl>(SecondDecl);
10854 
10855         TemplateParameterList *FirstTPL =
10856             FirstTemplate->getTemplateParameters();
10857         TemplateParameterList *SecondTPL =
10858             SecondTemplate->getTemplateParameters();
10859 
10860         if (FirstTPL->size() != SecondTPL->size()) {
10861           ODRDiagDeclError(FirstRecord, FirstModule,
10862                            FirstTemplate->getLocation(),
10863                            FirstTemplate->getSourceRange(),
10864                            FunctionTemplateDifferentNumberParameters)
10865               << FirstTemplate << FirstTPL->size();
10866           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10867                           SecondTemplate->getSourceRange(),
10868                           FunctionTemplateDifferentNumberParameters)
10869               << SecondTemplate << SecondTPL->size();
10870 
10871           Diagnosed = true;
10872           break;
10873         }
10874 
10875         bool ParameterMismatch = false;
10876         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10877           NamedDecl *FirstParam = FirstTPL->getParam(i);
10878           NamedDecl *SecondParam = SecondTPL->getParam(i);
10879 
10880           if (FirstParam->getKind() != SecondParam->getKind()) {
10881             enum {
10882               TemplateTypeParameter,
10883               NonTypeTemplateParameter,
10884               TemplateTemplateParameter,
10885             };
10886             auto GetParamType = [](NamedDecl *D) {
10887               switch (D->getKind()) {
10888                 default:
10889                   llvm_unreachable("Unexpected template parameter type");
10890                 case Decl::TemplateTypeParm:
10891                   return TemplateTypeParameter;
10892                 case Decl::NonTypeTemplateParm:
10893                   return NonTypeTemplateParameter;
10894                 case Decl::TemplateTemplateParm:
10895                   return TemplateTemplateParameter;
10896               }
10897             };
10898 
10899             ODRDiagDeclError(FirstRecord, FirstModule,
10900                              FirstTemplate->getLocation(),
10901                              FirstTemplate->getSourceRange(),
10902                              FunctionTemplateParameterDifferentKind)
10903                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10904             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10905                             SecondTemplate->getSourceRange(),
10906                             FunctionTemplateParameterDifferentKind)
10907                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10908 
10909             ParameterMismatch = true;
10910             break;
10911           }
10912 
10913           if (FirstParam->getName() != SecondParam->getName()) {
10914             ODRDiagDeclError(
10915                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10916                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10917                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10918                 << FirstParam;
10919             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10920                             SecondTemplate->getSourceRange(),
10921                             FunctionTemplateParameterName)
10922                 << SecondTemplate << (i + 1)
10923                 << (bool)SecondParam->getIdentifier() << SecondParam;
10924             ParameterMismatch = true;
10925             break;
10926           }
10927 
10928           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10929               isa<TemplateTypeParmDecl>(SecondParam)) {
10930             TemplateTypeParmDecl *FirstTTPD =
10931                 cast<TemplateTypeParmDecl>(FirstParam);
10932             TemplateTypeParmDecl *SecondTTPD =
10933                 cast<TemplateTypeParmDecl>(SecondParam);
10934             bool HasFirstDefaultArgument =
10935                 FirstTTPD->hasDefaultArgument() &&
10936                 !FirstTTPD->defaultArgumentWasInherited();
10937             bool HasSecondDefaultArgument =
10938                 SecondTTPD->hasDefaultArgument() &&
10939                 !SecondTTPD->defaultArgumentWasInherited();
10940             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10941               ODRDiagDeclError(FirstRecord, FirstModule,
10942                                FirstTemplate->getLocation(),
10943                                FirstTemplate->getSourceRange(),
10944                                FunctionTemplateParameterSingleDefaultArgument)
10945                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10946               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10947                               SecondTemplate->getSourceRange(),
10948                               FunctionTemplateParameterSingleDefaultArgument)
10949                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10950               ParameterMismatch = true;
10951               break;
10952             }
10953 
10954             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10955               QualType FirstType = FirstTTPD->getDefaultArgument();
10956               QualType SecondType = SecondTTPD->getDefaultArgument();
10957               if (ComputeQualTypeODRHash(FirstType) !=
10958                   ComputeQualTypeODRHash(SecondType)) {
10959                 ODRDiagDeclError(
10960                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10961                     FirstTemplate->getSourceRange(),
10962                     FunctionTemplateParameterDifferentDefaultArgument)
10963                     << FirstTemplate << (i + 1) << FirstType;
10964                 ODRDiagDeclNote(
10965                     SecondModule, SecondTemplate->getLocation(),
10966                     SecondTemplate->getSourceRange(),
10967                     FunctionTemplateParameterDifferentDefaultArgument)
10968                     << SecondTemplate << (i + 1) << SecondType;
10969                 ParameterMismatch = true;
10970                 break;
10971               }
10972             }
10973 
10974             if (FirstTTPD->isParameterPack() !=
10975                 SecondTTPD->isParameterPack()) {
10976               ODRDiagDeclError(FirstRecord, FirstModule,
10977                                FirstTemplate->getLocation(),
10978                                FirstTemplate->getSourceRange(),
10979                                FunctionTemplatePackParameter)
10980                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
10981               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10982                               SecondTemplate->getSourceRange(),
10983                               FunctionTemplatePackParameter)
10984                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
10985               ParameterMismatch = true;
10986               break;
10987             }
10988           }
10989 
10990           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
10991               isa<TemplateTemplateParmDecl>(SecondParam)) {
10992             TemplateTemplateParmDecl *FirstTTPD =
10993                 cast<TemplateTemplateParmDecl>(FirstParam);
10994             TemplateTemplateParmDecl *SecondTTPD =
10995                 cast<TemplateTemplateParmDecl>(SecondParam);
10996 
10997             TemplateParameterList *FirstTPL =
10998                 FirstTTPD->getTemplateParameters();
10999             TemplateParameterList *SecondTPL =
11000                 SecondTTPD->getTemplateParameters();
11001 
11002             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11003                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11004               ODRDiagDeclError(FirstRecord, FirstModule,
11005                                FirstTemplate->getLocation(),
11006                                FirstTemplate->getSourceRange(),
11007                                FunctionTemplateParameterDifferentType)
11008                   << FirstTemplate << (i + 1);
11009               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11010                               SecondTemplate->getSourceRange(),
11011                               FunctionTemplateParameterDifferentType)
11012                   << SecondTemplate << (i + 1);
11013               ParameterMismatch = true;
11014               break;
11015             }
11016 
11017             bool HasFirstDefaultArgument =
11018                 FirstTTPD->hasDefaultArgument() &&
11019                 !FirstTTPD->defaultArgumentWasInherited();
11020             bool HasSecondDefaultArgument =
11021                 SecondTTPD->hasDefaultArgument() &&
11022                 !SecondTTPD->defaultArgumentWasInherited();
11023             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11024               ODRDiagDeclError(FirstRecord, FirstModule,
11025                                FirstTemplate->getLocation(),
11026                                FirstTemplate->getSourceRange(),
11027                                FunctionTemplateParameterSingleDefaultArgument)
11028                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11029               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11030                               SecondTemplate->getSourceRange(),
11031                               FunctionTemplateParameterSingleDefaultArgument)
11032                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11033               ParameterMismatch = true;
11034               break;
11035             }
11036 
11037             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11038               TemplateArgument FirstTA =
11039                   FirstTTPD->getDefaultArgument().getArgument();
11040               TemplateArgument SecondTA =
11041                   SecondTTPD->getDefaultArgument().getArgument();
11042               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11043                   ComputeTemplateArgumentODRHash(SecondTA)) {
11044                 ODRDiagDeclError(
11045                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11046                     FirstTemplate->getSourceRange(),
11047                     FunctionTemplateParameterDifferentDefaultArgument)
11048                     << FirstTemplate << (i + 1) << FirstTA;
11049                 ODRDiagDeclNote(
11050                     SecondModule, SecondTemplate->getLocation(),
11051                     SecondTemplate->getSourceRange(),
11052                     FunctionTemplateParameterDifferentDefaultArgument)
11053                     << SecondTemplate << (i + 1) << SecondTA;
11054                 ParameterMismatch = true;
11055                 break;
11056               }
11057             }
11058 
11059             if (FirstTTPD->isParameterPack() !=
11060                 SecondTTPD->isParameterPack()) {
11061               ODRDiagDeclError(FirstRecord, FirstModule,
11062                                FirstTemplate->getLocation(),
11063                                FirstTemplate->getSourceRange(),
11064                                FunctionTemplatePackParameter)
11065                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11066               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11067                               SecondTemplate->getSourceRange(),
11068                               FunctionTemplatePackParameter)
11069                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11070               ParameterMismatch = true;
11071               break;
11072             }
11073           }
11074 
11075           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11076               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11077             NonTypeTemplateParmDecl *FirstNTTPD =
11078                 cast<NonTypeTemplateParmDecl>(FirstParam);
11079             NonTypeTemplateParmDecl *SecondNTTPD =
11080                 cast<NonTypeTemplateParmDecl>(SecondParam);
11081 
11082             QualType FirstType = FirstNTTPD->getType();
11083             QualType SecondType = SecondNTTPD->getType();
11084             if (ComputeQualTypeODRHash(FirstType) !=
11085                 ComputeQualTypeODRHash(SecondType)) {
11086               ODRDiagDeclError(FirstRecord, FirstModule,
11087                                FirstTemplate->getLocation(),
11088                                FirstTemplate->getSourceRange(),
11089                                FunctionTemplateParameterDifferentType)
11090                   << FirstTemplate << (i + 1);
11091               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11092                               SecondTemplate->getSourceRange(),
11093                               FunctionTemplateParameterDifferentType)
11094                   << SecondTemplate << (i + 1);
11095               ParameterMismatch = true;
11096               break;
11097             }
11098 
11099             bool HasFirstDefaultArgument =
11100                 FirstNTTPD->hasDefaultArgument() &&
11101                 !FirstNTTPD->defaultArgumentWasInherited();
11102             bool HasSecondDefaultArgument =
11103                 SecondNTTPD->hasDefaultArgument() &&
11104                 !SecondNTTPD->defaultArgumentWasInherited();
11105             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11106               ODRDiagDeclError(FirstRecord, FirstModule,
11107                                FirstTemplate->getLocation(),
11108                                FirstTemplate->getSourceRange(),
11109                                FunctionTemplateParameterSingleDefaultArgument)
11110                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11111               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11112                               SecondTemplate->getSourceRange(),
11113                               FunctionTemplateParameterSingleDefaultArgument)
11114                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11115               ParameterMismatch = true;
11116               break;
11117             }
11118 
11119             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11120               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11121               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11122               if (ComputeODRHash(FirstDefaultArgument) !=
11123                   ComputeODRHash(SecondDefaultArgument)) {
11124                 ODRDiagDeclError(
11125                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11126                     FirstTemplate->getSourceRange(),
11127                     FunctionTemplateParameterDifferentDefaultArgument)
11128                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11129                 ODRDiagDeclNote(
11130                     SecondModule, SecondTemplate->getLocation(),
11131                     SecondTemplate->getSourceRange(),
11132                     FunctionTemplateParameterDifferentDefaultArgument)
11133                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11134                 ParameterMismatch = true;
11135                 break;
11136               }
11137             }
11138 
11139             if (FirstNTTPD->isParameterPack() !=
11140                 SecondNTTPD->isParameterPack()) {
11141               ODRDiagDeclError(FirstRecord, FirstModule,
11142                                FirstTemplate->getLocation(),
11143                                FirstTemplate->getSourceRange(),
11144                                FunctionTemplatePackParameter)
11145                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11146               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11147                               SecondTemplate->getSourceRange(),
11148                               FunctionTemplatePackParameter)
11149                   << SecondTemplate << (i + 1)
11150                   << SecondNTTPD->isParameterPack();
11151               ParameterMismatch = true;
11152               break;
11153             }
11154           }
11155         }
11156 
11157         if (ParameterMismatch) {
11158           Diagnosed = true;
11159           break;
11160         }
11161 
11162         break;
11163       }
11164       }
11165 
11166       if (Diagnosed)
11167         continue;
11168 
11169       Diag(FirstDecl->getLocation(),
11170            diag::err_module_odr_violation_mismatch_decl_unknown)
11171           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11172           << FirstDecl->getSourceRange();
11173       Diag(SecondDecl->getLocation(),
11174            diag::note_module_odr_violation_mismatch_decl_unknown)
11175           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11176       Diagnosed = true;
11177     }
11178 
11179     if (!Diagnosed) {
11180       // All definitions are updates to the same declaration. This happens if a
11181       // module instantiates the declaration of a class template specialization
11182       // and two or more other modules instantiate its definition.
11183       //
11184       // FIXME: Indicate which modules had instantiations of this definition.
11185       // FIXME: How can this even happen?
11186       Diag(Merge.first->getLocation(),
11187            diag::err_module_odr_violation_different_instantiations)
11188         << Merge.first;
11189     }
11190   }
11191 
11192   // Issue ODR failures diagnostics for functions.
11193   for (auto &Merge : FunctionOdrMergeFailures) {
11194     enum ODRFunctionDifference {
11195       ReturnType,
11196       ParameterName,
11197       ParameterType,
11198       ParameterSingleDefaultArgument,
11199       ParameterDifferentDefaultArgument,
11200       FunctionBody,
11201     };
11202 
11203     FunctionDecl *FirstFunction = Merge.first;
11204     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11205 
11206     bool Diagnosed = false;
11207     for (auto &SecondFunction : Merge.second) {
11208 
11209       if (FirstFunction == SecondFunction)
11210         continue;
11211 
11212       std::string SecondModule =
11213           getOwningModuleNameForDiagnostic(SecondFunction);
11214 
11215       auto ODRDiagError = [FirstFunction, &FirstModule,
11216                            this](SourceLocation Loc, SourceRange Range,
11217                                  ODRFunctionDifference DiffType) {
11218         return Diag(Loc, diag::err_module_odr_violation_function)
11219                << FirstFunction << FirstModule.empty() << FirstModule << Range
11220                << DiffType;
11221       };
11222       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11223                                                SourceRange Range,
11224                                                ODRFunctionDifference DiffType) {
11225         return Diag(Loc, diag::note_module_odr_violation_function)
11226                << SecondModule << Range << DiffType;
11227       };
11228 
11229       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11230           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11231         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11232                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11233             << FirstFunction->getReturnType();
11234         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11235                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11236             << SecondFunction->getReturnType();
11237         Diagnosed = true;
11238         break;
11239       }
11240 
11241       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11242              "Merged functions with different number of parameters");
11243 
11244       auto ParamSize = FirstFunction->param_size();
11245       bool ParameterMismatch = false;
11246       for (unsigned I = 0; I < ParamSize; ++I) {
11247         auto *FirstParam = FirstFunction->getParamDecl(I);
11248         auto *SecondParam = SecondFunction->getParamDecl(I);
11249 
11250         assert(getContext().hasSameType(FirstParam->getType(),
11251                                       SecondParam->getType()) &&
11252                "Merged function has different parameter types.");
11253 
11254         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11255           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11256                        ParameterName)
11257               << I + 1 << FirstParam->getDeclName();
11258           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11259                       ParameterName)
11260               << I + 1 << SecondParam->getDeclName();
11261           ParameterMismatch = true;
11262           break;
11263         };
11264 
11265         QualType FirstParamType = FirstParam->getType();
11266         QualType SecondParamType = SecondParam->getType();
11267         if (FirstParamType != SecondParamType &&
11268             ComputeQualTypeODRHash(FirstParamType) !=
11269                 ComputeQualTypeODRHash(SecondParamType)) {
11270           if (const DecayedType *ParamDecayedType =
11271                   FirstParamType->getAs<DecayedType>()) {
11272             ODRDiagError(FirstParam->getLocation(),
11273                          FirstParam->getSourceRange(), ParameterType)
11274                 << (I + 1) << FirstParamType << true
11275                 << ParamDecayedType->getOriginalType();
11276           } else {
11277             ODRDiagError(FirstParam->getLocation(),
11278                          FirstParam->getSourceRange(), ParameterType)
11279                 << (I + 1) << FirstParamType << false;
11280           }
11281 
11282           if (const DecayedType *ParamDecayedType =
11283                   SecondParamType->getAs<DecayedType>()) {
11284             ODRDiagNote(SecondParam->getLocation(),
11285                         SecondParam->getSourceRange(), ParameterType)
11286                 << (I + 1) << SecondParamType << true
11287                 << ParamDecayedType->getOriginalType();
11288           } else {
11289             ODRDiagNote(SecondParam->getLocation(),
11290                         SecondParam->getSourceRange(), ParameterType)
11291                 << (I + 1) << SecondParamType << false;
11292           }
11293           ParameterMismatch = true;
11294           break;
11295         }
11296 
11297         const Expr *FirstInit = FirstParam->getInit();
11298         const Expr *SecondInit = SecondParam->getInit();
11299         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11300           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11301                        ParameterSingleDefaultArgument)
11302               << (I + 1) << (FirstInit == nullptr)
11303               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11304           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11305                       ParameterSingleDefaultArgument)
11306               << (I + 1) << (SecondInit == nullptr)
11307               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11308           ParameterMismatch = true;
11309           break;
11310         }
11311 
11312         if (FirstInit && SecondInit &&
11313             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11314           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11315                        ParameterDifferentDefaultArgument)
11316               << (I + 1) << FirstInit->getSourceRange();
11317           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11318                       ParameterDifferentDefaultArgument)
11319               << (I + 1) << SecondInit->getSourceRange();
11320           ParameterMismatch = true;
11321           break;
11322         }
11323 
11324         assert(ComputeSubDeclODRHash(FirstParam) ==
11325                    ComputeSubDeclODRHash(SecondParam) &&
11326                "Undiagnosed parameter difference.");
11327       }
11328 
11329       if (ParameterMismatch) {
11330         Diagnosed = true;
11331         break;
11332       }
11333 
11334       // If no error has been generated before now, assume the problem is in
11335       // the body and generate a message.
11336       ODRDiagError(FirstFunction->getLocation(),
11337                    FirstFunction->getSourceRange(), FunctionBody);
11338       ODRDiagNote(SecondFunction->getLocation(),
11339                   SecondFunction->getSourceRange(), FunctionBody);
11340       Diagnosed = true;
11341       break;
11342     }
11343     (void)Diagnosed;
11344     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11345   }
11346 
11347   // Issue ODR failures diagnostics for enums.
11348   for (auto &Merge : EnumOdrMergeFailures) {
11349     enum ODREnumDifference {
11350       SingleScopedEnum,
11351       EnumTagKeywordMismatch,
11352       SingleSpecifiedType,
11353       DifferentSpecifiedTypes,
11354       DifferentNumberEnumConstants,
11355       EnumConstantName,
11356       EnumConstantSingleInitilizer,
11357       EnumConstantDifferentInitilizer,
11358     };
11359 
11360     // If we've already pointed out a specific problem with this enum, don't
11361     // bother issuing a general "something's different" diagnostic.
11362     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11363       continue;
11364 
11365     EnumDecl *FirstEnum = Merge.first;
11366     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11367 
11368     using DeclHashes =
11369         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11370     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11371                               DeclHashes &Hashes, EnumDecl *Enum) {
11372       for (auto *D : Enum->decls()) {
11373         // Due to decl merging, the first EnumDecl is the parent of
11374         // Decls in both records.
11375         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11376           continue;
11377         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11378         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11379                             ComputeSubDeclODRHash(D));
11380       }
11381     };
11382     DeclHashes FirstHashes;
11383     PopulateHashes(FirstHashes, FirstEnum);
11384     bool Diagnosed = false;
11385     for (auto &SecondEnum : Merge.second) {
11386 
11387       if (FirstEnum == SecondEnum)
11388         continue;
11389 
11390       std::string SecondModule =
11391           getOwningModuleNameForDiagnostic(SecondEnum);
11392 
11393       auto ODRDiagError = [FirstEnum, &FirstModule,
11394                            this](SourceLocation Loc, SourceRange Range,
11395                                  ODREnumDifference DiffType) {
11396         return Diag(Loc, diag::err_module_odr_violation_enum)
11397                << FirstEnum << FirstModule.empty() << FirstModule << Range
11398                << DiffType;
11399       };
11400       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11401                                                SourceRange Range,
11402                                                ODREnumDifference DiffType) {
11403         return Diag(Loc, diag::note_module_odr_violation_enum)
11404                << SecondModule << Range << DiffType;
11405       };
11406 
11407       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11408         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11409                      SingleScopedEnum)
11410             << FirstEnum->isScoped();
11411         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11412                     SingleScopedEnum)
11413             << SecondEnum->isScoped();
11414         Diagnosed = true;
11415         continue;
11416       }
11417 
11418       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11419         if (FirstEnum->isScopedUsingClassTag() !=
11420             SecondEnum->isScopedUsingClassTag()) {
11421           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11422                        EnumTagKeywordMismatch)
11423               << FirstEnum->isScopedUsingClassTag();
11424           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11425                       EnumTagKeywordMismatch)
11426               << SecondEnum->isScopedUsingClassTag();
11427           Diagnosed = true;
11428           continue;
11429         }
11430       }
11431 
11432       QualType FirstUnderlyingType =
11433           FirstEnum->getIntegerTypeSourceInfo()
11434               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11435               : QualType();
11436       QualType SecondUnderlyingType =
11437           SecondEnum->getIntegerTypeSourceInfo()
11438               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11439               : QualType();
11440       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11441           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11442                        SingleSpecifiedType)
11443               << !FirstUnderlyingType.isNull();
11444           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11445                       SingleSpecifiedType)
11446               << !SecondUnderlyingType.isNull();
11447           Diagnosed = true;
11448           continue;
11449       }
11450 
11451       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11452         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11453             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11454           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11455                        DifferentSpecifiedTypes)
11456               << FirstUnderlyingType;
11457           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11458                       DifferentSpecifiedTypes)
11459               << SecondUnderlyingType;
11460           Diagnosed = true;
11461           continue;
11462         }
11463       }
11464 
11465       DeclHashes SecondHashes;
11466       PopulateHashes(SecondHashes, SecondEnum);
11467 
11468       if (FirstHashes.size() != SecondHashes.size()) {
11469         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11470                      DifferentNumberEnumConstants)
11471             << (int)FirstHashes.size();
11472         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11473                     DifferentNumberEnumConstants)
11474             << (int)SecondHashes.size();
11475         Diagnosed = true;
11476         continue;
11477       }
11478 
11479       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11480         if (FirstHashes[I].second == SecondHashes[I].second)
11481           continue;
11482         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11483         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11484 
11485         if (FirstEnumConstant->getDeclName() !=
11486             SecondEnumConstant->getDeclName()) {
11487 
11488           ODRDiagError(FirstEnumConstant->getLocation(),
11489                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11490               << I + 1 << FirstEnumConstant;
11491           ODRDiagNote(SecondEnumConstant->getLocation(),
11492                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11493               << I + 1 << SecondEnumConstant;
11494           Diagnosed = true;
11495           break;
11496         }
11497 
11498         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11499         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11500         if (!FirstInit && !SecondInit)
11501           continue;
11502 
11503         if (!FirstInit || !SecondInit) {
11504           ODRDiagError(FirstEnumConstant->getLocation(),
11505                        FirstEnumConstant->getSourceRange(),
11506                        EnumConstantSingleInitilizer)
11507               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11508           ODRDiagNote(SecondEnumConstant->getLocation(),
11509                       SecondEnumConstant->getSourceRange(),
11510                       EnumConstantSingleInitilizer)
11511               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11512           Diagnosed = true;
11513           break;
11514         }
11515 
11516         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11517           ODRDiagError(FirstEnumConstant->getLocation(),
11518                        FirstEnumConstant->getSourceRange(),
11519                        EnumConstantDifferentInitilizer)
11520               << I + 1 << FirstEnumConstant;
11521           ODRDiagNote(SecondEnumConstant->getLocation(),
11522                       SecondEnumConstant->getSourceRange(),
11523                       EnumConstantDifferentInitilizer)
11524               << I + 1 << SecondEnumConstant;
11525           Diagnosed = true;
11526           break;
11527         }
11528       }
11529     }
11530 
11531     (void)Diagnosed;
11532     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11533   }
11534 }
11535 
11536 void ASTReader::StartedDeserializing() {
11537   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11538     ReadTimer->startTimer();
11539 }
11540 
11541 void ASTReader::FinishedDeserializing() {
11542   assert(NumCurrentElementsDeserializing &&
11543          "FinishedDeserializing not paired with StartedDeserializing");
11544   if (NumCurrentElementsDeserializing == 1) {
11545     // We decrease NumCurrentElementsDeserializing only after pending actions
11546     // are finished, to avoid recursively re-calling finishPendingActions().
11547     finishPendingActions();
11548   }
11549   --NumCurrentElementsDeserializing;
11550 
11551   if (NumCurrentElementsDeserializing == 0) {
11552     // Propagate exception specification and deduced type updates along
11553     // redeclaration chains.
11554     //
11555     // We do this now rather than in finishPendingActions because we want to
11556     // be able to walk the complete redeclaration chains of the updated decls.
11557     while (!PendingExceptionSpecUpdates.empty() ||
11558            !PendingDeducedTypeUpdates.empty()) {
11559       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11560       PendingExceptionSpecUpdates.clear();
11561       for (auto Update : ESUpdates) {
11562         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11563         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11564         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11565         if (auto *Listener = getContext().getASTMutationListener())
11566           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11567         for (auto *Redecl : Update.second->redecls())
11568           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11569       }
11570 
11571       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11572       PendingDeducedTypeUpdates.clear();
11573       for (auto Update : DTUpdates) {
11574         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11575         // FIXME: If the return type is already deduced, check that it matches.
11576         getContext().adjustDeducedFunctionResultType(Update.first,
11577                                                      Update.second);
11578       }
11579     }
11580 
11581     if (ReadTimer)
11582       ReadTimer->stopTimer();
11583 
11584     diagnoseOdrViolations();
11585 
11586     // We are not in recursive loading, so it's safe to pass the "interesting"
11587     // decls to the consumer.
11588     if (Consumer)
11589       PassInterestingDeclsToConsumer();
11590   }
11591 }
11592 
11593 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11594   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11595     // Remove any fake results before adding any real ones.
11596     auto It = PendingFakeLookupResults.find(II);
11597     if (It != PendingFakeLookupResults.end()) {
11598       for (auto *ND : It->second)
11599         SemaObj->IdResolver.RemoveDecl(ND);
11600       // FIXME: this works around module+PCH performance issue.
11601       // Rather than erase the result from the map, which is O(n), just clear
11602       // the vector of NamedDecls.
11603       It->second.clear();
11604     }
11605   }
11606 
11607   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11608     SemaObj->TUScope->AddDecl(D);
11609   } else if (SemaObj->TUScope) {
11610     // Adding the decl to IdResolver may have failed because it was already in
11611     // (even though it was not added in scope). If it is already in, make sure
11612     // it gets in the scope as well.
11613     if (std::find(SemaObj->IdResolver.begin(Name),
11614                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11615       SemaObj->TUScope->AddDecl(D);
11616   }
11617 }
11618 
11619 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11620                      ASTContext *Context,
11621                      const PCHContainerReader &PCHContainerRdr,
11622                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11623                      StringRef isysroot,
11624                      DisableValidationForModuleKind DisableValidationKind,
11625                      bool AllowASTWithCompilerErrors,
11626                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11627                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11628                      std::unique_ptr<llvm::Timer> ReadTimer)
11629     : Listener(bool(DisableValidationKind &DisableValidationForModuleKind::PCH)
11630                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11631                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11632       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11633       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11634       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11635                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11636       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11637       DisableValidationKind(DisableValidationKind),
11638       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11639       AllowConfigurationMismatch(AllowConfigurationMismatch),
11640       ValidateSystemInputs(ValidateSystemInputs),
11641       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11642       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11643   SourceMgr.setExternalSLocEntrySource(this);
11644 
11645   for (const auto &Ext : Extensions) {
11646     auto BlockName = Ext->getExtensionMetadata().BlockName;
11647     auto Known = ModuleFileExtensions.find(BlockName);
11648     if (Known != ModuleFileExtensions.end()) {
11649       Diags.Report(diag::warn_duplicate_module_file_extension)
11650         << BlockName;
11651       continue;
11652     }
11653 
11654     ModuleFileExtensions.insert({BlockName, Ext});
11655   }
11656 }
11657 
11658 ASTReader::~ASTReader() {
11659   if (OwnsDeserializationListener)
11660     delete DeserializationListener;
11661 }
11662 
11663 IdentifierResolver &ASTReader::getIdResolver() {
11664   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11665 }
11666 
11667 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11668                                                unsigned AbbrevID) {
11669   Idx = 0;
11670   Record.clear();
11671   return Cursor.readRecord(AbbrevID, Record);
11672 }
11673 //===----------------------------------------------------------------------===//
11674 //// OMPClauseReader implementation
11675 ////===----------------------------------------------------------------------===//
11676 
11677 // This has to be in namespace clang because it's friended by all
11678 // of the OMP clauses.
11679 namespace clang {
11680 
11681 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11682   ASTRecordReader &Record;
11683   ASTContext &Context;
11684 
11685 public:
11686   OMPClauseReader(ASTRecordReader &Record)
11687       : Record(Record), Context(Record.getContext()) {}
11688 #define GEN_CLANG_CLAUSE_CLASS
11689 #define CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11690 #include "llvm/Frontend/OpenMP/OMP.inc"
11691   OMPClause *readClause();
11692   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11693   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11694 };
11695 
11696 } // end namespace clang
11697 
11698 OMPClause *ASTRecordReader::readOMPClause() {
11699   return OMPClauseReader(*this).readClause();
11700 }
11701 
11702 OMPClause *OMPClauseReader::readClause() {
11703   OMPClause *C = nullptr;
11704   switch (llvm::omp::Clause(Record.readInt())) {
11705   case llvm::omp::OMPC_if:
11706     C = new (Context) OMPIfClause();
11707     break;
11708   case llvm::omp::OMPC_final:
11709     C = new (Context) OMPFinalClause();
11710     break;
11711   case llvm::omp::OMPC_num_threads:
11712     C = new (Context) OMPNumThreadsClause();
11713     break;
11714   case llvm::omp::OMPC_safelen:
11715     C = new (Context) OMPSafelenClause();
11716     break;
11717   case llvm::omp::OMPC_simdlen:
11718     C = new (Context) OMPSimdlenClause();
11719     break;
11720   case llvm::omp::OMPC_sizes: {
11721     unsigned NumSizes = Record.readInt();
11722     C = OMPSizesClause::CreateEmpty(Context, NumSizes);
11723     break;
11724   }
11725   case llvm::omp::OMPC_full:
11726     C = OMPFullClause::CreateEmpty(Context);
11727     break;
11728   case llvm::omp::OMPC_partial:
11729     C = OMPPartialClause::CreateEmpty(Context);
11730     break;
11731   case llvm::omp::OMPC_allocator:
11732     C = new (Context) OMPAllocatorClause();
11733     break;
11734   case llvm::omp::OMPC_collapse:
11735     C = new (Context) OMPCollapseClause();
11736     break;
11737   case llvm::omp::OMPC_default:
11738     C = new (Context) OMPDefaultClause();
11739     break;
11740   case llvm::omp::OMPC_proc_bind:
11741     C = new (Context) OMPProcBindClause();
11742     break;
11743   case llvm::omp::OMPC_schedule:
11744     C = new (Context) OMPScheduleClause();
11745     break;
11746   case llvm::omp::OMPC_ordered:
11747     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11748     break;
11749   case llvm::omp::OMPC_nowait:
11750     C = new (Context) OMPNowaitClause();
11751     break;
11752   case llvm::omp::OMPC_untied:
11753     C = new (Context) OMPUntiedClause();
11754     break;
11755   case llvm::omp::OMPC_mergeable:
11756     C = new (Context) OMPMergeableClause();
11757     break;
11758   case llvm::omp::OMPC_read:
11759     C = new (Context) OMPReadClause();
11760     break;
11761   case llvm::omp::OMPC_write:
11762     C = new (Context) OMPWriteClause();
11763     break;
11764   case llvm::omp::OMPC_update:
11765     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11766     break;
11767   case llvm::omp::OMPC_capture:
11768     C = new (Context) OMPCaptureClause();
11769     break;
11770   case llvm::omp::OMPC_compare:
11771     C = new (Context) OMPCompareClause();
11772     break;
11773   case llvm::omp::OMPC_seq_cst:
11774     C = new (Context) OMPSeqCstClause();
11775     break;
11776   case llvm::omp::OMPC_acq_rel:
11777     C = new (Context) OMPAcqRelClause();
11778     break;
11779   case llvm::omp::OMPC_acquire:
11780     C = new (Context) OMPAcquireClause();
11781     break;
11782   case llvm::omp::OMPC_release:
11783     C = new (Context) OMPReleaseClause();
11784     break;
11785   case llvm::omp::OMPC_relaxed:
11786     C = new (Context) OMPRelaxedClause();
11787     break;
11788   case llvm::omp::OMPC_threads:
11789     C = new (Context) OMPThreadsClause();
11790     break;
11791   case llvm::omp::OMPC_simd:
11792     C = new (Context) OMPSIMDClause();
11793     break;
11794   case llvm::omp::OMPC_nogroup:
11795     C = new (Context) OMPNogroupClause();
11796     break;
11797   case llvm::omp::OMPC_unified_address:
11798     C = new (Context) OMPUnifiedAddressClause();
11799     break;
11800   case llvm::omp::OMPC_unified_shared_memory:
11801     C = new (Context) OMPUnifiedSharedMemoryClause();
11802     break;
11803   case llvm::omp::OMPC_reverse_offload:
11804     C = new (Context) OMPReverseOffloadClause();
11805     break;
11806   case llvm::omp::OMPC_dynamic_allocators:
11807     C = new (Context) OMPDynamicAllocatorsClause();
11808     break;
11809   case llvm::omp::OMPC_atomic_default_mem_order:
11810     C = new (Context) OMPAtomicDefaultMemOrderClause();
11811     break;
11812  case llvm::omp::OMPC_private:
11813     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11814     break;
11815   case llvm::omp::OMPC_firstprivate:
11816     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11817     break;
11818   case llvm::omp::OMPC_lastprivate:
11819     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11820     break;
11821   case llvm::omp::OMPC_shared:
11822     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11823     break;
11824   case llvm::omp::OMPC_reduction: {
11825     unsigned N = Record.readInt();
11826     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11827     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11828     break;
11829   }
11830   case llvm::omp::OMPC_task_reduction:
11831     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11832     break;
11833   case llvm::omp::OMPC_in_reduction:
11834     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11835     break;
11836   case llvm::omp::OMPC_linear:
11837     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11838     break;
11839   case llvm::omp::OMPC_aligned:
11840     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11841     break;
11842   case llvm::omp::OMPC_copyin:
11843     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11844     break;
11845   case llvm::omp::OMPC_copyprivate:
11846     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11847     break;
11848   case llvm::omp::OMPC_flush:
11849     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11850     break;
11851   case llvm::omp::OMPC_depobj:
11852     C = OMPDepobjClause::CreateEmpty(Context);
11853     break;
11854   case llvm::omp::OMPC_depend: {
11855     unsigned NumVars = Record.readInt();
11856     unsigned NumLoops = Record.readInt();
11857     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11858     break;
11859   }
11860   case llvm::omp::OMPC_device:
11861     C = new (Context) OMPDeviceClause();
11862     break;
11863   case llvm::omp::OMPC_map: {
11864     OMPMappableExprListSizeTy Sizes;
11865     Sizes.NumVars = Record.readInt();
11866     Sizes.NumUniqueDeclarations = Record.readInt();
11867     Sizes.NumComponentLists = Record.readInt();
11868     Sizes.NumComponents = Record.readInt();
11869     C = OMPMapClause::CreateEmpty(Context, Sizes);
11870     break;
11871   }
11872   case llvm::omp::OMPC_num_teams:
11873     C = new (Context) OMPNumTeamsClause();
11874     break;
11875   case llvm::omp::OMPC_thread_limit:
11876     C = new (Context) OMPThreadLimitClause();
11877     break;
11878   case llvm::omp::OMPC_priority:
11879     C = new (Context) OMPPriorityClause();
11880     break;
11881   case llvm::omp::OMPC_grainsize:
11882     C = new (Context) OMPGrainsizeClause();
11883     break;
11884   case llvm::omp::OMPC_num_tasks:
11885     C = new (Context) OMPNumTasksClause();
11886     break;
11887   case llvm::omp::OMPC_hint:
11888     C = new (Context) OMPHintClause();
11889     break;
11890   case llvm::omp::OMPC_dist_schedule:
11891     C = new (Context) OMPDistScheduleClause();
11892     break;
11893   case llvm::omp::OMPC_defaultmap:
11894     C = new (Context) OMPDefaultmapClause();
11895     break;
11896   case llvm::omp::OMPC_to: {
11897     OMPMappableExprListSizeTy Sizes;
11898     Sizes.NumVars = Record.readInt();
11899     Sizes.NumUniqueDeclarations = Record.readInt();
11900     Sizes.NumComponentLists = Record.readInt();
11901     Sizes.NumComponents = Record.readInt();
11902     C = OMPToClause::CreateEmpty(Context, Sizes);
11903     break;
11904   }
11905   case llvm::omp::OMPC_from: {
11906     OMPMappableExprListSizeTy Sizes;
11907     Sizes.NumVars = Record.readInt();
11908     Sizes.NumUniqueDeclarations = Record.readInt();
11909     Sizes.NumComponentLists = Record.readInt();
11910     Sizes.NumComponents = Record.readInt();
11911     C = OMPFromClause::CreateEmpty(Context, Sizes);
11912     break;
11913   }
11914   case llvm::omp::OMPC_use_device_ptr: {
11915     OMPMappableExprListSizeTy Sizes;
11916     Sizes.NumVars = Record.readInt();
11917     Sizes.NumUniqueDeclarations = Record.readInt();
11918     Sizes.NumComponentLists = Record.readInt();
11919     Sizes.NumComponents = Record.readInt();
11920     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11921     break;
11922   }
11923   case llvm::omp::OMPC_use_device_addr: {
11924     OMPMappableExprListSizeTy Sizes;
11925     Sizes.NumVars = Record.readInt();
11926     Sizes.NumUniqueDeclarations = Record.readInt();
11927     Sizes.NumComponentLists = Record.readInt();
11928     Sizes.NumComponents = Record.readInt();
11929     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11930     break;
11931   }
11932   case llvm::omp::OMPC_is_device_ptr: {
11933     OMPMappableExprListSizeTy Sizes;
11934     Sizes.NumVars = Record.readInt();
11935     Sizes.NumUniqueDeclarations = Record.readInt();
11936     Sizes.NumComponentLists = Record.readInt();
11937     Sizes.NumComponents = Record.readInt();
11938     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11939     break;
11940   }
11941   case llvm::omp::OMPC_allocate:
11942     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11943     break;
11944   case llvm::omp::OMPC_nontemporal:
11945     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11946     break;
11947   case llvm::omp::OMPC_inclusive:
11948     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11949     break;
11950   case llvm::omp::OMPC_exclusive:
11951     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11952     break;
11953   case llvm::omp::OMPC_order:
11954     C = new (Context) OMPOrderClause();
11955     break;
11956   case llvm::omp::OMPC_init:
11957     C = OMPInitClause::CreateEmpty(Context, Record.readInt());
11958     break;
11959   case llvm::omp::OMPC_use:
11960     C = new (Context) OMPUseClause();
11961     break;
11962   case llvm::omp::OMPC_destroy:
11963     C = new (Context) OMPDestroyClause();
11964     break;
11965   case llvm::omp::OMPC_novariants:
11966     C = new (Context) OMPNovariantsClause();
11967     break;
11968   case llvm::omp::OMPC_nocontext:
11969     C = new (Context) OMPNocontextClause();
11970     break;
11971   case llvm::omp::OMPC_detach:
11972     C = new (Context) OMPDetachClause();
11973     break;
11974   case llvm::omp::OMPC_uses_allocators:
11975     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11976     break;
11977   case llvm::omp::OMPC_affinity:
11978     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11979     break;
11980   case llvm::omp::OMPC_filter:
11981     C = new (Context) OMPFilterClause();
11982     break;
11983   case llvm::omp::OMPC_bind:
11984     C = OMPBindClause::CreateEmpty(Context);
11985     break;
11986   case llvm::omp::OMPC_align:
11987     C = new (Context) OMPAlignClause();
11988     break;
11989 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11990   case llvm::omp::Enum:                                                        \
11991     break;
11992 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11993   default:
11994     break;
11995   }
11996   assert(C && "Unknown OMPClause type");
11997 
11998   Visit(C);
11999   C->setLocStart(Record.readSourceLocation());
12000   C->setLocEnd(Record.readSourceLocation());
12001 
12002   return C;
12003 }
12004 
12005 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12006   C->setPreInitStmt(Record.readSubStmt(),
12007                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12008 }
12009 
12010 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12011   VisitOMPClauseWithPreInit(C);
12012   C->setPostUpdateExpr(Record.readSubExpr());
12013 }
12014 
12015 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12016   VisitOMPClauseWithPreInit(C);
12017   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12018   C->setNameModifierLoc(Record.readSourceLocation());
12019   C->setColonLoc(Record.readSourceLocation());
12020   C->setCondition(Record.readSubExpr());
12021   C->setLParenLoc(Record.readSourceLocation());
12022 }
12023 
12024 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12025   VisitOMPClauseWithPreInit(C);
12026   C->setCondition(Record.readSubExpr());
12027   C->setLParenLoc(Record.readSourceLocation());
12028 }
12029 
12030 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12031   VisitOMPClauseWithPreInit(C);
12032   C->setNumThreads(Record.readSubExpr());
12033   C->setLParenLoc(Record.readSourceLocation());
12034 }
12035 
12036 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12037   C->setSafelen(Record.readSubExpr());
12038   C->setLParenLoc(Record.readSourceLocation());
12039 }
12040 
12041 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12042   C->setSimdlen(Record.readSubExpr());
12043   C->setLParenLoc(Record.readSourceLocation());
12044 }
12045 
12046 void OMPClauseReader::VisitOMPSizesClause(OMPSizesClause *C) {
12047   for (Expr *&E : C->getSizesRefs())
12048     E = Record.readSubExpr();
12049   C->setLParenLoc(Record.readSourceLocation());
12050 }
12051 
12052 void OMPClauseReader::VisitOMPFullClause(OMPFullClause *C) {}
12053 
12054 void OMPClauseReader::VisitOMPPartialClause(OMPPartialClause *C) {
12055   C->setFactor(Record.readSubExpr());
12056   C->setLParenLoc(Record.readSourceLocation());
12057 }
12058 
12059 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12060   C->setAllocator(Record.readExpr());
12061   C->setLParenLoc(Record.readSourceLocation());
12062 }
12063 
12064 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12065   C->setNumForLoops(Record.readSubExpr());
12066   C->setLParenLoc(Record.readSourceLocation());
12067 }
12068 
12069 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12070   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12071   C->setLParenLoc(Record.readSourceLocation());
12072   C->setDefaultKindKwLoc(Record.readSourceLocation());
12073 }
12074 
12075 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12076   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12077   C->setLParenLoc(Record.readSourceLocation());
12078   C->setProcBindKindKwLoc(Record.readSourceLocation());
12079 }
12080 
12081 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12082   VisitOMPClauseWithPreInit(C);
12083   C->setScheduleKind(
12084        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12085   C->setFirstScheduleModifier(
12086       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12087   C->setSecondScheduleModifier(
12088       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12089   C->setChunkSize(Record.readSubExpr());
12090   C->setLParenLoc(Record.readSourceLocation());
12091   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12092   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12093   C->setScheduleKindLoc(Record.readSourceLocation());
12094   C->setCommaLoc(Record.readSourceLocation());
12095 }
12096 
12097 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12098   C->setNumForLoops(Record.readSubExpr());
12099   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12100     C->setLoopNumIterations(I, Record.readSubExpr());
12101   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12102     C->setLoopCounter(I, Record.readSubExpr());
12103   C->setLParenLoc(Record.readSourceLocation());
12104 }
12105 
12106 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12107   C->setEventHandler(Record.readSubExpr());
12108   C->setLParenLoc(Record.readSourceLocation());
12109 }
12110 
12111 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12112 
12113 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12114 
12115 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12116 
12117 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12118 
12119 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12120 
12121 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12122   if (C->isExtended()) {
12123     C->setLParenLoc(Record.readSourceLocation());
12124     C->setArgumentLoc(Record.readSourceLocation());
12125     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12126   }
12127 }
12128 
12129 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12130 
12131 void OMPClauseReader::VisitOMPCompareClause(OMPCompareClause *) {}
12132 
12133 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12134 
12135 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12136 
12137 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12138 
12139 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12140 
12141 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12142 
12143 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12144 
12145 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12146 
12147 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12148 
12149 void OMPClauseReader::VisitOMPInitClause(OMPInitClause *C) {
12150   unsigned NumVars = C->varlist_size();
12151   SmallVector<Expr *, 16> Vars;
12152   Vars.reserve(NumVars);
12153   for (unsigned I = 0; I != NumVars; ++I)
12154     Vars.push_back(Record.readSubExpr());
12155   C->setVarRefs(Vars);
12156   C->setIsTarget(Record.readBool());
12157   C->setIsTargetSync(Record.readBool());
12158   C->setLParenLoc(Record.readSourceLocation());
12159   C->setVarLoc(Record.readSourceLocation());
12160 }
12161 
12162 void OMPClauseReader::VisitOMPUseClause(OMPUseClause *C) {
12163   C->setInteropVar(Record.readSubExpr());
12164   C->setLParenLoc(Record.readSourceLocation());
12165   C->setVarLoc(Record.readSourceLocation());
12166 }
12167 
12168 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *C) {
12169   C->setInteropVar(Record.readSubExpr());
12170   C->setLParenLoc(Record.readSourceLocation());
12171   C->setVarLoc(Record.readSourceLocation());
12172 }
12173 
12174 void OMPClauseReader::VisitOMPNovariantsClause(OMPNovariantsClause *C) {
12175   VisitOMPClauseWithPreInit(C);
12176   C->setCondition(Record.readSubExpr());
12177   C->setLParenLoc(Record.readSourceLocation());
12178 }
12179 
12180 void OMPClauseReader::VisitOMPNocontextClause(OMPNocontextClause *C) {
12181   VisitOMPClauseWithPreInit(C);
12182   C->setCondition(Record.readSubExpr());
12183   C->setLParenLoc(Record.readSourceLocation());
12184 }
12185 
12186 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12187 
12188 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12189     OMPUnifiedSharedMemoryClause *) {}
12190 
12191 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12192 
12193 void
12194 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12195 }
12196 
12197 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12198     OMPAtomicDefaultMemOrderClause *C) {
12199   C->setAtomicDefaultMemOrderKind(
12200       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12201   C->setLParenLoc(Record.readSourceLocation());
12202   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12203 }
12204 
12205 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12206   C->setLParenLoc(Record.readSourceLocation());
12207   unsigned NumVars = C->varlist_size();
12208   SmallVector<Expr *, 16> Vars;
12209   Vars.reserve(NumVars);
12210   for (unsigned i = 0; i != NumVars; ++i)
12211     Vars.push_back(Record.readSubExpr());
12212   C->setVarRefs(Vars);
12213   Vars.clear();
12214   for (unsigned i = 0; i != NumVars; ++i)
12215     Vars.push_back(Record.readSubExpr());
12216   C->setPrivateCopies(Vars);
12217 }
12218 
12219 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12220   VisitOMPClauseWithPreInit(C);
12221   C->setLParenLoc(Record.readSourceLocation());
12222   unsigned NumVars = C->varlist_size();
12223   SmallVector<Expr *, 16> Vars;
12224   Vars.reserve(NumVars);
12225   for (unsigned i = 0; i != NumVars; ++i)
12226     Vars.push_back(Record.readSubExpr());
12227   C->setVarRefs(Vars);
12228   Vars.clear();
12229   for (unsigned i = 0; i != NumVars; ++i)
12230     Vars.push_back(Record.readSubExpr());
12231   C->setPrivateCopies(Vars);
12232   Vars.clear();
12233   for (unsigned i = 0; i != NumVars; ++i)
12234     Vars.push_back(Record.readSubExpr());
12235   C->setInits(Vars);
12236 }
12237 
12238 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12239   VisitOMPClauseWithPostUpdate(C);
12240   C->setLParenLoc(Record.readSourceLocation());
12241   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12242   C->setKindLoc(Record.readSourceLocation());
12243   C->setColonLoc(Record.readSourceLocation());
12244   unsigned NumVars = C->varlist_size();
12245   SmallVector<Expr *, 16> Vars;
12246   Vars.reserve(NumVars);
12247   for (unsigned i = 0; i != NumVars; ++i)
12248     Vars.push_back(Record.readSubExpr());
12249   C->setVarRefs(Vars);
12250   Vars.clear();
12251   for (unsigned i = 0; i != NumVars; ++i)
12252     Vars.push_back(Record.readSubExpr());
12253   C->setPrivateCopies(Vars);
12254   Vars.clear();
12255   for (unsigned i = 0; i != NumVars; ++i)
12256     Vars.push_back(Record.readSubExpr());
12257   C->setSourceExprs(Vars);
12258   Vars.clear();
12259   for (unsigned i = 0; i != NumVars; ++i)
12260     Vars.push_back(Record.readSubExpr());
12261   C->setDestinationExprs(Vars);
12262   Vars.clear();
12263   for (unsigned i = 0; i != NumVars; ++i)
12264     Vars.push_back(Record.readSubExpr());
12265   C->setAssignmentOps(Vars);
12266 }
12267 
12268 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12269   C->setLParenLoc(Record.readSourceLocation());
12270   unsigned NumVars = C->varlist_size();
12271   SmallVector<Expr *, 16> Vars;
12272   Vars.reserve(NumVars);
12273   for (unsigned i = 0; i != NumVars; ++i)
12274     Vars.push_back(Record.readSubExpr());
12275   C->setVarRefs(Vars);
12276 }
12277 
12278 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12279   VisitOMPClauseWithPostUpdate(C);
12280   C->setLParenLoc(Record.readSourceLocation());
12281   C->setModifierLoc(Record.readSourceLocation());
12282   C->setColonLoc(Record.readSourceLocation());
12283   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12284   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12285   C->setQualifierLoc(NNSL);
12286   C->setNameInfo(DNI);
12287 
12288   unsigned NumVars = C->varlist_size();
12289   SmallVector<Expr *, 16> Vars;
12290   Vars.reserve(NumVars);
12291   for (unsigned i = 0; i != NumVars; ++i)
12292     Vars.push_back(Record.readSubExpr());
12293   C->setVarRefs(Vars);
12294   Vars.clear();
12295   for (unsigned i = 0; i != NumVars; ++i)
12296     Vars.push_back(Record.readSubExpr());
12297   C->setPrivates(Vars);
12298   Vars.clear();
12299   for (unsigned i = 0; i != NumVars; ++i)
12300     Vars.push_back(Record.readSubExpr());
12301   C->setLHSExprs(Vars);
12302   Vars.clear();
12303   for (unsigned i = 0; i != NumVars; ++i)
12304     Vars.push_back(Record.readSubExpr());
12305   C->setRHSExprs(Vars);
12306   Vars.clear();
12307   for (unsigned i = 0; i != NumVars; ++i)
12308     Vars.push_back(Record.readSubExpr());
12309   C->setReductionOps(Vars);
12310   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12311     Vars.clear();
12312     for (unsigned i = 0; i != NumVars; ++i)
12313       Vars.push_back(Record.readSubExpr());
12314     C->setInscanCopyOps(Vars);
12315     Vars.clear();
12316     for (unsigned i = 0; i != NumVars; ++i)
12317       Vars.push_back(Record.readSubExpr());
12318     C->setInscanCopyArrayTemps(Vars);
12319     Vars.clear();
12320     for (unsigned i = 0; i != NumVars; ++i)
12321       Vars.push_back(Record.readSubExpr());
12322     C->setInscanCopyArrayElems(Vars);
12323   }
12324 }
12325 
12326 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12327   VisitOMPClauseWithPostUpdate(C);
12328   C->setLParenLoc(Record.readSourceLocation());
12329   C->setColonLoc(Record.readSourceLocation());
12330   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12331   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12332   C->setQualifierLoc(NNSL);
12333   C->setNameInfo(DNI);
12334 
12335   unsigned NumVars = C->varlist_size();
12336   SmallVector<Expr *, 16> Vars;
12337   Vars.reserve(NumVars);
12338   for (unsigned I = 0; I != NumVars; ++I)
12339     Vars.push_back(Record.readSubExpr());
12340   C->setVarRefs(Vars);
12341   Vars.clear();
12342   for (unsigned I = 0; I != NumVars; ++I)
12343     Vars.push_back(Record.readSubExpr());
12344   C->setPrivates(Vars);
12345   Vars.clear();
12346   for (unsigned I = 0; I != NumVars; ++I)
12347     Vars.push_back(Record.readSubExpr());
12348   C->setLHSExprs(Vars);
12349   Vars.clear();
12350   for (unsigned I = 0; I != NumVars; ++I)
12351     Vars.push_back(Record.readSubExpr());
12352   C->setRHSExprs(Vars);
12353   Vars.clear();
12354   for (unsigned I = 0; I != NumVars; ++I)
12355     Vars.push_back(Record.readSubExpr());
12356   C->setReductionOps(Vars);
12357 }
12358 
12359 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12360   VisitOMPClauseWithPostUpdate(C);
12361   C->setLParenLoc(Record.readSourceLocation());
12362   C->setColonLoc(Record.readSourceLocation());
12363   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12364   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12365   C->setQualifierLoc(NNSL);
12366   C->setNameInfo(DNI);
12367 
12368   unsigned NumVars = C->varlist_size();
12369   SmallVector<Expr *, 16> Vars;
12370   Vars.reserve(NumVars);
12371   for (unsigned I = 0; I != NumVars; ++I)
12372     Vars.push_back(Record.readSubExpr());
12373   C->setVarRefs(Vars);
12374   Vars.clear();
12375   for (unsigned I = 0; I != NumVars; ++I)
12376     Vars.push_back(Record.readSubExpr());
12377   C->setPrivates(Vars);
12378   Vars.clear();
12379   for (unsigned I = 0; I != NumVars; ++I)
12380     Vars.push_back(Record.readSubExpr());
12381   C->setLHSExprs(Vars);
12382   Vars.clear();
12383   for (unsigned I = 0; I != NumVars; ++I)
12384     Vars.push_back(Record.readSubExpr());
12385   C->setRHSExprs(Vars);
12386   Vars.clear();
12387   for (unsigned I = 0; I != NumVars; ++I)
12388     Vars.push_back(Record.readSubExpr());
12389   C->setReductionOps(Vars);
12390   Vars.clear();
12391   for (unsigned I = 0; I != NumVars; ++I)
12392     Vars.push_back(Record.readSubExpr());
12393   C->setTaskgroupDescriptors(Vars);
12394 }
12395 
12396 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12397   VisitOMPClauseWithPostUpdate(C);
12398   C->setLParenLoc(Record.readSourceLocation());
12399   C->setColonLoc(Record.readSourceLocation());
12400   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12401   C->setModifierLoc(Record.readSourceLocation());
12402   unsigned NumVars = C->varlist_size();
12403   SmallVector<Expr *, 16> Vars;
12404   Vars.reserve(NumVars);
12405   for (unsigned i = 0; i != NumVars; ++i)
12406     Vars.push_back(Record.readSubExpr());
12407   C->setVarRefs(Vars);
12408   Vars.clear();
12409   for (unsigned i = 0; i != NumVars; ++i)
12410     Vars.push_back(Record.readSubExpr());
12411   C->setPrivates(Vars);
12412   Vars.clear();
12413   for (unsigned i = 0; i != NumVars; ++i)
12414     Vars.push_back(Record.readSubExpr());
12415   C->setInits(Vars);
12416   Vars.clear();
12417   for (unsigned i = 0; i != NumVars; ++i)
12418     Vars.push_back(Record.readSubExpr());
12419   C->setUpdates(Vars);
12420   Vars.clear();
12421   for (unsigned i = 0; i != NumVars; ++i)
12422     Vars.push_back(Record.readSubExpr());
12423   C->setFinals(Vars);
12424   C->setStep(Record.readSubExpr());
12425   C->setCalcStep(Record.readSubExpr());
12426   Vars.clear();
12427   for (unsigned I = 0; I != NumVars + 1; ++I)
12428     Vars.push_back(Record.readSubExpr());
12429   C->setUsedExprs(Vars);
12430 }
12431 
12432 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12433   C->setLParenLoc(Record.readSourceLocation());
12434   C->setColonLoc(Record.readSourceLocation());
12435   unsigned NumVars = C->varlist_size();
12436   SmallVector<Expr *, 16> Vars;
12437   Vars.reserve(NumVars);
12438   for (unsigned i = 0; i != NumVars; ++i)
12439     Vars.push_back(Record.readSubExpr());
12440   C->setVarRefs(Vars);
12441   C->setAlignment(Record.readSubExpr());
12442 }
12443 
12444 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12445   C->setLParenLoc(Record.readSourceLocation());
12446   unsigned NumVars = C->varlist_size();
12447   SmallVector<Expr *, 16> Exprs;
12448   Exprs.reserve(NumVars);
12449   for (unsigned i = 0; i != NumVars; ++i)
12450     Exprs.push_back(Record.readSubExpr());
12451   C->setVarRefs(Exprs);
12452   Exprs.clear();
12453   for (unsigned i = 0; i != NumVars; ++i)
12454     Exprs.push_back(Record.readSubExpr());
12455   C->setSourceExprs(Exprs);
12456   Exprs.clear();
12457   for (unsigned i = 0; i != NumVars; ++i)
12458     Exprs.push_back(Record.readSubExpr());
12459   C->setDestinationExprs(Exprs);
12460   Exprs.clear();
12461   for (unsigned i = 0; i != NumVars; ++i)
12462     Exprs.push_back(Record.readSubExpr());
12463   C->setAssignmentOps(Exprs);
12464 }
12465 
12466 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12467   C->setLParenLoc(Record.readSourceLocation());
12468   unsigned NumVars = C->varlist_size();
12469   SmallVector<Expr *, 16> Exprs;
12470   Exprs.reserve(NumVars);
12471   for (unsigned i = 0; i != NumVars; ++i)
12472     Exprs.push_back(Record.readSubExpr());
12473   C->setVarRefs(Exprs);
12474   Exprs.clear();
12475   for (unsigned i = 0; i != NumVars; ++i)
12476     Exprs.push_back(Record.readSubExpr());
12477   C->setSourceExprs(Exprs);
12478   Exprs.clear();
12479   for (unsigned i = 0; i != NumVars; ++i)
12480     Exprs.push_back(Record.readSubExpr());
12481   C->setDestinationExprs(Exprs);
12482   Exprs.clear();
12483   for (unsigned i = 0; i != NumVars; ++i)
12484     Exprs.push_back(Record.readSubExpr());
12485   C->setAssignmentOps(Exprs);
12486 }
12487 
12488 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12489   C->setLParenLoc(Record.readSourceLocation());
12490   unsigned NumVars = C->varlist_size();
12491   SmallVector<Expr *, 16> Vars;
12492   Vars.reserve(NumVars);
12493   for (unsigned i = 0; i != NumVars; ++i)
12494     Vars.push_back(Record.readSubExpr());
12495   C->setVarRefs(Vars);
12496 }
12497 
12498 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12499   C->setDepobj(Record.readSubExpr());
12500   C->setLParenLoc(Record.readSourceLocation());
12501 }
12502 
12503 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12504   C->setLParenLoc(Record.readSourceLocation());
12505   C->setModifier(Record.readSubExpr());
12506   C->setDependencyKind(
12507       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12508   C->setDependencyLoc(Record.readSourceLocation());
12509   C->setColonLoc(Record.readSourceLocation());
12510   unsigned NumVars = C->varlist_size();
12511   SmallVector<Expr *, 16> Vars;
12512   Vars.reserve(NumVars);
12513   for (unsigned I = 0; I != NumVars; ++I)
12514     Vars.push_back(Record.readSubExpr());
12515   C->setVarRefs(Vars);
12516   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12517     C->setLoopData(I, Record.readSubExpr());
12518 }
12519 
12520 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12521   VisitOMPClauseWithPreInit(C);
12522   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12523   C->setDevice(Record.readSubExpr());
12524   C->setModifierLoc(Record.readSourceLocation());
12525   C->setLParenLoc(Record.readSourceLocation());
12526 }
12527 
12528 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12529   C->setLParenLoc(Record.readSourceLocation());
12530   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12531     C->setMapTypeModifier(
12532         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12533     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12534   }
12535   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12536   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12537   C->setMapType(
12538      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12539   C->setMapLoc(Record.readSourceLocation());
12540   C->setColonLoc(Record.readSourceLocation());
12541   auto NumVars = C->varlist_size();
12542   auto UniqueDecls = C->getUniqueDeclarationsNum();
12543   auto TotalLists = C->getTotalComponentListNum();
12544   auto TotalComponents = C->getTotalComponentsNum();
12545 
12546   SmallVector<Expr *, 16> Vars;
12547   Vars.reserve(NumVars);
12548   for (unsigned i = 0; i != NumVars; ++i)
12549     Vars.push_back(Record.readExpr());
12550   C->setVarRefs(Vars);
12551 
12552   SmallVector<Expr *, 16> UDMappers;
12553   UDMappers.reserve(NumVars);
12554   for (unsigned I = 0; I < NumVars; ++I)
12555     UDMappers.push_back(Record.readExpr());
12556   C->setUDMapperRefs(UDMappers);
12557 
12558   SmallVector<ValueDecl *, 16> Decls;
12559   Decls.reserve(UniqueDecls);
12560   for (unsigned i = 0; i < UniqueDecls; ++i)
12561     Decls.push_back(Record.readDeclAs<ValueDecl>());
12562   C->setUniqueDecls(Decls);
12563 
12564   SmallVector<unsigned, 16> ListsPerDecl;
12565   ListsPerDecl.reserve(UniqueDecls);
12566   for (unsigned i = 0; i < UniqueDecls; ++i)
12567     ListsPerDecl.push_back(Record.readInt());
12568   C->setDeclNumLists(ListsPerDecl);
12569 
12570   SmallVector<unsigned, 32> ListSizes;
12571   ListSizes.reserve(TotalLists);
12572   for (unsigned i = 0; i < TotalLists; ++i)
12573     ListSizes.push_back(Record.readInt());
12574   C->setComponentListSizes(ListSizes);
12575 
12576   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12577   Components.reserve(TotalComponents);
12578   for (unsigned i = 0; i < TotalComponents; ++i) {
12579     Expr *AssociatedExprPr = Record.readExpr();
12580     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12581     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12582                             /*IsNonContiguous=*/false);
12583   }
12584   C->setComponents(Components, ListSizes);
12585 }
12586 
12587 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12588   C->setLParenLoc(Record.readSourceLocation());
12589   C->setColonLoc(Record.readSourceLocation());
12590   C->setAllocator(Record.readSubExpr());
12591   unsigned NumVars = C->varlist_size();
12592   SmallVector<Expr *, 16> Vars;
12593   Vars.reserve(NumVars);
12594   for (unsigned i = 0; i != NumVars; ++i)
12595     Vars.push_back(Record.readSubExpr());
12596   C->setVarRefs(Vars);
12597 }
12598 
12599 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12600   VisitOMPClauseWithPreInit(C);
12601   C->setNumTeams(Record.readSubExpr());
12602   C->setLParenLoc(Record.readSourceLocation());
12603 }
12604 
12605 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12606   VisitOMPClauseWithPreInit(C);
12607   C->setThreadLimit(Record.readSubExpr());
12608   C->setLParenLoc(Record.readSourceLocation());
12609 }
12610 
12611 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12612   VisitOMPClauseWithPreInit(C);
12613   C->setPriority(Record.readSubExpr());
12614   C->setLParenLoc(Record.readSourceLocation());
12615 }
12616 
12617 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12618   VisitOMPClauseWithPreInit(C);
12619   C->setGrainsize(Record.readSubExpr());
12620   C->setLParenLoc(Record.readSourceLocation());
12621 }
12622 
12623 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12624   VisitOMPClauseWithPreInit(C);
12625   C->setNumTasks(Record.readSubExpr());
12626   C->setLParenLoc(Record.readSourceLocation());
12627 }
12628 
12629 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12630   C->setHint(Record.readSubExpr());
12631   C->setLParenLoc(Record.readSourceLocation());
12632 }
12633 
12634 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12635   VisitOMPClauseWithPreInit(C);
12636   C->setDistScheduleKind(
12637       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12638   C->setChunkSize(Record.readSubExpr());
12639   C->setLParenLoc(Record.readSourceLocation());
12640   C->setDistScheduleKindLoc(Record.readSourceLocation());
12641   C->setCommaLoc(Record.readSourceLocation());
12642 }
12643 
12644 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12645   C->setDefaultmapKind(
12646        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12647   C->setDefaultmapModifier(
12648       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12649   C->setLParenLoc(Record.readSourceLocation());
12650   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12651   C->setDefaultmapKindLoc(Record.readSourceLocation());
12652 }
12653 
12654 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12655   C->setLParenLoc(Record.readSourceLocation());
12656   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12657     C->setMotionModifier(
12658         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12659     C->setMotionModifierLoc(I, Record.readSourceLocation());
12660   }
12661   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12662   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12663   C->setColonLoc(Record.readSourceLocation());
12664   auto NumVars = C->varlist_size();
12665   auto UniqueDecls = C->getUniqueDeclarationsNum();
12666   auto TotalLists = C->getTotalComponentListNum();
12667   auto TotalComponents = C->getTotalComponentsNum();
12668 
12669   SmallVector<Expr *, 16> Vars;
12670   Vars.reserve(NumVars);
12671   for (unsigned i = 0; i != NumVars; ++i)
12672     Vars.push_back(Record.readSubExpr());
12673   C->setVarRefs(Vars);
12674 
12675   SmallVector<Expr *, 16> UDMappers;
12676   UDMappers.reserve(NumVars);
12677   for (unsigned I = 0; I < NumVars; ++I)
12678     UDMappers.push_back(Record.readSubExpr());
12679   C->setUDMapperRefs(UDMappers);
12680 
12681   SmallVector<ValueDecl *, 16> Decls;
12682   Decls.reserve(UniqueDecls);
12683   for (unsigned i = 0; i < UniqueDecls; ++i)
12684     Decls.push_back(Record.readDeclAs<ValueDecl>());
12685   C->setUniqueDecls(Decls);
12686 
12687   SmallVector<unsigned, 16> ListsPerDecl;
12688   ListsPerDecl.reserve(UniqueDecls);
12689   for (unsigned i = 0; i < UniqueDecls; ++i)
12690     ListsPerDecl.push_back(Record.readInt());
12691   C->setDeclNumLists(ListsPerDecl);
12692 
12693   SmallVector<unsigned, 32> ListSizes;
12694   ListSizes.reserve(TotalLists);
12695   for (unsigned i = 0; i < TotalLists; ++i)
12696     ListSizes.push_back(Record.readInt());
12697   C->setComponentListSizes(ListSizes);
12698 
12699   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12700   Components.reserve(TotalComponents);
12701   for (unsigned i = 0; i < TotalComponents; ++i) {
12702     Expr *AssociatedExprPr = Record.readSubExpr();
12703     bool IsNonContiguous = Record.readBool();
12704     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12705     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12706   }
12707   C->setComponents(Components, ListSizes);
12708 }
12709 
12710 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12711   C->setLParenLoc(Record.readSourceLocation());
12712   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12713     C->setMotionModifier(
12714         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12715     C->setMotionModifierLoc(I, Record.readSourceLocation());
12716   }
12717   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12718   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12719   C->setColonLoc(Record.readSourceLocation());
12720   auto NumVars = C->varlist_size();
12721   auto UniqueDecls = C->getUniqueDeclarationsNum();
12722   auto TotalLists = C->getTotalComponentListNum();
12723   auto TotalComponents = C->getTotalComponentsNum();
12724 
12725   SmallVector<Expr *, 16> Vars;
12726   Vars.reserve(NumVars);
12727   for (unsigned i = 0; i != NumVars; ++i)
12728     Vars.push_back(Record.readSubExpr());
12729   C->setVarRefs(Vars);
12730 
12731   SmallVector<Expr *, 16> UDMappers;
12732   UDMappers.reserve(NumVars);
12733   for (unsigned I = 0; I < NumVars; ++I)
12734     UDMappers.push_back(Record.readSubExpr());
12735   C->setUDMapperRefs(UDMappers);
12736 
12737   SmallVector<ValueDecl *, 16> Decls;
12738   Decls.reserve(UniqueDecls);
12739   for (unsigned i = 0; i < UniqueDecls; ++i)
12740     Decls.push_back(Record.readDeclAs<ValueDecl>());
12741   C->setUniqueDecls(Decls);
12742 
12743   SmallVector<unsigned, 16> ListsPerDecl;
12744   ListsPerDecl.reserve(UniqueDecls);
12745   for (unsigned i = 0; i < UniqueDecls; ++i)
12746     ListsPerDecl.push_back(Record.readInt());
12747   C->setDeclNumLists(ListsPerDecl);
12748 
12749   SmallVector<unsigned, 32> ListSizes;
12750   ListSizes.reserve(TotalLists);
12751   for (unsigned i = 0; i < TotalLists; ++i)
12752     ListSizes.push_back(Record.readInt());
12753   C->setComponentListSizes(ListSizes);
12754 
12755   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12756   Components.reserve(TotalComponents);
12757   for (unsigned i = 0; i < TotalComponents; ++i) {
12758     Expr *AssociatedExprPr = Record.readSubExpr();
12759     bool IsNonContiguous = Record.readBool();
12760     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12761     Components.emplace_back(AssociatedExprPr, AssociatedDecl, IsNonContiguous);
12762   }
12763   C->setComponents(Components, ListSizes);
12764 }
12765 
12766 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12767   C->setLParenLoc(Record.readSourceLocation());
12768   auto NumVars = C->varlist_size();
12769   auto UniqueDecls = C->getUniqueDeclarationsNum();
12770   auto TotalLists = C->getTotalComponentListNum();
12771   auto TotalComponents = C->getTotalComponentsNum();
12772 
12773   SmallVector<Expr *, 16> Vars;
12774   Vars.reserve(NumVars);
12775   for (unsigned i = 0; i != NumVars; ++i)
12776     Vars.push_back(Record.readSubExpr());
12777   C->setVarRefs(Vars);
12778   Vars.clear();
12779   for (unsigned i = 0; i != NumVars; ++i)
12780     Vars.push_back(Record.readSubExpr());
12781   C->setPrivateCopies(Vars);
12782   Vars.clear();
12783   for (unsigned i = 0; i != NumVars; ++i)
12784     Vars.push_back(Record.readSubExpr());
12785   C->setInits(Vars);
12786 
12787   SmallVector<ValueDecl *, 16> Decls;
12788   Decls.reserve(UniqueDecls);
12789   for (unsigned i = 0; i < UniqueDecls; ++i)
12790     Decls.push_back(Record.readDeclAs<ValueDecl>());
12791   C->setUniqueDecls(Decls);
12792 
12793   SmallVector<unsigned, 16> ListsPerDecl;
12794   ListsPerDecl.reserve(UniqueDecls);
12795   for (unsigned i = 0; i < UniqueDecls; ++i)
12796     ListsPerDecl.push_back(Record.readInt());
12797   C->setDeclNumLists(ListsPerDecl);
12798 
12799   SmallVector<unsigned, 32> ListSizes;
12800   ListSizes.reserve(TotalLists);
12801   for (unsigned i = 0; i < TotalLists; ++i)
12802     ListSizes.push_back(Record.readInt());
12803   C->setComponentListSizes(ListSizes);
12804 
12805   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12806   Components.reserve(TotalComponents);
12807   for (unsigned i = 0; i < TotalComponents; ++i) {
12808     auto *AssociatedExprPr = Record.readSubExpr();
12809     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12810     Components.emplace_back(AssociatedExprPr, AssociatedDecl,
12811                             /*IsNonContiguous=*/false);
12812   }
12813   C->setComponents(Components, ListSizes);
12814 }
12815 
12816 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12817   C->setLParenLoc(Record.readSourceLocation());
12818   auto NumVars = C->varlist_size();
12819   auto UniqueDecls = C->getUniqueDeclarationsNum();
12820   auto TotalLists = C->getTotalComponentListNum();
12821   auto TotalComponents = C->getTotalComponentsNum();
12822 
12823   SmallVector<Expr *, 16> Vars;
12824   Vars.reserve(NumVars);
12825   for (unsigned i = 0; i != NumVars; ++i)
12826     Vars.push_back(Record.readSubExpr());
12827   C->setVarRefs(Vars);
12828 
12829   SmallVector<ValueDecl *, 16> Decls;
12830   Decls.reserve(UniqueDecls);
12831   for (unsigned i = 0; i < UniqueDecls; ++i)
12832     Decls.push_back(Record.readDeclAs<ValueDecl>());
12833   C->setUniqueDecls(Decls);
12834 
12835   SmallVector<unsigned, 16> ListsPerDecl;
12836   ListsPerDecl.reserve(UniqueDecls);
12837   for (unsigned i = 0; i < UniqueDecls; ++i)
12838     ListsPerDecl.push_back(Record.readInt());
12839   C->setDeclNumLists(ListsPerDecl);
12840 
12841   SmallVector<unsigned, 32> ListSizes;
12842   ListSizes.reserve(TotalLists);
12843   for (unsigned i = 0; i < TotalLists; ++i)
12844     ListSizes.push_back(Record.readInt());
12845   C->setComponentListSizes(ListSizes);
12846 
12847   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12848   Components.reserve(TotalComponents);
12849   for (unsigned i = 0; i < TotalComponents; ++i) {
12850     Expr *AssociatedExpr = Record.readSubExpr();
12851     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12852     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12853                             /*IsNonContiguous*/ false);
12854   }
12855   C->setComponents(Components, ListSizes);
12856 }
12857 
12858 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12859   C->setLParenLoc(Record.readSourceLocation());
12860   auto NumVars = C->varlist_size();
12861   auto UniqueDecls = C->getUniqueDeclarationsNum();
12862   auto TotalLists = C->getTotalComponentListNum();
12863   auto TotalComponents = C->getTotalComponentsNum();
12864 
12865   SmallVector<Expr *, 16> Vars;
12866   Vars.reserve(NumVars);
12867   for (unsigned i = 0; i != NumVars; ++i)
12868     Vars.push_back(Record.readSubExpr());
12869   C->setVarRefs(Vars);
12870   Vars.clear();
12871 
12872   SmallVector<ValueDecl *, 16> Decls;
12873   Decls.reserve(UniqueDecls);
12874   for (unsigned i = 0; i < UniqueDecls; ++i)
12875     Decls.push_back(Record.readDeclAs<ValueDecl>());
12876   C->setUniqueDecls(Decls);
12877 
12878   SmallVector<unsigned, 16> ListsPerDecl;
12879   ListsPerDecl.reserve(UniqueDecls);
12880   for (unsigned i = 0; i < UniqueDecls; ++i)
12881     ListsPerDecl.push_back(Record.readInt());
12882   C->setDeclNumLists(ListsPerDecl);
12883 
12884   SmallVector<unsigned, 32> ListSizes;
12885   ListSizes.reserve(TotalLists);
12886   for (unsigned i = 0; i < TotalLists; ++i)
12887     ListSizes.push_back(Record.readInt());
12888   C->setComponentListSizes(ListSizes);
12889 
12890   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12891   Components.reserve(TotalComponents);
12892   for (unsigned i = 0; i < TotalComponents; ++i) {
12893     Expr *AssociatedExpr = Record.readSubExpr();
12894     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12895     Components.emplace_back(AssociatedExpr, AssociatedDecl,
12896                             /*IsNonContiguous=*/false);
12897   }
12898   C->setComponents(Components, ListSizes);
12899 }
12900 
12901 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12902   C->setLParenLoc(Record.readSourceLocation());
12903   unsigned NumVars = C->varlist_size();
12904   SmallVector<Expr *, 16> Vars;
12905   Vars.reserve(NumVars);
12906   for (unsigned i = 0; i != NumVars; ++i)
12907     Vars.push_back(Record.readSubExpr());
12908   C->setVarRefs(Vars);
12909   Vars.clear();
12910   Vars.reserve(NumVars);
12911   for (unsigned i = 0; i != NumVars; ++i)
12912     Vars.push_back(Record.readSubExpr());
12913   C->setPrivateRefs(Vars);
12914 }
12915 
12916 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12917   C->setLParenLoc(Record.readSourceLocation());
12918   unsigned NumVars = C->varlist_size();
12919   SmallVector<Expr *, 16> Vars;
12920   Vars.reserve(NumVars);
12921   for (unsigned i = 0; i != NumVars; ++i)
12922     Vars.push_back(Record.readSubExpr());
12923   C->setVarRefs(Vars);
12924 }
12925 
12926 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12927   C->setLParenLoc(Record.readSourceLocation());
12928   unsigned NumVars = C->varlist_size();
12929   SmallVector<Expr *, 16> Vars;
12930   Vars.reserve(NumVars);
12931   for (unsigned i = 0; i != NumVars; ++i)
12932     Vars.push_back(Record.readSubExpr());
12933   C->setVarRefs(Vars);
12934 }
12935 
12936 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12937   C->setLParenLoc(Record.readSourceLocation());
12938   unsigned NumOfAllocators = C->getNumberOfAllocators();
12939   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12940   Data.reserve(NumOfAllocators);
12941   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12942     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12943     D.Allocator = Record.readSubExpr();
12944     D.AllocatorTraits = Record.readSubExpr();
12945     D.LParenLoc = Record.readSourceLocation();
12946     D.RParenLoc = Record.readSourceLocation();
12947   }
12948   C->setAllocatorsData(Data);
12949 }
12950 
12951 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12952   C->setLParenLoc(Record.readSourceLocation());
12953   C->setModifier(Record.readSubExpr());
12954   C->setColonLoc(Record.readSourceLocation());
12955   unsigned NumOfLocators = C->varlist_size();
12956   SmallVector<Expr *, 4> Locators;
12957   Locators.reserve(NumOfLocators);
12958   for (unsigned I = 0; I != NumOfLocators; ++I)
12959     Locators.push_back(Record.readSubExpr());
12960   C->setVarRefs(Locators);
12961 }
12962 
12963 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12964   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12965   C->setLParenLoc(Record.readSourceLocation());
12966   C->setKindKwLoc(Record.readSourceLocation());
12967 }
12968 
12969 void OMPClauseReader::VisitOMPFilterClause(OMPFilterClause *C) {
12970   VisitOMPClauseWithPreInit(C);
12971   C->setThreadID(Record.readSubExpr());
12972   C->setLParenLoc(Record.readSourceLocation());
12973 }
12974 
12975 void OMPClauseReader::VisitOMPBindClause(OMPBindClause *C) {
12976   C->setBindKind(Record.readEnum<OpenMPBindClauseKind>());
12977   C->setLParenLoc(Record.readSourceLocation());
12978   C->setBindKindLoc(Record.readSourceLocation());
12979 }
12980 
12981 void OMPClauseReader::VisitOMPAlignClause(OMPAlignClause *C) {
12982   C->setAlignment(Record.readExpr());
12983   C->setLParenLoc(Record.readSourceLocation());
12984 }
12985 
12986 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12987   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12988   TI.Sets.resize(readUInt32());
12989   for (auto &Set : TI.Sets) {
12990     Set.Kind = readEnum<llvm::omp::TraitSet>();
12991     Set.Selectors.resize(readUInt32());
12992     for (auto &Selector : Set.Selectors) {
12993       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12994       Selector.ScoreOrCondition = nullptr;
12995       if (readBool())
12996         Selector.ScoreOrCondition = readExprRef();
12997       Selector.Properties.resize(readUInt32());
12998       for (auto &Property : Selector.Properties)
12999         Property.Kind = readEnum<llvm::omp::TraitProperty>();
13000     }
13001   }
13002   return &TI;
13003 }
13004 
13005 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
13006   if (!Data)
13007     return;
13008   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
13009     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
13010     skipInts(3);
13011   }
13012   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
13013   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
13014     Clauses[I] = readOMPClause();
13015   Data->setClauses(Clauses);
13016   if (Data->hasAssociatedStmt())
13017     Data->setAssociatedStmt(readStmt());
13018   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
13019     Data->getChildren()[I] = readStmt();
13020 }
13021