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 FLOAT_CONTROL_PRAGMA_OPTIONS: {
3783       if (Record.size() < 3) {
3784         Error("invalid pragma pack record");
3785         return Failure;
3786       }
3787       FpPragmaCurrentValue = Record[0];
3788       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3789       unsigned NumStackEntries = Record[2];
3790       unsigned Idx = 3;
3791       // Reset the stack when importing a new module.
3792       FpPragmaStack.clear();
3793       for (unsigned I = 0; I < NumStackEntries; ++I) {
3794         FpPragmaStackEntry Entry;
3795         Entry.Value = Record[Idx++];
3796         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3797         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3798         FpPragmaStrings.push_back(ReadString(Record, Idx));
3799         Entry.SlotLabel = FpPragmaStrings.back();
3800         FpPragmaStack.push_back(Entry);
3801       }
3802       break;
3803     }
3804 
3805     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3806       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3807         DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3808       break;
3809     }
3810   }
3811 }
3812 
3813 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3814   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3815 
3816   // Additional remapping information.
3817   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3818   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3819   F.ModuleOffsetMap = StringRef();
3820 
3821   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3822   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3823     F.SLocRemap.insert(std::make_pair(0U, 0));
3824     F.SLocRemap.insert(std::make_pair(2U, 1));
3825   }
3826 
3827   // Continuous range maps we may be updating in our module.
3828   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3829   RemapBuilder SLocRemap(F.SLocRemap);
3830   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3831   RemapBuilder MacroRemap(F.MacroRemap);
3832   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3833   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3834   RemapBuilder SelectorRemap(F.SelectorRemap);
3835   RemapBuilder DeclRemap(F.DeclRemap);
3836   RemapBuilder TypeRemap(F.TypeRemap);
3837 
3838   while (Data < DataEnd) {
3839     // FIXME: Looking up dependency modules by filename is horrible. Let's
3840     // start fixing this with prebuilt and explicit modules and see how it
3841     // goes...
3842     using namespace llvm::support;
3843     ModuleKind Kind = static_cast<ModuleKind>(
3844       endian::readNext<uint8_t, little, unaligned>(Data));
3845     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3846     StringRef Name = StringRef((const char*)Data, Len);
3847     Data += Len;
3848     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule
3849                       ? ModuleMgr.lookupByModuleName(Name)
3850                       : ModuleMgr.lookupByFileName(Name));
3851     if (!OM) {
3852       std::string Msg =
3853           "SourceLocation remap refers to unknown module, cannot find ";
3854       Msg.append(std::string(Name));
3855       Error(Msg);
3856       return;
3857     }
3858 
3859     uint32_t SLocOffset =
3860         endian::readNext<uint32_t, little, unaligned>(Data);
3861     uint32_t IdentifierIDOffset =
3862         endian::readNext<uint32_t, little, unaligned>(Data);
3863     uint32_t MacroIDOffset =
3864         endian::readNext<uint32_t, little, unaligned>(Data);
3865     uint32_t PreprocessedEntityIDOffset =
3866         endian::readNext<uint32_t, little, unaligned>(Data);
3867     uint32_t SubmoduleIDOffset =
3868         endian::readNext<uint32_t, little, unaligned>(Data);
3869     uint32_t SelectorIDOffset =
3870         endian::readNext<uint32_t, little, unaligned>(Data);
3871     uint32_t DeclIDOffset =
3872         endian::readNext<uint32_t, little, unaligned>(Data);
3873     uint32_t TypeIndexOffset =
3874         endian::readNext<uint32_t, little, unaligned>(Data);
3875 
3876     uint32_t None = std::numeric_limits<uint32_t>::max();
3877 
3878     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3879                          RemapBuilder &Remap) {
3880       if (Offset != None)
3881         Remap.insert(std::make_pair(Offset,
3882                                     static_cast<int>(BaseOffset - Offset)));
3883     };
3884     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3885     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3886     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3887     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3888               PreprocessedEntityRemap);
3889     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3890     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3891     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3892     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3893 
3894     // Global -> local mappings.
3895     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3896   }
3897 }
3898 
3899 ASTReader::ASTReadResult
3900 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3901                                   const ModuleFile *ImportedBy,
3902                                   unsigned ClientLoadCapabilities) {
3903   unsigned Idx = 0;
3904   F.ModuleMapPath = ReadPath(F, Record, Idx);
3905 
3906   // Try to resolve ModuleName in the current header search context and
3907   // verify that it is found in the same module map file as we saved. If the
3908   // top-level AST file is a main file, skip this check because there is no
3909   // usable header search context.
3910   assert(!F.ModuleName.empty() &&
3911          "MODULE_NAME should come before MODULE_MAP_FILE");
3912   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3913     // An implicitly-loaded module file should have its module listed in some
3914     // module map file that we've already loaded.
3915     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3916     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3917     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3918     // Don't emit module relocation error if we have -fno-validate-pch
3919     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3920       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3921         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3922           // This module was defined by an imported (explicit) module.
3923           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3924                                                << ASTFE->getName();
3925         } else {
3926           // This module was built with a different module map.
3927           Diag(diag::err_imported_module_not_found)
3928               << F.ModuleName << F.FileName
3929               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3930               << !ImportedBy;
3931           // In case it was imported by a PCH, there's a chance the user is
3932           // just missing to include the search path to the directory containing
3933           // the modulemap.
3934           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3935             Diag(diag::note_imported_by_pch_module_not_found)
3936                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3937         }
3938       }
3939       return OutOfDate;
3940     }
3941 
3942     assert(M->Name == F.ModuleName && "found module with different name");
3943 
3944     // Check the primary module map file.
3945     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3946     if (!StoredModMap || *StoredModMap != ModMap) {
3947       assert(ModMap && "found module is missing module map file");
3948       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3949              "top-level import should be verified");
3950       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3951       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3952         Diag(diag::err_imported_module_modmap_changed)
3953             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3954             << ModMap->getName() << F.ModuleMapPath << NotImported;
3955       return OutOfDate;
3956     }
3957 
3958     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3959     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3960       // FIXME: we should use input files rather than storing names.
3961       std::string Filename = ReadPath(F, Record, Idx);
3962       auto F = FileMgr.getFile(Filename, false, false);
3963       if (!F) {
3964         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3965           Error("could not find file '" + Filename +"' referenced by AST file");
3966         return OutOfDate;
3967       }
3968       AdditionalStoredMaps.insert(*F);
3969     }
3970 
3971     // Check any additional module map files (e.g. module.private.modulemap)
3972     // that are not in the pcm.
3973     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3974       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3975         // Remove files that match
3976         // Note: SmallPtrSet::erase is really remove
3977         if (!AdditionalStoredMaps.erase(ModMap)) {
3978           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3979             Diag(diag::err_module_different_modmap)
3980               << F.ModuleName << /*new*/0 << ModMap->getName();
3981           return OutOfDate;
3982         }
3983       }
3984     }
3985 
3986     // Check any additional module map files that are in the pcm, but not
3987     // found in header search. Cases that match are already removed.
3988     for (const FileEntry *ModMap : AdditionalStoredMaps) {
3989       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3990         Diag(diag::err_module_different_modmap)
3991           << F.ModuleName << /*not new*/1 << ModMap->getName();
3992       return OutOfDate;
3993     }
3994   }
3995 
3996   if (Listener)
3997     Listener->ReadModuleMapFile(F.ModuleMapPath);
3998   return Success;
3999 }
4000 
4001 /// Move the given method to the back of the global list of methods.
4002 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4003   // Find the entry for this selector in the method pool.
4004   Sema::GlobalMethodPool::iterator Known
4005     = S.MethodPool.find(Method->getSelector());
4006   if (Known == S.MethodPool.end())
4007     return;
4008 
4009   // Retrieve the appropriate method list.
4010   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4011                                                     : Known->second.second;
4012   bool Found = false;
4013   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4014     if (!Found) {
4015       if (List->getMethod() == Method) {
4016         Found = true;
4017       } else {
4018         // Keep searching.
4019         continue;
4020       }
4021     }
4022 
4023     if (List->getNext())
4024       List->setMethod(List->getNext()->getMethod());
4025     else
4026       List->setMethod(Method);
4027   }
4028 }
4029 
4030 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4031   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4032   for (Decl *D : Names) {
4033     bool wasHidden = D->isHidden();
4034     D->setVisibleDespiteOwningModule();
4035 
4036     if (wasHidden && SemaObj) {
4037       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4038         moveMethodToBackOfGlobalList(*SemaObj, Method);
4039       }
4040     }
4041   }
4042 }
4043 
4044 void ASTReader::makeModuleVisible(Module *Mod,
4045                                   Module::NameVisibilityKind NameVisibility,
4046                                   SourceLocation ImportLoc) {
4047   llvm::SmallPtrSet<Module *, 4> Visited;
4048   SmallVector<Module *, 4> Stack;
4049   Stack.push_back(Mod);
4050   while (!Stack.empty()) {
4051     Mod = Stack.pop_back_val();
4052 
4053     if (NameVisibility <= Mod->NameVisibility) {
4054       // This module already has this level of visibility (or greater), so
4055       // there is nothing more to do.
4056       continue;
4057     }
4058 
4059     if (Mod->isUnimportable()) {
4060       // Modules that aren't importable cannot be made visible.
4061       continue;
4062     }
4063 
4064     // Update the module's name visibility.
4065     Mod->NameVisibility = NameVisibility;
4066 
4067     // If we've already deserialized any names from this module,
4068     // mark them as visible.
4069     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4070     if (Hidden != HiddenNamesMap.end()) {
4071       auto HiddenNames = std::move(*Hidden);
4072       HiddenNamesMap.erase(Hidden);
4073       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4074       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4075              "making names visible added hidden names");
4076     }
4077 
4078     // Push any exported modules onto the stack to be marked as visible.
4079     SmallVector<Module *, 16> Exports;
4080     Mod->getExportedModules(Exports);
4081     for (SmallVectorImpl<Module *>::iterator
4082            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4083       Module *Exported = *I;
4084       if (Visited.insert(Exported).second)
4085         Stack.push_back(Exported);
4086     }
4087   }
4088 }
4089 
4090 /// We've merged the definition \p MergedDef into the existing definition
4091 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4092 /// visible.
4093 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4094                                           NamedDecl *MergedDef) {
4095   if (Def->isHidden()) {
4096     // If MergedDef is visible or becomes visible, make the definition visible.
4097     if (!MergedDef->isHidden())
4098       Def->setVisibleDespiteOwningModule();
4099     else {
4100       getContext().mergeDefinitionIntoModule(
4101           Def, MergedDef->getImportedOwningModule(),
4102           /*NotifyListeners*/ false);
4103       PendingMergedDefinitionsToDeduplicate.insert(Def);
4104     }
4105   }
4106 }
4107 
4108 bool ASTReader::loadGlobalIndex() {
4109   if (GlobalIndex)
4110     return false;
4111 
4112   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4113       !PP.getLangOpts().Modules)
4114     return true;
4115 
4116   // Try to load the global index.
4117   TriedLoadingGlobalIndex = true;
4118   StringRef ModuleCachePath
4119     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4120   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4121       GlobalModuleIndex::readIndex(ModuleCachePath);
4122   if (llvm::Error Err = std::move(Result.second)) {
4123     assert(!Result.first);
4124     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4125     return true;
4126   }
4127 
4128   GlobalIndex.reset(Result.first);
4129   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4130   return false;
4131 }
4132 
4133 bool ASTReader::isGlobalIndexUnavailable() const {
4134   return PP.getLangOpts().Modules && UseGlobalIndex &&
4135          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4136 }
4137 
4138 static void updateModuleTimestamp(ModuleFile &MF) {
4139   // Overwrite the timestamp file contents so that file's mtime changes.
4140   std::string TimestampFilename = MF.getTimestampFilename();
4141   std::error_code EC;
4142   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4143   if (EC)
4144     return;
4145   OS << "Timestamp file\n";
4146   OS.close();
4147   OS.clear_error(); // Avoid triggering a fatal error.
4148 }
4149 
4150 /// Given a cursor at the start of an AST file, scan ahead and drop the
4151 /// cursor into the start of the given block ID, returning false on success and
4152 /// true on failure.
4153 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4154   while (true) {
4155     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4156     if (!MaybeEntry) {
4157       // FIXME this drops errors on the floor.
4158       consumeError(MaybeEntry.takeError());
4159       return true;
4160     }
4161     llvm::BitstreamEntry Entry = MaybeEntry.get();
4162 
4163     switch (Entry.Kind) {
4164     case llvm::BitstreamEntry::Error:
4165     case llvm::BitstreamEntry::EndBlock:
4166       return true;
4167 
4168     case llvm::BitstreamEntry::Record:
4169       // Ignore top-level records.
4170       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4171         break;
4172       else {
4173         // FIXME this drops errors on the floor.
4174         consumeError(Skipped.takeError());
4175         return true;
4176       }
4177 
4178     case llvm::BitstreamEntry::SubBlock:
4179       if (Entry.ID == BlockID) {
4180         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4181           // FIXME this drops the error on the floor.
4182           consumeError(std::move(Err));
4183           return true;
4184         }
4185         // Found it!
4186         return false;
4187       }
4188 
4189       if (llvm::Error Err = Cursor.SkipBlock()) {
4190         // FIXME this drops the error on the floor.
4191         consumeError(std::move(Err));
4192         return true;
4193       }
4194     }
4195   }
4196 }
4197 
4198 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4199                                             ModuleKind Type,
4200                                             SourceLocation ImportLoc,
4201                                             unsigned ClientLoadCapabilities,
4202                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4203   llvm::SaveAndRestore<SourceLocation>
4204     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4205 
4206   // Defer any pending actions until we get to the end of reading the AST file.
4207   Deserializing AnASTFile(this);
4208 
4209   // Bump the generation number.
4210   unsigned PreviousGeneration = 0;
4211   if (ContextObj)
4212     PreviousGeneration = incrementGeneration(*ContextObj);
4213 
4214   unsigned NumModules = ModuleMgr.size();
4215   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4216     assert(ReadResult && "expected to return error");
4217     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4218                             PP.getLangOpts().Modules
4219                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4220                                 : nullptr);
4221 
4222     // If we find that any modules are unusable, the global index is going
4223     // to be out-of-date. Just remove it.
4224     GlobalIndex.reset();
4225     ModuleMgr.setGlobalIndex(nullptr);
4226     return ReadResult;
4227   };
4228 
4229   SmallVector<ImportedModule, 4> Loaded;
4230   switch (ASTReadResult ReadResult =
4231               ReadASTCore(FileName, Type, ImportLoc,
4232                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4233                           ASTFileSignature(), ClientLoadCapabilities)) {
4234   case Failure:
4235   case Missing:
4236   case OutOfDate:
4237   case VersionMismatch:
4238   case ConfigurationMismatch:
4239   case HadErrors:
4240     return removeModulesAndReturn(ReadResult);
4241   case Success:
4242     break;
4243   }
4244 
4245   // Here comes stuff that we only do once the entire chain is loaded.
4246 
4247   // Load the AST blocks of all of the modules that we loaded.  We can still
4248   // hit errors parsing the ASTs at this point.
4249   for (ImportedModule &M : Loaded) {
4250     ModuleFile &F = *M.Mod;
4251 
4252     // Read the AST block.
4253     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4254       return removeModulesAndReturn(Result);
4255 
4256     // The AST block should always have a definition for the main module.
4257     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4258       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4259       return removeModulesAndReturn(Failure);
4260     }
4261 
4262     // Read the extension blocks.
4263     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4264       if (ASTReadResult Result = ReadExtensionBlock(F))
4265         return removeModulesAndReturn(Result);
4266     }
4267 
4268     // Once read, set the ModuleFile bit base offset and update the size in
4269     // bits of all files we've seen.
4270     F.GlobalBitOffset = TotalModulesSizeInBits;
4271     TotalModulesSizeInBits += F.SizeInBits;
4272     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4273   }
4274 
4275   // Preload source locations and interesting indentifiers.
4276   for (ImportedModule &M : Loaded) {
4277     ModuleFile &F = *M.Mod;
4278 
4279     // Preload SLocEntries.
4280     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4281       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4282       // Load it through the SourceManager and don't call ReadSLocEntry()
4283       // directly because the entry may have already been loaded in which case
4284       // calling ReadSLocEntry() directly would trigger an assertion in
4285       // SourceManager.
4286       SourceMgr.getLoadedSLocEntryByID(Index);
4287     }
4288 
4289     // Map the original source file ID into the ID space of the current
4290     // compilation.
4291     if (F.OriginalSourceFileID.isValid()) {
4292       F.OriginalSourceFileID = FileID::get(
4293           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4294     }
4295 
4296     // Preload all the pending interesting identifiers by marking them out of
4297     // date.
4298     for (auto Offset : F.PreloadIdentifierOffsets) {
4299       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4300           F.IdentifierTableData + Offset);
4301 
4302       ASTIdentifierLookupTrait Trait(*this, F);
4303       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4304       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4305       auto &II = PP.getIdentifierTable().getOwn(Key);
4306       II.setOutOfDate(true);
4307 
4308       // Mark this identifier as being from an AST file so that we can track
4309       // whether we need to serialize it.
4310       markIdentifierFromAST(*this, II);
4311 
4312       // Associate the ID with the identifier so that the writer can reuse it.
4313       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4314       SetIdentifierInfo(ID, &II);
4315     }
4316   }
4317 
4318   // Setup the import locations and notify the module manager that we've
4319   // committed to these module files.
4320   for (ImportedModule &M : Loaded) {
4321     ModuleFile &F = *M.Mod;
4322 
4323     ModuleMgr.moduleFileAccepted(&F);
4324 
4325     // Set the import location.
4326     F.DirectImportLoc = ImportLoc;
4327     // FIXME: We assume that locations from PCH / preamble do not need
4328     // any translation.
4329     if (!M.ImportedBy)
4330       F.ImportLoc = M.ImportLoc;
4331     else
4332       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4333   }
4334 
4335   if (!PP.getLangOpts().CPlusPlus ||
4336       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4337        Type != MK_PrebuiltModule)) {
4338     // Mark all of the identifiers in the identifier table as being out of date,
4339     // so that various accessors know to check the loaded modules when the
4340     // identifier is used.
4341     //
4342     // For C++ modules, we don't need information on many identifiers (just
4343     // those that provide macros or are poisoned), so we mark all of
4344     // the interesting ones via PreloadIdentifierOffsets.
4345     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4346                                 IdEnd = PP.getIdentifierTable().end();
4347          Id != IdEnd; ++Id)
4348       Id->second->setOutOfDate(true);
4349   }
4350   // Mark selectors as out of date.
4351   for (auto Sel : SelectorGeneration)
4352     SelectorOutOfDate[Sel.first] = true;
4353 
4354   // Resolve any unresolved module exports.
4355   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4356     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4357     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4358     Module *ResolvedMod = getSubmodule(GlobalID);
4359 
4360     switch (Unresolved.Kind) {
4361     case UnresolvedModuleRef::Conflict:
4362       if (ResolvedMod) {
4363         Module::Conflict Conflict;
4364         Conflict.Other = ResolvedMod;
4365         Conflict.Message = Unresolved.String.str();
4366         Unresolved.Mod->Conflicts.push_back(Conflict);
4367       }
4368       continue;
4369 
4370     case UnresolvedModuleRef::Import:
4371       if (ResolvedMod)
4372         Unresolved.Mod->Imports.insert(ResolvedMod);
4373       continue;
4374 
4375     case UnresolvedModuleRef::Export:
4376       if (ResolvedMod || Unresolved.IsWildcard)
4377         Unresolved.Mod->Exports.push_back(
4378           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4379       continue;
4380     }
4381   }
4382   UnresolvedModuleRefs.clear();
4383 
4384   if (Imported)
4385     Imported->append(ImportedModules.begin(),
4386                      ImportedModules.end());
4387 
4388   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4389   // Might be unnecessary as use declarations are only used to build the
4390   // module itself.
4391 
4392   if (ContextObj)
4393     InitializeContext();
4394 
4395   if (SemaObj)
4396     UpdateSema();
4397 
4398   if (DeserializationListener)
4399     DeserializationListener->ReaderInitialized(this);
4400 
4401   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4402   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4403     // If this AST file is a precompiled preamble, then set the
4404     // preamble file ID of the source manager to the file source file
4405     // from which the preamble was built.
4406     if (Type == MK_Preamble) {
4407       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4408     } else if (Type == MK_MainFile) {
4409       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4410     }
4411   }
4412 
4413   // For any Objective-C class definitions we have already loaded, make sure
4414   // that we load any additional categories.
4415   if (ContextObj) {
4416     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4417       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4418                          ObjCClassesLoaded[I],
4419                          PreviousGeneration);
4420     }
4421   }
4422 
4423   if (PP.getHeaderSearchInfo()
4424           .getHeaderSearchOpts()
4425           .ModulesValidateOncePerBuildSession) {
4426     // Now we are certain that the module and all modules it depends on are
4427     // up to date.  Create or update timestamp files for modules that are
4428     // located in the module cache (not for PCH files that could be anywhere
4429     // in the filesystem).
4430     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4431       ImportedModule &M = Loaded[I];
4432       if (M.Mod->Kind == MK_ImplicitModule) {
4433         updateModuleTimestamp(*M.Mod);
4434       }
4435     }
4436   }
4437 
4438   return Success;
4439 }
4440 
4441 static ASTFileSignature readASTFileSignature(StringRef PCH);
4442 
4443 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4444 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4445   // FIXME checking magic headers is done in other places such as
4446   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4447   // always done the same. Unify it all with a helper.
4448   if (!Stream.canSkipToPos(4))
4449     return llvm::createStringError(std::errc::illegal_byte_sequence,
4450                                    "file too small to contain AST file magic");
4451   for (unsigned C : {'C', 'P', 'C', 'H'})
4452     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4453       if (Res.get() != C)
4454         return llvm::createStringError(
4455             std::errc::illegal_byte_sequence,
4456             "file doesn't start with AST file magic");
4457     } else
4458       return Res.takeError();
4459   return llvm::Error::success();
4460 }
4461 
4462 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4463   switch (Kind) {
4464   case MK_PCH:
4465     return 0; // PCH
4466   case MK_ImplicitModule:
4467   case MK_ExplicitModule:
4468   case MK_PrebuiltModule:
4469     return 1; // module
4470   case MK_MainFile:
4471   case MK_Preamble:
4472     return 2; // main source file
4473   }
4474   llvm_unreachable("unknown module kind");
4475 }
4476 
4477 ASTReader::ASTReadResult
4478 ASTReader::ReadASTCore(StringRef FileName,
4479                        ModuleKind Type,
4480                        SourceLocation ImportLoc,
4481                        ModuleFile *ImportedBy,
4482                        SmallVectorImpl<ImportedModule> &Loaded,
4483                        off_t ExpectedSize, time_t ExpectedModTime,
4484                        ASTFileSignature ExpectedSignature,
4485                        unsigned ClientLoadCapabilities) {
4486   ModuleFile *M;
4487   std::string ErrorStr;
4488   ModuleManager::AddModuleResult AddResult
4489     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4490                           getGeneration(), ExpectedSize, ExpectedModTime,
4491                           ExpectedSignature, readASTFileSignature,
4492                           M, ErrorStr);
4493 
4494   switch (AddResult) {
4495   case ModuleManager::AlreadyLoaded:
4496     Diag(diag::remark_module_import)
4497         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4498         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4499     return Success;
4500 
4501   case ModuleManager::NewlyLoaded:
4502     // Load module file below.
4503     break;
4504 
4505   case ModuleManager::Missing:
4506     // The module file was missing; if the client can handle that, return
4507     // it.
4508     if (ClientLoadCapabilities & ARR_Missing)
4509       return Missing;
4510 
4511     // Otherwise, return an error.
4512     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4513                                           << FileName << !ErrorStr.empty()
4514                                           << ErrorStr;
4515     return Failure;
4516 
4517   case ModuleManager::OutOfDate:
4518     // We couldn't load the module file because it is out-of-date. If the
4519     // client can handle out-of-date, return it.
4520     if (ClientLoadCapabilities & ARR_OutOfDate)
4521       return OutOfDate;
4522 
4523     // Otherwise, return an error.
4524     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4525                                             << FileName << !ErrorStr.empty()
4526                                             << ErrorStr;
4527     return Failure;
4528   }
4529 
4530   assert(M && "Missing module file");
4531 
4532   bool ShouldFinalizePCM = false;
4533   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4534     auto &MC = getModuleManager().getModuleCache();
4535     if (ShouldFinalizePCM)
4536       MC.finalizePCM(FileName);
4537     else
4538       MC.tryToDropPCM(FileName);
4539   });
4540   ModuleFile &F = *M;
4541   BitstreamCursor &Stream = F.Stream;
4542   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4543   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4544 
4545   // Sniff for the signature.
4546   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4547     Diag(diag::err_module_file_invalid)
4548         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4549     return Failure;
4550   }
4551 
4552   // This is used for compatibility with older PCH formats.
4553   bool HaveReadControlBlock = false;
4554   while (true) {
4555     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4556     if (!MaybeEntry) {
4557       Error(MaybeEntry.takeError());
4558       return Failure;
4559     }
4560     llvm::BitstreamEntry Entry = MaybeEntry.get();
4561 
4562     switch (Entry.Kind) {
4563     case llvm::BitstreamEntry::Error:
4564     case llvm::BitstreamEntry::Record:
4565     case llvm::BitstreamEntry::EndBlock:
4566       Error("invalid record at top-level of AST file");
4567       return Failure;
4568 
4569     case llvm::BitstreamEntry::SubBlock:
4570       break;
4571     }
4572 
4573     switch (Entry.ID) {
4574     case CONTROL_BLOCK_ID:
4575       HaveReadControlBlock = true;
4576       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4577       case Success:
4578         // Check that we didn't try to load a non-module AST file as a module.
4579         //
4580         // FIXME: Should we also perform the converse check? Loading a module as
4581         // a PCH file sort of works, but it's a bit wonky.
4582         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4583              Type == MK_PrebuiltModule) &&
4584             F.ModuleName.empty()) {
4585           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4586           if (Result != OutOfDate ||
4587               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4588             Diag(diag::err_module_file_not_module) << FileName;
4589           return Result;
4590         }
4591         break;
4592 
4593       case Failure: return Failure;
4594       case Missing: return Missing;
4595       case OutOfDate: return OutOfDate;
4596       case VersionMismatch: return VersionMismatch;
4597       case ConfigurationMismatch: return ConfigurationMismatch;
4598       case HadErrors: return HadErrors;
4599       }
4600       break;
4601 
4602     case AST_BLOCK_ID:
4603       if (!HaveReadControlBlock) {
4604         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4605           Diag(diag::err_pch_version_too_old);
4606         return VersionMismatch;
4607       }
4608 
4609       // Record that we've loaded this module.
4610       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4611       ShouldFinalizePCM = true;
4612       return Success;
4613 
4614     case UNHASHED_CONTROL_BLOCK_ID:
4615       // This block is handled using look-ahead during ReadControlBlock.  We
4616       // shouldn't get here!
4617       Error("malformed block record in AST file");
4618       return Failure;
4619 
4620     default:
4621       if (llvm::Error Err = Stream.SkipBlock()) {
4622         Error(std::move(Err));
4623         return Failure;
4624       }
4625       break;
4626     }
4627   }
4628 
4629   llvm_unreachable("unexpected break; expected return");
4630 }
4631 
4632 ASTReader::ASTReadResult
4633 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4634                                     unsigned ClientLoadCapabilities) {
4635   const HeaderSearchOptions &HSOpts =
4636       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4637   bool AllowCompatibleConfigurationMismatch =
4638       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4639 
4640   ASTReadResult Result = readUnhashedControlBlockImpl(
4641       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4642       Listener.get(),
4643       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4644 
4645   // If F was directly imported by another module, it's implicitly validated by
4646   // the importing module.
4647   if (DisableValidation || WasImportedBy ||
4648       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4649     return Success;
4650 
4651   if (Result == Failure) {
4652     Error("malformed block record in AST file");
4653     return Failure;
4654   }
4655 
4656   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4657     // If this module has already been finalized in the ModuleCache, we're stuck
4658     // with it; we can only load a single version of each module.
4659     //
4660     // This can happen when a module is imported in two contexts: in one, as a
4661     // user module; in another, as a system module (due to an import from
4662     // another module marked with the [system] flag).  It usually indicates a
4663     // bug in the module map: this module should also be marked with [system].
4664     //
4665     // If -Wno-system-headers (the default), and the first import is as a
4666     // system module, then validation will fail during the as-user import,
4667     // since -Werror flags won't have been validated.  However, it's reasonable
4668     // to treat this consistently as a system module.
4669     //
4670     // If -Wsystem-headers, the PCM on disk was built with
4671     // -Wno-system-headers, and the first import is as a user module, then
4672     // validation will fail during the as-system import since the PCM on disk
4673     // doesn't guarantee that -Werror was respected.  However, the -Werror
4674     // flags were checked during the initial as-user import.
4675     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4676       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4677       return Success;
4678     }
4679   }
4680 
4681   return Result;
4682 }
4683 
4684 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4685     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4686     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4687     bool ValidateDiagnosticOptions) {
4688   // Initialize a stream.
4689   BitstreamCursor Stream(StreamData);
4690 
4691   // Sniff for the signature.
4692   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4693     // FIXME this drops the error on the floor.
4694     consumeError(std::move(Err));
4695     return Failure;
4696   }
4697 
4698   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4699   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4700     return Failure;
4701 
4702   // Read all of the records in the options block.
4703   RecordData Record;
4704   ASTReadResult Result = Success;
4705   while (true) {
4706     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4707     if (!MaybeEntry) {
4708       // FIXME this drops the error on the floor.
4709       consumeError(MaybeEntry.takeError());
4710       return Failure;
4711     }
4712     llvm::BitstreamEntry Entry = MaybeEntry.get();
4713 
4714     switch (Entry.Kind) {
4715     case llvm::BitstreamEntry::Error:
4716     case llvm::BitstreamEntry::SubBlock:
4717       return Failure;
4718 
4719     case llvm::BitstreamEntry::EndBlock:
4720       return Result;
4721 
4722     case llvm::BitstreamEntry::Record:
4723       // The interesting case.
4724       break;
4725     }
4726 
4727     // Read and process a record.
4728     Record.clear();
4729     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4730     if (!MaybeRecordType) {
4731       // FIXME this drops the error.
4732       return Failure;
4733     }
4734     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4735     case SIGNATURE:
4736       if (F)
4737         std::copy(Record.begin(), Record.end(), F->Signature.data());
4738       break;
4739     case DIAGNOSTIC_OPTIONS: {
4740       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4741       if (Listener && ValidateDiagnosticOptions &&
4742           !AllowCompatibleConfigurationMismatch &&
4743           ParseDiagnosticOptions(Record, Complain, *Listener))
4744         Result = OutOfDate; // Don't return early.  Read the signature.
4745       break;
4746     }
4747     case DIAG_PRAGMA_MAPPINGS:
4748       if (!F)
4749         break;
4750       if (F->PragmaDiagMappings.empty())
4751         F->PragmaDiagMappings.swap(Record);
4752       else
4753         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4754                                      Record.begin(), Record.end());
4755       break;
4756     }
4757   }
4758 }
4759 
4760 /// Parse a record and blob containing module file extension metadata.
4761 static bool parseModuleFileExtensionMetadata(
4762               const SmallVectorImpl<uint64_t> &Record,
4763               StringRef Blob,
4764               ModuleFileExtensionMetadata &Metadata) {
4765   if (Record.size() < 4) return true;
4766 
4767   Metadata.MajorVersion = Record[0];
4768   Metadata.MinorVersion = Record[1];
4769 
4770   unsigned BlockNameLen = Record[2];
4771   unsigned UserInfoLen = Record[3];
4772 
4773   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4774 
4775   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4776   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4777                                   Blob.data() + BlockNameLen + UserInfoLen);
4778   return false;
4779 }
4780 
4781 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4782   BitstreamCursor &Stream = F.Stream;
4783 
4784   RecordData Record;
4785   while (true) {
4786     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4787     if (!MaybeEntry) {
4788       Error(MaybeEntry.takeError());
4789       return Failure;
4790     }
4791     llvm::BitstreamEntry Entry = MaybeEntry.get();
4792 
4793     switch (Entry.Kind) {
4794     case llvm::BitstreamEntry::SubBlock:
4795       if (llvm::Error Err = Stream.SkipBlock()) {
4796         Error(std::move(Err));
4797         return Failure;
4798       }
4799       continue;
4800 
4801     case llvm::BitstreamEntry::EndBlock:
4802       return Success;
4803 
4804     case llvm::BitstreamEntry::Error:
4805       return HadErrors;
4806 
4807     case llvm::BitstreamEntry::Record:
4808       break;
4809     }
4810 
4811     Record.clear();
4812     StringRef Blob;
4813     Expected<unsigned> MaybeRecCode =
4814         Stream.readRecord(Entry.ID, Record, &Blob);
4815     if (!MaybeRecCode) {
4816       Error(MaybeRecCode.takeError());
4817       return Failure;
4818     }
4819     switch (MaybeRecCode.get()) {
4820     case EXTENSION_METADATA: {
4821       ModuleFileExtensionMetadata Metadata;
4822       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4823         Error("malformed EXTENSION_METADATA in AST file");
4824         return Failure;
4825       }
4826 
4827       // Find a module file extension with this block name.
4828       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4829       if (Known == ModuleFileExtensions.end()) break;
4830 
4831       // Form a reader.
4832       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4833                                                              F, Stream)) {
4834         F.ExtensionReaders.push_back(std::move(Reader));
4835       }
4836 
4837       break;
4838     }
4839     }
4840   }
4841 
4842   return Success;
4843 }
4844 
4845 void ASTReader::InitializeContext() {
4846   assert(ContextObj && "no context to initialize");
4847   ASTContext &Context = *ContextObj;
4848 
4849   // If there's a listener, notify them that we "read" the translation unit.
4850   if (DeserializationListener)
4851     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4852                                       Context.getTranslationUnitDecl());
4853 
4854   // FIXME: Find a better way to deal with collisions between these
4855   // built-in types. Right now, we just ignore the problem.
4856 
4857   // Load the special types.
4858   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4859     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4860       if (!Context.CFConstantStringTypeDecl)
4861         Context.setCFConstantStringType(GetType(String));
4862     }
4863 
4864     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4865       QualType FileType = GetType(File);
4866       if (FileType.isNull()) {
4867         Error("FILE type is NULL");
4868         return;
4869       }
4870 
4871       if (!Context.FILEDecl) {
4872         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4873           Context.setFILEDecl(Typedef->getDecl());
4874         else {
4875           const TagType *Tag = FileType->getAs<TagType>();
4876           if (!Tag) {
4877             Error("Invalid FILE type in AST file");
4878             return;
4879           }
4880           Context.setFILEDecl(Tag->getDecl());
4881         }
4882       }
4883     }
4884 
4885     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4886       QualType Jmp_bufType = GetType(Jmp_buf);
4887       if (Jmp_bufType.isNull()) {
4888         Error("jmp_buf type is NULL");
4889         return;
4890       }
4891 
4892       if (!Context.jmp_bufDecl) {
4893         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4894           Context.setjmp_bufDecl(Typedef->getDecl());
4895         else {
4896           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4897           if (!Tag) {
4898             Error("Invalid jmp_buf type in AST file");
4899             return;
4900           }
4901           Context.setjmp_bufDecl(Tag->getDecl());
4902         }
4903       }
4904     }
4905 
4906     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4907       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4908       if (Sigjmp_bufType.isNull()) {
4909         Error("sigjmp_buf type is NULL");
4910         return;
4911       }
4912 
4913       if (!Context.sigjmp_bufDecl) {
4914         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4915           Context.setsigjmp_bufDecl(Typedef->getDecl());
4916         else {
4917           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4918           assert(Tag && "Invalid sigjmp_buf type in AST file");
4919           Context.setsigjmp_bufDecl(Tag->getDecl());
4920         }
4921       }
4922     }
4923 
4924     if (unsigned ObjCIdRedef
4925           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4926       if (Context.ObjCIdRedefinitionType.isNull())
4927         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4928     }
4929 
4930     if (unsigned ObjCClassRedef
4931           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4932       if (Context.ObjCClassRedefinitionType.isNull())
4933         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4934     }
4935 
4936     if (unsigned ObjCSelRedef
4937           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4938       if (Context.ObjCSelRedefinitionType.isNull())
4939         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4940     }
4941 
4942     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4943       QualType Ucontext_tType = GetType(Ucontext_t);
4944       if (Ucontext_tType.isNull()) {
4945         Error("ucontext_t type is NULL");
4946         return;
4947       }
4948 
4949       if (!Context.ucontext_tDecl) {
4950         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4951           Context.setucontext_tDecl(Typedef->getDecl());
4952         else {
4953           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4954           assert(Tag && "Invalid ucontext_t type in AST file");
4955           Context.setucontext_tDecl(Tag->getDecl());
4956         }
4957       }
4958     }
4959   }
4960 
4961   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4962 
4963   // If there were any CUDA special declarations, deserialize them.
4964   if (!CUDASpecialDeclRefs.empty()) {
4965     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4966     Context.setcudaConfigureCallDecl(
4967                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4968   }
4969 
4970   // Re-export any modules that were imported by a non-module AST file.
4971   // FIXME: This does not make macro-only imports visible again.
4972   for (auto &Import : ImportedModules) {
4973     if (Module *Imported = getSubmodule(Import.ID)) {
4974       makeModuleVisible(Imported, Module::AllVisible,
4975                         /*ImportLoc=*/Import.ImportLoc);
4976       if (Import.ImportLoc.isValid())
4977         PP.makeModuleVisible(Imported, Import.ImportLoc);
4978       // FIXME: should we tell Sema to make the module visible too?
4979     }
4980   }
4981   ImportedModules.clear();
4982 }
4983 
4984 void ASTReader::finalizeForWriting() {
4985   // Nothing to do for now.
4986 }
4987 
4988 /// Reads and return the signature record from \p PCH's control block, or
4989 /// else returns 0.
4990 static ASTFileSignature readASTFileSignature(StringRef PCH) {
4991   BitstreamCursor Stream(PCH);
4992   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4993     // FIXME this drops the error on the floor.
4994     consumeError(std::move(Err));
4995     return ASTFileSignature();
4996   }
4997 
4998   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4999   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5000     return ASTFileSignature();
5001 
5002   // Scan for SIGNATURE inside the diagnostic options block.
5003   ASTReader::RecordData Record;
5004   while (true) {
5005     Expected<llvm::BitstreamEntry> MaybeEntry =
5006         Stream.advanceSkippingSubblocks();
5007     if (!MaybeEntry) {
5008       // FIXME this drops the error on the floor.
5009       consumeError(MaybeEntry.takeError());
5010       return ASTFileSignature();
5011     }
5012     llvm::BitstreamEntry Entry = MaybeEntry.get();
5013 
5014     if (Entry.Kind != llvm::BitstreamEntry::Record)
5015       return ASTFileSignature();
5016 
5017     Record.clear();
5018     StringRef Blob;
5019     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5020     if (!MaybeRecord) {
5021       // FIXME this drops the error on the floor.
5022       consumeError(MaybeRecord.takeError());
5023       return ASTFileSignature();
5024     }
5025     if (SIGNATURE == MaybeRecord.get())
5026       return {{{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2],
5027                 (uint32_t)Record[3], (uint32_t)Record[4]}}};
5028   }
5029 }
5030 
5031 /// Retrieve the name of the original source file name
5032 /// directly from the AST file, without actually loading the AST
5033 /// file.
5034 std::string ASTReader::getOriginalSourceFile(
5035     const std::string &ASTFileName, FileManager &FileMgr,
5036     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5037   // Open the AST file.
5038   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5039   if (!Buffer) {
5040     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5041         << ASTFileName << Buffer.getError().message();
5042     return std::string();
5043   }
5044 
5045   // Initialize the stream
5046   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5047 
5048   // Sniff for the signature.
5049   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5050     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5051     return std::string();
5052   }
5053 
5054   // Scan for the CONTROL_BLOCK_ID block.
5055   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5056     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5057     return std::string();
5058   }
5059 
5060   // Scan for ORIGINAL_FILE inside the control block.
5061   RecordData Record;
5062   while (true) {
5063     Expected<llvm::BitstreamEntry> MaybeEntry =
5064         Stream.advanceSkippingSubblocks();
5065     if (!MaybeEntry) {
5066       // FIXME this drops errors on the floor.
5067       consumeError(MaybeEntry.takeError());
5068       return std::string();
5069     }
5070     llvm::BitstreamEntry Entry = MaybeEntry.get();
5071 
5072     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5073       return std::string();
5074 
5075     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5076       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5077       return std::string();
5078     }
5079 
5080     Record.clear();
5081     StringRef Blob;
5082     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5083     if (!MaybeRecord) {
5084       // FIXME this drops the errors on the floor.
5085       consumeError(MaybeRecord.takeError());
5086       return std::string();
5087     }
5088     if (ORIGINAL_FILE == MaybeRecord.get())
5089       return Blob.str();
5090   }
5091 }
5092 
5093 namespace {
5094 
5095   class SimplePCHValidator : public ASTReaderListener {
5096     const LangOptions &ExistingLangOpts;
5097     const TargetOptions &ExistingTargetOpts;
5098     const PreprocessorOptions &ExistingPPOpts;
5099     std::string ExistingModuleCachePath;
5100     FileManager &FileMgr;
5101 
5102   public:
5103     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5104                        const TargetOptions &ExistingTargetOpts,
5105                        const PreprocessorOptions &ExistingPPOpts,
5106                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5107         : ExistingLangOpts(ExistingLangOpts),
5108           ExistingTargetOpts(ExistingTargetOpts),
5109           ExistingPPOpts(ExistingPPOpts),
5110           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5111 
5112     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5113                              bool AllowCompatibleDifferences) override {
5114       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5115                                   AllowCompatibleDifferences);
5116     }
5117 
5118     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5119                            bool AllowCompatibleDifferences) override {
5120       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5121                                 AllowCompatibleDifferences);
5122     }
5123 
5124     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5125                                  StringRef SpecificModuleCachePath,
5126                                  bool Complain) override {
5127       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5128                                       ExistingModuleCachePath,
5129                                       nullptr, ExistingLangOpts);
5130     }
5131 
5132     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5133                                  bool Complain,
5134                                  std::string &SuggestedPredefines) override {
5135       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5136                                       SuggestedPredefines, ExistingLangOpts);
5137     }
5138   };
5139 
5140 } // namespace
5141 
5142 bool ASTReader::readASTFileControlBlock(
5143     StringRef Filename, FileManager &FileMgr,
5144     const PCHContainerReader &PCHContainerRdr,
5145     bool FindModuleFileExtensions,
5146     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5147   // Open the AST file.
5148   // FIXME: This allows use of the VFS; we do not allow use of the
5149   // VFS when actually loading a module.
5150   auto Buffer = FileMgr.getBufferForFile(Filename);
5151   if (!Buffer) {
5152     return true;
5153   }
5154 
5155   // Initialize the stream
5156   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5157   BitstreamCursor Stream(Bytes);
5158 
5159   // Sniff for the signature.
5160   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5161     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5162     return true;
5163   }
5164 
5165   // Scan for the CONTROL_BLOCK_ID block.
5166   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5167     return true;
5168 
5169   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5170   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5171   bool NeedsImports = Listener.needsImportVisitation();
5172   BitstreamCursor InputFilesCursor;
5173 
5174   RecordData Record;
5175   std::string ModuleDir;
5176   bool DoneWithControlBlock = false;
5177   while (!DoneWithControlBlock) {
5178     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5179     if (!MaybeEntry) {
5180       // FIXME this drops the error on the floor.
5181       consumeError(MaybeEntry.takeError());
5182       return true;
5183     }
5184     llvm::BitstreamEntry Entry = MaybeEntry.get();
5185 
5186     switch (Entry.Kind) {
5187     case llvm::BitstreamEntry::SubBlock: {
5188       switch (Entry.ID) {
5189       case OPTIONS_BLOCK_ID: {
5190         std::string IgnoredSuggestedPredefines;
5191         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5192                              /*AllowCompatibleConfigurationMismatch*/ false,
5193                              Listener, IgnoredSuggestedPredefines) != Success)
5194           return true;
5195         break;
5196       }
5197 
5198       case INPUT_FILES_BLOCK_ID:
5199         InputFilesCursor = Stream;
5200         if (llvm::Error Err = Stream.SkipBlock()) {
5201           // FIXME this drops the error on the floor.
5202           consumeError(std::move(Err));
5203           return true;
5204         }
5205         if (NeedsInputFiles &&
5206             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5207           return true;
5208         break;
5209 
5210       default:
5211         if (llvm::Error Err = Stream.SkipBlock()) {
5212           // FIXME this drops the error on the floor.
5213           consumeError(std::move(Err));
5214           return true;
5215         }
5216         break;
5217       }
5218 
5219       continue;
5220     }
5221 
5222     case llvm::BitstreamEntry::EndBlock:
5223       DoneWithControlBlock = true;
5224       break;
5225 
5226     case llvm::BitstreamEntry::Error:
5227       return true;
5228 
5229     case llvm::BitstreamEntry::Record:
5230       break;
5231     }
5232 
5233     if (DoneWithControlBlock) break;
5234 
5235     Record.clear();
5236     StringRef Blob;
5237     Expected<unsigned> MaybeRecCode =
5238         Stream.readRecord(Entry.ID, Record, &Blob);
5239     if (!MaybeRecCode) {
5240       // FIXME this drops the error.
5241       return Failure;
5242     }
5243     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5244     case METADATA:
5245       if (Record[0] != VERSION_MAJOR)
5246         return true;
5247       if (Listener.ReadFullVersionInformation(Blob))
5248         return true;
5249       break;
5250     case MODULE_NAME:
5251       Listener.ReadModuleName(Blob);
5252       break;
5253     case MODULE_DIRECTORY:
5254       ModuleDir = std::string(Blob);
5255       break;
5256     case MODULE_MAP_FILE: {
5257       unsigned Idx = 0;
5258       auto Path = ReadString(Record, Idx);
5259       ResolveImportedPath(Path, ModuleDir);
5260       Listener.ReadModuleMapFile(Path);
5261       break;
5262     }
5263     case INPUT_FILE_OFFSETS: {
5264       if (!NeedsInputFiles)
5265         break;
5266 
5267       unsigned NumInputFiles = Record[0];
5268       unsigned NumUserFiles = Record[1];
5269       const llvm::support::unaligned_uint64_t *InputFileOffs =
5270           (const llvm::support::unaligned_uint64_t *)Blob.data();
5271       for (unsigned I = 0; I != NumInputFiles; ++I) {
5272         // Go find this input file.
5273         bool isSystemFile = I >= NumUserFiles;
5274 
5275         if (isSystemFile && !NeedsSystemInputFiles)
5276           break; // the rest are system input files
5277 
5278         BitstreamCursor &Cursor = InputFilesCursor;
5279         SavedStreamPosition SavedPosition(Cursor);
5280         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5281           // FIXME this drops errors on the floor.
5282           consumeError(std::move(Err));
5283         }
5284 
5285         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5286         if (!MaybeCode) {
5287           // FIXME this drops errors on the floor.
5288           consumeError(MaybeCode.takeError());
5289         }
5290         unsigned Code = MaybeCode.get();
5291 
5292         RecordData Record;
5293         StringRef Blob;
5294         bool shouldContinue = false;
5295         Expected<unsigned> MaybeRecordType =
5296             Cursor.readRecord(Code, Record, &Blob);
5297         if (!MaybeRecordType) {
5298           // FIXME this drops errors on the floor.
5299           consumeError(MaybeRecordType.takeError());
5300         }
5301         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5302         case INPUT_FILE_HASH:
5303           break;
5304         case INPUT_FILE:
5305           bool Overridden = static_cast<bool>(Record[3]);
5306           std::string Filename = std::string(Blob);
5307           ResolveImportedPath(Filename, ModuleDir);
5308           shouldContinue = Listener.visitInputFile(
5309               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5310           break;
5311         }
5312         if (!shouldContinue)
5313           break;
5314       }
5315       break;
5316     }
5317 
5318     case IMPORTS: {
5319       if (!NeedsImports)
5320         break;
5321 
5322       unsigned Idx = 0, N = Record.size();
5323       while (Idx < N) {
5324         // Read information about the AST file.
5325         Idx += 1+1+1+1+5; // Kind, ImportLoc, Size, ModTime, Signature
5326         std::string ModuleName = ReadString(Record, Idx);
5327         std::string Filename = ReadString(Record, Idx);
5328         ResolveImportedPath(Filename, ModuleDir);
5329         Listener.visitImport(ModuleName, Filename);
5330       }
5331       break;
5332     }
5333 
5334     default:
5335       // No other validation to perform.
5336       break;
5337     }
5338   }
5339 
5340   // Look for module file extension blocks, if requested.
5341   if (FindModuleFileExtensions) {
5342     BitstreamCursor SavedStream = Stream;
5343     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5344       bool DoneWithExtensionBlock = false;
5345       while (!DoneWithExtensionBlock) {
5346         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5347         if (!MaybeEntry) {
5348           // FIXME this drops the error.
5349           return true;
5350         }
5351         llvm::BitstreamEntry Entry = MaybeEntry.get();
5352 
5353         switch (Entry.Kind) {
5354         case llvm::BitstreamEntry::SubBlock:
5355           if (llvm::Error Err = Stream.SkipBlock()) {
5356             // FIXME this drops the error on the floor.
5357             consumeError(std::move(Err));
5358             return true;
5359           }
5360           continue;
5361 
5362         case llvm::BitstreamEntry::EndBlock:
5363           DoneWithExtensionBlock = true;
5364           continue;
5365 
5366         case llvm::BitstreamEntry::Error:
5367           return true;
5368 
5369         case llvm::BitstreamEntry::Record:
5370           break;
5371         }
5372 
5373        Record.clear();
5374        StringRef Blob;
5375        Expected<unsigned> MaybeRecCode =
5376            Stream.readRecord(Entry.ID, Record, &Blob);
5377        if (!MaybeRecCode) {
5378          // FIXME this drops the error.
5379          return true;
5380        }
5381        switch (MaybeRecCode.get()) {
5382        case EXTENSION_METADATA: {
5383          ModuleFileExtensionMetadata Metadata;
5384          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5385            return true;
5386 
5387          Listener.readModuleFileExtension(Metadata);
5388          break;
5389        }
5390        }
5391       }
5392     }
5393     Stream = SavedStream;
5394   }
5395 
5396   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5397   if (readUnhashedControlBlockImpl(
5398           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5399           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5400           ValidateDiagnosticOptions) != Success)
5401     return true;
5402 
5403   return false;
5404 }
5405 
5406 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5407                                     const PCHContainerReader &PCHContainerRdr,
5408                                     const LangOptions &LangOpts,
5409                                     const TargetOptions &TargetOpts,
5410                                     const PreprocessorOptions &PPOpts,
5411                                     StringRef ExistingModuleCachePath) {
5412   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5413                                ExistingModuleCachePath, FileMgr);
5414   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5415                                   /*FindModuleFileExtensions=*/false,
5416                                   validator,
5417                                   /*ValidateDiagnosticOptions=*/true);
5418 }
5419 
5420 ASTReader::ASTReadResult
5421 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5422   // Enter the submodule block.
5423   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5424     Error(std::move(Err));
5425     return Failure;
5426   }
5427 
5428   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5429   bool First = true;
5430   Module *CurrentModule = nullptr;
5431   RecordData Record;
5432   while (true) {
5433     Expected<llvm::BitstreamEntry> MaybeEntry =
5434         F.Stream.advanceSkippingSubblocks();
5435     if (!MaybeEntry) {
5436       Error(MaybeEntry.takeError());
5437       return Failure;
5438     }
5439     llvm::BitstreamEntry Entry = MaybeEntry.get();
5440 
5441     switch (Entry.Kind) {
5442     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5443     case llvm::BitstreamEntry::Error:
5444       Error("malformed block record in AST file");
5445       return Failure;
5446     case llvm::BitstreamEntry::EndBlock:
5447       return Success;
5448     case llvm::BitstreamEntry::Record:
5449       // The interesting case.
5450       break;
5451     }
5452 
5453     // Read a record.
5454     StringRef Blob;
5455     Record.clear();
5456     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5457     if (!MaybeKind) {
5458       Error(MaybeKind.takeError());
5459       return Failure;
5460     }
5461     unsigned Kind = MaybeKind.get();
5462 
5463     if ((Kind == SUBMODULE_METADATA) != First) {
5464       Error("submodule metadata record should be at beginning of block");
5465       return Failure;
5466     }
5467     First = false;
5468 
5469     // Submodule information is only valid if we have a current module.
5470     // FIXME: Should we error on these cases?
5471     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5472         Kind != SUBMODULE_DEFINITION)
5473       continue;
5474 
5475     switch (Kind) {
5476     default:  // Default behavior: ignore.
5477       break;
5478 
5479     case SUBMODULE_DEFINITION: {
5480       if (Record.size() < 12) {
5481         Error("malformed module definition");
5482         return Failure;
5483       }
5484 
5485       StringRef Name = Blob;
5486       unsigned Idx = 0;
5487       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5488       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5489       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5490       bool IsFramework = Record[Idx++];
5491       bool IsExplicit = Record[Idx++];
5492       bool IsSystem = Record[Idx++];
5493       bool IsExternC = Record[Idx++];
5494       bool InferSubmodules = Record[Idx++];
5495       bool InferExplicitSubmodules = Record[Idx++];
5496       bool InferExportWildcard = Record[Idx++];
5497       bool ConfigMacrosExhaustive = Record[Idx++];
5498       bool ModuleMapIsPrivate = Record[Idx++];
5499 
5500       Module *ParentModule = nullptr;
5501       if (Parent)
5502         ParentModule = getSubmodule(Parent);
5503 
5504       // Retrieve this (sub)module from the module map, creating it if
5505       // necessary.
5506       CurrentModule =
5507           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5508               .first;
5509 
5510       // FIXME: set the definition loc for CurrentModule, or call
5511       // ModMap.setInferredModuleAllowedBy()
5512 
5513       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5514       if (GlobalIndex >= SubmodulesLoaded.size() ||
5515           SubmodulesLoaded[GlobalIndex]) {
5516         Error("too many submodules");
5517         return Failure;
5518       }
5519 
5520       if (!ParentModule) {
5521         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5522           // Don't emit module relocation error if we have -fno-validate-pch
5523           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5524               CurFile != F.File) {
5525             Error(diag::err_module_file_conflict,
5526                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5527                   F.File->getName());
5528             return Failure;
5529           }
5530         }
5531 
5532         F.DidReadTopLevelSubmodule = true;
5533         CurrentModule->setASTFile(F.File);
5534         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5535       }
5536 
5537       CurrentModule->Kind = Kind;
5538       CurrentModule->Signature = F.Signature;
5539       CurrentModule->IsFromModuleFile = true;
5540       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5541       CurrentModule->IsExternC = IsExternC;
5542       CurrentModule->InferSubmodules = InferSubmodules;
5543       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5544       CurrentModule->InferExportWildcard = InferExportWildcard;
5545       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5546       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5547       if (DeserializationListener)
5548         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5549 
5550       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5551 
5552       // Clear out data that will be replaced by what is in the module file.
5553       CurrentModule->LinkLibraries.clear();
5554       CurrentModule->ConfigMacros.clear();
5555       CurrentModule->UnresolvedConflicts.clear();
5556       CurrentModule->Conflicts.clear();
5557 
5558       // The module is available unless it's missing a requirement; relevant
5559       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5560       // Missing headers that were present when the module was built do not
5561       // make it unavailable -- if we got this far, this must be an explicitly
5562       // imported module file.
5563       CurrentModule->Requirements.clear();
5564       CurrentModule->MissingHeaders.clear();
5565       CurrentModule->IsUnimportable =
5566           ParentModule && ParentModule->IsUnimportable;
5567       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5568       break;
5569     }
5570 
5571     case SUBMODULE_UMBRELLA_HEADER: {
5572       std::string Filename = std::string(Blob);
5573       ResolveImportedPath(F, Filename);
5574       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5575         if (!CurrentModule->getUmbrellaHeader())
5576           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5577         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5578           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5579             Error("mismatched umbrella headers in submodule");
5580           return OutOfDate;
5581         }
5582       }
5583       break;
5584     }
5585 
5586     case SUBMODULE_HEADER:
5587     case SUBMODULE_EXCLUDED_HEADER:
5588     case SUBMODULE_PRIVATE_HEADER:
5589       // We lazily associate headers with their modules via the HeaderInfo table.
5590       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5591       // of complete filenames or remove it entirely.
5592       break;
5593 
5594     case SUBMODULE_TEXTUAL_HEADER:
5595     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5596       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5597       // them here.
5598       break;
5599 
5600     case SUBMODULE_TOPHEADER:
5601       CurrentModule->addTopHeaderFilename(Blob);
5602       break;
5603 
5604     case SUBMODULE_UMBRELLA_DIR: {
5605       std::string Dirname = std::string(Blob);
5606       ResolveImportedPath(F, Dirname);
5607       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5608         if (!CurrentModule->getUmbrellaDir())
5609           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5610         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5611           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5612             Error("mismatched umbrella directories in submodule");
5613           return OutOfDate;
5614         }
5615       }
5616       break;
5617     }
5618 
5619     case SUBMODULE_METADATA: {
5620       F.BaseSubmoduleID = getTotalNumSubmodules();
5621       F.LocalNumSubmodules = Record[0];
5622       unsigned LocalBaseSubmoduleID = Record[1];
5623       if (F.LocalNumSubmodules > 0) {
5624         // Introduce the global -> local mapping for submodules within this
5625         // module.
5626         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5627 
5628         // Introduce the local -> global mapping for submodules within this
5629         // module.
5630         F.SubmoduleRemap.insertOrReplace(
5631           std::make_pair(LocalBaseSubmoduleID,
5632                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5633 
5634         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5635       }
5636       break;
5637     }
5638 
5639     case SUBMODULE_IMPORTS:
5640       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5641         UnresolvedModuleRef Unresolved;
5642         Unresolved.File = &F;
5643         Unresolved.Mod = CurrentModule;
5644         Unresolved.ID = Record[Idx];
5645         Unresolved.Kind = UnresolvedModuleRef::Import;
5646         Unresolved.IsWildcard = false;
5647         UnresolvedModuleRefs.push_back(Unresolved);
5648       }
5649       break;
5650 
5651     case SUBMODULE_EXPORTS:
5652       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5653         UnresolvedModuleRef Unresolved;
5654         Unresolved.File = &F;
5655         Unresolved.Mod = CurrentModule;
5656         Unresolved.ID = Record[Idx];
5657         Unresolved.Kind = UnresolvedModuleRef::Export;
5658         Unresolved.IsWildcard = Record[Idx + 1];
5659         UnresolvedModuleRefs.push_back(Unresolved);
5660       }
5661 
5662       // Once we've loaded the set of exports, there's no reason to keep
5663       // the parsed, unresolved exports around.
5664       CurrentModule->UnresolvedExports.clear();
5665       break;
5666 
5667     case SUBMODULE_REQUIRES:
5668       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5669                                     PP.getTargetInfo());
5670       break;
5671 
5672     case SUBMODULE_LINK_LIBRARY:
5673       ModMap.resolveLinkAsDependencies(CurrentModule);
5674       CurrentModule->LinkLibraries.push_back(
5675           Module::LinkLibrary(std::string(Blob), Record[0]));
5676       break;
5677 
5678     case SUBMODULE_CONFIG_MACRO:
5679       CurrentModule->ConfigMacros.push_back(Blob.str());
5680       break;
5681 
5682     case SUBMODULE_CONFLICT: {
5683       UnresolvedModuleRef Unresolved;
5684       Unresolved.File = &F;
5685       Unresolved.Mod = CurrentModule;
5686       Unresolved.ID = Record[0];
5687       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5688       Unresolved.IsWildcard = false;
5689       Unresolved.String = Blob;
5690       UnresolvedModuleRefs.push_back(Unresolved);
5691       break;
5692     }
5693 
5694     case SUBMODULE_INITIALIZERS: {
5695       if (!ContextObj)
5696         break;
5697       SmallVector<uint32_t, 16> Inits;
5698       for (auto &ID : Record)
5699         Inits.push_back(getGlobalDeclID(F, ID));
5700       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5701       break;
5702     }
5703 
5704     case SUBMODULE_EXPORT_AS:
5705       CurrentModule->ExportAsModule = Blob.str();
5706       ModMap.addLinkAsDependency(CurrentModule);
5707       break;
5708     }
5709   }
5710 }
5711 
5712 /// Parse the record that corresponds to a LangOptions data
5713 /// structure.
5714 ///
5715 /// This routine parses the language options from the AST file and then gives
5716 /// them to the AST listener if one is set.
5717 ///
5718 /// \returns true if the listener deems the file unacceptable, false otherwise.
5719 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5720                                      bool Complain,
5721                                      ASTReaderListener &Listener,
5722                                      bool AllowCompatibleDifferences) {
5723   LangOptions LangOpts;
5724   unsigned Idx = 0;
5725 #define LANGOPT(Name, Bits, Default, Description) \
5726   LangOpts.Name = Record[Idx++];
5727 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5728   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5729 #include "clang/Basic/LangOptions.def"
5730 #define SANITIZER(NAME, ID)                                                    \
5731   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5732 #include "clang/Basic/Sanitizers.def"
5733 
5734   for (unsigned N = Record[Idx++]; N; --N)
5735     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5736 
5737   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5738   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5739   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5740 
5741   LangOpts.CurrentModule = ReadString(Record, Idx);
5742 
5743   // Comment options.
5744   for (unsigned N = Record[Idx++]; N; --N) {
5745     LangOpts.CommentOpts.BlockCommandNames.push_back(
5746       ReadString(Record, Idx));
5747   }
5748   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5749 
5750   // OpenMP offloading options.
5751   for (unsigned N = Record[Idx++]; N; --N) {
5752     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5753   }
5754 
5755   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5756 
5757   return Listener.ReadLanguageOptions(LangOpts, Complain,
5758                                       AllowCompatibleDifferences);
5759 }
5760 
5761 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5762                                    ASTReaderListener &Listener,
5763                                    bool AllowCompatibleDifferences) {
5764   unsigned Idx = 0;
5765   TargetOptions TargetOpts;
5766   TargetOpts.Triple = ReadString(Record, Idx);
5767   TargetOpts.CPU = ReadString(Record, Idx);
5768   TargetOpts.ABI = ReadString(Record, Idx);
5769   for (unsigned N = Record[Idx++]; N; --N) {
5770     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5771   }
5772   for (unsigned N = Record[Idx++]; N; --N) {
5773     TargetOpts.Features.push_back(ReadString(Record, Idx));
5774   }
5775 
5776   return Listener.ReadTargetOptions(TargetOpts, Complain,
5777                                     AllowCompatibleDifferences);
5778 }
5779 
5780 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5781                                        ASTReaderListener &Listener) {
5782   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5783   unsigned Idx = 0;
5784 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5785 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5786   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5787 #include "clang/Basic/DiagnosticOptions.def"
5788 
5789   for (unsigned N = Record[Idx++]; N; --N)
5790     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5791   for (unsigned N = Record[Idx++]; N; --N)
5792     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5793 
5794   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5795 }
5796 
5797 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5798                                        ASTReaderListener &Listener) {
5799   FileSystemOptions FSOpts;
5800   unsigned Idx = 0;
5801   FSOpts.WorkingDir = ReadString(Record, Idx);
5802   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5803 }
5804 
5805 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5806                                          bool Complain,
5807                                          ASTReaderListener &Listener) {
5808   HeaderSearchOptions HSOpts;
5809   unsigned Idx = 0;
5810   HSOpts.Sysroot = ReadString(Record, Idx);
5811 
5812   // Include entries.
5813   for (unsigned N = Record[Idx++]; N; --N) {
5814     std::string Path = ReadString(Record, Idx);
5815     frontend::IncludeDirGroup Group
5816       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5817     bool IsFramework = Record[Idx++];
5818     bool IgnoreSysRoot = Record[Idx++];
5819     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5820                                     IgnoreSysRoot);
5821   }
5822 
5823   // System header prefixes.
5824   for (unsigned N = Record[Idx++]; N; --N) {
5825     std::string Prefix = ReadString(Record, Idx);
5826     bool IsSystemHeader = Record[Idx++];
5827     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5828   }
5829 
5830   HSOpts.ResourceDir = ReadString(Record, Idx);
5831   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5832   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5833   HSOpts.DisableModuleHash = Record[Idx++];
5834   HSOpts.ImplicitModuleMaps = Record[Idx++];
5835   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5836   HSOpts.UseBuiltinIncludes = Record[Idx++];
5837   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5838   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5839   HSOpts.UseLibcxx = Record[Idx++];
5840   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5841 
5842   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5843                                           Complain);
5844 }
5845 
5846 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5847                                          bool Complain,
5848                                          ASTReaderListener &Listener,
5849                                          std::string &SuggestedPredefines) {
5850   PreprocessorOptions PPOpts;
5851   unsigned Idx = 0;
5852 
5853   // Macro definitions/undefs
5854   for (unsigned N = Record[Idx++]; N; --N) {
5855     std::string Macro = ReadString(Record, Idx);
5856     bool IsUndef = Record[Idx++];
5857     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5858   }
5859 
5860   // Includes
5861   for (unsigned N = Record[Idx++]; N; --N) {
5862     PPOpts.Includes.push_back(ReadString(Record, Idx));
5863   }
5864 
5865   // Macro Includes
5866   for (unsigned N = Record[Idx++]; N; --N) {
5867     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5868   }
5869 
5870   PPOpts.UsePredefines = Record[Idx++];
5871   PPOpts.DetailedRecord = Record[Idx++];
5872   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5873   PPOpts.ObjCXXARCStandardLibrary =
5874     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5875   SuggestedPredefines.clear();
5876   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5877                                           SuggestedPredefines);
5878 }
5879 
5880 std::pair<ModuleFile *, unsigned>
5881 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5882   GlobalPreprocessedEntityMapType::iterator
5883   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5884   assert(I != GlobalPreprocessedEntityMap.end() &&
5885          "Corrupted global preprocessed entity map");
5886   ModuleFile *M = I->second;
5887   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5888   return std::make_pair(M, LocalIndex);
5889 }
5890 
5891 llvm::iterator_range<PreprocessingRecord::iterator>
5892 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5893   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5894     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5895                                              Mod.NumPreprocessedEntities);
5896 
5897   return llvm::make_range(PreprocessingRecord::iterator(),
5898                           PreprocessingRecord::iterator());
5899 }
5900 
5901 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5902 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5903   return llvm::make_range(
5904       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5905       ModuleDeclIterator(this, &Mod,
5906                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5907 }
5908 
5909 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5910   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5911   assert(I != GlobalSkippedRangeMap.end() &&
5912     "Corrupted global skipped range map");
5913   ModuleFile *M = I->second;
5914   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5915   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5916   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5917   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5918                     TranslateSourceLocation(*M, RawRange.getEnd()));
5919   assert(Range.isValid());
5920   return Range;
5921 }
5922 
5923 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5924   PreprocessedEntityID PPID = Index+1;
5925   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5926   ModuleFile &M = *PPInfo.first;
5927   unsigned LocalIndex = PPInfo.second;
5928   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5929 
5930   if (!PP.getPreprocessingRecord()) {
5931     Error("no preprocessing record");
5932     return nullptr;
5933   }
5934 
5935   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5936   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5937           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5938     Error(std::move(Err));
5939     return nullptr;
5940   }
5941 
5942   Expected<llvm::BitstreamEntry> MaybeEntry =
5943       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5944   if (!MaybeEntry) {
5945     Error(MaybeEntry.takeError());
5946     return nullptr;
5947   }
5948   llvm::BitstreamEntry Entry = MaybeEntry.get();
5949 
5950   if (Entry.Kind != llvm::BitstreamEntry::Record)
5951     return nullptr;
5952 
5953   // Read the record.
5954   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5955                     TranslateSourceLocation(M, PPOffs.getEnd()));
5956   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5957   StringRef Blob;
5958   RecordData Record;
5959   Expected<unsigned> MaybeRecType =
5960       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5961   if (!MaybeRecType) {
5962     Error(MaybeRecType.takeError());
5963     return nullptr;
5964   }
5965   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5966   case PPD_MACRO_EXPANSION: {
5967     bool isBuiltin = Record[0];
5968     IdentifierInfo *Name = nullptr;
5969     MacroDefinitionRecord *Def = nullptr;
5970     if (isBuiltin)
5971       Name = getLocalIdentifier(M, Record[1]);
5972     else {
5973       PreprocessedEntityID GlobalID =
5974           getGlobalPreprocessedEntityID(M, Record[1]);
5975       Def = cast<MacroDefinitionRecord>(
5976           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5977     }
5978 
5979     MacroExpansion *ME;
5980     if (isBuiltin)
5981       ME = new (PPRec) MacroExpansion(Name, Range);
5982     else
5983       ME = new (PPRec) MacroExpansion(Def, Range);
5984 
5985     return ME;
5986   }
5987 
5988   case PPD_MACRO_DEFINITION: {
5989     // Decode the identifier info and then check again; if the macro is
5990     // still defined and associated with the identifier,
5991     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
5992     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
5993 
5994     if (DeserializationListener)
5995       DeserializationListener->MacroDefinitionRead(PPID, MD);
5996 
5997     return MD;
5998   }
5999 
6000   case PPD_INCLUSION_DIRECTIVE: {
6001     const char *FullFileNameStart = Blob.data() + Record[0];
6002     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6003     const FileEntry *File = nullptr;
6004     if (!FullFileName.empty())
6005       if (auto FE = PP.getFileManager().getFile(FullFileName))
6006         File = *FE;
6007 
6008     // FIXME: Stable encoding
6009     InclusionDirective::InclusionKind Kind
6010       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6011     InclusionDirective *ID
6012       = new (PPRec) InclusionDirective(PPRec, Kind,
6013                                        StringRef(Blob.data(), Record[0]),
6014                                        Record[1], Record[3],
6015                                        File,
6016                                        Range);
6017     return ID;
6018   }
6019   }
6020 
6021   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6022 }
6023 
6024 /// Find the next module that contains entities and return the ID
6025 /// of the first entry.
6026 ///
6027 /// \param SLocMapI points at a chunk of a module that contains no
6028 /// preprocessed entities or the entities it contains are not the ones we are
6029 /// looking for.
6030 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6031                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6032   ++SLocMapI;
6033   for (GlobalSLocOffsetMapType::const_iterator
6034          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6035     ModuleFile &M = *SLocMapI->second;
6036     if (M.NumPreprocessedEntities)
6037       return M.BasePreprocessedEntityID;
6038   }
6039 
6040   return getTotalNumPreprocessedEntities();
6041 }
6042 
6043 namespace {
6044 
6045 struct PPEntityComp {
6046   const ASTReader &Reader;
6047   ModuleFile &M;
6048 
6049   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6050 
6051   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6052     SourceLocation LHS = getLoc(L);
6053     SourceLocation RHS = getLoc(R);
6054     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6055   }
6056 
6057   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6058     SourceLocation LHS = getLoc(L);
6059     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6060   }
6061 
6062   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6063     SourceLocation RHS = getLoc(R);
6064     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6065   }
6066 
6067   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6068     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6069   }
6070 };
6071 
6072 } // namespace
6073 
6074 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6075                                                        bool EndsAfter) const {
6076   if (SourceMgr.isLocalSourceLocation(Loc))
6077     return getTotalNumPreprocessedEntities();
6078 
6079   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6080       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6081   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6082          "Corrupted global sloc offset map");
6083 
6084   if (SLocMapI->second->NumPreprocessedEntities == 0)
6085     return findNextPreprocessedEntity(SLocMapI);
6086 
6087   ModuleFile &M = *SLocMapI->second;
6088 
6089   using pp_iterator = const PPEntityOffset *;
6090 
6091   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6092   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6093 
6094   size_t Count = M.NumPreprocessedEntities;
6095   size_t Half;
6096   pp_iterator First = pp_begin;
6097   pp_iterator PPI;
6098 
6099   if (EndsAfter) {
6100     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6101                            PPEntityComp(*this, M));
6102   } else {
6103     // Do a binary search manually instead of using std::lower_bound because
6104     // The end locations of entities may be unordered (when a macro expansion
6105     // is inside another macro argument), but for this case it is not important
6106     // whether we get the first macro expansion or its containing macro.
6107     while (Count > 0) {
6108       Half = Count / 2;
6109       PPI = First;
6110       std::advance(PPI, Half);
6111       if (SourceMgr.isBeforeInTranslationUnit(
6112               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6113         First = PPI;
6114         ++First;
6115         Count = Count - Half - 1;
6116       } else
6117         Count = Half;
6118     }
6119   }
6120 
6121   if (PPI == pp_end)
6122     return findNextPreprocessedEntity(SLocMapI);
6123 
6124   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6125 }
6126 
6127 /// Returns a pair of [Begin, End) indices of preallocated
6128 /// preprocessed entities that \arg Range encompasses.
6129 std::pair<unsigned, unsigned>
6130     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6131   if (Range.isInvalid())
6132     return std::make_pair(0,0);
6133   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6134 
6135   PreprocessedEntityID BeginID =
6136       findPreprocessedEntity(Range.getBegin(), false);
6137   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6138   return std::make_pair(BeginID, EndID);
6139 }
6140 
6141 /// Optionally returns true or false if the preallocated preprocessed
6142 /// entity with index \arg Index came from file \arg FID.
6143 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6144                                                              FileID FID) {
6145   if (FID.isInvalid())
6146     return false;
6147 
6148   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6149   ModuleFile &M = *PPInfo.first;
6150   unsigned LocalIndex = PPInfo.second;
6151   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6152 
6153   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6154   if (Loc.isInvalid())
6155     return false;
6156 
6157   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6158     return true;
6159   else
6160     return false;
6161 }
6162 
6163 namespace {
6164 
6165   /// Visitor used to search for information about a header file.
6166   class HeaderFileInfoVisitor {
6167     const FileEntry *FE;
6168     Optional<HeaderFileInfo> HFI;
6169 
6170   public:
6171     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6172 
6173     bool operator()(ModuleFile &M) {
6174       HeaderFileInfoLookupTable *Table
6175         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6176       if (!Table)
6177         return false;
6178 
6179       // Look in the on-disk hash table for an entry for this file name.
6180       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6181       if (Pos == Table->end())
6182         return false;
6183 
6184       HFI = *Pos;
6185       return true;
6186     }
6187 
6188     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6189   };
6190 
6191 } // namespace
6192 
6193 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6194   HeaderFileInfoVisitor Visitor(FE);
6195   ModuleMgr.visit(Visitor);
6196   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6197     return *HFI;
6198 
6199   return HeaderFileInfo();
6200 }
6201 
6202 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6203   using DiagState = DiagnosticsEngine::DiagState;
6204   SmallVector<DiagState *, 32> DiagStates;
6205 
6206   for (ModuleFile &F : ModuleMgr) {
6207     unsigned Idx = 0;
6208     auto &Record = F.PragmaDiagMappings;
6209     if (Record.empty())
6210       continue;
6211 
6212     DiagStates.clear();
6213 
6214     auto ReadDiagState =
6215         [&](const DiagState &BasedOn, SourceLocation Loc,
6216             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6217       unsigned BackrefID = Record[Idx++];
6218       if (BackrefID != 0)
6219         return DiagStates[BackrefID - 1];
6220 
6221       // A new DiagState was created here.
6222       Diag.DiagStates.push_back(BasedOn);
6223       DiagState *NewState = &Diag.DiagStates.back();
6224       DiagStates.push_back(NewState);
6225       unsigned Size = Record[Idx++];
6226       assert(Idx + Size * 2 <= Record.size() &&
6227              "Invalid data, not enough diag/map pairs");
6228       while (Size--) {
6229         unsigned DiagID = Record[Idx++];
6230         DiagnosticMapping NewMapping =
6231             DiagnosticMapping::deserialize(Record[Idx++]);
6232         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6233           continue;
6234 
6235         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6236 
6237         // If this mapping was specified as a warning but the severity was
6238         // upgraded due to diagnostic settings, simulate the current diagnostic
6239         // settings (and use a warning).
6240         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6241           NewMapping.setSeverity(diag::Severity::Warning);
6242           NewMapping.setUpgradedFromWarning(false);
6243         }
6244 
6245         Mapping = NewMapping;
6246       }
6247       return NewState;
6248     };
6249 
6250     // Read the first state.
6251     DiagState *FirstState;
6252     if (F.Kind == MK_ImplicitModule) {
6253       // Implicitly-built modules are reused with different diagnostic
6254       // settings.  Use the initial diagnostic state from Diag to simulate this
6255       // compilation's diagnostic settings.
6256       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6257       DiagStates.push_back(FirstState);
6258 
6259       // Skip the initial diagnostic state from the serialized module.
6260       assert(Record[1] == 0 &&
6261              "Invalid data, unexpected backref in initial state");
6262       Idx = 3 + Record[2] * 2;
6263       assert(Idx < Record.size() &&
6264              "Invalid data, not enough state change pairs in initial state");
6265     } else if (F.isModule()) {
6266       // For an explicit module, preserve the flags from the module build
6267       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6268       // -Wblah flags.
6269       unsigned Flags = Record[Idx++];
6270       DiagState Initial;
6271       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6272       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6273       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6274       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6275       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6276       Initial.ExtBehavior = (diag::Severity)Flags;
6277       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6278 
6279       assert(F.OriginalSourceFileID.isValid());
6280 
6281       // Set up the root buffer of the module to start with the initial
6282       // diagnostic state of the module itself, to cover files that contain no
6283       // explicit transitions (for which we did not serialize anything).
6284       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6285           .StateTransitions.push_back({FirstState, 0});
6286     } else {
6287       // For prefix ASTs, start with whatever the user configured on the
6288       // command line.
6289       Idx++; // Skip flags.
6290       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6291                                  SourceLocation(), false);
6292     }
6293 
6294     // Read the state transitions.
6295     unsigned NumLocations = Record[Idx++];
6296     while (NumLocations--) {
6297       assert(Idx < Record.size() &&
6298              "Invalid data, missing pragma diagnostic states");
6299       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6300       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6301       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6302       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6303       unsigned Transitions = Record[Idx++];
6304 
6305       // Note that we don't need to set up Parent/ParentOffset here, because
6306       // we won't be changing the diagnostic state within imported FileIDs
6307       // (other than perhaps appending to the main source file, which has no
6308       // parent).
6309       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6310       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6311       for (unsigned I = 0; I != Transitions; ++I) {
6312         unsigned Offset = Record[Idx++];
6313         auto *State =
6314             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6315         F.StateTransitions.push_back({State, Offset});
6316       }
6317     }
6318 
6319     // Read the final state.
6320     assert(Idx < Record.size() &&
6321            "Invalid data, missing final pragma diagnostic state");
6322     SourceLocation CurStateLoc =
6323         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6324     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6325 
6326     if (!F.isModule()) {
6327       Diag.DiagStatesByLoc.CurDiagState = CurState;
6328       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6329 
6330       // Preserve the property that the imaginary root file describes the
6331       // current state.
6332       FileID NullFile;
6333       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6334       if (T.empty())
6335         T.push_back({CurState, 0});
6336       else
6337         T[0].State = CurState;
6338     }
6339 
6340     // Don't try to read these mappings again.
6341     Record.clear();
6342   }
6343 }
6344 
6345 /// Get the correct cursor and offset for loading a type.
6346 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6347   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6348   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6349   ModuleFile *M = I->second;
6350   return RecordLocation(
6351       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset());
6352 }
6353 
6354 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6355   switch (code) {
6356 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6357   case TYPE_##CODE_ID: return Type::CLASS_ID;
6358 #include "clang/Serialization/TypeBitCodes.def"
6359   default: return llvm::None;
6360   }
6361 }
6362 
6363 /// Read and return the type with the given index..
6364 ///
6365 /// The index is the type ID, shifted and minus the number of predefs. This
6366 /// routine actually reads the record corresponding to the type at the given
6367 /// location. It is a helper routine for GetType, which deals with reading type
6368 /// IDs.
6369 QualType ASTReader::readTypeRecord(unsigned Index) {
6370   assert(ContextObj && "reading type with no AST context");
6371   ASTContext &Context = *ContextObj;
6372   RecordLocation Loc = TypeCursorForIndex(Index);
6373   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6374 
6375   // Keep track of where we are in the stream, then jump back there
6376   // after reading this type.
6377   SavedStreamPosition SavedPosition(DeclsCursor);
6378 
6379   ReadingKindTracker ReadingKind(Read_Type, *this);
6380 
6381   // Note that we are loading a type record.
6382   Deserializing AType(this);
6383 
6384   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6385     Error(std::move(Err));
6386     return QualType();
6387   }
6388   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6389   if (!RawCode) {
6390     Error(RawCode.takeError());
6391     return QualType();
6392   }
6393 
6394   ASTRecordReader Record(*this, *Loc.F);
6395   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6396   if (!Code) {
6397     Error(Code.takeError());
6398     return QualType();
6399   }
6400   if (Code.get() == TYPE_EXT_QUAL) {
6401     QualType baseType = Record.readQualType();
6402     Qualifiers quals = Record.readQualifiers();
6403     return Context.getQualifiedType(baseType, quals);
6404   }
6405 
6406   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6407   if (!maybeClass) {
6408     Error("Unexpected code for type");
6409     return QualType();
6410   }
6411 
6412   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6413   return TypeReader.read(*maybeClass);
6414 }
6415 
6416 namespace clang {
6417 
6418 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6419   ASTRecordReader &Reader;
6420 
6421   SourceLocation readSourceLocation() {
6422     return Reader.readSourceLocation();
6423   }
6424 
6425   TypeSourceInfo *GetTypeSourceInfo() {
6426     return Reader.readTypeSourceInfo();
6427   }
6428 
6429   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6430     return Reader.readNestedNameSpecifierLoc();
6431   }
6432 
6433   Attr *ReadAttr() {
6434     return Reader.readAttr();
6435   }
6436 
6437 public:
6438   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6439 
6440   // We want compile-time assurance that we've enumerated all of
6441   // these, so unfortunately we have to declare them first, then
6442   // define them out-of-line.
6443 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6444 #define TYPELOC(CLASS, PARENT) \
6445   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6446 #include "clang/AST/TypeLocNodes.def"
6447 
6448   void VisitFunctionTypeLoc(FunctionTypeLoc);
6449   void VisitArrayTypeLoc(ArrayTypeLoc);
6450 };
6451 
6452 } // namespace clang
6453 
6454 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6455   // nothing to do
6456 }
6457 
6458 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6459   TL.setBuiltinLoc(readSourceLocation());
6460   if (TL.needsExtraLocalData()) {
6461     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6462     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6463     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6464     TL.setModeAttr(Reader.readInt());
6465   }
6466 }
6467 
6468 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6469   TL.setNameLoc(readSourceLocation());
6470 }
6471 
6472 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6473   TL.setStarLoc(readSourceLocation());
6474 }
6475 
6476 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6477   // nothing to do
6478 }
6479 
6480 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6481   // nothing to do
6482 }
6483 
6484 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6485   TL.setExpansionLoc(readSourceLocation());
6486 }
6487 
6488 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6489   TL.setCaretLoc(readSourceLocation());
6490 }
6491 
6492 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6493   TL.setAmpLoc(readSourceLocation());
6494 }
6495 
6496 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6497   TL.setAmpAmpLoc(readSourceLocation());
6498 }
6499 
6500 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6501   TL.setStarLoc(readSourceLocation());
6502   TL.setClassTInfo(GetTypeSourceInfo());
6503 }
6504 
6505 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6506   TL.setLBracketLoc(readSourceLocation());
6507   TL.setRBracketLoc(readSourceLocation());
6508   if (Reader.readBool())
6509     TL.setSizeExpr(Reader.readExpr());
6510   else
6511     TL.setSizeExpr(nullptr);
6512 }
6513 
6514 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6515   VisitArrayTypeLoc(TL);
6516 }
6517 
6518 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6519   VisitArrayTypeLoc(TL);
6520 }
6521 
6522 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6523   VisitArrayTypeLoc(TL);
6524 }
6525 
6526 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6527                                             DependentSizedArrayTypeLoc TL) {
6528   VisitArrayTypeLoc(TL);
6529 }
6530 
6531 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6532     DependentAddressSpaceTypeLoc TL) {
6533 
6534     TL.setAttrNameLoc(readSourceLocation());
6535     TL.setAttrOperandParensRange(Reader.readSourceRange());
6536     TL.setAttrExprOperand(Reader.readExpr());
6537 }
6538 
6539 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6540                                         DependentSizedExtVectorTypeLoc TL) {
6541   TL.setNameLoc(readSourceLocation());
6542 }
6543 
6544 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6545   TL.setNameLoc(readSourceLocation());
6546 }
6547 
6548 void TypeLocReader::VisitDependentVectorTypeLoc(
6549     DependentVectorTypeLoc TL) {
6550   TL.setNameLoc(readSourceLocation());
6551 }
6552 
6553 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6554   TL.setNameLoc(readSourceLocation());
6555 }
6556 
6557 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6558   TL.setAttrNameLoc(readSourceLocation());
6559   TL.setAttrOperandParensRange(Reader.readSourceRange());
6560   TL.setAttrRowOperand(Reader.readExpr());
6561   TL.setAttrColumnOperand(Reader.readExpr());
6562 }
6563 
6564 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6565     DependentSizedMatrixTypeLoc TL) {
6566   TL.setAttrNameLoc(readSourceLocation());
6567   TL.setAttrOperandParensRange(Reader.readSourceRange());
6568   TL.setAttrRowOperand(Reader.readExpr());
6569   TL.setAttrColumnOperand(Reader.readExpr());
6570 }
6571 
6572 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6573   TL.setLocalRangeBegin(readSourceLocation());
6574   TL.setLParenLoc(readSourceLocation());
6575   TL.setRParenLoc(readSourceLocation());
6576   TL.setExceptionSpecRange(Reader.readSourceRange());
6577   TL.setLocalRangeEnd(readSourceLocation());
6578   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6579     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6580   }
6581 }
6582 
6583 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6584   VisitFunctionTypeLoc(TL);
6585 }
6586 
6587 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6588   VisitFunctionTypeLoc(TL);
6589 }
6590 
6591 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6592   TL.setNameLoc(readSourceLocation());
6593 }
6594 
6595 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6596   TL.setNameLoc(readSourceLocation());
6597 }
6598 
6599 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6600   TL.setTypeofLoc(readSourceLocation());
6601   TL.setLParenLoc(readSourceLocation());
6602   TL.setRParenLoc(readSourceLocation());
6603 }
6604 
6605 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6606   TL.setTypeofLoc(readSourceLocation());
6607   TL.setLParenLoc(readSourceLocation());
6608   TL.setRParenLoc(readSourceLocation());
6609   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6610 }
6611 
6612 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6613   TL.setNameLoc(readSourceLocation());
6614 }
6615 
6616 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6617   TL.setKWLoc(readSourceLocation());
6618   TL.setLParenLoc(readSourceLocation());
6619   TL.setRParenLoc(readSourceLocation());
6620   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6621 }
6622 
6623 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6624   TL.setNameLoc(readSourceLocation());
6625   if (Reader.readBool()) {
6626     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6627     TL.setTemplateKWLoc(readSourceLocation());
6628     TL.setConceptNameLoc(readSourceLocation());
6629     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6630     TL.setLAngleLoc(readSourceLocation());
6631     TL.setRAngleLoc(readSourceLocation());
6632     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6633       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6634                               TL.getTypePtr()->getArg(i).getKind()));
6635   }
6636 }
6637 
6638 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6639     DeducedTemplateSpecializationTypeLoc TL) {
6640   TL.setTemplateNameLoc(readSourceLocation());
6641 }
6642 
6643 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6644   TL.setNameLoc(readSourceLocation());
6645 }
6646 
6647 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6648   TL.setNameLoc(readSourceLocation());
6649 }
6650 
6651 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6652   TL.setAttr(ReadAttr());
6653 }
6654 
6655 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6656   TL.setNameLoc(readSourceLocation());
6657 }
6658 
6659 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6660                                             SubstTemplateTypeParmTypeLoc TL) {
6661   TL.setNameLoc(readSourceLocation());
6662 }
6663 
6664 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6665                                           SubstTemplateTypeParmPackTypeLoc TL) {
6666   TL.setNameLoc(readSourceLocation());
6667 }
6668 
6669 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6670                                            TemplateSpecializationTypeLoc TL) {
6671   TL.setTemplateKeywordLoc(readSourceLocation());
6672   TL.setTemplateNameLoc(readSourceLocation());
6673   TL.setLAngleLoc(readSourceLocation());
6674   TL.setRAngleLoc(readSourceLocation());
6675   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6676     TL.setArgLocInfo(
6677         i,
6678         Reader.readTemplateArgumentLocInfo(
6679           TL.getTypePtr()->getArg(i).getKind()));
6680 }
6681 
6682 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6683   TL.setLParenLoc(readSourceLocation());
6684   TL.setRParenLoc(readSourceLocation());
6685 }
6686 
6687 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6688   TL.setElaboratedKeywordLoc(readSourceLocation());
6689   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6690 }
6691 
6692 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6693   TL.setNameLoc(readSourceLocation());
6694 }
6695 
6696 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6697   TL.setElaboratedKeywordLoc(readSourceLocation());
6698   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6699   TL.setNameLoc(readSourceLocation());
6700 }
6701 
6702 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6703        DependentTemplateSpecializationTypeLoc TL) {
6704   TL.setElaboratedKeywordLoc(readSourceLocation());
6705   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6706   TL.setTemplateKeywordLoc(readSourceLocation());
6707   TL.setTemplateNameLoc(readSourceLocation());
6708   TL.setLAngleLoc(readSourceLocation());
6709   TL.setRAngleLoc(readSourceLocation());
6710   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6711     TL.setArgLocInfo(
6712         I,
6713         Reader.readTemplateArgumentLocInfo(
6714             TL.getTypePtr()->getArg(I).getKind()));
6715 }
6716 
6717 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6718   TL.setEllipsisLoc(readSourceLocation());
6719 }
6720 
6721 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6722   TL.setNameLoc(readSourceLocation());
6723 }
6724 
6725 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6726   if (TL.getNumProtocols()) {
6727     TL.setProtocolLAngleLoc(readSourceLocation());
6728     TL.setProtocolRAngleLoc(readSourceLocation());
6729   }
6730   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6731     TL.setProtocolLoc(i, readSourceLocation());
6732 }
6733 
6734 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6735   TL.setHasBaseTypeAsWritten(Reader.readBool());
6736   TL.setTypeArgsLAngleLoc(readSourceLocation());
6737   TL.setTypeArgsRAngleLoc(readSourceLocation());
6738   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6739     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6740   TL.setProtocolLAngleLoc(readSourceLocation());
6741   TL.setProtocolRAngleLoc(readSourceLocation());
6742   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6743     TL.setProtocolLoc(i, readSourceLocation());
6744 }
6745 
6746 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6747   TL.setStarLoc(readSourceLocation());
6748 }
6749 
6750 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6751   TL.setKWLoc(readSourceLocation());
6752   TL.setLParenLoc(readSourceLocation());
6753   TL.setRParenLoc(readSourceLocation());
6754 }
6755 
6756 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6757   TL.setKWLoc(readSourceLocation());
6758 }
6759 
6760 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6761   TL.setNameLoc(readSourceLocation());
6762 }
6763 void TypeLocReader::VisitDependentExtIntTypeLoc(
6764     clang::DependentExtIntTypeLoc TL) {
6765   TL.setNameLoc(readSourceLocation());
6766 }
6767 
6768 
6769 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6770   TypeLocReader TLR(*this);
6771   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6772     TLR.Visit(TL);
6773 }
6774 
6775 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6776   QualType InfoTy = readType();
6777   if (InfoTy.isNull())
6778     return nullptr;
6779 
6780   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6781   readTypeLoc(TInfo->getTypeLoc());
6782   return TInfo;
6783 }
6784 
6785 QualType ASTReader::GetType(TypeID ID) {
6786   assert(ContextObj && "reading type with no AST context");
6787   ASTContext &Context = *ContextObj;
6788 
6789   unsigned FastQuals = ID & Qualifiers::FastMask;
6790   unsigned Index = ID >> Qualifiers::FastWidth;
6791 
6792   if (Index < NUM_PREDEF_TYPE_IDS) {
6793     QualType T;
6794     switch ((PredefinedTypeIDs)Index) {
6795     case PREDEF_TYPE_NULL_ID:
6796       return QualType();
6797     case PREDEF_TYPE_VOID_ID:
6798       T = Context.VoidTy;
6799       break;
6800     case PREDEF_TYPE_BOOL_ID:
6801       T = Context.BoolTy;
6802       break;
6803     case PREDEF_TYPE_CHAR_U_ID:
6804     case PREDEF_TYPE_CHAR_S_ID:
6805       // FIXME: Check that the signedness of CharTy is correct!
6806       T = Context.CharTy;
6807       break;
6808     case PREDEF_TYPE_UCHAR_ID:
6809       T = Context.UnsignedCharTy;
6810       break;
6811     case PREDEF_TYPE_USHORT_ID:
6812       T = Context.UnsignedShortTy;
6813       break;
6814     case PREDEF_TYPE_UINT_ID:
6815       T = Context.UnsignedIntTy;
6816       break;
6817     case PREDEF_TYPE_ULONG_ID:
6818       T = Context.UnsignedLongTy;
6819       break;
6820     case PREDEF_TYPE_ULONGLONG_ID:
6821       T = Context.UnsignedLongLongTy;
6822       break;
6823     case PREDEF_TYPE_UINT128_ID:
6824       T = Context.UnsignedInt128Ty;
6825       break;
6826     case PREDEF_TYPE_SCHAR_ID:
6827       T = Context.SignedCharTy;
6828       break;
6829     case PREDEF_TYPE_WCHAR_ID:
6830       T = Context.WCharTy;
6831       break;
6832     case PREDEF_TYPE_SHORT_ID:
6833       T = Context.ShortTy;
6834       break;
6835     case PREDEF_TYPE_INT_ID:
6836       T = Context.IntTy;
6837       break;
6838     case PREDEF_TYPE_LONG_ID:
6839       T = Context.LongTy;
6840       break;
6841     case PREDEF_TYPE_LONGLONG_ID:
6842       T = Context.LongLongTy;
6843       break;
6844     case PREDEF_TYPE_INT128_ID:
6845       T = Context.Int128Ty;
6846       break;
6847     case PREDEF_TYPE_HALF_ID:
6848       T = Context.HalfTy;
6849       break;
6850     case PREDEF_TYPE_FLOAT_ID:
6851       T = Context.FloatTy;
6852       break;
6853     case PREDEF_TYPE_DOUBLE_ID:
6854       T = Context.DoubleTy;
6855       break;
6856     case PREDEF_TYPE_LONGDOUBLE_ID:
6857       T = Context.LongDoubleTy;
6858       break;
6859     case PREDEF_TYPE_SHORT_ACCUM_ID:
6860       T = Context.ShortAccumTy;
6861       break;
6862     case PREDEF_TYPE_ACCUM_ID:
6863       T = Context.AccumTy;
6864       break;
6865     case PREDEF_TYPE_LONG_ACCUM_ID:
6866       T = Context.LongAccumTy;
6867       break;
6868     case PREDEF_TYPE_USHORT_ACCUM_ID:
6869       T = Context.UnsignedShortAccumTy;
6870       break;
6871     case PREDEF_TYPE_UACCUM_ID:
6872       T = Context.UnsignedAccumTy;
6873       break;
6874     case PREDEF_TYPE_ULONG_ACCUM_ID:
6875       T = Context.UnsignedLongAccumTy;
6876       break;
6877     case PREDEF_TYPE_SHORT_FRACT_ID:
6878       T = Context.ShortFractTy;
6879       break;
6880     case PREDEF_TYPE_FRACT_ID:
6881       T = Context.FractTy;
6882       break;
6883     case PREDEF_TYPE_LONG_FRACT_ID:
6884       T = Context.LongFractTy;
6885       break;
6886     case PREDEF_TYPE_USHORT_FRACT_ID:
6887       T = Context.UnsignedShortFractTy;
6888       break;
6889     case PREDEF_TYPE_UFRACT_ID:
6890       T = Context.UnsignedFractTy;
6891       break;
6892     case PREDEF_TYPE_ULONG_FRACT_ID:
6893       T = Context.UnsignedLongFractTy;
6894       break;
6895     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6896       T = Context.SatShortAccumTy;
6897       break;
6898     case PREDEF_TYPE_SAT_ACCUM_ID:
6899       T = Context.SatAccumTy;
6900       break;
6901     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6902       T = Context.SatLongAccumTy;
6903       break;
6904     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6905       T = Context.SatUnsignedShortAccumTy;
6906       break;
6907     case PREDEF_TYPE_SAT_UACCUM_ID:
6908       T = Context.SatUnsignedAccumTy;
6909       break;
6910     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6911       T = Context.SatUnsignedLongAccumTy;
6912       break;
6913     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6914       T = Context.SatShortFractTy;
6915       break;
6916     case PREDEF_TYPE_SAT_FRACT_ID:
6917       T = Context.SatFractTy;
6918       break;
6919     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6920       T = Context.SatLongFractTy;
6921       break;
6922     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6923       T = Context.SatUnsignedShortFractTy;
6924       break;
6925     case PREDEF_TYPE_SAT_UFRACT_ID:
6926       T = Context.SatUnsignedFractTy;
6927       break;
6928     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6929       T = Context.SatUnsignedLongFractTy;
6930       break;
6931     case PREDEF_TYPE_FLOAT16_ID:
6932       T = Context.Float16Ty;
6933       break;
6934     case PREDEF_TYPE_FLOAT128_ID:
6935       T = Context.Float128Ty;
6936       break;
6937     case PREDEF_TYPE_OVERLOAD_ID:
6938       T = Context.OverloadTy;
6939       break;
6940     case PREDEF_TYPE_BOUND_MEMBER:
6941       T = Context.BoundMemberTy;
6942       break;
6943     case PREDEF_TYPE_PSEUDO_OBJECT:
6944       T = Context.PseudoObjectTy;
6945       break;
6946     case PREDEF_TYPE_DEPENDENT_ID:
6947       T = Context.DependentTy;
6948       break;
6949     case PREDEF_TYPE_UNKNOWN_ANY:
6950       T = Context.UnknownAnyTy;
6951       break;
6952     case PREDEF_TYPE_NULLPTR_ID:
6953       T = Context.NullPtrTy;
6954       break;
6955     case PREDEF_TYPE_CHAR8_ID:
6956       T = Context.Char8Ty;
6957       break;
6958     case PREDEF_TYPE_CHAR16_ID:
6959       T = Context.Char16Ty;
6960       break;
6961     case PREDEF_TYPE_CHAR32_ID:
6962       T = Context.Char32Ty;
6963       break;
6964     case PREDEF_TYPE_OBJC_ID:
6965       T = Context.ObjCBuiltinIdTy;
6966       break;
6967     case PREDEF_TYPE_OBJC_CLASS:
6968       T = Context.ObjCBuiltinClassTy;
6969       break;
6970     case PREDEF_TYPE_OBJC_SEL:
6971       T = Context.ObjCBuiltinSelTy;
6972       break;
6973 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6974     case PREDEF_TYPE_##Id##_ID: \
6975       T = Context.SingletonId; \
6976       break;
6977 #include "clang/Basic/OpenCLImageTypes.def"
6978 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
6979     case PREDEF_TYPE_##Id##_ID: \
6980       T = Context.Id##Ty; \
6981       break;
6982 #include "clang/Basic/OpenCLExtensionTypes.def"
6983     case PREDEF_TYPE_SAMPLER_ID:
6984       T = Context.OCLSamplerTy;
6985       break;
6986     case PREDEF_TYPE_EVENT_ID:
6987       T = Context.OCLEventTy;
6988       break;
6989     case PREDEF_TYPE_CLK_EVENT_ID:
6990       T = Context.OCLClkEventTy;
6991       break;
6992     case PREDEF_TYPE_QUEUE_ID:
6993       T = Context.OCLQueueTy;
6994       break;
6995     case PREDEF_TYPE_RESERVE_ID_ID:
6996       T = Context.OCLReserveIDTy;
6997       break;
6998     case PREDEF_TYPE_AUTO_DEDUCT:
6999       T = Context.getAutoDeductType();
7000       break;
7001     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7002       T = Context.getAutoRRefDeductType();
7003       break;
7004     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7005       T = Context.ARCUnbridgedCastTy;
7006       break;
7007     case PREDEF_TYPE_BUILTIN_FN:
7008       T = Context.BuiltinFnTy;
7009       break;
7010     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7011       T = Context.IncompleteMatrixIdxTy;
7012       break;
7013     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7014       T = Context.OMPArraySectionTy;
7015       break;
7016     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7017       T = Context.OMPArraySectionTy;
7018       break;
7019     case PREDEF_TYPE_OMP_ITERATOR:
7020       T = Context.OMPIteratorTy;
7021       break;
7022 #define SVE_TYPE(Name, Id, SingletonId) \
7023     case PREDEF_TYPE_##Id##_ID: \
7024       T = Context.SingletonId; \
7025       break;
7026 #include "clang/Basic/AArch64SVEACLETypes.def"
7027     }
7028 
7029     assert(!T.isNull() && "Unknown predefined type");
7030     return T.withFastQualifiers(FastQuals);
7031   }
7032 
7033   Index -= NUM_PREDEF_TYPE_IDS;
7034   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7035   if (TypesLoaded[Index].isNull()) {
7036     TypesLoaded[Index] = readTypeRecord(Index);
7037     if (TypesLoaded[Index].isNull())
7038       return QualType();
7039 
7040     TypesLoaded[Index]->setFromAST();
7041     if (DeserializationListener)
7042       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7043                                         TypesLoaded[Index]);
7044   }
7045 
7046   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7047 }
7048 
7049 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7050   return GetType(getGlobalTypeID(F, LocalID));
7051 }
7052 
7053 serialization::TypeID
7054 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7055   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7056   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7057 
7058   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7059     return LocalID;
7060 
7061   if (!F.ModuleOffsetMap.empty())
7062     ReadModuleOffsetMap(F);
7063 
7064   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7065     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7066   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7067 
7068   unsigned GlobalIndex = LocalIndex + I->second;
7069   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7070 }
7071 
7072 TemplateArgumentLocInfo
7073 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7074   switch (Kind) {
7075   case TemplateArgument::Expression:
7076     return readExpr();
7077   case TemplateArgument::Type:
7078     return readTypeSourceInfo();
7079   case TemplateArgument::Template: {
7080     NestedNameSpecifierLoc QualifierLoc =
7081       readNestedNameSpecifierLoc();
7082     SourceLocation TemplateNameLoc = readSourceLocation();
7083     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7084                                    SourceLocation());
7085   }
7086   case TemplateArgument::TemplateExpansion: {
7087     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7088     SourceLocation TemplateNameLoc = readSourceLocation();
7089     SourceLocation EllipsisLoc = readSourceLocation();
7090     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7091                                    EllipsisLoc);
7092   }
7093   case TemplateArgument::Null:
7094   case TemplateArgument::Integral:
7095   case TemplateArgument::Declaration:
7096   case TemplateArgument::NullPtr:
7097   case TemplateArgument::Pack:
7098     // FIXME: Is this right?
7099     return TemplateArgumentLocInfo();
7100   }
7101   llvm_unreachable("unexpected template argument loc");
7102 }
7103 
7104 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7105   TemplateArgument Arg = readTemplateArgument();
7106 
7107   if (Arg.getKind() == TemplateArgument::Expression) {
7108     if (readBool()) // bool InfoHasSameExpr.
7109       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7110   }
7111   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7112 }
7113 
7114 const ASTTemplateArgumentListInfo *
7115 ASTRecordReader::readASTTemplateArgumentListInfo() {
7116   SourceLocation LAngleLoc = readSourceLocation();
7117   SourceLocation RAngleLoc = readSourceLocation();
7118   unsigned NumArgsAsWritten = readInt();
7119   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7120   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7121     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7122   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7123 }
7124 
7125 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7126   return GetDecl(ID);
7127 }
7128 
7129 void ASTReader::CompleteRedeclChain(const Decl *D) {
7130   if (NumCurrentElementsDeserializing) {
7131     // We arrange to not care about the complete redeclaration chain while we're
7132     // deserializing. Just remember that the AST has marked this one as complete
7133     // but that it's not actually complete yet, so we know we still need to
7134     // complete it later.
7135     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7136     return;
7137   }
7138 
7139   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7140 
7141   // If this is a named declaration, complete it by looking it up
7142   // within its context.
7143   //
7144   // FIXME: Merging a function definition should merge
7145   // all mergeable entities within it.
7146   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7147       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7148     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7149       if (!getContext().getLangOpts().CPlusPlus &&
7150           isa<TranslationUnitDecl>(DC)) {
7151         // Outside of C++, we don't have a lookup table for the TU, so update
7152         // the identifier instead. (For C++ modules, we don't store decls
7153         // in the serialized identifier table, so we do the lookup in the TU.)
7154         auto *II = Name.getAsIdentifierInfo();
7155         assert(II && "non-identifier name in C?");
7156         if (II->isOutOfDate())
7157           updateOutOfDateIdentifier(*II);
7158       } else
7159         DC->lookup(Name);
7160     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7161       // Find all declarations of this kind from the relevant context.
7162       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7163         auto *DC = cast<DeclContext>(DCDecl);
7164         SmallVector<Decl*, 8> Decls;
7165         FindExternalLexicalDecls(
7166             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7167       }
7168     }
7169   }
7170 
7171   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7172     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7173   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7174     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7175   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7176     if (auto *Template = FD->getPrimaryTemplate())
7177       Template->LoadLazySpecializations();
7178   }
7179 }
7180 
7181 CXXCtorInitializer **
7182 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7183   RecordLocation Loc = getLocalBitOffset(Offset);
7184   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7185   SavedStreamPosition SavedPosition(Cursor);
7186   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7187     Error(std::move(Err));
7188     return nullptr;
7189   }
7190   ReadingKindTracker ReadingKind(Read_Decl, *this);
7191 
7192   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7193   if (!MaybeCode) {
7194     Error(MaybeCode.takeError());
7195     return nullptr;
7196   }
7197   unsigned Code = MaybeCode.get();
7198 
7199   ASTRecordReader Record(*this, *Loc.F);
7200   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7201   if (!MaybeRecCode) {
7202     Error(MaybeRecCode.takeError());
7203     return nullptr;
7204   }
7205   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7206     Error("malformed AST file: missing C++ ctor initializers");
7207     return nullptr;
7208   }
7209 
7210   return Record.readCXXCtorInitializers();
7211 }
7212 
7213 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7214   assert(ContextObj && "reading base specifiers with no AST context");
7215   ASTContext &Context = *ContextObj;
7216 
7217   RecordLocation Loc = getLocalBitOffset(Offset);
7218   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7219   SavedStreamPosition SavedPosition(Cursor);
7220   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7221     Error(std::move(Err));
7222     return nullptr;
7223   }
7224   ReadingKindTracker ReadingKind(Read_Decl, *this);
7225 
7226   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7227   if (!MaybeCode) {
7228     Error(MaybeCode.takeError());
7229     return nullptr;
7230   }
7231   unsigned Code = MaybeCode.get();
7232 
7233   ASTRecordReader Record(*this, *Loc.F);
7234   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7235   if (!MaybeRecCode) {
7236     Error(MaybeCode.takeError());
7237     return nullptr;
7238   }
7239   unsigned RecCode = MaybeRecCode.get();
7240 
7241   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7242     Error("malformed AST file: missing C++ base specifiers");
7243     return nullptr;
7244   }
7245 
7246   unsigned NumBases = Record.readInt();
7247   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7248   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7249   for (unsigned I = 0; I != NumBases; ++I)
7250     Bases[I] = Record.readCXXBaseSpecifier();
7251   return Bases;
7252 }
7253 
7254 serialization::DeclID
7255 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7256   if (LocalID < NUM_PREDEF_DECL_IDS)
7257     return LocalID;
7258 
7259   if (!F.ModuleOffsetMap.empty())
7260     ReadModuleOffsetMap(F);
7261 
7262   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7263     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7264   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7265 
7266   return LocalID + I->second;
7267 }
7268 
7269 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7270                                    ModuleFile &M) const {
7271   // Predefined decls aren't from any module.
7272   if (ID < NUM_PREDEF_DECL_IDS)
7273     return false;
7274 
7275   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7276          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7277 }
7278 
7279 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7280   if (!D->isFromASTFile())
7281     return nullptr;
7282   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7283   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7284   return I->second;
7285 }
7286 
7287 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7288   if (ID < NUM_PREDEF_DECL_IDS)
7289     return SourceLocation();
7290 
7291   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7292 
7293   if (Index > DeclsLoaded.size()) {
7294     Error("declaration ID out-of-range for AST file");
7295     return SourceLocation();
7296   }
7297 
7298   if (Decl *D = DeclsLoaded[Index])
7299     return D->getLocation();
7300 
7301   SourceLocation Loc;
7302   DeclCursorForID(ID, Loc);
7303   return Loc;
7304 }
7305 
7306 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7307   switch (ID) {
7308   case PREDEF_DECL_NULL_ID:
7309     return nullptr;
7310 
7311   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7312     return Context.getTranslationUnitDecl();
7313 
7314   case PREDEF_DECL_OBJC_ID_ID:
7315     return Context.getObjCIdDecl();
7316 
7317   case PREDEF_DECL_OBJC_SEL_ID:
7318     return Context.getObjCSelDecl();
7319 
7320   case PREDEF_DECL_OBJC_CLASS_ID:
7321     return Context.getObjCClassDecl();
7322 
7323   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7324     return Context.getObjCProtocolDecl();
7325 
7326   case PREDEF_DECL_INT_128_ID:
7327     return Context.getInt128Decl();
7328 
7329   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7330     return Context.getUInt128Decl();
7331 
7332   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7333     return Context.getObjCInstanceTypeDecl();
7334 
7335   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7336     return Context.getBuiltinVaListDecl();
7337 
7338   case PREDEF_DECL_VA_LIST_TAG:
7339     return Context.getVaListTagDecl();
7340 
7341   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7342     return Context.getBuiltinMSVaListDecl();
7343 
7344   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7345     return Context.getMSGuidTagDecl();
7346 
7347   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7348     return Context.getExternCContextDecl();
7349 
7350   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7351     return Context.getMakeIntegerSeqDecl();
7352 
7353   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7354     return Context.getCFConstantStringDecl();
7355 
7356   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7357     return Context.getCFConstantStringTagDecl();
7358 
7359   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7360     return Context.getTypePackElementDecl();
7361   }
7362   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7363 }
7364 
7365 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7366   assert(ContextObj && "reading decl with no AST context");
7367   if (ID < NUM_PREDEF_DECL_IDS) {
7368     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7369     if (D) {
7370       // Track that we have merged the declaration with ID \p ID into the
7371       // pre-existing predefined declaration \p D.
7372       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7373       if (Merged.empty())
7374         Merged.push_back(ID);
7375     }
7376     return D;
7377   }
7378 
7379   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7380 
7381   if (Index >= DeclsLoaded.size()) {
7382     assert(0 && "declaration ID out-of-range for AST file");
7383     Error("declaration ID out-of-range for AST file");
7384     return nullptr;
7385   }
7386 
7387   return DeclsLoaded[Index];
7388 }
7389 
7390 Decl *ASTReader::GetDecl(DeclID ID) {
7391   if (ID < NUM_PREDEF_DECL_IDS)
7392     return GetExistingDecl(ID);
7393 
7394   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7395 
7396   if (Index >= DeclsLoaded.size()) {
7397     assert(0 && "declaration ID out-of-range for AST file");
7398     Error("declaration ID out-of-range for AST file");
7399     return nullptr;
7400   }
7401 
7402   if (!DeclsLoaded[Index]) {
7403     ReadDeclRecord(ID);
7404     if (DeserializationListener)
7405       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7406   }
7407 
7408   return DeclsLoaded[Index];
7409 }
7410 
7411 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7412                                                   DeclID GlobalID) {
7413   if (GlobalID < NUM_PREDEF_DECL_IDS)
7414     return GlobalID;
7415 
7416   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7417   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7418   ModuleFile *Owner = I->second;
7419 
7420   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7421     = M.GlobalToLocalDeclIDs.find(Owner);
7422   if (Pos == M.GlobalToLocalDeclIDs.end())
7423     return 0;
7424 
7425   return GlobalID - Owner->BaseDeclID + Pos->second;
7426 }
7427 
7428 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7429                                             const RecordData &Record,
7430                                             unsigned &Idx) {
7431   if (Idx >= Record.size()) {
7432     Error("Corrupted AST file");
7433     return 0;
7434   }
7435 
7436   return getGlobalDeclID(F, Record[Idx++]);
7437 }
7438 
7439 /// Resolve the offset of a statement into a statement.
7440 ///
7441 /// This operation will read a new statement from the external
7442 /// source each time it is called, and is meant to be used via a
7443 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7444 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7445   // Switch case IDs are per Decl.
7446   ClearSwitchCaseIDs();
7447 
7448   // Offset here is a global offset across the entire chain.
7449   RecordLocation Loc = getLocalBitOffset(Offset);
7450   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7451     Error(std::move(Err));
7452     return nullptr;
7453   }
7454   assert(NumCurrentElementsDeserializing == 0 &&
7455          "should not be called while already deserializing");
7456   Deserializing D(this);
7457   return ReadStmtFromStream(*Loc.F);
7458 }
7459 
7460 void ASTReader::FindExternalLexicalDecls(
7461     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7462     SmallVectorImpl<Decl *> &Decls) {
7463   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7464 
7465   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7466     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7467     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7468       auto K = (Decl::Kind)+LexicalDecls[I];
7469       if (!IsKindWeWant(K))
7470         continue;
7471 
7472       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7473 
7474       // Don't add predefined declarations to the lexical context more
7475       // than once.
7476       if (ID < NUM_PREDEF_DECL_IDS) {
7477         if (PredefsVisited[ID])
7478           continue;
7479 
7480         PredefsVisited[ID] = true;
7481       }
7482 
7483       if (Decl *D = GetLocalDecl(*M, ID)) {
7484         assert(D->getKind() == K && "wrong kind for lexical decl");
7485         if (!DC->isDeclInLexicalTraversal(D))
7486           Decls.push_back(D);
7487       }
7488     }
7489   };
7490 
7491   if (isa<TranslationUnitDecl>(DC)) {
7492     for (auto Lexical : TULexicalDecls)
7493       Visit(Lexical.first, Lexical.second);
7494   } else {
7495     auto I = LexicalDecls.find(DC);
7496     if (I != LexicalDecls.end())
7497       Visit(I->second.first, I->second.second);
7498   }
7499 
7500   ++NumLexicalDeclContextsRead;
7501 }
7502 
7503 namespace {
7504 
7505 class DeclIDComp {
7506   ASTReader &Reader;
7507   ModuleFile &Mod;
7508 
7509 public:
7510   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7511 
7512   bool operator()(LocalDeclID L, LocalDeclID R) const {
7513     SourceLocation LHS = getLocation(L);
7514     SourceLocation RHS = getLocation(R);
7515     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7516   }
7517 
7518   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7519     SourceLocation RHS = getLocation(R);
7520     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7521   }
7522 
7523   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7524     SourceLocation LHS = getLocation(L);
7525     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7526   }
7527 
7528   SourceLocation getLocation(LocalDeclID ID) const {
7529     return Reader.getSourceManager().getFileLoc(
7530             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7531   }
7532 };
7533 
7534 } // namespace
7535 
7536 void ASTReader::FindFileRegionDecls(FileID File,
7537                                     unsigned Offset, unsigned Length,
7538                                     SmallVectorImpl<Decl *> &Decls) {
7539   SourceManager &SM = getSourceManager();
7540 
7541   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7542   if (I == FileDeclIDs.end())
7543     return;
7544 
7545   FileDeclsInfo &DInfo = I->second;
7546   if (DInfo.Decls.empty())
7547     return;
7548 
7549   SourceLocation
7550     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7551   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7552 
7553   DeclIDComp DIDComp(*this, *DInfo.Mod);
7554   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7555       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7556   if (BeginIt != DInfo.Decls.begin())
7557     --BeginIt;
7558 
7559   // If we are pointing at a top-level decl inside an objc container, we need
7560   // to backtrack until we find it otherwise we will fail to report that the
7561   // region overlaps with an objc container.
7562   while (BeginIt != DInfo.Decls.begin() &&
7563          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7564              ->isTopLevelDeclInObjCContainer())
7565     --BeginIt;
7566 
7567   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7568       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7569   if (EndIt != DInfo.Decls.end())
7570     ++EndIt;
7571 
7572   for (ArrayRef<serialization::LocalDeclID>::iterator
7573          DIt = BeginIt; DIt != EndIt; ++DIt)
7574     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7575 }
7576 
7577 bool
7578 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7579                                           DeclarationName Name) {
7580   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7581          "DeclContext has no visible decls in storage");
7582   if (!Name)
7583     return false;
7584 
7585   auto It = Lookups.find(DC);
7586   if (It == Lookups.end())
7587     return false;
7588 
7589   Deserializing LookupResults(this);
7590 
7591   // Load the list of declarations.
7592   SmallVector<NamedDecl *, 64> Decls;
7593   for (DeclID ID : It->second.Table.find(Name)) {
7594     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7595     if (ND->getDeclName() == Name)
7596       Decls.push_back(ND);
7597   }
7598 
7599   ++NumVisibleDeclContextsRead;
7600   SetExternalVisibleDeclsForName(DC, Name, Decls);
7601   return !Decls.empty();
7602 }
7603 
7604 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7605   if (!DC->hasExternalVisibleStorage())
7606     return;
7607 
7608   auto It = Lookups.find(DC);
7609   assert(It != Lookups.end() &&
7610          "have external visible storage but no lookup tables");
7611 
7612   DeclsMap Decls;
7613 
7614   for (DeclID ID : It->second.Table.findAll()) {
7615     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7616     Decls[ND->getDeclName()].push_back(ND);
7617   }
7618 
7619   ++NumVisibleDeclContextsRead;
7620 
7621   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7622     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7623   }
7624   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7625 }
7626 
7627 const serialization::reader::DeclContextLookupTable *
7628 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7629   auto I = Lookups.find(Primary);
7630   return I == Lookups.end() ? nullptr : &I->second;
7631 }
7632 
7633 /// Under non-PCH compilation the consumer receives the objc methods
7634 /// before receiving the implementation, and codegen depends on this.
7635 /// We simulate this by deserializing and passing to consumer the methods of the
7636 /// implementation before passing the deserialized implementation decl.
7637 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7638                                        ASTConsumer *Consumer) {
7639   assert(ImplD && Consumer);
7640 
7641   for (auto *I : ImplD->methods())
7642     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7643 
7644   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7645 }
7646 
7647 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7648   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7649     PassObjCImplDeclToConsumer(ImplD, Consumer);
7650   else
7651     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7652 }
7653 
7654 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7655   this->Consumer = Consumer;
7656 
7657   if (Consumer)
7658     PassInterestingDeclsToConsumer();
7659 
7660   if (DeserializationListener)
7661     DeserializationListener->ReaderInitialized(this);
7662 }
7663 
7664 void ASTReader::PrintStats() {
7665   std::fprintf(stderr, "*** AST File Statistics:\n");
7666 
7667   unsigned NumTypesLoaded
7668     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7669                                       QualType());
7670   unsigned NumDeclsLoaded
7671     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7672                                       (Decl *)nullptr);
7673   unsigned NumIdentifiersLoaded
7674     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7675                                             IdentifiersLoaded.end(),
7676                                             (IdentifierInfo *)nullptr);
7677   unsigned NumMacrosLoaded
7678     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7679                                        MacrosLoaded.end(),
7680                                        (MacroInfo *)nullptr);
7681   unsigned NumSelectorsLoaded
7682     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7683                                           SelectorsLoaded.end(),
7684                                           Selector());
7685 
7686   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7687     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7688                  NumSLocEntriesRead, TotalNumSLocEntries,
7689                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7690   if (!TypesLoaded.empty())
7691     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7692                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7693                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7694   if (!DeclsLoaded.empty())
7695     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7696                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7697                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7698   if (!IdentifiersLoaded.empty())
7699     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7700                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7701                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7702   if (!MacrosLoaded.empty())
7703     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7704                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7705                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7706   if (!SelectorsLoaded.empty())
7707     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7708                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7709                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7710   if (TotalNumStatements)
7711     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7712                  NumStatementsRead, TotalNumStatements,
7713                  ((float)NumStatementsRead/TotalNumStatements * 100));
7714   if (TotalNumMacros)
7715     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7716                  NumMacrosRead, TotalNumMacros,
7717                  ((float)NumMacrosRead/TotalNumMacros * 100));
7718   if (TotalLexicalDeclContexts)
7719     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7720                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7721                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7722                   * 100));
7723   if (TotalVisibleDeclContexts)
7724     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7725                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7726                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7727                   * 100));
7728   if (TotalNumMethodPoolEntries)
7729     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7730                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7731                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7732                   * 100));
7733   if (NumMethodPoolLookups)
7734     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7735                  NumMethodPoolHits, NumMethodPoolLookups,
7736                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7737   if (NumMethodPoolTableLookups)
7738     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7739                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7740                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7741                   * 100.0));
7742   if (NumIdentifierLookupHits)
7743     std::fprintf(stderr,
7744                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7745                  NumIdentifierLookupHits, NumIdentifierLookups,
7746                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7747 
7748   if (GlobalIndex) {
7749     std::fprintf(stderr, "\n");
7750     GlobalIndex->printStats();
7751   }
7752 
7753   std::fprintf(stderr, "\n");
7754   dump();
7755   std::fprintf(stderr, "\n");
7756 }
7757 
7758 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7759 LLVM_DUMP_METHOD static void
7760 dumpModuleIDMap(StringRef Name,
7761                 const ContinuousRangeMap<Key, ModuleFile *,
7762                                          InitialCapacity> &Map) {
7763   if (Map.begin() == Map.end())
7764     return;
7765 
7766   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7767 
7768   llvm::errs() << Name << ":\n";
7769   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7770        I != IEnd; ++I) {
7771     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7772       << "\n";
7773   }
7774 }
7775 
7776 LLVM_DUMP_METHOD void ASTReader::dump() {
7777   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7778   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7779   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7780   dumpModuleIDMap("Global type map", GlobalTypeMap);
7781   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7782   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7783   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7784   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7785   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7786   dumpModuleIDMap("Global preprocessed entity map",
7787                   GlobalPreprocessedEntityMap);
7788 
7789   llvm::errs() << "\n*** PCH/Modules Loaded:";
7790   for (ModuleFile &M : ModuleMgr)
7791     M.dump();
7792 }
7793 
7794 /// Return the amount of memory used by memory buffers, breaking down
7795 /// by heap-backed versus mmap'ed memory.
7796 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7797   for (ModuleFile &I : ModuleMgr) {
7798     if (llvm::MemoryBuffer *buf = I.Buffer) {
7799       size_t bytes = buf->getBufferSize();
7800       switch (buf->getBufferKind()) {
7801         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7802           sizes.malloc_bytes += bytes;
7803           break;
7804         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7805           sizes.mmap_bytes += bytes;
7806           break;
7807       }
7808     }
7809   }
7810 }
7811 
7812 void ASTReader::InitializeSema(Sema &S) {
7813   SemaObj = &S;
7814   S.addExternalSource(this);
7815 
7816   // Makes sure any declarations that were deserialized "too early"
7817   // still get added to the identifier's declaration chains.
7818   for (uint64_t ID : PreloadedDeclIDs) {
7819     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7820     pushExternalDeclIntoScope(D, D->getDeclName());
7821   }
7822   PreloadedDeclIDs.clear();
7823 
7824   // FIXME: What happens if these are changed by a module import?
7825   if (!FPPragmaOptions.empty()) {
7826     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7827     SemaObj->CurFPFeatures = FPOptions(FPPragmaOptions[0]);
7828   }
7829 
7830   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7831   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7832   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7833 
7834   UpdateSema();
7835 }
7836 
7837 void ASTReader::UpdateSema() {
7838   assert(SemaObj && "no Sema to update");
7839 
7840   // Load the offsets of the declarations that Sema references.
7841   // They will be lazily deserialized when needed.
7842   if (!SemaDeclRefs.empty()) {
7843     assert(SemaDeclRefs.size() % 3 == 0);
7844     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7845       if (!SemaObj->StdNamespace)
7846         SemaObj->StdNamespace = SemaDeclRefs[I];
7847       if (!SemaObj->StdBadAlloc)
7848         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7849       if (!SemaObj->StdAlignValT)
7850         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7851     }
7852     SemaDeclRefs.clear();
7853   }
7854 
7855   // Update the state of pragmas. Use the same API as if we had encountered the
7856   // pragma in the source.
7857   if(OptimizeOffPragmaLocation.isValid())
7858     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7859   if (PragmaMSStructState != -1)
7860     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7861   if (PointersToMembersPragmaLocation.isValid()) {
7862     SemaObj->ActOnPragmaMSPointersToMembers(
7863         (LangOptions::PragmaMSPointersToMembersKind)
7864             PragmaMSPointersToMembersState,
7865         PointersToMembersPragmaLocation);
7866   }
7867   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7868 
7869   if (PragmaPackCurrentValue) {
7870     // The bottom of the stack might have a default value. It must be adjusted
7871     // to the current value to ensure that the packing state is preserved after
7872     // popping entries that were included/imported from a PCH/module.
7873     bool DropFirst = false;
7874     if (!PragmaPackStack.empty() &&
7875         PragmaPackStack.front().Location.isInvalid()) {
7876       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7877              "Expected a default alignment value");
7878       SemaObj->PackStack.Stack.emplace_back(
7879           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7880           SemaObj->PackStack.CurrentPragmaLocation,
7881           PragmaPackStack.front().PushLocation);
7882       DropFirst = true;
7883     }
7884     for (const auto &Entry :
7885          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7886       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7887                                             Entry.Location, Entry.PushLocation);
7888     if (PragmaPackCurrentLocation.isInvalid()) {
7889       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7890              "Expected a default alignment value");
7891       // Keep the current values.
7892     } else {
7893       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7894       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7895     }
7896   }
7897   if (FpPragmaCurrentValue) {
7898     // The bottom of the stack might have a default value. It must be adjusted
7899     // to the current value to ensure that fp-pragma state is preserved after
7900     // popping entries that were included/imported from a PCH/module.
7901     bool DropFirst = false;
7902     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7903       assert(FpPragmaStack.front().Value ==
7904                  SemaObj->FpPragmaStack.DefaultValue &&
7905              "Expected a default pragma float_control value");
7906       SemaObj->FpPragmaStack.Stack.emplace_back(
7907           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7908           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7909           FpPragmaStack.front().PushLocation);
7910       DropFirst = true;
7911     }
7912     for (const auto &Entry :
7913          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7914       SemaObj->FpPragmaStack.Stack.emplace_back(
7915           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7916     if (FpPragmaCurrentLocation.isInvalid()) {
7917       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7918              "Expected a default pragma float_control value");
7919       // Keep the current values.
7920     } else {
7921       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7922       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7923     }
7924   }
7925 }
7926 
7927 IdentifierInfo *ASTReader::get(StringRef Name) {
7928   // Note that we are loading an identifier.
7929   Deserializing AnIdentifier(this);
7930 
7931   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7932                                   NumIdentifierLookups,
7933                                   NumIdentifierLookupHits);
7934 
7935   // We don't need to do identifier table lookups in C++ modules (we preload
7936   // all interesting declarations, and don't need to use the scope for name
7937   // lookups). Perform the lookup in PCH files, though, since we don't build
7938   // a complete initial identifier table if we're carrying on from a PCH.
7939   if (PP.getLangOpts().CPlusPlus) {
7940     for (auto F : ModuleMgr.pch_modules())
7941       if (Visitor(*F))
7942         break;
7943   } else {
7944     // If there is a global index, look there first to determine which modules
7945     // provably do not have any results for this identifier.
7946     GlobalModuleIndex::HitSet Hits;
7947     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7948     if (!loadGlobalIndex()) {
7949       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7950         HitsPtr = &Hits;
7951       }
7952     }
7953 
7954     ModuleMgr.visit(Visitor, HitsPtr);
7955   }
7956 
7957   IdentifierInfo *II = Visitor.getIdentifierInfo();
7958   markIdentifierUpToDate(II);
7959   return II;
7960 }
7961 
7962 namespace clang {
7963 
7964   /// An identifier-lookup iterator that enumerates all of the
7965   /// identifiers stored within a set of AST files.
7966   class ASTIdentifierIterator : public IdentifierIterator {
7967     /// The AST reader whose identifiers are being enumerated.
7968     const ASTReader &Reader;
7969 
7970     /// The current index into the chain of AST files stored in
7971     /// the AST reader.
7972     unsigned Index;
7973 
7974     /// The current position within the identifier lookup table
7975     /// of the current AST file.
7976     ASTIdentifierLookupTable::key_iterator Current;
7977 
7978     /// The end position within the identifier lookup table of
7979     /// the current AST file.
7980     ASTIdentifierLookupTable::key_iterator End;
7981 
7982     /// Whether to skip any modules in the ASTReader.
7983     bool SkipModules;
7984 
7985   public:
7986     explicit ASTIdentifierIterator(const ASTReader &Reader,
7987                                    bool SkipModules = false);
7988 
7989     StringRef Next() override;
7990   };
7991 
7992 } // namespace clang
7993 
7994 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
7995                                              bool SkipModules)
7996     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
7997 }
7998 
7999 StringRef ASTIdentifierIterator::Next() {
8000   while (Current == End) {
8001     // If we have exhausted all of our AST files, we're done.
8002     if (Index == 0)
8003       return StringRef();
8004 
8005     --Index;
8006     ModuleFile &F = Reader.ModuleMgr[Index];
8007     if (SkipModules && F.isModule())
8008       continue;
8009 
8010     ASTIdentifierLookupTable *IdTable =
8011         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8012     Current = IdTable->key_begin();
8013     End = IdTable->key_end();
8014   }
8015 
8016   // We have any identifiers remaining in the current AST file; return
8017   // the next one.
8018   StringRef Result = *Current;
8019   ++Current;
8020   return Result;
8021 }
8022 
8023 namespace {
8024 
8025 /// A utility for appending two IdentifierIterators.
8026 class ChainedIdentifierIterator : public IdentifierIterator {
8027   std::unique_ptr<IdentifierIterator> Current;
8028   std::unique_ptr<IdentifierIterator> Queued;
8029 
8030 public:
8031   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8032                             std::unique_ptr<IdentifierIterator> Second)
8033       : Current(std::move(First)), Queued(std::move(Second)) {}
8034 
8035   StringRef Next() override {
8036     if (!Current)
8037       return StringRef();
8038 
8039     StringRef result = Current->Next();
8040     if (!result.empty())
8041       return result;
8042 
8043     // Try the queued iterator, which may itself be empty.
8044     Current.reset();
8045     std::swap(Current, Queued);
8046     return Next();
8047   }
8048 };
8049 
8050 } // namespace
8051 
8052 IdentifierIterator *ASTReader::getIdentifiers() {
8053   if (!loadGlobalIndex()) {
8054     std::unique_ptr<IdentifierIterator> ReaderIter(
8055         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8056     std::unique_ptr<IdentifierIterator> ModulesIter(
8057         GlobalIndex->createIdentifierIterator());
8058     return new ChainedIdentifierIterator(std::move(ReaderIter),
8059                                          std::move(ModulesIter));
8060   }
8061 
8062   return new ASTIdentifierIterator(*this);
8063 }
8064 
8065 namespace clang {
8066 namespace serialization {
8067 
8068   class ReadMethodPoolVisitor {
8069     ASTReader &Reader;
8070     Selector Sel;
8071     unsigned PriorGeneration;
8072     unsigned InstanceBits = 0;
8073     unsigned FactoryBits = 0;
8074     bool InstanceHasMoreThanOneDecl = false;
8075     bool FactoryHasMoreThanOneDecl = false;
8076     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8077     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8078 
8079   public:
8080     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8081                           unsigned PriorGeneration)
8082         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8083 
8084     bool operator()(ModuleFile &M) {
8085       if (!M.SelectorLookupTable)
8086         return false;
8087 
8088       // If we've already searched this module file, skip it now.
8089       if (M.Generation <= PriorGeneration)
8090         return true;
8091 
8092       ++Reader.NumMethodPoolTableLookups;
8093       ASTSelectorLookupTable *PoolTable
8094         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8095       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8096       if (Pos == PoolTable->end())
8097         return false;
8098 
8099       ++Reader.NumMethodPoolTableHits;
8100       ++Reader.NumSelectorsRead;
8101       // FIXME: Not quite happy with the statistics here. We probably should
8102       // disable this tracking when called via LoadSelector.
8103       // Also, should entries without methods count as misses?
8104       ++Reader.NumMethodPoolEntriesRead;
8105       ASTSelectorLookupTrait::data_type Data = *Pos;
8106       if (Reader.DeserializationListener)
8107         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8108 
8109       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8110       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8111       InstanceBits = Data.InstanceBits;
8112       FactoryBits = Data.FactoryBits;
8113       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8114       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8115       return true;
8116     }
8117 
8118     /// Retrieve the instance methods found by this visitor.
8119     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8120       return InstanceMethods;
8121     }
8122 
8123     /// Retrieve the instance methods found by this visitor.
8124     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8125       return FactoryMethods;
8126     }
8127 
8128     unsigned getInstanceBits() const { return InstanceBits; }
8129     unsigned getFactoryBits() const { return FactoryBits; }
8130 
8131     bool instanceHasMoreThanOneDecl() const {
8132       return InstanceHasMoreThanOneDecl;
8133     }
8134 
8135     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8136   };
8137 
8138 } // namespace serialization
8139 } // namespace clang
8140 
8141 /// Add the given set of methods to the method list.
8142 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8143                              ObjCMethodList &List) {
8144   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8145     S.addMethodToGlobalList(&List, Methods[I]);
8146   }
8147 }
8148 
8149 void ASTReader::ReadMethodPool(Selector Sel) {
8150   // Get the selector generation and update it to the current generation.
8151   unsigned &Generation = SelectorGeneration[Sel];
8152   unsigned PriorGeneration = Generation;
8153   Generation = getGeneration();
8154   SelectorOutOfDate[Sel] = false;
8155 
8156   // Search for methods defined with this selector.
8157   ++NumMethodPoolLookups;
8158   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8159   ModuleMgr.visit(Visitor);
8160 
8161   if (Visitor.getInstanceMethods().empty() &&
8162       Visitor.getFactoryMethods().empty())
8163     return;
8164 
8165   ++NumMethodPoolHits;
8166 
8167   if (!getSema())
8168     return;
8169 
8170   Sema &S = *getSema();
8171   Sema::GlobalMethodPool::iterator Pos
8172     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8173 
8174   Pos->second.first.setBits(Visitor.getInstanceBits());
8175   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8176   Pos->second.second.setBits(Visitor.getFactoryBits());
8177   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8178 
8179   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8180   // when building a module we keep every method individually and may need to
8181   // update hasMoreThanOneDecl as we add the methods.
8182   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8183   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8184 }
8185 
8186 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8187   if (SelectorOutOfDate[Sel])
8188     ReadMethodPool(Sel);
8189 }
8190 
8191 void ASTReader::ReadKnownNamespaces(
8192                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8193   Namespaces.clear();
8194 
8195   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8196     if (NamespaceDecl *Namespace
8197                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8198       Namespaces.push_back(Namespace);
8199   }
8200 }
8201 
8202 void ASTReader::ReadUndefinedButUsed(
8203     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8204   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8205     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8206     SourceLocation Loc =
8207         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8208     Undefined.insert(std::make_pair(D, Loc));
8209   }
8210 }
8211 
8212 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8213     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8214                                                      Exprs) {
8215   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8216     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8217     uint64_t Count = DelayedDeleteExprs[Idx++];
8218     for (uint64_t C = 0; C < Count; ++C) {
8219       SourceLocation DeleteLoc =
8220           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8221       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8222       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8223     }
8224   }
8225 }
8226 
8227 void ASTReader::ReadTentativeDefinitions(
8228                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8229   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8230     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8231     if (Var)
8232       TentativeDefs.push_back(Var);
8233   }
8234   TentativeDefinitions.clear();
8235 }
8236 
8237 void ASTReader::ReadUnusedFileScopedDecls(
8238                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8239   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8240     DeclaratorDecl *D
8241       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8242     if (D)
8243       Decls.push_back(D);
8244   }
8245   UnusedFileScopedDecls.clear();
8246 }
8247 
8248 void ASTReader::ReadDelegatingConstructors(
8249                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8250   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8251     CXXConstructorDecl *D
8252       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8253     if (D)
8254       Decls.push_back(D);
8255   }
8256   DelegatingCtorDecls.clear();
8257 }
8258 
8259 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8260   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8261     TypedefNameDecl *D
8262       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8263     if (D)
8264       Decls.push_back(D);
8265   }
8266   ExtVectorDecls.clear();
8267 }
8268 
8269 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8270     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8271   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8272        ++I) {
8273     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8274         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8275     if (D)
8276       Decls.insert(D);
8277   }
8278   UnusedLocalTypedefNameCandidates.clear();
8279 }
8280 
8281 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8282     llvm::SmallVector<Decl *, 4> &Decls) {
8283   for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8284        ++I) {
8285     auto *D = dyn_cast_or_null<Decl>(
8286         GetDecl(DeclsToCheckForDeferredDiags[I]));
8287     if (D)
8288       Decls.push_back(D);
8289   }
8290   DeclsToCheckForDeferredDiags.clear();
8291 }
8292 
8293 
8294 void ASTReader::ReadReferencedSelectors(
8295        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8296   if (ReferencedSelectorsData.empty())
8297     return;
8298 
8299   // If there are @selector references added them to its pool. This is for
8300   // implementation of -Wselector.
8301   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8302   unsigned I = 0;
8303   while (I < DataSize) {
8304     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8305     SourceLocation SelLoc
8306       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8307     Sels.push_back(std::make_pair(Sel, SelLoc));
8308   }
8309   ReferencedSelectorsData.clear();
8310 }
8311 
8312 void ASTReader::ReadWeakUndeclaredIdentifiers(
8313        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8314   if (WeakUndeclaredIdentifiers.empty())
8315     return;
8316 
8317   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8318     IdentifierInfo *WeakId
8319       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8320     IdentifierInfo *AliasId
8321       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8322     SourceLocation Loc
8323       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8324     bool Used = WeakUndeclaredIdentifiers[I++];
8325     WeakInfo WI(AliasId, Loc);
8326     WI.setUsed(Used);
8327     WeakIDs.push_back(std::make_pair(WeakId, WI));
8328   }
8329   WeakUndeclaredIdentifiers.clear();
8330 }
8331 
8332 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8333   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8334     ExternalVTableUse VT;
8335     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8336     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8337     VT.DefinitionRequired = VTableUses[Idx++];
8338     VTables.push_back(VT);
8339   }
8340 
8341   VTableUses.clear();
8342 }
8343 
8344 void ASTReader::ReadPendingInstantiations(
8345        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8346   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8347     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8348     SourceLocation Loc
8349       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8350 
8351     Pending.push_back(std::make_pair(D, Loc));
8352   }
8353   PendingInstantiations.clear();
8354 }
8355 
8356 void ASTReader::ReadLateParsedTemplates(
8357     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8358         &LPTMap) {
8359   for (unsigned Idx = 0, N = LateParsedTemplates.size(); Idx < N;
8360        /* In loop */) {
8361     FunctionDecl *FD = cast<FunctionDecl>(GetDecl(LateParsedTemplates[Idx++]));
8362 
8363     auto LT = std::make_unique<LateParsedTemplate>();
8364     LT->D = GetDecl(LateParsedTemplates[Idx++]);
8365 
8366     ModuleFile *F = getOwningModuleFile(LT->D);
8367     assert(F && "No module");
8368 
8369     unsigned TokN = LateParsedTemplates[Idx++];
8370     LT->Toks.reserve(TokN);
8371     for (unsigned T = 0; T < TokN; ++T)
8372       LT->Toks.push_back(ReadToken(*F, LateParsedTemplates, Idx));
8373 
8374     LPTMap.insert(std::make_pair(FD, std::move(LT)));
8375   }
8376 
8377   LateParsedTemplates.clear();
8378 }
8379 
8380 void ASTReader::LoadSelector(Selector Sel) {
8381   // It would be complicated to avoid reading the methods anyway. So don't.
8382   ReadMethodPool(Sel);
8383 }
8384 
8385 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8386   assert(ID && "Non-zero identifier ID required");
8387   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8388   IdentifiersLoaded[ID - 1] = II;
8389   if (DeserializationListener)
8390     DeserializationListener->IdentifierRead(ID, II);
8391 }
8392 
8393 /// Set the globally-visible declarations associated with the given
8394 /// identifier.
8395 ///
8396 /// If the AST reader is currently in a state where the given declaration IDs
8397 /// cannot safely be resolved, they are queued until it is safe to resolve
8398 /// them.
8399 ///
8400 /// \param II an IdentifierInfo that refers to one or more globally-visible
8401 /// declarations.
8402 ///
8403 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8404 /// visible at global scope.
8405 ///
8406 /// \param Decls if non-null, this vector will be populated with the set of
8407 /// deserialized declarations. These declarations will not be pushed into
8408 /// scope.
8409 void
8410 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8411                               const SmallVectorImpl<uint32_t> &DeclIDs,
8412                                    SmallVectorImpl<Decl *> *Decls) {
8413   if (NumCurrentElementsDeserializing && !Decls) {
8414     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8415     return;
8416   }
8417 
8418   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8419     if (!SemaObj) {
8420       // Queue this declaration so that it will be added to the
8421       // translation unit scope and identifier's declaration chain
8422       // once a Sema object is known.
8423       PreloadedDeclIDs.push_back(DeclIDs[I]);
8424       continue;
8425     }
8426 
8427     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8428 
8429     // If we're simply supposed to record the declarations, do so now.
8430     if (Decls) {
8431       Decls->push_back(D);
8432       continue;
8433     }
8434 
8435     // Introduce this declaration into the translation-unit scope
8436     // and add it to the declaration chain for this identifier, so
8437     // that (unqualified) name lookup will find it.
8438     pushExternalDeclIntoScope(D, II);
8439   }
8440 }
8441 
8442 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8443   if (ID == 0)
8444     return nullptr;
8445 
8446   if (IdentifiersLoaded.empty()) {
8447     Error("no identifier table in AST file");
8448     return nullptr;
8449   }
8450 
8451   ID -= 1;
8452   if (!IdentifiersLoaded[ID]) {
8453     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8454     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8455     ModuleFile *M = I->second;
8456     unsigned Index = ID - M->BaseIdentifierID;
8457     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8458 
8459     // All of the strings in the AST file are preceded by a 16-bit length.
8460     // Extract that 16-bit length to avoid having to execute strlen().
8461     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8462     //  unsigned integers.  This is important to avoid integer overflow when
8463     //  we cast them to 'unsigned'.
8464     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8465     unsigned StrLen = (((unsigned) StrLenPtr[0])
8466                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8467     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8468     IdentifiersLoaded[ID] = &II;
8469     markIdentifierFromAST(*this,  II);
8470     if (DeserializationListener)
8471       DeserializationListener->IdentifierRead(ID + 1, &II);
8472   }
8473 
8474   return IdentifiersLoaded[ID];
8475 }
8476 
8477 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8478   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8479 }
8480 
8481 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8482   if (LocalID < NUM_PREDEF_IDENT_IDS)
8483     return LocalID;
8484 
8485   if (!M.ModuleOffsetMap.empty())
8486     ReadModuleOffsetMap(M);
8487 
8488   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8489     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8490   assert(I != M.IdentifierRemap.end()
8491          && "Invalid index into identifier index remap");
8492 
8493   return LocalID + I->second;
8494 }
8495 
8496 MacroInfo *ASTReader::getMacro(MacroID ID) {
8497   if (ID == 0)
8498     return nullptr;
8499 
8500   if (MacrosLoaded.empty()) {
8501     Error("no macro table in AST file");
8502     return nullptr;
8503   }
8504 
8505   ID -= NUM_PREDEF_MACRO_IDS;
8506   if (!MacrosLoaded[ID]) {
8507     GlobalMacroMapType::iterator I
8508       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8509     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8510     ModuleFile *M = I->second;
8511     unsigned Index = ID - M->BaseMacroID;
8512     MacrosLoaded[ID] =
8513         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8514 
8515     if (DeserializationListener)
8516       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8517                                          MacrosLoaded[ID]);
8518   }
8519 
8520   return MacrosLoaded[ID];
8521 }
8522 
8523 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8524   if (LocalID < NUM_PREDEF_MACRO_IDS)
8525     return LocalID;
8526 
8527   if (!M.ModuleOffsetMap.empty())
8528     ReadModuleOffsetMap(M);
8529 
8530   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8531     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8532   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8533 
8534   return LocalID + I->second;
8535 }
8536 
8537 serialization::SubmoduleID
8538 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8539   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8540     return LocalID;
8541 
8542   if (!M.ModuleOffsetMap.empty())
8543     ReadModuleOffsetMap(M);
8544 
8545   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8546     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8547   assert(I != M.SubmoduleRemap.end()
8548          && "Invalid index into submodule index remap");
8549 
8550   return LocalID + I->second;
8551 }
8552 
8553 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8554   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8555     assert(GlobalID == 0 && "Unhandled global submodule ID");
8556     return nullptr;
8557   }
8558 
8559   if (GlobalID > SubmodulesLoaded.size()) {
8560     Error("submodule ID out of range in AST file");
8561     return nullptr;
8562   }
8563 
8564   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8565 }
8566 
8567 Module *ASTReader::getModule(unsigned ID) {
8568   return getSubmodule(ID);
8569 }
8570 
8571 bool ASTReader::DeclIsFromPCHWithObjectFile(const Decl *D) {
8572   ModuleFile *MF = getOwningModuleFile(D);
8573   return MF && MF->PCHHasObjectFile;
8574 }
8575 
8576 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8577   if (ID & 1) {
8578     // It's a module, look it up by submodule ID.
8579     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8580     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8581   } else {
8582     // It's a prefix (preamble, PCH, ...). Look it up by index.
8583     unsigned IndexFromEnd = ID >> 1;
8584     assert(IndexFromEnd && "got reference to unknown module file");
8585     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8586   }
8587 }
8588 
8589 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8590   if (!F)
8591     return 1;
8592 
8593   // For a file representing a module, use the submodule ID of the top-level
8594   // module as the file ID. For any other kind of file, the number of such
8595   // files loaded beforehand will be the same on reload.
8596   // FIXME: Is this true even if we have an explicit module file and a PCH?
8597   if (F->isModule())
8598     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8599 
8600   auto PCHModules = getModuleManager().pch_modules();
8601   auto I = llvm::find(PCHModules, F);
8602   assert(I != PCHModules.end() && "emitting reference to unknown file");
8603   return (I - PCHModules.end()) << 1;
8604 }
8605 
8606 llvm::Optional<ASTSourceDescriptor>
8607 ASTReader::getSourceDescriptor(unsigned ID) {
8608   if (Module *M = getSubmodule(ID))
8609     return ASTSourceDescriptor(*M);
8610 
8611   // If there is only a single PCH, return it instead.
8612   // Chained PCH are not supported.
8613   const auto &PCHChain = ModuleMgr.pch_modules();
8614   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8615     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8616     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8617     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8618     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8619                                MF.Signature);
8620   }
8621   return None;
8622 }
8623 
8624 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8625   auto I = DefinitionSource.find(FD);
8626   if (I == DefinitionSource.end())
8627     return EK_ReplyHazy;
8628   return I->second ? EK_Never : EK_Always;
8629 }
8630 
8631 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8632   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8633 }
8634 
8635 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8636   if (ID == 0)
8637     return Selector();
8638 
8639   if (ID > SelectorsLoaded.size()) {
8640     Error("selector ID out of range in AST file");
8641     return Selector();
8642   }
8643 
8644   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8645     // Load this selector from the selector table.
8646     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8647     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8648     ModuleFile &M = *I->second;
8649     ASTSelectorLookupTrait Trait(*this, M);
8650     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8651     SelectorsLoaded[ID - 1] =
8652       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8653     if (DeserializationListener)
8654       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8655   }
8656 
8657   return SelectorsLoaded[ID - 1];
8658 }
8659 
8660 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8661   return DecodeSelector(ID);
8662 }
8663 
8664 uint32_t ASTReader::GetNumExternalSelectors() {
8665   // ID 0 (the null selector) is considered an external selector.
8666   return getTotalNumSelectors() + 1;
8667 }
8668 
8669 serialization::SelectorID
8670 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8671   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8672     return LocalID;
8673 
8674   if (!M.ModuleOffsetMap.empty())
8675     ReadModuleOffsetMap(M);
8676 
8677   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8678     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8679   assert(I != M.SelectorRemap.end()
8680          && "Invalid index into selector index remap");
8681 
8682   return LocalID + I->second;
8683 }
8684 
8685 DeclarationNameLoc
8686 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8687   DeclarationNameLoc DNLoc;
8688   switch (Name.getNameKind()) {
8689   case DeclarationName::CXXConstructorName:
8690   case DeclarationName::CXXDestructorName:
8691   case DeclarationName::CXXConversionFunctionName:
8692     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8693     break;
8694 
8695   case DeclarationName::CXXOperatorName:
8696     DNLoc.CXXOperatorName.BeginOpNameLoc
8697       = readSourceLocation().getRawEncoding();
8698     DNLoc.CXXOperatorName.EndOpNameLoc
8699       = readSourceLocation().getRawEncoding();
8700     break;
8701 
8702   case DeclarationName::CXXLiteralOperatorName:
8703     DNLoc.CXXLiteralOperatorName.OpNameLoc
8704       = readSourceLocation().getRawEncoding();
8705     break;
8706 
8707   case DeclarationName::Identifier:
8708   case DeclarationName::ObjCZeroArgSelector:
8709   case DeclarationName::ObjCOneArgSelector:
8710   case DeclarationName::ObjCMultiArgSelector:
8711   case DeclarationName::CXXUsingDirective:
8712   case DeclarationName::CXXDeductionGuideName:
8713     break;
8714   }
8715   return DNLoc;
8716 }
8717 
8718 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8719   DeclarationNameInfo NameInfo;
8720   NameInfo.setName(readDeclarationName());
8721   NameInfo.setLoc(readSourceLocation());
8722   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8723   return NameInfo;
8724 }
8725 
8726 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8727   Info.QualifierLoc = readNestedNameSpecifierLoc();
8728   unsigned NumTPLists = readInt();
8729   Info.NumTemplParamLists = NumTPLists;
8730   if (NumTPLists) {
8731     Info.TemplParamLists =
8732         new (getContext()) TemplateParameterList *[NumTPLists];
8733     for (unsigned i = 0; i != NumTPLists; ++i)
8734       Info.TemplParamLists[i] = readTemplateParameterList();
8735   }
8736 }
8737 
8738 TemplateParameterList *
8739 ASTRecordReader::readTemplateParameterList() {
8740   SourceLocation TemplateLoc = readSourceLocation();
8741   SourceLocation LAngleLoc = readSourceLocation();
8742   SourceLocation RAngleLoc = readSourceLocation();
8743 
8744   unsigned NumParams = readInt();
8745   SmallVector<NamedDecl *, 16> Params;
8746   Params.reserve(NumParams);
8747   while (NumParams--)
8748     Params.push_back(readDeclAs<NamedDecl>());
8749 
8750   bool HasRequiresClause = readBool();
8751   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8752 
8753   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8754       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8755   return TemplateParams;
8756 }
8757 
8758 void ASTRecordReader::readTemplateArgumentList(
8759                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8760                         bool Canonicalize) {
8761   unsigned NumTemplateArgs = readInt();
8762   TemplArgs.reserve(NumTemplateArgs);
8763   while (NumTemplateArgs--)
8764     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8765 }
8766 
8767 /// Read a UnresolvedSet structure.
8768 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8769   unsigned NumDecls = readInt();
8770   Set.reserve(getContext(), NumDecls);
8771   while (NumDecls--) {
8772     DeclID ID = readDeclID();
8773     AccessSpecifier AS = (AccessSpecifier) readInt();
8774     Set.addLazyDecl(getContext(), ID, AS);
8775   }
8776 }
8777 
8778 CXXBaseSpecifier
8779 ASTRecordReader::readCXXBaseSpecifier() {
8780   bool isVirtual = readBool();
8781   bool isBaseOfClass = readBool();
8782   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8783   bool inheritConstructors = readBool();
8784   TypeSourceInfo *TInfo = readTypeSourceInfo();
8785   SourceRange Range = readSourceRange();
8786   SourceLocation EllipsisLoc = readSourceLocation();
8787   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8788                           EllipsisLoc);
8789   Result.setInheritConstructors(inheritConstructors);
8790   return Result;
8791 }
8792 
8793 CXXCtorInitializer **
8794 ASTRecordReader::readCXXCtorInitializers() {
8795   ASTContext &Context = getContext();
8796   unsigned NumInitializers = readInt();
8797   assert(NumInitializers && "wrote ctor initializers but have no inits");
8798   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8799   for (unsigned i = 0; i != NumInitializers; ++i) {
8800     TypeSourceInfo *TInfo = nullptr;
8801     bool IsBaseVirtual = false;
8802     FieldDecl *Member = nullptr;
8803     IndirectFieldDecl *IndirectMember = nullptr;
8804 
8805     CtorInitializerType Type = (CtorInitializerType) readInt();
8806     switch (Type) {
8807     case CTOR_INITIALIZER_BASE:
8808       TInfo = readTypeSourceInfo();
8809       IsBaseVirtual = readBool();
8810       break;
8811 
8812     case CTOR_INITIALIZER_DELEGATING:
8813       TInfo = readTypeSourceInfo();
8814       break;
8815 
8816      case CTOR_INITIALIZER_MEMBER:
8817       Member = readDeclAs<FieldDecl>();
8818       break;
8819 
8820      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8821       IndirectMember = readDeclAs<IndirectFieldDecl>();
8822       break;
8823     }
8824 
8825     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8826     Expr *Init = readExpr();
8827     SourceLocation LParenLoc = readSourceLocation();
8828     SourceLocation RParenLoc = readSourceLocation();
8829 
8830     CXXCtorInitializer *BOMInit;
8831     if (Type == CTOR_INITIALIZER_BASE)
8832       BOMInit = new (Context)
8833           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8834                              RParenLoc, MemberOrEllipsisLoc);
8835     else if (Type == CTOR_INITIALIZER_DELEGATING)
8836       BOMInit = new (Context)
8837           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8838     else if (Member)
8839       BOMInit = new (Context)
8840           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8841                              Init, RParenLoc);
8842     else
8843       BOMInit = new (Context)
8844           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8845                              LParenLoc, Init, RParenLoc);
8846 
8847     if (/*IsWritten*/readBool()) {
8848       unsigned SourceOrder = readInt();
8849       BOMInit->setSourceOrder(SourceOrder);
8850     }
8851 
8852     CtorInitializers[i] = BOMInit;
8853   }
8854 
8855   return CtorInitializers;
8856 }
8857 
8858 NestedNameSpecifierLoc
8859 ASTRecordReader::readNestedNameSpecifierLoc() {
8860   ASTContext &Context = getContext();
8861   unsigned N = readInt();
8862   NestedNameSpecifierLocBuilder Builder;
8863   for (unsigned I = 0; I != N; ++I) {
8864     auto Kind = readNestedNameSpecifierKind();
8865     switch (Kind) {
8866     case NestedNameSpecifier::Identifier: {
8867       IdentifierInfo *II = readIdentifier();
8868       SourceRange Range = readSourceRange();
8869       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8870       break;
8871     }
8872 
8873     case NestedNameSpecifier::Namespace: {
8874       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8875       SourceRange Range = readSourceRange();
8876       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8877       break;
8878     }
8879 
8880     case NestedNameSpecifier::NamespaceAlias: {
8881       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8882       SourceRange Range = readSourceRange();
8883       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8884       break;
8885     }
8886 
8887     case NestedNameSpecifier::TypeSpec:
8888     case NestedNameSpecifier::TypeSpecWithTemplate: {
8889       bool Template = readBool();
8890       TypeSourceInfo *T = readTypeSourceInfo();
8891       if (!T)
8892         return NestedNameSpecifierLoc();
8893       SourceLocation ColonColonLoc = readSourceLocation();
8894 
8895       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8896       Builder.Extend(Context,
8897                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8898                      T->getTypeLoc(), ColonColonLoc);
8899       break;
8900     }
8901 
8902     case NestedNameSpecifier::Global: {
8903       SourceLocation ColonColonLoc = readSourceLocation();
8904       Builder.MakeGlobal(Context, ColonColonLoc);
8905       break;
8906     }
8907 
8908     case NestedNameSpecifier::Super: {
8909       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8910       SourceRange Range = readSourceRange();
8911       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8912       break;
8913     }
8914     }
8915   }
8916 
8917   return Builder.getWithLocInContext(Context);
8918 }
8919 
8920 SourceRange
8921 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8922                            unsigned &Idx) {
8923   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8924   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8925   return SourceRange(beg, end);
8926 }
8927 
8928 static FixedPointSemantics
8929 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8930                         unsigned &Idx) {
8931   unsigned Width = Record[Idx++];
8932   unsigned Scale = Record[Idx++];
8933   uint64_t Tmp = Record[Idx++];
8934   bool IsSigned = Tmp & 0x1;
8935   bool IsSaturated = Tmp & 0x2;
8936   bool HasUnsignedPadding = Tmp & 0x4;
8937   return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8938                              HasUnsignedPadding);
8939 }
8940 
8941 static const llvm::fltSemantics &
8942 readAPFloatSemantics(ASTRecordReader &reader) {
8943   return llvm::APFloatBase::EnumToSemantics(
8944     static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8945 }
8946 
8947 APValue ASTRecordReader::readAPValue() {
8948   unsigned Kind = readInt();
8949   switch ((APValue::ValueKind) Kind) {
8950   case APValue::None:
8951     return APValue();
8952   case APValue::Indeterminate:
8953     return APValue::IndeterminateValue();
8954   case APValue::Int:
8955     return APValue(readAPSInt());
8956   case APValue::Float: {
8957     const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8958     return APValue(readAPFloat(FloatSema));
8959   }
8960   case APValue::FixedPoint: {
8961     FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8962     return APValue(APFixedPoint(readAPInt(), FPSema));
8963   }
8964   case APValue::ComplexInt: {
8965     llvm::APSInt First = readAPSInt();
8966     return APValue(std::move(First), readAPSInt());
8967   }
8968   case APValue::ComplexFloat: {
8969     const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
8970     llvm::APFloat First = readAPFloat(FloatSema1);
8971     const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
8972     return APValue(std::move(First), readAPFloat(FloatSema2));
8973   }
8974   case APValue::LValue:
8975   case APValue::Vector:
8976   case APValue::Array:
8977   case APValue::Struct:
8978   case APValue::Union:
8979   case APValue::MemberPointer:
8980   case APValue::AddrLabelDiff:
8981     // TODO : Handle all these APValue::ValueKind.
8982     return APValue();
8983   }
8984   llvm_unreachable("Invalid APValue::ValueKind");
8985 }
8986 
8987 /// Read a floating-point value
8988 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
8989   return llvm::APFloat(Sem, readAPInt());
8990 }
8991 
8992 // Read a string
8993 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
8994   unsigned Len = Record[Idx++];
8995   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
8996   Idx += Len;
8997   return Result;
8998 }
8999 
9000 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9001                                 unsigned &Idx) {
9002   std::string Filename = ReadString(Record, Idx);
9003   ResolveImportedPath(F, Filename);
9004   return Filename;
9005 }
9006 
9007 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9008                                 const RecordData &Record, unsigned &Idx) {
9009   std::string Filename = ReadString(Record, Idx);
9010   if (!BaseDirectory.empty())
9011     ResolveImportedPath(Filename, BaseDirectory);
9012   return Filename;
9013 }
9014 
9015 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9016                                          unsigned &Idx) {
9017   unsigned Major = Record[Idx++];
9018   unsigned Minor = Record[Idx++];
9019   unsigned Subminor = Record[Idx++];
9020   if (Minor == 0)
9021     return VersionTuple(Major);
9022   if (Subminor == 0)
9023     return VersionTuple(Major, Minor - 1);
9024   return VersionTuple(Major, Minor - 1, Subminor - 1);
9025 }
9026 
9027 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9028                                           const RecordData &Record,
9029                                           unsigned &Idx) {
9030   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9031   return CXXTemporary::Create(getContext(), Decl);
9032 }
9033 
9034 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9035   return Diag(CurrentImportLoc, DiagID);
9036 }
9037 
9038 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9039   return Diags.Report(Loc, DiagID);
9040 }
9041 
9042 /// Retrieve the identifier table associated with the
9043 /// preprocessor.
9044 IdentifierTable &ASTReader::getIdentifierTable() {
9045   return PP.getIdentifierTable();
9046 }
9047 
9048 /// Record that the given ID maps to the given switch-case
9049 /// statement.
9050 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9051   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9052          "Already have a SwitchCase with this ID");
9053   (*CurrSwitchCaseStmts)[ID] = SC;
9054 }
9055 
9056 /// Retrieve the switch-case statement with the given ID.
9057 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9058   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9059   return (*CurrSwitchCaseStmts)[ID];
9060 }
9061 
9062 void ASTReader::ClearSwitchCaseIDs() {
9063   CurrSwitchCaseStmts->clear();
9064 }
9065 
9066 void ASTReader::ReadComments() {
9067   ASTContext &Context = getContext();
9068   std::vector<RawComment *> Comments;
9069   for (SmallVectorImpl<std::pair<BitstreamCursor,
9070                                  serialization::ModuleFile *>>::iterator
9071        I = CommentsCursors.begin(),
9072        E = CommentsCursors.end();
9073        I != E; ++I) {
9074     Comments.clear();
9075     BitstreamCursor &Cursor = I->first;
9076     serialization::ModuleFile &F = *I->second;
9077     SavedStreamPosition SavedPosition(Cursor);
9078 
9079     RecordData Record;
9080     while (true) {
9081       Expected<llvm::BitstreamEntry> MaybeEntry =
9082           Cursor.advanceSkippingSubblocks(
9083               BitstreamCursor::AF_DontPopBlockAtEnd);
9084       if (!MaybeEntry) {
9085         Error(MaybeEntry.takeError());
9086         return;
9087       }
9088       llvm::BitstreamEntry Entry = MaybeEntry.get();
9089 
9090       switch (Entry.Kind) {
9091       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9092       case llvm::BitstreamEntry::Error:
9093         Error("malformed block record in AST file");
9094         return;
9095       case llvm::BitstreamEntry::EndBlock:
9096         goto NextCursor;
9097       case llvm::BitstreamEntry::Record:
9098         // The interesting case.
9099         break;
9100       }
9101 
9102       // Read a record.
9103       Record.clear();
9104       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9105       if (!MaybeComment) {
9106         Error(MaybeComment.takeError());
9107         return;
9108       }
9109       switch ((CommentRecordTypes)MaybeComment.get()) {
9110       case COMMENTS_RAW_COMMENT: {
9111         unsigned Idx = 0;
9112         SourceRange SR = ReadSourceRange(F, Record, Idx);
9113         RawComment::CommentKind Kind =
9114             (RawComment::CommentKind) Record[Idx++];
9115         bool IsTrailingComment = Record[Idx++];
9116         bool IsAlmostTrailingComment = Record[Idx++];
9117         Comments.push_back(new (Context) RawComment(
9118             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9119         break;
9120       }
9121       }
9122     }
9123   NextCursor:
9124     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9125         FileToOffsetToComment;
9126     for (RawComment *C : Comments) {
9127       SourceLocation CommentLoc = C->getBeginLoc();
9128       if (CommentLoc.isValid()) {
9129         std::pair<FileID, unsigned> Loc =
9130             SourceMgr.getDecomposedLoc(CommentLoc);
9131         if (Loc.first.isValid())
9132           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9133       }
9134     }
9135   }
9136 }
9137 
9138 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9139                                 bool IncludeSystem, bool Complain,
9140                     llvm::function_ref<void(const serialization::InputFile &IF,
9141                                             bool isSystem)> Visitor) {
9142   unsigned NumUserInputs = MF.NumUserInputFiles;
9143   unsigned NumInputs = MF.InputFilesLoaded.size();
9144   assert(NumUserInputs <= NumInputs);
9145   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9146   for (unsigned I = 0; I < N; ++I) {
9147     bool IsSystem = I >= NumUserInputs;
9148     InputFile IF = getInputFile(MF, I+1, Complain);
9149     Visitor(IF, IsSystem);
9150   }
9151 }
9152 
9153 void ASTReader::visitTopLevelModuleMaps(
9154     serialization::ModuleFile &MF,
9155     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9156   unsigned NumInputs = MF.InputFilesLoaded.size();
9157   for (unsigned I = 0; I < NumInputs; ++I) {
9158     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9159     if (IFI.TopLevelModuleMap)
9160       // FIXME: This unnecessarily re-reads the InputFileInfo.
9161       if (auto *FE = getInputFile(MF, I + 1).getFile())
9162         Visitor(FE);
9163   }
9164 }
9165 
9166 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9167   // If we know the owning module, use it.
9168   if (Module *M = D->getImportedOwningModule())
9169     return M->getFullModuleName();
9170 
9171   // Otherwise, use the name of the top-level module the decl is within.
9172   if (ModuleFile *M = getOwningModuleFile(D))
9173     return M->ModuleName;
9174 
9175   // Not from a module.
9176   return {};
9177 }
9178 
9179 void ASTReader::finishPendingActions() {
9180   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9181          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9182          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9183          !PendingUpdateRecords.empty()) {
9184     // If any identifiers with corresponding top-level declarations have
9185     // been loaded, load those declarations now.
9186     using TopLevelDeclsMap =
9187         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9188     TopLevelDeclsMap TopLevelDecls;
9189 
9190     while (!PendingIdentifierInfos.empty()) {
9191       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9192       SmallVector<uint32_t, 4> DeclIDs =
9193           std::move(PendingIdentifierInfos.back().second);
9194       PendingIdentifierInfos.pop_back();
9195 
9196       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9197     }
9198 
9199     // Load each function type that we deferred loading because it was a
9200     // deduced type that might refer to a local type declared within itself.
9201     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9202       auto *FD = PendingFunctionTypes[I].first;
9203       FD->setType(GetType(PendingFunctionTypes[I].second));
9204 
9205       // If we gave a function a deduced return type, remember that we need to
9206       // propagate that along the redeclaration chain.
9207       auto *DT = FD->getReturnType()->getContainedDeducedType();
9208       if (DT && DT->isDeduced())
9209         PendingDeducedTypeUpdates.insert(
9210             {FD->getCanonicalDecl(), FD->getReturnType()});
9211     }
9212     PendingFunctionTypes.clear();
9213 
9214     // For each decl chain that we wanted to complete while deserializing, mark
9215     // it as "still needs to be completed".
9216     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9217       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9218     }
9219     PendingIncompleteDeclChains.clear();
9220 
9221     // Load pending declaration chains.
9222     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9223       loadPendingDeclChain(PendingDeclChains[I].first,
9224                            PendingDeclChains[I].second);
9225     PendingDeclChains.clear();
9226 
9227     // Make the most recent of the top-level declarations visible.
9228     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9229            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9230       IdentifierInfo *II = TLD->first;
9231       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9232         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9233       }
9234     }
9235 
9236     // Load any pending macro definitions.
9237     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9238       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9239       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9240       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9241       // Initialize the macro history from chained-PCHs ahead of module imports.
9242       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9243            ++IDIdx) {
9244         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9245         if (!Info.M->isModule())
9246           resolvePendingMacro(II, Info);
9247       }
9248       // Handle module imports.
9249       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9250            ++IDIdx) {
9251         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9252         if (Info.M->isModule())
9253           resolvePendingMacro(II, Info);
9254       }
9255     }
9256     PendingMacroIDs.clear();
9257 
9258     // Wire up the DeclContexts for Decls that we delayed setting until
9259     // recursive loading is completed.
9260     while (!PendingDeclContextInfos.empty()) {
9261       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9262       PendingDeclContextInfos.pop_front();
9263       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9264       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9265       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9266     }
9267 
9268     // Perform any pending declaration updates.
9269     while (!PendingUpdateRecords.empty()) {
9270       auto Update = PendingUpdateRecords.pop_back_val();
9271       ReadingKindTracker ReadingKind(Read_Decl, *this);
9272       loadDeclUpdateRecords(Update);
9273     }
9274   }
9275 
9276   // At this point, all update records for loaded decls are in place, so any
9277   // fake class definitions should have become real.
9278   assert(PendingFakeDefinitionData.empty() &&
9279          "faked up a class definition but never saw the real one");
9280 
9281   // If we deserialized any C++ or Objective-C class definitions, any
9282   // Objective-C protocol definitions, or any redeclarable templates, make sure
9283   // that all redeclarations point to the definitions. Note that this can only
9284   // happen now, after the redeclaration chains have been fully wired.
9285   for (Decl *D : PendingDefinitions) {
9286     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9287       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9288         // Make sure that the TagType points at the definition.
9289         const_cast<TagType*>(TagT)->decl = TD;
9290       }
9291 
9292       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9293         for (auto *R = getMostRecentExistingDecl(RD); R;
9294              R = R->getPreviousDecl()) {
9295           assert((R == D) ==
9296                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9297                  "declaration thinks it's the definition but it isn't");
9298           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9299         }
9300       }
9301 
9302       continue;
9303     }
9304 
9305     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9306       // Make sure that the ObjCInterfaceType points at the definition.
9307       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9308         ->Decl = ID;
9309 
9310       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9311         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9312 
9313       continue;
9314     }
9315 
9316     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9317       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9318         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9319 
9320       continue;
9321     }
9322 
9323     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9324     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9325       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9326   }
9327   PendingDefinitions.clear();
9328 
9329   // Load the bodies of any functions or methods we've encountered. We do
9330   // this now (delayed) so that we can be sure that the declaration chains
9331   // have been fully wired up (hasBody relies on this).
9332   // FIXME: We shouldn't require complete redeclaration chains here.
9333   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9334                                PBEnd = PendingBodies.end();
9335        PB != PBEnd; ++PB) {
9336     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9337       // For a function defined inline within a class template, force the
9338       // canonical definition to be the one inside the canonical definition of
9339       // the template. This ensures that we instantiate from a correct view
9340       // of the template.
9341       //
9342       // Sadly we can't do this more generally: we can't be sure that all
9343       // copies of an arbitrary class definition will have the same members
9344       // defined (eg, some member functions may not be instantiated, and some
9345       // special members may or may not have been implicitly defined).
9346       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9347         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9348           continue;
9349 
9350       // FIXME: Check for =delete/=default?
9351       // FIXME: Complain about ODR violations here?
9352       const FunctionDecl *Defn = nullptr;
9353       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9354         FD->setLazyBody(PB->second);
9355       } else {
9356         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9357         mergeDefinitionVisibility(NonConstDefn, FD);
9358 
9359         if (!FD->isLateTemplateParsed() &&
9360             !NonConstDefn->isLateTemplateParsed() &&
9361             FD->getODRHash() != NonConstDefn->getODRHash()) {
9362           if (!isa<CXXMethodDecl>(FD)) {
9363             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9364           } else if (FD->getLexicalParent()->isFileContext() &&
9365                      NonConstDefn->getLexicalParent()->isFileContext()) {
9366             // Only diagnose out-of-line method definitions.  If they are
9367             // in class definitions, then an error will be generated when
9368             // processing the class bodies.
9369             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9370           }
9371         }
9372       }
9373       continue;
9374     }
9375 
9376     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9377     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9378       MD->setLazyBody(PB->second);
9379   }
9380   PendingBodies.clear();
9381 
9382   // Do some cleanup.
9383   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9384     getContext().deduplicateMergedDefinitonsFor(ND);
9385   PendingMergedDefinitionsToDeduplicate.clear();
9386 }
9387 
9388 void ASTReader::diagnoseOdrViolations() {
9389   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9390       PendingFunctionOdrMergeFailures.empty() &&
9391       PendingEnumOdrMergeFailures.empty())
9392     return;
9393 
9394   // Trigger the import of the full definition of each class that had any
9395   // odr-merging problems, so we can produce better diagnostics for them.
9396   // These updates may in turn find and diagnose some ODR failures, so take
9397   // ownership of the set first.
9398   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9399   PendingOdrMergeFailures.clear();
9400   for (auto &Merge : OdrMergeFailures) {
9401     Merge.first->buildLookup();
9402     Merge.first->decls_begin();
9403     Merge.first->bases_begin();
9404     Merge.first->vbases_begin();
9405     for (auto &RecordPair : Merge.second) {
9406       auto *RD = RecordPair.first;
9407       RD->decls_begin();
9408       RD->bases_begin();
9409       RD->vbases_begin();
9410     }
9411   }
9412 
9413   // Trigger the import of functions.
9414   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9415   PendingFunctionOdrMergeFailures.clear();
9416   for (auto &Merge : FunctionOdrMergeFailures) {
9417     Merge.first->buildLookup();
9418     Merge.first->decls_begin();
9419     Merge.first->getBody();
9420     for (auto &FD : Merge.second) {
9421       FD->buildLookup();
9422       FD->decls_begin();
9423       FD->getBody();
9424     }
9425   }
9426 
9427   // Trigger the import of enums.
9428   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9429   PendingEnumOdrMergeFailures.clear();
9430   for (auto &Merge : EnumOdrMergeFailures) {
9431     Merge.first->decls_begin();
9432     for (auto &Enum : Merge.second) {
9433       Enum->decls_begin();
9434     }
9435   }
9436 
9437   // For each declaration from a merged context, check that the canonical
9438   // definition of that context also contains a declaration of the same
9439   // entity.
9440   //
9441   // Caution: this loop does things that might invalidate iterators into
9442   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9443   while (!PendingOdrMergeChecks.empty()) {
9444     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9445 
9446     // FIXME: Skip over implicit declarations for now. This matters for things
9447     // like implicitly-declared special member functions. This isn't entirely
9448     // correct; we can end up with multiple unmerged declarations of the same
9449     // implicit entity.
9450     if (D->isImplicit())
9451       continue;
9452 
9453     DeclContext *CanonDef = D->getDeclContext();
9454 
9455     bool Found = false;
9456     const Decl *DCanon = D->getCanonicalDecl();
9457 
9458     for (auto RI : D->redecls()) {
9459       if (RI->getLexicalDeclContext() == CanonDef) {
9460         Found = true;
9461         break;
9462       }
9463     }
9464     if (Found)
9465       continue;
9466 
9467     // Quick check failed, time to do the slow thing. Note, we can't just
9468     // look up the name of D in CanonDef here, because the member that is
9469     // in CanonDef might not be found by name lookup (it might have been
9470     // replaced by a more recent declaration in the lookup table), and we
9471     // can't necessarily find it in the redeclaration chain because it might
9472     // be merely mergeable, not redeclarable.
9473     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9474     for (auto *CanonMember : CanonDef->decls()) {
9475       if (CanonMember->getCanonicalDecl() == DCanon) {
9476         // This can happen if the declaration is merely mergeable and not
9477         // actually redeclarable (we looked for redeclarations earlier).
9478         //
9479         // FIXME: We should be able to detect this more efficiently, without
9480         // pulling in all of the members of CanonDef.
9481         Found = true;
9482         break;
9483       }
9484       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9485         if (ND->getDeclName() == D->getDeclName())
9486           Candidates.push_back(ND);
9487     }
9488 
9489     if (!Found) {
9490       // The AST doesn't like TagDecls becoming invalid after they've been
9491       // completed. We only really need to mark FieldDecls as invalid here.
9492       if (!isa<TagDecl>(D))
9493         D->setInvalidDecl();
9494 
9495       // Ensure we don't accidentally recursively enter deserialization while
9496       // we're producing our diagnostic.
9497       Deserializing RecursionGuard(this);
9498 
9499       std::string CanonDefModule =
9500           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9501       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9502         << D << getOwningModuleNameForDiagnostic(D)
9503         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9504 
9505       if (Candidates.empty())
9506         Diag(cast<Decl>(CanonDef)->getLocation(),
9507              diag::note_module_odr_violation_no_possible_decls) << D;
9508       else {
9509         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9510           Diag(Candidates[I]->getLocation(),
9511                diag::note_module_odr_violation_possible_decl)
9512             << Candidates[I];
9513       }
9514 
9515       DiagnosedOdrMergeFailures.insert(CanonDef);
9516     }
9517   }
9518 
9519   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9520       EnumOdrMergeFailures.empty())
9521     return;
9522 
9523   // Ensure we don't accidentally recursively enter deserialization while
9524   // we're producing our diagnostics.
9525   Deserializing RecursionGuard(this);
9526 
9527   // Common code for hashing helpers.
9528   ODRHash Hash;
9529   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9530     Hash.clear();
9531     Hash.AddQualType(Ty);
9532     return Hash.CalculateHash();
9533   };
9534 
9535   auto ComputeODRHash = [&Hash](const Stmt *S) {
9536     assert(S);
9537     Hash.clear();
9538     Hash.AddStmt(S);
9539     return Hash.CalculateHash();
9540   };
9541 
9542   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9543     assert(D);
9544     Hash.clear();
9545     Hash.AddSubDecl(D);
9546     return Hash.CalculateHash();
9547   };
9548 
9549   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9550     Hash.clear();
9551     Hash.AddTemplateArgument(TA);
9552     return Hash.CalculateHash();
9553   };
9554 
9555   auto ComputeTemplateParameterListODRHash =
9556       [&Hash](const TemplateParameterList *TPL) {
9557         assert(TPL);
9558         Hash.clear();
9559         Hash.AddTemplateParameterList(TPL);
9560         return Hash.CalculateHash();
9561       };
9562 
9563   // Used with err_module_odr_violation_mismatch_decl and
9564   // note_module_odr_violation_mismatch_decl
9565   // This list should be the same Decl's as in ODRHash::isWhiteListedDecl
9566   enum ODRMismatchDecl {
9567     EndOfClass,
9568     PublicSpecifer,
9569     PrivateSpecifer,
9570     ProtectedSpecifer,
9571     StaticAssert,
9572     Field,
9573     CXXMethod,
9574     TypeAlias,
9575     TypeDef,
9576     Var,
9577     Friend,
9578     FunctionTemplate,
9579     Other
9580   };
9581 
9582   // Used with err_module_odr_violation_mismatch_decl_diff and
9583   // note_module_odr_violation_mismatch_decl_diff
9584   enum ODRMismatchDeclDifference {
9585     StaticAssertCondition,
9586     StaticAssertMessage,
9587     StaticAssertOnlyMessage,
9588     FieldName,
9589     FieldTypeName,
9590     FieldSingleBitField,
9591     FieldDifferentWidthBitField,
9592     FieldSingleMutable,
9593     FieldSingleInitializer,
9594     FieldDifferentInitializers,
9595     MethodName,
9596     MethodDeleted,
9597     MethodDefaulted,
9598     MethodVirtual,
9599     MethodStatic,
9600     MethodVolatile,
9601     MethodConst,
9602     MethodInline,
9603     MethodNumberParameters,
9604     MethodParameterType,
9605     MethodParameterName,
9606     MethodParameterSingleDefaultArgument,
9607     MethodParameterDifferentDefaultArgument,
9608     MethodNoTemplateArguments,
9609     MethodDifferentNumberTemplateArguments,
9610     MethodDifferentTemplateArgument,
9611     MethodSingleBody,
9612     MethodDifferentBody,
9613     TypedefName,
9614     TypedefType,
9615     VarName,
9616     VarType,
9617     VarSingleInitializer,
9618     VarDifferentInitializer,
9619     VarConstexpr,
9620     FriendTypeFunction,
9621     FriendType,
9622     FriendFunction,
9623     FunctionTemplateDifferentNumberParameters,
9624     FunctionTemplateParameterDifferentKind,
9625     FunctionTemplateParameterName,
9626     FunctionTemplateParameterSingleDefaultArgument,
9627     FunctionTemplateParameterDifferentDefaultArgument,
9628     FunctionTemplateParameterDifferentType,
9629     FunctionTemplatePackParameter,
9630   };
9631 
9632   // These lambdas have the common portions of the ODR diagnostics.  This
9633   // has the same return as Diag(), so addition parameters can be passed
9634   // in with operator<<
9635   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9636                                  SourceLocation Loc, SourceRange Range,
9637                                  ODRMismatchDeclDifference DiffType) {
9638     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9639            << FirstRecord << FirstModule.empty() << FirstModule << Range
9640            << DiffType;
9641   };
9642   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9643                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9644     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9645            << SecondModule << Range << DiffType;
9646   };
9647 
9648   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9649                        &ComputeQualTypeODRHash, &ComputeODRHash](
9650                           NamedDecl *FirstRecord, StringRef FirstModule,
9651                           StringRef SecondModule, FieldDecl *FirstField,
9652                           FieldDecl *SecondField) {
9653     IdentifierInfo *FirstII = FirstField->getIdentifier();
9654     IdentifierInfo *SecondII = SecondField->getIdentifier();
9655     if (FirstII->getName() != SecondII->getName()) {
9656       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9657                        FirstField->getSourceRange(), FieldName)
9658           << FirstII;
9659       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9660                       SecondField->getSourceRange(), FieldName)
9661           << SecondII;
9662 
9663       return true;
9664     }
9665 
9666     assert(getContext().hasSameType(FirstField->getType(),
9667                                     SecondField->getType()));
9668 
9669     QualType FirstType = FirstField->getType();
9670     QualType SecondType = SecondField->getType();
9671     if (ComputeQualTypeODRHash(FirstType) !=
9672         ComputeQualTypeODRHash(SecondType)) {
9673       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9674                        FirstField->getSourceRange(), FieldTypeName)
9675           << FirstII << FirstType;
9676       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9677                       SecondField->getSourceRange(), FieldTypeName)
9678           << SecondII << SecondType;
9679 
9680       return true;
9681     }
9682 
9683     const bool IsFirstBitField = FirstField->isBitField();
9684     const bool IsSecondBitField = SecondField->isBitField();
9685     if (IsFirstBitField != IsSecondBitField) {
9686       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9687                        FirstField->getSourceRange(), FieldSingleBitField)
9688           << FirstII << IsFirstBitField;
9689       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9690                       SecondField->getSourceRange(), FieldSingleBitField)
9691           << SecondII << IsSecondBitField;
9692       return true;
9693     }
9694 
9695     if (IsFirstBitField && IsSecondBitField) {
9696       unsigned FirstBitWidthHash =
9697           ComputeODRHash(FirstField->getBitWidth());
9698       unsigned SecondBitWidthHash =
9699           ComputeODRHash(SecondField->getBitWidth());
9700       if (FirstBitWidthHash != SecondBitWidthHash) {
9701         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9702                          FirstField->getSourceRange(),
9703                          FieldDifferentWidthBitField)
9704             << FirstII << FirstField->getBitWidth()->getSourceRange();
9705         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9706                         SecondField->getSourceRange(),
9707                         FieldDifferentWidthBitField)
9708             << SecondII << SecondField->getBitWidth()->getSourceRange();
9709         return true;
9710       }
9711     }
9712 
9713     if (!PP.getLangOpts().CPlusPlus)
9714       return false;
9715 
9716     const bool IsFirstMutable = FirstField->isMutable();
9717     const bool IsSecondMutable = SecondField->isMutable();
9718     if (IsFirstMutable != IsSecondMutable) {
9719       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9720                        FirstField->getSourceRange(), FieldSingleMutable)
9721           << FirstII << IsFirstMutable;
9722       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9723                       SecondField->getSourceRange(), FieldSingleMutable)
9724           << SecondII << IsSecondMutable;
9725       return true;
9726     }
9727 
9728     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9729     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9730     if ((!FirstInitializer && SecondInitializer) ||
9731         (FirstInitializer && !SecondInitializer)) {
9732       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9733                        FirstField->getSourceRange(), FieldSingleInitializer)
9734           << FirstII << (FirstInitializer != nullptr);
9735       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9736                       SecondField->getSourceRange(), FieldSingleInitializer)
9737           << SecondII << (SecondInitializer != nullptr);
9738       return true;
9739     }
9740 
9741     if (FirstInitializer && SecondInitializer) {
9742       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9743       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9744       if (FirstInitHash != SecondInitHash) {
9745         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9746                          FirstField->getSourceRange(),
9747                          FieldDifferentInitializers)
9748             << FirstII << FirstInitializer->getSourceRange();
9749         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9750                         SecondField->getSourceRange(),
9751                         FieldDifferentInitializers)
9752             << SecondII << SecondInitializer->getSourceRange();
9753         return true;
9754       }
9755     }
9756 
9757     return false;
9758   };
9759 
9760   auto ODRDiagTypeDefOrAlias =
9761       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9762           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9763           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9764           bool IsTypeAlias) {
9765         auto FirstName = FirstTD->getDeclName();
9766         auto SecondName = SecondTD->getDeclName();
9767         if (FirstName != SecondName) {
9768           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9769                            FirstTD->getSourceRange(), TypedefName)
9770               << IsTypeAlias << FirstName;
9771           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9772                           SecondTD->getSourceRange(), TypedefName)
9773               << IsTypeAlias << SecondName;
9774           return true;
9775         }
9776 
9777         QualType FirstType = FirstTD->getUnderlyingType();
9778         QualType SecondType = SecondTD->getUnderlyingType();
9779         if (ComputeQualTypeODRHash(FirstType) !=
9780             ComputeQualTypeODRHash(SecondType)) {
9781           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9782                            FirstTD->getSourceRange(), TypedefType)
9783               << IsTypeAlias << FirstName << FirstType;
9784           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9785                           SecondTD->getSourceRange(), TypedefType)
9786               << IsTypeAlias << SecondName << SecondType;
9787           return true;
9788         }
9789 
9790         return false;
9791   };
9792 
9793   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9794                      &ComputeQualTypeODRHash, &ComputeODRHash,
9795                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9796                            StringRef SecondModule, VarDecl *FirstVD,
9797                            VarDecl *SecondVD) {
9798     auto FirstName = FirstVD->getDeclName();
9799     auto SecondName = SecondVD->getDeclName();
9800     if (FirstName != SecondName) {
9801       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9802                        FirstVD->getSourceRange(), VarName)
9803           << FirstName;
9804       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9805                       SecondVD->getSourceRange(), VarName)
9806           << SecondName;
9807       return true;
9808     }
9809 
9810     QualType FirstType = FirstVD->getType();
9811     QualType SecondType = SecondVD->getType();
9812     if (ComputeQualTypeODRHash(FirstType) !=
9813         ComputeQualTypeODRHash(SecondType)) {
9814       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9815                        FirstVD->getSourceRange(), VarType)
9816           << FirstName << FirstType;
9817       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9818                       SecondVD->getSourceRange(), VarType)
9819           << SecondName << SecondType;
9820       return true;
9821     }
9822 
9823     if (!PP.getLangOpts().CPlusPlus)
9824       return false;
9825 
9826     const Expr *FirstInit = FirstVD->getInit();
9827     const Expr *SecondInit = SecondVD->getInit();
9828     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9829       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9830                        FirstVD->getSourceRange(), VarSingleInitializer)
9831           << FirstName << (FirstInit == nullptr)
9832           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9833       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9834                       SecondVD->getSourceRange(), VarSingleInitializer)
9835           << SecondName << (SecondInit == nullptr)
9836           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9837       return true;
9838     }
9839 
9840     if (FirstInit && SecondInit &&
9841         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9842       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9843                        FirstVD->getSourceRange(), VarDifferentInitializer)
9844           << FirstName << FirstInit->getSourceRange();
9845       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9846                       SecondVD->getSourceRange(), VarDifferentInitializer)
9847           << SecondName << SecondInit->getSourceRange();
9848       return true;
9849     }
9850 
9851     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9852     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9853     if (FirstIsConstexpr != SecondIsConstexpr) {
9854       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9855                        FirstVD->getSourceRange(), VarConstexpr)
9856           << FirstName << FirstIsConstexpr;
9857       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9858                       SecondVD->getSourceRange(), VarConstexpr)
9859           << SecondName << SecondIsConstexpr;
9860       return true;
9861     }
9862     return false;
9863   };
9864 
9865   auto DifferenceSelector = [](Decl *D) {
9866     assert(D && "valid Decl required");
9867     switch (D->getKind()) {
9868     default:
9869       return Other;
9870     case Decl::AccessSpec:
9871       switch (D->getAccess()) {
9872       case AS_public:
9873         return PublicSpecifer;
9874       case AS_private:
9875         return PrivateSpecifer;
9876       case AS_protected:
9877         return ProtectedSpecifer;
9878       case AS_none:
9879         break;
9880       }
9881       llvm_unreachable("Invalid access specifier");
9882     case Decl::StaticAssert:
9883       return StaticAssert;
9884     case Decl::Field:
9885       return Field;
9886     case Decl::CXXMethod:
9887     case Decl::CXXConstructor:
9888     case Decl::CXXDestructor:
9889       return CXXMethod;
9890     case Decl::TypeAlias:
9891       return TypeAlias;
9892     case Decl::Typedef:
9893       return TypeDef;
9894     case Decl::Var:
9895       return Var;
9896     case Decl::Friend:
9897       return Friend;
9898     case Decl::FunctionTemplate:
9899       return FunctionTemplate;
9900     }
9901   };
9902 
9903   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9904   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9905                                                  RecordDecl *Record,
9906                                                  const DeclContext *DC) {
9907     for (auto *D : Record->decls()) {
9908       if (!ODRHash::isWhitelistedDecl(D, DC))
9909         continue;
9910       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9911     }
9912   };
9913 
9914   struct DiffResult {
9915     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9916     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9917   };
9918 
9919   // If there is a diagnoseable difference, FirstDiffType and
9920   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9921   // filled in if not EndOfClass.
9922   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9923                                              DeclHashes &SecondHashes) {
9924     DiffResult DR;
9925     auto FirstIt = FirstHashes.begin();
9926     auto SecondIt = SecondHashes.begin();
9927     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9928       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9929           FirstIt->second == SecondIt->second) {
9930         ++FirstIt;
9931         ++SecondIt;
9932         continue;
9933       }
9934 
9935       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9936       DR.SecondDecl =
9937           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9938 
9939       DR.FirstDiffType =
9940           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9941       DR.SecondDiffType =
9942           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9943       return DR;
9944     }
9945     return DR;
9946   };
9947 
9948   // Use this to diagnose that an unexpected Decl was encountered
9949   // or no difference was detected. This causes a generic error
9950   // message to be emitted.
9951   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9952                                       StringRef FirstModule,
9953                                       NamedDecl *SecondRecord,
9954                                       StringRef SecondModule) {
9955     Diag(FirstRecord->getLocation(),
9956          diag::err_module_odr_violation_different_definitions)
9957         << FirstRecord << FirstModule.empty() << FirstModule;
9958 
9959     if (DR.FirstDecl) {
9960       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9961           << FirstRecord << DR.FirstDecl->getSourceRange();
9962     }
9963 
9964     Diag(SecondRecord->getLocation(),
9965          diag::note_module_odr_violation_different_definitions)
9966         << SecondModule;
9967 
9968     if (DR.SecondDecl) {
9969       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
9970           << DR.SecondDecl->getSourceRange();
9971     }
9972   };
9973 
9974   auto DiagnoseODRMismatch =
9975       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
9976              NamedDecl *SecondRecord, StringRef SecondModule) {
9977         SourceLocation FirstLoc;
9978         SourceRange FirstRange;
9979         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
9980         if (DR.FirstDiffType == EndOfClass && FirstTag) {
9981           FirstLoc = FirstTag->getBraceRange().getEnd();
9982         } else {
9983           FirstLoc = DR.FirstDecl->getLocation();
9984           FirstRange = DR.FirstDecl->getSourceRange();
9985         }
9986         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
9987             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
9988             << DR.FirstDiffType;
9989 
9990         SourceLocation SecondLoc;
9991         SourceRange SecondRange;
9992         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
9993         if (DR.SecondDiffType == EndOfClass && SecondTag) {
9994           SecondLoc = SecondTag->getBraceRange().getEnd();
9995         } else {
9996           SecondLoc = DR.SecondDecl->getLocation();
9997           SecondRange = DR.SecondDecl->getSourceRange();
9998         }
9999         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10000             << SecondModule << SecondRange << DR.SecondDiffType;
10001       };
10002 
10003   // Issue any pending ODR-failure diagnostics.
10004   for (auto &Merge : OdrMergeFailures) {
10005     // If we've already pointed out a specific problem with this class, don't
10006     // bother issuing a general "something's different" diagnostic.
10007     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10008       continue;
10009 
10010     bool Diagnosed = false;
10011     CXXRecordDecl *FirstRecord = Merge.first;
10012     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10013     for (auto &RecordPair : Merge.second) {
10014       CXXRecordDecl *SecondRecord = RecordPair.first;
10015       // Multiple different declarations got merged together; tell the user
10016       // where they came from.
10017       if (FirstRecord == SecondRecord)
10018         continue;
10019 
10020       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10021 
10022       auto *FirstDD = FirstRecord->DefinitionData;
10023       auto *SecondDD = RecordPair.second;
10024 
10025       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10026 
10027       // Diagnostics from DefinitionData are emitted here.
10028       if (FirstDD != SecondDD) {
10029         enum ODRDefinitionDataDifference {
10030           NumBases,
10031           NumVBases,
10032           BaseType,
10033           BaseVirtual,
10034           BaseAccess,
10035         };
10036         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10037                                  this](SourceLocation Loc, SourceRange Range,
10038                                        ODRDefinitionDataDifference DiffType) {
10039           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10040                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10041                  << DiffType;
10042         };
10043         auto ODRDiagBaseNote = [&SecondModule,
10044                                 this](SourceLocation Loc, SourceRange Range,
10045                                       ODRDefinitionDataDifference DiffType) {
10046           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10047                  << SecondModule << Range << DiffType;
10048         };
10049 
10050         unsigned FirstNumBases = FirstDD->NumBases;
10051         unsigned FirstNumVBases = FirstDD->NumVBases;
10052         unsigned SecondNumBases = SecondDD->NumBases;
10053         unsigned SecondNumVBases = SecondDD->NumVBases;
10054 
10055         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10056           unsigned NumBases = DD->NumBases;
10057           if (NumBases == 0) return SourceRange();
10058           auto bases = DD->bases();
10059           return SourceRange(bases[0].getBeginLoc(),
10060                              bases[NumBases - 1].getEndLoc());
10061         };
10062 
10063         if (FirstNumBases != SecondNumBases) {
10064           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10065                            NumBases)
10066               << FirstNumBases;
10067           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10068                           NumBases)
10069               << SecondNumBases;
10070           Diagnosed = true;
10071           break;
10072         }
10073 
10074         if (FirstNumVBases != SecondNumVBases) {
10075           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10076                            NumVBases)
10077               << FirstNumVBases;
10078           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10079                           NumVBases)
10080               << SecondNumVBases;
10081           Diagnosed = true;
10082           break;
10083         }
10084 
10085         auto FirstBases = FirstDD->bases();
10086         auto SecondBases = SecondDD->bases();
10087         unsigned i = 0;
10088         for (i = 0; i < FirstNumBases; ++i) {
10089           auto FirstBase = FirstBases[i];
10090           auto SecondBase = SecondBases[i];
10091           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10092               ComputeQualTypeODRHash(SecondBase.getType())) {
10093             ODRDiagBaseError(FirstRecord->getLocation(),
10094                              FirstBase.getSourceRange(), BaseType)
10095                 << (i + 1) << FirstBase.getType();
10096             ODRDiagBaseNote(SecondRecord->getLocation(),
10097                             SecondBase.getSourceRange(), BaseType)
10098                 << (i + 1) << SecondBase.getType();
10099             break;
10100           }
10101 
10102           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10103             ODRDiagBaseError(FirstRecord->getLocation(),
10104                              FirstBase.getSourceRange(), BaseVirtual)
10105                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10106             ODRDiagBaseNote(SecondRecord->getLocation(),
10107                             SecondBase.getSourceRange(), BaseVirtual)
10108                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10109             break;
10110           }
10111 
10112           if (FirstBase.getAccessSpecifierAsWritten() !=
10113               SecondBase.getAccessSpecifierAsWritten()) {
10114             ODRDiagBaseError(FirstRecord->getLocation(),
10115                              FirstBase.getSourceRange(), BaseAccess)
10116                 << (i + 1) << FirstBase.getType()
10117                 << (int)FirstBase.getAccessSpecifierAsWritten();
10118             ODRDiagBaseNote(SecondRecord->getLocation(),
10119                             SecondBase.getSourceRange(), BaseAccess)
10120                 << (i + 1) << SecondBase.getType()
10121                 << (int)SecondBase.getAccessSpecifierAsWritten();
10122             break;
10123           }
10124         }
10125 
10126         if (i != FirstNumBases) {
10127           Diagnosed = true;
10128           break;
10129         }
10130       }
10131 
10132       const ClassTemplateDecl *FirstTemplate =
10133           FirstRecord->getDescribedClassTemplate();
10134       const ClassTemplateDecl *SecondTemplate =
10135           SecondRecord->getDescribedClassTemplate();
10136 
10137       assert(!FirstTemplate == !SecondTemplate &&
10138              "Both pointers should be null or non-null");
10139 
10140       enum ODRTemplateDifference {
10141         ParamEmptyName,
10142         ParamName,
10143         ParamSingleDefaultArgument,
10144         ParamDifferentDefaultArgument,
10145       };
10146 
10147       if (FirstTemplate && SecondTemplate) {
10148         DeclHashes FirstTemplateHashes;
10149         DeclHashes SecondTemplateHashes;
10150 
10151         auto PopulateTemplateParameterHashs =
10152             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10153                                      const ClassTemplateDecl *TD) {
10154               for (auto *D : TD->getTemplateParameters()->asArray()) {
10155                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10156               }
10157             };
10158 
10159         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10160         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10161 
10162         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10163                "Number of template parameters should be equal.");
10164 
10165         auto FirstIt = FirstTemplateHashes.begin();
10166         auto FirstEnd = FirstTemplateHashes.end();
10167         auto SecondIt = SecondTemplateHashes.begin();
10168         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10169           if (FirstIt->second == SecondIt->second)
10170             continue;
10171 
10172           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10173                                           SourceLocation Loc, SourceRange Range,
10174                                           ODRTemplateDifference DiffType) {
10175             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10176                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10177                    << DiffType;
10178           };
10179           auto ODRDiagTemplateNote = [&SecondModule, this](
10180                                          SourceLocation Loc, SourceRange Range,
10181                                          ODRTemplateDifference DiffType) {
10182             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10183                    << SecondModule << Range << DiffType;
10184           };
10185 
10186           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10187           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10188 
10189           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10190                  "Parameter Decl's should be the same kind.");
10191 
10192           DeclarationName FirstName = FirstDecl->getDeclName();
10193           DeclarationName SecondName = SecondDecl->getDeclName();
10194 
10195           if (FirstName != SecondName) {
10196             const bool FirstNameEmpty =
10197                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10198             const bool SecondNameEmpty =
10199                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10200             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10201                    "Both template parameters cannot be unnamed.");
10202             ODRDiagTemplateError(FirstDecl->getLocation(),
10203                                  FirstDecl->getSourceRange(),
10204                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10205                 << FirstName;
10206             ODRDiagTemplateNote(SecondDecl->getLocation(),
10207                                 SecondDecl->getSourceRange(),
10208                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10209                 << SecondName;
10210             break;
10211           }
10212 
10213           switch (FirstDecl->getKind()) {
10214           default:
10215             llvm_unreachable("Invalid template parameter type.");
10216           case Decl::TemplateTypeParm: {
10217             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10218             const auto *SecondParam = cast<TemplateTypeParmDecl>(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           case Decl::NonTypeTemplateParm: {
10251             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10252             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10253             const bool HasFirstDefaultArgument =
10254                 FirstParam->hasDefaultArgument() &&
10255                 !FirstParam->defaultArgumentWasInherited();
10256             const bool HasSecondDefaultArgument =
10257                 SecondParam->hasDefaultArgument() &&
10258                 !SecondParam->defaultArgumentWasInherited();
10259 
10260             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10261               ODRDiagTemplateError(FirstDecl->getLocation(),
10262                                    FirstDecl->getSourceRange(),
10263                                    ParamSingleDefaultArgument)
10264                   << HasFirstDefaultArgument;
10265               ODRDiagTemplateNote(SecondDecl->getLocation(),
10266                                   SecondDecl->getSourceRange(),
10267                                   ParamSingleDefaultArgument)
10268                   << HasSecondDefaultArgument;
10269               break;
10270             }
10271 
10272             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10273                    "Expecting default arguments.");
10274 
10275             ODRDiagTemplateError(FirstDecl->getLocation(),
10276                                  FirstDecl->getSourceRange(),
10277                                  ParamDifferentDefaultArgument);
10278             ODRDiagTemplateNote(SecondDecl->getLocation(),
10279                                 SecondDecl->getSourceRange(),
10280                                 ParamDifferentDefaultArgument);
10281 
10282             break;
10283           }
10284           case Decl::TemplateTemplateParm: {
10285             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10286             const auto *SecondParam =
10287                 cast<TemplateTemplateParmDecl>(SecondDecl);
10288             const bool HasFirstDefaultArgument =
10289                 FirstParam->hasDefaultArgument() &&
10290                 !FirstParam->defaultArgumentWasInherited();
10291             const bool HasSecondDefaultArgument =
10292                 SecondParam->hasDefaultArgument() &&
10293                 !SecondParam->defaultArgumentWasInherited();
10294 
10295             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10296               ODRDiagTemplateError(FirstDecl->getLocation(),
10297                                    FirstDecl->getSourceRange(),
10298                                    ParamSingleDefaultArgument)
10299                   << HasFirstDefaultArgument;
10300               ODRDiagTemplateNote(SecondDecl->getLocation(),
10301                                   SecondDecl->getSourceRange(),
10302                                   ParamSingleDefaultArgument)
10303                   << HasSecondDefaultArgument;
10304               break;
10305             }
10306 
10307             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10308                    "Expecting default arguments.");
10309 
10310             ODRDiagTemplateError(FirstDecl->getLocation(),
10311                                  FirstDecl->getSourceRange(),
10312                                  ParamDifferentDefaultArgument);
10313             ODRDiagTemplateNote(SecondDecl->getLocation(),
10314                                 SecondDecl->getSourceRange(),
10315                                 ParamDifferentDefaultArgument);
10316 
10317             break;
10318           }
10319           }
10320 
10321           break;
10322         }
10323 
10324         if (FirstIt != FirstEnd) {
10325           Diagnosed = true;
10326           break;
10327         }
10328       }
10329 
10330       DeclHashes FirstHashes;
10331       DeclHashes SecondHashes;
10332       const DeclContext *DC = FirstRecord;
10333       PopulateHashes(FirstHashes, FirstRecord, DC);
10334       PopulateHashes(SecondHashes, SecondRecord, DC);
10335 
10336       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10337       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10338       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10339       Decl *FirstDecl = DR.FirstDecl;
10340       Decl *SecondDecl = DR.SecondDecl;
10341 
10342       if (FirstDiffType == Other || SecondDiffType == Other) {
10343         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10344                               SecondModule);
10345         Diagnosed = true;
10346         break;
10347       }
10348 
10349       if (FirstDiffType != SecondDiffType) {
10350         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10351                             SecondModule);
10352         Diagnosed = true;
10353         break;
10354       }
10355 
10356       assert(FirstDiffType == SecondDiffType);
10357 
10358       switch (FirstDiffType) {
10359       case Other:
10360       case EndOfClass:
10361       case PublicSpecifer:
10362       case PrivateSpecifer:
10363       case ProtectedSpecifer:
10364         llvm_unreachable("Invalid diff type");
10365 
10366       case StaticAssert: {
10367         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10368         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10369 
10370         Expr *FirstExpr = FirstSA->getAssertExpr();
10371         Expr *SecondExpr = SecondSA->getAssertExpr();
10372         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10373         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10374         if (FirstODRHash != SecondODRHash) {
10375           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10376                            FirstExpr->getSourceRange(), StaticAssertCondition);
10377           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10378                           SecondExpr->getSourceRange(), StaticAssertCondition);
10379           Diagnosed = true;
10380           break;
10381         }
10382 
10383         StringLiteral *FirstStr = FirstSA->getMessage();
10384         StringLiteral *SecondStr = SecondSA->getMessage();
10385         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10386         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10387           SourceLocation FirstLoc, SecondLoc;
10388           SourceRange FirstRange, SecondRange;
10389           if (FirstStr) {
10390             FirstLoc = FirstStr->getBeginLoc();
10391             FirstRange = FirstStr->getSourceRange();
10392           } else {
10393             FirstLoc = FirstSA->getBeginLoc();
10394             FirstRange = FirstSA->getSourceRange();
10395           }
10396           if (SecondStr) {
10397             SecondLoc = SecondStr->getBeginLoc();
10398             SecondRange = SecondStr->getSourceRange();
10399           } else {
10400             SecondLoc = SecondSA->getBeginLoc();
10401             SecondRange = SecondSA->getSourceRange();
10402           }
10403           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10404                            StaticAssertOnlyMessage)
10405               << (FirstStr == nullptr);
10406           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10407                           StaticAssertOnlyMessage)
10408               << (SecondStr == nullptr);
10409           Diagnosed = true;
10410           break;
10411         }
10412 
10413         if (FirstStr && SecondStr &&
10414             FirstStr->getString() != SecondStr->getString()) {
10415           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10416                            FirstStr->getSourceRange(), StaticAssertMessage);
10417           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10418                           SecondStr->getSourceRange(), StaticAssertMessage);
10419           Diagnosed = true;
10420           break;
10421         }
10422         break;
10423       }
10424       case Field: {
10425         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10426                                  cast<FieldDecl>(FirstDecl),
10427                                  cast<FieldDecl>(SecondDecl));
10428         break;
10429       }
10430       case CXXMethod: {
10431         enum {
10432           DiagMethod,
10433           DiagConstructor,
10434           DiagDestructor,
10435         } FirstMethodType,
10436             SecondMethodType;
10437         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10438           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10439           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10440           return DiagMethod;
10441         };
10442         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10443         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10444         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10445         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10446         auto FirstName = FirstMethod->getDeclName();
10447         auto SecondName = SecondMethod->getDeclName();
10448         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10449           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10450                            FirstMethod->getSourceRange(), MethodName)
10451               << FirstMethodType << FirstName;
10452           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10453                           SecondMethod->getSourceRange(), MethodName)
10454               << SecondMethodType << SecondName;
10455 
10456           Diagnosed = true;
10457           break;
10458         }
10459 
10460         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10461         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10462         if (FirstDeleted != SecondDeleted) {
10463           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10464                            FirstMethod->getSourceRange(), MethodDeleted)
10465               << FirstMethodType << FirstName << FirstDeleted;
10466 
10467           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10468                           SecondMethod->getSourceRange(), MethodDeleted)
10469               << SecondMethodType << SecondName << SecondDeleted;
10470           Diagnosed = true;
10471           break;
10472         }
10473 
10474         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10475         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10476         if (FirstDefaulted != SecondDefaulted) {
10477           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10478                            FirstMethod->getSourceRange(), MethodDefaulted)
10479               << FirstMethodType << FirstName << FirstDefaulted;
10480 
10481           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10482                           SecondMethod->getSourceRange(), MethodDefaulted)
10483               << SecondMethodType << SecondName << SecondDefaulted;
10484           Diagnosed = true;
10485           break;
10486         }
10487 
10488         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10489         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10490         const bool FirstPure = FirstMethod->isPure();
10491         const bool SecondPure = SecondMethod->isPure();
10492         if ((FirstVirtual || SecondVirtual) &&
10493             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10494           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10495                            FirstMethod->getSourceRange(), MethodVirtual)
10496               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10497           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10498                           SecondMethod->getSourceRange(), MethodVirtual)
10499               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10500           Diagnosed = true;
10501           break;
10502         }
10503 
10504         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10505         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10506         // class needs to be checked instead.
10507         const auto FirstStorage = FirstMethod->getStorageClass();
10508         const auto SecondStorage = SecondMethod->getStorageClass();
10509         const bool FirstStatic = FirstStorage == SC_Static;
10510         const bool SecondStatic = SecondStorage == SC_Static;
10511         if (FirstStatic != SecondStatic) {
10512           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10513                            FirstMethod->getSourceRange(), MethodStatic)
10514               << FirstMethodType << FirstName << FirstStatic;
10515           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10516                           SecondMethod->getSourceRange(), MethodStatic)
10517               << SecondMethodType << SecondName << SecondStatic;
10518           Diagnosed = true;
10519           break;
10520         }
10521 
10522         const bool FirstVolatile = FirstMethod->isVolatile();
10523         const bool SecondVolatile = SecondMethod->isVolatile();
10524         if (FirstVolatile != SecondVolatile) {
10525           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10526                            FirstMethod->getSourceRange(), MethodVolatile)
10527               << FirstMethodType << FirstName << FirstVolatile;
10528           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10529                           SecondMethod->getSourceRange(), MethodVolatile)
10530               << SecondMethodType << SecondName << SecondVolatile;
10531           Diagnosed = true;
10532           break;
10533         }
10534 
10535         const bool FirstConst = FirstMethod->isConst();
10536         const bool SecondConst = SecondMethod->isConst();
10537         if (FirstConst != SecondConst) {
10538           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10539                            FirstMethod->getSourceRange(), MethodConst)
10540               << FirstMethodType << FirstName << FirstConst;
10541           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10542                           SecondMethod->getSourceRange(), MethodConst)
10543               << SecondMethodType << SecondName << SecondConst;
10544           Diagnosed = true;
10545           break;
10546         }
10547 
10548         const bool FirstInline = FirstMethod->isInlineSpecified();
10549         const bool SecondInline = SecondMethod->isInlineSpecified();
10550         if (FirstInline != SecondInline) {
10551           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10552                            FirstMethod->getSourceRange(), MethodInline)
10553               << FirstMethodType << FirstName << FirstInline;
10554           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10555                           SecondMethod->getSourceRange(), MethodInline)
10556               << SecondMethodType << SecondName << SecondInline;
10557           Diagnosed = true;
10558           break;
10559         }
10560 
10561         const unsigned FirstNumParameters = FirstMethod->param_size();
10562         const unsigned SecondNumParameters = SecondMethod->param_size();
10563         if (FirstNumParameters != SecondNumParameters) {
10564           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10565                            FirstMethod->getSourceRange(),
10566                            MethodNumberParameters)
10567               << FirstMethodType << FirstName << FirstNumParameters;
10568           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10569                           SecondMethod->getSourceRange(),
10570                           MethodNumberParameters)
10571               << SecondMethodType << SecondName << SecondNumParameters;
10572           Diagnosed = true;
10573           break;
10574         }
10575 
10576         // Need this status boolean to know when break out of the switch.
10577         bool ParameterMismatch = false;
10578         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10579           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10580           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10581 
10582           QualType FirstParamType = FirstParam->getType();
10583           QualType SecondParamType = SecondParam->getType();
10584           if (FirstParamType != SecondParamType &&
10585               ComputeQualTypeODRHash(FirstParamType) !=
10586                   ComputeQualTypeODRHash(SecondParamType)) {
10587             if (const DecayedType *ParamDecayedType =
10588                     FirstParamType->getAs<DecayedType>()) {
10589               ODRDiagDeclError(
10590                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10591                   FirstMethod->getSourceRange(), MethodParameterType)
10592                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10593                   << true << ParamDecayedType->getOriginalType();
10594             } else {
10595               ODRDiagDeclError(
10596                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10597                   FirstMethod->getSourceRange(), MethodParameterType)
10598                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10599                   << false;
10600             }
10601 
10602             if (const DecayedType *ParamDecayedType =
10603                     SecondParamType->getAs<DecayedType>()) {
10604               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10605                               SecondMethod->getSourceRange(),
10606                               MethodParameterType)
10607                   << SecondMethodType << SecondName << (I + 1)
10608                   << SecondParamType << true
10609                   << ParamDecayedType->getOriginalType();
10610             } else {
10611               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10612                               SecondMethod->getSourceRange(),
10613                               MethodParameterType)
10614                   << SecondMethodType << SecondName << (I + 1)
10615                   << SecondParamType << false;
10616             }
10617             ParameterMismatch = true;
10618             break;
10619           }
10620 
10621           DeclarationName FirstParamName = FirstParam->getDeclName();
10622           DeclarationName SecondParamName = SecondParam->getDeclName();
10623           if (FirstParamName != SecondParamName) {
10624             ODRDiagDeclError(FirstRecord, FirstModule,
10625                              FirstMethod->getLocation(),
10626                              FirstMethod->getSourceRange(), MethodParameterName)
10627                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10628             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10629                             SecondMethod->getSourceRange(), MethodParameterName)
10630                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10631             ParameterMismatch = true;
10632             break;
10633           }
10634 
10635           const Expr *FirstInit = FirstParam->getInit();
10636           const Expr *SecondInit = SecondParam->getInit();
10637           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10638             ODRDiagDeclError(FirstRecord, FirstModule,
10639                              FirstMethod->getLocation(),
10640                              FirstMethod->getSourceRange(),
10641                              MethodParameterSingleDefaultArgument)
10642                 << FirstMethodType << FirstName << (I + 1)
10643                 << (FirstInit == nullptr)
10644                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10645             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10646                             SecondMethod->getSourceRange(),
10647                             MethodParameterSingleDefaultArgument)
10648                 << SecondMethodType << SecondName << (I + 1)
10649                 << (SecondInit == nullptr)
10650                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10651             ParameterMismatch = true;
10652             break;
10653           }
10654 
10655           if (FirstInit && SecondInit &&
10656               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10657             ODRDiagDeclError(FirstRecord, FirstModule,
10658                              FirstMethod->getLocation(),
10659                              FirstMethod->getSourceRange(),
10660                              MethodParameterDifferentDefaultArgument)
10661                 << FirstMethodType << FirstName << (I + 1)
10662                 << FirstInit->getSourceRange();
10663             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10664                             SecondMethod->getSourceRange(),
10665                             MethodParameterDifferentDefaultArgument)
10666                 << SecondMethodType << SecondName << (I + 1)
10667                 << SecondInit->getSourceRange();
10668             ParameterMismatch = true;
10669             break;
10670 
10671           }
10672         }
10673 
10674         if (ParameterMismatch) {
10675           Diagnosed = true;
10676           break;
10677         }
10678 
10679         const auto *FirstTemplateArgs =
10680             FirstMethod->getTemplateSpecializationArgs();
10681         const auto *SecondTemplateArgs =
10682             SecondMethod->getTemplateSpecializationArgs();
10683 
10684         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10685             (!FirstTemplateArgs && SecondTemplateArgs)) {
10686           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10687                            FirstMethod->getSourceRange(),
10688                            MethodNoTemplateArguments)
10689               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10690           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10691                           SecondMethod->getSourceRange(),
10692                           MethodNoTemplateArguments)
10693               << SecondMethodType << SecondName
10694               << (SecondTemplateArgs != nullptr);
10695 
10696           Diagnosed = true;
10697           break;
10698         }
10699 
10700         if (FirstTemplateArgs && SecondTemplateArgs) {
10701           // Remove pack expansions from argument list.
10702           auto ExpandTemplateArgumentList =
10703               [](const TemplateArgumentList *TAL) {
10704                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10705                 for (const TemplateArgument &TA : TAL->asArray()) {
10706                   if (TA.getKind() != TemplateArgument::Pack) {
10707                     ExpandedList.push_back(&TA);
10708                     continue;
10709                   }
10710                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10711                     ExpandedList.push_back(&PackTA);
10712                   }
10713                 }
10714                 return ExpandedList;
10715               };
10716           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10717               ExpandTemplateArgumentList(FirstTemplateArgs);
10718           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10719               ExpandTemplateArgumentList(SecondTemplateArgs);
10720 
10721           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10722             ODRDiagDeclError(FirstRecord, FirstModule,
10723                              FirstMethod->getLocation(),
10724                              FirstMethod->getSourceRange(),
10725                              MethodDifferentNumberTemplateArguments)
10726                 << FirstMethodType << FirstName
10727                 << (unsigned)FirstExpandedList.size();
10728             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10729                             SecondMethod->getSourceRange(),
10730                             MethodDifferentNumberTemplateArguments)
10731                 << SecondMethodType << SecondName
10732                 << (unsigned)SecondExpandedList.size();
10733 
10734             Diagnosed = true;
10735             break;
10736           }
10737 
10738           bool TemplateArgumentMismatch = false;
10739           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10740             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10741                                    &SecondTA = *SecondExpandedList[i];
10742             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10743                 ComputeTemplateArgumentODRHash(SecondTA)) {
10744               continue;
10745             }
10746 
10747             ODRDiagDeclError(
10748                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10749                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10750                 << FirstMethodType << FirstName << FirstTA << i + 1;
10751             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10752                             SecondMethod->getSourceRange(),
10753                             MethodDifferentTemplateArgument)
10754                 << SecondMethodType << SecondName << SecondTA << i + 1;
10755 
10756             TemplateArgumentMismatch = true;
10757             break;
10758           }
10759 
10760           if (TemplateArgumentMismatch) {
10761             Diagnosed = true;
10762             break;
10763           }
10764         }
10765 
10766         // Compute the hash of the method as if it has no body.
10767         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10768           Hash.clear();
10769           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10770           return Hash.CalculateHash();
10771         };
10772 
10773         // Compare the hash generated to the hash stored.  A difference means
10774         // that a body was present in the original source.  Due to merging,
10775         // the stardard way of detecting a body will not work.
10776         const bool HasFirstBody =
10777             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10778         const bool HasSecondBody =
10779             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10780 
10781         if (HasFirstBody != HasSecondBody) {
10782           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10783                            FirstMethod->getSourceRange(), MethodSingleBody)
10784               << FirstMethodType << FirstName << HasFirstBody;
10785           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10786                           SecondMethod->getSourceRange(), MethodSingleBody)
10787               << SecondMethodType << SecondName << HasSecondBody;
10788           Diagnosed = true;
10789           break;
10790         }
10791 
10792         if (HasFirstBody && HasSecondBody) {
10793           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10794                            FirstMethod->getSourceRange(), MethodDifferentBody)
10795               << FirstMethodType << FirstName;
10796           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10797                           SecondMethod->getSourceRange(), MethodDifferentBody)
10798               << SecondMethodType << SecondName;
10799           Diagnosed = true;
10800           break;
10801         }
10802 
10803         break;
10804       }
10805       case TypeAlias:
10806       case TypeDef: {
10807         Diagnosed = ODRDiagTypeDefOrAlias(
10808             FirstRecord, FirstModule, SecondModule,
10809             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10810             FirstDiffType == TypeAlias);
10811         break;
10812       }
10813       case Var: {
10814         Diagnosed =
10815             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10816                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10817         break;
10818       }
10819       case Friend: {
10820         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10821         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10822 
10823         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10824         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10825 
10826         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10827         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10828 
10829         if (FirstND && SecondND) {
10830           ODRDiagDeclError(FirstRecord, FirstModule,
10831                            FirstFriend->getFriendLoc(),
10832                            FirstFriend->getSourceRange(), FriendFunction)
10833               << FirstND;
10834           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10835                           SecondFriend->getSourceRange(), FriendFunction)
10836               << SecondND;
10837 
10838           Diagnosed = true;
10839           break;
10840         }
10841 
10842         if (FirstTSI && SecondTSI) {
10843           QualType FirstFriendType = FirstTSI->getType();
10844           QualType SecondFriendType = SecondTSI->getType();
10845           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10846                  ComputeQualTypeODRHash(SecondFriendType));
10847           ODRDiagDeclError(FirstRecord, FirstModule,
10848                            FirstFriend->getFriendLoc(),
10849                            FirstFriend->getSourceRange(), FriendType)
10850               << FirstFriendType;
10851           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10852                           SecondFriend->getSourceRange(), FriendType)
10853               << SecondFriendType;
10854           Diagnosed = true;
10855           break;
10856         }
10857 
10858         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10859                          FirstFriend->getSourceRange(), FriendTypeFunction)
10860             << (FirstTSI == nullptr);
10861         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10862                         SecondFriend->getSourceRange(), FriendTypeFunction)
10863             << (SecondTSI == nullptr);
10864 
10865         Diagnosed = true;
10866         break;
10867       }
10868       case FunctionTemplate: {
10869         FunctionTemplateDecl *FirstTemplate =
10870             cast<FunctionTemplateDecl>(FirstDecl);
10871         FunctionTemplateDecl *SecondTemplate =
10872             cast<FunctionTemplateDecl>(SecondDecl);
10873 
10874         TemplateParameterList *FirstTPL =
10875             FirstTemplate->getTemplateParameters();
10876         TemplateParameterList *SecondTPL =
10877             SecondTemplate->getTemplateParameters();
10878 
10879         if (FirstTPL->size() != SecondTPL->size()) {
10880           ODRDiagDeclError(FirstRecord, FirstModule,
10881                            FirstTemplate->getLocation(),
10882                            FirstTemplate->getSourceRange(),
10883                            FunctionTemplateDifferentNumberParameters)
10884               << FirstTemplate << FirstTPL->size();
10885           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10886                           SecondTemplate->getSourceRange(),
10887                           FunctionTemplateDifferentNumberParameters)
10888               << SecondTemplate << SecondTPL->size();
10889 
10890           Diagnosed = true;
10891           break;
10892         }
10893 
10894         bool ParameterMismatch = false;
10895         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10896           NamedDecl *FirstParam = FirstTPL->getParam(i);
10897           NamedDecl *SecondParam = SecondTPL->getParam(i);
10898 
10899           if (FirstParam->getKind() != SecondParam->getKind()) {
10900             enum {
10901               TemplateTypeParameter,
10902               NonTypeTemplateParameter,
10903               TemplateTemplateParameter,
10904             };
10905             auto GetParamType = [](NamedDecl *D) {
10906               switch (D->getKind()) {
10907                 default:
10908                   llvm_unreachable("Unexpected template parameter type");
10909                 case Decl::TemplateTypeParm:
10910                   return TemplateTypeParameter;
10911                 case Decl::NonTypeTemplateParm:
10912                   return NonTypeTemplateParameter;
10913                 case Decl::TemplateTemplateParm:
10914                   return TemplateTemplateParameter;
10915               }
10916             };
10917 
10918             ODRDiagDeclError(FirstRecord, FirstModule,
10919                              FirstTemplate->getLocation(),
10920                              FirstTemplate->getSourceRange(),
10921                              FunctionTemplateParameterDifferentKind)
10922                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10923             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10924                             SecondTemplate->getSourceRange(),
10925                             FunctionTemplateParameterDifferentKind)
10926                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10927 
10928             ParameterMismatch = true;
10929             break;
10930           }
10931 
10932           if (FirstParam->getName() != SecondParam->getName()) {
10933             ODRDiagDeclError(
10934                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10935                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10936                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10937                 << FirstParam;
10938             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10939                             SecondTemplate->getSourceRange(),
10940                             FunctionTemplateParameterName)
10941                 << SecondTemplate << (i + 1)
10942                 << (bool)SecondParam->getIdentifier() << SecondParam;
10943             ParameterMismatch = true;
10944             break;
10945           }
10946 
10947           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10948               isa<TemplateTypeParmDecl>(SecondParam)) {
10949             TemplateTypeParmDecl *FirstTTPD =
10950                 cast<TemplateTypeParmDecl>(FirstParam);
10951             TemplateTypeParmDecl *SecondTTPD =
10952                 cast<TemplateTypeParmDecl>(SecondParam);
10953             bool HasFirstDefaultArgument =
10954                 FirstTTPD->hasDefaultArgument() &&
10955                 !FirstTTPD->defaultArgumentWasInherited();
10956             bool HasSecondDefaultArgument =
10957                 SecondTTPD->hasDefaultArgument() &&
10958                 !SecondTTPD->defaultArgumentWasInherited();
10959             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10960               ODRDiagDeclError(FirstRecord, FirstModule,
10961                                FirstTemplate->getLocation(),
10962                                FirstTemplate->getSourceRange(),
10963                                FunctionTemplateParameterSingleDefaultArgument)
10964                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10965               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10966                               SecondTemplate->getSourceRange(),
10967                               FunctionTemplateParameterSingleDefaultArgument)
10968                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
10969               ParameterMismatch = true;
10970               break;
10971             }
10972 
10973             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
10974               QualType FirstType = FirstTTPD->getDefaultArgument();
10975               QualType SecondType = SecondTTPD->getDefaultArgument();
10976               if (ComputeQualTypeODRHash(FirstType) !=
10977                   ComputeQualTypeODRHash(SecondType)) {
10978                 ODRDiagDeclError(
10979                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
10980                     FirstTemplate->getSourceRange(),
10981                     FunctionTemplateParameterDifferentDefaultArgument)
10982                     << FirstTemplate << (i + 1) << FirstType;
10983                 ODRDiagDeclNote(
10984                     SecondModule, SecondTemplate->getLocation(),
10985                     SecondTemplate->getSourceRange(),
10986                     FunctionTemplateParameterDifferentDefaultArgument)
10987                     << SecondTemplate << (i + 1) << SecondType;
10988                 ParameterMismatch = true;
10989                 break;
10990               }
10991             }
10992 
10993             if (FirstTTPD->isParameterPack() !=
10994                 SecondTTPD->isParameterPack()) {
10995               ODRDiagDeclError(FirstRecord, FirstModule,
10996                                FirstTemplate->getLocation(),
10997                                FirstTemplate->getSourceRange(),
10998                                FunctionTemplatePackParameter)
10999                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11000               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11001                               SecondTemplate->getSourceRange(),
11002                               FunctionTemplatePackParameter)
11003                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11004               ParameterMismatch = true;
11005               break;
11006             }
11007           }
11008 
11009           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11010               isa<TemplateTemplateParmDecl>(SecondParam)) {
11011             TemplateTemplateParmDecl *FirstTTPD =
11012                 cast<TemplateTemplateParmDecl>(FirstParam);
11013             TemplateTemplateParmDecl *SecondTTPD =
11014                 cast<TemplateTemplateParmDecl>(SecondParam);
11015 
11016             TemplateParameterList *FirstTPL =
11017                 FirstTTPD->getTemplateParameters();
11018             TemplateParameterList *SecondTPL =
11019                 SecondTTPD->getTemplateParameters();
11020 
11021             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11022                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11023               ODRDiagDeclError(FirstRecord, FirstModule,
11024                                FirstTemplate->getLocation(),
11025                                FirstTemplate->getSourceRange(),
11026                                FunctionTemplateParameterDifferentType)
11027                   << FirstTemplate << (i + 1);
11028               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11029                               SecondTemplate->getSourceRange(),
11030                               FunctionTemplateParameterDifferentType)
11031                   << SecondTemplate << (i + 1);
11032               ParameterMismatch = true;
11033               break;
11034             }
11035 
11036             bool HasFirstDefaultArgument =
11037                 FirstTTPD->hasDefaultArgument() &&
11038                 !FirstTTPD->defaultArgumentWasInherited();
11039             bool HasSecondDefaultArgument =
11040                 SecondTTPD->hasDefaultArgument() &&
11041                 !SecondTTPD->defaultArgumentWasInherited();
11042             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11043               ODRDiagDeclError(FirstRecord, FirstModule,
11044                                FirstTemplate->getLocation(),
11045                                FirstTemplate->getSourceRange(),
11046                                FunctionTemplateParameterSingleDefaultArgument)
11047                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11048               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11049                               SecondTemplate->getSourceRange(),
11050                               FunctionTemplateParameterSingleDefaultArgument)
11051                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11052               ParameterMismatch = true;
11053               break;
11054             }
11055 
11056             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11057               TemplateArgument FirstTA =
11058                   FirstTTPD->getDefaultArgument().getArgument();
11059               TemplateArgument SecondTA =
11060                   SecondTTPD->getDefaultArgument().getArgument();
11061               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11062                   ComputeTemplateArgumentODRHash(SecondTA)) {
11063                 ODRDiagDeclError(
11064                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11065                     FirstTemplate->getSourceRange(),
11066                     FunctionTemplateParameterDifferentDefaultArgument)
11067                     << FirstTemplate << (i + 1) << FirstTA;
11068                 ODRDiagDeclNote(
11069                     SecondModule, SecondTemplate->getLocation(),
11070                     SecondTemplate->getSourceRange(),
11071                     FunctionTemplateParameterDifferentDefaultArgument)
11072                     << SecondTemplate << (i + 1) << SecondTA;
11073                 ParameterMismatch = true;
11074                 break;
11075               }
11076             }
11077 
11078             if (FirstTTPD->isParameterPack() !=
11079                 SecondTTPD->isParameterPack()) {
11080               ODRDiagDeclError(FirstRecord, FirstModule,
11081                                FirstTemplate->getLocation(),
11082                                FirstTemplate->getSourceRange(),
11083                                FunctionTemplatePackParameter)
11084                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11085               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11086                               SecondTemplate->getSourceRange(),
11087                               FunctionTemplatePackParameter)
11088                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11089               ParameterMismatch = true;
11090               break;
11091             }
11092           }
11093 
11094           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11095               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11096             NonTypeTemplateParmDecl *FirstNTTPD =
11097                 cast<NonTypeTemplateParmDecl>(FirstParam);
11098             NonTypeTemplateParmDecl *SecondNTTPD =
11099                 cast<NonTypeTemplateParmDecl>(SecondParam);
11100 
11101             QualType FirstType = FirstNTTPD->getType();
11102             QualType SecondType = SecondNTTPD->getType();
11103             if (ComputeQualTypeODRHash(FirstType) !=
11104                 ComputeQualTypeODRHash(SecondType)) {
11105               ODRDiagDeclError(FirstRecord, FirstModule,
11106                                FirstTemplate->getLocation(),
11107                                FirstTemplate->getSourceRange(),
11108                                FunctionTemplateParameterDifferentType)
11109                   << FirstTemplate << (i + 1);
11110               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11111                               SecondTemplate->getSourceRange(),
11112                               FunctionTemplateParameterDifferentType)
11113                   << SecondTemplate << (i + 1);
11114               ParameterMismatch = true;
11115               break;
11116             }
11117 
11118             bool HasFirstDefaultArgument =
11119                 FirstNTTPD->hasDefaultArgument() &&
11120                 !FirstNTTPD->defaultArgumentWasInherited();
11121             bool HasSecondDefaultArgument =
11122                 SecondNTTPD->hasDefaultArgument() &&
11123                 !SecondNTTPD->defaultArgumentWasInherited();
11124             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11125               ODRDiagDeclError(FirstRecord, FirstModule,
11126                                FirstTemplate->getLocation(),
11127                                FirstTemplate->getSourceRange(),
11128                                FunctionTemplateParameterSingleDefaultArgument)
11129                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11130               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11131                               SecondTemplate->getSourceRange(),
11132                               FunctionTemplateParameterSingleDefaultArgument)
11133                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11134               ParameterMismatch = true;
11135               break;
11136             }
11137 
11138             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11139               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11140               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11141               if (ComputeODRHash(FirstDefaultArgument) !=
11142                   ComputeODRHash(SecondDefaultArgument)) {
11143                 ODRDiagDeclError(
11144                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11145                     FirstTemplate->getSourceRange(),
11146                     FunctionTemplateParameterDifferentDefaultArgument)
11147                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11148                 ODRDiagDeclNote(
11149                     SecondModule, SecondTemplate->getLocation(),
11150                     SecondTemplate->getSourceRange(),
11151                     FunctionTemplateParameterDifferentDefaultArgument)
11152                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11153                 ParameterMismatch = true;
11154                 break;
11155               }
11156             }
11157 
11158             if (FirstNTTPD->isParameterPack() !=
11159                 SecondNTTPD->isParameterPack()) {
11160               ODRDiagDeclError(FirstRecord, FirstModule,
11161                                FirstTemplate->getLocation(),
11162                                FirstTemplate->getSourceRange(),
11163                                FunctionTemplatePackParameter)
11164                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11165               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11166                               SecondTemplate->getSourceRange(),
11167                               FunctionTemplatePackParameter)
11168                   << SecondTemplate << (i + 1)
11169                   << SecondNTTPD->isParameterPack();
11170               ParameterMismatch = true;
11171               break;
11172             }
11173           }
11174         }
11175 
11176         if (ParameterMismatch) {
11177           Diagnosed = true;
11178           break;
11179         }
11180 
11181         break;
11182       }
11183       }
11184 
11185       if (Diagnosed)
11186         continue;
11187 
11188       Diag(FirstDecl->getLocation(),
11189            diag::err_module_odr_violation_mismatch_decl_unknown)
11190           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11191           << FirstDecl->getSourceRange();
11192       Diag(SecondDecl->getLocation(),
11193            diag::note_module_odr_violation_mismatch_decl_unknown)
11194           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11195       Diagnosed = true;
11196     }
11197 
11198     if (!Diagnosed) {
11199       // All definitions are updates to the same declaration. This happens if a
11200       // module instantiates the declaration of a class template specialization
11201       // and two or more other modules instantiate its definition.
11202       //
11203       // FIXME: Indicate which modules had instantiations of this definition.
11204       // FIXME: How can this even happen?
11205       Diag(Merge.first->getLocation(),
11206            diag::err_module_odr_violation_different_instantiations)
11207         << Merge.first;
11208     }
11209   }
11210 
11211   // Issue ODR failures diagnostics for functions.
11212   for (auto &Merge : FunctionOdrMergeFailures) {
11213     enum ODRFunctionDifference {
11214       ReturnType,
11215       ParameterName,
11216       ParameterType,
11217       ParameterSingleDefaultArgument,
11218       ParameterDifferentDefaultArgument,
11219       FunctionBody,
11220     };
11221 
11222     FunctionDecl *FirstFunction = Merge.first;
11223     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11224 
11225     bool Diagnosed = false;
11226     for (auto &SecondFunction : Merge.second) {
11227 
11228       if (FirstFunction == SecondFunction)
11229         continue;
11230 
11231       std::string SecondModule =
11232           getOwningModuleNameForDiagnostic(SecondFunction);
11233 
11234       auto ODRDiagError = [FirstFunction, &FirstModule,
11235                            this](SourceLocation Loc, SourceRange Range,
11236                                  ODRFunctionDifference DiffType) {
11237         return Diag(Loc, diag::err_module_odr_violation_function)
11238                << FirstFunction << FirstModule.empty() << FirstModule << Range
11239                << DiffType;
11240       };
11241       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11242                                                SourceRange Range,
11243                                                ODRFunctionDifference DiffType) {
11244         return Diag(Loc, diag::note_module_odr_violation_function)
11245                << SecondModule << Range << DiffType;
11246       };
11247 
11248       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11249           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11250         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11251                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11252             << FirstFunction->getReturnType();
11253         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11254                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11255             << SecondFunction->getReturnType();
11256         Diagnosed = true;
11257         break;
11258       }
11259 
11260       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11261              "Merged functions with different number of parameters");
11262 
11263       auto ParamSize = FirstFunction->param_size();
11264       bool ParameterMismatch = false;
11265       for (unsigned I = 0; I < ParamSize; ++I) {
11266         auto *FirstParam = FirstFunction->getParamDecl(I);
11267         auto *SecondParam = SecondFunction->getParamDecl(I);
11268 
11269         assert(getContext().hasSameType(FirstParam->getType(),
11270                                       SecondParam->getType()) &&
11271                "Merged function has different parameter types.");
11272 
11273         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11274           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11275                        ParameterName)
11276               << I + 1 << FirstParam->getDeclName();
11277           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11278                       ParameterName)
11279               << I + 1 << SecondParam->getDeclName();
11280           ParameterMismatch = true;
11281           break;
11282         };
11283 
11284         QualType FirstParamType = FirstParam->getType();
11285         QualType SecondParamType = SecondParam->getType();
11286         if (FirstParamType != SecondParamType &&
11287             ComputeQualTypeODRHash(FirstParamType) !=
11288                 ComputeQualTypeODRHash(SecondParamType)) {
11289           if (const DecayedType *ParamDecayedType =
11290                   FirstParamType->getAs<DecayedType>()) {
11291             ODRDiagError(FirstParam->getLocation(),
11292                          FirstParam->getSourceRange(), ParameterType)
11293                 << (I + 1) << FirstParamType << true
11294                 << ParamDecayedType->getOriginalType();
11295           } else {
11296             ODRDiagError(FirstParam->getLocation(),
11297                          FirstParam->getSourceRange(), ParameterType)
11298                 << (I + 1) << FirstParamType << false;
11299           }
11300 
11301           if (const DecayedType *ParamDecayedType =
11302                   SecondParamType->getAs<DecayedType>()) {
11303             ODRDiagNote(SecondParam->getLocation(),
11304                         SecondParam->getSourceRange(), ParameterType)
11305                 << (I + 1) << SecondParamType << true
11306                 << ParamDecayedType->getOriginalType();
11307           } else {
11308             ODRDiagNote(SecondParam->getLocation(),
11309                         SecondParam->getSourceRange(), ParameterType)
11310                 << (I + 1) << SecondParamType << false;
11311           }
11312           ParameterMismatch = true;
11313           break;
11314         }
11315 
11316         const Expr *FirstInit = FirstParam->getInit();
11317         const Expr *SecondInit = SecondParam->getInit();
11318         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11319           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11320                        ParameterSingleDefaultArgument)
11321               << (I + 1) << (FirstInit == nullptr)
11322               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11323           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11324                       ParameterSingleDefaultArgument)
11325               << (I + 1) << (SecondInit == nullptr)
11326               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11327           ParameterMismatch = true;
11328           break;
11329         }
11330 
11331         if (FirstInit && SecondInit &&
11332             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11333           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11334                        ParameterDifferentDefaultArgument)
11335               << (I + 1) << FirstInit->getSourceRange();
11336           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11337                       ParameterDifferentDefaultArgument)
11338               << (I + 1) << SecondInit->getSourceRange();
11339           ParameterMismatch = true;
11340           break;
11341         }
11342 
11343         assert(ComputeSubDeclODRHash(FirstParam) ==
11344                    ComputeSubDeclODRHash(SecondParam) &&
11345                "Undiagnosed parameter difference.");
11346       }
11347 
11348       if (ParameterMismatch) {
11349         Diagnosed = true;
11350         break;
11351       }
11352 
11353       // If no error has been generated before now, assume the problem is in
11354       // the body and generate a message.
11355       ODRDiagError(FirstFunction->getLocation(),
11356                    FirstFunction->getSourceRange(), FunctionBody);
11357       ODRDiagNote(SecondFunction->getLocation(),
11358                   SecondFunction->getSourceRange(), FunctionBody);
11359       Diagnosed = true;
11360       break;
11361     }
11362     (void)Diagnosed;
11363     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11364   }
11365 
11366   // Issue ODR failures diagnostics for enums.
11367   for (auto &Merge : EnumOdrMergeFailures) {
11368     enum ODREnumDifference {
11369       SingleScopedEnum,
11370       EnumTagKeywordMismatch,
11371       SingleSpecifiedType,
11372       DifferentSpecifiedTypes,
11373       DifferentNumberEnumConstants,
11374       EnumConstantName,
11375       EnumConstantSingleInitilizer,
11376       EnumConstantDifferentInitilizer,
11377     };
11378 
11379     // If we've already pointed out a specific problem with this enum, don't
11380     // bother issuing a general "something's different" diagnostic.
11381     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11382       continue;
11383 
11384     EnumDecl *FirstEnum = Merge.first;
11385     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11386 
11387     using DeclHashes =
11388         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11389     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11390                               DeclHashes &Hashes, EnumDecl *Enum) {
11391       for (auto *D : Enum->decls()) {
11392         // Due to decl merging, the first EnumDecl is the parent of
11393         // Decls in both records.
11394         if (!ODRHash::isWhitelistedDecl(D, FirstEnum))
11395           continue;
11396         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11397         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11398                             ComputeSubDeclODRHash(D));
11399       }
11400     };
11401     DeclHashes FirstHashes;
11402     PopulateHashes(FirstHashes, FirstEnum);
11403     bool Diagnosed = false;
11404     for (auto &SecondEnum : Merge.second) {
11405 
11406       if (FirstEnum == SecondEnum)
11407         continue;
11408 
11409       std::string SecondModule =
11410           getOwningModuleNameForDiagnostic(SecondEnum);
11411 
11412       auto ODRDiagError = [FirstEnum, &FirstModule,
11413                            this](SourceLocation Loc, SourceRange Range,
11414                                  ODREnumDifference DiffType) {
11415         return Diag(Loc, diag::err_module_odr_violation_enum)
11416                << FirstEnum << FirstModule.empty() << FirstModule << Range
11417                << DiffType;
11418       };
11419       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11420                                                SourceRange Range,
11421                                                ODREnumDifference DiffType) {
11422         return Diag(Loc, diag::note_module_odr_violation_enum)
11423                << SecondModule << Range << DiffType;
11424       };
11425 
11426       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11427         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11428                      SingleScopedEnum)
11429             << FirstEnum->isScoped();
11430         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11431                     SingleScopedEnum)
11432             << SecondEnum->isScoped();
11433         Diagnosed = true;
11434         continue;
11435       }
11436 
11437       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11438         if (FirstEnum->isScopedUsingClassTag() !=
11439             SecondEnum->isScopedUsingClassTag()) {
11440           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11441                        EnumTagKeywordMismatch)
11442               << FirstEnum->isScopedUsingClassTag();
11443           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11444                       EnumTagKeywordMismatch)
11445               << SecondEnum->isScopedUsingClassTag();
11446           Diagnosed = true;
11447           continue;
11448         }
11449       }
11450 
11451       QualType FirstUnderlyingType =
11452           FirstEnum->getIntegerTypeSourceInfo()
11453               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11454               : QualType();
11455       QualType SecondUnderlyingType =
11456           SecondEnum->getIntegerTypeSourceInfo()
11457               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11458               : QualType();
11459       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11460           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11461                        SingleSpecifiedType)
11462               << !FirstUnderlyingType.isNull();
11463           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11464                       SingleSpecifiedType)
11465               << !SecondUnderlyingType.isNull();
11466           Diagnosed = true;
11467           continue;
11468       }
11469 
11470       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11471         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11472             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11473           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11474                        DifferentSpecifiedTypes)
11475               << FirstUnderlyingType;
11476           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11477                       DifferentSpecifiedTypes)
11478               << SecondUnderlyingType;
11479           Diagnosed = true;
11480           continue;
11481         }
11482       }
11483 
11484       DeclHashes SecondHashes;
11485       PopulateHashes(SecondHashes, SecondEnum);
11486 
11487       if (FirstHashes.size() != SecondHashes.size()) {
11488         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11489                      DifferentNumberEnumConstants)
11490             << (int)FirstHashes.size();
11491         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11492                     DifferentNumberEnumConstants)
11493             << (int)SecondHashes.size();
11494         Diagnosed = true;
11495         continue;
11496       }
11497 
11498       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11499         if (FirstHashes[I].second == SecondHashes[I].second)
11500           continue;
11501         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11502         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11503 
11504         if (FirstEnumConstant->getDeclName() !=
11505             SecondEnumConstant->getDeclName()) {
11506 
11507           ODRDiagError(FirstEnumConstant->getLocation(),
11508                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11509               << I + 1 << FirstEnumConstant;
11510           ODRDiagNote(SecondEnumConstant->getLocation(),
11511                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11512               << I + 1 << SecondEnumConstant;
11513           Diagnosed = true;
11514           break;
11515         }
11516 
11517         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11518         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11519         if (!FirstInit && !SecondInit)
11520           continue;
11521 
11522         if (!FirstInit || !SecondInit) {
11523           ODRDiagError(FirstEnumConstant->getLocation(),
11524                        FirstEnumConstant->getSourceRange(),
11525                        EnumConstantSingleInitilizer)
11526               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11527           ODRDiagNote(SecondEnumConstant->getLocation(),
11528                       SecondEnumConstant->getSourceRange(),
11529                       EnumConstantSingleInitilizer)
11530               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11531           Diagnosed = true;
11532           break;
11533         }
11534 
11535         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11536           ODRDiagError(FirstEnumConstant->getLocation(),
11537                        FirstEnumConstant->getSourceRange(),
11538                        EnumConstantDifferentInitilizer)
11539               << I + 1 << FirstEnumConstant;
11540           ODRDiagNote(SecondEnumConstant->getLocation(),
11541                       SecondEnumConstant->getSourceRange(),
11542                       EnumConstantDifferentInitilizer)
11543               << I + 1 << SecondEnumConstant;
11544           Diagnosed = true;
11545           break;
11546         }
11547       }
11548     }
11549 
11550     (void)Diagnosed;
11551     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11552   }
11553 }
11554 
11555 void ASTReader::StartedDeserializing() {
11556   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11557     ReadTimer->startTimer();
11558 }
11559 
11560 void ASTReader::FinishedDeserializing() {
11561   assert(NumCurrentElementsDeserializing &&
11562          "FinishedDeserializing not paired with StartedDeserializing");
11563   if (NumCurrentElementsDeserializing == 1) {
11564     // We decrease NumCurrentElementsDeserializing only after pending actions
11565     // are finished, to avoid recursively re-calling finishPendingActions().
11566     finishPendingActions();
11567   }
11568   --NumCurrentElementsDeserializing;
11569 
11570   if (NumCurrentElementsDeserializing == 0) {
11571     // Propagate exception specification and deduced type updates along
11572     // redeclaration chains.
11573     //
11574     // We do this now rather than in finishPendingActions because we want to
11575     // be able to walk the complete redeclaration chains of the updated decls.
11576     while (!PendingExceptionSpecUpdates.empty() ||
11577            !PendingDeducedTypeUpdates.empty()) {
11578       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11579       PendingExceptionSpecUpdates.clear();
11580       for (auto Update : ESUpdates) {
11581         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11582         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11583         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11584         if (auto *Listener = getContext().getASTMutationListener())
11585           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11586         for (auto *Redecl : Update.second->redecls())
11587           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11588       }
11589 
11590       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11591       PendingDeducedTypeUpdates.clear();
11592       for (auto Update : DTUpdates) {
11593         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11594         // FIXME: If the return type is already deduced, check that it matches.
11595         getContext().adjustDeducedFunctionResultType(Update.first,
11596                                                      Update.second);
11597       }
11598     }
11599 
11600     if (ReadTimer)
11601       ReadTimer->stopTimer();
11602 
11603     diagnoseOdrViolations();
11604 
11605     // We are not in recursive loading, so it's safe to pass the "interesting"
11606     // decls to the consumer.
11607     if (Consumer)
11608       PassInterestingDeclsToConsumer();
11609   }
11610 }
11611 
11612 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11613   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11614     // Remove any fake results before adding any real ones.
11615     auto It = PendingFakeLookupResults.find(II);
11616     if (It != PendingFakeLookupResults.end()) {
11617       for (auto *ND : It->second)
11618         SemaObj->IdResolver.RemoveDecl(ND);
11619       // FIXME: this works around module+PCH performance issue.
11620       // Rather than erase the result from the map, which is O(n), just clear
11621       // the vector of NamedDecls.
11622       It->second.clear();
11623     }
11624   }
11625 
11626   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11627     SemaObj->TUScope->AddDecl(D);
11628   } else if (SemaObj->TUScope) {
11629     // Adding the decl to IdResolver may have failed because it was already in
11630     // (even though it was not added in scope). If it is already in, make sure
11631     // it gets in the scope as well.
11632     if (std::find(SemaObj->IdResolver.begin(Name),
11633                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11634       SemaObj->TUScope->AddDecl(D);
11635   }
11636 }
11637 
11638 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11639                      ASTContext *Context,
11640                      const PCHContainerReader &PCHContainerRdr,
11641                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11642                      StringRef isysroot, bool DisableValidation,
11643                      bool AllowASTWithCompilerErrors,
11644                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11645                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11646                      std::unique_ptr<llvm::Timer> ReadTimer)
11647     : Listener(DisableValidation
11648                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11649                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11650       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11651       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11652       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11653                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11654       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11655       DisableValidation(DisableValidation),
11656       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11657       AllowConfigurationMismatch(AllowConfigurationMismatch),
11658       ValidateSystemInputs(ValidateSystemInputs),
11659       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11660       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11661   SourceMgr.setExternalSLocEntrySource(this);
11662 
11663   for (const auto &Ext : Extensions) {
11664     auto BlockName = Ext->getExtensionMetadata().BlockName;
11665     auto Known = ModuleFileExtensions.find(BlockName);
11666     if (Known != ModuleFileExtensions.end()) {
11667       Diags.Report(diag::warn_duplicate_module_file_extension)
11668         << BlockName;
11669       continue;
11670     }
11671 
11672     ModuleFileExtensions.insert({BlockName, Ext});
11673   }
11674 }
11675 
11676 ASTReader::~ASTReader() {
11677   if (OwnsDeserializationListener)
11678     delete DeserializationListener;
11679 }
11680 
11681 IdentifierResolver &ASTReader::getIdResolver() {
11682   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11683 }
11684 
11685 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11686                                                unsigned AbbrevID) {
11687   Idx = 0;
11688   Record.clear();
11689   return Cursor.readRecord(AbbrevID, Record);
11690 }
11691 //===----------------------------------------------------------------------===//
11692 //// OMPClauseReader implementation
11693 ////===----------------------------------------------------------------------===//
11694 
11695 // This has to be in namespace clang because it's friended by all
11696 // of the OMP clauses.
11697 namespace clang {
11698 
11699 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11700   ASTRecordReader &Record;
11701   ASTContext &Context;
11702 
11703 public:
11704   OMPClauseReader(ASTRecordReader &Record)
11705       : Record(Record), Context(Record.getContext()) {}
11706 
11707 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11708 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11709   OMPClause *readClause();
11710   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11711   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11712 };
11713 
11714 } // end namespace clang
11715 
11716 OMPClause *ASTRecordReader::readOMPClause() {
11717   return OMPClauseReader(*this).readClause();
11718 }
11719 
11720 OMPClause *OMPClauseReader::readClause() {
11721   OMPClause *C = nullptr;
11722   switch (llvm::omp::Clause(Record.readInt())) {
11723   case llvm::omp::OMPC_if:
11724     C = new (Context) OMPIfClause();
11725     break;
11726   case llvm::omp::OMPC_final:
11727     C = new (Context) OMPFinalClause();
11728     break;
11729   case llvm::omp::OMPC_num_threads:
11730     C = new (Context) OMPNumThreadsClause();
11731     break;
11732   case llvm::omp::OMPC_safelen:
11733     C = new (Context) OMPSafelenClause();
11734     break;
11735   case llvm::omp::OMPC_simdlen:
11736     C = new (Context) OMPSimdlenClause();
11737     break;
11738   case llvm::omp::OMPC_allocator:
11739     C = new (Context) OMPAllocatorClause();
11740     break;
11741   case llvm::omp::OMPC_collapse:
11742     C = new (Context) OMPCollapseClause();
11743     break;
11744   case llvm::omp::OMPC_default:
11745     C = new (Context) OMPDefaultClause();
11746     break;
11747   case llvm::omp::OMPC_proc_bind:
11748     C = new (Context) OMPProcBindClause();
11749     break;
11750   case llvm::omp::OMPC_schedule:
11751     C = new (Context) OMPScheduleClause();
11752     break;
11753   case llvm::omp::OMPC_ordered:
11754     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11755     break;
11756   case llvm::omp::OMPC_nowait:
11757     C = new (Context) OMPNowaitClause();
11758     break;
11759   case llvm::omp::OMPC_untied:
11760     C = new (Context) OMPUntiedClause();
11761     break;
11762   case llvm::omp::OMPC_mergeable:
11763     C = new (Context) OMPMergeableClause();
11764     break;
11765   case llvm::omp::OMPC_read:
11766     C = new (Context) OMPReadClause();
11767     break;
11768   case llvm::omp::OMPC_write:
11769     C = new (Context) OMPWriteClause();
11770     break;
11771   case llvm::omp::OMPC_update:
11772     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11773     break;
11774   case llvm::omp::OMPC_capture:
11775     C = new (Context) OMPCaptureClause();
11776     break;
11777   case llvm::omp::OMPC_seq_cst:
11778     C = new (Context) OMPSeqCstClause();
11779     break;
11780   case llvm::omp::OMPC_acq_rel:
11781     C = new (Context) OMPAcqRelClause();
11782     break;
11783   case llvm::omp::OMPC_acquire:
11784     C = new (Context) OMPAcquireClause();
11785     break;
11786   case llvm::omp::OMPC_release:
11787     C = new (Context) OMPReleaseClause();
11788     break;
11789   case llvm::omp::OMPC_relaxed:
11790     C = new (Context) OMPRelaxedClause();
11791     break;
11792   case llvm::omp::OMPC_threads:
11793     C = new (Context) OMPThreadsClause();
11794     break;
11795   case llvm::omp::OMPC_simd:
11796     C = new (Context) OMPSIMDClause();
11797     break;
11798   case llvm::omp::OMPC_nogroup:
11799     C = new (Context) OMPNogroupClause();
11800     break;
11801   case llvm::omp::OMPC_unified_address:
11802     C = new (Context) OMPUnifiedAddressClause();
11803     break;
11804   case llvm::omp::OMPC_unified_shared_memory:
11805     C = new (Context) OMPUnifiedSharedMemoryClause();
11806     break;
11807   case llvm::omp::OMPC_reverse_offload:
11808     C = new (Context) OMPReverseOffloadClause();
11809     break;
11810   case llvm::omp::OMPC_dynamic_allocators:
11811     C = new (Context) OMPDynamicAllocatorsClause();
11812     break;
11813   case llvm::omp::OMPC_atomic_default_mem_order:
11814     C = new (Context) OMPAtomicDefaultMemOrderClause();
11815     break;
11816  case llvm::omp::OMPC_private:
11817     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11818     break;
11819   case llvm::omp::OMPC_firstprivate:
11820     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11821     break;
11822   case llvm::omp::OMPC_lastprivate:
11823     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11824     break;
11825   case llvm::omp::OMPC_shared:
11826     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11827     break;
11828   case llvm::omp::OMPC_reduction:
11829     C = OMPReductionClause::CreateEmpty(Context, Record.readInt());
11830     break;
11831   case llvm::omp::OMPC_task_reduction:
11832     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11833     break;
11834   case llvm::omp::OMPC_in_reduction:
11835     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11836     break;
11837   case llvm::omp::OMPC_linear:
11838     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11839     break;
11840   case llvm::omp::OMPC_aligned:
11841     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11842     break;
11843   case llvm::omp::OMPC_copyin:
11844     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11845     break;
11846   case llvm::omp::OMPC_copyprivate:
11847     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11848     break;
11849   case llvm::omp::OMPC_flush:
11850     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11851     break;
11852   case llvm::omp::OMPC_depobj:
11853     C = OMPDepobjClause::CreateEmpty(Context);
11854     break;
11855   case llvm::omp::OMPC_depend: {
11856     unsigned NumVars = Record.readInt();
11857     unsigned NumLoops = Record.readInt();
11858     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11859     break;
11860   }
11861   case llvm::omp::OMPC_device:
11862     C = new (Context) OMPDeviceClause();
11863     break;
11864   case llvm::omp::OMPC_map: {
11865     OMPMappableExprListSizeTy Sizes;
11866     Sizes.NumVars = Record.readInt();
11867     Sizes.NumUniqueDeclarations = Record.readInt();
11868     Sizes.NumComponentLists = Record.readInt();
11869     Sizes.NumComponents = Record.readInt();
11870     C = OMPMapClause::CreateEmpty(Context, Sizes);
11871     break;
11872   }
11873   case llvm::omp::OMPC_num_teams:
11874     C = new (Context) OMPNumTeamsClause();
11875     break;
11876   case llvm::omp::OMPC_thread_limit:
11877     C = new (Context) OMPThreadLimitClause();
11878     break;
11879   case llvm::omp::OMPC_priority:
11880     C = new (Context) OMPPriorityClause();
11881     break;
11882   case llvm::omp::OMPC_grainsize:
11883     C = new (Context) OMPGrainsizeClause();
11884     break;
11885   case llvm::omp::OMPC_num_tasks:
11886     C = new (Context) OMPNumTasksClause();
11887     break;
11888   case llvm::omp::OMPC_hint:
11889     C = new (Context) OMPHintClause();
11890     break;
11891   case llvm::omp::OMPC_dist_schedule:
11892     C = new (Context) OMPDistScheduleClause();
11893     break;
11894   case llvm::omp::OMPC_defaultmap:
11895     C = new (Context) OMPDefaultmapClause();
11896     break;
11897   case llvm::omp::OMPC_to: {
11898     OMPMappableExprListSizeTy Sizes;
11899     Sizes.NumVars = Record.readInt();
11900     Sizes.NumUniqueDeclarations = Record.readInt();
11901     Sizes.NumComponentLists = Record.readInt();
11902     Sizes.NumComponents = Record.readInt();
11903     C = OMPToClause::CreateEmpty(Context, Sizes);
11904     break;
11905   }
11906   case llvm::omp::OMPC_from: {
11907     OMPMappableExprListSizeTy Sizes;
11908     Sizes.NumVars = Record.readInt();
11909     Sizes.NumUniqueDeclarations = Record.readInt();
11910     Sizes.NumComponentLists = Record.readInt();
11911     Sizes.NumComponents = Record.readInt();
11912     C = OMPFromClause::CreateEmpty(Context, Sizes);
11913     break;
11914   }
11915   case llvm::omp::OMPC_use_device_ptr: {
11916     OMPMappableExprListSizeTy Sizes;
11917     Sizes.NumVars = Record.readInt();
11918     Sizes.NumUniqueDeclarations = Record.readInt();
11919     Sizes.NumComponentLists = Record.readInt();
11920     Sizes.NumComponents = Record.readInt();
11921     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11922     break;
11923   }
11924   case llvm::omp::OMPC_use_device_addr: {
11925     OMPMappableExprListSizeTy Sizes;
11926     Sizes.NumVars = Record.readInt();
11927     Sizes.NumUniqueDeclarations = Record.readInt();
11928     Sizes.NumComponentLists = Record.readInt();
11929     Sizes.NumComponents = Record.readInt();
11930     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11931     break;
11932   }
11933   case llvm::omp::OMPC_is_device_ptr: {
11934     OMPMappableExprListSizeTy Sizes;
11935     Sizes.NumVars = Record.readInt();
11936     Sizes.NumUniqueDeclarations = Record.readInt();
11937     Sizes.NumComponentLists = Record.readInt();
11938     Sizes.NumComponents = Record.readInt();
11939     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11940     break;
11941   }
11942   case llvm::omp::OMPC_allocate:
11943     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11944     break;
11945   case llvm::omp::OMPC_nontemporal:
11946     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11947     break;
11948   case llvm::omp::OMPC_inclusive:
11949     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11950     break;
11951   case llvm::omp::OMPC_exclusive:
11952     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11953     break;
11954   case llvm::omp::OMPC_order:
11955     C = new (Context) OMPOrderClause();
11956     break;
11957   case llvm::omp::OMPC_destroy:
11958     C = new (Context) OMPDestroyClause();
11959     break;
11960   case llvm::omp::OMPC_detach:
11961     C = new (Context) OMPDetachClause();
11962     break;
11963   case llvm::omp::OMPC_uses_allocators:
11964     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
11965     break;
11966   case llvm::omp::OMPC_affinity:
11967     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
11968     break;
11969 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
11970   case llvm::omp::Enum:                                                        \
11971     break;
11972 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11973   }
11974   assert(C && "Unknown OMPClause type");
11975 
11976   Visit(C);
11977   C->setLocStart(Record.readSourceLocation());
11978   C->setLocEnd(Record.readSourceLocation());
11979 
11980   return C;
11981 }
11982 
11983 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
11984   C->setPreInitStmt(Record.readSubStmt(),
11985                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
11986 }
11987 
11988 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
11989   VisitOMPClauseWithPreInit(C);
11990   C->setPostUpdateExpr(Record.readSubExpr());
11991 }
11992 
11993 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
11994   VisitOMPClauseWithPreInit(C);
11995   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
11996   C->setNameModifierLoc(Record.readSourceLocation());
11997   C->setColonLoc(Record.readSourceLocation());
11998   C->setCondition(Record.readSubExpr());
11999   C->setLParenLoc(Record.readSourceLocation());
12000 }
12001 
12002 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12003   VisitOMPClauseWithPreInit(C);
12004   C->setCondition(Record.readSubExpr());
12005   C->setLParenLoc(Record.readSourceLocation());
12006 }
12007 
12008 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12009   VisitOMPClauseWithPreInit(C);
12010   C->setNumThreads(Record.readSubExpr());
12011   C->setLParenLoc(Record.readSourceLocation());
12012 }
12013 
12014 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12015   C->setSafelen(Record.readSubExpr());
12016   C->setLParenLoc(Record.readSourceLocation());
12017 }
12018 
12019 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12020   C->setSimdlen(Record.readSubExpr());
12021   C->setLParenLoc(Record.readSourceLocation());
12022 }
12023 
12024 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12025   C->setAllocator(Record.readExpr());
12026   C->setLParenLoc(Record.readSourceLocation());
12027 }
12028 
12029 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12030   C->setNumForLoops(Record.readSubExpr());
12031   C->setLParenLoc(Record.readSourceLocation());
12032 }
12033 
12034 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12035   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12036   C->setLParenLoc(Record.readSourceLocation());
12037   C->setDefaultKindKwLoc(Record.readSourceLocation());
12038 }
12039 
12040 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12041   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12042   C->setLParenLoc(Record.readSourceLocation());
12043   C->setProcBindKindKwLoc(Record.readSourceLocation());
12044 }
12045 
12046 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12047   VisitOMPClauseWithPreInit(C);
12048   C->setScheduleKind(
12049        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12050   C->setFirstScheduleModifier(
12051       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12052   C->setSecondScheduleModifier(
12053       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12054   C->setChunkSize(Record.readSubExpr());
12055   C->setLParenLoc(Record.readSourceLocation());
12056   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12057   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12058   C->setScheduleKindLoc(Record.readSourceLocation());
12059   C->setCommaLoc(Record.readSourceLocation());
12060 }
12061 
12062 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12063   C->setNumForLoops(Record.readSubExpr());
12064   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12065     C->setLoopNumIterations(I, Record.readSubExpr());
12066   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12067     C->setLoopCounter(I, Record.readSubExpr());
12068   C->setLParenLoc(Record.readSourceLocation());
12069 }
12070 
12071 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12072   C->setEventHandler(Record.readSubExpr());
12073   C->setLParenLoc(Record.readSourceLocation());
12074 }
12075 
12076 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12077 
12078 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12079 
12080 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12081 
12082 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12083 
12084 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12085 
12086 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12087   if (C->isExtended()) {
12088     C->setLParenLoc(Record.readSourceLocation());
12089     C->setArgumentLoc(Record.readSourceLocation());
12090     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12091   }
12092 }
12093 
12094 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12095 
12096 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12097 
12098 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12099 
12100 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12101 
12102 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12103 
12104 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12105 
12106 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12107 
12108 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12109 
12110 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12111 
12112 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12113 
12114 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12115 
12116 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12117     OMPUnifiedSharedMemoryClause *) {}
12118 
12119 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12120 
12121 void
12122 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12123 }
12124 
12125 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12126     OMPAtomicDefaultMemOrderClause *C) {
12127   C->setAtomicDefaultMemOrderKind(
12128       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12129   C->setLParenLoc(Record.readSourceLocation());
12130   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12131 }
12132 
12133 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12134   C->setLParenLoc(Record.readSourceLocation());
12135   unsigned NumVars = C->varlist_size();
12136   SmallVector<Expr *, 16> Vars;
12137   Vars.reserve(NumVars);
12138   for (unsigned i = 0; i != NumVars; ++i)
12139     Vars.push_back(Record.readSubExpr());
12140   C->setVarRefs(Vars);
12141   Vars.clear();
12142   for (unsigned i = 0; i != NumVars; ++i)
12143     Vars.push_back(Record.readSubExpr());
12144   C->setPrivateCopies(Vars);
12145 }
12146 
12147 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12148   VisitOMPClauseWithPreInit(C);
12149   C->setLParenLoc(Record.readSourceLocation());
12150   unsigned NumVars = C->varlist_size();
12151   SmallVector<Expr *, 16> Vars;
12152   Vars.reserve(NumVars);
12153   for (unsigned i = 0; i != NumVars; ++i)
12154     Vars.push_back(Record.readSubExpr());
12155   C->setVarRefs(Vars);
12156   Vars.clear();
12157   for (unsigned i = 0; i != NumVars; ++i)
12158     Vars.push_back(Record.readSubExpr());
12159   C->setPrivateCopies(Vars);
12160   Vars.clear();
12161   for (unsigned i = 0; i != NumVars; ++i)
12162     Vars.push_back(Record.readSubExpr());
12163   C->setInits(Vars);
12164 }
12165 
12166 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12167   VisitOMPClauseWithPostUpdate(C);
12168   C->setLParenLoc(Record.readSourceLocation());
12169   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12170   C->setKindLoc(Record.readSourceLocation());
12171   C->setColonLoc(Record.readSourceLocation());
12172   unsigned NumVars = C->varlist_size();
12173   SmallVector<Expr *, 16> Vars;
12174   Vars.reserve(NumVars);
12175   for (unsigned i = 0; i != NumVars; ++i)
12176     Vars.push_back(Record.readSubExpr());
12177   C->setVarRefs(Vars);
12178   Vars.clear();
12179   for (unsigned i = 0; i != NumVars; ++i)
12180     Vars.push_back(Record.readSubExpr());
12181   C->setPrivateCopies(Vars);
12182   Vars.clear();
12183   for (unsigned i = 0; i != NumVars; ++i)
12184     Vars.push_back(Record.readSubExpr());
12185   C->setSourceExprs(Vars);
12186   Vars.clear();
12187   for (unsigned i = 0; i != NumVars; ++i)
12188     Vars.push_back(Record.readSubExpr());
12189   C->setDestinationExprs(Vars);
12190   Vars.clear();
12191   for (unsigned i = 0; i != NumVars; ++i)
12192     Vars.push_back(Record.readSubExpr());
12193   C->setAssignmentOps(Vars);
12194 }
12195 
12196 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12197   C->setLParenLoc(Record.readSourceLocation());
12198   unsigned NumVars = C->varlist_size();
12199   SmallVector<Expr *, 16> Vars;
12200   Vars.reserve(NumVars);
12201   for (unsigned i = 0; i != NumVars; ++i)
12202     Vars.push_back(Record.readSubExpr());
12203   C->setVarRefs(Vars);
12204 }
12205 
12206 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12207   VisitOMPClauseWithPostUpdate(C);
12208   C->setLParenLoc(Record.readSourceLocation());
12209   C->setModifierLoc(Record.readSourceLocation());
12210   C->setColonLoc(Record.readSourceLocation());
12211   C->setModifier(Record.readEnum<OpenMPReductionClauseModifier>());
12212   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12213   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12214   C->setQualifierLoc(NNSL);
12215   C->setNameInfo(DNI);
12216 
12217   unsigned NumVars = C->varlist_size();
12218   SmallVector<Expr *, 16> Vars;
12219   Vars.reserve(NumVars);
12220   for (unsigned i = 0; i != NumVars; ++i)
12221     Vars.push_back(Record.readSubExpr());
12222   C->setVarRefs(Vars);
12223   Vars.clear();
12224   for (unsigned i = 0; i != NumVars; ++i)
12225     Vars.push_back(Record.readSubExpr());
12226   C->setPrivates(Vars);
12227   Vars.clear();
12228   for (unsigned i = 0; i != NumVars; ++i)
12229     Vars.push_back(Record.readSubExpr());
12230   C->setLHSExprs(Vars);
12231   Vars.clear();
12232   for (unsigned i = 0; i != NumVars; ++i)
12233     Vars.push_back(Record.readSubExpr());
12234   C->setRHSExprs(Vars);
12235   Vars.clear();
12236   for (unsigned i = 0; i != NumVars; ++i)
12237     Vars.push_back(Record.readSubExpr());
12238   C->setReductionOps(Vars);
12239 }
12240 
12241 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12242   VisitOMPClauseWithPostUpdate(C);
12243   C->setLParenLoc(Record.readSourceLocation());
12244   C->setColonLoc(Record.readSourceLocation());
12245   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12246   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12247   C->setQualifierLoc(NNSL);
12248   C->setNameInfo(DNI);
12249 
12250   unsigned NumVars = C->varlist_size();
12251   SmallVector<Expr *, 16> Vars;
12252   Vars.reserve(NumVars);
12253   for (unsigned I = 0; I != NumVars; ++I)
12254     Vars.push_back(Record.readSubExpr());
12255   C->setVarRefs(Vars);
12256   Vars.clear();
12257   for (unsigned I = 0; I != NumVars; ++I)
12258     Vars.push_back(Record.readSubExpr());
12259   C->setPrivates(Vars);
12260   Vars.clear();
12261   for (unsigned I = 0; I != NumVars; ++I)
12262     Vars.push_back(Record.readSubExpr());
12263   C->setLHSExprs(Vars);
12264   Vars.clear();
12265   for (unsigned I = 0; I != NumVars; ++I)
12266     Vars.push_back(Record.readSubExpr());
12267   C->setRHSExprs(Vars);
12268   Vars.clear();
12269   for (unsigned I = 0; I != NumVars; ++I)
12270     Vars.push_back(Record.readSubExpr());
12271   C->setReductionOps(Vars);
12272 }
12273 
12274 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12275   VisitOMPClauseWithPostUpdate(C);
12276   C->setLParenLoc(Record.readSourceLocation());
12277   C->setColonLoc(Record.readSourceLocation());
12278   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12279   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12280   C->setQualifierLoc(NNSL);
12281   C->setNameInfo(DNI);
12282 
12283   unsigned NumVars = C->varlist_size();
12284   SmallVector<Expr *, 16> Vars;
12285   Vars.reserve(NumVars);
12286   for (unsigned I = 0; I != NumVars; ++I)
12287     Vars.push_back(Record.readSubExpr());
12288   C->setVarRefs(Vars);
12289   Vars.clear();
12290   for (unsigned I = 0; I != NumVars; ++I)
12291     Vars.push_back(Record.readSubExpr());
12292   C->setPrivates(Vars);
12293   Vars.clear();
12294   for (unsigned I = 0; I != NumVars; ++I)
12295     Vars.push_back(Record.readSubExpr());
12296   C->setLHSExprs(Vars);
12297   Vars.clear();
12298   for (unsigned I = 0; I != NumVars; ++I)
12299     Vars.push_back(Record.readSubExpr());
12300   C->setRHSExprs(Vars);
12301   Vars.clear();
12302   for (unsigned I = 0; I != NumVars; ++I)
12303     Vars.push_back(Record.readSubExpr());
12304   C->setReductionOps(Vars);
12305   Vars.clear();
12306   for (unsigned I = 0; I != NumVars; ++I)
12307     Vars.push_back(Record.readSubExpr());
12308   C->setTaskgroupDescriptors(Vars);
12309 }
12310 
12311 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12312   VisitOMPClauseWithPostUpdate(C);
12313   C->setLParenLoc(Record.readSourceLocation());
12314   C->setColonLoc(Record.readSourceLocation());
12315   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12316   C->setModifierLoc(Record.readSourceLocation());
12317   unsigned NumVars = C->varlist_size();
12318   SmallVector<Expr *, 16> Vars;
12319   Vars.reserve(NumVars);
12320   for (unsigned i = 0; i != NumVars; ++i)
12321     Vars.push_back(Record.readSubExpr());
12322   C->setVarRefs(Vars);
12323   Vars.clear();
12324   for (unsigned i = 0; i != NumVars; ++i)
12325     Vars.push_back(Record.readSubExpr());
12326   C->setPrivates(Vars);
12327   Vars.clear();
12328   for (unsigned i = 0; i != NumVars; ++i)
12329     Vars.push_back(Record.readSubExpr());
12330   C->setInits(Vars);
12331   Vars.clear();
12332   for (unsigned i = 0; i != NumVars; ++i)
12333     Vars.push_back(Record.readSubExpr());
12334   C->setUpdates(Vars);
12335   Vars.clear();
12336   for (unsigned i = 0; i != NumVars; ++i)
12337     Vars.push_back(Record.readSubExpr());
12338   C->setFinals(Vars);
12339   C->setStep(Record.readSubExpr());
12340   C->setCalcStep(Record.readSubExpr());
12341   Vars.clear();
12342   for (unsigned I = 0; I != NumVars + 1; ++I)
12343     Vars.push_back(Record.readSubExpr());
12344   C->setUsedExprs(Vars);
12345 }
12346 
12347 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12348   C->setLParenLoc(Record.readSourceLocation());
12349   C->setColonLoc(Record.readSourceLocation());
12350   unsigned NumVars = C->varlist_size();
12351   SmallVector<Expr *, 16> Vars;
12352   Vars.reserve(NumVars);
12353   for (unsigned i = 0; i != NumVars; ++i)
12354     Vars.push_back(Record.readSubExpr());
12355   C->setVarRefs(Vars);
12356   C->setAlignment(Record.readSubExpr());
12357 }
12358 
12359 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12360   C->setLParenLoc(Record.readSourceLocation());
12361   unsigned NumVars = C->varlist_size();
12362   SmallVector<Expr *, 16> Exprs;
12363   Exprs.reserve(NumVars);
12364   for (unsigned i = 0; i != NumVars; ++i)
12365     Exprs.push_back(Record.readSubExpr());
12366   C->setVarRefs(Exprs);
12367   Exprs.clear();
12368   for (unsigned i = 0; i != NumVars; ++i)
12369     Exprs.push_back(Record.readSubExpr());
12370   C->setSourceExprs(Exprs);
12371   Exprs.clear();
12372   for (unsigned i = 0; i != NumVars; ++i)
12373     Exprs.push_back(Record.readSubExpr());
12374   C->setDestinationExprs(Exprs);
12375   Exprs.clear();
12376   for (unsigned i = 0; i != NumVars; ++i)
12377     Exprs.push_back(Record.readSubExpr());
12378   C->setAssignmentOps(Exprs);
12379 }
12380 
12381 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12382   C->setLParenLoc(Record.readSourceLocation());
12383   unsigned NumVars = C->varlist_size();
12384   SmallVector<Expr *, 16> Exprs;
12385   Exprs.reserve(NumVars);
12386   for (unsigned i = 0; i != NumVars; ++i)
12387     Exprs.push_back(Record.readSubExpr());
12388   C->setVarRefs(Exprs);
12389   Exprs.clear();
12390   for (unsigned i = 0; i != NumVars; ++i)
12391     Exprs.push_back(Record.readSubExpr());
12392   C->setSourceExprs(Exprs);
12393   Exprs.clear();
12394   for (unsigned i = 0; i != NumVars; ++i)
12395     Exprs.push_back(Record.readSubExpr());
12396   C->setDestinationExprs(Exprs);
12397   Exprs.clear();
12398   for (unsigned i = 0; i != NumVars; ++i)
12399     Exprs.push_back(Record.readSubExpr());
12400   C->setAssignmentOps(Exprs);
12401 }
12402 
12403 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12404   C->setLParenLoc(Record.readSourceLocation());
12405   unsigned NumVars = C->varlist_size();
12406   SmallVector<Expr *, 16> Vars;
12407   Vars.reserve(NumVars);
12408   for (unsigned i = 0; i != NumVars; ++i)
12409     Vars.push_back(Record.readSubExpr());
12410   C->setVarRefs(Vars);
12411 }
12412 
12413 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12414   C->setDepobj(Record.readSubExpr());
12415   C->setLParenLoc(Record.readSourceLocation());
12416 }
12417 
12418 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12419   C->setLParenLoc(Record.readSourceLocation());
12420   C->setModifier(Record.readSubExpr());
12421   C->setDependencyKind(
12422       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12423   C->setDependencyLoc(Record.readSourceLocation());
12424   C->setColonLoc(Record.readSourceLocation());
12425   unsigned NumVars = C->varlist_size();
12426   SmallVector<Expr *, 16> Vars;
12427   Vars.reserve(NumVars);
12428   for (unsigned I = 0; I != NumVars; ++I)
12429     Vars.push_back(Record.readSubExpr());
12430   C->setVarRefs(Vars);
12431   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12432     C->setLoopData(I, Record.readSubExpr());
12433 }
12434 
12435 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12436   VisitOMPClauseWithPreInit(C);
12437   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12438   C->setDevice(Record.readSubExpr());
12439   C->setModifierLoc(Record.readSourceLocation());
12440   C->setLParenLoc(Record.readSourceLocation());
12441 }
12442 
12443 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12444   C->setLParenLoc(Record.readSourceLocation());
12445   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12446     C->setMapTypeModifier(
12447         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12448     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12449   }
12450   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12451   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12452   C->setMapType(
12453      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12454   C->setMapLoc(Record.readSourceLocation());
12455   C->setColonLoc(Record.readSourceLocation());
12456   auto NumVars = C->varlist_size();
12457   auto UniqueDecls = C->getUniqueDeclarationsNum();
12458   auto TotalLists = C->getTotalComponentListNum();
12459   auto TotalComponents = C->getTotalComponentsNum();
12460 
12461   SmallVector<Expr *, 16> Vars;
12462   Vars.reserve(NumVars);
12463   for (unsigned i = 0; i != NumVars; ++i)
12464     Vars.push_back(Record.readExpr());
12465   C->setVarRefs(Vars);
12466 
12467   SmallVector<Expr *, 16> UDMappers;
12468   UDMappers.reserve(NumVars);
12469   for (unsigned I = 0; I < NumVars; ++I)
12470     UDMappers.push_back(Record.readExpr());
12471   C->setUDMapperRefs(UDMappers);
12472 
12473   SmallVector<ValueDecl *, 16> Decls;
12474   Decls.reserve(UniqueDecls);
12475   for (unsigned i = 0; i < UniqueDecls; ++i)
12476     Decls.push_back(Record.readDeclAs<ValueDecl>());
12477   C->setUniqueDecls(Decls);
12478 
12479   SmallVector<unsigned, 16> ListsPerDecl;
12480   ListsPerDecl.reserve(UniqueDecls);
12481   for (unsigned i = 0; i < UniqueDecls; ++i)
12482     ListsPerDecl.push_back(Record.readInt());
12483   C->setDeclNumLists(ListsPerDecl);
12484 
12485   SmallVector<unsigned, 32> ListSizes;
12486   ListSizes.reserve(TotalLists);
12487   for (unsigned i = 0; i < TotalLists; ++i)
12488     ListSizes.push_back(Record.readInt());
12489   C->setComponentListSizes(ListSizes);
12490 
12491   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12492   Components.reserve(TotalComponents);
12493   for (unsigned i = 0; i < TotalComponents; ++i) {
12494     Expr *AssociatedExpr = Record.readExpr();
12495     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12496     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12497         AssociatedExpr, AssociatedDecl));
12498   }
12499   C->setComponents(Components, ListSizes);
12500 }
12501 
12502 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12503   C->setLParenLoc(Record.readSourceLocation());
12504   C->setColonLoc(Record.readSourceLocation());
12505   C->setAllocator(Record.readSubExpr());
12506   unsigned NumVars = C->varlist_size();
12507   SmallVector<Expr *, 16> Vars;
12508   Vars.reserve(NumVars);
12509   for (unsigned i = 0; i != NumVars; ++i)
12510     Vars.push_back(Record.readSubExpr());
12511   C->setVarRefs(Vars);
12512 }
12513 
12514 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12515   VisitOMPClauseWithPreInit(C);
12516   C->setNumTeams(Record.readSubExpr());
12517   C->setLParenLoc(Record.readSourceLocation());
12518 }
12519 
12520 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12521   VisitOMPClauseWithPreInit(C);
12522   C->setThreadLimit(Record.readSubExpr());
12523   C->setLParenLoc(Record.readSourceLocation());
12524 }
12525 
12526 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12527   VisitOMPClauseWithPreInit(C);
12528   C->setPriority(Record.readSubExpr());
12529   C->setLParenLoc(Record.readSourceLocation());
12530 }
12531 
12532 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12533   VisitOMPClauseWithPreInit(C);
12534   C->setGrainsize(Record.readSubExpr());
12535   C->setLParenLoc(Record.readSourceLocation());
12536 }
12537 
12538 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12539   VisitOMPClauseWithPreInit(C);
12540   C->setNumTasks(Record.readSubExpr());
12541   C->setLParenLoc(Record.readSourceLocation());
12542 }
12543 
12544 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12545   C->setHint(Record.readSubExpr());
12546   C->setLParenLoc(Record.readSourceLocation());
12547 }
12548 
12549 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12550   VisitOMPClauseWithPreInit(C);
12551   C->setDistScheduleKind(
12552       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12553   C->setChunkSize(Record.readSubExpr());
12554   C->setLParenLoc(Record.readSourceLocation());
12555   C->setDistScheduleKindLoc(Record.readSourceLocation());
12556   C->setCommaLoc(Record.readSourceLocation());
12557 }
12558 
12559 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12560   C->setDefaultmapKind(
12561        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12562   C->setDefaultmapModifier(
12563       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12564   C->setLParenLoc(Record.readSourceLocation());
12565   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12566   C->setDefaultmapKindLoc(Record.readSourceLocation());
12567 }
12568 
12569 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12570   C->setLParenLoc(Record.readSourceLocation());
12571   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12572   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12573   auto NumVars = C->varlist_size();
12574   auto UniqueDecls = C->getUniqueDeclarationsNum();
12575   auto TotalLists = C->getTotalComponentListNum();
12576   auto TotalComponents = C->getTotalComponentsNum();
12577 
12578   SmallVector<Expr *, 16> Vars;
12579   Vars.reserve(NumVars);
12580   for (unsigned i = 0; i != NumVars; ++i)
12581     Vars.push_back(Record.readSubExpr());
12582   C->setVarRefs(Vars);
12583 
12584   SmallVector<Expr *, 16> UDMappers;
12585   UDMappers.reserve(NumVars);
12586   for (unsigned I = 0; I < NumVars; ++I)
12587     UDMappers.push_back(Record.readSubExpr());
12588   C->setUDMapperRefs(UDMappers);
12589 
12590   SmallVector<ValueDecl *, 16> Decls;
12591   Decls.reserve(UniqueDecls);
12592   for (unsigned i = 0; i < UniqueDecls; ++i)
12593     Decls.push_back(Record.readDeclAs<ValueDecl>());
12594   C->setUniqueDecls(Decls);
12595 
12596   SmallVector<unsigned, 16> ListsPerDecl;
12597   ListsPerDecl.reserve(UniqueDecls);
12598   for (unsigned i = 0; i < UniqueDecls; ++i)
12599     ListsPerDecl.push_back(Record.readInt());
12600   C->setDeclNumLists(ListsPerDecl);
12601 
12602   SmallVector<unsigned, 32> ListSizes;
12603   ListSizes.reserve(TotalLists);
12604   for (unsigned i = 0; i < TotalLists; ++i)
12605     ListSizes.push_back(Record.readInt());
12606   C->setComponentListSizes(ListSizes);
12607 
12608   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12609   Components.reserve(TotalComponents);
12610   for (unsigned i = 0; i < TotalComponents; ++i) {
12611     Expr *AssociatedExpr = Record.readSubExpr();
12612     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12613     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12614         AssociatedExpr, AssociatedDecl));
12615   }
12616   C->setComponents(Components, ListSizes);
12617 }
12618 
12619 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12620   C->setLParenLoc(Record.readSourceLocation());
12621   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12622   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12623   auto NumVars = C->varlist_size();
12624   auto UniqueDecls = C->getUniqueDeclarationsNum();
12625   auto TotalLists = C->getTotalComponentListNum();
12626   auto TotalComponents = C->getTotalComponentsNum();
12627 
12628   SmallVector<Expr *, 16> Vars;
12629   Vars.reserve(NumVars);
12630   for (unsigned i = 0; i != NumVars; ++i)
12631     Vars.push_back(Record.readSubExpr());
12632   C->setVarRefs(Vars);
12633 
12634   SmallVector<Expr *, 16> UDMappers;
12635   UDMappers.reserve(NumVars);
12636   for (unsigned I = 0; I < NumVars; ++I)
12637     UDMappers.push_back(Record.readSubExpr());
12638   C->setUDMapperRefs(UDMappers);
12639 
12640   SmallVector<ValueDecl *, 16> Decls;
12641   Decls.reserve(UniqueDecls);
12642   for (unsigned i = 0; i < UniqueDecls; ++i)
12643     Decls.push_back(Record.readDeclAs<ValueDecl>());
12644   C->setUniqueDecls(Decls);
12645 
12646   SmallVector<unsigned, 16> ListsPerDecl;
12647   ListsPerDecl.reserve(UniqueDecls);
12648   for (unsigned i = 0; i < UniqueDecls; ++i)
12649     ListsPerDecl.push_back(Record.readInt());
12650   C->setDeclNumLists(ListsPerDecl);
12651 
12652   SmallVector<unsigned, 32> ListSizes;
12653   ListSizes.reserve(TotalLists);
12654   for (unsigned i = 0; i < TotalLists; ++i)
12655     ListSizes.push_back(Record.readInt());
12656   C->setComponentListSizes(ListSizes);
12657 
12658   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12659   Components.reserve(TotalComponents);
12660   for (unsigned i = 0; i < TotalComponents; ++i) {
12661     Expr *AssociatedExpr = Record.readSubExpr();
12662     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12663     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12664         AssociatedExpr, AssociatedDecl));
12665   }
12666   C->setComponents(Components, ListSizes);
12667 }
12668 
12669 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12670   C->setLParenLoc(Record.readSourceLocation());
12671   auto NumVars = C->varlist_size();
12672   auto UniqueDecls = C->getUniqueDeclarationsNum();
12673   auto TotalLists = C->getTotalComponentListNum();
12674   auto TotalComponents = C->getTotalComponentsNum();
12675 
12676   SmallVector<Expr *, 16> Vars;
12677   Vars.reserve(NumVars);
12678   for (unsigned i = 0; i != NumVars; ++i)
12679     Vars.push_back(Record.readSubExpr());
12680   C->setVarRefs(Vars);
12681   Vars.clear();
12682   for (unsigned i = 0; i != NumVars; ++i)
12683     Vars.push_back(Record.readSubExpr());
12684   C->setPrivateCopies(Vars);
12685   Vars.clear();
12686   for (unsigned i = 0; i != NumVars; ++i)
12687     Vars.push_back(Record.readSubExpr());
12688   C->setInits(Vars);
12689 
12690   SmallVector<ValueDecl *, 16> Decls;
12691   Decls.reserve(UniqueDecls);
12692   for (unsigned i = 0; i < UniqueDecls; ++i)
12693     Decls.push_back(Record.readDeclAs<ValueDecl>());
12694   C->setUniqueDecls(Decls);
12695 
12696   SmallVector<unsigned, 16> ListsPerDecl;
12697   ListsPerDecl.reserve(UniqueDecls);
12698   for (unsigned i = 0; i < UniqueDecls; ++i)
12699     ListsPerDecl.push_back(Record.readInt());
12700   C->setDeclNumLists(ListsPerDecl);
12701 
12702   SmallVector<unsigned, 32> ListSizes;
12703   ListSizes.reserve(TotalLists);
12704   for (unsigned i = 0; i < TotalLists; ++i)
12705     ListSizes.push_back(Record.readInt());
12706   C->setComponentListSizes(ListSizes);
12707 
12708   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12709   Components.reserve(TotalComponents);
12710   for (unsigned i = 0; i < TotalComponents; ++i) {
12711     Expr *AssociatedExpr = Record.readSubExpr();
12712     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12713     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12714         AssociatedExpr, AssociatedDecl));
12715   }
12716   C->setComponents(Components, ListSizes);
12717 }
12718 
12719 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12720   C->setLParenLoc(Record.readSourceLocation());
12721   auto NumVars = C->varlist_size();
12722   auto UniqueDecls = C->getUniqueDeclarationsNum();
12723   auto TotalLists = C->getTotalComponentListNum();
12724   auto TotalComponents = C->getTotalComponentsNum();
12725 
12726   SmallVector<Expr *, 16> Vars;
12727   Vars.reserve(NumVars);
12728   for (unsigned i = 0; i != NumVars; ++i)
12729     Vars.push_back(Record.readSubExpr());
12730   C->setVarRefs(Vars);
12731 
12732   SmallVector<ValueDecl *, 16> Decls;
12733   Decls.reserve(UniqueDecls);
12734   for (unsigned i = 0; i < UniqueDecls; ++i)
12735     Decls.push_back(Record.readDeclAs<ValueDecl>());
12736   C->setUniqueDecls(Decls);
12737 
12738   SmallVector<unsigned, 16> ListsPerDecl;
12739   ListsPerDecl.reserve(UniqueDecls);
12740   for (unsigned i = 0; i < UniqueDecls; ++i)
12741     ListsPerDecl.push_back(Record.readInt());
12742   C->setDeclNumLists(ListsPerDecl);
12743 
12744   SmallVector<unsigned, 32> ListSizes;
12745   ListSizes.reserve(TotalLists);
12746   for (unsigned i = 0; i < TotalLists; ++i)
12747     ListSizes.push_back(Record.readInt());
12748   C->setComponentListSizes(ListSizes);
12749 
12750   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12751   Components.reserve(TotalComponents);
12752   for (unsigned i = 0; i < TotalComponents; ++i) {
12753     Expr *AssociatedExpr = Record.readSubExpr();
12754     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12755     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12756         AssociatedExpr, AssociatedDecl));
12757   }
12758   C->setComponents(Components, ListSizes);
12759 }
12760 
12761 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12762   C->setLParenLoc(Record.readSourceLocation());
12763   auto NumVars = C->varlist_size();
12764   auto UniqueDecls = C->getUniqueDeclarationsNum();
12765   auto TotalLists = C->getTotalComponentListNum();
12766   auto TotalComponents = C->getTotalComponentsNum();
12767 
12768   SmallVector<Expr *, 16> Vars;
12769   Vars.reserve(NumVars);
12770   for (unsigned i = 0; i != NumVars; ++i)
12771     Vars.push_back(Record.readSubExpr());
12772   C->setVarRefs(Vars);
12773   Vars.clear();
12774 
12775   SmallVector<ValueDecl *, 16> Decls;
12776   Decls.reserve(UniqueDecls);
12777   for (unsigned i = 0; i < UniqueDecls; ++i)
12778     Decls.push_back(Record.readDeclAs<ValueDecl>());
12779   C->setUniqueDecls(Decls);
12780 
12781   SmallVector<unsigned, 16> ListsPerDecl;
12782   ListsPerDecl.reserve(UniqueDecls);
12783   for (unsigned i = 0; i < UniqueDecls; ++i)
12784     ListsPerDecl.push_back(Record.readInt());
12785   C->setDeclNumLists(ListsPerDecl);
12786 
12787   SmallVector<unsigned, 32> ListSizes;
12788   ListSizes.reserve(TotalLists);
12789   for (unsigned i = 0; i < TotalLists; ++i)
12790     ListSizes.push_back(Record.readInt());
12791   C->setComponentListSizes(ListSizes);
12792 
12793   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12794   Components.reserve(TotalComponents);
12795   for (unsigned i = 0; i < TotalComponents; ++i) {
12796     Expr *AssociatedExpr = Record.readSubExpr();
12797     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12798     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12799         AssociatedExpr, AssociatedDecl));
12800   }
12801   C->setComponents(Components, ListSizes);
12802 }
12803 
12804 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12805   C->setLParenLoc(Record.readSourceLocation());
12806   unsigned NumVars = C->varlist_size();
12807   SmallVector<Expr *, 16> Vars;
12808   Vars.reserve(NumVars);
12809   for (unsigned i = 0; i != NumVars; ++i)
12810     Vars.push_back(Record.readSubExpr());
12811   C->setVarRefs(Vars);
12812   Vars.clear();
12813   Vars.reserve(NumVars);
12814   for (unsigned i = 0; i != NumVars; ++i)
12815     Vars.push_back(Record.readSubExpr());
12816   C->setPrivateRefs(Vars);
12817 }
12818 
12819 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12820   C->setLParenLoc(Record.readSourceLocation());
12821   unsigned NumVars = C->varlist_size();
12822   SmallVector<Expr *, 16> Vars;
12823   Vars.reserve(NumVars);
12824   for (unsigned i = 0; i != NumVars; ++i)
12825     Vars.push_back(Record.readSubExpr());
12826   C->setVarRefs(Vars);
12827 }
12828 
12829 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12830   C->setLParenLoc(Record.readSourceLocation());
12831   unsigned NumVars = C->varlist_size();
12832   SmallVector<Expr *, 16> Vars;
12833   Vars.reserve(NumVars);
12834   for (unsigned i = 0; i != NumVars; ++i)
12835     Vars.push_back(Record.readSubExpr());
12836   C->setVarRefs(Vars);
12837 }
12838 
12839 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12840   C->setLParenLoc(Record.readSourceLocation());
12841   unsigned NumOfAllocators = C->getNumberOfAllocators();
12842   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12843   Data.reserve(NumOfAllocators);
12844   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12845     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12846     D.Allocator = Record.readSubExpr();
12847     D.AllocatorTraits = Record.readSubExpr();
12848     D.LParenLoc = Record.readSourceLocation();
12849     D.RParenLoc = Record.readSourceLocation();
12850   }
12851   C->setAllocatorsData(Data);
12852 }
12853 
12854 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12855   C->setLParenLoc(Record.readSourceLocation());
12856   C->setModifier(Record.readSubExpr());
12857   C->setColonLoc(Record.readSourceLocation());
12858   unsigned NumOfLocators = C->varlist_size();
12859   SmallVector<Expr *, 4> Locators;
12860   Locators.reserve(NumOfLocators);
12861   for (unsigned I = 0; I != NumOfLocators; ++I)
12862     Locators.push_back(Record.readSubExpr());
12863   C->setVarRefs(Locators);
12864 }
12865 
12866 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12867   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12868   C->setLParenLoc(Record.readSourceLocation());
12869   C->setKindKwLoc(Record.readSourceLocation());
12870 }
12871 
12872 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12873   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12874   TI.Sets.resize(readUInt32());
12875   for (auto &Set : TI.Sets) {
12876     Set.Kind = readEnum<llvm::omp::TraitSet>();
12877     Set.Selectors.resize(readUInt32());
12878     for (auto &Selector : Set.Selectors) {
12879       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12880       Selector.ScoreOrCondition = nullptr;
12881       if (readBool())
12882         Selector.ScoreOrCondition = readExprRef();
12883       Selector.Properties.resize(readUInt32());
12884       for (auto &Property : Selector.Properties)
12885         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12886     }
12887   }
12888   return &TI;
12889 }
12890