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     CHECK_TARGET_OPT(TuneCPU, "tune CPU");
396   }
397 
398 #undef CHECK_TARGET_OPT
399 
400   // Compare feature sets.
401   SmallVector<StringRef, 4> ExistingFeatures(
402                                              ExistingTargetOpts.FeaturesAsWritten.begin(),
403                                              ExistingTargetOpts.FeaturesAsWritten.end());
404   SmallVector<StringRef, 4> ReadFeatures(TargetOpts.FeaturesAsWritten.begin(),
405                                          TargetOpts.FeaturesAsWritten.end());
406   llvm::sort(ExistingFeatures);
407   llvm::sort(ReadFeatures);
408 
409   // We compute the set difference in both directions explicitly so that we can
410   // diagnose the differences differently.
411   SmallVector<StringRef, 4> UnmatchedExistingFeatures, UnmatchedReadFeatures;
412   std::set_difference(
413       ExistingFeatures.begin(), ExistingFeatures.end(), ReadFeatures.begin(),
414       ReadFeatures.end(), std::back_inserter(UnmatchedExistingFeatures));
415   std::set_difference(ReadFeatures.begin(), ReadFeatures.end(),
416                       ExistingFeatures.begin(), ExistingFeatures.end(),
417                       std::back_inserter(UnmatchedReadFeatures));
418 
419   // If we are allowing compatible differences and the read feature set is
420   // a strict subset of the existing feature set, there is nothing to diagnose.
421   if (AllowCompatibleDifferences && UnmatchedReadFeatures.empty())
422     return false;
423 
424   if (Diags) {
425     for (StringRef Feature : UnmatchedReadFeatures)
426       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
427           << /* is-existing-feature */ false << Feature;
428     for (StringRef Feature : UnmatchedExistingFeatures)
429       Diags->Report(diag::err_pch_targetopt_feature_mismatch)
430           << /* is-existing-feature */ true << Feature;
431   }
432 
433   return !UnmatchedReadFeatures.empty() || !UnmatchedExistingFeatures.empty();
434 }
435 
436 bool
437 PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts,
438                                   bool Complain,
439                                   bool AllowCompatibleDifferences) {
440   const LangOptions &ExistingLangOpts = PP.getLangOpts();
441   return checkLanguageOptions(LangOpts, ExistingLangOpts,
442                               Complain ? &Reader.Diags : nullptr,
443                               AllowCompatibleDifferences);
444 }
445 
446 bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts,
447                                      bool Complain,
448                                      bool AllowCompatibleDifferences) {
449   const TargetOptions &ExistingTargetOpts = PP.getTargetInfo().getTargetOpts();
450   return checkTargetOptions(TargetOpts, ExistingTargetOpts,
451                             Complain ? &Reader.Diags : nullptr,
452                             AllowCompatibleDifferences);
453 }
454 
455 namespace {
456 
457 using MacroDefinitionsMap =
458     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>;
459 using DeclsMap = llvm::DenseMap<DeclarationName, SmallVector<NamedDecl *, 8>>;
460 
461 } // namespace
462 
463 static bool checkDiagnosticGroupMappings(DiagnosticsEngine &StoredDiags,
464                                          DiagnosticsEngine &Diags,
465                                          bool Complain) {
466   using Level = DiagnosticsEngine::Level;
467 
468   // Check current mappings for new -Werror mappings, and the stored mappings
469   // for cases that were explicitly mapped to *not* be errors that are now
470   // errors because of options like -Werror.
471   DiagnosticsEngine *MappingSources[] = { &Diags, &StoredDiags };
472 
473   for (DiagnosticsEngine *MappingSource : MappingSources) {
474     for (auto DiagIDMappingPair : MappingSource->getDiagnosticMappings()) {
475       diag::kind DiagID = DiagIDMappingPair.first;
476       Level CurLevel = Diags.getDiagnosticLevel(DiagID, SourceLocation());
477       if (CurLevel < DiagnosticsEngine::Error)
478         continue; // not significant
479       Level StoredLevel =
480           StoredDiags.getDiagnosticLevel(DiagID, SourceLocation());
481       if (StoredLevel < DiagnosticsEngine::Error) {
482         if (Complain)
483           Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror=" +
484               Diags.getDiagnosticIDs()->getWarningOptionForDiag(DiagID).str();
485         return true;
486       }
487     }
488   }
489 
490   return false;
491 }
492 
493 static bool isExtHandlingFromDiagsError(DiagnosticsEngine &Diags) {
494   diag::Severity Ext = Diags.getExtensionHandlingBehavior();
495   if (Ext == diag::Severity::Warning && Diags.getWarningsAsErrors())
496     return true;
497   return Ext >= diag::Severity::Error;
498 }
499 
500 static bool checkDiagnosticMappings(DiagnosticsEngine &StoredDiags,
501                                     DiagnosticsEngine &Diags,
502                                     bool IsSystem, bool Complain) {
503   // Top-level options
504   if (IsSystem) {
505     if (Diags.getSuppressSystemWarnings())
506       return false;
507     // If -Wsystem-headers was not enabled before, be conservative
508     if (StoredDiags.getSuppressSystemWarnings()) {
509       if (Complain)
510         Diags.Report(diag::err_pch_diagopt_mismatch) << "-Wsystem-headers";
511       return true;
512     }
513   }
514 
515   if (Diags.getWarningsAsErrors() && !StoredDiags.getWarningsAsErrors()) {
516     if (Complain)
517       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Werror";
518     return true;
519   }
520 
521   if (Diags.getWarningsAsErrors() && Diags.getEnableAllWarnings() &&
522       !StoredDiags.getEnableAllWarnings()) {
523     if (Complain)
524       Diags.Report(diag::err_pch_diagopt_mismatch) << "-Weverything -Werror";
525     return true;
526   }
527 
528   if (isExtHandlingFromDiagsError(Diags) &&
529       !isExtHandlingFromDiagsError(StoredDiags)) {
530     if (Complain)
531       Diags.Report(diag::err_pch_diagopt_mismatch) << "-pedantic-errors";
532     return true;
533   }
534 
535   return checkDiagnosticGroupMappings(StoredDiags, Diags, Complain);
536 }
537 
538 /// Return the top import module if it is implicit, nullptr otherwise.
539 static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
540                                           Preprocessor &PP) {
541   // If the original import came from a file explicitly generated by the user,
542   // don't check the diagnostic mappings.
543   // FIXME: currently this is approximated by checking whether this is not a
544   // module import of an implicitly-loaded module file.
545   // Note: ModuleMgr.rbegin() may not be the current module, but it must be in
546   // the transitive closure of its imports, since unrelated modules cannot be
547   // imported until after this module finishes validation.
548   ModuleFile *TopImport = &*ModuleMgr.rbegin();
549   while (!TopImport->ImportedBy.empty())
550     TopImport = TopImport->ImportedBy[0];
551   if (TopImport->Kind != MK_ImplicitModule)
552     return nullptr;
553 
554   StringRef ModuleName = TopImport->ModuleName;
555   assert(!ModuleName.empty() && "diagnostic options read before module name");
556 
557   Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
558   assert(M && "missing module");
559   return M;
560 }
561 
562 bool PCHValidator::ReadDiagnosticOptions(
563     IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, bool Complain) {
564   DiagnosticsEngine &ExistingDiags = PP.getDiagnostics();
565   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(ExistingDiags.getDiagnosticIDs());
566   IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
567       new DiagnosticsEngine(DiagIDs, DiagOpts.get()));
568   // This should never fail, because we would have processed these options
569   // before writing them to an ASTFile.
570   ProcessWarningOptions(*Diags, *DiagOpts, /*Report*/false);
571 
572   ModuleManager &ModuleMgr = Reader.getModuleManager();
573   assert(ModuleMgr.size() >= 1 && "what ASTFile is this then");
574 
575   Module *TopM = getTopImportImplicitModule(ModuleMgr, PP);
576   if (!TopM)
577     return false;
578 
579   // FIXME: if the diagnostics are incompatible, save a DiagnosticOptions that
580   // contains the union of their flags.
581   return checkDiagnosticMappings(*Diags, ExistingDiags, TopM->IsSystem,
582                                  Complain);
583 }
584 
585 /// Collect the macro definitions provided by the given preprocessor
586 /// options.
587 static void
588 collectMacroDefinitions(const PreprocessorOptions &PPOpts,
589                         MacroDefinitionsMap &Macros,
590                         SmallVectorImpl<StringRef> *MacroNames = nullptr) {
591   for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
592     StringRef Macro = PPOpts.Macros[I].first;
593     bool IsUndef = PPOpts.Macros[I].second;
594 
595     std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
596     StringRef MacroName = MacroPair.first;
597     StringRef MacroBody = MacroPair.second;
598 
599     // For an #undef'd macro, we only care about the name.
600     if (IsUndef) {
601       if (MacroNames && !Macros.count(MacroName))
602         MacroNames->push_back(MacroName);
603 
604       Macros[MacroName] = std::make_pair("", true);
605       continue;
606     }
607 
608     // For a #define'd macro, figure out the actual definition.
609     if (MacroName.size() == Macro.size())
610       MacroBody = "1";
611     else {
612       // Note: GCC drops anything following an end-of-line character.
613       StringRef::size_type End = MacroBody.find_first_of("\n\r");
614       MacroBody = MacroBody.substr(0, End);
615     }
616 
617     if (MacroNames && !Macros.count(MacroName))
618       MacroNames->push_back(MacroName);
619     Macros[MacroName] = std::make_pair(MacroBody, false);
620   }
621 }
622 
623 /// Check the preprocessor options deserialized from the control block
624 /// against the preprocessor options in an existing preprocessor.
625 ///
626 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
627 /// \param Validate If true, validate preprocessor options. If false, allow
628 ///        macros defined by \p ExistingPPOpts to override those defined by
629 ///        \p PPOpts in SuggestedPredefines.
630 static bool checkPreprocessorOptions(const PreprocessorOptions &PPOpts,
631                                      const PreprocessorOptions &ExistingPPOpts,
632                                      DiagnosticsEngine *Diags,
633                                      FileManager &FileMgr,
634                                      std::string &SuggestedPredefines,
635                                      const LangOptions &LangOpts,
636                                      bool Validate = true) {
637   // Check macro definitions.
638   MacroDefinitionsMap ASTFileMacros;
639   collectMacroDefinitions(PPOpts, ASTFileMacros);
640   MacroDefinitionsMap ExistingMacros;
641   SmallVector<StringRef, 4> ExistingMacroNames;
642   collectMacroDefinitions(ExistingPPOpts, ExistingMacros, &ExistingMacroNames);
643 
644   for (unsigned I = 0, N = ExistingMacroNames.size(); I != N; ++I) {
645     // Dig out the macro definition in the existing preprocessor options.
646     StringRef MacroName = ExistingMacroNames[I];
647     std::pair<StringRef, bool> Existing = ExistingMacros[MacroName];
648 
649     // Check whether we know anything about this macro name or not.
650     llvm::StringMap<std::pair<StringRef, bool /*IsUndef*/>>::iterator Known =
651         ASTFileMacros.find(MacroName);
652     if (!Validate || Known == ASTFileMacros.end()) {
653       // FIXME: Check whether this identifier was referenced anywhere in the
654       // AST file. If so, we should reject the AST file. Unfortunately, this
655       // information isn't in the control block. What shall we do about it?
656 
657       if (Existing.second) {
658         SuggestedPredefines += "#undef ";
659         SuggestedPredefines += MacroName.str();
660         SuggestedPredefines += '\n';
661       } else {
662         SuggestedPredefines += "#define ";
663         SuggestedPredefines += MacroName.str();
664         SuggestedPredefines += ' ';
665         SuggestedPredefines += Existing.first.str();
666         SuggestedPredefines += '\n';
667       }
668       continue;
669     }
670 
671     // If the macro was defined in one but undef'd in the other, we have a
672     // conflict.
673     if (Existing.second != Known->second.second) {
674       if (Diags) {
675         Diags->Report(diag::err_pch_macro_def_undef)
676           << MacroName << Known->second.second;
677       }
678       return true;
679     }
680 
681     // If the macro was #undef'd in both, or if the macro bodies are identical,
682     // it's fine.
683     if (Existing.second || Existing.first == Known->second.first)
684       continue;
685 
686     // The macro bodies differ; complain.
687     if (Diags) {
688       Diags->Report(diag::err_pch_macro_def_conflict)
689         << MacroName << Known->second.first << Existing.first;
690     }
691     return true;
692   }
693 
694   // Check whether we're using predefines.
695   if (PPOpts.UsePredefines != ExistingPPOpts.UsePredefines && Validate) {
696     if (Diags) {
697       Diags->Report(diag::err_pch_undef) << ExistingPPOpts.UsePredefines;
698     }
699     return true;
700   }
701 
702   // Detailed record is important since it is used for the module cache hash.
703   if (LangOpts.Modules &&
704       PPOpts.DetailedRecord != ExistingPPOpts.DetailedRecord && Validate) {
705     if (Diags) {
706       Diags->Report(diag::err_pch_pp_detailed_record) << PPOpts.DetailedRecord;
707     }
708     return true;
709   }
710 
711   // Compute the #include and #include_macros lines we need.
712   for (unsigned I = 0, N = ExistingPPOpts.Includes.size(); I != N; ++I) {
713     StringRef File = ExistingPPOpts.Includes[I];
714 
715     if (!ExistingPPOpts.ImplicitPCHInclude.empty() &&
716         !ExistingPPOpts.PCHThroughHeader.empty()) {
717       // In case the through header is an include, we must add all the includes
718       // to the predefines so the start point can be determined.
719       SuggestedPredefines += "#include \"";
720       SuggestedPredefines += File;
721       SuggestedPredefines += "\"\n";
722       continue;
723     }
724 
725     if (File == ExistingPPOpts.ImplicitPCHInclude)
726       continue;
727 
728     if (std::find(PPOpts.Includes.begin(), PPOpts.Includes.end(), File)
729           != PPOpts.Includes.end())
730       continue;
731 
732     SuggestedPredefines += "#include \"";
733     SuggestedPredefines += File;
734     SuggestedPredefines += "\"\n";
735   }
736 
737   for (unsigned I = 0, N = ExistingPPOpts.MacroIncludes.size(); I != N; ++I) {
738     StringRef File = ExistingPPOpts.MacroIncludes[I];
739     if (std::find(PPOpts.MacroIncludes.begin(), PPOpts.MacroIncludes.end(),
740                   File)
741         != PPOpts.MacroIncludes.end())
742       continue;
743 
744     SuggestedPredefines += "#__include_macros \"";
745     SuggestedPredefines += File;
746     SuggestedPredefines += "\"\n##\n";
747   }
748 
749   return false;
750 }
751 
752 bool PCHValidator::ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
753                                            bool Complain,
754                                            std::string &SuggestedPredefines) {
755   const PreprocessorOptions &ExistingPPOpts = PP.getPreprocessorOpts();
756 
757   return checkPreprocessorOptions(PPOpts, ExistingPPOpts,
758                                   Complain? &Reader.Diags : nullptr,
759                                   PP.getFileManager(),
760                                   SuggestedPredefines,
761                                   PP.getLangOpts());
762 }
763 
764 bool SimpleASTReaderListener::ReadPreprocessorOptions(
765                                   const PreprocessorOptions &PPOpts,
766                                   bool Complain,
767                                   std::string &SuggestedPredefines) {
768   return checkPreprocessorOptions(PPOpts,
769                                   PP.getPreprocessorOpts(),
770                                   nullptr,
771                                   PP.getFileManager(),
772                                   SuggestedPredefines,
773                                   PP.getLangOpts(),
774                                   false);
775 }
776 
777 /// Check the header search options deserialized from the control block
778 /// against the header search options in an existing preprocessor.
779 ///
780 /// \param Diags If non-null, produce diagnostics for any mismatches incurred.
781 static bool checkHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
782                                      StringRef SpecificModuleCachePath,
783                                      StringRef ExistingModuleCachePath,
784                                      DiagnosticsEngine *Diags,
785                                      const LangOptions &LangOpts) {
786   if (LangOpts.Modules) {
787     if (SpecificModuleCachePath != ExistingModuleCachePath) {
788       if (Diags)
789         Diags->Report(diag::err_pch_modulecache_mismatch)
790           << SpecificModuleCachePath << ExistingModuleCachePath;
791       return true;
792     }
793   }
794 
795   return false;
796 }
797 
798 bool PCHValidator::ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
799                                            StringRef SpecificModuleCachePath,
800                                            bool Complain) {
801   return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
802                                   PP.getHeaderSearchInfo().getModuleCachePath(),
803                                   Complain ? &Reader.Diags : nullptr,
804                                   PP.getLangOpts());
805 }
806 
807 void PCHValidator::ReadCounter(const ModuleFile &M, unsigned Value) {
808   PP.setCounterValue(Value);
809 }
810 
811 //===----------------------------------------------------------------------===//
812 // AST reader implementation
813 //===----------------------------------------------------------------------===//
814 
815 void ASTReader::setDeserializationListener(ASTDeserializationListener *Listener,
816                                            bool TakeOwnership) {
817   DeserializationListener = Listener;
818   OwnsDeserializationListener = TakeOwnership;
819 }
820 
821 unsigned ASTSelectorLookupTrait::ComputeHash(Selector Sel) {
822   return serialization::ComputeHash(Sel);
823 }
824 
825 std::pair<unsigned, unsigned>
826 ASTSelectorLookupTrait::ReadKeyDataLength(const unsigned char*& d) {
827   using namespace llvm::support;
828 
829   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
830   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
831   return std::make_pair(KeyLen, DataLen);
832 }
833 
834 ASTSelectorLookupTrait::internal_key_type
835 ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
836   using namespace llvm::support;
837 
838   SelectorTable &SelTable = Reader.getContext().Selectors;
839   unsigned N = endian::readNext<uint16_t, little, unaligned>(d);
840   IdentifierInfo *FirstII = Reader.getLocalIdentifier(
841       F, endian::readNext<uint32_t, little, unaligned>(d));
842   if (N == 0)
843     return SelTable.getNullarySelector(FirstII);
844   else if (N == 1)
845     return SelTable.getUnarySelector(FirstII);
846 
847   SmallVector<IdentifierInfo *, 16> Args;
848   Args.push_back(FirstII);
849   for (unsigned I = 1; I != N; ++I)
850     Args.push_back(Reader.getLocalIdentifier(
851         F, endian::readNext<uint32_t, little, unaligned>(d)));
852 
853   return SelTable.getSelector(N, Args.data());
854 }
855 
856 ASTSelectorLookupTrait::data_type
857 ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
858                                  unsigned DataLen) {
859   using namespace llvm::support;
860 
861   data_type Result;
862 
863   Result.ID = Reader.getGlobalSelectorID(
864       F, endian::readNext<uint32_t, little, unaligned>(d));
865   unsigned FullInstanceBits = endian::readNext<uint16_t, little, unaligned>(d);
866   unsigned FullFactoryBits = endian::readNext<uint16_t, little, unaligned>(d);
867   Result.InstanceBits = FullInstanceBits & 0x3;
868   Result.InstanceHasMoreThanOneDecl = (FullInstanceBits >> 2) & 0x1;
869   Result.FactoryBits = FullFactoryBits & 0x3;
870   Result.FactoryHasMoreThanOneDecl = (FullFactoryBits >> 2) & 0x1;
871   unsigned NumInstanceMethods = FullInstanceBits >> 3;
872   unsigned NumFactoryMethods = FullFactoryBits >> 3;
873 
874   // Load instance methods
875   for (unsigned I = 0; I != NumInstanceMethods; ++I) {
876     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
877             F, endian::readNext<uint32_t, little, unaligned>(d)))
878       Result.Instance.push_back(Method);
879   }
880 
881   // Load factory methods
882   for (unsigned I = 0; I != NumFactoryMethods; ++I) {
883     if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
884             F, endian::readNext<uint32_t, little, unaligned>(d)))
885       Result.Factory.push_back(Method);
886   }
887 
888   return Result;
889 }
890 
891 unsigned ASTIdentifierLookupTraitBase::ComputeHash(const internal_key_type& a) {
892   return llvm::djbHash(a);
893 }
894 
895 std::pair<unsigned, unsigned>
896 ASTIdentifierLookupTraitBase::ReadKeyDataLength(const unsigned char*& d) {
897   using namespace llvm::support;
898 
899   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
900   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
901   return std::make_pair(KeyLen, DataLen);
902 }
903 
904 ASTIdentifierLookupTraitBase::internal_key_type
905 ASTIdentifierLookupTraitBase::ReadKey(const unsigned char* d, unsigned n) {
906   assert(n >= 2 && d[n-1] == '\0');
907   return StringRef((const char*) d, n-1);
908 }
909 
910 /// Whether the given identifier is "interesting".
911 static bool isInterestingIdentifier(ASTReader &Reader, IdentifierInfo &II,
912                                     bool IsModule) {
913   return II.hadMacroDefinition() ||
914          II.isPoisoned() ||
915          (IsModule ? II.hasRevertedBuiltin() : II.getObjCOrBuiltinID()) ||
916          II.hasRevertedTokenIDToIdentifier() ||
917          (!(IsModule && Reader.getPreprocessor().getLangOpts().CPlusPlus) &&
918           II.getFETokenInfo());
919 }
920 
921 static bool readBit(unsigned &Bits) {
922   bool Value = Bits & 0x1;
923   Bits >>= 1;
924   return Value;
925 }
926 
927 IdentID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
928   using namespace llvm::support;
929 
930   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
931   return Reader.getGlobalIdentifierID(F, RawID >> 1);
932 }
933 
934 static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
935   if (!II.isFromAST()) {
936     II.setIsFromAST();
937     bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
938     if (isInterestingIdentifier(Reader, II, IsModule))
939       II.setChangedSinceDeserialization();
940   }
941 }
942 
943 IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
944                                                    const unsigned char* d,
945                                                    unsigned DataLen) {
946   using namespace llvm::support;
947 
948   unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
949   bool IsInteresting = RawID & 0x01;
950 
951   // Wipe out the "is interesting" bit.
952   RawID = RawID >> 1;
953 
954   // Build the IdentifierInfo and link the identifier ID with it.
955   IdentifierInfo *II = KnownII;
956   if (!II) {
957     II = &Reader.getIdentifierTable().getOwn(k);
958     KnownII = II;
959   }
960   markIdentifierFromAST(Reader, *II);
961   Reader.markIdentifierUpToDate(II);
962 
963   IdentID ID = Reader.getGlobalIdentifierID(F, RawID);
964   if (!IsInteresting) {
965     // For uninteresting identifiers, there's nothing else to do. Just notify
966     // the reader that we've finished loading this identifier.
967     Reader.SetIdentifierInfo(ID, II);
968     return II;
969   }
970 
971   unsigned ObjCOrBuiltinID = endian::readNext<uint16_t, little, unaligned>(d);
972   unsigned Bits = endian::readNext<uint16_t, little, unaligned>(d);
973   bool CPlusPlusOperatorKeyword = readBit(Bits);
974   bool HasRevertedTokenIDToIdentifier = readBit(Bits);
975   bool HasRevertedBuiltin = readBit(Bits);
976   bool Poisoned = readBit(Bits);
977   bool ExtensionToken = readBit(Bits);
978   bool HadMacroDefinition = readBit(Bits);
979 
980   assert(Bits == 0 && "Extra bits in the identifier?");
981   DataLen -= 8;
982 
983   // Set or check the various bits in the IdentifierInfo structure.
984   // Token IDs are read-only.
985   if (HasRevertedTokenIDToIdentifier && II->getTokenID() != tok::identifier)
986     II->revertTokenIDToIdentifier();
987   if (!F.isModule())
988     II->setObjCOrBuiltinID(ObjCOrBuiltinID);
989   else if (HasRevertedBuiltin && II->getBuiltinID()) {
990     II->revertBuiltin();
991     assert((II->hasRevertedBuiltin() ||
992             II->getObjCOrBuiltinID() == ObjCOrBuiltinID) &&
993            "Incorrect ObjC keyword or builtin ID");
994   }
995   assert(II->isExtensionToken() == ExtensionToken &&
996          "Incorrect extension token flag");
997   (void)ExtensionToken;
998   if (Poisoned)
999     II->setIsPoisoned(true);
1000   assert(II->isCPlusPlusOperatorKeyword() == CPlusPlusOperatorKeyword &&
1001          "Incorrect C++ operator keyword flag");
1002   (void)CPlusPlusOperatorKeyword;
1003 
1004   // If this identifier is a macro, deserialize the macro
1005   // definition.
1006   if (HadMacroDefinition) {
1007     uint32_t MacroDirectivesOffset =
1008         endian::readNext<uint32_t, little, unaligned>(d);
1009     DataLen -= 4;
1010 
1011     Reader.addPendingMacro(II, &F, MacroDirectivesOffset);
1012   }
1013 
1014   Reader.SetIdentifierInfo(ID, II);
1015 
1016   // Read all of the declarations visible at global scope with this
1017   // name.
1018   if (DataLen > 0) {
1019     SmallVector<uint32_t, 4> DeclIDs;
1020     for (; DataLen > 0; DataLen -= 4)
1021       DeclIDs.push_back(Reader.getGlobalDeclID(
1022           F, endian::readNext<uint32_t, little, unaligned>(d)));
1023     Reader.SetGloballyVisibleDecls(II, DeclIDs);
1024   }
1025 
1026   return II;
1027 }
1028 
1029 DeclarationNameKey::DeclarationNameKey(DeclarationName Name)
1030     : Kind(Name.getNameKind()) {
1031   switch (Kind) {
1032   case DeclarationName::Identifier:
1033     Data = (uint64_t)Name.getAsIdentifierInfo();
1034     break;
1035   case DeclarationName::ObjCZeroArgSelector:
1036   case DeclarationName::ObjCOneArgSelector:
1037   case DeclarationName::ObjCMultiArgSelector:
1038     Data = (uint64_t)Name.getObjCSelector().getAsOpaquePtr();
1039     break;
1040   case DeclarationName::CXXOperatorName:
1041     Data = Name.getCXXOverloadedOperator();
1042     break;
1043   case DeclarationName::CXXLiteralOperatorName:
1044     Data = (uint64_t)Name.getCXXLiteralIdentifier();
1045     break;
1046   case DeclarationName::CXXDeductionGuideName:
1047     Data = (uint64_t)Name.getCXXDeductionGuideTemplate()
1048                ->getDeclName().getAsIdentifierInfo();
1049     break;
1050   case DeclarationName::CXXConstructorName:
1051   case DeclarationName::CXXDestructorName:
1052   case DeclarationName::CXXConversionFunctionName:
1053   case DeclarationName::CXXUsingDirective:
1054     Data = 0;
1055     break;
1056   }
1057 }
1058 
1059 unsigned DeclarationNameKey::getHash() const {
1060   llvm::FoldingSetNodeID ID;
1061   ID.AddInteger(Kind);
1062 
1063   switch (Kind) {
1064   case DeclarationName::Identifier:
1065   case DeclarationName::CXXLiteralOperatorName:
1066   case DeclarationName::CXXDeductionGuideName:
1067     ID.AddString(((IdentifierInfo*)Data)->getName());
1068     break;
1069   case DeclarationName::ObjCZeroArgSelector:
1070   case DeclarationName::ObjCOneArgSelector:
1071   case DeclarationName::ObjCMultiArgSelector:
1072     ID.AddInteger(serialization::ComputeHash(Selector(Data)));
1073     break;
1074   case DeclarationName::CXXOperatorName:
1075     ID.AddInteger((OverloadedOperatorKind)Data);
1076     break;
1077   case DeclarationName::CXXConstructorName:
1078   case DeclarationName::CXXDestructorName:
1079   case DeclarationName::CXXConversionFunctionName:
1080   case DeclarationName::CXXUsingDirective:
1081     break;
1082   }
1083 
1084   return ID.ComputeHash();
1085 }
1086 
1087 ModuleFile *
1088 ASTDeclContextNameLookupTrait::ReadFileRef(const unsigned char *&d) {
1089   using namespace llvm::support;
1090 
1091   uint32_t ModuleFileID = endian::readNext<uint32_t, little, unaligned>(d);
1092   return Reader.getLocalModuleFile(F, ModuleFileID);
1093 }
1094 
1095 std::pair<unsigned, unsigned>
1096 ASTDeclContextNameLookupTrait::ReadKeyDataLength(const unsigned char *&d) {
1097   using namespace llvm::support;
1098 
1099   unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
1100   unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
1101   return std::make_pair(KeyLen, DataLen);
1102 }
1103 
1104 ASTDeclContextNameLookupTrait::internal_key_type
1105 ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
1106   using namespace llvm::support;
1107 
1108   auto Kind = (DeclarationName::NameKind)*d++;
1109   uint64_t Data;
1110   switch (Kind) {
1111   case DeclarationName::Identifier:
1112   case DeclarationName::CXXLiteralOperatorName:
1113   case DeclarationName::CXXDeductionGuideName:
1114     Data = (uint64_t)Reader.getLocalIdentifier(
1115         F, endian::readNext<uint32_t, little, unaligned>(d));
1116     break;
1117   case DeclarationName::ObjCZeroArgSelector:
1118   case DeclarationName::ObjCOneArgSelector:
1119   case DeclarationName::ObjCMultiArgSelector:
1120     Data =
1121         (uint64_t)Reader.getLocalSelector(
1122                              F, endian::readNext<uint32_t, little, unaligned>(
1123                                     d)).getAsOpaquePtr();
1124     break;
1125   case DeclarationName::CXXOperatorName:
1126     Data = *d++; // OverloadedOperatorKind
1127     break;
1128   case DeclarationName::CXXConstructorName:
1129   case DeclarationName::CXXDestructorName:
1130   case DeclarationName::CXXConversionFunctionName:
1131   case DeclarationName::CXXUsingDirective:
1132     Data = 0;
1133     break;
1134   }
1135 
1136   return DeclarationNameKey(Kind, Data);
1137 }
1138 
1139 void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
1140                                                  const unsigned char *d,
1141                                                  unsigned DataLen,
1142                                                  data_type_builder &Val) {
1143   using namespace llvm::support;
1144 
1145   for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1146     uint32_t LocalID = endian::readNext<uint32_t, little, unaligned>(d);
1147     Val.insert(Reader.getGlobalDeclID(F, LocalID));
1148   }
1149 }
1150 
1151 bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
1152                                               BitstreamCursor &Cursor,
1153                                               uint64_t Offset,
1154                                               DeclContext *DC) {
1155   assert(Offset != 0);
1156 
1157   SavedStreamPosition SavedPosition(Cursor);
1158   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1159     Error(std::move(Err));
1160     return true;
1161   }
1162 
1163   RecordData Record;
1164   StringRef Blob;
1165   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1166   if (!MaybeCode) {
1167     Error(MaybeCode.takeError());
1168     return true;
1169   }
1170   unsigned Code = MaybeCode.get();
1171 
1172   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1173   if (!MaybeRecCode) {
1174     Error(MaybeRecCode.takeError());
1175     return true;
1176   }
1177   unsigned RecCode = MaybeRecCode.get();
1178   if (RecCode != DECL_CONTEXT_LEXICAL) {
1179     Error("Expected lexical block");
1180     return true;
1181   }
1182 
1183   assert(!isa<TranslationUnitDecl>(DC) &&
1184          "expected a TU_UPDATE_LEXICAL record for TU");
1185   // If we are handling a C++ class template instantiation, we can see multiple
1186   // lexical updates for the same record. It's important that we select only one
1187   // of them, so that field numbering works properly. Just pick the first one we
1188   // see.
1189   auto &Lex = LexicalDecls[DC];
1190   if (!Lex.first) {
1191     Lex = std::make_pair(
1192         &M, llvm::makeArrayRef(
1193                 reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1194                     Blob.data()),
1195                 Blob.size() / 4));
1196   }
1197   DC->setHasExternalLexicalStorage(true);
1198   return false;
1199 }
1200 
1201 bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M,
1202                                               BitstreamCursor &Cursor,
1203                                               uint64_t Offset,
1204                                               DeclID ID) {
1205   assert(Offset != 0);
1206 
1207   SavedStreamPosition SavedPosition(Cursor);
1208   if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1209     Error(std::move(Err));
1210     return true;
1211   }
1212 
1213   RecordData Record;
1214   StringRef Blob;
1215   Expected<unsigned> MaybeCode = Cursor.ReadCode();
1216   if (!MaybeCode) {
1217     Error(MaybeCode.takeError());
1218     return true;
1219   }
1220   unsigned Code = MaybeCode.get();
1221 
1222   Expected<unsigned> MaybeRecCode = Cursor.readRecord(Code, Record, &Blob);
1223   if (!MaybeRecCode) {
1224     Error(MaybeRecCode.takeError());
1225     return true;
1226   }
1227   unsigned RecCode = MaybeRecCode.get();
1228   if (RecCode != DECL_CONTEXT_VISIBLE) {
1229     Error("Expected visible lookup table block");
1230     return true;
1231   }
1232 
1233   // We can't safely determine the primary context yet, so delay attaching the
1234   // lookup table until we're done with recursive deserialization.
1235   auto *Data = (const unsigned char*)Blob.data();
1236   PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&M, Data});
1237   return false;
1238 }
1239 
1240 void ASTReader::Error(StringRef Msg) const {
1241   Error(diag::err_fe_pch_malformed, Msg);
1242   if (PP.getLangOpts().Modules && !Diags.isDiagnosticInFlight() &&
1243       !PP.getHeaderSearchInfo().getModuleCachePath().empty()) {
1244     Diag(diag::note_module_cache_path)
1245       << PP.getHeaderSearchInfo().getModuleCachePath();
1246   }
1247 }
1248 
1249 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1250                       StringRef Arg3) const {
1251   if (Diags.isDiagnosticInFlight())
1252     Diags.SetDelayedDiagnostic(DiagID, Arg1, Arg2, Arg3);
1253   else
1254     Diag(DiagID) << Arg1 << Arg2 << Arg3;
1255 }
1256 
1257 void ASTReader::Error(unsigned DiagID, StringRef Arg1, StringRef Arg2,
1258                       unsigned Select) const {
1259   if (!Diags.isDiagnosticInFlight())
1260     Diag(DiagID) << Arg1 << Arg2 << Select;
1261 }
1262 
1263 void ASTReader::Error(llvm::Error &&Err) const {
1264   Error(toString(std::move(Err)));
1265 }
1266 
1267 //===----------------------------------------------------------------------===//
1268 // Source Manager Deserialization
1269 //===----------------------------------------------------------------------===//
1270 
1271 /// Read the line table in the source manager block.
1272 /// \returns true if there was an error.
1273 bool ASTReader::ParseLineTable(ModuleFile &F,
1274                                const RecordData &Record) {
1275   unsigned Idx = 0;
1276   LineTableInfo &LineTable = SourceMgr.getLineTable();
1277 
1278   // Parse the file names
1279   std::map<int, int> FileIDs;
1280   FileIDs[-1] = -1; // For unspecified filenames.
1281   for (unsigned I = 0; Record[Idx]; ++I) {
1282     // Extract the file name
1283     auto Filename = ReadPath(F, Record, Idx);
1284     FileIDs[I] = LineTable.getLineTableFilenameID(Filename);
1285   }
1286   ++Idx;
1287 
1288   // Parse the line entries
1289   std::vector<LineEntry> Entries;
1290   while (Idx < Record.size()) {
1291     int FID = Record[Idx++];
1292     assert(FID >= 0 && "Serialized line entries for non-local file.");
1293     // Remap FileID from 1-based old view.
1294     FID += F.SLocEntryBaseID - 1;
1295 
1296     // Extract the line entries
1297     unsigned NumEntries = Record[Idx++];
1298     assert(NumEntries && "no line entries for file ID");
1299     Entries.clear();
1300     Entries.reserve(NumEntries);
1301     for (unsigned I = 0; I != NumEntries; ++I) {
1302       unsigned FileOffset = Record[Idx++];
1303       unsigned LineNo = Record[Idx++];
1304       int FilenameID = FileIDs[Record[Idx++]];
1305       SrcMgr::CharacteristicKind FileKind
1306         = (SrcMgr::CharacteristicKind)Record[Idx++];
1307       unsigned IncludeOffset = Record[Idx++];
1308       Entries.push_back(LineEntry::get(FileOffset, LineNo, FilenameID,
1309                                        FileKind, IncludeOffset));
1310     }
1311     LineTable.AddEntry(FileID::get(FID), Entries);
1312   }
1313 
1314   return false;
1315 }
1316 
1317 /// Read a source manager block
1318 bool ASTReader::ReadSourceManagerBlock(ModuleFile &F) {
1319   using namespace SrcMgr;
1320 
1321   BitstreamCursor &SLocEntryCursor = F.SLocEntryCursor;
1322 
1323   // Set the source-location entry cursor to the current position in
1324   // the stream. This cursor will be used to read the contents of the
1325   // source manager block initially, and then lazily read
1326   // source-location entries as needed.
1327   SLocEntryCursor = F.Stream;
1328 
1329   // The stream itself is going to skip over the source manager block.
1330   if (llvm::Error Err = F.Stream.SkipBlock()) {
1331     Error(std::move(Err));
1332     return true;
1333   }
1334 
1335   // Enter the source manager block.
1336   if (llvm::Error Err =
1337           SLocEntryCursor.EnterSubBlock(SOURCE_MANAGER_BLOCK_ID)) {
1338     Error(std::move(Err));
1339     return true;
1340   }
1341   F.SourceManagerBlockStartOffset = SLocEntryCursor.GetCurrentBitNo();
1342 
1343   RecordData Record;
1344   while (true) {
1345     Expected<llvm::BitstreamEntry> MaybeE =
1346         SLocEntryCursor.advanceSkippingSubblocks();
1347     if (!MaybeE) {
1348       Error(MaybeE.takeError());
1349       return true;
1350     }
1351     llvm::BitstreamEntry E = MaybeE.get();
1352 
1353     switch (E.Kind) {
1354     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1355     case llvm::BitstreamEntry::Error:
1356       Error("malformed block record in AST file");
1357       return true;
1358     case llvm::BitstreamEntry::EndBlock:
1359       return false;
1360     case llvm::BitstreamEntry::Record:
1361       // The interesting case.
1362       break;
1363     }
1364 
1365     // Read a record.
1366     Record.clear();
1367     StringRef Blob;
1368     Expected<unsigned> MaybeRecord =
1369         SLocEntryCursor.readRecord(E.ID, Record, &Blob);
1370     if (!MaybeRecord) {
1371       Error(MaybeRecord.takeError());
1372       return true;
1373     }
1374     switch (MaybeRecord.get()) {
1375     default:  // Default behavior: ignore.
1376       break;
1377 
1378     case SM_SLOC_FILE_ENTRY:
1379     case SM_SLOC_BUFFER_ENTRY:
1380     case SM_SLOC_EXPANSION_ENTRY:
1381       // Once we hit one of the source location entries, we're done.
1382       return false;
1383     }
1384   }
1385 }
1386 
1387 /// If a header file is not found at the path that we expect it to be
1388 /// and the PCH file was moved from its original location, try to resolve the
1389 /// file by assuming that header+PCH were moved together and the header is in
1390 /// the same place relative to the PCH.
1391 static std::string
1392 resolveFileRelativeToOriginalDir(const std::string &Filename,
1393                                  const std::string &OriginalDir,
1394                                  const std::string &CurrDir) {
1395   assert(OriginalDir != CurrDir &&
1396          "No point trying to resolve the file if the PCH dir didn't change");
1397 
1398   using namespace llvm::sys;
1399 
1400   SmallString<128> filePath(Filename);
1401   fs::make_absolute(filePath);
1402   assert(path::is_absolute(OriginalDir));
1403   SmallString<128> currPCHPath(CurrDir);
1404 
1405   path::const_iterator fileDirI = path::begin(path::parent_path(filePath)),
1406                        fileDirE = path::end(path::parent_path(filePath));
1407   path::const_iterator origDirI = path::begin(OriginalDir),
1408                        origDirE = path::end(OriginalDir);
1409   // Skip the common path components from filePath and OriginalDir.
1410   while (fileDirI != fileDirE && origDirI != origDirE &&
1411          *fileDirI == *origDirI) {
1412     ++fileDirI;
1413     ++origDirI;
1414   }
1415   for (; origDirI != origDirE; ++origDirI)
1416     path::append(currPCHPath, "..");
1417   path::append(currPCHPath, fileDirI, fileDirE);
1418   path::append(currPCHPath, path::filename(Filename));
1419   return std::string(currPCHPath.str());
1420 }
1421 
1422 bool ASTReader::ReadSLocEntry(int ID) {
1423   if (ID == 0)
1424     return false;
1425 
1426   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1427     Error("source location entry ID out-of-range for AST file");
1428     return true;
1429   }
1430 
1431   // Local helper to read the (possibly-compressed) buffer data following the
1432   // entry record.
1433   auto ReadBuffer = [this](
1434       BitstreamCursor &SLocEntryCursor,
1435       StringRef Name) -> std::unique_ptr<llvm::MemoryBuffer> {
1436     RecordData Record;
1437     StringRef Blob;
1438     Expected<unsigned> MaybeCode = SLocEntryCursor.ReadCode();
1439     if (!MaybeCode) {
1440       Error(MaybeCode.takeError());
1441       return nullptr;
1442     }
1443     unsigned Code = MaybeCode.get();
1444 
1445     Expected<unsigned> MaybeRecCode =
1446         SLocEntryCursor.readRecord(Code, Record, &Blob);
1447     if (!MaybeRecCode) {
1448       Error(MaybeRecCode.takeError());
1449       return nullptr;
1450     }
1451     unsigned RecCode = MaybeRecCode.get();
1452 
1453     if (RecCode == SM_SLOC_BUFFER_BLOB_COMPRESSED) {
1454       if (!llvm::zlib::isAvailable()) {
1455         Error("zlib is not available");
1456         return nullptr;
1457       }
1458       SmallString<0> Uncompressed;
1459       if (llvm::Error E =
1460               llvm::zlib::uncompress(Blob, Uncompressed, Record[0])) {
1461         Error("could not decompress embedded file contents: " +
1462               llvm::toString(std::move(E)));
1463         return nullptr;
1464       }
1465       return llvm::MemoryBuffer::getMemBufferCopy(Uncompressed, Name);
1466     } else if (RecCode == SM_SLOC_BUFFER_BLOB) {
1467       return llvm::MemoryBuffer::getMemBuffer(Blob.drop_back(1), Name, true);
1468     } else {
1469       Error("AST record has invalid code");
1470       return nullptr;
1471     }
1472   };
1473 
1474   ModuleFile *F = GlobalSLocEntryMap.find(-ID)->second;
1475   if (llvm::Error Err = F->SLocEntryCursor.JumpToBit(
1476           F->SLocEntryOffsetsBase +
1477           F->SLocEntryOffsets[ID - F->SLocEntryBaseID])) {
1478     Error(std::move(Err));
1479     return true;
1480   }
1481 
1482   BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor;
1483   unsigned BaseOffset = F->SLocEntryBaseOffset;
1484 
1485   ++NumSLocEntriesRead;
1486   Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance();
1487   if (!MaybeEntry) {
1488     Error(MaybeEntry.takeError());
1489     return true;
1490   }
1491   llvm::BitstreamEntry Entry = MaybeEntry.get();
1492 
1493   if (Entry.Kind != llvm::BitstreamEntry::Record) {
1494     Error("incorrectly-formatted source location entry in AST file");
1495     return true;
1496   }
1497 
1498   RecordData Record;
1499   StringRef Blob;
1500   Expected<unsigned> MaybeSLOC =
1501       SLocEntryCursor.readRecord(Entry.ID, Record, &Blob);
1502   if (!MaybeSLOC) {
1503     Error(MaybeSLOC.takeError());
1504     return true;
1505   }
1506   switch (MaybeSLOC.get()) {
1507   default:
1508     Error("incorrectly-formatted source location entry in AST file");
1509     return true;
1510 
1511   case SM_SLOC_FILE_ENTRY: {
1512     // We will detect whether a file changed and return 'Failure' for it, but
1513     // we will also try to fail gracefully by setting up the SLocEntry.
1514     unsigned InputID = Record[4];
1515     InputFile IF = getInputFile(*F, InputID);
1516     const FileEntry *File = IF.getFile();
1517     bool OverriddenBuffer = IF.isOverridden();
1518 
1519     // Note that we only check if a File was returned. If it was out-of-date
1520     // we have complained but we will continue creating a FileID to recover
1521     // gracefully.
1522     if (!File)
1523       return true;
1524 
1525     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1526     if (IncludeLoc.isInvalid() && F->Kind != MK_MainFile) {
1527       // This is the module's main file.
1528       IncludeLoc = getImportLocation(F);
1529     }
1530     SrcMgr::CharacteristicKind
1531       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1532     // FIXME: The FileID should be created from the FileEntryRef.
1533     FileID FID = SourceMgr.createFileID(File, IncludeLoc, FileCharacter,
1534                                         ID, BaseOffset + Record[0]);
1535     SrcMgr::FileInfo &FileInfo =
1536           const_cast<SrcMgr::FileInfo&>(SourceMgr.getSLocEntry(FID).getFile());
1537     FileInfo.NumCreatedFIDs = Record[5];
1538     if (Record[3])
1539       FileInfo.setHasLineDirectives();
1540 
1541     unsigned NumFileDecls = Record[7];
1542     if (NumFileDecls && ContextObj) {
1543       const DeclID *FirstDecl = F->FileSortedDecls + Record[6];
1544       assert(F->FileSortedDecls && "FILE_SORTED_DECLS not encountered yet ?");
1545       FileDeclIDs[FID] = FileDeclsInfo(F, llvm::makeArrayRef(FirstDecl,
1546                                                              NumFileDecls));
1547     }
1548 
1549     const SrcMgr::ContentCache *ContentCache
1550       = SourceMgr.getOrCreateContentCache(File, isSystem(FileCharacter));
1551     if (OverriddenBuffer && !ContentCache->BufferOverridden &&
1552         ContentCache->ContentsEntry == ContentCache->OrigEntry &&
1553         !ContentCache->getRawBuffer()) {
1554       auto Buffer = ReadBuffer(SLocEntryCursor, File->getName());
1555       if (!Buffer)
1556         return true;
1557       SourceMgr.overrideFileContents(File, std::move(Buffer));
1558     }
1559 
1560     break;
1561   }
1562 
1563   case SM_SLOC_BUFFER_ENTRY: {
1564     const char *Name = Blob.data();
1565     unsigned Offset = Record[0];
1566     SrcMgr::CharacteristicKind
1567       FileCharacter = (SrcMgr::CharacteristicKind)Record[2];
1568     SourceLocation IncludeLoc = ReadSourceLocation(*F, Record[1]);
1569     if (IncludeLoc.isInvalid() && F->isModule()) {
1570       IncludeLoc = getImportLocation(F);
1571     }
1572 
1573     auto Buffer = ReadBuffer(SLocEntryCursor, Name);
1574     if (!Buffer)
1575       return true;
1576     SourceMgr.createFileID(std::move(Buffer), FileCharacter, ID,
1577                            BaseOffset + Offset, IncludeLoc);
1578     break;
1579   }
1580 
1581   case SM_SLOC_EXPANSION_ENTRY: {
1582     SourceLocation SpellingLoc = ReadSourceLocation(*F, Record[1]);
1583     SourceMgr.createExpansionLoc(SpellingLoc,
1584                                      ReadSourceLocation(*F, Record[2]),
1585                                      ReadSourceLocation(*F, Record[3]),
1586                                      Record[5],
1587                                      Record[4],
1588                                      ID,
1589                                      BaseOffset + Record[0]);
1590     break;
1591   }
1592   }
1593 
1594   return false;
1595 }
1596 
1597 std::pair<SourceLocation, StringRef> ASTReader::getModuleImportLoc(int ID) {
1598   if (ID == 0)
1599     return std::make_pair(SourceLocation(), "");
1600 
1601   if (unsigned(-ID) - 2 >= getTotalNumSLocs() || ID > 0) {
1602     Error("source location entry ID out-of-range for AST file");
1603     return std::make_pair(SourceLocation(), "");
1604   }
1605 
1606   // Find which module file this entry lands in.
1607   ModuleFile *M = GlobalSLocEntryMap.find(-ID)->second;
1608   if (!M->isModule())
1609     return std::make_pair(SourceLocation(), "");
1610 
1611   // FIXME: Can we map this down to a particular submodule? That would be
1612   // ideal.
1613   return std::make_pair(M->ImportLoc, StringRef(M->ModuleName));
1614 }
1615 
1616 /// Find the location where the module F is imported.
1617 SourceLocation ASTReader::getImportLocation(ModuleFile *F) {
1618   if (F->ImportLoc.isValid())
1619     return F->ImportLoc;
1620 
1621   // Otherwise we have a PCH. It's considered to be "imported" at the first
1622   // location of its includer.
1623   if (F->ImportedBy.empty() || !F->ImportedBy[0]) {
1624     // Main file is the importer.
1625     assert(SourceMgr.getMainFileID().isValid() && "missing main file");
1626     return SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
1627   }
1628   return F->ImportedBy[0]->FirstLoc;
1629 }
1630 
1631 /// Enter a subblock of the specified BlockID with the specified cursor. Read
1632 /// the abbreviations that are at the top of the block and then leave the cursor
1633 /// pointing into the block.
1634 bool ASTReader::ReadBlockAbbrevs(BitstreamCursor &Cursor, unsigned BlockID,
1635                                  uint64_t *StartOfBlockOffset) {
1636   if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
1637     // FIXME this drops errors on the floor.
1638     consumeError(std::move(Err));
1639     return true;
1640   }
1641 
1642   if (StartOfBlockOffset)
1643     *StartOfBlockOffset = Cursor.GetCurrentBitNo();
1644 
1645   while (true) {
1646     uint64_t Offset = Cursor.GetCurrentBitNo();
1647     Expected<unsigned> MaybeCode = Cursor.ReadCode();
1648     if (!MaybeCode) {
1649       // FIXME this drops errors on the floor.
1650       consumeError(MaybeCode.takeError());
1651       return true;
1652     }
1653     unsigned Code = MaybeCode.get();
1654 
1655     // We expect all abbrevs to be at the start of the block.
1656     if (Code != llvm::bitc::DEFINE_ABBREV) {
1657       if (llvm::Error Err = Cursor.JumpToBit(Offset)) {
1658         // FIXME this drops errors on the floor.
1659         consumeError(std::move(Err));
1660         return true;
1661       }
1662       return false;
1663     }
1664     if (llvm::Error Err = Cursor.ReadAbbrevRecord()) {
1665       // FIXME this drops errors on the floor.
1666       consumeError(std::move(Err));
1667       return true;
1668     }
1669   }
1670 }
1671 
1672 Token ASTReader::ReadToken(ModuleFile &F, const RecordDataImpl &Record,
1673                            unsigned &Idx) {
1674   Token Tok;
1675   Tok.startToken();
1676   Tok.setLocation(ReadSourceLocation(F, Record, Idx));
1677   Tok.setLength(Record[Idx++]);
1678   if (IdentifierInfo *II = getLocalIdentifier(F, Record[Idx++]))
1679     Tok.setIdentifierInfo(II);
1680   Tok.setKind((tok::TokenKind)Record[Idx++]);
1681   Tok.setFlag((Token::TokenFlags)Record[Idx++]);
1682   return Tok;
1683 }
1684 
1685 MacroInfo *ASTReader::ReadMacroRecord(ModuleFile &F, uint64_t Offset) {
1686   BitstreamCursor &Stream = F.MacroCursor;
1687 
1688   // Keep track of where we are in the stream, then jump back there
1689   // after reading this macro.
1690   SavedStreamPosition SavedPosition(Stream);
1691 
1692   if (llvm::Error Err = Stream.JumpToBit(Offset)) {
1693     // FIXME this drops errors on the floor.
1694     consumeError(std::move(Err));
1695     return nullptr;
1696   }
1697   RecordData Record;
1698   SmallVector<IdentifierInfo*, 16> MacroParams;
1699   MacroInfo *Macro = nullptr;
1700 
1701   while (true) {
1702     // Advance to the next record, but if we get to the end of the block, don't
1703     // pop it (removing all the abbreviations from the cursor) since we want to
1704     // be able to reseek within the block and read entries.
1705     unsigned Flags = BitstreamCursor::AF_DontPopBlockAtEnd;
1706     Expected<llvm::BitstreamEntry> MaybeEntry =
1707         Stream.advanceSkippingSubblocks(Flags);
1708     if (!MaybeEntry) {
1709       Error(MaybeEntry.takeError());
1710       return Macro;
1711     }
1712     llvm::BitstreamEntry Entry = MaybeEntry.get();
1713 
1714     switch (Entry.Kind) {
1715     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1716     case llvm::BitstreamEntry::Error:
1717       Error("malformed block record in AST file");
1718       return Macro;
1719     case llvm::BitstreamEntry::EndBlock:
1720       return Macro;
1721     case llvm::BitstreamEntry::Record:
1722       // The interesting case.
1723       break;
1724     }
1725 
1726     // Read a record.
1727     Record.clear();
1728     PreprocessorRecordTypes RecType;
1729     if (Expected<unsigned> MaybeRecType = Stream.readRecord(Entry.ID, Record))
1730       RecType = (PreprocessorRecordTypes)MaybeRecType.get();
1731     else {
1732       Error(MaybeRecType.takeError());
1733       return Macro;
1734     }
1735     switch (RecType) {
1736     case PP_MODULE_MACRO:
1737     case PP_MACRO_DIRECTIVE_HISTORY:
1738       return Macro;
1739 
1740     case PP_MACRO_OBJECT_LIKE:
1741     case PP_MACRO_FUNCTION_LIKE: {
1742       // If we already have a macro, that means that we've hit the end
1743       // of the definition of the macro we were looking for. We're
1744       // done.
1745       if (Macro)
1746         return Macro;
1747 
1748       unsigned NextIndex = 1; // Skip identifier ID.
1749       SourceLocation Loc = ReadSourceLocation(F, Record, NextIndex);
1750       MacroInfo *MI = PP.AllocateMacroInfo(Loc);
1751       MI->setDefinitionEndLoc(ReadSourceLocation(F, Record, NextIndex));
1752       MI->setIsUsed(Record[NextIndex++]);
1753       MI->setUsedForHeaderGuard(Record[NextIndex++]);
1754 
1755       if (RecType == PP_MACRO_FUNCTION_LIKE) {
1756         // Decode function-like macro info.
1757         bool isC99VarArgs = Record[NextIndex++];
1758         bool isGNUVarArgs = Record[NextIndex++];
1759         bool hasCommaPasting = Record[NextIndex++];
1760         MacroParams.clear();
1761         unsigned NumArgs = Record[NextIndex++];
1762         for (unsigned i = 0; i != NumArgs; ++i)
1763           MacroParams.push_back(getLocalIdentifier(F, Record[NextIndex++]));
1764 
1765         // Install function-like macro info.
1766         MI->setIsFunctionLike();
1767         if (isC99VarArgs) MI->setIsC99Varargs();
1768         if (isGNUVarArgs) MI->setIsGNUVarargs();
1769         if (hasCommaPasting) MI->setHasCommaPasting();
1770         MI->setParameterList(MacroParams, PP.getPreprocessorAllocator());
1771       }
1772 
1773       // Remember that we saw this macro last so that we add the tokens that
1774       // form its body to it.
1775       Macro = MI;
1776 
1777       if (NextIndex + 1 == Record.size() && PP.getPreprocessingRecord() &&
1778           Record[NextIndex]) {
1779         // We have a macro definition. Register the association
1780         PreprocessedEntityID
1781             GlobalID = getGlobalPreprocessedEntityID(F, Record[NextIndex]);
1782         PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
1783         PreprocessingRecord::PPEntityID PPID =
1784             PPRec.getPPEntityID(GlobalID - 1, /*isLoaded=*/true);
1785         MacroDefinitionRecord *PPDef = cast_or_null<MacroDefinitionRecord>(
1786             PPRec.getPreprocessedEntity(PPID));
1787         if (PPDef)
1788           PPRec.RegisterMacroDefinition(Macro, PPDef);
1789       }
1790 
1791       ++NumMacrosRead;
1792       break;
1793     }
1794 
1795     case PP_TOKEN: {
1796       // If we see a TOKEN before a PP_MACRO_*, then the file is
1797       // erroneous, just pretend we didn't see this.
1798       if (!Macro) break;
1799 
1800       unsigned Idx = 0;
1801       Token Tok = ReadToken(F, Record, Idx);
1802       Macro->AddTokenToBody(Tok);
1803       break;
1804     }
1805     }
1806   }
1807 }
1808 
1809 PreprocessedEntityID
1810 ASTReader::getGlobalPreprocessedEntityID(ModuleFile &M,
1811                                          unsigned LocalID) const {
1812   if (!M.ModuleOffsetMap.empty())
1813     ReadModuleOffsetMap(M);
1814 
1815   ContinuousRangeMap<uint32_t, int, 2>::const_iterator
1816     I = M.PreprocessedEntityRemap.find(LocalID - NUM_PREDEF_PP_ENTITY_IDS);
1817   assert(I != M.PreprocessedEntityRemap.end()
1818          && "Invalid index into preprocessed entity index remap");
1819 
1820   return LocalID + I->second;
1821 }
1822 
1823 unsigned HeaderFileInfoTrait::ComputeHash(internal_key_ref ikey) {
1824   return llvm::hash_combine(ikey.Size, ikey.ModTime);
1825 }
1826 
1827 HeaderFileInfoTrait::internal_key_type
1828 HeaderFileInfoTrait::GetInternalKey(const FileEntry *FE) {
1829   internal_key_type ikey = {FE->getSize(),
1830                             M.HasTimestamps ? FE->getModificationTime() : 0,
1831                             FE->getName(), /*Imported*/ false};
1832   return ikey;
1833 }
1834 
1835 bool HeaderFileInfoTrait::EqualKey(internal_key_ref a, internal_key_ref b) {
1836   if (a.Size != b.Size || (a.ModTime && b.ModTime && a.ModTime != b.ModTime))
1837     return false;
1838 
1839   if (llvm::sys::path::is_absolute(a.Filename) && a.Filename == b.Filename)
1840     return true;
1841 
1842   // Determine whether the actual files are equivalent.
1843   FileManager &FileMgr = Reader.getFileManager();
1844   auto GetFile = [&](const internal_key_type &Key) -> const FileEntry* {
1845     if (!Key.Imported) {
1846       if (auto File = FileMgr.getFile(Key.Filename))
1847         return *File;
1848       return nullptr;
1849     }
1850 
1851     std::string Resolved = std::string(Key.Filename);
1852     Reader.ResolveImportedPath(M, Resolved);
1853     if (auto File = FileMgr.getFile(Resolved))
1854       return *File;
1855     return nullptr;
1856   };
1857 
1858   const FileEntry *FEA = GetFile(a);
1859   const FileEntry *FEB = GetFile(b);
1860   return FEA && FEA == FEB;
1861 }
1862 
1863 std::pair<unsigned, unsigned>
1864 HeaderFileInfoTrait::ReadKeyDataLength(const unsigned char*& d) {
1865   using namespace llvm::support;
1866 
1867   unsigned KeyLen = (unsigned) endian::readNext<uint16_t, little, unaligned>(d);
1868   unsigned DataLen = (unsigned) *d++;
1869   return std::make_pair(KeyLen, DataLen);
1870 }
1871 
1872 HeaderFileInfoTrait::internal_key_type
1873 HeaderFileInfoTrait::ReadKey(const unsigned char *d, unsigned) {
1874   using namespace llvm::support;
1875 
1876   internal_key_type ikey;
1877   ikey.Size = off_t(endian::readNext<uint64_t, little, unaligned>(d));
1878   ikey.ModTime = time_t(endian::readNext<uint64_t, little, unaligned>(d));
1879   ikey.Filename = (const char *)d;
1880   ikey.Imported = true;
1881   return ikey;
1882 }
1883 
1884 HeaderFileInfoTrait::data_type
1885 HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
1886                               unsigned DataLen) {
1887   using namespace llvm::support;
1888 
1889   const unsigned char *End = d + DataLen;
1890   HeaderFileInfo HFI;
1891   unsigned Flags = *d++;
1892   // FIXME: Refactor with mergeHeaderFileInfo in HeaderSearch.cpp.
1893   HFI.isImport |= (Flags >> 5) & 0x01;
1894   HFI.isPragmaOnce |= (Flags >> 4) & 0x01;
1895   HFI.DirInfo = (Flags >> 1) & 0x07;
1896   HFI.IndexHeaderMapHeader = Flags & 0x01;
1897   // FIXME: Find a better way to handle this. Maybe just store a
1898   // "has been included" flag?
1899   HFI.NumIncludes = std::max(endian::readNext<uint16_t, little, unaligned>(d),
1900                              HFI.NumIncludes);
1901   HFI.ControllingMacroID = Reader.getGlobalIdentifierID(
1902       M, endian::readNext<uint32_t, little, unaligned>(d));
1903   if (unsigned FrameworkOffset =
1904           endian::readNext<uint32_t, little, unaligned>(d)) {
1905     // The framework offset is 1 greater than the actual offset,
1906     // since 0 is used as an indicator for "no framework name".
1907     StringRef FrameworkName(FrameworkStrings + FrameworkOffset - 1);
1908     HFI.Framework = HS->getUniqueFrameworkName(FrameworkName);
1909   }
1910 
1911   assert((End - d) % 4 == 0 &&
1912          "Wrong data length in HeaderFileInfo deserialization");
1913   while (d != End) {
1914     uint32_t LocalSMID = endian::readNext<uint32_t, little, unaligned>(d);
1915     auto HeaderRole = static_cast<ModuleMap::ModuleHeaderRole>(LocalSMID & 3);
1916     LocalSMID >>= 2;
1917 
1918     // This header is part of a module. Associate it with the module to enable
1919     // implicit module import.
1920     SubmoduleID GlobalSMID = Reader.getGlobalSubmoduleID(M, LocalSMID);
1921     Module *Mod = Reader.getSubmodule(GlobalSMID);
1922     FileManager &FileMgr = Reader.getFileManager();
1923     ModuleMap &ModMap =
1924         Reader.getPreprocessor().getHeaderSearchInfo().getModuleMap();
1925 
1926     std::string Filename = std::string(key.Filename);
1927     if (key.Imported)
1928       Reader.ResolveImportedPath(M, Filename);
1929     // FIXME: This is not always the right filename-as-written, but we're not
1930     // going to use this information to rebuild the module, so it doesn't make
1931     // a lot of difference.
1932     Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
1933     ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
1934     HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
1935   }
1936 
1937   // This HeaderFileInfo was externally loaded.
1938   HFI.External = true;
1939   HFI.IsValid = true;
1940   return HFI;
1941 }
1942 
1943 void ASTReader::addPendingMacro(IdentifierInfo *II, ModuleFile *M,
1944                                 uint32_t MacroDirectivesOffset) {
1945   assert(NumCurrentElementsDeserializing > 0 &&"Missing deserialization guard");
1946   PendingMacroIDs[II].push_back(PendingMacroInfo(M, MacroDirectivesOffset));
1947 }
1948 
1949 void ASTReader::ReadDefinedMacros() {
1950   // Note that we are loading defined macros.
1951   Deserializing Macros(this);
1952 
1953   for (ModuleFile &I : llvm::reverse(ModuleMgr)) {
1954     BitstreamCursor &MacroCursor = I.MacroCursor;
1955 
1956     // If there was no preprocessor block, skip this file.
1957     if (MacroCursor.getBitcodeBytes().empty())
1958       continue;
1959 
1960     BitstreamCursor Cursor = MacroCursor;
1961     if (llvm::Error Err = Cursor.JumpToBit(I.MacroStartOffset)) {
1962       Error(std::move(Err));
1963       return;
1964     }
1965 
1966     RecordData Record;
1967     while (true) {
1968       Expected<llvm::BitstreamEntry> MaybeE = Cursor.advanceSkippingSubblocks();
1969       if (!MaybeE) {
1970         Error(MaybeE.takeError());
1971         return;
1972       }
1973       llvm::BitstreamEntry E = MaybeE.get();
1974 
1975       switch (E.Kind) {
1976       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
1977       case llvm::BitstreamEntry::Error:
1978         Error("malformed block record in AST file");
1979         return;
1980       case llvm::BitstreamEntry::EndBlock:
1981         goto NextCursor;
1982 
1983       case llvm::BitstreamEntry::Record: {
1984         Record.clear();
1985         Expected<unsigned> MaybeRecord = Cursor.readRecord(E.ID, Record);
1986         if (!MaybeRecord) {
1987           Error(MaybeRecord.takeError());
1988           return;
1989         }
1990         switch (MaybeRecord.get()) {
1991         default:  // Default behavior: ignore.
1992           break;
1993 
1994         case PP_MACRO_OBJECT_LIKE:
1995         case PP_MACRO_FUNCTION_LIKE: {
1996           IdentifierInfo *II = getLocalIdentifier(I, Record[0]);
1997           if (II->isOutOfDate())
1998             updateOutOfDateIdentifier(*II);
1999           break;
2000         }
2001 
2002         case PP_TOKEN:
2003           // Ignore tokens.
2004           break;
2005         }
2006         break;
2007       }
2008       }
2009     }
2010     NextCursor:  ;
2011   }
2012 }
2013 
2014 namespace {
2015 
2016   /// Visitor class used to look up identifirs in an AST file.
2017   class IdentifierLookupVisitor {
2018     StringRef Name;
2019     unsigned NameHash;
2020     unsigned PriorGeneration;
2021     unsigned &NumIdentifierLookups;
2022     unsigned &NumIdentifierLookupHits;
2023     IdentifierInfo *Found = nullptr;
2024 
2025   public:
2026     IdentifierLookupVisitor(StringRef Name, unsigned PriorGeneration,
2027                             unsigned &NumIdentifierLookups,
2028                             unsigned &NumIdentifierLookupHits)
2029       : Name(Name), NameHash(ASTIdentifierLookupTrait::ComputeHash(Name)),
2030         PriorGeneration(PriorGeneration),
2031         NumIdentifierLookups(NumIdentifierLookups),
2032         NumIdentifierLookupHits(NumIdentifierLookupHits) {}
2033 
2034     bool operator()(ModuleFile &M) {
2035       // If we've already searched this module file, skip it now.
2036       if (M.Generation <= PriorGeneration)
2037         return true;
2038 
2039       ASTIdentifierLookupTable *IdTable
2040         = (ASTIdentifierLookupTable *)M.IdentifierLookupTable;
2041       if (!IdTable)
2042         return false;
2043 
2044       ASTIdentifierLookupTrait Trait(IdTable->getInfoObj().getReader(), M,
2045                                      Found);
2046       ++NumIdentifierLookups;
2047       ASTIdentifierLookupTable::iterator Pos =
2048           IdTable->find_hashed(Name, NameHash, &Trait);
2049       if (Pos == IdTable->end())
2050         return false;
2051 
2052       // Dereferencing the iterator has the effect of building the
2053       // IdentifierInfo node and populating it with the various
2054       // declarations it needs.
2055       ++NumIdentifierLookupHits;
2056       Found = *Pos;
2057       return true;
2058     }
2059 
2060     // Retrieve the identifier info found within the module
2061     // files.
2062     IdentifierInfo *getIdentifierInfo() const { return Found; }
2063   };
2064 
2065 } // namespace
2066 
2067 void ASTReader::updateOutOfDateIdentifier(IdentifierInfo &II) {
2068   // Note that we are loading an identifier.
2069   Deserializing AnIdentifier(this);
2070 
2071   unsigned PriorGeneration = 0;
2072   if (getContext().getLangOpts().Modules)
2073     PriorGeneration = IdentifierGeneration[&II];
2074 
2075   // If there is a global index, look there first to determine which modules
2076   // provably do not have any results for this identifier.
2077   GlobalModuleIndex::HitSet Hits;
2078   GlobalModuleIndex::HitSet *HitsPtr = nullptr;
2079   if (!loadGlobalIndex()) {
2080     if (GlobalIndex->lookupIdentifier(II.getName(), Hits)) {
2081       HitsPtr = &Hits;
2082     }
2083   }
2084 
2085   IdentifierLookupVisitor Visitor(II.getName(), PriorGeneration,
2086                                   NumIdentifierLookups,
2087                                   NumIdentifierLookupHits);
2088   ModuleMgr.visit(Visitor, HitsPtr);
2089   markIdentifierUpToDate(&II);
2090 }
2091 
2092 void ASTReader::markIdentifierUpToDate(IdentifierInfo *II) {
2093   if (!II)
2094     return;
2095 
2096   II->setOutOfDate(false);
2097 
2098   // Update the generation for this identifier.
2099   if (getContext().getLangOpts().Modules)
2100     IdentifierGeneration[II] = getGeneration();
2101 }
2102 
2103 void ASTReader::resolvePendingMacro(IdentifierInfo *II,
2104                                     const PendingMacroInfo &PMInfo) {
2105   ModuleFile &M = *PMInfo.M;
2106 
2107   BitstreamCursor &Cursor = M.MacroCursor;
2108   SavedStreamPosition SavedPosition(Cursor);
2109   if (llvm::Error Err =
2110           Cursor.JumpToBit(M.MacroOffsetsBase + PMInfo.MacroDirectivesOffset)) {
2111     Error(std::move(Err));
2112     return;
2113   }
2114 
2115   struct ModuleMacroRecord {
2116     SubmoduleID SubModID;
2117     MacroInfo *MI;
2118     SmallVector<SubmoduleID, 8> Overrides;
2119   };
2120   llvm::SmallVector<ModuleMacroRecord, 8> ModuleMacros;
2121 
2122   // We expect to see a sequence of PP_MODULE_MACRO records listing exported
2123   // macros, followed by a PP_MACRO_DIRECTIVE_HISTORY record with the complete
2124   // macro histroy.
2125   RecordData Record;
2126   while (true) {
2127     Expected<llvm::BitstreamEntry> MaybeEntry =
2128         Cursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
2129     if (!MaybeEntry) {
2130       Error(MaybeEntry.takeError());
2131       return;
2132     }
2133     llvm::BitstreamEntry Entry = MaybeEntry.get();
2134 
2135     if (Entry.Kind != llvm::BitstreamEntry::Record) {
2136       Error("malformed block record in AST file");
2137       return;
2138     }
2139 
2140     Record.clear();
2141     Expected<unsigned> MaybePP = Cursor.readRecord(Entry.ID, Record);
2142     if (!MaybePP) {
2143       Error(MaybePP.takeError());
2144       return;
2145     }
2146     switch ((PreprocessorRecordTypes)MaybePP.get()) {
2147     case PP_MACRO_DIRECTIVE_HISTORY:
2148       break;
2149 
2150     case PP_MODULE_MACRO: {
2151       ModuleMacros.push_back(ModuleMacroRecord());
2152       auto &Info = ModuleMacros.back();
2153       Info.SubModID = getGlobalSubmoduleID(M, Record[0]);
2154       Info.MI = getMacro(getGlobalMacroID(M, Record[1]));
2155       for (int I = 2, N = Record.size(); I != N; ++I)
2156         Info.Overrides.push_back(getGlobalSubmoduleID(M, Record[I]));
2157       continue;
2158     }
2159 
2160     default:
2161       Error("malformed block record in AST file");
2162       return;
2163     }
2164 
2165     // We found the macro directive history; that's the last record
2166     // for this macro.
2167     break;
2168   }
2169 
2170   // Module macros are listed in reverse dependency order.
2171   {
2172     std::reverse(ModuleMacros.begin(), ModuleMacros.end());
2173     llvm::SmallVector<ModuleMacro*, 8> Overrides;
2174     for (auto &MMR : ModuleMacros) {
2175       Overrides.clear();
2176       for (unsigned ModID : MMR.Overrides) {
2177         Module *Mod = getSubmodule(ModID);
2178         auto *Macro = PP.getModuleMacro(Mod, II);
2179         assert(Macro && "missing definition for overridden macro");
2180         Overrides.push_back(Macro);
2181       }
2182 
2183       bool Inserted = false;
2184       Module *Owner = getSubmodule(MMR.SubModID);
2185       PP.addModuleMacro(Owner, II, MMR.MI, Overrides, Inserted);
2186     }
2187   }
2188 
2189   // Don't read the directive history for a module; we don't have anywhere
2190   // to put it.
2191   if (M.isModule())
2192     return;
2193 
2194   // Deserialize the macro directives history in reverse source-order.
2195   MacroDirective *Latest = nullptr, *Earliest = nullptr;
2196   unsigned Idx = 0, N = Record.size();
2197   while (Idx < N) {
2198     MacroDirective *MD = nullptr;
2199     SourceLocation Loc = ReadSourceLocation(M, Record, Idx);
2200     MacroDirective::Kind K = (MacroDirective::Kind)Record[Idx++];
2201     switch (K) {
2202     case MacroDirective::MD_Define: {
2203       MacroInfo *MI = getMacro(getGlobalMacroID(M, Record[Idx++]));
2204       MD = PP.AllocateDefMacroDirective(MI, Loc);
2205       break;
2206     }
2207     case MacroDirective::MD_Undefine:
2208       MD = PP.AllocateUndefMacroDirective(Loc);
2209       break;
2210     case MacroDirective::MD_Visibility:
2211       bool isPublic = Record[Idx++];
2212       MD = PP.AllocateVisibilityMacroDirective(Loc, isPublic);
2213       break;
2214     }
2215 
2216     if (!Latest)
2217       Latest = MD;
2218     if (Earliest)
2219       Earliest->setPrevious(MD);
2220     Earliest = MD;
2221   }
2222 
2223   if (Latest)
2224     PP.setLoadedMacroDirective(II, Earliest, Latest);
2225 }
2226 
2227 ASTReader::InputFileInfo
2228 ASTReader::readInputFileInfo(ModuleFile &F, unsigned ID) {
2229   // Go find this input file.
2230   BitstreamCursor &Cursor = F.InputFilesCursor;
2231   SavedStreamPosition SavedPosition(Cursor);
2232   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2233     // FIXME this drops errors on the floor.
2234     consumeError(std::move(Err));
2235   }
2236 
2237   Expected<unsigned> MaybeCode = Cursor.ReadCode();
2238   if (!MaybeCode) {
2239     // FIXME this drops errors on the floor.
2240     consumeError(MaybeCode.takeError());
2241   }
2242   unsigned Code = MaybeCode.get();
2243   RecordData Record;
2244   StringRef Blob;
2245 
2246   if (Expected<unsigned> Maybe = Cursor.readRecord(Code, Record, &Blob))
2247     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE &&
2248            "invalid record type for input file");
2249   else {
2250     // FIXME this drops errors on the floor.
2251     consumeError(Maybe.takeError());
2252   }
2253 
2254   assert(Record[0] == ID && "Bogus stored ID or offset");
2255   InputFileInfo R;
2256   R.StoredSize = static_cast<off_t>(Record[1]);
2257   R.StoredTime = static_cast<time_t>(Record[2]);
2258   R.Overridden = static_cast<bool>(Record[3]);
2259   R.Transient = static_cast<bool>(Record[4]);
2260   R.TopLevelModuleMap = static_cast<bool>(Record[5]);
2261   R.Filename = std::string(Blob);
2262   ResolveImportedPath(F, R.Filename);
2263 
2264   Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
2265   if (!MaybeEntry) // FIXME this drops errors on the floor.
2266     consumeError(MaybeEntry.takeError());
2267   llvm::BitstreamEntry Entry = MaybeEntry.get();
2268   assert(Entry.Kind == llvm::BitstreamEntry::Record &&
2269          "expected record type for input file hash");
2270 
2271   Record.clear();
2272   if (Expected<unsigned> Maybe = Cursor.readRecord(Entry.ID, Record))
2273     assert(static_cast<InputFileRecordTypes>(Maybe.get()) == INPUT_FILE_HASH &&
2274            "invalid record type for input file hash");
2275   else {
2276     // FIXME this drops errors on the floor.
2277     consumeError(Maybe.takeError());
2278   }
2279   R.ContentHash = (static_cast<uint64_t>(Record[1]) << 32) |
2280                   static_cast<uint64_t>(Record[0]);
2281   return R;
2282 }
2283 
2284 static unsigned moduleKindForDiagnostic(ModuleKind Kind);
2285 InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2286   // If this ID is bogus, just return an empty input file.
2287   if (ID == 0 || ID > F.InputFilesLoaded.size())
2288     return InputFile();
2289 
2290   // If we've already loaded this input file, return it.
2291   if (F.InputFilesLoaded[ID-1].getFile())
2292     return F.InputFilesLoaded[ID-1];
2293 
2294   if (F.InputFilesLoaded[ID-1].isNotFound())
2295     return InputFile();
2296 
2297   // Go find this input file.
2298   BitstreamCursor &Cursor = F.InputFilesCursor;
2299   SavedStreamPosition SavedPosition(Cursor);
2300   if (llvm::Error Err = Cursor.JumpToBit(F.InputFileOffsets[ID - 1])) {
2301     // FIXME this drops errors on the floor.
2302     consumeError(std::move(Err));
2303   }
2304 
2305   InputFileInfo FI = readInputFileInfo(F, ID);
2306   off_t StoredSize = FI.StoredSize;
2307   time_t StoredTime = FI.StoredTime;
2308   bool Overridden = FI.Overridden;
2309   bool Transient = FI.Transient;
2310   StringRef Filename = FI.Filename;
2311   uint64_t StoredContentHash = FI.ContentHash;
2312 
2313   const FileEntry *File = nullptr;
2314   if (auto FE = FileMgr.getFile(Filename, /*OpenFile=*/false))
2315     File = *FE;
2316 
2317   // If we didn't find the file, resolve it relative to the
2318   // original directory from which this AST file was created.
2319   if (File == nullptr && !F.OriginalDir.empty() && !F.BaseDirectory.empty() &&
2320       F.OriginalDir != F.BaseDirectory) {
2321     std::string Resolved = resolveFileRelativeToOriginalDir(
2322         std::string(Filename), F.OriginalDir, F.BaseDirectory);
2323     if (!Resolved.empty())
2324       if (auto FE = FileMgr.getFile(Resolved))
2325         File = *FE;
2326   }
2327 
2328   // For an overridden file, create a virtual file with the stored
2329   // size/timestamp.
2330   if ((Overridden || Transient) && File == nullptr)
2331     File = FileMgr.getVirtualFile(Filename, StoredSize, StoredTime);
2332 
2333   if (File == nullptr) {
2334     if (Complain) {
2335       std::string ErrorStr = "could not find file '";
2336       ErrorStr += Filename;
2337       ErrorStr += "' referenced by AST file '";
2338       ErrorStr += F.FileName;
2339       ErrorStr += "'";
2340       Error(ErrorStr);
2341     }
2342     // Record that we didn't find the file.
2343     F.InputFilesLoaded[ID-1] = InputFile::getNotFound();
2344     return InputFile();
2345   }
2346 
2347   // Check if there was a request to override the contents of the file
2348   // that was part of the precompiled header. Overriding such a file
2349   // can lead to problems when lexing using the source locations from the
2350   // PCH.
2351   SourceManager &SM = getSourceManager();
2352   // FIXME: Reject if the overrides are different.
2353   if ((!Overridden && !Transient) && SM.isFileOverridden(File)) {
2354     if (Complain)
2355       Error(diag::err_fe_pch_file_overridden, Filename);
2356 
2357     // After emitting the diagnostic, bypass the overriding file to recover
2358     // (this creates a separate FileEntry).
2359     File = SM.bypassFileContentsOverride(*File);
2360     if (!File) {
2361       F.InputFilesLoaded[ID - 1] = InputFile::getNotFound();
2362       return InputFile();
2363     }
2364   }
2365 
2366   enum ModificationType {
2367     Size,
2368     ModTime,
2369     Content,
2370     None,
2371   };
2372   auto HasInputFileChanged = [&]() {
2373     if (StoredSize != File->getSize())
2374       return ModificationType::Size;
2375     if (!DisableValidation && StoredTime &&
2376         StoredTime != File->getModificationTime()) {
2377       // In case the modification time changes but not the content,
2378       // accept the cached file as legit.
2379       if (ValidateASTInputFilesContent &&
2380           StoredContentHash != static_cast<uint64_t>(llvm::hash_code(-1))) {
2381         auto MemBuffOrError = FileMgr.getBufferForFile(File);
2382         if (!MemBuffOrError) {
2383           if (!Complain)
2384             return ModificationType::ModTime;
2385           std::string ErrorStr = "could not get buffer for file '";
2386           ErrorStr += File->getName();
2387           ErrorStr += "'";
2388           Error(ErrorStr);
2389           return ModificationType::ModTime;
2390         }
2391 
2392         auto ContentHash = hash_value(MemBuffOrError.get()->getBuffer());
2393         if (StoredContentHash == static_cast<uint64_t>(ContentHash))
2394           return ModificationType::None;
2395         return ModificationType::Content;
2396       }
2397       return ModificationType::ModTime;
2398     }
2399     return ModificationType::None;
2400   };
2401 
2402   bool IsOutOfDate = false;
2403   auto FileChange = HasInputFileChanged();
2404   // For an overridden file, there is nothing to validate.
2405   if (!Overridden && FileChange != ModificationType::None) {
2406     if (Complain) {
2407       // Build a list of the PCH imports that got us here (in reverse).
2408       SmallVector<ModuleFile *, 4> ImportStack(1, &F);
2409       while (!ImportStack.back()->ImportedBy.empty())
2410         ImportStack.push_back(ImportStack.back()->ImportedBy[0]);
2411 
2412       // The top-level PCH is stale.
2413       StringRef TopLevelPCHName(ImportStack.back()->FileName);
2414       unsigned DiagnosticKind =
2415           moduleKindForDiagnostic(ImportStack.back()->Kind);
2416       if (DiagnosticKind == 0)
2417         Error(diag::err_fe_pch_file_modified, Filename, TopLevelPCHName,
2418               (unsigned)FileChange);
2419       else if (DiagnosticKind == 1)
2420         Error(diag::err_fe_module_file_modified, Filename, TopLevelPCHName,
2421               (unsigned)FileChange);
2422       else
2423         Error(diag::err_fe_ast_file_modified, Filename, TopLevelPCHName,
2424               (unsigned)FileChange);
2425 
2426       // Print the import stack.
2427       if (ImportStack.size() > 1 && !Diags.isDiagnosticInFlight()) {
2428         Diag(diag::note_pch_required_by)
2429           << Filename << ImportStack[0]->FileName;
2430         for (unsigned I = 1; I < ImportStack.size(); ++I)
2431           Diag(diag::note_pch_required_by)
2432             << ImportStack[I-1]->FileName << ImportStack[I]->FileName;
2433       }
2434 
2435       if (!Diags.isDiagnosticInFlight())
2436         Diag(diag::note_pch_rebuild_required) << TopLevelPCHName;
2437     }
2438 
2439     IsOutOfDate = true;
2440   }
2441   // FIXME: If the file is overridden and we've already opened it,
2442   // issue an error (or split it into a separate FileEntry).
2443 
2444   InputFile IF = InputFile(File, Overridden || Transient, IsOutOfDate);
2445 
2446   // Note that we've loaded this input file.
2447   F.InputFilesLoaded[ID-1] = IF;
2448   return IF;
2449 }
2450 
2451 /// If we are loading a relocatable PCH or module file, and the filename
2452 /// is not an absolute path, add the system or module root to the beginning of
2453 /// the file name.
2454 void ASTReader::ResolveImportedPath(ModuleFile &M, std::string &Filename) {
2455   // Resolve relative to the base directory, if we have one.
2456   if (!M.BaseDirectory.empty())
2457     return ResolveImportedPath(Filename, M.BaseDirectory);
2458 }
2459 
2460 void ASTReader::ResolveImportedPath(std::string &Filename, StringRef Prefix) {
2461   if (Filename.empty() || llvm::sys::path::is_absolute(Filename))
2462     return;
2463 
2464   SmallString<128> Buffer;
2465   llvm::sys::path::append(Buffer, Prefix, Filename);
2466   Filename.assign(Buffer.begin(), Buffer.end());
2467 }
2468 
2469 static bool isDiagnosedResult(ASTReader::ASTReadResult ARR, unsigned Caps) {
2470   switch (ARR) {
2471   case ASTReader::Failure: return true;
2472   case ASTReader::Missing: return !(Caps & ASTReader::ARR_Missing);
2473   case ASTReader::OutOfDate: return !(Caps & ASTReader::ARR_OutOfDate);
2474   case ASTReader::VersionMismatch: return !(Caps & ASTReader::ARR_VersionMismatch);
2475   case ASTReader::ConfigurationMismatch:
2476     return !(Caps & ASTReader::ARR_ConfigurationMismatch);
2477   case ASTReader::HadErrors: return true;
2478   case ASTReader::Success: return false;
2479   }
2480 
2481   llvm_unreachable("unknown ASTReadResult");
2482 }
2483 
2484 ASTReader::ASTReadResult ASTReader::ReadOptionsBlock(
2485     BitstreamCursor &Stream, unsigned ClientLoadCapabilities,
2486     bool AllowCompatibleConfigurationMismatch, ASTReaderListener &Listener,
2487     std::string &SuggestedPredefines) {
2488   if (llvm::Error Err = Stream.EnterSubBlock(OPTIONS_BLOCK_ID)) {
2489     // FIXME this drops errors on the floor.
2490     consumeError(std::move(Err));
2491     return Failure;
2492   }
2493 
2494   // Read all of the records in the options block.
2495   RecordData Record;
2496   ASTReadResult Result = Success;
2497   while (true) {
2498     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2499     if (!MaybeEntry) {
2500       // FIXME this drops errors on the floor.
2501       consumeError(MaybeEntry.takeError());
2502       return Failure;
2503     }
2504     llvm::BitstreamEntry Entry = MaybeEntry.get();
2505 
2506     switch (Entry.Kind) {
2507     case llvm::BitstreamEntry::Error:
2508     case llvm::BitstreamEntry::SubBlock:
2509       return Failure;
2510 
2511     case llvm::BitstreamEntry::EndBlock:
2512       return Result;
2513 
2514     case llvm::BitstreamEntry::Record:
2515       // The interesting case.
2516       break;
2517     }
2518 
2519     // Read and process a record.
2520     Record.clear();
2521     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
2522     if (!MaybeRecordType) {
2523       // FIXME this drops errors on the floor.
2524       consumeError(MaybeRecordType.takeError());
2525       return Failure;
2526     }
2527     switch ((OptionsRecordTypes)MaybeRecordType.get()) {
2528     case LANGUAGE_OPTIONS: {
2529       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2530       if (ParseLanguageOptions(Record, Complain, Listener,
2531                                AllowCompatibleConfigurationMismatch))
2532         Result = ConfigurationMismatch;
2533       break;
2534     }
2535 
2536     case TARGET_OPTIONS: {
2537       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2538       if (ParseTargetOptions(Record, Complain, Listener,
2539                              AllowCompatibleConfigurationMismatch))
2540         Result = ConfigurationMismatch;
2541       break;
2542     }
2543 
2544     case FILE_SYSTEM_OPTIONS: {
2545       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2546       if (!AllowCompatibleConfigurationMismatch &&
2547           ParseFileSystemOptions(Record, Complain, Listener))
2548         Result = ConfigurationMismatch;
2549       break;
2550     }
2551 
2552     case HEADER_SEARCH_OPTIONS: {
2553       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2554       if (!AllowCompatibleConfigurationMismatch &&
2555           ParseHeaderSearchOptions(Record, Complain, Listener))
2556         Result = ConfigurationMismatch;
2557       break;
2558     }
2559 
2560     case PREPROCESSOR_OPTIONS:
2561       bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0;
2562       if (!AllowCompatibleConfigurationMismatch &&
2563           ParsePreprocessorOptions(Record, Complain, Listener,
2564                                    SuggestedPredefines))
2565         Result = ConfigurationMismatch;
2566       break;
2567     }
2568   }
2569 }
2570 
2571 ASTReader::ASTReadResult
2572 ASTReader::ReadControlBlock(ModuleFile &F,
2573                             SmallVectorImpl<ImportedModule> &Loaded,
2574                             const ModuleFile *ImportedBy,
2575                             unsigned ClientLoadCapabilities) {
2576   BitstreamCursor &Stream = F.Stream;
2577 
2578   if (llvm::Error Err = Stream.EnterSubBlock(CONTROL_BLOCK_ID)) {
2579     Error(std::move(Err));
2580     return Failure;
2581   }
2582 
2583   // Lambda to read the unhashed control block the first time it's called.
2584   //
2585   // For PCM files, the unhashed control block cannot be read until after the
2586   // MODULE_NAME record.  However, PCH files have no MODULE_NAME, and yet still
2587   // need to look ahead before reading the IMPORTS record.  For consistency,
2588   // this block is always read somehow (see BitstreamEntry::EndBlock).
2589   bool HasReadUnhashedControlBlock = false;
2590   auto readUnhashedControlBlockOnce = [&]() {
2591     if (!HasReadUnhashedControlBlock) {
2592       HasReadUnhashedControlBlock = true;
2593       if (ASTReadResult Result =
2594               readUnhashedControlBlock(F, ImportedBy, ClientLoadCapabilities))
2595         return Result;
2596     }
2597     return Success;
2598   };
2599 
2600   // Read all of the records and blocks in the control block.
2601   RecordData Record;
2602   unsigned NumInputs = 0;
2603   unsigned NumUserInputs = 0;
2604   StringRef BaseDirectoryAsWritten;
2605   while (true) {
2606     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2607     if (!MaybeEntry) {
2608       Error(MaybeEntry.takeError());
2609       return Failure;
2610     }
2611     llvm::BitstreamEntry Entry = MaybeEntry.get();
2612 
2613     switch (Entry.Kind) {
2614     case llvm::BitstreamEntry::Error:
2615       Error("malformed block record in AST file");
2616       return Failure;
2617     case llvm::BitstreamEntry::EndBlock: {
2618       // Validate the module before returning.  This call catches an AST with
2619       // no module name and no imports.
2620       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2621         return Result;
2622 
2623       // Validate input files.
2624       const HeaderSearchOptions &HSOpts =
2625           PP.getHeaderSearchInfo().getHeaderSearchOpts();
2626 
2627       // All user input files reside at the index range [0, NumUserInputs), and
2628       // system input files reside at [NumUserInputs, NumInputs). For explicitly
2629       // loaded module files, ignore missing inputs.
2630       if (!DisableValidation && F.Kind != MK_ExplicitModule &&
2631           F.Kind != MK_PrebuiltModule) {
2632         bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
2633 
2634         // If we are reading a module, we will create a verification timestamp,
2635         // so we verify all input files.  Otherwise, verify only user input
2636         // files.
2637 
2638         unsigned N = NumUserInputs;
2639         if (ValidateSystemInputs ||
2640             (HSOpts.ModulesValidateOncePerBuildSession &&
2641              F.InputFilesValidationTimestamp <= HSOpts.BuildSessionTimestamp &&
2642              F.Kind == MK_ImplicitModule))
2643           N = NumInputs;
2644 
2645         for (unsigned I = 0; I < N; ++I) {
2646           InputFile IF = getInputFile(F, I+1, Complain);
2647           if (!IF.getFile() || IF.isOutOfDate())
2648             return OutOfDate;
2649         }
2650       }
2651 
2652       if (Listener)
2653         Listener->visitModuleFile(F.FileName, F.Kind);
2654 
2655       if (Listener && Listener->needsInputFileVisitation()) {
2656         unsigned N = Listener->needsSystemInputFileVisitation() ? NumInputs
2657                                                                 : NumUserInputs;
2658         for (unsigned I = 0; I < N; ++I) {
2659           bool IsSystem = I >= NumUserInputs;
2660           InputFileInfo FI = readInputFileInfo(F, I+1);
2661           Listener->visitInputFile(FI.Filename, IsSystem, FI.Overridden,
2662                                    F.Kind == MK_ExplicitModule ||
2663                                    F.Kind == MK_PrebuiltModule);
2664         }
2665       }
2666 
2667       return Success;
2668     }
2669 
2670     case llvm::BitstreamEntry::SubBlock:
2671       switch (Entry.ID) {
2672       case INPUT_FILES_BLOCK_ID:
2673         F.InputFilesCursor = Stream;
2674         if (llvm::Error Err = Stream.SkipBlock()) {
2675           Error(std::move(Err));
2676           return Failure;
2677         }
2678         if (ReadBlockAbbrevs(F.InputFilesCursor, INPUT_FILES_BLOCK_ID)) {
2679           Error("malformed block record in AST file");
2680           return Failure;
2681         }
2682         continue;
2683 
2684       case OPTIONS_BLOCK_ID:
2685         // If we're reading the first module for this group, check its options
2686         // are compatible with ours. For modules it imports, no further checking
2687         // is required, because we checked them when we built it.
2688         if (Listener && !ImportedBy) {
2689           // Should we allow the configuration of the module file to differ from
2690           // the configuration of the current translation unit in a compatible
2691           // way?
2692           //
2693           // FIXME: Allow this for files explicitly specified with -include-pch.
2694           bool AllowCompatibleConfigurationMismatch =
2695               F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
2696 
2697           ASTReadResult Result =
2698               ReadOptionsBlock(Stream, ClientLoadCapabilities,
2699                                AllowCompatibleConfigurationMismatch, *Listener,
2700                                SuggestedPredefines);
2701           if (Result == Failure) {
2702             Error("malformed block record in AST file");
2703             return Result;
2704           }
2705 
2706           if (DisableValidation ||
2707               (AllowConfigurationMismatch && Result == ConfigurationMismatch))
2708             Result = Success;
2709 
2710           // If we can't load the module, exit early since we likely
2711           // will rebuild the module anyway. The stream may be in the
2712           // middle of a block.
2713           if (Result != Success)
2714             return Result;
2715         } else if (llvm::Error Err = Stream.SkipBlock()) {
2716           Error(std::move(Err));
2717           return Failure;
2718         }
2719         continue;
2720 
2721       default:
2722         if (llvm::Error Err = Stream.SkipBlock()) {
2723           Error(std::move(Err));
2724           return Failure;
2725         }
2726         continue;
2727       }
2728 
2729     case llvm::BitstreamEntry::Record:
2730       // The interesting case.
2731       break;
2732     }
2733 
2734     // Read and process a record.
2735     Record.clear();
2736     StringRef Blob;
2737     Expected<unsigned> MaybeRecordType =
2738         Stream.readRecord(Entry.ID, Record, &Blob);
2739     if (!MaybeRecordType) {
2740       Error(MaybeRecordType.takeError());
2741       return Failure;
2742     }
2743     switch ((ControlRecordTypes)MaybeRecordType.get()) {
2744     case METADATA: {
2745       if (Record[0] != VERSION_MAJOR && !DisableValidation) {
2746         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2747           Diag(Record[0] < VERSION_MAJOR? diag::err_pch_version_too_old
2748                                         : diag::err_pch_version_too_new);
2749         return VersionMismatch;
2750       }
2751 
2752       bool hasErrors = Record[6];
2753       if (hasErrors && !DisableValidation && !AllowASTWithCompilerErrors) {
2754         Diag(diag::err_pch_with_compiler_errors);
2755         return HadErrors;
2756       }
2757       if (hasErrors) {
2758         Diags.ErrorOccurred = true;
2759         Diags.UncompilableErrorOccurred = true;
2760         Diags.UnrecoverableErrorOccurred = true;
2761       }
2762 
2763       F.RelocatablePCH = Record[4];
2764       // Relative paths in a relocatable PCH are relative to our sysroot.
2765       if (F.RelocatablePCH)
2766         F.BaseDirectory = isysroot.empty() ? "/" : isysroot;
2767 
2768       F.HasTimestamps = Record[5];
2769 
2770       const std::string &CurBranch = getClangFullRepositoryVersion();
2771       StringRef ASTBranch = Blob;
2772       if (StringRef(CurBranch) != ASTBranch && !DisableValidation) {
2773         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
2774           Diag(diag::err_pch_different_branch) << ASTBranch << CurBranch;
2775         return VersionMismatch;
2776       }
2777       break;
2778     }
2779 
2780     case IMPORTS: {
2781       // Validate the AST before processing any imports (otherwise, untangling
2782       // them can be error-prone and expensive).  A module will have a name and
2783       // will already have been validated, but this catches the PCH case.
2784       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2785         return Result;
2786 
2787       // Load each of the imported PCH files.
2788       unsigned Idx = 0, N = Record.size();
2789       while (Idx < N) {
2790         // Read information about the AST file.
2791         ModuleKind ImportedKind = (ModuleKind)Record[Idx++];
2792         // The import location will be the local one for now; we will adjust
2793         // all import locations of module imports after the global source
2794         // location info are setup, in ReadAST.
2795         SourceLocation ImportLoc =
2796             ReadUntranslatedSourceLocation(Record[Idx++]);
2797         off_t StoredSize = (off_t)Record[Idx++];
2798         time_t StoredModTime = (time_t)Record[Idx++];
2799         auto FirstSignatureByte = Record.begin() + Idx;
2800         ASTFileSignature StoredSignature = ASTFileSignature::create(
2801             FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
2802         Idx += ASTFileSignature::size;
2803 
2804         std::string ImportedName = ReadString(Record, Idx);
2805         std::string ImportedFile;
2806 
2807         // For prebuilt and explicit modules first consult the file map for
2808         // an override. Note that here we don't search prebuilt module
2809         // directories, only the explicit name to file mappings. Also, we will
2810         // still verify the size/signature making sure it is essentially the
2811         // same file but perhaps in a different location.
2812         if (ImportedKind == MK_PrebuiltModule || ImportedKind == MK_ExplicitModule)
2813           ImportedFile = PP.getHeaderSearchInfo().getPrebuiltModuleFileName(
2814             ImportedName, /*FileMapOnly*/ true);
2815 
2816         if (ImportedFile.empty())
2817           // Use BaseDirectoryAsWritten to ensure we use the same path in the
2818           // ModuleCache as when writing.
2819           ImportedFile = ReadPath(BaseDirectoryAsWritten, Record, Idx);
2820         else
2821           SkipPath(Record, Idx);
2822 
2823         // If our client can't cope with us being out of date, we can't cope with
2824         // our dependency being missing.
2825         unsigned Capabilities = ClientLoadCapabilities;
2826         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2827           Capabilities &= ~ARR_Missing;
2828 
2829         // Load the AST file.
2830         auto Result = ReadASTCore(ImportedFile, ImportedKind, ImportLoc, &F,
2831                                   Loaded, StoredSize, StoredModTime,
2832                                   StoredSignature, Capabilities);
2833 
2834         // If we diagnosed a problem, produce a backtrace.
2835         if (isDiagnosedResult(Result, Capabilities))
2836           Diag(diag::note_module_file_imported_by)
2837               << F.FileName << !F.ModuleName.empty() << F.ModuleName;
2838 
2839         switch (Result) {
2840         case Failure: return Failure;
2841           // If we have to ignore the dependency, we'll have to ignore this too.
2842         case Missing:
2843         case OutOfDate: return OutOfDate;
2844         case VersionMismatch: return VersionMismatch;
2845         case ConfigurationMismatch: return ConfigurationMismatch;
2846         case HadErrors: return HadErrors;
2847         case Success: break;
2848         }
2849       }
2850       break;
2851     }
2852 
2853     case ORIGINAL_FILE:
2854       F.OriginalSourceFileID = FileID::get(Record[0]);
2855       F.ActualOriginalSourceFileName = std::string(Blob);
2856       F.OriginalSourceFileName = F.ActualOriginalSourceFileName;
2857       ResolveImportedPath(F, F.OriginalSourceFileName);
2858       break;
2859 
2860     case ORIGINAL_FILE_ID:
2861       F.OriginalSourceFileID = FileID::get(Record[0]);
2862       break;
2863 
2864     case ORIGINAL_PCH_DIR:
2865       F.OriginalDir = std::string(Blob);
2866       break;
2867 
2868     case MODULE_NAME:
2869       F.ModuleName = std::string(Blob);
2870       Diag(diag::remark_module_import)
2871           << F.ModuleName << F.FileName << (ImportedBy ? true : false)
2872           << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
2873       if (Listener)
2874         Listener->ReadModuleName(F.ModuleName);
2875 
2876       // Validate the AST as soon as we have a name so we can exit early on
2877       // failure.
2878       if (ASTReadResult Result = readUnhashedControlBlockOnce())
2879         return Result;
2880 
2881       break;
2882 
2883     case MODULE_DIRECTORY: {
2884       // Save the BaseDirectory as written in the PCM for computing the module
2885       // filename for the ModuleCache.
2886       BaseDirectoryAsWritten = Blob;
2887       assert(!F.ModuleName.empty() &&
2888              "MODULE_DIRECTORY found before MODULE_NAME");
2889       // If we've already loaded a module map file covering this module, we may
2890       // have a better path for it (relative to the current build).
2891       Module *M = PP.getHeaderSearchInfo().lookupModule(
2892           F.ModuleName, /*AllowSearch*/ true,
2893           /*AllowExtraModuleMapSearch*/ true);
2894       if (M && M->Directory) {
2895         // If we're implicitly loading a module, the base directory can't
2896         // change between the build and use.
2897         // Don't emit module relocation error if we have -fno-validate-pch
2898         if (!PP.getPreprocessorOpts().DisablePCHValidation &&
2899             F.Kind != MK_ExplicitModule && F.Kind != MK_PrebuiltModule) {
2900           auto BuildDir = PP.getFileManager().getDirectory(Blob);
2901           if (!BuildDir || *BuildDir != M->Directory) {
2902             if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
2903               Diag(diag::err_imported_module_relocated)
2904                   << F.ModuleName << Blob << M->Directory->getName();
2905             return OutOfDate;
2906           }
2907         }
2908         F.BaseDirectory = std::string(M->Directory->getName());
2909       } else {
2910         F.BaseDirectory = std::string(Blob);
2911       }
2912       break;
2913     }
2914 
2915     case MODULE_MAP_FILE:
2916       if (ASTReadResult Result =
2917               ReadModuleMapFileBlock(Record, F, ImportedBy, ClientLoadCapabilities))
2918         return Result;
2919       break;
2920 
2921     case INPUT_FILE_OFFSETS:
2922       NumInputs = Record[0];
2923       NumUserInputs = Record[1];
2924       F.InputFileOffsets =
2925           (const llvm::support::unaligned_uint64_t *)Blob.data();
2926       F.InputFilesLoaded.resize(NumInputs);
2927       F.NumUserInputFiles = NumUserInputs;
2928       break;
2929     }
2930   }
2931 }
2932 
2933 ASTReader::ASTReadResult
2934 ASTReader::ReadASTBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
2935   BitstreamCursor &Stream = F.Stream;
2936 
2937   if (llvm::Error Err = Stream.EnterSubBlock(AST_BLOCK_ID)) {
2938     Error(std::move(Err));
2939     return Failure;
2940   }
2941   F.ASTBlockStartOffset = Stream.GetCurrentBitNo();
2942 
2943   // Read all of the records and blocks for the AST file.
2944   RecordData Record;
2945   while (true) {
2946     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
2947     if (!MaybeEntry) {
2948       Error(MaybeEntry.takeError());
2949       return Failure;
2950     }
2951     llvm::BitstreamEntry Entry = MaybeEntry.get();
2952 
2953     switch (Entry.Kind) {
2954     case llvm::BitstreamEntry::Error:
2955       Error("error at end of module block in AST file");
2956       return Failure;
2957     case llvm::BitstreamEntry::EndBlock:
2958       // Outside of C++, we do not store a lookup map for the translation unit.
2959       // Instead, mark it as needing a lookup map to be built if this module
2960       // contains any declarations lexically within it (which it always does!).
2961       // This usually has no cost, since we very rarely need the lookup map for
2962       // the translation unit outside C++.
2963       if (ASTContext *Ctx = ContextObj) {
2964         DeclContext *DC = Ctx->getTranslationUnitDecl();
2965         if (DC->hasExternalLexicalStorage() && !Ctx->getLangOpts().CPlusPlus)
2966           DC->setMustBuildLookupTable();
2967       }
2968 
2969       return Success;
2970     case llvm::BitstreamEntry::SubBlock:
2971       switch (Entry.ID) {
2972       case DECLTYPES_BLOCK_ID:
2973         // We lazily load the decls block, but we want to set up the
2974         // DeclsCursor cursor to point into it.  Clone our current bitcode
2975         // cursor to it, enter the block and read the abbrevs in that block.
2976         // With the main cursor, we just skip over it.
2977         F.DeclsCursor = Stream;
2978         if (llvm::Error Err = Stream.SkipBlock()) {
2979           Error(std::move(Err));
2980           return Failure;
2981         }
2982         if (ReadBlockAbbrevs(F.DeclsCursor, DECLTYPES_BLOCK_ID,
2983                              &F.DeclsBlockStartOffset)) {
2984           Error("malformed block record in AST file");
2985           return Failure;
2986         }
2987         break;
2988 
2989       case PREPROCESSOR_BLOCK_ID:
2990         F.MacroCursor = Stream;
2991         if (!PP.getExternalSource())
2992           PP.setExternalSource(this);
2993 
2994         if (llvm::Error Err = Stream.SkipBlock()) {
2995           Error(std::move(Err));
2996           return Failure;
2997         }
2998         if (ReadBlockAbbrevs(F.MacroCursor, PREPROCESSOR_BLOCK_ID)) {
2999           Error("malformed block record in AST file");
3000           return Failure;
3001         }
3002         F.MacroStartOffset = F.MacroCursor.GetCurrentBitNo();
3003         break;
3004 
3005       case PREPROCESSOR_DETAIL_BLOCK_ID:
3006         F.PreprocessorDetailCursor = Stream;
3007 
3008         if (llvm::Error Err = Stream.SkipBlock()) {
3009           Error(std::move(Err));
3010           return Failure;
3011         }
3012         if (ReadBlockAbbrevs(F.PreprocessorDetailCursor,
3013                              PREPROCESSOR_DETAIL_BLOCK_ID)) {
3014           Error("malformed preprocessor detail record in AST file");
3015           return Failure;
3016         }
3017         F.PreprocessorDetailStartOffset
3018         = F.PreprocessorDetailCursor.GetCurrentBitNo();
3019 
3020         if (!PP.getPreprocessingRecord())
3021           PP.createPreprocessingRecord();
3022         if (!PP.getPreprocessingRecord()->getExternalSource())
3023           PP.getPreprocessingRecord()->SetExternalSource(*this);
3024         break;
3025 
3026       case SOURCE_MANAGER_BLOCK_ID:
3027         if (ReadSourceManagerBlock(F))
3028           return Failure;
3029         break;
3030 
3031       case SUBMODULE_BLOCK_ID:
3032         if (ASTReadResult Result =
3033                 ReadSubmoduleBlock(F, ClientLoadCapabilities))
3034           return Result;
3035         break;
3036 
3037       case COMMENTS_BLOCK_ID: {
3038         BitstreamCursor C = Stream;
3039 
3040         if (llvm::Error Err = Stream.SkipBlock()) {
3041           Error(std::move(Err));
3042           return Failure;
3043         }
3044         if (ReadBlockAbbrevs(C, COMMENTS_BLOCK_ID)) {
3045           Error("malformed comments block in AST file");
3046           return Failure;
3047         }
3048         CommentsCursors.push_back(std::make_pair(C, &F));
3049         break;
3050       }
3051 
3052       default:
3053         if (llvm::Error Err = Stream.SkipBlock()) {
3054           Error(std::move(Err));
3055           return Failure;
3056         }
3057         break;
3058       }
3059       continue;
3060 
3061     case llvm::BitstreamEntry::Record:
3062       // The interesting case.
3063       break;
3064     }
3065 
3066     // Read and process a record.
3067     Record.clear();
3068     StringRef Blob;
3069     Expected<unsigned> MaybeRecordType =
3070         Stream.readRecord(Entry.ID, Record, &Blob);
3071     if (!MaybeRecordType) {
3072       Error(MaybeRecordType.takeError());
3073       return Failure;
3074     }
3075     ASTRecordTypes RecordType = (ASTRecordTypes)MaybeRecordType.get();
3076 
3077     // If we're not loading an AST context, we don't care about most records.
3078     if (!ContextObj) {
3079       switch (RecordType) {
3080       case IDENTIFIER_TABLE:
3081       case IDENTIFIER_OFFSET:
3082       case INTERESTING_IDENTIFIERS:
3083       case STATISTICS:
3084       case PP_CONDITIONAL_STACK:
3085       case PP_COUNTER_VALUE:
3086       case SOURCE_LOCATION_OFFSETS:
3087       case MODULE_OFFSET_MAP:
3088       case SOURCE_MANAGER_LINE_TABLE:
3089       case SOURCE_LOCATION_PRELOADS:
3090       case PPD_ENTITIES_OFFSETS:
3091       case HEADER_SEARCH_TABLE:
3092       case IMPORTED_MODULES:
3093       case MACRO_OFFSET:
3094         break;
3095       default:
3096         continue;
3097       }
3098     }
3099 
3100     switch (RecordType) {
3101     default:  // Default behavior: ignore.
3102       break;
3103 
3104     case TYPE_OFFSET: {
3105       if (F.LocalNumTypes != 0) {
3106         Error("duplicate TYPE_OFFSET record in AST file");
3107         return Failure;
3108       }
3109       F.TypeOffsets = reinterpret_cast<const UnderalignedInt64 *>(Blob.data());
3110       F.LocalNumTypes = Record[0];
3111       unsigned LocalBaseTypeIndex = Record[1];
3112       F.BaseTypeIndex = getTotalNumTypes();
3113 
3114       if (F.LocalNumTypes > 0) {
3115         // Introduce the global -> local mapping for types within this module.
3116         GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3117 
3118         // Introduce the local -> global mapping for types within this module.
3119         F.TypeRemap.insertOrReplace(
3120           std::make_pair(LocalBaseTypeIndex,
3121                          F.BaseTypeIndex - LocalBaseTypeIndex));
3122 
3123         TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3124       }
3125       break;
3126     }
3127 
3128     case DECL_OFFSET: {
3129       if (F.LocalNumDecls != 0) {
3130         Error("duplicate DECL_OFFSET record in AST file");
3131         return Failure;
3132       }
3133       F.DeclOffsets = (const DeclOffset *)Blob.data();
3134       F.LocalNumDecls = Record[0];
3135       unsigned LocalBaseDeclID = Record[1];
3136       F.BaseDeclID = getTotalNumDecls();
3137 
3138       if (F.LocalNumDecls > 0) {
3139         // Introduce the global -> local mapping for declarations within this
3140         // module.
3141         GlobalDeclMap.insert(
3142           std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F));
3143 
3144         // Introduce the local -> global mapping for declarations within this
3145         // module.
3146         F.DeclRemap.insertOrReplace(
3147           std::make_pair(LocalBaseDeclID, F.BaseDeclID - LocalBaseDeclID));
3148 
3149         // Introduce the global -> local mapping for declarations within this
3150         // module.
3151         F.GlobalToLocalDeclIDs[&F] = LocalBaseDeclID;
3152 
3153         DeclsLoaded.resize(DeclsLoaded.size() + F.LocalNumDecls);
3154       }
3155       break;
3156     }
3157 
3158     case TU_UPDATE_LEXICAL: {
3159       DeclContext *TU = ContextObj->getTranslationUnitDecl();
3160       LexicalContents Contents(
3161           reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3162               Blob.data()),
3163           static_cast<unsigned int>(Blob.size() / 4));
3164       TULexicalDecls.push_back(std::make_pair(&F, Contents));
3165       TU->setHasExternalLexicalStorage(true);
3166       break;
3167     }
3168 
3169     case UPDATE_VISIBLE: {
3170       unsigned Idx = 0;
3171       serialization::DeclID ID = ReadDeclID(F, Record, Idx);
3172       auto *Data = (const unsigned char*)Blob.data();
3173       PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data});
3174       // If we've already loaded the decl, perform the updates when we finish
3175       // loading this block.
3176       if (Decl *D = GetExistingDecl(ID))
3177         PendingUpdateRecords.push_back(
3178             PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3179       break;
3180     }
3181 
3182     case IDENTIFIER_TABLE:
3183       F.IdentifierTableData = Blob.data();
3184       if (Record[0]) {
3185         F.IdentifierLookupTable = ASTIdentifierLookupTable::Create(
3186             (const unsigned char *)F.IdentifierTableData + Record[0],
3187             (const unsigned char *)F.IdentifierTableData + sizeof(uint32_t),
3188             (const unsigned char *)F.IdentifierTableData,
3189             ASTIdentifierLookupTrait(*this, F));
3190 
3191         PP.getIdentifierTable().setExternalIdentifierLookup(this);
3192       }
3193       break;
3194 
3195     case IDENTIFIER_OFFSET: {
3196       if (F.LocalNumIdentifiers != 0) {
3197         Error("duplicate IDENTIFIER_OFFSET record in AST file");
3198         return Failure;
3199       }
3200       F.IdentifierOffsets = (const uint32_t *)Blob.data();
3201       F.LocalNumIdentifiers = Record[0];
3202       unsigned LocalBaseIdentifierID = Record[1];
3203       F.BaseIdentifierID = getTotalNumIdentifiers();
3204 
3205       if (F.LocalNumIdentifiers > 0) {
3206         // Introduce the global -> local mapping for identifiers within this
3207         // module.
3208         GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3209                                                   &F));
3210 
3211         // Introduce the local -> global mapping for identifiers within this
3212         // module.
3213         F.IdentifierRemap.insertOrReplace(
3214           std::make_pair(LocalBaseIdentifierID,
3215                          F.BaseIdentifierID - LocalBaseIdentifierID));
3216 
3217         IdentifiersLoaded.resize(IdentifiersLoaded.size()
3218                                  + F.LocalNumIdentifiers);
3219       }
3220       break;
3221     }
3222 
3223     case INTERESTING_IDENTIFIERS:
3224       F.PreloadIdentifierOffsets.assign(Record.begin(), Record.end());
3225       break;
3226 
3227     case EAGERLY_DESERIALIZED_DECLS:
3228       // FIXME: Skip reading this record if our ASTConsumer doesn't care
3229       // about "interesting" decls (for instance, if we're building a module).
3230       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3231         EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3232       break;
3233 
3234     case MODULAR_CODEGEN_DECLS:
3235       // FIXME: Skip reading this record if our ASTConsumer doesn't care about
3236       // them (ie: if we're not codegenerating this module).
3237       if (F.Kind == MK_MainFile ||
3238           getContext().getLangOpts().BuildingPCHWithObjectFile)
3239         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3240           EagerlyDeserializedDecls.push_back(getGlobalDeclID(F, Record[I]));
3241       break;
3242 
3243     case SPECIAL_TYPES:
3244       if (SpecialTypes.empty()) {
3245         for (unsigned I = 0, N = Record.size(); I != N; ++I)
3246           SpecialTypes.push_back(getGlobalTypeID(F, Record[I]));
3247         break;
3248       }
3249 
3250       if (SpecialTypes.size() != Record.size()) {
3251         Error("invalid special-types record");
3252         return Failure;
3253       }
3254 
3255       for (unsigned I = 0, N = Record.size(); I != N; ++I) {
3256         serialization::TypeID ID = getGlobalTypeID(F, Record[I]);
3257         if (!SpecialTypes[I])
3258           SpecialTypes[I] = ID;
3259         // FIXME: If ID && SpecialTypes[I] != ID, do we need a separate
3260         // merge step?
3261       }
3262       break;
3263 
3264     case STATISTICS:
3265       TotalNumStatements += Record[0];
3266       TotalNumMacros += Record[1];
3267       TotalLexicalDeclContexts += Record[2];
3268       TotalVisibleDeclContexts += Record[3];
3269       break;
3270 
3271     case UNUSED_FILESCOPED_DECLS:
3272       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3273         UnusedFileScopedDecls.push_back(getGlobalDeclID(F, Record[I]));
3274       break;
3275 
3276     case DELEGATING_CTORS:
3277       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3278         DelegatingCtorDecls.push_back(getGlobalDeclID(F, Record[I]));
3279       break;
3280 
3281     case WEAK_UNDECLARED_IDENTIFIERS:
3282       if (Record.size() % 4 != 0) {
3283         Error("invalid weak identifiers record");
3284         return Failure;
3285       }
3286 
3287       // FIXME: Ignore weak undeclared identifiers from non-original PCH
3288       // files. This isn't the way to do it :)
3289       WeakUndeclaredIdentifiers.clear();
3290 
3291       // Translate the weak, undeclared identifiers into global IDs.
3292       for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
3293         WeakUndeclaredIdentifiers.push_back(
3294           getGlobalIdentifierID(F, Record[I++]));
3295         WeakUndeclaredIdentifiers.push_back(
3296           getGlobalIdentifierID(F, Record[I++]));
3297         WeakUndeclaredIdentifiers.push_back(
3298           ReadSourceLocation(F, Record, I).getRawEncoding());
3299         WeakUndeclaredIdentifiers.push_back(Record[I++]);
3300       }
3301       break;
3302 
3303     case SELECTOR_OFFSETS: {
3304       F.SelectorOffsets = (const uint32_t *)Blob.data();
3305       F.LocalNumSelectors = Record[0];
3306       unsigned LocalBaseSelectorID = Record[1];
3307       F.BaseSelectorID = getTotalNumSelectors();
3308 
3309       if (F.LocalNumSelectors > 0) {
3310         // Introduce the global -> local mapping for selectors within this
3311         // module.
3312         GlobalSelectorMap.insert(std::make_pair(getTotalNumSelectors()+1, &F));
3313 
3314         // Introduce the local -> global mapping for selectors within this
3315         // module.
3316         F.SelectorRemap.insertOrReplace(
3317           std::make_pair(LocalBaseSelectorID,
3318                          F.BaseSelectorID - LocalBaseSelectorID));
3319 
3320         SelectorsLoaded.resize(SelectorsLoaded.size() + F.LocalNumSelectors);
3321       }
3322       break;
3323     }
3324 
3325     case METHOD_POOL:
3326       F.SelectorLookupTableData = (const unsigned char *)Blob.data();
3327       if (Record[0])
3328         F.SelectorLookupTable
3329           = ASTSelectorLookupTable::Create(
3330                         F.SelectorLookupTableData + Record[0],
3331                         F.SelectorLookupTableData,
3332                         ASTSelectorLookupTrait(*this, F));
3333       TotalNumMethodPoolEntries += Record[1];
3334       break;
3335 
3336     case REFERENCED_SELECTOR_POOL:
3337       if (!Record.empty()) {
3338         for (unsigned Idx = 0, N = Record.size() - 1; Idx < N; /* in loop */) {
3339           ReferencedSelectorsData.push_back(getGlobalSelectorID(F,
3340                                                                 Record[Idx++]));
3341           ReferencedSelectorsData.push_back(ReadSourceLocation(F, Record, Idx).
3342                                               getRawEncoding());
3343         }
3344       }
3345       break;
3346 
3347     case PP_CONDITIONAL_STACK:
3348       if (!Record.empty()) {
3349         unsigned Idx = 0, End = Record.size() - 1;
3350         bool ReachedEOFWhileSkipping = Record[Idx++];
3351         llvm::Optional<Preprocessor::PreambleSkipInfo> SkipInfo;
3352         if (ReachedEOFWhileSkipping) {
3353           SourceLocation HashToken = ReadSourceLocation(F, Record, Idx);
3354           SourceLocation IfTokenLoc = ReadSourceLocation(F, Record, Idx);
3355           bool FoundNonSkipPortion = Record[Idx++];
3356           bool FoundElse = Record[Idx++];
3357           SourceLocation ElseLoc = ReadSourceLocation(F, Record, Idx);
3358           SkipInfo.emplace(HashToken, IfTokenLoc, FoundNonSkipPortion,
3359                            FoundElse, ElseLoc);
3360         }
3361         SmallVector<PPConditionalInfo, 4> ConditionalStack;
3362         while (Idx < End) {
3363           auto Loc = ReadSourceLocation(F, Record, Idx);
3364           bool WasSkipping = Record[Idx++];
3365           bool FoundNonSkip = Record[Idx++];
3366           bool FoundElse = Record[Idx++];
3367           ConditionalStack.push_back(
3368               {Loc, WasSkipping, FoundNonSkip, FoundElse});
3369         }
3370         PP.setReplayablePreambleConditionalStack(ConditionalStack, SkipInfo);
3371       }
3372       break;
3373 
3374     case PP_COUNTER_VALUE:
3375       if (!Record.empty() && Listener)
3376         Listener->ReadCounter(F, Record[0]);
3377       break;
3378 
3379     case FILE_SORTED_DECLS:
3380       F.FileSortedDecls = (const DeclID *)Blob.data();
3381       F.NumFileSortedDecls = Record[0];
3382       break;
3383 
3384     case SOURCE_LOCATION_OFFSETS: {
3385       F.SLocEntryOffsets = (const uint32_t *)Blob.data();
3386       F.LocalNumSLocEntries = Record[0];
3387       unsigned SLocSpaceSize = Record[1];
3388       F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset;
3389       std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) =
3390           SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries,
3391                                               SLocSpaceSize);
3392       if (!F.SLocEntryBaseID) {
3393         Error("ran out of source locations");
3394         break;
3395       }
3396       // Make our entry in the range map. BaseID is negative and growing, so
3397       // we invert it. Because we invert it, though, we need the other end of
3398       // the range.
3399       unsigned RangeStart =
3400           unsigned(-F.SLocEntryBaseID) - F.LocalNumSLocEntries + 1;
3401       GlobalSLocEntryMap.insert(std::make_pair(RangeStart, &F));
3402       F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset);
3403 
3404       // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing.
3405       assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0);
3406       GlobalSLocOffsetMap.insert(
3407           std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset
3408                            - SLocSpaceSize,&F));
3409 
3410       // Initialize the remapping table.
3411       // Invalid stays invalid.
3412       F.SLocRemap.insertOrReplace(std::make_pair(0U, 0));
3413       // This module. Base was 2 when being compiled.
3414       F.SLocRemap.insertOrReplace(std::make_pair(2U,
3415                                   static_cast<int>(F.SLocEntryBaseOffset - 2)));
3416 
3417       TotalNumSLocEntries += F.LocalNumSLocEntries;
3418       break;
3419     }
3420 
3421     case MODULE_OFFSET_MAP:
3422       F.ModuleOffsetMap = Blob;
3423       break;
3424 
3425     case SOURCE_MANAGER_LINE_TABLE:
3426       if (ParseLineTable(F, Record)) {
3427         Error("malformed SOURCE_MANAGER_LINE_TABLE in AST file");
3428         return Failure;
3429       }
3430       break;
3431 
3432     case SOURCE_LOCATION_PRELOADS: {
3433       // Need to transform from the local view (1-based IDs) to the global view,
3434       // which is based off F.SLocEntryBaseID.
3435       if (!F.PreloadSLocEntries.empty()) {
3436         Error("Multiple SOURCE_LOCATION_PRELOADS records in AST file");
3437         return Failure;
3438       }
3439 
3440       F.PreloadSLocEntries.swap(Record);
3441       break;
3442     }
3443 
3444     case EXT_VECTOR_DECLS:
3445       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3446         ExtVectorDecls.push_back(getGlobalDeclID(F, Record[I]));
3447       break;
3448 
3449     case VTABLE_USES:
3450       if (Record.size() % 3 != 0) {
3451         Error("Invalid VTABLE_USES record");
3452         return Failure;
3453       }
3454 
3455       // Later tables overwrite earlier ones.
3456       // FIXME: Modules will have some trouble with this. This is clearly not
3457       // the right way to do this.
3458       VTableUses.clear();
3459 
3460       for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) {
3461         VTableUses.push_back(getGlobalDeclID(F, Record[Idx++]));
3462         VTableUses.push_back(
3463           ReadSourceLocation(F, Record, Idx).getRawEncoding());
3464         VTableUses.push_back(Record[Idx++]);
3465       }
3466       break;
3467 
3468     case PENDING_IMPLICIT_INSTANTIATIONS:
3469       if (PendingInstantiations.size() % 2 != 0) {
3470         Error("Invalid existing PendingInstantiations");
3471         return Failure;
3472       }
3473 
3474       if (Record.size() % 2 != 0) {
3475         Error("Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
3476         return Failure;
3477       }
3478 
3479       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3480         PendingInstantiations.push_back(getGlobalDeclID(F, Record[I++]));
3481         PendingInstantiations.push_back(
3482           ReadSourceLocation(F, Record, I).getRawEncoding());
3483       }
3484       break;
3485 
3486     case SEMA_DECL_REFS:
3487       if (Record.size() != 3) {
3488         Error("Invalid SEMA_DECL_REFS block");
3489         return Failure;
3490       }
3491       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3492         SemaDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3493       break;
3494 
3495     case PPD_ENTITIES_OFFSETS: {
3496       F.PreprocessedEntityOffsets = (const PPEntityOffset *)Blob.data();
3497       assert(Blob.size() % sizeof(PPEntityOffset) == 0);
3498       F.NumPreprocessedEntities = Blob.size() / sizeof(PPEntityOffset);
3499 
3500       unsigned LocalBasePreprocessedEntityID = Record[0];
3501 
3502       unsigned StartingID;
3503       if (!PP.getPreprocessingRecord())
3504         PP.createPreprocessingRecord();
3505       if (!PP.getPreprocessingRecord()->getExternalSource())
3506         PP.getPreprocessingRecord()->SetExternalSource(*this);
3507       StartingID
3508         = PP.getPreprocessingRecord()
3509             ->allocateLoadedEntities(F.NumPreprocessedEntities);
3510       F.BasePreprocessedEntityID = StartingID;
3511 
3512       if (F.NumPreprocessedEntities > 0) {
3513         // Introduce the global -> local mapping for preprocessed entities in
3514         // this module.
3515         GlobalPreprocessedEntityMap.insert(std::make_pair(StartingID, &F));
3516 
3517         // Introduce the local -> global mapping for preprocessed entities in
3518         // this module.
3519         F.PreprocessedEntityRemap.insertOrReplace(
3520           std::make_pair(LocalBasePreprocessedEntityID,
3521             F.BasePreprocessedEntityID - LocalBasePreprocessedEntityID));
3522       }
3523 
3524       break;
3525     }
3526 
3527     case PPD_SKIPPED_RANGES: {
3528       F.PreprocessedSkippedRangeOffsets = (const PPSkippedRange*)Blob.data();
3529       assert(Blob.size() % sizeof(PPSkippedRange) == 0);
3530       F.NumPreprocessedSkippedRanges = Blob.size() / sizeof(PPSkippedRange);
3531 
3532       if (!PP.getPreprocessingRecord())
3533         PP.createPreprocessingRecord();
3534       if (!PP.getPreprocessingRecord()->getExternalSource())
3535         PP.getPreprocessingRecord()->SetExternalSource(*this);
3536       F.BasePreprocessedSkippedRangeID = PP.getPreprocessingRecord()
3537           ->allocateSkippedRanges(F.NumPreprocessedSkippedRanges);
3538 
3539       if (F.NumPreprocessedSkippedRanges > 0)
3540         GlobalSkippedRangeMap.insert(
3541             std::make_pair(F.BasePreprocessedSkippedRangeID, &F));
3542       break;
3543     }
3544 
3545     case DECL_UPDATE_OFFSETS:
3546       if (Record.size() % 2 != 0) {
3547         Error("invalid DECL_UPDATE_OFFSETS block in AST file");
3548         return Failure;
3549       }
3550       for (unsigned I = 0, N = Record.size(); I != N; I += 2) {
3551         GlobalDeclID ID = getGlobalDeclID(F, Record[I]);
3552         DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I + 1]));
3553 
3554         // If we've already loaded the decl, perform the updates when we finish
3555         // loading this block.
3556         if (Decl *D = GetExistingDecl(ID))
3557           PendingUpdateRecords.push_back(
3558               PendingUpdateRecord(ID, D, /*JustLoaded=*/false));
3559       }
3560       break;
3561 
3562     case OBJC_CATEGORIES_MAP:
3563       if (F.LocalNumObjCCategoriesInMap != 0) {
3564         Error("duplicate OBJC_CATEGORIES_MAP record in AST file");
3565         return Failure;
3566       }
3567 
3568       F.LocalNumObjCCategoriesInMap = Record[0];
3569       F.ObjCCategoriesMap = (const ObjCCategoriesInfo *)Blob.data();
3570       break;
3571 
3572     case OBJC_CATEGORIES:
3573       F.ObjCCategories.swap(Record);
3574       break;
3575 
3576     case CUDA_SPECIAL_DECL_REFS:
3577       // Later tables overwrite earlier ones.
3578       // FIXME: Modules will have trouble with this.
3579       CUDASpecialDeclRefs.clear();
3580       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3581         CUDASpecialDeclRefs.push_back(getGlobalDeclID(F, Record[I]));
3582       break;
3583 
3584     case HEADER_SEARCH_TABLE:
3585       F.HeaderFileInfoTableData = Blob.data();
3586       F.LocalNumHeaderFileInfos = Record[1];
3587       if (Record[0]) {
3588         F.HeaderFileInfoTable
3589           = HeaderFileInfoLookupTable::Create(
3590                    (const unsigned char *)F.HeaderFileInfoTableData + Record[0],
3591                    (const unsigned char *)F.HeaderFileInfoTableData,
3592                    HeaderFileInfoTrait(*this, F,
3593                                        &PP.getHeaderSearchInfo(),
3594                                        Blob.data() + Record[2]));
3595 
3596         PP.getHeaderSearchInfo().SetExternalSource(this);
3597         if (!PP.getHeaderSearchInfo().getExternalLookup())
3598           PP.getHeaderSearchInfo().SetExternalLookup(this);
3599       }
3600       break;
3601 
3602     case FP_PRAGMA_OPTIONS:
3603       // Later tables overwrite earlier ones.
3604       FPPragmaOptions.swap(Record);
3605       break;
3606 
3607     case OPENCL_EXTENSIONS:
3608       for (unsigned I = 0, E = Record.size(); I != E; ) {
3609         auto Name = ReadString(Record, I);
3610         auto &Opt = OpenCLExtensions.OptMap[Name];
3611         Opt.Supported = Record[I++] != 0;
3612         Opt.Enabled = Record[I++] != 0;
3613         Opt.Avail = Record[I++];
3614         Opt.Core = Record[I++];
3615       }
3616       break;
3617 
3618     case OPENCL_EXTENSION_TYPES:
3619       for (unsigned I = 0, E = Record.size(); I != E;) {
3620         auto TypeID = static_cast<::TypeID>(Record[I++]);
3621         auto *Type = GetType(TypeID).getTypePtr();
3622         auto NumExt = static_cast<unsigned>(Record[I++]);
3623         for (unsigned II = 0; II != NumExt; ++II) {
3624           auto Ext = ReadString(Record, I);
3625           OpenCLTypeExtMap[Type].insert(Ext);
3626         }
3627       }
3628       break;
3629 
3630     case OPENCL_EXTENSION_DECLS:
3631       for (unsigned I = 0, E = Record.size(); I != E;) {
3632         auto DeclID = static_cast<::DeclID>(Record[I++]);
3633         auto *Decl = GetDecl(DeclID);
3634         auto NumExt = static_cast<unsigned>(Record[I++]);
3635         for (unsigned II = 0; II != NumExt; ++II) {
3636           auto Ext = ReadString(Record, I);
3637           OpenCLDeclExtMap[Decl].insert(Ext);
3638         }
3639       }
3640       break;
3641 
3642     case TENTATIVE_DEFINITIONS:
3643       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3644         TentativeDefinitions.push_back(getGlobalDeclID(F, Record[I]));
3645       break;
3646 
3647     case KNOWN_NAMESPACES:
3648       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3649         KnownNamespaces.push_back(getGlobalDeclID(F, Record[I]));
3650       break;
3651 
3652     case UNDEFINED_BUT_USED:
3653       if (UndefinedButUsed.size() % 2 != 0) {
3654         Error("Invalid existing UndefinedButUsed");
3655         return Failure;
3656       }
3657 
3658       if (Record.size() % 2 != 0) {
3659         Error("invalid undefined-but-used record");
3660         return Failure;
3661       }
3662       for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
3663         UndefinedButUsed.push_back(getGlobalDeclID(F, Record[I++]));
3664         UndefinedButUsed.push_back(
3665             ReadSourceLocation(F, Record, I).getRawEncoding());
3666       }
3667       break;
3668 
3669     case DELETE_EXPRS_TO_ANALYZE:
3670       for (unsigned I = 0, N = Record.size(); I != N;) {
3671         DelayedDeleteExprs.push_back(getGlobalDeclID(F, Record[I++]));
3672         const uint64_t Count = Record[I++];
3673         DelayedDeleteExprs.push_back(Count);
3674         for (uint64_t C = 0; C < Count; ++C) {
3675           DelayedDeleteExprs.push_back(ReadSourceLocation(F, Record, I).getRawEncoding());
3676           bool IsArrayForm = Record[I++] == 1;
3677           DelayedDeleteExprs.push_back(IsArrayForm);
3678         }
3679       }
3680       break;
3681 
3682     case IMPORTED_MODULES:
3683       if (!F.isModule()) {
3684         // If we aren't loading a module (which has its own exports), make
3685         // all of the imported modules visible.
3686         // FIXME: Deal with macros-only imports.
3687         for (unsigned I = 0, N = Record.size(); I != N; /**/) {
3688           unsigned GlobalID = getGlobalSubmoduleID(F, Record[I++]);
3689           SourceLocation Loc = ReadSourceLocation(F, Record, I);
3690           if (GlobalID) {
3691             ImportedModules.push_back(ImportedSubmodule(GlobalID, Loc));
3692             if (DeserializationListener)
3693               DeserializationListener->ModuleImportRead(GlobalID, Loc);
3694           }
3695         }
3696       }
3697       break;
3698 
3699     case MACRO_OFFSET: {
3700       if (F.LocalNumMacros != 0) {
3701         Error("duplicate MACRO_OFFSET record in AST file");
3702         return Failure;
3703       }
3704       F.MacroOffsets = (const uint32_t *)Blob.data();
3705       F.LocalNumMacros = Record[0];
3706       unsigned LocalBaseMacroID = Record[1];
3707       F.MacroOffsetsBase = Record[2] + F.ASTBlockStartOffset;
3708       F.BaseMacroID = getTotalNumMacros();
3709 
3710       if (F.LocalNumMacros > 0) {
3711         // Introduce the global -> local mapping for macros within this module.
3712         GlobalMacroMap.insert(std::make_pair(getTotalNumMacros() + 1, &F));
3713 
3714         // Introduce the local -> global mapping for macros within this module.
3715         F.MacroRemap.insertOrReplace(
3716           std::make_pair(LocalBaseMacroID,
3717                          F.BaseMacroID - LocalBaseMacroID));
3718 
3719         MacrosLoaded.resize(MacrosLoaded.size() + F.LocalNumMacros);
3720       }
3721       break;
3722     }
3723 
3724     case LATE_PARSED_TEMPLATE:
3725       LateParsedTemplates.emplace_back(
3726           std::piecewise_construct, std::forward_as_tuple(&F),
3727           std::forward_as_tuple(Record.begin(), Record.end()));
3728       break;
3729 
3730     case OPTIMIZE_PRAGMA_OPTIONS:
3731       if (Record.size() != 1) {
3732         Error("invalid pragma optimize record");
3733         return Failure;
3734       }
3735       OptimizeOffPragmaLocation = ReadSourceLocation(F, Record[0]);
3736       break;
3737 
3738     case MSSTRUCT_PRAGMA_OPTIONS:
3739       if (Record.size() != 1) {
3740         Error("invalid pragma ms_struct record");
3741         return Failure;
3742       }
3743       PragmaMSStructState = Record[0];
3744       break;
3745 
3746     case POINTERS_TO_MEMBERS_PRAGMA_OPTIONS:
3747       if (Record.size() != 2) {
3748         Error("invalid pragma ms_struct record");
3749         return Failure;
3750       }
3751       PragmaMSPointersToMembersState = Record[0];
3752       PointersToMembersPragmaLocation = ReadSourceLocation(F, Record[1]);
3753       break;
3754 
3755     case UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES:
3756       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3757         UnusedLocalTypedefNameCandidates.push_back(
3758             getGlobalDeclID(F, Record[I]));
3759       break;
3760 
3761     case CUDA_PRAGMA_FORCE_HOST_DEVICE_DEPTH:
3762       if (Record.size() != 1) {
3763         Error("invalid cuda pragma options record");
3764         return Failure;
3765       }
3766       ForceCUDAHostDeviceDepth = Record[0];
3767       break;
3768 
3769     case PACK_PRAGMA_OPTIONS: {
3770       if (Record.size() < 3) {
3771         Error("invalid pragma pack record");
3772         return Failure;
3773       }
3774       PragmaPackCurrentValue = Record[0];
3775       PragmaPackCurrentLocation = ReadSourceLocation(F, Record[1]);
3776       unsigned NumStackEntries = Record[2];
3777       unsigned Idx = 3;
3778       // Reset the stack when importing a new module.
3779       PragmaPackStack.clear();
3780       for (unsigned I = 0; I < NumStackEntries; ++I) {
3781         PragmaPackStackEntry Entry;
3782         Entry.Value = Record[Idx++];
3783         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3784         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3785         PragmaPackStrings.push_back(ReadString(Record, Idx));
3786         Entry.SlotLabel = PragmaPackStrings.back();
3787         PragmaPackStack.push_back(Entry);
3788       }
3789       break;
3790     }
3791 
3792     case FLOAT_CONTROL_PRAGMA_OPTIONS: {
3793       if (Record.size() < 3) {
3794         Error("invalid pragma pack record");
3795         return Failure;
3796       }
3797       FpPragmaCurrentValue = FPOptionsOverride::getFromOpaqueInt(Record[0]);
3798       FpPragmaCurrentLocation = ReadSourceLocation(F, Record[1]);
3799       unsigned NumStackEntries = Record[2];
3800       unsigned Idx = 3;
3801       // Reset the stack when importing a new module.
3802       FpPragmaStack.clear();
3803       for (unsigned I = 0; I < NumStackEntries; ++I) {
3804         FpPragmaStackEntry Entry;
3805         Entry.Value = FPOptionsOverride::getFromOpaqueInt(Record[Idx++]);
3806         Entry.Location = ReadSourceLocation(F, Record[Idx++]);
3807         Entry.PushLocation = ReadSourceLocation(F, Record[Idx++]);
3808         FpPragmaStrings.push_back(ReadString(Record, Idx));
3809         Entry.SlotLabel = FpPragmaStrings.back();
3810         FpPragmaStack.push_back(Entry);
3811       }
3812       break;
3813     }
3814 
3815     case DECLS_TO_CHECK_FOR_DEFERRED_DIAGS:
3816       for (unsigned I = 0, N = Record.size(); I != N; ++I)
3817         DeclsToCheckForDeferredDiags.push_back(getGlobalDeclID(F, Record[I]));
3818       break;
3819     }
3820   }
3821 }
3822 
3823 void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
3824   assert(!F.ModuleOffsetMap.empty() && "no module offset map to read");
3825 
3826   // Additional remapping information.
3827   const unsigned char *Data = (const unsigned char*)F.ModuleOffsetMap.data();
3828   const unsigned char *DataEnd = Data + F.ModuleOffsetMap.size();
3829   F.ModuleOffsetMap = StringRef();
3830 
3831   // If we see this entry before SOURCE_LOCATION_OFFSETS, add placeholders.
3832   if (F.SLocRemap.find(0) == F.SLocRemap.end()) {
3833     F.SLocRemap.insert(std::make_pair(0U, 0));
3834     F.SLocRemap.insert(std::make_pair(2U, 1));
3835   }
3836 
3837   // Continuous range maps we may be updating in our module.
3838   using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
3839   RemapBuilder SLocRemap(F.SLocRemap);
3840   RemapBuilder IdentifierRemap(F.IdentifierRemap);
3841   RemapBuilder MacroRemap(F.MacroRemap);
3842   RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
3843   RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
3844   RemapBuilder SelectorRemap(F.SelectorRemap);
3845   RemapBuilder DeclRemap(F.DeclRemap);
3846   RemapBuilder TypeRemap(F.TypeRemap);
3847 
3848   while (Data < DataEnd) {
3849     // FIXME: Looking up dependency modules by filename is horrible. Let's
3850     // start fixing this with prebuilt, explicit and implicit modules and see
3851     // how it goes...
3852     using namespace llvm::support;
3853     ModuleKind Kind = static_cast<ModuleKind>(
3854       endian::readNext<uint8_t, little, unaligned>(Data));
3855     uint16_t Len = endian::readNext<uint16_t, little, unaligned>(Data);
3856     StringRef Name = StringRef((const char*)Data, Len);
3857     Data += Len;
3858     ModuleFile *OM = (Kind == MK_PrebuiltModule || Kind == MK_ExplicitModule ||
3859                               Kind == MK_ImplicitModule
3860                           ? ModuleMgr.lookupByModuleName(Name)
3861                           : ModuleMgr.lookupByFileName(Name));
3862     if (!OM) {
3863       std::string Msg =
3864           "SourceLocation remap refers to unknown module, cannot find ";
3865       Msg.append(std::string(Name));
3866       Error(Msg);
3867       return;
3868     }
3869 
3870     uint32_t SLocOffset =
3871         endian::readNext<uint32_t, little, unaligned>(Data);
3872     uint32_t IdentifierIDOffset =
3873         endian::readNext<uint32_t, little, unaligned>(Data);
3874     uint32_t MacroIDOffset =
3875         endian::readNext<uint32_t, little, unaligned>(Data);
3876     uint32_t PreprocessedEntityIDOffset =
3877         endian::readNext<uint32_t, little, unaligned>(Data);
3878     uint32_t SubmoduleIDOffset =
3879         endian::readNext<uint32_t, little, unaligned>(Data);
3880     uint32_t SelectorIDOffset =
3881         endian::readNext<uint32_t, little, unaligned>(Data);
3882     uint32_t DeclIDOffset =
3883         endian::readNext<uint32_t, little, unaligned>(Data);
3884     uint32_t TypeIndexOffset =
3885         endian::readNext<uint32_t, little, unaligned>(Data);
3886 
3887     uint32_t None = std::numeric_limits<uint32_t>::max();
3888 
3889     auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
3890                          RemapBuilder &Remap) {
3891       if (Offset != None)
3892         Remap.insert(std::make_pair(Offset,
3893                                     static_cast<int>(BaseOffset - Offset)));
3894     };
3895     mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap);
3896     mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
3897     mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
3898     mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
3899               PreprocessedEntityRemap);
3900     mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
3901     mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
3902     mapOffset(DeclIDOffset, OM->BaseDeclID, DeclRemap);
3903     mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
3904 
3905     // Global -> local mappings.
3906     F.GlobalToLocalDeclIDs[OM] = DeclIDOffset;
3907   }
3908 }
3909 
3910 ASTReader::ASTReadResult
3911 ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
3912                                   const ModuleFile *ImportedBy,
3913                                   unsigned ClientLoadCapabilities) {
3914   unsigned Idx = 0;
3915   F.ModuleMapPath = ReadPath(F, Record, Idx);
3916 
3917   // Try to resolve ModuleName in the current header search context and
3918   // verify that it is found in the same module map file as we saved. If the
3919   // top-level AST file is a main file, skip this check because there is no
3920   // usable header search context.
3921   assert(!F.ModuleName.empty() &&
3922          "MODULE_NAME should come before MODULE_MAP_FILE");
3923   if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
3924     // An implicitly-loaded module file should have its module listed in some
3925     // module map file that we've already loaded.
3926     Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3927     auto &Map = PP.getHeaderSearchInfo().getModuleMap();
3928     const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
3929     // Don't emit module relocation error if we have -fno-validate-pch
3930     if (!PP.getPreprocessorOpts().DisablePCHValidation && !ModMap) {
3931       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0) {
3932         if (auto *ASTFE = M ? M->getASTFile() : nullptr) {
3933           // This module was defined by an imported (explicit) module.
3934           Diag(diag::err_module_file_conflict) << F.ModuleName << F.FileName
3935                                                << ASTFE->getName();
3936         } else {
3937           // This module was built with a different module map.
3938           Diag(diag::err_imported_module_not_found)
3939               << F.ModuleName << F.FileName
3940               << (ImportedBy ? ImportedBy->FileName : "") << F.ModuleMapPath
3941               << !ImportedBy;
3942           // In case it was imported by a PCH, there's a chance the user is
3943           // just missing to include the search path to the directory containing
3944           // the modulemap.
3945           if (ImportedBy && ImportedBy->Kind == MK_PCH)
3946             Diag(diag::note_imported_by_pch_module_not_found)
3947                 << llvm::sys::path::parent_path(F.ModuleMapPath);
3948         }
3949       }
3950       return OutOfDate;
3951     }
3952 
3953     assert(M->Name == F.ModuleName && "found module with different name");
3954 
3955     // Check the primary module map file.
3956     auto StoredModMap = FileMgr.getFile(F.ModuleMapPath);
3957     if (!StoredModMap || *StoredModMap != ModMap) {
3958       assert(ModMap && "found module is missing module map file");
3959       assert((ImportedBy || F.Kind == MK_ImplicitModule) &&
3960              "top-level import should be verified");
3961       bool NotImported = F.Kind == MK_ImplicitModule && !ImportedBy;
3962       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3963         Diag(diag::err_imported_module_modmap_changed)
3964             << F.ModuleName << (NotImported ? F.FileName : ImportedBy->FileName)
3965             << ModMap->getName() << F.ModuleMapPath << NotImported;
3966       return OutOfDate;
3967     }
3968 
3969     llvm::SmallPtrSet<const FileEntry *, 1> AdditionalStoredMaps;
3970     for (unsigned I = 0, N = Record[Idx++]; I < N; ++I) {
3971       // FIXME: we should use input files rather than storing names.
3972       std::string Filename = ReadPath(F, Record, Idx);
3973       auto F = FileMgr.getFile(Filename, false, false);
3974       if (!F) {
3975         if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3976           Error("could not find file '" + Filename +"' referenced by AST file");
3977         return OutOfDate;
3978       }
3979       AdditionalStoredMaps.insert(*F);
3980     }
3981 
3982     // Check any additional module map files (e.g. module.private.modulemap)
3983     // that are not in the pcm.
3984     if (auto *AdditionalModuleMaps = Map.getAdditionalModuleMapFiles(M)) {
3985       for (const FileEntry *ModMap : *AdditionalModuleMaps) {
3986         // Remove files that match
3987         // Note: SmallPtrSet::erase is really remove
3988         if (!AdditionalStoredMaps.erase(ModMap)) {
3989           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
3990             Diag(diag::err_module_different_modmap)
3991               << F.ModuleName << /*new*/0 << ModMap->getName();
3992           return OutOfDate;
3993         }
3994       }
3995     }
3996 
3997     // Check any additional module map files that are in the pcm, but not
3998     // found in header search. Cases that match are already removed.
3999     for (const FileEntry *ModMap : AdditionalStoredMaps) {
4000       if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
4001         Diag(diag::err_module_different_modmap)
4002           << F.ModuleName << /*not new*/1 << ModMap->getName();
4003       return OutOfDate;
4004     }
4005   }
4006 
4007   if (Listener)
4008     Listener->ReadModuleMapFile(F.ModuleMapPath);
4009   return Success;
4010 }
4011 
4012 /// Move the given method to the back of the global list of methods.
4013 static void moveMethodToBackOfGlobalList(Sema &S, ObjCMethodDecl *Method) {
4014   // Find the entry for this selector in the method pool.
4015   Sema::GlobalMethodPool::iterator Known
4016     = S.MethodPool.find(Method->getSelector());
4017   if (Known == S.MethodPool.end())
4018     return;
4019 
4020   // Retrieve the appropriate method list.
4021   ObjCMethodList &Start = Method->isInstanceMethod()? Known->second.first
4022                                                     : Known->second.second;
4023   bool Found = false;
4024   for (ObjCMethodList *List = &Start; List; List = List->getNext()) {
4025     if (!Found) {
4026       if (List->getMethod() == Method) {
4027         Found = true;
4028       } else {
4029         // Keep searching.
4030         continue;
4031       }
4032     }
4033 
4034     if (List->getNext())
4035       List->setMethod(List->getNext()->getMethod());
4036     else
4037       List->setMethod(Method);
4038   }
4039 }
4040 
4041 void ASTReader::makeNamesVisible(const HiddenNames &Names, Module *Owner) {
4042   assert(Owner->NameVisibility != Module::Hidden && "nothing to make visible?");
4043   for (Decl *D : Names) {
4044     bool wasHidden = !D->isUnconditionallyVisible();
4045     D->setVisibleDespiteOwningModule();
4046 
4047     if (wasHidden && SemaObj) {
4048       if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D)) {
4049         moveMethodToBackOfGlobalList(*SemaObj, Method);
4050       }
4051     }
4052   }
4053 }
4054 
4055 void ASTReader::makeModuleVisible(Module *Mod,
4056                                   Module::NameVisibilityKind NameVisibility,
4057                                   SourceLocation ImportLoc) {
4058   llvm::SmallPtrSet<Module *, 4> Visited;
4059   SmallVector<Module *, 4> Stack;
4060   Stack.push_back(Mod);
4061   while (!Stack.empty()) {
4062     Mod = Stack.pop_back_val();
4063 
4064     if (NameVisibility <= Mod->NameVisibility) {
4065       // This module already has this level of visibility (or greater), so
4066       // there is nothing more to do.
4067       continue;
4068     }
4069 
4070     if (Mod->isUnimportable()) {
4071       // Modules that aren't importable cannot be made visible.
4072       continue;
4073     }
4074 
4075     // Update the module's name visibility.
4076     Mod->NameVisibility = NameVisibility;
4077 
4078     // If we've already deserialized any names from this module,
4079     // mark them as visible.
4080     HiddenNamesMapType::iterator Hidden = HiddenNamesMap.find(Mod);
4081     if (Hidden != HiddenNamesMap.end()) {
4082       auto HiddenNames = std::move(*Hidden);
4083       HiddenNamesMap.erase(Hidden);
4084       makeNamesVisible(HiddenNames.second, HiddenNames.first);
4085       assert(HiddenNamesMap.find(Mod) == HiddenNamesMap.end() &&
4086              "making names visible added hidden names");
4087     }
4088 
4089     // Push any exported modules onto the stack to be marked as visible.
4090     SmallVector<Module *, 16> Exports;
4091     Mod->getExportedModules(Exports);
4092     for (SmallVectorImpl<Module *>::iterator
4093            I = Exports.begin(), E = Exports.end(); I != E; ++I) {
4094       Module *Exported = *I;
4095       if (Visited.insert(Exported).second)
4096         Stack.push_back(Exported);
4097     }
4098   }
4099 }
4100 
4101 /// We've merged the definition \p MergedDef into the existing definition
4102 /// \p Def. Ensure that \p Def is made visible whenever \p MergedDef is made
4103 /// visible.
4104 void ASTReader::mergeDefinitionVisibility(NamedDecl *Def,
4105                                           NamedDecl *MergedDef) {
4106   if (!Def->isUnconditionallyVisible()) {
4107     // If MergedDef is visible or becomes visible, make the definition visible.
4108     if (MergedDef->isUnconditionallyVisible())
4109       Def->setVisibleDespiteOwningModule();
4110     else {
4111       getContext().mergeDefinitionIntoModule(
4112           Def, MergedDef->getImportedOwningModule(),
4113           /*NotifyListeners*/ false);
4114       PendingMergedDefinitionsToDeduplicate.insert(Def);
4115     }
4116   }
4117 }
4118 
4119 bool ASTReader::loadGlobalIndex() {
4120   if (GlobalIndex)
4121     return false;
4122 
4123   if (TriedLoadingGlobalIndex || !UseGlobalIndex ||
4124       !PP.getLangOpts().Modules)
4125     return true;
4126 
4127   // Try to load the global index.
4128   TriedLoadingGlobalIndex = true;
4129   StringRef ModuleCachePath
4130     = getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
4131   std::pair<GlobalModuleIndex *, llvm::Error> Result =
4132       GlobalModuleIndex::readIndex(ModuleCachePath);
4133   if (llvm::Error Err = std::move(Result.second)) {
4134     assert(!Result.first);
4135     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
4136     return true;
4137   }
4138 
4139   GlobalIndex.reset(Result.first);
4140   ModuleMgr.setGlobalIndex(GlobalIndex.get());
4141   return false;
4142 }
4143 
4144 bool ASTReader::isGlobalIndexUnavailable() const {
4145   return PP.getLangOpts().Modules && UseGlobalIndex &&
4146          !hasGlobalIndex() && TriedLoadingGlobalIndex;
4147 }
4148 
4149 static void updateModuleTimestamp(ModuleFile &MF) {
4150   // Overwrite the timestamp file contents so that file's mtime changes.
4151   std::string TimestampFilename = MF.getTimestampFilename();
4152   std::error_code EC;
4153   llvm::raw_fd_ostream OS(TimestampFilename, EC, llvm::sys::fs::OF_Text);
4154   if (EC)
4155     return;
4156   OS << "Timestamp file\n";
4157   OS.close();
4158   OS.clear_error(); // Avoid triggering a fatal error.
4159 }
4160 
4161 /// Given a cursor at the start of an AST file, scan ahead and drop the
4162 /// cursor into the start of the given block ID, returning false on success and
4163 /// true on failure.
4164 static bool SkipCursorToBlock(BitstreamCursor &Cursor, unsigned BlockID) {
4165   while (true) {
4166     Expected<llvm::BitstreamEntry> MaybeEntry = Cursor.advance();
4167     if (!MaybeEntry) {
4168       // FIXME this drops errors on the floor.
4169       consumeError(MaybeEntry.takeError());
4170       return true;
4171     }
4172     llvm::BitstreamEntry Entry = MaybeEntry.get();
4173 
4174     switch (Entry.Kind) {
4175     case llvm::BitstreamEntry::Error:
4176     case llvm::BitstreamEntry::EndBlock:
4177       return true;
4178 
4179     case llvm::BitstreamEntry::Record:
4180       // Ignore top-level records.
4181       if (Expected<unsigned> Skipped = Cursor.skipRecord(Entry.ID))
4182         break;
4183       else {
4184         // FIXME this drops errors on the floor.
4185         consumeError(Skipped.takeError());
4186         return true;
4187       }
4188 
4189     case llvm::BitstreamEntry::SubBlock:
4190       if (Entry.ID == BlockID) {
4191         if (llvm::Error Err = Cursor.EnterSubBlock(BlockID)) {
4192           // FIXME this drops the error on the floor.
4193           consumeError(std::move(Err));
4194           return true;
4195         }
4196         // Found it!
4197         return false;
4198       }
4199 
4200       if (llvm::Error Err = Cursor.SkipBlock()) {
4201         // FIXME this drops the error on the floor.
4202         consumeError(std::move(Err));
4203         return true;
4204       }
4205     }
4206   }
4207 }
4208 
4209 ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
4210                                             ModuleKind Type,
4211                                             SourceLocation ImportLoc,
4212                                             unsigned ClientLoadCapabilities,
4213                                             SmallVectorImpl<ImportedSubmodule> *Imported) {
4214   llvm::SaveAndRestore<SourceLocation>
4215     SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
4216 
4217   // Defer any pending actions until we get to the end of reading the AST file.
4218   Deserializing AnASTFile(this);
4219 
4220   // Bump the generation number.
4221   unsigned PreviousGeneration = 0;
4222   if (ContextObj)
4223     PreviousGeneration = incrementGeneration(*ContextObj);
4224 
4225   unsigned NumModules = ModuleMgr.size();
4226   auto removeModulesAndReturn = [&](ASTReadResult ReadResult) {
4227     assert(ReadResult && "expected to return error");
4228     ModuleMgr.removeModules(ModuleMgr.begin() + NumModules,
4229                             PP.getLangOpts().Modules
4230                                 ? &PP.getHeaderSearchInfo().getModuleMap()
4231                                 : nullptr);
4232 
4233     // If we find that any modules are unusable, the global index is going
4234     // to be out-of-date. Just remove it.
4235     GlobalIndex.reset();
4236     ModuleMgr.setGlobalIndex(nullptr);
4237     return ReadResult;
4238   };
4239 
4240   SmallVector<ImportedModule, 4> Loaded;
4241   switch (ASTReadResult ReadResult =
4242               ReadASTCore(FileName, Type, ImportLoc,
4243                           /*ImportedBy=*/nullptr, Loaded, 0, 0,
4244                           ASTFileSignature(), ClientLoadCapabilities)) {
4245   case Failure:
4246   case Missing:
4247   case OutOfDate:
4248   case VersionMismatch:
4249   case ConfigurationMismatch:
4250   case HadErrors:
4251     return removeModulesAndReturn(ReadResult);
4252   case Success:
4253     break;
4254   }
4255 
4256   // Here comes stuff that we only do once the entire chain is loaded.
4257 
4258   // Load the AST blocks of all of the modules that we loaded.  We can still
4259   // hit errors parsing the ASTs at this point.
4260   for (ImportedModule &M : Loaded) {
4261     ModuleFile &F = *M.Mod;
4262 
4263     // Read the AST block.
4264     if (ASTReadResult Result = ReadASTBlock(F, ClientLoadCapabilities))
4265       return removeModulesAndReturn(Result);
4266 
4267     // The AST block should always have a definition for the main module.
4268     if (F.isModule() && !F.DidReadTopLevelSubmodule) {
4269       Error(diag::err_module_file_missing_top_level_submodule, F.FileName);
4270       return removeModulesAndReturn(Failure);
4271     }
4272 
4273     // Read the extension blocks.
4274     while (!SkipCursorToBlock(F.Stream, EXTENSION_BLOCK_ID)) {
4275       if (ASTReadResult Result = ReadExtensionBlock(F))
4276         return removeModulesAndReturn(Result);
4277     }
4278 
4279     // Once read, set the ModuleFile bit base offset and update the size in
4280     // bits of all files we've seen.
4281     F.GlobalBitOffset = TotalModulesSizeInBits;
4282     TotalModulesSizeInBits += F.SizeInBits;
4283     GlobalBitOffsetsMap.insert(std::make_pair(F.GlobalBitOffset, &F));
4284   }
4285 
4286   // Preload source locations and interesting indentifiers.
4287   for (ImportedModule &M : Loaded) {
4288     ModuleFile &F = *M.Mod;
4289 
4290     // Preload SLocEntries.
4291     for (unsigned I = 0, N = F.PreloadSLocEntries.size(); I != N; ++I) {
4292       int Index = int(F.PreloadSLocEntries[I] - 1) + F.SLocEntryBaseID;
4293       // Load it through the SourceManager and don't call ReadSLocEntry()
4294       // directly because the entry may have already been loaded in which case
4295       // calling ReadSLocEntry() directly would trigger an assertion in
4296       // SourceManager.
4297       SourceMgr.getLoadedSLocEntryByID(Index);
4298     }
4299 
4300     // Map the original source file ID into the ID space of the current
4301     // compilation.
4302     if (F.OriginalSourceFileID.isValid()) {
4303       F.OriginalSourceFileID = FileID::get(
4304           F.SLocEntryBaseID + F.OriginalSourceFileID.getOpaqueValue() - 1);
4305     }
4306 
4307     // Preload all the pending interesting identifiers by marking them out of
4308     // date.
4309     for (auto Offset : F.PreloadIdentifierOffsets) {
4310       const unsigned char *Data = reinterpret_cast<const unsigned char *>(
4311           F.IdentifierTableData + Offset);
4312 
4313       ASTIdentifierLookupTrait Trait(*this, F);
4314       auto KeyDataLen = Trait.ReadKeyDataLength(Data);
4315       auto Key = Trait.ReadKey(Data, KeyDataLen.first);
4316       auto &II = PP.getIdentifierTable().getOwn(Key);
4317       II.setOutOfDate(true);
4318 
4319       // Mark this identifier as being from an AST file so that we can track
4320       // whether we need to serialize it.
4321       markIdentifierFromAST(*this, II);
4322 
4323       // Associate the ID with the identifier so that the writer can reuse it.
4324       auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
4325       SetIdentifierInfo(ID, &II);
4326     }
4327   }
4328 
4329   // Setup the import locations and notify the module manager that we've
4330   // committed to these module files.
4331   for (ImportedModule &M : Loaded) {
4332     ModuleFile &F = *M.Mod;
4333 
4334     ModuleMgr.moduleFileAccepted(&F);
4335 
4336     // Set the import location.
4337     F.DirectImportLoc = ImportLoc;
4338     // FIXME: We assume that locations from PCH / preamble do not need
4339     // any translation.
4340     if (!M.ImportedBy)
4341       F.ImportLoc = M.ImportLoc;
4342     else
4343       F.ImportLoc = TranslateSourceLocation(*M.ImportedBy, M.ImportLoc);
4344   }
4345 
4346   if (!PP.getLangOpts().CPlusPlus ||
4347       (Type != MK_ImplicitModule && Type != MK_ExplicitModule &&
4348        Type != MK_PrebuiltModule)) {
4349     // Mark all of the identifiers in the identifier table as being out of date,
4350     // so that various accessors know to check the loaded modules when the
4351     // identifier is used.
4352     //
4353     // For C++ modules, we don't need information on many identifiers (just
4354     // those that provide macros or are poisoned), so we mark all of
4355     // the interesting ones via PreloadIdentifierOffsets.
4356     for (IdentifierTable::iterator Id = PP.getIdentifierTable().begin(),
4357                                 IdEnd = PP.getIdentifierTable().end();
4358          Id != IdEnd; ++Id)
4359       Id->second->setOutOfDate(true);
4360   }
4361   // Mark selectors as out of date.
4362   for (auto Sel : SelectorGeneration)
4363     SelectorOutOfDate[Sel.first] = true;
4364 
4365   // Resolve any unresolved module exports.
4366   for (unsigned I = 0, N = UnresolvedModuleRefs.size(); I != N; ++I) {
4367     UnresolvedModuleRef &Unresolved = UnresolvedModuleRefs[I];
4368     SubmoduleID GlobalID = getGlobalSubmoduleID(*Unresolved.File,Unresolved.ID);
4369     Module *ResolvedMod = getSubmodule(GlobalID);
4370 
4371     switch (Unresolved.Kind) {
4372     case UnresolvedModuleRef::Conflict:
4373       if (ResolvedMod) {
4374         Module::Conflict Conflict;
4375         Conflict.Other = ResolvedMod;
4376         Conflict.Message = Unresolved.String.str();
4377         Unresolved.Mod->Conflicts.push_back(Conflict);
4378       }
4379       continue;
4380 
4381     case UnresolvedModuleRef::Import:
4382       if (ResolvedMod)
4383         Unresolved.Mod->Imports.insert(ResolvedMod);
4384       continue;
4385 
4386     case UnresolvedModuleRef::Export:
4387       if (ResolvedMod || Unresolved.IsWildcard)
4388         Unresolved.Mod->Exports.push_back(
4389           Module::ExportDecl(ResolvedMod, Unresolved.IsWildcard));
4390       continue;
4391     }
4392   }
4393   UnresolvedModuleRefs.clear();
4394 
4395   if (Imported)
4396     Imported->append(ImportedModules.begin(),
4397                      ImportedModules.end());
4398 
4399   // FIXME: How do we load the 'use'd modules? They may not be submodules.
4400   // Might be unnecessary as use declarations are only used to build the
4401   // module itself.
4402 
4403   if (ContextObj)
4404     InitializeContext();
4405 
4406   if (SemaObj)
4407     UpdateSema();
4408 
4409   if (DeserializationListener)
4410     DeserializationListener->ReaderInitialized(this);
4411 
4412   ModuleFile &PrimaryModule = ModuleMgr.getPrimaryModule();
4413   if (PrimaryModule.OriginalSourceFileID.isValid()) {
4414     // If this AST file is a precompiled preamble, then set the
4415     // preamble file ID of the source manager to the file source file
4416     // from which the preamble was built.
4417     if (Type == MK_Preamble) {
4418       SourceMgr.setPreambleFileID(PrimaryModule.OriginalSourceFileID);
4419     } else if (Type == MK_MainFile) {
4420       SourceMgr.setMainFileID(PrimaryModule.OriginalSourceFileID);
4421     }
4422   }
4423 
4424   // For any Objective-C class definitions we have already loaded, make sure
4425   // that we load any additional categories.
4426   if (ContextObj) {
4427     for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) {
4428       loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(),
4429                          ObjCClassesLoaded[I],
4430                          PreviousGeneration);
4431     }
4432   }
4433 
4434   if (PP.getHeaderSearchInfo()
4435           .getHeaderSearchOpts()
4436           .ModulesValidateOncePerBuildSession) {
4437     // Now we are certain that the module and all modules it depends on are
4438     // up to date.  Create or update timestamp files for modules that are
4439     // located in the module cache (not for PCH files that could be anywhere
4440     // in the filesystem).
4441     for (unsigned I = 0, N = Loaded.size(); I != N; ++I) {
4442       ImportedModule &M = Loaded[I];
4443       if (M.Mod->Kind == MK_ImplicitModule) {
4444         updateModuleTimestamp(*M.Mod);
4445       }
4446     }
4447   }
4448 
4449   return Success;
4450 }
4451 
4452 static ASTFileSignature readASTFileSignature(StringRef PCH);
4453 
4454 /// Whether \p Stream doesn't start with the AST/PCH file magic number 'CPCH'.
4455 static llvm::Error doesntStartWithASTFileMagic(BitstreamCursor &Stream) {
4456   // FIXME checking magic headers is done in other places such as
4457   // SerializedDiagnosticReader and GlobalModuleIndex, but error handling isn't
4458   // always done the same. Unify it all with a helper.
4459   if (!Stream.canSkipToPos(4))
4460     return llvm::createStringError(std::errc::illegal_byte_sequence,
4461                                    "file too small to contain AST file magic");
4462   for (unsigned C : {'C', 'P', 'C', 'H'})
4463     if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Stream.Read(8)) {
4464       if (Res.get() != C)
4465         return llvm::createStringError(
4466             std::errc::illegal_byte_sequence,
4467             "file doesn't start with AST file magic");
4468     } else
4469       return Res.takeError();
4470   return llvm::Error::success();
4471 }
4472 
4473 static unsigned moduleKindForDiagnostic(ModuleKind Kind) {
4474   switch (Kind) {
4475   case MK_PCH:
4476     return 0; // PCH
4477   case MK_ImplicitModule:
4478   case MK_ExplicitModule:
4479   case MK_PrebuiltModule:
4480     return 1; // module
4481   case MK_MainFile:
4482   case MK_Preamble:
4483     return 2; // main source file
4484   }
4485   llvm_unreachable("unknown module kind");
4486 }
4487 
4488 ASTReader::ASTReadResult
4489 ASTReader::ReadASTCore(StringRef FileName,
4490                        ModuleKind Type,
4491                        SourceLocation ImportLoc,
4492                        ModuleFile *ImportedBy,
4493                        SmallVectorImpl<ImportedModule> &Loaded,
4494                        off_t ExpectedSize, time_t ExpectedModTime,
4495                        ASTFileSignature ExpectedSignature,
4496                        unsigned ClientLoadCapabilities) {
4497   ModuleFile *M;
4498   std::string ErrorStr;
4499   ModuleManager::AddModuleResult AddResult
4500     = ModuleMgr.addModule(FileName, Type, ImportLoc, ImportedBy,
4501                           getGeneration(), ExpectedSize, ExpectedModTime,
4502                           ExpectedSignature, readASTFileSignature,
4503                           M, ErrorStr);
4504 
4505   switch (AddResult) {
4506   case ModuleManager::AlreadyLoaded:
4507     Diag(diag::remark_module_import)
4508         << M->ModuleName << M->FileName << (ImportedBy ? true : false)
4509         << (ImportedBy ? StringRef(ImportedBy->ModuleName) : StringRef());
4510     return Success;
4511 
4512   case ModuleManager::NewlyLoaded:
4513     // Load module file below.
4514     break;
4515 
4516   case ModuleManager::Missing:
4517     // The module file was missing; if the client can handle that, return
4518     // it.
4519     if (ClientLoadCapabilities & ARR_Missing)
4520       return Missing;
4521 
4522     // Otherwise, return an error.
4523     Diag(diag::err_module_file_not_found) << moduleKindForDiagnostic(Type)
4524                                           << FileName << !ErrorStr.empty()
4525                                           << ErrorStr;
4526     return Failure;
4527 
4528   case ModuleManager::OutOfDate:
4529     // We couldn't load the module file because it is out-of-date. If the
4530     // client can handle out-of-date, return it.
4531     if (ClientLoadCapabilities & ARR_OutOfDate)
4532       return OutOfDate;
4533 
4534     // Otherwise, return an error.
4535     Diag(diag::err_module_file_out_of_date) << moduleKindForDiagnostic(Type)
4536                                             << FileName << !ErrorStr.empty()
4537                                             << ErrorStr;
4538     return Failure;
4539   }
4540 
4541   assert(M && "Missing module file");
4542 
4543   bool ShouldFinalizePCM = false;
4544   auto FinalizeOrDropPCM = llvm::make_scope_exit([&]() {
4545     auto &MC = getModuleManager().getModuleCache();
4546     if (ShouldFinalizePCM)
4547       MC.finalizePCM(FileName);
4548     else
4549       MC.tryToDropPCM(FileName);
4550   });
4551   ModuleFile &F = *M;
4552   BitstreamCursor &Stream = F.Stream;
4553   Stream = BitstreamCursor(PCHContainerRdr.ExtractPCH(*F.Buffer));
4554   F.SizeInBits = F.Buffer->getBufferSize() * 8;
4555 
4556   // Sniff for the signature.
4557   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4558     Diag(diag::err_module_file_invalid)
4559         << moduleKindForDiagnostic(Type) << FileName << std::move(Err);
4560     return Failure;
4561   }
4562 
4563   // This is used for compatibility with older PCH formats.
4564   bool HaveReadControlBlock = false;
4565   while (true) {
4566     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4567     if (!MaybeEntry) {
4568       Error(MaybeEntry.takeError());
4569       return Failure;
4570     }
4571     llvm::BitstreamEntry Entry = MaybeEntry.get();
4572 
4573     switch (Entry.Kind) {
4574     case llvm::BitstreamEntry::Error:
4575     case llvm::BitstreamEntry::Record:
4576     case llvm::BitstreamEntry::EndBlock:
4577       Error("invalid record at top-level of AST file");
4578       return Failure;
4579 
4580     case llvm::BitstreamEntry::SubBlock:
4581       break;
4582     }
4583 
4584     switch (Entry.ID) {
4585     case CONTROL_BLOCK_ID:
4586       HaveReadControlBlock = true;
4587       switch (ReadControlBlock(F, Loaded, ImportedBy, ClientLoadCapabilities)) {
4588       case Success:
4589         // Check that we didn't try to load a non-module AST file as a module.
4590         //
4591         // FIXME: Should we also perform the converse check? Loading a module as
4592         // a PCH file sort of works, but it's a bit wonky.
4593         if ((Type == MK_ImplicitModule || Type == MK_ExplicitModule ||
4594              Type == MK_PrebuiltModule) &&
4595             F.ModuleName.empty()) {
4596           auto Result = (Type == MK_ImplicitModule) ? OutOfDate : Failure;
4597           if (Result != OutOfDate ||
4598               (ClientLoadCapabilities & ARR_OutOfDate) == 0)
4599             Diag(diag::err_module_file_not_module) << FileName;
4600           return Result;
4601         }
4602         break;
4603 
4604       case Failure: return Failure;
4605       case Missing: return Missing;
4606       case OutOfDate: return OutOfDate;
4607       case VersionMismatch: return VersionMismatch;
4608       case ConfigurationMismatch: return ConfigurationMismatch;
4609       case HadErrors: return HadErrors;
4610       }
4611       break;
4612 
4613     case AST_BLOCK_ID:
4614       if (!HaveReadControlBlock) {
4615         if ((ClientLoadCapabilities & ARR_VersionMismatch) == 0)
4616           Diag(diag::err_pch_version_too_old);
4617         return VersionMismatch;
4618       }
4619 
4620       // Record that we've loaded this module.
4621       Loaded.push_back(ImportedModule(M, ImportedBy, ImportLoc));
4622       ShouldFinalizePCM = true;
4623       return Success;
4624 
4625     case UNHASHED_CONTROL_BLOCK_ID:
4626       // This block is handled using look-ahead during ReadControlBlock.  We
4627       // shouldn't get here!
4628       Error("malformed block record in AST file");
4629       return Failure;
4630 
4631     default:
4632       if (llvm::Error Err = Stream.SkipBlock()) {
4633         Error(std::move(Err));
4634         return Failure;
4635       }
4636       break;
4637     }
4638   }
4639 
4640   llvm_unreachable("unexpected break; expected return");
4641 }
4642 
4643 ASTReader::ASTReadResult
4644 ASTReader::readUnhashedControlBlock(ModuleFile &F, bool WasImportedBy,
4645                                     unsigned ClientLoadCapabilities) {
4646   const HeaderSearchOptions &HSOpts =
4647       PP.getHeaderSearchInfo().getHeaderSearchOpts();
4648   bool AllowCompatibleConfigurationMismatch =
4649       F.Kind == MK_ExplicitModule || F.Kind == MK_PrebuiltModule;
4650 
4651   ASTReadResult Result = readUnhashedControlBlockImpl(
4652       &F, F.Data, ClientLoadCapabilities, AllowCompatibleConfigurationMismatch,
4653       Listener.get(),
4654       WasImportedBy ? false : HSOpts.ModulesValidateDiagnosticOptions);
4655 
4656   // If F was directly imported by another module, it's implicitly validated by
4657   // the importing module.
4658   if (DisableValidation || WasImportedBy ||
4659       (AllowConfigurationMismatch && Result == ConfigurationMismatch))
4660     return Success;
4661 
4662   if (Result == Failure) {
4663     Error("malformed block record in AST file");
4664     return Failure;
4665   }
4666 
4667   if (Result == OutOfDate && F.Kind == MK_ImplicitModule) {
4668     // If this module has already been finalized in the ModuleCache, we're stuck
4669     // with it; we can only load a single version of each module.
4670     //
4671     // This can happen when a module is imported in two contexts: in one, as a
4672     // user module; in another, as a system module (due to an import from
4673     // another module marked with the [system] flag).  It usually indicates a
4674     // bug in the module map: this module should also be marked with [system].
4675     //
4676     // If -Wno-system-headers (the default), and the first import is as a
4677     // system module, then validation will fail during the as-user import,
4678     // since -Werror flags won't have been validated.  However, it's reasonable
4679     // to treat this consistently as a system module.
4680     //
4681     // If -Wsystem-headers, the PCM on disk was built with
4682     // -Wno-system-headers, and the first import is as a user module, then
4683     // validation will fail during the as-system import since the PCM on disk
4684     // doesn't guarantee that -Werror was respected.  However, the -Werror
4685     // flags were checked during the initial as-user import.
4686     if (getModuleManager().getModuleCache().isPCMFinal(F.FileName)) {
4687       Diag(diag::warn_module_system_bit_conflict) << F.FileName;
4688       return Success;
4689     }
4690   }
4691 
4692   return Result;
4693 }
4694 
4695 ASTReader::ASTReadResult ASTReader::readUnhashedControlBlockImpl(
4696     ModuleFile *F, llvm::StringRef StreamData, unsigned ClientLoadCapabilities,
4697     bool AllowCompatibleConfigurationMismatch, ASTReaderListener *Listener,
4698     bool ValidateDiagnosticOptions) {
4699   // Initialize a stream.
4700   BitstreamCursor Stream(StreamData);
4701 
4702   // Sniff for the signature.
4703   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
4704     // FIXME this drops the error on the floor.
4705     consumeError(std::move(Err));
4706     return Failure;
4707   }
4708 
4709   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
4710   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
4711     return Failure;
4712 
4713   // Read all of the records in the options block.
4714   RecordData Record;
4715   ASTReadResult Result = Success;
4716   while (true) {
4717     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4718     if (!MaybeEntry) {
4719       // FIXME this drops the error on the floor.
4720       consumeError(MaybeEntry.takeError());
4721       return Failure;
4722     }
4723     llvm::BitstreamEntry Entry = MaybeEntry.get();
4724 
4725     switch (Entry.Kind) {
4726     case llvm::BitstreamEntry::Error:
4727     case llvm::BitstreamEntry::SubBlock:
4728       return Failure;
4729 
4730     case llvm::BitstreamEntry::EndBlock:
4731       return Result;
4732 
4733     case llvm::BitstreamEntry::Record:
4734       // The interesting case.
4735       break;
4736     }
4737 
4738     // Read and process a record.
4739     Record.clear();
4740     Expected<unsigned> MaybeRecordType = Stream.readRecord(Entry.ID, Record);
4741     if (!MaybeRecordType) {
4742       // FIXME this drops the error.
4743       return Failure;
4744     }
4745     switch ((UnhashedControlBlockRecordTypes)MaybeRecordType.get()) {
4746     case SIGNATURE:
4747       if (F)
4748         F->Signature = ASTFileSignature::create(Record.begin(), Record.end());
4749       break;
4750     case AST_BLOCK_HASH:
4751       if (F)
4752         F->ASTBlockHash =
4753             ASTFileSignature::create(Record.begin(), Record.end());
4754       break;
4755     case DIAGNOSTIC_OPTIONS: {
4756       bool Complain = (ClientLoadCapabilities & ARR_OutOfDate) == 0;
4757       if (Listener && ValidateDiagnosticOptions &&
4758           !AllowCompatibleConfigurationMismatch &&
4759           ParseDiagnosticOptions(Record, Complain, *Listener))
4760         Result = OutOfDate; // Don't return early.  Read the signature.
4761       break;
4762     }
4763     case DIAG_PRAGMA_MAPPINGS:
4764       if (!F)
4765         break;
4766       if (F->PragmaDiagMappings.empty())
4767         F->PragmaDiagMappings.swap(Record);
4768       else
4769         F->PragmaDiagMappings.insert(F->PragmaDiagMappings.end(),
4770                                      Record.begin(), Record.end());
4771       break;
4772     }
4773   }
4774 }
4775 
4776 /// Parse a record and blob containing module file extension metadata.
4777 static bool parseModuleFileExtensionMetadata(
4778               const SmallVectorImpl<uint64_t> &Record,
4779               StringRef Blob,
4780               ModuleFileExtensionMetadata &Metadata) {
4781   if (Record.size() < 4) return true;
4782 
4783   Metadata.MajorVersion = Record[0];
4784   Metadata.MinorVersion = Record[1];
4785 
4786   unsigned BlockNameLen = Record[2];
4787   unsigned UserInfoLen = Record[3];
4788 
4789   if (BlockNameLen + UserInfoLen > Blob.size()) return true;
4790 
4791   Metadata.BlockName = std::string(Blob.data(), Blob.data() + BlockNameLen);
4792   Metadata.UserInfo = std::string(Blob.data() + BlockNameLen,
4793                                   Blob.data() + BlockNameLen + UserInfoLen);
4794   return false;
4795 }
4796 
4797 ASTReader::ASTReadResult ASTReader::ReadExtensionBlock(ModuleFile &F) {
4798   BitstreamCursor &Stream = F.Stream;
4799 
4800   RecordData Record;
4801   while (true) {
4802     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
4803     if (!MaybeEntry) {
4804       Error(MaybeEntry.takeError());
4805       return Failure;
4806     }
4807     llvm::BitstreamEntry Entry = MaybeEntry.get();
4808 
4809     switch (Entry.Kind) {
4810     case llvm::BitstreamEntry::SubBlock:
4811       if (llvm::Error Err = Stream.SkipBlock()) {
4812         Error(std::move(Err));
4813         return Failure;
4814       }
4815       continue;
4816 
4817     case llvm::BitstreamEntry::EndBlock:
4818       return Success;
4819 
4820     case llvm::BitstreamEntry::Error:
4821       return HadErrors;
4822 
4823     case llvm::BitstreamEntry::Record:
4824       break;
4825     }
4826 
4827     Record.clear();
4828     StringRef Blob;
4829     Expected<unsigned> MaybeRecCode =
4830         Stream.readRecord(Entry.ID, Record, &Blob);
4831     if (!MaybeRecCode) {
4832       Error(MaybeRecCode.takeError());
4833       return Failure;
4834     }
4835     switch (MaybeRecCode.get()) {
4836     case EXTENSION_METADATA: {
4837       ModuleFileExtensionMetadata Metadata;
4838       if (parseModuleFileExtensionMetadata(Record, Blob, Metadata)) {
4839         Error("malformed EXTENSION_METADATA in AST file");
4840         return Failure;
4841       }
4842 
4843       // Find a module file extension with this block name.
4844       auto Known = ModuleFileExtensions.find(Metadata.BlockName);
4845       if (Known == ModuleFileExtensions.end()) break;
4846 
4847       // Form a reader.
4848       if (auto Reader = Known->second->createExtensionReader(Metadata, *this,
4849                                                              F, Stream)) {
4850         F.ExtensionReaders.push_back(std::move(Reader));
4851       }
4852 
4853       break;
4854     }
4855     }
4856   }
4857 
4858   return Success;
4859 }
4860 
4861 void ASTReader::InitializeContext() {
4862   assert(ContextObj && "no context to initialize");
4863   ASTContext &Context = *ContextObj;
4864 
4865   // If there's a listener, notify them that we "read" the translation unit.
4866   if (DeserializationListener)
4867     DeserializationListener->DeclRead(PREDEF_DECL_TRANSLATION_UNIT_ID,
4868                                       Context.getTranslationUnitDecl());
4869 
4870   // FIXME: Find a better way to deal with collisions between these
4871   // built-in types. Right now, we just ignore the problem.
4872 
4873   // Load the special types.
4874   if (SpecialTypes.size() >= NumSpecialTypeIDs) {
4875     if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
4876       if (!Context.CFConstantStringTypeDecl)
4877         Context.setCFConstantStringType(GetType(String));
4878     }
4879 
4880     if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
4881       QualType FileType = GetType(File);
4882       if (FileType.isNull()) {
4883         Error("FILE type is NULL");
4884         return;
4885       }
4886 
4887       if (!Context.FILEDecl) {
4888         if (const TypedefType *Typedef = FileType->getAs<TypedefType>())
4889           Context.setFILEDecl(Typedef->getDecl());
4890         else {
4891           const TagType *Tag = FileType->getAs<TagType>();
4892           if (!Tag) {
4893             Error("Invalid FILE type in AST file");
4894             return;
4895           }
4896           Context.setFILEDecl(Tag->getDecl());
4897         }
4898       }
4899     }
4900 
4901     if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
4902       QualType Jmp_bufType = GetType(Jmp_buf);
4903       if (Jmp_bufType.isNull()) {
4904         Error("jmp_buf type is NULL");
4905         return;
4906       }
4907 
4908       if (!Context.jmp_bufDecl) {
4909         if (const TypedefType *Typedef = Jmp_bufType->getAs<TypedefType>())
4910           Context.setjmp_bufDecl(Typedef->getDecl());
4911         else {
4912           const TagType *Tag = Jmp_bufType->getAs<TagType>();
4913           if (!Tag) {
4914             Error("Invalid jmp_buf type in AST file");
4915             return;
4916           }
4917           Context.setjmp_bufDecl(Tag->getDecl());
4918         }
4919       }
4920     }
4921 
4922     if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
4923       QualType Sigjmp_bufType = GetType(Sigjmp_buf);
4924       if (Sigjmp_bufType.isNull()) {
4925         Error("sigjmp_buf type is NULL");
4926         return;
4927       }
4928 
4929       if (!Context.sigjmp_bufDecl) {
4930         if (const TypedefType *Typedef = Sigjmp_bufType->getAs<TypedefType>())
4931           Context.setsigjmp_bufDecl(Typedef->getDecl());
4932         else {
4933           const TagType *Tag = Sigjmp_bufType->getAs<TagType>();
4934           assert(Tag && "Invalid sigjmp_buf type in AST file");
4935           Context.setsigjmp_bufDecl(Tag->getDecl());
4936         }
4937       }
4938     }
4939 
4940     if (unsigned ObjCIdRedef
4941           = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
4942       if (Context.ObjCIdRedefinitionType.isNull())
4943         Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
4944     }
4945 
4946     if (unsigned ObjCClassRedef
4947           = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
4948       if (Context.ObjCClassRedefinitionType.isNull())
4949         Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
4950     }
4951 
4952     if (unsigned ObjCSelRedef
4953           = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
4954       if (Context.ObjCSelRedefinitionType.isNull())
4955         Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
4956     }
4957 
4958     if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
4959       QualType Ucontext_tType = GetType(Ucontext_t);
4960       if (Ucontext_tType.isNull()) {
4961         Error("ucontext_t type is NULL");
4962         return;
4963       }
4964 
4965       if (!Context.ucontext_tDecl) {
4966         if (const TypedefType *Typedef = Ucontext_tType->getAs<TypedefType>())
4967           Context.setucontext_tDecl(Typedef->getDecl());
4968         else {
4969           const TagType *Tag = Ucontext_tType->getAs<TagType>();
4970           assert(Tag && "Invalid ucontext_t type in AST file");
4971           Context.setucontext_tDecl(Tag->getDecl());
4972         }
4973       }
4974     }
4975   }
4976 
4977   ReadPragmaDiagnosticMappings(Context.getDiagnostics());
4978 
4979   // If there were any CUDA special declarations, deserialize them.
4980   if (!CUDASpecialDeclRefs.empty()) {
4981     assert(CUDASpecialDeclRefs.size() == 1 && "More decl refs than expected!");
4982     Context.setcudaConfigureCallDecl(
4983                            cast<FunctionDecl>(GetDecl(CUDASpecialDeclRefs[0])));
4984   }
4985 
4986   // Re-export any modules that were imported by a non-module AST file.
4987   // FIXME: This does not make macro-only imports visible again.
4988   for (auto &Import : ImportedModules) {
4989     if (Module *Imported = getSubmodule(Import.ID)) {
4990       makeModuleVisible(Imported, Module::AllVisible,
4991                         /*ImportLoc=*/Import.ImportLoc);
4992       if (Import.ImportLoc.isValid())
4993         PP.makeModuleVisible(Imported, Import.ImportLoc);
4994       // This updates visibility for Preprocessor only. For Sema, which can be
4995       // nullptr here, we do the same later, in UpdateSema().
4996     }
4997   }
4998 }
4999 
5000 void ASTReader::finalizeForWriting() {
5001   // Nothing to do for now.
5002 }
5003 
5004 /// Reads and return the signature record from \p PCH's control block, or
5005 /// else returns 0.
5006 static ASTFileSignature readASTFileSignature(StringRef PCH) {
5007   BitstreamCursor Stream(PCH);
5008   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5009     // FIXME this drops the error on the floor.
5010     consumeError(std::move(Err));
5011     return ASTFileSignature();
5012   }
5013 
5014   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5015   if (SkipCursorToBlock(Stream, UNHASHED_CONTROL_BLOCK_ID))
5016     return ASTFileSignature();
5017 
5018   // Scan for SIGNATURE inside the diagnostic options block.
5019   ASTReader::RecordData Record;
5020   while (true) {
5021     Expected<llvm::BitstreamEntry> MaybeEntry =
5022         Stream.advanceSkippingSubblocks();
5023     if (!MaybeEntry) {
5024       // FIXME this drops the error on the floor.
5025       consumeError(MaybeEntry.takeError());
5026       return ASTFileSignature();
5027     }
5028     llvm::BitstreamEntry Entry = MaybeEntry.get();
5029 
5030     if (Entry.Kind != llvm::BitstreamEntry::Record)
5031       return ASTFileSignature();
5032 
5033     Record.clear();
5034     StringRef Blob;
5035     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5036     if (!MaybeRecord) {
5037       // FIXME this drops the error on the floor.
5038       consumeError(MaybeRecord.takeError());
5039       return ASTFileSignature();
5040     }
5041     if (SIGNATURE == MaybeRecord.get())
5042       return ASTFileSignature::create(Record.begin(),
5043                                       Record.begin() + ASTFileSignature::size);
5044   }
5045 }
5046 
5047 /// Retrieve the name of the original source file name
5048 /// directly from the AST file, without actually loading the AST
5049 /// file.
5050 std::string ASTReader::getOriginalSourceFile(
5051     const std::string &ASTFileName, FileManager &FileMgr,
5052     const PCHContainerReader &PCHContainerRdr, DiagnosticsEngine &Diags) {
5053   // Open the AST file.
5054   auto Buffer = FileMgr.getBufferForFile(ASTFileName);
5055   if (!Buffer) {
5056     Diags.Report(diag::err_fe_unable_to_read_pch_file)
5057         << ASTFileName << Buffer.getError().message();
5058     return std::string();
5059   }
5060 
5061   // Initialize the stream
5062   BitstreamCursor Stream(PCHContainerRdr.ExtractPCH(**Buffer));
5063 
5064   // Sniff for the signature.
5065   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5066     Diags.Report(diag::err_fe_not_a_pch_file) << ASTFileName << std::move(Err);
5067     return std::string();
5068   }
5069 
5070   // Scan for the CONTROL_BLOCK_ID block.
5071   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID)) {
5072     Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5073     return std::string();
5074   }
5075 
5076   // Scan for ORIGINAL_FILE inside the control block.
5077   RecordData Record;
5078   while (true) {
5079     Expected<llvm::BitstreamEntry> MaybeEntry =
5080         Stream.advanceSkippingSubblocks();
5081     if (!MaybeEntry) {
5082       // FIXME this drops errors on the floor.
5083       consumeError(MaybeEntry.takeError());
5084       return std::string();
5085     }
5086     llvm::BitstreamEntry Entry = MaybeEntry.get();
5087 
5088     if (Entry.Kind == llvm::BitstreamEntry::EndBlock)
5089       return std::string();
5090 
5091     if (Entry.Kind != llvm::BitstreamEntry::Record) {
5092       Diags.Report(diag::err_fe_pch_malformed_block) << ASTFileName;
5093       return std::string();
5094     }
5095 
5096     Record.clear();
5097     StringRef Blob;
5098     Expected<unsigned> MaybeRecord = Stream.readRecord(Entry.ID, Record, &Blob);
5099     if (!MaybeRecord) {
5100       // FIXME this drops the errors on the floor.
5101       consumeError(MaybeRecord.takeError());
5102       return std::string();
5103     }
5104     if (ORIGINAL_FILE == MaybeRecord.get())
5105       return Blob.str();
5106   }
5107 }
5108 
5109 namespace {
5110 
5111   class SimplePCHValidator : public ASTReaderListener {
5112     const LangOptions &ExistingLangOpts;
5113     const TargetOptions &ExistingTargetOpts;
5114     const PreprocessorOptions &ExistingPPOpts;
5115     std::string ExistingModuleCachePath;
5116     FileManager &FileMgr;
5117 
5118   public:
5119     SimplePCHValidator(const LangOptions &ExistingLangOpts,
5120                        const TargetOptions &ExistingTargetOpts,
5121                        const PreprocessorOptions &ExistingPPOpts,
5122                        StringRef ExistingModuleCachePath, FileManager &FileMgr)
5123         : ExistingLangOpts(ExistingLangOpts),
5124           ExistingTargetOpts(ExistingTargetOpts),
5125           ExistingPPOpts(ExistingPPOpts),
5126           ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr) {}
5127 
5128     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
5129                              bool AllowCompatibleDifferences) override {
5130       return checkLanguageOptions(ExistingLangOpts, LangOpts, nullptr,
5131                                   AllowCompatibleDifferences);
5132     }
5133 
5134     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
5135                            bool AllowCompatibleDifferences) override {
5136       return checkTargetOptions(ExistingTargetOpts, TargetOpts, nullptr,
5137                                 AllowCompatibleDifferences);
5138     }
5139 
5140     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
5141                                  StringRef SpecificModuleCachePath,
5142                                  bool Complain) override {
5143       return checkHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5144                                       ExistingModuleCachePath,
5145                                       nullptr, ExistingLangOpts);
5146     }
5147 
5148     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
5149                                  bool Complain,
5150                                  std::string &SuggestedPredefines) override {
5151       return checkPreprocessorOptions(ExistingPPOpts, PPOpts, nullptr, FileMgr,
5152                                       SuggestedPredefines, ExistingLangOpts);
5153     }
5154   };
5155 
5156 } // namespace
5157 
5158 bool ASTReader::readASTFileControlBlock(
5159     StringRef Filename, FileManager &FileMgr,
5160     const PCHContainerReader &PCHContainerRdr,
5161     bool FindModuleFileExtensions,
5162     ASTReaderListener &Listener, bool ValidateDiagnosticOptions) {
5163   // Open the AST file.
5164   // FIXME: This allows use of the VFS; we do not allow use of the
5165   // VFS when actually loading a module.
5166   auto Buffer = FileMgr.getBufferForFile(Filename);
5167   if (!Buffer) {
5168     return true;
5169   }
5170 
5171   // Initialize the stream
5172   StringRef Bytes = PCHContainerRdr.ExtractPCH(**Buffer);
5173   BitstreamCursor Stream(Bytes);
5174 
5175   // Sniff for the signature.
5176   if (llvm::Error Err = doesntStartWithASTFileMagic(Stream)) {
5177     consumeError(std::move(Err)); // FIXME this drops errors on the floor.
5178     return true;
5179   }
5180 
5181   // Scan for the CONTROL_BLOCK_ID block.
5182   if (SkipCursorToBlock(Stream, CONTROL_BLOCK_ID))
5183     return true;
5184 
5185   bool NeedsInputFiles = Listener.needsInputFileVisitation();
5186   bool NeedsSystemInputFiles = Listener.needsSystemInputFileVisitation();
5187   bool NeedsImports = Listener.needsImportVisitation();
5188   BitstreamCursor InputFilesCursor;
5189 
5190   RecordData Record;
5191   std::string ModuleDir;
5192   bool DoneWithControlBlock = false;
5193   while (!DoneWithControlBlock) {
5194     Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5195     if (!MaybeEntry) {
5196       // FIXME this drops the error on the floor.
5197       consumeError(MaybeEntry.takeError());
5198       return true;
5199     }
5200     llvm::BitstreamEntry Entry = MaybeEntry.get();
5201 
5202     switch (Entry.Kind) {
5203     case llvm::BitstreamEntry::SubBlock: {
5204       switch (Entry.ID) {
5205       case OPTIONS_BLOCK_ID: {
5206         std::string IgnoredSuggestedPredefines;
5207         if (ReadOptionsBlock(Stream, ARR_ConfigurationMismatch | ARR_OutOfDate,
5208                              /*AllowCompatibleConfigurationMismatch*/ false,
5209                              Listener, IgnoredSuggestedPredefines) != Success)
5210           return true;
5211         break;
5212       }
5213 
5214       case INPUT_FILES_BLOCK_ID:
5215         InputFilesCursor = Stream;
5216         if (llvm::Error Err = Stream.SkipBlock()) {
5217           // FIXME this drops the error on the floor.
5218           consumeError(std::move(Err));
5219           return true;
5220         }
5221         if (NeedsInputFiles &&
5222             ReadBlockAbbrevs(InputFilesCursor, INPUT_FILES_BLOCK_ID))
5223           return true;
5224         break;
5225 
5226       default:
5227         if (llvm::Error Err = Stream.SkipBlock()) {
5228           // FIXME this drops the error on the floor.
5229           consumeError(std::move(Err));
5230           return true;
5231         }
5232         break;
5233       }
5234 
5235       continue;
5236     }
5237 
5238     case llvm::BitstreamEntry::EndBlock:
5239       DoneWithControlBlock = true;
5240       break;
5241 
5242     case llvm::BitstreamEntry::Error:
5243       return true;
5244 
5245     case llvm::BitstreamEntry::Record:
5246       break;
5247     }
5248 
5249     if (DoneWithControlBlock) break;
5250 
5251     Record.clear();
5252     StringRef Blob;
5253     Expected<unsigned> MaybeRecCode =
5254         Stream.readRecord(Entry.ID, Record, &Blob);
5255     if (!MaybeRecCode) {
5256       // FIXME this drops the error.
5257       return Failure;
5258     }
5259     switch ((ControlRecordTypes)MaybeRecCode.get()) {
5260     case METADATA:
5261       if (Record[0] != VERSION_MAJOR)
5262         return true;
5263       if (Listener.ReadFullVersionInformation(Blob))
5264         return true;
5265       break;
5266     case MODULE_NAME:
5267       Listener.ReadModuleName(Blob);
5268       break;
5269     case MODULE_DIRECTORY:
5270       ModuleDir = std::string(Blob);
5271       break;
5272     case MODULE_MAP_FILE: {
5273       unsigned Idx = 0;
5274       auto Path = ReadString(Record, Idx);
5275       ResolveImportedPath(Path, ModuleDir);
5276       Listener.ReadModuleMapFile(Path);
5277       break;
5278     }
5279     case INPUT_FILE_OFFSETS: {
5280       if (!NeedsInputFiles)
5281         break;
5282 
5283       unsigned NumInputFiles = Record[0];
5284       unsigned NumUserFiles = Record[1];
5285       const llvm::support::unaligned_uint64_t *InputFileOffs =
5286           (const llvm::support::unaligned_uint64_t *)Blob.data();
5287       for (unsigned I = 0; I != NumInputFiles; ++I) {
5288         // Go find this input file.
5289         bool isSystemFile = I >= NumUserFiles;
5290 
5291         if (isSystemFile && !NeedsSystemInputFiles)
5292           break; // the rest are system input files
5293 
5294         BitstreamCursor &Cursor = InputFilesCursor;
5295         SavedStreamPosition SavedPosition(Cursor);
5296         if (llvm::Error Err = Cursor.JumpToBit(InputFileOffs[I])) {
5297           // FIXME this drops errors on the floor.
5298           consumeError(std::move(Err));
5299         }
5300 
5301         Expected<unsigned> MaybeCode = Cursor.ReadCode();
5302         if (!MaybeCode) {
5303           // FIXME this drops errors on the floor.
5304           consumeError(MaybeCode.takeError());
5305         }
5306         unsigned Code = MaybeCode.get();
5307 
5308         RecordData Record;
5309         StringRef Blob;
5310         bool shouldContinue = false;
5311         Expected<unsigned> MaybeRecordType =
5312             Cursor.readRecord(Code, Record, &Blob);
5313         if (!MaybeRecordType) {
5314           // FIXME this drops errors on the floor.
5315           consumeError(MaybeRecordType.takeError());
5316         }
5317         switch ((InputFileRecordTypes)MaybeRecordType.get()) {
5318         case INPUT_FILE_HASH:
5319           break;
5320         case INPUT_FILE:
5321           bool Overridden = static_cast<bool>(Record[3]);
5322           std::string Filename = std::string(Blob);
5323           ResolveImportedPath(Filename, ModuleDir);
5324           shouldContinue = Listener.visitInputFile(
5325               Filename, isSystemFile, Overridden, /*IsExplicitModule*/false);
5326           break;
5327         }
5328         if (!shouldContinue)
5329           break;
5330       }
5331       break;
5332     }
5333 
5334     case IMPORTS: {
5335       if (!NeedsImports)
5336         break;
5337 
5338       unsigned Idx = 0, N = Record.size();
5339       while (Idx < N) {
5340         // Read information about the AST file.
5341         Idx +=
5342             1 + 1 + 1 + 1 +
5343             ASTFileSignature::size; // Kind, ImportLoc, Size, ModTime, Signature
5344         std::string ModuleName = ReadString(Record, Idx);
5345         std::string Filename = ReadString(Record, Idx);
5346         ResolveImportedPath(Filename, ModuleDir);
5347         Listener.visitImport(ModuleName, Filename);
5348       }
5349       break;
5350     }
5351 
5352     default:
5353       // No other validation to perform.
5354       break;
5355     }
5356   }
5357 
5358   // Look for module file extension blocks, if requested.
5359   if (FindModuleFileExtensions) {
5360     BitstreamCursor SavedStream = Stream;
5361     while (!SkipCursorToBlock(Stream, EXTENSION_BLOCK_ID)) {
5362       bool DoneWithExtensionBlock = false;
5363       while (!DoneWithExtensionBlock) {
5364         Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance();
5365         if (!MaybeEntry) {
5366           // FIXME this drops the error.
5367           return true;
5368         }
5369         llvm::BitstreamEntry Entry = MaybeEntry.get();
5370 
5371         switch (Entry.Kind) {
5372         case llvm::BitstreamEntry::SubBlock:
5373           if (llvm::Error Err = Stream.SkipBlock()) {
5374             // FIXME this drops the error on the floor.
5375             consumeError(std::move(Err));
5376             return true;
5377           }
5378           continue;
5379 
5380         case llvm::BitstreamEntry::EndBlock:
5381           DoneWithExtensionBlock = true;
5382           continue;
5383 
5384         case llvm::BitstreamEntry::Error:
5385           return true;
5386 
5387         case llvm::BitstreamEntry::Record:
5388           break;
5389         }
5390 
5391        Record.clear();
5392        StringRef Blob;
5393        Expected<unsigned> MaybeRecCode =
5394            Stream.readRecord(Entry.ID, Record, &Blob);
5395        if (!MaybeRecCode) {
5396          // FIXME this drops the error.
5397          return true;
5398        }
5399        switch (MaybeRecCode.get()) {
5400        case EXTENSION_METADATA: {
5401          ModuleFileExtensionMetadata Metadata;
5402          if (parseModuleFileExtensionMetadata(Record, Blob, Metadata))
5403            return true;
5404 
5405          Listener.readModuleFileExtension(Metadata);
5406          break;
5407        }
5408        }
5409       }
5410     }
5411     Stream = SavedStream;
5412   }
5413 
5414   // Scan for the UNHASHED_CONTROL_BLOCK_ID block.
5415   if (readUnhashedControlBlockImpl(
5416           nullptr, Bytes, ARR_ConfigurationMismatch | ARR_OutOfDate,
5417           /*AllowCompatibleConfigurationMismatch*/ false, &Listener,
5418           ValidateDiagnosticOptions) != Success)
5419     return true;
5420 
5421   return false;
5422 }
5423 
5424 bool ASTReader::isAcceptableASTFile(StringRef Filename, FileManager &FileMgr,
5425                                     const PCHContainerReader &PCHContainerRdr,
5426                                     const LangOptions &LangOpts,
5427                                     const TargetOptions &TargetOpts,
5428                                     const PreprocessorOptions &PPOpts,
5429                                     StringRef ExistingModuleCachePath) {
5430   SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts,
5431                                ExistingModuleCachePath, FileMgr);
5432   return !readASTFileControlBlock(Filename, FileMgr, PCHContainerRdr,
5433                                   /*FindModuleFileExtensions=*/false,
5434                                   validator,
5435                                   /*ValidateDiagnosticOptions=*/true);
5436 }
5437 
5438 ASTReader::ASTReadResult
5439 ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
5440   // Enter the submodule block.
5441   if (llvm::Error Err = F.Stream.EnterSubBlock(SUBMODULE_BLOCK_ID)) {
5442     Error(std::move(Err));
5443     return Failure;
5444   }
5445 
5446   ModuleMap &ModMap = PP.getHeaderSearchInfo().getModuleMap();
5447   bool First = true;
5448   Module *CurrentModule = nullptr;
5449   RecordData Record;
5450   while (true) {
5451     Expected<llvm::BitstreamEntry> MaybeEntry =
5452         F.Stream.advanceSkippingSubblocks();
5453     if (!MaybeEntry) {
5454       Error(MaybeEntry.takeError());
5455       return Failure;
5456     }
5457     llvm::BitstreamEntry Entry = MaybeEntry.get();
5458 
5459     switch (Entry.Kind) {
5460     case llvm::BitstreamEntry::SubBlock: // Handled for us already.
5461     case llvm::BitstreamEntry::Error:
5462       Error("malformed block record in AST file");
5463       return Failure;
5464     case llvm::BitstreamEntry::EndBlock:
5465       return Success;
5466     case llvm::BitstreamEntry::Record:
5467       // The interesting case.
5468       break;
5469     }
5470 
5471     // Read a record.
5472     StringRef Blob;
5473     Record.clear();
5474     Expected<unsigned> MaybeKind = F.Stream.readRecord(Entry.ID, Record, &Blob);
5475     if (!MaybeKind) {
5476       Error(MaybeKind.takeError());
5477       return Failure;
5478     }
5479     unsigned Kind = MaybeKind.get();
5480 
5481     if ((Kind == SUBMODULE_METADATA) != First) {
5482       Error("submodule metadata record should be at beginning of block");
5483       return Failure;
5484     }
5485     First = false;
5486 
5487     // Submodule information is only valid if we have a current module.
5488     // FIXME: Should we error on these cases?
5489     if (!CurrentModule && Kind != SUBMODULE_METADATA &&
5490         Kind != SUBMODULE_DEFINITION)
5491       continue;
5492 
5493     switch (Kind) {
5494     default:  // Default behavior: ignore.
5495       break;
5496 
5497     case SUBMODULE_DEFINITION: {
5498       if (Record.size() < 12) {
5499         Error("malformed module definition");
5500         return Failure;
5501       }
5502 
5503       StringRef Name = Blob;
5504       unsigned Idx = 0;
5505       SubmoduleID GlobalID = getGlobalSubmoduleID(F, Record[Idx++]);
5506       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[Idx++]);
5507       Module::ModuleKind Kind = (Module::ModuleKind)Record[Idx++];
5508       bool IsFramework = Record[Idx++];
5509       bool IsExplicit = Record[Idx++];
5510       bool IsSystem = Record[Idx++];
5511       bool IsExternC = Record[Idx++];
5512       bool InferSubmodules = Record[Idx++];
5513       bool InferExplicitSubmodules = Record[Idx++];
5514       bool InferExportWildcard = Record[Idx++];
5515       bool ConfigMacrosExhaustive = Record[Idx++];
5516       bool ModuleMapIsPrivate = Record[Idx++];
5517 
5518       Module *ParentModule = nullptr;
5519       if (Parent)
5520         ParentModule = getSubmodule(Parent);
5521 
5522       // Retrieve this (sub)module from the module map, creating it if
5523       // necessary.
5524       CurrentModule =
5525           ModMap.findOrCreateModule(Name, ParentModule, IsFramework, IsExplicit)
5526               .first;
5527 
5528       // FIXME: set the definition loc for CurrentModule, or call
5529       // ModMap.setInferredModuleAllowedBy()
5530 
5531       SubmoduleID GlobalIndex = GlobalID - NUM_PREDEF_SUBMODULE_IDS;
5532       if (GlobalIndex >= SubmodulesLoaded.size() ||
5533           SubmodulesLoaded[GlobalIndex]) {
5534         Error("too many submodules");
5535         return Failure;
5536       }
5537 
5538       if (!ParentModule) {
5539         if (const FileEntry *CurFile = CurrentModule->getASTFile()) {
5540           // Don't emit module relocation error if we have -fno-validate-pch
5541           if (!PP.getPreprocessorOpts().DisablePCHValidation &&
5542               CurFile != F.File) {
5543             Error(diag::err_module_file_conflict,
5544                   CurrentModule->getTopLevelModuleName(), CurFile->getName(),
5545                   F.File->getName());
5546             return Failure;
5547           }
5548         }
5549 
5550         F.DidReadTopLevelSubmodule = true;
5551         CurrentModule->setASTFile(F.File);
5552         CurrentModule->PresumedModuleMapFile = F.ModuleMapPath;
5553       }
5554 
5555       CurrentModule->Kind = Kind;
5556       CurrentModule->Signature = F.Signature;
5557       CurrentModule->IsFromModuleFile = true;
5558       CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
5559       CurrentModule->IsExternC = IsExternC;
5560       CurrentModule->InferSubmodules = InferSubmodules;
5561       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
5562       CurrentModule->InferExportWildcard = InferExportWildcard;
5563       CurrentModule->ConfigMacrosExhaustive = ConfigMacrosExhaustive;
5564       CurrentModule->ModuleMapIsPrivate = ModuleMapIsPrivate;
5565       if (DeserializationListener)
5566         DeserializationListener->ModuleRead(GlobalID, CurrentModule);
5567 
5568       SubmodulesLoaded[GlobalIndex] = CurrentModule;
5569 
5570       // Clear out data that will be replaced by what is in the module file.
5571       CurrentModule->LinkLibraries.clear();
5572       CurrentModule->ConfigMacros.clear();
5573       CurrentModule->UnresolvedConflicts.clear();
5574       CurrentModule->Conflicts.clear();
5575 
5576       // The module is available unless it's missing a requirement; relevant
5577       // requirements will be (re-)added by SUBMODULE_REQUIRES records.
5578       // Missing headers that were present when the module was built do not
5579       // make it unavailable -- if we got this far, this must be an explicitly
5580       // imported module file.
5581       CurrentModule->Requirements.clear();
5582       CurrentModule->MissingHeaders.clear();
5583       CurrentModule->IsUnimportable =
5584           ParentModule && ParentModule->IsUnimportable;
5585       CurrentModule->IsAvailable = !CurrentModule->IsUnimportable;
5586       break;
5587     }
5588 
5589     case SUBMODULE_UMBRELLA_HEADER: {
5590       std::string Filename = std::string(Blob);
5591       ResolveImportedPath(F, Filename);
5592       if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
5593         if (!CurrentModule->getUmbrellaHeader())
5594           ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
5595         else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
5596           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5597             Error("mismatched umbrella headers in submodule");
5598           return OutOfDate;
5599         }
5600       }
5601       break;
5602     }
5603 
5604     case SUBMODULE_HEADER:
5605     case SUBMODULE_EXCLUDED_HEADER:
5606     case SUBMODULE_PRIVATE_HEADER:
5607       // We lazily associate headers with their modules via the HeaderInfo table.
5608       // FIXME: Re-evaluate this section; maybe only store InputFile IDs instead
5609       // of complete filenames or remove it entirely.
5610       break;
5611 
5612     case SUBMODULE_TEXTUAL_HEADER:
5613     case SUBMODULE_PRIVATE_TEXTUAL_HEADER:
5614       // FIXME: Textual headers are not marked in the HeaderInfo table. Load
5615       // them here.
5616       break;
5617 
5618     case SUBMODULE_TOPHEADER:
5619       CurrentModule->addTopHeaderFilename(Blob);
5620       break;
5621 
5622     case SUBMODULE_UMBRELLA_DIR: {
5623       std::string Dirname = std::string(Blob);
5624       ResolveImportedPath(F, Dirname);
5625       if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
5626         if (!CurrentModule->getUmbrellaDir())
5627           ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
5628         else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
5629           if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
5630             Error("mismatched umbrella directories in submodule");
5631           return OutOfDate;
5632         }
5633       }
5634       break;
5635     }
5636 
5637     case SUBMODULE_METADATA: {
5638       F.BaseSubmoduleID = getTotalNumSubmodules();
5639       F.LocalNumSubmodules = Record[0];
5640       unsigned LocalBaseSubmoduleID = Record[1];
5641       if (F.LocalNumSubmodules > 0) {
5642         // Introduce the global -> local mapping for submodules within this
5643         // module.
5644         GlobalSubmoduleMap.insert(std::make_pair(getTotalNumSubmodules()+1,&F));
5645 
5646         // Introduce the local -> global mapping for submodules within this
5647         // module.
5648         F.SubmoduleRemap.insertOrReplace(
5649           std::make_pair(LocalBaseSubmoduleID,
5650                          F.BaseSubmoduleID - LocalBaseSubmoduleID));
5651 
5652         SubmodulesLoaded.resize(SubmodulesLoaded.size() + F.LocalNumSubmodules);
5653       }
5654       break;
5655     }
5656 
5657     case SUBMODULE_IMPORTS:
5658       for (unsigned Idx = 0; Idx != Record.size(); ++Idx) {
5659         UnresolvedModuleRef Unresolved;
5660         Unresolved.File = &F;
5661         Unresolved.Mod = CurrentModule;
5662         Unresolved.ID = Record[Idx];
5663         Unresolved.Kind = UnresolvedModuleRef::Import;
5664         Unresolved.IsWildcard = false;
5665         UnresolvedModuleRefs.push_back(Unresolved);
5666       }
5667       break;
5668 
5669     case SUBMODULE_EXPORTS:
5670       for (unsigned Idx = 0; Idx + 1 < Record.size(); Idx += 2) {
5671         UnresolvedModuleRef Unresolved;
5672         Unresolved.File = &F;
5673         Unresolved.Mod = CurrentModule;
5674         Unresolved.ID = Record[Idx];
5675         Unresolved.Kind = UnresolvedModuleRef::Export;
5676         Unresolved.IsWildcard = Record[Idx + 1];
5677         UnresolvedModuleRefs.push_back(Unresolved);
5678       }
5679 
5680       // Once we've loaded the set of exports, there's no reason to keep
5681       // the parsed, unresolved exports around.
5682       CurrentModule->UnresolvedExports.clear();
5683       break;
5684 
5685     case SUBMODULE_REQUIRES:
5686       CurrentModule->addRequirement(Blob, Record[0], PP.getLangOpts(),
5687                                     PP.getTargetInfo());
5688       break;
5689 
5690     case SUBMODULE_LINK_LIBRARY:
5691       ModMap.resolveLinkAsDependencies(CurrentModule);
5692       CurrentModule->LinkLibraries.push_back(
5693           Module::LinkLibrary(std::string(Blob), Record[0]));
5694       break;
5695 
5696     case SUBMODULE_CONFIG_MACRO:
5697       CurrentModule->ConfigMacros.push_back(Blob.str());
5698       break;
5699 
5700     case SUBMODULE_CONFLICT: {
5701       UnresolvedModuleRef Unresolved;
5702       Unresolved.File = &F;
5703       Unresolved.Mod = CurrentModule;
5704       Unresolved.ID = Record[0];
5705       Unresolved.Kind = UnresolvedModuleRef::Conflict;
5706       Unresolved.IsWildcard = false;
5707       Unresolved.String = Blob;
5708       UnresolvedModuleRefs.push_back(Unresolved);
5709       break;
5710     }
5711 
5712     case SUBMODULE_INITIALIZERS: {
5713       if (!ContextObj)
5714         break;
5715       SmallVector<uint32_t, 16> Inits;
5716       for (auto &ID : Record)
5717         Inits.push_back(getGlobalDeclID(F, ID));
5718       ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
5719       break;
5720     }
5721 
5722     case SUBMODULE_EXPORT_AS:
5723       CurrentModule->ExportAsModule = Blob.str();
5724       ModMap.addLinkAsDependency(CurrentModule);
5725       break;
5726     }
5727   }
5728 }
5729 
5730 /// Parse the record that corresponds to a LangOptions data
5731 /// structure.
5732 ///
5733 /// This routine parses the language options from the AST file and then gives
5734 /// them to the AST listener if one is set.
5735 ///
5736 /// \returns true if the listener deems the file unacceptable, false otherwise.
5737 bool ASTReader::ParseLanguageOptions(const RecordData &Record,
5738                                      bool Complain,
5739                                      ASTReaderListener &Listener,
5740                                      bool AllowCompatibleDifferences) {
5741   LangOptions LangOpts;
5742   unsigned Idx = 0;
5743 #define LANGOPT(Name, Bits, Default, Description) \
5744   LangOpts.Name = Record[Idx++];
5745 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
5746   LangOpts.set##Name(static_cast<LangOptions::Type>(Record[Idx++]));
5747 #include "clang/Basic/LangOptions.def"
5748 #define SANITIZER(NAME, ID)                                                    \
5749   LangOpts.Sanitize.set(SanitizerKind::ID, Record[Idx++]);
5750 #include "clang/Basic/Sanitizers.def"
5751 
5752   for (unsigned N = Record[Idx++]; N; --N)
5753     LangOpts.ModuleFeatures.push_back(ReadString(Record, Idx));
5754 
5755   ObjCRuntime::Kind runtimeKind = (ObjCRuntime::Kind) Record[Idx++];
5756   VersionTuple runtimeVersion = ReadVersionTuple(Record, Idx);
5757   LangOpts.ObjCRuntime = ObjCRuntime(runtimeKind, runtimeVersion);
5758 
5759   LangOpts.CurrentModule = ReadString(Record, Idx);
5760 
5761   // Comment options.
5762   for (unsigned N = Record[Idx++]; N; --N) {
5763     LangOpts.CommentOpts.BlockCommandNames.push_back(
5764       ReadString(Record, Idx));
5765   }
5766   LangOpts.CommentOpts.ParseAllComments = Record[Idx++];
5767 
5768   // OpenMP offloading options.
5769   for (unsigned N = Record[Idx++]; N; --N) {
5770     LangOpts.OMPTargetTriples.push_back(llvm::Triple(ReadString(Record, Idx)));
5771   }
5772 
5773   LangOpts.OMPHostIRFile = ReadString(Record, Idx);
5774 
5775   return Listener.ReadLanguageOptions(LangOpts, Complain,
5776                                       AllowCompatibleDifferences);
5777 }
5778 
5779 bool ASTReader::ParseTargetOptions(const RecordData &Record, bool Complain,
5780                                    ASTReaderListener &Listener,
5781                                    bool AllowCompatibleDifferences) {
5782   unsigned Idx = 0;
5783   TargetOptions TargetOpts;
5784   TargetOpts.Triple = ReadString(Record, Idx);
5785   TargetOpts.CPU = ReadString(Record, Idx);
5786   TargetOpts.TuneCPU = ReadString(Record, Idx);
5787   TargetOpts.ABI = ReadString(Record, Idx);
5788   for (unsigned N = Record[Idx++]; N; --N) {
5789     TargetOpts.FeaturesAsWritten.push_back(ReadString(Record, Idx));
5790   }
5791   for (unsigned N = Record[Idx++]; N; --N) {
5792     TargetOpts.Features.push_back(ReadString(Record, Idx));
5793   }
5794 
5795   return Listener.ReadTargetOptions(TargetOpts, Complain,
5796                                     AllowCompatibleDifferences);
5797 }
5798 
5799 bool ASTReader::ParseDiagnosticOptions(const RecordData &Record, bool Complain,
5800                                        ASTReaderListener &Listener) {
5801   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(new DiagnosticOptions);
5802   unsigned Idx = 0;
5803 #define DIAGOPT(Name, Bits, Default) DiagOpts->Name = Record[Idx++];
5804 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
5805   DiagOpts->set##Name(static_cast<Type>(Record[Idx++]));
5806 #include "clang/Basic/DiagnosticOptions.def"
5807 
5808   for (unsigned N = Record[Idx++]; N; --N)
5809     DiagOpts->Warnings.push_back(ReadString(Record, Idx));
5810   for (unsigned N = Record[Idx++]; N; --N)
5811     DiagOpts->Remarks.push_back(ReadString(Record, Idx));
5812 
5813   return Listener.ReadDiagnosticOptions(DiagOpts, Complain);
5814 }
5815 
5816 bool ASTReader::ParseFileSystemOptions(const RecordData &Record, bool Complain,
5817                                        ASTReaderListener &Listener) {
5818   FileSystemOptions FSOpts;
5819   unsigned Idx = 0;
5820   FSOpts.WorkingDir = ReadString(Record, Idx);
5821   return Listener.ReadFileSystemOptions(FSOpts, Complain);
5822 }
5823 
5824 bool ASTReader::ParseHeaderSearchOptions(const RecordData &Record,
5825                                          bool Complain,
5826                                          ASTReaderListener &Listener) {
5827   HeaderSearchOptions HSOpts;
5828   unsigned Idx = 0;
5829   HSOpts.Sysroot = ReadString(Record, Idx);
5830 
5831   // Include entries.
5832   for (unsigned N = Record[Idx++]; N; --N) {
5833     std::string Path = ReadString(Record, Idx);
5834     frontend::IncludeDirGroup Group
5835       = static_cast<frontend::IncludeDirGroup>(Record[Idx++]);
5836     bool IsFramework = Record[Idx++];
5837     bool IgnoreSysRoot = Record[Idx++];
5838     HSOpts.UserEntries.emplace_back(std::move(Path), Group, IsFramework,
5839                                     IgnoreSysRoot);
5840   }
5841 
5842   // System header prefixes.
5843   for (unsigned N = Record[Idx++]; N; --N) {
5844     std::string Prefix = ReadString(Record, Idx);
5845     bool IsSystemHeader = Record[Idx++];
5846     HSOpts.SystemHeaderPrefixes.emplace_back(std::move(Prefix), IsSystemHeader);
5847   }
5848 
5849   HSOpts.ResourceDir = ReadString(Record, Idx);
5850   HSOpts.ModuleCachePath = ReadString(Record, Idx);
5851   HSOpts.ModuleUserBuildPath = ReadString(Record, Idx);
5852   HSOpts.DisableModuleHash = Record[Idx++];
5853   HSOpts.ImplicitModuleMaps = Record[Idx++];
5854   HSOpts.ModuleMapFileHomeIsCwd = Record[Idx++];
5855   HSOpts.UseBuiltinIncludes = Record[Idx++];
5856   HSOpts.UseStandardSystemIncludes = Record[Idx++];
5857   HSOpts.UseStandardCXXIncludes = Record[Idx++];
5858   HSOpts.UseLibcxx = Record[Idx++];
5859   std::string SpecificModuleCachePath = ReadString(Record, Idx);
5860 
5861   return Listener.ReadHeaderSearchOptions(HSOpts, SpecificModuleCachePath,
5862                                           Complain);
5863 }
5864 
5865 bool ASTReader::ParsePreprocessorOptions(const RecordData &Record,
5866                                          bool Complain,
5867                                          ASTReaderListener &Listener,
5868                                          std::string &SuggestedPredefines) {
5869   PreprocessorOptions PPOpts;
5870   unsigned Idx = 0;
5871 
5872   // Macro definitions/undefs
5873   for (unsigned N = Record[Idx++]; N; --N) {
5874     std::string Macro = ReadString(Record, Idx);
5875     bool IsUndef = Record[Idx++];
5876     PPOpts.Macros.push_back(std::make_pair(Macro, IsUndef));
5877   }
5878 
5879   // Includes
5880   for (unsigned N = Record[Idx++]; N; --N) {
5881     PPOpts.Includes.push_back(ReadString(Record, Idx));
5882   }
5883 
5884   // Macro Includes
5885   for (unsigned N = Record[Idx++]; N; --N) {
5886     PPOpts.MacroIncludes.push_back(ReadString(Record, Idx));
5887   }
5888 
5889   PPOpts.UsePredefines = Record[Idx++];
5890   PPOpts.DetailedRecord = Record[Idx++];
5891   PPOpts.ImplicitPCHInclude = ReadString(Record, Idx);
5892   PPOpts.ObjCXXARCStandardLibrary =
5893     static_cast<ObjCXXARCStandardLibraryKind>(Record[Idx++]);
5894   SuggestedPredefines.clear();
5895   return Listener.ReadPreprocessorOptions(PPOpts, Complain,
5896                                           SuggestedPredefines);
5897 }
5898 
5899 std::pair<ModuleFile *, unsigned>
5900 ASTReader::getModulePreprocessedEntity(unsigned GlobalIndex) {
5901   GlobalPreprocessedEntityMapType::iterator
5902   I = GlobalPreprocessedEntityMap.find(GlobalIndex);
5903   assert(I != GlobalPreprocessedEntityMap.end() &&
5904          "Corrupted global preprocessed entity map");
5905   ModuleFile *M = I->second;
5906   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedEntityID;
5907   return std::make_pair(M, LocalIndex);
5908 }
5909 
5910 llvm::iterator_range<PreprocessingRecord::iterator>
5911 ASTReader::getModulePreprocessedEntities(ModuleFile &Mod) const {
5912   if (PreprocessingRecord *PPRec = PP.getPreprocessingRecord())
5913     return PPRec->getIteratorsForLoadedRange(Mod.BasePreprocessedEntityID,
5914                                              Mod.NumPreprocessedEntities);
5915 
5916   return llvm::make_range(PreprocessingRecord::iterator(),
5917                           PreprocessingRecord::iterator());
5918 }
5919 
5920 llvm::iterator_range<ASTReader::ModuleDeclIterator>
5921 ASTReader::getModuleFileLevelDecls(ModuleFile &Mod) {
5922   return llvm::make_range(
5923       ModuleDeclIterator(this, &Mod, Mod.FileSortedDecls),
5924       ModuleDeclIterator(this, &Mod,
5925                          Mod.FileSortedDecls + Mod.NumFileSortedDecls));
5926 }
5927 
5928 SourceRange ASTReader::ReadSkippedRange(unsigned GlobalIndex) {
5929   auto I = GlobalSkippedRangeMap.find(GlobalIndex);
5930   assert(I != GlobalSkippedRangeMap.end() &&
5931     "Corrupted global skipped range map");
5932   ModuleFile *M = I->second;
5933   unsigned LocalIndex = GlobalIndex - M->BasePreprocessedSkippedRangeID;
5934   assert(LocalIndex < M->NumPreprocessedSkippedRanges);
5935   PPSkippedRange RawRange = M->PreprocessedSkippedRangeOffsets[LocalIndex];
5936   SourceRange Range(TranslateSourceLocation(*M, RawRange.getBegin()),
5937                     TranslateSourceLocation(*M, RawRange.getEnd()));
5938   assert(Range.isValid());
5939   return Range;
5940 }
5941 
5942 PreprocessedEntity *ASTReader::ReadPreprocessedEntity(unsigned Index) {
5943   PreprocessedEntityID PPID = Index+1;
5944   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
5945   ModuleFile &M = *PPInfo.first;
5946   unsigned LocalIndex = PPInfo.second;
5947   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
5948 
5949   if (!PP.getPreprocessingRecord()) {
5950     Error("no preprocessing record");
5951     return nullptr;
5952   }
5953 
5954   SavedStreamPosition SavedPosition(M.PreprocessorDetailCursor);
5955   if (llvm::Error Err = M.PreprocessorDetailCursor.JumpToBit(
5956           M.MacroOffsetsBase + PPOffs.BitOffset)) {
5957     Error(std::move(Err));
5958     return nullptr;
5959   }
5960 
5961   Expected<llvm::BitstreamEntry> MaybeEntry =
5962       M.PreprocessorDetailCursor.advance(BitstreamCursor::AF_DontPopBlockAtEnd);
5963   if (!MaybeEntry) {
5964     Error(MaybeEntry.takeError());
5965     return nullptr;
5966   }
5967   llvm::BitstreamEntry Entry = MaybeEntry.get();
5968 
5969   if (Entry.Kind != llvm::BitstreamEntry::Record)
5970     return nullptr;
5971 
5972   // Read the record.
5973   SourceRange Range(TranslateSourceLocation(M, PPOffs.getBegin()),
5974                     TranslateSourceLocation(M, PPOffs.getEnd()));
5975   PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
5976   StringRef Blob;
5977   RecordData Record;
5978   Expected<unsigned> MaybeRecType =
5979       M.PreprocessorDetailCursor.readRecord(Entry.ID, Record, &Blob);
5980   if (!MaybeRecType) {
5981     Error(MaybeRecType.takeError());
5982     return nullptr;
5983   }
5984   switch ((PreprocessorDetailRecordTypes)MaybeRecType.get()) {
5985   case PPD_MACRO_EXPANSION: {
5986     bool isBuiltin = Record[0];
5987     IdentifierInfo *Name = nullptr;
5988     MacroDefinitionRecord *Def = nullptr;
5989     if (isBuiltin)
5990       Name = getLocalIdentifier(M, Record[1]);
5991     else {
5992       PreprocessedEntityID GlobalID =
5993           getGlobalPreprocessedEntityID(M, Record[1]);
5994       Def = cast<MacroDefinitionRecord>(
5995           PPRec.getLoadedPreprocessedEntity(GlobalID - 1));
5996     }
5997 
5998     MacroExpansion *ME;
5999     if (isBuiltin)
6000       ME = new (PPRec) MacroExpansion(Name, Range);
6001     else
6002       ME = new (PPRec) MacroExpansion(Def, Range);
6003 
6004     return ME;
6005   }
6006 
6007   case PPD_MACRO_DEFINITION: {
6008     // Decode the identifier info and then check again; if the macro is
6009     // still defined and associated with the identifier,
6010     IdentifierInfo *II = getLocalIdentifier(M, Record[0]);
6011     MacroDefinitionRecord *MD = new (PPRec) MacroDefinitionRecord(II, Range);
6012 
6013     if (DeserializationListener)
6014       DeserializationListener->MacroDefinitionRead(PPID, MD);
6015 
6016     return MD;
6017   }
6018 
6019   case PPD_INCLUSION_DIRECTIVE: {
6020     const char *FullFileNameStart = Blob.data() + Record[0];
6021     StringRef FullFileName(FullFileNameStart, Blob.size() - Record[0]);
6022     const FileEntry *File = nullptr;
6023     if (!FullFileName.empty())
6024       if (auto FE = PP.getFileManager().getFile(FullFileName))
6025         File = *FE;
6026 
6027     // FIXME: Stable encoding
6028     InclusionDirective::InclusionKind Kind
6029       = static_cast<InclusionDirective::InclusionKind>(Record[2]);
6030     InclusionDirective *ID
6031       = new (PPRec) InclusionDirective(PPRec, Kind,
6032                                        StringRef(Blob.data(), Record[0]),
6033                                        Record[1], Record[3],
6034                                        File,
6035                                        Range);
6036     return ID;
6037   }
6038   }
6039 
6040   llvm_unreachable("Invalid PreprocessorDetailRecordTypes");
6041 }
6042 
6043 /// Find the next module that contains entities and return the ID
6044 /// of the first entry.
6045 ///
6046 /// \param SLocMapI points at a chunk of a module that contains no
6047 /// preprocessed entities or the entities it contains are not the ones we are
6048 /// looking for.
6049 PreprocessedEntityID ASTReader::findNextPreprocessedEntity(
6050                        GlobalSLocOffsetMapType::const_iterator SLocMapI) const {
6051   ++SLocMapI;
6052   for (GlobalSLocOffsetMapType::const_iterator
6053          EndI = GlobalSLocOffsetMap.end(); SLocMapI != EndI; ++SLocMapI) {
6054     ModuleFile &M = *SLocMapI->second;
6055     if (M.NumPreprocessedEntities)
6056       return M.BasePreprocessedEntityID;
6057   }
6058 
6059   return getTotalNumPreprocessedEntities();
6060 }
6061 
6062 namespace {
6063 
6064 struct PPEntityComp {
6065   const ASTReader &Reader;
6066   ModuleFile &M;
6067 
6068   PPEntityComp(const ASTReader &Reader, ModuleFile &M) : Reader(Reader), M(M) {}
6069 
6070   bool operator()(const PPEntityOffset &L, const PPEntityOffset &R) const {
6071     SourceLocation LHS = getLoc(L);
6072     SourceLocation RHS = getLoc(R);
6073     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6074   }
6075 
6076   bool operator()(const PPEntityOffset &L, SourceLocation RHS) const {
6077     SourceLocation LHS = getLoc(L);
6078     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6079   }
6080 
6081   bool operator()(SourceLocation LHS, const PPEntityOffset &R) const {
6082     SourceLocation RHS = getLoc(R);
6083     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
6084   }
6085 
6086   SourceLocation getLoc(const PPEntityOffset &PPE) const {
6087     return Reader.TranslateSourceLocation(M, PPE.getBegin());
6088   }
6089 };
6090 
6091 } // namespace
6092 
6093 PreprocessedEntityID ASTReader::findPreprocessedEntity(SourceLocation Loc,
6094                                                        bool EndsAfter) const {
6095   if (SourceMgr.isLocalSourceLocation(Loc))
6096     return getTotalNumPreprocessedEntities();
6097 
6098   GlobalSLocOffsetMapType::const_iterator SLocMapI = GlobalSLocOffsetMap.find(
6099       SourceManager::MaxLoadedOffset - Loc.getOffset() - 1);
6100   assert(SLocMapI != GlobalSLocOffsetMap.end() &&
6101          "Corrupted global sloc offset map");
6102 
6103   if (SLocMapI->second->NumPreprocessedEntities == 0)
6104     return findNextPreprocessedEntity(SLocMapI);
6105 
6106   ModuleFile &M = *SLocMapI->second;
6107 
6108   using pp_iterator = const PPEntityOffset *;
6109 
6110   pp_iterator pp_begin = M.PreprocessedEntityOffsets;
6111   pp_iterator pp_end = pp_begin + M.NumPreprocessedEntities;
6112 
6113   size_t Count = M.NumPreprocessedEntities;
6114   size_t Half;
6115   pp_iterator First = pp_begin;
6116   pp_iterator PPI;
6117 
6118   if (EndsAfter) {
6119     PPI = std::upper_bound(pp_begin, pp_end, Loc,
6120                            PPEntityComp(*this, M));
6121   } else {
6122     // Do a binary search manually instead of using std::lower_bound because
6123     // The end locations of entities may be unordered (when a macro expansion
6124     // is inside another macro argument), but for this case it is not important
6125     // whether we get the first macro expansion or its containing macro.
6126     while (Count > 0) {
6127       Half = Count / 2;
6128       PPI = First;
6129       std::advance(PPI, Half);
6130       if (SourceMgr.isBeforeInTranslationUnit(
6131               TranslateSourceLocation(M, PPI->getEnd()), Loc)) {
6132         First = PPI;
6133         ++First;
6134         Count = Count - Half - 1;
6135       } else
6136         Count = Half;
6137     }
6138   }
6139 
6140   if (PPI == pp_end)
6141     return findNextPreprocessedEntity(SLocMapI);
6142 
6143   return M.BasePreprocessedEntityID + (PPI - pp_begin);
6144 }
6145 
6146 /// Returns a pair of [Begin, End) indices of preallocated
6147 /// preprocessed entities that \arg Range encompasses.
6148 std::pair<unsigned, unsigned>
6149     ASTReader::findPreprocessedEntitiesInRange(SourceRange Range) {
6150   if (Range.isInvalid())
6151     return std::make_pair(0,0);
6152   assert(!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),Range.getBegin()));
6153 
6154   PreprocessedEntityID BeginID =
6155       findPreprocessedEntity(Range.getBegin(), false);
6156   PreprocessedEntityID EndID = findPreprocessedEntity(Range.getEnd(), true);
6157   return std::make_pair(BeginID, EndID);
6158 }
6159 
6160 /// Optionally returns true or false if the preallocated preprocessed
6161 /// entity with index \arg Index came from file \arg FID.
6162 Optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index,
6163                                                              FileID FID) {
6164   if (FID.isInvalid())
6165     return false;
6166 
6167   std::pair<ModuleFile *, unsigned> PPInfo = getModulePreprocessedEntity(Index);
6168   ModuleFile &M = *PPInfo.first;
6169   unsigned LocalIndex = PPInfo.second;
6170   const PPEntityOffset &PPOffs = M.PreprocessedEntityOffsets[LocalIndex];
6171 
6172   SourceLocation Loc = TranslateSourceLocation(M, PPOffs.getBegin());
6173   if (Loc.isInvalid())
6174     return false;
6175 
6176   if (SourceMgr.isInFileID(SourceMgr.getFileLoc(Loc), FID))
6177     return true;
6178   else
6179     return false;
6180 }
6181 
6182 namespace {
6183 
6184   /// Visitor used to search for information about a header file.
6185   class HeaderFileInfoVisitor {
6186     const FileEntry *FE;
6187     Optional<HeaderFileInfo> HFI;
6188 
6189   public:
6190     explicit HeaderFileInfoVisitor(const FileEntry *FE) : FE(FE) {}
6191 
6192     bool operator()(ModuleFile &M) {
6193       HeaderFileInfoLookupTable *Table
6194         = static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable);
6195       if (!Table)
6196         return false;
6197 
6198       // Look in the on-disk hash table for an entry for this file name.
6199       HeaderFileInfoLookupTable::iterator Pos = Table->find(FE);
6200       if (Pos == Table->end())
6201         return false;
6202 
6203       HFI = *Pos;
6204       return true;
6205     }
6206 
6207     Optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; }
6208   };
6209 
6210 } // namespace
6211 
6212 HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) {
6213   HeaderFileInfoVisitor Visitor(FE);
6214   ModuleMgr.visit(Visitor);
6215   if (Optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo())
6216     return *HFI;
6217 
6218   return HeaderFileInfo();
6219 }
6220 
6221 void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
6222   using DiagState = DiagnosticsEngine::DiagState;
6223   SmallVector<DiagState *, 32> DiagStates;
6224 
6225   for (ModuleFile &F : ModuleMgr) {
6226     unsigned Idx = 0;
6227     auto &Record = F.PragmaDiagMappings;
6228     if (Record.empty())
6229       continue;
6230 
6231     DiagStates.clear();
6232 
6233     auto ReadDiagState =
6234         [&](const DiagState &BasedOn, SourceLocation Loc,
6235             bool IncludeNonPragmaStates) -> DiagnosticsEngine::DiagState * {
6236       unsigned BackrefID = Record[Idx++];
6237       if (BackrefID != 0)
6238         return DiagStates[BackrefID - 1];
6239 
6240       // A new DiagState was created here.
6241       Diag.DiagStates.push_back(BasedOn);
6242       DiagState *NewState = &Diag.DiagStates.back();
6243       DiagStates.push_back(NewState);
6244       unsigned Size = Record[Idx++];
6245       assert(Idx + Size * 2 <= Record.size() &&
6246              "Invalid data, not enough diag/map pairs");
6247       while (Size--) {
6248         unsigned DiagID = Record[Idx++];
6249         DiagnosticMapping NewMapping =
6250             DiagnosticMapping::deserialize(Record[Idx++]);
6251         if (!NewMapping.isPragma() && !IncludeNonPragmaStates)
6252           continue;
6253 
6254         DiagnosticMapping &Mapping = NewState->getOrAddMapping(DiagID);
6255 
6256         // If this mapping was specified as a warning but the severity was
6257         // upgraded due to diagnostic settings, simulate the current diagnostic
6258         // settings (and use a warning).
6259         if (NewMapping.wasUpgradedFromWarning() && !Mapping.isErrorOrFatal()) {
6260           NewMapping.setSeverity(diag::Severity::Warning);
6261           NewMapping.setUpgradedFromWarning(false);
6262         }
6263 
6264         Mapping = NewMapping;
6265       }
6266       return NewState;
6267     };
6268 
6269     // Read the first state.
6270     DiagState *FirstState;
6271     if (F.Kind == MK_ImplicitModule) {
6272       // Implicitly-built modules are reused with different diagnostic
6273       // settings.  Use the initial diagnostic state from Diag to simulate this
6274       // compilation's diagnostic settings.
6275       FirstState = Diag.DiagStatesByLoc.FirstDiagState;
6276       DiagStates.push_back(FirstState);
6277 
6278       // Skip the initial diagnostic state from the serialized module.
6279       assert(Record[1] == 0 &&
6280              "Invalid data, unexpected backref in initial state");
6281       Idx = 3 + Record[2] * 2;
6282       assert(Idx < Record.size() &&
6283              "Invalid data, not enough state change pairs in initial state");
6284     } else if (F.isModule()) {
6285       // For an explicit module, preserve the flags from the module build
6286       // command line (-w, -Weverything, -Werror, ...) along with any explicit
6287       // -Wblah flags.
6288       unsigned Flags = Record[Idx++];
6289       DiagState Initial;
6290       Initial.SuppressSystemWarnings = Flags & 1; Flags >>= 1;
6291       Initial.ErrorsAsFatal = Flags & 1; Flags >>= 1;
6292       Initial.WarningsAsErrors = Flags & 1; Flags >>= 1;
6293       Initial.EnableAllWarnings = Flags & 1; Flags >>= 1;
6294       Initial.IgnoreAllWarnings = Flags & 1; Flags >>= 1;
6295       Initial.ExtBehavior = (diag::Severity)Flags;
6296       FirstState = ReadDiagState(Initial, SourceLocation(), true);
6297 
6298       assert(F.OriginalSourceFileID.isValid());
6299 
6300       // Set up the root buffer of the module to start with the initial
6301       // diagnostic state of the module itself, to cover files that contain no
6302       // explicit transitions (for which we did not serialize anything).
6303       Diag.DiagStatesByLoc.Files[F.OriginalSourceFileID]
6304           .StateTransitions.push_back({FirstState, 0});
6305     } else {
6306       // For prefix ASTs, start with whatever the user configured on the
6307       // command line.
6308       Idx++; // Skip flags.
6309       FirstState = ReadDiagState(*Diag.DiagStatesByLoc.CurDiagState,
6310                                  SourceLocation(), false);
6311     }
6312 
6313     // Read the state transitions.
6314     unsigned NumLocations = Record[Idx++];
6315     while (NumLocations--) {
6316       assert(Idx < Record.size() &&
6317              "Invalid data, missing pragma diagnostic states");
6318       SourceLocation Loc = ReadSourceLocation(F, Record[Idx++]);
6319       auto IDAndOffset = SourceMgr.getDecomposedLoc(Loc);
6320       assert(IDAndOffset.first.isValid() && "invalid FileID for transition");
6321       assert(IDAndOffset.second == 0 && "not a start location for a FileID");
6322       unsigned Transitions = Record[Idx++];
6323 
6324       // Note that we don't need to set up Parent/ParentOffset here, because
6325       // we won't be changing the diagnostic state within imported FileIDs
6326       // (other than perhaps appending to the main source file, which has no
6327       // parent).
6328       auto &F = Diag.DiagStatesByLoc.Files[IDAndOffset.first];
6329       F.StateTransitions.reserve(F.StateTransitions.size() + Transitions);
6330       for (unsigned I = 0; I != Transitions; ++I) {
6331         unsigned Offset = Record[Idx++];
6332         auto *State =
6333             ReadDiagState(*FirstState, Loc.getLocWithOffset(Offset), false);
6334         F.StateTransitions.push_back({State, Offset});
6335       }
6336     }
6337 
6338     // Read the final state.
6339     assert(Idx < Record.size() &&
6340            "Invalid data, missing final pragma diagnostic state");
6341     SourceLocation CurStateLoc =
6342         ReadSourceLocation(F, F.PragmaDiagMappings[Idx++]);
6343     auto *CurState = ReadDiagState(*FirstState, CurStateLoc, false);
6344 
6345     if (!F.isModule()) {
6346       Diag.DiagStatesByLoc.CurDiagState = CurState;
6347       Diag.DiagStatesByLoc.CurDiagStateLoc = CurStateLoc;
6348 
6349       // Preserve the property that the imaginary root file describes the
6350       // current state.
6351       FileID NullFile;
6352       auto &T = Diag.DiagStatesByLoc.Files[NullFile].StateTransitions;
6353       if (T.empty())
6354         T.push_back({CurState, 0});
6355       else
6356         T[0].State = CurState;
6357     }
6358 
6359     // Don't try to read these mappings again.
6360     Record.clear();
6361   }
6362 }
6363 
6364 /// Get the correct cursor and offset for loading a type.
6365 ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6366   GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6367   assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6368   ModuleFile *M = I->second;
6369   return RecordLocation(
6370       M, M->TypeOffsets[Index - M->BaseTypeIndex].getBitOffset() +
6371              M->DeclsBlockStartOffset);
6372 }
6373 
6374 static llvm::Optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
6375   switch (code) {
6376 #define TYPE_BIT_CODE(CLASS_ID, CODE_ID, CODE_VALUE) \
6377   case TYPE_##CODE_ID: return Type::CLASS_ID;
6378 #include "clang/Serialization/TypeBitCodes.def"
6379   default: return llvm::None;
6380   }
6381 }
6382 
6383 /// Read and return the type with the given index..
6384 ///
6385 /// The index is the type ID, shifted and minus the number of predefs. This
6386 /// routine actually reads the record corresponding to the type at the given
6387 /// location. It is a helper routine for GetType, which deals with reading type
6388 /// IDs.
6389 QualType ASTReader::readTypeRecord(unsigned Index) {
6390   assert(ContextObj && "reading type with no AST context");
6391   ASTContext &Context = *ContextObj;
6392   RecordLocation Loc = TypeCursorForIndex(Index);
6393   BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
6394 
6395   // Keep track of where we are in the stream, then jump back there
6396   // after reading this type.
6397   SavedStreamPosition SavedPosition(DeclsCursor);
6398 
6399   ReadingKindTracker ReadingKind(Read_Type, *this);
6400 
6401   // Note that we are loading a type record.
6402   Deserializing AType(this);
6403 
6404   if (llvm::Error Err = DeclsCursor.JumpToBit(Loc.Offset)) {
6405     Error(std::move(Err));
6406     return QualType();
6407   }
6408   Expected<unsigned> RawCode = DeclsCursor.ReadCode();
6409   if (!RawCode) {
6410     Error(RawCode.takeError());
6411     return QualType();
6412   }
6413 
6414   ASTRecordReader Record(*this, *Loc.F);
6415   Expected<unsigned> Code = Record.readRecord(DeclsCursor, RawCode.get());
6416   if (!Code) {
6417     Error(Code.takeError());
6418     return QualType();
6419   }
6420   if (Code.get() == TYPE_EXT_QUAL) {
6421     QualType baseType = Record.readQualType();
6422     Qualifiers quals = Record.readQualifiers();
6423     return Context.getQualifiedType(baseType, quals);
6424   }
6425 
6426   auto maybeClass = getTypeClassForCode((TypeCode) Code.get());
6427   if (!maybeClass) {
6428     Error("Unexpected code for type");
6429     return QualType();
6430   }
6431 
6432   serialization::AbstractTypeReader<ASTRecordReader> TypeReader(Record);
6433   return TypeReader.read(*maybeClass);
6434 }
6435 
6436 namespace clang {
6437 
6438 class TypeLocReader : public TypeLocVisitor<TypeLocReader> {
6439   ASTRecordReader &Reader;
6440 
6441   SourceLocation readSourceLocation() {
6442     return Reader.readSourceLocation();
6443   }
6444 
6445   TypeSourceInfo *GetTypeSourceInfo() {
6446     return Reader.readTypeSourceInfo();
6447   }
6448 
6449   NestedNameSpecifierLoc ReadNestedNameSpecifierLoc() {
6450     return Reader.readNestedNameSpecifierLoc();
6451   }
6452 
6453   Attr *ReadAttr() {
6454     return Reader.readAttr();
6455   }
6456 
6457 public:
6458   TypeLocReader(ASTRecordReader &Reader) : Reader(Reader) {}
6459 
6460   // We want compile-time assurance that we've enumerated all of
6461   // these, so unfortunately we have to declare them first, then
6462   // define them out-of-line.
6463 #define ABSTRACT_TYPELOC(CLASS, PARENT)
6464 #define TYPELOC(CLASS, PARENT) \
6465   void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
6466 #include "clang/AST/TypeLocNodes.def"
6467 
6468   void VisitFunctionTypeLoc(FunctionTypeLoc);
6469   void VisitArrayTypeLoc(ArrayTypeLoc);
6470 };
6471 
6472 } // namespace clang
6473 
6474 void TypeLocReader::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
6475   // nothing to do
6476 }
6477 
6478 void TypeLocReader::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
6479   TL.setBuiltinLoc(readSourceLocation());
6480   if (TL.needsExtraLocalData()) {
6481     TL.setWrittenTypeSpec(static_cast<DeclSpec::TST>(Reader.readInt()));
6482     TL.setWrittenSignSpec(static_cast<DeclSpec::TSS>(Reader.readInt()));
6483     TL.setWrittenWidthSpec(static_cast<DeclSpec::TSW>(Reader.readInt()));
6484     TL.setModeAttr(Reader.readInt());
6485   }
6486 }
6487 
6488 void TypeLocReader::VisitComplexTypeLoc(ComplexTypeLoc TL) {
6489   TL.setNameLoc(readSourceLocation());
6490 }
6491 
6492 void TypeLocReader::VisitPointerTypeLoc(PointerTypeLoc TL) {
6493   TL.setStarLoc(readSourceLocation());
6494 }
6495 
6496 void TypeLocReader::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
6497   // nothing to do
6498 }
6499 
6500 void TypeLocReader::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
6501   // nothing to do
6502 }
6503 
6504 void TypeLocReader::VisitMacroQualifiedTypeLoc(MacroQualifiedTypeLoc TL) {
6505   TL.setExpansionLoc(readSourceLocation());
6506 }
6507 
6508 void TypeLocReader::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
6509   TL.setCaretLoc(readSourceLocation());
6510 }
6511 
6512 void TypeLocReader::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
6513   TL.setAmpLoc(readSourceLocation());
6514 }
6515 
6516 void TypeLocReader::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
6517   TL.setAmpAmpLoc(readSourceLocation());
6518 }
6519 
6520 void TypeLocReader::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
6521   TL.setStarLoc(readSourceLocation());
6522   TL.setClassTInfo(GetTypeSourceInfo());
6523 }
6524 
6525 void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) {
6526   TL.setLBracketLoc(readSourceLocation());
6527   TL.setRBracketLoc(readSourceLocation());
6528   if (Reader.readBool())
6529     TL.setSizeExpr(Reader.readExpr());
6530   else
6531     TL.setSizeExpr(nullptr);
6532 }
6533 
6534 void TypeLocReader::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
6535   VisitArrayTypeLoc(TL);
6536 }
6537 
6538 void TypeLocReader::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
6539   VisitArrayTypeLoc(TL);
6540 }
6541 
6542 void TypeLocReader::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
6543   VisitArrayTypeLoc(TL);
6544 }
6545 
6546 void TypeLocReader::VisitDependentSizedArrayTypeLoc(
6547                                             DependentSizedArrayTypeLoc TL) {
6548   VisitArrayTypeLoc(TL);
6549 }
6550 
6551 void TypeLocReader::VisitDependentAddressSpaceTypeLoc(
6552     DependentAddressSpaceTypeLoc TL) {
6553 
6554     TL.setAttrNameLoc(readSourceLocation());
6555     TL.setAttrOperandParensRange(Reader.readSourceRange());
6556     TL.setAttrExprOperand(Reader.readExpr());
6557 }
6558 
6559 void TypeLocReader::VisitDependentSizedExtVectorTypeLoc(
6560                                         DependentSizedExtVectorTypeLoc TL) {
6561   TL.setNameLoc(readSourceLocation());
6562 }
6563 
6564 void TypeLocReader::VisitVectorTypeLoc(VectorTypeLoc TL) {
6565   TL.setNameLoc(readSourceLocation());
6566 }
6567 
6568 void TypeLocReader::VisitDependentVectorTypeLoc(
6569     DependentVectorTypeLoc TL) {
6570   TL.setNameLoc(readSourceLocation());
6571 }
6572 
6573 void TypeLocReader::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
6574   TL.setNameLoc(readSourceLocation());
6575 }
6576 
6577 void TypeLocReader::VisitConstantMatrixTypeLoc(ConstantMatrixTypeLoc TL) {
6578   TL.setAttrNameLoc(readSourceLocation());
6579   TL.setAttrOperandParensRange(Reader.readSourceRange());
6580   TL.setAttrRowOperand(Reader.readExpr());
6581   TL.setAttrColumnOperand(Reader.readExpr());
6582 }
6583 
6584 void TypeLocReader::VisitDependentSizedMatrixTypeLoc(
6585     DependentSizedMatrixTypeLoc TL) {
6586   TL.setAttrNameLoc(readSourceLocation());
6587   TL.setAttrOperandParensRange(Reader.readSourceRange());
6588   TL.setAttrRowOperand(Reader.readExpr());
6589   TL.setAttrColumnOperand(Reader.readExpr());
6590 }
6591 
6592 void TypeLocReader::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
6593   TL.setLocalRangeBegin(readSourceLocation());
6594   TL.setLParenLoc(readSourceLocation());
6595   TL.setRParenLoc(readSourceLocation());
6596   TL.setExceptionSpecRange(Reader.readSourceRange());
6597   TL.setLocalRangeEnd(readSourceLocation());
6598   for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i) {
6599     TL.setParam(i, Reader.readDeclAs<ParmVarDecl>());
6600   }
6601 }
6602 
6603 void TypeLocReader::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
6604   VisitFunctionTypeLoc(TL);
6605 }
6606 
6607 void TypeLocReader::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
6608   VisitFunctionTypeLoc(TL);
6609 }
6610 
6611 void TypeLocReader::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
6612   TL.setNameLoc(readSourceLocation());
6613 }
6614 
6615 void TypeLocReader::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
6616   TL.setNameLoc(readSourceLocation());
6617 }
6618 
6619 void TypeLocReader::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
6620   TL.setTypeofLoc(readSourceLocation());
6621   TL.setLParenLoc(readSourceLocation());
6622   TL.setRParenLoc(readSourceLocation());
6623 }
6624 
6625 void TypeLocReader::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
6626   TL.setTypeofLoc(readSourceLocation());
6627   TL.setLParenLoc(readSourceLocation());
6628   TL.setRParenLoc(readSourceLocation());
6629   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6630 }
6631 
6632 void TypeLocReader::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
6633   TL.setNameLoc(readSourceLocation());
6634 }
6635 
6636 void TypeLocReader::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
6637   TL.setKWLoc(readSourceLocation());
6638   TL.setLParenLoc(readSourceLocation());
6639   TL.setRParenLoc(readSourceLocation());
6640   TL.setUnderlyingTInfo(GetTypeSourceInfo());
6641 }
6642 
6643 void TypeLocReader::VisitAutoTypeLoc(AutoTypeLoc TL) {
6644   TL.setNameLoc(readSourceLocation());
6645   if (Reader.readBool()) {
6646     TL.setNestedNameSpecifierLoc(ReadNestedNameSpecifierLoc());
6647     TL.setTemplateKWLoc(readSourceLocation());
6648     TL.setConceptNameLoc(readSourceLocation());
6649     TL.setFoundDecl(Reader.readDeclAs<NamedDecl>());
6650     TL.setLAngleLoc(readSourceLocation());
6651     TL.setRAngleLoc(readSourceLocation());
6652     for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6653       TL.setArgLocInfo(i, Reader.readTemplateArgumentLocInfo(
6654                               TL.getTypePtr()->getArg(i).getKind()));
6655   }
6656 }
6657 
6658 void TypeLocReader::VisitDeducedTemplateSpecializationTypeLoc(
6659     DeducedTemplateSpecializationTypeLoc TL) {
6660   TL.setTemplateNameLoc(readSourceLocation());
6661 }
6662 
6663 void TypeLocReader::VisitRecordTypeLoc(RecordTypeLoc TL) {
6664   TL.setNameLoc(readSourceLocation());
6665 }
6666 
6667 void TypeLocReader::VisitEnumTypeLoc(EnumTypeLoc TL) {
6668   TL.setNameLoc(readSourceLocation());
6669 }
6670 
6671 void TypeLocReader::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
6672   TL.setAttr(ReadAttr());
6673 }
6674 
6675 void TypeLocReader::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
6676   TL.setNameLoc(readSourceLocation());
6677 }
6678 
6679 void TypeLocReader::VisitSubstTemplateTypeParmTypeLoc(
6680                                             SubstTemplateTypeParmTypeLoc TL) {
6681   TL.setNameLoc(readSourceLocation());
6682 }
6683 
6684 void TypeLocReader::VisitSubstTemplateTypeParmPackTypeLoc(
6685                                           SubstTemplateTypeParmPackTypeLoc TL) {
6686   TL.setNameLoc(readSourceLocation());
6687 }
6688 
6689 void TypeLocReader::VisitTemplateSpecializationTypeLoc(
6690                                            TemplateSpecializationTypeLoc TL) {
6691   TL.setTemplateKeywordLoc(readSourceLocation());
6692   TL.setTemplateNameLoc(readSourceLocation());
6693   TL.setLAngleLoc(readSourceLocation());
6694   TL.setRAngleLoc(readSourceLocation());
6695   for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
6696     TL.setArgLocInfo(
6697         i,
6698         Reader.readTemplateArgumentLocInfo(
6699           TL.getTypePtr()->getArg(i).getKind()));
6700 }
6701 
6702 void TypeLocReader::VisitParenTypeLoc(ParenTypeLoc TL) {
6703   TL.setLParenLoc(readSourceLocation());
6704   TL.setRParenLoc(readSourceLocation());
6705 }
6706 
6707 void TypeLocReader::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
6708   TL.setElaboratedKeywordLoc(readSourceLocation());
6709   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6710 }
6711 
6712 void TypeLocReader::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
6713   TL.setNameLoc(readSourceLocation());
6714 }
6715 
6716 void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
6717   TL.setElaboratedKeywordLoc(readSourceLocation());
6718   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6719   TL.setNameLoc(readSourceLocation());
6720 }
6721 
6722 void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc(
6723        DependentTemplateSpecializationTypeLoc TL) {
6724   TL.setElaboratedKeywordLoc(readSourceLocation());
6725   TL.setQualifierLoc(ReadNestedNameSpecifierLoc());
6726   TL.setTemplateKeywordLoc(readSourceLocation());
6727   TL.setTemplateNameLoc(readSourceLocation());
6728   TL.setLAngleLoc(readSourceLocation());
6729   TL.setRAngleLoc(readSourceLocation());
6730   for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
6731     TL.setArgLocInfo(
6732         I,
6733         Reader.readTemplateArgumentLocInfo(
6734             TL.getTypePtr()->getArg(I).getKind()));
6735 }
6736 
6737 void TypeLocReader::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
6738   TL.setEllipsisLoc(readSourceLocation());
6739 }
6740 
6741 void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
6742   TL.setNameLoc(readSourceLocation());
6743 }
6744 
6745 void TypeLocReader::VisitObjCTypeParamTypeLoc(ObjCTypeParamTypeLoc TL) {
6746   if (TL.getNumProtocols()) {
6747     TL.setProtocolLAngleLoc(readSourceLocation());
6748     TL.setProtocolRAngleLoc(readSourceLocation());
6749   }
6750   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6751     TL.setProtocolLoc(i, readSourceLocation());
6752 }
6753 
6754 void TypeLocReader::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
6755   TL.setHasBaseTypeAsWritten(Reader.readBool());
6756   TL.setTypeArgsLAngleLoc(readSourceLocation());
6757   TL.setTypeArgsRAngleLoc(readSourceLocation());
6758   for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
6759     TL.setTypeArgTInfo(i, GetTypeSourceInfo());
6760   TL.setProtocolLAngleLoc(readSourceLocation());
6761   TL.setProtocolRAngleLoc(readSourceLocation());
6762   for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
6763     TL.setProtocolLoc(i, readSourceLocation());
6764 }
6765 
6766 void TypeLocReader::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
6767   TL.setStarLoc(readSourceLocation());
6768 }
6769 
6770 void TypeLocReader::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
6771   TL.setKWLoc(readSourceLocation());
6772   TL.setLParenLoc(readSourceLocation());
6773   TL.setRParenLoc(readSourceLocation());
6774 }
6775 
6776 void TypeLocReader::VisitPipeTypeLoc(PipeTypeLoc TL) {
6777   TL.setKWLoc(readSourceLocation());
6778 }
6779 
6780 void TypeLocReader::VisitExtIntTypeLoc(clang::ExtIntTypeLoc TL) {
6781   TL.setNameLoc(readSourceLocation());
6782 }
6783 void TypeLocReader::VisitDependentExtIntTypeLoc(
6784     clang::DependentExtIntTypeLoc TL) {
6785   TL.setNameLoc(readSourceLocation());
6786 }
6787 
6788 
6789 void ASTRecordReader::readTypeLoc(TypeLoc TL) {
6790   TypeLocReader TLR(*this);
6791   for (; !TL.isNull(); TL = TL.getNextTypeLoc())
6792     TLR.Visit(TL);
6793 }
6794 
6795 TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
6796   QualType InfoTy = readType();
6797   if (InfoTy.isNull())
6798     return nullptr;
6799 
6800   TypeSourceInfo *TInfo = getContext().CreateTypeSourceInfo(InfoTy);
6801   readTypeLoc(TInfo->getTypeLoc());
6802   return TInfo;
6803 }
6804 
6805 QualType ASTReader::GetType(TypeID ID) {
6806   assert(ContextObj && "reading type with no AST context");
6807   ASTContext &Context = *ContextObj;
6808 
6809   unsigned FastQuals = ID & Qualifiers::FastMask;
6810   unsigned Index = ID >> Qualifiers::FastWidth;
6811 
6812   if (Index < NUM_PREDEF_TYPE_IDS) {
6813     QualType T;
6814     switch ((PredefinedTypeIDs)Index) {
6815     case PREDEF_TYPE_NULL_ID:
6816       return QualType();
6817     case PREDEF_TYPE_VOID_ID:
6818       T = Context.VoidTy;
6819       break;
6820     case PREDEF_TYPE_BOOL_ID:
6821       T = Context.BoolTy;
6822       break;
6823     case PREDEF_TYPE_CHAR_U_ID:
6824     case PREDEF_TYPE_CHAR_S_ID:
6825       // FIXME: Check that the signedness of CharTy is correct!
6826       T = Context.CharTy;
6827       break;
6828     case PREDEF_TYPE_UCHAR_ID:
6829       T = Context.UnsignedCharTy;
6830       break;
6831     case PREDEF_TYPE_USHORT_ID:
6832       T = Context.UnsignedShortTy;
6833       break;
6834     case PREDEF_TYPE_UINT_ID:
6835       T = Context.UnsignedIntTy;
6836       break;
6837     case PREDEF_TYPE_ULONG_ID:
6838       T = Context.UnsignedLongTy;
6839       break;
6840     case PREDEF_TYPE_ULONGLONG_ID:
6841       T = Context.UnsignedLongLongTy;
6842       break;
6843     case PREDEF_TYPE_UINT128_ID:
6844       T = Context.UnsignedInt128Ty;
6845       break;
6846     case PREDEF_TYPE_SCHAR_ID:
6847       T = Context.SignedCharTy;
6848       break;
6849     case PREDEF_TYPE_WCHAR_ID:
6850       T = Context.WCharTy;
6851       break;
6852     case PREDEF_TYPE_SHORT_ID:
6853       T = Context.ShortTy;
6854       break;
6855     case PREDEF_TYPE_INT_ID:
6856       T = Context.IntTy;
6857       break;
6858     case PREDEF_TYPE_LONG_ID:
6859       T = Context.LongTy;
6860       break;
6861     case PREDEF_TYPE_LONGLONG_ID:
6862       T = Context.LongLongTy;
6863       break;
6864     case PREDEF_TYPE_INT128_ID:
6865       T = Context.Int128Ty;
6866       break;
6867     case PREDEF_TYPE_BFLOAT16_ID:
6868       T = Context.BFloat16Ty;
6869       break;
6870     case PREDEF_TYPE_HALF_ID:
6871       T = Context.HalfTy;
6872       break;
6873     case PREDEF_TYPE_FLOAT_ID:
6874       T = Context.FloatTy;
6875       break;
6876     case PREDEF_TYPE_DOUBLE_ID:
6877       T = Context.DoubleTy;
6878       break;
6879     case PREDEF_TYPE_LONGDOUBLE_ID:
6880       T = Context.LongDoubleTy;
6881       break;
6882     case PREDEF_TYPE_SHORT_ACCUM_ID:
6883       T = Context.ShortAccumTy;
6884       break;
6885     case PREDEF_TYPE_ACCUM_ID:
6886       T = Context.AccumTy;
6887       break;
6888     case PREDEF_TYPE_LONG_ACCUM_ID:
6889       T = Context.LongAccumTy;
6890       break;
6891     case PREDEF_TYPE_USHORT_ACCUM_ID:
6892       T = Context.UnsignedShortAccumTy;
6893       break;
6894     case PREDEF_TYPE_UACCUM_ID:
6895       T = Context.UnsignedAccumTy;
6896       break;
6897     case PREDEF_TYPE_ULONG_ACCUM_ID:
6898       T = Context.UnsignedLongAccumTy;
6899       break;
6900     case PREDEF_TYPE_SHORT_FRACT_ID:
6901       T = Context.ShortFractTy;
6902       break;
6903     case PREDEF_TYPE_FRACT_ID:
6904       T = Context.FractTy;
6905       break;
6906     case PREDEF_TYPE_LONG_FRACT_ID:
6907       T = Context.LongFractTy;
6908       break;
6909     case PREDEF_TYPE_USHORT_FRACT_ID:
6910       T = Context.UnsignedShortFractTy;
6911       break;
6912     case PREDEF_TYPE_UFRACT_ID:
6913       T = Context.UnsignedFractTy;
6914       break;
6915     case PREDEF_TYPE_ULONG_FRACT_ID:
6916       T = Context.UnsignedLongFractTy;
6917       break;
6918     case PREDEF_TYPE_SAT_SHORT_ACCUM_ID:
6919       T = Context.SatShortAccumTy;
6920       break;
6921     case PREDEF_TYPE_SAT_ACCUM_ID:
6922       T = Context.SatAccumTy;
6923       break;
6924     case PREDEF_TYPE_SAT_LONG_ACCUM_ID:
6925       T = Context.SatLongAccumTy;
6926       break;
6927     case PREDEF_TYPE_SAT_USHORT_ACCUM_ID:
6928       T = Context.SatUnsignedShortAccumTy;
6929       break;
6930     case PREDEF_TYPE_SAT_UACCUM_ID:
6931       T = Context.SatUnsignedAccumTy;
6932       break;
6933     case PREDEF_TYPE_SAT_ULONG_ACCUM_ID:
6934       T = Context.SatUnsignedLongAccumTy;
6935       break;
6936     case PREDEF_TYPE_SAT_SHORT_FRACT_ID:
6937       T = Context.SatShortFractTy;
6938       break;
6939     case PREDEF_TYPE_SAT_FRACT_ID:
6940       T = Context.SatFractTy;
6941       break;
6942     case PREDEF_TYPE_SAT_LONG_FRACT_ID:
6943       T = Context.SatLongFractTy;
6944       break;
6945     case PREDEF_TYPE_SAT_USHORT_FRACT_ID:
6946       T = Context.SatUnsignedShortFractTy;
6947       break;
6948     case PREDEF_TYPE_SAT_UFRACT_ID:
6949       T = Context.SatUnsignedFractTy;
6950       break;
6951     case PREDEF_TYPE_SAT_ULONG_FRACT_ID:
6952       T = Context.SatUnsignedLongFractTy;
6953       break;
6954     case PREDEF_TYPE_FLOAT16_ID:
6955       T = Context.Float16Ty;
6956       break;
6957     case PREDEF_TYPE_FLOAT128_ID:
6958       T = Context.Float128Ty;
6959       break;
6960     case PREDEF_TYPE_OVERLOAD_ID:
6961       T = Context.OverloadTy;
6962       break;
6963     case PREDEF_TYPE_BOUND_MEMBER:
6964       T = Context.BoundMemberTy;
6965       break;
6966     case PREDEF_TYPE_PSEUDO_OBJECT:
6967       T = Context.PseudoObjectTy;
6968       break;
6969     case PREDEF_TYPE_DEPENDENT_ID:
6970       T = Context.DependentTy;
6971       break;
6972     case PREDEF_TYPE_UNKNOWN_ANY:
6973       T = Context.UnknownAnyTy;
6974       break;
6975     case PREDEF_TYPE_NULLPTR_ID:
6976       T = Context.NullPtrTy;
6977       break;
6978     case PREDEF_TYPE_CHAR8_ID:
6979       T = Context.Char8Ty;
6980       break;
6981     case PREDEF_TYPE_CHAR16_ID:
6982       T = Context.Char16Ty;
6983       break;
6984     case PREDEF_TYPE_CHAR32_ID:
6985       T = Context.Char32Ty;
6986       break;
6987     case PREDEF_TYPE_OBJC_ID:
6988       T = Context.ObjCBuiltinIdTy;
6989       break;
6990     case PREDEF_TYPE_OBJC_CLASS:
6991       T = Context.ObjCBuiltinClassTy;
6992       break;
6993     case PREDEF_TYPE_OBJC_SEL:
6994       T = Context.ObjCBuiltinSelTy;
6995       break;
6996 #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
6997     case PREDEF_TYPE_##Id##_ID: \
6998       T = Context.SingletonId; \
6999       break;
7000 #include "clang/Basic/OpenCLImageTypes.def"
7001 #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
7002     case PREDEF_TYPE_##Id##_ID: \
7003       T = Context.Id##Ty; \
7004       break;
7005 #include "clang/Basic/OpenCLExtensionTypes.def"
7006     case PREDEF_TYPE_SAMPLER_ID:
7007       T = Context.OCLSamplerTy;
7008       break;
7009     case PREDEF_TYPE_EVENT_ID:
7010       T = Context.OCLEventTy;
7011       break;
7012     case PREDEF_TYPE_CLK_EVENT_ID:
7013       T = Context.OCLClkEventTy;
7014       break;
7015     case PREDEF_TYPE_QUEUE_ID:
7016       T = Context.OCLQueueTy;
7017       break;
7018     case PREDEF_TYPE_RESERVE_ID_ID:
7019       T = Context.OCLReserveIDTy;
7020       break;
7021     case PREDEF_TYPE_AUTO_DEDUCT:
7022       T = Context.getAutoDeductType();
7023       break;
7024     case PREDEF_TYPE_AUTO_RREF_DEDUCT:
7025       T = Context.getAutoRRefDeductType();
7026       break;
7027     case PREDEF_TYPE_ARC_UNBRIDGED_CAST:
7028       T = Context.ARCUnbridgedCastTy;
7029       break;
7030     case PREDEF_TYPE_BUILTIN_FN:
7031       T = Context.BuiltinFnTy;
7032       break;
7033     case PREDEF_TYPE_INCOMPLETE_MATRIX_IDX:
7034       T = Context.IncompleteMatrixIdxTy;
7035       break;
7036     case PREDEF_TYPE_OMP_ARRAY_SECTION:
7037       T = Context.OMPArraySectionTy;
7038       break;
7039     case PREDEF_TYPE_OMP_ARRAY_SHAPING:
7040       T = Context.OMPArraySectionTy;
7041       break;
7042     case PREDEF_TYPE_OMP_ITERATOR:
7043       T = Context.OMPIteratorTy;
7044       break;
7045 #define SVE_TYPE(Name, Id, SingletonId) \
7046     case PREDEF_TYPE_##Id##_ID: \
7047       T = Context.SingletonId; \
7048       break;
7049 #include "clang/Basic/AArch64SVEACLETypes.def"
7050     }
7051 
7052     assert(!T.isNull() && "Unknown predefined type");
7053     return T.withFastQualifiers(FastQuals);
7054   }
7055 
7056   Index -= NUM_PREDEF_TYPE_IDS;
7057   assert(Index < TypesLoaded.size() && "Type index out-of-range");
7058   if (TypesLoaded[Index].isNull()) {
7059     TypesLoaded[Index] = readTypeRecord(Index);
7060     if (TypesLoaded[Index].isNull())
7061       return QualType();
7062 
7063     TypesLoaded[Index]->setFromAST();
7064     if (DeserializationListener)
7065       DeserializationListener->TypeRead(TypeIdx::fromTypeID(ID),
7066                                         TypesLoaded[Index]);
7067   }
7068 
7069   return TypesLoaded[Index].withFastQualifiers(FastQuals);
7070 }
7071 
7072 QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7073   return GetType(getGlobalTypeID(F, LocalID));
7074 }
7075 
7076 serialization::TypeID
7077 ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7078   unsigned FastQuals = LocalID & Qualifiers::FastMask;
7079   unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7080 
7081   if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7082     return LocalID;
7083 
7084   if (!F.ModuleOffsetMap.empty())
7085     ReadModuleOffsetMap(F);
7086 
7087   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7088     = F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7089   assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7090 
7091   unsigned GlobalIndex = LocalIndex + I->second;
7092   return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7093 }
7094 
7095 TemplateArgumentLocInfo
7096 ASTRecordReader::readTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind) {
7097   switch (Kind) {
7098   case TemplateArgument::Expression:
7099     return readExpr();
7100   case TemplateArgument::Type:
7101     return readTypeSourceInfo();
7102   case TemplateArgument::Template: {
7103     NestedNameSpecifierLoc QualifierLoc =
7104       readNestedNameSpecifierLoc();
7105     SourceLocation TemplateNameLoc = readSourceLocation();
7106     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7107                                    SourceLocation());
7108   }
7109   case TemplateArgument::TemplateExpansion: {
7110     NestedNameSpecifierLoc QualifierLoc = readNestedNameSpecifierLoc();
7111     SourceLocation TemplateNameLoc = readSourceLocation();
7112     SourceLocation EllipsisLoc = readSourceLocation();
7113     return TemplateArgumentLocInfo(QualifierLoc, TemplateNameLoc,
7114                                    EllipsisLoc);
7115   }
7116   case TemplateArgument::Null:
7117   case TemplateArgument::Integral:
7118   case TemplateArgument::Declaration:
7119   case TemplateArgument::NullPtr:
7120   case TemplateArgument::Pack:
7121     // FIXME: Is this right?
7122     return TemplateArgumentLocInfo();
7123   }
7124   llvm_unreachable("unexpected template argument loc");
7125 }
7126 
7127 TemplateArgumentLoc ASTRecordReader::readTemplateArgumentLoc() {
7128   TemplateArgument Arg = readTemplateArgument();
7129 
7130   if (Arg.getKind() == TemplateArgument::Expression) {
7131     if (readBool()) // bool InfoHasSameExpr.
7132       return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr()));
7133   }
7134   return TemplateArgumentLoc(Arg, readTemplateArgumentLocInfo(Arg.getKind()));
7135 }
7136 
7137 const ASTTemplateArgumentListInfo *
7138 ASTRecordReader::readASTTemplateArgumentListInfo() {
7139   SourceLocation LAngleLoc = readSourceLocation();
7140   SourceLocation RAngleLoc = readSourceLocation();
7141   unsigned NumArgsAsWritten = readInt();
7142   TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
7143   for (unsigned i = 0; i != NumArgsAsWritten; ++i)
7144     TemplArgsInfo.addArgument(readTemplateArgumentLoc());
7145   return ASTTemplateArgumentListInfo::Create(getContext(), TemplArgsInfo);
7146 }
7147 
7148 Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7149   return GetDecl(ID);
7150 }
7151 
7152 void ASTReader::CompleteRedeclChain(const Decl *D) {
7153   if (NumCurrentElementsDeserializing) {
7154     // We arrange to not care about the complete redeclaration chain while we're
7155     // deserializing. Just remember that the AST has marked this one as complete
7156     // but that it's not actually complete yet, so we know we still need to
7157     // complete it later.
7158     PendingIncompleteDeclChains.push_back(const_cast<Decl*>(D));
7159     return;
7160   }
7161 
7162   const DeclContext *DC = D->getDeclContext()->getRedeclContext();
7163 
7164   // If this is a named declaration, complete it by looking it up
7165   // within its context.
7166   //
7167   // FIXME: Merging a function definition should merge
7168   // all mergeable entities within it.
7169   if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC) ||
7170       isa<CXXRecordDecl>(DC) || isa<EnumDecl>(DC)) {
7171     if (DeclarationName Name = cast<NamedDecl>(D)->getDeclName()) {
7172       if (!getContext().getLangOpts().CPlusPlus &&
7173           isa<TranslationUnitDecl>(DC)) {
7174         // Outside of C++, we don't have a lookup table for the TU, so update
7175         // the identifier instead. (For C++ modules, we don't store decls
7176         // in the serialized identifier table, so we do the lookup in the TU.)
7177         auto *II = Name.getAsIdentifierInfo();
7178         assert(II && "non-identifier name in C?");
7179         if (II->isOutOfDate())
7180           updateOutOfDateIdentifier(*II);
7181       } else
7182         DC->lookup(Name);
7183     } else if (needsAnonymousDeclarationNumber(cast<NamedDecl>(D))) {
7184       // Find all declarations of this kind from the relevant context.
7185       for (auto *DCDecl : cast<Decl>(D->getLexicalDeclContext())->redecls()) {
7186         auto *DC = cast<DeclContext>(DCDecl);
7187         SmallVector<Decl*, 8> Decls;
7188         FindExternalLexicalDecls(
7189             DC, [&](Decl::Kind K) { return K == D->getKind(); }, Decls);
7190       }
7191     }
7192   }
7193 
7194   if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D))
7195     CTSD->getSpecializedTemplate()->LoadLazySpecializations();
7196   if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D))
7197     VTSD->getSpecializedTemplate()->LoadLazySpecializations();
7198   if (auto *FD = dyn_cast<FunctionDecl>(D)) {
7199     if (auto *Template = FD->getPrimaryTemplate())
7200       Template->LoadLazySpecializations();
7201   }
7202 }
7203 
7204 CXXCtorInitializer **
7205 ASTReader::GetExternalCXXCtorInitializers(uint64_t Offset) {
7206   RecordLocation Loc = getLocalBitOffset(Offset);
7207   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7208   SavedStreamPosition SavedPosition(Cursor);
7209   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7210     Error(std::move(Err));
7211     return nullptr;
7212   }
7213   ReadingKindTracker ReadingKind(Read_Decl, *this);
7214 
7215   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7216   if (!MaybeCode) {
7217     Error(MaybeCode.takeError());
7218     return nullptr;
7219   }
7220   unsigned Code = MaybeCode.get();
7221 
7222   ASTRecordReader Record(*this, *Loc.F);
7223   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7224   if (!MaybeRecCode) {
7225     Error(MaybeRecCode.takeError());
7226     return nullptr;
7227   }
7228   if (MaybeRecCode.get() != DECL_CXX_CTOR_INITIALIZERS) {
7229     Error("malformed AST file: missing C++ ctor initializers");
7230     return nullptr;
7231   }
7232 
7233   return Record.readCXXCtorInitializers();
7234 }
7235 
7236 CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
7237   assert(ContextObj && "reading base specifiers with no AST context");
7238   ASTContext &Context = *ContextObj;
7239 
7240   RecordLocation Loc = getLocalBitOffset(Offset);
7241   BitstreamCursor &Cursor = Loc.F->DeclsCursor;
7242   SavedStreamPosition SavedPosition(Cursor);
7243   if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
7244     Error(std::move(Err));
7245     return nullptr;
7246   }
7247   ReadingKindTracker ReadingKind(Read_Decl, *this);
7248 
7249   Expected<unsigned> MaybeCode = Cursor.ReadCode();
7250   if (!MaybeCode) {
7251     Error(MaybeCode.takeError());
7252     return nullptr;
7253   }
7254   unsigned Code = MaybeCode.get();
7255 
7256   ASTRecordReader Record(*this, *Loc.F);
7257   Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
7258   if (!MaybeRecCode) {
7259     Error(MaybeCode.takeError());
7260     return nullptr;
7261   }
7262   unsigned RecCode = MaybeRecCode.get();
7263 
7264   if (RecCode != DECL_CXX_BASE_SPECIFIERS) {
7265     Error("malformed AST file: missing C++ base specifiers");
7266     return nullptr;
7267   }
7268 
7269   unsigned NumBases = Record.readInt();
7270   void *Mem = Context.Allocate(sizeof(CXXBaseSpecifier) * NumBases);
7271   CXXBaseSpecifier *Bases = new (Mem) CXXBaseSpecifier [NumBases];
7272   for (unsigned I = 0; I != NumBases; ++I)
7273     Bases[I] = Record.readCXXBaseSpecifier();
7274   return Bases;
7275 }
7276 
7277 serialization::DeclID
7278 ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
7279   if (LocalID < NUM_PREDEF_DECL_IDS)
7280     return LocalID;
7281 
7282   if (!F.ModuleOffsetMap.empty())
7283     ReadModuleOffsetMap(F);
7284 
7285   ContinuousRangeMap<uint32_t, int, 2>::iterator I
7286     = F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7287   assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
7288 
7289   return LocalID + I->second;
7290 }
7291 
7292 bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID,
7293                                    ModuleFile &M) const {
7294   // Predefined decls aren't from any module.
7295   if (ID < NUM_PREDEF_DECL_IDS)
7296     return false;
7297 
7298   return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID &&
7299          ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls;
7300 }
7301 
7302 ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) {
7303   if (!D->isFromASTFile())
7304     return nullptr;
7305   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID());
7306   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7307   return I->second;
7308 }
7309 
7310 SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) {
7311   if (ID < NUM_PREDEF_DECL_IDS)
7312     return SourceLocation();
7313 
7314   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7315 
7316   if (Index > DeclsLoaded.size()) {
7317     Error("declaration ID out-of-range for AST file");
7318     return SourceLocation();
7319   }
7320 
7321   if (Decl *D = DeclsLoaded[Index])
7322     return D->getLocation();
7323 
7324   SourceLocation Loc;
7325   DeclCursorForID(ID, Loc);
7326   return Loc;
7327 }
7328 
7329 static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) {
7330   switch (ID) {
7331   case PREDEF_DECL_NULL_ID:
7332     return nullptr;
7333 
7334   case PREDEF_DECL_TRANSLATION_UNIT_ID:
7335     return Context.getTranslationUnitDecl();
7336 
7337   case PREDEF_DECL_OBJC_ID_ID:
7338     return Context.getObjCIdDecl();
7339 
7340   case PREDEF_DECL_OBJC_SEL_ID:
7341     return Context.getObjCSelDecl();
7342 
7343   case PREDEF_DECL_OBJC_CLASS_ID:
7344     return Context.getObjCClassDecl();
7345 
7346   case PREDEF_DECL_OBJC_PROTOCOL_ID:
7347     return Context.getObjCProtocolDecl();
7348 
7349   case PREDEF_DECL_INT_128_ID:
7350     return Context.getInt128Decl();
7351 
7352   case PREDEF_DECL_UNSIGNED_INT_128_ID:
7353     return Context.getUInt128Decl();
7354 
7355   case PREDEF_DECL_OBJC_INSTANCETYPE_ID:
7356     return Context.getObjCInstanceTypeDecl();
7357 
7358   case PREDEF_DECL_BUILTIN_VA_LIST_ID:
7359     return Context.getBuiltinVaListDecl();
7360 
7361   case PREDEF_DECL_VA_LIST_TAG:
7362     return Context.getVaListTagDecl();
7363 
7364   case PREDEF_DECL_BUILTIN_MS_VA_LIST_ID:
7365     return Context.getBuiltinMSVaListDecl();
7366 
7367   case PREDEF_DECL_BUILTIN_MS_GUID_ID:
7368     return Context.getMSGuidTagDecl();
7369 
7370   case PREDEF_DECL_EXTERN_C_CONTEXT_ID:
7371     return Context.getExternCContextDecl();
7372 
7373   case PREDEF_DECL_MAKE_INTEGER_SEQ_ID:
7374     return Context.getMakeIntegerSeqDecl();
7375 
7376   case PREDEF_DECL_CF_CONSTANT_STRING_ID:
7377     return Context.getCFConstantStringDecl();
7378 
7379   case PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID:
7380     return Context.getCFConstantStringTagDecl();
7381 
7382   case PREDEF_DECL_TYPE_PACK_ELEMENT_ID:
7383     return Context.getTypePackElementDecl();
7384   }
7385   llvm_unreachable("PredefinedDeclIDs unknown enum value");
7386 }
7387 
7388 Decl *ASTReader::GetExistingDecl(DeclID ID) {
7389   assert(ContextObj && "reading decl with no AST context");
7390   if (ID < NUM_PREDEF_DECL_IDS) {
7391     Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID);
7392     if (D) {
7393       // Track that we have merged the declaration with ID \p ID into the
7394       // pre-existing predefined declaration \p D.
7395       auto &Merged = KeyDecls[D->getCanonicalDecl()];
7396       if (Merged.empty())
7397         Merged.push_back(ID);
7398     }
7399     return D;
7400   }
7401 
7402   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7403 
7404   if (Index >= DeclsLoaded.size()) {
7405     assert(0 && "declaration ID out-of-range for AST file");
7406     Error("declaration ID out-of-range for AST file");
7407     return nullptr;
7408   }
7409 
7410   return DeclsLoaded[Index];
7411 }
7412 
7413 Decl *ASTReader::GetDecl(DeclID ID) {
7414   if (ID < NUM_PREDEF_DECL_IDS)
7415     return GetExistingDecl(ID);
7416 
7417   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
7418 
7419   if (Index >= DeclsLoaded.size()) {
7420     assert(0 && "declaration ID out-of-range for AST file");
7421     Error("declaration ID out-of-range for AST file");
7422     return nullptr;
7423   }
7424 
7425   if (!DeclsLoaded[Index]) {
7426     ReadDeclRecord(ID);
7427     if (DeserializationListener)
7428       DeserializationListener->DeclRead(ID, DeclsLoaded[Index]);
7429   }
7430 
7431   return DeclsLoaded[Index];
7432 }
7433 
7434 DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
7435                                                   DeclID GlobalID) {
7436   if (GlobalID < NUM_PREDEF_DECL_IDS)
7437     return GlobalID;
7438 
7439   GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID);
7440   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
7441   ModuleFile *Owner = I->second;
7442 
7443   llvm::DenseMap<ModuleFile *, serialization::DeclID>::iterator Pos
7444     = M.GlobalToLocalDeclIDs.find(Owner);
7445   if (Pos == M.GlobalToLocalDeclIDs.end())
7446     return 0;
7447 
7448   return GlobalID - Owner->BaseDeclID + Pos->second;
7449 }
7450 
7451 serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F,
7452                                             const RecordData &Record,
7453                                             unsigned &Idx) {
7454   if (Idx >= Record.size()) {
7455     Error("Corrupted AST file");
7456     return 0;
7457   }
7458 
7459   return getGlobalDeclID(F, Record[Idx++]);
7460 }
7461 
7462 /// Resolve the offset of a statement into a statement.
7463 ///
7464 /// This operation will read a new statement from the external
7465 /// source each time it is called, and is meant to be used via a
7466 /// LazyOffsetPtr (which is used by Decls for the body of functions, etc).
7467 Stmt *ASTReader::GetExternalDeclStmt(uint64_t Offset) {
7468   // Switch case IDs are per Decl.
7469   ClearSwitchCaseIDs();
7470 
7471   // Offset here is a global offset across the entire chain.
7472   RecordLocation Loc = getLocalBitOffset(Offset);
7473   if (llvm::Error Err = Loc.F->DeclsCursor.JumpToBit(Loc.Offset)) {
7474     Error(std::move(Err));
7475     return nullptr;
7476   }
7477   assert(NumCurrentElementsDeserializing == 0 &&
7478          "should not be called while already deserializing");
7479   Deserializing D(this);
7480   return ReadStmtFromStream(*Loc.F);
7481 }
7482 
7483 void ASTReader::FindExternalLexicalDecls(
7484     const DeclContext *DC, llvm::function_ref<bool(Decl::Kind)> IsKindWeWant,
7485     SmallVectorImpl<Decl *> &Decls) {
7486   bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
7487 
7488   auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7489     assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7490     for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7491       auto K = (Decl::Kind)+LexicalDecls[I];
7492       if (!IsKindWeWant(K))
7493         continue;
7494 
7495       auto ID = (serialization::DeclID)+LexicalDecls[I + 1];
7496 
7497       // Don't add predefined declarations to the lexical context more
7498       // than once.
7499       if (ID < NUM_PREDEF_DECL_IDS) {
7500         if (PredefsVisited[ID])
7501           continue;
7502 
7503         PredefsVisited[ID] = true;
7504       }
7505 
7506       if (Decl *D = GetLocalDecl(*M, ID)) {
7507         assert(D->getKind() == K && "wrong kind for lexical decl");
7508         if (!DC->isDeclInLexicalTraversal(D))
7509           Decls.push_back(D);
7510       }
7511     }
7512   };
7513 
7514   if (isa<TranslationUnitDecl>(DC)) {
7515     for (auto Lexical : TULexicalDecls)
7516       Visit(Lexical.first, Lexical.second);
7517   } else {
7518     auto I = LexicalDecls.find(DC);
7519     if (I != LexicalDecls.end())
7520       Visit(I->second.first, I->second.second);
7521   }
7522 
7523   ++NumLexicalDeclContextsRead;
7524 }
7525 
7526 namespace {
7527 
7528 class DeclIDComp {
7529   ASTReader &Reader;
7530   ModuleFile &Mod;
7531 
7532 public:
7533   DeclIDComp(ASTReader &Reader, ModuleFile &M) : Reader(Reader), Mod(M) {}
7534 
7535   bool operator()(LocalDeclID L, LocalDeclID R) const {
7536     SourceLocation LHS = getLocation(L);
7537     SourceLocation RHS = getLocation(R);
7538     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7539   }
7540 
7541   bool operator()(SourceLocation LHS, LocalDeclID R) const {
7542     SourceLocation RHS = getLocation(R);
7543     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7544   }
7545 
7546   bool operator()(LocalDeclID L, SourceLocation RHS) const {
7547     SourceLocation LHS = getLocation(L);
7548     return Reader.getSourceManager().isBeforeInTranslationUnit(LHS, RHS);
7549   }
7550 
7551   SourceLocation getLocation(LocalDeclID ID) const {
7552     return Reader.getSourceManager().getFileLoc(
7553             Reader.getSourceLocationForDeclID(Reader.getGlobalDeclID(Mod, ID)));
7554   }
7555 };
7556 
7557 } // namespace
7558 
7559 void ASTReader::FindFileRegionDecls(FileID File,
7560                                     unsigned Offset, unsigned Length,
7561                                     SmallVectorImpl<Decl *> &Decls) {
7562   SourceManager &SM = getSourceManager();
7563 
7564   llvm::DenseMap<FileID, FileDeclsInfo>::iterator I = FileDeclIDs.find(File);
7565   if (I == FileDeclIDs.end())
7566     return;
7567 
7568   FileDeclsInfo &DInfo = I->second;
7569   if (DInfo.Decls.empty())
7570     return;
7571 
7572   SourceLocation
7573     BeginLoc = SM.getLocForStartOfFile(File).getLocWithOffset(Offset);
7574   SourceLocation EndLoc = BeginLoc.getLocWithOffset(Length);
7575 
7576   DeclIDComp DIDComp(*this, *DInfo.Mod);
7577   ArrayRef<serialization::LocalDeclID>::iterator BeginIt =
7578       llvm::lower_bound(DInfo.Decls, BeginLoc, DIDComp);
7579   if (BeginIt != DInfo.Decls.begin())
7580     --BeginIt;
7581 
7582   // If we are pointing at a top-level decl inside an objc container, we need
7583   // to backtrack until we find it otherwise we will fail to report that the
7584   // region overlaps with an objc container.
7585   while (BeginIt != DInfo.Decls.begin() &&
7586          GetDecl(getGlobalDeclID(*DInfo.Mod, *BeginIt))
7587              ->isTopLevelDeclInObjCContainer())
7588     --BeginIt;
7589 
7590   ArrayRef<serialization::LocalDeclID>::iterator EndIt =
7591       llvm::upper_bound(DInfo.Decls, EndLoc, DIDComp);
7592   if (EndIt != DInfo.Decls.end())
7593     ++EndIt;
7594 
7595   for (ArrayRef<serialization::LocalDeclID>::iterator
7596          DIt = BeginIt; DIt != EndIt; ++DIt)
7597     Decls.push_back(GetDecl(getGlobalDeclID(*DInfo.Mod, *DIt)));
7598 }
7599 
7600 bool
7601 ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
7602                                           DeclarationName Name) {
7603   assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
7604          "DeclContext has no visible decls in storage");
7605   if (!Name)
7606     return false;
7607 
7608   auto It = Lookups.find(DC);
7609   if (It == Lookups.end())
7610     return false;
7611 
7612   Deserializing LookupResults(this);
7613 
7614   // Load the list of declarations.
7615   SmallVector<NamedDecl *, 64> Decls;
7616   for (DeclID ID : It->second.Table.find(Name)) {
7617     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7618     if (ND->getDeclName() == Name)
7619       Decls.push_back(ND);
7620   }
7621 
7622   ++NumVisibleDeclContextsRead;
7623   SetExternalVisibleDeclsForName(DC, Name, Decls);
7624   return !Decls.empty();
7625 }
7626 
7627 void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) {
7628   if (!DC->hasExternalVisibleStorage())
7629     return;
7630 
7631   auto It = Lookups.find(DC);
7632   assert(It != Lookups.end() &&
7633          "have external visible storage but no lookup tables");
7634 
7635   DeclsMap Decls;
7636 
7637   for (DeclID ID : It->second.Table.findAll()) {
7638     NamedDecl *ND = cast<NamedDecl>(GetDecl(ID));
7639     Decls[ND->getDeclName()].push_back(ND);
7640   }
7641 
7642   ++NumVisibleDeclContextsRead;
7643 
7644   for (DeclsMap::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
7645     SetExternalVisibleDeclsForName(DC, I->first, I->second);
7646   }
7647   const_cast<DeclContext *>(DC)->setHasExternalVisibleStorage(false);
7648 }
7649 
7650 const serialization::reader::DeclContextLookupTable *
7651 ASTReader::getLoadedLookupTables(DeclContext *Primary) const {
7652   auto I = Lookups.find(Primary);
7653   return I == Lookups.end() ? nullptr : &I->second;
7654 }
7655 
7656 /// Under non-PCH compilation the consumer receives the objc methods
7657 /// before receiving the implementation, and codegen depends on this.
7658 /// We simulate this by deserializing and passing to consumer the methods of the
7659 /// implementation before passing the deserialized implementation decl.
7660 static void PassObjCImplDeclToConsumer(ObjCImplDecl *ImplD,
7661                                        ASTConsumer *Consumer) {
7662   assert(ImplD && Consumer);
7663 
7664   for (auto *I : ImplD->methods())
7665     Consumer->HandleInterestingDecl(DeclGroupRef(I));
7666 
7667   Consumer->HandleInterestingDecl(DeclGroupRef(ImplD));
7668 }
7669 
7670 void ASTReader::PassInterestingDeclToConsumer(Decl *D) {
7671   if (ObjCImplDecl *ImplD = dyn_cast<ObjCImplDecl>(D))
7672     PassObjCImplDeclToConsumer(ImplD, Consumer);
7673   else
7674     Consumer->HandleInterestingDecl(DeclGroupRef(D));
7675 }
7676 
7677 void ASTReader::StartTranslationUnit(ASTConsumer *Consumer) {
7678   this->Consumer = Consumer;
7679 
7680   if (Consumer)
7681     PassInterestingDeclsToConsumer();
7682 
7683   if (DeserializationListener)
7684     DeserializationListener->ReaderInitialized(this);
7685 }
7686 
7687 void ASTReader::PrintStats() {
7688   std::fprintf(stderr, "*** AST File Statistics:\n");
7689 
7690   unsigned NumTypesLoaded
7691     = TypesLoaded.size() - std::count(TypesLoaded.begin(), TypesLoaded.end(),
7692                                       QualType());
7693   unsigned NumDeclsLoaded
7694     = DeclsLoaded.size() - std::count(DeclsLoaded.begin(), DeclsLoaded.end(),
7695                                       (Decl *)nullptr);
7696   unsigned NumIdentifiersLoaded
7697     = IdentifiersLoaded.size() - std::count(IdentifiersLoaded.begin(),
7698                                             IdentifiersLoaded.end(),
7699                                             (IdentifierInfo *)nullptr);
7700   unsigned NumMacrosLoaded
7701     = MacrosLoaded.size() - std::count(MacrosLoaded.begin(),
7702                                        MacrosLoaded.end(),
7703                                        (MacroInfo *)nullptr);
7704   unsigned NumSelectorsLoaded
7705     = SelectorsLoaded.size() - std::count(SelectorsLoaded.begin(),
7706                                           SelectorsLoaded.end(),
7707                                           Selector());
7708 
7709   if (unsigned TotalNumSLocEntries = getTotalNumSLocs())
7710     std::fprintf(stderr, "  %u/%u source location entries read (%f%%)\n",
7711                  NumSLocEntriesRead, TotalNumSLocEntries,
7712                  ((float)NumSLocEntriesRead/TotalNumSLocEntries * 100));
7713   if (!TypesLoaded.empty())
7714     std::fprintf(stderr, "  %u/%u types read (%f%%)\n",
7715                  NumTypesLoaded, (unsigned)TypesLoaded.size(),
7716                  ((float)NumTypesLoaded/TypesLoaded.size() * 100));
7717   if (!DeclsLoaded.empty())
7718     std::fprintf(stderr, "  %u/%u declarations read (%f%%)\n",
7719                  NumDeclsLoaded, (unsigned)DeclsLoaded.size(),
7720                  ((float)NumDeclsLoaded/DeclsLoaded.size() * 100));
7721   if (!IdentifiersLoaded.empty())
7722     std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
7723                  NumIdentifiersLoaded, (unsigned)IdentifiersLoaded.size(),
7724                  ((float)NumIdentifiersLoaded/IdentifiersLoaded.size() * 100));
7725   if (!MacrosLoaded.empty())
7726     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7727                  NumMacrosLoaded, (unsigned)MacrosLoaded.size(),
7728                  ((float)NumMacrosLoaded/MacrosLoaded.size() * 100));
7729   if (!SelectorsLoaded.empty())
7730     std::fprintf(stderr, "  %u/%u selectors read (%f%%)\n",
7731                  NumSelectorsLoaded, (unsigned)SelectorsLoaded.size(),
7732                  ((float)NumSelectorsLoaded/SelectorsLoaded.size() * 100));
7733   if (TotalNumStatements)
7734     std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
7735                  NumStatementsRead, TotalNumStatements,
7736                  ((float)NumStatementsRead/TotalNumStatements * 100));
7737   if (TotalNumMacros)
7738     std::fprintf(stderr, "  %u/%u macros read (%f%%)\n",
7739                  NumMacrosRead, TotalNumMacros,
7740                  ((float)NumMacrosRead/TotalNumMacros * 100));
7741   if (TotalLexicalDeclContexts)
7742     std::fprintf(stderr, "  %u/%u lexical declcontexts read (%f%%)\n",
7743                  NumLexicalDeclContextsRead, TotalLexicalDeclContexts,
7744                  ((float)NumLexicalDeclContextsRead/TotalLexicalDeclContexts
7745                   * 100));
7746   if (TotalVisibleDeclContexts)
7747     std::fprintf(stderr, "  %u/%u visible declcontexts read (%f%%)\n",
7748                  NumVisibleDeclContextsRead, TotalVisibleDeclContexts,
7749                  ((float)NumVisibleDeclContextsRead/TotalVisibleDeclContexts
7750                   * 100));
7751   if (TotalNumMethodPoolEntries)
7752     std::fprintf(stderr, "  %u/%u method pool entries read (%f%%)\n",
7753                  NumMethodPoolEntriesRead, TotalNumMethodPoolEntries,
7754                  ((float)NumMethodPoolEntriesRead/TotalNumMethodPoolEntries
7755                   * 100));
7756   if (NumMethodPoolLookups)
7757     std::fprintf(stderr, "  %u/%u method pool lookups succeeded (%f%%)\n",
7758                  NumMethodPoolHits, NumMethodPoolLookups,
7759                  ((float)NumMethodPoolHits/NumMethodPoolLookups * 100.0));
7760   if (NumMethodPoolTableLookups)
7761     std::fprintf(stderr, "  %u/%u method pool table lookups succeeded (%f%%)\n",
7762                  NumMethodPoolTableHits, NumMethodPoolTableLookups,
7763                  ((float)NumMethodPoolTableHits/NumMethodPoolTableLookups
7764                   * 100.0));
7765   if (NumIdentifierLookupHits)
7766     std::fprintf(stderr,
7767                  "  %u / %u identifier table lookups succeeded (%f%%)\n",
7768                  NumIdentifierLookupHits, NumIdentifierLookups,
7769                  (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
7770 
7771   if (GlobalIndex) {
7772     std::fprintf(stderr, "\n");
7773     GlobalIndex->printStats();
7774   }
7775 
7776   std::fprintf(stderr, "\n");
7777   dump();
7778   std::fprintf(stderr, "\n");
7779 }
7780 
7781 template<typename Key, typename ModuleFile, unsigned InitialCapacity>
7782 LLVM_DUMP_METHOD static void
7783 dumpModuleIDMap(StringRef Name,
7784                 const ContinuousRangeMap<Key, ModuleFile *,
7785                                          InitialCapacity> &Map) {
7786   if (Map.begin() == Map.end())
7787     return;
7788 
7789   using MapType = ContinuousRangeMap<Key, ModuleFile *, InitialCapacity>;
7790 
7791   llvm::errs() << Name << ":\n";
7792   for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end();
7793        I != IEnd; ++I) {
7794     llvm::errs() << "  " << I->first << " -> " << I->second->FileName
7795       << "\n";
7796   }
7797 }
7798 
7799 LLVM_DUMP_METHOD void ASTReader::dump() {
7800   llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
7801   dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
7802   dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
7803   dumpModuleIDMap("Global type map", GlobalTypeMap);
7804   dumpModuleIDMap("Global declaration map", GlobalDeclMap);
7805   dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
7806   dumpModuleIDMap("Global macro map", GlobalMacroMap);
7807   dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
7808   dumpModuleIDMap("Global selector map", GlobalSelectorMap);
7809   dumpModuleIDMap("Global preprocessed entity map",
7810                   GlobalPreprocessedEntityMap);
7811 
7812   llvm::errs() << "\n*** PCH/Modules Loaded:";
7813   for (ModuleFile &M : ModuleMgr)
7814     M.dump();
7815 }
7816 
7817 /// Return the amount of memory used by memory buffers, breaking down
7818 /// by heap-backed versus mmap'ed memory.
7819 void ASTReader::getMemoryBufferSizes(MemoryBufferSizes &sizes) const {
7820   for (ModuleFile &I : ModuleMgr) {
7821     if (llvm::MemoryBuffer *buf = I.Buffer) {
7822       size_t bytes = buf->getBufferSize();
7823       switch (buf->getBufferKind()) {
7824         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
7825           sizes.malloc_bytes += bytes;
7826           break;
7827         case llvm::MemoryBuffer::MemoryBuffer_MMap:
7828           sizes.mmap_bytes += bytes;
7829           break;
7830       }
7831     }
7832   }
7833 }
7834 
7835 void ASTReader::InitializeSema(Sema &S) {
7836   SemaObj = &S;
7837   S.addExternalSource(this);
7838 
7839   // Makes sure any declarations that were deserialized "too early"
7840   // still get added to the identifier's declaration chains.
7841   for (uint64_t ID : PreloadedDeclIDs) {
7842     NamedDecl *D = cast<NamedDecl>(GetDecl(ID));
7843     pushExternalDeclIntoScope(D, D->getDeclName());
7844   }
7845   PreloadedDeclIDs.clear();
7846 
7847   // FIXME: What happens if these are changed by a module import?
7848   if (!FPPragmaOptions.empty()) {
7849     assert(FPPragmaOptions.size() == 1 && "Wrong number of FP_PRAGMA_OPTIONS");
7850     FPOptionsOverride NewOverrides =
7851         FPOptionsOverride::getFromOpaqueInt(FPPragmaOptions[0]);
7852     SemaObj->CurFPFeatures =
7853         NewOverrides.applyOverrides(SemaObj->getLangOpts());
7854   }
7855 
7856   SemaObj->OpenCLFeatures.copy(OpenCLExtensions);
7857   SemaObj->OpenCLTypeExtMap = OpenCLTypeExtMap;
7858   SemaObj->OpenCLDeclExtMap = OpenCLDeclExtMap;
7859 
7860   UpdateSema();
7861 }
7862 
7863 void ASTReader::UpdateSema() {
7864   assert(SemaObj && "no Sema to update");
7865 
7866   // Load the offsets of the declarations that Sema references.
7867   // They will be lazily deserialized when needed.
7868   if (!SemaDeclRefs.empty()) {
7869     assert(SemaDeclRefs.size() % 3 == 0);
7870     for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) {
7871       if (!SemaObj->StdNamespace)
7872         SemaObj->StdNamespace = SemaDeclRefs[I];
7873       if (!SemaObj->StdBadAlloc)
7874         SemaObj->StdBadAlloc = SemaDeclRefs[I+1];
7875       if (!SemaObj->StdAlignValT)
7876         SemaObj->StdAlignValT = SemaDeclRefs[I+2];
7877     }
7878     SemaDeclRefs.clear();
7879   }
7880 
7881   // Update the state of pragmas. Use the same API as if we had encountered the
7882   // pragma in the source.
7883   if(OptimizeOffPragmaLocation.isValid())
7884     SemaObj->ActOnPragmaOptimize(/* On = */ false, OptimizeOffPragmaLocation);
7885   if (PragmaMSStructState != -1)
7886     SemaObj->ActOnPragmaMSStruct((PragmaMSStructKind)PragmaMSStructState);
7887   if (PointersToMembersPragmaLocation.isValid()) {
7888     SemaObj->ActOnPragmaMSPointersToMembers(
7889         (LangOptions::PragmaMSPointersToMembersKind)
7890             PragmaMSPointersToMembersState,
7891         PointersToMembersPragmaLocation);
7892   }
7893   SemaObj->ForceCUDAHostDeviceDepth = ForceCUDAHostDeviceDepth;
7894 
7895   if (PragmaPackCurrentValue) {
7896     // The bottom of the stack might have a default value. It must be adjusted
7897     // to the current value to ensure that the packing state is preserved after
7898     // popping entries that were included/imported from a PCH/module.
7899     bool DropFirst = false;
7900     if (!PragmaPackStack.empty() &&
7901         PragmaPackStack.front().Location.isInvalid()) {
7902       assert(PragmaPackStack.front().Value == SemaObj->PackStack.DefaultValue &&
7903              "Expected a default alignment value");
7904       SemaObj->PackStack.Stack.emplace_back(
7905           PragmaPackStack.front().SlotLabel, SemaObj->PackStack.CurrentValue,
7906           SemaObj->PackStack.CurrentPragmaLocation,
7907           PragmaPackStack.front().PushLocation);
7908       DropFirst = true;
7909     }
7910     for (const auto &Entry :
7911          llvm::makeArrayRef(PragmaPackStack).drop_front(DropFirst ? 1 : 0))
7912       SemaObj->PackStack.Stack.emplace_back(Entry.SlotLabel, Entry.Value,
7913                                             Entry.Location, Entry.PushLocation);
7914     if (PragmaPackCurrentLocation.isInvalid()) {
7915       assert(*PragmaPackCurrentValue == SemaObj->PackStack.DefaultValue &&
7916              "Expected a default alignment value");
7917       // Keep the current values.
7918     } else {
7919       SemaObj->PackStack.CurrentValue = *PragmaPackCurrentValue;
7920       SemaObj->PackStack.CurrentPragmaLocation = PragmaPackCurrentLocation;
7921     }
7922   }
7923   if (FpPragmaCurrentValue) {
7924     // The bottom of the stack might have a default value. It must be adjusted
7925     // to the current value to ensure that fp-pragma state is preserved after
7926     // popping entries that were included/imported from a PCH/module.
7927     bool DropFirst = false;
7928     if (!FpPragmaStack.empty() && FpPragmaStack.front().Location.isInvalid()) {
7929       assert(FpPragmaStack.front().Value ==
7930                  SemaObj->FpPragmaStack.DefaultValue &&
7931              "Expected a default pragma float_control value");
7932       SemaObj->FpPragmaStack.Stack.emplace_back(
7933           FpPragmaStack.front().SlotLabel, SemaObj->FpPragmaStack.CurrentValue,
7934           SemaObj->FpPragmaStack.CurrentPragmaLocation,
7935           FpPragmaStack.front().PushLocation);
7936       DropFirst = true;
7937     }
7938     for (const auto &Entry :
7939          llvm::makeArrayRef(FpPragmaStack).drop_front(DropFirst ? 1 : 0))
7940       SemaObj->FpPragmaStack.Stack.emplace_back(
7941           Entry.SlotLabel, Entry.Value, Entry.Location, Entry.PushLocation);
7942     if (FpPragmaCurrentLocation.isInvalid()) {
7943       assert(*FpPragmaCurrentValue == SemaObj->FpPragmaStack.DefaultValue &&
7944              "Expected a default pragma float_control value");
7945       // Keep the current values.
7946     } else {
7947       SemaObj->FpPragmaStack.CurrentValue = *FpPragmaCurrentValue;
7948       SemaObj->FpPragmaStack.CurrentPragmaLocation = FpPragmaCurrentLocation;
7949     }
7950   }
7951 
7952   // For non-modular AST files, restore visiblity of modules.
7953   for (auto &Import : ImportedModules) {
7954     if (Import.ImportLoc.isInvalid())
7955       continue;
7956     if (Module *Imported = getSubmodule(Import.ID)) {
7957       SemaObj->makeModuleVisible(Imported, Import.ImportLoc);
7958     }
7959   }
7960 }
7961 
7962 IdentifierInfo *ASTReader::get(StringRef Name) {
7963   // Note that we are loading an identifier.
7964   Deserializing AnIdentifier(this);
7965 
7966   IdentifierLookupVisitor Visitor(Name, /*PriorGeneration=*/0,
7967                                   NumIdentifierLookups,
7968                                   NumIdentifierLookupHits);
7969 
7970   // We don't need to do identifier table lookups in C++ modules (we preload
7971   // all interesting declarations, and don't need to use the scope for name
7972   // lookups). Perform the lookup in PCH files, though, since we don't build
7973   // a complete initial identifier table if we're carrying on from a PCH.
7974   if (PP.getLangOpts().CPlusPlus) {
7975     for (auto F : ModuleMgr.pch_modules())
7976       if (Visitor(*F))
7977         break;
7978   } else {
7979     // If there is a global index, look there first to determine which modules
7980     // provably do not have any results for this identifier.
7981     GlobalModuleIndex::HitSet Hits;
7982     GlobalModuleIndex::HitSet *HitsPtr = nullptr;
7983     if (!loadGlobalIndex()) {
7984       if (GlobalIndex->lookupIdentifier(Name, Hits)) {
7985         HitsPtr = &Hits;
7986       }
7987     }
7988 
7989     ModuleMgr.visit(Visitor, HitsPtr);
7990   }
7991 
7992   IdentifierInfo *II = Visitor.getIdentifierInfo();
7993   markIdentifierUpToDate(II);
7994   return II;
7995 }
7996 
7997 namespace clang {
7998 
7999   /// An identifier-lookup iterator that enumerates all of the
8000   /// identifiers stored within a set of AST files.
8001   class ASTIdentifierIterator : public IdentifierIterator {
8002     /// The AST reader whose identifiers are being enumerated.
8003     const ASTReader &Reader;
8004 
8005     /// The current index into the chain of AST files stored in
8006     /// the AST reader.
8007     unsigned Index;
8008 
8009     /// The current position within the identifier lookup table
8010     /// of the current AST file.
8011     ASTIdentifierLookupTable::key_iterator Current;
8012 
8013     /// The end position within the identifier lookup table of
8014     /// the current AST file.
8015     ASTIdentifierLookupTable::key_iterator End;
8016 
8017     /// Whether to skip any modules in the ASTReader.
8018     bool SkipModules;
8019 
8020   public:
8021     explicit ASTIdentifierIterator(const ASTReader &Reader,
8022                                    bool SkipModules = false);
8023 
8024     StringRef Next() override;
8025   };
8026 
8027 } // namespace clang
8028 
8029 ASTIdentifierIterator::ASTIdentifierIterator(const ASTReader &Reader,
8030                                              bool SkipModules)
8031     : Reader(Reader), Index(Reader.ModuleMgr.size()), SkipModules(SkipModules) {
8032 }
8033 
8034 StringRef ASTIdentifierIterator::Next() {
8035   while (Current == End) {
8036     // If we have exhausted all of our AST files, we're done.
8037     if (Index == 0)
8038       return StringRef();
8039 
8040     --Index;
8041     ModuleFile &F = Reader.ModuleMgr[Index];
8042     if (SkipModules && F.isModule())
8043       continue;
8044 
8045     ASTIdentifierLookupTable *IdTable =
8046         (ASTIdentifierLookupTable *)F.IdentifierLookupTable;
8047     Current = IdTable->key_begin();
8048     End = IdTable->key_end();
8049   }
8050 
8051   // We have any identifiers remaining in the current AST file; return
8052   // the next one.
8053   StringRef Result = *Current;
8054   ++Current;
8055   return Result;
8056 }
8057 
8058 namespace {
8059 
8060 /// A utility for appending two IdentifierIterators.
8061 class ChainedIdentifierIterator : public IdentifierIterator {
8062   std::unique_ptr<IdentifierIterator> Current;
8063   std::unique_ptr<IdentifierIterator> Queued;
8064 
8065 public:
8066   ChainedIdentifierIterator(std::unique_ptr<IdentifierIterator> First,
8067                             std::unique_ptr<IdentifierIterator> Second)
8068       : Current(std::move(First)), Queued(std::move(Second)) {}
8069 
8070   StringRef Next() override {
8071     if (!Current)
8072       return StringRef();
8073 
8074     StringRef result = Current->Next();
8075     if (!result.empty())
8076       return result;
8077 
8078     // Try the queued iterator, which may itself be empty.
8079     Current.reset();
8080     std::swap(Current, Queued);
8081     return Next();
8082   }
8083 };
8084 
8085 } // namespace
8086 
8087 IdentifierIterator *ASTReader::getIdentifiers() {
8088   if (!loadGlobalIndex()) {
8089     std::unique_ptr<IdentifierIterator> ReaderIter(
8090         new ASTIdentifierIterator(*this, /*SkipModules=*/true));
8091     std::unique_ptr<IdentifierIterator> ModulesIter(
8092         GlobalIndex->createIdentifierIterator());
8093     return new ChainedIdentifierIterator(std::move(ReaderIter),
8094                                          std::move(ModulesIter));
8095   }
8096 
8097   return new ASTIdentifierIterator(*this);
8098 }
8099 
8100 namespace clang {
8101 namespace serialization {
8102 
8103   class ReadMethodPoolVisitor {
8104     ASTReader &Reader;
8105     Selector Sel;
8106     unsigned PriorGeneration;
8107     unsigned InstanceBits = 0;
8108     unsigned FactoryBits = 0;
8109     bool InstanceHasMoreThanOneDecl = false;
8110     bool FactoryHasMoreThanOneDecl = false;
8111     SmallVector<ObjCMethodDecl *, 4> InstanceMethods;
8112     SmallVector<ObjCMethodDecl *, 4> FactoryMethods;
8113 
8114   public:
8115     ReadMethodPoolVisitor(ASTReader &Reader, Selector Sel,
8116                           unsigned PriorGeneration)
8117         : Reader(Reader), Sel(Sel), PriorGeneration(PriorGeneration) {}
8118 
8119     bool operator()(ModuleFile &M) {
8120       if (!M.SelectorLookupTable)
8121         return false;
8122 
8123       // If we've already searched this module file, skip it now.
8124       if (M.Generation <= PriorGeneration)
8125         return true;
8126 
8127       ++Reader.NumMethodPoolTableLookups;
8128       ASTSelectorLookupTable *PoolTable
8129         = (ASTSelectorLookupTable*)M.SelectorLookupTable;
8130       ASTSelectorLookupTable::iterator Pos = PoolTable->find(Sel);
8131       if (Pos == PoolTable->end())
8132         return false;
8133 
8134       ++Reader.NumMethodPoolTableHits;
8135       ++Reader.NumSelectorsRead;
8136       // FIXME: Not quite happy with the statistics here. We probably should
8137       // disable this tracking when called via LoadSelector.
8138       // Also, should entries without methods count as misses?
8139       ++Reader.NumMethodPoolEntriesRead;
8140       ASTSelectorLookupTrait::data_type Data = *Pos;
8141       if (Reader.DeserializationListener)
8142         Reader.DeserializationListener->SelectorRead(Data.ID, Sel);
8143 
8144       InstanceMethods.append(Data.Instance.begin(), Data.Instance.end());
8145       FactoryMethods.append(Data.Factory.begin(), Data.Factory.end());
8146       InstanceBits = Data.InstanceBits;
8147       FactoryBits = Data.FactoryBits;
8148       InstanceHasMoreThanOneDecl = Data.InstanceHasMoreThanOneDecl;
8149       FactoryHasMoreThanOneDecl = Data.FactoryHasMoreThanOneDecl;
8150       return true;
8151     }
8152 
8153     /// Retrieve the instance methods found by this visitor.
8154     ArrayRef<ObjCMethodDecl *> getInstanceMethods() const {
8155       return InstanceMethods;
8156     }
8157 
8158     /// Retrieve the instance methods found by this visitor.
8159     ArrayRef<ObjCMethodDecl *> getFactoryMethods() const {
8160       return FactoryMethods;
8161     }
8162 
8163     unsigned getInstanceBits() const { return InstanceBits; }
8164     unsigned getFactoryBits() const { return FactoryBits; }
8165 
8166     bool instanceHasMoreThanOneDecl() const {
8167       return InstanceHasMoreThanOneDecl;
8168     }
8169 
8170     bool factoryHasMoreThanOneDecl() const { return FactoryHasMoreThanOneDecl; }
8171   };
8172 
8173 } // namespace serialization
8174 } // namespace clang
8175 
8176 /// Add the given set of methods to the method list.
8177 static void addMethodsToPool(Sema &S, ArrayRef<ObjCMethodDecl *> Methods,
8178                              ObjCMethodList &List) {
8179   for (unsigned I = 0, N = Methods.size(); I != N; ++I) {
8180     S.addMethodToGlobalList(&List, Methods[I]);
8181   }
8182 }
8183 
8184 void ASTReader::ReadMethodPool(Selector Sel) {
8185   // Get the selector generation and update it to the current generation.
8186   unsigned &Generation = SelectorGeneration[Sel];
8187   unsigned PriorGeneration = Generation;
8188   Generation = getGeneration();
8189   SelectorOutOfDate[Sel] = false;
8190 
8191   // Search for methods defined with this selector.
8192   ++NumMethodPoolLookups;
8193   ReadMethodPoolVisitor Visitor(*this, Sel, PriorGeneration);
8194   ModuleMgr.visit(Visitor);
8195 
8196   if (Visitor.getInstanceMethods().empty() &&
8197       Visitor.getFactoryMethods().empty())
8198     return;
8199 
8200   ++NumMethodPoolHits;
8201 
8202   if (!getSema())
8203     return;
8204 
8205   Sema &S = *getSema();
8206   Sema::GlobalMethodPool::iterator Pos
8207     = S.MethodPool.insert(std::make_pair(Sel, Sema::GlobalMethods())).first;
8208 
8209   Pos->second.first.setBits(Visitor.getInstanceBits());
8210   Pos->second.first.setHasMoreThanOneDecl(Visitor.instanceHasMoreThanOneDecl());
8211   Pos->second.second.setBits(Visitor.getFactoryBits());
8212   Pos->second.second.setHasMoreThanOneDecl(Visitor.factoryHasMoreThanOneDecl());
8213 
8214   // Add methods to the global pool *after* setting hasMoreThanOneDecl, since
8215   // when building a module we keep every method individually and may need to
8216   // update hasMoreThanOneDecl as we add the methods.
8217   addMethodsToPool(S, Visitor.getInstanceMethods(), Pos->second.first);
8218   addMethodsToPool(S, Visitor.getFactoryMethods(), Pos->second.second);
8219 }
8220 
8221 void ASTReader::updateOutOfDateSelector(Selector Sel) {
8222   if (SelectorOutOfDate[Sel])
8223     ReadMethodPool(Sel);
8224 }
8225 
8226 void ASTReader::ReadKnownNamespaces(
8227                           SmallVectorImpl<NamespaceDecl *> &Namespaces) {
8228   Namespaces.clear();
8229 
8230   for (unsigned I = 0, N = KnownNamespaces.size(); I != N; ++I) {
8231     if (NamespaceDecl *Namespace
8232                 = dyn_cast_or_null<NamespaceDecl>(GetDecl(KnownNamespaces[I])))
8233       Namespaces.push_back(Namespace);
8234   }
8235 }
8236 
8237 void ASTReader::ReadUndefinedButUsed(
8238     llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) {
8239   for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) {
8240     NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++]));
8241     SourceLocation Loc =
8242         SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]);
8243     Undefined.insert(std::make_pair(D, Loc));
8244   }
8245 }
8246 
8247 void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector<
8248     FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> &
8249                                                      Exprs) {
8250   for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) {
8251     FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++]));
8252     uint64_t Count = DelayedDeleteExprs[Idx++];
8253     for (uint64_t C = 0; C < Count; ++C) {
8254       SourceLocation DeleteLoc =
8255           SourceLocation::getFromRawEncoding(DelayedDeleteExprs[Idx++]);
8256       const bool IsArrayForm = DelayedDeleteExprs[Idx++];
8257       Exprs[FD].push_back(std::make_pair(DeleteLoc, IsArrayForm));
8258     }
8259   }
8260 }
8261 
8262 void ASTReader::ReadTentativeDefinitions(
8263                   SmallVectorImpl<VarDecl *> &TentativeDefs) {
8264   for (unsigned I = 0, N = TentativeDefinitions.size(); I != N; ++I) {
8265     VarDecl *Var = dyn_cast_or_null<VarDecl>(GetDecl(TentativeDefinitions[I]));
8266     if (Var)
8267       TentativeDefs.push_back(Var);
8268   }
8269   TentativeDefinitions.clear();
8270 }
8271 
8272 void ASTReader::ReadUnusedFileScopedDecls(
8273                                SmallVectorImpl<const DeclaratorDecl *> &Decls) {
8274   for (unsigned I = 0, N = UnusedFileScopedDecls.size(); I != N; ++I) {
8275     DeclaratorDecl *D
8276       = dyn_cast_or_null<DeclaratorDecl>(GetDecl(UnusedFileScopedDecls[I]));
8277     if (D)
8278       Decls.push_back(D);
8279   }
8280   UnusedFileScopedDecls.clear();
8281 }
8282 
8283 void ASTReader::ReadDelegatingConstructors(
8284                                  SmallVectorImpl<CXXConstructorDecl *> &Decls) {
8285   for (unsigned I = 0, N = DelegatingCtorDecls.size(); I != N; ++I) {
8286     CXXConstructorDecl *D
8287       = dyn_cast_or_null<CXXConstructorDecl>(GetDecl(DelegatingCtorDecls[I]));
8288     if (D)
8289       Decls.push_back(D);
8290   }
8291   DelegatingCtorDecls.clear();
8292 }
8293 
8294 void ASTReader::ReadExtVectorDecls(SmallVectorImpl<TypedefNameDecl *> &Decls) {
8295   for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) {
8296     TypedefNameDecl *D
8297       = dyn_cast_or_null<TypedefNameDecl>(GetDecl(ExtVectorDecls[I]));
8298     if (D)
8299       Decls.push_back(D);
8300   }
8301   ExtVectorDecls.clear();
8302 }
8303 
8304 void ASTReader::ReadUnusedLocalTypedefNameCandidates(
8305     llvm::SmallSetVector<const TypedefNameDecl *, 4> &Decls) {
8306   for (unsigned I = 0, N = UnusedLocalTypedefNameCandidates.size(); I != N;
8307        ++I) {
8308     TypedefNameDecl *D = dyn_cast_or_null<TypedefNameDecl>(
8309         GetDecl(UnusedLocalTypedefNameCandidates[I]));
8310     if (D)
8311       Decls.insert(D);
8312   }
8313   UnusedLocalTypedefNameCandidates.clear();
8314 }
8315 
8316 void ASTReader::ReadDeclsToCheckForDeferredDiags(
8317     llvm::SmallVector<Decl *, 4> &Decls) {
8318   for (unsigned I = 0, N = DeclsToCheckForDeferredDiags.size(); I != N;
8319        ++I) {
8320     auto *D = dyn_cast_or_null<Decl>(
8321         GetDecl(DeclsToCheckForDeferredDiags[I]));
8322     if (D)
8323       Decls.push_back(D);
8324   }
8325   DeclsToCheckForDeferredDiags.clear();
8326 }
8327 
8328 
8329 void ASTReader::ReadReferencedSelectors(
8330        SmallVectorImpl<std::pair<Selector, SourceLocation>> &Sels) {
8331   if (ReferencedSelectorsData.empty())
8332     return;
8333 
8334   // If there are @selector references added them to its pool. This is for
8335   // implementation of -Wselector.
8336   unsigned int DataSize = ReferencedSelectorsData.size()-1;
8337   unsigned I = 0;
8338   while (I < DataSize) {
8339     Selector Sel = DecodeSelector(ReferencedSelectorsData[I++]);
8340     SourceLocation SelLoc
8341       = SourceLocation::getFromRawEncoding(ReferencedSelectorsData[I++]);
8342     Sels.push_back(std::make_pair(Sel, SelLoc));
8343   }
8344   ReferencedSelectorsData.clear();
8345 }
8346 
8347 void ASTReader::ReadWeakUndeclaredIdentifiers(
8348        SmallVectorImpl<std::pair<IdentifierInfo *, WeakInfo>> &WeakIDs) {
8349   if (WeakUndeclaredIdentifiers.empty())
8350     return;
8351 
8352   for (unsigned I = 0, N = WeakUndeclaredIdentifiers.size(); I < N; /*none*/) {
8353     IdentifierInfo *WeakId
8354       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8355     IdentifierInfo *AliasId
8356       = DecodeIdentifierInfo(WeakUndeclaredIdentifiers[I++]);
8357     SourceLocation Loc
8358       = SourceLocation::getFromRawEncoding(WeakUndeclaredIdentifiers[I++]);
8359     bool Used = WeakUndeclaredIdentifiers[I++];
8360     WeakInfo WI(AliasId, Loc);
8361     WI.setUsed(Used);
8362     WeakIDs.push_back(std::make_pair(WeakId, WI));
8363   }
8364   WeakUndeclaredIdentifiers.clear();
8365 }
8366 
8367 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
8368   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
8369     ExternalVTableUse VT;
8370     VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++]));
8371     VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]);
8372     VT.DefinitionRequired = VTableUses[Idx++];
8373     VTables.push_back(VT);
8374   }
8375 
8376   VTableUses.clear();
8377 }
8378 
8379 void ASTReader::ReadPendingInstantiations(
8380        SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) {
8381   for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) {
8382     ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++]));
8383     SourceLocation Loc
8384       = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]);
8385 
8386     Pending.push_back(std::make_pair(D, Loc));
8387   }
8388   PendingInstantiations.clear();
8389 }
8390 
8391 void ASTReader::ReadLateParsedTemplates(
8392     llvm::MapVector<const FunctionDecl *, std::unique_ptr<LateParsedTemplate>>
8393         &LPTMap) {
8394   for (auto &LPT : LateParsedTemplates) {
8395     ModuleFile *FMod = LPT.first;
8396     RecordDataImpl &LateParsed = LPT.second;
8397     for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
8398          /* In loop */) {
8399       FunctionDecl *FD =
8400           cast<FunctionDecl>(GetLocalDecl(*FMod, LateParsed[Idx++]));
8401 
8402       auto LT = std::make_unique<LateParsedTemplate>();
8403       LT->D = GetLocalDecl(*FMod, LateParsed[Idx++]);
8404 
8405       ModuleFile *F = getOwningModuleFile(LT->D);
8406       assert(F && "No module");
8407 
8408       unsigned TokN = LateParsed[Idx++];
8409       LT->Toks.reserve(TokN);
8410       for (unsigned T = 0; T < TokN; ++T)
8411         LT->Toks.push_back(ReadToken(*F, LateParsed, Idx));
8412 
8413       LPTMap.insert(std::make_pair(FD, std::move(LT)));
8414     }
8415   }
8416 }
8417 
8418 void ASTReader::LoadSelector(Selector Sel) {
8419   // It would be complicated to avoid reading the methods anyway. So don't.
8420   ReadMethodPool(Sel);
8421 }
8422 
8423 void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
8424   assert(ID && "Non-zero identifier ID required");
8425   assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8426   IdentifiersLoaded[ID - 1] = II;
8427   if (DeserializationListener)
8428     DeserializationListener->IdentifierRead(ID, II);
8429 }
8430 
8431 /// Set the globally-visible declarations associated with the given
8432 /// identifier.
8433 ///
8434 /// If the AST reader is currently in a state where the given declaration IDs
8435 /// cannot safely be resolved, they are queued until it is safe to resolve
8436 /// them.
8437 ///
8438 /// \param II an IdentifierInfo that refers to one or more globally-visible
8439 /// declarations.
8440 ///
8441 /// \param DeclIDs the set of declaration IDs with the name @p II that are
8442 /// visible at global scope.
8443 ///
8444 /// \param Decls if non-null, this vector will be populated with the set of
8445 /// deserialized declarations. These declarations will not be pushed into
8446 /// scope.
8447 void
8448 ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8449                               const SmallVectorImpl<uint32_t> &DeclIDs,
8450                                    SmallVectorImpl<Decl *> *Decls) {
8451   if (NumCurrentElementsDeserializing && !Decls) {
8452     PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
8453     return;
8454   }
8455 
8456   for (unsigned I = 0, N = DeclIDs.size(); I != N; ++I) {
8457     if (!SemaObj) {
8458       // Queue this declaration so that it will be added to the
8459       // translation unit scope and identifier's declaration chain
8460       // once a Sema object is known.
8461       PreloadedDeclIDs.push_back(DeclIDs[I]);
8462       continue;
8463     }
8464 
8465     NamedDecl *D = cast<NamedDecl>(GetDecl(DeclIDs[I]));
8466 
8467     // If we're simply supposed to record the declarations, do so now.
8468     if (Decls) {
8469       Decls->push_back(D);
8470       continue;
8471     }
8472 
8473     // Introduce this declaration into the translation-unit scope
8474     // and add it to the declaration chain for this identifier, so
8475     // that (unqualified) name lookup will find it.
8476     pushExternalDeclIntoScope(D, II);
8477   }
8478 }
8479 
8480 IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
8481   if (ID == 0)
8482     return nullptr;
8483 
8484   if (IdentifiersLoaded.empty()) {
8485     Error("no identifier table in AST file");
8486     return nullptr;
8487   }
8488 
8489   ID -= 1;
8490   if (!IdentifiersLoaded[ID]) {
8491     GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
8492     assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
8493     ModuleFile *M = I->second;
8494     unsigned Index = ID - M->BaseIdentifierID;
8495     const char *Str = M->IdentifierTableData + M->IdentifierOffsets[Index];
8496 
8497     // All of the strings in the AST file are preceded by a 16-bit length.
8498     // Extract that 16-bit length to avoid having to execute strlen().
8499     // NOTE: 'StrLenPtr' is an 'unsigned char*' so that we load bytes as
8500     //  unsigned integers.  This is important to avoid integer overflow when
8501     //  we cast them to 'unsigned'.
8502     const unsigned char *StrLenPtr = (const unsigned char*) Str - 2;
8503     unsigned StrLen = (((unsigned) StrLenPtr[0])
8504                        | (((unsigned) StrLenPtr[1]) << 8)) - 1;
8505     auto &II = PP.getIdentifierTable().get(StringRef(Str, StrLen));
8506     IdentifiersLoaded[ID] = &II;
8507     markIdentifierFromAST(*this,  II);
8508     if (DeserializationListener)
8509       DeserializationListener->IdentifierRead(ID + 1, &II);
8510   }
8511 
8512   return IdentifiersLoaded[ID];
8513 }
8514 
8515 IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
8516   return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
8517 }
8518 
8519 IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
8520   if (LocalID < NUM_PREDEF_IDENT_IDS)
8521     return LocalID;
8522 
8523   if (!M.ModuleOffsetMap.empty())
8524     ReadModuleOffsetMap(M);
8525 
8526   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8527     = M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
8528   assert(I != M.IdentifierRemap.end()
8529          && "Invalid index into identifier index remap");
8530 
8531   return LocalID + I->second;
8532 }
8533 
8534 MacroInfo *ASTReader::getMacro(MacroID ID) {
8535   if (ID == 0)
8536     return nullptr;
8537 
8538   if (MacrosLoaded.empty()) {
8539     Error("no macro table in AST file");
8540     return nullptr;
8541   }
8542 
8543   ID -= NUM_PREDEF_MACRO_IDS;
8544   if (!MacrosLoaded[ID]) {
8545     GlobalMacroMapType::iterator I
8546       = GlobalMacroMap.find(ID + NUM_PREDEF_MACRO_IDS);
8547     assert(I != GlobalMacroMap.end() && "Corrupted global macro map");
8548     ModuleFile *M = I->second;
8549     unsigned Index = ID - M->BaseMacroID;
8550     MacrosLoaded[ID] =
8551         ReadMacroRecord(*M, M->MacroOffsetsBase + M->MacroOffsets[Index]);
8552 
8553     if (DeserializationListener)
8554       DeserializationListener->MacroRead(ID + NUM_PREDEF_MACRO_IDS,
8555                                          MacrosLoaded[ID]);
8556   }
8557 
8558   return MacrosLoaded[ID];
8559 }
8560 
8561 MacroID ASTReader::getGlobalMacroID(ModuleFile &M, unsigned LocalID) {
8562   if (LocalID < NUM_PREDEF_MACRO_IDS)
8563     return LocalID;
8564 
8565   if (!M.ModuleOffsetMap.empty())
8566     ReadModuleOffsetMap(M);
8567 
8568   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8569     = M.MacroRemap.find(LocalID - NUM_PREDEF_MACRO_IDS);
8570   assert(I != M.MacroRemap.end() && "Invalid index into macro index remap");
8571 
8572   return LocalID + I->second;
8573 }
8574 
8575 serialization::SubmoduleID
8576 ASTReader::getGlobalSubmoduleID(ModuleFile &M, unsigned LocalID) {
8577   if (LocalID < NUM_PREDEF_SUBMODULE_IDS)
8578     return LocalID;
8579 
8580   if (!M.ModuleOffsetMap.empty())
8581     ReadModuleOffsetMap(M);
8582 
8583   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8584     = M.SubmoduleRemap.find(LocalID - NUM_PREDEF_SUBMODULE_IDS);
8585   assert(I != M.SubmoduleRemap.end()
8586          && "Invalid index into submodule index remap");
8587 
8588   return LocalID + I->second;
8589 }
8590 
8591 Module *ASTReader::getSubmodule(SubmoduleID GlobalID) {
8592   if (GlobalID < NUM_PREDEF_SUBMODULE_IDS) {
8593     assert(GlobalID == 0 && "Unhandled global submodule ID");
8594     return nullptr;
8595   }
8596 
8597   if (GlobalID > SubmodulesLoaded.size()) {
8598     Error("submodule ID out of range in AST file");
8599     return nullptr;
8600   }
8601 
8602   return SubmodulesLoaded[GlobalID - NUM_PREDEF_SUBMODULE_IDS];
8603 }
8604 
8605 Module *ASTReader::getModule(unsigned ID) {
8606   return getSubmodule(ID);
8607 }
8608 
8609 ModuleFile *ASTReader::getLocalModuleFile(ModuleFile &F, unsigned ID) {
8610   if (ID & 1) {
8611     // It's a module, look it up by submodule ID.
8612     auto I = GlobalSubmoduleMap.find(getGlobalSubmoduleID(F, ID >> 1));
8613     return I == GlobalSubmoduleMap.end() ? nullptr : I->second;
8614   } else {
8615     // It's a prefix (preamble, PCH, ...). Look it up by index.
8616     unsigned IndexFromEnd = ID >> 1;
8617     assert(IndexFromEnd && "got reference to unknown module file");
8618     return getModuleManager().pch_modules().end()[-IndexFromEnd];
8619   }
8620 }
8621 
8622 unsigned ASTReader::getModuleFileID(ModuleFile *F) {
8623   if (!F)
8624     return 1;
8625 
8626   // For a file representing a module, use the submodule ID of the top-level
8627   // module as the file ID. For any other kind of file, the number of such
8628   // files loaded beforehand will be the same on reload.
8629   // FIXME: Is this true even if we have an explicit module file and a PCH?
8630   if (F->isModule())
8631     return ((F->BaseSubmoduleID + NUM_PREDEF_SUBMODULE_IDS) << 1) | 1;
8632 
8633   auto PCHModules = getModuleManager().pch_modules();
8634   auto I = llvm::find(PCHModules, F);
8635   assert(I != PCHModules.end() && "emitting reference to unknown file");
8636   return (I - PCHModules.end()) << 1;
8637 }
8638 
8639 llvm::Optional<ASTSourceDescriptor>
8640 ASTReader::getSourceDescriptor(unsigned ID) {
8641   if (Module *M = getSubmodule(ID))
8642     return ASTSourceDescriptor(*M);
8643 
8644   // If there is only a single PCH, return it instead.
8645   // Chained PCH are not supported.
8646   const auto &PCHChain = ModuleMgr.pch_modules();
8647   if (std::distance(std::begin(PCHChain), std::end(PCHChain))) {
8648     ModuleFile &MF = ModuleMgr.getPrimaryModule();
8649     StringRef ModuleName = llvm::sys::path::filename(MF.OriginalSourceFileName);
8650     StringRef FileName = llvm::sys::path::filename(MF.FileName);
8651     return ASTSourceDescriptor(ModuleName, MF.OriginalDir, FileName,
8652                                MF.Signature);
8653   }
8654   return None;
8655 }
8656 
8657 ExternalASTSource::ExtKind ASTReader::hasExternalDefinitions(const Decl *FD) {
8658   auto I = DefinitionSource.find(FD);
8659   if (I == DefinitionSource.end())
8660     return EK_ReplyHazy;
8661   return I->second ? EK_Never : EK_Always;
8662 }
8663 
8664 Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
8665   return DecodeSelector(getGlobalSelectorID(M, LocalID));
8666 }
8667 
8668 Selector ASTReader::DecodeSelector(serialization::SelectorID ID) {
8669   if (ID == 0)
8670     return Selector();
8671 
8672   if (ID > SelectorsLoaded.size()) {
8673     Error("selector ID out of range in AST file");
8674     return Selector();
8675   }
8676 
8677   if (SelectorsLoaded[ID - 1].getAsOpaquePtr() == nullptr) {
8678     // Load this selector from the selector table.
8679     GlobalSelectorMapType::iterator I = GlobalSelectorMap.find(ID);
8680     assert(I != GlobalSelectorMap.end() && "Corrupted global selector map");
8681     ModuleFile &M = *I->second;
8682     ASTSelectorLookupTrait Trait(*this, M);
8683     unsigned Idx = ID - M.BaseSelectorID - NUM_PREDEF_SELECTOR_IDS;
8684     SelectorsLoaded[ID - 1] =
8685       Trait.ReadKey(M.SelectorLookupTableData + M.SelectorOffsets[Idx], 0);
8686     if (DeserializationListener)
8687       DeserializationListener->SelectorRead(ID, SelectorsLoaded[ID - 1]);
8688   }
8689 
8690   return SelectorsLoaded[ID - 1];
8691 }
8692 
8693 Selector ASTReader::GetExternalSelector(serialization::SelectorID ID) {
8694   return DecodeSelector(ID);
8695 }
8696 
8697 uint32_t ASTReader::GetNumExternalSelectors() {
8698   // ID 0 (the null selector) is considered an external selector.
8699   return getTotalNumSelectors() + 1;
8700 }
8701 
8702 serialization::SelectorID
8703 ASTReader::getGlobalSelectorID(ModuleFile &M, unsigned LocalID) const {
8704   if (LocalID < NUM_PREDEF_SELECTOR_IDS)
8705     return LocalID;
8706 
8707   if (!M.ModuleOffsetMap.empty())
8708     ReadModuleOffsetMap(M);
8709 
8710   ContinuousRangeMap<uint32_t, int, 2>::iterator I
8711     = M.SelectorRemap.find(LocalID - NUM_PREDEF_SELECTOR_IDS);
8712   assert(I != M.SelectorRemap.end()
8713          && "Invalid index into selector index remap");
8714 
8715   return LocalID + I->second;
8716 }
8717 
8718 DeclarationNameLoc
8719 ASTRecordReader::readDeclarationNameLoc(DeclarationName Name) {
8720   DeclarationNameLoc DNLoc;
8721   switch (Name.getNameKind()) {
8722   case DeclarationName::CXXConstructorName:
8723   case DeclarationName::CXXDestructorName:
8724   case DeclarationName::CXXConversionFunctionName:
8725     DNLoc.NamedType.TInfo = readTypeSourceInfo();
8726     break;
8727 
8728   case DeclarationName::CXXOperatorName:
8729     DNLoc.CXXOperatorName.BeginOpNameLoc
8730       = readSourceLocation().getRawEncoding();
8731     DNLoc.CXXOperatorName.EndOpNameLoc
8732       = readSourceLocation().getRawEncoding();
8733     break;
8734 
8735   case DeclarationName::CXXLiteralOperatorName:
8736     DNLoc.CXXLiteralOperatorName.OpNameLoc
8737       = readSourceLocation().getRawEncoding();
8738     break;
8739 
8740   case DeclarationName::Identifier:
8741   case DeclarationName::ObjCZeroArgSelector:
8742   case DeclarationName::ObjCOneArgSelector:
8743   case DeclarationName::ObjCMultiArgSelector:
8744   case DeclarationName::CXXUsingDirective:
8745   case DeclarationName::CXXDeductionGuideName:
8746     break;
8747   }
8748   return DNLoc;
8749 }
8750 
8751 DeclarationNameInfo ASTRecordReader::readDeclarationNameInfo() {
8752   DeclarationNameInfo NameInfo;
8753   NameInfo.setName(readDeclarationName());
8754   NameInfo.setLoc(readSourceLocation());
8755   NameInfo.setInfo(readDeclarationNameLoc(NameInfo.getName()));
8756   return NameInfo;
8757 }
8758 
8759 void ASTRecordReader::readQualifierInfo(QualifierInfo &Info) {
8760   Info.QualifierLoc = readNestedNameSpecifierLoc();
8761   unsigned NumTPLists = readInt();
8762   Info.NumTemplParamLists = NumTPLists;
8763   if (NumTPLists) {
8764     Info.TemplParamLists =
8765         new (getContext()) TemplateParameterList *[NumTPLists];
8766     for (unsigned i = 0; i != NumTPLists; ++i)
8767       Info.TemplParamLists[i] = readTemplateParameterList();
8768   }
8769 }
8770 
8771 TemplateParameterList *
8772 ASTRecordReader::readTemplateParameterList() {
8773   SourceLocation TemplateLoc = readSourceLocation();
8774   SourceLocation LAngleLoc = readSourceLocation();
8775   SourceLocation RAngleLoc = readSourceLocation();
8776 
8777   unsigned NumParams = readInt();
8778   SmallVector<NamedDecl *, 16> Params;
8779   Params.reserve(NumParams);
8780   while (NumParams--)
8781     Params.push_back(readDeclAs<NamedDecl>());
8782 
8783   bool HasRequiresClause = readBool();
8784   Expr *RequiresClause = HasRequiresClause ? readExpr() : nullptr;
8785 
8786   TemplateParameterList *TemplateParams = TemplateParameterList::Create(
8787       getContext(), TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause);
8788   return TemplateParams;
8789 }
8790 
8791 void ASTRecordReader::readTemplateArgumentList(
8792                         SmallVectorImpl<TemplateArgument> &TemplArgs,
8793                         bool Canonicalize) {
8794   unsigned NumTemplateArgs = readInt();
8795   TemplArgs.reserve(NumTemplateArgs);
8796   while (NumTemplateArgs--)
8797     TemplArgs.push_back(readTemplateArgument(Canonicalize));
8798 }
8799 
8800 /// Read a UnresolvedSet structure.
8801 void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) {
8802   unsigned NumDecls = readInt();
8803   Set.reserve(getContext(), NumDecls);
8804   while (NumDecls--) {
8805     DeclID ID = readDeclID();
8806     AccessSpecifier AS = (AccessSpecifier) readInt();
8807     Set.addLazyDecl(getContext(), ID, AS);
8808   }
8809 }
8810 
8811 CXXBaseSpecifier
8812 ASTRecordReader::readCXXBaseSpecifier() {
8813   bool isVirtual = readBool();
8814   bool isBaseOfClass = readBool();
8815   AccessSpecifier AS = static_cast<AccessSpecifier>(readInt());
8816   bool inheritConstructors = readBool();
8817   TypeSourceInfo *TInfo = readTypeSourceInfo();
8818   SourceRange Range = readSourceRange();
8819   SourceLocation EllipsisLoc = readSourceLocation();
8820   CXXBaseSpecifier Result(Range, isVirtual, isBaseOfClass, AS, TInfo,
8821                           EllipsisLoc);
8822   Result.setInheritConstructors(inheritConstructors);
8823   return Result;
8824 }
8825 
8826 CXXCtorInitializer **
8827 ASTRecordReader::readCXXCtorInitializers() {
8828   ASTContext &Context = getContext();
8829   unsigned NumInitializers = readInt();
8830   assert(NumInitializers && "wrote ctor initializers but have no inits");
8831   auto **CtorInitializers = new (Context) CXXCtorInitializer*[NumInitializers];
8832   for (unsigned i = 0; i != NumInitializers; ++i) {
8833     TypeSourceInfo *TInfo = nullptr;
8834     bool IsBaseVirtual = false;
8835     FieldDecl *Member = nullptr;
8836     IndirectFieldDecl *IndirectMember = nullptr;
8837 
8838     CtorInitializerType Type = (CtorInitializerType) readInt();
8839     switch (Type) {
8840     case CTOR_INITIALIZER_BASE:
8841       TInfo = readTypeSourceInfo();
8842       IsBaseVirtual = readBool();
8843       break;
8844 
8845     case CTOR_INITIALIZER_DELEGATING:
8846       TInfo = readTypeSourceInfo();
8847       break;
8848 
8849      case CTOR_INITIALIZER_MEMBER:
8850       Member = readDeclAs<FieldDecl>();
8851       break;
8852 
8853      case CTOR_INITIALIZER_INDIRECT_MEMBER:
8854       IndirectMember = readDeclAs<IndirectFieldDecl>();
8855       break;
8856     }
8857 
8858     SourceLocation MemberOrEllipsisLoc = readSourceLocation();
8859     Expr *Init = readExpr();
8860     SourceLocation LParenLoc = readSourceLocation();
8861     SourceLocation RParenLoc = readSourceLocation();
8862 
8863     CXXCtorInitializer *BOMInit;
8864     if (Type == CTOR_INITIALIZER_BASE)
8865       BOMInit = new (Context)
8866           CXXCtorInitializer(Context, TInfo, IsBaseVirtual, LParenLoc, Init,
8867                              RParenLoc, MemberOrEllipsisLoc);
8868     else if (Type == CTOR_INITIALIZER_DELEGATING)
8869       BOMInit = new (Context)
8870           CXXCtorInitializer(Context, TInfo, LParenLoc, Init, RParenLoc);
8871     else if (Member)
8872       BOMInit = new (Context)
8873           CXXCtorInitializer(Context, Member, MemberOrEllipsisLoc, LParenLoc,
8874                              Init, RParenLoc);
8875     else
8876       BOMInit = new (Context)
8877           CXXCtorInitializer(Context, IndirectMember, MemberOrEllipsisLoc,
8878                              LParenLoc, Init, RParenLoc);
8879 
8880     if (/*IsWritten*/readBool()) {
8881       unsigned SourceOrder = readInt();
8882       BOMInit->setSourceOrder(SourceOrder);
8883     }
8884 
8885     CtorInitializers[i] = BOMInit;
8886   }
8887 
8888   return CtorInitializers;
8889 }
8890 
8891 NestedNameSpecifierLoc
8892 ASTRecordReader::readNestedNameSpecifierLoc() {
8893   ASTContext &Context = getContext();
8894   unsigned N = readInt();
8895   NestedNameSpecifierLocBuilder Builder;
8896   for (unsigned I = 0; I != N; ++I) {
8897     auto Kind = readNestedNameSpecifierKind();
8898     switch (Kind) {
8899     case NestedNameSpecifier::Identifier: {
8900       IdentifierInfo *II = readIdentifier();
8901       SourceRange Range = readSourceRange();
8902       Builder.Extend(Context, II, Range.getBegin(), Range.getEnd());
8903       break;
8904     }
8905 
8906     case NestedNameSpecifier::Namespace: {
8907       NamespaceDecl *NS = readDeclAs<NamespaceDecl>();
8908       SourceRange Range = readSourceRange();
8909       Builder.Extend(Context, NS, Range.getBegin(), Range.getEnd());
8910       break;
8911     }
8912 
8913     case NestedNameSpecifier::NamespaceAlias: {
8914       NamespaceAliasDecl *Alias = readDeclAs<NamespaceAliasDecl>();
8915       SourceRange Range = readSourceRange();
8916       Builder.Extend(Context, Alias, Range.getBegin(), Range.getEnd());
8917       break;
8918     }
8919 
8920     case NestedNameSpecifier::TypeSpec:
8921     case NestedNameSpecifier::TypeSpecWithTemplate: {
8922       bool Template = readBool();
8923       TypeSourceInfo *T = readTypeSourceInfo();
8924       if (!T)
8925         return NestedNameSpecifierLoc();
8926       SourceLocation ColonColonLoc = readSourceLocation();
8927 
8928       // FIXME: 'template' keyword location not saved anywhere, so we fake it.
8929       Builder.Extend(Context,
8930                      Template? T->getTypeLoc().getBeginLoc() : SourceLocation(),
8931                      T->getTypeLoc(), ColonColonLoc);
8932       break;
8933     }
8934 
8935     case NestedNameSpecifier::Global: {
8936       SourceLocation ColonColonLoc = readSourceLocation();
8937       Builder.MakeGlobal(Context, ColonColonLoc);
8938       break;
8939     }
8940 
8941     case NestedNameSpecifier::Super: {
8942       CXXRecordDecl *RD = readDeclAs<CXXRecordDecl>();
8943       SourceRange Range = readSourceRange();
8944       Builder.MakeSuper(Context, RD, Range.getBegin(), Range.getEnd());
8945       break;
8946     }
8947     }
8948   }
8949 
8950   return Builder.getWithLocInContext(Context);
8951 }
8952 
8953 SourceRange
8954 ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
8955                            unsigned &Idx) {
8956   SourceLocation beg = ReadSourceLocation(F, Record, Idx);
8957   SourceLocation end = ReadSourceLocation(F, Record, Idx);
8958   return SourceRange(beg, end);
8959 }
8960 
8961 static llvm::FixedPointSemantics
8962 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
8963                         unsigned &Idx) {
8964   unsigned Width = Record[Idx++];
8965   unsigned Scale = Record[Idx++];
8966   uint64_t Tmp = Record[Idx++];
8967   bool IsSigned = Tmp & 0x1;
8968   bool IsSaturated = Tmp & 0x2;
8969   bool HasUnsignedPadding = Tmp & 0x4;
8970   return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8971                                    HasUnsignedPadding);
8972 }
8973 
8974 static const llvm::fltSemantics &
8975 readAPFloatSemantics(ASTRecordReader &reader) {
8976   return llvm::APFloatBase::EnumToSemantics(
8977     static_cast<llvm::APFloatBase::Semantics>(reader.readInt()));
8978 }
8979 
8980 APValue ASTRecordReader::readAPValue() {
8981   unsigned Kind = readInt();
8982   switch ((APValue::ValueKind) Kind) {
8983   case APValue::None:
8984     return APValue();
8985   case APValue::Indeterminate:
8986     return APValue::IndeterminateValue();
8987   case APValue::Int:
8988     return APValue(readAPSInt());
8989   case APValue::Float: {
8990     const llvm::fltSemantics &FloatSema = readAPFloatSemantics(*this);
8991     return APValue(readAPFloat(FloatSema));
8992   }
8993   case APValue::FixedPoint: {
8994     llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8995     return APValue(llvm::APFixedPoint(readAPInt(), FPSema));
8996   }
8997   case APValue::ComplexInt: {
8998     llvm::APSInt First = readAPSInt();
8999     return APValue(std::move(First), readAPSInt());
9000   }
9001   case APValue::ComplexFloat: {
9002     const llvm::fltSemantics &FloatSema1 = readAPFloatSemantics(*this);
9003     llvm::APFloat First = readAPFloat(FloatSema1);
9004     const llvm::fltSemantics &FloatSema2 = readAPFloatSemantics(*this);
9005     return APValue(std::move(First), readAPFloat(FloatSema2));
9006   }
9007   case APValue::LValue:
9008   case APValue::Vector:
9009   case APValue::Array:
9010   case APValue::Struct:
9011   case APValue::Union:
9012   case APValue::MemberPointer:
9013   case APValue::AddrLabelDiff:
9014     // TODO : Handle all these APValue::ValueKind.
9015     return APValue();
9016   }
9017   llvm_unreachable("Invalid APValue::ValueKind");
9018 }
9019 
9020 /// Read a floating-point value
9021 llvm::APFloat ASTRecordReader::readAPFloat(const llvm::fltSemantics &Sem) {
9022   return llvm::APFloat(Sem, readAPInt());
9023 }
9024 
9025 // Read a string
9026 std::string ASTReader::ReadString(const RecordData &Record, unsigned &Idx) {
9027   unsigned Len = Record[Idx++];
9028   std::string Result(Record.data() + Idx, Record.data() + Idx + Len);
9029   Idx += Len;
9030   return Result;
9031 }
9032 
9033 std::string ASTReader::ReadPath(ModuleFile &F, const RecordData &Record,
9034                                 unsigned &Idx) {
9035   std::string Filename = ReadString(Record, Idx);
9036   ResolveImportedPath(F, Filename);
9037   return Filename;
9038 }
9039 
9040 std::string ASTReader::ReadPath(StringRef BaseDirectory,
9041                                 const RecordData &Record, unsigned &Idx) {
9042   std::string Filename = ReadString(Record, Idx);
9043   if (!BaseDirectory.empty())
9044     ResolveImportedPath(Filename, BaseDirectory);
9045   return Filename;
9046 }
9047 
9048 VersionTuple ASTReader::ReadVersionTuple(const RecordData &Record,
9049                                          unsigned &Idx) {
9050   unsigned Major = Record[Idx++];
9051   unsigned Minor = Record[Idx++];
9052   unsigned Subminor = Record[Idx++];
9053   if (Minor == 0)
9054     return VersionTuple(Major);
9055   if (Subminor == 0)
9056     return VersionTuple(Major, Minor - 1);
9057   return VersionTuple(Major, Minor - 1, Subminor - 1);
9058 }
9059 
9060 CXXTemporary *ASTReader::ReadCXXTemporary(ModuleFile &F,
9061                                           const RecordData &Record,
9062                                           unsigned &Idx) {
9063   CXXDestructorDecl *Decl = ReadDeclAs<CXXDestructorDecl>(F, Record, Idx);
9064   return CXXTemporary::Create(getContext(), Decl);
9065 }
9066 
9067 DiagnosticBuilder ASTReader::Diag(unsigned DiagID) const {
9068   return Diag(CurrentImportLoc, DiagID);
9069 }
9070 
9071 DiagnosticBuilder ASTReader::Diag(SourceLocation Loc, unsigned DiagID) const {
9072   return Diags.Report(Loc, DiagID);
9073 }
9074 
9075 /// Retrieve the identifier table associated with the
9076 /// preprocessor.
9077 IdentifierTable &ASTReader::getIdentifierTable() {
9078   return PP.getIdentifierTable();
9079 }
9080 
9081 /// Record that the given ID maps to the given switch-case
9082 /// statement.
9083 void ASTReader::RecordSwitchCaseID(SwitchCase *SC, unsigned ID) {
9084   assert((*CurrSwitchCaseStmts)[ID] == nullptr &&
9085          "Already have a SwitchCase with this ID");
9086   (*CurrSwitchCaseStmts)[ID] = SC;
9087 }
9088 
9089 /// Retrieve the switch-case statement with the given ID.
9090 SwitchCase *ASTReader::getSwitchCaseWithID(unsigned ID) {
9091   assert((*CurrSwitchCaseStmts)[ID] != nullptr && "No SwitchCase with this ID");
9092   return (*CurrSwitchCaseStmts)[ID];
9093 }
9094 
9095 void ASTReader::ClearSwitchCaseIDs() {
9096   CurrSwitchCaseStmts->clear();
9097 }
9098 
9099 void ASTReader::ReadComments() {
9100   ASTContext &Context = getContext();
9101   std::vector<RawComment *> Comments;
9102   for (SmallVectorImpl<std::pair<BitstreamCursor,
9103                                  serialization::ModuleFile *>>::iterator
9104        I = CommentsCursors.begin(),
9105        E = CommentsCursors.end();
9106        I != E; ++I) {
9107     Comments.clear();
9108     BitstreamCursor &Cursor = I->first;
9109     serialization::ModuleFile &F = *I->second;
9110     SavedStreamPosition SavedPosition(Cursor);
9111 
9112     RecordData Record;
9113     while (true) {
9114       Expected<llvm::BitstreamEntry> MaybeEntry =
9115           Cursor.advanceSkippingSubblocks(
9116               BitstreamCursor::AF_DontPopBlockAtEnd);
9117       if (!MaybeEntry) {
9118         Error(MaybeEntry.takeError());
9119         return;
9120       }
9121       llvm::BitstreamEntry Entry = MaybeEntry.get();
9122 
9123       switch (Entry.Kind) {
9124       case llvm::BitstreamEntry::SubBlock: // Handled for us already.
9125       case llvm::BitstreamEntry::Error:
9126         Error("malformed block record in AST file");
9127         return;
9128       case llvm::BitstreamEntry::EndBlock:
9129         goto NextCursor;
9130       case llvm::BitstreamEntry::Record:
9131         // The interesting case.
9132         break;
9133       }
9134 
9135       // Read a record.
9136       Record.clear();
9137       Expected<unsigned> MaybeComment = Cursor.readRecord(Entry.ID, Record);
9138       if (!MaybeComment) {
9139         Error(MaybeComment.takeError());
9140         return;
9141       }
9142       switch ((CommentRecordTypes)MaybeComment.get()) {
9143       case COMMENTS_RAW_COMMENT: {
9144         unsigned Idx = 0;
9145         SourceRange SR = ReadSourceRange(F, Record, Idx);
9146         RawComment::CommentKind Kind =
9147             (RawComment::CommentKind) Record[Idx++];
9148         bool IsTrailingComment = Record[Idx++];
9149         bool IsAlmostTrailingComment = Record[Idx++];
9150         Comments.push_back(new (Context) RawComment(
9151             SR, Kind, IsTrailingComment, IsAlmostTrailingComment));
9152         break;
9153       }
9154       }
9155     }
9156   NextCursor:
9157     llvm::DenseMap<FileID, std::map<unsigned, RawComment *>>
9158         FileToOffsetToComment;
9159     for (RawComment *C : Comments) {
9160       SourceLocation CommentLoc = C->getBeginLoc();
9161       if (CommentLoc.isValid()) {
9162         std::pair<FileID, unsigned> Loc =
9163             SourceMgr.getDecomposedLoc(CommentLoc);
9164         if (Loc.first.isValid())
9165           Context.Comments.OrderedComments[Loc.first].emplace(Loc.second, C);
9166       }
9167     }
9168   }
9169 }
9170 
9171 void ASTReader::visitInputFiles(serialization::ModuleFile &MF,
9172                                 bool IncludeSystem, bool Complain,
9173                     llvm::function_ref<void(const serialization::InputFile &IF,
9174                                             bool isSystem)> Visitor) {
9175   unsigned NumUserInputs = MF.NumUserInputFiles;
9176   unsigned NumInputs = MF.InputFilesLoaded.size();
9177   assert(NumUserInputs <= NumInputs);
9178   unsigned N = IncludeSystem ? NumInputs : NumUserInputs;
9179   for (unsigned I = 0; I < N; ++I) {
9180     bool IsSystem = I >= NumUserInputs;
9181     InputFile IF = getInputFile(MF, I+1, Complain);
9182     Visitor(IF, IsSystem);
9183   }
9184 }
9185 
9186 void ASTReader::visitTopLevelModuleMaps(
9187     serialization::ModuleFile &MF,
9188     llvm::function_ref<void(const FileEntry *FE)> Visitor) {
9189   unsigned NumInputs = MF.InputFilesLoaded.size();
9190   for (unsigned I = 0; I < NumInputs; ++I) {
9191     InputFileInfo IFI = readInputFileInfo(MF, I + 1);
9192     if (IFI.TopLevelModuleMap)
9193       // FIXME: This unnecessarily re-reads the InputFileInfo.
9194       if (auto *FE = getInputFile(MF, I + 1).getFile())
9195         Visitor(FE);
9196   }
9197 }
9198 
9199 std::string ASTReader::getOwningModuleNameForDiagnostic(const Decl *D) {
9200   // If we know the owning module, use it.
9201   if (Module *M = D->getImportedOwningModule())
9202     return M->getFullModuleName();
9203 
9204   // Otherwise, use the name of the top-level module the decl is within.
9205   if (ModuleFile *M = getOwningModuleFile(D))
9206     return M->ModuleName;
9207 
9208   // Not from a module.
9209   return {};
9210 }
9211 
9212 void ASTReader::finishPendingActions() {
9213   while (!PendingIdentifierInfos.empty() || !PendingFunctionTypes.empty() ||
9214          !PendingIncompleteDeclChains.empty() || !PendingDeclChains.empty() ||
9215          !PendingMacroIDs.empty() || !PendingDeclContextInfos.empty() ||
9216          !PendingUpdateRecords.empty()) {
9217     // If any identifiers with corresponding top-level declarations have
9218     // been loaded, load those declarations now.
9219     using TopLevelDeclsMap =
9220         llvm::DenseMap<IdentifierInfo *, SmallVector<Decl *, 2>>;
9221     TopLevelDeclsMap TopLevelDecls;
9222 
9223     while (!PendingIdentifierInfos.empty()) {
9224       IdentifierInfo *II = PendingIdentifierInfos.back().first;
9225       SmallVector<uint32_t, 4> DeclIDs =
9226           std::move(PendingIdentifierInfos.back().second);
9227       PendingIdentifierInfos.pop_back();
9228 
9229       SetGloballyVisibleDecls(II, DeclIDs, &TopLevelDecls[II]);
9230     }
9231 
9232     // Load each function type that we deferred loading because it was a
9233     // deduced type that might refer to a local type declared within itself.
9234     for (unsigned I = 0; I != PendingFunctionTypes.size(); ++I) {
9235       auto *FD = PendingFunctionTypes[I].first;
9236       FD->setType(GetType(PendingFunctionTypes[I].second));
9237 
9238       // If we gave a function a deduced return type, remember that we need to
9239       // propagate that along the redeclaration chain.
9240       auto *DT = FD->getReturnType()->getContainedDeducedType();
9241       if (DT && DT->isDeduced())
9242         PendingDeducedTypeUpdates.insert(
9243             {FD->getCanonicalDecl(), FD->getReturnType()});
9244     }
9245     PendingFunctionTypes.clear();
9246 
9247     // For each decl chain that we wanted to complete while deserializing, mark
9248     // it as "still needs to be completed".
9249     for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {
9250       markIncompleteDeclChain(PendingIncompleteDeclChains[I]);
9251     }
9252     PendingIncompleteDeclChains.clear();
9253 
9254     // Load pending declaration chains.
9255     for (unsigned I = 0; I != PendingDeclChains.size(); ++I)
9256       loadPendingDeclChain(PendingDeclChains[I].first,
9257                            PendingDeclChains[I].second);
9258     PendingDeclChains.clear();
9259 
9260     // Make the most recent of the top-level declarations visible.
9261     for (TopLevelDeclsMap::iterator TLD = TopLevelDecls.begin(),
9262            TLDEnd = TopLevelDecls.end(); TLD != TLDEnd; ++TLD) {
9263       IdentifierInfo *II = TLD->first;
9264       for (unsigned I = 0, N = TLD->second.size(); I != N; ++I) {
9265         pushExternalDeclIntoScope(cast<NamedDecl>(TLD->second[I]), II);
9266       }
9267     }
9268 
9269     // Load any pending macro definitions.
9270     for (unsigned I = 0; I != PendingMacroIDs.size(); ++I) {
9271       IdentifierInfo *II = PendingMacroIDs.begin()[I].first;
9272       SmallVector<PendingMacroInfo, 2> GlobalIDs;
9273       GlobalIDs.swap(PendingMacroIDs.begin()[I].second);
9274       // Initialize the macro history from chained-PCHs ahead of module imports.
9275       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9276            ++IDIdx) {
9277         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9278         if (!Info.M->isModule())
9279           resolvePendingMacro(II, Info);
9280       }
9281       // Handle module imports.
9282       for (unsigned IDIdx = 0, NumIDs = GlobalIDs.size(); IDIdx != NumIDs;
9283            ++IDIdx) {
9284         const PendingMacroInfo &Info = GlobalIDs[IDIdx];
9285         if (Info.M->isModule())
9286           resolvePendingMacro(II, Info);
9287       }
9288     }
9289     PendingMacroIDs.clear();
9290 
9291     // Wire up the DeclContexts for Decls that we delayed setting until
9292     // recursive loading is completed.
9293     while (!PendingDeclContextInfos.empty()) {
9294       PendingDeclContextInfo Info = PendingDeclContextInfos.front();
9295       PendingDeclContextInfos.pop_front();
9296       DeclContext *SemaDC = cast<DeclContext>(GetDecl(Info.SemaDC));
9297       DeclContext *LexicalDC = cast<DeclContext>(GetDecl(Info.LexicalDC));
9298       Info.D->setDeclContextsImpl(SemaDC, LexicalDC, getContext());
9299     }
9300 
9301     // Perform any pending declaration updates.
9302     while (!PendingUpdateRecords.empty()) {
9303       auto Update = PendingUpdateRecords.pop_back_val();
9304       ReadingKindTracker ReadingKind(Read_Decl, *this);
9305       loadDeclUpdateRecords(Update);
9306     }
9307   }
9308 
9309   // At this point, all update records for loaded decls are in place, so any
9310   // fake class definitions should have become real.
9311   assert(PendingFakeDefinitionData.empty() &&
9312          "faked up a class definition but never saw the real one");
9313 
9314   // If we deserialized any C++ or Objective-C class definitions, any
9315   // Objective-C protocol definitions, or any redeclarable templates, make sure
9316   // that all redeclarations point to the definitions. Note that this can only
9317   // happen now, after the redeclaration chains have been fully wired.
9318   for (Decl *D : PendingDefinitions) {
9319     if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
9320       if (const TagType *TagT = dyn_cast<TagType>(TD->getTypeForDecl())) {
9321         // Make sure that the TagType points at the definition.
9322         const_cast<TagType*>(TagT)->decl = TD;
9323       }
9324 
9325       if (auto RD = dyn_cast<CXXRecordDecl>(D)) {
9326         for (auto *R = getMostRecentExistingDecl(RD); R;
9327              R = R->getPreviousDecl()) {
9328           assert((R == D) ==
9329                      cast<CXXRecordDecl>(R)->isThisDeclarationADefinition() &&
9330                  "declaration thinks it's the definition but it isn't");
9331           cast<CXXRecordDecl>(R)->DefinitionData = RD->DefinitionData;
9332         }
9333       }
9334 
9335       continue;
9336     }
9337 
9338     if (auto ID = dyn_cast<ObjCInterfaceDecl>(D)) {
9339       // Make sure that the ObjCInterfaceType points at the definition.
9340       const_cast<ObjCInterfaceType *>(cast<ObjCInterfaceType>(ID->TypeForDecl))
9341         ->Decl = ID;
9342 
9343       for (auto *R = getMostRecentExistingDecl(ID); R; R = R->getPreviousDecl())
9344         cast<ObjCInterfaceDecl>(R)->Data = ID->Data;
9345 
9346       continue;
9347     }
9348 
9349     if (auto PD = dyn_cast<ObjCProtocolDecl>(D)) {
9350       for (auto *R = getMostRecentExistingDecl(PD); R; R = R->getPreviousDecl())
9351         cast<ObjCProtocolDecl>(R)->Data = PD->Data;
9352 
9353       continue;
9354     }
9355 
9356     auto RTD = cast<RedeclarableTemplateDecl>(D)->getCanonicalDecl();
9357     for (auto *R = getMostRecentExistingDecl(RTD); R; R = R->getPreviousDecl())
9358       cast<RedeclarableTemplateDecl>(R)->Common = RTD->Common;
9359   }
9360   PendingDefinitions.clear();
9361 
9362   // Load the bodies of any functions or methods we've encountered. We do
9363   // this now (delayed) so that we can be sure that the declaration chains
9364   // have been fully wired up (hasBody relies on this).
9365   // FIXME: We shouldn't require complete redeclaration chains here.
9366   for (PendingBodiesMap::iterator PB = PendingBodies.begin(),
9367                                PBEnd = PendingBodies.end();
9368        PB != PBEnd; ++PB) {
9369     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(PB->first)) {
9370       // For a function defined inline within a class template, force the
9371       // canonical definition to be the one inside the canonical definition of
9372       // the template. This ensures that we instantiate from a correct view
9373       // of the template.
9374       //
9375       // Sadly we can't do this more generally: we can't be sure that all
9376       // copies of an arbitrary class definition will have the same members
9377       // defined (eg, some member functions may not be instantiated, and some
9378       // special members may or may not have been implicitly defined).
9379       if (auto *RD = dyn_cast<CXXRecordDecl>(FD->getLexicalParent()))
9380         if (RD->isDependentContext() && !RD->isThisDeclarationADefinition())
9381           continue;
9382 
9383       // FIXME: Check for =delete/=default?
9384       // FIXME: Complain about ODR violations here?
9385       const FunctionDecl *Defn = nullptr;
9386       if (!getContext().getLangOpts().Modules || !FD->hasBody(Defn)) {
9387         FD->setLazyBody(PB->second);
9388       } else {
9389         auto *NonConstDefn = const_cast<FunctionDecl*>(Defn);
9390         mergeDefinitionVisibility(NonConstDefn, FD);
9391 
9392         if (!FD->isLateTemplateParsed() &&
9393             !NonConstDefn->isLateTemplateParsed() &&
9394             FD->getODRHash() != NonConstDefn->getODRHash()) {
9395           if (!isa<CXXMethodDecl>(FD)) {
9396             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9397           } else if (FD->getLexicalParent()->isFileContext() &&
9398                      NonConstDefn->getLexicalParent()->isFileContext()) {
9399             // Only diagnose out-of-line method definitions.  If they are
9400             // in class definitions, then an error will be generated when
9401             // processing the class bodies.
9402             PendingFunctionOdrMergeFailures[FD].push_back(NonConstDefn);
9403           }
9404         }
9405       }
9406       continue;
9407     }
9408 
9409     ObjCMethodDecl *MD = cast<ObjCMethodDecl>(PB->first);
9410     if (!getContext().getLangOpts().Modules || !MD->hasBody())
9411       MD->setLazyBody(PB->second);
9412   }
9413   PendingBodies.clear();
9414 
9415   // Do some cleanup.
9416   for (auto *ND : PendingMergedDefinitionsToDeduplicate)
9417     getContext().deduplicateMergedDefinitonsFor(ND);
9418   PendingMergedDefinitionsToDeduplicate.clear();
9419 }
9420 
9421 void ASTReader::diagnoseOdrViolations() {
9422   if (PendingOdrMergeFailures.empty() && PendingOdrMergeChecks.empty() &&
9423       PendingFunctionOdrMergeFailures.empty() &&
9424       PendingEnumOdrMergeFailures.empty())
9425     return;
9426 
9427   // Trigger the import of the full definition of each class that had any
9428   // odr-merging problems, so we can produce better diagnostics for them.
9429   // These updates may in turn find and diagnose some ODR failures, so take
9430   // ownership of the set first.
9431   auto OdrMergeFailures = std::move(PendingOdrMergeFailures);
9432   PendingOdrMergeFailures.clear();
9433   for (auto &Merge : OdrMergeFailures) {
9434     Merge.first->buildLookup();
9435     Merge.first->decls_begin();
9436     Merge.first->bases_begin();
9437     Merge.first->vbases_begin();
9438     for (auto &RecordPair : Merge.second) {
9439       auto *RD = RecordPair.first;
9440       RD->decls_begin();
9441       RD->bases_begin();
9442       RD->vbases_begin();
9443     }
9444   }
9445 
9446   // Trigger the import of functions.
9447   auto FunctionOdrMergeFailures = std::move(PendingFunctionOdrMergeFailures);
9448   PendingFunctionOdrMergeFailures.clear();
9449   for (auto &Merge : FunctionOdrMergeFailures) {
9450     Merge.first->buildLookup();
9451     Merge.first->decls_begin();
9452     Merge.first->getBody();
9453     for (auto &FD : Merge.second) {
9454       FD->buildLookup();
9455       FD->decls_begin();
9456       FD->getBody();
9457     }
9458   }
9459 
9460   // Trigger the import of enums.
9461   auto EnumOdrMergeFailures = std::move(PendingEnumOdrMergeFailures);
9462   PendingEnumOdrMergeFailures.clear();
9463   for (auto &Merge : EnumOdrMergeFailures) {
9464     Merge.first->decls_begin();
9465     for (auto &Enum : Merge.second) {
9466       Enum->decls_begin();
9467     }
9468   }
9469 
9470   // For each declaration from a merged context, check that the canonical
9471   // definition of that context also contains a declaration of the same
9472   // entity.
9473   //
9474   // Caution: this loop does things that might invalidate iterators into
9475   // PendingOdrMergeChecks. Don't turn this into a range-based for loop!
9476   while (!PendingOdrMergeChecks.empty()) {
9477     NamedDecl *D = PendingOdrMergeChecks.pop_back_val();
9478 
9479     // FIXME: Skip over implicit declarations for now. This matters for things
9480     // like implicitly-declared special member functions. This isn't entirely
9481     // correct; we can end up with multiple unmerged declarations of the same
9482     // implicit entity.
9483     if (D->isImplicit())
9484       continue;
9485 
9486     DeclContext *CanonDef = D->getDeclContext();
9487 
9488     bool Found = false;
9489     const Decl *DCanon = D->getCanonicalDecl();
9490 
9491     for (auto RI : D->redecls()) {
9492       if (RI->getLexicalDeclContext() == CanonDef) {
9493         Found = true;
9494         break;
9495       }
9496     }
9497     if (Found)
9498       continue;
9499 
9500     // Quick check failed, time to do the slow thing. Note, we can't just
9501     // look up the name of D in CanonDef here, because the member that is
9502     // in CanonDef might not be found by name lookup (it might have been
9503     // replaced by a more recent declaration in the lookup table), and we
9504     // can't necessarily find it in the redeclaration chain because it might
9505     // be merely mergeable, not redeclarable.
9506     llvm::SmallVector<const NamedDecl*, 4> Candidates;
9507     for (auto *CanonMember : CanonDef->decls()) {
9508       if (CanonMember->getCanonicalDecl() == DCanon) {
9509         // This can happen if the declaration is merely mergeable and not
9510         // actually redeclarable (we looked for redeclarations earlier).
9511         //
9512         // FIXME: We should be able to detect this more efficiently, without
9513         // pulling in all of the members of CanonDef.
9514         Found = true;
9515         break;
9516       }
9517       if (auto *ND = dyn_cast<NamedDecl>(CanonMember))
9518         if (ND->getDeclName() == D->getDeclName())
9519           Candidates.push_back(ND);
9520     }
9521 
9522     if (!Found) {
9523       // The AST doesn't like TagDecls becoming invalid after they've been
9524       // completed. We only really need to mark FieldDecls as invalid here.
9525       if (!isa<TagDecl>(D))
9526         D->setInvalidDecl();
9527 
9528       // Ensure we don't accidentally recursively enter deserialization while
9529       // we're producing our diagnostic.
9530       Deserializing RecursionGuard(this);
9531 
9532       std::string CanonDefModule =
9533           getOwningModuleNameForDiagnostic(cast<Decl>(CanonDef));
9534       Diag(D->getLocation(), diag::err_module_odr_violation_missing_decl)
9535         << D << getOwningModuleNameForDiagnostic(D)
9536         << CanonDef << CanonDefModule.empty() << CanonDefModule;
9537 
9538       if (Candidates.empty())
9539         Diag(cast<Decl>(CanonDef)->getLocation(),
9540              diag::note_module_odr_violation_no_possible_decls) << D;
9541       else {
9542         for (unsigned I = 0, N = Candidates.size(); I != N; ++I)
9543           Diag(Candidates[I]->getLocation(),
9544                diag::note_module_odr_violation_possible_decl)
9545             << Candidates[I];
9546       }
9547 
9548       DiagnosedOdrMergeFailures.insert(CanonDef);
9549     }
9550   }
9551 
9552   if (OdrMergeFailures.empty() && FunctionOdrMergeFailures.empty() &&
9553       EnumOdrMergeFailures.empty())
9554     return;
9555 
9556   // Ensure we don't accidentally recursively enter deserialization while
9557   // we're producing our diagnostics.
9558   Deserializing RecursionGuard(this);
9559 
9560   // Common code for hashing helpers.
9561   ODRHash Hash;
9562   auto ComputeQualTypeODRHash = [&Hash](QualType Ty) {
9563     Hash.clear();
9564     Hash.AddQualType(Ty);
9565     return Hash.CalculateHash();
9566   };
9567 
9568   auto ComputeODRHash = [&Hash](const Stmt *S) {
9569     assert(S);
9570     Hash.clear();
9571     Hash.AddStmt(S);
9572     return Hash.CalculateHash();
9573   };
9574 
9575   auto ComputeSubDeclODRHash = [&Hash](const Decl *D) {
9576     assert(D);
9577     Hash.clear();
9578     Hash.AddSubDecl(D);
9579     return Hash.CalculateHash();
9580   };
9581 
9582   auto ComputeTemplateArgumentODRHash = [&Hash](const TemplateArgument &TA) {
9583     Hash.clear();
9584     Hash.AddTemplateArgument(TA);
9585     return Hash.CalculateHash();
9586   };
9587 
9588   auto ComputeTemplateParameterListODRHash =
9589       [&Hash](const TemplateParameterList *TPL) {
9590         assert(TPL);
9591         Hash.clear();
9592         Hash.AddTemplateParameterList(TPL);
9593         return Hash.CalculateHash();
9594       };
9595 
9596   // Used with err_module_odr_violation_mismatch_decl and
9597   // note_module_odr_violation_mismatch_decl
9598   // This list should be the same Decl's as in ODRHash::isDeclToBeProcessed
9599   enum ODRMismatchDecl {
9600     EndOfClass,
9601     PublicSpecifer,
9602     PrivateSpecifer,
9603     ProtectedSpecifer,
9604     StaticAssert,
9605     Field,
9606     CXXMethod,
9607     TypeAlias,
9608     TypeDef,
9609     Var,
9610     Friend,
9611     FunctionTemplate,
9612     Other
9613   };
9614 
9615   // Used with err_module_odr_violation_mismatch_decl_diff and
9616   // note_module_odr_violation_mismatch_decl_diff
9617   enum ODRMismatchDeclDifference {
9618     StaticAssertCondition,
9619     StaticAssertMessage,
9620     StaticAssertOnlyMessage,
9621     FieldName,
9622     FieldTypeName,
9623     FieldSingleBitField,
9624     FieldDifferentWidthBitField,
9625     FieldSingleMutable,
9626     FieldSingleInitializer,
9627     FieldDifferentInitializers,
9628     MethodName,
9629     MethodDeleted,
9630     MethodDefaulted,
9631     MethodVirtual,
9632     MethodStatic,
9633     MethodVolatile,
9634     MethodConst,
9635     MethodInline,
9636     MethodNumberParameters,
9637     MethodParameterType,
9638     MethodParameterName,
9639     MethodParameterSingleDefaultArgument,
9640     MethodParameterDifferentDefaultArgument,
9641     MethodNoTemplateArguments,
9642     MethodDifferentNumberTemplateArguments,
9643     MethodDifferentTemplateArgument,
9644     MethodSingleBody,
9645     MethodDifferentBody,
9646     TypedefName,
9647     TypedefType,
9648     VarName,
9649     VarType,
9650     VarSingleInitializer,
9651     VarDifferentInitializer,
9652     VarConstexpr,
9653     FriendTypeFunction,
9654     FriendType,
9655     FriendFunction,
9656     FunctionTemplateDifferentNumberParameters,
9657     FunctionTemplateParameterDifferentKind,
9658     FunctionTemplateParameterName,
9659     FunctionTemplateParameterSingleDefaultArgument,
9660     FunctionTemplateParameterDifferentDefaultArgument,
9661     FunctionTemplateParameterDifferentType,
9662     FunctionTemplatePackParameter,
9663   };
9664 
9665   // These lambdas have the common portions of the ODR diagnostics.  This
9666   // has the same return as Diag(), so addition parameters can be passed
9667   // in with operator<<
9668   auto ODRDiagDeclError = [this](NamedDecl *FirstRecord, StringRef FirstModule,
9669                                  SourceLocation Loc, SourceRange Range,
9670                                  ODRMismatchDeclDifference DiffType) {
9671     return Diag(Loc, diag::err_module_odr_violation_mismatch_decl_diff)
9672            << FirstRecord << FirstModule.empty() << FirstModule << Range
9673            << DiffType;
9674   };
9675   auto ODRDiagDeclNote = [this](StringRef SecondModule, SourceLocation Loc,
9676                                 SourceRange Range, ODRMismatchDeclDifference DiffType) {
9677     return Diag(Loc, diag::note_module_odr_violation_mismatch_decl_diff)
9678            << SecondModule << Range << DiffType;
9679   };
9680 
9681   auto ODRDiagField = [this, &ODRDiagDeclError, &ODRDiagDeclNote,
9682                        &ComputeQualTypeODRHash, &ComputeODRHash](
9683                           NamedDecl *FirstRecord, StringRef FirstModule,
9684                           StringRef SecondModule, FieldDecl *FirstField,
9685                           FieldDecl *SecondField) {
9686     IdentifierInfo *FirstII = FirstField->getIdentifier();
9687     IdentifierInfo *SecondII = SecondField->getIdentifier();
9688     if (FirstII->getName() != SecondII->getName()) {
9689       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9690                        FirstField->getSourceRange(), FieldName)
9691           << FirstII;
9692       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9693                       SecondField->getSourceRange(), FieldName)
9694           << SecondII;
9695 
9696       return true;
9697     }
9698 
9699     assert(getContext().hasSameType(FirstField->getType(),
9700                                     SecondField->getType()));
9701 
9702     QualType FirstType = FirstField->getType();
9703     QualType SecondType = SecondField->getType();
9704     if (ComputeQualTypeODRHash(FirstType) !=
9705         ComputeQualTypeODRHash(SecondType)) {
9706       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9707                        FirstField->getSourceRange(), FieldTypeName)
9708           << FirstII << FirstType;
9709       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9710                       SecondField->getSourceRange(), FieldTypeName)
9711           << SecondII << SecondType;
9712 
9713       return true;
9714     }
9715 
9716     const bool IsFirstBitField = FirstField->isBitField();
9717     const bool IsSecondBitField = SecondField->isBitField();
9718     if (IsFirstBitField != IsSecondBitField) {
9719       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9720                        FirstField->getSourceRange(), FieldSingleBitField)
9721           << FirstII << IsFirstBitField;
9722       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9723                       SecondField->getSourceRange(), FieldSingleBitField)
9724           << SecondII << IsSecondBitField;
9725       return true;
9726     }
9727 
9728     if (IsFirstBitField && IsSecondBitField) {
9729       unsigned FirstBitWidthHash =
9730           ComputeODRHash(FirstField->getBitWidth());
9731       unsigned SecondBitWidthHash =
9732           ComputeODRHash(SecondField->getBitWidth());
9733       if (FirstBitWidthHash != SecondBitWidthHash) {
9734         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9735                          FirstField->getSourceRange(),
9736                          FieldDifferentWidthBitField)
9737             << FirstII << FirstField->getBitWidth()->getSourceRange();
9738         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9739                         SecondField->getSourceRange(),
9740                         FieldDifferentWidthBitField)
9741             << SecondII << SecondField->getBitWidth()->getSourceRange();
9742         return true;
9743       }
9744     }
9745 
9746     if (!PP.getLangOpts().CPlusPlus)
9747       return false;
9748 
9749     const bool IsFirstMutable = FirstField->isMutable();
9750     const bool IsSecondMutable = SecondField->isMutable();
9751     if (IsFirstMutable != IsSecondMutable) {
9752       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9753                        FirstField->getSourceRange(), FieldSingleMutable)
9754           << FirstII << IsFirstMutable;
9755       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9756                       SecondField->getSourceRange(), FieldSingleMutable)
9757           << SecondII << IsSecondMutable;
9758       return true;
9759     }
9760 
9761     const Expr *FirstInitializer = FirstField->getInClassInitializer();
9762     const Expr *SecondInitializer = SecondField->getInClassInitializer();
9763     if ((!FirstInitializer && SecondInitializer) ||
9764         (FirstInitializer && !SecondInitializer)) {
9765       ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9766                        FirstField->getSourceRange(), FieldSingleInitializer)
9767           << FirstII << (FirstInitializer != nullptr);
9768       ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9769                       SecondField->getSourceRange(), FieldSingleInitializer)
9770           << SecondII << (SecondInitializer != nullptr);
9771       return true;
9772     }
9773 
9774     if (FirstInitializer && SecondInitializer) {
9775       unsigned FirstInitHash = ComputeODRHash(FirstInitializer);
9776       unsigned SecondInitHash = ComputeODRHash(SecondInitializer);
9777       if (FirstInitHash != SecondInitHash) {
9778         ODRDiagDeclError(FirstRecord, FirstModule, FirstField->getLocation(),
9779                          FirstField->getSourceRange(),
9780                          FieldDifferentInitializers)
9781             << FirstII << FirstInitializer->getSourceRange();
9782         ODRDiagDeclNote(SecondModule, SecondField->getLocation(),
9783                         SecondField->getSourceRange(),
9784                         FieldDifferentInitializers)
9785             << SecondII << SecondInitializer->getSourceRange();
9786         return true;
9787       }
9788     }
9789 
9790     return false;
9791   };
9792 
9793   auto ODRDiagTypeDefOrAlias =
9794       [&ODRDiagDeclError, &ODRDiagDeclNote, &ComputeQualTypeODRHash](
9795           NamedDecl *FirstRecord, StringRef FirstModule, StringRef SecondModule,
9796           TypedefNameDecl *FirstTD, TypedefNameDecl *SecondTD,
9797           bool IsTypeAlias) {
9798         auto FirstName = FirstTD->getDeclName();
9799         auto SecondName = SecondTD->getDeclName();
9800         if (FirstName != SecondName) {
9801           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9802                            FirstTD->getSourceRange(), TypedefName)
9803               << IsTypeAlias << FirstName;
9804           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9805                           SecondTD->getSourceRange(), TypedefName)
9806               << IsTypeAlias << SecondName;
9807           return true;
9808         }
9809 
9810         QualType FirstType = FirstTD->getUnderlyingType();
9811         QualType SecondType = SecondTD->getUnderlyingType();
9812         if (ComputeQualTypeODRHash(FirstType) !=
9813             ComputeQualTypeODRHash(SecondType)) {
9814           ODRDiagDeclError(FirstRecord, FirstModule, FirstTD->getLocation(),
9815                            FirstTD->getSourceRange(), TypedefType)
9816               << IsTypeAlias << FirstName << FirstType;
9817           ODRDiagDeclNote(SecondModule, SecondTD->getLocation(),
9818                           SecondTD->getSourceRange(), TypedefType)
9819               << IsTypeAlias << SecondName << SecondType;
9820           return true;
9821         }
9822 
9823         return false;
9824   };
9825 
9826   auto ODRDiagVar = [&ODRDiagDeclError, &ODRDiagDeclNote,
9827                      &ComputeQualTypeODRHash, &ComputeODRHash,
9828                      this](NamedDecl *FirstRecord, StringRef FirstModule,
9829                            StringRef SecondModule, VarDecl *FirstVD,
9830                            VarDecl *SecondVD) {
9831     auto FirstName = FirstVD->getDeclName();
9832     auto SecondName = SecondVD->getDeclName();
9833     if (FirstName != SecondName) {
9834       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9835                        FirstVD->getSourceRange(), VarName)
9836           << FirstName;
9837       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9838                       SecondVD->getSourceRange(), VarName)
9839           << SecondName;
9840       return true;
9841     }
9842 
9843     QualType FirstType = FirstVD->getType();
9844     QualType SecondType = SecondVD->getType();
9845     if (ComputeQualTypeODRHash(FirstType) !=
9846         ComputeQualTypeODRHash(SecondType)) {
9847       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9848                        FirstVD->getSourceRange(), VarType)
9849           << FirstName << FirstType;
9850       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9851                       SecondVD->getSourceRange(), VarType)
9852           << SecondName << SecondType;
9853       return true;
9854     }
9855 
9856     if (!PP.getLangOpts().CPlusPlus)
9857       return false;
9858 
9859     const Expr *FirstInit = FirstVD->getInit();
9860     const Expr *SecondInit = SecondVD->getInit();
9861     if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
9862       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9863                        FirstVD->getSourceRange(), VarSingleInitializer)
9864           << FirstName << (FirstInit == nullptr)
9865           << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
9866       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9867                       SecondVD->getSourceRange(), VarSingleInitializer)
9868           << SecondName << (SecondInit == nullptr)
9869           << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
9870       return true;
9871     }
9872 
9873     if (FirstInit && SecondInit &&
9874         ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
9875       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9876                        FirstVD->getSourceRange(), VarDifferentInitializer)
9877           << FirstName << FirstInit->getSourceRange();
9878       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9879                       SecondVD->getSourceRange(), VarDifferentInitializer)
9880           << SecondName << SecondInit->getSourceRange();
9881       return true;
9882     }
9883 
9884     const bool FirstIsConstexpr = FirstVD->isConstexpr();
9885     const bool SecondIsConstexpr = SecondVD->isConstexpr();
9886     if (FirstIsConstexpr != SecondIsConstexpr) {
9887       ODRDiagDeclError(FirstRecord, FirstModule, FirstVD->getLocation(),
9888                        FirstVD->getSourceRange(), VarConstexpr)
9889           << FirstName << FirstIsConstexpr;
9890       ODRDiagDeclNote(SecondModule, SecondVD->getLocation(),
9891                       SecondVD->getSourceRange(), VarConstexpr)
9892           << SecondName << SecondIsConstexpr;
9893       return true;
9894     }
9895     return false;
9896   };
9897 
9898   auto DifferenceSelector = [](Decl *D) {
9899     assert(D && "valid Decl required");
9900     switch (D->getKind()) {
9901     default:
9902       return Other;
9903     case Decl::AccessSpec:
9904       switch (D->getAccess()) {
9905       case AS_public:
9906         return PublicSpecifer;
9907       case AS_private:
9908         return PrivateSpecifer;
9909       case AS_protected:
9910         return ProtectedSpecifer;
9911       case AS_none:
9912         break;
9913       }
9914       llvm_unreachable("Invalid access specifier");
9915     case Decl::StaticAssert:
9916       return StaticAssert;
9917     case Decl::Field:
9918       return Field;
9919     case Decl::CXXMethod:
9920     case Decl::CXXConstructor:
9921     case Decl::CXXDestructor:
9922       return CXXMethod;
9923     case Decl::TypeAlias:
9924       return TypeAlias;
9925     case Decl::Typedef:
9926       return TypeDef;
9927     case Decl::Var:
9928       return Var;
9929     case Decl::Friend:
9930       return Friend;
9931     case Decl::FunctionTemplate:
9932       return FunctionTemplate;
9933     }
9934   };
9935 
9936   using DeclHashes = llvm::SmallVector<std::pair<Decl *, unsigned>, 4>;
9937   auto PopulateHashes = [&ComputeSubDeclODRHash](DeclHashes &Hashes,
9938                                                  RecordDecl *Record,
9939                                                  const DeclContext *DC) {
9940     for (auto *D : Record->decls()) {
9941       if (!ODRHash::isDeclToBeProcessed(D, DC))
9942         continue;
9943       Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
9944     }
9945   };
9946 
9947   struct DiffResult {
9948     Decl *FirstDecl = nullptr, *SecondDecl = nullptr;
9949     ODRMismatchDecl FirstDiffType = Other, SecondDiffType = Other;
9950   };
9951 
9952   // If there is a diagnoseable difference, FirstDiffType and
9953   // SecondDiffType will not be Other and FirstDecl and SecondDecl will be
9954   // filled in if not EndOfClass.
9955   auto FindTypeDiffs = [&DifferenceSelector](DeclHashes &FirstHashes,
9956                                              DeclHashes &SecondHashes) {
9957     DiffResult DR;
9958     auto FirstIt = FirstHashes.begin();
9959     auto SecondIt = SecondHashes.begin();
9960     while (FirstIt != FirstHashes.end() || SecondIt != SecondHashes.end()) {
9961       if (FirstIt != FirstHashes.end() && SecondIt != SecondHashes.end() &&
9962           FirstIt->second == SecondIt->second) {
9963         ++FirstIt;
9964         ++SecondIt;
9965         continue;
9966       }
9967 
9968       DR.FirstDecl = FirstIt == FirstHashes.end() ? nullptr : FirstIt->first;
9969       DR.SecondDecl =
9970           SecondIt == SecondHashes.end() ? nullptr : SecondIt->first;
9971 
9972       DR.FirstDiffType =
9973           DR.FirstDecl ? DifferenceSelector(DR.FirstDecl) : EndOfClass;
9974       DR.SecondDiffType =
9975           DR.SecondDecl ? DifferenceSelector(DR.SecondDecl) : EndOfClass;
9976       return DR;
9977     }
9978     return DR;
9979   };
9980 
9981   // Use this to diagnose that an unexpected Decl was encountered
9982   // or no difference was detected. This causes a generic error
9983   // message to be emitted.
9984   auto DiagnoseODRUnexpected = [this](DiffResult &DR, NamedDecl *FirstRecord,
9985                                       StringRef FirstModule,
9986                                       NamedDecl *SecondRecord,
9987                                       StringRef SecondModule) {
9988     Diag(FirstRecord->getLocation(),
9989          diag::err_module_odr_violation_different_definitions)
9990         << FirstRecord << FirstModule.empty() << FirstModule;
9991 
9992     if (DR.FirstDecl) {
9993       Diag(DR.FirstDecl->getLocation(), diag::note_first_module_difference)
9994           << FirstRecord << DR.FirstDecl->getSourceRange();
9995     }
9996 
9997     Diag(SecondRecord->getLocation(),
9998          diag::note_module_odr_violation_different_definitions)
9999         << SecondModule;
10000 
10001     if (DR.SecondDecl) {
10002       Diag(DR.SecondDecl->getLocation(), diag::note_second_module_difference)
10003           << DR.SecondDecl->getSourceRange();
10004     }
10005   };
10006 
10007   auto DiagnoseODRMismatch =
10008       [this](DiffResult &DR, NamedDecl *FirstRecord, StringRef FirstModule,
10009              NamedDecl *SecondRecord, StringRef SecondModule) {
10010         SourceLocation FirstLoc;
10011         SourceRange FirstRange;
10012         auto *FirstTag = dyn_cast<TagDecl>(FirstRecord);
10013         if (DR.FirstDiffType == EndOfClass && FirstTag) {
10014           FirstLoc = FirstTag->getBraceRange().getEnd();
10015         } else {
10016           FirstLoc = DR.FirstDecl->getLocation();
10017           FirstRange = DR.FirstDecl->getSourceRange();
10018         }
10019         Diag(FirstLoc, diag::err_module_odr_violation_mismatch_decl)
10020             << FirstRecord << FirstModule.empty() << FirstModule << FirstRange
10021             << DR.FirstDiffType;
10022 
10023         SourceLocation SecondLoc;
10024         SourceRange SecondRange;
10025         auto *SecondTag = dyn_cast<TagDecl>(SecondRecord);
10026         if (DR.SecondDiffType == EndOfClass && SecondTag) {
10027           SecondLoc = SecondTag->getBraceRange().getEnd();
10028         } else {
10029           SecondLoc = DR.SecondDecl->getLocation();
10030           SecondRange = DR.SecondDecl->getSourceRange();
10031         }
10032         Diag(SecondLoc, diag::note_module_odr_violation_mismatch_decl)
10033             << SecondModule << SecondRange << DR.SecondDiffType;
10034       };
10035 
10036   // Issue any pending ODR-failure diagnostics.
10037   for (auto &Merge : OdrMergeFailures) {
10038     // If we've already pointed out a specific problem with this class, don't
10039     // bother issuing a general "something's different" diagnostic.
10040     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
10041       continue;
10042 
10043     bool Diagnosed = false;
10044     CXXRecordDecl *FirstRecord = Merge.first;
10045     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstRecord);
10046     for (auto &RecordPair : Merge.second) {
10047       CXXRecordDecl *SecondRecord = RecordPair.first;
10048       // Multiple different declarations got merged together; tell the user
10049       // where they came from.
10050       if (FirstRecord == SecondRecord)
10051         continue;
10052 
10053       std::string SecondModule = getOwningModuleNameForDiagnostic(SecondRecord);
10054 
10055       auto *FirstDD = FirstRecord->DefinitionData;
10056       auto *SecondDD = RecordPair.second;
10057 
10058       assert(FirstDD && SecondDD && "Definitions without DefinitionData");
10059 
10060       // Diagnostics from DefinitionData are emitted here.
10061       if (FirstDD != SecondDD) {
10062         enum ODRDefinitionDataDifference {
10063           NumBases,
10064           NumVBases,
10065           BaseType,
10066           BaseVirtual,
10067           BaseAccess,
10068         };
10069         auto ODRDiagBaseError = [FirstRecord, &FirstModule,
10070                                  this](SourceLocation Loc, SourceRange Range,
10071                                        ODRDefinitionDataDifference DiffType) {
10072           return Diag(Loc, diag::err_module_odr_violation_definition_data)
10073                  << FirstRecord << FirstModule.empty() << FirstModule << Range
10074                  << DiffType;
10075         };
10076         auto ODRDiagBaseNote = [&SecondModule,
10077                                 this](SourceLocation Loc, SourceRange Range,
10078                                       ODRDefinitionDataDifference DiffType) {
10079           return Diag(Loc, diag::note_module_odr_violation_definition_data)
10080                  << SecondModule << Range << DiffType;
10081         };
10082 
10083         unsigned FirstNumBases = FirstDD->NumBases;
10084         unsigned FirstNumVBases = FirstDD->NumVBases;
10085         unsigned SecondNumBases = SecondDD->NumBases;
10086         unsigned SecondNumVBases = SecondDD->NumVBases;
10087 
10088         auto GetSourceRange = [](struct CXXRecordDecl::DefinitionData *DD) {
10089           unsigned NumBases = DD->NumBases;
10090           if (NumBases == 0) return SourceRange();
10091           auto bases = DD->bases();
10092           return SourceRange(bases[0].getBeginLoc(),
10093                              bases[NumBases - 1].getEndLoc());
10094         };
10095 
10096         if (FirstNumBases != SecondNumBases) {
10097           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10098                            NumBases)
10099               << FirstNumBases;
10100           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10101                           NumBases)
10102               << SecondNumBases;
10103           Diagnosed = true;
10104           break;
10105         }
10106 
10107         if (FirstNumVBases != SecondNumVBases) {
10108           ODRDiagBaseError(FirstRecord->getLocation(), GetSourceRange(FirstDD),
10109                            NumVBases)
10110               << FirstNumVBases;
10111           ODRDiagBaseNote(SecondRecord->getLocation(), GetSourceRange(SecondDD),
10112                           NumVBases)
10113               << SecondNumVBases;
10114           Diagnosed = true;
10115           break;
10116         }
10117 
10118         auto FirstBases = FirstDD->bases();
10119         auto SecondBases = SecondDD->bases();
10120         unsigned i = 0;
10121         for (i = 0; i < FirstNumBases; ++i) {
10122           auto FirstBase = FirstBases[i];
10123           auto SecondBase = SecondBases[i];
10124           if (ComputeQualTypeODRHash(FirstBase.getType()) !=
10125               ComputeQualTypeODRHash(SecondBase.getType())) {
10126             ODRDiagBaseError(FirstRecord->getLocation(),
10127                              FirstBase.getSourceRange(), BaseType)
10128                 << (i + 1) << FirstBase.getType();
10129             ODRDiagBaseNote(SecondRecord->getLocation(),
10130                             SecondBase.getSourceRange(), BaseType)
10131                 << (i + 1) << SecondBase.getType();
10132             break;
10133           }
10134 
10135           if (FirstBase.isVirtual() != SecondBase.isVirtual()) {
10136             ODRDiagBaseError(FirstRecord->getLocation(),
10137                              FirstBase.getSourceRange(), BaseVirtual)
10138                 << (i + 1) << FirstBase.isVirtual() << FirstBase.getType();
10139             ODRDiagBaseNote(SecondRecord->getLocation(),
10140                             SecondBase.getSourceRange(), BaseVirtual)
10141                 << (i + 1) << SecondBase.isVirtual() << SecondBase.getType();
10142             break;
10143           }
10144 
10145           if (FirstBase.getAccessSpecifierAsWritten() !=
10146               SecondBase.getAccessSpecifierAsWritten()) {
10147             ODRDiagBaseError(FirstRecord->getLocation(),
10148                              FirstBase.getSourceRange(), BaseAccess)
10149                 << (i + 1) << FirstBase.getType()
10150                 << (int)FirstBase.getAccessSpecifierAsWritten();
10151             ODRDiagBaseNote(SecondRecord->getLocation(),
10152                             SecondBase.getSourceRange(), BaseAccess)
10153                 << (i + 1) << SecondBase.getType()
10154                 << (int)SecondBase.getAccessSpecifierAsWritten();
10155             break;
10156           }
10157         }
10158 
10159         if (i != FirstNumBases) {
10160           Diagnosed = true;
10161           break;
10162         }
10163       }
10164 
10165       const ClassTemplateDecl *FirstTemplate =
10166           FirstRecord->getDescribedClassTemplate();
10167       const ClassTemplateDecl *SecondTemplate =
10168           SecondRecord->getDescribedClassTemplate();
10169 
10170       assert(!FirstTemplate == !SecondTemplate &&
10171              "Both pointers should be null or non-null");
10172 
10173       enum ODRTemplateDifference {
10174         ParamEmptyName,
10175         ParamName,
10176         ParamSingleDefaultArgument,
10177         ParamDifferentDefaultArgument,
10178       };
10179 
10180       if (FirstTemplate && SecondTemplate) {
10181         DeclHashes FirstTemplateHashes;
10182         DeclHashes SecondTemplateHashes;
10183 
10184         auto PopulateTemplateParameterHashs =
10185             [&ComputeSubDeclODRHash](DeclHashes &Hashes,
10186                                      const ClassTemplateDecl *TD) {
10187               for (auto *D : TD->getTemplateParameters()->asArray()) {
10188                 Hashes.emplace_back(D, ComputeSubDeclODRHash(D));
10189               }
10190             };
10191 
10192         PopulateTemplateParameterHashs(FirstTemplateHashes, FirstTemplate);
10193         PopulateTemplateParameterHashs(SecondTemplateHashes, SecondTemplate);
10194 
10195         assert(FirstTemplateHashes.size() == SecondTemplateHashes.size() &&
10196                "Number of template parameters should be equal.");
10197 
10198         auto FirstIt = FirstTemplateHashes.begin();
10199         auto FirstEnd = FirstTemplateHashes.end();
10200         auto SecondIt = SecondTemplateHashes.begin();
10201         for (; FirstIt != FirstEnd; ++FirstIt, ++SecondIt) {
10202           if (FirstIt->second == SecondIt->second)
10203             continue;
10204 
10205           auto ODRDiagTemplateError = [FirstRecord, &FirstModule, this](
10206                                           SourceLocation Loc, SourceRange Range,
10207                                           ODRTemplateDifference DiffType) {
10208             return Diag(Loc, diag::err_module_odr_violation_template_parameter)
10209                    << FirstRecord << FirstModule.empty() << FirstModule << Range
10210                    << DiffType;
10211           };
10212           auto ODRDiagTemplateNote = [&SecondModule, this](
10213                                          SourceLocation Loc, SourceRange Range,
10214                                          ODRTemplateDifference DiffType) {
10215             return Diag(Loc, diag::note_module_odr_violation_template_parameter)
10216                    << SecondModule << Range << DiffType;
10217           };
10218 
10219           const NamedDecl* FirstDecl = cast<NamedDecl>(FirstIt->first);
10220           const NamedDecl* SecondDecl = cast<NamedDecl>(SecondIt->first);
10221 
10222           assert(FirstDecl->getKind() == SecondDecl->getKind() &&
10223                  "Parameter Decl's should be the same kind.");
10224 
10225           DeclarationName FirstName = FirstDecl->getDeclName();
10226           DeclarationName SecondName = SecondDecl->getDeclName();
10227 
10228           if (FirstName != SecondName) {
10229             const bool FirstNameEmpty =
10230                 FirstName.isIdentifier() && !FirstName.getAsIdentifierInfo();
10231             const bool SecondNameEmpty =
10232                 SecondName.isIdentifier() && !SecondName.getAsIdentifierInfo();
10233             assert((!FirstNameEmpty || !SecondNameEmpty) &&
10234                    "Both template parameters cannot be unnamed.");
10235             ODRDiagTemplateError(FirstDecl->getLocation(),
10236                                  FirstDecl->getSourceRange(),
10237                                  FirstNameEmpty ? ParamEmptyName : ParamName)
10238                 << FirstName;
10239             ODRDiagTemplateNote(SecondDecl->getLocation(),
10240                                 SecondDecl->getSourceRange(),
10241                                 SecondNameEmpty ? ParamEmptyName : ParamName)
10242                 << SecondName;
10243             break;
10244           }
10245 
10246           switch (FirstDecl->getKind()) {
10247           default:
10248             llvm_unreachable("Invalid template parameter type.");
10249           case Decl::TemplateTypeParm: {
10250             const auto *FirstParam = cast<TemplateTypeParmDecl>(FirstDecl);
10251             const auto *SecondParam = cast<TemplateTypeParmDecl>(SecondDecl);
10252             const bool HasFirstDefaultArgument =
10253                 FirstParam->hasDefaultArgument() &&
10254                 !FirstParam->defaultArgumentWasInherited();
10255             const bool HasSecondDefaultArgument =
10256                 SecondParam->hasDefaultArgument() &&
10257                 !SecondParam->defaultArgumentWasInherited();
10258 
10259             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10260               ODRDiagTemplateError(FirstDecl->getLocation(),
10261                                    FirstDecl->getSourceRange(),
10262                                    ParamSingleDefaultArgument)
10263                   << HasFirstDefaultArgument;
10264               ODRDiagTemplateNote(SecondDecl->getLocation(),
10265                                   SecondDecl->getSourceRange(),
10266                                   ParamSingleDefaultArgument)
10267                   << HasSecondDefaultArgument;
10268               break;
10269             }
10270 
10271             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10272                    "Expecting default arguments.");
10273 
10274             ODRDiagTemplateError(FirstDecl->getLocation(),
10275                                  FirstDecl->getSourceRange(),
10276                                  ParamDifferentDefaultArgument);
10277             ODRDiagTemplateNote(SecondDecl->getLocation(),
10278                                 SecondDecl->getSourceRange(),
10279                                 ParamDifferentDefaultArgument);
10280 
10281             break;
10282           }
10283           case Decl::NonTypeTemplateParm: {
10284             const auto *FirstParam = cast<NonTypeTemplateParmDecl>(FirstDecl);
10285             const auto *SecondParam = cast<NonTypeTemplateParmDecl>(SecondDecl);
10286             const bool HasFirstDefaultArgument =
10287                 FirstParam->hasDefaultArgument() &&
10288                 !FirstParam->defaultArgumentWasInherited();
10289             const bool HasSecondDefaultArgument =
10290                 SecondParam->hasDefaultArgument() &&
10291                 !SecondParam->defaultArgumentWasInherited();
10292 
10293             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10294               ODRDiagTemplateError(FirstDecl->getLocation(),
10295                                    FirstDecl->getSourceRange(),
10296                                    ParamSingleDefaultArgument)
10297                   << HasFirstDefaultArgument;
10298               ODRDiagTemplateNote(SecondDecl->getLocation(),
10299                                   SecondDecl->getSourceRange(),
10300                                   ParamSingleDefaultArgument)
10301                   << HasSecondDefaultArgument;
10302               break;
10303             }
10304 
10305             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10306                    "Expecting default arguments.");
10307 
10308             ODRDiagTemplateError(FirstDecl->getLocation(),
10309                                  FirstDecl->getSourceRange(),
10310                                  ParamDifferentDefaultArgument);
10311             ODRDiagTemplateNote(SecondDecl->getLocation(),
10312                                 SecondDecl->getSourceRange(),
10313                                 ParamDifferentDefaultArgument);
10314 
10315             break;
10316           }
10317           case Decl::TemplateTemplateParm: {
10318             const auto *FirstParam = cast<TemplateTemplateParmDecl>(FirstDecl);
10319             const auto *SecondParam =
10320                 cast<TemplateTemplateParmDecl>(SecondDecl);
10321             const bool HasFirstDefaultArgument =
10322                 FirstParam->hasDefaultArgument() &&
10323                 !FirstParam->defaultArgumentWasInherited();
10324             const bool HasSecondDefaultArgument =
10325                 SecondParam->hasDefaultArgument() &&
10326                 !SecondParam->defaultArgumentWasInherited();
10327 
10328             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10329               ODRDiagTemplateError(FirstDecl->getLocation(),
10330                                    FirstDecl->getSourceRange(),
10331                                    ParamSingleDefaultArgument)
10332                   << HasFirstDefaultArgument;
10333               ODRDiagTemplateNote(SecondDecl->getLocation(),
10334                                   SecondDecl->getSourceRange(),
10335                                   ParamSingleDefaultArgument)
10336                   << HasSecondDefaultArgument;
10337               break;
10338             }
10339 
10340             assert(HasFirstDefaultArgument && HasSecondDefaultArgument &&
10341                    "Expecting default arguments.");
10342 
10343             ODRDiagTemplateError(FirstDecl->getLocation(),
10344                                  FirstDecl->getSourceRange(),
10345                                  ParamDifferentDefaultArgument);
10346             ODRDiagTemplateNote(SecondDecl->getLocation(),
10347                                 SecondDecl->getSourceRange(),
10348                                 ParamDifferentDefaultArgument);
10349 
10350             break;
10351           }
10352           }
10353 
10354           break;
10355         }
10356 
10357         if (FirstIt != FirstEnd) {
10358           Diagnosed = true;
10359           break;
10360         }
10361       }
10362 
10363       DeclHashes FirstHashes;
10364       DeclHashes SecondHashes;
10365       const DeclContext *DC = FirstRecord;
10366       PopulateHashes(FirstHashes, FirstRecord, DC);
10367       PopulateHashes(SecondHashes, SecondRecord, DC);
10368 
10369       auto DR = FindTypeDiffs(FirstHashes, SecondHashes);
10370       ODRMismatchDecl FirstDiffType = DR.FirstDiffType;
10371       ODRMismatchDecl SecondDiffType = DR.SecondDiffType;
10372       Decl *FirstDecl = DR.FirstDecl;
10373       Decl *SecondDecl = DR.SecondDecl;
10374 
10375       if (FirstDiffType == Other || SecondDiffType == Other) {
10376         DiagnoseODRUnexpected(DR, FirstRecord, FirstModule, SecondRecord,
10377                               SecondModule);
10378         Diagnosed = true;
10379         break;
10380       }
10381 
10382       if (FirstDiffType != SecondDiffType) {
10383         DiagnoseODRMismatch(DR, FirstRecord, FirstModule, SecondRecord,
10384                             SecondModule);
10385         Diagnosed = true;
10386         break;
10387       }
10388 
10389       assert(FirstDiffType == SecondDiffType);
10390 
10391       switch (FirstDiffType) {
10392       case Other:
10393       case EndOfClass:
10394       case PublicSpecifer:
10395       case PrivateSpecifer:
10396       case ProtectedSpecifer:
10397         llvm_unreachable("Invalid diff type");
10398 
10399       case StaticAssert: {
10400         StaticAssertDecl *FirstSA = cast<StaticAssertDecl>(FirstDecl);
10401         StaticAssertDecl *SecondSA = cast<StaticAssertDecl>(SecondDecl);
10402 
10403         Expr *FirstExpr = FirstSA->getAssertExpr();
10404         Expr *SecondExpr = SecondSA->getAssertExpr();
10405         unsigned FirstODRHash = ComputeODRHash(FirstExpr);
10406         unsigned SecondODRHash = ComputeODRHash(SecondExpr);
10407         if (FirstODRHash != SecondODRHash) {
10408           ODRDiagDeclError(FirstRecord, FirstModule, FirstExpr->getBeginLoc(),
10409                            FirstExpr->getSourceRange(), StaticAssertCondition);
10410           ODRDiagDeclNote(SecondModule, SecondExpr->getBeginLoc(),
10411                           SecondExpr->getSourceRange(), StaticAssertCondition);
10412           Diagnosed = true;
10413           break;
10414         }
10415 
10416         StringLiteral *FirstStr = FirstSA->getMessage();
10417         StringLiteral *SecondStr = SecondSA->getMessage();
10418         assert((FirstStr || SecondStr) && "Both messages cannot be empty");
10419         if ((FirstStr && !SecondStr) || (!FirstStr && SecondStr)) {
10420           SourceLocation FirstLoc, SecondLoc;
10421           SourceRange FirstRange, SecondRange;
10422           if (FirstStr) {
10423             FirstLoc = FirstStr->getBeginLoc();
10424             FirstRange = FirstStr->getSourceRange();
10425           } else {
10426             FirstLoc = FirstSA->getBeginLoc();
10427             FirstRange = FirstSA->getSourceRange();
10428           }
10429           if (SecondStr) {
10430             SecondLoc = SecondStr->getBeginLoc();
10431             SecondRange = SecondStr->getSourceRange();
10432           } else {
10433             SecondLoc = SecondSA->getBeginLoc();
10434             SecondRange = SecondSA->getSourceRange();
10435           }
10436           ODRDiagDeclError(FirstRecord, FirstModule, FirstLoc, FirstRange,
10437                            StaticAssertOnlyMessage)
10438               << (FirstStr == nullptr);
10439           ODRDiagDeclNote(SecondModule, SecondLoc, SecondRange,
10440                           StaticAssertOnlyMessage)
10441               << (SecondStr == nullptr);
10442           Diagnosed = true;
10443           break;
10444         }
10445 
10446         if (FirstStr && SecondStr &&
10447             FirstStr->getString() != SecondStr->getString()) {
10448           ODRDiagDeclError(FirstRecord, FirstModule, FirstStr->getBeginLoc(),
10449                            FirstStr->getSourceRange(), StaticAssertMessage);
10450           ODRDiagDeclNote(SecondModule, SecondStr->getBeginLoc(),
10451                           SecondStr->getSourceRange(), StaticAssertMessage);
10452           Diagnosed = true;
10453           break;
10454         }
10455         break;
10456       }
10457       case Field: {
10458         Diagnosed = ODRDiagField(FirstRecord, FirstModule, SecondModule,
10459                                  cast<FieldDecl>(FirstDecl),
10460                                  cast<FieldDecl>(SecondDecl));
10461         break;
10462       }
10463       case CXXMethod: {
10464         enum {
10465           DiagMethod,
10466           DiagConstructor,
10467           DiagDestructor,
10468         } FirstMethodType,
10469             SecondMethodType;
10470         auto GetMethodTypeForDiagnostics = [](const CXXMethodDecl* D) {
10471           if (isa<CXXConstructorDecl>(D)) return DiagConstructor;
10472           if (isa<CXXDestructorDecl>(D)) return DiagDestructor;
10473           return DiagMethod;
10474         };
10475         const CXXMethodDecl *FirstMethod = cast<CXXMethodDecl>(FirstDecl);
10476         const CXXMethodDecl *SecondMethod = cast<CXXMethodDecl>(SecondDecl);
10477         FirstMethodType = GetMethodTypeForDiagnostics(FirstMethod);
10478         SecondMethodType = GetMethodTypeForDiagnostics(SecondMethod);
10479         auto FirstName = FirstMethod->getDeclName();
10480         auto SecondName = SecondMethod->getDeclName();
10481         if (FirstMethodType != SecondMethodType || FirstName != SecondName) {
10482           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10483                            FirstMethod->getSourceRange(), MethodName)
10484               << FirstMethodType << FirstName;
10485           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10486                           SecondMethod->getSourceRange(), MethodName)
10487               << SecondMethodType << SecondName;
10488 
10489           Diagnosed = true;
10490           break;
10491         }
10492 
10493         const bool FirstDeleted = FirstMethod->isDeletedAsWritten();
10494         const bool SecondDeleted = SecondMethod->isDeletedAsWritten();
10495         if (FirstDeleted != SecondDeleted) {
10496           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10497                            FirstMethod->getSourceRange(), MethodDeleted)
10498               << FirstMethodType << FirstName << FirstDeleted;
10499 
10500           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10501                           SecondMethod->getSourceRange(), MethodDeleted)
10502               << SecondMethodType << SecondName << SecondDeleted;
10503           Diagnosed = true;
10504           break;
10505         }
10506 
10507         const bool FirstDefaulted = FirstMethod->isExplicitlyDefaulted();
10508         const bool SecondDefaulted = SecondMethod->isExplicitlyDefaulted();
10509         if (FirstDefaulted != SecondDefaulted) {
10510           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10511                            FirstMethod->getSourceRange(), MethodDefaulted)
10512               << FirstMethodType << FirstName << FirstDefaulted;
10513 
10514           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10515                           SecondMethod->getSourceRange(), MethodDefaulted)
10516               << SecondMethodType << SecondName << SecondDefaulted;
10517           Diagnosed = true;
10518           break;
10519         }
10520 
10521         const bool FirstVirtual = FirstMethod->isVirtualAsWritten();
10522         const bool SecondVirtual = SecondMethod->isVirtualAsWritten();
10523         const bool FirstPure = FirstMethod->isPure();
10524         const bool SecondPure = SecondMethod->isPure();
10525         if ((FirstVirtual || SecondVirtual) &&
10526             (FirstVirtual != SecondVirtual || FirstPure != SecondPure)) {
10527           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10528                            FirstMethod->getSourceRange(), MethodVirtual)
10529               << FirstMethodType << FirstName << FirstPure << FirstVirtual;
10530           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10531                           SecondMethod->getSourceRange(), MethodVirtual)
10532               << SecondMethodType << SecondName << SecondPure << SecondVirtual;
10533           Diagnosed = true;
10534           break;
10535         }
10536 
10537         // CXXMethodDecl::isStatic uses the canonical Decl.  With Decl merging,
10538         // FirstDecl is the canonical Decl of SecondDecl, so the storage
10539         // class needs to be checked instead.
10540         const auto FirstStorage = FirstMethod->getStorageClass();
10541         const auto SecondStorage = SecondMethod->getStorageClass();
10542         const bool FirstStatic = FirstStorage == SC_Static;
10543         const bool SecondStatic = SecondStorage == SC_Static;
10544         if (FirstStatic != SecondStatic) {
10545           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10546                            FirstMethod->getSourceRange(), MethodStatic)
10547               << FirstMethodType << FirstName << FirstStatic;
10548           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10549                           SecondMethod->getSourceRange(), MethodStatic)
10550               << SecondMethodType << SecondName << SecondStatic;
10551           Diagnosed = true;
10552           break;
10553         }
10554 
10555         const bool FirstVolatile = FirstMethod->isVolatile();
10556         const bool SecondVolatile = SecondMethod->isVolatile();
10557         if (FirstVolatile != SecondVolatile) {
10558           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10559                            FirstMethod->getSourceRange(), MethodVolatile)
10560               << FirstMethodType << FirstName << FirstVolatile;
10561           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10562                           SecondMethod->getSourceRange(), MethodVolatile)
10563               << SecondMethodType << SecondName << SecondVolatile;
10564           Diagnosed = true;
10565           break;
10566         }
10567 
10568         const bool FirstConst = FirstMethod->isConst();
10569         const bool SecondConst = SecondMethod->isConst();
10570         if (FirstConst != SecondConst) {
10571           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10572                            FirstMethod->getSourceRange(), MethodConst)
10573               << FirstMethodType << FirstName << FirstConst;
10574           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10575                           SecondMethod->getSourceRange(), MethodConst)
10576               << SecondMethodType << SecondName << SecondConst;
10577           Diagnosed = true;
10578           break;
10579         }
10580 
10581         const bool FirstInline = FirstMethod->isInlineSpecified();
10582         const bool SecondInline = SecondMethod->isInlineSpecified();
10583         if (FirstInline != SecondInline) {
10584           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10585                            FirstMethod->getSourceRange(), MethodInline)
10586               << FirstMethodType << FirstName << FirstInline;
10587           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10588                           SecondMethod->getSourceRange(), MethodInline)
10589               << SecondMethodType << SecondName << SecondInline;
10590           Diagnosed = true;
10591           break;
10592         }
10593 
10594         const unsigned FirstNumParameters = FirstMethod->param_size();
10595         const unsigned SecondNumParameters = SecondMethod->param_size();
10596         if (FirstNumParameters != SecondNumParameters) {
10597           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10598                            FirstMethod->getSourceRange(),
10599                            MethodNumberParameters)
10600               << FirstMethodType << FirstName << FirstNumParameters;
10601           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10602                           SecondMethod->getSourceRange(),
10603                           MethodNumberParameters)
10604               << SecondMethodType << SecondName << SecondNumParameters;
10605           Diagnosed = true;
10606           break;
10607         }
10608 
10609         // Need this status boolean to know when break out of the switch.
10610         bool ParameterMismatch = false;
10611         for (unsigned I = 0; I < FirstNumParameters; ++I) {
10612           const ParmVarDecl *FirstParam = FirstMethod->getParamDecl(I);
10613           const ParmVarDecl *SecondParam = SecondMethod->getParamDecl(I);
10614 
10615           QualType FirstParamType = FirstParam->getType();
10616           QualType SecondParamType = SecondParam->getType();
10617           if (FirstParamType != SecondParamType &&
10618               ComputeQualTypeODRHash(FirstParamType) !=
10619                   ComputeQualTypeODRHash(SecondParamType)) {
10620             if (const DecayedType *ParamDecayedType =
10621                     FirstParamType->getAs<DecayedType>()) {
10622               ODRDiagDeclError(
10623                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10624                   FirstMethod->getSourceRange(), MethodParameterType)
10625                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10626                   << true << ParamDecayedType->getOriginalType();
10627             } else {
10628               ODRDiagDeclError(
10629                   FirstRecord, FirstModule, FirstMethod->getLocation(),
10630                   FirstMethod->getSourceRange(), MethodParameterType)
10631                   << FirstMethodType << FirstName << (I + 1) << FirstParamType
10632                   << false;
10633             }
10634 
10635             if (const DecayedType *ParamDecayedType =
10636                     SecondParamType->getAs<DecayedType>()) {
10637               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10638                               SecondMethod->getSourceRange(),
10639                               MethodParameterType)
10640                   << SecondMethodType << SecondName << (I + 1)
10641                   << SecondParamType << true
10642                   << ParamDecayedType->getOriginalType();
10643             } else {
10644               ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10645                               SecondMethod->getSourceRange(),
10646                               MethodParameterType)
10647                   << SecondMethodType << SecondName << (I + 1)
10648                   << SecondParamType << false;
10649             }
10650             ParameterMismatch = true;
10651             break;
10652           }
10653 
10654           DeclarationName FirstParamName = FirstParam->getDeclName();
10655           DeclarationName SecondParamName = SecondParam->getDeclName();
10656           if (FirstParamName != SecondParamName) {
10657             ODRDiagDeclError(FirstRecord, FirstModule,
10658                              FirstMethod->getLocation(),
10659                              FirstMethod->getSourceRange(), MethodParameterName)
10660                 << FirstMethodType << FirstName << (I + 1) << FirstParamName;
10661             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10662                             SecondMethod->getSourceRange(), MethodParameterName)
10663                 << SecondMethodType << SecondName << (I + 1) << SecondParamName;
10664             ParameterMismatch = true;
10665             break;
10666           }
10667 
10668           const Expr *FirstInit = FirstParam->getInit();
10669           const Expr *SecondInit = SecondParam->getInit();
10670           if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
10671             ODRDiagDeclError(FirstRecord, FirstModule,
10672                              FirstMethod->getLocation(),
10673                              FirstMethod->getSourceRange(),
10674                              MethodParameterSingleDefaultArgument)
10675                 << FirstMethodType << FirstName << (I + 1)
10676                 << (FirstInit == nullptr)
10677                 << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
10678             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10679                             SecondMethod->getSourceRange(),
10680                             MethodParameterSingleDefaultArgument)
10681                 << SecondMethodType << SecondName << (I + 1)
10682                 << (SecondInit == nullptr)
10683                 << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
10684             ParameterMismatch = true;
10685             break;
10686           }
10687 
10688           if (FirstInit && SecondInit &&
10689               ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
10690             ODRDiagDeclError(FirstRecord, FirstModule,
10691                              FirstMethod->getLocation(),
10692                              FirstMethod->getSourceRange(),
10693                              MethodParameterDifferentDefaultArgument)
10694                 << FirstMethodType << FirstName << (I + 1)
10695                 << FirstInit->getSourceRange();
10696             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10697                             SecondMethod->getSourceRange(),
10698                             MethodParameterDifferentDefaultArgument)
10699                 << SecondMethodType << SecondName << (I + 1)
10700                 << SecondInit->getSourceRange();
10701             ParameterMismatch = true;
10702             break;
10703 
10704           }
10705         }
10706 
10707         if (ParameterMismatch) {
10708           Diagnosed = true;
10709           break;
10710         }
10711 
10712         const auto *FirstTemplateArgs =
10713             FirstMethod->getTemplateSpecializationArgs();
10714         const auto *SecondTemplateArgs =
10715             SecondMethod->getTemplateSpecializationArgs();
10716 
10717         if ((FirstTemplateArgs && !SecondTemplateArgs) ||
10718             (!FirstTemplateArgs && SecondTemplateArgs)) {
10719           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10720                            FirstMethod->getSourceRange(),
10721                            MethodNoTemplateArguments)
10722               << FirstMethodType << FirstName << (FirstTemplateArgs != nullptr);
10723           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10724                           SecondMethod->getSourceRange(),
10725                           MethodNoTemplateArguments)
10726               << SecondMethodType << SecondName
10727               << (SecondTemplateArgs != nullptr);
10728 
10729           Diagnosed = true;
10730           break;
10731         }
10732 
10733         if (FirstTemplateArgs && SecondTemplateArgs) {
10734           // Remove pack expansions from argument list.
10735           auto ExpandTemplateArgumentList =
10736               [](const TemplateArgumentList *TAL) {
10737                 llvm::SmallVector<const TemplateArgument *, 8> ExpandedList;
10738                 for (const TemplateArgument &TA : TAL->asArray()) {
10739                   if (TA.getKind() != TemplateArgument::Pack) {
10740                     ExpandedList.push_back(&TA);
10741                     continue;
10742                   }
10743                   for (const TemplateArgument &PackTA : TA.getPackAsArray()) {
10744                     ExpandedList.push_back(&PackTA);
10745                   }
10746                 }
10747                 return ExpandedList;
10748               };
10749           llvm::SmallVector<const TemplateArgument *, 8> FirstExpandedList =
10750               ExpandTemplateArgumentList(FirstTemplateArgs);
10751           llvm::SmallVector<const TemplateArgument *, 8> SecondExpandedList =
10752               ExpandTemplateArgumentList(SecondTemplateArgs);
10753 
10754           if (FirstExpandedList.size() != SecondExpandedList.size()) {
10755             ODRDiagDeclError(FirstRecord, FirstModule,
10756                              FirstMethod->getLocation(),
10757                              FirstMethod->getSourceRange(),
10758                              MethodDifferentNumberTemplateArguments)
10759                 << FirstMethodType << FirstName
10760                 << (unsigned)FirstExpandedList.size();
10761             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10762                             SecondMethod->getSourceRange(),
10763                             MethodDifferentNumberTemplateArguments)
10764                 << SecondMethodType << SecondName
10765                 << (unsigned)SecondExpandedList.size();
10766 
10767             Diagnosed = true;
10768             break;
10769           }
10770 
10771           bool TemplateArgumentMismatch = false;
10772           for (unsigned i = 0, e = FirstExpandedList.size(); i != e; ++i) {
10773             const TemplateArgument &FirstTA = *FirstExpandedList[i],
10774                                    &SecondTA = *SecondExpandedList[i];
10775             if (ComputeTemplateArgumentODRHash(FirstTA) ==
10776                 ComputeTemplateArgumentODRHash(SecondTA)) {
10777               continue;
10778             }
10779 
10780             ODRDiagDeclError(
10781                 FirstRecord, FirstModule, FirstMethod->getLocation(),
10782                 FirstMethod->getSourceRange(), MethodDifferentTemplateArgument)
10783                 << FirstMethodType << FirstName << FirstTA << i + 1;
10784             ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10785                             SecondMethod->getSourceRange(),
10786                             MethodDifferentTemplateArgument)
10787                 << SecondMethodType << SecondName << SecondTA << i + 1;
10788 
10789             TemplateArgumentMismatch = true;
10790             break;
10791           }
10792 
10793           if (TemplateArgumentMismatch) {
10794             Diagnosed = true;
10795             break;
10796           }
10797         }
10798 
10799         // Compute the hash of the method as if it has no body.
10800         auto ComputeCXXMethodODRHash = [&Hash](const CXXMethodDecl *D) {
10801           Hash.clear();
10802           Hash.AddFunctionDecl(D, true /*SkipBody*/);
10803           return Hash.CalculateHash();
10804         };
10805 
10806         // Compare the hash generated to the hash stored.  A difference means
10807         // that a body was present in the original source.  Due to merging,
10808         // the stardard way of detecting a body will not work.
10809         const bool HasFirstBody =
10810             ComputeCXXMethodODRHash(FirstMethod) != FirstMethod->getODRHash();
10811         const bool HasSecondBody =
10812             ComputeCXXMethodODRHash(SecondMethod) != SecondMethod->getODRHash();
10813 
10814         if (HasFirstBody != HasSecondBody) {
10815           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10816                            FirstMethod->getSourceRange(), MethodSingleBody)
10817               << FirstMethodType << FirstName << HasFirstBody;
10818           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10819                           SecondMethod->getSourceRange(), MethodSingleBody)
10820               << SecondMethodType << SecondName << HasSecondBody;
10821           Diagnosed = true;
10822           break;
10823         }
10824 
10825         if (HasFirstBody && HasSecondBody) {
10826           ODRDiagDeclError(FirstRecord, FirstModule, FirstMethod->getLocation(),
10827                            FirstMethod->getSourceRange(), MethodDifferentBody)
10828               << FirstMethodType << FirstName;
10829           ODRDiagDeclNote(SecondModule, SecondMethod->getLocation(),
10830                           SecondMethod->getSourceRange(), MethodDifferentBody)
10831               << SecondMethodType << SecondName;
10832           Diagnosed = true;
10833           break;
10834         }
10835 
10836         break;
10837       }
10838       case TypeAlias:
10839       case TypeDef: {
10840         Diagnosed = ODRDiagTypeDefOrAlias(
10841             FirstRecord, FirstModule, SecondModule,
10842             cast<TypedefNameDecl>(FirstDecl), cast<TypedefNameDecl>(SecondDecl),
10843             FirstDiffType == TypeAlias);
10844         break;
10845       }
10846       case Var: {
10847         Diagnosed =
10848             ODRDiagVar(FirstRecord, FirstModule, SecondModule,
10849                        cast<VarDecl>(FirstDecl), cast<VarDecl>(SecondDecl));
10850         break;
10851       }
10852       case Friend: {
10853         FriendDecl *FirstFriend = cast<FriendDecl>(FirstDecl);
10854         FriendDecl *SecondFriend = cast<FriendDecl>(SecondDecl);
10855 
10856         NamedDecl *FirstND = FirstFriend->getFriendDecl();
10857         NamedDecl *SecondND = SecondFriend->getFriendDecl();
10858 
10859         TypeSourceInfo *FirstTSI = FirstFriend->getFriendType();
10860         TypeSourceInfo *SecondTSI = SecondFriend->getFriendType();
10861 
10862         if (FirstND && SecondND) {
10863           ODRDiagDeclError(FirstRecord, FirstModule,
10864                            FirstFriend->getFriendLoc(),
10865                            FirstFriend->getSourceRange(), FriendFunction)
10866               << FirstND;
10867           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10868                           SecondFriend->getSourceRange(), FriendFunction)
10869               << SecondND;
10870 
10871           Diagnosed = true;
10872           break;
10873         }
10874 
10875         if (FirstTSI && SecondTSI) {
10876           QualType FirstFriendType = FirstTSI->getType();
10877           QualType SecondFriendType = SecondTSI->getType();
10878           assert(ComputeQualTypeODRHash(FirstFriendType) !=
10879                  ComputeQualTypeODRHash(SecondFriendType));
10880           ODRDiagDeclError(FirstRecord, FirstModule,
10881                            FirstFriend->getFriendLoc(),
10882                            FirstFriend->getSourceRange(), FriendType)
10883               << FirstFriendType;
10884           ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10885                           SecondFriend->getSourceRange(), FriendType)
10886               << SecondFriendType;
10887           Diagnosed = true;
10888           break;
10889         }
10890 
10891         ODRDiagDeclError(FirstRecord, FirstModule, FirstFriend->getFriendLoc(),
10892                          FirstFriend->getSourceRange(), FriendTypeFunction)
10893             << (FirstTSI == nullptr);
10894         ODRDiagDeclNote(SecondModule, SecondFriend->getFriendLoc(),
10895                         SecondFriend->getSourceRange(), FriendTypeFunction)
10896             << (SecondTSI == nullptr);
10897 
10898         Diagnosed = true;
10899         break;
10900       }
10901       case FunctionTemplate: {
10902         FunctionTemplateDecl *FirstTemplate =
10903             cast<FunctionTemplateDecl>(FirstDecl);
10904         FunctionTemplateDecl *SecondTemplate =
10905             cast<FunctionTemplateDecl>(SecondDecl);
10906 
10907         TemplateParameterList *FirstTPL =
10908             FirstTemplate->getTemplateParameters();
10909         TemplateParameterList *SecondTPL =
10910             SecondTemplate->getTemplateParameters();
10911 
10912         if (FirstTPL->size() != SecondTPL->size()) {
10913           ODRDiagDeclError(FirstRecord, FirstModule,
10914                            FirstTemplate->getLocation(),
10915                            FirstTemplate->getSourceRange(),
10916                            FunctionTemplateDifferentNumberParameters)
10917               << FirstTemplate << FirstTPL->size();
10918           ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10919                           SecondTemplate->getSourceRange(),
10920                           FunctionTemplateDifferentNumberParameters)
10921               << SecondTemplate << SecondTPL->size();
10922 
10923           Diagnosed = true;
10924           break;
10925         }
10926 
10927         bool ParameterMismatch = false;
10928         for (unsigned i = 0, e = FirstTPL->size(); i != e; ++i) {
10929           NamedDecl *FirstParam = FirstTPL->getParam(i);
10930           NamedDecl *SecondParam = SecondTPL->getParam(i);
10931 
10932           if (FirstParam->getKind() != SecondParam->getKind()) {
10933             enum {
10934               TemplateTypeParameter,
10935               NonTypeTemplateParameter,
10936               TemplateTemplateParameter,
10937             };
10938             auto GetParamType = [](NamedDecl *D) {
10939               switch (D->getKind()) {
10940                 default:
10941                   llvm_unreachable("Unexpected template parameter type");
10942                 case Decl::TemplateTypeParm:
10943                   return TemplateTypeParameter;
10944                 case Decl::NonTypeTemplateParm:
10945                   return NonTypeTemplateParameter;
10946                 case Decl::TemplateTemplateParm:
10947                   return TemplateTemplateParameter;
10948               }
10949             };
10950 
10951             ODRDiagDeclError(FirstRecord, FirstModule,
10952                              FirstTemplate->getLocation(),
10953                              FirstTemplate->getSourceRange(),
10954                              FunctionTemplateParameterDifferentKind)
10955                 << FirstTemplate << (i + 1) << GetParamType(FirstParam);
10956             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10957                             SecondTemplate->getSourceRange(),
10958                             FunctionTemplateParameterDifferentKind)
10959                 << SecondTemplate << (i + 1) << GetParamType(SecondParam);
10960 
10961             ParameterMismatch = true;
10962             break;
10963           }
10964 
10965           if (FirstParam->getName() != SecondParam->getName()) {
10966             ODRDiagDeclError(
10967                 FirstRecord, FirstModule, FirstTemplate->getLocation(),
10968                 FirstTemplate->getSourceRange(), FunctionTemplateParameterName)
10969                 << FirstTemplate << (i + 1) << (bool)FirstParam->getIdentifier()
10970                 << FirstParam;
10971             ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10972                             SecondTemplate->getSourceRange(),
10973                             FunctionTemplateParameterName)
10974                 << SecondTemplate << (i + 1)
10975                 << (bool)SecondParam->getIdentifier() << SecondParam;
10976             ParameterMismatch = true;
10977             break;
10978           }
10979 
10980           if (isa<TemplateTypeParmDecl>(FirstParam) &&
10981               isa<TemplateTypeParmDecl>(SecondParam)) {
10982             TemplateTypeParmDecl *FirstTTPD =
10983                 cast<TemplateTypeParmDecl>(FirstParam);
10984             TemplateTypeParmDecl *SecondTTPD =
10985                 cast<TemplateTypeParmDecl>(SecondParam);
10986             bool HasFirstDefaultArgument =
10987                 FirstTTPD->hasDefaultArgument() &&
10988                 !FirstTTPD->defaultArgumentWasInherited();
10989             bool HasSecondDefaultArgument =
10990                 SecondTTPD->hasDefaultArgument() &&
10991                 !SecondTTPD->defaultArgumentWasInherited();
10992             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
10993               ODRDiagDeclError(FirstRecord, FirstModule,
10994                                FirstTemplate->getLocation(),
10995                                FirstTemplate->getSourceRange(),
10996                                FunctionTemplateParameterSingleDefaultArgument)
10997                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
10998               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
10999                               SecondTemplate->getSourceRange(),
11000                               FunctionTemplateParameterSingleDefaultArgument)
11001                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11002               ParameterMismatch = true;
11003               break;
11004             }
11005 
11006             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11007               QualType FirstType = FirstTTPD->getDefaultArgument();
11008               QualType SecondType = SecondTTPD->getDefaultArgument();
11009               if (ComputeQualTypeODRHash(FirstType) !=
11010                   ComputeQualTypeODRHash(SecondType)) {
11011                 ODRDiagDeclError(
11012                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11013                     FirstTemplate->getSourceRange(),
11014                     FunctionTemplateParameterDifferentDefaultArgument)
11015                     << FirstTemplate << (i + 1) << FirstType;
11016                 ODRDiagDeclNote(
11017                     SecondModule, SecondTemplate->getLocation(),
11018                     SecondTemplate->getSourceRange(),
11019                     FunctionTemplateParameterDifferentDefaultArgument)
11020                     << SecondTemplate << (i + 1) << SecondType;
11021                 ParameterMismatch = true;
11022                 break;
11023               }
11024             }
11025 
11026             if (FirstTTPD->isParameterPack() !=
11027                 SecondTTPD->isParameterPack()) {
11028               ODRDiagDeclError(FirstRecord, FirstModule,
11029                                FirstTemplate->getLocation(),
11030                                FirstTemplate->getSourceRange(),
11031                                FunctionTemplatePackParameter)
11032                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11033               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11034                               SecondTemplate->getSourceRange(),
11035                               FunctionTemplatePackParameter)
11036                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11037               ParameterMismatch = true;
11038               break;
11039             }
11040           }
11041 
11042           if (isa<TemplateTemplateParmDecl>(FirstParam) &&
11043               isa<TemplateTemplateParmDecl>(SecondParam)) {
11044             TemplateTemplateParmDecl *FirstTTPD =
11045                 cast<TemplateTemplateParmDecl>(FirstParam);
11046             TemplateTemplateParmDecl *SecondTTPD =
11047                 cast<TemplateTemplateParmDecl>(SecondParam);
11048 
11049             TemplateParameterList *FirstTPL =
11050                 FirstTTPD->getTemplateParameters();
11051             TemplateParameterList *SecondTPL =
11052                 SecondTTPD->getTemplateParameters();
11053 
11054             if (ComputeTemplateParameterListODRHash(FirstTPL) !=
11055                 ComputeTemplateParameterListODRHash(SecondTPL)) {
11056               ODRDiagDeclError(FirstRecord, FirstModule,
11057                                FirstTemplate->getLocation(),
11058                                FirstTemplate->getSourceRange(),
11059                                FunctionTemplateParameterDifferentType)
11060                   << FirstTemplate << (i + 1);
11061               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11062                               SecondTemplate->getSourceRange(),
11063                               FunctionTemplateParameterDifferentType)
11064                   << SecondTemplate << (i + 1);
11065               ParameterMismatch = true;
11066               break;
11067             }
11068 
11069             bool HasFirstDefaultArgument =
11070                 FirstTTPD->hasDefaultArgument() &&
11071                 !FirstTTPD->defaultArgumentWasInherited();
11072             bool HasSecondDefaultArgument =
11073                 SecondTTPD->hasDefaultArgument() &&
11074                 !SecondTTPD->defaultArgumentWasInherited();
11075             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11076               ODRDiagDeclError(FirstRecord, FirstModule,
11077                                FirstTemplate->getLocation(),
11078                                FirstTemplate->getSourceRange(),
11079                                FunctionTemplateParameterSingleDefaultArgument)
11080                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11081               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11082                               SecondTemplate->getSourceRange(),
11083                               FunctionTemplateParameterSingleDefaultArgument)
11084                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11085               ParameterMismatch = true;
11086               break;
11087             }
11088 
11089             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11090               TemplateArgument FirstTA =
11091                   FirstTTPD->getDefaultArgument().getArgument();
11092               TemplateArgument SecondTA =
11093                   SecondTTPD->getDefaultArgument().getArgument();
11094               if (ComputeTemplateArgumentODRHash(FirstTA) !=
11095                   ComputeTemplateArgumentODRHash(SecondTA)) {
11096                 ODRDiagDeclError(
11097                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11098                     FirstTemplate->getSourceRange(),
11099                     FunctionTemplateParameterDifferentDefaultArgument)
11100                     << FirstTemplate << (i + 1) << FirstTA;
11101                 ODRDiagDeclNote(
11102                     SecondModule, SecondTemplate->getLocation(),
11103                     SecondTemplate->getSourceRange(),
11104                     FunctionTemplateParameterDifferentDefaultArgument)
11105                     << SecondTemplate << (i + 1) << SecondTA;
11106                 ParameterMismatch = true;
11107                 break;
11108               }
11109             }
11110 
11111             if (FirstTTPD->isParameterPack() !=
11112                 SecondTTPD->isParameterPack()) {
11113               ODRDiagDeclError(FirstRecord, FirstModule,
11114                                FirstTemplate->getLocation(),
11115                                FirstTemplate->getSourceRange(),
11116                                FunctionTemplatePackParameter)
11117                   << FirstTemplate << (i + 1) << FirstTTPD->isParameterPack();
11118               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11119                               SecondTemplate->getSourceRange(),
11120                               FunctionTemplatePackParameter)
11121                   << SecondTemplate << (i + 1) << SecondTTPD->isParameterPack();
11122               ParameterMismatch = true;
11123               break;
11124             }
11125           }
11126 
11127           if (isa<NonTypeTemplateParmDecl>(FirstParam) &&
11128               isa<NonTypeTemplateParmDecl>(SecondParam)) {
11129             NonTypeTemplateParmDecl *FirstNTTPD =
11130                 cast<NonTypeTemplateParmDecl>(FirstParam);
11131             NonTypeTemplateParmDecl *SecondNTTPD =
11132                 cast<NonTypeTemplateParmDecl>(SecondParam);
11133 
11134             QualType FirstType = FirstNTTPD->getType();
11135             QualType SecondType = SecondNTTPD->getType();
11136             if (ComputeQualTypeODRHash(FirstType) !=
11137                 ComputeQualTypeODRHash(SecondType)) {
11138               ODRDiagDeclError(FirstRecord, FirstModule,
11139                                FirstTemplate->getLocation(),
11140                                FirstTemplate->getSourceRange(),
11141                                FunctionTemplateParameterDifferentType)
11142                   << FirstTemplate << (i + 1);
11143               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11144                               SecondTemplate->getSourceRange(),
11145                               FunctionTemplateParameterDifferentType)
11146                   << SecondTemplate << (i + 1);
11147               ParameterMismatch = true;
11148               break;
11149             }
11150 
11151             bool HasFirstDefaultArgument =
11152                 FirstNTTPD->hasDefaultArgument() &&
11153                 !FirstNTTPD->defaultArgumentWasInherited();
11154             bool HasSecondDefaultArgument =
11155                 SecondNTTPD->hasDefaultArgument() &&
11156                 !SecondNTTPD->defaultArgumentWasInherited();
11157             if (HasFirstDefaultArgument != HasSecondDefaultArgument) {
11158               ODRDiagDeclError(FirstRecord, FirstModule,
11159                                FirstTemplate->getLocation(),
11160                                FirstTemplate->getSourceRange(),
11161                                FunctionTemplateParameterSingleDefaultArgument)
11162                   << FirstTemplate << (i + 1) << HasFirstDefaultArgument;
11163               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11164                               SecondTemplate->getSourceRange(),
11165                               FunctionTemplateParameterSingleDefaultArgument)
11166                   << SecondTemplate << (i + 1) << HasSecondDefaultArgument;
11167               ParameterMismatch = true;
11168               break;
11169             }
11170 
11171             if (HasFirstDefaultArgument && HasSecondDefaultArgument) {
11172               Expr *FirstDefaultArgument = FirstNTTPD->getDefaultArgument();
11173               Expr *SecondDefaultArgument = SecondNTTPD->getDefaultArgument();
11174               if (ComputeODRHash(FirstDefaultArgument) !=
11175                   ComputeODRHash(SecondDefaultArgument)) {
11176                 ODRDiagDeclError(
11177                     FirstRecord, FirstModule, FirstTemplate->getLocation(),
11178                     FirstTemplate->getSourceRange(),
11179                     FunctionTemplateParameterDifferentDefaultArgument)
11180                     << FirstTemplate << (i + 1) << FirstDefaultArgument;
11181                 ODRDiagDeclNote(
11182                     SecondModule, SecondTemplate->getLocation(),
11183                     SecondTemplate->getSourceRange(),
11184                     FunctionTemplateParameterDifferentDefaultArgument)
11185                     << SecondTemplate << (i + 1) << SecondDefaultArgument;
11186                 ParameterMismatch = true;
11187                 break;
11188               }
11189             }
11190 
11191             if (FirstNTTPD->isParameterPack() !=
11192                 SecondNTTPD->isParameterPack()) {
11193               ODRDiagDeclError(FirstRecord, FirstModule,
11194                                FirstTemplate->getLocation(),
11195                                FirstTemplate->getSourceRange(),
11196                                FunctionTemplatePackParameter)
11197                   << FirstTemplate << (i + 1) << FirstNTTPD->isParameterPack();
11198               ODRDiagDeclNote(SecondModule, SecondTemplate->getLocation(),
11199                               SecondTemplate->getSourceRange(),
11200                               FunctionTemplatePackParameter)
11201                   << SecondTemplate << (i + 1)
11202                   << SecondNTTPD->isParameterPack();
11203               ParameterMismatch = true;
11204               break;
11205             }
11206           }
11207         }
11208 
11209         if (ParameterMismatch) {
11210           Diagnosed = true;
11211           break;
11212         }
11213 
11214         break;
11215       }
11216       }
11217 
11218       if (Diagnosed)
11219         continue;
11220 
11221       Diag(FirstDecl->getLocation(),
11222            diag::err_module_odr_violation_mismatch_decl_unknown)
11223           << FirstRecord << FirstModule.empty() << FirstModule << FirstDiffType
11224           << FirstDecl->getSourceRange();
11225       Diag(SecondDecl->getLocation(),
11226            diag::note_module_odr_violation_mismatch_decl_unknown)
11227           << SecondModule << FirstDiffType << SecondDecl->getSourceRange();
11228       Diagnosed = true;
11229     }
11230 
11231     if (!Diagnosed) {
11232       // All definitions are updates to the same declaration. This happens if a
11233       // module instantiates the declaration of a class template specialization
11234       // and two or more other modules instantiate its definition.
11235       //
11236       // FIXME: Indicate which modules had instantiations of this definition.
11237       // FIXME: How can this even happen?
11238       Diag(Merge.first->getLocation(),
11239            diag::err_module_odr_violation_different_instantiations)
11240         << Merge.first;
11241     }
11242   }
11243 
11244   // Issue ODR failures diagnostics for functions.
11245   for (auto &Merge : FunctionOdrMergeFailures) {
11246     enum ODRFunctionDifference {
11247       ReturnType,
11248       ParameterName,
11249       ParameterType,
11250       ParameterSingleDefaultArgument,
11251       ParameterDifferentDefaultArgument,
11252       FunctionBody,
11253     };
11254 
11255     FunctionDecl *FirstFunction = Merge.first;
11256     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstFunction);
11257 
11258     bool Diagnosed = false;
11259     for (auto &SecondFunction : Merge.second) {
11260 
11261       if (FirstFunction == SecondFunction)
11262         continue;
11263 
11264       std::string SecondModule =
11265           getOwningModuleNameForDiagnostic(SecondFunction);
11266 
11267       auto ODRDiagError = [FirstFunction, &FirstModule,
11268                            this](SourceLocation Loc, SourceRange Range,
11269                                  ODRFunctionDifference DiffType) {
11270         return Diag(Loc, diag::err_module_odr_violation_function)
11271                << FirstFunction << FirstModule.empty() << FirstModule << Range
11272                << DiffType;
11273       };
11274       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11275                                                SourceRange Range,
11276                                                ODRFunctionDifference DiffType) {
11277         return Diag(Loc, diag::note_module_odr_violation_function)
11278                << SecondModule << Range << DiffType;
11279       };
11280 
11281       if (ComputeQualTypeODRHash(FirstFunction->getReturnType()) !=
11282           ComputeQualTypeODRHash(SecondFunction->getReturnType())) {
11283         ODRDiagError(FirstFunction->getReturnTypeSourceRange().getBegin(),
11284                      FirstFunction->getReturnTypeSourceRange(), ReturnType)
11285             << FirstFunction->getReturnType();
11286         ODRDiagNote(SecondFunction->getReturnTypeSourceRange().getBegin(),
11287                     SecondFunction->getReturnTypeSourceRange(), ReturnType)
11288             << SecondFunction->getReturnType();
11289         Diagnosed = true;
11290         break;
11291       }
11292 
11293       assert(FirstFunction->param_size() == SecondFunction->param_size() &&
11294              "Merged functions with different number of parameters");
11295 
11296       auto ParamSize = FirstFunction->param_size();
11297       bool ParameterMismatch = false;
11298       for (unsigned I = 0; I < ParamSize; ++I) {
11299         auto *FirstParam = FirstFunction->getParamDecl(I);
11300         auto *SecondParam = SecondFunction->getParamDecl(I);
11301 
11302         assert(getContext().hasSameType(FirstParam->getType(),
11303                                       SecondParam->getType()) &&
11304                "Merged function has different parameter types.");
11305 
11306         if (FirstParam->getDeclName() != SecondParam->getDeclName()) {
11307           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11308                        ParameterName)
11309               << I + 1 << FirstParam->getDeclName();
11310           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11311                       ParameterName)
11312               << I + 1 << SecondParam->getDeclName();
11313           ParameterMismatch = true;
11314           break;
11315         };
11316 
11317         QualType FirstParamType = FirstParam->getType();
11318         QualType SecondParamType = SecondParam->getType();
11319         if (FirstParamType != SecondParamType &&
11320             ComputeQualTypeODRHash(FirstParamType) !=
11321                 ComputeQualTypeODRHash(SecondParamType)) {
11322           if (const DecayedType *ParamDecayedType =
11323                   FirstParamType->getAs<DecayedType>()) {
11324             ODRDiagError(FirstParam->getLocation(),
11325                          FirstParam->getSourceRange(), ParameterType)
11326                 << (I + 1) << FirstParamType << true
11327                 << ParamDecayedType->getOriginalType();
11328           } else {
11329             ODRDiagError(FirstParam->getLocation(),
11330                          FirstParam->getSourceRange(), ParameterType)
11331                 << (I + 1) << FirstParamType << false;
11332           }
11333 
11334           if (const DecayedType *ParamDecayedType =
11335                   SecondParamType->getAs<DecayedType>()) {
11336             ODRDiagNote(SecondParam->getLocation(),
11337                         SecondParam->getSourceRange(), ParameterType)
11338                 << (I + 1) << SecondParamType << true
11339                 << ParamDecayedType->getOriginalType();
11340           } else {
11341             ODRDiagNote(SecondParam->getLocation(),
11342                         SecondParam->getSourceRange(), ParameterType)
11343                 << (I + 1) << SecondParamType << false;
11344           }
11345           ParameterMismatch = true;
11346           break;
11347         }
11348 
11349         const Expr *FirstInit = FirstParam->getInit();
11350         const Expr *SecondInit = SecondParam->getInit();
11351         if ((FirstInit == nullptr) != (SecondInit == nullptr)) {
11352           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11353                        ParameterSingleDefaultArgument)
11354               << (I + 1) << (FirstInit == nullptr)
11355               << (FirstInit ? FirstInit->getSourceRange() : SourceRange());
11356           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11357                       ParameterSingleDefaultArgument)
11358               << (I + 1) << (SecondInit == nullptr)
11359               << (SecondInit ? SecondInit->getSourceRange() : SourceRange());
11360           ParameterMismatch = true;
11361           break;
11362         }
11363 
11364         if (FirstInit && SecondInit &&
11365             ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11366           ODRDiagError(FirstParam->getLocation(), FirstParam->getSourceRange(),
11367                        ParameterDifferentDefaultArgument)
11368               << (I + 1) << FirstInit->getSourceRange();
11369           ODRDiagNote(SecondParam->getLocation(), SecondParam->getSourceRange(),
11370                       ParameterDifferentDefaultArgument)
11371               << (I + 1) << SecondInit->getSourceRange();
11372           ParameterMismatch = true;
11373           break;
11374         }
11375 
11376         assert(ComputeSubDeclODRHash(FirstParam) ==
11377                    ComputeSubDeclODRHash(SecondParam) &&
11378                "Undiagnosed parameter difference.");
11379       }
11380 
11381       if (ParameterMismatch) {
11382         Diagnosed = true;
11383         break;
11384       }
11385 
11386       // If no error has been generated before now, assume the problem is in
11387       // the body and generate a message.
11388       ODRDiagError(FirstFunction->getLocation(),
11389                    FirstFunction->getSourceRange(), FunctionBody);
11390       ODRDiagNote(SecondFunction->getLocation(),
11391                   SecondFunction->getSourceRange(), FunctionBody);
11392       Diagnosed = true;
11393       break;
11394     }
11395     (void)Diagnosed;
11396     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11397   }
11398 
11399   // Issue ODR failures diagnostics for enums.
11400   for (auto &Merge : EnumOdrMergeFailures) {
11401     enum ODREnumDifference {
11402       SingleScopedEnum,
11403       EnumTagKeywordMismatch,
11404       SingleSpecifiedType,
11405       DifferentSpecifiedTypes,
11406       DifferentNumberEnumConstants,
11407       EnumConstantName,
11408       EnumConstantSingleInitilizer,
11409       EnumConstantDifferentInitilizer,
11410     };
11411 
11412     // If we've already pointed out a specific problem with this enum, don't
11413     // bother issuing a general "something's different" diagnostic.
11414     if (!DiagnosedOdrMergeFailures.insert(Merge.first).second)
11415       continue;
11416 
11417     EnumDecl *FirstEnum = Merge.first;
11418     std::string FirstModule = getOwningModuleNameForDiagnostic(FirstEnum);
11419 
11420     using DeclHashes =
11421         llvm::SmallVector<std::pair<EnumConstantDecl *, unsigned>, 4>;
11422     auto PopulateHashes = [&ComputeSubDeclODRHash, FirstEnum](
11423                               DeclHashes &Hashes, EnumDecl *Enum) {
11424       for (auto *D : Enum->decls()) {
11425         // Due to decl merging, the first EnumDecl is the parent of
11426         // Decls in both records.
11427         if (!ODRHash::isDeclToBeProcessed(D, FirstEnum))
11428           continue;
11429         assert(isa<EnumConstantDecl>(D) && "Unexpected Decl kind");
11430         Hashes.emplace_back(cast<EnumConstantDecl>(D),
11431                             ComputeSubDeclODRHash(D));
11432       }
11433     };
11434     DeclHashes FirstHashes;
11435     PopulateHashes(FirstHashes, FirstEnum);
11436     bool Diagnosed = false;
11437     for (auto &SecondEnum : Merge.second) {
11438 
11439       if (FirstEnum == SecondEnum)
11440         continue;
11441 
11442       std::string SecondModule =
11443           getOwningModuleNameForDiagnostic(SecondEnum);
11444 
11445       auto ODRDiagError = [FirstEnum, &FirstModule,
11446                            this](SourceLocation Loc, SourceRange Range,
11447                                  ODREnumDifference DiffType) {
11448         return Diag(Loc, diag::err_module_odr_violation_enum)
11449                << FirstEnum << FirstModule.empty() << FirstModule << Range
11450                << DiffType;
11451       };
11452       auto ODRDiagNote = [&SecondModule, this](SourceLocation Loc,
11453                                                SourceRange Range,
11454                                                ODREnumDifference DiffType) {
11455         return Diag(Loc, diag::note_module_odr_violation_enum)
11456                << SecondModule << Range << DiffType;
11457       };
11458 
11459       if (FirstEnum->isScoped() != SecondEnum->isScoped()) {
11460         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11461                      SingleScopedEnum)
11462             << FirstEnum->isScoped();
11463         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11464                     SingleScopedEnum)
11465             << SecondEnum->isScoped();
11466         Diagnosed = true;
11467         continue;
11468       }
11469 
11470       if (FirstEnum->isScoped() && SecondEnum->isScoped()) {
11471         if (FirstEnum->isScopedUsingClassTag() !=
11472             SecondEnum->isScopedUsingClassTag()) {
11473           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11474                        EnumTagKeywordMismatch)
11475               << FirstEnum->isScopedUsingClassTag();
11476           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11477                       EnumTagKeywordMismatch)
11478               << SecondEnum->isScopedUsingClassTag();
11479           Diagnosed = true;
11480           continue;
11481         }
11482       }
11483 
11484       QualType FirstUnderlyingType =
11485           FirstEnum->getIntegerTypeSourceInfo()
11486               ? FirstEnum->getIntegerTypeSourceInfo()->getType()
11487               : QualType();
11488       QualType SecondUnderlyingType =
11489           SecondEnum->getIntegerTypeSourceInfo()
11490               ? SecondEnum->getIntegerTypeSourceInfo()->getType()
11491               : QualType();
11492       if (FirstUnderlyingType.isNull() != SecondUnderlyingType.isNull()) {
11493           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11494                        SingleSpecifiedType)
11495               << !FirstUnderlyingType.isNull();
11496           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11497                       SingleSpecifiedType)
11498               << !SecondUnderlyingType.isNull();
11499           Diagnosed = true;
11500           continue;
11501       }
11502 
11503       if (!FirstUnderlyingType.isNull() && !SecondUnderlyingType.isNull()) {
11504         if (ComputeQualTypeODRHash(FirstUnderlyingType) !=
11505             ComputeQualTypeODRHash(SecondUnderlyingType)) {
11506           ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11507                        DifferentSpecifiedTypes)
11508               << FirstUnderlyingType;
11509           ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11510                       DifferentSpecifiedTypes)
11511               << SecondUnderlyingType;
11512           Diagnosed = true;
11513           continue;
11514         }
11515       }
11516 
11517       DeclHashes SecondHashes;
11518       PopulateHashes(SecondHashes, SecondEnum);
11519 
11520       if (FirstHashes.size() != SecondHashes.size()) {
11521         ODRDiagError(FirstEnum->getLocation(), FirstEnum->getSourceRange(),
11522                      DifferentNumberEnumConstants)
11523             << (int)FirstHashes.size();
11524         ODRDiagNote(SecondEnum->getLocation(), SecondEnum->getSourceRange(),
11525                     DifferentNumberEnumConstants)
11526             << (int)SecondHashes.size();
11527         Diagnosed = true;
11528         continue;
11529       }
11530 
11531       for (unsigned I = 0; I < FirstHashes.size(); ++I) {
11532         if (FirstHashes[I].second == SecondHashes[I].second)
11533           continue;
11534         const EnumConstantDecl *FirstEnumConstant = FirstHashes[I].first;
11535         const EnumConstantDecl *SecondEnumConstant = SecondHashes[I].first;
11536 
11537         if (FirstEnumConstant->getDeclName() !=
11538             SecondEnumConstant->getDeclName()) {
11539 
11540           ODRDiagError(FirstEnumConstant->getLocation(),
11541                        FirstEnumConstant->getSourceRange(), EnumConstantName)
11542               << I + 1 << FirstEnumConstant;
11543           ODRDiagNote(SecondEnumConstant->getLocation(),
11544                       SecondEnumConstant->getSourceRange(), EnumConstantName)
11545               << I + 1 << SecondEnumConstant;
11546           Diagnosed = true;
11547           break;
11548         }
11549 
11550         const Expr *FirstInit = FirstEnumConstant->getInitExpr();
11551         const Expr *SecondInit = SecondEnumConstant->getInitExpr();
11552         if (!FirstInit && !SecondInit)
11553           continue;
11554 
11555         if (!FirstInit || !SecondInit) {
11556           ODRDiagError(FirstEnumConstant->getLocation(),
11557                        FirstEnumConstant->getSourceRange(),
11558                        EnumConstantSingleInitilizer)
11559               << I + 1 << FirstEnumConstant << (FirstInit != nullptr);
11560           ODRDiagNote(SecondEnumConstant->getLocation(),
11561                       SecondEnumConstant->getSourceRange(),
11562                       EnumConstantSingleInitilizer)
11563               << I + 1 << SecondEnumConstant << (SecondInit != nullptr);
11564           Diagnosed = true;
11565           break;
11566         }
11567 
11568         if (ComputeODRHash(FirstInit) != ComputeODRHash(SecondInit)) {
11569           ODRDiagError(FirstEnumConstant->getLocation(),
11570                        FirstEnumConstant->getSourceRange(),
11571                        EnumConstantDifferentInitilizer)
11572               << I + 1 << FirstEnumConstant;
11573           ODRDiagNote(SecondEnumConstant->getLocation(),
11574                       SecondEnumConstant->getSourceRange(),
11575                       EnumConstantDifferentInitilizer)
11576               << I + 1 << SecondEnumConstant;
11577           Diagnosed = true;
11578           break;
11579         }
11580       }
11581     }
11582 
11583     (void)Diagnosed;
11584     assert(Diagnosed && "Unable to emit ODR diagnostic.");
11585   }
11586 }
11587 
11588 void ASTReader::StartedDeserializing() {
11589   if (++NumCurrentElementsDeserializing == 1 && ReadTimer.get())
11590     ReadTimer->startTimer();
11591 }
11592 
11593 void ASTReader::FinishedDeserializing() {
11594   assert(NumCurrentElementsDeserializing &&
11595          "FinishedDeserializing not paired with StartedDeserializing");
11596   if (NumCurrentElementsDeserializing == 1) {
11597     // We decrease NumCurrentElementsDeserializing only after pending actions
11598     // are finished, to avoid recursively re-calling finishPendingActions().
11599     finishPendingActions();
11600   }
11601   --NumCurrentElementsDeserializing;
11602 
11603   if (NumCurrentElementsDeserializing == 0) {
11604     // Propagate exception specification and deduced type updates along
11605     // redeclaration chains.
11606     //
11607     // We do this now rather than in finishPendingActions because we want to
11608     // be able to walk the complete redeclaration chains of the updated decls.
11609     while (!PendingExceptionSpecUpdates.empty() ||
11610            !PendingDeducedTypeUpdates.empty()) {
11611       auto ESUpdates = std::move(PendingExceptionSpecUpdates);
11612       PendingExceptionSpecUpdates.clear();
11613       for (auto Update : ESUpdates) {
11614         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11615         auto *FPT = Update.second->getType()->castAs<FunctionProtoType>();
11616         auto ESI = FPT->getExtProtoInfo().ExceptionSpec;
11617         if (auto *Listener = getContext().getASTMutationListener())
11618           Listener->ResolvedExceptionSpec(cast<FunctionDecl>(Update.second));
11619         for (auto *Redecl : Update.second->redecls())
11620           getContext().adjustExceptionSpec(cast<FunctionDecl>(Redecl), ESI);
11621       }
11622 
11623       auto DTUpdates = std::move(PendingDeducedTypeUpdates);
11624       PendingDeducedTypeUpdates.clear();
11625       for (auto Update : DTUpdates) {
11626         ProcessingUpdatesRAIIObj ProcessingUpdates(*this);
11627         // FIXME: If the return type is already deduced, check that it matches.
11628         getContext().adjustDeducedFunctionResultType(Update.first,
11629                                                      Update.second);
11630       }
11631     }
11632 
11633     if (ReadTimer)
11634       ReadTimer->stopTimer();
11635 
11636     diagnoseOdrViolations();
11637 
11638     // We are not in recursive loading, so it's safe to pass the "interesting"
11639     // decls to the consumer.
11640     if (Consumer)
11641       PassInterestingDeclsToConsumer();
11642   }
11643 }
11644 
11645 void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) {
11646   if (IdentifierInfo *II = Name.getAsIdentifierInfo()) {
11647     // Remove any fake results before adding any real ones.
11648     auto It = PendingFakeLookupResults.find(II);
11649     if (It != PendingFakeLookupResults.end()) {
11650       for (auto *ND : It->second)
11651         SemaObj->IdResolver.RemoveDecl(ND);
11652       // FIXME: this works around module+PCH performance issue.
11653       // Rather than erase the result from the map, which is O(n), just clear
11654       // the vector of NamedDecls.
11655       It->second.clear();
11656     }
11657   }
11658 
11659   if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) {
11660     SemaObj->TUScope->AddDecl(D);
11661   } else if (SemaObj->TUScope) {
11662     // Adding the decl to IdResolver may have failed because it was already in
11663     // (even though it was not added in scope). If it is already in, make sure
11664     // it gets in the scope as well.
11665     if (std::find(SemaObj->IdResolver.begin(Name),
11666                   SemaObj->IdResolver.end(), D) != SemaObj->IdResolver.end())
11667       SemaObj->TUScope->AddDecl(D);
11668   }
11669 }
11670 
11671 ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
11672                      ASTContext *Context,
11673                      const PCHContainerReader &PCHContainerRdr,
11674                      ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
11675                      StringRef isysroot, bool DisableValidation,
11676                      bool AllowASTWithCompilerErrors,
11677                      bool AllowConfigurationMismatch, bool ValidateSystemInputs,
11678                      bool ValidateASTInputFilesContent, bool UseGlobalIndex,
11679                      std::unique_ptr<llvm::Timer> ReadTimer)
11680     : Listener(DisableValidation
11681                    ? cast<ASTReaderListener>(new SimpleASTReaderListener(PP))
11682                    : cast<ASTReaderListener>(new PCHValidator(PP, *this))),
11683       SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()),
11684       PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), PP(PP),
11685       ContextObj(Context), ModuleMgr(PP.getFileManager(), ModuleCache,
11686                                      PCHContainerRdr, PP.getHeaderSearchInfo()),
11687       DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot),
11688       DisableValidation(DisableValidation),
11689       AllowASTWithCompilerErrors(AllowASTWithCompilerErrors),
11690       AllowConfigurationMismatch(AllowConfigurationMismatch),
11691       ValidateSystemInputs(ValidateSystemInputs),
11692       ValidateASTInputFilesContent(ValidateASTInputFilesContent),
11693       UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
11694   SourceMgr.setExternalSLocEntrySource(this);
11695 
11696   for (const auto &Ext : Extensions) {
11697     auto BlockName = Ext->getExtensionMetadata().BlockName;
11698     auto Known = ModuleFileExtensions.find(BlockName);
11699     if (Known != ModuleFileExtensions.end()) {
11700       Diags.Report(diag::warn_duplicate_module_file_extension)
11701         << BlockName;
11702       continue;
11703     }
11704 
11705     ModuleFileExtensions.insert({BlockName, Ext});
11706   }
11707 }
11708 
11709 ASTReader::~ASTReader() {
11710   if (OwnsDeserializationListener)
11711     delete DeserializationListener;
11712 }
11713 
11714 IdentifierResolver &ASTReader::getIdResolver() {
11715   return SemaObj ? SemaObj->IdResolver : DummyIdResolver;
11716 }
11717 
11718 Expected<unsigned> ASTRecordReader::readRecord(llvm::BitstreamCursor &Cursor,
11719                                                unsigned AbbrevID) {
11720   Idx = 0;
11721   Record.clear();
11722   return Cursor.readRecord(AbbrevID, Record);
11723 }
11724 //===----------------------------------------------------------------------===//
11725 //// OMPClauseReader implementation
11726 ////===----------------------------------------------------------------------===//
11727 
11728 // This has to be in namespace clang because it's friended by all
11729 // of the OMP clauses.
11730 namespace clang {
11731 
11732 class OMPClauseReader : public OMPClauseVisitor<OMPClauseReader> {
11733   ASTRecordReader &Record;
11734   ASTContext &Context;
11735 
11736 public:
11737   OMPClauseReader(ASTRecordReader &Record)
11738       : Record(Record), Context(Record.getContext()) {}
11739 
11740 #define OMP_CLAUSE_CLASS(Enum, Str, Class) void Visit##Class(Class *C);
11741 #include "llvm/Frontend/OpenMP/OMPKinds.def"
11742   OMPClause *readClause();
11743   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
11744   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
11745 };
11746 
11747 } // end namespace clang
11748 
11749 OMPClause *ASTRecordReader::readOMPClause() {
11750   return OMPClauseReader(*this).readClause();
11751 }
11752 
11753 OMPClause *OMPClauseReader::readClause() {
11754   OMPClause *C = nullptr;
11755   switch (llvm::omp::Clause(Record.readInt())) {
11756   case llvm::omp::OMPC_if:
11757     C = new (Context) OMPIfClause();
11758     break;
11759   case llvm::omp::OMPC_final:
11760     C = new (Context) OMPFinalClause();
11761     break;
11762   case llvm::omp::OMPC_num_threads:
11763     C = new (Context) OMPNumThreadsClause();
11764     break;
11765   case llvm::omp::OMPC_safelen:
11766     C = new (Context) OMPSafelenClause();
11767     break;
11768   case llvm::omp::OMPC_simdlen:
11769     C = new (Context) OMPSimdlenClause();
11770     break;
11771   case llvm::omp::OMPC_allocator:
11772     C = new (Context) OMPAllocatorClause();
11773     break;
11774   case llvm::omp::OMPC_collapse:
11775     C = new (Context) OMPCollapseClause();
11776     break;
11777   case llvm::omp::OMPC_default:
11778     C = new (Context) OMPDefaultClause();
11779     break;
11780   case llvm::omp::OMPC_proc_bind:
11781     C = new (Context) OMPProcBindClause();
11782     break;
11783   case llvm::omp::OMPC_schedule:
11784     C = new (Context) OMPScheduleClause();
11785     break;
11786   case llvm::omp::OMPC_ordered:
11787     C = OMPOrderedClause::CreateEmpty(Context, Record.readInt());
11788     break;
11789   case llvm::omp::OMPC_nowait:
11790     C = new (Context) OMPNowaitClause();
11791     break;
11792   case llvm::omp::OMPC_untied:
11793     C = new (Context) OMPUntiedClause();
11794     break;
11795   case llvm::omp::OMPC_mergeable:
11796     C = new (Context) OMPMergeableClause();
11797     break;
11798   case llvm::omp::OMPC_read:
11799     C = new (Context) OMPReadClause();
11800     break;
11801   case llvm::omp::OMPC_write:
11802     C = new (Context) OMPWriteClause();
11803     break;
11804   case llvm::omp::OMPC_update:
11805     C = OMPUpdateClause::CreateEmpty(Context, Record.readInt());
11806     break;
11807   case llvm::omp::OMPC_capture:
11808     C = new (Context) OMPCaptureClause();
11809     break;
11810   case llvm::omp::OMPC_seq_cst:
11811     C = new (Context) OMPSeqCstClause();
11812     break;
11813   case llvm::omp::OMPC_acq_rel:
11814     C = new (Context) OMPAcqRelClause();
11815     break;
11816   case llvm::omp::OMPC_acquire:
11817     C = new (Context) OMPAcquireClause();
11818     break;
11819   case llvm::omp::OMPC_release:
11820     C = new (Context) OMPReleaseClause();
11821     break;
11822   case llvm::omp::OMPC_relaxed:
11823     C = new (Context) OMPRelaxedClause();
11824     break;
11825   case llvm::omp::OMPC_threads:
11826     C = new (Context) OMPThreadsClause();
11827     break;
11828   case llvm::omp::OMPC_simd:
11829     C = new (Context) OMPSIMDClause();
11830     break;
11831   case llvm::omp::OMPC_nogroup:
11832     C = new (Context) OMPNogroupClause();
11833     break;
11834   case llvm::omp::OMPC_unified_address:
11835     C = new (Context) OMPUnifiedAddressClause();
11836     break;
11837   case llvm::omp::OMPC_unified_shared_memory:
11838     C = new (Context) OMPUnifiedSharedMemoryClause();
11839     break;
11840   case llvm::omp::OMPC_reverse_offload:
11841     C = new (Context) OMPReverseOffloadClause();
11842     break;
11843   case llvm::omp::OMPC_dynamic_allocators:
11844     C = new (Context) OMPDynamicAllocatorsClause();
11845     break;
11846   case llvm::omp::OMPC_atomic_default_mem_order:
11847     C = new (Context) OMPAtomicDefaultMemOrderClause();
11848     break;
11849  case llvm::omp::OMPC_private:
11850     C = OMPPrivateClause::CreateEmpty(Context, Record.readInt());
11851     break;
11852   case llvm::omp::OMPC_firstprivate:
11853     C = OMPFirstprivateClause::CreateEmpty(Context, Record.readInt());
11854     break;
11855   case llvm::omp::OMPC_lastprivate:
11856     C = OMPLastprivateClause::CreateEmpty(Context, Record.readInt());
11857     break;
11858   case llvm::omp::OMPC_shared:
11859     C = OMPSharedClause::CreateEmpty(Context, Record.readInt());
11860     break;
11861   case llvm::omp::OMPC_reduction: {
11862     unsigned N = Record.readInt();
11863     auto Modifier = Record.readEnum<OpenMPReductionClauseModifier>();
11864     C = OMPReductionClause::CreateEmpty(Context, N, Modifier);
11865     break;
11866   }
11867   case llvm::omp::OMPC_task_reduction:
11868     C = OMPTaskReductionClause::CreateEmpty(Context, Record.readInt());
11869     break;
11870   case llvm::omp::OMPC_in_reduction:
11871     C = OMPInReductionClause::CreateEmpty(Context, Record.readInt());
11872     break;
11873   case llvm::omp::OMPC_linear:
11874     C = OMPLinearClause::CreateEmpty(Context, Record.readInt());
11875     break;
11876   case llvm::omp::OMPC_aligned:
11877     C = OMPAlignedClause::CreateEmpty(Context, Record.readInt());
11878     break;
11879   case llvm::omp::OMPC_copyin:
11880     C = OMPCopyinClause::CreateEmpty(Context, Record.readInt());
11881     break;
11882   case llvm::omp::OMPC_copyprivate:
11883     C = OMPCopyprivateClause::CreateEmpty(Context, Record.readInt());
11884     break;
11885   case llvm::omp::OMPC_flush:
11886     C = OMPFlushClause::CreateEmpty(Context, Record.readInt());
11887     break;
11888   case llvm::omp::OMPC_depobj:
11889     C = OMPDepobjClause::CreateEmpty(Context);
11890     break;
11891   case llvm::omp::OMPC_depend: {
11892     unsigned NumVars = Record.readInt();
11893     unsigned NumLoops = Record.readInt();
11894     C = OMPDependClause::CreateEmpty(Context, NumVars, NumLoops);
11895     break;
11896   }
11897   case llvm::omp::OMPC_device:
11898     C = new (Context) OMPDeviceClause();
11899     break;
11900   case llvm::omp::OMPC_map: {
11901     OMPMappableExprListSizeTy Sizes;
11902     Sizes.NumVars = Record.readInt();
11903     Sizes.NumUniqueDeclarations = Record.readInt();
11904     Sizes.NumComponentLists = Record.readInt();
11905     Sizes.NumComponents = Record.readInt();
11906     C = OMPMapClause::CreateEmpty(Context, Sizes);
11907     break;
11908   }
11909   case llvm::omp::OMPC_num_teams:
11910     C = new (Context) OMPNumTeamsClause();
11911     break;
11912   case llvm::omp::OMPC_thread_limit:
11913     C = new (Context) OMPThreadLimitClause();
11914     break;
11915   case llvm::omp::OMPC_priority:
11916     C = new (Context) OMPPriorityClause();
11917     break;
11918   case llvm::omp::OMPC_grainsize:
11919     C = new (Context) OMPGrainsizeClause();
11920     break;
11921   case llvm::omp::OMPC_num_tasks:
11922     C = new (Context) OMPNumTasksClause();
11923     break;
11924   case llvm::omp::OMPC_hint:
11925     C = new (Context) OMPHintClause();
11926     break;
11927   case llvm::omp::OMPC_dist_schedule:
11928     C = new (Context) OMPDistScheduleClause();
11929     break;
11930   case llvm::omp::OMPC_defaultmap:
11931     C = new (Context) OMPDefaultmapClause();
11932     break;
11933   case llvm::omp::OMPC_to: {
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 = OMPToClause::CreateEmpty(Context, Sizes);
11940     break;
11941   }
11942   case llvm::omp::OMPC_from: {
11943     OMPMappableExprListSizeTy Sizes;
11944     Sizes.NumVars = Record.readInt();
11945     Sizes.NumUniqueDeclarations = Record.readInt();
11946     Sizes.NumComponentLists = Record.readInt();
11947     Sizes.NumComponents = Record.readInt();
11948     C = OMPFromClause::CreateEmpty(Context, Sizes);
11949     break;
11950   }
11951   case llvm::omp::OMPC_use_device_ptr: {
11952     OMPMappableExprListSizeTy Sizes;
11953     Sizes.NumVars = Record.readInt();
11954     Sizes.NumUniqueDeclarations = Record.readInt();
11955     Sizes.NumComponentLists = Record.readInt();
11956     Sizes.NumComponents = Record.readInt();
11957     C = OMPUseDevicePtrClause::CreateEmpty(Context, Sizes);
11958     break;
11959   }
11960   case llvm::omp::OMPC_use_device_addr: {
11961     OMPMappableExprListSizeTy Sizes;
11962     Sizes.NumVars = Record.readInt();
11963     Sizes.NumUniqueDeclarations = Record.readInt();
11964     Sizes.NumComponentLists = Record.readInt();
11965     Sizes.NumComponents = Record.readInt();
11966     C = OMPUseDeviceAddrClause::CreateEmpty(Context, Sizes);
11967     break;
11968   }
11969   case llvm::omp::OMPC_is_device_ptr: {
11970     OMPMappableExprListSizeTy Sizes;
11971     Sizes.NumVars = Record.readInt();
11972     Sizes.NumUniqueDeclarations = Record.readInt();
11973     Sizes.NumComponentLists = Record.readInt();
11974     Sizes.NumComponents = Record.readInt();
11975     C = OMPIsDevicePtrClause::CreateEmpty(Context, Sizes);
11976     break;
11977   }
11978   case llvm::omp::OMPC_allocate:
11979     C = OMPAllocateClause::CreateEmpty(Context, Record.readInt());
11980     break;
11981   case llvm::omp::OMPC_nontemporal:
11982     C = OMPNontemporalClause::CreateEmpty(Context, Record.readInt());
11983     break;
11984   case llvm::omp::OMPC_inclusive:
11985     C = OMPInclusiveClause::CreateEmpty(Context, Record.readInt());
11986     break;
11987   case llvm::omp::OMPC_exclusive:
11988     C = OMPExclusiveClause::CreateEmpty(Context, Record.readInt());
11989     break;
11990   case llvm::omp::OMPC_order:
11991     C = new (Context) OMPOrderClause();
11992     break;
11993   case llvm::omp::OMPC_destroy:
11994     C = new (Context) OMPDestroyClause();
11995     break;
11996   case llvm::omp::OMPC_detach:
11997     C = new (Context) OMPDetachClause();
11998     break;
11999   case llvm::omp::OMPC_uses_allocators:
12000     C = OMPUsesAllocatorsClause::CreateEmpty(Context, Record.readInt());
12001     break;
12002   case llvm::omp::OMPC_affinity:
12003     C = OMPAffinityClause::CreateEmpty(Context, Record.readInt());
12004     break;
12005 #define OMP_CLAUSE_NO_CLASS(Enum, Str)                                         \
12006   case llvm::omp::Enum:                                                        \
12007     break;
12008 #include "llvm/Frontend/OpenMP/OMPKinds.def"
12009   default:
12010     break;
12011   }
12012   assert(C && "Unknown OMPClause type");
12013 
12014   Visit(C);
12015   C->setLocStart(Record.readSourceLocation());
12016   C->setLocEnd(Record.readSourceLocation());
12017 
12018   return C;
12019 }
12020 
12021 void OMPClauseReader::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
12022   C->setPreInitStmt(Record.readSubStmt(),
12023                     static_cast<OpenMPDirectiveKind>(Record.readInt()));
12024 }
12025 
12026 void OMPClauseReader::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
12027   VisitOMPClauseWithPreInit(C);
12028   C->setPostUpdateExpr(Record.readSubExpr());
12029 }
12030 
12031 void OMPClauseReader::VisitOMPIfClause(OMPIfClause *C) {
12032   VisitOMPClauseWithPreInit(C);
12033   C->setNameModifier(static_cast<OpenMPDirectiveKind>(Record.readInt()));
12034   C->setNameModifierLoc(Record.readSourceLocation());
12035   C->setColonLoc(Record.readSourceLocation());
12036   C->setCondition(Record.readSubExpr());
12037   C->setLParenLoc(Record.readSourceLocation());
12038 }
12039 
12040 void OMPClauseReader::VisitOMPFinalClause(OMPFinalClause *C) {
12041   VisitOMPClauseWithPreInit(C);
12042   C->setCondition(Record.readSubExpr());
12043   C->setLParenLoc(Record.readSourceLocation());
12044 }
12045 
12046 void OMPClauseReader::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
12047   VisitOMPClauseWithPreInit(C);
12048   C->setNumThreads(Record.readSubExpr());
12049   C->setLParenLoc(Record.readSourceLocation());
12050 }
12051 
12052 void OMPClauseReader::VisitOMPSafelenClause(OMPSafelenClause *C) {
12053   C->setSafelen(Record.readSubExpr());
12054   C->setLParenLoc(Record.readSourceLocation());
12055 }
12056 
12057 void OMPClauseReader::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
12058   C->setSimdlen(Record.readSubExpr());
12059   C->setLParenLoc(Record.readSourceLocation());
12060 }
12061 
12062 void OMPClauseReader::VisitOMPAllocatorClause(OMPAllocatorClause *C) {
12063   C->setAllocator(Record.readExpr());
12064   C->setLParenLoc(Record.readSourceLocation());
12065 }
12066 
12067 void OMPClauseReader::VisitOMPCollapseClause(OMPCollapseClause *C) {
12068   C->setNumForLoops(Record.readSubExpr());
12069   C->setLParenLoc(Record.readSourceLocation());
12070 }
12071 
12072 void OMPClauseReader::VisitOMPDefaultClause(OMPDefaultClause *C) {
12073   C->setDefaultKind(static_cast<llvm::omp::DefaultKind>(Record.readInt()));
12074   C->setLParenLoc(Record.readSourceLocation());
12075   C->setDefaultKindKwLoc(Record.readSourceLocation());
12076 }
12077 
12078 void OMPClauseReader::VisitOMPProcBindClause(OMPProcBindClause *C) {
12079   C->setProcBindKind(static_cast<llvm::omp::ProcBindKind>(Record.readInt()));
12080   C->setLParenLoc(Record.readSourceLocation());
12081   C->setProcBindKindKwLoc(Record.readSourceLocation());
12082 }
12083 
12084 void OMPClauseReader::VisitOMPScheduleClause(OMPScheduleClause *C) {
12085   VisitOMPClauseWithPreInit(C);
12086   C->setScheduleKind(
12087        static_cast<OpenMPScheduleClauseKind>(Record.readInt()));
12088   C->setFirstScheduleModifier(
12089       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12090   C->setSecondScheduleModifier(
12091       static_cast<OpenMPScheduleClauseModifier>(Record.readInt()));
12092   C->setChunkSize(Record.readSubExpr());
12093   C->setLParenLoc(Record.readSourceLocation());
12094   C->setFirstScheduleModifierLoc(Record.readSourceLocation());
12095   C->setSecondScheduleModifierLoc(Record.readSourceLocation());
12096   C->setScheduleKindLoc(Record.readSourceLocation());
12097   C->setCommaLoc(Record.readSourceLocation());
12098 }
12099 
12100 void OMPClauseReader::VisitOMPOrderedClause(OMPOrderedClause *C) {
12101   C->setNumForLoops(Record.readSubExpr());
12102   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12103     C->setLoopNumIterations(I, Record.readSubExpr());
12104   for (unsigned I = 0, E = C->NumberOfLoops; I < E; ++I)
12105     C->setLoopCounter(I, Record.readSubExpr());
12106   C->setLParenLoc(Record.readSourceLocation());
12107 }
12108 
12109 void OMPClauseReader::VisitOMPDetachClause(OMPDetachClause *C) {
12110   C->setEventHandler(Record.readSubExpr());
12111   C->setLParenLoc(Record.readSourceLocation());
12112 }
12113 
12114 void OMPClauseReader::VisitOMPNowaitClause(OMPNowaitClause *) {}
12115 
12116 void OMPClauseReader::VisitOMPUntiedClause(OMPUntiedClause *) {}
12117 
12118 void OMPClauseReader::VisitOMPMergeableClause(OMPMergeableClause *) {}
12119 
12120 void OMPClauseReader::VisitOMPReadClause(OMPReadClause *) {}
12121 
12122 void OMPClauseReader::VisitOMPWriteClause(OMPWriteClause *) {}
12123 
12124 void OMPClauseReader::VisitOMPUpdateClause(OMPUpdateClause *C) {
12125   if (C->isExtended()) {
12126     C->setLParenLoc(Record.readSourceLocation());
12127     C->setArgumentLoc(Record.readSourceLocation());
12128     C->setDependencyKind(Record.readEnum<OpenMPDependClauseKind>());
12129   }
12130 }
12131 
12132 void OMPClauseReader::VisitOMPCaptureClause(OMPCaptureClause *) {}
12133 
12134 void OMPClauseReader::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
12135 
12136 void OMPClauseReader::VisitOMPAcqRelClause(OMPAcqRelClause *) {}
12137 
12138 void OMPClauseReader::VisitOMPAcquireClause(OMPAcquireClause *) {}
12139 
12140 void OMPClauseReader::VisitOMPReleaseClause(OMPReleaseClause *) {}
12141 
12142 void OMPClauseReader::VisitOMPRelaxedClause(OMPRelaxedClause *) {}
12143 
12144 void OMPClauseReader::VisitOMPThreadsClause(OMPThreadsClause *) {}
12145 
12146 void OMPClauseReader::VisitOMPSIMDClause(OMPSIMDClause *) {}
12147 
12148 void OMPClauseReader::VisitOMPNogroupClause(OMPNogroupClause *) {}
12149 
12150 void OMPClauseReader::VisitOMPDestroyClause(OMPDestroyClause *) {}
12151 
12152 void OMPClauseReader::VisitOMPUnifiedAddressClause(OMPUnifiedAddressClause *) {}
12153 
12154 void OMPClauseReader::VisitOMPUnifiedSharedMemoryClause(
12155     OMPUnifiedSharedMemoryClause *) {}
12156 
12157 void OMPClauseReader::VisitOMPReverseOffloadClause(OMPReverseOffloadClause *) {}
12158 
12159 void
12160 OMPClauseReader::VisitOMPDynamicAllocatorsClause(OMPDynamicAllocatorsClause *) {
12161 }
12162 
12163 void OMPClauseReader::VisitOMPAtomicDefaultMemOrderClause(
12164     OMPAtomicDefaultMemOrderClause *C) {
12165   C->setAtomicDefaultMemOrderKind(
12166       static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Record.readInt()));
12167   C->setLParenLoc(Record.readSourceLocation());
12168   C->setAtomicDefaultMemOrderKindKwLoc(Record.readSourceLocation());
12169 }
12170 
12171 void OMPClauseReader::VisitOMPPrivateClause(OMPPrivateClause *C) {
12172   C->setLParenLoc(Record.readSourceLocation());
12173   unsigned NumVars = C->varlist_size();
12174   SmallVector<Expr *, 16> Vars;
12175   Vars.reserve(NumVars);
12176   for (unsigned i = 0; i != NumVars; ++i)
12177     Vars.push_back(Record.readSubExpr());
12178   C->setVarRefs(Vars);
12179   Vars.clear();
12180   for (unsigned i = 0; i != NumVars; ++i)
12181     Vars.push_back(Record.readSubExpr());
12182   C->setPrivateCopies(Vars);
12183 }
12184 
12185 void OMPClauseReader::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
12186   VisitOMPClauseWithPreInit(C);
12187   C->setLParenLoc(Record.readSourceLocation());
12188   unsigned NumVars = C->varlist_size();
12189   SmallVector<Expr *, 16> Vars;
12190   Vars.reserve(NumVars);
12191   for (unsigned i = 0; i != NumVars; ++i)
12192     Vars.push_back(Record.readSubExpr());
12193   C->setVarRefs(Vars);
12194   Vars.clear();
12195   for (unsigned i = 0; i != NumVars; ++i)
12196     Vars.push_back(Record.readSubExpr());
12197   C->setPrivateCopies(Vars);
12198   Vars.clear();
12199   for (unsigned i = 0; i != NumVars; ++i)
12200     Vars.push_back(Record.readSubExpr());
12201   C->setInits(Vars);
12202 }
12203 
12204 void OMPClauseReader::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
12205   VisitOMPClauseWithPostUpdate(C);
12206   C->setLParenLoc(Record.readSourceLocation());
12207   C->setKind(Record.readEnum<OpenMPLastprivateModifier>());
12208   C->setKindLoc(Record.readSourceLocation());
12209   C->setColonLoc(Record.readSourceLocation());
12210   unsigned NumVars = C->varlist_size();
12211   SmallVector<Expr *, 16> Vars;
12212   Vars.reserve(NumVars);
12213   for (unsigned i = 0; i != NumVars; ++i)
12214     Vars.push_back(Record.readSubExpr());
12215   C->setVarRefs(Vars);
12216   Vars.clear();
12217   for (unsigned i = 0; i != NumVars; ++i)
12218     Vars.push_back(Record.readSubExpr());
12219   C->setPrivateCopies(Vars);
12220   Vars.clear();
12221   for (unsigned i = 0; i != NumVars; ++i)
12222     Vars.push_back(Record.readSubExpr());
12223   C->setSourceExprs(Vars);
12224   Vars.clear();
12225   for (unsigned i = 0; i != NumVars; ++i)
12226     Vars.push_back(Record.readSubExpr());
12227   C->setDestinationExprs(Vars);
12228   Vars.clear();
12229   for (unsigned i = 0; i != NumVars; ++i)
12230     Vars.push_back(Record.readSubExpr());
12231   C->setAssignmentOps(Vars);
12232 }
12233 
12234 void OMPClauseReader::VisitOMPSharedClause(OMPSharedClause *C) {
12235   C->setLParenLoc(Record.readSourceLocation());
12236   unsigned NumVars = C->varlist_size();
12237   SmallVector<Expr *, 16> Vars;
12238   Vars.reserve(NumVars);
12239   for (unsigned i = 0; i != NumVars; ++i)
12240     Vars.push_back(Record.readSubExpr());
12241   C->setVarRefs(Vars);
12242 }
12243 
12244 void OMPClauseReader::VisitOMPReductionClause(OMPReductionClause *C) {
12245   VisitOMPClauseWithPostUpdate(C);
12246   C->setLParenLoc(Record.readSourceLocation());
12247   C->setModifierLoc(Record.readSourceLocation());
12248   C->setColonLoc(Record.readSourceLocation());
12249   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12250   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12251   C->setQualifierLoc(NNSL);
12252   C->setNameInfo(DNI);
12253 
12254   unsigned NumVars = C->varlist_size();
12255   SmallVector<Expr *, 16> Vars;
12256   Vars.reserve(NumVars);
12257   for (unsigned i = 0; i != NumVars; ++i)
12258     Vars.push_back(Record.readSubExpr());
12259   C->setVarRefs(Vars);
12260   Vars.clear();
12261   for (unsigned i = 0; i != NumVars; ++i)
12262     Vars.push_back(Record.readSubExpr());
12263   C->setPrivates(Vars);
12264   Vars.clear();
12265   for (unsigned i = 0; i != NumVars; ++i)
12266     Vars.push_back(Record.readSubExpr());
12267   C->setLHSExprs(Vars);
12268   Vars.clear();
12269   for (unsigned i = 0; i != NumVars; ++i)
12270     Vars.push_back(Record.readSubExpr());
12271   C->setRHSExprs(Vars);
12272   Vars.clear();
12273   for (unsigned i = 0; i != NumVars; ++i)
12274     Vars.push_back(Record.readSubExpr());
12275   C->setReductionOps(Vars);
12276   if (C->getModifier() == OMPC_REDUCTION_inscan) {
12277     Vars.clear();
12278     for (unsigned i = 0; i != NumVars; ++i)
12279       Vars.push_back(Record.readSubExpr());
12280     C->setInscanCopyOps(Vars);
12281     Vars.clear();
12282     for (unsigned i = 0; i != NumVars; ++i)
12283       Vars.push_back(Record.readSubExpr());
12284     C->setInscanCopyArrayTemps(Vars);
12285     Vars.clear();
12286     for (unsigned i = 0; i != NumVars; ++i)
12287       Vars.push_back(Record.readSubExpr());
12288     C->setInscanCopyArrayElems(Vars);
12289   }
12290 }
12291 
12292 void OMPClauseReader::VisitOMPTaskReductionClause(OMPTaskReductionClause *C) {
12293   VisitOMPClauseWithPostUpdate(C);
12294   C->setLParenLoc(Record.readSourceLocation());
12295   C->setColonLoc(Record.readSourceLocation());
12296   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12297   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12298   C->setQualifierLoc(NNSL);
12299   C->setNameInfo(DNI);
12300 
12301   unsigned NumVars = C->varlist_size();
12302   SmallVector<Expr *, 16> Vars;
12303   Vars.reserve(NumVars);
12304   for (unsigned I = 0; I != NumVars; ++I)
12305     Vars.push_back(Record.readSubExpr());
12306   C->setVarRefs(Vars);
12307   Vars.clear();
12308   for (unsigned I = 0; I != NumVars; ++I)
12309     Vars.push_back(Record.readSubExpr());
12310   C->setPrivates(Vars);
12311   Vars.clear();
12312   for (unsigned I = 0; I != NumVars; ++I)
12313     Vars.push_back(Record.readSubExpr());
12314   C->setLHSExprs(Vars);
12315   Vars.clear();
12316   for (unsigned I = 0; I != NumVars; ++I)
12317     Vars.push_back(Record.readSubExpr());
12318   C->setRHSExprs(Vars);
12319   Vars.clear();
12320   for (unsigned I = 0; I != NumVars; ++I)
12321     Vars.push_back(Record.readSubExpr());
12322   C->setReductionOps(Vars);
12323 }
12324 
12325 void OMPClauseReader::VisitOMPInReductionClause(OMPInReductionClause *C) {
12326   VisitOMPClauseWithPostUpdate(C);
12327   C->setLParenLoc(Record.readSourceLocation());
12328   C->setColonLoc(Record.readSourceLocation());
12329   NestedNameSpecifierLoc NNSL = Record.readNestedNameSpecifierLoc();
12330   DeclarationNameInfo DNI = Record.readDeclarationNameInfo();
12331   C->setQualifierLoc(NNSL);
12332   C->setNameInfo(DNI);
12333 
12334   unsigned NumVars = C->varlist_size();
12335   SmallVector<Expr *, 16> Vars;
12336   Vars.reserve(NumVars);
12337   for (unsigned I = 0; I != NumVars; ++I)
12338     Vars.push_back(Record.readSubExpr());
12339   C->setVarRefs(Vars);
12340   Vars.clear();
12341   for (unsigned I = 0; I != NumVars; ++I)
12342     Vars.push_back(Record.readSubExpr());
12343   C->setPrivates(Vars);
12344   Vars.clear();
12345   for (unsigned I = 0; I != NumVars; ++I)
12346     Vars.push_back(Record.readSubExpr());
12347   C->setLHSExprs(Vars);
12348   Vars.clear();
12349   for (unsigned I = 0; I != NumVars; ++I)
12350     Vars.push_back(Record.readSubExpr());
12351   C->setRHSExprs(Vars);
12352   Vars.clear();
12353   for (unsigned I = 0; I != NumVars; ++I)
12354     Vars.push_back(Record.readSubExpr());
12355   C->setReductionOps(Vars);
12356   Vars.clear();
12357   for (unsigned I = 0; I != NumVars; ++I)
12358     Vars.push_back(Record.readSubExpr());
12359   C->setTaskgroupDescriptors(Vars);
12360 }
12361 
12362 void OMPClauseReader::VisitOMPLinearClause(OMPLinearClause *C) {
12363   VisitOMPClauseWithPostUpdate(C);
12364   C->setLParenLoc(Record.readSourceLocation());
12365   C->setColonLoc(Record.readSourceLocation());
12366   C->setModifier(static_cast<OpenMPLinearClauseKind>(Record.readInt()));
12367   C->setModifierLoc(Record.readSourceLocation());
12368   unsigned NumVars = C->varlist_size();
12369   SmallVector<Expr *, 16> Vars;
12370   Vars.reserve(NumVars);
12371   for (unsigned i = 0; i != NumVars; ++i)
12372     Vars.push_back(Record.readSubExpr());
12373   C->setVarRefs(Vars);
12374   Vars.clear();
12375   for (unsigned i = 0; i != NumVars; ++i)
12376     Vars.push_back(Record.readSubExpr());
12377   C->setPrivates(Vars);
12378   Vars.clear();
12379   for (unsigned i = 0; i != NumVars; ++i)
12380     Vars.push_back(Record.readSubExpr());
12381   C->setInits(Vars);
12382   Vars.clear();
12383   for (unsigned i = 0; i != NumVars; ++i)
12384     Vars.push_back(Record.readSubExpr());
12385   C->setUpdates(Vars);
12386   Vars.clear();
12387   for (unsigned i = 0; i != NumVars; ++i)
12388     Vars.push_back(Record.readSubExpr());
12389   C->setFinals(Vars);
12390   C->setStep(Record.readSubExpr());
12391   C->setCalcStep(Record.readSubExpr());
12392   Vars.clear();
12393   for (unsigned I = 0; I != NumVars + 1; ++I)
12394     Vars.push_back(Record.readSubExpr());
12395   C->setUsedExprs(Vars);
12396 }
12397 
12398 void OMPClauseReader::VisitOMPAlignedClause(OMPAlignedClause *C) {
12399   C->setLParenLoc(Record.readSourceLocation());
12400   C->setColonLoc(Record.readSourceLocation());
12401   unsigned NumVars = C->varlist_size();
12402   SmallVector<Expr *, 16> Vars;
12403   Vars.reserve(NumVars);
12404   for (unsigned i = 0; i != NumVars; ++i)
12405     Vars.push_back(Record.readSubExpr());
12406   C->setVarRefs(Vars);
12407   C->setAlignment(Record.readSubExpr());
12408 }
12409 
12410 void OMPClauseReader::VisitOMPCopyinClause(OMPCopyinClause *C) {
12411   C->setLParenLoc(Record.readSourceLocation());
12412   unsigned NumVars = C->varlist_size();
12413   SmallVector<Expr *, 16> Exprs;
12414   Exprs.reserve(NumVars);
12415   for (unsigned i = 0; i != NumVars; ++i)
12416     Exprs.push_back(Record.readSubExpr());
12417   C->setVarRefs(Exprs);
12418   Exprs.clear();
12419   for (unsigned i = 0; i != NumVars; ++i)
12420     Exprs.push_back(Record.readSubExpr());
12421   C->setSourceExprs(Exprs);
12422   Exprs.clear();
12423   for (unsigned i = 0; i != NumVars; ++i)
12424     Exprs.push_back(Record.readSubExpr());
12425   C->setDestinationExprs(Exprs);
12426   Exprs.clear();
12427   for (unsigned i = 0; i != NumVars; ++i)
12428     Exprs.push_back(Record.readSubExpr());
12429   C->setAssignmentOps(Exprs);
12430 }
12431 
12432 void OMPClauseReader::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
12433   C->setLParenLoc(Record.readSourceLocation());
12434   unsigned NumVars = C->varlist_size();
12435   SmallVector<Expr *, 16> Exprs;
12436   Exprs.reserve(NumVars);
12437   for (unsigned i = 0; i != NumVars; ++i)
12438     Exprs.push_back(Record.readSubExpr());
12439   C->setVarRefs(Exprs);
12440   Exprs.clear();
12441   for (unsigned i = 0; i != NumVars; ++i)
12442     Exprs.push_back(Record.readSubExpr());
12443   C->setSourceExprs(Exprs);
12444   Exprs.clear();
12445   for (unsigned i = 0; i != NumVars; ++i)
12446     Exprs.push_back(Record.readSubExpr());
12447   C->setDestinationExprs(Exprs);
12448   Exprs.clear();
12449   for (unsigned i = 0; i != NumVars; ++i)
12450     Exprs.push_back(Record.readSubExpr());
12451   C->setAssignmentOps(Exprs);
12452 }
12453 
12454 void OMPClauseReader::VisitOMPFlushClause(OMPFlushClause *C) {
12455   C->setLParenLoc(Record.readSourceLocation());
12456   unsigned NumVars = C->varlist_size();
12457   SmallVector<Expr *, 16> Vars;
12458   Vars.reserve(NumVars);
12459   for (unsigned i = 0; i != NumVars; ++i)
12460     Vars.push_back(Record.readSubExpr());
12461   C->setVarRefs(Vars);
12462 }
12463 
12464 void OMPClauseReader::VisitOMPDepobjClause(OMPDepobjClause *C) {
12465   C->setDepobj(Record.readSubExpr());
12466   C->setLParenLoc(Record.readSourceLocation());
12467 }
12468 
12469 void OMPClauseReader::VisitOMPDependClause(OMPDependClause *C) {
12470   C->setLParenLoc(Record.readSourceLocation());
12471   C->setModifier(Record.readSubExpr());
12472   C->setDependencyKind(
12473       static_cast<OpenMPDependClauseKind>(Record.readInt()));
12474   C->setDependencyLoc(Record.readSourceLocation());
12475   C->setColonLoc(Record.readSourceLocation());
12476   unsigned NumVars = C->varlist_size();
12477   SmallVector<Expr *, 16> Vars;
12478   Vars.reserve(NumVars);
12479   for (unsigned I = 0; I != NumVars; ++I)
12480     Vars.push_back(Record.readSubExpr());
12481   C->setVarRefs(Vars);
12482   for (unsigned I = 0, E = C->getNumLoops(); I < E; ++I)
12483     C->setLoopData(I, Record.readSubExpr());
12484 }
12485 
12486 void OMPClauseReader::VisitOMPDeviceClause(OMPDeviceClause *C) {
12487   VisitOMPClauseWithPreInit(C);
12488   C->setModifier(Record.readEnum<OpenMPDeviceClauseModifier>());
12489   C->setDevice(Record.readSubExpr());
12490   C->setModifierLoc(Record.readSourceLocation());
12491   C->setLParenLoc(Record.readSourceLocation());
12492 }
12493 
12494 void OMPClauseReader::VisitOMPMapClause(OMPMapClause *C) {
12495   C->setLParenLoc(Record.readSourceLocation());
12496   for (unsigned I = 0; I < NumberOfOMPMapClauseModifiers; ++I) {
12497     C->setMapTypeModifier(
12498         I, static_cast<OpenMPMapModifierKind>(Record.readInt()));
12499     C->setMapTypeModifierLoc(I, Record.readSourceLocation());
12500   }
12501   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12502   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12503   C->setMapType(
12504      static_cast<OpenMPMapClauseKind>(Record.readInt()));
12505   C->setMapLoc(Record.readSourceLocation());
12506   C->setColonLoc(Record.readSourceLocation());
12507   auto NumVars = C->varlist_size();
12508   auto UniqueDecls = C->getUniqueDeclarationsNum();
12509   auto TotalLists = C->getTotalComponentListNum();
12510   auto TotalComponents = C->getTotalComponentsNum();
12511 
12512   SmallVector<Expr *, 16> Vars;
12513   Vars.reserve(NumVars);
12514   for (unsigned i = 0; i != NumVars; ++i)
12515     Vars.push_back(Record.readExpr());
12516   C->setVarRefs(Vars);
12517 
12518   SmallVector<Expr *, 16> UDMappers;
12519   UDMappers.reserve(NumVars);
12520   for (unsigned I = 0; I < NumVars; ++I)
12521     UDMappers.push_back(Record.readExpr());
12522   C->setUDMapperRefs(UDMappers);
12523 
12524   SmallVector<ValueDecl *, 16> Decls;
12525   Decls.reserve(UniqueDecls);
12526   for (unsigned i = 0; i < UniqueDecls; ++i)
12527     Decls.push_back(Record.readDeclAs<ValueDecl>());
12528   C->setUniqueDecls(Decls);
12529 
12530   SmallVector<unsigned, 16> ListsPerDecl;
12531   ListsPerDecl.reserve(UniqueDecls);
12532   for (unsigned i = 0; i < UniqueDecls; ++i)
12533     ListsPerDecl.push_back(Record.readInt());
12534   C->setDeclNumLists(ListsPerDecl);
12535 
12536   SmallVector<unsigned, 32> ListSizes;
12537   ListSizes.reserve(TotalLists);
12538   for (unsigned i = 0; i < TotalLists; ++i)
12539     ListSizes.push_back(Record.readInt());
12540   C->setComponentListSizes(ListSizes);
12541 
12542   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12543   Components.reserve(TotalComponents);
12544   for (unsigned i = 0; i < TotalComponents; ++i) {
12545     Expr *AssociatedExpr = Record.readExpr();
12546     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12547     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12548         AssociatedExpr, AssociatedDecl));
12549   }
12550   C->setComponents(Components, ListSizes);
12551 }
12552 
12553 void OMPClauseReader::VisitOMPAllocateClause(OMPAllocateClause *C) {
12554   C->setLParenLoc(Record.readSourceLocation());
12555   C->setColonLoc(Record.readSourceLocation());
12556   C->setAllocator(Record.readSubExpr());
12557   unsigned NumVars = C->varlist_size();
12558   SmallVector<Expr *, 16> Vars;
12559   Vars.reserve(NumVars);
12560   for (unsigned i = 0; i != NumVars; ++i)
12561     Vars.push_back(Record.readSubExpr());
12562   C->setVarRefs(Vars);
12563 }
12564 
12565 void OMPClauseReader::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
12566   VisitOMPClauseWithPreInit(C);
12567   C->setNumTeams(Record.readSubExpr());
12568   C->setLParenLoc(Record.readSourceLocation());
12569 }
12570 
12571 void OMPClauseReader::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
12572   VisitOMPClauseWithPreInit(C);
12573   C->setThreadLimit(Record.readSubExpr());
12574   C->setLParenLoc(Record.readSourceLocation());
12575 }
12576 
12577 void OMPClauseReader::VisitOMPPriorityClause(OMPPriorityClause *C) {
12578   VisitOMPClauseWithPreInit(C);
12579   C->setPriority(Record.readSubExpr());
12580   C->setLParenLoc(Record.readSourceLocation());
12581 }
12582 
12583 void OMPClauseReader::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
12584   VisitOMPClauseWithPreInit(C);
12585   C->setGrainsize(Record.readSubExpr());
12586   C->setLParenLoc(Record.readSourceLocation());
12587 }
12588 
12589 void OMPClauseReader::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
12590   VisitOMPClauseWithPreInit(C);
12591   C->setNumTasks(Record.readSubExpr());
12592   C->setLParenLoc(Record.readSourceLocation());
12593 }
12594 
12595 void OMPClauseReader::VisitOMPHintClause(OMPHintClause *C) {
12596   C->setHint(Record.readSubExpr());
12597   C->setLParenLoc(Record.readSourceLocation());
12598 }
12599 
12600 void OMPClauseReader::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
12601   VisitOMPClauseWithPreInit(C);
12602   C->setDistScheduleKind(
12603       static_cast<OpenMPDistScheduleClauseKind>(Record.readInt()));
12604   C->setChunkSize(Record.readSubExpr());
12605   C->setLParenLoc(Record.readSourceLocation());
12606   C->setDistScheduleKindLoc(Record.readSourceLocation());
12607   C->setCommaLoc(Record.readSourceLocation());
12608 }
12609 
12610 void OMPClauseReader::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
12611   C->setDefaultmapKind(
12612        static_cast<OpenMPDefaultmapClauseKind>(Record.readInt()));
12613   C->setDefaultmapModifier(
12614       static_cast<OpenMPDefaultmapClauseModifier>(Record.readInt()));
12615   C->setLParenLoc(Record.readSourceLocation());
12616   C->setDefaultmapModifierLoc(Record.readSourceLocation());
12617   C->setDefaultmapKindLoc(Record.readSourceLocation());
12618 }
12619 
12620 void OMPClauseReader::VisitOMPToClause(OMPToClause *C) {
12621   C->setLParenLoc(Record.readSourceLocation());
12622   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12623     C->setMotionModifier(
12624         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12625     C->setMotionModifierLoc(I, Record.readSourceLocation());
12626   }
12627   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12628   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12629   C->setColonLoc(Record.readSourceLocation());
12630   auto NumVars = C->varlist_size();
12631   auto UniqueDecls = C->getUniqueDeclarationsNum();
12632   auto TotalLists = C->getTotalComponentListNum();
12633   auto TotalComponents = C->getTotalComponentsNum();
12634 
12635   SmallVector<Expr *, 16> Vars;
12636   Vars.reserve(NumVars);
12637   for (unsigned i = 0; i != NumVars; ++i)
12638     Vars.push_back(Record.readSubExpr());
12639   C->setVarRefs(Vars);
12640 
12641   SmallVector<Expr *, 16> UDMappers;
12642   UDMappers.reserve(NumVars);
12643   for (unsigned I = 0; I < NumVars; ++I)
12644     UDMappers.push_back(Record.readSubExpr());
12645   C->setUDMapperRefs(UDMappers);
12646 
12647   SmallVector<ValueDecl *, 16> Decls;
12648   Decls.reserve(UniqueDecls);
12649   for (unsigned i = 0; i < UniqueDecls; ++i)
12650     Decls.push_back(Record.readDeclAs<ValueDecl>());
12651   C->setUniqueDecls(Decls);
12652 
12653   SmallVector<unsigned, 16> ListsPerDecl;
12654   ListsPerDecl.reserve(UniqueDecls);
12655   for (unsigned i = 0; i < UniqueDecls; ++i)
12656     ListsPerDecl.push_back(Record.readInt());
12657   C->setDeclNumLists(ListsPerDecl);
12658 
12659   SmallVector<unsigned, 32> ListSizes;
12660   ListSizes.reserve(TotalLists);
12661   for (unsigned i = 0; i < TotalLists; ++i)
12662     ListSizes.push_back(Record.readInt());
12663   C->setComponentListSizes(ListSizes);
12664 
12665   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12666   Components.reserve(TotalComponents);
12667   for (unsigned i = 0; i < TotalComponents; ++i) {
12668     Expr *AssociatedExpr = Record.readSubExpr();
12669     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12670     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12671         AssociatedExpr, AssociatedDecl));
12672   }
12673   C->setComponents(Components, ListSizes);
12674 }
12675 
12676 void OMPClauseReader::VisitOMPFromClause(OMPFromClause *C) {
12677   C->setLParenLoc(Record.readSourceLocation());
12678   for (unsigned I = 0; I < NumberOfOMPMotionModifiers; ++I) {
12679     C->setMotionModifier(
12680         I, static_cast<OpenMPMotionModifierKind>(Record.readInt()));
12681     C->setMotionModifierLoc(I, Record.readSourceLocation());
12682   }
12683   C->setMapperQualifierLoc(Record.readNestedNameSpecifierLoc());
12684   C->setMapperIdInfo(Record.readDeclarationNameInfo());
12685   C->setColonLoc(Record.readSourceLocation());
12686   auto NumVars = C->varlist_size();
12687   auto UniqueDecls = C->getUniqueDeclarationsNum();
12688   auto TotalLists = C->getTotalComponentListNum();
12689   auto TotalComponents = C->getTotalComponentsNum();
12690 
12691   SmallVector<Expr *, 16> Vars;
12692   Vars.reserve(NumVars);
12693   for (unsigned i = 0; i != NumVars; ++i)
12694     Vars.push_back(Record.readSubExpr());
12695   C->setVarRefs(Vars);
12696 
12697   SmallVector<Expr *, 16> UDMappers;
12698   UDMappers.reserve(NumVars);
12699   for (unsigned I = 0; I < NumVars; ++I)
12700     UDMappers.push_back(Record.readSubExpr());
12701   C->setUDMapperRefs(UDMappers);
12702 
12703   SmallVector<ValueDecl *, 16> Decls;
12704   Decls.reserve(UniqueDecls);
12705   for (unsigned i = 0; i < UniqueDecls; ++i)
12706     Decls.push_back(Record.readDeclAs<ValueDecl>());
12707   C->setUniqueDecls(Decls);
12708 
12709   SmallVector<unsigned, 16> ListsPerDecl;
12710   ListsPerDecl.reserve(UniqueDecls);
12711   for (unsigned i = 0; i < UniqueDecls; ++i)
12712     ListsPerDecl.push_back(Record.readInt());
12713   C->setDeclNumLists(ListsPerDecl);
12714 
12715   SmallVector<unsigned, 32> ListSizes;
12716   ListSizes.reserve(TotalLists);
12717   for (unsigned i = 0; i < TotalLists; ++i)
12718     ListSizes.push_back(Record.readInt());
12719   C->setComponentListSizes(ListSizes);
12720 
12721   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12722   Components.reserve(TotalComponents);
12723   for (unsigned i = 0; i < TotalComponents; ++i) {
12724     Expr *AssociatedExpr = Record.readSubExpr();
12725     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12726     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12727         AssociatedExpr, AssociatedDecl));
12728   }
12729   C->setComponents(Components, ListSizes);
12730 }
12731 
12732 void OMPClauseReader::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
12733   C->setLParenLoc(Record.readSourceLocation());
12734   auto NumVars = C->varlist_size();
12735   auto UniqueDecls = C->getUniqueDeclarationsNum();
12736   auto TotalLists = C->getTotalComponentListNum();
12737   auto TotalComponents = C->getTotalComponentsNum();
12738 
12739   SmallVector<Expr *, 16> Vars;
12740   Vars.reserve(NumVars);
12741   for (unsigned i = 0; i != NumVars; ++i)
12742     Vars.push_back(Record.readSubExpr());
12743   C->setVarRefs(Vars);
12744   Vars.clear();
12745   for (unsigned i = 0; i != NumVars; ++i)
12746     Vars.push_back(Record.readSubExpr());
12747   C->setPrivateCopies(Vars);
12748   Vars.clear();
12749   for (unsigned i = 0; i != NumVars; ++i)
12750     Vars.push_back(Record.readSubExpr());
12751   C->setInits(Vars);
12752 
12753   SmallVector<ValueDecl *, 16> Decls;
12754   Decls.reserve(UniqueDecls);
12755   for (unsigned i = 0; i < UniqueDecls; ++i)
12756     Decls.push_back(Record.readDeclAs<ValueDecl>());
12757   C->setUniqueDecls(Decls);
12758 
12759   SmallVector<unsigned, 16> ListsPerDecl;
12760   ListsPerDecl.reserve(UniqueDecls);
12761   for (unsigned i = 0; i < UniqueDecls; ++i)
12762     ListsPerDecl.push_back(Record.readInt());
12763   C->setDeclNumLists(ListsPerDecl);
12764 
12765   SmallVector<unsigned, 32> ListSizes;
12766   ListSizes.reserve(TotalLists);
12767   for (unsigned i = 0; i < TotalLists; ++i)
12768     ListSizes.push_back(Record.readInt());
12769   C->setComponentListSizes(ListSizes);
12770 
12771   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12772   Components.reserve(TotalComponents);
12773   for (unsigned i = 0; i < TotalComponents; ++i) {
12774     Expr *AssociatedExpr = Record.readSubExpr();
12775     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12776     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12777         AssociatedExpr, AssociatedDecl));
12778   }
12779   C->setComponents(Components, ListSizes);
12780 }
12781 
12782 void OMPClauseReader::VisitOMPUseDeviceAddrClause(OMPUseDeviceAddrClause *C) {
12783   C->setLParenLoc(Record.readSourceLocation());
12784   auto NumVars = C->varlist_size();
12785   auto UniqueDecls = C->getUniqueDeclarationsNum();
12786   auto TotalLists = C->getTotalComponentListNum();
12787   auto TotalComponents = C->getTotalComponentsNum();
12788 
12789   SmallVector<Expr *, 16> Vars;
12790   Vars.reserve(NumVars);
12791   for (unsigned i = 0; i != NumVars; ++i)
12792     Vars.push_back(Record.readSubExpr());
12793   C->setVarRefs(Vars);
12794 
12795   SmallVector<ValueDecl *, 16> Decls;
12796   Decls.reserve(UniqueDecls);
12797   for (unsigned i = 0; i < UniqueDecls; ++i)
12798     Decls.push_back(Record.readDeclAs<ValueDecl>());
12799   C->setUniqueDecls(Decls);
12800 
12801   SmallVector<unsigned, 16> ListsPerDecl;
12802   ListsPerDecl.reserve(UniqueDecls);
12803   for (unsigned i = 0; i < UniqueDecls; ++i)
12804     ListsPerDecl.push_back(Record.readInt());
12805   C->setDeclNumLists(ListsPerDecl);
12806 
12807   SmallVector<unsigned, 32> ListSizes;
12808   ListSizes.reserve(TotalLists);
12809   for (unsigned i = 0; i < TotalLists; ++i)
12810     ListSizes.push_back(Record.readInt());
12811   C->setComponentListSizes(ListSizes);
12812 
12813   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12814   Components.reserve(TotalComponents);
12815   for (unsigned i = 0; i < TotalComponents; ++i) {
12816     Expr *AssociatedExpr = Record.readSubExpr();
12817     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12818     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12819         AssociatedExpr, AssociatedDecl));
12820   }
12821   C->setComponents(Components, ListSizes);
12822 }
12823 
12824 void OMPClauseReader::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
12825   C->setLParenLoc(Record.readSourceLocation());
12826   auto NumVars = C->varlist_size();
12827   auto UniqueDecls = C->getUniqueDeclarationsNum();
12828   auto TotalLists = C->getTotalComponentListNum();
12829   auto TotalComponents = C->getTotalComponentsNum();
12830 
12831   SmallVector<Expr *, 16> Vars;
12832   Vars.reserve(NumVars);
12833   for (unsigned i = 0; i != NumVars; ++i)
12834     Vars.push_back(Record.readSubExpr());
12835   C->setVarRefs(Vars);
12836   Vars.clear();
12837 
12838   SmallVector<ValueDecl *, 16> Decls;
12839   Decls.reserve(UniqueDecls);
12840   for (unsigned i = 0; i < UniqueDecls; ++i)
12841     Decls.push_back(Record.readDeclAs<ValueDecl>());
12842   C->setUniqueDecls(Decls);
12843 
12844   SmallVector<unsigned, 16> ListsPerDecl;
12845   ListsPerDecl.reserve(UniqueDecls);
12846   for (unsigned i = 0; i < UniqueDecls; ++i)
12847     ListsPerDecl.push_back(Record.readInt());
12848   C->setDeclNumLists(ListsPerDecl);
12849 
12850   SmallVector<unsigned, 32> ListSizes;
12851   ListSizes.reserve(TotalLists);
12852   for (unsigned i = 0; i < TotalLists; ++i)
12853     ListSizes.push_back(Record.readInt());
12854   C->setComponentListSizes(ListSizes);
12855 
12856   SmallVector<OMPClauseMappableExprCommon::MappableComponent, 32> Components;
12857   Components.reserve(TotalComponents);
12858   for (unsigned i = 0; i < TotalComponents; ++i) {
12859     Expr *AssociatedExpr = Record.readSubExpr();
12860     auto *AssociatedDecl = Record.readDeclAs<ValueDecl>();
12861     Components.push_back(OMPClauseMappableExprCommon::MappableComponent(
12862         AssociatedExpr, AssociatedDecl));
12863   }
12864   C->setComponents(Components, ListSizes);
12865 }
12866 
12867 void OMPClauseReader::VisitOMPNontemporalClause(OMPNontemporalClause *C) {
12868   C->setLParenLoc(Record.readSourceLocation());
12869   unsigned NumVars = C->varlist_size();
12870   SmallVector<Expr *, 16> Vars;
12871   Vars.reserve(NumVars);
12872   for (unsigned i = 0; i != NumVars; ++i)
12873     Vars.push_back(Record.readSubExpr());
12874   C->setVarRefs(Vars);
12875   Vars.clear();
12876   Vars.reserve(NumVars);
12877   for (unsigned i = 0; i != NumVars; ++i)
12878     Vars.push_back(Record.readSubExpr());
12879   C->setPrivateRefs(Vars);
12880 }
12881 
12882 void OMPClauseReader::VisitOMPInclusiveClause(OMPInclusiveClause *C) {
12883   C->setLParenLoc(Record.readSourceLocation());
12884   unsigned NumVars = C->varlist_size();
12885   SmallVector<Expr *, 16> Vars;
12886   Vars.reserve(NumVars);
12887   for (unsigned i = 0; i != NumVars; ++i)
12888     Vars.push_back(Record.readSubExpr());
12889   C->setVarRefs(Vars);
12890 }
12891 
12892 void OMPClauseReader::VisitOMPExclusiveClause(OMPExclusiveClause *C) {
12893   C->setLParenLoc(Record.readSourceLocation());
12894   unsigned NumVars = C->varlist_size();
12895   SmallVector<Expr *, 16> Vars;
12896   Vars.reserve(NumVars);
12897   for (unsigned i = 0; i != NumVars; ++i)
12898     Vars.push_back(Record.readSubExpr());
12899   C->setVarRefs(Vars);
12900 }
12901 
12902 void OMPClauseReader::VisitOMPUsesAllocatorsClause(OMPUsesAllocatorsClause *C) {
12903   C->setLParenLoc(Record.readSourceLocation());
12904   unsigned NumOfAllocators = C->getNumberOfAllocators();
12905   SmallVector<OMPUsesAllocatorsClause::Data, 4> Data;
12906   Data.reserve(NumOfAllocators);
12907   for (unsigned I = 0; I != NumOfAllocators; ++I) {
12908     OMPUsesAllocatorsClause::Data &D = Data.emplace_back();
12909     D.Allocator = Record.readSubExpr();
12910     D.AllocatorTraits = Record.readSubExpr();
12911     D.LParenLoc = Record.readSourceLocation();
12912     D.RParenLoc = Record.readSourceLocation();
12913   }
12914   C->setAllocatorsData(Data);
12915 }
12916 
12917 void OMPClauseReader::VisitOMPAffinityClause(OMPAffinityClause *C) {
12918   C->setLParenLoc(Record.readSourceLocation());
12919   C->setModifier(Record.readSubExpr());
12920   C->setColonLoc(Record.readSourceLocation());
12921   unsigned NumOfLocators = C->varlist_size();
12922   SmallVector<Expr *, 4> Locators;
12923   Locators.reserve(NumOfLocators);
12924   for (unsigned I = 0; I != NumOfLocators; ++I)
12925     Locators.push_back(Record.readSubExpr());
12926   C->setVarRefs(Locators);
12927 }
12928 
12929 void OMPClauseReader::VisitOMPOrderClause(OMPOrderClause *C) {
12930   C->setKind(Record.readEnum<OpenMPOrderClauseKind>());
12931   C->setLParenLoc(Record.readSourceLocation());
12932   C->setKindKwLoc(Record.readSourceLocation());
12933 }
12934 
12935 OMPTraitInfo *ASTRecordReader::readOMPTraitInfo() {
12936   OMPTraitInfo &TI = getContext().getNewOMPTraitInfo();
12937   TI.Sets.resize(readUInt32());
12938   for (auto &Set : TI.Sets) {
12939     Set.Kind = readEnum<llvm::omp::TraitSet>();
12940     Set.Selectors.resize(readUInt32());
12941     for (auto &Selector : Set.Selectors) {
12942       Selector.Kind = readEnum<llvm::omp::TraitSelector>();
12943       Selector.ScoreOrCondition = nullptr;
12944       if (readBool())
12945         Selector.ScoreOrCondition = readExprRef();
12946       Selector.Properties.resize(readUInt32());
12947       for (auto &Property : Selector.Properties)
12948         Property.Kind = readEnum<llvm::omp::TraitProperty>();
12949     }
12950   }
12951   return &TI;
12952 }
12953 
12954 void ASTRecordReader::readOMPChildren(OMPChildren *Data) {
12955   if (!Data)
12956     return;
12957   if (Reader->ReadingKind == ASTReader::Read_Stmt) {
12958     // Skip NumClauses, NumChildren and HasAssociatedStmt fields.
12959     skipInts(3);
12960   }
12961   SmallVector<OMPClause *, 4> Clauses(Data->getNumClauses());
12962   for (unsigned I = 0, E = Data->getNumClauses(); I < E; ++I)
12963     Clauses[I] = readOMPClause();
12964   Data->setClauses(Clauses);
12965   if (Data->hasAssociatedStmt())
12966     Data->setAssociatedStmt(readStmt());
12967   for (unsigned I = 0, E = Data->getNumChildren(); I < E; ++I)
12968     Data->getChildren()[I] = readStmt();
12969 }
12970