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