1 //===- ASTReader.cpp - AST File Reader ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTReader class, which reads AST files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Basic/OpenMPKinds.h"
14 #include "clang/Serialization/ASTRecordReader.h"
15 #include "ASTCommon.h"
16 #include "ASTReaderInternals.h"
17 #include "clang/AST/AbstractTypeReader.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/ASTMutationListener.h"
21 #include "clang/AST/ASTUnresolvedSet.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclBase.h"
24 #include "clang/AST/DeclCXX.h"
25 #include "clang/AST/DeclFriend.h"
26 #include "clang/AST/DeclGroup.h"
27 #include "clang/AST/DeclObjC.h"
28 #include "clang/AST/DeclTemplate.h"
29 #include "clang/AST/DeclarationName.h"
30 #include "clang/AST/Expr.h"
31 #include "clang/AST/ExprCXX.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/AST/NestedNameSpecifier.h"
34 #include "clang/AST/OpenMPClause.h"
35 #include "clang/AST/ODRHash.h"
36 #include "clang/AST/RawCommentList.h"
37 #include "clang/AST/TemplateBase.h"
38 #include "clang/AST/TemplateName.h"
39 #include "clang/AST/Type.h"
40 #include "clang/AST/TypeLoc.h"
41 #include "clang/AST/TypeLocVisitor.h"
42 #include "clang/AST/UnresolvedSet.h"
43 #include "clang/Basic/CommentOptions.h"
44 #include "clang/Basic/Diagnostic.h"
45 #include "clang/Basic/DiagnosticOptions.h"
46 #include "clang/Basic/ExceptionSpecificationType.h"
47 #include "clang/Basic/FileManager.h"
48 #include "clang/Basic/FileSystemOptions.h"
49 #include "clang/Basic/IdentifierTable.h"
50 #include "clang/Basic/LLVM.h"
51 #include "clang/Basic/LangOptions.h"
52 #include "clang/Basic/Module.h"
53 #include "clang/Basic/ObjCRuntime.h"
54 #include "clang/Basic/OperatorKinds.h"
55 #include "clang/Basic/PragmaKinds.h"
56 #include "clang/Basic/Sanitizers.h"
57 #include "clang/Basic/SourceLocation.h"
58 #include "clang/Basic/SourceManager.h"
59 #include "clang/Basic/SourceManagerInternals.h"
60 #include "clang/Basic/Specifiers.h"
61 #include "clang/Basic/TargetInfo.h"
62 #include "clang/Basic/TargetOptions.h"
63 #include "clang/Basic/TokenKinds.h"
64 #include "clang/Basic/Version.h"
65 #include "clang/Lex/HeaderSearch.h"
66 #include "clang/Lex/HeaderSearchOptions.h"
67 #include "clang/Lex/MacroInfo.h"
68 #include "clang/Lex/ModuleMap.h"
69 #include "clang/Lex/PreprocessingRecord.h"
70 #include "clang/Lex/Preprocessor.h"
71 #include "clang/Lex/PreprocessorOptions.h"
72 #include "clang/Lex/Token.h"
73 #include "clang/Sema/ObjCMethodList.h"
74 #include "clang/Sema/Scope.h"
75 #include "clang/Sema/Sema.h"
76 #include "clang/Sema/Weak.h"
77 #include "clang/Serialization/ASTBitCodes.h"
78 #include "clang/Serialization/ASTDeserializationListener.h"
79 #include "clang/Serialization/ContinuousRangeMap.h"
80 #include "clang/Serialization/GlobalModuleIndex.h"
81 #include "clang/Serialization/InMemoryModuleCache.h"
82 #include "clang/Serialization/ModuleFile.h"
83 #include "clang/Serialization/ModuleFileExtension.h"
84 #include "clang/Serialization/ModuleManager.h"
85 #include "clang/Serialization/PCHContainerOperations.h"
86 #include "clang/Serialization/SerializationDiagnostic.h"
87 #include "llvm/ADT/APFloat.h"
88 #include "llvm/ADT/APInt.h"
89 #include "llvm/ADT/APSInt.h"
90 #include "llvm/ADT/ArrayRef.h"
91 #include "llvm/ADT/DenseMap.h"
92 #include "llvm/ADT/FloatingPointMode.h"
93 #include "llvm/ADT/FoldingSet.h"
94 #include "llvm/ADT/Hashing.h"
95 #include "llvm/ADT/IntrusiveRefCntPtr.h"
96 #include "llvm/ADT/None.h"
97 #include "llvm/ADT/Optional.h"
98 #include "llvm/ADT/STLExtras.h"
99 #include "llvm/ADT/ScopeExit.h"
100 #include "llvm/ADT/SmallPtrSet.h"
101 #include "llvm/ADT/SmallString.h"
102 #include "llvm/ADT/SmallVector.h"
103 #include "llvm/ADT/StringExtras.h"
104 #include "llvm/ADT/StringMap.h"
105 #include "llvm/ADT/StringRef.h"
106 #include "llvm/ADT/Triple.h"
107 #include "llvm/ADT/iterator_range.h"
108 #include "llvm/Bitstream/BitstreamReader.h"
109 #include "llvm/Support/Casting.h"
110 #include "llvm/Support/Compiler.h"
111 #include "llvm/Support/Compression.h"
112 #include "llvm/Support/DJB.h"
113 #include "llvm/Support/Endian.h"
114 #include "llvm/Support/Error.h"
115 #include "llvm/Support/ErrorHandling.h"
116 #include "llvm/Support/FileSystem.h"
117 #include "llvm/Support/MemoryBuffer.h"
118 #include "llvm/Support/Path.h"
119 #include "llvm/Support/SaveAndRestore.h"
120 #include "llvm/Support/Timer.h"
121 #include "llvm/Support/VersionTuple.h"
122 #include "llvm/Support/raw_ostream.h"
123 #include <algorithm>
124 #include <cassert>
125 #include <cstddef>
126 #include <cstdint>
127 #include <cstdio>
128 #include <ctime>
129 #include <iterator>
130 #include <limits>
131 #include <map>
132 #include <memory>
133 #include <string>
134 #include <system_error>
135 #include <tuple>
136 #include <utility>
137 #include <vector>
138 
139 using namespace clang;
140 using namespace clang::serialization;
141 using namespace clang::serialization::reader;
142 using llvm::BitstreamCursor;
143 using llvm::RoundingMode;
144 
145 //===----------------------------------------------------------------------===//
146 // ChainedASTReaderListener implementation
147 //===----------------------------------------------------------------------===//
148 
149 bool
150 ChainedASTReaderListener::ReadFullVersionInformation(StringRef FullVersion) {
151   return First->ReadFullVersionInformation(FullVersion) ||
152          Second->ReadFullVersionInformation(FullVersion);
153 }
154 
155 void ChainedASTReaderListener::ReadModuleName(StringRef ModuleName) {
156   First->ReadModuleName(ModuleName);
157   Second->ReadModuleName(ModuleName);
158 }
159 
160 void ChainedASTReaderListener::ReadModuleMapFile(StringRef ModuleMapPath) {
161   First->ReadModuleMapFile(ModuleMapPath);
162   Second->ReadModuleMapFile(ModuleMapPath);
163 }
164 
165 bool
166 ChainedASTReaderListener::ReadLanguageOptions(const LangOptions &LangOpts,
167                                               bool Complain,
168                                               bool AllowCompatibleDifferences) {
169   return First->ReadLanguageOptions(LangOpts, Complain,
170                                     AllowCompatibleDifferences) ||
171          Second->ReadLanguageOptions(LangOpts, Complain,
172                                      AllowCompatibleDifferences);
173 }
174 
175 bool ChainedASTReaderListener::ReadTargetOptions(
176     const TargetOptions &TargetOpts, bool Complain,
177     bool AllowCompatibleDifferences) {
178   return First->ReadTargetOptions(TargetOpts, Complain,
179                                   AllowCompatibleDifferences) ||
180          Second->ReadTargetOptions(TargetOpts, Complain,
181                                    AllowCompatibleDifferences);
182 }
183 
184 bool ChainedASTReaderListener::ReadDiagnosticOptions(
185     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
186   return First->ReadDiagnosticOptions(DiagOpts, Complain) ||
187          Second->ReadDiagnosticOptions(DiagOpts, Complain);
188 }
189 
190 bool
191 ChainedASTReaderListener::ReadFileSystemOptions(const FileSystemOptions &FSOpts,
192                                                 bool Complain) {
193   return First->ReadFileSystemOptions(FSOpts, Complain) ||
194          Second->ReadFileSystemOptions(FSOpts, Complain);
195 }
196 
197 bool ChainedASTReaderListener::ReadHeaderSearchOptions(
198     const HeaderSearchOptions &HSOpts, StringRef SpecificModuleCachePath,
199     bool Complain) {
200   return First->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
201                                         Complain) ||
202          Second->ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
203                                          Complain);
204 }
205 
206 bool ChainedASTReaderListener::ReadPreprocessorOptions(
207     const PreprocessorOptions &PPOpts, bool Complain,
208     std::string &SuggestedPredefines) {
209   return First->ReadPreprocessorOptions(PPOpts, Complain,
210                                         SuggestedPredefines) ||
211          Second->ReadPreprocessorOptions(PPOpts, Complain, SuggestedPredefines);
212 }
213 
214 void ChainedASTReaderListener::ReadCounter(const serialization::ModuleFile &M,
215                                            unsigned Value) {
216   First->ReadCounter(M, Value);
217   Second->ReadCounter(M, Value);
218 }
219 
220 bool ChainedASTReaderListener::needsInputFileVisitation() {
221   return First->needsInputFileVisitation() ||
222          Second->needsInputFileVisitation();
223 }
224 
225 bool ChainedASTReaderListener::needsSystemInputFileVisitation() {
226   return First->needsSystemInputFileVisitation() ||
227   Second->needsSystemInputFileVisitation();
228 }
229 
230 void ChainedASTReaderListener::visitModuleFile(StringRef Filename,
231                                                ModuleKind Kind) {
232   First->visitModuleFile(Filename, Kind);
233   Second->visitModuleFile(Filename, Kind);
234 }
235 
236 bool ChainedASTReaderListener::visitInputFile(StringRef Filename,
237                                               bool isSystem,
238                                               bool isOverridden,
239                                               bool isExplicitModule) {
240   bool Continue = false;
241   if (First->needsInputFileVisitation() &&
242       (!isSystem || First->needsSystemInputFileVisitation()))
243     Continue |= First->visitInputFile(Filename, isSystem, isOverridden,
244                                       isExplicitModule);
245   if (Second->needsInputFileVisitation() &&
246       (!isSystem || Second->needsSystemInputFileVisitation()))
247     Continue |= Second->visitInputFile(Filename, isSystem, isOverridden,
248                                        isExplicitModule);
249   return Continue;
250 }
251 
252 void ChainedASTReaderListener::readModuleFileExtension(
253        const ModuleFileExtensionMetadata &Metadata) {
254   First->readModuleFileExtension(Metadata);
255   Second->readModuleFileExtension(Metadata);
256 }
257 
258 //===----------------------------------------------------------------------===//
259 // PCH validator implementation
260 //===----------------------------------------------------------------------===//
261 
262 ASTReaderListener::~ASTReaderListener() = default;
263 
264 /// Compare the given set of language options against an existing set of
265 /// language options.
266 ///
267 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
268 /// \param AllowCompatibleDifferences If true, differences between compatible
269 ///        language options will be permitted.
270 ///
271 /// \returns true if the languagae options mis-match, false otherwise.
272 static bool checkLanguageOptions(const LangOptions &LangOpts,
273                                  const LangOptions &ExistingLangOpts,
274                                  DiagnosticsEngine *Diags,
275                                  bool AllowCompatibleDifferences = true) {
276 #define LANGOPT(Name, Bits, Default, Description)                 \
277   if (ExistingLangOpts.Name != LangOpts.Name) {                   \
278     if (Diags)                                                    \
279       Diags->Report(diag::err_pch_langopt_mismatch)               \
280         << Description << LangOpts.Name << ExistingLangOpts.Name; \
281     return true;                                                  \
282   }
283 
284 #define VALUE_LANGOPT(Name, Bits, Default, Description)   \
285   if (ExistingLangOpts.Name != LangOpts.Name) {           \
286     if (Diags)                                            \
287       Diags->Report(diag::err_pch_langopt_value_mismatch) \
288         << Description;                                   \
289     return true;                                          \
290   }
291 
292 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description)   \
293   if (ExistingLangOpts.get##Name() != LangOpts.get##Name()) {  \
294     if (Diags)                                                 \
295       Diags->Report(diag::err_pch_langopt_value_mismatch)      \
296         << Description;                                        \
297     return true;                                               \
298   }
299 
300 #define COMPATIBLE_LANGOPT(Name, Bits, Default, Description)  \
301   if (!AllowCompatibleDifferences)                            \
302     LANGOPT(Name, Bits, Default, Description)
303 
304 #define COMPATIBLE_ENUM_LANGOPT(Name, Bits, Default, Description)  \
305   if (!AllowCompatibleDifferences)                                 \
306     ENUM_LANGOPT(Name, Bits, Default, Description)
307 
308 #define COMPATIBLE_VALUE_LANGOPT(Name, Bits, Default, Description) \
309   if (!AllowCompatibleDifferences)                                 \
310     VALUE_LANGOPT(Name, Bits, Default, Description)
311 
312 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
313 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
314 #define BENIGN_VALUE_LANGOPT(Name, Type, Bits, Default, Description)
315 #include "clang/Basic/LangOptions.def"
316 
317   if (ExistingLangOpts.ModuleFeatures != LangOpts.ModuleFeatures) {
318     if (Diags)
319       Diags->Report(diag::err_pch_langopt_value_mismatch) << "module features";
320     return true;
321   }
322 
323   if (ExistingLangOpts.ObjCRuntime != LangOpts.ObjCRuntime) {
324     if (Diags)
325       Diags->Report(diag::err_pch_langopt_value_mismatch)
326       << "target Objective-C runtime";
327     return true;
328   }
329 
330   if (ExistingLangOpts.CommentOpts.BlockCommandNames !=
331       LangOpts.CommentOpts.BlockCommandNames) {
332     if (Diags)
333       Diags->Report(diag::err_pch_langopt_value_mismatch)
334         << "block command names";
335     return true;
336   }
337 
338   // Sanitizer feature mismatches are treated as compatible differences. If
339   // compatible differences aren't allowed, we still only want to check for
340   // mismatches of non-modular sanitizers (the only ones which can affect AST
341   // generation).
342   if (!AllowCompatibleDifferences) {
343     SanitizerMask ModularSanitizers = getPPTransparentSanitizers();
344     SanitizerSet ExistingSanitizers = ExistingLangOpts.Sanitize;
345     SanitizerSet ImportedSanitizers = LangOpts.Sanitize;
346     ExistingSanitizers.clear(ModularSanitizers);
347     ImportedSanitizers.clear(ModularSanitizers);
348     if (ExistingSanitizers.Mask != ImportedSanitizers.Mask) {
349       const std::string Flag = "-fsanitize=";
350       if (Diags) {
351 #define SANITIZER(NAME, ID)                                                    \
352   {                                                                            \
353     bool InExistingModule = ExistingSanitizers.has(SanitizerKind::ID);         \
354     bool InImportedModule = ImportedSanitizers.has(SanitizerKind::ID);         \
355     if (InExistingModule != InImportedModule)                                  \
356       Diags->Report(diag::err_pch_targetopt_feature_mismatch)                  \
357           << InExistingModule << (Flag + NAME);                                \
358   }
359 #include "clang/Basic/Sanitizers.def"
360       }
361       return true;
362     }
363   }
364 
365   return false;
366 }
367 
368 /// Compare the given set of target options against an existing set of
369 /// target options.
370 ///
371 /// \param Diags If non-NULL, diagnostics will be emitted via this engine.
372 ///
373 /// \returns true if the target options mis-match, false otherwise.
374 static bool checkTargetOptions(const TargetOptions &TargetOpts,
375                                const TargetOptions &ExistingTargetOpts,
376                                DiagnosticsEngine *Diags,
377                                bool AllowCompatibleDifferences = true) {
378 #define CHECK_TARGET_OPT(Field, Name)                             \
379   if (TargetOpts.Field != ExistingTargetOpts.Field) {             \
380     if (Diags)                                                    \
381       Diags->Report(diag::err_pch_targetopt_mismatch)             \
382         << Name << TargetOpts.Field << ExistingTargetOpts.Field;  \
383     return true;                                                  \
384   }
385 
386   // The triple and ABI must match exactly.
387   CHECK_TARGET_OPT(Triple, "target");
388   CHECK_TARGET_OPT(ABI, "target ABI");
389 
390   // We can tolerate different CPUs in many cases, notably when one CPU
391   // supports a strict superset of another. When allowing compatible
392   // differences skip this check.
393   if (!AllowCompatibleDifferences)
394     CHECK_TARGET_OPT(CPU, "target CPU");
395 
396 #undef CHECK_TARGET_OPT
397 
398   // Compare feature sets.
399   SmallVector<StringRef, 4> ExistingFeatures(
400                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
401                                              ExistingTargetOpts.FeaturesAsWritten.end());
402   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
403                                          TargetOpts.FeaturesAsWritten.end());
404   llvm::sort(ExistingFeatures);
405   llvm::sort(ReadFeatures);
406 
407   // We compute the set difference in both directions explicitly so that we can
408   // diagnose the differences differently.
409   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
410   std::set_difference(
411       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
412       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
413   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
414                       ExistingFeatures.begin(), ExistingFeatures.end(),
415                       std::back_inserter(UnmatchedReadFeatures));
416 
417   // If we are allowing compatible differences and the read feature set is
418   // a strict subset of the existing feature set, there is nothing to diagnose.
419   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
420     return false;
421 
422   if (Diags) {
423     for (StringRef Feature : UnmatchedReadFeatures)
424       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
425           << /* is-existing-feature */ false << Feature;
426     for (StringRef Feature : UnmatchedExistingFeatures)
427       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
428           << /* is-existing-feature */ true << Feature;
429   }
430 
431   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
432 }
433 
434 bool
435 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
436                                   bool Complain,
437                                   bool AllowCompatibleDifferences) {
438   const LangOptions &ExistingLangOpts = PP.getLangOpts();
439   return checkLanguageOptions(LangOpts, ExistingLangOpts,
440                               Complain ? &Reader.Diags : nullptr,
441                               AllowCompatibleDifferences);
442 }
443 
444 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
445                                      bool Complain,
446                                      bool AllowCompatibleDifferences) {
447   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
448   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
449                             Complain ? &Reader.Diags : nullptr,
450                             AllowCompatibleDifferences);
451 }
452 
453 namespace {
454 
455 using MacroDefinitionsMap =
456     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
457 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
458 
459 } // namespace
460 
461 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
462                                          DiagnosticsEngine &Diags,
463                                          bool Complain) {
464   using Level = DiagnosticsEngine::Level;
465 
466   // Check current mappings for new -Werror mappings, and the stored mappings
467   // for cases that were explicitly mapped to *not* be errors that are now
468   // errors because of options like -Werror.
469   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
470 
471   for (DiagnosticsEngine *MappingSource : MappingSources) {
472     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
473       diag::kind DiagID = DiagIDMappingPair.first;
474       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
475       if (CurLevel < DiagnosticsEngine::Error)
476         continue; // not significant
477       Level StoredLevel =
478           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
479       if (StoredLevel < DiagnosticsEngine::Error) {
480         if (Complain)
481           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
482               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
483         return true;
484       }
485     }
486   }
487 
488   return false;
489 }
490 
491 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
492   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
493   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
494     return true;
495   return Ext >= diag::Severity::Error;
496 }
497 
498 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
499                                     DiagnosticsEngine &Diags,
500                                     bool IsSystem, bool Complain) {
501   // Top-level options
502   if (IsSystem) {
503     if (Diags.getSuppressSystemWarnings())
504       return false;
505     // If -Wsystem-headers was not enabled before, be conservative
506     if (StoredDiags.getSuppressSystemWarnings()) {
507       if (Complain)
508         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
509       return true;
510     }
511   }
512 
513   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
514     if (Complain)
515       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
516     return true;
517   }
518 
519   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
520       !StoredDiags.getEnableAllWarnings()) {
521     if (Complain)
522       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
523     return true;
524   }
525 
526   if (isExtHandlingFromDiagsError(Diags) &&
527       !isExtHandlingFromDiagsError(StoredDiags)) {
528     if (Complain)
529       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
530     return true;
531   }
532 
533   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
534 }
535 
536 /// Return the top import module if it is implicit, nullptr otherwise.
537 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
538                                           Preprocessor &PP) {
539   // If the original import came from a file explicitly generated by the user,
540   // don't check the diagnostic mappings.
541   // FIXME: currently this is approximated by checking whether this is not a
542   // module import of an implicitly-loaded module file.
543   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
544   // the transitive closure of its imports, since unrelated modules cannot be
545   // imported until after this module finishes validation.
546   ModuleFile *TopImport = &*ModuleMgr.rbegin();
547   while (!TopImport->ImportedBy.empty())
548     TopImport = TopImport->ImportedBy[0];
549   if (TopImport->Kind != MK_ImplicitModule)
550     return nullptr;
551 
552   StringRef ModuleName = TopImport->ModuleName;
553   assert(!ModuleName.empty() && "diagnostic options read before module name");
554 
555   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
556   assert(M && "missing module");
557   return M;
558 }
559 
560 bool PCHValidator::ReadDiagnosticOptions(
561     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
562   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
563   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
564   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
565       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
566   // This should never fail, because we would have processed these options
567   // before writing them to an ASTFile.
568   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
569 
570   ModuleManager &ModuleMgr = Reader.getModuleManager();
571   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
572 
573   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
574   if (!TopM)
575     return false;
576 
577   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
578   // contains the union of their flags.
579   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
580                                  Complain);
581 }
582 
583 /// Collect the macro definitions provided by the given preprocessor
584 /// options.
585 static void
586 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
587                         MacroDefinitionsMap &Macros,
588                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
589   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
590     StringRef Macro = PPOpts.Macros[I].first;
591     bool IsUndef = PPOpts.Macros[I].second;
592 
593     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
594     StringRef MacroName = MacroPair.first;
595     StringRef MacroBody = MacroPair.second;
596 
597     // For an #undef'd macro, we only care about the name.
598     if (IsUndef) {
599       if (MacroNames && !Macros.count(MacroName))
600         MacroNames->push_back(MacroName);
601 
602       Macros[MacroName] = std::make_pair("", true);
603       continue;
604     }
605 
606     // For a #define'd macro, figure out the actual definition.
607     if (MacroName.size() == Macro.size())
608       MacroBody = "1";
609     else {
610       // Note: GCC drops anything following an end-of-line character.
611       StringRef::size_type End = MacroBody.find_first_of("\n\r");
612       MacroBody = MacroBody.substr(0, End);
613     }
614 
615     if (MacroNames && !Macros.count(MacroName))
616       MacroNames->push_back(MacroName);
617     Macros[MacroName] = std::make_pair(MacroBody, false);
618   }
619 }
620 
621 /// Check the preprocessor options deserialized from the control block
622 /// against the preprocessor options in an existing preprocessor.
623 ///
624 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
625 /// \param Validate If true, validate preprocessor options. If false, allow
626 ///        macros defined by \p ExistingPPOpts to override those defined by
627 ///        \p PPOpts in SuggestedPredefines.
628 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
629                                      const PreprocessorOptions &ExistingPPOpts,
630                                      DiagnosticsEngine *Diags,
631                                      FileManager &FileMgr,
632                                      std::string &SuggestedPredefines,
633                                      const LangOptions &LangOpts,
634                                      bool Validate = true) {
635   // Check macro definitions.
636   MacroDefinitionsMap ASTFileMacros;
637   collectMacroDefinitions(PPOpts, ASTFileMacros);
638   MacroDefinitionsMap ExistingMacros;
639   SmallVector<StringRef, 4> ExistingMacroNames;
640   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
641 
642   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
643     // Dig out the macro definition in the existing preprocessor options.
644     StringRef MacroName = ExistingMacroNames[I];
645     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
646 
647     // Check whether we know anything about this macro name or not.
648     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
649         ASTFileMacros.find(MacroName);
650     if (!Validate || Known == ASTFileMacros.end()) {
651       // FIXME: Check whether this identifier was referenced anywhere in the
652       // AST file. If so, we should reject the AST file. Unfortunately, this
653       // information isn't in the control block. What shall we do about it?
654 
655       if (Existing.second) {
656         SuggestedPredefines += "#undef ";
657         SuggestedPredefines += MacroName.str();
658         SuggestedPredefines += '\n';
659       } else {
660         SuggestedPredefines += "#define ";
661         SuggestedPredefines += MacroName.str();
662         SuggestedPredefines += ' ';
663         SuggestedPredefines += Existing.first.str();
664         SuggestedPredefines += '\n';
665       }
666       continue;
667     }
668 
669     // If the macro was defined in one but undef'd in the other, we have a
670     // conflict.
671     if (Existing.second != Known->second.second) {
672       if (Diags) {
673         Diags->Report(diag::err_pch_macro_def_undef)
674           << MacroName << Known->second.second;
675       }
676       return true;
677     }
678 
679     // If the macro was #undef'd in both, or if the macro bodies are identical,
680     // it's fine.
681     if (Existing.second || Existing.first == Known->second.first)
682       continue;
683 
684     // The macro bodies differ; complain.
685     if (Diags) {
686       Diags->Report(diag::err_pch_macro_def_conflict)
687         << MacroName << Known->second.first << Existing.first;
688     }
689     return true;
690   }
691 
692   // Check whether we're using predefines.
693   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
694     if (Diags) {
695       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
696     }
697     return true;
698   }
699 
700   // Detailed record is important since it is used for the module cache hash.
701   if (LangOpts.Modules &&
702       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
703     if (Diags) {
704       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
705     }
706     return true;
707   }
708 
709   // Compute the #include and #include_macros lines we need.
710   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
711     StringRef File = ExistingPPOpts.Includes[I];
712 
713     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
714         !ExistingPPOpts.PCHThroughHeader.empty()) {
715       // In case the through header is an include, we must add all the includes
716       // to the predefines so the start point can be determined.
717       SuggestedPredefines += "#include \"";
718       SuggestedPredefines += File;
719       SuggestedPredefines += "\"\n";
720       continue;
721     }
722 
723     if (File == ExistingPPOpts.ImplicitPCHInclude)
724       continue;
725 
726     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
727           != PPOpts.Includes.end())
728       continue;
729 
730     SuggestedPredefines += "#include \"";
731     SuggestedPredefines += File;
732     SuggestedPredefines += "\"\n";
733   }
734 
735   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
736     StringRef File = ExistingPPOpts.MacroIncludes[I];
737     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
738                   File)
739         != PPOpts.MacroIncludes.end())
740       continue;
741 
742     SuggestedPredefines += "#__include_macros \"";
743     SuggestedPredefines += File;
744     SuggestedPredefines += "\"\n##\n";
745   }
746 
747   return false;
748 }
749 
750 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
751                                            bool Complain,
752                                            std::string &SuggestedPredefines) {
753   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
754 
755   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
756                                   Complain? &Reader.Diags : nullptr,
757                                   PP.getFileManager(),
758                                   SuggestedPredefines,
759                                   PP.getLangOpts());
760 }
761 
762 bool SimpleASTReaderListener::ReadPreprocessorOptions(
763                                   const PreprocessorOptions &PPOpts,
764                                   bool Complain,
765                                   std::string &SuggestedPredefines) {
766   return checkPreprocessorOptions(PPOpts,
767                                   PP.getPreprocessorOpts(),
768                                   nullptr,
769                                   PP.getFileManager(),
770                                   SuggestedPredefines,
771                                   PP.getLangOpts(),
772                                   false);
773 }
774 
775 /// Check the header search options deserialized from the control block
776 /// against the header search options in an existing preprocessor.
777 ///
778 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
779 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
780                                      StringRef SpecificModuleCachePath,
781                                      StringRef ExistingModuleCachePath,
782                                      DiagnosticsEngine *Diags,
783                                      const LangOptions &LangOpts) {
784   if (LangOpts.Modules) {
785     if (SpecificModuleCachePath != ExistingModuleCachePath) {
786       if (Diags)
787         Diags->Report(diag::err_pch_modulecache_mismatch)
788           << SpecificModuleCachePath << ExistingModuleCachePath;
789       return true;
790     }
791   }
792 
793   return false;
794 }
795 
796 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
797                                            StringRef SpecificModuleCachePath,
798                                            bool Complain) {
799   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
800                                   PP.getHeaderSearchInfo().getModuleCachePath(),
801                                   Complain ? &Reader.Diags : nullptr,
802                                   PP.getLangOpts());
803 }
804 
805 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
806   PP.setCounterValue(Value);
807 }
808 
809 //===----------------------------------------------------------------------===//
810 // AST reader implementation
811 //===----------------------------------------------------------------------===//
812 
813 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
814                                            bool TakeOwnership) {
815   DeserializationListener = Listener;
816   OwnsDeserializationListener = TakeOwnership;
817 }
818 
819 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
820   return serialization::ComputeHash(Sel);
821 }
822 
823 std::pair<unsigned, unsigned>
824 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
825   using namespace llvm::support;
826 
827   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
828   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
829   return std::make_pair(KeyLen, DataLen);
830 }
831 
832 ASTSelectorLookupTrait::internal_key_type
833 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
834   using namespace llvm::support;
835 
836   SelectorTable &SelTable = Reader.getContext().Selectors;
837   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
838   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
839       F, endian::readNext<uint32_t, little, unaligned>(d));
840   if (N == 0)
841     return SelTable.getNullarySelector(FirstII);
842   else if (N == 1)
843     return SelTable.getUnarySelector(FirstII);
844 
845   SmallVector<IdentifierInfo *, 16> Args;
846   Args.push_back(FirstII);
847   for (unsigned I = 1; I != N; ++I)
848     Args.push_back(Reader.getLocalIdentifier(
849         F, endian::readNext<uint32_t, little, unaligned>(d)));
850 
851   return SelTable.getSelector(N, Args.data());
852 }
853 
854 ASTSelectorLookupTrait::data_type
855 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
856                                  unsigned DataLen) {
857   using namespace llvm::support;
858 
859   data_type Result;
860 
861   Result.ID = Reader.getGlobalSelectorID(
862       F, endian::readNext<uint32_t, little, unaligned>(d));
863   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
864   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
865   Result.InstanceBits = FullInstanceBits & 0x3;
866   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
867   Result.FactoryBits = FullFactoryBits & 0x3;
868   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
869   unsigned NumInstanceMethods = FullInstanceBits >> 3;
870   unsigned NumFactoryMethods = FullFactoryBits >> 3;
871 
872   // Load instance methods
873   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
874     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
875             F, endian::readNext<uint32_t, little, unaligned>(d)))
876       Result.Instance.push_back(Method);
877   }
878 
879   // Load factory methods
880   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
881     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
882             F, endian::readNext<uint32_t, little, unaligned>(d)))
883       Result.Factory.push_back(Method);
884   }
885 
886   return Result;
887 }
888 
889 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
890   return llvm::djbHash(a);
891 }
892 
893 std::pair<unsigned, unsigned>
894 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
895   using namespace llvm::support;
896 
897   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
898   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
899   return std::make_pair(KeyLen, DataLen);
900 }
901 
902 ASTIdentifierLookupTraitBase::internal_key_type
903 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
904   assert(n >= 2 && d[n-1] == '\0');
905   return StringRef((const char*) d, n-1);
906 }
907 
908 /// Whether the given identifier is "interesting".
909 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
910                                     bool IsModule) {
911   return II.hadMacroDefinition() ||
912          II.isPoisoned() ||
913          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
914          II.hasRevertedTokenIDToIdentifier() ||
915          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
916           II.getFETokenInfo());
917 }
918 
919 static bool readBit(unsigned &Bits) {
920   bool Value = Bits & 0x1;
921   Bits >>= 1;
922   return Value;
923 }
924 
925 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
926   using namespace llvm::support;
927 
928   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
929   return Reader.getGlobalIdentifierID(F, RawID >> 1);
930 }
931 
932 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
933   if (!II.isFromAST()) {
934     II.setIsFromAST();
935     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
936     if (isInterestingIdentifier(Reader, II, IsModule))
937       II.setChangedSinceDeserialization();
938   }
939 }
940 
941 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
942                                                    const unsigned char* d,
943                                                    unsigned DataLen) {
944   using namespace llvm::support;
945 
946   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
947   bool IsInteresting = RawID & 0x01;
948 
949   // Wipe out the "is interesting" bit.
950   RawID = RawID >> 1;
951 
952   // Build the IdentifierInfo and link the identifier ID with it.
953   IdentifierInfo *II = KnownII;
954   if (!II) {
955     II = &Reader.getIdentifierTable().getOwn(k);
956     KnownII = II;
957   }
958   markIdentifierFromAST(Reader, *II);
959   Reader.markIdentifierUpToDate(II);
960 
961   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
962   if (!IsInteresting) {
963     // For uninteresting identifiers, there's nothing else to do. Just notify
964     // the reader that we've finished loading this identifier.
965     Reader.SetIdentifierInfo(ID, II);
966     return II;
967   }
968 
969   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
970   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
971   bool CPlusPlusOperatorKeyword = readBit(Bits);
972   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
973   bool HasRevertedBuiltin = readBit(Bits);
974   bool Poisoned = readBit(Bits);
975   bool ExtensionToken = readBit(Bits);
976   bool HadMacroDefinition = readBit(Bits);
977 
978   assert(Bits == 0 && "Extra bits in the identifier?");
979   DataLen -= 8;
980 
981   // Set or check the various bits in the IdentifierInfo structure.
982   // Token IDs are read-only.
983   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
984     II->revertTokenIDToIdentifier();
985   if (!F.isModule())
986     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
987   else if (HasRevertedBuiltin && II->getBuiltinID()) {
988     II->revertBuiltin();
989     assert((II->hasRevertedBuiltin() ||
990             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
991            "Incorrect ObjC keyword or builtin ID");
992   }
993   assert(II->isExtensionToken() == ExtensionToken &&
994          "Incorrect extension token flag");
995   (void)ExtensionToken;
996   if (Poisoned)
997     II->setIsPoisoned(true);
998   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
999          "Incorrect C++ operator keyword flag");
1000   (void)CPlusPlusOperatorKeyword;
1001 
1002   // If this identifier is a macro, deserialize the macro
1003   // definition.
1004   if (HadMacroDefinition) {
1005     uint32_t MacroDirectivesOffset =
1006         endian::readNext<uint32_t, little, unaligned>(d);
1007     DataLen -= 4;
1008 
1009     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1010   }
1011 
1012   Reader.SetIdentifierInfo(ID, II);
1013 
1014   // Read all of the declarations visible at global scope with this
1015   // name.
1016   if (DataLen > 0) {
1017     SmallVector<uint32_t, 4> DeclIDs;
1018     for (; DataLen > 0; DataLen -= 4)
1019       DeclIDs.push_back(Reader.getGlobalDeclID(
1020           F, endian::readNext<uint32_t, little, unaligned>(d)));
1021     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1022   }
1023 
1024   return II;
1025 }
1026 
1027 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1028     : Kind(Name.getNameKind()) {
1029   switch (Kind) {
1030   case DeclarationName::Identifier:
1031     Data = (uint64_t)Name.getAsIdentifierInfo();
1032     break;
1033   case DeclarationName::ObjCZeroArgSelector:
1034   case DeclarationName::ObjCOneArgSelector:
1035   case DeclarationName::ObjCMultiArgSelector:
1036     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1037     break;
1038   case DeclarationName::CXXOperatorName:
1039     Data = Name.getCXXOverloadedOperator();
1040     break;
1041   case DeclarationName::CXXLiteralOperatorName:
1042     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1043     break;
1044   case DeclarationName::CXXDeductionGuideName:
1045     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1046                ->getDeclName().getAsIdentifierInfo();
1047     break;
1048   case DeclarationName::CXXConstructorName:
1049   case DeclarationName::CXXDestructorName:
1050   case DeclarationName::CXXConversionFunctionName:
1051   case DeclarationName::CXXUsingDirective:
1052     Data = 0;
1053     break;
1054   }
1055 }
1056 
1057 unsigned DeclarationNameKey::getHash() const {
1058   llvm::FoldingSetNodeID ID;
1059   ID.AddInteger(Kind);
1060 
1061   switch (Kind) {
1062   case DeclarationName::Identifier:
1063   case DeclarationName::CXXLiteralOperatorName:
1064   case DeclarationName::CXXDeductionGuideName:
1065     ID.AddString(((IdentifierInfo*)Data)->getName());
1066     break;
1067   case DeclarationName::ObjCZeroArgSelector:
1068   case DeclarationName::ObjCOneArgSelector:
1069   case DeclarationName::ObjCMultiArgSelector:
1070     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1071     break;
1072   case DeclarationName::CXXOperatorName:
1073     ID.AddInteger((OverloadedOperatorKind)Data);
1074     break;
1075   case DeclarationName::CXXConstructorName:
1076   case DeclarationName::CXXDestructorName:
1077   case DeclarationName::CXXConversionFunctionName:
1078   case DeclarationName::CXXUsingDirective:
1079     break;
1080   }
1081 
1082   return ID.ComputeHash();
1083 }
1084 
1085 ModuleFile *
1086 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1087   using namespace llvm::support;
1088 
1089   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1090   return Reader.getLocalModuleFile(F, ModuleFileID);
1091 }
1092 
1093 std::pair<unsigned, unsigned>
1094 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1095   using namespace llvm::support;
1096 
1097   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1098   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1099   return std::make_pair(KeyLen, DataLen);
1100 }
1101 
1102 ASTDeclContextNameLookupTrait::internal_key_type
1103 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1104   using namespace llvm::support;
1105 
1106   auto Kind = (DeclarationName::NameKind)*d++;
1107   uint64_t Data;
1108   switch (Kind) {
1109   case DeclarationName::Identifier:
1110   case DeclarationName::CXXLiteralOperatorName:
1111   case DeclarationName::CXXDeductionGuideName:
1112     Data = (uint64_t)Reader.getLocalIdentifier(
1113         F, endian::readNext<uint32_t, little, unaligned>(d));
1114     break;
1115   case DeclarationName::ObjCZeroArgSelector:
1116   case DeclarationName::ObjCOneArgSelector:
1117   case DeclarationName::ObjCMultiArgSelector:
1118     Data =
1119         (uint64_t)Reader.getLocalSelector(
1120                              F, endian::readNext<uint32_t, little, unaligned>(
1121                                     d)).getAsOpaquePtr();
1122     break;
1123   case DeclarationName::CXXOperatorName:
1124     Data = *d++; // OverloadedOperatorKind
1125     break;
1126   case DeclarationName::CXXConstructorName:
1127   case DeclarationName::CXXDestructorName:
1128   case DeclarationName::CXXConversionFunctionName:
1129   case DeclarationName::CXXUsingDirective:
1130     Data = 0;
1131     break;
1132   }
1133 
1134   return DeclarationNameKey(Kind, Data);
1135 }
1136 
1137 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1138                                                  const unsigned char *d,
1139                                                  unsigned DataLen,
1140                                                  data_type_builder &Val) {
1141   using namespace llvm::support;
1142 
1143   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1144     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1145     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1146   }
1147 }
1148 
1149 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1150                                               BitstreamCursor &Cursor,
1151                                               uint64_t Offset,
1152                                               DeclContext *DC) {
1153   assert(Offset != 0);
1154 
1155   SavedStreamPosition SavedPosition(Cursor);
1156   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1157     Error(std::move(Err));
1158     return true;
1159   }
1160 
1161   RecordData Record;
1162   StringRef Blob;
1163   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1164   if (!MaybeCode) {
1165     Error(MaybeCode.takeError());
1166     return true;
1167   }
1168   unsigned Code = MaybeCode.get();
1169 
1170   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1171   if (!MaybeRecCode) {
1172     Error(MaybeRecCode.takeError());
1173     return true;
1174   }
1175   unsigned RecCode = MaybeRecCode.get();
1176   if (RecCode != DECL_CONTEXT_LEXICAL) {
1177     Error("Expected lexical block");
1178     return true;
1179   }
1180 
1181   assert(!isa<TranslationUnitDecl>(DC) &&
1182          "expected a TU_UPDATE_LEXICAL record for TU");
1183   // If we are handling a C++ class template instantiation, we can see multiple
1184   // lexical updates for the same record. It's important that we select only one
1185   // of them, so that field numbering works properly. Just pick the first one we
1186   // see.
1187   auto &Lex = LexicalDecls[DC];
1188   if (!Lex.first) {
1189     Lex = std::make_pair(
1190         &M, llvm::makeArrayRef(
1191                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1192                     Blob.data()),
1193                 Blob.size() / 4));
1194   }
1195   DC->setHasExternalLexicalStorage(true);
1196   return false;
1197 }
1198 
1199 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1200                                               BitstreamCursor &Cursor,
1201                                               uint64_t Offset,
1202                                               DeclID ID) {
1203   assert(Offset != 0);
1204 
1205   SavedStreamPosition SavedPosition(Cursor);
1206   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1207     Error(std::move(Err));
1208     return true;
1209   }
1210 
1211   RecordData Record;
1212   StringRef Blob;
1213   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1214   if (!MaybeCode) {
1215     Error(MaybeCode.takeError());
1216     return true;
1217   }
1218   unsigned Code = MaybeCode.get();
1219 
1220   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1221   if (!MaybeRecCode) {
1222     Error(MaybeRecCode.takeError());
1223     return true;
1224   }
1225   unsigned RecCode = MaybeRecCode.get();
1226   if (RecCode != DECL_CONTEXT_VISIBLE) {
1227     Error("Expected visible lookup table block");
1228     return true;
1229   }
1230 
1231   // We can't safely determine the primary context yet, so delay attaching the
1232   // lookup table until we're done with recursive deserialization.
1233   auto *Data = (const unsigned char*)Blob.data();
1234   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1235   return false;
1236 }
1237 
1238 void ASTReader::Error(StringRef Msg) const {
1239   Error(diag::err_fe_pch_malformed, Msg);
1240   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1241       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1242     Diag(diag::note_module_cache_path)
1243       << PP.getHeaderSearchInfo().getModuleCachePath();
1244   }
1245 }
1246 
1247 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1248                       StringRef Arg3) const {
1249   if (Diags.isDiagnosticInFlight())
1250     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1251   else
1252     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1253 }
1254 
1255 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1256                       unsigned Select) const {
1257   if (!Diags.isDiagnosticInFlight())
1258     Diag(DiagID) << Arg1 << Arg2 << Select;
1259 }
1260 
1261 void ASTReader::Error(llvm::Error &&Err) const {
1262   Error(toString(std::move(Err)));
1263 }
1264 
1265 //===----------------------------------------------------------------------===//
1266 // Source Manager Deserialization
1267 //===----------------------------------------------------------------------===//
1268 
1269 /// Read the line table in the source manager block.
1270 /// \returns true if there was an error.
1271 bool ASTReader::ParseLineTable(ModuleFile &F,
1272                                const RecordData &Record) {
1273   unsigned Idx = 0;
1274   LineTableInfo &LineTable = SourceMgr.getLineTable();
1275 
1276   // Parse the file names
1277   std::map<int, int> FileIDs;
1278   FileIDs[-1] = -1; // For unspecified filenames.
1279   for (unsigned I = 0; Record[Idx]; ++I) {
1280     // Extract the file name
1281     auto Filename = ReadPath(F, Record, Idx);
1282     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1283   }
1284   ++Idx;
1285 
1286   // Parse the line entries
1287   std::vector<LineEntry> Entries;
1288   while (Idx < Record.size()) {
1289     int FID = Record[Idx++];
1290     assert(FID >= 0 && "Serialized line entries for non-local file.");
1291     // Remap FileID from 1-based old view.
1292     FID += F.SLocEntryBaseID - 1;
1293 
1294     // Extract the line entries
1295     unsigned NumEntries = Record[Idx++];
1296     assert(NumEntries && "no line entries for file ID");
1297     Entries.clear();
1298     Entries.reserve(NumEntries);
1299     for (unsigned I = 0; I != NumEntries; ++I) {
1300       unsigned FileOffset = Record[Idx++];
1301       unsigned LineNo = Record[Idx++];
1302       int FilenameID = FileIDs[Record[Idx++]];
1303       SrcMgr::CharacteristicKind FileKind
1304         = (SrcMgr::CharacteristicKind)Record[Idx++];
1305       unsigned IncludeOffset = Record[Idx++];
1306       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1307                                        FileKind, IncludeOffset));
1308     }
1309     LineTable.AddEntry(FileID::get(FID), Entries);
1310   }
1311 
1312   return false;
1313 }
1314 
1315 /// Read a source manager block
1316 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1317   using namespace SrcMgr;
1318 
1319   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1320 
1321   // Set the source-location entry cursor to the current position in
1322   // the stream. This cursor will be used to read the contents of the
1323   // source manager block initially, and then lazily read
1324   // source-location entries as needed.
1325   SLocEntryCursor = F.Stream;
1326 
1327   // The stream itself is going to skip over the source manager block.
1328   if (llvm::Error Err = F.Stream.SkipBlock()) {
1329     Error(std::move(Err));
1330     return true;
1331   }
1332 
1333   // Enter the source manager block.
1334   if (llvm::Error Err =
1335           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1336     Error(std::move(Err));
1337     return true;
1338   }
1339   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1340 
1341   RecordData Record;
1342   while (true) {
1343     Expected<llvm::BitstreamEntry> MaybeE =
1344         SLocEntryCursor.advanceSkippingSubblocks();
1345     if (!MaybeE) {
1346       Error(MaybeE.takeError());
1347       return true;
1348     }
1349     llvm::BitstreamEntry E = MaybeE.get();
1350 
1351     switch (E.Kind) {
1352     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1353     case llvm::BitstreamEntry::Error:
1354       Error("malformed block record in AST file");
1355       return true;
1356     case llvm::BitstreamEntry::EndBlock:
1357       return false;
1358     case llvm::BitstreamEntry::Record:
1359       // The interesting case.
1360       break;
1361     }
1362 
1363     // Read a record.
1364     Record.clear();
1365     StringRef Blob;
1366     Expected<unsigned> MaybeRecord =
1367         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1368     if (!MaybeRecord) {
1369       Error(MaybeRecord.takeError());
1370       return true;
1371     }
1372     switch (MaybeRecord.get()) {
1373     default:  // Default behavior: ignore.
1374       break;
1375 
1376     case SM_SLOC_FILE_ENTRY:
1377     case SM_SLOC_BUFFER_ENTRY:
1378     case SM_SLOC_EXPANSION_ENTRY:
1379       // Once we hit one of the source location entries, we're done.
1380       return false;
1381     }
1382   }
1383 }
1384 
1385 /// If a header file is not found at the path that we expect it to be
1386 /// and the PCH file was moved from its original location, try to resolve the
1387 /// file by assuming that header+PCH were moved together and the header is in
1388 /// the same place relative to the PCH.
1389 static std::string
1390 resolveFileRelativeToOriginalDir(const std::string &Filename,
1391                                  const std::string &OriginalDir,
1392                                  const std::string &CurrDir) {
1393   assert(OriginalDir != CurrDir &&
1394          "No point trying to resolve the file if the PCH dir didn't change");
1395 
1396   using namespace llvm::sys;
1397 
1398   SmallString<128> filePath(Filename);
1399   fs::make_absolute(filePath);
1400   assert(path::is_absolute(OriginalDir));
1401   SmallString<128> currPCHPath(CurrDir);
1402 
1403   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1404                        fileDirE = path::end(path::parent_path(filePath));
1405   path::const_iterator origDirI = path::begin(OriginalDir),
1406                        origDirE = path::end(OriginalDir);
1407   // Skip the common path components from filePath and OriginalDir.
1408   while (fileDirI != fileDirE && origDirI != origDirE &&
1409          *fileDirI == *origDirI) {
1410     ++fileDirI;
1411     ++origDirI;
1412   }
1413   for (; origDirI != origDirE; ++origDirI)
1414     path::append(currPCHPath, "..");
1415   path::append(currPCHPath, fileDirI, fileDirE);
1416   path::append(currPCHPath, path::filename(Filename));
1417   return std::string(currPCHPath.str());
1418 }
1419 
1420 bool ASTReader::ReadSLocEntry(int ID) {
1421   if (ID == 0)
1422     return false;
1423 
1424   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1425     Error("source location entry ID out-of-range for AST file");
1426     return true;
1427   }
1428 
1429   // Local helper to read the (possibly-compressed) buffer data following the
1430   // entry record.
1431   auto ReadBuffer = [this](
1432       BitstreamCursor &SLocEntryCursor,
1433       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1434     RecordData Record;
1435     StringRef Blob;
1436     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1437     if (!MaybeCode) {
1438       Error(MaybeCode.takeError());
1439       return nullptr;
1440     }
1441     unsigned Code = MaybeCode.get();
1442 
1443     Expected<unsigned> MaybeRecCode =
1444         SLocEntryCursor.readRecord(Code, Record, &Blob);
1445     if (!MaybeRecCode) {
1446       Error(MaybeRecCode.takeError());
1447       return nullptr;
1448     }
1449     unsigned RecCode = MaybeRecCode.get();
1450 
1451     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1452       if (!llvm::zlib::isAvailable()) {
1453         Error("zlib is not available");
1454         return nullptr;
1455       }
1456       SmallString<0> Uncompressed;
1457       if (llvm::Error E =
1458               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1459         Error("could not decompress embedded file contents: " +
1460               llvm::toString(std::move(E)));
1461         return nullptr;
1462       }
1463       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1464     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1465       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1466     } else {
1467       Error("AST record has invalid code");
1468       return nullptr;
1469     }
1470   };
1471 
1472   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1473   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1474           F->SLocEntryOffsetsBase +
1475           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1476     Error(std::move(Err));
1477     return true;
1478   }
1479 
1480   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1481   unsigned BaseOffset = F->SLocEntryBaseOffset;
1482 
1483   ++NumSLocEntriesRead;
1484   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1485   if (!MaybeEntry) {
1486     Error(MaybeEntry.takeError());
1487     return true;
1488   }
1489   llvm::BitstreamEntry Entry = MaybeEntry.get();
1490 
1491   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1492     Error("incorrectly-formatted source location entry in AST file");
1493     return true;
1494   }
1495 
1496   RecordData Record;
1497   StringRef Blob;
1498   Expected<unsigned> MaybeSLOC =
1499       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1500   if (!MaybeSLOC) {
1501     Error(MaybeSLOC.takeError());
1502     return true;
1503   }
1504   switch (MaybeSLOC.get()) {
1505   default:
1506     Error("incorrectly-formatted source location entry in AST file");
1507     return true;
1508 
1509   case SM_SLOC_FILE_ENTRY: {
1510     // We will detect whether a file changed and return 'Failure' for it, but
1511     // we will also try to fail gracefully by setting up the SLocEntry.
1512     unsigned InputID = Record[4];
1513     InputFile IF = getInputFile(*F, InputID);
1514     const FileEntry *File = IF.getFile();
1515     bool OverriddenBuffer = IF.isOverridden();
1516 
1517     // Note that we only check if a File was returned. If it was out-of-date
1518     // we have complained but we will continue creating a FileID to recover
1519     // gracefully.
1520     if (!File)
1521       return true;
1522 
1523     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1524     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1525       // This is the module's main file.
1526       IncludeLoc = getImportLocation(F);
1527     }
1528     SrcMgr::CharacteristicKind
1529       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1530     // FIXME: The FileID should be created from the FileEntryRef.
1531     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1532                                         ID, BaseOffset + Record[0]);
1533     SrcMgr::FileInfo &FileInfo =
1534           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1535     FileInfo.NumCreatedFIDs = Record[5];
1536     if (Record[3])
1537       FileInfo.setHasLineDirectives();
1538 
1539     unsigned NumFileDecls = Record[7];
1540     if (NumFileDecls && ContextObj) {
1541       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1542       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1543       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1544                                                              NumFileDecls));
1545     }
1546 
1547     const SrcMgr::ContentCache *ContentCache
1548       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1549     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1550         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1551         !ContentCache->getRawBuffer()) {
1552       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1553       if (!Buffer)
1554         return true;
1555       SourceMgr.overrideFileContents(File, std::move(Buffer));
1556     }
1557 
1558     break;
1559   }
1560 
1561   case SM_SLOC_BUFFER_ENTRY: {
1562     const char *Name = Blob.data();
1563     unsigned Offset = Record[0];
1564     SrcMgr::CharacteristicKind
1565       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1566     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1567     if (IncludeLoc.isInvalid() && F->isModule()) {
1568       IncludeLoc = getImportLocation(F);
1569     }
1570 
1571     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1572     if (!Buffer)
1573       return true;
1574     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1575                            BaseOffset + Offset, IncludeLoc);
1576     break;
1577   }
1578 
1579   case SM_SLOC_EXPANSION_ENTRY: {
1580     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1581     SourceMgr.createExpansionLoc(SpellingLoc,
1582                                      ReadSourceLocation(*F, Record[2]),
1583                                      ReadSourceLocation(*F, Record[3]),
1584                                      Record[5],
1585                                      Record[4],
1586                                      ID,
1587                                      BaseOffset + Record[0]);
1588     break;
1589   }
1590   }
1591 
1592   return false;
1593 }
1594 
1595 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1596   if (ID == 0)
1597     return std::make_pair(SourceLocation(), "");
1598 
1599   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1600     Error("source location entry ID out-of-range for AST file");
1601     return std::make_pair(SourceLocation(), "");
1602   }
1603 
1604   // Find which module file this entry lands in.
1605   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1606   if (!M->isModule())
1607     return std::make_pair(SourceLocation(), "");
1608 
1609   // FIXME: Can we map this down to a particular submodule? That would be
1610   // ideal.
1611   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1612 }
1613 
1614 /// Find the location where the module F is imported.
1615 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1616   if (F->ImportLoc.isValid())
1617     return F->ImportLoc;
1618 
1619   // Otherwise we have a PCH. It's considered to be "imported" at the first
1620   // location of its includer.
1621   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1622     // Main file is the importer.
1623     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1624     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1625   }
1626   return F->ImportedBy[0]->FirstLoc;
1627 }
1628 
1629 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1630 /// the abbreviations that are at the top of the block and then leave the cursor
1631 /// pointing into the block.
1632 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1633                                  uint64_t *StartOfBlockOffset) {
1634   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1635     // FIXME this drops errors on the floor.
1636     consumeError(std::move(Err));
1637     return true;
1638   }
1639 
1640   if (StartOfBlockOffset)
1641     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1642 
1643   while (true) {
1644     uint64_t Offset = Cursor.GetCurrentBitNo();
1645     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1646     if (!MaybeCode) {
1647       // FIXME this drops errors on the floor.
1648       consumeError(MaybeCode.takeError());
1649       return true;
1650     }
1651     unsigned Code = MaybeCode.get();
1652 
1653     // We expect all abbrevs to be at the start of the block.
1654     if (Code != llvm::bitc::DEFINE_ABBREV) {
1655       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1656         // FIXME this drops errors on the floor.
1657         consumeError(std::move(Err));
1658         return true;
1659       }
1660       return false;
1661     }
1662     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1663       // FIXME this drops errors on the floor.
1664       consumeError(std::move(Err));
1665       return true;
1666     }
1667   }
1668 }
1669 
1670 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1671                            unsigned &Idx) {
1672   Token Tok;
1673   Tok.startToken();
1674   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1675   Tok.setLength(Record[Idx++]);
1676   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1677     Tok.setIdentifierInfo(II);
1678   Tok.setKind((tok::TokenKind)Record[Idx++]);
1679   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1680   return Tok;
1681 }
1682 
1683 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1684   BitstreamCursor &Stream = F.MacroCursor;
1685 
1686   // Keep track of where we are in the stream, then jump back there
1687   // after reading this macro.
1688   SavedStreamPosition SavedPosition(Stream);
1689 
1690   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1691     // FIXME this drops errors on the floor.
1692     consumeError(std::move(Err));
1693     return nullptr;
1694   }
1695   RecordData Record;
1696   SmallVector<IdentifierInfo*, 16> MacroParams;
1697   MacroInfo *Macro = nullptr;
1698 
1699   while (true) {
1700     // Advance to the next record, but if we get to the end of the block, don't
1701     // pop it (removing all the abbreviations from the cursor) since we want to
1702     // be able to reseek within the block and read entries.
1703     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1704     Expected<llvm::BitstreamEntry> MaybeEntry =
1705         Stream.advanceSkippingSubblocks(Flags);
1706     if (!MaybeEntry) {
1707       Error(MaybeEntry.takeError());
1708       return Macro;
1709     }
1710     llvm::BitstreamEntry Entry = MaybeEntry.get();
1711 
1712     switch (Entry.Kind) {
1713     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1714     case llvm::BitstreamEntry::Error:
1715       Error("malformed block record in AST file");
1716       return Macro;
1717     case llvm::BitstreamEntry::EndBlock:
1718       return Macro;
1719     case llvm::BitstreamEntry::Record:
1720       // The interesting case.
1721       break;
1722     }
1723 
1724     // Read a record.
1725     Record.clear();
1726     PreprocessorRecordTypes RecType;
1727     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1728       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1729     else {
1730       Error(MaybeRecType.takeError());
1731       return Macro;
1732     }
1733     switch (RecType) {
1734     case PP_MODULE_MACRO:
1735     case PP_MACRO_DIRECTIVE_HISTORY:
1736       return Macro;
1737 
1738     case PP_MACRO_OBJECT_LIKE:
1739     case PP_MACRO_FUNCTION_LIKE: {
1740       // If we already have a macro, that means that we've hit the end
1741       // of the definition of the macro we were looking for. We're
1742       // done.
1743       if (Macro)
1744         return Macro;
1745 
1746       unsigned NextIndex = 1; // Skip identifier ID.
1747       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1748       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1749       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1750       MI->setIsUsed(Record[NextIndex++]);
1751       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1752 
1753       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1754         // Decode function-like macro info.
1755         bool isC99VarArgs = Record[NextIndex++];
1756         bool isGNUVarArgs = Record[NextIndex++];
1757         bool hasCommaPasting = Record[NextIndex++];
1758         MacroParams.clear();
1759         unsigned NumArgs = Record[NextIndex++];
1760         for (unsigned i = 0; i != NumArgs; ++i)
1761           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1762 
1763         // Install function-like macro info.
1764         MI->setIsFunctionLike();
1765         if (isC99VarArgs) MI->setIsC99Varargs();
1766         if (isGNUVarArgs) MI->setIsGNUVarargs();
1767         if (hasCommaPasting) MI->setHasCommaPasting();
1768         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1769       }
1770 
1771       // Remember that we saw this macro last so that we add the tokens that
1772       // form its body to it.
1773       Macro = MI;
1774 
1775       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1776           Record[NextIndex]) {
1777         // We have a macro definition. Register the association
1778         PreprocessedEntityID
1779             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1780         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1781         PreprocessingRecord::PPEntityID PPID =
1782             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1783         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1784             PPRec.getPreprocessedEntity(PPID));
1785         if (PPDef)
1786           PPRec.RegisterMacroDefinition(Macro, PPDef);
1787       }
1788 
1789       ++NumMacrosRead;
1790       break;
1791     }
1792 
1793     case PP_TOKEN: {
1794       // If we see a TOKEN before a PP_MACRO_*, then the file is
1795       // erroneous, just pretend we didn't see this.
1796       if (!Macro) break;
1797 
1798       unsigned Idx = 0;
1799       Token Tok = ReadToken(F, Record, Idx);
1800       Macro->AddTokenToBody(Tok);
1801       break;
1802     }
1803     }
1804   }
1805 }
1806 
1807 PreprocessedEntityID
1808 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1809                                          unsigned LocalID) const {
1810   if (!M.ModuleOffsetMap.empty())
1811     ReadModuleOffsetMap(M);
1812 
1813   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1814     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1815   assert(I != M.PreprocessedEntityRemap.end()
1816          && "Invalid index into preprocessed entity index remap");
1817 
1818   return LocalID + I->second;
1819 }
1820 
1821 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1822   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1823 }
1824 
1825 HeaderFileInfoTrait::internal_key_type
1826 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1827   internal_key_type ikey = {FE->getSize(),
1828                             M.HasTimestamps ? FE->getModificationTime() : 0,
1829                             FE->getName(), /*Imported*/ false};
1830   return ikey;
1831 }
1832 
1833 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1834   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1835     return false;
1836 
1837   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1838     return true;
1839 
1840   // Determine whether the actual files are equivalent.
1841   FileManager &FileMgr = Reader.getFileManager();
1842   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1843     if (!Key.Imported) {
1844       if (auto File = FileMgr.getFile(Key.Filename))
1845         return *File;
1846       return nullptr;
1847     }
1848 
1849     std::string Resolved = std::string(Key.Filename);
1850     Reader.ResolveImportedPath(M, Resolved);
1851     if (auto File = FileMgr.getFile(Resolved))
1852       return *File;
1853     return nullptr;
1854   };
1855 
1856   const FileEntry *FEA = GetFile(a);
1857   const FileEntry *FEB = GetFile(b);
1858   return FEA && FEA == FEB;
1859 }
1860 
1861 std::pair<unsigned, unsigned>
1862 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1863   using namespace llvm::support;
1864 
1865   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1866   unsigned DataLen = (unsigned) *d++;
1867   return std::make_pair(KeyLen, DataLen);
1868 }
1869 
1870 HeaderFileInfoTrait::internal_key_type
1871 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1872   using namespace llvm::support;
1873 
1874   internal_key_type ikey;
1875   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1876   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1877   ikey.Filename = (const char *)d;
1878   ikey.Imported = true;
1879   return ikey;
1880 }
1881 
1882 HeaderFileInfoTrait::data_type
1883 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1884                               unsigned DataLen) {
1885   using namespace llvm::support;
1886 
1887   const unsigned char *End = d + DataLen;
1888   HeaderFileInfo HFI;
1889   unsigned Flags = *d++;
1890   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1891   HFI.isImport |= (Flags >> 5) & 0x01;
1892   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1893   HFI.DirInfo = (Flags >> 1) & 0x07;
1894   HFI.IndexHeaderMapHeader = Flags & 0x01;
1895   // FIXME: Find a better way to handle this. Maybe just store a
1896   // "has been included" flag?
1897   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1898                              HFI.NumIncludes);
1899   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1900       M, endian::readNext<uint32_t, little, unaligned>(d));
1901   if (unsigned FrameworkOffset =
1902           endian::readNext<uint32_t, little, unaligned>(d)) {
1903     // The framework offset is 1 greater than the actual offset,
1904     // since 0 is used as an indicator for "no framework name".
1905     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1906     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1907   }
1908 
1909   assert((End - d) % 4 == 0 &&
1910          "Wrong data length in HeaderFileInfo deserialization");
1911   while (d != End) {
1912     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1913     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1914     LocalSMID >>= 2;
1915 
1916     // This header is part of a module. Associate it with the module to enable
1917     // implicit module import.
1918     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1919     Module *Mod = Reader.getSubmodule(GlobalSMID);
1920     FileManager &FileMgr = Reader.getFileManager();
1921     ModuleMap &ModMap =
1922         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1923 
1924     std::string Filename = std::string(key.Filename);
1925     if (key.Imported)
1926       Reader.ResolveImportedPath(M, Filename);
1927     // FIXME: This is not always the right filename-as-written, but we're not
1928     // going to use this information to rebuild the module, so it doesn't make
1929     // a lot of difference.
1930     Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
1931     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1932     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1933   }
1934 
1935   // This HeaderFileInfo was externally loaded.
1936   HFI.External = true;
1937   HFI.IsValid = true;
1938   return HFI;
1939 }
1940 
1941 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1942                                 uint32_t MacroDirectivesOffset) {
1943   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1944   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1945 }
1946 
1947 void ASTReader::ReadDefinedMacros() {
1948   // Note that we are loading defined macros.
1949   Deserializing Macros(this);
1950 
1951   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1952     BitstreamCursor &MacroCursor = I.MacroCursor;
1953 
1954     // If there was no preprocessor block, skip this file.
1955     if (MacroCursor.getBitcodeBytes().empty())
1956       continue;
1957 
1958     BitstreamCursor Cursor = MacroCursor;
1959     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1960       Error(std::move(Err));
1961       return;
1962     }
1963 
1964     RecordData Record;
1965     while (true) {
1966       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1967       if (!MaybeE) {
1968         Error(MaybeE.takeError());
1969         return;
1970       }
1971       llvm::BitstreamEntry E = MaybeE.get();
1972 
1973       switch (E.Kind) {
1974       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1975       case llvm::BitstreamEntry::Error:
1976         Error("malformed block record in AST file");
1977         return;
1978       case llvm::BitstreamEntry::EndBlock:
1979         goto NextCursor;
1980 
1981       case llvm::BitstreamEntry::Record: {
1982         Record.clear();
1983         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1984         if (!MaybeRecord) {
1985           Error(MaybeRecord.takeError());
1986           return;
1987         }
1988         switch (MaybeRecord.get()) {
1989         default:  // Default behavior: ignore.
1990           break;
1991 
1992         case PP_MACRO_OBJECT_LIKE:
1993         case PP_MACRO_FUNCTION_LIKE: {
1994           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1995           if (II->isOutOfDate())
1996             updateOutOfDateIdentifier(*II);
1997           break;
1998         }
1999 
2000         case PP_TOKEN:
2001           // Ignore tokens.
2002           break;
2003         }
2004         break;
2005       }
2006       }
2007     }
2008     NextCursor:  ;
2009   }
2010 }
2011 
2012 namespace {
2013 
2014   /// Visitor class used to look up identifirs in an AST file.
2015   class IdentifierLookupVisitor {
2016     StringRef Name;
2017     unsigned NameHash;
2018     unsigned PriorGeneration;
2019     unsigned &NumIdentifierLookups;
2020     unsigned &NumIdentifierLookupHits;
2021     IdentifierInfo *Found = nullptr;
2022 
2023   public:
2024     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2025                             unsigned &NumIdentifierLookups,
2026                             unsigned &NumIdentifierLookupHits)
2027       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2028         PriorGeneration(PriorGeneration),
2029         NumIdentifierLookups(NumIdentifierLookups),
2030         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2031 
2032     bool operator()(ModuleFile &M) {
2033       // If we've already searched this module file, skip it now.
2034       if (M.Generation <= PriorGeneration)
2035         return true;
2036 
2037       ASTIdentifierLookupTable *IdTable
2038         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2039       if (!IdTable)
2040         return false;
2041 
2042       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2043                                      Found);
2044       ++NumIdentifierLookups;
2045       ASTIdentifierLookupTable::iterator Pos =
2046           IdTable->find_hashed(Name, NameHash, &Trait);
2047       if (Pos == IdTable->end())
2048         return false;
2049 
2050       // Dereferencing the iterator has the effect of building the
2051       // IdentifierInfo node and populating it with the various
2052       // declarations it needs.
2053       ++NumIdentifierLookupHits;
2054       Found = *Pos;
2055       return true;
2056     }
2057 
2058     // Retrieve the identifier info found within the module
2059     // files.
2060     IdentifierInfo *getIdentifierInfo() const { return Found; }
2061   };
2062 
2063 } // namespace
2064 
2065 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2066   // Note that we are loading an identifier.
2067   Deserializing AnIdentifier(this);
2068 
2069   unsigned PriorGeneration = 0;
2070   if (getContext().getLangOpts().Modules)
2071     PriorGeneration = IdentifierGeneration[&II];
2072 
2073   // If there is a global index, look there first to determine which modules
2074   // provably do not have any results for this identifier.
2075   GlobalModuleIndex::HitSet Hits;
2076   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2077   if (!loadGlobalIndex()) {
2078     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2079       HitsPtr = &Hits;
2080     }
2081   }
2082 
2083   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2084                                   NumIdentifierLookups,
2085                                   NumIdentifierLookupHits);
2086   ModuleMgr.visit(Visitor, HitsPtr);
2087   markIdentifierUpToDate(&II);
2088 }
2089 
2090 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2091   if (!II)
2092     return;
2093 
2094   II->setOutOfDate(false);
2095 
2096   // Update the generation for this identifier.
2097   if (getContext().getLangOpts().Modules)
2098     IdentifierGeneration[II] = getGeneration();
2099 }
2100 
2101 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2102                                     const PendingMacroInfo &PMInfo) {
2103   ModuleFile &M = *PMInfo.M;
2104 
2105   BitstreamCursor &Cursor = M.MacroCursor;
2106   SavedStreamPosition SavedPosition(Cursor);
2107   if (llvm::Error Err =
2108           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2109     Error(std::move(Err));
2110     return;
2111   }
2112 
2113   struct ModuleMacroRecord {
2114     SubmoduleID SubModID;
2115     MacroInfo *MI;
2116     SmallVector<SubmoduleID, 8> Overrides;
2117   };
2118   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2119 
2120   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2121   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2122   // macro histroy.
2123   RecordData Record;
2124   while (true) {
2125     Expected<llvm::BitstreamEntry> MaybeEntry =
2126         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2127     if (!MaybeEntry) {
2128       Error(MaybeEntry.takeError());
2129       return;
2130     }
2131     llvm::BitstreamEntry Entry = MaybeEntry.get();
2132 
2133     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2134       Error("malformed block record in AST file");
2135       return;
2136     }
2137 
2138     Record.clear();
2139     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2140     if (!MaybePP) {
2141       Error(MaybePP.takeError());
2142       return;
2143     }
2144     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2145     case PP_MACRO_DIRECTIVE_HISTORY:
2146       break;
2147 
2148     case PP_MODULE_MACRO: {
2149       ModuleMacros.push_back(ModuleMacroRecord());
2150       auto &Info = ModuleMacros.back();
2151       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2152       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2153       for (int I = 2, N = Record.size(); I != N; ++I)
2154         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2155       continue;
2156     }
2157 
2158     default:
2159       Error("malformed block record in AST file");
2160       return;
2161     }
2162 
2163     // We found the macro directive history; that's the last record
2164     // for this macro.
2165     break;
2166   }
2167 
2168   // Module macros are listed in reverse dependency order.
2169   {
2170     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2171     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2172     for (auto &MMR : ModuleMacros) {
2173       Overrides.clear();
2174       for (unsigned ModID : MMR.Overrides) {
2175         Module *Mod = getSubmodule(ModID);
2176         auto *Macro = PP.getModuleMacro(Mod, II);
2177         assert(Macro && "missing definition for overridden macro");
2178         Overrides.push_back(Macro);
2179       }
2180 
2181       bool Inserted = false;
2182       Module *Owner = getSubmodule(MMR.SubModID);
2183       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2184     }
2185   }
2186 
2187   // Don't read the directive history for a module; we don't have anywhere
2188   // to put it.
2189   if (M.isModule())
2190     return;
2191 
2192   // Deserialize the macro directives history in reverse source-order.
2193   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2194   unsigned Idx = 0, N = Record.size();
2195   while (Idx < N) {
2196     MacroDirective *MD = nullptr;
2197     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2198     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2199     switch (K) {
2200     case MacroDirective::MD_Define: {
2201       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2202       MD = PP.AllocateDefMacroDirective(MI, Loc);
2203       break;
2204     }
2205     case MacroDirective::MD_Undefine:
2206       MD = PP.AllocateUndefMacroDirective(Loc);
2207       break;
2208     case MacroDirective::MD_Visibility:
2209       bool isPublic = Record[Idx++];
2210       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2211       break;
2212     }
2213 
2214     if (!Latest)
2215       Latest = MD;
2216     if (Earliest)
2217       Earliest->setPrevious(MD);
2218     Earliest = MD;
2219   }
2220 
2221   if (Latest)
2222     PP.setLoadedMacroDirective(II, Earliest, Latest);
2223 }
2224 
2225 ASTReader::InputFileInfo
2226 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2227   // Go find this input file.
2228   BitstreamCursor &Cursor = F.InputFilesCursor;
2229   SavedStreamPosition SavedPosition(Cursor);
2230   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2231     // FIXME this drops errors on the floor.
2232     consumeError(std::move(Err));
2233   }
2234 
2235   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2236   if (!MaybeCode) {
2237     // FIXME this drops errors on the floor.
2238     consumeError(MaybeCode.takeError());
2239   }
2240   unsigned Code = MaybeCode.get();
2241   RecordData Record;
2242   StringRef Blob;
2243 
2244   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2245     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2246            "invalid record type for input file");
2247   else {
2248     // FIXME this drops errors on the floor.
2249     consumeError(Maybe.takeError());
2250   }
2251 
2252   assert(Record[0] == ID && "Bogus stored ID or offset");
2253   InputFileInfo R;
2254   R.StoredSize = static_cast<off_t>(Record[1]);
2255   R.StoredTime = static_cast<time_t>(Record[2]);
2256   R.Overridden = static_cast<bool>(Record[3]);
2257   R.Transient = static_cast<bool>(Record[4]);
2258   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2259   R.Filename = std::string(Blob);
2260   ResolveImportedPath(F, R.Filename);
2261 
2262   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2263   if (!MaybeEntry) // FIXME this drops errors on the floor.
2264     consumeError(MaybeEntry.takeError());
2265   llvm::BitstreamEntry Entry = MaybeEntry.get();
2266   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2267          "expected record type for input file hash");
2268 
2269   Record.clear();
2270   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2271     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2272            "invalid record type for input file hash");
2273   else {
2274     // FIXME this drops errors on the floor.
2275     consumeError(Maybe.takeError());
2276   }
2277   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2278                   static_cast<uint64_t>(Record[0]);
2279   return R;
2280 }
2281 
2282 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2283 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2284   // If this ID is bogus, just return an empty input file.
2285   if (ID == 0 || ID > F.InputFilesLoaded.size())
2286     return InputFile();
2287 
2288   // If we've already loaded this input file, return it.
2289   if (F.InputFilesLoaded[ID-1].getFile())
2290     return F.InputFilesLoaded[ID-1];
2291 
2292   if (F.InputFilesLoaded[ID-1].isNotFound())
2293     return InputFile();
2294 
2295   // Go find this input file.
2296   BitstreamCursor &Cursor = F.InputFilesCursor;
2297   SavedStreamPosition SavedPosition(Cursor);
2298   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2299     // FIXME this drops errors on the floor.
2300     consumeError(std::move(Err));
2301   }
2302 
2303   InputFileInfo FI = readInputFileInfo(F, ID);
2304   off_t StoredSize = FI.StoredSize;
2305   time_t StoredTime = FI.StoredTime;
2306   bool Overridden = FI.Overridden;
2307   bool Transient = FI.Transient;
2308   StringRef Filename = FI.Filename;
2309   uint64_t StoredContentHash = FI.ContentHash;
2310 
2311   const FileEntry *File = nullptr;
2312   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2313     File = *FE;
2314 
2315   // If we didn't find the file, resolve it relative to the
2316   // original directory from which this AST file was created.
2317   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2318       F.OriginalDir != F.BaseDirectory) {
2319     std::string Resolved = resolveFileRelativeToOriginalDir(
2320         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2321     if (!Resolved.empty())
2322       if (auto FE = FileMgr.getFile(Resolved))
2323         File = *FE;
2324   }
2325 
2326   // For an overridden file, create a virtual file with the stored
2327   // size/timestamp.
2328   if ((Overridden || Transient) && File == nullptr)
2329     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2330 
2331   if (File == nullptr) {
2332     if (Complain) {
2333       std::string ErrorStr = "could not find file '";
2334       ErrorStr += Filename;
2335       ErrorStr += "' referenced by AST file '";
2336       ErrorStr += F.FileName;
2337       ErrorStr += "'";
2338       Error(ErrorStr);
2339     }
2340     // Record that we didn't find the file.
2341     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2342     return InputFile();
2343   }
2344 
2345   // Check if there was a request to override the contents of the file
2346   // that was part of the precompiled header. Overriding such a file
2347   // can lead to problems when lexing using the source locations from the
2348   // PCH.
2349   SourceManager &SM = getSourceManager();
2350   // FIXME: Reject if the overrides are different.
2351   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2352     if (Complain)
2353       Error(diag::err_fe_pch_file_overridden, Filename);
2354 
2355     // After emitting the diagnostic, bypass the overriding file to recover
2356     // (this creates a separate FileEntry).
2357     File = SM.bypassFileContentsOverride(*File);
2358     if (!File) {
2359       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2360       return InputFile();
2361     }
2362   }
2363 
2364   enum ModificationType {
2365     Size,
2366     ModTime,
2367     Content,
2368     None,
2369   };
2370   auto HasInputFileChanged = [&]() {
2371     if (StoredSize != File->getSize())
2372       return ModificationType::Size;
2373     if (!DisableValidation && StoredTime &&
2374         StoredTime != File->getModificationTime()) {
2375       // In case the modification time changes but not the content,
2376       // accept the cached file as legit.
2377       if (ValidateASTInputFilesContent &&
2378           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2379         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2380         if (!MemBuffOrError) {
2381           if (!Complain)
2382             return ModificationType::ModTime;
2383           std::string ErrorStr = "could not get buffer for file '";
2384           ErrorStr += File->getName();
2385           ErrorStr += "'";
2386           Error(ErrorStr);
2387           return ModificationType::ModTime;
2388         }
2389 
2390         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2391         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2392           return ModificationType::None;
2393         return ModificationType::Content;
2394       }
2395       return ModificationType::ModTime;
2396     }
2397     return ModificationType::None;
2398   };
2399 
2400   bool IsOutOfDate = false;
2401   auto FileChange = HasInputFileChanged();
2402   // For an overridden file, there is nothing to validate.
2403   if (!Overridden && FileChange != ModificationType::None) {
2404     if (Complain) {
2405       // Build a list of the PCH imports that got us here (in reverse).
2406       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2407       while (!ImportStack.back()->ImportedBy.empty())
2408         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2409 
2410       // The top-level PCH is stale.
2411       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2412       unsigned DiagnosticKind =
2413           moduleKindForDiagnostic(ImportStack.back()->Kind);
2414       if (DiagnosticKind == 0)
2415         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2416               (unsigned)FileChange);
2417       else if (DiagnosticKind == 1)
2418         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2419               (unsigned)FileChange);
2420       else
2421         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2422               (unsigned)FileChange);
2423 
2424       // Print the import stack.
2425       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2426         Diag(diag::note_pch_required_by)
2427           << Filename << ImportStack[0]->FileName;
2428         for (unsigned I = 1; I < ImportStack.size(); ++I)
2429           Diag(diag::note_pch_required_by)
2430             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2431       }
2432 
2433       if (!Diags.isDiagnosticInFlight())
2434         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2435     }
2436 
2437     IsOutOfDate = true;
2438   }
2439   // FIXME: If the file is overridden and we've already opened it,
2440   // issue an error (or split it into a separate FileEntry).
2441 
2442   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2443 
2444   // Note that we've loaded this input file.
2445   F.InputFilesLoaded[ID-1] = IF;
2446   return IF;
2447 }
2448 
2449 /// If we are loading a relocatable PCH or module file, and the filename
2450 /// is not an absolute path, add the system or module root to the beginning of
2451 /// the file name.
2452 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2453   // Resolve relative to the base directory, if we have one.
2454   if (!M.BaseDirectory.empty())
2455     return ResolveImportedPath(Filename, M.BaseDirectory);
2456 }
2457 
2458 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2459   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2460     return;
2461 
2462   SmallString<128> Buffer;
2463   llvm::sys::path::append(Buffer, Prefix, Filename);
2464   Filename.assign(Buffer.begin(), Buffer.end());
2465 }
2466 
2467 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2468   switch (ARR) {
2469   case ASTReader::Failure: return true;
2470   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2471   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2472   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2473   case ASTReader::ConfigurationMismatch:
2474     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2475   case ASTReader::HadErrors: return true;
2476   case ASTReader::Success: return false;
2477   }
2478 
2479   llvm_unreachable("unknown ASTReadResult");
2480 }
2481 
2482 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2483     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2484     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2485     std::string &SuggestedPredefines) {
2486   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2487     // FIXME this drops errors on the floor.
2488     consumeError(std::move(Err));
2489     return Failure;
2490   }
2491 
2492   // Read all of the records in the options block.
2493   RecordData Record;
2494   ASTReadResult Result = Success;
2495   while (true) {
2496     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2497     if (!MaybeEntry) {
2498       // FIXME this drops errors on the floor.
2499       consumeError(MaybeEntry.takeError());
2500       return Failure;
2501     }
2502     llvm::BitstreamEntry Entry = MaybeEntry.get();
2503 
2504     switch (Entry.Kind) {
2505     case llvm::BitstreamEntry::Error:
2506     case llvm::BitstreamEntry::SubBlock:
2507       return Failure;
2508 
2509     case llvm::BitstreamEntry::EndBlock:
2510       return Result;
2511 
2512     case llvm::BitstreamEntry::Record:
2513       // The interesting case.
2514       break;
2515     }
2516 
2517     // Read and process a record.
2518     Record.clear();
2519     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2520     if (!MaybeRecordType) {
2521       // FIXME this drops errors on the floor.
2522       consumeError(MaybeRecordType.takeError());
2523       return Failure;
2524     }
2525     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2526     case LANGUAGE_OPTIONS: {
2527       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2528       if (ParseLanguageOptions(Record, Complain, Listener,
2529                                AllowCompatibleConfigurationMismatch))
2530         Result = ConfigurationMismatch;
2531       break;
2532     }
2533 
2534     case TARGET_OPTIONS: {
2535       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2536       if (ParseTargetOptions(Record, Complain, Listener,
2537                              AllowCompatibleConfigurationMismatch))
2538         Result = ConfigurationMismatch;
2539       break;
2540     }
2541 
2542     case FILE_SYSTEM_OPTIONS: {
2543       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2544       if (!AllowCompatibleConfigurationMismatch &&
2545           ParseFileSystemOptions(Record, Complain, Listener))
2546         Result = ConfigurationMismatch;
2547       break;
2548     }
2549 
2550     case HEADER_SEARCH_OPTIONS: {
2551       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2552       if (!AllowCompatibleConfigurationMismatch &&
2553           ParseHeaderSearchOptions(Record, Complain, Listener))
2554         Result = ConfigurationMismatch;
2555       break;
2556     }
2557 
2558     case PREPROCESSOR_OPTIONS:
2559       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2560       if (!AllowCompatibleConfigurationMismatch &&
2561           ParsePreprocessorOptions(Record, Complain, Listener,
2562                                    SuggestedPredefines))
2563         Result = ConfigurationMismatch;
2564       break;
2565     }
2566   }
2567 }
2568 
2569 ASTReader::ASTReadResult
2570 ASTReader::ReadControlBlock(ModuleFile &F,
2571                             SmallVectorImpl<ImportedModule> &Loaded,
2572                             const ModuleFile *ImportedBy,
2573                             unsigned ClientLoadCapabilities) {
2574   BitstreamCursor &Stream = F.Stream;
2575 
2576   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2577     Error(std::move(Err));
2578     return Failure;
2579   }
2580 
2581   // Lambda to read the unhashed control block the first time it's called.
2582   //
2583   // For PCM files, the unhashed control block cannot be read until after the
2584   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2585   // need to look ahead before reading the IMPORTS record.  For consistency,
2586   // this block is always read somehow (see BitstreamEntry::EndBlock).
2587   bool HasReadUnhashedControlBlock = false;
2588   auto readUnhashedControlBlockOnce = [&]() {
2589     if (!HasReadUnhashedControlBlock) {
2590       HasReadUnhashedControlBlock = true;
2591       if (ASTReadResult Result =
2592               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2593         return Result;
2594     }
2595     return Success;
2596   };
2597 
2598   // Read all of the records and blocks in the control block.
2599   RecordData Record;
2600   unsigned NumInputs = 0;
2601   unsigned NumUserInputs = 0;
2602   StringRef BaseDirectoryAsWritten;
2603   while (true) {
2604     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2605     if (!MaybeEntry) {
2606       Error(MaybeEntry.takeError());
2607       return Failure;
2608     }
2609     llvm::BitstreamEntry Entry = MaybeEntry.get();
2610 
2611     switch (Entry.Kind) {
2612     case llvm::BitstreamEntry::Error:
2613       Error("malformed block record in AST file");
2614       return Failure;
2615     case llvm::BitstreamEntry::EndBlock: {
2616       // Validate the module before returning.  This call catches an AST with
2617       // no module name and no imports.
2618       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2619         return Result;
2620 
2621       // Validate input files.
2622       const HeaderSearchOptions &HSOpts =
2623           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2624 
2625       // All user input files reside at the index range [0, NumUserInputs), and
2626       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2627       // loaded module files, ignore missing inputs.
2628       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2629           F.Kind != MK_PrebuiltModule) {
2630         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2631 
2632         // If we are reading a module, we will create a verification timestamp,
2633         // so we verify all input files.  Otherwise, verify only user input
2634         // files.
2635 
2636         unsigned N = NumUserInputs;
2637         if (ValidateSystemInputs ||
2638             (HSOpts.ModulesValidateOncePerBuildSession &&
2639              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2640              F.Kind == MK_ImplicitModule))
2641           N = NumInputs;
2642 
2643         for (unsigned I = 0; I < N; ++I) {
2644           InputFile IF = getInputFile(F, I+1, Complain);
2645           if (!IF.getFile() || IF.isOutOfDate())
2646             return OutOfDate;
2647         }
2648       }
2649 
2650       if (Listener)
2651         Listener->visitModuleFile(F.FileName, F.Kind);
2652 
2653       if (Listener && Listener->needsInputFileVisitation()) {
2654         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2655                                                                 : NumUserInputs;
2656         for (unsigned I = 0; I < N; ++I) {
2657           bool IsSystem = I >= NumUserInputs;
2658           InputFileInfo FI = readInputFileInfo(F, I+1);
2659           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2660                                    F.Kind == MK_ExplicitModule ||
2661                                    F.Kind == MK_PrebuiltModule);
2662         }
2663       }
2664 
2665       return Success;
2666     }
2667 
2668     case llvm::BitstreamEntry::SubBlock:
2669       switch (Entry.ID) {
2670       case INPUT_FILES_BLOCK_ID:
2671         F.InputFilesCursor = Stream;
2672         if (llvm::Error Err = Stream.SkipBlock()) {
2673           Error(std::move(Err));
2674           return Failure;
2675         }
2676         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2677           Error("malformed block record in AST file");
2678           return Failure;
2679         }
2680         continue;
2681 
2682       case OPTIONS_BLOCK_ID:
2683         // If we're reading the first module for this group, check its options
2684         // are compatible with ours. For modules it imports, no further checking
2685         // is required, because we checked them when we built it.
2686         if (Listener && !ImportedBy) {
2687           // Should we allow the configuration of the module file to differ from
2688           // the configuration of the current translation unit in a compatible
2689           // way?
2690           //
2691           // FIXME: Allow this for files explicitly specified with -include-pch.
2692           bool AllowCompatibleConfigurationMismatch =
2693               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2694 
2695           ASTReadResult Result =
2696               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2697                                AllowCompatibleConfigurationMismatch, *Listener,
2698                                SuggestedPredefines);
2699           if (Result == Failure) {
2700             Error("malformed block record in AST file");
2701             return Result;
2702           }
2703 
2704           if (DisableValidation ||
2705               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2706             Result = Success;
2707 
2708           // If we can't load the module, exit early since we likely
2709           // will rebuild the module anyway. The stream may be in the
2710           // middle of a block.
2711           if (Result != Success)
2712             return Result;
2713         } else if (llvm::Error Err = Stream.SkipBlock()) {
2714           Error(std::move(Err));
2715           return Failure;
2716         }
2717         continue;
2718 
2719       default:
2720         if (llvm::Error Err = Stream.SkipBlock()) {
2721           Error(std::move(Err));
2722           return Failure;
2723         }
2724         continue;
2725       }
2726 
2727     case llvm::BitstreamEntry::Record:
2728       // The interesting case.
2729       break;
2730     }
2731 
2732     // Read and process a record.
2733     Record.clear();
2734     StringRef Blob;
2735     Expected<unsigned> MaybeRecordType =
2736         Stream.readRecord(Entry.ID, Record, &Blob);
2737     if (!MaybeRecordType) {
2738       Error(MaybeRecordType.takeError());
2739       return Failure;
2740     }
2741     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2742     case METADATA: {
2743       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2744         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2745           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2746                                         : diag::err_pch_version_too_new);
2747         return VersionMismatch;
2748       }
2749 
2750       bool hasErrors = Record[6];
2751       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2752         Diag(diag::err_pch_with_compiler_errors);
2753         return HadErrors;
2754       }
2755       if (hasErrors) {
2756         Diags.ErrorOccurred = true;
2757         Diags.UncompilableErrorOccurred = true;
2758         Diags.UnrecoverableErrorOccurred = true;
2759       }
2760 
2761       F.RelocatablePCH = Record[4];
2762       // Relative paths in a relocatable PCH are relative to our sysroot.
2763       if (F.RelocatablePCH)
2764         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2765 
2766       F.HasTimestamps = Record[5];
2767 
2768       const std::string &CurBranch = getClangFullRepositoryVersion();
2769       StringRef ASTBranch = Blob;
2770       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2771         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2772           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2773         return VersionMismatch;
2774       }
2775       break;
2776     }
2777 
2778     case IMPORTS: {
2779       // Validate the AST before processing any imports (otherwise, untangling
2780       // them can be error-prone and expensive).  A module will have a name and
2781       // will already have been validated, but this catches the PCH case.
2782       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2783         return Result;
2784 
2785       // Load each of the imported PCH files.
2786       unsigned Idx = 0, N = Record.size();
2787       while (Idx < N) {
2788         // Read information about the AST file.
2789         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2790         // The import location will be the local one for now; we will adjust
2791         // all import locations of module imports after the global source
2792         // location info are setup, in ReadAST.
2793         SourceLocation ImportLoc =
2794             ReadUntranslatedSourceLocation(Record[Idx++]);
2795         off_t StoredSize = (off_t)Record[Idx++];
2796         time_t StoredModTime = (time_t)Record[Idx++];
2797         auto FirstSignatureByte = Record.begin() + Idx;
2798         ASTFileSignature StoredSignature = ASTFileSignature::create(
2799             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2800         Idx += ASTFileSignature::size;
2801 
2802         std::string ImportedName = ReadString(Record, Idx);
2803         std::string ImportedFile;
2804 
2805         // For prebuilt and explicit modules first consult the file map for
2806         // an override. Note that here we don't search prebuilt module
2807         // directories, only the explicit name to file mappings. Also, we will
2808         // still verify the size/signature making sure it is essentially the
2809         // same file but perhaps in a different location.
2810         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2811           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2812             ImportedName, /*FileMapOnly*/ true);
2813 
2814         if (ImportedFile.empty())
2815           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2816           // ModuleCache as when writing.
2817           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2818         else
2819           SkipPath(Record, Idx);
2820 
2821         // If our client can't cope with us being out of date, we can't cope with
2822         // our dependency being missing.
2823         unsigned Capabilities = ClientLoadCapabilities;
2824         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2825           Capabilities &= ~ARR_Missing;
2826 
2827         // Load the AST file.
2828         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2829                                   Loaded, StoredSize, StoredModTime,
2830                                   StoredSignature, Capabilities);
2831 
2832         // If we diagnosed a problem, produce a backtrace.
2833         if (isDiagnosedResult(Result, Capabilities))
2834           Diag(diag::note_module_file_imported_by)
2835               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2836 
2837         switch (Result) {
2838         case Failure: return Failure;
2839           // If we have to ignore the dependency, we'll have to ignore this too.
2840         case Missing:
2841         case OutOfDate: return OutOfDate;
2842         case VersionMismatch: return VersionMismatch;
2843         case ConfigurationMismatch: return ConfigurationMismatch;
2844         case HadErrors: return HadErrors;
2845         case Success: break;
2846         }
2847       }
2848       break;
2849     }
2850 
2851     case ORIGINAL_FILE:
2852       F.OriginalSourceFileID = FileID::get(Record[0]);
2853       F.ActualOriginalSourceFileName = std::string(Blob);
2854       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2855       ResolveImportedPath(F, F.OriginalSourceFileName);
2856       break;
2857 
2858     case ORIGINAL_FILE_ID:
2859       F.OriginalSourceFileID = FileID::get(Record[0]);
2860       break;
2861 
2862     case ORIGINAL_PCH_DIR:
2863       F.OriginalDir = std::string(Blob);
2864       break;
2865 
2866     case MODULE_NAME:
2867       F.ModuleName = std::string(Blob);
2868       Diag(diag::remark_module_import)
2869           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2870           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2871       if (Listener)
2872         Listener->ReadModuleName(F.ModuleName);
2873 
2874       // Validate the AST as soon as we have a name so we can exit early on
2875       // failure.
2876       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2877         return Result;
2878 
2879       break;
2880 
2881     case MODULE_DIRECTORY: {
2882       // Save the BaseDirectory as written in the PCM for computing the module
2883       // filename for the ModuleCache.
2884       BaseDirectoryAsWritten = Blob;
2885       assert(!F.ModuleName.empty() &&
2886              "MODULE_DIRECTORY found before MODULE_NAME");
2887       // If we've already loaded a module map file covering this module, we may
2888       // have a better path for it (relative to the current build).
2889       Module *M = PP.getHeaderSearchInfo().lookupModule(
2890           F.ModuleName, /*AllowSearch*/ true,
2891           /*AllowExtraModuleMapSearch*/ true);
2892       if (M && M->Directory) {
2893         // If we're implicitly loading a module, the base directory can't
2894         // change between the build and use.
2895         // Don't emit module relocation error if we have -fno-validate-pch
2896         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2897             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2898           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2899           if (!BuildDir || *BuildDir != M->Directory) {
2900             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2901               Diag(diag::err_imported_module_relocated)
2902                   << F.ModuleName << Blob << M->Directory->getName();
2903             return OutOfDate;
2904           }
2905         }
2906         F.BaseDirectory = std::string(M->Directory->getName());
2907       } else {
2908         F.BaseDirectory = std::string(Blob);
2909       }
2910       break;
2911     }
2912 
2913     case MODULE_MAP_FILE:
2914       if (ASTReadResult Result =
2915               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2916         return Result;
2917       break;
2918 
2919     case INPUT_FILE_OFFSETS:
2920       NumInputs = Record[0];
2921       NumUserInputs = Record[1];
2922       F.InputFileOffsets =
2923           (const llvm::support::unaligned_uint64_t *)Blob.data();
2924       F.InputFilesLoaded.resize(NumInputs);
2925       F.NumUserInputFiles = NumUserInputs;
2926       break;
2927     }
2928   }
2929 }
2930 
2931 ASTReader::ASTReadResult
2932 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2933   BitstreamCursor &Stream = F.Stream;
2934 
2935   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2936     Error(std::move(Err));
2937     return Failure;
2938   }
2939   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2940 
2941   // Read all of the records and blocks for the AST file.
2942   RecordData Record;
2943   while (true) {
2944     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2945     if (!MaybeEntry) {
2946       Error(MaybeEntry.takeError());
2947       return Failure;
2948     }
2949     llvm::BitstreamEntry Entry = MaybeEntry.get();
2950 
2951     switch (Entry.Kind) {
2952     case llvm::BitstreamEntry::Error:
2953       Error("error at end of module block in AST file");
2954       return Failure;
2955     case llvm::BitstreamEntry::EndBlock:
2956       // Outside of C++, we do not store a lookup map for the translation unit.
2957       // Instead, mark it as needing a lookup map to be built if this module
2958       // contains any declarations lexically within it (which it always does!).
2959       // This usually has no cost, since we very rarely need the lookup map for
2960       // the translation unit outside C++.
2961       if (ASTContext *Ctx = ContextObj) {
2962         DeclContext *DC = Ctx->getTranslationUnitDecl();
2963         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2964           DC->setMustBuildLookupTable();
2965       }
2966 
2967       return Success;
2968     case llvm::BitstreamEntry::SubBlock:
2969       switch (Entry.ID) {
2970       case DECLTYPES_BLOCK_ID:
2971         // We lazily load the decls block, but we want to set up the
2972         // DeclsCursor cursor to point into it.  Clone our current bitcode
2973         // cursor to it, enter the block and read the abbrevs in that block.
2974         // With the main cursor, we just skip over it.
2975         F.DeclsCursor = Stream;
2976         if (llvm::Error Err = Stream.SkipBlock()) {
2977           Error(std::move(Err));
2978           return Failure;
2979         }
2980         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2981                              &F.DeclsBlockStartOffset)) {
2982           Error("malformed block record in AST file");
2983           return Failure;
2984         }
2985         break;
2986 
2987       case PREPROCESSOR_BLOCK_ID:
2988         F.MacroCursor = Stream;
2989         if (!PP.getExternalSource())
2990           PP.setExternalSource(this);
2991 
2992         if (llvm::Error Err = Stream.SkipBlock()) {
2993           Error(std::move(Err));
2994           return Failure;
2995         }
2996         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2997           Error("malformed block record in AST file");
2998           return Failure;
2999         }
3000         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3001         break;
3002 
3003       case PREPROCESSOR_DETAIL_BLOCK_ID:
3004         F.PreprocessorDetailCursor = Stream;
3005 
3006         if (llvm::Error Err = Stream.SkipBlock()) {
3007           Error(std::move(Err));
3008           return Failure;
3009         }
3010         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3011                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3012           Error("malformed preprocessor detail record in AST file");
3013           return Failure;
3014         }
3015         F.PreprocessorDetailStartOffset
3016         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3017 
3018         if (!PP.getPreprocessingRecord())
3019           PP.createPreprocessingRecord();
3020         if (!PP.getPreprocessingRecord()->getExternalSource())
3021           PP.getPreprocessingRecord()->SetExternalSource(*this);
3022         break;
3023 
3024       case SOURCE_MANAGER_BLOCK_ID:
3025         if (ReadSourceManagerBlock(F))
3026           return Failure;
3027         break;
3028 
3029       case SUBMODULE_BLOCK_ID:
3030         if (ASTReadResult Result =
3031                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3032           return Result;
3033         break;
3034 
3035       case COMMENTS_BLOCK_ID: {
3036         BitstreamCursor C = Stream;
3037 
3038         if (llvm::Error Err = Stream.SkipBlock()) {
3039           Error(std::move(Err));
3040           return Failure;
3041         }
3042         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3043           Error("malformed comments block in AST file");
3044           return Failure;
3045         }
3046         CommentsCursors.push_back(std::make_pair(C, &F));
3047         break;
3048       }
3049 
3050       default:
3051         if (llvm::Error Err = Stream.SkipBlock()) {
3052           Error(std::move(Err));
3053           return Failure;
3054         }
3055         break;
3056       }
3057       continue;
3058 
3059     case llvm::BitstreamEntry::Record:
3060       // The interesting case.
3061       break;
3062     }
3063 
3064     // Read and process a record.
3065     Record.clear();
3066     StringRef Blob;
3067     Expected<unsigned> MaybeRecordType =
3068         Stream.readRecord(Entry.ID, Record, &Blob);
3069     if (!MaybeRecordType) {
3070       Error(MaybeRecordType.takeError());
3071       return Failure;
3072     }
3073     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3074 
3075     // If we're not loading an AST context, we don't care about most records.
3076     if (!ContextObj) {
3077       switch (RecordType) {
3078       case IDENTIFIER_TABLE:
3079       case IDENTIFIER_OFFSET:
3080       case INTERESTING_IDENTIFIERS:
3081       case STATISTICS:
3082       case PP_CONDITIONAL_STACK:
3083       case PP_COUNTER_VALUE:
3084       case SOURCE_LOCATION_OFFSETS:
3085       case MODULE_OFFSET_MAP:
3086       case SOURCE_MANAGER_LINE_TABLE:
3087       case SOURCE_LOCATION_PRELOADS:
3088       case PPD_ENTITIES_OFFSETS:
3089       case HEADER_SEARCH_TABLE:
3090       case IMPORTED_MODULES:
3091       case MACRO_OFFSET:
3092         break;
3093       default:
3094         continue;
3095       }
3096     }
3097 
3098     switch (RecordType) {
3099     default:  // Default behavior: ignore.
3100       break;
3101 
3102     case TYPE_OFFSET: {
3103       if (F.LocalNumTypes != 0) {
3104         Error("duplicate TYPE_OFFSET record in AST file");
3105         return Failure;
3106       }
3107       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3108       F.LocalNumTypes = Record[0];
3109       unsigned LocalBaseTypeIndex = Record[1];
3110       F.BaseTypeIndex = getTotalNumTypes();
3111 
3112       if (F.LocalNumTypes > 0) {
3113         // Introduce the global -> local mapping for types within this module.
3114         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3115 
3116         // Introduce the local -> global mapping for types within this module.
3117         F.TypeRemap.insertOrReplace(
3118           std::make_pair(LocalBaseTypeIndex,
3119                          F.BaseTypeIndex - LocalBaseTypeIndex));
3120 
3121         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3122       }
3123       break;
3124     }
3125 
3126     case DECL_OFFSET: {
3127       if (F.LocalNumDecls != 0) {
3128         Error("duplicate DECL_OFFSET record in AST file");
3129         return Failure;
3130       }
3131       F.DeclOffsets = (const DeclOffset *)Blob.data();
3132       F.LocalNumDecls = Record[0];
3133       unsigned LocalBaseDeclID = Record[1];
3134       F.BaseDeclID = getTotalNumDecls();
3135 
3136       if (F.LocalNumDecls > 0) {
3137         // Introduce the global -> local mapping for declarations within this
3138         // module.
3139         GlobalDeclMap.insert(
3140           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3141 
3142         // Introduce the local -> global mapping for declarations within this
3143         // module.
3144         F.DeclRemap.insertOrReplace(
3145           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3146 
3147         // Introduce the global -> local mapping for declarations within this
3148         // module.
3149         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3150 
3151         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3152       }
3153       break;
3154     }
3155 
3156     case TU_UPDATE_LEXICAL: {
3157       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3158       LexicalContents Contents(
3159           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3160               Blob.data()),
3161           static_cast<unsigned int>(Blob.size() / 4));
3162       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3163       TU->setHasExternalLexicalStorage(true);
3164       break;
3165     }
3166 
3167     case UPDATE_VISIBLE: {
3168       unsigned Idx = 0;
3169       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3170       auto *Data = (const unsigned char*)Blob.data();
3171       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3172       // If we've already loaded the decl, perform the updates when we finish
3173       // loading this block.
3174       if (Decl *D = GetExistingDecl(ID))
3175         PendingUpdateRecords.push_back(
3176             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3177       break;
3178     }
3179 
3180     case IDENTIFIER_TABLE:
3181       F.IdentifierTableData = Blob.data();
3182       if (Record[0]) {
3183         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3184             (const unsigned char *)F.IdentifierTableData + Record[0],
3185             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3186             (const unsigned char *)F.IdentifierTableData,
3187             ASTIdentifierLookupTrait(*this, F));
3188 
3189         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3190       }
3191       break;
3192 
3193     case IDENTIFIER_OFFSET: {
3194       if (F.LocalNumIdentifiers != 0) {
3195         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3196         return Failure;
3197       }
3198       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3199       F.LocalNumIdentifiers = Record[0];
3200       unsigned LocalBaseIdentifierID = Record[1];
3201       F.BaseIdentifierID = getTotalNumIdentifiers();
3202 
3203       if (F.LocalNumIdentifiers > 0) {
3204         // Introduce the global -> local mapping for identifiers within this
3205         // module.
3206         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3207                                                   &F));
3208 
3209         // Introduce the local -> global mapping for identifiers within this
3210         // module.
3211         F.IdentifierRemap.insertOrReplace(
3212           std::make_pair(LocalBaseIdentifierID,
3213                          F.BaseIdentifierID - LocalBaseIdentifierID));
3214 
3215         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3216                                  + F.LocalNumIdentifiers);
3217       }
3218       break;
3219     }
3220 
3221     case INTERESTING_IDENTIFIERS:
3222       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3223       break;
3224 
3225     case EAGERLY_DESERIALIZED_DECLS:
3226       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3227       // about "interesting" decls (for instance, if we're building a module).
3228       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3229         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3230       break;
3231 
3232     case MODULAR_CODEGEN_DECLS:
3233       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3234       // them (ie: if we're not codegenerating this module).
3235       if (F.Kind == MK_MainFile ||
3236           getContext().getLangOpts().BuildingPCHWithObjectFile)
3237         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3238           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3239       break;
3240 
3241     case SPECIAL_TYPES:
3242       if (SpecialTypes.empty()) {
3243         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3244           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3245         break;
3246       }
3247 
3248       if (SpecialTypes.size() != Record.size()) {
3249         Error("invalid special-types record");
3250         return Failure;
3251       }
3252 
3253       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3254         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3255         if (!SpecialTypes[I])
3256           SpecialTypes[I] = ID;
3257         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3258         // merge step?
3259       }
3260       break;
3261 
3262     case STATISTICS:
3263       TotalNumStatements += Record[0];
3264       TotalNumMacros += Record[1];
3265       TotalLexicalDeclContexts += Record[2];
3266       TotalVisibleDeclContexts += Record[3];
3267       break;
3268 
3269     case UNUSED_FILESCOPED_DECLS:
3270       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3271         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3272       break;
3273 
3274     case DELEGATING_CTORS:
3275       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3276         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3277       break;
3278 
3279     case WEAK_UNDECLARED_IDENTIFIERS:
3280       if (Record.size() % 4 != 0) {
3281         Error("invalid weak identifiers record");
3282         return Failure;
3283       }
3284 
3285       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3286       // files. This isn't the way to do it :)
3287       WeakUndeclaredIdentifiers.clear();
3288 
3289       // Translate the weak, undeclared identifiers into global IDs.
3290       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3291         WeakUndeclaredIdentifiers.push_back(
3292           getGlobalIdentifierID(F, Record[I++]));
3293         WeakUndeclaredIdentifiers.push_back(
3294           getGlobalIdentifierID(F, Record[I++]));
3295         WeakUndeclaredIdentifiers.push_back(
3296           ReadSourceLocation(F, Record, I).getRawEncoding());
3297         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3298       }
3299       break;
3300 
3301     case SELECTOR_OFFSETS: {
3302       F.SelectorOffsets = (const uint32_t *)Blob.data();
3303       F.LocalNumSelectors = Record[0];
3304       unsigned LocalBaseSelectorID = Record[1];
3305       F.BaseSelectorID = getTotalNumSelectors();
3306 
3307       if (F.LocalNumSelectors > 0) {
3308         // Introduce the global -> local mapping for selectors within this
3309         // module.
3310         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3311 
3312         // Introduce the local -> global mapping for selectors within this
3313         // module.
3314         F.SelectorRemap.insertOrReplace(
3315           std::make_pair(LocalBaseSelectorID,
3316                          F.BaseSelectorID - LocalBaseSelectorID));
3317 
3318         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3319       }
3320       break;
3321     }
3322 
3323     case METHOD_POOL:
3324       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3325       if (Record[0])
3326         F.SelectorLookupTable
3327           = ASTSelectorLookupTable::Create(
3328                         F.SelectorLookupTableData + Record[0],
3329                         F.SelectorLookupTableData,
3330                         ASTSelectorLookupTrait(*this, F));
3331       TotalNumMethodPoolEntries += Record[1];
3332       break;
3333 
3334     case REFERENCED_SELECTOR_POOL:
3335       if (!Record.empty()) {
3336         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3337           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3338                                                                 Record[Idx++]));
3339           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3340                                               getRawEncoding());
3341         }
3342       }
3343       break;
3344 
3345     case PP_CONDITIONAL_STACK:
3346       if (!Record.empty()) {
3347         unsigned Idx = 0, End = Record.size() - 1;
3348         bool ReachedEOFWhileSkipping = Record[Idx++];
3349         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3350         if (ReachedEOFWhileSkipping) {
3351           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3352           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3353           bool FoundNonSkipPortion = Record[Idx++];
3354           bool FoundElse = Record[Idx++];
3355           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3356           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3357                            FoundElse, ElseLoc);
3358         }
3359         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3360         while (Idx < End) {
3361           auto Loc = ReadSourceLocation(F, Record, Idx);
3362           bool WasSkipping = Record[Idx++];
3363           bool FoundNonSkip = Record[Idx++];
3364           bool FoundElse = Record[Idx++];
3365           ConditionalStack.push_back(
3366               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3367         }
3368         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3369       }
3370       break;
3371 
3372     case PP_COUNTER_VALUE:
3373       if (!Record.empty() && Listener)
3374         Listener->ReadCounter(F, Record[0]);
3375       break;
3376 
3377     case FILE_SORTED_DECLS:
3378       F.FileSortedDecls = (const DeclID *)Blob.data();
3379       F.NumFileSortedDecls = Record[0];
3380       break;
3381 
3382     case SOURCE_LOCATION_OFFSETS: {
3383       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3384       F.LocalNumSLocEntries = Record[0];
3385       unsigned SLocSpaceSize = Record[1];
3386       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3387       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3388           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3389                                               SLocSpaceSize);
3390       if (!F.SLocEntryBaseID) {
3391         Error("ran out of source locations");
3392         break;
3393       }
3394       // Make our entry in the range map. BaseID is negative and growing, so
3395       // we invert it. Because we invert it, though, we need the other end of
3396       // the range.
3397       unsigned RangeStart =
3398           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3399       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3400       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3401 
3402       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3403       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3404       GlobalSLocOffsetMap.insert(
3405           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3406                            - SLocSpaceSize,&F));
3407 
3408       // Initialize the remapping table.
3409       // Invalid stays invalid.
3410       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3411       // This module. Base was 2 when being compiled.
3412       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3413                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3414 
3415       TotalNumSLocEntries += F.LocalNumSLocEntries;
3416       break;
3417     }
3418 
3419     case MODULE_OFFSET_MAP:
3420       F.ModuleOffsetMap = Blob;
3421       break;
3422 
3423     case SOURCE_MANAGER_LINE_TABLE:
3424       if (ParseLineTable(F, Record)) {
3425         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3426         return Failure;
3427       }
3428       break;
3429 
3430     case SOURCE_LOCATION_PRELOADS: {
3431       // Need to transform from the local view (1-based IDs) to the global view,
3432       // which is based off F.SLocEntryBaseID.
3433       if (!F.PreloadSLocEntries.empty()) {
3434         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3435         return Failure;
3436       }
3437 
3438       F.PreloadSLocEntries.swap(Record);
3439       break;
3440     }
3441 
3442     case EXT_VECTOR_DECLS:
3443       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3444         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3445       break;
3446 
3447     case VTABLE_USES:
3448       if (Record.size() % 3 != 0) {
3449         Error("Invalid VTABLE_USES record");
3450         return Failure;
3451       }
3452 
3453       // Later tables overwrite earlier ones.
3454       // FIXME: Modules will have some trouble with this. This is clearly not
3455       // the right way to do this.
3456       VTableUses.clear();
3457 
3458       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3459         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3460         VTableUses.push_back(
3461           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3462         VTableUses.push_back(Record[Idx++]);
3463       }
3464       break;
3465 
3466     case PENDING_IMPLICIT_INSTANTIATIONS:
3467       if (PendingInstantiations.size() % 2 != 0) {
3468         Error("Invalid existing PendingInstantiations");
3469         return Failure;
3470       }
3471 
3472       if (Record.size() % 2 != 0) {
3473         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3474         return Failure;
3475       }
3476 
3477       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3478         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3479         PendingInstantiations.push_back(
3480           ReadSourceLocation(F, Record, I).getRawEncoding());
3481       }
3482       break;
3483 
3484     case SEMA_DECL_REFS:
3485       if (Record.size() != 3) {
3486         Error("Invalid SEMA_DECL_REFS block");
3487         return Failure;
3488       }
3489       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3490         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3491       break;
3492 
3493     case PPD_ENTITIES_OFFSETS: {
3494       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3495       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3496       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3497 
3498       unsigned LocalBasePreprocessedEntityID = Record[0];
3499 
3500       unsigned StartingID;
3501       if (!PP.getPreprocessingRecord())
3502         PP.createPreprocessingRecord();
3503       if (!PP.getPreprocessingRecord()->getExternalSource())
3504         PP.getPreprocessingRecord()->SetExternalSource(*this);
3505       StartingID
3506         = PP.getPreprocessingRecord()
3507             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3508       F.BasePreprocessedEntityID = StartingID;
3509 
3510       if (F.NumPreprocessedEntities > 0) {
3511         // Introduce the global -> local mapping for preprocessed entities in
3512         // this module.
3513         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3514 
3515         // Introduce the local -> global mapping for preprocessed entities in
3516         // this module.
3517         F.PreprocessedEntityRemap.insertOrReplace(
3518           std::make_pair(LocalBasePreprocessedEntityID,
3519             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3520       }
3521 
3522       break;
3523     }
3524 
3525     case PPD_SKIPPED_RANGES: {
3526       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3527       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3528       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3529 
3530       if (!PP.getPreprocessingRecord())
3531         PP.createPreprocessingRecord();
3532       if (!PP.getPreprocessingRecord()->getExternalSource())
3533         PP.getPreprocessingRecord()->SetExternalSource(*this);
3534       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3535           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3536 
3537       if (F.NumPreprocessedSkippedRanges > 0)
3538         GlobalSkippedRangeMap.insert(
3539             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3540       break;
3541     }
3542 
3543     case DECL_UPDATE_OFFSETS:
3544       if (Record.size() % 2 != 0) {
3545         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3546         return Failure;
3547       }
3548       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3549         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3550         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3551 
3552         // If we've already loaded the decl, perform the updates when we finish
3553         // loading this block.
3554         if (Decl *D = GetExistingDecl(ID))
3555           PendingUpdateRecords.push_back(
3556               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3557       }
3558       break;
3559 
3560     case OBJC_CATEGORIES_MAP:
3561       if (F.LocalNumObjCCategoriesInMap != 0) {
3562         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3563         return Failure;
3564       }
3565 
3566       F.LocalNumObjCCategoriesInMap = Record[0];
3567       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3568       break;
3569 
3570     case OBJC_CATEGORIES:
3571       F.ObjCCategories.swap(Record);
3572       break;
3573 
3574     case CUDA_SPECIAL_DECL_REFS:
3575       // Later tables overwrite earlier ones.
3576       // FIXME: Modules will have trouble with this.
3577       CUDASpecialDeclRefs.clear();
3578       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3579         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3580       break;
3581 
3582     case HEADER_SEARCH_TABLE:
3583       F.HeaderFileInfoTableData = Blob.data();
3584       F.LocalNumHeaderFileInfos = Record[1];
3585       if (Record[0]) {
3586         F.HeaderFileInfoTable
3587           = HeaderFileInfoLookupTable::Create(
3588                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3589                    (const unsigned char *)F.HeaderFileInfoTableData,
3590                    HeaderFileInfoTrait(*this, F,
3591                                        &PP.getHeaderSearchInfo(),
3592                                        Blob.data() + Record[2]));
3593 
3594         PP.getHeaderSearchInfo().SetExternalSource(this);
3595         if (!PP.getHeaderSearchInfo().getExternalLookup())
3596           PP.getHeaderSearchInfo().SetExternalLookup(this);
3597       }
3598       break;
3599 
3600     case FP_PRAGMA_OPTIONS:
3601       // Later tables overwrite earlier ones.
3602       FPPragmaOptions.swap(Record);
3603       break;
3604 
3605     case OPENCL_EXTENSIONS:
3606       for (unsigned I = 0, E = Record.size(); I != E; ) {
3607         auto Name = ReadString(Record, I);
3608         auto &Opt = OpenCLExtensions.OptMap[Name];
3609         Opt.Supported = Record[I++] != 0;
3610         Opt.Enabled = Record[I++] != 0;
3611         Opt.Avail = Record[I++];
3612         Opt.Core = Record[I++];
3613       }
3614       break;
3615 
3616     case OPENCL_EXTENSION_TYPES:
3617       for (unsigned I = 0, E = Record.size(); I != E;) {
3618         auto TypeID = static_cast<::TypeID>(Record[I++]);
3619         auto *Type = GetType(TypeID).getTypePtr();
3620         auto NumExt = static_cast<unsigned>(Record[I++]);
3621         for (unsigned II = 0; II != NumExt; ++II) {
3622           auto Ext = ReadString(Record, I);
3623           OpenCLTypeExtMap[Type].insert(Ext);
3624         }
3625       }
3626       break;
3627 
3628     case OPENCL_EXTENSION_DECLS:
3629       for (unsigned I = 0, E = Record.size(); I != E;) {
3630         auto DeclID = static_cast<::DeclID>(Record[I++]);
3631         auto *Decl = GetDecl(DeclID);
3632         auto NumExt = static_cast<unsigned>(Record[I++]);
3633         for (unsigned II = 0; II != NumExt; ++II) {
3634           auto Ext = ReadString(Record, I);
3635           OpenCLDeclExtMap[Decl].insert(Ext);
3636         }
3637       }
3638       break;
3639 
3640     case TENTATIVE_DEFINITIONS:
3641       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3642         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3643       break;
3644 
3645     case KNOWN_NAMESPACES:
3646       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3647         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3648       break;
3649 
3650     case UNDEFINED_BUT_USED:
3651       if (UndefinedButUsed.size() % 2 != 0) {
3652         Error("Invalid existing UndefinedButUsed");
3653         return Failure;
3654       }
3655 
3656       if (Record.size() % 2 != 0) {
3657         Error("invalid undefined-but-used record");
3658         return Failure;
3659       }
3660       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3661         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3662         UndefinedButUsed.push_back(
3663             ReadSourceLocation(F, Record, I).getRawEncoding());
3664       }
3665       break;
3666 
3667     case DELETE_EXPRS_TO_ANALYZE:
3668       for (unsigned I = 0, N = Record.size(); I != N;) {
3669         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3670         const uint64_t Count = Record[I++];
3671         DelayedDeleteExprs.push_back(Count);
3672         for (uint64_t C = 0; C < Count; ++C) {
3673           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3674           bool IsArrayForm = Record[I++] == 1;
3675           DelayedDeleteExprs.push_back(IsArrayForm);
3676         }
3677       }
3678       break;
3679 
3680     case IMPORTED_MODULES:
3681       if (!F.isModule()) {
3682         // If we aren't loading a module (which has its own exports), make
3683         // all of the imported modules visible.
3684         // FIXME: Deal with macros-only imports.
3685         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3686           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3687           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3688           if (GlobalID) {
3689             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3690             if (DeserializationListener)
3691               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3692           }
3693         }
3694       }
3695       break;
3696 
3697     case MACRO_OFFSET: {
3698       if (F.LocalNumMacros != 0) {
3699         Error("duplicate MACRO_OFFSET record in AST file");
3700         return Failure;
3701       }
3702       F.MacroOffsets = (const uint32_t *)Blob.data();
3703       F.LocalNumMacros = Record[0];
3704       unsigned LocalBaseMacroID = Record[1];
3705       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3706       F.BaseMacroID = getTotalNumMacros();
3707 
3708       if (F.LocalNumMacros > 0) {
3709         // Introduce the global -> local mapping for macros within this module.
3710         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3711 
3712         // Introduce the local -> global mapping for macros within this module.
3713         F.MacroRemap.insertOrReplace(
3714           std::make_pair(LocalBaseMacroID,
3715                          F.BaseMacroID - LocalBaseMacroID));
3716 
3717         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3718       }
3719       break;
3720     }
3721 
3722     case LATE_PARSED_TEMPLATE:
3723       LateParsedTemplates.append(Record.begin(), Record.end());
3724       break;
3725 
3726     case OPTIMIZE_PRAGMA_OPTIONS:
3727       if (Record.size() != 1) {
3728         Error("invalid pragma optimize record");
3729         return Failure;
3730       }
3731       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3732       break;
3733 
3734     case MSSTRUCT_PRAGMA_OPTIONS:
3735       if (Record.size() != 1) {
3736         Error("invalid pragma ms_struct record");
3737         return Failure;
3738       }
3739       PragmaMSStructState = Record[0];
3740       break;
3741 
3742     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3743       if (Record.size() != 2) {
3744         Error("invalid pragma ms_struct record");
3745         return Failure;
3746       }
3747       PragmaMSPointersToMembersState = Record[0];
3748       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3749       break;
3750 
3751     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3752       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3753         UnusedLocalTypedefNameCandidates.push_back(
3754             getGlobalDeclID(F, Record[I]));
3755       break;
3756 
3757     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3758       if (Record.size() != 1) {
3759         Error("invalid cuda pragma options record");
3760         return Failure;
3761       }
3762       ForceCUDAHostDeviceDepth = Record[0];
3763       break;
3764 
3765     case PACK_PRAGMA_OPTIONS: {
3766       if (Record.size() < 3) {
3767         Error("invalid pragma pack record");
3768         return Failure;
3769       }
3770       PragmaPackCurrentValue = Record[0];
3771       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3772       unsigned NumStackEntries = Record[2];
3773       unsigned Idx = 3;
3774       // Reset the stack when importing a new module.
3775       PragmaPackStack.clear();
3776       for (unsigned I = 0; I < NumStackEntries; ++I) {
3777         PragmaPackStackEntry Entry;
3778         Entry.Value = Record[Idx++];
3779         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3780         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3781         PragmaPackStrings.push_back(ReadString(Record, Idx));
3782         Entry.SlotLabel = PragmaPackStrings.back();
3783         PragmaPackStack.push_back(Entry);
3784       }
3785       break;
3786     }
3787 
3788     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3789       if (Record.size() < 3) {
3790         Error("invalid pragma pack record");
3791         return Failure;
3792       }
3793       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3794       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3795       unsigned NumStackEntries = Record[2];
3796       unsigned Idx = 3;
3797       // Reset the stack when importing a new module.
3798       FpPragmaStack.clear();
3799       for (unsigned I = 0; I < NumStackEntries; ++I) {
3800         FpPragmaStackEntry Entry;
3801         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3802         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3803         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3804         FpPragmaStrings.push_back(ReadString(Record, Idx));
3805         Entry.SlotLabel = FpPragmaStrings.back();
3806         FpPragmaStack.push_back(Entry);
3807       }
3808       break;
3809     }
3810 
3811     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3812       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3813         DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3814       break;
3815     }
3816   }
3817 }
3818 
3819 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3820   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3821 
3822   // Additional remapping information.
3823   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3824   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3825   F.ModuleOffsetMap = StringRef();
3826 
3827   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3828   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3829     F.SLocRemap.insert(std::make_pair(0U, 0));
3830     F.SLocRemap.insert(std::make_pair(2U, 1));
3831   }
3832 
3833   // Continuous range maps we may be updating in our module.
3834   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3835   RemapBuilder SLocRemap(F.SLocRemap);
3836   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3837   RemapBuilder MacroRemap(F.MacroRemap);
3838   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3839   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3840   RemapBuilder SelectorRemap(F.SelectorRemap);
3841   RemapBuilder DeclRemap(F.DeclRemap);
3842   RemapBuilder TypeRemap(F.TypeRemap);
3843 
3844   while (Data < DataEnd) {
3845     // FIXME: Looking up dependency modules by filename is horrible. Let's
3846     // start fixing this with prebuilt, explicit and implicit modules and see
3847     // how it goes...
3848     using namespace llvm::support;
3849     ModuleKind Kind = static_cast<ModuleKind>(
3850       endian::readNext<uint8_t, little, unaligned>(Data));
3851     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3852     StringRef Name = StringRef((const char*)Data, Len);
3853     Data += Len;
3854     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3855                               Kind == MK_ImplicitModule
3856                           ? ModuleMgr.lookupByModuleName(Name)
3857                           : ModuleMgr.lookupByFileName(Name));
3858     if (!OM) {
3859       std::string Msg =
3860           "SourceLocation remap refers to unknown module, cannot find ";
3861       Msg.append(std::string(Name));
3862       Error(Msg);
3863       return;
3864     }
3865 
3866     uint32_t SLocOffset =
3867         endian::readNext<uint32_t, little, unaligned>(Data);
3868     uint32_t IdentifierIDOffset =
3869         endian::readNext<uint32_t, little, unaligned>(Data);
3870     uint32_t MacroIDOffset =
3871         endian::readNext<uint32_t, little, unaligned>(Data);
3872     uint32_t PreprocessedEntityIDOffset =
3873         endian::readNext<uint32_t, little, unaligned>(Data);
3874     uint32_t SubmoduleIDOffset =
3875         endian::readNext<uint32_t, little, unaligned>(Data);
3876     uint32_t SelectorIDOffset =
3877         endian::readNext<uint32_t, little, unaligned>(Data);
3878     uint32_t DeclIDOffset =
3879         endian::readNext<uint32_t, little, unaligned>(Data);
3880     uint32_t TypeIndexOffset =
3881         endian::readNext<uint32_t, little, unaligned>(Data);
3882 
3883     uint32_t None = std::numeric_limits<uint32_t>::max();
3884 
3885     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3886                          RemapBuilder &Remap) {
3887       if (Offset != None)
3888         Remap.insert(std::make_pair(Offset,
3889                                     static_cast<int>(BaseOffset - Offset)));
3890     };
3891     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3892     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3893     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3894     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3895               PreprocessedEntityRemap);
3896     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3897     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3898     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3899     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3900 
3901     // Global -> local mappings.
3902     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3903   }
3904 }
3905 
3906 ASTReader::ASTReadResult
3907 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3908                                   const ModuleFile *ImportedBy,
3909                                   unsigned ClientLoadCapabilities) {
3910   unsigned Idx = 0;
3911   F.ModuleMapPath = ReadPath(F, Record, Idx);
3912 
3913   // Try to resolve ModuleName in the current header search context and
3914   // verify that it is found in the same module map file as we saved. If the
3915   // top-level AST file is a main file, skip this check because there is no
3916   // usable header search context.
3917   assert(!F.ModuleName.empty() &&
3918          "MODULE_NAME should come before MODULE_MAP_FILE");
3919   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3920     // An implicitly-loaded module file should have its module listed in some
3921     // module map file that we've already loaded.
3922     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3923     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3924     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3925     // Don't emit module relocation error if we have -fno-validate-pch
3926     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3927       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3928         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3929           // This module was defined by an imported (explicit) module.
3930           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3931                                                << ASTFE->getName();
3932         } else {
3933           // This module was built with a different module map.
3934           Diag(diag::err_imported_module_not_found)
3935               << F.ModuleName << F.FileName
3936               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3937               << !ImportedBy;
3938           // In case it was imported by a PCH, there's a chance the user is
3939           // just missing to include the search path to the directory containing
3940           // the modulemap.
3941           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3942             Diag(diag::note_imported_by_pch_module_not_found)
3943                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3944         }
3945       }
3946       return OutOfDate;
3947     }
3948 
3949     assert(M->Name == F.ModuleName && "found module with different name");
3950 
3951     // Check the primary module map file.
3952     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3953     if (!StoredModMap || *StoredModMap != ModMap) {
3954       assert(ModMap && "found module is missing module map file");
3955       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3956              "top-level import should be verified");
3957       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3958       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3959         Diag(diag::err_imported_module_modmap_changed)
3960             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3961             << ModMap->getName() << F.ModuleMapPath << NotImported;
3962       return OutOfDate;
3963     }
3964 
3965     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3966     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3967       // FIXME: we should use input files rather than storing names.
3968       std::string Filename = ReadPath(F, Record, Idx);
3969       auto F = FileMgr.getFile(Filename, false, false);
3970       if (!F) {
3971         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3972           Error("could not find file '" + Filename +"' referenced by AST file");
3973         return OutOfDate;
3974       }
3975       AdditionalStoredMaps.insert(*F);
3976     }
3977 
3978     // Check any additional module map files (e.g. module.private.modulemap)
3979     // that are not in the pcm.
3980     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3981       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3982         // Remove files that match
3983         // Note: SmallPtrSet::erase is really remove
3984         if (!AdditionalStoredMaps.erase(ModMap)) {
3985           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3986             Diag(diag::err_module_different_modmap)
3987               << F.ModuleName << /*new*/0 << ModMap->getName();
3988           return OutOfDate;
3989         }
3990       }
3991     }
3992 
3993     // Check any additional module map files that are in the pcm, but not
3994     // found in header search. Cases that match are already removed.
3995     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3996       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3997         Diag(diag::err_module_different_modmap)
3998           << F.ModuleName << /*not new*/1 << ModMap->getName();
3999       return OutOfDate;
4000     }
4001   }
4002 
4003   if (Listener)
4004     Listener->ReadModuleMapFile(F.ModuleMapPath);
4005   return Success;
4006 }
4007 
4008 /// Move the given method to the back of the global list of methods.
4009 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4010   // Find the entry for this selector in the method pool.
4011   Sema::GlobalMethodPool::iterator Known
4012     = S.MethodPool.find(Method->getSelector());
4013   if (Known == S.MethodPool.end())
4014     return;
4015 
4016   // Retrieve the appropriate method list.
4017   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4018                                                     : Known->second.second;
4019   bool Found = false;
4020   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4021     if (!Found) {
4022       if (List->getMethod() == Method) {
4023         Found = true;
4024       } else {
4025         // Keep searching.
4026         continue;
4027       }
4028     }
4029 
4030     if (List->getNext())
4031       List->setMethod(List->getNext()->getMethod());
4032     else
4033       List->setMethod(Method);
4034   }
4035 }
4036 
4037 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4038   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4039   for (Decl *D : Names) {
4040     bool wasHidden = !D->isUnconditionallyVisible();
4041     D->setVisibleDespiteOwningModule();
4042 
4043     if (wasHidden && SemaObj) {
4044       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4045         moveMethodToBackOfGlobalList(*SemaObj, Method);
4046       }
4047     }
4048   }
4049 }
4050 
4051 void ASTReader::makeModuleVisible(Module *Mod,
4052                                   Module::NameVisibilityKind NameVisibility,
4053                                   SourceLocation ImportLoc) {
4054   llvm::SmallPtrSet<Module *, 4> Visited;
4055   SmallVector<Module *, 4> Stack;
4056   Stack.push_back(Mod);
4057   while (!Stack.empty()) {
4058     Mod = Stack.pop_back_val();
4059 
4060     if (NameVisibility <= Mod->NameVisibility) {
4061       // This module already has this level of visibility (or greater), so
4062       // there is nothing more to do.
4063       continue;
4064     }
4065 
4066     if (Mod->isUnimportable()) {
4067       // Modules that aren't importable cannot be made visible.
4068       continue;
4069     }
4070 
4071     // Update the module's name visibility.
4072     Mod->NameVisibility = NameVisibility;
4073 
4074     // If we've already deserialized any names from this module,
4075     // mark them as visible.
4076     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4077     if (Hidden != HiddenNamesMap.end()) {
4078       auto HiddenNames = std::move(*Hidden);
4079       HiddenNamesMap.erase(Hidden);
4080       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4081       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4082              "making names visible added hidden names");
4083     }
4084 
4085     // Push any exported modules onto the stack to be marked as visible.
4086     SmallVector<Module *, 16> Exports;
4087     Mod->getExportedModules(Exports);
4088     for (SmallVectorImpl<Module *>::iterator
4089            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4090       Module *Exported = *I;
4091       if (Visited.insert(Exported).second)
4092         Stack.push_back(Exported);
4093     }
4094   }
4095 }
4096 
4097 /// We've merged the definition \p MergedDef into the existing definition
4098 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4099 /// visible.
4100 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4101                                           NamedDecl *MergedDef) {
4102   if (!Def->isUnconditionallyVisible()) {
4103     // If MergedDef is visible or becomes visible, make the definition visible.
4104     if (MergedDef->isUnconditionallyVisible())
4105       Def->setVisibleDespiteOwningModule();
4106     else {
4107       getContext().mergeDefinitionIntoModule(
4108           Def, MergedDef->getImportedOwningModule(),
4109           /*NotifyListeners*/ false);
4110       PendingMergedDefinitionsToDeduplicate.insert(Def);
4111     }
4112   }
4113 }
4114 
4115 bool ASTReader::loadGlobalIndex() {
4116   if (GlobalIndex)
4117     return false;
4118 
4119   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4120       !PP.getLangOpts().Modules)
4121     return true;
4122 
4123   // Try to load the global index.
4124   TriedLoadingGlobalIndex = true;
4125   StringRef ModuleCachePath
4126     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4127   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4128       GlobalModuleIndex::readIndex(ModuleCachePath);
4129   if (llvm::Error Err = std::move(Result.second)) {
4130     assert(!Result.first);
4131     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4132     return true;
4133   }
4134 
4135   GlobalIndex.reset(Result.first);
4136   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4137   return false;
4138 }
4139 
4140 bool ASTReader::isGlobalIndexUnavailable() const {
4141   return PP.getLangOpts().Modules && UseGlobalIndex &&
4142          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4143 }
4144 
4145 static void updateModuleTimestamp(ModuleFile &MF) {
4146   // Overwrite the timestamp file contents so that file's mtime changes.
4147   std::string TimestampFilename = MF.getTimestampFilename();
4148   std::error_code EC;
4149   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4150   if (EC)
4151     return;
4152   OS << "Timestamp file\n";
4153   OS.close();
4154   OS.clear_error(); // Avoid triggering a fatal error.
4155 }
4156 
4157 /// Given a cursor at the start of an AST file, scan ahead and drop the
4158 /// cursor into the start of the given block ID, returning false on success and
4159 /// true on failure.
4160 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4161   while (true) {
4162     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4163     if (!MaybeEntry) {
4164       // FIXME this drops errors on the floor.
4165       consumeError(MaybeEntry.takeError());
4166       return true;
4167     }
4168     llvm::BitstreamEntry Entry = MaybeEntry.get();
4169 
4170     switch (Entry.Kind) {
4171     case llvm::BitstreamEntry::Error:
4172     case llvm::BitstreamEntry::EndBlock:
4173       return true;
4174 
4175     case llvm::BitstreamEntry::Record:
4176       // Ignore top-level records.
4177       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4178         break;
4179       else {
4180         // FIXME this drops errors on the floor.
4181         consumeError(Skipped.takeError());
4182         return true;
4183       }
4184 
4185     case llvm::BitstreamEntry::SubBlock:
4186       if (Entry.ID == BlockID) {
4187         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4188           // FIXME this drops the error on the floor.
4189           consumeError(std::move(Err));
4190           return true;
4191         }
4192         // Found it!
4193         return false;
4194       }
4195 
4196       if (llvm::Error Err = Cursor.SkipBlock()) {
4197         // FIXME this drops the error on the floor.
4198         consumeError(std::move(Err));
4199         return true;
4200       }
4201     }
4202   }
4203 }
4204 
4205 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4206                                             ModuleKind Type,
4207                                             SourceLocation ImportLoc,
4208                                             unsigned ClientLoadCapabilities,
4209                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4210   llvm::SaveAndRestore<SourceLocation>
4211     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4212 
4213   // Defer any pending actions until we get to the end of reading the AST file.
4214   Deserializing AnASTFile(this);
4215 
4216   // Bump the generation number.
4217   unsigned PreviousGeneration = 0;
4218   if (ContextObj)
4219     PreviousGeneration = incrementGeneration(*ContextObj);
4220 
4221   unsigned NumModules = ModuleMgr.size();
4222   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4223     assert(ReadResult && "expected to return error");
4224     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4225                             PP.getLangOpts().Modules
4226                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4227                                 : nullptr);
4228 
4229     // If we find that any modules are unusable, the global index is going
4230     // to be out-of-date. Just remove it.
4231     GlobalIndex.reset();
4232     ModuleMgr.setGlobalIndex(nullptr);
4233     return ReadResult;
4234   };
4235 
4236   SmallVector<ImportedModule, 4> Loaded;
4237   switch (ASTReadResult ReadResult =
4238               ReadASTCore(FileName, Type, ImportLoc,
4239                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4240                           ASTFileSignature(), ClientLoadCapabilities)) {
4241   case Failure:
4242   case Missing:
4243   case OutOfDate:
4244   case VersionMismatch:
4245   case ConfigurationMismatch:
4246   case HadErrors:
4247     return removeModulesAndReturn(ReadResult);
4248   case Success:
4249     break;
4250   }
4251 
4252   // Here comes stuff that we only do once the entire chain is loaded.
4253 
4254   // Load the AST blocks of all of the modules that we loaded.  We can still
4255   // hit errors parsing the ASTs at this point.
4256   for (ImportedModule &M : Loaded) {
4257     ModuleFile &F = *M.Mod;
4258 
4259     // Read the AST block.
4260     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4261       return removeModulesAndReturn(Result);
4262 
4263     // The AST block should always have a definition for the main module.
4264     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4265       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4266       return removeModulesAndReturn(Failure);
4267     }
4268 
4269     // Read the extension blocks.
4270     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4271       if (ASTReadResult Result = ReadExtensionBlock(F))
4272         return removeModulesAndReturn(Result);
4273     }
4274 
4275     // Once read, set the ModuleFile bit base offset and update the size in
4276     // bits of all files we've seen.
4277     F.GlobalBitOffset = TotalModulesSizeInBits;
4278     TotalModulesSizeInBits += F.SizeInBits;
4279     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4280   }
4281 
4282   // Preload source locations and interesting indentifiers.
4283   for (ImportedModule &M : Loaded) {
4284     ModuleFile &F = *M.Mod;
4285 
4286     // Preload SLocEntries.
4287     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4288       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4289       // Load it through the SourceManager and don't call ReadSLocEntry()
4290       // directly because the entry may have already been loaded in which case
4291       // calling ReadSLocEntry() directly would trigger an assertion in
4292       // SourceManager.
4293       SourceMgr.getLoadedSLocEntryByID(Index);
4294     }
4295 
4296     // Map the original source file ID into the ID space of the current
4297     // compilation.
4298     if (F.OriginalSourceFileID.isValid()) {
4299       F.OriginalSourceFileID = FileID::get(
4300           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4301     }
4302 
4303     // Preload all the pending interesting identifiers by marking them out of
4304     // date.
4305     for (auto Offset : F.PreloadIdentifierOffsets) {
4306       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4307           F.IdentifierTableData + Offset);
4308 
4309       ASTIdentifierLookupTrait Trait(*this, F);
4310       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4311       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4312       auto &II = PP.getIdentifierTable().getOwn(Key);
4313       II.setOutOfDate(true);
4314 
4315       // Mark this identifier as being from an AST file so that we can track
4316       // whether we need to serialize it.
4317       markIdentifierFromAST(*this, II);
4318 
4319       // Associate the ID with the identifier so that the writer can reuse it.
4320       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4321       SetIdentifierInfo(ID, &II);
4322     }
4323   }
4324 
4325   // Setup the import locations and notify the module manager that we've
4326   // committed to these module files.
4327   for (ImportedModule &M : Loaded) {
4328     ModuleFile &F = *M.Mod;
4329 
4330     ModuleMgr.moduleFileAccepted(&F);
4331 
4332     // Set the import location.
4333     F.DirectImportLoc = ImportLoc;
4334     // FIXME: We assume that locations from PCH / preamble do not need
4335     // any translation.
4336     if (!M.ImportedBy)
4337       F.ImportLoc = M.ImportLoc;
4338     else
4339       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4340   }
4341 
4342   if (!PP.getLangOpts().CPlusPlus ||
4343       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4344        Type != MK_PrebuiltModule)) {
4345     // Mark all of the identifiers in the identifier table as being out of date,
4346     // so that various accessors know to check the loaded modules when the
4347     // identifier is used.
4348     //
4349     // For C++ modules, we don't need information on many identifiers (just
4350     // those that provide macros or are poisoned), so we mark all of
4351     // the interesting ones via PreloadIdentifierOffsets.
4352     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4353                                 IdEnd = PP.getIdentifierTable().end();
4354          Id != IdEnd; ++Id)
4355       Id->second->setOutOfDate(true);
4356   }
4357   // Mark selectors as out of date.
4358   for (auto Sel : SelectorGeneration)
4359     SelectorOutOfDate[Sel.first] = true;
4360 
4361   // Resolve any unresolved module exports.
4362   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4363     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4364     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4365     Module *ResolvedMod = getSubmodule(GlobalID);
4366 
4367     switch (Unresolved.Kind) {
4368     case UnresolvedModuleRef::Conflict:
4369       if (ResolvedMod) {
4370         Module::Conflict Conflict;
4371         Conflict.Other = ResolvedMod;
4372         Conflict.Message = Unresolved.String.str();
4373         Unresolved.Mod->Conflicts.push_back(Conflict);
4374       }
4375       continue;
4376 
4377     case UnresolvedModuleRef::Import:
4378       if (ResolvedMod)
4379         Unresolved.Mod->Imports.insert(ResolvedMod);
4380       continue;
4381 
4382     case UnresolvedModuleRef::Export:
4383       if (ResolvedMod || Unresolved.IsWildcard)
4384         Unresolved.Mod->Exports.push_back(
4385           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4386       continue;
4387     }
4388   }
4389   UnresolvedModuleRefs.clear();
4390 
4391   if (Imported)
4392     Imported->append(ImportedModules.begin(),
4393                      ImportedModules.end());
4394 
4395   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4396   // Might be unnecessary as use declarations are only used to build the
4397   // module itself.
4398 
4399   if (ContextObj)
4400     InitializeContext();
4401 
4402   if (SemaObj)
4403     UpdateSema();
4404 
4405   if (DeserializationListener)
4406     DeserializationListener->ReaderInitialized(this);
4407 
4408   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4409   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4410     // If this AST file is a precompiled preamble, then set the
4411     // preamble file ID of the source manager to the file source file
4412     // from which the preamble was built.
4413     if (Type == MK_Preamble) {
4414       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4415     } else if (Type == MK_MainFile) {
4416       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4417     }
4418   }
4419 
4420   // For any Objective-C class definitions we have already loaded, make sure
4421   // that we load any additional categories.
4422   if (ContextObj) {
4423     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4424       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4425                          ObjCClassesLoaded[I],
4426                          PreviousGeneration);
4427     }
4428   }
4429 
4430   if (PP.getHeaderSearchInfo()
4431           .getHeaderSearchOpts()
4432           .ModulesValidateOncePerBuildSession) {
4433     // Now we are certain that the module and all modules it depends on are
4434     // up to date.  Create or update timestamp files for modules that are
4435     // located in the module cache (not for PCH files that could be anywhere
4436     // in the filesystem).
4437     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4438       ImportedModule &M = Loaded[I];
4439       if (M.Mod->Kind == MK_ImplicitModule) {
4440         updateModuleTimestamp(*M.Mod);
4441       }
4442     }
4443   }
4444 
4445   return Success;
4446 }
4447 
4448 static ASTFileSignature readASTFileSignature(StringRef PCH);
4449 
4450 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4451 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4452   // FIXME checking magic headers is done in other places such as
4453   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4454   // always done the same. Unify it all with a helper.
4455   if (!Stream.canSkipToPos(4))
4456     return llvm::createStringError(std::errc::illegal_byte_sequence,
4457                                    "file too small to contain AST file magic");
4458   for (unsigned C : {'C', 'P', 'C', 'H'})
4459     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4460       if (Res.get() != C)
4461         return llvm::createStringError(
4462             std::errc::illegal_byte_sequence,
4463             "file doesn't start with AST file magic");
4464     } else
4465       return Res.takeError();
4466   return llvm::Error::success();
4467 }
4468 
4469 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4470   switch (Kind) {
4471   case MK_PCH:
4472     return 0; // PCH
4473   case MK_ImplicitModule:
4474   case MK_ExplicitModule:
4475   case MK_PrebuiltModule:
4476     return 1; // module
4477   case MK_MainFile:
4478   case MK_Preamble:
4479     return 2; // main source file
4480   }
4481   llvm_unreachable("unknown module kind");
4482 }
4483 
4484 ASTReader::ASTReadResult
4485 ASTReader::ReadASTCore(StringRef FileName,
4486                        ModuleKind Type,
4487                        SourceLocation ImportLoc,
4488                        ModuleFile *ImportedBy,
4489                        SmallVectorImpl<ImportedModule> &Loaded,
4490                        off_t ExpectedSize, time_t ExpectedModTime,
4491                        ASTFileSignature ExpectedSignature,
4492                        unsigned ClientLoadCapabilities) {
4493   ModuleFile *M;
4494   std::string ErrorStr;
4495   ModuleManager::AddModuleResult AddResult
4496     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4497                           getGeneration(), ExpectedSize, ExpectedModTime,
4498                           ExpectedSignature, readASTFileSignature,
4499                           M, ErrorStr);
4500 
4501   switch (AddResult) {
4502   case ModuleManager::AlreadyLoaded:
4503     Diag(diag::remark_module_import)
4504         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4505         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4506     return Success;
4507 
4508   case ModuleManager::NewlyLoaded:
4509     // Load module file below.
4510     break;
4511 
4512   case ModuleManager::Missing:
4513     // The module file was missing; if the client can handle that, return
4514     // it.
4515     if (ClientLoadCapabilities & ARR_Missing)
4516       return Missing;
4517 
4518     // Otherwise, return an error.
4519     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4520                                           << FileName << !ErrorStr.empty()
4521                                           << ErrorStr;
4522     return Failure;
4523 
4524   case ModuleManager::OutOfDate:
4525     // We couldn't load the module file because it is out-of-date. If the
4526     // client can handle out-of-date, return it.
4527     if (ClientLoadCapabilities & ARR_OutOfDate)
4528       return OutOfDate;
4529 
4530     // Otherwise, return an error.
4531     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4532                                             << FileName << !ErrorStr.empty()
4533                                             << ErrorStr;
4534     return Failure;
4535   }
4536 
4537   assert(M && "Missing module file");
4538 
4539   bool ShouldFinalizePCM = false;
4540   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4541     auto &MC = getModuleManager().getModuleCache();
4542     if (ShouldFinalizePCM)
4543       MC.finalizePCM(FileName);
4544     else
4545       MC.tryToDropPCM(FileName);
4546   });
4547   ModuleFile &F = *M;
4548   BitstreamCursor &Stream = F.Stream;
4549   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4550   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4551 
4552   // Sniff for the signature.
4553   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4554     Diag(diag::err_module_file_invalid)
4555         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4556     return Failure;
4557   }
4558 
4559   // This is used for compatibility with older PCH formats.
4560   bool HaveReadControlBlock = false;
4561   while (true) {
4562     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4563     if (!MaybeEntry) {
4564       Error(MaybeEntry.takeError());
4565       return Failure;
4566     }
4567     llvm::BitstreamEntry Entry = MaybeEntry.get();
4568 
4569     switch (Entry.Kind) {
4570     case llvm::BitstreamEntry::Error:
4571     case llvm::BitstreamEntry::Record:
4572     case llvm::BitstreamEntry::EndBlock:
4573       Error("invalid record at top-level of AST file");
4574       return Failure;
4575 
4576     case llvm::BitstreamEntry::SubBlock:
4577       break;
4578     }
4579 
4580     switch (Entry.ID) {
4581     case CONTROL_BLOCK_ID:
4582       HaveReadControlBlock = true;
4583       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4584       case Success:
4585         // Check that we didn't try to load a non-module AST file as a module.
4586         //
4587         // FIXME: Should we also perform the converse check? Loading a module as
4588         // a PCH file sort of works, but it's a bit wonky.
4589         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4590              Type == MK_PrebuiltModule) &&
4591             F.ModuleName.empty()) {
4592           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4593           if (Result != OutOfDate ||
4594               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4595             Diag(diag::err_module_file_not_module) << FileName;
4596           return Result;
4597         }
4598         break;
4599 
4600       case Failure: return Failure;
4601       case Missing: return Missing;
4602       case OutOfDate: return OutOfDate;
4603       case VersionMismatch: return VersionMismatch;
4604       case ConfigurationMismatch: return ConfigurationMismatch;
4605       case HadErrors: return HadErrors;
4606       }
4607       break;
4608 
4609     case AST_BLOCK_ID:
4610       if (!HaveReadControlBlock) {
4611         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4612           Diag(diag::err_pch_version_too_old);
4613         return VersionMismatch;
4614       }
4615 
4616       // Record that we've loaded this module.
4617       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4618       ShouldFinalizePCM = true;
4619       return Success;
4620 
4621     case UNHASHED_CONTROL_BLOCK_ID:
4622       // This block is handled using look-ahead during ReadControlBlock.  We
4623       // shouldn't get here!
4624       Error("malformed block record in AST file");
4625       return Failure;
4626 
4627     default:
4628       if (llvm::Error Err = Stream.SkipBlock()) {
4629         Error(std::move(Err));
4630         return Failure;
4631       }
4632       break;
4633     }
4634   }
4635 
4636   llvm_unreachable("unexpected break; expected return");
4637 }
4638 
4639 ASTReader::ASTReadResult
4640 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4641                                     unsigned ClientLoadCapabilities) {
4642   const HeaderSearchOptions &HSOpts =
4643       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4644   bool AllowCompatibleConfigurationMismatch =
4645       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4646 
4647   ASTReadResult Result = readUnhashedControlBlockImpl(
4648       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4649       Listener.get(),
4650       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4651 
4652   // If F was directly imported by another module, it's implicitly validated by
4653   // the importing module.
4654   if (DisableValidation || WasImportedBy ||
4655       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4656     return Success;
4657 
4658   if (Result == Failure) {
4659     Error("malformed block record in AST file");
4660     return Failure;
4661   }
4662 
4663   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4664     // If this module has already been finalized in the ModuleCache, we're stuck
4665     // with it; we can only load a single version of each module.
4666     //
4667     // This can happen when a module is imported in two contexts: in one, as a
4668     // user module; in another, as a system module (due to an import from
4669     // another module marked with the [system] flag).  It usually indicates a
4670     // bug in the module map: this module should also be marked with [system].
4671     //
4672     // If -Wno-system-headers (the default), and the first import is as a
4673     // system module, then validation will fail during the as-user import,
4674     // since -Werror flags won't have been validated.  However, it's reasonable
4675     // to treat this consistently as a system module.
4676     //
4677     // If -Wsystem-headers, the PCM on disk was built with
4678     // -Wno-system-headers, and the first import is as a user module, then
4679     // validation will fail during the as-system import since the PCM on disk
4680     // doesn't guarantee that -Werror was respected.  However, the -Werror
4681     // flags were checked during the initial as-user import.
4682     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4683       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4684       return Success;
4685     }
4686   }
4687 
4688   return Result;
4689 }
4690 
4691 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4692     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4693     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4694     bool ValidateDiagnosticOptions) {
4695   // Initialize a stream.
4696   BitstreamCursor Stream(StreamData);
4697 
4698   // Sniff for the signature.
4699   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4700     // FIXME this drops the error on the floor.
4701     consumeError(std::move(Err));
4702     return Failure;
4703   }
4704 
4705   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4706   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4707     return Failure;
4708 
4709   // Read all of the records in the options block.
4710   RecordData Record;
4711   ASTReadResult Result = Success;
4712   while (true) {
4713     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4714     if (!MaybeEntry) {
4715       // FIXME this drops the error on the floor.
4716       consumeError(MaybeEntry.takeError());
4717       return Failure;
4718     }
4719     llvm::BitstreamEntry Entry = MaybeEntry.get();
4720 
4721     switch (Entry.Kind) {
4722     case llvm::BitstreamEntry::Error:
4723     case llvm::BitstreamEntry::SubBlock:
4724       return Failure;
4725 
4726     case llvm::BitstreamEntry::EndBlock:
4727       return Result;
4728 
4729     case llvm::BitstreamEntry::Record:
4730       // The interesting case.
4731       break;
4732     }
4733 
4734     // Read and process a record.
4735     Record.clear();
4736     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4737     if (!MaybeRecordType) {
4738       // FIXME this drops the error.
4739       return Failure;
4740     }
4741     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4742     case SIGNATURE:
4743       if (F)
4744         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4745       break;
4746     case AST_BLOCK_HASH:
4747       if (F)
4748         F->ASTBlockHash =
4749             ASTFileSignature::create(Record.begin(), Record.end());
4750       break;
4751     case DIAGNOSTIC_OPTIONS: {
4752       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4753       if (Listener && ValidateDiagnosticOptions &&
4754           !AllowCompatibleConfigurationMismatch &&
4755           ParseDiagnosticOptions(Record, Complain, *Listener))
4756         Result = OutOfDate; // Don't return early.  Read the signature.
4757       break;
4758     }
4759     case DIAG_PRAGMA_MAPPINGS:
4760       if (!F)
4761         break;
4762       if (F->PragmaDiagMappings.empty())
4763         F->PragmaDiagMappings.swap(Record);
4764       else
4765         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4766                                      Record.begin(), Record.end());
4767       break;
4768     }
4769   }
4770 }
4771 
4772 /// Parse a record and blob containing module file extension metadata.
4773 static bool parseModuleFileExtensionMetadata(
4774               const SmallVectorImpl<uint64_t> &Record,
4775               StringRef Blob,
4776               ModuleFileExtensionMetadata &Metadata) {
4777   if (Record.size() < 4) return true;
4778 
4779   Metadata.MajorVersion = Record[0];
4780   Metadata.MinorVersion = Record[1];
4781 
4782   unsigned BlockNameLen = Record[2];
4783   unsigned UserInfoLen = Record[3];
4784 
4785   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4786 
4787   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4788   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4789                                   Blob.data() + BlockNameLen + UserInfoLen);
4790   return false;
4791 }
4792 
4793 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4794   BitstreamCursor &Stream = F.Stream;
4795 
4796   RecordData Record;
4797   while (true) {
4798     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4799     if (!MaybeEntry) {
4800       Error(MaybeEntry.takeError());
4801       return Failure;
4802     }
4803     llvm::BitstreamEntry Entry = MaybeEntry.get();
4804 
4805     switch (Entry.Kind) {
4806     case llvm::BitstreamEntry::SubBlock:
4807       if (llvm::Error Err = Stream.SkipBlock()) {
4808         Error(std::move(Err));
4809         return Failure;
4810       }
4811       continue;
4812 
4813     case llvm::BitstreamEntry::EndBlock:
4814       return Success;
4815 
4816     case llvm::BitstreamEntry::Error:
4817       return HadErrors;
4818 
4819     case llvm::BitstreamEntry::Record:
4820       break;
4821     }
4822 
4823     Record.clear();
4824     StringRef Blob;
4825     Expected<unsigned> MaybeRecCode =
4826         Stream.readRecord(Entry.ID, Record, &Blob);
4827     if (!MaybeRecCode) {
4828       Error(MaybeRecCode.takeError());
4829       return Failure;
4830     }
4831     switch (MaybeRecCode.get()) {
4832     case EXTENSION_METADATA: {
4833       ModuleFileExtensionMetadata Metadata;
4834       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4835         Error("malformed EXTENSION_METADATA in AST file");
4836         return Failure;
4837       }
4838 
4839       // Find a module file extension with this block name.
4840       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4841       if (Known == ModuleFileExtensions.end()) break;
4842 
4843       // Form a reader.
4844       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4845                                                              F, Stream)) {
4846         F.ExtensionReaders.push_back(std::move(Reader));
4847       }
4848 
4849       break;
4850     }
4851     }
4852   }
4853 
4854   return Success;
4855 }
4856 
4857 void ASTReader::InitializeContext() {
4858   assert(ContextObj && "no context to initialize");
4859   ASTContext &Context = *ContextObj;
4860 
4861   // If there's a listener, notify them that we "read" the translation unit.
4862   if (DeserializationListener)
4863     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4864                                       Context.getTranslationUnitDecl());
4865 
4866   // FIXME: Find a better way to deal with collisions between these
4867   // built-in types. Right now, we just ignore the problem.
4868 
4869   // Load the special types.
4870   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4871     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4872       if (!Context.CFConstantStringTypeDecl)
4873         Context.setCFConstantStringType(GetType(String));
4874     }
4875 
4876     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4877       QualType FileType = GetType(File);
4878       if (FileType.isNull()) {
4879         Error("FILE type is NULL");
4880         return;
4881       }
4882 
4883       if (!Context.FILEDecl) {
4884         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4885           Context.setFILEDecl(Typedef->getDecl());
4886         else {
4887           const TagType *Tag = FileType->getAs<TagType>();
4888           if (!Tag) {
4889             Error("Invalid FILE type in AST file");
4890             return;
4891           }
4892           Context.setFILEDecl(Tag->getDecl());
4893         }
4894       }
4895     }
4896 
4897     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4898       QualType Jmp_bufType = GetType(Jmp_buf);
4899       if (Jmp_bufType.isNull()) {
4900         Error("jmp_buf type is NULL");
4901         return;
4902       }
4903 
4904       if (!Context.jmp_bufDecl) {
4905         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4906           Context.setjmp_bufDecl(Typedef->getDecl());
4907         else {
4908           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4909           if (!Tag) {
4910             Error("Invalid jmp_buf type in AST file");
4911             return;
4912           }
4913           Context.setjmp_bufDecl(Tag->getDecl());
4914         }
4915       }
4916     }
4917 
4918     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4919       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4920       if (Sigjmp_bufType.isNull()) {
4921         Error("sigjmp_buf type is NULL");
4922         return;
4923       }
4924 
4925       if (!Context.sigjmp_bufDecl) {
4926         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4927           Context.setsigjmp_bufDecl(Typedef->getDecl());
4928         else {
4929           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4930           assert(Tag && "Invalid sigjmp_buf type in AST file");
4931           Context.setsigjmp_bufDecl(Tag->getDecl());
4932         }
4933       }
4934     }
4935 
4936     if (unsigned ObjCIdRedef
4937           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4938       if (Context.ObjCIdRedefinitionType.isNull())
4939         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4940     }
4941 
4942     if (unsigned ObjCClassRedef
4943           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4944       if (Context.ObjCClassRedefinitionType.isNull())
4945         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4946     }
4947 
4948     if (unsigned ObjCSelRedef
4949           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4950       if (Context.ObjCSelRedefinitionType.isNull())
4951         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4952     }
4953 
4954     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4955       QualType Ucontext_tType = GetType(Ucontext_t);
4956       if (Ucontext_tType.isNull()) {
4957         Error("ucontext_t type is NULL");
4958         return;
4959       }
4960 
4961       if (!Context.ucontext_tDecl) {
4962         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4963           Context.setucontext_tDecl(Typedef->getDecl());
4964         else {
4965           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4966           assert(Tag && "Invalid ucontext_t type in AST file");
4967           Context.setucontext_tDecl(Tag->getDecl());
4968         }
4969       }
4970     }
4971   }
4972 
4973   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4974 
4975   // If there were any CUDA special declarations, deserialize them.
4976   if (!CUDASpecialDeclRefs.empty()) {
4977     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4978     Context.setcudaConfigureCallDecl(
4979                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4980   }
4981 
4982   // Re-export any modules that were imported by a non-module AST file.
4983   // FIXME: This does not make macro-only imports visible again.
4984   for (auto &Import : ImportedModules) {
4985     if (Module *Imported = getSubmodule(Import.ID)) {
4986       makeModuleVisible(Imported, Module::AllVisible,
4987                         /*ImportLoc=*/Import.ImportLoc);
4988       if (Import.ImportLoc.isValid())
4989         PP.makeModuleVisible(Imported, Import.ImportLoc);
4990       // FIXME: should we tell Sema to make the module visible too?
4991     }
4992   }
4993   ImportedModules.clear();
4994 }
4995 
4996 void ASTReader::finalizeForWriting() {
4997   // Nothing to do for now.
4998 }
4999 
5000 /// Reads and return the signature record from \p PCH's control block, or
5001 /// else returns 0.
5002 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5003   BitstreamCursor Stream(PCH);
5004   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5005     // FIXME this drops the error on the floor.
5006     consumeError(std::move(Err));
5007     return ASTFileSignature();
5008   }
5009 
5010   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5011   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5012     return ASTFileSignature();
5013 
5014   // Scan for SIGNATURE inside the diagnostic options block.
5015   ASTReader::RecordData Record;
5016   while (true) {
5017     Expected<llvm::BitstreamEntry> MaybeEntry =
5018         Stream.advanceSkippingSubblocks();
5019     if (!MaybeEntry) {
5020       // FIXME this drops the error on the floor.
5021       consumeError(MaybeEntry.takeError());
5022       return ASTFileSignature();
5023     }
5024     llvm::BitstreamEntry Entry = MaybeEntry.get();
5025 
5026     if (Entry.Kind != llvm::BitstreamEntry::Record)
5027       return ASTFileSignature();
5028 
5029     Record.clear();
5030     StringRef Blob;
5031     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5032     if (!MaybeRecord) {
5033       // FIXME this drops the error on the floor.
5034       consumeError(MaybeRecord.takeError());
5035       return ASTFileSignature();
5036     }
5037     if (SIGNATURE == MaybeRecord.get())
5038       return ASTFileSignature::create(Record.begin(),
5039                                       Record.begin() + ASTFileSignature::size);
5040   }
5041 }
5042 
5043 /// Retrieve the name of the original source file name
5044 /// directly from the AST file, without actually loading the AST
5045 /// file.
5046 std::string ASTReader::getOriginalSourceFile(
5047     const std::string &ASTFileName, FileManager &FileMgr,
5048     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5049   // Open the AST file.
5050   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5051   if (!Buffer) {
5052     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5053         << ASTFileName << Buffer.getError().message();
5054     return std::string();
5055   }
5056 
5057   // Initialize the stream
5058   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5059 
5060   // Sniff for the signature.
5061   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5062     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5063     return std::string();
5064   }
5065 
5066   // Scan for the CONTROL_BLOCK_ID block.
5067   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5068     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5069     return std::string();
5070   }
5071 
5072   // Scan for ORIGINAL_FILE inside the control block.
5073   RecordData Record;
5074   while (true) {
5075     Expected<llvm::BitstreamEntry> MaybeEntry =
5076         Stream.advanceSkippingSubblocks();
5077     if (!MaybeEntry) {
5078       // FIXME this drops errors on the floor.
5079       consumeError(MaybeEntry.takeError());
5080       return std::string();
5081     }
5082     llvm::BitstreamEntry Entry = MaybeEntry.get();
5083 
5084     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5085       return std::string();
5086 
5087     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5088       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5089       return std::string();
5090     }
5091 
5092     Record.clear();
5093     StringRef Blob;
5094     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5095     if (!MaybeRecord) {
5096       // FIXME this drops the errors on the floor.
5097       consumeError(MaybeRecord.takeError());
5098       return std::string();
5099     }
5100     if (ORIGINAL_FILE == MaybeRecord.get())
5101       return Blob.str();
5102   }
5103 }
5104 
5105 namespace {
5106 
5107   class SimplePCHValidator : public ASTReaderListener {
5108     const LangOptions &ExistingLangOpts;
5109     const TargetOptions &ExistingTargetOpts;
5110     const PreprocessorOptions &ExistingPPOpts;
5111     std::string ExistingModuleCachePath;
5112     FileManager &FileMgr;
5113 
5114   public:
5115     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5116                        const TargetOptions &ExistingTargetOpts,
5117                        const PreprocessorOptions &ExistingPPOpts,
5118                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5119         : ExistingLangOpts(ExistingLangOpts),
5120           ExistingTargetOpts(ExistingTargetOpts),
5121           ExistingPPOpts(ExistingPPOpts),
5122           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5123 
5124     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5125                              bool AllowCompatibleDifferences) override {
5126       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5127                                   AllowCompatibleDifferences);
5128     }
5129 
5130     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5131                            bool AllowCompatibleDifferences) override {
5132       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5133                                 AllowCompatibleDifferences);
5134     }
5135 
5136     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5137                                  StringRef SpecificModuleCachePath,
5138                                  bool Complain) override {
5139       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5140                                       ExistingModuleCachePath,
5141                                       nullptr, ExistingLangOpts);
5142     }
5143 
5144     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5145                                  bool Complain,
5146                                  std::string &SuggestedPredefines) override {
5147       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5148                                       SuggestedPredefines, ExistingLangOpts);
5149     }
5150   };
5151 
5152 } // namespace
5153 
5154 bool ASTReader::readASTFileControlBlock(
5155     StringRef Filename, FileManager &FileMgr,
5156     const PCHContainerReader &PCHContainerRdr,
5157     bool FindModuleFileExtensions,
5158     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5159   // Open the AST file.
5160   // FIXME: This allows use of the VFS; we do not allow use of the
5161   // VFS when actually loading a module.
5162   auto Buffer = FileMgr.getBufferForFile(Filename);
5163   if (!Buffer) {
5164     return true;
5165   }
5166 
5167   // Initialize the stream
5168   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5169   BitstreamCursor Stream(Bytes);
5170 
5171   // Sniff for the signature.
5172   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5173     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5174     return true;
5175   }
5176 
5177   // Scan for the CONTROL_BLOCK_ID block.
5178   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5179     return true;
5180 
5181   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5182   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5183   bool NeedsImports = Listener.needsImportVisitation();
5184   BitstreamCursor InputFilesCursor;
5185 
5186   RecordData Record;
5187   std::string ModuleDir;
5188   bool DoneWithControlBlock = false;
5189   while (!DoneWithControlBlock) {
5190     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5191     if (!MaybeEntry) {
5192       // FIXME this drops the error on the floor.
5193       consumeError(MaybeEntry.takeError());
5194       return true;
5195     }
5196     llvm::BitstreamEntry Entry = MaybeEntry.get();
5197 
5198     switch (Entry.Kind) {
5199     case llvm::BitstreamEntry::SubBlock: {
5200       switch (Entry.ID) {
5201       case OPTIONS_BLOCK_ID: {
5202         std::string IgnoredSuggestedPredefines;
5203         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5204                              /*AllowCompatibleConfigurationMismatch*/ false,
5205                              Listener, IgnoredSuggestedPredefines) != Success)
5206           return true;
5207         break;
5208       }
5209 
5210       case INPUT_FILES_BLOCK_ID:
5211         InputFilesCursor = Stream;
5212         if (llvm::Error Err = Stream.SkipBlock()) {
5213           // FIXME this drops the error on the floor.
5214           consumeError(std::move(Err));
5215           return true;
5216         }
5217         if (NeedsInputFiles &&
5218             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5219           return true;
5220         break;
5221 
5222       default:
5223         if (llvm::Error Err = Stream.SkipBlock()) {
5224           // FIXME this drops the error on the floor.
5225           consumeError(std::move(Err));
5226           return true;
5227         }
5228         break;
5229       }
5230 
5231       continue;
5232     }
5233 
5234     case llvm::BitstreamEntry::EndBlock:
5235       DoneWithControlBlock = true;
5236       break;
5237 
5238     case llvm::BitstreamEntry::Error:
5239       return true;
5240 
5241     case llvm::BitstreamEntry::Record:
5242       break;
5243     }
5244 
5245     if (DoneWithControlBlock) break;
5246 
5247     Record.clear();
5248     StringRef Blob;
5249     Expected<unsigned> MaybeRecCode =
5250         Stream.readRecord(Entry.ID, Record, &Blob);
5251     if (!MaybeRecCode) {
5252       // FIXME this drops the error.
5253       return Failure;
5254     }
5255     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5256     case METADATA:
5257       if (Record[0] != VERSION_MAJOR)
5258         return true;
5259       if (Listener.ReadFullVersionInformation(Blob))
5260         return true;
5261       break;
5262     case MODULE_NAME:
5263       Listener.ReadModuleName(Blob);
5264       break;
5265     case MODULE_DIRECTORY:
5266       ModuleDir = std::string(Blob);
5267       break;
5268     case MODULE_MAP_FILE: {
5269       unsigned Idx = 0;
5270       auto Path = ReadString(Record, Idx);
5271       ResolveImportedPath(Path, ModuleDir);
5272       Listener.ReadModuleMapFile(Path);
5273       break;
5274     }
5275     case INPUT_FILE_OFFSETS: {
5276       if (!NeedsInputFiles)
5277         break;
5278 
5279       unsigned NumInputFiles = Record[0];
5280       unsigned NumUserFiles = Record[1];
5281       const llvm::support::unaligned_uint64_t *InputFileOffs =
5282           (const llvm::support::unaligned_uint64_t *)Blob.data();
5283       for (unsigned I = 0; I != NumInputFiles; ++I) {
5284         // Go find this input file.
5285         bool isSystemFile = I >= NumUserFiles;
5286 
5287         if (isSystemFile && !NeedsSystemInputFiles)
5288           break; // the rest are system input files
5289 
5290         BitstreamCursor &Cursor = InputFilesCursor;
5291         SavedStreamPosition SavedPosition(Cursor);
5292         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5293           // FIXME this drops errors on the floor.
5294           consumeError(std::move(Err));
5295         }
5296 
5297         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5298         if (!MaybeCode) {
5299           // FIXME this drops errors on the floor.
5300           consumeError(MaybeCode.takeError());
5301         }
5302         unsigned Code = MaybeCode.get();
5303 
5304         RecordData Record;
5305         StringRef Blob;
5306         bool shouldContinue = false;
5307         Expected<unsigned> MaybeRecordType =
5308             Cursor.readRecord(Code, Record, &Blob);
5309         if (!MaybeRecordType) {
5310           // FIXME this drops errors on the floor.
5311           consumeError(MaybeRecordType.takeError());
5312         }
5313         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5314         case INPUT_FILE_HASH:
5315           break;
5316         case INPUT_FILE:
5317           bool Overridden = static_cast<bool>(Record[3]);
5318           std::string Filename = std::string(Blob);
5319           ResolveImportedPath(Filename, ModuleDir);
5320           shouldContinue = Listener.visitInputFile(
5321               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5322           break;
5323         }
5324         if (!shouldContinue)
5325           break;
5326       }
5327       break;
5328     }
5329 
5330     case IMPORTS: {
5331       if (!NeedsImports)
5332         break;
5333 
5334       unsigned Idx = 0, N = Record.size();
5335       while (Idx < N) {
5336         // Read information about the AST file.
5337         Idx +=
5338             1 + 1 + 1 + 1 +
5339             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5340         std::string ModuleName = ReadString(Record, Idx);
5341         std::string Filename = ReadString(Record, Idx);
5342         ResolveImportedPath(Filename, ModuleDir);
5343         Listener.visitImport(ModuleName, Filename);
5344       }
5345       break;
5346     }
5347 
5348     default:
5349       // No other validation to perform.
5350       break;
5351     }
5352   }
5353 
5354   // Look for module file extension blocks, if requested.
5355   if (FindModuleFileExtensions) {
5356     BitstreamCursor SavedStream = Stream;
5357     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5358       bool DoneWithExtensionBlock = false;
5359       while (!DoneWithExtensionBlock) {
5360         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5361         if (!MaybeEntry) {
5362           // FIXME this drops the error.
5363           return true;
5364         }
5365         llvm::BitstreamEntry Entry = MaybeEntry.get();
5366 
5367         switch (Entry.Kind) {
5368         case llvm::BitstreamEntry::SubBlock:
5369           if (llvm::Error Err = Stream.SkipBlock()) {
5370             // FIXME this drops the error on the floor.
5371             consumeError(std::move(Err));
5372             return true;
5373           }
5374           continue;
5375 
5376         case llvm::BitstreamEntry::EndBlock:
5377           DoneWithExtensionBlock = true;
5378           continue;
5379 
5380         case llvm::BitstreamEntry::Error:
5381           return true;
5382 
5383         case llvm::BitstreamEntry::Record:
5384           break;
5385         }
5386 
5387        Record.clear();
5388        StringRef Blob;
5389        Expected<unsigned> MaybeRecCode =
5390            Stream.readRecord(Entry.ID, Record, &Blob);
5391        if (!MaybeRecCode) {
5392          // FIXME this drops the error.
5393          return true;
5394        }
5395        switch (MaybeRecCode.get()) {
5396        case EXTENSION_METADATA: {
5397          ModuleFileExtensionMetadata Metadata;
5398          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5399            return true;
5400 
5401          Listener.readModuleFileExtension(Metadata);
5402          break;
5403        }
5404        }
5405       }
5406     }
5407     Stream = SavedStream;
5408   }
5409 
5410   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5411   if (readUnhashedControlBlockImpl(
5412           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5413           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5414           ValidateDiagnosticOptions) != Success)
5415     return true;
5416 
5417   return false;
5418 }
5419 
5420 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5421                                     const PCHContainerReader &PCHContainerRdr,
5422                                     const LangOptions &LangOpts,
5423                                     const TargetOptions &TargetOpts,
5424                                     const PreprocessorOptions &PPOpts,
5425                                     StringRef ExistingModuleCachePath) {
5426   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5427                                ExistingModuleCachePath, FileMgr);
5428   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5429                                   /*FindModuleFileExtensions=*/false,
5430                                   validator,
5431                                   /*ValidateDiagnosticOptions=*/true);
5432 }
5433 
5434 ASTReader::ASTReadResult
5435 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5436   // Enter the submodule block.
5437   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5438     Error(std::move(Err));
5439     return Failure;
5440   }
5441 
5442   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5443   bool First = true;
5444   Module *CurrentModule = nullptr;
5445   RecordData Record;
5446   while (true) {
5447     Expected<llvm::BitstreamEntry> MaybeEntry =
5448         F.Stream.advanceSkippingSubblocks();
5449     if (!MaybeEntry) {
5450       Error(MaybeEntry.takeError());
5451       return Failure;
5452     }
5453     llvm::BitstreamEntry Entry = MaybeEntry.get();
5454 
5455     switch (Entry.Kind) {
5456     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5457     case llvm::BitstreamEntry::Error:
5458       Error("malformed block record in AST file");
5459       return Failure;
5460     case llvm::BitstreamEntry::EndBlock:
5461       return Success;
5462     case llvm::BitstreamEntry::Record:
5463       // The interesting case.
5464       break;
5465     }
5466 
5467     // Read a record.
5468     StringRef Blob;
5469     Record.clear();
5470     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5471     if (!MaybeKind) {
5472       Error(MaybeKind.takeError());
5473       return Failure;
5474     }
5475     unsigned Kind = MaybeKind.get();
5476 
5477     if ((Kind == SUBMODULE_METADATA) != First) {
5478       Error("submodule metadata record should be at beginning of block");
5479       return Failure;
5480     }
5481     First = false;
5482 
5483     // Submodule information is only valid if we have a current module.
5484     // FIXME: Should we error on these cases?
5485     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5486         Kind != SUBMODULE_DEFINITION)
5487       continue;
5488 
5489     switch (Kind) {
5490     default:  // Default behavior: ignore.
5491       break;
5492 
5493     case SUBMODULE_DEFINITION: {
5494       if (Record.size() < 12) {
5495         Error("malformed module definition");
5496         return Failure;
5497       }
5498 
5499       StringRef Name = Blob;
5500       unsigned Idx = 0;
5501       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5502       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5503       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5504       bool IsFramework = Record[Idx++];
5505       bool IsExplicit = Record[Idx++];
5506       bool IsSystem = Record[Idx++];
5507       bool IsExternC = Record[Idx++];
5508       bool InferSubmodules = Record[Idx++];
5509       bool InferExplicitSubmodules = Record[Idx++];
5510       bool InferExportWildcard = Record[Idx++];
5511       bool ConfigMacrosExhaustive = Record[Idx++];
5512       bool ModuleMapIsPrivate = Record[Idx++];
5513 
5514       Module *ParentModule = nullptr;
5515       if (Parent)
5516         ParentModule = getSubmodule(Parent);
5517 
5518       // Retrieve this (sub)module from the module map, creating it if
5519       // necessary.
5520       CurrentModule =
5521           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5522               .first;
5523 
5524       // FIXME: set the definition loc for CurrentModule, or call
5525       // ModMap.setInferredModuleAllowedBy()
5526 
5527       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5528       if (GlobalIndex >= SubmodulesLoaded.size() ||
5529           SubmodulesLoaded[GlobalIndex]) {
5530         Error("too many submodules");
5531         return Failure;
5532       }
5533 
5534       if (!ParentModule) {
5535         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5536           // Don't emit module relocation error if we have -fno-validate-pch
5537           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5538               CurFile != F.File) {
5539             Error(diag::err_module_file_conflict,
5540                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5541                   F.File->getName());
5542             return Failure;
5543           }
5544         }
5545 
5546         F.DidReadTopLevelSubmodule = true;
5547         CurrentModule->setASTFile(F.File);
5548         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5549       }
5550 
5551       CurrentModule->Kind = Kind;
5552       CurrentModule->Signature = F.Signature;
5553       CurrentModule->IsFromModuleFile = true;
5554       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5555       CurrentModule->IsExternC = IsExternC;
5556       CurrentModule->InferSubmodules = InferSubmodules;
5557       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5558       CurrentModule->InferExportWildcard = InferExportWildcard;
5559       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5560       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5561       if (DeserializationListener)
5562         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5563 
5564       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5565 
5566       // Clear out data that will be replaced by what is in the module file.
5567       CurrentModule->LinkLibraries.clear();
5568       CurrentModule->ConfigMacros.clear();
5569       CurrentModule->UnresolvedConflicts.clear();
5570       CurrentModule->Conflicts.clear();
5571 
5572       // The module is available unless it's missing a requirement; relevant
5573       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5574       // Missing headers that were present when the module was built do not
5575       // make it unavailable -- if we got this far, this must be an explicitly
5576       // imported module file.
5577       CurrentModule->Requirements.clear();
5578       CurrentModule->MissingHeaders.clear();
5579       CurrentModule->IsUnimportable =
5580           ParentModule && ParentModule->IsUnimportable;
5581       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5582       break;
5583     }
5584 
5585     case SUBMODULE_UMBRELLA_HEADER: {
5586       std::string Filename = std::string(Blob);
5587       ResolveImportedPath(F, Filename);
5588       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5589         if (!CurrentModule->getUmbrellaHeader())
5590           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5591         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5592           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5593             Error("mismatched umbrella headers in submodule");
5594           return OutOfDate;
5595         }
5596       }
5597       break;
5598     }
5599 
5600     case SUBMODULE_HEADER:
5601     case SUBMODULE_EXCLUDED_HEADER:
5602     case SUBMODULE_PRIVATE_HEADER:
5603       // We lazily associate headers with their modules via the HeaderInfo table.
5604       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5605       // of complete filenames or remove it entirely.
5606       break;
5607 
5608     case SUBMODULE_TEXTUAL_HEADER:
5609     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5610       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5611       // them here.
5612       break;
5613 
5614     case SUBMODULE_TOPHEADER:
5615       CurrentModule->addTopHeaderFilename(Blob);
5616       break;
5617 
5618     case SUBMODULE_UMBRELLA_DIR: {
5619       std::string Dirname = std::string(Blob);
5620       ResolveImportedPath(F, Dirname);
5621       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5622         if (!CurrentModule->getUmbrellaDir())
5623           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5624         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5625           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5626             Error("mismatched umbrella directories in submodule");
5627           return OutOfDate;
5628         }
5629       }
5630       break;
5631     }
5632 
5633     case SUBMODULE_METADATA: {
5634       F.BaseSubmoduleID = getTotalNumSubmodules();
5635       F.LocalNumSubmodules = Record[0];
5636       unsigned LocalBaseSubmoduleID = Record[1];
5637       if (F.LocalNumSubmodules > 0) {
5638         // Introduce the global -> local mapping for submodules within this
5639         // module.
5640         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5641 
5642         // Introduce the local -> global mapping for submodules within this
5643         // module.
5644         F.SubmoduleRemap.insertOrReplace(
5645           std::make_pair(LocalBaseSubmoduleID,
5646                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5647 
5648         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5649       }
5650       break;
5651     }
5652 
5653     case SUBMODULE_IMPORTS:
5654       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5655         UnresolvedModuleRef Unresolved;
5656         Unresolved.File = &F;
5657         Unresolved.Mod = CurrentModule;
5658         Unresolved.ID = Record[Idx];
5659         Unresolved.Kind = UnresolvedModuleRef::Import;
5660         Unresolved.IsWildcard = false;
5661         UnresolvedModuleRefs.push_back(Unresolved);
5662       }
5663       break;
5664 
5665     case SUBMODULE_EXPORTS:
5666       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5667         UnresolvedModuleRef Unresolved;
5668         Unresolved.File = &F;
5669         Unresolved.Mod = CurrentModule;
5670         Unresolved.ID = Record[Idx];
5671         Unresolved.Kind = UnresolvedModuleRef::Export;
5672         Unresolved.IsWildcard = Record[Idx + 1];
5673         UnresolvedModuleRefs.push_back(Unresolved);
5674       }
5675 
5676       // Once we've loaded the set of exports, there's no reason to keep
5677       // the parsed, unresolved exports around.
5678       CurrentModule->UnresolvedExports.clear();
5679       break;
5680 
5681     case SUBMODULE_REQUIRES:
5682       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5683                                     PP.getTargetInfo());
5684       break;
5685 
5686     case SUBMODULE_LINK_LIBRARY:
5687       ModMap.resolveLinkAsDependencies(CurrentModule);
5688       CurrentModule->LinkLibraries.push_back(
5689           Module::LinkLibrary(std::string(Blob), Record[0]));
5690       break;
5691 
5692     case SUBMODULE_CONFIG_MACRO:
5693       CurrentModule->ConfigMacros.push_back(Blob.str());
5694       break;
5695 
5696     case SUBMODULE_CONFLICT: {
5697       UnresolvedModuleRef Unresolved;
5698       Unresolved.File = &F;
5699       Unresolved.Mod = CurrentModule;
5700       Unresolved.ID = Record[0];
5701       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5702       Unresolved.IsWildcard = false;
5703       Unresolved.String = Blob;
5704       UnresolvedModuleRefs.push_back(Unresolved);
5705       break;
5706     }
5707 
5708     case SUBMODULE_INITIALIZERS: {
5709       if (!ContextObj)
5710         break;
5711       SmallVector<uint32_t, 16> Inits;
5712       for (auto &ID : Record)
5713         Inits.push_back(getGlobalDeclID(F, ID));
5714       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5715       break;
5716     }
5717 
5718     case SUBMODULE_EXPORT_AS:
5719       CurrentModule->ExportAsModule = Blob.str();
5720       ModMap.addLinkAsDependency(CurrentModule);
5721       break;
5722     }
5723   }
5724 }
5725 
5726 /// Parse the record that corresponds to a LangOptions data
5727 /// structure.
5728 ///
5729 /// This routine parses the language options from the AST file and then gives
5730 /// them to the AST listener if one is set.
5731 ///
5732 /// \returns true if the listener deems the file unacceptable, false otherwise.
5733 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5734                                      bool Complain,
5735                                      ASTReaderListener &Listener,
5736                                      bool AllowCompatibleDifferences) {
5737   LangOptions LangOpts;
5738   unsigned Idx = 0;
5739 #define LANGOPT(Name, Bits, Default, Description) \
5740   LangOpts.Name = Record[Idx++];
5741 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5742   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5743 #include "clang/Basic/LangOptions.def"
5744 #define SANITIZER(NAME, ID)                                                    \
5745   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5746 #include "clang/Basic/Sanitizers.def"
5747 
5748   for (unsigned N = Record[Idx++]; N; --N)
5749     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5750 
5751   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5752   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5753   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5754 
5755   LangOpts.CurrentModule = ReadString(Record, Idx);
5756 
5757   // Comment options.
5758   for (unsigned N = Record[Idx++]; N; --N) {
5759     LangOpts.CommentOpts.BlockCommandNames.push_back(
5760       ReadString(Record, Idx));
5761   }
5762   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5763 
5764   // OpenMP offloading options.
5765   for (unsigned N = Record[Idx++]; N; --N) {
5766     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5767   }
5768 
5769   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5770 
5771   return Listener.ReadLanguageOptions(LangOpts, Complain,
5772                                       AllowCompatibleDifferences);
5773 }
5774 
5775 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5776                                    ASTReaderListener &Listener,
5777                                    bool AllowCompatibleDifferences) {
5778   unsigned Idx = 0;
5779   TargetOptions TargetOpts;
5780   TargetOpts.Triple = ReadString(Record, Idx);
5781   TargetOpts.CPU = ReadString(Record, Idx);
5782   TargetOpts.ABI = ReadString(Record, Idx);
5783   for (unsigned N = Record[Idx++]; N; --N) {
5784     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5785   }
5786   for (unsigned N = Record[Idx++]; N; --N) {
5787     TargetOpts.Features.push_back(ReadString(Record, Idx));
5788   }
5789 
5790   return Listener.ReadTargetOptions(TargetOpts, Complain,
5791                                     AllowCompatibleDifferences);
5792 }
5793 
5794 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5795                                        ASTReaderListener &Listener) {
5796   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5797   unsigned Idx = 0;
5798 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5799 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5800   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5801 #include "clang/Basic/DiagnosticOptions.def"
5802 
5803   for (unsigned N = Record[Idx++]; N; --N)
5804     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5805   for (unsigned N = Record[Idx++]; N; --N)
5806     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5807 
5808   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5809 }
5810 
5811 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5812                                        ASTReaderListener &Listener) {
5813   FileSystemOptions FSOpts;
5814   unsigned Idx = 0;
5815   FSOpts.WorkingDir = ReadString(Record, Idx);
5816   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5817 }
5818 
5819 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5820                                          bool Complain,
5821                                          ASTReaderListener &Listener) {
5822   HeaderSearchOptions HSOpts;
5823   unsigned Idx = 0;
5824   HSOpts.Sysroot = ReadString(Record, Idx);
5825 
5826   // Include entries.
5827   for (unsigned N = Record[Idx++]; N; --N) {
5828     std::string Path = ReadString(Record, Idx);
5829     frontend::IncludeDirGroup Group
5830       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5831     bool IsFramework = Record[Idx++];
5832     bool IgnoreSysRoot = Record[Idx++];
5833     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5834                                     IgnoreSysRoot);
5835   }
5836 
5837   // System header prefixes.
5838   for (unsigned N = Record[Idx++]; N; --N) {
5839     std::string Prefix = ReadString(Record, Idx);
5840     bool IsSystemHeader = Record[Idx++];
5841     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5842   }
5843 
5844   HSOpts.ResourceDir = ReadString(Record, Idx);
5845   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5846   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5847   HSOpts.DisableModuleHash = Record[Idx++];
5848   HSOpts.ImplicitModuleMaps = Record[Idx++];
5849   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5850   HSOpts.UseBuiltinIncludes = Record[Idx++];
5851   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5852   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5853   HSOpts.UseLibcxx = Record[Idx++];
5854   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5855 
5856   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5857                                           Complain);
5858 }
5859 
5860 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5861                                          bool Complain,
5862                                          ASTReaderListener &Listener,
5863                                          std::string &SuggestedPredefines) {
5864   PreprocessorOptions PPOpts;
5865   unsigned Idx = 0;
5866 
5867   // Macro definitions/undefs
5868   for (unsigned N = Record[Idx++]; N; --N) {
5869     std::string Macro = ReadString(Record, Idx);
5870     bool IsUndef = Record[Idx++];
5871     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5872   }
5873 
5874   // Includes
5875   for (unsigned N = Record[Idx++]; N; --N) {
5876     PPOpts.Includes.push_back(ReadString(Record, Idx));
5877   }
5878 
5879   // Macro Includes
5880   for (unsigned N = Record[Idx++]; N; --N) {
5881     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5882   }
5883 
5884   PPOpts.UsePredefines = Record[Idx++];
5885   PPOpts.DetailedRecord = Record[Idx++];
5886   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5887   PPOpts.ObjCXXARCStandardLibrary =
5888     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5889   SuggestedPredefines.clear();
5890   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5891                                           SuggestedPredefines);
5892 }
5893 
5894 std::pair<ModuleFile *, unsigned>
5895 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5896   GlobalPreprocessedEntityMapType::iterator
5897   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5898   assert(I != GlobalPreprocessedEntityMap.end() &&
5899          "Corrupted global preprocessed entity map");
5900   ModuleFile *M = I->second;
5901   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5902   return std::make_pair(M, LocalIndex);
5903 }
5904 
5905 llvm::iterator_range<PreprocessingRecord::iterator>
5906 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5907   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5908     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5909                                              Mod.NumPreprocessedEntities);
5910 
5911   return llvm::make_range(PreprocessingRecord::iterator(),
5912                           PreprocessingRecord::iterator());
5913 }
5914 
5915 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5916 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5917   return llvm::make_range(
5918       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5919       ModuleDeclIterator(this, &Mod,
5920                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5921 }
5922 
5923 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5924   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5925   assert(I != GlobalSkippedRangeMap.end() &&
5926     "Corrupted global skipped range map");
5927   ModuleFile *M = I->second;
5928   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5929   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5930   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5931   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5932                     TranslateSourceLocation(*M, RawRange.getEnd()));
5933   assert(Range.isValid());
5934   return Range;
5935 }
5936 
5937 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5938   PreprocessedEntityID PPID = Index+1;
5939   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5940   ModuleFile &M = *PPInfo.first;
5941   unsigned LocalIndex = PPInfo.second;
5942   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5943 
5944   if (!PP.getPreprocessingRecord()) {
5945     Error("no preprocessing record");
5946     return nullptr;
5947   }
5948 
5949   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5950   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5951           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5952     Error(std::move(Err));
5953     return nullptr;
5954   }
5955 
5956   Expected<llvm::BitstreamEntry> MaybeEntry =
5957       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5958   if (!MaybeEntry) {
5959     Error(MaybeEntry.takeError());
5960     return nullptr;
5961   }
5962   llvm::BitstreamEntry Entry = MaybeEntry.get();
5963 
5964   if (Entry.Kind != llvm::BitstreamEntry::Record)
5965     return nullptr;
5966 
5967   // Read the record.
5968   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5969                     TranslateSourceLocation(M, PPOffs.getEnd()));
5970   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5971   StringRef Blob;
5972   RecordData Record;
5973   Expected<unsigned> MaybeRecType =
5974       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5975   if (!MaybeRecType) {
5976     Error(MaybeRecType.takeError());
5977     return nullptr;
5978   }
5979   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5980   case PPD_MACRO_EXPANSION: {
5981     bool isBuiltin = Record[0];
5982     IdentifierInfo *Name = nullptr;
5983     MacroDefinitionRecord *Def = nullptr;
5984     if (isBuiltin)
5985       Name = getLocalIdentifier(M, Record[1]);
5986     else {
5987       PreprocessedEntityID GlobalID =
5988           getGlobalPreprocessedEntityID(M, Record[1]);
5989       Def = cast<MacroDefinitionRecord>(
5990           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5991     }
5992 
5993     MacroExpansion *ME;
5994     if (isBuiltin)
5995       ME = new (PPRec) MacroExpansion(Name, Range);
5996     else
5997       ME = new (PPRec) MacroExpansion(Def, Range);
5998 
5999     return ME;
6000   }
6001 
6002   case PPD_MACRO_DEFINITION: {
6003     // Decode the identifier info and then check again; if the macro is
6004     // still defined and associated with the identifier,
6005     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6006     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6007 
6008     if (DeserializationListener)
6009       DeserializationListener->MacroDefinitionRead(PPID, MD);
6010 
6011     return MD;
6012   }
6013 
6014   case PPD_INCLUSION_DIRECTIVE: {
6015     const char *FullFileNameStart = Blob.data() + Record[0];
6016     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6017     const FileEntry *File = nullptr;
6018     if (!FullFileName.empty())
6019       if (auto FE = PP.getFileManager().getFile(FullFileName))
6020         File = *FE;
6021 
6022     // FIXME: Stable encoding
6023     InclusionDirective::InclusionKind Kind
6024       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6025     InclusionDirective *ID
6026       = new (PPRec) InclusionDirective(PPRec, Kind,
6027                                        StringRef(Blob.data(), Record[0]),
6028                                        Record[1], Record[3],
6029                                        File,
6030                                        Range);
6031     return ID;
6032   }
6033   }
6034 
6035   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6036 }
6037 
6038 /// Find the next module that contains entities and return the ID
6039 /// of the first entry.
6040 ///
6041 /// \param SLocMapI points at a chunk of a module that contains no
6042 /// preprocessed entities or the entities it contains are not the ones we are
6043 /// looking for.
6044 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6045                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6046   ++SLocMapI;
6047   for (GlobalSLocOffsetMapType::const_iterator
6048          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6049     ModuleFile &M = *SLocMapI->second;
6050     if (M.NumPreprocessedEntities)
6051       return M.BasePreprocessedEntityID;
6052   }
6053 
6054   return getTotalNumPreprocessedEntities();
6055 }
6056 
6057 namespace {
6058 
6059 struct PPEntityComp {
6060   const ASTReader &Reader;
6061   ModuleFile &M;
6062 
6063   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6064 
6065   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6066     SourceLocation LHS = getLoc(L);
6067     SourceLocation RHS = getLoc(R);
6068     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6069   }
6070 
6071   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6072     SourceLocation LHS = getLoc(L);
6073     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6074   }
6075 
6076   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6077     SourceLocation RHS = getLoc(R);
6078     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6079   }
6080 
6081   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6082     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6083   }
6084 };
6085 
6086 } // namespace
6087 
6088 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6089                                                        bool EndsAfter) const {
6090   if (SourceMgr.isLocalSourceLocation(Loc))
6091     return getTotalNumPreprocessedEntities();
6092 
6093   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6094       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6095   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6096          "Corrupted global sloc offset map");
6097 
6098   if (SLocMapI->second->NumPreprocessedEntities == 0)
6099     return findNextPreprocessedEntity(SLocMapI);
6100 
6101   ModuleFile &M = *SLocMapI->second;
6102 
6103   using pp_iterator = const PPEntityOffset *;
6104 
6105   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6106   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6107 
6108   size_t Count = M.NumPreprocessedEntities;
6109   size_t Half;
6110   pp_iterator First = pp_begin;
6111   pp_iterator PPI;
6112 
6113   if (EndsAfter) {
6114     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6115                            PPEntityComp(*this, M));
6116   } else {
6117     // Do a binary search manually instead of using std::lower_bound because
6118     // The end locations of entities may be unordered (when a macro expansion
6119     // is inside another macro argument), but for this case it is not important
6120     // whether we get the first macro expansion or its containing macro.
6121     while (Count > 0) {
6122       Half = Count / 2;
6123       PPI = First;
6124       std::advance(PPI, Half);
6125       if (SourceMgr.isBeforeInTranslationUnit(
6126               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6127         First = PPI;
6128         ++First;
6129         Count = Count - Half - 1;
6130       } else
6131         Count = Half;
6132     }
6133   }
6134 
6135   if (PPI == pp_end)
6136     return findNextPreprocessedEntity(SLocMapI);
6137 
6138   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6139 }
6140 
6141 /// Returns a pair of [Begin, End) indices of preallocated
6142 /// preprocessed entities that \arg Range encompasses.
6143 std::pair<unsigned, unsigned>
6144     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6145   if (Range.isInvalid())
6146     return std::make_pair(0,0);
6147   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6148 
6149   PreprocessedEntityID BeginID =
6150       findPreprocessedEntity(Range.getBegin(), false);
6151   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6152   return std::make_pair(BeginID, EndID);
6153 }
6154 
6155 /// Optionally returns true or false if the preallocated preprocessed
6156 /// entity with index \arg Index came from file \arg FID.
6157 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6158                                                              FileID FID) {
6159   if (FID.isInvalid())
6160     return false;
6161 
6162   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6163   ModuleFile &M = *PPInfo.first;
6164   unsigned LocalIndex = PPInfo.second;
6165   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6166 
6167   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6168   if (Loc.isInvalid())
6169     return false;
6170 
6171   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6172     return true;
6173   else
6174     return false;
6175 }
6176 
6177 namespace {
6178 
6179   /// Visitor used to search for information about a header file.
6180   class HeaderFileInfoVisitor {
6181     const FileEntry *FE;
6182     Optional<HeaderFileInfo> HFI;
6183 
6184   public:
6185     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6186 
6187     bool operator()(ModuleFile &M) {
6188       HeaderFileInfoLookupTable *Table
6189         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6190       if (!Table)
6191         return false;
6192 
6193       // Look in the on-disk hash table for an entry for this file name.
6194       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6195       if (Pos == Table->end())
6196         return false;
6197 
6198       HFI = *Pos;
6199       return true;
6200     }
6201 
6202     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6203   };
6204 
6205 } // namespace
6206 
6207 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6208   HeaderFileInfoVisitor Visitor(FE);
6209   ModuleMgr.visit(Visitor);
6210   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6211     return *HFI;
6212 
6213   return HeaderFileInfo();
6214 }
6215 
6216 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6217   using DiagState = DiagnosticsEngine::DiagState;
6218   SmallVector<DiagState *, 32> DiagStates;
6219 
6220   for (ModuleFile &F : ModuleMgr) {
6221     unsigned Idx = 0;
6222     auto &Record = F.PragmaDiagMappings;
6223     if (Record.empty())
6224       continue;
6225 
6226     DiagStates.clear();
6227 
6228     auto ReadDiagState =
6229         [&](const DiagState &BasedOn, SourceLocation Loc,
6230             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6231       unsigned BackrefID = Record[Idx++];
6232       if (BackrefID != 0)
6233         return DiagStates[BackrefID - 1];
6234 
6235       // A new DiagState was created here.
6236       Diag.DiagStates.push_back(BasedOn);
6237       DiagState *NewState = &Diag.DiagStates.back();
6238       DiagStates.push_back(NewState);
6239       unsigned Size = Record[Idx++];
6240       assert(Idx + Size * 2 <= Record.size() &&
6241              "Invalid data, not enough diag/map pairs");
6242       while (Size--) {
6243         unsigned DiagID = Record[Idx++];
6244         DiagnosticMapping NewMapping =
6245             DiagnosticMapping::deserialize(Record[Idx++]);
6246         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6247           continue;
6248 
6249         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6250 
6251         // If this mapping was specified as a warning but the severity was
6252         // upgraded due to diagnostic settings, simulate the current diagnostic
6253         // settings (and use a warning).
6254         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6255           NewMapping.setSeverity(diag::Severity::Warning);
6256           NewMapping.setUpgradedFromWarning(false);
6257         }
6258 
6259         Mapping = NewMapping;
6260       }
6261       return NewState;
6262     };
6263 
6264     // Read the first state.
6265     DiagState *FirstState;
6266     if (F.Kind == MK_ImplicitModule) {
6267       // Implicitly-built modules are reused with different diagnostic
6268       // settings.  Use the initial diagnostic state from Diag to simulate this
6269       // compilation's diagnostic settings.
6270       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6271       DiagStates.push_back(FirstState);
6272 
6273       // Skip the initial diagnostic state from the serialized module.
6274       assert(Record[1] == 0 &&
6275              "Invalid data, unexpected backref in initial state");
6276       Idx = 3 + Record[2] * 2;
6277       assert(Idx < Record.size() &&
6278              "Invalid data, not enough state change pairs in initial state");
6279     } else if (F.isModule()) {
6280       // For an explicit module, preserve the flags from the module build
6281       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6282       // -Wblah flags.
6283       unsigned Flags = Record[Idx++];
6284       DiagState Initial;
6285       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6286       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6287       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6288       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6289       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6290       Initial.ExtBehavior = (diag::Severity)Flags;
6291       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6292 
6293       assert(F.OriginalSourceFileID.isValid());
6294 
6295       // Set up the root buffer of the module to start with the initial
6296       // diagnostic state of the module itself, to cover files that contain no
6297       // explicit transitions (for which we did not serialize anything).
6298       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6299           .StateTransitions.push_back({FirstState, 0});
6300     } else {
6301       // For prefix ASTs, start with whatever the user configured on the
6302       // command line.
6303       Idx++; // Skip flags.
6304       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6305                                  SourceLocation(), false);
6306     }
6307 
6308     // Read the state transitions.
6309     unsigned NumLocations = Record[Idx++];
6310     while (NumLocations--) {
6311       assert(Idx < Record.size() &&
6312              "Invalid data, missing pragma diagnostic states");
6313       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6314       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6315       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6316       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6317       unsigned Transitions = Record[Idx++];
6318 
6319       // Note that we don't need to set up Parent/ParentOffset here, because
6320       // we won't be changing the diagnostic state within imported FileIDs
6321       // (other than perhaps appending to the main source file, which has no
6322       // parent).
6323       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6324       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6325       for (unsigned I = 0; I != Transitions; ++I) {
6326         unsigned Offset = Record[Idx++];
6327         auto *State =
6328             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6329         F.StateTransitions.push_back({State, Offset});
6330       }
6331     }
6332 
6333     // Read the final state.
6334     assert(Idx < Record.size() &&
6335            "Invalid data, missing final pragma diagnostic state");
6336     SourceLocation CurStateLoc =
6337         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6338     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6339 
6340     if (!F.isModule()) {
6341       Diag.DiagStatesByLoc.CurDiagState = CurState;
6342       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6343 
6344       // Preserve the property that the imaginary root file describes the
6345       // current state.
6346       FileID NullFile;
6347       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6348       if (T.empty())
6349         T.push_back({CurState, 0});
6350       else
6351         T[0].State = CurState;
6352     }
6353 
6354     // Don't try to read these mappings again.
6355     Record.clear();
6356   }
6357 }
6358 
6359 /// Get the correct cursor and offset for loading a type.
6360 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6361   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6362   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6363   ModuleFile *M = I->second;
6364   return RecordLocation(
6365       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6366              M->DeclsBlockStartOffset);
6367 }
6368 
6369 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6370   switch (code) {
6371 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6372   case TYPE_##CODE_ID: return Type::CLASS_ID;
6373 #include "clang/Serialization/TypeBitCodes.def"
6374   default: return llvm::None;
6375   }
6376 }
6377 
6378 /// Read and return the type with the given index..
6379 ///
6380 /// The index is the type ID, shifted and minus the number of predefs. This
6381 /// routine actually reads the record corresponding to the type at the given
6382 /// location. It is a helper routine for GetType, which deals with reading type
6383 /// IDs.
6384 QualType ASTReader::readTypeRecord(unsigned Index) {
6385   assert(ContextObj && "reading type with no AST context");
6386   ASTContext &Context = *ContextObj;
6387   RecordLocation Loc = TypeCursorForIndex(Index);
6388   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6389 
6390   // Keep track of where we are in the stream, then jump back there
6391   // after reading this type.
6392   SavedStreamPosition SavedPosition(DeclsCursor);
6393 
6394   ReadingKindTracker ReadingKind(Read_Type, *this);
6395 
6396   // Note that we are loading a type record.
6397   Deserializing AType(this);
6398 
6399   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6400     Error(std::move(Err));
6401     return QualType();
6402   }
6403   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6404   if (!RawCode) {
6405     Error(RawCode.takeError());
6406     return QualType();
6407   }
6408 
6409   ASTRecordReader Record(*this, *Loc.F);
6410   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6411   if (!Code) {
6412     Error(Code.takeError());
6413     return QualType();
6414   }
6415   if (Code.get() == TYPE_EXT_QUAL) {
6416     QualType baseType = Record.readQualType();
6417     Qualifiers quals = Record.readQualifiers();
6418     return Context.getQualifiedType(baseType, quals);
6419   }
6420 
6421   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6422   if (!maybeClass) {
6423     Error("Unexpected code for type");
6424     return QualType();
6425   }
6426 
6427   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6428   return TypeReader.read(*maybeClass);
6429 }
6430 
6431 namespace clang {
6432 
6433 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6434   ASTRecordReader &Reader;
6435 
6436   SourceLocation readSourceLocation() {
6437     return Reader.readSourceLocation();
6438   }
6439 
6440   TypeSourceInfo *GetTypeSourceInfo() {
6441     return Reader.readTypeSourceInfo();
6442   }
6443 
6444   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6445     return Reader.readNestedNameSpecifierLoc();
6446   }
6447 
6448   Attr *ReadAttr() {
6449     return Reader.readAttr();
6450   }
6451 
6452 public:
6453   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6454 
6455   // We want compile-time assurance that we've enumerated all of
6456   // these, so unfortunately we have to declare them first, then
6457   // define them out-of-line.
6458 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6459 #define TYPELOC(CLASS, PARENT) \
6460   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6461 #include "clang/AST/TypeLocNodes.def"
6462 
6463   void VisitFunctionTypeLoc(FunctionTypeLoc);
6464   void VisitArrayTypeLoc(ArrayTypeLoc);
6465 };
6466 
6467 } // namespace clang
6468 
6469 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6470   // nothing to do
6471 }
6472 
6473 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6474   TL.setBuiltinLoc(readSourceLocation());
6475   if (TL.needsExtraLocalData()) {
6476     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6477     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6478     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6479     TL.setModeAttr(Reader.readInt());
6480   }
6481 }
6482 
6483 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6484   TL.setNameLoc(readSourceLocation());
6485 }
6486 
6487 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6488   TL.setStarLoc(readSourceLocation());
6489 }
6490 
6491 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6492   // nothing to do
6493 }
6494 
6495 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6496   // nothing to do
6497 }
6498 
6499 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6500   TL.setExpansionLoc(readSourceLocation());
6501 }
6502 
6503 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6504   TL.setCaretLoc(readSourceLocation());
6505 }
6506 
6507 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6508   TL.setAmpLoc(readSourceLocation());
6509 }
6510 
6511 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6512   TL.setAmpAmpLoc(readSourceLocation());
6513 }
6514 
6515 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6516   TL.setStarLoc(readSourceLocation());
6517   TL.setClassTInfo(GetTypeSourceInfo());
6518 }
6519 
6520 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6521   TL.setLBracketLoc(readSourceLocation());
6522   TL.setRBracketLoc(readSourceLocation());
6523   if (Reader.readBool())
6524     TL.setSizeExpr(Reader.readExpr());
6525   else
6526     TL.setSizeExpr(nullptr);
6527 }
6528 
6529 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6530   VisitArrayTypeLoc(TL);
6531 }
6532 
6533 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6534   VisitArrayTypeLoc(TL);
6535 }
6536 
6537 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6538   VisitArrayTypeLoc(TL);
6539 }
6540 
6541 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6542                                             DependentSizedArrayTypeLoc TL) {
6543   VisitArrayTypeLoc(TL);
6544 }
6545 
6546 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6547     DependentAddressSpaceTypeLoc TL) {
6548 
6549     TL.setAttrNameLoc(readSourceLocation());
6550     TL.setAttrOperandParensRange(Reader.readSourceRange());
6551     TL.setAttrExprOperand(Reader.readExpr());
6552 }
6553 
6554 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6555                                         DependentSizedExtVectorTypeLoc TL) {
6556   TL.setNameLoc(readSourceLocation());
6557 }
6558 
6559 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6560   TL.setNameLoc(readSourceLocation());
6561 }
6562 
6563 void TypeLocReader::VisitDependentVectorTypeLoc(
6564     DependentVectorTypeLoc TL) {
6565   TL.setNameLoc(readSourceLocation());
6566 }
6567 
6568 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6569   TL.setNameLoc(readSourceLocation());
6570 }
6571 
6572 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6573   TL.setAttrNameLoc(readSourceLocation());
6574   TL.setAttrOperandParensRange(Reader.readSourceRange());
6575   TL.setAttrRowOperand(Reader.readExpr());
6576   TL.setAttrColumnOperand(Reader.readExpr());
6577 }
6578 
6579 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6580     DependentSizedMatrixTypeLoc TL) {
6581   TL.setAttrNameLoc(readSourceLocation());
6582   TL.setAttrOperandParensRange(Reader.readSourceRange());
6583   TL.setAttrRowOperand(Reader.readExpr());
6584   TL.setAttrColumnOperand(Reader.readExpr());
6585 }
6586 
6587 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6588   TL.setLocalRangeBegin(readSourceLocation());
6589   TL.setLParenLoc(readSourceLocation());
6590   TL.setRParenLoc(readSourceLocation());
6591   TL.setExceptionSpecRange(Reader.readSourceRange());
6592   TL.setLocalRangeEnd(readSourceLocation());
6593   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6594     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6595   }
6596 }
6597 
6598 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6599   VisitFunctionTypeLoc(TL);
6600 }
6601 
6602 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6603   VisitFunctionTypeLoc(TL);
6604 }
6605 
6606 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6607   TL.setNameLoc(readSourceLocation());
6608 }
6609 
6610 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6611   TL.setNameLoc(readSourceLocation());
6612 }
6613 
6614 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6615   TL.setTypeofLoc(readSourceLocation());
6616   TL.setLParenLoc(readSourceLocation());
6617   TL.setRParenLoc(readSourceLocation());
6618 }
6619 
6620 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6621   TL.setTypeofLoc(readSourceLocation());
6622   TL.setLParenLoc(readSourceLocation());
6623   TL.setRParenLoc(readSourceLocation());
6624   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6625 }
6626 
6627 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6628   TL.setNameLoc(readSourceLocation());
6629 }
6630 
6631 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6632   TL.setKWLoc(readSourceLocation());
6633   TL.setLParenLoc(readSourceLocation());
6634   TL.setRParenLoc(readSourceLocation());
6635   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6636 }
6637 
6638 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6639   TL.setNameLoc(readSourceLocation());
6640   if (Reader.readBool()) {
6641     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6642     TL.setTemplateKWLoc(readSourceLocation());
6643     TL.setConceptNameLoc(readSourceLocation());
6644     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6645     TL.setLAngleLoc(readSourceLocation());
6646     TL.setRAngleLoc(readSourceLocation());
6647     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6648       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6649                               TL.getTypePtr()->getArg(i).getKind()));
6650   }
6651 }
6652 
6653 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6654     DeducedTemplateSpecializationTypeLoc TL) {
6655   TL.setTemplateNameLoc(readSourceLocation());
6656 }
6657 
6658 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6659   TL.setNameLoc(readSourceLocation());
6660 }
6661 
6662 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6663   TL.setNameLoc(readSourceLocation());
6664 }
6665 
6666 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6667   TL.setAttr(ReadAttr());
6668 }
6669 
6670 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6671   TL.setNameLoc(readSourceLocation());
6672 }
6673 
6674 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6675                                             SubstTemplateTypeParmTypeLoc TL) {
6676   TL.setNameLoc(readSourceLocation());
6677 }
6678 
6679 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6680                                           SubstTemplateTypeParmPackTypeLoc TL) {
6681   TL.setNameLoc(readSourceLocation());
6682 }
6683 
6684 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6685                                            TemplateSpecializationTypeLoc TL) {
6686   TL.setTemplateKeywordLoc(readSourceLocation());
6687   TL.setTemplateNameLoc(readSourceLocation());
6688   TL.setLAngleLoc(readSourceLocation());
6689   TL.setRAngleLoc(readSourceLocation());
6690   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6691     TL.setArgLocInfo(
6692         i,
6693         Reader.readTemplateArgumentLocInfo(
6694           TL.getTypePtr()->getArg(i).getKind()));
6695 }
6696 
6697 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6698   TL.setLParenLoc(readSourceLocation());
6699   TL.setRParenLoc(readSourceLocation());
6700 }
6701 
6702 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6703   TL.setElaboratedKeywordLoc(readSourceLocation());
6704   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6705 }
6706 
6707 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6708   TL.setNameLoc(readSourceLocation());
6709 }
6710 
6711 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6712   TL.setElaboratedKeywordLoc(readSourceLocation());
6713   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6714   TL.setNameLoc(readSourceLocation());
6715 }
6716 
6717 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6718        DependentTemplateSpecializationTypeLoc TL) {
6719   TL.setElaboratedKeywordLoc(readSourceLocation());
6720   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6721   TL.setTemplateKeywordLoc(readSourceLocation());
6722   TL.setTemplateNameLoc(readSourceLocation());
6723   TL.setLAngleLoc(readSourceLocation());
6724   TL.setRAngleLoc(readSourceLocation());
6725   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6726     TL.setArgLocInfo(
6727         I,
6728         Reader.readTemplateArgumentLocInfo(
6729             TL.getTypePtr()->getArg(I).getKind()));
6730 }
6731 
6732 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6733   TL.setEllipsisLoc(readSourceLocation());
6734 }
6735 
6736 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6737   TL.setNameLoc(readSourceLocation());
6738 }
6739 
6740 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6741   if (TL.getNumProtocols()) {
6742     TL.setProtocolLAngleLoc(readSourceLocation());
6743     TL.setProtocolRAngleLoc(readSourceLocation());
6744   }
6745   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6746     TL.setProtocolLoc(i, readSourceLocation());
6747 }
6748 
6749 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6750   TL.setHasBaseTypeAsWritten(Reader.readBool());
6751   TL.setTypeArgsLAngleLoc(readSourceLocation());
6752   TL.setTypeArgsRAngleLoc(readSourceLocation());
6753   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6754     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6755   TL.setProtocolLAngleLoc(readSourceLocation());
6756   TL.setProtocolRAngleLoc(readSourceLocation());
6757   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6758     TL.setProtocolLoc(i, readSourceLocation());
6759 }
6760 
6761 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6762   TL.setStarLoc(readSourceLocation());
6763 }
6764 
6765 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6766   TL.setKWLoc(readSourceLocation());
6767   TL.setLParenLoc(readSourceLocation());
6768   TL.setRParenLoc(readSourceLocation());
6769 }
6770 
6771 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6772   TL.setKWLoc(readSourceLocation());
6773 }
6774 
6775 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6776   TL.setNameLoc(readSourceLocation());
6777 }
6778 void TypeLocReader::VisitDependentExtIntTypeLoc(
6779     clang::DependentExtIntTypeLoc TL) {
6780   TL.setNameLoc(readSourceLocation());
6781 }
6782 
6783 
6784 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6785   TypeLocReader TLR(*this);
6786   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6787     TLR.Visit(TL);
6788 }
6789 
6790 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6791   QualType InfoTy = readType();
6792   if (InfoTy.isNull())
6793     return nullptr;
6794 
6795   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6796   readTypeLoc(TInfo->getTypeLoc());
6797   return TInfo;
6798 }
6799 
6800 QualType ASTReader::GetType(TypeID ID) {
6801   assert(ContextObj && "reading type with no AST context");
6802   ASTContext &Context = *ContextObj;
6803 
6804   unsigned FastQuals = ID & Qualifiers::FastMask;
6805   unsigned Index = ID >> Qualifiers::FastWidth;
6806 
6807   if (Index < NUM_PREDEF_TYPE_IDS) {
6808     QualType T;
6809     switch ((PredefinedTypeIDs)Index) {
6810     case PREDEF_TYPE_NULL_ID:
6811       return QualType();
6812     case PREDEF_TYPE_VOID_ID:
6813       T = Context.VoidTy;
6814       break;
6815     case PREDEF_TYPE_BOOL_ID:
6816       T = Context.BoolTy;
6817       break;
6818     case PREDEF_TYPE_CHAR_U_ID:
6819     case PREDEF_TYPE_CHAR_S_ID:
6820       // FIXME: Check that the signedness of CharTy is correct!
6821       T = Context.CharTy;
6822       break;
6823     case PREDEF_TYPE_UCHAR_ID:
6824       T = Context.UnsignedCharTy;
6825       break;
6826     case PREDEF_TYPE_USHORT_ID:
6827       T = Context.UnsignedShortTy;
6828       break;
6829     case PREDEF_TYPE_UINT_ID:
6830       T = Context.UnsignedIntTy;
6831       break;
6832     case PREDEF_TYPE_ULONG_ID:
6833       T = Context.UnsignedLongTy;
6834       break;
6835     case PREDEF_TYPE_ULONGLONG_ID:
6836       T = Context.UnsignedLongLongTy;
6837       break;
6838     case PREDEF_TYPE_UINT128_ID:
6839       T = Context.UnsignedInt128Ty;
6840       break;
6841     case PREDEF_TYPE_SCHAR_ID:
6842       T = Context.SignedCharTy;
6843       break;
6844     case PREDEF_TYPE_WCHAR_ID:
6845       T = Context.WCharTy;
6846       break;
6847     case PREDEF_TYPE_SHORT_ID:
6848       T = Context.ShortTy;
6849       break;
6850     case PREDEF_TYPE_INT_ID:
6851       T = Context.IntTy;
6852       break;
6853     case PREDEF_TYPE_LONG_ID:
6854       T = Context.LongTy;
6855       break;
6856     case PREDEF_TYPE_LONGLONG_ID:
6857       T = Context.LongLongTy;
6858       break;
6859     case PREDEF_TYPE_INT128_ID:
6860       T = Context.Int128Ty;
6861       break;
6862     case PREDEF_TYPE_BFLOAT16_ID:
6863       T = Context.BFloat16Ty;
6864       break;
6865     case PREDEF_TYPE_HALF_ID:
6866       T = Context.HalfTy;
6867       break;
6868     case PREDEF_TYPE_FLOAT_ID:
6869       T = Context.FloatTy;
6870       break;
6871     case PREDEF_TYPE_DOUBLE_ID:
6872       T = Context.DoubleTy;
6873       break;
6874     case PREDEF_TYPE_LONGDOUBLE_ID:
6875       T = Context.LongDoubleTy;
6876       break;
6877     case PREDEF_TYPE_SHORT_ACCUM_ID:
6878       T = Context.ShortAccumTy;
6879       break;
6880     case PREDEF_TYPE_ACCUM_ID:
6881       T = Context.AccumTy;
6882       break;
6883     case PREDEF_TYPE_LONG_ACCUM_ID:
6884       T = Context.LongAccumTy;
6885       break;
6886     case PREDEF_TYPE_USHORT_ACCUM_ID:
6887       T = Context.UnsignedShortAccumTy;
6888       break;
6889     case PREDEF_TYPE_UACCUM_ID:
6890       T = Context.UnsignedAccumTy;
6891       break;
6892     case PREDEF_TYPE_ULONG_ACCUM_ID:
6893       T = Context.UnsignedLongAccumTy;
6894       break;
6895     case PREDEF_TYPE_SHORT_FRACT_ID:
6896       T = Context.ShortFractTy;
6897       break;
6898     case PREDEF_TYPE_FRACT_ID:
6899       T = Context.FractTy;
6900       break;
6901     case PREDEF_TYPE_LONG_FRACT_ID:
6902       T = Context.LongFractTy;
6903       break;
6904     case PREDEF_TYPE_USHORT_FRACT_ID:
6905       T = Context.UnsignedShortFractTy;
6906       break;
6907     case PREDEF_TYPE_UFRACT_ID:
6908       T = Context.UnsignedFractTy;
6909       break;
6910     case PREDEF_TYPE_ULONG_FRACT_ID:
6911       T = Context.UnsignedLongFractTy;
6912       break;
6913     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6914       T = Context.SatShortAccumTy;
6915       break;
6916     case PREDEF_TYPE_SAT_ACCUM_ID:
6917       T = Context.SatAccumTy;
6918       break;
6919     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6920       T = Context.SatLongAccumTy;
6921       break;
6922     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6923       T = Context.SatUnsignedShortAccumTy;
6924       break;
6925     case PREDEF_TYPE_SAT_UACCUM_ID:
6926       T = Context.SatUnsignedAccumTy;
6927       break;
6928     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6929       T = Context.SatUnsignedLongAccumTy;
6930       break;
6931     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6932       T = Context.SatShortFractTy;
6933       break;
6934     case PREDEF_TYPE_SAT_FRACT_ID:
6935       T = Context.SatFractTy;
6936       break;
6937     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6938       T = Context.SatLongFractTy;
6939       break;
6940     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6941       T = Context.SatUnsignedShortFractTy;
6942       break;
6943     case PREDEF_TYPE_SAT_UFRACT_ID:
6944       T = Context.SatUnsignedFractTy;
6945       break;
6946     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6947       T = Context.SatUnsignedLongFractTy;
6948       break;
6949     case PREDEF_TYPE_FLOAT16_ID:
6950       T = Context.Float16Ty;
6951       break;
6952     case PREDEF_TYPE_FLOAT128_ID:
6953       T = Context.Float128Ty;
6954       break;
6955     case PREDEF_TYPE_OVERLOAD_ID:
6956       T = Context.OverloadTy;
6957       break;
6958     case PREDEF_TYPE_BOUND_MEMBER:
6959       T = Context.BoundMemberTy;
6960       break;
6961     case PREDEF_TYPE_PSEUDO_OBJECT:
6962       T = Context.PseudoObjectTy;
6963       break;
6964     case PREDEF_TYPE_DEPENDENT_ID:
6965       T = Context.DependentTy;
6966       break;
6967     case PREDEF_TYPE_UNKNOWN_ANY:
6968       T = Context.UnknownAnyTy;
6969       break;
6970     case PREDEF_TYPE_NULLPTR_ID:
6971       T = Context.NullPtrTy;
6972       break;
6973     case PREDEF_TYPE_CHAR8_ID:
6974       T = Context.Char8Ty;
6975       break;
6976     case PREDEF_TYPE_CHAR16_ID:
6977       T = Context.Char16Ty;
6978       break;
6979     case PREDEF_TYPE_CHAR32_ID:
6980       T = Context.Char32Ty;
6981       break;
6982     case PREDEF_TYPE_OBJC_ID:
6983       T = Context.ObjCBuiltinIdTy;
6984       break;
6985     case PREDEF_TYPE_OBJC_CLASS:
6986       T = Context.ObjCBuiltinClassTy;
6987       break;
6988     case PREDEF_TYPE_OBJC_SEL:
6989       T = Context.ObjCBuiltinSelTy;
6990       break;
6991 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6992     case PREDEF_TYPE_##Id##_ID: \
6993       T = Context.SingletonId; \
6994       break;
6995 #include "clang/Basic/OpenCLImageTypes.def"
6996 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6997     case PREDEF_TYPE_##Id##_ID: \
6998       T = Context.Id##Ty; \
6999       break;
7000 #include "clang/Basic/OpenCLExtensionTypes.def"
7001     case PREDEF_TYPE_SAMPLER_ID:
7002       T = Context.OCLSamplerTy;
7003       break;
7004     case PREDEF_TYPE_EVENT_ID:
7005       T = Context.OCLEventTy;
7006       break;
7007     case PREDEF_TYPE_CLK_EVENT_ID:
7008       T = Context.OCLClkEventTy;
7009       break;
7010     case PREDEF_TYPE_QUEUE_ID:
7011       T = Context.OCLQueueTy;
7012       break;
7013     case PREDEF_TYPE_RESERVE_ID_ID:
7014       T = Context.OCLReserveIDTy;
7015       break;
7016     case PREDEF_TYPE_AUTO_DEDUCT:
7017       T = Context.getAutoDeductType();
7018       break;
7019     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7020       T = Context.getAutoRRefDeductType();
7021       break;
7022     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7023       T = Context.ARCUnbridgedCastTy;
7024       break;
7025     case PREDEF_TYPE_BUILTIN_FN:
7026       T = Context.BuiltinFnTy;
7027       break;
7028     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7029       T = Context.IncompleteMatrixIdxTy;
7030       break;
7031     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7032       T = Context.OMPArraySectionTy;
7033       break;
7034     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7035       T = Context.OMPArraySectionTy;
7036       break;
7037     case PREDEF_TYPE_OMP_ITERATOR:
7038       T = Context.OMPIteratorTy;
7039       break;
7040 #define SVE_TYPE(Name, Id, SingletonId) \
7041     case PREDEF_TYPE_##Id##_ID: \
7042       T = Context.SingletonId; \
7043       break;
7044 #include "clang/Basic/AArch64SVEACLETypes.def"
7045     }
7046 
7047     assert(!T.isNull() && "Unknown predefined type");
7048     return T.withFastQualifiers(FastQuals);
7049   }
7050 
7051   Index -= NUM_PREDEF_TYPE_IDS;
7052   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7053   if (TypesLoaded[Index].isNull()) {
7054     TypesLoaded[Index] = readTypeRecord(Index);
7055     if (TypesLoaded[Index].isNull())
7056       return QualType();
7057 
7058     TypesLoaded[Index]->setFromAST();
7059     if (DeserializationListener)
7060       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7061                                         TypesLoaded[Index]);
7062   }
7063 
7064   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7065 }
7066 
7067 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7068   return GetType(getGlobalTypeID(F, LocalID));
7069 }
7070 
7071 serialization::TypeID
7072 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7073   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7074   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7075 
7076   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7077     return LocalID;
7078 
7079   if (!F.ModuleOffsetMap.empty())
7080     ReadModuleOffsetMap(F);
7081 
7082   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7083     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7084   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7085 
7086   unsigned GlobalIndex = LocalIndex + I->second;
7087   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7088 }
7089 
7090 TemplateArgumentLocInfo
7091 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7092   switch (Kind) {
7093   case TemplateArgument::Expression:
7094     return readExpr();
7095   case TemplateArgument::Type:
7096     return readTypeSourceInfo();
7097   case TemplateArgument::Template: {
7098     NestedNameSpecifierLoc QualifierLoc =
7099       readNestedNameSpecifierLoc();
7100     SourceLocation TemplateNameLoc = readSourceLocation();
7101     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7102                                    SourceLocation());
7103   }
7104   case TemplateArgument::TemplateExpansion: {
7105     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7106     SourceLocation TemplateNameLoc = readSourceLocation();
7107     SourceLocation EllipsisLoc = readSourceLocation();
7108     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7109                                    EllipsisLoc);
7110   }
7111   case TemplateArgument::Null:
7112   case TemplateArgument::Integral:
7113   case TemplateArgument::Declaration:
7114   case TemplateArgument::NullPtr:
7115   case TemplateArgument::Pack:
7116     // FIXME: Is this right?
7117     return TemplateArgumentLocInfo();
7118   }
7119   llvm_unreachable("unexpected template argument loc");
7120 }
7121 
7122 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7123   TemplateArgument Arg = readTemplateArgument();
7124 
7125   if (Arg.getKind() == TemplateArgument::Expression) {
7126     if (readBool()) // bool InfoHasSameExpr.
7127       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7128   }
7129   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7130 }
7131 
7132 const ASTTemplateArgumentListInfo *
7133 ASTRecordReader::readASTTemplateArgumentListInfo() {
7134   SourceLocation LAngleLoc = readSourceLocation();
7135   SourceLocation RAngleLoc = readSourceLocation();
7136   unsigned NumArgsAsWritten = readInt();
7137   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7138   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7139     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7140   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7141 }
7142 
7143 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7144   return GetDecl(ID);
7145 }
7146 
7147 void ASTReader::CompleteRedeclChain(const Decl *D) {
7148   if (NumCurrentElementsDeserializing) {
7149     // We arrange to not care about the complete redeclaration chain while we're
7150     // deserializing. Just remember that the AST has marked this one as complete
7151     // but that it's not actually complete yet, so we know we still need to
7152     // complete it later.
7153     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7154     return;
7155   }
7156 
7157   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7158 
7159   // If this is a named declaration, complete it by looking it up
7160   // within its context.
7161   //
7162   // FIXME: Merging a function definition should merge
7163   // all mergeable entities within it.
7164   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7165       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7166     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7167       if (!getContext().getLangOpts().CPlusPlus &&
7168           isa<TranslationUnitDecl>(DC)) {
7169         // Outside of C++, we don't have a lookup table for the TU, so update
7170         // the identifier instead. (For C++ modules, we don't store decls
7171         // in the serialized identifier table, so we do the lookup in the TU.)
7172         auto *II = Name.getAsIdentifierInfo();
7173         assert(II && "non-identifier name in C?");
7174         if (II->isOutOfDate())
7175           updateOutOfDateIdentifier(*II);
7176       } else
7177         DC->lookup(Name);
7178     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7179       // Find all declarations of this kind from the relevant context.
7180       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7181         auto *DC = cast<DeclContext>(DCDecl);
7182         SmallVector<Decl*, 8> Decls;
7183         FindExternalLexicalDecls(
7184             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7185       }
7186     }
7187   }
7188 
7189   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7190     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7191   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7192     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7193   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7194     if (auto *Template = FD->getPrimaryTemplate())
7195       Template->LoadLazySpecializations();
7196   }
7197 }
7198 
7199 CXXCtorInitializer **
7200 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7201   RecordLocation Loc = getLocalBitOffset(Offset);
7202   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7203   SavedStreamPosition SavedPosition(Cursor);
7204   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7205     Error(std::move(Err));
7206     return nullptr;
7207   }
7208   ReadingKindTracker ReadingKind(Read_Decl, *this);
7209 
7210   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7211   if (!MaybeCode) {
7212     Error(MaybeCode.takeError());
7213     return nullptr;
7214   }
7215   unsigned Code = MaybeCode.get();
7216 
7217   ASTRecordReader Record(*this, *Loc.F);
7218   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7219   if (!MaybeRecCode) {
7220     Error(MaybeRecCode.takeError());
7221     return nullptr;
7222   }
7223   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7224     Error("malformed AST file: missing C++ ctor initializers");
7225     return nullptr;
7226   }
7227 
7228   return Record.readCXXCtorInitializers();
7229 }
7230 
7231 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7232   assert(ContextObj && "reading base specifiers with no AST context");
7233   ASTContext &Context = *ContextObj;
7234 
7235   RecordLocation Loc = getLocalBitOffset(Offset);
7236   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7237   SavedStreamPosition SavedPosition(Cursor);
7238   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7239     Error(std::move(Err));
7240     return nullptr;
7241   }
7242   ReadingKindTracker ReadingKind(Read_Decl, *this);
7243 
7244   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7245   if (!MaybeCode) {
7246     Error(MaybeCode.takeError());
7247     return nullptr;
7248   }
7249   unsigned Code = MaybeCode.get();
7250 
7251   ASTRecordReader Record(*this, *Loc.F);
7252   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7253   if (!MaybeRecCode) {
7254     Error(MaybeCode.takeError());
7255     return nullptr;
7256   }
7257   unsigned RecCode = MaybeRecCode.get();
7258 
7259   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7260     Error("malformed AST file: missing C++ base specifiers");
7261     return nullptr;
7262   }
7263 
7264   unsigned NumBases = Record.readInt();
7265   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7266   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7267   for (unsigned I = 0; I != NumBases; ++I)
7268     Bases[I] = Record.readCXXBaseSpecifier();
7269   return Bases;
7270 }
7271 
7272 serialization::DeclID
7273 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7274   if (LocalID < NUM_PREDEF_DECL_IDS)
7275     return LocalID;
7276 
7277   if (!F.ModuleOffsetMap.empty())
7278     ReadModuleOffsetMap(F);
7279 
7280   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7281     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7282   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7283 
7284   return LocalID + I->second;
7285 }
7286 
7287 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7288                                    ModuleFile &M) const {
7289   // Predefined decls aren't from any module.
7290   if (ID < NUM_PREDEF_DECL_IDS)
7291     return false;
7292 
7293   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7294          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7295 }
7296 
7297 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7298   if (!D->isFromASTFile())
7299     return nullptr;
7300   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7301   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7302   return I->second;
7303 }
7304 
7305 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7306   if (ID < NUM_PREDEF_DECL_IDS)
7307     return SourceLocation();
7308 
7309   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7310 
7311   if (Index > DeclsLoaded.size()) {
7312     Error("declaration ID out-of-range for AST file");
7313     return SourceLocation();
7314   }
7315 
7316   if (Decl *D = DeclsLoaded[Index])
7317     return D->getLocation();
7318 
7319   SourceLocation Loc;
7320   DeclCursorForID(ID, Loc);
7321   return Loc;
7322 }
7323 
7324 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7325   switch (ID) {
7326   case PREDEF_DECL_NULL_ID:
7327     return nullptr;
7328 
7329   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7330     return Context.getTranslationUnitDecl();
7331 
7332   case PREDEF_DECL_OBJC_ID_ID:
7333     return Context.getObjCIdDecl();
7334 
7335   case PREDEF_DECL_OBJC_SEL_ID:
7336     return Context.getObjCSelDecl();
7337 
7338   case PREDEF_DECL_OBJC_CLASS_ID:
7339     return Context.getObjCClassDecl();
7340 
7341   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7342     return Context.getObjCProtocolDecl();
7343 
7344   case PREDEF_DECL_INT_128_ID:
7345     return Context.getInt128Decl();
7346 
7347   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7348     return Context.getUInt128Decl();
7349 
7350   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7351     return Context.getObjCInstanceTypeDecl();
7352 
7353   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7354     return Context.getBuiltinVaListDecl();
7355 
7356   case PREDEF_DECL_VA_LIST_TAG:
7357     return Context.getVaListTagDecl();
7358 
7359   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7360     return Context.getBuiltinMSVaListDecl();
7361 
7362   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7363     return Context.getMSGuidTagDecl();
7364 
7365   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7366     return Context.getExternCContextDecl();
7367 
7368   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7369     return Context.getMakeIntegerSeqDecl();
7370 
7371   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7372     return Context.getCFConstantStringDecl();
7373 
7374   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7375     return Context.getCFConstantStringTagDecl();
7376 
7377   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7378     return Context.getTypePackElementDecl();
7379   }
7380   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7381 }
7382 
7383 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7384   assert(ContextObj && "reading decl with no AST context");
7385   if (ID < NUM_PREDEF_DECL_IDS) {
7386     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7387     if (D) {
7388       // Track that we have merged the declaration with ID \p ID into the
7389       // pre-existing predefined declaration \p D.
7390       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7391       if (Merged.empty())
7392         Merged.push_back(ID);
7393     }
7394     return D;
7395   }
7396 
7397   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7398 
7399   if (Index >= DeclsLoaded.size()) {
7400     assert(0 && "declaration ID out-of-range for AST file");
7401     Error("declaration ID out-of-range for AST file");
7402     return nullptr;
7403   }
7404 
7405   return DeclsLoaded[Index];
7406 }
7407 
7408 Decl *ASTReader::GetDecl(DeclID ID) {
7409   if (ID < NUM_PREDEF_DECL_IDS)
7410     return GetExistingDecl(ID);
7411 
7412   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7413 
7414   if (Index >= DeclsLoaded.size()) {
7415     assert(0 && "declaration ID out-of-range for AST file");
7416     Error("declaration ID out-of-range for AST file");
7417     return nullptr;
7418   }
7419 
7420   if (!DeclsLoaded[Index]) {
7421     ReadDeclRecord(ID);
7422     if (DeserializationListener)
7423       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7424   }
7425 
7426   return DeclsLoaded[Index];
7427 }
7428 
7429 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7430                                                   DeclID GlobalID) {
7431   if (GlobalID < NUM_PREDEF_DECL_IDS)
7432     return GlobalID;
7433 
7434   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7435   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7436   ModuleFile *Owner = I->second;
7437 
7438   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7439     = M.GlobalToLocalDeclIDs.find(Owner);
7440   if (Pos == M.GlobalToLocalDeclIDs.end())
7441     return 0;
7442 
7443   return GlobalID - Owner->BaseDeclID + Pos->second;
7444 }
7445 
7446 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7447                                             const RecordData &Record,
7448                                             unsigned &Idx) {
7449   if (Idx >= Record.size()) {
7450     Error("Corrupted AST file");
7451     return 0;
7452   }
7453 
7454   return getGlobalDeclID(F, Record[Idx++]);
7455 }
7456 
7457 /// Resolve the offset of a statement into a statement.
7458 ///
7459 /// This operation will read a new statement from the external
7460 /// source each time it is called, and is meant to be used via a
7461 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7462 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7463   // Switch case IDs are per Decl.
7464   ClearSwitchCaseIDs();
7465 
7466   // Offset here is a global offset across the entire chain.
7467   RecordLocation Loc = getLocalBitOffset(Offset);
7468   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7469     Error(std::move(Err));
7470     return nullptr;
7471   }
7472   assert(NumCurrentElementsDeserializing == 0 &&
7473          "should not be called while already deserializing");
7474   Deserializing D(this);
7475   return ReadStmtFromStream(*Loc.F);
7476 }
7477 
7478 void ASTReader::FindExternalLexicalDecls(
7479     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7480     SmallVectorImpl<Decl *> &Decls) {
7481   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7482 
7483   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7484     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7485     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7486       auto K = (Decl::Kind)+LexicalDecls[I];
7487       if (!IsKindWeWant(K))
7488         continue;
7489 
7490       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7491 
7492       // Don't add predefined declarations to the lexical context more
7493       // than once.
7494       if (ID < NUM_PREDEF_DECL_IDS) {
7495         if (PredefsVisited[ID])
7496           continue;
7497 
7498         PredefsVisited[ID] = true;
7499       }
7500 
7501       if (Decl *D = GetLocalDecl(*M, ID)) {
7502         assert(D->getKind() == K && "wrong kind for lexical decl");
7503         if (!DC->isDeclInLexicalTraversal(D))
7504           Decls.push_back(D);
7505       }
7506     }
7507   };
7508 
7509   if (isa<TranslationUnitDecl>(DC)) {
7510     for (auto Lexical : TULexicalDecls)
7511       Visit(Lexical.first, Lexical.second);
7512   } else {
7513     auto I = LexicalDecls.find(DC);
7514     if (I != LexicalDecls.end())
7515       Visit(I->second.first, I->second.second);
7516   }
7517 
7518   ++NumLexicalDeclContextsRead;
7519 }
7520 
7521 namespace {
7522 
7523 class DeclIDComp {
7524   ASTReader &Reader;
7525   ModuleFile &Mod;
7526 
7527 public:
7528   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7529 
7530   bool operator()(LocalDeclID L, LocalDeclID R) const {
7531     SourceLocation LHS = getLocation(L);
7532     SourceLocation RHS = getLocation(R);
7533     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7534   }
7535 
7536   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7537     SourceLocation RHS = getLocation(R);
7538     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7539   }
7540 
7541   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7542     SourceLocation LHS = getLocation(L);
7543     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7544   }
7545 
7546   SourceLocation getLocation(LocalDeclID ID) const {
7547     return Reader.getSourceManager().getFileLoc(
7548             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7549   }
7550 };
7551 
7552 } // namespace
7553 
7554 void ASTReader::FindFileRegionDecls(FileID File,
7555                                     unsigned Offset, unsigned Length,
7556                                     SmallVectorImpl<Decl *> &Decls) {
7557   SourceManager &SM = getSourceManager();
7558 
7559   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7560   if (I == FileDeclIDs.end())
7561     return;
7562 
7563   FileDeclsInfo &DInfo = I->second;
7564   if (DInfo.Decls.empty())
7565     return;
7566 
7567   SourceLocation
7568     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7569   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7570 
7571   DeclIDComp DIDComp(*this, *DInfo.Mod);
7572   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7573       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7574   if (BeginIt != DInfo.Decls.begin())
7575     --BeginIt;
7576 
7577   // If we are pointing at a top-level decl inside an objc container, we need
7578   // to backtrack until we find it otherwise we will fail to report that the
7579   // region overlaps with an objc container.
7580   while (BeginIt != DInfo.Decls.begin() &&
7581          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7582              ->isTopLevelDeclInObjCContainer())
7583     --BeginIt;
7584 
7585   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7586       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7587   if (EndIt != DInfo.Decls.end())
7588     ++EndIt;
7589 
7590   for (ArrayRef<serialization::LocalDeclID>::iterator
7591          DIt = BeginIt; DIt != EndIt; ++DIt)
7592     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7593 }
7594 
7595 bool
7596 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7597                                           DeclarationName Name) {
7598   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7599          "DeclContext has no visible decls in storage");
7600   if (!Name)
7601     return false;
7602 
7603   auto It = Lookups.find(DC);
7604   if (It == Lookups.end())
7605     return false;
7606 
7607   Deserializing LookupResults(this);
7608 
7609   // Load the list of declarations.
7610   SmallVector<NamedDecl *, 64> Decls;
7611   for (DeclID ID : It->second.Table.find(Name)) {
7612     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7613     if (ND->getDeclName() == Name)
7614       Decls.push_back(ND);
7615   }
7616 
7617   ++NumVisibleDeclContextsRead;
7618   SetExternalVisibleDeclsForName(DC, Name, Decls);
7619   return !Decls.empty();
7620 }
7621 
7622 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7623   if (!DC->hasExternalVisibleStorage())
7624     return;
7625 
7626   auto It = Lookups.find(DC);
7627   assert(It != Lookups.end() &&
7628          "have external visible storage but no lookup tables");
7629 
7630   DeclsMap Decls;
7631 
7632   for (DeclID ID : It->second.Table.findAll()) {
7633     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7634     Decls[ND->getDeclName()].push_back(ND);
7635   }
7636 
7637   ++NumVisibleDeclContextsRead;
7638 
7639   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7640     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7641   }
7642   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7643 }
7644 
7645 const serialization::reader::DeclContextLookupTable *
7646 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7647   auto I = Lookups.find(Primary);
7648   return I == Lookups.end() ? nullptr : &I->second;
7649 }
7650 
7651 /// Under non-PCH compilation the consumer receives the objc methods
7652 /// before receiving the implementation, and codegen depends on this.
7653 /// We simulate this by deserializing and passing to consumer the methods of the
7654 /// implementation before passing the deserialized implementation decl.
7655 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7656                                        ASTConsumer *Consumer) {
7657   assert(ImplD && Consumer);
7658 
7659   for (auto *I : ImplD->methods())
7660     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7661 
7662   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7663 }
7664 
7665 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7666   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7667     PassObjCImplDeclToConsumer(ImplD, Consumer);
7668   else
7669     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7670 }
7671 
7672 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7673   this->Consumer = Consumer;
7674 
7675   if (Consumer)
7676     PassInterestingDeclsToConsumer();
7677 
7678   if (DeserializationListener)
7679     DeserializationListener->ReaderInitialized(this);
7680 }
7681 
7682 void ASTReader::PrintStats() {
7683   std::fprintf(stderr, "*** AST File Statistics:\n");
7684 
7685   unsigned NumTypesLoaded
7686     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7687                                       QualType());
7688   unsigned NumDeclsLoaded
7689     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7690                                       (Decl *)nullptr);
7691   unsigned NumIdentifiersLoaded
7692     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7693                                             IdentifiersLoaded.end(),
7694                                             (IdentifierInfo *)nullptr);
7695   unsigned NumMacrosLoaded
7696     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7697                                        MacrosLoaded.end(),
7698                                        (MacroInfo *)nullptr);
7699   unsigned NumSelectorsLoaded
7700     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7701                                           SelectorsLoaded.end(),
7702                                           Selector());
7703 
7704   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7705     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7706                  NumSLocEntriesRead, TotalNumSLocEntries,
7707                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7708   if (!TypesLoaded.empty())
7709     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7710                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7711                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7712   if (!DeclsLoaded.empty())
7713     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7714                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7715                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7716   if (!IdentifiersLoaded.empty())
7717     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7718                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7719                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7720   if (!MacrosLoaded.empty())
7721     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7722                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7723                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7724   if (!SelectorsLoaded.empty())
7725     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7726                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7727                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7728   if (TotalNumStatements)
7729     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7730                  NumStatementsRead, TotalNumStatements,
7731                  ((float)NumStatementsRead/TotalNumStatements * 100));
7732   if (TotalNumMacros)
7733     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7734                  NumMacrosRead, TotalNumMacros,
7735                  ((float)NumMacrosRead/TotalNumMacros * 100));
7736   if (TotalLexicalDeclContexts)
7737     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7738                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7739                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7740                   * 100));
7741   if (TotalVisibleDeclContexts)
7742     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7743                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7744                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7745                   * 100));
7746   if (TotalNumMethodPoolEntries)
7747     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7748                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7749                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7750                   * 100));
7751   if (NumMethodPoolLookups)
7752     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7753                  NumMethodPoolHits, NumMethodPoolLookups,
7754                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7755   if (NumMethodPoolTableLookups)
7756     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7757                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7758                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7759                   * 100.0));
7760   if (NumIdentifierLookupHits)
7761     std::fprintf(stderr,
7762                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7763                  NumIdentifierLookupHits, NumIdentifierLookups,
7764                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7765 
7766   if (GlobalIndex) {
7767     std::fprintf(stderr, "\n");
7768     GlobalIndex->printStats();
7769   }
7770 
7771   std::fprintf(stderr, "\n");
7772   dump();
7773   std::fprintf(stderr, "\n");
7774 }
7775 
7776 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7777 LLVM_DUMP_METHOD static void
7778 dumpModuleIDMap(StringRef Name,
7779                 const ContinuousRangeMap<Key, ModuleFile *,
7780                                          InitialCapacity> &Map) {
7781   if (Map.begin() == Map.end())
7782     return;
7783 
7784   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7785 
7786   llvm::errs() << Name << ":\n";
7787   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7788        I != IEnd; ++I) {
7789     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7790       << "\n";
7791   }
7792 }
7793 
7794 LLVM_DUMP_METHOD void ASTReader::dump() {
7795   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7796   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7797   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7798   dumpModuleIDMap("Global type map", GlobalTypeMap);
7799   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7800   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7801   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7802   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7803   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7804   dumpModuleIDMap("Global preprocessed entity map",
7805                   GlobalPreprocessedEntityMap);
7806 
7807   llvm::errs() << "\n*** PCH/Modules Loaded:";
7808   for (ModuleFile &M : ModuleMgr)
7809     M.dump();
7810 }
7811 
7812 /// Return the amount of memory used by memory buffers, breaking down
7813 /// by heap-backed versus mmap'ed memory.
7814 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7815   for (ModuleFile &I : ModuleMgr) {
7816     if (llvm::MemoryBuffer *buf = I.Buffer) {
7817       size_t bytes = buf->getBufferSize();
7818       switch (buf->getBufferKind()) {
7819         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7820           sizes.malloc_bytes += bytes;
7821           break;
7822         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7823           sizes.mmap_bytes += bytes;
7824           break;
7825       }
7826     }
7827   }
7828 }
7829 
7830 void ASTReader::InitializeSema(Sema &S) {
7831   SemaObj = &S;
7832   S.addExternalSource(this);
7833 
7834   // Makes sure any declarations that were deserialized "too early"
7835   // still get added to the identifier's declaration chains.
7836   for (uint64_t ID : PreloadedDeclIDs) {
7837     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7838     pushExternalDeclIntoScope(D, D->getDeclName());
7839   }
7840   PreloadedDeclIDs.clear();
7841 
7842   // FIXME: What happens if these are changed by a module import?
7843   if (!FPPragmaOptions.empty()) {
7844     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7845     FPOptionsOverride NewOverrides =
7846         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7847     SemaObj->CurFPFeatures =
7848         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7849   }
7850 
7851   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7852   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7853   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7854 
7855   UpdateSema();
7856 }
7857 
7858 void ASTReader::UpdateSema() {
7859   assert(SemaObj && "no Sema to update");
7860 
7861   // Load the offsets of the declarations that Sema references.
7862   // They will be lazily deserialized when needed.
7863   if (!SemaDeclRefs.empty()) {
7864     assert(SemaDeclRefs.size() % 3 == 0);
7865     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7866       if (!SemaObj->StdNamespace)
7867         SemaObj->StdNamespace = SemaDeclRefs[I];
7868       if (!SemaObj->StdBadAlloc)
7869         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7870       if (!SemaObj->StdAlignValT)
7871         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7872     }
7873     SemaDeclRefs.clear();
7874   }
7875 
7876   // Update the state of pragmas. Use the same API as if we had encountered the
7877   // pragma in the source.
7878   if(OptimizeOffPragmaLocation.isValid())
7879     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7880   if (PragmaMSStructState != -1)
7881     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7882   if (PointersToMembersPragmaLocation.isValid()) {
7883     SemaObj->ActOnPragmaMSPointersToMembers(
7884         (LangOptions::PragmaMSPointersToMembersKind)
7885             PragmaMSPointersToMembersState,
7886         PointersToMembersPragmaLocation);
7887   }
7888   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7889 
7890   if (PragmaPackCurrentValue) {
7891     // The bottom of the stack might have a default value. It must be adjusted
7892     // to the current value to ensure that the packing state is preserved after
7893     // popping entries that were included/imported from a PCH/module.
7894     bool DropFirst = false;
7895     if (!PragmaPackStack.empty() &&
7896         PragmaPackStack.front().Location.isInvalid()) {
7897       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7898              "Expected a default alignment value");
7899       SemaObj->PackStack.Stack.emplace_back(
7900           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7901           SemaObj->PackStack.CurrentPragmaLocation,
7902           PragmaPackStack.front().PushLocation);
7903       DropFirst = true;
7904     }
7905     for (const auto &Entry :
7906          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7907       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7908                                             Entry.Location, Entry.PushLocation);
7909     if (PragmaPackCurrentLocation.isInvalid()) {
7910       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7911              "Expected a default alignment value");
7912       // Keep the current values.
7913     } else {
7914       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7915       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7916     }
7917   }
7918   if (FpPragmaCurrentValue) {
7919     // The bottom of the stack might have a default value. It must be adjusted
7920     // to the current value to ensure that fp-pragma state is preserved after
7921     // popping entries that were included/imported from a PCH/module.
7922     bool DropFirst = false;
7923     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7924       assert(FpPragmaStack.front().Value ==
7925                  SemaObj->FpPragmaStack.DefaultValue &&
7926              "Expected a default pragma float_control value");
7927       SemaObj->FpPragmaStack.Stack.emplace_back(
7928           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7929           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7930           FpPragmaStack.front().PushLocation);
7931       DropFirst = true;
7932     }
7933     for (const auto &Entry :
7934          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7935       SemaObj->FpPragmaStack.Stack.emplace_back(
7936           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7937     if (FpPragmaCurrentLocation.isInvalid()) {
7938       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7939              "Expected a default pragma float_control value");
7940       // Keep the current values.
7941     } else {
7942       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7943       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7944     }
7945   }
7946 }
7947 
7948 IdentifierInfo *ASTReader::get(StringRef Name) {
7949   // Note that we are loading an identifier.
7950   Deserializing AnIdentifier(this);
7951 
7952   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7953                                   NumIdentifierLookups,
7954                                   NumIdentifierLookupHits);
7955 
7956   // We don't need to do identifier table lookups in C++ modules (we preload
7957   // all interesting declarations, and don't need to use the scope for name
7958   // lookups). Perform the lookup in PCH files, though, since we don't build
7959   // a complete initial identifier table if we're carrying on from a PCH.
7960   if (PP.getLangOpts().CPlusPlus) {
7961     for (auto F : ModuleMgr.pch_modules())
7962       if (Visitor(*F))
7963         break;
7964   } else {
7965     // If there is a global index, look there first to determine which modules
7966     // provably do not have any results for this identifier.
7967     GlobalModuleIndex::HitSet Hits;
7968     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7969     if (!loadGlobalIndex()) {
7970       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7971         HitsPtr = &Hits;
7972       }
7973     }
7974 
7975     ModuleMgr.visit(Visitor, HitsPtr);
7976   }
7977 
7978   IdentifierInfo *II = Visitor.getIdentifierInfo();
7979   markIdentifierUpToDate(II);
7980   return II;
7981 }
7982 
7983 namespace clang {
7984 
7985   /// An identifier-lookup iterator that enumerates all of the
7986   /// identifiers stored within a set of AST files.
7987   class ASTIdentifierIterator : public IdentifierIterator {
7988     /// The AST reader whose identifiers are being enumerated.
7989     const ASTReader &Reader;
7990 
7991     /// The current index into the chain of AST files stored in
7992     /// the AST reader.
7993     unsigned Index;
7994 
7995     /// The current position within the identifier lookup table
7996     /// of the current AST file.
7997     ASTIdentifierLookupTable::key_iterator Current;
7998 
7999     /// The end position within the identifier lookup table of
8000     /// the current AST file.
8001     ASTIdentifierLookupTable::key_iterator End;
8002 
8003     /// Whether to skip any modules in the ASTReader.
8004     bool SkipModules;
8005 
8006   public:
8007     explicit ASTIdentifierIterator(const ASTReader &Reader,
8008                                    bool SkipModules = false);
8009 
8010     StringRef Next() override;
8011   };
8012 
8013 } // namespace clang
8014 
8015 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8016                                              bool SkipModules)
8017     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8018 }
8019 
8020 StringRef ASTIdentifierIterator::Next() {
8021   while (Current == End) {
8022     // If we have exhausted all of our AST files, we're done.
8023     if (Index == 0)
8024       return StringRef();
8025 
8026     --Index;
8027     ModuleFile &F = Reader.ModuleMgr[Index];
8028     if (SkipModules && F.isModule())
8029       continue;
8030 
8031     ASTIdentifierLookupTable *IdTable =
8032         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8033     Current = IdTable->key_begin();
8034     End = IdTable->key_end();
8035   }
8036 
8037   // We have any identifiers remaining in the current AST file; return
8038   // the next one.
8039   StringRef Result = *Current;
8040   ++Current;
8041   return Result;
8042 }
8043 
8044 namespace {
8045 
8046 /// A utility for appending two IdentifierIterators.
8047 class ChainedIdentifierIterator : public IdentifierIterator {
8048   std::unique_ptr<IdentifierIterator> Current;
8049   std::unique_ptr<IdentifierIterator> Queued;
8050 
8051 public:
8052   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8053                             std::unique_ptr<IdentifierIterator> Second)
8054       : Current(std::move(First)), Queued(std::move(Second)) {}
8055 
8056   StringRef Next() override {
8057     if (!Current)
8058       return StringRef();
8059 
8060     StringRef result = Current->Next();
8061     if (!result.empty())
8062       return result;
8063 
8064     // Try the queued iterator, which may itself be empty.
8065     Current.reset();
8066     std::swap(Current, Queued);
8067     return Next();
8068   }
8069 };
8070 
8071 } // namespace
8072 
8073 IdentifierIterator *ASTReader::getIdentifiers() {
8074   if (!loadGlobalIndex()) {
8075     std::unique_ptr<IdentifierIterator> ReaderIter(
8076         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8077     std::unique_ptr<IdentifierIterator> ModulesIter(
8078         GlobalIndex->createIdentifierIterator());
8079     return new ChainedIdentifierIterator(std::move(ReaderIter),
8080                                          std::move(ModulesIter));
8081   }
8082 
8083   return new ASTIdentifierIterator(*this);
8084 }
8085 
8086 namespace clang {
8087 namespace serialization {
8088 
8089   class ReadMethodPoolVisitor {
8090     ASTReader &Reader;
8091     Selector Sel;
8092     unsigned PriorGeneration;
8093     unsigned InstanceBits = 0;
8094     unsigned FactoryBits = 0;
8095     bool InstanceHasMoreThanOneDecl = false;
8096     bool FactoryHasMoreThanOneDecl = false;
8097     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8098     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8099 
8100   public:
8101     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8102                           unsigned PriorGeneration)
8103         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8104 
8105     bool operator()(ModuleFile &M) {
8106       if (!M.SelectorLookupTable)
8107         return false;
8108 
8109       // If we've already searched this module file, skip it now.
8110       if (M.Generation <= PriorGeneration)
8111         return true;
8112 
8113       ++Reader.NumMethodPoolTableLookups;
8114       ASTSelectorLookupTable *PoolTable
8115         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8116       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8117       if (Pos == PoolTable->end())
8118         return false;
8119 
8120       ++Reader.NumMethodPoolTableHits;
8121       ++Reader.NumSelectorsRead;
8122       // FIXME: Not quite happy with the statistics here. We probably should
8123       // disable this tracking when called via LoadSelector.
8124       // Also, should entries without methods count as misses?
8125       ++Reader.NumMethodPoolEntriesRead;
8126       ASTSelectorLookupTrait::data_type Data = *Pos;
8127       if (Reader.DeserializationListener)
8128         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8129 
8130       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8131       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8132       InstanceBits = Data.InstanceBits;
8133       FactoryBits = Data.FactoryBits;
8134       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8135       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8136       return true;
8137     }
8138 
8139     /// Retrieve the instance methods found by this visitor.
8140     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8141       return InstanceMethods;
8142     }
8143 
8144     /// Retrieve the instance methods found by this visitor.
8145     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8146       return FactoryMethods;
8147     }
8148 
8149     unsigned getInstanceBits() const { return InstanceBits; }
8150     unsigned getFactoryBits() const { return FactoryBits; }
8151 
8152     bool instanceHasMoreThanOneDecl() const {
8153       return InstanceHasMoreThanOneDecl;
8154     }
8155 
8156     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8157   };
8158 
8159 } // namespace serialization
8160 } // namespace clang
8161 
8162 /// Add the given set of methods to the method list.
8163 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8164                              ObjCMethodList &List) {
8165   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8166     S.addMethodToGlobalList(&List, Methods[I]);
8167   }
8168 }
8169 
8170 void ASTReader::ReadMethodPool(Selector Sel) {
8171   // Get the selector generation and update it to the current generation.
8172   unsigned &Generation = SelectorGeneration[Sel];
8173   unsigned PriorGeneration = Generation;
8174   Generation = getGeneration();
8175   SelectorOutOfDate[Sel] = false;
8176 
8177   // Search for methods defined with this selector.
8178   ++NumMethodPoolLookups;
8179   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8180   ModuleMgr.visit(Visitor);
8181 
8182   if (Visitor.getInstanceMethods().empty() &&
8183       Visitor.getFactoryMethods().empty())
8184     return;
8185 
8186   ++NumMethodPoolHits;
8187 
8188   if (!getSema())
8189     return;
8190 
8191   Sema &S = *getSema();
8192   Sema::GlobalMethodPool::iterator Pos
8193     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8194 
8195   Pos->second.first.setBits(Visitor.getInstanceBits());
8196   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8197   Pos->second.second.setBits(Visitor.getFactoryBits());
8198   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8199 
8200   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8201   // when building a module we keep every method individually and may need to
8202   // update hasMoreThanOneDecl as we add the methods.
8203   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8204   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8205 }
8206 
8207 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8208   if (SelectorOutOfDate[Sel])
8209     ReadMethodPool(Sel);
8210 }
8211 
8212 void ASTReader::ReadKnownNamespaces(
8213                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8214   Namespaces.clear();
8215 
8216   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8217     if (NamespaceDecl *Namespace
8218                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8219       Namespaces.push_back(Namespace);
8220   }
8221 }
8222 
8223 void ASTReader::ReadUndefinedButUsed(
8224     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8225   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8226     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8227     SourceLocation Loc =
8228         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8229     Undefined.insert(std::make_pair(D, Loc));
8230   }
8231 }
8232 
8233 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8234     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8235                                                      Exprs) {
8236   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8237     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8238     uint64_t Count = DelayedDeleteExprs[Idx++];
8239     for (uint64_t C = 0; C < Count; ++C) {
8240       SourceLocation DeleteLoc =
8241           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8242       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8243       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8244     }
8245   }
8246 }
8247 
8248 void ASTReader::ReadTentativeDefinitions(
8249                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8250   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8251     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8252     if (Var)
8253       TentativeDefs.push_back(Var);
8254   }
8255   TentativeDefinitions.clear();
8256 }
8257 
8258 void ASTReader::ReadUnusedFileScopedDecls(
8259                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8260   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8261     DeclaratorDecl *D
8262       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8263     if (D)
8264       Decls.push_back(D);
8265   }
8266   UnusedFileScopedDecls.clear();
8267 }
8268 
8269 void ASTReader::ReadDelegatingConstructors(
8270                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8271   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8272     CXXConstructorDecl *D
8273       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8274     if (D)
8275       Decls.push_back(D);
8276   }
8277   DelegatingCtorDecls.clear();
8278 }
8279 
8280 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8281   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8282     TypedefNameDecl *D
8283       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8284     if (D)
8285       Decls.push_back(D);
8286   }
8287   ExtVectorDecls.clear();
8288 }
8289 
8290 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8291     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8292   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8293        ++I) {
8294     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8295         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8296     if (D)
8297       Decls.insert(D);
8298   }
8299   UnusedLocalTypedefNameCandidates.clear();
8300 }
8301 
8302 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8303     llvm::SmallVector<Decl *, 4> &Decls) {
8304   for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8305        ++I) {
8306     auto *D = dyn_cast_or_null<Decl>(
8307         GetDecl(DeclsToCheckForDeferredDiags[I]));
8308     if (D)
8309       Decls.push_back(D);
8310   }
8311   DeclsToCheckForDeferredDiags.clear();
8312 }
8313 
8314 
8315 void ASTReader::ReadReferencedSelectors(
8316        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8317   if (ReferencedSelectorsData.empty())
8318     return;
8319 
8320   // If there are @selector references added them to its pool. This is for
8321   // implementation of -Wselector.
8322   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8323   unsigned I = 0;
8324   while (I < DataSize) {
8325     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8326     SourceLocation SelLoc
8327       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8328     Sels.push_back(std::make_pair(Sel, SelLoc));
8329   }
8330   ReferencedSelectorsData.clear();
8331 }
8332 
8333 void ASTReader::ReadWeakUndeclaredIdentifiers(
8334        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8335   if (WeakUndeclaredIdentifiers.empty())
8336     return;
8337 
8338   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8339     IdentifierInfo *WeakId
8340       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8341     IdentifierInfo *AliasId
8342       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8343     SourceLocation Loc
8344       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8345     bool Used = WeakUndeclaredIdentifiers[I++];
8346     WeakInfo WI(AliasId, Loc);
8347     WI.setUsed(Used);
8348     WeakIDs.push_back(std::make_pair(WeakId, WI));
8349   }
8350   WeakUndeclaredIdentifiers.clear();
8351 }
8352 
8353 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8354   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8355     ExternalVTableUse VT;
8356     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8357     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8358     VT.DefinitionRequired = VTableUses[Idx++];
8359     VTables.push_back(VT);
8360   }
8361 
8362   VTableUses.clear();
8363 }
8364 
8365 void ASTReader::ReadPendingInstantiations(
8366        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8367   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8368     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8369     SourceLocation Loc
8370       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8371 
8372     Pending.push_back(std::make_pair(D, Loc));
8373   }
8374   PendingInstantiations.clear();
8375 }
8376 
8377 void ASTReader::ReadLateParsedTemplates(
8378     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8379         &LPTMap) {
8380   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8381        /* In loop */) {
8382     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8383 
8384     auto LT = std::make_unique<LateParsedTemplate>();
8385     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8386 
8387     ModuleFile *F = getOwningModuleFile(LT->D);
8388     assert(F && "No module");
8389 
8390     unsigned TokN = LateParsedTemplates[Idx++];
8391     LT->Toks.reserve(TokN);
8392     for (unsigned T = 0; T < TokN; ++T)
8393       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8394 
8395     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8396   }
8397 
8398   LateParsedTemplates.clear();
8399 }
8400 
8401 void ASTReader::LoadSelector(Selector Sel) {
8402   // It would be complicated to avoid reading the methods anyway. So don't.
8403   ReadMethodPool(Sel);
8404 }
8405 
8406 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8407   assert(ID && "Non-zero identifier ID required");
8408   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8409   IdentifiersLoaded[ID - 1] = II;
8410   if (DeserializationListener)
8411     DeserializationListener->IdentifierRead(ID, II);
8412 }
8413 
8414 /// Set the globally-visible declarations associated with the given
8415 /// identifier.
8416 ///
8417 /// If the AST reader is currently in a state where the given declaration IDs
8418 /// cannot safely be resolved, they are queued until it is safe to resolve
8419 /// them.
8420 ///
8421 /// \param II an IdentifierInfo that refers to one or more globally-visible
8422 /// declarations.
8423 ///
8424 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8425 /// visible at global scope.
8426 ///
8427 /// \param Decls if non-null, this vector will be populated with the set of
8428 /// deserialized declarations. These declarations will not be pushed into
8429 /// scope.
8430 void
8431 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8432                               const SmallVectorImpl<uint32_t> &DeclIDs,
8433                                    SmallVectorImpl<Decl *> *Decls) {
8434   if (NumCurrentElementsDeserializing && !Decls) {
8435     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8436     return;
8437   }
8438 
8439   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8440     if (!SemaObj) {
8441       // Queue this declaration so that it will be added to the
8442       // translation unit scope and identifier's declaration chain
8443       // once a Sema object is known.
8444       PreloadedDeclIDs.push_back(DeclIDs[I]);
8445       continue;
8446     }
8447 
8448     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8449 
8450     // If we're simply supposed to record the declarations, do so now.
8451     if (Decls) {
8452       Decls->push_back(D);
8453       continue;
8454     }
8455 
8456     // Introduce this declaration into the translation-unit scope
8457     // and add it to the declaration chain for this identifier, so
8458     // that (unqualified) name lookup will find it.
8459     pushExternalDeclIntoScope(D, II);
8460   }
8461 }
8462 
8463 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8464   if (ID == 0)
8465     return nullptr;
8466 
8467   if (IdentifiersLoaded.empty()) {
8468     Error("no identifier table in AST file");
8469     return nullptr;
8470   }
8471 
8472   ID -= 1;
8473   if (!IdentifiersLoaded[ID]) {
8474     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8475     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8476     ModuleFile *M = I->second;
8477     unsigned Index = ID - M->BaseIdentifierID;
8478     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8479 
8480     // All of the strings in the AST file are preceded by a 16-bit length.
8481     // Extract that 16-bit length to avoid having to execute strlen().
8482     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8483     //  unsigned integers.  This is important to avoid integer overflow when
8484     //  we cast them to 'unsigned'.
8485     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8486     unsigned StrLen = (((unsigned) StrLenPtr[0])
8487                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8488     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8489     IdentifiersLoaded[ID] = &II;
8490     markIdentifierFromAST(*this,  II);
8491     if (DeserializationListener)
8492       DeserializationListener->IdentifierRead(ID + 1, &II);
8493   }
8494 
8495   return IdentifiersLoaded[ID];
8496 }
8497 
8498 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8499   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8500 }
8501 
8502 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8503   if (LocalID < NUM_PREDEF_IDENT_IDS)
8504     return LocalID;
8505 
8506   if (!M.ModuleOffsetMap.empty())
8507     ReadModuleOffsetMap(M);
8508 
8509   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8510     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8511   assert(I != M.IdentifierRemap.end()
8512          && "Invalid index into identifier index remap");
8513 
8514   return LocalID + I->second;
8515 }
8516 
8517 MacroInfo *ASTReader::getMacro(MacroID ID) {
8518   if (ID == 0)
8519     return nullptr;
8520 
8521   if (MacrosLoaded.empty()) {
8522     Error("no macro table in AST file");
8523     return nullptr;
8524   }
8525 
8526   ID -= NUM_PREDEF_MACRO_IDS;
8527   if (!MacrosLoaded[ID]) {
8528     GlobalMacroMapType::iterator I
8529       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8530     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8531     ModuleFile *M = I->second;
8532     unsigned Index = ID - M->BaseMacroID;
8533     MacrosLoaded[ID] =
8534         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8535 
8536     if (DeserializationListener)
8537       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8538                                          MacrosLoaded[ID]);
8539   }
8540 
8541   return MacrosLoaded[ID];
8542 }
8543 
8544 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8545   if (LocalID < NUM_PREDEF_MACRO_IDS)
8546     return LocalID;
8547 
8548   if (!M.ModuleOffsetMap.empty())
8549     ReadModuleOffsetMap(M);
8550 
8551   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8552     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8553   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8554 
8555   return LocalID + I->second;
8556 }
8557 
8558 serialization::SubmoduleID
8559 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8560   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8561     return LocalID;
8562 
8563   if (!M.ModuleOffsetMap.empty())
8564     ReadModuleOffsetMap(M);
8565 
8566   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8567     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8568   assert(I != M.SubmoduleRemap.end()
8569          && "Invalid index into submodule index remap");
8570 
8571   return LocalID + I->second;
8572 }
8573 
8574 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8575   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8576     assert(GlobalID == 0 && "Unhandled global submodule ID");
8577     return nullptr;
8578   }
8579 
8580   if (GlobalID > SubmodulesLoaded.size()) {
8581     Error("submodule ID out of range in AST file");
8582     return nullptr;
8583   }
8584 
8585   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8586 }
8587 
8588 Module *ASTReader::getModule(unsigned ID) {
8589   return getSubmodule(ID);
8590 }
8591 
8592 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8593   if (ID & 1) {
8594     // It's a module, look it up by submodule ID.
8595     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8596     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8597   } else {
8598     // It's a prefix (preamble, PCH, ...). Look it up by index.
8599     unsigned IndexFromEnd = ID >> 1;
8600     assert(IndexFromEnd && "got reference to unknown module file");
8601     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8602   }
8603 }
8604 
8605 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8606   if (!F)
8607     return 1;
8608 
8609   // For a file representing a module, use the submodule ID of the top-level
8610   // module as the file ID. For any other kind of file, the number of such
8611   // files loaded beforehand will be the same on reload.
8612   // FIXME: Is this true even if we have an explicit module file and a PCH?
8613   if (F->isModule())
8614     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8615 
8616   auto PCHModules = getModuleManager().pch_modules();
8617   auto I = llvm::find(PCHModules, F);
8618   assert(I != PCHModules.end() && "emitting reference to unknown file");
8619   return (I - PCHModules.end()) << 1;
8620 }
8621 
8622 llvm::Optional<ASTSourceDescriptor>
8623 ASTReader::getSourceDescriptor(unsigned ID) {
8624   if (Module *M = getSubmodule(ID))
8625     return ASTSourceDescriptor(*M);
8626 
8627   // If there is only a single PCH, return it instead.
8628   // Chained PCH are not supported.
8629   const auto &PCHChain = ModuleMgr.pch_modules();
8630   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8631     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8632     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8633     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8634     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8635                                MF.Signature);
8636   }
8637   return None;
8638 }
8639 
8640 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8641   auto I = DefinitionSource.find(FD);
8642   if (I == DefinitionSource.end())
8643     return EK_ReplyHazy;
8644   return I->second ? EK_Never : EK_Always;
8645 }
8646 
8647 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8648   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8649 }
8650 
8651 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8652   if (ID == 0)
8653     return Selector();
8654 
8655   if (ID > SelectorsLoaded.size()) {
8656     Error("selector ID out of range in AST file");
8657     return Selector();
8658   }
8659 
8660   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8661     // Load this selector from the selector table.
8662     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8663     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8664     ModuleFile &M = *I->second;
8665     ASTSelectorLookupTrait Trait(*this, M);
8666     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8667     SelectorsLoaded[ID - 1] =
8668       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8669     if (DeserializationListener)
8670       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8671   }
8672 
8673   return SelectorsLoaded[ID - 1];
8674 }
8675 
8676 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8677   return DecodeSelector(ID);
8678 }
8679 
8680 uint32_t ASTReader::GetNumExternalSelectors() {
8681   // ID 0 (the null selector) is considered an external selector.
8682   return getTotalNumSelectors() + 1;
8683 }
8684 
8685 serialization::SelectorID
8686 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8687   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8688     return LocalID;
8689 
8690   if (!M.ModuleOffsetMap.empty())
8691     ReadModuleOffsetMap(M);
8692 
8693   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8694     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8695   assert(I != M.SelectorRemap.end()
8696          && "Invalid index into selector index remap");
8697 
8698   return LocalID + I->second;
8699 }
8700 
8701 DeclarationNameLoc
8702 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8703   DeclarationNameLoc DNLoc;
8704   switch (Name.getNameKind()) {
8705   case DeclarationName::CXXConstructorName:
8706   case DeclarationName::CXXDestructorName:
8707   case DeclarationName::CXXConversionFunctionName:
8708     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8709     break;
8710 
8711   case DeclarationName::CXXOperatorName:
8712     DNLoc.CXXOperatorName.BeginOpNameLoc
8713       = readSourceLocation().getRawEncoding();
8714     DNLoc.CXXOperatorName.EndOpNameLoc
8715       = readSourceLocation().getRawEncoding();
8716     break;
8717 
8718   case DeclarationName::CXXLiteralOperatorName:
8719     DNLoc.CXXLiteralOperatorName.OpNameLoc
8720       = readSourceLocation().getRawEncoding();
8721     break;
8722 
8723   case DeclarationName::Identifier:
8724   case DeclarationName::ObjCZeroArgSelector:
8725   case DeclarationName::ObjCOneArgSelector:
8726   case DeclarationName::ObjCMultiArgSelector:
8727   case DeclarationName::CXXUsingDirective:
8728   case DeclarationName::CXXDeductionGuideName:
8729     break;
8730   }
8731   return DNLoc;
8732 }
8733 
8734 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8735   DeclarationNameInfo NameInfo;
8736   NameInfo.setName(readDeclarationName());
8737   NameInfo.setLoc(readSourceLocation());
8738   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8739   return NameInfo;
8740 }
8741 
8742 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8743   Info.QualifierLoc = readNestedNameSpecifierLoc();
8744   unsigned NumTPLists = readInt();
8745   Info.NumTemplParamLists = NumTPLists;
8746   if (NumTPLists) {
8747     Info.TemplParamLists =
8748         new (getContext()) TemplateParameterList *[NumTPLists];
8749     for (unsigned i = 0; i != NumTPLists; ++i)
8750       Info.TemplParamLists[i] = readTemplateParameterList();
8751   }
8752 }
8753 
8754 TemplateParameterList *
8755 ASTRecordReader::readTemplateParameterList() {
8756   SourceLocation TemplateLoc = readSourceLocation();
8757   SourceLocation LAngleLoc = readSourceLocation();
8758   SourceLocation RAngleLoc = readSourceLocation();
8759 
8760   unsigned NumParams = readInt();
8761   SmallVector<NamedDecl *, 16> Params;
8762   Params.reserve(NumParams);
8763   while (NumParams--)
8764     Params.push_back(readDeclAs<NamedDecl>());
8765 
8766   bool HasRequiresClause = readBool();
8767   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8768 
8769   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8770       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8771   return TemplateParams;
8772 }
8773 
8774 void ASTRecordReader::readTemplateArgumentList(
8775                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8776                         bool Canonicalize) {
8777   unsigned NumTemplateArgs = readInt();
8778   TemplArgs.reserve(NumTemplateArgs);
8779   while (NumTemplateArgs--)
8780     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8781 }
8782 
8783 /// Read a UnresolvedSet structure.
8784 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8785   unsigned NumDecls = readInt();
8786   Set.reserve(getContext(), NumDecls);
8787   while (NumDecls--) {
8788     DeclID ID = readDeclID();
8789     AccessSpecifier AS = (AccessSpecifier) readInt();
8790     Set.addLazyDecl(getContext(), ID, AS);
8791   }
8792 }
8793 
8794 CXXBaseSpecifier
8795 ASTRecordReader::readCXXBaseSpecifier() {
8796   bool isVirtual = readBool();
8797   bool isBaseOfClass = readBool();
8798   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8799   bool inheritConstructors = readBool();
8800   TypeSourceInfo *TInfo = readTypeSourceInfo();
8801   SourceRange Range = readSourceRange();
8802   SourceLocation EllipsisLoc = readSourceLocation();
8803   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8804                           EllipsisLoc);
8805   Result.setInheritConstructors(inheritConstructors);
8806   return Result;
8807 }
8808 
8809 CXXCtorInitializer **
8810 ASTRecordReader::readCXXCtorInitializers() {
8811   ASTContext &Context = getContext();
8812   unsigned NumInitializers = readInt();
8813   assert(NumInitializers && "wrote ctor initializers but have no inits");
8814   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8815   for (unsigned i = 0; i != NumInitializers; ++i) {
8816     TypeSourceInfo *TInfo = nullptr;
8817     bool IsBaseVirtual = false;
8818     FieldDecl *Member = nullptr;
8819     IndirectFieldDecl *IndirectMember = nullptr;
8820 
8821     CtorInitializerType Type = (CtorInitializerType) readInt();
8822     switch (Type) {
8823     case CTOR_INITIALIZER_BASE:
8824       TInfo = readTypeSourceInfo();
8825       IsBaseVirtual = readBool();
8826       break;
8827 
8828     case CTOR_INITIALIZER_DELEGATING:
8829       TInfo = readTypeSourceInfo();
8830       break;
8831 
8832      case CTOR_INITIALIZER_MEMBER:
8833       Member = readDeclAs<FieldDecl>();
8834       break;
8835 
8836      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8837       IndirectMember = readDeclAs<IndirectFieldDecl>();
8838       break;
8839     }
8840 
8841     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8842     Expr *Init = readExpr();
8843     SourceLocation LParenLoc = readSourceLocation();
8844     SourceLocation RParenLoc = readSourceLocation();
8845 
8846     CXXCtorInitializer *BOMInit;
8847     if (Type == CTOR_INITIALIZER_BASE)
8848       BOMInit = new (Context)
8849           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8850                              RParenLoc, MemberOrEllipsisLoc);
8851     else if (Type == CTOR_INITIALIZER_DELEGATING)
8852       BOMInit = new (Context)
8853           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8854     else if (Member)
8855       BOMInit = new (Context)
8856           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8857                              Init, RParenLoc);
8858     else
8859       BOMInit = new (Context)
8860           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8861                              LParenLoc, Init, RParenLoc);
8862 
8863     if (/*IsWritten*/readBool()) {
8864       unsigned SourceOrder = readInt();
8865       BOMInit->setSourceOrder(SourceOrder);
8866     }
8867 
8868     CtorInitializers[i] = BOMInit;
8869   }
8870 
8871   return CtorInitializers;
8872 }
8873 
8874 NestedNameSpecifierLoc
8875 ASTRecordReader::readNestedNameSpecifierLoc() {
8876   ASTContext &Context = getContext();
8877   unsigned N = readInt();
8878   NestedNameSpecifierLocBuilder Builder;
8879   for (unsigned I = 0; I != N; ++I) {
8880     auto Kind = readNestedNameSpecifierKind();
8881     switch (Kind) {
8882     case NestedNameSpecifier::Identifier: {
8883       IdentifierInfo *II = readIdentifier();
8884       SourceRange Range = readSourceRange();
8885       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8886       break;
8887     }
8888 
8889     case NestedNameSpecifier::Namespace: {
8890       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8891       SourceRange Range = readSourceRange();
8892       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8893       break;
8894     }
8895 
8896     case NestedNameSpecifier::NamespaceAlias: {
8897       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8898       SourceRange Range = readSourceRange();
8899       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8900       break;
8901     }
8902 
8903     case NestedNameSpecifier::TypeSpec:
8904     case NestedNameSpecifier::TypeSpecWithTemplate: {
8905       bool Template = readBool();
8906       TypeSourceInfo *T = readTypeSourceInfo();
8907       if (!T)
8908         return NestedNameSpecifierLoc();
8909       SourceLocation ColonColonLoc = readSourceLocation();
8910 
8911       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8912       Builder.Extend(Context,
8913                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8914                      T->getTypeLoc(), ColonColonLoc);
8915       break;
8916     }
8917 
8918     case NestedNameSpecifier::Global: {
8919       SourceLocation ColonColonLoc = readSourceLocation();
8920       Builder.MakeGlobal(Context, ColonColonLoc);
8921       break;
8922     }
8923 
8924     case NestedNameSpecifier::Super: {
8925       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8926       SourceRange Range = readSourceRange();
8927       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8928       break;
8929     }
8930     }
8931   }
8932 
8933   return Builder.getWithLocInContext(Context);
8934 }
8935 
8936 SourceRange
8937 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8938                            unsigned &Idx) {
8939   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8940   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8941   return SourceRange(beg, end);
8942 }
8943 
8944 static FixedPointSemantics
8945 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8946                         unsigned &Idx) {
8947   unsigned Width = Record[Idx++];
8948   unsigned Scale = Record[Idx++];
8949   uint64_t Tmp = Record[Idx++];
8950   bool IsSigned = Tmp & 0x1;
8951   bool IsSaturated = Tmp & 0x2;
8952   bool HasUnsignedPadding = Tmp & 0x4;
8953   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8954                              HasUnsignedPadding);
8955 }
8956 
8957 static const llvm::fltSemantics &
8958 readAPFloatSemantics(ASTRecordReader &reader) {
8959   return llvm::APFloatBase::EnumToSemantics(
8960     static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8961 }
8962 
8963 APValue ASTRecordReader::readAPValue() {
8964   unsigned Kind = readInt();
8965   switch ((APValue::ValueKind) Kind) {
8966   case APValue::None:
8967     return APValue();
8968   case APValue::Indeterminate:
8969     return APValue::IndeterminateValue();
8970   case APValue::Int:
8971     return APValue(readAPSInt());
8972   case APValue::Float: {
8973     const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8974     return APValue(readAPFloat(FloatSema));
8975   }
8976   case APValue::FixedPoint: {
8977     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8978     return APValue(APFixedPoint(readAPInt(), FPSema));
8979   }
8980   case APValue::ComplexInt: {
8981     llvm::APSInt First = readAPSInt();
8982     return APValue(std::move(First), readAPSInt());
8983   }
8984   case APValue::ComplexFloat: {
8985     const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8986     llvm::APFloat First = readAPFloat(FloatSema1);
8987     const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8988     return APValue(std::move(First), readAPFloat(FloatSema2));
8989   }
8990   case APValue::LValue:
8991   case APValue::Vector:
8992   case APValue::Array:
8993   case APValue::Struct:
8994   case APValue::Union:
8995   case APValue::MemberPointer:
8996   case APValue::AddrLabelDiff:
8997     // TODO : Handle all these APValue::ValueKind.
8998     return APValue();
8999   }
9000   llvm_unreachable("Invalid APValue::ValueKind");
9001 }
9002 
9003 /// Read a floating-point value
9004 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9005   return llvm::APFloat(Sem, readAPInt());
9006 }
9007 
9008 // Read a string
9009 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9010   unsigned Len = Record[Idx++];
9011   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9012   Idx += Len;
9013   return Result;
9014 }
9015 
9016 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9017                                 unsigned &Idx) {
9018   std::string Filename = ReadString(Record, Idx);
9019   ResolveImportedPath(F, Filename);
9020   return Filename;
9021 }
9022 
9023 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9024                                 const RecordData &Record, unsigned &Idx) {
9025   std::string Filename = ReadString(Record, Idx);
9026   if (!BaseDirectory.empty())
9027     ResolveImportedPath(Filename, BaseDirectory);
9028   return Filename;
9029 }
9030 
9031 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9032                                          unsigned &Idx) {
9033   unsigned Major = Record[Idx++];
9034   unsigned Minor = Record[Idx++];
9035   unsigned Subminor = Record[Idx++];
9036   if (Minor == 0)
9037     return VersionTuple(Major);
9038   if (Subminor == 0)
9039     return VersionTuple(Major, Minor - 1);
9040   return VersionTuple(Major, Minor - 1, Subminor - 1);
9041 }
9042 
9043 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9044                                           const RecordData &Record,
9045                                           unsigned &Idx) {
9046   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9047   return CXXTemporary::Create(getContext(), Decl);
9048 }
9049 
9050 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9051   return Diag(CurrentImportLoc, DiagID);
9052 }
9053 
9054 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9055   return Diags.Report(Loc, DiagID);
9056 }
9057 
9058 /// Retrieve the identifier table associated with the
9059 /// preprocessor.
9060 IdentifierTable &ASTReader::getIdentifierTable() {
9061   return PP.getIdentifierTable();
9062 }
9063 
9064 /// Record that the given ID maps to the given switch-case
9065 /// statement.
9066 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9067   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9068          "Already have a SwitchCase with this ID");
9069   (*CurrSwitchCaseStmts)[ID] = SC;
9070 }
9071 
9072 /// Retrieve the switch-case statement with the given ID.
9073 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9074   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9075   return (*CurrSwitchCaseStmts)[ID];
9076 }
9077 
9078 void ASTReader::ClearSwitchCaseIDs() {
9079   CurrSwitchCaseStmts->clear();
9080 }
9081 
9082 void ASTReader::ReadComments() {
9083   ASTContext &Context = getContext();
9084   std::vector<RawComment *> Comments;
9085   for (SmallVectorImpl<std::pair<BitstreamCursor,
9086                                  serialization::ModuleFile *>>::iterator
9087        I = CommentsCursors.begin(),
9088        E = CommentsCursors.end();
9089        I != E; ++I) {
9090     Comments.clear();
9091     BitstreamCursor &Cursor = I->first;
9092     serialization::ModuleFile &F = *I->second;
9093     SavedStreamPosition SavedPosition(Cursor);
9094 
9095     RecordData Record;
9096     while (true) {
9097       Expected<llvm::BitstreamEntry> MaybeEntry =
9098           Cursor.advanceSkippingSubblocks(
9099               BitstreamCursor::AF_DontPopBlockAtEnd);
9100       if (!MaybeEntry) {
9101         Error(MaybeEntry.takeError());
9102         return;
9103       }
9104       llvm::BitstreamEntry Entry = MaybeEntry.get();
9105 
9106       switch (Entry.Kind) {
9107       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9108       case llvm::BitstreamEntry::Error:
9109         Error("malformed block record in AST file");
9110         return;
9111       case llvm::BitstreamEntry::EndBlock:
9112         goto NextCursor;
9113       case llvm::BitstreamEntry::Record:
9114         // The interesting case.
9115         break;
9116       }
9117 
9118       // Read a record.
9119       Record.clear();
9120       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9121       if (!MaybeComment) {
9122         Error(MaybeComment.takeError());
9123         return;
9124       }
9125       switch ((CommentRecordTypes)MaybeComment.get()) {
9126       case COMMENTS_RAW_COMMENT: {
9127         unsigned Idx = 0;
9128         SourceRange SR = ReadSourceRange(F, Record, Idx);
9129         RawComment::CommentKind Kind =
9130             (RawComment::CommentKind) Record[Idx++];
9131         bool IsTrailingComment = Record[Idx++];
9132         bool IsAlmostTrailingComment = Record[Idx++];
9133         Comments.push_back(new (Context) RawComment(
9134             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9135         break;
9136       }
9137       }
9138     }
9139   NextCursor:
9140     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9141         FileToOffsetToComment;
9142     for (RawComment *C : Comments) {
9143       SourceLocation CommentLoc = C->getBeginLoc();
9144       if (CommentLoc.isValid()) {
9145         std::pair<FileID, unsigned> Loc =
9146             SourceMgr.getDecomposedLoc(CommentLoc);
9147         if (Loc.first.isValid())
9148           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9149       }
9150     }
9151   }
9152 }
9153 
9154 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9155                                 bool IncludeSystem, bool Complain,
9156                     llvm::function_ref<void(const serialization::InputFile &IF,
9157                                             bool isSystem)> Visitor) {
9158   unsigned NumUserInputs = MF.NumUserInputFiles;
9159   unsigned NumInputs = MF.InputFilesLoaded.size();
9160   assert(NumUserInputs <= NumInputs);
9161   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9162   for (unsigned I = 0; I < N; ++I) {
9163     bool IsSystem = I >= NumUserInputs;
9164     InputFile IF = getInputFile(MF, I+1, Complain);
9165     Visitor(IF, IsSystem);
9166   }
9167 }
9168 
9169 void ASTReader::visitTopLevelModuleMaps(
9170     serialization::ModuleFile &MF,
9171     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9172   unsigned NumInputs = MF.InputFilesLoaded.size();
9173   for (unsigned I = 0; I < NumInputs; ++I) {
9174     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9175     if (IFI.TopLevelModuleMap)
9176       // FIXME: This unnecessarily re-reads the InputFileInfo.
9177       if (auto *FE = getInputFile(MF, I + 1).getFile())
9178         Visitor(FE);
9179   }
9180 }
9181 
9182 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9183   // If we know the owning module, use it.
9184   if (Module *M = D->getImportedOwningModule())
9185     return M->getFullModuleName();
9186 
9187   // Otherwise, use the name of the top-level module the decl is within.
9188   if (ModuleFile *M = getOwningModuleFile(D))
9189     return M->ModuleName;
9190 
9191   // Not from a module.
9192   return {};
9193 }
9194 
9195 void ASTReader::finishPendingActions() {
9196   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9197          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9198          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9199          !PendingUpdateRecords.empty()) {
9200     // If any identifiers with corresponding top-level declarations have
9201     // been loaded, load those declarations now.
9202     using TopLevelDeclsMap =
9203         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9204     TopLevelDeclsMap TopLevelDecls;
9205 
9206     while (!PendingIdentifierInfos.empty()) {
9207       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9208       SmallVector<uint32_t, 4> DeclIDs =
9209           std::move(PendingIdentifierInfos.back().second);
9210       PendingIdentifierInfos.pop_back();
9211 
9212       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9213     }
9214 
9215     // Load each function type that we deferred loading because it was a
9216     // deduced type that might refer to a local type declared within itself.
9217     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9218       auto *FD = PendingFunctionTypes[I].first;
9219       FD->setType(GetType(PendingFunctionTypes[I].second));
9220 
9221       // If we gave a function a deduced return type, remember that we need to
9222       // propagate that along the redeclaration chain.
9223       auto *DT = FD->getReturnType()->getContainedDeducedType();
9224       if (DT && DT->isDeduced())
9225         PendingDeducedTypeUpdates.insert(
9226             {FD->getCanonicalDecl(), FD->getReturnType()});
9227     }
9228     PendingFunctionTypes.clear();
9229 
9230     // For each decl chain that we wanted to complete while deserializing, mark
9231     // it as "still needs to be completed".
9232     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9233       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9234     }
9235     PendingIncompleteDeclChains.clear();
9236 
9237     // Load pending declaration chains.
9238     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9239       loadPendingDeclChain(PendingDeclChains[I].first,
9240                            PendingDeclChains[I].second);
9241     PendingDeclChains.clear();
9242 
9243     // Make the most recent of the top-level declarations visible.
9244     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9245            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9246       IdentifierInfo *II = TLD->first;
9247       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9248         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9249       }
9250     }
9251 
9252     // Load any pending macro definitions.
9253     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9254       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9255       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9256       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9257       // Initialize the macro history from chained-PCHs ahead of module imports.
9258       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9259            ++IDIdx) {
9260         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9261         if (!Info.M->isModule())
9262           resolvePendingMacro(II, Info);
9263       }
9264       // Handle module imports.
9265       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9266            ++IDIdx) {
9267         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9268         if (Info.M->isModule())
9269           resolvePendingMacro(II, Info);
9270       }
9271     }
9272     PendingMacroIDs.clear();
9273 
9274     // Wire up the DeclContexts for Decls that we delayed setting until
9275     // recursive loading is completed.
9276     while (!PendingDeclContextInfos.empty()) {
9277       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9278       PendingDeclContextInfos.pop_front();
9279       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9280       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9281       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9282     }
9283 
9284     // Perform any pending declaration updates.
9285     while (!PendingUpdateRecords.empty()) {
9286       auto Update = PendingUpdateRecords.pop_back_val();
9287       ReadingKindTracker ReadingKind(Read_Decl, *this);
9288       loadDeclUpdateRecords(Update);
9289     }
9290   }
9291 
9292   // At this point, all update records for loaded decls are in place, so any
9293   // fake class definitions should have become real.
9294   assert(PendingFakeDefinitionData.empty() &&
9295          "faked up a class definition but never saw the real one");
9296 
9297   // If we deserialized any C++ or Objective-C class definitions, any
9298   // Objective-C protocol definitions, or any redeclarable templates, make sure
9299   // that all redeclarations point to the definitions. Note that this can only
9300   // happen now, after the redeclaration chains have been fully wired.
9301   for (Decl *D : PendingDefinitions) {
9302     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9303       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9304         // Make sure that the TagType points at the definition.
9305         const_cast<TagType*>(TagT)->decl = TD;
9306       }
9307 
9308       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9309         for (auto *R = getMostRecentExistingDecl(RD); R;
9310              R = R->getPreviousDecl()) {
9311           assert((R == D) ==
9312                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9313                  "declaration thinks it's the definition but it isn't");
9314           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9315         }
9316       }
9317 
9318       continue;
9319     }
9320 
9321     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9322       // Make sure that the ObjCInterfaceType points at the definition.
9323       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9324         ->Decl = ID;
9325 
9326       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9327         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9328 
9329       continue;
9330     }
9331 
9332     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9333       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9334         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9335 
9336       continue;
9337     }
9338 
9339     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9340     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9341       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9342   }
9343   PendingDefinitions.clear();
9344 
9345   // Load the bodies of any functions or methods we've encountered. We do
9346   // this now (delayed) so that we can be sure that the declaration chains
9347   // have been fully wired up (hasBody relies on this).
9348   // FIXME: We shouldn't require complete redeclaration chains here.
9349   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9350                                PBEnd = PendingBodies.end();
9351        PB != PBEnd; ++PB) {
9352     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9353       // For a function defined inline within a class template, force the
9354       // canonical definition to be the one inside the canonical definition of
9355       // the template. This ensures that we instantiate from a correct view
9356       // of the template.
9357       //
9358       // Sadly we can't do this more generally: we can't be sure that all
9359       // copies of an arbitrary class definition will have the same members
9360       // defined (eg, some member functions may not be instantiated, and some
9361       // special members may or may not have been implicitly defined).
9362       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9363         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9364           continue;
9365 
9366       // FIXME: Check for =delete/=default?
9367       // FIXME: Complain about ODR violations here?
9368       const FunctionDecl *Defn = nullptr;
9369       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9370         FD->setLazyBody(PB->second);
9371       } else {
9372         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9373         mergeDefinitionVisibility(NonConstDefn, FD);
9374 
9375         if (!FD->isLateTemplateParsed() &&
9376             !NonConstDefn->isLateTemplateParsed() &&
9377             FD->getODRHash() != NonConstDefn->getODRHash()) {
9378           if (!isa<CXXMethodDecl>(FD)) {
9379             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9380           } else if (FD->getLexicalParent()->isFileContext() &&
9381                      NonConstDefn->getLexicalParent()->isFileContext()) {
9382             // Only diagnose out-of-line method definitions.  If they are
9383             // in class definitions, then an error will be generated when
9384             // processing the class bodies.
9385             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9386           }
9387         }
9388       }
9389       continue;
9390     }
9391 
9392     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9393     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9394       MD->setLazyBody(PB->second);
9395   }
9396   PendingBodies.clear();
9397 
9398   // Do some cleanup.
9399   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9400     getContext().deduplicateMergedDefinitonsFor(ND);
9401   PendingMergedDefinitionsToDeduplicate.clear();
9402 }
9403 
9404 void ASTReader::diagnoseOdrViolations() {
9405   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9406       PendingFunctionOdrMergeFailures.empty() &&
9407       PendingEnumOdrMergeFailures.empty())
9408     return;
9409 
9410   // Trigger the import of the full definition of each class that had any
9411   // odr-merging problems, so we can produce better diagnostics for them.
9412   // These updates may in turn find and diagnose some ODR failures, so take
9413   // ownership of the set first.
9414   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9415   PendingOdrMergeFailures.clear();
9416   for (auto &Merge : OdrMergeFailures) {
9417     Merge.first->buildLookup();
9418     Merge.first->decls_begin();
9419     Merge.first->bases_begin();
9420     Merge.first->vbases_begin();
9421     for (auto &RecordPair : Merge.second) {
9422       auto *RD = RecordPair.first;
9423       RD->decls_begin();
9424       RD->bases_begin();
9425       RD->vbases_begin();
9426     }
9427   }
9428 
9429   // Trigger the import of functions.
9430   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9431   PendingFunctionOdrMergeFailures.clear();
9432   for (auto &Merge : FunctionOdrMergeFailures) {
9433     Merge.first->buildLookup();
9434     Merge.first->decls_begin();
9435     Merge.first->getBody();
9436     for (auto &FD : Merge.second) {
9437       FD->buildLookup();
9438       FD->decls_begin();
9439       FD->getBody();
9440     }
9441   }
9442 
9443   // Trigger the import of enums.
9444   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9445   PendingEnumOdrMergeFailures.clear();
9446   for (auto &Merge : EnumOdrMergeFailures) {
9447     Merge.first->decls_begin();
9448     for (auto &Enum : Merge.second) {
9449       Enum->decls_begin();
9450     }
9451   }
9452 
9453   // For each declaration from a merged context, check that the canonical
9454   // definition of that context also contains a declaration of the same
9455   // entity.
9456   //
9457   // Caution: this loop does things that might invalidate iterators into
9458   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9459   while (!PendingOdrMergeChecks.empty()) {
9460     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9461 
9462     // FIXME: Skip over implicit declarations for now. This matters for things
9463     // like implicitly-declared special member functions. This isn't entirely
9464     // correct; we can end up with multiple unmerged declarations of the same
9465     // implicit entity.
9466     if (D->isImplicit())
9467       continue;
9468 
9469     DeclContext *CanonDef = D->getDeclContext();
9470 
9471     bool Found = false;
9472     const Decl *DCanon = D->getCanonicalDecl();
9473 
9474     for (auto RI : D->redecls()) {
9475       if (RI->getLexicalDeclContext() == CanonDef) {
9476         Found = true;
9477         break;
9478       }
9479     }
9480     if (Found)
9481       continue;
9482 
9483     // Quick check failed, time to do the slow thing. Note, we can't just
9484     // look up the name of D in CanonDef here, because the member that is
9485     // in CanonDef might not be found by name lookup (it might have been
9486     // replaced by a more recent declaration in the lookup table), and we
9487     // can't necessarily find it in the redeclaration chain because it might
9488     // be merely mergeable, not redeclarable.
9489     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9490     for (auto *CanonMember : CanonDef->decls()) {
9491       if (CanonMember->getCanonicalDecl() == DCanon) {
9492         // This can happen if the declaration is merely mergeable and not
9493         // actually redeclarable (we looked for redeclarations earlier).
9494         //
9495         // FIXME: We should be able to detect this more efficiently, without
9496         // pulling in all of the members of CanonDef.
9497         Found = true;
9498         break;
9499       }
9500       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9501         if (ND->getDeclName() == D->getDeclName())
9502           Candidates.push_back(ND);
9503     }
9504 
9505     if (!Found) {
9506       // The AST doesn't like TagDecls becoming invalid after they've been
9507       // completed. We only really need to mark FieldDecls as invalid here.
9508       if (!isa<TagDecl>(D))
9509         D->setInvalidDecl();
9510 
9511       // Ensure we don't accidentally recursively enter deserialization while
9512       // we're producing our diagnostic.
9513       Deserializing RecursionGuard(this);
9514 
9515       std::string CanonDefModule =
9516           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9517       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9518         << D << getOwningModuleNameForDiagnostic(D)
9519         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9520 
9521       if (Candidates.empty())
9522         Diag(cast<Decl>(CanonDef)->getLocation(),
9523              diag::note_module_odr_violation_no_possible_decls) << D;
9524       else {
9525         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9526           Diag(Candidates[I]->getLocation(),
9527                diag::note_module_odr_violation_possible_decl)
9528             << Candidates[I];
9529       }
9530 
9531       DiagnosedOdrMergeFailures.insert(CanonDef);
9532     }
9533   }
9534 
9535   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9536       EnumOdrMergeFailures.empty())
9537     return;
9538 
9539   // Ensure we don't accidentally recursively enter deserialization while
9540   // we're producing our diagnostics.
9541   Deserializing RecursionGuard(this);
9542 
9543   // Common code for hashing helpers.
9544   ODRHash Hash;
9545   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9546     Hash.clear();
9547     Hash.AddQualType(Ty);
9548     return Hash.CalculateHash();
9549   };
9550 
9551   auto ComputeODRHash = [&Hash](const Stmt *S) {
9552     assert(S);
9553     Hash.clear();
9554     Hash.AddStmt(S);
9555     return Hash.CalculateHash();
9556   };
9557 
9558   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9559     assert(D);
9560     Hash.clear();
9561     Hash.AddSubDecl(D);
9562     return Hash.CalculateHash();
9563   };
9564 
9565   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9566     Hash.clear();
9567     Hash.AddTemplateArgument(TA);
9568     return Hash.CalculateHash();
9569   };
9570 
9571   auto ComputeTemplateParameterListODRHash =
9572       [&Hash](const TemplateParameterList *TPL) {
9573         assert(TPL);
9574         Hash.clear();
9575         Hash.AddTemplateParameterList(TPL);
9576         return Hash.CalculateHash();
9577       };
9578 
9579   // Used with err_module_odr_violation_mismatch_decl and
9580   // note_module_odr_violation_mismatch_decl
9581   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9582   enum ODRMismatchDecl {
9583     EndOfClass,
9584     PublicSpecifer,
9585     PrivateSpecifer,
9586     ProtectedSpecifer,
9587     StaticAssert,
9588     Field,
9589     CXXMethod,
9590     TypeAlias,
9591     TypeDef,
9592     Var,
9593     Friend,
9594     FunctionTemplate,
9595     Other
9596   };
9597 
9598   // Used with err_module_odr_violation_mismatch_decl_diff and
9599   // note_module_odr_violation_mismatch_decl_diff
9600   enum ODRMismatchDeclDifference {
9601     StaticAssertCondition,
9602     StaticAssertMessage,
9603     StaticAssertOnlyMessage,
9604     FieldName,
9605     FieldTypeName,
9606     FieldSingleBitField,
9607     FieldDifferentWidthBitField,
9608     FieldSingleMutable,
9609     FieldSingleInitializer,
9610     FieldDifferentInitializers,
9611     MethodName,
9612     MethodDeleted,
9613     MethodDefaulted,
9614     MethodVirtual,
9615     MethodStatic,
9616     MethodVolatile,
9617     MethodConst,
9618     MethodInline,
9619     MethodNumberParameters,
9620     MethodParameterType,
9621     MethodParameterName,
9622     MethodParameterSingleDefaultArgument,
9623     MethodParameterDifferentDefaultArgument,
9624     MethodNoTemplateArguments,
9625     MethodDifferentNumberTemplateArguments,
9626     MethodDifferentTemplateArgument,
9627     MethodSingleBody,
9628     MethodDifferentBody,
9629     TypedefName,
9630     TypedefType,
9631     VarName,
9632     VarType,
9633     VarSingleInitializer,
9634     VarDifferentInitializer,
9635     VarConstexpr,
9636     FriendTypeFunction,
9637     FriendType,
9638     FriendFunction,
9639     FunctionTemplateDifferentNumberParameters,
9640     FunctionTemplateParameterDifferentKind,
9641     FunctionTemplateParameterName,
9642     FunctionTemplateParameterSingleDefaultArgument,
9643     FunctionTemplateParameterDifferentDefaultArgument,
9644     FunctionTemplateParameterDifferentType,
9645     FunctionTemplatePackParameter,
9646   };
9647 
9648   // These lambdas have the common portions of the ODR diagnostics.  This
9649   // has the same return as Diag(), so addition parameters can be passed
9650   // in with operator<<
9651   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9652                                  SourceLocation Loc, SourceRange Range,
9653                                  ODRMismatchDeclDifference DiffType) {
9654     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9655            << FirstRecord << FirstModule.empty() << FirstModule << Range
9656            << DiffType;
9657   };
9658   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9659                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9660     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9661            << SecondModule << Range << DiffType;
9662   };
9663 
9664   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9665                        &ComputeQualTypeODRHash, &ComputeODRHash](
9666                           NamedDecl *FirstRecord, StringRef FirstModule,
9667                           StringRef SecondModule, FieldDecl *FirstField,
9668                           FieldDecl *SecondField) {
9669     IdentifierInfo *FirstII = FirstField->getIdentifier();
9670     IdentifierInfo *SecondII = SecondField->getIdentifier();
9671     if (FirstII->getName() != SecondII->getName()) {
9672       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9673                        FirstField->getSourceRange(), FieldName)
9674           << FirstII;
9675       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9676                       SecondField->getSourceRange(), FieldName)
9677           << SecondII;
9678 
9679       return true;
9680     }
9681 
9682     assert(getContext().hasSameType(FirstField->getType(),
9683                                     SecondField->getType()));
9684 
9685     QualType FirstType = FirstField->getType();
9686     QualType SecondType = SecondField->getType();
9687     if (ComputeQualTypeODRHash(FirstType) !=
9688         ComputeQualTypeODRHash(SecondType)) {
9689       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9690                        FirstField->getSourceRange(), FieldTypeName)
9691           << FirstII << FirstType;
9692       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9693                       SecondField->getSourceRange(), FieldTypeName)
9694           << SecondII << SecondType;
9695 
9696       return true;
9697     }
9698 
9699     const bool IsFirstBitField = FirstField->isBitField();
9700     const bool IsSecondBitField = SecondField->isBitField();
9701     if (IsFirstBitField != IsSecondBitField) {
9702       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9703                        FirstField->getSourceRange(), FieldSingleBitField)
9704           << FirstII << IsFirstBitField;
9705       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9706                       SecondField->getSourceRange(), FieldSingleBitField)
9707           << SecondII << IsSecondBitField;
9708       return true;
9709     }
9710 
9711     if (IsFirstBitField && IsSecondBitField) {
9712       unsigned FirstBitWidthHash =
9713           ComputeODRHash(FirstField->getBitWidth());
9714       unsigned SecondBitWidthHash =
9715           ComputeODRHash(SecondField->getBitWidth());
9716       if (FirstBitWidthHash != SecondBitWidthHash) {
9717         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9718                          FirstField->getSourceRange(),
9719                          FieldDifferentWidthBitField)
9720             << FirstII << FirstField->getBitWidth()->getSourceRange();
9721         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9722                         SecondField->getSourceRange(),
9723                         FieldDifferentWidthBitField)
9724             << SecondII << SecondField->getBitWidth()->getSourceRange();
9725         return true;
9726       }
9727     }
9728 
9729     if (!PP.getLangOpts().CPlusPlus)
9730       return false;
9731 
9732     const bool IsFirstMutable = FirstField->isMutable();
9733     const bool IsSecondMutable = SecondField->isMutable();
9734     if (IsFirstMutable != IsSecondMutable) {
9735       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9736                        FirstField->getSourceRange(), FieldSingleMutable)
9737           << FirstII << IsFirstMutable;
9738       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9739                       SecondField->getSourceRange(), FieldSingleMutable)
9740           << SecondII << IsSecondMutable;
9741       return true;
9742     }
9743 
9744     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9745     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9746     if ((!FirstInitializer && SecondInitializer) ||
9747         (FirstInitializer && !SecondInitializer)) {
9748       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9749                        FirstField->getSourceRange(), FieldSingleInitializer)
9750           << FirstII << (FirstInitializer != nullptr);
9751       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9752                       SecondField->getSourceRange(), FieldSingleInitializer)
9753           << SecondII << (SecondInitializer != nullptr);
9754       return true;
9755     }
9756 
9757     if (FirstInitializer && SecondInitializer) {
9758       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9759       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9760       if (FirstInitHash != SecondInitHash) {
9761         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9762                          FirstField->getSourceRange(),
9763                          FieldDifferentInitializers)
9764             << FirstII << FirstInitializer->getSourceRange();
9765         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9766                         SecondField->getSourceRange(),
9767                         FieldDifferentInitializers)
9768             << SecondII << SecondInitializer->getSourceRange();
9769         return true;
9770       }
9771     }
9772 
9773     return false;
9774   };
9775 
9776   auto ODRDiagTypeDefOrAlias =
9777       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9778           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9779           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9780           bool IsTypeAlias) {
9781         auto FirstName = FirstTD->getDeclName();
9782         auto SecondName = SecondTD->getDeclName();
9783         if (FirstName != SecondName) {
9784           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9785                            FirstTD->getSourceRange(), TypedefName)
9786               << IsTypeAlias << FirstName;
9787           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9788                           SecondTD->getSourceRange(), TypedefName)
9789               << IsTypeAlias << SecondName;
9790           return true;
9791         }
9792 
9793         QualType FirstType = FirstTD->getUnderlyingType();
9794         QualType SecondType = SecondTD->getUnderlyingType();
9795         if (ComputeQualTypeODRHash(FirstType) !=
9796             ComputeQualTypeODRHash(SecondType)) {
9797           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9798                            FirstTD->getSourceRange(), TypedefType)
9799               << IsTypeAlias << FirstName << FirstType;
9800           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9801                           SecondTD->getSourceRange(), TypedefType)
9802               << IsTypeAlias << SecondName << SecondType;
9803           return true;
9804         }
9805 
9806         return false;
9807   };
9808 
9809   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9810                      &ComputeQualTypeODRHash, &ComputeODRHash,
9811                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9812                            StringRef SecondModule, VarDecl *FirstVD,
9813                            VarDecl *SecondVD) {
9814     auto FirstName = FirstVD->getDeclName();
9815     auto SecondName = SecondVD->getDeclName();
9816     if (FirstName != SecondName) {
9817       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9818                        FirstVD->getSourceRange(), VarName)
9819           << FirstName;
9820       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9821                       SecondVD->getSourceRange(), VarName)
9822           << SecondName;
9823       return true;
9824     }
9825 
9826     QualType FirstType = FirstVD->getType();
9827     QualType SecondType = SecondVD->getType();
9828     if (ComputeQualTypeODRHash(FirstType) !=
9829         ComputeQualTypeODRHash(SecondType)) {
9830       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9831                        FirstVD->getSourceRange(), VarType)
9832           << FirstName << FirstType;
9833       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9834                       SecondVD->getSourceRange(), VarType)
9835           << SecondName << SecondType;
9836       return true;
9837     }
9838 
9839     if (!PP.getLangOpts().CPlusPlus)
9840       return false;
9841 
9842     const Expr *FirstInit = FirstVD->getInit();
9843     const Expr *SecondInit = SecondVD->getInit();
9844     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9845       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9846                        FirstVD->getSourceRange(), VarSingleInitializer)
9847           << FirstName << (FirstInit == nullptr)
9848           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9849       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9850                       SecondVD->getSourceRange(), VarSingleInitializer)
9851           << SecondName << (SecondInit == nullptr)
9852           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9853       return true;
9854     }
9855 
9856     if (FirstInit && SecondInit &&
9857         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9858       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9859                        FirstVD->getSourceRange(), VarDifferentInitializer)
9860           << FirstName << FirstInit->getSourceRange();
9861       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9862                       SecondVD->getSourceRange(), VarDifferentInitializer)
9863           << SecondName << SecondInit->getSourceRange();
9864       return true;
9865     }
9866 
9867     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9868     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9869     if (FirstIsConstexpr != SecondIsConstexpr) {
9870       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9871                        FirstVD->getSourceRange(), VarConstexpr)
9872           << FirstName << FirstIsConstexpr;
9873       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9874                       SecondVD->getSourceRange(), VarConstexpr)
9875           << SecondName << SecondIsConstexpr;
9876       return true;
9877     }
9878     return false;
9879   };
9880 
9881   auto DifferenceSelector = [](Decl *D) {
9882     assert(D && "valid Decl required");
9883     switch (D->getKind()) {
9884     default:
9885       return Other;
9886     case Decl::AccessSpec:
9887       switch (D->getAccess()) {
9888       case AS_public:
9889         return PublicSpecifer;
9890       case AS_private:
9891         return PrivateSpecifer;
9892       case AS_protected:
9893         return ProtectedSpecifer;
9894       case AS_none:
9895         break;
9896       }
9897       llvm_unreachable("Invalid access specifier");
9898     case Decl::StaticAssert:
9899       return StaticAssert;
9900     case Decl::Field:
9901       return Field;
9902     case Decl::CXXMethod:
9903     case Decl::CXXConstructor:
9904     case Decl::CXXDestructor:
9905       return CXXMethod;
9906     case Decl::TypeAlias:
9907       return TypeAlias;
9908     case Decl::Typedef:
9909       return TypeDef;
9910     case Decl::Var:
9911       return Var;
9912     case Decl::Friend:
9913       return Friend;
9914     case Decl::FunctionTemplate:
9915       return FunctionTemplate;
9916     }
9917   };
9918 
9919   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9920   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9921                                                  RecordDecl *Record,
9922                                                  const DeclContext *DC) {
9923     for (auto *D : Record->decls()) {
9924       if (!ODRHash::isDeclToBeProcessed(D, DC))
9925         continue;
9926       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9927     }
9928   };
9929 
9930   struct DiffResult {
9931     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9932     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9933   };
9934 
9935   // If there is a diagnoseable difference, FirstDiffType and
9936   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9937   // filled in if not EndOfClass.
9938   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9939                                              DeclHashes &SecondHashes) {
9940     DiffResult DR;
9941     auto FirstIt = FirstHashes.begin();
9942     auto SecondIt = SecondHashes.begin();
9943     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9944       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9945           FirstIt->second == SecondIt->second) {
9946         ++FirstIt;
9947         ++SecondIt;
9948         continue;
9949       }
9950 
9951       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9952       DR.SecondDecl =
9953           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9954 
9955       DR.FirstDiffType =
9956           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9957       DR.SecondDiffType =
9958           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9959       return DR;
9960     }
9961     return DR;
9962   };
9963 
9964   // Use this to diagnose that an unexpected Decl was encountered
9965   // or no difference was detected. This causes a generic error
9966   // message to be emitted.
9967   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9968                                       StringRef FirstModule,
9969                                       NamedDecl *SecondRecord,
9970                                       StringRef SecondModule) {
9971     Diag(FirstRecord->getLocation(),
9972          diag::err_module_odr_violation_different_definitions)
9973         << FirstRecord << FirstModule.empty() << FirstModule;
9974 
9975     if (DR.FirstDecl) {
9976       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9977           << FirstRecord << DR.FirstDecl->getSourceRange();
9978     }
9979 
9980     Diag(SecondRecord->getLocation(),
9981          diag::note_module_odr_violation_different_definitions)
9982         << SecondModule;
9983 
9984     if (DR.SecondDecl) {
9985       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9986           << DR.SecondDecl->getSourceRange();
9987     }
9988   };
9989 
9990   auto DiagnoseODRMismatch =
9991       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9992              NamedDecl *SecondRecord, StringRef SecondModule) {
9993         SourceLocation FirstLoc;
9994         SourceRange FirstRange;
9995         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9996         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9997           FirstLoc = FirstTag->getBraceRange().getEnd();
9998         } else {
9999           FirstLoc = DR.FirstDecl->getLocation();
10000           FirstRange = DR.FirstDecl->getSourceRange();
10001         }
10002         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10003             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10004             << DR.FirstDiffType;
10005 
10006         SourceLocation SecondLoc;
10007         SourceRange SecondRange;
10008         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10009         if (DR.SecondDiffType == EndOfClass && SecondTag) {
10010           SecondLoc = SecondTag->getBraceRange().getEnd();
10011         } else {
10012           SecondLoc = DR.SecondDecl->getLocation();
10013           SecondRange = DR.SecondDecl->getSourceRange();
10014         }
10015         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10016             << SecondModule << SecondRange << DR.SecondDiffType;
10017       };
10018 
10019   // Issue any pending ODR-failure diagnostics.
10020   for (auto &Merge : OdrMergeFailures) {
10021     // If we've already pointed out a specific problem with this class, don't
10022     // bother issuing a general "something's different" diagnostic.
10023     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10024       continue;
10025 
10026     bool Diagnosed = false;
10027     CXXRecordDecl *FirstRecord = Merge.first;
10028     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10029     for (auto &RecordPair : Merge.second) {
10030       CXXRecordDecl *SecondRecord = RecordPair.first;
10031       // Multiple different declarations got merged together; tell the user
10032       // where they came from.
10033       if (FirstRecord == SecondRecord)
10034         continue;
10035 
10036       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10037 
10038       auto *FirstDD = FirstRecord->DefinitionData;
10039       auto *SecondDD = RecordPair.second;
10040 
10041       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10042 
10043       // Diagnostics from DefinitionData are emitted here.
10044       if (FirstDD != SecondDD) {
10045         enum ODRDefinitionDataDifference {
10046           NumBases,
10047           NumVBases,
10048           BaseType,
10049           BaseVirtual,
10050           BaseAccess,
10051         };
10052         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10053                                  this](SourceLocation Loc, SourceRange Range,
10054                                        ODRDefinitionDataDifference DiffType) {
10055           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10056                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10057                  << DiffType;
10058         };
10059         auto ODRDiagBaseNote = [&SecondModule,
10060                                 this](SourceLocation Loc, SourceRange Range,
10061                                       ODRDefinitionDataDifference DiffType) {
10062           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10063                  << SecondModule << Range << DiffType;
10064         };
10065 
10066         unsigned FirstNumBases = FirstDD->NumBases;
10067         unsigned FirstNumVBases = FirstDD->NumVBases;
10068         unsigned SecondNumBases = SecondDD->NumBases;
10069         unsigned SecondNumVBases = SecondDD->NumVBases;
10070 
10071         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10072           unsigned NumBases = DD->NumBases;
10073           if (NumBases == 0) return SourceRange();
10074           auto bases = DD->bases();
10075           return SourceRange(bases[0].getBeginLoc(),
10076                              bases[NumBases - 1].getEndLoc());
10077         };
10078 
10079         if (FirstNumBases != SecondNumBases) {
10080           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10081                            NumBases)
10082               << FirstNumBases;
10083           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10084                           NumBases)
10085               << SecondNumBases;
10086           Diagnosed = true;
10087           break;
10088         }
10089 
10090         if (FirstNumVBases != SecondNumVBases) {
10091           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10092                            NumVBases)
10093               << FirstNumVBases;
10094           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10095                           NumVBases)
10096               << SecondNumVBases;
10097           Diagnosed = true;
10098           break;
10099         }
10100 
10101         auto FirstBases = FirstDD->bases();
10102         auto SecondBases = SecondDD->bases();
10103         unsigned i = 0;
10104         for (i = 0; i < FirstNumBases; ++i) {
10105           auto FirstBase = FirstBases[i];
10106           auto SecondBase = SecondBases[i];
10107           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10108               ComputeQualTypeODRHash(SecondBase.getType())) {
10109             ODRDiagBaseError(FirstRecord->getLocation(),
10110                              FirstBase.getSourceRange(), BaseType)
10111                 << (i + 1) << FirstBase.getType();
10112             ODRDiagBaseNote(SecondRecord->getLocation(),
10113                             SecondBase.getSourceRange(), BaseType)
10114                 << (i + 1) << SecondBase.getType();
10115             break;
10116           }
10117 
10118           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10119             ODRDiagBaseError(FirstRecord->getLocation(),
10120                              FirstBase.getSourceRange(), BaseVirtual)
10121                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10122             ODRDiagBaseNote(SecondRecord->getLocation(),
10123                             SecondBase.getSourceRange(), BaseVirtual)
10124                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10125             break;
10126           }
10127 
10128           if (FirstBase.getAccessSpecifierAsWritten() !=
10129               SecondBase.getAccessSpecifierAsWritten()) {
10130             ODRDiagBaseError(FirstRecord->getLocation(),
10131                              FirstBase.getSourceRange(), BaseAccess)
10132                 << (i + 1) << FirstBase.getType()
10133                 << (int)FirstBase.getAccessSpecifierAsWritten();
10134             ODRDiagBaseNote(SecondRecord->getLocation(),
10135                             SecondBase.getSourceRange(), BaseAccess)
10136                 << (i + 1) << SecondBase.getType()
10137                 << (int)SecondBase.getAccessSpecifierAsWritten();
10138             break;
10139           }
10140         }
10141 
10142         if (i != FirstNumBases) {
10143           Diagnosed = true;
10144           break;
10145         }
10146       }
10147 
10148       const ClassTemplateDecl *FirstTemplate =
10149           FirstRecord->getDescribedClassTemplate();
10150       const ClassTemplateDecl *SecondTemplate =
10151           SecondRecord->getDescribedClassTemplate();
10152 
10153       assert(!FirstTemplate == !SecondTemplate &&
10154              "Both pointers should be null or non-null");
10155 
10156       enum ODRTemplateDifference {
10157         ParamEmptyName,
10158         ParamName,
10159         ParamSingleDefaultArgument,
10160         ParamDifferentDefaultArgument,
10161       };
10162 
10163       if (FirstTemplate && SecondTemplate) {
10164         DeclHashes FirstTemplateHashes;
10165         DeclHashes SecondTemplateHashes;
10166 
10167         auto PopulateTemplateParameterHashs =
10168             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10169                                      const ClassTemplateDecl *TD) {
10170               for (auto *D : TD->getTemplateParameters()->asArray()) {
10171                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10172               }
10173             };
10174 
10175         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10176         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10177 
10178         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10179                "Number of template parameters should be equal.");
10180 
10181         auto FirstIt = FirstTemplateHashes.begin();
10182         auto FirstEnd = FirstTemplateHashes.end();
10183         auto SecondIt = SecondTemplateHashes.begin();
10184         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10185           if (FirstIt->second == SecondIt->second)
10186             continue;
10187 
10188           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10189                                           SourceLocation Loc, SourceRange Range,
10190                                           ODRTemplateDifference DiffType) {
10191             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10192                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10193                    << DiffType;
10194           };
10195           auto ODRDiagTemplateNote = [&SecondModule, this](
10196                                          SourceLocation Loc, SourceRange Range,
10197                                          ODRTemplateDifference DiffType) {
10198             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10199                    << SecondModule << Range << DiffType;
10200           };
10201 
10202           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10203           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10204 
10205           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10206                  "Parameter Decl's should be the same kind.");
10207 
10208           DeclarationName FirstName = FirstDecl->getDeclName();
10209           DeclarationName SecondName = SecondDecl->getDeclName();
10210 
10211           if (FirstName != SecondName) {
10212             const bool FirstNameEmpty =
10213                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10214             const bool SecondNameEmpty =
10215                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10216             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10217                    "Both template parameters cannot be unnamed.");
10218             ODRDiagTemplateError(FirstDecl->getLocation(),
10219                                  FirstDecl->getSourceRange(),
10220                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10221                 << FirstName;
10222             ODRDiagTemplateNote(SecondDecl->getLocation(),
10223                                 SecondDecl->getSourceRange(),
10224                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10225                 << SecondName;
10226             break;
10227           }
10228 
10229           switch (FirstDecl->getKind()) {
10230           default:
10231             llvm_unreachable("Invalid template parameter type.");
10232           case Decl::TemplateTypeParm: {
10233             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10234             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10235             const bool HasFirstDefaultArgument =
10236                 FirstParam->hasDefaultArgument() &&
10237                 !FirstParam->defaultArgumentWasInherited();
10238             const bool HasSecondDefaultArgument =
10239                 SecondParam->hasDefaultArgument() &&
10240                 !SecondParam->defaultArgumentWasInherited();
10241 
10242             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10243               ODRDiagTemplateError(FirstDecl->getLocation(),
10244                                    FirstDecl->getSourceRange(),
10245                                    ParamSingleDefaultArgument)
10246                   << HasFirstDefaultArgument;
10247               ODRDiagTemplateNote(SecondDecl->getLocation(),
10248                                   SecondDecl->getSourceRange(),
10249                                   ParamSingleDefaultArgument)
10250                   << HasSecondDefaultArgument;
10251               break;
10252             }
10253 
10254             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10255                    "Expecting default arguments.");
10256 
10257             ODRDiagTemplateError(FirstDecl->getLocation(),
10258                                  FirstDecl->getSourceRange(),
10259                                  ParamDifferentDefaultArgument);
10260             ODRDiagTemplateNote(SecondDecl->getLocation(),
10261                                 SecondDecl->getSourceRange(),
10262                                 ParamDifferentDefaultArgument);
10263 
10264             break;
10265           }
10266           case Decl::NonTypeTemplateParm: {
10267             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10268             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(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           case Decl::TemplateTemplateParm: {
10301             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10302             const auto *SecondParam =
10303                 cast<TemplateTemplateParmDecl>(SecondDecl);
10304             const bool HasFirstDefaultArgument =
10305                 FirstParam->hasDefaultArgument() &&
10306                 !FirstParam->defaultArgumentWasInherited();
10307             const bool HasSecondDefaultArgument =
10308                 SecondParam->hasDefaultArgument() &&
10309                 !SecondParam->defaultArgumentWasInherited();
10310 
10311             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10312               ODRDiagTemplateError(FirstDecl->getLocation(),
10313                                    FirstDecl->getSourceRange(),
10314                                    ParamSingleDefaultArgument)
10315                   << HasFirstDefaultArgument;
10316               ODRDiagTemplateNote(SecondDecl->getLocation(),
10317                                   SecondDecl->getSourceRange(),
10318                                   ParamSingleDefaultArgument)
10319                   << HasSecondDefaultArgument;
10320               break;
10321             }
10322 
10323             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10324                    "Expecting default arguments.");
10325 
10326             ODRDiagTemplateError(FirstDecl->getLocation(),
10327                                  FirstDecl->getSourceRange(),
10328                                  ParamDifferentDefaultArgument);
10329             ODRDiagTemplateNote(SecondDecl->getLocation(),
10330                                 SecondDecl->getSourceRange(),
10331                                 ParamDifferentDefaultArgument);
10332 
10333             break;
10334           }
10335           }
10336 
10337           break;
10338         }
10339 
10340         if (FirstIt != FirstEnd) {
10341           Diagnosed = true;
10342           break;
10343         }
10344       }
10345 
10346       DeclHashes FirstHashes;
10347       DeclHashes SecondHashes;
10348       const DeclContext *DC = FirstRecord;
10349       PopulateHashes(FirstHashes, FirstRecord, DC);
10350       PopulateHashes(SecondHashes, SecondRecord, DC);
10351 
10352       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10353       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10354       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10355       Decl *FirstDecl = DR.FirstDecl;
10356       Decl *SecondDecl = DR.SecondDecl;
10357 
10358       if (FirstDiffType == Other || SecondDiffType == Other) {
10359         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10360                               SecondModule);
10361         Diagnosed = true;
10362         break;
10363       }
10364 
10365       if (FirstDiffType != SecondDiffType) {
10366         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10367                             SecondModule);
10368         Diagnosed = true;
10369         break;
10370       }
10371 
10372       assert(FirstDiffType == SecondDiffType);
10373 
10374       switch (FirstDiffType) {
10375       case Other:
10376       case EndOfClass:
10377       case PublicSpecifer:
10378       case PrivateSpecifer:
10379       case ProtectedSpecifer:
10380         llvm_unreachable("Invalid diff type");
10381 
10382       case StaticAssert: {
10383         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10384         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10385 
10386         Expr *FirstExpr = FirstSA->getAssertExpr();
10387         Expr *SecondExpr = SecondSA->getAssertExpr();
10388         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10389         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10390         if (FirstODRHash != SecondODRHash) {
10391           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10392                            FirstExpr->getSourceRange(), StaticAssertCondition);
10393           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10394                           SecondExpr->getSourceRange(), StaticAssertCondition);
10395           Diagnosed = true;
10396           break;
10397         }
10398 
10399         StringLiteral *FirstStr = FirstSA->getMessage();
10400         StringLiteral *SecondStr = SecondSA->getMessage();
10401         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10402         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10403           SourceLocation FirstLoc, SecondLoc;
10404           SourceRange FirstRange, SecondRange;
10405           if (FirstStr) {
10406             FirstLoc = FirstStr->getBeginLoc();
10407             FirstRange = FirstStr->getSourceRange();
10408           } else {
10409             FirstLoc = FirstSA->getBeginLoc();
10410             FirstRange = FirstSA->getSourceRange();
10411           }
10412           if (SecondStr) {
10413             SecondLoc = SecondStr->getBeginLoc();
10414             SecondRange = SecondStr->getSourceRange();
10415           } else {
10416             SecondLoc = SecondSA->getBeginLoc();
10417             SecondRange = SecondSA->getSourceRange();
10418           }
10419           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10420                            StaticAssertOnlyMessage)
10421               << (FirstStr == nullptr);
10422           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10423                           StaticAssertOnlyMessage)
10424               << (SecondStr == nullptr);
10425           Diagnosed = true;
10426           break;
10427         }
10428 
10429         if (FirstStr && SecondStr &&
10430             FirstStr->getString() != SecondStr->getString()) {
10431           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10432                            FirstStr->getSourceRange(), StaticAssertMessage);
10433           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10434                           SecondStr->getSourceRange(), StaticAssertMessage);
10435           Diagnosed = true;
10436           break;
10437         }
10438         break;
10439       }
10440       case Field: {
10441         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10442                                  cast<FieldDecl>(FirstDecl),
10443                                  cast<FieldDecl>(SecondDecl));
10444         break;
10445       }
10446       case CXXMethod: {
10447         enum {
10448           DiagMethod,
10449           DiagConstructor,
10450           DiagDestructor,
10451         } FirstMethodType,
10452             SecondMethodType;
10453         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10454           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10455           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10456           return DiagMethod;
10457         };
10458         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10459         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10460         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10461         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10462         auto FirstName = FirstMethod->getDeclName();
10463         auto SecondName = SecondMethod->getDeclName();
10464         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10465           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10466                            FirstMethod->getSourceRange(), MethodName)
10467               << FirstMethodType << FirstName;
10468           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10469                           SecondMethod->getSourceRange(), MethodName)
10470               << SecondMethodType << SecondName;
10471 
10472           Diagnosed = true;
10473           break;
10474         }
10475 
10476         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10477         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10478         if (FirstDeleted != SecondDeleted) {
10479           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10480                            FirstMethod->getSourceRange(), MethodDeleted)
10481               << FirstMethodType << FirstName << FirstDeleted;
10482 
10483           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10484                           SecondMethod->getSourceRange(), MethodDeleted)
10485               << SecondMethodType << SecondName << SecondDeleted;
10486           Diagnosed = true;
10487           break;
10488         }
10489 
10490         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10491         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10492         if (FirstDefaulted != SecondDefaulted) {
10493           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10494                            FirstMethod->getSourceRange(), MethodDefaulted)
10495               << FirstMethodType << FirstName << FirstDefaulted;
10496 
10497           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10498                           SecondMethod->getSourceRange(), MethodDefaulted)
10499               << SecondMethodType << SecondName << SecondDefaulted;
10500           Diagnosed = true;
10501           break;
10502         }
10503 
10504         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10505         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10506         const bool FirstPure = FirstMethod->isPure();
10507         const bool SecondPure = SecondMethod->isPure();
10508         if ((FirstVirtual || SecondVirtual) &&
10509             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10510           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10511                            FirstMethod->getSourceRange(), MethodVirtual)
10512               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10513           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10514                           SecondMethod->getSourceRange(), MethodVirtual)
10515               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10516           Diagnosed = true;
10517           break;
10518         }
10519 
10520         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10521         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10522         // class needs to be checked instead.
10523         const auto FirstStorage = FirstMethod->getStorageClass();
10524         const auto SecondStorage = SecondMethod->getStorageClass();
10525         const bool FirstStatic = FirstStorage == SC_Static;
10526         const bool SecondStatic = SecondStorage == SC_Static;
10527         if (FirstStatic != SecondStatic) {
10528           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10529                            FirstMethod->getSourceRange(), MethodStatic)
10530               << FirstMethodType << FirstName << FirstStatic;
10531           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10532                           SecondMethod->getSourceRange(), MethodStatic)
10533               << SecondMethodType << SecondName << SecondStatic;
10534           Diagnosed = true;
10535           break;
10536         }
10537 
10538         const bool FirstVolatile = FirstMethod->isVolatile();
10539         const bool SecondVolatile = SecondMethod->isVolatile();
10540         if (FirstVolatile != SecondVolatile) {
10541           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10542                            FirstMethod->getSourceRange(), MethodVolatile)
10543               << FirstMethodType << FirstName << FirstVolatile;
10544           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10545                           SecondMethod->getSourceRange(), MethodVolatile)
10546               << SecondMethodType << SecondName << SecondVolatile;
10547           Diagnosed = true;
10548           break;
10549         }
10550 
10551         const bool FirstConst = FirstMethod->isConst();
10552         const bool SecondConst = SecondMethod->isConst();
10553         if (FirstConst != SecondConst) {
10554           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10555                            FirstMethod->getSourceRange(), MethodConst)
10556               << FirstMethodType << FirstName << FirstConst;
10557           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10558                           SecondMethod->getSourceRange(), MethodConst)
10559               << SecondMethodType << SecondName << SecondConst;
10560           Diagnosed = true;
10561           break;
10562         }
10563 
10564         const bool FirstInline = FirstMethod->isInlineSpecified();
10565         const bool SecondInline = SecondMethod->isInlineSpecified();
10566         if (FirstInline != SecondInline) {
10567           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10568                            FirstMethod->getSourceRange(), MethodInline)
10569               << FirstMethodType << FirstName << FirstInline;
10570           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10571                           SecondMethod->getSourceRange(), MethodInline)
10572               << SecondMethodType << SecondName << SecondInline;
10573           Diagnosed = true;
10574           break;
10575         }
10576 
10577         const unsigned FirstNumParameters = FirstMethod->param_size();
10578         const unsigned SecondNumParameters = SecondMethod->param_size();
10579         if (FirstNumParameters != SecondNumParameters) {
10580           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10581                            FirstMethod->getSourceRange(),
10582                            MethodNumberParameters)
10583               << FirstMethodType << FirstName << FirstNumParameters;
10584           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10585                           SecondMethod->getSourceRange(),
10586                           MethodNumberParameters)
10587               << SecondMethodType << SecondName << SecondNumParameters;
10588           Diagnosed = true;
10589           break;
10590         }
10591 
10592         // Need this status boolean to know when break out of the switch.
10593         bool ParameterMismatch = false;
10594         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10595           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10596           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10597 
10598           QualType FirstParamType = FirstParam->getType();
10599           QualType SecondParamType = SecondParam->getType();
10600           if (FirstParamType != SecondParamType &&
10601               ComputeQualTypeODRHash(FirstParamType) !=
10602                   ComputeQualTypeODRHash(SecondParamType)) {
10603             if (const DecayedType *ParamDecayedType =
10604                     FirstParamType->getAs<DecayedType>()) {
10605               ODRDiagDeclError(
10606                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10607                   FirstMethod->getSourceRange(), MethodParameterType)
10608                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10609                   << true << ParamDecayedType->getOriginalType();
10610             } else {
10611               ODRDiagDeclError(
10612                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10613                   FirstMethod->getSourceRange(), MethodParameterType)
10614                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10615                   << false;
10616             }
10617 
10618             if (const DecayedType *ParamDecayedType =
10619                     SecondParamType->getAs<DecayedType>()) {
10620               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10621                               SecondMethod->getSourceRange(),
10622                               MethodParameterType)
10623                   << SecondMethodType << SecondName << (I + 1)
10624                   << SecondParamType << true
10625                   << ParamDecayedType->getOriginalType();
10626             } else {
10627               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10628                               SecondMethod->getSourceRange(),
10629                               MethodParameterType)
10630                   << SecondMethodType << SecondName << (I + 1)
10631                   << SecondParamType << false;
10632             }
10633             ParameterMismatch = true;
10634             break;
10635           }
10636 
10637           DeclarationName FirstParamName = FirstParam->getDeclName();
10638           DeclarationName SecondParamName = SecondParam->getDeclName();
10639           if (FirstParamName != SecondParamName) {
10640             ODRDiagDeclError(FirstRecord, FirstModule,
10641                              FirstMethod->getLocation(),
10642                              FirstMethod->getSourceRange(), MethodParameterName)
10643                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10644             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10645                             SecondMethod->getSourceRange(), MethodParameterName)
10646                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10647             ParameterMismatch = true;
10648             break;
10649           }
10650 
10651           const Expr *FirstInit = FirstParam->getInit();
10652           const Expr *SecondInit = SecondParam->getInit();
10653           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10654             ODRDiagDeclError(FirstRecord, FirstModule,
10655                              FirstMethod->getLocation(),
10656                              FirstMethod->getSourceRange(),
10657                              MethodParameterSingleDefaultArgument)
10658                 << FirstMethodType << FirstName << (I + 1)
10659                 << (FirstInit == nullptr)
10660                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10661             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10662                             SecondMethod->getSourceRange(),
10663                             MethodParameterSingleDefaultArgument)
10664                 << SecondMethodType << SecondName << (I + 1)
10665                 << (SecondInit == nullptr)
10666                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10667             ParameterMismatch = true;
10668             break;
10669           }
10670 
10671           if (FirstInit && SecondInit &&
10672               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10673             ODRDiagDeclError(FirstRecord, FirstModule,
10674                              FirstMethod->getLocation(),
10675                              FirstMethod->getSourceRange(),
10676                              MethodParameterDifferentDefaultArgument)
10677                 << FirstMethodType << FirstName << (I + 1)
10678                 << FirstInit->getSourceRange();
10679             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10680                             SecondMethod->getSourceRange(),
10681                             MethodParameterDifferentDefaultArgument)
10682                 << SecondMethodType << SecondName << (I + 1)
10683                 << SecondInit->getSourceRange();
10684             ParameterMismatch = true;
10685             break;
10686 
10687           }
10688         }
10689 
10690         if (ParameterMismatch) {
10691           Diagnosed = true;
10692           break;
10693         }
10694 
10695         const auto *FirstTemplateArgs =
10696             FirstMethod->getTemplateSpecializationArgs();
10697         const auto *SecondTemplateArgs =
10698             SecondMethod->getTemplateSpecializationArgs();
10699 
10700         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10701             (!FirstTemplateArgs && SecondTemplateArgs)) {
10702           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10703                            FirstMethod->getSourceRange(),
10704                            MethodNoTemplateArguments)
10705               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10706           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10707                           SecondMethod->getSourceRange(),
10708                           MethodNoTemplateArguments)
10709               << SecondMethodType << SecondName
10710               << (SecondTemplateArgs != nullptr);
10711 
10712           Diagnosed = true;
10713           break;
10714         }
10715 
10716         if (FirstTemplateArgs && SecondTemplateArgs) {
10717           // Remove pack expansions from argument list.
10718           auto ExpandTemplateArgumentList =
10719               [](const TemplateArgumentList *TAL) {
10720                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10721                 for (const TemplateArgument &TA : TAL->asArray()) {
10722                   if (TA.getKind() != TemplateArgument::Pack) {
10723                     ExpandedList.push_back(&TA);
10724                     continue;
10725                   }
10726                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10727                     ExpandedList.push_back(&PackTA);
10728                   }
10729                 }
10730                 return ExpandedList;
10731               };
10732           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10733               ExpandTemplateArgumentList(FirstTemplateArgs);
10734           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10735               ExpandTemplateArgumentList(SecondTemplateArgs);
10736 
10737           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10738             ODRDiagDeclError(FirstRecord, FirstModule,
10739                              FirstMethod->getLocation(),
10740                              FirstMethod->getSourceRange(),
10741                              MethodDifferentNumberTemplateArguments)
10742                 << FirstMethodType << FirstName
10743                 << (unsigned)FirstExpandedList.size();
10744             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10745                             SecondMethod->getSourceRange(),
10746                             MethodDifferentNumberTemplateArguments)
10747                 << SecondMethodType << SecondName
10748                 << (unsigned)SecondExpandedList.size();
10749 
10750             Diagnosed = true;
10751             break;
10752           }
10753 
10754           bool TemplateArgumentMismatch = false;
10755           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10756             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10757                                    &SecondTA = *SecondExpandedList[i];
10758             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10759                 ComputeTemplateArgumentODRHash(SecondTA)) {
10760               continue;
10761             }
10762 
10763             ODRDiagDeclError(
10764                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10765                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10766                 << FirstMethodType << FirstName << FirstTA << i + 1;
10767             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10768                             SecondMethod->getSourceRange(),
10769                             MethodDifferentTemplateArgument)
10770                 << SecondMethodType << SecondName << SecondTA << i + 1;
10771 
10772             TemplateArgumentMismatch = true;
10773             break;
10774           }
10775 
10776           if (TemplateArgumentMismatch) {
10777             Diagnosed = true;
10778             break;
10779           }
10780         }
10781 
10782         // Compute the hash of the method as if it has no body.
10783         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10784           Hash.clear();
10785           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10786           return Hash.CalculateHash();
10787         };
10788 
10789         // Compare the hash generated to the hash stored.  A difference means
10790         // that a body was present in the original source.  Due to merging,
10791         // the stardard way of detecting a body will not work.
10792         const bool HasFirstBody =
10793             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10794         const bool HasSecondBody =
10795             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10796 
10797         if (HasFirstBody != HasSecondBody) {
10798           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10799                            FirstMethod->getSourceRange(), MethodSingleBody)
10800               << FirstMethodType << FirstName << HasFirstBody;
10801           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10802                           SecondMethod->getSourceRange(), MethodSingleBody)
10803               << SecondMethodType << SecondName << HasSecondBody;
10804           Diagnosed = true;
10805           break;
10806         }
10807 
10808         if (HasFirstBody && HasSecondBody) {
10809           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10810                            FirstMethod->getSourceRange(), MethodDifferentBody)
10811               << FirstMethodType << FirstName;
10812           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10813                           SecondMethod->getSourceRange(), MethodDifferentBody)
10814               << SecondMethodType << SecondName;
10815           Diagnosed = true;
10816           break;
10817         }
10818 
10819         break;
10820       }
10821       case TypeAlias:
10822       case TypeDef: {
10823         Diagnosed = ODRDiagTypeDefOrAlias(
10824             FirstRecord, FirstModule, SecondModule,
10825             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10826             FirstDiffType == TypeAlias);
10827         break;
10828       }
10829       case Var: {
10830         Diagnosed =
10831             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10832                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10833         break;
10834       }
10835       case Friend: {
10836         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10837         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10838 
10839         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10840         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10841 
10842         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10843         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10844 
10845         if (FirstND && SecondND) {
10846           ODRDiagDeclError(FirstRecord, FirstModule,
10847                            FirstFriend->getFriendLoc(),
10848                            FirstFriend->getSourceRange(), FriendFunction)
10849               << FirstND;
10850           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10851                           SecondFriend->getSourceRange(), FriendFunction)
10852               << SecondND;
10853 
10854           Diagnosed = true;
10855           break;
10856         }
10857 
10858         if (FirstTSI && SecondTSI) {
10859           QualType FirstFriendType = FirstTSI->getType();
10860           QualType SecondFriendType = SecondTSI->getType();
10861           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10862                  ComputeQualTypeODRHash(SecondFriendType));
10863           ODRDiagDeclError(FirstRecord, FirstModule,
10864                            FirstFriend->getFriendLoc(),
10865                            FirstFriend->getSourceRange(), FriendType)
10866               << FirstFriendType;
10867           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10868                           SecondFriend->getSourceRange(), FriendType)
10869               << SecondFriendType;
10870           Diagnosed = true;
10871           break;
10872         }
10873 
10874         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10875                          FirstFriend->getSourceRange(), FriendTypeFunction)
10876             << (FirstTSI == nullptr);
10877         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10878                         SecondFriend->getSourceRange(), FriendTypeFunction)
10879             << (SecondTSI == nullptr);
10880 
10881         Diagnosed = true;
10882         break;
10883       }
10884       case FunctionTemplate: {
10885         FunctionTemplateDecl *FirstTemplate =
10886             cast<FunctionTemplateDecl>(FirstDecl);
10887         FunctionTemplateDecl *SecondTemplate =
10888             cast<FunctionTemplateDecl>(SecondDecl);
10889 
10890         TemplateParameterList *FirstTPL =
10891             FirstTemplate->getTemplateParameters();
10892         TemplateParameterList *SecondTPL =
10893             SecondTemplate->getTemplateParameters();
10894 
10895         if (FirstTPL->size() != SecondTPL->size()) {
10896           ODRDiagDeclError(FirstRecord, FirstModule,
10897                            FirstTemplate->getLocation(),
10898                            FirstTemplate->getSourceRange(),
10899                            FunctionTemplateDifferentNumberParameters)
10900               << FirstTemplate << FirstTPL->size();
10901           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10902                           SecondTemplate->getSourceRange(),
10903                           FunctionTemplateDifferentNumberParameters)
10904               << SecondTemplate << SecondTPL->size();
10905 
10906           Diagnosed = true;
10907           break;
10908         }
10909 
10910         bool ParameterMismatch = false;
10911         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10912           NamedDecl *FirstParam = FirstTPL->getParam(i);
10913           NamedDecl *SecondParam = SecondTPL->getParam(i);
10914 
10915           if (FirstParam->getKind() != SecondParam->getKind()) {
10916             enum {
10917               TemplateTypeParameter,
10918               NonTypeTemplateParameter,
10919               TemplateTemplateParameter,
10920             };
10921             auto GetParamType = [](NamedDecl *D) {
10922               switch (D->getKind()) {
10923                 default:
10924                   llvm_unreachable("Unexpected template parameter type");
10925                 case Decl::TemplateTypeParm:
10926                   return TemplateTypeParameter;
10927                 case Decl::NonTypeTemplateParm:
10928                   return NonTypeTemplateParameter;
10929                 case Decl::TemplateTemplateParm:
10930                   return TemplateTemplateParameter;
10931               }
10932             };
10933 
10934             ODRDiagDeclError(FirstRecord, FirstModule,
10935                              FirstTemplate->getLocation(),
10936                              FirstTemplate->getSourceRange(),
10937                              FunctionTemplateParameterDifferentKind)
10938                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10939             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10940                             SecondTemplate->getSourceRange(),
10941                             FunctionTemplateParameterDifferentKind)
10942                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10943 
10944             ParameterMismatch = true;
10945             break;
10946           }
10947 
10948           if (FirstParam->getName() != SecondParam->getName()) {
10949             ODRDiagDeclError(
10950                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10951                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10952                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10953                 << FirstParam;
10954             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10955                             SecondTemplate->getSourceRange(),
10956                             FunctionTemplateParameterName)
10957                 << SecondTemplate << (i + 1)
10958                 << (bool)SecondParam->getIdentifier() << SecondParam;
10959             ParameterMismatch = true;
10960             break;
10961           }
10962 
10963           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10964               isa<TemplateTypeParmDecl>(SecondParam)) {
10965             TemplateTypeParmDecl *FirstTTPD =
10966                 cast<TemplateTypeParmDecl>(FirstParam);
10967             TemplateTypeParmDecl *SecondTTPD =
10968                 cast<TemplateTypeParmDecl>(SecondParam);
10969             bool HasFirstDefaultArgument =
10970                 FirstTTPD->hasDefaultArgument() &&
10971                 !FirstTTPD->defaultArgumentWasInherited();
10972             bool HasSecondDefaultArgument =
10973                 SecondTTPD->hasDefaultArgument() &&
10974                 !SecondTTPD->defaultArgumentWasInherited();
10975             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10976               ODRDiagDeclError(FirstRecord, FirstModule,
10977                                FirstTemplate->getLocation(),
10978                                FirstTemplate->getSourceRange(),
10979                                FunctionTemplateParameterSingleDefaultArgument)
10980                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10981               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10982                               SecondTemplate->getSourceRange(),
10983                               FunctionTemplateParameterSingleDefaultArgument)
10984                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10985               ParameterMismatch = true;
10986               break;
10987             }
10988 
10989             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10990               QualType FirstType = FirstTTPD->getDefaultArgument();
10991               QualType SecondType = SecondTTPD->getDefaultArgument();
10992               if (ComputeQualTypeODRHash(FirstType) !=
10993                   ComputeQualTypeODRHash(SecondType)) {
10994                 ODRDiagDeclError(
10995                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10996                     FirstTemplate->getSourceRange(),
10997                     FunctionTemplateParameterDifferentDefaultArgument)
10998                     << FirstTemplate << (i + 1) << FirstType;
10999                 ODRDiagDeclNote(
11000                     SecondModule, SecondTemplate->getLocation(),
11001                     SecondTemplate->getSourceRange(),
11002                     FunctionTemplateParameterDifferentDefaultArgument)
11003                     << SecondTemplate << (i + 1) << SecondType;
11004                 ParameterMismatch = true;
11005                 break;
11006               }
11007             }
11008 
11009             if (FirstTTPD->isParameterPack() !=
11010                 SecondTTPD->isParameterPack()) {
11011               ODRDiagDeclError(FirstRecord, FirstModule,
11012                                FirstTemplate->getLocation(),
11013                                FirstTemplate->getSourceRange(),
11014                                FunctionTemplatePackParameter)
11015                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11016               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11017                               SecondTemplate->getSourceRange(),
11018                               FunctionTemplatePackParameter)
11019                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11020               ParameterMismatch = true;
11021               break;
11022             }
11023           }
11024 
11025           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11026               isa<TemplateTemplateParmDecl>(SecondParam)) {
11027             TemplateTemplateParmDecl *FirstTTPD =
11028                 cast<TemplateTemplateParmDecl>(FirstParam);
11029             TemplateTemplateParmDecl *SecondTTPD =
11030                 cast<TemplateTemplateParmDecl>(SecondParam);
11031 
11032             TemplateParameterList *FirstTPL =
11033                 FirstTTPD->getTemplateParameters();
11034             TemplateParameterList *SecondTPL =
11035                 SecondTTPD->getTemplateParameters();
11036 
11037             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11038                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11039               ODRDiagDeclError(FirstRecord, FirstModule,
11040                                FirstTemplate->getLocation(),
11041                                FirstTemplate->getSourceRange(),
11042                                FunctionTemplateParameterDifferentType)
11043                   << FirstTemplate << (i + 1);
11044               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11045                               SecondTemplate->getSourceRange(),
11046                               FunctionTemplateParameterDifferentType)
11047                   << SecondTemplate << (i + 1);
11048               ParameterMismatch = true;
11049               break;
11050             }
11051 
11052             bool HasFirstDefaultArgument =
11053                 FirstTTPD->hasDefaultArgument() &&
11054                 !FirstTTPD->defaultArgumentWasInherited();
11055             bool HasSecondDefaultArgument =
11056                 SecondTTPD->hasDefaultArgument() &&
11057                 !SecondTTPD->defaultArgumentWasInherited();
11058             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11059               ODRDiagDeclError(FirstRecord, FirstModule,
11060                                FirstTemplate->getLocation(),
11061                                FirstTemplate->getSourceRange(),
11062                                FunctionTemplateParameterSingleDefaultArgument)
11063                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11064               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11065                               SecondTemplate->getSourceRange(),
11066                               FunctionTemplateParameterSingleDefaultArgument)
11067                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11068               ParameterMismatch = true;
11069               break;
11070             }
11071 
11072             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11073               TemplateArgument FirstTA =
11074                   FirstTTPD->getDefaultArgument().getArgument();
11075               TemplateArgument SecondTA =
11076                   SecondTTPD->getDefaultArgument().getArgument();
11077               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11078                   ComputeTemplateArgumentODRHash(SecondTA)) {
11079                 ODRDiagDeclError(
11080                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11081                     FirstTemplate->getSourceRange(),
11082                     FunctionTemplateParameterDifferentDefaultArgument)
11083                     << FirstTemplate << (i + 1) << FirstTA;
11084                 ODRDiagDeclNote(
11085                     SecondModule, SecondTemplate->getLocation(),
11086                     SecondTemplate->getSourceRange(),
11087                     FunctionTemplateParameterDifferentDefaultArgument)
11088                     << SecondTemplate << (i + 1) << SecondTA;
11089                 ParameterMismatch = true;
11090                 break;
11091               }
11092             }
11093 
11094             if (FirstTTPD->isParameterPack() !=
11095                 SecondTTPD->isParameterPack()) {
11096               ODRDiagDeclError(FirstRecord, FirstModule,
11097                                FirstTemplate->getLocation(),
11098                                FirstTemplate->getSourceRange(),
11099                                FunctionTemplatePackParameter)
11100                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11101               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11102                               SecondTemplate->getSourceRange(),
11103                               FunctionTemplatePackParameter)
11104                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11105               ParameterMismatch = true;
11106               break;
11107             }
11108           }
11109 
11110           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11111               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11112             NonTypeTemplateParmDecl *FirstNTTPD =
11113                 cast<NonTypeTemplateParmDecl>(FirstParam);
11114             NonTypeTemplateParmDecl *SecondNTTPD =
11115                 cast<NonTypeTemplateParmDecl>(SecondParam);
11116 
11117             QualType FirstType = FirstNTTPD->getType();
11118             QualType SecondType = SecondNTTPD->getType();
11119             if (ComputeQualTypeODRHash(FirstType) !=
11120                 ComputeQualTypeODRHash(SecondType)) {
11121               ODRDiagDeclError(FirstRecord, FirstModule,
11122                                FirstTemplate->getLocation(),
11123                                FirstTemplate->getSourceRange(),
11124                                FunctionTemplateParameterDifferentType)
11125                   << FirstTemplate << (i + 1);
11126               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11127                               SecondTemplate->getSourceRange(),
11128                               FunctionTemplateParameterDifferentType)
11129                   << SecondTemplate << (i + 1);
11130               ParameterMismatch = true;
11131               break;
11132             }
11133 
11134             bool HasFirstDefaultArgument =
11135                 FirstNTTPD->hasDefaultArgument() &&
11136                 !FirstNTTPD->defaultArgumentWasInherited();
11137             bool HasSecondDefaultArgument =
11138                 SecondNTTPD->hasDefaultArgument() &&
11139                 !SecondNTTPD->defaultArgumentWasInherited();
11140             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11141               ODRDiagDeclError(FirstRecord, FirstModule,
11142                                FirstTemplate->getLocation(),
11143                                FirstTemplate->getSourceRange(),
11144                                FunctionTemplateParameterSingleDefaultArgument)
11145                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11146               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11147                               SecondTemplate->getSourceRange(),
11148                               FunctionTemplateParameterSingleDefaultArgument)
11149                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11150               ParameterMismatch = true;
11151               break;
11152             }
11153 
11154             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11155               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11156               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11157               if (ComputeODRHash(FirstDefaultArgument) !=
11158                   ComputeODRHash(SecondDefaultArgument)) {
11159                 ODRDiagDeclError(
11160                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11161                     FirstTemplate->getSourceRange(),
11162                     FunctionTemplateParameterDifferentDefaultArgument)
11163                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11164                 ODRDiagDeclNote(
11165                     SecondModule, SecondTemplate->getLocation(),
11166                     SecondTemplate->getSourceRange(),
11167                     FunctionTemplateParameterDifferentDefaultArgument)
11168                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11169                 ParameterMismatch = true;
11170                 break;
11171               }
11172             }
11173 
11174             if (FirstNTTPD->isParameterPack() !=
11175                 SecondNTTPD->isParameterPack()) {
11176               ODRDiagDeclError(FirstRecord, FirstModule,
11177                                FirstTemplate->getLocation(),
11178                                FirstTemplate->getSourceRange(),
11179                                FunctionTemplatePackParameter)
11180                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11181               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11182                               SecondTemplate->getSourceRange(),
11183                               FunctionTemplatePackParameter)
11184                   << SecondTemplate << (i + 1)
11185                   << SecondNTTPD->isParameterPack();
11186               ParameterMismatch = true;
11187               break;
11188             }
11189           }
11190         }
11191 
11192         if (ParameterMismatch) {
11193           Diagnosed = true;
11194           break;
11195         }
11196 
11197         break;
11198       }
11199       }
11200 
11201       if (Diagnosed)
11202         continue;
11203 
11204       Diag(FirstDecl->getLocation(),
11205            diag::err_module_odr_violation_mismatch_decl_unknown)
11206           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11207           << FirstDecl->getSourceRange();
11208       Diag(SecondDecl->getLocation(),
11209            diag::note_module_odr_violation_mismatch_decl_unknown)
11210           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11211       Diagnosed = true;
11212     }
11213 
11214     if (!Diagnosed) {
11215       // All definitions are updates to the same declaration. This happens if a
11216       // module instantiates the declaration of a class template specialization
11217       // and two or more other modules instantiate its definition.
11218       //
11219       // FIXME: Indicate which modules had instantiations of this definition.
11220       // FIXME: How can this even happen?
11221       Diag(Merge.first->getLocation(),
11222            diag::err_module_odr_violation_different_instantiations)
11223         << Merge.first;
11224     }
11225   }
11226 
11227   // Issue ODR failures diagnostics for functions.
11228   for (auto &Merge : FunctionOdrMergeFailures) {
11229     enum ODRFunctionDifference {
11230       ReturnType,
11231       ParameterName,
11232       ParameterType,
11233       ParameterSingleDefaultArgument,
11234       ParameterDifferentDefaultArgument,
11235       FunctionBody,
11236     };
11237 
11238     FunctionDecl *FirstFunction = Merge.first;
11239     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11240 
11241     bool Diagnosed = false;
11242     for (auto &SecondFunction : Merge.second) {
11243 
11244       if (FirstFunction == SecondFunction)
11245         continue;
11246 
11247       std::string SecondModule =
11248           getOwningModuleNameForDiagnostic(SecondFunction);
11249 
11250       auto ODRDiagError = [FirstFunction, &FirstModule,
11251                            this](SourceLocation Loc, SourceRange Range,
11252                                  ODRFunctionDifference DiffType) {
11253         return Diag(Loc, diag::err_module_odr_violation_function)
11254                << FirstFunction << FirstModule.empty() << FirstModule << Range
11255                << DiffType;
11256       };
11257       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11258                                                SourceRange Range,
11259                                                ODRFunctionDifference DiffType) {
11260         return Diag(Loc, diag::note_module_odr_violation_function)
11261                << SecondModule << Range << DiffType;
11262       };
11263 
11264       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11265           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11266         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11267                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11268             << FirstFunction->getReturnType();
11269         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11270                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11271             << SecondFunction->getReturnType();
11272         Diagnosed = true;
11273         break;
11274       }
11275 
11276       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11277              "Merged functions with different number of parameters");
11278 
11279       auto ParamSize = FirstFunction->param_size();
11280       bool ParameterMismatch = false;
11281       for (unsigned I = 0; I < ParamSize; ++I) {
11282         auto *FirstParam = FirstFunction->getParamDecl(I);
11283         auto *SecondParam = SecondFunction->getParamDecl(I);
11284 
11285         assert(getContext().hasSameType(FirstParam->getType(),
11286                                       SecondParam->getType()) &&
11287                "Merged function has different parameter types.");
11288 
11289         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11290           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11291                        ParameterName)
11292               << I + 1 << FirstParam->getDeclName();
11293           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11294                       ParameterName)
11295               << I + 1 << SecondParam->getDeclName();
11296           ParameterMismatch = true;
11297           break;
11298         };
11299 
11300         QualType FirstParamType = FirstParam->getType();
11301         QualType SecondParamType = SecondParam->getType();
11302         if (FirstParamType != SecondParamType &&
11303             ComputeQualTypeODRHash(FirstParamType) !=
11304                 ComputeQualTypeODRHash(SecondParamType)) {
11305           if (const DecayedType *ParamDecayedType =
11306                   FirstParamType->getAs<DecayedType>()) {
11307             ODRDiagError(FirstParam->getLocation(),
11308                          FirstParam->getSourceRange(), ParameterType)
11309                 << (I + 1) << FirstParamType << true
11310                 << ParamDecayedType->getOriginalType();
11311           } else {
11312             ODRDiagError(FirstParam->getLocation(),
11313                          FirstParam->getSourceRange(), ParameterType)
11314                 << (I + 1) << FirstParamType << false;
11315           }
11316 
11317           if (const DecayedType *ParamDecayedType =
11318                   SecondParamType->getAs<DecayedType>()) {
11319             ODRDiagNote(SecondParam->getLocation(),
11320                         SecondParam->getSourceRange(), ParameterType)
11321                 << (I + 1) << SecondParamType << true
11322                 << ParamDecayedType->getOriginalType();
11323           } else {
11324             ODRDiagNote(SecondParam->getLocation(),
11325                         SecondParam->getSourceRange(), ParameterType)
11326                 << (I + 1) << SecondParamType << false;
11327           }
11328           ParameterMismatch = true;
11329           break;
11330         }
11331 
11332         const Expr *FirstInit = FirstParam->getInit();
11333         const Expr *SecondInit = SecondParam->getInit();
11334         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11335           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11336                        ParameterSingleDefaultArgument)
11337               << (I + 1) << (FirstInit == nullptr)
11338               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11339           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11340                       ParameterSingleDefaultArgument)
11341               << (I + 1) << (SecondInit == nullptr)
11342               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11343           ParameterMismatch = true;
11344           break;
11345         }
11346 
11347         if (FirstInit && SecondInit &&
11348             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11349           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11350                        ParameterDifferentDefaultArgument)
11351               << (I + 1) << FirstInit->getSourceRange();
11352           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11353                       ParameterDifferentDefaultArgument)
11354               << (I + 1) << SecondInit->getSourceRange();
11355           ParameterMismatch = true;
11356           break;
11357         }
11358 
11359         assert(ComputeSubDeclODRHash(FirstParam) ==
11360                    ComputeSubDeclODRHash(SecondParam) &&
11361                "Undiagnosed parameter difference.");
11362       }
11363 
11364       if (ParameterMismatch) {
11365         Diagnosed = true;
11366         break;
11367       }
11368 
11369       // If no error has been generated before now, assume the problem is in
11370       // the body and generate a message.
11371       ODRDiagError(FirstFunction->getLocation(),
11372                    FirstFunction->getSourceRange(), FunctionBody);
11373       ODRDiagNote(SecondFunction->getLocation(),
11374                   SecondFunction->getSourceRange(), FunctionBody);
11375       Diagnosed = true;
11376       break;
11377     }
11378     (void)Diagnosed;
11379     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11380   }
11381 
11382   // Issue ODR failures diagnostics for enums.
11383   for (auto &Merge : EnumOdrMergeFailures) {
11384     enum ODREnumDifference {
11385       SingleScopedEnum,
11386       EnumTagKeywordMismatch,
11387       SingleSpecifiedType,
11388       DifferentSpecifiedTypes,
11389       DifferentNumberEnumConstants,
11390       EnumConstantName,
11391       EnumConstantSingleInitilizer,
11392       EnumConstantDifferentInitilizer,
11393     };
11394 
11395     // If we've already pointed out a specific problem with this enum, don't
11396     // bother issuing a general "something's different" diagnostic.
11397     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11398       continue;
11399 
11400     EnumDecl *FirstEnum = Merge.first;
11401     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11402 
11403     using DeclHashes =
11404         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11405     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11406                               DeclHashes &Hashes, EnumDecl *Enum) {
11407       for (auto *D : Enum->decls()) {
11408         // Due to decl merging, the first EnumDecl is the parent of
11409         // Decls in both records.
11410         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11411           continue;
11412         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11413         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11414                             ComputeSubDeclODRHash(D));
11415       }
11416     };
11417     DeclHashes FirstHashes;
11418     PopulateHashes(FirstHashes, FirstEnum);
11419     bool Diagnosed = false;
11420     for (auto &SecondEnum : Merge.second) {
11421 
11422       if (FirstEnum == SecondEnum)
11423         continue;
11424 
11425       std::string SecondModule =
11426           getOwningModuleNameForDiagnostic(SecondEnum);
11427 
11428       auto ODRDiagError = [FirstEnum, &FirstModule,
11429                            this](SourceLocation Loc, SourceRange Range,
11430                                  ODREnumDifference DiffType) {
11431         return Diag(Loc, diag::err_module_odr_violation_enum)
11432                << FirstEnum << FirstModule.empty() << FirstModule << Range
11433                << DiffType;
11434       };
11435       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11436                                                SourceRange Range,
11437                                                ODREnumDifference DiffType) {
11438         return Diag(Loc, diag::note_module_odr_violation_enum)
11439                << SecondModule << Range << DiffType;
11440       };
11441 
11442       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11443         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11444                      SingleScopedEnum)
11445             << FirstEnum->isScoped();
11446         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11447                     SingleScopedEnum)
11448             << SecondEnum->isScoped();
11449         Diagnosed = true;
11450         continue;
11451       }
11452 
11453       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11454         if (FirstEnum->isScopedUsingClassTag() !=
11455             SecondEnum->isScopedUsingClassTag()) {
11456           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11457                        EnumTagKeywordMismatch)
11458               << FirstEnum->isScopedUsingClassTag();
11459           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11460                       EnumTagKeywordMismatch)
11461               << SecondEnum->isScopedUsingClassTag();
11462           Diagnosed = true;
11463           continue;
11464         }
11465       }
11466 
11467       QualType FirstUnderlyingType =
11468           FirstEnum->getIntegerTypeSourceInfo()
11469               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11470               : QualType();
11471       QualType SecondUnderlyingType =
11472           SecondEnum->getIntegerTypeSourceInfo()
11473               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11474               : QualType();
11475       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11476           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11477                        SingleSpecifiedType)
11478               << !FirstUnderlyingType.isNull();
11479           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11480                       SingleSpecifiedType)
11481               << !SecondUnderlyingType.isNull();
11482           Diagnosed = true;
11483           continue;
11484       }
11485 
11486       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11487         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11488             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11489           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11490                        DifferentSpecifiedTypes)
11491               << FirstUnderlyingType;
11492           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11493                       DifferentSpecifiedTypes)
11494               << SecondUnderlyingType;
11495           Diagnosed = true;
11496           continue;
11497         }
11498       }
11499 
11500       DeclHashes SecondHashes;
11501       PopulateHashes(SecondHashes, SecondEnum);
11502 
11503       if (FirstHashes.size() != SecondHashes.size()) {
11504         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11505                      DifferentNumberEnumConstants)
11506             << (int)FirstHashes.size();
11507         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11508                     DifferentNumberEnumConstants)
11509             << (int)SecondHashes.size();
11510         Diagnosed = true;
11511         continue;
11512       }
11513 
11514       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11515         if (FirstHashes[I].second == SecondHashes[I].second)
11516           continue;
11517         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11518         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11519 
11520         if (FirstEnumConstant->getDeclName() !=
11521             SecondEnumConstant->getDeclName()) {
11522 
11523           ODRDiagError(FirstEnumConstant->getLocation(),
11524                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11525               << I + 1 << FirstEnumConstant;
11526           ODRDiagNote(SecondEnumConstant->getLocation(),
11527                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11528               << I + 1 << SecondEnumConstant;
11529           Diagnosed = true;
11530           break;
11531         }
11532 
11533         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11534         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11535         if (!FirstInit && !SecondInit)
11536           continue;
11537 
11538         if (!FirstInit || !SecondInit) {
11539           ODRDiagError(FirstEnumConstant->getLocation(),
11540                        FirstEnumConstant->getSourceRange(),
11541                        EnumConstantSingleInitilizer)
11542               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11543           ODRDiagNote(SecondEnumConstant->getLocation(),
11544                       SecondEnumConstant->getSourceRange(),
11545                       EnumConstantSingleInitilizer)
11546               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11547           Diagnosed = true;
11548           break;
11549         }
11550 
11551         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11552           ODRDiagError(FirstEnumConstant->getLocation(),
11553                        FirstEnumConstant->getSourceRange(),
11554                        EnumConstantDifferentInitilizer)
11555               << I + 1 << FirstEnumConstant;
11556           ODRDiagNote(SecondEnumConstant->getLocation(),
11557                       SecondEnumConstant->getSourceRange(),
11558                       EnumConstantDifferentInitilizer)
11559               << I + 1 << SecondEnumConstant;
11560           Diagnosed = true;
11561           break;
11562         }
11563       }
11564     }
11565 
11566     (void)Diagnosed;
11567     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11568   }
11569 }
11570 
11571 void ASTReader::StartedDeserializing() {
11572   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11573     ReadTimer->startTimer();
11574 }
11575 
11576 void ASTReader::FinishedDeserializing() {
11577   assert(NumCurrentElementsDeserializing &&
11578          "FinishedDeserializing not paired with StartedDeserializing");
11579   if (NumCurrentElementsDeserializing == 1) {
11580     // We decrease NumCurrentElementsDeserializing only after pending actions
11581     // are finished, to avoid recursively re-calling finishPendingActions().
11582     finishPendingActions();
11583   }
11584   --NumCurrentElementsDeserializing;
11585 
11586   if (NumCurrentElementsDeserializing == 0) {
11587     // Propagate exception specification and deduced type updates along
11588     // redeclaration chains.
11589     //
11590     // We do this now rather than in finishPendingActions because we want to
11591     // be able to walk the complete redeclaration chains of the updated decls.
11592     while (!PendingExceptionSpecUpdates.empty() ||
11593            !PendingDeducedTypeUpdates.empty()) {
11594       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11595       PendingExceptionSpecUpdates.clear();
11596       for (auto Update : ESUpdates) {
11597         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11598         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11599         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11600         if (auto *Listener = getContext().getASTMutationListener())
11601           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11602         for (auto *Redecl : Update.second->redecls())
11603           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11604       }
11605 
11606       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11607       PendingDeducedTypeUpdates.clear();
11608       for (auto Update : DTUpdates) {
11609         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11610         // FIXME: If the return type is already deduced, check that it matches.
11611         getContext().adjustDeducedFunctionResultType(Update.first,
11612                                                      Update.second);
11613       }
11614     }
11615 
11616     if (ReadTimer)
11617       ReadTimer->stopTimer();
11618 
11619     diagnoseOdrViolations();
11620 
11621     // We are not in recursive loading, so it's safe to pass the "interesting"
11622     // decls to the consumer.
11623     if (Consumer)
11624       PassInterestingDeclsToConsumer();
11625   }
11626 }
11627 
11628 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11629   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11630     // Remove any fake results before adding any real ones.
11631     auto It = PendingFakeLookupResults.find(II);
11632     if (It != PendingFakeLookupResults.end()) {
11633       for (auto *ND : It->second)
11634         SemaObj->IdResolver.RemoveDecl(ND);
11635       // FIXME: this works around module+PCH performance issue.
11636       // Rather than erase the result from the map, which is O(n), just clear
11637       // the vector of NamedDecls.
11638       It->second.clear();
11639     }
11640   }
11641 
11642   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11643     SemaObj->TUScope->AddDecl(D);
11644   } else if (SemaObj->TUScope) {
11645     // Adding the decl to IdResolver may have failed because it was already in
11646     // (even though it was not added in scope). If it is already in, make sure
11647     // it gets in the scope as well.
11648     if (std::find(SemaObj->IdResolver.begin(Name),
11649                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11650       SemaObj->TUScope->AddDecl(D);
11651   }
11652 }
11653 
11654 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11655                      ASTContext *Context,
11656                      const PCHContainerReader &PCHContainerRdr,
11657                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11658                      StringRef isysroot, bool DisableValidation,
11659                      bool AllowASTWithCompilerErrors,
11660                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11661                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11662                      std::unique_ptr<llvm::Timer> ReadTimer)
11663     : Listener(DisableValidation
11664                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11665                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11666       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11667       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11668       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11669                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11670       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11671       DisableValidation(DisableValidation),
11672       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11673       AllowConfigurationMismatch(AllowConfigurationMismatch),
11674       ValidateSystemInputs(ValidateSystemInputs),
11675       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11676       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11677   SourceMgr.setExternalSLocEntrySource(this);
11678 
11679   for (const auto &Ext : Extensions) {
11680     auto BlockName = Ext->getExtensionMetadata().BlockName;
11681     auto Known = ModuleFileExtensions.find(BlockName);
11682     if (Known != ModuleFileExtensions.end()) {
11683       Diags.Report(diag::warn_duplicate_module_file_extension)
11684         << BlockName;
11685       continue;
11686     }
11687 
11688     ModuleFileExtensions.insert({BlockName, Ext});
11689   }
11690 }
11691 
11692 ASTReader::~ASTReader() {
11693   if (OwnsDeserializationListener)
11694     delete DeserializationListener;
11695 }
11696 
11697 IdentifierResolver &ASTReader::getIdResolver() {
11698   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11699 }
11700 
11701 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11702                                                unsigned AbbrevID) {
11703   Idx = 0;
11704   Record.clear();
11705   return Cursor.readRecord(AbbrevID, Record);
11706 }
11707 //===----------------------------------------------------------------------===//
11708 //// OMPClauseReader implementation
11709 ////===----------------------------------------------------------------------===//
11710 
11711 // This has to be in namespace clang because it's friended by all
11712 // of the OMP clauses.
11713 namespace clang {
11714 
11715 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11716   ASTRecordReader &Record;
11717   ASTContext &Context;
11718 
11719 public:
11720   OMPClauseReader(ASTRecordReader &Record)
11721       : Record(Record), Context(Record.getContext()) {}
11722 
11723 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11724 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11725   OMPClause *readClause();
11726   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11727   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11728 };
11729 
11730 } // end namespace clang
11731 
11732 OMPClause *ASTRecordReader::readOMPClause() {
11733   return OMPClauseReader(*this).readClause();
11734 }
11735 
11736 OMPClause *OMPClauseReader::readClause() {
11737   OMPClause *C = nullptr;
11738   switch (llvm::omp::Clause(Record.readInt())) {
11739   case llvm::omp::OMPC_if:
11740     C = new (Context) OMPIfClause();
11741     break;
11742   case llvm::omp::OMPC_final:
11743     C = new (Context) OMPFinalClause();
11744     break;
11745   case llvm::omp::OMPC_num_threads:
11746     C = new (Context) OMPNumThreadsClause();
11747     break;
11748   case llvm::omp::OMPC_safelen:
11749     C = new (Context) OMPSafelenClause();
11750     break;
11751   case llvm::omp::OMPC_simdlen:
11752     C = new (Context) OMPSimdlenClause();
11753     break;
11754   case llvm::omp::OMPC_allocator:
11755     C = new (Context) OMPAllocatorClause();
11756     break;
11757   case llvm::omp::OMPC_collapse:
11758     C = new (Context) OMPCollapseClause();
11759     break;
11760   case llvm::omp::OMPC_default:
11761     C = new (Context) OMPDefaultClause();
11762     break;
11763   case llvm::omp::OMPC_proc_bind:
11764     C = new (Context) OMPProcBindClause();
11765     break;
11766   case llvm::omp::OMPC_schedule:
11767     C = new (Context) OMPScheduleClause();
11768     break;
11769   case llvm::omp::OMPC_ordered:
11770     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11771     break;
11772   case llvm::omp::OMPC_nowait:
11773     C = new (Context) OMPNowaitClause();
11774     break;
11775   case llvm::omp::OMPC_untied:
11776     C = new (Context) OMPUntiedClause();
11777     break;
11778   case llvm::omp::OMPC_mergeable:
11779     C = new (Context) OMPMergeableClause();
11780     break;
11781   case llvm::omp::OMPC_read:
11782     C = new (Context) OMPReadClause();
11783     break;
11784   case llvm::omp::OMPC_write:
11785     C = new (Context) OMPWriteClause();
11786     break;
11787   case llvm::omp::OMPC_update:
11788     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11789     break;
11790   case llvm::omp::OMPC_capture:
11791     C = new (Context) OMPCaptureClause();
11792     break;
11793   case llvm::omp::OMPC_seq_cst:
11794     C = new (Context) OMPSeqCstClause();
11795     break;
11796   case llvm::omp::OMPC_acq_rel:
11797     C = new (Context) OMPAcqRelClause();
11798     break;
11799   case llvm::omp::OMPC_acquire:
11800     C = new (Context) OMPAcquireClause();
11801     break;
11802   case llvm::omp::OMPC_release:
11803     C = new (Context) OMPReleaseClause();
11804     break;
11805   case llvm::omp::OMPC_relaxed:
11806     C = new (Context) OMPRelaxedClause();
11807     break;
11808   case llvm::omp::OMPC_threads:
11809     C = new (Context) OMPThreadsClause();
11810     break;
11811   case llvm::omp::OMPC_simd:
11812     C = new (Context) OMPSIMDClause();
11813     break;
11814   case llvm::omp::OMPC_nogroup:
11815     C = new (Context) OMPNogroupClause();
11816     break;
11817   case llvm::omp::OMPC_unified_address:
11818     C = new (Context) OMPUnifiedAddressClause();
11819     break;
11820   case llvm::omp::OMPC_unified_shared_memory:
11821     C = new (Context) OMPUnifiedSharedMemoryClause();
11822     break;
11823   case llvm::omp::OMPC_reverse_offload:
11824     C = new (Context) OMPReverseOffloadClause();
11825     break;
11826   case llvm::omp::OMPC_dynamic_allocators:
11827     C = new (Context) OMPDynamicAllocatorsClause();
11828     break;
11829   case llvm::omp::OMPC_atomic_default_mem_order:
11830     C = new (Context) OMPAtomicDefaultMemOrderClause();
11831     break;
11832  case llvm::omp::OMPC_private:
11833     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11834     break;
11835   case llvm::omp::OMPC_firstprivate:
11836     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11837     break;
11838   case llvm::omp::OMPC_lastprivate:
11839     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11840     break;
11841   case llvm::omp::OMPC_shared:
11842     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11843     break;
11844   case llvm::omp::OMPC_reduction: {
11845     unsigned N = Record.readInt();
11846     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11847     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11848     break;
11849   }
11850   case llvm::omp::OMPC_task_reduction:
11851     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11852     break;
11853   case llvm::omp::OMPC_in_reduction:
11854     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11855     break;
11856   case llvm::omp::OMPC_linear:
11857     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11858     break;
11859   case llvm::omp::OMPC_aligned:
11860     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11861     break;
11862   case llvm::omp::OMPC_copyin:
11863     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11864     break;
11865   case llvm::omp::OMPC_copyprivate:
11866     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11867     break;
11868   case llvm::omp::OMPC_flush:
11869     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11870     break;
11871   case llvm::omp::OMPC_depobj:
11872     C = OMPDepobjClause::CreateEmpty(Context);
11873     break;
11874   case llvm::omp::OMPC_depend: {
11875     unsigned NumVars = Record.readInt();
11876     unsigned NumLoops = Record.readInt();
11877     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11878     break;
11879   }
11880   case llvm::omp::OMPC_device:
11881     C = new (Context) OMPDeviceClause();
11882     break;
11883   case llvm::omp::OMPC_map: {
11884     OMPMappableExprListSizeTy Sizes;
11885     Sizes.NumVars = Record.readInt();
11886     Sizes.NumUniqueDeclarations = Record.readInt();
11887     Sizes.NumComponentLists = Record.readInt();
11888     Sizes.NumComponents = Record.readInt();
11889     C = OMPMapClause::CreateEmpty(Context, Sizes);
11890     break;
11891   }
11892   case llvm::omp::OMPC_num_teams:
11893     C = new (Context) OMPNumTeamsClause();
11894     break;
11895   case llvm::omp::OMPC_thread_limit:
11896     C = new (Context) OMPThreadLimitClause();
11897     break;
11898   case llvm::omp::OMPC_priority:
11899     C = new (Context) OMPPriorityClause();
11900     break;
11901   case llvm::omp::OMPC_grainsize:
11902     C = new (Context) OMPGrainsizeClause();
11903     break;
11904   case llvm::omp::OMPC_num_tasks:
11905     C = new (Context) OMPNumTasksClause();
11906     break;
11907   case llvm::omp::OMPC_hint:
11908     C = new (Context) OMPHintClause();
11909     break;
11910   case llvm::omp::OMPC_dist_schedule:
11911     C = new (Context) OMPDistScheduleClause();
11912     break;
11913   case llvm::omp::OMPC_defaultmap:
11914     C = new (Context) OMPDefaultmapClause();
11915     break;
11916   case llvm::omp::OMPC_to: {
11917     OMPMappableExprListSizeTy Sizes;
11918     Sizes.NumVars = Record.readInt();
11919     Sizes.NumUniqueDeclarations = Record.readInt();
11920     Sizes.NumComponentLists = Record.readInt();
11921     Sizes.NumComponents = Record.readInt();
11922     C = OMPToClause::CreateEmpty(Context, Sizes);
11923     break;
11924   }
11925   case llvm::omp::OMPC_from: {
11926     OMPMappableExprListSizeTy Sizes;
11927     Sizes.NumVars = Record.readInt();
11928     Sizes.NumUniqueDeclarations = Record.readInt();
11929     Sizes.NumComponentLists = Record.readInt();
11930     Sizes.NumComponents = Record.readInt();
11931     C = OMPFromClause::CreateEmpty(Context, Sizes);
11932     break;
11933   }
11934   case llvm::omp::OMPC_use_device_ptr: {
11935     OMPMappableExprListSizeTy Sizes;
11936     Sizes.NumVars = Record.readInt();
11937     Sizes.NumUniqueDeclarations = Record.readInt();
11938     Sizes.NumComponentLists = Record.readInt();
11939     Sizes.NumComponents = Record.readInt();
11940     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11941     break;
11942   }
11943   case llvm::omp::OMPC_use_device_addr: {
11944     OMPMappableExprListSizeTy Sizes;
11945     Sizes.NumVars = Record.readInt();
11946     Sizes.NumUniqueDeclarations = Record.readInt();
11947     Sizes.NumComponentLists = Record.readInt();
11948     Sizes.NumComponents = Record.readInt();
11949     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11950     break;
11951   }
11952   case llvm::omp::OMPC_is_device_ptr: {
11953     OMPMappableExprListSizeTy Sizes;
11954     Sizes.NumVars = Record.readInt();
11955     Sizes.NumUniqueDeclarations = Record.readInt();
11956     Sizes.NumComponentLists = Record.readInt();
11957     Sizes.NumComponents = Record.readInt();
11958     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11959     break;
11960   }
11961   case llvm::omp::OMPC_allocate:
11962     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11963     break;
11964   case llvm::omp::OMPC_nontemporal:
11965     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11966     break;
11967   case llvm::omp::OMPC_inclusive:
11968     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11969     break;
11970   case llvm::omp::OMPC_exclusive:
11971     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11972     break;
11973   case llvm::omp::OMPC_order:
11974     C = new (Context) OMPOrderClause();
11975     break;
11976   case llvm::omp::OMPC_destroy:
11977     C = new (Context) OMPDestroyClause();
11978     break;
11979   case llvm::omp::OMPC_detach:
11980     C = new (Context) OMPDetachClause();
11981     break;
11982   case llvm::omp::OMPC_uses_allocators:
11983     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11984     break;
11985   case llvm::omp::OMPC_affinity:
11986     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11987     break;
11988 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11989   case llvm::omp::Enum:                                                        \
11990     break;
11991 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11992   default:
11993     break;
11994   }
11995   assert(C && "Unknown OMPClause type");
11996 
11997   Visit(C);
11998   C->setLocStart(Record.readSourceLocation());
11999   C->setLocEnd(Record.readSourceLocation());
12000 
12001   return C;
12002 }
12003 
12004 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12005   C->setPreInitStmt(Record.readSubStmt(),
12006                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12007 }
12008 
12009 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12010   VisitOMPClauseWithPreInit(C);
12011   C->setPostUpdateExpr(Record.readSubExpr());
12012 }
12013 
12014 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12015   VisitOMPClauseWithPreInit(C);
12016   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12017   C->setNameModifierLoc(Record.readSourceLocation());
12018   C->setColonLoc(Record.readSourceLocation());
12019   C->setCondition(Record.readSubExpr());
12020   C->setLParenLoc(Record.readSourceLocation());
12021 }
12022 
12023 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12024   VisitOMPClauseWithPreInit(C);
12025   C->setCondition(Record.readSubExpr());
12026   C->setLParenLoc(Record.readSourceLocation());
12027 }
12028 
12029 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12030   VisitOMPClauseWithPreInit(C);
12031   C->setNumThreads(Record.readSubExpr());
12032   C->setLParenLoc(Record.readSourceLocation());
12033 }
12034 
12035 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12036   C->setSafelen(Record.readSubExpr());
12037   C->setLParenLoc(Record.readSourceLocation());
12038 }
12039 
12040 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12041   C->setSimdlen(Record.readSubExpr());
12042   C->setLParenLoc(Record.readSourceLocation());
12043 }
12044 
12045 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12046   C->setAllocator(Record.readExpr());
12047   C->setLParenLoc(Record.readSourceLocation());
12048 }
12049 
12050 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12051   C->setNumForLoops(Record.readSubExpr());
12052   C->setLParenLoc(Record.readSourceLocation());
12053 }
12054 
12055 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12056   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12057   C->setLParenLoc(Record.readSourceLocation());
12058   C->setDefaultKindKwLoc(Record.readSourceLocation());
12059 }
12060 
12061 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12062   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12063   C->setLParenLoc(Record.readSourceLocation());
12064   C->setProcBindKindKwLoc(Record.readSourceLocation());
12065 }
12066 
12067 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12068   VisitOMPClauseWithPreInit(C);
12069   C->setScheduleKind(
12070        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12071   C->setFirstScheduleModifier(
12072       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12073   C->setSecondScheduleModifier(
12074       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12075   C->setChunkSize(Record.readSubExpr());
12076   C->setLParenLoc(Record.readSourceLocation());
12077   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12078   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12079   C->setScheduleKindLoc(Record.readSourceLocation());
12080   C->setCommaLoc(Record.readSourceLocation());
12081 }
12082 
12083 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12084   C->setNumForLoops(Record.readSubExpr());
12085   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12086     C->setLoopNumIterations(I, Record.readSubExpr());
12087   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12088     C->setLoopCounter(I, Record.readSubExpr());
12089   C->setLParenLoc(Record.readSourceLocation());
12090 }
12091 
12092 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12093   C->setEventHandler(Record.readSubExpr());
12094   C->setLParenLoc(Record.readSourceLocation());
12095 }
12096 
12097 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12098 
12099 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12100 
12101 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12102 
12103 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12104 
12105 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12106 
12107 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12108   if (C->isExtended()) {
12109     C->setLParenLoc(Record.readSourceLocation());
12110     C->setArgumentLoc(Record.readSourceLocation());
12111     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12112   }
12113 }
12114 
12115 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12116 
12117 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12118 
12119 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12120 
12121 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12122 
12123 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12124 
12125 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12126 
12127 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12128 
12129 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12130 
12131 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12132 
12133 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12134 
12135 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12136 
12137 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12138     OMPUnifiedSharedMemoryClause *) {}
12139 
12140 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12141 
12142 void
12143 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12144 }
12145 
12146 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12147     OMPAtomicDefaultMemOrderClause *C) {
12148   C->setAtomicDefaultMemOrderKind(
12149       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12150   C->setLParenLoc(Record.readSourceLocation());
12151   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12152 }
12153 
12154 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12155   C->setLParenLoc(Record.readSourceLocation());
12156   unsigned NumVars = C->varlist_size();
12157   SmallVector<Expr *, 16> Vars;
12158   Vars.reserve(NumVars);
12159   for (unsigned i = 0; i != NumVars; ++i)
12160     Vars.push_back(Record.readSubExpr());
12161   C->setVarRefs(Vars);
12162   Vars.clear();
12163   for (unsigned i = 0; i != NumVars; ++i)
12164     Vars.push_back(Record.readSubExpr());
12165   C->setPrivateCopies(Vars);
12166 }
12167 
12168 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12169   VisitOMPClauseWithPreInit(C);
12170   C->setLParenLoc(Record.readSourceLocation());
12171   unsigned NumVars = C->varlist_size();
12172   SmallVector<Expr *, 16> Vars;
12173   Vars.reserve(NumVars);
12174   for (unsigned i = 0; i != NumVars; ++i)
12175     Vars.push_back(Record.readSubExpr());
12176   C->setVarRefs(Vars);
12177   Vars.clear();
12178   for (unsigned i = 0; i != NumVars; ++i)
12179     Vars.push_back(Record.readSubExpr());
12180   C->setPrivateCopies(Vars);
12181   Vars.clear();
12182   for (unsigned i = 0; i != NumVars; ++i)
12183     Vars.push_back(Record.readSubExpr());
12184   C->setInits(Vars);
12185 }
12186 
12187 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12188   VisitOMPClauseWithPostUpdate(C);
12189   C->setLParenLoc(Record.readSourceLocation());
12190   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12191   C->setKindLoc(Record.readSourceLocation());
12192   C->setColonLoc(Record.readSourceLocation());
12193   unsigned NumVars = C->varlist_size();
12194   SmallVector<Expr *, 16> Vars;
12195   Vars.reserve(NumVars);
12196   for (unsigned i = 0; i != NumVars; ++i)
12197     Vars.push_back(Record.readSubExpr());
12198   C->setVarRefs(Vars);
12199   Vars.clear();
12200   for (unsigned i = 0; i != NumVars; ++i)
12201     Vars.push_back(Record.readSubExpr());
12202   C->setPrivateCopies(Vars);
12203   Vars.clear();
12204   for (unsigned i = 0; i != NumVars; ++i)
12205     Vars.push_back(Record.readSubExpr());
12206   C->setSourceExprs(Vars);
12207   Vars.clear();
12208   for (unsigned i = 0; i != NumVars; ++i)
12209     Vars.push_back(Record.readSubExpr());
12210   C->setDestinationExprs(Vars);
12211   Vars.clear();
12212   for (unsigned i = 0; i != NumVars; ++i)
12213     Vars.push_back(Record.readSubExpr());
12214   C->setAssignmentOps(Vars);
12215 }
12216 
12217 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12218   C->setLParenLoc(Record.readSourceLocation());
12219   unsigned NumVars = C->varlist_size();
12220   SmallVector<Expr *, 16> Vars;
12221   Vars.reserve(NumVars);
12222   for (unsigned i = 0; i != NumVars; ++i)
12223     Vars.push_back(Record.readSubExpr());
12224   C->setVarRefs(Vars);
12225 }
12226 
12227 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12228   VisitOMPClauseWithPostUpdate(C);
12229   C->setLParenLoc(Record.readSourceLocation());
12230   C->setModifierLoc(Record.readSourceLocation());
12231   C->setColonLoc(Record.readSourceLocation());
12232   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12233   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12234   C->setQualifierLoc(NNSL);
12235   C->setNameInfo(DNI);
12236 
12237   unsigned NumVars = C->varlist_size();
12238   SmallVector<Expr *, 16> Vars;
12239   Vars.reserve(NumVars);
12240   for (unsigned i = 0; i != NumVars; ++i)
12241     Vars.push_back(Record.readSubExpr());
12242   C->setVarRefs(Vars);
12243   Vars.clear();
12244   for (unsigned i = 0; i != NumVars; ++i)
12245     Vars.push_back(Record.readSubExpr());
12246   C->setPrivates(Vars);
12247   Vars.clear();
12248   for (unsigned i = 0; i != NumVars; ++i)
12249     Vars.push_back(Record.readSubExpr());
12250   C->setLHSExprs(Vars);
12251   Vars.clear();
12252   for (unsigned i = 0; i != NumVars; ++i)
12253     Vars.push_back(Record.readSubExpr());
12254   C->setRHSExprs(Vars);
12255   Vars.clear();
12256   for (unsigned i = 0; i != NumVars; ++i)
12257     Vars.push_back(Record.readSubExpr());
12258   C->setReductionOps(Vars);
12259   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12260     Vars.clear();
12261     for (unsigned i = 0; i != NumVars; ++i)
12262       Vars.push_back(Record.readSubExpr());
12263     C->setInscanCopyOps(Vars);
12264     Vars.clear();
12265     for (unsigned i = 0; i != NumVars; ++i)
12266       Vars.push_back(Record.readSubExpr());
12267     C->setInscanCopyArrayTemps(Vars);
12268     Vars.clear();
12269     for (unsigned i = 0; i != NumVars; ++i)
12270       Vars.push_back(Record.readSubExpr());
12271     C->setInscanCopyArrayElems(Vars);
12272   }
12273 }
12274 
12275 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12276   VisitOMPClauseWithPostUpdate(C);
12277   C->setLParenLoc(Record.readSourceLocation());
12278   C->setColonLoc(Record.readSourceLocation());
12279   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12280   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12281   C->setQualifierLoc(NNSL);
12282   C->setNameInfo(DNI);
12283 
12284   unsigned NumVars = C->varlist_size();
12285   SmallVector<Expr *, 16> Vars;
12286   Vars.reserve(NumVars);
12287   for (unsigned I = 0; I != NumVars; ++I)
12288     Vars.push_back(Record.readSubExpr());
12289   C->setVarRefs(Vars);
12290   Vars.clear();
12291   for (unsigned I = 0; I != NumVars; ++I)
12292     Vars.push_back(Record.readSubExpr());
12293   C->setPrivates(Vars);
12294   Vars.clear();
12295   for (unsigned I = 0; I != NumVars; ++I)
12296     Vars.push_back(Record.readSubExpr());
12297   C->setLHSExprs(Vars);
12298   Vars.clear();
12299   for (unsigned I = 0; I != NumVars; ++I)
12300     Vars.push_back(Record.readSubExpr());
12301   C->setRHSExprs(Vars);
12302   Vars.clear();
12303   for (unsigned I = 0; I != NumVars; ++I)
12304     Vars.push_back(Record.readSubExpr());
12305   C->setReductionOps(Vars);
12306 }
12307 
12308 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12309   VisitOMPClauseWithPostUpdate(C);
12310   C->setLParenLoc(Record.readSourceLocation());
12311   C->setColonLoc(Record.readSourceLocation());
12312   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12313   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12314   C->setQualifierLoc(NNSL);
12315   C->setNameInfo(DNI);
12316 
12317   unsigned NumVars = C->varlist_size();
12318   SmallVector<Expr *, 16> Vars;
12319   Vars.reserve(NumVars);
12320   for (unsigned I = 0; I != NumVars; ++I)
12321     Vars.push_back(Record.readSubExpr());
12322   C->setVarRefs(Vars);
12323   Vars.clear();
12324   for (unsigned I = 0; I != NumVars; ++I)
12325     Vars.push_back(Record.readSubExpr());
12326   C->setPrivates(Vars);
12327   Vars.clear();
12328   for (unsigned I = 0; I != NumVars; ++I)
12329     Vars.push_back(Record.readSubExpr());
12330   C->setLHSExprs(Vars);
12331   Vars.clear();
12332   for (unsigned I = 0; I != NumVars; ++I)
12333     Vars.push_back(Record.readSubExpr());
12334   C->setRHSExprs(Vars);
12335   Vars.clear();
12336   for (unsigned I = 0; I != NumVars; ++I)
12337     Vars.push_back(Record.readSubExpr());
12338   C->setReductionOps(Vars);
12339   Vars.clear();
12340   for (unsigned I = 0; I != NumVars; ++I)
12341     Vars.push_back(Record.readSubExpr());
12342   C->setTaskgroupDescriptors(Vars);
12343 }
12344 
12345 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12346   VisitOMPClauseWithPostUpdate(C);
12347   C->setLParenLoc(Record.readSourceLocation());
12348   C->setColonLoc(Record.readSourceLocation());
12349   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12350   C->setModifierLoc(Record.readSourceLocation());
12351   unsigned NumVars = C->varlist_size();
12352   SmallVector<Expr *, 16> Vars;
12353   Vars.reserve(NumVars);
12354   for (unsigned i = 0; i != NumVars; ++i)
12355     Vars.push_back(Record.readSubExpr());
12356   C->setVarRefs(Vars);
12357   Vars.clear();
12358   for (unsigned i = 0; i != NumVars; ++i)
12359     Vars.push_back(Record.readSubExpr());
12360   C->setPrivates(Vars);
12361   Vars.clear();
12362   for (unsigned i = 0; i != NumVars; ++i)
12363     Vars.push_back(Record.readSubExpr());
12364   C->setInits(Vars);
12365   Vars.clear();
12366   for (unsigned i = 0; i != NumVars; ++i)
12367     Vars.push_back(Record.readSubExpr());
12368   C->setUpdates(Vars);
12369   Vars.clear();
12370   for (unsigned i = 0; i != NumVars; ++i)
12371     Vars.push_back(Record.readSubExpr());
12372   C->setFinals(Vars);
12373   C->setStep(Record.readSubExpr());
12374   C->setCalcStep(Record.readSubExpr());
12375   Vars.clear();
12376   for (unsigned I = 0; I != NumVars + 1; ++I)
12377     Vars.push_back(Record.readSubExpr());
12378   C->setUsedExprs(Vars);
12379 }
12380 
12381 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12382   C->setLParenLoc(Record.readSourceLocation());
12383   C->setColonLoc(Record.readSourceLocation());
12384   unsigned NumVars = C->varlist_size();
12385   SmallVector<Expr *, 16> Vars;
12386   Vars.reserve(NumVars);
12387   for (unsigned i = 0; i != NumVars; ++i)
12388     Vars.push_back(Record.readSubExpr());
12389   C->setVarRefs(Vars);
12390   C->setAlignment(Record.readSubExpr());
12391 }
12392 
12393 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12394   C->setLParenLoc(Record.readSourceLocation());
12395   unsigned NumVars = C->varlist_size();
12396   SmallVector<Expr *, 16> Exprs;
12397   Exprs.reserve(NumVars);
12398   for (unsigned i = 0; i != NumVars; ++i)
12399     Exprs.push_back(Record.readSubExpr());
12400   C->setVarRefs(Exprs);
12401   Exprs.clear();
12402   for (unsigned i = 0; i != NumVars; ++i)
12403     Exprs.push_back(Record.readSubExpr());
12404   C->setSourceExprs(Exprs);
12405   Exprs.clear();
12406   for (unsigned i = 0; i != NumVars; ++i)
12407     Exprs.push_back(Record.readSubExpr());
12408   C->setDestinationExprs(Exprs);
12409   Exprs.clear();
12410   for (unsigned i = 0; i != NumVars; ++i)
12411     Exprs.push_back(Record.readSubExpr());
12412   C->setAssignmentOps(Exprs);
12413 }
12414 
12415 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12416   C->setLParenLoc(Record.readSourceLocation());
12417   unsigned NumVars = C->varlist_size();
12418   SmallVector<Expr *, 16> Exprs;
12419   Exprs.reserve(NumVars);
12420   for (unsigned i = 0; i != NumVars; ++i)
12421     Exprs.push_back(Record.readSubExpr());
12422   C->setVarRefs(Exprs);
12423   Exprs.clear();
12424   for (unsigned i = 0; i != NumVars; ++i)
12425     Exprs.push_back(Record.readSubExpr());
12426   C->setSourceExprs(Exprs);
12427   Exprs.clear();
12428   for (unsigned i = 0; i != NumVars; ++i)
12429     Exprs.push_back(Record.readSubExpr());
12430   C->setDestinationExprs(Exprs);
12431   Exprs.clear();
12432   for (unsigned i = 0; i != NumVars; ++i)
12433     Exprs.push_back(Record.readSubExpr());
12434   C->setAssignmentOps(Exprs);
12435 }
12436 
12437 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12438   C->setLParenLoc(Record.readSourceLocation());
12439   unsigned NumVars = C->varlist_size();
12440   SmallVector<Expr *, 16> Vars;
12441   Vars.reserve(NumVars);
12442   for (unsigned i = 0; i != NumVars; ++i)
12443     Vars.push_back(Record.readSubExpr());
12444   C->setVarRefs(Vars);
12445 }
12446 
12447 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12448   C->setDepobj(Record.readSubExpr());
12449   C->setLParenLoc(Record.readSourceLocation());
12450 }
12451 
12452 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12453   C->setLParenLoc(Record.readSourceLocation());
12454   C->setModifier(Record.readSubExpr());
12455   C->setDependencyKind(
12456       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12457   C->setDependencyLoc(Record.readSourceLocation());
12458   C->setColonLoc(Record.readSourceLocation());
12459   unsigned NumVars = C->varlist_size();
12460   SmallVector<Expr *, 16> Vars;
12461   Vars.reserve(NumVars);
12462   for (unsigned I = 0; I != NumVars; ++I)
12463     Vars.push_back(Record.readSubExpr());
12464   C->setVarRefs(Vars);
12465   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12466     C->setLoopData(I, Record.readSubExpr());
12467 }
12468 
12469 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12470   VisitOMPClauseWithPreInit(C);
12471   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12472   C->setDevice(Record.readSubExpr());
12473   C->setModifierLoc(Record.readSourceLocation());
12474   C->setLParenLoc(Record.readSourceLocation());
12475 }
12476 
12477 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12478   C->setLParenLoc(Record.readSourceLocation());
12479   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12480     C->setMapTypeModifier(
12481         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12482     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12483   }
12484   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12485   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12486   C->setMapType(
12487      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12488   C->setMapLoc(Record.readSourceLocation());
12489   C->setColonLoc(Record.readSourceLocation());
12490   auto NumVars = C->varlist_size();
12491   auto UniqueDecls = C->getUniqueDeclarationsNum();
12492   auto TotalLists = C->getTotalComponentListNum();
12493   auto TotalComponents = C->getTotalComponentsNum();
12494 
12495   SmallVector<Expr *, 16> Vars;
12496   Vars.reserve(NumVars);
12497   for (unsigned i = 0; i != NumVars; ++i)
12498     Vars.push_back(Record.readExpr());
12499   C->setVarRefs(Vars);
12500 
12501   SmallVector<Expr *, 16> UDMappers;
12502   UDMappers.reserve(NumVars);
12503   for (unsigned I = 0; I < NumVars; ++I)
12504     UDMappers.push_back(Record.readExpr());
12505   C->setUDMapperRefs(UDMappers);
12506 
12507   SmallVector<ValueDecl *, 16> Decls;
12508   Decls.reserve(UniqueDecls);
12509   for (unsigned i = 0; i < UniqueDecls; ++i)
12510     Decls.push_back(Record.readDeclAs<ValueDecl>());
12511   C->setUniqueDecls(Decls);
12512 
12513   SmallVector<unsigned, 16> ListsPerDecl;
12514   ListsPerDecl.reserve(UniqueDecls);
12515   for (unsigned i = 0; i < UniqueDecls; ++i)
12516     ListsPerDecl.push_back(Record.readInt());
12517   C->setDeclNumLists(ListsPerDecl);
12518 
12519   SmallVector<unsigned, 32> ListSizes;
12520   ListSizes.reserve(TotalLists);
12521   for (unsigned i = 0; i < TotalLists; ++i)
12522     ListSizes.push_back(Record.readInt());
12523   C->setComponentListSizes(ListSizes);
12524 
12525   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12526   Components.reserve(TotalComponents);
12527   for (unsigned i = 0; i < TotalComponents; ++i) {
12528     Expr *AssociatedExpr = Record.readExpr();
12529     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12530     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12531         AssociatedExpr, AssociatedDecl));
12532   }
12533   C->setComponents(Components, ListSizes);
12534 }
12535 
12536 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12537   C->setLParenLoc(Record.readSourceLocation());
12538   C->setColonLoc(Record.readSourceLocation());
12539   C->setAllocator(Record.readSubExpr());
12540   unsigned NumVars = C->varlist_size();
12541   SmallVector<Expr *, 16> Vars;
12542   Vars.reserve(NumVars);
12543   for (unsigned i = 0; i != NumVars; ++i)
12544     Vars.push_back(Record.readSubExpr());
12545   C->setVarRefs(Vars);
12546 }
12547 
12548 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12549   VisitOMPClauseWithPreInit(C);
12550   C->setNumTeams(Record.readSubExpr());
12551   C->setLParenLoc(Record.readSourceLocation());
12552 }
12553 
12554 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12555   VisitOMPClauseWithPreInit(C);
12556   C->setThreadLimit(Record.readSubExpr());
12557   C->setLParenLoc(Record.readSourceLocation());
12558 }
12559 
12560 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12561   VisitOMPClauseWithPreInit(C);
12562   C->setPriority(Record.readSubExpr());
12563   C->setLParenLoc(Record.readSourceLocation());
12564 }
12565 
12566 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12567   VisitOMPClauseWithPreInit(C);
12568   C->setGrainsize(Record.readSubExpr());
12569   C->setLParenLoc(Record.readSourceLocation());
12570 }
12571 
12572 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12573   VisitOMPClauseWithPreInit(C);
12574   C->setNumTasks(Record.readSubExpr());
12575   C->setLParenLoc(Record.readSourceLocation());
12576 }
12577 
12578 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12579   C->setHint(Record.readSubExpr());
12580   C->setLParenLoc(Record.readSourceLocation());
12581 }
12582 
12583 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12584   VisitOMPClauseWithPreInit(C);
12585   C->setDistScheduleKind(
12586       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12587   C->setChunkSize(Record.readSubExpr());
12588   C->setLParenLoc(Record.readSourceLocation());
12589   C->setDistScheduleKindLoc(Record.readSourceLocation());
12590   C->setCommaLoc(Record.readSourceLocation());
12591 }
12592 
12593 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12594   C->setDefaultmapKind(
12595        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12596   C->setDefaultmapModifier(
12597       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12598   C->setLParenLoc(Record.readSourceLocation());
12599   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12600   C->setDefaultmapKindLoc(Record.readSourceLocation());
12601 }
12602 
12603 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12604   C->setLParenLoc(Record.readSourceLocation());
12605   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12606     C->setMotionModifier(
12607         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12608     C->setMotionModifierLoc(I, Record.readSourceLocation());
12609   }
12610   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12611   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12612   C->setColonLoc(Record.readSourceLocation());
12613   auto NumVars = C->varlist_size();
12614   auto UniqueDecls = C->getUniqueDeclarationsNum();
12615   auto TotalLists = C->getTotalComponentListNum();
12616   auto TotalComponents = C->getTotalComponentsNum();
12617 
12618   SmallVector<Expr *, 16> Vars;
12619   Vars.reserve(NumVars);
12620   for (unsigned i = 0; i != NumVars; ++i)
12621     Vars.push_back(Record.readSubExpr());
12622   C->setVarRefs(Vars);
12623 
12624   SmallVector<Expr *, 16> UDMappers;
12625   UDMappers.reserve(NumVars);
12626   for (unsigned I = 0; I < NumVars; ++I)
12627     UDMappers.push_back(Record.readSubExpr());
12628   C->setUDMapperRefs(UDMappers);
12629 
12630   SmallVector<ValueDecl *, 16> Decls;
12631   Decls.reserve(UniqueDecls);
12632   for (unsigned i = 0; i < UniqueDecls; ++i)
12633     Decls.push_back(Record.readDeclAs<ValueDecl>());
12634   C->setUniqueDecls(Decls);
12635 
12636   SmallVector<unsigned, 16> ListsPerDecl;
12637   ListsPerDecl.reserve(UniqueDecls);
12638   for (unsigned i = 0; i < UniqueDecls; ++i)
12639     ListsPerDecl.push_back(Record.readInt());
12640   C->setDeclNumLists(ListsPerDecl);
12641 
12642   SmallVector<unsigned, 32> ListSizes;
12643   ListSizes.reserve(TotalLists);
12644   for (unsigned i = 0; i < TotalLists; ++i)
12645     ListSizes.push_back(Record.readInt());
12646   C->setComponentListSizes(ListSizes);
12647 
12648   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12649   Components.reserve(TotalComponents);
12650   for (unsigned i = 0; i < TotalComponents; ++i) {
12651     Expr *AssociatedExpr = Record.readSubExpr();
12652     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12653     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12654         AssociatedExpr, AssociatedDecl));
12655   }
12656   C->setComponents(Components, ListSizes);
12657 }
12658 
12659 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12660   C->setLParenLoc(Record.readSourceLocation());
12661   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12662     C->setMotionModifier(
12663         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12664     C->setMotionModifierLoc(I, Record.readSourceLocation());
12665   }
12666   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12667   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12668   C->setColonLoc(Record.readSourceLocation());
12669   auto NumVars = C->varlist_size();
12670   auto UniqueDecls = C->getUniqueDeclarationsNum();
12671   auto TotalLists = C->getTotalComponentListNum();
12672   auto TotalComponents = C->getTotalComponentsNum();
12673 
12674   SmallVector<Expr *, 16> Vars;
12675   Vars.reserve(NumVars);
12676   for (unsigned i = 0; i != NumVars; ++i)
12677     Vars.push_back(Record.readSubExpr());
12678   C->setVarRefs(Vars);
12679 
12680   SmallVector<Expr *, 16> UDMappers;
12681   UDMappers.reserve(NumVars);
12682   for (unsigned I = 0; I < NumVars; ++I)
12683     UDMappers.push_back(Record.readSubExpr());
12684   C->setUDMapperRefs(UDMappers);
12685 
12686   SmallVector<ValueDecl *, 16> Decls;
12687   Decls.reserve(UniqueDecls);
12688   for (unsigned i = 0; i < UniqueDecls; ++i)
12689     Decls.push_back(Record.readDeclAs<ValueDecl>());
12690   C->setUniqueDecls(Decls);
12691 
12692   SmallVector<unsigned, 16> ListsPerDecl;
12693   ListsPerDecl.reserve(UniqueDecls);
12694   for (unsigned i = 0; i < UniqueDecls; ++i)
12695     ListsPerDecl.push_back(Record.readInt());
12696   C->setDeclNumLists(ListsPerDecl);
12697 
12698   SmallVector<unsigned, 32> ListSizes;
12699   ListSizes.reserve(TotalLists);
12700   for (unsigned i = 0; i < TotalLists; ++i)
12701     ListSizes.push_back(Record.readInt());
12702   C->setComponentListSizes(ListSizes);
12703 
12704   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12705   Components.reserve(TotalComponents);
12706   for (unsigned i = 0; i < TotalComponents; ++i) {
12707     Expr *AssociatedExpr = Record.readSubExpr();
12708     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12709     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12710         AssociatedExpr, AssociatedDecl));
12711   }
12712   C->setComponents(Components, ListSizes);
12713 }
12714 
12715 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12716   C->setLParenLoc(Record.readSourceLocation());
12717   auto NumVars = C->varlist_size();
12718   auto UniqueDecls = C->getUniqueDeclarationsNum();
12719   auto TotalLists = C->getTotalComponentListNum();
12720   auto TotalComponents = C->getTotalComponentsNum();
12721 
12722   SmallVector<Expr *, 16> Vars;
12723   Vars.reserve(NumVars);
12724   for (unsigned i = 0; i != NumVars; ++i)
12725     Vars.push_back(Record.readSubExpr());
12726   C->setVarRefs(Vars);
12727   Vars.clear();
12728   for (unsigned i = 0; i != NumVars; ++i)
12729     Vars.push_back(Record.readSubExpr());
12730   C->setPrivateCopies(Vars);
12731   Vars.clear();
12732   for (unsigned i = 0; i != NumVars; ++i)
12733     Vars.push_back(Record.readSubExpr());
12734   C->setInits(Vars);
12735 
12736   SmallVector<ValueDecl *, 16> Decls;
12737   Decls.reserve(UniqueDecls);
12738   for (unsigned i = 0; i < UniqueDecls; ++i)
12739     Decls.push_back(Record.readDeclAs<ValueDecl>());
12740   C->setUniqueDecls(Decls);
12741 
12742   SmallVector<unsigned, 16> ListsPerDecl;
12743   ListsPerDecl.reserve(UniqueDecls);
12744   for (unsigned i = 0; i < UniqueDecls; ++i)
12745     ListsPerDecl.push_back(Record.readInt());
12746   C->setDeclNumLists(ListsPerDecl);
12747 
12748   SmallVector<unsigned, 32> ListSizes;
12749   ListSizes.reserve(TotalLists);
12750   for (unsigned i = 0; i < TotalLists; ++i)
12751     ListSizes.push_back(Record.readInt());
12752   C->setComponentListSizes(ListSizes);
12753 
12754   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12755   Components.reserve(TotalComponents);
12756   for (unsigned i = 0; i < TotalComponents; ++i) {
12757     Expr *AssociatedExpr = Record.readSubExpr();
12758     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12759     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12760         AssociatedExpr, AssociatedDecl));
12761   }
12762   C->setComponents(Components, ListSizes);
12763 }
12764 
12765 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12766   C->setLParenLoc(Record.readSourceLocation());
12767   auto NumVars = C->varlist_size();
12768   auto UniqueDecls = C->getUniqueDeclarationsNum();
12769   auto TotalLists = C->getTotalComponentListNum();
12770   auto TotalComponents = C->getTotalComponentsNum();
12771 
12772   SmallVector<Expr *, 16> Vars;
12773   Vars.reserve(NumVars);
12774   for (unsigned i = 0; i != NumVars; ++i)
12775     Vars.push_back(Record.readSubExpr());
12776   C->setVarRefs(Vars);
12777 
12778   SmallVector<ValueDecl *, 16> Decls;
12779   Decls.reserve(UniqueDecls);
12780   for (unsigned i = 0; i < UniqueDecls; ++i)
12781     Decls.push_back(Record.readDeclAs<ValueDecl>());
12782   C->setUniqueDecls(Decls);
12783 
12784   SmallVector<unsigned, 16> ListsPerDecl;
12785   ListsPerDecl.reserve(UniqueDecls);
12786   for (unsigned i = 0; i < UniqueDecls; ++i)
12787     ListsPerDecl.push_back(Record.readInt());
12788   C->setDeclNumLists(ListsPerDecl);
12789 
12790   SmallVector<unsigned, 32> ListSizes;
12791   ListSizes.reserve(TotalLists);
12792   for (unsigned i = 0; i < TotalLists; ++i)
12793     ListSizes.push_back(Record.readInt());
12794   C->setComponentListSizes(ListSizes);
12795 
12796   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12797   Components.reserve(TotalComponents);
12798   for (unsigned i = 0; i < TotalComponents; ++i) {
12799     Expr *AssociatedExpr = Record.readSubExpr();
12800     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12801     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12802         AssociatedExpr, AssociatedDecl));
12803   }
12804   C->setComponents(Components, ListSizes);
12805 }
12806 
12807 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12808   C->setLParenLoc(Record.readSourceLocation());
12809   auto NumVars = C->varlist_size();
12810   auto UniqueDecls = C->getUniqueDeclarationsNum();
12811   auto TotalLists = C->getTotalComponentListNum();
12812   auto TotalComponents = C->getTotalComponentsNum();
12813 
12814   SmallVector<Expr *, 16> Vars;
12815   Vars.reserve(NumVars);
12816   for (unsigned i = 0; i != NumVars; ++i)
12817     Vars.push_back(Record.readSubExpr());
12818   C->setVarRefs(Vars);
12819   Vars.clear();
12820 
12821   SmallVector<ValueDecl *, 16> Decls;
12822   Decls.reserve(UniqueDecls);
12823   for (unsigned i = 0; i < UniqueDecls; ++i)
12824     Decls.push_back(Record.readDeclAs<ValueDecl>());
12825   C->setUniqueDecls(Decls);
12826 
12827   SmallVector<unsigned, 16> ListsPerDecl;
12828   ListsPerDecl.reserve(UniqueDecls);
12829   for (unsigned i = 0; i < UniqueDecls; ++i)
12830     ListsPerDecl.push_back(Record.readInt());
12831   C->setDeclNumLists(ListsPerDecl);
12832 
12833   SmallVector<unsigned, 32> ListSizes;
12834   ListSizes.reserve(TotalLists);
12835   for (unsigned i = 0; i < TotalLists; ++i)
12836     ListSizes.push_back(Record.readInt());
12837   C->setComponentListSizes(ListSizes);
12838 
12839   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12840   Components.reserve(TotalComponents);
12841   for (unsigned i = 0; i < TotalComponents; ++i) {
12842     Expr *AssociatedExpr = Record.readSubExpr();
12843     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12844     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12845         AssociatedExpr, AssociatedDecl));
12846   }
12847   C->setComponents(Components, ListSizes);
12848 }
12849 
12850 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12851   C->setLParenLoc(Record.readSourceLocation());
12852   unsigned NumVars = C->varlist_size();
12853   SmallVector<Expr *, 16> Vars;
12854   Vars.reserve(NumVars);
12855   for (unsigned i = 0; i != NumVars; ++i)
12856     Vars.push_back(Record.readSubExpr());
12857   C->setVarRefs(Vars);
12858   Vars.clear();
12859   Vars.reserve(NumVars);
12860   for (unsigned i = 0; i != NumVars; ++i)
12861     Vars.push_back(Record.readSubExpr());
12862   C->setPrivateRefs(Vars);
12863 }
12864 
12865 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12866   C->setLParenLoc(Record.readSourceLocation());
12867   unsigned NumVars = C->varlist_size();
12868   SmallVector<Expr *, 16> Vars;
12869   Vars.reserve(NumVars);
12870   for (unsigned i = 0; i != NumVars; ++i)
12871     Vars.push_back(Record.readSubExpr());
12872   C->setVarRefs(Vars);
12873 }
12874 
12875 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12876   C->setLParenLoc(Record.readSourceLocation());
12877   unsigned NumVars = C->varlist_size();
12878   SmallVector<Expr *, 16> Vars;
12879   Vars.reserve(NumVars);
12880   for (unsigned i = 0; i != NumVars; ++i)
12881     Vars.push_back(Record.readSubExpr());
12882   C->setVarRefs(Vars);
12883 }
12884 
12885 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12886   C->setLParenLoc(Record.readSourceLocation());
12887   unsigned NumOfAllocators = C->getNumberOfAllocators();
12888   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12889   Data.reserve(NumOfAllocators);
12890   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12891     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12892     D.Allocator = Record.readSubExpr();
12893     D.AllocatorTraits = Record.readSubExpr();
12894     D.LParenLoc = Record.readSourceLocation();
12895     D.RParenLoc = Record.readSourceLocation();
12896   }
12897   C->setAllocatorsData(Data);
12898 }
12899 
12900 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12901   C->setLParenLoc(Record.readSourceLocation());
12902   C->setModifier(Record.readSubExpr());
12903   C->setColonLoc(Record.readSourceLocation());
12904   unsigned NumOfLocators = C->varlist_size();
12905   SmallVector<Expr *, 4> Locators;
12906   Locators.reserve(NumOfLocators);
12907   for (unsigned I = 0; I != NumOfLocators; ++I)
12908     Locators.push_back(Record.readSubExpr());
12909   C->setVarRefs(Locators);
12910 }
12911 
12912 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12913   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12914   C->setLParenLoc(Record.readSourceLocation());
12915   C->setKindKwLoc(Record.readSourceLocation());
12916 }
12917 
12918 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12919   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12920   TI.Sets.resize(readUInt32());
12921   for (auto &Set : TI.Sets) {
12922     Set.Kind = readEnum<llvm::omp::TraitSet>();
12923     Set.Selectors.resize(readUInt32());
12924     for (auto &Selector : Set.Selectors) {
12925       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12926       Selector.ScoreOrCondition = nullptr;
12927       if (readBool())
12928         Selector.ScoreOrCondition = readExprRef();
12929       Selector.Properties.resize(readUInt32());
12930       for (auto &Property : Selector.Properties)
12931         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12932     }
12933   }
12934   return &TI;
12935 }
12936 
12937 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12938   if (!Data)
12939     return;
12940   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12941     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12942     skipInts(3);
12943   }
12944   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12945   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12946     Clauses[I] = readOMPClause();
12947   Data->setClauses(Clauses);
12948   if (Data->hasAssociatedStmt())
12949     Data->setAssociatedStmt(readStmt());
12950   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12951     Data->getChildren()[I] = readStmt();
12952 }
12953